raspberry-pi.patch 3.2 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454744557445674457744587445974460744617446274463744647446574466744677446874469744707447174472744737447474475744767447774478744797448074481744827448374484744857448674487744887448974490744917449274493744947449574496744977449874499745007450174502745037450474505745067450774508745097451074511745127451374514745157451674517745187451974520745217452274523745247452574526745277452874529745307453174532745337453474535745367453774538745397454074541745427454374544745457454674547745487454974550745517455274553745547455574556745577455874559745607456174562745637456474565745667456774568745697457074571745727457374574745757457674577745787457974580745817458274583745847458574586745877458874589745907459174592745937459474595745967459774598745997460074601746027460374604746057460674607746087460974610746117461274613746147461574616746177461874619746207462174622746237462474625746267462774628746297463074631746327463374634746357463674637746387463974640746417464274643746447464574646746477464874649746507465174652746537465474655746567465774658746597466074661746627466374664746657466674667746687466974670746717467274673746747467574676746777467874679746807468174682746837468474685746867468774688746897469074691746927469374694746957469674697746987469974700747017470274703747047470574706747077470874709747107471174712747137471474715747167471774718747197472074721747227472374724747257472674727747287472974730747317473274733747347473574736747377473874739747407474174742747437474474745747467474774748747497475074751747527475374754747557475674757747587475974760747617476274763747647476574766747677476874769747707477174772747737477474775747767477774778747797478074781747827478374784747857478674787747887478974790747917479274793747947479574796747977479874799748007480174802748037480474805748067480774808748097481074811748127481374814748157481674817748187481974820748217482274823748247482574826748277482874829748307483174832748337483474835748367483774838748397484074841748427484374844748457484674847748487484974850748517485274853748547485574856748577485874859748607486174862748637486474865748667486774868748697487074871748727487374874748757487674877748787487974880748817488274883748847488574886748877488874889748907489174892748937489474895748967489774898748997490074901749027490374904749057490674907749087490974910749117491274913749147491574916749177491874919749207492174922749237492474925749267492774928749297493074931749327493374934749357493674937749387493974940749417494274943749447494574946749477494874949749507495174952749537495474955749567495774958749597496074961749627496374964749657496674967749687496974970749717497274973749747497574976749777497874979749807498174982749837498474985749867498774988749897499074991749927499374994749957499674997749987499975000750017500275003750047500575006750077500875009750107501175012750137501475015750167501775018750197502075021750227502375024750257502675027750287502975030750317503275033750347503575036750377503875039750407504175042750437504475045750467504775048750497505075051750527505375054750557505675057750587505975060750617506275063750647506575066750677506875069750707507175072750737507475075750767507775078750797508075081750827508375084750857508675087750887508975090750917509275093750947509575096750977509875099751007510175102751037510475105751067510775108751097511075111751127511375114751157511675117751187511975120751217512275123751247512575126751277512875129751307513175132751337513475135751367513775138751397514075141751427514375144751457514675147751487514975150751517515275153751547515575156751577515875159751607516175162751637516475165751667516775168751697517075171751727517375174751757517675177751787517975180751817518275183751847518575186751877518875189751907519175192751937519475195751967519775198751997520075201752027520375204752057520675207752087520975210752117521275213752147521575216752177521875219752207522175222752237522475225752267522775228752297523075231752327523375234752357523675237752387523975240752417524275243752447524575246752477524875249752507525175252752537525475255752567525775258752597526075261752627526375264752657526675267752687526975270752717527275273752747527575276752777527875279752807528175282752837528475285752867528775288752897529075291752927529375294752957529675297752987529975300753017530275303753047530575306753077530875309753107531175312753137531475315753167531775318753197532075321753227532375324753257532675327753287532975330753317533275333753347533575336753377533875339753407534175342753437534475345753467534775348753497535075351753527535375354753557535675357753587535975360753617536275363753647536575366753677536875369753707537175372753737537475375753767537775378753797538075381753827538375384753857538675387753887538975390753917539275393753947539575396753977539875399754007540175402754037540475405754067540775408754097541075411754127541375414754157541675417754187541975420754217542275423754247542575426754277542875429754307543175432754337543475435754367543775438754397544075441754427544375444754457544675447754487544975450754517545275453754547545575456754577545875459754607546175462754637546475465754667546775468754697547075471754727547375474754757547675477754787547975480754817548275483754847548575486754877548875489754907549175492754937549475495754967549775498754997550075501755027550375504755057550675507755087550975510755117551275513755147551575516755177551875519755207552175522755237552475525755267552775528755297553075531755327553375534755357553675537755387553975540755417554275543755447554575546755477554875549755507555175552755537555475555755567555775558755597556075561755627556375564755657556675567755687556975570755717557275573755747557575576755777557875579755807558175582755837558475585755867558775588755897559075591755927559375594755957559675597755987559975600756017560275603756047560575606756077560875609756107561175612756137561475615756167561775618756197562075621756227562375624756257562675627756287562975630756317563275633756347563575636756377563875639756407564175642756437564475645756467564775648756497565075651756527565375654756557565675657756587565975660756617566275663756647566575666756677566875669756707567175672756737567475675756767567775678756797568075681756827568375684756857568675687756887568975690756917569275693756947569575696756977569875699757007570175702757037570475705757067570775708757097571075711757127571375714757157571675717757187571975720757217572275723757247572575726757277572875729757307573175732757337573475735757367573775738757397574075741757427574375744757457574675747757487574975750757517575275753757547575575756757577575875759757607576175762757637576475765757667576775768757697577075771757727577375774757757577675777757787577975780757817578275783757847578575786757877578875789757907579175792757937579475795757967579775798757997580075801758027580375804758057580675807758087580975810758117581275813758147581575816758177581875819758207582175822758237582475825758267582775828758297583075831758327583375834758357583675837758387583975840758417584275843758447584575846758477584875849758507585175852758537585475855758567585775858758597586075861758627586375864758657586675867758687586975870758717587275873758747587575876758777587875879758807588175882758837588475885758867588775888758897589075891758927589375894758957589675897758987589975900759017590275903759047590575906759077590875909759107591175912759137591475915759167591775918759197592075921759227592375924759257592675927759287592975930759317593275933759347593575936759377593875939759407594175942759437594475945759467594775948759497595075951759527595375954759557595675957759587595975960759617596275963759647596575966759677596875969759707597175972759737597475975759767597775978759797598075981759827598375984759857598675987759887598975990759917599275993759947599575996759977599875999760007600176002760037600476005760067600776008760097601076011760127601376014760157601676017760187601976020760217602276023760247602576026760277602876029760307603176032760337603476035760367603776038760397604076041760427604376044760457604676047760487604976050760517605276053760547605576056760577605876059760607606176062760637606476065760667606776068760697607076071760727607376074760757607676077760787607976080760817608276083760847608576086760877608876089760907609176092760937609476095760967609776098760997610076101761027610376104761057610676107761087610976110761117611276113761147611576116761177611876119761207612176122761237612476125761267612776128761297613076131761327613376134761357613676137761387613976140761417614276143761447614576146761477614876149761507615176152761537615476155761567615776158761597616076161761627616376164761657616676167761687616976170761717617276173761747617576176761777617876179761807618176182761837618476185761867618776188761897619076191761927619376194761957619676197761987619976200762017620276203762047620576206762077620876209762107621176212762137621476215762167621776218762197622076221762227622376224762257622676227762287622976230762317623276233762347623576236762377623876239762407624176242762437624476245762467624776248762497625076251762527625376254762557625676257762587625976260762617626276263762647626576266762677626876269762707627176272762737627476275762767627776278762797628076281762827628376284762857628676287762887628976290762917629276293762947629576296762977629876299763007630176302763037630476305763067630776308763097631076311763127631376314763157631676317763187631976320763217632276323763247632576326763277632876329763307633176332763337633476335763367633776338763397634076341763427634376344763457634676347763487634976350763517635276353763547635576356763577635876359763607636176362763637636476365763667636776368763697637076371763727637376374763757637676377763787637976380763817638276383763847638576386763877638876389763907639176392763937639476395763967639776398763997640076401764027640376404764057640676407764087640976410764117641276413764147641576416764177641876419764207642176422764237642476425764267642776428764297643076431764327643376434764357643676437764387643976440764417644276443764447644576446764477644876449764507645176452764537645476455764567645776458764597646076461764627646376464764657646676467764687646976470764717647276473764747647576476764777647876479764807648176482764837648476485764867648776488764897649076491764927649376494764957649676497764987649976500765017650276503765047650576506765077650876509765107651176512765137651476515765167651776518765197652076521765227652376524765257652676527765287652976530765317653276533765347653576536765377653876539765407654176542765437654476545765467654776548765497655076551765527655376554765557655676557765587655976560765617656276563765647656576566765677656876569765707657176572765737657476575765767657776578765797658076581765827658376584765857658676587765887658976590765917659276593765947659576596765977659876599766007660176602766037660476605766067660776608766097661076611766127661376614766157661676617766187661976620766217662276623766247662576626766277662876629766307663176632766337663476635766367663776638766397664076641766427664376644766457664676647766487664976650766517665276653766547665576656766577665876659766607666176662766637666476665766667666776668766697667076671766727667376674766757667676677766787667976680766817668276683766847668576686766877668876689766907669176692766937669476695766967669776698766997670076701767027670376704767057670676707767087670976710767117671276713767147671576716767177671876719767207672176722767237672476725767267672776728767297673076731767327673376734767357673676737767387673976740767417674276743767447674576746767477674876749767507675176752767537675476755767567675776758767597676076761767627676376764767657676676767767687676976770767717677276773767747677576776767777677876779767807678176782767837678476785767867678776788767897679076791767927679376794767957679676797767987679976800768017680276803768047680576806768077680876809768107681176812768137681476815768167681776818768197682076821768227682376824768257682676827768287682976830768317683276833768347683576836768377683876839768407684176842768437684476845768467684776848768497685076851768527685376854768557685676857768587685976860768617686276863768647686576866768677686876869768707687176872768737687476875768767687776878768797688076881768827688376884768857688676887768887688976890768917689276893768947689576896768977689876899769007690176902769037690476905769067690776908769097691076911769127691376914769157691676917769187691976920769217692276923769247692576926769277692876929769307693176932769337693476935769367693776938769397694076941769427694376944769457694676947769487694976950769517695276953769547695576956769577695876959769607696176962769637696476965769667696776968769697697076971769727697376974769757697676977769787697976980769817698276983769847698576986769877698876989769907699176992769937699476995769967699776998769997700077001770027700377004770057700677007770087700977010770117701277013770147701577016770177701877019770207702177022770237702477025770267702777028770297703077031770327703377034770357703677037770387703977040770417704277043770447704577046770477704877049770507705177052770537705477055770567705777058770597706077061770627706377064770657706677067770687706977070770717707277073770747707577076770777707877079770807708177082770837708477085770867708777088770897709077091770927709377094770957709677097770987709977100771017710277103771047710577106771077710877109771107711177112771137711477115771167711777118771197712077121771227712377124771257712677127771287712977130771317713277133771347713577136771377713877139771407714177142771437714477145771467714777148771497715077151771527715377154771557715677157771587715977160771617716277163771647716577166771677716877169771707717177172771737717477175771767717777178771797718077181771827718377184771857718677187771887718977190771917719277193771947719577196771977719877199772007720177202772037720477205772067720777208772097721077211772127721377214772157721677217772187721977220772217722277223772247722577226772277722877229772307723177232772337723477235772367723777238772397724077241772427724377244772457724677247772487724977250772517725277253772547725577256772577725877259772607726177262772637726477265772667726777268772697727077271772727727377274772757727677277772787727977280772817728277283772847728577286772877728877289772907729177292772937729477295772967729777298772997730077301773027730377304773057730677307773087730977310773117731277313773147731577316773177731877319773207732177322773237732477325773267732777328773297733077331773327733377334773357733677337773387733977340773417734277343773447734577346773477734877349773507735177352773537735477355773567735777358773597736077361773627736377364773657736677367773687736977370773717737277373773747737577376773777737877379773807738177382773837738477385773867738777388773897739077391773927739377394773957739677397773987739977400774017740277403774047740577406774077740877409774107741177412774137741477415774167741777418774197742077421774227742377424774257742677427774287742977430774317743277433774347743577436774377743877439774407744177442774437744477445774467744777448774497745077451774527745377454774557745677457774587745977460774617746277463774647746577466774677746877469774707747177472774737747477475774767747777478774797748077481774827748377484774857748677487774887748977490774917749277493774947749577496774977749877499775007750177502775037750477505775067750777508775097751077511775127751377514775157751677517775187751977520775217752277523775247752577526775277752877529775307753177532775337753477535775367753777538775397754077541775427754377544775457754677547775487754977550775517755277553775547755577556775577755877559775607756177562775637756477565775667756777568775697757077571775727757377574775757757677577775787757977580775817758277583775847758577586775877758877589775907759177592775937759477595775967759777598775997760077601776027760377604776057760677607776087760977610776117761277613776147761577616776177761877619776207762177622776237762477625776267762777628776297763077631776327763377634776357763677637776387763977640776417764277643776447764577646776477764877649776507765177652776537765477655776567765777658776597766077661776627766377664776657766677667776687766977670776717767277673776747767577676776777767877679776807768177682776837768477685776867768777688776897769077691776927769377694776957769677697776987769977700777017770277703777047770577706777077770877709777107771177712777137771477715777167771777718777197772077721777227772377724777257772677727777287772977730777317773277733777347773577736777377773877739777407774177742777437774477745777467774777748777497775077751777527775377754777557775677757777587775977760777617776277763777647776577766777677776877769777707777177772777737777477775777767777777778777797778077781777827778377784777857778677787777887778977790777917779277793777947779577796777977779877799778007780177802778037780477805778067780777808778097781077811778127781377814778157781677817778187781977820778217782277823778247782577826778277782877829778307783177832778337783477835778367783777838778397784077841778427784377844778457784677847778487784977850778517785277853778547785577856778577785877859778607786177862778637786477865778667786777868778697787077871778727787377874778757787677877778787787977880778817788277883778847788577886778877788877889778907789177892778937789477895778967789777898778997790077901779027790377904779057790677907779087790977910779117791277913779147791577916779177791877919779207792177922779237792477925779267792777928779297793077931779327793377934779357793677937779387793977940779417794277943779447794577946779477794877949779507795177952779537795477955779567795777958779597796077961779627796377964779657796677967779687796977970779717797277973779747797577976779777797877979779807798177982779837798477985779867798777988779897799077991779927799377994779957799677997779987799978000780017800278003780047800578006780077800878009780107801178012780137801478015780167801778018780197802078021780227802378024780257802678027780287802978030780317803278033780347803578036780377803878039780407804178042780437804478045780467804778048780497805078051780527805378054780557805678057780587805978060780617806278063780647806578066780677806878069780707807178072780737807478075780767807778078780797808078081780827808378084780857808678087780887808978090780917809278093780947809578096780977809878099781007810178102781037810478105781067810778108781097811078111781127811378114781157811678117781187811978120781217812278123781247812578126781277812878129781307813178132781337813478135781367813778138781397814078141781427814378144781457814678147781487814978150781517815278153781547815578156781577815878159781607816178162781637816478165781667816778168781697817078171781727817378174781757817678177781787817978180781817818278183781847818578186781877818878189781907819178192781937819478195781967819778198781997820078201782027820378204782057820678207782087820978210782117821278213782147821578216782177821878219782207822178222782237822478225782267822778228782297823078231782327823378234782357823678237782387823978240782417824278243782447824578246782477824878249782507825178252782537825478255782567825778258782597826078261782627826378264782657826678267782687826978270782717827278273782747827578276782777827878279782807828178282782837828478285782867828778288782897829078291782927829378294782957829678297782987829978300783017830278303783047830578306783077830878309783107831178312783137831478315783167831778318783197832078321783227832378324783257832678327783287832978330783317833278333783347833578336783377833878339783407834178342783437834478345783467834778348783497835078351783527835378354783557835678357783587835978360783617836278363783647836578366783677836878369783707837178372783737837478375783767837778378783797838078381783827838378384783857838678387783887838978390783917839278393783947839578396783977839878399784007840178402784037840478405784067840778408784097841078411784127841378414784157841678417784187841978420784217842278423784247842578426784277842878429784307843178432784337843478435784367843778438784397844078441784427844378444784457844678447784487844978450784517845278453784547845578456784577845878459784607846178462784637846478465784667846778468784697847078471784727847378474784757847678477784787847978480784817848278483784847848578486784877848878489784907849178492784937849478495784967849778498784997850078501785027850378504785057850678507785087850978510785117851278513785147851578516785177851878519785207852178522785237852478525785267852778528785297853078531785327853378534785357853678537785387853978540785417854278543785447854578546785477854878549785507855178552785537855478555785567855778558785597856078561785627856378564785657856678567785687856978570785717857278573785747857578576785777857878579785807858178582785837858478585785867858778588785897859078591785927859378594785957859678597785987859978600786017860278603786047860578606786077860878609786107861178612786137861478615786167861778618786197862078621786227862378624786257862678627786287862978630786317863278633786347863578636786377863878639786407864178642786437864478645786467864778648786497865078651786527865378654786557865678657786587865978660786617866278663786647866578666786677866878669786707867178672786737867478675786767867778678786797868078681786827868378684786857868678687786887868978690786917869278693786947869578696786977869878699787007870178702787037870478705787067870778708787097871078711787127871378714787157871678717787187871978720787217872278723787247872578726787277872878729787307873178732787337873478735787367873778738787397874078741787427874378744787457874678747787487874978750787517875278753787547875578756787577875878759787607876178762787637876478765787667876778768787697877078771787727877378774787757877678777787787877978780787817878278783787847878578786787877878878789787907879178792787937879478795787967879778798787997880078801788027880378804788057880678807788087880978810788117881278813788147881578816788177881878819788207882178822788237882478825788267882778828788297883078831788327883378834788357883678837788387883978840788417884278843788447884578846788477884878849788507885178852788537885478855788567885778858788597886078861788627886378864788657886678867788687886978870788717887278873788747887578876788777887878879788807888178882788837888478885788867888778888788897889078891788927889378894788957889678897788987889978900789017890278903789047890578906789077890878909789107891178912789137891478915789167891778918789197892078921789227892378924789257892678927789287892978930789317893278933789347893578936789377893878939789407894178942789437894478945789467894778948789497895078951789527895378954789557895678957789587895978960789617896278963789647896578966789677896878969789707897178972789737897478975789767897778978789797898078981789827898378984789857898678987789887898978990789917899278993789947899578996789977899878999790007900179002790037900479005790067900779008790097901079011790127901379014790157901679017790187901979020790217902279023790247902579026790277902879029790307903179032790337903479035790367903779038790397904079041790427904379044790457904679047790487904979050790517905279053790547905579056790577905879059790607906179062790637906479065790667906779068790697907079071790727907379074790757907679077790787907979080790817908279083790847908579086790877908879089790907909179092790937909479095790967909779098790997910079101791027910379104791057910679107791087910979110791117911279113791147911579116791177911879119791207912179122791237912479125791267912779128791297913079131791327913379134791357913679137791387913979140791417914279143791447914579146791477914879149791507915179152791537915479155791567915779158791597916079161791627916379164791657916679167791687916979170791717917279173791747917579176791777917879179791807918179182791837918479185791867918779188791897919079191791927919379194791957919679197791987919979200792017920279203792047920579206792077920879209792107921179212792137921479215792167921779218792197922079221792227922379224792257922679227792287922979230792317923279233792347923579236792377923879239792407924179242792437924479245792467924779248792497925079251792527925379254792557925679257792587925979260792617926279263792647926579266792677926879269792707927179272792737927479275792767927779278792797928079281792827928379284792857928679287792887928979290792917929279293792947929579296792977929879299793007930179302793037930479305793067930779308793097931079311793127931379314793157931679317793187931979320793217932279323793247932579326793277932879329793307933179332793337933479335793367933779338793397934079341793427934379344793457934679347793487934979350793517935279353793547935579356793577935879359793607936179362793637936479365793667936779368793697937079371793727937379374793757937679377793787937979380793817938279383793847938579386793877938879389793907939179392793937939479395793967939779398793997940079401794027940379404794057940679407794087940979410794117941279413794147941579416794177941879419794207942179422794237942479425794267942779428794297943079431794327943379434794357943679437794387943979440794417944279443794447944579446794477944879449794507945179452794537945479455794567945779458794597946079461794627946379464794657946679467794687946979470794717947279473794747947579476794777947879479794807948179482794837948479485794867948779488794897949079491794927949379494794957949679497794987949979500795017950279503795047950579506795077950879509795107951179512795137951479515795167951779518795197952079521795227952379524795257952679527795287952979530795317953279533795347953579536795377953879539795407954179542795437954479545795467954779548795497955079551795527955379554795557955679557795587955979560795617956279563795647956579566795677956879569795707957179572795737957479575795767957779578795797958079581795827958379584795857958679587795887958979590795917959279593795947959579596795977959879599796007960179602796037960479605796067960779608796097961079611796127961379614796157961679617796187961979620796217962279623796247962579626796277962879629796307963179632796337963479635796367963779638796397964079641796427964379644796457964679647796487964979650796517965279653796547965579656796577965879659796607966179662796637966479665796667966779668796697967079671796727967379674796757967679677796787967979680796817968279683796847968579686796877968879689796907969179692796937969479695796967969779698796997970079701797027970379704797057970679707797087970979710797117971279713797147971579716797177971879719797207972179722797237972479725797267972779728797297973079731797327973379734797357973679737797387973979740797417974279743797447974579746797477974879749797507975179752797537975479755797567975779758797597976079761797627976379764797657976679767797687976979770797717977279773797747977579776797777977879779797807978179782797837978479785797867978779788797897979079791797927979379794797957979679797797987979979800798017980279803798047980579806798077980879809798107981179812798137981479815798167981779818798197982079821798227982379824798257982679827798287982979830798317983279833798347983579836798377983879839798407984179842798437984479845798467984779848798497985079851798527985379854798557985679857798587985979860798617986279863798647986579866798677986879869798707987179872798737987479875798767987779878798797988079881798827988379884798857988679887798887988979890798917989279893798947989579896798977989879899799007990179902799037990479905799067990779908799097991079911799127991379914799157991679917799187991979920799217992279923799247992579926799277992879929799307993179932799337993479935799367993779938799397994079941799427994379944799457994679947799487994979950799517995279953799547995579956799577995879959799607996179962799637996479965799667996779968799697997079971799727997379974799757997679977799787997979980799817998279983799847998579986799877998879989799907999179992799937999479995799967999779998799998000080001800028000380004800058000680007800088000980010800118001280013800148001580016800178001880019800208002180022800238002480025800268002780028800298003080031800328003380034800358003680037800388003980040800418004280043800448004580046800478004880049800508005180052800538005480055800568005780058800598006080061800628006380064800658006680067800688006980070800718007280073800748007580076800778007880079800808008180082800838008480085800868008780088800898009080091800928009380094800958009680097800988009980100801018010280103801048010580106801078010880109801108011180112801138011480115801168011780118801198012080121801228012380124801258012680127801288012980130801318013280133801348013580136801378013880139801408014180142801438014480145801468014780148801498015080151801528015380154801558015680157801588015980160801618016280163801648016580166801678016880169801708017180172801738017480175801768017780178801798018080181801828018380184801858018680187801888018980190801918019280193801948019580196801978019880199802008020180202802038020480205802068020780208802098021080211802128021380214802158021680217802188021980220802218022280223802248022580226802278022880229802308023180232802338023480235802368023780238802398024080241802428024380244802458024680247802488024980250802518025280253802548025580256802578025880259802608026180262802638026480265802668026780268802698027080271802728027380274802758027680277802788027980280802818028280283802848028580286802878028880289802908029180292802938029480295802968029780298802998030080301803028030380304803058030680307803088030980310803118031280313803148031580316803178031880319803208032180322803238032480325803268032780328803298033080331803328033380334803358033680337803388033980340803418034280343803448034580346803478034880349803508035180352803538035480355803568035780358803598036080361803628036380364803658036680367803688036980370803718037280373803748037580376803778037880379803808038180382803838038480385803868038780388803898039080391803928039380394803958039680397803988039980400804018040280403804048040580406804078040880409804108041180412804138041480415804168041780418804198042080421804228042380424804258042680427804288042980430804318043280433804348043580436804378043880439804408044180442804438044480445804468044780448804498045080451804528045380454804558045680457804588045980460804618046280463804648046580466804678046880469804708047180472804738047480475804768047780478804798048080481804828048380484804858048680487804888048980490804918049280493804948049580496804978049880499805008050180502805038050480505805068050780508805098051080511805128051380514805158051680517805188051980520805218052280523805248052580526805278052880529805308053180532805338053480535805368053780538805398054080541805428054380544805458054680547805488054980550805518055280553805548055580556805578055880559805608056180562805638056480565805668056780568805698057080571805728057380574805758057680577805788057980580805818058280583805848058580586805878058880589805908059180592805938059480595805968059780598805998060080601806028060380604806058060680607806088060980610806118061280613806148061580616806178061880619806208062180622806238062480625806268062780628806298063080631806328063380634806358063680637806388063980640806418064280643806448064580646806478064880649806508065180652806538065480655806568065780658806598066080661806628066380664806658066680667806688066980670806718067280673806748067580676806778067880679806808068180682806838068480685806868068780688806898069080691806928069380694806958069680697806988069980700807018070280703807048070580706807078070880709807108071180712807138071480715807168071780718807198072080721807228072380724807258072680727807288072980730807318073280733807348073580736807378073880739807408074180742807438074480745807468074780748807498075080751807528075380754807558075680757807588075980760807618076280763807648076580766807678076880769807708077180772807738077480775807768077780778807798078080781807828078380784807858078680787807888078980790807918079280793807948079580796807978079880799808008080180802808038080480805808068080780808808098081080811808128081380814808158081680817808188081980820808218082280823808248082580826808278082880829808308083180832808338083480835808368083780838808398084080841808428084380844808458084680847808488084980850808518085280853808548085580856808578085880859808608086180862808638086480865808668086780868808698087080871808728087380874808758087680877808788087980880808818088280883808848088580886808878088880889808908089180892808938089480895808968089780898808998090080901809028090380904809058090680907809088090980910809118091280913809148091580916809178091880919809208092180922809238092480925809268092780928809298093080931809328093380934809358093680937809388093980940809418094280943809448094580946809478094880949809508095180952809538095480955809568095780958809598096080961809628096380964809658096680967809688096980970809718097280973809748097580976809778097880979809808098180982809838098480985809868098780988809898099080991809928099380994809958099680997809988099981000810018100281003810048100581006810078100881009810108101181012810138101481015810168101781018810198102081021810228102381024810258102681027810288102981030810318103281033810348103581036810378103881039810408104181042810438104481045810468104781048810498105081051810528105381054810558105681057810588105981060810618106281063810648106581066810678106881069810708107181072810738107481075810768107781078810798108081081810828108381084810858108681087810888108981090810918109281093810948109581096810978109881099811008110181102811038110481105811068110781108811098111081111811128111381114811158111681117811188111981120811218112281123811248112581126811278112881129811308113181132811338113481135811368113781138811398114081141811428114381144811458114681147811488114981150811518115281153811548115581156811578115881159811608116181162811638116481165811668116781168811698117081171811728117381174811758117681177811788117981180811818118281183811848118581186811878118881189811908119181192811938119481195811968119781198811998120081201812028120381204812058120681207812088120981210812118121281213812148121581216812178121881219812208122181222812238122481225812268122781228812298123081231812328123381234812358123681237812388123981240812418124281243812448124581246812478124881249812508125181252812538125481255812568125781258812598126081261812628126381264812658126681267812688126981270812718127281273812748127581276812778127881279812808128181282812838128481285812868128781288812898129081291812928129381294812958129681297812988129981300813018130281303813048130581306813078130881309813108131181312813138131481315813168131781318813198132081321813228132381324813258132681327813288132981330813318133281333813348133581336813378133881339813408134181342813438134481345813468134781348813498135081351813528135381354813558135681357813588135981360813618136281363813648136581366813678136881369813708137181372813738137481375813768137781378813798138081381813828138381384813858138681387813888138981390813918139281393813948139581396813978139881399814008140181402814038140481405814068140781408814098141081411814128141381414814158141681417814188141981420814218142281423814248142581426814278142881429814308143181432814338143481435814368143781438814398144081441814428144381444814458144681447814488144981450814518145281453814548145581456814578145881459814608146181462814638146481465814668146781468814698147081471814728147381474814758147681477814788147981480814818148281483814848148581486814878148881489814908149181492814938149481495814968149781498814998150081501815028150381504815058150681507815088150981510815118151281513815148151581516815178151881519815208152181522815238152481525815268152781528815298153081531815328153381534815358153681537815388153981540815418154281543815448154581546815478154881549815508155181552815538155481555815568155781558815598156081561815628156381564815658156681567815688156981570815718157281573815748157581576815778157881579815808158181582815838158481585815868158781588815898159081591815928159381594815958159681597815988159981600816018160281603816048160581606816078160881609816108161181612816138161481615816168161781618816198162081621816228162381624816258162681627816288162981630816318163281633816348163581636816378163881639816408164181642816438164481645816468164781648816498165081651816528165381654816558165681657816588165981660816618166281663816648166581666816678166881669816708167181672816738167481675816768167781678816798168081681816828168381684816858168681687816888168981690816918169281693816948169581696816978169881699817008170181702817038170481705817068170781708817098171081711817128171381714817158171681717817188171981720817218172281723817248172581726817278172881729817308173181732817338173481735817368173781738817398174081741817428174381744817458174681747817488174981750817518175281753817548175581756817578175881759817608176181762817638176481765817668176781768817698177081771817728177381774817758177681777817788177981780817818178281783817848178581786817878178881789817908179181792817938179481795817968179781798817998180081801818028180381804818058180681807818088180981810818118181281813818148181581816818178181881819818208182181822818238182481825818268182781828818298183081831818328183381834818358183681837818388183981840818418184281843818448184581846818478184881849818508185181852818538185481855818568185781858818598186081861818628186381864818658186681867818688186981870818718187281873818748187581876818778187881879818808188181882818838188481885818868188781888818898189081891818928189381894818958189681897818988189981900819018190281903819048190581906819078190881909819108191181912819138191481915819168191781918819198192081921819228192381924819258192681927819288192981930819318193281933819348193581936819378193881939819408194181942819438194481945819468194781948819498195081951819528195381954819558195681957819588195981960819618196281963819648196581966819678196881969819708197181972819738197481975819768197781978819798198081981819828198381984819858198681987819888198981990819918199281993819948199581996819978199881999820008200182002820038200482005820068200782008820098201082011820128201382014820158201682017820188201982020820218202282023820248202582026820278202882029820308203182032820338203482035820368203782038820398204082041820428204382044820458204682047820488204982050820518205282053820548205582056820578205882059820608206182062820638206482065820668206782068820698207082071820728207382074820758207682077820788207982080820818208282083820848208582086820878208882089820908209182092820938209482095820968209782098820998210082101821028210382104821058210682107821088210982110821118211282113821148211582116821178211882119821208212182122821238212482125821268212782128821298213082131821328213382134821358213682137821388213982140821418214282143821448214582146821478214882149821508215182152821538215482155821568215782158821598216082161821628216382164821658216682167821688216982170821718217282173821748217582176821778217882179821808218182182821838218482185821868218782188821898219082191821928219382194821958219682197821988219982200822018220282203822048220582206822078220882209822108221182212822138221482215822168221782218822198222082221822228222382224822258222682227822288222982230822318223282233822348223582236822378223882239822408224182242822438224482245822468224782248822498225082251822528225382254822558225682257822588225982260822618226282263822648226582266822678226882269822708227182272822738227482275822768227782278822798228082281822828228382284822858228682287822888228982290822918229282293822948229582296822978229882299823008230182302823038230482305823068230782308823098231082311823128231382314823158231682317823188231982320823218232282323823248232582326823278232882329823308233182332823338233482335823368233782338823398234082341823428234382344823458234682347823488234982350823518235282353823548235582356823578235882359823608236182362823638236482365823668236782368823698237082371823728237382374823758237682377823788237982380823818238282383823848238582386823878238882389823908239182392823938239482395823968239782398823998240082401824028240382404824058240682407824088240982410824118241282413824148241582416824178241882419824208242182422824238242482425824268242782428824298243082431824328243382434824358243682437824388243982440824418244282443824448244582446824478244882449824508245182452824538245482455824568245782458824598246082461824628246382464824658246682467824688246982470824718247282473824748247582476824778247882479824808248182482824838248482485824868248782488824898249082491824928249382494824958249682497824988249982500825018250282503825048250582506825078250882509825108251182512825138251482515825168251782518825198252082521825228252382524825258252682527825288252982530825318253282533825348253582536825378253882539825408254182542825438254482545825468254782548825498255082551825528255382554825558255682557825588255982560825618256282563825648256582566825678256882569825708257182572825738257482575825768257782578825798258082581825828258382584825858258682587825888258982590825918259282593825948259582596825978259882599826008260182602826038260482605826068260782608826098261082611826128261382614826158261682617826188261982620826218262282623826248262582626826278262882629826308263182632826338263482635826368263782638826398264082641826428264382644826458264682647826488264982650826518265282653826548265582656826578265882659826608266182662826638266482665826668266782668826698267082671826728267382674826758267682677826788267982680826818268282683826848268582686826878268882689826908269182692826938269482695826968269782698826998270082701827028270382704827058270682707827088270982710827118271282713827148271582716827178271882719827208272182722827238272482725827268272782728827298273082731827328273382734827358273682737827388273982740827418274282743827448274582746827478274882749827508275182752827538275482755827568275782758827598276082761827628276382764827658276682767827688276982770827718277282773827748277582776827778277882779827808278182782827838278482785827868278782788827898279082791827928279382794827958279682797827988279982800828018280282803828048280582806828078280882809828108281182812828138281482815828168281782818828198282082821828228282382824828258282682827828288282982830828318283282833828348283582836828378283882839828408284182842828438284482845828468284782848828498285082851828528285382854828558285682857828588285982860828618286282863828648286582866828678286882869828708287182872828738287482875828768287782878828798288082881828828288382884828858288682887828888288982890828918289282893828948289582896828978289882899829008290182902829038290482905829068290782908829098291082911829128291382914829158291682917829188291982920829218292282923829248292582926829278292882929829308293182932829338293482935829368293782938829398294082941829428294382944829458294682947829488294982950829518295282953829548295582956829578295882959829608296182962829638296482965829668296782968829698297082971829728297382974829758297682977829788297982980829818298282983829848298582986829878298882989829908299182992829938299482995829968299782998829998300083001830028300383004830058300683007830088300983010830118301283013830148301583016830178301883019830208302183022830238302483025830268302783028830298303083031830328303383034830358303683037830388303983040830418304283043830448304583046830478304883049830508305183052830538305483055830568305783058830598306083061830628306383064830658306683067830688306983070830718307283073830748307583076830778307883079830808308183082830838308483085830868308783088830898309083091830928309383094830958309683097830988309983100831018310283103831048310583106831078310883109831108311183112831138311483115831168311783118831198312083121831228312383124831258312683127831288312983130831318313283133831348313583136831378313883139831408314183142831438314483145831468314783148831498315083151831528315383154831558315683157831588315983160831618316283163831648316583166831678316883169831708317183172831738317483175831768317783178831798318083181831828318383184831858318683187831888318983190831918319283193831948319583196831978319883199832008320183202832038320483205832068320783208832098321083211832128321383214832158321683217832188321983220832218322283223832248322583226832278322883229832308323183232832338323483235832368323783238832398324083241832428324383244832458324683247832488324983250832518325283253832548325583256832578325883259832608326183262832638326483265832668326783268832698327083271832728327383274832758327683277832788327983280832818328283283832848328583286832878328883289832908329183292832938329483295832968329783298832998330083301833028330383304833058330683307833088330983310833118331283313833148331583316833178331883319833208332183322833238332483325833268332783328833298333083331833328333383334833358333683337833388333983340833418334283343833448334583346833478334883349833508335183352833538335483355833568335783358833598336083361833628336383364833658336683367833688336983370833718337283373833748337583376833778337883379833808338183382833838338483385833868338783388833898339083391833928339383394833958339683397833988339983400834018340283403834048340583406834078340883409834108341183412834138341483415834168341783418834198342083421834228342383424834258342683427834288342983430834318343283433834348343583436834378343883439834408344183442834438344483445834468344783448834498345083451834528345383454834558345683457834588345983460834618346283463834648346583466834678346883469834708347183472834738347483475834768347783478834798348083481834828348383484834858348683487834888348983490834918349283493834948349583496834978349883499835008350183502835038350483505835068350783508835098351083511835128351383514835158351683517835188351983520835218352283523835248352583526835278352883529835308353183532835338353483535835368353783538835398354083541835428354383544835458354683547835488354983550835518355283553835548355583556835578355883559835608356183562835638356483565835668356783568835698357083571835728357383574835758357683577835788357983580835818358283583835848358583586835878358883589835908359183592835938359483595835968359783598835998360083601836028360383604836058360683607836088360983610836118361283613836148361583616836178361883619836208362183622836238362483625836268362783628836298363083631836328363383634836358363683637836388363983640836418364283643836448364583646836478364883649836508365183652836538365483655836568365783658836598366083661836628366383664836658366683667836688366983670836718367283673836748367583676836778367883679836808368183682836838368483685836868368783688836898369083691836928369383694836958369683697836988369983700837018370283703837048370583706837078370883709837108371183712837138371483715837168371783718837198372083721837228372383724837258372683727837288372983730837318373283733837348373583736837378373883739837408374183742837438374483745837468374783748837498375083751837528375383754837558375683757837588375983760837618376283763837648376583766837678376883769837708377183772837738377483775837768377783778837798378083781837828378383784837858378683787837888378983790837918379283793837948379583796837978379883799838008380183802838038380483805838068380783808838098381083811838128381383814838158381683817838188381983820838218382283823838248382583826838278382883829838308383183832838338383483835838368383783838838398384083841838428384383844838458384683847838488384983850838518385283853838548385583856838578385883859838608386183862838638386483865838668386783868838698387083871838728387383874838758387683877838788387983880838818388283883838848388583886838878388883889838908389183892838938389483895838968389783898838998390083901839028390383904839058390683907839088390983910839118391283913839148391583916839178391883919839208392183922839238392483925839268392783928839298393083931839328393383934839358393683937839388393983940839418394283943839448394583946839478394883949839508395183952839538395483955839568395783958839598396083961839628396383964839658396683967839688396983970839718397283973839748397583976839778397883979839808398183982839838398483985839868398783988839898399083991839928399383994839958399683997839988399984000840018400284003840048400584006840078400884009840108401184012840138401484015840168401784018840198402084021840228402384024840258402684027840288402984030840318403284033840348403584036840378403884039840408404184042840438404484045840468404784048840498405084051840528405384054840558405684057840588405984060840618406284063840648406584066840678406884069840708407184072840738407484075840768407784078840798408084081840828408384084840858408684087840888408984090840918409284093840948409584096840978409884099841008410184102841038410484105841068410784108841098411084111841128411384114841158411684117841188411984120841218412284123841248412584126841278412884129841308413184132841338413484135841368413784138841398414084141841428414384144841458414684147841488414984150841518415284153841548415584156841578415884159841608416184162841638416484165841668416784168841698417084171841728417384174841758417684177841788417984180841818418284183841848418584186841878418884189841908419184192841938419484195841968419784198841998420084201842028420384204842058420684207842088420984210842118421284213842148421584216842178421884219842208422184222842238422484225842268422784228842298423084231842328423384234842358423684237842388423984240842418424284243842448424584246842478424884249842508425184252842538425484255842568425784258842598426084261842628426384264842658426684267842688426984270842718427284273842748427584276842778427884279842808428184282842838428484285842868428784288842898429084291842928429384294842958429684297842988429984300843018430284303843048430584306843078430884309843108431184312843138431484315843168431784318843198432084321843228432384324843258432684327843288432984330843318433284333843348433584336843378433884339843408434184342843438434484345843468434784348843498435084351843528435384354843558435684357843588435984360843618436284363843648436584366843678436884369843708437184372843738437484375843768437784378843798438084381843828438384384843858438684387843888438984390843918439284393843948439584396843978439884399844008440184402844038440484405844068440784408844098441084411844128441384414844158441684417844188441984420844218442284423844248442584426844278442884429844308443184432844338443484435844368443784438844398444084441844428444384444844458444684447844488444984450844518445284453844548445584456844578445884459844608446184462844638446484465844668446784468844698447084471844728447384474844758447684477844788447984480844818448284483844848448584486844878448884489844908449184492844938449484495844968449784498844998450084501845028450384504845058450684507845088450984510845118451284513845148451584516845178451884519845208452184522845238452484525845268452784528845298453084531845328453384534845358453684537845388453984540845418454284543845448454584546845478454884549845508455184552845538455484555845568455784558845598456084561845628456384564845658456684567845688456984570845718457284573845748457584576845778457884579845808458184582845838458484585845868458784588845898459084591845928459384594845958459684597845988459984600846018460284603846048460584606846078460884609846108461184612846138461484615846168461784618846198462084621846228462384624846258462684627846288462984630846318463284633846348463584636846378463884639846408464184642846438464484645846468464784648846498465084651846528465384654846558465684657846588465984660846618466284663846648466584666846678466884669846708467184672846738467484675846768467784678846798468084681846828468384684846858468684687846888468984690846918469284693846948469584696846978469884699847008470184702847038470484705847068470784708847098471084711847128471384714847158471684717847188471984720847218472284723847248472584726847278472884729847308473184732847338473484735847368473784738847398474084741847428474384744847458474684747847488474984750847518475284753847548475584756847578475884759847608476184762847638476484765847668476784768847698477084771847728477384774847758477684777847788477984780847818478284783847848478584786847878478884789847908479184792847938479484795847968479784798847998480084801848028480384804848058480684807848088480984810848118481284813848148481584816848178481884819848208482184822848238482484825848268482784828848298483084831848328483384834848358483684837848388483984840848418484284843848448484584846848478484884849848508485184852848538485484855848568485784858848598486084861848628486384864848658486684867848688486984870848718487284873848748487584876848778487884879848808488184882848838488484885848868488784888848898489084891848928489384894848958489684897848988489984900849018490284903849048490584906849078490884909849108491184912849138491484915849168491784918849198492084921849228492384924849258492684927849288492984930849318493284933849348493584936849378493884939849408494184942849438494484945849468494784948849498495084951849528495384954849558495684957849588495984960849618496284963849648496584966849678496884969849708497184972849738497484975849768497784978849798498084981849828498384984849858498684987849888498984990849918499284993849948499584996849978499884999850008500185002850038500485005850068500785008850098501085011850128501385014850158501685017850188501985020850218502285023850248502585026850278502885029850308503185032850338503485035850368503785038850398504085041850428504385044850458504685047850488504985050850518505285053850548505585056850578505885059850608506185062850638506485065850668506785068850698507085071850728507385074850758507685077850788507985080850818508285083850848508585086850878508885089850908509185092850938509485095850968509785098850998510085101851028510385104851058510685107851088510985110851118511285113851148511585116851178511885119851208512185122851238512485125851268512785128851298513085131851328513385134851358513685137851388513985140851418514285143851448514585146851478514885149851508515185152851538515485155851568515785158851598516085161851628516385164851658516685167851688516985170851718517285173851748517585176851778517885179851808518185182851838518485185851868518785188851898519085191851928519385194851958519685197851988519985200852018520285203852048520585206852078520885209852108521185212852138521485215852168521785218852198522085221852228522385224852258522685227852288522985230852318523285233852348523585236852378523885239852408524185242852438524485245852468524785248852498525085251852528525385254852558525685257852588525985260852618526285263852648526585266852678526885269852708527185272852738527485275852768527785278852798528085281852828528385284852858528685287852888528985290852918529285293852948529585296852978529885299853008530185302853038530485305853068530785308853098531085311853128531385314853158531685317853188531985320853218532285323853248532585326853278532885329853308533185332853338533485335853368533785338853398534085341853428534385344853458534685347853488534985350853518535285353853548535585356853578535885359853608536185362853638536485365853668536785368853698537085371853728537385374853758537685377853788537985380853818538285383853848538585386853878538885389853908539185392853938539485395853968539785398853998540085401854028540385404854058540685407854088540985410854118541285413854148541585416854178541885419854208542185422854238542485425854268542785428854298543085431854328543385434854358543685437854388543985440854418544285443854448544585446854478544885449854508545185452854538545485455854568545785458854598546085461854628546385464854658546685467854688546985470854718547285473854748547585476854778547885479854808548185482854838548485485854868548785488854898549085491854928549385494854958549685497854988549985500855018550285503855048550585506855078550885509855108551185512855138551485515855168551785518855198552085521855228552385524855258552685527855288552985530855318553285533855348553585536855378553885539855408554185542855438554485545855468554785548855498555085551855528555385554855558555685557855588555985560855618556285563855648556585566855678556885569855708557185572855738557485575855768557785578855798558085581855828558385584855858558685587855888558985590855918559285593855948559585596855978559885599856008560185602856038560485605856068560785608856098561085611856128561385614856158561685617856188561985620856218562285623856248562585626856278562885629856308563185632856338563485635856368563785638856398564085641856428564385644856458564685647856488564985650856518565285653856548565585656856578565885659856608566185662856638566485665856668566785668856698567085671856728567385674856758567685677856788567985680856818568285683856848568585686856878568885689856908569185692856938569485695856968569785698856998570085701857028570385704857058570685707857088570985710857118571285713857148571585716857178571885719857208572185722857238572485725857268572785728857298573085731857328573385734857358573685737857388573985740857418574285743857448574585746857478574885749857508575185752857538575485755857568575785758857598576085761857628576385764857658576685767857688576985770857718577285773857748577585776857778577885779857808578185782857838578485785857868578785788857898579085791857928579385794857958579685797857988579985800858018580285803858048580585806858078580885809858108581185812858138581485815858168581785818858198582085821858228582385824858258582685827858288582985830858318583285833858348583585836858378583885839858408584185842858438584485845858468584785848858498585085851858528585385854858558585685857858588585985860858618586285863858648586585866858678586885869858708587185872858738587485875858768587785878858798588085881858828588385884858858588685887858888588985890858918589285893858948589585896858978589885899859008590185902859038590485905859068590785908859098591085911859128591385914859158591685917859188591985920859218592285923859248592585926859278592885929859308593185932859338593485935859368593785938859398594085941859428594385944859458594685947859488594985950859518595285953859548595585956859578595885959859608596185962859638596485965859668596785968859698597085971859728597385974859758597685977859788597985980859818598285983859848598585986859878598885989859908599185992859938599485995859968599785998859998600086001860028600386004860058600686007860088600986010860118601286013860148601586016860178601886019860208602186022860238602486025860268602786028860298603086031860328603386034860358603686037860388603986040860418604286043860448604586046860478604886049860508605186052860538605486055860568605786058860598606086061860628606386064860658606686067860688606986070860718607286073860748607586076860778607886079860808608186082860838608486085860868608786088860898609086091860928609386094860958609686097860988609986100861018610286103861048610586106861078610886109861108611186112861138611486115861168611786118861198612086121861228612386124861258612686127861288612986130861318613286133861348613586136861378613886139861408614186142861438614486145861468614786148861498615086151861528615386154861558615686157861588615986160861618616286163861648616586166861678616886169861708617186172861738617486175861768617786178861798618086181861828618386184861858618686187861888618986190861918619286193861948619586196861978619886199862008620186202862038620486205862068620786208862098621086211862128621386214862158621686217862188621986220862218622286223862248622586226862278622886229862308623186232862338623486235862368623786238862398624086241862428624386244862458624686247862488624986250862518625286253862548625586256862578625886259862608626186262862638626486265862668626786268862698627086271862728627386274862758627686277862788627986280862818628286283862848628586286862878628886289862908629186292862938629486295862968629786298862998630086301863028630386304863058630686307863088630986310863118631286313863148631586316863178631886319863208632186322863238632486325863268632786328863298633086331863328633386334863358633686337863388633986340863418634286343863448634586346863478634886349863508635186352863538635486355863568635786358863598636086361863628636386364863658636686367863688636986370863718637286373863748637586376863778637886379863808638186382863838638486385863868638786388863898639086391863928639386394863958639686397863988639986400864018640286403864048640586406864078640886409864108641186412864138641486415864168641786418864198642086421864228642386424864258642686427864288642986430864318643286433864348643586436864378643886439864408644186442864438644486445864468644786448864498645086451864528645386454864558645686457864588645986460864618646286463864648646586466864678646886469864708647186472864738647486475864768647786478864798648086481864828648386484864858648686487864888648986490864918649286493864948649586496864978649886499865008650186502865038650486505865068650786508865098651086511865128651386514865158651686517865188651986520865218652286523865248652586526865278652886529865308653186532865338653486535865368653786538865398654086541865428654386544865458654686547865488654986550865518655286553865548655586556865578655886559865608656186562865638656486565865668656786568865698657086571865728657386574865758657686577865788657986580865818658286583865848658586586865878658886589865908659186592865938659486595865968659786598865998660086601866028660386604866058660686607866088660986610866118661286613866148661586616866178661886619866208662186622866238662486625866268662786628866298663086631866328663386634866358663686637866388663986640866418664286643866448664586646866478664886649866508665186652866538665486655866568665786658866598666086661866628666386664866658666686667866688666986670866718667286673866748667586676866778667886679866808668186682866838668486685866868668786688866898669086691866928669386694866958669686697866988669986700867018670286703867048670586706867078670886709867108671186712867138671486715867168671786718867198672086721867228672386724867258672686727867288672986730867318673286733867348673586736867378673886739867408674186742867438674486745867468674786748867498675086751867528675386754867558675686757867588675986760867618676286763867648676586766867678676886769867708677186772867738677486775867768677786778867798678086781867828678386784867858678686787867888678986790867918679286793867948679586796867978679886799868008680186802868038680486805868068680786808868098681086811868128681386814868158681686817868188681986820868218682286823868248682586826868278682886829868308683186832868338683486835868368683786838868398684086841868428684386844868458684686847868488684986850868518685286853868548685586856868578685886859868608686186862868638686486865868668686786868868698687086871868728687386874868758687686877868788687986880868818688286883868848688586886868878688886889868908689186892868938689486895868968689786898868998690086901869028690386904869058690686907869088690986910869118691286913869148691586916869178691886919869208692186922869238692486925869268692786928869298693086931869328693386934869358693686937869388693986940869418694286943869448694586946869478694886949869508695186952869538695486955869568695786958869598696086961869628696386964869658696686967869688696986970869718697286973869748697586976869778697886979869808698186982869838698486985869868698786988869898699086991869928699386994869958699686997869988699987000870018700287003870048700587006870078700887009870108701187012870138701487015870168701787018870198702087021870228702387024870258702687027870288702987030870318703287033870348703587036870378703887039870408704187042870438704487045870468704787048870498705087051870528705387054870558705687057870588705987060870618706287063870648706587066870678706887069870708707187072870738707487075870768707787078870798708087081870828708387084870858708687087870888708987090870918709287093870948709587096870978709887099871008710187102871038710487105871068710787108871098711087111871128711387114871158711687117871188711987120871218712287123871248712587126871278712887129871308713187132871338713487135871368713787138871398714087141871428714387144871458714687147871488714987150871518715287153871548715587156871578715887159871608716187162871638716487165871668716787168871698717087171871728717387174871758717687177871788717987180871818718287183871848718587186871878718887189871908719187192871938719487195871968719787198871998720087201872028720387204872058720687207872088720987210872118721287213872148721587216872178721887219872208722187222872238722487225872268722787228872298723087231872328723387234872358723687237872388723987240872418724287243872448724587246872478724887249872508725187252872538725487255872568725787258872598726087261872628726387264872658726687267872688726987270872718727287273872748727587276872778727887279872808728187282872838728487285872868728787288872898729087291872928729387294872958729687297872988729987300873018730287303873048730587306873078730887309873108731187312873138731487315873168731787318873198732087321873228732387324873258732687327873288732987330873318733287333873348733587336873378733887339873408734187342873438734487345873468734787348873498735087351873528735387354873558735687357873588735987360873618736287363873648736587366873678736887369873708737187372873738737487375873768737787378873798738087381873828738387384873858738687387873888738987390873918739287393873948739587396873978739887399874008740187402874038740487405874068740787408874098741087411874128741387414874158741687417874188741987420874218742287423874248742587426874278742887429874308743187432874338743487435874368743787438874398744087441874428744387444874458744687447874488744987450874518745287453874548745587456874578745887459874608746187462874638746487465874668746787468874698747087471874728747387474874758747687477874788747987480874818748287483874848748587486874878748887489874908749187492874938749487495874968749787498874998750087501875028750387504875058750687507875088750987510875118751287513875148751587516875178751887519875208752187522875238752487525875268752787528875298753087531875328753387534875358753687537875388753987540875418754287543875448754587546875478754887549875508755187552875538755487555875568755787558875598756087561875628756387564875658756687567875688756987570875718757287573875748757587576875778757887579875808758187582875838758487585875868758787588875898759087591875928759387594875958759687597875988759987600876018760287603876048760587606876078760887609876108761187612876138761487615876168761787618876198762087621876228762387624876258762687627876288762987630876318763287633876348763587636876378763887639876408764187642876438764487645876468764787648876498765087651876528765387654876558765687657876588765987660876618766287663876648766587666876678766887669876708767187672876738767487675876768767787678876798768087681876828768387684876858768687687876888768987690876918769287693876948769587696876978769887699877008770187702877038770487705877068770787708877098771087711877128771387714877158771687717877188771987720877218772287723877248772587726877278772887729877308773187732877338773487735877368773787738877398774087741877428774387744877458774687747877488774987750877518775287753877548775587756877578775887759877608776187762877638776487765877668776787768877698777087771877728777387774877758777687777877788777987780877818778287783877848778587786877878778887789877908779187792877938779487795877968779787798877998780087801878028780387804878058780687807878088780987810878118781287813878148781587816878178781887819878208782187822878238782487825878268782787828878298783087831878328783387834878358783687837878388783987840878418784287843878448784587846878478784887849878508785187852878538785487855878568785787858878598786087861878628786387864878658786687867878688786987870878718787287873878748787587876878778787887879878808788187882878838788487885878868788787888878898789087891878928789387894878958789687897878988789987900879018790287903879048790587906879078790887909879108791187912879138791487915879168791787918879198792087921879228792387924879258792687927879288792987930879318793287933879348793587936879378793887939879408794187942879438794487945879468794787948879498795087951879528795387954879558795687957879588795987960879618796287963879648796587966879678796887969879708797187972879738797487975879768797787978879798798087981879828798387984879858798687987879888798987990879918799287993879948799587996879978799887999880008800188002880038800488005880068800788008880098801088011880128801388014880158801688017880188801988020880218802288023880248802588026880278802888029880308803188032880338803488035880368803788038880398804088041880428804388044880458804688047880488804988050880518805288053880548805588056880578805888059880608806188062880638806488065880668806788068880698807088071880728807388074880758807688077880788807988080880818808288083880848808588086880878808888089880908809188092880938809488095880968809788098880998810088101881028810388104881058810688107881088810988110881118811288113881148811588116881178811888119881208812188122881238812488125881268812788128881298813088131881328813388134881358813688137881388813988140881418814288143881448814588146881478814888149881508815188152881538815488155881568815788158881598816088161881628816388164881658816688167881688816988170881718817288173881748817588176881778817888179881808818188182881838818488185881868818788188881898819088191881928819388194881958819688197881988819988200882018820288203882048820588206882078820888209882108821188212882138821488215882168821788218882198822088221882228822388224882258822688227882288822988230882318823288233882348823588236882378823888239882408824188242882438824488245882468824788248882498825088251882528825388254882558825688257882588825988260882618826288263882648826588266882678826888269882708827188272882738827488275882768827788278882798828088281882828828388284882858828688287882888828988290882918829288293882948829588296882978829888299883008830188302883038830488305883068830788308883098831088311883128831388314883158831688317883188831988320883218832288323883248832588326883278832888329883308833188332883338833488335883368833788338883398834088341883428834388344883458834688347883488834988350883518835288353883548835588356883578835888359883608836188362883638836488365883668836788368883698837088371883728837388374883758837688377883788837988380883818838288383883848838588386883878838888389883908839188392883938839488395883968839788398883998840088401884028840388404884058840688407884088840988410884118841288413884148841588416884178841888419884208842188422884238842488425884268842788428884298843088431884328843388434884358843688437884388843988440884418844288443884448844588446884478844888449884508845188452884538845488455884568845788458884598846088461884628846388464884658846688467884688846988470884718847288473884748847588476884778847888479884808848188482884838848488485884868848788488884898849088491884928849388494884958849688497884988849988500885018850288503885048850588506885078850888509885108851188512885138851488515885168851788518885198852088521885228852388524885258852688527885288852988530885318853288533885348853588536885378853888539885408854188542885438854488545885468854788548885498855088551885528855388554885558855688557885588855988560885618856288563885648856588566885678856888569885708857188572885738857488575885768857788578885798858088581885828858388584885858858688587885888858988590885918859288593885948859588596885978859888599886008860188602886038860488605886068860788608886098861088611886128861388614886158861688617886188861988620886218862288623886248862588626886278862888629886308863188632886338863488635886368863788638886398864088641886428864388644886458864688647886488864988650886518865288653886548865588656886578865888659886608866188662886638866488665886668866788668886698867088671886728867388674886758867688677886788867988680886818868288683886848868588686886878868888689886908869188692886938869488695886968869788698886998870088701887028870388704887058870688707887088870988710887118871288713887148871588716887178871888719887208872188722887238872488725887268872788728887298873088731887328873388734887358873688737887388873988740887418874288743887448874588746887478874888749887508875188752887538875488755887568875788758887598876088761887628876388764887658876688767887688876988770887718877288773887748877588776887778877888779887808878188782887838878488785887868878788788887898879088791887928879388794887958879688797887988879988800888018880288803888048880588806888078880888809888108881188812888138881488815888168881788818888198882088821888228882388824888258882688827888288882988830888318883288833888348883588836888378883888839888408884188842888438884488845888468884788848888498885088851888528885388854888558885688857888588885988860888618886288863888648886588866888678886888869888708887188872888738887488875888768887788878888798888088881888828888388884888858888688887888888888988890888918889288893888948889588896888978889888899889008890188902889038890488905889068890788908889098891088911889128891388914889158891688917889188891988920889218892288923889248892588926889278892888929889308893188932889338893488935889368893788938889398894088941889428894388944889458894688947889488894988950889518895288953889548895588956889578895888959889608896188962889638896488965889668896788968889698897088971889728897388974889758897688977889788897988980889818898288983889848898588986889878898888989889908899188992889938899488995889968899788998889998900089001890028900389004890058900689007890088900989010890118901289013890148901589016890178901889019890208902189022890238902489025890268902789028890298903089031890328903389034890358903689037890388903989040890418904289043890448904589046890478904889049890508905189052890538905489055890568905789058890598906089061890628906389064890658906689067890688906989070890718907289073890748907589076890778907889079890808908189082890838908489085890868908789088890898909089091890928909389094890958909689097890988909989100891018910289103891048910589106891078910889109891108911189112891138911489115891168911789118891198912089121891228912389124891258912689127891288912989130891318913289133891348913589136891378913889139891408914189142891438914489145891468914789148891498915089151891528915389154891558915689157891588915989160891618916289163891648916589166891678916889169891708917189172891738917489175891768917789178891798918089181891828918389184891858918689187891888918989190891918919289193891948919589196891978919889199892008920189202892038920489205892068920789208892098921089211892128921389214892158921689217892188921989220892218922289223892248922589226892278922889229892308923189232892338923489235892368923789238892398924089241892428924389244892458924689247892488924989250892518925289253892548925589256892578925889259892608926189262892638926489265892668926789268892698927089271892728927389274892758927689277892788927989280892818928289283892848928589286892878928889289892908929189292892938929489295892968929789298892998930089301893028930389304893058930689307893088930989310893118931289313893148931589316893178931889319893208932189322893238932489325893268932789328893298933089331893328933389334893358933689337893388933989340893418934289343893448934589346893478934889349893508935189352893538935489355893568935789358893598936089361893628936389364893658936689367893688936989370893718937289373893748937589376893778937889379893808938189382893838938489385893868938789388893898939089391893928939389394893958939689397893988939989400894018940289403894048940589406894078940889409894108941189412894138941489415894168941789418894198942089421894228942389424894258942689427894288942989430894318943289433894348943589436894378943889439894408944189442894438944489445894468944789448894498945089451894528945389454894558945689457894588945989460894618946289463894648946589466894678946889469894708947189472894738947489475894768947789478894798948089481894828948389484894858948689487894888948989490894918949289493894948949589496894978949889499895008950189502895038950489505895068950789508895098951089511895128951389514895158951689517895188951989520895218952289523895248952589526895278952889529895308953189532895338953489535895368953789538895398954089541895428954389544895458954689547895488954989550895518955289553895548955589556895578955889559895608956189562895638956489565895668956789568895698957089571895728957389574895758957689577895788957989580895818958289583895848958589586895878958889589895908959189592895938959489595895968959789598895998960089601896028960389604896058960689607896088960989610896118961289613896148961589616896178961889619896208962189622896238962489625896268962789628896298963089631896328963389634896358963689637896388963989640896418964289643896448964589646896478964889649896508965189652896538965489655896568965789658896598966089661896628966389664896658966689667896688966989670896718967289673896748967589676896778967889679896808968189682896838968489685896868968789688896898969089691896928969389694896958969689697896988969989700897018970289703897048970589706897078970889709897108971189712897138971489715897168971789718897198972089721897228972389724897258972689727897288972989730897318973289733897348973589736897378973889739897408974189742897438974489745897468974789748897498975089751897528975389754897558975689757897588975989760897618976289763897648976589766897678976889769897708977189772897738977489775897768977789778897798978089781897828978389784897858978689787897888978989790897918979289793897948979589796897978979889799898008980189802898038980489805898068980789808898098981089811898128981389814898158981689817898188981989820898218982289823898248982589826898278982889829898308983189832898338983489835898368983789838898398984089841898428984389844898458984689847898488984989850898518985289853898548985589856898578985889859898608986189862898638986489865898668986789868898698987089871898728987389874898758987689877898788987989880898818988289883898848988589886898878988889889898908989189892898938989489895898968989789898898998990089901899028990389904899058990689907899088990989910899118991289913899148991589916899178991889919899208992189922899238992489925899268992789928899298993089931899328993389934899358993689937899388993989940899418994289943899448994589946899478994889949899508995189952899538995489955899568995789958899598996089961899628996389964899658996689967899688996989970899718997289973899748997589976899778997889979899808998189982899838998489985899868998789988899898999089991899928999389994899958999689997899988999990000900019000290003900049000590006900079000890009900109001190012900139001490015900169001790018900199002090021900229002390024900259002690027900289002990030900319003290033900349003590036900379003890039900409004190042900439004490045900469004790048900499005090051900529005390054900559005690057900589005990060900619006290063900649006590066900679006890069900709007190072900739007490075900769007790078900799008090081900829008390084900859008690087900889008990090900919009290093900949009590096900979009890099901009010190102901039010490105901069010790108901099011090111901129011390114901159011690117901189011990120901219012290123901249012590126901279012890129901309013190132901339013490135901369013790138901399014090141901429014390144901459014690147901489014990150901519015290153901549015590156901579015890159901609016190162901639016490165901669016790168901699017090171901729017390174901759017690177901789017990180901819018290183901849018590186901879018890189901909019190192901939019490195901969019790198901999020090201902029020390204902059020690207902089020990210902119021290213902149021590216902179021890219902209022190222902239022490225902269022790228902299023090231902329023390234902359023690237902389023990240902419024290243902449024590246902479024890249902509025190252902539025490255902569025790258902599026090261902629026390264902659026690267902689026990270902719027290273902749027590276902779027890279902809028190282902839028490285902869028790288902899029090291902929029390294902959029690297902989029990300903019030290303903049030590306903079030890309903109031190312903139031490315903169031790318903199032090321903229032390324903259032690327903289032990330903319033290333903349033590336903379033890339903409034190342903439034490345903469034790348903499035090351903529035390354903559035690357903589035990360903619036290363903649036590366903679036890369903709037190372903739037490375903769037790378903799038090381903829038390384903859038690387903889038990390903919039290393903949039590396903979039890399904009040190402904039040490405904069040790408904099041090411904129041390414904159041690417904189041990420904219042290423904249042590426904279042890429904309043190432904339043490435904369043790438904399044090441904429044390444904459044690447904489044990450904519045290453904549045590456904579045890459904609046190462904639046490465904669046790468904699047090471904729047390474904759047690477904789047990480904819048290483904849048590486904879048890489904909049190492904939049490495904969049790498904999050090501905029050390504905059050690507905089050990510905119051290513905149051590516905179051890519905209052190522905239052490525905269052790528905299053090531905329053390534905359053690537905389053990540905419054290543905449054590546905479054890549905509055190552905539055490555905569055790558905599056090561905629056390564905659056690567905689056990570905719057290573905749057590576905779057890579905809058190582905839058490585905869058790588905899059090591905929059390594905959059690597905989059990600906019060290603906049060590606906079060890609906109061190612906139061490615906169061790618906199062090621906229062390624906259062690627906289062990630906319063290633906349063590636906379063890639906409064190642906439064490645906469064790648906499065090651906529065390654906559065690657906589065990660906619066290663906649066590666906679066890669906709067190672906739067490675906769067790678906799068090681906829068390684906859068690687906889068990690906919069290693906949069590696906979069890699907009070190702907039070490705907069070790708907099071090711907129071390714907159071690717907189071990720907219072290723907249072590726907279072890729907309073190732907339073490735907369073790738907399074090741907429074390744907459074690747907489074990750907519075290753907549075590756907579075890759907609076190762907639076490765907669076790768907699077090771907729077390774907759077690777907789077990780907819078290783907849078590786907879078890789907909079190792907939079490795907969079790798907999080090801908029080390804908059080690807908089080990810908119081290813908149081590816908179081890819908209082190822908239082490825908269082790828908299083090831908329083390834908359083690837908389083990840908419084290843908449084590846908479084890849908509085190852908539085490855908569085790858908599086090861908629086390864908659086690867908689086990870908719087290873908749087590876908779087890879908809088190882908839088490885908869088790888908899089090891908929089390894908959089690897908989089990900909019090290903909049090590906909079090890909909109091190912909139091490915909169091790918909199092090921909229092390924909259092690927909289092990930909319093290933909349093590936909379093890939909409094190942909439094490945909469094790948909499095090951909529095390954909559095690957909589095990960909619096290963909649096590966909679096890969909709097190972909739097490975909769097790978909799098090981909829098390984909859098690987909889098990990909919099290993909949099590996909979099890999910009100191002910039100491005910069100791008910099101091011910129101391014910159101691017910189101991020910219102291023910249102591026910279102891029910309103191032910339103491035910369103791038910399104091041910429104391044910459104691047910489104991050910519105291053910549105591056910579105891059910609106191062910639106491065910669106791068910699107091071910729107391074910759107691077910789107991080910819108291083910849108591086910879108891089910909109191092910939109491095910969109791098910999110091101911029110391104911059110691107911089110991110911119111291113911149111591116911179111891119911209112191122911239112491125911269112791128911299113091131911329113391134911359113691137911389113991140911419114291143911449114591146911479114891149911509115191152911539115491155911569115791158911599116091161911629116391164911659116691167911689116991170911719117291173911749117591176911779117891179911809118191182911839118491185911869118791188911899119091191911929119391194911959119691197911989119991200912019120291203912049120591206912079120891209912109121191212912139121491215912169121791218912199122091221912229122391224912259122691227912289122991230912319123291233912349123591236912379123891239912409124191242912439124491245912469124791248912499125091251912529125391254912559125691257912589125991260912619126291263912649126591266912679126891269912709127191272912739127491275912769127791278912799128091281912829128391284912859128691287912889128991290912919129291293912949129591296912979129891299913009130191302913039130491305913069130791308913099131091311913129131391314913159131691317913189131991320913219132291323913249132591326913279132891329913309133191332913339133491335913369133791338913399134091341913429134391344913459134691347913489134991350913519135291353913549135591356913579135891359913609136191362913639136491365913669136791368913699137091371913729137391374913759137691377913789137991380913819138291383913849138591386913879138891389913909139191392913939139491395913969139791398913999140091401914029140391404914059140691407914089140991410914119141291413914149141591416914179141891419914209142191422914239142491425914269142791428914299143091431914329143391434914359143691437914389143991440914419144291443914449144591446914479144891449914509145191452914539145491455914569145791458914599146091461914629146391464914659146691467914689146991470914719147291473914749147591476914779147891479914809148191482914839148491485914869148791488914899149091491914929149391494914959149691497914989149991500915019150291503915049150591506915079150891509915109151191512915139151491515915169151791518915199152091521915229152391524915259152691527915289152991530915319153291533915349153591536915379153891539915409154191542915439154491545915469154791548915499155091551915529155391554915559155691557915589155991560915619156291563915649156591566915679156891569915709157191572915739157491575915769157791578915799158091581915829158391584915859158691587915889158991590915919159291593915949159591596915979159891599916009160191602916039160491605916069160791608916099161091611916129161391614916159161691617916189161991620916219162291623916249162591626916279162891629916309163191632916339163491635916369163791638916399164091641916429164391644916459164691647916489164991650916519165291653916549165591656916579165891659916609166191662916639166491665916669166791668916699167091671916729167391674916759167691677916789167991680916819168291683916849168591686916879168891689916909169191692916939169491695916969169791698916999170091701917029170391704917059170691707917089170991710917119171291713917149171591716917179171891719917209172191722917239172491725917269172791728917299173091731917329173391734917359173691737917389173991740917419174291743917449174591746917479174891749917509175191752917539175491755917569175791758917599176091761917629176391764917659176691767917689176991770917719177291773917749177591776917779177891779917809178191782917839178491785917869178791788917899179091791917929179391794917959179691797917989179991800918019180291803918049180591806918079180891809918109181191812918139181491815918169181791818918199182091821918229182391824918259182691827918289182991830918319183291833918349183591836918379183891839918409184191842918439184491845918469184791848918499185091851918529185391854918559185691857918589185991860918619186291863918649186591866918679186891869918709187191872918739187491875918769187791878918799188091881918829188391884918859188691887918889188991890918919189291893918949189591896918979189891899919009190191902919039190491905919069190791908919099191091911919129191391914919159191691917919189191991920919219192291923919249192591926919279192891929919309193191932919339193491935919369193791938919399194091941919429194391944919459194691947919489194991950919519195291953919549195591956919579195891959919609196191962919639196491965919669196791968919699197091971919729197391974919759197691977919789197991980919819198291983919849198591986919879198891989919909199191992919939199491995919969199791998919999200092001920029200392004920059200692007920089200992010920119201292013920149201592016920179201892019920209202192022920239202492025920269202792028920299203092031920329203392034920359203692037920389203992040920419204292043920449204592046920479204892049920509205192052920539205492055920569205792058920599206092061920629206392064920659206692067920689206992070920719207292073920749207592076920779207892079920809208192082920839208492085920869208792088920899209092091920929209392094920959209692097920989209992100921019210292103921049210592106921079210892109921109211192112921139211492115921169211792118921199212092121921229212392124921259212692127921289212992130921319213292133921349213592136921379213892139921409214192142921439214492145921469214792148921499215092151921529215392154921559215692157921589215992160921619216292163921649216592166921679216892169921709217192172921739217492175921769217792178921799218092181921829218392184921859218692187921889218992190921919219292193921949219592196921979219892199922009220192202922039220492205922069220792208922099221092211922129221392214922159221692217922189221992220922219222292223922249222592226922279222892229922309223192232922339223492235922369223792238922399224092241922429224392244922459224692247922489224992250922519225292253922549225592256922579225892259922609226192262922639226492265922669226792268922699227092271922729227392274922759227692277922789227992280922819228292283922849228592286922879228892289922909229192292922939229492295922969229792298922999230092301923029230392304923059230692307923089230992310923119231292313923149231592316923179231892319923209232192322923239232492325923269232792328923299233092331923329233392334923359233692337923389233992340923419234292343923449234592346923479234892349923509235192352923539235492355923569235792358923599236092361923629236392364923659236692367923689236992370923719237292373923749237592376923779237892379923809238192382923839238492385923869238792388923899239092391923929239392394923959239692397923989239992400924019240292403924049240592406924079240892409924109241192412924139241492415924169241792418924199242092421924229242392424924259242692427924289242992430924319243292433924349243592436924379243892439924409244192442924439244492445924469244792448924499245092451924529245392454924559245692457924589245992460924619246292463924649246592466924679246892469924709247192472924739247492475924769247792478924799248092481924829248392484924859248692487924889248992490924919249292493924949249592496924979249892499925009250192502925039250492505925069250792508925099251092511925129251392514925159251692517925189251992520925219252292523925249252592526925279252892529925309253192532925339253492535925369253792538925399254092541925429254392544925459254692547925489254992550925519255292553925549255592556925579255892559925609256192562925639256492565925669256792568925699257092571925729257392574925759257692577925789257992580925819258292583925849258592586925879258892589925909259192592925939259492595925969259792598925999260092601926029260392604926059260692607926089260992610926119261292613926149261592616926179261892619926209262192622926239262492625926269262792628926299263092631926329263392634926359263692637926389263992640926419264292643926449264592646926479264892649926509265192652926539265492655926569265792658926599266092661926629266392664926659266692667926689266992670926719267292673926749267592676926779267892679926809268192682926839268492685926869268792688926899269092691926929269392694926959269692697926989269992700927019270292703927049270592706927079270892709927109271192712927139271492715927169271792718927199272092721927229272392724927259272692727927289272992730927319273292733927349273592736927379273892739927409274192742927439274492745927469274792748927499275092751927529275392754927559275692757927589275992760927619276292763927649276592766927679276892769927709277192772927739277492775927769277792778927799278092781927829278392784927859278692787927889278992790927919279292793927949279592796927979279892799928009280192802928039280492805928069280792808928099281092811928129281392814928159281692817928189281992820928219282292823928249282592826928279282892829928309283192832928339283492835928369283792838928399284092841928429284392844928459284692847928489284992850928519285292853928549285592856928579285892859928609286192862928639286492865928669286792868928699287092871928729287392874928759287692877928789287992880928819288292883928849288592886928879288892889928909289192892928939289492895928969289792898928999290092901929029290392904929059290692907929089290992910929119291292913929149291592916929179291892919929209292192922929239292492925929269292792928929299293092931929329293392934929359293692937929389293992940929419294292943929449294592946929479294892949929509295192952929539295492955929569295792958929599296092961929629296392964929659296692967929689296992970929719297292973929749297592976929779297892979929809298192982929839298492985929869298792988929899299092991929929299392994929959299692997929989299993000930019300293003930049300593006930079300893009930109301193012930139301493015930169301793018930199302093021930229302393024930259302693027930289302993030930319303293033930349303593036930379303893039930409304193042930439304493045930469304793048930499305093051930529305393054930559305693057930589305993060930619306293063930649306593066930679306893069930709307193072930739307493075930769307793078930799308093081930829308393084930859308693087930889308993090930919309293093930949309593096930979309893099931009310193102931039310493105931069310793108931099311093111931129311393114931159311693117931189311993120931219312293123931249312593126931279312893129931309313193132931339313493135931369313793138931399314093141931429314393144931459314693147931489314993150931519315293153931549315593156931579315893159931609316193162931639316493165931669316793168931699317093171931729317393174931759317693177931789317993180931819318293183931849318593186931879318893189931909319193192931939319493195931969319793198931999320093201932029320393204932059320693207932089320993210932119321293213932149321593216932179321893219932209322193222932239322493225932269322793228932299323093231932329323393234932359323693237932389323993240932419324293243932449324593246932479324893249932509325193252932539325493255932569325793258932599326093261932629326393264932659326693267932689326993270932719327293273932749327593276932779327893279932809328193282932839328493285932869328793288932899329093291932929329393294932959329693297932989329993300933019330293303933049330593306933079330893309933109331193312933139331493315933169331793318933199332093321933229332393324933259332693327933289332993330933319333293333933349333593336933379333893339933409334193342933439334493345933469334793348933499335093351933529335393354933559335693357933589335993360933619336293363933649336593366933679336893369933709337193372933739337493375933769337793378933799338093381933829338393384933859338693387933889338993390933919339293393933949339593396933979339893399934009340193402934039340493405934069340793408934099341093411934129341393414934159341693417934189341993420934219342293423934249342593426934279342893429934309343193432934339343493435934369343793438934399344093441934429344393444934459344693447934489344993450934519345293453934549345593456934579345893459934609346193462934639346493465934669346793468934699347093471934729347393474934759347693477934789347993480934819348293483934849348593486934879348893489934909349193492934939349493495934969349793498934999350093501935029350393504935059350693507935089350993510935119351293513935149351593516935179351893519935209352193522935239352493525935269352793528935299353093531935329353393534935359353693537935389353993540935419354293543935449354593546935479354893549935509355193552935539355493555935569355793558935599356093561935629356393564935659356693567935689356993570935719357293573935749357593576935779357893579935809358193582935839358493585935869358793588935899359093591935929359393594935959359693597935989359993600936019360293603936049360593606936079360893609936109361193612936139361493615936169361793618936199362093621936229362393624936259362693627936289362993630936319363293633936349363593636936379363893639936409364193642936439364493645936469364793648936499365093651936529365393654936559365693657936589365993660936619366293663936649366593666936679366893669936709367193672936739367493675936769367793678936799368093681936829368393684936859368693687936889368993690936919369293693936949369593696936979369893699937009370193702937039370493705937069370793708937099371093711937129371393714937159371693717937189371993720937219372293723937249372593726937279372893729937309373193732937339373493735937369373793738937399374093741937429374393744937459374693747937489374993750937519375293753937549375593756937579375893759937609376193762937639376493765937669376793768937699377093771937729377393774937759377693777937789377993780937819378293783937849378593786937879378893789937909379193792937939379493795937969379793798937999380093801938029380393804938059380693807938089380993810938119381293813938149381593816938179381893819938209382193822938239382493825938269382793828938299383093831938329383393834938359383693837938389383993840938419384293843938449384593846938479384893849938509385193852938539385493855938569385793858938599386093861938629386393864938659386693867938689386993870938719387293873938749387593876938779387893879938809388193882938839388493885938869388793888938899389093891938929389393894938959389693897938989389993900939019390293903939049390593906939079390893909939109391193912939139391493915939169391793918939199392093921939229392393924939259392693927939289392993930939319393293933939349393593936939379393893939939409394193942939439394493945939469394793948939499395093951939529395393954939559395693957939589395993960939619396293963939649396593966939679396893969939709397193972939739397493975939769397793978939799398093981939829398393984939859398693987939889398993990939919399293993939949399593996939979399893999940009400194002940039400494005940069400794008940099401094011940129401394014940159401694017940189401994020940219402294023940249402594026940279402894029940309403194032940339403494035940369403794038940399404094041940429404394044940459404694047940489404994050940519405294053940549405594056940579405894059940609406194062940639406494065940669406794068940699407094071940729407394074940759407694077940789407994080940819408294083940849408594086940879408894089940909409194092940939409494095940969409794098940999410094101941029410394104941059410694107941089410994110941119411294113941149411594116941179411894119941209412194122941239412494125941269412794128941299413094131941329413394134941359413694137941389413994140941419414294143941449414594146941479414894149941509415194152941539415494155941569415794158941599416094161941629416394164941659416694167941689416994170941719417294173941749417594176941779417894179941809418194182941839418494185941869418794188941899419094191941929419394194941959419694197941989419994200942019420294203942049420594206942079420894209942109421194212942139421494215942169421794218942199422094221942229422394224942259422694227942289422994230942319423294233942349423594236942379423894239942409424194242942439424494245942469424794248942499425094251942529425394254942559425694257942589425994260942619426294263942649426594266942679426894269942709427194272942739427494275942769427794278942799428094281942829428394284942859428694287942889428994290942919429294293942949429594296942979429894299943009430194302943039430494305943069430794308943099431094311943129431394314943159431694317943189431994320943219432294323943249432594326943279432894329943309433194332943339433494335943369433794338943399434094341943429434394344943459434694347943489434994350943519435294353943549435594356943579435894359943609436194362943639436494365943669436794368943699437094371943729437394374943759437694377943789437994380943819438294383943849438594386943879438894389943909439194392943939439494395943969439794398943999440094401944029440394404944059440694407944089440994410944119441294413944149441594416944179441894419944209442194422944239442494425944269442794428944299443094431944329443394434944359443694437944389443994440944419444294443944449444594446944479444894449944509445194452944539445494455944569445794458944599446094461944629446394464944659446694467944689446994470944719447294473944749447594476944779447894479944809448194482944839448494485944869448794488944899449094491944929449394494944959449694497944989449994500945019450294503945049450594506945079450894509945109451194512945139451494515945169451794518945199452094521945229452394524945259452694527945289452994530945319453294533945349453594536945379453894539945409454194542945439454494545945469454794548945499455094551945529455394554945559455694557945589455994560945619456294563945649456594566945679456894569945709457194572945739457494575945769457794578945799458094581945829458394584945859458694587945889458994590945919459294593945949459594596945979459894599946009460194602946039460494605946069460794608946099461094611946129461394614946159461694617946189461994620946219462294623946249462594626946279462894629946309463194632946339463494635946369463794638946399464094641946429464394644946459464694647946489464994650946519465294653946549465594656946579465894659946609466194662946639466494665946669466794668946699467094671946729467394674946759467694677946789467994680946819468294683946849468594686946879468894689946909469194692946939469494695946969469794698946999470094701947029470394704947059470694707947089470994710947119471294713947149471594716947179471894719947209472194722947239472494725947269472794728947299473094731947329473394734947359473694737947389473994740947419474294743947449474594746947479474894749947509475194752947539475494755947569475794758947599476094761947629476394764947659476694767947689476994770947719477294773947749477594776947779477894779947809478194782947839478494785947869478794788947899479094791947929479394794947959479694797947989479994800948019480294803948049480594806948079480894809948109481194812948139481494815948169481794818948199482094821948229482394824948259482694827948289482994830948319483294833948349483594836948379483894839948409484194842948439484494845948469484794848948499485094851948529485394854948559485694857948589485994860948619486294863948649486594866948679486894869948709487194872948739487494875948769487794878948799488094881948829488394884948859488694887948889488994890948919489294893948949489594896948979489894899949009490194902949039490494905949069490794908949099491094911949129491394914949159491694917949189491994920949219492294923949249492594926949279492894929949309493194932949339493494935949369493794938949399494094941949429494394944949459494694947949489494994950949519495294953949549495594956949579495894959949609496194962949639496494965949669496794968949699497094971949729497394974949759497694977949789497994980949819498294983949849498594986949879498894989949909499194992949939499494995949969499794998949999500095001950029500395004950059500695007950089500995010950119501295013950149501595016950179501895019950209502195022950239502495025950269502795028950299503095031950329503395034950359503695037950389503995040950419504295043950449504595046950479504895049950509505195052950539505495055950569505795058950599506095061950629506395064950659506695067950689506995070950719507295073950749507595076950779507895079950809508195082950839508495085950869508795088950899509095091950929509395094950959509695097950989509995100951019510295103951049510595106951079510895109951109511195112951139511495115951169511795118951199512095121951229512395124951259512695127951289512995130951319513295133951349513595136951379513895139951409514195142951439514495145951469514795148951499515095151951529515395154951559515695157951589515995160951619516295163951649516595166951679516895169951709517195172951739517495175951769517795178951799518095181951829518395184951859518695187951889518995190951919519295193951949519595196951979519895199952009520195202952039520495205952069520795208952099521095211952129521395214952159521695217952189521995220952219522295223952249522595226952279522895229952309523195232952339523495235952369523795238952399524095241952429524395244952459524695247952489524995250952519525295253952549525595256952579525895259952609526195262952639526495265952669526795268952699527095271952729527395274952759527695277952789527995280952819528295283952849528595286952879528895289952909529195292952939529495295952969529795298952999530095301953029530395304953059530695307953089530995310953119531295313953149531595316953179531895319953209532195322953239532495325953269532795328953299533095331953329533395334953359533695337953389533995340953419534295343953449534595346953479534895349953509535195352953539535495355953569535795358953599536095361953629536395364953659536695367953689536995370953719537295373953749537595376953779537895379953809538195382953839538495385953869538795388953899539095391953929539395394953959539695397953989539995400954019540295403954049540595406954079540895409954109541195412954139541495415954169541795418954199542095421954229542395424954259542695427954289542995430954319543295433954349543595436954379543895439954409544195442954439544495445954469544795448954499545095451954529545395454954559545695457954589545995460954619546295463954649546595466954679546895469954709547195472954739547495475954769547795478954799548095481954829548395484954859548695487954889548995490954919549295493954949549595496954979549895499955009550195502955039550495505955069550795508955099551095511955129551395514955159551695517955189551995520955219552295523955249552595526955279552895529955309553195532955339553495535955369553795538955399554095541955429554395544955459554695547955489554995550955519555295553955549555595556955579555895559955609556195562955639556495565955669556795568955699557095571955729557395574955759557695577955789557995580955819558295583955849558595586955879558895589955909559195592955939559495595955969559795598955999560095601956029560395604956059560695607956089560995610956119561295613956149561595616956179561895619956209562195622956239562495625956269562795628956299563095631956329563395634956359563695637956389563995640956419564295643956449564595646956479564895649956509565195652956539565495655956569565795658956599566095661956629566395664956659566695667956689566995670956719567295673956749567595676956779567895679956809568195682956839568495685956869568795688956899569095691956929569395694956959569695697956989569995700957019570295703957049570595706957079570895709957109571195712957139571495715957169571795718957199572095721957229572395724957259572695727957289572995730957319573295733957349573595736957379573895739957409574195742957439574495745957469574795748957499575095751957529575395754957559575695757957589575995760957619576295763957649576595766957679576895769957709577195772957739577495775957769577795778957799578095781957829578395784957859578695787957889578995790957919579295793957949579595796957979579895799958009580195802958039580495805958069580795808958099581095811958129581395814958159581695817958189581995820958219582295823958249582595826958279582895829958309583195832958339583495835958369583795838958399584095841958429584395844958459584695847958489584995850958519585295853958549585595856958579585895859958609586195862958639586495865958669586795868958699587095871958729587395874958759587695877958789587995880958819588295883958849588595886958879588895889958909589195892958939589495895958969589795898958999590095901959029590395904959059590695907959089590995910959119591295913959149591595916959179591895919959209592195922959239592495925959269592795928959299593095931959329593395934959359593695937959389593995940959419594295943959449594595946959479594895949959509595195952959539595495955959569595795958959599596095961959629596395964959659596695967959689596995970959719597295973959749597595976959779597895979959809598195982959839598495985959869598795988959899599095991959929599395994959959599695997959989599996000960019600296003960049600596006960079600896009960109601196012960139601496015960169601796018960199602096021960229602396024960259602696027960289602996030960319603296033960349603596036960379603896039960409604196042960439604496045960469604796048960499605096051960529605396054960559605696057960589605996060960619606296063960649606596066960679606896069960709607196072960739607496075960769607796078960799608096081960829608396084960859608696087960889608996090960919609296093960949609596096960979609896099961009610196102961039610496105961069610796108961099611096111961129611396114961159611696117961189611996120961219612296123961249612596126961279612896129961309613196132961339613496135961369613796138961399614096141961429614396144961459614696147961489614996150961519615296153961549615596156961579615896159961609616196162961639616496165961669616796168961699617096171961729617396174961759617696177961789617996180961819618296183961849618596186961879618896189961909619196192961939619496195961969619796198961999620096201962029620396204962059620696207962089620996210962119621296213962149621596216962179621896219962209622196222962239622496225962269622796228962299623096231962329623396234962359623696237962389623996240962419624296243962449624596246962479624896249962509625196252962539625496255962569625796258962599626096261962629626396264962659626696267962689626996270962719627296273962749627596276962779627896279962809628196282962839628496285962869628796288962899629096291962929629396294962959629696297962989629996300963019630296303963049630596306963079630896309963109631196312963139631496315963169631796318963199632096321963229632396324963259632696327963289632996330963319633296333963349633596336963379633896339963409634196342963439634496345963469634796348963499635096351963529635396354963559635696357963589635996360963619636296363963649636596366963679636896369963709637196372963739637496375963769637796378963799638096381963829638396384963859638696387963889638996390963919639296393963949639596396963979639896399964009640196402964039640496405964069640796408964099641096411964129641396414964159641696417964189641996420964219642296423964249642596426964279642896429964309643196432964339643496435964369643796438964399644096441964429644396444964459644696447964489644996450964519645296453964549645596456964579645896459964609646196462964639646496465964669646796468964699647096471964729647396474964759647696477964789647996480964819648296483964849648596486964879648896489964909649196492964939649496495964969649796498964999650096501965029650396504965059650696507965089650996510965119651296513965149651596516965179651896519965209652196522965239652496525965269652796528965299653096531965329653396534965359653696537965389653996540965419654296543965449654596546965479654896549965509655196552965539655496555965569655796558965599656096561965629656396564965659656696567965689656996570965719657296573965749657596576965779657896579965809658196582965839658496585965869658796588965899659096591965929659396594965959659696597965989659996600966019660296603966049660596606966079660896609966109661196612966139661496615966169661796618966199662096621966229662396624966259662696627966289662996630966319663296633966349663596636966379663896639966409664196642966439664496645966469664796648966499665096651966529665396654966559665696657966589665996660966619666296663966649666596666966679666896669966709667196672966739667496675966769667796678966799668096681966829668396684966859668696687966889668996690966919669296693966949669596696966979669896699967009670196702967039670496705967069670796708967099671096711967129671396714967159671696717967189671996720967219672296723967249672596726967279672896729967309673196732967339673496735967369673796738967399674096741967429674396744967459674696747967489674996750967519675296753967549675596756967579675896759967609676196762967639676496765967669676796768967699677096771967729677396774967759677696777967789677996780967819678296783967849678596786967879678896789967909679196792967939679496795967969679796798967999680096801968029680396804968059680696807968089680996810968119681296813968149681596816968179681896819968209682196822968239682496825968269682796828968299683096831968329683396834968359683696837968389683996840968419684296843968449684596846968479684896849968509685196852968539685496855968569685796858968599686096861968629686396864968659686696867968689686996870968719687296873968749687596876968779687896879968809688196882968839688496885968869688796888968899689096891968929689396894968959689696897968989689996900969019690296903969049690596906969079690896909969109691196912969139691496915969169691796918969199692096921969229692396924969259692696927969289692996930969319693296933969349693596936969379693896939969409694196942969439694496945969469694796948969499695096951969529695396954969559695696957969589695996960969619696296963969649696596966969679696896969969709697196972969739697496975969769697796978969799698096981969829698396984969859698696987969889698996990969919699296993969949699596996969979699896999970009700197002970039700497005970069700797008970099701097011970129701397014970159701697017970189701997020970219702297023970249702597026970279702897029970309703197032970339703497035970369703797038970399704097041970429704397044970459704697047970489704997050970519705297053970549705597056970579705897059970609706197062970639706497065970669706797068970699707097071970729707397074970759707697077970789707997080970819708297083970849708597086970879708897089970909709197092970939709497095970969709797098970999710097101971029710397104971059710697107971089710997110971119711297113971149711597116971179711897119971209712197122971239712497125971269712797128971299713097131971329713397134971359713697137971389713997140971419714297143971449714597146971479714897149971509715197152971539715497155971569715797158971599716097161971629716397164971659716697167971689716997170971719717297173971749717597176971779717897179971809718197182971839718497185971869718797188971899719097191971929719397194971959719697197971989719997200972019720297203972049720597206972079720897209972109721197212972139721497215972169721797218972199722097221972229722397224972259722697227972289722997230972319723297233972349723597236972379723897239972409724197242972439724497245972469724797248972499725097251972529725397254972559725697257972589725997260972619726297263972649726597266972679726897269972709727197272972739727497275972769727797278972799728097281972829728397284972859728697287972889728997290972919729297293972949729597296972979729897299973009730197302973039730497305973069730797308973099731097311973129731397314973159731697317973189731997320973219732297323973249732597326973279732897329973309733197332973339733497335973369733797338973399734097341973429734397344973459734697347973489734997350973519735297353973549735597356973579735897359973609736197362973639736497365973669736797368973699737097371973729737397374973759737697377973789737997380973819738297383973849738597386973879738897389973909739197392973939739497395973969739797398973999740097401974029740397404974059740697407974089740997410974119741297413974149741597416974179741897419974209742197422974239742497425974269742797428974299743097431974329743397434974359743697437974389743997440974419744297443974449744597446974479744897449974509745197452974539745497455974569745797458974599746097461974629746397464974659746697467974689746997470974719747297473974749747597476974779747897479974809748197482974839748497485974869748797488974899749097491974929749397494974959749697497974989749997500975019750297503975049750597506975079750897509975109751197512975139751497515975169751797518975199752097521975229752397524975259752697527975289752997530975319753297533975349753597536975379753897539975409754197542975439754497545975469754797548975499755097551975529755397554975559755697557975589755997560975619756297563975649756597566975679756897569975709757197572975739757497575975769757797578975799758097581975829758397584975859758697587975889758997590975919759297593975949759597596975979759897599976009760197602976039760497605976069760797608976099761097611976129761397614976159761697617976189761997620976219762297623976249762597626976279762897629976309763197632976339763497635976369763797638976399764097641976429764397644976459764697647976489764997650976519765297653976549765597656976579765897659976609766197662976639766497665976669766797668976699767097671976729767397674976759767697677976789767997680976819768297683976849768597686976879768897689976909769197692976939769497695976969769797698976999770097701977029770397704977059770697707977089770997710977119771297713977149771597716977179771897719977209772197722977239772497725977269772797728977299773097731977329773397734977359773697737977389773997740977419774297743977449774597746977479774897749977509775197752977539775497755977569775797758977599776097761977629776397764977659776697767977689776997770977719777297773977749777597776977779777897779977809778197782977839778497785977869778797788977899779097791977929779397794977959779697797977989779997800978019780297803978049780597806978079780897809978109781197812978139781497815978169781797818978199782097821978229782397824978259782697827978289782997830978319783297833978349783597836978379783897839978409784197842978439784497845978469784797848978499785097851978529785397854978559785697857978589785997860978619786297863978649786597866978679786897869978709787197872978739787497875978769787797878978799788097881978829788397884978859788697887978889788997890978919789297893978949789597896978979789897899979009790197902979039790497905979069790797908979099791097911979129791397914979159791697917979189791997920979219792297923979249792597926979279792897929979309793197932979339793497935979369793797938979399794097941979429794397944979459794697947979489794997950979519795297953979549795597956979579795897959979609796197962979639796497965979669796797968979699797097971979729797397974979759797697977979789797997980979819798297983979849798597986979879798897989979909799197992979939799497995979969799797998979999800098001980029800398004980059800698007980089800998010980119801298013980149801598016980179801898019980209802198022980239802498025980269802798028980299803098031980329803398034980359803698037980389803998040980419804298043980449804598046980479804898049980509805198052980539805498055980569805798058980599806098061980629806398064980659806698067980689806998070980719807298073980749807598076980779807898079980809808198082980839808498085980869808798088980899809098091980929809398094980959809698097980989809998100981019810298103981049810598106981079810898109981109811198112981139811498115981169811798118981199812098121981229812398124981259812698127981289812998130981319813298133981349813598136981379813898139981409814198142981439814498145981469814798148981499815098151981529815398154981559815698157981589815998160981619816298163981649816598166981679816898169981709817198172981739817498175981769817798178981799818098181981829818398184981859818698187981889818998190981919819298193981949819598196981979819898199982009820198202982039820498205982069820798208982099821098211982129821398214982159821698217982189821998220982219822298223982249822598226982279822898229982309823198232982339823498235982369823798238982399824098241982429824398244982459824698247982489824998250982519825298253982549825598256982579825898259982609826198262982639826498265982669826798268982699827098271982729827398274982759827698277982789827998280982819828298283982849828598286982879828898289982909829198292982939829498295982969829798298982999830098301983029830398304983059830698307983089830998310983119831298313983149831598316983179831898319983209832198322983239832498325983269832798328983299833098331983329833398334983359833698337983389833998340983419834298343983449834598346983479834898349983509835198352983539835498355983569835798358983599836098361983629836398364983659836698367983689836998370983719837298373983749837598376983779837898379983809838198382983839838498385983869838798388983899839098391983929839398394983959839698397983989839998400984019840298403984049840598406984079840898409984109841198412984139841498415984169841798418984199842098421984229842398424984259842698427984289842998430984319843298433984349843598436984379843898439984409844198442984439844498445984469844798448984499845098451984529845398454984559845698457984589845998460984619846298463984649846598466984679846898469984709847198472984739847498475984769847798478984799848098481984829848398484984859848698487984889848998490984919849298493984949849598496984979849898499985009850198502985039850498505985069850798508985099851098511985129851398514985159851698517985189851998520985219852298523985249852598526985279852898529985309853198532985339853498535985369853798538985399854098541985429854398544985459854698547985489854998550985519855298553985549855598556985579855898559985609856198562985639856498565985669856798568985699857098571985729857398574985759857698577985789857998580985819858298583985849858598586985879858898589985909859198592985939859498595985969859798598985999860098601986029860398604986059860698607986089860998610986119861298613986149861598616986179861898619986209862198622986239862498625986269862798628986299863098631986329863398634986359863698637986389863998640986419864298643986449864598646986479864898649986509865198652986539865498655986569865798658986599866098661986629866398664986659866698667986689866998670986719867298673986749867598676986779867898679986809868198682986839868498685986869868798688986899869098691986929869398694986959869698697986989869998700987019870298703987049870598706987079870898709987109871198712987139871498715987169871798718987199872098721987229872398724987259872698727987289872998730987319873298733987349873598736987379873898739987409874198742987439874498745987469874798748987499875098751987529875398754987559875698757987589875998760987619876298763987649876598766987679876898769987709877198772987739877498775987769877798778987799878098781987829878398784987859878698787987889878998790987919879298793987949879598796987979879898799988009880198802988039880498805988069880798808988099881098811988129881398814988159881698817988189881998820988219882298823988249882598826988279882898829988309883198832988339883498835988369883798838988399884098841988429884398844988459884698847988489884998850988519885298853988549885598856988579885898859988609886198862988639886498865988669886798868988699887098871988729887398874988759887698877988789887998880988819888298883988849888598886988879888898889988909889198892988939889498895988969889798898988999890098901989029890398904989059890698907989089890998910989119891298913989149891598916989179891898919989209892198922989239892498925989269892798928989299893098931989329893398934989359893698937989389893998940989419894298943989449894598946989479894898949989509895198952989539895498955989569895798958989599896098961989629896398964989659896698967989689896998970989719897298973989749897598976989779897898979989809898198982989839898498985989869898798988989899899098991989929899398994989959899698997989989899999000990019900299003990049900599006990079900899009990109901199012990139901499015990169901799018990199902099021990229902399024990259902699027990289902999030990319903299033990349903599036990379903899039990409904199042990439904499045990469904799048990499905099051990529905399054990559905699057990589905999060990619906299063990649906599066990679906899069990709907199072990739907499075990769907799078990799908099081990829908399084990859908699087990889908999090990919909299093990949909599096990979909899099991009910199102991039910499105991069910799108991099911099111991129911399114991159911699117991189911999120991219912299123991249912599126991279912899129991309913199132991339913499135991369913799138991399914099141991429914399144991459914699147991489914999150991519915299153991549915599156991579915899159991609916199162991639916499165991669916799168991699917099171991729917399174991759917699177991789917999180991819918299183991849918599186991879918899189991909919199192991939919499195991969919799198991999920099201992029920399204992059920699207992089920999210992119921299213992149921599216992179921899219992209922199222992239922499225992269922799228992299923099231992329923399234992359923699237992389923999240992419924299243992449924599246992479924899249992509925199252992539925499255992569925799258992599926099261992629926399264992659926699267992689926999270992719927299273992749927599276992779927899279992809928199282992839928499285992869928799288992899929099291992929929399294992959929699297992989929999300993019930299303993049930599306993079930899309993109931199312993139931499315993169931799318993199932099321993229932399324993259932699327993289932999330993319933299333993349933599336993379933899339993409934199342993439934499345993469934799348993499935099351993529935399354993559935699357993589935999360993619936299363993649936599366993679936899369993709937199372993739937499375993769937799378993799938099381993829938399384993859938699387993889938999390993919939299393993949939599396993979939899399994009940199402994039940499405994069940799408994099941099411994129941399414994159941699417994189941999420994219942299423994249942599426994279942899429994309943199432994339943499435994369943799438994399944099441994429944399444994459944699447994489944999450994519945299453994549945599456994579945899459994609946199462994639946499465994669946799468994699947099471994729947399474994759947699477994789947999480994819948299483994849948599486994879948899489994909949199492994939949499495994969949799498994999950099501995029950399504995059950699507995089950999510995119951299513995149951599516995179951899519995209952199522995239952499525995269952799528995299953099531995329953399534995359953699537995389953999540995419954299543995449954599546995479954899549995509955199552995539955499555995569955799558995599956099561995629956399564995659956699567995689956999570995719957299573995749957599576995779957899579995809958199582995839958499585995869958799588995899959099591995929959399594995959959699597995989959999600996019960299603996049960599606996079960899609996109961199612996139961499615996169961799618996199962099621996229962399624996259962699627996289962999630996319963299633996349963599636996379963899639996409964199642996439964499645996469964799648996499965099651996529965399654996559965699657996589965999660996619966299663996649966599666996679966899669996709967199672996739967499675996769967799678996799968099681996829968399684996859968699687996889968999690996919969299693996949969599696996979969899699997009970199702997039970499705997069970799708997099971099711997129971399714997159971699717997189971999720997219972299723997249972599726997279972899729997309973199732997339973499735997369973799738997399974099741997429974399744997459974699747997489974999750997519975299753997549975599756997579975899759997609976199762997639976499765997669976799768997699977099771997729977399774997759977699777997789977999780997819978299783997849978599786997879978899789997909979199792997939979499795997969979799798997999980099801998029980399804998059980699807998089980999810998119981299813998149981599816998179981899819998209982199822998239982499825998269982799828998299983099831998329983399834998359983699837998389983999840998419984299843998449984599846998479984899849998509985199852998539985499855998569985799858998599986099861998629986399864998659986699867998689986999870998719987299873998749987599876998779987899879998809988199882998839988499885998869988799888998899989099891998929989399894998959989699897998989989999900999019990299903999049990599906999079990899909999109991199912999139991499915999169991799918999199992099921999229992399924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999100000100001100002100003100004100005100006100007100008100009100010100011100012100013100014100015100016100017100018100019100020100021100022100023100024100025100026100027100028100029100030100031100032100033100034100035100036100037100038100039100040100041100042100043100044100045100046100047100048100049100050100051100052100053100054100055100056100057100058100059100060100061100062100063100064100065100066100067100068100069100070100071100072100073100074100075100076100077100078100079100080100081100082100083100084100085100086100087100088100089100090100091100092100093100094100095100096100097100098100099100100100101100102100103100104100105100106100107100108100109100110100111100112100113100114100115100116100117100118100119100120100121100122100123100124100125100126100127100128100129100130100131100132100133100134100135100136100137100138100139100140100141100142100143100144100145100146100147100148100149100150100151100152100153100154100155100156100157100158100159100160100161100162100163100164100165100166100167100168100169100170100171100172100173100174100175100176100177100178100179100180100181100182100183100184100185100186100187100188100189100190100191100192100193100194100195100196100197100198100199100200100201100202100203100204100205100206100207100208100209100210100211100212100213100214100215100216100217100218100219100220100221100222100223100224100225100226100227100228100229100230100231100232100233100234100235100236100237100238100239100240100241100242100243100244100245100246100247100248100249100250100251100252100253100254100255100256100257100258100259100260100261100262100263100264100265100266100267100268100269100270100271100272100273100274100275100276100277100278100279100280100281100282100283100284100285100286100287100288100289100290100291100292100293100294100295100296100297100298100299100300100301100302100303100304100305100306100307100308100309100310100311100312100313100314100315100316100317100318100319100320100321100322100323100324100325100326100327100328100329100330100331100332100333100334100335100336100337100338100339100340100341100342100343100344100345100346100347100348100349100350100351100352100353100354100355100356100357100358100359100360100361100362100363100364100365100366100367100368100369100370100371100372100373100374100375100376100377100378100379100380100381100382100383100384100385100386100387100388100389100390100391100392100393100394100395100396100397100398100399100400100401100402100403100404100405100406100407100408100409100410100411100412100413100414100415100416100417100418100419100420100421100422100423100424100425100426100427100428100429100430100431100432100433100434100435100436100437100438100439100440100441100442100443100444100445100446100447100448100449100450100451100452100453100454100455100456100457100458100459100460100461100462100463100464100465100466100467100468100469100470100471100472100473100474100475100476100477100478100479100480100481100482100483100484100485100486100487100488100489100490100491100492100493100494100495100496100497100498100499100500100501100502100503100504100505100506100507100508100509100510100511100512100513100514100515100516100517100518100519100520100521100522100523100524100525100526100527100528100529100530100531100532100533100534100535100536100537100538100539100540100541100542100543100544100545100546100547100548100549100550100551100552100553100554100555100556100557100558100559100560100561100562100563100564100565100566100567100568100569100570100571100572100573100574100575100576100577100578100579100580100581100582100583100584100585100586100587100588100589100590100591100592100593100594100595100596100597100598100599100600100601100602100603100604100605100606100607100608100609100610100611100612100613100614100615100616100617100618100619100620100621100622100623100624100625100626100627100628100629100630100631100632100633100634100635100636100637100638100639100640100641100642100643100644100645100646100647100648100649100650100651100652100653100654100655100656100657100658100659100660100661100662100663100664100665100666100667100668100669100670100671100672100673100674100675100676100677100678100679100680100681100682100683100684100685100686100687100688100689100690100691100692100693100694100695100696100697100698100699100700100701100702100703100704100705100706100707100708100709100710100711100712100713100714100715100716100717100718100719100720100721100722100723100724100725100726100727100728100729100730100731100732100733100734100735100736100737100738100739100740100741100742100743100744100745100746100747100748100749100750100751100752100753100754100755100756100757100758100759100760100761100762100763100764100765100766100767100768100769100770100771100772100773100774100775100776100777100778100779100780100781100782100783100784100785100786100787100788100789100790100791100792100793100794100795100796100797100798100799100800100801100802100803100804100805100806100807100808100809100810100811100812100813100814100815100816100817100818100819100820100821100822100823100824100825100826100827100828100829100830100831100832100833100834100835100836100837100838100839100840100841100842100843100844100845100846100847100848100849100850100851100852100853100854100855100856100857100858100859100860100861100862100863100864100865100866100867100868100869100870100871100872100873100874100875100876100877100878100879100880100881100882100883100884100885100886100887100888100889100890100891100892100893100894100895100896100897100898100899100900100901100902100903100904100905100906100907100908100909100910100911100912100913100914100915100916100917100918100919100920100921100922100923100924100925100926100927100928100929100930100931100932100933100934100935100936100937100938100939100940100941100942100943100944100945100946100947100948100949100950100951100952100953100954100955100956100957100958100959100960100961100962100963100964100965100966100967100968100969100970100971100972100973100974100975100976100977100978100979100980100981100982100983100984100985100986100987100988100989100990100991100992100993100994100995100996100997100998100999101000101001101002101003101004101005101006101007101008101009101010101011101012101013101014101015101016101017101018101019101020101021101022101023101024101025101026101027101028101029101030101031101032101033101034101035101036101037101038101039101040101041101042101043101044101045101046101047101048101049101050101051101052101053101054101055101056101057101058101059101060101061101062101063101064101065101066101067101068101069101070101071101072101073101074101075101076101077101078101079101080101081101082101083101084101085101086101087101088101089101090101091101092101093101094101095101096101097101098101099101100101101101102101103101104101105101106101107101108101109101110101111101112101113101114101115101116101117101118101119101120101121101122101123101124101125101126101127101128101129101130101131101132101133101134101135101136101137101138101139101140101141101142101143101144101145101146101147101148101149101150101151101152101153101154101155101156101157101158101159101160101161101162101163101164101165101166101167101168101169101170101171101172101173101174101175101176101177101178101179101180101181101182101183101184101185101186101187101188101189101190101191101192101193101194101195101196101197101198101199101200101201101202101203101204101205101206101207101208101209101210101211101212101213101214101215101216101217101218101219101220101221101222101223101224101225101226101227101228101229101230101231101232101233101234101235101236101237101238101239101240101241101242101243101244101245101246101247101248101249101250101251101252101253101254101255101256101257101258101259101260101261101262101263101264101265101266101267101268101269101270101271101272101273101274101275101276101277101278101279101280101281101282101283101284101285101286101287101288101289101290101291101292101293101294101295101296101297101298101299101300101301101302101303101304101305101306101307101308101309101310101311101312101313101314101315101316101317101318101319101320101321101322101323101324101325101326101327101328101329101330101331101332101333101334101335101336101337101338101339101340101341101342101343101344101345101346101347101348101349101350101351101352101353101354101355101356101357101358101359101360101361101362101363101364101365101366101367101368101369101370101371101372101373101374101375101376101377101378101379101380101381101382101383101384101385101386101387101388101389101390101391101392101393101394101395101396101397101398101399101400101401101402101403101404101405101406101407101408101409101410101411101412101413101414101415101416101417101418101419101420101421101422101423101424101425101426101427101428101429101430101431101432101433101434101435101436101437101438101439101440101441101442101443101444101445101446101447101448101449101450101451101452101453101454101455101456101457101458101459101460101461101462101463101464101465101466101467101468101469101470101471101472101473101474101475101476101477101478101479101480101481101482101483101484101485101486101487101488101489101490101491101492101493101494101495101496101497101498101499101500101501101502101503101504101505101506101507101508101509101510101511101512101513101514101515101516101517101518101519101520101521101522101523101524101525101526101527101528101529101530101531101532101533101534101535101536101537101538101539101540101541101542101543101544101545101546101547101548101549101550101551101552101553101554101555101556101557101558101559101560101561101562101563101564101565101566101567101568101569101570101571101572101573101574101575101576101577101578101579101580101581101582101583101584101585101586101587101588101589101590101591101592101593101594101595101596101597101598101599101600101601101602101603101604101605101606101607101608101609101610101611101612101613101614101615101616101617101618101619101620101621101622101623101624101625101626101627101628101629101630101631101632101633101634101635101636101637101638101639101640101641101642101643101644101645101646101647101648101649101650101651101652101653101654101655101656101657101658101659101660101661101662101663101664101665101666101667101668101669101670101671101672101673101674101675101676101677101678101679101680101681101682101683101684101685101686101687101688101689101690101691101692101693101694101695101696101697101698101699101700101701101702101703101704101705101706101707101708101709101710101711101712101713101714101715101716101717101718101719101720101721101722101723101724101725101726101727101728101729101730101731101732101733101734101735101736101737101738101739101740101741101742101743101744101745101746101747101748101749101750101751101752101753101754101755101756101757101758101759101760101761101762101763101764101765101766101767101768101769101770101771101772101773101774101775101776101777101778101779101780101781101782101783101784101785101786101787101788101789101790101791101792101793101794101795101796101797101798101799101800101801101802101803101804101805101806101807101808101809101810101811101812101813101814101815101816101817101818101819101820101821101822101823101824101825101826101827101828101829101830101831101832101833101834101835101836101837101838101839101840101841101842101843101844101845101846101847101848101849101850101851101852101853101854101855101856101857101858101859101860101861101862101863101864101865101866101867101868101869101870101871101872101873101874101875101876101877101878101879101880101881101882101883101884101885101886101887101888101889101890101891101892101893101894101895101896101897101898101899101900101901101902101903101904101905101906101907101908101909101910101911101912101913101914101915101916101917101918101919101920101921101922101923101924101925101926101927101928101929101930101931101932101933101934101935101936101937101938101939101940101941101942101943101944101945101946101947101948101949101950101951101952101953101954101955101956101957101958101959101960101961101962101963101964101965101966101967101968101969101970101971101972101973101974101975101976101977101978101979101980101981101982101983101984101985101986101987101988101989101990101991101992101993101994101995101996101997101998101999102000102001102002102003102004102005102006102007102008102009102010102011102012102013102014102015102016102017102018102019102020102021102022102023102024102025102026102027102028102029102030102031102032102033102034102035102036102037102038102039102040102041102042102043102044102045102046102047102048102049102050102051102052102053102054102055102056102057102058102059102060102061102062102063102064102065102066102067102068102069102070102071102072102073102074102075102076102077102078102079102080102081102082102083102084102085102086102087102088102089102090102091102092102093102094102095102096102097102098102099102100102101102102102103102104102105102106102107102108102109102110102111102112102113102114102115102116102117102118102119102120102121102122102123102124102125102126102127102128102129102130102131102132102133102134102135102136102137102138102139102140102141102142102143102144102145102146102147102148102149102150102151102152102153102154102155102156102157102158102159102160102161102162102163102164102165102166102167102168102169102170102171102172102173102174102175102176102177102178102179102180102181102182102183102184102185102186102187102188102189102190102191102192102193102194102195102196102197102198102199102200102201102202102203102204102205102206102207102208102209102210102211102212102213102214102215102216102217102218102219102220102221102222102223102224102225102226102227102228102229102230102231102232102233102234102235102236102237102238102239102240102241102242102243102244102245102246102247102248102249102250102251102252102253102254102255102256102257102258102259102260102261102262102263102264102265102266102267102268102269102270102271102272102273102274102275102276102277102278102279102280102281102282102283102284102285102286102287102288102289102290102291102292102293102294102295102296102297102298102299102300102301102302102303102304102305102306102307102308102309102310102311102312102313102314102315102316102317102318102319102320102321102322102323102324102325102326102327102328102329102330102331102332102333102334102335102336102337102338102339102340102341102342102343102344102345102346102347102348102349102350102351102352102353102354102355102356102357102358102359102360102361102362102363102364102365102366102367102368102369102370102371102372102373102374102375102376102377102378102379102380102381102382102383102384102385102386102387102388102389102390102391102392102393102394102395102396102397102398102399102400102401102402102403102404102405102406102407102408102409102410102411102412102413102414102415102416102417102418102419102420102421102422102423102424102425102426102427102428102429102430102431102432102433102434102435102436102437102438102439102440102441102442102443102444102445102446102447102448102449102450102451102452102453102454102455102456102457102458102459102460102461102462102463102464102465102466102467102468102469102470102471102472102473102474102475102476102477102478102479102480102481102482102483102484102485102486102487102488102489102490102491102492102493102494102495102496102497102498102499102500102501102502102503102504102505102506102507102508102509102510102511102512102513102514102515102516102517102518102519102520102521102522102523102524102525102526102527102528102529102530102531102532102533102534102535102536102537102538102539102540102541102542102543102544102545102546102547102548102549102550102551102552102553102554102555102556102557102558102559102560102561102562102563102564102565102566102567102568102569102570102571102572102573102574102575102576102577102578102579102580102581102582102583102584102585102586102587102588102589102590102591102592102593102594102595102596102597102598102599102600102601102602102603102604102605102606102607102608102609102610102611102612102613102614102615102616102617102618102619102620102621102622102623102624102625102626102627102628102629102630102631102632102633102634102635102636102637102638102639102640102641102642102643102644102645102646102647102648102649102650102651102652102653102654102655102656102657102658102659102660102661102662102663102664102665102666102667102668102669102670102671102672102673102674102675102676102677102678102679102680102681102682102683102684102685102686102687102688102689102690102691102692102693102694102695102696102697102698102699102700102701102702102703102704102705102706102707102708102709102710102711102712102713102714102715102716102717102718102719102720102721102722102723102724102725102726102727102728102729102730102731102732102733102734102735102736102737102738102739102740102741102742102743102744102745102746102747102748102749102750102751102752102753102754102755102756102757102758102759102760102761102762102763102764102765102766102767102768102769102770102771102772102773102774102775102776102777102778102779102780102781102782102783102784102785102786102787102788102789102790102791102792102793102794102795102796102797102798102799102800102801102802102803102804102805102806102807102808102809102810102811102812102813102814102815102816102817102818102819102820102821102822102823102824102825102826102827102828102829102830102831102832102833102834102835102836102837102838102839102840102841102842102843102844102845102846102847102848102849102850102851102852102853102854102855102856102857102858102859102860102861102862102863102864102865102866102867102868102869102870102871102872102873102874102875102876102877102878102879102880102881102882102883102884102885102886102887102888102889102890102891102892102893102894102895102896102897102898102899102900102901102902102903102904102905102906102907102908102909102910102911102912102913102914102915102916102917102918102919102920102921102922102923102924102925102926102927102928102929102930102931102932102933102934102935102936102937102938102939102940102941102942102943102944102945102946102947102948102949102950102951102952102953102954102955102956102957102958102959102960102961102962102963102964102965102966102967102968102969102970102971102972102973102974102975102976102977102978102979102980102981102982102983102984102985102986102987102988102989102990102991102992102993102994102995102996102997102998102999103000103001103002103003103004103005103006103007103008103009103010103011103012103013103014103015103016103017103018103019103020103021103022103023103024103025103026103027103028103029103030103031103032103033103034103035103036103037103038103039103040103041103042103043103044103045103046103047103048103049103050103051103052103053103054103055103056103057103058103059103060103061103062103063103064103065103066103067103068103069103070103071103072103073103074103075103076103077103078103079103080103081103082103083103084103085103086103087103088103089103090103091103092103093103094103095103096103097103098103099103100103101103102103103103104103105103106103107103108103109103110103111103112103113103114103115103116103117103118103119103120103121103122103123103124103125103126103127103128103129103130103131103132103133103134103135103136103137103138103139103140103141103142103143103144103145103146103147103148103149103150103151103152103153103154103155103156103157103158103159103160103161103162103163103164103165103166103167103168103169103170103171103172103173103174103175103176103177103178103179103180103181103182103183103184103185103186103187103188103189103190103191103192103193103194103195103196103197103198103199103200103201103202103203103204103205103206103207103208103209103210103211103212103213103214103215103216103217103218103219103220103221103222103223103224103225103226103227103228103229103230103231103232103233103234103235103236103237103238103239103240103241103242103243103244103245103246103247103248103249103250103251103252103253103254103255103256103257103258103259103260103261103262103263103264103265103266103267103268103269103270103271103272103273103274103275103276103277103278103279103280103281103282103283103284103285103286103287103288103289103290103291103292103293103294103295103296103297103298103299103300103301103302103303103304103305103306103307103308103309103310103311103312103313103314103315103316103317103318103319103320103321103322103323103324103325103326103327103328103329103330103331103332103333103334103335103336103337103338103339103340103341103342103343103344103345103346103347103348103349103350103351103352103353103354103355103356103357103358103359103360103361103362103363103364103365103366103367103368103369103370103371103372103373103374103375103376103377103378103379103380103381103382103383103384103385103386103387103388103389103390103391103392103393103394103395103396103397103398103399103400103401103402103403103404103405103406103407103408103409103410103411103412103413103414103415103416103417103418103419103420103421103422103423103424103425103426103427103428103429103430103431103432103433103434103435103436103437103438103439103440103441103442103443103444103445103446103447103448103449103450103451103452103453103454103455103456103457103458103459103460103461103462103463103464103465103466103467103468103469103470103471103472103473103474103475103476103477103478103479103480103481103482103483103484103485103486103487103488103489103490103491103492103493103494103495103496103497103498103499103500103501103502103503103504103505103506103507103508103509103510103511103512103513103514103515103516103517103518103519103520103521103522103523103524103525103526103527103528103529103530103531103532103533103534103535103536103537103538103539103540103541103542103543103544103545103546103547103548103549103550103551103552103553103554103555103556103557103558103559103560103561103562103563103564103565103566103567103568103569103570103571103572103573103574103575103576103577103578103579103580103581103582103583103584103585103586103587103588103589103590103591103592103593103594103595103596103597103598103599103600103601103602103603103604103605103606103607103608103609103610103611103612103613103614103615103616103617103618103619103620103621103622103623103624103625103626103627103628103629103630103631103632103633103634103635103636103637103638103639103640103641103642103643103644103645103646103647103648103649103650103651103652103653103654103655103656103657103658103659103660103661103662103663103664103665103666103667103668103669103670103671103672103673103674103675103676103677103678103679103680103681103682103683103684103685103686103687103688103689103690103691103692103693103694103695103696103697103698103699103700103701103702103703103704103705103706103707103708103709103710103711103712103713103714103715103716103717103718103719103720103721103722103723103724103725103726103727103728103729103730103731103732103733103734103735103736103737103738103739103740103741103742103743103744103745103746103747103748103749103750103751103752103753103754103755103756103757103758103759103760103761103762103763103764103765103766103767103768103769103770103771103772103773103774103775103776103777103778103779103780103781103782103783103784103785103786103787103788103789103790103791103792103793103794103795103796103797103798103799103800103801103802103803103804103805103806103807103808103809103810103811103812103813103814103815103816103817103818103819103820103821103822103823103824103825103826103827103828103829103830103831103832103833103834103835103836103837103838103839103840103841103842103843103844103845103846103847103848103849103850103851103852103853103854103855103856103857103858103859103860103861103862103863103864103865103866103867103868103869103870103871103872103873103874103875103876103877103878103879103880103881103882103883103884103885103886103887103888103889103890103891103892103893103894103895103896103897103898103899103900103901103902103903103904103905103906103907103908103909103910103911103912103913103914103915103916103917103918103919103920103921103922103923103924103925103926103927103928103929103930103931103932103933103934103935103936103937103938103939103940103941103942103943103944103945103946103947103948103949103950103951103952103953103954103955103956103957103958103959103960103961103962103963103964103965103966103967103968103969103970103971103972103973103974103975103976103977103978103979103980103981103982103983103984103985103986103987103988103989103990103991103992103993103994103995103996103997103998103999104000104001104002104003104004104005104006104007104008104009104010104011104012104013104014104015104016104017104018104019104020104021104022104023104024104025104026104027104028104029104030104031104032104033104034104035104036104037104038104039104040104041104042104043104044104045104046104047104048104049104050104051104052104053104054104055104056104057104058104059104060104061104062104063104064104065104066104067104068104069104070104071104072104073104074104075104076104077104078104079104080104081104082104083104084104085104086104087104088104089104090104091104092104093104094104095104096104097104098104099104100104101104102104103104104104105104106104107104108104109104110104111104112104113104114104115104116104117104118104119104120104121104122104123104124104125104126104127104128104129104130104131104132104133104134104135104136104137104138104139104140104141104142104143104144104145104146104147104148104149104150104151104152104153104154104155104156104157104158104159104160104161104162104163104164104165104166104167104168104169104170104171104172104173104174104175104176104177104178104179104180104181104182104183104184104185104186104187104188104189104190104191104192104193104194104195104196104197104198104199104200104201104202104203104204104205104206104207104208104209104210104211104212104213104214104215104216104217104218104219104220104221104222104223104224104225104226104227104228104229104230104231104232104233104234104235104236104237104238104239104240104241104242104243104244104245104246104247104248104249104250104251104252104253104254104255104256104257104258104259104260104261104262104263104264104265104266104267104268104269104270104271104272104273104274104275104276104277104278104279104280104281104282104283104284104285104286104287104288104289104290104291104292104293104294104295104296104297104298104299104300104301104302104303104304104305104306104307104308104309104310104311104312104313104314104315104316104317104318104319104320104321104322104323104324104325104326104327104328104329104330104331104332104333104334104335104336104337104338104339104340104341104342104343104344104345104346104347104348104349104350104351104352104353104354104355104356104357104358104359104360104361104362104363104364104365104366104367104368104369104370104371104372104373104374104375104376104377104378104379104380104381104382104383104384104385104386104387104388104389104390104391104392104393104394104395104396104397104398104399104400104401104402104403104404104405104406104407104408104409104410104411104412104413104414104415104416104417104418104419104420104421104422104423104424104425104426104427104428104429104430104431104432104433104434104435104436104437104438104439104440104441104442104443104444104445104446104447104448104449104450104451104452104453104454104455104456104457104458104459104460104461104462104463104464104465104466104467104468104469104470104471104472104473104474104475104476104477104478104479104480104481104482104483104484104485104486104487104488104489104490104491104492104493104494104495104496104497104498104499104500104501104502104503104504104505104506104507104508104509104510104511104512104513104514104515104516104517104518104519104520104521104522104523104524104525104526104527104528104529104530104531104532104533104534104535104536104537104538104539104540104541104542104543104544104545104546104547104548104549104550104551104552104553104554104555104556104557104558104559104560104561104562104563104564104565104566104567104568104569104570104571104572104573104574104575104576104577104578104579104580104581104582104583104584104585104586104587104588104589104590104591104592104593104594104595104596104597104598104599104600104601104602104603104604104605104606104607104608104609104610104611104612104613104614104615104616104617104618104619104620104621104622104623104624104625104626104627104628104629104630104631104632104633104634104635104636104637104638104639104640104641104642104643104644104645104646104647104648104649104650104651104652104653104654104655104656104657104658104659104660104661104662104663104664104665104666104667104668104669104670104671104672104673104674104675104676104677104678104679104680104681104682104683104684104685104686104687104688104689104690104691104692104693104694104695104696104697104698104699104700104701104702104703104704104705104706104707104708104709104710104711104712104713104714104715104716104717104718104719104720104721104722104723104724104725104726104727104728104729104730104731104732104733104734104735104736104737104738104739104740104741104742104743104744104745104746104747104748104749104750104751104752104753104754104755104756104757104758104759104760104761104762104763104764104765104766104767104768104769104770104771104772104773104774104775104776104777104778104779104780104781104782104783104784104785104786104787104788104789104790104791104792104793104794104795104796104797104798104799104800104801104802104803104804104805104806104807104808104809104810104811104812104813104814104815104816104817104818104819104820104821104822104823104824104825104826104827104828104829104830104831104832104833104834104835104836104837104838104839104840104841104842104843104844104845104846104847104848104849104850104851104852104853104854104855104856104857104858104859104860104861104862104863104864104865104866104867104868104869104870104871104872104873104874104875104876104877104878104879104880104881104882104883104884104885104886104887104888104889104890104891104892104893104894104895104896104897104898104899104900104901104902104903104904104905104906104907104908104909104910104911104912104913104914104915104916104917104918104919104920104921104922104923104924104925104926104927104928104929104930104931104932104933104934104935104936104937104938104939104940104941104942104943104944104945104946104947104948104949104950104951104952104953104954104955104956104957104958104959104960104961104962104963104964104965104966104967104968104969104970104971104972104973104974104975104976104977104978104979104980104981104982104983104984104985104986104987104988104989104990104991104992104993104994104995104996104997104998104999105000105001105002105003105004105005105006105007105008105009105010105011105012105013105014105015105016105017105018105019105020105021105022105023105024105025105026105027105028105029105030105031105032105033105034105035105036105037105038105039105040105041105042105043105044105045105046105047105048105049105050105051105052105053105054105055105056105057105058105059105060105061105062105063105064105065105066105067105068105069105070105071105072105073105074105075105076105077105078105079105080105081105082105083105084105085105086105087105088105089105090105091105092105093105094105095105096105097105098105099105100105101105102105103105104105105105106105107105108105109105110105111105112105113105114105115105116105117105118105119105120105121105122105123105124105125105126105127105128105129105130105131105132105133105134105135105136105137105138105139105140105141105142105143105144105145105146105147105148105149105150105151105152105153105154105155105156105157105158105159105160105161105162105163105164105165105166105167105168105169105170105171105172105173105174105175105176105177105178105179105180105181105182105183105184105185105186105187105188105189105190105191105192105193105194105195105196105197105198105199105200105201105202105203105204105205105206105207105208105209105210105211105212105213105214105215105216105217105218105219105220105221105222105223105224105225105226105227105228105229105230105231105232105233105234105235105236105237105238105239105240105241105242105243105244105245105246105247105248105249105250105251105252105253105254105255105256105257105258105259105260105261105262105263105264105265105266105267105268105269105270105271105272105273105274105275105276105277105278105279105280105281105282105283105284105285105286105287105288105289105290105291105292105293105294105295105296105297105298105299105300105301105302105303105304105305105306105307105308105309105310105311105312105313105314105315105316105317105318105319105320105321105322105323105324105325105326105327105328105329105330105331105332105333105334105335105336105337105338105339105340105341105342105343105344105345105346105347105348105349105350105351105352105353105354105355105356105357105358105359105360105361105362105363105364105365105366105367105368105369105370105371105372105373105374105375105376105377105378105379105380105381105382105383105384105385105386105387105388105389105390105391105392105393105394105395105396105397105398105399105400105401105402105403105404105405105406105407105408105409105410105411105412105413105414105415105416105417105418105419105420105421105422105423105424105425105426105427105428105429105430105431105432105433105434105435105436105437105438105439105440105441105442105443105444105445105446105447105448105449105450105451105452105453105454105455105456105457105458105459105460105461105462105463105464105465105466105467105468105469105470105471105472105473105474105475105476105477105478105479105480105481105482105483105484105485105486105487105488105489105490105491105492105493105494105495105496105497105498105499105500105501105502105503105504105505105506105507105508105509105510105511105512105513105514105515105516105517105518105519105520105521105522105523105524105525105526105527105528105529105530105531105532105533105534105535105536105537105538105539105540105541105542105543105544105545105546105547105548105549105550105551105552105553105554105555105556105557105558105559105560105561105562105563105564105565105566105567105568105569105570105571105572105573105574105575105576105577105578105579105580105581105582105583105584105585105586105587105588105589105590105591105592105593105594105595105596105597105598105599105600105601105602105603105604105605105606105607105608105609105610105611105612105613105614105615105616105617105618105619105620105621105622105623105624105625105626105627105628105629105630105631105632105633105634105635105636105637105638105639105640105641105642105643105644105645105646105647105648105649105650105651105652105653105654105655105656105657105658105659105660105661105662105663105664105665105666105667105668105669105670105671105672105673105674105675105676105677105678105679105680105681105682105683105684105685105686105687105688105689105690105691105692105693105694105695105696105697105698105699105700105701105702105703105704105705105706105707105708105709105710105711105712105713105714105715105716105717105718105719105720105721105722105723105724105725105726105727105728105729105730105731105732105733105734105735105736105737105738105739105740105741105742105743105744105745105746105747105748105749105750105751105752105753105754105755105756105757105758105759105760105761105762105763105764105765105766105767105768105769105770105771105772105773105774105775105776105777105778105779105780105781105782105783105784105785105786105787105788105789105790105791105792105793105794105795105796105797105798105799105800105801105802105803105804105805105806105807105808105809105810105811105812105813105814105815105816105817105818105819105820105821105822105823105824105825105826105827105828105829105830105831105832105833105834105835105836105837105838105839105840105841105842105843105844105845105846105847105848105849105850105851105852105853105854105855105856105857105858105859105860105861105862105863105864105865105866105867105868105869105870105871105872105873105874105875105876105877105878105879105880105881105882105883105884105885105886105887105888105889105890105891105892105893105894105895105896105897105898105899105900105901105902105903105904105905105906105907105908105909105910105911105912105913105914105915105916105917105918105919105920105921105922105923105924105925105926105927105928105929105930105931105932105933105934105935105936105937105938105939105940105941105942105943105944105945105946105947105948105949105950105951105952105953105954105955105956105957105958105959105960105961105962105963105964105965105966105967105968105969105970105971105972105973105974105975105976105977105978105979105980105981105982105983105984105985105986105987105988105989105990105991105992105993105994105995105996105997105998105999106000106001106002106003106004106005106006106007106008106009106010106011106012106013106014106015106016106017106018106019106020106021106022106023106024106025106026106027106028106029106030106031106032106033106034106035106036106037106038106039106040106041106042106043106044106045106046106047106048106049106050106051106052106053106054106055106056106057106058106059106060106061106062106063106064106065106066106067106068106069106070106071106072106073106074106075106076106077106078106079106080106081106082106083106084106085106086106087106088106089106090106091106092106093106094106095106096106097106098106099106100106101106102106103106104106105106106106107106108106109106110106111106112106113106114106115106116106117106118106119106120106121106122106123106124106125106126106127106128106129106130106131106132106133106134106135106136106137106138106139106140106141106142106143106144106145106146106147106148106149106150106151106152106153106154106155106156106157106158106159106160106161106162106163106164106165106166106167106168106169106170106171106172106173106174106175106176106177106178106179106180106181106182106183106184106185106186106187106188106189106190106191106192106193106194106195106196106197106198106199106200106201106202106203106204106205106206106207106208106209106210106211106212106213106214106215106216106217106218106219106220106221106222106223106224106225106226106227106228106229106230106231106232106233106234106235106236106237106238106239106240106241106242106243106244106245106246106247106248106249106250106251106252106253106254106255106256106257106258106259106260106261106262106263106264106265106266106267106268106269106270106271106272106273106274106275106276106277106278106279106280106281106282106283106284106285106286106287106288106289106290106291106292106293106294106295106296106297106298106299106300106301106302106303106304106305106306106307106308106309106310106311106312106313106314106315106316106317106318106319106320106321106322106323106324106325106326106327106328106329106330106331106332106333106334106335106336106337106338106339106340106341106342106343106344106345106346106347106348106349106350106351106352106353106354106355106356106357106358106359106360106361106362106363106364106365106366106367106368106369106370106371106372106373106374106375106376106377106378106379106380106381106382106383106384106385106386106387106388106389106390106391106392106393106394106395106396106397106398106399106400106401106402106403106404106405106406106407106408106409106410106411106412106413106414106415106416106417106418106419106420106421106422106423106424106425106426106427106428106429106430106431106432106433106434106435106436106437106438106439106440106441106442106443106444106445106446106447106448106449106450106451106452106453106454106455106456106457106458106459106460106461106462106463106464106465106466106467106468106469106470106471106472106473106474106475106476106477106478106479106480106481106482106483106484106485106486106487106488106489106490106491106492106493106494106495106496106497106498106499106500106501106502106503106504106505106506106507106508106509106510106511106512106513106514106515106516106517106518106519106520106521106522106523106524106525106526106527106528106529106530106531106532106533106534106535106536106537106538106539106540106541106542106543106544106545106546106547106548106549106550106551106552106553106554106555106556106557106558106559106560106561106562106563106564106565106566106567106568106569106570106571106572106573106574106575106576106577106578106579106580106581106582106583106584106585106586106587106588106589106590106591106592106593106594106595106596106597106598106599106600106601106602106603106604106605106606106607106608106609106610106611106612106613106614106615106616106617106618106619106620106621106622106623106624106625106626106627106628106629106630106631106632106633106634106635106636106637106638106639106640106641106642106643106644106645106646106647106648106649106650106651106652106653106654106655106656106657106658106659106660106661106662106663106664106665106666106667106668106669106670106671106672106673106674106675106676106677106678106679106680106681106682106683106684106685106686106687106688106689106690106691106692106693106694106695106696106697106698106699106700106701106702106703106704106705106706106707106708106709106710106711106712106713106714106715106716106717106718106719106720106721106722106723106724106725106726106727106728106729106730106731106732106733106734106735106736106737106738106739106740106741106742106743106744106745106746106747106748106749106750106751106752106753106754106755106756106757106758106759106760106761106762106763106764106765106766106767106768106769106770106771106772106773106774106775106776106777106778106779106780106781106782106783106784106785106786106787106788106789106790106791106792106793106794106795106796106797106798106799106800106801106802106803106804106805106806106807106808106809106810106811106812106813106814106815106816106817106818106819106820106821106822106823106824106825106826106827106828106829106830106831106832106833106834106835106836106837106838106839106840106841106842106843106844106845106846106847106848106849106850106851106852106853106854106855106856106857106858106859106860106861106862106863106864106865106866106867106868106869106870106871106872106873106874106875106876106877106878106879106880106881106882106883106884106885106886106887106888106889106890106891106892106893106894106895106896106897106898106899106900106901106902106903106904106905106906106907106908106909106910106911106912106913106914106915106916106917106918106919106920106921106922106923106924106925106926106927106928106929106930106931106932106933106934106935106936106937106938106939106940106941106942106943106944106945106946106947106948106949106950106951106952106953106954106955106956106957106958106959106960106961106962106963106964106965106966106967106968106969106970106971106972106973106974106975106976106977106978106979106980106981106982106983106984106985106986106987106988106989106990106991106992106993106994106995106996106997106998106999107000107001107002107003107004107005107006107007107008107009107010107011107012107013107014107015107016107017107018107019107020107021107022107023107024107025107026107027107028107029107030107031107032107033107034107035107036107037107038107039107040107041107042107043107044107045107046107047107048107049107050107051107052107053107054107055107056107057107058107059107060107061107062107063107064107065107066107067107068107069107070107071107072107073107074107075107076107077107078107079107080107081107082107083107084107085107086107087107088107089107090107091107092107093107094107095107096107097107098107099107100107101107102107103107104107105107106107107107108107109107110107111107112107113107114107115107116107117107118107119107120107121107122107123107124107125107126107127107128107129107130107131107132107133107134107135107136107137107138107139107140107141107142107143107144107145107146107147107148107149107150107151107152107153107154107155107156107157107158107159107160107161107162107163107164107165107166107167107168107169107170107171107172107173107174107175107176107177107178107179107180107181107182107183107184107185107186107187107188107189107190107191107192107193107194107195107196107197107198107199107200107201107202107203107204107205107206107207107208107209107210107211107212107213107214107215107216107217107218107219107220107221107222107223107224107225107226107227107228107229107230107231107232107233107234107235107236107237107238107239107240107241107242107243107244107245107246107247107248107249107250107251107252107253107254107255107256107257107258107259107260107261107262107263107264107265107266107267107268107269107270107271107272107273107274107275107276107277107278107279107280107281107282107283107284107285107286107287107288107289107290107291107292107293107294107295107296107297107298107299107300107301107302107303107304107305107306107307107308107309107310107311107312107313107314107315107316107317107318107319107320107321107322107323107324107325107326107327107328107329107330107331107332107333107334107335107336107337107338107339107340107341107342107343107344107345107346107347107348107349107350107351107352107353107354107355107356107357107358107359107360107361107362107363107364107365107366107367107368107369107370107371107372107373107374107375107376107377107378107379107380107381107382107383107384107385107386107387107388107389107390107391107392107393107394107395107396107397107398107399107400107401107402107403107404107405107406107407107408107409107410107411107412107413107414107415107416107417107418107419107420107421107422107423107424107425107426107427107428107429107430107431107432107433107434107435107436107437107438107439107440107441107442107443107444107445107446107447107448107449107450107451107452107453107454107455107456107457107458107459107460107461107462107463107464107465107466107467107468107469107470107471107472107473107474107475107476107477107478107479107480107481107482107483107484107485107486107487107488107489107490107491107492107493107494107495107496107497107498107499107500107501107502107503107504107505107506107507107508107509107510107511107512107513107514107515107516107517107518107519107520107521107522107523107524107525107526107527107528107529107530107531107532107533107534107535107536107537107538107539107540107541107542107543107544107545107546107547107548107549107550107551107552107553107554107555107556107557107558107559107560107561107562107563107564107565107566107567107568107569107570107571107572107573107574107575107576107577107578107579107580107581107582107583107584107585107586107587107588107589107590107591107592107593107594107595107596107597107598107599107600107601107602107603107604107605107606107607107608107609107610107611107612107613107614107615107616107617107618107619107620107621107622107623107624107625107626107627107628107629107630107631107632107633107634107635107636107637107638107639107640107641107642107643107644107645107646107647107648107649107650107651107652107653107654107655107656107657107658107659107660107661107662107663107664107665107666107667107668107669107670107671107672107673107674107675107676107677107678107679107680107681107682107683107684107685107686107687107688107689107690107691107692107693107694107695107696107697107698107699107700107701107702107703107704107705107706107707107708107709107710107711107712107713107714107715107716107717107718107719107720107721107722107723107724107725107726107727107728107729107730107731107732107733107734107735107736107737107738107739107740107741107742107743107744107745107746107747107748107749107750107751107752107753107754107755107756107757107758107759107760107761107762107763107764107765107766107767107768107769107770107771107772107773107774107775107776107777107778107779107780107781107782107783107784107785107786107787107788107789107790107791107792107793107794107795107796107797107798107799107800107801107802107803107804107805107806107807107808107809107810107811107812107813107814107815107816107817107818107819107820107821107822107823107824107825107826107827107828107829107830107831107832107833107834107835107836107837107838107839107840107841107842107843107844107845107846107847107848107849107850107851107852107853107854107855107856107857107858107859107860107861107862107863107864107865107866107867107868107869107870107871107872107873107874107875107876107877107878107879107880107881107882107883107884107885107886107887107888107889107890107891107892107893107894107895107896107897107898107899107900107901107902107903107904107905107906107907107908107909107910107911107912107913107914107915107916107917107918107919107920107921107922107923107924107925107926107927107928107929107930107931107932107933107934107935107936107937107938107939107940107941107942107943107944107945107946107947107948107949107950107951107952107953107954107955107956107957107958107959107960107961107962107963107964107965107966107967107968107969107970107971107972107973107974107975107976107977107978107979107980107981107982107983107984107985107986107987107988107989107990107991107992107993107994107995107996107997107998107999108000108001108002108003108004108005108006108007108008108009108010108011108012108013108014108015108016108017108018108019108020108021108022108023108024108025108026108027108028108029108030108031108032108033108034108035108036108037108038108039108040108041108042108043108044108045108046108047108048108049108050108051108052108053108054108055108056108057108058108059108060108061108062108063108064108065108066108067108068108069108070108071108072108073108074108075108076108077108078108079108080108081108082108083108084108085108086108087108088108089108090108091108092108093108094108095108096108097108098108099108100108101108102108103108104108105108106108107108108108109108110108111108112108113108114108115108116108117108118108119108120108121108122108123108124108125108126108127108128108129108130108131108132108133108134108135108136108137108138108139108140108141108142108143108144108145108146108147108148108149108150108151108152108153108154108155108156108157108158108159108160108161108162108163108164108165108166108167108168108169108170108171108172108173108174108175108176108177108178108179108180108181108182108183108184108185108186108187108188108189108190108191108192108193108194108195108196108197108198108199108200108201108202108203108204108205108206108207108208108209108210108211108212108213108214108215108216108217108218108219108220108221108222108223108224108225108226108227108228108229108230108231108232108233108234108235108236108237108238108239108240108241108242108243108244108245108246108247108248108249108250108251108252108253108254108255108256108257108258108259108260108261108262108263108264108265108266108267108268108269108270108271108272108273108274108275108276108277108278108279108280108281108282108283108284108285108286108287108288108289108290108291108292108293108294108295108296108297108298108299108300108301108302108303108304108305108306108307108308108309108310108311108312108313108314108315108316108317108318108319108320108321108322108323108324108325108326108327108328108329108330108331108332108333108334108335108336108337108338108339108340108341108342108343108344108345108346108347108348108349108350108351108352108353108354108355108356108357108358108359108360108361108362108363108364108365108366108367108368108369108370108371108372108373108374108375108376108377108378108379108380108381108382108383108384108385108386108387108388108389108390108391108392108393108394108395108396108397108398108399108400108401108402108403108404108405108406108407108408108409108410108411108412108413108414108415108416108417108418108419108420108421108422108423108424108425108426108427108428108429108430108431108432108433108434108435108436108437108438108439108440108441108442108443108444108445108446108447108448108449108450108451108452108453108454108455108456108457108458108459108460108461108462108463108464108465108466108467108468108469108470108471108472108473108474108475108476108477108478108479108480108481108482108483108484108485108486108487108488108489108490108491108492108493108494108495108496108497108498108499108500108501108502108503108504108505108506108507108508108509108510108511108512108513108514108515108516108517108518108519108520108521108522108523108524108525108526108527108528108529108530108531108532108533108534108535108536108537108538108539108540108541108542108543108544108545108546108547108548108549108550108551108552108553108554108555108556108557108558108559108560108561108562108563108564108565108566108567108568108569108570108571108572108573108574108575108576108577108578108579108580108581108582108583108584108585108586108587108588108589108590108591108592108593108594108595108596108597108598108599108600108601108602108603108604108605108606108607108608108609108610108611108612108613108614108615108616108617108618108619108620108621108622108623108624108625108626108627108628108629108630108631108632108633108634108635108636108637108638108639108640108641108642108643108644108645108646108647108648108649108650108651108652108653108654108655108656108657108658108659108660108661108662108663108664108665108666108667108668108669108670108671108672108673108674108675108676108677108678108679108680108681108682108683108684108685108686108687108688108689108690108691108692108693108694108695108696108697108698108699108700108701108702108703108704108705108706108707108708108709108710108711108712108713108714108715108716108717108718108719108720108721108722108723108724108725108726108727108728108729108730108731108732108733108734108735108736108737108738108739108740108741108742108743108744108745108746108747108748108749108750108751108752108753108754108755108756108757108758108759108760108761108762108763108764108765108766108767108768108769108770108771108772108773108774108775108776108777108778108779108780108781108782108783108784108785108786108787108788108789108790108791108792108793108794108795108796108797108798108799108800108801108802108803108804108805108806108807108808108809108810108811108812108813108814108815108816108817108818108819108820108821108822108823108824108825108826108827108828108829108830108831108832108833108834108835108836108837108838108839108840108841108842108843108844108845108846108847108848108849108850108851108852108853108854108855108856108857108858108859108860108861108862108863108864108865108866108867108868108869108870108871108872108873108874108875108876108877108878108879108880108881108882108883108884108885108886108887108888108889108890108891108892108893108894108895108896108897108898108899108900108901108902108903108904108905108906108907108908108909108910108911108912108913108914108915108916108917108918108919108920108921108922108923108924108925108926108927108928108929108930108931108932108933108934108935108936108937108938108939108940108941108942108943108944108945108946108947108948108949108950108951108952108953108954108955108956108957108958108959108960108961108962108963108964108965108966108967108968108969108970108971108972108973108974108975108976108977108978108979108980108981108982108983108984108985108986108987108988108989108990108991108992108993108994108995108996108997108998108999109000109001109002109003109004109005109006109007109008109009109010109011109012109013109014109015109016109017109018109019109020109021109022109023109024109025109026109027109028109029109030109031109032109033109034109035109036109037109038109039109040109041109042109043109044109045109046109047109048109049109050109051109052109053109054109055109056109057109058109059109060109061109062109063109064109065109066109067109068109069109070109071109072109073109074109075109076109077109078109079109080109081109082109083109084109085109086109087109088109089109090109091109092109093109094109095109096109097109098109099109100109101109102109103109104109105109106109107109108109109109110109111109112109113109114109115109116109117109118109119109120109121109122109123109124109125109126109127109128109129109130109131109132109133109134109135109136109137109138109139109140109141109142109143109144109145109146109147109148109149109150109151109152109153109154109155109156109157109158109159109160109161109162109163109164109165109166109167109168109169109170109171109172109173109174109175109176109177109178109179109180109181109182109183109184109185109186109187109188109189109190109191109192109193109194109195109196109197109198109199109200109201109202109203109204109205109206109207109208109209109210109211109212109213109214109215109216109217109218109219109220109221109222109223109224109225109226109227109228109229109230109231109232109233109234109235109236109237109238109239109240109241109242109243109244109245109246109247109248109249109250109251109252109253109254109255109256109257109258109259109260109261109262109263109264109265109266109267109268109269109270109271109272109273109274109275109276109277109278109279109280109281109282109283109284109285109286109287109288109289109290109291109292109293109294109295109296109297109298109299109300109301109302109303109304109305109306109307109308109309109310109311109312109313109314109315109316109317109318109319109320109321109322109323109324109325109326109327109328109329109330109331109332109333109334109335109336109337109338109339109340109341109342109343109344109345109346109347109348109349109350109351109352109353109354109355109356109357109358109359109360109361109362109363109364109365109366109367109368109369109370109371109372109373109374109375109376109377109378109379109380109381109382109383109384109385109386109387109388109389109390109391109392109393109394109395109396109397109398109399109400109401109402109403109404109405109406109407109408109409109410109411109412109413109414109415109416109417109418109419109420109421109422109423109424109425109426109427109428109429109430109431109432109433109434109435109436109437109438109439109440109441109442109443109444109445109446109447109448109449109450109451109452109453109454109455109456109457109458109459109460109461109462109463109464109465109466109467109468109469109470109471109472109473109474109475109476109477109478109479109480109481109482109483109484109485109486109487109488109489109490109491109492109493109494109495109496109497109498109499109500109501109502109503109504109505109506109507109508109509109510109511109512109513109514109515109516109517109518109519109520109521109522109523109524109525109526109527109528109529109530109531109532109533109534109535109536109537109538109539109540109541109542109543109544109545109546109547109548109549109550109551109552109553109554109555109556109557109558109559109560109561109562109563109564109565109566109567109568109569109570109571109572109573109574109575109576109577109578109579109580109581109582109583109584109585109586109587109588109589109590109591109592109593109594109595109596109597109598109599109600109601109602109603109604109605109606109607109608109609109610109611109612109613109614109615109616109617109618109619109620109621109622109623109624109625109626109627109628109629109630109631109632109633109634109635109636109637109638109639109640109641109642109643109644109645109646109647109648109649109650109651109652109653109654109655109656109657109658109659109660109661109662109663109664109665109666109667109668109669109670109671109672109673109674109675109676109677109678109679109680109681109682109683109684109685109686109687109688109689109690109691109692109693109694109695109696109697109698109699109700109701109702109703109704109705109706109707109708109709109710109711109712109713109714109715109716109717109718109719109720109721109722109723109724109725109726109727109728109729109730109731109732109733109734109735109736109737109738109739109740109741109742109743109744109745109746109747109748109749109750109751109752109753109754109755109756109757109758109759109760109761109762109763109764109765109766109767109768109769109770109771109772109773109774109775109776109777109778109779109780109781109782109783109784109785109786109787109788109789109790109791109792109793109794109795109796109797109798109799109800109801109802109803109804109805109806109807109808109809109810109811109812109813109814109815109816109817109818109819109820109821109822109823109824109825109826109827109828109829109830109831109832109833109834109835109836109837109838109839109840109841109842109843109844109845109846109847109848109849109850109851109852109853109854109855109856109857109858109859109860109861109862109863109864109865109866109867109868109869109870109871109872109873109874109875109876109877109878109879109880109881109882109883109884109885109886109887109888109889109890109891109892109893109894109895109896109897109898109899109900109901109902109903109904109905109906109907109908109909109910109911109912109913109914109915109916109917109918109919109920109921109922109923109924109925109926109927109928109929109930109931109932109933109934109935109936109937109938109939109940109941109942109943109944109945109946109947109948109949109950109951109952109953109954109955109956109957109958109959109960109961109962109963109964109965109966109967109968109969109970109971109972109973109974109975109976109977109978109979109980109981109982109983109984109985109986109987109988109989109990109991109992109993109994109995109996109997109998109999110000110001110002110003110004110005110006110007110008110009110010110011110012110013110014110015110016110017110018110019110020110021110022110023110024110025110026110027110028110029110030110031110032110033110034110035110036110037110038110039110040110041110042110043110044110045110046110047110048110049110050110051110052110053110054110055110056110057110058110059110060110061110062110063110064110065110066110067110068110069110070110071110072110073110074110075110076110077110078110079110080110081110082110083110084110085110086110087110088110089110090110091110092110093110094110095110096110097110098110099110100110101110102110103110104110105110106110107110108110109110110110111110112110113110114110115110116110117110118110119110120110121110122110123110124110125110126110127110128110129110130110131110132110133110134110135110136110137110138110139110140110141110142110143110144110145110146110147110148110149110150110151110152110153110154110155110156110157110158110159110160110161110162110163110164110165110166110167110168110169110170110171110172110173110174110175110176110177110178110179110180110181110182110183110184110185110186110187110188110189110190110191110192110193110194110195110196110197110198110199110200110201110202110203110204110205110206110207110208110209110210110211110212110213110214110215110216110217110218110219110220110221110222110223110224110225110226110227110228110229110230110231110232110233110234110235110236110237110238110239110240110241110242110243110244110245110246110247110248110249110250110251110252110253110254110255110256110257110258110259110260110261110262110263110264110265110266110267110268110269110270110271110272110273110274110275110276110277110278110279110280110281110282110283110284110285110286110287110288110289110290110291110292110293110294110295110296110297110298110299110300110301110302110303110304110305110306110307110308110309110310110311110312110313110314110315110316110317110318110319110320110321110322110323110324110325110326110327110328110329110330110331110332110333110334110335110336110337110338110339110340110341110342110343110344110345110346110347110348110349110350110351110352110353110354110355110356110357110358110359110360110361110362110363110364110365110366110367110368110369110370110371110372110373110374110375110376110377110378110379110380110381110382110383110384110385110386110387110388110389110390110391110392110393110394110395110396110397110398110399110400110401110402110403110404110405110406110407110408110409110410110411110412110413110414110415110416110417110418110419110420110421110422110423110424110425110426110427110428110429110430110431110432110433110434110435110436110437110438110439110440110441110442110443110444110445110446110447110448110449110450110451110452110453110454110455110456110457
  1. diff -Nur linux-3.17.5/arch/arm/boot/dts/bcm2708.dtsi linux-rpi/arch/arm/boot/dts/bcm2708.dtsi
  2. --- linux-3.17.5/arch/arm/boot/dts/bcm2708.dtsi 1969-12-31 18:00:00.000000000 -0600
  3. +++ linux-rpi/arch/arm/boot/dts/bcm2708.dtsi 2014-12-11 14:05:36.712418001 -0600
  4. @@ -0,0 +1,96 @@
  5. +/include/ "skeleton.dtsi"
  6. +
  7. +/ {
  8. + compatible = "brcm,bcm2708";
  9. + model = "BCM2708";
  10. +
  11. + interrupt-parent = <&intc>;
  12. +
  13. + chosen {
  14. + /* No padding required - the boot loader can do that. */
  15. + bootargs = "";
  16. + };
  17. +
  18. + soc {
  19. + compatible = "simple-bus";
  20. + #address-cells = <1>;
  21. + #size-cells = <1>;
  22. + ranges = <0x7e000000 0x20000000 0x02000000>;
  23. +
  24. + intc: interrupt-controller {
  25. + compatible = "brcm,bcm2708-armctrl-ic";
  26. + reg = <0x7e00b200 0x200>;
  27. + interrupt-controller;
  28. + #interrupt-cells = <2>;
  29. + };
  30. +
  31. + gpio: gpio {
  32. + compatible = "brcm,bcm2708-pinctrl";
  33. + reg = <0x7e200000 0xb4>;
  34. + gpio-controller;
  35. + #gpio-cells = <2>;
  36. + };
  37. +
  38. + i2s: i2s@7e203000 {
  39. + compatible = "brcm,bcm2708-i2s";
  40. + reg = <0x7e203000 0x20>,
  41. + <0x7e101098 0x02>;
  42. +
  43. + //dmas = <&dma 2>,
  44. + // <&dma 3>;
  45. + dma-names = "tx", "rx";
  46. + status = "disabled";
  47. + };
  48. +
  49. + spi0: spi@7e204000 {
  50. + compatible = "brcm,bcm2708-spi";
  51. + reg = <0x7e204000 0x1000>;
  52. + interrupts = <2 22>;
  53. + clocks = <&clk_spi>;
  54. + #address-cells = <1>;
  55. + #size-cells = <0>;
  56. + status = "disabled";
  57. + };
  58. +
  59. + i2c0: i2c@7e205000 {
  60. + compatible = "brcm,bcm2708-i2c";
  61. + reg = <0x7e205000 0x1000>;
  62. + interrupts = <2 21>;
  63. + clocks = <&clk_i2c>;
  64. + #address-cells = <1>;
  65. + #size-cells = <0>;
  66. + status = "disabled";
  67. + };
  68. +
  69. + i2c1: i2c@7e804000 {
  70. + compatible = "brcm,bcm2708-i2c";
  71. + reg = <0x7e804000 0x1000>;
  72. + interrupts = <2 21>;
  73. + clocks = <&clk_i2c>;
  74. + #address-cells = <1>;
  75. + #size-cells = <0>;
  76. + status = "disabled";
  77. + };
  78. + };
  79. +
  80. + clocks {
  81. + compatible = "simple-bus";
  82. + #address-cells = <1>;
  83. + #size-cells = <0>;
  84. +
  85. + clk_i2c: i2c {
  86. + compatible = "fixed-clock";
  87. + reg = <1>;
  88. + #clock-cells = <0>;
  89. + clock-frequency = <250000000>;
  90. + };
  91. +
  92. + clk_spi: clock@2 {
  93. + compatible = "fixed-clock";
  94. + reg = <2>;
  95. + #clock-cells = <0>;
  96. + clock-output-names = "spi";
  97. + clock-frequency = <250000000>;
  98. + };
  99. + };
  100. +};
  101. diff -Nur linux-3.17.5/arch/arm/boot/dts/bcm2708-rpi-b.dts linux-rpi/arch/arm/boot/dts/bcm2708-rpi-b.dts
  102. --- linux-3.17.5/arch/arm/boot/dts/bcm2708-rpi-b.dts 1969-12-31 18:00:00.000000000 -0600
  103. +++ linux-rpi/arch/arm/boot/dts/bcm2708-rpi-b.dts 2014-12-11 14:05:36.712418001 -0600
  104. @@ -0,0 +1,81 @@
  105. +/dts-v1/;
  106. +
  107. +/include/ "bcm2708.dtsi"
  108. +
  109. +/ {
  110. + compatible = "brcm,bcm2708";
  111. + model = "Raspberry Pi Model B";
  112. +
  113. + aliases {
  114. + spi0 = &spi0;
  115. + i2c0 = &i2c0;
  116. + i2c1 = &i2c1;
  117. + i2s = &i2s;
  118. + gpio = &gpio;
  119. + sound = &sound;
  120. + };
  121. +
  122. + sound: sound {
  123. + };
  124. +};
  125. +
  126. +&gpio {
  127. + spi0_pins: spi0_pins {
  128. + brcm,pins = <7 8 9 10 11>;
  129. + brcm,function = <4>; /* alt0 */
  130. + };
  131. +
  132. + i2c0_pins: i2c0 {
  133. + brcm,pins = <0 1>;
  134. + brcm,function = <4>;
  135. + };
  136. +
  137. + i2c1_pins: i2c1 {
  138. + brcm,pins = <2 3>;
  139. + brcm,function = <4>;
  140. + };
  141. +
  142. + i2s_pins: i2s {
  143. + brcm,pins = <28 29 30 31>;
  144. + brcm,function = <4>; /* alt0 */
  145. + };
  146. +};
  147. +
  148. +&spi0 {
  149. + pinctrl-names = "default";
  150. + pinctrl-0 = <&spi0_pins>;
  151. +
  152. + spidev@0{
  153. + compatible = "spidev";
  154. + reg = <0>; /* CE0 */
  155. + #address-cells = <1>;
  156. + #size-cells = <0>;
  157. + spi-max-frequency = <500000>;
  158. + };
  159. +
  160. + spidev@1{
  161. + compatible = "spidev";
  162. + reg = <1>; /* CE1 */
  163. + #address-cells = <1>;
  164. + #size-cells = <0>;
  165. + spi-max-frequency = <500000>;
  166. + };
  167. +};
  168. +
  169. +&i2c0 {
  170. + pinctrl-names = "default";
  171. + pinctrl-0 = <&i2c0_pins>;
  172. + clock-frequency = <100000>;
  173. +};
  174. +
  175. +&i2c1 {
  176. + pinctrl-names = "default";
  177. + pinctrl-0 = <&i2c1_pins>;
  178. + clock-frequency = <100000>;
  179. +};
  180. +
  181. +&i2s {
  182. + #sound-dai-cells = <0>;
  183. + pinctrl-names = "default";
  184. + pinctrl-0 = <&i2s_pins>;
  185. +};
  186. diff -Nur linux-3.17.5/arch/arm/boot/dts/bcm2708-rpi-b-plus.dts linux-rpi/arch/arm/boot/dts/bcm2708-rpi-b-plus.dts
  187. --- linux-3.17.5/arch/arm/boot/dts/bcm2708-rpi-b-plus.dts 1969-12-31 18:00:00.000000000 -0600
  188. +++ linux-rpi/arch/arm/boot/dts/bcm2708-rpi-b-plus.dts 2014-12-11 14:05:36.712418001 -0600
  189. @@ -0,0 +1,81 @@
  190. +/dts-v1/;
  191. +
  192. +/include/ "bcm2708.dtsi"
  193. +
  194. +/ {
  195. + compatible = "brcm,bcm2708";
  196. + model = "Raspberry Pi Model B+";
  197. +
  198. + aliases {
  199. + spi0 = &spi0;
  200. + i2c0 = &i2c0;
  201. + i2c1 = &i2c1;
  202. + i2s = &i2s;
  203. + gpio = &gpio;
  204. + sound = &sound;
  205. + };
  206. +
  207. + sound: sound {
  208. + };
  209. +};
  210. +
  211. +&gpio {
  212. + spi0_pins: spi0_pins {
  213. + brcm,pins = <7 8 9 10 11>;
  214. + brcm,function = <4>; /* alt0 */
  215. + };
  216. +
  217. + i2c0_pins: i2c0 {
  218. + brcm,pins = <0 1>;
  219. + brcm,function = <4>;
  220. + };
  221. +
  222. + i2c1_pins: i2c1 {
  223. + brcm,pins = <2 3>;
  224. + brcm,function = <4>;
  225. + };
  226. +
  227. + i2s_pins: i2s {
  228. + brcm,pins = <18 19 20 21>;
  229. + brcm,function = <4>; /* alt0 */
  230. + };
  231. +};
  232. +
  233. +&spi0 {
  234. + pinctrl-names = "default";
  235. + pinctrl-0 = <&spi0_pins>;
  236. +
  237. + spidev@0{
  238. + compatible = "spidev";
  239. + reg = <0>; /* CE0 */
  240. + #address-cells = <1>;
  241. + #size-cells = <0>;
  242. + spi-max-frequency = <500000>;
  243. + };
  244. +
  245. + spidev@1{
  246. + compatible = "spidev";
  247. + reg = <1>; /* CE1 */
  248. + #address-cells = <1>;
  249. + #size-cells = <0>;
  250. + spi-max-frequency = <500000>;
  251. + };
  252. +};
  253. +
  254. +&i2c0 {
  255. + pinctrl-names = "default";
  256. + pinctrl-0 = <&i2c0_pins>;
  257. + clock-frequency = <100000>;
  258. +};
  259. +
  260. +&i2c1 {
  261. + pinctrl-names = "default";
  262. + pinctrl-0 = <&i2c1_pins>;
  263. + clock-frequency = <100000>;
  264. +};
  265. +
  266. +&i2s {
  267. + #sound-dai-cells = <0>;
  268. + pinctrl-names = "default";
  269. + pinctrl-0 = <&i2s_pins>;
  270. +};
  271. diff -Nur linux-3.17.5/arch/arm/boot/dts/hifiberry-dac-overlay.dts linux-rpi/arch/arm/boot/dts/hifiberry-dac-overlay.dts
  272. --- linux-3.17.5/arch/arm/boot/dts/hifiberry-dac-overlay.dts 1969-12-31 18:00:00.000000000 -0600
  273. +++ linux-rpi/arch/arm/boot/dts/hifiberry-dac-overlay.dts 2014-12-11 14:05:36.720418001 -0600
  274. @@ -0,0 +1,34 @@
  275. +// Definitions for HiFiBerry DAC
  276. +/dts-v1/;
  277. +/plugin/;
  278. +
  279. +/ {
  280. + compatible = "brcm,bcm2708";
  281. +
  282. + fragment@0 {
  283. + target = <&sound>;
  284. + __overlay__ {
  285. + compatible = "hifiberry,hifiberry-dac";
  286. + i2s-controller = <&i2s>;
  287. + status = "okay";
  288. + };
  289. + };
  290. +
  291. + fragment@1 {
  292. + target = <&i2s>;
  293. + __overlay__ {
  294. + status = "okay";
  295. + };
  296. + };
  297. +
  298. + fragment@2 {
  299. + target-path = "/";
  300. + __overlay__ {
  301. + pcm5102a-codec {
  302. + #sound-dai-cells = <0>;
  303. + compatible = "ti,pcm5102a";
  304. + status = "okay";
  305. + };
  306. + };
  307. + };
  308. +};
  309. diff -Nur linux-3.17.5/arch/arm/boot/dts/hifiberry-dacplus-overlay.dts linux-rpi/arch/arm/boot/dts/hifiberry-dacplus-overlay.dts
  310. --- linux-3.17.5/arch/arm/boot/dts/hifiberry-dacplus-overlay.dts 1969-12-31 18:00:00.000000000 -0600
  311. +++ linux-rpi/arch/arm/boot/dts/hifiberry-dacplus-overlay.dts 2014-12-11 14:05:36.720418001 -0600
  312. @@ -0,0 +1,39 @@
  313. +// Definitions for HiFiBerry DAC+
  314. +/dts-v1/;
  315. +/plugin/;
  316. +
  317. +/ {
  318. + compatible = "brcm,bcm2708";
  319. +
  320. + fragment@0 {
  321. + target = <&sound>;
  322. + __overlay__ {
  323. + compatible = "hifiberry,hifiberry-dacplus";
  324. + i2s-controller = <&i2s>;
  325. + status = "okay";
  326. + };
  327. + };
  328. +
  329. + fragment@1 {
  330. + target = <&i2s>;
  331. + __overlay__ {
  332. + status = "okay";
  333. + };
  334. + };
  335. +
  336. + fragment@2 {
  337. + target = <&i2c1>;
  338. + __overlay__ {
  339. + #address-cells = <1>;
  340. + #size-cells = <0>;
  341. + status = "okay";
  342. +
  343. + pcm5122@4d {
  344. + #sound-dai-cells = <0>;
  345. + compatible = "ti,pcm5122";
  346. + reg = <0x4d>;
  347. + status = "okay";
  348. + };
  349. + };
  350. + };
  351. +};
  352. diff -Nur linux-3.17.5/arch/arm/boot/dts/hifiberry-digi-overlay.dts linux-rpi/arch/arm/boot/dts/hifiberry-digi-overlay.dts
  353. --- linux-3.17.5/arch/arm/boot/dts/hifiberry-digi-overlay.dts 1969-12-31 18:00:00.000000000 -0600
  354. +++ linux-rpi/arch/arm/boot/dts/hifiberry-digi-overlay.dts 2014-12-11 14:05:36.720418001 -0600
  355. @@ -0,0 +1,39 @@
  356. +// Definitions for HiFiBerry Digi
  357. +/dts-v1/;
  358. +/plugin/;
  359. +
  360. +/ {
  361. + compatible = "brcm,bcm2708";
  362. +
  363. + fragment@0 {
  364. + target = <&sound>;
  365. + __overlay__ {
  366. + compatible = "hifiberry,hifiberry-digi";
  367. + i2s-controller = <&i2s>;
  368. + status = "okay";
  369. + };
  370. + };
  371. +
  372. + fragment@1 {
  373. + target = <&i2s>;
  374. + __overlay__ {
  375. + status = "okay";
  376. + };
  377. + };
  378. +
  379. + fragment@2 {
  380. + target = <&i2c1>;
  381. + __overlay__ {
  382. + #address-cells = <1>;
  383. + #size-cells = <0>;
  384. + status = "okay";
  385. +
  386. + wm8804@3b {
  387. + #sound-dai-cells = <0>;
  388. + compatible = "wlf,wm8804";
  389. + reg = <0x3b>;
  390. + status = "okay";
  391. + };
  392. + };
  393. + };
  394. +};
  395. diff -Nur linux-3.17.5/arch/arm/boot/dts/iqaudio-dac-overlay.dts linux-rpi/arch/arm/boot/dts/iqaudio-dac-overlay.dts
  396. --- linux-3.17.5/arch/arm/boot/dts/iqaudio-dac-overlay.dts 1969-12-31 18:00:00.000000000 -0600
  397. +++ linux-rpi/arch/arm/boot/dts/iqaudio-dac-overlay.dts 2014-12-11 14:05:36.744418001 -0600
  398. @@ -0,0 +1,39 @@
  399. +// Definitions for IQaudIO DAC
  400. +/dts-v1/;
  401. +/plugin/;
  402. +
  403. +/ {
  404. + compatible = "brcm,bcm2708";
  405. +
  406. + fragment@0 {
  407. + target = <&sound>;
  408. + __overlay__ {
  409. + compatible = "iqaudio,iqaudio-dac";
  410. + i2s-controller = <&i2s>;
  411. + status = "okay";
  412. + };
  413. + };
  414. +
  415. + fragment@1 {
  416. + target = <&i2s>;
  417. + __overlay__ {
  418. + status = "okay";
  419. + };
  420. + };
  421. +
  422. + fragment@2 {
  423. + target = <&i2c1>;
  424. + __overlay__ {
  425. + #address-cells = <1>;
  426. + #size-cells = <0>;
  427. + status = "okay";
  428. +
  429. + pcm5122@4c {
  430. + #sound-dai-cells = <0>;
  431. + compatible = "ti,pcm5122";
  432. + reg = <0x4c>;
  433. + status = "okay";
  434. + };
  435. + };
  436. + };
  437. +};
  438. diff -Nur linux-3.17.5/arch/arm/boot/dts/iqaudio-dacplus-overlay.dts linux-rpi/arch/arm/boot/dts/iqaudio-dacplus-overlay.dts
  439. --- linux-3.17.5/arch/arm/boot/dts/iqaudio-dacplus-overlay.dts 1969-12-31 18:00:00.000000000 -0600
  440. +++ linux-rpi/arch/arm/boot/dts/iqaudio-dacplus-overlay.dts 2014-12-11 14:05:36.744418001 -0600
  441. @@ -0,0 +1,39 @@
  442. +// Definitions for IQaudIO DAC+
  443. +/dts-v1/;
  444. +/plugin/;
  445. +
  446. +/ {
  447. + compatible = "brcm,bcm2708";
  448. +
  449. + fragment@0 {
  450. + target = <&sound>;
  451. + __overlay__ {
  452. + compatible = "iqaudio,iqaudio-dac";
  453. + i2s-controller = <&i2s>;
  454. + status = "okay";
  455. + };
  456. + };
  457. +
  458. + fragment@1 {
  459. + target = <&i2s>;
  460. + __overlay__ {
  461. + status = "okay";
  462. + };
  463. + };
  464. +
  465. + fragment@2 {
  466. + target = <&i2c1>;
  467. + __overlay__ {
  468. + #address-cells = <1>;
  469. + #size-cells = <0>;
  470. + status = "okay";
  471. +
  472. + pcm5122@4c {
  473. + #sound-dai-cells = <0>;
  474. + compatible = "ti,pcm5122";
  475. + reg = <0x4c>;
  476. + status = "okay";
  477. + };
  478. + };
  479. + };
  480. +};
  481. diff -Nur linux-3.17.5/arch/arm/boot/dts/Makefile linux-rpi/arch/arm/boot/dts/Makefile
  482. --- linux-3.17.5/arch/arm/boot/dts/Makefile 2014-12-06 17:57:59.000000000 -0600
  483. +++ linux-rpi/arch/arm/boot/dts/Makefile 2014-12-11 14:05:36.704418001 -0600
  484. @@ -51,6 +51,8 @@
  485. dtb-$(CONFIG_ARCH_ATLAS6) += atlas6-evb.dtb
  486. dtb-$(CONFIG_ARCH_AXXIA) += axm5516-amarillo.dtb
  487. +dtb-$(CONFIG_BCM2708_DT) += bcm2708-rpi-b.dtb
  488. +dtb-$(CONFIG_BCM2708_DT) += bcm2708-rpi-b-plus.dtb
  489. dtb-$(CONFIG_ARCH_BCM2835) += bcm2835-rpi-b.dtb
  490. dtb-$(CONFIG_ARCH_BCM_5301X) += bcm4708-netgear-r6250.dtb
  491. dtb-$(CONFIG_ARCH_BCM_MOBILE) += bcm28155-ap.dtb \
  492. @@ -494,6 +496,7 @@
  493. targets += dtbs dtbs_install
  494. targets += $(dtb-y)
  495. +
  496. endif
  497. # *.dtb used to be generated in the directory above. Clean out the
  498. diff -Nur linux-3.17.5/arch/arm/configs/bcmrpi_defconfig linux-rpi/arch/arm/configs/bcmrpi_defconfig
  499. --- linux-3.17.5/arch/arm/configs/bcmrpi_defconfig 1969-12-31 18:00:00.000000000 -0600
  500. +++ linux-rpi/arch/arm/configs/bcmrpi_defconfig 2014-12-11 14:05:36.792418001 -0600
  501. @@ -0,0 +1,1117 @@
  502. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  503. +CONFIG_PHYS_OFFSET=0
  504. +# CONFIG_LOCALVERSION_AUTO is not set
  505. +CONFIG_SYSVIPC=y
  506. +CONFIG_POSIX_MQUEUE=y
  507. +CONFIG_FHANDLE=y
  508. +CONFIG_AUDIT=y
  509. +CONFIG_NO_HZ=y
  510. +CONFIG_HIGH_RES_TIMERS=y
  511. +CONFIG_BSD_PROCESS_ACCT=y
  512. +CONFIG_BSD_PROCESS_ACCT_V3=y
  513. +CONFIG_TASKSTATS=y
  514. +CONFIG_TASK_DELAY_ACCT=y
  515. +CONFIG_TASK_XACCT=y
  516. +CONFIG_TASK_IO_ACCOUNTING=y
  517. +CONFIG_IKCONFIG=y
  518. +CONFIG_IKCONFIG_PROC=y
  519. +CONFIG_CGROUP_FREEZER=y
  520. +CONFIG_CGROUP_DEVICE=y
  521. +CONFIG_CGROUP_CPUACCT=y
  522. +CONFIG_RESOURCE_COUNTERS=y
  523. +CONFIG_MEMCG=y
  524. +CONFIG_BLK_CGROUP=y
  525. +CONFIG_NAMESPACES=y
  526. +CONFIG_SCHED_AUTOGROUP=y
  527. +CONFIG_BLK_DEV_INITRD=y
  528. +CONFIG_EMBEDDED=y
  529. +# CONFIG_COMPAT_BRK is not set
  530. +CONFIG_PROFILING=y
  531. +CONFIG_OPROFILE=m
  532. +CONFIG_KPROBES=y
  533. +CONFIG_JUMP_LABEL=y
  534. +CONFIG_MODULES=y
  535. +CONFIG_MODULE_UNLOAD=y
  536. +CONFIG_MODVERSIONS=y
  537. +CONFIG_MODULE_SRCVERSION_ALL=y
  538. +CONFIG_BLK_DEV_THROTTLING=y
  539. +CONFIG_PARTITION_ADVANCED=y
  540. +CONFIG_MAC_PARTITION=y
  541. +CONFIG_CFQ_GROUP_IOSCHED=y
  542. +CONFIG_ARCH_BCM2708=y
  543. +CONFIG_BCM2708_DT=y
  544. +CONFIG_PREEMPT=y
  545. +CONFIG_AEABI=y
  546. +CONFIG_CLEANCACHE=y
  547. +CONFIG_FRONTSWAP=y
  548. +CONFIG_CMA=y
  549. +CONFIG_UACCESS_WITH_MEMCPY=y
  550. +CONFIG_SECCOMP=y
  551. +CONFIG_ZBOOT_ROM_TEXT=0x0
  552. +CONFIG_ZBOOT_ROM_BSS=0x0
  553. +CONFIG_CMDLINE="console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  554. +CONFIG_KEXEC=y
  555. +CONFIG_CPU_FREQ=y
  556. +CONFIG_CPU_FREQ_STAT=m
  557. +CONFIG_CPU_FREQ_STAT_DETAILS=y
  558. +CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND=y
  559. +CONFIG_CPU_FREQ_GOV_POWERSAVE=y
  560. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  561. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  562. +CONFIG_CPU_IDLE=y
  563. +CONFIG_VFP=y
  564. +CONFIG_BINFMT_MISC=m
  565. +CONFIG_NET=y
  566. +CONFIG_PACKET=y
  567. +CONFIG_UNIX=y
  568. +CONFIG_XFRM_USER=y
  569. +CONFIG_NET_KEY=m
  570. +CONFIG_INET=y
  571. +CONFIG_IP_MULTICAST=y
  572. +CONFIG_IP_ADVANCED_ROUTER=y
  573. +CONFIG_IP_MULTIPLE_TABLES=y
  574. +CONFIG_IP_ROUTE_MULTIPATH=y
  575. +CONFIG_IP_ROUTE_VERBOSE=y
  576. +CONFIG_IP_PNP=y
  577. +CONFIG_IP_PNP_DHCP=y
  578. +CONFIG_IP_PNP_RARP=y
  579. +CONFIG_NET_IPIP=m
  580. +CONFIG_NET_IPGRE_DEMUX=m
  581. +CONFIG_NET_IPGRE=m
  582. +CONFIG_IP_MROUTE=y
  583. +CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
  584. +CONFIG_IP_PIMSM_V1=y
  585. +CONFIG_IP_PIMSM_V2=y
  586. +CONFIG_SYN_COOKIES=y
  587. +CONFIG_INET_AH=m
  588. +CONFIG_INET_ESP=m
  589. +CONFIG_INET_IPCOMP=m
  590. +CONFIG_INET_XFRM_MODE_TRANSPORT=m
  591. +CONFIG_INET_XFRM_MODE_TUNNEL=m
  592. +CONFIG_INET_XFRM_MODE_BEET=m
  593. +CONFIG_INET_LRO=m
  594. +CONFIG_INET_DIAG=m
  595. +CONFIG_INET6_AH=m
  596. +CONFIG_INET6_ESP=m
  597. +CONFIG_INET6_IPCOMP=m
  598. +CONFIG_IPV6_TUNNEL=m
  599. +CONFIG_IPV6_MULTIPLE_TABLES=y
  600. +CONFIG_IPV6_MROUTE=y
  601. +CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
  602. +CONFIG_IPV6_PIMSM_V2=y
  603. +CONFIG_NETFILTER=y
  604. +CONFIG_NF_CONNTRACK=m
  605. +CONFIG_NF_CONNTRACK_ZONES=y
  606. +CONFIG_NF_CONNTRACK_EVENTS=y
  607. +CONFIG_NF_CONNTRACK_TIMESTAMP=y
  608. +CONFIG_NF_CT_PROTO_DCCP=m
  609. +CONFIG_NF_CT_PROTO_UDPLITE=m
  610. +CONFIG_NF_CONNTRACK_AMANDA=m
  611. +CONFIG_NF_CONNTRACK_FTP=m
  612. +CONFIG_NF_CONNTRACK_H323=m
  613. +CONFIG_NF_CONNTRACK_IRC=m
  614. +CONFIG_NF_CONNTRACK_NETBIOS_NS=m
  615. +CONFIG_NF_CONNTRACK_SNMP=m
  616. +CONFIG_NF_CONNTRACK_PPTP=m
  617. +CONFIG_NF_CONNTRACK_SANE=m
  618. +CONFIG_NF_CONNTRACK_SIP=m
  619. +CONFIG_NF_CONNTRACK_TFTP=m
  620. +CONFIG_NF_CT_NETLINK=m
  621. +CONFIG_NETFILTER_XT_SET=m
  622. +CONFIG_NETFILTER_XT_TARGET_AUDIT=m
  623. +CONFIG_NETFILTER_XT_TARGET_CHECKSUM=m
  624. +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
  625. +CONFIG_NETFILTER_XT_TARGET_CONNMARK=m
  626. +CONFIG_NETFILTER_XT_TARGET_DSCP=m
  627. +CONFIG_NETFILTER_XT_TARGET_HMARK=m
  628. +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=m
  629. +CONFIG_NETFILTER_XT_TARGET_LED=m
  630. +CONFIG_NETFILTER_XT_TARGET_LOG=m
  631. +CONFIG_NETFILTER_XT_TARGET_MARK=m
  632. +CONFIG_NETFILTER_XT_TARGET_NFLOG=m
  633. +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
  634. +CONFIG_NETFILTER_XT_TARGET_NOTRACK=m
  635. +CONFIG_NETFILTER_XT_TARGET_TEE=m
  636. +CONFIG_NETFILTER_XT_TARGET_TPROXY=m
  637. +CONFIG_NETFILTER_XT_TARGET_TRACE=m
  638. +CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
  639. +CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=m
  640. +CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=m
  641. +CONFIG_NETFILTER_XT_MATCH_BPF=m
  642. +CONFIG_NETFILTER_XT_MATCH_CLUSTER=m
  643. +CONFIG_NETFILTER_XT_MATCH_COMMENT=m
  644. +CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
  645. +CONFIG_NETFILTER_XT_MATCH_CONNLABEL=m
  646. +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=m
  647. +CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
  648. +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
  649. +CONFIG_NETFILTER_XT_MATCH_CPU=m
  650. +CONFIG_NETFILTER_XT_MATCH_DCCP=m
  651. +CONFIG_NETFILTER_XT_MATCH_DEVGROUP=m
  652. +CONFIG_NETFILTER_XT_MATCH_DSCP=m
  653. +CONFIG_NETFILTER_XT_MATCH_ESP=m
  654. +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
  655. +CONFIG_NETFILTER_XT_MATCH_HELPER=m
  656. +CONFIG_NETFILTER_XT_MATCH_IPRANGE=m
  657. +CONFIG_NETFILTER_XT_MATCH_IPVS=m
  658. +CONFIG_NETFILTER_XT_MATCH_LENGTH=m
  659. +CONFIG_NETFILTER_XT_MATCH_LIMIT=m
  660. +CONFIG_NETFILTER_XT_MATCH_MAC=m
  661. +CONFIG_NETFILTER_XT_MATCH_MARK=m
  662. +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
  663. +CONFIG_NETFILTER_XT_MATCH_NFACCT=m
  664. +CONFIG_NETFILTER_XT_MATCH_OSF=m
  665. +CONFIG_NETFILTER_XT_MATCH_OWNER=m
  666. +CONFIG_NETFILTER_XT_MATCH_POLICY=m
  667. +CONFIG_NETFILTER_XT_MATCH_PHYSDEV=m
  668. +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
  669. +CONFIG_NETFILTER_XT_MATCH_QUOTA=m
  670. +CONFIG_NETFILTER_XT_MATCH_RATEEST=m
  671. +CONFIG_NETFILTER_XT_MATCH_REALM=m
  672. +CONFIG_NETFILTER_XT_MATCH_RECENT=m
  673. +CONFIG_NETFILTER_XT_MATCH_SOCKET=m
  674. +CONFIG_NETFILTER_XT_MATCH_STATE=m
  675. +CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
  676. +CONFIG_NETFILTER_XT_MATCH_STRING=m
  677. +CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
  678. +CONFIG_NETFILTER_XT_MATCH_TIME=m
  679. +CONFIG_NETFILTER_XT_MATCH_U32=m
  680. +CONFIG_IP_SET=m
  681. +CONFIG_IP_SET_BITMAP_IP=m
  682. +CONFIG_IP_SET_BITMAP_IPMAC=m
  683. +CONFIG_IP_SET_BITMAP_PORT=m
  684. +CONFIG_IP_SET_HASH_IP=m
  685. +CONFIG_IP_SET_HASH_IPPORT=m
  686. +CONFIG_IP_SET_HASH_IPPORTIP=m
  687. +CONFIG_IP_SET_HASH_IPPORTNET=m
  688. +CONFIG_IP_SET_HASH_NET=m
  689. +CONFIG_IP_SET_HASH_NETPORT=m
  690. +CONFIG_IP_SET_HASH_NETIFACE=m
  691. +CONFIG_IP_SET_LIST_SET=m
  692. +CONFIG_IP_VS=m
  693. +CONFIG_IP_VS_PROTO_TCP=y
  694. +CONFIG_IP_VS_PROTO_UDP=y
  695. +CONFIG_IP_VS_PROTO_ESP=y
  696. +CONFIG_IP_VS_PROTO_AH=y
  697. +CONFIG_IP_VS_PROTO_SCTP=y
  698. +CONFIG_IP_VS_RR=m
  699. +CONFIG_IP_VS_WRR=m
  700. +CONFIG_IP_VS_LC=m
  701. +CONFIG_IP_VS_WLC=m
  702. +CONFIG_IP_VS_LBLC=m
  703. +CONFIG_IP_VS_LBLCR=m
  704. +CONFIG_IP_VS_DH=m
  705. +CONFIG_IP_VS_SH=m
  706. +CONFIG_IP_VS_SED=m
  707. +CONFIG_IP_VS_NQ=m
  708. +CONFIG_IP_VS_FTP=m
  709. +CONFIG_IP_VS_PE_SIP=m
  710. +CONFIG_NF_CONNTRACK_IPV4=m
  711. +CONFIG_NF_NAT_IPV4=m
  712. +CONFIG_IP_NF_IPTABLES=m
  713. +CONFIG_IP_NF_MATCH_AH=m
  714. +CONFIG_IP_NF_MATCH_ECN=m
  715. +CONFIG_IP_NF_MATCH_TTL=m
  716. +CONFIG_IP_NF_FILTER=m
  717. +CONFIG_IP_NF_TARGET_REJECT=m
  718. +CONFIG_IP_NF_MANGLE=m
  719. +CONFIG_IP_NF_TARGET_ECN=m
  720. +CONFIG_IP_NF_TARGET_TTL=m
  721. +CONFIG_IP_NF_RAW=m
  722. +CONFIG_IP_NF_ARPTABLES=m
  723. +CONFIG_IP_NF_ARPFILTER=m
  724. +CONFIG_IP_NF_ARP_MANGLE=m
  725. +CONFIG_NF_CONNTRACK_IPV6=m
  726. +CONFIG_NF_NAT_IPV6=m
  727. +CONFIG_IP6_NF_IPTABLES=m
  728. +CONFIG_IP6_NF_MATCH_AH=m
  729. +CONFIG_IP6_NF_MATCH_EUI64=m
  730. +CONFIG_IP6_NF_MATCH_FRAG=m
  731. +CONFIG_IP6_NF_MATCH_OPTS=m
  732. +CONFIG_IP6_NF_MATCH_HL=m
  733. +CONFIG_IP6_NF_MATCH_IPV6HEADER=m
  734. +CONFIG_IP6_NF_MATCH_MH=m
  735. +CONFIG_IP6_NF_MATCH_RT=m
  736. +CONFIG_IP6_NF_TARGET_HL=m
  737. +CONFIG_IP6_NF_FILTER=m
  738. +CONFIG_IP6_NF_TARGET_REJECT=m
  739. +CONFIG_IP6_NF_MANGLE=m
  740. +CONFIG_IP6_NF_RAW=m
  741. +CONFIG_BRIDGE_NF_EBTABLES=m
  742. +CONFIG_BRIDGE_EBT_BROUTE=m
  743. +CONFIG_BRIDGE_EBT_T_FILTER=m
  744. +CONFIG_BRIDGE_EBT_T_NAT=m
  745. +CONFIG_BRIDGE_EBT_802_3=m
  746. +CONFIG_BRIDGE_EBT_AMONG=m
  747. +CONFIG_BRIDGE_EBT_ARP=m
  748. +CONFIG_BRIDGE_EBT_IP=m
  749. +CONFIG_BRIDGE_EBT_IP6=m
  750. +CONFIG_BRIDGE_EBT_LIMIT=m
  751. +CONFIG_BRIDGE_EBT_MARK=m
  752. +CONFIG_BRIDGE_EBT_PKTTYPE=m
  753. +CONFIG_BRIDGE_EBT_STP=m
  754. +CONFIG_BRIDGE_EBT_VLAN=m
  755. +CONFIG_BRIDGE_EBT_ARPREPLY=m
  756. +CONFIG_BRIDGE_EBT_DNAT=m
  757. +CONFIG_BRIDGE_EBT_MARK_T=m
  758. +CONFIG_BRIDGE_EBT_REDIRECT=m
  759. +CONFIG_BRIDGE_EBT_SNAT=m
  760. +CONFIG_BRIDGE_EBT_LOG=m
  761. +CONFIG_BRIDGE_EBT_NFLOG=m
  762. +CONFIG_SCTP_COOKIE_HMAC_SHA1=y
  763. +CONFIG_ATM=m
  764. +CONFIG_L2TP=m
  765. +CONFIG_L2TP_V3=y
  766. +CONFIG_L2TP_IP=m
  767. +CONFIG_L2TP_ETH=m
  768. +CONFIG_BRIDGE=m
  769. +CONFIG_VLAN_8021Q=m
  770. +CONFIG_VLAN_8021Q_GVRP=y
  771. +CONFIG_ATALK=m
  772. +CONFIG_NET_SCHED=y
  773. +CONFIG_NET_SCH_CBQ=m
  774. +CONFIG_NET_SCH_HTB=m
  775. +CONFIG_NET_SCH_HFSC=m
  776. +CONFIG_NET_SCH_PRIO=m
  777. +CONFIG_NET_SCH_MULTIQ=m
  778. +CONFIG_NET_SCH_RED=m
  779. +CONFIG_NET_SCH_SFB=m
  780. +CONFIG_NET_SCH_SFQ=m
  781. +CONFIG_NET_SCH_TEQL=m
  782. +CONFIG_NET_SCH_TBF=m
  783. +CONFIG_NET_SCH_GRED=m
  784. +CONFIG_NET_SCH_DSMARK=m
  785. +CONFIG_NET_SCH_NETEM=m
  786. +CONFIG_NET_SCH_DRR=m
  787. +CONFIG_NET_SCH_MQPRIO=m
  788. +CONFIG_NET_SCH_CHOKE=m
  789. +CONFIG_NET_SCH_QFQ=m
  790. +CONFIG_NET_SCH_CODEL=m
  791. +CONFIG_NET_SCH_FQ_CODEL=m
  792. +CONFIG_NET_SCH_INGRESS=m
  793. +CONFIG_NET_SCH_PLUG=m
  794. +CONFIG_NET_CLS_BASIC=m
  795. +CONFIG_NET_CLS_TCINDEX=m
  796. +CONFIG_NET_CLS_ROUTE4=m
  797. +CONFIG_NET_CLS_FW=m
  798. +CONFIG_NET_CLS_U32=m
  799. +CONFIG_CLS_U32_MARK=y
  800. +CONFIG_NET_CLS_RSVP=m
  801. +CONFIG_NET_CLS_RSVP6=m
  802. +CONFIG_NET_CLS_FLOW=m
  803. +CONFIG_NET_CLS_CGROUP=m
  804. +CONFIG_NET_EMATCH=y
  805. +CONFIG_NET_EMATCH_CMP=m
  806. +CONFIG_NET_EMATCH_NBYTE=m
  807. +CONFIG_NET_EMATCH_U32=m
  808. +CONFIG_NET_EMATCH_META=m
  809. +CONFIG_NET_EMATCH_TEXT=m
  810. +CONFIG_NET_EMATCH_IPSET=m
  811. +CONFIG_NET_CLS_ACT=y
  812. +CONFIG_NET_ACT_POLICE=m
  813. +CONFIG_NET_ACT_GACT=m
  814. +CONFIG_GACT_PROB=y
  815. +CONFIG_NET_ACT_MIRRED=m
  816. +CONFIG_NET_ACT_IPT=m
  817. +CONFIG_NET_ACT_NAT=m
  818. +CONFIG_NET_ACT_PEDIT=m
  819. +CONFIG_NET_ACT_SIMP=m
  820. +CONFIG_NET_ACT_SKBEDIT=m
  821. +CONFIG_NET_ACT_CSUM=m
  822. +CONFIG_BATMAN_ADV=m
  823. +CONFIG_OPENVSWITCH=m
  824. +CONFIG_NET_PKTGEN=m
  825. +CONFIG_HAMRADIO=y
  826. +CONFIG_AX25=m
  827. +CONFIG_NETROM=m
  828. +CONFIG_ROSE=m
  829. +CONFIG_MKISS=m
  830. +CONFIG_6PACK=m
  831. +CONFIG_BPQETHER=m
  832. +CONFIG_BAYCOM_SER_FDX=m
  833. +CONFIG_BAYCOM_SER_HDX=m
  834. +CONFIG_YAM=m
  835. +CONFIG_IRDA=m
  836. +CONFIG_IRLAN=m
  837. +CONFIG_IRNET=m
  838. +CONFIG_IRCOMM=m
  839. +CONFIG_IRDA_ULTRA=y
  840. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  841. +CONFIG_IRDA_FAST_RR=y
  842. +CONFIG_IRTTY_SIR=m
  843. +CONFIG_KINGSUN_DONGLE=m
  844. +CONFIG_KSDAZZLE_DONGLE=m
  845. +CONFIG_KS959_DONGLE=m
  846. +CONFIG_USB_IRDA=m
  847. +CONFIG_SIGMATEL_FIR=m
  848. +CONFIG_MCS_FIR=m
  849. +CONFIG_BT=m
  850. +CONFIG_BT_RFCOMM=m
  851. +CONFIG_BT_RFCOMM_TTY=y
  852. +CONFIG_BT_BNEP=m
  853. +CONFIG_BT_BNEP_MC_FILTER=y
  854. +CONFIG_BT_BNEP_PROTO_FILTER=y
  855. +CONFIG_BT_HIDP=m
  856. +CONFIG_BT_HCIBTUSB=m
  857. +CONFIG_BT_HCIBCM203X=m
  858. +CONFIG_BT_HCIBPA10X=m
  859. +CONFIG_BT_HCIBFUSB=m
  860. +CONFIG_BT_HCIVHCI=m
  861. +CONFIG_BT_MRVL=m
  862. +CONFIG_BT_MRVL_SDIO=m
  863. +CONFIG_BT_ATH3K=m
  864. +CONFIG_BT_WILINK=m
  865. +CONFIG_CFG80211=m
  866. +CONFIG_CFG80211_WEXT=y
  867. +CONFIG_MAC80211=m
  868. +CONFIG_MAC80211_MESH=y
  869. +CONFIG_WIMAX=m
  870. +CONFIG_RFKILL=m
  871. +CONFIG_RFKILL_INPUT=y
  872. +CONFIG_NET_9P=m
  873. +CONFIG_NFC=m
  874. +CONFIG_NFC_PN533=m
  875. +CONFIG_DEVTMPFS=y
  876. +CONFIG_DEVTMPFS_MOUNT=y
  877. +CONFIG_DMA_CMA=y
  878. +CONFIG_CMA_SIZE_MBYTES=5
  879. +CONFIG_BLK_DEV_LOOP=y
  880. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  881. +CONFIG_BLK_DEV_DRBD=m
  882. +CONFIG_BLK_DEV_NBD=m
  883. +CONFIG_BLK_DEV_RAM=y
  884. +CONFIG_CDROM_PKTCDVD=m
  885. +CONFIG_EEPROM_AT24=m
  886. +CONFIG_SCSI=y
  887. +# CONFIG_SCSI_PROC_FS is not set
  888. +CONFIG_BLK_DEV_SD=y
  889. +CONFIG_CHR_DEV_ST=m
  890. +CONFIG_CHR_DEV_OSST=m
  891. +CONFIG_BLK_DEV_SR=m
  892. +CONFIG_CHR_DEV_SG=m
  893. +CONFIG_SCSI_ISCSI_ATTRS=y
  894. +CONFIG_ISCSI_TCP=m
  895. +CONFIG_ISCSI_BOOT_SYSFS=m
  896. +CONFIG_MD=y
  897. +CONFIG_MD_LINEAR=m
  898. +CONFIG_MD_RAID0=m
  899. +CONFIG_BLK_DEV_DM=m
  900. +CONFIG_DM_CRYPT=m
  901. +CONFIG_DM_SNAPSHOT=m
  902. +CONFIG_DM_MIRROR=m
  903. +CONFIG_DM_LOG_USERSPACE=m
  904. +CONFIG_DM_RAID=m
  905. +CONFIG_DM_ZERO=m
  906. +CONFIG_DM_DELAY=m
  907. +CONFIG_NETDEVICES=y
  908. +CONFIG_BONDING=m
  909. +CONFIG_DUMMY=m
  910. +CONFIG_IFB=m
  911. +CONFIG_MACVLAN=m
  912. +CONFIG_NETCONSOLE=m
  913. +CONFIG_TUN=m
  914. +CONFIG_VETH=m
  915. +CONFIG_MDIO_BITBANG=m
  916. +CONFIG_PPP=m
  917. +CONFIG_PPP_BSDCOMP=m
  918. +CONFIG_PPP_DEFLATE=m
  919. +CONFIG_PPP_FILTER=y
  920. +CONFIG_PPP_MPPE=m
  921. +CONFIG_PPP_MULTILINK=y
  922. +CONFIG_PPPOATM=m
  923. +CONFIG_PPPOE=m
  924. +CONFIG_PPPOL2TP=m
  925. +CONFIG_PPP_ASYNC=m
  926. +CONFIG_PPP_SYNC_TTY=m
  927. +CONFIG_SLIP=m
  928. +CONFIG_SLIP_COMPRESSED=y
  929. +CONFIG_SLIP_SMART=y
  930. +CONFIG_USB_CATC=m
  931. +CONFIG_USB_KAWETH=m
  932. +CONFIG_USB_PEGASUS=m
  933. +CONFIG_USB_RTL8150=m
  934. +CONFIG_USB_RTL8152=m
  935. +CONFIG_USB_USBNET=y
  936. +CONFIG_USB_NET_AX8817X=m
  937. +CONFIG_USB_NET_AX88179_178A=m
  938. +CONFIG_USB_NET_CDCETHER=m
  939. +CONFIG_USB_NET_CDC_EEM=m
  940. +CONFIG_USB_NET_CDC_NCM=m
  941. +CONFIG_USB_NET_CDC_MBIM=m
  942. +CONFIG_USB_NET_DM9601=m
  943. +CONFIG_USB_NET_SMSC75XX=m
  944. +CONFIG_USB_NET_SMSC95XX=y
  945. +CONFIG_USB_NET_GL620A=m
  946. +CONFIG_USB_NET_NET1080=m
  947. +CONFIG_USB_NET_PLUSB=m
  948. +CONFIG_USB_NET_MCS7830=m
  949. +CONFIG_USB_NET_CDC_SUBSET=m
  950. +CONFIG_USB_ALI_M5632=y
  951. +CONFIG_USB_AN2720=y
  952. +CONFIG_USB_EPSON2888=y
  953. +CONFIG_USB_KC2190=y
  954. +CONFIG_USB_NET_ZAURUS=m
  955. +CONFIG_USB_NET_CX82310_ETH=m
  956. +CONFIG_USB_NET_KALMIA=m
  957. +CONFIG_USB_NET_QMI_WWAN=m
  958. +CONFIG_USB_HSO=m
  959. +CONFIG_USB_NET_INT51X1=m
  960. +CONFIG_USB_IPHETH=m
  961. +CONFIG_USB_SIERRA_NET=m
  962. +CONFIG_USB_VL600=m
  963. +CONFIG_LIBERTAS_THINFIRM=m
  964. +CONFIG_LIBERTAS_THINFIRM_USB=m
  965. +CONFIG_AT76C50X_USB=m
  966. +CONFIG_USB_ZD1201=m
  967. +CONFIG_USB_NET_RNDIS_WLAN=m
  968. +CONFIG_RTL8187=m
  969. +CONFIG_MAC80211_HWSIM=m
  970. +CONFIG_ATH_CARDS=m
  971. +CONFIG_ATH9K=m
  972. +CONFIG_ATH9K_HTC=m
  973. +CONFIG_CARL9170=m
  974. +CONFIG_ATH6KL=m
  975. +CONFIG_ATH6KL_USB=m
  976. +CONFIG_AR5523=m
  977. +CONFIG_B43=m
  978. +# CONFIG_B43_PHY_N is not set
  979. +CONFIG_B43LEGACY=m
  980. +CONFIG_HOSTAP=m
  981. +CONFIG_LIBERTAS=m
  982. +CONFIG_LIBERTAS_USB=m
  983. +CONFIG_LIBERTAS_SDIO=m
  984. +CONFIG_P54_COMMON=m
  985. +CONFIG_P54_USB=m
  986. +CONFIG_RT2X00=m
  987. +CONFIG_RT2500USB=m
  988. +CONFIG_RT73USB=m
  989. +CONFIG_RT2800USB=m
  990. +CONFIG_RT2800USB_RT3573=y
  991. +CONFIG_RT2800USB_RT53XX=y
  992. +CONFIG_RT2800USB_RT55XX=y
  993. +CONFIG_RT2800USB_UNKNOWN=y
  994. +CONFIG_RTL8192CU=m
  995. +CONFIG_ZD1211RW=m
  996. +CONFIG_MWIFIEX=m
  997. +CONFIG_MWIFIEX_SDIO=m
  998. +CONFIG_WIMAX_I2400M_USB=m
  999. +CONFIG_INPUT_POLLDEV=m
  1000. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1001. +CONFIG_INPUT_JOYDEV=m
  1002. +CONFIG_INPUT_EVDEV=m
  1003. +# CONFIG_INPUT_KEYBOARD is not set
  1004. +# CONFIG_INPUT_MOUSE is not set
  1005. +CONFIG_INPUT_JOYSTICK=y
  1006. +CONFIG_JOYSTICK_IFORCE=m
  1007. +CONFIG_JOYSTICK_IFORCE_USB=y
  1008. +CONFIG_JOYSTICK_XPAD=m
  1009. +CONFIG_JOYSTICK_XPAD_FF=y
  1010. +CONFIG_INPUT_TOUCHSCREEN=y
  1011. +CONFIG_TOUCHSCREEN_ADS7846=m
  1012. +CONFIG_INPUT_MISC=y
  1013. +CONFIG_INPUT_AD714X=m
  1014. +CONFIG_INPUT_ATI_REMOTE2=m
  1015. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1016. +CONFIG_INPUT_POWERMATE=m
  1017. +CONFIG_INPUT_YEALINK=m
  1018. +CONFIG_INPUT_CM109=m
  1019. +CONFIG_INPUT_UINPUT=m
  1020. +CONFIG_INPUT_ADXL34X=m
  1021. +CONFIG_INPUT_CMA3000=m
  1022. +CONFIG_SERIO=m
  1023. +CONFIG_SERIO_RAW=m
  1024. +CONFIG_GAMEPORT=m
  1025. +CONFIG_GAMEPORT_NS558=m
  1026. +CONFIG_GAMEPORT_L4=m
  1027. +CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
  1028. +# CONFIG_LEGACY_PTYS is not set
  1029. +# CONFIG_DEVKMEM is not set
  1030. +CONFIG_SERIAL_AMBA_PL011=y
  1031. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1032. +CONFIG_TTY_PRINTK=y
  1033. +CONFIG_HW_RANDOM=y
  1034. +CONFIG_HW_RANDOM_BCM2708=m
  1035. +CONFIG_RAW_DRIVER=y
  1036. +CONFIG_BRCM_CHAR_DRIVERS=y
  1037. +CONFIG_BCM_VC_CMA=y
  1038. +CONFIG_BCM_VC_SM=y
  1039. +CONFIG_I2C=y
  1040. +CONFIG_I2C_CHARDEV=m
  1041. +CONFIG_I2C_BCM2708=m
  1042. +CONFIG_SPI=y
  1043. +CONFIG_SPI_BCM2708=m
  1044. +CONFIG_SPI_SPIDEV=y
  1045. +CONFIG_GPIO_SYSFS=y
  1046. +CONFIG_GPIO_ARIZONA=m
  1047. +CONFIG_W1=m
  1048. +CONFIG_W1_MASTER_DS2490=m
  1049. +CONFIG_W1_MASTER_DS2482=m
  1050. +CONFIG_W1_MASTER_DS1WM=m
  1051. +CONFIG_W1_MASTER_GPIO=m
  1052. +CONFIG_W1_SLAVE_THERM=m
  1053. +CONFIG_W1_SLAVE_SMEM=m
  1054. +CONFIG_W1_SLAVE_DS2408=m
  1055. +CONFIG_W1_SLAVE_DS2413=m
  1056. +CONFIG_W1_SLAVE_DS2423=m
  1057. +CONFIG_W1_SLAVE_DS2431=m
  1058. +CONFIG_W1_SLAVE_DS2433=m
  1059. +CONFIG_W1_SLAVE_DS2760=m
  1060. +CONFIG_W1_SLAVE_DS2780=m
  1061. +CONFIG_W1_SLAVE_DS2781=m
  1062. +CONFIG_W1_SLAVE_DS28E04=m
  1063. +CONFIG_W1_SLAVE_BQ27000=m
  1064. +CONFIG_BATTERY_DS2760=m
  1065. +# CONFIG_HWMON is not set
  1066. +CONFIG_THERMAL=y
  1067. +CONFIG_THERMAL_BCM2835=y
  1068. +CONFIG_WATCHDOG=y
  1069. +CONFIG_BCM2708_WDT=m
  1070. +CONFIG_UCB1400_CORE=m
  1071. +CONFIG_MFD_ARIZONA_I2C=m
  1072. +CONFIG_MFD_ARIZONA_SPI=m
  1073. +CONFIG_MFD_WM5102=y
  1074. +CONFIG_MEDIA_SUPPORT=m
  1075. +CONFIG_MEDIA_CAMERA_SUPPORT=y
  1076. +CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
  1077. +CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
  1078. +CONFIG_MEDIA_RADIO_SUPPORT=y
  1079. +CONFIG_MEDIA_RC_SUPPORT=y
  1080. +CONFIG_MEDIA_CONTROLLER=y
  1081. +CONFIG_LIRC=m
  1082. +CONFIG_RC_DEVICES=y
  1083. +CONFIG_RC_ATI_REMOTE=m
  1084. +CONFIG_IR_IMON=m
  1085. +CONFIG_IR_MCEUSB=m
  1086. +CONFIG_IR_REDRAT3=m
  1087. +CONFIG_IR_STREAMZAP=m
  1088. +CONFIG_IR_IGUANA=m
  1089. +CONFIG_IR_TTUSBIR=m
  1090. +CONFIG_RC_LOOPBACK=m
  1091. +CONFIG_IR_GPIO_CIR=m
  1092. +CONFIG_MEDIA_USB_SUPPORT=y
  1093. +CONFIG_USB_VIDEO_CLASS=m
  1094. +CONFIG_USB_M5602=m
  1095. +CONFIG_USB_STV06XX=m
  1096. +CONFIG_USB_GL860=m
  1097. +CONFIG_USB_GSPCA_BENQ=m
  1098. +CONFIG_USB_GSPCA_CONEX=m
  1099. +CONFIG_USB_GSPCA_CPIA1=m
  1100. +CONFIG_USB_GSPCA_DTCS033=m
  1101. +CONFIG_USB_GSPCA_ETOMS=m
  1102. +CONFIG_USB_GSPCA_FINEPIX=m
  1103. +CONFIG_USB_GSPCA_JEILINJ=m
  1104. +CONFIG_USB_GSPCA_JL2005BCD=m
  1105. +CONFIG_USB_GSPCA_KINECT=m
  1106. +CONFIG_USB_GSPCA_KONICA=m
  1107. +CONFIG_USB_GSPCA_MARS=m
  1108. +CONFIG_USB_GSPCA_MR97310A=m
  1109. +CONFIG_USB_GSPCA_NW80X=m
  1110. +CONFIG_USB_GSPCA_OV519=m
  1111. +CONFIG_USB_GSPCA_OV534=m
  1112. +CONFIG_USB_GSPCA_OV534_9=m
  1113. +CONFIG_USB_GSPCA_PAC207=m
  1114. +CONFIG_USB_GSPCA_PAC7302=m
  1115. +CONFIG_USB_GSPCA_PAC7311=m
  1116. +CONFIG_USB_GSPCA_SE401=m
  1117. +CONFIG_USB_GSPCA_SN9C2028=m
  1118. +CONFIG_USB_GSPCA_SN9C20X=m
  1119. +CONFIG_USB_GSPCA_SONIXB=m
  1120. +CONFIG_USB_GSPCA_SONIXJ=m
  1121. +CONFIG_USB_GSPCA_SPCA500=m
  1122. +CONFIG_USB_GSPCA_SPCA501=m
  1123. +CONFIG_USB_GSPCA_SPCA505=m
  1124. +CONFIG_USB_GSPCA_SPCA506=m
  1125. +CONFIG_USB_GSPCA_SPCA508=m
  1126. +CONFIG_USB_GSPCA_SPCA561=m
  1127. +CONFIG_USB_GSPCA_SPCA1528=m
  1128. +CONFIG_USB_GSPCA_SQ905=m
  1129. +CONFIG_USB_GSPCA_SQ905C=m
  1130. +CONFIG_USB_GSPCA_SQ930X=m
  1131. +CONFIG_USB_GSPCA_STK014=m
  1132. +CONFIG_USB_GSPCA_STK1135=m
  1133. +CONFIG_USB_GSPCA_STV0680=m
  1134. +CONFIG_USB_GSPCA_SUNPLUS=m
  1135. +CONFIG_USB_GSPCA_T613=m
  1136. +CONFIG_USB_GSPCA_TOPRO=m
  1137. +CONFIG_USB_GSPCA_TV8532=m
  1138. +CONFIG_USB_GSPCA_VC032X=m
  1139. +CONFIG_USB_GSPCA_VICAM=m
  1140. +CONFIG_USB_GSPCA_XIRLINK_CIT=m
  1141. +CONFIG_USB_GSPCA_ZC3XX=m
  1142. +CONFIG_USB_PWC=m
  1143. +CONFIG_VIDEO_CPIA2=m
  1144. +CONFIG_USB_ZR364XX=m
  1145. +CONFIG_USB_STKWEBCAM=m
  1146. +CONFIG_USB_S2255=m
  1147. +CONFIG_VIDEO_USBTV=m
  1148. +CONFIG_VIDEO_PVRUSB2=m
  1149. +CONFIG_VIDEO_HDPVR=m
  1150. +CONFIG_VIDEO_TLG2300=m
  1151. +CONFIG_VIDEO_USBVISION=m
  1152. +CONFIG_VIDEO_STK1160_COMMON=m
  1153. +CONFIG_VIDEO_STK1160_AC97=y
  1154. +CONFIG_VIDEO_GO7007=m
  1155. +CONFIG_VIDEO_GO7007_USB=m
  1156. +CONFIG_VIDEO_GO7007_USB_S2250_BOARD=m
  1157. +CONFIG_VIDEO_AU0828=m
  1158. +CONFIG_VIDEO_AU0828_RC=y
  1159. +CONFIG_VIDEO_CX231XX=m
  1160. +CONFIG_VIDEO_CX231XX_ALSA=m
  1161. +CONFIG_VIDEO_CX231XX_DVB=m
  1162. +CONFIG_VIDEO_TM6000=m
  1163. +CONFIG_VIDEO_TM6000_ALSA=m
  1164. +CONFIG_VIDEO_TM6000_DVB=m
  1165. +CONFIG_DVB_USB=m
  1166. +CONFIG_DVB_USB_A800=m
  1167. +CONFIG_DVB_USB_DIBUSB_MB=m
  1168. +CONFIG_DVB_USB_DIBUSB_MB_FAULTY=y
  1169. +CONFIG_DVB_USB_DIBUSB_MC=m
  1170. +CONFIG_DVB_USB_DIB0700=m
  1171. +CONFIG_DVB_USB_UMT_010=m
  1172. +CONFIG_DVB_USB_CXUSB=m
  1173. +CONFIG_DVB_USB_M920X=m
  1174. +CONFIG_DVB_USB_DIGITV=m
  1175. +CONFIG_DVB_USB_VP7045=m
  1176. +CONFIG_DVB_USB_VP702X=m
  1177. +CONFIG_DVB_USB_GP8PSK=m
  1178. +CONFIG_DVB_USB_NOVA_T_USB2=m
  1179. +CONFIG_DVB_USB_TTUSB2=m
  1180. +CONFIG_DVB_USB_DTT200U=m
  1181. +CONFIG_DVB_USB_OPERA1=m
  1182. +CONFIG_DVB_USB_AF9005=m
  1183. +CONFIG_DVB_USB_AF9005_REMOTE=m
  1184. +CONFIG_DVB_USB_PCTV452E=m
  1185. +CONFIG_DVB_USB_DW2102=m
  1186. +CONFIG_DVB_USB_CINERGY_T2=m
  1187. +CONFIG_DVB_USB_DTV5100=m
  1188. +CONFIG_DVB_USB_FRIIO=m
  1189. +CONFIG_DVB_USB_AZ6027=m
  1190. +CONFIG_DVB_USB_TECHNISAT_USB2=m
  1191. +CONFIG_DVB_USB_V2=m
  1192. +CONFIG_DVB_USB_AF9015=m
  1193. +CONFIG_DVB_USB_AF9035=m
  1194. +CONFIG_DVB_USB_ANYSEE=m
  1195. +CONFIG_DVB_USB_AU6610=m
  1196. +CONFIG_DVB_USB_AZ6007=m
  1197. +CONFIG_DVB_USB_CE6230=m
  1198. +CONFIG_DVB_USB_EC168=m
  1199. +CONFIG_DVB_USB_GL861=m
  1200. +CONFIG_DVB_USB_LME2510=m
  1201. +CONFIG_DVB_USB_MXL111SF=m
  1202. +CONFIG_DVB_USB_RTL28XXU=m
  1203. +CONFIG_SMS_USB_DRV=m
  1204. +CONFIG_DVB_B2C2_FLEXCOP_USB=m
  1205. +CONFIG_VIDEO_EM28XX=m
  1206. +CONFIG_VIDEO_EM28XX_ALSA=m
  1207. +CONFIG_VIDEO_EM28XX_DVB=m
  1208. +CONFIG_V4L_PLATFORM_DRIVERS=y
  1209. +CONFIG_VIDEO_BCM2835=y
  1210. +CONFIG_VIDEO_BCM2835_MMAL=m
  1211. +CONFIG_RADIO_SI470X=y
  1212. +CONFIG_USB_SI470X=m
  1213. +CONFIG_I2C_SI470X=m
  1214. +CONFIG_RADIO_SI4713=m
  1215. +CONFIG_USB_MR800=m
  1216. +CONFIG_USB_DSBR=m
  1217. +CONFIG_RADIO_SHARK=m
  1218. +CONFIG_RADIO_SHARK2=m
  1219. +CONFIG_USB_KEENE=m
  1220. +CONFIG_USB_MA901=m
  1221. +CONFIG_RADIO_TEA5764=m
  1222. +CONFIG_RADIO_SAA7706H=m
  1223. +CONFIG_RADIO_TEF6862=m
  1224. +CONFIG_RADIO_WL1273=m
  1225. +CONFIG_RADIO_WL128X=m
  1226. +CONFIG_FB=y
  1227. +CONFIG_FB_BCM2708=y
  1228. +# CONFIG_BACKLIGHT_GENERIC is not set
  1229. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1230. +CONFIG_LOGO=y
  1231. +# CONFIG_LOGO_LINUX_MONO is not set
  1232. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1233. +CONFIG_SOUND=y
  1234. +CONFIG_SND=m
  1235. +CONFIG_SND_SEQUENCER=m
  1236. +CONFIG_SND_SEQ_DUMMY=m
  1237. +CONFIG_SND_MIXER_OSS=m
  1238. +CONFIG_SND_PCM_OSS=m
  1239. +CONFIG_SND_SEQUENCER_OSS=y
  1240. +CONFIG_SND_HRTIMER=m
  1241. +CONFIG_SND_DUMMY=m
  1242. +CONFIG_SND_ALOOP=m
  1243. +CONFIG_SND_VIRMIDI=m
  1244. +CONFIG_SND_MTPAV=m
  1245. +CONFIG_SND_SERIAL_U16550=m
  1246. +CONFIG_SND_MPU401=m
  1247. +CONFIG_SND_BCM2835=m
  1248. +CONFIG_SND_USB_AUDIO=m
  1249. +CONFIG_SND_USB_UA101=m
  1250. +CONFIG_SND_USB_CAIAQ=m
  1251. +CONFIG_SND_USB_CAIAQ_INPUT=y
  1252. +CONFIG_SND_USB_6FIRE=m
  1253. +CONFIG_SND_SOC=m
  1254. +CONFIG_SND_BCM2708_SOC_I2S=m
  1255. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
  1256. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
  1257. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
  1258. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP=m
  1259. +CONFIG_SND_BCM2708_SOC_RPI_DAC=m
  1260. +CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
  1261. +CONFIG_SND_SIMPLE_CARD=m
  1262. +CONFIG_SOUND_PRIME=m
  1263. +CONFIG_HIDRAW=y
  1264. +CONFIG_HID_A4TECH=m
  1265. +CONFIG_HID_ACRUX=m
  1266. +CONFIG_HID_APPLE=m
  1267. +CONFIG_HID_BELKIN=m
  1268. +CONFIG_HID_CHERRY=m
  1269. +CONFIG_HID_CHICONY=m
  1270. +CONFIG_HID_CYPRESS=m
  1271. +CONFIG_HID_DRAGONRISE=m
  1272. +CONFIG_HID_EMS_FF=m
  1273. +CONFIG_HID_ELECOM=m
  1274. +CONFIG_HID_EZKEY=m
  1275. +CONFIG_HID_HOLTEK=m
  1276. +CONFIG_HID_KEYTOUCH=m
  1277. +CONFIG_HID_KYE=m
  1278. +CONFIG_HID_UCLOGIC=m
  1279. +CONFIG_HID_WALTOP=m
  1280. +CONFIG_HID_GYRATION=m
  1281. +CONFIG_HID_TWINHAN=m
  1282. +CONFIG_HID_KENSINGTON=m
  1283. +CONFIG_HID_LCPOWER=m
  1284. +CONFIG_HID_LOGITECH=m
  1285. +CONFIG_HID_MAGICMOUSE=m
  1286. +CONFIG_HID_MICROSOFT=m
  1287. +CONFIG_HID_MONTEREY=m
  1288. +CONFIG_HID_MULTITOUCH=m
  1289. +CONFIG_HID_NTRIG=m
  1290. +CONFIG_HID_ORTEK=m
  1291. +CONFIG_HID_PANTHERLORD=m
  1292. +CONFIG_HID_PETALYNX=m
  1293. +CONFIG_HID_PICOLCD=m
  1294. +CONFIG_HID_ROCCAT=m
  1295. +CONFIG_HID_SAMSUNG=m
  1296. +CONFIG_HID_SONY=m
  1297. +CONFIG_HID_SPEEDLINK=m
  1298. +CONFIG_HID_SUNPLUS=m
  1299. +CONFIG_HID_GREENASIA=m
  1300. +CONFIG_HID_SMARTJOYPLUS=m
  1301. +CONFIG_HID_TOPSEED=m
  1302. +CONFIG_HID_THINGM=m
  1303. +CONFIG_HID_THRUSTMASTER=m
  1304. +CONFIG_HID_WACOM=m
  1305. +CONFIG_HID_WIIMOTE=m
  1306. +CONFIG_HID_XINMO=m
  1307. +CONFIG_HID_ZEROPLUS=m
  1308. +CONFIG_HID_ZYDACRON=m
  1309. +CONFIG_HID_PID=y
  1310. +CONFIG_USB_HIDDEV=y
  1311. +CONFIG_USB=y
  1312. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1313. +CONFIG_USB_MON=m
  1314. +CONFIG_USB_DWCOTG=y
  1315. +CONFIG_USB_PRINTER=m
  1316. +CONFIG_USB_STORAGE=y
  1317. +CONFIG_USB_STORAGE_REALTEK=m
  1318. +CONFIG_USB_STORAGE_DATAFAB=m
  1319. +CONFIG_USB_STORAGE_FREECOM=m
  1320. +CONFIG_USB_STORAGE_ISD200=m
  1321. +CONFIG_USB_STORAGE_USBAT=m
  1322. +CONFIG_USB_STORAGE_SDDR09=m
  1323. +CONFIG_USB_STORAGE_SDDR55=m
  1324. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1325. +CONFIG_USB_STORAGE_ALAUDA=m
  1326. +CONFIG_USB_STORAGE_ONETOUCH=m
  1327. +CONFIG_USB_STORAGE_KARMA=m
  1328. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1329. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1330. +CONFIG_USB_UAS=m
  1331. +CONFIG_USB_MDC800=m
  1332. +CONFIG_USB_MICROTEK=m
  1333. +CONFIG_USB_SERIAL=m
  1334. +CONFIG_USB_SERIAL_GENERIC=y
  1335. +CONFIG_USB_SERIAL_AIRCABLE=m
  1336. +CONFIG_USB_SERIAL_ARK3116=m
  1337. +CONFIG_USB_SERIAL_BELKIN=m
  1338. +CONFIG_USB_SERIAL_CH341=m
  1339. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1340. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1341. +CONFIG_USB_SERIAL_CP210X=m
  1342. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1343. +CONFIG_USB_SERIAL_EMPEG=m
  1344. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1345. +CONFIG_USB_SERIAL_VISOR=m
  1346. +CONFIG_USB_SERIAL_IPAQ=m
  1347. +CONFIG_USB_SERIAL_IR=m
  1348. +CONFIG_USB_SERIAL_EDGEPORT=m
  1349. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1350. +CONFIG_USB_SERIAL_F81232=m
  1351. +CONFIG_USB_SERIAL_GARMIN=m
  1352. +CONFIG_USB_SERIAL_IPW=m
  1353. +CONFIG_USB_SERIAL_IUU=m
  1354. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1355. +CONFIG_USB_SERIAL_KEYSPAN=m
  1356. +CONFIG_USB_SERIAL_KLSI=m
  1357. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1358. +CONFIG_USB_SERIAL_MCT_U232=m
  1359. +CONFIG_USB_SERIAL_METRO=m
  1360. +CONFIG_USB_SERIAL_MOS7720=m
  1361. +CONFIG_USB_SERIAL_MOS7840=m
  1362. +CONFIG_USB_SERIAL_NAVMAN=m
  1363. +CONFIG_USB_SERIAL_PL2303=m
  1364. +CONFIG_USB_SERIAL_OTI6858=m
  1365. +CONFIG_USB_SERIAL_QCAUX=m
  1366. +CONFIG_USB_SERIAL_QUALCOMM=m
  1367. +CONFIG_USB_SERIAL_SPCP8X5=m
  1368. +CONFIG_USB_SERIAL_SAFE=m
  1369. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1370. +CONFIG_USB_SERIAL_SYMBOL=m
  1371. +CONFIG_USB_SERIAL_TI=m
  1372. +CONFIG_USB_SERIAL_CYBERJACK=m
  1373. +CONFIG_USB_SERIAL_XIRCOM=m
  1374. +CONFIG_USB_SERIAL_OPTION=m
  1375. +CONFIG_USB_SERIAL_OMNINET=m
  1376. +CONFIG_USB_SERIAL_OPTICON=m
  1377. +CONFIG_USB_SERIAL_XSENS_MT=m
  1378. +CONFIG_USB_SERIAL_WISHBONE=m
  1379. +CONFIG_USB_SERIAL_ZTE=m
  1380. +CONFIG_USB_SERIAL_SSU100=m
  1381. +CONFIG_USB_SERIAL_QT2=m
  1382. +CONFIG_USB_SERIAL_DEBUG=m
  1383. +CONFIG_USB_EMI62=m
  1384. +CONFIG_USB_EMI26=m
  1385. +CONFIG_USB_ADUTUX=m
  1386. +CONFIG_USB_SEVSEG=m
  1387. +CONFIG_USB_RIO500=m
  1388. +CONFIG_USB_LEGOTOWER=m
  1389. +CONFIG_USB_LCD=m
  1390. +CONFIG_USB_LED=m
  1391. +CONFIG_USB_CYPRESS_CY7C63=m
  1392. +CONFIG_USB_CYTHERM=m
  1393. +CONFIG_USB_IDMOUSE=m
  1394. +CONFIG_USB_FTDI_ELAN=m
  1395. +CONFIG_USB_APPLEDISPLAY=m
  1396. +CONFIG_USB_LD=m
  1397. +CONFIG_USB_TRANCEVIBRATOR=m
  1398. +CONFIG_USB_IOWARRIOR=m
  1399. +CONFIG_USB_TEST=m
  1400. +CONFIG_USB_ISIGHTFW=m
  1401. +CONFIG_USB_YUREX=m
  1402. +CONFIG_USB_ATM=m
  1403. +CONFIG_USB_SPEEDTOUCH=m
  1404. +CONFIG_USB_CXACRU=m
  1405. +CONFIG_USB_UEAGLEATM=m
  1406. +CONFIG_USB_XUSBATM=m
  1407. +CONFIG_MMC=y
  1408. +CONFIG_MMC_BLOCK_MINORS=32
  1409. +CONFIG_MMC_SDHCI=y
  1410. +CONFIG_MMC_SDHCI_PLTFM=y
  1411. +CONFIG_MMC_BCM2835=y
  1412. +CONFIG_MMC_BCM2835_DMA=y
  1413. +CONFIG_MMC_SPI=m
  1414. +CONFIG_LEDS_GPIO=m
  1415. +CONFIG_LEDS_TRIGGER_TIMER=y
  1416. +CONFIG_LEDS_TRIGGER_ONESHOT=y
  1417. +CONFIG_LEDS_TRIGGER_HEARTBEAT=y
  1418. +CONFIG_LEDS_TRIGGER_BACKLIGHT=y
  1419. +CONFIG_LEDS_TRIGGER_CPU=y
  1420. +CONFIG_LEDS_TRIGGER_GPIO=y
  1421. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
  1422. +CONFIG_LEDS_TRIGGER_TRANSIENT=m
  1423. +CONFIG_LEDS_TRIGGER_CAMERA=m
  1424. +CONFIG_RTC_CLASS=y
  1425. +# CONFIG_RTC_HCTOSYS is not set
  1426. +CONFIG_RTC_DRV_DS1307=m
  1427. +CONFIG_RTC_DRV_DS1374=m
  1428. +CONFIG_RTC_DRV_DS1672=m
  1429. +CONFIG_RTC_DRV_DS3232=m
  1430. +CONFIG_RTC_DRV_MAX6900=m
  1431. +CONFIG_RTC_DRV_RS5C372=m
  1432. +CONFIG_RTC_DRV_ISL1208=m
  1433. +CONFIG_RTC_DRV_ISL12022=m
  1434. +CONFIG_RTC_DRV_ISL12057=m
  1435. +CONFIG_RTC_DRV_X1205=m
  1436. +CONFIG_RTC_DRV_PCF2127=m
  1437. +CONFIG_RTC_DRV_PCF8523=m
  1438. +CONFIG_RTC_DRV_PCF8563=m
  1439. +CONFIG_RTC_DRV_PCF8583=m
  1440. +CONFIG_RTC_DRV_M41T80=m
  1441. +CONFIG_RTC_DRV_BQ32K=m
  1442. +CONFIG_RTC_DRV_S35390A=m
  1443. +CONFIG_RTC_DRV_FM3130=m
  1444. +CONFIG_RTC_DRV_RX8581=m
  1445. +CONFIG_RTC_DRV_RX8025=m
  1446. +CONFIG_RTC_DRV_EM3027=m
  1447. +CONFIG_RTC_DRV_RV3029C2=m
  1448. +CONFIG_RTC_DRV_M41T93=m
  1449. +CONFIG_RTC_DRV_M41T94=m
  1450. +CONFIG_RTC_DRV_DS1305=m
  1451. +CONFIG_RTC_DRV_DS1390=m
  1452. +CONFIG_RTC_DRV_MAX6902=m
  1453. +CONFIG_RTC_DRV_R9701=m
  1454. +CONFIG_RTC_DRV_RS5C348=m
  1455. +CONFIG_RTC_DRV_DS3234=m
  1456. +CONFIG_RTC_DRV_PCF2123=m
  1457. +CONFIG_RTC_DRV_RX4581=m
  1458. +CONFIG_DMADEVICES=y
  1459. +CONFIG_DMA_BCM2708=y
  1460. +CONFIG_UIO=m
  1461. +CONFIG_UIO_PDRV_GENIRQ=m
  1462. +CONFIG_STAGING=y
  1463. +CONFIG_PRISM2_USB=m
  1464. +CONFIG_R8712U=m
  1465. +CONFIG_VT6656=m
  1466. +CONFIG_SPEAKUP=m
  1467. +CONFIG_SPEAKUP_SYNTH_SOFT=m
  1468. +CONFIG_STAGING_MEDIA=y
  1469. +CONFIG_DVB_AS102=m
  1470. +CONFIG_LIRC_STAGING=y
  1471. +CONFIG_LIRC_IGORPLUGUSB=m
  1472. +CONFIG_LIRC_IMON=m
  1473. +CONFIG_LIRC_RPI=m
  1474. +CONFIG_LIRC_SASEM=m
  1475. +CONFIG_LIRC_SERIAL=m
  1476. +# CONFIG_IOMMU_SUPPORT is not set
  1477. +CONFIG_EXTCON=m
  1478. +CONFIG_EXTCON_ARIZONA=m
  1479. +CONFIG_EXT4_FS=y
  1480. +CONFIG_EXT4_FS_POSIX_ACL=y
  1481. +CONFIG_EXT4_FS_SECURITY=y
  1482. +CONFIG_REISERFS_FS=m
  1483. +CONFIG_REISERFS_FS_XATTR=y
  1484. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1485. +CONFIG_REISERFS_FS_SECURITY=y
  1486. +CONFIG_JFS_FS=m
  1487. +CONFIG_JFS_POSIX_ACL=y
  1488. +CONFIG_JFS_SECURITY=y
  1489. +CONFIG_JFS_STATISTICS=y
  1490. +CONFIG_XFS_FS=m
  1491. +CONFIG_XFS_QUOTA=y
  1492. +CONFIG_XFS_POSIX_ACL=y
  1493. +CONFIG_XFS_RT=y
  1494. +CONFIG_GFS2_FS=m
  1495. +CONFIG_OCFS2_FS=m
  1496. +CONFIG_BTRFS_FS=m
  1497. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1498. +CONFIG_NILFS2_FS=m
  1499. +CONFIG_FANOTIFY=y
  1500. +CONFIG_QFMT_V1=m
  1501. +CONFIG_QFMT_V2=m
  1502. +CONFIG_AUTOFS4_FS=y
  1503. +CONFIG_FUSE_FS=m
  1504. +CONFIG_CUSE=m
  1505. +CONFIG_FSCACHE=y
  1506. +CONFIG_FSCACHE_STATS=y
  1507. +CONFIG_FSCACHE_HISTOGRAM=y
  1508. +CONFIG_CACHEFILES=y
  1509. +CONFIG_ISO9660_FS=m
  1510. +CONFIG_JOLIET=y
  1511. +CONFIG_ZISOFS=y
  1512. +CONFIG_UDF_FS=m
  1513. +CONFIG_MSDOS_FS=y
  1514. +CONFIG_VFAT_FS=y
  1515. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1516. +CONFIG_NTFS_FS=m
  1517. +CONFIG_NTFS_RW=y
  1518. +CONFIG_TMPFS=y
  1519. +CONFIG_TMPFS_POSIX_ACL=y
  1520. +CONFIG_CONFIGFS_FS=y
  1521. +CONFIG_ECRYPT_FS=m
  1522. +CONFIG_HFS_FS=m
  1523. +CONFIG_HFSPLUS_FS=m
  1524. +CONFIG_SQUASHFS=m
  1525. +CONFIG_SQUASHFS_XATTR=y
  1526. +CONFIG_SQUASHFS_LZO=y
  1527. +CONFIG_SQUASHFS_XZ=y
  1528. +CONFIG_F2FS_FS=y
  1529. +CONFIG_NFS_FS=y
  1530. +CONFIG_NFS_V3_ACL=y
  1531. +CONFIG_NFS_V4=y
  1532. +CONFIG_ROOT_NFS=y
  1533. +CONFIG_NFS_FSCACHE=y
  1534. +CONFIG_NFSD=m
  1535. +CONFIG_NFSD_V3_ACL=y
  1536. +CONFIG_NFSD_V4=y
  1537. +CONFIG_CIFS=m
  1538. +CONFIG_CIFS_WEAK_PW_HASH=y
  1539. +CONFIG_CIFS_XATTR=y
  1540. +CONFIG_CIFS_POSIX=y
  1541. +CONFIG_9P_FS=m
  1542. +CONFIG_9P_FS_POSIX_ACL=y
  1543. +CONFIG_NLS_DEFAULT="utf8"
  1544. +CONFIG_NLS_CODEPAGE_437=y
  1545. +CONFIG_NLS_CODEPAGE_737=m
  1546. +CONFIG_NLS_CODEPAGE_775=m
  1547. +CONFIG_NLS_CODEPAGE_850=m
  1548. +CONFIG_NLS_CODEPAGE_852=m
  1549. +CONFIG_NLS_CODEPAGE_855=m
  1550. +CONFIG_NLS_CODEPAGE_857=m
  1551. +CONFIG_NLS_CODEPAGE_860=m
  1552. +CONFIG_NLS_CODEPAGE_861=m
  1553. +CONFIG_NLS_CODEPAGE_862=m
  1554. +CONFIG_NLS_CODEPAGE_863=m
  1555. +CONFIG_NLS_CODEPAGE_864=m
  1556. +CONFIG_NLS_CODEPAGE_865=m
  1557. +CONFIG_NLS_CODEPAGE_866=m
  1558. +CONFIG_NLS_CODEPAGE_869=m
  1559. +CONFIG_NLS_CODEPAGE_936=m
  1560. +CONFIG_NLS_CODEPAGE_950=m
  1561. +CONFIG_NLS_CODEPAGE_932=m
  1562. +CONFIG_NLS_CODEPAGE_949=m
  1563. +CONFIG_NLS_CODEPAGE_874=m
  1564. +CONFIG_NLS_ISO8859_8=m
  1565. +CONFIG_NLS_CODEPAGE_1250=m
  1566. +CONFIG_NLS_CODEPAGE_1251=m
  1567. +CONFIG_NLS_ASCII=y
  1568. +CONFIG_NLS_ISO8859_1=m
  1569. +CONFIG_NLS_ISO8859_2=m
  1570. +CONFIG_NLS_ISO8859_3=m
  1571. +CONFIG_NLS_ISO8859_4=m
  1572. +CONFIG_NLS_ISO8859_5=m
  1573. +CONFIG_NLS_ISO8859_6=m
  1574. +CONFIG_NLS_ISO8859_7=m
  1575. +CONFIG_NLS_ISO8859_9=m
  1576. +CONFIG_NLS_ISO8859_13=m
  1577. +CONFIG_NLS_ISO8859_14=m
  1578. +CONFIG_NLS_ISO8859_15=m
  1579. +CONFIG_NLS_KOI8_R=m
  1580. +CONFIG_NLS_KOI8_U=m
  1581. +CONFIG_DLM=m
  1582. +CONFIG_PRINTK_TIME=y
  1583. +CONFIG_BOOT_PRINTK_DELAY=y
  1584. +CONFIG_DEBUG_MEMORY_INIT=y
  1585. +CONFIG_DETECT_HUNG_TASK=y
  1586. +CONFIG_TIMER_STATS=y
  1587. +# CONFIG_DEBUG_PREEMPT is not set
  1588. +CONFIG_LATENCYTOP=y
  1589. +CONFIG_IRQSOFF_TRACER=y
  1590. +CONFIG_SCHED_TRACER=y
  1591. +CONFIG_STACK_TRACER=y
  1592. +CONFIG_BLK_DEV_IO_TRACE=y
  1593. +# CONFIG_KPROBE_EVENT is not set
  1594. +CONFIG_FUNCTION_PROFILER=y
  1595. +CONFIG_KGDB=y
  1596. +CONFIG_KGDB_KDB=y
  1597. +CONFIG_KDB_KEYBOARD=y
  1598. +CONFIG_STRICT_DEVMEM=y
  1599. +CONFIG_CRYPTO_USER=m
  1600. +CONFIG_CRYPTO_NULL=m
  1601. +CONFIG_CRYPTO_CRYPTD=m
  1602. +CONFIG_CRYPTO_CBC=y
  1603. +CONFIG_CRYPTO_CTS=m
  1604. +CONFIG_CRYPTO_XTS=m
  1605. +CONFIG_CRYPTO_XCBC=m
  1606. +CONFIG_CRYPTO_MD5=y
  1607. +CONFIG_CRYPTO_SHA1=y
  1608. +CONFIG_CRYPTO_SHA1_ARM=m
  1609. +CONFIG_CRYPTO_SHA512=m
  1610. +CONFIG_CRYPTO_TGR192=m
  1611. +CONFIG_CRYPTO_WP512=m
  1612. +CONFIG_CRYPTO_AES_ARM=m
  1613. +CONFIG_CRYPTO_CAST5=m
  1614. +CONFIG_CRYPTO_DES=y
  1615. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1616. +# CONFIG_CRYPTO_HW is not set
  1617. +CONFIG_CRC_ITU_T=y
  1618. +CONFIG_LIBCRC32C=y
  1619. diff -Nur linux-3.17.5/arch/arm/configs/bcmrpi_quick_defconfig linux-rpi/arch/arm/configs/bcmrpi_quick_defconfig
  1620. --- linux-3.17.5/arch/arm/configs/bcmrpi_quick_defconfig 1969-12-31 18:00:00.000000000 -0600
  1621. +++ linux-rpi/arch/arm/configs/bcmrpi_quick_defconfig 2014-12-11 14:05:36.792418001 -0600
  1622. @@ -0,0 +1,197 @@
  1623. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  1624. +CONFIG_LOCALVERSION="-quick"
  1625. +# CONFIG_LOCALVERSION_AUTO is not set
  1626. +# CONFIG_SWAP is not set
  1627. +CONFIG_SYSVIPC=y
  1628. +CONFIG_POSIX_MQUEUE=y
  1629. +CONFIG_NO_HZ=y
  1630. +CONFIG_HIGH_RES_TIMERS=y
  1631. +CONFIG_IKCONFIG=y
  1632. +CONFIG_IKCONFIG_PROC=y
  1633. +CONFIG_KALLSYMS_ALL=y
  1634. +CONFIG_EMBEDDED=y
  1635. +CONFIG_PERF_EVENTS=y
  1636. +# CONFIG_COMPAT_BRK is not set
  1637. +CONFIG_SLAB=y
  1638. +CONFIG_MODULES=y
  1639. +CONFIG_MODULE_UNLOAD=y
  1640. +CONFIG_MODVERSIONS=y
  1641. +CONFIG_MODULE_SRCVERSION_ALL=y
  1642. +# CONFIG_BLK_DEV_BSG is not set
  1643. +CONFIG_ARCH_BCM2708=y
  1644. +CONFIG_PREEMPT=y
  1645. +CONFIG_AEABI=y
  1646. +CONFIG_UACCESS_WITH_MEMCPY=y
  1647. +CONFIG_ZBOOT_ROM_TEXT=0x0
  1648. +CONFIG_ZBOOT_ROM_BSS=0x0
  1649. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  1650. +CONFIG_CPU_FREQ=y
  1651. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  1652. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  1653. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  1654. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  1655. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  1656. +CONFIG_CPU_IDLE=y
  1657. +CONFIG_VFP=y
  1658. +CONFIG_BINFMT_MISC=y
  1659. +CONFIG_NET=y
  1660. +CONFIG_PACKET=y
  1661. +CONFIG_UNIX=y
  1662. +CONFIG_INET=y
  1663. +CONFIG_IP_MULTICAST=y
  1664. +CONFIG_IP_PNP=y
  1665. +CONFIG_IP_PNP_DHCP=y
  1666. +CONFIG_IP_PNP_RARP=y
  1667. +CONFIG_SYN_COOKIES=y
  1668. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  1669. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  1670. +# CONFIG_INET_XFRM_MODE_BEET is not set
  1671. +# CONFIG_INET_LRO is not set
  1672. +# CONFIG_INET_DIAG is not set
  1673. +# CONFIG_IPV6 is not set
  1674. +# CONFIG_WIRELESS is not set
  1675. +CONFIG_DEVTMPFS=y
  1676. +CONFIG_DEVTMPFS_MOUNT=y
  1677. +CONFIG_BLK_DEV_LOOP=y
  1678. +CONFIG_BLK_DEV_RAM=y
  1679. +CONFIG_SCSI=y
  1680. +# CONFIG_SCSI_PROC_FS is not set
  1681. +# CONFIG_SCSI_LOWLEVEL is not set
  1682. +CONFIG_NETDEVICES=y
  1683. +# CONFIG_NET_VENDOR_BROADCOM is not set
  1684. +# CONFIG_NET_VENDOR_CIRRUS is not set
  1685. +# CONFIG_NET_VENDOR_FARADAY is not set
  1686. +# CONFIG_NET_VENDOR_INTEL is not set
  1687. +# CONFIG_NET_VENDOR_MARVELL is not set
  1688. +# CONFIG_NET_VENDOR_MICREL is not set
  1689. +# CONFIG_NET_VENDOR_NATSEMI is not set
  1690. +# CONFIG_NET_VENDOR_SEEQ is not set
  1691. +# CONFIG_NET_VENDOR_STMICRO is not set
  1692. +# CONFIG_NET_VENDOR_WIZNET is not set
  1693. +CONFIG_USB_USBNET=y
  1694. +# CONFIG_USB_NET_AX8817X is not set
  1695. +# CONFIG_USB_NET_CDCETHER is not set
  1696. +# CONFIG_USB_NET_CDC_NCM is not set
  1697. +CONFIG_USB_NET_SMSC95XX=y
  1698. +# CONFIG_USB_NET_NET1080 is not set
  1699. +# CONFIG_USB_NET_CDC_SUBSET is not set
  1700. +# CONFIG_USB_NET_ZAURUS is not set
  1701. +# CONFIG_WLAN is not set
  1702. +# CONFIG_INPUT_MOUSEDEV is not set
  1703. +CONFIG_INPUT_EVDEV=y
  1704. +# CONFIG_INPUT_KEYBOARD is not set
  1705. +# CONFIG_INPUT_MOUSE is not set
  1706. +# CONFIG_SERIO is not set
  1707. +CONFIG_VT_HW_CONSOLE_BINDING=y
  1708. +# CONFIG_LEGACY_PTYS is not set
  1709. +# CONFIG_DEVKMEM is not set
  1710. +CONFIG_SERIAL_AMBA_PL011=y
  1711. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1712. +CONFIG_TTY_PRINTK=y
  1713. +CONFIG_HW_RANDOM=y
  1714. +CONFIG_HW_RANDOM_BCM2708=y
  1715. +CONFIG_RAW_DRIVER=y
  1716. +CONFIG_THERMAL=y
  1717. +CONFIG_THERMAL_BCM2835=y
  1718. +CONFIG_WATCHDOG=y
  1719. +CONFIG_BCM2708_WDT=y
  1720. +CONFIG_REGULATOR=y
  1721. +CONFIG_REGULATOR_DEBUG=y
  1722. +CONFIG_REGULATOR_FIXED_VOLTAGE=y
  1723. +CONFIG_REGULATOR_VIRTUAL_CONSUMER=y
  1724. +CONFIG_REGULATOR_USERSPACE_CONSUMER=y
  1725. +CONFIG_FB=y
  1726. +CONFIG_FB_BCM2708=y
  1727. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1728. +CONFIG_LOGO=y
  1729. +# CONFIG_LOGO_LINUX_MONO is not set
  1730. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1731. +CONFIG_SOUND=y
  1732. +CONFIG_SND=y
  1733. +CONFIG_SND_BCM2835=y
  1734. +# CONFIG_SND_USB is not set
  1735. +CONFIG_USB=y
  1736. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1737. +CONFIG_USB_DWCOTG=y
  1738. +CONFIG_MMC=y
  1739. +CONFIG_MMC_SDHCI=y
  1740. +CONFIG_MMC_SDHCI_PLTFM=y
  1741. +CONFIG_MMC_SDHCI_BCM2708=y
  1742. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1743. +CONFIG_NEW_LEDS=y
  1744. +CONFIG_LEDS_CLASS=y
  1745. +CONFIG_LEDS_TRIGGERS=y
  1746. +# CONFIG_IOMMU_SUPPORT is not set
  1747. +CONFIG_EXT4_FS=y
  1748. +CONFIG_EXT4_FS_POSIX_ACL=y
  1749. +CONFIG_EXT4_FS_SECURITY=y
  1750. +CONFIG_AUTOFS4_FS=y
  1751. +CONFIG_FSCACHE=y
  1752. +CONFIG_CACHEFILES=y
  1753. +CONFIG_MSDOS_FS=y
  1754. +CONFIG_VFAT_FS=y
  1755. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1756. +CONFIG_TMPFS=y
  1757. +CONFIG_TMPFS_POSIX_ACL=y
  1758. +CONFIG_CONFIGFS_FS=y
  1759. +# CONFIG_MISC_FILESYSTEMS is not set
  1760. +CONFIG_NFS_FS=y
  1761. +CONFIG_NFS_V3_ACL=y
  1762. +CONFIG_NFS_V4=y
  1763. +CONFIG_ROOT_NFS=y
  1764. +CONFIG_NFS_FSCACHE=y
  1765. +CONFIG_NLS_DEFAULT="utf8"
  1766. +CONFIG_NLS_CODEPAGE_437=y
  1767. +CONFIG_NLS_CODEPAGE_737=y
  1768. +CONFIG_NLS_CODEPAGE_775=y
  1769. +CONFIG_NLS_CODEPAGE_850=y
  1770. +CONFIG_NLS_CODEPAGE_852=y
  1771. +CONFIG_NLS_CODEPAGE_855=y
  1772. +CONFIG_NLS_CODEPAGE_857=y
  1773. +CONFIG_NLS_CODEPAGE_860=y
  1774. +CONFIG_NLS_CODEPAGE_861=y
  1775. +CONFIG_NLS_CODEPAGE_862=y
  1776. +CONFIG_NLS_CODEPAGE_863=y
  1777. +CONFIG_NLS_CODEPAGE_864=y
  1778. +CONFIG_NLS_CODEPAGE_865=y
  1779. +CONFIG_NLS_CODEPAGE_866=y
  1780. +CONFIG_NLS_CODEPAGE_869=y
  1781. +CONFIG_NLS_CODEPAGE_936=y
  1782. +CONFIG_NLS_CODEPAGE_950=y
  1783. +CONFIG_NLS_CODEPAGE_932=y
  1784. +CONFIG_NLS_CODEPAGE_949=y
  1785. +CONFIG_NLS_CODEPAGE_874=y
  1786. +CONFIG_NLS_ISO8859_8=y
  1787. +CONFIG_NLS_CODEPAGE_1250=y
  1788. +CONFIG_NLS_CODEPAGE_1251=y
  1789. +CONFIG_NLS_ASCII=y
  1790. +CONFIG_NLS_ISO8859_1=y
  1791. +CONFIG_NLS_ISO8859_2=y
  1792. +CONFIG_NLS_ISO8859_3=y
  1793. +CONFIG_NLS_ISO8859_4=y
  1794. +CONFIG_NLS_ISO8859_5=y
  1795. +CONFIG_NLS_ISO8859_6=y
  1796. +CONFIG_NLS_ISO8859_7=y
  1797. +CONFIG_NLS_ISO8859_9=y
  1798. +CONFIG_NLS_ISO8859_13=y
  1799. +CONFIG_NLS_ISO8859_14=y
  1800. +CONFIG_NLS_ISO8859_15=y
  1801. +CONFIG_NLS_UTF8=y
  1802. +CONFIG_PRINTK_TIME=y
  1803. +CONFIG_DEBUG_FS=y
  1804. +CONFIG_DETECT_HUNG_TASK=y
  1805. +# CONFIG_DEBUG_PREEMPT is not set
  1806. +# CONFIG_DEBUG_BUGVERBOSE is not set
  1807. +# CONFIG_FTRACE is not set
  1808. +CONFIG_KGDB=y
  1809. +CONFIG_KGDB_KDB=y
  1810. +# CONFIG_ARM_UNWIND is not set
  1811. +CONFIG_CRYPTO_CBC=y
  1812. +CONFIG_CRYPTO_HMAC=y
  1813. +CONFIG_CRYPTO_MD5=y
  1814. +CONFIG_CRYPTO_SHA1=y
  1815. +CONFIG_CRYPTO_DES=y
  1816. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1817. +# CONFIG_CRYPTO_HW is not set
  1818. +CONFIG_CRC_ITU_T=y
  1819. +CONFIG_LIBCRC32C=y
  1820. diff -Nur linux-3.17.5/arch/arm/include/asm/dma-mapping.h linux-rpi/arch/arm/include/asm/dma-mapping.h
  1821. --- linux-3.17.5/arch/arm/include/asm/dma-mapping.h 2014-12-06 17:57:59.000000000 -0600
  1822. +++ linux-rpi/arch/arm/include/asm/dma-mapping.h 2014-12-11 14:05:36.800418001 -0600
  1823. @@ -58,37 +58,21 @@
  1824. #ifndef __arch_pfn_to_dma
  1825. static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
  1826. {
  1827. - if (dev)
  1828. - pfn -= dev->dma_pfn_offset;
  1829. return (dma_addr_t)__pfn_to_bus(pfn);
  1830. }
  1831. static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
  1832. {
  1833. - unsigned long pfn = __bus_to_pfn(addr);
  1834. -
  1835. - if (dev)
  1836. - pfn += dev->dma_pfn_offset;
  1837. -
  1838. - return pfn;
  1839. + return __bus_to_pfn(addr);
  1840. }
  1841. static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
  1842. {
  1843. - if (dev) {
  1844. - unsigned long pfn = dma_to_pfn(dev, addr);
  1845. -
  1846. - return phys_to_virt(__pfn_to_phys(pfn));
  1847. - }
  1848. -
  1849. return (void *)__bus_to_virt((unsigned long)addr);
  1850. }
  1851. static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
  1852. {
  1853. - if (dev)
  1854. - return pfn_to_dma(dev, virt_to_pfn(addr));
  1855. -
  1856. return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
  1857. }
  1858. diff -Nur linux-3.17.5/arch/arm/include/asm/irqflags.h linux-rpi/arch/arm/include/asm/irqflags.h
  1859. --- linux-3.17.5/arch/arm/include/asm/irqflags.h 2014-12-06 17:57:59.000000000 -0600
  1860. +++ linux-rpi/arch/arm/include/asm/irqflags.h 2014-12-11 14:02:51.488418001 -0600
  1861. @@ -145,12 +145,22 @@
  1862. }
  1863. /*
  1864. - * restore saved IRQ & FIQ state
  1865. + * restore saved IRQ state
  1866. */
  1867. static inline void arch_local_irq_restore(unsigned long flags)
  1868. {
  1869. - asm volatile(
  1870. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  1871. + unsigned long temp = 0;
  1872. + flags &= ~(1 << 6);
  1873. + asm volatile (
  1874. + " mrs %0, cpsr"
  1875. + : "=r" (temp)
  1876. + :
  1877. + : "memory", "cc");
  1878. + /* Preserve FIQ bit */
  1879. + temp &= (1 << 6);
  1880. + flags = flags | temp;
  1881. + asm volatile (
  1882. + " msr cpsr_c, %0 @ local_irq_restore"
  1883. :
  1884. : "r" (flags)
  1885. : "memory", "cc");
  1886. diff -Nur linux-3.17.5/arch/arm/include/asm/string.h linux-rpi/arch/arm/include/asm/string.h
  1887. --- linux-3.17.5/arch/arm/include/asm/string.h 2014-12-06 17:57:59.000000000 -0600
  1888. +++ linux-rpi/arch/arm/include/asm/string.h 2014-12-11 14:02:51.488418001 -0600
  1889. @@ -24,6 +24,11 @@
  1890. #define __HAVE_ARCH_MEMSET
  1891. extern void * memset(void *, int, __kernel_size_t);
  1892. +#ifdef CONFIG_MACH_BCM2708
  1893. +#define __HAVE_ARCH_MEMCMP
  1894. +extern int memcmp(const void *, const void *, size_t);
  1895. +#endif
  1896. +
  1897. extern void __memzero(void *ptr, __kernel_size_t n);
  1898. #define memset(p,v,n) \
  1899. diff -Nur linux-3.17.5/arch/arm/include/asm/uaccess.h linux-rpi/arch/arm/include/asm/uaccess.h
  1900. --- linux-3.17.5/arch/arm/include/asm/uaccess.h 2014-12-06 17:57:59.000000000 -0600
  1901. +++ linux-rpi/arch/arm/include/asm/uaccess.h 2014-12-11 14:05:36.804418001 -0600
  1902. @@ -475,6 +475,7 @@
  1903. #ifdef CONFIG_MMU
  1904. extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
  1905. +extern unsigned long __must_check __copy_from_user_std(void *to, const void __user *from, unsigned long n);
  1906. extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
  1907. extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n);
  1908. extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
  1909. diff -Nur linux-3.17.5/arch/arm/Kconfig linux-rpi/arch/arm/Kconfig
  1910. --- linux-3.17.5/arch/arm/Kconfig 2014-12-06 17:57:59.000000000 -0600
  1911. +++ linux-rpi/arch/arm/Kconfig 2014-12-11 14:05:36.704418001 -0600
  1912. @@ -378,6 +378,23 @@
  1913. This enables support for systems based on Atmel
  1914. AT91RM9200 and AT91SAM9* processors.
  1915. +config ARCH_BCM2708
  1916. + bool "Broadcom BCM2708 family"
  1917. + select CPU_V6
  1918. + select ARM_AMBA
  1919. + select HAVE_SCHED_CLOCK
  1920. + select NEED_MACH_GPIO_H
  1921. + select NEED_MACH_MEMORY_H
  1922. + select COMMON_CLK
  1923. + select ARCH_HAS_CPUFREQ
  1924. + select GENERIC_CLOCKEVENTS
  1925. + select ARM_ERRATA_411920
  1926. + select MACH_BCM2708
  1927. + select VC4
  1928. + select FIQ
  1929. + help
  1930. + This enables support for Broadcom BCM2708 boards.
  1931. +
  1932. config ARCH_CLPS711X
  1933. bool "Cirrus Logic CLPS711x/EP721x/EP731x-based"
  1934. select ARCH_REQUIRE_GPIOLIB
  1935. @@ -964,6 +981,7 @@
  1936. source "arch/arm/mach-vt8500/Kconfig"
  1937. source "arch/arm/mach-w90x900/Kconfig"
  1938. +source "arch/arm/mach-bcm2708/Kconfig"
  1939. source "arch/arm/mach-zynq/Kconfig"
  1940. diff -Nur linux-3.17.5/arch/arm/Kconfig.debug linux-rpi/arch/arm/Kconfig.debug
  1941. --- linux-3.17.5/arch/arm/Kconfig.debug 2014-12-06 17:57:59.000000000 -0600
  1942. +++ linux-rpi/arch/arm/Kconfig.debug 2014-12-11 14:05:36.704418001 -0600
  1943. @@ -953,6 +953,14 @@
  1944. options; the platform specific options are deprecated
  1945. and will be soon removed.
  1946. + config DEBUG_BCM2708_UART0
  1947. + bool "Broadcom BCM2708 UART0 (PL011)"
  1948. + depends on MACH_BCM2708
  1949. + help
  1950. + Say Y here if you want the debug print routines to direct
  1951. + their output to UART 0. The port must have been initialised
  1952. + by the boot-loader before use.
  1953. +
  1954. endchoice
  1955. config DEBUG_EXYNOS_UART
  1956. diff -Nur linux-3.17.5/arch/arm/kernel/fiqasm.S linux-rpi/arch/arm/kernel/fiqasm.S
  1957. --- linux-3.17.5/arch/arm/kernel/fiqasm.S 2014-12-06 17:57:59.000000000 -0600
  1958. +++ linux-rpi/arch/arm/kernel/fiqasm.S 2014-12-11 14:05:36.808418001 -0600
  1959. @@ -47,3 +47,7 @@
  1960. mov r0, r0 @ avoid hazard prior to ARMv4
  1961. ret lr
  1962. ENDPROC(__get_fiq_regs)
  1963. +
  1964. +ENTRY(__FIQ_Branch)
  1965. + mov pc, r8
  1966. +ENDPROC(__FIQ_Branch)
  1967. diff -Nur linux-3.17.5/arch/arm/kernel/process.c linux-rpi/arch/arm/kernel/process.c
  1968. --- linux-3.17.5/arch/arm/kernel/process.c 2014-12-06 17:57:59.000000000 -0600
  1969. +++ linux-rpi/arch/arm/kernel/process.c 2014-12-11 14:05:36.816418001 -0600
  1970. @@ -171,6 +171,16 @@
  1971. }
  1972. #endif
  1973. +char bcm2708_reboot_mode = 'h';
  1974. +
  1975. +int __init reboot_setup(char *str)
  1976. +{
  1977. + bcm2708_reboot_mode = str[0];
  1978. + return 1;
  1979. +}
  1980. +
  1981. +__setup("reboot=", reboot_setup);
  1982. +
  1983. /*
  1984. * Called by kexec, immediately prior to machine_kexec().
  1985. *
  1986. diff -Nur linux-3.17.5/arch/arm/lib/arm-mem.h linux-rpi/arch/arm/lib/arm-mem.h
  1987. --- linux-3.17.5/arch/arm/lib/arm-mem.h 1969-12-31 18:00:00.000000000 -0600
  1988. +++ linux-rpi/arch/arm/lib/arm-mem.h 2014-12-11 14:02:51.504418001 -0600
  1989. @@ -0,0 +1,159 @@
  1990. +/*
  1991. +Copyright (c) 2013, Raspberry Pi Foundation
  1992. +Copyright (c) 2013, RISC OS Open Ltd
  1993. +All rights reserved.
  1994. +
  1995. +Redistribution and use in source and binary forms, with or without
  1996. +modification, are permitted provided that the following conditions are met:
  1997. + * Redistributions of source code must retain the above copyright
  1998. + notice, this list of conditions and the following disclaimer.
  1999. + * Redistributions in binary form must reproduce the above copyright
  2000. + notice, this list of conditions and the following disclaimer in the
  2001. + documentation and/or other materials provided with the distribution.
  2002. + * Neither the name of the copyright holder nor the
  2003. + names of its contributors may be used to endorse or promote products
  2004. + derived from this software without specific prior written permission.
  2005. +
  2006. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2007. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2008. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2009. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  2010. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2011. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2012. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2013. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2014. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2015. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2016. +*/
  2017. +
  2018. +.macro myfunc fname
  2019. + .func fname
  2020. + .global fname
  2021. +fname:
  2022. +.endm
  2023. +
  2024. +.macro preload_leading_step1 backwards, ptr, base
  2025. +/* If the destination is already 16-byte aligned, then we need to preload
  2026. + * between 0 and prefetch_distance (inclusive) cache lines ahead so there
  2027. + * are no gaps when the inner loop starts.
  2028. + */
  2029. + .if backwards
  2030. + sub ptr, base, #1
  2031. + bic ptr, ptr, #31
  2032. + .else
  2033. + bic ptr, base, #31
  2034. + .endif
  2035. + .set OFFSET, 0
  2036. + .rept prefetch_distance+1
  2037. + pld [ptr, #OFFSET]
  2038. + .if backwards
  2039. + .set OFFSET, OFFSET-32
  2040. + .else
  2041. + .set OFFSET, OFFSET+32
  2042. + .endif
  2043. + .endr
  2044. +.endm
  2045. +
  2046. +.macro preload_leading_step2 backwards, ptr, base, leading_bytes, tmp
  2047. +/* However, if the destination is not 16-byte aligned, we may need to
  2048. + * preload one more cache line than that. The question we need to ask is:
  2049. + * are the leading bytes more than the amount by which the source
  2050. + * pointer will be rounded down for preloading, and if so, by how many
  2051. + * cache lines?
  2052. + */
  2053. + .if backwards
  2054. +/* Here we compare against how many bytes we are into the
  2055. + * cache line, counting down from the highest such address.
  2056. + * Effectively, we want to calculate
  2057. + * leading_bytes = dst&15
  2058. + * cacheline_offset = 31-((src-leading_bytes-1)&31)
  2059. + * extra_needed = leading_bytes - cacheline_offset
  2060. + * and test if extra_needed is <= 0, or rearranging:
  2061. + * leading_bytes + (src-leading_bytes-1)&31 <= 31
  2062. + */
  2063. + mov tmp, base, lsl #32-5
  2064. + sbc tmp, tmp, leading_bytes, lsl #32-5
  2065. + adds tmp, tmp, leading_bytes, lsl #32-5
  2066. + bcc 61f
  2067. + pld [ptr, #-32*(prefetch_distance+1)]
  2068. + .else
  2069. +/* Effectively, we want to calculate
  2070. + * leading_bytes = (-dst)&15
  2071. + * cacheline_offset = (src+leading_bytes)&31
  2072. + * extra_needed = leading_bytes - cacheline_offset
  2073. + * and test if extra_needed is <= 0.
  2074. + */
  2075. + mov tmp, base, lsl #32-5
  2076. + add tmp, tmp, leading_bytes, lsl #32-5
  2077. + rsbs tmp, tmp, leading_bytes, lsl #32-5
  2078. + bls 61f
  2079. + pld [ptr, #32*(prefetch_distance+1)]
  2080. + .endif
  2081. +61:
  2082. +.endm
  2083. +
  2084. +.macro preload_trailing backwards, base, remain, tmp
  2085. + /* We need either 0, 1 or 2 extra preloads */
  2086. + .if backwards
  2087. + rsb tmp, base, #0
  2088. + mov tmp, tmp, lsl #32-5
  2089. + .else
  2090. + mov tmp, base, lsl #32-5
  2091. + .endif
  2092. + adds tmp, tmp, remain, lsl #32-5
  2093. + adceqs tmp, tmp, #0
  2094. + /* The instruction above has two effects: ensures Z is only
  2095. + * set if C was clear (so Z indicates that both shifted quantities
  2096. + * were 0), and clears C if Z was set (so C indicates that the sum
  2097. + * of the shifted quantities was greater and not equal to 32) */
  2098. + beq 82f
  2099. + .if backwards
  2100. + sub tmp, base, #1
  2101. + bic tmp, tmp, #31
  2102. + .else
  2103. + bic tmp, base, #31
  2104. + .endif
  2105. + bcc 81f
  2106. + .if backwards
  2107. + pld [tmp, #-32*(prefetch_distance+1)]
  2108. +81:
  2109. + pld [tmp, #-32*prefetch_distance]
  2110. + .else
  2111. + pld [tmp, #32*(prefetch_distance+2)]
  2112. +81:
  2113. + pld [tmp, #32*(prefetch_distance+1)]
  2114. + .endif
  2115. +82:
  2116. +.endm
  2117. +
  2118. +.macro preload_all backwards, narrow_case, shift, base, remain, tmp0, tmp1
  2119. + .if backwards
  2120. + sub tmp0, base, #1
  2121. + bic tmp0, tmp0, #31
  2122. + pld [tmp0]
  2123. + sub tmp1, base, remain, lsl #shift
  2124. + .else
  2125. + bic tmp0, base, #31
  2126. + pld [tmp0]
  2127. + add tmp1, base, remain, lsl #shift
  2128. + sub tmp1, tmp1, #1
  2129. + .endif
  2130. + bic tmp1, tmp1, #31
  2131. + cmp tmp1, tmp0
  2132. + beq 92f
  2133. + .if narrow_case
  2134. + /* In this case, all the data fits in either 1 or 2 cache lines */
  2135. + pld [tmp1]
  2136. + .else
  2137. +91:
  2138. + .if backwards
  2139. + sub tmp0, tmp0, #32
  2140. + .else
  2141. + add tmp0, tmp0, #32
  2142. + .endif
  2143. + cmp tmp0, tmp1
  2144. + pld [tmp0]
  2145. + bne 91b
  2146. + .endif
  2147. +92:
  2148. +.endm
  2149. diff -Nur linux-3.17.5/arch/arm/lib/copy_from_user.S linux-rpi/arch/arm/lib/copy_from_user.S
  2150. --- linux-3.17.5/arch/arm/lib/copy_from_user.S 2014-12-06 17:57:59.000000000 -0600
  2151. +++ linux-rpi/arch/arm/lib/copy_from_user.S 2014-12-11 14:02:51.504418001 -0600
  2152. @@ -84,11 +84,13 @@
  2153. .text
  2154. -ENTRY(__copy_from_user)
  2155. +ENTRY(__copy_from_user_std)
  2156. +WEAK(__copy_from_user)
  2157. #include "copy_template.S"
  2158. ENDPROC(__copy_from_user)
  2159. +ENDPROC(__copy_from_user_std)
  2160. .pushsection .fixup,"ax"
  2161. .align 0
  2162. diff -Nur linux-3.17.5/arch/arm/lib/exports_rpi.c linux-rpi/arch/arm/lib/exports_rpi.c
  2163. --- linux-3.17.5/arch/arm/lib/exports_rpi.c 1969-12-31 18:00:00.000000000 -0600
  2164. +++ linux-rpi/arch/arm/lib/exports_rpi.c 2014-12-11 14:02:51.504418001 -0600
  2165. @@ -0,0 +1,37 @@
  2166. +/**
  2167. + * Copyright (c) 2014, Raspberry Pi (Trading) Ltd.
  2168. + *
  2169. + * Redistribution and use in source and binary forms, with or without
  2170. + * modification, are permitted provided that the following conditions
  2171. + * are met:
  2172. + * 1. Redistributions of source code must retain the above copyright
  2173. + * notice, this list of conditions, and the following disclaimer,
  2174. + * without modification.
  2175. + * 2. Redistributions in binary form must reproduce the above copyright
  2176. + * notice, this list of conditions and the following disclaimer in the
  2177. + * documentation and/or other materials provided with the distribution.
  2178. + * 3. The names of the above-listed copyright holders may not be used
  2179. + * to endorse or promote products derived from this software without
  2180. + * specific prior written permission.
  2181. + *
  2182. + * ALTERNATIVELY, this software may be distributed under the terms of the
  2183. + * GNU General Public License ("GPL") version 2, as published by the Free
  2184. + * Software Foundation.
  2185. + *
  2186. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  2187. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  2188. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  2189. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  2190. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  2191. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  2192. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  2193. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2194. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2195. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2196. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2197. + */
  2198. +
  2199. +#include <linux/kernel.h>
  2200. +#include <linux/module.h>
  2201. +
  2202. +EXPORT_SYMBOL(memcmp);
  2203. diff -Nur linux-3.17.5/arch/arm/lib/Makefile linux-rpi/arch/arm/lib/Makefile
  2204. --- linux-3.17.5/arch/arm/lib/Makefile 2014-12-06 17:57:59.000000000 -0600
  2205. +++ linux-rpi/arch/arm/lib/Makefile 2014-12-11 14:05:36.820418001 -0600
  2206. @@ -6,15 +6,24 @@
  2207. lib-y := backtrace.o changebit.o csumipv6.o csumpartial.o \
  2208. csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
  2209. - delay.o delay-loop.o findbit.o memchr.o memcpy.o \
  2210. - memmove.o memset.o memzero.o setbit.o \
  2211. - strchr.o strrchr.o \
  2212. + delay.o delay-loop.o findbit.o memchr.o memzero.o \
  2213. + setbit.o strchr.o strrchr.o \
  2214. testchangebit.o testclearbit.o testsetbit.o \
  2215. ashldi3.o ashrdi3.o lshrdi3.o muldi3.o \
  2216. ucmpdi2.o lib1funcs.o div64.o \
  2217. io-readsb.o io-writesb.o io-readsl.o io-writesl.o \
  2218. call_with_stack.o bswapsdi2.o
  2219. +# Choose optimised implementations for Raspberry Pi
  2220. +ifeq ($(CONFIG_MACH_BCM2708),y)
  2221. + CFLAGS_uaccess_with_memcpy.o += -DCOPY_FROM_USER_THRESHOLD=1600
  2222. + CFLAGS_uaccess_with_memcpy.o += -DCOPY_TO_USER_THRESHOLD=672
  2223. + obj-$(CONFIG_MODULES) += exports_rpi.o
  2224. + lib-y += memcpy_rpi.o memmove_rpi.o memset_rpi.o memcmp_rpi.o
  2225. +else
  2226. + lib-y += memcpy.o memmove.o memset.o
  2227. +endif
  2228. +
  2229. mmu-y := clear_user.o copy_page.o getuser.o putuser.o
  2230. # the code in uaccess.S is not preemption safe and
  2231. diff -Nur linux-3.17.5/arch/arm/lib/memcmp_rpi.S linux-rpi/arch/arm/lib/memcmp_rpi.S
  2232. --- linux-3.17.5/arch/arm/lib/memcmp_rpi.S 1969-12-31 18:00:00.000000000 -0600
  2233. +++ linux-rpi/arch/arm/lib/memcmp_rpi.S 2014-12-11 14:02:51.504418001 -0600
  2234. @@ -0,0 +1,285 @@
  2235. +/*
  2236. +Copyright (c) 2013, Raspberry Pi Foundation
  2237. +Copyright (c) 2013, RISC OS Open Ltd
  2238. +All rights reserved.
  2239. +
  2240. +Redistribution and use in source and binary forms, with or without
  2241. +modification, are permitted provided that the following conditions are met:
  2242. + * Redistributions of source code must retain the above copyright
  2243. + notice, this list of conditions and the following disclaimer.
  2244. + * Redistributions in binary form must reproduce the above copyright
  2245. + notice, this list of conditions and the following disclaimer in the
  2246. + documentation and/or other materials provided with the distribution.
  2247. + * Neither the name of the copyright holder nor the
  2248. + names of its contributors may be used to endorse or promote products
  2249. + derived from this software without specific prior written permission.
  2250. +
  2251. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2252. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2253. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2254. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  2255. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2256. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2257. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2258. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2259. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2260. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2261. +*/
  2262. +
  2263. +#include <linux/linkage.h>
  2264. +#include "arm-mem.h"
  2265. +
  2266. +/* Prevent the stack from becoming executable */
  2267. +#if defined(__linux__) && defined(__ELF__)
  2268. +.section .note.GNU-stack,"",%progbits
  2269. +#endif
  2270. +
  2271. + .text
  2272. + .arch armv6
  2273. + .object_arch armv4
  2274. + .arm
  2275. + .altmacro
  2276. + .p2align 2
  2277. +
  2278. +.macro memcmp_process_head unaligned
  2279. + .if unaligned
  2280. + ldr DAT0, [S_1], #4
  2281. + ldr DAT1, [S_1], #4
  2282. + ldr DAT2, [S_1], #4
  2283. + ldr DAT3, [S_1], #4
  2284. + .else
  2285. + ldmia S_1!, {DAT0, DAT1, DAT2, DAT3}
  2286. + .endif
  2287. + ldmia S_2!, {DAT4, DAT5, DAT6, DAT7}
  2288. +.endm
  2289. +
  2290. +.macro memcmp_process_tail
  2291. + cmp DAT0, DAT4
  2292. + cmpeq DAT1, DAT5
  2293. + cmpeq DAT2, DAT6
  2294. + cmpeq DAT3, DAT7
  2295. + bne 200f
  2296. +.endm
  2297. +
  2298. +.macro memcmp_leading_31bytes
  2299. + movs DAT0, OFF, lsl #31
  2300. + ldrmib DAT0, [S_1], #1
  2301. + ldrcsh DAT1, [S_1], #2
  2302. + ldrmib DAT4, [S_2], #1
  2303. + ldrcsh DAT5, [S_2], #2
  2304. + movpl DAT0, #0
  2305. + movcc DAT1, #0
  2306. + movpl DAT4, #0
  2307. + movcc DAT5, #0
  2308. + submi N, N, #1
  2309. + subcs N, N, #2
  2310. + cmp DAT0, DAT4
  2311. + cmpeq DAT1, DAT5
  2312. + bne 200f
  2313. + movs DAT0, OFF, lsl #29
  2314. + ldrmi DAT0, [S_1], #4
  2315. + ldrcs DAT1, [S_1], #4
  2316. + ldrcs DAT2, [S_1], #4
  2317. + ldrmi DAT4, [S_2], #4
  2318. + ldmcsia S_2!, {DAT5, DAT6}
  2319. + movpl DAT0, #0
  2320. + movcc DAT1, #0
  2321. + movcc DAT2, #0
  2322. + movpl DAT4, #0
  2323. + movcc DAT5, #0
  2324. + movcc DAT6, #0
  2325. + submi N, N, #4
  2326. + subcs N, N, #8
  2327. + cmp DAT0, DAT4
  2328. + cmpeq DAT1, DAT5
  2329. + cmpeq DAT2, DAT6
  2330. + bne 200f
  2331. + tst OFF, #16
  2332. + beq 105f
  2333. + memcmp_process_head 1
  2334. + sub N, N, #16
  2335. + memcmp_process_tail
  2336. +105:
  2337. +.endm
  2338. +
  2339. +.macro memcmp_trailing_15bytes unaligned
  2340. + movs N, N, lsl #29
  2341. + .if unaligned
  2342. + ldrcs DAT0, [S_1], #4
  2343. + ldrcs DAT1, [S_1], #4
  2344. + .else
  2345. + ldmcsia S_1!, {DAT0, DAT1}
  2346. + .endif
  2347. + ldrmi DAT2, [S_1], #4
  2348. + ldmcsia S_2!, {DAT4, DAT5}
  2349. + ldrmi DAT6, [S_2], #4
  2350. + movcc DAT0, #0
  2351. + movcc DAT1, #0
  2352. + movpl DAT2, #0
  2353. + movcc DAT4, #0
  2354. + movcc DAT5, #0
  2355. + movpl DAT6, #0
  2356. + cmp DAT0, DAT4
  2357. + cmpeq DAT1, DAT5
  2358. + cmpeq DAT2, DAT6
  2359. + bne 200f
  2360. + movs N, N, lsl #2
  2361. + ldrcsh DAT0, [S_1], #2
  2362. + ldrmib DAT1, [S_1]
  2363. + ldrcsh DAT4, [S_2], #2
  2364. + ldrmib DAT5, [S_2]
  2365. + movcc DAT0, #0
  2366. + movpl DAT1, #0
  2367. + movcc DAT4, #0
  2368. + movpl DAT5, #0
  2369. + cmp DAT0, DAT4
  2370. + cmpeq DAT1, DAT5
  2371. + bne 200f
  2372. +.endm
  2373. +
  2374. +.macro memcmp_long_inner_loop unaligned
  2375. +110:
  2376. + memcmp_process_head unaligned
  2377. + pld [S_2, #prefetch_distance*32 + 16]
  2378. + memcmp_process_tail
  2379. + memcmp_process_head unaligned
  2380. + pld [S_1, OFF]
  2381. + memcmp_process_tail
  2382. + subs N, N, #32
  2383. + bhs 110b
  2384. + /* Just before the final (prefetch_distance+1) 32-byte blocks,
  2385. + * deal with final preloads */
  2386. + preload_trailing 0, S_1, N, DAT0
  2387. + preload_trailing 0, S_2, N, DAT0
  2388. + add N, N, #(prefetch_distance+2)*32 - 16
  2389. +120:
  2390. + memcmp_process_head unaligned
  2391. + memcmp_process_tail
  2392. + subs N, N, #16
  2393. + bhs 120b
  2394. + /* Trailing words and bytes */
  2395. + tst N, #15
  2396. + beq 199f
  2397. + memcmp_trailing_15bytes unaligned
  2398. +199: /* Reached end without detecting a difference */
  2399. + mov a1, #0
  2400. + setend le
  2401. + pop {DAT1-DAT6, pc}
  2402. +.endm
  2403. +
  2404. +.macro memcmp_short_inner_loop unaligned
  2405. + subs N, N, #16 /* simplifies inner loop termination */
  2406. + blo 122f
  2407. +120:
  2408. + memcmp_process_head unaligned
  2409. + memcmp_process_tail
  2410. + subs N, N, #16
  2411. + bhs 120b
  2412. +122: /* Trailing words and bytes */
  2413. + tst N, #15
  2414. + beq 199f
  2415. + memcmp_trailing_15bytes unaligned
  2416. +199: /* Reached end without detecting a difference */
  2417. + mov a1, #0
  2418. + setend le
  2419. + pop {DAT1-DAT6, pc}
  2420. +.endm
  2421. +
  2422. +/*
  2423. + * int memcmp(const void *s1, const void *s2, size_t n);
  2424. + * On entry:
  2425. + * a1 = pointer to buffer 1
  2426. + * a2 = pointer to buffer 2
  2427. + * a3 = number of bytes to compare (as unsigned chars)
  2428. + * On exit:
  2429. + * a1 = >0/=0/<0 if s1 >/=/< s2
  2430. + */
  2431. +
  2432. +.set prefetch_distance, 2
  2433. +
  2434. +ENTRY(memcmp)
  2435. + S_1 .req a1
  2436. + S_2 .req a2
  2437. + N .req a3
  2438. + DAT0 .req a4
  2439. + DAT1 .req v1
  2440. + DAT2 .req v2
  2441. + DAT3 .req v3
  2442. + DAT4 .req v4
  2443. + DAT5 .req v5
  2444. + DAT6 .req v6
  2445. + DAT7 .req ip
  2446. + OFF .req lr
  2447. +
  2448. + push {DAT1-DAT6, lr}
  2449. + setend be /* lowest-addressed bytes are most significant */
  2450. +
  2451. + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
  2452. + cmp N, #(prefetch_distance+3)*32 - 1
  2453. + blo 170f
  2454. +
  2455. + /* Long case */
  2456. + /* Adjust N so that the decrement instruction can also test for
  2457. + * inner loop termination. We want it to stop when there are
  2458. + * (prefetch_distance+1) complete blocks to go. */
  2459. + sub N, N, #(prefetch_distance+2)*32
  2460. + preload_leading_step1 0, DAT0, S_1
  2461. + preload_leading_step1 0, DAT1, S_2
  2462. + tst S_2, #31
  2463. + beq 154f
  2464. + rsb OFF, S_2, #0 /* no need to AND with 15 here */
  2465. + preload_leading_step2 0, DAT0, S_1, OFF, DAT2
  2466. + preload_leading_step2 0, DAT1, S_2, OFF, DAT2
  2467. + memcmp_leading_31bytes
  2468. +154: /* Second source now cacheline (32-byte) aligned; we have at
  2469. + * least one prefetch to go. */
  2470. + /* Prefetch offset is best selected such that it lies in the
  2471. + * first 8 of each 32 bytes - but it's just as easy to aim for
  2472. + * the first one */
  2473. + and OFF, S_1, #31
  2474. + rsb OFF, OFF, #32*prefetch_distance
  2475. + tst S_1, #3
  2476. + bne 140f
  2477. + memcmp_long_inner_loop 0
  2478. +140: memcmp_long_inner_loop 1
  2479. +
  2480. +170: /* Short case */
  2481. + teq N, #0
  2482. + beq 199f
  2483. + preload_all 0, 0, 0, S_1, N, DAT0, DAT1
  2484. + preload_all 0, 0, 0, S_2, N, DAT0, DAT1
  2485. + tst S_2, #3
  2486. + beq 174f
  2487. +172: subs N, N, #1
  2488. + blo 199f
  2489. + ldrb DAT0, [S_1], #1
  2490. + ldrb DAT4, [S_2], #1
  2491. + cmp DAT0, DAT4
  2492. + bne 200f
  2493. + tst S_2, #3
  2494. + bne 172b
  2495. +174: /* Second source now 4-byte aligned; we have 0 or more bytes to go */
  2496. + tst S_1, #3
  2497. + bne 140f
  2498. + memcmp_short_inner_loop 0
  2499. +140: memcmp_short_inner_loop 1
  2500. +
  2501. +200: /* Difference found: determine sign. */
  2502. + movhi a1, #1
  2503. + movlo a1, #-1
  2504. + setend le
  2505. + pop {DAT1-DAT6, pc}
  2506. +
  2507. + .unreq S_1
  2508. + .unreq S_2
  2509. + .unreq N
  2510. + .unreq DAT0
  2511. + .unreq DAT1
  2512. + .unreq DAT2
  2513. + .unreq DAT3
  2514. + .unreq DAT4
  2515. + .unreq DAT5
  2516. + .unreq DAT6
  2517. + .unreq DAT7
  2518. + .unreq OFF
  2519. +ENDPROC(memcmp)
  2520. diff -Nur linux-3.17.5/arch/arm/lib/memcpymove.h linux-rpi/arch/arm/lib/memcpymove.h
  2521. --- linux-3.17.5/arch/arm/lib/memcpymove.h 1969-12-31 18:00:00.000000000 -0600
  2522. +++ linux-rpi/arch/arm/lib/memcpymove.h 2014-12-11 14:02:51.504418001 -0600
  2523. @@ -0,0 +1,506 @@
  2524. +/*
  2525. +Copyright (c) 2013, Raspberry Pi Foundation
  2526. +Copyright (c) 2013, RISC OS Open Ltd
  2527. +All rights reserved.
  2528. +
  2529. +Redistribution and use in source and binary forms, with or without
  2530. +modification, are permitted provided that the following conditions are met:
  2531. + * Redistributions of source code must retain the above copyright
  2532. + notice, this list of conditions and the following disclaimer.
  2533. + * Redistributions in binary form must reproduce the above copyright
  2534. + notice, this list of conditions and the following disclaimer in the
  2535. + documentation and/or other materials provided with the distribution.
  2536. + * Neither the name of the copyright holder nor the
  2537. + names of its contributors may be used to endorse or promote products
  2538. + derived from this software without specific prior written permission.
  2539. +
  2540. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2541. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2542. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2543. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  2544. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2545. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2546. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2547. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2548. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2549. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2550. +*/
  2551. +
  2552. +.macro unaligned_words backwards, align, use_pld, words, r0, r1, r2, r3, r4, r5, r6, r7, r8
  2553. + .if words == 1
  2554. + .if backwards
  2555. + mov r1, r0, lsl #32-align*8
  2556. + ldr r0, [S, #-4]!
  2557. + orr r1, r1, r0, lsr #align*8
  2558. + str r1, [D, #-4]!
  2559. + .else
  2560. + mov r0, r1, lsr #align*8
  2561. + ldr r1, [S, #4]!
  2562. + orr r0, r0, r1, lsl #32-align*8
  2563. + str r0, [D], #4
  2564. + .endif
  2565. + .elseif words == 2
  2566. + .if backwards
  2567. + ldr r1, [S, #-4]!
  2568. + mov r2, r0, lsl #32-align*8
  2569. + ldr r0, [S, #-4]!
  2570. + orr r2, r2, r1, lsr #align*8
  2571. + mov r1, r1, lsl #32-align*8
  2572. + orr r1, r1, r0, lsr #align*8
  2573. + stmdb D!, {r1, r2}
  2574. + .else
  2575. + ldr r1, [S, #4]!
  2576. + mov r0, r2, lsr #align*8
  2577. + ldr r2, [S, #4]!
  2578. + orr r0, r0, r1, lsl #32-align*8
  2579. + mov r1, r1, lsr #align*8
  2580. + orr r1, r1, r2, lsl #32-align*8
  2581. + stmia D!, {r0, r1}
  2582. + .endif
  2583. + .elseif words == 4
  2584. + .if backwards
  2585. + ldmdb S!, {r2, r3}
  2586. + mov r4, r0, lsl #32-align*8
  2587. + ldmdb S!, {r0, r1}
  2588. + orr r4, r4, r3, lsr #align*8
  2589. + mov r3, r3, lsl #32-align*8
  2590. + orr r3, r3, r2, lsr #align*8
  2591. + mov r2, r2, lsl #32-align*8
  2592. + orr r2, r2, r1, lsr #align*8
  2593. + mov r1, r1, lsl #32-align*8
  2594. + orr r1, r1, r0, lsr #align*8
  2595. + stmdb D!, {r1, r2, r3, r4}
  2596. + .else
  2597. + ldmib S!, {r1, r2}
  2598. + mov r0, r4, lsr #align*8
  2599. + ldmib S!, {r3, r4}
  2600. + orr r0, r0, r1, lsl #32-align*8
  2601. + mov r1, r1, lsr #align*8
  2602. + orr r1, r1, r2, lsl #32-align*8
  2603. + mov r2, r2, lsr #align*8
  2604. + orr r2, r2, r3, lsl #32-align*8
  2605. + mov r3, r3, lsr #align*8
  2606. + orr r3, r3, r4, lsl #32-align*8
  2607. + stmia D!, {r0, r1, r2, r3}
  2608. + .endif
  2609. + .elseif words == 8
  2610. + .if backwards
  2611. + ldmdb S!, {r4, r5, r6, r7}
  2612. + mov r8, r0, lsl #32-align*8
  2613. + ldmdb S!, {r0, r1, r2, r3}
  2614. + .if use_pld
  2615. + pld [S, OFF]
  2616. + .endif
  2617. + orr r8, r8, r7, lsr #align*8
  2618. + mov r7, r7, lsl #32-align*8
  2619. + orr r7, r7, r6, lsr #align*8
  2620. + mov r6, r6, lsl #32-align*8
  2621. + orr r6, r6, r5, lsr #align*8
  2622. + mov r5, r5, lsl #32-align*8
  2623. + orr r5, r5, r4, lsr #align*8
  2624. + mov r4, r4, lsl #32-align*8
  2625. + orr r4, r4, r3, lsr #align*8
  2626. + mov r3, r3, lsl #32-align*8
  2627. + orr r3, r3, r2, lsr #align*8
  2628. + mov r2, r2, lsl #32-align*8
  2629. + orr r2, r2, r1, lsr #align*8
  2630. + mov r1, r1, lsl #32-align*8
  2631. + orr r1, r1, r0, lsr #align*8
  2632. + stmdb D!, {r5, r6, r7, r8}
  2633. + stmdb D!, {r1, r2, r3, r4}
  2634. + .else
  2635. + ldmib S!, {r1, r2, r3, r4}
  2636. + mov r0, r8, lsr #align*8
  2637. + ldmib S!, {r5, r6, r7, r8}
  2638. + .if use_pld
  2639. + pld [S, OFF]
  2640. + .endif
  2641. + orr r0, r0, r1, lsl #32-align*8
  2642. + mov r1, r1, lsr #align*8
  2643. + orr r1, r1, r2, lsl #32-align*8
  2644. + mov r2, r2, lsr #align*8
  2645. + orr r2, r2, r3, lsl #32-align*8
  2646. + mov r3, r3, lsr #align*8
  2647. + orr r3, r3, r4, lsl #32-align*8
  2648. + mov r4, r4, lsr #align*8
  2649. + orr r4, r4, r5, lsl #32-align*8
  2650. + mov r5, r5, lsr #align*8
  2651. + orr r5, r5, r6, lsl #32-align*8
  2652. + mov r6, r6, lsr #align*8
  2653. + orr r6, r6, r7, lsl #32-align*8
  2654. + mov r7, r7, lsr #align*8
  2655. + orr r7, r7, r8, lsl #32-align*8
  2656. + stmia D!, {r0, r1, r2, r3}
  2657. + stmia D!, {r4, r5, r6, r7}
  2658. + .endif
  2659. + .endif
  2660. +.endm
  2661. +
  2662. +.macro memcpy_leading_15bytes backwards, align
  2663. + movs DAT1, DAT2, lsl #31
  2664. + sub N, N, DAT2
  2665. + .if backwards
  2666. + ldrmib DAT0, [S, #-1]!
  2667. + ldrcsh DAT1, [S, #-2]!
  2668. + strmib DAT0, [D, #-1]!
  2669. + strcsh DAT1, [D, #-2]!
  2670. + .else
  2671. + ldrmib DAT0, [S], #1
  2672. + ldrcsh DAT1, [S], #2
  2673. + strmib DAT0, [D], #1
  2674. + strcsh DAT1, [D], #2
  2675. + .endif
  2676. + movs DAT1, DAT2, lsl #29
  2677. + .if backwards
  2678. + ldrmi DAT0, [S, #-4]!
  2679. + .if align == 0
  2680. + ldmcsdb S!, {DAT1, DAT2}
  2681. + .else
  2682. + ldrcs DAT2, [S, #-4]!
  2683. + ldrcs DAT1, [S, #-4]!
  2684. + .endif
  2685. + strmi DAT0, [D, #-4]!
  2686. + stmcsdb D!, {DAT1, DAT2}
  2687. + .else
  2688. + ldrmi DAT0, [S], #4
  2689. + .if align == 0
  2690. + ldmcsia S!, {DAT1, DAT2}
  2691. + .else
  2692. + ldrcs DAT1, [S], #4
  2693. + ldrcs DAT2, [S], #4
  2694. + .endif
  2695. + strmi DAT0, [D], #4
  2696. + stmcsia D!, {DAT1, DAT2}
  2697. + .endif
  2698. +.endm
  2699. +
  2700. +.macro memcpy_trailing_15bytes backwards, align
  2701. + movs N, N, lsl #29
  2702. + .if backwards
  2703. + .if align == 0
  2704. + ldmcsdb S!, {DAT0, DAT1}
  2705. + .else
  2706. + ldrcs DAT1, [S, #-4]!
  2707. + ldrcs DAT0, [S, #-4]!
  2708. + .endif
  2709. + ldrmi DAT2, [S, #-4]!
  2710. + stmcsdb D!, {DAT0, DAT1}
  2711. + strmi DAT2, [D, #-4]!
  2712. + .else
  2713. + .if align == 0
  2714. + ldmcsia S!, {DAT0, DAT1}
  2715. + .else
  2716. + ldrcs DAT0, [S], #4
  2717. + ldrcs DAT1, [S], #4
  2718. + .endif
  2719. + ldrmi DAT2, [S], #4
  2720. + stmcsia D!, {DAT0, DAT1}
  2721. + strmi DAT2, [D], #4
  2722. + .endif
  2723. + movs N, N, lsl #2
  2724. + .if backwards
  2725. + ldrcsh DAT0, [S, #-2]!
  2726. + ldrmib DAT1, [S, #-1]
  2727. + strcsh DAT0, [D, #-2]!
  2728. + strmib DAT1, [D, #-1]
  2729. + .else
  2730. + ldrcsh DAT0, [S], #2
  2731. + ldrmib DAT1, [S]
  2732. + strcsh DAT0, [D], #2
  2733. + strmib DAT1, [D]
  2734. + .endif
  2735. +.endm
  2736. +
  2737. +.macro memcpy_long_inner_loop backwards, align
  2738. + .if align != 0
  2739. + .if backwards
  2740. + ldr DAT0, [S, #-align]!
  2741. + .else
  2742. + ldr LAST, [S, #-align]!
  2743. + .endif
  2744. + .endif
  2745. +110:
  2746. + .if align == 0
  2747. + .if backwards
  2748. + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  2749. + pld [S, OFF]
  2750. + stmdb D!, {DAT4, DAT5, DAT6, LAST}
  2751. + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
  2752. + .else
  2753. + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  2754. + pld [S, OFF]
  2755. + stmia D!, {DAT0, DAT1, DAT2, DAT3}
  2756. + stmia D!, {DAT4, DAT5, DAT6, LAST}
  2757. + .endif
  2758. + .else
  2759. + unaligned_words backwards, align, 1, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
  2760. + .endif
  2761. + subs N, N, #32
  2762. + bhs 110b
  2763. + /* Just before the final (prefetch_distance+1) 32-byte blocks, deal with final preloads */
  2764. + preload_trailing backwards, S, N, OFF
  2765. + add N, N, #(prefetch_distance+2)*32 - 32
  2766. +120:
  2767. + .if align == 0
  2768. + .if backwards
  2769. + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  2770. + stmdb D!, {DAT4, DAT5, DAT6, LAST}
  2771. + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
  2772. + .else
  2773. + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  2774. + stmia D!, {DAT0, DAT1, DAT2, DAT3}
  2775. + stmia D!, {DAT4, DAT5, DAT6, LAST}
  2776. + .endif
  2777. + .else
  2778. + unaligned_words backwards, align, 0, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
  2779. + .endif
  2780. + subs N, N, #32
  2781. + bhs 120b
  2782. + tst N, #16
  2783. + .if align == 0
  2784. + .if backwards
  2785. + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
  2786. + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
  2787. + .else
  2788. + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
  2789. + stmneia D!, {DAT0, DAT1, DAT2, LAST}
  2790. + .endif
  2791. + .else
  2792. + beq 130f
  2793. + unaligned_words backwards, align, 0, 4, DAT0, DAT1, DAT2, DAT3, LAST
  2794. +130:
  2795. + .endif
  2796. + /* Trailing words and bytes */
  2797. + tst N, #15
  2798. + beq 199f
  2799. + .if align != 0
  2800. + add S, S, #align
  2801. + .endif
  2802. + memcpy_trailing_15bytes backwards, align
  2803. +199:
  2804. + pop {DAT3, DAT4, DAT5, DAT6, DAT7}
  2805. + pop {D, DAT1, DAT2, pc}
  2806. +.endm
  2807. +
  2808. +.macro memcpy_medium_inner_loop backwards, align
  2809. +120:
  2810. + .if backwards
  2811. + .if align == 0
  2812. + ldmdb S!, {DAT0, DAT1, DAT2, LAST}
  2813. + .else
  2814. + ldr LAST, [S, #-4]!
  2815. + ldr DAT2, [S, #-4]!
  2816. + ldr DAT1, [S, #-4]!
  2817. + ldr DAT0, [S, #-4]!
  2818. + .endif
  2819. + stmdb D!, {DAT0, DAT1, DAT2, LAST}
  2820. + .else
  2821. + .if align == 0
  2822. + ldmia S!, {DAT0, DAT1, DAT2, LAST}
  2823. + .else
  2824. + ldr DAT0, [S], #4
  2825. + ldr DAT1, [S], #4
  2826. + ldr DAT2, [S], #4
  2827. + ldr LAST, [S], #4
  2828. + .endif
  2829. + stmia D!, {DAT0, DAT1, DAT2, LAST}
  2830. + .endif
  2831. + subs N, N, #16
  2832. + bhs 120b
  2833. + /* Trailing words and bytes */
  2834. + tst N, #15
  2835. + beq 199f
  2836. + memcpy_trailing_15bytes backwards, align
  2837. +199:
  2838. + pop {D, DAT1, DAT2, pc}
  2839. +.endm
  2840. +
  2841. +.macro memcpy_short_inner_loop backwards, align
  2842. + tst N, #16
  2843. + .if backwards
  2844. + .if align == 0
  2845. + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
  2846. + .else
  2847. + ldrne LAST, [S, #-4]!
  2848. + ldrne DAT2, [S, #-4]!
  2849. + ldrne DAT1, [S, #-4]!
  2850. + ldrne DAT0, [S, #-4]!
  2851. + .endif
  2852. + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
  2853. + .else
  2854. + .if align == 0
  2855. + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
  2856. + .else
  2857. + ldrne DAT0, [S], #4
  2858. + ldrne DAT1, [S], #4
  2859. + ldrne DAT2, [S], #4
  2860. + ldrne LAST, [S], #4
  2861. + .endif
  2862. + stmneia D!, {DAT0, DAT1, DAT2, LAST}
  2863. + .endif
  2864. + memcpy_trailing_15bytes backwards, align
  2865. +199:
  2866. + pop {D, DAT1, DAT2, pc}
  2867. +.endm
  2868. +
  2869. +.macro memcpy backwards
  2870. + D .req a1
  2871. + S .req a2
  2872. + N .req a3
  2873. + DAT0 .req a4
  2874. + DAT1 .req v1
  2875. + DAT2 .req v2
  2876. + DAT3 .req v3
  2877. + DAT4 .req v4
  2878. + DAT5 .req v5
  2879. + DAT6 .req v6
  2880. + DAT7 .req sl
  2881. + LAST .req ip
  2882. + OFF .req lr
  2883. +
  2884. + .cfi_startproc
  2885. +
  2886. + push {D, DAT1, DAT2, lr}
  2887. +
  2888. + .cfi_def_cfa_offset 16
  2889. + .cfi_rel_offset D, 0
  2890. + .cfi_undefined S
  2891. + .cfi_undefined N
  2892. + .cfi_undefined DAT0
  2893. + .cfi_rel_offset DAT1, 4
  2894. + .cfi_rel_offset DAT2, 8
  2895. + .cfi_undefined LAST
  2896. + .cfi_rel_offset lr, 12
  2897. +
  2898. + .if backwards
  2899. + add D, D, N
  2900. + add S, S, N
  2901. + .endif
  2902. +
  2903. + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
  2904. + cmp N, #31
  2905. + blo 170f
  2906. + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
  2907. + cmp N, #(prefetch_distance+3)*32 - 1
  2908. + blo 160f
  2909. +
  2910. + /* Long case */
  2911. + push {DAT3, DAT4, DAT5, DAT6, DAT7}
  2912. +
  2913. + .cfi_def_cfa_offset 36
  2914. + .cfi_rel_offset D, 20
  2915. + .cfi_rel_offset DAT1, 24
  2916. + .cfi_rel_offset DAT2, 28
  2917. + .cfi_rel_offset DAT3, 0
  2918. + .cfi_rel_offset DAT4, 4
  2919. + .cfi_rel_offset DAT5, 8
  2920. + .cfi_rel_offset DAT6, 12
  2921. + .cfi_rel_offset DAT7, 16
  2922. + .cfi_rel_offset lr, 32
  2923. +
  2924. + /* Adjust N so that the decrement instruction can also test for
  2925. + * inner loop termination. We want it to stop when there are
  2926. + * (prefetch_distance+1) complete blocks to go. */
  2927. + sub N, N, #(prefetch_distance+2)*32
  2928. + preload_leading_step1 backwards, DAT0, S
  2929. + .if backwards
  2930. + /* Bug in GAS: it accepts, but mis-assembles the instruction
  2931. + * ands DAT2, D, #60, 2
  2932. + * which sets DAT2 to the number of leading bytes until destination is aligned and also clears C (sets borrow)
  2933. + */
  2934. + .word 0xE210513C
  2935. + beq 154f
  2936. + .else
  2937. + ands DAT2, D, #15
  2938. + beq 154f
  2939. + rsb DAT2, DAT2, #16 /* number of leading bytes until destination aligned */
  2940. + .endif
  2941. + preload_leading_step2 backwards, DAT0, S, DAT2, OFF
  2942. + memcpy_leading_15bytes backwards, 1
  2943. +154: /* Destination now 16-byte aligned; we have at least one prefetch as well as at least one 16-byte output block */
  2944. + /* Prefetch offset is best selected such that it lies in the first 8 of each 32 bytes - but it's just as easy to aim for the first one */
  2945. + .if backwards
  2946. + rsb OFF, S, #3
  2947. + and OFF, OFF, #28
  2948. + sub OFF, OFF, #32*(prefetch_distance+1)
  2949. + .else
  2950. + and OFF, S, #28
  2951. + rsb OFF, OFF, #32*prefetch_distance
  2952. + .endif
  2953. + movs DAT0, S, lsl #31
  2954. + bhi 157f
  2955. + bcs 156f
  2956. + bmi 155f
  2957. + memcpy_long_inner_loop backwards, 0
  2958. +155: memcpy_long_inner_loop backwards, 1
  2959. +156: memcpy_long_inner_loop backwards, 2
  2960. +157: memcpy_long_inner_loop backwards, 3
  2961. +
  2962. + .cfi_def_cfa_offset 16
  2963. + .cfi_rel_offset D, 0
  2964. + .cfi_rel_offset DAT1, 4
  2965. + .cfi_rel_offset DAT2, 8
  2966. + .cfi_same_value DAT3
  2967. + .cfi_same_value DAT4
  2968. + .cfi_same_value DAT5
  2969. + .cfi_same_value DAT6
  2970. + .cfi_same_value DAT7
  2971. + .cfi_rel_offset lr, 12
  2972. +
  2973. +160: /* Medium case */
  2974. + preload_all backwards, 0, 0, S, N, DAT2, OFF
  2975. + sub N, N, #16 /* simplifies inner loop termination */
  2976. + .if backwards
  2977. + ands DAT2, D, #15
  2978. + beq 164f
  2979. + .else
  2980. + ands DAT2, D, #15
  2981. + beq 164f
  2982. + rsb DAT2, DAT2, #16
  2983. + .endif
  2984. + memcpy_leading_15bytes backwards, align
  2985. +164: /* Destination now 16-byte aligned; we have at least one 16-byte output block */
  2986. + tst S, #3
  2987. + bne 140f
  2988. + memcpy_medium_inner_loop backwards, 0
  2989. +140: memcpy_medium_inner_loop backwards, 1
  2990. +
  2991. +170: /* Short case, less than 31 bytes, so no guarantee of at least one 16-byte block */
  2992. + teq N, #0
  2993. + beq 199f
  2994. + preload_all backwards, 1, 0, S, N, DAT2, LAST
  2995. + tst D, #3
  2996. + beq 174f
  2997. +172: subs N, N, #1
  2998. + blo 199f
  2999. + .if backwards
  3000. + ldrb DAT0, [S, #-1]!
  3001. + strb DAT0, [D, #-1]!
  3002. + .else
  3003. + ldrb DAT0, [S], #1
  3004. + strb DAT0, [D], #1
  3005. + .endif
  3006. + tst D, #3
  3007. + bne 172b
  3008. +174: /* Destination now 4-byte aligned; we have 0 or more output bytes to go */
  3009. + tst S, #3
  3010. + bne 140f
  3011. + memcpy_short_inner_loop backwards, 0
  3012. +140: memcpy_short_inner_loop backwards, 1
  3013. +
  3014. + .cfi_endproc
  3015. +
  3016. + .unreq D
  3017. + .unreq S
  3018. + .unreq N
  3019. + .unreq DAT0
  3020. + .unreq DAT1
  3021. + .unreq DAT2
  3022. + .unreq DAT3
  3023. + .unreq DAT4
  3024. + .unreq DAT5
  3025. + .unreq DAT6
  3026. + .unreq DAT7
  3027. + .unreq LAST
  3028. + .unreq OFF
  3029. +.endm
  3030. diff -Nur linux-3.17.5/arch/arm/lib/memcpy_rpi.S linux-rpi/arch/arm/lib/memcpy_rpi.S
  3031. --- linux-3.17.5/arch/arm/lib/memcpy_rpi.S 1969-12-31 18:00:00.000000000 -0600
  3032. +++ linux-rpi/arch/arm/lib/memcpy_rpi.S 2014-12-11 14:02:51.504418001 -0600
  3033. @@ -0,0 +1,59 @@
  3034. +/*
  3035. +Copyright (c) 2013, Raspberry Pi Foundation
  3036. +Copyright (c) 2013, RISC OS Open Ltd
  3037. +All rights reserved.
  3038. +
  3039. +Redistribution and use in source and binary forms, with or without
  3040. +modification, are permitted provided that the following conditions are met:
  3041. + * Redistributions of source code must retain the above copyright
  3042. + notice, this list of conditions and the following disclaimer.
  3043. + * Redistributions in binary form must reproduce the above copyright
  3044. + notice, this list of conditions and the following disclaimer in the
  3045. + documentation and/or other materials provided with the distribution.
  3046. + * Neither the name of the copyright holder nor the
  3047. + names of its contributors may be used to endorse or promote products
  3048. + derived from this software without specific prior written permission.
  3049. +
  3050. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3051. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3052. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3053. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  3054. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3055. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3056. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3057. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3058. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3059. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3060. +*/
  3061. +
  3062. +#include <linux/linkage.h>
  3063. +#include "arm-mem.h"
  3064. +#include "memcpymove.h"
  3065. +
  3066. +/* Prevent the stack from becoming executable */
  3067. +#if defined(__linux__) && defined(__ELF__)
  3068. +.section .note.GNU-stack,"",%progbits
  3069. +#endif
  3070. +
  3071. + .text
  3072. + .arch armv6
  3073. + .object_arch armv4
  3074. + .arm
  3075. + .altmacro
  3076. + .p2align 2
  3077. +
  3078. +/*
  3079. + * void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
  3080. + * On entry:
  3081. + * a1 = pointer to destination
  3082. + * a2 = pointer to source
  3083. + * a3 = number of bytes to copy
  3084. + * On exit:
  3085. + * a1 preserved
  3086. + */
  3087. +
  3088. +.set prefetch_distance, 3
  3089. +
  3090. +ENTRY(memcpy)
  3091. + memcpy 0
  3092. +ENDPROC(memcpy)
  3093. diff -Nur linux-3.17.5/arch/arm/lib/memmove_rpi.S linux-rpi/arch/arm/lib/memmove_rpi.S
  3094. --- linux-3.17.5/arch/arm/lib/memmove_rpi.S 1969-12-31 18:00:00.000000000 -0600
  3095. +++ linux-rpi/arch/arm/lib/memmove_rpi.S 2014-12-11 14:02:51.504418001 -0600
  3096. @@ -0,0 +1,61 @@
  3097. +/*
  3098. +Copyright (c) 2013, Raspberry Pi Foundation
  3099. +Copyright (c) 2013, RISC OS Open Ltd
  3100. +All rights reserved.
  3101. +
  3102. +Redistribution and use in source and binary forms, with or without
  3103. +modification, are permitted provided that the following conditions are met:
  3104. + * Redistributions of source code must retain the above copyright
  3105. + notice, this list of conditions and the following disclaimer.
  3106. + * Redistributions in binary form must reproduce the above copyright
  3107. + notice, this list of conditions and the following disclaimer in the
  3108. + documentation and/or other materials provided with the distribution.
  3109. + * Neither the name of the copyright holder nor the
  3110. + names of its contributors may be used to endorse or promote products
  3111. + derived from this software without specific prior written permission.
  3112. +
  3113. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3114. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3115. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3116. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  3117. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3118. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3119. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3120. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3121. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3122. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3123. +*/
  3124. +
  3125. +#include <linux/linkage.h>
  3126. +#include "arm-mem.h"
  3127. +#include "memcpymove.h"
  3128. +
  3129. +/* Prevent the stack from becoming executable */
  3130. +#if defined(__linux__) && defined(__ELF__)
  3131. +.section .note.GNU-stack,"",%progbits
  3132. +#endif
  3133. +
  3134. + .text
  3135. + .arch armv6
  3136. + .object_arch armv4
  3137. + .arm
  3138. + .altmacro
  3139. + .p2align 2
  3140. +
  3141. +/*
  3142. + * void *memmove(void *s1, const void *s2, size_t n);
  3143. + * On entry:
  3144. + * a1 = pointer to destination
  3145. + * a2 = pointer to source
  3146. + * a3 = number of bytes to copy
  3147. + * On exit:
  3148. + * a1 preserved
  3149. + */
  3150. +
  3151. +.set prefetch_distance, 3
  3152. +
  3153. +ENTRY(memmove)
  3154. + cmp a2, a1
  3155. + bpl memcpy /* pl works even over -1 - 0 and 0x7fffffff - 0x80000000 boundaries */
  3156. + memcpy 1
  3157. +ENDPROC(memmove)
  3158. diff -Nur linux-3.17.5/arch/arm/lib/memset_rpi.S linux-rpi/arch/arm/lib/memset_rpi.S
  3159. --- linux-3.17.5/arch/arm/lib/memset_rpi.S 1969-12-31 18:00:00.000000000 -0600
  3160. +++ linux-rpi/arch/arm/lib/memset_rpi.S 2014-12-11 14:02:51.504418001 -0600
  3161. @@ -0,0 +1,121 @@
  3162. +/*
  3163. +Copyright (c) 2013, Raspberry Pi Foundation
  3164. +Copyright (c) 2013, RISC OS Open Ltd
  3165. +All rights reserved.
  3166. +
  3167. +Redistribution and use in source and binary forms, with or without
  3168. +modification, are permitted provided that the following conditions are met:
  3169. + * Redistributions of source code must retain the above copyright
  3170. + notice, this list of conditions and the following disclaimer.
  3171. + * Redistributions in binary form must reproduce the above copyright
  3172. + notice, this list of conditions and the following disclaimer in the
  3173. + documentation and/or other materials provided with the distribution.
  3174. + * Neither the name of the copyright holder nor the
  3175. + names of its contributors may be used to endorse or promote products
  3176. + derived from this software without specific prior written permission.
  3177. +
  3178. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3179. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3180. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3181. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  3182. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3183. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3184. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3185. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3186. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3187. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3188. +*/
  3189. +
  3190. +#include <linux/linkage.h>
  3191. +#include "arm-mem.h"
  3192. +
  3193. +/* Prevent the stack from becoming executable */
  3194. +#if defined(__linux__) && defined(__ELF__)
  3195. +.section .note.GNU-stack,"",%progbits
  3196. +#endif
  3197. +
  3198. + .text
  3199. + .arch armv6
  3200. + .object_arch armv4
  3201. + .arm
  3202. + .altmacro
  3203. + .p2align 2
  3204. +
  3205. +/*
  3206. + * void *memset(void *s, int c, size_t n);
  3207. + * On entry:
  3208. + * a1 = pointer to buffer to fill
  3209. + * a2 = byte pattern to fill with (caller-narrowed)
  3210. + * a3 = number of bytes to fill
  3211. + * On exit:
  3212. + * a1 preserved
  3213. + */
  3214. +ENTRY(memset)
  3215. + S .req a1
  3216. + DAT0 .req a2
  3217. + N .req a3
  3218. + DAT1 .req a4
  3219. + DAT2 .req ip
  3220. + DAT3 .req lr
  3221. +
  3222. + orr DAT0, DAT0, lsl #8
  3223. + push {S, lr}
  3224. + orr DAT0, DAT0, lsl #16
  3225. + mov DAT1, DAT0
  3226. +
  3227. + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
  3228. + cmp N, #31
  3229. + blo 170f
  3230. +
  3231. +161: sub N, N, #16 /* simplifies inner loop termination */
  3232. + /* Leading words and bytes */
  3233. + tst S, #15
  3234. + beq 164f
  3235. + rsb DAT3, S, #0 /* bits 0-3 = number of leading bytes until aligned */
  3236. + movs DAT2, DAT3, lsl #31
  3237. + submi N, N, #1
  3238. + strmib DAT0, [S], #1
  3239. + subcs N, N, #2
  3240. + strcsh DAT0, [S], #2
  3241. + movs DAT2, DAT3, lsl #29
  3242. + submi N, N, #4
  3243. + strmi DAT0, [S], #4
  3244. + subcs N, N, #8
  3245. + stmcsia S!, {DAT0, DAT1}
  3246. +164: /* Delayed set up of DAT2 and DAT3 so we could use them as scratch registers above */
  3247. + mov DAT2, DAT0
  3248. + mov DAT3, DAT0
  3249. + /* Now the inner loop of 16-byte stores */
  3250. +165: stmia S!, {DAT0, DAT1, DAT2, DAT3}
  3251. + subs N, N, #16
  3252. + bhs 165b
  3253. +166: /* Trailing words and bytes */
  3254. + movs N, N, lsl #29
  3255. + stmcsia S!, {DAT0, DAT1}
  3256. + strmi DAT0, [S], #4
  3257. + movs N, N, lsl #2
  3258. + strcsh DAT0, [S], #2
  3259. + strmib DAT0, [S]
  3260. +199: pop {S, pc}
  3261. +
  3262. +170: /* Short case */
  3263. + mov DAT2, DAT0
  3264. + mov DAT3, DAT0
  3265. + tst S, #3
  3266. + beq 174f
  3267. +172: subs N, N, #1
  3268. + blo 199b
  3269. + strb DAT0, [S], #1
  3270. + tst S, #3
  3271. + bne 172b
  3272. +174: tst N, #16
  3273. + stmneia S!, {DAT0, DAT1, DAT2, DAT3}
  3274. + b 166b
  3275. +
  3276. + .unreq S
  3277. + .unreq DAT0
  3278. + .unreq N
  3279. + .unreq DAT1
  3280. + .unreq DAT2
  3281. + .unreq DAT3
  3282. +ENDPROC(memset)
  3283. diff -Nur linux-3.17.5/arch/arm/lib/uaccess_with_memcpy.c linux-rpi/arch/arm/lib/uaccess_with_memcpy.c
  3284. --- linux-3.17.5/arch/arm/lib/uaccess_with_memcpy.c 2014-12-06 17:57:59.000000000 -0600
  3285. +++ linux-rpi/arch/arm/lib/uaccess_with_memcpy.c 2014-12-11 14:05:36.820418001 -0600
  3286. @@ -22,6 +22,14 @@
  3287. #include <asm/current.h>
  3288. #include <asm/page.h>
  3289. +#ifndef COPY_FROM_USER_THRESHOLD
  3290. +#define COPY_FROM_USER_THRESHOLD 64
  3291. +#endif
  3292. +
  3293. +#ifndef COPY_TO_USER_THRESHOLD
  3294. +#define COPY_TO_USER_THRESHOLD 64
  3295. +#endif
  3296. +
  3297. static int
  3298. pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
  3299. {
  3300. @@ -85,7 +93,44 @@
  3301. return 1;
  3302. }
  3303. -static unsigned long noinline
  3304. +static int
  3305. +pin_page_for_read(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
  3306. +{
  3307. + unsigned long addr = (unsigned long)_addr;
  3308. + pgd_t *pgd;
  3309. + pmd_t *pmd;
  3310. + pte_t *pte;
  3311. + pud_t *pud;
  3312. + spinlock_t *ptl;
  3313. +
  3314. + pgd = pgd_offset(current->mm, addr);
  3315. + if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
  3316. + {
  3317. + return 0;
  3318. + }
  3319. + pud = pud_offset(pgd, addr);
  3320. + if (unlikely(pud_none(*pud) || pud_bad(*pud)))
  3321. + {
  3322. + return 0;
  3323. + }
  3324. +
  3325. + pmd = pmd_offset(pud, addr);
  3326. + if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
  3327. + return 0;
  3328. +
  3329. + pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
  3330. + if (unlikely(!pte_present(*pte) || !pte_young(*pte))) {
  3331. + pte_unmap_unlock(pte, ptl);
  3332. + return 0;
  3333. + }
  3334. +
  3335. + *ptep = pte;
  3336. + *ptlp = ptl;
  3337. +
  3338. + return 1;
  3339. +}
  3340. +
  3341. +unsigned long noinline
  3342. __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
  3343. {
  3344. int atomic;
  3345. @@ -135,6 +180,54 @@
  3346. return n;
  3347. }
  3348. +unsigned long noinline
  3349. +__copy_from_user_memcpy(void *to, const void __user *from, unsigned long n)
  3350. +{
  3351. + int atomic;
  3352. +
  3353. + if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  3354. + memcpy(to, (const void *)from, n);
  3355. + return 0;
  3356. + }
  3357. +
  3358. + /* the mmap semaphore is taken only if not in an atomic context */
  3359. + atomic = in_atomic();
  3360. +
  3361. + if (!atomic)
  3362. + down_read(&current->mm->mmap_sem);
  3363. + while (n) {
  3364. + pte_t *pte;
  3365. + spinlock_t *ptl;
  3366. + int tocopy;
  3367. +
  3368. + while (!pin_page_for_read(from, &pte, &ptl)) {
  3369. + char temp;
  3370. + if (!atomic)
  3371. + up_read(&current->mm->mmap_sem);
  3372. + if (__get_user(temp, (char __user *)from))
  3373. + goto out;
  3374. + if (!atomic)
  3375. + down_read(&current->mm->mmap_sem);
  3376. + }
  3377. +
  3378. + tocopy = (~(unsigned long)from & ~PAGE_MASK) + 1;
  3379. + if (tocopy > n)
  3380. + tocopy = n;
  3381. +
  3382. + memcpy(to, (const void *)from, tocopy);
  3383. + to += tocopy;
  3384. + from += tocopy;
  3385. + n -= tocopy;
  3386. +
  3387. + pte_unmap_unlock(pte, ptl);
  3388. + }
  3389. + if (!atomic)
  3390. + up_read(&current->mm->mmap_sem);
  3391. +
  3392. +out:
  3393. + return n;
  3394. +}
  3395. +
  3396. unsigned long
  3397. __copy_to_user(void __user *to, const void *from, unsigned long n)
  3398. {
  3399. @@ -145,10 +238,25 @@
  3400. * With frame pointer disabled, tail call optimization kicks in
  3401. * as well making this test almost invisible.
  3402. */
  3403. - if (n < 64)
  3404. + if (n < COPY_TO_USER_THRESHOLD)
  3405. return __copy_to_user_std(to, from, n);
  3406. return __copy_to_user_memcpy(to, from, n);
  3407. }
  3408. +
  3409. +unsigned long
  3410. +__copy_from_user(void *to, const void __user *from, unsigned long n)
  3411. +{
  3412. + /*
  3413. + * This test is stubbed out of the main function above to keep
  3414. + * the overhead for small copies low by avoiding a large
  3415. + * register dump on the stack just to reload them right away.
  3416. + * With frame pointer disabled, tail call optimization kicks in
  3417. + * as well making this test almost invisible.
  3418. + */
  3419. + if (n < COPY_FROM_USER_THRESHOLD)
  3420. + return __copy_from_user_std(to, from, n);
  3421. + return __copy_from_user_memcpy(to, from, n);
  3422. +}
  3423. static unsigned long noinline
  3424. __clear_user_memset(void __user *addr, unsigned long n)
  3425. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/armctrl.c linux-rpi/arch/arm/mach-bcm2708/armctrl.c
  3426. --- linux-3.17.5/arch/arm/mach-bcm2708/armctrl.c 1969-12-31 18:00:00.000000000 -0600
  3427. +++ linux-rpi/arch/arm/mach-bcm2708/armctrl.c 2014-12-11 14:05:36.828418001 -0600
  3428. @@ -0,0 +1,316 @@
  3429. +/*
  3430. + * linux/arch/arm/mach-bcm2708/armctrl.c
  3431. + *
  3432. + * Copyright (C) 2010 Broadcom
  3433. + *
  3434. + * This program is free software; you can redistribute it and/or modify
  3435. + * it under the terms of the GNU General Public License as published by
  3436. + * the Free Software Foundation; either version 2 of the License, or
  3437. + * (at your option) any later version.
  3438. + *
  3439. + * This program is distributed in the hope that it will be useful,
  3440. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3441. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3442. + * GNU General Public License for more details.
  3443. + *
  3444. + * You should have received a copy of the GNU General Public License
  3445. + * along with this program; if not, write to the Free Software
  3446. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3447. + */
  3448. +#include <linux/init.h>
  3449. +#include <linux/list.h>
  3450. +#include <linux/io.h>
  3451. +#include <linux/version.h>
  3452. +#include <linux/syscore_ops.h>
  3453. +#include <linux/interrupt.h>
  3454. +#include <linux/irqdomain.h>
  3455. +#include <linux/of.h>
  3456. +
  3457. +#include <asm/mach/irq.h>
  3458. +#include <mach/hardware.h>
  3459. +#include "armctrl.h"
  3460. +
  3461. +/* For support of kernels >= 3.0 assume only one VIC for now*/
  3462. +static unsigned int remap_irqs[(INTERRUPT_ARASANSDIO + 1) - INTERRUPT_JPEG] = {
  3463. + INTERRUPT_VC_JPEG,
  3464. + INTERRUPT_VC_USB,
  3465. + INTERRUPT_VC_3D,
  3466. + INTERRUPT_VC_DMA2,
  3467. + INTERRUPT_VC_DMA3,
  3468. + INTERRUPT_VC_I2C,
  3469. + INTERRUPT_VC_SPI,
  3470. + INTERRUPT_VC_I2SPCM,
  3471. + INTERRUPT_VC_SDIO,
  3472. + INTERRUPT_VC_UART,
  3473. + INTERRUPT_VC_ARASANSDIO
  3474. +};
  3475. +
  3476. +static void armctrl_mask_irq(struct irq_data *d)
  3477. +{
  3478. + static const unsigned int disables[4] = {
  3479. + ARM_IRQ_DIBL1,
  3480. + ARM_IRQ_DIBL2,
  3481. + ARM_IRQ_DIBL3,
  3482. + 0
  3483. + };
  3484. +
  3485. + if (d->irq >= FIQ_START) {
  3486. + writel(0, __io_address(ARM_IRQ_FAST));
  3487. + } else {
  3488. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  3489. + writel(1 << (data & 0x1f), __io_address(disables[(data >> 5) & 0x3]));
  3490. + }
  3491. +}
  3492. +
  3493. +static void armctrl_unmask_irq(struct irq_data *d)
  3494. +{
  3495. + static const unsigned int enables[4] = {
  3496. + ARM_IRQ_ENBL1,
  3497. + ARM_IRQ_ENBL2,
  3498. + ARM_IRQ_ENBL3,
  3499. + 0
  3500. + };
  3501. +
  3502. + if (d->irq >= FIQ_START) {
  3503. + unsigned int data =
  3504. + (unsigned int)irq_get_chip_data(d->irq) - FIQ_START;
  3505. + writel(0x80 | data, __io_address(ARM_IRQ_FAST));
  3506. + } else {
  3507. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  3508. + writel(1 << (data & 0x1f), __io_address(enables[(data >> 5) & 0x3]));
  3509. + }
  3510. +}
  3511. +
  3512. +#ifdef CONFIG_OF
  3513. +
  3514. +#define NR_IRQS_BANK0 21
  3515. +#define NR_BANKS 3 + 1 /* bank 3 is used for GPIO interrupts */
  3516. +#define IRQS_PER_BANK 32
  3517. +
  3518. +/* from drivers/irqchip/irq-bcm2835.c */
  3519. +static int armctrl_xlate(struct irq_domain *d, struct device_node *ctrlr,
  3520. + const u32 *intspec, unsigned int intsize,
  3521. + unsigned long *out_hwirq, unsigned int *out_type)
  3522. +{
  3523. + if (WARN_ON(intsize != 2))
  3524. + return -EINVAL;
  3525. +
  3526. + if (WARN_ON(intspec[0] >= NR_BANKS))
  3527. + return -EINVAL;
  3528. +
  3529. + if (WARN_ON(intspec[1] >= IRQS_PER_BANK))
  3530. + return -EINVAL;
  3531. +
  3532. + if (WARN_ON(intspec[0] == 0 && intspec[1] >= NR_IRQS_BANK0))
  3533. + return -EINVAL;
  3534. +
  3535. + if (intspec[0] == 0)
  3536. + *out_hwirq = ARM_IRQ0_BASE + intspec[1];
  3537. + else if (intspec[0] == 1)
  3538. + *out_hwirq = ARM_IRQ1_BASE + intspec[1];
  3539. + else if (intspec[0] == 2)
  3540. + *out_hwirq = ARM_IRQ2_BASE + intspec[1];
  3541. + else
  3542. + *out_hwirq = GPIO_IRQ_START + intspec[1];
  3543. +
  3544. + /* reverse remap_irqs[] */
  3545. + switch (*out_hwirq) {
  3546. + case INTERRUPT_VC_JPEG:
  3547. + *out_hwirq = INTERRUPT_JPEG;
  3548. + break;
  3549. + case INTERRUPT_VC_USB:
  3550. + *out_hwirq = INTERRUPT_USB;
  3551. + break;
  3552. + case INTERRUPT_VC_3D:
  3553. + *out_hwirq = INTERRUPT_3D;
  3554. + break;
  3555. + case INTERRUPT_VC_DMA2:
  3556. + *out_hwirq = INTERRUPT_DMA2;
  3557. + break;
  3558. + case INTERRUPT_VC_DMA3:
  3559. + *out_hwirq = INTERRUPT_DMA3;
  3560. + break;
  3561. + case INTERRUPT_VC_I2C:
  3562. + *out_hwirq = INTERRUPT_I2C;
  3563. + break;
  3564. + case INTERRUPT_VC_SPI:
  3565. + *out_hwirq = INTERRUPT_SPI;
  3566. + break;
  3567. + case INTERRUPT_VC_I2SPCM:
  3568. + *out_hwirq = INTERRUPT_I2SPCM;
  3569. + break;
  3570. + case INTERRUPT_VC_SDIO:
  3571. + *out_hwirq = INTERRUPT_SDIO;
  3572. + break;
  3573. + case INTERRUPT_VC_UART:
  3574. + *out_hwirq = INTERRUPT_UART;
  3575. + break;
  3576. + case INTERRUPT_VC_ARASANSDIO:
  3577. + *out_hwirq = INTERRUPT_ARASANSDIO;
  3578. + break;
  3579. + }
  3580. +
  3581. + *out_type = IRQ_TYPE_NONE;
  3582. + return 0;
  3583. +}
  3584. +
  3585. +static struct irq_domain_ops armctrl_ops = {
  3586. + .xlate = armctrl_xlate
  3587. +};
  3588. +
  3589. +void __init armctrl_dt_init(void)
  3590. +{
  3591. + struct device_node *np;
  3592. + struct irq_domain *domain;
  3593. +
  3594. + np = of_find_compatible_node(NULL, NULL, "brcm,bcm2708-armctrl-ic");
  3595. + if (!np)
  3596. + return;
  3597. +
  3598. + domain = irq_domain_add_legacy(np, NR_IRQS, IRQ_ARMCTRL_START, 0,
  3599. + &armctrl_ops, NULL);
  3600. + WARN_ON(!domain);
  3601. +}
  3602. +#else
  3603. +void __init armctrl_dt_init(void) { }
  3604. +#endif /* CONFIG_OF */
  3605. +
  3606. +#if defined(CONFIG_PM)
  3607. +
  3608. +/* for kernels 3.xx use the new syscore_ops apis but for older kernels use the sys dev class */
  3609. +
  3610. +/* Static defines
  3611. + * struct armctrl_device - VIC PM device (< 3.xx)
  3612. + * @sysdev: The system device which is registered. (< 3.xx)
  3613. + * @irq: The IRQ number for the base of the VIC.
  3614. + * @base: The register base for the VIC.
  3615. + * @resume_sources: A bitmask of interrupts for resume.
  3616. + * @resume_irqs: The IRQs enabled for resume.
  3617. + * @int_select: Save for VIC_INT_SELECT.
  3618. + * @int_enable: Save for VIC_INT_ENABLE.
  3619. + * @soft_int: Save for VIC_INT_SOFT.
  3620. + * @protect: Save for VIC_PROTECT.
  3621. + */
  3622. +struct armctrl_info {
  3623. + void __iomem *base;
  3624. + int irq;
  3625. + u32 resume_sources;
  3626. + u32 resume_irqs;
  3627. + u32 int_select;
  3628. + u32 int_enable;
  3629. + u32 soft_int;
  3630. + u32 protect;
  3631. +} armctrl;
  3632. +
  3633. +static int armctrl_suspend(void)
  3634. +{
  3635. + return 0;
  3636. +}
  3637. +
  3638. +static void armctrl_resume(void)
  3639. +{
  3640. + return;
  3641. +}
  3642. +
  3643. +/**
  3644. + * armctrl_pm_register - Register a VIC for later power management control
  3645. + * @base: The base address of the VIC.
  3646. + * @irq: The base IRQ for the VIC.
  3647. + * @resume_sources: bitmask of interrupts allowed for resume sources.
  3648. + *
  3649. + * For older kernels (< 3.xx) do -
  3650. + * Register the VIC with the system device tree so that it can be notified
  3651. + * of suspend and resume requests and ensure that the correct actions are
  3652. + * taken to re-instate the settings on resume.
  3653. + */
  3654. +static void __init armctrl_pm_register(void __iomem * base, unsigned int irq,
  3655. + u32 resume_sources)
  3656. +{
  3657. + armctrl.base = base;
  3658. + armctrl.resume_sources = resume_sources;
  3659. + armctrl.irq = irq;
  3660. +}
  3661. +
  3662. +static int armctrl_set_wake(struct irq_data *d, unsigned int on)
  3663. +{
  3664. + unsigned int off = d->irq & 31;
  3665. + u32 bit = 1 << off;
  3666. +
  3667. + if (!(bit & armctrl.resume_sources))
  3668. + return -EINVAL;
  3669. +
  3670. + if (on)
  3671. + armctrl.resume_irqs |= bit;
  3672. + else
  3673. + armctrl.resume_irqs &= ~bit;
  3674. +
  3675. + return 0;
  3676. +}
  3677. +
  3678. +#else
  3679. +static inline void armctrl_pm_register(void __iomem * base, unsigned int irq,
  3680. + u32 arg1)
  3681. +{
  3682. +}
  3683. +
  3684. +#define armctrl_suspend NULL
  3685. +#define armctrl_resume NULL
  3686. +#define armctrl_set_wake NULL
  3687. +#endif /* CONFIG_PM */
  3688. +
  3689. +static struct syscore_ops armctrl_syscore_ops = {
  3690. + .suspend = armctrl_suspend,
  3691. + .resume = armctrl_resume,
  3692. +};
  3693. +
  3694. +/**
  3695. + * armctrl_syscore_init - initicall to register VIC pm functions
  3696. + *
  3697. + * This is called via late_initcall() to register
  3698. + * the resources for the VICs due to the early
  3699. + * nature of the VIC's registration.
  3700. +*/
  3701. +static int __init armctrl_syscore_init(void)
  3702. +{
  3703. + register_syscore_ops(&armctrl_syscore_ops);
  3704. + return 0;
  3705. +}
  3706. +
  3707. +late_initcall(armctrl_syscore_init);
  3708. +
  3709. +static struct irq_chip armctrl_chip = {
  3710. + .name = "ARMCTRL",
  3711. + .irq_ack = NULL,
  3712. + .irq_mask = armctrl_mask_irq,
  3713. + .irq_unmask = armctrl_unmask_irq,
  3714. + .irq_set_wake = armctrl_set_wake,
  3715. +};
  3716. +
  3717. +/**
  3718. + * armctrl_init - initialise a vectored interrupt controller
  3719. + * @base: iomem base address
  3720. + * @irq_start: starting interrupt number, must be muliple of 32
  3721. + * @armctrl_sources: bitmask of interrupt sources to allow
  3722. + * @resume_sources: bitmask of interrupt sources to allow for resume
  3723. + */
  3724. +int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  3725. + u32 armctrl_sources, u32 resume_sources)
  3726. +{
  3727. + unsigned int irq;
  3728. +
  3729. + for (irq = 0; irq < NR_IRQS; irq++) {
  3730. + unsigned int data = irq;
  3731. + if (irq >= INTERRUPT_JPEG && irq <= INTERRUPT_ARASANSDIO)
  3732. + data = remap_irqs[irq - INTERRUPT_JPEG];
  3733. +
  3734. + irq_set_chip(irq, &armctrl_chip);
  3735. + irq_set_chip_data(irq, (void *)data);
  3736. + irq_set_handler(irq, handle_level_irq);
  3737. + set_irq_flags(irq, IRQF_VALID | IRQF_PROBE | IRQF_DISABLED);
  3738. + }
  3739. +
  3740. + armctrl_pm_register(base, irq_start, resume_sources);
  3741. + init_FIQ(FIQ_START);
  3742. + armctrl_dt_init();
  3743. + return 0;
  3744. +}
  3745. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/armctrl.h linux-rpi/arch/arm/mach-bcm2708/armctrl.h
  3746. --- linux-3.17.5/arch/arm/mach-bcm2708/armctrl.h 1969-12-31 18:00:00.000000000 -0600
  3747. +++ linux-rpi/arch/arm/mach-bcm2708/armctrl.h 2014-12-11 14:02:51.516418001 -0600
  3748. @@ -0,0 +1,27 @@
  3749. +/*
  3750. + * linux/arch/arm/mach-bcm2708/armctrl.h
  3751. + *
  3752. + * Copyright (C) 2010 Broadcom
  3753. + *
  3754. + * This program is free software; you can redistribute it and/or modify
  3755. + * it under the terms of the GNU General Public License as published by
  3756. + * the Free Software Foundation; either version 2 of the License, or
  3757. + * (at your option) any later version.
  3758. + *
  3759. + * This program is distributed in the hope that it will be useful,
  3760. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3761. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3762. + * GNU General Public License for more details.
  3763. + *
  3764. + * You should have received a copy of the GNU General Public License
  3765. + * along with this program; if not, write to the Free Software
  3766. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3767. + */
  3768. +
  3769. +#ifndef __BCM2708_ARMCTRL_H
  3770. +#define __BCM2708_ARMCTRL_H
  3771. +
  3772. +extern int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  3773. + u32 armctrl_sources, u32 resume_sources);
  3774. +
  3775. +#endif
  3776. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/bcm2708.c linux-rpi/arch/arm/mach-bcm2708/bcm2708.c
  3777. --- linux-3.17.5/arch/arm/mach-bcm2708/bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  3778. +++ linux-rpi/arch/arm/mach-bcm2708/bcm2708.c 2014-12-11 14:05:36.828418001 -0600
  3779. @@ -0,0 +1,1094 @@
  3780. +/*
  3781. + * linux/arch/arm/mach-bcm2708/bcm2708.c
  3782. + *
  3783. + * Copyright (C) 2010 Broadcom
  3784. + *
  3785. + * This program is free software; you can redistribute it and/or modify
  3786. + * it under the terms of the GNU General Public License as published by
  3787. + * the Free Software Foundation; either version 2 of the License, or
  3788. + * (at your option) any later version.
  3789. + *
  3790. + * This program is distributed in the hope that it will be useful,
  3791. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3792. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3793. + * GNU General Public License for more details.
  3794. + *
  3795. + * You should have received a copy of the GNU General Public License
  3796. + * along with this program; if not, write to the Free Software
  3797. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3798. + */
  3799. +
  3800. +#include <linux/init.h>
  3801. +#include <linux/device.h>
  3802. +#include <linux/dma-mapping.h>
  3803. +#include <linux/serial_8250.h>
  3804. +#include <linux/platform_device.h>
  3805. +#include <linux/syscore_ops.h>
  3806. +#include <linux/interrupt.h>
  3807. +#include <linux/amba/bus.h>
  3808. +#include <linux/amba/clcd.h>
  3809. +#include <linux/clk-provider.h>
  3810. +#include <linux/clkdev.h>
  3811. +#include <linux/clockchips.h>
  3812. +#include <linux/cnt32_to_63.h>
  3813. +#include <linux/io.h>
  3814. +#include <linux/module.h>
  3815. +#include <linux/of_platform.h>
  3816. +#include <linux/spi/spi.h>
  3817. +#include <linux/gpio/machine.h>
  3818. +#include <linux/w1-gpio.h>
  3819. +
  3820. +#include <linux/version.h>
  3821. +#include <linux/clkdev.h>
  3822. +#include <asm/system_info.h>
  3823. +#include <mach/hardware.h>
  3824. +#include <asm/irq.h>
  3825. +#include <linux/leds.h>
  3826. +#include <asm/mach-types.h>
  3827. +#include <linux/sched_clock.h>
  3828. +
  3829. +#include <asm/mach/arch.h>
  3830. +#include <asm/mach/flash.h>
  3831. +#include <asm/mach/irq.h>
  3832. +#include <asm/mach/time.h>
  3833. +#include <asm/mach/map.h>
  3834. +
  3835. +#include <mach/timex.h>
  3836. +#include <mach/dma.h>
  3837. +#include <mach/vcio.h>
  3838. +#include <mach/system.h>
  3839. +
  3840. +#include <linux/delay.h>
  3841. +
  3842. +#include "bcm2708.h"
  3843. +#include "armctrl.h"
  3844. +
  3845. +#ifdef CONFIG_BCM_VC_CMA
  3846. +#include <linux/broadcom/vc_cma.h>
  3847. +#endif
  3848. +
  3849. +
  3850. +/* Effectively we have an IOMMU (ARM<->VideoCore map) that is set up to
  3851. + * give us IO access only to 64Mbytes of physical memory (26 bits). We could
  3852. + * represent this window by setting our dmamasks to 26 bits but, in fact
  3853. + * we're not going to use addresses outside this range (they're not in real
  3854. + * memory) so we don't bother.
  3855. + *
  3856. + * In the future we might include code to use this IOMMU to remap other
  3857. + * physical addresses onto VideoCore memory then the use of 32-bits would be
  3858. + * more legitimate.
  3859. + */
  3860. +#define DMA_MASK_BITS_COMMON 32
  3861. +
  3862. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  3863. +#define W1_GPIO 4
  3864. +// ensure one-wire GPIO pullup is disabled by default
  3865. +#define W1_PULLUP -1
  3866. +
  3867. +/* command line parameters */
  3868. +static unsigned boardrev, serial;
  3869. +static unsigned uart_clock = UART0_CLOCK;
  3870. +static unsigned disk_led_gpio = 16;
  3871. +static unsigned disk_led_active_low = 1;
  3872. +static unsigned reboot_part = 0;
  3873. +static unsigned w1_gpio_pin = W1_GPIO;
  3874. +static unsigned w1_gpio_pullup = W1_PULLUP;
  3875. +
  3876. +static unsigned use_dt = 0;
  3877. +
  3878. +static void __init bcm2708_init_led(void);
  3879. +
  3880. +void __init bcm2708_init_irq(void)
  3881. +{
  3882. + armctrl_init(__io_address(ARMCTRL_IC_BASE), 0, 0, 0);
  3883. +}
  3884. +
  3885. +static struct map_desc bcm2708_io_desc[] __initdata = {
  3886. + {
  3887. + .virtual = IO_ADDRESS(ARMCTRL_BASE),
  3888. + .pfn = __phys_to_pfn(ARMCTRL_BASE),
  3889. + .length = SZ_4K,
  3890. + .type = MT_DEVICE},
  3891. + {
  3892. + .virtual = IO_ADDRESS(UART0_BASE),
  3893. + .pfn = __phys_to_pfn(UART0_BASE),
  3894. + .length = SZ_4K,
  3895. + .type = MT_DEVICE},
  3896. + {
  3897. + .virtual = IO_ADDRESS(UART1_BASE),
  3898. + .pfn = __phys_to_pfn(UART1_BASE),
  3899. + .length = SZ_4K,
  3900. + .type = MT_DEVICE},
  3901. + {
  3902. + .virtual = IO_ADDRESS(DMA_BASE),
  3903. + .pfn = __phys_to_pfn(DMA_BASE),
  3904. + .length = SZ_4K,
  3905. + .type = MT_DEVICE},
  3906. + {
  3907. + .virtual = IO_ADDRESS(MCORE_BASE),
  3908. + .pfn = __phys_to_pfn(MCORE_BASE),
  3909. + .length = SZ_4K,
  3910. + .type = MT_DEVICE},
  3911. + {
  3912. + .virtual = IO_ADDRESS(ST_BASE),
  3913. + .pfn = __phys_to_pfn(ST_BASE),
  3914. + .length = SZ_4K,
  3915. + .type = MT_DEVICE},
  3916. + {
  3917. + .virtual = IO_ADDRESS(USB_BASE),
  3918. + .pfn = __phys_to_pfn(USB_BASE),
  3919. + .length = SZ_128K,
  3920. + .type = MT_DEVICE},
  3921. + {
  3922. + .virtual = IO_ADDRESS(PM_BASE),
  3923. + .pfn = __phys_to_pfn(PM_BASE),
  3924. + .length = SZ_4K,
  3925. + .type = MT_DEVICE},
  3926. + {
  3927. + .virtual = IO_ADDRESS(GPIO_BASE),
  3928. + .pfn = __phys_to_pfn(GPIO_BASE),
  3929. + .length = SZ_4K,
  3930. + .type = MT_DEVICE}
  3931. +};
  3932. +
  3933. +void __init bcm2708_map_io(void)
  3934. +{
  3935. + iotable_init(bcm2708_io_desc, ARRAY_SIZE(bcm2708_io_desc));
  3936. +}
  3937. +
  3938. +/* The STC is a free running counter that increments at the rate of 1MHz */
  3939. +#define STC_FREQ_HZ 1000000
  3940. +
  3941. +static inline uint32_t timer_read(void)
  3942. +{
  3943. + /* STC: a free running counter that increments at the rate of 1MHz */
  3944. + return readl(__io_address(ST_BASE + 0x04));
  3945. +}
  3946. +
  3947. +static unsigned long bcm2708_read_current_timer(void)
  3948. +{
  3949. + return timer_read();
  3950. +}
  3951. +
  3952. +static u64 notrace bcm2708_read_sched_clock(void)
  3953. +{
  3954. + return timer_read();
  3955. +}
  3956. +
  3957. +static cycle_t clksrc_read(struct clocksource *cs)
  3958. +{
  3959. + return timer_read();
  3960. +}
  3961. +
  3962. +static struct clocksource clocksource_stc = {
  3963. + .name = "stc",
  3964. + .rating = 300,
  3965. + .read = clksrc_read,
  3966. + .mask = CLOCKSOURCE_MASK(32),
  3967. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  3968. +};
  3969. +
  3970. +unsigned long frc_clock_ticks32(void)
  3971. +{
  3972. + return timer_read();
  3973. +}
  3974. +
  3975. +static void __init bcm2708_clocksource_init(void)
  3976. +{
  3977. + if (clocksource_register_hz(&clocksource_stc, STC_FREQ_HZ)) {
  3978. + printk(KERN_ERR "timer: failed to initialize clock "
  3979. + "source %s\n", clocksource_stc.name);
  3980. + }
  3981. +}
  3982. +
  3983. +struct clk __init *bcm2708_clk_register(const char *name, unsigned long fixed_rate)
  3984. +{
  3985. + struct clk *clk;
  3986. +
  3987. + clk = clk_register_fixed_rate(NULL, name, NULL, CLK_IS_ROOT,
  3988. + fixed_rate);
  3989. + if (IS_ERR(clk))
  3990. + pr_err("%s not registered\n", name);
  3991. +
  3992. + return clk;
  3993. +}
  3994. +
  3995. +void __init bcm2708_register_clkdev(struct clk *clk, const char *name)
  3996. +{
  3997. + int ret;
  3998. +
  3999. + ret = clk_register_clkdev(clk, NULL, name);
  4000. + if (ret)
  4001. + pr_err("%s alias not registered\n", name);
  4002. +}
  4003. +
  4004. +void __init bcm2708_init_clocks(void)
  4005. +{
  4006. + struct clk *clk;
  4007. +
  4008. + clk = bcm2708_clk_register("uart0_clk", uart_clock);
  4009. + bcm2708_register_clkdev(clk, "dev:f1");
  4010. +
  4011. + clk = bcm2708_clk_register("sdhost_clk", 250000000);
  4012. + bcm2708_register_clkdev(clk, "bcm2708_spi.0");
  4013. + bcm2708_register_clkdev(clk, "bcm2708_i2c.0");
  4014. + bcm2708_register_clkdev(clk, "bcm2708_i2c.1");
  4015. +}
  4016. +
  4017. +#define UART0_IRQ { IRQ_UART, 0 /*NO_IRQ*/ }
  4018. +#define UART0_DMA { 15, 14 }
  4019. +
  4020. +AMBA_DEVICE(uart0, "dev:f1", UART0, NULL);
  4021. +
  4022. +static struct amba_device *amba_devs[] __initdata = {
  4023. + &uart0_device,
  4024. +};
  4025. +
  4026. +static struct resource bcm2708_dmaman_resources[] = {
  4027. + {
  4028. + .start = DMA_BASE,
  4029. + .end = DMA_BASE + SZ_4K - 1,
  4030. + .flags = IORESOURCE_MEM,
  4031. + }
  4032. +};
  4033. +
  4034. +static struct platform_device bcm2708_dmaman_device = {
  4035. + .name = BCM_DMAMAN_DRIVER_NAME,
  4036. + .id = 0, /* first bcm2708_dma */
  4037. + .resource = bcm2708_dmaman_resources,
  4038. + .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  4039. +};
  4040. +
  4041. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  4042. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  4043. + .pin = W1_GPIO,
  4044. + .ext_pullup_enable_pin = W1_PULLUP,
  4045. + .is_open_drain = 0,
  4046. +};
  4047. +
  4048. +static struct platform_device w1_device = {
  4049. + .name = "w1-gpio",
  4050. + .id = -1,
  4051. + .dev.platform_data = &w1_gpio_pdata,
  4052. +};
  4053. +#endif
  4054. +
  4055. +static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4056. +
  4057. +static struct platform_device bcm2708_fb_device = {
  4058. + .name = "bcm2708_fb",
  4059. + .id = -1, /* only one bcm2708_fb */
  4060. + .resource = NULL,
  4061. + .num_resources = 0,
  4062. + .dev = {
  4063. + .dma_mask = &fb_dmamask,
  4064. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4065. + },
  4066. +};
  4067. +
  4068. +static struct plat_serial8250_port bcm2708_uart1_platform_data[] = {
  4069. + {
  4070. + .mapbase = UART1_BASE + 0x40,
  4071. + .irq = IRQ_AUX,
  4072. + .uartclk = 125000000,
  4073. + .regshift = 2,
  4074. + .iotype = UPIO_MEM,
  4075. + .flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_SKIP_TEST,
  4076. + .type = PORT_8250,
  4077. + },
  4078. + {},
  4079. +};
  4080. +
  4081. +static struct platform_device bcm2708_uart1_device = {
  4082. + .name = "serial8250",
  4083. + .id = PLAT8250_DEV_PLATFORM,
  4084. + .dev = {
  4085. + .platform_data = bcm2708_uart1_platform_data,
  4086. + },
  4087. +};
  4088. +
  4089. +static struct resource bcm2708_usb_resources[] = {
  4090. + [0] = {
  4091. + .start = USB_BASE,
  4092. + .end = USB_BASE + SZ_128K - 1,
  4093. + .flags = IORESOURCE_MEM,
  4094. + },
  4095. + [1] = {
  4096. + .start = MPHI_BASE,
  4097. + .end = MPHI_BASE + SZ_4K - 1,
  4098. + .flags = IORESOURCE_MEM,
  4099. + },
  4100. + [2] = {
  4101. + .start = IRQ_HOSTPORT,
  4102. + .end = IRQ_HOSTPORT,
  4103. + .flags = IORESOURCE_IRQ,
  4104. + },
  4105. + [3] = {
  4106. + .start = IRQ_USB,
  4107. + .end = IRQ_USB,
  4108. + .flags = IORESOURCE_IRQ,
  4109. + },
  4110. +};
  4111. +
  4112. +
  4113. +static u64 usb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4114. +
  4115. +static struct platform_device bcm2708_usb_device = {
  4116. + .name = "bcm2708_usb",
  4117. + .id = -1, /* only one bcm2708_usb */
  4118. + .resource = bcm2708_usb_resources,
  4119. + .num_resources = ARRAY_SIZE(bcm2708_usb_resources),
  4120. + .dev = {
  4121. + .dma_mask = &usb_dmamask,
  4122. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4123. + },
  4124. +};
  4125. +
  4126. +static struct resource bcm2708_vcio_resources[] = {
  4127. + [0] = { /* mailbox/semaphore/doorbell access */
  4128. + .start = MCORE_BASE,
  4129. + .end = MCORE_BASE + SZ_4K - 1,
  4130. + .flags = IORESOURCE_MEM,
  4131. + },
  4132. +};
  4133. +
  4134. +static u64 vcio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4135. +
  4136. +static struct platform_device bcm2708_vcio_device = {
  4137. + .name = BCM_VCIO_DRIVER_NAME,
  4138. + .id = -1, /* only one VideoCore I/O area */
  4139. + .resource = bcm2708_vcio_resources,
  4140. + .num_resources = ARRAY_SIZE(bcm2708_vcio_resources),
  4141. + .dev = {
  4142. + .dma_mask = &vcio_dmamask,
  4143. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4144. + },
  4145. +};
  4146. +
  4147. +#ifdef CONFIG_BCM2708_GPIO
  4148. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  4149. +
  4150. +static struct resource bcm2708_gpio_resources[] = {
  4151. + [0] = { /* general purpose I/O */
  4152. + .start = GPIO_BASE,
  4153. + .end = GPIO_BASE + SZ_4K - 1,
  4154. + .flags = IORESOURCE_MEM,
  4155. + },
  4156. +};
  4157. +
  4158. +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4159. +
  4160. +static struct platform_device bcm2708_gpio_device = {
  4161. + .name = BCM_GPIO_DRIVER_NAME,
  4162. + .id = -1, /* only one VideoCore I/O area */
  4163. + .resource = bcm2708_gpio_resources,
  4164. + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
  4165. + .dev = {
  4166. + .dma_mask = &gpio_dmamask,
  4167. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4168. + },
  4169. +};
  4170. +#endif
  4171. +
  4172. +static struct resource bcm2708_systemtimer_resources[] = {
  4173. + [0] = { /* system timer access */
  4174. + .start = ST_BASE,
  4175. + .end = ST_BASE + SZ_4K - 1,
  4176. + .flags = IORESOURCE_MEM,
  4177. + },
  4178. + {
  4179. + .start = IRQ_TIMER3,
  4180. + .end = IRQ_TIMER3,
  4181. + .flags = IORESOURCE_IRQ,
  4182. + }
  4183. +
  4184. +};
  4185. +
  4186. +static u64 systemtimer_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4187. +
  4188. +static struct platform_device bcm2708_systemtimer_device = {
  4189. + .name = "bcm2708_systemtimer",
  4190. + .id = -1, /* only one VideoCore I/O area */
  4191. + .resource = bcm2708_systemtimer_resources,
  4192. + .num_resources = ARRAY_SIZE(bcm2708_systemtimer_resources),
  4193. + .dev = {
  4194. + .dma_mask = &systemtimer_dmamask,
  4195. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4196. + },
  4197. +};
  4198. +
  4199. +#ifdef CONFIG_MMC_BCM2835 /* Arasan emmc SD (new) */
  4200. +static struct resource bcm2835_emmc_resources[] = {
  4201. + [0] = {
  4202. + .start = EMMC_BASE,
  4203. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  4204. + /* the memory map actually makes SZ_4K available */
  4205. + .flags = IORESOURCE_MEM,
  4206. + },
  4207. + [1] = {
  4208. + .start = IRQ_ARASANSDIO,
  4209. + .end = IRQ_ARASANSDIO,
  4210. + .flags = IORESOURCE_IRQ,
  4211. + },
  4212. +};
  4213. +
  4214. +static u64 bcm2835_emmc_dmamask = 0xffffffffUL;
  4215. +
  4216. +struct platform_device bcm2835_emmc_device = {
  4217. + .name = "mmc-bcm2835",
  4218. + .id = 0,
  4219. + .num_resources = ARRAY_SIZE(bcm2835_emmc_resources),
  4220. + .resource = bcm2835_emmc_resources,
  4221. + .dev = {
  4222. + .dma_mask = &bcm2835_emmc_dmamask,
  4223. + .coherent_dma_mask = 0xffffffffUL},
  4224. +};
  4225. +#endif /* CONFIG_MMC_BCM2835 */
  4226. +
  4227. +static struct resource bcm2708_powerman_resources[] = {
  4228. + [0] = {
  4229. + .start = PM_BASE,
  4230. + .end = PM_BASE + SZ_256 - 1,
  4231. + .flags = IORESOURCE_MEM,
  4232. + },
  4233. +};
  4234. +
  4235. +static u64 powerman_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4236. +
  4237. +struct platform_device bcm2708_powerman_device = {
  4238. + .name = "bcm2708_powerman",
  4239. + .id = 0,
  4240. + .num_resources = ARRAY_SIZE(bcm2708_powerman_resources),
  4241. + .resource = bcm2708_powerman_resources,
  4242. + .dev = {
  4243. + .dma_mask = &powerman_dmamask,
  4244. + .coherent_dma_mask = 0xffffffffUL},
  4245. +};
  4246. +
  4247. +
  4248. +static struct platform_device bcm2708_alsa_devices[] = {
  4249. + [0] = {
  4250. + .name = "bcm2835_AUD0",
  4251. + .id = 0, /* first audio device */
  4252. + .resource = 0,
  4253. + .num_resources = 0,
  4254. + },
  4255. + [1] = {
  4256. + .name = "bcm2835_AUD1",
  4257. + .id = 1, /* second audio device */
  4258. + .resource = 0,
  4259. + .num_resources = 0,
  4260. + },
  4261. + [2] = {
  4262. + .name = "bcm2835_AUD2",
  4263. + .id = 2, /* third audio device */
  4264. + .resource = 0,
  4265. + .num_resources = 0,
  4266. + },
  4267. + [3] = {
  4268. + .name = "bcm2835_AUD3",
  4269. + .id = 3, /* forth audio device */
  4270. + .resource = 0,
  4271. + .num_resources = 0,
  4272. + },
  4273. + [4] = {
  4274. + .name = "bcm2835_AUD4",
  4275. + .id = 4, /* fifth audio device */
  4276. + .resource = 0,
  4277. + .num_resources = 0,
  4278. + },
  4279. + [5] = {
  4280. + .name = "bcm2835_AUD5",
  4281. + .id = 5, /* sixth audio device */
  4282. + .resource = 0,
  4283. + .num_resources = 0,
  4284. + },
  4285. + [6] = {
  4286. + .name = "bcm2835_AUD6",
  4287. + .id = 6, /* seventh audio device */
  4288. + .resource = 0,
  4289. + .num_resources = 0,
  4290. + },
  4291. + [7] = {
  4292. + .name = "bcm2835_AUD7",
  4293. + .id = 7, /* eighth audio device */
  4294. + .resource = 0,
  4295. + .num_resources = 0,
  4296. + },
  4297. +};
  4298. +
  4299. +static struct resource bcm2708_spi_resources[] = {
  4300. + {
  4301. + .start = SPI0_BASE,
  4302. + .end = SPI0_BASE + SZ_256 - 1,
  4303. + .flags = IORESOURCE_MEM,
  4304. + }, {
  4305. + .start = IRQ_SPI,
  4306. + .end = IRQ_SPI,
  4307. + .flags = IORESOURCE_IRQ,
  4308. + }
  4309. +};
  4310. +
  4311. +
  4312. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4313. +static struct platform_device bcm2708_spi_device = {
  4314. + .name = "bcm2708_spi",
  4315. + .id = 0,
  4316. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  4317. + .resource = bcm2708_spi_resources,
  4318. + .dev = {
  4319. + .dma_mask = &bcm2708_spi_dmamask,
  4320. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  4321. +};
  4322. +
  4323. +#ifdef CONFIG_BCM2708_SPIDEV
  4324. +static struct spi_board_info bcm2708_spi_devices[] = {
  4325. +#ifdef CONFIG_SPI_SPIDEV
  4326. + {
  4327. + .modalias = "spidev",
  4328. + .max_speed_hz = 500000,
  4329. + .bus_num = 0,
  4330. + .chip_select = 0,
  4331. + .mode = SPI_MODE_0,
  4332. + }, {
  4333. + .modalias = "spidev",
  4334. + .max_speed_hz = 500000,
  4335. + .bus_num = 0,
  4336. + .chip_select = 1,
  4337. + .mode = SPI_MODE_0,
  4338. + }
  4339. +#endif
  4340. +};
  4341. +#endif
  4342. +
  4343. +static struct resource bcm2708_bsc0_resources[] = {
  4344. + {
  4345. + .start = BSC0_BASE,
  4346. + .end = BSC0_BASE + SZ_256 - 1,
  4347. + .flags = IORESOURCE_MEM,
  4348. + }, {
  4349. + .start = INTERRUPT_I2C,
  4350. + .end = INTERRUPT_I2C,
  4351. + .flags = IORESOURCE_IRQ,
  4352. + }
  4353. +};
  4354. +
  4355. +static struct platform_device bcm2708_bsc0_device = {
  4356. + .name = "bcm2708_i2c",
  4357. + .id = 0,
  4358. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  4359. + .resource = bcm2708_bsc0_resources,
  4360. +};
  4361. +
  4362. +
  4363. +static struct resource bcm2708_bsc1_resources[] = {
  4364. + {
  4365. + .start = BSC1_BASE,
  4366. + .end = BSC1_BASE + SZ_256 - 1,
  4367. + .flags = IORESOURCE_MEM,
  4368. + }, {
  4369. + .start = INTERRUPT_I2C,
  4370. + .end = INTERRUPT_I2C,
  4371. + .flags = IORESOURCE_IRQ,
  4372. + }
  4373. +};
  4374. +
  4375. +static struct platform_device bcm2708_bsc1_device = {
  4376. + .name = "bcm2708_i2c",
  4377. + .id = 1,
  4378. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  4379. + .resource = bcm2708_bsc1_resources,
  4380. +};
  4381. +
  4382. +static struct platform_device bcm2835_hwmon_device = {
  4383. + .name = "bcm2835_hwmon",
  4384. +};
  4385. +
  4386. +static struct platform_device bcm2835_thermal_device = {
  4387. + .name = "bcm2835_thermal",
  4388. +};
  4389. +
  4390. +#if defined(CONFIG_SND_BCM2708_SOC_I2S) || defined(CONFIG_SND_BCM2708_SOC_I2S_MODULE)
  4391. +static struct resource bcm2708_i2s_resources[] = {
  4392. + {
  4393. + .start = I2S_BASE,
  4394. + .end = I2S_BASE + 0x20,
  4395. + .flags = IORESOURCE_MEM,
  4396. + },
  4397. + {
  4398. + .start = PCM_CLOCK_BASE,
  4399. + .end = PCM_CLOCK_BASE + 0x02,
  4400. + .flags = IORESOURCE_MEM,
  4401. + }
  4402. +};
  4403. +
  4404. +static struct platform_device bcm2708_i2s_device = {
  4405. + .name = "bcm2708-i2s",
  4406. + .id = 0,
  4407. + .num_resources = ARRAY_SIZE(bcm2708_i2s_resources),
  4408. + .resource = bcm2708_i2s_resources,
  4409. +};
  4410. +#endif
  4411. +
  4412. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  4413. +static struct platform_device snd_hifiberry_dac_device = {
  4414. + .name = "snd-hifiberry-dac",
  4415. + .id = 0,
  4416. + .num_resources = 0,
  4417. +};
  4418. +
  4419. +static struct platform_device snd_pcm5102a_codec_device = {
  4420. + .name = "pcm5102a-codec",
  4421. + .id = -1,
  4422. + .num_resources = 0,
  4423. +};
  4424. +#endif
  4425. +
  4426. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS_MODULE)
  4427. +static struct platform_device snd_rpi_hifiberry_dacplus_device = {
  4428. + .name = "snd-rpi-hifiberry-dacplus",
  4429. + .id = 0,
  4430. + .num_resources = 0,
  4431. +};
  4432. +
  4433. +static struct i2c_board_info __initdata snd_pcm512x_hbdacplus_i2c_devices[] = {
  4434. + {
  4435. + I2C_BOARD_INFO("pcm5122", 0x4d)
  4436. + },
  4437. +};
  4438. +#endif
  4439. +
  4440. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  4441. +static struct platform_device snd_hifiberry_digi_device = {
  4442. + .name = "snd-hifiberry-digi",
  4443. + .id = 0,
  4444. + .num_resources = 0,
  4445. +};
  4446. +
  4447. +static struct i2c_board_info __initdata snd_wm8804_i2c_devices[] = {
  4448. + {
  4449. + I2C_BOARD_INFO("wm8804", 0x3b)
  4450. + },
  4451. +};
  4452. +
  4453. +#endif
  4454. +
  4455. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP_MODULE)
  4456. +static struct platform_device snd_hifiberry_amp_device = {
  4457. + .name = "snd-hifiberry-amp",
  4458. + .id = 0,
  4459. + .num_resources = 0,
  4460. +};
  4461. +
  4462. +static struct i2c_board_info __initdata snd_tas5713_i2c_devices[] = {
  4463. + {
  4464. + I2C_BOARD_INFO("tas5713", 0x1b)
  4465. + },
  4466. +};
  4467. +#endif
  4468. +
  4469. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  4470. +static struct platform_device snd_rpi_dac_device = {
  4471. + .name = "snd-rpi-dac",
  4472. + .id = 0,
  4473. + .num_resources = 0,
  4474. +};
  4475. +
  4476. +static struct platform_device snd_pcm1794a_codec_device = {
  4477. + .name = "pcm1794a-codec",
  4478. + .id = -1,
  4479. + .num_resources = 0,
  4480. +};
  4481. +#endif
  4482. +
  4483. +
  4484. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  4485. +static struct platform_device snd_rpi_iqaudio_dac_device = {
  4486. + .name = "snd-rpi-iqaudio-dac",
  4487. + .id = 0,
  4488. + .num_resources = 0,
  4489. +};
  4490. +
  4491. +// Use the actual device name rather than generic driver name
  4492. +static struct i2c_board_info __initdata snd_pcm512x_i2c_devices[] = {
  4493. + {
  4494. + I2C_BOARD_INFO("pcm5122", 0x4c)
  4495. + },
  4496. +};
  4497. +#endif
  4498. +
  4499. +int __init bcm_register_device(struct platform_device *pdev)
  4500. +{
  4501. + int ret;
  4502. +
  4503. + ret = platform_device_register(pdev);
  4504. + if (ret)
  4505. + pr_debug("Unable to register platform device '%s': %d\n",
  4506. + pdev->name, ret);
  4507. +
  4508. + return ret;
  4509. +}
  4510. +
  4511. +/*
  4512. + * Use these macros for platform and i2c devices that are present in the
  4513. + * Device Tree. This way the devices are only added on non-DT systems.
  4514. + */
  4515. +#define bcm_register_device_dt(pdev) \
  4516. + if (!use_dt) bcm_register_device(pdev)
  4517. +
  4518. +#define i2c_register_board_info_dt(busnum, info, n) \
  4519. + if (!use_dt) i2c_register_board_info(busnum, info, n)
  4520. +
  4521. +int calc_rsts(int partition)
  4522. +{
  4523. + return PM_PASSWORD |
  4524. + ((partition & (1 << 0)) << 0) |
  4525. + ((partition & (1 << 1)) << 1) |
  4526. + ((partition & (1 << 2)) << 2) |
  4527. + ((partition & (1 << 3)) << 3) |
  4528. + ((partition & (1 << 4)) << 4) |
  4529. + ((partition & (1 << 5)) << 5);
  4530. +}
  4531. +
  4532. +static void bcm2708_restart(enum reboot_mode mode, const char *cmd)
  4533. +{
  4534. + extern char bcm2708_reboot_mode;
  4535. + uint32_t pm_rstc, pm_wdog;
  4536. + uint32_t timeout = 10;
  4537. + uint32_t pm_rsts = 0;
  4538. +
  4539. + if(bcm2708_reboot_mode == 'q')
  4540. + {
  4541. + // NOOBS < 1.3 booting with reboot=q
  4542. + pm_rsts = readl(__io_address(PM_RSTS));
  4543. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRQ_SET;
  4544. + }
  4545. + else if(bcm2708_reboot_mode == 'p')
  4546. + {
  4547. + // NOOBS < 1.3 halting
  4548. + pm_rsts = readl(__io_address(PM_RSTS));
  4549. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRH_SET;
  4550. + }
  4551. + else
  4552. + {
  4553. + pm_rsts = calc_rsts(reboot_part);
  4554. + }
  4555. +
  4556. + writel(pm_rsts, __io_address(PM_RSTS));
  4557. +
  4558. + /* Setup watchdog for reset */
  4559. + pm_rstc = readl(__io_address(PM_RSTC));
  4560. +
  4561. + pm_wdog = PM_PASSWORD | (timeout & PM_WDOG_TIME_SET); // watchdog timer = timer clock / 16; need password (31:16) + value (11:0)
  4562. + pm_rstc = PM_PASSWORD | (pm_rstc & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  4563. +
  4564. + writel(pm_wdog, __io_address(PM_WDOG));
  4565. + writel(pm_rstc, __io_address(PM_RSTC));
  4566. +}
  4567. +
  4568. +/* 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 */
  4569. +static void bcm2708_power_off(void)
  4570. +{
  4571. + extern char bcm2708_reboot_mode;
  4572. + if(bcm2708_reboot_mode == 'q')
  4573. + {
  4574. + // NOOBS < v1.3
  4575. + bcm2708_restart('p', "");
  4576. + }
  4577. + else
  4578. + {
  4579. + /* partition 63 is special code for HALT the bootloader knows not to boot*/
  4580. + reboot_part = 63;
  4581. + /* continue with normal reset mechanism */
  4582. + bcm2708_restart(0, "");
  4583. + }
  4584. +}
  4585. +
  4586. +#ifdef CONFIG_OF
  4587. +static void __init bcm2708_dt_init(void)
  4588. +{
  4589. + int ret;
  4590. +
  4591. + of_clk_init(NULL);
  4592. + ret = of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  4593. + if (ret) {
  4594. + pr_err("of_platform_populate failed: %d\n", ret);
  4595. + /* Proceed as if CONFIG_OF was not defined */
  4596. + } else {
  4597. + use_dt = 1;
  4598. + }
  4599. +}
  4600. +#else
  4601. +static void __init bcm2708_dt_init(void) { }
  4602. +#endif /* CONFIG_OF */
  4603. +
  4604. +void __init bcm2708_init(void)
  4605. +{
  4606. + int i;
  4607. +
  4608. +#if defined(CONFIG_BCM_VC_CMA)
  4609. + vc_cma_early_init();
  4610. +#endif
  4611. + printk("bcm2708.uart_clock = %d\n", uart_clock);
  4612. + pm_power_off = bcm2708_power_off;
  4613. +
  4614. + bcm2708_init_clocks();
  4615. + bcm2708_dt_init();
  4616. +
  4617. + bcm_register_device(&bcm2708_dmaman_device);
  4618. + bcm_register_device(&bcm2708_vcio_device);
  4619. +#ifdef CONFIG_BCM2708_GPIO
  4620. + bcm_register_device(&bcm2708_gpio_device);
  4621. +#endif
  4622. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  4623. + w1_gpio_pdata.pin = w1_gpio_pin;
  4624. + w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
  4625. + bcm_register_device_dt(&w1_device);
  4626. +#endif
  4627. + bcm_register_device(&bcm2708_systemtimer_device);
  4628. + bcm_register_device(&bcm2708_fb_device);
  4629. + bcm_register_device(&bcm2708_usb_device);
  4630. + bcm_register_device(&bcm2708_uart1_device);
  4631. + bcm_register_device(&bcm2708_powerman_device);
  4632. +
  4633. +#ifdef CONFIG_MMC_BCM2835
  4634. + bcm_register_device(&bcm2835_emmc_device);
  4635. +#endif
  4636. + bcm2708_init_led();
  4637. + for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  4638. + bcm_register_device(&bcm2708_alsa_devices[i]);
  4639. +
  4640. + bcm_register_device(&bcm2835_hwmon_device);
  4641. + bcm_register_device(&bcm2835_thermal_device);
  4642. +
  4643. + bcm_register_device_dt(&bcm2708_spi_device);
  4644. + bcm_register_device_dt(&bcm2708_bsc0_device);
  4645. + bcm_register_device_dt(&bcm2708_bsc1_device);
  4646. +
  4647. +#if defined(CONFIG_SND_BCM2708_SOC_I2S) || defined(CONFIG_SND_BCM2708_SOC_I2S_MODULE)
  4648. + bcm_register_device_dt(&bcm2708_i2s_device);
  4649. +#endif
  4650. +
  4651. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  4652. + bcm_register_device_dt(&snd_hifiberry_dac_device);
  4653. + bcm_register_device_dt(&snd_pcm5102a_codec_device);
  4654. +#endif
  4655. +
  4656. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS_MODULE)
  4657. + bcm_register_device_dt(&snd_rpi_hifiberry_dacplus_device);
  4658. + i2c_register_board_info_dt(1, snd_pcm512x_hbdacplus_i2c_devices, ARRAY_SIZE(snd_pcm512x_hbdacplus_i2c_devices));
  4659. +#endif
  4660. +
  4661. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  4662. + bcm_register_device_dt(&snd_hifiberry_digi_device);
  4663. + i2c_register_board_info_dt(1, snd_wm8804_i2c_devices, ARRAY_SIZE(snd_wm8804_i2c_devices));
  4664. +#endif
  4665. +
  4666. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP_MODULE)
  4667. + bcm_register_device_dt(&snd_hifiberry_amp_device);
  4668. + i2c_register_board_info_dt(1, snd_tas5713_i2c_devices, ARRAY_SIZE(snd_tas5713_i2c_devices));
  4669. +#endif
  4670. +
  4671. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  4672. + bcm_register_device_dt(&snd_rpi_dac_device);
  4673. + bcm_register_device_dt(&snd_pcm1794a_codec_device);
  4674. +#endif
  4675. +
  4676. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  4677. + bcm_register_device_dt(&snd_rpi_iqaudio_dac_device);
  4678. + i2c_register_board_info_dt(1, snd_pcm512x_i2c_devices, ARRAY_SIZE(snd_pcm512x_i2c_devices));
  4679. +#endif
  4680. +
  4681. +
  4682. + for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  4683. + struct amba_device *d = amba_devs[i];
  4684. + amba_device_register(d, &iomem_resource);
  4685. + }
  4686. + system_rev = boardrev;
  4687. + system_serial_low = serial;
  4688. +
  4689. +#ifdef CONFIG_BCM2708_SPIDEV
  4690. + spi_register_board_info(bcm2708_spi_devices,
  4691. + ARRAY_SIZE(bcm2708_spi_devices));
  4692. +#endif
  4693. +}
  4694. +
  4695. +static void timer_set_mode(enum clock_event_mode mode,
  4696. + struct clock_event_device *clk)
  4697. +{
  4698. + switch (mode) {
  4699. + case CLOCK_EVT_MODE_ONESHOT: /* Leave the timer disabled, .set_next_event will enable it */
  4700. + case CLOCK_EVT_MODE_SHUTDOWN:
  4701. + break;
  4702. + case CLOCK_EVT_MODE_PERIODIC:
  4703. +
  4704. + case CLOCK_EVT_MODE_UNUSED:
  4705. + case CLOCK_EVT_MODE_RESUME:
  4706. +
  4707. + default:
  4708. + printk(KERN_ERR "timer_set_mode: unhandled mode:%d\n",
  4709. + (int)mode);
  4710. + break;
  4711. + }
  4712. +
  4713. +}
  4714. +
  4715. +static int timer_set_next_event(unsigned long cycles,
  4716. + struct clock_event_device *unused)
  4717. +{
  4718. + unsigned long stc;
  4719. + do {
  4720. + stc = readl(__io_address(ST_BASE + 0x04));
  4721. + /* We could take a FIQ here, which may push ST above STC3 */
  4722. + writel(stc + cycles, __io_address(ST_BASE + 0x18));
  4723. + } while ((signed long) cycles >= 0 &&
  4724. + (signed long) (readl(__io_address(ST_BASE + 0x04)) - stc)
  4725. + >= (signed long) cycles);
  4726. + return 0;
  4727. +}
  4728. +
  4729. +static struct clock_event_device timer0_clockevent = {
  4730. + .name = "timer0",
  4731. + .shift = 32,
  4732. + .features = CLOCK_EVT_FEAT_ONESHOT,
  4733. + .set_mode = timer_set_mode,
  4734. + .set_next_event = timer_set_next_event,
  4735. +};
  4736. +
  4737. +/*
  4738. + * IRQ handler for the timer
  4739. + */
  4740. +static irqreturn_t bcm2708_timer_interrupt(int irq, void *dev_id)
  4741. +{
  4742. + struct clock_event_device *evt = &timer0_clockevent;
  4743. +
  4744. + writel(1 << 3, __io_address(ST_BASE + 0x00)); /* stcs clear timer int */
  4745. +
  4746. + evt->event_handler(evt);
  4747. +
  4748. + return IRQ_HANDLED;
  4749. +}
  4750. +
  4751. +static struct irqaction bcm2708_timer_irq = {
  4752. + .name = "BCM2708 Timer Tick",
  4753. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  4754. + .handler = bcm2708_timer_interrupt,
  4755. +};
  4756. +
  4757. +/*
  4758. + * Set up timer interrupt, and return the current time in seconds.
  4759. + */
  4760. +
  4761. +static struct delay_timer bcm2708_delay_timer = {
  4762. + .read_current_timer = bcm2708_read_current_timer,
  4763. + .freq = STC_FREQ_HZ,
  4764. +};
  4765. +
  4766. +static void __init bcm2708_timer_init(void)
  4767. +{
  4768. + /* init high res timer */
  4769. + bcm2708_clocksource_init();
  4770. +
  4771. + /*
  4772. + * Initialise to a known state (all timers off)
  4773. + */
  4774. + writel(0, __io_address(ARM_T_CONTROL));
  4775. + /*
  4776. + * Make irqs happen for the system timer
  4777. + */
  4778. + setup_irq(IRQ_TIMER3, &bcm2708_timer_irq);
  4779. +
  4780. + sched_clock_register(bcm2708_read_sched_clock, 32, STC_FREQ_HZ);
  4781. +
  4782. + timer0_clockevent.mult =
  4783. + div_sc(STC_FREQ_HZ, NSEC_PER_SEC, timer0_clockevent.shift);
  4784. + timer0_clockevent.max_delta_ns =
  4785. + clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  4786. + timer0_clockevent.min_delta_ns =
  4787. + clockevent_delta2ns(0xf, &timer0_clockevent);
  4788. +
  4789. + timer0_clockevent.cpumask = cpumask_of(0);
  4790. + clockevents_register_device(&timer0_clockevent);
  4791. +
  4792. + register_current_timer_delay(&bcm2708_delay_timer);
  4793. +}
  4794. +
  4795. +#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  4796. +#include <linux/leds.h>
  4797. +
  4798. +static struct gpio_led bcm2708_leds[] = {
  4799. + [0] = {
  4800. + .gpio = 16,
  4801. + .name = "led0",
  4802. + .default_trigger = "mmc0",
  4803. + .active_low = 1,
  4804. + },
  4805. +};
  4806. +
  4807. +static struct gpio_led_platform_data bcm2708_led_pdata = {
  4808. + .num_leds = ARRAY_SIZE(bcm2708_leds),
  4809. + .leds = bcm2708_leds,
  4810. +};
  4811. +
  4812. +static struct platform_device bcm2708_led_device = {
  4813. + .name = "leds-gpio",
  4814. + .id = -1,
  4815. + .dev = {
  4816. + .platform_data = &bcm2708_led_pdata,
  4817. + },
  4818. +};
  4819. +
  4820. +static void __init bcm2708_init_led(void)
  4821. +{
  4822. + bcm2708_leds[0].gpio = disk_led_gpio;
  4823. + bcm2708_leds[0].active_low = disk_led_active_low;
  4824. + bcm_register_device_dt(&bcm2708_led_device);
  4825. +}
  4826. +#else
  4827. +static inline void bcm2708_init_led(void)
  4828. +{
  4829. +}
  4830. +#endif
  4831. +
  4832. +void __init bcm2708_init_early(void)
  4833. +{
  4834. + /*
  4835. + * Some devices allocate their coherent buffers from atomic
  4836. + * context. Increase size of atomic coherent pool to make sure such
  4837. + * the allocations won't fail.
  4838. + */
  4839. + init_dma_coherent_pool_size(SZ_4M);
  4840. +}
  4841. +
  4842. +static void __init board_reserve(void)
  4843. +{
  4844. +#if defined(CONFIG_BCM_VC_CMA)
  4845. + vc_cma_reserve();
  4846. +#endif
  4847. +}
  4848. +
  4849. +static const char * const bcm2708_compat[] = {
  4850. + "brcm,bcm2708",
  4851. + NULL
  4852. +};
  4853. +
  4854. +MACHINE_START(BCM2708, "BCM2708")
  4855. + /* Maintainer: Broadcom Europe Ltd. */
  4856. + .map_io = bcm2708_map_io,
  4857. + .init_irq = bcm2708_init_irq,
  4858. + .init_time = bcm2708_timer_init,
  4859. + .init_machine = bcm2708_init,
  4860. + .init_early = bcm2708_init_early,
  4861. + .reserve = board_reserve,
  4862. + .restart = bcm2708_restart,
  4863. + .dt_compat = bcm2708_compat,
  4864. +MACHINE_END
  4865. +
  4866. +module_param(boardrev, uint, 0644);
  4867. +module_param(serial, uint, 0644);
  4868. +module_param(uart_clock, uint, 0644);
  4869. +module_param(disk_led_gpio, uint, 0644);
  4870. +module_param(disk_led_active_low, uint, 0644);
  4871. +module_param(reboot_part, uint, 0644);
  4872. +module_param(w1_gpio_pin, uint, 0644);
  4873. +module_param(w1_gpio_pullup, uint, 0644);
  4874. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/bcm2708_gpio.c linux-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c
  4875. --- linux-3.17.5/arch/arm/mach-bcm2708/bcm2708_gpio.c 1969-12-31 18:00:00.000000000 -0600
  4876. +++ linux-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c 2014-12-11 14:02:51.516418001 -0600
  4877. @@ -0,0 +1,401 @@
  4878. +/*
  4879. + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
  4880. + *
  4881. + * Copyright (C) 2010 Broadcom
  4882. + *
  4883. + * This program is free software; you can redistribute it and/or modify
  4884. + * it under the terms of the GNU General Public License version 2 as
  4885. + * published by the Free Software Foundation.
  4886. + *
  4887. + */
  4888. +
  4889. +#include <linux/spinlock.h>
  4890. +#include <linux/module.h>
  4891. +#include <linux/delay.h>
  4892. +#include <linux/list.h>
  4893. +#include <linux/io.h>
  4894. +#include <linux/irq.h>
  4895. +#include <linux/interrupt.h>
  4896. +#include <linux/slab.h>
  4897. +#include <mach/gpio.h>
  4898. +#include <linux/gpio.h>
  4899. +#include <linux/platform_device.h>
  4900. +#include <mach/platform.h>
  4901. +
  4902. +#include <linux/platform_data/bcm2708.h>
  4903. +
  4904. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  4905. +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
  4906. +#define BCM_GPIO_USE_IRQ 1
  4907. +
  4908. +#define GPIOFSEL(x) (0x00+(x)*4)
  4909. +#define GPIOSET(x) (0x1c+(x)*4)
  4910. +#define GPIOCLR(x) (0x28+(x)*4)
  4911. +#define GPIOLEV(x) (0x34+(x)*4)
  4912. +#define GPIOEDS(x) (0x40+(x)*4)
  4913. +#define GPIOREN(x) (0x4c+(x)*4)
  4914. +#define GPIOFEN(x) (0x58+(x)*4)
  4915. +#define GPIOHEN(x) (0x64+(x)*4)
  4916. +#define GPIOLEN(x) (0x70+(x)*4)
  4917. +#define GPIOAREN(x) (0x7c+(x)*4)
  4918. +#define GPIOAFEN(x) (0x88+(x)*4)
  4919. +#define GPIOUD(x) (0x94+(x)*4)
  4920. +#define GPIOUDCLK(x) (0x98+(x)*4)
  4921. +
  4922. +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
  4923. + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
  4924. + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
  4925. + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
  4926. +};
  4927. +
  4928. + /* Each of the two spinlocks protects a different set of hardware
  4929. + * regiters and data structurs. This decouples the code of the IRQ from
  4930. + * the GPIO code. This also makes the case of a GPIO routine call from
  4931. + * the IRQ code simpler.
  4932. + */
  4933. +static DEFINE_SPINLOCK(lock); /* GPIO registers */
  4934. +
  4935. +struct bcm2708_gpio {
  4936. + struct list_head list;
  4937. + void __iomem *base;
  4938. + struct gpio_chip gc;
  4939. + unsigned long rising[(BCM2708_NR_GPIOS + 31) / 32];
  4940. + unsigned long falling[(BCM2708_NR_GPIOS + 31) / 32];
  4941. + unsigned long high[(BCM2708_NR_GPIOS + 31) / 32];
  4942. + unsigned long low[(BCM2708_NR_GPIOS + 31) / 32];
  4943. +};
  4944. +
  4945. +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
  4946. + int function)
  4947. +{
  4948. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  4949. + unsigned long flags;
  4950. + unsigned gpiodir;
  4951. + unsigned gpio_bank = offset / 10;
  4952. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  4953. +
  4954. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
  4955. + if (offset >= BCM2708_NR_GPIOS)
  4956. + return -EINVAL;
  4957. +
  4958. + spin_lock_irqsave(&lock, flags);
  4959. +
  4960. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  4961. + gpiodir &= ~(7 << gpio_field_offset);
  4962. + gpiodir |= function << gpio_field_offset;
  4963. + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
  4964. + spin_unlock_irqrestore(&lock, flags);
  4965. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  4966. +
  4967. + return 0;
  4968. +}
  4969. +
  4970. +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
  4971. +{
  4972. + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
  4973. +}
  4974. +
  4975. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  4976. +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
  4977. + int value)
  4978. +{
  4979. + int ret;
  4980. + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
  4981. + if (ret >= 0)
  4982. + bcm2708_gpio_set(gc, offset, value);
  4983. + return ret;
  4984. +}
  4985. +
  4986. +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
  4987. +{
  4988. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  4989. + unsigned gpio_bank = offset / 32;
  4990. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  4991. + unsigned lev;
  4992. +
  4993. + if (offset >= BCM2708_NR_GPIOS)
  4994. + return 0;
  4995. + lev = readl(gpio->base + GPIOLEV(gpio_bank));
  4996. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
  4997. + return 0x1 & (lev >> gpio_field_offset);
  4998. +}
  4999. +
  5000. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  5001. +{
  5002. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  5003. + unsigned gpio_bank = offset / 32;
  5004. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  5005. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
  5006. + if (offset >= BCM2708_NR_GPIOS)
  5007. + return;
  5008. + if (value)
  5009. + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
  5010. + else
  5011. + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
  5012. +}
  5013. +
  5014. +/**********************
  5015. + * extension to configure pullups
  5016. + */
  5017. +int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
  5018. + bcm2708_gpio_pull_t value)
  5019. +{
  5020. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  5021. + unsigned gpio_bank = offset / 32;
  5022. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  5023. +
  5024. + if (offset >= BCM2708_NR_GPIOS)
  5025. + return -EINVAL;
  5026. +
  5027. + switch (value) {
  5028. + case BCM2708_PULL_UP:
  5029. + writel(2, gpio->base + GPIOUD(0));
  5030. + break;
  5031. + case BCM2708_PULL_DOWN:
  5032. + writel(1, gpio->base + GPIOUD(0));
  5033. + break;
  5034. + case BCM2708_PULL_OFF:
  5035. + writel(0, gpio->base + GPIOUD(0));
  5036. + break;
  5037. + }
  5038. +
  5039. + udelay(5);
  5040. + writel(1 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
  5041. + udelay(5);
  5042. + writel(0, gpio->base + GPIOUD(0));
  5043. + writel(0 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
  5044. +
  5045. + return 0;
  5046. +}
  5047. +EXPORT_SYMBOL(bcm2708_gpio_setpull);
  5048. +
  5049. +/*************************************************************************************************************************
  5050. + * bcm2708 GPIO IRQ
  5051. + */
  5052. +
  5053. +#if BCM_GPIO_USE_IRQ
  5054. +
  5055. +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  5056. +{
  5057. + return gpio_to_irq(gpio);
  5058. +}
  5059. +
  5060. +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
  5061. +{
  5062. + unsigned irq = d->irq;
  5063. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  5064. + unsigned gn = irq_to_gpio(irq);
  5065. + unsigned gb = gn / 32;
  5066. + unsigned go = gn % 32;
  5067. +
  5068. + gpio->rising[gb] &= ~(1 << go);
  5069. + gpio->falling[gb] &= ~(1 << go);
  5070. + gpio->high[gb] &= ~(1 << go);
  5071. + gpio->low[gb] &= ~(1 << go);
  5072. +
  5073. + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  5074. + return -EINVAL;
  5075. +
  5076. + if (type & IRQ_TYPE_EDGE_RISING)
  5077. + gpio->rising[gb] |= (1 << go);
  5078. + if (type & IRQ_TYPE_EDGE_FALLING)
  5079. + gpio->falling[gb] |= (1 << go);
  5080. + if (type & IRQ_TYPE_LEVEL_HIGH)
  5081. + gpio->high[gb] |= (1 << go);
  5082. + if (type & IRQ_TYPE_LEVEL_LOW)
  5083. + gpio->low[gb] |= (1 << go);
  5084. + return 0;
  5085. +}
  5086. +
  5087. +static void bcm2708_gpio_irq_mask(struct irq_data *d)
  5088. +{
  5089. + unsigned irq = d->irq;
  5090. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  5091. + unsigned gn = irq_to_gpio(irq);
  5092. + unsigned gb = gn / 32;
  5093. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  5094. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  5095. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  5096. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  5097. +
  5098. + gn = gn % 32;
  5099. +
  5100. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  5101. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  5102. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  5103. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  5104. +}
  5105. +
  5106. +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
  5107. +{
  5108. + unsigned irq = d->irq;
  5109. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  5110. + unsigned gn = irq_to_gpio(irq);
  5111. + unsigned gb = gn / 32;
  5112. + unsigned go = gn % 32;
  5113. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  5114. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  5115. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  5116. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  5117. +
  5118. + writel(1 << go, gpio->base + GPIOEDS(gb));
  5119. +
  5120. + if (gpio->rising[gb] & (1 << go)) {
  5121. + writel(rising | (1 << go), gpio->base + GPIOREN(gb));
  5122. + } else {
  5123. + writel(rising & ~(1 << go), gpio->base + GPIOREN(gb));
  5124. + }
  5125. +
  5126. + if (gpio->falling[gb] & (1 << go)) {
  5127. + writel(falling | (1 << go), gpio->base + GPIOFEN(gb));
  5128. + } else {
  5129. + writel(falling & ~(1 << go), gpio->base + GPIOFEN(gb));
  5130. + }
  5131. +
  5132. + if (gpio->high[gb] & (1 << go)) {
  5133. + writel(high | (1 << go), gpio->base + GPIOHEN(gb));
  5134. + } else {
  5135. + writel(high & ~(1 << go), gpio->base + GPIOHEN(gb));
  5136. + }
  5137. +
  5138. + if (gpio->low[gb] & (1 << go)) {
  5139. + writel(low | (1 << go), gpio->base + GPIOLEN(gb));
  5140. + } else {
  5141. + writel(low & ~(1 << go), gpio->base + GPIOLEN(gb));
  5142. + }
  5143. +}
  5144. +
  5145. +static struct irq_chip bcm2708_irqchip = {
  5146. + .name = "GPIO",
  5147. + .irq_enable = bcm2708_gpio_irq_unmask,
  5148. + .irq_disable = bcm2708_gpio_irq_mask,
  5149. + .irq_unmask = bcm2708_gpio_irq_unmask,
  5150. + .irq_mask = bcm2708_gpio_irq_mask,
  5151. + .irq_set_type = bcm2708_gpio_irq_set_type,
  5152. +};
  5153. +
  5154. +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
  5155. +{
  5156. + unsigned long edsr;
  5157. + unsigned bank;
  5158. + int i;
  5159. + unsigned gpio;
  5160. + for (bank = 0; bank <= 1; bank++) {
  5161. + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
  5162. + for_each_set_bit(i, &edsr, 32) {
  5163. + gpio = i + bank * 32;
  5164. + generic_handle_irq(gpio_to_irq(gpio));
  5165. + }
  5166. + writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
  5167. + }
  5168. + return IRQ_HANDLED;
  5169. +}
  5170. +
  5171. +static struct irqaction bcm2708_gpio_irq = {
  5172. + .name = "BCM2708 GPIO catchall handler",
  5173. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  5174. + .handler = bcm2708_gpio_interrupt,
  5175. +};
  5176. +
  5177. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  5178. +{
  5179. + unsigned irq;
  5180. +
  5181. + ucb->gc.to_irq = bcm2708_gpio_to_irq;
  5182. +
  5183. + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
  5184. + irq_set_chip_data(irq, ucb);
  5185. + irq_set_chip(irq, &bcm2708_irqchip);
  5186. + set_irq_flags(irq, IRQF_VALID);
  5187. + }
  5188. + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
  5189. +}
  5190. +
  5191. +#else
  5192. +
  5193. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  5194. +{
  5195. +}
  5196. +
  5197. +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
  5198. +
  5199. +static int bcm2708_gpio_probe(struct platform_device *dev)
  5200. +{
  5201. + struct bcm2708_gpio *ucb;
  5202. + struct resource *res;
  5203. + int err = 0;
  5204. +
  5205. + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
  5206. +
  5207. + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
  5208. + if (NULL == ucb) {
  5209. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  5210. + "mailbox memory\n");
  5211. + err = -ENOMEM;
  5212. + goto err;
  5213. + }
  5214. +
  5215. + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  5216. +
  5217. + platform_set_drvdata(dev, ucb);
  5218. + ucb->base = __io_address(GPIO_BASE);
  5219. +
  5220. + ucb->gc.label = "bcm2708_gpio";
  5221. + ucb->gc.base = 0;
  5222. + ucb->gc.ngpio = BCM2708_NR_GPIOS;
  5223. + ucb->gc.owner = THIS_MODULE;
  5224. +
  5225. + ucb->gc.direction_input = bcm2708_gpio_dir_in;
  5226. + ucb->gc.direction_output = bcm2708_gpio_dir_out;
  5227. + ucb->gc.get = bcm2708_gpio_get;
  5228. + ucb->gc.set = bcm2708_gpio_set;
  5229. + ucb->gc.can_sleep = 0;
  5230. +
  5231. + bcm2708_gpio_irq_init(ucb);
  5232. +
  5233. + err = gpiochip_add(&ucb->gc);
  5234. + if (err)
  5235. + goto err;
  5236. +
  5237. +err:
  5238. + return err;
  5239. +
  5240. +}
  5241. +
  5242. +static int bcm2708_gpio_remove(struct platform_device *dev)
  5243. +{
  5244. + int err = 0;
  5245. + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
  5246. +
  5247. + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
  5248. +
  5249. + err = gpiochip_remove(&ucb->gc);
  5250. +
  5251. + platform_set_drvdata(dev, NULL);
  5252. + kfree(ucb);
  5253. +
  5254. + return err;
  5255. +}
  5256. +
  5257. +static struct platform_driver bcm2708_gpio_driver = {
  5258. + .probe = bcm2708_gpio_probe,
  5259. + .remove = bcm2708_gpio_remove,
  5260. + .driver = {
  5261. + .name = "bcm2708_gpio"},
  5262. +};
  5263. +
  5264. +static int __init bcm2708_gpio_init(void)
  5265. +{
  5266. + return platform_driver_register(&bcm2708_gpio_driver);
  5267. +}
  5268. +
  5269. +static void __exit bcm2708_gpio_exit(void)
  5270. +{
  5271. + platform_driver_unregister(&bcm2708_gpio_driver);
  5272. +}
  5273. +
  5274. +module_init(bcm2708_gpio_init);
  5275. +module_exit(bcm2708_gpio_exit);
  5276. +
  5277. +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
  5278. +MODULE_LICENSE("GPL");
  5279. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/bcm2708.h linux-rpi/arch/arm/mach-bcm2708/bcm2708.h
  5280. --- linux-3.17.5/arch/arm/mach-bcm2708/bcm2708.h 1969-12-31 18:00:00.000000000 -0600
  5281. +++ linux-rpi/arch/arm/mach-bcm2708/bcm2708.h 2014-12-11 14:02:51.516418001 -0600
  5282. @@ -0,0 +1,49 @@
  5283. +/*
  5284. + * linux/arch/arm/mach-bcm2708/bcm2708.h
  5285. + *
  5286. + * BCM2708 machine support header
  5287. + *
  5288. + * Copyright (C) 2010 Broadcom
  5289. + *
  5290. + * This program is free software; you can redistribute it and/or modify
  5291. + * it under the terms of the GNU General Public License as published by
  5292. + * the Free Software Foundation; either version 2 of the License, or
  5293. + * (at your option) any later version.
  5294. + *
  5295. + * This program is distributed in the hope that it will be useful,
  5296. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5297. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5298. + * GNU General Public License for more details.
  5299. + *
  5300. + * You should have received a copy of the GNU General Public License
  5301. + * along with this program; if not, write to the Free Software
  5302. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5303. + */
  5304. +
  5305. +#ifndef __BCM2708_BCM2708_H
  5306. +#define __BCM2708_BCM2708_H
  5307. +
  5308. +#include <linux/amba/bus.h>
  5309. +
  5310. +extern void __init bcm2708_init(void);
  5311. +extern void __init bcm2708_init_irq(void);
  5312. +extern void __init bcm2708_map_io(void);
  5313. +extern struct sys_timer bcm2708_timer;
  5314. +extern unsigned int mmc_status(struct device *dev);
  5315. +
  5316. +#define AMBA_DEVICE(name, busid, base, plat) \
  5317. +static struct amba_device name##_device = { \
  5318. + .dev = { \
  5319. + .coherent_dma_mask = ~0, \
  5320. + .init_name = busid, \
  5321. + .platform_data = plat, \
  5322. + }, \
  5323. + .res = { \
  5324. + .start = base##_BASE, \
  5325. + .end = (base##_BASE) + SZ_4K - 1,\
  5326. + .flags = IORESOURCE_MEM, \
  5327. + }, \
  5328. + .irq = base##_IRQ, \
  5329. +}
  5330. +
  5331. +#endif
  5332. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/dma.c linux-rpi/arch/arm/mach-bcm2708/dma.c
  5333. --- linux-3.17.5/arch/arm/mach-bcm2708/dma.c 1969-12-31 18:00:00.000000000 -0600
  5334. +++ linux-rpi/arch/arm/mach-bcm2708/dma.c 2014-12-11 14:02:51.516418001 -0600
  5335. @@ -0,0 +1,409 @@
  5336. +/*
  5337. + * linux/arch/arm/mach-bcm2708/dma.c
  5338. + *
  5339. + * Copyright (C) 2010 Broadcom
  5340. + *
  5341. + * This program is free software; you can redistribute it and/or modify
  5342. + * it under the terms of the GNU General Public License version 2 as
  5343. + * published by the Free Software Foundation.
  5344. + */
  5345. +
  5346. +#include <linux/slab.h>
  5347. +#include <linux/device.h>
  5348. +#include <linux/platform_device.h>
  5349. +#include <linux/module.h>
  5350. +#include <linux/scatterlist.h>
  5351. +
  5352. +#include <mach/dma.h>
  5353. +#include <mach/irqs.h>
  5354. +
  5355. +/*****************************************************************************\
  5356. + * *
  5357. + * Configuration *
  5358. + * *
  5359. +\*****************************************************************************/
  5360. +
  5361. +#define CACHE_LINE_MASK 31
  5362. +#define DRIVER_NAME BCM_DMAMAN_DRIVER_NAME
  5363. +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
  5364. +
  5365. +/* valid only for channels 0 - 14, 15 has its own base address */
  5366. +#define BCM2708_DMA_CHAN(n) ((n)<<8) /* base address */
  5367. +#define BCM2708_DMA_CHANIO(dma_base, n) \
  5368. + ((void __iomem *)((char *)(dma_base)+BCM2708_DMA_CHAN(n)))
  5369. +
  5370. +
  5371. +/*****************************************************************************\
  5372. + * *
  5373. + * DMA Auxilliary Functions *
  5374. + * *
  5375. +\*****************************************************************************/
  5376. +
  5377. +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
  5378. + section inside the DMA buffer and another section outside it.
  5379. + Even if we flush DMA buffers from the cache there is always the chance that
  5380. + during a DMA someone will access the part of a cache line that is outside
  5381. + the DMA buffer - which will then bring in unwelcome data.
  5382. + Without being able to dictate our own buffer pools we must insist that
  5383. + DMA buffers consist of a whole number of cache lines.
  5384. +*/
  5385. +
  5386. +extern int
  5387. +bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
  5388. +{
  5389. + int i;
  5390. +
  5391. + for (i = 0; i < sg_len; i++) {
  5392. + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
  5393. + sg_ptr[i].length & CACHE_LINE_MASK)
  5394. + return 0;
  5395. + }
  5396. +
  5397. + return 1;
  5398. +}
  5399. +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
  5400. +
  5401. +extern void
  5402. +bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block)
  5403. +{
  5404. + dsb(); /* ARM data synchronization (push) operation */
  5405. +
  5406. + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
  5407. + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
  5408. +}
  5409. +
  5410. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
  5411. +{
  5412. + dsb();
  5413. +
  5414. + /* ugly busy wait only option for now */
  5415. + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
  5416. + cpu_relax();
  5417. +}
  5418. +
  5419. +EXPORT_SYMBOL_GPL(bcm_dma_start);
  5420. +
  5421. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
  5422. +{
  5423. + dsb();
  5424. +
  5425. + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
  5426. +}
  5427. +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
  5428. +
  5429. +/* Complete an ongoing DMA (assuming its results are to be ignored)
  5430. + Does nothing if there is no DMA in progress.
  5431. + This routine waits for the current AXI transfer to complete before
  5432. + terminating the current DMA. If the current transfer is hung on a DREQ used
  5433. + by an uncooperative peripheral the AXI transfer may never complete. In this
  5434. + case the routine times out and return a non-zero error code.
  5435. + Use of this routine doesn't guarantee that the ongoing or aborted DMA
  5436. + does not produce an interrupt.
  5437. +*/
  5438. +extern int
  5439. +bcm_dma_abort(void __iomem *dma_chan_base)
  5440. +{
  5441. + unsigned long int cs;
  5442. + int rc = 0;
  5443. +
  5444. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  5445. +
  5446. + if (BCM2708_DMA_ACTIVE & cs) {
  5447. + long int timeout = 10000;
  5448. +
  5449. + /* write 0 to the active bit - pause the DMA */
  5450. + writel(0, dma_chan_base + BCM2708_DMA_CS);
  5451. +
  5452. + /* wait for any current AXI transfer to complete */
  5453. + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
  5454. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  5455. +
  5456. + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
  5457. + /* we'll un-pause when we set of our next DMA */
  5458. + rc = -ETIMEDOUT;
  5459. +
  5460. + } else if (BCM2708_DMA_ACTIVE & cs) {
  5461. + /* terminate the control block chain */
  5462. + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
  5463. +
  5464. + /* abort the whole DMA */
  5465. + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
  5466. + dma_chan_base + BCM2708_DMA_CS);
  5467. + }
  5468. + }
  5469. +
  5470. + return rc;
  5471. +}
  5472. +EXPORT_SYMBOL_GPL(bcm_dma_abort);
  5473. +
  5474. +
  5475. +/***************************************************************************** \
  5476. + * *
  5477. + * DMA Manager Device Methods *
  5478. + * *
  5479. +\*****************************************************************************/
  5480. +
  5481. +struct vc_dmaman {
  5482. + void __iomem *dma_base;
  5483. + u32 chan_available; /* bitmap of available channels */
  5484. + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
  5485. +};
  5486. +
  5487. +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
  5488. + u32 chans_available)
  5489. +{
  5490. + dmaman->dma_base = dma_base;
  5491. + dmaman->chan_available = chans_available;
  5492. + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* chans 2 & 3 */
  5493. + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* chan 0 */
  5494. + dmaman->has_feature[BCM_DMA_FEATURE_NORMAL_ORD] = 0xfe; /* chans 1 to 7 */
  5495. + dmaman->has_feature[BCM_DMA_FEATURE_LITE_ORD] = 0x7f00; /* chans 8 to 14 */
  5496. +}
  5497. +
  5498. +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
  5499. + unsigned preferred_feature_set)
  5500. +{
  5501. + u32 chans;
  5502. + int feature;
  5503. +
  5504. + chans = dmaman->chan_available;
  5505. + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
  5506. + /* select the subset of available channels with the desired
  5507. + feature so long as some of the candidate channels have that
  5508. + feature */
  5509. + if ((preferred_feature_set & (1 << feature)) &&
  5510. + (chans & dmaman->has_feature[feature]))
  5511. + chans &= dmaman->has_feature[feature];
  5512. +
  5513. + if (chans) {
  5514. + int chan = 0;
  5515. + /* return the ordinal of the first channel in the bitmap */
  5516. + while (chans != 0 && (chans & 1) == 0) {
  5517. + chans >>= 1;
  5518. + chan++;
  5519. + }
  5520. + /* claim the channel */
  5521. + dmaman->chan_available &= ~(1 << chan);
  5522. + return chan;
  5523. + } else
  5524. + return -ENOMEM;
  5525. +}
  5526. +
  5527. +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
  5528. +{
  5529. + if (chan < 0)
  5530. + return -EINVAL;
  5531. + else if ((1 << chan) & dmaman->chan_available)
  5532. + return -EIDRM;
  5533. + else {
  5534. + dmaman->chan_available |= (1 << chan);
  5535. + return 0;
  5536. + }
  5537. +}
  5538. +
  5539. +/*****************************************************************************\
  5540. + * *
  5541. + * DMA IRQs *
  5542. + * *
  5543. +\*****************************************************************************/
  5544. +
  5545. +static unsigned char bcm_dma_irqs[] = {
  5546. + IRQ_DMA0,
  5547. + IRQ_DMA1,
  5548. + IRQ_DMA2,
  5549. + IRQ_DMA3,
  5550. + IRQ_DMA4,
  5551. + IRQ_DMA5,
  5552. + IRQ_DMA6,
  5553. + IRQ_DMA7,
  5554. + IRQ_DMA8,
  5555. + IRQ_DMA9,
  5556. + IRQ_DMA10,
  5557. + IRQ_DMA11,
  5558. + IRQ_DMA12
  5559. +};
  5560. +
  5561. +
  5562. +/***************************************************************************** \
  5563. + * *
  5564. + * DMA Manager Monitor *
  5565. + * *
  5566. +\*****************************************************************************/
  5567. +
  5568. +static struct device *dmaman_dev; /* we assume there's only one! */
  5569. +
  5570. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  5571. + void __iomem **out_dma_base, int *out_dma_irq)
  5572. +{
  5573. + if (!dmaman_dev)
  5574. + return -ENODEV;
  5575. + else {
  5576. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  5577. + int rc;
  5578. +
  5579. + device_lock(dmaman_dev);
  5580. + rc = vc_dmaman_chan_alloc(dmaman, preferred_feature_set);
  5581. + if (rc >= 0) {
  5582. + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base,
  5583. + rc);
  5584. + *out_dma_irq = bcm_dma_irqs[rc];
  5585. + }
  5586. + device_unlock(dmaman_dev);
  5587. +
  5588. + return rc;
  5589. + }
  5590. +}
  5591. +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
  5592. +
  5593. +extern int bcm_dma_chan_free(int channel)
  5594. +{
  5595. + if (dmaman_dev) {
  5596. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  5597. + int rc;
  5598. +
  5599. + device_lock(dmaman_dev);
  5600. + rc = vc_dmaman_chan_free(dmaman, channel);
  5601. + device_unlock(dmaman_dev);
  5602. +
  5603. + return rc;
  5604. + } else
  5605. + return -ENODEV;
  5606. +}
  5607. +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
  5608. +
  5609. +static int dev_dmaman_register(const char *dev_name, struct device *dev)
  5610. +{
  5611. + int rc = dmaman_dev ? -EINVAL : 0;
  5612. + dmaman_dev = dev;
  5613. + return rc;
  5614. +}
  5615. +
  5616. +static void dev_dmaman_deregister(const char *dev_name, struct device *dev)
  5617. +{
  5618. + dmaman_dev = NULL;
  5619. +}
  5620. +
  5621. +/*****************************************************************************\
  5622. + * *
  5623. + * DMA Device *
  5624. + * *
  5625. +\*****************************************************************************/
  5626. +
  5627. +static int dmachans = -1; /* module parameter */
  5628. +
  5629. +static int bcm_dmaman_probe(struct platform_device *pdev)
  5630. +{
  5631. + int ret = 0;
  5632. + struct vc_dmaman *dmaman;
  5633. + struct resource *dma_res = NULL;
  5634. + void __iomem *dma_base = NULL;
  5635. + int have_dma_region = 0;
  5636. +
  5637. + dmaman = kzalloc(sizeof(*dmaman), GFP_KERNEL);
  5638. + if (NULL == dmaman) {
  5639. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  5640. + "DMA management memory\n");
  5641. + ret = -ENOMEM;
  5642. + } else {
  5643. +
  5644. + dma_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  5645. + if (dma_res == NULL) {
  5646. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  5647. + "resource\n");
  5648. + ret = -ENODEV;
  5649. + } else if (!request_mem_region(dma_res->start,
  5650. + resource_size(dma_res),
  5651. + DRIVER_NAME)) {
  5652. + dev_err(&pdev->dev, "cannot obtain DMA region\n");
  5653. + ret = -EBUSY;
  5654. + } else {
  5655. + have_dma_region = 1;
  5656. + dma_base = ioremap(dma_res->start,
  5657. + resource_size(dma_res));
  5658. + if (!dma_base) {
  5659. + dev_err(&pdev->dev, "cannot map DMA region\n");
  5660. + ret = -ENOMEM;
  5661. + } else {
  5662. + /* use module parameter if one was provided */
  5663. + if (dmachans > 0)
  5664. + vc_dmaman_init(dmaman, dma_base,
  5665. + dmachans);
  5666. + else
  5667. + vc_dmaman_init(dmaman, dma_base,
  5668. + DEFAULT_DMACHAN_BITMAP);
  5669. +
  5670. + platform_set_drvdata(pdev, dmaman);
  5671. + dev_dmaman_register(DRIVER_NAME, &pdev->dev);
  5672. +
  5673. + printk(KERN_INFO DRIVER_NAME ": DMA manager "
  5674. + "at %p\n", dma_base);
  5675. + }
  5676. + }
  5677. + }
  5678. + if (ret != 0) {
  5679. + if (dma_base)
  5680. + iounmap(dma_base);
  5681. + if (dma_res && have_dma_region)
  5682. + release_mem_region(dma_res->start,
  5683. + resource_size(dma_res));
  5684. + if (dmaman)
  5685. + kfree(dmaman);
  5686. + }
  5687. + return ret;
  5688. +}
  5689. +
  5690. +static int bcm_dmaman_remove(struct platform_device *pdev)
  5691. +{
  5692. + struct vc_dmaman *dmaman = platform_get_drvdata(pdev);
  5693. +
  5694. + platform_set_drvdata(pdev, NULL);
  5695. + dev_dmaman_deregister(DRIVER_NAME, &pdev->dev);
  5696. + kfree(dmaman);
  5697. +
  5698. + return 0;
  5699. +}
  5700. +
  5701. +static struct platform_driver bcm_dmaman_driver = {
  5702. + .probe = bcm_dmaman_probe,
  5703. + .remove = bcm_dmaman_remove,
  5704. +
  5705. + .driver = {
  5706. + .name = DRIVER_NAME,
  5707. + .owner = THIS_MODULE,
  5708. + },
  5709. +};
  5710. +
  5711. +/*****************************************************************************\
  5712. + * *
  5713. + * Driver init/exit *
  5714. + * *
  5715. +\*****************************************************************************/
  5716. +
  5717. +static int __init bcm_dmaman_drv_init(void)
  5718. +{
  5719. + int ret;
  5720. +
  5721. + ret = platform_driver_register(&bcm_dmaman_driver);
  5722. + if (ret != 0) {
  5723. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  5724. + "on platform\n");
  5725. + }
  5726. +
  5727. + return ret;
  5728. +}
  5729. +
  5730. +static void __exit bcm_dmaman_drv_exit(void)
  5731. +{
  5732. + platform_driver_unregister(&bcm_dmaman_driver);
  5733. +}
  5734. +
  5735. +module_init(bcm_dmaman_drv_init);
  5736. +module_exit(bcm_dmaman_drv_exit);
  5737. +
  5738. +module_param(dmachans, int, 0644);
  5739. +
  5740. +MODULE_AUTHOR("Gray Girling <grayg@broadcom.com>");
  5741. +MODULE_DESCRIPTION("DMA channel manager driver");
  5742. +MODULE_LICENSE("GPL");
  5743. +
  5744. +MODULE_PARM_DESC(dmachans, "Bitmap of DMA channels available to the ARM");
  5745. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/arm_control.h linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h
  5746. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/arm_control.h 1969-12-31 18:00:00.000000000 -0600
  5747. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h 2014-12-11 14:02:51.516418001 -0600
  5748. @@ -0,0 +1,419 @@
  5749. +/*
  5750. + * linux/arch/arm/mach-bcm2708/arm_control.h
  5751. + *
  5752. + * Copyright (C) 2010 Broadcom
  5753. + *
  5754. + * This program is free software; you can redistribute it and/or modify
  5755. + * it under the terms of the GNU General Public License as published by
  5756. + * the Free Software Foundation; either version 2 of the License, or
  5757. + * (at your option) any later version.
  5758. + *
  5759. + * This program is distributed in the hope that it will be useful,
  5760. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5761. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5762. + * GNU General Public License for more details.
  5763. + *
  5764. + * You should have received a copy of the GNU General Public License
  5765. + * along with this program; if not, write to the Free Software
  5766. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5767. + */
  5768. +
  5769. +#ifndef __BCM2708_ARM_CONTROL_H
  5770. +#define __BCM2708_ARM_CONTROL_H
  5771. +
  5772. +/*
  5773. + * Definitions and addresses for the ARM CONTROL logic
  5774. + * This file is manually generated.
  5775. + */
  5776. +
  5777. +#define ARM_BASE 0x7E00B000
  5778. +
  5779. +/* Basic configuration */
  5780. +#define ARM_CONTROL0 HW_REGISTER_RW(ARM_BASE+0x000)
  5781. +#define ARM_C0_SIZ128M 0x00000000
  5782. +#define ARM_C0_SIZ256M 0x00000001
  5783. +#define ARM_C0_SIZ512M 0x00000002
  5784. +#define ARM_C0_SIZ1G 0x00000003
  5785. +#define ARM_C0_BRESP0 0x00000000
  5786. +#define ARM_C0_BRESP1 0x00000004
  5787. +#define ARM_C0_BRESP2 0x00000008
  5788. +#define ARM_C0_BOOTHI 0x00000010
  5789. +#define ARM_C0_UNUSED05 0x00000020 /* free */
  5790. +#define ARM_C0_FULLPERI 0x00000040
  5791. +#define ARM_C0_UNUSED78 0x00000180 /* free */
  5792. +#define ARM_C0_JTAGMASK 0x00000E00
  5793. +#define ARM_C0_JTAGOFF 0x00000000
  5794. +#define ARM_C0_JTAGBASH 0x00000800 /* Debug on GPIO off */
  5795. +#define ARM_C0_JTAGGPIO 0x00000C00 /* Debug on GPIO on */
  5796. +#define ARM_C0_APROTMSK 0x0000F000
  5797. +#define ARM_C0_DBG0SYNC 0x00010000 /* VPU0 halt sync */
  5798. +#define ARM_C0_DBG1SYNC 0x00020000 /* VPU1 halt sync */
  5799. +#define ARM_C0_SWDBGREQ 0x00040000 /* HW debug request */
  5800. +#define ARM_C0_PASSHALT 0x00080000 /* ARM halt passed to debugger */
  5801. +#define ARM_C0_PRIO_PER 0x00F00000 /* per priority mask */
  5802. +#define ARM_C0_PRIO_L2 0x0F000000
  5803. +#define ARM_C0_PRIO_UC 0xF0000000
  5804. +
  5805. +#define ARM_C0_APROTPASS 0x0000A000 /* Translate 1:1 */
  5806. +#define ARM_C0_APROTUSER 0x00000000 /* Only user mode */
  5807. +#define ARM_C0_APROTSYST 0x0000F000 /* Only system mode */
  5808. +
  5809. +
  5810. +#define ARM_CONTROL1 HW_REGISTER_RW(ARM_BASE+0x440)
  5811. +#define ARM_C1_TIMER 0x00000001 /* re-route timer IRQ to VC */
  5812. +#define ARM_C1_MAIL 0x00000002 /* re-route Mail IRQ to VC */
  5813. +#define ARM_C1_BELL0 0x00000004 /* re-route Doorbell 0 to VC */
  5814. +#define ARM_C1_BELL1 0x00000008 /* re-route Doorbell 1 to VC */
  5815. +#define ARM_C1_PERSON 0x00000100 /* peripherals on */
  5816. +#define ARM_C1_REQSTOP 0x00000200 /* ASYNC bridge request stop */
  5817. +
  5818. +#define ARM_STATUS HW_REGISTER_RW(ARM_BASE+0x444)
  5819. +#define ARM_S_ACKSTOP 0x80000000 /* Bridge stopped */
  5820. +#define ARM_S_READPEND 0x000003FF /* pending reads counter */
  5821. +#define ARM_S_WRITPEND 0x000FFC00 /* pending writes counter */
  5822. +
  5823. +#define ARM_ERRHALT HW_REGISTER_RW(ARM_BASE+0x448)
  5824. +#define ARM_EH_PERIBURST 0x00000001 /* Burst write seen on peri bus */
  5825. +#define ARM_EH_ILLADDRS1 0x00000002 /* Address bits 25-27 error */
  5826. +#define ARM_EH_ILLADDRS2 0x00000004 /* Address bits 31-28 error */
  5827. +#define ARM_EH_VPU0HALT 0x00000008 /* VPU0 halted & in debug mode */
  5828. +#define ARM_EH_VPU1HALT 0x00000010 /* VPU1 halted & in debug mode */
  5829. +#define ARM_EH_ARMHALT 0x00000020 /* ARM in halted debug mode */
  5830. +
  5831. +#define ARM_ID_SECURE HW_REGISTER_RW(ARM_BASE+0x00C)
  5832. +#define ARM_ID HW_REGISTER_RW(ARM_BASE+0x44C)
  5833. +#define ARM_IDVAL 0x364D5241
  5834. +
  5835. +/* Translation memory */
  5836. +#define ARM_TRANSLATE HW_REGISTER_RW(ARM_BASE+0x100)
  5837. +/* 32 locations: 0x100.. 0x17F */
  5838. +/* 32 spare means we CAN go to 64 pages.... */
  5839. +
  5840. +
  5841. +/* Interrupts */
  5842. +#define ARM_IRQ_PEND0 HW_REGISTER_RW(ARM_BASE+0x200) /* Top IRQ bits */
  5843. +#define ARM_I0_TIMER 0x00000001 /* timer IRQ */
  5844. +#define ARM_I0_MAIL 0x00000002 /* Mail IRQ */
  5845. +#define ARM_I0_BELL0 0x00000004 /* Doorbell 0 */
  5846. +#define ARM_I0_BELL1 0x00000008 /* Doorbell 1 */
  5847. +#define ARM_I0_BANK1 0x00000100 /* Bank1 IRQ */
  5848. +#define ARM_I0_BANK2 0x00000200 /* Bank2 IRQ */
  5849. +
  5850. +#define ARM_IRQ_PEND1 HW_REGISTER_RW(ARM_BASE+0x204) /* All bank1 IRQ bits */
  5851. +/* todo: all I1_interrupt sources */
  5852. +#define ARM_IRQ_PEND2 HW_REGISTER_RW(ARM_BASE+0x208) /* All bank2 IRQ bits */
  5853. +/* todo: all I2_interrupt sources */
  5854. +
  5855. +#define ARM_IRQ_FAST HW_REGISTER_RW(ARM_BASE+0x20C) /* FIQ control */
  5856. +#define ARM_IF_INDEX 0x0000007F /* FIQ select */
  5857. +#define ARM_IF_ENABLE 0x00000080 /* FIQ enable */
  5858. +#define ARM_IF_VCMASK 0x0000003F /* FIQ = (index from VC source) */
  5859. +#define ARM_IF_TIMER 0x00000040 /* FIQ = ARM timer */
  5860. +#define ARM_IF_MAIL 0x00000041 /* FIQ = ARM Mail */
  5861. +#define ARM_IF_BELL0 0x00000042 /* FIQ = ARM Doorbell 0 */
  5862. +#define ARM_IF_BELL1 0x00000043 /* FIQ = ARM Doorbell 1 */
  5863. +#define ARM_IF_VP0HALT 0x00000044 /* FIQ = VPU0 Halt seen */
  5864. +#define ARM_IF_VP1HALT 0x00000045 /* FIQ = VPU1 Halt seen */
  5865. +#define ARM_IF_ILLEGAL 0x00000046 /* FIQ = Illegal access seen */
  5866. +
  5867. +#define ARM_IRQ_ENBL1 HW_REGISTER_RW(ARM_BASE+0x210) /* Bank1 enable bits */
  5868. +#define ARM_IRQ_ENBL2 HW_REGISTER_RW(ARM_BASE+0x214) /* Bank2 enable bits */
  5869. +#define ARM_IRQ_ENBL3 HW_REGISTER_RW(ARM_BASE+0x218) /* ARM irqs enable bits */
  5870. +#define ARM_IRQ_DIBL1 HW_REGISTER_RW(ARM_BASE+0x21C) /* Bank1 disable bits */
  5871. +#define ARM_IRQ_DIBL2 HW_REGISTER_RW(ARM_BASE+0x220) /* Bank2 disable bits */
  5872. +#define ARM_IRQ_DIBL3 HW_REGISTER_RW(ARM_BASE+0x224) /* ARM irqs disable bits */
  5873. +#define ARM_IE_TIMER 0x00000001 /* Timer IRQ */
  5874. +#define ARM_IE_MAIL 0x00000002 /* Mail IRQ */
  5875. +#define ARM_IE_BELL0 0x00000004 /* Doorbell 0 */
  5876. +#define ARM_IE_BELL1 0x00000008 /* Doorbell 1 */
  5877. +#define ARM_IE_VP0HALT 0x00000010 /* VPU0 Halt */
  5878. +#define ARM_IE_VP1HALT 0x00000020 /* VPU1 Halt */
  5879. +#define ARM_IE_ILLEGAL 0x00000040 /* Illegal access seen */
  5880. +
  5881. +/* Timer */
  5882. +/* For reg. fields see sp804 spec. */
  5883. +#define ARM_T_LOAD HW_REGISTER_RW(ARM_BASE+0x400)
  5884. +#define ARM_T_VALUE HW_REGISTER_RW(ARM_BASE+0x404)
  5885. +#define ARM_T_CONTROL HW_REGISTER_RW(ARM_BASE+0x408)
  5886. +#define ARM_T_IRQCNTL HW_REGISTER_RW(ARM_BASE+0x40C)
  5887. +#define ARM_T_RAWIRQ HW_REGISTER_RW(ARM_BASE+0x410)
  5888. +#define ARM_T_MSKIRQ HW_REGISTER_RW(ARM_BASE+0x414)
  5889. +#define ARM_T_RELOAD HW_REGISTER_RW(ARM_BASE+0x418)
  5890. +#define ARM_T_PREDIV HW_REGISTER_RW(ARM_BASE+0x41c)
  5891. +#define ARM_T_FREECNT HW_REGISTER_RW(ARM_BASE+0x420)
  5892. +
  5893. +#define TIMER_CTRL_ONESHOT (1 << 0)
  5894. +#define TIMER_CTRL_32BIT (1 << 1)
  5895. +#define TIMER_CTRL_DIV1 (0 << 2)
  5896. +#define TIMER_CTRL_DIV16 (1 << 2)
  5897. +#define TIMER_CTRL_DIV256 (2 << 2)
  5898. +#define TIMER_CTRL_IE (1 << 5)
  5899. +#define TIMER_CTRL_PERIODIC (1 << 6)
  5900. +#define TIMER_CTRL_ENABLE (1 << 7)
  5901. +#define TIMER_CTRL_DBGHALT (1 << 8)
  5902. +#define TIMER_CTRL_ENAFREE (1 << 9)
  5903. +#define TIMER_CTRL_FREEDIV_SHIFT 16)
  5904. +#define TIMER_CTRL_FREEDIV_MASK 0xff
  5905. +
  5906. +/* Semaphores, Doorbells, Mailboxes */
  5907. +#define ARM_SBM_OWN0 (ARM_BASE+0x800)
  5908. +#define ARM_SBM_OWN1 (ARM_BASE+0x900)
  5909. +#define ARM_SBM_OWN2 (ARM_BASE+0xA00)
  5910. +#define ARM_SBM_OWN3 (ARM_BASE+0xB00)
  5911. +
  5912. +/* MAILBOXES
  5913. + * Register flags are common across all
  5914. + * owner registers. See end of this section
  5915. + *
  5916. + * Semaphores, Doorbells, Mailboxes Owner 0
  5917. + *
  5918. + */
  5919. +
  5920. +#define ARM_0_SEMS HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5921. +#define ARM_0_SEM0 HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5922. +#define ARM_0_SEM1 HW_REGISTER_RW(ARM_SBM_OWN0+0x04)
  5923. +#define ARM_0_SEM2 HW_REGISTER_RW(ARM_SBM_OWN0+0x08)
  5924. +#define ARM_0_SEM3 HW_REGISTER_RW(ARM_SBM_OWN0+0x0C)
  5925. +#define ARM_0_SEM4 HW_REGISTER_RW(ARM_SBM_OWN0+0x10)
  5926. +#define ARM_0_SEM5 HW_REGISTER_RW(ARM_SBM_OWN0+0x14)
  5927. +#define ARM_0_SEM6 HW_REGISTER_RW(ARM_SBM_OWN0+0x18)
  5928. +#define ARM_0_SEM7 HW_REGISTER_RW(ARM_SBM_OWN0+0x1C)
  5929. +#define ARM_0_BELL0 HW_REGISTER_RW(ARM_SBM_OWN0+0x40)
  5930. +#define ARM_0_BELL1 HW_REGISTER_RW(ARM_SBM_OWN0+0x44)
  5931. +#define ARM_0_BELL2 HW_REGISTER_RW(ARM_SBM_OWN0+0x48)
  5932. +#define ARM_0_BELL3 HW_REGISTER_RW(ARM_SBM_OWN0+0x4C)
  5933. +/* MAILBOX 0 access in Owner 0 area */
  5934. +/* Some addresses should ONLY be used by owner 0 */
  5935. +#define ARM_0_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) */
  5936. +#define ARM_0_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) Normal read */
  5937. +#define ARM_0_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN0+0x90) /* none-pop read */
  5938. +#define ARM_0_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN0+0x94) /* Sender read (only LS 2 bits) */
  5939. +#define ARM_0_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN0+0x98) /* Status read */
  5940. +#define ARM_0_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0x9C) /* Config read/write */
  5941. +/* MAILBOX 1 access in Owner 0 area */
  5942. +/* Owner 0 should only WRITE to this mailbox */
  5943. +#define ARM_0_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) /* .. 0xAC (4 locations) */
  5944. +/*#define ARM_0_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) */ /* DO NOT USE THIS !!!!! */
  5945. +/*#define ARM_0_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN0+0xB0) */ /* DO NOT USE THIS !!!!! */
  5946. +/*#define ARM_0_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN0+0xB4) */ /* DO NOT USE THIS !!!!! */
  5947. +#define ARM_0_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN0+0xB8) /* Status read */
  5948. +/*#define ARM_0_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0xBC) */ /* DO NOT USE THIS !!!!! */
  5949. +/* General SEM, BELL, MAIL config/status */
  5950. +#define ARM_0_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE0) /* semaphore clear/debug register */
  5951. +#define ARM_0_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE4) /* Doorbells clear/debug register */
  5952. +#define ARM_0_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xF8) /* ALL interrupts */
  5953. +#define ARM_0_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xFC) /* IRQS pending for owner 0 */
  5954. +
  5955. +/* Semaphores, Doorbells, Mailboxes Owner 1 */
  5956. +#define ARM_1_SEMS HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5957. +#define ARM_1_SEM0 HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5958. +#define ARM_1_SEM1 HW_REGISTER_RW(ARM_SBM_OWN1+0x04)
  5959. +#define ARM_1_SEM2 HW_REGISTER_RW(ARM_SBM_OWN1+0x08)
  5960. +#define ARM_1_SEM3 HW_REGISTER_RW(ARM_SBM_OWN1+0x0C)
  5961. +#define ARM_1_SEM4 HW_REGISTER_RW(ARM_SBM_OWN1+0x10)
  5962. +#define ARM_1_SEM5 HW_REGISTER_RW(ARM_SBM_OWN1+0x14)
  5963. +#define ARM_1_SEM6 HW_REGISTER_RW(ARM_SBM_OWN1+0x18)
  5964. +#define ARM_1_SEM7 HW_REGISTER_RW(ARM_SBM_OWN1+0x1C)
  5965. +#define ARM_1_BELL0 HW_REGISTER_RW(ARM_SBM_OWN1+0x40)
  5966. +#define ARM_1_BELL1 HW_REGISTER_RW(ARM_SBM_OWN1+0x44)
  5967. +#define ARM_1_BELL2 HW_REGISTER_RW(ARM_SBM_OWN1+0x48)
  5968. +#define ARM_1_BELL3 HW_REGISTER_RW(ARM_SBM_OWN1+0x4C)
  5969. +/* MAILBOX 0 access in Owner 0 area */
  5970. +/* Owner 1 should only WRITE to this mailbox */
  5971. +#define ARM_1_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0x80) /* .. 0x8C (4 locations) */
  5972. +/*#define ARM_1_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN1+0x80) */ /* DO NOT USE THIS !!!!! */
  5973. +/*#define ARM_1_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN1+0x90) */ /* DO NOT USE THIS !!!!! */
  5974. +/*#define ARM_1_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN1+0x94) */ /* DO NOT USE THIS !!!!! */
  5975. +#define ARM_1_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN1+0x98) /* Status read */
  5976. +/*#define ARM_1_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0x9C) */ /* DO NOT USE THIS !!!!! */
  5977. +/* MAILBOX 1 access in Owner 0 area */
  5978. +#define ARM_1_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) */
  5979. +#define ARM_1_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) Normal read */
  5980. +#define ARM_1_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN1+0xB0) /* none-pop read */
  5981. +#define ARM_1_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN1+0xB4) /* Sender read (only LS 2 bits) */
  5982. +#define ARM_1_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN1+0xB8) /* Status read */
  5983. +#define ARM_1_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0xBC)
  5984. +/* General SEM, BELL, MAIL config/status */
  5985. +#define ARM_1_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE0) /* semaphore clear/debug register */
  5986. +#define ARM_1_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE4) /* Doorbells clear/debug register */
  5987. +#define ARM_1_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xFC) /* IRQS pending for owner 1 */
  5988. +#define ARM_1_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xF8) /* ALL interrupts */
  5989. +
  5990. +/* Semaphores, Doorbells, Mailboxes Owner 2 */
  5991. +#define ARM_2_SEMS HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5992. +#define ARM_2_SEM0 HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5993. +#define ARM_2_SEM1 HW_REGISTER_RW(ARM_SBM_OWN2+0x04)
  5994. +#define ARM_2_SEM2 HW_REGISTER_RW(ARM_SBM_OWN2+0x08)
  5995. +#define ARM_2_SEM3 HW_REGISTER_RW(ARM_SBM_OWN2+0x0C)
  5996. +#define ARM_2_SEM4 HW_REGISTER_RW(ARM_SBM_OWN2+0x10)
  5997. +#define ARM_2_SEM5 HW_REGISTER_RW(ARM_SBM_OWN2+0x14)
  5998. +#define ARM_2_SEM6 HW_REGISTER_RW(ARM_SBM_OWN2+0x18)
  5999. +#define ARM_2_SEM7 HW_REGISTER_RW(ARM_SBM_OWN2+0x1C)
  6000. +#define ARM_2_BELL0 HW_REGISTER_RW(ARM_SBM_OWN2+0x40)
  6001. +#define ARM_2_BELL1 HW_REGISTER_RW(ARM_SBM_OWN2+0x44)
  6002. +#define ARM_2_BELL2 HW_REGISTER_RW(ARM_SBM_OWN2+0x48)
  6003. +#define ARM_2_BELL3 HW_REGISTER_RW(ARM_SBM_OWN2+0x4C)
  6004. +/* MAILBOX 0 access in Owner 2 area */
  6005. +/* Owner 2 should only WRITE to this mailbox */
  6006. +#define ARM_2_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0x80) /* .. 0x8C (4 locations) */
  6007. +/*#define ARM_2_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN2+0x80) */ /* DO NOT USE THIS !!!!! */
  6008. +/*#define ARM_2_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN2+0x90) */ /* DO NOT USE THIS !!!!! */
  6009. +/*#define ARM_2_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN2+0x94) */ /* DO NOT USE THIS !!!!! */
  6010. +#define ARM_2_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN2+0x98) /* Status read */
  6011. +/*#define ARM_2_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0x9C) */ /* DO NOT USE THIS !!!!! */
  6012. +/* MAILBOX 1 access in Owner 2 area */
  6013. +/* Owner 2 should only WRITE to this mailbox */
  6014. +#define ARM_2_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) /* .. 0xAC (4 locations) */
  6015. +/*#define ARM_2_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) */ /* DO NOT USE THIS !!!!! */
  6016. +/*#define ARM_2_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN2+0xB0) */ /* DO NOT USE THIS !!!!! */
  6017. +/*#define ARM_2_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN2+0xB4) */ /* DO NOT USE THIS !!!!! */
  6018. +#define ARM_2_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN2+0xB8) /* Status read */
  6019. +/*#define ARM_2_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0xBC) */ /* DO NOT USE THIS !!!!! */
  6020. +/* General SEM, BELL, MAIL config/status */
  6021. +#define ARM_2_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE0) /* semaphore clear/debug register */
  6022. +#define ARM_2_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE4) /* Doorbells clear/debug register */
  6023. +#define ARM_2_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xFC) /* IRQS pending for owner 2 */
  6024. +#define ARM_2_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xF8) /* ALL interrupts */
  6025. +
  6026. +/* Semaphores, Doorbells, Mailboxes Owner 3 */
  6027. +#define ARM_3_SEMS HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  6028. +#define ARM_3_SEM0 HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  6029. +#define ARM_3_SEM1 HW_REGISTER_RW(ARM_SBM_OWN3+0x04)
  6030. +#define ARM_3_SEM2 HW_REGISTER_RW(ARM_SBM_OWN3+0x08)
  6031. +#define ARM_3_SEM3 HW_REGISTER_RW(ARM_SBM_OWN3+0x0C)
  6032. +#define ARM_3_SEM4 HW_REGISTER_RW(ARM_SBM_OWN3+0x10)
  6033. +#define ARM_3_SEM5 HW_REGISTER_RW(ARM_SBM_OWN3+0x14)
  6034. +#define ARM_3_SEM6 HW_REGISTER_RW(ARM_SBM_OWN3+0x18)
  6035. +#define ARM_3_SEM7 HW_REGISTER_RW(ARM_SBM_OWN3+0x1C)
  6036. +#define ARM_3_BELL0 HW_REGISTER_RW(ARM_SBM_OWN3+0x40)
  6037. +#define ARM_3_BELL1 HW_REGISTER_RW(ARM_SBM_OWN3+0x44)
  6038. +#define ARM_3_BELL2 HW_REGISTER_RW(ARM_SBM_OWN3+0x48)
  6039. +#define ARM_3_BELL3 HW_REGISTER_RW(ARM_SBM_OWN3+0x4C)
  6040. +/* MAILBOX 0 access in Owner 3 area */
  6041. +/* Owner 3 should only WRITE to this mailbox */
  6042. +#define ARM_3_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0x80) /* .. 0x8C (4 locations) */
  6043. +/*#define ARM_3_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN3+0x80) */ /* DO NOT USE THIS !!!!! */
  6044. +/*#define ARM_3_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN3+0x90) */ /* DO NOT USE THIS !!!!! */
  6045. +/*#define ARM_3_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN3+0x94) */ /* DO NOT USE THIS !!!!! */
  6046. +#define ARM_3_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN3+0x98) /* Status read */
  6047. +/*#define ARM_3_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0x9C) */ /* DO NOT USE THIS !!!!! */
  6048. +/* MAILBOX 1 access in Owner 3 area */
  6049. +/* Owner 3 should only WRITE to this mailbox */
  6050. +#define ARM_3_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) /* .. 0xAC (4 locations) */
  6051. +/*#define ARM_3_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) */ /* DO NOT USE THIS !!!!! */
  6052. +/*#define ARM_3_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN3+0xB0) */ /* DO NOT USE THIS !!!!! */
  6053. +/*#define ARM_3_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN3+0xB4) */ /* DO NOT USE THIS !!!!! */
  6054. +#define ARM_3_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN3+0xB8) /* Status read */
  6055. +/*#define ARM_3_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0xBC) */ /* DO NOT USE THIS !!!!! */
  6056. +/* General SEM, BELL, MAIL config/status */
  6057. +#define ARM_3_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE0) /* semaphore clear/debug register */
  6058. +#define ARM_3_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE4) /* Doorbells clear/debug register */
  6059. +#define ARM_3_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xFC) /* IRQS pending for owner 3 */
  6060. +#define ARM_3_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xF8) /* ALL interrupts */
  6061. +
  6062. +
  6063. +
  6064. +/* Mailbox flags. Valid for all owners */
  6065. +
  6066. +/* Mailbox status register (...0x98) */
  6067. +#define ARM_MS_FULL 0x80000000
  6068. +#define ARM_MS_EMPTY 0x40000000
  6069. +#define ARM_MS_LEVEL 0x400000FF /* Max. value depdnds on mailbox depth parameter */
  6070. +
  6071. +/* MAILBOX config/status register (...0x9C) */
  6072. +/* ANY write to this register clears the error bits! */
  6073. +#define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mailbox irq enable: has data */
  6074. +#define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mailbox irq enable: has space */
  6075. +#define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mailbox irq enable: Opp. is empty */
  6076. +#define ARM_MC_MAIL_CLEAR 0x00000008 /* mailbox clear write 1, then 0 */
  6077. +#define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mailbox irq pending: has space */
  6078. +#define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mailbox irq pending: Opp. is empty */
  6079. +#define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mailbox irq pending */
  6080. +/* Bit 7 is unused */
  6081. +#define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
  6082. +#define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
  6083. +#define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
  6084. +
  6085. +/* Semaphore clear/debug register (...0xE0) */
  6086. +#define ARM_SD_OWN0 0x00000003 /* Owner of sem 0 */
  6087. +#define ARM_SD_OWN1 0x0000000C /* Owner of sem 1 */
  6088. +#define ARM_SD_OWN2 0x00000030 /* Owner of sem 2 */
  6089. +#define ARM_SD_OWN3 0x000000C0 /* Owner of sem 3 */
  6090. +#define ARM_SD_OWN4 0x00000300 /* Owner of sem 4 */
  6091. +#define ARM_SD_OWN5 0x00000C00 /* Owner of sem 5 */
  6092. +#define ARM_SD_OWN6 0x00003000 /* Owner of sem 6 */
  6093. +#define ARM_SD_OWN7 0x0000C000 /* Owner of sem 7 */
  6094. +#define ARM_SD_SEM0 0x00010000 /* Status of sem 0 */
  6095. +#define ARM_SD_SEM1 0x00020000 /* Status of sem 1 */
  6096. +#define ARM_SD_SEM2 0x00040000 /* Status of sem 2 */
  6097. +#define ARM_SD_SEM3 0x00080000 /* Status of sem 3 */
  6098. +#define ARM_SD_SEM4 0x00100000 /* Status of sem 4 */
  6099. +#define ARM_SD_SEM5 0x00200000 /* Status of sem 5 */
  6100. +#define ARM_SD_SEM6 0x00400000 /* Status of sem 6 */
  6101. +#define ARM_SD_SEM7 0x00800000 /* Status of sem 7 */
  6102. +
  6103. +/* Doorbells clear/debug register (...0xE4) */
  6104. +#define ARM_BD_OWN0 0x00000003 /* Owner of doorbell 0 */
  6105. +#define ARM_BD_OWN1 0x0000000C /* Owner of doorbell 1 */
  6106. +#define ARM_BD_OWN2 0x00000030 /* Owner of doorbell 2 */
  6107. +#define ARM_BD_OWN3 0x000000C0 /* Owner of doorbell 3 */
  6108. +#define ARM_BD_BELL0 0x00000100 /* Status of doorbell 0 */
  6109. +#define ARM_BD_BELL1 0x00000200 /* Status of doorbell 1 */
  6110. +#define ARM_BD_BELL2 0x00000400 /* Status of doorbell 2 */
  6111. +#define ARM_BD_BELL3 0x00000800 /* Status of doorbell 3 */
  6112. +
  6113. +/* MY IRQS register (...0xF8) */
  6114. +#define ARM_MYIRQ_BELL 0x00000001 /* This owner has a doorbell IRQ */
  6115. +#define ARM_MYIRQ_MAIL 0x00000002 /* This owner has a mailbox IRQ */
  6116. +
  6117. +/* ALL IRQS register (...0xF8) */
  6118. +#define ARM_AIS_BELL0 0x00000001 /* Doorbell 0 IRQ pending */
  6119. +#define ARM_AIS_BELL1 0x00000002 /* Doorbell 1 IRQ pending */
  6120. +#define ARM_AIS_BELL2 0x00000004 /* Doorbell 2 IRQ pending */
  6121. +#define ARM_AIS_BELL3 0x00000008 /* Doorbell 3 IRQ pending */
  6122. +#define ARM_AIS0_HAVEDATA 0x00000010 /* MAIL 0 has data IRQ pending */
  6123. +#define ARM_AIS0_HAVESPAC 0x00000020 /* MAIL 0 has space IRQ pending */
  6124. +#define ARM_AIS0_OPPEMPTY 0x00000040 /* MAIL 0 opposite is empty IRQ */
  6125. +#define ARM_AIS1_HAVEDATA 0x00000080 /* MAIL 1 has data IRQ pending */
  6126. +#define ARM_AIS1_HAVESPAC 0x00000100 /* MAIL 1 has space IRQ pending */
  6127. +#define ARM_AIS1_OPPEMPTY 0x00000200 /* MAIL 1 opposite is empty IRQ */
  6128. +/* Note that bell-0, bell-1 and MAIL0 IRQ go only to the ARM */
  6129. +/* Whilst that bell-2, bell-3 and MAIL1 IRQ go only to the VC */
  6130. +/* */
  6131. +/* ARM JTAG BASH */
  6132. +/* */
  6133. +#define AJB_BASE 0x7e2000c0
  6134. +
  6135. +#define AJBCONF HW_REGISTER_RW(AJB_BASE+0x00)
  6136. +#define AJB_BITS0 0x000000
  6137. +#define AJB_BITS4 0x000004
  6138. +#define AJB_BITS8 0x000008
  6139. +#define AJB_BITS12 0x00000C
  6140. +#define AJB_BITS16 0x000010
  6141. +#define AJB_BITS20 0x000014
  6142. +#define AJB_BITS24 0x000018
  6143. +#define AJB_BITS28 0x00001C
  6144. +#define AJB_BITS32 0x000020
  6145. +#define AJB_BITS34 0x000022
  6146. +#define AJB_OUT_MS 0x000040
  6147. +#define AJB_OUT_LS 0x000000
  6148. +#define AJB_INV_CLK 0x000080
  6149. +#define AJB_D0_RISE 0x000100
  6150. +#define AJB_D0_FALL 0x000000
  6151. +#define AJB_D1_RISE 0x000200
  6152. +#define AJB_D1_FALL 0x000000
  6153. +#define AJB_IN_RISE 0x000400
  6154. +#define AJB_IN_FALL 0x000000
  6155. +#define AJB_ENABLE 0x000800
  6156. +#define AJB_HOLD0 0x000000
  6157. +#define AJB_HOLD1 0x001000
  6158. +#define AJB_HOLD2 0x002000
  6159. +#define AJB_HOLD3 0x003000
  6160. +#define AJB_RESETN 0x004000
  6161. +#define AJB_CLKSHFT 16
  6162. +#define AJB_BUSY 0x80000000
  6163. +#define AJBTMS HW_REGISTER_RW(AJB_BASE+0x04)
  6164. +#define AJBTDI HW_REGISTER_RW(AJB_BASE+0x08)
  6165. +#define AJBTDO HW_REGISTER_RW(AJB_BASE+0x0c)
  6166. +
  6167. +#endif
  6168. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/arm_power.h linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h
  6169. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/arm_power.h 1969-12-31 18:00:00.000000000 -0600
  6170. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h 2014-12-11 14:02:51.516418001 -0600
  6171. @@ -0,0 +1,62 @@
  6172. +/*
  6173. + * linux/arch/arm/mach-bcm2708/include/mach/arm_power.h
  6174. + *
  6175. + * Copyright (C) 2010 Broadcom
  6176. + *
  6177. + * This program is free software; you can redistribute it and/or modify
  6178. + * it under the terms of the GNU General Public License as published by
  6179. + * the Free Software Foundation; either version 2 of the License, or
  6180. + * (at your option) any later version.
  6181. + *
  6182. + * This program is distributed in the hope that it will be useful,
  6183. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6184. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6185. + * GNU General Public License for more details.
  6186. + *
  6187. + * You should have received a copy of the GNU General Public License
  6188. + * along with this program; if not, write to the Free Software
  6189. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6190. + */
  6191. +
  6192. +#ifndef _ARM_POWER_H
  6193. +#define _ARM_POWER_H
  6194. +
  6195. +/* Use meaningful names on each side */
  6196. +#ifdef __VIDEOCORE__
  6197. +#define PREFIX(x) ARM_##x
  6198. +#else
  6199. +#define PREFIX(x) BCM_##x
  6200. +#endif
  6201. +
  6202. +enum {
  6203. + PREFIX(POWER_SDCARD_BIT),
  6204. + PREFIX(POWER_UART_BIT),
  6205. + PREFIX(POWER_MINIUART_BIT),
  6206. + PREFIX(POWER_USB_BIT),
  6207. + PREFIX(POWER_I2C0_BIT),
  6208. + PREFIX(POWER_I2C1_BIT),
  6209. + PREFIX(POWER_I2C2_BIT),
  6210. + PREFIX(POWER_SPI_BIT),
  6211. + PREFIX(POWER_CCP2TX_BIT),
  6212. + PREFIX(POWER_DSI_BIT),
  6213. +
  6214. + PREFIX(POWER_MAX)
  6215. +};
  6216. +
  6217. +enum {
  6218. + PREFIX(POWER_SDCARD) = (1 << PREFIX(POWER_SDCARD_BIT)),
  6219. + PREFIX(POWER_UART) = (1 << PREFIX(POWER_UART_BIT)),
  6220. + PREFIX(POWER_MINIUART) = (1 << PREFIX(POWER_MINIUART_BIT)),
  6221. + PREFIX(POWER_USB) = (1 << PREFIX(POWER_USB_BIT)),
  6222. + PREFIX(POWER_I2C0) = (1 << PREFIX(POWER_I2C0_BIT)),
  6223. + PREFIX(POWER_I2C1_MASK) = (1 << PREFIX(POWER_I2C1_BIT)),
  6224. + PREFIX(POWER_I2C2_MASK) = (1 << PREFIX(POWER_I2C2_BIT)),
  6225. + PREFIX(POWER_SPI_MASK) = (1 << PREFIX(POWER_SPI_BIT)),
  6226. + PREFIX(POWER_CCP2TX_MASK) = (1 << PREFIX(POWER_CCP2TX_BIT)),
  6227. + PREFIX(POWER_DSI) = (1 << PREFIX(POWER_DSI_BIT)),
  6228. +
  6229. + PREFIX(POWER_MASK) = (1 << PREFIX(POWER_MAX)) - 1,
  6230. + PREFIX(POWER_NONE) = 0
  6231. +};
  6232. +
  6233. +#endif
  6234. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/clkdev.h linux-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h
  6235. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/clkdev.h 1969-12-31 18:00:00.000000000 -0600
  6236. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h 2014-12-11 14:02:51.516418001 -0600
  6237. @@ -0,0 +1,7 @@
  6238. +#ifndef __ASM_MACH_CLKDEV_H
  6239. +#define __ASM_MACH_CLKDEV_H
  6240. +
  6241. +#define __clk_get(clk) ({ 1; })
  6242. +#define __clk_put(clk) do { } while (0)
  6243. +
  6244. +#endif
  6245. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/debug-macro.S linux-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S
  6246. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/debug-macro.S 1969-12-31 18:00:00.000000000 -0600
  6247. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S 2014-12-11 14:02:51.516418001 -0600
  6248. @@ -0,0 +1,22 @@
  6249. +/* arch/arm/mach-bcm2708/include/mach/debug-macro.S
  6250. + *
  6251. + * Debugging macro include header
  6252. + *
  6253. + * Copyright (C) 2010 Broadcom
  6254. + * Copyright (C) 1994-1999 Russell King
  6255. + * Moved from linux/arch/arm/kernel/debug.S by Ben Dooks
  6256. + *
  6257. + * This program is free software; you can redistribute it and/or modify
  6258. + * it under the terms of the GNU General Public License version 2 as
  6259. + * published by the Free Software Foundation.
  6260. + *
  6261. +*/
  6262. +
  6263. +#include <mach/platform.h>
  6264. +
  6265. + .macro addruart, rp, rv, tmp
  6266. + ldr \rp, =UART0_BASE
  6267. + ldr \rv, =IO_ADDRESS(UART0_BASE)
  6268. + .endm
  6269. +
  6270. +#include <debug/pl01x.S>
  6271. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/dma.h linux-rpi/arch/arm/mach-bcm2708/include/mach/dma.h
  6272. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/dma.h 1969-12-31 18:00:00.000000000 -0600
  6273. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/dma.h 2014-12-11 14:02:51.516418001 -0600
  6274. @@ -0,0 +1,94 @@
  6275. +/*
  6276. + * linux/arch/arm/mach-bcm2708/include/mach/dma.h
  6277. + *
  6278. + * Copyright (C) 2010 Broadcom
  6279. + *
  6280. + * This program is free software; you can redistribute it and/or modify
  6281. + * it under the terms of the GNU General Public License version 2 as
  6282. + * published by the Free Software Foundation.
  6283. + */
  6284. +
  6285. +
  6286. +#ifndef _MACH_BCM2708_DMA_H
  6287. +#define _MACH_BCM2708_DMA_H
  6288. +
  6289. +#define BCM_DMAMAN_DRIVER_NAME "bcm2708_dma"
  6290. +
  6291. +/* DMA CS Control and Status bits */
  6292. +#define BCM2708_DMA_ACTIVE (1 << 0)
  6293. +#define BCM2708_DMA_INT (1 << 2)
  6294. +#define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
  6295. +#define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
  6296. +#define BCM2708_DMA_ERR (1 << 8)
  6297. +#define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
  6298. +#define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
  6299. +
  6300. +/* DMA control block "info" field bits */
  6301. +#define BCM2708_DMA_INT_EN (1 << 0)
  6302. +#define BCM2708_DMA_TDMODE (1 << 1)
  6303. +#define BCM2708_DMA_WAIT_RESP (1 << 3)
  6304. +#define BCM2708_DMA_D_INC (1 << 4)
  6305. +#define BCM2708_DMA_D_WIDTH (1 << 5)
  6306. +#define BCM2708_DMA_D_DREQ (1 << 6)
  6307. +#define BCM2708_DMA_S_INC (1 << 8)
  6308. +#define BCM2708_DMA_S_WIDTH (1 << 9)
  6309. +#define BCM2708_DMA_S_DREQ (1 << 10)
  6310. +
  6311. +#define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
  6312. +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
  6313. +#define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
  6314. +
  6315. +#define BCM2708_DMA_DREQ_EMMC 11
  6316. +#define BCM2708_DMA_DREQ_SDHOST 13
  6317. +
  6318. +#define BCM2708_DMA_CS 0x00 /* Control and Status */
  6319. +#define BCM2708_DMA_ADDR 0x04
  6320. +/* the current control block appears in the following registers - read only */
  6321. +#define BCM2708_DMA_INFO 0x08
  6322. +#define BCM2708_DMA_SOURCE_AD 0x0c
  6323. +#define BCM2708_DMA_DEST_AD 0x10
  6324. +#define BCM2708_DMA_NEXTCB 0x1C
  6325. +#define BCM2708_DMA_DEBUG 0x20
  6326. +
  6327. +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
  6328. +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
  6329. +
  6330. +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
  6331. +
  6332. +struct bcm2708_dma_cb {
  6333. + unsigned long info;
  6334. + unsigned long src;
  6335. + unsigned long dst;
  6336. + unsigned long length;
  6337. + unsigned long stride;
  6338. + unsigned long next;
  6339. + unsigned long pad[2];
  6340. +};
  6341. +struct scatterlist;
  6342. +
  6343. +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
  6344. +extern void bcm_dma_start(void __iomem *dma_chan_base,
  6345. + dma_addr_t control_block);
  6346. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base);
  6347. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base);
  6348. +extern int /*rc*/ bcm_dma_abort(void __iomem *dma_chan_base);
  6349. +
  6350. +/* When listing features we can ask for when allocating DMA channels give
  6351. + those with higher priority smaller ordinal numbers */
  6352. +#define BCM_DMA_FEATURE_FAST_ORD 0
  6353. +#define BCM_DMA_FEATURE_BULK_ORD 1
  6354. +#define BCM_DMA_FEATURE_NORMAL_ORD 2
  6355. +#define BCM_DMA_FEATURE_LITE_ORD 3
  6356. +#define BCM_DMA_FEATURE_FAST (1<<BCM_DMA_FEATURE_FAST_ORD)
  6357. +#define BCM_DMA_FEATURE_BULK (1<<BCM_DMA_FEATURE_BULK_ORD)
  6358. +#define BCM_DMA_FEATURE_NORMAL (1<<BCM_DMA_FEATURE_NORMAL_ORD)
  6359. +#define BCM_DMA_FEATURE_LITE (1<<BCM_DMA_FEATURE_LITE_ORD)
  6360. +#define BCM_DMA_FEATURE_COUNT 4
  6361. +
  6362. +/* return channel no or -ve error */
  6363. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  6364. + void __iomem **out_dma_base, int *out_dma_irq);
  6365. +extern int bcm_dma_chan_free(int channel);
  6366. +
  6367. +
  6368. +#endif /* _MACH_BCM2708_DMA_H */
  6369. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/entry-macro.S linux-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S
  6370. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/entry-macro.S 1969-12-31 18:00:00.000000000 -0600
  6371. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S 2014-12-11 14:02:51.516418001 -0600
  6372. @@ -0,0 +1,69 @@
  6373. +/*
  6374. + * arch/arm/mach-bcm2708/include/mach/entry-macro.S
  6375. + *
  6376. + * Low-level IRQ helper macros for BCM2708 platforms
  6377. + *
  6378. + * Copyright (C) 2010 Broadcom
  6379. + *
  6380. + * This program is free software; you can redistribute it and/or modify
  6381. + * it under the terms of the GNU General Public License as published by
  6382. + * the Free Software Foundation; either version 2 of the License, or
  6383. + * (at your option) any later version.
  6384. + *
  6385. + * This program is distributed in the hope that it will be useful,
  6386. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6387. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6388. + * GNU General Public License for more details.
  6389. + *
  6390. + * You should have received a copy of the GNU General Public License
  6391. + * along with this program; if not, write to the Free Software
  6392. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6393. + */
  6394. +#include <mach/hardware.h>
  6395. +
  6396. + .macro disable_fiq
  6397. + .endm
  6398. +
  6399. + .macro get_irqnr_preamble, base, tmp
  6400. + ldr \base, =IO_ADDRESS(ARMCTRL_IC_BASE)
  6401. + .endm
  6402. +
  6403. + .macro arch_ret_to_user, tmp1, tmp2
  6404. + .endm
  6405. +
  6406. + .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
  6407. + /* get masked status */
  6408. + ldr \irqstat, [\base, #(ARM_IRQ_PEND0 - ARMCTRL_IC_BASE)]
  6409. + mov \irqnr, #(ARM_IRQ0_BASE + 31)
  6410. + and \tmp, \irqstat, #0x300 @ save bits 8 and 9
  6411. + /* clear bits 8 and 9, and test */
  6412. + bics \irqstat, \irqstat, #0x300
  6413. + bne 1010f
  6414. +
  6415. + tst \tmp, #0x100
  6416. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND1 - ARMCTRL_IC_BASE)]
  6417. + movne \irqnr, #(ARM_IRQ1_BASE + 31)
  6418. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  6419. + bicne \irqstat, #((1<<7) | (1<<9) | (1<<10))
  6420. + bicne \irqstat, #((1<<18) | (1<<19))
  6421. + bne 1010f
  6422. +
  6423. + tst \tmp, #0x200
  6424. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND2 - ARMCTRL_IC_BASE)]
  6425. + movne \irqnr, #(ARM_IRQ2_BASE + 31)
  6426. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  6427. + bicne \irqstat, #((1<<21) | (1<<22) | (1<<23) | (1<<24) | (1<<25))
  6428. + bicne \irqstat, #((1<<30))
  6429. + beq 1020f
  6430. +
  6431. +1010:
  6432. + @ For non-zero x, LSB(x) = 31 - CLZ(x^(x-1))
  6433. + @ N.B. CLZ is an ARM5 instruction.
  6434. + sub \tmp, \irqstat, #1
  6435. + eor \irqstat, \irqstat, \tmp
  6436. + clz \tmp, \irqstat
  6437. + sub \irqnr, \tmp
  6438. +
  6439. +1020: @ EQ will be set if no irqs pending
  6440. +
  6441. + .endm
  6442. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/frc.h linux-rpi/arch/arm/mach-bcm2708/include/mach/frc.h
  6443. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/frc.h 1969-12-31 18:00:00.000000000 -0600
  6444. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/frc.h 2014-12-11 14:02:51.516418001 -0600
  6445. @@ -0,0 +1,38 @@
  6446. +/*
  6447. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6448. + *
  6449. + * BCM2708 free running counter (timer)
  6450. + *
  6451. + * Copyright (C) 2010 Broadcom
  6452. + *
  6453. + * This program is free software; you can redistribute it and/or modify
  6454. + * it under the terms of the GNU General Public License as published by
  6455. + * the Free Software Foundation; either version 2 of the License, or
  6456. + * (at your option) any later version.
  6457. + *
  6458. + * This program is distributed in the hope that it will be useful,
  6459. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6460. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6461. + * GNU General Public License for more details.
  6462. + *
  6463. + * You should have received a copy of the GNU General Public License
  6464. + * along with this program; if not, write to the Free Software
  6465. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6466. + */
  6467. +
  6468. +#ifndef _MACH_FRC_H
  6469. +#define _MACH_FRC_H
  6470. +
  6471. +#define FRC_TICK_RATE (1000000)
  6472. +
  6473. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  6474. + (slightly faster than frc_clock_ticks63()
  6475. + */
  6476. +extern unsigned long frc_clock_ticks32(void);
  6477. +
  6478. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  6479. + * Note - top bit should be ignored (see cnt32_to_63)
  6480. + */
  6481. +extern unsigned long long frc_clock_ticks63(void);
  6482. +
  6483. +#endif
  6484. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/gpio.h linux-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h
  6485. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/gpio.h 1969-12-31 18:00:00.000000000 -0600
  6486. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h 2014-12-11 14:02:51.516418001 -0600
  6487. @@ -0,0 +1,17 @@
  6488. +/*
  6489. + * arch/arm/mach-bcm2708/include/mach/gpio.h
  6490. + *
  6491. + * This file is licensed under the terms of the GNU General Public
  6492. + * License version 2. This program is licensed "as is" without any
  6493. + * warranty of any kind, whether express or implied.
  6494. + */
  6495. +
  6496. +#ifndef __ASM_ARCH_GPIO_H
  6497. +#define __ASM_ARCH_GPIO_H
  6498. +
  6499. +#define BCM2708_NR_GPIOS 54 // number of gpio lines
  6500. +
  6501. +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
  6502. +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
  6503. +
  6504. +#endif
  6505. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/hardware.h linux-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h
  6506. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/hardware.h 1969-12-31 18:00:00.000000000 -0600
  6507. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h 2014-12-11 14:02:51.516418001 -0600
  6508. @@ -0,0 +1,28 @@
  6509. +/*
  6510. + * arch/arm/mach-bcm2708/include/mach/hardware.h
  6511. + *
  6512. + * This file contains the hardware definitions of the BCM2708 devices.
  6513. + *
  6514. + * Copyright (C) 2010 Broadcom
  6515. + *
  6516. + * This program is free software; you can redistribute it and/or modify
  6517. + * it under the terms of the GNU General Public License as published by
  6518. + * the Free Software Foundation; either version 2 of the License, or
  6519. + * (at your option) any later version.
  6520. + *
  6521. + * This program is distributed in the hope that it will be useful,
  6522. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6523. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6524. + * GNU General Public License for more details.
  6525. + *
  6526. + * You should have received a copy of the GNU General Public License
  6527. + * along with this program; if not, write to the Free Software
  6528. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6529. + */
  6530. +#ifndef __ASM_ARCH_HARDWARE_H
  6531. +#define __ASM_ARCH_HARDWARE_H
  6532. +
  6533. +#include <asm/sizes.h>
  6534. +#include <mach/platform.h>
  6535. +
  6536. +#endif
  6537. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/io.h linux-rpi/arch/arm/mach-bcm2708/include/mach/io.h
  6538. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/io.h 1969-12-31 18:00:00.000000000 -0600
  6539. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/io.h 2014-12-11 14:02:51.516418001 -0600
  6540. @@ -0,0 +1,27 @@
  6541. +/*
  6542. + * arch/arm/mach-bcm2708/include/mach/io.h
  6543. + *
  6544. + * Copyright (C) 2003 ARM Limited
  6545. + *
  6546. + * This program is free software; you can redistribute it and/or modify
  6547. + * it under the terms of the GNU General Public License as published by
  6548. + * the Free Software Foundation; either version 2 of the License, or
  6549. + * (at your option) any later version.
  6550. + *
  6551. + * This program is distributed in the hope that it will be useful,
  6552. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6553. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6554. + * GNU General Public License for more details.
  6555. + *
  6556. + * You should have received a copy of the GNU General Public License
  6557. + * along with this program; if not, write to the Free Software
  6558. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6559. + */
  6560. +#ifndef __ASM_ARM_ARCH_IO_H
  6561. +#define __ASM_ARM_ARCH_IO_H
  6562. +
  6563. +#define IO_SPACE_LIMIT 0xffffffff
  6564. +
  6565. +#define __io(a) __typesafe_io(a)
  6566. +
  6567. +#endif
  6568. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/irqs.h linux-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h
  6569. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/irqs.h 1969-12-31 18:00:00.000000000 -0600
  6570. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h 2014-12-11 14:02:51.516418001 -0600
  6571. @@ -0,0 +1,197 @@
  6572. +/*
  6573. + * arch/arm/mach-bcm2708/include/mach/irqs.h
  6574. + *
  6575. + * Copyright (C) 2010 Broadcom
  6576. + * Copyright (C) 2003 ARM Limited
  6577. + * Copyright (C) 2000 Deep Blue Solutions Ltd.
  6578. + *
  6579. + * This program is free software; you can redistribute it and/or modify
  6580. + * it under the terms of the GNU General Public License as published by
  6581. + * the Free Software Foundation; either version 2 of the License, or
  6582. + * (at your option) any later version.
  6583. + *
  6584. + * This program is distributed in the hope that it will be useful,
  6585. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6586. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6587. + * GNU General Public License for more details.
  6588. + *
  6589. + * You should have received a copy of the GNU General Public License
  6590. + * along with this program; if not, write to the Free Software
  6591. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6592. + */
  6593. +
  6594. +#ifndef _BCM2708_IRQS_H_
  6595. +#define _BCM2708_IRQS_H_
  6596. +
  6597. +#include <mach/platform.h>
  6598. +
  6599. +/*
  6600. + * IRQ interrupts definitions are the same as the INT definitions
  6601. + * held within platform.h
  6602. + */
  6603. +#define IRQ_ARMCTRL_START 0
  6604. +#define IRQ_TIMER0 (IRQ_ARMCTRL_START + INTERRUPT_TIMER0)
  6605. +#define IRQ_TIMER1 (IRQ_ARMCTRL_START + INTERRUPT_TIMER1)
  6606. +#define IRQ_TIMER2 (IRQ_ARMCTRL_START + INTERRUPT_TIMER2)
  6607. +#define IRQ_TIMER3 (IRQ_ARMCTRL_START + INTERRUPT_TIMER3)
  6608. +#define IRQ_CODEC0 (IRQ_ARMCTRL_START + INTERRUPT_CODEC0)
  6609. +#define IRQ_CODEC1 (IRQ_ARMCTRL_START + INTERRUPT_CODEC1)
  6610. +#define IRQ_CODEC2 (IRQ_ARMCTRL_START + INTERRUPT_CODEC2)
  6611. +#define IRQ_JPEG (IRQ_ARMCTRL_START + INTERRUPT_JPEG)
  6612. +#define IRQ_ISP (IRQ_ARMCTRL_START + INTERRUPT_ISP)
  6613. +#define IRQ_USB (IRQ_ARMCTRL_START + INTERRUPT_USB)
  6614. +#define IRQ_3D (IRQ_ARMCTRL_START + INTERRUPT_3D)
  6615. +#define IRQ_TRANSPOSER (IRQ_ARMCTRL_START + INTERRUPT_TRANSPOSER)
  6616. +#define IRQ_MULTICORESYNC0 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC0)
  6617. +#define IRQ_MULTICORESYNC1 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC1)
  6618. +#define IRQ_MULTICORESYNC2 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC2)
  6619. +#define IRQ_MULTICORESYNC3 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC3)
  6620. +#define IRQ_DMA0 (IRQ_ARMCTRL_START + INTERRUPT_DMA0)
  6621. +#define IRQ_DMA1 (IRQ_ARMCTRL_START + INTERRUPT_DMA1)
  6622. +#define IRQ_DMA2 (IRQ_ARMCTRL_START + INTERRUPT_DMA2)
  6623. +#define IRQ_DMA3 (IRQ_ARMCTRL_START + INTERRUPT_DMA3)
  6624. +#define IRQ_DMA4 (IRQ_ARMCTRL_START + INTERRUPT_DMA4)
  6625. +#define IRQ_DMA5 (IRQ_ARMCTRL_START + INTERRUPT_DMA5)
  6626. +#define IRQ_DMA6 (IRQ_ARMCTRL_START + INTERRUPT_DMA6)
  6627. +#define IRQ_DMA7 (IRQ_ARMCTRL_START + INTERRUPT_DMA7)
  6628. +#define IRQ_DMA8 (IRQ_ARMCTRL_START + INTERRUPT_DMA8)
  6629. +#define IRQ_DMA9 (IRQ_ARMCTRL_START + INTERRUPT_DMA9)
  6630. +#define IRQ_DMA10 (IRQ_ARMCTRL_START + INTERRUPT_DMA10)
  6631. +#define IRQ_DMA11 (IRQ_ARMCTRL_START + INTERRUPT_DMA11)
  6632. +#define IRQ_DMA12 (IRQ_ARMCTRL_START + INTERRUPT_DMA12)
  6633. +#define IRQ_AUX (IRQ_ARMCTRL_START + INTERRUPT_AUX)
  6634. +#define IRQ_ARM (IRQ_ARMCTRL_START + INTERRUPT_ARM)
  6635. +#define IRQ_VPUDMA (IRQ_ARMCTRL_START + INTERRUPT_VPUDMA)
  6636. +#define IRQ_HOSTPORT (IRQ_ARMCTRL_START + INTERRUPT_HOSTPORT)
  6637. +#define IRQ_VIDEOSCALER (IRQ_ARMCTRL_START + INTERRUPT_VIDEOSCALER)
  6638. +#define IRQ_CCP2TX (IRQ_ARMCTRL_START + INTERRUPT_CCP2TX)
  6639. +#define IRQ_SDC (IRQ_ARMCTRL_START + INTERRUPT_SDC)
  6640. +#define IRQ_DSI0 (IRQ_ARMCTRL_START + INTERRUPT_DSI0)
  6641. +#define IRQ_AVE (IRQ_ARMCTRL_START + INTERRUPT_AVE)
  6642. +#define IRQ_CAM0 (IRQ_ARMCTRL_START + INTERRUPT_CAM0)
  6643. +#define IRQ_CAM1 (IRQ_ARMCTRL_START + INTERRUPT_CAM1)
  6644. +#define IRQ_HDMI0 (IRQ_ARMCTRL_START + INTERRUPT_HDMI0)
  6645. +#define IRQ_HDMI1 (IRQ_ARMCTRL_START + INTERRUPT_HDMI1)
  6646. +#define IRQ_PIXELVALVE1 (IRQ_ARMCTRL_START + INTERRUPT_PIXELVALVE1)
  6647. +#define IRQ_I2CSPISLV (IRQ_ARMCTRL_START + INTERRUPT_I2CSPISLV)
  6648. +#define IRQ_DSI1 (IRQ_ARMCTRL_START + INTERRUPT_DSI1)
  6649. +#define IRQ_PWA0 (IRQ_ARMCTRL_START + INTERRUPT_PWA0)
  6650. +#define IRQ_PWA1 (IRQ_ARMCTRL_START + INTERRUPT_PWA1)
  6651. +#define IRQ_CPR (IRQ_ARMCTRL_START + INTERRUPT_CPR)
  6652. +#define IRQ_SMI (IRQ_ARMCTRL_START + INTERRUPT_SMI)
  6653. +#define IRQ_GPIO0 (IRQ_ARMCTRL_START + INTERRUPT_GPIO0)
  6654. +#define IRQ_GPIO1 (IRQ_ARMCTRL_START + INTERRUPT_GPIO1)
  6655. +#define IRQ_GPIO2 (IRQ_ARMCTRL_START + INTERRUPT_GPIO2)
  6656. +#define IRQ_GPIO3 (IRQ_ARMCTRL_START + INTERRUPT_GPIO3)
  6657. +#define IRQ_I2C (IRQ_ARMCTRL_START + INTERRUPT_I2C)
  6658. +#define IRQ_SPI (IRQ_ARMCTRL_START + INTERRUPT_SPI)
  6659. +#define IRQ_I2SPCM (IRQ_ARMCTRL_START + INTERRUPT_I2SPCM)
  6660. +#define IRQ_SDIO (IRQ_ARMCTRL_START + INTERRUPT_SDIO)
  6661. +#define IRQ_UART (IRQ_ARMCTRL_START + INTERRUPT_UART)
  6662. +#define IRQ_SLIMBUS (IRQ_ARMCTRL_START + INTERRUPT_SLIMBUS)
  6663. +#define IRQ_VEC (IRQ_ARMCTRL_START + INTERRUPT_VEC)
  6664. +#define IRQ_CPG (IRQ_ARMCTRL_START + INTERRUPT_CPG)
  6665. +#define IRQ_RNG (IRQ_ARMCTRL_START + INTERRUPT_RNG)
  6666. +#define IRQ_ARASANSDIO (IRQ_ARMCTRL_START + INTERRUPT_ARASANSDIO)
  6667. +#define IRQ_AVSPMON (IRQ_ARMCTRL_START + INTERRUPT_AVSPMON)
  6668. +
  6669. +#define IRQ_ARM_TIMER (IRQ_ARMCTRL_START + INTERRUPT_ARM_TIMER)
  6670. +#define IRQ_ARM_MAILBOX (IRQ_ARMCTRL_START + INTERRUPT_ARM_MAILBOX)
  6671. +#define IRQ_ARM_DOORBELL_0 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_0)
  6672. +#define IRQ_ARM_DOORBELL_1 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_1)
  6673. +#define IRQ_VPU0_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU0_HALTED)
  6674. +#define IRQ_VPU1_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU1_HALTED)
  6675. +#define IRQ_ILLEGAL_TYPE0 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE0)
  6676. +#define IRQ_ILLEGAL_TYPE1 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE1)
  6677. +#define IRQ_PENDING1 (IRQ_ARMCTRL_START + INTERRUPT_PENDING1)
  6678. +#define IRQ_PENDING2 (IRQ_ARMCTRL_START + INTERRUPT_PENDING2)
  6679. +
  6680. +#define FIQ_START HARD_IRQS
  6681. +
  6682. +/*
  6683. + * FIQ interrupts definitions are the same as the INT definitions.
  6684. + */
  6685. +#define FIQ_TIMER0 (FIQ_START+INTERRUPT_TIMER0)
  6686. +#define FIQ_TIMER1 (FIQ_START+INTERRUPT_TIMER1)
  6687. +#define FIQ_TIMER2 (FIQ_START+INTERRUPT_TIMER2)
  6688. +#define FIQ_TIMER3 (FIQ_START+INTERRUPT_TIMER3)
  6689. +#define FIQ_CODEC0 (FIQ_START+INTERRUPT_CODEC0)
  6690. +#define FIQ_CODEC1 (FIQ_START+INTERRUPT_CODEC1)
  6691. +#define FIQ_CODEC2 (FIQ_START+INTERRUPT_CODEC2)
  6692. +#define FIQ_JPEG (FIQ_START+INTERRUPT_JPEG)
  6693. +#define FIQ_ISP (FIQ_START+INTERRUPT_ISP)
  6694. +#define FIQ_USB (FIQ_START+INTERRUPT_USB)
  6695. +#define FIQ_3D (FIQ_START+INTERRUPT_3D)
  6696. +#define FIQ_TRANSPOSER (FIQ_START+INTERRUPT_TRANSPOSER)
  6697. +#define FIQ_MULTICORESYNC0 (FIQ_START+INTERRUPT_MULTICORESYNC0)
  6698. +#define FIQ_MULTICORESYNC1 (FIQ_START+INTERRUPT_MULTICORESYNC1)
  6699. +#define FIQ_MULTICORESYNC2 (FIQ_START+INTERRUPT_MULTICORESYNC2)
  6700. +#define FIQ_MULTICORESYNC3 (FIQ_START+INTERRUPT_MULTICORESYNC3)
  6701. +#define FIQ_DMA0 (FIQ_START+INTERRUPT_DMA0)
  6702. +#define FIQ_DMA1 (FIQ_START+INTERRUPT_DMA1)
  6703. +#define FIQ_DMA2 (FIQ_START+INTERRUPT_DMA2)
  6704. +#define FIQ_DMA3 (FIQ_START+INTERRUPT_DMA3)
  6705. +#define FIQ_DMA4 (FIQ_START+INTERRUPT_DMA4)
  6706. +#define FIQ_DMA5 (FIQ_START+INTERRUPT_DMA5)
  6707. +#define FIQ_DMA6 (FIQ_START+INTERRUPT_DMA6)
  6708. +#define FIQ_DMA7 (FIQ_START+INTERRUPT_DMA7)
  6709. +#define FIQ_DMA8 (FIQ_START+INTERRUPT_DMA8)
  6710. +#define FIQ_DMA9 (FIQ_START+INTERRUPT_DMA9)
  6711. +#define FIQ_DMA10 (FIQ_START+INTERRUPT_DMA10)
  6712. +#define FIQ_DMA11 (FIQ_START+INTERRUPT_DMA11)
  6713. +#define FIQ_DMA12 (FIQ_START+INTERRUPT_DMA12)
  6714. +#define FIQ_AUX (FIQ_START+INTERRUPT_AUX)
  6715. +#define FIQ_ARM (FIQ_START+INTERRUPT_ARM)
  6716. +#define FIQ_VPUDMA (FIQ_START+INTERRUPT_VPUDMA)
  6717. +#define FIQ_HOSTPORT (FIQ_START+INTERRUPT_HOSTPORT)
  6718. +#define FIQ_VIDEOSCALER (FIQ_START+INTERRUPT_VIDEOSCALER)
  6719. +#define FIQ_CCP2TX (FIQ_START+INTERRUPT_CCP2TX)
  6720. +#define FIQ_SDC (FIQ_START+INTERRUPT_SDC)
  6721. +#define FIQ_DSI0 (FIQ_START+INTERRUPT_DSI0)
  6722. +#define FIQ_AVE (FIQ_START+INTERRUPT_AVE)
  6723. +#define FIQ_CAM0 (FIQ_START+INTERRUPT_CAM0)
  6724. +#define FIQ_CAM1 (FIQ_START+INTERRUPT_CAM1)
  6725. +#define FIQ_HDMI0 (FIQ_START+INTERRUPT_HDMI0)
  6726. +#define FIQ_HDMI1 (FIQ_START+INTERRUPT_HDMI1)
  6727. +#define FIQ_PIXELVALVE1 (FIQ_START+INTERRUPT_PIXELVALVE1)
  6728. +#define FIQ_I2CSPISLV (FIQ_START+INTERRUPT_I2CSPISLV)
  6729. +#define FIQ_DSI1 (FIQ_START+INTERRUPT_DSI1)
  6730. +#define FIQ_PWA0 (FIQ_START+INTERRUPT_PWA0)
  6731. +#define FIQ_PWA1 (FIQ_START+INTERRUPT_PWA1)
  6732. +#define FIQ_CPR (FIQ_START+INTERRUPT_CPR)
  6733. +#define FIQ_SMI (FIQ_START+INTERRUPT_SMI)
  6734. +#define FIQ_GPIO0 (FIQ_START+INTERRUPT_GPIO0)
  6735. +#define FIQ_GPIO1 (FIQ_START+INTERRUPT_GPIO1)
  6736. +#define FIQ_GPIO2 (FIQ_START+INTERRUPT_GPIO2)
  6737. +#define FIQ_GPIO3 (FIQ_START+INTERRUPT_GPIO3)
  6738. +#define FIQ_I2C (FIQ_START+INTERRUPT_I2C)
  6739. +#define FIQ_SPI (FIQ_START+INTERRUPT_SPI)
  6740. +#define FIQ_I2SPCM (FIQ_START+INTERRUPT_I2SPCM)
  6741. +#define FIQ_SDIO (FIQ_START+INTERRUPT_SDIO)
  6742. +#define FIQ_UART (FIQ_START+INTERRUPT_UART)
  6743. +#define FIQ_SLIMBUS (FIQ_START+INTERRUPT_SLIMBUS)
  6744. +#define FIQ_VEC (FIQ_START+INTERRUPT_VEC)
  6745. +#define FIQ_CPG (FIQ_START+INTERRUPT_CPG)
  6746. +#define FIQ_RNG (FIQ_START+INTERRUPT_RNG)
  6747. +#define FIQ_ARASANSDIO (FIQ_START+INTERRUPT_ARASANSDIO)
  6748. +#define FIQ_AVSPMON (FIQ_START+INTERRUPT_AVSPMON)
  6749. +
  6750. +#define FIQ_ARM_TIMER (FIQ_START+INTERRUPT_ARM_TIMER)
  6751. +#define FIQ_ARM_MAILBOX (FIQ_START+INTERRUPT_ARM_MAILBOX)
  6752. +#define FIQ_ARM_DOORBELL_0 (FIQ_START+INTERRUPT_ARM_DOORBELL_0)
  6753. +#define FIQ_ARM_DOORBELL_1 (FIQ_START+INTERRUPT_ARM_DOORBELL_1)
  6754. +#define FIQ_VPU0_HALTED (FIQ_START+INTERRUPT_VPU0_HALTED)
  6755. +#define FIQ_VPU1_HALTED (FIQ_START+INTERRUPT_VPU1_HALTED)
  6756. +#define FIQ_ILLEGAL_TYPE0 (FIQ_START+INTERRUPT_ILLEGAL_TYPE0)
  6757. +#define FIQ_ILLEGAL_TYPE1 (FIQ_START+INTERRUPT_ILLEGAL_TYPE1)
  6758. +#define FIQ_PENDING1 (FIQ_START+INTERRUPT_PENDING1)
  6759. +#define FIQ_PENDING2 (FIQ_START+INTERRUPT_PENDING2)
  6760. +
  6761. +#define HARD_IRQS (64 + 21)
  6762. +#define FIQ_IRQS (64 + 21)
  6763. +#define GPIO_IRQ_START (HARD_IRQS + FIQ_IRQS)
  6764. +#define GPIO_IRQS (32*5)
  6765. +#define SPARE_IRQS (64)
  6766. +#define NR_IRQS (HARD_IRQS+FIQ_IRQS+GPIO_IRQS+SPARE_IRQS)
  6767. +
  6768. +#endif /* _BCM2708_IRQS_H_ */
  6769. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/memory.h linux-rpi/arch/arm/mach-bcm2708/include/mach/memory.h
  6770. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/memory.h 1969-12-31 18:00:00.000000000 -0600
  6771. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/memory.h 2014-12-11 14:05:36.828418001 -0600
  6772. @@ -0,0 +1,57 @@
  6773. +/*
  6774. + * arch/arm/mach-bcm2708/include/mach/memory.h
  6775. + *
  6776. + * Copyright (C) 2010 Broadcom
  6777. + *
  6778. + * This program is free software; you can redistribute it and/or modify
  6779. + * it under the terms of the GNU General Public License as published by
  6780. + * the Free Software Foundation; either version 2 of the License, or
  6781. + * (at your option) any later version.
  6782. + *
  6783. + * This program is distributed in the hope that it will be useful,
  6784. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6785. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6786. + * GNU General Public License for more details.
  6787. + *
  6788. + * You should have received a copy of the GNU General Public License
  6789. + * along with this program; if not, write to the Free Software
  6790. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6791. + */
  6792. +#ifndef __ASM_ARCH_MEMORY_H
  6793. +#define __ASM_ARCH_MEMORY_H
  6794. +
  6795. +/* Memory overview:
  6796. +
  6797. + [ARMcore] <--virtual addr-->
  6798. + [ARMmmu] <--physical addr-->
  6799. + [GERTmap] <--bus add-->
  6800. + [VCperiph]
  6801. +
  6802. +*/
  6803. +
  6804. +/*
  6805. + * Physical DRAM offset.
  6806. + */
  6807. +#define BCM_PLAT_PHYS_OFFSET UL(0x00000000)
  6808. +#define VC_ARMMEM_OFFSET UL(0x00000000) /* offset in VC of ARM memory */
  6809. +
  6810. +#ifdef CONFIG_BCM2708_NOL2CACHE
  6811. + #define _REAL_BUS_OFFSET UL(0xC0000000) /* don't use L1 or L2 caches */
  6812. +#else
  6813. + #define _REAL_BUS_OFFSET UL(0x40000000) /* use L2 cache */
  6814. +#endif
  6815. +
  6816. +/* We're using the memory at 64M in the VideoCore for Linux - this adjustment
  6817. + * will provide the offset into this area as well as setting the bits that
  6818. + * stop the L1 and L2 cache from being used
  6819. + *
  6820. + * WARNING: this only works because the ARM is given memory at a fixed location
  6821. + * (ARMMEM_OFFSET)
  6822. + */
  6823. +#define BUS_OFFSET (VC_ARMMEM_OFFSET + _REAL_BUS_OFFSET)
  6824. +#define __virt_to_bus(x) ((x) + (BUS_OFFSET - PAGE_OFFSET))
  6825. +#define __bus_to_virt(x) ((x) - (BUS_OFFSET - PAGE_OFFSET))
  6826. +#define __pfn_to_bus(x) (__pfn_to_phys(x) + (BUS_OFFSET - BCM_PLAT_PHYS_OFFSET))
  6827. +#define __bus_to_pfn(x) __phys_to_pfn((x) - (BUS_OFFSET - BCM_PLAT_PHYS_OFFSET))
  6828. +
  6829. +#endif
  6830. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/platform.h linux-rpi/arch/arm/mach-bcm2708/include/mach/platform.h
  6831. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/platform.h 1969-12-31 18:00:00.000000000 -0600
  6832. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/platform.h 2014-12-11 14:02:51.516418001 -0600
  6833. @@ -0,0 +1,228 @@
  6834. +/*
  6835. + * arch/arm/mach-bcm2708/include/mach/platform.h
  6836. + *
  6837. + * Copyright (C) 2010 Broadcom
  6838. + *
  6839. + * This program is free software; you can redistribute it and/or modify
  6840. + * it under the terms of the GNU General Public License as published by
  6841. + * the Free Software Foundation; either version 2 of the License, or
  6842. + * (at your option) any later version.
  6843. + *
  6844. + * This program is distributed in the hope that it will be useful,
  6845. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6846. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6847. + * GNU General Public License for more details.
  6848. + *
  6849. + * You should have received a copy of the GNU General Public License
  6850. + * along with this program; if not, write to the Free Software
  6851. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6852. + */
  6853. +
  6854. +#ifndef _BCM2708_PLATFORM_H
  6855. +#define _BCM2708_PLATFORM_H
  6856. +
  6857. +
  6858. +/* macros to get at IO space when running virtually */
  6859. +#define IO_ADDRESS(x) (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  6860. +
  6861. +#define __io_address(n) IOMEM(IO_ADDRESS(n))
  6862. +
  6863. +
  6864. +/*
  6865. + * SDRAM
  6866. + */
  6867. +#define BCM2708_SDRAM_BASE 0x00000000
  6868. +
  6869. +/*
  6870. + * Logic expansion modules
  6871. + *
  6872. + */
  6873. +
  6874. +
  6875. +/* ------------------------------------------------------------------------
  6876. + * BCM2708 ARMCTRL Registers
  6877. + * ------------------------------------------------------------------------
  6878. + */
  6879. +
  6880. +#define HW_REGISTER_RW(addr) (addr)
  6881. +#define HW_REGISTER_RO(addr) (addr)
  6882. +
  6883. +#include "arm_control.h"
  6884. +#undef ARM_BASE
  6885. +
  6886. +/*
  6887. + * Definitions and addresses for the ARM CONTROL logic
  6888. + * This file is manually generated.
  6889. + */
  6890. +
  6891. +#define BCM2708_PERI_BASE 0x20000000
  6892. +#define IC0_BASE (BCM2708_PERI_BASE + 0x2000)
  6893. +#define ST_BASE (BCM2708_PERI_BASE + 0x3000) /* System Timer */
  6894. +#define MPHI_BASE (BCM2708_PERI_BASE + 0x6000) /* Message -based Parallel Host Interface */
  6895. +#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
  6896. +#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
  6897. +#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
  6898. +#define PCM_CLOCK_BASE (BCM2708_PERI_BASE + 0x101098) /* PCM Clock */
  6899. +#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
  6900. +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
  6901. +#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
  6902. +#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
  6903. +#define I2S_BASE (BCM2708_PERI_BASE + 0x203000) /* I2S */
  6904. +#define SPI0_BASE (BCM2708_PERI_BASE + 0x204000) /* SPI0 */
  6905. +#define BSC0_BASE (BCM2708_PERI_BASE + 0x205000) /* BSC0 I2C/TWI */
  6906. +#define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */
  6907. +#define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */
  6908. +#define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */
  6909. +#define BSC1_BASE (BCM2708_PERI_BASE + 0x804000) /* BSC1 I2C/TWI */
  6910. +#define USB_BASE (BCM2708_PERI_BASE + 0x980000) /* DTC_OTG USB controller */
  6911. +#define MCORE_BASE (BCM2708_PERI_BASE + 0x0000) /* Fake frame buffer device (actually the multicore sync block*/
  6912. +
  6913. +#define ARMCTRL_BASE (ARM_BASE + 0x000)
  6914. +#define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  6915. +#define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  6916. +#define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  6917. +
  6918. +
  6919. +/*
  6920. + * Interrupt assignments
  6921. + */
  6922. +
  6923. +#define ARM_IRQ1_BASE 0
  6924. +#define INTERRUPT_TIMER0 (ARM_IRQ1_BASE + 0)
  6925. +#define INTERRUPT_TIMER1 (ARM_IRQ1_BASE + 1)
  6926. +#define INTERRUPT_TIMER2 (ARM_IRQ1_BASE + 2)
  6927. +#define INTERRUPT_TIMER3 (ARM_IRQ1_BASE + 3)
  6928. +#define INTERRUPT_CODEC0 (ARM_IRQ1_BASE + 4)
  6929. +#define INTERRUPT_CODEC1 (ARM_IRQ1_BASE + 5)
  6930. +#define INTERRUPT_CODEC2 (ARM_IRQ1_BASE + 6)
  6931. +#define INTERRUPT_VC_JPEG (ARM_IRQ1_BASE + 7)
  6932. +#define INTERRUPT_ISP (ARM_IRQ1_BASE + 8)
  6933. +#define INTERRUPT_VC_USB (ARM_IRQ1_BASE + 9)
  6934. +#define INTERRUPT_VC_3D (ARM_IRQ1_BASE + 10)
  6935. +#define INTERRUPT_TRANSPOSER (ARM_IRQ1_BASE + 11)
  6936. +#define INTERRUPT_MULTICORESYNC0 (ARM_IRQ1_BASE + 12)
  6937. +#define INTERRUPT_MULTICORESYNC1 (ARM_IRQ1_BASE + 13)
  6938. +#define INTERRUPT_MULTICORESYNC2 (ARM_IRQ1_BASE + 14)
  6939. +#define INTERRUPT_MULTICORESYNC3 (ARM_IRQ1_BASE + 15)
  6940. +#define INTERRUPT_DMA0 (ARM_IRQ1_BASE + 16)
  6941. +#define INTERRUPT_DMA1 (ARM_IRQ1_BASE + 17)
  6942. +#define INTERRUPT_VC_DMA2 (ARM_IRQ1_BASE + 18)
  6943. +#define INTERRUPT_VC_DMA3 (ARM_IRQ1_BASE + 19)
  6944. +#define INTERRUPT_DMA4 (ARM_IRQ1_BASE + 20)
  6945. +#define INTERRUPT_DMA5 (ARM_IRQ1_BASE + 21)
  6946. +#define INTERRUPT_DMA6 (ARM_IRQ1_BASE + 22)
  6947. +#define INTERRUPT_DMA7 (ARM_IRQ1_BASE + 23)
  6948. +#define INTERRUPT_DMA8 (ARM_IRQ1_BASE + 24)
  6949. +#define INTERRUPT_DMA9 (ARM_IRQ1_BASE + 25)
  6950. +#define INTERRUPT_DMA10 (ARM_IRQ1_BASE + 26)
  6951. +#define INTERRUPT_DMA11 (ARM_IRQ1_BASE + 27)
  6952. +#define INTERRUPT_DMA12 (ARM_IRQ1_BASE + 28)
  6953. +#define INTERRUPT_AUX (ARM_IRQ1_BASE + 29)
  6954. +#define INTERRUPT_ARM (ARM_IRQ1_BASE + 30)
  6955. +#define INTERRUPT_VPUDMA (ARM_IRQ1_BASE + 31)
  6956. +
  6957. +#define ARM_IRQ2_BASE 32
  6958. +#define INTERRUPT_HOSTPORT (ARM_IRQ2_BASE + 0)
  6959. +#define INTERRUPT_VIDEOSCALER (ARM_IRQ2_BASE + 1)
  6960. +#define INTERRUPT_CCP2TX (ARM_IRQ2_BASE + 2)
  6961. +#define INTERRUPT_SDC (ARM_IRQ2_BASE + 3)
  6962. +#define INTERRUPT_DSI0 (ARM_IRQ2_BASE + 4)
  6963. +#define INTERRUPT_AVE (ARM_IRQ2_BASE + 5)
  6964. +#define INTERRUPT_CAM0 (ARM_IRQ2_BASE + 6)
  6965. +#define INTERRUPT_CAM1 (ARM_IRQ2_BASE + 7)
  6966. +#define INTERRUPT_HDMI0 (ARM_IRQ2_BASE + 8)
  6967. +#define INTERRUPT_HDMI1 (ARM_IRQ2_BASE + 9)
  6968. +#define INTERRUPT_PIXELVALVE1 (ARM_IRQ2_BASE + 10)
  6969. +#define INTERRUPT_I2CSPISLV (ARM_IRQ2_BASE + 11)
  6970. +#define INTERRUPT_DSI1 (ARM_IRQ2_BASE + 12)
  6971. +#define INTERRUPT_PWA0 (ARM_IRQ2_BASE + 13)
  6972. +#define INTERRUPT_PWA1 (ARM_IRQ2_BASE + 14)
  6973. +#define INTERRUPT_CPR (ARM_IRQ2_BASE + 15)
  6974. +#define INTERRUPT_SMI (ARM_IRQ2_BASE + 16)
  6975. +#define INTERRUPT_GPIO0 (ARM_IRQ2_BASE + 17)
  6976. +#define INTERRUPT_GPIO1 (ARM_IRQ2_BASE + 18)
  6977. +#define INTERRUPT_GPIO2 (ARM_IRQ2_BASE + 19)
  6978. +#define INTERRUPT_GPIO3 (ARM_IRQ2_BASE + 20)
  6979. +#define INTERRUPT_VC_I2C (ARM_IRQ2_BASE + 21)
  6980. +#define INTERRUPT_VC_SPI (ARM_IRQ2_BASE + 22)
  6981. +#define INTERRUPT_VC_I2SPCM (ARM_IRQ2_BASE + 23)
  6982. +#define INTERRUPT_VC_SDIO (ARM_IRQ2_BASE + 24)
  6983. +#define INTERRUPT_VC_UART (ARM_IRQ2_BASE + 25)
  6984. +#define INTERRUPT_SLIMBUS (ARM_IRQ2_BASE + 26)
  6985. +#define INTERRUPT_VEC (ARM_IRQ2_BASE + 27)
  6986. +#define INTERRUPT_CPG (ARM_IRQ2_BASE + 28)
  6987. +#define INTERRUPT_RNG (ARM_IRQ2_BASE + 29)
  6988. +#define INTERRUPT_VC_ARASANSDIO (ARM_IRQ2_BASE + 30)
  6989. +#define INTERRUPT_AVSPMON (ARM_IRQ2_BASE + 31)
  6990. +
  6991. +#define ARM_IRQ0_BASE 64
  6992. +#define INTERRUPT_ARM_TIMER (ARM_IRQ0_BASE + 0)
  6993. +#define INTERRUPT_ARM_MAILBOX (ARM_IRQ0_BASE + 1)
  6994. +#define INTERRUPT_ARM_DOORBELL_0 (ARM_IRQ0_BASE + 2)
  6995. +#define INTERRUPT_ARM_DOORBELL_1 (ARM_IRQ0_BASE + 3)
  6996. +#define INTERRUPT_VPU0_HALTED (ARM_IRQ0_BASE + 4)
  6997. +#define INTERRUPT_VPU1_HALTED (ARM_IRQ0_BASE + 5)
  6998. +#define INTERRUPT_ILLEGAL_TYPE0 (ARM_IRQ0_BASE + 6)
  6999. +#define INTERRUPT_ILLEGAL_TYPE1 (ARM_IRQ0_BASE + 7)
  7000. +#define INTERRUPT_PENDING1 (ARM_IRQ0_BASE + 8)
  7001. +#define INTERRUPT_PENDING2 (ARM_IRQ0_BASE + 9)
  7002. +#define INTERRUPT_JPEG (ARM_IRQ0_BASE + 10)
  7003. +#define INTERRUPT_USB (ARM_IRQ0_BASE + 11)
  7004. +#define INTERRUPT_3D (ARM_IRQ0_BASE + 12)
  7005. +#define INTERRUPT_DMA2 (ARM_IRQ0_BASE + 13)
  7006. +#define INTERRUPT_DMA3 (ARM_IRQ0_BASE + 14)
  7007. +#define INTERRUPT_I2C (ARM_IRQ0_BASE + 15)
  7008. +#define INTERRUPT_SPI (ARM_IRQ0_BASE + 16)
  7009. +#define INTERRUPT_I2SPCM (ARM_IRQ0_BASE + 17)
  7010. +#define INTERRUPT_SDIO (ARM_IRQ0_BASE + 18)
  7011. +#define INTERRUPT_UART (ARM_IRQ0_BASE + 19)
  7012. +#define INTERRUPT_ARASANSDIO (ARM_IRQ0_BASE + 20)
  7013. +
  7014. +#define MAXIRQNUM (32 + 32 + 20)
  7015. +#define MAXFIQNUM (32 + 32 + 20)
  7016. +
  7017. +#define MAX_TIMER 2
  7018. +#define MAX_PERIOD 699050
  7019. +#define TICKS_PER_uSEC 1
  7020. +
  7021. +/*
  7022. + * These are useconds NOT ticks.
  7023. + *
  7024. + */
  7025. +#define mSEC_1 1000
  7026. +#define mSEC_5 (mSEC_1 * 5)
  7027. +#define mSEC_10 (mSEC_1 * 10)
  7028. +#define mSEC_25 (mSEC_1 * 25)
  7029. +#define SEC_1 (mSEC_1 * 1000)
  7030. +
  7031. +/*
  7032. + * Watchdog
  7033. + */
  7034. +#define PM_RSTC (PM_BASE+0x1c)
  7035. +#define PM_RSTS (PM_BASE+0x20)
  7036. +#define PM_WDOG (PM_BASE+0x24)
  7037. +
  7038. +#define PM_WDOG_RESET 0000000000
  7039. +#define PM_PASSWORD 0x5a000000
  7040. +#define PM_WDOG_TIME_SET 0x000fffff
  7041. +#define PM_RSTC_WRCFG_CLR 0xffffffcf
  7042. +#define PM_RSTC_WRCFG_SET 0x00000030
  7043. +#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  7044. +#define PM_RSTC_RESET 0x00000102
  7045. +
  7046. +#define PM_RSTS_HADPOR_SET 0x00001000
  7047. +#define PM_RSTS_HADSRH_SET 0x00000400
  7048. +#define PM_RSTS_HADSRF_SET 0x00000200
  7049. +#define PM_RSTS_HADSRQ_SET 0x00000100
  7050. +#define PM_RSTS_HADWRH_SET 0x00000040
  7051. +#define PM_RSTS_HADWRF_SET 0x00000020
  7052. +#define PM_RSTS_HADWRQ_SET 0x00000010
  7053. +#define PM_RSTS_HADDRH_SET 0x00000004
  7054. +#define PM_RSTS_HADDRF_SET 0x00000002
  7055. +#define PM_RSTS_HADDRQ_SET 0x00000001
  7056. +
  7057. +#define UART0_CLOCK 3000000
  7058. +
  7059. +#endif
  7060. +
  7061. +/* END */
  7062. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/power.h linux-rpi/arch/arm/mach-bcm2708/include/mach/power.h
  7063. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/power.h 1969-12-31 18:00:00.000000000 -0600
  7064. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/power.h 2014-12-11 14:02:51.516418001 -0600
  7065. @@ -0,0 +1,26 @@
  7066. +/*
  7067. + * linux/arch/arm/mach-bcm2708/power.h
  7068. + *
  7069. + * Copyright (C) 2010 Broadcom
  7070. + *
  7071. + * This program is free software; you can redistribute it and/or modify
  7072. + * it under the terms of the GNU General Public License version 2 as
  7073. + * published by the Free Software Foundation.
  7074. + *
  7075. + * This device provides a shared mechanism for controlling the power to
  7076. + * VideoCore subsystems.
  7077. + */
  7078. +
  7079. +#ifndef _MACH_BCM2708_POWER_H
  7080. +#define _MACH_BCM2708_POWER_H
  7081. +
  7082. +#include <linux/types.h>
  7083. +#include <mach/arm_power.h>
  7084. +
  7085. +typedef unsigned int BCM_POWER_HANDLE_T;
  7086. +
  7087. +extern int bcm_power_open(BCM_POWER_HANDLE_T *handle);
  7088. +extern int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request);
  7089. +extern int bcm_power_close(BCM_POWER_HANDLE_T handle);
  7090. +
  7091. +#endif
  7092. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/system.h linux-rpi/arch/arm/mach-bcm2708/include/mach/system.h
  7093. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/system.h 1969-12-31 18:00:00.000000000 -0600
  7094. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/system.h 2014-12-11 14:02:51.516418001 -0600
  7095. @@ -0,0 +1,38 @@
  7096. +/*
  7097. + * arch/arm/mach-bcm2708/include/mach/system.h
  7098. + *
  7099. + * Copyright (C) 2010 Broadcom
  7100. + * Copyright (C) 2003 ARM Limited
  7101. + * Copyright (C) 2000 Deep Blue Solutions Ltd
  7102. + *
  7103. + * This program is free software; you can redistribute it and/or modify
  7104. + * it under the terms of the GNU General Public License as published by
  7105. + * the Free Software Foundation; either version 2 of the License, or
  7106. + * (at your option) any later version.
  7107. + *
  7108. + * This program is distributed in the hope that it will be useful,
  7109. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7110. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7111. + * GNU General Public License for more details.
  7112. + *
  7113. + * You should have received a copy of the GNU General Public License
  7114. + * along with this program; if not, write to the Free Software
  7115. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7116. + */
  7117. +#ifndef __ASM_ARCH_SYSTEM_H
  7118. +#define __ASM_ARCH_SYSTEM_H
  7119. +
  7120. +#include <linux/io.h>
  7121. +#include <mach/hardware.h>
  7122. +#include <mach/platform.h>
  7123. +
  7124. +static inline void arch_idle(void)
  7125. +{
  7126. + /*
  7127. + * This should do all the clock switching
  7128. + * and wait for interrupt tricks
  7129. + */
  7130. + cpu_do_idle();
  7131. +}
  7132. +
  7133. +#endif
  7134. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/timex.h linux-rpi/arch/arm/mach-bcm2708/include/mach/timex.h
  7135. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/timex.h 1969-12-31 18:00:00.000000000 -0600
  7136. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/timex.h 2014-12-11 14:02:51.516418001 -0600
  7137. @@ -0,0 +1,23 @@
  7138. +/*
  7139. + * arch/arm/mach-bcm2708/include/mach/timex.h
  7140. + *
  7141. + * BCM2708 sysem clock frequency
  7142. + *
  7143. + * Copyright (C) 2010 Broadcom
  7144. + *
  7145. + * This program is free software; you can redistribute it and/or modify
  7146. + * it under the terms of the GNU General Public License as published by
  7147. + * the Free Software Foundation; either version 2 of the License, or
  7148. + * (at your option) any later version.
  7149. + *
  7150. + * This program is distributed in the hope that it will be useful,
  7151. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7152. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7153. + * GNU General Public License for more details.
  7154. + *
  7155. + * You should have received a copy of the GNU General Public License
  7156. + * along with this program; if not, write to the Free Software
  7157. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7158. + */
  7159. +
  7160. +#define CLOCK_TICK_RATE (1000000)
  7161. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/uncompress.h linux-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h
  7162. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/uncompress.h 1969-12-31 18:00:00.000000000 -0600
  7163. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h 2014-12-11 14:02:51.516418001 -0600
  7164. @@ -0,0 +1,84 @@
  7165. +/*
  7166. + * arch/arm/mach-bcn2708/include/mach/uncompress.h
  7167. + *
  7168. + * Copyright (C) 2010 Broadcom
  7169. + * Copyright (C) 2003 ARM Limited
  7170. + *
  7171. + * This program is free software; you can redistribute it and/or modify
  7172. + * it under the terms of the GNU General Public License as published by
  7173. + * the Free Software Foundation; either version 2 of the License, or
  7174. + * (at your option) any later version.
  7175. + *
  7176. + * This program is distributed in the hope that it will be useful,
  7177. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7178. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7179. + * GNU General Public License for more details.
  7180. + *
  7181. + * You should have received a copy of the GNU General Public License
  7182. + * along with this program; if not, write to the Free Software
  7183. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7184. + */
  7185. +
  7186. +#include <linux/io.h>
  7187. +#include <linux/amba/serial.h>
  7188. +#include <mach/hardware.h>
  7189. +
  7190. +#define UART_BAUD 115200
  7191. +
  7192. +#define BCM2708_UART_DR __io(UART0_BASE + UART01x_DR)
  7193. +#define BCM2708_UART_FR __io(UART0_BASE + UART01x_FR)
  7194. +#define BCM2708_UART_IBRD __io(UART0_BASE + UART011_IBRD)
  7195. +#define BCM2708_UART_FBRD __io(UART0_BASE + UART011_FBRD)
  7196. +#define BCM2708_UART_LCRH __io(UART0_BASE + UART011_LCRH)
  7197. +#define BCM2708_UART_CR __io(UART0_BASE + UART011_CR)
  7198. +
  7199. +/*
  7200. + * This does not append a newline
  7201. + */
  7202. +static inline void putc(int c)
  7203. +{
  7204. + while (__raw_readl(BCM2708_UART_FR) & UART01x_FR_TXFF)
  7205. + barrier();
  7206. +
  7207. + __raw_writel(c, BCM2708_UART_DR);
  7208. +}
  7209. +
  7210. +static inline void flush(void)
  7211. +{
  7212. + int fr;
  7213. +
  7214. + do {
  7215. + fr = __raw_readl(BCM2708_UART_FR);
  7216. + barrier();
  7217. + } while ((fr & (UART011_FR_TXFE | UART01x_FR_BUSY)) != UART011_FR_TXFE);
  7218. +}
  7219. +
  7220. +static inline void arch_decomp_setup(void)
  7221. +{
  7222. + int temp, div, rem, frac;
  7223. +
  7224. + temp = 16 * UART_BAUD;
  7225. + div = UART0_CLOCK / temp;
  7226. + rem = UART0_CLOCK % temp;
  7227. + temp = (8 * rem) / UART_BAUD;
  7228. + frac = (temp >> 1) + (temp & 1);
  7229. +
  7230. + /* Make sure the UART is disabled before we start */
  7231. + __raw_writel(0, BCM2708_UART_CR);
  7232. +
  7233. + /* Set the baud rate */
  7234. + __raw_writel(div, BCM2708_UART_IBRD);
  7235. + __raw_writel(frac, BCM2708_UART_FBRD);
  7236. +
  7237. + /* Set the UART to 8n1, FIFO enabled */
  7238. + __raw_writel(UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN, BCM2708_UART_LCRH);
  7239. +
  7240. + /* Enable the UART */
  7241. + __raw_writel(UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE,
  7242. + BCM2708_UART_CR);
  7243. +}
  7244. +
  7245. +/*
  7246. + * nothing to do
  7247. + */
  7248. +#define arch_decomp_wdog()
  7249. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vcio.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h
  7250. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vcio.h 1969-12-31 18:00:00.000000000 -0600
  7251. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h 2014-12-11 14:02:51.516418001 -0600
  7252. @@ -0,0 +1,165 @@
  7253. +/*
  7254. + * arch/arm/mach-bcm2708/include/mach/vcio.h
  7255. + *
  7256. + * Copyright (C) 2010 Broadcom
  7257. + *
  7258. + * This program is free software; you can redistribute it and/or modify
  7259. + * it under the terms of the GNU General Public License as published by
  7260. + * the Free Software Foundation; either version 2 of the License, or
  7261. + * (at your option) any later version.
  7262. + *
  7263. + * This program is distributed in the hope that it will be useful,
  7264. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7265. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7266. + * GNU General Public License for more details.
  7267. + *
  7268. + * You should have received a copy of the GNU General Public License
  7269. + * along with this program; if not, write to the Free Software
  7270. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7271. + */
  7272. +#ifndef _MACH_BCM2708_VCIO_H
  7273. +#define _MACH_BCM2708_VCIO_H
  7274. +
  7275. +/* Routines to handle I/O via the VideoCore "ARM control" registers
  7276. + * (semaphores, doorbells, mailboxes)
  7277. + */
  7278. +
  7279. +#define BCM_VCIO_DRIVER_NAME "bcm2708_vcio"
  7280. +
  7281. +/* Constants shared with the ARM identifying separate mailbox channels */
  7282. +#define MBOX_CHAN_POWER 0 /* for use by the power management interface */
  7283. +#define MBOX_CHAN_FB 1 /* for use by the frame buffer */
  7284. +#define MBOX_CHAN_VCHIQ 3 /* for use by the VCHIQ interface */
  7285. +#define MBOX_CHAN_PROPERTY 8 /* for use by the property channel */
  7286. +#define MBOX_CHAN_COUNT 9
  7287. +
  7288. +enum {
  7289. + VCMSG_PROCESS_REQUEST = 0x00000000
  7290. +};
  7291. +enum {
  7292. + VCMSG_REQUEST_SUCCESSFUL = 0x80000000,
  7293. + VCMSG_REQUEST_FAILED = 0x80000001
  7294. +};
  7295. +/* Mailbox property tags */
  7296. +enum {
  7297. + VCMSG_PROPERTY_END = 0x00000000,
  7298. + VCMSG_GET_FIRMWARE_REVISION = 0x00000001,
  7299. + VCMSG_GET_BOARD_MODEL = 0x00010001,
  7300. + VCMSG_GET_BOARD_REVISION = 0x00010002,
  7301. + VCMSG_GET_BOARD_MAC_ADDRESS = 0x00010003,
  7302. + VCMSG_GET_BOARD_SERIAL = 0x00010004,
  7303. + VCMSG_GET_ARM_MEMORY = 0x00010005,
  7304. + VCMSG_GET_VC_MEMORY = 0x00010006,
  7305. + VCMSG_GET_CLOCKS = 0x00010007,
  7306. + VCMSG_GET_COMMAND_LINE = 0x00050001,
  7307. + VCMSG_GET_DMA_CHANNELS = 0x00060001,
  7308. + VCMSG_GET_POWER_STATE = 0x00020001,
  7309. + VCMSG_GET_TIMING = 0x00020002,
  7310. + VCMSG_SET_POWER_STATE = 0x00028001,
  7311. + VCMSG_GET_CLOCK_STATE = 0x00030001,
  7312. + VCMSG_SET_CLOCK_STATE = 0x00038001,
  7313. + VCMSG_GET_CLOCK_RATE = 0x00030002,
  7314. + VCMSG_SET_CLOCK_RATE = 0x00038002,
  7315. + VCMSG_GET_VOLTAGE = 0x00030003,
  7316. + VCMSG_SET_VOLTAGE = 0x00038003,
  7317. + VCMSG_GET_MAX_CLOCK = 0x00030004,
  7318. + VCMSG_GET_MAX_VOLTAGE = 0x00030005,
  7319. + VCMSG_GET_TEMPERATURE = 0x00030006,
  7320. + VCMSG_GET_MIN_CLOCK = 0x00030007,
  7321. + VCMSG_GET_MIN_VOLTAGE = 0x00030008,
  7322. + VCMSG_GET_TURBO = 0x00030009,
  7323. + VCMSG_GET_MAX_TEMPERATURE = 0x0003000a,
  7324. + VCMSG_GET_STC = 0x0003000b,
  7325. + VCMSG_SET_TURBO = 0x00038009,
  7326. + VCMSG_SET_ALLOCATE_MEM = 0x0003000c,
  7327. + VCMSG_SET_LOCK_MEM = 0x0003000d,
  7328. + VCMSG_SET_UNLOCK_MEM = 0x0003000e,
  7329. + VCMSG_SET_RELEASE_MEM = 0x0003000f,
  7330. + VCMSG_SET_EXECUTE_CODE = 0x00030010,
  7331. + VCMSG_SET_EXECUTE_QPU = 0x00030011,
  7332. + VCMSG_SET_ENABLE_QPU = 0x00030012,
  7333. + VCMSG_GET_RESOURCE_HANDLE = 0x00030014,
  7334. + VCMSG_GET_EDID_BLOCK = 0x00030020,
  7335. + VCMSG_GET_CUSTOMER_OTP = 0x00030021,
  7336. + VCMSG_SET_CUSTOMER_OTP = 0x00038021,
  7337. + VCMSG_SET_ALLOCATE_BUFFER = 0x00040001,
  7338. + VCMSG_SET_RELEASE_BUFFER = 0x00048001,
  7339. + VCMSG_SET_BLANK_SCREEN = 0x00040002,
  7340. + VCMSG_TST_BLANK_SCREEN = 0x00044002,
  7341. + VCMSG_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
  7342. + VCMSG_TST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  7343. + VCMSG_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
  7344. + VCMSG_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
  7345. + VCMSG_TST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
  7346. + VCMSG_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
  7347. + VCMSG_GET_DEPTH = 0x00040005,
  7348. + VCMSG_TST_DEPTH = 0x00044005,
  7349. + VCMSG_SET_DEPTH = 0x00048005,
  7350. + VCMSG_GET_PIXEL_ORDER = 0x00040006,
  7351. + VCMSG_TST_PIXEL_ORDER = 0x00044006,
  7352. + VCMSG_SET_PIXEL_ORDER = 0x00048006,
  7353. + VCMSG_GET_ALPHA_MODE = 0x00040007,
  7354. + VCMSG_TST_ALPHA_MODE = 0x00044007,
  7355. + VCMSG_SET_ALPHA_MODE = 0x00048007,
  7356. + VCMSG_GET_PITCH = 0x00040008,
  7357. + VCMSG_TST_PITCH = 0x00044008,
  7358. + VCMSG_SET_PITCH = 0x00048008,
  7359. + VCMSG_GET_VIRTUAL_OFFSET = 0x00040009,
  7360. + VCMSG_TST_VIRTUAL_OFFSET = 0x00044009,
  7361. + VCMSG_SET_VIRTUAL_OFFSET = 0x00048009,
  7362. + VCMSG_GET_OVERSCAN = 0x0004000a,
  7363. + VCMSG_TST_OVERSCAN = 0x0004400a,
  7364. + VCMSG_SET_OVERSCAN = 0x0004800a,
  7365. + VCMSG_GET_PALETTE = 0x0004000b,
  7366. + VCMSG_TST_PALETTE = 0x0004400b,
  7367. + VCMSG_SET_PALETTE = 0x0004800b,
  7368. + VCMSG_GET_LAYER = 0x0004000c,
  7369. + VCMSG_TST_LAYER = 0x0004400c,
  7370. + VCMSG_SET_LAYER = 0x0004800c,
  7371. + VCMSG_GET_TRANSFORM = 0x0004000d,
  7372. + VCMSG_TST_TRANSFORM = 0x0004400d,
  7373. + VCMSG_SET_TRANSFORM = 0x0004800d,
  7374. + VCMSG_TST_VSYNC = 0x0004400e,
  7375. + VCMSG_SET_VSYNC = 0x0004800e,
  7376. + VCMSG_SET_CURSOR_INFO = 0x00008010,
  7377. + VCMSG_SET_CURSOR_STATE = 0x00008011,
  7378. +};
  7379. +
  7380. +extern int /*rc*/ bcm_mailbox_read(unsigned chan, uint32_t *data28);
  7381. +extern int /*rc*/ bcm_mailbox_write(unsigned chan, uint32_t data28);
  7382. +extern int /*rc*/ bcm_mailbox_property(void *data, int size);
  7383. +
  7384. +#include <linux/ioctl.h>
  7385. +
  7386. +/*
  7387. + * The major device number. We can't rely on dynamic
  7388. + * registration any more, because ioctls need to know
  7389. + * it.
  7390. + */
  7391. +#define MAJOR_NUM 100
  7392. +
  7393. +/*
  7394. + * Set the message of the device driver
  7395. + */
  7396. +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
  7397. +/*
  7398. + * _IOWR means that we're creating an ioctl command
  7399. + * number for passing information from a user process
  7400. + * to the kernel module and from the kernel module to user process
  7401. + *
  7402. + * The first arguments, MAJOR_NUM, is the major device
  7403. + * number we're using.
  7404. + *
  7405. + * The second argument is the number of the command
  7406. + * (there could be several with different meanings).
  7407. + *
  7408. + * The third argument is the type we want to get from
  7409. + * the process to the kernel.
  7410. + */
  7411. +
  7412. +/*
  7413. + * The name of the device file
  7414. + */
  7415. +#define DEVICE_FILE_NAME "vcio"
  7416. +
  7417. +#endif
  7418. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_mem.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h
  7419. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_mem.h 1969-12-31 18:00:00.000000000 -0600
  7420. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h 2014-12-11 14:02:51.516418001 -0600
  7421. @@ -0,0 +1,35 @@
  7422. +/*****************************************************************************
  7423. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  7424. +*
  7425. +* Unless you and Broadcom execute a separate written software license
  7426. +* agreement governing use of this software, this software is licensed to you
  7427. +* under the terms of the GNU General Public License version 2, available at
  7428. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7429. +*
  7430. +* Notwithstanding the above, under no circumstances may you combine this
  7431. +* software in any way with any other Broadcom software provided under a
  7432. +* license other than the GPL, without Broadcom's express prior written
  7433. +* consent.
  7434. +*****************************************************************************/
  7435. +
  7436. +#if !defined( VC_MEM_H )
  7437. +#define VC_MEM_H
  7438. +
  7439. +#include <linux/ioctl.h>
  7440. +
  7441. +#define VC_MEM_IOC_MAGIC 'v'
  7442. +
  7443. +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
  7444. +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
  7445. +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
  7446. +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
  7447. +
  7448. +#if defined( __KERNEL__ )
  7449. +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
  7450. +
  7451. +extern unsigned long mm_vc_mem_phys_addr;
  7452. +extern unsigned int mm_vc_mem_size;
  7453. +extern int vc_mem_get_current_size( void );
  7454. +#endif
  7455. +
  7456. +#endif /* VC_MEM_H */
  7457. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h
  7458. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h 1969-12-31 18:00:00.000000000 -0600
  7459. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h 2014-12-11 14:02:51.516418001 -0600
  7460. @@ -0,0 +1,181 @@
  7461. +/*****************************************************************************
  7462. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  7463. +*
  7464. +* Unless you and Broadcom execute a separate written software license
  7465. +* agreement governing use of this software, this software is licensed to you
  7466. +* under the terms of the GNU General Public License version 2, available at
  7467. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7468. +*
  7469. +* Notwithstanding the above, under no circumstances may you combine this
  7470. +* software in any way with any other Broadcom software provided under a
  7471. +* license other than the GPL, without Broadcom's express prior written
  7472. +* consent.
  7473. +*****************************************************************************/
  7474. +
  7475. +#ifndef __VC_SM_DEFS_H__INCLUDED__
  7476. +#define __VC_SM_DEFS_H__INCLUDED__
  7477. +
  7478. +/* FourCC code used for VCHI connection */
  7479. +#define VC_SM_SERVER_NAME MAKE_FOURCC("SMEM")
  7480. +
  7481. +/* Maximum message length */
  7482. +#define VC_SM_MAX_MSG_LEN (sizeof(VC_SM_MSG_UNION_T) + \
  7483. + sizeof(VC_SM_MSG_HDR_T))
  7484. +#define VC_SM_MAX_RSP_LEN (sizeof(VC_SM_MSG_UNION_T))
  7485. +
  7486. +/* Resource name maximum size */
  7487. +#define VC_SM_RESOURCE_NAME 32
  7488. +
  7489. +/* All message types supported for HOST->VC direction */
  7490. +typedef enum {
  7491. + /* Allocate shared memory block */
  7492. + VC_SM_MSG_TYPE_ALLOC,
  7493. + /* Lock allocated shared memory block */
  7494. + VC_SM_MSG_TYPE_LOCK,
  7495. + /* Unlock allocated shared memory block */
  7496. + VC_SM_MSG_TYPE_UNLOCK,
  7497. + /* Unlock allocated shared memory block, do not answer command */
  7498. + VC_SM_MSG_TYPE_UNLOCK_NOANS,
  7499. + /* Free shared memory block */
  7500. + VC_SM_MSG_TYPE_FREE,
  7501. + /* Resize a shared memory block */
  7502. + VC_SM_MSG_TYPE_RESIZE,
  7503. + /* Walk the allocated shared memory block(s) */
  7504. + VC_SM_MSG_TYPE_WALK_ALLOC,
  7505. +
  7506. + /* A previously applied action will need to be reverted */
  7507. + VC_SM_MSG_TYPE_ACTION_CLEAN,
  7508. + VC_SM_MSG_TYPE_MAX
  7509. +} VC_SM_MSG_TYPE;
  7510. +
  7511. +/* Type of memory to be allocated */
  7512. +typedef enum {
  7513. + VC_SM_ALLOC_CACHED,
  7514. + VC_SM_ALLOC_NON_CACHED,
  7515. +
  7516. +} VC_SM_ALLOC_TYPE_T;
  7517. +
  7518. +/* Message header for all messages in HOST->VC direction */
  7519. +typedef struct {
  7520. + int32_t type;
  7521. + uint32_t trans_id;
  7522. + uint8_t body[0];
  7523. +
  7524. +} VC_SM_MSG_HDR_T;
  7525. +
  7526. +/* Request to allocate memory (HOST->VC) */
  7527. +typedef struct {
  7528. + /* type of memory to allocate */
  7529. + VC_SM_ALLOC_TYPE_T type;
  7530. + /* byte amount of data to allocate per unit */
  7531. + uint32_t base_unit;
  7532. + /* number of unit to allocate */
  7533. + uint32_t num_unit;
  7534. + /* alignement to be applied on allocation */
  7535. + uint32_t alignement;
  7536. + /* identity of who allocated this block */
  7537. + uint32_t allocator;
  7538. + /* resource name (for easier tracking on vc side) */
  7539. + char name[VC_SM_RESOURCE_NAME];
  7540. +
  7541. +} VC_SM_ALLOC_T;
  7542. +
  7543. +/* Result of a requested memory allocation (VC->HOST) */
  7544. +typedef struct {
  7545. + /* Transaction identifier */
  7546. + uint32_t trans_id;
  7547. +
  7548. + /* Resource handle */
  7549. + uint32_t res_handle;
  7550. + /* Pointer to resource buffer */
  7551. + void *res_mem;
  7552. + /* Resource base size (bytes) */
  7553. + uint32_t res_base_size;
  7554. + /* Resource number */
  7555. + uint32_t res_num;
  7556. +
  7557. +} VC_SM_ALLOC_RESULT_T;
  7558. +
  7559. +/* Request to free a previously allocated memory (HOST->VC) */
  7560. +typedef struct {
  7561. + /* Resource handle (returned from alloc) */
  7562. + uint32_t res_handle;
  7563. + /* Resource buffer (returned from alloc) */
  7564. + void *res_mem;
  7565. +
  7566. +} VC_SM_FREE_T;
  7567. +
  7568. +/* Request to lock a previously allocated memory (HOST->VC) */
  7569. +typedef struct {
  7570. + /* Resource handle (returned from alloc) */
  7571. + uint32_t res_handle;
  7572. + /* Resource buffer (returned from alloc) */
  7573. + void *res_mem;
  7574. +
  7575. +} VC_SM_LOCK_UNLOCK_T;
  7576. +
  7577. +/* Request to resize a previously allocated memory (HOST->VC) */
  7578. +typedef struct {
  7579. + /* Resource handle (returned from alloc) */
  7580. + uint32_t res_handle;
  7581. + /* Resource buffer (returned from alloc) */
  7582. + void *res_mem;
  7583. + /* Resource *new* size requested (bytes) */
  7584. + uint32_t res_new_size;
  7585. +
  7586. +} VC_SM_RESIZE_T;
  7587. +
  7588. +/* Result of a requested memory lock (VC->HOST) */
  7589. +typedef struct {
  7590. + /* Transaction identifier */
  7591. + uint32_t trans_id;
  7592. +
  7593. + /* Resource handle */
  7594. + uint32_t res_handle;
  7595. + /* Pointer to resource buffer */
  7596. + void *res_mem;
  7597. + /* Pointer to former resource buffer if the memory
  7598. + * was reallocated */
  7599. + void *res_old_mem;
  7600. +
  7601. +} VC_SM_LOCK_RESULT_T;
  7602. +
  7603. +/* Generic result for a request (VC->HOST) */
  7604. +typedef struct {
  7605. + /* Transaction identifier */
  7606. + uint32_t trans_id;
  7607. +
  7608. + int32_t success;
  7609. +
  7610. +} VC_SM_RESULT_T;
  7611. +
  7612. +/* Request to revert a previously applied action (HOST->VC) */
  7613. +typedef struct {
  7614. + /* Action of interest */
  7615. + VC_SM_MSG_TYPE res_action;
  7616. + /* Transaction identifier for the action of interest */
  7617. + uint32_t action_trans_id;
  7618. +
  7619. +} VC_SM_ACTION_CLEAN_T;
  7620. +
  7621. +/* Request to remove all data associated with a given allocator (HOST->VC) */
  7622. +typedef struct {
  7623. + /* Allocator identifier */
  7624. + uint32_t allocator;
  7625. +
  7626. +} VC_SM_FREE_ALL_T;
  7627. +
  7628. +/* Union of ALL messages */
  7629. +typedef union {
  7630. + VC_SM_ALLOC_T alloc;
  7631. + VC_SM_ALLOC_RESULT_T alloc_result;
  7632. + VC_SM_FREE_T free;
  7633. + VC_SM_ACTION_CLEAN_T action_clean;
  7634. + VC_SM_RESIZE_T resize;
  7635. + VC_SM_LOCK_RESULT_T lock_result;
  7636. + VC_SM_RESULT_T result;
  7637. + VC_SM_FREE_ALL_T free_all;
  7638. +
  7639. +} VC_SM_MSG_UNION_T;
  7640. +
  7641. +#endif /* __VC_SM_DEFS_H__INCLUDED__ */
  7642. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h
  7643. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h 1969-12-31 18:00:00.000000000 -0600
  7644. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h 2014-12-11 14:02:51.516418001 -0600
  7645. @@ -0,0 +1,55 @@
  7646. +/*****************************************************************************
  7647. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  7648. +*
  7649. +* Unless you and Broadcom execute a separate written software license
  7650. +* agreement governing use of this software, this software is licensed to you
  7651. +* under the terms of the GNU General Public License version 2, available at
  7652. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7653. +*
  7654. +* Notwithstanding the above, under no circumstances may you combine this
  7655. +* software in any way with any other Broadcom software provided under a
  7656. +* license other than the GPL, without Broadcom's express prior written
  7657. +* consent.
  7658. +*****************************************************************************/
  7659. +
  7660. +#ifndef __VC_SM_KNL_H__INCLUDED__
  7661. +#define __VC_SM_KNL_H__INCLUDED__
  7662. +
  7663. +#if !defined(__KERNEL__)
  7664. +#error "This interface is for kernel use only..."
  7665. +#endif
  7666. +
  7667. +/* Type of memory to be locked (ie mapped) */
  7668. +typedef enum {
  7669. + VC_SM_LOCK_CACHED,
  7670. + VC_SM_LOCK_NON_CACHED,
  7671. +
  7672. +} VC_SM_LOCK_CACHE_MODE_T;
  7673. +
  7674. +/* Allocate a shared memory handle and block.
  7675. +*/
  7676. +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle);
  7677. +
  7678. +/* Free a previously allocated shared memory handle and block.
  7679. +*/
  7680. +int vc_sm_free(int handle);
  7681. +
  7682. +/* Lock a memory handle for use by kernel.
  7683. +*/
  7684. +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
  7685. + long unsigned int *data);
  7686. +
  7687. +/* Unlock a memory handle in use by kernel.
  7688. +*/
  7689. +int vc_sm_unlock(int handle, int flush, int no_vc_unlock);
  7690. +
  7691. +/* Get an internal resource handle mapped from the external one.
  7692. +*/
  7693. +int vc_sm_int_handle(int handle);
  7694. +
  7695. +/* Map a shared memory region for use by kernel.
  7696. +*/
  7697. +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
  7698. + long unsigned int *data);
  7699. +
  7700. +#endif /* __VC_SM_KNL_H__INCLUDED__ */
  7701. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h
  7702. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h 1969-12-31 18:00:00.000000000 -0600
  7703. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h 2014-12-11 14:02:51.516418001 -0600
  7704. @@ -0,0 +1,82 @@
  7705. +/*****************************************************************************
  7706. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  7707. +*
  7708. +* Unless you and Broadcom execute a separate written software license
  7709. +* agreement governing use of this software, this software is licensed to you
  7710. +* under the terms of the GNU General Public License version 2, available at
  7711. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7712. +*
  7713. +* Notwithstanding the above, under no circumstances may you combine this
  7714. +* software in any way with any other Broadcom software provided under a
  7715. +* license other than the GPL, without Broadcom's express prior written
  7716. +* consent.
  7717. +*****************************************************************************/
  7718. +
  7719. +#ifndef __VC_VCHI_SM_H__INCLUDED__
  7720. +#define __VC_VCHI_SM_H__INCLUDED__
  7721. +
  7722. +#include "interface/vchi/vchi.h"
  7723. +
  7724. +#include "vc_sm_defs.h"
  7725. +
  7726. +/* Forward declare.
  7727. +*/
  7728. +typedef struct sm_instance *VC_VCHI_SM_HANDLE_T;
  7729. +
  7730. +/* Initialize the shared memory service, opens up vchi connection to talk to it.
  7731. +*/
  7732. +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
  7733. + VCHI_CONNECTION_T **vchi_connections,
  7734. + uint32_t num_connections);
  7735. +
  7736. +/* Terminates the shared memory service.
  7737. +*/
  7738. +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle);
  7739. +
  7740. +/* Ask the shared memory service to allocate some memory on videocre and
  7741. +** return the result of this allocation (which upon success will be a pointer
  7742. +** to some memory in videocore space).
  7743. +*/
  7744. +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle,
  7745. + VC_SM_ALLOC_T *alloc,
  7746. + VC_SM_ALLOC_RESULT_T *alloc_result, uint32_t *trans_id);
  7747. +
  7748. +/* Ask the shared memory service to free up some memory that was previously
  7749. +** allocated by the vc_vchi_sm_alloc function call.
  7750. +*/
  7751. +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
  7752. + VC_SM_FREE_T *free, uint32_t *trans_id);
  7753. +
  7754. +/* Ask the shared memory service to lock up some memory that was previously
  7755. +** allocated by the vc_vchi_sm_alloc function call.
  7756. +*/
  7757. +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
  7758. + VC_SM_LOCK_UNLOCK_T *lock_unlock,
  7759. + VC_SM_LOCK_RESULT_T *lock_result, uint32_t *trans_id);
  7760. +
  7761. +/* Ask the shared memory service to unlock some memory that was previously
  7762. +** allocated by the vc_vchi_sm_alloc function call.
  7763. +*/
  7764. +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
  7765. + VC_SM_LOCK_UNLOCK_T *lock_unlock,
  7766. + uint32_t *trans_id, uint8_t wait_reply);
  7767. +
  7768. +/* Ask the shared memory service to resize some memory that was previously
  7769. +** allocated by the vc_vchi_sm_alloc function call.
  7770. +*/
  7771. +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle,
  7772. + VC_SM_RESIZE_T *resize, uint32_t *trans_id);
  7773. +
  7774. +/* Walk the allocated resources on the videocore side, the allocation will
  7775. +** show up in the log. This is purely for debug/information and takes no
  7776. +** specific actions.
  7777. +*/
  7778. +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle);
  7779. +
  7780. +/* Clean up following a previously interrupted action which left the system
  7781. +** in a bad state of some sort.
  7782. +*/
  7783. +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle,
  7784. + VC_SM_ACTION_CLEAN_T *action_clean);
  7785. +
  7786. +#endif /* __VC_VCHI_SM_H__INCLUDED__ */
  7787. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vmalloc.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h
  7788. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vmalloc.h 1969-12-31 18:00:00.000000000 -0600
  7789. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h 2014-12-11 14:02:51.516418001 -0600
  7790. @@ -0,0 +1,20 @@
  7791. +/*
  7792. + * arch/arm/mach-bcm2708/include/mach/vmalloc.h
  7793. + *
  7794. + * Copyright (C) 2010 Broadcom
  7795. + *
  7796. + * This program is free software; you can redistribute it and/or modify
  7797. + * it under the terms of the GNU General Public License as published by
  7798. + * the Free Software Foundation; either version 2 of the License, or
  7799. + * (at your option) any later version.
  7800. + *
  7801. + * This program is distributed in the hope that it will be useful,
  7802. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7803. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7804. + * GNU General Public License for more details.
  7805. + *
  7806. + * You should have received a copy of the GNU General Public License
  7807. + * along with this program; if not, write to the Free Software
  7808. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7809. + */
  7810. +#define VMALLOC_END (0xe8000000)
  7811. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h linux-rpi/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h
  7812. --- linux-3.17.5/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h 1969-12-31 18:00:00.000000000 -0600
  7813. +++ linux-rpi/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h 2014-12-11 14:02:51.520418001 -0600
  7814. @@ -0,0 +1,233 @@
  7815. +/*****************************************************************************
  7816. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  7817. +*
  7818. +* Unless you and Broadcom execute a separate written software license
  7819. +* agreement governing use of this software, this software is licensed to you
  7820. +* under the terms of the GNU General Public License version 2, available at
  7821. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7822. +*
  7823. +* Notwithstanding the above, under no circumstances may you combine this
  7824. +* software in any way with any other Broadcom software provided under a
  7825. +* license other than the GPL, without Broadcom's express prior written
  7826. +* consent.
  7827. +*
  7828. +*****************************************************************************/
  7829. +
  7830. +#if !defined(__VMCS_SM_IOCTL_H__INCLUDED__)
  7831. +#define __VMCS_SM_IOCTL_H__INCLUDED__
  7832. +
  7833. +/* ---- Include Files ---------------------------------------------------- */
  7834. +
  7835. +#if defined(__KERNEL__)
  7836. +#include <linux/types.h> /* Needed for standard types */
  7837. +#else
  7838. +#include <stdint.h>
  7839. +#endif
  7840. +
  7841. +#include <linux/ioctl.h>
  7842. +
  7843. +/* ---- Constants and Types ---------------------------------------------- */
  7844. +
  7845. +#define VMCS_SM_RESOURCE_NAME 32
  7846. +#define VMCS_SM_RESOURCE_NAME_DEFAULT "sm-host-resource"
  7847. +
  7848. +/* Type define used to create unique IOCTL number */
  7849. +#define VMCS_SM_MAGIC_TYPE 'I'
  7850. +
  7851. +/* IOCTL commands */
  7852. +enum vmcs_sm_cmd_e {
  7853. + VMCS_SM_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
  7854. + VMCS_SM_CMD_ALLOC_SHARE,
  7855. + VMCS_SM_CMD_LOCK,
  7856. + VMCS_SM_CMD_LOCK_CACHE,
  7857. + VMCS_SM_CMD_UNLOCK,
  7858. + VMCS_SM_CMD_RESIZE,
  7859. + VMCS_SM_CMD_UNMAP,
  7860. + VMCS_SM_CMD_FREE,
  7861. + VMCS_SM_CMD_FLUSH,
  7862. + VMCS_SM_CMD_INVALID,
  7863. +
  7864. + VMCS_SM_CMD_SIZE_USR_HANDLE,
  7865. + VMCS_SM_CMD_CHK_USR_HANDLE,
  7866. +
  7867. + VMCS_SM_CMD_MAPPED_USR_HANDLE,
  7868. + VMCS_SM_CMD_MAPPED_USR_ADDRESS,
  7869. + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,
  7870. + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,
  7871. + VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,
  7872. +
  7873. + VMCS_SM_CMD_VC_WALK_ALLOC,
  7874. + VMCS_SM_CMD_HOST_WALK_MAP,
  7875. + VMCS_SM_CMD_HOST_WALK_PID_ALLOC,
  7876. + VMCS_SM_CMD_HOST_WALK_PID_MAP,
  7877. +
  7878. + VMCS_SM_CMD_LAST /* Do no delete */
  7879. +};
  7880. +
  7881. +/* Cache type supported, conveniently matches the user space definition in
  7882. +** user-vcsm.h.
  7883. +*/
  7884. +enum vmcs_sm_cache_e {
  7885. + VMCS_SM_CACHE_NONE,
  7886. + VMCS_SM_CACHE_HOST,
  7887. + VMCS_SM_CACHE_VC,
  7888. + VMCS_SM_CACHE_BOTH,
  7889. +};
  7890. +
  7891. +/* IOCTL Data structures */
  7892. +struct vmcs_sm_ioctl_alloc {
  7893. + /* user -> kernel */
  7894. + unsigned int size;
  7895. + unsigned int num;
  7896. + enum vmcs_sm_cache_e cached;
  7897. + char name[VMCS_SM_RESOURCE_NAME];
  7898. +
  7899. + /* kernel -> user */
  7900. + unsigned int handle;
  7901. + /* unsigned int base_addr; */
  7902. +};
  7903. +
  7904. +struct vmcs_sm_ioctl_alloc_share {
  7905. + /* user -> kernel */
  7906. + unsigned int handle;
  7907. + unsigned int size;
  7908. +};
  7909. +
  7910. +struct vmcs_sm_ioctl_free {
  7911. + /* user -> kernel */
  7912. + unsigned int handle;
  7913. + /* unsigned int base_addr; */
  7914. +};
  7915. +
  7916. +struct vmcs_sm_ioctl_lock_unlock {
  7917. + /* user -> kernel */
  7918. + unsigned int handle;
  7919. +
  7920. + /* kernel -> user */
  7921. + unsigned int addr;
  7922. +};
  7923. +
  7924. +struct vmcs_sm_ioctl_lock_cache {
  7925. + /* user -> kernel */
  7926. + unsigned int handle;
  7927. + enum vmcs_sm_cache_e cached;
  7928. +};
  7929. +
  7930. +struct vmcs_sm_ioctl_resize {
  7931. + /* user -> kernel */
  7932. + unsigned int handle;
  7933. + unsigned int new_size;
  7934. +
  7935. + /* kernel -> user */
  7936. + unsigned int old_size;
  7937. +};
  7938. +
  7939. +struct vmcs_sm_ioctl_map {
  7940. + /* user -> kernel */
  7941. + /* and kernel -> user */
  7942. + unsigned int pid;
  7943. + unsigned int handle;
  7944. + unsigned int addr;
  7945. +
  7946. + /* kernel -> user */
  7947. + unsigned int size;
  7948. +};
  7949. +
  7950. +struct vmcs_sm_ioctl_walk {
  7951. + /* user -> kernel */
  7952. + unsigned int pid;
  7953. +};
  7954. +
  7955. +struct vmcs_sm_ioctl_chk {
  7956. + /* user -> kernel */
  7957. + unsigned int handle;
  7958. +
  7959. + /* kernel -> user */
  7960. + unsigned int addr;
  7961. + unsigned int size;
  7962. + enum vmcs_sm_cache_e cache;
  7963. +};
  7964. +
  7965. +struct vmcs_sm_ioctl_size {
  7966. + /* user -> kernel */
  7967. + unsigned int handle;
  7968. +
  7969. + /* kernel -> user */
  7970. + unsigned int size;
  7971. +};
  7972. +
  7973. +struct vmcs_sm_ioctl_cache {
  7974. + /* user -> kernel */
  7975. + unsigned int handle;
  7976. + unsigned int addr;
  7977. + unsigned int size;
  7978. +};
  7979. +
  7980. +/* IOCTL numbers */
  7981. +#define VMCS_SM_IOCTL_MEM_ALLOC\
  7982. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC,\
  7983. + struct vmcs_sm_ioctl_alloc)
  7984. +#define VMCS_SM_IOCTL_MEM_ALLOC_SHARE\
  7985. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC_SHARE,\
  7986. + struct vmcs_sm_ioctl_alloc_share)
  7987. +#define VMCS_SM_IOCTL_MEM_LOCK\
  7988. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK,\
  7989. + struct vmcs_sm_ioctl_lock_unlock)
  7990. +#define VMCS_SM_IOCTL_MEM_LOCK_CACHE\
  7991. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK_CACHE,\
  7992. + struct vmcs_sm_ioctl_lock_cache)
  7993. +#define VMCS_SM_IOCTL_MEM_UNLOCK\
  7994. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_UNLOCK,\
  7995. + struct vmcs_sm_ioctl_lock_unlock)
  7996. +#define VMCS_SM_IOCTL_MEM_RESIZE\
  7997. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_RESIZE,\
  7998. + struct vmcs_sm_ioctl_resize)
  7999. +#define VMCS_SM_IOCTL_MEM_FREE\
  8000. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FREE,\
  8001. + struct vmcs_sm_ioctl_free)
  8002. +#define VMCS_SM_IOCTL_MEM_FLUSH\
  8003. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FLUSH,\
  8004. + struct vmcs_sm_ioctl_cache)
  8005. +#define VMCS_SM_IOCTL_MEM_INVALID\
  8006. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_INVALID,\
  8007. + struct vmcs_sm_ioctl_cache)
  8008. +
  8009. +#define VMCS_SM_IOCTL_SIZE_USR_HDL\
  8010. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_SIZE_USR_HANDLE,\
  8011. + struct vmcs_sm_ioctl_size)
  8012. +#define VMCS_SM_IOCTL_CHK_USR_HDL\
  8013. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_CHK_USR_HANDLE,\
  8014. + struct vmcs_sm_ioctl_chk)
  8015. +
  8016. +#define VMCS_SM_IOCTL_MAP_USR_HDL\
  8017. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_HANDLE,\
  8018. + struct vmcs_sm_ioctl_map)
  8019. +#define VMCS_SM_IOCTL_MAP_USR_ADDRESS\
  8020. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_ADDRESS,\
  8021. + struct vmcs_sm_ioctl_map)
  8022. +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_ADDR\
  8023. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,\
  8024. + struct vmcs_sm_ioctl_map)
  8025. +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_HDL\
  8026. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,\
  8027. + struct vmcs_sm_ioctl_map)
  8028. +#define VMCS_SM_IOCTL_MAP_VC_ADDR_FR_HDL\
  8029. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,\
  8030. + struct vmcs_sm_ioctl_map)
  8031. +
  8032. +#define VMCS_SM_IOCTL_VC_WALK_ALLOC\
  8033. + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_VC_WALK_ALLOC)
  8034. +#define VMCS_SM_IOCTL_HOST_WALK_MAP\
  8035. + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_MAP)
  8036. +#define VMCS_SM_IOCTL_HOST_WALK_PID_ALLOC\
  8037. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_ALLOC,\
  8038. + struct vmcs_sm_ioctl_walk)
  8039. +#define VMCS_SM_IOCTL_HOST_WALK_PID_MAP\
  8040. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_MAP,\
  8041. + struct vmcs_sm_ioctl_walk)
  8042. +
  8043. +/* ---- Variable Externs ------------------------------------------------- */
  8044. +
  8045. +/* ---- Function Prototypes ---------------------------------------------- */
  8046. +
  8047. +#endif /* __VMCS_SM_IOCTL_H__INCLUDED__ */
  8048. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/Kconfig linux-rpi/arch/arm/mach-bcm2708/Kconfig
  8049. --- linux-3.17.5/arch/arm/mach-bcm2708/Kconfig 1969-12-31 18:00:00.000000000 -0600
  8050. +++ linux-rpi/arch/arm/mach-bcm2708/Kconfig 2014-12-11 14:05:36.828418001 -0600
  8051. @@ -0,0 +1,52 @@
  8052. +menu "Broadcom BCM2708 Implementations"
  8053. + depends on ARCH_BCM2708
  8054. +
  8055. +config MACH_BCM2708
  8056. + bool "Broadcom BCM2708 Development Platform"
  8057. + select NEED_MACH_MEMORY_H
  8058. + select NEED_MACH_IO_H
  8059. + select CPU_V6
  8060. + help
  8061. + Include support for the Broadcom(R) BCM2708 platform.
  8062. +
  8063. +config BCM2708_DT
  8064. + bool "BCM2708 Device Tree support"
  8065. + depends on MACH_BCM2708
  8066. + default n
  8067. + select USE_OF
  8068. + select PINCTRL
  8069. + select PINCTRL_BCM2708
  8070. + select BCM2708_GPIO
  8071. + help
  8072. + Enable Device Tree support for BCM2708
  8073. +
  8074. +config BCM2708_GPIO
  8075. + bool "BCM2708 gpio support"
  8076. + depends on MACH_BCM2708
  8077. + select ARCH_REQUIRE_GPIOLIB
  8078. + default y
  8079. + help
  8080. + Include support for the Broadcom(R) BCM2708 gpio.
  8081. +
  8082. +config BCM2708_VCMEM
  8083. + bool "Videocore Memory"
  8084. + depends on MACH_BCM2708
  8085. + default y
  8086. + help
  8087. + Helper for videocore memory access and total size allocation.
  8088. +
  8089. +config BCM2708_NOL2CACHE
  8090. + bool "Videocore L2 cache disable"
  8091. + depends on MACH_BCM2708
  8092. + default n
  8093. + help
  8094. + Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  8095. +
  8096. +config BCM2708_SPIDEV
  8097. + bool "Bind spidev to SPI0 master"
  8098. + depends on MACH_BCM2708 && !USE_OF
  8099. + depends on SPI
  8100. + default y
  8101. + help
  8102. + Binds spidev driver to the SPI0 master
  8103. +endmenu
  8104. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/Makefile linux-rpi/arch/arm/mach-bcm2708/Makefile
  8105. --- linux-3.17.5/arch/arm/mach-bcm2708/Makefile 1969-12-31 18:00:00.000000000 -0600
  8106. +++ linux-rpi/arch/arm/mach-bcm2708/Makefile 2014-12-11 14:05:36.828418001 -0600
  8107. @@ -0,0 +1,7 @@
  8108. +#
  8109. +# Makefile for the linux kernel.
  8110. +#
  8111. +
  8112. +obj-$(CONFIG_MACH_BCM2708) += bcm2708.o armctrl.o vcio.o power.o dma.o
  8113. +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
  8114. +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
  8115. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/Makefile.boot linux-rpi/arch/arm/mach-bcm2708/Makefile.boot
  8116. --- linux-3.17.5/arch/arm/mach-bcm2708/Makefile.boot 1969-12-31 18:00:00.000000000 -0600
  8117. +++ linux-rpi/arch/arm/mach-bcm2708/Makefile.boot 2014-12-11 14:02:51.516418001 -0600
  8118. @@ -0,0 +1,3 @@
  8119. + zreladdr-y := 0x00008000
  8120. +params_phys-y := 0x00000100
  8121. +initrd_phys-y := 0x00800000
  8122. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/power.c linux-rpi/arch/arm/mach-bcm2708/power.c
  8123. --- linux-3.17.5/arch/arm/mach-bcm2708/power.c 1969-12-31 18:00:00.000000000 -0600
  8124. +++ linux-rpi/arch/arm/mach-bcm2708/power.c 2014-12-11 14:02:51.520418001 -0600
  8125. @@ -0,0 +1,194 @@
  8126. +/*
  8127. + * linux/arch/arm/mach-bcm2708/power.c
  8128. + *
  8129. + * Copyright (C) 2010 Broadcom
  8130. + *
  8131. + * This program is free software; you can redistribute it and/or modify
  8132. + * it under the terms of the GNU General Public License version 2 as
  8133. + * published by the Free Software Foundation.
  8134. + *
  8135. + * This device provides a shared mechanism for controlling the power to
  8136. + * VideoCore subsystems.
  8137. + */
  8138. +
  8139. +#include <linux/module.h>
  8140. +#include <linux/semaphore.h>
  8141. +#include <linux/bug.h>
  8142. +#include <mach/power.h>
  8143. +#include <mach/vcio.h>
  8144. +#include <mach/arm_power.h>
  8145. +
  8146. +#define DRIVER_NAME "bcm2708_power"
  8147. +
  8148. +#define BCM_POWER_MAXCLIENTS 4
  8149. +#define BCM_POWER_NOCLIENT (1<<31)
  8150. +
  8151. +/* Some drivers expect there devices to be permanently powered */
  8152. +#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
  8153. +
  8154. +#if 1
  8155. +#define DPRINTK printk
  8156. +#else
  8157. +#define DPRINTK if (0) printk
  8158. +#endif
  8159. +
  8160. +struct state_struct {
  8161. + uint32_t global_request;
  8162. + uint32_t client_request[BCM_POWER_MAXCLIENTS];
  8163. + struct semaphore client_mutex;
  8164. + struct semaphore mutex;
  8165. +} g_state;
  8166. +
  8167. +int bcm_power_open(BCM_POWER_HANDLE_T *handle)
  8168. +{
  8169. + BCM_POWER_HANDLE_T i;
  8170. + int ret = -EBUSY;
  8171. +
  8172. + down(&g_state.client_mutex);
  8173. +
  8174. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  8175. + if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
  8176. + g_state.client_request[i] = BCM_POWER_NONE;
  8177. + *handle = i;
  8178. + ret = 0;
  8179. + break;
  8180. + }
  8181. + }
  8182. +
  8183. + up(&g_state.client_mutex);
  8184. +
  8185. + DPRINTK("bcm_power_open() -> %d\n", *handle);
  8186. +
  8187. + return ret;
  8188. +}
  8189. +EXPORT_SYMBOL_GPL(bcm_power_open);
  8190. +
  8191. +int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
  8192. +{
  8193. + int rc = 0;
  8194. +
  8195. + DPRINTK("bcm_power_request(%d, %x)\n", handle, request);
  8196. +
  8197. + if ((handle < BCM_POWER_MAXCLIENTS) &&
  8198. + (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
  8199. + if (down_interruptible(&g_state.mutex) != 0) {
  8200. + DPRINTK("bcm_power_request -> interrupted\n");
  8201. + return -EINTR;
  8202. + }
  8203. +
  8204. + if (request != g_state.client_request[handle]) {
  8205. + uint32_t others_request = 0;
  8206. + uint32_t global_request;
  8207. + BCM_POWER_HANDLE_T i;
  8208. +
  8209. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  8210. + if (i != handle)
  8211. + others_request |=
  8212. + g_state.client_request[i];
  8213. + }
  8214. + others_request &= ~BCM_POWER_NOCLIENT;
  8215. +
  8216. + global_request = request | others_request;
  8217. + if (global_request != g_state.global_request) {
  8218. + uint32_t actual;
  8219. +
  8220. + /* Send a request to VideoCore */
  8221. + bcm_mailbox_write(MBOX_CHAN_POWER,
  8222. + global_request << 4);
  8223. +
  8224. + /* Wait for a response during power-up */
  8225. + if (global_request & ~g_state.global_request) {
  8226. + rc = bcm_mailbox_read(MBOX_CHAN_POWER,
  8227. + &actual);
  8228. + DPRINTK
  8229. + ("bcm_mailbox_read -> %08x, %d\n",
  8230. + actual, rc);
  8231. + actual >>= 4;
  8232. + } else {
  8233. + rc = 0;
  8234. + actual = global_request;
  8235. + }
  8236. +
  8237. + if (rc == 0) {
  8238. + if (actual != global_request) {
  8239. + printk(KERN_ERR
  8240. + "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
  8241. + __func__,
  8242. + g_state.global_request,
  8243. + global_request, actual, request, others_request);
  8244. + /* A failure */
  8245. + BUG_ON((others_request & actual)
  8246. + != others_request);
  8247. + request &= actual;
  8248. + rc = -EIO;
  8249. + }
  8250. +
  8251. + g_state.global_request = actual;
  8252. + g_state.client_request[handle] =
  8253. + request;
  8254. + }
  8255. + }
  8256. + }
  8257. + up(&g_state.mutex);
  8258. + } else {
  8259. + rc = -EINVAL;
  8260. + }
  8261. + DPRINTK("bcm_power_request -> %d\n", rc);
  8262. + return rc;
  8263. +}
  8264. +EXPORT_SYMBOL_GPL(bcm_power_request);
  8265. +
  8266. +int bcm_power_close(BCM_POWER_HANDLE_T handle)
  8267. +{
  8268. + int rc;
  8269. +
  8270. + DPRINTK("bcm_power_close(%d)\n", handle);
  8271. +
  8272. + rc = bcm_power_request(handle, BCM_POWER_NONE);
  8273. + if (rc == 0)
  8274. + g_state.client_request[handle] = BCM_POWER_NOCLIENT;
  8275. +
  8276. + return rc;
  8277. +}
  8278. +EXPORT_SYMBOL_GPL(bcm_power_close);
  8279. +
  8280. +static int __init bcm_power_init(void)
  8281. +{
  8282. +#if defined(BCM_POWER_ALWAYS_ON)
  8283. + BCM_POWER_HANDLE_T always_on_handle;
  8284. +#endif
  8285. + int rc = 0;
  8286. + int i;
  8287. +
  8288. + printk(KERN_INFO "bcm_power: Broadcom power driver\n");
  8289. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  8290. +
  8291. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
  8292. + g_state.client_request[i] = BCM_POWER_NOCLIENT;
  8293. +
  8294. + sema_init(&g_state.client_mutex, 1);
  8295. + sema_init(&g_state.mutex, 1);
  8296. +
  8297. + g_state.global_request = 0;
  8298. +
  8299. +#if defined(BCM_POWER_ALWAYS_ON)
  8300. + if (BCM_POWER_ALWAYS_ON) {
  8301. + bcm_power_open(&always_on_handle);
  8302. + bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
  8303. + }
  8304. +#endif
  8305. +
  8306. + return rc;
  8307. +}
  8308. +
  8309. +static void __exit bcm_power_exit(void)
  8310. +{
  8311. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  8312. +}
  8313. +
  8314. +arch_initcall(bcm_power_init); /* Initialize early */
  8315. +module_exit(bcm_power_exit);
  8316. +
  8317. +MODULE_AUTHOR("Phil Elwell");
  8318. +MODULE_DESCRIPTION("Interface to BCM2708 power management");
  8319. +MODULE_LICENSE("GPL");
  8320. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/vcio.c linux-rpi/arch/arm/mach-bcm2708/vcio.c
  8321. --- linux-3.17.5/arch/arm/mach-bcm2708/vcio.c 1969-12-31 18:00:00.000000000 -0600
  8322. +++ linux-rpi/arch/arm/mach-bcm2708/vcio.c 2014-12-11 14:02:51.520418001 -0600
  8323. @@ -0,0 +1,474 @@
  8324. +/*
  8325. + * linux/arch/arm/mach-bcm2708/vcio.c
  8326. + *
  8327. + * Copyright (C) 2010 Broadcom
  8328. + *
  8329. + * This program is free software; you can redistribute it and/or modify
  8330. + * it under the terms of the GNU General Public License version 2 as
  8331. + * published by the Free Software Foundation.
  8332. + *
  8333. + * This device provides a shared mechanism for writing to the mailboxes,
  8334. + * semaphores, doorbells etc. that are shared between the ARM and the
  8335. + * VideoCore processor
  8336. + */
  8337. +
  8338. +#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  8339. +#define SUPPORT_SYSRQ
  8340. +#endif
  8341. +
  8342. +#include <linux/module.h>
  8343. +#include <linux/console.h>
  8344. +#include <linux/serial_core.h>
  8345. +#include <linux/serial.h>
  8346. +#include <linux/errno.h>
  8347. +#include <linux/device.h>
  8348. +#include <linux/init.h>
  8349. +#include <linux/mm.h>
  8350. +#include <linux/dma-mapping.h>
  8351. +#include <linux/platform_device.h>
  8352. +#include <linux/sysrq.h>
  8353. +#include <linux/delay.h>
  8354. +#include <linux/slab.h>
  8355. +#include <linux/interrupt.h>
  8356. +#include <linux/irq.h>
  8357. +
  8358. +#include <linux/io.h>
  8359. +
  8360. +#include <mach/vcio.h>
  8361. +#include <mach/platform.h>
  8362. +
  8363. +#include <asm/uaccess.h>
  8364. +
  8365. +
  8366. +#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
  8367. +
  8368. +/* ----------------------------------------------------------------------
  8369. + * Mailbox
  8370. + * -------------------------------------------------------------------- */
  8371. +
  8372. +/* offsets from a mail box base address */
  8373. +#define MAIL_WRT 0x00 /* write - and next 4 words */
  8374. +#define MAIL_RD 0x00 /* read - and next 4 words */
  8375. +#define MAIL_POL 0x10 /* read without popping the fifo */
  8376. +#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
  8377. +#define MAIL_STA 0x18 /* status */
  8378. +#define MAIL_CNF 0x1C /* configuration */
  8379. +
  8380. +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  8381. +#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
  8382. +#define MBOX_CHAN(msg) ((msg) & 0xf)
  8383. +#define MBOX_DATA28(msg) ((msg) & ~0xf)
  8384. +#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
  8385. +
  8386. +#define MBOX_MAGIC 0xd0d0c0de
  8387. +
  8388. +struct vc_mailbox {
  8389. + struct device *dev; /* parent device */
  8390. + void __iomem *status;
  8391. + void __iomem *config;
  8392. + void __iomem *read;
  8393. + void __iomem *write;
  8394. + uint32_t msg[MBOX_CHAN_COUNT];
  8395. + struct semaphore sema[MBOX_CHAN_COUNT];
  8396. + uint32_t magic;
  8397. +};
  8398. +
  8399. +static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
  8400. + uint32_t addr_mbox)
  8401. +{
  8402. + int i;
  8403. +
  8404. + mbox_out->dev = dev;
  8405. + mbox_out->status = __io_address(addr_mbox + MAIL_STA);
  8406. + mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
  8407. + mbox_out->read = __io_address(addr_mbox + MAIL_RD);
  8408. + /* Write to the other mailbox */
  8409. + mbox_out->write =
  8410. + __io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
  8411. + MAIL_WRT);
  8412. +
  8413. + for (i = 0; i < MBOX_CHAN_COUNT; i++) {
  8414. + mbox_out->msg[i] = 0;
  8415. + sema_init(&mbox_out->sema[i], 0);
  8416. + }
  8417. +
  8418. + /* Enable the interrupt on data reception */
  8419. + writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
  8420. +
  8421. + mbox_out->magic = MBOX_MAGIC;
  8422. +}
  8423. +
  8424. +static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
  8425. +{
  8426. + int rc;
  8427. +
  8428. + if (mbox->magic != MBOX_MAGIC)
  8429. + rc = -EINVAL;
  8430. + else {
  8431. + /* wait for the mailbox FIFO to have some space in it */
  8432. + while (0 != (readl(mbox->status) & ARM_MS_FULL))
  8433. + cpu_relax();
  8434. +
  8435. + writel(MBOX_MSG(chan, data28), mbox->write);
  8436. + rc = 0;
  8437. + }
  8438. + return rc;
  8439. +}
  8440. +
  8441. +static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
  8442. +{
  8443. + int rc;
  8444. +
  8445. + if (mbox->magic != MBOX_MAGIC)
  8446. + rc = -EINVAL;
  8447. + else {
  8448. + down(&mbox->sema[chan]);
  8449. + *data28 = MBOX_DATA28(mbox->msg[chan]);
  8450. + mbox->msg[chan] = 0;
  8451. + rc = 0;
  8452. + }
  8453. + return rc;
  8454. +}
  8455. +
  8456. +static irqreturn_t mbox_irq(int irq, void *dev_id)
  8457. +{
  8458. + /* wait for the mailbox FIFO to have some data in it */
  8459. + struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
  8460. + int status = readl(mbox->status);
  8461. + int ret = IRQ_NONE;
  8462. +
  8463. + while (!(status & ARM_MS_EMPTY)) {
  8464. + uint32_t msg = readl(mbox->read);
  8465. + int chan = MBOX_CHAN(msg);
  8466. + if (chan < MBOX_CHAN_COUNT) {
  8467. + if (mbox->msg[chan]) {
  8468. + /* Overflow */
  8469. + printk(KERN_ERR DRIVER_NAME
  8470. + ": mbox chan %d overflow - drop %08x\n",
  8471. + chan, msg);
  8472. + } else {
  8473. + mbox->msg[chan] = (msg | 0xf);
  8474. + up(&mbox->sema[chan]);
  8475. + }
  8476. + } else {
  8477. + printk(KERN_ERR DRIVER_NAME
  8478. + ": invalid channel selector (msg %08x)\n", msg);
  8479. + }
  8480. + ret = IRQ_HANDLED;
  8481. + status = readl(mbox->status);
  8482. + }
  8483. + return ret;
  8484. +}
  8485. +
  8486. +static struct irqaction mbox_irqaction = {
  8487. + .name = "ARM Mailbox IRQ",
  8488. + .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  8489. + .handler = mbox_irq,
  8490. +};
  8491. +
  8492. +/* ----------------------------------------------------------------------
  8493. + * Mailbox Methods
  8494. + * -------------------------------------------------------------------- */
  8495. +
  8496. +static struct device *mbox_dev; /* we assume there's only one! */
  8497. +
  8498. +static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
  8499. +{
  8500. + int rc;
  8501. +
  8502. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  8503. + device_lock(dev);
  8504. + rc = mbox_write(mailbox, chan, data28);
  8505. + device_unlock(dev);
  8506. +
  8507. + return rc;
  8508. +}
  8509. +
  8510. +static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
  8511. +{
  8512. + int rc;
  8513. +
  8514. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  8515. + device_lock(dev);
  8516. + rc = mbox_read(mailbox, chan, data28);
  8517. + device_unlock(dev);
  8518. +
  8519. + return rc;
  8520. +}
  8521. +
  8522. +extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
  8523. +{
  8524. + if (mbox_dev)
  8525. + return dev_mbox_write(mbox_dev, chan, data28);
  8526. + else
  8527. + return -ENODEV;
  8528. +}
  8529. +EXPORT_SYMBOL_GPL(bcm_mailbox_write);
  8530. +
  8531. +extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
  8532. +{
  8533. + if (mbox_dev)
  8534. + return dev_mbox_read(mbox_dev, chan, data28);
  8535. + else
  8536. + return -ENODEV;
  8537. +}
  8538. +EXPORT_SYMBOL_GPL(bcm_mailbox_read);
  8539. +
  8540. +static void dev_mbox_register(const char *dev_name, struct device *dev)
  8541. +{
  8542. + mbox_dev = dev;
  8543. +}
  8544. +
  8545. +static int mbox_copy_from_user(void *dst, const void *src, int size)
  8546. +{
  8547. + if ( (uint32_t)src < TASK_SIZE)
  8548. + {
  8549. + return copy_from_user(dst, src, size);
  8550. + }
  8551. + else
  8552. + {
  8553. + memcpy( dst, src, size );
  8554. + return 0;
  8555. + }
  8556. +}
  8557. +
  8558. +static int mbox_copy_to_user(void *dst, const void *src, int size)
  8559. +{
  8560. + if ( (uint32_t)dst < TASK_SIZE)
  8561. + {
  8562. + return copy_to_user(dst, src, size);
  8563. + }
  8564. + else
  8565. + {
  8566. + memcpy( dst, src, size );
  8567. + return 0;
  8568. + }
  8569. +}
  8570. +
  8571. +static DEFINE_MUTEX(mailbox_lock);
  8572. +extern int bcm_mailbox_property(void *data, int size)
  8573. +{
  8574. + uint32_t success;
  8575. + dma_addr_t mem_bus; /* the memory address accessed from videocore */
  8576. + void *mem_kern; /* the memory address accessed from driver */
  8577. + int s = 0;
  8578. +
  8579. + mutex_lock(&mailbox_lock);
  8580. + /* allocate some memory for the messages communicating with GPU */
  8581. + mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
  8582. + if (mem_kern) {
  8583. + /* create the message */
  8584. + mbox_copy_from_user(mem_kern, data, size);
  8585. +
  8586. + /* send the message */
  8587. + wmb();
  8588. + s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
  8589. + if (s == 0) {
  8590. + s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
  8591. + }
  8592. + if (s == 0) {
  8593. + /* copy the response */
  8594. + rmb();
  8595. + mbox_copy_to_user(data, mem_kern, size);
  8596. + }
  8597. + dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
  8598. + } else {
  8599. + s = -ENOMEM;
  8600. + }
  8601. + if (s != 0)
  8602. + printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
  8603. +
  8604. + mutex_unlock(&mailbox_lock);
  8605. + return s;
  8606. +}
  8607. +EXPORT_SYMBOL_GPL(bcm_mailbox_property);
  8608. +
  8609. +/* ----------------------------------------------------------------------
  8610. + * Platform Device for Mailbox
  8611. + * -------------------------------------------------------------------- */
  8612. +
  8613. +/*
  8614. + * Is the device open right now? Used to prevent
  8615. + * concurent access into the same device
  8616. + */
  8617. +static int Device_Open = 0;
  8618. +
  8619. +/*
  8620. + * This is called whenever a process attempts to open the device file
  8621. + */
  8622. +static int device_open(struct inode *inode, struct file *file)
  8623. +{
  8624. + /*
  8625. + * We don't want to talk to two processes at the same time
  8626. + */
  8627. + if (Device_Open)
  8628. + return -EBUSY;
  8629. +
  8630. + Device_Open++;
  8631. + /*
  8632. + * Initialize the message
  8633. + */
  8634. + try_module_get(THIS_MODULE);
  8635. + return 0;
  8636. +}
  8637. +
  8638. +static int device_release(struct inode *inode, struct file *file)
  8639. +{
  8640. + /*
  8641. + * We're now ready for our next caller
  8642. + */
  8643. + Device_Open--;
  8644. +
  8645. + module_put(THIS_MODULE);
  8646. + return 0;
  8647. +}
  8648. +
  8649. +/*
  8650. + * This function is called whenever a process tries to do an ioctl on our
  8651. + * device file. We get two extra parameters (additional to the inode and file
  8652. + * structures, which all device functions get): the number of the ioctl called
  8653. + * and the parameter given to the ioctl function.
  8654. + *
  8655. + * If the ioctl is write or read/write (meaning output is returned to the
  8656. + * calling process), the ioctl call returns the output of this function.
  8657. + *
  8658. + */
  8659. +static long device_ioctl(struct file *file, /* see include/linux/fs.h */
  8660. + unsigned int ioctl_num, /* number and param for ioctl */
  8661. + unsigned long ioctl_param)
  8662. +{
  8663. + unsigned size;
  8664. + /*
  8665. + * Switch according to the ioctl called
  8666. + */
  8667. + switch (ioctl_num) {
  8668. + case IOCTL_MBOX_PROPERTY:
  8669. + /*
  8670. + * Receive a pointer to a message (in user space) and set that
  8671. + * to be the device's message. Get the parameter given to
  8672. + * ioctl by the process.
  8673. + */
  8674. + mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
  8675. + return bcm_mailbox_property((void *)ioctl_param, size);
  8676. + break;
  8677. + default:
  8678. + printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
  8679. + return -EINVAL;
  8680. + }
  8681. +
  8682. + return 0;
  8683. +}
  8684. +
  8685. +/* Module Declarations */
  8686. +
  8687. +/*
  8688. + * This structure will hold the functions to be called
  8689. + * when a process does something to the device we
  8690. + * created. Since a pointer to this structure is kept in
  8691. + * the devices table, it can't be local to
  8692. + * init_module. NULL is for unimplemented functios.
  8693. + */
  8694. +struct file_operations fops = {
  8695. + .unlocked_ioctl = device_ioctl,
  8696. + .open = device_open,
  8697. + .release = device_release, /* a.k.a. close */
  8698. +};
  8699. +
  8700. +static int bcm_vcio_probe(struct platform_device *pdev)
  8701. +{
  8702. + int ret = 0;
  8703. + struct vc_mailbox *mailbox;
  8704. +
  8705. + mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
  8706. + if (NULL == mailbox) {
  8707. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  8708. + "mailbox memory\n");
  8709. + ret = -ENOMEM;
  8710. + } else {
  8711. + struct resource *res;
  8712. +
  8713. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  8714. + if (res == NULL) {
  8715. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  8716. + "resource\n");
  8717. + ret = -ENODEV;
  8718. + kfree(mailbox);
  8719. + } else {
  8720. + /* should be based on the registers from res really */
  8721. + mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
  8722. +
  8723. + platform_set_drvdata(pdev, mailbox);
  8724. + dev_mbox_register(DRIVER_NAME, &pdev->dev);
  8725. +
  8726. + mbox_irqaction.dev_id = mailbox;
  8727. + setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
  8728. + printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
  8729. + __io_address(ARM_0_MAIL0_RD));
  8730. + }
  8731. + }
  8732. +
  8733. + if (ret == 0) {
  8734. + /*
  8735. + * Register the character device
  8736. + */
  8737. + ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
  8738. +
  8739. + /*
  8740. + * Negative values signify an error
  8741. + */
  8742. + if (ret < 0) {
  8743. + printk(KERN_ERR DRIVER_NAME
  8744. + "Failed registering the character device %d\n", ret);
  8745. + return ret;
  8746. + }
  8747. + }
  8748. + return ret;
  8749. +}
  8750. +
  8751. +static int bcm_vcio_remove(struct platform_device *pdev)
  8752. +{
  8753. + struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
  8754. +
  8755. + platform_set_drvdata(pdev, NULL);
  8756. + kfree(mailbox);
  8757. +
  8758. + return 0;
  8759. +}
  8760. +
  8761. +static struct platform_driver bcm_mbox_driver = {
  8762. + .probe = bcm_vcio_probe,
  8763. + .remove = bcm_vcio_remove,
  8764. +
  8765. + .driver = {
  8766. + .name = DRIVER_NAME,
  8767. + .owner = THIS_MODULE,
  8768. + },
  8769. +};
  8770. +
  8771. +static int __init bcm_mbox_init(void)
  8772. +{
  8773. + int ret;
  8774. +
  8775. + printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
  8776. +
  8777. + ret = platform_driver_register(&bcm_mbox_driver);
  8778. + if (ret != 0) {
  8779. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  8780. + "on platform\n");
  8781. + }
  8782. +
  8783. + return ret;
  8784. +}
  8785. +
  8786. +static void __exit bcm_mbox_exit(void)
  8787. +{
  8788. + platform_driver_unregister(&bcm_mbox_driver);
  8789. +}
  8790. +
  8791. +arch_initcall(bcm_mbox_init); /* Initialize early */
  8792. +module_exit(bcm_mbox_exit);
  8793. +
  8794. +MODULE_AUTHOR("Gray Girling");
  8795. +MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
  8796. +MODULE_LICENSE("GPL");
  8797. +MODULE_ALIAS("platform:bcm-mbox");
  8798. diff -Nur linux-3.17.5/arch/arm/mach-bcm2708/vc_mem.c linux-rpi/arch/arm/mach-bcm2708/vc_mem.c
  8799. --- linux-3.17.5/arch/arm/mach-bcm2708/vc_mem.c 1969-12-31 18:00:00.000000000 -0600
  8800. +++ linux-rpi/arch/arm/mach-bcm2708/vc_mem.c 2014-12-11 14:02:51.520418001 -0600
  8801. @@ -0,0 +1,432 @@
  8802. +/*****************************************************************************
  8803. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  8804. +*
  8805. +* Unless you and Broadcom execute a separate written software license
  8806. +* agreement governing use of this software, this software is licensed to you
  8807. +* under the terms of the GNU General Public License version 2, available at
  8808. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8809. +*
  8810. +* Notwithstanding the above, under no circumstances may you combine this
  8811. +* software in any way with any other Broadcom software provided under a
  8812. +* license other than the GPL, without Broadcom's express prior written
  8813. +* consent.
  8814. +*****************************************************************************/
  8815. +
  8816. +#include <linux/kernel.h>
  8817. +#include <linux/module.h>
  8818. +#include <linux/fs.h>
  8819. +#include <linux/device.h>
  8820. +#include <linux/cdev.h>
  8821. +#include <linux/mm.h>
  8822. +#include <linux/slab.h>
  8823. +#include <linux/debugfs.h>
  8824. +#include <asm/uaccess.h>
  8825. +#include <linux/dma-mapping.h>
  8826. +
  8827. +#ifdef CONFIG_ARCH_KONA
  8828. +#include <chal/chal_ipc.h>
  8829. +#elif CONFIG_ARCH_BCM2708
  8830. +#else
  8831. +#include <csp/chal_ipc.h>
  8832. +#endif
  8833. +
  8834. +#include "mach/vc_mem.h"
  8835. +#include <mach/vcio.h>
  8836. +
  8837. +#define DRIVER_NAME "vc-mem"
  8838. +
  8839. +// Device (/dev) related variables
  8840. +static dev_t vc_mem_devnum = 0;
  8841. +static struct class *vc_mem_class = NULL;
  8842. +static struct cdev vc_mem_cdev;
  8843. +static int vc_mem_inited = 0;
  8844. +
  8845. +#ifdef CONFIG_DEBUG_FS
  8846. +static struct dentry *vc_mem_debugfs_entry;
  8847. +#endif
  8848. +
  8849. +/*
  8850. + * Videocore memory addresses and size
  8851. + *
  8852. + * Drivers that wish to know the videocore memory addresses and sizes should
  8853. + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
  8854. + * headers. This allows the other drivers to not be tied down to a a certain
  8855. + * address/size at compile time.
  8856. + *
  8857. + * In the future, the goal is to have the videocore memory virtual address and
  8858. + * size be calculated at boot time rather than at compile time. The decision of
  8859. + * where the videocore memory resides and its size would be in the hands of the
  8860. + * bootloader (and/or kernel). When that happens, the values of these variables
  8861. + * would be calculated and assigned in the init function.
  8862. + */
  8863. +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
  8864. +unsigned long mm_vc_mem_phys_addr = 0x00000000;
  8865. +unsigned int mm_vc_mem_size = 0;
  8866. +unsigned int mm_vc_mem_base = 0;
  8867. +
  8868. +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
  8869. +EXPORT_SYMBOL(mm_vc_mem_size);
  8870. +EXPORT_SYMBOL(mm_vc_mem_base);
  8871. +
  8872. +static uint phys_addr = 0;
  8873. +static uint mem_size = 0;
  8874. +static uint mem_base = 0;
  8875. +
  8876. +
  8877. +/****************************************************************************
  8878. +*
  8879. +* vc_mem_open
  8880. +*
  8881. +***************************************************************************/
  8882. +
  8883. +static int
  8884. +vc_mem_open(struct inode *inode, struct file *file)
  8885. +{
  8886. + (void) inode;
  8887. + (void) file;
  8888. +
  8889. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  8890. +
  8891. + return 0;
  8892. +}
  8893. +
  8894. +/****************************************************************************
  8895. +*
  8896. +* vc_mem_release
  8897. +*
  8898. +***************************************************************************/
  8899. +
  8900. +static int
  8901. +vc_mem_release(struct inode *inode, struct file *file)
  8902. +{
  8903. + (void) inode;
  8904. + (void) file;
  8905. +
  8906. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  8907. +
  8908. + return 0;
  8909. +}
  8910. +
  8911. +/****************************************************************************
  8912. +*
  8913. +* vc_mem_get_size
  8914. +*
  8915. +***************************************************************************/
  8916. +
  8917. +static void
  8918. +vc_mem_get_size(void)
  8919. +{
  8920. +}
  8921. +
  8922. +/****************************************************************************
  8923. +*
  8924. +* vc_mem_get_base
  8925. +*
  8926. +***************************************************************************/
  8927. +
  8928. +static void
  8929. +vc_mem_get_base(void)
  8930. +{
  8931. +}
  8932. +
  8933. +/****************************************************************************
  8934. +*
  8935. +* vc_mem_get_current_size
  8936. +*
  8937. +***************************************************************************/
  8938. +
  8939. +int
  8940. +vc_mem_get_current_size(void)
  8941. +{
  8942. + return mm_vc_mem_size;
  8943. +}
  8944. +
  8945. +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
  8946. +
  8947. +/****************************************************************************
  8948. +*
  8949. +* vc_mem_ioctl
  8950. +*
  8951. +***************************************************************************/
  8952. +
  8953. +static long
  8954. +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  8955. +{
  8956. + int rc = 0;
  8957. +
  8958. + (void) cmd;
  8959. + (void) arg;
  8960. +
  8961. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  8962. +
  8963. + switch (cmd) {
  8964. + case VC_MEM_IOC_MEM_PHYS_ADDR:
  8965. + {
  8966. + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
  8967. + __func__, (void *) mm_vc_mem_phys_addr);
  8968. +
  8969. + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
  8970. + sizeof (mm_vc_mem_phys_addr)) != 0) {
  8971. + rc = -EFAULT;
  8972. + }
  8973. + break;
  8974. + }
  8975. + case VC_MEM_IOC_MEM_SIZE:
  8976. + {
  8977. + // Get the videocore memory size first
  8978. + vc_mem_get_size();
  8979. +
  8980. + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
  8981. + mm_vc_mem_size);
  8982. +
  8983. + if (copy_to_user((void *) arg, &mm_vc_mem_size,
  8984. + sizeof (mm_vc_mem_size)) != 0) {
  8985. + rc = -EFAULT;
  8986. + }
  8987. + break;
  8988. + }
  8989. + case VC_MEM_IOC_MEM_BASE:
  8990. + {
  8991. + // Get the videocore memory base
  8992. + vc_mem_get_base();
  8993. +
  8994. + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
  8995. + mm_vc_mem_base);
  8996. +
  8997. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  8998. + sizeof (mm_vc_mem_base)) != 0) {
  8999. + rc = -EFAULT;
  9000. + }
  9001. + break;
  9002. + }
  9003. + case VC_MEM_IOC_MEM_LOAD:
  9004. + {
  9005. + // Get the videocore memory base
  9006. + vc_mem_get_base();
  9007. +
  9008. + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
  9009. + mm_vc_mem_base);
  9010. +
  9011. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  9012. + sizeof (mm_vc_mem_base)) != 0) {
  9013. + rc = -EFAULT;
  9014. + }
  9015. + break;
  9016. + }
  9017. + default:
  9018. + {
  9019. + return -ENOTTY;
  9020. + }
  9021. + }
  9022. + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
  9023. +
  9024. + return rc;
  9025. +}
  9026. +
  9027. +/****************************************************************************
  9028. +*
  9029. +* vc_mem_mmap
  9030. +*
  9031. +***************************************************************************/
  9032. +
  9033. +static int
  9034. +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
  9035. +{
  9036. + int rc = 0;
  9037. + unsigned long length = vma->vm_end - vma->vm_start;
  9038. + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  9039. +
  9040. + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
  9041. + __func__, (long) vma->vm_start, (long) vma->vm_end,
  9042. + (long) vma->vm_pgoff);
  9043. +
  9044. + if (offset + length > mm_vc_mem_size) {
  9045. + pr_err("%s: length %ld is too big\n", __func__, length);
  9046. + return -EINVAL;
  9047. + }
  9048. + // Do not cache the memory map
  9049. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  9050. +
  9051. + rc = remap_pfn_range(vma, vma->vm_start,
  9052. + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
  9053. + vma->vm_pgoff, length, vma->vm_page_prot);
  9054. + if (rc != 0) {
  9055. + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
  9056. + }
  9057. +
  9058. + return rc;
  9059. +}
  9060. +
  9061. +/****************************************************************************
  9062. +*
  9063. +* File Operations for the driver.
  9064. +*
  9065. +***************************************************************************/
  9066. +
  9067. +static const struct file_operations vc_mem_fops = {
  9068. + .owner = THIS_MODULE,
  9069. + .open = vc_mem_open,
  9070. + .release = vc_mem_release,
  9071. + .unlocked_ioctl = vc_mem_ioctl,
  9072. + .mmap = vc_mem_mmap,
  9073. +};
  9074. +
  9075. +#ifdef CONFIG_DEBUG_FS
  9076. +static void vc_mem_debugfs_deinit(void)
  9077. +{
  9078. + debugfs_remove_recursive(vc_mem_debugfs_entry);
  9079. + vc_mem_debugfs_entry = NULL;
  9080. +}
  9081. +
  9082. +
  9083. +static int vc_mem_debugfs_init(
  9084. + struct device *dev)
  9085. +{
  9086. + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
  9087. + if (!vc_mem_debugfs_entry) {
  9088. + dev_warn(dev, "could not create debugfs entry\n");
  9089. + return -EFAULT;
  9090. + }
  9091. +
  9092. + if (!debugfs_create_x32("vc_mem_phys_addr",
  9093. + 0444,
  9094. + vc_mem_debugfs_entry,
  9095. + (u32 *)&mm_vc_mem_phys_addr)) {
  9096. + dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
  9097. + __func__);
  9098. + goto fail;
  9099. + }
  9100. +
  9101. + if (!debugfs_create_x32("vc_mem_size",
  9102. + 0444,
  9103. + vc_mem_debugfs_entry,
  9104. + (u32 *)&mm_vc_mem_size)) {
  9105. + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
  9106. + __func__);
  9107. + goto fail;
  9108. + }
  9109. +
  9110. + if (!debugfs_create_x32("vc_mem_base",
  9111. + 0444,
  9112. + vc_mem_debugfs_entry,
  9113. + (u32 *)&mm_vc_mem_base)) {
  9114. + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
  9115. + __func__);
  9116. + goto fail;
  9117. + }
  9118. +
  9119. + return 0;
  9120. +
  9121. +fail:
  9122. + vc_mem_debugfs_deinit();
  9123. + return -EFAULT;
  9124. +}
  9125. +
  9126. +#endif /* CONFIG_DEBUG_FS */
  9127. +
  9128. +
  9129. +/****************************************************************************
  9130. +*
  9131. +* vc_mem_init
  9132. +*
  9133. +***************************************************************************/
  9134. +
  9135. +static int __init
  9136. +vc_mem_init(void)
  9137. +{
  9138. + int rc = -EFAULT;
  9139. + struct device *dev;
  9140. +
  9141. + pr_debug("%s: called\n", __func__);
  9142. +
  9143. + mm_vc_mem_phys_addr = phys_addr;
  9144. + mm_vc_mem_size = mem_size;
  9145. + mm_vc_mem_base = mem_base;
  9146. +
  9147. + vc_mem_get_size();
  9148. +
  9149. + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
  9150. + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
  9151. +
  9152. + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
  9153. + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
  9154. + __func__, rc);
  9155. + goto out_err;
  9156. + }
  9157. +
  9158. + cdev_init(&vc_mem_cdev, &vc_mem_fops);
  9159. + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
  9160. + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
  9161. + goto out_unregister;
  9162. + }
  9163. +
  9164. + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
  9165. + if (IS_ERR(vc_mem_class)) {
  9166. + rc = PTR_ERR(vc_mem_class);
  9167. + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
  9168. + goto out_cdev_del;
  9169. + }
  9170. +
  9171. + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
  9172. + DRIVER_NAME);
  9173. + if (IS_ERR(dev)) {
  9174. + rc = PTR_ERR(dev);
  9175. + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
  9176. + goto out_class_destroy;
  9177. + }
  9178. +
  9179. +#ifdef CONFIG_DEBUG_FS
  9180. + /* don't fail if the debug entries cannot be created */
  9181. + vc_mem_debugfs_init(dev);
  9182. +#endif
  9183. +
  9184. + vc_mem_inited = 1;
  9185. + return 0;
  9186. +
  9187. + device_destroy(vc_mem_class, vc_mem_devnum);
  9188. +
  9189. + out_class_destroy:
  9190. + class_destroy(vc_mem_class);
  9191. + vc_mem_class = NULL;
  9192. +
  9193. + out_cdev_del:
  9194. + cdev_del(&vc_mem_cdev);
  9195. +
  9196. + out_unregister:
  9197. + unregister_chrdev_region(vc_mem_devnum, 1);
  9198. +
  9199. + out_err:
  9200. + return -1;
  9201. +}
  9202. +
  9203. +/****************************************************************************
  9204. +*
  9205. +* vc_mem_exit
  9206. +*
  9207. +***************************************************************************/
  9208. +
  9209. +static void __exit
  9210. +vc_mem_exit(void)
  9211. +{
  9212. + pr_debug("%s: called\n", __func__);
  9213. +
  9214. + if (vc_mem_inited) {
  9215. +#if CONFIG_DEBUG_FS
  9216. + vc_mem_debugfs_deinit();
  9217. +#endif
  9218. + device_destroy(vc_mem_class, vc_mem_devnum);
  9219. + class_destroy(vc_mem_class);
  9220. + cdev_del(&vc_mem_cdev);
  9221. + unregister_chrdev_region(vc_mem_devnum, 1);
  9222. + }
  9223. +}
  9224. +
  9225. +module_init(vc_mem_init);
  9226. +module_exit(vc_mem_exit);
  9227. +MODULE_LICENSE("GPL");
  9228. +MODULE_AUTHOR("Broadcom Corporation");
  9229. +
  9230. +module_param(phys_addr, uint, 0644);
  9231. +module_param(mem_size, uint, 0644);
  9232. +module_param(mem_base, uint, 0644);
  9233. +
  9234. diff -Nur linux-3.17.5/arch/arm/Makefile linux-rpi/arch/arm/Makefile
  9235. --- linux-3.17.5/arch/arm/Makefile 2014-12-06 17:57:59.000000000 -0600
  9236. +++ linux-rpi/arch/arm/Makefile 2014-12-11 14:05:36.704418001 -0600
  9237. @@ -148,6 +148,7 @@
  9238. machine-$(CONFIG_ARCH_AT91) += at91
  9239. machine-$(CONFIG_ARCH_AXXIA) += axxia
  9240. machine-$(CONFIG_ARCH_BCM) += bcm
  9241. +machine-$(CONFIG_ARCH_BCM2708) += bcm2708
  9242. machine-$(CONFIG_ARCH_BERLIN) += berlin
  9243. machine-$(CONFIG_ARCH_CLPS711X) += clps711x
  9244. machine-$(CONFIG_ARCH_CNS3XXX) += cns3xxx
  9245. diff -Nur linux-3.17.5/arch/arm/mm/Kconfig linux-rpi/arch/arm/mm/Kconfig
  9246. --- linux-3.17.5/arch/arm/mm/Kconfig 2014-12-06 17:57:59.000000000 -0600
  9247. +++ linux-rpi/arch/arm/mm/Kconfig 2014-12-11 14:05:36.916418001 -0600
  9248. @@ -358,7 +358,7 @@
  9249. # ARMv6
  9250. config CPU_V6
  9251. - bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX
  9252. + bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX || MACH_BCM2708
  9253. select CPU_32v6
  9254. select CPU_ABRT_EV6
  9255. select CPU_CACHE_V6
  9256. diff -Nur linux-3.17.5/arch/arm/mm/proc-v6.S linux-rpi/arch/arm/mm/proc-v6.S
  9257. --- linux-3.17.5/arch/arm/mm/proc-v6.S 2014-12-06 17:57:59.000000000 -0600
  9258. +++ linux-rpi/arch/arm/mm/proc-v6.S 2014-12-11 14:05:36.924418001 -0600
  9259. @@ -73,10 +73,19 @@
  9260. *
  9261. * IRQs are already disabled.
  9262. */
  9263. +
  9264. +/* See jira SW-5991 for details of this workaround */
  9265. ENTRY(cpu_v6_do_idle)
  9266. - mov r1, #0
  9267. - mcr p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  9268. - mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt
  9269. + .align 5
  9270. + mov r1, #2
  9271. +1: subs r1, #1
  9272. + nop
  9273. + mcreq p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  9274. + mcreq p15, 0, r1, c7, c0, 4 @ wait for interrupt
  9275. + nop
  9276. + nop
  9277. + nop
  9278. + bne 1b
  9279. ret lr
  9280. ENTRY(cpu_v6_dcache_clean_area)
  9281. diff -Nur linux-3.17.5/arch/arm/tools/mach-types linux-rpi/arch/arm/tools/mach-types
  9282. --- linux-3.17.5/arch/arm/tools/mach-types 2014-12-06 17:57:59.000000000 -0600
  9283. +++ linux-rpi/arch/arm/tools/mach-types 2014-12-11 14:02:51.684418001 -0600
  9284. @@ -522,6 +522,7 @@
  9285. prima2_evb MACH_PRIMA2_EVB PRIMA2_EVB 3103
  9286. paz00 MACH_PAZ00 PAZ00 3128
  9287. acmenetusfoxg20 MACH_ACMENETUSFOXG20 ACMENETUSFOXG20 3129
  9288. +bcm2708 MACH_BCM2708 BCM2708 3138
  9289. ag5evm MACH_AG5EVM AG5EVM 3189
  9290. ics_if_voip MACH_ICS_IF_VOIP ICS_IF_VOIP 3206
  9291. wlf_cragg_6410 MACH_WLF_CRAGG_6410 WLF_CRAGG_6410 3207
  9292. diff -Nur linux-3.17.5/Documentation/video4linux/bcm2835-v4l2.txt linux-rpi/Documentation/video4linux/bcm2835-v4l2.txt
  9293. --- linux-3.17.5/Documentation/video4linux/bcm2835-v4l2.txt 1969-12-31 18:00:00.000000000 -0600
  9294. +++ linux-rpi/Documentation/video4linux/bcm2835-v4l2.txt 2014-12-11 14:02:51.268418001 -0600
  9295. @@ -0,0 +1,60 @@
  9296. +
  9297. +BCM2835 (aka Raspberry Pi) V4L2 driver
  9298. +======================================
  9299. +
  9300. +1. Copyright
  9301. +============
  9302. +
  9303. +Copyright © 2013 Raspberry Pi (Trading) Ltd.
  9304. +
  9305. +2. License
  9306. +==========
  9307. +
  9308. +This program is free software; you can redistribute it and/or modify
  9309. +it under the terms of the GNU General Public License as published by
  9310. +the Free Software Foundation; either version 2 of the License, or
  9311. +(at your option) any later version.
  9312. +
  9313. +This program is distributed in the hope that it will be useful,
  9314. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  9315. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9316. +GNU General Public License for more details.
  9317. +
  9318. +You should have received a copy of the GNU General Public License
  9319. +along with this program; if not, write to the Free Software
  9320. +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  9321. +
  9322. +3. Quick Start
  9323. +==============
  9324. +
  9325. +You need a version 1.0 or later of v4l2-ctl, available from:
  9326. + git://git.linuxtv.org/v4l-utils.git
  9327. +
  9328. +$ sudo modprobe bcm2835-v4l2
  9329. +
  9330. +Turn on the overlay:
  9331. +
  9332. +$ v4l2-ctl --overlay=1
  9333. +
  9334. +Turn off the overlay:
  9335. +
  9336. +$ v4l2-ctl --overlay=0
  9337. +
  9338. +Set the capture format for video:
  9339. +
  9340. +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
  9341. +
  9342. +(Note: 1088 not 1080).
  9343. +
  9344. +Capture:
  9345. +
  9346. +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
  9347. +
  9348. +Stills capture:
  9349. +
  9350. +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
  9351. +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
  9352. +
  9353. +List of available formats:
  9354. +
  9355. +$ v4l2-ctl --list-formats
  9356. diff -Nur linux-3.17.5/drivers/char/broadcom/Kconfig linux-rpi/drivers/char/broadcom/Kconfig
  9357. --- linux-3.17.5/drivers/char/broadcom/Kconfig 1969-12-31 18:00:00.000000000 -0600
  9358. +++ linux-rpi/drivers/char/broadcom/Kconfig 2014-12-11 14:02:52.800418001 -0600
  9359. @@ -0,0 +1,22 @@
  9360. +#
  9361. +# Broadcom char driver config
  9362. +#
  9363. +
  9364. +menuconfig BRCM_CHAR_DRIVERS
  9365. + bool "Broadcom Char Drivers"
  9366. + help
  9367. + Broadcom's char drivers
  9368. +
  9369. +config BCM_VC_CMA
  9370. + bool "Videocore CMA"
  9371. + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
  9372. + default n
  9373. + help
  9374. + Helper for videocore CMA access.
  9375. +
  9376. +config BCM_VC_SM
  9377. + tristate "VMCS Shared Memory"
  9378. + default n
  9379. + help
  9380. + Support for the VC shared memory on the Broadcom reference
  9381. + design. Uses the VCHIQ stack.
  9382. diff -Nur linux-3.17.5/drivers/char/broadcom/Makefile linux-rpi/drivers/char/broadcom/Makefile
  9383. --- linux-3.17.5/drivers/char/broadcom/Makefile 1969-12-31 18:00:00.000000000 -0600
  9384. +++ linux-rpi/drivers/char/broadcom/Makefile 2014-12-11 14:02:52.800418001 -0600
  9385. @@ -0,0 +1,2 @@
  9386. +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
  9387. +obj-$(CONFIG_BCM_VC_SM) += vc_sm/
  9388. diff -Nur linux-3.17.5/drivers/char/broadcom/vc_cma/Makefile linux-rpi/drivers/char/broadcom/vc_cma/Makefile
  9389. --- linux-3.17.5/drivers/char/broadcom/vc_cma/Makefile 1969-12-31 18:00:00.000000000 -0600
  9390. +++ linux-rpi/drivers/char/broadcom/vc_cma/Makefile 2014-12-11 14:02:52.800418001 -0600
  9391. @@ -0,0 +1,14 @@
  9392. +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
  9393. +ccflags-y += -Werror
  9394. +ccflags-y += -Iinclude/linux/broadcom
  9395. +ccflags-y += -Idrivers/misc/vc04_services
  9396. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
  9397. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
  9398. +
  9399. +ccflags-y += -D__KERNEL__
  9400. +ccflags-y += -D__linux__
  9401. +ccflags-y += -Werror
  9402. +
  9403. +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
  9404. +
  9405. +vc-cma-objs := vc_cma.o
  9406. diff -Nur linux-3.17.5/drivers/char/broadcom/vc_cma/vc_cma.c linux-rpi/drivers/char/broadcom/vc_cma/vc_cma.c
  9407. --- linux-3.17.5/drivers/char/broadcom/vc_cma/vc_cma.c 1969-12-31 18:00:00.000000000 -0600
  9408. +++ linux-rpi/drivers/char/broadcom/vc_cma/vc_cma.c 2014-12-11 14:02:52.800418001 -0600
  9409. @@ -0,0 +1,1143 @@
  9410. +/**
  9411. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9412. + *
  9413. + * Redistribution and use in source and binary forms, with or without
  9414. + * modification, are permitted provided that the following conditions
  9415. + * are met:
  9416. + * 1. Redistributions of source code must retain the above copyright
  9417. + * notice, this list of conditions, and the following disclaimer,
  9418. + * without modification.
  9419. + * 2. Redistributions in binary form must reproduce the above copyright
  9420. + * notice, this list of conditions and the following disclaimer in the
  9421. + * documentation and/or other materials provided with the distribution.
  9422. + * 3. The names of the above-listed copyright holders may not be used
  9423. + * to endorse or promote products derived from this software without
  9424. + * specific prior written permission.
  9425. + *
  9426. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9427. + * GNU General Public License ("GPL") version 2, as published by the Free
  9428. + * Software Foundation.
  9429. + *
  9430. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9431. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9432. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9433. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9434. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9435. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9436. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9437. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9438. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9439. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9440. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9441. + */
  9442. +
  9443. +#include <linux/kernel.h>
  9444. +#include <linux/module.h>
  9445. +#include <linux/kthread.h>
  9446. +#include <linux/fs.h>
  9447. +#include <linux/device.h>
  9448. +#include <linux/cdev.h>
  9449. +#include <linux/mm.h>
  9450. +#include <linux/proc_fs.h>
  9451. +#include <linux/seq_file.h>
  9452. +#include <linux/dma-mapping.h>
  9453. +#include <linux/dma-contiguous.h>
  9454. +#include <linux/platform_device.h>
  9455. +#include <linux/uaccess.h>
  9456. +#include <asm/cacheflush.h>
  9457. +
  9458. +#include "vc_cma.h"
  9459. +
  9460. +#include "vchiq_util.h"
  9461. +#include "vchiq_connected.h"
  9462. +//#include "debug_sym.h"
  9463. +//#include "vc_mem.h"
  9464. +
  9465. +#define DRIVER_NAME "vc-cma"
  9466. +
  9467. +#define LOG_DBG(fmt, ...) \
  9468. + if (vc_cma_debug) \
  9469. + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
  9470. +#define LOG_ERR(fmt, ...) \
  9471. + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
  9472. +
  9473. +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
  9474. +#define VC_CMA_VERSION 2
  9475. +
  9476. +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
  9477. +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
  9478. +#define VC_CMA_MAX_PARAMS_PER_MSG \
  9479. + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
  9480. +#define VC_CMA_RESERVE_COUNT_MAX 16
  9481. +
  9482. +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
  9483. +
  9484. +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
  9485. +
  9486. +#define loud_error(...) \
  9487. + LOG_ERR("===== " __VA_ARGS__)
  9488. +
  9489. +enum {
  9490. + VC_CMA_MSG_QUIT,
  9491. + VC_CMA_MSG_OPEN,
  9492. + VC_CMA_MSG_TICK,
  9493. + VC_CMA_MSG_ALLOC, /* chunk count */
  9494. + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
  9495. + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
  9496. + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
  9497. + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
  9498. + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
  9499. + VC_CMA_MSG_UPDATE_RESERVE,
  9500. + VC_CMA_MSG_MAX
  9501. +};
  9502. +
  9503. +struct cma_msg {
  9504. + unsigned short type;
  9505. + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
  9506. +};
  9507. +
  9508. +struct vc_cma_reserve_user {
  9509. + unsigned int pid;
  9510. + unsigned int reserve;
  9511. +};
  9512. +
  9513. +/* Device (/dev) related variables */
  9514. +static dev_t vc_cma_devnum;
  9515. +static struct class *vc_cma_class;
  9516. +static struct cdev vc_cma_cdev;
  9517. +static int vc_cma_inited;
  9518. +static int vc_cma_debug;
  9519. +
  9520. +/* Proc entry */
  9521. +static struct proc_dir_entry *vc_cma_proc_entry;
  9522. +
  9523. +phys_addr_t vc_cma_base;
  9524. +struct page *vc_cma_base_page;
  9525. +unsigned int vc_cma_size;
  9526. +EXPORT_SYMBOL(vc_cma_size);
  9527. +unsigned int vc_cma_initial;
  9528. +unsigned int vc_cma_chunks;
  9529. +unsigned int vc_cma_chunks_used;
  9530. +unsigned int vc_cma_chunks_reserved;
  9531. +
  9532. +static int in_loud_error;
  9533. +
  9534. +unsigned int vc_cma_reserve_total;
  9535. +unsigned int vc_cma_reserve_count;
  9536. +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
  9537. +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
  9538. +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
  9539. +
  9540. +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
  9541. +static struct platform_device vc_cma_device = {
  9542. + .name = "vc-cma",
  9543. + .id = 0,
  9544. + .dev = {
  9545. + .dma_mask = &vc_cma_dma_mask,
  9546. + .coherent_dma_mask = DMA_BIT_MASK(32),
  9547. + },
  9548. +};
  9549. +
  9550. +static VCHIQ_INSTANCE_T cma_instance;
  9551. +static VCHIQ_SERVICE_HANDLE_T cma_service;
  9552. +static VCHIU_QUEUE_T cma_msg_queue;
  9553. +static struct task_struct *cma_worker;
  9554. +
  9555. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
  9556. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
  9557. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  9558. + VCHIQ_HEADER_T * header,
  9559. + VCHIQ_SERVICE_HANDLE_T service,
  9560. + void *bulk_userdata);
  9561. +static void send_vc_msg(unsigned short type,
  9562. + unsigned short param1, unsigned short param2);
  9563. +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
  9564. +
  9565. +static int early_vc_cma_mem(char *p)
  9566. +{
  9567. + unsigned int new_size;
  9568. + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
  9569. + vc_cma_size = memparse(p, &p);
  9570. + vc_cma_initial = vc_cma_size;
  9571. + if (*p == '/')
  9572. + vc_cma_size = memparse(p + 1, &p);
  9573. + if (*p == '@')
  9574. + vc_cma_base = memparse(p + 1, &p);
  9575. +
  9576. + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
  9577. + & ~(VC_CMA_CHUNK_SIZE - 1);
  9578. + if (new_size > vc_cma_size)
  9579. + vc_cma_size = 0;
  9580. + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
  9581. + & ~(VC_CMA_CHUNK_SIZE - 1);
  9582. + if (vc_cma_initial > vc_cma_size)
  9583. + vc_cma_initial = vc_cma_size;
  9584. + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
  9585. + & ~(VC_CMA_CHUNK_SIZE - 1);
  9586. +
  9587. + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
  9588. + vc_cma_size, (unsigned int)vc_cma_base);
  9589. +
  9590. + return 0;
  9591. +}
  9592. +
  9593. +early_param("vc-cma-mem", early_vc_cma_mem);
  9594. +
  9595. +void vc_cma_early_init(void)
  9596. +{
  9597. + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
  9598. + if (vc_cma_size) {
  9599. + int rc = platform_device_register(&vc_cma_device);
  9600. + LOG_DBG("platform_device_register -> %d", rc);
  9601. + }
  9602. +}
  9603. +
  9604. +void vc_cma_reserve(void)
  9605. +{
  9606. + /* if vc_cma_size is set, then declare vc CMA area of the same
  9607. + * size from the end of memory
  9608. + */
  9609. + if (vc_cma_size) {
  9610. + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
  9611. + vc_cma_base, 0) == 0) {
  9612. + } else {
  9613. + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
  9614. + vc_cma_size, (unsigned int)vc_cma_base);
  9615. + vc_cma_size = 0;
  9616. + }
  9617. + }
  9618. + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
  9619. +}
  9620. +
  9621. +/****************************************************************************
  9622. +*
  9623. +* vc_cma_open
  9624. +*
  9625. +***************************************************************************/
  9626. +
  9627. +static int vc_cma_open(struct inode *inode, struct file *file)
  9628. +{
  9629. + (void)inode;
  9630. + (void)file;
  9631. +
  9632. + return 0;
  9633. +}
  9634. +
  9635. +/****************************************************************************
  9636. +*
  9637. +* vc_cma_release
  9638. +*
  9639. +***************************************************************************/
  9640. +
  9641. +static int vc_cma_release(struct inode *inode, struct file *file)
  9642. +{
  9643. + (void)inode;
  9644. + (void)file;
  9645. +
  9646. + vc_cma_set_reserve(0, current->tgid);
  9647. +
  9648. + return 0;
  9649. +}
  9650. +
  9651. +/****************************************************************************
  9652. +*
  9653. +* vc_cma_ioctl
  9654. +*
  9655. +***************************************************************************/
  9656. +
  9657. +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  9658. +{
  9659. + int rc = 0;
  9660. +
  9661. + (void)cmd;
  9662. + (void)arg;
  9663. +
  9664. + switch (cmd) {
  9665. + case VC_CMA_IOC_RESERVE:
  9666. + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
  9667. + if (rc >= 0)
  9668. + rc = 0;
  9669. + break;
  9670. + default:
  9671. + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
  9672. + return -ENOTTY;
  9673. + }
  9674. +
  9675. + return rc;
  9676. +}
  9677. +
  9678. +/****************************************************************************
  9679. +*
  9680. +* File Operations for the driver.
  9681. +*
  9682. +***************************************************************************/
  9683. +
  9684. +static const struct file_operations vc_cma_fops = {
  9685. + .owner = THIS_MODULE,
  9686. + .open = vc_cma_open,
  9687. + .release = vc_cma_release,
  9688. + .unlocked_ioctl = vc_cma_ioctl,
  9689. +};
  9690. +
  9691. +/****************************************************************************
  9692. +*
  9693. +* vc_cma_proc_open
  9694. +*
  9695. +***************************************************************************/
  9696. +
  9697. +static int vc_cma_show_info(struct seq_file *m, void *v)
  9698. +{
  9699. + int i;
  9700. +
  9701. + seq_printf(m, "Videocore CMA:\n");
  9702. + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
  9703. + seq_printf(m, " Length : %08x\n", vc_cma_size);
  9704. + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
  9705. + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
  9706. + seq_printf(m, " Chunks : %4d (%d bytes)\n",
  9707. + (int)vc_cma_chunks,
  9708. + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
  9709. + seq_printf(m, " Used : %4d (%d bytes)\n",
  9710. + (int)vc_cma_chunks_used,
  9711. + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
  9712. + seq_printf(m, " Reserved : %4d (%d bytes)\n",
  9713. + (unsigned int)vc_cma_chunks_reserved,
  9714. + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
  9715. +
  9716. + for (i = 0; i < vc_cma_reserve_count; i++) {
  9717. + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
  9718. + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
  9719. + user->reserve);
  9720. + }
  9721. +
  9722. + seq_printf(m, "\n");
  9723. +
  9724. + return 0;
  9725. +}
  9726. +
  9727. +static int vc_cma_proc_open(struct inode *inode, struct file *file)
  9728. +{
  9729. + return single_open(file, vc_cma_show_info, NULL);
  9730. +}
  9731. +
  9732. +/****************************************************************************
  9733. +*
  9734. +* vc_cma_proc_write
  9735. +*
  9736. +***************************************************************************/
  9737. +
  9738. +static int vc_cma_proc_write(struct file *file,
  9739. + const char __user *buffer,
  9740. + size_t size, loff_t *ppos)
  9741. +{
  9742. + int rc = -EFAULT;
  9743. + char input_str[20];
  9744. +
  9745. + memset(input_str, 0, sizeof(input_str));
  9746. +
  9747. + if (size > sizeof(input_str)) {
  9748. + LOG_ERR("%s: input string length too long", __func__);
  9749. + goto out;
  9750. + }
  9751. +
  9752. + if (copy_from_user(input_str, buffer, size - 1)) {
  9753. + LOG_ERR("%s: failed to get input string", __func__);
  9754. + goto out;
  9755. + }
  9756. +#define ALLOC_STR "alloc"
  9757. +#define FREE_STR "free"
  9758. +#define DEBUG_STR "debug"
  9759. +#define RESERVE_STR "reserve"
  9760. + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
  9761. + int size;
  9762. + char *p = input_str + strlen(ALLOC_STR);
  9763. +
  9764. + while (*p == ' ')
  9765. + p++;
  9766. + size = memparse(p, NULL);
  9767. + LOG_ERR("/proc/vc-cma: alloc %d", size);
  9768. + if (size)
  9769. + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
  9770. + size / VC_CMA_CHUNK_SIZE, 0);
  9771. + else
  9772. + LOG_ERR("invalid size '%s'", p);
  9773. + rc = size;
  9774. + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
  9775. + int size;
  9776. + char *p = input_str + strlen(FREE_STR);
  9777. +
  9778. + while (*p == ' ')
  9779. + p++;
  9780. + size = memparse(p, NULL);
  9781. + LOG_ERR("/proc/vc-cma: free %d", size);
  9782. + if (size)
  9783. + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
  9784. + size / VC_CMA_CHUNK_SIZE, 0);
  9785. + else
  9786. + LOG_ERR("invalid size '%s'", p);
  9787. + rc = size;
  9788. + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
  9789. + char *p = input_str + strlen(DEBUG_STR);
  9790. + while (*p == ' ')
  9791. + p++;
  9792. + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
  9793. + vc_cma_debug = 1;
  9794. + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
  9795. + vc_cma_debug = 0;
  9796. + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
  9797. + rc = size;
  9798. + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
  9799. + int size;
  9800. + int reserved;
  9801. + char *p = input_str + strlen(RESERVE_STR);
  9802. + while (*p == ' ')
  9803. + p++;
  9804. + size = memparse(p, NULL);
  9805. +
  9806. + reserved = vc_cma_set_reserve(size, current->tgid);
  9807. + rc = (reserved >= 0) ? size : reserved;
  9808. + }
  9809. +
  9810. +out:
  9811. + return rc;
  9812. +}
  9813. +
  9814. +/****************************************************************************
  9815. +*
  9816. +* File Operations for /proc interface.
  9817. +*
  9818. +***************************************************************************/
  9819. +
  9820. +static const struct file_operations vc_cma_proc_fops = {
  9821. + .open = vc_cma_proc_open,
  9822. + .read = seq_read,
  9823. + .write = vc_cma_proc_write,
  9824. + .llseek = seq_lseek,
  9825. + .release = single_release
  9826. +};
  9827. +
  9828. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
  9829. +{
  9830. + struct vc_cma_reserve_user *user = NULL;
  9831. + int delta = 0;
  9832. + int i;
  9833. +
  9834. + if (down_interruptible(&vc_cma_reserve_mutex))
  9835. + return -ERESTARTSYS;
  9836. +
  9837. + for (i = 0; i < vc_cma_reserve_count; i++) {
  9838. + if (pid == vc_cma_reserve_users[i].pid) {
  9839. + user = &vc_cma_reserve_users[i];
  9840. + delta = reserve - user->reserve;
  9841. + if (reserve)
  9842. + user->reserve = reserve;
  9843. + else {
  9844. + /* Remove this entry by copying downwards */
  9845. + while ((i + 1) < vc_cma_reserve_count) {
  9846. + user[0].pid = user[1].pid;
  9847. + user[0].reserve = user[1].reserve;
  9848. + user++;
  9849. + i++;
  9850. + }
  9851. + vc_cma_reserve_count--;
  9852. + user = NULL;
  9853. + }
  9854. + break;
  9855. + }
  9856. + }
  9857. +
  9858. + if (reserve && !user) {
  9859. + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
  9860. + LOG_ERR("vc-cma: Too many reservations - "
  9861. + "increase CMA_RESERVE_COUNT_MAX");
  9862. + up(&vc_cma_reserve_mutex);
  9863. + return -EBUSY;
  9864. + }
  9865. + user = &vc_cma_reserve_users[vc_cma_reserve_count];
  9866. + user->pid = pid;
  9867. + user->reserve = reserve;
  9868. + delta = reserve;
  9869. + vc_cma_reserve_count++;
  9870. + }
  9871. +
  9872. + vc_cma_reserve_total += delta;
  9873. +
  9874. + send_vc_msg(VC_CMA_MSG_RESERVE,
  9875. + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
  9876. +
  9877. + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
  9878. +
  9879. + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
  9880. + reserve, pid, vc_cma_reserve_total);
  9881. +
  9882. + up(&vc_cma_reserve_mutex);
  9883. +
  9884. + return vc_cma_reserve_total;
  9885. +}
  9886. +
  9887. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  9888. + VCHIQ_HEADER_T * header,
  9889. + VCHIQ_SERVICE_HANDLE_T service,
  9890. + void *bulk_userdata)
  9891. +{
  9892. + switch (reason) {
  9893. + case VCHIQ_MESSAGE_AVAILABLE:
  9894. + if (!send_worker_msg(header))
  9895. + return VCHIQ_RETRY;
  9896. + break;
  9897. + case VCHIQ_SERVICE_CLOSED:
  9898. + LOG_DBG("CMA service closed");
  9899. + break;
  9900. + default:
  9901. + LOG_ERR("Unexpected CMA callback reason %d", reason);
  9902. + break;
  9903. + }
  9904. + return VCHIQ_SUCCESS;
  9905. +}
  9906. +
  9907. +static void send_vc_msg(unsigned short type,
  9908. + unsigned short param1, unsigned short param2)
  9909. +{
  9910. + unsigned short msg[] = { type, param1, param2 };
  9911. + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
  9912. + VCHIQ_STATUS_T ret;
  9913. + vchiq_use_service(cma_service);
  9914. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9915. + vchiq_release_service(cma_service);
  9916. + if (ret != VCHIQ_SUCCESS)
  9917. + LOG_ERR("vchiq_queue_message returned %x", ret);
  9918. +}
  9919. +
  9920. +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
  9921. +{
  9922. + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
  9923. + return false;
  9924. + vchiu_queue_push(&cma_msg_queue, msg);
  9925. + up(&vc_cma_worker_queue_push_mutex);
  9926. + return true;
  9927. +}
  9928. +
  9929. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
  9930. +{
  9931. + int i;
  9932. + for (i = 0; i < num_chunks; i++) {
  9933. + struct page *chunk;
  9934. + unsigned int chunk_num;
  9935. + uint8_t *chunk_addr;
  9936. + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
  9937. +
  9938. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  9939. + PAGES_PER_CHUNK,
  9940. + VC_CMA_CHUNK_ORDER);
  9941. + if (!chunk)
  9942. + break;
  9943. +
  9944. + chunk_addr = page_address(chunk);
  9945. + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
  9946. + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
  9947. + chunk_size);
  9948. +
  9949. + chunk_num =
  9950. + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
  9951. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  9952. + VC_CMA_CHUNK_SIZE) != 0);
  9953. + if (chunk_num >= vc_cma_chunks) {
  9954. + LOG_ERR("%s: ===============================",
  9955. + __func__);
  9956. + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
  9957. + "bad SPARSEMEM configuration?",
  9958. + __func__, (unsigned int)page_to_phys(chunk),
  9959. + vc_cma_base, vc_cma_base + vc_cma_size - 1);
  9960. + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
  9961. + (void*)0/*vc_cma_device.dev.cma_area*/);
  9962. + LOG_ERR("%s: ===============================",
  9963. + __func__);
  9964. + break;
  9965. + }
  9966. + reply->params[i] = chunk_num;
  9967. + vc_cma_chunks_used++;
  9968. + }
  9969. +
  9970. + if (i < num_chunks) {
  9971. + LOG_ERR("%s: dma_alloc_from_contiguous failed "
  9972. + "for %x bytes (alloc %d of %d, %d free)",
  9973. + __func__, VC_CMA_CHUNK_SIZE, i,
  9974. + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
  9975. + num_chunks = i;
  9976. + }
  9977. +
  9978. + LOG_DBG("CMA allocated %d chunks -> %d used",
  9979. + num_chunks, vc_cma_chunks_used);
  9980. + reply->type = VC_CMA_MSG_ALLOCATED;
  9981. +
  9982. + {
  9983. + VCHIQ_ELEMENT_T elem = {
  9984. + reply,
  9985. + offsetof(struct cma_msg, params[0]) +
  9986. + num_chunks * sizeof(reply->params[0])
  9987. + };
  9988. + VCHIQ_STATUS_T ret;
  9989. + vchiq_use_service(cma_service);
  9990. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9991. + vchiq_release_service(cma_service);
  9992. + if (ret != VCHIQ_SUCCESS)
  9993. + LOG_ERR("vchiq_queue_message return " "%x", ret);
  9994. + }
  9995. +
  9996. + return num_chunks;
  9997. +}
  9998. +
  9999. +static int cma_worker_proc(void *param)
  10000. +{
  10001. + static struct cma_msg reply;
  10002. + (void)param;
  10003. +
  10004. + while (1) {
  10005. + VCHIQ_HEADER_T *msg;
  10006. + static struct cma_msg msg_copy;
  10007. + struct cma_msg *cma_msg = &msg_copy;
  10008. + int type, msg_size;
  10009. +
  10010. + msg = vchiu_queue_pop(&cma_msg_queue);
  10011. + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
  10012. + msg_size = msg->size;
  10013. + memcpy(&msg_copy, msg->data, msg_size);
  10014. + type = cma_msg->type;
  10015. + vchiq_release_message(cma_service, msg);
  10016. + } else {
  10017. + msg_size = 0;
  10018. + type = (int)msg;
  10019. + if (type == VC_CMA_MSG_QUIT)
  10020. + break;
  10021. + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
  10022. + msg = NULL;
  10023. + cma_msg = NULL;
  10024. + } else {
  10025. + BUG();
  10026. + continue;
  10027. + }
  10028. + }
  10029. +
  10030. + switch (type) {
  10031. + case VC_CMA_MSG_ALLOC:{
  10032. + int num_chunks, free_chunks;
  10033. + num_chunks = cma_msg->params[0];
  10034. + free_chunks =
  10035. + vc_cma_chunks - vc_cma_chunks_used;
  10036. + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
  10037. + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
  10038. + LOG_ERR
  10039. + ("CMA_MSG_ALLOC - chunk count (%d) "
  10040. + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
  10041. + num_chunks,
  10042. + VC_CMA_MAX_PARAMS_PER_MSG);
  10043. + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
  10044. + }
  10045. +
  10046. + if (num_chunks > free_chunks) {
  10047. + LOG_ERR
  10048. + ("CMA_MSG_ALLOC - chunk count (%d) "
  10049. + "exceeds free chunks (%d)",
  10050. + num_chunks, free_chunks);
  10051. + num_chunks = free_chunks;
  10052. + }
  10053. +
  10054. + vc_cma_alloc_chunks(num_chunks, &reply);
  10055. + }
  10056. + break;
  10057. +
  10058. + case VC_CMA_MSG_FREE:{
  10059. + int chunk_count =
  10060. + (msg_size -
  10061. + offsetof(struct cma_msg,
  10062. + params)) /
  10063. + sizeof(cma_msg->params[0]);
  10064. + int i;
  10065. + BUG_ON(chunk_count <= 0);
  10066. +
  10067. + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
  10068. + chunk_count, cma_msg->params[0]);
  10069. + for (i = 0; i < chunk_count; i++) {
  10070. + int chunk_num = cma_msg->params[i];
  10071. + struct page *page = vc_cma_base_page +
  10072. + chunk_num * PAGES_PER_CHUNK;
  10073. + if (chunk_num >= vc_cma_chunks) {
  10074. + LOG_ERR
  10075. + ("CMA_MSG_FREE - chunk %d of %d"
  10076. + " (value %x) exceeds maximum "
  10077. + "(%x)", i, chunk_count,
  10078. + chunk_num,
  10079. + vc_cma_chunks - 1);
  10080. + break;
  10081. + }
  10082. +
  10083. + if (!dma_release_from_contiguous
  10084. + (NULL /*&vc_cma_device.dev*/, page,
  10085. + PAGES_PER_CHUNK)) {
  10086. + LOG_ERR
  10087. + ("CMA_MSG_FREE - failed to "
  10088. + "release chunk %d (phys %x, "
  10089. + "page %x)", chunk_num,
  10090. + page_to_phys(page),
  10091. + (unsigned int)page);
  10092. + }
  10093. + vc_cma_chunks_used--;
  10094. + }
  10095. + LOG_DBG("CMA released %d chunks -> %d used",
  10096. + i, vc_cma_chunks_used);
  10097. + }
  10098. + break;
  10099. +
  10100. + case VC_CMA_MSG_UPDATE_RESERVE:{
  10101. + int chunks_needed =
  10102. + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
  10103. + 1)
  10104. + / VC_CMA_CHUNK_SIZE) -
  10105. + vc_cma_chunks_reserved;
  10106. +
  10107. + LOG_DBG
  10108. + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
  10109. + chunks_needed);
  10110. +
  10111. + /* Cap the reservations to what is available */
  10112. + if (chunks_needed > 0) {
  10113. + if (chunks_needed >
  10114. + (vc_cma_chunks -
  10115. + vc_cma_chunks_used))
  10116. + chunks_needed =
  10117. + (vc_cma_chunks -
  10118. + vc_cma_chunks_used);
  10119. +
  10120. + chunks_needed =
  10121. + vc_cma_alloc_chunks(chunks_needed,
  10122. + &reply);
  10123. + }
  10124. +
  10125. + LOG_DBG
  10126. + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
  10127. + chunks_needed);
  10128. + vc_cma_chunks_reserved += chunks_needed;
  10129. + }
  10130. + break;
  10131. +
  10132. + default:
  10133. + LOG_ERR("unexpected msg type %d", type);
  10134. + break;
  10135. + }
  10136. + }
  10137. +
  10138. + LOG_DBG("quitting...");
  10139. + return 0;
  10140. +}
  10141. +
  10142. +/****************************************************************************
  10143. +*
  10144. +* vc_cma_connected_init
  10145. +*
  10146. +* This function is called once the videocore has been connected.
  10147. +*
  10148. +***************************************************************************/
  10149. +
  10150. +static void vc_cma_connected_init(void)
  10151. +{
  10152. + VCHIQ_SERVICE_PARAMS_T service_params;
  10153. +
  10154. + LOG_DBG("vc_cma_connected_init");
  10155. +
  10156. + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
  10157. + LOG_ERR("could not create CMA msg queue");
  10158. + goto fail_queue;
  10159. + }
  10160. +
  10161. + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
  10162. + goto fail_vchiq_init;
  10163. +
  10164. + vchiq_connect(cma_instance);
  10165. +
  10166. + service_params.fourcc = VC_CMA_FOURCC;
  10167. + service_params.callback = cma_service_callback;
  10168. + service_params.userdata = NULL;
  10169. + service_params.version = VC_CMA_VERSION;
  10170. + service_params.version_min = VC_CMA_VERSION;
  10171. +
  10172. + if (vchiq_open_service(cma_instance, &service_params,
  10173. + &cma_service) != VCHIQ_SUCCESS) {
  10174. + LOG_ERR("failed to open service - already in use?");
  10175. + goto fail_vchiq_open;
  10176. + }
  10177. +
  10178. + vchiq_release_service(cma_service);
  10179. +
  10180. + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
  10181. + if (!cma_worker) {
  10182. + LOG_ERR("could not create CMA worker thread");
  10183. + goto fail_worker;
  10184. + }
  10185. + set_user_nice(cma_worker, -20);
  10186. + wake_up_process(cma_worker);
  10187. +
  10188. + return;
  10189. +
  10190. +fail_worker:
  10191. + vchiq_close_service(cma_service);
  10192. +fail_vchiq_open:
  10193. + vchiq_shutdown(cma_instance);
  10194. +fail_vchiq_init:
  10195. + vchiu_queue_delete(&cma_msg_queue);
  10196. +fail_queue:
  10197. + return;
  10198. +}
  10199. +
  10200. +void
  10201. +loud_error_header(void)
  10202. +{
  10203. + if (in_loud_error)
  10204. + return;
  10205. +
  10206. + LOG_ERR("============================================================"
  10207. + "================");
  10208. + LOG_ERR("============================================================"
  10209. + "================");
  10210. + LOG_ERR("=====");
  10211. +
  10212. + in_loud_error = 1;
  10213. +}
  10214. +
  10215. +void
  10216. +loud_error_footer(void)
  10217. +{
  10218. + if (!in_loud_error)
  10219. + return;
  10220. +
  10221. + LOG_ERR("=====");
  10222. + LOG_ERR("============================================================"
  10223. + "================");
  10224. + LOG_ERR("============================================================"
  10225. + "================");
  10226. +
  10227. + in_loud_error = 0;
  10228. +}
  10229. +
  10230. +#if 1
  10231. +static int check_cma_config(void) { return 1; }
  10232. +#else
  10233. +static int
  10234. +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
  10235. + const char *symbol,
  10236. + void *buf, size_t bufsize)
  10237. +{
  10238. + VC_MEM_ADDR_T vcMemAddr;
  10239. + size_t vcMemSize;
  10240. + uint8_t *mapAddr;
  10241. + off_t vcMapAddr;
  10242. +
  10243. + if (!LookupVideoCoreSymbol(handle, symbol,
  10244. + &vcMemAddr,
  10245. + &vcMemSize)) {
  10246. + loud_error_header();
  10247. + loud_error(
  10248. + "failed to find VC symbol \"%s\".",
  10249. + symbol);
  10250. + loud_error_footer();
  10251. + return 0;
  10252. + }
  10253. +
  10254. + if (vcMemSize != bufsize) {
  10255. + loud_error_header();
  10256. + loud_error(
  10257. + "VC symbol \"%s\" is the wrong size.",
  10258. + symbol);
  10259. + loud_error_footer();
  10260. + return 0;
  10261. + }
  10262. +
  10263. + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
  10264. + vcMapAddr += mm_vc_mem_phys_addr;
  10265. + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
  10266. + if (mapAddr == 0) {
  10267. + loud_error_header();
  10268. + loud_error(
  10269. + "failed to ioremap \"%s\" @ 0x%x "
  10270. + "(phys: 0x%x, size: %u).",
  10271. + symbol,
  10272. + (unsigned int)vcMapAddr,
  10273. + (unsigned int)vcMemAddr,
  10274. + (unsigned int)vcMemSize);
  10275. + loud_error_footer();
  10276. + return 0;
  10277. + }
  10278. +
  10279. + memcpy(buf, mapAddr, bufsize);
  10280. + iounmap(mapAddr);
  10281. +
  10282. + return 1;
  10283. +}
  10284. +
  10285. +
  10286. +static int
  10287. +check_cma_config(void)
  10288. +{
  10289. + VC_MEM_ACCESS_HANDLE_T mem_hndl;
  10290. + VC_MEM_ADDR_T mempool_start;
  10291. + VC_MEM_ADDR_T mempool_end;
  10292. + VC_MEM_ADDR_T mempool_offline_start;
  10293. + VC_MEM_ADDR_T mempool_offline_end;
  10294. + VC_MEM_ADDR_T cam_alloc_base;
  10295. + VC_MEM_ADDR_T cam_alloc_size;
  10296. + VC_MEM_ADDR_T cam_alloc_end;
  10297. + int success = 0;
  10298. +
  10299. + if (OpenVideoCoreMemory(&mem_hndl) != 0)
  10300. + goto out;
  10301. +
  10302. + /* Read the relevant VideoCore variables */
  10303. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
  10304. + &mempool_start,
  10305. + sizeof(mempool_start)))
  10306. + goto close;
  10307. +
  10308. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
  10309. + &mempool_end,
  10310. + sizeof(mempool_end)))
  10311. + goto close;
  10312. +
  10313. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
  10314. + &mempool_offline_start,
  10315. + sizeof(mempool_offline_start)))
  10316. + goto close;
  10317. +
  10318. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
  10319. + &mempool_offline_end,
  10320. + sizeof(mempool_offline_end)))
  10321. + goto close;
  10322. +
  10323. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
  10324. + &cam_alloc_base,
  10325. + sizeof(cam_alloc_base)))
  10326. + goto close;
  10327. +
  10328. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
  10329. + &cam_alloc_size,
  10330. + sizeof(cam_alloc_size)))
  10331. + goto close;
  10332. +
  10333. + cam_alloc_end = cam_alloc_base + cam_alloc_size;
  10334. +
  10335. + success = 1;
  10336. +
  10337. + /* Now the sanity checks */
  10338. + if (!mempool_offline_start)
  10339. + mempool_offline_start = mempool_start;
  10340. + if (!mempool_offline_end)
  10341. + mempool_offline_end = mempool_end;
  10342. +
  10343. + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
  10344. + loud_error_header();
  10345. + loud_error(
  10346. + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
  10347. + "vc_cma_base(%x)",
  10348. + mempool_offline_start,
  10349. + VCADDR_TO_PHYSADDR(mempool_offline_start),
  10350. + vc_cma_base);
  10351. + success = 0;
  10352. + }
  10353. +
  10354. + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
  10355. + (vc_cma_base + vc_cma_size)) {
  10356. + loud_error_header();
  10357. + loud_error(
  10358. + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
  10359. + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
  10360. + mempool_offline_start,
  10361. + VCADDR_TO_PHYSADDR(mempool_offline_end),
  10362. + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
  10363. + success = 0;
  10364. + }
  10365. +
  10366. + if (mempool_end < mempool_start) {
  10367. + loud_error_header();
  10368. + loud_error(
  10369. + "__MEMPOOL_END(%x) must not be before "
  10370. + "__MEMPOOL_START(%x)",
  10371. + mempool_end,
  10372. + mempool_start);
  10373. + success = 0;
  10374. + }
  10375. +
  10376. + if (mempool_offline_end < mempool_offline_start) {
  10377. + loud_error_header();
  10378. + loud_error(
  10379. + "__MEMPOOL_OFFLINE_END(%x) must not be before "
  10380. + "__MEMPOOL_OFFLINE_START(%x)",
  10381. + mempool_offline_end,
  10382. + mempool_offline_start);
  10383. + success = 0;
  10384. + }
  10385. +
  10386. + if (mempool_offline_start < mempool_start) {
  10387. + loud_error_header();
  10388. + loud_error(
  10389. + "__MEMPOOL_OFFLINE_START(%x) must not be before "
  10390. + "__MEMPOOL_START(%x)",
  10391. + mempool_offline_start,
  10392. + mempool_start);
  10393. + success = 0;
  10394. + }
  10395. +
  10396. + if (mempool_offline_end > mempool_end) {
  10397. + loud_error_header();
  10398. + loud_error(
  10399. + "__MEMPOOL_OFFLINE_END(%x) must not be after "
  10400. + "__MEMPOOL_END(%x)",
  10401. + mempool_offline_end,
  10402. + mempool_end);
  10403. + success = 0;
  10404. + }
  10405. +
  10406. + if ((cam_alloc_base < mempool_end) &&
  10407. + (cam_alloc_end > mempool_start)) {
  10408. + loud_error_header();
  10409. + loud_error(
  10410. + "cam_alloc pool(%x-%x) overlaps "
  10411. + "mempool(%x-%x)",
  10412. + cam_alloc_base, cam_alloc_end,
  10413. + mempool_start, mempool_end);
  10414. + success = 0;
  10415. + }
  10416. +
  10417. + loud_error_footer();
  10418. +
  10419. +close:
  10420. + CloseVideoCoreMemory(mem_hndl);
  10421. +
  10422. +out:
  10423. + return success;
  10424. +}
  10425. +#endif
  10426. +
  10427. +static int vc_cma_init(void)
  10428. +{
  10429. + int rc = -EFAULT;
  10430. + struct device *dev;
  10431. +
  10432. + if (!check_cma_config())
  10433. + goto out_release;
  10434. +
  10435. + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
  10436. + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
  10437. + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
  10438. + vc_cma_size, vc_cma_size / (1024 * 1024));
  10439. + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
  10440. + vc_cma_initial, vc_cma_initial / (1024 * 1024));
  10441. +
  10442. + vc_cma_base_page = phys_to_page(vc_cma_base);
  10443. +
  10444. + if (vc_cma_chunks) {
  10445. + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
  10446. +
  10447. + for (vc_cma_chunks_used = 0;
  10448. + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
  10449. + struct page *chunk;
  10450. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  10451. + PAGES_PER_CHUNK,
  10452. + VC_CMA_CHUNK_ORDER);
  10453. + if (!chunk)
  10454. + break;
  10455. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  10456. + VC_CMA_CHUNK_SIZE) != 0);
  10457. + }
  10458. + if (vc_cma_chunks_used != chunks_needed) {
  10459. + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
  10460. + "bytes, allocation %d of %d)",
  10461. + __func__, VC_CMA_CHUNK_SIZE,
  10462. + vc_cma_chunks_used, chunks_needed);
  10463. + goto out_release;
  10464. + }
  10465. +
  10466. + vchiq_add_connected_callback(vc_cma_connected_init);
  10467. + }
  10468. +
  10469. + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
  10470. + if (rc < 0) {
  10471. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  10472. + goto out_release;
  10473. + }
  10474. +
  10475. + cdev_init(&vc_cma_cdev, &vc_cma_fops);
  10476. + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
  10477. + if (rc != 0) {
  10478. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  10479. + goto out_unregister;
  10480. + }
  10481. +
  10482. + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
  10483. + if (IS_ERR(vc_cma_class)) {
  10484. + rc = PTR_ERR(vc_cma_class);
  10485. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  10486. + goto out_cdev_del;
  10487. + }
  10488. +
  10489. + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
  10490. + DRIVER_NAME);
  10491. + if (IS_ERR(dev)) {
  10492. + rc = PTR_ERR(dev);
  10493. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  10494. + goto out_class_destroy;
  10495. + }
  10496. +
  10497. + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
  10498. + if (vc_cma_proc_entry == NULL) {
  10499. + rc = -EFAULT;
  10500. + LOG_ERR("%s: proc_create failed", __func__);
  10501. + goto out_device_destroy;
  10502. + }
  10503. +
  10504. + vc_cma_inited = 1;
  10505. + return 0;
  10506. +
  10507. +out_device_destroy:
  10508. + device_destroy(vc_cma_class, vc_cma_devnum);
  10509. +
  10510. +out_class_destroy:
  10511. + class_destroy(vc_cma_class);
  10512. + vc_cma_class = NULL;
  10513. +
  10514. +out_cdev_del:
  10515. + cdev_del(&vc_cma_cdev);
  10516. +
  10517. +out_unregister:
  10518. + unregister_chrdev_region(vc_cma_devnum, 1);
  10519. +
  10520. +out_release:
  10521. + /* It is tempting to try to clean up by calling
  10522. + dma_release_from_contiguous for all allocated chunks, but it isn't
  10523. + a very safe thing to do. If vc_cma_initial is non-zero it is because
  10524. + VideoCore is already using that memory, so giving it back to Linux
  10525. + is likely to be fatal.
  10526. + */
  10527. + return -1;
  10528. +}
  10529. +
  10530. +/****************************************************************************
  10531. +*
  10532. +* vc_cma_exit
  10533. +*
  10534. +***************************************************************************/
  10535. +
  10536. +static void __exit vc_cma_exit(void)
  10537. +{
  10538. + LOG_DBG("%s: called", __func__);
  10539. +
  10540. + if (vc_cma_inited) {
  10541. + remove_proc_entry(DRIVER_NAME, NULL);
  10542. + device_destroy(vc_cma_class, vc_cma_devnum);
  10543. + class_destroy(vc_cma_class);
  10544. + cdev_del(&vc_cma_cdev);
  10545. + unregister_chrdev_region(vc_cma_devnum, 1);
  10546. + }
  10547. +}
  10548. +
  10549. +module_init(vc_cma_init);
  10550. +module_exit(vc_cma_exit);
  10551. +MODULE_LICENSE("GPL");
  10552. +MODULE_AUTHOR("Broadcom Corporation");
  10553. diff -Nur linux-3.17.5/drivers/char/broadcom/vc_sm/Makefile linux-rpi/drivers/char/broadcom/vc_sm/Makefile
  10554. --- linux-3.17.5/drivers/char/broadcom/vc_sm/Makefile 1969-12-31 18:00:00.000000000 -0600
  10555. +++ linux-rpi/drivers/char/broadcom/vc_sm/Makefile 2014-12-11 14:02:52.800418001 -0600
  10556. @@ -0,0 +1,21 @@
  10557. +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs -O2
  10558. +
  10559. +EXTRA_CFLAGS += -I"./arch/arm/mach-bcm2708/include/mach"
  10560. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
  10561. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
  10562. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
  10563. +EXTRA_CFLAGS += -I"$(srctree)/fs/"
  10564. +
  10565. +EXTRA_CFLAGS += -DOS_ASSERT_FAILURE
  10566. +EXTRA_CFLAGS += -D__STDC_VERSION=199901L
  10567. +EXTRA_CFLAGS += -D__STDC_VERSION__=199901L
  10568. +EXTRA_CFLAGS += -D__VCCOREVER__=0
  10569. +EXTRA_CFLAGS += -D__KERNEL__
  10570. +EXTRA_CFLAGS += -D__linux__
  10571. +EXTRA_CFLAGS += -Werror
  10572. +
  10573. +obj-$(CONFIG_BCM_VC_SM) := vc-sm.o
  10574. +
  10575. +vc-sm-objs := \
  10576. + vmcs_sm.o \
  10577. + vc_vchi_sm.o
  10578. diff -Nur linux-3.17.5/drivers/char/broadcom/vc_sm/vc_vchi_sm.c linux-rpi/drivers/char/broadcom/vc_sm/vc_vchi_sm.c
  10579. --- linux-3.17.5/drivers/char/broadcom/vc_sm/vc_vchi_sm.c 1969-12-31 18:00:00.000000000 -0600
  10580. +++ linux-rpi/drivers/char/broadcom/vc_sm/vc_vchi_sm.c 2014-12-11 14:02:52.800418001 -0600
  10581. @@ -0,0 +1,492 @@
  10582. +/*****************************************************************************
  10583. +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
  10584. +*
  10585. +* Unless you and Broadcom execute a separate written software license
  10586. +* agreement governing use of this software, this software is licensed to you
  10587. +* under the terms of the GNU General Public License version 2, available at
  10588. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  10589. +*
  10590. +* Notwithstanding the above, under no circumstances may you combine this
  10591. +* software in any way with any other Broadcom software provided under a
  10592. +* license other than the GPL, without Broadcom's express prior written
  10593. +* consent.
  10594. +*****************************************************************************/
  10595. +
  10596. +/* ---- Include Files ----------------------------------------------------- */
  10597. +#include <linux/types.h>
  10598. +#include <linux/kernel.h>
  10599. +#include <linux/list.h>
  10600. +#include <linux/semaphore.h>
  10601. +#include <linux/mutex.h>
  10602. +#include <linux/slab.h>
  10603. +#include <linux/kthread.h>
  10604. +
  10605. +#include "vc_vchi_sm.h"
  10606. +
  10607. +#define VC_SM_VER 1
  10608. +#define VC_SM_MIN_VER 0
  10609. +
  10610. +/* ---- Private Constants and Types -------------------------------------- */
  10611. +
  10612. +/* Command blocks come from a pool */
  10613. +#define SM_MAX_NUM_CMD_RSP_BLKS 32
  10614. +
  10615. +struct sm_cmd_rsp_blk {
  10616. + struct list_head head; /* To create lists */
  10617. + struct semaphore sema; /* To be signaled when the response is there */
  10618. +
  10619. + uint16_t id;
  10620. + uint16_t length;
  10621. +
  10622. + uint8_t msg[VC_SM_MAX_MSG_LEN];
  10623. +
  10624. + uint32_t wait:1;
  10625. + uint32_t sent:1;
  10626. + uint32_t alloc:1;
  10627. +
  10628. +};
  10629. +
  10630. +struct sm_instance {
  10631. + uint32_t num_connections;
  10632. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  10633. + struct task_struct *io_thread;
  10634. + struct semaphore io_sema;
  10635. +
  10636. + uint32_t trans_id;
  10637. +
  10638. + struct mutex lock;
  10639. + struct list_head cmd_list;
  10640. + struct list_head rsp_list;
  10641. + struct list_head dead_list;
  10642. +
  10643. + struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
  10644. + struct list_head free_list;
  10645. + struct mutex free_lock;
  10646. + struct semaphore free_sema;
  10647. +
  10648. +};
  10649. +
  10650. +/* ---- Private Variables ------------------------------------------------ */
  10651. +
  10652. +/* ---- Private Function Prototypes -------------------------------------- */
  10653. +
  10654. +/* ---- Private Functions ------------------------------------------------ */
  10655. +static struct
  10656. +sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
  10657. + VC_SM_MSG_TYPE id, void *msg,
  10658. + uint32_t size, int wait)
  10659. +{
  10660. + struct sm_cmd_rsp_blk *blk;
  10661. + VC_SM_MSG_HDR_T *hdr;
  10662. +
  10663. + if (down_interruptible(&instance->free_sema)) {
  10664. + blk = kmalloc(sizeof(*blk), GFP_KERNEL);
  10665. + if (!blk)
  10666. + return NULL;
  10667. +
  10668. + blk->alloc = 1;
  10669. + sema_init(&blk->sema, 0);
  10670. + } else {
  10671. + mutex_lock(&instance->free_lock);
  10672. + blk =
  10673. + list_first_entry(&instance->free_list,
  10674. + struct sm_cmd_rsp_blk, head);
  10675. + list_del(&blk->head);
  10676. + mutex_unlock(&instance->free_lock);
  10677. + }
  10678. +
  10679. + blk->sent = 0;
  10680. + blk->wait = wait;
  10681. + blk->length = sizeof(*hdr) + size;
  10682. +
  10683. + hdr = (VC_SM_MSG_HDR_T *) blk->msg;
  10684. + hdr->type = id;
  10685. + mutex_lock(&instance->lock);
  10686. + hdr->trans_id = blk->id = ++instance->trans_id;
  10687. + mutex_unlock(&instance->lock);
  10688. +
  10689. + if (size)
  10690. + memcpy(hdr->body, msg, size);
  10691. +
  10692. + return blk;
  10693. +}
  10694. +
  10695. +static void
  10696. +vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
  10697. +{
  10698. + if (blk->alloc) {
  10699. + kfree(blk);
  10700. + return;
  10701. + }
  10702. +
  10703. + mutex_lock(&instance->free_lock);
  10704. + list_add(&blk->head, &instance->free_list);
  10705. + mutex_unlock(&instance->free_lock);
  10706. + up(&instance->free_sema);
  10707. +}
  10708. +
  10709. +static int vc_vchi_sm_videocore_io(void *arg)
  10710. +{
  10711. + struct sm_instance *instance = arg;
  10712. + struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
  10713. + VC_SM_RESULT_T *reply;
  10714. + uint32_t reply_len;
  10715. + int32_t status;
  10716. + int svc_use = 1;
  10717. +
  10718. + while (1) {
  10719. + if (svc_use)
  10720. + vchi_service_release(instance->vchi_handle[0]);
  10721. + svc_use = 0;
  10722. + if (!down_interruptible(&instance->io_sema)) {
  10723. + vchi_service_use(instance->vchi_handle[0]);
  10724. + svc_use = 1;
  10725. +
  10726. + do {
  10727. + unsigned int flags;
  10728. + /*
  10729. + * Get new command and move it to response list
  10730. + */
  10731. + mutex_lock(&instance->lock);
  10732. + if (list_empty(&instance->cmd_list)) {
  10733. + /* no more commands to process */
  10734. + mutex_unlock(&instance->lock);
  10735. + break;
  10736. + }
  10737. + cmd =
  10738. + list_first_entry(&instance->cmd_list,
  10739. + struct sm_cmd_rsp_blk,
  10740. + head);
  10741. + list_move(&cmd->head, &instance->rsp_list);
  10742. + cmd->sent = 1;
  10743. + mutex_unlock(&instance->lock);
  10744. +
  10745. + /* Send the command */
  10746. + flags = VCHI_FLAGS_BLOCK_UNTIL_QUEUED;
  10747. + status = vchi_msg_queue(
  10748. + instance->vchi_handle[0],
  10749. + cmd->msg, cmd->length,
  10750. + flags, NULL);
  10751. + if (status) {
  10752. + pr_err("%s: failed to queue message (%d)",
  10753. + __func__, status);
  10754. + }
  10755. +
  10756. + /* If no reply is needed then we're done */
  10757. + if (!cmd->wait) {
  10758. + mutex_lock(&instance->lock);
  10759. + list_del(&cmd->head);
  10760. + mutex_unlock(&instance->lock);
  10761. + vc_vchi_cmd_delete(instance, cmd);
  10762. + continue;
  10763. + }
  10764. +
  10765. + if (status) {
  10766. + up(&cmd->sema);
  10767. + continue;
  10768. + }
  10769. +
  10770. + } while (1);
  10771. +
  10772. + while (!vchi_msg_peek
  10773. + (instance->vchi_handle[0], (void **)&reply,
  10774. + &reply_len, VCHI_FLAGS_NONE)) {
  10775. + mutex_lock(&instance->lock);
  10776. + list_for_each_entry(cmd, &instance->rsp_list,
  10777. + head) {
  10778. + if (cmd->id == reply->trans_id)
  10779. + break;
  10780. + }
  10781. + mutex_unlock(&instance->lock);
  10782. +
  10783. + if (&cmd->head == &instance->rsp_list) {
  10784. + pr_debug("%s: received response %u, throw away...",
  10785. + __func__, reply->trans_id);
  10786. + } else if (reply_len > sizeof(cmd->msg)) {
  10787. + pr_err("%s: reply too big (%u) %u, throw away...",
  10788. + __func__, reply_len,
  10789. + reply->trans_id);
  10790. + } else {
  10791. + memcpy(cmd->msg, reply, reply_len);
  10792. + up(&cmd->sema);
  10793. + }
  10794. +
  10795. + vchi_msg_remove(instance->vchi_handle[0]);
  10796. + }
  10797. +
  10798. + /* Go through the dead list and free them */
  10799. + mutex_lock(&instance->lock);
  10800. + list_for_each_entry_safe(cmd, cmd_tmp,
  10801. + &instance->dead_list, head) {
  10802. + list_del(&cmd->head);
  10803. + vc_vchi_cmd_delete(instance, cmd);
  10804. + }
  10805. + mutex_unlock(&instance->lock);
  10806. + }
  10807. + }
  10808. +
  10809. + return 0;
  10810. +}
  10811. +
  10812. +static void vc_sm_vchi_callback(void *param,
  10813. + const VCHI_CALLBACK_REASON_T reason,
  10814. + void *msg_handle)
  10815. +{
  10816. + struct sm_instance *instance = param;
  10817. +
  10818. + (void)msg_handle;
  10819. +
  10820. + switch (reason) {
  10821. + case VCHI_CALLBACK_MSG_AVAILABLE:
  10822. + up(&instance->io_sema);
  10823. + break;
  10824. +
  10825. + case VCHI_CALLBACK_SERVICE_CLOSED:
  10826. + pr_info("%s: service CLOSED!!", __func__);
  10827. + default:
  10828. + break;
  10829. + }
  10830. +}
  10831. +
  10832. +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
  10833. + VCHI_CONNECTION_T **vchi_connections,
  10834. + uint32_t num_connections)
  10835. +{
  10836. + uint32_t i;
  10837. + struct sm_instance *instance;
  10838. + int status;
  10839. +
  10840. + pr_debug("%s: start", __func__);
  10841. +
  10842. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  10843. + pr_err("%s: unsupported number of connections %u (max=%u)",
  10844. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  10845. +
  10846. + goto err_null;
  10847. + }
  10848. + /* Allocate memory for this instance */
  10849. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  10850. +
  10851. + /* Misc initialisations */
  10852. + mutex_init(&instance->lock);
  10853. + sema_init(&instance->io_sema, 0);
  10854. + INIT_LIST_HEAD(&instance->cmd_list);
  10855. + INIT_LIST_HEAD(&instance->rsp_list);
  10856. + INIT_LIST_HEAD(&instance->dead_list);
  10857. + INIT_LIST_HEAD(&instance->free_list);
  10858. + sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
  10859. + mutex_init(&instance->free_lock);
  10860. + for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
  10861. + sema_init(&instance->free_blk[i].sema, 0);
  10862. + list_add(&instance->free_blk[i].head, &instance->free_list);
  10863. + }
  10864. +
  10865. + /* Open the VCHI service connections */
  10866. + instance->num_connections = num_connections;
  10867. + for (i = 0; i < num_connections; i++) {
  10868. + SERVICE_CREATION_T params = {
  10869. + VCHI_VERSION_EX(VC_SM_VER, VC_SM_MIN_VER),
  10870. + VC_SM_SERVER_NAME,
  10871. + vchi_connections[i],
  10872. + 0,
  10873. + 0,
  10874. + vc_sm_vchi_callback,
  10875. + instance,
  10876. + 0,
  10877. + 0,
  10878. + 0,
  10879. + };
  10880. +
  10881. + status = vchi_service_open(vchi_instance,
  10882. + &params, &instance->vchi_handle[i]);
  10883. + if (status) {
  10884. + pr_err("%s: failed to open VCHI service (%d)",
  10885. + __func__, status);
  10886. +
  10887. + goto err_close_services;
  10888. + }
  10889. + }
  10890. +
  10891. + /* Create the thread which takes care of all io to/from videoocore. */
  10892. + instance->io_thread = kthread_create(&vc_vchi_sm_videocore_io,
  10893. + (void *)instance, "SMIO");
  10894. + if (instance->io_thread == NULL) {
  10895. + pr_err("%s: failed to create SMIO thread", __func__);
  10896. +
  10897. + goto err_close_services;
  10898. + }
  10899. + set_user_nice(instance->io_thread, -10);
  10900. + wake_up_process(instance->io_thread);
  10901. +
  10902. + pr_debug("%s: success - instance 0x%x", __func__, (unsigned)instance);
  10903. + return instance;
  10904. +
  10905. +err_close_services:
  10906. + for (i = 0; i < instance->num_connections; i++) {
  10907. + if (instance->vchi_handle[i] != NULL)
  10908. + vchi_service_close(instance->vchi_handle[i]);
  10909. + }
  10910. + kfree(instance);
  10911. +err_null:
  10912. + pr_debug("%s: FAILED", __func__);
  10913. + return NULL;
  10914. +}
  10915. +
  10916. +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle)
  10917. +{
  10918. + struct sm_instance *instance;
  10919. + uint32_t i;
  10920. +
  10921. + if (handle == NULL) {
  10922. + pr_err("%s: invalid pointer to handle %p", __func__, handle);
  10923. + goto lock;
  10924. + }
  10925. +
  10926. + if (*handle == NULL) {
  10927. + pr_err("%s: invalid handle %p", __func__, *handle);
  10928. + goto lock;
  10929. + }
  10930. +
  10931. + instance = *handle;
  10932. +
  10933. + /* Close all VCHI service connections */
  10934. + for (i = 0; i < instance->num_connections; i++) {
  10935. + int32_t success;
  10936. + vchi_service_use(instance->vchi_handle[i]);
  10937. +
  10938. + success = vchi_service_close(instance->vchi_handle[i]);
  10939. + }
  10940. +
  10941. + kfree(instance);
  10942. +
  10943. + *handle = NULL;
  10944. + return 0;
  10945. +
  10946. +lock:
  10947. + return -EINVAL;
  10948. +}
  10949. +
  10950. +int vc_vchi_sm_send_msg(VC_VCHI_SM_HANDLE_T handle,
  10951. + VC_SM_MSG_TYPE msg_id,
  10952. + void *msg, uint32_t msg_size,
  10953. + void *result, uint32_t result_size,
  10954. + uint32_t *cur_trans_id, uint8_t wait_reply)
  10955. +{
  10956. + int status = 0;
  10957. + struct sm_instance *instance = handle;
  10958. + struct sm_cmd_rsp_blk *cmd_blk;
  10959. +
  10960. + if (handle == NULL) {
  10961. + pr_err("%s: invalid handle", __func__);
  10962. + return -EINVAL;
  10963. + }
  10964. + if (msg == NULL) {
  10965. + pr_err("%s: invalid msg pointer", __func__);
  10966. + return -EINVAL;
  10967. + }
  10968. +
  10969. + cmd_blk =
  10970. + vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
  10971. + if (cmd_blk == NULL) {
  10972. + pr_err("[%s]: failed to allocate global tracking resource",
  10973. + __func__);
  10974. + return -ENOMEM;
  10975. + }
  10976. +
  10977. + if (cur_trans_id != NULL)
  10978. + *cur_trans_id = cmd_blk->id;
  10979. +
  10980. + mutex_lock(&instance->lock);
  10981. + list_add_tail(&cmd_blk->head, &instance->cmd_list);
  10982. + mutex_unlock(&instance->lock);
  10983. + up(&instance->io_sema);
  10984. +
  10985. + if (!wait_reply)
  10986. + /* We're done */
  10987. + return 0;
  10988. +
  10989. + /* Wait for the response */
  10990. + if (down_interruptible(&cmd_blk->sema)) {
  10991. + mutex_lock(&instance->lock);
  10992. + if (!cmd_blk->sent) {
  10993. + list_del(&cmd_blk->head);
  10994. + mutex_unlock(&instance->lock);
  10995. + vc_vchi_cmd_delete(instance, cmd_blk);
  10996. + return -ENXIO;
  10997. + }
  10998. + mutex_unlock(&instance->lock);
  10999. +
  11000. + mutex_lock(&instance->lock);
  11001. + list_move(&cmd_blk->head, &instance->dead_list);
  11002. + mutex_unlock(&instance->lock);
  11003. + up(&instance->io_sema);
  11004. + return -EINTR; /* We're done */
  11005. + }
  11006. +
  11007. + if (result && result_size) {
  11008. + memcpy(result, cmd_blk->msg, result_size);
  11009. + } else {
  11010. + VC_SM_RESULT_T *res = (VC_SM_RESULT_T *) cmd_blk->msg;
  11011. + status = (res->success == 0) ? 0 : -ENXIO;
  11012. + }
  11013. +
  11014. + mutex_lock(&instance->lock);
  11015. + list_del(&cmd_blk->head);
  11016. + mutex_unlock(&instance->lock);
  11017. + vc_vchi_cmd_delete(instance, cmd_blk);
  11018. + return status;
  11019. +}
  11020. +
  11021. +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle, VC_SM_ALLOC_T *msg,
  11022. + VC_SM_ALLOC_RESULT_T *result, uint32_t *cur_trans_id)
  11023. +{
  11024. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ALLOC,
  11025. + msg, sizeof(*msg), result, sizeof(*result),
  11026. + cur_trans_id, 1);
  11027. +}
  11028. +
  11029. +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
  11030. + VC_SM_FREE_T *msg, uint32_t *cur_trans_id)
  11031. +{
  11032. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_FREE,
  11033. + msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
  11034. +}
  11035. +
  11036. +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
  11037. + VC_SM_LOCK_UNLOCK_T *msg,
  11038. + VC_SM_LOCK_RESULT_T *result, uint32_t *cur_trans_id)
  11039. +{
  11040. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_LOCK,
  11041. + msg, sizeof(*msg), result, sizeof(*result),
  11042. + cur_trans_id, 1);
  11043. +}
  11044. +
  11045. +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
  11046. + VC_SM_LOCK_UNLOCK_T *msg,
  11047. + uint32_t *cur_trans_id, uint8_t wait_reply)
  11048. +{
  11049. + return vc_vchi_sm_send_msg(handle, wait_reply ?
  11050. + VC_SM_MSG_TYPE_UNLOCK :
  11051. + VC_SM_MSG_TYPE_UNLOCK_NOANS, msg,
  11052. + sizeof(*msg), 0, 0, cur_trans_id,
  11053. + wait_reply);
  11054. +}
  11055. +
  11056. +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle, VC_SM_RESIZE_T *msg,
  11057. + uint32_t *cur_trans_id)
  11058. +{
  11059. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_RESIZE,
  11060. + msg, sizeof(*msg), 0, 0, cur_trans_id, 1);
  11061. +}
  11062. +
  11063. +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle)
  11064. +{
  11065. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_WALK_ALLOC,
  11066. + 0, 0, 0, 0, 0, 0);
  11067. +}
  11068. +
  11069. +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle, VC_SM_ACTION_CLEAN_T *msg)
  11070. +{
  11071. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ACTION_CLEAN,
  11072. + msg, sizeof(*msg), 0, 0, 0, 0);
  11073. +}
  11074. diff -Nur linux-3.17.5/drivers/char/broadcom/vc_sm/vmcs_sm.c linux-rpi/drivers/char/broadcom/vc_sm/vmcs_sm.c
  11075. --- linux-3.17.5/drivers/char/broadcom/vc_sm/vmcs_sm.c 1969-12-31 18:00:00.000000000 -0600
  11076. +++ linux-rpi/drivers/char/broadcom/vc_sm/vmcs_sm.c 2014-12-11 14:02:52.800418001 -0600
  11077. @@ -0,0 +1,3163 @@
  11078. +/*****************************************************************************
  11079. +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
  11080. +*
  11081. +* Unless you and Broadcom execute a separate written software license
  11082. +* agreement governing use of this software, this software is licensed to you
  11083. +* under the terms of the GNU General Public License version 2, available at
  11084. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  11085. +*
  11086. +* Notwithstanding the above, under no circumstances may you combine this
  11087. +* software in any way with any other Broadcom software provided under a
  11088. +* license other than the GPL, without Broadcom's express prior written
  11089. +* consent.
  11090. +*****************************************************************************/
  11091. +
  11092. +/* ---- Include Files ----------------------------------------------------- */
  11093. +
  11094. +#include <linux/cdev.h>
  11095. +#include <linux/device.h>
  11096. +#include <linux/debugfs.h>
  11097. +#include <linux/dma-mapping.h>
  11098. +#include <linux/errno.h>
  11099. +#include <linux/fs.h>
  11100. +#include <linux/hugetlb.h>
  11101. +#include <linux/ioctl.h>
  11102. +#include <linux/kernel.h>
  11103. +#include <linux/list.h>
  11104. +#include <linux/module.h>
  11105. +#include <linux/mm.h>
  11106. +#include <linux/pfn.h>
  11107. +#include <linux/proc_fs.h>
  11108. +#include <linux/pagemap.h>
  11109. +#include <linux/semaphore.h>
  11110. +#include <linux/slab.h>
  11111. +#include <linux/seq_file.h>
  11112. +#include <linux/types.h>
  11113. +#include <asm/cacheflush.h>
  11114. +
  11115. +#include <vc_mem.h>
  11116. +
  11117. +#include "vchiq_connected.h"
  11118. +#include "vc_vchi_sm.h"
  11119. +
  11120. +#include <vmcs_sm_ioctl.h>
  11121. +#include "vc_sm_knl.h"
  11122. +
  11123. +/* ---- Private Constants and Types --------------------------------------- */
  11124. +
  11125. +#define DEVICE_NAME "vcsm"
  11126. +#define DEVICE_MINOR 0
  11127. +
  11128. +#define VC_SM_DIR_ROOT_NAME "vc-smem"
  11129. +#define VC_SM_DIR_ALLOC_NAME "alloc"
  11130. +#define VC_SM_STATE "state"
  11131. +#define VC_SM_STATS "statistics"
  11132. +#define VC_SM_RESOURCES "resources"
  11133. +#define VC_SM_DEBUG "debug"
  11134. +#define VC_SM_WRITE_BUF_SIZE 128
  11135. +
  11136. +/* Statistics tracked per resource and globally.
  11137. +*/
  11138. +enum SM_STATS_T {
  11139. + /* Attempt. */
  11140. + ALLOC,
  11141. + FREE,
  11142. + LOCK,
  11143. + UNLOCK,
  11144. + MAP,
  11145. + FLUSH,
  11146. + INVALID,
  11147. +
  11148. + END_ATTEMPT,
  11149. +
  11150. + /* Failure. */
  11151. + ALLOC_FAIL,
  11152. + FREE_FAIL,
  11153. + LOCK_FAIL,
  11154. + UNLOCK_FAIL,
  11155. + MAP_FAIL,
  11156. + FLUSH_FAIL,
  11157. + INVALID_FAIL,
  11158. +
  11159. + END_ALL,
  11160. +
  11161. +};
  11162. +
  11163. +static const char *const sm_stats_human_read[] = {
  11164. + "Alloc",
  11165. + "Free",
  11166. + "Lock",
  11167. + "Unlock",
  11168. + "Map",
  11169. + "Cache Flush",
  11170. + "Cache Invalidate",
  11171. +};
  11172. +
  11173. +typedef int (*VC_SM_SHOW) (struct seq_file *s, void *v);
  11174. +struct SM_PDE_T {
  11175. + VC_SM_SHOW show; /* Debug fs function hookup. */
  11176. + struct dentry *dir_entry; /* Debug fs directory entry. */
  11177. + void *priv_data; /* Private data */
  11178. +
  11179. +};
  11180. +
  11181. +/* Single resource allocation tracked for all devices.
  11182. +*/
  11183. +struct sm_mmap {
  11184. + struct list_head map_list; /* Linked list of maps. */
  11185. +
  11186. + struct SM_RESOURCE_T *resource; /* Pointer to the resource. */
  11187. +
  11188. + pid_t res_pid; /* PID owning that resource. */
  11189. + unsigned int res_vc_hdl; /* Resource handle (videocore). */
  11190. + unsigned int res_usr_hdl; /* Resource handle (user). */
  11191. +
  11192. + long unsigned int res_addr; /* Mapped virtual address. */
  11193. + struct vm_area_struct *vma; /* VM area for this mapping. */
  11194. + unsigned int ref_count; /* Reference count to this vma. */
  11195. +
  11196. + /* Used to link maps associated with a resource. */
  11197. + struct list_head resource_map_list;
  11198. +};
  11199. +
  11200. +/* Single resource allocation tracked for each opened device.
  11201. +*/
  11202. +struct SM_RESOURCE_T {
  11203. + struct list_head resource_list; /* List of resources. */
  11204. + struct list_head global_resource_list; /* Global list of resources. */
  11205. +
  11206. + pid_t pid; /* PID owning that resource. */
  11207. + uint32_t res_guid; /* Unique identifier. */
  11208. + uint32_t lock_count; /* Lock count for this resource. */
  11209. + uint32_t ref_count; /* Ref count for this resource. */
  11210. +
  11211. + uint32_t res_handle; /* Resource allocation handle. */
  11212. + void *res_base_mem; /* Resource base memory address. */
  11213. + uint32_t res_size; /* Resource size allocated. */
  11214. + enum vmcs_sm_cache_e res_cached; /* Resource cache type. */
  11215. + struct SM_RESOURCE_T *res_shared; /* Shared resource */
  11216. +
  11217. + enum SM_STATS_T res_stats[END_ALL]; /* Resource statistics. */
  11218. +
  11219. + uint8_t map_count; /* Counter of mappings for this resource. */
  11220. + struct list_head map_list; /* Maps associated with a resource. */
  11221. +
  11222. + struct SM_PRIV_DATA_T *private;
  11223. +};
  11224. +
  11225. +/* Private file data associated with each opened device.
  11226. +*/
  11227. +struct SM_PRIV_DATA_T {
  11228. + struct list_head resource_list; /* List of resources. */
  11229. +
  11230. + pid_t pid; /* PID of creator. */
  11231. +
  11232. + struct dentry *dir_pid; /* Debug fs entries root. */
  11233. + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
  11234. + struct SM_PDE_T dir_res; /* Debug fs resource sub-tree. */
  11235. +
  11236. + int restart_sys; /* Tracks restart on interrupt. */
  11237. + VC_SM_MSG_TYPE int_action; /* Interrupted action. */
  11238. + uint32_t int_trans_id; /* Interrupted transaction. */
  11239. +
  11240. +};
  11241. +
  11242. +/* Global state information.
  11243. +*/
  11244. +struct SM_STATE_T {
  11245. + VC_VCHI_SM_HANDLE_T sm_handle; /* Handle for videocore service. */
  11246. + struct dentry *dir_root; /* Debug fs entries root. */
  11247. + struct dentry *dir_alloc; /* Debug fs entries allocations. */
  11248. + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
  11249. + struct SM_PDE_T dir_state; /* Debug fs entries state sub-tree. */
  11250. + struct dentry *debug; /* Debug fs entries debug. */
  11251. +
  11252. + struct mutex map_lock; /* Global map lock. */
  11253. + struct list_head map_list; /* List of maps. */
  11254. + struct list_head resource_list; /* List of resources. */
  11255. +
  11256. + enum SM_STATS_T deceased[END_ALL]; /* Natural termination stats. */
  11257. + enum SM_STATS_T terminated[END_ALL]; /* Forced termination stats. */
  11258. + uint32_t res_deceased_cnt; /* Natural termination counter. */
  11259. + uint32_t res_terminated_cnt; /* Forced termination counter. */
  11260. +
  11261. + struct cdev sm_cdev; /* Device. */
  11262. + dev_t sm_devid; /* Device identifier. */
  11263. + struct class *sm_class; /* Class. */
  11264. + struct device *sm_dev; /* Device. */
  11265. +
  11266. + struct SM_PRIV_DATA_T *data_knl; /* Kernel internal data tracking. */
  11267. +
  11268. + struct mutex lock; /* Global lock. */
  11269. + uint32_t guid; /* GUID (next) tracker. */
  11270. +
  11271. +};
  11272. +
  11273. +/* ---- Private Variables ----------------------------------------------- */
  11274. +
  11275. +static struct SM_STATE_T *sm_state;
  11276. +static int sm_inited;
  11277. +
  11278. +static const char *const sm_cache_map_vector[] = {
  11279. + "(null)",
  11280. + "host",
  11281. + "videocore",
  11282. + "host+videocore",
  11283. +};
  11284. +
  11285. +/* ---- Private Function Prototypes -------------------------------------- */
  11286. +
  11287. +/* ---- Private Functions ------------------------------------------------ */
  11288. +
  11289. +static inline unsigned vcaddr_to_pfn(unsigned long vc_addr)
  11290. +{
  11291. + unsigned long pfn = vc_addr & 0x3FFFFFFF;
  11292. + pfn += mm_vc_mem_phys_addr;
  11293. + pfn >>= PAGE_SHIFT;
  11294. + return pfn;
  11295. +}
  11296. +
  11297. +/* Carries over to the state statistics the statistics once owned by a deceased
  11298. +** resource.
  11299. +*/
  11300. +static void vc_sm_resource_deceased(struct SM_RESOURCE_T *p_res, int terminated)
  11301. +{
  11302. + if (sm_state != NULL) {
  11303. + if (p_res != NULL) {
  11304. + int ix;
  11305. +
  11306. + if (terminated)
  11307. + sm_state->res_terminated_cnt++;
  11308. + else
  11309. + sm_state->res_deceased_cnt++;
  11310. +
  11311. + for (ix = 0; ix < END_ALL; ix++) {
  11312. + if (terminated)
  11313. + sm_state->terminated[ix] +=
  11314. + p_res->res_stats[ix];
  11315. + else
  11316. + sm_state->deceased[ix] +=
  11317. + p_res->res_stats[ix];
  11318. + }
  11319. + }
  11320. + }
  11321. +}
  11322. +
  11323. +/* Fetch a videocore handle corresponding to a mapping of the pid+address
  11324. +** returns 0 (ie NULL) if no such handle exists in the global map.
  11325. +*/
  11326. +static unsigned int vmcs_sm_vc_handle_from_pid_and_address(unsigned int pid,
  11327. + unsigned int addr)
  11328. +{
  11329. + struct sm_mmap *map = NULL;
  11330. + unsigned int handle = 0;
  11331. +
  11332. + if (!sm_state || addr == 0)
  11333. + goto out;
  11334. +
  11335. + mutex_lock(&(sm_state->map_lock));
  11336. +
  11337. + /* Lookup the resource.
  11338. + */
  11339. + if (!list_empty(&sm_state->map_list)) {
  11340. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11341. + if (map->res_pid != pid || map->res_addr != addr)
  11342. + continue;
  11343. +
  11344. + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> vc-hdl %x (usr-hdl %x)\n",
  11345. + __func__, map, map->res_pid, map->res_addr,
  11346. + map->res_vc_hdl, map->res_usr_hdl);
  11347. +
  11348. + handle = map->res_vc_hdl;
  11349. + break;
  11350. + }
  11351. + }
  11352. +
  11353. + mutex_unlock(&(sm_state->map_lock));
  11354. +
  11355. +out:
  11356. + /* Use a debug log here as it may be a valid situation that we query
  11357. + ** for something that is not mapped, we do not want a kernel log each
  11358. + ** time around.
  11359. + **
  11360. + ** There are other error log that would pop up accordingly if someone
  11361. + ** subsequently tries to use something invalid after being told not to
  11362. + ** use it...
  11363. + */
  11364. + if (handle == 0) {
  11365. + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
  11366. + __func__, pid, addr);
  11367. + }
  11368. +
  11369. + return handle;
  11370. +}
  11371. +
  11372. +/* Fetch a user handle corresponding to a mapping of the pid+address
  11373. +** returns 0 (ie NULL) if no such handle exists in the global map.
  11374. +*/
  11375. +static unsigned int vmcs_sm_usr_handle_from_pid_and_address(unsigned int pid,
  11376. + unsigned int addr)
  11377. +{
  11378. + struct sm_mmap *map = NULL;
  11379. + unsigned int handle = 0;
  11380. +
  11381. + if (!sm_state || addr == 0)
  11382. + goto out;
  11383. +
  11384. + mutex_lock(&(sm_state->map_lock));
  11385. +
  11386. + /* Lookup the resource.
  11387. + */
  11388. + if (!list_empty(&sm_state->map_list)) {
  11389. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11390. + if (map->res_pid != pid || map->res_addr != addr)
  11391. + continue;
  11392. +
  11393. + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> usr-hdl %x (vc-hdl %x)\n",
  11394. + __func__, map, map->res_pid, map->res_addr,
  11395. + map->res_usr_hdl, map->res_vc_hdl);
  11396. +
  11397. + handle = map->res_usr_hdl;
  11398. + break;
  11399. + }
  11400. + }
  11401. +
  11402. + mutex_unlock(&(sm_state->map_lock));
  11403. +
  11404. +out:
  11405. + /* Use a debug log here as it may be a valid situation that we query
  11406. + * for something that is not mapped yet.
  11407. + *
  11408. + * There are other error log that would pop up accordingly if someone
  11409. + * subsequently tries to use something invalid after being told not to
  11410. + * use it...
  11411. + */
  11412. + if (handle == 0)
  11413. + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
  11414. + __func__, pid, addr);
  11415. +
  11416. + return handle;
  11417. +}
  11418. +
  11419. +#if defined(DO_NOT_USE)
  11420. +/* Fetch an address corresponding to a mapping of the pid+handle
  11421. +** returns 0 (ie NULL) if no such address exists in the global map.
  11422. +*/
  11423. +static unsigned int vmcs_sm_usr_address_from_pid_and_vc_handle(unsigned int pid,
  11424. + unsigned int hdl)
  11425. +{
  11426. + struct sm_mmap *map = NULL;
  11427. + unsigned int addr = 0;
  11428. +
  11429. + if (sm_state == NULL || hdl == 0)
  11430. + goto out;
  11431. +
  11432. + mutex_lock(&(sm_state->map_lock));
  11433. +
  11434. + /* Lookup the resource.
  11435. + */
  11436. + if (!list_empty(&sm_state->map_list)) {
  11437. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11438. + if (map->res_pid != pid || map->res_vc_hdl != hdl)
  11439. + continue;
  11440. +
  11441. + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
  11442. + __func__, map, map->res_pid, map->res_vc_hdl,
  11443. + map->res_usr_hdl, map->res_addr);
  11444. +
  11445. + addr = map->res_addr;
  11446. + break;
  11447. + }
  11448. + }
  11449. +
  11450. + mutex_unlock(&(sm_state->map_lock));
  11451. +
  11452. +out:
  11453. + /* Use a debug log here as it may be a valid situation that we query
  11454. + ** for something that is not mapped, we do not want a kernel log each
  11455. + ** time around.
  11456. + **
  11457. + ** There are other error log that would pop up accordingly if someone
  11458. + ** subsequently tries to use something invalid after being told not to
  11459. + ** use it...
  11460. + */
  11461. + if (addr == 0)
  11462. + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n",
  11463. + __func__, pid, hdl);
  11464. +
  11465. + return addr;
  11466. +}
  11467. +#endif
  11468. +
  11469. +/* Fetch an address corresponding to a mapping of the pid+handle
  11470. +** returns 0 (ie NULL) if no such address exists in the global map.
  11471. +*/
  11472. +static unsigned int vmcs_sm_usr_address_from_pid_and_usr_handle(unsigned int
  11473. + pid,
  11474. + unsigned int
  11475. + hdl)
  11476. +{
  11477. + struct sm_mmap *map = NULL;
  11478. + unsigned int addr = 0;
  11479. +
  11480. + if (sm_state == NULL || hdl == 0)
  11481. + goto out;
  11482. +
  11483. + mutex_lock(&(sm_state->map_lock));
  11484. +
  11485. + /* Lookup the resource.
  11486. + */
  11487. + if (!list_empty(&sm_state->map_list)) {
  11488. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11489. + if (map->res_pid != pid || map->res_usr_hdl != hdl)
  11490. + continue;
  11491. +
  11492. + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
  11493. + __func__, map, map->res_pid, map->res_vc_hdl,
  11494. + map->res_usr_hdl, map->res_addr);
  11495. +
  11496. + addr = map->res_addr;
  11497. + break;
  11498. + }
  11499. + }
  11500. +
  11501. + mutex_unlock(&(sm_state->map_lock));
  11502. +
  11503. +out:
  11504. + /* Use a debug log here as it may be a valid situation that we query
  11505. + * for something that is not mapped, we do not want a kernel log each
  11506. + * time around.
  11507. + *
  11508. + * There are other error log that would pop up accordingly if someone
  11509. + * subsequently tries to use something invalid after being told not to
  11510. + * use it...
  11511. + */
  11512. + if (addr == 0)
  11513. + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n", __func__,
  11514. + pid, hdl);
  11515. +
  11516. + return addr;
  11517. +}
  11518. +
  11519. +/* Adds a resource mapping to the global data list.
  11520. +*/
  11521. +static void vmcs_sm_add_map(struct SM_STATE_T *state,
  11522. + struct SM_RESOURCE_T *resource, struct sm_mmap *map)
  11523. +{
  11524. + mutex_lock(&(state->map_lock));
  11525. +
  11526. + /* Add to the global list of mappings
  11527. + */
  11528. + list_add(&map->map_list, &state->map_list);
  11529. +
  11530. + /* Add to the list of mappings for this resource
  11531. + */
  11532. + list_add(&map->resource_map_list, &resource->map_list);
  11533. + resource->map_count++;
  11534. +
  11535. + mutex_unlock(&(state->map_lock));
  11536. +
  11537. + pr_debug("[%s]: added map %p (pid %u, vc-hdl %x, usr-hdl %x, addr %lx)\n",
  11538. + __func__, map, map->res_pid, map->res_vc_hdl,
  11539. + map->res_usr_hdl, map->res_addr);
  11540. +}
  11541. +
  11542. +/* Removes a resource mapping from the global data list.
  11543. +*/
  11544. +static void vmcs_sm_remove_map(struct SM_STATE_T *state,
  11545. + struct SM_RESOURCE_T *resource,
  11546. + struct sm_mmap *map)
  11547. +{
  11548. + mutex_lock(&(state->map_lock));
  11549. +
  11550. + /* Remove from the global list of mappings
  11551. + */
  11552. + list_del(&map->map_list);
  11553. +
  11554. + /* Remove from the list of mapping for this resource
  11555. + */
  11556. + list_del(&map->resource_map_list);
  11557. + if (resource->map_count > 0)
  11558. + resource->map_count--;
  11559. +
  11560. + mutex_unlock(&(state->map_lock));
  11561. +
  11562. + pr_debug("[%s]: removed map %p (pid %d, vc-hdl %x, usr-hdl %x, addr %lx)\n",
  11563. + __func__, map, map->res_pid, map->res_vc_hdl, map->res_usr_hdl,
  11564. + map->res_addr);
  11565. +
  11566. + kfree(map);
  11567. +}
  11568. +
  11569. +/* Read callback for the global state proc entry.
  11570. +*/
  11571. +static int vc_sm_global_state_show(struct seq_file *s, void *v)
  11572. +{
  11573. + struct sm_mmap *map = NULL;
  11574. + int map_count = 0;
  11575. +
  11576. + if (sm_state == NULL)
  11577. + return 0;
  11578. +
  11579. + seq_printf(s, "\nVC-ServiceHandle 0x%x\n",
  11580. + (unsigned int)sm_state->sm_handle);
  11581. +
  11582. + /* Log all applicable mapping(s).
  11583. + */
  11584. +
  11585. + mutex_lock(&(sm_state->map_lock));
  11586. +
  11587. + if (!list_empty(&sm_state->map_list)) {
  11588. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11589. + map_count++;
  11590. +
  11591. + seq_printf(s, "\nMapping 0x%x\n",
  11592. + (unsigned int)map);
  11593. + seq_printf(s, " TGID %u\n",
  11594. + map->res_pid);
  11595. + seq_printf(s, " VC-HDL 0x%x\n",
  11596. + map->res_vc_hdl);
  11597. + seq_printf(s, " USR-HDL 0x%x\n",
  11598. + map->res_usr_hdl);
  11599. + seq_printf(s, " USR-ADDR 0x%lx\n",
  11600. + map->res_addr);
  11601. + }
  11602. + }
  11603. +
  11604. + mutex_unlock(&(sm_state->map_lock));
  11605. + seq_printf(s, "\n\nTotal map count: %d\n\n", map_count);
  11606. +
  11607. + return 0;
  11608. +}
  11609. +
  11610. +static int vc_sm_global_statistics_show(struct seq_file *s, void *v)
  11611. +{
  11612. + int ix;
  11613. +
  11614. + /* Global state tracked statistics.
  11615. + */
  11616. + if (sm_state != NULL) {
  11617. + seq_puts(s, "\nDeceased Resources Statistics\n");
  11618. +
  11619. + seq_printf(s, "\nNatural Cause (%u occurences)\n",
  11620. + sm_state->res_deceased_cnt);
  11621. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  11622. + if (sm_state->deceased[ix] > 0) {
  11623. + seq_printf(s, " %u\t%s\n",
  11624. + sm_state->deceased[ix],
  11625. + sm_stats_human_read[ix]);
  11626. + }
  11627. + }
  11628. + seq_puts(s, "\n");
  11629. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  11630. + if (sm_state->deceased[ix + END_ATTEMPT] > 0) {
  11631. + seq_printf(s, " %u\tFAILED %s\n",
  11632. + sm_state->deceased[ix + END_ATTEMPT],
  11633. + sm_stats_human_read[ix]);
  11634. + }
  11635. + }
  11636. +
  11637. + seq_printf(s, "\nForcefull (%u occurences)\n",
  11638. + sm_state->res_terminated_cnt);
  11639. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  11640. + if (sm_state->terminated[ix] > 0) {
  11641. + seq_printf(s, " %u\t%s\n",
  11642. + sm_state->terminated[ix],
  11643. + sm_stats_human_read[ix]);
  11644. + }
  11645. + }
  11646. + seq_puts(s, "\n");
  11647. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  11648. + if (sm_state->terminated[ix + END_ATTEMPT] > 0) {
  11649. + seq_printf(s, " %u\tFAILED %s\n",
  11650. + sm_state->terminated[ix +
  11651. + END_ATTEMPT],
  11652. + sm_stats_human_read[ix]);
  11653. + }
  11654. + }
  11655. + }
  11656. +
  11657. + return 0;
  11658. +}
  11659. +
  11660. +#if 0
  11661. +/* Read callback for the statistics proc entry.
  11662. +*/
  11663. +static int vc_sm_statistics_show(struct seq_file *s, void *v)
  11664. +{
  11665. + int ix;
  11666. + struct SM_PRIV_DATA_T *file_data;
  11667. + struct SM_RESOURCE_T *resource;
  11668. + int res_count = 0;
  11669. + struct SM_PDE_T *p_pde;
  11670. +
  11671. + p_pde = (struct SM_PDE_T *)(s->private);
  11672. + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
  11673. +
  11674. + if (file_data == NULL)
  11675. + return 0;
  11676. +
  11677. + /* Per process statistics.
  11678. + */
  11679. +
  11680. + seq_printf(s, "\nStatistics for TGID %d\n", file_data->pid);
  11681. +
  11682. + mutex_lock(&(sm_state->map_lock));
  11683. +
  11684. + if (!list_empty(&file_data->resource_list)) {
  11685. + list_for_each_entry(resource, &file_data->resource_list,
  11686. + resource_list) {
  11687. + res_count++;
  11688. +
  11689. + seq_printf(s, "\nGUID: 0x%x\n\n",
  11690. + resource->res_guid);
  11691. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  11692. + if (resource->res_stats[ix] > 0) {
  11693. + seq_printf(s,
  11694. + " %u\t%s\n",
  11695. + resource->res_stats[ix],
  11696. + sm_stats_human_read[ix]);
  11697. + }
  11698. + }
  11699. + seq_puts(s, "\n");
  11700. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  11701. + if (resource->res_stats[ix + END_ATTEMPT] > 0) {
  11702. + seq_printf(s,
  11703. + " %u\tFAILED %s\n",
  11704. + resource->res_stats[
  11705. + ix + END_ATTEMPT],
  11706. + sm_stats_human_read[ix]);
  11707. + }
  11708. + }
  11709. + }
  11710. + }
  11711. +
  11712. + mutex_unlock(&(sm_state->map_lock));
  11713. +
  11714. + seq_printf(s, "\nResources Count %d\n", res_count);
  11715. +
  11716. + return 0;
  11717. +}
  11718. +#endif
  11719. +
  11720. +#if 0
  11721. +/* Read callback for the allocation proc entry. */
  11722. +static int vc_sm_alloc_show(struct seq_file *s, void *v)
  11723. +{
  11724. + struct SM_PRIV_DATA_T *file_data;
  11725. + struct SM_RESOURCE_T *resource;
  11726. + int alloc_count = 0;
  11727. + struct SM_PDE_T *p_pde;
  11728. +
  11729. + p_pde = (struct SM_PDE_T *)(s->private);
  11730. + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
  11731. +
  11732. + if (!file_data)
  11733. + return 0;
  11734. +
  11735. + /* Per process statistics. */
  11736. + seq_printf(s, "\nAllocation for TGID %d\n", file_data->pid);
  11737. +
  11738. + mutex_lock(&(sm_state->map_lock));
  11739. +
  11740. + if (!list_empty(&file_data->resource_list)) {
  11741. + list_for_each_entry(resource, &file_data->resource_list,
  11742. + resource_list) {
  11743. + alloc_count++;
  11744. +
  11745. + seq_printf(s, "\nGUID: 0x%x\n",
  11746. + resource->res_guid);
  11747. + seq_printf(s, "Lock Count: %u\n",
  11748. + resource->lock_count);
  11749. + seq_printf(s, "Mapped: %s\n",
  11750. + (resource->map_count ? "yes" : "no"));
  11751. + seq_printf(s, "VC-handle: 0x%x\n",
  11752. + resource->res_handle);
  11753. + seq_printf(s, "VC-address: 0x%p\n",
  11754. + resource->res_base_mem);
  11755. + seq_printf(s, "VC-size (bytes): %u\n",
  11756. + resource->res_size);
  11757. + seq_printf(s, "Cache: %s\n",
  11758. + sm_cache_map_vector[resource->res_cached]);
  11759. + }
  11760. + }
  11761. +
  11762. + mutex_unlock(&(sm_state->map_lock));
  11763. +
  11764. + seq_printf(s, "\n\nTotal allocation count: %d\n\n", alloc_count);
  11765. +
  11766. + return 0;
  11767. +}
  11768. +#endif
  11769. +
  11770. +static int vc_sm_seq_file_show(struct seq_file *s, void *v)
  11771. +{
  11772. + struct SM_PDE_T *sm_pde;
  11773. +
  11774. + sm_pde = (struct SM_PDE_T *)(s->private);
  11775. +
  11776. + if (sm_pde && sm_pde->show)
  11777. + sm_pde->show(s, v);
  11778. +
  11779. + return 0;
  11780. +}
  11781. +
  11782. +static int vc_sm_single_open(struct inode *inode, struct file *file)
  11783. +{
  11784. + return single_open(file, vc_sm_seq_file_show, inode->i_private);
  11785. +}
  11786. +
  11787. +static const struct file_operations vc_sm_debug_fs_fops = {
  11788. + .open = vc_sm_single_open,
  11789. + .read = seq_read,
  11790. + .llseek = seq_lseek,
  11791. + .release = single_release,
  11792. +};
  11793. +
  11794. +/* Adds a resource to the private data list which tracks all the allocated
  11795. +** data.
  11796. +*/
  11797. +static void vmcs_sm_add_resource(struct SM_PRIV_DATA_T *privdata,
  11798. + struct SM_RESOURCE_T *resource)
  11799. +{
  11800. + mutex_lock(&(sm_state->map_lock));
  11801. + list_add(&resource->resource_list, &privdata->resource_list);
  11802. + list_add(&resource->global_resource_list, &sm_state->resource_list);
  11803. + mutex_unlock(&(sm_state->map_lock));
  11804. +
  11805. + pr_debug("[%s]: added resource %p (base addr %p, hdl %x, size %u, cache %u)\n",
  11806. + __func__, resource, resource->res_base_mem,
  11807. + resource->res_handle, resource->res_size, resource->res_cached);
  11808. +}
  11809. +
  11810. +/* Locates a resource and acquire a reference on it.
  11811. +** The resource won't be deleted while there is a reference on it.
  11812. +*/
  11813. +static struct SM_RESOURCE_T *vmcs_sm_acquire_resource(struct SM_PRIV_DATA_T
  11814. + *private,
  11815. + unsigned int res_guid)
  11816. +{
  11817. + struct SM_RESOURCE_T *resource, *ret = NULL;
  11818. +
  11819. + mutex_lock(&(sm_state->map_lock));
  11820. +
  11821. + list_for_each_entry(resource, &private->resource_list, resource_list) {
  11822. + if (resource->res_guid != res_guid)
  11823. + continue;
  11824. +
  11825. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  11826. + __func__, resource, resource->res_guid,
  11827. + resource->res_base_mem, resource->res_handle,
  11828. + resource->res_size, resource->res_cached);
  11829. + resource->ref_count++;
  11830. + ret = resource;
  11831. + break;
  11832. + }
  11833. +
  11834. + mutex_unlock(&(sm_state->map_lock));
  11835. +
  11836. + return ret;
  11837. +}
  11838. +
  11839. +/* Locates a resource and acquire a reference on it.
  11840. +** The resource won't be deleted while there is a reference on it.
  11841. +*/
  11842. +static struct SM_RESOURCE_T *vmcs_sm_acquire_first_resource(
  11843. + struct SM_PRIV_DATA_T *private)
  11844. +{
  11845. + struct SM_RESOURCE_T *resource, *ret = NULL;
  11846. +
  11847. + mutex_lock(&(sm_state->map_lock));
  11848. +
  11849. + list_for_each_entry(resource, &private->resource_list, resource_list) {
  11850. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  11851. + __func__, resource, resource->res_guid,
  11852. + resource->res_base_mem, resource->res_handle,
  11853. + resource->res_size, resource->res_cached);
  11854. + resource->ref_count++;
  11855. + ret = resource;
  11856. + break;
  11857. + }
  11858. +
  11859. + mutex_unlock(&(sm_state->map_lock));
  11860. +
  11861. + return ret;
  11862. +}
  11863. +
  11864. +/* Locates a resource and acquire a reference on it.
  11865. +** The resource won't be deleted while there is a reference on it.
  11866. +*/
  11867. +static struct SM_RESOURCE_T *vmcs_sm_acquire_global_resource(unsigned int
  11868. + res_guid)
  11869. +{
  11870. + struct SM_RESOURCE_T *resource, *ret = NULL;
  11871. +
  11872. + mutex_lock(&(sm_state->map_lock));
  11873. +
  11874. + list_for_each_entry(resource, &sm_state->resource_list,
  11875. + global_resource_list) {
  11876. + if (resource->res_guid != res_guid)
  11877. + continue;
  11878. +
  11879. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  11880. + __func__, resource, resource->res_guid,
  11881. + resource->res_base_mem, resource->res_handle,
  11882. + resource->res_size, resource->res_cached);
  11883. + resource->ref_count++;
  11884. + ret = resource;
  11885. + break;
  11886. + }
  11887. +
  11888. + mutex_unlock(&(sm_state->map_lock));
  11889. +
  11890. + return ret;
  11891. +}
  11892. +
  11893. +/* Release a previously acquired resource.
  11894. +** The resource will be deleted when its refcount reaches 0.
  11895. +*/
  11896. +static void vmcs_sm_release_resource(struct SM_RESOURCE_T *resource, int force)
  11897. +{
  11898. + struct SM_PRIV_DATA_T *private = resource->private;
  11899. + struct sm_mmap *map, *map_tmp;
  11900. + struct SM_RESOURCE_T *res_tmp;
  11901. + int ret;
  11902. +
  11903. + mutex_lock(&(sm_state->map_lock));
  11904. +
  11905. + if (--resource->ref_count) {
  11906. + if (force)
  11907. + pr_err("[%s]: resource %p in use\n", __func__, resource);
  11908. +
  11909. + mutex_unlock(&(sm_state->map_lock));
  11910. + return;
  11911. + }
  11912. +
  11913. + /* Time to free the resource. Start by removing it from the list */
  11914. + list_del(&resource->resource_list);
  11915. + list_del(&resource->global_resource_list);
  11916. +
  11917. + /* Walk the global resource list, find out if the resource is used
  11918. + * somewhere else. In which case we don't want to delete it.
  11919. + */
  11920. + list_for_each_entry(res_tmp, &sm_state->resource_list,
  11921. + global_resource_list) {
  11922. + if (res_tmp->res_handle == resource->res_handle) {
  11923. + resource->res_handle = 0;
  11924. + break;
  11925. + }
  11926. + }
  11927. +
  11928. + mutex_unlock(&(sm_state->map_lock));
  11929. +
  11930. + pr_debug("[%s]: freeing data - guid %x, hdl %x, base address %p\n",
  11931. + __func__, resource->res_guid, resource->res_handle,
  11932. + resource->res_base_mem);
  11933. + resource->res_stats[FREE]++;
  11934. +
  11935. + /* Make sure the resource we're removing is unmapped first */
  11936. + if (resource->map_count && !list_empty(&resource->map_list)) {
  11937. + down_write(&current->mm->mmap_sem);
  11938. + list_for_each_entry_safe(map, map_tmp, &resource->map_list,
  11939. + resource_map_list) {
  11940. + ret =
  11941. + do_munmap(current->mm, map->res_addr,
  11942. + resource->res_size);
  11943. + if (ret) {
  11944. + pr_err("[%s]: could not unmap resource %p\n",
  11945. + __func__, resource);
  11946. + }
  11947. + }
  11948. + up_write(&current->mm->mmap_sem);
  11949. + }
  11950. +
  11951. + /* Free up the videocore allocated resource.
  11952. + */
  11953. + if (resource->res_handle) {
  11954. + VC_SM_FREE_T free = {
  11955. + resource->res_handle, resource->res_base_mem
  11956. + };
  11957. + int status = vc_vchi_sm_free(sm_state->sm_handle, &free,
  11958. + &private->int_trans_id);
  11959. + if (status != 0 && status != -EINTR) {
  11960. + pr_err("[%s]: failed to free memory on videocore (status: %u, trans_id: %u)\n",
  11961. + __func__, status, private->int_trans_id);
  11962. + resource->res_stats[FREE_FAIL]++;
  11963. + ret = -EPERM;
  11964. + }
  11965. + }
  11966. +
  11967. + /* Free up the shared resource.
  11968. + */
  11969. + if (resource->res_shared)
  11970. + vmcs_sm_release_resource(resource->res_shared, 0);
  11971. +
  11972. + /* Free up the local resource tracking this allocation.
  11973. + */
  11974. + vc_sm_resource_deceased(resource, force);
  11975. + kfree(resource);
  11976. +}
  11977. +
  11978. +/* Dump the map table for the driver. If process is -1, dumps the whole table,
  11979. +** if process is a valid pid (non -1) dump only the entries associated with the
  11980. +** pid of interest.
  11981. +*/
  11982. +static void vmcs_sm_host_walk_map_per_pid(int pid)
  11983. +{
  11984. + struct sm_mmap *map = NULL;
  11985. +
  11986. + /* Make sure the device was started properly.
  11987. + */
  11988. + if (sm_state == NULL) {
  11989. + pr_err("[%s]: invalid device\n", __func__);
  11990. + return;
  11991. + }
  11992. +
  11993. + mutex_lock(&(sm_state->map_lock));
  11994. +
  11995. + /* Log all applicable mapping(s).
  11996. + */
  11997. + if (!list_empty(&sm_state->map_list)) {
  11998. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11999. + if (pid == -1 || map->res_pid == pid) {
  12000. + pr_info("[%s]: tgid: %u - vc-hdl: %x, usr-hdl: %x, usr-addr: %lx\n",
  12001. + __func__, map->res_pid, map->res_vc_hdl,
  12002. + map->res_usr_hdl, map->res_addr);
  12003. + }
  12004. + }
  12005. + }
  12006. +
  12007. + mutex_unlock(&(sm_state->map_lock));
  12008. +
  12009. + return;
  12010. +}
  12011. +
  12012. +/* Dump the allocation table from host side point of view. This only dumps the
  12013. +** data allocated for this process/device referenced by the file_data.
  12014. +*/
  12015. +static void vmcs_sm_host_walk_alloc(struct SM_PRIV_DATA_T *file_data)
  12016. +{
  12017. + struct SM_RESOURCE_T *resource = NULL;
  12018. +
  12019. + /* Make sure the device was started properly.
  12020. + */
  12021. + if ((sm_state == NULL) || (file_data == NULL)) {
  12022. + pr_err("[%s]: invalid device\n", __func__);
  12023. + return;
  12024. + }
  12025. +
  12026. + mutex_lock(&(sm_state->map_lock));
  12027. +
  12028. + if (!list_empty(&file_data->resource_list)) {
  12029. + list_for_each_entry(resource, &file_data->resource_list,
  12030. + resource_list) {
  12031. + pr_info("[%s]: guid: %x - hdl: %x, vc-mem: %p, size: %u, cache: %u\n",
  12032. + __func__, resource->res_guid, resource->res_handle,
  12033. + resource->res_base_mem, resource->res_size,
  12034. + resource->res_cached);
  12035. + }
  12036. + }
  12037. +
  12038. + mutex_unlock(&(sm_state->map_lock));
  12039. +
  12040. + return;
  12041. +}
  12042. +
  12043. +/* Create support for private data tracking.
  12044. +*/
  12045. +static struct SM_PRIV_DATA_T *vc_sm_create_priv_data(pid_t id)
  12046. +{
  12047. + char alloc_name[32];
  12048. + struct SM_PRIV_DATA_T *file_data = NULL;
  12049. +
  12050. + /* Allocate private structure. */
  12051. + file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
  12052. +
  12053. + if (!file_data) {
  12054. + pr_err("[%s]: cannot allocate file data\n", __func__);
  12055. + goto out;
  12056. + }
  12057. +
  12058. + snprintf(alloc_name, sizeof(alloc_name), "%d", id);
  12059. +
  12060. + INIT_LIST_HEAD(&file_data->resource_list);
  12061. + file_data->pid = id;
  12062. + file_data->dir_pid = debugfs_create_dir(alloc_name,
  12063. + sm_state->dir_alloc);
  12064. +#if 0
  12065. + /* TODO: fix this to support querying statistics per pid */
  12066. +
  12067. + if (IS_ERR_OR_NULL(file_data->dir_pid)) {
  12068. + file_data->dir_pid = NULL;
  12069. + } else {
  12070. + struct dentry *dir_entry;
  12071. +
  12072. + dir_entry = debugfs_create_file(VC_SM_RESOURCES, S_IRUGO,
  12073. + file_data->dir_pid, file_data,
  12074. + vc_sm_debug_fs_fops);
  12075. +
  12076. + file_data->dir_res.dir_entry = dir_entry;
  12077. + file_data->dir_res.priv_data = file_data;
  12078. + file_data->dir_res.show = &vc_sm_alloc_show;
  12079. +
  12080. + dir_entry = debugfs_create_file(VC_SM_STATS, S_IRUGO,
  12081. + file_data->dir_pid, file_data,
  12082. + vc_sm_debug_fs_fops);
  12083. +
  12084. + file_data->dir_res.dir_entry = dir_entry;
  12085. + file_data->dir_res.priv_data = file_data;
  12086. + file_data->dir_res.show = &vc_sm_statistics_show;
  12087. + }
  12088. + pr_debug("[%s]: private data allocated %p\n", __func__, file_data);
  12089. +
  12090. +#endif
  12091. +out:
  12092. + return file_data;
  12093. +}
  12094. +
  12095. +/* Open the device. Creates a private state to help track all allocation
  12096. +** associated with this device.
  12097. +*/
  12098. +static int vc_sm_open(struct inode *inode, struct file *file)
  12099. +{
  12100. + int ret = 0;
  12101. +
  12102. + /* Make sure the device was started properly.
  12103. + */
  12104. + if (!sm_state) {
  12105. + pr_err("[%s]: invalid device\n", __func__);
  12106. + ret = -EPERM;
  12107. + goto out;
  12108. + }
  12109. +
  12110. + file->private_data = vc_sm_create_priv_data(current->tgid);
  12111. + if (file->private_data == NULL) {
  12112. + pr_err("[%s]: failed to create data tracker\n", __func__);
  12113. +
  12114. + ret = -ENOMEM;
  12115. + goto out;
  12116. + }
  12117. +
  12118. +out:
  12119. + return ret;
  12120. +}
  12121. +
  12122. +/* Close the device. Free up all resources still associated with this device
  12123. +** at the time.
  12124. +*/
  12125. +static int vc_sm_release(struct inode *inode, struct file *file)
  12126. +{
  12127. + struct SM_PRIV_DATA_T *file_data =
  12128. + (struct SM_PRIV_DATA_T *)file->private_data;
  12129. + struct SM_RESOURCE_T *resource;
  12130. + int ret = 0;
  12131. +
  12132. + /* Make sure the device was started properly.
  12133. + */
  12134. + if (sm_state == NULL || file_data == NULL) {
  12135. + pr_err("[%s]: invalid device\n", __func__);
  12136. + ret = -EPERM;
  12137. + goto out;
  12138. + }
  12139. +
  12140. + pr_debug("[%s]: using private data %p\n", __func__, file_data);
  12141. +
  12142. + if (file_data->restart_sys == -EINTR) {
  12143. + VC_SM_ACTION_CLEAN_T action_clean;
  12144. +
  12145. + pr_debug("[%s]: releasing following EINTR on %u (trans_id: %u) (likely due to signal)...\n",
  12146. + __func__, file_data->int_action,
  12147. + file_data->int_trans_id);
  12148. +
  12149. + action_clean.res_action = file_data->int_action;
  12150. + action_clean.action_trans_id = file_data->int_trans_id;
  12151. +
  12152. + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
  12153. + }
  12154. +
  12155. + while ((resource = vmcs_sm_acquire_first_resource(file_data)) != NULL) {
  12156. + vmcs_sm_release_resource(resource, 0);
  12157. + vmcs_sm_release_resource(resource, 1);
  12158. + }
  12159. +
  12160. + /* Remove the corresponding proc entry. */
  12161. + debugfs_remove_recursive(file_data->dir_pid);
  12162. +
  12163. + /* Terminate the private data.
  12164. + */
  12165. + kfree(file_data);
  12166. +
  12167. +out:
  12168. + return ret;
  12169. +}
  12170. +
  12171. +static void vcsm_vma_open(struct vm_area_struct *vma)
  12172. +{
  12173. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  12174. +
  12175. + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
  12176. + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
  12177. + (int)vma->vm_pgoff);
  12178. +
  12179. + map->ref_count++;
  12180. +}
  12181. +
  12182. +static void vcsm_vma_close(struct vm_area_struct *vma)
  12183. +{
  12184. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  12185. +
  12186. + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
  12187. + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
  12188. + (int)vma->vm_pgoff);
  12189. +
  12190. + map->ref_count--;
  12191. +
  12192. + /* Remove from the map table.
  12193. + */
  12194. + if (map->ref_count == 0)
  12195. + vmcs_sm_remove_map(sm_state, map->resource, map);
  12196. +}
  12197. +
  12198. +static int vcsm_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  12199. +{
  12200. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  12201. + struct SM_RESOURCE_T *resource = map->resource;
  12202. + pgoff_t page_offset;
  12203. + unsigned long pfn;
  12204. + int ret = 0;
  12205. +
  12206. + /* Lock the resource if necessary.
  12207. + */
  12208. + if (!resource->lock_count) {
  12209. + VC_SM_LOCK_UNLOCK_T lock_unlock;
  12210. + VC_SM_LOCK_RESULT_T lock_result;
  12211. + int status;
  12212. +
  12213. + lock_unlock.res_handle = resource->res_handle;
  12214. + lock_unlock.res_mem = resource->res_base_mem;
  12215. +
  12216. + pr_debug("[%s]: attempt to lock data - hdl %x, base address %p\n",
  12217. + __func__, lock_unlock.res_handle, lock_unlock.res_mem);
  12218. +
  12219. + /* Lock the videocore allocated resource.
  12220. + */
  12221. + status = vc_vchi_sm_lock(sm_state->sm_handle,
  12222. + &lock_unlock, &lock_result, 0);
  12223. + if ((status != 0) ||
  12224. + ((status == 0) && (lock_result.res_mem == NULL))) {
  12225. + pr_err("[%s]: failed to lock memory on videocore (status: %u)\n",
  12226. + __func__, status);
  12227. + resource->res_stats[LOCK_FAIL]++;
  12228. + return VM_FAULT_SIGBUS;
  12229. + }
  12230. +
  12231. + pfn = vcaddr_to_pfn((unsigned long)resource->res_base_mem);
  12232. + outer_inv_range(__pfn_to_phys(pfn),
  12233. + __pfn_to_phys(pfn) + resource->res_size);
  12234. +
  12235. + resource->res_stats[LOCK]++;
  12236. + resource->lock_count++;
  12237. +
  12238. + /* Keep track of the new base memory.
  12239. + */
  12240. + if ((lock_result.res_mem != NULL) &&
  12241. + (lock_result.res_old_mem != NULL) &&
  12242. + (lock_result.res_mem != lock_result.res_old_mem)) {
  12243. + resource->res_base_mem = lock_result.res_mem;
  12244. + }
  12245. + }
  12246. +
  12247. + /* We don't use vmf->pgoff since that has the fake offset */
  12248. + page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start);
  12249. + pfn = (uint32_t)resource->res_base_mem & 0x3FFFFFFF;
  12250. + pfn += mm_vc_mem_phys_addr;
  12251. + pfn += page_offset;
  12252. + pfn >>= PAGE_SHIFT;
  12253. +
  12254. + /* Finally, remap it */
  12255. + ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  12256. +
  12257. + switch (ret) {
  12258. + case 0:
  12259. + case -ERESTARTSYS:
  12260. + return VM_FAULT_NOPAGE;
  12261. + case -ENOMEM:
  12262. + case -EAGAIN:
  12263. + return VM_FAULT_OOM;
  12264. + default:
  12265. + return VM_FAULT_SIGBUS;
  12266. + }
  12267. +}
  12268. +
  12269. +static struct vm_operations_struct vcsm_vm_ops = {
  12270. + .open = vcsm_vma_open,
  12271. + .close = vcsm_vma_close,
  12272. + .fault = vcsm_vma_fault,
  12273. +};
  12274. +
  12275. +/* Walks a VMA and clean each valid page from the cache */
  12276. +static void vcsm_vma_cache_clean_page_range(unsigned long addr,
  12277. + unsigned long end)
  12278. +{
  12279. + pgd_t *pgd;
  12280. + pud_t *pud;
  12281. + pmd_t *pmd;
  12282. + pte_t *pte;
  12283. + unsigned long pgd_next, pud_next, pmd_next;
  12284. +
  12285. + if (addr >= end)
  12286. + return;
  12287. +
  12288. + /* Walk PGD */
  12289. + pgd = pgd_offset(current->mm, addr);
  12290. + do {
  12291. + pgd_next = pgd_addr_end(addr, end);
  12292. +
  12293. + if (pgd_none(*pgd) || pgd_bad(*pgd))
  12294. + continue;
  12295. +
  12296. + /* Walk PUD */
  12297. + pud = pud_offset(pgd, addr);
  12298. + do {
  12299. + pud_next = pud_addr_end(addr, pgd_next);
  12300. + if (pud_none(*pud) || pud_bad(*pud))
  12301. + continue;
  12302. +
  12303. + /* Walk PMD */
  12304. + pmd = pmd_offset(pud, addr);
  12305. + do {
  12306. + pmd_next = pmd_addr_end(addr, pud_next);
  12307. + if (pmd_none(*pmd) || pmd_bad(*pmd))
  12308. + continue;
  12309. +
  12310. + /* Walk PTE */
  12311. + pte = pte_offset_map(pmd, addr);
  12312. + do {
  12313. + if (pte_none(*pte)
  12314. + || !pte_present(*pte))
  12315. + continue;
  12316. +
  12317. + /* Clean + invalidate */
  12318. + dmac_flush_range((const void *) addr,
  12319. + (const void *)
  12320. + (addr + PAGE_SIZE));
  12321. +
  12322. + } while (pte++, addr +=
  12323. + PAGE_SIZE, addr != pmd_next);
  12324. + pte_unmap(pte);
  12325. +
  12326. + } while (pmd++, addr = pmd_next, addr != pud_next);
  12327. +
  12328. + } while (pud++, addr = pud_next, addr != pgd_next);
  12329. + } while (pgd++, addr = pgd_next, addr != end);
  12330. +}
  12331. +
  12332. +/* Map an allocated data into something that the user space.
  12333. +*/
  12334. +static int vc_sm_mmap(struct file *file, struct vm_area_struct *vma)
  12335. +{
  12336. + int ret = 0;
  12337. + struct SM_PRIV_DATA_T *file_data =
  12338. + (struct SM_PRIV_DATA_T *)file->private_data;
  12339. + struct SM_RESOURCE_T *resource = NULL;
  12340. + struct sm_mmap *map = NULL;
  12341. +
  12342. + /* Make sure the device was started properly.
  12343. + */
  12344. + if ((sm_state == NULL) || (file_data == NULL)) {
  12345. + pr_err("[%s]: invalid device\n", __func__);
  12346. + return -EPERM;
  12347. + }
  12348. +
  12349. + pr_debug("[%s]: private data %p, guid %x\n", __func__, file_data,
  12350. + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
  12351. +
  12352. + /* We lookup to make sure that the data we are being asked to mmap is
  12353. + ** something that we allocated.
  12354. + **
  12355. + ** We use the offset information as the key to tell us which resource
  12356. + ** we are mapping.
  12357. + */
  12358. + resource = vmcs_sm_acquire_resource(file_data,
  12359. + ((unsigned int)vma->vm_pgoff <<
  12360. + PAGE_SHIFT));
  12361. + if (resource == NULL) {
  12362. + pr_err("[%s]: failed to locate resource for guid %x\n", __func__,
  12363. + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
  12364. + return -ENOMEM;
  12365. + }
  12366. +
  12367. + pr_debug("[%s]: guid %x, tgid %u, %u, %u\n",
  12368. + __func__, resource->res_guid, current->tgid, resource->pid,
  12369. + file_data->pid);
  12370. +
  12371. + /* Check permissions.
  12372. + */
  12373. + if (resource->pid && (resource->pid != current->tgid)) {
  12374. + pr_err("[%s]: current tgid %u != %u owner\n",
  12375. + __func__, current->tgid, resource->pid);
  12376. + ret = -EPERM;
  12377. + goto error;
  12378. + }
  12379. +
  12380. + /* Verify that what we are asked to mmap is proper.
  12381. + */
  12382. + if (resource->res_size != (unsigned int)(vma->vm_end - vma->vm_start)) {
  12383. + pr_err("[%s]: size inconsistency (resource: %u - mmap: %u)\n",
  12384. + __func__,
  12385. + resource->res_size,
  12386. + (unsigned int)(vma->vm_end - vma->vm_start));
  12387. +
  12388. + ret = -EINVAL;
  12389. + goto error;
  12390. + }
  12391. +
  12392. + /* Keep track of the tuple in the global resource list such that one
  12393. + * can do a mapping lookup for address/memory handle.
  12394. + */
  12395. + map = kzalloc(sizeof(*map), GFP_KERNEL);
  12396. + if (map == NULL) {
  12397. + pr_err("[%s]: failed to allocate global tracking resource\n",
  12398. + __func__);
  12399. + ret = -ENOMEM;
  12400. + goto error;
  12401. + }
  12402. +
  12403. + map->res_pid = current->tgid;
  12404. + map->res_vc_hdl = resource->res_handle;
  12405. + map->res_usr_hdl = resource->res_guid;
  12406. + map->res_addr = (long unsigned int)vma->vm_start;
  12407. + map->resource = resource;
  12408. + map->vma = vma;
  12409. + vmcs_sm_add_map(sm_state, resource, map);
  12410. +
  12411. + /* We are not actually mapping the pages, we just provide a fault
  12412. + ** handler to allow pages to be mapped when accessed
  12413. + */
  12414. + vma->vm_flags |=
  12415. + VM_IO | VM_PFNMAP | VM_DONTCOPY | VM_DONTEXPAND;
  12416. + vma->vm_ops = &vcsm_vm_ops;
  12417. + vma->vm_private_data = map;
  12418. +
  12419. + /* vm_pgoff is the first PFN of the mapped memory */
  12420. + vma->vm_pgoff = (unsigned long)resource->res_base_mem & 0x3FFFFFFF;
  12421. + vma->vm_pgoff += mm_vc_mem_phys_addr;
  12422. + vma->vm_pgoff >>= PAGE_SHIFT;
  12423. +
  12424. + if ((resource->res_cached == VMCS_SM_CACHE_NONE) ||
  12425. + (resource->res_cached == VMCS_SM_CACHE_VC)) {
  12426. + /* Allocated non host cached memory, honour it.
  12427. + */
  12428. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  12429. + }
  12430. +
  12431. + pr_debug("[%s]: resource %p (guid %x) - cnt %u, base address %p, handle %x, size %u (%u), cache %u\n",
  12432. + __func__,
  12433. + resource, resource->res_guid, resource->lock_count,
  12434. + resource->res_base_mem, resource->res_handle,
  12435. + resource->res_size, (unsigned int)(vma->vm_end - vma->vm_start),
  12436. + resource->res_cached);
  12437. +
  12438. + pr_debug("[%s]: resource %p (base address %p, handle %x) - map-count %d, usr-addr %x\n",
  12439. + __func__, resource, resource->res_base_mem,
  12440. + resource->res_handle, resource->map_count,
  12441. + (unsigned int)vma->vm_start);
  12442. +
  12443. + vcsm_vma_open(vma);
  12444. + resource->res_stats[MAP]++;
  12445. + vmcs_sm_release_resource(resource, 0);
  12446. + return 0;
  12447. +
  12448. +error:
  12449. + vmcs_sm_release_resource(resource, 0);
  12450. + resource->res_stats[MAP_FAIL]++;
  12451. + return ret;
  12452. +}
  12453. +
  12454. +/* Allocate a shared memory handle and block.
  12455. +*/
  12456. +int vc_sm_ioctl_alloc(struct SM_PRIV_DATA_T *private,
  12457. + struct vmcs_sm_ioctl_alloc *ioparam)
  12458. +{
  12459. + int ret = 0;
  12460. + int status;
  12461. + struct SM_RESOURCE_T *resource;
  12462. + VC_SM_ALLOC_T alloc = { 0 };
  12463. + VC_SM_ALLOC_RESULT_T result = { 0 };
  12464. +
  12465. + /* Setup our allocation parameters */
  12466. + alloc.type = ((ioparam->cached == VMCS_SM_CACHE_VC)
  12467. + || (ioparam->cached ==
  12468. + VMCS_SM_CACHE_BOTH)) ? VC_SM_ALLOC_CACHED :
  12469. + VC_SM_ALLOC_NON_CACHED;
  12470. + alloc.base_unit = ioparam->size;
  12471. + alloc.num_unit = ioparam->num;
  12472. + alloc.allocator = current->tgid;
  12473. + /* Align to kernel page size */
  12474. + alloc.alignement = 4096;
  12475. + /* Align the size to the kernel page size */
  12476. + alloc.base_unit =
  12477. + (alloc.base_unit + alloc.alignement - 1) & ~(alloc.alignement - 1);
  12478. + if (*ioparam->name) {
  12479. + memcpy(alloc.name, ioparam->name, sizeof(alloc.name) - 1);
  12480. + } else {
  12481. + memcpy(alloc.name, VMCS_SM_RESOURCE_NAME_DEFAULT,
  12482. + sizeof(VMCS_SM_RESOURCE_NAME_DEFAULT));
  12483. + }
  12484. +
  12485. + pr_debug("[%s]: attempt to allocate \"%s\" data - type %u, base %u (%u), num %u, alignement %u\n",
  12486. + __func__, alloc.name, alloc.type, ioparam->size,
  12487. + alloc.base_unit, alloc.num_unit, alloc.alignement);
  12488. +
  12489. + /* Allocate local resource to track this allocation.
  12490. + */
  12491. + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  12492. + if (!resource) {
  12493. + ret = -ENOMEM;
  12494. + goto error;
  12495. + }
  12496. + INIT_LIST_HEAD(&resource->map_list);
  12497. + resource->ref_count++;
  12498. + resource->pid = current->tgid;
  12499. +
  12500. + /* Allocate the videocore resource.
  12501. + */
  12502. + status = vc_vchi_sm_alloc(sm_state->sm_handle, &alloc, &result,
  12503. + &private->int_trans_id);
  12504. + if (status == -EINTR) {
  12505. + pr_debug("[%s]: requesting allocate memory action restart (trans_id: %u)\n",
  12506. + __func__, private->int_trans_id);
  12507. + ret = -ERESTARTSYS;
  12508. + private->restart_sys = -EINTR;
  12509. + private->int_action = VC_SM_MSG_TYPE_ALLOC;
  12510. + goto error;
  12511. + } else if (status != 0 || (status == 0 && result.res_mem == NULL)) {
  12512. + pr_err("[%s]: failed to allocate memory on videocore (status: %u, trans_id: %u)\n",
  12513. + __func__, status, private->int_trans_id);
  12514. + ret = -ENOMEM;
  12515. + resource->res_stats[ALLOC_FAIL]++;
  12516. + goto error;
  12517. + }
  12518. +
  12519. + /* Keep track of the resource we created.
  12520. + */
  12521. + resource->private = private;
  12522. + resource->res_handle = result.res_handle;
  12523. + resource->res_base_mem = result.res_mem;
  12524. + resource->res_size = alloc.base_unit * alloc.num_unit;
  12525. + resource->res_cached = ioparam->cached;
  12526. +
  12527. + /* Kernel/user GUID. This global identifier is used for mmap'ing the
  12528. + * allocated region from user space, it is passed as the mmap'ing
  12529. + * offset, we use it to 'hide' the videocore handle/address.
  12530. + */
  12531. + mutex_lock(&sm_state->lock);
  12532. + resource->res_guid = ++sm_state->guid;
  12533. + mutex_unlock(&sm_state->lock);
  12534. + resource->res_guid <<= PAGE_SHIFT;
  12535. +
  12536. + vmcs_sm_add_resource(private, resource);
  12537. +
  12538. + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
  12539. + __func__, resource->res_guid, resource->res_handle,
  12540. + resource->res_base_mem, resource->res_size,
  12541. + resource->res_cached);
  12542. +
  12543. + /* We're done */
  12544. + resource->res_stats[ALLOC]++;
  12545. + ioparam->handle = resource->res_guid;
  12546. + return 0;
  12547. +
  12548. +error:
  12549. + pr_err("[%s]: failed to allocate \"%s\" data (%i) - type %u, base %u (%u), num %u, alignment %u\n",
  12550. + __func__, alloc.name, ret, alloc.type, ioparam->size,
  12551. + alloc.base_unit, alloc.num_unit, alloc.alignement);
  12552. + if (resource != NULL) {
  12553. + vc_sm_resource_deceased(resource, 1);
  12554. + kfree(resource);
  12555. + }
  12556. + return ret;
  12557. +}
  12558. +
  12559. +/* Share an allocate memory handle and block.
  12560. +*/
  12561. +int vc_sm_ioctl_alloc_share(struct SM_PRIV_DATA_T *private,
  12562. + struct vmcs_sm_ioctl_alloc_share *ioparam)
  12563. +{
  12564. + struct SM_RESOURCE_T *resource, *shared_resource;
  12565. + int ret = 0;
  12566. +
  12567. + pr_debug("[%s]: attempt to share resource %u\n", __func__,
  12568. + ioparam->handle);
  12569. +
  12570. + shared_resource = vmcs_sm_acquire_global_resource(ioparam->handle);
  12571. + if (shared_resource == NULL) {
  12572. + ret = -ENOMEM;
  12573. + goto error;
  12574. + }
  12575. +
  12576. + /* Allocate local resource to track this allocation.
  12577. + */
  12578. + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  12579. + if (resource == NULL) {
  12580. + pr_err("[%s]: failed to allocate local tracking resource\n",
  12581. + __func__);
  12582. + ret = -ENOMEM;
  12583. + goto error;
  12584. + }
  12585. + INIT_LIST_HEAD(&resource->map_list);
  12586. + resource->ref_count++;
  12587. + resource->pid = current->tgid;
  12588. +
  12589. + /* Keep track of the resource we created.
  12590. + */
  12591. + resource->private = private;
  12592. + resource->res_handle = shared_resource->res_handle;
  12593. + resource->res_base_mem = shared_resource->res_base_mem;
  12594. + resource->res_size = shared_resource->res_size;
  12595. + resource->res_cached = shared_resource->res_cached;
  12596. + resource->res_shared = shared_resource;
  12597. +
  12598. + mutex_lock(&sm_state->lock);
  12599. + resource->res_guid = ++sm_state->guid;
  12600. + mutex_unlock(&sm_state->lock);
  12601. + resource->res_guid <<= PAGE_SHIFT;
  12602. +
  12603. + vmcs_sm_add_resource(private, resource);
  12604. +
  12605. + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
  12606. + __func__, resource->res_guid, resource->res_handle,
  12607. + resource->res_base_mem, resource->res_size,
  12608. + resource->res_cached);
  12609. +
  12610. + /* We're done */
  12611. + resource->res_stats[ALLOC]++;
  12612. + ioparam->handle = resource->res_guid;
  12613. + ioparam->size = resource->res_size;
  12614. + return 0;
  12615. +
  12616. +error:
  12617. + pr_err("[%s]: failed to share %u\n", __func__, ioparam->handle);
  12618. + if (shared_resource != NULL)
  12619. + vmcs_sm_release_resource(shared_resource, 0);
  12620. +
  12621. + return ret;
  12622. +}
  12623. +
  12624. +/* Free a previously allocated shared memory handle and block.
  12625. +*/
  12626. +static int vc_sm_ioctl_free(struct SM_PRIV_DATA_T *private,
  12627. + struct vmcs_sm_ioctl_free *ioparam)
  12628. +{
  12629. + struct SM_RESOURCE_T *resource =
  12630. + vmcs_sm_acquire_resource(private, ioparam->handle);
  12631. +
  12632. + if (resource == NULL) {
  12633. + pr_err("[%s]: resource for guid %u does not exist\n", __func__,
  12634. + ioparam->handle);
  12635. + return -EINVAL;
  12636. + }
  12637. +
  12638. + /* Check permissions.
  12639. + */
  12640. + if (resource->pid && (resource->pid != current->tgid)) {
  12641. + pr_err("[%s]: current tgid %u != %u owner\n",
  12642. + __func__, current->tgid, resource->pid);
  12643. + vmcs_sm_release_resource(resource, 0);
  12644. + return -EPERM;
  12645. + }
  12646. +
  12647. + vmcs_sm_release_resource(resource, 0);
  12648. + vmcs_sm_release_resource(resource, 0);
  12649. + return 0;
  12650. +}
  12651. +
  12652. +/* Resize a previously allocated shared memory handle and block.
  12653. +*/
  12654. +static int vc_sm_ioctl_resize(struct SM_PRIV_DATA_T *private,
  12655. + struct vmcs_sm_ioctl_resize *ioparam)
  12656. +{
  12657. + int ret = 0;
  12658. + int status;
  12659. + VC_SM_RESIZE_T resize;
  12660. + struct SM_RESOURCE_T *resource;
  12661. +
  12662. + /* Locate resource from GUID.
  12663. + */
  12664. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  12665. + if (!resource) {
  12666. + pr_err("[%s]: failed resource - guid %x\n",
  12667. + __func__, ioparam->handle);
  12668. + ret = -EFAULT;
  12669. + goto error;
  12670. + }
  12671. +
  12672. + /* If the resource is locked, its reference count will be not NULL,
  12673. + ** in which case we will not be allowed to resize it anyways, so
  12674. + ** reject the attempt here.
  12675. + */
  12676. + if (resource->lock_count != 0) {
  12677. + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
  12678. + __func__, ioparam->handle, resource->lock_count);
  12679. + ret = -EFAULT;
  12680. + goto error;
  12681. + }
  12682. +
  12683. + /* Check permissions.
  12684. + */
  12685. + if (resource->pid && (resource->pid != current->tgid)) {
  12686. + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
  12687. + current->tgid, resource->pid);
  12688. + ret = -EPERM;
  12689. + goto error;
  12690. + }
  12691. +
  12692. + if (resource->map_count != 0) {
  12693. + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
  12694. + __func__, ioparam->handle, resource->map_count);
  12695. + ret = -EFAULT;
  12696. + goto error;
  12697. + }
  12698. +
  12699. + resize.res_handle = resource->res_handle;
  12700. + resize.res_mem = resource->res_base_mem;
  12701. + resize.res_new_size = ioparam->new_size;
  12702. +
  12703. + pr_debug("[%s]: attempt to resize data - guid %x, hdl %x, base address %p\n",
  12704. + __func__, ioparam->handle, resize.res_handle, resize.res_mem);
  12705. +
  12706. + /* Resize the videocore allocated resource.
  12707. + */
  12708. + status = vc_vchi_sm_resize(sm_state->sm_handle, &resize,
  12709. + &private->int_trans_id);
  12710. + if (status == -EINTR) {
  12711. + pr_debug("[%s]: requesting resize memory action restart (trans_id: %u)\n",
  12712. + __func__, private->int_trans_id);
  12713. + ret = -ERESTARTSYS;
  12714. + private->restart_sys = -EINTR;
  12715. + private->int_action = VC_SM_MSG_TYPE_RESIZE;
  12716. + goto error;
  12717. + } else if (status != 0) {
  12718. + pr_err("[%s]: failed to resize memory on videocore (status: %u, trans_id: %u)\n",
  12719. + __func__, status, private->int_trans_id);
  12720. + ret = -EPERM;
  12721. + goto error;
  12722. + }
  12723. +
  12724. + pr_debug("[%s]: success to resize data - hdl %x, size %d -> %d\n",
  12725. + __func__, resize.res_handle, resource->res_size,
  12726. + resize.res_new_size);
  12727. +
  12728. + /* Successfully resized, save the information and inform the user.
  12729. + */
  12730. + ioparam->old_size = resource->res_size;
  12731. + resource->res_size = resize.res_new_size;
  12732. +
  12733. +error:
  12734. + if (resource)
  12735. + vmcs_sm_release_resource(resource, 0);
  12736. +
  12737. + return ret;
  12738. +}
  12739. +
  12740. +/* Lock a previously allocated shared memory handle and block.
  12741. +*/
  12742. +static int vc_sm_ioctl_lock(struct SM_PRIV_DATA_T *private,
  12743. + struct vmcs_sm_ioctl_lock_unlock *ioparam,
  12744. + int change_cache, enum vmcs_sm_cache_e cache_type,
  12745. + unsigned int vc_addr)
  12746. +{
  12747. + int status;
  12748. + VC_SM_LOCK_UNLOCK_T lock;
  12749. + VC_SM_LOCK_RESULT_T result;
  12750. + struct SM_RESOURCE_T *resource;
  12751. + int ret = 0;
  12752. + struct sm_mmap *map, *map_tmp;
  12753. + long unsigned int phys_addr;
  12754. +
  12755. + map = NULL;
  12756. +
  12757. + /* Locate resource from GUID.
  12758. + */
  12759. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  12760. + if (resource == NULL) {
  12761. + ret = -EINVAL;
  12762. + goto error;
  12763. + }
  12764. +
  12765. + /* Check permissions.
  12766. + */
  12767. + if (resource->pid && (resource->pid != current->tgid)) {
  12768. + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
  12769. + current->tgid, resource->pid);
  12770. + ret = -EPERM;
  12771. + goto error;
  12772. + }
  12773. +
  12774. + lock.res_handle = resource->res_handle;
  12775. + lock.res_mem = resource->res_base_mem;
  12776. +
  12777. + /* Take the lock and get the address to be mapped.
  12778. + */
  12779. + if (vc_addr == 0) {
  12780. + pr_debug("[%s]: attempt to lock data - guid %x, hdl %x, base address %p\n",
  12781. + __func__, ioparam->handle, lock.res_handle,
  12782. + lock.res_mem);
  12783. +
  12784. + /* Lock the videocore allocated resource.
  12785. + */
  12786. + status = vc_vchi_sm_lock(sm_state->sm_handle, &lock, &result,
  12787. + &private->int_trans_id);
  12788. + if (status == -EINTR) {
  12789. + pr_debug("[%s]: requesting lock memory action restart (trans_id: %u)\n",
  12790. + __func__, private->int_trans_id);
  12791. + ret = -ERESTARTSYS;
  12792. + private->restart_sys = -EINTR;
  12793. + private->int_action = VC_SM_MSG_TYPE_LOCK;
  12794. + goto error;
  12795. + } else if (status != 0 ||
  12796. + (status == 0 && result.res_mem == NULL)) {
  12797. + pr_err("[%s]: failed to lock memory on videocore (status: %u, trans_id: %u)\n",
  12798. + __func__, status, private->int_trans_id);
  12799. + ret = -EPERM;
  12800. + resource->res_stats[LOCK_FAIL]++;
  12801. + goto error;
  12802. + }
  12803. +
  12804. + pr_debug("[%s]: succeed to lock data - hdl %x, base address %p (%p), ref-cnt %d\n",
  12805. + __func__, lock.res_handle, result.res_mem,
  12806. + lock.res_mem, resource->lock_count);
  12807. + }
  12808. + /* Lock assumed taken already, address to be mapped is known.
  12809. + */
  12810. + else
  12811. + resource->res_base_mem = (void *)vc_addr;
  12812. +
  12813. + resource->res_stats[LOCK]++;
  12814. + resource->lock_count++;
  12815. +
  12816. + /* Keep track of the new base memory allocation if it has changed.
  12817. + */
  12818. + if ((vc_addr == 0) &&
  12819. + (result.res_mem != NULL) &&
  12820. + (result.res_old_mem != NULL) &&
  12821. + (result.res_mem != result.res_old_mem)) {
  12822. + resource->res_base_mem = result.res_mem;
  12823. +
  12824. + /* Kernel allocated resources.
  12825. + */
  12826. + if (resource->pid == 0) {
  12827. + if (!list_empty(&resource->map_list)) {
  12828. + list_for_each_entry_safe(map, map_tmp,
  12829. + &resource->map_list,
  12830. + resource_map_list) {
  12831. + if (map->res_addr) {
  12832. + iounmap((void *)map->res_addr);
  12833. + map->res_addr = 0;
  12834. +
  12835. + vmcs_sm_remove_map(sm_state,
  12836. + map->resource,
  12837. + map);
  12838. + break;
  12839. + }
  12840. + }
  12841. + }
  12842. + }
  12843. + }
  12844. +
  12845. + if (change_cache)
  12846. + resource->res_cached = cache_type;
  12847. +
  12848. + if (resource->map_count) {
  12849. + ioparam->addr =
  12850. + vmcs_sm_usr_address_from_pid_and_usr_handle(
  12851. + current->tgid, ioparam->handle);
  12852. +
  12853. + pr_debug("[%s] map_count %d private->pid %d current->tgid %d hnd %x addr %u\n",
  12854. + __func__, resource->map_count, private->pid,
  12855. + current->tgid, ioparam->handle, ioparam->addr);
  12856. + } else {
  12857. + /* Kernel allocated resources.
  12858. + */
  12859. + if (resource->pid == 0) {
  12860. + pr_debug("[%s]: attempt mapping kernel resource - guid %x, hdl %x\n",
  12861. + __func__, ioparam->handle, lock.res_handle);
  12862. +
  12863. + ioparam->addr = 0;
  12864. +
  12865. + map = kzalloc(sizeof(*map), GFP_KERNEL);
  12866. + if (map == NULL) {
  12867. + pr_err("[%s]: failed allocating tracker\n",
  12868. + __func__);
  12869. + ret = -ENOMEM;
  12870. + goto error;
  12871. + } else {
  12872. + phys_addr = (uint32_t)resource->res_base_mem &
  12873. + 0x3FFFFFFF;
  12874. + phys_addr += mm_vc_mem_phys_addr;
  12875. + if (resource->res_cached
  12876. + == VMCS_SM_CACHE_HOST) {
  12877. + ioparam->addr = (long unsigned int)
  12878. + /* TODO - make cached work */
  12879. + ioremap_nocache(phys_addr,
  12880. + resource->res_size);
  12881. +
  12882. + pr_debug("[%s]: mapping kernel - guid %x, hdl %x - cached mapping %u\n",
  12883. + __func__, ioparam->handle,
  12884. + lock.res_handle, ioparam->addr);
  12885. + } else {
  12886. + ioparam->addr = (long unsigned int)
  12887. + ioremap_nocache(phys_addr,
  12888. + resource->res_size);
  12889. +
  12890. + pr_debug("[%s]: mapping kernel- guid %x, hdl %x - non cached mapping %u\n",
  12891. + __func__, ioparam->handle,
  12892. + lock.res_handle, ioparam->addr);
  12893. + }
  12894. +
  12895. + map->res_pid = 0;
  12896. + map->res_vc_hdl = resource->res_handle;
  12897. + map->res_usr_hdl = resource->res_guid;
  12898. + map->res_addr = ioparam->addr;
  12899. + map->resource = resource;
  12900. + map->vma = NULL;
  12901. +
  12902. + vmcs_sm_add_map(sm_state, resource, map);
  12903. + }
  12904. + } else
  12905. + ioparam->addr = 0;
  12906. + }
  12907. +
  12908. +error:
  12909. + if (resource)
  12910. + vmcs_sm_release_resource(resource, 0);
  12911. +
  12912. + return ret;
  12913. +}
  12914. +
  12915. +/* Unlock a previously allocated shared memory handle and block.
  12916. +*/
  12917. +static int vc_sm_ioctl_unlock(struct SM_PRIV_DATA_T *private,
  12918. + struct vmcs_sm_ioctl_lock_unlock *ioparam,
  12919. + int flush, int wait_reply, int no_vc_unlock)
  12920. +{
  12921. + int status;
  12922. + VC_SM_LOCK_UNLOCK_T unlock;
  12923. + struct sm_mmap *map, *map_tmp;
  12924. + struct SM_RESOURCE_T *resource;
  12925. + int ret = 0;
  12926. +
  12927. + map = NULL;
  12928. +
  12929. + /* Locate resource from GUID.
  12930. + */
  12931. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  12932. + if (resource == NULL) {
  12933. + ret = -EINVAL;
  12934. + goto error;
  12935. + }
  12936. +
  12937. + /* Check permissions.
  12938. + */
  12939. + if (resource->pid && (resource->pid != current->tgid)) {
  12940. + pr_err("[%s]: current tgid %u != %u owner\n",
  12941. + __func__, current->tgid, resource->pid);
  12942. + ret = -EPERM;
  12943. + goto error;
  12944. + }
  12945. +
  12946. + unlock.res_handle = resource->res_handle;
  12947. + unlock.res_mem = resource->res_base_mem;
  12948. +
  12949. + pr_debug("[%s]: attempt to unlock data - guid %x, hdl %x, base address %p\n",
  12950. + __func__, ioparam->handle, unlock.res_handle, unlock.res_mem);
  12951. +
  12952. + /* User space allocated resources.
  12953. + */
  12954. + if (resource->pid) {
  12955. + /* Flush if requested */
  12956. + if (resource->res_cached && flush) {
  12957. + dma_addr_t phys_addr = 0;
  12958. + resource->res_stats[FLUSH]++;
  12959. +
  12960. + phys_addr =
  12961. + (dma_addr_t)((uint32_t)resource->res_base_mem &
  12962. + 0x3FFFFFFF);
  12963. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  12964. +
  12965. + /* L1 cache flush */
  12966. + down_read(&current->mm->mmap_sem);
  12967. + list_for_each_entry(map, &resource->map_list,
  12968. + resource_map_list) {
  12969. + if (map->vma) {
  12970. + unsigned long start;
  12971. + unsigned long end;
  12972. + start = map->vma->vm_start;
  12973. + end = map->vma->vm_end;
  12974. +
  12975. + vcsm_vma_cache_clean_page_range(
  12976. + start, end);
  12977. + }
  12978. + }
  12979. + up_read(&current->mm->mmap_sem);
  12980. +
  12981. + /* L2 cache flush */
  12982. + outer_clean_range(phys_addr,
  12983. + phys_addr +
  12984. + (size_t) resource->res_size);
  12985. + }
  12986. +
  12987. + /* We need to zap all the vmas associated with this resource */
  12988. + if (resource->lock_count == 1) {
  12989. + down_read(&current->mm->mmap_sem);
  12990. + list_for_each_entry(map, &resource->map_list,
  12991. + resource_map_list) {
  12992. + if (map->vma) {
  12993. + zap_vma_ptes(map->vma,
  12994. + map->vma->vm_start,
  12995. + map->vma->vm_end -
  12996. + map->vma->vm_start);
  12997. + }
  12998. + }
  12999. + up_read(&current->mm->mmap_sem);
  13000. + }
  13001. + }
  13002. + /* Kernel allocated resources. */
  13003. + else {
  13004. + /* Global + Taken in this context */
  13005. + if (resource->ref_count == 2) {
  13006. + if (!list_empty(&resource->map_list)) {
  13007. + list_for_each_entry_safe(map, map_tmp,
  13008. + &resource->map_list,
  13009. + resource_map_list) {
  13010. + if (map->res_addr) {
  13011. + if (flush &&
  13012. + (resource->res_cached ==
  13013. + VMCS_SM_CACHE_HOST)) {
  13014. + long unsigned int
  13015. + phys_addr;
  13016. + phys_addr = (uint32_t)
  13017. + resource->res_base_mem & 0x3FFFFFFF;
  13018. + phys_addr +=
  13019. + mm_vc_mem_phys_addr;
  13020. +
  13021. + /* L1 cache flush */
  13022. + dmac_flush_range((const
  13023. + void
  13024. + *)
  13025. + map->res_addr, (const void *)
  13026. + (map->res_addr + resource->res_size));
  13027. +
  13028. + /* L2 cache flush */
  13029. + outer_clean_range
  13030. + (phys_addr,
  13031. + phys_addr +
  13032. + (size_t)
  13033. + resource->res_size);
  13034. + }
  13035. +
  13036. + iounmap((void *)map->res_addr);
  13037. + map->res_addr = 0;
  13038. +
  13039. + vmcs_sm_remove_map(sm_state,
  13040. + map->resource,
  13041. + map);
  13042. + break;
  13043. + }
  13044. + }
  13045. + }
  13046. + }
  13047. + }
  13048. +
  13049. + if (resource->lock_count) {
  13050. + /* Bypass the videocore unlock.
  13051. + */
  13052. + if (no_vc_unlock)
  13053. + status = 0;
  13054. + /* Unlock the videocore allocated resource.
  13055. + */
  13056. + else {
  13057. + status =
  13058. + vc_vchi_sm_unlock(sm_state->sm_handle, &unlock,
  13059. + &private->int_trans_id,
  13060. + wait_reply);
  13061. + if (status == -EINTR) {
  13062. + pr_debug("[%s]: requesting unlock memory action restart (trans_id: %u)\n",
  13063. + __func__, private->int_trans_id);
  13064. +
  13065. + ret = -ERESTARTSYS;
  13066. + resource->res_stats[UNLOCK]--;
  13067. + private->restart_sys = -EINTR;
  13068. + private->int_action = VC_SM_MSG_TYPE_UNLOCK;
  13069. + goto error;
  13070. + } else if (status != 0) {
  13071. + pr_err("[%s]: failed to unlock vc mem (status: %u, trans_id: %u)\n",
  13072. + __func__, status, private->int_trans_id);
  13073. +
  13074. + ret = -EPERM;
  13075. + resource->res_stats[UNLOCK_FAIL]++;
  13076. + goto error;
  13077. + }
  13078. + }
  13079. +
  13080. + resource->res_stats[UNLOCK]++;
  13081. + resource->lock_count--;
  13082. + }
  13083. +
  13084. + pr_debug("[%s]: success to unlock data - hdl %x, base address %p, ref-cnt %d\n",
  13085. + __func__, unlock.res_handle, unlock.res_mem,
  13086. + resource->lock_count);
  13087. +
  13088. +error:
  13089. + if (resource)
  13090. + vmcs_sm_release_resource(resource, 0);
  13091. +
  13092. + return ret;
  13093. +}
  13094. +
  13095. +/* Handle control from host. */
  13096. +static long vc_sm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  13097. +{
  13098. + int ret = 0;
  13099. + unsigned int cmdnr = _IOC_NR(cmd);
  13100. + struct SM_PRIV_DATA_T *file_data =
  13101. + (struct SM_PRIV_DATA_T *)file->private_data;
  13102. + struct SM_RESOURCE_T *resource = NULL;
  13103. +
  13104. + /* Validate we can work with this device. */
  13105. + if ((sm_state == NULL) || (file_data == NULL)) {
  13106. + pr_err("[%s]: invalid device\n", __func__);
  13107. + ret = -EPERM;
  13108. + goto out;
  13109. + }
  13110. +
  13111. + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
  13112. + current->tgid, file_data->pid);
  13113. +
  13114. + /* Action is a re-post of a previously interrupted action? */
  13115. + if (file_data->restart_sys == -EINTR) {
  13116. + VC_SM_ACTION_CLEAN_T action_clean;
  13117. +
  13118. + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
  13119. + __func__, file_data->int_action,
  13120. + file_data->int_trans_id);
  13121. +
  13122. + action_clean.res_action = file_data->int_action;
  13123. + action_clean.action_trans_id = file_data->int_trans_id;
  13124. +
  13125. + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
  13126. +
  13127. + file_data->restart_sys = 0;
  13128. + }
  13129. +
  13130. + /* Now process the command.
  13131. + */
  13132. + switch (cmdnr) {
  13133. + /* New memory allocation.
  13134. + */
  13135. + case VMCS_SM_CMD_ALLOC:
  13136. + {
  13137. + struct vmcs_sm_ioctl_alloc ioparam;
  13138. +
  13139. + /* Get the parameter data.
  13140. + */
  13141. + if (copy_from_user
  13142. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13143. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13144. + __func__, cmdnr);
  13145. + ret = -EFAULT;
  13146. + goto out;
  13147. + }
  13148. +
  13149. + ret = vc_sm_ioctl_alloc(file_data, &ioparam);
  13150. + if (!ret &&
  13151. + (copy_to_user((void *)arg,
  13152. + &ioparam, sizeof(ioparam)) != 0)) {
  13153. + struct vmcs_sm_ioctl_free freeparam = {
  13154. + ioparam.handle
  13155. + };
  13156. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13157. + __func__, cmdnr);
  13158. + vc_sm_ioctl_free(file_data, &freeparam);
  13159. + ret = -EFAULT;
  13160. + }
  13161. +
  13162. + /* Done.
  13163. + */
  13164. + goto out;
  13165. + }
  13166. + break;
  13167. +
  13168. + /* Share existing memory allocation.
  13169. + */
  13170. + case VMCS_SM_CMD_ALLOC_SHARE:
  13171. + {
  13172. + struct vmcs_sm_ioctl_alloc_share ioparam;
  13173. +
  13174. + /* Get the parameter data.
  13175. + */
  13176. + if (copy_from_user
  13177. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13178. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13179. + __func__, cmdnr);
  13180. + ret = -EFAULT;
  13181. + goto out;
  13182. + }
  13183. +
  13184. + ret = vc_sm_ioctl_alloc_share(file_data, &ioparam);
  13185. +
  13186. + /* Copy result back to user.
  13187. + */
  13188. + if (!ret
  13189. + && copy_to_user((void *)arg, &ioparam,
  13190. + sizeof(ioparam)) != 0) {
  13191. + struct vmcs_sm_ioctl_free freeparam = {
  13192. + ioparam.handle
  13193. + };
  13194. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13195. + __func__, cmdnr);
  13196. + vc_sm_ioctl_free(file_data, &freeparam);
  13197. + ret = -EFAULT;
  13198. + }
  13199. +
  13200. + /* Done.
  13201. + */
  13202. + goto out;
  13203. + }
  13204. + break;
  13205. +
  13206. + /* Lock (attempt to) *and* register a cache behavior change.
  13207. + */
  13208. + case VMCS_SM_CMD_LOCK_CACHE:
  13209. + {
  13210. + struct vmcs_sm_ioctl_lock_cache ioparam;
  13211. + struct vmcs_sm_ioctl_lock_unlock lock;
  13212. +
  13213. + /* Get parameter data.
  13214. + */
  13215. + if (copy_from_user
  13216. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13217. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13218. + __func__, cmdnr);
  13219. + ret = -EFAULT;
  13220. + goto out;
  13221. + }
  13222. +
  13223. + lock.handle = ioparam.handle;
  13224. + ret =
  13225. + vc_sm_ioctl_lock(file_data, &lock, 1,
  13226. + ioparam.cached, 0);
  13227. +
  13228. + /* Done.
  13229. + */
  13230. + goto out;
  13231. + }
  13232. + break;
  13233. +
  13234. + /* Lock (attempt to) existing memory allocation.
  13235. + */
  13236. + case VMCS_SM_CMD_LOCK:
  13237. + {
  13238. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13239. +
  13240. + /* Get parameter data.
  13241. + */
  13242. + if (copy_from_user
  13243. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13244. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13245. + __func__, cmdnr);
  13246. + ret = -EFAULT;
  13247. + goto out;
  13248. + }
  13249. +
  13250. + ret = vc_sm_ioctl_lock(file_data, &ioparam, 0, 0, 0);
  13251. +
  13252. + /* Copy result back to user.
  13253. + */
  13254. + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
  13255. + != 0) {
  13256. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13257. + __func__, cmdnr);
  13258. + ret = -EFAULT;
  13259. + }
  13260. +
  13261. + /* Done.
  13262. + */
  13263. + goto out;
  13264. + }
  13265. + break;
  13266. +
  13267. + /* Unlock (attempt to) existing memory allocation.
  13268. + */
  13269. + case VMCS_SM_CMD_UNLOCK:
  13270. + {
  13271. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13272. +
  13273. + /* Get parameter data.
  13274. + */
  13275. + if (copy_from_user
  13276. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13277. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13278. + __func__, cmdnr);
  13279. + ret = -EFAULT;
  13280. + goto out;
  13281. + }
  13282. +
  13283. + ret = vc_sm_ioctl_unlock(file_data, &ioparam, 0, 1, 0);
  13284. +
  13285. + /* Done.
  13286. + */
  13287. + goto out;
  13288. + }
  13289. + break;
  13290. +
  13291. + /* Resize (attempt to) existing memory allocation.
  13292. + */
  13293. + case VMCS_SM_CMD_RESIZE:
  13294. + {
  13295. + struct vmcs_sm_ioctl_resize ioparam;
  13296. +
  13297. + /* Get parameter data.
  13298. + */
  13299. + if (copy_from_user
  13300. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13301. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13302. + __func__, cmdnr);
  13303. + ret = -EFAULT;
  13304. + goto out;
  13305. + }
  13306. +
  13307. + ret = vc_sm_ioctl_resize(file_data, &ioparam);
  13308. +
  13309. + /* Copy result back to user.
  13310. + */
  13311. + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
  13312. + != 0) {
  13313. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13314. + __func__, cmdnr);
  13315. + ret = -EFAULT;
  13316. + }
  13317. +
  13318. + /* Done.
  13319. + */
  13320. + goto out;
  13321. + }
  13322. + break;
  13323. +
  13324. + /* Terminate existing memory allocation.
  13325. + */
  13326. + case VMCS_SM_CMD_FREE:
  13327. + {
  13328. + struct vmcs_sm_ioctl_free ioparam;
  13329. +
  13330. + /* Get parameter data.
  13331. + */
  13332. + if (copy_from_user
  13333. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13334. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13335. + __func__, cmdnr);
  13336. + ret = -EFAULT;
  13337. + goto out;
  13338. + }
  13339. +
  13340. + ret = vc_sm_ioctl_free(file_data, &ioparam);
  13341. +
  13342. + /* Done.
  13343. + */
  13344. + goto out;
  13345. + }
  13346. + break;
  13347. +
  13348. + /* Walk allocation on videocore, information shows up in the
  13349. + ** videocore log.
  13350. + */
  13351. + case VMCS_SM_CMD_VC_WALK_ALLOC:
  13352. + {
  13353. + pr_debug("[%s]: invoking walk alloc\n", __func__);
  13354. +
  13355. + if (vc_vchi_sm_walk_alloc(sm_state->sm_handle) != 0)
  13356. + pr_err("[%s]: failed to walk-alloc on videocore\n",
  13357. + __func__);
  13358. +
  13359. + /* Done.
  13360. + */
  13361. + goto out;
  13362. + }
  13363. + break;
  13364. +/* Walk mapping table on host, information shows up in the
  13365. + ** kernel log.
  13366. + */
  13367. + case VMCS_SM_CMD_HOST_WALK_MAP:
  13368. + {
  13369. + /* Use pid of -1 to tell to walk the whole map. */
  13370. + vmcs_sm_host_walk_map_per_pid(-1);
  13371. +
  13372. + /* Done. */
  13373. + goto out;
  13374. + }
  13375. + break;
  13376. +
  13377. + /* Walk mapping table per process on host. */
  13378. + case VMCS_SM_CMD_HOST_WALK_PID_ALLOC:
  13379. + {
  13380. + struct vmcs_sm_ioctl_walk ioparam;
  13381. +
  13382. + /* Get parameter data. */
  13383. + if (copy_from_user(&ioparam,
  13384. + (void *)arg, sizeof(ioparam)) != 0) {
  13385. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13386. + __func__, cmdnr);
  13387. + ret = -EFAULT;
  13388. + goto out;
  13389. + }
  13390. +
  13391. + vmcs_sm_host_walk_alloc(file_data);
  13392. +
  13393. + /* Done. */
  13394. + goto out;
  13395. + }
  13396. + break;
  13397. +
  13398. + /* Walk allocation per process on host. */
  13399. + case VMCS_SM_CMD_HOST_WALK_PID_MAP:
  13400. + {
  13401. + struct vmcs_sm_ioctl_walk ioparam;
  13402. +
  13403. + /* Get parameter data. */
  13404. + if (copy_from_user(&ioparam,
  13405. + (void *)arg, sizeof(ioparam)) != 0) {
  13406. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13407. + __func__, cmdnr);
  13408. + ret = -EFAULT;
  13409. + goto out;
  13410. + }
  13411. +
  13412. + vmcs_sm_host_walk_map_per_pid(ioparam.pid);
  13413. +
  13414. + /* Done. */
  13415. + goto out;
  13416. + }
  13417. + break;
  13418. +
  13419. + /* Gets the size of the memory associated with a user handle. */
  13420. + case VMCS_SM_CMD_SIZE_USR_HANDLE:
  13421. + {
  13422. + struct vmcs_sm_ioctl_size ioparam;
  13423. +
  13424. + /* Get parameter data. */
  13425. + if (copy_from_user(&ioparam,
  13426. + (void *)arg, sizeof(ioparam)) != 0) {
  13427. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13428. + __func__, cmdnr);
  13429. + ret = -EFAULT;
  13430. + goto out;
  13431. + }
  13432. +
  13433. + /* Locate resource from GUID. */
  13434. + resource =
  13435. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13436. + if (resource != NULL) {
  13437. + ioparam.size = resource->res_size;
  13438. + vmcs_sm_release_resource(resource, 0);
  13439. + } else {
  13440. + ioparam.size = 0;
  13441. + }
  13442. +
  13443. + if (copy_to_user((void *)arg,
  13444. + &ioparam, sizeof(ioparam)) != 0) {
  13445. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13446. + __func__, cmdnr);
  13447. + ret = -EFAULT;
  13448. + }
  13449. +
  13450. + /* Done. */
  13451. + goto out;
  13452. + }
  13453. + break;
  13454. +
  13455. + /* Verify we are dealing with a valid resource. */
  13456. + case VMCS_SM_CMD_CHK_USR_HANDLE:
  13457. + {
  13458. + struct vmcs_sm_ioctl_chk ioparam;
  13459. +
  13460. + /* Get parameter data.
  13461. + */
  13462. + if (copy_from_user(&ioparam,
  13463. + (void *)arg, sizeof(ioparam)) != 0) {
  13464. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13465. + __func__, cmdnr);
  13466. +
  13467. + ret = -EFAULT;
  13468. + goto out;
  13469. + }
  13470. +
  13471. + /* Locate resource from GUID. */
  13472. + resource =
  13473. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13474. + if (resource == NULL)
  13475. + ret = -EINVAL;
  13476. + /* If the resource is cacheable, return additional
  13477. + * information that may be needed to flush the cache.
  13478. + */
  13479. + else if ((resource->res_cached == VMCS_SM_CACHE_HOST) ||
  13480. + (resource->res_cached == VMCS_SM_CACHE_BOTH)) {
  13481. + ioparam.addr =
  13482. + vmcs_sm_usr_address_from_pid_and_usr_handle
  13483. + (current->tgid, ioparam.handle);
  13484. + ioparam.size = resource->res_size;
  13485. + ioparam.cache = resource->res_cached;
  13486. + } else {
  13487. + ioparam.addr = 0;
  13488. + ioparam.size = 0;
  13489. + ioparam.cache = resource->res_cached;
  13490. + }
  13491. +
  13492. + if (resource)
  13493. + vmcs_sm_release_resource(resource, 0);
  13494. +
  13495. + if (copy_to_user((void *)arg,
  13496. + &ioparam, sizeof(ioparam)) != 0) {
  13497. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13498. + __func__, cmdnr);
  13499. + ret = -EFAULT;
  13500. + }
  13501. +
  13502. + /* Done.
  13503. + */
  13504. + goto out;
  13505. + }
  13506. + break;
  13507. +
  13508. + /*
  13509. + * Maps a user handle given the process and the virtual address.
  13510. + */
  13511. + case VMCS_SM_CMD_MAPPED_USR_HANDLE:
  13512. + {
  13513. + struct vmcs_sm_ioctl_map ioparam;
  13514. +
  13515. + /* Get parameter data. */
  13516. + if (copy_from_user(&ioparam,
  13517. + (void *)arg, sizeof(ioparam)) != 0) {
  13518. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13519. + __func__, cmdnr);
  13520. +
  13521. + ret = -EFAULT;
  13522. + goto out;
  13523. + }
  13524. +
  13525. + ioparam.handle =
  13526. + vmcs_sm_usr_handle_from_pid_and_address(
  13527. + ioparam.pid, ioparam.addr);
  13528. +
  13529. + resource =
  13530. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13531. + if ((resource != NULL)
  13532. + && ((resource->res_cached == VMCS_SM_CACHE_HOST)
  13533. + || (resource->res_cached ==
  13534. + VMCS_SM_CACHE_BOTH))) {
  13535. + ioparam.size = resource->res_size;
  13536. + } else {
  13537. + ioparam.size = 0;
  13538. + }
  13539. +
  13540. + if (resource)
  13541. + vmcs_sm_release_resource(resource, 0);
  13542. +
  13543. + if (copy_to_user((void *)arg,
  13544. + &ioparam, sizeof(ioparam)) != 0) {
  13545. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13546. + __func__, cmdnr);
  13547. + ret = -EFAULT;
  13548. + }
  13549. +
  13550. + /* Done. */
  13551. + goto out;
  13552. + }
  13553. + break;
  13554. +
  13555. + /*
  13556. + * Maps a videocore handle given process and virtual address.
  13557. + */
  13558. + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR:
  13559. + {
  13560. + struct vmcs_sm_ioctl_map ioparam;
  13561. +
  13562. + /* Get parameter data. */
  13563. + if (copy_from_user(&ioparam,
  13564. + (void *)arg, sizeof(ioparam)) != 0) {
  13565. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13566. + __func__, cmdnr);
  13567. + ret = -EFAULT;
  13568. + goto out;
  13569. + }
  13570. +
  13571. + ioparam.handle = vmcs_sm_vc_handle_from_pid_and_address(
  13572. + ioparam.pid, ioparam.addr);
  13573. +
  13574. + if (copy_to_user((void *)arg,
  13575. + &ioparam, sizeof(ioparam)) != 0) {
  13576. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13577. + __func__, cmdnr);
  13578. +
  13579. + ret = -EFAULT;
  13580. + }
  13581. +
  13582. + /* Done.
  13583. + */
  13584. + goto out;
  13585. + }
  13586. + break;
  13587. +
  13588. + /* Maps a videocore handle given process and user handle. */
  13589. + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL:
  13590. + {
  13591. + struct vmcs_sm_ioctl_map ioparam;
  13592. +
  13593. + /* Get parameter data. */
  13594. + if (copy_from_user(&ioparam,
  13595. + (void *)arg, sizeof(ioparam)) != 0) {
  13596. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13597. + __func__, cmdnr);
  13598. + ret = -EFAULT;
  13599. + goto out;
  13600. + }
  13601. +
  13602. + /* Locate resource from GUID. */
  13603. + resource =
  13604. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13605. + if (resource != NULL) {
  13606. + ioparam.handle = resource->res_handle;
  13607. + vmcs_sm_release_resource(resource, 0);
  13608. + } else {
  13609. + ioparam.handle = 0;
  13610. + }
  13611. +
  13612. + if (copy_to_user((void *)arg,
  13613. + &ioparam, sizeof(ioparam)) != 0) {
  13614. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13615. + __func__, cmdnr);
  13616. +
  13617. + ret = -EFAULT;
  13618. + }
  13619. +
  13620. + /* Done. */
  13621. + goto out;
  13622. + }
  13623. + break;
  13624. +
  13625. + /*
  13626. + * Maps a videocore address given process and videocore handle.
  13627. + */
  13628. + case VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL:
  13629. + {
  13630. + struct vmcs_sm_ioctl_map ioparam;
  13631. +
  13632. + /* Get parameter data. */
  13633. + if (copy_from_user(&ioparam,
  13634. + (void *)arg, sizeof(ioparam)) != 0) {
  13635. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13636. + __func__, cmdnr);
  13637. +
  13638. + ret = -EFAULT;
  13639. + goto out;
  13640. + }
  13641. +
  13642. + /* Locate resource from GUID. */
  13643. + resource =
  13644. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13645. + if (resource != NULL) {
  13646. + ioparam.addr =
  13647. + (unsigned int)resource->res_base_mem;
  13648. + vmcs_sm_release_resource(resource, 0);
  13649. + } else {
  13650. + ioparam.addr = 0;
  13651. + }
  13652. +
  13653. + if (copy_to_user((void *)arg,
  13654. + &ioparam, sizeof(ioparam)) != 0) {
  13655. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13656. + __func__, cmdnr);
  13657. + ret = -EFAULT;
  13658. + }
  13659. +
  13660. + /* Done. */
  13661. + goto out;
  13662. + }
  13663. + break;
  13664. +
  13665. + /* Maps a user address given process and vc handle.
  13666. + */
  13667. + case VMCS_SM_CMD_MAPPED_USR_ADDRESS:
  13668. + {
  13669. + struct vmcs_sm_ioctl_map ioparam;
  13670. +
  13671. + /* Get parameter data. */
  13672. + if (copy_from_user(&ioparam,
  13673. + (void *)arg, sizeof(ioparam)) != 0) {
  13674. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13675. + __func__, cmdnr);
  13676. + ret = -EFAULT;
  13677. + goto out;
  13678. + }
  13679. +
  13680. + /*
  13681. + * Return the address information from the mapping,
  13682. + * 0 (ie NULL) if it cannot locate the actual mapping.
  13683. + */
  13684. + ioparam.addr =
  13685. + vmcs_sm_usr_address_from_pid_and_usr_handle
  13686. + (ioparam.pid, ioparam.handle);
  13687. +
  13688. + if (copy_to_user((void *)arg,
  13689. + &ioparam, sizeof(ioparam)) != 0) {
  13690. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13691. + __func__, cmdnr);
  13692. + ret = -EFAULT;
  13693. + }
  13694. +
  13695. + /* Done. */
  13696. + goto out;
  13697. + }
  13698. + break;
  13699. +
  13700. + /* Flush the cache for a given mapping. */
  13701. + case VMCS_SM_CMD_FLUSH:
  13702. + {
  13703. + struct vmcs_sm_ioctl_cache ioparam;
  13704. +
  13705. + /* Get parameter data. */
  13706. + if (copy_from_user(&ioparam,
  13707. + (void *)arg, sizeof(ioparam)) != 0) {
  13708. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13709. + __func__, cmdnr);
  13710. + ret = -EFAULT;
  13711. + goto out;
  13712. + }
  13713. +
  13714. + /* Locate resource from GUID. */
  13715. + resource =
  13716. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13717. +
  13718. + if ((resource != NULL) && resource->res_cached) {
  13719. + dma_addr_t phys_addr = 0;
  13720. +
  13721. + resource->res_stats[FLUSH]++;
  13722. +
  13723. + phys_addr =
  13724. + (dma_addr_t)((uint32_t)
  13725. + resource->res_base_mem &
  13726. + 0x3FFFFFFF);
  13727. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  13728. +
  13729. + /* L1 cache flush */
  13730. + down_read(&current->mm->mmap_sem);
  13731. + vcsm_vma_cache_clean_page_range((unsigned long)
  13732. + ioparam.addr,
  13733. + (unsigned long)
  13734. + ioparam.addr +
  13735. + ioparam.size);
  13736. + up_read(&current->mm->mmap_sem);
  13737. +
  13738. + /* L2 cache flush */
  13739. + outer_clean_range(phys_addr,
  13740. + phys_addr +
  13741. + (size_t) ioparam.size);
  13742. + } else if (resource == NULL) {
  13743. + ret = -EINVAL;
  13744. + goto out;
  13745. + }
  13746. +
  13747. + if (resource)
  13748. + vmcs_sm_release_resource(resource, 0);
  13749. +
  13750. + /* Done. */
  13751. + goto out;
  13752. + }
  13753. + break;
  13754. +
  13755. + /* Invalidate the cache for a given mapping. */
  13756. + case VMCS_SM_CMD_INVALID:
  13757. + {
  13758. + struct vmcs_sm_ioctl_cache ioparam;
  13759. +
  13760. + /* Get parameter data. */
  13761. + if (copy_from_user(&ioparam,
  13762. + (void *)arg, sizeof(ioparam)) != 0) {
  13763. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13764. + __func__, cmdnr);
  13765. + ret = -EFAULT;
  13766. + goto out;
  13767. + }
  13768. +
  13769. + /* Locate resource from GUID.
  13770. + */
  13771. + resource =
  13772. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13773. +
  13774. + if ((resource != NULL) && resource->res_cached) {
  13775. + dma_addr_t phys_addr = 0;
  13776. +
  13777. + resource->res_stats[INVALID]++;
  13778. +
  13779. + phys_addr =
  13780. + (dma_addr_t)((uint32_t)
  13781. + resource->res_base_mem &
  13782. + 0x3FFFFFFF);
  13783. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  13784. +
  13785. + /* L2 cache invalidate */
  13786. + outer_inv_range(phys_addr,
  13787. + phys_addr +
  13788. + (size_t) ioparam.size);
  13789. +
  13790. + /* L1 cache invalidate */
  13791. + down_read(&current->mm->mmap_sem);
  13792. + vcsm_vma_cache_clean_page_range((unsigned long)
  13793. + ioparam.addr,
  13794. + (unsigned long)
  13795. + ioparam.addr +
  13796. + ioparam.size);
  13797. + up_read(&current->mm->mmap_sem);
  13798. + } else if (resource == NULL) {
  13799. + ret = -EINVAL;
  13800. + goto out;
  13801. + }
  13802. +
  13803. + if (resource)
  13804. + vmcs_sm_release_resource(resource, 0);
  13805. +
  13806. + /* Done.
  13807. + */
  13808. + goto out;
  13809. + }
  13810. + break;
  13811. +
  13812. + default:
  13813. + {
  13814. + ret = -EINVAL;
  13815. + goto out;
  13816. + }
  13817. + break;
  13818. + }
  13819. +
  13820. +out:
  13821. + return ret;
  13822. +}
  13823. +
  13824. +/* Device operations that we managed in this driver.
  13825. +*/
  13826. +static const struct file_operations vmcs_sm_ops = {
  13827. + .owner = THIS_MODULE,
  13828. + .unlocked_ioctl = vc_sm_ioctl,
  13829. + .open = vc_sm_open,
  13830. + .release = vc_sm_release,
  13831. + .mmap = vc_sm_mmap,
  13832. +};
  13833. +
  13834. +/* Creation of device.
  13835. +*/
  13836. +static int vc_sm_create_sharedmemory(void)
  13837. +{
  13838. + int ret;
  13839. +
  13840. + if (sm_state == NULL) {
  13841. + ret = -ENOMEM;
  13842. + goto out;
  13843. + }
  13844. +
  13845. + /* Create a device class for creating dev nodes.
  13846. + */
  13847. + sm_state->sm_class = class_create(THIS_MODULE, "vc-sm");
  13848. + if (IS_ERR(sm_state->sm_class)) {
  13849. + pr_err("[%s]: unable to create device class\n", __func__);
  13850. + ret = PTR_ERR(sm_state->sm_class);
  13851. + goto out;
  13852. + }
  13853. +
  13854. + /* Create a character driver.
  13855. + */
  13856. + ret = alloc_chrdev_region(&sm_state->sm_devid,
  13857. + DEVICE_MINOR, 1, DEVICE_NAME);
  13858. + if (ret != 0) {
  13859. + pr_err("[%s]: unable to allocate device number\n", __func__);
  13860. + goto out_dev_class_destroy;
  13861. + }
  13862. +
  13863. + cdev_init(&sm_state->sm_cdev, &vmcs_sm_ops);
  13864. + ret = cdev_add(&sm_state->sm_cdev, sm_state->sm_devid, 1);
  13865. + if (ret != 0) {
  13866. + pr_err("[%s]: unable to register device\n", __func__);
  13867. + goto out_chrdev_unreg;
  13868. + }
  13869. +
  13870. + /* Create a device node.
  13871. + */
  13872. + sm_state->sm_dev = device_create(sm_state->sm_class,
  13873. + NULL,
  13874. + MKDEV(MAJOR(sm_state->sm_devid),
  13875. + DEVICE_MINOR), NULL,
  13876. + DEVICE_NAME);
  13877. + if (IS_ERR(sm_state->sm_dev)) {
  13878. + pr_err("[%s]: unable to create device node\n", __func__);
  13879. + ret = PTR_ERR(sm_state->sm_dev);
  13880. + goto out_chrdev_del;
  13881. + }
  13882. +
  13883. + goto out;
  13884. +
  13885. +out_chrdev_del:
  13886. + cdev_del(&sm_state->sm_cdev);
  13887. +out_chrdev_unreg:
  13888. + unregister_chrdev_region(sm_state->sm_devid, 1);
  13889. +out_dev_class_destroy:
  13890. + class_destroy(sm_state->sm_class);
  13891. + sm_state->sm_class = NULL;
  13892. +out:
  13893. + return ret;
  13894. +}
  13895. +
  13896. +/* Termination of the device.
  13897. +*/
  13898. +static int vc_sm_remove_sharedmemory(void)
  13899. +{
  13900. + int ret;
  13901. +
  13902. + if (sm_state == NULL) {
  13903. + /* Nothing to do.
  13904. + */
  13905. + ret = 0;
  13906. + goto out;
  13907. + }
  13908. +
  13909. + /* Remove the sharedmemory character driver.
  13910. + */
  13911. + cdev_del(&sm_state->sm_cdev);
  13912. +
  13913. + /* Unregister region.
  13914. + */
  13915. + unregister_chrdev_region(sm_state->sm_devid, 1);
  13916. +
  13917. + ret = 0;
  13918. + goto out;
  13919. +
  13920. +out:
  13921. + return ret;
  13922. +}
  13923. +
  13924. +/* Videocore connected. */
  13925. +static void vc_sm_connected_init(void)
  13926. +{
  13927. + int ret;
  13928. + VCHI_INSTANCE_T vchi_instance;
  13929. + VCHI_CONNECTION_T *vchi_connection = NULL;
  13930. +
  13931. + pr_info("[%s]: start\n", __func__);
  13932. +
  13933. + /* Allocate memory for the state structure.
  13934. + */
  13935. + sm_state = kzalloc(sizeof(struct SM_STATE_T), GFP_KERNEL);
  13936. + if (sm_state == NULL) {
  13937. + pr_err("[%s]: failed to allocate memory\n", __func__);
  13938. + ret = -ENOMEM;
  13939. + goto out;
  13940. + }
  13941. +
  13942. + mutex_init(&sm_state->lock);
  13943. + mutex_init(&sm_state->map_lock);
  13944. +
  13945. + /* Initialize and create a VCHI connection for the shared memory service
  13946. + ** running on videocore.
  13947. + */
  13948. + ret = vchi_initialise(&vchi_instance);
  13949. + if (ret != 0) {
  13950. + pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
  13951. + __func__, ret);
  13952. +
  13953. + ret = -EIO;
  13954. + goto err_free_mem;
  13955. + }
  13956. +
  13957. + ret = vchi_connect(NULL, 0, vchi_instance);
  13958. + if (ret != 0) {
  13959. + pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
  13960. + __func__, ret);
  13961. +
  13962. + ret = -EIO;
  13963. + goto err_free_mem;
  13964. + }
  13965. +
  13966. + /* Initialize an instance of the shared memory service. */
  13967. + sm_state->sm_handle =
  13968. + vc_vchi_sm_init(vchi_instance, &vchi_connection, 1);
  13969. + if (sm_state->sm_handle == NULL) {
  13970. + pr_err("[%s]: failed to initialize shared memory service\n",
  13971. + __func__);
  13972. +
  13973. + ret = -EPERM;
  13974. + goto err_free_mem;
  13975. + }
  13976. +
  13977. + /* Create a debug fs directory entry (root). */
  13978. + sm_state->dir_root = debugfs_create_dir(VC_SM_DIR_ROOT_NAME, NULL);
  13979. + if (!sm_state->dir_root) {
  13980. + pr_err("[%s]: failed to create \'%s\' directory entry\n",
  13981. + __func__, VC_SM_DIR_ROOT_NAME);
  13982. +
  13983. + ret = -EPERM;
  13984. + goto err_stop_sm_service;
  13985. + }
  13986. +
  13987. + sm_state->dir_state.show = &vc_sm_global_state_show;
  13988. + sm_state->dir_state.dir_entry = debugfs_create_file(VC_SM_STATE,
  13989. + S_IRUGO, sm_state->dir_root, &sm_state->dir_state,
  13990. + &vc_sm_debug_fs_fops);
  13991. +
  13992. + sm_state->dir_stats.show = &vc_sm_global_statistics_show;
  13993. + sm_state->dir_stats.dir_entry = debugfs_create_file(VC_SM_STATS,
  13994. + S_IRUGO, sm_state->dir_root, &sm_state->dir_stats,
  13995. + &vc_sm_debug_fs_fops);
  13996. +
  13997. + /* Create the proc entry children. */
  13998. + sm_state->dir_alloc = debugfs_create_dir(VC_SM_DIR_ALLOC_NAME,
  13999. + sm_state->dir_root);
  14000. +
  14001. + /* Create a shared memory device. */
  14002. + ret = vc_sm_create_sharedmemory();
  14003. + if (ret != 0) {
  14004. + pr_err("[%s]: failed to create shared memory device\n",
  14005. + __func__);
  14006. + goto err_remove_debugfs;
  14007. + }
  14008. +
  14009. + INIT_LIST_HEAD(&sm_state->map_list);
  14010. + INIT_LIST_HEAD(&sm_state->resource_list);
  14011. +
  14012. + sm_state->data_knl = vc_sm_create_priv_data(0);
  14013. + if (sm_state->data_knl == NULL) {
  14014. + pr_err("[%s]: failed to create kernel private data tracker\n",
  14015. + __func__);
  14016. + goto err_remove_shared_memory;
  14017. + }
  14018. +
  14019. + /* Done!
  14020. + */
  14021. + sm_inited = 1;
  14022. + goto out;
  14023. +
  14024. +err_remove_shared_memory:
  14025. + vc_sm_remove_sharedmemory();
  14026. +err_remove_debugfs:
  14027. + debugfs_remove_recursive(sm_state->dir_root);
  14028. +err_stop_sm_service:
  14029. + vc_vchi_sm_stop(&sm_state->sm_handle);
  14030. +err_free_mem:
  14031. + kfree(sm_state);
  14032. +out:
  14033. + pr_info("[%s]: end - returning %d\n", __func__, ret);
  14034. +}
  14035. +
  14036. +/* Driver loading. */
  14037. +static int __init vc_sm_init(void)
  14038. +{
  14039. + pr_info("vc-sm: Videocore shared memory driver\n");
  14040. + vchiq_add_connected_callback(vc_sm_connected_init);
  14041. + return 0;
  14042. +}
  14043. +
  14044. +/* Driver unloading. */
  14045. +static void __exit vc_sm_exit(void)
  14046. +{
  14047. + pr_debug("[%s]: start\n", __func__);
  14048. + if (sm_inited) {
  14049. + /* Remove shared memory device.
  14050. + */
  14051. + vc_sm_remove_sharedmemory();
  14052. +
  14053. + /* Remove all proc entries.
  14054. + */
  14055. + debugfs_remove_recursive(sm_state->dir_root);
  14056. +
  14057. + /* Stop the videocore shared memory service.
  14058. + */
  14059. + vc_vchi_sm_stop(&sm_state->sm_handle);
  14060. +
  14061. + /* Free the memory for the state structure.
  14062. + */
  14063. + mutex_destroy(&(sm_state->map_lock));
  14064. + kfree(sm_state);
  14065. + }
  14066. +
  14067. + pr_debug("[%s]: end\n", __func__);
  14068. +}
  14069. +
  14070. +#if defined(__KERNEL__)
  14071. +/* Allocate a shared memory handle and block. */
  14072. +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle)
  14073. +{
  14074. + struct vmcs_sm_ioctl_alloc ioparam = { 0 };
  14075. + int ret;
  14076. + struct SM_RESOURCE_T *resource;
  14077. +
  14078. + /* Validate we can work with this device.
  14079. + */
  14080. + if (sm_state == NULL || alloc == NULL || handle == NULL) {
  14081. + pr_err("[%s]: invalid input\n", __func__);
  14082. + return -EPERM;
  14083. + }
  14084. +
  14085. + ioparam.size = alloc->base_unit;
  14086. + ioparam.num = alloc->num_unit;
  14087. + ioparam.cached =
  14088. + alloc->type == VC_SM_ALLOC_CACHED ? VMCS_SM_CACHE_VC : 0;
  14089. +
  14090. + ret = vc_sm_ioctl_alloc(sm_state->data_knl, &ioparam);
  14091. +
  14092. + if (ret == 0) {
  14093. + resource =
  14094. + vmcs_sm_acquire_resource(sm_state->data_knl,
  14095. + ioparam.handle);
  14096. + if (resource) {
  14097. + resource->pid = 0;
  14098. + vmcs_sm_release_resource(resource, 0);
  14099. +
  14100. + /* Assign valid handle at this time.
  14101. + */
  14102. + *handle = ioparam.handle;
  14103. + } else {
  14104. + ret = -ENOMEM;
  14105. + }
  14106. + }
  14107. +
  14108. + return ret;
  14109. +}
  14110. +EXPORT_SYMBOL_GPL(vc_sm_alloc);
  14111. +
  14112. +/* Get an internal resource handle mapped from the external one.
  14113. +*/
  14114. +int vc_sm_int_handle(int handle)
  14115. +{
  14116. + struct SM_RESOURCE_T *resource;
  14117. + int ret = 0;
  14118. +
  14119. + /* Validate we can work with this device.
  14120. + */
  14121. + if (sm_state == NULL || handle == 0) {
  14122. + pr_err("[%s]: invalid input\n", __func__);
  14123. + return 0;
  14124. + }
  14125. +
  14126. + /* Locate resource from GUID.
  14127. + */
  14128. + resource = vmcs_sm_acquire_resource(sm_state->data_knl, handle);
  14129. + if (resource) {
  14130. + ret = resource->res_handle;
  14131. + vmcs_sm_release_resource(resource, 0);
  14132. + }
  14133. +
  14134. + return ret;
  14135. +}
  14136. +EXPORT_SYMBOL_GPL(vc_sm_int_handle);
  14137. +
  14138. +/* Free a previously allocated shared memory handle and block.
  14139. +*/
  14140. +int vc_sm_free(int handle)
  14141. +{
  14142. + struct vmcs_sm_ioctl_free ioparam = { handle };
  14143. +
  14144. + /* Validate we can work with this device.
  14145. + */
  14146. + if (sm_state == NULL || handle == 0) {
  14147. + pr_err("[%s]: invalid input\n", __func__);
  14148. + return -EPERM;
  14149. + }
  14150. +
  14151. + return vc_sm_ioctl_free(sm_state->data_knl, &ioparam);
  14152. +}
  14153. +EXPORT_SYMBOL_GPL(vc_sm_free);
  14154. +
  14155. +/* Lock a memory handle for use by kernel.
  14156. +*/
  14157. +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
  14158. + long unsigned int *data)
  14159. +{
  14160. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  14161. + int ret;
  14162. +
  14163. + /* Validate we can work with this device.
  14164. + */
  14165. + if (sm_state == NULL || handle == 0 || data == NULL) {
  14166. + pr_err("[%s]: invalid input\n", __func__);
  14167. + return -EPERM;
  14168. + }
  14169. +
  14170. + *data = 0;
  14171. +
  14172. + ioparam.handle = handle;
  14173. + ret = vc_sm_ioctl_lock(sm_state->data_knl,
  14174. + &ioparam,
  14175. + 1,
  14176. + ((mode ==
  14177. + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
  14178. + VMCS_SM_CACHE_NONE), 0);
  14179. +
  14180. + *data = ioparam.addr;
  14181. + return ret;
  14182. +}
  14183. +EXPORT_SYMBOL_GPL(vc_sm_lock);
  14184. +
  14185. +/* Unlock a memory handle in use by kernel.
  14186. +*/
  14187. +int vc_sm_unlock(int handle, int flush, int no_vc_unlock)
  14188. +{
  14189. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  14190. +
  14191. + /* Validate we can work with this device.
  14192. + */
  14193. + if (sm_state == NULL || handle == 0) {
  14194. + pr_err("[%s]: invalid input\n", __func__);
  14195. + return -EPERM;
  14196. + }
  14197. +
  14198. + ioparam.handle = handle;
  14199. + return vc_sm_ioctl_unlock(sm_state->data_knl,
  14200. + &ioparam, flush, 0, no_vc_unlock);
  14201. +}
  14202. +EXPORT_SYMBOL_GPL(vc_sm_unlock);
  14203. +
  14204. +/* Map a shared memory region for use by kernel.
  14205. +*/
  14206. +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
  14207. + long unsigned int *data)
  14208. +{
  14209. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  14210. + int ret;
  14211. +
  14212. + /* Validate we can work with this device.
  14213. + */
  14214. + if (sm_state == NULL || handle == 0 || data == NULL || sm_addr == 0) {
  14215. + pr_err("[%s]: invalid input\n", __func__);
  14216. + return -EPERM;
  14217. + }
  14218. +
  14219. + *data = 0;
  14220. +
  14221. + ioparam.handle = handle;
  14222. + ret = vc_sm_ioctl_lock(sm_state->data_knl,
  14223. + &ioparam,
  14224. + 1,
  14225. + ((mode ==
  14226. + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
  14227. + VMCS_SM_CACHE_NONE), sm_addr);
  14228. +
  14229. + *data = ioparam.addr;
  14230. + return ret;
  14231. +}
  14232. +EXPORT_SYMBOL_GPL(vc_sm_map);
  14233. +#endif
  14234. +
  14235. +late_initcall(vc_sm_init);
  14236. +module_exit(vc_sm_exit);
  14237. +
  14238. +MODULE_AUTHOR("Broadcom");
  14239. +MODULE_DESCRIPTION("VideoCore SharedMemory Driver");
  14240. +MODULE_LICENSE("GPL v2");
  14241. diff -Nur linux-3.17.5/drivers/char/hw_random/bcm2708-rng.c linux-rpi/drivers/char/hw_random/bcm2708-rng.c
  14242. --- linux-3.17.5/drivers/char/hw_random/bcm2708-rng.c 1969-12-31 18:00:00.000000000 -0600
  14243. +++ linux-rpi/drivers/char/hw_random/bcm2708-rng.c 2014-12-11 14:05:37.428418001 -0600
  14244. @@ -0,0 +1,118 @@
  14245. +/**
  14246. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  14247. + *
  14248. + * Redistribution and use in source and binary forms, with or without
  14249. + * modification, are permitted provided that the following conditions
  14250. + * are met:
  14251. + * 1. Redistributions of source code must retain the above copyright
  14252. + * notice, this list of conditions, and the following disclaimer,
  14253. + * without modification.
  14254. + * 2. Redistributions in binary form must reproduce the above copyright
  14255. + * notice, this list of conditions and the following disclaimer in the
  14256. + * documentation and/or other materials provided with the distribution.
  14257. + * 3. The names of the above-listed copyright holders may not be used
  14258. + * to endorse or promote products derived from this software without
  14259. + * specific prior written permission.
  14260. + *
  14261. + * ALTERNATIVELY, this software may be distributed under the terms of the
  14262. + * GNU General Public License ("GPL") version 2, as published by the Free
  14263. + * Software Foundation.
  14264. + *
  14265. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  14266. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  14267. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14268. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  14269. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  14270. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14271. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  14272. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  14273. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  14274. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  14275. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14276. + */
  14277. +
  14278. +#include <linux/kernel.h>
  14279. +#include <linux/module.h>
  14280. +#include <linux/init.h>
  14281. +#include <linux/hw_random.h>
  14282. +#include <linux/printk.h>
  14283. +
  14284. +#include <asm/io.h>
  14285. +#include <mach/hardware.h>
  14286. +#include <mach/platform.h>
  14287. +
  14288. +#define RNG_CTRL (0x0)
  14289. +#define RNG_STATUS (0x4)
  14290. +#define RNG_DATA (0x8)
  14291. +#define RNG_FF_THRESHOLD (0xc)
  14292. +
  14293. +/* enable rng */
  14294. +#define RNG_RBGEN 0x1
  14295. +/* double speed, less random mode */
  14296. +#define RNG_RBG2X 0x2
  14297. +
  14298. +/* the initial numbers generated are "less random" so will be discarded */
  14299. +#define RNG_WARMUP_COUNT 0x40000
  14300. +
  14301. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  14302. +{
  14303. + void __iomem *rng_base = (void __iomem *)rng->priv;
  14304. + unsigned words;
  14305. + /* wait for a random number to be in fifo */
  14306. + do {
  14307. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  14308. + }
  14309. + while (words == 0);
  14310. + /* read the random number */
  14311. + *buffer = __raw_readl(rng_base + RNG_DATA);
  14312. + return 4;
  14313. +}
  14314. +
  14315. +static struct hwrng bcm2708_rng_ops = {
  14316. + .name = "bcm2708",
  14317. + .data_read = bcm2708_rng_data_read,
  14318. +};
  14319. +
  14320. +static int __init bcm2708_rng_init(void)
  14321. +{
  14322. + void __iomem *rng_base;
  14323. + int err;
  14324. +
  14325. + /* map peripheral */
  14326. + rng_base = ioremap(RNG_BASE, 0x10);
  14327. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  14328. + if (!rng_base) {
  14329. + pr_err("bcm2708_rng_init failed to ioremap\n");
  14330. + return -ENOMEM;
  14331. + }
  14332. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  14333. +
  14334. + /* set warm-up count & enable */
  14335. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  14336. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  14337. +
  14338. + /* register driver */
  14339. + err = hwrng_register(&bcm2708_rng_ops);
  14340. + if (err) {
  14341. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  14342. + iounmap(rng_base);
  14343. + }
  14344. + return err;
  14345. +}
  14346. +
  14347. +static void __exit bcm2708_rng_exit(void)
  14348. +{
  14349. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  14350. + pr_info("bcm2708_rng_exit\n");
  14351. + /* disable rng hardware */
  14352. + __raw_writel(0, rng_base + RNG_CTRL);
  14353. + /* unregister driver */
  14354. + hwrng_unregister(&bcm2708_rng_ops);
  14355. + iounmap(rng_base);
  14356. +}
  14357. +
  14358. +module_init(bcm2708_rng_init);
  14359. +module_exit(bcm2708_rng_exit);
  14360. +
  14361. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  14362. +MODULE_LICENSE("GPL and additional rights");
  14363. diff -Nur linux-3.17.5/drivers/char/hw_random/Kconfig linux-rpi/drivers/char/hw_random/Kconfig
  14364. --- linux-3.17.5/drivers/char/hw_random/Kconfig 2014-12-06 17:57:59.000000000 -0600
  14365. +++ linux-rpi/drivers/char/hw_random/Kconfig 2014-12-11 14:05:37.428418001 -0600
  14366. @@ -320,6 +320,17 @@
  14367. If unsure, say Y.
  14368. +config HW_RANDOM_BCM2708
  14369. + tristate "BCM2708 generic true random number generator support"
  14370. + depends on HW_RANDOM && ARCH_BCM2708
  14371. + ---help---
  14372. + This driver provides the kernel-side support for the BCM2708 hardware.
  14373. +
  14374. + To compile this driver as a module, choose M here: the
  14375. + module will be called bcm2708-rng.
  14376. +
  14377. + If unsure, say N.
  14378. +
  14379. config HW_RANDOM_MSM
  14380. tristate "Qualcomm SoCs Random Number Generator support"
  14381. depends on HW_RANDOM && ARCH_QCOM
  14382. diff -Nur linux-3.17.5/drivers/char/hw_random/Makefile linux-rpi/drivers/char/hw_random/Makefile
  14383. --- linux-3.17.5/drivers/char/hw_random/Makefile 2014-12-06 17:57:59.000000000 -0600
  14384. +++ linux-rpi/drivers/char/hw_random/Makefile 2014-12-11 14:05:37.428418001 -0600
  14385. @@ -28,4 +28,5 @@
  14386. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  14387. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  14388. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  14389. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  14390. obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
  14391. diff -Nur linux-3.17.5/drivers/char/Kconfig linux-rpi/drivers/char/Kconfig
  14392. --- linux-3.17.5/drivers/char/Kconfig 2014-12-06 17:57:59.000000000 -0600
  14393. +++ linux-rpi/drivers/char/Kconfig 2014-12-11 14:05:37.424418001 -0600
  14394. @@ -581,6 +581,8 @@
  14395. source "drivers/s390/char/Kconfig"
  14396. +source "drivers/char/broadcom/Kconfig"
  14397. +
  14398. config MSM_SMD_PKT
  14399. bool "Enable device interface for some SMD packet ports"
  14400. default n
  14401. diff -Nur linux-3.17.5/drivers/char/Makefile linux-rpi/drivers/char/Makefile
  14402. --- linux-3.17.5/drivers/char/Makefile 2014-12-06 17:57:59.000000000 -0600
  14403. +++ linux-rpi/drivers/char/Makefile 2014-12-11 14:05:37.424418001 -0600
  14404. @@ -61,3 +61,5 @@
  14405. js-rtc-y = rtc.o
  14406. obj-$(CONFIG_TILE_SROM) += tile-srom.o
  14407. +
  14408. +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  14409. diff -Nur linux-3.17.5/drivers/cpufreq/bcm2835-cpufreq.c linux-rpi/drivers/cpufreq/bcm2835-cpufreq.c
  14410. --- linux-3.17.5/drivers/cpufreq/bcm2835-cpufreq.c 1969-12-31 18:00:00.000000000 -0600
  14411. +++ linux-rpi/drivers/cpufreq/bcm2835-cpufreq.c 2014-12-11 14:05:37.532418001 -0600
  14412. @@ -0,0 +1,224 @@
  14413. +/*****************************************************************************
  14414. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  14415. +*
  14416. +* Unless you and Broadcom execute a separate written software license
  14417. +* agreement governing use of this software, this software is licensed to you
  14418. +* under the terms of the GNU General Public License version 2, available at
  14419. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  14420. +*
  14421. +* Notwithstanding the above, under no circumstances may you combine this
  14422. +* software in any way with any other Broadcom software provided under a
  14423. +* license other than the GPL, without Broadcom's express prior written
  14424. +* consent.
  14425. +*****************************************************************************/
  14426. +
  14427. +/*****************************************************************************
  14428. +* FILENAME: bcm2835-cpufreq.h
  14429. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  14430. +* processor. Messages are sent to Videocore either setting or requesting the
  14431. +* frequency of the ARM in order to match an appropiate frequency to the current
  14432. +* usage of the processor. The policy which selects the frequency to use is
  14433. +* defined in the kernel .config file, but can be changed during runtime.
  14434. +*****************************************************************************/
  14435. +
  14436. +/* ---------- INCLUDES ---------- */
  14437. +#include <linux/kernel.h>
  14438. +#include <linux/init.h>
  14439. +#include <linux/module.h>
  14440. +#include <linux/cpufreq.h>
  14441. +#include <mach/vcio.h>
  14442. +
  14443. +/* ---------- DEFINES ---------- */
  14444. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  14445. +#define MODULE_NAME "bcm2835-cpufreq"
  14446. +
  14447. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  14448. +
  14449. +/* debug printk macros */
  14450. +#ifdef CPUFREQ_DEBUG_ENABLE
  14451. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  14452. +#else
  14453. +#define print_debug(fmt,...)
  14454. +#endif
  14455. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  14456. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  14457. +
  14458. +/* tag part of the message */
  14459. +struct vc_msg_tag {
  14460. + uint32_t tag_id; /* the message id */
  14461. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  14462. + uint32_t data_size; /* amount of data being sent or received */
  14463. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  14464. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  14465. +};
  14466. +
  14467. +/* message structure to be sent to videocore */
  14468. +struct vc_msg {
  14469. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  14470. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  14471. + struct vc_msg_tag tag; /* the tag structure above to make */
  14472. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  14473. +};
  14474. +
  14475. +/* ---------- GLOBALS ---------- */
  14476. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  14477. +
  14478. +static struct cpufreq_frequency_table bcm2835_freq_table[] = {
  14479. + {0, 0, 0},
  14480. + {0, 0, 0},
  14481. + {0, 0, CPUFREQ_TABLE_END},
  14482. +};
  14483. +
  14484. +/*
  14485. + ===============================================
  14486. + clk_rate either gets or sets the clock rates.
  14487. + ===============================================
  14488. +*/
  14489. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  14490. +{
  14491. + int s, actual_rate=0;
  14492. + struct vc_msg msg;
  14493. +
  14494. + /* wipe all previous message data */
  14495. + memset(&msg, 0, sizeof msg);
  14496. +
  14497. + msg.msg_size = sizeof msg;
  14498. +
  14499. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  14500. + msg.tag.buffer_size = 8;
  14501. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  14502. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  14503. + msg.tag.val = arm_rate * 1000;
  14504. +
  14505. + /* send the message */
  14506. + s = bcm_mailbox_property(&msg, sizeof msg);
  14507. +
  14508. + /* check if it was all ok and return the rate in KHz */
  14509. + if (s == 0 && (msg.request_code & 0x80000000))
  14510. + actual_rate = msg.tag.val/1000;
  14511. +
  14512. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  14513. + return actual_rate;
  14514. +}
  14515. +
  14516. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  14517. +{
  14518. + int s;
  14519. + int arm_rate = 0;
  14520. + struct vc_msg msg;
  14521. +
  14522. + /* wipe all previous message data */
  14523. + memset(&msg, 0, sizeof msg);
  14524. +
  14525. + msg.msg_size = sizeof msg;
  14526. + msg.tag.tag_id = tag;
  14527. + msg.tag.buffer_size = 8;
  14528. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  14529. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  14530. +
  14531. + /* send the message */
  14532. + s = bcm_mailbox_property(&msg, sizeof msg);
  14533. +
  14534. + /* check if it was all ok and return the rate in KHz */
  14535. + if (s == 0 && (msg.request_code & 0x80000000))
  14536. + arm_rate = msg.tag.val/1000;
  14537. +
  14538. + print_debug("%s frequency = %d\n",
  14539. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  14540. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  14541. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  14542. + "Unexpected", arm_rate);
  14543. +
  14544. + return arm_rate;
  14545. +}
  14546. +
  14547. +/*
  14548. + ====================================================
  14549. + Module Initialisation registers the cpufreq driver
  14550. + ====================================================
  14551. +*/
  14552. +static int __init bcm2835_cpufreq_module_init(void)
  14553. +{
  14554. + print_debug("IN\n");
  14555. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  14556. +}
  14557. +
  14558. +/*
  14559. + =============
  14560. + Module exit
  14561. + =============
  14562. +*/
  14563. +static void __exit bcm2835_cpufreq_module_exit(void)
  14564. +{
  14565. + print_debug("IN\n");
  14566. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  14567. + return;
  14568. +}
  14569. +
  14570. +/*
  14571. + ==============================================================
  14572. + Initialisation function sets up the CPU policy for first use
  14573. + ==============================================================
  14574. +*/
  14575. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  14576. +{
  14577. + /* measured value of how long it takes to change frequency */
  14578. + const unsigned int transition_latency = 355000; /* ns */
  14579. +
  14580. + /* now find out what the maximum and minimum frequencies are */
  14581. + bcm2835_freq_table[0].frequency = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  14582. + bcm2835_freq_table[1].frequency = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  14583. +
  14584. + print_info("min=%d max=%d\n", bcm2835_freq_table[0].frequency, bcm2835_freq_table[1].frequency);
  14585. + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
  14586. +}
  14587. +
  14588. +/*
  14589. + =====================================================================
  14590. + Target index function chooses the requested frequency from the table
  14591. + =====================================================================
  14592. +*/
  14593. +
  14594. +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
  14595. +{
  14596. + unsigned int target_freq = bcm2835_freq_table[state].frequency;
  14597. + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
  14598. +
  14599. + if (!cur)
  14600. + {
  14601. + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
  14602. + return -EINVAL;
  14603. + }
  14604. + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
  14605. + return 0;
  14606. +}
  14607. +
  14608. +/*
  14609. + ======================================================
  14610. + Get function returns the current frequency from table
  14611. + ======================================================
  14612. +*/
  14613. +
  14614. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  14615. +{
  14616. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  14617. + print_debug("%d: freq=%d\n", cpu, actual_rate);
  14618. + return actual_rate <= bcm2835_freq_table[0].frequency ? bcm2835_freq_table[0].frequency : bcm2835_freq_table[1].frequency;
  14619. +}
  14620. +
  14621. +/* the CPUFreq driver */
  14622. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  14623. + .name = "BCM2835 CPUFreq",
  14624. + .init = bcm2835_cpufreq_driver_init,
  14625. + .verify = cpufreq_generic_frequency_table_verify,
  14626. + .target_index = bcm2835_cpufreq_driver_target_index,
  14627. + .get = bcm2835_cpufreq_driver_get,
  14628. + .attr = cpufreq_generic_attr,
  14629. +};
  14630. +
  14631. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  14632. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  14633. +MODULE_LICENSE("GPL");
  14634. +
  14635. +module_init(bcm2835_cpufreq_module_init);
  14636. +module_exit(bcm2835_cpufreq_module_exit);
  14637. diff -Nur linux-3.17.5/drivers/cpufreq/Kconfig.arm linux-rpi/drivers/cpufreq/Kconfig.arm
  14638. --- linux-3.17.5/drivers/cpufreq/Kconfig.arm 2014-12-06 17:57:59.000000000 -0600
  14639. +++ linux-rpi/drivers/cpufreq/Kconfig.arm 2014-12-11 14:05:37.532418001 -0600
  14640. @@ -241,6 +241,14 @@
  14641. help
  14642. This adds the CPUFreq driver support for SPEAr SOCs.
  14643. +config ARM_BCM2835_CPUFREQ
  14644. + bool "BCM2835 Driver"
  14645. + default y
  14646. + help
  14647. + This adds the CPUFreq driver for BCM2835
  14648. +
  14649. + If in doubt, say N.
  14650. +
  14651. config ARM_TEGRA_CPUFREQ
  14652. bool "TEGRA CPUFreq support"
  14653. depends on ARCH_TEGRA
  14654. diff -Nur linux-3.17.5/drivers/cpufreq/Makefile linux-rpi/drivers/cpufreq/Makefile
  14655. --- linux-3.17.5/drivers/cpufreq/Makefile 2014-12-06 17:57:59.000000000 -0600
  14656. +++ linux-rpi/drivers/cpufreq/Makefile 2014-12-11 14:05:37.532418001 -0600
  14657. @@ -75,6 +75,7 @@
  14658. obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
  14659. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  14660. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  14661. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  14662. obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
  14663. obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
  14664. diff -Nur linux-3.17.5/drivers/dma/bcm2708-dmaengine.c linux-rpi/drivers/dma/bcm2708-dmaengine.c
  14665. --- linux-3.17.5/drivers/dma/bcm2708-dmaengine.c 1969-12-31 18:00:00.000000000 -0600
  14666. +++ linux-rpi/drivers/dma/bcm2708-dmaengine.c 2014-12-11 14:05:37.560418001 -0600
  14667. @@ -0,0 +1,1041 @@
  14668. +/*
  14669. + * BCM2835 DMA engine support
  14670. + *
  14671. + * This driver supports cyclic and scatter/gather DMA transfers.
  14672. + *
  14673. + * Author: Florian Meier <florian.meier@koalo.de>
  14674. + * Gellert Weisz <gellert@raspberrypi.org>
  14675. + * Copyright 2013-2014
  14676. + *
  14677. + * Based on
  14678. + * OMAP DMAengine support by Russell King
  14679. + *
  14680. + * BCM2708 DMA Driver
  14681. + * Copyright (C) 2010 Broadcom
  14682. + *
  14683. + * Raspberry Pi PCM I2S ALSA Driver
  14684. + * Copyright (c) by Phil Poole 2013
  14685. + *
  14686. + * MARVELL MMP Peripheral DMA Driver
  14687. + * Copyright 2012 Marvell International Ltd.
  14688. + *
  14689. + * This program is free software; you can redistribute it and/or modify
  14690. + * it under the terms of the GNU General Public License as published by
  14691. + * the Free Software Foundation; either version 2 of the License, or
  14692. + * (at your option) any later version.
  14693. + *
  14694. + * This program is distributed in the hope that it will be useful,
  14695. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14696. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14697. + * GNU General Public License for more details.
  14698. + */
  14699. +
  14700. +#include <linux/dmaengine.h>
  14701. +#include <linux/dma-mapping.h>
  14702. +#include <linux/err.h>
  14703. +#include <linux/init.h>
  14704. +#include <linux/interrupt.h>
  14705. +#include <linux/list.h>
  14706. +#include <linux/module.h>
  14707. +#include <linux/platform_device.h>
  14708. +#include <linux/slab.h>
  14709. +#include <linux/io.h>
  14710. +#include <linux/spinlock.h>
  14711. +
  14712. +#ifndef CONFIG_ARCH_BCM2835
  14713. +
  14714. +/* dma manager */
  14715. +#include <mach/dma.h>
  14716. +
  14717. +//#define DMA_COMPLETE DMA_SUCCESS
  14718. +
  14719. +#endif
  14720. +
  14721. +#include <linux/of.h>
  14722. +#include <linux/of_dma.h>
  14723. +
  14724. +#include "virt-dma.h"
  14725. +
  14726. +
  14727. +struct bcm2835_dmadev {
  14728. + struct dma_device ddev;
  14729. + spinlock_t lock;
  14730. + void __iomem *base;
  14731. + struct device_dma_parameters dma_parms;
  14732. +};
  14733. +
  14734. +struct bcm2835_dma_cb {
  14735. + uint32_t info;
  14736. + uint32_t src;
  14737. + uint32_t dst;
  14738. + uint32_t length;
  14739. + uint32_t stride;
  14740. + uint32_t next;
  14741. + uint32_t pad[2];
  14742. +};
  14743. +
  14744. +struct bcm2835_chan {
  14745. + struct virt_dma_chan vc;
  14746. + struct list_head node;
  14747. +
  14748. + struct dma_slave_config cfg;
  14749. + bool cyclic;
  14750. +
  14751. + int ch;
  14752. + struct bcm2835_desc *desc;
  14753. +
  14754. + void __iomem *chan_base;
  14755. + int irq_number;
  14756. +
  14757. + unsigned int dreq;
  14758. +};
  14759. +
  14760. +struct bcm2835_desc {
  14761. + struct virt_dma_desc vd;
  14762. + enum dma_transfer_direction dir;
  14763. +
  14764. + unsigned int control_block_size;
  14765. + struct bcm2835_dma_cb *control_block_base;
  14766. + dma_addr_t control_block_base_phys;
  14767. +
  14768. + unsigned int frames;
  14769. + size_t size;
  14770. +};
  14771. +
  14772. +#define BCM2835_DMA_CS 0x00
  14773. +#define BCM2835_DMA_ADDR 0x04
  14774. +#define BCM2835_DMA_SOURCE_AD 0x0c
  14775. +#define BCM2835_DMA_DEST_AD 0x10
  14776. +#define BCM2835_DMA_NEXTCB 0x1C
  14777. +
  14778. +/* DMA CS Control and Status bits */
  14779. +#define BCM2835_DMA_ACTIVE BIT(0)
  14780. +#define BCM2835_DMA_INT BIT(2)
  14781. +#define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
  14782. +#define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
  14783. +#define BCM2835_DMA_ERR BIT(8)
  14784. +#define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
  14785. +#define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
  14786. +
  14787. +#define BCM2835_DMA_INT_EN BIT(0)
  14788. +#define BCM2835_DMA_WAIT_RESP BIT(3)
  14789. +#define BCM2835_DMA_D_INC BIT(4)
  14790. +#define BCM2835_DMA_D_WIDTH BIT(5)
  14791. +#define BCM2835_DMA_D_DREQ BIT(6)
  14792. +#define BCM2835_DMA_S_INC BIT(8)
  14793. +#define BCM2835_DMA_S_WIDTH BIT(9)
  14794. +#define BCM2835_DMA_S_DREQ BIT(10)
  14795. +
  14796. +#define BCM2835_DMA_PER_MAP(x) ((x) << 16)
  14797. +#define BCM2835_DMA_WAITS(x) (((x)&0x1f) << 21)
  14798. +
  14799. +#define SDHCI_BCM_DMA_WAITS 20 /* delays slowing DMA transfers: 0-31 */
  14800. +
  14801. +#define BCM2835_DMA_DATA_TYPE_S8 1
  14802. +#define BCM2835_DMA_DATA_TYPE_S16 2
  14803. +#define BCM2835_DMA_DATA_TYPE_S32 4
  14804. +#define BCM2835_DMA_DATA_TYPE_S128 16
  14805. +
  14806. +#define BCM2835_DMA_BULK_MASK BIT(0)
  14807. +#define BCM2835_DMA_FIQ_MASK (BIT(2) | BIT(3))
  14808. +
  14809. +
  14810. +/* Valid only for channels 0 - 14, 15 has its own base address */
  14811. +#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  14812. +#define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  14813. +
  14814. +#define MAX_LITE_TRANSFER 32768
  14815. +#define MAX_NORMAL_TRANSFER 1073741824
  14816. +
  14817. +static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  14818. +{
  14819. + return container_of(d, struct bcm2835_dmadev, ddev);
  14820. +}
  14821. +
  14822. +static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
  14823. +{
  14824. + return container_of(c, struct bcm2835_chan, vc.chan);
  14825. +}
  14826. +
  14827. +static inline struct bcm2835_desc *to_bcm2835_dma_desc(
  14828. + struct dma_async_tx_descriptor *t)
  14829. +{
  14830. + return container_of(t, struct bcm2835_desc, vd.tx);
  14831. +}
  14832. +
  14833. +static void dma_dumpregs(struct bcm2835_chan *c)
  14834. +{
  14835. + pr_debug("-------------DMA DUMPREGS-------------\n");
  14836. + pr_debug("CS= %u\n",
  14837. + readl(c->chan_base + BCM2835_DMA_CS));
  14838. + pr_debug("ADDR= %u\n",
  14839. + readl(c->chan_base + BCM2835_DMA_ADDR));
  14840. + pr_debug("SOURCE_ADDR= %u\n",
  14841. + readl(c->chan_base + BCM2835_DMA_SOURCE_AD));
  14842. + pr_debug("DEST_AD= %u\n",
  14843. + readl(c->chan_base + BCM2835_DMA_DEST_AD));
  14844. + pr_debug("NEXTCB= %u\n",
  14845. + readl(c->chan_base + BCM2835_DMA_NEXTCB));
  14846. + pr_debug("--------------------------------------\n");
  14847. +}
  14848. +
  14849. +static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  14850. +{
  14851. + struct bcm2835_desc *desc = container_of(vd, struct bcm2835_desc, vd);
  14852. + dma_free_coherent(desc->vd.tx.chan->device->dev,
  14853. + desc->control_block_size,
  14854. + desc->control_block_base,
  14855. + desc->control_block_base_phys);
  14856. + kfree(desc);
  14857. +}
  14858. +
  14859. +static int bcm2835_dma_abort(void __iomem *chan_base)
  14860. +{
  14861. + unsigned long cs;
  14862. + long int timeout = 10000;
  14863. +
  14864. + cs = readl(chan_base + BCM2835_DMA_CS);
  14865. + if (!(cs & BCM2835_DMA_ACTIVE))
  14866. + return 0;
  14867. +
  14868. + /* Write 0 to the active bit - Pause the DMA */
  14869. + writel(0, chan_base + BCM2835_DMA_CS);
  14870. +
  14871. + /* Wait for any current AXI transfer to complete */
  14872. + while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
  14873. + cpu_relax();
  14874. + cs = readl(chan_base + BCM2835_DMA_CS);
  14875. + }
  14876. +
  14877. + /* We'll un-pause when we set of our next DMA */
  14878. + if (!timeout)
  14879. + return -ETIMEDOUT;
  14880. +
  14881. + if (!(cs & BCM2835_DMA_ACTIVE))
  14882. + return 0;
  14883. +
  14884. + /* Terminate the control block chain */
  14885. + writel(0, chan_base + BCM2835_DMA_NEXTCB);
  14886. +
  14887. + /* Abort the whole DMA */
  14888. + writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
  14889. + chan_base + BCM2835_DMA_CS);
  14890. +
  14891. + return 0;
  14892. +}
  14893. +
  14894. +
  14895. +static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
  14896. +{
  14897. + struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  14898. + struct bcm2835_desc *d;
  14899. +
  14900. + if (!vd) {
  14901. + c->desc = NULL;
  14902. + return;
  14903. + }
  14904. +
  14905. + list_del(&vd->node);
  14906. +
  14907. + c->desc = d = to_bcm2835_dma_desc(&vd->tx);
  14908. +
  14909. + writel(d->control_block_base_phys, c->chan_base + BCM2835_DMA_ADDR);
  14910. + writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  14911. +
  14912. +}
  14913. +
  14914. +static irqreturn_t bcm2835_dma_callback(int irq, void *data)
  14915. +{
  14916. + struct bcm2835_chan *c = data;
  14917. + struct bcm2835_desc *d;
  14918. + unsigned long flags;
  14919. +
  14920. + spin_lock_irqsave(&c->vc.lock, flags);
  14921. +
  14922. + /* Acknowledge interrupt */
  14923. + writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
  14924. +
  14925. + d = c->desc;
  14926. +
  14927. + if (d) {
  14928. + if (c->cyclic) {
  14929. + vchan_cyclic_callback(&d->vd);
  14930. +
  14931. + /* Keep the DMA engine running */
  14932. + writel(BCM2835_DMA_ACTIVE,
  14933. + c->chan_base + BCM2835_DMA_CS);
  14934. +
  14935. + } else {
  14936. + vchan_cookie_complete(&c->desc->vd);
  14937. + bcm2835_dma_start_desc(c);
  14938. + }
  14939. + }
  14940. +
  14941. + spin_unlock_irqrestore(&c->vc.lock, flags);
  14942. +
  14943. + return IRQ_HANDLED;
  14944. +}
  14945. +
  14946. +static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  14947. +{
  14948. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14949. + int ret;
  14950. +
  14951. + dev_dbg(c->vc.chan.device->dev,
  14952. + "Allocating DMA channel %d\n", c->ch);
  14953. +
  14954. + ret = request_irq(c->irq_number,
  14955. + bcm2835_dma_callback, 0, "DMA IRQ", c);
  14956. +
  14957. + return ret;
  14958. +}
  14959. +
  14960. +static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  14961. +{
  14962. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14963. +
  14964. + vchan_free_chan_resources(&c->vc);
  14965. + free_irq(c->irq_number, c);
  14966. +
  14967. + dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  14968. +}
  14969. +
  14970. +static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  14971. +{
  14972. + return d->size;
  14973. +}
  14974. +
  14975. +static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  14976. +{
  14977. + unsigned int i;
  14978. + size_t size;
  14979. +
  14980. + for (size = i = 0; i < d->frames; i++) {
  14981. + struct bcm2835_dma_cb *control_block =
  14982. + &d->control_block_base[i];
  14983. + size_t this_size = control_block->length;
  14984. + dma_addr_t dma;
  14985. +
  14986. + if (d->dir == DMA_DEV_TO_MEM)
  14987. + dma = control_block->dst;
  14988. + else
  14989. + dma = control_block->src;
  14990. +
  14991. + if (size)
  14992. + size += this_size;
  14993. + else if (addr >= dma && addr < dma + this_size)
  14994. + size += dma + this_size - addr;
  14995. + }
  14996. +
  14997. + return size;
  14998. +}
  14999. +
  15000. +static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  15001. + dma_cookie_t cookie, struct dma_tx_state *txstate)
  15002. +{
  15003. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15004. + struct bcm2835_desc *d;
  15005. + struct virt_dma_desc *vd;
  15006. + enum dma_status ret;
  15007. + unsigned long flags;
  15008. + dma_addr_t pos;
  15009. +
  15010. + ret = dma_cookie_status(chan, cookie, txstate);
  15011. + if (ret == DMA_COMPLETE || !txstate)
  15012. + return ret;
  15013. +
  15014. + spin_lock_irqsave(&c->vc.lock, flags);
  15015. + vd = vchan_find_desc(&c->vc, cookie);
  15016. + if (vd) {
  15017. + txstate->residue =
  15018. + bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  15019. + } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  15020. + d = c->desc;
  15021. +
  15022. + if (d->dir == DMA_MEM_TO_DEV)
  15023. + pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  15024. + else if (d->dir == DMA_DEV_TO_MEM)
  15025. + pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  15026. + else
  15027. + pos = 0;
  15028. +
  15029. + txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  15030. + } else {
  15031. + txstate->residue = 0;
  15032. + }
  15033. +
  15034. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15035. +
  15036. + return ret;
  15037. +}
  15038. +
  15039. +static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  15040. +{
  15041. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15042. + unsigned long flags;
  15043. +
  15044. + spin_lock_irqsave(&c->vc.lock, flags);
  15045. + if (vchan_issue_pending(&c->vc) && !c->desc)
  15046. + bcm2835_dma_start_desc(c);
  15047. +
  15048. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15049. +}
  15050. +
  15051. +static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  15052. + struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  15053. + size_t period_len, enum dma_transfer_direction direction,
  15054. + unsigned long flags)
  15055. +{
  15056. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15057. + enum dma_slave_buswidth dev_width;
  15058. + struct bcm2835_desc *d;
  15059. + dma_addr_t dev_addr;
  15060. + unsigned int es, sync_type;
  15061. + unsigned int frame;
  15062. +
  15063. + /* Grab configuration */
  15064. + if (!is_slave_direction(direction)) {
  15065. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  15066. + return NULL;
  15067. + }
  15068. +
  15069. + if (direction == DMA_DEV_TO_MEM) {
  15070. + dev_addr = c->cfg.src_addr;
  15071. + dev_width = c->cfg.src_addr_width;
  15072. + sync_type = BCM2835_DMA_S_DREQ;
  15073. + } else {
  15074. + dev_addr = c->cfg.dst_addr;
  15075. + dev_width = c->cfg.dst_addr_width;
  15076. + sync_type = BCM2835_DMA_D_DREQ;
  15077. + }
  15078. +
  15079. + /* Bus width translates to the element size (ES) */
  15080. + switch (dev_width) {
  15081. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  15082. + es = BCM2835_DMA_DATA_TYPE_S32;
  15083. + break;
  15084. + default:
  15085. + return NULL;
  15086. + }
  15087. +
  15088. + /* Now allocate and setup the descriptor. */
  15089. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  15090. + if (!d)
  15091. + return NULL;
  15092. +
  15093. + d->dir = direction;
  15094. + d->frames = buf_len / period_len;
  15095. +
  15096. + /* Allocate memory for control blocks */
  15097. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  15098. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  15099. + d->control_block_size, &d->control_block_base_phys,
  15100. + GFP_NOWAIT);
  15101. +
  15102. + if (!d->control_block_base) {
  15103. + kfree(d);
  15104. + return NULL;
  15105. + }
  15106. +
  15107. + /*
  15108. + * Iterate over all frames, create a control block
  15109. + * for each frame and link them together.
  15110. + */
  15111. + for (frame = 0; frame < d->frames; frame++) {
  15112. + struct bcm2835_dma_cb *control_block =
  15113. + &d->control_block_base[frame];
  15114. +
  15115. + /* Setup adresses */
  15116. + if (d->dir == DMA_DEV_TO_MEM) {
  15117. + control_block->info = BCM2835_DMA_D_INC;
  15118. + control_block->src = dev_addr;
  15119. + control_block->dst = buf_addr + frame * period_len;
  15120. + } else {
  15121. + control_block->info = BCM2835_DMA_S_INC;
  15122. + control_block->src = buf_addr + frame * period_len;
  15123. + control_block->dst = dev_addr;
  15124. + }
  15125. +
  15126. + /* Enable interrupt */
  15127. + control_block->info |= BCM2835_DMA_INT_EN;
  15128. +
  15129. + /* Setup synchronization */
  15130. + if (sync_type != 0)
  15131. + control_block->info |= sync_type;
  15132. +
  15133. + /* Setup DREQ channel */
  15134. + if (c->cfg.slave_id != 0)
  15135. + control_block->info |=
  15136. + BCM2835_DMA_PER_MAP(c->cfg.slave_id);
  15137. +
  15138. + /* Length of a frame */
  15139. + control_block->length = period_len;
  15140. + d->size += control_block->length;
  15141. +
  15142. + /*
  15143. + * Next block is the next frame.
  15144. + * This function is called on cyclic DMA transfers.
  15145. + * Therefore, wrap around at number of frames.
  15146. + */
  15147. + control_block->next = d->control_block_base_phys +
  15148. + sizeof(struct bcm2835_dma_cb)
  15149. + * ((frame + 1) % d->frames);
  15150. + }
  15151. +
  15152. + c->cyclic = true;
  15153. +
  15154. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  15155. +}
  15156. +
  15157. +
  15158. +static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
  15159. + struct dma_chan *chan, struct scatterlist *sgl,
  15160. + unsigned int sg_len, enum dma_transfer_direction direction,
  15161. + unsigned long flags, void *context)
  15162. +{
  15163. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15164. + enum dma_slave_buswidth dev_width;
  15165. + struct bcm2835_desc *d;
  15166. + dma_addr_t dev_addr;
  15167. + struct scatterlist *sgent;
  15168. + unsigned int es, sync_type;
  15169. + unsigned int i, j, splitct, max_size;
  15170. +
  15171. + if (!is_slave_direction(direction)) {
  15172. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  15173. + return NULL;
  15174. + }
  15175. +
  15176. + if (direction == DMA_DEV_TO_MEM) {
  15177. + dev_addr = c->cfg.src_addr;
  15178. + dev_width = c->cfg.src_addr_width;
  15179. + sync_type = BCM2835_DMA_S_DREQ;
  15180. + } else {
  15181. + dev_addr = c->cfg.dst_addr;
  15182. + dev_width = c->cfg.dst_addr_width;
  15183. + sync_type = BCM2835_DMA_D_DREQ;
  15184. + }
  15185. +
  15186. + /* Bus width translates to the element size (ES) */
  15187. + switch (dev_width) {
  15188. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  15189. + es = BCM2835_DMA_DATA_TYPE_S32;
  15190. + break;
  15191. + default:
  15192. + return NULL;
  15193. + }
  15194. +
  15195. + /* Now allocate and setup the descriptor. */
  15196. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  15197. + if (!d)
  15198. + return NULL;
  15199. +
  15200. + d->dir = direction;
  15201. +
  15202. + if (c->ch >= 8) /* we have a LITE channel */
  15203. + max_size = MAX_LITE_TRANSFER;
  15204. + else
  15205. + max_size = MAX_NORMAL_TRANSFER;
  15206. +
  15207. + /* We store the length of the SG list in d->frames
  15208. + taking care to account for splitting up transfers
  15209. + too large for a LITE channel */
  15210. +
  15211. + d->frames = 0;
  15212. + for_each_sg(sgl, sgent, sg_len, i) {
  15213. + uint32_t len = sg_dma_len(sgent);
  15214. + d->frames += 1 + len / max_size;
  15215. + }
  15216. +
  15217. + /* Allocate memory for control blocks */
  15218. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  15219. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  15220. + d->control_block_size, &d->control_block_base_phys,
  15221. + GFP_NOWAIT);
  15222. +
  15223. + if (!d->control_block_base) {
  15224. + kfree(d);
  15225. + return NULL;
  15226. + }
  15227. +
  15228. + /*
  15229. + * Iterate over all SG entries, create a control block
  15230. + * for each frame and link them together.
  15231. + */
  15232. +
  15233. + /* we count the number of times an SG entry had to be splitct
  15234. + as a result of using a LITE channel */
  15235. + splitct = 0;
  15236. +
  15237. + for_each_sg(sgl, sgent, sg_len, i) {
  15238. + dma_addr_t addr = sg_dma_address(sgent);
  15239. + uint32_t len = sg_dma_len(sgent);
  15240. +
  15241. + for (j = 0; j < len; j += max_size) {
  15242. + struct bcm2835_dma_cb *control_block =
  15243. + &d->control_block_base[i+splitct];
  15244. +
  15245. + /* Setup adresses */
  15246. + if (d->dir == DMA_DEV_TO_MEM) {
  15247. + control_block->info = BCM2835_DMA_D_INC |
  15248. + BCM2835_DMA_D_WIDTH | BCM2835_DMA_S_DREQ;
  15249. + control_block->src = dev_addr;
  15250. + control_block->dst = addr + (dma_addr_t)j;
  15251. + } else {
  15252. + control_block->info = BCM2835_DMA_S_INC |
  15253. + BCM2835_DMA_S_WIDTH | BCM2835_DMA_D_DREQ;
  15254. + control_block->src = addr + (dma_addr_t)j;
  15255. + control_block->dst = dev_addr;
  15256. + }
  15257. +
  15258. + /* Common part */
  15259. + control_block->info |= BCM2835_DMA_WAITS(SDHCI_BCM_DMA_WAITS);
  15260. + control_block->info |= BCM2835_DMA_WAIT_RESP;
  15261. +
  15262. + /* Enable */
  15263. + if (i == sg_len-1 && len-j <= max_size)
  15264. + control_block->info |= BCM2835_DMA_INT_EN;
  15265. +
  15266. + /* Setup synchronization */
  15267. + if (sync_type != 0)
  15268. + control_block->info |= sync_type;
  15269. +
  15270. + /* Setup DREQ channel */
  15271. + c->dreq = c->cfg.slave_id; /* DREQ loaded from config */
  15272. +
  15273. + if (c->dreq != 0)
  15274. + control_block->info |=
  15275. + BCM2835_DMA_PER_MAP(c->dreq);
  15276. +
  15277. + /* Length of a frame */
  15278. + control_block->length = min(len-j, max_size);
  15279. + d->size += control_block->length;
  15280. +
  15281. + /*
  15282. + * Next block is the next frame.
  15283. + */
  15284. + if (i < sg_len-1 || len-j > max_size) {
  15285. + /* next block is the next frame. */
  15286. + control_block->next = d->control_block_base_phys +
  15287. + sizeof(struct bcm2835_dma_cb) * (i + splitct + 1);
  15288. + } else {
  15289. + /* next block is empty. */
  15290. + control_block->next = 0;
  15291. + }
  15292. +
  15293. + if (len-j > max_size)
  15294. + splitct++;
  15295. + }
  15296. + }
  15297. +
  15298. + c->cyclic = false;
  15299. +
  15300. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  15301. +}
  15302. +
  15303. +static int bcm2835_dma_slave_config(struct bcm2835_chan *c,
  15304. + struct dma_slave_config *cfg)
  15305. +{
  15306. + if ((cfg->direction == DMA_DEV_TO_MEM &&
  15307. + cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  15308. + (cfg->direction == DMA_MEM_TO_DEV &&
  15309. + cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  15310. + !is_slave_direction(cfg->direction)) {
  15311. + return -EINVAL;
  15312. + }
  15313. +
  15314. + c->cfg = *cfg;
  15315. +
  15316. + return 0;
  15317. +}
  15318. +
  15319. +static int bcm2835_dma_terminate_all(struct bcm2835_chan *c)
  15320. +{
  15321. + struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  15322. + unsigned long flags;
  15323. + int timeout = 10000;
  15324. + LIST_HEAD(head);
  15325. +
  15326. + spin_lock_irqsave(&c->vc.lock, flags);
  15327. +
  15328. + /* Prevent this channel being scheduled */
  15329. + spin_lock(&d->lock);
  15330. + list_del_init(&c->node);
  15331. + spin_unlock(&d->lock);
  15332. +
  15333. + /*
  15334. + * Stop DMA activity: we assume the callback will not be called
  15335. + * after bcm_dma_abort() returns (even if it does, it will see
  15336. + * c->desc is NULL and exit.)
  15337. + */
  15338. + if (c->desc) {
  15339. + c->desc = NULL;
  15340. + bcm2835_dma_abort(c->chan_base);
  15341. +
  15342. + /* Wait for stopping */
  15343. + while (--timeout) {
  15344. + if (!(readl(c->chan_base + BCM2835_DMA_CS) &
  15345. + BCM2835_DMA_ACTIVE))
  15346. + break;
  15347. +
  15348. + cpu_relax();
  15349. + }
  15350. +
  15351. + if (!timeout)
  15352. + dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  15353. + }
  15354. +
  15355. + vchan_get_all_descriptors(&c->vc, &head);
  15356. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15357. + vchan_dma_desc_free_list(&c->vc, &head);
  15358. +
  15359. + return 0;
  15360. +}
  15361. +
  15362. +static int bcm2835_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  15363. + unsigned long arg)
  15364. +{
  15365. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15366. +
  15367. + switch (cmd) {
  15368. + case DMA_SLAVE_CONFIG:
  15369. + return bcm2835_dma_slave_config(c,
  15370. + (struct dma_slave_config *)arg);
  15371. +
  15372. + case DMA_TERMINATE_ALL:
  15373. + return bcm2835_dma_terminate_all(c);
  15374. +
  15375. + default:
  15376. + return -ENXIO;
  15377. + }
  15378. +}
  15379. +
  15380. +#ifdef CONFIG_ARCH_BCM2835
  15381. +static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id, int irq)
  15382. +{
  15383. + struct bcm2835_chan *c;
  15384. +
  15385. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  15386. + if (!c)
  15387. + return -ENOMEM;
  15388. +
  15389. + c->vc.desc_free = bcm2835_dma_desc_free;
  15390. + vchan_init(&c->vc, &d->ddev);
  15391. + INIT_LIST_HEAD(&c->node);
  15392. +
  15393. + d->ddev.chancnt++;
  15394. +
  15395. + c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  15396. + c->ch = chan_id;
  15397. + c->irq_number = irq;
  15398. +
  15399. + return 0;
  15400. +}
  15401. +#endif
  15402. +
  15403. +static int bcm2708_dma_chan_init(struct bcm2835_dmadev *d,
  15404. + void __iomem *chan_base, int chan_id, int irq)
  15405. +{
  15406. + struct bcm2835_chan *c;
  15407. +
  15408. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  15409. + if (!c)
  15410. + return -ENOMEM;
  15411. +
  15412. + c->vc.desc_free = bcm2835_dma_desc_free;
  15413. + vchan_init(&c->vc, &d->ddev);
  15414. + INIT_LIST_HEAD(&c->node);
  15415. +
  15416. + d->ddev.chancnt++;
  15417. +
  15418. + c->chan_base = chan_base;
  15419. + c->ch = chan_id;
  15420. + c->irq_number = irq;
  15421. +
  15422. + return 0;
  15423. +}
  15424. +
  15425. +
  15426. +static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  15427. +{
  15428. + struct bcm2835_chan *c, *next;
  15429. +
  15430. + list_for_each_entry_safe(c, next, &od->ddev.channels,
  15431. + vc.chan.device_node) {
  15432. + list_del(&c->vc.chan.device_node);
  15433. + tasklet_kill(&c->vc.task);
  15434. + }
  15435. +}
  15436. +
  15437. +static const struct of_device_id bcm2835_dma_of_match[] = {
  15438. + { .compatible = "brcm,bcm2835-dma", },
  15439. + {},
  15440. +};
  15441. +MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  15442. +
  15443. +#ifdef CONFIG_ARCH_BCM2835
  15444. +static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  15445. + struct of_dma *ofdma)
  15446. +{
  15447. + struct bcm2835_dmadev *d = ofdma->of_dma_data;
  15448. + struct dma_chan *chan;
  15449. +
  15450. + chan = dma_get_any_slave_channel(&d->ddev);
  15451. + if (!chan)
  15452. + return NULL;
  15453. +
  15454. + /* Set DREQ from param */
  15455. + to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  15456. +
  15457. + return chan;
  15458. +}
  15459. +#endif
  15460. +
  15461. +static int bcm2835_dma_device_slave_caps(struct dma_chan *dchan,
  15462. + struct dma_slave_caps *caps)
  15463. +{
  15464. + caps->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  15465. + caps->dstn_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  15466. + caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  15467. + caps->cmd_pause = false;
  15468. + caps->cmd_terminate = true;
  15469. +
  15470. + return 0;
  15471. +}
  15472. +
  15473. +static int bcm2835_dma_probe(struct platform_device *pdev)
  15474. +{
  15475. + struct bcm2835_dmadev *od;
  15476. +#ifdef CONFIG_ARCH_BCM2835
  15477. + struct resource *res;
  15478. + void __iomem *base;
  15479. + uint32_t chans_available;
  15480. +#endif
  15481. + int rc;
  15482. + int i;
  15483. + int irq;
  15484. +
  15485. +
  15486. + if (!pdev->dev.dma_mask)
  15487. + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  15488. +
  15489. + /* If CONFIG_ARCH_BCM2835 is selected, device tree is used */
  15490. + /* hence the difference between probing */
  15491. +
  15492. +#ifndef CONFIG_ARCH_BCM2835
  15493. +
  15494. + rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  15495. + if (rc)
  15496. + return rc;
  15497. + dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  15498. +
  15499. +
  15500. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  15501. + if (!od)
  15502. + return -ENOMEM;
  15503. +
  15504. + pdev->dev.dma_parms = &od->dma_parms;
  15505. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  15506. +
  15507. +
  15508. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  15509. + dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  15510. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  15511. + od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  15512. + od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  15513. + od->ddev.device_tx_status = bcm2835_dma_tx_status;
  15514. + od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  15515. + od->ddev.device_slave_caps = bcm2835_dma_device_slave_caps;
  15516. + od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  15517. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  15518. + od->ddev.device_control = bcm2835_dma_control;
  15519. + od->ddev.dev = &pdev->dev;
  15520. + INIT_LIST_HEAD(&od->ddev.channels);
  15521. + spin_lock_init(&od->lock);
  15522. +
  15523. + platform_set_drvdata(pdev, od);
  15524. +
  15525. + for (i = 0; i < 5; i++) {
  15526. + void __iomem *chan_base;
  15527. + int chan_id;
  15528. +
  15529. + chan_id = bcm_dma_chan_alloc(BCM_DMA_FEATURE_LITE,
  15530. + &chan_base,
  15531. + &irq);
  15532. +
  15533. + if (chan_id < 0)
  15534. + break;
  15535. +
  15536. + rc = bcm2708_dma_chan_init(od, chan_base, chan_id, irq);
  15537. + if (rc)
  15538. + goto err_no_dma;
  15539. + }
  15540. +#else
  15541. + rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  15542. + if (rc)
  15543. + return rc;
  15544. +
  15545. +
  15546. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  15547. + if (!od)
  15548. + return -ENOMEM;
  15549. +
  15550. + pdev->dev.dma_parms = &od->dma_parms;
  15551. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  15552. +
  15553. +
  15554. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  15555. + base = devm_ioremap_resource(&pdev->dev, res);
  15556. + if (IS_ERR(base))
  15557. + return PTR_ERR(base);
  15558. +
  15559. + od->base = base;
  15560. +
  15561. +
  15562. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  15563. + dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  15564. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  15565. + od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  15566. + od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  15567. + od->ddev.device_tx_status = bcm2835_dma_tx_status;
  15568. + od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  15569. + od->ddev.device_slave_caps = bcm2835_dma_device_slave_caps;
  15570. + od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  15571. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  15572. + od->ddev.device_control = bcm2835_dma_control;
  15573. + od->ddev.dev = &pdev->dev;
  15574. + INIT_LIST_HEAD(&od->ddev.channels);
  15575. + spin_lock_init(&od->lock);
  15576. +
  15577. + platform_set_drvdata(pdev, od);
  15578. +
  15579. +
  15580. + /* Request DMA channel mask from device tree */
  15581. + if (of_property_read_u32(pdev->dev.of_node,
  15582. + "brcm,dma-channel-mask",
  15583. + &chans_available)) {
  15584. + dev_err(&pdev->dev, "Failed to get channel mask\n");
  15585. + rc = -EINVAL;
  15586. + goto err_no_dma;
  15587. + }
  15588. +
  15589. +
  15590. + /*
  15591. + * Do not use the FIQ and BULK channels,
  15592. + * because they are used by the GPU.
  15593. + */
  15594. + chans_available &= ~(BCM2835_DMA_FIQ_MASK | BCM2835_DMA_BULK_MASK);
  15595. +
  15596. +
  15597. + for (i = 0; i < pdev->num_resources; i++) {
  15598. + irq = platform_get_irq(pdev, i);
  15599. + if (irq < 0)
  15600. + break;
  15601. +
  15602. + if (chans_available & (1 << i)) {
  15603. + rc = bcm2835_dma_chan_init(od, i, irq);
  15604. + if (rc)
  15605. + goto err_no_dma;
  15606. + }
  15607. + }
  15608. +
  15609. + dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  15610. +
  15611. + /* Device-tree DMA controller registration */
  15612. + rc = of_dma_controller_register(pdev->dev.of_node,
  15613. + bcm2835_dma_xlate, od);
  15614. + if (rc) {
  15615. + dev_err(&pdev->dev, "Failed to register DMA controller\n");
  15616. + goto err_no_dma;
  15617. + }
  15618. +#endif
  15619. +
  15620. + rc = dma_async_device_register(&od->ddev);
  15621. + if (rc) {
  15622. + dev_err(&pdev->dev,
  15623. + "Failed to register slave DMA engine device: %d\n", rc);
  15624. + goto err_no_dma;
  15625. + }
  15626. +
  15627. + dev_info(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  15628. +
  15629. + return 0;
  15630. +
  15631. +err_no_dma:
  15632. + bcm2835_dma_free(od);
  15633. + return rc;
  15634. +}
  15635. +
  15636. +static int bcm2835_dma_remove(struct platform_device *pdev)
  15637. +{
  15638. + struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  15639. +
  15640. + dma_async_device_unregister(&od->ddev);
  15641. + bcm2835_dma_free(od);
  15642. +
  15643. + return 0;
  15644. +}
  15645. +
  15646. +#ifndef CONFIG_ARCH_BCM2835
  15647. +
  15648. +
  15649. +static struct platform_driver bcm2835_dma_driver = {
  15650. + .probe = bcm2835_dma_probe,
  15651. + .remove = bcm2835_dma_remove,
  15652. + .driver = {
  15653. + .name = "bcm2708-dmaengine",
  15654. + .owner = THIS_MODULE,
  15655. + },
  15656. +};
  15657. +
  15658. +static struct platform_device *pdev;
  15659. +
  15660. +static const struct platform_device_info bcm2835_dma_dev_info = {
  15661. + .name = "bcm2708-dmaengine",
  15662. + .id = -1,
  15663. +};
  15664. +
  15665. +static int bcm2835_dma_init(void)
  15666. +{
  15667. + int rc = platform_driver_register(&bcm2835_dma_driver);
  15668. +
  15669. + if (rc == 0) {
  15670. + pdev = platform_device_register_full(&bcm2835_dma_dev_info);
  15671. + if (IS_ERR(pdev)) {
  15672. + platform_driver_unregister(&bcm2835_dma_driver);
  15673. + rc = PTR_ERR(pdev);
  15674. + }
  15675. + }
  15676. +
  15677. + return rc;
  15678. +}
  15679. +module_init(bcm2835_dma_init); /* preferable to subsys_initcall */
  15680. +
  15681. +static void __exit bcm2835_dma_exit(void)
  15682. +{
  15683. + platform_device_unregister(pdev);
  15684. + platform_driver_unregister(&bcm2835_dma_driver);
  15685. +}
  15686. +module_exit(bcm2835_dma_exit);
  15687. +
  15688. +#else
  15689. +
  15690. +static struct platform_driver bcm2835_dma_driver = {
  15691. + .probe = bcm2835_dma_probe,
  15692. + .remove = bcm2835_dma_remove,
  15693. + .driver = {
  15694. + .name = "bcm2835-dma",
  15695. + .owner = THIS_MODULE,
  15696. + .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  15697. + },
  15698. +};
  15699. +
  15700. +module_platform_driver(bcm2835_dma_driver);
  15701. +
  15702. +#endif
  15703. +
  15704. +MODULE_ALIAS("platform:bcm2835-dma");
  15705. +MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  15706. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  15707. +MODULE_AUTHOR("Gellert Weisz <gellert@raspberrypi.org>");
  15708. +MODULE_LICENSE("GPL v2");
  15709. diff -Nur linux-3.17.5/drivers/dma/Kconfig linux-rpi/drivers/dma/Kconfig
  15710. --- linux-3.17.5/drivers/dma/Kconfig 2014-12-06 17:57:59.000000000 -0600
  15711. +++ linux-rpi/drivers/dma/Kconfig 2014-12-11 14:05:37.560418001 -0600
  15712. @@ -330,6 +330,12 @@
  15713. select DMA_ENGINE
  15714. select DMA_VIRTUAL_CHANNELS
  15715. +config DMA_BCM2708
  15716. + tristate "BCM2708 DMA engine support"
  15717. + depends on MACH_BCM2708
  15718. + select DMA_ENGINE
  15719. + select DMA_VIRTUAL_CHANNELS
  15720. +
  15721. config TI_CPPI41
  15722. tristate "AM33xx CPPI41 DMA support"
  15723. depends on ARCH_OMAP
  15724. diff -Nur linux-3.17.5/drivers/dma/Makefile linux-rpi/drivers/dma/Makefile
  15725. --- linux-3.17.5/drivers/dma/Makefile 2014-12-06 17:57:59.000000000 -0600
  15726. +++ linux-rpi/drivers/dma/Makefile 2014-12-11 14:05:37.560418001 -0600
  15727. @@ -39,6 +39,7 @@
  15728. obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o
  15729. obj-$(CONFIG_DMA_OMAP) += omap-dma.o
  15730. obj-$(CONFIG_DMA_BCM2835) += bcm2835-dma.o
  15731. +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
  15732. obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
  15733. obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
  15734. obj-$(CONFIG_TI_CPPI41) += cppi41.o
  15735. diff -Nur linux-3.17.5/drivers/hid/usbhid/hid-core.c linux-rpi/drivers/hid/usbhid/hid-core.c
  15736. --- linux-3.17.5/drivers/hid/usbhid/hid-core.c 2014-12-06 17:57:59.000000000 -0600
  15737. +++ linux-rpi/drivers/hid/usbhid/hid-core.c 2014-12-11 14:05:37.764418001 -0600
  15738. @@ -49,7 +49,7 @@
  15739. * Module parameters.
  15740. */
  15741. -static unsigned int hid_mousepoll_interval;
  15742. +static unsigned int hid_mousepoll_interval = ~0;
  15743. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  15744. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  15745. @@ -1095,8 +1095,12 @@
  15746. }
  15747. /* Change the polling interval of mice. */
  15748. - if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  15749. - interval = hid_mousepoll_interval;
  15750. + if (hid->collection->usage == HID_GD_MOUSE) {
  15751. + if (hid_mousepoll_interval == ~0 && interval < 16)
  15752. + interval = 16;
  15753. + else if (hid_mousepoll_interval != ~0 && hid_mousepoll_interval != 0)
  15754. + interval = hid_mousepoll_interval;
  15755. + }
  15756. ret = -ENOMEM;
  15757. if (usb_endpoint_dir_in(endpoint)) {
  15758. diff -Nur linux-3.17.5/drivers/hwmon/bcm2835-hwmon.c linux-rpi/drivers/hwmon/bcm2835-hwmon.c
  15759. --- linux-3.17.5/drivers/hwmon/bcm2835-hwmon.c 1969-12-31 18:00:00.000000000 -0600
  15760. +++ linux-rpi/drivers/hwmon/bcm2835-hwmon.c 2014-12-11 14:02:53.048418001 -0600
  15761. @@ -0,0 +1,219 @@
  15762. +/*****************************************************************************
  15763. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  15764. +*
  15765. +* Unless you and Broadcom execute a separate written software license
  15766. +* agreement governing use of this software, this software is licensed to you
  15767. +* under the terms of the GNU General Public License version 2, available at
  15768. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  15769. +*
  15770. +* Notwithstanding the above, under no circumstances may you combine this
  15771. +* software in any way with any other Broadcom software provided under a
  15772. +* license other than the GPL, without Broadcom's express prior written
  15773. +* consent.
  15774. +*****************************************************************************/
  15775. +
  15776. +#include <linux/kernel.h>
  15777. +#include <linux/module.h>
  15778. +#include <linux/init.h>
  15779. +#include <linux/hwmon.h>
  15780. +#include <linux/hwmon-sysfs.h>
  15781. +#include <linux/platform_device.h>
  15782. +#include <linux/sysfs.h>
  15783. +#include <mach/vcio.h>
  15784. +#include <linux/slab.h>
  15785. +#include <linux/err.h>
  15786. +
  15787. +#define MODULE_NAME "bcm2835_hwmon"
  15788. +
  15789. +/*#define HWMON_DEBUG_ENABLE*/
  15790. +
  15791. +#ifdef HWMON_DEBUG_ENABLE
  15792. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  15793. +#else
  15794. +#define print_debug(fmt,...)
  15795. +#endif
  15796. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  15797. +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
  15798. +
  15799. +#define VC_TAG_GET_TEMP 0x00030006
  15800. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  15801. +
  15802. +/* --- STRUCTS --- */
  15803. +struct bcm2835_hwmon_data {
  15804. + struct device *hwmon_dev;
  15805. +};
  15806. +
  15807. +/* tag part of the message */
  15808. +struct vc_msg_tag {
  15809. + uint32_t tag_id; /* the tag ID for the temperature */
  15810. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  15811. + uint32_t request_code; /* identifies message as a request (should be 0) */
  15812. + uint32_t id; /* extra ID field (should be 0) */
  15813. + uint32_t val; /* returned value of the temperature */
  15814. +};
  15815. +
  15816. +/* message structure to be sent to videocore */
  15817. +struct vc_msg {
  15818. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  15819. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  15820. + struct vc_msg_tag tag; /* the tag structure above to make */
  15821. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  15822. +};
  15823. +
  15824. +typedef enum {
  15825. + TEMP,
  15826. + MAX_TEMP,
  15827. +} temp_type;
  15828. +
  15829. +/* --- PROTOTYPES --- */
  15830. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
  15831. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
  15832. +
  15833. +/* --- GLOBALS --- */
  15834. +
  15835. +static struct bcm2835_hwmon_data *bcm2835_data;
  15836. +static struct platform_driver bcm2835_hwmon_driver;
  15837. +
  15838. +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
  15839. +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
  15840. +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
  15841. +
  15842. +static struct attribute* bcm2835_attributes[] = {
  15843. + &sensor_dev_attr_name.dev_attr.attr,
  15844. + &sensor_dev_attr_temp1_input.dev_attr.attr,
  15845. + &sensor_dev_attr_temp1_max.dev_attr.attr,
  15846. + NULL,
  15847. +};
  15848. +
  15849. +static struct attribute_group bcm2835_attr_group = {
  15850. + .attrs = bcm2835_attributes,
  15851. +};
  15852. +
  15853. +/* --- FUNCTIONS --- */
  15854. +
  15855. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
  15856. +{
  15857. + return sprintf(buf,"bcm2835_hwmon\n");
  15858. +}
  15859. +
  15860. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
  15861. +{
  15862. + struct vc_msg msg;
  15863. + int result;
  15864. + uint temp = 0;
  15865. + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
  15866. +
  15867. + print_debug("IN");
  15868. +
  15869. + /* wipe all previous message data */
  15870. + memset(&msg, 0, sizeof msg);
  15871. +
  15872. + /* determine the message type */
  15873. + if(index == TEMP)
  15874. + msg.tag.tag_id = VC_TAG_GET_TEMP;
  15875. + else if (index == MAX_TEMP)
  15876. + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
  15877. + else
  15878. + {
  15879. + print_debug("Unknown temperature message!");
  15880. + return -EINVAL;
  15881. + }
  15882. +
  15883. + msg.msg_size = sizeof msg;
  15884. + msg.tag.buffer_size = 8;
  15885. +
  15886. + /* send the message */
  15887. + result = bcm_mailbox_property(&msg, sizeof msg);
  15888. +
  15889. + /* check if it was all ok and return the rate in milli degrees C */
  15890. + if (result == 0 && (msg.request_code & 0x80000000))
  15891. + temp = (uint)msg.tag.val;
  15892. + #ifdef HWMON_DEBUG_ENABLE
  15893. + else
  15894. + print_debug("Failed to get temperature!");
  15895. + #endif
  15896. + print_debug("Got temperature as %u",temp);
  15897. + print_debug("OUT");
  15898. + return sprintf(buf, "%u\n", temp);
  15899. +}
  15900. +
  15901. +
  15902. +static int bcm2835_hwmon_probe(struct platform_device *pdev)
  15903. +{
  15904. + int err;
  15905. +
  15906. + print_debug("IN");
  15907. + print_debug("HWMON Driver has been probed!");
  15908. +
  15909. + /* check that the device isn't null!*/
  15910. + if(pdev == NULL)
  15911. + {
  15912. + print_debug("Platform device is empty!");
  15913. + return -ENODEV;
  15914. + }
  15915. +
  15916. + /* allocate memory for neccessary data */
  15917. + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
  15918. + if(!bcm2835_data)
  15919. + {
  15920. + print_debug("Unable to allocate memory for hwmon data!");
  15921. + err = -ENOMEM;
  15922. + goto kzalloc_error;
  15923. + }
  15924. +
  15925. + /* create the sysfs files */
  15926. + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
  15927. + {
  15928. + print_debug("Unable to create sysfs files!");
  15929. + err = -EFAULT;
  15930. + goto sysfs_error;
  15931. + }
  15932. +
  15933. + /* register the hwmon device */
  15934. + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  15935. + if (IS_ERR(bcm2835_data->hwmon_dev))
  15936. + {
  15937. + err = PTR_ERR(bcm2835_data->hwmon_dev);
  15938. + goto hwmon_error;
  15939. + }
  15940. + print_debug("OUT");
  15941. + return 0;
  15942. +
  15943. + /* error goto's */
  15944. + hwmon_error:
  15945. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  15946. +
  15947. + sysfs_error:
  15948. + kfree(bcm2835_data);
  15949. +
  15950. + kzalloc_error:
  15951. +
  15952. + return err;
  15953. +
  15954. +}
  15955. +
  15956. +static int bcm2835_hwmon_remove(struct platform_device *pdev)
  15957. +{
  15958. + print_debug("IN");
  15959. + hwmon_device_unregister(bcm2835_data->hwmon_dev);
  15960. +
  15961. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  15962. + print_debug("OUT");
  15963. + return 0;
  15964. +}
  15965. +
  15966. +/* Hwmon Driver */
  15967. +static struct platform_driver bcm2835_hwmon_driver = {
  15968. + .probe = bcm2835_hwmon_probe,
  15969. + .remove = bcm2835_hwmon_remove,
  15970. + .driver = {
  15971. + .name = "bcm2835_hwmon",
  15972. + .owner = THIS_MODULE,
  15973. + },
  15974. +};
  15975. +
  15976. +MODULE_LICENSE("GPL");
  15977. +MODULE_AUTHOR("Dorian Peake");
  15978. +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
  15979. +
  15980. +module_platform_driver(bcm2835_hwmon_driver);
  15981. diff -Nur linux-3.17.5/drivers/hwmon/Kconfig linux-rpi/drivers/hwmon/Kconfig
  15982. --- linux-3.17.5/drivers/hwmon/Kconfig 2014-12-06 17:57:59.000000000 -0600
  15983. +++ linux-rpi/drivers/hwmon/Kconfig 2014-12-11 14:05:37.768418001 -0600
  15984. @@ -1669,6 +1669,16 @@
  15985. This driver provides support for the Ultra45 workstation environmental
  15986. sensors.
  15987. +config SENSORS_BCM2835
  15988. + depends on THERMAL_BCM2835=n
  15989. + tristate "Broadcom BCM2835 HWMON Driver"
  15990. + help
  15991. + If you say yes here you get support for the hardware
  15992. + monitoring features of the BCM2835 Chip
  15993. +
  15994. + This driver can also be built as a module. If so, the module
  15995. + will be called bcm2835-hwmon.
  15996. +
  15997. if ACPI
  15998. comment "ACPI drivers"
  15999. diff -Nur linux-3.17.5/drivers/hwmon/Makefile linux-rpi/drivers/hwmon/Makefile
  16000. --- linux-3.17.5/drivers/hwmon/Makefile 2014-12-06 17:57:59.000000000 -0600
  16001. +++ linux-rpi/drivers/hwmon/Makefile 2014-12-11 14:05:37.768418001 -0600
  16002. @@ -152,6 +152,7 @@
  16003. obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
  16004. obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
  16005. obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
  16006. +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
  16007. obj-$(CONFIG_PMBUS) += pmbus/
  16008. diff -Nur linux-3.17.5/drivers/i2c/busses/i2c-bcm2708.c linux-rpi/drivers/i2c/busses/i2c-bcm2708.c
  16009. --- linux-3.17.5/drivers/i2c/busses/i2c-bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  16010. +++ linux-rpi/drivers/i2c/busses/i2c-bcm2708.c 2014-12-11 14:05:37.796418001 -0600
  16011. @@ -0,0 +1,472 @@
  16012. +/*
  16013. + * Driver for Broadcom BCM2708 BSC Controllers
  16014. + *
  16015. + * Copyright (C) 2012 Chris Boot & Frank Buss
  16016. + *
  16017. + * This driver is inspired by:
  16018. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  16019. + *
  16020. + * This program is free software; you can redistribute it and/or modify
  16021. + * it under the terms of the GNU General Public License as published by
  16022. + * the Free Software Foundation; either version 2 of the License, or
  16023. + * (at your option) any later version.
  16024. + *
  16025. + * This program is distributed in the hope that it will be useful,
  16026. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16027. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16028. + * GNU General Public License for more details.
  16029. + *
  16030. + * You should have received a copy of the GNU General Public License
  16031. + * along with this program; if not, write to the Free Software
  16032. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16033. + */
  16034. +
  16035. +#include <linux/kernel.h>
  16036. +#include <linux/module.h>
  16037. +#include <linux/spinlock.h>
  16038. +#include <linux/clk.h>
  16039. +#include <linux/err.h>
  16040. +#include <linux/of.h>
  16041. +#include <linux/platform_device.h>
  16042. +#include <linux/io.h>
  16043. +#include <linux/slab.h>
  16044. +#include <linux/i2c.h>
  16045. +#include <linux/interrupt.h>
  16046. +#include <linux/sched.h>
  16047. +#include <linux/wait.h>
  16048. +
  16049. +/* BSC register offsets */
  16050. +#define BSC_C 0x00
  16051. +#define BSC_S 0x04
  16052. +#define BSC_DLEN 0x08
  16053. +#define BSC_A 0x0c
  16054. +#define BSC_FIFO 0x10
  16055. +#define BSC_DIV 0x14
  16056. +#define BSC_DEL 0x18
  16057. +#define BSC_CLKT 0x1c
  16058. +
  16059. +/* Bitfields in BSC_C */
  16060. +#define BSC_C_I2CEN 0x00008000
  16061. +#define BSC_C_INTR 0x00000400
  16062. +#define BSC_C_INTT 0x00000200
  16063. +#define BSC_C_INTD 0x00000100
  16064. +#define BSC_C_ST 0x00000080
  16065. +#define BSC_C_CLEAR_1 0x00000020
  16066. +#define BSC_C_CLEAR_2 0x00000010
  16067. +#define BSC_C_READ 0x00000001
  16068. +
  16069. +/* Bitfields in BSC_S */
  16070. +#define BSC_S_CLKT 0x00000200
  16071. +#define BSC_S_ERR 0x00000100
  16072. +#define BSC_S_RXF 0x00000080
  16073. +#define BSC_S_TXE 0x00000040
  16074. +#define BSC_S_RXD 0x00000020
  16075. +#define BSC_S_TXD 0x00000010
  16076. +#define BSC_S_RXR 0x00000008
  16077. +#define BSC_S_TXW 0x00000004
  16078. +#define BSC_S_DONE 0x00000002
  16079. +#define BSC_S_TA 0x00000001
  16080. +
  16081. +#define I2C_TIMEOUT_MS 150
  16082. +
  16083. +#define DRV_NAME "bcm2708_i2c"
  16084. +
  16085. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  16086. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  16087. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  16088. +
  16089. +static bool combined = false;
  16090. +module_param(combined, bool, 0644);
  16091. +MODULE_PARM_DESC(combined, "Use combined transactions");
  16092. +
  16093. +struct bcm2708_i2c {
  16094. + struct i2c_adapter adapter;
  16095. +
  16096. + spinlock_t lock;
  16097. + void __iomem *base;
  16098. + int irq;
  16099. + struct clk *clk;
  16100. +
  16101. + struct completion done;
  16102. +
  16103. + struct i2c_msg *msg;
  16104. + int pos;
  16105. + int nmsgs;
  16106. + bool error;
  16107. +};
  16108. +
  16109. +/*
  16110. + * This function sets the ALT mode on the I2C pins so that we can use them with
  16111. + * the BSC hardware.
  16112. + *
  16113. + * FIXME: This is a hack. Use pinmux / pinctrl.
  16114. + */
  16115. +static void bcm2708_i2c_init_pinmode(int id)
  16116. +{
  16117. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  16118. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  16119. +
  16120. + int pin;
  16121. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  16122. +
  16123. + BUG_ON(id != 0 && id != 1);
  16124. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  16125. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  16126. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  16127. + INP_GPIO(pin); /* set mode to GPIO input first */
  16128. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  16129. + }
  16130. +
  16131. + iounmap(gpio);
  16132. +
  16133. +#undef INP_GPIO
  16134. +#undef SET_GPIO_ALT
  16135. +}
  16136. +
  16137. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  16138. +{
  16139. + return readl(bi->base + reg);
  16140. +}
  16141. +
  16142. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  16143. +{
  16144. + writel(val, bi->base + reg);
  16145. +}
  16146. +
  16147. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  16148. +{
  16149. + bcm2708_wr(bi, BSC_C, 0);
  16150. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  16151. +}
  16152. +
  16153. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  16154. +{
  16155. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  16156. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  16157. +}
  16158. +
  16159. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  16160. +{
  16161. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  16162. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  16163. +}
  16164. +
  16165. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  16166. +{
  16167. + unsigned long bus_hz;
  16168. + u32 cdiv, s;
  16169. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  16170. +
  16171. + bus_hz = clk_get_rate(bi->clk);
  16172. + cdiv = bus_hz / baudrate;
  16173. + if (cdiv > 0xffff)
  16174. + cdiv = 0xffff;
  16175. +
  16176. + if (bi->msg->flags & I2C_M_RD)
  16177. + c |= BSC_C_INTR | BSC_C_READ;
  16178. + else
  16179. + c |= BSC_C_INTT;
  16180. +
  16181. + bcm2708_wr(bi, BSC_DIV, cdiv);
  16182. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  16183. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  16184. + if (combined)
  16185. + {
  16186. + /* Do the next two messages meet combined transaction criteria?
  16187. + - Current message is a write, next message is a read
  16188. + - Both messages to same slave address
  16189. + - Write message can fit inside FIFO (16 bytes or less) */
  16190. + if ( (bi->nmsgs > 1) &&
  16191. + !(bi->msg[0].flags & I2C_M_RD) && (bi->msg[1].flags & I2C_M_RD) &&
  16192. + (bi->msg[0].addr == bi->msg[1].addr) && (bi->msg[0].len <= 16)) {
  16193. + /* Fill FIFO with entire write message (16 byte FIFO) */
  16194. + while (bi->pos < bi->msg->len)
  16195. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  16196. + /* Start write transfer (no interrupts, don't clear FIFO) */
  16197. + bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
  16198. + /* poll for transfer start bit (should only take 1-20 polls) */
  16199. + do {
  16200. + s = bcm2708_rd(bi, BSC_S);
  16201. + } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)));
  16202. + /* Send next read message before the write transfer finishes. */
  16203. + bi->nmsgs--;
  16204. + bi->msg++;
  16205. + bi->pos = 0;
  16206. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  16207. + c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_INTR | BSC_C_ST | BSC_C_READ;
  16208. + }
  16209. + }
  16210. + bcm2708_wr(bi, BSC_C, c);
  16211. +}
  16212. +
  16213. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  16214. +{
  16215. + struct bcm2708_i2c *bi = dev_id;
  16216. + bool handled = true;
  16217. + u32 s;
  16218. +
  16219. + spin_lock(&bi->lock);
  16220. +
  16221. + /* we may see camera interrupts on the "other" I2C channel
  16222. + Just return if we've not sent anything */
  16223. + if (!bi->nmsgs || !bi->msg )
  16224. + goto early_exit;
  16225. +
  16226. + s = bcm2708_rd(bi, BSC_S);
  16227. +
  16228. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  16229. + bcm2708_bsc_reset(bi);
  16230. + bi->error = true;
  16231. +
  16232. + /* wake up our bh */
  16233. + complete(&bi->done);
  16234. + } else if (s & BSC_S_DONE) {
  16235. + bi->nmsgs--;
  16236. +
  16237. + if (bi->msg->flags & I2C_M_RD)
  16238. + bcm2708_bsc_fifo_drain(bi);
  16239. +
  16240. + bcm2708_bsc_reset(bi);
  16241. +
  16242. + if (bi->nmsgs) {
  16243. + /* advance to next message */
  16244. + bi->msg++;
  16245. + bi->pos = 0;
  16246. + bcm2708_bsc_setup(bi);
  16247. + } else {
  16248. + /* wake up our bh */
  16249. + complete(&bi->done);
  16250. + }
  16251. + } else if (s & BSC_S_TXW) {
  16252. + bcm2708_bsc_fifo_fill(bi);
  16253. + } else if (s & BSC_S_RXR) {
  16254. + bcm2708_bsc_fifo_drain(bi);
  16255. + } else {
  16256. + handled = false;
  16257. + }
  16258. +
  16259. +early_exit:
  16260. + spin_unlock(&bi->lock);
  16261. +
  16262. + return handled ? IRQ_HANDLED : IRQ_NONE;
  16263. +}
  16264. +
  16265. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  16266. + struct i2c_msg *msgs, int num)
  16267. +{
  16268. + struct bcm2708_i2c *bi = adap->algo_data;
  16269. + unsigned long flags;
  16270. + int ret;
  16271. +
  16272. + spin_lock_irqsave(&bi->lock, flags);
  16273. +
  16274. + reinit_completion(&bi->done);
  16275. + bi->msg = msgs;
  16276. + bi->pos = 0;
  16277. + bi->nmsgs = num;
  16278. + bi->error = false;
  16279. +
  16280. + spin_unlock_irqrestore(&bi->lock, flags);
  16281. +
  16282. + bcm2708_bsc_setup(bi);
  16283. +
  16284. + ret = wait_for_completion_timeout(&bi->done,
  16285. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  16286. + if (ret == 0) {
  16287. + dev_err(&adap->dev, "transfer timed out\n");
  16288. + spin_lock_irqsave(&bi->lock, flags);
  16289. + bcm2708_bsc_reset(bi);
  16290. + spin_unlock_irqrestore(&bi->lock, flags);
  16291. + return -ETIMEDOUT;
  16292. + }
  16293. +
  16294. + return bi->error ? -EIO : num;
  16295. +}
  16296. +
  16297. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  16298. +{
  16299. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  16300. +}
  16301. +
  16302. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  16303. + .master_xfer = bcm2708_i2c_master_xfer,
  16304. + .functionality = bcm2708_i2c_functionality,
  16305. +};
  16306. +
  16307. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  16308. +{
  16309. + struct resource *regs;
  16310. + int irq, err = -ENOMEM;
  16311. + struct clk *clk;
  16312. + struct bcm2708_i2c *bi;
  16313. + struct i2c_adapter *adap;
  16314. + unsigned long bus_hz;
  16315. + u32 cdiv;
  16316. +
  16317. + if (pdev->dev.of_node) {
  16318. + u32 bus_clk_rate;
  16319. + pdev->id = of_alias_get_id(pdev->dev.of_node, "i2c");
  16320. + if (pdev->id < 0) {
  16321. + dev_err(&pdev->dev, "alias is missing\n");
  16322. + return -EINVAL;
  16323. + }
  16324. + if (!of_property_read_u32(pdev->dev.of_node,
  16325. + "clock-frequency", &bus_clk_rate))
  16326. + baudrate = bus_clk_rate;
  16327. + else
  16328. + dev_warn(&pdev->dev,
  16329. + "Could not read clock-frequency property\n");
  16330. + }
  16331. +
  16332. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  16333. + if (!regs) {
  16334. + dev_err(&pdev->dev, "could not get IO memory\n");
  16335. + return -ENXIO;
  16336. + }
  16337. +
  16338. + irq = platform_get_irq(pdev, 0);
  16339. + if (irq < 0) {
  16340. + dev_err(&pdev->dev, "could not get IRQ\n");
  16341. + return irq;
  16342. + }
  16343. +
  16344. + clk = clk_get(&pdev->dev, NULL);
  16345. + if (IS_ERR(clk)) {
  16346. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  16347. + return PTR_ERR(clk);
  16348. + }
  16349. +
  16350. + bcm2708_i2c_init_pinmode(pdev->id);
  16351. +
  16352. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  16353. + if (!bi)
  16354. + goto out_clk_put;
  16355. +
  16356. + platform_set_drvdata(pdev, bi);
  16357. +
  16358. + adap = &bi->adapter;
  16359. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  16360. + adap->algo = &bcm2708_i2c_algorithm;
  16361. + adap->algo_data = bi;
  16362. + adap->dev.parent = &pdev->dev;
  16363. + adap->nr = pdev->id;
  16364. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  16365. + adap->dev.of_node = pdev->dev.of_node;
  16366. +
  16367. + switch (pdev->id) {
  16368. + case 0:
  16369. + adap->class = I2C_CLASS_HWMON;
  16370. + break;
  16371. + case 1:
  16372. + adap->class = I2C_CLASS_DDC;
  16373. + break;
  16374. + default:
  16375. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  16376. + err = -ENXIO;
  16377. + goto out_free_bi;
  16378. + }
  16379. +
  16380. + spin_lock_init(&bi->lock);
  16381. + init_completion(&bi->done);
  16382. +
  16383. + bi->base = ioremap(regs->start, resource_size(regs));
  16384. + if (!bi->base) {
  16385. + dev_err(&pdev->dev, "could not remap memory\n");
  16386. + goto out_free_bi;
  16387. + }
  16388. +
  16389. + bi->irq = irq;
  16390. + bi->clk = clk;
  16391. +
  16392. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  16393. + dev_name(&pdev->dev), bi);
  16394. + if (err) {
  16395. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  16396. + goto out_iounmap;
  16397. + }
  16398. +
  16399. + bcm2708_bsc_reset(bi);
  16400. +
  16401. + err = i2c_add_numbered_adapter(adap);
  16402. + if (err < 0) {
  16403. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  16404. + goto out_free_irq;
  16405. + }
  16406. +
  16407. + bus_hz = clk_get_rate(bi->clk);
  16408. + cdiv = bus_hz / baudrate;
  16409. + if (cdiv > 0xffff) {
  16410. + cdiv = 0xffff;
  16411. + baudrate = bus_hz / cdiv;
  16412. + }
  16413. +
  16414. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
  16415. + pdev->id, (unsigned long)regs->start, irq, baudrate);
  16416. +
  16417. + return 0;
  16418. +
  16419. +out_free_irq:
  16420. + free_irq(bi->irq, bi);
  16421. +out_iounmap:
  16422. + iounmap(bi->base);
  16423. +out_free_bi:
  16424. + kfree(bi);
  16425. +out_clk_put:
  16426. + clk_put(clk);
  16427. + return err;
  16428. +}
  16429. +
  16430. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  16431. +{
  16432. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  16433. +
  16434. + platform_set_drvdata(pdev, NULL);
  16435. +
  16436. + i2c_del_adapter(&bi->adapter);
  16437. + free_irq(bi->irq, bi);
  16438. + iounmap(bi->base);
  16439. + clk_disable(bi->clk);
  16440. + clk_put(bi->clk);
  16441. + kfree(bi);
  16442. +
  16443. + return 0;
  16444. +}
  16445. +
  16446. +static const struct of_device_id bcm2708_i2c_of_match[] = {
  16447. + { .compatible = "brcm,bcm2708-i2c" },
  16448. + {},
  16449. +};
  16450. +MODULE_DEVICE_TABLE(of, bcm2708_i2c_of_match);
  16451. +
  16452. +static struct platform_driver bcm2708_i2c_driver = {
  16453. + .driver = {
  16454. + .name = DRV_NAME,
  16455. + .owner = THIS_MODULE,
  16456. + .of_match_table = bcm2708_i2c_of_match,
  16457. + },
  16458. + .probe = bcm2708_i2c_probe,
  16459. + .remove = bcm2708_i2c_remove,
  16460. +};
  16461. +
  16462. +// module_platform_driver(bcm2708_i2c_driver);
  16463. +
  16464. +
  16465. +static int __init bcm2708_i2c_init(void)
  16466. +{
  16467. + return platform_driver_register(&bcm2708_i2c_driver);
  16468. +}
  16469. +
  16470. +static void __exit bcm2708_i2c_exit(void)
  16471. +{
  16472. + platform_driver_unregister(&bcm2708_i2c_driver);
  16473. +}
  16474. +
  16475. +module_init(bcm2708_i2c_init);
  16476. +module_exit(bcm2708_i2c_exit);
  16477. +
  16478. +
  16479. +
  16480. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  16481. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  16482. +MODULE_LICENSE("GPL v2");
  16483. +MODULE_ALIAS("platform:" DRV_NAME);
  16484. diff -Nur linux-3.17.5/drivers/i2c/busses/Kconfig linux-rpi/drivers/i2c/busses/Kconfig
  16485. --- linux-3.17.5/drivers/i2c/busses/Kconfig 2014-12-06 17:57:59.000000000 -0600
  16486. +++ linux-rpi/drivers/i2c/busses/Kconfig 2014-12-11 14:05:37.796418001 -0600
  16487. @@ -339,7 +339,7 @@
  16488. config I2C_BCM2835
  16489. tristate "Broadcom BCM2835 I2C controller"
  16490. - depends on ARCH_BCM2835
  16491. + depends on ARCH_BCM2835 || ARCH_BCM2708
  16492. help
  16493. If you say yes to this option, support will be included for the
  16494. BCM2835 I2C controller.
  16495. @@ -349,6 +349,25 @@
  16496. This support is also available as a module. If so, the module
  16497. will be called i2c-bcm2835.
  16498. +config I2C_BCM2708
  16499. + tristate "BCM2708 BSC"
  16500. + depends on MACH_BCM2708
  16501. + help
  16502. + Enabling this option will add BSC (Broadcom Serial Controller)
  16503. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  16504. + with I2C/TWI/SMBus.
  16505. +
  16506. +config I2C_BCM2708_BAUDRATE
  16507. + prompt "BCM2708 I2C baudrate"
  16508. + depends on I2C_BCM2708
  16509. + int
  16510. + default 100000
  16511. + help
  16512. + Set the I2C baudrate. This will alter the default value. A
  16513. + different baudrate can be set by using a module parameter as well. If
  16514. + no parameter is provided when loading, this is the value that will be
  16515. + used.
  16516. +
  16517. config I2C_BCM_KONA
  16518. tristate "BCM Kona I2C adapter"
  16519. depends on ARCH_BCM_MOBILE
  16520. diff -Nur linux-3.17.5/drivers/i2c/busses/Makefile linux-rpi/drivers/i2c/busses/Makefile
  16521. --- linux-3.17.5/drivers/i2c/busses/Makefile 2014-12-06 17:57:59.000000000 -0600
  16522. +++ linux-rpi/drivers/i2c/busses/Makefile 2014-12-11 14:05:37.796418001 -0600
  16523. @@ -32,6 +32,7 @@
  16524. obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  16525. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  16526. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  16527. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  16528. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  16529. obj-$(CONFIG_I2C_CADENCE) += i2c-cadence.o
  16530. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  16531. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/bcm2835-camera.c linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c
  16532. --- linux-3.17.5/drivers/media/platform/bcm2835/bcm2835-camera.c 1969-12-31 18:00:00.000000000 -0600
  16533. +++ linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c 2014-12-11 14:05:38.032418001 -0600
  16534. @@ -0,0 +1,1824 @@
  16535. +/*
  16536. + * Broadcom BM2835 V4L2 driver
  16537. + *
  16538. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  16539. + *
  16540. + * This file is subject to the terms and conditions of the GNU General Public
  16541. + * License. See the file COPYING in the main directory of this archive
  16542. + * for more details.
  16543. + *
  16544. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  16545. + * Dave Stevenson <dsteve@broadcom.com>
  16546. + * Simon Mellor <simellor@broadcom.com>
  16547. + * Luke Diamand <luked@broadcom.com>
  16548. + */
  16549. +
  16550. +#include <linux/errno.h>
  16551. +#include <linux/kernel.h>
  16552. +#include <linux/module.h>
  16553. +#include <linux/slab.h>
  16554. +#include <media/videobuf2-vmalloc.h>
  16555. +#include <media/videobuf2-dma-contig.h>
  16556. +#include <media/v4l2-device.h>
  16557. +#include <media/v4l2-ioctl.h>
  16558. +#include <media/v4l2-ctrls.h>
  16559. +#include <media/v4l2-fh.h>
  16560. +#include <media/v4l2-event.h>
  16561. +#include <media/v4l2-common.h>
  16562. +#include <linux/delay.h>
  16563. +
  16564. +#include "mmal-common.h"
  16565. +#include "mmal-encodings.h"
  16566. +#include "mmal-vchiq.h"
  16567. +#include "mmal-msg.h"
  16568. +#include "mmal-parameters.h"
  16569. +#include "bcm2835-camera.h"
  16570. +
  16571. +#define BM2835_MMAL_VERSION "0.0.2"
  16572. +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
  16573. +#define MIN_WIDTH 16
  16574. +#define MIN_HEIGHT 16
  16575. +#define MAX_WIDTH 2592
  16576. +#define MAX_HEIGHT 1944
  16577. +#define MIN_BUFFER_SIZE (80*1024)
  16578. +
  16579. +#define MAX_VIDEO_MODE_WIDTH 1280
  16580. +#define MAX_VIDEO_MODE_HEIGHT 720
  16581. +
  16582. +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
  16583. +MODULE_AUTHOR("Vincent Sanders");
  16584. +MODULE_LICENSE("GPL");
  16585. +MODULE_VERSION(BM2835_MMAL_VERSION);
  16586. +
  16587. +int bcm2835_v4l2_debug;
  16588. +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
  16589. +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
  16590. +
  16591. +int max_video_width = MAX_VIDEO_MODE_WIDTH;
  16592. +int max_video_height = MAX_VIDEO_MODE_HEIGHT;
  16593. +module_param(max_video_width, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  16594. +MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
  16595. +module_param(max_video_height, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  16596. +MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
  16597. +
  16598. +/* Gstreamer bug https://bugzilla.gnome.org/show_bug.cgi?id=726521
  16599. + * v4l2src does bad (and actually wrong) things when the vidioc_enum_framesizes
  16600. + * function says type V4L2_FRMSIZE_TYPE_STEPWISE, which we do by default.
  16601. + * It's happier if we just don't say anything at all, when it then
  16602. + * sets up a load of defaults that it thinks might work.
  16603. + * If gst_v4l2src_is_broken is non-zero, then we remove the function from
  16604. + * our function table list (actually switch to an alternate set, but same
  16605. + * result).
  16606. + */
  16607. +int gst_v4l2src_is_broken = 0;
  16608. +module_param(gst_v4l2src_is_broken, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  16609. +MODULE_PARM_DESC(gst_v4l2src_is_broken, "If non-zero, enable workaround for Gstreamer");
  16610. +
  16611. +static struct bm2835_mmal_dev *gdev; /* global device data */
  16612. +
  16613. +#define FPS_MIN 1
  16614. +#define FPS_MAX 90
  16615. +
  16616. +/* timeperframe: min/max and default */
  16617. +static const struct v4l2_fract
  16618. + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  16619. + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
  16620. + tpf_default = {.numerator = 1000, .denominator = 30000};
  16621. +
  16622. +/* video formats */
  16623. +static struct mmal_fmt formats[] = {
  16624. + {
  16625. + .name = "4:2:0, packed YUV",
  16626. + .fourcc = V4L2_PIX_FMT_YUV420,
  16627. + .flags = 0,
  16628. + .mmal = MMAL_ENCODING_I420,
  16629. + .depth = 12,
  16630. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16631. + },
  16632. + {
  16633. + .name = "4:2:2, packed, YUYV",
  16634. + .fourcc = V4L2_PIX_FMT_YUYV,
  16635. + .flags = 0,
  16636. + .mmal = MMAL_ENCODING_YUYV,
  16637. + .depth = 16,
  16638. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16639. + },
  16640. + {
  16641. + .name = "RGB24 (LE)",
  16642. + .fourcc = V4L2_PIX_FMT_RGB24,
  16643. + .flags = 0,
  16644. + .mmal = MMAL_ENCODING_BGR24,
  16645. + .depth = 24,
  16646. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16647. + },
  16648. + {
  16649. + .name = "JPEG",
  16650. + .fourcc = V4L2_PIX_FMT_JPEG,
  16651. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  16652. + .mmal = MMAL_ENCODING_JPEG,
  16653. + .depth = 8,
  16654. + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
  16655. + },
  16656. + {
  16657. + .name = "H264",
  16658. + .fourcc = V4L2_PIX_FMT_H264,
  16659. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  16660. + .mmal = MMAL_ENCODING_H264,
  16661. + .depth = 8,
  16662. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  16663. + },
  16664. + {
  16665. + .name = "MJPEG",
  16666. + .fourcc = V4L2_PIX_FMT_MJPEG,
  16667. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  16668. + .mmal = MMAL_ENCODING_MJPEG,
  16669. + .depth = 8,
  16670. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  16671. + },
  16672. + {
  16673. + .name = "4:2:2, packed, YVYU",
  16674. + .fourcc = V4L2_PIX_FMT_YVYU,
  16675. + .flags = 0,
  16676. + .mmal = MMAL_ENCODING_YVYU,
  16677. + .depth = 16,
  16678. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16679. + },
  16680. + {
  16681. + .name = "4:2:2, packed, VYUY",
  16682. + .fourcc = V4L2_PIX_FMT_VYUY,
  16683. + .flags = 0,
  16684. + .mmal = MMAL_ENCODING_VYUY,
  16685. + .depth = 16,
  16686. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16687. + },
  16688. + {
  16689. + .name = "4:2:2, packed, UYVY",
  16690. + .fourcc = V4L2_PIX_FMT_UYVY,
  16691. + .flags = 0,
  16692. + .mmal = MMAL_ENCODING_UYVY,
  16693. + .depth = 16,
  16694. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16695. + },
  16696. + {
  16697. + .name = "4:2:0, packed, NV12",
  16698. + .fourcc = V4L2_PIX_FMT_NV12,
  16699. + .flags = 0,
  16700. + .mmal = MMAL_ENCODING_NV12,
  16701. + .depth = 12,
  16702. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16703. + },
  16704. + {
  16705. + .name = "RGB24 (BE)",
  16706. + .fourcc = V4L2_PIX_FMT_BGR24,
  16707. + .flags = 0,
  16708. + .mmal = MMAL_ENCODING_RGB24,
  16709. + .depth = 24,
  16710. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16711. + },
  16712. + {
  16713. + .name = "4:2:0, packed YVU",
  16714. + .fourcc = V4L2_PIX_FMT_YVU420,
  16715. + .flags = 0,
  16716. + .mmal = MMAL_ENCODING_YV12,
  16717. + .depth = 12,
  16718. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16719. + },
  16720. + {
  16721. + .name = "4:2:0, packed, NV21",
  16722. + .fourcc = V4L2_PIX_FMT_NV21,
  16723. + .flags = 0,
  16724. + .mmal = MMAL_ENCODING_NV21,
  16725. + .depth = 12,
  16726. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16727. + },
  16728. + {
  16729. + .name = "RGB32 (BE)",
  16730. + .fourcc = V4L2_PIX_FMT_BGR32,
  16731. + .flags = 0,
  16732. + .mmal = MMAL_ENCODING_BGRA,
  16733. + .depth = 32,
  16734. + .mmal_component = MMAL_COMPONENT_CAMERA,
  16735. + },
  16736. +};
  16737. +
  16738. +static struct mmal_fmt *get_format(struct v4l2_format *f)
  16739. +{
  16740. + struct mmal_fmt *fmt;
  16741. + unsigned int k;
  16742. +
  16743. + for (k = 0; k < ARRAY_SIZE(formats); k++) {
  16744. + fmt = &formats[k];
  16745. + if (fmt->fourcc == f->fmt.pix.pixelformat)
  16746. + break;
  16747. + }
  16748. +
  16749. + if (k == ARRAY_SIZE(formats))
  16750. + return NULL;
  16751. +
  16752. + return &formats[k];
  16753. +}
  16754. +
  16755. +/* ------------------------------------------------------------------
  16756. + Videobuf queue operations
  16757. + ------------------------------------------------------------------*/
  16758. +
  16759. +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  16760. + unsigned int *nbuffers, unsigned int *nplanes,
  16761. + unsigned int sizes[], void *alloc_ctxs[])
  16762. +{
  16763. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  16764. + unsigned long size;
  16765. +
  16766. + /* refuse queue setup if port is not configured */
  16767. + if (dev->capture.port == NULL) {
  16768. + v4l2_err(&dev->v4l2_dev,
  16769. + "%s: capture port not configured\n", __func__);
  16770. + return -EINVAL;
  16771. + }
  16772. +
  16773. + size = dev->capture.port->current_buffer.size;
  16774. + if (size == 0) {
  16775. + v4l2_err(&dev->v4l2_dev,
  16776. + "%s: capture port buffer size is zero\n", __func__);
  16777. + return -EINVAL;
  16778. + }
  16779. +
  16780. + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
  16781. + *nbuffers = (dev->capture.port->current_buffer.num + 2);
  16782. +
  16783. + *nplanes = 1;
  16784. +
  16785. + sizes[0] = size;
  16786. +
  16787. + /*
  16788. + * videobuf2-vmalloc allocator is context-less so no need to set
  16789. + * alloc_ctxs array.
  16790. + */
  16791. +
  16792. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  16793. + __func__, dev);
  16794. +
  16795. + return 0;
  16796. +}
  16797. +
  16798. +static int buffer_prepare(struct vb2_buffer *vb)
  16799. +{
  16800. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  16801. + unsigned long size;
  16802. +
  16803. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  16804. + __func__, dev);
  16805. +
  16806. + BUG_ON(dev->capture.port == NULL);
  16807. + BUG_ON(dev->capture.fmt == NULL);
  16808. +
  16809. + size = dev->capture.stride * dev->capture.height;
  16810. + if (vb2_plane_size(vb, 0) < size) {
  16811. + v4l2_err(&dev->v4l2_dev,
  16812. + "%s data will not fit into plane (%lu < %lu)\n",
  16813. + __func__, vb2_plane_size(vb, 0), size);
  16814. + return -EINVAL;
  16815. + }
  16816. +
  16817. + return 0;
  16818. +}
  16819. +
  16820. +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
  16821. +{
  16822. + return dev->capture.camera_port ==
  16823. + &dev->
  16824. + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
  16825. +}
  16826. +
  16827. +static void buffer_cb(struct vchiq_mmal_instance *instance,
  16828. + struct vchiq_mmal_port *port,
  16829. + int status,
  16830. + struct mmal_buffer *buf,
  16831. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
  16832. +{
  16833. + struct bm2835_mmal_dev *dev = port->cb_ctx;
  16834. +
  16835. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16836. + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
  16837. + __func__, status, buf, length, mmal_flags, pts);
  16838. +
  16839. + if (status != 0) {
  16840. + /* error in transfer */
  16841. + if (buf != NULL) {
  16842. + /* there was a buffer with the error so return it */
  16843. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  16844. + }
  16845. + return;
  16846. + } else if (length == 0) {
  16847. + /* stream ended */
  16848. + if (buf != NULL) {
  16849. + /* this should only ever happen if the port is
  16850. + * disabled and there are buffers still queued
  16851. + */
  16852. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  16853. + pr_debug("Empty buffer");
  16854. + } else if (dev->capture.frame_count) {
  16855. + /* grab another frame */
  16856. + if (is_capturing(dev)) {
  16857. + pr_debug("Grab another frame");
  16858. + vchiq_mmal_port_parameter_set(
  16859. + instance,
  16860. + dev->capture.
  16861. + camera_port,
  16862. + MMAL_PARAMETER_CAPTURE,
  16863. + &dev->capture.
  16864. + frame_count,
  16865. + sizeof(dev->capture.frame_count));
  16866. + }
  16867. + } else {
  16868. + /* signal frame completion */
  16869. + complete(&dev->capture.frame_cmplt);
  16870. + }
  16871. + } else {
  16872. + if (dev->capture.frame_count) {
  16873. + if (dev->capture.vc_start_timestamp != -1 &&
  16874. + pts != 0) {
  16875. + s64 runtime_us = pts -
  16876. + dev->capture.vc_start_timestamp;
  16877. + u32 div = 0;
  16878. + u32 rem = 0;
  16879. +
  16880. + div =
  16881. + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
  16882. + buf->vb.v4l2_buf.timestamp.tv_sec =
  16883. + dev->capture.kernel_start_ts.tv_sec - 1 +
  16884. + div;
  16885. + buf->vb.v4l2_buf.timestamp.tv_usec =
  16886. + dev->capture.kernel_start_ts.tv_usec + rem;
  16887. +
  16888. + if (buf->vb.v4l2_buf.timestamp.tv_usec >=
  16889. + USEC_PER_SEC) {
  16890. + buf->vb.v4l2_buf.timestamp.tv_sec++;
  16891. + buf->vb.v4l2_buf.timestamp.tv_usec -=
  16892. + USEC_PER_SEC;
  16893. + }
  16894. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16895. + "Convert start time %d.%06d and %llu "
  16896. + "with offset %llu to %d.%06d\n",
  16897. + (int)dev->capture.kernel_start_ts.
  16898. + tv_sec,
  16899. + (int)dev->capture.kernel_start_ts.
  16900. + tv_usec,
  16901. + dev->capture.vc_start_timestamp, pts,
  16902. + (int)buf->vb.v4l2_buf.timestamp.tv_sec,
  16903. + (int)buf->vb.v4l2_buf.timestamp.
  16904. + tv_usec);
  16905. + } else {
  16906. + v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  16907. + }
  16908. +
  16909. + vb2_set_plane_payload(&buf->vb, 0, length);
  16910. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  16911. +
  16912. + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
  16913. + is_capturing(dev)) {
  16914. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16915. + "Grab another frame as buffer has EOS");
  16916. + vchiq_mmal_port_parameter_set(
  16917. + instance,
  16918. + dev->capture.
  16919. + camera_port,
  16920. + MMAL_PARAMETER_CAPTURE,
  16921. + &dev->capture.
  16922. + frame_count,
  16923. + sizeof(dev->capture.frame_count));
  16924. + }
  16925. + } else {
  16926. + /* signal frame completion */
  16927. + complete(&dev->capture.frame_cmplt);
  16928. + }
  16929. + }
  16930. +}
  16931. +
  16932. +static int enable_camera(struct bm2835_mmal_dev *dev)
  16933. +{
  16934. + int ret;
  16935. + if (!dev->camera_use_count) {
  16936. + ret = vchiq_mmal_component_enable(
  16937. + dev->instance,
  16938. + dev->component[MMAL_COMPONENT_CAMERA]);
  16939. + if (ret < 0) {
  16940. + v4l2_err(&dev->v4l2_dev,
  16941. + "Failed enabling camera, ret %d\n", ret);
  16942. + return -EINVAL;
  16943. + }
  16944. + }
  16945. + dev->camera_use_count++;
  16946. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16947. + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
  16948. + dev->camera_use_count);
  16949. + return 0;
  16950. +}
  16951. +
  16952. +static int disable_camera(struct bm2835_mmal_dev *dev)
  16953. +{
  16954. + int ret;
  16955. + if (!dev->camera_use_count) {
  16956. + v4l2_err(&dev->v4l2_dev,
  16957. + "Disabled the camera when already disabled\n");
  16958. + return -EINVAL;
  16959. + }
  16960. + dev->camera_use_count--;
  16961. + if (!dev->camera_use_count) {
  16962. + unsigned int i = 0xFFFFFFFF;
  16963. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16964. + "Disabling camera\n");
  16965. + ret =
  16966. + vchiq_mmal_component_disable(
  16967. + dev->instance,
  16968. + dev->component[MMAL_COMPONENT_CAMERA]);
  16969. + if (ret < 0) {
  16970. + v4l2_err(&dev->v4l2_dev,
  16971. + "Failed disabling camera, ret %d\n", ret);
  16972. + return -EINVAL;
  16973. + }
  16974. + vchiq_mmal_port_parameter_set(
  16975. + dev->instance,
  16976. + &dev->component[MMAL_COMPONENT_CAMERA]->control,
  16977. + MMAL_PARAMETER_CAMERA_NUM, &i,
  16978. + sizeof(i));
  16979. + }
  16980. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16981. + "Camera refcount now %d\n", dev->camera_use_count);
  16982. + return 0;
  16983. +}
  16984. +
  16985. +static void buffer_queue(struct vb2_buffer *vb)
  16986. +{
  16987. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  16988. + struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
  16989. + int ret;
  16990. +
  16991. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16992. + "%s: dev:%p buf:%p\n", __func__, dev, buf);
  16993. +
  16994. + buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
  16995. + buf->buffer_size = vb2_plane_size(&buf->vb, 0);
  16996. +
  16997. + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
  16998. + if (ret < 0)
  16999. + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
  17000. + __func__);
  17001. +}
  17002. +
  17003. +static int start_streaming(struct vb2_queue *vq, unsigned int count)
  17004. +{
  17005. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17006. + int ret;
  17007. + int parameter_size;
  17008. +
  17009. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  17010. + __func__, dev);
  17011. +
  17012. + /* ensure a format has actually been set */
  17013. + if (dev->capture.port == NULL)
  17014. + return -EINVAL;
  17015. +
  17016. + if (enable_camera(dev) < 0) {
  17017. + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
  17018. + return -EINVAL;
  17019. + }
  17020. +
  17021. + /*init_completion(&dev->capture.frame_cmplt); */
  17022. +
  17023. + /* enable frame capture */
  17024. + dev->capture.frame_count = 1;
  17025. +
  17026. + /* if the preview is not already running, wait for a few frames for AGC
  17027. + * to settle down.
  17028. + */
  17029. + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
  17030. + msleep(300);
  17031. +
  17032. + /* enable the connection from camera to encoder (if applicable) */
  17033. + if (dev->capture.camera_port != dev->capture.port
  17034. + && dev->capture.camera_port) {
  17035. + ret = vchiq_mmal_port_enable(dev->instance,
  17036. + dev->capture.camera_port, NULL);
  17037. + if (ret) {
  17038. + v4l2_err(&dev->v4l2_dev,
  17039. + "Failed to enable encode tunnel - error %d\n",
  17040. + ret);
  17041. + return -1;
  17042. + }
  17043. + }
  17044. +
  17045. + /* Get VC timestamp at this point in time */
  17046. + parameter_size = sizeof(dev->capture.vc_start_timestamp);
  17047. + if (vchiq_mmal_port_parameter_get(dev->instance,
  17048. + dev->capture.camera_port,
  17049. + MMAL_PARAMETER_SYSTEM_TIME,
  17050. + &dev->capture.vc_start_timestamp,
  17051. + &parameter_size)) {
  17052. + v4l2_err(&dev->v4l2_dev,
  17053. + "Failed to get VC start time - update your VC f/w\n");
  17054. +
  17055. + /* Flag to indicate just to rely on kernel timestamps */
  17056. + dev->capture.vc_start_timestamp = -1;
  17057. + } else
  17058. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17059. + "Start time %lld size %d\n",
  17060. + dev->capture.vc_start_timestamp, parameter_size);
  17061. +
  17062. + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
  17063. +
  17064. + /* enable the camera port */
  17065. + dev->capture.port->cb_ctx = dev;
  17066. + ret =
  17067. + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
  17068. + if (ret) {
  17069. + v4l2_err(&dev->v4l2_dev,
  17070. + "Failed to enable capture port - error %d. "
  17071. + "Disabling camera port again\n", ret);
  17072. +
  17073. + vchiq_mmal_port_disable(dev->instance,
  17074. + dev->capture.camera_port);
  17075. + if (disable_camera(dev) < 0) {
  17076. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
  17077. + return -EINVAL;
  17078. + }
  17079. + return -1;
  17080. + }
  17081. +
  17082. + /* capture the first frame */
  17083. + vchiq_mmal_port_parameter_set(dev->instance,
  17084. + dev->capture.camera_port,
  17085. + MMAL_PARAMETER_CAPTURE,
  17086. + &dev->capture.frame_count,
  17087. + sizeof(dev->capture.frame_count));
  17088. + return 0;
  17089. +}
  17090. +
  17091. +/* abort streaming and wait for last buffer */
  17092. +static void stop_streaming(struct vb2_queue *vq)
  17093. +{
  17094. + int ret;
  17095. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17096. +
  17097. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  17098. + __func__, dev);
  17099. +
  17100. + init_completion(&dev->capture.frame_cmplt);
  17101. + dev->capture.frame_count = 0;
  17102. +
  17103. + /* ensure a format has actually been set */
  17104. + if (dev->capture.port == NULL) {
  17105. + v4l2_err(&dev->v4l2_dev,
  17106. + "no capture port - stream not started?\n");
  17107. + return;
  17108. + }
  17109. +
  17110. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
  17111. +
  17112. + /* stop capturing frames */
  17113. + vchiq_mmal_port_parameter_set(dev->instance,
  17114. + dev->capture.camera_port,
  17115. + MMAL_PARAMETER_CAPTURE,
  17116. + &dev->capture.frame_count,
  17117. + sizeof(dev->capture.frame_count));
  17118. +
  17119. + /* wait for last frame to complete */
  17120. + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
  17121. + if (ret <= 0)
  17122. + v4l2_err(&dev->v4l2_dev,
  17123. + "error %d waiting for frame completion\n", ret);
  17124. +
  17125. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17126. + "disabling connection\n");
  17127. +
  17128. + /* disable the connection from camera to encoder */
  17129. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
  17130. + if (!ret && dev->capture.camera_port != dev->capture.port) {
  17131. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17132. + "disabling port\n");
  17133. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
  17134. + } else if (dev->capture.camera_port != dev->capture.port) {
  17135. + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
  17136. + ret);
  17137. + }
  17138. +
  17139. + if (disable_camera(dev) < 0)
  17140. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
  17141. +}
  17142. +
  17143. +static void bm2835_mmal_lock(struct vb2_queue *vq)
  17144. +{
  17145. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17146. + mutex_lock(&dev->mutex);
  17147. +}
  17148. +
  17149. +static void bm2835_mmal_unlock(struct vb2_queue *vq)
  17150. +{
  17151. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17152. + mutex_unlock(&dev->mutex);
  17153. +}
  17154. +
  17155. +static struct vb2_ops bm2835_mmal_video_qops = {
  17156. + .queue_setup = queue_setup,
  17157. + .buf_prepare = buffer_prepare,
  17158. + .buf_queue = buffer_queue,
  17159. + .start_streaming = start_streaming,
  17160. + .stop_streaming = stop_streaming,
  17161. + .wait_prepare = bm2835_mmal_unlock,
  17162. + .wait_finish = bm2835_mmal_lock,
  17163. +};
  17164. +
  17165. +/* ------------------------------------------------------------------
  17166. + IOCTL operations
  17167. + ------------------------------------------------------------------*/
  17168. +
  17169. +/* overlay ioctl */
  17170. +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  17171. + struct v4l2_fmtdesc *f)
  17172. +{
  17173. + struct mmal_fmt *fmt;
  17174. +
  17175. + if (f->index >= ARRAY_SIZE(formats))
  17176. + return -EINVAL;
  17177. +
  17178. + fmt = &formats[f->index];
  17179. +
  17180. + strlcpy(f->description, fmt->name, sizeof(f->description));
  17181. + f->pixelformat = fmt->fourcc;
  17182. + f->flags = fmt->flags;
  17183. +
  17184. + return 0;
  17185. +}
  17186. +
  17187. +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  17188. + struct v4l2_format *f)
  17189. +{
  17190. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17191. +
  17192. + f->fmt.win = dev->overlay;
  17193. +
  17194. + return 0;
  17195. +}
  17196. +
  17197. +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  17198. + struct v4l2_format *f)
  17199. +{
  17200. + /* Only support one format so get the current one. */
  17201. + vidioc_g_fmt_vid_overlay(file, priv, f);
  17202. +
  17203. + /* todo: allow the size and/or offset to be changed. */
  17204. + return 0;
  17205. +}
  17206. +
  17207. +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  17208. + struct v4l2_format *f)
  17209. +{
  17210. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17211. +
  17212. + vidioc_try_fmt_vid_overlay(file, priv, f);
  17213. +
  17214. + dev->overlay = f->fmt.win;
  17215. +
  17216. + /* todo: program the preview port parameters */
  17217. + return 0;
  17218. +}
  17219. +
  17220. +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
  17221. +{
  17222. + int ret;
  17223. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17224. + struct vchiq_mmal_port *src;
  17225. + struct vchiq_mmal_port *dst;
  17226. + struct mmal_parameter_displayregion prev_config = {
  17227. + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
  17228. + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
  17229. + .layer = PREVIEW_LAYER,
  17230. + .alpha = 255,
  17231. + .fullscreen = 0,
  17232. + .dest_rect = {
  17233. + .x = dev->overlay.w.left,
  17234. + .y = dev->overlay.w.top,
  17235. + .width = dev->overlay.w.width,
  17236. + .height = dev->overlay.w.height,
  17237. + },
  17238. + };
  17239. +
  17240. + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
  17241. + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
  17242. + return 0; /* already in requested state */
  17243. +
  17244. + src =
  17245. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17246. + output[MMAL_CAMERA_PORT_PREVIEW];
  17247. +
  17248. + if (!on) {
  17249. + /* disconnect preview ports and disable component */
  17250. + ret = vchiq_mmal_port_disable(dev->instance, src);
  17251. + if (!ret)
  17252. + ret =
  17253. + vchiq_mmal_port_connect_tunnel(dev->instance, src,
  17254. + NULL);
  17255. + if (ret >= 0)
  17256. + ret = vchiq_mmal_component_disable(
  17257. + dev->instance,
  17258. + dev->component[MMAL_COMPONENT_PREVIEW]);
  17259. +
  17260. + disable_camera(dev);
  17261. + return ret;
  17262. + }
  17263. +
  17264. + /* set preview port format and connect it to output */
  17265. + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
  17266. +
  17267. + ret = vchiq_mmal_port_set_format(dev->instance, src);
  17268. + if (ret < 0)
  17269. + goto error;
  17270. +
  17271. + ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
  17272. + MMAL_PARAMETER_DISPLAYREGION,
  17273. + &prev_config, sizeof(prev_config));
  17274. + if (ret < 0)
  17275. + goto error;
  17276. +
  17277. + if (enable_camera(dev) < 0)
  17278. + goto error;
  17279. +
  17280. + ret = vchiq_mmal_component_enable(
  17281. + dev->instance,
  17282. + dev->component[MMAL_COMPONENT_PREVIEW]);
  17283. + if (ret < 0)
  17284. + goto error;
  17285. +
  17286. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
  17287. + src, dst);
  17288. + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
  17289. + if (!ret)
  17290. + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
  17291. +error:
  17292. + return ret;
  17293. +}
  17294. +
  17295. +static int vidioc_g_fbuf(struct file *file, void *fh,
  17296. + struct v4l2_framebuffer *a)
  17297. +{
  17298. + /* The video overlay must stay within the framebuffer and can't be
  17299. + positioned independently. */
  17300. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17301. + struct vchiq_mmal_port *preview_port =
  17302. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17303. + output[MMAL_CAMERA_PORT_PREVIEW];
  17304. + a->flags = V4L2_FBUF_FLAG_OVERLAY;
  17305. + a->fmt.width = preview_port->es.video.width;
  17306. + a->fmt.height = preview_port->es.video.height;
  17307. + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
  17308. + a->fmt.bytesperline = (preview_port->es.video.width * 3)>>1;
  17309. + a->fmt.sizeimage = (preview_port->es.video.width *
  17310. + preview_port->es.video.height * 3)>>1;
  17311. + a->fmt.colorspace = V4L2_COLORSPACE_JPEG;
  17312. +
  17313. + return 0;
  17314. +}
  17315. +
  17316. +/* input ioctls */
  17317. +static int vidioc_enum_input(struct file *file, void *priv,
  17318. + struct v4l2_input *inp)
  17319. +{
  17320. + /* only a single camera input */
  17321. + if (inp->index != 0)
  17322. + return -EINVAL;
  17323. +
  17324. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  17325. + sprintf(inp->name, "Camera %u", inp->index);
  17326. + return 0;
  17327. +}
  17328. +
  17329. +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  17330. +{
  17331. + *i = 0;
  17332. + return 0;
  17333. +}
  17334. +
  17335. +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  17336. +{
  17337. + if (i != 0)
  17338. + return -EINVAL;
  17339. +
  17340. + return 0;
  17341. +}
  17342. +
  17343. +/* capture ioctls */
  17344. +static int vidioc_querycap(struct file *file, void *priv,
  17345. + struct v4l2_capability *cap)
  17346. +{
  17347. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17348. + u32 major;
  17349. + u32 minor;
  17350. +
  17351. + vchiq_mmal_version(dev->instance, &major, &minor);
  17352. +
  17353. + strcpy(cap->driver, "bm2835 mmal");
  17354. + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
  17355. + major, minor);
  17356. +
  17357. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  17358. + "platform:%s", dev->v4l2_dev.name);
  17359. + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
  17360. + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  17361. + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  17362. +
  17363. + return 0;
  17364. +}
  17365. +
  17366. +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  17367. + struct v4l2_fmtdesc *f)
  17368. +{
  17369. + struct mmal_fmt *fmt;
  17370. +
  17371. + if (f->index >= ARRAY_SIZE(formats))
  17372. + return -EINVAL;
  17373. +
  17374. + fmt = &formats[f->index];
  17375. +
  17376. + strlcpy(f->description, fmt->name, sizeof(f->description));
  17377. + f->pixelformat = fmt->fourcc;
  17378. + f->flags = fmt->flags;
  17379. +
  17380. + return 0;
  17381. +}
  17382. +
  17383. +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  17384. + struct v4l2_format *f)
  17385. +{
  17386. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17387. +
  17388. + f->fmt.pix.width = dev->capture.width;
  17389. + f->fmt.pix.height = dev->capture.height;
  17390. + f->fmt.pix.field = V4L2_FIELD_NONE;
  17391. + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
  17392. + f->fmt.pix.bytesperline = dev->capture.stride;
  17393. + f->fmt.pix.sizeimage = dev->capture.buffersize;
  17394. +
  17395. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
  17396. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  17397. + else
  17398. + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  17399. + f->fmt.pix.priv = 0;
  17400. +
  17401. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  17402. + __func__);
  17403. + return 0;
  17404. +}
  17405. +
  17406. +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  17407. + struct v4l2_format *f)
  17408. +{
  17409. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17410. + struct mmal_fmt *mfmt;
  17411. +
  17412. + mfmt = get_format(f);
  17413. + if (!mfmt) {
  17414. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17415. + "Fourcc format (0x%08x) unknown.\n",
  17416. + f->fmt.pix.pixelformat);
  17417. + f->fmt.pix.pixelformat = formats[0].fourcc;
  17418. + mfmt = get_format(f);
  17419. + }
  17420. +
  17421. + f->fmt.pix.field = V4L2_FIELD_NONE;
  17422. +
  17423. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17424. + "Clipping/aligning %dx%d format %08X\n",
  17425. + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
  17426. +
  17427. + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 1,
  17428. + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 1, 0);
  17429. + f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth)>>3;
  17430. +
  17431. + /* Image buffer has to be padded to allow for alignment, even though
  17432. + * we then remove that padding before delivering the buffer.
  17433. + */
  17434. + f->fmt.pix.sizeimage = ((f->fmt.pix.height+15)&~15) *
  17435. + (((f->fmt.pix.width+31)&~31) * mfmt->depth) >> 3;
  17436. +
  17437. + if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
  17438. + f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
  17439. + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
  17440. +
  17441. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
  17442. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  17443. + else
  17444. + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  17445. + f->fmt.pix.priv = 0;
  17446. +
  17447. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17448. + "Now %dx%d format %08X\n",
  17449. + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
  17450. +
  17451. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  17452. + __func__);
  17453. + return 0;
  17454. +}
  17455. +
  17456. +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
  17457. + struct v4l2_format *f)
  17458. +{
  17459. + int ret;
  17460. + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
  17461. + struct vchiq_mmal_component *encode_component = NULL;
  17462. + struct mmal_fmt *mfmt = get_format(f);
  17463. +
  17464. + BUG_ON(!mfmt);
  17465. +
  17466. + if (dev->capture.encode_component) {
  17467. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17468. + "vid_cap - disconnect previous tunnel\n");
  17469. +
  17470. + /* Disconnect any previous connection */
  17471. + vchiq_mmal_port_connect_tunnel(dev->instance,
  17472. + dev->capture.camera_port, NULL);
  17473. + dev->capture.camera_port = NULL;
  17474. + ret = vchiq_mmal_component_disable(dev->instance,
  17475. + dev->capture.
  17476. + encode_component);
  17477. + if (ret)
  17478. + v4l2_err(&dev->v4l2_dev,
  17479. + "Failed to disable encode component %d\n",
  17480. + ret);
  17481. +
  17482. + dev->capture.encode_component = NULL;
  17483. + }
  17484. + /* format dependant port setup */
  17485. + switch (mfmt->mmal_component) {
  17486. + case MMAL_COMPONENT_CAMERA:
  17487. + /* Make a further decision on port based on resolution */
  17488. + if (f->fmt.pix.width <= max_video_width
  17489. + && f->fmt.pix.height <= max_video_height)
  17490. + camera_port = port =
  17491. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17492. + output[MMAL_CAMERA_PORT_VIDEO];
  17493. + else
  17494. + camera_port = port =
  17495. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17496. + output[MMAL_CAMERA_PORT_CAPTURE];
  17497. + break;
  17498. + case MMAL_COMPONENT_IMAGE_ENCODE:
  17499. + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
  17500. + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  17501. + camera_port =
  17502. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17503. + output[MMAL_CAMERA_PORT_CAPTURE];
  17504. + break;
  17505. + case MMAL_COMPONENT_VIDEO_ENCODE:
  17506. + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
  17507. + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  17508. + camera_port =
  17509. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17510. + output[MMAL_CAMERA_PORT_VIDEO];
  17511. + break;
  17512. + default:
  17513. + break;
  17514. + }
  17515. +
  17516. + if (!port)
  17517. + return -EINVAL;
  17518. +
  17519. + if (encode_component)
  17520. + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
  17521. + else
  17522. + camera_port->format.encoding = mfmt->mmal;
  17523. +
  17524. + camera_port->format.encoding_variant = 0;
  17525. + camera_port->es.video.width = f->fmt.pix.width;
  17526. + camera_port->es.video.height = f->fmt.pix.height;
  17527. + camera_port->es.video.crop.x = 0;
  17528. + camera_port->es.video.crop.y = 0;
  17529. + camera_port->es.video.crop.width = f->fmt.pix.width;
  17530. + camera_port->es.video.crop.height = f->fmt.pix.height;
  17531. + camera_port->es.video.frame_rate.num = 0;
  17532. + camera_port->es.video.frame_rate.den = 1;
  17533. + camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
  17534. +
  17535. + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
  17536. +
  17537. + if (!ret
  17538. + && camera_port ==
  17539. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17540. + output[MMAL_CAMERA_PORT_VIDEO]) {
  17541. + bool overlay_enabled =
  17542. + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
  17543. + struct vchiq_mmal_port *preview_port =
  17544. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17545. + output[MMAL_CAMERA_PORT_PREVIEW];
  17546. + /* Preview and encode ports need to match on resolution */
  17547. + if (overlay_enabled) {
  17548. + /* Need to disable the overlay before we can update
  17549. + * the resolution
  17550. + */
  17551. + ret =
  17552. + vchiq_mmal_port_disable(dev->instance,
  17553. + preview_port);
  17554. + if (!ret)
  17555. + ret =
  17556. + vchiq_mmal_port_connect_tunnel(
  17557. + dev->instance,
  17558. + preview_port,
  17559. + NULL);
  17560. + }
  17561. + preview_port->es.video.width = f->fmt.pix.width;
  17562. + preview_port->es.video.height = f->fmt.pix.height;
  17563. + preview_port->es.video.crop.x = 0;
  17564. + preview_port->es.video.crop.y = 0;
  17565. + preview_port->es.video.crop.width = f->fmt.pix.width;
  17566. + preview_port->es.video.crop.height = f->fmt.pix.height;
  17567. + preview_port->es.video.frame_rate.num =
  17568. + dev->capture.timeperframe.denominator;
  17569. + preview_port->es.video.frame_rate.den =
  17570. + dev->capture.timeperframe.numerator;
  17571. + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
  17572. + if (overlay_enabled) {
  17573. + ret = vchiq_mmal_port_connect_tunnel(
  17574. + dev->instance,
  17575. + preview_port,
  17576. + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
  17577. + if (!ret)
  17578. + ret = vchiq_mmal_port_enable(dev->instance,
  17579. + preview_port,
  17580. + NULL);
  17581. + }
  17582. + }
  17583. +
  17584. + if (ret) {
  17585. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17586. + "%s failed to set format %dx%d %08X\n", __func__,
  17587. + f->fmt.pix.width, f->fmt.pix.height,
  17588. + f->fmt.pix.pixelformat);
  17589. + /* ensure capture is not going to be tried */
  17590. + dev->capture.port = NULL;
  17591. + } else {
  17592. + if (encode_component) {
  17593. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17594. + "vid_cap - set up encode comp\n");
  17595. +
  17596. + /* configure buffering */
  17597. + camera_port->current_buffer.size =
  17598. + camera_port->recommended_buffer.size;
  17599. + camera_port->current_buffer.num =
  17600. + camera_port->recommended_buffer.num;
  17601. +
  17602. + ret =
  17603. + vchiq_mmal_port_connect_tunnel(
  17604. + dev->instance,
  17605. + camera_port,
  17606. + &encode_component->input[0]);
  17607. + if (ret) {
  17608. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17609. + &dev->v4l2_dev,
  17610. + "%s failed to create connection\n",
  17611. + __func__);
  17612. + /* ensure capture is not going to be tried */
  17613. + dev->capture.port = NULL;
  17614. + } else {
  17615. + port->es.video.width = f->fmt.pix.width;
  17616. + port->es.video.height = f->fmt.pix.height;
  17617. + port->es.video.crop.x = 0;
  17618. + port->es.video.crop.y = 0;
  17619. + port->es.video.crop.width = f->fmt.pix.width;
  17620. + port->es.video.crop.height = f->fmt.pix.height;
  17621. + port->es.video.frame_rate.num =
  17622. + dev->capture.timeperframe.denominator;
  17623. + port->es.video.frame_rate.den =
  17624. + dev->capture.timeperframe.numerator;
  17625. +
  17626. + port->format.encoding = mfmt->mmal;
  17627. + port->format.encoding_variant = 0;
  17628. + /* Set any encoding specific parameters */
  17629. + switch (mfmt->mmal_component) {
  17630. + case MMAL_COMPONENT_VIDEO_ENCODE:
  17631. + port->format.bitrate =
  17632. + dev->capture.encode_bitrate;
  17633. + break;
  17634. + case MMAL_COMPONENT_IMAGE_ENCODE:
  17635. + /* Could set EXIF parameters here */
  17636. + break;
  17637. + default:
  17638. + break;
  17639. + }
  17640. + ret = vchiq_mmal_port_set_format(dev->instance,
  17641. + port);
  17642. + if (ret)
  17643. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17644. + &dev->v4l2_dev,
  17645. + "%s failed to set format %dx%d fmt %08X\n",
  17646. + __func__,
  17647. + f->fmt.pix.width,
  17648. + f->fmt.pix.height,
  17649. + f->fmt.pix.pixelformat
  17650. + );
  17651. + }
  17652. +
  17653. + if (!ret) {
  17654. + ret = vchiq_mmal_component_enable(
  17655. + dev->instance,
  17656. + encode_component);
  17657. + if (ret) {
  17658. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17659. + &dev->v4l2_dev,
  17660. + "%s Failed to enable encode components\n",
  17661. + __func__);
  17662. + }
  17663. + }
  17664. + if (!ret) {
  17665. + /* configure buffering */
  17666. + port->current_buffer.num = 1;
  17667. + port->current_buffer.size =
  17668. + f->fmt.pix.sizeimage;
  17669. + if (port->format.encoding ==
  17670. + MMAL_ENCODING_JPEG) {
  17671. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17672. + &dev->v4l2_dev,
  17673. + "JPG - buf size now %d was %d\n",
  17674. + f->fmt.pix.sizeimage,
  17675. + port->current_buffer.size);
  17676. + port->current_buffer.size =
  17677. + (f->fmt.pix.sizeimage <
  17678. + (100 << 10))
  17679. + ? (100 << 10) : f->fmt.pix.
  17680. + sizeimage;
  17681. + }
  17682. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17683. + &dev->v4l2_dev,
  17684. + "vid_cap - cur_buf.size set to %d\n",
  17685. + f->fmt.pix.sizeimage);
  17686. + port->current_buffer.alignment = 0;
  17687. + }
  17688. + } else {
  17689. + /* configure buffering */
  17690. + camera_port->current_buffer.num = 1;
  17691. + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
  17692. + camera_port->current_buffer.alignment = 0;
  17693. + }
  17694. +
  17695. + if (!ret) {
  17696. + dev->capture.fmt = mfmt;
  17697. + dev->capture.stride = f->fmt.pix.bytesperline;
  17698. + dev->capture.width = camera_port->es.video.crop.width;
  17699. + dev->capture.height = camera_port->es.video.crop.height;
  17700. + dev->capture.buffersize = port->current_buffer.size;
  17701. +
  17702. + /* select port for capture */
  17703. + dev->capture.port = port;
  17704. + dev->capture.camera_port = camera_port;
  17705. + dev->capture.encode_component = encode_component;
  17706. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17707. + &dev->v4l2_dev,
  17708. + "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
  17709. + port->format.encoding,
  17710. + dev->capture.width, dev->capture.height,
  17711. + dev->capture.stride, dev->capture.buffersize);
  17712. + }
  17713. + }
  17714. +
  17715. + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
  17716. + return ret;
  17717. +}
  17718. +
  17719. +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  17720. + struct v4l2_format *f)
  17721. +{
  17722. + int ret;
  17723. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17724. + struct mmal_fmt *mfmt;
  17725. +
  17726. + /* try the format to set valid parameters */
  17727. + ret = vidioc_try_fmt_vid_cap(file, priv, f);
  17728. + if (ret) {
  17729. + v4l2_err(&dev->v4l2_dev,
  17730. + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
  17731. + return ret;
  17732. + }
  17733. +
  17734. + /* if a capture is running refuse to set format */
  17735. + if (vb2_is_busy(&dev->capture.vb_vidq)) {
  17736. + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
  17737. + return -EBUSY;
  17738. + }
  17739. +
  17740. + /* If the format is unsupported v4l2 says we should switch to
  17741. + * a supported one and not return an error. */
  17742. + mfmt = get_format(f);
  17743. + if (!mfmt) {
  17744. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17745. + "Fourcc format (0x%08x) unknown.\n",
  17746. + f->fmt.pix.pixelformat);
  17747. + f->fmt.pix.pixelformat = formats[0].fourcc;
  17748. + mfmt = get_format(f);
  17749. + }
  17750. +
  17751. + ret = mmal_setup_components(dev, f);
  17752. + if (ret != 0) {
  17753. + v4l2_err(&dev->v4l2_dev,
  17754. + "%s: failed to setup mmal components: %d\n",
  17755. + __func__, ret);
  17756. + ret = -EINVAL;
  17757. + }
  17758. +
  17759. + return ret;
  17760. +}
  17761. +
  17762. +int vidioc_enum_framesizes(struct file *file, void *fh,
  17763. + struct v4l2_frmsizeenum *fsize)
  17764. +{
  17765. + static const struct v4l2_frmsize_stepwise sizes = {
  17766. + MIN_WIDTH, MAX_WIDTH, 2,
  17767. + MIN_HEIGHT, MAX_HEIGHT, 2
  17768. + };
  17769. + int i;
  17770. +
  17771. + if (fsize->index)
  17772. + return -EINVAL;
  17773. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  17774. + if (formats[i].fourcc == fsize->pixel_format)
  17775. + break;
  17776. + if (i == ARRAY_SIZE(formats))
  17777. + return -EINVAL;
  17778. + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  17779. + fsize->stepwise = sizes;
  17780. + return 0;
  17781. +}
  17782. +
  17783. +/* timeperframe is arbitrary and continous */
  17784. +static int vidioc_enum_frameintervals(struct file *file, void *priv,
  17785. + struct v4l2_frmivalenum *fival)
  17786. +{
  17787. + int i;
  17788. +
  17789. + if (fival->index)
  17790. + return -EINVAL;
  17791. +
  17792. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  17793. + if (formats[i].fourcc == fival->pixel_format)
  17794. + break;
  17795. + if (i == ARRAY_SIZE(formats))
  17796. + return -EINVAL;
  17797. +
  17798. + /* regarding width & height - we support any within range */
  17799. + if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH ||
  17800. + fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT)
  17801. + return -EINVAL;
  17802. +
  17803. + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  17804. +
  17805. + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
  17806. + fival->stepwise.min = tpf_min;
  17807. + fival->stepwise.max = tpf_max;
  17808. + fival->stepwise.step = (struct v4l2_fract) {1, 1};
  17809. +
  17810. + return 0;
  17811. +}
  17812. +
  17813. +static int vidioc_g_parm(struct file *file, void *priv,
  17814. + struct v4l2_streamparm *parm)
  17815. +{
  17816. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17817. +
  17818. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  17819. + return -EINVAL;
  17820. +
  17821. + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  17822. + parm->parm.capture.timeperframe = dev->capture.timeperframe;
  17823. + parm->parm.capture.readbuffers = 1;
  17824. + return 0;
  17825. +}
  17826. +
  17827. +#define FRACT_CMP(a, OP, b) \
  17828. + ((u64)(a).numerator * (b).denominator OP \
  17829. + (u64)(b).numerator * (a).denominator)
  17830. +
  17831. +static int vidioc_s_parm(struct file *file, void *priv,
  17832. + struct v4l2_streamparm *parm)
  17833. +{
  17834. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17835. + struct v4l2_fract tpf;
  17836. + struct mmal_parameter_rational fps_param;
  17837. +
  17838. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  17839. + return -EINVAL;
  17840. +
  17841. + tpf = parm->parm.capture.timeperframe;
  17842. +
  17843. + /* tpf: {*, 0} resets timing; clip to [min, max]*/
  17844. + tpf = tpf.denominator ? tpf : tpf_default;
  17845. + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  17846. + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  17847. +
  17848. + dev->capture.timeperframe = tpf;
  17849. + parm->parm.capture.timeperframe = tpf;
  17850. + parm->parm.capture.readbuffers = 1;
  17851. +
  17852. + fps_param.num = 0; /* Select variable fps, and then use
  17853. + * FPS_RANGE to select the actual limits.
  17854. + */
  17855. + fps_param.den = 1;
  17856. + set_framerate_params(dev);
  17857. +
  17858. + return 0;
  17859. +}
  17860. +
  17861. +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
  17862. + /* overlay */
  17863. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  17864. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  17865. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  17866. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  17867. + .vidioc_overlay = vidioc_overlay,
  17868. + .vidioc_g_fbuf = vidioc_g_fbuf,
  17869. +
  17870. + /* inputs */
  17871. + .vidioc_enum_input = vidioc_enum_input,
  17872. + .vidioc_g_input = vidioc_g_input,
  17873. + .vidioc_s_input = vidioc_s_input,
  17874. +
  17875. + /* capture */
  17876. + .vidioc_querycap = vidioc_querycap,
  17877. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  17878. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  17879. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  17880. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  17881. +
  17882. + /* buffer management */
  17883. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  17884. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  17885. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  17886. + .vidioc_querybuf = vb2_ioctl_querybuf,
  17887. + .vidioc_qbuf = vb2_ioctl_qbuf,
  17888. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  17889. + .vidioc_enum_framesizes = vidioc_enum_framesizes,
  17890. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  17891. + .vidioc_g_parm = vidioc_g_parm,
  17892. + .vidioc_s_parm = vidioc_s_parm,
  17893. + .vidioc_streamon = vb2_ioctl_streamon,
  17894. + .vidioc_streamoff = vb2_ioctl_streamoff,
  17895. +
  17896. + .vidioc_log_status = v4l2_ctrl_log_status,
  17897. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  17898. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  17899. +};
  17900. +
  17901. +static const struct v4l2_ioctl_ops camera0_ioctl_ops_gstreamer = {
  17902. + /* overlay */
  17903. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  17904. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  17905. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  17906. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  17907. + .vidioc_overlay = vidioc_overlay,
  17908. + .vidioc_g_fbuf = vidioc_g_fbuf,
  17909. +
  17910. + /* inputs */
  17911. + .vidioc_enum_input = vidioc_enum_input,
  17912. + .vidioc_g_input = vidioc_g_input,
  17913. + .vidioc_s_input = vidioc_s_input,
  17914. +
  17915. + /* capture */
  17916. + .vidioc_querycap = vidioc_querycap,
  17917. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  17918. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  17919. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  17920. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  17921. +
  17922. + /* buffer management */
  17923. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  17924. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  17925. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  17926. + .vidioc_querybuf = vb2_ioctl_querybuf,
  17927. + .vidioc_qbuf = vb2_ioctl_qbuf,
  17928. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  17929. + /* Remove this function ptr to fix gstreamer bug
  17930. + .vidioc_enum_framesizes = vidioc_enum_framesizes, */
  17931. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  17932. + .vidioc_g_parm = vidioc_g_parm,
  17933. + .vidioc_s_parm = vidioc_s_parm,
  17934. + .vidioc_streamon = vb2_ioctl_streamon,
  17935. + .vidioc_streamoff = vb2_ioctl_streamoff,
  17936. +
  17937. + .vidioc_log_status = v4l2_ctrl_log_status,
  17938. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  17939. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  17940. +};
  17941. +
  17942. +/* ------------------------------------------------------------------
  17943. + Driver init/finalise
  17944. + ------------------------------------------------------------------*/
  17945. +
  17946. +static const struct v4l2_file_operations camera0_fops = {
  17947. + .owner = THIS_MODULE,
  17948. + .open = v4l2_fh_open,
  17949. + .release = vb2_fop_release,
  17950. + .read = vb2_fop_read,
  17951. + .poll = vb2_fop_poll,
  17952. + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  17953. + .mmap = vb2_fop_mmap,
  17954. +};
  17955. +
  17956. +static struct video_device vdev_template = {
  17957. + .name = "camera0",
  17958. + .fops = &camera0_fops,
  17959. + .ioctl_ops = &camera0_ioctl_ops,
  17960. + .release = video_device_release_empty,
  17961. +};
  17962. +
  17963. +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
  17964. + struct vchiq_mmal_component *camera)
  17965. +{
  17966. + int ret;
  17967. + struct mmal_parameter_camera_config cam_config = {
  17968. + .max_stills_w = MAX_WIDTH,
  17969. + .max_stills_h = MAX_HEIGHT,
  17970. + .stills_yuv422 = 1,
  17971. + .one_shot_stills = 1,
  17972. + .max_preview_video_w = (max_video_width > 1920) ?
  17973. + max_video_width : 1920,
  17974. + .max_preview_video_h = (max_video_height > 1088) ?
  17975. + max_video_height : 1088,
  17976. + .num_preview_video_frames = 3,
  17977. + .stills_capture_circular_buffer_height = 0,
  17978. + .fast_preview_resume = 0,
  17979. + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
  17980. + };
  17981. +
  17982. + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
  17983. + MMAL_PARAMETER_CAMERA_CONFIG,
  17984. + &cam_config, sizeof(cam_config));
  17985. + return ret;
  17986. +}
  17987. +
  17988. +/* MMAL instance and component init */
  17989. +static int __init mmal_init(struct bm2835_mmal_dev *dev)
  17990. +{
  17991. + int ret;
  17992. + struct mmal_es_format *format;
  17993. + u32 bool_true = 1;
  17994. +
  17995. + ret = vchiq_mmal_init(&dev->instance);
  17996. + if (ret < 0)
  17997. + return ret;
  17998. +
  17999. + /* get the camera component ready */
  18000. + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
  18001. + &dev->component[MMAL_COMPONENT_CAMERA]);
  18002. + if (ret < 0)
  18003. + goto unreg_mmal;
  18004. +
  18005. + if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  18006. + MMAL_CAMERA_PORT_COUNT) {
  18007. + ret = -EINVAL;
  18008. + goto unreg_camera;
  18009. + }
  18010. +
  18011. + ret = set_camera_parameters(dev->instance,
  18012. + dev->component[MMAL_COMPONENT_CAMERA]);
  18013. + if (ret < 0)
  18014. + goto unreg_camera;
  18015. +
  18016. + format =
  18017. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18018. + output[MMAL_CAMERA_PORT_PREVIEW].format;
  18019. +
  18020. + format->encoding = MMAL_ENCODING_OPAQUE;
  18021. + format->encoding_variant = MMAL_ENCODING_I420;
  18022. +
  18023. + format->es->video.width = 1024;
  18024. + format->es->video.height = 768;
  18025. + format->es->video.crop.x = 0;
  18026. + format->es->video.crop.y = 0;
  18027. + format->es->video.crop.width = 1024;
  18028. + format->es->video.crop.height = 768;
  18029. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  18030. + format->es->video.frame_rate.den = 1;
  18031. +
  18032. + format =
  18033. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18034. + output[MMAL_CAMERA_PORT_VIDEO].format;
  18035. +
  18036. + format->encoding = MMAL_ENCODING_OPAQUE;
  18037. + format->encoding_variant = MMAL_ENCODING_I420;
  18038. +
  18039. + format->es->video.width = 1024;
  18040. + format->es->video.height = 768;
  18041. + format->es->video.crop.x = 0;
  18042. + format->es->video.crop.y = 0;
  18043. + format->es->video.crop.width = 1024;
  18044. + format->es->video.crop.height = 768;
  18045. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  18046. + format->es->video.frame_rate.den = 1;
  18047. +
  18048. + vchiq_mmal_port_parameter_set(dev->instance,
  18049. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18050. + output[MMAL_CAMERA_PORT_VIDEO],
  18051. + MMAL_PARAMETER_NO_IMAGE_PADDING,
  18052. + &bool_true, sizeof(bool_true));
  18053. +
  18054. + format =
  18055. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18056. + output[MMAL_CAMERA_PORT_CAPTURE].format;
  18057. +
  18058. + format->encoding = MMAL_ENCODING_OPAQUE;
  18059. +
  18060. + format->es->video.width = 2592;
  18061. + format->es->video.height = 1944;
  18062. + format->es->video.crop.x = 0;
  18063. + format->es->video.crop.y = 0;
  18064. + format->es->video.crop.width = 2592;
  18065. + format->es->video.crop.height = 1944;
  18066. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  18067. + format->es->video.frame_rate.den = 1;
  18068. +
  18069. + dev->capture.width = format->es->video.width;
  18070. + dev->capture.height = format->es->video.height;
  18071. + dev->capture.fmt = &formats[0];
  18072. + dev->capture.encode_component = NULL;
  18073. + dev->capture.timeperframe = tpf_default;
  18074. + dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  18075. + dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  18076. +
  18077. + vchiq_mmal_port_parameter_set(dev->instance,
  18078. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18079. + output[MMAL_CAMERA_PORT_CAPTURE],
  18080. + MMAL_PARAMETER_NO_IMAGE_PADDING,
  18081. + &bool_true, sizeof(bool_true));
  18082. +
  18083. + /* get the preview component ready */
  18084. + ret = vchiq_mmal_component_init(
  18085. + dev->instance, "ril.video_render",
  18086. + &dev->component[MMAL_COMPONENT_PREVIEW]);
  18087. + if (ret < 0)
  18088. + goto unreg_camera;
  18089. +
  18090. + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
  18091. + ret = -EINVAL;
  18092. + pr_debug("too few input ports %d needed %d\n",
  18093. + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
  18094. + goto unreg_preview;
  18095. + }
  18096. +
  18097. + /* get the image encoder component ready */
  18098. + ret = vchiq_mmal_component_init(
  18099. + dev->instance, "ril.image_encode",
  18100. + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  18101. + if (ret < 0)
  18102. + goto unreg_preview;
  18103. +
  18104. + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
  18105. + ret = -EINVAL;
  18106. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  18107. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
  18108. + 1);
  18109. + goto unreg_image_encoder;
  18110. + }
  18111. +
  18112. + /* get the video encoder component ready */
  18113. + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
  18114. + &dev->
  18115. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  18116. + if (ret < 0)
  18117. + goto unreg_image_encoder;
  18118. +
  18119. + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
  18120. + ret = -EINVAL;
  18121. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  18122. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
  18123. + 1);
  18124. + goto unreg_vid_encoder;
  18125. + }
  18126. +
  18127. + {
  18128. + struct vchiq_mmal_port *encoder_port =
  18129. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  18130. + encoder_port->format.encoding = MMAL_ENCODING_H264;
  18131. + ret = vchiq_mmal_port_set_format(dev->instance,
  18132. + encoder_port);
  18133. + }
  18134. +
  18135. + {
  18136. + unsigned int enable = 1;
  18137. + vchiq_mmal_port_parameter_set(
  18138. + dev->instance,
  18139. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  18140. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  18141. + &enable, sizeof(enable));
  18142. +
  18143. + vchiq_mmal_port_parameter_set(dev->instance,
  18144. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  18145. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  18146. + &enable,
  18147. + sizeof(enable));
  18148. + }
  18149. + ret = bm2835_mmal_set_all_camera_controls(dev);
  18150. + if (ret < 0)
  18151. + goto unreg_vid_encoder;
  18152. +
  18153. + return 0;
  18154. +
  18155. +unreg_vid_encoder:
  18156. + pr_err("Cleanup: Destroy video encoder\n");
  18157. + vchiq_mmal_component_finalise(
  18158. + dev->instance,
  18159. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
  18160. +
  18161. +unreg_image_encoder:
  18162. + pr_err("Cleanup: Destroy image encoder\n");
  18163. + vchiq_mmal_component_finalise(
  18164. + dev->instance,
  18165. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  18166. +
  18167. +unreg_preview:
  18168. + pr_err("Cleanup: Destroy video render\n");
  18169. + vchiq_mmal_component_finalise(dev->instance,
  18170. + dev->component[MMAL_COMPONENT_PREVIEW]);
  18171. +
  18172. +unreg_camera:
  18173. + pr_err("Cleanup: Destroy camera\n");
  18174. + vchiq_mmal_component_finalise(dev->instance,
  18175. + dev->component[MMAL_COMPONENT_CAMERA]);
  18176. +
  18177. +unreg_mmal:
  18178. + vchiq_mmal_finalise(dev->instance);
  18179. + return ret;
  18180. +}
  18181. +
  18182. +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
  18183. + struct video_device *vfd)
  18184. +{
  18185. + int ret;
  18186. +
  18187. + *vfd = vdev_template;
  18188. + if (gst_v4l2src_is_broken) {
  18189. + v4l2_info(&dev->v4l2_dev,
  18190. + "Work-around for gstreamer issue is active.\n");
  18191. + vfd->ioctl_ops = &camera0_ioctl_ops_gstreamer;
  18192. + }
  18193. +
  18194. + vfd->v4l2_dev = &dev->v4l2_dev;
  18195. +
  18196. + vfd->lock = &dev->mutex;
  18197. +
  18198. + vfd->queue = &dev->capture.vb_vidq;
  18199. +
  18200. + /* video device needs to be able to access instance data */
  18201. + video_set_drvdata(vfd, dev);
  18202. +
  18203. + ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  18204. + if (ret < 0)
  18205. + return ret;
  18206. +
  18207. + v4l2_info(vfd->v4l2_dev,
  18208. + "V4L2 device registered as %s - stills mode > %dx%d\n",
  18209. + video_device_node_name(vfd), max_video_width, max_video_height);
  18210. +
  18211. + return 0;
  18212. +}
  18213. +
  18214. +static struct v4l2_format default_v4l2_format = {
  18215. + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
  18216. + .fmt.pix.width = 1024,
  18217. + .fmt.pix.bytesperline = 1024,
  18218. + .fmt.pix.height = 768,
  18219. + .fmt.pix.sizeimage = 1024*768,
  18220. +};
  18221. +
  18222. +static int __init bm2835_mmal_init(void)
  18223. +{
  18224. + int ret;
  18225. + struct bm2835_mmal_dev *dev;
  18226. + struct vb2_queue *q;
  18227. +
  18228. + dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  18229. + if (!dev)
  18230. + return -ENOMEM;
  18231. +
  18232. + /* setup device defaults */
  18233. + dev->overlay.w.left = 150;
  18234. + dev->overlay.w.top = 50;
  18235. + dev->overlay.w.width = 1024;
  18236. + dev->overlay.w.height = 768;
  18237. + dev->overlay.clipcount = 0;
  18238. + dev->overlay.field = V4L2_FIELD_NONE;
  18239. +
  18240. + dev->capture.fmt = &formats[3]; /* JPEG */
  18241. +
  18242. + /* v4l device registration */
  18243. + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  18244. + "%s", BM2835_MMAL_MODULE_NAME);
  18245. + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  18246. + if (ret)
  18247. + goto free_dev;
  18248. +
  18249. + /* setup v4l controls */
  18250. + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
  18251. + if (ret < 0)
  18252. + goto unreg_dev;
  18253. + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  18254. +
  18255. + /* mmal init */
  18256. + ret = mmal_init(dev);
  18257. + if (ret < 0)
  18258. + goto unreg_dev;
  18259. +
  18260. + /* initialize queue */
  18261. + q = &dev->capture.vb_vidq;
  18262. + memset(q, 0, sizeof(*q));
  18263. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  18264. + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  18265. + q->drv_priv = dev;
  18266. + q->buf_struct_size = sizeof(struct mmal_buffer);
  18267. + q->ops = &bm2835_mmal_video_qops;
  18268. + q->mem_ops = &vb2_vmalloc_memops;
  18269. + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  18270. + ret = vb2_queue_init(q);
  18271. + if (ret < 0)
  18272. + goto unreg_dev;
  18273. +
  18274. + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
  18275. + mutex_init(&dev->mutex);
  18276. +
  18277. + /* initialise video devices */
  18278. + ret = bm2835_mmal_init_device(dev, &dev->vdev);
  18279. + if (ret < 0)
  18280. + goto unreg_dev;
  18281. +
  18282. + /* Really want to call vidioc_s_fmt_vid_cap with the default
  18283. + * format, but currently the APIs don't join up.
  18284. + */
  18285. + ret = mmal_setup_components(dev, &default_v4l2_format);
  18286. + if (ret < 0) {
  18287. + v4l2_err(&dev->v4l2_dev,
  18288. + "%s: could not setup components\n", __func__);
  18289. + goto unreg_dev;
  18290. + }
  18291. +
  18292. + v4l2_info(&dev->v4l2_dev,
  18293. + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
  18294. + BM2835_MMAL_VERSION);
  18295. +
  18296. + gdev = dev;
  18297. + return 0;
  18298. +
  18299. +unreg_dev:
  18300. + v4l2_ctrl_handler_free(&dev->ctrl_handler);
  18301. + v4l2_device_unregister(&dev->v4l2_dev);
  18302. +
  18303. +free_dev:
  18304. + kfree(dev);
  18305. +
  18306. + v4l2_err(&dev->v4l2_dev,
  18307. + "%s: error %d while loading driver\n",
  18308. + BM2835_MMAL_MODULE_NAME, ret);
  18309. +
  18310. + return ret;
  18311. +}
  18312. +
  18313. +static void __exit bm2835_mmal_exit(void)
  18314. +{
  18315. + if (!gdev)
  18316. + return;
  18317. +
  18318. + v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
  18319. + video_device_node_name(&gdev->vdev));
  18320. +
  18321. + video_unregister_device(&gdev->vdev);
  18322. +
  18323. + if (gdev->capture.encode_component) {
  18324. + v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
  18325. + "mmal_exit - disconnect tunnel\n");
  18326. + vchiq_mmal_port_connect_tunnel(gdev->instance,
  18327. + gdev->capture.camera_port, NULL);
  18328. + vchiq_mmal_component_disable(gdev->instance,
  18329. + gdev->capture.encode_component);
  18330. + }
  18331. + vchiq_mmal_component_disable(gdev->instance,
  18332. + gdev->component[MMAL_COMPONENT_CAMERA]);
  18333. +
  18334. + vchiq_mmal_component_finalise(gdev->instance,
  18335. + gdev->
  18336. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  18337. +
  18338. + vchiq_mmal_component_finalise(gdev->instance,
  18339. + gdev->
  18340. + component[MMAL_COMPONENT_IMAGE_ENCODE]);
  18341. +
  18342. + vchiq_mmal_component_finalise(gdev->instance,
  18343. + gdev->component[MMAL_COMPONENT_PREVIEW]);
  18344. +
  18345. + vchiq_mmal_component_finalise(gdev->instance,
  18346. + gdev->component[MMAL_COMPONENT_CAMERA]);
  18347. +
  18348. + vchiq_mmal_finalise(gdev->instance);
  18349. +
  18350. + v4l2_ctrl_handler_free(&gdev->ctrl_handler);
  18351. +
  18352. + v4l2_device_unregister(&gdev->v4l2_dev);
  18353. +
  18354. + kfree(gdev);
  18355. +}
  18356. +
  18357. +module_init(bm2835_mmal_init);
  18358. +module_exit(bm2835_mmal_exit);
  18359. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/bcm2835-camera.h linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h
  18360. --- linux-3.17.5/drivers/media/platform/bcm2835/bcm2835-camera.h 1969-12-31 18:00:00.000000000 -0600
  18361. +++ linux-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h 2014-12-11 14:02:53.384418001 -0600
  18362. @@ -0,0 +1,126 @@
  18363. +/*
  18364. + * Broadcom BM2835 V4L2 driver
  18365. + *
  18366. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  18367. + *
  18368. + * This file is subject to the terms and conditions of the GNU General Public
  18369. + * License. See the file COPYING in the main directory of this archive
  18370. + * for more details.
  18371. + *
  18372. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  18373. + * Dave Stevenson <dsteve@broadcom.com>
  18374. + * Simon Mellor <simellor@broadcom.com>
  18375. + * Luke Diamand <luked@broadcom.com>
  18376. + *
  18377. + * core driver device
  18378. + */
  18379. +
  18380. +#define V4L2_CTRL_COUNT 28 /* number of v4l controls */
  18381. +
  18382. +enum {
  18383. + MMAL_COMPONENT_CAMERA = 0,
  18384. + MMAL_COMPONENT_PREVIEW,
  18385. + MMAL_COMPONENT_IMAGE_ENCODE,
  18386. + MMAL_COMPONENT_VIDEO_ENCODE,
  18387. + MMAL_COMPONENT_COUNT
  18388. +};
  18389. +
  18390. +enum {
  18391. + MMAL_CAMERA_PORT_PREVIEW = 0,
  18392. + MMAL_CAMERA_PORT_VIDEO,
  18393. + MMAL_CAMERA_PORT_CAPTURE,
  18394. + MMAL_CAMERA_PORT_COUNT
  18395. +};
  18396. +
  18397. +#define PREVIEW_LAYER 2
  18398. +
  18399. +extern int bcm2835_v4l2_debug;
  18400. +
  18401. +struct bm2835_mmal_dev {
  18402. + /* v4l2 devices */
  18403. + struct v4l2_device v4l2_dev;
  18404. + struct video_device vdev;
  18405. + struct mutex mutex;
  18406. +
  18407. + /* controls */
  18408. + struct v4l2_ctrl_handler ctrl_handler;
  18409. + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
  18410. + enum v4l2_scene_mode scene_mode;
  18411. + struct mmal_colourfx colourfx;
  18412. + int hflip;
  18413. + int vflip;
  18414. + int red_gain;
  18415. + int blue_gain;
  18416. + enum mmal_parameter_exposuremode exposure_mode_user;
  18417. + enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
  18418. + /* active exposure mode may differ if selected via a scene mode */
  18419. + enum mmal_parameter_exposuremode exposure_mode_active;
  18420. + enum mmal_parameter_exposuremeteringmode metering_mode;
  18421. + unsigned int manual_shutter_speed;
  18422. + bool exp_auto_priority;
  18423. +
  18424. + /* allocated mmal instance and components */
  18425. + struct vchiq_mmal_instance *instance;
  18426. + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
  18427. + int camera_use_count;
  18428. +
  18429. + struct v4l2_window overlay;
  18430. +
  18431. + struct {
  18432. + unsigned int width; /* width */
  18433. + unsigned int height; /* height */
  18434. + unsigned int stride; /* stride */
  18435. + unsigned int buffersize; /* buffer size with padding */
  18436. + struct mmal_fmt *fmt;
  18437. + struct v4l2_fract timeperframe;
  18438. +
  18439. + /* H264 encode bitrate */
  18440. + int encode_bitrate;
  18441. + /* H264 bitrate mode. CBR/VBR */
  18442. + int encode_bitrate_mode;
  18443. + /* H264 profile */
  18444. + enum v4l2_mpeg_video_h264_profile enc_profile;
  18445. + /* H264 level */
  18446. + enum v4l2_mpeg_video_h264_level enc_level;
  18447. + /* JPEG Q-factor */
  18448. + int q_factor;
  18449. +
  18450. + struct vb2_queue vb_vidq;
  18451. +
  18452. + /* VC start timestamp for streaming */
  18453. + s64 vc_start_timestamp;
  18454. + /* Kernel start timestamp for streaming */
  18455. + struct timeval kernel_start_ts;
  18456. +
  18457. + struct vchiq_mmal_port *port; /* port being used for capture */
  18458. + /* camera port being used for capture */
  18459. + struct vchiq_mmal_port *camera_port;
  18460. + /* component being used for encode */
  18461. + struct vchiq_mmal_component *encode_component;
  18462. + /* number of frames remaining which driver should capture */
  18463. + unsigned int frame_count;
  18464. + /* last frame completion */
  18465. + struct completion frame_cmplt;
  18466. +
  18467. + } capture;
  18468. +
  18469. +};
  18470. +
  18471. +int bm2835_mmal_init_controls(
  18472. + struct bm2835_mmal_dev *dev,
  18473. + struct v4l2_ctrl_handler *hdl);
  18474. +
  18475. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
  18476. +int set_framerate_params(struct bm2835_mmal_dev *dev);
  18477. +
  18478. +/* Debug helpers */
  18479. +
  18480. +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
  18481. +{ \
  18482. + v4l2_dbg(level, debug, dev, \
  18483. +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
  18484. + desc == NULL ? "" : desc, \
  18485. + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
  18486. + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
  18487. + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
  18488. +}
  18489. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/controls.c linux-rpi/drivers/media/platform/bcm2835/controls.c
  18490. --- linux-3.17.5/drivers/media/platform/bcm2835/controls.c 1969-12-31 18:00:00.000000000 -0600
  18491. +++ linux-rpi/drivers/media/platform/bcm2835/controls.c 2014-12-11 14:02:53.384418001 -0600
  18492. @@ -0,0 +1,1322 @@
  18493. +/*
  18494. + * Broadcom BM2835 V4L2 driver
  18495. + *
  18496. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  18497. + *
  18498. + * This file is subject to the terms and conditions of the GNU General Public
  18499. + * License. See the file COPYING in the main directory of this archive
  18500. + * for more details.
  18501. + *
  18502. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  18503. + * Dave Stevenson <dsteve@broadcom.com>
  18504. + * Simon Mellor <simellor@broadcom.com>
  18505. + * Luke Diamand <luked@broadcom.com>
  18506. + */
  18507. +
  18508. +#include <linux/errno.h>
  18509. +#include <linux/kernel.h>
  18510. +#include <linux/module.h>
  18511. +#include <linux/slab.h>
  18512. +#include <media/videobuf2-vmalloc.h>
  18513. +#include <media/v4l2-device.h>
  18514. +#include <media/v4l2-ioctl.h>
  18515. +#include <media/v4l2-ctrls.h>
  18516. +#include <media/v4l2-fh.h>
  18517. +#include <media/v4l2-event.h>
  18518. +#include <media/v4l2-common.h>
  18519. +
  18520. +#include "mmal-common.h"
  18521. +#include "mmal-vchiq.h"
  18522. +#include "mmal-parameters.h"
  18523. +#include "bcm2835-camera.h"
  18524. +
  18525. +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
  18526. + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
  18527. + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
  18528. + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
  18529. + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
  18530. + * -4 to +4
  18531. + */
  18532. +static const s64 ev_bias_qmenu[] = {
  18533. + -4000, -3667, -3333,
  18534. + -3000, -2667, -2333,
  18535. + -2000, -1667, -1333,
  18536. + -1000, -667, -333,
  18537. + 0, 333, 667,
  18538. + 1000, 1333, 1667,
  18539. + 2000, 2333, 2667,
  18540. + 3000, 3333, 3667,
  18541. + 4000
  18542. +};
  18543. +
  18544. +/* Supported ISO values
  18545. + * ISOO = auto ISO
  18546. + */
  18547. +static const s64 iso_qmenu[] = {
  18548. + 0, 100, 200, 400, 800,
  18549. +};
  18550. +
  18551. +static const s64 mains_freq_qmenu[] = {
  18552. + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  18553. + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
  18554. + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  18555. + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
  18556. +};
  18557. +
  18558. +/* Supported video encode modes */
  18559. +static const s64 bitrate_mode_qmenu[] = {
  18560. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  18561. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  18562. +};
  18563. +
  18564. +enum bm2835_mmal_ctrl_type {
  18565. + MMAL_CONTROL_TYPE_STD,
  18566. + MMAL_CONTROL_TYPE_STD_MENU,
  18567. + MMAL_CONTROL_TYPE_INT_MENU,
  18568. + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
  18569. +};
  18570. +
  18571. +struct bm2835_mmal_v4l2_ctrl;
  18572. +
  18573. +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
  18574. + struct bm2835_mmal_dev *dev,
  18575. + struct v4l2_ctrl *ctrl,
  18576. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
  18577. +
  18578. +struct bm2835_mmal_v4l2_ctrl {
  18579. + u32 id; /* v4l2 control identifier */
  18580. + enum bm2835_mmal_ctrl_type type;
  18581. + /* control minimum value or
  18582. + * mask for MMAL_CONTROL_TYPE_STD_MENU */
  18583. + s32 min;
  18584. + s32 max; /* maximum value of control */
  18585. + s32 def; /* default value of control */
  18586. + s32 step; /* step size of the control */
  18587. + const s64 *imenu; /* integer menu array */
  18588. + u32 mmal_id; /* mmal parameter id */
  18589. + bm2835_mmal_v4l2_ctrl_cb *setter;
  18590. + bool ignore_errors;
  18591. +};
  18592. +
  18593. +struct v4l2_to_mmal_effects_setting {
  18594. + u32 v4l2_effect;
  18595. + u32 mmal_effect;
  18596. + s32 col_fx_enable;
  18597. + s32 col_fx_fixed_cbcr;
  18598. + u32 u;
  18599. + u32 v;
  18600. + u32 num_effect_params;
  18601. + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
  18602. +};
  18603. +
  18604. +static const struct v4l2_to_mmal_effects_setting
  18605. + v4l2_to_mmal_effects_values[] = {
  18606. + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
  18607. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18608. + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
  18609. + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
  18610. + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
  18611. + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
  18612. + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
  18613. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18614. + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
  18615. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18616. + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
  18617. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18618. + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
  18619. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18620. + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  18621. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18622. + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
  18623. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18624. + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
  18625. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18626. + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
  18627. + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
  18628. + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
  18629. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18630. + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
  18631. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  18632. + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
  18633. + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
  18634. + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  18635. + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
  18636. + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
  18637. + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
  18638. +};
  18639. +
  18640. +struct v4l2_mmal_scene_config {
  18641. + enum v4l2_scene_mode v4l2_scene;
  18642. + enum mmal_parameter_exposuremode exposure_mode;
  18643. + enum mmal_parameter_exposuremeteringmode metering_mode;
  18644. +};
  18645. +
  18646. +static const struct v4l2_mmal_scene_config scene_configs[] = {
  18647. + /* V4L2_SCENE_MODE_NONE automatically added */
  18648. + {
  18649. + V4L2_SCENE_MODE_NIGHT,
  18650. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  18651. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  18652. + },
  18653. + {
  18654. + V4L2_SCENE_MODE_SPORTS,
  18655. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  18656. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  18657. + },
  18658. +};
  18659. +
  18660. +/* control handlers*/
  18661. +
  18662. +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
  18663. + struct v4l2_ctrl *ctrl,
  18664. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18665. +{
  18666. + struct mmal_parameter_rational rational_value;
  18667. + struct vchiq_mmal_port *control;
  18668. +
  18669. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18670. +
  18671. + rational_value.num = ctrl->val;
  18672. + rational_value.den = 100;
  18673. +
  18674. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18675. + mmal_ctrl->mmal_id,
  18676. + &rational_value,
  18677. + sizeof(rational_value));
  18678. +}
  18679. +
  18680. +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
  18681. + struct v4l2_ctrl *ctrl,
  18682. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18683. +{
  18684. + u32 u32_value;
  18685. + struct vchiq_mmal_port *control;
  18686. +
  18687. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18688. +
  18689. + u32_value = ctrl->val;
  18690. +
  18691. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18692. + mmal_ctrl->mmal_id,
  18693. + &u32_value, sizeof(u32_value));
  18694. +}
  18695. +
  18696. +static int ctrl_set_value_menu(struct bm2835_mmal_dev *dev,
  18697. + struct v4l2_ctrl *ctrl,
  18698. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18699. +{
  18700. + u32 u32_value;
  18701. + struct vchiq_mmal_port *control;
  18702. +
  18703. + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
  18704. + return 1;
  18705. +
  18706. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18707. +
  18708. + u32_value = mmal_ctrl->imenu[ctrl->val];
  18709. +
  18710. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18711. + mmal_ctrl->mmal_id,
  18712. + &u32_value, sizeof(u32_value));
  18713. +}
  18714. +
  18715. +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
  18716. + struct v4l2_ctrl *ctrl,
  18717. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18718. +{
  18719. + s32 s32_value;
  18720. + struct vchiq_mmal_port *control;
  18721. +
  18722. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18723. +
  18724. + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
  18725. +
  18726. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18727. + mmal_ctrl->mmal_id,
  18728. + &s32_value, sizeof(s32_value));
  18729. +}
  18730. +
  18731. +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
  18732. + struct v4l2_ctrl *ctrl,
  18733. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18734. +{
  18735. + int ret;
  18736. + u32 u32_value;
  18737. + struct vchiq_mmal_component *camera;
  18738. +
  18739. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  18740. +
  18741. + u32_value = ((ctrl->val % 360) / 90) * 90;
  18742. +
  18743. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  18744. + mmal_ctrl->mmal_id,
  18745. + &u32_value, sizeof(u32_value));
  18746. + if (ret < 0)
  18747. + return ret;
  18748. +
  18749. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  18750. + mmal_ctrl->mmal_id,
  18751. + &u32_value, sizeof(u32_value));
  18752. + if (ret < 0)
  18753. + return ret;
  18754. +
  18755. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  18756. + mmal_ctrl->mmal_id,
  18757. + &u32_value, sizeof(u32_value));
  18758. +
  18759. + return ret;
  18760. +}
  18761. +
  18762. +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
  18763. + struct v4l2_ctrl *ctrl,
  18764. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18765. +{
  18766. + int ret;
  18767. + u32 u32_value;
  18768. + struct vchiq_mmal_component *camera;
  18769. +
  18770. + if (ctrl->id == V4L2_CID_HFLIP)
  18771. + dev->hflip = ctrl->val;
  18772. + else
  18773. + dev->vflip = ctrl->val;
  18774. +
  18775. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  18776. +
  18777. + if (dev->hflip && dev->vflip)
  18778. + u32_value = MMAL_PARAM_MIRROR_BOTH;
  18779. + else if (dev->hflip)
  18780. + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
  18781. + else if (dev->vflip)
  18782. + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
  18783. + else
  18784. + u32_value = MMAL_PARAM_MIRROR_NONE;
  18785. +
  18786. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  18787. + mmal_ctrl->mmal_id,
  18788. + &u32_value, sizeof(u32_value));
  18789. + if (ret < 0)
  18790. + return ret;
  18791. +
  18792. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  18793. + mmal_ctrl->mmal_id,
  18794. + &u32_value, sizeof(u32_value));
  18795. + if (ret < 0)
  18796. + return ret;
  18797. +
  18798. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  18799. + mmal_ctrl->mmal_id,
  18800. + &u32_value, sizeof(u32_value));
  18801. +
  18802. + return ret;
  18803. +
  18804. +}
  18805. +
  18806. +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
  18807. + struct v4l2_ctrl *ctrl,
  18808. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18809. +{
  18810. + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode_user;
  18811. + u32 shutter_speed = 0;
  18812. + struct vchiq_mmal_port *control;
  18813. + int ret = 0;
  18814. +
  18815. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18816. +
  18817. + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
  18818. + /* V4L2 is in 100usec increments.
  18819. + * MMAL is 1usec.
  18820. + */
  18821. + dev->manual_shutter_speed = ctrl->val * 100;
  18822. + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
  18823. + switch (ctrl->val) {
  18824. + case V4L2_EXPOSURE_AUTO:
  18825. + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
  18826. + break;
  18827. +
  18828. + case V4L2_EXPOSURE_MANUAL:
  18829. + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
  18830. + break;
  18831. + }
  18832. + dev->exposure_mode_user = exp_mode;
  18833. + dev->exposure_mode_v4l2_user = ctrl->val;
  18834. + } else if (mmal_ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
  18835. + dev->exp_auto_priority = ctrl->val;
  18836. + }
  18837. +
  18838. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  18839. + if (exp_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  18840. + shutter_speed = dev->manual_shutter_speed;
  18841. +
  18842. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  18843. + control,
  18844. + MMAL_PARAMETER_SHUTTER_SPEED,
  18845. + &shutter_speed,
  18846. + sizeof(shutter_speed));
  18847. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18848. + control,
  18849. + MMAL_PARAMETER_EXPOSURE_MODE,
  18850. + &exp_mode,
  18851. + sizeof(u32));
  18852. + dev->exposure_mode_active = exp_mode;
  18853. + }
  18854. + /* exposure_dynamic_framerate (V4L2_CID_EXPOSURE_AUTO_PRIORITY) should
  18855. + * always apply irrespective of scene mode.
  18856. + */
  18857. + ret += set_framerate_params(dev);
  18858. +
  18859. + return ret;
  18860. +}
  18861. +
  18862. +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
  18863. + struct v4l2_ctrl *ctrl,
  18864. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18865. +{
  18866. + switch (ctrl->val) {
  18867. + case V4L2_EXPOSURE_METERING_AVERAGE:
  18868. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  18869. + break;
  18870. +
  18871. + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  18872. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  18873. + break;
  18874. +
  18875. + case V4L2_EXPOSURE_METERING_SPOT:
  18876. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  18877. + break;
  18878. +
  18879. + /* todo matrix weighting not added to Linux API till 3.9
  18880. + case V4L2_EXPOSURE_METERING_MATRIX:
  18881. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  18882. + break;
  18883. + */
  18884. +
  18885. + }
  18886. +
  18887. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  18888. + struct vchiq_mmal_port *control;
  18889. + u32 u32_value = dev->metering_mode;
  18890. +
  18891. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18892. +
  18893. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18894. + mmal_ctrl->mmal_id,
  18895. + &u32_value, sizeof(u32_value));
  18896. + } else
  18897. + return 0;
  18898. +}
  18899. +
  18900. +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
  18901. + struct v4l2_ctrl *ctrl,
  18902. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18903. +{
  18904. + u32 u32_value;
  18905. + struct vchiq_mmal_port *control;
  18906. +
  18907. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18908. +
  18909. + switch (ctrl->val) {
  18910. + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  18911. + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
  18912. + break;
  18913. + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  18914. + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
  18915. + break;
  18916. + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  18917. + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
  18918. + break;
  18919. + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  18920. + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
  18921. + break;
  18922. + }
  18923. +
  18924. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18925. + mmal_ctrl->mmal_id,
  18926. + &u32_value, sizeof(u32_value));
  18927. +}
  18928. +
  18929. +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
  18930. + struct v4l2_ctrl *ctrl,
  18931. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18932. +{
  18933. + u32 u32_value;
  18934. + struct vchiq_mmal_port *control;
  18935. +
  18936. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18937. +
  18938. + switch (ctrl->val) {
  18939. + case V4L2_WHITE_BALANCE_MANUAL:
  18940. + u32_value = MMAL_PARAM_AWBMODE_OFF;
  18941. + break;
  18942. +
  18943. + case V4L2_WHITE_BALANCE_AUTO:
  18944. + u32_value = MMAL_PARAM_AWBMODE_AUTO;
  18945. + break;
  18946. +
  18947. + case V4L2_WHITE_BALANCE_INCANDESCENT:
  18948. + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
  18949. + break;
  18950. +
  18951. + case V4L2_WHITE_BALANCE_FLUORESCENT:
  18952. + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
  18953. + break;
  18954. +
  18955. + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
  18956. + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
  18957. + break;
  18958. +
  18959. + case V4L2_WHITE_BALANCE_HORIZON:
  18960. + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
  18961. + break;
  18962. +
  18963. + case V4L2_WHITE_BALANCE_DAYLIGHT:
  18964. + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
  18965. + break;
  18966. +
  18967. + case V4L2_WHITE_BALANCE_FLASH:
  18968. + u32_value = MMAL_PARAM_AWBMODE_FLASH;
  18969. + break;
  18970. +
  18971. + case V4L2_WHITE_BALANCE_CLOUDY:
  18972. + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
  18973. + break;
  18974. +
  18975. + case V4L2_WHITE_BALANCE_SHADE:
  18976. + u32_value = MMAL_PARAM_AWBMODE_SHADE;
  18977. + break;
  18978. +
  18979. + }
  18980. +
  18981. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18982. + mmal_ctrl->mmal_id,
  18983. + &u32_value, sizeof(u32_value));
  18984. +}
  18985. +
  18986. +static int ctrl_set_awb_gains(struct bm2835_mmal_dev *dev,
  18987. + struct v4l2_ctrl *ctrl,
  18988. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18989. +{
  18990. + struct vchiq_mmal_port *control;
  18991. + struct mmal_parameter_awbgains gains;
  18992. +
  18993. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18994. +
  18995. + if (ctrl->id == V4L2_CID_RED_BALANCE)
  18996. + dev->red_gain = ctrl->val;
  18997. + else if (ctrl->id == V4L2_CID_BLUE_BALANCE)
  18998. + dev->blue_gain = ctrl->val;
  18999. +
  19000. + gains.r_gain.num = dev->red_gain;
  19001. + gains.b_gain.num = dev->blue_gain;
  19002. + gains.r_gain.den = gains.b_gain.den = 1000;
  19003. +
  19004. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19005. + mmal_ctrl->mmal_id,
  19006. + &gains, sizeof(gains));
  19007. +}
  19008. +
  19009. +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
  19010. + struct v4l2_ctrl *ctrl,
  19011. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19012. +{
  19013. + int ret = -EINVAL;
  19014. + int i, j;
  19015. + struct vchiq_mmal_port *control;
  19016. + struct mmal_parameter_imagefx_parameters imagefx;
  19017. +
  19018. + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
  19019. + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
  19020. +
  19021. + imagefx.effect =
  19022. + v4l2_to_mmal_effects_values[i].mmal_effect;
  19023. + imagefx.num_effect_params =
  19024. + v4l2_to_mmal_effects_values[i].num_effect_params;
  19025. +
  19026. + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
  19027. + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
  19028. +
  19029. + for (j = 0; j < imagefx.num_effect_params; j++)
  19030. + imagefx.effect_parameter[j] =
  19031. + v4l2_to_mmal_effects_values[i].effect_params[j];
  19032. +
  19033. + dev->colourfx.enable =
  19034. + v4l2_to_mmal_effects_values[i].col_fx_enable;
  19035. + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
  19036. + dev->colourfx.u =
  19037. + v4l2_to_mmal_effects_values[i].u;
  19038. + dev->colourfx.v =
  19039. + v4l2_to_mmal_effects_values[i].v;
  19040. + }
  19041. +
  19042. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19043. +
  19044. + ret = vchiq_mmal_port_parameter_set(
  19045. + dev->instance, control,
  19046. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  19047. + &imagefx, sizeof(imagefx));
  19048. + if (ret)
  19049. + goto exit;
  19050. +
  19051. + ret = vchiq_mmal_port_parameter_set(
  19052. + dev->instance, control,
  19053. + MMAL_PARAMETER_COLOUR_EFFECT,
  19054. + &dev->colourfx, sizeof(dev->colourfx));
  19055. + }
  19056. + }
  19057. +
  19058. +exit:
  19059. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19060. + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
  19061. + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
  19062. + dev->colourfx.enable ? "true" : "false",
  19063. + dev->colourfx.u, dev->colourfx.v,
  19064. + ret, (ret == 0 ? 0 : -EINVAL));
  19065. + return (ret == 0 ? 0 : EINVAL);
  19066. +}
  19067. +
  19068. +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
  19069. + struct v4l2_ctrl *ctrl,
  19070. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19071. +{
  19072. + int ret = -EINVAL;
  19073. + struct vchiq_mmal_port *control;
  19074. +
  19075. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19076. +
  19077. + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
  19078. + dev->colourfx.enable = ctrl->val & 0xff;
  19079. +
  19080. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  19081. + MMAL_PARAMETER_COLOUR_EFFECT,
  19082. + &dev->colourfx, sizeof(dev->colourfx));
  19083. +
  19084. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19085. + "%s: After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
  19086. + __func__, mmal_ctrl, ctrl->id, ctrl->val, ret,
  19087. + (ret == 0 ? 0 : -EINVAL));
  19088. + return (ret == 0 ? 0 : EINVAL);
  19089. +}
  19090. +
  19091. +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
  19092. + struct v4l2_ctrl *ctrl,
  19093. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19094. +{
  19095. + int ret;
  19096. + struct vchiq_mmal_port *encoder_out;
  19097. +
  19098. + dev->capture.encode_bitrate = ctrl->val;
  19099. +
  19100. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  19101. +
  19102. + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  19103. + mmal_ctrl->mmal_id,
  19104. + &ctrl->val, sizeof(ctrl->val));
  19105. + ret = 0;
  19106. + return ret;
  19107. +}
  19108. +
  19109. +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
  19110. + struct v4l2_ctrl *ctrl,
  19111. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19112. +{
  19113. + u32 bitrate_mode;
  19114. + struct vchiq_mmal_port *encoder_out;
  19115. +
  19116. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  19117. +
  19118. + dev->capture.encode_bitrate_mode = ctrl->val;
  19119. + switch (ctrl->val) {
  19120. + default:
  19121. + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
  19122. + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
  19123. + break;
  19124. + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
  19125. + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
  19126. + break;
  19127. + }
  19128. +
  19129. + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  19130. + mmal_ctrl->mmal_id,
  19131. + &bitrate_mode,
  19132. + sizeof(bitrate_mode));
  19133. + return 0;
  19134. +}
  19135. +
  19136. +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
  19137. + struct v4l2_ctrl *ctrl,
  19138. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19139. +{
  19140. + u32 u32_value;
  19141. + struct vchiq_mmal_port *jpeg_out;
  19142. +
  19143. + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  19144. +
  19145. + u32_value = ctrl->val;
  19146. +
  19147. + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
  19148. + mmal_ctrl->mmal_id,
  19149. + &u32_value, sizeof(u32_value));
  19150. +}
  19151. +
  19152. +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
  19153. + struct v4l2_ctrl *ctrl,
  19154. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19155. +{
  19156. + u32 u32_value;
  19157. + struct vchiq_mmal_port *vid_enc_ctl;
  19158. +
  19159. + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  19160. +
  19161. + u32_value = ctrl->val;
  19162. +
  19163. + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
  19164. + mmal_ctrl->mmal_id,
  19165. + &u32_value, sizeof(u32_value));
  19166. +}
  19167. +
  19168. +static int ctrl_set_video_encode_profile_level(struct bm2835_mmal_dev *dev,
  19169. + struct v4l2_ctrl *ctrl,
  19170. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19171. +{
  19172. + struct mmal_parameter_video_profile param;
  19173. + int ret = 0;
  19174. +
  19175. + if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_PROFILE) {
  19176. + switch (ctrl->val) {
  19177. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  19178. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  19179. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  19180. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  19181. + dev->capture.enc_profile = ctrl->val;
  19182. + break;
  19183. + default:
  19184. + ret = -EINVAL;
  19185. + break;
  19186. + }
  19187. + } else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_LEVEL) {
  19188. + switch (ctrl->val) {
  19189. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  19190. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  19191. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  19192. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  19193. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  19194. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  19195. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  19196. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  19197. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  19198. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  19199. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  19200. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  19201. + dev->capture.enc_level = ctrl->val;
  19202. + break;
  19203. + default:
  19204. + ret = -EINVAL;
  19205. + break;
  19206. + }
  19207. + }
  19208. +
  19209. + if (!ret) {
  19210. + switch (dev->capture.enc_profile) {
  19211. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  19212. + param.profile = MMAL_VIDEO_PROFILE_H264_BASELINE;
  19213. + break;
  19214. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  19215. + param.profile =
  19216. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE;
  19217. + break;
  19218. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  19219. + param.profile = MMAL_VIDEO_PROFILE_H264_MAIN;
  19220. + break;
  19221. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  19222. + param.profile = MMAL_VIDEO_PROFILE_H264_HIGH;
  19223. + break;
  19224. + default:
  19225. + /* Should never get here */
  19226. + break;
  19227. + }
  19228. +
  19229. + switch (dev->capture.enc_level) {
  19230. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  19231. + param.level = MMAL_VIDEO_LEVEL_H264_1;
  19232. + break;
  19233. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  19234. + param.level = MMAL_VIDEO_LEVEL_H264_1b;
  19235. + break;
  19236. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  19237. + param.level = MMAL_VIDEO_LEVEL_H264_11;
  19238. + break;
  19239. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  19240. + param.level = MMAL_VIDEO_LEVEL_H264_12;
  19241. + break;
  19242. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  19243. + param.level = MMAL_VIDEO_LEVEL_H264_13;
  19244. + break;
  19245. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  19246. + param.level = MMAL_VIDEO_LEVEL_H264_2;
  19247. + break;
  19248. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  19249. + param.level = MMAL_VIDEO_LEVEL_H264_21;
  19250. + break;
  19251. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  19252. + param.level = MMAL_VIDEO_LEVEL_H264_22;
  19253. + break;
  19254. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  19255. + param.level = MMAL_VIDEO_LEVEL_H264_3;
  19256. + break;
  19257. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  19258. + param.level = MMAL_VIDEO_LEVEL_H264_31;
  19259. + break;
  19260. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  19261. + param.level = MMAL_VIDEO_LEVEL_H264_32;
  19262. + break;
  19263. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  19264. + param.level = MMAL_VIDEO_LEVEL_H264_4;
  19265. + break;
  19266. + default:
  19267. + /* Should never get here */
  19268. + break;
  19269. + }
  19270. +
  19271. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  19272. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0],
  19273. + mmal_ctrl->mmal_id,
  19274. + &param, sizeof(param));
  19275. + }
  19276. + return ret;
  19277. +}
  19278. +
  19279. +static int ctrl_set_scene_mode(struct bm2835_mmal_dev *dev,
  19280. + struct v4l2_ctrl *ctrl,
  19281. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19282. +{
  19283. + int ret = 0;
  19284. + int shutter_speed;
  19285. + struct vchiq_mmal_port *control;
  19286. +
  19287. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19288. + "scene mode selected %d, was %d\n", ctrl->val,
  19289. + dev->scene_mode);
  19290. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19291. +
  19292. + if (ctrl->val == dev->scene_mode)
  19293. + return 0;
  19294. +
  19295. + if (ctrl->val == V4L2_SCENE_MODE_NONE) {
  19296. + /* Restore all user selections */
  19297. + dev->scene_mode = V4L2_SCENE_MODE_NONE;
  19298. +
  19299. + if (dev->exposure_mode_user == MMAL_PARAM_EXPOSUREMODE_OFF)
  19300. + shutter_speed = dev->manual_shutter_speed;
  19301. + else
  19302. + shutter_speed = 0;
  19303. +
  19304. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19305. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  19306. + __func__, shutter_speed, dev->exposure_mode_user,
  19307. + dev->metering_mode);
  19308. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  19309. + control,
  19310. + MMAL_PARAMETER_SHUTTER_SPEED,
  19311. + &shutter_speed,
  19312. + sizeof(shutter_speed));
  19313. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19314. + control,
  19315. + MMAL_PARAMETER_EXPOSURE_MODE,
  19316. + &dev->exposure_mode_user,
  19317. + sizeof(u32));
  19318. + dev->exposure_mode_active = dev->exposure_mode_user;
  19319. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19320. + control,
  19321. + MMAL_PARAMETER_EXP_METERING_MODE,
  19322. + &dev->metering_mode,
  19323. + sizeof(u32));
  19324. + ret += set_framerate_params(dev);
  19325. + } else {
  19326. + /* Set up scene mode */
  19327. + int i;
  19328. + const struct v4l2_mmal_scene_config *scene = NULL;
  19329. + int shutter_speed;
  19330. + enum mmal_parameter_exposuremode exposure_mode;
  19331. + enum mmal_parameter_exposuremeteringmode metering_mode;
  19332. +
  19333. + for (i = 0; i < ARRAY_SIZE(scene_configs); i++) {
  19334. + if (scene_configs[i].v4l2_scene ==
  19335. + ctrl->val) {
  19336. + scene = &scene_configs[i];
  19337. + break;
  19338. + }
  19339. + }
  19340. + if (i >= ARRAY_SIZE(scene_configs))
  19341. + return -EINVAL;
  19342. +
  19343. + /* Set all the values */
  19344. + dev->scene_mode = ctrl->val;
  19345. +
  19346. + if (scene->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  19347. + shutter_speed = dev->manual_shutter_speed;
  19348. + else
  19349. + shutter_speed = 0;
  19350. + exposure_mode = scene->exposure_mode;
  19351. + metering_mode = scene->metering_mode;
  19352. +
  19353. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19354. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  19355. + __func__, shutter_speed, exposure_mode, metering_mode);
  19356. +
  19357. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  19358. + MMAL_PARAMETER_SHUTTER_SPEED,
  19359. + &shutter_speed,
  19360. + sizeof(shutter_speed));
  19361. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19362. + control,
  19363. + MMAL_PARAMETER_EXPOSURE_MODE,
  19364. + &exposure_mode,
  19365. + sizeof(u32));
  19366. + dev->exposure_mode_active = exposure_mode;
  19367. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  19368. + MMAL_PARAMETER_EXPOSURE_MODE,
  19369. + &exposure_mode,
  19370. + sizeof(u32));
  19371. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  19372. + MMAL_PARAMETER_EXP_METERING_MODE,
  19373. + &metering_mode,
  19374. + sizeof(u32));
  19375. + ret += set_framerate_params(dev);
  19376. + }
  19377. + if (ret) {
  19378. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19379. + "%s: Setting scene to %d, ret=%d\n",
  19380. + __func__, ctrl->val, ret);
  19381. + ret = -EINVAL;
  19382. + }
  19383. + return 0;
  19384. +}
  19385. +
  19386. +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
  19387. +{
  19388. + struct bm2835_mmal_dev *dev =
  19389. + container_of(ctrl->handler, struct bm2835_mmal_dev,
  19390. + ctrl_handler);
  19391. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
  19392. + int ret;
  19393. +
  19394. + if ((mmal_ctrl == NULL) ||
  19395. + (mmal_ctrl->id != ctrl->id) ||
  19396. + (mmal_ctrl->setter == NULL)) {
  19397. + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
  19398. + return -EINVAL;
  19399. + }
  19400. +
  19401. + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
  19402. + if (ret)
  19403. + pr_warn("ctrl id:%d/MMAL param %08X- returned ret %d\n",
  19404. + ctrl->id, mmal_ctrl->mmal_id, ret);
  19405. + if (mmal_ctrl->ignore_errors)
  19406. + ret = 0;
  19407. + return ret;
  19408. +}
  19409. +
  19410. +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
  19411. + .s_ctrl = bm2835_mmal_s_ctrl,
  19412. +};
  19413. +
  19414. +
  19415. +
  19416. +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
  19417. + {
  19418. + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
  19419. + -100, 100, 0, 1, NULL,
  19420. + MMAL_PARAMETER_SATURATION,
  19421. + &ctrl_set_rational,
  19422. + false
  19423. + },
  19424. + {
  19425. + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
  19426. + -100, 100, 0, 1, NULL,
  19427. + MMAL_PARAMETER_SHARPNESS,
  19428. + &ctrl_set_rational,
  19429. + false
  19430. + },
  19431. + {
  19432. + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
  19433. + -100, 100, 0, 1, NULL,
  19434. + MMAL_PARAMETER_CONTRAST,
  19435. + &ctrl_set_rational,
  19436. + false
  19437. + },
  19438. + {
  19439. + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
  19440. + 0, 100, 50, 1, NULL,
  19441. + MMAL_PARAMETER_BRIGHTNESS,
  19442. + &ctrl_set_rational,
  19443. + false
  19444. + },
  19445. + {
  19446. + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
  19447. + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
  19448. + MMAL_PARAMETER_ISO,
  19449. + &ctrl_set_value_menu,
  19450. + false
  19451. + },
  19452. + {
  19453. + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
  19454. + 0, 1, 0, 1, NULL,
  19455. + MMAL_PARAMETER_VIDEO_STABILISATION,
  19456. + &ctrl_set_value,
  19457. + false
  19458. + },
  19459. +/* {
  19460. + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
  19461. + }, */
  19462. + {
  19463. + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
  19464. + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
  19465. + MMAL_PARAMETER_EXPOSURE_MODE,
  19466. + &ctrl_set_exposure,
  19467. + false
  19468. + },
  19469. +/* todo this needs mixing in with set exposure
  19470. + {
  19471. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  19472. + },
  19473. + */
  19474. + {
  19475. + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
  19476. + /* Units of 100usecs */
  19477. + 1, 1*1000*10, 100*10, 1, NULL,
  19478. + MMAL_PARAMETER_SHUTTER_SPEED,
  19479. + &ctrl_set_exposure,
  19480. + false
  19481. + },
  19482. + {
  19483. + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
  19484. + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
  19485. + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
  19486. + MMAL_PARAMETER_EXPOSURE_COMP,
  19487. + &ctrl_set_value_ev,
  19488. + false
  19489. + },
  19490. + {
  19491. + V4L2_CID_EXPOSURE_AUTO_PRIORITY, MMAL_CONTROL_TYPE_STD,
  19492. + 0, 1,
  19493. + 0, 1, NULL,
  19494. + 0, /* Dummy MMAL ID as it gets mapped into FPS range*/
  19495. + &ctrl_set_exposure,
  19496. + false
  19497. + },
  19498. + {
  19499. + V4L2_CID_EXPOSURE_METERING,
  19500. + MMAL_CONTROL_TYPE_STD_MENU,
  19501. + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
  19502. + MMAL_PARAMETER_EXP_METERING_MODE,
  19503. + &ctrl_set_metering_mode,
  19504. + false
  19505. + },
  19506. + {
  19507. + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  19508. + MMAL_CONTROL_TYPE_STD_MENU,
  19509. + ~0x3ff, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
  19510. + MMAL_PARAMETER_AWB_MODE,
  19511. + &ctrl_set_awb_mode,
  19512. + false
  19513. + },
  19514. + {
  19515. + V4L2_CID_RED_BALANCE, MMAL_CONTROL_TYPE_STD,
  19516. + 1, 7999, 1000, 1, NULL,
  19517. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  19518. + &ctrl_set_awb_gains,
  19519. + false
  19520. + },
  19521. + {
  19522. + V4L2_CID_BLUE_BALANCE, MMAL_CONTROL_TYPE_STD,
  19523. + 1, 7999, 1000, 1, NULL,
  19524. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  19525. + &ctrl_set_awb_gains,
  19526. + false
  19527. + },
  19528. + {
  19529. + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
  19530. + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
  19531. + MMAL_PARAMETER_IMAGE_EFFECT,
  19532. + &ctrl_set_image_effect,
  19533. + false
  19534. + },
  19535. + {
  19536. + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
  19537. + 0, 0xffff, 0x8080, 1, NULL,
  19538. + MMAL_PARAMETER_COLOUR_EFFECT,
  19539. + &ctrl_set_colfx,
  19540. + false
  19541. + },
  19542. + {
  19543. + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
  19544. + 0, 360, 0, 90, NULL,
  19545. + MMAL_PARAMETER_ROTATION,
  19546. + &ctrl_set_rotate,
  19547. + false
  19548. + },
  19549. + {
  19550. + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
  19551. + 0, 1, 0, 1, NULL,
  19552. + MMAL_PARAMETER_MIRROR,
  19553. + &ctrl_set_flip,
  19554. + false
  19555. + },
  19556. + {
  19557. + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
  19558. + 0, 1, 0, 1, NULL,
  19559. + MMAL_PARAMETER_MIRROR,
  19560. + &ctrl_set_flip,
  19561. + false
  19562. + },
  19563. + {
  19564. + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  19565. + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
  19566. + 0, 0, bitrate_mode_qmenu,
  19567. + MMAL_PARAMETER_RATECONTROL,
  19568. + &ctrl_set_bitrate_mode,
  19569. + false
  19570. + },
  19571. + {
  19572. + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
  19573. + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
  19574. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  19575. + &ctrl_set_bitrate,
  19576. + false
  19577. + },
  19578. + {
  19579. + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
  19580. + 1, 100,
  19581. + 30, 1, NULL,
  19582. + MMAL_PARAMETER_JPEG_Q_FACTOR,
  19583. + &ctrl_set_image_encode_output,
  19584. + false
  19585. + },
  19586. + {
  19587. + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
  19588. + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
  19589. + 1, 1, NULL,
  19590. + MMAL_PARAMETER_FLICKER_AVOID,
  19591. + &ctrl_set_flicker_avoidance,
  19592. + false
  19593. + },
  19594. + {
  19595. + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
  19596. + 0, 1,
  19597. + 0, 1, NULL,
  19598. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
  19599. + &ctrl_set_video_encode_param_output,
  19600. + true /* Errors ignored as requires latest firmware to work */
  19601. + },
  19602. + {
  19603. + V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  19604. + MMAL_CONTROL_TYPE_STD_MENU,
  19605. + ~((1<<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  19606. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
  19607. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  19608. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  19609. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  19610. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1, NULL,
  19611. + MMAL_PARAMETER_PROFILE,
  19612. + &ctrl_set_video_encode_profile_level,
  19613. + false
  19614. + },
  19615. + {
  19616. + V4L2_CID_MPEG_VIDEO_H264_LEVEL, MMAL_CONTROL_TYPE_STD_MENU,
  19617. + ~((1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
  19618. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1B) |
  19619. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_1) |
  19620. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_2) |
  19621. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_3) |
  19622. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  19623. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_1) |
  19624. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_2) |
  19625. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  19626. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  19627. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  19628. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
  19629. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  19630. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1, NULL,
  19631. + MMAL_PARAMETER_PROFILE,
  19632. + &ctrl_set_video_encode_profile_level,
  19633. + false
  19634. + },
  19635. + {
  19636. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  19637. + -1, /* Min is computed at runtime */
  19638. + V4L2_SCENE_MODE_TEXT,
  19639. + V4L2_SCENE_MODE_NONE, 1, NULL,
  19640. + MMAL_PARAMETER_PROFILE,
  19641. + &ctrl_set_scene_mode,
  19642. + false
  19643. + },
  19644. + {
  19645. + V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, MMAL_CONTROL_TYPE_STD,
  19646. + 0, 0x7FFFFFFF, 60, 1, NULL,
  19647. + MMAL_PARAMETER_INTRAPERIOD,
  19648. + &ctrl_set_video_encode_param_output,
  19649. + false
  19650. + },
  19651. +};
  19652. +
  19653. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
  19654. +{
  19655. + int c;
  19656. + int ret = 0;
  19657. +
  19658. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  19659. + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
  19660. + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
  19661. + &v4l2_ctrls[c]);
  19662. + if (!v4l2_ctrls[c].ignore_errors && ret) {
  19663. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19664. + "Failed when setting default values for ctrl %d\n",
  19665. + c);
  19666. + break;
  19667. + }
  19668. + }
  19669. + }
  19670. + return ret;
  19671. +}
  19672. +
  19673. +int set_framerate_params(struct bm2835_mmal_dev *dev)
  19674. +{
  19675. + struct mmal_parameter_fps_range fps_range;
  19676. + int ret;
  19677. +
  19678. + if ((dev->exposure_mode_active != MMAL_PARAM_EXPOSUREMODE_OFF) &&
  19679. + (dev->exp_auto_priority)) {
  19680. + /* Variable FPS. Define min FPS as 1fps.
  19681. + * Max as max defined FPS.
  19682. + */
  19683. + fps_range.fps_low.num = 1;
  19684. + fps_range.fps_low.den = 1;
  19685. + fps_range.fps_high.num = dev->capture.timeperframe.denominator;
  19686. + fps_range.fps_high.den = dev->capture.timeperframe.numerator;
  19687. + } else {
  19688. + /* Fixed FPS - set min and max to be the same */
  19689. + fps_range.fps_low.num = fps_range.fps_high.num =
  19690. + dev->capture.timeperframe.denominator;
  19691. + fps_range.fps_low.den = fps_range.fps_high.den =
  19692. + dev->capture.timeperframe.numerator;
  19693. + }
  19694. +
  19695. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19696. + "Set fps range to %d/%d to %d/%d\n",
  19697. + fps_range.fps_low.num,
  19698. + fps_range.fps_low.den,
  19699. + fps_range.fps_high.num,
  19700. + fps_range.fps_high.den
  19701. + );
  19702. +
  19703. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  19704. + &dev->component[MMAL_COMPONENT_CAMERA]->
  19705. + output[MMAL_CAMERA_PORT_PREVIEW],
  19706. + MMAL_PARAMETER_FPS_RANGE,
  19707. + &fps_range, sizeof(fps_range));
  19708. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19709. + &dev->component[MMAL_COMPONENT_CAMERA]->
  19710. + output[MMAL_CAMERA_PORT_VIDEO],
  19711. + MMAL_PARAMETER_FPS_RANGE,
  19712. + &fps_range, sizeof(fps_range));
  19713. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19714. + &dev->component[MMAL_COMPONENT_CAMERA]->
  19715. + output[MMAL_CAMERA_PORT_CAPTURE],
  19716. + MMAL_PARAMETER_FPS_RANGE,
  19717. + &fps_range, sizeof(fps_range));
  19718. + if (ret)
  19719. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19720. + "Failed to set fps ret %d\n",
  19721. + ret);
  19722. +
  19723. + return ret;
  19724. +
  19725. +}
  19726. +
  19727. +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
  19728. + struct v4l2_ctrl_handler *hdl)
  19729. +{
  19730. + int c;
  19731. + const struct bm2835_mmal_v4l2_ctrl *ctrl;
  19732. +
  19733. + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
  19734. +
  19735. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  19736. + ctrl = &v4l2_ctrls[c];
  19737. +
  19738. + switch (ctrl->type) {
  19739. + case MMAL_CONTROL_TYPE_STD:
  19740. + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
  19741. + &bm2835_mmal_ctrl_ops, ctrl->id,
  19742. + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
  19743. + break;
  19744. +
  19745. + case MMAL_CONTROL_TYPE_STD_MENU:
  19746. + {
  19747. + int mask = ctrl->min;
  19748. +
  19749. + if (ctrl->id == V4L2_CID_SCENE_MODE) {
  19750. + /* Special handling to work out the mask
  19751. + * value based on the scene_configs array
  19752. + * at runtime. Reduces the chance of
  19753. + * mismatches.
  19754. + */
  19755. + int i;
  19756. + mask = 1<<V4L2_SCENE_MODE_NONE;
  19757. + for (i = 0;
  19758. + i < ARRAY_SIZE(scene_configs);
  19759. + i++) {
  19760. + mask |= 1<<scene_configs[i].v4l2_scene;
  19761. + }
  19762. + mask = ~mask;
  19763. + }
  19764. +
  19765. + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
  19766. + &bm2835_mmal_ctrl_ops, ctrl->id,
  19767. + ctrl->max, mask, ctrl->def);
  19768. + break;
  19769. + }
  19770. +
  19771. + case MMAL_CONTROL_TYPE_INT_MENU:
  19772. + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
  19773. + &bm2835_mmal_ctrl_ops, ctrl->id,
  19774. + ctrl->max, ctrl->def, ctrl->imenu);
  19775. + break;
  19776. +
  19777. + case MMAL_CONTROL_TYPE_CLUSTER:
  19778. + /* skip this entry when constructing controls */
  19779. + continue;
  19780. + }
  19781. +
  19782. + if (hdl->error)
  19783. + break;
  19784. +
  19785. + dev->ctrls[c]->priv = (void *)ctrl;
  19786. + }
  19787. +
  19788. + if (hdl->error) {
  19789. + pr_err("error adding control %d/%d id 0x%x\n", c,
  19790. + V4L2_CTRL_COUNT, ctrl->id);
  19791. + return hdl->error;
  19792. + }
  19793. +
  19794. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  19795. + ctrl = &v4l2_ctrls[c];
  19796. +
  19797. + switch (ctrl->type) {
  19798. + case MMAL_CONTROL_TYPE_CLUSTER:
  19799. + v4l2_ctrl_auto_cluster(ctrl->min,
  19800. + &dev->ctrls[c+1],
  19801. + ctrl->max,
  19802. + ctrl->def);
  19803. + break;
  19804. +
  19805. + case MMAL_CONTROL_TYPE_STD:
  19806. + case MMAL_CONTROL_TYPE_STD_MENU:
  19807. + case MMAL_CONTROL_TYPE_INT_MENU:
  19808. + break;
  19809. + }
  19810. +
  19811. + }
  19812. +
  19813. + return 0;
  19814. +}
  19815. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/Kconfig linux-rpi/drivers/media/platform/bcm2835/Kconfig
  19816. --- linux-3.17.5/drivers/media/platform/bcm2835/Kconfig 1969-12-31 18:00:00.000000000 -0600
  19817. +++ linux-rpi/drivers/media/platform/bcm2835/Kconfig 2014-12-11 14:02:53.384418001 -0600
  19818. @@ -0,0 +1,25 @@
  19819. +# Broadcom VideoCore IV v4l2 camera support
  19820. +
  19821. +config VIDEO_BCM2835
  19822. + bool "Broadcom BCM2835 camera interface driver"
  19823. + depends on VIDEO_V4L2 && ARCH_BCM2708
  19824. + ---help---
  19825. + Say Y here to enable camera host interface devices for
  19826. + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
  19827. + to a service running on VideoCore.
  19828. +
  19829. +
  19830. +if VIDEO_BCM2835
  19831. +
  19832. +config VIDEO_BCM2835_MMAL
  19833. + tristate "Broadcom BM2835 MMAL camera interface driver"
  19834. + depends on BCM2708_VCHIQ
  19835. + select VIDEOBUF2_VMALLOC
  19836. + ---help---
  19837. + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
  19838. +
  19839. + To compile this driver as a module, choose M here: the
  19840. + module will be called bcm2835-v4l2.o
  19841. +
  19842. +
  19843. +endif # VIDEO_BM2835
  19844. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/Makefile linux-rpi/drivers/media/platform/bcm2835/Makefile
  19845. --- linux-3.17.5/drivers/media/platform/bcm2835/Makefile 1969-12-31 18:00:00.000000000 -0600
  19846. +++ linux-rpi/drivers/media/platform/bcm2835/Makefile 2014-12-11 14:02:53.384418001 -0600
  19847. @@ -0,0 +1,5 @@
  19848. +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
  19849. +
  19850. +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
  19851. +
  19852. +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  19853. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-common.h linux-rpi/drivers/media/platform/bcm2835/mmal-common.h
  19854. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-common.h 1969-12-31 18:00:00.000000000 -0600
  19855. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-common.h 2014-12-11 14:02:53.384418001 -0600
  19856. @@ -0,0 +1,53 @@
  19857. +/*
  19858. + * Broadcom BM2835 V4L2 driver
  19859. + *
  19860. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19861. + *
  19862. + * This file is subject to the terms and conditions of the GNU General Public
  19863. + * License. See the file COPYING in the main directory of this archive
  19864. + * for more details.
  19865. + *
  19866. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19867. + * Dave Stevenson <dsteve@broadcom.com>
  19868. + * Simon Mellor <simellor@broadcom.com>
  19869. + * Luke Diamand <luked@broadcom.com>
  19870. + *
  19871. + * MMAL structures
  19872. + *
  19873. + */
  19874. +
  19875. +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
  19876. +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
  19877. +
  19878. +/** Special value signalling that time is not known */
  19879. +#define MMAL_TIME_UNKNOWN (1LL<<63)
  19880. +
  19881. +/* mapping between v4l and mmal video modes */
  19882. +struct mmal_fmt {
  19883. + char *name;
  19884. + u32 fourcc; /* v4l2 format id */
  19885. + int flags; /* v4l2 flags field */
  19886. + u32 mmal;
  19887. + int depth;
  19888. + u32 mmal_component; /* MMAL component index to be used to encode */
  19889. +};
  19890. +
  19891. +/* buffer for one video frame */
  19892. +struct mmal_buffer {
  19893. + /* v4l buffer data -- must be first */
  19894. + struct vb2_buffer vb;
  19895. +
  19896. + /* list of buffers available */
  19897. + struct list_head list;
  19898. +
  19899. + void *buffer; /* buffer pointer */
  19900. + unsigned long buffer_size; /* size of allocated buffer */
  19901. +};
  19902. +
  19903. +/* */
  19904. +struct mmal_colourfx {
  19905. + s32 enable;
  19906. + u32 u;
  19907. + u32 v;
  19908. +};
  19909. +
  19910. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-encodings.h linux-rpi/drivers/media/platform/bcm2835/mmal-encodings.h
  19911. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-encodings.h 1969-12-31 18:00:00.000000000 -0600
  19912. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-encodings.h 2014-12-11 14:02:53.384418001 -0600
  19913. @@ -0,0 +1,127 @@
  19914. +/*
  19915. + * Broadcom BM2835 V4L2 driver
  19916. + *
  19917. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19918. + *
  19919. + * This file is subject to the terms and conditions of the GNU General Public
  19920. + * License. See the file COPYING in the main directory of this archive
  19921. + * for more details.
  19922. + *
  19923. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19924. + * Dave Stevenson <dsteve@broadcom.com>
  19925. + * Simon Mellor <simellor@broadcom.com>
  19926. + * Luke Diamand <luked@broadcom.com>
  19927. + */
  19928. +#ifndef MMAL_ENCODINGS_H
  19929. +#define MMAL_ENCODINGS_H
  19930. +
  19931. +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
  19932. +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
  19933. +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
  19934. +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
  19935. +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
  19936. +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
  19937. +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
  19938. +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
  19939. +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
  19940. +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
  19941. +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
  19942. +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
  19943. +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
  19944. +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
  19945. +#define MMAL_ENCODING_MJPEG MMAL_FOURCC('M', 'J', 'P', 'G')
  19946. +
  19947. +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
  19948. +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
  19949. +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
  19950. +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
  19951. +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
  19952. +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
  19953. +
  19954. +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
  19955. +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
  19956. +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
  19957. +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
  19958. +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
  19959. +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
  19960. +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
  19961. +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
  19962. +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
  19963. +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
  19964. +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
  19965. +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
  19966. +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
  19967. +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
  19968. +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
  19969. +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
  19970. +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
  19971. +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
  19972. +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
  19973. +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
  19974. +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
  19975. +
  19976. +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
  19977. + * This format is *not* opaque - if requested you will receive full frames
  19978. + * of YUV_UV video.
  19979. + */
  19980. +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
  19981. +
  19982. +/** VideoCore opaque image format, image handles are returned to
  19983. + * the host but not the actual image data.
  19984. + */
  19985. +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
  19986. +
  19987. +/** An EGL image handle
  19988. + */
  19989. +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
  19990. +
  19991. +/* }@ */
  19992. +
  19993. +/** \name Pre-defined audio encodings */
  19994. +/* @{ */
  19995. +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
  19996. +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
  19997. +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
  19998. +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
  19999. +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
  20000. +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
  20001. +
  20002. +/* Pre-defined H264 encoding variants */
  20003. +
  20004. +/** ISO 14496-10 Annex B byte stream format */
  20005. +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
  20006. +/** ISO 14496-15 AVC stream format */
  20007. +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
  20008. +/** Implicitly delineated NAL units without emulation prevention */
  20009. +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
  20010. +
  20011. +
  20012. +/** \defgroup MmalColorSpace List of pre-defined video color spaces
  20013. + * This defines a list of common color spaces. This list isn't exhaustive and
  20014. + * is only provided as a convenience to avoid clients having to use FourCC
  20015. + * codes directly. However components are allowed to define and use their own
  20016. + * FourCC codes.
  20017. + */
  20018. +/* @{ */
  20019. +
  20020. +/** Unknown color space */
  20021. +#define MMAL_COLOR_SPACE_UNKNOWN 0
  20022. +/** ITU-R BT.601-5 [SDTV] */
  20023. +#define MMAL_COLOR_SPACE_ITUR_BT601 MMAL_FOURCC('Y', '6', '0', '1')
  20024. +/** ITU-R BT.709-3 [HDTV] */
  20025. +#define MMAL_COLOR_SPACE_ITUR_BT709 MMAL_FOURCC('Y', '7', '0', '9')
  20026. +/** JPEG JFIF */
  20027. +#define MMAL_COLOR_SPACE_JPEG_JFIF MMAL_FOURCC('Y', 'J', 'F', 'I')
  20028. +/** Title 47 Code of Federal Regulations (2003) 73.682 (a) (20) */
  20029. +#define MMAL_COLOR_SPACE_FCC MMAL_FOURCC('Y', 'F', 'C', 'C')
  20030. +/** Society of Motion Picture and Television Engineers 240M (1999) */
  20031. +#define MMAL_COLOR_SPACE_SMPTE240M MMAL_FOURCC('Y', '2', '4', '0')
  20032. +/** ITU-R BT.470-2 System M */
  20033. +#define MMAL_COLOR_SPACE_BT470_2_M MMAL_FOURCC('Y', '_', '_', 'M')
  20034. +/** ITU-R BT.470-2 System BG */
  20035. +#define MMAL_COLOR_SPACE_BT470_2_BG MMAL_FOURCC('Y', '_', 'B', 'G')
  20036. +/** JPEG JFIF, but with 16..255 luma */
  20037. +#define MMAL_COLOR_SPACE_JFIF_Y16_255 MMAL_FOURCC('Y', 'Y', '1', '6')
  20038. +/* @} MmalColorSpace List */
  20039. +
  20040. +#endif /* MMAL_ENCODINGS_H */
  20041. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg-common.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h
  20042. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg-common.h 1969-12-31 18:00:00.000000000 -0600
  20043. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h 2014-12-11 14:02:53.384418001 -0600
  20044. @@ -0,0 +1,50 @@
  20045. +/*
  20046. + * Broadcom BM2835 V4L2 driver
  20047. + *
  20048. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20049. + *
  20050. + * This file is subject to the terms and conditions of the GNU General Public
  20051. + * License. See the file COPYING in the main directory of this archive
  20052. + * for more details.
  20053. + *
  20054. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20055. + * Dave Stevenson <dsteve@broadcom.com>
  20056. + * Simon Mellor <simellor@broadcom.com>
  20057. + * Luke Diamand <luked@broadcom.com>
  20058. + */
  20059. +
  20060. +#ifndef MMAL_MSG_COMMON_H
  20061. +#define MMAL_MSG_COMMON_H
  20062. +
  20063. +enum mmal_msg_status {
  20064. + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
  20065. + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
  20066. + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
  20067. + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
  20068. + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
  20069. + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
  20070. + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
  20071. + MMAL_MSG_STATUS_EIO, /**< I/O error */
  20072. + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
  20073. + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
  20074. + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
  20075. + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
  20076. + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
  20077. + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
  20078. + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
  20079. + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
  20080. +};
  20081. +
  20082. +struct mmal_rect {
  20083. + s32 x; /**< x coordinate (from left) */
  20084. + s32 y; /**< y coordinate (from top) */
  20085. + s32 width; /**< width */
  20086. + s32 height; /**< height */
  20087. +};
  20088. +
  20089. +struct mmal_rational {
  20090. + s32 num; /**< Numerator */
  20091. + s32 den; /**< Denominator */
  20092. +};
  20093. +
  20094. +#endif /* MMAL_MSG_COMMON_H */
  20095. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg-format.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h
  20096. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg-format.h 1969-12-31 18:00:00.000000000 -0600
  20097. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h 2014-12-11 14:02:53.384418001 -0600
  20098. @@ -0,0 +1,81 @@
  20099. +/*
  20100. + * Broadcom BM2835 V4L2 driver
  20101. + *
  20102. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20103. + *
  20104. + * This file is subject to the terms and conditions of the GNU General Public
  20105. + * License. See the file COPYING in the main directory of this archive
  20106. + * for more details.
  20107. + *
  20108. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20109. + * Dave Stevenson <dsteve@broadcom.com>
  20110. + * Simon Mellor <simellor@broadcom.com>
  20111. + * Luke Diamand <luked@broadcom.com>
  20112. + */
  20113. +
  20114. +#ifndef MMAL_MSG_FORMAT_H
  20115. +#define MMAL_MSG_FORMAT_H
  20116. +
  20117. +#include "mmal-msg-common.h"
  20118. +
  20119. +/* MMAL_ES_FORMAT_T */
  20120. +
  20121. +
  20122. +struct mmal_audio_format {
  20123. + u32 channels; /**< Number of audio channels */
  20124. + u32 sample_rate; /**< Sample rate */
  20125. +
  20126. + u32 bits_per_sample; /**< Bits per sample */
  20127. + u32 block_align; /**< Size of a block of data */
  20128. +};
  20129. +
  20130. +struct mmal_video_format {
  20131. + u32 width; /**< Width of frame in pixels */
  20132. + u32 height; /**< Height of frame in rows of pixels */
  20133. + struct mmal_rect crop; /**< Visible region of the frame */
  20134. + struct mmal_rational frame_rate; /**< Frame rate */
  20135. + struct mmal_rational par; /**< Pixel aspect ratio */
  20136. +
  20137. + /* FourCC specifying the color space of the video stream. See the
  20138. + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
  20139. + */
  20140. + u32 color_space;
  20141. +};
  20142. +
  20143. +struct mmal_subpicture_format {
  20144. + u32 x_offset;
  20145. + u32 y_offset;
  20146. +};
  20147. +
  20148. +union mmal_es_specific_format {
  20149. + struct mmal_audio_format audio;
  20150. + struct mmal_video_format video;
  20151. + struct mmal_subpicture_format subpicture;
  20152. +};
  20153. +
  20154. +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
  20155. +struct mmal_es_format {
  20156. + u32 type; /* enum mmal_es_type */
  20157. +
  20158. + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
  20159. + u32 encoding_variant; /* FourCC specifying the specific
  20160. + * encoding variant of the elementary
  20161. + * stream.
  20162. + */
  20163. +
  20164. + union mmal_es_specific_format *es; /* TODO: pointers in
  20165. + * message serialisation?!?
  20166. + */
  20167. + /* Type specific
  20168. + * information for the
  20169. + * elementary stream
  20170. + */
  20171. +
  20172. + u32 bitrate; /**< Bitrate in bits per second */
  20173. + u32 flags; /**< Flags describing properties of the elementary stream. */
  20174. +
  20175. + u32 extradata_size; /**< Size of the codec specific data */
  20176. + u8 *extradata; /**< Codec specific data */
  20177. +};
  20178. +
  20179. +#endif /* MMAL_MSG_FORMAT_H */
  20180. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg.h
  20181. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg.h 1969-12-31 18:00:00.000000000 -0600
  20182. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg.h 2014-12-11 14:02:53.384418001 -0600
  20183. @@ -0,0 +1,404 @@
  20184. +/*
  20185. + * Broadcom BM2835 V4L2 driver
  20186. + *
  20187. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20188. + *
  20189. + * This file is subject to the terms and conditions of the GNU General Public
  20190. + * License. See the file COPYING in the main directory of this archive
  20191. + * for more details.
  20192. + *
  20193. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20194. + * Dave Stevenson <dsteve@broadcom.com>
  20195. + * Simon Mellor <simellor@broadcom.com>
  20196. + * Luke Diamand <luked@broadcom.com>
  20197. + */
  20198. +
  20199. +/* all the data structures which serialise the MMAL protocol. note
  20200. + * these are directly mapped onto the recived message data.
  20201. + *
  20202. + * BEWARE: They seem to *assume* pointers are u32 and that there is no
  20203. + * structure padding!
  20204. + *
  20205. + * NOTE: this implementation uses kernel types to ensure sizes. Rather
  20206. + * than assigning values to enums to force their size the
  20207. + * implementation uses fixed size types and not the enums (though the
  20208. + * comments have the actual enum type
  20209. + */
  20210. +
  20211. +#define VC_MMAL_VER 15
  20212. +#define VC_MMAL_MIN_VER 10
  20213. +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
  20214. +
  20215. +/* max total message size is 512 bytes */
  20216. +#define MMAL_MSG_MAX_SIZE 512
  20217. +/* with six 32bit header elements max payload is therefore 488 bytes */
  20218. +#define MMAL_MSG_MAX_PAYLOAD 488
  20219. +
  20220. +#include "mmal-msg-common.h"
  20221. +#include "mmal-msg-format.h"
  20222. +#include "mmal-msg-port.h"
  20223. +
  20224. +enum mmal_msg_type {
  20225. + MMAL_MSG_TYPE_QUIT = 1,
  20226. + MMAL_MSG_TYPE_SERVICE_CLOSED,
  20227. + MMAL_MSG_TYPE_GET_VERSION,
  20228. + MMAL_MSG_TYPE_COMPONENT_CREATE,
  20229. + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
  20230. + MMAL_MSG_TYPE_COMPONENT_ENABLE,
  20231. + MMAL_MSG_TYPE_COMPONENT_DISABLE,
  20232. + MMAL_MSG_TYPE_PORT_INFO_GET,
  20233. + MMAL_MSG_TYPE_PORT_INFO_SET,
  20234. + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
  20235. + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
  20236. + MMAL_MSG_TYPE_BUFFER_TO_HOST,
  20237. + MMAL_MSG_TYPE_GET_STATS,
  20238. + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
  20239. + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
  20240. + MMAL_MSG_TYPE_EVENT_TO_HOST,
  20241. + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
  20242. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
  20243. + MMAL_MSG_TYPE_CONSUME_MEM,
  20244. + MMAL_MSG_TYPE_LMK, /* 20 */
  20245. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
  20246. + MMAL_MSG_TYPE_DRM_GET_LHS32,
  20247. + MMAL_MSG_TYPE_DRM_GET_TIME,
  20248. + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
  20249. + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
  20250. + MMAL_MSG_TYPE_HOST_LOG,
  20251. + MMAL_MSG_TYPE_MSG_LAST
  20252. +};
  20253. +
  20254. +/* port action request messages differ depending on the action type */
  20255. +enum mmal_msg_port_action_type {
  20256. + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
  20257. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
  20258. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
  20259. + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
  20260. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
  20261. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
  20262. + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
  20263. +};
  20264. +
  20265. +struct mmal_msg_header {
  20266. + u32 magic;
  20267. + u32 type; /** enum mmal_msg_type */
  20268. +
  20269. + /* Opaque handle to the control service */
  20270. + struct mmal_control_service *control_service;
  20271. +
  20272. + struct mmal_msg_context *context; /** a u32 per message context */
  20273. + u32 status; /** The status of the vchiq operation */
  20274. + u32 padding;
  20275. +};
  20276. +
  20277. +/* Send from VC to host to report version */
  20278. +struct mmal_msg_version {
  20279. + u32 flags;
  20280. + u32 major;
  20281. + u32 minor;
  20282. + u32 minimum;
  20283. +};
  20284. +
  20285. +/* request to VC to create component */
  20286. +struct mmal_msg_component_create {
  20287. + void *client_component; /* component context */
  20288. + char name[128];
  20289. + u32 pid; /* For debug */
  20290. +};
  20291. +
  20292. +/* reply from VC to component creation request */
  20293. +struct mmal_msg_component_create_reply {
  20294. + u32 status; /** enum mmal_msg_status - how does this differ to
  20295. + * the one in the header?
  20296. + */
  20297. + u32 component_handle; /* VideoCore handle for component */
  20298. + u32 input_num; /* Number of input ports */
  20299. + u32 output_num; /* Number of output ports */
  20300. + u32 clock_num; /* Number of clock ports */
  20301. +};
  20302. +
  20303. +/* request to VC to destroy a component */
  20304. +struct mmal_msg_component_destroy {
  20305. + u32 component_handle;
  20306. +};
  20307. +
  20308. +struct mmal_msg_component_destroy_reply {
  20309. + u32 status; /** The component destruction status */
  20310. +};
  20311. +
  20312. +
  20313. +/* request and reply to VC to enable a component */
  20314. +struct mmal_msg_component_enable {
  20315. + u32 component_handle;
  20316. +};
  20317. +
  20318. +struct mmal_msg_component_enable_reply {
  20319. + u32 status; /** The component enable status */
  20320. +};
  20321. +
  20322. +
  20323. +/* request and reply to VC to disable a component */
  20324. +struct mmal_msg_component_disable {
  20325. + u32 component_handle;
  20326. +};
  20327. +
  20328. +struct mmal_msg_component_disable_reply {
  20329. + u32 status; /** The component disable status */
  20330. +};
  20331. +
  20332. +/* request to VC to get port information */
  20333. +struct mmal_msg_port_info_get {
  20334. + u32 component_handle; /* component handle port is associated with */
  20335. + u32 port_type; /* enum mmal_msg_port_type */
  20336. + u32 index; /* port index to query */
  20337. +};
  20338. +
  20339. +/* reply from VC to get port info request */
  20340. +struct mmal_msg_port_info_get_reply {
  20341. + u32 status; /** enum mmal_msg_status */
  20342. + u32 component_handle; /* component handle port is associated with */
  20343. + u32 port_type; /* enum mmal_msg_port_type */
  20344. + u32 port_index; /* port indexed in query */
  20345. + s32 found; /* unused */
  20346. + u32 port_handle; /**< Handle to use for this port */
  20347. + struct mmal_port port;
  20348. + struct mmal_es_format format; /* elementry stream format */
  20349. + union mmal_es_specific_format es; /* es type specific data */
  20350. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
  20351. +};
  20352. +
  20353. +/* request to VC to set port information */
  20354. +struct mmal_msg_port_info_set {
  20355. + u32 component_handle;
  20356. + u32 port_type; /* enum mmal_msg_port_type */
  20357. + u32 port_index; /* port indexed in query */
  20358. + struct mmal_port port;
  20359. + struct mmal_es_format format;
  20360. + union mmal_es_specific_format es;
  20361. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  20362. +};
  20363. +
  20364. +/* reply from VC to port info set request */
  20365. +struct mmal_msg_port_info_set_reply {
  20366. + u32 status;
  20367. + u32 component_handle; /* component handle port is associated with */
  20368. + u32 port_type; /* enum mmal_msg_port_type */
  20369. + u32 index; /* port indexed in query */
  20370. + s32 found; /* unused */
  20371. + u32 port_handle; /**< Handle to use for this port */
  20372. + struct mmal_port port;
  20373. + struct mmal_es_format format;
  20374. + union mmal_es_specific_format es;
  20375. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  20376. +};
  20377. +
  20378. +
  20379. +/* port action requests that take a mmal_port as a parameter */
  20380. +struct mmal_msg_port_action_port {
  20381. + u32 component_handle;
  20382. + u32 port_handle;
  20383. + u32 action; /* enum mmal_msg_port_action_type */
  20384. + struct mmal_port port;
  20385. +};
  20386. +
  20387. +/* port action requests that take handles as a parameter */
  20388. +struct mmal_msg_port_action_handle {
  20389. + u32 component_handle;
  20390. + u32 port_handle;
  20391. + u32 action; /* enum mmal_msg_port_action_type */
  20392. + u32 connect_component_handle;
  20393. + u32 connect_port_handle;
  20394. +};
  20395. +
  20396. +struct mmal_msg_port_action_reply {
  20397. + u32 status; /** The port action operation status */
  20398. +};
  20399. +
  20400. +
  20401. +
  20402. +
  20403. +/* MMAL buffer transfer */
  20404. +
  20405. +/** Size of space reserved in a buffer message for short messages. */
  20406. +#define MMAL_VC_SHORT_DATA 128
  20407. +
  20408. +/** Signals that the current payload is the end of the stream of data */
  20409. +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
  20410. +/** Signals that the start of the current payload starts a frame */
  20411. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
  20412. +/** Signals that the end of the current payload ends a frame */
  20413. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
  20414. +/** Signals that the current payload contains only complete frames (>1) */
  20415. +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
  20416. + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  20417. +/** Signals that the current payload is a keyframe (i.e. self decodable) */
  20418. +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
  20419. +/** Signals a discontinuity in the stream of data (e.g. after a seek).
  20420. + * Can be used for instance by a decoder to reset its state */
  20421. +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
  20422. +/** Signals a buffer containing some kind of config data for the component
  20423. + * (e.g. codec config data) */
  20424. +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
  20425. +/** Signals an encrypted payload */
  20426. +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
  20427. +/** Signals a buffer containing side information */
  20428. +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
  20429. +/** Signals a buffer which is the snapshot/postview image from a stills
  20430. + * capture
  20431. + */
  20432. +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
  20433. +/** Signals a buffer which contains data known to be corrupted */
  20434. +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
  20435. +/** Signals that a buffer failed to be transmitted */
  20436. +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
  20437. +
  20438. +struct mmal_driver_buffer {
  20439. + u32 magic;
  20440. + u32 component_handle;
  20441. + u32 port_handle;
  20442. + void *client_context;
  20443. +};
  20444. +
  20445. +/* buffer header */
  20446. +struct mmal_buffer_header {
  20447. + struct mmal_buffer_header *next; /* next header */
  20448. + void *priv; /* framework private data */
  20449. + u32 cmd;
  20450. + void *data;
  20451. + u32 alloc_size;
  20452. + u32 length;
  20453. + u32 offset;
  20454. + u32 flags;
  20455. + s64 pts;
  20456. + s64 dts;
  20457. + void *type;
  20458. + void *user_data;
  20459. +};
  20460. +
  20461. +struct mmal_buffer_header_type_specific {
  20462. + union {
  20463. + struct {
  20464. + u32 planes;
  20465. + u32 offset[4];
  20466. + u32 pitch[4];
  20467. + u32 flags;
  20468. + } video;
  20469. + } u;
  20470. +};
  20471. +
  20472. +struct mmal_msg_buffer_from_host {
  20473. + /* The front 32 bytes of the buffer header are copied
  20474. + * back to us in the reply to allow for context. This
  20475. + * area is used to store two mmal_driver_buffer structures to
  20476. + * allow for multiple concurrent service users.
  20477. + */
  20478. + /* control data */
  20479. + struct mmal_driver_buffer drvbuf;
  20480. +
  20481. + /* referenced control data for passthrough buffer management */
  20482. + struct mmal_driver_buffer drvbuf_ref;
  20483. + struct mmal_buffer_header buffer_header; /* buffer header itself */
  20484. + struct mmal_buffer_header_type_specific buffer_header_type_specific;
  20485. + s32 is_zero_copy;
  20486. + s32 has_reference;
  20487. +
  20488. + /** allows short data to be xfered in control message */
  20489. + u32 payload_in_message;
  20490. + u8 short_data[MMAL_VC_SHORT_DATA];
  20491. +};
  20492. +
  20493. +
  20494. +/* port parameter setting */
  20495. +
  20496. +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
  20497. +
  20498. +struct mmal_msg_port_parameter_set {
  20499. + u32 component_handle; /* component */
  20500. + u32 port_handle; /* port */
  20501. + u32 id; /* Parameter ID */
  20502. + u32 size; /* Parameter size */
  20503. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  20504. +};
  20505. +
  20506. +struct mmal_msg_port_parameter_set_reply {
  20507. + u32 status; /** enum mmal_msg_status todo: how does this
  20508. + * differ to the one in the header?
  20509. + */
  20510. +};
  20511. +
  20512. +/* port parameter getting */
  20513. +
  20514. +struct mmal_msg_port_parameter_get {
  20515. + u32 component_handle; /* component */
  20516. + u32 port_handle; /* port */
  20517. + u32 id; /* Parameter ID */
  20518. + u32 size; /* Parameter size */
  20519. +};
  20520. +
  20521. +struct mmal_msg_port_parameter_get_reply {
  20522. + u32 status; /* Status of mmal_port_parameter_get call */
  20523. + u32 id; /* Parameter ID */
  20524. + u32 size; /* Parameter size */
  20525. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  20526. +};
  20527. +
  20528. +/* event messages */
  20529. +#define MMAL_WORKER_EVENT_SPACE 256
  20530. +
  20531. +struct mmal_msg_event_to_host {
  20532. + void *client_component; /* component context */
  20533. +
  20534. + u32 port_type;
  20535. + u32 port_num;
  20536. +
  20537. + u32 cmd;
  20538. + u32 length;
  20539. + u8 data[MMAL_WORKER_EVENT_SPACE];
  20540. + struct mmal_buffer_header *delayed_buffer;
  20541. +};
  20542. +
  20543. +/* all mmal messages are serialised through this structure */
  20544. +struct mmal_msg {
  20545. + /* header */
  20546. + struct mmal_msg_header h;
  20547. + /* payload */
  20548. + union {
  20549. + struct mmal_msg_version version;
  20550. +
  20551. + struct mmal_msg_component_create component_create;
  20552. + struct mmal_msg_component_create_reply component_create_reply;
  20553. +
  20554. + struct mmal_msg_component_destroy component_destroy;
  20555. + struct mmal_msg_component_destroy_reply component_destroy_reply;
  20556. +
  20557. + struct mmal_msg_component_enable component_enable;
  20558. + struct mmal_msg_component_enable_reply component_enable_reply;
  20559. +
  20560. + struct mmal_msg_component_disable component_disable;
  20561. + struct mmal_msg_component_disable_reply component_disable_reply;
  20562. +
  20563. + struct mmal_msg_port_info_get port_info_get;
  20564. + struct mmal_msg_port_info_get_reply port_info_get_reply;
  20565. +
  20566. + struct mmal_msg_port_info_set port_info_set;
  20567. + struct mmal_msg_port_info_set_reply port_info_set_reply;
  20568. +
  20569. + struct mmal_msg_port_action_port port_action_port;
  20570. + struct mmal_msg_port_action_handle port_action_handle;
  20571. + struct mmal_msg_port_action_reply port_action_reply;
  20572. +
  20573. + struct mmal_msg_buffer_from_host buffer_from_host;
  20574. +
  20575. + struct mmal_msg_port_parameter_set port_parameter_set;
  20576. + struct mmal_msg_port_parameter_set_reply
  20577. + port_parameter_set_reply;
  20578. + struct mmal_msg_port_parameter_get
  20579. + port_parameter_get;
  20580. + struct mmal_msg_port_parameter_get_reply
  20581. + port_parameter_get_reply;
  20582. +
  20583. + struct mmal_msg_event_to_host event_to_host;
  20584. +
  20585. + u8 payload[MMAL_MSG_MAX_PAYLOAD];
  20586. + } u;
  20587. +};
  20588. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg-port.h linux-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h
  20589. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-msg-port.h 1969-12-31 18:00:00.000000000 -0600
  20590. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h 2014-12-11 14:02:53.384418001 -0600
  20591. @@ -0,0 +1,107 @@
  20592. +/*
  20593. + * Broadcom BM2835 V4L2 driver
  20594. + *
  20595. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20596. + *
  20597. + * This file is subject to the terms and conditions of the GNU General Public
  20598. + * License. See the file COPYING in the main directory of this archive
  20599. + * for more details.
  20600. + *
  20601. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20602. + * Dave Stevenson <dsteve@broadcom.com>
  20603. + * Simon Mellor <simellor@broadcom.com>
  20604. + * Luke Diamand <luked@broadcom.com>
  20605. + */
  20606. +
  20607. +/* MMAL_PORT_TYPE_T */
  20608. +enum mmal_port_type {
  20609. + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
  20610. + MMAL_PORT_TYPE_CONTROL, /**< Control port */
  20611. + MMAL_PORT_TYPE_INPUT, /**< Input port */
  20612. + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
  20613. + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
  20614. +};
  20615. +
  20616. +/** The port is pass-through and doesn't need buffer headers allocated */
  20617. +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
  20618. +/** The port wants to allocate the buffer payloads.
  20619. + * This signals a preference that payload allocation should be done
  20620. + * on this port for efficiency reasons. */
  20621. +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
  20622. +/** The port supports format change events.
  20623. + * This applies to input ports and is used to let the client know
  20624. + * whether the port supports being reconfigured via a format
  20625. + * change event (i.e. without having to disable the port). */
  20626. +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
  20627. +
  20628. +/* mmal port structure (MMAL_PORT_T)
  20629. + *
  20630. + * most elements are informational only, the pointer values for
  20631. + * interogation messages are generally provided as additional
  20632. + * strucures within the message. When used to set values only teh
  20633. + * buffer_num, buffer_size and userdata parameters are writable.
  20634. + */
  20635. +struct mmal_port {
  20636. + void *priv; /* Private member used by the framework */
  20637. + const char *name; /* Port name. Used for debugging purposes (RO) */
  20638. +
  20639. + u32 type; /* Type of the port (RO) enum mmal_port_type */
  20640. + u16 index; /* Index of the port in its type list (RO) */
  20641. + u16 index_all; /* Index of the port in the list of all ports (RO) */
  20642. +
  20643. + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
  20644. + struct mmal_es_format *format; /* Format of the elementary stream */
  20645. +
  20646. + u32 buffer_num_min; /* Minimum number of buffers the port
  20647. + * requires (RO). This is set by the
  20648. + * component.
  20649. + */
  20650. +
  20651. + u32 buffer_size_min; /* Minimum size of buffers the port
  20652. + * requires (RO). This is set by the
  20653. + * component.
  20654. + */
  20655. +
  20656. + u32 buffer_alignment_min; /* Minimum alignment requirement for
  20657. + * the buffers (RO). A value of
  20658. + * zero means no special alignment
  20659. + * requirements. This is set by the
  20660. + * component.
  20661. + */
  20662. +
  20663. + u32 buffer_num_recommended; /* Number of buffers the port
  20664. + * recommends for optimal
  20665. + * performance (RO). A value of
  20666. + * zero means no special
  20667. + * recommendation. This is set
  20668. + * by the component.
  20669. + */
  20670. +
  20671. + u32 buffer_size_recommended; /* Size of buffers the port
  20672. + * recommends for optimal
  20673. + * performance (RO). A value of
  20674. + * zero means no special
  20675. + * recommendation. This is set
  20676. + * by the component.
  20677. + */
  20678. +
  20679. + u32 buffer_num; /* Actual number of buffers the port will use.
  20680. + * This is set by the client.
  20681. + */
  20682. +
  20683. + u32 buffer_size; /* Actual maximum size of the buffers that
  20684. + * will be sent to the port. This is set by
  20685. + * the client.
  20686. + */
  20687. +
  20688. + void *component; /* Component this port belongs to (Read Only) */
  20689. +
  20690. + void *userdata; /* Field reserved for use by the client */
  20691. +
  20692. + u32 capabilities; /* Flags describing the capabilities of a
  20693. + * port (RO). Bitwise combination of \ref
  20694. + * portcapabilities "Port capabilities"
  20695. + * values.
  20696. + */
  20697. +
  20698. +};
  20699. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-parameters.h linux-rpi/drivers/media/platform/bcm2835/mmal-parameters.h
  20700. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-parameters.h 1969-12-31 18:00:00.000000000 -0600
  20701. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-parameters.h 2014-12-11 14:02:53.384418001 -0600
  20702. @@ -0,0 +1,656 @@
  20703. +/*
  20704. + * Broadcom BM2835 V4L2 driver
  20705. + *
  20706. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20707. + *
  20708. + * This file is subject to the terms and conditions of the GNU General Public
  20709. + * License. See the file COPYING in the main directory of this archive
  20710. + * for more details.
  20711. + *
  20712. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20713. + * Dave Stevenson <dsteve@broadcom.com>
  20714. + * Simon Mellor <simellor@broadcom.com>
  20715. + * Luke Diamand <luked@broadcom.com>
  20716. + */
  20717. +
  20718. +/* common parameters */
  20719. +
  20720. +/** @name Parameter groups
  20721. + * Parameters are divided into groups, and then allocated sequentially within
  20722. + * a group using an enum.
  20723. + * @{
  20724. + */
  20725. +
  20726. +/** Common parameter ID group, used with many types of component. */
  20727. +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
  20728. +/** Camera-specific parameter ID group. */
  20729. +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
  20730. +/** Video-specific parameter ID group. */
  20731. +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
  20732. +/** Audio-specific parameter ID group. */
  20733. +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
  20734. +/** Clock-specific parameter ID group. */
  20735. +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
  20736. +/** Miracast-specific parameter ID group. */
  20737. +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
  20738. +
  20739. +/* Common parameters */
  20740. +enum mmal_parameter_common_type {
  20741. + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
  20742. + = MMAL_PARAMETER_GROUP_COMMON,
  20743. + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
  20744. + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
  20745. +
  20746. + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
  20747. + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
  20748. +
  20749. + /** MMAL_PARAMETER_BOOLEAN_T */
  20750. + MMAL_PARAMETER_ZERO_COPY,
  20751. +
  20752. + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
  20753. + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
  20754. +
  20755. + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
  20756. + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
  20757. + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
  20758. + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
  20759. + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
  20760. + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
  20761. + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
  20762. + MMAL_PARAMETER_SYSTEM_TIME, /**< MMAL_PARAMETER_UINT64_T */
  20763. + MMAL_PARAMETER_NO_IMAGE_PADDING /**< MMAL_PARAMETER_BOOLEAN_T */
  20764. +};
  20765. +
  20766. +/* camera parameters */
  20767. +
  20768. +enum mmal_parameter_camera_type {
  20769. + /* 0 */
  20770. + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
  20771. + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
  20772. + = MMAL_PARAMETER_GROUP_CAMERA,
  20773. + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
  20774. + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
  20775. + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20776. + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
  20777. + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
  20778. + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
  20779. + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
  20780. + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
  20781. + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
  20782. + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
  20783. + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
  20784. + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
  20785. + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  20786. + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
  20787. + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
  20788. +
  20789. + /* 0x10 */
  20790. + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
  20791. + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20792. + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
  20793. + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
  20794. + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
  20795. + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
  20796. + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
  20797. + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
  20798. + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20799. + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
  20800. + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
  20801. + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
  20802. + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
  20803. + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20804. + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
  20805. + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20806. +
  20807. + /* 0x20 */
  20808. + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
  20809. + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20810. + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20811. + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
  20812. + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
  20813. + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
  20814. + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
  20815. + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
  20816. + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
  20817. + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20818. + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
  20819. + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
  20820. + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20821. + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20822. + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20823. + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20824. +
  20825. + /* 0x30 */
  20826. + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
  20827. + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20828. +
  20829. + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
  20830. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  20831. +
  20832. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20833. + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
  20834. +
  20835. + /** @ref MMAL_PARAMETER_UINT32_T */
  20836. + MMAL_PARAMETER_CAMERA_MIN_ISO,
  20837. +
  20838. + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
  20839. + MMAL_PARAMETER_CAMERA_USE_CASE,
  20840. +
  20841. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20842. + MMAL_PARAMETER_CAPTURE_STATS_PASS,
  20843. +
  20844. + /** @ref MMAL_PARAMETER_UINT32_T */
  20845. + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
  20846. +
  20847. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20848. + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
  20849. +
  20850. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20851. + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
  20852. +
  20853. + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
  20854. + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
  20855. +
  20856. + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
  20857. + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
  20858. + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20859. + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
  20860. + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
  20861. + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  20862. +
  20863. + /* 0x40 */
  20864. + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20865. + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20866. + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20867. + MMAL_PARAMETER_SHUTTER_SPEED, /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
  20868. + MMAL_PARAMETER_CUSTOM_AWB_GAINS, /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
  20869. +};
  20870. +
  20871. +struct mmal_parameter_rational {
  20872. + s32 num; /**< Numerator */
  20873. + s32 den; /**< Denominator */
  20874. +};
  20875. +
  20876. +enum mmal_parameter_camera_config_timestamp_mode {
  20877. + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
  20878. + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
  20879. + * for the frame timestamp
  20880. + */
  20881. + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
  20882. + * but subtract the
  20883. + * timestamp of the first
  20884. + * frame sent to give a
  20885. + * zero based timestamp.
  20886. + */
  20887. +};
  20888. +
  20889. +struct mmal_parameter_fps_range {
  20890. + /**< Low end of the permitted framerate range */
  20891. + struct mmal_parameter_rational fps_low;
  20892. + /**< High end of the permitted framerate range */
  20893. + struct mmal_parameter_rational fps_high;
  20894. +};
  20895. +
  20896. +
  20897. +/* camera configuration parameter */
  20898. +struct mmal_parameter_camera_config {
  20899. + /* Parameters for setting up the image pools */
  20900. + u32 max_stills_w; /* Max size of stills capture */
  20901. + u32 max_stills_h;
  20902. + u32 stills_yuv422; /* Allow YUV422 stills capture */
  20903. + u32 one_shot_stills; /* Continuous or one shot stills captures. */
  20904. +
  20905. + u32 max_preview_video_w; /* Max size of the preview or video
  20906. + * capture frames
  20907. + */
  20908. + u32 max_preview_video_h;
  20909. + u32 num_preview_video_frames;
  20910. +
  20911. + /** Sets the height of the circular buffer for stills capture. */
  20912. + u32 stills_capture_circular_buffer_height;
  20913. +
  20914. + /** Allows preview/encode to resume as fast as possible after the stills
  20915. + * input frame has been received, and then processes the still frame in
  20916. + * the background whilst preview/encode has resumed.
  20917. + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
  20918. + */
  20919. + u32 fast_preview_resume;
  20920. +
  20921. + /** Selects algorithm for timestamping frames if
  20922. + * there is no clock component connected.
  20923. + * enum mmal_parameter_camera_config_timestamp_mode
  20924. + */
  20925. + s32 use_stc_timestamp;
  20926. +};
  20927. +
  20928. +
  20929. +enum mmal_parameter_exposuremode {
  20930. + MMAL_PARAM_EXPOSUREMODE_OFF,
  20931. + MMAL_PARAM_EXPOSUREMODE_AUTO,
  20932. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  20933. + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
  20934. + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
  20935. + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
  20936. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  20937. + MMAL_PARAM_EXPOSUREMODE_SNOW,
  20938. + MMAL_PARAM_EXPOSUREMODE_BEACH,
  20939. + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
  20940. + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
  20941. + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
  20942. + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
  20943. +};
  20944. +
  20945. +enum mmal_parameter_exposuremeteringmode {
  20946. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
  20947. + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
  20948. + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
  20949. + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
  20950. +};
  20951. +
  20952. +enum mmal_parameter_awbmode {
  20953. + MMAL_PARAM_AWBMODE_OFF,
  20954. + MMAL_PARAM_AWBMODE_AUTO,
  20955. + MMAL_PARAM_AWBMODE_SUNLIGHT,
  20956. + MMAL_PARAM_AWBMODE_CLOUDY,
  20957. + MMAL_PARAM_AWBMODE_SHADE,
  20958. + MMAL_PARAM_AWBMODE_TUNGSTEN,
  20959. + MMAL_PARAM_AWBMODE_FLUORESCENT,
  20960. + MMAL_PARAM_AWBMODE_INCANDESCENT,
  20961. + MMAL_PARAM_AWBMODE_FLASH,
  20962. + MMAL_PARAM_AWBMODE_HORIZON,
  20963. +};
  20964. +
  20965. +enum mmal_parameter_imagefx {
  20966. + MMAL_PARAM_IMAGEFX_NONE,
  20967. + MMAL_PARAM_IMAGEFX_NEGATIVE,
  20968. + MMAL_PARAM_IMAGEFX_SOLARIZE,
  20969. + MMAL_PARAM_IMAGEFX_POSTERIZE,
  20970. + MMAL_PARAM_IMAGEFX_WHITEBOARD,
  20971. + MMAL_PARAM_IMAGEFX_BLACKBOARD,
  20972. + MMAL_PARAM_IMAGEFX_SKETCH,
  20973. + MMAL_PARAM_IMAGEFX_DENOISE,
  20974. + MMAL_PARAM_IMAGEFX_EMBOSS,
  20975. + MMAL_PARAM_IMAGEFX_OILPAINT,
  20976. + MMAL_PARAM_IMAGEFX_HATCH,
  20977. + MMAL_PARAM_IMAGEFX_GPEN,
  20978. + MMAL_PARAM_IMAGEFX_PASTEL,
  20979. + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  20980. + MMAL_PARAM_IMAGEFX_FILM,
  20981. + MMAL_PARAM_IMAGEFX_BLUR,
  20982. + MMAL_PARAM_IMAGEFX_SATURATION,
  20983. + MMAL_PARAM_IMAGEFX_COLOURSWAP,
  20984. + MMAL_PARAM_IMAGEFX_WASHEDOUT,
  20985. + MMAL_PARAM_IMAGEFX_POSTERISE,
  20986. + MMAL_PARAM_IMAGEFX_COLOURPOINT,
  20987. + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  20988. + MMAL_PARAM_IMAGEFX_CARTOON,
  20989. +};
  20990. +
  20991. +enum MMAL_PARAM_FLICKERAVOID_T {
  20992. + MMAL_PARAM_FLICKERAVOID_OFF,
  20993. + MMAL_PARAM_FLICKERAVOID_AUTO,
  20994. + MMAL_PARAM_FLICKERAVOID_50HZ,
  20995. + MMAL_PARAM_FLICKERAVOID_60HZ,
  20996. + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
  20997. +};
  20998. +
  20999. +struct mmal_parameter_awbgains {
  21000. + struct mmal_parameter_rational r_gain; /**< Red gain */
  21001. + struct mmal_parameter_rational b_gain; /**< Blue gain */
  21002. +};
  21003. +
  21004. +/** Manner of video rate control */
  21005. +enum mmal_parameter_rate_control_mode {
  21006. + MMAL_VIDEO_RATECONTROL_DEFAULT,
  21007. + MMAL_VIDEO_RATECONTROL_VARIABLE,
  21008. + MMAL_VIDEO_RATECONTROL_CONSTANT,
  21009. + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
  21010. + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
  21011. +};
  21012. +
  21013. +enum mmal_video_profile {
  21014. + MMAL_VIDEO_PROFILE_H263_BASELINE,
  21015. + MMAL_VIDEO_PROFILE_H263_H320CODING,
  21016. + MMAL_VIDEO_PROFILE_H263_BACKWARDCOMPATIBLE,
  21017. + MMAL_VIDEO_PROFILE_H263_ISWV2,
  21018. + MMAL_VIDEO_PROFILE_H263_ISWV3,
  21019. + MMAL_VIDEO_PROFILE_H263_HIGHCOMPRESSION,
  21020. + MMAL_VIDEO_PROFILE_H263_INTERNET,
  21021. + MMAL_VIDEO_PROFILE_H263_INTERLACE,
  21022. + MMAL_VIDEO_PROFILE_H263_HIGHLATENCY,
  21023. + MMAL_VIDEO_PROFILE_MP4V_SIMPLE,
  21024. + MMAL_VIDEO_PROFILE_MP4V_SIMPLESCALABLE,
  21025. + MMAL_VIDEO_PROFILE_MP4V_CORE,
  21026. + MMAL_VIDEO_PROFILE_MP4V_MAIN,
  21027. + MMAL_VIDEO_PROFILE_MP4V_NBIT,
  21028. + MMAL_VIDEO_PROFILE_MP4V_SCALABLETEXTURE,
  21029. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFACE,
  21030. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFBA,
  21031. + MMAL_VIDEO_PROFILE_MP4V_BASICANIMATED,
  21032. + MMAL_VIDEO_PROFILE_MP4V_HYBRID,
  21033. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDREALTIME,
  21034. + MMAL_VIDEO_PROFILE_MP4V_CORESCALABLE,
  21035. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCODING,
  21036. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCORE,
  21037. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSCALABLE,
  21038. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSIMPLE,
  21039. + MMAL_VIDEO_PROFILE_H264_BASELINE,
  21040. + MMAL_VIDEO_PROFILE_H264_MAIN,
  21041. + MMAL_VIDEO_PROFILE_H264_EXTENDED,
  21042. + MMAL_VIDEO_PROFILE_H264_HIGH,
  21043. + MMAL_VIDEO_PROFILE_H264_HIGH10,
  21044. + MMAL_VIDEO_PROFILE_H264_HIGH422,
  21045. + MMAL_VIDEO_PROFILE_H264_HIGH444,
  21046. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
  21047. + MMAL_VIDEO_PROFILE_DUMMY = 0x7FFFFFFF
  21048. +};
  21049. +
  21050. +enum mmal_video_level {
  21051. + MMAL_VIDEO_LEVEL_H263_10,
  21052. + MMAL_VIDEO_LEVEL_H263_20,
  21053. + MMAL_VIDEO_LEVEL_H263_30,
  21054. + MMAL_VIDEO_LEVEL_H263_40,
  21055. + MMAL_VIDEO_LEVEL_H263_45,
  21056. + MMAL_VIDEO_LEVEL_H263_50,
  21057. + MMAL_VIDEO_LEVEL_H263_60,
  21058. + MMAL_VIDEO_LEVEL_H263_70,
  21059. + MMAL_VIDEO_LEVEL_MP4V_0,
  21060. + MMAL_VIDEO_LEVEL_MP4V_0b,
  21061. + MMAL_VIDEO_LEVEL_MP4V_1,
  21062. + MMAL_VIDEO_LEVEL_MP4V_2,
  21063. + MMAL_VIDEO_LEVEL_MP4V_3,
  21064. + MMAL_VIDEO_LEVEL_MP4V_4,
  21065. + MMAL_VIDEO_LEVEL_MP4V_4a,
  21066. + MMAL_VIDEO_LEVEL_MP4V_5,
  21067. + MMAL_VIDEO_LEVEL_MP4V_6,
  21068. + MMAL_VIDEO_LEVEL_H264_1,
  21069. + MMAL_VIDEO_LEVEL_H264_1b,
  21070. + MMAL_VIDEO_LEVEL_H264_11,
  21071. + MMAL_VIDEO_LEVEL_H264_12,
  21072. + MMAL_VIDEO_LEVEL_H264_13,
  21073. + MMAL_VIDEO_LEVEL_H264_2,
  21074. + MMAL_VIDEO_LEVEL_H264_21,
  21075. + MMAL_VIDEO_LEVEL_H264_22,
  21076. + MMAL_VIDEO_LEVEL_H264_3,
  21077. + MMAL_VIDEO_LEVEL_H264_31,
  21078. + MMAL_VIDEO_LEVEL_H264_32,
  21079. + MMAL_VIDEO_LEVEL_H264_4,
  21080. + MMAL_VIDEO_LEVEL_H264_41,
  21081. + MMAL_VIDEO_LEVEL_H264_42,
  21082. + MMAL_VIDEO_LEVEL_H264_5,
  21083. + MMAL_VIDEO_LEVEL_H264_51,
  21084. + MMAL_VIDEO_LEVEL_DUMMY = 0x7FFFFFFF
  21085. +};
  21086. +
  21087. +struct mmal_parameter_video_profile {
  21088. + enum mmal_video_profile profile;
  21089. + enum mmal_video_level level;
  21090. +};
  21091. +
  21092. +/* video parameters */
  21093. +
  21094. +enum mmal_parameter_video_type {
  21095. + /** @ref MMAL_DISPLAYREGION_T */
  21096. + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
  21097. +
  21098. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  21099. + MMAL_PARAMETER_SUPPORTED_PROFILES,
  21100. +
  21101. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  21102. + MMAL_PARAMETER_PROFILE,
  21103. +
  21104. + /** @ref MMAL_PARAMETER_UINT32_T */
  21105. + MMAL_PARAMETER_INTRAPERIOD,
  21106. +
  21107. + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
  21108. + MMAL_PARAMETER_RATECONTROL,
  21109. +
  21110. + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
  21111. + MMAL_PARAMETER_NALUNITFORMAT,
  21112. +
  21113. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21114. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  21115. +
  21116. + /** @ref MMAL_PARAMETER_UINT32_T.
  21117. + * Setting the value to zero resets to the default (one slice per frame).
  21118. + */
  21119. + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
  21120. +
  21121. + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
  21122. + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
  21123. +
  21124. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
  21125. + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
  21126. +
  21127. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
  21128. + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
  21129. +
  21130. + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
  21131. + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
  21132. + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
  21133. + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
  21134. +
  21135. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21136. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  21137. +
  21138. + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
  21139. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  21140. +
  21141. + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
  21142. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  21143. +
  21144. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21145. + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
  21146. +
  21147. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21148. + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
  21149. +
  21150. + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
  21151. + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
  21152. +
  21153. + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
  21154. + /** @ref MMAL_PARAMETER_UINT32_T.
  21155. + * Changing this parameter from the default can reduce frame rate
  21156. + * because image buffers need to be re-pitched.
  21157. + */
  21158. + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
  21159. +
  21160. + /** @ref MMAL_PARAMETER_UINT32_T.
  21161. + * Changing this parameter from the default can reduce frame rate
  21162. + * because image buffers need to be re-pitched.
  21163. + */
  21164. + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
  21165. +
  21166. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21167. + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
  21168. +
  21169. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21170. + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
  21171. +
  21172. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  21173. + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
  21174. +
  21175. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  21176. + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
  21177. +
  21178. + /** @ref MMAL_PARAMETER_UINT32_T */
  21179. + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
  21180. +
  21181. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21182. + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
  21183. +
  21184. + /* H264 specific parameters */
  21185. +
  21186. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21187. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
  21188. +
  21189. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21190. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
  21191. +
  21192. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21193. + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
  21194. +
  21195. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21196. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
  21197. +
  21198. + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
  21199. + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
  21200. +
  21201. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21202. + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
  21203. +
  21204. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21205. + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
  21206. +
  21207. + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
  21208. + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
  21209. +
  21210. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21211. + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
  21212. +
  21213. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21214. + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
  21215. +
  21216. + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
  21217. + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
  21218. +
  21219. + /** @ref MMAL_PARAMETER_BYTES_T */
  21220. + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
  21221. +
  21222. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21223. + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
  21224. +
  21225. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21226. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
  21227. +
  21228. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21229. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
  21230. +};
  21231. +
  21232. +/** Valid mirror modes */
  21233. +enum mmal_parameter_mirror {
  21234. + MMAL_PARAM_MIRROR_NONE,
  21235. + MMAL_PARAM_MIRROR_VERTICAL,
  21236. + MMAL_PARAM_MIRROR_HORIZONTAL,
  21237. + MMAL_PARAM_MIRROR_BOTH,
  21238. +};
  21239. +
  21240. +enum mmal_parameter_displaytransform {
  21241. + MMAL_DISPLAY_ROT0 = 0,
  21242. + MMAL_DISPLAY_MIRROR_ROT0 = 1,
  21243. + MMAL_DISPLAY_MIRROR_ROT180 = 2,
  21244. + MMAL_DISPLAY_ROT180 = 3,
  21245. + MMAL_DISPLAY_MIRROR_ROT90 = 4,
  21246. + MMAL_DISPLAY_ROT270 = 5,
  21247. + MMAL_DISPLAY_ROT90 = 6,
  21248. + MMAL_DISPLAY_MIRROR_ROT270 = 7,
  21249. +};
  21250. +
  21251. +enum mmal_parameter_displaymode {
  21252. + MMAL_DISPLAY_MODE_FILL = 0,
  21253. + MMAL_DISPLAY_MODE_LETTERBOX = 1,
  21254. +};
  21255. +
  21256. +enum mmal_parameter_displayset {
  21257. + MMAL_DISPLAY_SET_NONE = 0,
  21258. + MMAL_DISPLAY_SET_NUM = 1,
  21259. + MMAL_DISPLAY_SET_FULLSCREEN = 2,
  21260. + MMAL_DISPLAY_SET_TRANSFORM = 4,
  21261. + MMAL_DISPLAY_SET_DEST_RECT = 8,
  21262. + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
  21263. + MMAL_DISPLAY_SET_MODE = 0x20,
  21264. + MMAL_DISPLAY_SET_PIXEL = 0x40,
  21265. + MMAL_DISPLAY_SET_NOASPECT = 0x80,
  21266. + MMAL_DISPLAY_SET_LAYER = 0x100,
  21267. + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
  21268. + MMAL_DISPLAY_SET_ALPHA = 0x400,
  21269. +};
  21270. +
  21271. +struct mmal_parameter_displayregion {
  21272. + /** Bitfield that indicates which fields are set and should be
  21273. + * used. All other fields will maintain their current value.
  21274. + * \ref MMAL_DISPLAYSET_T defines the bits that can be
  21275. + * combined.
  21276. + */
  21277. + u32 set;
  21278. +
  21279. + /** Describes the display output device, with 0 typically
  21280. + * being a directly connected LCD display. The actual values
  21281. + * will depend on the hardware. Code using hard-wired numbers
  21282. + * (e.g. 2) is certain to fail.
  21283. + */
  21284. +
  21285. + u32 display_num;
  21286. + /** Indicates that we are using the full device screen area,
  21287. + * rather than a window of the display. If zero, then
  21288. + * dest_rect is used to specify a region of the display to
  21289. + * use.
  21290. + */
  21291. +
  21292. + s32 fullscreen;
  21293. + /** Indicates any rotation or flipping used to map frames onto
  21294. + * the natural display orientation.
  21295. + */
  21296. + u32 transform; /* enum mmal_parameter_displaytransform */
  21297. +
  21298. + /** Where to display the frame within the screen, if
  21299. + * fullscreen is zero.
  21300. + */
  21301. + struct vchiq_mmal_rect dest_rect;
  21302. +
  21303. + /** Indicates which area of the frame to display. If all
  21304. + * values are zero, the whole frame will be used.
  21305. + */
  21306. + struct vchiq_mmal_rect src_rect;
  21307. +
  21308. + /** If set to non-zero, indicates that any display scaling
  21309. + * should disregard the aspect ratio of the frame region being
  21310. + * displayed.
  21311. + */
  21312. + s32 noaspect;
  21313. +
  21314. + /** Indicates how the image should be scaled to fit the
  21315. + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
  21316. + * that the image should fill the screen by potentially
  21317. + * cropping the frames. Setting \code mode \endcode to \code
  21318. + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
  21319. + * source region should be displayed and black bars added if
  21320. + * necessary.
  21321. + */
  21322. + u32 mode; /* enum mmal_parameter_displaymode */
  21323. +
  21324. + /** If non-zero, defines the width of a source pixel relative
  21325. + * to \code pixel_y \endcode. If zero, then pixels default to
  21326. + * being square.
  21327. + */
  21328. + u32 pixel_x;
  21329. +
  21330. + /** If non-zero, defines the height of a source pixel relative
  21331. + * to \code pixel_x \endcode. If zero, then pixels default to
  21332. + * being square.
  21333. + */
  21334. + u32 pixel_y;
  21335. +
  21336. + /** Sets the relative depth of the images, with greater values
  21337. + * being in front of smaller values.
  21338. + */
  21339. + u32 layer;
  21340. +
  21341. + /** Set to non-zero to ensure copy protection is used on
  21342. + * output.
  21343. + */
  21344. + s32 copyprotect_required;
  21345. +
  21346. + /** Level of opacity of the layer, where zero is fully
  21347. + * transparent and 255 is fully opaque.
  21348. + */
  21349. + u32 alpha;
  21350. +};
  21351. +
  21352. +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
  21353. +
  21354. +struct mmal_parameter_imagefx_parameters {
  21355. + enum mmal_parameter_imagefx effect;
  21356. + u32 num_effect_params;
  21357. + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
  21358. +};
  21359. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-vchiq.c linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c
  21360. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-vchiq.c 1969-12-31 18:00:00.000000000 -0600
  21361. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c 2014-12-11 14:02:53.384418001 -0600
  21362. @@ -0,0 +1,1916 @@
  21363. +/*
  21364. + * Broadcom BM2835 V4L2 driver
  21365. + *
  21366. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  21367. + *
  21368. + * This file is subject to the terms and conditions of the GNU General Public
  21369. + * License. See the file COPYING in the main directory of this archive
  21370. + * for more details.
  21371. + *
  21372. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  21373. + * Dave Stevenson <dsteve@broadcom.com>
  21374. + * Simon Mellor <simellor@broadcom.com>
  21375. + * Luke Diamand <luked@broadcom.com>
  21376. + *
  21377. + * V4L2 driver MMAL vchiq interface code
  21378. + */
  21379. +
  21380. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21381. +
  21382. +#include <linux/errno.h>
  21383. +#include <linux/kernel.h>
  21384. +#include <linux/mutex.h>
  21385. +#include <linux/mm.h>
  21386. +#include <linux/slab.h>
  21387. +#include <linux/completion.h>
  21388. +#include <linux/vmalloc.h>
  21389. +#include <asm/cacheflush.h>
  21390. +#include <media/videobuf2-vmalloc.h>
  21391. +
  21392. +#include "mmal-common.h"
  21393. +#include "mmal-vchiq.h"
  21394. +#include "mmal-msg.h"
  21395. +
  21396. +#define USE_VCHIQ_ARM
  21397. +#include "interface/vchi/vchi.h"
  21398. +
  21399. +/* maximum number of components supported */
  21400. +#define VCHIQ_MMAL_MAX_COMPONENTS 4
  21401. +
  21402. +/*#define FULL_MSG_DUMP 1*/
  21403. +
  21404. +#ifdef DEBUG
  21405. +static const char *const msg_type_names[] = {
  21406. + "UNKNOWN",
  21407. + "QUIT",
  21408. + "SERVICE_CLOSED",
  21409. + "GET_VERSION",
  21410. + "COMPONENT_CREATE",
  21411. + "COMPONENT_DESTROY",
  21412. + "COMPONENT_ENABLE",
  21413. + "COMPONENT_DISABLE",
  21414. + "PORT_INFO_GET",
  21415. + "PORT_INFO_SET",
  21416. + "PORT_ACTION",
  21417. + "BUFFER_FROM_HOST",
  21418. + "BUFFER_TO_HOST",
  21419. + "GET_STATS",
  21420. + "PORT_PARAMETER_SET",
  21421. + "PORT_PARAMETER_GET",
  21422. + "EVENT_TO_HOST",
  21423. + "GET_CORE_STATS_FOR_PORT",
  21424. + "OPAQUE_ALLOCATOR",
  21425. + "CONSUME_MEM",
  21426. + "LMK",
  21427. + "OPAQUE_ALLOCATOR_DESC",
  21428. + "DRM_GET_LHS32",
  21429. + "DRM_GET_TIME",
  21430. + "BUFFER_FROM_HOST_ZEROLEN",
  21431. + "PORT_FLUSH",
  21432. + "HOST_LOG",
  21433. +};
  21434. +#endif
  21435. +
  21436. +static const char *const port_action_type_names[] = {
  21437. + "UNKNOWN",
  21438. + "ENABLE",
  21439. + "DISABLE",
  21440. + "FLUSH",
  21441. + "CONNECT",
  21442. + "DISCONNECT",
  21443. + "SET_REQUIREMENTS",
  21444. +};
  21445. +
  21446. +#if defined(DEBUG)
  21447. +#if defined(FULL_MSG_DUMP)
  21448. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  21449. + do { \
  21450. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  21451. + msg_type_names[(MSG)->h.type], \
  21452. + (MSG)->h.type, (MSG_LEN)); \
  21453. + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
  21454. + 16, 4, (MSG), \
  21455. + sizeof(struct mmal_msg_header), 1); \
  21456. + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
  21457. + 16, 4, \
  21458. + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
  21459. + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
  21460. + } while (0)
  21461. +#else
  21462. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  21463. + { \
  21464. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  21465. + msg_type_names[(MSG)->h.type], \
  21466. + (MSG)->h.type, (MSG_LEN)); \
  21467. + }
  21468. +#endif
  21469. +#else
  21470. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
  21471. +#endif
  21472. +
  21473. +/* normal message context */
  21474. +struct mmal_msg_context {
  21475. + union {
  21476. + struct {
  21477. + /* work struct for defered callback - must come first */
  21478. + struct work_struct work;
  21479. + /* mmal instance */
  21480. + struct vchiq_mmal_instance *instance;
  21481. + /* mmal port */
  21482. + struct vchiq_mmal_port *port;
  21483. + /* actual buffer used to store bulk reply */
  21484. + struct mmal_buffer *buffer;
  21485. + /* amount of buffer used */
  21486. + unsigned long buffer_used;
  21487. + /* MMAL buffer flags */
  21488. + u32 mmal_flags;
  21489. + /* Presentation and Decode timestamps */
  21490. + s64 pts;
  21491. + s64 dts;
  21492. +
  21493. + int status; /* context status */
  21494. +
  21495. + } bulk; /* bulk data */
  21496. +
  21497. + struct {
  21498. + /* message handle to release */
  21499. + VCHI_HELD_MSG_T msg_handle;
  21500. + /* pointer to received message */
  21501. + struct mmal_msg *msg;
  21502. + /* received message length */
  21503. + u32 msg_len;
  21504. + /* completion upon reply */
  21505. + struct completion cmplt;
  21506. + } sync; /* synchronous response */
  21507. + } u;
  21508. +
  21509. +};
  21510. +
  21511. +struct vchiq_mmal_instance {
  21512. + VCHI_SERVICE_HANDLE_T handle;
  21513. +
  21514. + /* ensure serialised access to service */
  21515. + struct mutex vchiq_mutex;
  21516. +
  21517. + /* ensure serialised access to bulk operations */
  21518. + struct mutex bulk_mutex;
  21519. +
  21520. + /* vmalloc page to receive scratch bulk xfers into */
  21521. + void *bulk_scratch;
  21522. +
  21523. + /* component to use next */
  21524. + int component_idx;
  21525. + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
  21526. +};
  21527. +
  21528. +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
  21529. + *instance)
  21530. +{
  21531. + struct mmal_msg_context *msg_context;
  21532. +
  21533. + /* todo: should this be allocated from a pool to avoid kmalloc */
  21534. + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
  21535. + memset(msg_context, 0, sizeof(*msg_context));
  21536. +
  21537. + return msg_context;
  21538. +}
  21539. +
  21540. +static void release_msg_context(struct mmal_msg_context *msg_context)
  21541. +{
  21542. + kfree(msg_context);
  21543. +}
  21544. +
  21545. +/* deals with receipt of event to host message */
  21546. +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
  21547. + struct mmal_msg *msg, u32 msg_len)
  21548. +{
  21549. + pr_debug("unhandled event\n");
  21550. + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
  21551. + msg->u.event_to_host.client_component,
  21552. + msg->u.event_to_host.port_type,
  21553. + msg->u.event_to_host.port_num,
  21554. + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
  21555. +}
  21556. +
  21557. +/* workqueue scheduled callback
  21558. + *
  21559. + * we do this because it is important we do not call any other vchiq
  21560. + * sync calls from witin the message delivery thread
  21561. + */
  21562. +static void buffer_work_cb(struct work_struct *work)
  21563. +{
  21564. + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
  21565. +
  21566. + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
  21567. + msg_context->u.bulk.port,
  21568. + msg_context->u.bulk.status,
  21569. + msg_context->u.bulk.buffer,
  21570. + msg_context->u.bulk.buffer_used,
  21571. + msg_context->u.bulk.mmal_flags,
  21572. + msg_context->u.bulk.dts,
  21573. + msg_context->u.bulk.pts);
  21574. +
  21575. + /* release message context */
  21576. + release_msg_context(msg_context);
  21577. +}
  21578. +
  21579. +/* enqueue a bulk receive for a given message context */
  21580. +static int bulk_receive(struct vchiq_mmal_instance *instance,
  21581. + struct mmal_msg *msg,
  21582. + struct mmal_msg_context *msg_context)
  21583. +{
  21584. + unsigned long rd_len;
  21585. + unsigned long flags = 0;
  21586. + int ret;
  21587. +
  21588. + /* bulk mutex stops other bulk operations while we have a
  21589. + * receive in progress - released in callback
  21590. + */
  21591. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  21592. + if (ret != 0)
  21593. + return ret;
  21594. +
  21595. + rd_len = msg->u.buffer_from_host.buffer_header.length;
  21596. +
  21597. + /* take buffer from queue */
  21598. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  21599. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  21600. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  21601. + pr_err("buffer list empty trying to submit bulk receive\n");
  21602. +
  21603. + /* todo: this is a serious error, we should never have
  21604. + * commited a buffer_to_host operation to the mmal
  21605. + * port without the buffer to back it up (underflow
  21606. + * handling) and there is no obvious way to deal with
  21607. + * this - how is the mmal servie going to react when
  21608. + * we fail to do the xfer and reschedule a buffer when
  21609. + * it arrives? perhaps a starved flag to indicate a
  21610. + * waiting bulk receive?
  21611. + */
  21612. +
  21613. + mutex_unlock(&instance->bulk_mutex);
  21614. +
  21615. + return -EINVAL;
  21616. + }
  21617. +
  21618. + msg_context->u.bulk.buffer =
  21619. + list_entry(msg_context->u.bulk.port->buffers.next,
  21620. + struct mmal_buffer, list);
  21621. + list_del(&msg_context->u.bulk.buffer->list);
  21622. +
  21623. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  21624. +
  21625. + /* ensure we do not overrun the available buffer */
  21626. + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
  21627. + rd_len = msg_context->u.bulk.buffer->buffer_size;
  21628. + pr_warn("short read as not enough receive buffer space\n");
  21629. + /* todo: is this the correct response, what happens to
  21630. + * the rest of the message data?
  21631. + */
  21632. + }
  21633. +
  21634. + /* store length */
  21635. + msg_context->u.bulk.buffer_used = rd_len;
  21636. + msg_context->u.bulk.mmal_flags =
  21637. + msg->u.buffer_from_host.buffer_header.flags;
  21638. + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
  21639. + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
  21640. +
  21641. + // only need to flush L1 cache here, as VCHIQ takes care of the L2
  21642. + // cache.
  21643. + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
  21644. +
  21645. + /* queue the bulk submission */
  21646. + vchi_service_use(instance->handle);
  21647. + ret = vchi_bulk_queue_receive(instance->handle,
  21648. + msg_context->u.bulk.buffer->buffer,
  21649. + /* Actual receive needs to be a multiple
  21650. + * of 4 bytes
  21651. + */
  21652. + (rd_len + 3) & ~3,
  21653. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  21654. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  21655. + msg_context);
  21656. +
  21657. + vchi_service_release(instance->handle);
  21658. +
  21659. + if (ret != 0) {
  21660. + /* callback will not be clearing the mutex */
  21661. + mutex_unlock(&instance->bulk_mutex);
  21662. + }
  21663. +
  21664. + return ret;
  21665. +}
  21666. +
  21667. +/* enque a dummy bulk receive for a given message context */
  21668. +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
  21669. + struct mmal_msg_context *msg_context)
  21670. +{
  21671. + int ret;
  21672. +
  21673. + /* bulk mutex stops other bulk operations while we have a
  21674. + * receive in progress - released in callback
  21675. + */
  21676. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  21677. + if (ret != 0)
  21678. + return ret;
  21679. +
  21680. + /* zero length indicates this was a dummy transfer */
  21681. + msg_context->u.bulk.buffer_used = 0;
  21682. +
  21683. + /* queue the bulk submission */
  21684. + vchi_service_use(instance->handle);
  21685. +
  21686. + ret = vchi_bulk_queue_receive(instance->handle,
  21687. + instance->bulk_scratch,
  21688. + 8,
  21689. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  21690. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  21691. + msg_context);
  21692. +
  21693. + vchi_service_release(instance->handle);
  21694. +
  21695. + if (ret != 0) {
  21696. + /* callback will not be clearing the mutex */
  21697. + mutex_unlock(&instance->bulk_mutex);
  21698. + }
  21699. +
  21700. + return ret;
  21701. +}
  21702. +
  21703. +/* data in message, memcpy from packet into output buffer */
  21704. +static int inline_receive(struct vchiq_mmal_instance *instance,
  21705. + struct mmal_msg *msg,
  21706. + struct mmal_msg_context *msg_context)
  21707. +{
  21708. + unsigned long flags = 0;
  21709. +
  21710. + /* take buffer from queue */
  21711. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  21712. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  21713. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  21714. + pr_err("buffer list empty trying to receive inline\n");
  21715. +
  21716. + /* todo: this is a serious error, we should never have
  21717. + * commited a buffer_to_host operation to the mmal
  21718. + * port without the buffer to back it up (with
  21719. + * underflow handling) and there is no obvious way to
  21720. + * deal with this. Less bad than the bulk case as we
  21721. + * can just drop this on the floor but...unhelpful
  21722. + */
  21723. + return -EINVAL;
  21724. + }
  21725. +
  21726. + msg_context->u.bulk.buffer =
  21727. + list_entry(msg_context->u.bulk.port->buffers.next,
  21728. + struct mmal_buffer, list);
  21729. + list_del(&msg_context->u.bulk.buffer->list);
  21730. +
  21731. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  21732. +
  21733. + memcpy(msg_context->u.bulk.buffer->buffer,
  21734. + msg->u.buffer_from_host.short_data,
  21735. + msg->u.buffer_from_host.payload_in_message);
  21736. +
  21737. + msg_context->u.bulk.buffer_used =
  21738. + msg->u.buffer_from_host.payload_in_message;
  21739. +
  21740. + return 0;
  21741. +}
  21742. +
  21743. +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
  21744. +static int
  21745. +buffer_from_host(struct vchiq_mmal_instance *instance,
  21746. + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
  21747. +{
  21748. + struct mmal_msg_context *msg_context;
  21749. + struct mmal_msg m;
  21750. + int ret;
  21751. +
  21752. + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
  21753. +
  21754. + /* bulk mutex stops other bulk operations while we
  21755. + * have a receive in progress
  21756. + */
  21757. + if (mutex_lock_interruptible(&instance->bulk_mutex))
  21758. + return -EINTR;
  21759. +
  21760. + /* get context */
  21761. + msg_context = get_msg_context(instance);
  21762. + if (msg_context == NULL)
  21763. + return -ENOMEM;
  21764. +
  21765. + /* store bulk message context for when data arrives */
  21766. + msg_context->u.bulk.instance = instance;
  21767. + msg_context->u.bulk.port = port;
  21768. + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
  21769. + msg_context->u.bulk.buffer_used = 0;
  21770. +
  21771. + /* initialise work structure ready to schedule callback */
  21772. + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
  21773. +
  21774. + /* prep the buffer from host message */
  21775. + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
  21776. +
  21777. + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
  21778. + m.h.magic = MMAL_MAGIC;
  21779. + m.h.context = msg_context;
  21780. + m.h.status = 0;
  21781. +
  21782. + /* drvbuf is our private data passed back */
  21783. + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
  21784. + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
  21785. + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
  21786. + m.u.buffer_from_host.drvbuf.client_context = msg_context;
  21787. +
  21788. + /* buffer header */
  21789. + m.u.buffer_from_host.buffer_header.cmd = 0;
  21790. + m.u.buffer_from_host.buffer_header.data = buf->buffer;
  21791. + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
  21792. + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
  21793. + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
  21794. + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
  21795. + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
  21796. + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
  21797. +
  21798. + /* clear buffer type sepecific data */
  21799. + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
  21800. + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
  21801. +
  21802. + /* no payload in message */
  21803. + m.u.buffer_from_host.payload_in_message = 0;
  21804. +
  21805. + vchi_service_use(instance->handle);
  21806. +
  21807. + ret = vchi_msg_queue(instance->handle, &m,
  21808. + sizeof(struct mmal_msg_header) +
  21809. + sizeof(m.u.buffer_from_host),
  21810. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  21811. +
  21812. + if (ret != 0) {
  21813. + release_msg_context(msg_context);
  21814. + /* todo: is this correct error value? */
  21815. + }
  21816. +
  21817. + vchi_service_release(instance->handle);
  21818. +
  21819. + mutex_unlock(&instance->bulk_mutex);
  21820. +
  21821. + return ret;
  21822. +}
  21823. +
  21824. +/* submit a buffer to the mmal sevice
  21825. + *
  21826. + * the buffer_from_host uses size data from the ports next available
  21827. + * mmal_buffer and deals with there being no buffer available by
  21828. + * incrementing the underflow for later
  21829. + */
  21830. +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
  21831. + struct vchiq_mmal_port *port)
  21832. +{
  21833. + int ret;
  21834. + struct mmal_buffer *buf;
  21835. + unsigned long flags = 0;
  21836. +
  21837. + if (!port->enabled)
  21838. + return -EINVAL;
  21839. +
  21840. + /* peek buffer from queue */
  21841. + spin_lock_irqsave(&port->slock, flags);
  21842. + if (list_empty(&port->buffers)) {
  21843. + port->buffer_underflow++;
  21844. + spin_unlock_irqrestore(&port->slock, flags);
  21845. + return -ENOSPC;
  21846. + }
  21847. +
  21848. + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
  21849. +
  21850. + spin_unlock_irqrestore(&port->slock, flags);
  21851. +
  21852. + /* issue buffer to mmal service */
  21853. + ret = buffer_from_host(instance, port, buf);
  21854. + if (ret) {
  21855. + pr_err("adding buffer header failed\n");
  21856. + /* todo: how should this be dealt with */
  21857. + }
  21858. +
  21859. + return ret;
  21860. +}
  21861. +
  21862. +/* deals with receipt of buffer to host message */
  21863. +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
  21864. + struct mmal_msg *msg, u32 msg_len)
  21865. +{
  21866. + struct mmal_msg_context *msg_context;
  21867. +
  21868. + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
  21869. + instance, msg, msg_len);
  21870. +
  21871. + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
  21872. + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
  21873. + } else {
  21874. + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
  21875. + return;
  21876. + }
  21877. +
  21878. + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
  21879. + /* message reception had an error */
  21880. + pr_warn("error %d in reply\n", msg->h.status);
  21881. +
  21882. + msg_context->u.bulk.status = msg->h.status;
  21883. +
  21884. + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
  21885. + /* empty buffer */
  21886. + if (msg->u.buffer_from_host.buffer_header.flags &
  21887. + MMAL_BUFFER_HEADER_FLAG_EOS) {
  21888. + msg_context->u.bulk.status =
  21889. + dummy_bulk_receive(instance, msg_context);
  21890. + if (msg_context->u.bulk.status == 0)
  21891. + return; /* successful bulk submission, bulk
  21892. + * completion will trigger callback
  21893. + */
  21894. + } else {
  21895. + /* do callback with empty buffer - not EOS though */
  21896. + msg_context->u.bulk.status = 0;
  21897. + msg_context->u.bulk.buffer_used = 0;
  21898. + }
  21899. + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
  21900. + /* data is not in message, queue a bulk receive */
  21901. + msg_context->u.bulk.status =
  21902. + bulk_receive(instance, msg, msg_context);
  21903. + if (msg_context->u.bulk.status == 0)
  21904. + return; /* successful bulk submission, bulk
  21905. + * completion will trigger callback
  21906. + */
  21907. +
  21908. + /* failed to submit buffer, this will end badly */
  21909. + pr_err("error %d on bulk submission\n",
  21910. + msg_context->u.bulk.status);
  21911. +
  21912. + } else if (msg->u.buffer_from_host.payload_in_message <=
  21913. + MMAL_VC_SHORT_DATA) {
  21914. + /* data payload within message */
  21915. + msg_context->u.bulk.status = inline_receive(instance, msg,
  21916. + msg_context);
  21917. + } else {
  21918. + pr_err("message with invalid short payload\n");
  21919. +
  21920. + /* signal error */
  21921. + msg_context->u.bulk.status = -EINVAL;
  21922. + msg_context->u.bulk.buffer_used =
  21923. + msg->u.buffer_from_host.payload_in_message;
  21924. + }
  21925. +
  21926. + /* replace the buffer header */
  21927. + port_buffer_from_host(instance, msg_context->u.bulk.port);
  21928. +
  21929. + /* schedule the port callback */
  21930. + schedule_work(&msg_context->u.bulk.work);
  21931. +}
  21932. +
  21933. +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
  21934. + struct mmal_msg_context *msg_context)
  21935. +{
  21936. + /* bulk receive operation complete */
  21937. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  21938. +
  21939. + /* replace the buffer header */
  21940. + port_buffer_from_host(msg_context->u.bulk.instance,
  21941. + msg_context->u.bulk.port);
  21942. +
  21943. + msg_context->u.bulk.status = 0;
  21944. +
  21945. + /* schedule the port callback */
  21946. + schedule_work(&msg_context->u.bulk.work);
  21947. +}
  21948. +
  21949. +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
  21950. + struct mmal_msg_context *msg_context)
  21951. +{
  21952. + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
  21953. +
  21954. + /* bulk receive operation complete */
  21955. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  21956. +
  21957. + /* replace the buffer header */
  21958. + port_buffer_from_host(msg_context->u.bulk.instance,
  21959. + msg_context->u.bulk.port);
  21960. +
  21961. + msg_context->u.bulk.status = -EINTR;
  21962. +
  21963. + schedule_work(&msg_context->u.bulk.work);
  21964. +}
  21965. +
  21966. +/* incoming event service callback */
  21967. +static void service_callback(void *param,
  21968. + const VCHI_CALLBACK_REASON_T reason,
  21969. + void *bulk_ctx)
  21970. +{
  21971. + struct vchiq_mmal_instance *instance = param;
  21972. + int status;
  21973. + u32 msg_len;
  21974. + struct mmal_msg *msg;
  21975. + VCHI_HELD_MSG_T msg_handle;
  21976. +
  21977. + if (!instance) {
  21978. + pr_err("Message callback passed NULL instance\n");
  21979. + return;
  21980. + }
  21981. +
  21982. + switch (reason) {
  21983. + case VCHI_CALLBACK_MSG_AVAILABLE:
  21984. + status = vchi_msg_hold(instance->handle, (void **)&msg,
  21985. + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
  21986. + if (status) {
  21987. + pr_err("Unable to dequeue a message (%d)\n", status);
  21988. + break;
  21989. + }
  21990. +
  21991. + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
  21992. +
  21993. + /* handling is different for buffer messages */
  21994. + switch (msg->h.type) {
  21995. +
  21996. + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
  21997. + vchi_held_msg_release(&msg_handle);
  21998. + break;
  21999. +
  22000. + case MMAL_MSG_TYPE_EVENT_TO_HOST:
  22001. + event_to_host_cb(instance, msg, msg_len);
  22002. + vchi_held_msg_release(&msg_handle);
  22003. +
  22004. + break;
  22005. +
  22006. + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
  22007. + buffer_to_host_cb(instance, msg, msg_len);
  22008. + vchi_held_msg_release(&msg_handle);
  22009. + break;
  22010. +
  22011. + default:
  22012. + /* messages dependant on header context to complete */
  22013. +
  22014. + /* todo: the msg.context really ought to be sanity
  22015. + * checked before we just use it, afaict it comes back
  22016. + * and is used raw from the videocore. Perhaps it
  22017. + * should be verified the address lies in the kernel
  22018. + * address space.
  22019. + */
  22020. + if (msg->h.context == NULL) {
  22021. + pr_err("received message context was null!\n");
  22022. + vchi_held_msg_release(&msg_handle);
  22023. + break;
  22024. + }
  22025. +
  22026. + /* fill in context values */
  22027. + msg->h.context->u.sync.msg_handle = msg_handle;
  22028. + msg->h.context->u.sync.msg = msg;
  22029. + msg->h.context->u.sync.msg_len = msg_len;
  22030. +
  22031. + /* todo: should this check (completion_done()
  22032. + * == 1) for no one waiting? or do we need a
  22033. + * flag to tell us the completion has been
  22034. + * interrupted so we can free the message and
  22035. + * its context. This probably also solves the
  22036. + * message arriving after interruption todo
  22037. + * below
  22038. + */
  22039. +
  22040. + /* complete message so caller knows it happened */
  22041. + complete(&msg->h.context->u.sync.cmplt);
  22042. + break;
  22043. + }
  22044. +
  22045. + break;
  22046. +
  22047. + case VCHI_CALLBACK_BULK_RECEIVED:
  22048. + bulk_receive_cb(instance, bulk_ctx);
  22049. + break;
  22050. +
  22051. + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
  22052. + bulk_abort_cb(instance, bulk_ctx);
  22053. + break;
  22054. +
  22055. + case VCHI_CALLBACK_SERVICE_CLOSED:
  22056. + /* TODO: consider if this requires action if received when
  22057. + * driver is not explicitly closing the service
  22058. + */
  22059. + break;
  22060. +
  22061. + default:
  22062. + pr_err("Received unhandled message reason %d\n", reason);
  22063. + break;
  22064. + }
  22065. +}
  22066. +
  22067. +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
  22068. + struct mmal_msg *msg,
  22069. + unsigned int payload_len,
  22070. + struct mmal_msg **msg_out,
  22071. + VCHI_HELD_MSG_T *msg_handle_out)
  22072. +{
  22073. + struct mmal_msg_context msg_context;
  22074. + int ret;
  22075. +
  22076. + /* payload size must not cause message to exceed max size */
  22077. + if (payload_len >
  22078. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
  22079. + pr_err("payload length %d exceeds max:%d\n", payload_len,
  22080. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
  22081. + return -EINVAL;
  22082. + }
  22083. +
  22084. + init_completion(&msg_context.u.sync.cmplt);
  22085. +
  22086. + msg->h.magic = MMAL_MAGIC;
  22087. + msg->h.context = &msg_context;
  22088. + msg->h.status = 0;
  22089. +
  22090. + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
  22091. + ">>> sync message");
  22092. +
  22093. + vchi_service_use(instance->handle);
  22094. +
  22095. + ret = vchi_msg_queue(instance->handle,
  22096. + msg,
  22097. + sizeof(struct mmal_msg_header) + payload_len,
  22098. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  22099. +
  22100. + vchi_service_release(instance->handle);
  22101. +
  22102. + if (ret) {
  22103. + pr_err("error %d queuing message\n", ret);
  22104. + return ret;
  22105. + }
  22106. +
  22107. + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, 3*HZ);
  22108. + if (ret <= 0) {
  22109. + pr_err("error %d waiting for sync completion\n", ret);
  22110. + if (ret == 0)
  22111. + ret = -ETIME;
  22112. + /* todo: what happens if the message arrives after aborting */
  22113. + return ret;
  22114. + }
  22115. +
  22116. + *msg_out = msg_context.u.sync.msg;
  22117. + *msg_handle_out = msg_context.u.sync.msg_handle;
  22118. +
  22119. + return 0;
  22120. +}
  22121. +
  22122. +static void dump_port_info(struct vchiq_mmal_port *port)
  22123. +{
  22124. + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
  22125. +
  22126. + pr_debug("buffer minimum num:%d size:%d align:%d\n",
  22127. + port->minimum_buffer.num,
  22128. + port->minimum_buffer.size, port->minimum_buffer.alignment);
  22129. +
  22130. + pr_debug("buffer recommended num:%d size:%d align:%d\n",
  22131. + port->recommended_buffer.num,
  22132. + port->recommended_buffer.size,
  22133. + port->recommended_buffer.alignment);
  22134. +
  22135. + pr_debug("buffer current values num:%d size:%d align:%d\n",
  22136. + port->current_buffer.num,
  22137. + port->current_buffer.size, port->current_buffer.alignment);
  22138. +
  22139. + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
  22140. + port->format.type,
  22141. + port->format.encoding, port->format.encoding_variant);
  22142. +
  22143. + pr_debug(" bitrate:%d flags:0x%x\n",
  22144. + port->format.bitrate, port->format.flags);
  22145. +
  22146. + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
  22147. + pr_debug
  22148. + ("es video format: width:%d height:%d colourspace:0x%x\n",
  22149. + port->es.video.width, port->es.video.height,
  22150. + port->es.video.color_space);
  22151. +
  22152. + pr_debug(" : crop xywh %d,%d,%d,%d\n",
  22153. + port->es.video.crop.x,
  22154. + port->es.video.crop.y,
  22155. + port->es.video.crop.width, port->es.video.crop.height);
  22156. + pr_debug(" : framerate %d/%d aspect %d/%d\n",
  22157. + port->es.video.frame_rate.num,
  22158. + port->es.video.frame_rate.den,
  22159. + port->es.video.par.num, port->es.video.par.den);
  22160. + }
  22161. +}
  22162. +
  22163. +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
  22164. +{
  22165. +
  22166. + /* todo do readonly fields need setting at all? */
  22167. + p->type = port->type;
  22168. + p->index = port->index;
  22169. + p->index_all = 0;
  22170. + p->is_enabled = port->enabled;
  22171. + p->buffer_num_min = port->minimum_buffer.num;
  22172. + p->buffer_size_min = port->minimum_buffer.size;
  22173. + p->buffer_alignment_min = port->minimum_buffer.alignment;
  22174. + p->buffer_num_recommended = port->recommended_buffer.num;
  22175. + p->buffer_size_recommended = port->recommended_buffer.size;
  22176. +
  22177. + /* only three writable fields in a port */
  22178. + p->buffer_num = port->current_buffer.num;
  22179. + p->buffer_size = port->current_buffer.size;
  22180. + p->userdata = port;
  22181. +}
  22182. +
  22183. +static int port_info_set(struct vchiq_mmal_instance *instance,
  22184. + struct vchiq_mmal_port *port)
  22185. +{
  22186. + int ret;
  22187. + struct mmal_msg m;
  22188. + struct mmal_msg *rmsg;
  22189. + VCHI_HELD_MSG_T rmsg_handle;
  22190. +
  22191. + pr_debug("setting port info port %p\n", port);
  22192. + if (!port)
  22193. + return -1;
  22194. + dump_port_info(port);
  22195. +
  22196. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
  22197. +
  22198. + m.u.port_info_set.component_handle = port->component->handle;
  22199. + m.u.port_info_set.port_type = port->type;
  22200. + m.u.port_info_set.port_index = port->index;
  22201. +
  22202. + port_to_mmal_msg(port, &m.u.port_info_set.port);
  22203. +
  22204. + /* elementry stream format setup */
  22205. + m.u.port_info_set.format.type = port->format.type;
  22206. + m.u.port_info_set.format.encoding = port->format.encoding;
  22207. + m.u.port_info_set.format.encoding_variant =
  22208. + port->format.encoding_variant;
  22209. + m.u.port_info_set.format.bitrate = port->format.bitrate;
  22210. + m.u.port_info_set.format.flags = port->format.flags;
  22211. +
  22212. + memcpy(&m.u.port_info_set.es, &port->es,
  22213. + sizeof(union mmal_es_specific_format));
  22214. +
  22215. + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
  22216. + memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
  22217. + port->format.extradata_size);
  22218. +
  22219. + ret = send_synchronous_mmal_msg(instance, &m,
  22220. + sizeof(m.u.port_info_set),
  22221. + &rmsg, &rmsg_handle);
  22222. + if (ret)
  22223. + return ret;
  22224. +
  22225. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
  22226. + /* got an unexpected message type in reply */
  22227. + ret = -EINVAL;
  22228. + goto release_msg;
  22229. + }
  22230. +
  22231. + /* return operation status */
  22232. + ret = -rmsg->u.port_info_get_reply.status;
  22233. +
  22234. + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
  22235. + port->component->handle, port->handle);
  22236. +
  22237. +release_msg:
  22238. + vchi_held_msg_release(&rmsg_handle);
  22239. +
  22240. + return ret;
  22241. +
  22242. +}
  22243. +
  22244. +/* use port info get message to retrive port information */
  22245. +static int port_info_get(struct vchiq_mmal_instance *instance,
  22246. + struct vchiq_mmal_port *port)
  22247. +{
  22248. + int ret;
  22249. + struct mmal_msg m;
  22250. + struct mmal_msg *rmsg;
  22251. + VCHI_HELD_MSG_T rmsg_handle;
  22252. +
  22253. + /* port info time */
  22254. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
  22255. + m.u.port_info_get.component_handle = port->component->handle;
  22256. + m.u.port_info_get.port_type = port->type;
  22257. + m.u.port_info_get.index = port->index;
  22258. +
  22259. + ret = send_synchronous_mmal_msg(instance, &m,
  22260. + sizeof(m.u.port_info_get),
  22261. + &rmsg, &rmsg_handle);
  22262. + if (ret)
  22263. + return ret;
  22264. +
  22265. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
  22266. + /* got an unexpected message type in reply */
  22267. + ret = -EINVAL;
  22268. + goto release_msg;
  22269. + }
  22270. +
  22271. + /* return operation status */
  22272. + ret = -rmsg->u.port_info_get_reply.status;
  22273. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  22274. + goto release_msg;
  22275. +
  22276. + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
  22277. + port->enabled = false;
  22278. + else
  22279. + port->enabled = true;
  22280. +
  22281. + /* copy the values out of the message */
  22282. + port->handle = rmsg->u.port_info_get_reply.port_handle;
  22283. +
  22284. + /* port type and index cached to use on port info set becuase
  22285. + * it does not use a port handle
  22286. + */
  22287. + port->type = rmsg->u.port_info_get_reply.port_type;
  22288. + port->index = rmsg->u.port_info_get_reply.port_index;
  22289. +
  22290. + port->minimum_buffer.num =
  22291. + rmsg->u.port_info_get_reply.port.buffer_num_min;
  22292. + port->minimum_buffer.size =
  22293. + rmsg->u.port_info_get_reply.port.buffer_size_min;
  22294. + port->minimum_buffer.alignment =
  22295. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  22296. +
  22297. + port->recommended_buffer.alignment =
  22298. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  22299. + port->recommended_buffer.num =
  22300. + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
  22301. +
  22302. + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
  22303. + port->current_buffer.size =
  22304. + rmsg->u.port_info_get_reply.port.buffer_size;
  22305. +
  22306. + /* stream format */
  22307. + port->format.type = rmsg->u.port_info_get_reply.format.type;
  22308. + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
  22309. + port->format.encoding_variant =
  22310. + rmsg->u.port_info_get_reply.format.encoding_variant;
  22311. + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
  22312. + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
  22313. +
  22314. + /* elementry stream format */
  22315. + memcpy(&port->es,
  22316. + &rmsg->u.port_info_get_reply.es,
  22317. + sizeof(union mmal_es_specific_format));
  22318. + port->format.es = &port->es;
  22319. +
  22320. + port->format.extradata_size =
  22321. + rmsg->u.port_info_get_reply.format.extradata_size;
  22322. + memcpy(port->format.extradata,
  22323. + rmsg->u.port_info_get_reply.extradata,
  22324. + port->format.extradata_size);
  22325. +
  22326. + pr_debug("received port info\n");
  22327. + dump_port_info(port);
  22328. +
  22329. +release_msg:
  22330. +
  22331. + pr_debug("%s:result:%d component:0x%x port:%d\n",
  22332. + __func__, ret, port->component->handle, port->handle);
  22333. +
  22334. + vchi_held_msg_release(&rmsg_handle);
  22335. +
  22336. + return ret;
  22337. +}
  22338. +
  22339. +/* create comonent on vc */
  22340. +static int create_component(struct vchiq_mmal_instance *instance,
  22341. + struct vchiq_mmal_component *component,
  22342. + const char *name)
  22343. +{
  22344. + int ret;
  22345. + struct mmal_msg m;
  22346. + struct mmal_msg *rmsg;
  22347. + VCHI_HELD_MSG_T rmsg_handle;
  22348. +
  22349. + /* build component create message */
  22350. + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
  22351. + m.u.component_create.client_component = component;
  22352. + strncpy(m.u.component_create.name, name,
  22353. + sizeof(m.u.component_create.name));
  22354. +
  22355. + ret = send_synchronous_mmal_msg(instance, &m,
  22356. + sizeof(m.u.component_create),
  22357. + &rmsg, &rmsg_handle);
  22358. + if (ret)
  22359. + return ret;
  22360. +
  22361. + if (rmsg->h.type != m.h.type) {
  22362. + /* got an unexpected message type in reply */
  22363. + ret = -EINVAL;
  22364. + goto release_msg;
  22365. + }
  22366. +
  22367. + ret = -rmsg->u.component_create_reply.status;
  22368. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  22369. + goto release_msg;
  22370. +
  22371. + /* a valid component response received */
  22372. + component->handle = rmsg->u.component_create_reply.component_handle;
  22373. + component->inputs = rmsg->u.component_create_reply.input_num;
  22374. + component->outputs = rmsg->u.component_create_reply.output_num;
  22375. + component->clocks = rmsg->u.component_create_reply.clock_num;
  22376. +
  22377. + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
  22378. + component->handle,
  22379. + component->inputs, component->outputs, component->clocks);
  22380. +
  22381. +release_msg:
  22382. + vchi_held_msg_release(&rmsg_handle);
  22383. +
  22384. + return ret;
  22385. +}
  22386. +
  22387. +/* destroys a component on vc */
  22388. +static int destroy_component(struct vchiq_mmal_instance *instance,
  22389. + struct vchiq_mmal_component *component)
  22390. +{
  22391. + int ret;
  22392. + struct mmal_msg m;
  22393. + struct mmal_msg *rmsg;
  22394. + VCHI_HELD_MSG_T rmsg_handle;
  22395. +
  22396. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
  22397. + m.u.component_destroy.component_handle = component->handle;
  22398. +
  22399. + ret = send_synchronous_mmal_msg(instance, &m,
  22400. + sizeof(m.u.component_destroy),
  22401. + &rmsg, &rmsg_handle);
  22402. + if (ret)
  22403. + return ret;
  22404. +
  22405. + if (rmsg->h.type != m.h.type) {
  22406. + /* got an unexpected message type in reply */
  22407. + ret = -EINVAL;
  22408. + goto release_msg;
  22409. + }
  22410. +
  22411. + ret = -rmsg->u.component_destroy_reply.status;
  22412. +
  22413. +release_msg:
  22414. +
  22415. + vchi_held_msg_release(&rmsg_handle);
  22416. +
  22417. + return ret;
  22418. +}
  22419. +
  22420. +/* enable a component on vc */
  22421. +static int enable_component(struct vchiq_mmal_instance *instance,
  22422. + struct vchiq_mmal_component *component)
  22423. +{
  22424. + int ret;
  22425. + struct mmal_msg m;
  22426. + struct mmal_msg *rmsg;
  22427. + VCHI_HELD_MSG_T rmsg_handle;
  22428. +
  22429. + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
  22430. + m.u.component_enable.component_handle = component->handle;
  22431. +
  22432. + ret = send_synchronous_mmal_msg(instance, &m,
  22433. + sizeof(m.u.component_enable),
  22434. + &rmsg, &rmsg_handle);
  22435. + if (ret)
  22436. + return ret;
  22437. +
  22438. + if (rmsg->h.type != m.h.type) {
  22439. + /* got an unexpected message type in reply */
  22440. + ret = -EINVAL;
  22441. + goto release_msg;
  22442. + }
  22443. +
  22444. + ret = -rmsg->u.component_enable_reply.status;
  22445. +
  22446. +release_msg:
  22447. + vchi_held_msg_release(&rmsg_handle);
  22448. +
  22449. + return ret;
  22450. +}
  22451. +
  22452. +/* disable a component on vc */
  22453. +static int disable_component(struct vchiq_mmal_instance *instance,
  22454. + struct vchiq_mmal_component *component)
  22455. +{
  22456. + int ret;
  22457. + struct mmal_msg m;
  22458. + struct mmal_msg *rmsg;
  22459. + VCHI_HELD_MSG_T rmsg_handle;
  22460. +
  22461. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
  22462. + m.u.component_disable.component_handle = component->handle;
  22463. +
  22464. + ret = send_synchronous_mmal_msg(instance, &m,
  22465. + sizeof(m.u.component_disable),
  22466. + &rmsg, &rmsg_handle);
  22467. + if (ret)
  22468. + return ret;
  22469. +
  22470. + if (rmsg->h.type != m.h.type) {
  22471. + /* got an unexpected message type in reply */
  22472. + ret = -EINVAL;
  22473. + goto release_msg;
  22474. + }
  22475. +
  22476. + ret = -rmsg->u.component_disable_reply.status;
  22477. +
  22478. +release_msg:
  22479. +
  22480. + vchi_held_msg_release(&rmsg_handle);
  22481. +
  22482. + return ret;
  22483. +}
  22484. +
  22485. +/* get version of mmal implementation */
  22486. +static int get_version(struct vchiq_mmal_instance *instance,
  22487. + u32 *major_out, u32 *minor_out)
  22488. +{
  22489. + int ret;
  22490. + struct mmal_msg m;
  22491. + struct mmal_msg *rmsg;
  22492. + VCHI_HELD_MSG_T rmsg_handle;
  22493. +
  22494. + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
  22495. +
  22496. + ret = send_synchronous_mmal_msg(instance, &m,
  22497. + sizeof(m.u.version),
  22498. + &rmsg, &rmsg_handle);
  22499. + if (ret)
  22500. + return ret;
  22501. +
  22502. + if (rmsg->h.type != m.h.type) {
  22503. + /* got an unexpected message type in reply */
  22504. + ret = -EINVAL;
  22505. + goto release_msg;
  22506. + }
  22507. +
  22508. + *major_out = rmsg->u.version.major;
  22509. + *minor_out = rmsg->u.version.minor;
  22510. +
  22511. +release_msg:
  22512. + vchi_held_msg_release(&rmsg_handle);
  22513. +
  22514. + return ret;
  22515. +}
  22516. +
  22517. +/* do a port action with a port as a parameter */
  22518. +static int port_action_port(struct vchiq_mmal_instance *instance,
  22519. + struct vchiq_mmal_port *port,
  22520. + enum mmal_msg_port_action_type action_type)
  22521. +{
  22522. + int ret;
  22523. + struct mmal_msg m;
  22524. + struct mmal_msg *rmsg;
  22525. + VCHI_HELD_MSG_T rmsg_handle;
  22526. +
  22527. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  22528. + m.u.port_action_port.component_handle = port->component->handle;
  22529. + m.u.port_action_port.port_handle = port->handle;
  22530. + m.u.port_action_port.action = action_type;
  22531. +
  22532. + port_to_mmal_msg(port, &m.u.port_action_port.port);
  22533. +
  22534. + ret = send_synchronous_mmal_msg(instance, &m,
  22535. + sizeof(m.u.port_action_port),
  22536. + &rmsg, &rmsg_handle);
  22537. + if (ret)
  22538. + return ret;
  22539. +
  22540. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  22541. + /* got an unexpected message type in reply */
  22542. + ret = -EINVAL;
  22543. + goto release_msg;
  22544. + }
  22545. +
  22546. + ret = -rmsg->u.port_action_reply.status;
  22547. +
  22548. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
  22549. + __func__,
  22550. + ret, port->component->handle, port->handle,
  22551. + port_action_type_names[action_type], action_type);
  22552. +
  22553. +release_msg:
  22554. + vchi_held_msg_release(&rmsg_handle);
  22555. +
  22556. + return ret;
  22557. +}
  22558. +
  22559. +/* do a port action with handles as parameters */
  22560. +static int port_action_handle(struct vchiq_mmal_instance *instance,
  22561. + struct vchiq_mmal_port *port,
  22562. + enum mmal_msg_port_action_type action_type,
  22563. + u32 connect_component_handle,
  22564. + u32 connect_port_handle)
  22565. +{
  22566. + int ret;
  22567. + struct mmal_msg m;
  22568. + struct mmal_msg *rmsg;
  22569. + VCHI_HELD_MSG_T rmsg_handle;
  22570. +
  22571. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  22572. +
  22573. + m.u.port_action_handle.component_handle = port->component->handle;
  22574. + m.u.port_action_handle.port_handle = port->handle;
  22575. + m.u.port_action_handle.action = action_type;
  22576. +
  22577. + m.u.port_action_handle.connect_component_handle =
  22578. + connect_component_handle;
  22579. + m.u.port_action_handle.connect_port_handle = connect_port_handle;
  22580. +
  22581. + ret = send_synchronous_mmal_msg(instance, &m,
  22582. + sizeof(m.u.port_action_handle),
  22583. + &rmsg, &rmsg_handle);
  22584. + if (ret)
  22585. + return ret;
  22586. +
  22587. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  22588. + /* got an unexpected message type in reply */
  22589. + ret = -EINVAL;
  22590. + goto release_msg;
  22591. + }
  22592. +
  22593. + ret = -rmsg->u.port_action_reply.status;
  22594. +
  22595. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
  22596. + " connect component:0x%x connect port:%d\n",
  22597. + __func__,
  22598. + ret, port->component->handle, port->handle,
  22599. + port_action_type_names[action_type],
  22600. + action_type, connect_component_handle, connect_port_handle);
  22601. +
  22602. +release_msg:
  22603. + vchi_held_msg_release(&rmsg_handle);
  22604. +
  22605. + return ret;
  22606. +}
  22607. +
  22608. +static int port_parameter_set(struct vchiq_mmal_instance *instance,
  22609. + struct vchiq_mmal_port *port,
  22610. + u32 parameter_id, void *value, u32 value_size)
  22611. +{
  22612. + int ret;
  22613. + struct mmal_msg m;
  22614. + struct mmal_msg *rmsg;
  22615. + VCHI_HELD_MSG_T rmsg_handle;
  22616. +
  22617. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
  22618. +
  22619. + m.u.port_parameter_set.component_handle = port->component->handle;
  22620. + m.u.port_parameter_set.port_handle = port->handle;
  22621. + m.u.port_parameter_set.id = parameter_id;
  22622. + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
  22623. + memcpy(&m.u.port_parameter_set.value, value, value_size);
  22624. +
  22625. + ret = send_synchronous_mmal_msg(instance, &m,
  22626. + (4 * sizeof(u32)) + value_size,
  22627. + &rmsg, &rmsg_handle);
  22628. + if (ret)
  22629. + return ret;
  22630. +
  22631. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
  22632. + /* got an unexpected message type in reply */
  22633. + ret = -EINVAL;
  22634. + goto release_msg;
  22635. + }
  22636. +
  22637. + ret = -rmsg->u.port_parameter_set_reply.status;
  22638. +
  22639. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
  22640. + __func__,
  22641. + ret, port->component->handle, port->handle, parameter_id);
  22642. +
  22643. +release_msg:
  22644. + vchi_held_msg_release(&rmsg_handle);
  22645. +
  22646. + return ret;
  22647. +}
  22648. +
  22649. +static int port_parameter_get(struct vchiq_mmal_instance *instance,
  22650. + struct vchiq_mmal_port *port,
  22651. + u32 parameter_id, void *value, u32 *value_size)
  22652. +{
  22653. + int ret;
  22654. + struct mmal_msg m;
  22655. + struct mmal_msg *rmsg;
  22656. + VCHI_HELD_MSG_T rmsg_handle;
  22657. +
  22658. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
  22659. +
  22660. + m.u.port_parameter_get.component_handle = port->component->handle;
  22661. + m.u.port_parameter_get.port_handle = port->handle;
  22662. + m.u.port_parameter_get.id = parameter_id;
  22663. + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
  22664. +
  22665. + ret = send_synchronous_mmal_msg(instance, &m,
  22666. + sizeof(struct
  22667. + mmal_msg_port_parameter_get),
  22668. + &rmsg, &rmsg_handle);
  22669. + if (ret)
  22670. + return ret;
  22671. +
  22672. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
  22673. + /* got an unexpected message type in reply */
  22674. + pr_err("Incorrect reply type %d\n", rmsg->h.type);
  22675. + ret = -EINVAL;
  22676. + goto release_msg;
  22677. + }
  22678. +
  22679. + ret = -rmsg->u.port_parameter_get_reply.status;
  22680. + if (ret) {
  22681. + /* Copy only as much as we have space for
  22682. + * but report true size of parameter
  22683. + */
  22684. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  22685. + *value_size);
  22686. + *value_size = rmsg->u.port_parameter_get_reply.size;
  22687. + } else
  22688. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  22689. + rmsg->u.port_parameter_get_reply.size);
  22690. +
  22691. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
  22692. + ret, port->component->handle, port->handle, parameter_id);
  22693. +
  22694. +release_msg:
  22695. + vchi_held_msg_release(&rmsg_handle);
  22696. +
  22697. + return ret;
  22698. +}
  22699. +
  22700. +/* disables a port and drains buffers from it */
  22701. +static int port_disable(struct vchiq_mmal_instance *instance,
  22702. + struct vchiq_mmal_port *port)
  22703. +{
  22704. + int ret;
  22705. + struct list_head *q, *buf_head;
  22706. + unsigned long flags = 0;
  22707. +
  22708. + if (!port->enabled)
  22709. + return 0;
  22710. +
  22711. + port->enabled = false;
  22712. +
  22713. + ret = port_action_port(instance, port,
  22714. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
  22715. + if (ret == 0) {
  22716. +
  22717. + /* drain all queued buffers on port */
  22718. + spin_lock_irqsave(&port->slock, flags);
  22719. +
  22720. + list_for_each_safe(buf_head, q, &port->buffers) {
  22721. + struct mmal_buffer *mmalbuf;
  22722. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  22723. + list);
  22724. + list_del(buf_head);
  22725. + if (port->buffer_cb)
  22726. + port->buffer_cb(instance,
  22727. + port, 0, mmalbuf, 0, 0,
  22728. + MMAL_TIME_UNKNOWN,
  22729. + MMAL_TIME_UNKNOWN);
  22730. + }
  22731. +
  22732. + spin_unlock_irqrestore(&port->slock, flags);
  22733. +
  22734. + ret = port_info_get(instance, port);
  22735. + }
  22736. +
  22737. + return ret;
  22738. +}
  22739. +
  22740. +/* enable a port */
  22741. +static int port_enable(struct vchiq_mmal_instance *instance,
  22742. + struct vchiq_mmal_port *port)
  22743. +{
  22744. + unsigned int hdr_count;
  22745. + struct list_head *buf_head;
  22746. + int ret;
  22747. +
  22748. + if (port->enabled)
  22749. + return 0;
  22750. +
  22751. + /* ensure there are enough buffers queued to cover the buffer headers */
  22752. + if (port->buffer_cb != NULL) {
  22753. + hdr_count = 0;
  22754. + list_for_each(buf_head, &port->buffers) {
  22755. + hdr_count++;
  22756. + }
  22757. + if (hdr_count < port->current_buffer.num)
  22758. + return -ENOSPC;
  22759. + }
  22760. +
  22761. + ret = port_action_port(instance, port,
  22762. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
  22763. + if (ret)
  22764. + goto done;
  22765. +
  22766. + port->enabled = true;
  22767. +
  22768. + if (port->buffer_cb) {
  22769. + /* send buffer headers to videocore */
  22770. + hdr_count = 1;
  22771. + list_for_each(buf_head, &port->buffers) {
  22772. + struct mmal_buffer *mmalbuf;
  22773. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  22774. + list);
  22775. + ret = buffer_from_host(instance, port, mmalbuf);
  22776. + if (ret)
  22777. + goto done;
  22778. +
  22779. + hdr_count++;
  22780. + if (hdr_count > port->current_buffer.num)
  22781. + break;
  22782. + }
  22783. + }
  22784. +
  22785. + ret = port_info_get(instance, port);
  22786. +
  22787. +done:
  22788. + return ret;
  22789. +}
  22790. +
  22791. +/* ------------------------------------------------------------------
  22792. + * Exported API
  22793. + *------------------------------------------------------------------*/
  22794. +
  22795. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  22796. + struct vchiq_mmal_port *port)
  22797. +{
  22798. + int ret;
  22799. +
  22800. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22801. + return -EINTR;
  22802. +
  22803. + ret = port_info_set(instance, port);
  22804. + if (ret)
  22805. + goto release_unlock;
  22806. +
  22807. + /* read what has actually been set */
  22808. + ret = port_info_get(instance, port);
  22809. +
  22810. +release_unlock:
  22811. + mutex_unlock(&instance->vchiq_mutex);
  22812. +
  22813. + return ret;
  22814. +
  22815. +}
  22816. +
  22817. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  22818. + struct vchiq_mmal_port *port,
  22819. + u32 parameter, void *value, u32 value_size)
  22820. +{
  22821. + int ret;
  22822. +
  22823. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22824. + return -EINTR;
  22825. +
  22826. + ret = port_parameter_set(instance, port, parameter, value, value_size);
  22827. +
  22828. + mutex_unlock(&instance->vchiq_mutex);
  22829. +
  22830. + return ret;
  22831. +}
  22832. +
  22833. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  22834. + struct vchiq_mmal_port *port,
  22835. + u32 parameter, void *value, u32 *value_size)
  22836. +{
  22837. + int ret;
  22838. +
  22839. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22840. + return -EINTR;
  22841. +
  22842. + ret = port_parameter_get(instance, port, parameter, value, value_size);
  22843. +
  22844. + mutex_unlock(&instance->vchiq_mutex);
  22845. +
  22846. + return ret;
  22847. +}
  22848. +
  22849. +/* enable a port
  22850. + *
  22851. + * enables a port and queues buffers for satisfying callbacks if we
  22852. + * provide a callback handler
  22853. + */
  22854. +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
  22855. + struct vchiq_mmal_port *port,
  22856. + vchiq_mmal_buffer_cb buffer_cb)
  22857. +{
  22858. + int ret;
  22859. +
  22860. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22861. + return -EINTR;
  22862. +
  22863. + /* already enabled - noop */
  22864. + if (port->enabled) {
  22865. + ret = 0;
  22866. + goto unlock;
  22867. + }
  22868. +
  22869. + port->buffer_cb = buffer_cb;
  22870. +
  22871. + ret = port_enable(instance, port);
  22872. +
  22873. +unlock:
  22874. + mutex_unlock(&instance->vchiq_mutex);
  22875. +
  22876. + return ret;
  22877. +}
  22878. +
  22879. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  22880. + struct vchiq_mmal_port *port)
  22881. +{
  22882. + int ret;
  22883. +
  22884. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22885. + return -EINTR;
  22886. +
  22887. + if (!port->enabled) {
  22888. + mutex_unlock(&instance->vchiq_mutex);
  22889. + return 0;
  22890. + }
  22891. +
  22892. + ret = port_disable(instance, port);
  22893. +
  22894. + mutex_unlock(&instance->vchiq_mutex);
  22895. +
  22896. + return ret;
  22897. +}
  22898. +
  22899. +/* ports will be connected in a tunneled manner so data buffers
  22900. + * are not handled by client.
  22901. + */
  22902. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  22903. + struct vchiq_mmal_port *src,
  22904. + struct vchiq_mmal_port *dst)
  22905. +{
  22906. + int ret;
  22907. +
  22908. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22909. + return -EINTR;
  22910. +
  22911. + /* disconnect ports if connected */
  22912. + if (src->connected != NULL) {
  22913. + ret = port_disable(instance, src);
  22914. + if (ret) {
  22915. + pr_err("failed disabling src port(%d)\n", ret);
  22916. + goto release_unlock;
  22917. + }
  22918. +
  22919. + /* do not need to disable the destination port as they
  22920. + * are connected and it is done automatically
  22921. + */
  22922. +
  22923. + ret = port_action_handle(instance, src,
  22924. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
  22925. + src->connected->component->handle,
  22926. + src->connected->handle);
  22927. + if (ret < 0) {
  22928. + pr_err("failed disconnecting src port\n");
  22929. + goto release_unlock;
  22930. + }
  22931. + src->connected->enabled = false;
  22932. + src->connected = NULL;
  22933. + }
  22934. +
  22935. + if (dst == NULL) {
  22936. + /* do not make new connection */
  22937. + ret = 0;
  22938. + pr_debug("not making new connection\n");
  22939. + goto release_unlock;
  22940. + }
  22941. +
  22942. + /* copy src port format to dst */
  22943. + dst->format.encoding = src->format.encoding;
  22944. + dst->es.video.width = src->es.video.width;
  22945. + dst->es.video.height = src->es.video.height;
  22946. + dst->es.video.crop.x = src->es.video.crop.x;
  22947. + dst->es.video.crop.y = src->es.video.crop.y;
  22948. + dst->es.video.crop.width = src->es.video.crop.width;
  22949. + dst->es.video.crop.height = src->es.video.crop.height;
  22950. + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
  22951. + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
  22952. +
  22953. + /* set new format */
  22954. + ret = port_info_set(instance, dst);
  22955. + if (ret) {
  22956. + pr_debug("setting port info failed\n");
  22957. + goto release_unlock;
  22958. + }
  22959. +
  22960. + /* read what has actually been set */
  22961. + ret = port_info_get(instance, dst);
  22962. + if (ret) {
  22963. + pr_debug("read back port info failed\n");
  22964. + goto release_unlock;
  22965. + }
  22966. +
  22967. + /* connect two ports together */
  22968. + ret = port_action_handle(instance, src,
  22969. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
  22970. + dst->component->handle, dst->handle);
  22971. + if (ret < 0) {
  22972. + pr_debug("connecting port %d:%d to %d:%d failed\n",
  22973. + src->component->handle, src->handle,
  22974. + dst->component->handle, dst->handle);
  22975. + goto release_unlock;
  22976. + }
  22977. + src->connected = dst;
  22978. +
  22979. +release_unlock:
  22980. +
  22981. + mutex_unlock(&instance->vchiq_mutex);
  22982. +
  22983. + return ret;
  22984. +}
  22985. +
  22986. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  22987. + struct vchiq_mmal_port *port,
  22988. + struct mmal_buffer *buffer)
  22989. +{
  22990. + unsigned long flags = 0;
  22991. +
  22992. + spin_lock_irqsave(&port->slock, flags);
  22993. + list_add_tail(&buffer->list, &port->buffers);
  22994. + spin_unlock_irqrestore(&port->slock, flags);
  22995. +
  22996. + /* the port previously underflowed because it was missing a
  22997. + * mmal_buffer which has just been added, submit that buffer
  22998. + * to the mmal service.
  22999. + */
  23000. + if (port->buffer_underflow) {
  23001. + port_buffer_from_host(instance, port);
  23002. + port->buffer_underflow--;
  23003. + }
  23004. +
  23005. + return 0;
  23006. +}
  23007. +
  23008. +/* Initialise a mmal component and its ports
  23009. + *
  23010. + */
  23011. +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
  23012. + const char *name,
  23013. + struct vchiq_mmal_component **component_out)
  23014. +{
  23015. + int ret;
  23016. + int idx; /* port index */
  23017. + struct vchiq_mmal_component *component;
  23018. +
  23019. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23020. + return -EINTR;
  23021. +
  23022. + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
  23023. + ret = -EINVAL; /* todo is this correct error? */
  23024. + goto unlock;
  23025. + }
  23026. +
  23027. + component = &instance->component[instance->component_idx];
  23028. +
  23029. + ret = create_component(instance, component, name);
  23030. + if (ret < 0)
  23031. + goto unlock;
  23032. +
  23033. + /* ports info needs gathering */
  23034. + component->control.type = MMAL_PORT_TYPE_CONTROL;
  23035. + component->control.index = 0;
  23036. + component->control.component = component;
  23037. + spin_lock_init(&component->control.slock);
  23038. + INIT_LIST_HEAD(&component->control.buffers);
  23039. + ret = port_info_get(instance, &component->control);
  23040. + if (ret < 0)
  23041. + goto release_component;
  23042. +
  23043. + for (idx = 0; idx < component->inputs; idx++) {
  23044. + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
  23045. + component->input[idx].index = idx;
  23046. + component->input[idx].component = component;
  23047. + spin_lock_init(&component->input[idx].slock);
  23048. + INIT_LIST_HEAD(&component->input[idx].buffers);
  23049. + ret = port_info_get(instance, &component->input[idx]);
  23050. + if (ret < 0)
  23051. + goto release_component;
  23052. + }
  23053. +
  23054. + for (idx = 0; idx < component->outputs; idx++) {
  23055. + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
  23056. + component->output[idx].index = idx;
  23057. + component->output[idx].component = component;
  23058. + spin_lock_init(&component->output[idx].slock);
  23059. + INIT_LIST_HEAD(&component->output[idx].buffers);
  23060. + ret = port_info_get(instance, &component->output[idx]);
  23061. + if (ret < 0)
  23062. + goto release_component;
  23063. + }
  23064. +
  23065. + for (idx = 0; idx < component->clocks; idx++) {
  23066. + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
  23067. + component->clock[idx].index = idx;
  23068. + component->clock[idx].component = component;
  23069. + spin_lock_init(&component->clock[idx].slock);
  23070. + INIT_LIST_HEAD(&component->clock[idx].buffers);
  23071. + ret = port_info_get(instance, &component->clock[idx]);
  23072. + if (ret < 0)
  23073. + goto release_component;
  23074. + }
  23075. +
  23076. + instance->component_idx++;
  23077. +
  23078. + *component_out = component;
  23079. +
  23080. + mutex_unlock(&instance->vchiq_mutex);
  23081. +
  23082. + return 0;
  23083. +
  23084. +release_component:
  23085. + destroy_component(instance, component);
  23086. +unlock:
  23087. + mutex_unlock(&instance->vchiq_mutex);
  23088. +
  23089. + return ret;
  23090. +}
  23091. +
  23092. +/*
  23093. + * cause a mmal component to be destroyed
  23094. + */
  23095. +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
  23096. + struct vchiq_mmal_component *component)
  23097. +{
  23098. + int ret;
  23099. +
  23100. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23101. + return -EINTR;
  23102. +
  23103. + if (component->enabled)
  23104. + ret = disable_component(instance, component);
  23105. +
  23106. + ret = destroy_component(instance, component);
  23107. +
  23108. + mutex_unlock(&instance->vchiq_mutex);
  23109. +
  23110. + return ret;
  23111. +}
  23112. +
  23113. +/*
  23114. + * cause a mmal component to be enabled
  23115. + */
  23116. +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
  23117. + struct vchiq_mmal_component *component)
  23118. +{
  23119. + int ret;
  23120. +
  23121. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23122. + return -EINTR;
  23123. +
  23124. + if (component->enabled) {
  23125. + mutex_unlock(&instance->vchiq_mutex);
  23126. + return 0;
  23127. + }
  23128. +
  23129. + ret = enable_component(instance, component);
  23130. + if (ret == 0)
  23131. + component->enabled = true;
  23132. +
  23133. + mutex_unlock(&instance->vchiq_mutex);
  23134. +
  23135. + return ret;
  23136. +}
  23137. +
  23138. +/*
  23139. + * cause a mmal component to be enabled
  23140. + */
  23141. +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
  23142. + struct vchiq_mmal_component *component)
  23143. +{
  23144. + int ret;
  23145. +
  23146. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23147. + return -EINTR;
  23148. +
  23149. + if (!component->enabled) {
  23150. + mutex_unlock(&instance->vchiq_mutex);
  23151. + return 0;
  23152. + }
  23153. +
  23154. + ret = disable_component(instance, component);
  23155. + if (ret == 0)
  23156. + component->enabled = false;
  23157. +
  23158. + mutex_unlock(&instance->vchiq_mutex);
  23159. +
  23160. + return ret;
  23161. +}
  23162. +
  23163. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  23164. + u32 *major_out, u32 *minor_out)
  23165. +{
  23166. + int ret;
  23167. +
  23168. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23169. + return -EINTR;
  23170. +
  23171. + ret = get_version(instance, major_out, minor_out);
  23172. +
  23173. + mutex_unlock(&instance->vchiq_mutex);
  23174. +
  23175. + return ret;
  23176. +}
  23177. +
  23178. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
  23179. +{
  23180. + int status = 0;
  23181. +
  23182. + if (instance == NULL)
  23183. + return -EINVAL;
  23184. +
  23185. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23186. + return -EINTR;
  23187. +
  23188. + vchi_service_use(instance->handle);
  23189. +
  23190. + status = vchi_service_close(instance->handle);
  23191. + if (status != 0)
  23192. + pr_err("mmal-vchiq: VCHIQ close failed");
  23193. +
  23194. + mutex_unlock(&instance->vchiq_mutex);
  23195. +
  23196. + vfree(instance->bulk_scratch);
  23197. +
  23198. + kfree(instance);
  23199. +
  23200. + return status;
  23201. +}
  23202. +
  23203. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
  23204. +{
  23205. + int status;
  23206. + struct vchiq_mmal_instance *instance;
  23207. + static VCHI_CONNECTION_T *vchi_connection;
  23208. + static VCHI_INSTANCE_T vchi_instance;
  23209. + SERVICE_CREATION_T params = {
  23210. + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
  23211. + VC_MMAL_SERVER_NAME,
  23212. + vchi_connection,
  23213. + 0, /* rx fifo size (unused) */
  23214. + 0, /* tx fifo size (unused) */
  23215. + service_callback,
  23216. + NULL, /* service callback parameter */
  23217. + 1, /* unaligned bulk receives */
  23218. + 1, /* unaligned bulk transmits */
  23219. + 0 /* want crc check on bulk transfers */
  23220. + };
  23221. +
  23222. + /* compile time checks to ensure structure size as they are
  23223. + * directly (de)serialised from memory.
  23224. + */
  23225. +
  23226. + /* ensure the header structure has packed to the correct size */
  23227. + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
  23228. +
  23229. + /* ensure message structure does not exceed maximum length */
  23230. + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
  23231. +
  23232. + /* mmal port struct is correct size */
  23233. + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
  23234. +
  23235. + /* create a vchi instance */
  23236. + status = vchi_initialise(&vchi_instance);
  23237. + if (status) {
  23238. + pr_err("Failed to initialise VCHI instance (status=%d)\n",
  23239. + status);
  23240. + return -EIO;
  23241. + }
  23242. +
  23243. + status = vchi_connect(NULL, 0, vchi_instance);
  23244. + if (status) {
  23245. + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
  23246. + return -EIO;
  23247. + }
  23248. +
  23249. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  23250. + memset(instance, 0, sizeof(*instance));
  23251. +
  23252. + mutex_init(&instance->vchiq_mutex);
  23253. + mutex_init(&instance->bulk_mutex);
  23254. +
  23255. + instance->bulk_scratch = vmalloc(PAGE_SIZE);
  23256. +
  23257. + params.callback_param = instance;
  23258. +
  23259. + status = vchi_service_open(vchi_instance, &params, &instance->handle);
  23260. + if (status) {
  23261. + pr_err("Failed to open VCHI service connection (status=%d)\n",
  23262. + status);
  23263. + goto err_close_services;
  23264. + }
  23265. +
  23266. + vchi_service_release(instance->handle);
  23267. +
  23268. + *out_instance = instance;
  23269. +
  23270. + return 0;
  23271. +
  23272. +err_close_services:
  23273. +
  23274. + vchi_service_close(instance->handle);
  23275. + vfree(instance->bulk_scratch);
  23276. + kfree(instance);
  23277. + return -ENODEV;
  23278. +}
  23279. diff -Nur linux-3.17.5/drivers/media/platform/bcm2835/mmal-vchiq.h linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h
  23280. --- linux-3.17.5/drivers/media/platform/bcm2835/mmal-vchiq.h 1969-12-31 18:00:00.000000000 -0600
  23281. +++ linux-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h 2014-12-11 14:02:53.384418001 -0600
  23282. @@ -0,0 +1,178 @@
  23283. +/*
  23284. + * Broadcom BM2835 V4L2 driver
  23285. + *
  23286. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  23287. + *
  23288. + * This file is subject to the terms and conditions of the GNU General Public
  23289. + * License. See the file COPYING in the main directory of this archive
  23290. + * for more details.
  23291. + *
  23292. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  23293. + * Dave Stevenson <dsteve@broadcom.com>
  23294. + * Simon Mellor <simellor@broadcom.com>
  23295. + * Luke Diamand <luked@broadcom.com>
  23296. + *
  23297. + * MMAL interface to VCHIQ message passing
  23298. + */
  23299. +
  23300. +#ifndef MMAL_VCHIQ_H
  23301. +#define MMAL_VCHIQ_H
  23302. +
  23303. +#include "mmal-msg-format.h"
  23304. +
  23305. +#define MAX_PORT_COUNT 4
  23306. +
  23307. +/* Maximum size of the format extradata. */
  23308. +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
  23309. +
  23310. +struct vchiq_mmal_instance;
  23311. +
  23312. +enum vchiq_mmal_es_type {
  23313. + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
  23314. + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
  23315. + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
  23316. + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
  23317. + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
  23318. +};
  23319. +
  23320. +/* rectangle, used lots so it gets its own struct */
  23321. +struct vchiq_mmal_rect {
  23322. + s32 x;
  23323. + s32 y;
  23324. + s32 width;
  23325. + s32 height;
  23326. +};
  23327. +
  23328. +struct vchiq_mmal_port_buffer {
  23329. + unsigned int num; /* number of buffers */
  23330. + u32 size; /* size of buffers */
  23331. + u32 alignment; /* alignment of buffers */
  23332. +};
  23333. +
  23334. +struct vchiq_mmal_port;
  23335. +
  23336. +typedef void (*vchiq_mmal_buffer_cb)(
  23337. + struct vchiq_mmal_instance *instance,
  23338. + struct vchiq_mmal_port *port,
  23339. + int status, struct mmal_buffer *buffer,
  23340. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
  23341. +
  23342. +struct vchiq_mmal_port {
  23343. + bool enabled;
  23344. + u32 handle;
  23345. + u32 type; /* port type, cached to use on port info set */
  23346. + u32 index; /* port index, cached to use on port info set */
  23347. +
  23348. + /* component port belongs to, allows simple deref */
  23349. + struct vchiq_mmal_component *component;
  23350. +
  23351. + struct vchiq_mmal_port *connected; /* port conencted to */
  23352. +
  23353. + /* buffer info */
  23354. + struct vchiq_mmal_port_buffer minimum_buffer;
  23355. + struct vchiq_mmal_port_buffer recommended_buffer;
  23356. + struct vchiq_mmal_port_buffer current_buffer;
  23357. +
  23358. + /* stream format */
  23359. + struct mmal_es_format format;
  23360. + /* elementry stream format */
  23361. + union mmal_es_specific_format es;
  23362. +
  23363. + /* data buffers to fill */
  23364. + struct list_head buffers;
  23365. + /* lock to serialise adding and removing buffers from list */
  23366. + spinlock_t slock;
  23367. + /* count of how many buffer header refils have failed because
  23368. + * there was no buffer to satisfy them
  23369. + */
  23370. + int buffer_underflow;
  23371. + /* callback on buffer completion */
  23372. + vchiq_mmal_buffer_cb buffer_cb;
  23373. + /* callback context */
  23374. + void *cb_ctx;
  23375. +};
  23376. +
  23377. +struct vchiq_mmal_component {
  23378. + bool enabled;
  23379. + u32 handle; /* VideoCore handle for component */
  23380. + u32 inputs; /* Number of input ports */
  23381. + u32 outputs; /* Number of output ports */
  23382. + u32 clocks; /* Number of clock ports */
  23383. + struct vchiq_mmal_port control; /* control port */
  23384. + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
  23385. + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
  23386. + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
  23387. +};
  23388. +
  23389. +
  23390. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
  23391. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
  23392. +
  23393. +/* Initialise a mmal component and its ports
  23394. +*
  23395. +*/
  23396. +int vchiq_mmal_component_init(
  23397. + struct vchiq_mmal_instance *instance,
  23398. + const char *name,
  23399. + struct vchiq_mmal_component **component_out);
  23400. +
  23401. +int vchiq_mmal_component_finalise(
  23402. + struct vchiq_mmal_instance *instance,
  23403. + struct vchiq_mmal_component *component);
  23404. +
  23405. +int vchiq_mmal_component_enable(
  23406. + struct vchiq_mmal_instance *instance,
  23407. + struct vchiq_mmal_component *component);
  23408. +
  23409. +int vchiq_mmal_component_disable(
  23410. + struct vchiq_mmal_instance *instance,
  23411. + struct vchiq_mmal_component *component);
  23412. +
  23413. +
  23414. +
  23415. +/* enable a mmal port
  23416. + *
  23417. + * enables a port and if a buffer callback provided enque buffer
  23418. + * headers as apropriate for the port.
  23419. + */
  23420. +int vchiq_mmal_port_enable(
  23421. + struct vchiq_mmal_instance *instance,
  23422. + struct vchiq_mmal_port *port,
  23423. + vchiq_mmal_buffer_cb buffer_cb);
  23424. +
  23425. +/* disable a port
  23426. + *
  23427. + * disable a port will dequeue any pending buffers
  23428. + */
  23429. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  23430. + struct vchiq_mmal_port *port);
  23431. +
  23432. +
  23433. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  23434. + struct vchiq_mmal_port *port,
  23435. + u32 parameter,
  23436. + void *value,
  23437. + u32 value_size);
  23438. +
  23439. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  23440. + struct vchiq_mmal_port *port,
  23441. + u32 parameter,
  23442. + void *value,
  23443. + u32 *value_size);
  23444. +
  23445. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  23446. + struct vchiq_mmal_port *port);
  23447. +
  23448. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  23449. + struct vchiq_mmal_port *src,
  23450. + struct vchiq_mmal_port *dst);
  23451. +
  23452. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  23453. + u32 *major_out,
  23454. + u32 *minor_out);
  23455. +
  23456. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  23457. + struct vchiq_mmal_port *port,
  23458. + struct mmal_buffer *buf);
  23459. +
  23460. +#endif /* MMAL_VCHIQ_H */
  23461. diff -Nur linux-3.17.5/drivers/media/platform/Kconfig linux-rpi/drivers/media/platform/Kconfig
  23462. --- linux-3.17.5/drivers/media/platform/Kconfig 2014-12-06 17:57:59.000000000 -0600
  23463. +++ linux-rpi/drivers/media/platform/Kconfig 2014-12-11 14:05:38.032418001 -0600
  23464. @@ -121,6 +121,7 @@
  23465. source "drivers/media/platform/soc_camera/Kconfig"
  23466. source "drivers/media/platform/exynos4-is/Kconfig"
  23467. source "drivers/media/platform/s5p-tv/Kconfig"
  23468. +source "drivers/media/platform/bcm2835/Kconfig"
  23469. endif # V4L_PLATFORM_DRIVERS
  23470. diff -Nur linux-3.17.5/drivers/media/platform/Makefile linux-rpi/drivers/media/platform/Makefile
  23471. --- linux-3.17.5/drivers/media/platform/Makefile 2014-12-06 17:57:59.000000000 -0600
  23472. +++ linux-rpi/drivers/media/platform/Makefile 2014-12-11 14:05:38.032418001 -0600
  23473. @@ -51,4 +51,6 @@
  23474. obj-$(CONFIG_ARCH_OMAP) += omap/
  23475. +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
  23476. +
  23477. ccflags-y += -I$(srctree)/drivers/media/i2c
  23478. diff -Nur linux-3.17.5/drivers/media/usb/dvb-usb-v2/rtl28xxu.c linux-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
  23479. --- linux-3.17.5/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-12-06 17:57:59.000000000 -0600
  23480. +++ linux-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-12-11 14:05:38.092418001 -0600
  23481. @@ -1531,6 +1531,10 @@
  23482. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  23483. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  23484. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  23485. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xb803 /*USB_PID_AUGUST_DVBT205*/,
  23486. + &rtl2832u_props, "August DVB-T 205", NULL) },
  23487. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xa803 /*USB_PID_AUGUST_DVBT205*/,
  23488. + &rtl2832u_props, "August DVB-T 205", NULL) },
  23489. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  23490. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  23491. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  23492. diff -Nur linux-3.17.5/drivers/misc/Kconfig linux-rpi/drivers/misc/Kconfig
  23493. --- linux-3.17.5/drivers/misc/Kconfig 2014-12-06 17:57:59.000000000 -0600
  23494. +++ linux-rpi/drivers/misc/Kconfig 2014-12-11 14:05:38.152418001 -0600
  23495. @@ -524,6 +524,7 @@
  23496. source "drivers/misc/altera-stapl/Kconfig"
  23497. source "drivers/misc/mei/Kconfig"
  23498. source "drivers/misc/vmw_vmci/Kconfig"
  23499. +source "drivers/misc/vc04_services/Kconfig"
  23500. source "drivers/misc/mic/Kconfig"
  23501. source "drivers/misc/genwqe/Kconfig"
  23502. source "drivers/misc/echo/Kconfig"
  23503. diff -Nur linux-3.17.5/drivers/misc/Makefile linux-rpi/drivers/misc/Makefile
  23504. --- linux-3.17.5/drivers/misc/Makefile 2014-12-06 17:57:59.000000000 -0600
  23505. +++ linux-rpi/drivers/misc/Makefile 2014-12-11 14:05:38.152418001 -0600
  23506. @@ -51,6 +51,7 @@
  23507. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  23508. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  23509. obj-$(CONFIG_SRAM) += sram.o
  23510. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  23511. obj-y += mic/
  23512. obj-$(CONFIG_GENWQE) += genwqe/
  23513. obj-$(CONFIG_ECHO) += echo/
  23514. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/connections/connection.h linux-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  23515. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/connections/connection.h 1969-12-31 18:00:00.000000000 -0600
  23516. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h 2014-12-11 14:02:53.544418001 -0600
  23517. @@ -0,0 +1,328 @@
  23518. +/**
  23519. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23520. + *
  23521. + * Redistribution and use in source and binary forms, with or without
  23522. + * modification, are permitted provided that the following conditions
  23523. + * are met:
  23524. + * 1. Redistributions of source code must retain the above copyright
  23525. + * notice, this list of conditions, and the following disclaimer,
  23526. + * without modification.
  23527. + * 2. Redistributions in binary form must reproduce the above copyright
  23528. + * notice, this list of conditions and the following disclaimer in the
  23529. + * documentation and/or other materials provided with the distribution.
  23530. + * 3. The names of the above-listed copyright holders may not be used
  23531. + * to endorse or promote products derived from this software without
  23532. + * specific prior written permission.
  23533. + *
  23534. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23535. + * GNU General Public License ("GPL") version 2, as published by the Free
  23536. + * Software Foundation.
  23537. + *
  23538. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23539. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23540. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23541. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23542. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23543. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23544. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23545. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23546. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23547. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23548. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23549. + */
  23550. +
  23551. +#ifndef CONNECTION_H_
  23552. +#define CONNECTION_H_
  23553. +
  23554. +#include <linux/kernel.h>
  23555. +#include <linux/types.h>
  23556. +#include <linux/semaphore.h>
  23557. +
  23558. +#include "interface/vchi/vchi_cfg_internal.h"
  23559. +#include "interface/vchi/vchi_common.h"
  23560. +#include "interface/vchi/message_drivers/message.h"
  23561. +
  23562. +/******************************************************************************
  23563. + Global defs
  23564. + *****************************************************************************/
  23565. +
  23566. +// Opaque handle for a connection / service pair
  23567. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  23568. +
  23569. +// opaque handle to the connection state information
  23570. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  23571. +
  23572. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  23573. +
  23574. +
  23575. +/******************************************************************************
  23576. + API
  23577. + *****************************************************************************/
  23578. +
  23579. +// Routine to init a connection with a particular low level driver
  23580. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  23581. + const VCHI_MESSAGE_DRIVER_T * driver );
  23582. +
  23583. +// Routine to control CRC enabling at a connection level
  23584. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  23585. + VCHI_CRC_CONTROL_T control );
  23586. +
  23587. +// Routine to create a service
  23588. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  23589. + int32_t service_id,
  23590. + uint32_t rx_fifo_size,
  23591. + uint32_t tx_fifo_size,
  23592. + int server,
  23593. + VCHI_CALLBACK_T callback,
  23594. + void *callback_param,
  23595. + int32_t want_crc,
  23596. + int32_t want_unaligned_bulk_rx,
  23597. + int32_t want_unaligned_bulk_tx,
  23598. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  23599. +
  23600. +// Routine to close a service
  23601. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  23602. +
  23603. +// Routine to queue a message
  23604. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23605. + const void *data,
  23606. + uint32_t data_size,
  23607. + VCHI_FLAGS_T flags,
  23608. + void *msg_handle );
  23609. +
  23610. +// scatter-gather (vector) message queueing
  23611. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23612. + VCHI_MSG_VECTOR_T *vector,
  23613. + uint32_t count,
  23614. + VCHI_FLAGS_T flags,
  23615. + void *msg_handle );
  23616. +
  23617. +// Routine to dequeue a message
  23618. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23619. + void *data,
  23620. + uint32_t max_data_size_to_read,
  23621. + uint32_t *actual_msg_size,
  23622. + VCHI_FLAGS_T flags );
  23623. +
  23624. +// Routine to peek at a message
  23625. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23626. + void **data,
  23627. + uint32_t *msg_size,
  23628. + VCHI_FLAGS_T flags );
  23629. +
  23630. +// Routine to hold a message
  23631. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23632. + void **data,
  23633. + uint32_t *msg_size,
  23634. + VCHI_FLAGS_T flags,
  23635. + void **message_handle );
  23636. +
  23637. +// Routine to initialise a received message iterator
  23638. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23639. + VCHI_MSG_ITER_T *iter,
  23640. + VCHI_FLAGS_T flags );
  23641. +
  23642. +// Routine to release a held message
  23643. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23644. + void *message_handle );
  23645. +
  23646. +// Routine to get info on a held message
  23647. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23648. + void *message_handle,
  23649. + void **data,
  23650. + int32_t *msg_size,
  23651. + uint32_t *tx_timestamp,
  23652. + uint32_t *rx_timestamp );
  23653. +
  23654. +// Routine to check whether the iterator has a next message
  23655. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  23656. + const VCHI_MSG_ITER_T *iter );
  23657. +
  23658. +// Routine to advance the iterator
  23659. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  23660. + VCHI_MSG_ITER_T *iter,
  23661. + void **data,
  23662. + uint32_t *msg_size );
  23663. +
  23664. +// Routine to remove the last message returned by the iterator
  23665. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  23666. + VCHI_MSG_ITER_T *iter );
  23667. +
  23668. +// Routine to hold the last message returned by the iterator
  23669. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  23670. + VCHI_MSG_ITER_T *iter,
  23671. + void **msg_handle );
  23672. +
  23673. +// Routine to transmit bulk data
  23674. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23675. + const void *data_src,
  23676. + uint32_t data_size,
  23677. + VCHI_FLAGS_T flags,
  23678. + void *bulk_handle );
  23679. +
  23680. +// Routine to receive data
  23681. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  23682. + void *data_dst,
  23683. + uint32_t data_size,
  23684. + VCHI_FLAGS_T flags,
  23685. + void *bulk_handle );
  23686. +
  23687. +// Routine to report if a server is available
  23688. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  23689. +
  23690. +// Routine to report the number of RX slots available
  23691. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  23692. +
  23693. +// Routine to report the RX slot size
  23694. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  23695. +
  23696. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  23697. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  23698. + int32_t service,
  23699. + uint32_t length,
  23700. + MESSAGE_TX_CHANNEL_T channel,
  23701. + uint32_t channel_params,
  23702. + uint32_t data_length,
  23703. + uint32_t data_offset);
  23704. +
  23705. +// Callback to inform a service that a Xon or Xoff message has been received
  23706. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  23707. +
  23708. +// Callback to inform a service that a server available reply message has been received
  23709. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  23710. +
  23711. +// Callback to indicate that bulk auxiliary messages have arrived
  23712. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  23713. +
  23714. +// Callback to indicate that bulk auxiliary messages have arrived
  23715. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  23716. +
  23717. +// Callback with all the connection info you require
  23718. +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);
  23719. +
  23720. +// Callback to inform of a disconnect
  23721. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  23722. +
  23723. +// Callback to inform of a power control request
  23724. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  23725. +
  23726. +// allocate memory suitably aligned for this connection
  23727. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  23728. +
  23729. +// free memory allocated by buffer_allocate
  23730. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  23731. +
  23732. +
  23733. +/******************************************************************************
  23734. + System driver struct
  23735. + *****************************************************************************/
  23736. +
  23737. +struct opaque_vchi_connection_api_t
  23738. +{
  23739. + // Routine to init the connection
  23740. + VCHI_CONNECTION_INIT_T init;
  23741. +
  23742. + // Connection-level CRC control
  23743. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  23744. +
  23745. + // Routine to connect to or create service
  23746. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  23747. +
  23748. + // Routine to disconnect from a service
  23749. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  23750. +
  23751. + // Routine to queue a message
  23752. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  23753. +
  23754. + // scatter-gather (vector) message queue
  23755. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  23756. +
  23757. + // Routine to dequeue a message
  23758. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  23759. +
  23760. + // Routine to peek at a message
  23761. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  23762. +
  23763. + // Routine to hold a message
  23764. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  23765. +
  23766. + // Routine to initialise a received message iterator
  23767. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  23768. +
  23769. + // Routine to release a message
  23770. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  23771. +
  23772. + // Routine to get information on a held message
  23773. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  23774. +
  23775. + // Routine to check for next message on iterator
  23776. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  23777. +
  23778. + // Routine to get next message on iterator
  23779. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  23780. +
  23781. + // Routine to remove the last message returned by iterator
  23782. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  23783. +
  23784. + // Routine to hold the last message returned by iterator
  23785. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  23786. +
  23787. + // Routine to transmit bulk data
  23788. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  23789. +
  23790. + // Routine to receive data
  23791. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  23792. +
  23793. + // Routine to report the available servers
  23794. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  23795. +
  23796. + // Routine to report the number of RX slots available
  23797. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  23798. +
  23799. + // Routine to report the RX slot size
  23800. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  23801. +
  23802. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  23803. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  23804. +
  23805. + // Callback to inform a service that a Xon or Xoff message has been received
  23806. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  23807. +
  23808. + // Callback to inform a service that a server available reply message has been received
  23809. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  23810. +
  23811. + // Callback to indicate that bulk auxiliary messages have arrived
  23812. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  23813. +
  23814. + // Callback to indicate that a bulk auxiliary message has been transmitted
  23815. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  23816. +
  23817. + // Callback to provide information about the connection
  23818. + VCHI_CONNECTION_INFO connection_info;
  23819. +
  23820. + // Callback to notify that peer has requested disconnect
  23821. + VCHI_CONNECTION_DISCONNECT disconnect;
  23822. +
  23823. + // Callback to notify that peer has requested power change
  23824. + VCHI_CONNECTION_POWER_CONTROL power_control;
  23825. +
  23826. + // allocate memory suitably aligned for this connection
  23827. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  23828. +
  23829. + // free memory allocated by buffer_allocate
  23830. + VCHI_BUFFER_FREE buffer_free;
  23831. +
  23832. +};
  23833. +
  23834. +struct vchi_connection_t {
  23835. + const VCHI_CONNECTION_API_T *api;
  23836. + VCHI_CONNECTION_STATE_T *state;
  23837. +#ifdef VCHI_COARSE_LOCKING
  23838. + struct semaphore sem;
  23839. +#endif
  23840. +};
  23841. +
  23842. +
  23843. +#endif /* CONNECTION_H_ */
  23844. +
  23845. +/****************************** End of file **********************************/
  23846. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h linux-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  23847. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 1969-12-31 18:00:00.000000000 -0600
  23848. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 2014-12-11 14:02:53.544418001 -0600
  23849. @@ -0,0 +1,204 @@
  23850. +/**
  23851. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23852. + *
  23853. + * Redistribution and use in source and binary forms, with or without
  23854. + * modification, are permitted provided that the following conditions
  23855. + * are met:
  23856. + * 1. Redistributions of source code must retain the above copyright
  23857. + * notice, this list of conditions, and the following disclaimer,
  23858. + * without modification.
  23859. + * 2. Redistributions in binary form must reproduce the above copyright
  23860. + * notice, this list of conditions and the following disclaimer in the
  23861. + * documentation and/or other materials provided with the distribution.
  23862. + * 3. The names of the above-listed copyright holders may not be used
  23863. + * to endorse or promote products derived from this software without
  23864. + * specific prior written permission.
  23865. + *
  23866. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23867. + * GNU General Public License ("GPL") version 2, as published by the Free
  23868. + * Software Foundation.
  23869. + *
  23870. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23871. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23872. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23873. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23874. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23875. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23876. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23877. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23878. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23879. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23880. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23881. + */
  23882. +
  23883. +#ifndef _VCHI_MESSAGE_H_
  23884. +#define _VCHI_MESSAGE_H_
  23885. +
  23886. +#include <linux/kernel.h>
  23887. +#include <linux/types.h>
  23888. +#include <linux/semaphore.h>
  23889. +
  23890. +#include "interface/vchi/vchi_cfg_internal.h"
  23891. +#include "interface/vchi/vchi_common.h"
  23892. +
  23893. +
  23894. +typedef enum message_event_type {
  23895. + MESSAGE_EVENT_NONE,
  23896. + MESSAGE_EVENT_NOP,
  23897. + MESSAGE_EVENT_MESSAGE,
  23898. + MESSAGE_EVENT_SLOT_COMPLETE,
  23899. + MESSAGE_EVENT_RX_BULK_PAUSED,
  23900. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  23901. + MESSAGE_EVENT_TX_COMPLETE,
  23902. + MESSAGE_EVENT_MSG_DISCARDED
  23903. +} MESSAGE_EVENT_TYPE_T;
  23904. +
  23905. +typedef enum vchi_msg_flags
  23906. +{
  23907. + VCHI_MSG_FLAGS_NONE = 0x0,
  23908. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  23909. +} VCHI_MSG_FLAGS_T;
  23910. +
  23911. +typedef enum message_tx_channel
  23912. +{
  23913. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  23914. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  23915. +} MESSAGE_TX_CHANNEL_T;
  23916. +
  23917. +// Macros used for cycling through bulk channels
  23918. +#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)
  23919. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  23920. +
  23921. +typedef enum message_rx_channel
  23922. +{
  23923. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  23924. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  23925. +} MESSAGE_RX_CHANNEL_T;
  23926. +
  23927. +// Message receive slot information
  23928. +typedef struct rx_msg_slot_info {
  23929. +
  23930. + struct rx_msg_slot_info *next;
  23931. + //struct slot_info *prev;
  23932. +#if !defined VCHI_COARSE_LOCKING
  23933. + struct semaphore sem;
  23934. +#endif
  23935. +
  23936. + uint8_t *addr; // base address of slot
  23937. + uint32_t len; // length of slot in bytes
  23938. +
  23939. + uint32_t write_ptr; // hardware causes this to advance
  23940. + uint32_t read_ptr; // this module does the reading
  23941. + int active; // is this slot in the hardware dma fifo?
  23942. + uint32_t msgs_parsed; // count how many messages are in this slot
  23943. + uint32_t msgs_released; // how many messages have been released
  23944. + void *state; // connection state information
  23945. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  23946. +} RX_MSG_SLOTINFO_T;
  23947. +
  23948. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  23949. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  23950. +// driver will be tasked with sending the aligned core section.
  23951. +typedef struct rx_bulk_slotinfo_t {
  23952. + struct rx_bulk_slotinfo_t *next;
  23953. +
  23954. + struct semaphore *blocking;
  23955. +
  23956. + // needed by DMA
  23957. + void *addr;
  23958. + uint32_t len;
  23959. +
  23960. + // needed for the callback
  23961. + void *service;
  23962. + void *handle;
  23963. + VCHI_FLAGS_T flags;
  23964. +} RX_BULK_SLOTINFO_T;
  23965. +
  23966. +
  23967. +/* ----------------------------------------------------------------------
  23968. + * each connection driver will have a pool of the following struct.
  23969. + *
  23970. + * the pool will be managed by vchi_qman_*
  23971. + * this means there will be multiple queues (single linked lists)
  23972. + * a given struct message_info will be on exactly one of these queues
  23973. + * at any one time
  23974. + * -------------------------------------------------------------------- */
  23975. +typedef struct rx_message_info {
  23976. +
  23977. + struct message_info *next;
  23978. + //struct message_info *prev;
  23979. +
  23980. + uint8_t *addr;
  23981. + uint32_t len;
  23982. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  23983. + uint32_t tx_timestamp;
  23984. + uint32_t rx_timestamp;
  23985. +
  23986. +} RX_MESSAGE_INFO_T;
  23987. +
  23988. +typedef struct {
  23989. + MESSAGE_EVENT_TYPE_T type;
  23990. +
  23991. + struct {
  23992. + // for messages
  23993. + void *addr; // address of message
  23994. + uint16_t slot_delta; // whether this message indicated slot delta
  23995. + uint32_t len; // length of message
  23996. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  23997. + int32_t service; // service id this message is destined for
  23998. + uint32_t tx_timestamp; // timestamp from the header
  23999. + uint32_t rx_timestamp; // timestamp when we parsed it
  24000. + } message;
  24001. +
  24002. + // FIXME: cleanup slot reporting...
  24003. + RX_MSG_SLOTINFO_T *rx_msg;
  24004. + RX_BULK_SLOTINFO_T *rx_bulk;
  24005. + void *tx_handle;
  24006. + MESSAGE_TX_CHANNEL_T tx_channel;
  24007. +
  24008. +} MESSAGE_EVENT_T;
  24009. +
  24010. +
  24011. +// callbacks
  24012. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  24013. +
  24014. +typedef struct {
  24015. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  24016. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  24017. +
  24018. +
  24019. +// handle to this instance of message driver (as returned by ->open)
  24020. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  24021. +
  24022. +struct opaque_vchi_message_driver_t {
  24023. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  24024. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  24025. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  24026. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  24027. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  24028. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  24029. + 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)
  24030. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  24031. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  24032. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  24033. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  24034. +
  24035. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  24036. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  24037. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  24038. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  24039. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  24040. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  24041. +
  24042. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  24043. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  24044. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  24045. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  24046. + 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 );
  24047. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  24048. +};
  24049. +
  24050. +
  24051. +#endif // _VCHI_MESSAGE_H_
  24052. +
  24053. +/****************************** End of file ***********************************/
  24054. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  24055. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 1969-12-31 18:00:00.000000000 -0600
  24056. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 2014-12-11 14:02:53.544418001 -0600
  24057. @@ -0,0 +1,224 @@
  24058. +/**
  24059. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24060. + *
  24061. + * Redistribution and use in source and binary forms, with or without
  24062. + * modification, are permitted provided that the following conditions
  24063. + * are met:
  24064. + * 1. Redistributions of source code must retain the above copyright
  24065. + * notice, this list of conditions, and the following disclaimer,
  24066. + * without modification.
  24067. + * 2. Redistributions in binary form must reproduce the above copyright
  24068. + * notice, this list of conditions and the following disclaimer in the
  24069. + * documentation and/or other materials provided with the distribution.
  24070. + * 3. The names of the above-listed copyright holders may not be used
  24071. + * to endorse or promote products derived from this software without
  24072. + * specific prior written permission.
  24073. + *
  24074. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24075. + * GNU General Public License ("GPL") version 2, as published by the Free
  24076. + * Software Foundation.
  24077. + *
  24078. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24079. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24080. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24081. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24082. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24083. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24084. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24085. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24086. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24087. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24088. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24089. + */
  24090. +
  24091. +#ifndef VCHI_CFG_H_
  24092. +#define VCHI_CFG_H_
  24093. +
  24094. +/****************************************************************************************
  24095. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  24096. + * services.
  24097. + ***************************************************************************************/
  24098. +
  24099. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  24100. +/* Really determined by the message driver, and should be available from a run-time call. */
  24101. +#ifndef VCHI_BULK_ALIGN
  24102. +# if __VCCOREVER__ >= 0x04000000
  24103. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  24104. +# else
  24105. +# define VCHI_BULK_ALIGN 16
  24106. +# endif
  24107. +#endif
  24108. +
  24109. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  24110. +/* May be less than or greater than VCHI_BULK_ALIGN */
  24111. +/* Really determined by the message driver, and should be available from a run-time call. */
  24112. +#ifndef VCHI_BULK_GRANULARITY
  24113. +# if __VCCOREVER__ >= 0x04000000
  24114. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  24115. +# else
  24116. +# define VCHI_BULK_GRANULARITY 16
  24117. +# endif
  24118. +#endif
  24119. +
  24120. +/* The largest possible message to be queued with vchi_msg_queue. */
  24121. +#ifndef VCHI_MAX_MSG_SIZE
  24122. +# if defined VCHI_LOCAL_HOST_PORT
  24123. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  24124. +# else
  24125. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  24126. +# endif
  24127. +#endif
  24128. +
  24129. +/******************************************************************************************
  24130. + * Defines below are system configuration options, and should not be used by VCHI services.
  24131. + *****************************************************************************************/
  24132. +
  24133. +/* How many connections can we support? A localhost implementation uses 2 connections,
  24134. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  24135. + * driver. */
  24136. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  24137. +# define VCHI_MAX_NUM_CONNECTIONS 3
  24138. +#endif
  24139. +
  24140. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  24141. + * amount of static memory. */
  24142. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  24143. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  24144. +#endif
  24145. +
  24146. +/* Adjust if using a message driver that supports more logical TX channels */
  24147. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  24148. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  24149. +#endif
  24150. +
  24151. +/* Adjust if using a message driver that supports more logical RX channels */
  24152. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  24153. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  24154. +#endif
  24155. +
  24156. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  24157. + * receive queue space, less message headers. */
  24158. +#ifndef VCHI_NUM_READ_SLOTS
  24159. +# if defined(VCHI_LOCAL_HOST_PORT)
  24160. +# define VCHI_NUM_READ_SLOTS 4
  24161. +# else
  24162. +# define VCHI_NUM_READ_SLOTS 48
  24163. +# endif
  24164. +#endif
  24165. +
  24166. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  24167. + * performance. Only define on VideoCore end, talking to host.
  24168. + */
  24169. +//#define VCHI_MSG_RX_OVERRUN
  24170. +
  24171. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  24172. + * underneath VCHI will usually have its own buffering. */
  24173. +#ifndef VCHI_NUM_WRITE_SLOTS
  24174. +# define VCHI_NUM_WRITE_SLOTS 4
  24175. +#endif
  24176. +
  24177. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  24178. + * then it's taking up too much buffer space, and the peer service will be told to stop
  24179. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  24180. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  24181. + * is too high. */
  24182. +#ifndef VCHI_XOFF_THRESHOLD
  24183. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  24184. +#endif
  24185. +
  24186. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  24187. + * service has dequeued/released enough messages that it's now occupying
  24188. + * VCHI_XON_THRESHOLD slots or fewer. */
  24189. +#ifndef VCHI_XON_THRESHOLD
  24190. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  24191. +#endif
  24192. +
  24193. +/* A size below which a bulk transfer omits the handshake completely and always goes
  24194. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  24195. + * can guarantee this by enabling unaligned transmits).
  24196. + * Not API. */
  24197. +#ifndef VCHI_MIN_BULK_SIZE
  24198. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  24199. +#endif
  24200. +
  24201. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  24202. + * speed and latency; the smaller the chunk size the better change of messages and other
  24203. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  24204. + * break transmissions into chunks.
  24205. + */
  24206. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  24207. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  24208. +#endif
  24209. +
  24210. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  24211. + * with multiple-line frames. Only use if the receiver can cope. */
  24212. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  24213. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  24214. +#endif
  24215. +
  24216. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  24217. + * vchi_msg_queue will be blocked. */
  24218. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  24219. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  24220. +#endif
  24221. +
  24222. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  24223. + * will be suspended until older messages are dequeued/released. */
  24224. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  24225. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  24226. +#endif
  24227. +
  24228. +/* Really should be able to cope if we run out of received message descriptors, by
  24229. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  24230. + * under the carpet. */
  24231. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  24232. +# undef VCHI_RX_MSG_QUEUE_SIZE
  24233. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  24234. +#endif
  24235. +
  24236. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  24237. + * will be blocked. */
  24238. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  24239. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  24240. +#endif
  24241. +
  24242. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  24243. + * will be blocked. */
  24244. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  24245. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  24246. +#endif
  24247. +
  24248. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  24249. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  24250. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  24251. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  24252. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  24253. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  24254. +#endif
  24255. +
  24256. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  24257. + * transmitter on and off.
  24258. + */
  24259. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  24260. +
  24261. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  24262. +
  24263. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  24264. + * negative for no IDLE.
  24265. + */
  24266. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  24267. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  24268. +# endif
  24269. +
  24270. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  24271. + * negative for no OFF.
  24272. + */
  24273. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  24274. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  24275. +# endif
  24276. +
  24277. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  24278. +
  24279. +#endif /* VCHI_CFG_H_ */
  24280. +
  24281. +/****************************** End of file **********************************/
  24282. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  24283. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 1969-12-31 18:00:00.000000000 -0600
  24284. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 2014-12-11 14:02:53.544418001 -0600
  24285. @@ -0,0 +1,71 @@
  24286. +/**
  24287. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24288. + *
  24289. + * Redistribution and use in source and binary forms, with or without
  24290. + * modification, are permitted provided that the following conditions
  24291. + * are met:
  24292. + * 1. Redistributions of source code must retain the above copyright
  24293. + * notice, this list of conditions, and the following disclaimer,
  24294. + * without modification.
  24295. + * 2. Redistributions in binary form must reproduce the above copyright
  24296. + * notice, this list of conditions and the following disclaimer in the
  24297. + * documentation and/or other materials provided with the distribution.
  24298. + * 3. The names of the above-listed copyright holders may not be used
  24299. + * to endorse or promote products derived from this software without
  24300. + * specific prior written permission.
  24301. + *
  24302. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24303. + * GNU General Public License ("GPL") version 2, as published by the Free
  24304. + * Software Foundation.
  24305. + *
  24306. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24307. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24308. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24309. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24310. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24311. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24312. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24313. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24314. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24315. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24316. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24317. + */
  24318. +
  24319. +#ifndef VCHI_CFG_INTERNAL_H_
  24320. +#define VCHI_CFG_INTERNAL_H_
  24321. +
  24322. +/****************************************************************************************
  24323. + * Control optimisation attempts.
  24324. + ***************************************************************************************/
  24325. +
  24326. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  24327. +#define VCHI_COARSE_LOCKING
  24328. +
  24329. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  24330. +// (only relevant if VCHI_COARSE_LOCKING)
  24331. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  24332. +
  24333. +// Avoid lock on non-blocking peek
  24334. +// (only relevant if VCHI_COARSE_LOCKING)
  24335. +#define VCHI_AVOID_PEEK_LOCK
  24336. +
  24337. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  24338. +#define VCHI_MULTIPLE_HANDLER_THREADS
  24339. +
  24340. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  24341. +// our way through the pool of descriptors.
  24342. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  24343. +
  24344. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  24345. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  24346. +
  24347. +// Don't use message descriptors for TX messages that don't need them
  24348. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  24349. +
  24350. +// Nano-locks for multiqueue
  24351. +//#define VCHI_MQUEUE_NANOLOCKS
  24352. +
  24353. +// Lock-free(er) dequeuing
  24354. +//#define VCHI_RX_NANOLOCKS
  24355. +
  24356. +#endif /*VCHI_CFG_INTERNAL_H_*/
  24357. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_common.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  24358. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_common.h 1969-12-31 18:00:00.000000000 -0600
  24359. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h 2014-12-11 14:02:53.548418001 -0600
  24360. @@ -0,0 +1,174 @@
  24361. +/**
  24362. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24363. + *
  24364. + * Redistribution and use in source and binary forms, with or without
  24365. + * modification, are permitted provided that the following conditions
  24366. + * are met:
  24367. + * 1. Redistributions of source code must retain the above copyright
  24368. + * notice, this list of conditions, and the following disclaimer,
  24369. + * without modification.
  24370. + * 2. Redistributions in binary form must reproduce the above copyright
  24371. + * notice, this list of conditions and the following disclaimer in the
  24372. + * documentation and/or other materials provided with the distribution.
  24373. + * 3. The names of the above-listed copyright holders may not be used
  24374. + * to endorse or promote products derived from this software without
  24375. + * specific prior written permission.
  24376. + *
  24377. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24378. + * GNU General Public License ("GPL") version 2, as published by the Free
  24379. + * Software Foundation.
  24380. + *
  24381. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24382. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24383. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24384. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24385. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24386. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24387. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24388. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24389. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24390. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24391. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24392. + */
  24393. +
  24394. +#ifndef VCHI_COMMON_H_
  24395. +#define VCHI_COMMON_H_
  24396. +
  24397. +
  24398. +//flags used when sending messages (must be bitmapped)
  24399. +typedef enum
  24400. +{
  24401. + VCHI_FLAGS_NONE = 0x0,
  24402. + 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)
  24403. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  24404. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  24405. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  24406. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  24407. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  24408. +
  24409. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  24410. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  24411. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  24412. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  24413. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  24414. + VCHI_FLAGS_INTERNAL = 0xFF0000
  24415. +} VCHI_FLAGS_T;
  24416. +
  24417. +// constants for vchi_crc_control()
  24418. +typedef enum {
  24419. + VCHI_CRC_NOTHING = -1,
  24420. + VCHI_CRC_PER_SERVICE = 0,
  24421. + VCHI_CRC_EVERYTHING = 1,
  24422. +} VCHI_CRC_CONTROL_T;
  24423. +
  24424. +//callback reasons when an event occurs on a service
  24425. +typedef enum
  24426. +{
  24427. + VCHI_CALLBACK_REASON_MIN,
  24428. +
  24429. + //This indicates that there is data available
  24430. + //handle is the msg id that was transmitted with the data
  24431. + // When a message is received and there was no FULL message available previously, send callback
  24432. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  24433. + VCHI_CALLBACK_MSG_AVAILABLE,
  24434. + VCHI_CALLBACK_MSG_SENT,
  24435. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  24436. +
  24437. + // This indicates that a transfer from the other side has completed
  24438. + VCHI_CALLBACK_BULK_RECEIVED,
  24439. + //This indicates that data queued up to be sent has now gone
  24440. + //handle is the msg id that was used when sending the data
  24441. + VCHI_CALLBACK_BULK_SENT,
  24442. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  24443. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  24444. +
  24445. + VCHI_CALLBACK_SERVICE_CLOSED,
  24446. +
  24447. + // this side has sent XOFF to peer due to lack of data consumption by service
  24448. + // (suggests the service may need to take some recovery action if it has
  24449. + // been deliberately holding off consuming data)
  24450. + VCHI_CALLBACK_SENT_XOFF,
  24451. + VCHI_CALLBACK_SENT_XON,
  24452. +
  24453. + // indicates that a bulk transfer has finished reading the source buffer
  24454. + VCHI_CALLBACK_BULK_DATA_READ,
  24455. +
  24456. + // power notification events (currently host side only)
  24457. + VCHI_CALLBACK_PEER_OFF,
  24458. + VCHI_CALLBACK_PEER_SUSPENDED,
  24459. + VCHI_CALLBACK_PEER_ON,
  24460. + VCHI_CALLBACK_PEER_RESUMED,
  24461. + VCHI_CALLBACK_FORCED_POWER_OFF,
  24462. +
  24463. +#ifdef USE_VCHIQ_ARM
  24464. + // some extra notifications provided by vchiq_arm
  24465. + VCHI_CALLBACK_SERVICE_OPENED,
  24466. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  24467. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  24468. +#endif
  24469. +
  24470. + VCHI_CALLBACK_REASON_MAX
  24471. +} VCHI_CALLBACK_REASON_T;
  24472. +
  24473. +// service control options
  24474. +typedef enum
  24475. +{
  24476. + VCHI_SERVICE_OPTION_MIN,
  24477. +
  24478. + VCHI_SERVICE_OPTION_TRACE,
  24479. +
  24480. + VCHI_SERVICE_OPTION_MAX
  24481. +} VCHI_SERVICE_OPTION_T;
  24482. +
  24483. +
  24484. +//Callback used by all services / bulk transfers
  24485. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  24486. + VCHI_CALLBACK_REASON_T reason,
  24487. + void *handle ); //for transmitting msg's only
  24488. +
  24489. +
  24490. +
  24491. +/*
  24492. + * Define vector struct for scatter-gather (vector) operations
  24493. + * Vectors can be nested - if a vector element has negative length, then
  24494. + * the data pointer is treated as pointing to another vector array, with
  24495. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  24496. + * you can do this:
  24497. + *
  24498. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  24499. + * {
  24500. + * VCHI_MSG_VECTOR_T nv[2];
  24501. + * nv[0].vec_base = my_header;
  24502. + * nv[0].vec_len = sizeof my_header;
  24503. + * nv[1].vec_base = v;
  24504. + * nv[1].vec_len = -n;
  24505. + * ...
  24506. + *
  24507. + */
  24508. +typedef struct vchi_msg_vector {
  24509. + const void *vec_base;
  24510. + int32_t vec_len;
  24511. +} VCHI_MSG_VECTOR_T;
  24512. +
  24513. +// Opaque type for a connection API
  24514. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  24515. +
  24516. +// Opaque type for a message driver
  24517. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  24518. +
  24519. +
  24520. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  24521. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  24522. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  24523. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  24524. +// is used again after messages for that service are removed/dequeued by any
  24525. +// means other than vchi_msg_iter_... calls on the iterator itself.
  24526. +typedef struct {
  24527. + struct opaque_vchi_service_t *service;
  24528. + void *last;
  24529. + void *next;
  24530. + void *remove;
  24531. +} VCHI_MSG_ITER_T;
  24532. +
  24533. +
  24534. +#endif // VCHI_COMMON_H_
  24535. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h
  24536. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi.h 1969-12-31 18:00:00.000000000 -0600
  24537. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h 2014-12-11 14:02:53.544418001 -0600
  24538. @@ -0,0 +1,378 @@
  24539. +/**
  24540. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24541. + *
  24542. + * Redistribution and use in source and binary forms, with or without
  24543. + * modification, are permitted provided that the following conditions
  24544. + * are met:
  24545. + * 1. Redistributions of source code must retain the above copyright
  24546. + * notice, this list of conditions, and the following disclaimer,
  24547. + * without modification.
  24548. + * 2. Redistributions in binary form must reproduce the above copyright
  24549. + * notice, this list of conditions and the following disclaimer in the
  24550. + * documentation and/or other materials provided with the distribution.
  24551. + * 3. The names of the above-listed copyright holders may not be used
  24552. + * to endorse or promote products derived from this software without
  24553. + * specific prior written permission.
  24554. + *
  24555. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24556. + * GNU General Public License ("GPL") version 2, as published by the Free
  24557. + * Software Foundation.
  24558. + *
  24559. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24560. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24561. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24562. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24563. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24564. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24565. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24566. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24567. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24568. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24569. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24570. + */
  24571. +
  24572. +#ifndef VCHI_H_
  24573. +#define VCHI_H_
  24574. +
  24575. +#include "interface/vchi/vchi_cfg.h"
  24576. +#include "interface/vchi/vchi_common.h"
  24577. +#include "interface/vchi/connections/connection.h"
  24578. +#include "vchi_mh.h"
  24579. +
  24580. +
  24581. +/******************************************************************************
  24582. + Global defs
  24583. + *****************************************************************************/
  24584. +
  24585. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  24586. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  24587. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  24588. +
  24589. +#ifdef USE_VCHIQ_ARM
  24590. +#define VCHI_BULK_ALIGNED(x) 1
  24591. +#else
  24592. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  24593. +#endif
  24594. +
  24595. +struct vchi_version {
  24596. + uint32_t version;
  24597. + uint32_t version_min;
  24598. +};
  24599. +#define VCHI_VERSION(v_) { v_, v_ }
  24600. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  24601. +
  24602. +typedef enum
  24603. +{
  24604. + VCHI_VEC_POINTER,
  24605. + VCHI_VEC_HANDLE,
  24606. + VCHI_VEC_LIST
  24607. +} VCHI_MSG_VECTOR_TYPE_T;
  24608. +
  24609. +typedef struct vchi_msg_vector_ex {
  24610. +
  24611. + VCHI_MSG_VECTOR_TYPE_T type;
  24612. + union
  24613. + {
  24614. + // a memory handle
  24615. + struct
  24616. + {
  24617. + VCHI_MEM_HANDLE_T handle;
  24618. + uint32_t offset;
  24619. + int32_t vec_len;
  24620. + } handle;
  24621. +
  24622. + // an ordinary data pointer
  24623. + struct
  24624. + {
  24625. + const void *vec_base;
  24626. + int32_t vec_len;
  24627. + } ptr;
  24628. +
  24629. + // a nested vector list
  24630. + struct
  24631. + {
  24632. + struct vchi_msg_vector_ex *vec;
  24633. + uint32_t vec_len;
  24634. + } list;
  24635. + } u;
  24636. +} VCHI_MSG_VECTOR_EX_T;
  24637. +
  24638. +
  24639. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  24640. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  24641. +
  24642. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  24643. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  24644. +
  24645. +// Macros to manipulate 'FOURCC' values
  24646. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  24647. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  24648. +
  24649. +
  24650. +// Opaque service information
  24651. +struct opaque_vchi_service_t;
  24652. +
  24653. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  24654. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  24655. +typedef struct
  24656. +{
  24657. + struct opaque_vchi_service_t *service;
  24658. + void *message;
  24659. +} VCHI_HELD_MSG_T;
  24660. +
  24661. +
  24662. +
  24663. +// structure used to provide the information needed to open a server or a client
  24664. +typedef struct {
  24665. + struct vchi_version version;
  24666. + int32_t service_id;
  24667. + VCHI_CONNECTION_T *connection;
  24668. + uint32_t rx_fifo_size;
  24669. + uint32_t tx_fifo_size;
  24670. + VCHI_CALLBACK_T callback;
  24671. + void *callback_param;
  24672. + /* client intends to receive bulk transfers of
  24673. + odd lengths or into unaligned buffers */
  24674. + int32_t want_unaligned_bulk_rx;
  24675. + /* client intends to transmit bulk transfers of
  24676. + odd lengths or out of unaligned buffers */
  24677. + int32_t want_unaligned_bulk_tx;
  24678. + /* client wants to check CRCs on (bulk) xfers.
  24679. + Only needs to be set at 1 end - will do both directions. */
  24680. + int32_t want_crc;
  24681. +} SERVICE_CREATION_T;
  24682. +
  24683. +// Opaque handle for a VCHI instance
  24684. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  24685. +
  24686. +// Opaque handle for a server or client
  24687. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  24688. +
  24689. +// Service registration & startup
  24690. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  24691. +
  24692. +typedef struct service_info_tag {
  24693. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  24694. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  24695. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  24696. +} SERVICE_INFO_T;
  24697. +
  24698. +/******************************************************************************
  24699. + Global funcs - implementation is specific to which side you are on (local / remote)
  24700. + *****************************************************************************/
  24701. +
  24702. +#ifdef __cplusplus
  24703. +extern "C" {
  24704. +#endif
  24705. +
  24706. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  24707. + const VCHI_MESSAGE_DRIVER_T * low_level);
  24708. +
  24709. +
  24710. +// Routine used to initialise the vchi on both local + remote connections
  24711. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  24712. +
  24713. +extern int32_t vchi_exit( void );
  24714. +
  24715. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  24716. + const uint32_t num_connections,
  24717. + VCHI_INSTANCE_T instance_handle );
  24718. +
  24719. +//When this is called, ensure that all services have no data pending.
  24720. +//Bulk transfers can remain 'queued'
  24721. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  24722. +
  24723. +// Global control over bulk CRC checking
  24724. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  24725. + VCHI_CRC_CONTROL_T control );
  24726. +
  24727. +// helper functions
  24728. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  24729. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  24730. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  24731. +
  24732. +
  24733. +/******************************************************************************
  24734. + Global service API
  24735. + *****************************************************************************/
  24736. +// Routine to create a named service
  24737. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  24738. + SERVICE_CREATION_T *setup,
  24739. + VCHI_SERVICE_HANDLE_T *handle );
  24740. +
  24741. +// Routine to destory a service
  24742. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  24743. +
  24744. +// Routine to open a named service
  24745. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  24746. + SERVICE_CREATION_T *setup,
  24747. + VCHI_SERVICE_HANDLE_T *handle);
  24748. +
  24749. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  24750. + short *peer_version );
  24751. +
  24752. +// Routine to close a named service
  24753. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  24754. +
  24755. +// Routine to increment ref count on a named service
  24756. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  24757. +
  24758. +// Routine to decrement ref count on a named service
  24759. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  24760. +
  24761. +// Routine to set a control option for a named service
  24762. +extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
  24763. + VCHI_SERVICE_OPTION_T option,
  24764. + int value);
  24765. +
  24766. +// Routine to send a message across a service
  24767. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  24768. + const void *data,
  24769. + uint32_t data_size,
  24770. + VCHI_FLAGS_T flags,
  24771. + void *msg_handle );
  24772. +
  24773. +// scatter-gather (vector) and send message
  24774. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  24775. + VCHI_MSG_VECTOR_EX_T *vector,
  24776. + uint32_t count,
  24777. + VCHI_FLAGS_T flags,
  24778. + void *msg_handle );
  24779. +
  24780. +// legacy scatter-gather (vector) and send message, only handles pointers
  24781. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  24782. + VCHI_MSG_VECTOR_T *vector,
  24783. + uint32_t count,
  24784. + VCHI_FLAGS_T flags,
  24785. + void *msg_handle );
  24786. +
  24787. +// Routine to receive a msg from a service
  24788. +// Dequeue is equivalent to hold, copy into client buffer, release
  24789. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  24790. + void *data,
  24791. + uint32_t max_data_size_to_read,
  24792. + uint32_t *actual_msg_size,
  24793. + VCHI_FLAGS_T flags );
  24794. +
  24795. +// Routine to look at a message in place.
  24796. +// The message is not dequeued, so a subsequent call to peek or dequeue
  24797. +// will return the same message.
  24798. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  24799. + void **data,
  24800. + uint32_t *msg_size,
  24801. + VCHI_FLAGS_T flags );
  24802. +
  24803. +// Routine to remove a message after it has been read in place with peek
  24804. +// The first message on the queue is dequeued.
  24805. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  24806. +
  24807. +// Routine to look at a message in place.
  24808. +// The message is dequeued, so the caller is left holding it; the descriptor is
  24809. +// filled in and must be released when the user has finished with the message.
  24810. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  24811. + void **data, // } may be NULL, as info can be
  24812. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  24813. + VCHI_FLAGS_T flags,
  24814. + VCHI_HELD_MSG_T *message_descriptor );
  24815. +
  24816. +// Initialise an iterator to look through messages in place
  24817. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  24818. + VCHI_MSG_ITER_T *iter,
  24819. + VCHI_FLAGS_T flags );
  24820. +
  24821. +/******************************************************************************
  24822. + Global service support API - operations on held messages and message iterators
  24823. + *****************************************************************************/
  24824. +
  24825. +// Routine to get the address of a held message
  24826. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  24827. +
  24828. +// Routine to get the size of a held message
  24829. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  24830. +
  24831. +// Routine to get the transmit timestamp as written into the header by the peer
  24832. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  24833. +
  24834. +// Routine to get the reception timestamp, written as we parsed the header
  24835. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  24836. +
  24837. +// Routine to release a held message after it has been processed
  24838. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  24839. +
  24840. +// Indicates whether the iterator has a next message.
  24841. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  24842. +
  24843. +// Return the pointer and length for the next message and advance the iterator.
  24844. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  24845. + void **data,
  24846. + uint32_t *msg_size );
  24847. +
  24848. +// Remove the last message returned by vchi_msg_iter_next.
  24849. +// Can only be called once after each call to vchi_msg_iter_next.
  24850. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  24851. +
  24852. +// Hold the last message returned by vchi_msg_iter_next.
  24853. +// Can only be called once after each call to vchi_msg_iter_next.
  24854. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  24855. + VCHI_HELD_MSG_T *message );
  24856. +
  24857. +// Return information for the next message, and hold it, advancing the iterator.
  24858. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  24859. + void **data, // } may be NULL
  24860. + uint32_t *msg_size, // }
  24861. + VCHI_HELD_MSG_T *message );
  24862. +
  24863. +
  24864. +/******************************************************************************
  24865. + Global bulk API
  24866. + *****************************************************************************/
  24867. +
  24868. +// Routine to prepare interface for a transfer from the other side
  24869. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  24870. + void *data_dst,
  24871. + uint32_t data_size,
  24872. + VCHI_FLAGS_T flags,
  24873. + void *transfer_handle );
  24874. +
  24875. +
  24876. +// Prepare interface for a transfer from the other side into relocatable memory.
  24877. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  24878. + VCHI_MEM_HANDLE_T h_dst,
  24879. + uint32_t offset,
  24880. + uint32_t data_size,
  24881. + const VCHI_FLAGS_T flags,
  24882. + void * const bulk_handle );
  24883. +
  24884. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  24885. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  24886. + const void *data_src,
  24887. + uint32_t data_size,
  24888. + VCHI_FLAGS_T flags,
  24889. + void *transfer_handle );
  24890. +
  24891. +
  24892. +/******************************************************************************
  24893. + Configuration plumbing
  24894. + *****************************************************************************/
  24895. +
  24896. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  24897. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  24898. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  24899. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  24900. +
  24901. +// declare all message drivers here
  24902. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  24903. +
  24904. +#ifdef __cplusplus
  24905. +}
  24906. +#endif
  24907. +
  24908. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  24909. + VCHI_MEM_HANDLE_T h_src,
  24910. + uint32_t offset,
  24911. + uint32_t data_size,
  24912. + VCHI_FLAGS_T flags,
  24913. + void *transfer_handle );
  24914. +#endif /* VCHI_H_ */
  24915. +
  24916. +/****************************** End of file **********************************/
  24917. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_mh.h linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  24918. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 1969-12-31 18:00:00.000000000 -0600
  24919. +++ linux-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 2014-12-11 14:02:53.548418001 -0600
  24920. @@ -0,0 +1,42 @@
  24921. +/**
  24922. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24923. + *
  24924. + * Redistribution and use in source and binary forms, with or without
  24925. + * modification, are permitted provided that the following conditions
  24926. + * are met:
  24927. + * 1. Redistributions of source code must retain the above copyright
  24928. + * notice, this list of conditions, and the following disclaimer,
  24929. + * without modification.
  24930. + * 2. Redistributions in binary form must reproduce the above copyright
  24931. + * notice, this list of conditions and the following disclaimer in the
  24932. + * documentation and/or other materials provided with the distribution.
  24933. + * 3. The names of the above-listed copyright holders may not be used
  24934. + * to endorse or promote products derived from this software without
  24935. + * specific prior written permission.
  24936. + *
  24937. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24938. + * GNU General Public License ("GPL") version 2, as published by the Free
  24939. + * Software Foundation.
  24940. + *
  24941. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24942. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24943. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24944. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24945. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24946. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24947. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24948. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24949. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24950. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24951. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24952. + */
  24953. +
  24954. +#ifndef VCHI_MH_H_
  24955. +#define VCHI_MH_H_
  24956. +
  24957. +#include <linux/types.h>
  24958. +
  24959. +typedef int32_t VCHI_MEM_HANDLE_T;
  24960. +#define VCHI_MEM_HANDLE_INVALID 0
  24961. +
  24962. +#endif
  24963. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  24964. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 1969-12-31 18:00:00.000000000 -0600
  24965. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 2014-12-11 14:02:53.548418001 -0600
  24966. @@ -0,0 +1,562 @@
  24967. +/**
  24968. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24969. + *
  24970. + * Redistribution and use in source and binary forms, with or without
  24971. + * modification, are permitted provided that the following conditions
  24972. + * are met:
  24973. + * 1. Redistributions of source code must retain the above copyright
  24974. + * notice, this list of conditions, and the following disclaimer,
  24975. + * without modification.
  24976. + * 2. Redistributions in binary form must reproduce the above copyright
  24977. + * notice, this list of conditions and the following disclaimer in the
  24978. + * documentation and/or other materials provided with the distribution.
  24979. + * 3. The names of the above-listed copyright holders may not be used
  24980. + * to endorse or promote products derived from this software without
  24981. + * specific prior written permission.
  24982. + *
  24983. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24984. + * GNU General Public License ("GPL") version 2, as published by the Free
  24985. + * Software Foundation.
  24986. + *
  24987. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24988. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24989. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24990. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24991. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24992. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24993. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24994. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24995. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24996. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24997. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24998. + */
  24999. +
  25000. +#include <linux/kernel.h>
  25001. +#include <linux/types.h>
  25002. +#include <linux/errno.h>
  25003. +#include <linux/interrupt.h>
  25004. +#include <linux/irq.h>
  25005. +#include <linux/pagemap.h>
  25006. +#include <linux/dma-mapping.h>
  25007. +#include <linux/version.h>
  25008. +#include <linux/io.h>
  25009. +#include <linux/uaccess.h>
  25010. +#include <asm/pgtable.h>
  25011. +
  25012. +#include <mach/irqs.h>
  25013. +
  25014. +#include <mach/platform.h>
  25015. +#include <mach/vcio.h>
  25016. +
  25017. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  25018. +
  25019. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  25020. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  25021. +
  25022. +#include "vchiq_arm.h"
  25023. +#include "vchiq_2835.h"
  25024. +#include "vchiq_connected.h"
  25025. +#include "vchiq_killable.h"
  25026. +
  25027. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  25028. +
  25029. +typedef struct vchiq_2835_state_struct {
  25030. + int inited;
  25031. + VCHIQ_ARM_STATE_T arm_state;
  25032. +} VCHIQ_2835_ARM_STATE_T;
  25033. +
  25034. +static char *g_slot_mem;
  25035. +static int g_slot_mem_size;
  25036. +dma_addr_t g_slot_phys;
  25037. +static FRAGMENTS_T *g_fragments_base;
  25038. +static FRAGMENTS_T *g_free_fragments;
  25039. +struct semaphore g_free_fragments_sema;
  25040. +
  25041. +extern int vchiq_arm_log_level;
  25042. +
  25043. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  25044. +
  25045. +static irqreturn_t
  25046. +vchiq_doorbell_irq(int irq, void *dev_id);
  25047. +
  25048. +static int
  25049. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  25050. + struct task_struct *task, PAGELIST_T ** ppagelist);
  25051. +
  25052. +static void
  25053. +free_pagelist(PAGELIST_T *pagelist, int actual);
  25054. +
  25055. +int __init
  25056. +vchiq_platform_init(VCHIQ_STATE_T *state)
  25057. +{
  25058. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  25059. + int frag_mem_size;
  25060. + int err;
  25061. + int i;
  25062. +
  25063. + /* Allocate space for the channels in coherent memory */
  25064. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  25065. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  25066. +
  25067. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  25068. + &g_slot_phys, GFP_ATOMIC);
  25069. +
  25070. + if (!g_slot_mem) {
  25071. + vchiq_log_error(vchiq_arm_log_level,
  25072. + "Unable to allocate channel memory");
  25073. + err = -ENOMEM;
  25074. + goto failed_alloc;
  25075. + }
  25076. +
  25077. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  25078. +
  25079. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  25080. + if (!vchiq_slot_zero) {
  25081. + err = -EINVAL;
  25082. + goto failed_init_slots;
  25083. + }
  25084. +
  25085. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  25086. + (int)g_slot_phys + g_slot_mem_size;
  25087. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  25088. + MAX_FRAGMENTS;
  25089. +
  25090. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  25091. + g_slot_mem_size += frag_mem_size;
  25092. +
  25093. + g_free_fragments = g_fragments_base;
  25094. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  25095. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  25096. + &g_fragments_base[i + 1];
  25097. + }
  25098. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  25099. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  25100. +
  25101. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  25102. + VCHIQ_SUCCESS) {
  25103. + err = -EINVAL;
  25104. + goto failed_vchiq_init;
  25105. + }
  25106. +
  25107. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  25108. + IRQF_IRQPOLL, "VCHIQ doorbell",
  25109. + state);
  25110. + if (err < 0) {
  25111. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  25112. + "irq=%d err=%d", __func__,
  25113. + VCHIQ_DOORBELL_IRQ, err);
  25114. + goto failed_request_irq;
  25115. + }
  25116. +
  25117. + /* Send the base address of the slots to VideoCore */
  25118. +
  25119. + dsb(); /* Ensure all writes have completed */
  25120. +
  25121. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  25122. +
  25123. + vchiq_log_info(vchiq_arm_log_level,
  25124. + "vchiq_init - done (slots %x, phys %x)",
  25125. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  25126. +
  25127. + vchiq_call_connected_callbacks();
  25128. +
  25129. + return 0;
  25130. +
  25131. +failed_request_irq:
  25132. +failed_vchiq_init:
  25133. +failed_init_slots:
  25134. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  25135. +
  25136. +failed_alloc:
  25137. + return err;
  25138. +}
  25139. +
  25140. +void __exit
  25141. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  25142. +{
  25143. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  25144. + dma_free_coherent(NULL, g_slot_mem_size,
  25145. + g_slot_mem, g_slot_phys);
  25146. +}
  25147. +
  25148. +
  25149. +VCHIQ_STATUS_T
  25150. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  25151. +{
  25152. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25153. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  25154. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  25155. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  25156. + if(status != VCHIQ_SUCCESS)
  25157. + {
  25158. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  25159. + }
  25160. + return status;
  25161. +}
  25162. +
  25163. +VCHIQ_ARM_STATE_T*
  25164. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  25165. +{
  25166. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  25167. + {
  25168. + BUG();
  25169. + }
  25170. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  25171. +}
  25172. +
  25173. +void
  25174. +remote_event_signal(REMOTE_EVENT_T *event)
  25175. +{
  25176. + wmb();
  25177. +
  25178. + event->fired = 1;
  25179. +
  25180. + dsb(); /* data barrier operation */
  25181. +
  25182. + if (event->armed) {
  25183. + /* trigger vc interrupt */
  25184. +
  25185. + writel(0, __io_address(ARM_0_BELL2));
  25186. + }
  25187. +}
  25188. +
  25189. +int
  25190. +vchiq_copy_from_user(void *dst, const void *src, int size)
  25191. +{
  25192. + if ((uint32_t)src < TASK_SIZE) {
  25193. + return copy_from_user(dst, src, size);
  25194. + } else {
  25195. + memcpy(dst, src, size);
  25196. + return 0;
  25197. + }
  25198. +}
  25199. +
  25200. +VCHIQ_STATUS_T
  25201. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  25202. + void *offset, int size, int dir)
  25203. +{
  25204. + PAGELIST_T *pagelist;
  25205. + int ret;
  25206. +
  25207. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  25208. +
  25209. + ret = create_pagelist((char __user *)offset, size,
  25210. + (dir == VCHIQ_BULK_RECEIVE)
  25211. + ? PAGELIST_READ
  25212. + : PAGELIST_WRITE,
  25213. + current,
  25214. + &pagelist);
  25215. + if (ret != 0)
  25216. + return VCHIQ_ERROR;
  25217. +
  25218. + bulk->handle = memhandle;
  25219. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  25220. +
  25221. + /* Store the pagelist address in remote_data, which isn't used by the
  25222. + slave. */
  25223. + bulk->remote_data = pagelist;
  25224. +
  25225. + return VCHIQ_SUCCESS;
  25226. +}
  25227. +
  25228. +void
  25229. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  25230. +{
  25231. + if (bulk && bulk->remote_data && bulk->actual)
  25232. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  25233. +}
  25234. +
  25235. +void
  25236. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  25237. +{
  25238. + /*
  25239. + * This should only be called on the master (VideoCore) side, but
  25240. + * provide an implementation to avoid the need for ifdefery.
  25241. + */
  25242. + BUG();
  25243. +}
  25244. +
  25245. +void
  25246. +vchiq_dump_platform_state(void *dump_context)
  25247. +{
  25248. + char buf[80];
  25249. + int len;
  25250. + len = snprintf(buf, sizeof(buf),
  25251. + " Platform: 2835 (VC master)");
  25252. + vchiq_dump(dump_context, buf, len + 1);
  25253. +}
  25254. +
  25255. +VCHIQ_STATUS_T
  25256. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  25257. +{
  25258. + return VCHIQ_ERROR;
  25259. +}
  25260. +
  25261. +VCHIQ_STATUS_T
  25262. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  25263. +{
  25264. + return VCHIQ_SUCCESS;
  25265. +}
  25266. +
  25267. +void
  25268. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  25269. +{
  25270. +}
  25271. +
  25272. +void
  25273. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  25274. +{
  25275. +}
  25276. +
  25277. +int
  25278. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  25279. +{
  25280. + return 1; // autosuspend not supported - videocore always wanted
  25281. +}
  25282. +
  25283. +int
  25284. +vchiq_platform_use_suspend_timer(void)
  25285. +{
  25286. + return 0;
  25287. +}
  25288. +void
  25289. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  25290. +{
  25291. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  25292. +}
  25293. +void
  25294. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  25295. +{
  25296. + (void)state;
  25297. +}
  25298. +/*
  25299. + * Local functions
  25300. + */
  25301. +
  25302. +static irqreturn_t
  25303. +vchiq_doorbell_irq(int irq, void *dev_id)
  25304. +{
  25305. + VCHIQ_STATE_T *state = dev_id;
  25306. + irqreturn_t ret = IRQ_NONE;
  25307. + unsigned int status;
  25308. +
  25309. + /* Read (and clear) the doorbell */
  25310. + status = readl(__io_address(ARM_0_BELL0));
  25311. +
  25312. + if (status & 0x4) { /* Was the doorbell rung? */
  25313. + remote_event_pollall(state);
  25314. + ret = IRQ_HANDLED;
  25315. + }
  25316. +
  25317. + return ret;
  25318. +}
  25319. +
  25320. +/* There is a potential problem with partial cache lines (pages?)
  25321. +** at the ends of the block when reading. If the CPU accessed anything in
  25322. +** the same line (page?) then it may have pulled old data into the cache,
  25323. +** obscuring the new data underneath. We can solve this by transferring the
  25324. +** partial cache lines separately, and allowing the ARM to copy into the
  25325. +** cached area.
  25326. +
  25327. +** N.B. This implementation plays slightly fast and loose with the Linux
  25328. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  25329. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  25330. +** from increased speed as a result.
  25331. +*/
  25332. +
  25333. +static int
  25334. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  25335. + struct task_struct *task, PAGELIST_T ** ppagelist)
  25336. +{
  25337. + PAGELIST_T *pagelist;
  25338. + struct page **pages;
  25339. + struct page *page;
  25340. + unsigned long *addrs;
  25341. + unsigned int num_pages, offset, i;
  25342. + char *addr, *base_addr, *next_addr;
  25343. + int run, addridx, actual_pages;
  25344. + unsigned long *need_release;
  25345. +
  25346. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  25347. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  25348. +
  25349. + *ppagelist = NULL;
  25350. +
  25351. + /* Allocate enough storage to hold the page pointers and the page
  25352. + ** list
  25353. + */
  25354. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  25355. + (num_pages * sizeof(unsigned long)) +
  25356. + sizeof(unsigned long) +
  25357. + (num_pages * sizeof(pages[0])),
  25358. + GFP_KERNEL);
  25359. +
  25360. + vchiq_log_trace(vchiq_arm_log_level,
  25361. + "create_pagelist - %x", (unsigned int)pagelist);
  25362. + if (!pagelist)
  25363. + return -ENOMEM;
  25364. +
  25365. + addrs = pagelist->addrs;
  25366. + need_release = (unsigned long *)(addrs + num_pages);
  25367. + pages = (struct page **)(addrs + num_pages + 1);
  25368. +
  25369. + if (is_vmalloc_addr(buf)) {
  25370. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  25371. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  25372. + }
  25373. + *need_release = 0; /* do not try and release vmalloc pages */
  25374. + } else {
  25375. + down_read(&task->mm->mmap_sem);
  25376. + actual_pages = get_user_pages(task, task->mm,
  25377. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  25378. + num_pages,
  25379. + (type == PAGELIST_READ) /*Write */ ,
  25380. + 0 /*Force */ ,
  25381. + pages,
  25382. + NULL /*vmas */);
  25383. + up_read(&task->mm->mmap_sem);
  25384. +
  25385. + if (actual_pages != num_pages) {
  25386. + vchiq_log_info(vchiq_arm_log_level,
  25387. + "create_pagelist - only %d/%d pages locked",
  25388. + actual_pages,
  25389. + num_pages);
  25390. +
  25391. + /* This is probably due to the process being killed */
  25392. + while (actual_pages > 0)
  25393. + {
  25394. + actual_pages--;
  25395. + page_cache_release(pages[actual_pages]);
  25396. + }
  25397. + kfree(pagelist);
  25398. + if (actual_pages == 0)
  25399. + actual_pages = -ENOMEM;
  25400. + return actual_pages;
  25401. + }
  25402. + *need_release = 1; /* release user pages */
  25403. + }
  25404. +
  25405. + pagelist->length = count;
  25406. + pagelist->type = type;
  25407. + pagelist->offset = offset;
  25408. +
  25409. + /* Group the pages into runs of contiguous pages */
  25410. +
  25411. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  25412. + next_addr = base_addr + PAGE_SIZE;
  25413. + addridx = 0;
  25414. + run = 0;
  25415. +
  25416. + for (i = 1; i < num_pages; i++) {
  25417. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  25418. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  25419. + next_addr += PAGE_SIZE;
  25420. + run++;
  25421. + } else {
  25422. + addrs[addridx] = (unsigned long)base_addr + run;
  25423. + addridx++;
  25424. + base_addr = addr;
  25425. + next_addr = addr + PAGE_SIZE;
  25426. + run = 0;
  25427. + }
  25428. + }
  25429. +
  25430. + addrs[addridx] = (unsigned long)base_addr + run;
  25431. + addridx++;
  25432. +
  25433. + /* Partial cache lines (fragments) require special measures */
  25434. + if ((type == PAGELIST_READ) &&
  25435. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  25436. + ((pagelist->offset + pagelist->length) &
  25437. + (CACHE_LINE_SIZE - 1)))) {
  25438. + FRAGMENTS_T *fragments;
  25439. +
  25440. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  25441. + kfree(pagelist);
  25442. + return -EINTR;
  25443. + }
  25444. +
  25445. + WARN_ON(g_free_fragments == NULL);
  25446. +
  25447. + down(&g_free_fragments_mutex);
  25448. + fragments = (FRAGMENTS_T *) g_free_fragments;
  25449. + WARN_ON(fragments == NULL);
  25450. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  25451. + up(&g_free_fragments_mutex);
  25452. + pagelist->type =
  25453. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  25454. + g_fragments_base);
  25455. + }
  25456. +
  25457. + for (page = virt_to_page(pagelist);
  25458. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  25459. + flush_dcache_page(page);
  25460. + }
  25461. +
  25462. + *ppagelist = pagelist;
  25463. +
  25464. + return 0;
  25465. +}
  25466. +
  25467. +static void
  25468. +free_pagelist(PAGELIST_T *pagelist, int actual)
  25469. +{
  25470. + unsigned long *need_release;
  25471. + struct page **pages;
  25472. + unsigned int num_pages, i;
  25473. +
  25474. + vchiq_log_trace(vchiq_arm_log_level,
  25475. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  25476. +
  25477. + num_pages =
  25478. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  25479. + PAGE_SIZE;
  25480. +
  25481. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  25482. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  25483. +
  25484. + /* Deal with any partial cache lines (fragments) */
  25485. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  25486. + FRAGMENTS_T *fragments = g_fragments_base +
  25487. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  25488. + int head_bytes, tail_bytes;
  25489. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  25490. + (CACHE_LINE_SIZE - 1);
  25491. + tail_bytes = (pagelist->offset + actual) &
  25492. + (CACHE_LINE_SIZE - 1);
  25493. +
  25494. + if ((actual >= 0) && (head_bytes != 0)) {
  25495. + if (head_bytes > actual)
  25496. + head_bytes = actual;
  25497. +
  25498. + memcpy((char *)page_address(pages[0]) +
  25499. + pagelist->offset,
  25500. + fragments->headbuf,
  25501. + head_bytes);
  25502. + }
  25503. + if ((actual >= 0) && (head_bytes < actual) &&
  25504. + (tail_bytes != 0)) {
  25505. + memcpy((char *)page_address(pages[num_pages - 1]) +
  25506. + ((pagelist->offset + actual) &
  25507. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  25508. + fragments->tailbuf, tail_bytes);
  25509. + }
  25510. +
  25511. + down(&g_free_fragments_mutex);
  25512. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  25513. + g_free_fragments = fragments;
  25514. + up(&g_free_fragments_mutex);
  25515. + up(&g_free_fragments_sema);
  25516. + }
  25517. +
  25518. + if (*need_release) {
  25519. + for (i = 0; i < num_pages; i++) {
  25520. + if (pagelist->type != PAGELIST_WRITE)
  25521. + set_page_dirty(pages[i]);
  25522. +
  25523. + page_cache_release(pages[i]);
  25524. + }
  25525. + }
  25526. +
  25527. + kfree(pagelist);
  25528. +}
  25529. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  25530. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 1969-12-31 18:00:00.000000000 -0600
  25531. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 2014-12-11 14:02:53.548418001 -0600
  25532. @@ -0,0 +1,42 @@
  25533. +/**
  25534. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25535. + *
  25536. + * Redistribution and use in source and binary forms, with or without
  25537. + * modification, are permitted provided that the following conditions
  25538. + * are met:
  25539. + * 1. Redistributions of source code must retain the above copyright
  25540. + * notice, this list of conditions, and the following disclaimer,
  25541. + * without modification.
  25542. + * 2. Redistributions in binary form must reproduce the above copyright
  25543. + * notice, this list of conditions and the following disclaimer in the
  25544. + * documentation and/or other materials provided with the distribution.
  25545. + * 3. The names of the above-listed copyright holders may not be used
  25546. + * to endorse or promote products derived from this software without
  25547. + * specific prior written permission.
  25548. + *
  25549. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25550. + * GNU General Public License ("GPL") version 2, as published by the Free
  25551. + * Software Foundation.
  25552. + *
  25553. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25554. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25555. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25556. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25557. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25558. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25559. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25560. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25561. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25562. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25563. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25564. + */
  25565. +
  25566. +#ifndef VCHIQ_2835_H
  25567. +#define VCHIQ_2835_H
  25568. +
  25569. +#include "vchiq_pagelist.h"
  25570. +
  25571. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  25572. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  25573. +
  25574. +#endif /* VCHIQ_2835_H */
  25575. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  25576. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 1969-12-31 18:00:00.000000000 -0600
  25577. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 2014-12-11 14:05:38.164418001 -0600
  25578. @@ -0,0 +1,2884 @@
  25579. +/**
  25580. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  25581. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25582. + *
  25583. + * Redistribution and use in source and binary forms, with or without
  25584. + * modification, are permitted provided that the following conditions
  25585. + * are met:
  25586. + * 1. Redistributions of source code must retain the above copyright
  25587. + * notice, this list of conditions, and the following disclaimer,
  25588. + * without modification.
  25589. + * 2. Redistributions in binary form must reproduce the above copyright
  25590. + * notice, this list of conditions and the following disclaimer in the
  25591. + * documentation and/or other materials provided with the distribution.
  25592. + * 3. The names of the above-listed copyright holders may not be used
  25593. + * to endorse or promote products derived from this software without
  25594. + * specific prior written permission.
  25595. + *
  25596. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25597. + * GNU General Public License ("GPL") version 2, as published by the Free
  25598. + * Software Foundation.
  25599. + *
  25600. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25601. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25602. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25603. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25604. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25605. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25606. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25607. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25608. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25609. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25610. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25611. + */
  25612. +
  25613. +#include <linux/kernel.h>
  25614. +#include <linux/module.h>
  25615. +#include <linux/types.h>
  25616. +#include <linux/errno.h>
  25617. +#include <linux/cdev.h>
  25618. +#include <linux/fs.h>
  25619. +#include <linux/device.h>
  25620. +#include <linux/mm.h>
  25621. +#include <linux/highmem.h>
  25622. +#include <linux/pagemap.h>
  25623. +#include <linux/bug.h>
  25624. +#include <linux/semaphore.h>
  25625. +#include <linux/list.h>
  25626. +
  25627. +#include "vchiq_core.h"
  25628. +#include "vchiq_ioctl.h"
  25629. +#include "vchiq_arm.h"
  25630. +#include "vchiq_debugfs.h"
  25631. +#include "vchiq_killable.h"
  25632. +
  25633. +#define DEVICE_NAME "vchiq"
  25634. +
  25635. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  25636. +#undef MODULE_PARAM_PREFIX
  25637. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  25638. +
  25639. +#define VCHIQ_MINOR 0
  25640. +
  25641. +/* Some per-instance constants */
  25642. +#define MAX_COMPLETIONS 16
  25643. +#define MAX_SERVICES 64
  25644. +#define MAX_ELEMENTS 8
  25645. +#define MSG_QUEUE_SIZE 64
  25646. +
  25647. +#define KEEPALIVE_VER 1
  25648. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  25649. +
  25650. +/* Run time control of log level, based on KERN_XXX level. */
  25651. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  25652. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  25653. +
  25654. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  25655. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  25656. +
  25657. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  25658. +static const char *const suspend_state_names[] = {
  25659. + "VC_SUSPEND_FORCE_CANCELED",
  25660. + "VC_SUSPEND_REJECTED",
  25661. + "VC_SUSPEND_FAILED",
  25662. + "VC_SUSPEND_IDLE",
  25663. + "VC_SUSPEND_REQUESTED",
  25664. + "VC_SUSPEND_IN_PROGRESS",
  25665. + "VC_SUSPEND_SUSPENDED"
  25666. +};
  25667. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  25668. +static const char *const resume_state_names[] = {
  25669. + "VC_RESUME_FAILED",
  25670. + "VC_RESUME_IDLE",
  25671. + "VC_RESUME_REQUESTED",
  25672. + "VC_RESUME_IN_PROGRESS",
  25673. + "VC_RESUME_RESUMED"
  25674. +};
  25675. +/* The number of times we allow force suspend to timeout before actually
  25676. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  25677. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  25678. +*/
  25679. +#define FORCE_SUSPEND_FAIL_MAX 8
  25680. +
  25681. +/* The time in ms allowed for videocore to go idle when force suspend has been
  25682. + * requested */
  25683. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  25684. +
  25685. +
  25686. +static void suspend_timer_callback(unsigned long context);
  25687. +
  25688. +
  25689. +typedef struct user_service_struct {
  25690. + VCHIQ_SERVICE_T *service;
  25691. + void *userdata;
  25692. + VCHIQ_INSTANCE_T instance;
  25693. + char is_vchi;
  25694. + char dequeue_pending;
  25695. + char close_pending;
  25696. + int message_available_pos;
  25697. + int msg_insert;
  25698. + int msg_remove;
  25699. + struct semaphore insert_event;
  25700. + struct semaphore remove_event;
  25701. + struct semaphore close_event;
  25702. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  25703. +} USER_SERVICE_T;
  25704. +
  25705. +struct bulk_waiter_node {
  25706. + struct bulk_waiter bulk_waiter;
  25707. + int pid;
  25708. + struct list_head list;
  25709. +};
  25710. +
  25711. +struct vchiq_instance_struct {
  25712. + VCHIQ_STATE_T *state;
  25713. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  25714. + int completion_insert;
  25715. + int completion_remove;
  25716. + struct semaphore insert_event;
  25717. + struct semaphore remove_event;
  25718. + struct mutex completion_mutex;
  25719. +
  25720. + int connected;
  25721. + int closing;
  25722. + int pid;
  25723. + int mark;
  25724. + int use_close_delivered;
  25725. + int trace;
  25726. +
  25727. + struct list_head bulk_waiter_list;
  25728. + struct mutex bulk_waiter_list_mutex;
  25729. +
  25730. + VCHIQ_DEBUGFS_NODE_T debugfs_node;
  25731. +};
  25732. +
  25733. +typedef struct dump_context_struct {
  25734. + char __user *buf;
  25735. + size_t actual;
  25736. + size_t space;
  25737. + loff_t offset;
  25738. +} DUMP_CONTEXT_T;
  25739. +
  25740. +static struct cdev vchiq_cdev;
  25741. +static dev_t vchiq_devid;
  25742. +static VCHIQ_STATE_T g_state;
  25743. +static struct class *vchiq_class;
  25744. +static struct device *vchiq_dev;
  25745. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  25746. +
  25747. +static const char *const ioctl_names[] = {
  25748. + "CONNECT",
  25749. + "SHUTDOWN",
  25750. + "CREATE_SERVICE",
  25751. + "REMOVE_SERVICE",
  25752. + "QUEUE_MESSAGE",
  25753. + "QUEUE_BULK_TRANSMIT",
  25754. + "QUEUE_BULK_RECEIVE",
  25755. + "AWAIT_COMPLETION",
  25756. + "DEQUEUE_MESSAGE",
  25757. + "GET_CLIENT_ID",
  25758. + "GET_CONFIG",
  25759. + "CLOSE_SERVICE",
  25760. + "USE_SERVICE",
  25761. + "RELEASE_SERVICE",
  25762. + "SET_SERVICE_OPTION",
  25763. + "DUMP_PHYS_MEM",
  25764. + "LIB_VERSION",
  25765. + "CLOSE_DELIVERED"
  25766. +};
  25767. +
  25768. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  25769. + (VCHIQ_IOC_MAX + 1));
  25770. +
  25771. +static void
  25772. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  25773. +
  25774. +/****************************************************************************
  25775. +*
  25776. +* add_completion
  25777. +*
  25778. +***************************************************************************/
  25779. +
  25780. +static VCHIQ_STATUS_T
  25781. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  25782. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  25783. + void *bulk_userdata)
  25784. +{
  25785. + VCHIQ_COMPLETION_DATA_T *completion;
  25786. + DEBUG_INITIALISE(g_state.local)
  25787. +
  25788. + while (instance->completion_insert ==
  25789. + (instance->completion_remove + MAX_COMPLETIONS)) {
  25790. + /* Out of space - wait for the client */
  25791. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25792. + vchiq_log_trace(vchiq_arm_log_level,
  25793. + "add_completion - completion queue full");
  25794. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  25795. + if (down_interruptible(&instance->remove_event) != 0) {
  25796. + vchiq_log_info(vchiq_arm_log_level,
  25797. + "service_callback interrupted");
  25798. + return VCHIQ_RETRY;
  25799. + } else if (instance->closing) {
  25800. + vchiq_log_info(vchiq_arm_log_level,
  25801. + "service_callback closing");
  25802. + return VCHIQ_ERROR;
  25803. + }
  25804. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25805. + }
  25806. +
  25807. + completion =
  25808. + &instance->completions[instance->completion_insert &
  25809. + (MAX_COMPLETIONS - 1)];
  25810. +
  25811. + completion->header = header;
  25812. + completion->reason = reason;
  25813. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  25814. + completion->service_userdata = user_service->service;
  25815. + completion->bulk_userdata = bulk_userdata;
  25816. +
  25817. + if (reason == VCHIQ_SERVICE_CLOSED) {
  25818. + /* Take an extra reference, to be held until
  25819. + this CLOSED notification is delivered. */
  25820. + lock_service(user_service->service);
  25821. + if (instance->use_close_delivered)
  25822. + user_service->close_pending = 1;
  25823. + }
  25824. +
  25825. + /* A write barrier is needed here to ensure that the entire completion
  25826. + record is written out before the insert point. */
  25827. + wmb();
  25828. +
  25829. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  25830. + user_service->message_available_pos =
  25831. + instance->completion_insert;
  25832. + instance->completion_insert++;
  25833. +
  25834. + up(&instance->insert_event);
  25835. +
  25836. + return VCHIQ_SUCCESS;
  25837. +}
  25838. +
  25839. +/****************************************************************************
  25840. +*
  25841. +* service_callback
  25842. +*
  25843. +***************************************************************************/
  25844. +
  25845. +static VCHIQ_STATUS_T
  25846. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  25847. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  25848. +{
  25849. + /* How do we ensure the callback goes to the right client?
  25850. + ** The service_user data points to a USER_SERVICE_T record containing
  25851. + ** the original callback and the user state structure, which contains a
  25852. + ** circular buffer for completion records.
  25853. + */
  25854. + USER_SERVICE_T *user_service;
  25855. + VCHIQ_SERVICE_T *service;
  25856. + VCHIQ_INSTANCE_T instance;
  25857. + DEBUG_INITIALISE(g_state.local)
  25858. +
  25859. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25860. +
  25861. + service = handle_to_service(handle);
  25862. + BUG_ON(!service);
  25863. + user_service = (USER_SERVICE_T *)service->base.userdata;
  25864. + instance = user_service->instance;
  25865. +
  25866. + if (!instance || instance->closing)
  25867. + return VCHIQ_SUCCESS;
  25868. +
  25869. + vchiq_log_trace(vchiq_arm_log_level,
  25870. + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
  25871. + "instance %lx, bulk_userdata %lx",
  25872. + (unsigned long)user_service,
  25873. + service->localport, user_service->userdata,
  25874. + reason, (unsigned long)header,
  25875. + (unsigned long)instance, (unsigned long)bulk_userdata);
  25876. +
  25877. + if (header && user_service->is_vchi) {
  25878. + spin_lock(&msg_queue_spinlock);
  25879. + while (user_service->msg_insert ==
  25880. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  25881. + spin_unlock(&msg_queue_spinlock);
  25882. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25883. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  25884. + vchiq_log_trace(vchiq_arm_log_level,
  25885. + "service_callback - msg queue full");
  25886. + /* If there is no MESSAGE_AVAILABLE in the completion
  25887. + ** queue, add one
  25888. + */
  25889. + if ((user_service->message_available_pos -
  25890. + instance->completion_remove) < 0) {
  25891. + VCHIQ_STATUS_T status;
  25892. + vchiq_log_info(vchiq_arm_log_level,
  25893. + "Inserting extra MESSAGE_AVAILABLE");
  25894. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25895. + status = add_completion(instance, reason,
  25896. + NULL, user_service, bulk_userdata);
  25897. + if (status != VCHIQ_SUCCESS) {
  25898. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25899. + return status;
  25900. + }
  25901. + }
  25902. +
  25903. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25904. + if (down_interruptible(&user_service->remove_event)
  25905. + != 0) {
  25906. + vchiq_log_info(vchiq_arm_log_level,
  25907. + "service_callback interrupted");
  25908. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25909. + return VCHIQ_RETRY;
  25910. + } else if (instance->closing) {
  25911. + vchiq_log_info(vchiq_arm_log_level,
  25912. + "service_callback closing");
  25913. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25914. + return VCHIQ_ERROR;
  25915. + }
  25916. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25917. + spin_lock(&msg_queue_spinlock);
  25918. + }
  25919. +
  25920. + user_service->msg_queue[user_service->msg_insert &
  25921. + (MSG_QUEUE_SIZE - 1)] = header;
  25922. + user_service->msg_insert++;
  25923. + spin_unlock(&msg_queue_spinlock);
  25924. +
  25925. + up(&user_service->insert_event);
  25926. +
  25927. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  25928. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  25929. + ** bypass the completion queue.
  25930. + */
  25931. + if (((user_service->message_available_pos -
  25932. + instance->completion_remove) >= 0) ||
  25933. + user_service->dequeue_pending) {
  25934. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25935. + user_service->dequeue_pending = 0;
  25936. + return VCHIQ_SUCCESS;
  25937. + }
  25938. +
  25939. + header = NULL;
  25940. + }
  25941. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25942. +
  25943. + return add_completion(instance, reason, header, user_service,
  25944. + bulk_userdata);
  25945. +}
  25946. +
  25947. +/****************************************************************************
  25948. +*
  25949. +* user_service_free
  25950. +*
  25951. +***************************************************************************/
  25952. +static void
  25953. +user_service_free(void *userdata)
  25954. +{
  25955. + kfree(userdata);
  25956. +}
  25957. +
  25958. +/****************************************************************************
  25959. +*
  25960. +* close_delivered
  25961. +*
  25962. +***************************************************************************/
  25963. +static void close_delivered(USER_SERVICE_T *user_service)
  25964. +{
  25965. + vchiq_log_info(vchiq_arm_log_level,
  25966. + "close_delivered(handle=%x)",
  25967. + user_service->service->handle);
  25968. +
  25969. + if (user_service->close_pending) {
  25970. + /* Allow the underlying service to be culled */
  25971. + unlock_service(user_service->service);
  25972. +
  25973. + /* Wake the user-thread blocked in close_ or remove_service */
  25974. + up(&user_service->close_event);
  25975. +
  25976. + user_service->close_pending = 0;
  25977. + }
  25978. +}
  25979. +
  25980. +/****************************************************************************
  25981. +*
  25982. +* vchiq_ioctl
  25983. +*
  25984. +***************************************************************************/
  25985. +static long
  25986. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  25987. +{
  25988. + VCHIQ_INSTANCE_T instance = file->private_data;
  25989. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25990. + VCHIQ_SERVICE_T *service = NULL;
  25991. + long ret = 0;
  25992. + int i, rc;
  25993. + DEBUG_INITIALISE(g_state.local)
  25994. +
  25995. + vchiq_log_trace(vchiq_arm_log_level,
  25996. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  25997. + (unsigned int)instance,
  25998. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  25999. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  26000. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  26001. +
  26002. + switch (cmd) {
  26003. + case VCHIQ_IOC_SHUTDOWN:
  26004. + if (!instance->connected)
  26005. + break;
  26006. +
  26007. + /* Remove all services */
  26008. + i = 0;
  26009. + while ((service = next_service_by_instance(instance->state,
  26010. + instance, &i)) != NULL) {
  26011. + status = vchiq_remove_service(service->handle);
  26012. + unlock_service(service);
  26013. + if (status != VCHIQ_SUCCESS)
  26014. + break;
  26015. + }
  26016. + service = NULL;
  26017. +
  26018. + if (status == VCHIQ_SUCCESS) {
  26019. + /* Wake the completion thread and ask it to exit */
  26020. + instance->closing = 1;
  26021. + up(&instance->insert_event);
  26022. + }
  26023. +
  26024. + break;
  26025. +
  26026. + case VCHIQ_IOC_CONNECT:
  26027. + if (instance->connected) {
  26028. + ret = -EINVAL;
  26029. + break;
  26030. + }
  26031. + rc = mutex_lock_interruptible(&instance->state->mutex);
  26032. + if (rc != 0) {
  26033. + vchiq_log_error(vchiq_arm_log_level,
  26034. + "vchiq: connect: could not lock mutex for "
  26035. + "state %d: %d",
  26036. + instance->state->id, rc);
  26037. + ret = -EINTR;
  26038. + break;
  26039. + }
  26040. + status = vchiq_connect_internal(instance->state, instance);
  26041. + mutex_unlock(&instance->state->mutex);
  26042. +
  26043. + if (status == VCHIQ_SUCCESS)
  26044. + instance->connected = 1;
  26045. + else
  26046. + vchiq_log_error(vchiq_arm_log_level,
  26047. + "vchiq: could not connect: %d", status);
  26048. + break;
  26049. +
  26050. + case VCHIQ_IOC_CREATE_SERVICE: {
  26051. + VCHIQ_CREATE_SERVICE_T args;
  26052. + USER_SERVICE_T *user_service = NULL;
  26053. + void *userdata;
  26054. + int srvstate;
  26055. +
  26056. + if (copy_from_user
  26057. + (&args, (const void __user *)arg,
  26058. + sizeof(args)) != 0) {
  26059. + ret = -EFAULT;
  26060. + break;
  26061. + }
  26062. +
  26063. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  26064. + if (!user_service) {
  26065. + ret = -ENOMEM;
  26066. + break;
  26067. + }
  26068. +
  26069. + if (args.is_open) {
  26070. + if (!instance->connected) {
  26071. + ret = -ENOTCONN;
  26072. + kfree(user_service);
  26073. + break;
  26074. + }
  26075. + srvstate = VCHIQ_SRVSTATE_OPENING;
  26076. + } else {
  26077. + srvstate =
  26078. + instance->connected ?
  26079. + VCHIQ_SRVSTATE_LISTENING :
  26080. + VCHIQ_SRVSTATE_HIDDEN;
  26081. + }
  26082. +
  26083. + userdata = args.params.userdata;
  26084. + args.params.callback = service_callback;
  26085. + args.params.userdata = user_service;
  26086. + service = vchiq_add_service_internal(
  26087. + instance->state,
  26088. + &args.params, srvstate,
  26089. + instance, user_service_free);
  26090. +
  26091. + if (service != NULL) {
  26092. + user_service->service = service;
  26093. + user_service->userdata = userdata;
  26094. + user_service->instance = instance;
  26095. + user_service->is_vchi = (args.is_vchi != 0);
  26096. + user_service->dequeue_pending = 0;
  26097. + user_service->close_pending = 0;
  26098. + user_service->message_available_pos =
  26099. + instance->completion_remove - 1;
  26100. + user_service->msg_insert = 0;
  26101. + user_service->msg_remove = 0;
  26102. + sema_init(&user_service->insert_event, 0);
  26103. + sema_init(&user_service->remove_event, 0);
  26104. + sema_init(&user_service->close_event, 0);
  26105. +
  26106. + if (args.is_open) {
  26107. + status = vchiq_open_service_internal
  26108. + (service, instance->pid);
  26109. + if (status != VCHIQ_SUCCESS) {
  26110. + vchiq_remove_service(service->handle);
  26111. + service = NULL;
  26112. + ret = (status == VCHIQ_RETRY) ?
  26113. + -EINTR : -EIO;
  26114. + break;
  26115. + }
  26116. + }
  26117. +
  26118. + if (copy_to_user((void __user *)
  26119. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  26120. + arg)->handle),
  26121. + (const void *)&service->handle,
  26122. + sizeof(service->handle)) != 0) {
  26123. + ret = -EFAULT;
  26124. + vchiq_remove_service(service->handle);
  26125. + }
  26126. +
  26127. + service = NULL;
  26128. + } else {
  26129. + ret = -EEXIST;
  26130. + kfree(user_service);
  26131. + }
  26132. + } break;
  26133. +
  26134. + case VCHIQ_IOC_CLOSE_SERVICE: {
  26135. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26136. +
  26137. + service = find_service_for_instance(instance, handle);
  26138. + if (service != NULL) {
  26139. + USER_SERVICE_T *user_service =
  26140. + (USER_SERVICE_T *)service->base.userdata;
  26141. + /* close_pending is false on first entry, and when the
  26142. + wait in vchiq_close_service has been interrupted. */
  26143. + if (!user_service->close_pending) {
  26144. + status = vchiq_close_service(service->handle);
  26145. + if (status != VCHIQ_SUCCESS)
  26146. + break;
  26147. + }
  26148. +
  26149. + /* close_pending is true once the underlying service
  26150. + has been closed until the client library calls the
  26151. + CLOSE_DELIVERED ioctl, signalling close_event. */
  26152. + if (user_service->close_pending &&
  26153. + down_interruptible(&user_service->close_event))
  26154. + status = VCHIQ_RETRY;
  26155. + }
  26156. + else
  26157. + ret = -EINVAL;
  26158. + } break;
  26159. +
  26160. + case VCHIQ_IOC_REMOVE_SERVICE: {
  26161. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26162. +
  26163. + service = find_service_for_instance(instance, handle);
  26164. + if (service != NULL) {
  26165. + USER_SERVICE_T *user_service =
  26166. + (USER_SERVICE_T *)service->base.userdata;
  26167. + /* close_pending is false on first entry, and when the
  26168. + wait in vchiq_close_service has been interrupted. */
  26169. + if (!user_service->close_pending) {
  26170. + status = vchiq_remove_service(service->handle);
  26171. + if (status != VCHIQ_SUCCESS)
  26172. + break;
  26173. + }
  26174. +
  26175. + /* close_pending is true once the underlying service
  26176. + has been closed until the client library calls the
  26177. + CLOSE_DELIVERED ioctl, signalling close_event. */
  26178. + if (user_service->close_pending &&
  26179. + down_interruptible(&user_service->close_event))
  26180. + status = VCHIQ_RETRY;
  26181. + }
  26182. + else
  26183. + ret = -EINVAL;
  26184. + } break;
  26185. +
  26186. + case VCHIQ_IOC_USE_SERVICE:
  26187. + case VCHIQ_IOC_RELEASE_SERVICE: {
  26188. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26189. +
  26190. + service = find_service_for_instance(instance, handle);
  26191. + if (service != NULL) {
  26192. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  26193. + vchiq_use_service_internal(service) :
  26194. + vchiq_release_service_internal(service);
  26195. + if (status != VCHIQ_SUCCESS) {
  26196. + vchiq_log_error(vchiq_susp_log_level,
  26197. + "%s: cmd %s returned error %d for "
  26198. + "service %c%c%c%c:%03d",
  26199. + __func__,
  26200. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  26201. + "VCHIQ_IOC_USE_SERVICE" :
  26202. + "VCHIQ_IOC_RELEASE_SERVICE",
  26203. + status,
  26204. + VCHIQ_FOURCC_AS_4CHARS(
  26205. + service->base.fourcc),
  26206. + service->client_id);
  26207. + ret = -EINVAL;
  26208. + }
  26209. + } else
  26210. + ret = -EINVAL;
  26211. + } break;
  26212. +
  26213. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  26214. + VCHIQ_QUEUE_MESSAGE_T args;
  26215. + if (copy_from_user
  26216. + (&args, (const void __user *)arg,
  26217. + sizeof(args)) != 0) {
  26218. + ret = -EFAULT;
  26219. + break;
  26220. + }
  26221. +
  26222. + service = find_service_for_instance(instance, args.handle);
  26223. +
  26224. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  26225. + /* Copy elements into kernel space */
  26226. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  26227. + if (copy_from_user(elements, args.elements,
  26228. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  26229. + status = vchiq_queue_message
  26230. + (args.handle,
  26231. + elements, args.count);
  26232. + else
  26233. + ret = -EFAULT;
  26234. + } else {
  26235. + ret = -EINVAL;
  26236. + }
  26237. + } break;
  26238. +
  26239. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  26240. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  26241. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  26242. + struct bulk_waiter_node *waiter = NULL;
  26243. + VCHIQ_BULK_DIR_T dir =
  26244. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  26245. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  26246. +
  26247. + if (copy_from_user
  26248. + (&args, (const void __user *)arg,
  26249. + sizeof(args)) != 0) {
  26250. + ret = -EFAULT;
  26251. + break;
  26252. + }
  26253. +
  26254. + service = find_service_for_instance(instance, args.handle);
  26255. + if (!service) {
  26256. + ret = -EINVAL;
  26257. + break;
  26258. + }
  26259. +
  26260. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  26261. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  26262. + GFP_KERNEL);
  26263. + if (!waiter) {
  26264. + ret = -ENOMEM;
  26265. + break;
  26266. + }
  26267. + args.userdata = &waiter->bulk_waiter;
  26268. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  26269. + struct list_head *pos;
  26270. + mutex_lock(&instance->bulk_waiter_list_mutex);
  26271. + list_for_each(pos, &instance->bulk_waiter_list) {
  26272. + if (list_entry(pos, struct bulk_waiter_node,
  26273. + list)->pid == current->pid) {
  26274. + waiter = list_entry(pos,
  26275. + struct bulk_waiter_node,
  26276. + list);
  26277. + list_del(pos);
  26278. + break;
  26279. + }
  26280. +
  26281. + }
  26282. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  26283. + if (!waiter) {
  26284. + vchiq_log_error(vchiq_arm_log_level,
  26285. + "no bulk_waiter found for pid %d",
  26286. + current->pid);
  26287. + ret = -ESRCH;
  26288. + break;
  26289. + }
  26290. + vchiq_log_info(vchiq_arm_log_level,
  26291. + "found bulk_waiter %x for pid %d",
  26292. + (unsigned int)waiter, current->pid);
  26293. + args.userdata = &waiter->bulk_waiter;
  26294. + }
  26295. + status = vchiq_bulk_transfer
  26296. + (args.handle,
  26297. + VCHI_MEM_HANDLE_INVALID,
  26298. + args.data, args.size,
  26299. + args.userdata, args.mode,
  26300. + dir);
  26301. + if (!waiter)
  26302. + break;
  26303. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  26304. + !waiter->bulk_waiter.bulk) {
  26305. + if (waiter->bulk_waiter.bulk) {
  26306. + /* Cancel the signal when the transfer
  26307. + ** completes. */
  26308. + spin_lock(&bulk_waiter_spinlock);
  26309. + waiter->bulk_waiter.bulk->userdata = NULL;
  26310. + spin_unlock(&bulk_waiter_spinlock);
  26311. + }
  26312. + kfree(waiter);
  26313. + } else {
  26314. + const VCHIQ_BULK_MODE_T mode_waiting =
  26315. + VCHIQ_BULK_MODE_WAITING;
  26316. + waiter->pid = current->pid;
  26317. + mutex_lock(&instance->bulk_waiter_list_mutex);
  26318. + list_add(&waiter->list, &instance->bulk_waiter_list);
  26319. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  26320. + vchiq_log_info(vchiq_arm_log_level,
  26321. + "saved bulk_waiter %x for pid %d",
  26322. + (unsigned int)waiter, current->pid);
  26323. +
  26324. + if (copy_to_user((void __user *)
  26325. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  26326. + arg)->mode),
  26327. + (const void *)&mode_waiting,
  26328. + sizeof(mode_waiting)) != 0)
  26329. + ret = -EFAULT;
  26330. + }
  26331. + } break;
  26332. +
  26333. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  26334. + VCHIQ_AWAIT_COMPLETION_T args;
  26335. +
  26336. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  26337. + if (!instance->connected) {
  26338. + ret = -ENOTCONN;
  26339. + break;
  26340. + }
  26341. +
  26342. + if (copy_from_user(&args, (const void __user *)arg,
  26343. + sizeof(args)) != 0) {
  26344. + ret = -EFAULT;
  26345. + break;
  26346. + }
  26347. +
  26348. + mutex_lock(&instance->completion_mutex);
  26349. +
  26350. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  26351. + while ((instance->completion_remove ==
  26352. + instance->completion_insert)
  26353. + && !instance->closing) {
  26354. + int rc;
  26355. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  26356. + mutex_unlock(&instance->completion_mutex);
  26357. + rc = down_interruptible(&instance->insert_event);
  26358. + mutex_lock(&instance->completion_mutex);
  26359. + if (rc != 0) {
  26360. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  26361. + vchiq_log_info(vchiq_arm_log_level,
  26362. + "AWAIT_COMPLETION interrupted");
  26363. + ret = -EINTR;
  26364. + break;
  26365. + }
  26366. + }
  26367. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  26368. +
  26369. + /* A read memory barrier is needed to stop prefetch of a stale
  26370. + ** completion record
  26371. + */
  26372. + rmb();
  26373. +
  26374. + if (ret == 0) {
  26375. + int msgbufcount = args.msgbufcount;
  26376. + for (ret = 0; ret < args.count; ret++) {
  26377. + VCHIQ_COMPLETION_DATA_T *completion;
  26378. + VCHIQ_SERVICE_T *service;
  26379. + USER_SERVICE_T *user_service;
  26380. + VCHIQ_HEADER_T *header;
  26381. + if (instance->completion_remove ==
  26382. + instance->completion_insert)
  26383. + break;
  26384. + completion = &instance->completions[
  26385. + instance->completion_remove &
  26386. + (MAX_COMPLETIONS - 1)];
  26387. +
  26388. + service = completion->service_userdata;
  26389. + user_service = service->base.userdata;
  26390. + completion->service_userdata =
  26391. + user_service->userdata;
  26392. +
  26393. + header = completion->header;
  26394. + if (header) {
  26395. + void __user *msgbuf;
  26396. + int msglen;
  26397. +
  26398. + msglen = header->size +
  26399. + sizeof(VCHIQ_HEADER_T);
  26400. + /* This must be a VCHIQ-style service */
  26401. + if (args.msgbufsize < msglen) {
  26402. + vchiq_log_error(
  26403. + vchiq_arm_log_level,
  26404. + "header %x: msgbufsize"
  26405. + " %x < msglen %x",
  26406. + (unsigned int)header,
  26407. + args.msgbufsize,
  26408. + msglen);
  26409. + WARN(1, "invalid message "
  26410. + "size\n");
  26411. + if (ret == 0)
  26412. + ret = -EMSGSIZE;
  26413. + break;
  26414. + }
  26415. + if (msgbufcount <= 0)
  26416. + /* Stall here for lack of a
  26417. + ** buffer for the message. */
  26418. + break;
  26419. + /* Get the pointer from user space */
  26420. + msgbufcount--;
  26421. + if (copy_from_user(&msgbuf,
  26422. + (const void __user *)
  26423. + &args.msgbufs[msgbufcount],
  26424. + sizeof(msgbuf)) != 0) {
  26425. + if (ret == 0)
  26426. + ret = -EFAULT;
  26427. + break;
  26428. + }
  26429. +
  26430. + /* Copy the message to user space */
  26431. + if (copy_to_user(msgbuf, header,
  26432. + msglen) != 0) {
  26433. + if (ret == 0)
  26434. + ret = -EFAULT;
  26435. + break;
  26436. + }
  26437. +
  26438. + /* Now it has been copied, the message
  26439. + ** can be released. */
  26440. + vchiq_release_message(service->handle,
  26441. + header);
  26442. +
  26443. + /* The completion must point to the
  26444. + ** msgbuf. */
  26445. + completion->header = msgbuf;
  26446. + }
  26447. +
  26448. + if ((completion->reason ==
  26449. + VCHIQ_SERVICE_CLOSED) &&
  26450. + !instance->use_close_delivered)
  26451. + unlock_service(service);
  26452. +
  26453. + if (copy_to_user((void __user *)(
  26454. + (size_t)args.buf +
  26455. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  26456. + completion,
  26457. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  26458. + if (ret == 0)
  26459. + ret = -EFAULT;
  26460. + break;
  26461. + }
  26462. +
  26463. + instance->completion_remove++;
  26464. + }
  26465. +
  26466. + if (msgbufcount != args.msgbufcount) {
  26467. + if (copy_to_user((void __user *)
  26468. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  26469. + msgbufcount,
  26470. + &msgbufcount,
  26471. + sizeof(msgbufcount)) != 0) {
  26472. + ret = -EFAULT;
  26473. + }
  26474. + }
  26475. + }
  26476. +
  26477. + if (ret != 0)
  26478. + up(&instance->remove_event);
  26479. + mutex_unlock(&instance->completion_mutex);
  26480. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  26481. + } break;
  26482. +
  26483. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  26484. + VCHIQ_DEQUEUE_MESSAGE_T args;
  26485. + USER_SERVICE_T *user_service;
  26486. + VCHIQ_HEADER_T *header;
  26487. +
  26488. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  26489. + if (copy_from_user
  26490. + (&args, (const void __user *)arg,
  26491. + sizeof(args)) != 0) {
  26492. + ret = -EFAULT;
  26493. + break;
  26494. + }
  26495. + service = find_service_for_instance(instance, args.handle);
  26496. + if (!service) {
  26497. + ret = -EINVAL;
  26498. + break;
  26499. + }
  26500. + user_service = (USER_SERVICE_T *)service->base.userdata;
  26501. + if (user_service->is_vchi == 0) {
  26502. + ret = -EINVAL;
  26503. + break;
  26504. + }
  26505. +
  26506. + spin_lock(&msg_queue_spinlock);
  26507. + if (user_service->msg_remove == user_service->msg_insert) {
  26508. + if (!args.blocking) {
  26509. + spin_unlock(&msg_queue_spinlock);
  26510. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  26511. + ret = -EWOULDBLOCK;
  26512. + break;
  26513. + }
  26514. + user_service->dequeue_pending = 1;
  26515. + do {
  26516. + spin_unlock(&msg_queue_spinlock);
  26517. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  26518. + if (down_interruptible(
  26519. + &user_service->insert_event) != 0) {
  26520. + vchiq_log_info(vchiq_arm_log_level,
  26521. + "DEQUEUE_MESSAGE interrupted");
  26522. + ret = -EINTR;
  26523. + break;
  26524. + }
  26525. + spin_lock(&msg_queue_spinlock);
  26526. + } while (user_service->msg_remove ==
  26527. + user_service->msg_insert);
  26528. +
  26529. + if (ret)
  26530. + break;
  26531. + }
  26532. +
  26533. + BUG_ON((int)(user_service->msg_insert -
  26534. + user_service->msg_remove) < 0);
  26535. +
  26536. + header = user_service->msg_queue[user_service->msg_remove &
  26537. + (MSG_QUEUE_SIZE - 1)];
  26538. + user_service->msg_remove++;
  26539. + spin_unlock(&msg_queue_spinlock);
  26540. +
  26541. + up(&user_service->remove_event);
  26542. + if (header == NULL)
  26543. + ret = -ENOTCONN;
  26544. + else if (header->size <= args.bufsize) {
  26545. + /* Copy to user space if msgbuf is not NULL */
  26546. + if ((args.buf == NULL) ||
  26547. + (copy_to_user((void __user *)args.buf,
  26548. + header->data,
  26549. + header->size) == 0)) {
  26550. + ret = header->size;
  26551. + vchiq_release_message(
  26552. + service->handle,
  26553. + header);
  26554. + } else
  26555. + ret = -EFAULT;
  26556. + } else {
  26557. + vchiq_log_error(vchiq_arm_log_level,
  26558. + "header %x: bufsize %x < size %x",
  26559. + (unsigned int)header, args.bufsize,
  26560. + header->size);
  26561. + WARN(1, "invalid size\n");
  26562. + ret = -EMSGSIZE;
  26563. + }
  26564. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  26565. + } break;
  26566. +
  26567. + case VCHIQ_IOC_GET_CLIENT_ID: {
  26568. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26569. +
  26570. + ret = vchiq_get_client_id(handle);
  26571. + } break;
  26572. +
  26573. + case VCHIQ_IOC_GET_CONFIG: {
  26574. + VCHIQ_GET_CONFIG_T args;
  26575. + VCHIQ_CONFIG_T config;
  26576. +
  26577. + if (copy_from_user(&args, (const void __user *)arg,
  26578. + sizeof(args)) != 0) {
  26579. + ret = -EFAULT;
  26580. + break;
  26581. + }
  26582. + if (args.config_size > sizeof(config)) {
  26583. + ret = -EINVAL;
  26584. + break;
  26585. + }
  26586. + status = vchiq_get_config(instance, args.config_size, &config);
  26587. + if (status == VCHIQ_SUCCESS) {
  26588. + if (copy_to_user((void __user *)args.pconfig,
  26589. + &config, args.config_size) != 0) {
  26590. + ret = -EFAULT;
  26591. + break;
  26592. + }
  26593. + }
  26594. + } break;
  26595. +
  26596. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  26597. + VCHIQ_SET_SERVICE_OPTION_T args;
  26598. +
  26599. + if (copy_from_user(
  26600. + &args, (const void __user *)arg,
  26601. + sizeof(args)) != 0) {
  26602. + ret = -EFAULT;
  26603. + break;
  26604. + }
  26605. +
  26606. + service = find_service_for_instance(instance, args.handle);
  26607. + if (!service) {
  26608. + ret = -EINVAL;
  26609. + break;
  26610. + }
  26611. +
  26612. + status = vchiq_set_service_option(
  26613. + args.handle, args.option, args.value);
  26614. + } break;
  26615. +
  26616. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  26617. + VCHIQ_DUMP_MEM_T args;
  26618. +
  26619. + if (copy_from_user
  26620. + (&args, (const void __user *)arg,
  26621. + sizeof(args)) != 0) {
  26622. + ret = -EFAULT;
  26623. + break;
  26624. + }
  26625. + dump_phys_mem(args.virt_addr, args.num_bytes);
  26626. + } break;
  26627. +
  26628. + case VCHIQ_IOC_LIB_VERSION: {
  26629. + unsigned int lib_version = (unsigned int)arg;
  26630. +
  26631. + if (lib_version < VCHIQ_VERSION_MIN)
  26632. + ret = -EINVAL;
  26633. + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
  26634. + instance->use_close_delivered = 1;
  26635. + } break;
  26636. +
  26637. + case VCHIQ_IOC_CLOSE_DELIVERED: {
  26638. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26639. +
  26640. + service = find_closed_service_for_instance(instance, handle);
  26641. + if (service != NULL) {
  26642. + USER_SERVICE_T *user_service =
  26643. + (USER_SERVICE_T *)service->base.userdata;
  26644. + close_delivered(user_service);
  26645. + }
  26646. + else
  26647. + ret = -EINVAL;
  26648. + } break;
  26649. +
  26650. + default:
  26651. + ret = -ENOTTY;
  26652. + break;
  26653. + }
  26654. +
  26655. + if (service)
  26656. + unlock_service(service);
  26657. +
  26658. + if (ret == 0) {
  26659. + if (status == VCHIQ_ERROR)
  26660. + ret = -EIO;
  26661. + else if (status == VCHIQ_RETRY)
  26662. + ret = -EINTR;
  26663. + }
  26664. +
  26665. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  26666. + (ret != -EWOULDBLOCK))
  26667. + vchiq_log_info(vchiq_arm_log_level,
  26668. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  26669. + (unsigned long)instance,
  26670. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  26671. + ioctl_names[_IOC_NR(cmd)] :
  26672. + "<invalid>",
  26673. + status, ret);
  26674. + else
  26675. + vchiq_log_trace(vchiq_arm_log_level,
  26676. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  26677. + (unsigned long)instance,
  26678. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  26679. + ioctl_names[_IOC_NR(cmd)] :
  26680. + "<invalid>",
  26681. + status, ret);
  26682. +
  26683. + return ret;
  26684. +}
  26685. +
  26686. +/****************************************************************************
  26687. +*
  26688. +* vchiq_open
  26689. +*
  26690. +***************************************************************************/
  26691. +
  26692. +static int
  26693. +vchiq_open(struct inode *inode, struct file *file)
  26694. +{
  26695. + int dev = iminor(inode) & 0x0f;
  26696. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  26697. + switch (dev) {
  26698. + case VCHIQ_MINOR: {
  26699. + int ret;
  26700. + VCHIQ_STATE_T *state = vchiq_get_state();
  26701. + VCHIQ_INSTANCE_T instance;
  26702. +
  26703. + if (!state) {
  26704. + vchiq_log_error(vchiq_arm_log_level,
  26705. + "vchiq has no connection to VideoCore");
  26706. + return -ENOTCONN;
  26707. + }
  26708. +
  26709. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  26710. + if (!instance)
  26711. + return -ENOMEM;
  26712. +
  26713. + instance->state = state;
  26714. + instance->pid = current->tgid;
  26715. +
  26716. + ret = vchiq_debugfs_add_instance(instance);
  26717. + if (ret != 0) {
  26718. + kfree(instance);
  26719. + return ret;
  26720. + }
  26721. +
  26722. + sema_init(&instance->insert_event, 0);
  26723. + sema_init(&instance->remove_event, 0);
  26724. + mutex_init(&instance->completion_mutex);
  26725. + mutex_init(&instance->bulk_waiter_list_mutex);
  26726. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  26727. +
  26728. + file->private_data = instance;
  26729. + } break;
  26730. +
  26731. + default:
  26732. + vchiq_log_error(vchiq_arm_log_level,
  26733. + "Unknown minor device: %d", dev);
  26734. + return -ENXIO;
  26735. + }
  26736. +
  26737. + return 0;
  26738. +}
  26739. +
  26740. +/****************************************************************************
  26741. +*
  26742. +* vchiq_release
  26743. +*
  26744. +***************************************************************************/
  26745. +
  26746. +static int
  26747. +vchiq_release(struct inode *inode, struct file *file)
  26748. +{
  26749. + int dev = iminor(inode) & 0x0f;
  26750. + int ret = 0;
  26751. + switch (dev) {
  26752. + case VCHIQ_MINOR: {
  26753. + VCHIQ_INSTANCE_T instance = file->private_data;
  26754. + VCHIQ_STATE_T *state = vchiq_get_state();
  26755. + VCHIQ_SERVICE_T *service;
  26756. + int i;
  26757. +
  26758. + vchiq_log_info(vchiq_arm_log_level,
  26759. + "vchiq_release: instance=%lx",
  26760. + (unsigned long)instance);
  26761. +
  26762. + if (!state) {
  26763. + ret = -EPERM;
  26764. + goto out;
  26765. + }
  26766. +
  26767. + /* Ensure videocore is awake to allow termination. */
  26768. + vchiq_use_internal(instance->state, NULL,
  26769. + USE_TYPE_VCHIQ);
  26770. +
  26771. + mutex_lock(&instance->completion_mutex);
  26772. +
  26773. + /* Wake the completion thread and ask it to exit */
  26774. + instance->closing = 1;
  26775. + up(&instance->insert_event);
  26776. +
  26777. + mutex_unlock(&instance->completion_mutex);
  26778. +
  26779. + /* Wake the slot handler if the completion queue is full. */
  26780. + up(&instance->remove_event);
  26781. +
  26782. + /* Mark all services for termination... */
  26783. + i = 0;
  26784. + while ((service = next_service_by_instance(state, instance,
  26785. + &i)) != NULL) {
  26786. + USER_SERVICE_T *user_service = service->base.userdata;
  26787. +
  26788. + /* Wake the slot handler if the msg queue is full. */
  26789. + up(&user_service->remove_event);
  26790. +
  26791. + vchiq_terminate_service_internal(service);
  26792. + unlock_service(service);
  26793. + }
  26794. +
  26795. + /* ...and wait for them to die */
  26796. + i = 0;
  26797. + while ((service = next_service_by_instance(state, instance, &i))
  26798. + != NULL) {
  26799. + USER_SERVICE_T *user_service = service->base.userdata;
  26800. +
  26801. + down(&service->remove_event);
  26802. +
  26803. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  26804. +
  26805. + spin_lock(&msg_queue_spinlock);
  26806. +
  26807. + while (user_service->msg_remove !=
  26808. + user_service->msg_insert) {
  26809. + VCHIQ_HEADER_T *header = user_service->
  26810. + msg_queue[user_service->msg_remove &
  26811. + (MSG_QUEUE_SIZE - 1)];
  26812. + user_service->msg_remove++;
  26813. + spin_unlock(&msg_queue_spinlock);
  26814. +
  26815. + if (header)
  26816. + vchiq_release_message(
  26817. + service->handle,
  26818. + header);
  26819. + spin_lock(&msg_queue_spinlock);
  26820. + }
  26821. +
  26822. + spin_unlock(&msg_queue_spinlock);
  26823. +
  26824. + unlock_service(service);
  26825. + }
  26826. +
  26827. + /* Release any closed services */
  26828. + while (instance->completion_remove !=
  26829. + instance->completion_insert) {
  26830. + VCHIQ_COMPLETION_DATA_T *completion;
  26831. + VCHIQ_SERVICE_T *service;
  26832. + completion = &instance->completions[
  26833. + instance->completion_remove &
  26834. + (MAX_COMPLETIONS - 1)];
  26835. + service = completion->service_userdata;
  26836. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  26837. + {
  26838. + USER_SERVICE_T *user_service =
  26839. + service->base.userdata;
  26840. +
  26841. + /* Wake any blocked user-thread */
  26842. + if (instance->use_close_delivered)
  26843. + up(&user_service->close_event);
  26844. + unlock_service(service);
  26845. + }
  26846. + instance->completion_remove++;
  26847. + }
  26848. +
  26849. + /* Release the PEER service count. */
  26850. + vchiq_release_internal(instance->state, NULL);
  26851. +
  26852. + {
  26853. + struct list_head *pos, *next;
  26854. + list_for_each_safe(pos, next,
  26855. + &instance->bulk_waiter_list) {
  26856. + struct bulk_waiter_node *waiter;
  26857. + waiter = list_entry(pos,
  26858. + struct bulk_waiter_node,
  26859. + list);
  26860. + list_del(pos);
  26861. + vchiq_log_info(vchiq_arm_log_level,
  26862. + "bulk_waiter - cleaned up %x "
  26863. + "for pid %d",
  26864. + (unsigned int)waiter, waiter->pid);
  26865. + kfree(waiter);
  26866. + }
  26867. + }
  26868. +
  26869. + vchiq_debugfs_remove_instance(instance);
  26870. +
  26871. + kfree(instance);
  26872. + file->private_data = NULL;
  26873. + } break;
  26874. +
  26875. + default:
  26876. + vchiq_log_error(vchiq_arm_log_level,
  26877. + "Unknown minor device: %d", dev);
  26878. + ret = -ENXIO;
  26879. + }
  26880. +
  26881. +out:
  26882. + return ret;
  26883. +}
  26884. +
  26885. +/****************************************************************************
  26886. +*
  26887. +* vchiq_dump
  26888. +*
  26889. +***************************************************************************/
  26890. +
  26891. +void
  26892. +vchiq_dump(void *dump_context, const char *str, int len)
  26893. +{
  26894. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  26895. +
  26896. + if (context->actual < context->space) {
  26897. + int copy_bytes;
  26898. + if (context->offset > 0) {
  26899. + int skip_bytes = min(len, (int)context->offset);
  26900. + str += skip_bytes;
  26901. + len -= skip_bytes;
  26902. + context->offset -= skip_bytes;
  26903. + if (context->offset > 0)
  26904. + return;
  26905. + }
  26906. + copy_bytes = min(len, (int)(context->space - context->actual));
  26907. + if (copy_bytes == 0)
  26908. + return;
  26909. + if (copy_to_user(context->buf + context->actual, str,
  26910. + copy_bytes))
  26911. + context->actual = -EFAULT;
  26912. + context->actual += copy_bytes;
  26913. + len -= copy_bytes;
  26914. +
  26915. + /* If tne terminating NUL is included in the length, then it
  26916. + ** marks the end of a line and should be replaced with a
  26917. + ** carriage return. */
  26918. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  26919. + char cr = '\n';
  26920. + if (copy_to_user(context->buf + context->actual - 1,
  26921. + &cr, 1))
  26922. + context->actual = -EFAULT;
  26923. + }
  26924. + }
  26925. +}
  26926. +
  26927. +/****************************************************************************
  26928. +*
  26929. +* vchiq_dump_platform_instance_state
  26930. +*
  26931. +***************************************************************************/
  26932. +
  26933. +void
  26934. +vchiq_dump_platform_instances(void *dump_context)
  26935. +{
  26936. + VCHIQ_STATE_T *state = vchiq_get_state();
  26937. + char buf[80];
  26938. + int len;
  26939. + int i;
  26940. +
  26941. + /* There is no list of instances, so instead scan all services,
  26942. + marking those that have been dumped. */
  26943. +
  26944. + for (i = 0; i < state->unused_service; i++) {
  26945. + VCHIQ_SERVICE_T *service = state->services[i];
  26946. + VCHIQ_INSTANCE_T instance;
  26947. +
  26948. + if (service && (service->base.callback == service_callback)) {
  26949. + instance = service->instance;
  26950. + if (instance)
  26951. + instance->mark = 0;
  26952. + }
  26953. + }
  26954. +
  26955. + for (i = 0; i < state->unused_service; i++) {
  26956. + VCHIQ_SERVICE_T *service = state->services[i];
  26957. + VCHIQ_INSTANCE_T instance;
  26958. +
  26959. + if (service && (service->base.callback == service_callback)) {
  26960. + instance = service->instance;
  26961. + if (instance && !instance->mark) {
  26962. + len = snprintf(buf, sizeof(buf),
  26963. + "Instance %x: pid %d,%s completions "
  26964. + "%d/%d",
  26965. + (unsigned int)instance, instance->pid,
  26966. + instance->connected ? " connected, " :
  26967. + "",
  26968. + instance->completion_insert -
  26969. + instance->completion_remove,
  26970. + MAX_COMPLETIONS);
  26971. +
  26972. + vchiq_dump(dump_context, buf, len + 1);
  26973. +
  26974. + instance->mark = 1;
  26975. + }
  26976. + }
  26977. + }
  26978. +}
  26979. +
  26980. +/****************************************************************************
  26981. +*
  26982. +* vchiq_dump_platform_service_state
  26983. +*
  26984. +***************************************************************************/
  26985. +
  26986. +void
  26987. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  26988. +{
  26989. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  26990. + char buf[80];
  26991. + int len;
  26992. +
  26993. + len = snprintf(buf, sizeof(buf), " instance %x",
  26994. + (unsigned int)service->instance);
  26995. +
  26996. + if ((service->base.callback == service_callback) &&
  26997. + user_service->is_vchi) {
  26998. + len += snprintf(buf + len, sizeof(buf) - len,
  26999. + ", %d/%d messages",
  27000. + user_service->msg_insert - user_service->msg_remove,
  27001. + MSG_QUEUE_SIZE);
  27002. +
  27003. + if (user_service->dequeue_pending)
  27004. + len += snprintf(buf + len, sizeof(buf) - len,
  27005. + " (dequeue pending)");
  27006. + }
  27007. +
  27008. + vchiq_dump(dump_context, buf, len + 1);
  27009. +}
  27010. +
  27011. +/****************************************************************************
  27012. +*
  27013. +* dump_user_mem
  27014. +*
  27015. +***************************************************************************/
  27016. +
  27017. +static void
  27018. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  27019. +{
  27020. + int rc;
  27021. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  27022. + int num_pages;
  27023. + int offset;
  27024. + int end_offset;
  27025. + int page_idx;
  27026. + int prev_idx;
  27027. + struct page *page;
  27028. + struct page **pages;
  27029. + uint8_t *kmapped_virt_ptr;
  27030. +
  27031. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  27032. +
  27033. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  27034. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  27035. + ~0x0fuL);
  27036. +
  27037. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  27038. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  27039. +
  27040. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  27041. +
  27042. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  27043. + if (pages == NULL) {
  27044. + vchiq_log_error(vchiq_arm_log_level,
  27045. + "Unable to allocation memory for %d pages\n",
  27046. + num_pages);
  27047. + return;
  27048. + }
  27049. +
  27050. + down_read(&current->mm->mmap_sem);
  27051. + rc = get_user_pages(current, /* task */
  27052. + current->mm, /* mm */
  27053. + (unsigned long)virt_addr, /* start */
  27054. + num_pages, /* len */
  27055. + 0, /* write */
  27056. + 0, /* force */
  27057. + pages, /* pages (array of page pointers) */
  27058. + NULL); /* vmas */
  27059. + up_read(&current->mm->mmap_sem);
  27060. +
  27061. + prev_idx = -1;
  27062. + page = NULL;
  27063. +
  27064. + while (offset < end_offset) {
  27065. +
  27066. + int page_offset = offset % PAGE_SIZE;
  27067. + page_idx = offset / PAGE_SIZE;
  27068. +
  27069. + if (page_idx != prev_idx) {
  27070. +
  27071. + if (page != NULL)
  27072. + kunmap(page);
  27073. + page = pages[page_idx];
  27074. + kmapped_virt_ptr = kmap(page);
  27075. +
  27076. + prev_idx = page_idx;
  27077. + }
  27078. +
  27079. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  27080. + vchiq_log_dump_mem("ph",
  27081. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  27082. + page_offset],
  27083. + &kmapped_virt_ptr[page_offset], 16);
  27084. +
  27085. + offset += 16;
  27086. + }
  27087. + if (page != NULL)
  27088. + kunmap(page);
  27089. +
  27090. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  27091. + page_cache_release(pages[page_idx]);
  27092. +
  27093. + kfree(pages);
  27094. +}
  27095. +
  27096. +/****************************************************************************
  27097. +*
  27098. +* vchiq_read
  27099. +*
  27100. +***************************************************************************/
  27101. +
  27102. +static ssize_t
  27103. +vchiq_read(struct file *file, char __user *buf,
  27104. + size_t count, loff_t *ppos)
  27105. +{
  27106. + DUMP_CONTEXT_T context;
  27107. + context.buf = buf;
  27108. + context.actual = 0;
  27109. + context.space = count;
  27110. + context.offset = *ppos;
  27111. +
  27112. + vchiq_dump_state(&context, &g_state);
  27113. +
  27114. + *ppos += context.actual;
  27115. +
  27116. + return context.actual;
  27117. +}
  27118. +
  27119. +VCHIQ_STATE_T *
  27120. +vchiq_get_state(void)
  27121. +{
  27122. +
  27123. + if (g_state.remote == NULL)
  27124. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  27125. + else if (g_state.remote->initialised != 1)
  27126. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  27127. + __func__, g_state.remote->initialised);
  27128. +
  27129. + return ((g_state.remote != NULL) &&
  27130. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  27131. +}
  27132. +
  27133. +static const struct file_operations
  27134. +vchiq_fops = {
  27135. + .owner = THIS_MODULE,
  27136. + .unlocked_ioctl = vchiq_ioctl,
  27137. + .open = vchiq_open,
  27138. + .release = vchiq_release,
  27139. + .read = vchiq_read
  27140. +};
  27141. +
  27142. +/*
  27143. + * Autosuspend related functionality
  27144. + */
  27145. +
  27146. +int
  27147. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  27148. +{
  27149. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27150. + if (!arm_state)
  27151. + /* autosuspend not supported - always return wanted */
  27152. + return 1;
  27153. + else if (arm_state->blocked_count)
  27154. + return 1;
  27155. + else if (!arm_state->videocore_use_count)
  27156. + /* usage count zero - check for override unless we're forcing */
  27157. + if (arm_state->resume_blocked)
  27158. + return 0;
  27159. + else
  27160. + return vchiq_platform_videocore_wanted(state);
  27161. + else
  27162. + /* non-zero usage count - videocore still required */
  27163. + return 1;
  27164. +}
  27165. +
  27166. +static VCHIQ_STATUS_T
  27167. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  27168. + VCHIQ_HEADER_T *header,
  27169. + VCHIQ_SERVICE_HANDLE_T service_user,
  27170. + void *bulk_user)
  27171. +{
  27172. + vchiq_log_error(vchiq_susp_log_level,
  27173. + "%s callback reason %d", __func__, reason);
  27174. + return 0;
  27175. +}
  27176. +
  27177. +static int
  27178. +vchiq_keepalive_thread_func(void *v)
  27179. +{
  27180. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  27181. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27182. +
  27183. + VCHIQ_STATUS_T status;
  27184. + VCHIQ_INSTANCE_T instance;
  27185. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  27186. +
  27187. + VCHIQ_SERVICE_PARAMS_T params = {
  27188. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  27189. + .callback = vchiq_keepalive_vchiq_callback,
  27190. + .version = KEEPALIVE_VER,
  27191. + .version_min = KEEPALIVE_VER_MIN
  27192. + };
  27193. +
  27194. + status = vchiq_initialise(&instance);
  27195. + if (status != VCHIQ_SUCCESS) {
  27196. + vchiq_log_error(vchiq_susp_log_level,
  27197. + "%s vchiq_initialise failed %d", __func__, status);
  27198. + goto exit;
  27199. + }
  27200. +
  27201. + status = vchiq_connect(instance);
  27202. + if (status != VCHIQ_SUCCESS) {
  27203. + vchiq_log_error(vchiq_susp_log_level,
  27204. + "%s vchiq_connect failed %d", __func__, status);
  27205. + goto shutdown;
  27206. + }
  27207. +
  27208. + status = vchiq_add_service(instance, &params, &ka_handle);
  27209. + if (status != VCHIQ_SUCCESS) {
  27210. + vchiq_log_error(vchiq_susp_log_level,
  27211. + "%s vchiq_open_service failed %d", __func__, status);
  27212. + goto shutdown;
  27213. + }
  27214. +
  27215. + while (1) {
  27216. + long rc = 0, uc = 0;
  27217. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  27218. + != 0) {
  27219. + vchiq_log_error(vchiq_susp_log_level,
  27220. + "%s interrupted", __func__);
  27221. + flush_signals(current);
  27222. + continue;
  27223. + }
  27224. +
  27225. + /* read and clear counters. Do release_count then use_count to
  27226. + * prevent getting more releases than uses */
  27227. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  27228. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  27229. +
  27230. + /* Call use/release service the requisite number of times.
  27231. + * Process use before release so use counts don't go negative */
  27232. + while (uc--) {
  27233. + atomic_inc(&arm_state->ka_use_ack_count);
  27234. + status = vchiq_use_service(ka_handle);
  27235. + if (status != VCHIQ_SUCCESS) {
  27236. + vchiq_log_error(vchiq_susp_log_level,
  27237. + "%s vchiq_use_service error %d",
  27238. + __func__, status);
  27239. + }
  27240. + }
  27241. + while (rc--) {
  27242. + status = vchiq_release_service(ka_handle);
  27243. + if (status != VCHIQ_SUCCESS) {
  27244. + vchiq_log_error(vchiq_susp_log_level,
  27245. + "%s vchiq_release_service error %d",
  27246. + __func__, status);
  27247. + }
  27248. + }
  27249. + }
  27250. +
  27251. +shutdown:
  27252. + vchiq_shutdown(instance);
  27253. +exit:
  27254. + return 0;
  27255. +}
  27256. +
  27257. +
  27258. +
  27259. +VCHIQ_STATUS_T
  27260. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  27261. +{
  27262. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  27263. +
  27264. + if (arm_state) {
  27265. + rwlock_init(&arm_state->susp_res_lock);
  27266. +
  27267. + init_completion(&arm_state->ka_evt);
  27268. + atomic_set(&arm_state->ka_use_count, 0);
  27269. + atomic_set(&arm_state->ka_use_ack_count, 0);
  27270. + atomic_set(&arm_state->ka_release_count, 0);
  27271. +
  27272. + init_completion(&arm_state->vc_suspend_complete);
  27273. +
  27274. + init_completion(&arm_state->vc_resume_complete);
  27275. + /* Initialise to 'done' state. We only want to block on resume
  27276. + * completion while videocore is suspended. */
  27277. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  27278. +
  27279. + init_completion(&arm_state->resume_blocker);
  27280. + /* Initialise to 'done' state. We only want to block on this
  27281. + * completion while resume is blocked */
  27282. + complete_all(&arm_state->resume_blocker);
  27283. +
  27284. + init_completion(&arm_state->blocked_blocker);
  27285. + /* Initialise to 'done' state. We only want to block on this
  27286. + * completion while things are waiting on the resume blocker */
  27287. + complete_all(&arm_state->blocked_blocker);
  27288. +
  27289. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  27290. + arm_state->suspend_timer_running = 0;
  27291. + init_timer(&arm_state->suspend_timer);
  27292. + arm_state->suspend_timer.data = (unsigned long)(state);
  27293. + arm_state->suspend_timer.function = suspend_timer_callback;
  27294. +
  27295. + arm_state->first_connect = 0;
  27296. +
  27297. + }
  27298. + return status;
  27299. +}
  27300. +
  27301. +/*
  27302. +** Functions to modify the state variables;
  27303. +** set_suspend_state
  27304. +** set_resume_state
  27305. +**
  27306. +** There are more state variables than we might like, so ensure they remain in
  27307. +** step. Suspend and resume state are maintained separately, since most of
  27308. +** these state machines can operate independently. However, there are a few
  27309. +** states where state transitions in one state machine cause a reset to the
  27310. +** other state machine. In addition, there are some completion events which
  27311. +** need to occur on state machine reset and end-state(s), so these are also
  27312. +** dealt with in these functions.
  27313. +**
  27314. +** In all states we set the state variable according to the input, but in some
  27315. +** cases we perform additional steps outlined below;
  27316. +**
  27317. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  27318. +** The suspend completion is completed after any suspend
  27319. +** attempt. When we reset the state machine we also reset
  27320. +** the completion. This reset occurs when videocore is
  27321. +** resumed, and also if we initiate suspend after a suspend
  27322. +** failure.
  27323. +**
  27324. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  27325. +** suspend - ie from this point on we must try to suspend
  27326. +** before resuming can occur. We therefore also reset the
  27327. +** resume state machine to VC_RESUME_IDLE in this state.
  27328. +**
  27329. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  27330. +** complete_all on the suspend completion to notify
  27331. +** anything waiting for suspend to happen.
  27332. +**
  27333. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  27334. +** initiate resume, so no need to alter resume state.
  27335. +** We call complete_all on the suspend completion to notify
  27336. +** of suspend rejection.
  27337. +**
  27338. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  27339. +** suspend completion and reset the resume state machine.
  27340. +**
  27341. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  27342. +** resume completion is in it's 'done' state whenever
  27343. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  27344. +** implies that videocore is suspended.
  27345. +** Hence, any thread which needs to wait until videocore is
  27346. +** running can wait on this completion - it will only block
  27347. +** if videocore is suspended.
  27348. +**
  27349. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  27350. +** Call complete_all on the resume completion to unblock
  27351. +** any threads waiting for resume. Also reset the suspend
  27352. +** state machine to it's idle state.
  27353. +**
  27354. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  27355. +*/
  27356. +
  27357. +inline void
  27358. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  27359. + enum vc_suspend_status new_state)
  27360. +{
  27361. + /* set the state in all cases */
  27362. + arm_state->vc_suspend_state = new_state;
  27363. +
  27364. + /* state specific additional actions */
  27365. + switch (new_state) {
  27366. + case VC_SUSPEND_FORCE_CANCELED:
  27367. + complete_all(&arm_state->vc_suspend_complete);
  27368. + break;
  27369. + case VC_SUSPEND_REJECTED:
  27370. + complete_all(&arm_state->vc_suspend_complete);
  27371. + break;
  27372. + case VC_SUSPEND_FAILED:
  27373. + complete_all(&arm_state->vc_suspend_complete);
  27374. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  27375. + complete_all(&arm_state->vc_resume_complete);
  27376. + break;
  27377. + case VC_SUSPEND_IDLE:
  27378. + reinit_completion(&arm_state->vc_suspend_complete);
  27379. + break;
  27380. + case VC_SUSPEND_REQUESTED:
  27381. + break;
  27382. + case VC_SUSPEND_IN_PROGRESS:
  27383. + set_resume_state(arm_state, VC_RESUME_IDLE);
  27384. + break;
  27385. + case VC_SUSPEND_SUSPENDED:
  27386. + complete_all(&arm_state->vc_suspend_complete);
  27387. + break;
  27388. + default:
  27389. + BUG();
  27390. + break;
  27391. + }
  27392. +}
  27393. +
  27394. +inline void
  27395. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  27396. + enum vc_resume_status new_state)
  27397. +{
  27398. + /* set the state in all cases */
  27399. + arm_state->vc_resume_state = new_state;
  27400. +
  27401. + /* state specific additional actions */
  27402. + switch (new_state) {
  27403. + case VC_RESUME_FAILED:
  27404. + break;
  27405. + case VC_RESUME_IDLE:
  27406. + reinit_completion(&arm_state->vc_resume_complete);
  27407. + break;
  27408. + case VC_RESUME_REQUESTED:
  27409. + break;
  27410. + case VC_RESUME_IN_PROGRESS:
  27411. + break;
  27412. + case VC_RESUME_RESUMED:
  27413. + complete_all(&arm_state->vc_resume_complete);
  27414. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  27415. + break;
  27416. + default:
  27417. + BUG();
  27418. + break;
  27419. + }
  27420. +}
  27421. +
  27422. +
  27423. +/* should be called with the write lock held */
  27424. +inline void
  27425. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  27426. +{
  27427. + del_timer(&arm_state->suspend_timer);
  27428. + arm_state->suspend_timer.expires = jiffies +
  27429. + msecs_to_jiffies(arm_state->
  27430. + suspend_timer_timeout);
  27431. + add_timer(&arm_state->suspend_timer);
  27432. + arm_state->suspend_timer_running = 1;
  27433. +}
  27434. +
  27435. +/* should be called with the write lock held */
  27436. +static inline void
  27437. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  27438. +{
  27439. + if (arm_state->suspend_timer_running) {
  27440. + del_timer(&arm_state->suspend_timer);
  27441. + arm_state->suspend_timer_running = 0;
  27442. + }
  27443. +}
  27444. +
  27445. +static inline int
  27446. +need_resume(VCHIQ_STATE_T *state)
  27447. +{
  27448. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27449. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  27450. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  27451. + vchiq_videocore_wanted(state);
  27452. +}
  27453. +
  27454. +static int
  27455. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  27456. +{
  27457. + int status = VCHIQ_SUCCESS;
  27458. + const unsigned long timeout_val =
  27459. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  27460. + int resume_count = 0;
  27461. +
  27462. + /* Allow any threads which were blocked by the last force suspend to
  27463. + * complete if they haven't already. Only give this one shot; if
  27464. + * blocked_count is incremented after blocked_blocker is completed
  27465. + * (which only happens when blocked_count hits 0) then those threads
  27466. + * will have to wait until next time around */
  27467. + if (arm_state->blocked_count) {
  27468. + reinit_completion(&arm_state->blocked_blocker);
  27469. + write_unlock_bh(&arm_state->susp_res_lock);
  27470. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  27471. + "blocked clients", __func__);
  27472. + if (wait_for_completion_interruptible_timeout(
  27473. + &arm_state->blocked_blocker, timeout_val)
  27474. + <= 0) {
  27475. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  27476. + "previously blocked clients failed" , __func__);
  27477. + status = VCHIQ_ERROR;
  27478. + write_lock_bh(&arm_state->susp_res_lock);
  27479. + goto out;
  27480. + }
  27481. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  27482. + "clients resumed", __func__);
  27483. + write_lock_bh(&arm_state->susp_res_lock);
  27484. + }
  27485. +
  27486. + /* We need to wait for resume to complete if it's in process */
  27487. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  27488. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  27489. + if (resume_count > 1) {
  27490. + status = VCHIQ_ERROR;
  27491. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  27492. + "many times for resume" , __func__);
  27493. + goto out;
  27494. + }
  27495. + write_unlock_bh(&arm_state->susp_res_lock);
  27496. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  27497. + __func__);
  27498. + if (wait_for_completion_interruptible_timeout(
  27499. + &arm_state->vc_resume_complete, timeout_val)
  27500. + <= 0) {
  27501. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  27502. + "resume failed (%s)", __func__,
  27503. + resume_state_names[arm_state->vc_resume_state +
  27504. + VC_RESUME_NUM_OFFSET]);
  27505. + status = VCHIQ_ERROR;
  27506. + write_lock_bh(&arm_state->susp_res_lock);
  27507. + goto out;
  27508. + }
  27509. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  27510. + write_lock_bh(&arm_state->susp_res_lock);
  27511. + resume_count++;
  27512. + }
  27513. + reinit_completion(&arm_state->resume_blocker);
  27514. + arm_state->resume_blocked = 1;
  27515. +
  27516. +out:
  27517. + return status;
  27518. +}
  27519. +
  27520. +static inline void
  27521. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  27522. +{
  27523. + complete_all(&arm_state->resume_blocker);
  27524. + arm_state->resume_blocked = 0;
  27525. +}
  27526. +
  27527. +/* Initiate suspend via slot handler. Should be called with the write lock
  27528. + * held */
  27529. +VCHIQ_STATUS_T
  27530. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  27531. +{
  27532. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  27533. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27534. +
  27535. + if (!arm_state)
  27536. + goto out;
  27537. +
  27538. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27539. + status = VCHIQ_SUCCESS;
  27540. +
  27541. +
  27542. + switch (arm_state->vc_suspend_state) {
  27543. + case VC_SUSPEND_REQUESTED:
  27544. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  27545. + "requested", __func__);
  27546. + break;
  27547. + case VC_SUSPEND_IN_PROGRESS:
  27548. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  27549. + "progress", __func__);
  27550. + break;
  27551. +
  27552. + default:
  27553. + /* We don't expect to be in other states, so log but continue
  27554. + * anyway */
  27555. + vchiq_log_error(vchiq_susp_log_level,
  27556. + "%s unexpected suspend state %s", __func__,
  27557. + suspend_state_names[arm_state->vc_suspend_state +
  27558. + VC_SUSPEND_NUM_OFFSET]);
  27559. + /* fall through */
  27560. + case VC_SUSPEND_REJECTED:
  27561. + case VC_SUSPEND_FAILED:
  27562. + /* Ensure any idle state actions have been run */
  27563. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  27564. + /* fall through */
  27565. + case VC_SUSPEND_IDLE:
  27566. + vchiq_log_info(vchiq_susp_log_level,
  27567. + "%s: suspending", __func__);
  27568. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  27569. + /* kick the slot handler thread to initiate suspend */
  27570. + request_poll(state, NULL, 0);
  27571. + break;
  27572. + }
  27573. +
  27574. +out:
  27575. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  27576. + return status;
  27577. +}
  27578. +
  27579. +void
  27580. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  27581. +{
  27582. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27583. + int susp = 0;
  27584. +
  27585. + if (!arm_state)
  27586. + goto out;
  27587. +
  27588. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27589. +
  27590. + write_lock_bh(&arm_state->susp_res_lock);
  27591. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  27592. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  27593. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  27594. + susp = 1;
  27595. + }
  27596. + write_unlock_bh(&arm_state->susp_res_lock);
  27597. +
  27598. + if (susp)
  27599. + vchiq_platform_suspend(state);
  27600. +
  27601. +out:
  27602. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  27603. + return;
  27604. +}
  27605. +
  27606. +
  27607. +static void
  27608. +output_timeout_error(VCHIQ_STATE_T *state)
  27609. +{
  27610. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27611. + char service_err[50] = "";
  27612. + int vc_use_count = arm_state->videocore_use_count;
  27613. + int active_services = state->unused_service;
  27614. + int i;
  27615. +
  27616. + if (!arm_state->videocore_use_count) {
  27617. + snprintf(service_err, 50, " Videocore usecount is 0");
  27618. + goto output_msg;
  27619. + }
  27620. + for (i = 0; i < active_services; i++) {
  27621. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  27622. + if (service_ptr && service_ptr->service_use_count &&
  27623. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  27624. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  27625. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  27626. + service_ptr->base.fourcc),
  27627. + service_ptr->client_id,
  27628. + service_ptr->service_use_count,
  27629. + service_ptr->service_use_count ==
  27630. + vc_use_count ? "" : " (+ more)");
  27631. + break;
  27632. + }
  27633. + }
  27634. +
  27635. +output_msg:
  27636. + vchiq_log_error(vchiq_susp_log_level,
  27637. + "timed out waiting for vc suspend (%d).%s",
  27638. + arm_state->autosuspend_override, service_err);
  27639. +
  27640. +}
  27641. +
  27642. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  27643. +** We don't actually force suspend, since videocore may get into a bad state
  27644. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  27645. +** determine a good point to suspend. If this doesn't happen within 100ms we
  27646. +** report failure.
  27647. +**
  27648. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  27649. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  27650. +*/
  27651. +VCHIQ_STATUS_T
  27652. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  27653. +{
  27654. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27655. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  27656. + long rc = 0;
  27657. + int repeat = -1;
  27658. +
  27659. + if (!arm_state)
  27660. + goto out;
  27661. +
  27662. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27663. +
  27664. + write_lock_bh(&arm_state->susp_res_lock);
  27665. +
  27666. + status = block_resume(arm_state);
  27667. + if (status != VCHIQ_SUCCESS)
  27668. + goto unlock;
  27669. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  27670. + /* Already suspended - just block resume and exit */
  27671. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  27672. + __func__);
  27673. + status = VCHIQ_SUCCESS;
  27674. + goto unlock;
  27675. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  27676. + /* initiate suspend immediately in the case that we're waiting
  27677. + * for the timeout */
  27678. + stop_suspend_timer(arm_state);
  27679. + if (!vchiq_videocore_wanted(state)) {
  27680. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  27681. + "idle, initiating suspend", __func__);
  27682. + status = vchiq_arm_vcsuspend(state);
  27683. + } else if (arm_state->autosuspend_override <
  27684. + FORCE_SUSPEND_FAIL_MAX) {
  27685. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  27686. + "videocore go idle", __func__);
  27687. + status = VCHIQ_SUCCESS;
  27688. + } else {
  27689. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  27690. + "many times - attempting suspend", __func__);
  27691. + status = vchiq_arm_vcsuspend(state);
  27692. + }
  27693. + } else {
  27694. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  27695. + "in progress - wait for completion", __func__);
  27696. + status = VCHIQ_SUCCESS;
  27697. + }
  27698. +
  27699. + /* Wait for suspend to happen due to system idle (not forced..) */
  27700. + if (status != VCHIQ_SUCCESS)
  27701. + goto unblock_resume;
  27702. +
  27703. + do {
  27704. + write_unlock_bh(&arm_state->susp_res_lock);
  27705. +
  27706. + rc = wait_for_completion_interruptible_timeout(
  27707. + &arm_state->vc_suspend_complete,
  27708. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  27709. +
  27710. + write_lock_bh(&arm_state->susp_res_lock);
  27711. + if (rc < 0) {
  27712. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  27713. + "interrupted waiting for suspend", __func__);
  27714. + status = VCHIQ_ERROR;
  27715. + goto unblock_resume;
  27716. + } else if (rc == 0) {
  27717. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  27718. + /* Repeat timeout once if in progress */
  27719. + if (repeat < 0) {
  27720. + repeat = 1;
  27721. + continue;
  27722. + }
  27723. + }
  27724. + arm_state->autosuspend_override++;
  27725. + output_timeout_error(state);
  27726. +
  27727. + status = VCHIQ_RETRY;
  27728. + goto unblock_resume;
  27729. + }
  27730. + } while (0 < (repeat--));
  27731. +
  27732. + /* Check and report state in case we need to abort ARM suspend */
  27733. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  27734. + status = VCHIQ_RETRY;
  27735. + vchiq_log_error(vchiq_susp_log_level,
  27736. + "%s videocore suspend failed (state %s)", __func__,
  27737. + suspend_state_names[arm_state->vc_suspend_state +
  27738. + VC_SUSPEND_NUM_OFFSET]);
  27739. + /* Reset the state only if it's still in an error state.
  27740. + * Something could have already initiated another suspend. */
  27741. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  27742. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  27743. +
  27744. + goto unblock_resume;
  27745. + }
  27746. +
  27747. + /* successfully suspended - unlock and exit */
  27748. + goto unlock;
  27749. +
  27750. +unblock_resume:
  27751. + /* all error states need to unblock resume before exit */
  27752. + unblock_resume(arm_state);
  27753. +
  27754. +unlock:
  27755. + write_unlock_bh(&arm_state->susp_res_lock);
  27756. +
  27757. +out:
  27758. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  27759. + return status;
  27760. +}
  27761. +
  27762. +void
  27763. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  27764. +{
  27765. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27766. +
  27767. + if (!arm_state)
  27768. + goto out;
  27769. +
  27770. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27771. +
  27772. + write_lock_bh(&arm_state->susp_res_lock);
  27773. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  27774. + arm_state->first_connect &&
  27775. + !vchiq_videocore_wanted(state)) {
  27776. + vchiq_arm_vcsuspend(state);
  27777. + }
  27778. + write_unlock_bh(&arm_state->susp_res_lock);
  27779. +
  27780. +out:
  27781. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  27782. + return;
  27783. +}
  27784. +
  27785. +
  27786. +int
  27787. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  27788. +{
  27789. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27790. + int resume = 0;
  27791. + int ret = -1;
  27792. +
  27793. + if (!arm_state)
  27794. + goto out;
  27795. +
  27796. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27797. +
  27798. + write_lock_bh(&arm_state->susp_res_lock);
  27799. + unblock_resume(arm_state);
  27800. + resume = vchiq_check_resume(state);
  27801. + write_unlock_bh(&arm_state->susp_res_lock);
  27802. +
  27803. + if (resume) {
  27804. + if (wait_for_completion_interruptible(
  27805. + &arm_state->vc_resume_complete) < 0) {
  27806. + vchiq_log_error(vchiq_susp_log_level,
  27807. + "%s interrupted", __func__);
  27808. + /* failed, cannot accurately derive suspend
  27809. + * state, so exit early. */
  27810. + goto out;
  27811. + }
  27812. + }
  27813. +
  27814. + read_lock_bh(&arm_state->susp_res_lock);
  27815. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  27816. + vchiq_log_info(vchiq_susp_log_level,
  27817. + "%s: Videocore remains suspended", __func__);
  27818. + } else {
  27819. + vchiq_log_info(vchiq_susp_log_level,
  27820. + "%s: Videocore resumed", __func__);
  27821. + ret = 0;
  27822. + }
  27823. + read_unlock_bh(&arm_state->susp_res_lock);
  27824. +out:
  27825. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  27826. + return ret;
  27827. +}
  27828. +
  27829. +/* This function should be called with the write lock held */
  27830. +int
  27831. +vchiq_check_resume(VCHIQ_STATE_T *state)
  27832. +{
  27833. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27834. + int resume = 0;
  27835. +
  27836. + if (!arm_state)
  27837. + goto out;
  27838. +
  27839. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27840. +
  27841. + if (need_resume(state)) {
  27842. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  27843. + request_poll(state, NULL, 0);
  27844. + resume = 1;
  27845. + }
  27846. +
  27847. +out:
  27848. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  27849. + return resume;
  27850. +}
  27851. +
  27852. +void
  27853. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  27854. +{
  27855. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27856. + int res = 0;
  27857. +
  27858. + if (!arm_state)
  27859. + goto out;
  27860. +
  27861. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27862. +
  27863. + write_lock_bh(&arm_state->susp_res_lock);
  27864. + if (arm_state->wake_address == 0) {
  27865. + vchiq_log_info(vchiq_susp_log_level,
  27866. + "%s: already awake", __func__);
  27867. + goto unlock;
  27868. + }
  27869. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  27870. + vchiq_log_info(vchiq_susp_log_level,
  27871. + "%s: already resuming", __func__);
  27872. + goto unlock;
  27873. + }
  27874. +
  27875. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  27876. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  27877. + res = 1;
  27878. + } else
  27879. + vchiq_log_trace(vchiq_susp_log_level,
  27880. + "%s: not resuming (resume state %s)", __func__,
  27881. + resume_state_names[arm_state->vc_resume_state +
  27882. + VC_RESUME_NUM_OFFSET]);
  27883. +
  27884. +unlock:
  27885. + write_unlock_bh(&arm_state->susp_res_lock);
  27886. +
  27887. + if (res)
  27888. + vchiq_platform_resume(state);
  27889. +
  27890. +out:
  27891. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  27892. + return;
  27893. +
  27894. +}
  27895. +
  27896. +
  27897. +
  27898. +VCHIQ_STATUS_T
  27899. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  27900. + enum USE_TYPE_E use_type)
  27901. +{
  27902. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27903. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  27904. + char entity[16];
  27905. + int *entity_uc;
  27906. + int local_uc, local_entity_uc;
  27907. +
  27908. + if (!arm_state)
  27909. + goto out;
  27910. +
  27911. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27912. +
  27913. + if (use_type == USE_TYPE_VCHIQ) {
  27914. + sprintf(entity, "VCHIQ: ");
  27915. + entity_uc = &arm_state->peer_use_count;
  27916. + } else if (service) {
  27917. + sprintf(entity, "%c%c%c%c:%03d",
  27918. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  27919. + service->client_id);
  27920. + entity_uc = &service->service_use_count;
  27921. + } else {
  27922. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  27923. + "ptr", __func__);
  27924. + ret = VCHIQ_ERROR;
  27925. + goto out;
  27926. + }
  27927. +
  27928. + write_lock_bh(&arm_state->susp_res_lock);
  27929. + while (arm_state->resume_blocked) {
  27930. + /* If we call 'use' while force suspend is waiting for suspend,
  27931. + * then we're about to block the thread which the force is
  27932. + * waiting to complete, so we're bound to just time out. In this
  27933. + * case, set the suspend state such that the wait will be
  27934. + * canceled, so we can complete as quickly as possible. */
  27935. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  27936. + VC_SUSPEND_IDLE) {
  27937. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  27938. + break;
  27939. + }
  27940. + /* If suspend is already in progress then we need to block */
  27941. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  27942. + /* Indicate that there are threads waiting on the resume
  27943. + * blocker. These need to be allowed to complete before
  27944. + * a _second_ call to force suspend can complete,
  27945. + * otherwise low priority threads might never actually
  27946. + * continue */
  27947. + arm_state->blocked_count++;
  27948. + write_unlock_bh(&arm_state->susp_res_lock);
  27949. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  27950. + "blocked - waiting...", __func__, entity);
  27951. + if (wait_for_completion_killable(
  27952. + &arm_state->resume_blocker) != 0) {
  27953. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  27954. + "wait for resume blocker interrupted",
  27955. + __func__, entity);
  27956. + ret = VCHIQ_ERROR;
  27957. + write_lock_bh(&arm_state->susp_res_lock);
  27958. + arm_state->blocked_count--;
  27959. + write_unlock_bh(&arm_state->susp_res_lock);
  27960. + goto out;
  27961. + }
  27962. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  27963. + "unblocked", __func__, entity);
  27964. + write_lock_bh(&arm_state->susp_res_lock);
  27965. + if (--arm_state->blocked_count == 0)
  27966. + complete_all(&arm_state->blocked_blocker);
  27967. + }
  27968. + }
  27969. +
  27970. + stop_suspend_timer(arm_state);
  27971. +
  27972. + local_uc = ++arm_state->videocore_use_count;
  27973. + local_entity_uc = ++(*entity_uc);
  27974. +
  27975. + /* If there's a pending request which hasn't yet been serviced then
  27976. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  27977. + * vc_resume_complete will block until we either resume or fail to
  27978. + * suspend */
  27979. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  27980. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  27981. +
  27982. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  27983. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  27984. + vchiq_log_info(vchiq_susp_log_level,
  27985. + "%s %s count %d, state count %d",
  27986. + __func__, entity, local_entity_uc, local_uc);
  27987. + request_poll(state, NULL, 0);
  27988. + } else
  27989. + vchiq_log_trace(vchiq_susp_log_level,
  27990. + "%s %s count %d, state count %d",
  27991. + __func__, entity, *entity_uc, local_uc);
  27992. +
  27993. +
  27994. + write_unlock_bh(&arm_state->susp_res_lock);
  27995. +
  27996. + /* Completion is in a done state when we're not suspended, so this won't
  27997. + * block for the non-suspended case. */
  27998. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  27999. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  28000. + __func__, entity);
  28001. + if (wait_for_completion_killable(
  28002. + &arm_state->vc_resume_complete) != 0) {
  28003. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  28004. + "resume interrupted", __func__, entity);
  28005. + ret = VCHIQ_ERROR;
  28006. + goto out;
  28007. + }
  28008. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  28009. + entity);
  28010. + }
  28011. +
  28012. + if (ret == VCHIQ_SUCCESS) {
  28013. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  28014. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  28015. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  28016. + /* Send the use notify to videocore */
  28017. + status = vchiq_send_remote_use_active(state);
  28018. + if (status == VCHIQ_SUCCESS)
  28019. + ack_cnt--;
  28020. + else
  28021. + atomic_add(ack_cnt,
  28022. + &arm_state->ka_use_ack_count);
  28023. + }
  28024. + }
  28025. +
  28026. +out:
  28027. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  28028. + return ret;
  28029. +}
  28030. +
  28031. +VCHIQ_STATUS_T
  28032. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  28033. +{
  28034. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28035. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  28036. + char entity[16];
  28037. + int *entity_uc;
  28038. + int local_uc, local_entity_uc;
  28039. +
  28040. + if (!arm_state)
  28041. + goto out;
  28042. +
  28043. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28044. +
  28045. + if (service) {
  28046. + sprintf(entity, "%c%c%c%c:%03d",
  28047. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  28048. + service->client_id);
  28049. + entity_uc = &service->service_use_count;
  28050. + } else {
  28051. + sprintf(entity, "PEER: ");
  28052. + entity_uc = &arm_state->peer_use_count;
  28053. + }
  28054. +
  28055. + write_lock_bh(&arm_state->susp_res_lock);
  28056. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  28057. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  28058. + WARN_ON(!arm_state->videocore_use_count);
  28059. + WARN_ON(!(*entity_uc));
  28060. + ret = VCHIQ_ERROR;
  28061. + goto unlock;
  28062. + }
  28063. + local_uc = --arm_state->videocore_use_count;
  28064. + local_entity_uc = --(*entity_uc);
  28065. +
  28066. + if (!vchiq_videocore_wanted(state)) {
  28067. + if (vchiq_platform_use_suspend_timer() &&
  28068. + !arm_state->resume_blocked) {
  28069. + /* Only use the timer if we're not trying to force
  28070. + * suspend (=> resume_blocked) */
  28071. + start_suspend_timer(arm_state);
  28072. + } else {
  28073. + vchiq_log_info(vchiq_susp_log_level,
  28074. + "%s %s count %d, state count %d - suspending",
  28075. + __func__, entity, *entity_uc,
  28076. + arm_state->videocore_use_count);
  28077. + vchiq_arm_vcsuspend(state);
  28078. + }
  28079. + } else
  28080. + vchiq_log_trace(vchiq_susp_log_level,
  28081. + "%s %s count %d, state count %d",
  28082. + __func__, entity, *entity_uc,
  28083. + arm_state->videocore_use_count);
  28084. +
  28085. +unlock:
  28086. + write_unlock_bh(&arm_state->susp_res_lock);
  28087. +
  28088. +out:
  28089. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  28090. + return ret;
  28091. +}
  28092. +
  28093. +void
  28094. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  28095. +{
  28096. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28097. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28098. + atomic_inc(&arm_state->ka_use_count);
  28099. + complete(&arm_state->ka_evt);
  28100. +}
  28101. +
  28102. +void
  28103. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  28104. +{
  28105. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28106. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28107. + atomic_inc(&arm_state->ka_release_count);
  28108. + complete(&arm_state->ka_evt);
  28109. +}
  28110. +
  28111. +VCHIQ_STATUS_T
  28112. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  28113. +{
  28114. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  28115. +}
  28116. +
  28117. +VCHIQ_STATUS_T
  28118. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  28119. +{
  28120. + return vchiq_release_internal(service->state, service);
  28121. +}
  28122. +
  28123. +VCHIQ_DEBUGFS_NODE_T *
  28124. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
  28125. +{
  28126. + return &instance->debugfs_node;
  28127. +}
  28128. +
  28129. +int
  28130. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  28131. +{
  28132. + VCHIQ_SERVICE_T *service;
  28133. + int use_count = 0, i;
  28134. + i = 0;
  28135. + while ((service = next_service_by_instance(instance->state,
  28136. + instance, &i)) != NULL) {
  28137. + use_count += service->service_use_count;
  28138. + unlock_service(service);
  28139. + }
  28140. + return use_count;
  28141. +}
  28142. +
  28143. +int
  28144. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
  28145. +{
  28146. + return instance->pid;
  28147. +}
  28148. +
  28149. +int
  28150. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
  28151. +{
  28152. + return instance->trace;
  28153. +}
  28154. +
  28155. +void
  28156. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
  28157. +{
  28158. + VCHIQ_SERVICE_T *service;
  28159. + int i;
  28160. + i = 0;
  28161. + while ((service = next_service_by_instance(instance->state,
  28162. + instance, &i)) != NULL) {
  28163. + service->trace = trace;
  28164. + unlock_service(service);
  28165. + }
  28166. + instance->trace = (trace != 0);
  28167. +}
  28168. +
  28169. +static void suspend_timer_callback(unsigned long context)
  28170. +{
  28171. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  28172. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28173. + if (!arm_state)
  28174. + goto out;
  28175. + vchiq_log_info(vchiq_susp_log_level,
  28176. + "%s - suspend timer expired - check suspend", __func__);
  28177. + vchiq_check_suspend(state);
  28178. +out:
  28179. + return;
  28180. +}
  28181. +
  28182. +VCHIQ_STATUS_T
  28183. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  28184. +{
  28185. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28186. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28187. + if (service) {
  28188. + ret = vchiq_use_internal(service->state, service,
  28189. + USE_TYPE_SERVICE_NO_RESUME);
  28190. + unlock_service(service);
  28191. + }
  28192. + return ret;
  28193. +}
  28194. +
  28195. +VCHIQ_STATUS_T
  28196. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  28197. +{
  28198. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28199. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28200. + if (service) {
  28201. + ret = vchiq_use_internal(service->state, service,
  28202. + USE_TYPE_SERVICE);
  28203. + unlock_service(service);
  28204. + }
  28205. + return ret;
  28206. +}
  28207. +
  28208. +VCHIQ_STATUS_T
  28209. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  28210. +{
  28211. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28212. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28213. + if (service) {
  28214. + ret = vchiq_release_internal(service->state, service);
  28215. + unlock_service(service);
  28216. + }
  28217. + return ret;
  28218. +}
  28219. +
  28220. +void
  28221. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  28222. +{
  28223. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28224. + int i, j = 0;
  28225. + /* Only dump 64 services */
  28226. + static const int local_max_services = 64;
  28227. + /* If there's more than 64 services, only dump ones with
  28228. + * non-zero counts */
  28229. + int only_nonzero = 0;
  28230. + static const char *nz = "<-- preventing suspend";
  28231. +
  28232. + enum vc_suspend_status vc_suspend_state;
  28233. + enum vc_resume_status vc_resume_state;
  28234. + int peer_count;
  28235. + int vc_use_count;
  28236. + int active_services;
  28237. + struct service_data_struct {
  28238. + int fourcc;
  28239. + int clientid;
  28240. + int use_count;
  28241. + } service_data[local_max_services];
  28242. +
  28243. + if (!arm_state)
  28244. + return;
  28245. +
  28246. + read_lock_bh(&arm_state->susp_res_lock);
  28247. + vc_suspend_state = arm_state->vc_suspend_state;
  28248. + vc_resume_state = arm_state->vc_resume_state;
  28249. + peer_count = arm_state->peer_use_count;
  28250. + vc_use_count = arm_state->videocore_use_count;
  28251. + active_services = state->unused_service;
  28252. + if (active_services > local_max_services)
  28253. + only_nonzero = 1;
  28254. +
  28255. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  28256. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  28257. + if (!service_ptr)
  28258. + continue;
  28259. +
  28260. + if (only_nonzero && !service_ptr->service_use_count)
  28261. + continue;
  28262. +
  28263. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  28264. + service_data[j].fourcc = service_ptr->base.fourcc;
  28265. + service_data[j].clientid = service_ptr->client_id;
  28266. + service_data[j++].use_count = service_ptr->
  28267. + service_use_count;
  28268. + }
  28269. + }
  28270. +
  28271. + read_unlock_bh(&arm_state->susp_res_lock);
  28272. +
  28273. + vchiq_log_warning(vchiq_susp_log_level,
  28274. + "-- Videcore suspend state: %s --",
  28275. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  28276. + vchiq_log_warning(vchiq_susp_log_level,
  28277. + "-- Videcore resume state: %s --",
  28278. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  28279. +
  28280. + if (only_nonzero)
  28281. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  28282. + "services (%d). Only dumping up to first %d services "
  28283. + "with non-zero use-count", active_services,
  28284. + local_max_services);
  28285. +
  28286. + for (i = 0; i < j; i++) {
  28287. + vchiq_log_warning(vchiq_susp_log_level,
  28288. + "----- %c%c%c%c:%d service count %d %s",
  28289. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  28290. + service_data[i].clientid,
  28291. + service_data[i].use_count,
  28292. + service_data[i].use_count ? nz : "");
  28293. + }
  28294. + vchiq_log_warning(vchiq_susp_log_level,
  28295. + "----- VCHIQ use count count %d", peer_count);
  28296. + vchiq_log_warning(vchiq_susp_log_level,
  28297. + "--- Overall vchiq instance use count %d", vc_use_count);
  28298. +
  28299. + vchiq_dump_platform_use_state(state);
  28300. +}
  28301. +
  28302. +VCHIQ_STATUS_T
  28303. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  28304. +{
  28305. + VCHIQ_ARM_STATE_T *arm_state;
  28306. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28307. +
  28308. + if (!service || !service->state)
  28309. + goto out;
  28310. +
  28311. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28312. +
  28313. + arm_state = vchiq_platform_get_arm_state(service->state);
  28314. +
  28315. + read_lock_bh(&arm_state->susp_res_lock);
  28316. + if (service->service_use_count)
  28317. + ret = VCHIQ_SUCCESS;
  28318. + read_unlock_bh(&arm_state->susp_res_lock);
  28319. +
  28320. + if (ret == VCHIQ_ERROR) {
  28321. + vchiq_log_error(vchiq_susp_log_level,
  28322. + "%s ERROR - %c%c%c%c:%d service count %d, "
  28323. + "state count %d, videocore suspend state %s", __func__,
  28324. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  28325. + service->client_id, service->service_use_count,
  28326. + arm_state->videocore_use_count,
  28327. + suspend_state_names[arm_state->vc_suspend_state +
  28328. + VC_SUSPEND_NUM_OFFSET]);
  28329. + vchiq_dump_service_use_state(service->state);
  28330. + }
  28331. +out:
  28332. + return ret;
  28333. +}
  28334. +
  28335. +/* stub functions */
  28336. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  28337. +{
  28338. + (void)state;
  28339. +}
  28340. +
  28341. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  28342. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  28343. +{
  28344. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28345. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  28346. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  28347. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  28348. + write_lock_bh(&arm_state->susp_res_lock);
  28349. + if (!arm_state->first_connect) {
  28350. + char threadname[10];
  28351. + arm_state->first_connect = 1;
  28352. + write_unlock_bh(&arm_state->susp_res_lock);
  28353. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  28354. + state->id);
  28355. + arm_state->ka_thread = kthread_create(
  28356. + &vchiq_keepalive_thread_func,
  28357. + (void *)state,
  28358. + threadname);
  28359. + if (arm_state->ka_thread == NULL) {
  28360. + vchiq_log_error(vchiq_susp_log_level,
  28361. + "vchiq: FATAL: couldn't create thread %s",
  28362. + threadname);
  28363. + } else {
  28364. + wake_up_process(arm_state->ka_thread);
  28365. + }
  28366. + } else
  28367. + write_unlock_bh(&arm_state->susp_res_lock);
  28368. + }
  28369. +}
  28370. +
  28371. +
  28372. +/****************************************************************************
  28373. +*
  28374. +* vchiq_init - called when the module is loaded.
  28375. +*
  28376. +***************************************************************************/
  28377. +
  28378. +static int __init
  28379. +vchiq_init(void)
  28380. +{
  28381. + int err;
  28382. + void *ptr_err;
  28383. +
  28384. + /* create debugfs entries */
  28385. + err = vchiq_debugfs_init();
  28386. + if (err != 0)
  28387. + goto failed_debugfs_init;
  28388. +
  28389. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  28390. + if (err != 0) {
  28391. + vchiq_log_error(vchiq_arm_log_level,
  28392. + "Unable to allocate device number");
  28393. + goto failed_alloc_chrdev;
  28394. + }
  28395. + cdev_init(&vchiq_cdev, &vchiq_fops);
  28396. + vchiq_cdev.owner = THIS_MODULE;
  28397. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  28398. + if (err != 0) {
  28399. + vchiq_log_error(vchiq_arm_log_level,
  28400. + "Unable to register device");
  28401. + goto failed_cdev_add;
  28402. + }
  28403. +
  28404. + /* create sysfs entries */
  28405. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  28406. + ptr_err = vchiq_class;
  28407. + if (IS_ERR(ptr_err))
  28408. + goto failed_class_create;
  28409. +
  28410. + vchiq_dev = device_create(vchiq_class, NULL,
  28411. + vchiq_devid, NULL, "vchiq");
  28412. + ptr_err = vchiq_dev;
  28413. + if (IS_ERR(ptr_err))
  28414. + goto failed_device_create;
  28415. +
  28416. + err = vchiq_platform_init(&g_state);
  28417. + if (err != 0)
  28418. + goto failed_platform_init;
  28419. +
  28420. + vchiq_log_info(vchiq_arm_log_level,
  28421. + "vchiq: initialised - version %d (min %d), device %d.%d",
  28422. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  28423. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  28424. +
  28425. + return 0;
  28426. +
  28427. +failed_platform_init:
  28428. + device_destroy(vchiq_class, vchiq_devid);
  28429. +failed_device_create:
  28430. + class_destroy(vchiq_class);
  28431. +failed_class_create:
  28432. + cdev_del(&vchiq_cdev);
  28433. + err = PTR_ERR(ptr_err);
  28434. +failed_cdev_add:
  28435. + unregister_chrdev_region(vchiq_devid, 1);
  28436. +failed_alloc_chrdev:
  28437. + vchiq_debugfs_deinit();
  28438. +failed_debugfs_init:
  28439. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  28440. + return err;
  28441. +}
  28442. +
  28443. +/****************************************************************************
  28444. +*
  28445. +* vchiq_exit - called when the module is unloaded.
  28446. +*
  28447. +***************************************************************************/
  28448. +
  28449. +static void __exit
  28450. +vchiq_exit(void)
  28451. +{
  28452. + vchiq_platform_exit(&g_state);
  28453. + device_destroy(vchiq_class, vchiq_devid);
  28454. + class_destroy(vchiq_class);
  28455. + cdev_del(&vchiq_cdev);
  28456. + unregister_chrdev_region(vchiq_devid, 1);
  28457. +}
  28458. +
  28459. +module_init(vchiq_init);
  28460. +module_exit(vchiq_exit);
  28461. +MODULE_LICENSE("GPL");
  28462. +MODULE_AUTHOR("Broadcom Corporation");
  28463. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  28464. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 1969-12-31 18:00:00.000000000 -0600
  28465. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 2014-12-11 14:02:53.548418001 -0600
  28466. @@ -0,0 +1,223 @@
  28467. +/**
  28468. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  28469. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28470. + *
  28471. + * Redistribution and use in source and binary forms, with or without
  28472. + * modification, are permitted provided that the following conditions
  28473. + * are met:
  28474. + * 1. Redistributions of source code must retain the above copyright
  28475. + * notice, this list of conditions, and the following disclaimer,
  28476. + * without modification.
  28477. + * 2. Redistributions in binary form must reproduce the above copyright
  28478. + * notice, this list of conditions and the following disclaimer in the
  28479. + * documentation and/or other materials provided with the distribution.
  28480. + * 3. The names of the above-listed copyright holders may not be used
  28481. + * to endorse or promote products derived from this software without
  28482. + * specific prior written permission.
  28483. + *
  28484. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28485. + * GNU General Public License ("GPL") version 2, as published by the Free
  28486. + * Software Foundation.
  28487. + *
  28488. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28489. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28490. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28491. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28492. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28493. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28494. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28495. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28496. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28497. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28498. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28499. + */
  28500. +
  28501. +#ifndef VCHIQ_ARM_H
  28502. +#define VCHIQ_ARM_H
  28503. +
  28504. +#include <linux/mutex.h>
  28505. +#include <linux/semaphore.h>
  28506. +#include <linux/atomic.h>
  28507. +#include "vchiq_core.h"
  28508. +#include "vchiq_debugfs.h"
  28509. +
  28510. +
  28511. +enum vc_suspend_status {
  28512. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  28513. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  28514. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  28515. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  28516. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  28517. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  28518. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  28519. +};
  28520. +
  28521. +enum vc_resume_status {
  28522. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  28523. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  28524. + VC_RESUME_REQUESTED, /* User has requested resume */
  28525. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  28526. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  28527. +};
  28528. +
  28529. +
  28530. +enum USE_TYPE_E {
  28531. + USE_TYPE_SERVICE,
  28532. + USE_TYPE_SERVICE_NO_RESUME,
  28533. + USE_TYPE_VCHIQ
  28534. +};
  28535. +
  28536. +
  28537. +
  28538. +typedef struct vchiq_arm_state_struct {
  28539. + /* Keepalive-related data */
  28540. + struct task_struct *ka_thread;
  28541. + struct completion ka_evt;
  28542. + atomic_t ka_use_count;
  28543. + atomic_t ka_use_ack_count;
  28544. + atomic_t ka_release_count;
  28545. +
  28546. + struct completion vc_suspend_complete;
  28547. + struct completion vc_resume_complete;
  28548. +
  28549. + rwlock_t susp_res_lock;
  28550. + enum vc_suspend_status vc_suspend_state;
  28551. + enum vc_resume_status vc_resume_state;
  28552. +
  28553. + unsigned int wake_address;
  28554. +
  28555. + struct timer_list suspend_timer;
  28556. + int suspend_timer_timeout;
  28557. + int suspend_timer_running;
  28558. +
  28559. + /* Global use count for videocore.
  28560. + ** This is equal to the sum of the use counts for all services. When
  28561. + ** this hits zero the videocore suspend procedure will be initiated.
  28562. + */
  28563. + int videocore_use_count;
  28564. +
  28565. + /* Use count to track requests from videocore peer.
  28566. + ** This use count is not associated with a service, so needs to be
  28567. + ** tracked separately with the state.
  28568. + */
  28569. + int peer_use_count;
  28570. +
  28571. + /* Flag to indicate whether resume is blocked. This happens when the
  28572. + ** ARM is suspending
  28573. + */
  28574. + struct completion resume_blocker;
  28575. + int resume_blocked;
  28576. + struct completion blocked_blocker;
  28577. + int blocked_count;
  28578. +
  28579. + int autosuspend_override;
  28580. +
  28581. + /* Flag to indicate that the first vchiq connect has made it through.
  28582. + ** This means that both sides should be fully ready, and we should
  28583. + ** be able to suspend after this point.
  28584. + */
  28585. + int first_connect;
  28586. +
  28587. + unsigned long long suspend_start_time;
  28588. + unsigned long long sleep_start_time;
  28589. + unsigned long long resume_start_time;
  28590. + unsigned long long last_wake_time;
  28591. +
  28592. +} VCHIQ_ARM_STATE_T;
  28593. +
  28594. +extern int vchiq_arm_log_level;
  28595. +extern int vchiq_susp_log_level;
  28596. +
  28597. +extern int __init
  28598. +vchiq_platform_init(VCHIQ_STATE_T *state);
  28599. +
  28600. +extern void __exit
  28601. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  28602. +
  28603. +extern VCHIQ_STATE_T *
  28604. +vchiq_get_state(void);
  28605. +
  28606. +extern VCHIQ_STATUS_T
  28607. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  28608. +
  28609. +extern VCHIQ_STATUS_T
  28610. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  28611. +
  28612. +extern int
  28613. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  28614. +
  28615. +extern VCHIQ_STATUS_T
  28616. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  28617. +
  28618. +extern VCHIQ_STATUS_T
  28619. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  28620. +
  28621. +extern int
  28622. +vchiq_check_resume(VCHIQ_STATE_T *state);
  28623. +
  28624. +extern void
  28625. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  28626. + VCHIQ_STATUS_T
  28627. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  28628. +
  28629. +extern VCHIQ_STATUS_T
  28630. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  28631. +
  28632. +extern VCHIQ_STATUS_T
  28633. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  28634. +
  28635. +extern VCHIQ_STATUS_T
  28636. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  28637. +
  28638. +extern int
  28639. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  28640. +
  28641. +extern int
  28642. +vchiq_platform_use_suspend_timer(void);
  28643. +
  28644. +extern void
  28645. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  28646. +
  28647. +extern void
  28648. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  28649. +
  28650. +extern VCHIQ_ARM_STATE_T*
  28651. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  28652. +
  28653. +extern int
  28654. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  28655. +
  28656. +extern VCHIQ_STATUS_T
  28657. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  28658. + enum USE_TYPE_E use_type);
  28659. +extern VCHIQ_STATUS_T
  28660. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  28661. +
  28662. +extern VCHIQ_DEBUGFS_NODE_T *
  28663. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
  28664. +
  28665. +extern int
  28666. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
  28667. +
  28668. +extern int
  28669. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
  28670. +
  28671. +extern int
  28672. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
  28673. +
  28674. +extern void
  28675. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
  28676. +
  28677. +extern void
  28678. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  28679. + enum vc_suspend_status new_state);
  28680. +
  28681. +extern void
  28682. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  28683. + enum vc_resume_status new_state);
  28684. +
  28685. +extern void
  28686. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  28687. +
  28688. +
  28689. +#endif /* VCHIQ_ARM_H */
  28690. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  28691. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 1969-12-31 18:00:00.000000000 -0600
  28692. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 2014-12-11 14:02:53.548418001 -0600
  28693. @@ -0,0 +1,37 @@
  28694. +/**
  28695. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28696. + *
  28697. + * Redistribution and use in source and binary forms, with or without
  28698. + * modification, are permitted provided that the following conditions
  28699. + * are met:
  28700. + * 1. Redistributions of source code must retain the above copyright
  28701. + * notice, this list of conditions, and the following disclaimer,
  28702. + * without modification.
  28703. + * 2. Redistributions in binary form must reproduce the above copyright
  28704. + * notice, this list of conditions and the following disclaimer in the
  28705. + * documentation and/or other materials provided with the distribution.
  28706. + * 3. The names of the above-listed copyright holders may not be used
  28707. + * to endorse or promote products derived from this software without
  28708. + * specific prior written permission.
  28709. + *
  28710. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28711. + * GNU General Public License ("GPL") version 2, as published by the Free
  28712. + * Software Foundation.
  28713. + *
  28714. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28715. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28716. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28717. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28718. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28719. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28720. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28721. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28722. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28723. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28724. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28725. + */
  28726. +
  28727. +const char *vchiq_get_build_hostname(void);
  28728. +const char *vchiq_get_build_version(void);
  28729. +const char *vchiq_get_build_time(void);
  28730. +const char *vchiq_get_build_date(void);
  28731. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  28732. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 1969-12-31 18:00:00.000000000 -0600
  28733. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 2014-12-11 14:02:53.548418001 -0600
  28734. @@ -0,0 +1,66 @@
  28735. +/**
  28736. + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
  28737. + *
  28738. + * Redistribution and use in source and binary forms, with or without
  28739. + * modification, are permitted provided that the following conditions
  28740. + * are met:
  28741. + * 1. Redistributions of source code must retain the above copyright
  28742. + * notice, this list of conditions, and the following disclaimer,
  28743. + * without modification.
  28744. + * 2. Redistributions in binary form must reproduce the above copyright
  28745. + * notice, this list of conditions and the following disclaimer in the
  28746. + * documentation and/or other materials provided with the distribution.
  28747. + * 3. The names of the above-listed copyright holders may not be used
  28748. + * to endorse or promote products derived from this software without
  28749. + * specific prior written permission.
  28750. + *
  28751. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28752. + * GNU General Public License ("GPL") version 2, as published by the Free
  28753. + * Software Foundation.
  28754. + *
  28755. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28756. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28757. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28758. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28759. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28760. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28761. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28762. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28763. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28764. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28765. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28766. + */
  28767. +
  28768. +#ifndef VCHIQ_CFG_H
  28769. +#define VCHIQ_CFG_H
  28770. +
  28771. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  28772. +/* The version of VCHIQ - change with any non-trivial change */
  28773. +#define VCHIQ_VERSION 7
  28774. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  28775. +** incompatible change */
  28776. +#define VCHIQ_VERSION_MIN 3
  28777. +
  28778. +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
  28779. +#define VCHIQ_VERSION_LIB_VERSION 7
  28780. +
  28781. +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
  28782. +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
  28783. +
  28784. +#define VCHIQ_MAX_STATES 1
  28785. +#define VCHIQ_MAX_SERVICES 4096
  28786. +#define VCHIQ_MAX_SLOTS 128
  28787. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  28788. +
  28789. +#define VCHIQ_NUM_CURRENT_BULKS 32
  28790. +#define VCHIQ_NUM_SERVICE_BULKS 4
  28791. +
  28792. +#ifndef VCHIQ_ENABLE_DEBUG
  28793. +#define VCHIQ_ENABLE_DEBUG 1
  28794. +#endif
  28795. +
  28796. +#ifndef VCHIQ_ENABLE_STATS
  28797. +#define VCHIQ_ENABLE_STATS 1
  28798. +#endif
  28799. +
  28800. +#endif /* VCHIQ_CFG_H */
  28801. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  28802. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 1969-12-31 18:00:00.000000000 -0600
  28803. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 2014-12-11 14:02:53.548418001 -0600
  28804. @@ -0,0 +1,120 @@
  28805. +/**
  28806. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28807. + *
  28808. + * Redistribution and use in source and binary forms, with or without
  28809. + * modification, are permitted provided that the following conditions
  28810. + * are met:
  28811. + * 1. Redistributions of source code must retain the above copyright
  28812. + * notice, this list of conditions, and the following disclaimer,
  28813. + * without modification.
  28814. + * 2. Redistributions in binary form must reproduce the above copyright
  28815. + * notice, this list of conditions and the following disclaimer in the
  28816. + * documentation and/or other materials provided with the distribution.
  28817. + * 3. The names of the above-listed copyright holders may not be used
  28818. + * to endorse or promote products derived from this software without
  28819. + * specific prior written permission.
  28820. + *
  28821. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28822. + * GNU General Public License ("GPL") version 2, as published by the Free
  28823. + * Software Foundation.
  28824. + *
  28825. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28826. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28827. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28828. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28829. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28830. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28831. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28832. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28833. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28834. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28835. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28836. + */
  28837. +
  28838. +#include "vchiq_connected.h"
  28839. +#include "vchiq_core.h"
  28840. +#include "vchiq_killable.h"
  28841. +#include <linux/module.h>
  28842. +#include <linux/mutex.h>
  28843. +
  28844. +#define MAX_CALLBACKS 10
  28845. +
  28846. +static int g_connected;
  28847. +static int g_num_deferred_callbacks;
  28848. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  28849. +static int g_once_init;
  28850. +static struct mutex g_connected_mutex;
  28851. +
  28852. +/****************************************************************************
  28853. +*
  28854. +* Function to initialize our lock.
  28855. +*
  28856. +***************************************************************************/
  28857. +
  28858. +static void connected_init(void)
  28859. +{
  28860. + if (!g_once_init) {
  28861. + mutex_init(&g_connected_mutex);
  28862. + g_once_init = 1;
  28863. + }
  28864. +}
  28865. +
  28866. +/****************************************************************************
  28867. +*
  28868. +* This function is used to defer initialization until the vchiq stack is
  28869. +* initialized. If the stack is already initialized, then the callback will
  28870. +* be made immediately, otherwise it will be deferred until
  28871. +* vchiq_call_connected_callbacks is called.
  28872. +*
  28873. +***************************************************************************/
  28874. +
  28875. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  28876. +{
  28877. + connected_init();
  28878. +
  28879. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  28880. + return;
  28881. +
  28882. + if (g_connected)
  28883. + /* We're already connected. Call the callback immediately. */
  28884. +
  28885. + callback();
  28886. + else {
  28887. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  28888. + vchiq_log_error(vchiq_core_log_level,
  28889. + "There already %d callback registered - "
  28890. + "please increase MAX_CALLBACKS",
  28891. + g_num_deferred_callbacks);
  28892. + else {
  28893. + g_deferred_callback[g_num_deferred_callbacks] =
  28894. + callback;
  28895. + g_num_deferred_callbacks++;
  28896. + }
  28897. + }
  28898. + mutex_unlock(&g_connected_mutex);
  28899. +}
  28900. +
  28901. +/****************************************************************************
  28902. +*
  28903. +* This function is called by the vchiq stack once it has been connected to
  28904. +* the videocore and clients can start to use the stack.
  28905. +*
  28906. +***************************************************************************/
  28907. +
  28908. +void vchiq_call_connected_callbacks(void)
  28909. +{
  28910. + int i;
  28911. +
  28912. + connected_init();
  28913. +
  28914. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  28915. + return;
  28916. +
  28917. + for (i = 0; i < g_num_deferred_callbacks; i++)
  28918. + g_deferred_callback[i]();
  28919. +
  28920. + g_num_deferred_callbacks = 0;
  28921. + g_connected = 1;
  28922. + mutex_unlock(&g_connected_mutex);
  28923. +}
  28924. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  28925. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  28926. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 1969-12-31 18:00:00.000000000 -0600
  28927. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 2014-12-11 14:02:53.548418001 -0600
  28928. @@ -0,0 +1,50 @@
  28929. +/**
  28930. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28931. + *
  28932. + * Redistribution and use in source and binary forms, with or without
  28933. + * modification, are permitted provided that the following conditions
  28934. + * are met:
  28935. + * 1. Redistributions of source code must retain the above copyright
  28936. + * notice, this list of conditions, and the following disclaimer,
  28937. + * without modification.
  28938. + * 2. Redistributions in binary form must reproduce the above copyright
  28939. + * notice, this list of conditions and the following disclaimer in the
  28940. + * documentation and/or other materials provided with the distribution.
  28941. + * 3. The names of the above-listed copyright holders may not be used
  28942. + * to endorse or promote products derived from this software without
  28943. + * specific prior written permission.
  28944. + *
  28945. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28946. + * GNU General Public License ("GPL") version 2, as published by the Free
  28947. + * Software Foundation.
  28948. + *
  28949. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28950. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28951. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28952. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28953. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28954. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28955. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28956. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28957. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28958. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28959. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28960. + */
  28961. +
  28962. +#ifndef VCHIQ_CONNECTED_H
  28963. +#define VCHIQ_CONNECTED_H
  28964. +
  28965. +/* ---- Include Files ----------------------------------------------------- */
  28966. +
  28967. +/* ---- Constants and Types ---------------------------------------------- */
  28968. +
  28969. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  28970. +
  28971. +/* ---- Variable Externs ------------------------------------------------- */
  28972. +
  28973. +/* ---- Function Prototypes ---------------------------------------------- */
  28974. +
  28975. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  28976. +void vchiq_call_connected_callbacks(void);
  28977. +
  28978. +#endif /* VCHIQ_CONNECTED_H */
  28979. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  28980. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 1969-12-31 18:00:00.000000000 -0600
  28981. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 2014-12-11 14:02:53.548418001 -0600
  28982. @@ -0,0 +1,3862 @@
  28983. +/**
  28984. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28985. + *
  28986. + * Redistribution and use in source and binary forms, with or without
  28987. + * modification, are permitted provided that the following conditions
  28988. + * are met:
  28989. + * 1. Redistributions of source code must retain the above copyright
  28990. + * notice, this list of conditions, and the following disclaimer,
  28991. + * without modification.
  28992. + * 2. Redistributions in binary form must reproduce the above copyright
  28993. + * notice, this list of conditions and the following disclaimer in the
  28994. + * documentation and/or other materials provided with the distribution.
  28995. + * 3. The names of the above-listed copyright holders may not be used
  28996. + * to endorse or promote products derived from this software without
  28997. + * specific prior written permission.
  28998. + *
  28999. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29000. + * GNU General Public License ("GPL") version 2, as published by the Free
  29001. + * Software Foundation.
  29002. + *
  29003. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29004. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29005. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29006. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29007. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29008. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29009. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29010. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29011. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29012. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29013. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29014. + */
  29015. +
  29016. +#include "vchiq_core.h"
  29017. +#include "vchiq_killable.h"
  29018. +
  29019. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  29020. +
  29021. +#define HANDLE_STATE_SHIFT 12
  29022. +
  29023. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  29024. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  29025. +#define SLOT_INDEX_FROM_DATA(state, data) \
  29026. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  29027. + VCHIQ_SLOT_SIZE)
  29028. +#define SLOT_INDEX_FROM_INFO(state, info) \
  29029. + ((unsigned int)(info - state->slot_info))
  29030. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  29031. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  29032. +
  29033. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  29034. +
  29035. +#define SRVTRACE_LEVEL(srv) \
  29036. + (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
  29037. +#define SRVTRACE_ENABLED(srv, lev) \
  29038. + (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
  29039. +
  29040. +struct vchiq_open_payload {
  29041. + int fourcc;
  29042. + int client_id;
  29043. + short version;
  29044. + short version_min;
  29045. +};
  29046. +
  29047. +struct vchiq_openack_payload {
  29048. + short version;
  29049. +};
  29050. +
  29051. +/* we require this for consistency between endpoints */
  29052. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  29053. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  29054. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  29055. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  29056. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  29057. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  29058. +
  29059. +/* Run time control of log level, based on KERN_XXX level. */
  29060. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  29061. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  29062. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  29063. +
  29064. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  29065. +
  29066. +static DEFINE_SPINLOCK(service_spinlock);
  29067. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  29068. +DEFINE_SPINLOCK(quota_spinlock);
  29069. +
  29070. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  29071. +static unsigned int handle_seq;
  29072. +
  29073. +static const char *const srvstate_names[] = {
  29074. + "FREE",
  29075. + "HIDDEN",
  29076. + "LISTENING",
  29077. + "OPENING",
  29078. + "OPEN",
  29079. + "OPENSYNC",
  29080. + "CLOSESENT",
  29081. + "CLOSERECVD",
  29082. + "CLOSEWAIT",
  29083. + "CLOSED"
  29084. +};
  29085. +
  29086. +static const char *const reason_names[] = {
  29087. + "SERVICE_OPENED",
  29088. + "SERVICE_CLOSED",
  29089. + "MESSAGE_AVAILABLE",
  29090. + "BULK_TRANSMIT_DONE",
  29091. + "BULK_RECEIVE_DONE",
  29092. + "BULK_TRANSMIT_ABORTED",
  29093. + "BULK_RECEIVE_ABORTED"
  29094. +};
  29095. +
  29096. +static const char *const conn_state_names[] = {
  29097. + "DISCONNECTED",
  29098. + "CONNECTING",
  29099. + "CONNECTED",
  29100. + "PAUSING",
  29101. + "PAUSE_SENT",
  29102. + "PAUSED",
  29103. + "RESUMING",
  29104. + "PAUSE_TIMEOUT",
  29105. + "RESUME_TIMEOUT"
  29106. +};
  29107. +
  29108. +
  29109. +static void
  29110. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  29111. +
  29112. +static const char *msg_type_str(unsigned int msg_type)
  29113. +{
  29114. + switch (msg_type) {
  29115. + case VCHIQ_MSG_PADDING: return "PADDING";
  29116. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  29117. + case VCHIQ_MSG_OPEN: return "OPEN";
  29118. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  29119. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  29120. + case VCHIQ_MSG_DATA: return "DATA";
  29121. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  29122. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  29123. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  29124. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  29125. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  29126. + case VCHIQ_MSG_RESUME: return "RESUME";
  29127. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  29128. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  29129. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  29130. + }
  29131. + return "???";
  29132. +}
  29133. +
  29134. +static inline void
  29135. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  29136. +{
  29137. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  29138. + service->state->id, service->localport,
  29139. + srvstate_names[service->srvstate],
  29140. + srvstate_names[newstate]);
  29141. + service->srvstate = newstate;
  29142. +}
  29143. +
  29144. +VCHIQ_SERVICE_T *
  29145. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  29146. +{
  29147. + VCHIQ_SERVICE_T *service;
  29148. +
  29149. + spin_lock(&service_spinlock);
  29150. + service = handle_to_service(handle);
  29151. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  29152. + (service->handle == handle)) {
  29153. + BUG_ON(service->ref_count == 0);
  29154. + service->ref_count++;
  29155. + } else
  29156. + service = NULL;
  29157. + spin_unlock(&service_spinlock);
  29158. +
  29159. + if (!service)
  29160. + vchiq_log_info(vchiq_core_log_level,
  29161. + "Invalid service handle 0x%x", handle);
  29162. +
  29163. + return service;
  29164. +}
  29165. +
  29166. +VCHIQ_SERVICE_T *
  29167. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  29168. +{
  29169. + VCHIQ_SERVICE_T *service = NULL;
  29170. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  29171. + spin_lock(&service_spinlock);
  29172. + service = state->services[localport];
  29173. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  29174. + BUG_ON(service->ref_count == 0);
  29175. + service->ref_count++;
  29176. + } else
  29177. + service = NULL;
  29178. + spin_unlock(&service_spinlock);
  29179. + }
  29180. +
  29181. + if (!service)
  29182. + vchiq_log_info(vchiq_core_log_level,
  29183. + "Invalid port %d", localport);
  29184. +
  29185. + return service;
  29186. +}
  29187. +
  29188. +VCHIQ_SERVICE_T *
  29189. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  29190. + VCHIQ_SERVICE_HANDLE_T handle) {
  29191. + VCHIQ_SERVICE_T *service;
  29192. +
  29193. + spin_lock(&service_spinlock);
  29194. + service = handle_to_service(handle);
  29195. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  29196. + (service->handle == handle) &&
  29197. + (service->instance == instance)) {
  29198. + BUG_ON(service->ref_count == 0);
  29199. + service->ref_count++;
  29200. + } else
  29201. + service = NULL;
  29202. + spin_unlock(&service_spinlock);
  29203. +
  29204. + if (!service)
  29205. + vchiq_log_info(vchiq_core_log_level,
  29206. + "Invalid service handle 0x%x", handle);
  29207. +
  29208. + return service;
  29209. +}
  29210. +
  29211. +VCHIQ_SERVICE_T *
  29212. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  29213. + VCHIQ_SERVICE_HANDLE_T handle) {
  29214. + VCHIQ_SERVICE_T *service;
  29215. +
  29216. + spin_lock(&service_spinlock);
  29217. + service = handle_to_service(handle);
  29218. + if (service &&
  29219. + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  29220. + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
  29221. + (service->handle == handle) &&
  29222. + (service->instance == instance)) {
  29223. + BUG_ON(service->ref_count == 0);
  29224. + service->ref_count++;
  29225. + } else
  29226. + service = NULL;
  29227. + spin_unlock(&service_spinlock);
  29228. +
  29229. + if (!service)
  29230. + vchiq_log_info(vchiq_core_log_level,
  29231. + "Invalid service handle 0x%x", handle);
  29232. +
  29233. + return service;
  29234. +}
  29235. +
  29236. +VCHIQ_SERVICE_T *
  29237. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  29238. + int *pidx)
  29239. +{
  29240. + VCHIQ_SERVICE_T *service = NULL;
  29241. + int idx = *pidx;
  29242. +
  29243. + spin_lock(&service_spinlock);
  29244. + while (idx < state->unused_service) {
  29245. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  29246. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  29247. + (srv->instance == instance)) {
  29248. + service = srv;
  29249. + BUG_ON(service->ref_count == 0);
  29250. + service->ref_count++;
  29251. + break;
  29252. + }
  29253. + }
  29254. + spin_unlock(&service_spinlock);
  29255. +
  29256. + *pidx = idx;
  29257. +
  29258. + return service;
  29259. +}
  29260. +
  29261. +void
  29262. +lock_service(VCHIQ_SERVICE_T *service)
  29263. +{
  29264. + spin_lock(&service_spinlock);
  29265. + BUG_ON(!service || (service->ref_count == 0));
  29266. + if (service)
  29267. + service->ref_count++;
  29268. + spin_unlock(&service_spinlock);
  29269. +}
  29270. +
  29271. +void
  29272. +unlock_service(VCHIQ_SERVICE_T *service)
  29273. +{
  29274. + VCHIQ_STATE_T *state = service->state;
  29275. + spin_lock(&service_spinlock);
  29276. + BUG_ON(!service || (service->ref_count == 0));
  29277. + if (service && service->ref_count) {
  29278. + service->ref_count--;
  29279. + if (!service->ref_count) {
  29280. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  29281. + state->services[service->localport] = NULL;
  29282. + } else
  29283. + service = NULL;
  29284. + }
  29285. + spin_unlock(&service_spinlock);
  29286. +
  29287. + if (service && service->userdata_term)
  29288. + service->userdata_term(service->base.userdata);
  29289. +
  29290. + kfree(service);
  29291. +}
  29292. +
  29293. +int
  29294. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  29295. +{
  29296. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  29297. + int id;
  29298. +
  29299. + id = service ? service->client_id : 0;
  29300. + if (service)
  29301. + unlock_service(service);
  29302. +
  29303. + return id;
  29304. +}
  29305. +
  29306. +void *
  29307. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  29308. +{
  29309. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  29310. +
  29311. + return service ? service->base.userdata : NULL;
  29312. +}
  29313. +
  29314. +int
  29315. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  29316. +{
  29317. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  29318. +
  29319. + return service ? service->base.fourcc : 0;
  29320. +}
  29321. +
  29322. +static void
  29323. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  29324. +{
  29325. + VCHIQ_STATE_T *state = service->state;
  29326. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  29327. +
  29328. + service->closing = 1;
  29329. +
  29330. + /* Synchronise with other threads. */
  29331. + mutex_lock(&state->recycle_mutex);
  29332. + mutex_unlock(&state->recycle_mutex);
  29333. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  29334. + /* If we're pausing then the slot_mutex is held until resume
  29335. + * by the slot handler. Therefore don't try to acquire this
  29336. + * mutex if we're the slot handler and in the pause sent state.
  29337. + * We don't need to in this case anyway. */
  29338. + mutex_lock(&state->slot_mutex);
  29339. + mutex_unlock(&state->slot_mutex);
  29340. + }
  29341. +
  29342. + /* Unblock any sending thread. */
  29343. + service_quota = &state->service_quotas[service->localport];
  29344. + up(&service_quota->quota_event);
  29345. +}
  29346. +
  29347. +static void
  29348. +mark_service_closing(VCHIQ_SERVICE_T *service)
  29349. +{
  29350. + mark_service_closing_internal(service, 0);
  29351. +}
  29352. +
  29353. +static inline VCHIQ_STATUS_T
  29354. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  29355. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  29356. +{
  29357. + VCHIQ_STATUS_T status;
  29358. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  29359. + service->state->id, service->localport, reason_names[reason],
  29360. + (unsigned int)header, (unsigned int)bulk_userdata);
  29361. + status = service->base.callback(reason, header, service->handle,
  29362. + bulk_userdata);
  29363. + if (status == VCHIQ_ERROR) {
  29364. + vchiq_log_warning(vchiq_core_log_level,
  29365. + "%d: ignoring ERROR from callback to service %x",
  29366. + service->state->id, service->handle);
  29367. + status = VCHIQ_SUCCESS;
  29368. + }
  29369. + return status;
  29370. +}
  29371. +
  29372. +inline void
  29373. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  29374. +{
  29375. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  29376. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  29377. + conn_state_names[oldstate],
  29378. + conn_state_names[newstate]);
  29379. + state->conn_state = newstate;
  29380. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  29381. +}
  29382. +
  29383. +static inline void
  29384. +remote_event_create(REMOTE_EVENT_T *event)
  29385. +{
  29386. + event->armed = 0;
  29387. + /* Don't clear the 'fired' flag because it may already have been set
  29388. + ** by the other side. */
  29389. + sema_init(event->event, 0);
  29390. +}
  29391. +
  29392. +static inline void
  29393. +remote_event_destroy(REMOTE_EVENT_T *event)
  29394. +{
  29395. + (void)event;
  29396. +}
  29397. +
  29398. +static inline int
  29399. +remote_event_wait(REMOTE_EVENT_T *event)
  29400. +{
  29401. + if (!event->fired) {
  29402. + event->armed = 1;
  29403. + dsb();
  29404. + if (!event->fired) {
  29405. + if (down_interruptible(event->event) != 0) {
  29406. + event->armed = 0;
  29407. + return 0;
  29408. + }
  29409. + }
  29410. + event->armed = 0;
  29411. + wmb();
  29412. + }
  29413. +
  29414. + event->fired = 0;
  29415. + return 1;
  29416. +}
  29417. +
  29418. +static inline void
  29419. +remote_event_signal_local(REMOTE_EVENT_T *event)
  29420. +{
  29421. + event->armed = 0;
  29422. + up(event->event);
  29423. +}
  29424. +
  29425. +static inline void
  29426. +remote_event_poll(REMOTE_EVENT_T *event)
  29427. +{
  29428. + if (event->fired && event->armed)
  29429. + remote_event_signal_local(event);
  29430. +}
  29431. +
  29432. +void
  29433. +remote_event_pollall(VCHIQ_STATE_T *state)
  29434. +{
  29435. + remote_event_poll(&state->local->sync_trigger);
  29436. + remote_event_poll(&state->local->sync_release);
  29437. + remote_event_poll(&state->local->trigger);
  29438. + remote_event_poll(&state->local->recycle);
  29439. +}
  29440. +
  29441. +/* Round up message sizes so that any space at the end of a slot is always big
  29442. +** enough for a header. This relies on header size being a power of two, which
  29443. +** has been verified earlier by a static assertion. */
  29444. +
  29445. +static inline unsigned int
  29446. +calc_stride(unsigned int size)
  29447. +{
  29448. + /* Allow room for the header */
  29449. + size += sizeof(VCHIQ_HEADER_T);
  29450. +
  29451. + /* Round up */
  29452. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  29453. + - 1);
  29454. +}
  29455. +
  29456. +/* Called by the slot handler thread */
  29457. +static VCHIQ_SERVICE_T *
  29458. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  29459. +{
  29460. + int i;
  29461. +
  29462. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  29463. +
  29464. + for (i = 0; i < state->unused_service; i++) {
  29465. + VCHIQ_SERVICE_T *service = state->services[i];
  29466. + if (service &&
  29467. + (service->public_fourcc == fourcc) &&
  29468. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  29469. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  29470. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  29471. + lock_service(service);
  29472. + return service;
  29473. + }
  29474. + }
  29475. +
  29476. + return NULL;
  29477. +}
  29478. +
  29479. +/* Called by the slot handler thread */
  29480. +static VCHIQ_SERVICE_T *
  29481. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  29482. +{
  29483. + int i;
  29484. + for (i = 0; i < state->unused_service; i++) {
  29485. + VCHIQ_SERVICE_T *service = state->services[i];
  29486. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  29487. + && (service->remoteport == port)) {
  29488. + lock_service(service);
  29489. + return service;
  29490. + }
  29491. + }
  29492. + return NULL;
  29493. +}
  29494. +
  29495. +inline void
  29496. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  29497. +{
  29498. + uint32_t value;
  29499. +
  29500. + if (service) {
  29501. + do {
  29502. + value = atomic_read(&service->poll_flags);
  29503. + } while (atomic_cmpxchg(&service->poll_flags, value,
  29504. + value | (1 << poll_type)) != value);
  29505. +
  29506. + do {
  29507. + value = atomic_read(&state->poll_services[
  29508. + service->localport>>5]);
  29509. + } while (atomic_cmpxchg(
  29510. + &state->poll_services[service->localport>>5],
  29511. + value, value | (1 << (service->localport & 0x1f)))
  29512. + != value);
  29513. + }
  29514. +
  29515. + state->poll_needed = 1;
  29516. + wmb();
  29517. +
  29518. + /* ... and ensure the slot handler runs. */
  29519. + remote_event_signal_local(&state->local->trigger);
  29520. +}
  29521. +
  29522. +/* Called from queue_message, by the slot handler and application threads,
  29523. +** with slot_mutex held */
  29524. +static VCHIQ_HEADER_T *
  29525. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  29526. +{
  29527. + VCHIQ_SHARED_STATE_T *local = state->local;
  29528. + int tx_pos = state->local_tx_pos;
  29529. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  29530. +
  29531. + if (space > slot_space) {
  29532. + VCHIQ_HEADER_T *header;
  29533. + /* Fill the remaining space with padding */
  29534. + WARN_ON(state->tx_data == NULL);
  29535. + header = (VCHIQ_HEADER_T *)
  29536. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  29537. + header->msgid = VCHIQ_MSGID_PADDING;
  29538. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  29539. +
  29540. + tx_pos += slot_space;
  29541. + }
  29542. +
  29543. + /* If necessary, get the next slot. */
  29544. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  29545. + int slot_index;
  29546. +
  29547. + /* If there is no free slot... */
  29548. +
  29549. + if (down_trylock(&state->slot_available_event) != 0) {
  29550. + /* ...wait for one. */
  29551. +
  29552. + VCHIQ_STATS_INC(state, slot_stalls);
  29553. +
  29554. + /* But first, flush through the last slot. */
  29555. + state->local_tx_pos = tx_pos;
  29556. + local->tx_pos = tx_pos;
  29557. + remote_event_signal(&state->remote->trigger);
  29558. +
  29559. + if (!is_blocking ||
  29560. + (down_interruptible(
  29561. + &state->slot_available_event) != 0))
  29562. + return NULL; /* No space available */
  29563. + }
  29564. +
  29565. + BUG_ON(tx_pos ==
  29566. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  29567. +
  29568. + slot_index = local->slot_queue[
  29569. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  29570. + VCHIQ_SLOT_QUEUE_MASK];
  29571. + state->tx_data =
  29572. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  29573. + }
  29574. +
  29575. + state->local_tx_pos = tx_pos + space;
  29576. +
  29577. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  29578. +}
  29579. +
  29580. +/* Called by the recycle thread. */
  29581. +static void
  29582. +process_free_queue(VCHIQ_STATE_T *state)
  29583. +{
  29584. + VCHIQ_SHARED_STATE_T *local = state->local;
  29585. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  29586. + int slot_queue_available;
  29587. +
  29588. + /* Use a read memory barrier to ensure that any state that may have
  29589. + ** been modified by another thread is not masked by stale prefetched
  29590. + ** values. */
  29591. + rmb();
  29592. +
  29593. + /* Find slots which have been freed by the other side, and return them
  29594. + ** to the available queue. */
  29595. + slot_queue_available = state->slot_queue_available;
  29596. +
  29597. + while (slot_queue_available != local->slot_queue_recycle) {
  29598. + unsigned int pos;
  29599. + int slot_index = local->slot_queue[slot_queue_available++ &
  29600. + VCHIQ_SLOT_QUEUE_MASK];
  29601. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  29602. + int data_found = 0;
  29603. +
  29604. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  29605. + state->id, slot_index, (unsigned int)data,
  29606. + local->slot_queue_recycle, slot_queue_available);
  29607. +
  29608. + /* Initialise the bitmask for services which have used this
  29609. + ** slot */
  29610. + BITSET_ZERO(service_found);
  29611. +
  29612. + pos = 0;
  29613. +
  29614. + while (pos < VCHIQ_SLOT_SIZE) {
  29615. + VCHIQ_HEADER_T *header =
  29616. + (VCHIQ_HEADER_T *)(data + pos);
  29617. + int msgid = header->msgid;
  29618. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  29619. + int port = VCHIQ_MSG_SRCPORT(msgid);
  29620. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  29621. + &state->service_quotas[port];
  29622. + int count;
  29623. + spin_lock(&quota_spinlock);
  29624. + count = service_quota->message_use_count;
  29625. + if (count > 0)
  29626. + service_quota->message_use_count =
  29627. + count - 1;
  29628. + spin_unlock(&quota_spinlock);
  29629. +
  29630. + if (count == service_quota->message_quota)
  29631. + /* Signal the service that it
  29632. + ** has dropped below its quota
  29633. + */
  29634. + up(&service_quota->quota_event);
  29635. + else if (count == 0) {
  29636. + vchiq_log_error(vchiq_core_log_level,
  29637. + "service %d "
  29638. + "message_use_count=%d "
  29639. + "(header %x, msgid %x, "
  29640. + "header->msgid %x, "
  29641. + "header->size %x)",
  29642. + port,
  29643. + service_quota->
  29644. + message_use_count,
  29645. + (unsigned int)header, msgid,
  29646. + header->msgid,
  29647. + header->size);
  29648. + WARN(1, "invalid message use count\n");
  29649. + }
  29650. + if (!BITSET_IS_SET(service_found, port)) {
  29651. + /* Set the found bit for this service */
  29652. + BITSET_SET(service_found, port);
  29653. +
  29654. + spin_lock(&quota_spinlock);
  29655. + count = service_quota->slot_use_count;
  29656. + if (count > 0)
  29657. + service_quota->slot_use_count =
  29658. + count - 1;
  29659. + spin_unlock(&quota_spinlock);
  29660. +
  29661. + if (count > 0) {
  29662. + /* Signal the service in case
  29663. + ** it has dropped below its
  29664. + ** quota */
  29665. + up(&service_quota->quota_event);
  29666. + vchiq_log_trace(
  29667. + vchiq_core_log_level,
  29668. + "%d: pfq:%d %x@%x - "
  29669. + "slot_use->%d",
  29670. + state->id, port,
  29671. + header->size,
  29672. + (unsigned int)header,
  29673. + count - 1);
  29674. + } else {
  29675. + vchiq_log_error(
  29676. + vchiq_core_log_level,
  29677. + "service %d "
  29678. + "slot_use_count"
  29679. + "=%d (header %x"
  29680. + ", msgid %x, "
  29681. + "header->msgid"
  29682. + " %x, header->"
  29683. + "size %x)",
  29684. + port, count,
  29685. + (unsigned int)header,
  29686. + msgid,
  29687. + header->msgid,
  29688. + header->size);
  29689. + WARN(1, "bad slot use count\n");
  29690. + }
  29691. + }
  29692. +
  29693. + data_found = 1;
  29694. + }
  29695. +
  29696. + pos += calc_stride(header->size);
  29697. + if (pos > VCHIQ_SLOT_SIZE) {
  29698. + vchiq_log_error(vchiq_core_log_level,
  29699. + "pfq - pos %x: header %x, msgid %x, "
  29700. + "header->msgid %x, header->size %x",
  29701. + pos, (unsigned int)header, msgid,
  29702. + header->msgid, header->size);
  29703. + WARN(1, "invalid slot position\n");
  29704. + }
  29705. + }
  29706. +
  29707. + if (data_found) {
  29708. + int count;
  29709. + spin_lock(&quota_spinlock);
  29710. + count = state->data_use_count;
  29711. + if (count > 0)
  29712. + state->data_use_count =
  29713. + count - 1;
  29714. + spin_unlock(&quota_spinlock);
  29715. + if (count == state->data_quota)
  29716. + up(&state->data_quota_event);
  29717. + }
  29718. +
  29719. + state->slot_queue_available = slot_queue_available;
  29720. + up(&state->slot_available_event);
  29721. + }
  29722. +}
  29723. +
  29724. +/* Called by the slot handler and application threads */
  29725. +static VCHIQ_STATUS_T
  29726. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  29727. + int msgid, const VCHIQ_ELEMENT_T *elements,
  29728. + int count, int size, int is_blocking)
  29729. +{
  29730. + VCHIQ_SHARED_STATE_T *local;
  29731. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  29732. + VCHIQ_HEADER_T *header;
  29733. + int type = VCHIQ_MSG_TYPE(msgid);
  29734. +
  29735. + unsigned int stride;
  29736. +
  29737. + local = state->local;
  29738. +
  29739. + stride = calc_stride(size);
  29740. +
  29741. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  29742. +
  29743. + if ((type != VCHIQ_MSG_RESUME) &&
  29744. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  29745. + return VCHIQ_RETRY;
  29746. +
  29747. + if (type == VCHIQ_MSG_DATA) {
  29748. + int tx_end_index;
  29749. +
  29750. + BUG_ON(!service);
  29751. +
  29752. + if (service->closing) {
  29753. + /* The service has been closed */
  29754. + mutex_unlock(&state->slot_mutex);
  29755. + return VCHIQ_ERROR;
  29756. + }
  29757. +
  29758. + service_quota = &state->service_quotas[service->localport];
  29759. +
  29760. + spin_lock(&quota_spinlock);
  29761. +
  29762. + /* Ensure this service doesn't use more than its quota of
  29763. + ** messages or slots */
  29764. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  29765. + state->local_tx_pos + stride - 1);
  29766. +
  29767. + /* Ensure data messages don't use more than their quota of
  29768. + ** slots */
  29769. + while ((tx_end_index != state->previous_data_index) &&
  29770. + (state->data_use_count == state->data_quota)) {
  29771. + VCHIQ_STATS_INC(state, data_stalls);
  29772. + spin_unlock(&quota_spinlock);
  29773. + mutex_unlock(&state->slot_mutex);
  29774. +
  29775. + if (down_interruptible(&state->data_quota_event)
  29776. + != 0)
  29777. + return VCHIQ_RETRY;
  29778. +
  29779. + mutex_lock(&state->slot_mutex);
  29780. + spin_lock(&quota_spinlock);
  29781. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  29782. + state->local_tx_pos + stride - 1);
  29783. + if ((tx_end_index == state->previous_data_index) ||
  29784. + (state->data_use_count < state->data_quota)) {
  29785. + /* Pass the signal on to other waiters */
  29786. + up(&state->data_quota_event);
  29787. + break;
  29788. + }
  29789. + }
  29790. +
  29791. + while ((service_quota->message_use_count ==
  29792. + service_quota->message_quota) ||
  29793. + ((tx_end_index != service_quota->previous_tx_index) &&
  29794. + (service_quota->slot_use_count ==
  29795. + service_quota->slot_quota))) {
  29796. + spin_unlock(&quota_spinlock);
  29797. + vchiq_log_trace(vchiq_core_log_level,
  29798. + "%d: qm:%d %s,%x - quota stall "
  29799. + "(msg %d, slot %d)",
  29800. + state->id, service->localport,
  29801. + msg_type_str(type), size,
  29802. + service_quota->message_use_count,
  29803. + service_quota->slot_use_count);
  29804. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  29805. + mutex_unlock(&state->slot_mutex);
  29806. + if (down_interruptible(&service_quota->quota_event)
  29807. + != 0)
  29808. + return VCHIQ_RETRY;
  29809. + if (service->closing)
  29810. + return VCHIQ_ERROR;
  29811. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  29812. + return VCHIQ_RETRY;
  29813. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  29814. + /* The service has been closed */
  29815. + mutex_unlock(&state->slot_mutex);
  29816. + return VCHIQ_ERROR;
  29817. + }
  29818. + spin_lock(&quota_spinlock);
  29819. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  29820. + state->local_tx_pos + stride - 1);
  29821. + }
  29822. +
  29823. + spin_unlock(&quota_spinlock);
  29824. + }
  29825. +
  29826. + header = reserve_space(state, stride, is_blocking);
  29827. +
  29828. + if (!header) {
  29829. + if (service)
  29830. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  29831. + mutex_unlock(&state->slot_mutex);
  29832. + return VCHIQ_RETRY;
  29833. + }
  29834. +
  29835. + if (type == VCHIQ_MSG_DATA) {
  29836. + int i, pos;
  29837. + int tx_end_index;
  29838. + int slot_use_count;
  29839. +
  29840. + vchiq_log_info(vchiq_core_log_level,
  29841. + "%d: qm %s@%x,%x (%d->%d)",
  29842. + state->id,
  29843. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29844. + (unsigned int)header, size,
  29845. + VCHIQ_MSG_SRCPORT(msgid),
  29846. + VCHIQ_MSG_DSTPORT(msgid));
  29847. +
  29848. + BUG_ON(!service);
  29849. +
  29850. + for (i = 0, pos = 0; i < (unsigned int)count;
  29851. + pos += elements[i++].size)
  29852. + if (elements[i].size) {
  29853. + if (vchiq_copy_from_user
  29854. + (header->data + pos, elements[i].data,
  29855. + (size_t) elements[i].size) !=
  29856. + VCHIQ_SUCCESS) {
  29857. + mutex_unlock(&state->slot_mutex);
  29858. + VCHIQ_SERVICE_STATS_INC(service,
  29859. + error_count);
  29860. + return VCHIQ_ERROR;
  29861. + }
  29862. + if (i == 0) {
  29863. + if (SRVTRACE_ENABLED(service,
  29864. + VCHIQ_LOG_INFO))
  29865. + vchiq_log_dump_mem("Sent", 0,
  29866. + header->data + pos,
  29867. + min(64u,
  29868. + elements[0].size));
  29869. + }
  29870. + }
  29871. +
  29872. + spin_lock(&quota_spinlock);
  29873. + service_quota->message_use_count++;
  29874. +
  29875. + tx_end_index =
  29876. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  29877. +
  29878. + /* If this transmission can't fit in the last slot used by any
  29879. + ** service, the data_use_count must be increased. */
  29880. + if (tx_end_index != state->previous_data_index) {
  29881. + state->previous_data_index = tx_end_index;
  29882. + state->data_use_count++;
  29883. + }
  29884. +
  29885. + /* If this isn't the same slot last used by this service,
  29886. + ** the service's slot_use_count must be increased. */
  29887. + if (tx_end_index != service_quota->previous_tx_index) {
  29888. + service_quota->previous_tx_index = tx_end_index;
  29889. + slot_use_count = ++service_quota->slot_use_count;
  29890. + } else {
  29891. + slot_use_count = 0;
  29892. + }
  29893. +
  29894. + spin_unlock(&quota_spinlock);
  29895. +
  29896. + if (slot_use_count)
  29897. + vchiq_log_trace(vchiq_core_log_level,
  29898. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  29899. + state->id, service->localport,
  29900. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  29901. + slot_use_count, header);
  29902. +
  29903. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  29904. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  29905. + } else {
  29906. + vchiq_log_info(vchiq_core_log_level,
  29907. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  29908. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29909. + (unsigned int)header, size,
  29910. + VCHIQ_MSG_SRCPORT(msgid),
  29911. + VCHIQ_MSG_DSTPORT(msgid));
  29912. + if (size != 0) {
  29913. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  29914. + memcpy(header->data, elements[0].data,
  29915. + elements[0].size);
  29916. + }
  29917. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  29918. + }
  29919. +
  29920. + header->msgid = msgid;
  29921. + header->size = size;
  29922. +
  29923. + {
  29924. + int svc_fourcc;
  29925. +
  29926. + svc_fourcc = service
  29927. + ? service->base.fourcc
  29928. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  29929. +
  29930. + vchiq_log_info(SRVTRACE_LEVEL(service),
  29931. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  29932. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29933. + VCHIQ_MSG_TYPE(msgid),
  29934. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  29935. + VCHIQ_MSG_SRCPORT(msgid),
  29936. + VCHIQ_MSG_DSTPORT(msgid),
  29937. + size);
  29938. + }
  29939. +
  29940. + /* Make sure the new header is visible to the peer. */
  29941. + wmb();
  29942. +
  29943. + /* Make the new tx_pos visible to the peer. */
  29944. + local->tx_pos = state->local_tx_pos;
  29945. + wmb();
  29946. +
  29947. + if (service && (type == VCHIQ_MSG_CLOSE))
  29948. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  29949. +
  29950. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  29951. + mutex_unlock(&state->slot_mutex);
  29952. +
  29953. + remote_event_signal(&state->remote->trigger);
  29954. +
  29955. + return VCHIQ_SUCCESS;
  29956. +}
  29957. +
  29958. +/* Called by the slot handler and application threads */
  29959. +static VCHIQ_STATUS_T
  29960. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  29961. + int msgid, const VCHIQ_ELEMENT_T *elements,
  29962. + int count, int size, int is_blocking)
  29963. +{
  29964. + VCHIQ_SHARED_STATE_T *local;
  29965. + VCHIQ_HEADER_T *header;
  29966. +
  29967. + local = state->local;
  29968. +
  29969. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  29970. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  29971. + return VCHIQ_RETRY;
  29972. +
  29973. + remote_event_wait(&local->sync_release);
  29974. +
  29975. + rmb();
  29976. +
  29977. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  29978. + local->slot_sync);
  29979. +
  29980. + {
  29981. + int oldmsgid = header->msgid;
  29982. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  29983. + vchiq_log_error(vchiq_core_log_level,
  29984. + "%d: qms - msgid %x, not PADDING",
  29985. + state->id, oldmsgid);
  29986. + }
  29987. +
  29988. + if (service) {
  29989. + int i, pos;
  29990. +
  29991. + vchiq_log_info(vchiq_sync_log_level,
  29992. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  29993. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29994. + (unsigned int)header, size,
  29995. + VCHIQ_MSG_SRCPORT(msgid),
  29996. + VCHIQ_MSG_DSTPORT(msgid));
  29997. +
  29998. + for (i = 0, pos = 0; i < (unsigned int)count;
  29999. + pos += elements[i++].size)
  30000. + if (elements[i].size) {
  30001. + if (vchiq_copy_from_user
  30002. + (header->data + pos, elements[i].data,
  30003. + (size_t) elements[i].size) !=
  30004. + VCHIQ_SUCCESS) {
  30005. + mutex_unlock(&state->sync_mutex);
  30006. + VCHIQ_SERVICE_STATS_INC(service,
  30007. + error_count);
  30008. + return VCHIQ_ERROR;
  30009. + }
  30010. + if (i == 0) {
  30011. + if (vchiq_sync_log_level >=
  30012. + VCHIQ_LOG_TRACE)
  30013. + vchiq_log_dump_mem("Sent Sync",
  30014. + 0, header->data + pos,
  30015. + min(64u,
  30016. + elements[0].size));
  30017. + }
  30018. + }
  30019. +
  30020. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  30021. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  30022. + } else {
  30023. + vchiq_log_info(vchiq_sync_log_level,
  30024. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  30025. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30026. + (unsigned int)header, size,
  30027. + VCHIQ_MSG_SRCPORT(msgid),
  30028. + VCHIQ_MSG_DSTPORT(msgid));
  30029. + if (size != 0) {
  30030. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  30031. + memcpy(header->data, elements[0].data,
  30032. + elements[0].size);
  30033. + }
  30034. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  30035. + }
  30036. +
  30037. + header->size = size;
  30038. + header->msgid = msgid;
  30039. +
  30040. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  30041. + int svc_fourcc;
  30042. +
  30043. + svc_fourcc = service
  30044. + ? service->base.fourcc
  30045. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  30046. +
  30047. + vchiq_log_trace(vchiq_sync_log_level,
  30048. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  30049. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30050. + VCHIQ_MSG_TYPE(msgid),
  30051. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  30052. + VCHIQ_MSG_SRCPORT(msgid),
  30053. + VCHIQ_MSG_DSTPORT(msgid),
  30054. + size);
  30055. + }
  30056. +
  30057. + /* Make sure the new header is visible to the peer. */
  30058. + wmb();
  30059. +
  30060. + remote_event_signal(&state->remote->sync_trigger);
  30061. +
  30062. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  30063. + mutex_unlock(&state->sync_mutex);
  30064. +
  30065. + return VCHIQ_SUCCESS;
  30066. +}
  30067. +
  30068. +static inline void
  30069. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  30070. +{
  30071. + slot->use_count++;
  30072. +}
  30073. +
  30074. +static void
  30075. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  30076. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  30077. +{
  30078. + int release_count;
  30079. +
  30080. + mutex_lock(&state->recycle_mutex);
  30081. +
  30082. + if (header) {
  30083. + int msgid = header->msgid;
  30084. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  30085. + (service && service->closing)) {
  30086. + mutex_unlock(&state->recycle_mutex);
  30087. + return;
  30088. + }
  30089. +
  30090. + /* Rewrite the message header to prevent a double
  30091. + ** release */
  30092. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  30093. + }
  30094. +
  30095. + release_count = slot_info->release_count;
  30096. + slot_info->release_count = ++release_count;
  30097. +
  30098. + if (release_count == slot_info->use_count) {
  30099. + int slot_queue_recycle;
  30100. + /* Add to the freed queue */
  30101. +
  30102. + /* A read barrier is necessary here to prevent speculative
  30103. + ** fetches of remote->slot_queue_recycle from overtaking the
  30104. + ** mutex. */
  30105. + rmb();
  30106. +
  30107. + slot_queue_recycle = state->remote->slot_queue_recycle;
  30108. + state->remote->slot_queue[slot_queue_recycle &
  30109. + VCHIQ_SLOT_QUEUE_MASK] =
  30110. + SLOT_INDEX_FROM_INFO(state, slot_info);
  30111. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  30112. + vchiq_log_info(vchiq_core_log_level,
  30113. + "%d: release_slot %d - recycle->%x",
  30114. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  30115. + state->remote->slot_queue_recycle);
  30116. +
  30117. + /* A write barrier is necessary, but remote_event_signal
  30118. + ** contains one. */
  30119. + remote_event_signal(&state->remote->recycle);
  30120. + }
  30121. +
  30122. + mutex_unlock(&state->recycle_mutex);
  30123. +}
  30124. +
  30125. +/* Called by the slot handler - don't hold the bulk mutex */
  30126. +static VCHIQ_STATUS_T
  30127. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  30128. + int retry_poll)
  30129. +{
  30130. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  30131. +
  30132. + vchiq_log_trace(vchiq_core_log_level,
  30133. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  30134. + service->state->id, service->localport,
  30135. + (queue == &service->bulk_tx) ? 't' : 'r',
  30136. + queue->process, queue->remote_notify, queue->remove);
  30137. +
  30138. + if (service->state->is_master) {
  30139. + while (queue->remote_notify != queue->process) {
  30140. + VCHIQ_BULK_T *bulk =
  30141. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  30142. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  30143. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  30144. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  30145. + service->remoteport);
  30146. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  30147. + /* Only reply to non-dummy bulk requests */
  30148. + if (bulk->remote_data) {
  30149. + status = queue_message(service->state, NULL,
  30150. + msgid, &element, 1, 4, 0);
  30151. + if (status != VCHIQ_SUCCESS)
  30152. + break;
  30153. + }
  30154. + queue->remote_notify++;
  30155. + }
  30156. + } else {
  30157. + queue->remote_notify = queue->process;
  30158. + }
  30159. +
  30160. + if (status == VCHIQ_SUCCESS) {
  30161. + while (queue->remove != queue->remote_notify) {
  30162. + VCHIQ_BULK_T *bulk =
  30163. + &queue->bulks[BULK_INDEX(queue->remove)];
  30164. +
  30165. + /* Only generate callbacks for non-dummy bulk
  30166. + ** requests, and non-terminated services */
  30167. + if (bulk->data && service->instance) {
  30168. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  30169. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  30170. + VCHIQ_SERVICE_STATS_INC(service,
  30171. + bulk_tx_count);
  30172. + VCHIQ_SERVICE_STATS_ADD(service,
  30173. + bulk_tx_bytes,
  30174. + bulk->actual);
  30175. + } else {
  30176. + VCHIQ_SERVICE_STATS_INC(service,
  30177. + bulk_rx_count);
  30178. + VCHIQ_SERVICE_STATS_ADD(service,
  30179. + bulk_rx_bytes,
  30180. + bulk->actual);
  30181. + }
  30182. + } else {
  30183. + VCHIQ_SERVICE_STATS_INC(service,
  30184. + bulk_aborted_count);
  30185. + }
  30186. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  30187. + struct bulk_waiter *waiter;
  30188. + spin_lock(&bulk_waiter_spinlock);
  30189. + waiter = bulk->userdata;
  30190. + if (waiter) {
  30191. + waiter->actual = bulk->actual;
  30192. + up(&waiter->event);
  30193. + }
  30194. + spin_unlock(&bulk_waiter_spinlock);
  30195. + } else if (bulk->mode ==
  30196. + VCHIQ_BULK_MODE_CALLBACK) {
  30197. + VCHIQ_REASON_T reason = (bulk->dir ==
  30198. + VCHIQ_BULK_TRANSMIT) ?
  30199. + ((bulk->actual ==
  30200. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  30201. + VCHIQ_BULK_TRANSMIT_ABORTED :
  30202. + VCHIQ_BULK_TRANSMIT_DONE) :
  30203. + ((bulk->actual ==
  30204. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  30205. + VCHIQ_BULK_RECEIVE_ABORTED :
  30206. + VCHIQ_BULK_RECEIVE_DONE);
  30207. + status = make_service_callback(service,
  30208. + reason, NULL, bulk->userdata);
  30209. + if (status == VCHIQ_RETRY)
  30210. + break;
  30211. + }
  30212. + }
  30213. +
  30214. + queue->remove++;
  30215. + up(&service->bulk_remove_event);
  30216. + }
  30217. + if (!retry_poll)
  30218. + status = VCHIQ_SUCCESS;
  30219. + }
  30220. +
  30221. + if (status == VCHIQ_RETRY)
  30222. + request_poll(service->state, service,
  30223. + (queue == &service->bulk_tx) ?
  30224. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  30225. +
  30226. + return status;
  30227. +}
  30228. +
  30229. +/* Called by the slot handler thread */
  30230. +static void
  30231. +poll_services(VCHIQ_STATE_T *state)
  30232. +{
  30233. + int group, i;
  30234. +
  30235. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  30236. + uint32_t flags;
  30237. + flags = atomic_xchg(&state->poll_services[group], 0);
  30238. + for (i = 0; flags; i++) {
  30239. + if (flags & (1 << i)) {
  30240. + VCHIQ_SERVICE_T *service =
  30241. + find_service_by_port(state,
  30242. + (group<<5) + i);
  30243. + uint32_t service_flags;
  30244. + flags &= ~(1 << i);
  30245. + if (!service)
  30246. + continue;
  30247. + service_flags =
  30248. + atomic_xchg(&service->poll_flags, 0);
  30249. + if (service_flags &
  30250. + (1 << VCHIQ_POLL_REMOVE)) {
  30251. + vchiq_log_info(vchiq_core_log_level,
  30252. + "%d: ps - remove %d<->%d",
  30253. + state->id, service->localport,
  30254. + service->remoteport);
  30255. +
  30256. + /* Make it look like a client, because
  30257. + it must be removed and not left in
  30258. + the LISTENING state. */
  30259. + service->public_fourcc =
  30260. + VCHIQ_FOURCC_INVALID;
  30261. +
  30262. + if (vchiq_close_service_internal(
  30263. + service, 0/*!close_recvd*/) !=
  30264. + VCHIQ_SUCCESS)
  30265. + request_poll(state, service,
  30266. + VCHIQ_POLL_REMOVE);
  30267. + } else if (service_flags &
  30268. + (1 << VCHIQ_POLL_TERMINATE)) {
  30269. + vchiq_log_info(vchiq_core_log_level,
  30270. + "%d: ps - terminate %d<->%d",
  30271. + state->id, service->localport,
  30272. + service->remoteport);
  30273. + if (vchiq_close_service_internal(
  30274. + service, 0/*!close_recvd*/) !=
  30275. + VCHIQ_SUCCESS)
  30276. + request_poll(state, service,
  30277. + VCHIQ_POLL_TERMINATE);
  30278. + }
  30279. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  30280. + notify_bulks(service,
  30281. + &service->bulk_tx,
  30282. + 1/*retry_poll*/);
  30283. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  30284. + notify_bulks(service,
  30285. + &service->bulk_rx,
  30286. + 1/*retry_poll*/);
  30287. + unlock_service(service);
  30288. + }
  30289. + }
  30290. + }
  30291. +}
  30292. +
  30293. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  30294. +static int
  30295. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  30296. +{
  30297. + VCHIQ_STATE_T *state = service->state;
  30298. + int resolved = 0;
  30299. + int rc;
  30300. +
  30301. + while ((queue->process != queue->local_insert) &&
  30302. + (queue->process != queue->remote_insert)) {
  30303. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  30304. +
  30305. + vchiq_log_trace(vchiq_core_log_level,
  30306. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  30307. + state->id, service->localport,
  30308. + (queue == &service->bulk_tx) ? 't' : 'r',
  30309. + queue->local_insert, queue->remote_insert,
  30310. + queue->process);
  30311. +
  30312. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  30313. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  30314. +
  30315. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  30316. + if (rc != 0)
  30317. + break;
  30318. +
  30319. + vchiq_transfer_bulk(bulk);
  30320. + mutex_unlock(&state->bulk_transfer_mutex);
  30321. +
  30322. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  30323. + const char *header = (queue == &service->bulk_tx) ?
  30324. + "Send Bulk to" : "Recv Bulk from";
  30325. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  30326. + vchiq_log_info(SRVTRACE_LEVEL(service),
  30327. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  30328. + header,
  30329. + VCHIQ_FOURCC_AS_4CHARS(
  30330. + service->base.fourcc),
  30331. + service->remoteport,
  30332. + bulk->size,
  30333. + (unsigned int)bulk->data,
  30334. + (unsigned int)bulk->remote_data);
  30335. + else
  30336. + vchiq_log_info(SRVTRACE_LEVEL(service),
  30337. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  30338. + " rx len:%d %x<->%x",
  30339. + header,
  30340. + VCHIQ_FOURCC_AS_4CHARS(
  30341. + service->base.fourcc),
  30342. + service->remoteport,
  30343. + bulk->size,
  30344. + bulk->remote_size,
  30345. + (unsigned int)bulk->data,
  30346. + (unsigned int)bulk->remote_data);
  30347. + }
  30348. +
  30349. + vchiq_complete_bulk(bulk);
  30350. + queue->process++;
  30351. + resolved++;
  30352. + }
  30353. + return resolved;
  30354. +}
  30355. +
  30356. +/* Called with the bulk_mutex held */
  30357. +static void
  30358. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  30359. +{
  30360. + int is_tx = (queue == &service->bulk_tx);
  30361. + vchiq_log_trace(vchiq_core_log_level,
  30362. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  30363. + service->state->id, service->localport, is_tx ? 't' : 'r',
  30364. + queue->local_insert, queue->remote_insert, queue->process);
  30365. +
  30366. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  30367. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  30368. +
  30369. + while ((queue->process != queue->local_insert) ||
  30370. + (queue->process != queue->remote_insert)) {
  30371. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  30372. +
  30373. + if (queue->process == queue->remote_insert) {
  30374. + /* fabricate a matching dummy bulk */
  30375. + bulk->remote_data = NULL;
  30376. + bulk->remote_size = 0;
  30377. + queue->remote_insert++;
  30378. + }
  30379. +
  30380. + if (queue->process != queue->local_insert) {
  30381. + vchiq_complete_bulk(bulk);
  30382. +
  30383. + vchiq_log_info(SRVTRACE_LEVEL(service),
  30384. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  30385. + "rx len:%d",
  30386. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  30387. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  30388. + service->remoteport,
  30389. + bulk->size,
  30390. + bulk->remote_size);
  30391. + } else {
  30392. + /* fabricate a matching dummy bulk */
  30393. + bulk->data = NULL;
  30394. + bulk->size = 0;
  30395. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  30396. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  30397. + VCHIQ_BULK_RECEIVE;
  30398. + queue->local_insert++;
  30399. + }
  30400. +
  30401. + queue->process++;
  30402. + }
  30403. +}
  30404. +
  30405. +/* Called from the slot handler thread */
  30406. +static void
  30407. +pause_bulks(VCHIQ_STATE_T *state)
  30408. +{
  30409. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  30410. + WARN_ON_ONCE(1);
  30411. + atomic_set(&pause_bulks_count, 1);
  30412. + return;
  30413. + }
  30414. +
  30415. + /* Block bulk transfers from all services */
  30416. + mutex_lock(&state->bulk_transfer_mutex);
  30417. +}
  30418. +
  30419. +/* Called from the slot handler thread */
  30420. +static void
  30421. +resume_bulks(VCHIQ_STATE_T *state)
  30422. +{
  30423. + int i;
  30424. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  30425. + WARN_ON_ONCE(1);
  30426. + atomic_set(&pause_bulks_count, 0);
  30427. + return;
  30428. + }
  30429. +
  30430. + /* Allow bulk transfers from all services */
  30431. + mutex_unlock(&state->bulk_transfer_mutex);
  30432. +
  30433. + if (state->deferred_bulks == 0)
  30434. + return;
  30435. +
  30436. + /* Deal with any bulks which had to be deferred due to being in
  30437. + * paused state. Don't try to match up to number of deferred bulks
  30438. + * in case we've had something come and close the service in the
  30439. + * interim - just process all bulk queues for all services */
  30440. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  30441. + __func__, state->deferred_bulks);
  30442. +
  30443. + for (i = 0; i < state->unused_service; i++) {
  30444. + VCHIQ_SERVICE_T *service = state->services[i];
  30445. + int resolved_rx = 0;
  30446. + int resolved_tx = 0;
  30447. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  30448. + continue;
  30449. +
  30450. + mutex_lock(&service->bulk_mutex);
  30451. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  30452. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  30453. + mutex_unlock(&service->bulk_mutex);
  30454. + if (resolved_rx)
  30455. + notify_bulks(service, &service->bulk_rx, 1);
  30456. + if (resolved_tx)
  30457. + notify_bulks(service, &service->bulk_tx, 1);
  30458. + }
  30459. + state->deferred_bulks = 0;
  30460. +}
  30461. +
  30462. +static int
  30463. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  30464. +{
  30465. + VCHIQ_SERVICE_T *service = NULL;
  30466. + int msgid, size;
  30467. + int type;
  30468. + unsigned int localport, remoteport;
  30469. +
  30470. + msgid = header->msgid;
  30471. + size = header->size;
  30472. + type = VCHIQ_MSG_TYPE(msgid);
  30473. + localport = VCHIQ_MSG_DSTPORT(msgid);
  30474. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  30475. + if (size >= sizeof(struct vchiq_open_payload)) {
  30476. + const struct vchiq_open_payload *payload =
  30477. + (struct vchiq_open_payload *)header->data;
  30478. + unsigned int fourcc;
  30479. +
  30480. + fourcc = payload->fourcc;
  30481. + vchiq_log_info(vchiq_core_log_level,
  30482. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  30483. + state->id, (unsigned int)header,
  30484. + localport,
  30485. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  30486. +
  30487. + service = get_listening_service(state, fourcc);
  30488. +
  30489. + if (service) {
  30490. + /* A matching service exists */
  30491. + short version = payload->version;
  30492. + short version_min = payload->version_min;
  30493. + if ((service->version < version_min) ||
  30494. + (version < service->version_min)) {
  30495. + /* Version mismatch */
  30496. + vchiq_loud_error_header();
  30497. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  30498. + "version mismatch - local (%d, min %d)"
  30499. + " vs. remote (%d, min %d)",
  30500. + state->id, service->localport,
  30501. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  30502. + service->version, service->version_min,
  30503. + version, version_min);
  30504. + vchiq_loud_error_footer();
  30505. + unlock_service(service);
  30506. + service = NULL;
  30507. + goto fail_open;
  30508. + }
  30509. + service->peer_version = version;
  30510. +
  30511. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  30512. + struct vchiq_openack_payload ack_payload = {
  30513. + service->version
  30514. + };
  30515. + VCHIQ_ELEMENT_T body = {
  30516. + &ack_payload,
  30517. + sizeof(ack_payload)
  30518. + };
  30519. +
  30520. + /* Acknowledge the OPEN */
  30521. + if (service->sync) {
  30522. + if (queue_message_sync(state, NULL,
  30523. + VCHIQ_MAKE_MSG(
  30524. + VCHIQ_MSG_OPENACK,
  30525. + service->localport,
  30526. + remoteport),
  30527. + &body, 1, sizeof(ack_payload),
  30528. + 0) == VCHIQ_RETRY)
  30529. + goto bail_not_ready;
  30530. + } else {
  30531. + if (queue_message(state, NULL,
  30532. + VCHIQ_MAKE_MSG(
  30533. + VCHIQ_MSG_OPENACK,
  30534. + service->localport,
  30535. + remoteport),
  30536. + &body, 1, sizeof(ack_payload),
  30537. + 0) == VCHIQ_RETRY)
  30538. + goto bail_not_ready;
  30539. + }
  30540. +
  30541. + /* The service is now open */
  30542. + vchiq_set_service_state(service,
  30543. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  30544. + : VCHIQ_SRVSTATE_OPEN);
  30545. + }
  30546. +
  30547. + service->remoteport = remoteport;
  30548. + service->client_id = ((int *)header->data)[1];
  30549. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  30550. + NULL, NULL) == VCHIQ_RETRY) {
  30551. + /* Bail out if not ready */
  30552. + service->remoteport = VCHIQ_PORT_FREE;
  30553. + goto bail_not_ready;
  30554. + }
  30555. +
  30556. + /* Success - the message has been dealt with */
  30557. + unlock_service(service);
  30558. + return 1;
  30559. + }
  30560. + }
  30561. +
  30562. +fail_open:
  30563. + /* No available service, or an invalid request - send a CLOSE */
  30564. + if (queue_message(state, NULL,
  30565. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  30566. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  30567. + goto bail_not_ready;
  30568. +
  30569. + return 1;
  30570. +
  30571. +bail_not_ready:
  30572. + if (service)
  30573. + unlock_service(service);
  30574. +
  30575. + return 0;
  30576. +}
  30577. +
  30578. +/* Called by the slot handler thread */
  30579. +static void
  30580. +parse_rx_slots(VCHIQ_STATE_T *state)
  30581. +{
  30582. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  30583. + VCHIQ_SERVICE_T *service = NULL;
  30584. + int tx_pos;
  30585. + DEBUG_INITIALISE(state->local)
  30586. +
  30587. + tx_pos = remote->tx_pos;
  30588. +
  30589. + while (state->rx_pos != tx_pos) {
  30590. + VCHIQ_HEADER_T *header;
  30591. + int msgid, size;
  30592. + int type;
  30593. + unsigned int localport, remoteport;
  30594. +
  30595. + DEBUG_TRACE(PARSE_LINE);
  30596. + if (!state->rx_data) {
  30597. + int rx_index;
  30598. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  30599. + rx_index = remote->slot_queue[
  30600. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  30601. + VCHIQ_SLOT_QUEUE_MASK];
  30602. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  30603. + rx_index);
  30604. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  30605. +
  30606. + /* Initialise use_count to one, and increment
  30607. + ** release_count at the end of the slot to avoid
  30608. + ** releasing the slot prematurely. */
  30609. + state->rx_info->use_count = 1;
  30610. + state->rx_info->release_count = 0;
  30611. + }
  30612. +
  30613. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  30614. + (state->rx_pos & VCHIQ_SLOT_MASK));
  30615. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  30616. + msgid = header->msgid;
  30617. + DEBUG_VALUE(PARSE_MSGID, msgid);
  30618. + size = header->size;
  30619. + type = VCHIQ_MSG_TYPE(msgid);
  30620. + localport = VCHIQ_MSG_DSTPORT(msgid);
  30621. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  30622. +
  30623. + if (type != VCHIQ_MSG_DATA)
  30624. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  30625. +
  30626. + switch (type) {
  30627. + case VCHIQ_MSG_OPENACK:
  30628. + case VCHIQ_MSG_CLOSE:
  30629. + case VCHIQ_MSG_DATA:
  30630. + case VCHIQ_MSG_BULK_RX:
  30631. + case VCHIQ_MSG_BULK_TX:
  30632. + case VCHIQ_MSG_BULK_RX_DONE:
  30633. + case VCHIQ_MSG_BULK_TX_DONE:
  30634. + service = find_service_by_port(state, localport);
  30635. + if ((!service ||
  30636. + ((service->remoteport != remoteport) &&
  30637. + (service->remoteport != VCHIQ_PORT_FREE))) &&
  30638. + (localport == 0) &&
  30639. + (type == VCHIQ_MSG_CLOSE)) {
  30640. + /* This could be a CLOSE from a client which
  30641. + hadn't yet received the OPENACK - look for
  30642. + the connected service */
  30643. + if (service)
  30644. + unlock_service(service);
  30645. + service = get_connected_service(state,
  30646. + remoteport);
  30647. + if (service)
  30648. + vchiq_log_warning(vchiq_core_log_level,
  30649. + "%d: prs %s@%x (%d->%d) - "
  30650. + "found connected service %d",
  30651. + state->id, msg_type_str(type),
  30652. + (unsigned int)header,
  30653. + remoteport, localport,
  30654. + service->localport);
  30655. + }
  30656. +
  30657. + if (!service) {
  30658. + vchiq_log_error(vchiq_core_log_level,
  30659. + "%d: prs %s@%x (%d->%d) - "
  30660. + "invalid/closed service %d",
  30661. + state->id, msg_type_str(type),
  30662. + (unsigned int)header,
  30663. + remoteport, localport, localport);
  30664. + goto skip_message;
  30665. + }
  30666. + break;
  30667. + default:
  30668. + break;
  30669. + }
  30670. +
  30671. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  30672. + int svc_fourcc;
  30673. +
  30674. + svc_fourcc = service
  30675. + ? service->base.fourcc
  30676. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  30677. + vchiq_log_info(SRVTRACE_LEVEL(service),
  30678. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  30679. + "len:%d",
  30680. + msg_type_str(type), type,
  30681. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  30682. + remoteport, localport, size);
  30683. + if (size > 0)
  30684. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  30685. + min(64, size));
  30686. + }
  30687. +
  30688. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  30689. + > VCHIQ_SLOT_SIZE) {
  30690. + vchiq_log_error(vchiq_core_log_level,
  30691. + "header %x (msgid %x) - size %x too big for "
  30692. + "slot",
  30693. + (unsigned int)header, (unsigned int)msgid,
  30694. + (unsigned int)size);
  30695. + WARN(1, "oversized for slot\n");
  30696. + }
  30697. +
  30698. + switch (type) {
  30699. + case VCHIQ_MSG_OPEN:
  30700. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  30701. + if (!parse_open(state, header))
  30702. + goto bail_not_ready;
  30703. + break;
  30704. + case VCHIQ_MSG_OPENACK:
  30705. + if (size >= sizeof(struct vchiq_openack_payload)) {
  30706. + const struct vchiq_openack_payload *payload =
  30707. + (struct vchiq_openack_payload *)
  30708. + header->data;
  30709. + service->peer_version = payload->version;
  30710. + }
  30711. + vchiq_log_info(vchiq_core_log_level,
  30712. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  30713. + state->id, (unsigned int)header, size,
  30714. + remoteport, localport, service->peer_version);
  30715. + if (service->srvstate ==
  30716. + VCHIQ_SRVSTATE_OPENING) {
  30717. + service->remoteport = remoteport;
  30718. + vchiq_set_service_state(service,
  30719. + VCHIQ_SRVSTATE_OPEN);
  30720. + up(&service->remove_event);
  30721. + } else
  30722. + vchiq_log_error(vchiq_core_log_level,
  30723. + "OPENACK received in state %s",
  30724. + srvstate_names[service->srvstate]);
  30725. + break;
  30726. + case VCHIQ_MSG_CLOSE:
  30727. + WARN_ON(size != 0); /* There should be no data */
  30728. +
  30729. + vchiq_log_info(vchiq_core_log_level,
  30730. + "%d: prs CLOSE@%x (%d->%d)",
  30731. + state->id, (unsigned int)header,
  30732. + remoteport, localport);
  30733. +
  30734. + mark_service_closing_internal(service, 1);
  30735. +
  30736. + if (vchiq_close_service_internal(service,
  30737. + 1/*close_recvd*/) == VCHIQ_RETRY)
  30738. + goto bail_not_ready;
  30739. +
  30740. + vchiq_log_info(vchiq_core_log_level,
  30741. + "Close Service %c%c%c%c s:%u d:%d",
  30742. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  30743. + service->localport,
  30744. + service->remoteport);
  30745. + break;
  30746. + case VCHIQ_MSG_DATA:
  30747. + vchiq_log_trace(vchiq_core_log_level,
  30748. + "%d: prs DATA@%x,%x (%d->%d)",
  30749. + state->id, (unsigned int)header, size,
  30750. + remoteport, localport);
  30751. +
  30752. + if ((service->remoteport == remoteport)
  30753. + && (service->srvstate ==
  30754. + VCHIQ_SRVSTATE_OPEN)) {
  30755. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  30756. + claim_slot(state->rx_info);
  30757. + DEBUG_TRACE(PARSE_LINE);
  30758. + if (make_service_callback(service,
  30759. + VCHIQ_MESSAGE_AVAILABLE, header,
  30760. + NULL) == VCHIQ_RETRY) {
  30761. + DEBUG_TRACE(PARSE_LINE);
  30762. + goto bail_not_ready;
  30763. + }
  30764. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  30765. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  30766. + size);
  30767. + } else {
  30768. + VCHIQ_STATS_INC(state, error_count);
  30769. + }
  30770. + break;
  30771. + case VCHIQ_MSG_CONNECT:
  30772. + vchiq_log_info(vchiq_core_log_level,
  30773. + "%d: prs CONNECT@%x",
  30774. + state->id, (unsigned int)header);
  30775. + up(&state->connect);
  30776. + break;
  30777. + case VCHIQ_MSG_BULK_RX:
  30778. + case VCHIQ_MSG_BULK_TX: {
  30779. + VCHIQ_BULK_QUEUE_T *queue;
  30780. + WARN_ON(!state->is_master);
  30781. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  30782. + &service->bulk_tx : &service->bulk_rx;
  30783. + if ((service->remoteport == remoteport)
  30784. + && (service->srvstate ==
  30785. + VCHIQ_SRVSTATE_OPEN)) {
  30786. + VCHIQ_BULK_T *bulk;
  30787. + int resolved = 0;
  30788. +
  30789. + DEBUG_TRACE(PARSE_LINE);
  30790. + if (mutex_lock_interruptible(
  30791. + &service->bulk_mutex) != 0) {
  30792. + DEBUG_TRACE(PARSE_LINE);
  30793. + goto bail_not_ready;
  30794. + }
  30795. +
  30796. + WARN_ON(!(queue->remote_insert < queue->remove +
  30797. + VCHIQ_NUM_SERVICE_BULKS));
  30798. + bulk = &queue->bulks[
  30799. + BULK_INDEX(queue->remote_insert)];
  30800. + bulk->remote_data =
  30801. + (void *)((int *)header->data)[0];
  30802. + bulk->remote_size = ((int *)header->data)[1];
  30803. + wmb();
  30804. +
  30805. + vchiq_log_info(vchiq_core_log_level,
  30806. + "%d: prs %s@%x (%d->%d) %x@%x",
  30807. + state->id, msg_type_str(type),
  30808. + (unsigned int)header,
  30809. + remoteport, localport,
  30810. + bulk->remote_size,
  30811. + (unsigned int)bulk->remote_data);
  30812. +
  30813. + queue->remote_insert++;
  30814. +
  30815. + if (atomic_read(&pause_bulks_count)) {
  30816. + state->deferred_bulks++;
  30817. + vchiq_log_info(vchiq_core_log_level,
  30818. + "%s: deferring bulk (%d)",
  30819. + __func__,
  30820. + state->deferred_bulks);
  30821. + if (state->conn_state !=
  30822. + VCHIQ_CONNSTATE_PAUSE_SENT)
  30823. + vchiq_log_error(
  30824. + vchiq_core_log_level,
  30825. + "%s: bulks paused in "
  30826. + "unexpected state %s",
  30827. + __func__,
  30828. + conn_state_names[
  30829. + state->conn_state]);
  30830. + } else if (state->conn_state ==
  30831. + VCHIQ_CONNSTATE_CONNECTED) {
  30832. + DEBUG_TRACE(PARSE_LINE);
  30833. + resolved = resolve_bulks(service,
  30834. + queue);
  30835. + }
  30836. +
  30837. + mutex_unlock(&service->bulk_mutex);
  30838. + if (resolved)
  30839. + notify_bulks(service, queue,
  30840. + 1/*retry_poll*/);
  30841. + }
  30842. + } break;
  30843. + case VCHIQ_MSG_BULK_RX_DONE:
  30844. + case VCHIQ_MSG_BULK_TX_DONE:
  30845. + WARN_ON(state->is_master);
  30846. + if ((service->remoteport == remoteport)
  30847. + && (service->srvstate !=
  30848. + VCHIQ_SRVSTATE_FREE)) {
  30849. + VCHIQ_BULK_QUEUE_T *queue;
  30850. + VCHIQ_BULK_T *bulk;
  30851. +
  30852. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  30853. + &service->bulk_rx : &service->bulk_tx;
  30854. +
  30855. + DEBUG_TRACE(PARSE_LINE);
  30856. + if (mutex_lock_interruptible(
  30857. + &service->bulk_mutex) != 0) {
  30858. + DEBUG_TRACE(PARSE_LINE);
  30859. + goto bail_not_ready;
  30860. + }
  30861. + if ((int)(queue->remote_insert -
  30862. + queue->local_insert) >= 0) {
  30863. + vchiq_log_error(vchiq_core_log_level,
  30864. + "%d: prs %s@%x (%d->%d) "
  30865. + "unexpected (ri=%d,li=%d)",
  30866. + state->id, msg_type_str(type),
  30867. + (unsigned int)header,
  30868. + remoteport, localport,
  30869. + queue->remote_insert,
  30870. + queue->local_insert);
  30871. + mutex_unlock(&service->bulk_mutex);
  30872. + break;
  30873. + }
  30874. +
  30875. + BUG_ON(queue->process == queue->local_insert);
  30876. + BUG_ON(queue->process != queue->remote_insert);
  30877. +
  30878. + bulk = &queue->bulks[
  30879. + BULK_INDEX(queue->remote_insert)];
  30880. + bulk->actual = *(int *)header->data;
  30881. + queue->remote_insert++;
  30882. +
  30883. + vchiq_log_info(vchiq_core_log_level,
  30884. + "%d: prs %s@%x (%d->%d) %x@%x",
  30885. + state->id, msg_type_str(type),
  30886. + (unsigned int)header,
  30887. + remoteport, localport,
  30888. + bulk->actual, (unsigned int)bulk->data);
  30889. +
  30890. + vchiq_log_trace(vchiq_core_log_level,
  30891. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  30892. + state->id, localport,
  30893. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  30894. + 'r' : 't',
  30895. + queue->local_insert,
  30896. + queue->remote_insert, queue->process);
  30897. +
  30898. + DEBUG_TRACE(PARSE_LINE);
  30899. + WARN_ON(queue->process == queue->local_insert);
  30900. + vchiq_complete_bulk(bulk);
  30901. + queue->process++;
  30902. + mutex_unlock(&service->bulk_mutex);
  30903. + DEBUG_TRACE(PARSE_LINE);
  30904. + notify_bulks(service, queue, 1/*retry_poll*/);
  30905. + DEBUG_TRACE(PARSE_LINE);
  30906. + }
  30907. + break;
  30908. + case VCHIQ_MSG_PADDING:
  30909. + vchiq_log_trace(vchiq_core_log_level,
  30910. + "%d: prs PADDING@%x,%x",
  30911. + state->id, (unsigned int)header, size);
  30912. + break;
  30913. + case VCHIQ_MSG_PAUSE:
  30914. + /* If initiated, signal the application thread */
  30915. + vchiq_log_trace(vchiq_core_log_level,
  30916. + "%d: prs PAUSE@%x,%x",
  30917. + state->id, (unsigned int)header, size);
  30918. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  30919. + vchiq_log_error(vchiq_core_log_level,
  30920. + "%d: PAUSE received in state PAUSED",
  30921. + state->id);
  30922. + break;
  30923. + }
  30924. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  30925. + /* Send a PAUSE in response */
  30926. + if (queue_message(state, NULL,
  30927. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  30928. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  30929. + goto bail_not_ready;
  30930. + if (state->is_master)
  30931. + pause_bulks(state);
  30932. + }
  30933. + /* At this point slot_mutex is held */
  30934. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  30935. + vchiq_platform_paused(state);
  30936. + break;
  30937. + case VCHIQ_MSG_RESUME:
  30938. + vchiq_log_trace(vchiq_core_log_level,
  30939. + "%d: prs RESUME@%x,%x",
  30940. + state->id, (unsigned int)header, size);
  30941. + /* Release the slot mutex */
  30942. + mutex_unlock(&state->slot_mutex);
  30943. + if (state->is_master)
  30944. + resume_bulks(state);
  30945. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  30946. + vchiq_platform_resumed(state);
  30947. + break;
  30948. +
  30949. + case VCHIQ_MSG_REMOTE_USE:
  30950. + vchiq_on_remote_use(state);
  30951. + break;
  30952. + case VCHIQ_MSG_REMOTE_RELEASE:
  30953. + vchiq_on_remote_release(state);
  30954. + break;
  30955. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  30956. + vchiq_on_remote_use_active(state);
  30957. + break;
  30958. +
  30959. + default:
  30960. + vchiq_log_error(vchiq_core_log_level,
  30961. + "%d: prs invalid msgid %x@%x,%x",
  30962. + state->id, msgid, (unsigned int)header, size);
  30963. + WARN(1, "invalid message\n");
  30964. + break;
  30965. + }
  30966. +
  30967. +skip_message:
  30968. + if (service) {
  30969. + unlock_service(service);
  30970. + service = NULL;
  30971. + }
  30972. +
  30973. + state->rx_pos += calc_stride(size);
  30974. +
  30975. + DEBUG_TRACE(PARSE_LINE);
  30976. + /* Perform some housekeeping when the end of the slot is
  30977. + ** reached. */
  30978. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  30979. + /* Remove the extra reference count. */
  30980. + release_slot(state, state->rx_info, NULL, NULL);
  30981. + state->rx_data = NULL;
  30982. + }
  30983. + }
  30984. +
  30985. +bail_not_ready:
  30986. + if (service)
  30987. + unlock_service(service);
  30988. +}
  30989. +
  30990. +/* Called by the slot handler thread */
  30991. +static int
  30992. +slot_handler_func(void *v)
  30993. +{
  30994. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  30995. + VCHIQ_SHARED_STATE_T *local = state->local;
  30996. + DEBUG_INITIALISE(local)
  30997. +
  30998. + while (1) {
  30999. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  31000. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  31001. + remote_event_wait(&local->trigger);
  31002. +
  31003. + rmb();
  31004. +
  31005. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  31006. + if (state->poll_needed) {
  31007. + /* Check if we need to suspend - may change our
  31008. + * conn_state */
  31009. + vchiq_platform_check_suspend(state);
  31010. +
  31011. + state->poll_needed = 0;
  31012. +
  31013. + /* Handle service polling and other rare conditions here
  31014. + ** out of the mainline code */
  31015. + switch (state->conn_state) {
  31016. + case VCHIQ_CONNSTATE_CONNECTED:
  31017. + /* Poll the services as requested */
  31018. + poll_services(state);
  31019. + break;
  31020. +
  31021. + case VCHIQ_CONNSTATE_PAUSING:
  31022. + if (state->is_master)
  31023. + pause_bulks(state);
  31024. + if (queue_message(state, NULL,
  31025. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  31026. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  31027. + vchiq_set_conn_state(state,
  31028. + VCHIQ_CONNSTATE_PAUSE_SENT);
  31029. + } else {
  31030. + if (state->is_master)
  31031. + resume_bulks(state);
  31032. + /* Retry later */
  31033. + state->poll_needed = 1;
  31034. + }
  31035. + break;
  31036. +
  31037. + case VCHIQ_CONNSTATE_PAUSED:
  31038. + vchiq_platform_resume(state);
  31039. + break;
  31040. +
  31041. + case VCHIQ_CONNSTATE_RESUMING:
  31042. + if (queue_message(state, NULL,
  31043. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  31044. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  31045. + if (state->is_master)
  31046. + resume_bulks(state);
  31047. + vchiq_set_conn_state(state,
  31048. + VCHIQ_CONNSTATE_CONNECTED);
  31049. + vchiq_platform_resumed(state);
  31050. + } else {
  31051. + /* This should really be impossible,
  31052. + ** since the PAUSE should have flushed
  31053. + ** through outstanding messages. */
  31054. + vchiq_log_error(vchiq_core_log_level,
  31055. + "Failed to send RESUME "
  31056. + "message");
  31057. + BUG();
  31058. + }
  31059. + break;
  31060. +
  31061. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  31062. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  31063. + vchiq_platform_handle_timeout(state);
  31064. + break;
  31065. + default:
  31066. + break;
  31067. + }
  31068. +
  31069. +
  31070. + }
  31071. +
  31072. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  31073. + parse_rx_slots(state);
  31074. + }
  31075. + return 0;
  31076. +}
  31077. +
  31078. +
  31079. +/* Called by the recycle thread */
  31080. +static int
  31081. +recycle_func(void *v)
  31082. +{
  31083. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  31084. + VCHIQ_SHARED_STATE_T *local = state->local;
  31085. +
  31086. + while (1) {
  31087. + remote_event_wait(&local->recycle);
  31088. +
  31089. + process_free_queue(state);
  31090. + }
  31091. + return 0;
  31092. +}
  31093. +
  31094. +
  31095. +/* Called by the sync thread */
  31096. +static int
  31097. +sync_func(void *v)
  31098. +{
  31099. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  31100. + VCHIQ_SHARED_STATE_T *local = state->local;
  31101. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  31102. + state->remote->slot_sync);
  31103. +
  31104. + while (1) {
  31105. + VCHIQ_SERVICE_T *service;
  31106. + int msgid, size;
  31107. + int type;
  31108. + unsigned int localport, remoteport;
  31109. +
  31110. + remote_event_wait(&local->sync_trigger);
  31111. +
  31112. + rmb();
  31113. +
  31114. + msgid = header->msgid;
  31115. + size = header->size;
  31116. + type = VCHIQ_MSG_TYPE(msgid);
  31117. + localport = VCHIQ_MSG_DSTPORT(msgid);
  31118. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  31119. +
  31120. + service = find_service_by_port(state, localport);
  31121. +
  31122. + if (!service) {
  31123. + vchiq_log_error(vchiq_sync_log_level,
  31124. + "%d: sf %s@%x (%d->%d) - "
  31125. + "invalid/closed service %d",
  31126. + state->id, msg_type_str(type),
  31127. + (unsigned int)header,
  31128. + remoteport, localport, localport);
  31129. + release_message_sync(state, header);
  31130. + continue;
  31131. + }
  31132. +
  31133. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  31134. + int svc_fourcc;
  31135. +
  31136. + svc_fourcc = service
  31137. + ? service->base.fourcc
  31138. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  31139. + vchiq_log_trace(vchiq_sync_log_level,
  31140. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  31141. + msg_type_str(type),
  31142. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  31143. + remoteport, localport, size);
  31144. + if (size > 0)
  31145. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  31146. + min(64, size));
  31147. + }
  31148. +
  31149. + switch (type) {
  31150. + case VCHIQ_MSG_OPENACK:
  31151. + if (size >= sizeof(struct vchiq_openack_payload)) {
  31152. + const struct vchiq_openack_payload *payload =
  31153. + (struct vchiq_openack_payload *)
  31154. + header->data;
  31155. + service->peer_version = payload->version;
  31156. + }
  31157. + vchiq_log_info(vchiq_sync_log_level,
  31158. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  31159. + state->id, (unsigned int)header, size,
  31160. + remoteport, localport, service->peer_version);
  31161. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  31162. + service->remoteport = remoteport;
  31163. + vchiq_set_service_state(service,
  31164. + VCHIQ_SRVSTATE_OPENSYNC);
  31165. + up(&service->remove_event);
  31166. + }
  31167. + release_message_sync(state, header);
  31168. + break;
  31169. +
  31170. + case VCHIQ_MSG_DATA:
  31171. + vchiq_log_trace(vchiq_sync_log_level,
  31172. + "%d: sf DATA@%x,%x (%d->%d)",
  31173. + state->id, (unsigned int)header, size,
  31174. + remoteport, localport);
  31175. +
  31176. + if ((service->remoteport == remoteport) &&
  31177. + (service->srvstate ==
  31178. + VCHIQ_SRVSTATE_OPENSYNC)) {
  31179. + if (make_service_callback(service,
  31180. + VCHIQ_MESSAGE_AVAILABLE, header,
  31181. + NULL) == VCHIQ_RETRY)
  31182. + vchiq_log_error(vchiq_sync_log_level,
  31183. + "synchronous callback to "
  31184. + "service %d returns "
  31185. + "VCHIQ_RETRY",
  31186. + localport);
  31187. + }
  31188. + break;
  31189. +
  31190. + default:
  31191. + vchiq_log_error(vchiq_sync_log_level,
  31192. + "%d: sf unexpected msgid %x@%x,%x",
  31193. + state->id, msgid, (unsigned int)header, size);
  31194. + release_message_sync(state, header);
  31195. + break;
  31196. + }
  31197. +
  31198. + unlock_service(service);
  31199. + }
  31200. +
  31201. + return 0;
  31202. +}
  31203. +
  31204. +
  31205. +static void
  31206. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  31207. +{
  31208. + queue->local_insert = 0;
  31209. + queue->remote_insert = 0;
  31210. + queue->process = 0;
  31211. + queue->remote_notify = 0;
  31212. + queue->remove = 0;
  31213. +}
  31214. +
  31215. +
  31216. +inline const char *
  31217. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  31218. +{
  31219. + return conn_state_names[conn_state];
  31220. +}
  31221. +
  31222. +
  31223. +VCHIQ_SLOT_ZERO_T *
  31224. +vchiq_init_slots(void *mem_base, int mem_size)
  31225. +{
  31226. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  31227. + VCHIQ_SLOT_ZERO_T *slot_zero =
  31228. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  31229. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  31230. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  31231. +
  31232. + /* Ensure there is enough memory to run an absolutely minimum system */
  31233. + num_slots -= first_data_slot;
  31234. +
  31235. + if (num_slots < 4) {
  31236. + vchiq_log_error(vchiq_core_log_level,
  31237. + "vchiq_init_slots - insufficient memory %x bytes",
  31238. + mem_size);
  31239. + return NULL;
  31240. + }
  31241. +
  31242. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  31243. +
  31244. + slot_zero->magic = VCHIQ_MAGIC;
  31245. + slot_zero->version = VCHIQ_VERSION;
  31246. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  31247. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  31248. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  31249. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  31250. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  31251. +
  31252. + slot_zero->master.slot_sync = first_data_slot;
  31253. + slot_zero->master.slot_first = first_data_slot + 1;
  31254. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  31255. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  31256. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  31257. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  31258. +
  31259. + return slot_zero;
  31260. +}
  31261. +
  31262. +VCHIQ_STATUS_T
  31263. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  31264. + int is_master)
  31265. +{
  31266. + VCHIQ_SHARED_STATE_T *local;
  31267. + VCHIQ_SHARED_STATE_T *remote;
  31268. + VCHIQ_STATUS_T status;
  31269. + char threadname[10];
  31270. + static int id;
  31271. + int i;
  31272. +
  31273. + vchiq_log_warning(vchiq_core_log_level,
  31274. + "%s: slot_zero = 0x%08lx, is_master = %d",
  31275. + __func__, (unsigned long)slot_zero, is_master);
  31276. +
  31277. + /* Check the input configuration */
  31278. +
  31279. + if (slot_zero->magic != VCHIQ_MAGIC) {
  31280. + vchiq_loud_error_header();
  31281. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  31282. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  31283. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  31284. + vchiq_loud_error_footer();
  31285. + return VCHIQ_ERROR;
  31286. + }
  31287. +
  31288. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  31289. + vchiq_loud_error_header();
  31290. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  31291. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  31292. + "(minimum %d)",
  31293. + (unsigned int)slot_zero, slot_zero->version,
  31294. + VCHIQ_VERSION_MIN);
  31295. + vchiq_loud_error("Restart with a newer VideoCore image.");
  31296. + vchiq_loud_error_footer();
  31297. + return VCHIQ_ERROR;
  31298. + }
  31299. +
  31300. + if (VCHIQ_VERSION < slot_zero->version_min) {
  31301. + vchiq_loud_error_header();
  31302. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  31303. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  31304. + "minimum %d)",
  31305. + (unsigned int)slot_zero, VCHIQ_VERSION,
  31306. + slot_zero->version_min);
  31307. + vchiq_loud_error("Restart with a newer kernel.");
  31308. + vchiq_loud_error_footer();
  31309. + return VCHIQ_ERROR;
  31310. + }
  31311. +
  31312. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  31313. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  31314. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  31315. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  31316. + vchiq_loud_error_header();
  31317. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  31318. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  31319. + "(expected %x)",
  31320. + (unsigned int)slot_zero,
  31321. + slot_zero->slot_zero_size,
  31322. + sizeof(VCHIQ_SLOT_ZERO_T));
  31323. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  31324. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  31325. + "(expected %d",
  31326. + (unsigned int)slot_zero, slot_zero->slot_size,
  31327. + VCHIQ_SLOT_SIZE);
  31328. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  31329. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  31330. + "(expected %d)",
  31331. + (unsigned int)slot_zero, slot_zero->max_slots,
  31332. + VCHIQ_MAX_SLOTS);
  31333. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  31334. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  31335. + "(expected %d)",
  31336. + (unsigned int)slot_zero,
  31337. + slot_zero->max_slots_per_side,
  31338. + VCHIQ_MAX_SLOTS_PER_SIDE);
  31339. + vchiq_loud_error_footer();
  31340. + return VCHIQ_ERROR;
  31341. + }
  31342. +
  31343. + if (is_master) {
  31344. + local = &slot_zero->master;
  31345. + remote = &slot_zero->slave;
  31346. + } else {
  31347. + local = &slot_zero->slave;
  31348. + remote = &slot_zero->master;
  31349. + }
  31350. +
  31351. + if (local->initialised) {
  31352. + vchiq_loud_error_header();
  31353. + if (remote->initialised)
  31354. + vchiq_loud_error("local state has already been "
  31355. + "initialised");
  31356. + else
  31357. + vchiq_loud_error("master/slave mismatch - two %ss",
  31358. + is_master ? "master" : "slave");
  31359. + vchiq_loud_error_footer();
  31360. + return VCHIQ_ERROR;
  31361. + }
  31362. +
  31363. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  31364. +
  31365. + state->id = id++;
  31366. + state->is_master = is_master;
  31367. +
  31368. + /*
  31369. + initialize shared state pointers
  31370. + */
  31371. +
  31372. + state->local = local;
  31373. + state->remote = remote;
  31374. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  31375. +
  31376. + /*
  31377. + initialize events and mutexes
  31378. + */
  31379. +
  31380. + sema_init(&state->connect, 0);
  31381. + mutex_init(&state->mutex);
  31382. + sema_init(&state->trigger_event, 0);
  31383. + sema_init(&state->recycle_event, 0);
  31384. + sema_init(&state->sync_trigger_event, 0);
  31385. + sema_init(&state->sync_release_event, 0);
  31386. +
  31387. + mutex_init(&state->slot_mutex);
  31388. + mutex_init(&state->recycle_mutex);
  31389. + mutex_init(&state->sync_mutex);
  31390. + mutex_init(&state->bulk_transfer_mutex);
  31391. +
  31392. + sema_init(&state->slot_available_event, 0);
  31393. + sema_init(&state->slot_remove_event, 0);
  31394. + sema_init(&state->data_quota_event, 0);
  31395. +
  31396. + state->slot_queue_available = 0;
  31397. +
  31398. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  31399. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  31400. + &state->service_quotas[i];
  31401. + sema_init(&service_quota->quota_event, 0);
  31402. + }
  31403. +
  31404. + for (i = local->slot_first; i <= local->slot_last; i++) {
  31405. + local->slot_queue[state->slot_queue_available++] = i;
  31406. + up(&state->slot_available_event);
  31407. + }
  31408. +
  31409. + state->default_slot_quota = state->slot_queue_available/2;
  31410. + state->default_message_quota =
  31411. + min((unsigned short)(state->default_slot_quota * 256),
  31412. + (unsigned short)~0);
  31413. +
  31414. + state->previous_data_index = -1;
  31415. + state->data_use_count = 0;
  31416. + state->data_quota = state->slot_queue_available - 1;
  31417. +
  31418. + local->trigger.event = &state->trigger_event;
  31419. + remote_event_create(&local->trigger);
  31420. + local->tx_pos = 0;
  31421. +
  31422. + local->recycle.event = &state->recycle_event;
  31423. + remote_event_create(&local->recycle);
  31424. + local->slot_queue_recycle = state->slot_queue_available;
  31425. +
  31426. + local->sync_trigger.event = &state->sync_trigger_event;
  31427. + remote_event_create(&local->sync_trigger);
  31428. +
  31429. + local->sync_release.event = &state->sync_release_event;
  31430. + remote_event_create(&local->sync_release);
  31431. +
  31432. + /* At start-of-day, the slot is empty and available */
  31433. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  31434. + = VCHIQ_MSGID_PADDING;
  31435. + remote_event_signal_local(&local->sync_release);
  31436. +
  31437. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  31438. +
  31439. + status = vchiq_platform_init_state(state);
  31440. +
  31441. + /*
  31442. + bring up slot handler thread
  31443. + */
  31444. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  31445. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  31446. + (void *)state,
  31447. + threadname);
  31448. +
  31449. + if (state->slot_handler_thread == NULL) {
  31450. + vchiq_loud_error_header();
  31451. + vchiq_loud_error("couldn't create thread %s", threadname);
  31452. + vchiq_loud_error_footer();
  31453. + return VCHIQ_ERROR;
  31454. + }
  31455. + set_user_nice(state->slot_handler_thread, -19);
  31456. + wake_up_process(state->slot_handler_thread);
  31457. +
  31458. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  31459. + state->recycle_thread = kthread_create(&recycle_func,
  31460. + (void *)state,
  31461. + threadname);
  31462. + if (state->recycle_thread == NULL) {
  31463. + vchiq_loud_error_header();
  31464. + vchiq_loud_error("couldn't create thread %s", threadname);
  31465. + vchiq_loud_error_footer();
  31466. + return VCHIQ_ERROR;
  31467. + }
  31468. + set_user_nice(state->recycle_thread, -19);
  31469. + wake_up_process(state->recycle_thread);
  31470. +
  31471. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  31472. + state->sync_thread = kthread_create(&sync_func,
  31473. + (void *)state,
  31474. + threadname);
  31475. + if (state->sync_thread == NULL) {
  31476. + vchiq_loud_error_header();
  31477. + vchiq_loud_error("couldn't create thread %s", threadname);
  31478. + vchiq_loud_error_footer();
  31479. + return VCHIQ_ERROR;
  31480. + }
  31481. + set_user_nice(state->sync_thread, -20);
  31482. + wake_up_process(state->sync_thread);
  31483. +
  31484. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  31485. + vchiq_states[state->id] = state;
  31486. +
  31487. + /* Indicate readiness to the other side */
  31488. + local->initialised = 1;
  31489. +
  31490. + return status;
  31491. +}
  31492. +
  31493. +/* Called from application thread when a client or server service is created. */
  31494. +VCHIQ_SERVICE_T *
  31495. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  31496. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  31497. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  31498. +{
  31499. + VCHIQ_SERVICE_T *service;
  31500. +
  31501. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  31502. + if (service) {
  31503. + service->base.fourcc = params->fourcc;
  31504. + service->base.callback = params->callback;
  31505. + service->base.userdata = params->userdata;
  31506. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  31507. + service->ref_count = 1;
  31508. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  31509. + service->userdata_term = userdata_term;
  31510. + service->localport = VCHIQ_PORT_FREE;
  31511. + service->remoteport = VCHIQ_PORT_FREE;
  31512. +
  31513. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  31514. + VCHIQ_FOURCC_INVALID : params->fourcc;
  31515. + service->client_id = 0;
  31516. + service->auto_close = 1;
  31517. + service->sync = 0;
  31518. + service->closing = 0;
  31519. + service->trace = 0;
  31520. + atomic_set(&service->poll_flags, 0);
  31521. + service->version = params->version;
  31522. + service->version_min = params->version_min;
  31523. + service->state = state;
  31524. + service->instance = instance;
  31525. + service->service_use_count = 0;
  31526. + init_bulk_queue(&service->bulk_tx);
  31527. + init_bulk_queue(&service->bulk_rx);
  31528. + sema_init(&service->remove_event, 0);
  31529. + sema_init(&service->bulk_remove_event, 0);
  31530. + mutex_init(&service->bulk_mutex);
  31531. + memset(&service->stats, 0, sizeof(service->stats));
  31532. + } else {
  31533. + vchiq_log_error(vchiq_core_log_level,
  31534. + "Out of memory");
  31535. + }
  31536. +
  31537. + if (service) {
  31538. + VCHIQ_SERVICE_T **pservice = NULL;
  31539. + int i;
  31540. +
  31541. + /* Although it is perfectly possible to use service_spinlock
  31542. + ** to protect the creation of services, it is overkill as it
  31543. + ** disables interrupts while the array is searched.
  31544. + ** The only danger is of another thread trying to create a
  31545. + ** service - service deletion is safe.
  31546. + ** Therefore it is preferable to use state->mutex which,
  31547. + ** although slower to claim, doesn't block interrupts while
  31548. + ** it is held.
  31549. + */
  31550. +
  31551. + mutex_lock(&state->mutex);
  31552. +
  31553. + /* Prepare to use a previously unused service */
  31554. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  31555. + pservice = &state->services[state->unused_service];
  31556. +
  31557. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  31558. + for (i = 0; i < state->unused_service; i++) {
  31559. + VCHIQ_SERVICE_T *srv = state->services[i];
  31560. + if (!srv) {
  31561. + pservice = &state->services[i];
  31562. + break;
  31563. + }
  31564. + }
  31565. + } else {
  31566. + for (i = (state->unused_service - 1); i >= 0; i--) {
  31567. + VCHIQ_SERVICE_T *srv = state->services[i];
  31568. + if (!srv)
  31569. + pservice = &state->services[i];
  31570. + else if ((srv->public_fourcc == params->fourcc)
  31571. + && ((srv->instance != instance) ||
  31572. + (srv->base.callback !=
  31573. + params->callback))) {
  31574. + /* There is another server using this
  31575. + ** fourcc which doesn't match. */
  31576. + pservice = NULL;
  31577. + break;
  31578. + }
  31579. + }
  31580. + }
  31581. +
  31582. + if (pservice) {
  31583. + service->localport = (pservice - state->services);
  31584. + if (!handle_seq)
  31585. + handle_seq = VCHIQ_MAX_STATES *
  31586. + VCHIQ_MAX_SERVICES;
  31587. + service->handle = handle_seq |
  31588. + (state->id * VCHIQ_MAX_SERVICES) |
  31589. + service->localport;
  31590. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  31591. + *pservice = service;
  31592. + if (pservice == &state->services[state->unused_service])
  31593. + state->unused_service++;
  31594. + }
  31595. +
  31596. + mutex_unlock(&state->mutex);
  31597. +
  31598. + if (!pservice) {
  31599. + kfree(service);
  31600. + service = NULL;
  31601. + }
  31602. + }
  31603. +
  31604. + if (service) {
  31605. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  31606. + &state->service_quotas[service->localport];
  31607. + service_quota->slot_quota = state->default_slot_quota;
  31608. + service_quota->message_quota = state->default_message_quota;
  31609. + if (service_quota->slot_use_count == 0)
  31610. + service_quota->previous_tx_index =
  31611. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  31612. + - 1;
  31613. +
  31614. + /* Bring this service online */
  31615. + vchiq_set_service_state(service, srvstate);
  31616. +
  31617. + vchiq_log_info(vchiq_core_msg_log_level,
  31618. + "%s Service %c%c%c%c SrcPort:%d",
  31619. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  31620. + ? "Open" : "Add",
  31621. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  31622. + service->localport);
  31623. + }
  31624. +
  31625. + /* Don't unlock the service - leave it with a ref_count of 1. */
  31626. +
  31627. + return service;
  31628. +}
  31629. +
  31630. +VCHIQ_STATUS_T
  31631. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  31632. +{
  31633. + struct vchiq_open_payload payload = {
  31634. + service->base.fourcc,
  31635. + client_id,
  31636. + service->version,
  31637. + service->version_min
  31638. + };
  31639. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  31640. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31641. +
  31642. + service->client_id = client_id;
  31643. + vchiq_use_service_internal(service);
  31644. + status = queue_message(service->state, NULL,
  31645. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  31646. + &body, 1, sizeof(payload), 1);
  31647. + if (status == VCHIQ_SUCCESS) {
  31648. + /* Wait for the ACK/NAK */
  31649. + if (down_interruptible(&service->remove_event) != 0) {
  31650. + status = VCHIQ_RETRY;
  31651. + vchiq_release_service_internal(service);
  31652. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  31653. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  31654. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  31655. + vchiq_log_error(vchiq_core_log_level,
  31656. + "%d: osi - srvstate = %s (ref %d)",
  31657. + service->state->id,
  31658. + srvstate_names[service->srvstate],
  31659. + service->ref_count);
  31660. + status = VCHIQ_ERROR;
  31661. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  31662. + vchiq_release_service_internal(service);
  31663. + }
  31664. + }
  31665. + return status;
  31666. +}
  31667. +
  31668. +static void
  31669. +release_service_messages(VCHIQ_SERVICE_T *service)
  31670. +{
  31671. + VCHIQ_STATE_T *state = service->state;
  31672. + int slot_last = state->remote->slot_last;
  31673. + int i;
  31674. +
  31675. + /* Release any claimed messages */
  31676. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  31677. + VCHIQ_SLOT_INFO_T *slot_info =
  31678. + SLOT_INFO_FROM_INDEX(state, i);
  31679. + if (slot_info->release_count != slot_info->use_count) {
  31680. + char *data =
  31681. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  31682. + unsigned int pos, end;
  31683. +
  31684. + end = VCHIQ_SLOT_SIZE;
  31685. + if (data == state->rx_data)
  31686. + /* This buffer is still being read from - stop
  31687. + ** at the current read position */
  31688. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  31689. +
  31690. + pos = 0;
  31691. +
  31692. + while (pos < end) {
  31693. + VCHIQ_HEADER_T *header =
  31694. + (VCHIQ_HEADER_T *)(data + pos);
  31695. + int msgid = header->msgid;
  31696. + int port = VCHIQ_MSG_DSTPORT(msgid);
  31697. + if ((port == service->localport) &&
  31698. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  31699. + vchiq_log_info(vchiq_core_log_level,
  31700. + " fsi - hdr %x",
  31701. + (unsigned int)header);
  31702. + release_slot(state, slot_info, header,
  31703. + NULL);
  31704. + }
  31705. + pos += calc_stride(header->size);
  31706. + if (pos > VCHIQ_SLOT_SIZE) {
  31707. + vchiq_log_error(vchiq_core_log_level,
  31708. + "fsi - pos %x: header %x, "
  31709. + "msgid %x, header->msgid %x, "
  31710. + "header->size %x",
  31711. + pos, (unsigned int)header,
  31712. + msgid, header->msgid,
  31713. + header->size);
  31714. + WARN(1, "invalid slot position\n");
  31715. + }
  31716. + }
  31717. + }
  31718. + }
  31719. +}
  31720. +
  31721. +static int
  31722. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  31723. +{
  31724. + VCHIQ_STATUS_T status;
  31725. +
  31726. + /* Abort any outstanding bulk transfers */
  31727. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  31728. + return 0;
  31729. + abort_outstanding_bulks(service, &service->bulk_tx);
  31730. + abort_outstanding_bulks(service, &service->bulk_rx);
  31731. + mutex_unlock(&service->bulk_mutex);
  31732. +
  31733. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  31734. + if (status == VCHIQ_SUCCESS)
  31735. + status = notify_bulks(service, &service->bulk_rx,
  31736. + 0/*!retry_poll*/);
  31737. + return (status == VCHIQ_SUCCESS);
  31738. +}
  31739. +
  31740. +static VCHIQ_STATUS_T
  31741. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  31742. +{
  31743. + VCHIQ_STATUS_T status;
  31744. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  31745. + int newstate;
  31746. +
  31747. + switch (service->srvstate) {
  31748. + case VCHIQ_SRVSTATE_OPEN:
  31749. + case VCHIQ_SRVSTATE_CLOSESENT:
  31750. + case VCHIQ_SRVSTATE_CLOSERECVD:
  31751. + if (is_server) {
  31752. + if (service->auto_close) {
  31753. + service->client_id = 0;
  31754. + service->remoteport = VCHIQ_PORT_FREE;
  31755. + newstate = VCHIQ_SRVSTATE_LISTENING;
  31756. + } else
  31757. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  31758. + } else
  31759. + newstate = VCHIQ_SRVSTATE_CLOSED;
  31760. + vchiq_set_service_state(service, newstate);
  31761. + break;
  31762. + case VCHIQ_SRVSTATE_LISTENING:
  31763. + break;
  31764. + default:
  31765. + vchiq_log_error(vchiq_core_log_level,
  31766. + "close_service_complete(%x) called in state %s",
  31767. + service->handle, srvstate_names[service->srvstate]);
  31768. + WARN(1, "close_service_complete in unexpected state\n");
  31769. + return VCHIQ_ERROR;
  31770. + }
  31771. +
  31772. + status = make_service_callback(service,
  31773. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  31774. +
  31775. + if (status != VCHIQ_RETRY) {
  31776. + int uc = service->service_use_count;
  31777. + int i;
  31778. + /* Complete the close process */
  31779. + for (i = 0; i < uc; i++)
  31780. + /* cater for cases where close is forced and the
  31781. + ** client may not close all it's handles */
  31782. + vchiq_release_service_internal(service);
  31783. +
  31784. + service->client_id = 0;
  31785. + service->remoteport = VCHIQ_PORT_FREE;
  31786. +
  31787. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  31788. + vchiq_free_service_internal(service);
  31789. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  31790. + if (is_server)
  31791. + service->closing = 0;
  31792. +
  31793. + up(&service->remove_event);
  31794. + }
  31795. + } else
  31796. + vchiq_set_service_state(service, failstate);
  31797. +
  31798. + return status;
  31799. +}
  31800. +
  31801. +/* Called by the slot handler */
  31802. +VCHIQ_STATUS_T
  31803. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  31804. +{
  31805. + VCHIQ_STATE_T *state = service->state;
  31806. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31807. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  31808. +
  31809. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  31810. + service->state->id, service->localport, close_recvd,
  31811. + srvstate_names[service->srvstate]);
  31812. +
  31813. + switch (service->srvstate) {
  31814. + case VCHIQ_SRVSTATE_CLOSED:
  31815. + case VCHIQ_SRVSTATE_HIDDEN:
  31816. + case VCHIQ_SRVSTATE_LISTENING:
  31817. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  31818. + if (close_recvd)
  31819. + vchiq_log_error(vchiq_core_log_level,
  31820. + "vchiq_close_service_internal(1) called "
  31821. + "in state %s",
  31822. + srvstate_names[service->srvstate]);
  31823. + else if (is_server) {
  31824. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  31825. + status = VCHIQ_ERROR;
  31826. + } else {
  31827. + service->client_id = 0;
  31828. + service->remoteport = VCHIQ_PORT_FREE;
  31829. + if (service->srvstate ==
  31830. + VCHIQ_SRVSTATE_CLOSEWAIT)
  31831. + vchiq_set_service_state(service,
  31832. + VCHIQ_SRVSTATE_LISTENING);
  31833. + }
  31834. + up(&service->remove_event);
  31835. + } else
  31836. + vchiq_free_service_internal(service);
  31837. + break;
  31838. + case VCHIQ_SRVSTATE_OPENING:
  31839. + if (close_recvd) {
  31840. + /* The open was rejected - tell the user */
  31841. + vchiq_set_service_state(service,
  31842. + VCHIQ_SRVSTATE_CLOSEWAIT);
  31843. + up(&service->remove_event);
  31844. + } else {
  31845. + /* Shutdown mid-open - let the other side know */
  31846. + status = queue_message(state, service,
  31847. + VCHIQ_MAKE_MSG
  31848. + (VCHIQ_MSG_CLOSE,
  31849. + service->localport,
  31850. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  31851. + NULL, 0, 0, 0);
  31852. + }
  31853. + break;
  31854. +
  31855. + case VCHIQ_SRVSTATE_OPENSYNC:
  31856. + mutex_lock(&state->sync_mutex);
  31857. + /* Drop through */
  31858. +
  31859. + case VCHIQ_SRVSTATE_OPEN:
  31860. + if (state->is_master || close_recvd) {
  31861. + if (!do_abort_bulks(service))
  31862. + status = VCHIQ_RETRY;
  31863. + }
  31864. +
  31865. + release_service_messages(service);
  31866. +
  31867. + if (status == VCHIQ_SUCCESS)
  31868. + status = queue_message(state, service,
  31869. + VCHIQ_MAKE_MSG
  31870. + (VCHIQ_MSG_CLOSE,
  31871. + service->localport,
  31872. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  31873. + NULL, 0, 0, 0);
  31874. +
  31875. + if (status == VCHIQ_SUCCESS) {
  31876. + if (!close_recvd)
  31877. + break;
  31878. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  31879. + mutex_unlock(&state->sync_mutex);
  31880. + break;
  31881. + } else
  31882. + break;
  31883. +
  31884. + status = close_service_complete(service,
  31885. + VCHIQ_SRVSTATE_CLOSERECVD);
  31886. + break;
  31887. +
  31888. + case VCHIQ_SRVSTATE_CLOSESENT:
  31889. + if (!close_recvd)
  31890. + /* This happens when a process is killed mid-close */
  31891. + break;
  31892. +
  31893. + if (!state->is_master) {
  31894. + if (!do_abort_bulks(service)) {
  31895. + status = VCHIQ_RETRY;
  31896. + break;
  31897. + }
  31898. + }
  31899. +
  31900. + if (status == VCHIQ_SUCCESS)
  31901. + status = close_service_complete(service,
  31902. + VCHIQ_SRVSTATE_CLOSERECVD);
  31903. + break;
  31904. +
  31905. + case VCHIQ_SRVSTATE_CLOSERECVD:
  31906. + if (!close_recvd && is_server)
  31907. + /* Force into LISTENING mode */
  31908. + vchiq_set_service_state(service,
  31909. + VCHIQ_SRVSTATE_LISTENING);
  31910. + status = close_service_complete(service,
  31911. + VCHIQ_SRVSTATE_CLOSERECVD);
  31912. + break;
  31913. +
  31914. + default:
  31915. + vchiq_log_error(vchiq_core_log_level,
  31916. + "vchiq_close_service_internal(%d) called in state %s",
  31917. + close_recvd, srvstate_names[service->srvstate]);
  31918. + break;
  31919. + }
  31920. +
  31921. + return status;
  31922. +}
  31923. +
  31924. +/* Called from the application process upon process death */
  31925. +void
  31926. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  31927. +{
  31928. + VCHIQ_STATE_T *state = service->state;
  31929. +
  31930. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  31931. + state->id, service->localport, service->remoteport);
  31932. +
  31933. + mark_service_closing(service);
  31934. +
  31935. + /* Mark the service for removal by the slot handler */
  31936. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  31937. +}
  31938. +
  31939. +/* Called from the slot handler */
  31940. +void
  31941. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  31942. +{
  31943. + VCHIQ_STATE_T *state = service->state;
  31944. +
  31945. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  31946. + state->id, service->localport);
  31947. +
  31948. + switch (service->srvstate) {
  31949. + case VCHIQ_SRVSTATE_OPENING:
  31950. + case VCHIQ_SRVSTATE_CLOSED:
  31951. + case VCHIQ_SRVSTATE_HIDDEN:
  31952. + case VCHIQ_SRVSTATE_LISTENING:
  31953. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  31954. + break;
  31955. + default:
  31956. + vchiq_log_error(vchiq_core_log_level,
  31957. + "%d: fsi - (%d) in state %s",
  31958. + state->id, service->localport,
  31959. + srvstate_names[service->srvstate]);
  31960. + return;
  31961. + }
  31962. +
  31963. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  31964. +
  31965. + up(&service->remove_event);
  31966. +
  31967. + /* Release the initial lock */
  31968. + unlock_service(service);
  31969. +}
  31970. +
  31971. +VCHIQ_STATUS_T
  31972. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  31973. +{
  31974. + VCHIQ_SERVICE_T *service;
  31975. + int i;
  31976. +
  31977. + /* Find all services registered to this client and enable them. */
  31978. + i = 0;
  31979. + while ((service = next_service_by_instance(state, instance,
  31980. + &i)) != NULL) {
  31981. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  31982. + vchiq_set_service_state(service,
  31983. + VCHIQ_SRVSTATE_LISTENING);
  31984. + unlock_service(service);
  31985. + }
  31986. +
  31987. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  31988. + if (queue_message(state, NULL,
  31989. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  31990. + 0, 1) == VCHIQ_RETRY)
  31991. + return VCHIQ_RETRY;
  31992. +
  31993. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  31994. + }
  31995. +
  31996. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  31997. + if (down_interruptible(&state->connect) != 0)
  31998. + return VCHIQ_RETRY;
  31999. +
  32000. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  32001. + up(&state->connect);
  32002. + }
  32003. +
  32004. + return VCHIQ_SUCCESS;
  32005. +}
  32006. +
  32007. +VCHIQ_STATUS_T
  32008. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  32009. +{
  32010. + VCHIQ_SERVICE_T *service;
  32011. + int i;
  32012. +
  32013. + /* Find all services registered to this client and enable them. */
  32014. + i = 0;
  32015. + while ((service = next_service_by_instance(state, instance,
  32016. + &i)) != NULL) {
  32017. + (void)vchiq_remove_service(service->handle);
  32018. + unlock_service(service);
  32019. + }
  32020. +
  32021. + return VCHIQ_SUCCESS;
  32022. +}
  32023. +
  32024. +VCHIQ_STATUS_T
  32025. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  32026. +{
  32027. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32028. +
  32029. + switch (state->conn_state) {
  32030. + case VCHIQ_CONNSTATE_CONNECTED:
  32031. + /* Request a pause */
  32032. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  32033. + request_poll(state, NULL, 0);
  32034. + break;
  32035. + default:
  32036. + vchiq_log_error(vchiq_core_log_level,
  32037. + "vchiq_pause_internal in state %s\n",
  32038. + conn_state_names[state->conn_state]);
  32039. + status = VCHIQ_ERROR;
  32040. + VCHIQ_STATS_INC(state, error_count);
  32041. + break;
  32042. + }
  32043. +
  32044. + return status;
  32045. +}
  32046. +
  32047. +VCHIQ_STATUS_T
  32048. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  32049. +{
  32050. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32051. +
  32052. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  32053. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  32054. + request_poll(state, NULL, 0);
  32055. + } else {
  32056. + status = VCHIQ_ERROR;
  32057. + VCHIQ_STATS_INC(state, error_count);
  32058. + }
  32059. +
  32060. + return status;
  32061. +}
  32062. +
  32063. +VCHIQ_STATUS_T
  32064. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  32065. +{
  32066. + /* Unregister the service */
  32067. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32068. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32069. +
  32070. + if (!service)
  32071. + return VCHIQ_ERROR;
  32072. +
  32073. + vchiq_log_info(vchiq_core_log_level,
  32074. + "%d: close_service:%d",
  32075. + service->state->id, service->localport);
  32076. +
  32077. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  32078. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  32079. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  32080. + unlock_service(service);
  32081. + return VCHIQ_ERROR;
  32082. + }
  32083. +
  32084. + mark_service_closing(service);
  32085. +
  32086. + if (current == service->state->slot_handler_thread) {
  32087. + status = vchiq_close_service_internal(service,
  32088. + 0/*!close_recvd*/);
  32089. + BUG_ON(status == VCHIQ_RETRY);
  32090. + } else {
  32091. + /* Mark the service for termination by the slot handler */
  32092. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  32093. + }
  32094. +
  32095. + while (1) {
  32096. + if (down_interruptible(&service->remove_event) != 0) {
  32097. + status = VCHIQ_RETRY;
  32098. + break;
  32099. + }
  32100. +
  32101. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  32102. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  32103. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  32104. + break;
  32105. +
  32106. + vchiq_log_warning(vchiq_core_log_level,
  32107. + "%d: close_service:%d - waiting in state %s",
  32108. + service->state->id, service->localport,
  32109. + srvstate_names[service->srvstate]);
  32110. + }
  32111. +
  32112. + if ((status == VCHIQ_SUCCESS) &&
  32113. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  32114. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  32115. + status = VCHIQ_ERROR;
  32116. +
  32117. + unlock_service(service);
  32118. +
  32119. + return status;
  32120. +}
  32121. +
  32122. +VCHIQ_STATUS_T
  32123. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  32124. +{
  32125. + /* Unregister the service */
  32126. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32127. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32128. +
  32129. + if (!service)
  32130. + return VCHIQ_ERROR;
  32131. +
  32132. + vchiq_log_info(vchiq_core_log_level,
  32133. + "%d: remove_service:%d",
  32134. + service->state->id, service->localport);
  32135. +
  32136. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  32137. + unlock_service(service);
  32138. + return VCHIQ_ERROR;
  32139. + }
  32140. +
  32141. + mark_service_closing(service);
  32142. +
  32143. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  32144. + (current == service->state->slot_handler_thread)) {
  32145. + /* Make it look like a client, because it must be removed and
  32146. + not left in the LISTENING state. */
  32147. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  32148. +
  32149. + status = vchiq_close_service_internal(service,
  32150. + 0/*!close_recvd*/);
  32151. + BUG_ON(status == VCHIQ_RETRY);
  32152. + } else {
  32153. + /* Mark the service for removal by the slot handler */
  32154. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  32155. + }
  32156. + while (1) {
  32157. + if (down_interruptible(&service->remove_event) != 0) {
  32158. + status = VCHIQ_RETRY;
  32159. + break;
  32160. + }
  32161. +
  32162. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  32163. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  32164. + break;
  32165. +
  32166. + vchiq_log_warning(vchiq_core_log_level,
  32167. + "%d: remove_service:%d - waiting in state %s",
  32168. + service->state->id, service->localport,
  32169. + srvstate_names[service->srvstate]);
  32170. + }
  32171. +
  32172. + if ((status == VCHIQ_SUCCESS) &&
  32173. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  32174. + status = VCHIQ_ERROR;
  32175. +
  32176. + unlock_service(service);
  32177. +
  32178. + return status;
  32179. +}
  32180. +
  32181. +
  32182. +/* This function may be called by kernel threads or user threads.
  32183. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  32184. + * received and the call should be retried after being returned to user
  32185. + * context.
  32186. + * When called in blocking mode, the userdata field points to a bulk_waiter
  32187. + * structure.
  32188. + */
  32189. +VCHIQ_STATUS_T
  32190. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  32191. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  32192. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  32193. +{
  32194. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32195. + VCHIQ_BULK_QUEUE_T *queue;
  32196. + VCHIQ_BULK_T *bulk;
  32197. + VCHIQ_STATE_T *state;
  32198. + struct bulk_waiter *bulk_waiter = NULL;
  32199. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  32200. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  32201. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  32202. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  32203. +
  32204. + if (!service ||
  32205. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  32206. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  32207. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  32208. + goto error_exit;
  32209. +
  32210. + switch (mode) {
  32211. + case VCHIQ_BULK_MODE_NOCALLBACK:
  32212. + case VCHIQ_BULK_MODE_CALLBACK:
  32213. + break;
  32214. + case VCHIQ_BULK_MODE_BLOCKING:
  32215. + bulk_waiter = (struct bulk_waiter *)userdata;
  32216. + sema_init(&bulk_waiter->event, 0);
  32217. + bulk_waiter->actual = 0;
  32218. + bulk_waiter->bulk = NULL;
  32219. + break;
  32220. + case VCHIQ_BULK_MODE_WAITING:
  32221. + bulk_waiter = (struct bulk_waiter *)userdata;
  32222. + bulk = bulk_waiter->bulk;
  32223. + goto waiting;
  32224. + default:
  32225. + goto error_exit;
  32226. + }
  32227. +
  32228. + state = service->state;
  32229. +
  32230. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  32231. + &service->bulk_tx : &service->bulk_rx;
  32232. +
  32233. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  32234. + status = VCHIQ_RETRY;
  32235. + goto error_exit;
  32236. + }
  32237. +
  32238. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  32239. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  32240. + do {
  32241. + mutex_unlock(&service->bulk_mutex);
  32242. + if (down_interruptible(&service->bulk_remove_event)
  32243. + != 0) {
  32244. + status = VCHIQ_RETRY;
  32245. + goto error_exit;
  32246. + }
  32247. + if (mutex_lock_interruptible(&service->bulk_mutex)
  32248. + != 0) {
  32249. + status = VCHIQ_RETRY;
  32250. + goto error_exit;
  32251. + }
  32252. + } while (queue->local_insert == queue->remove +
  32253. + VCHIQ_NUM_SERVICE_BULKS);
  32254. + }
  32255. +
  32256. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  32257. +
  32258. + bulk->mode = mode;
  32259. + bulk->dir = dir;
  32260. + bulk->userdata = userdata;
  32261. + bulk->size = size;
  32262. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  32263. +
  32264. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  32265. + VCHIQ_SUCCESS)
  32266. + goto unlock_error_exit;
  32267. +
  32268. + wmb();
  32269. +
  32270. + vchiq_log_info(vchiq_core_log_level,
  32271. + "%d: bt (%d->%d) %cx %x@%x %x",
  32272. + state->id,
  32273. + service->localport, service->remoteport, dir_char,
  32274. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  32275. +
  32276. + if (state->is_master) {
  32277. + queue->local_insert++;
  32278. + if (resolve_bulks(service, queue))
  32279. + request_poll(state, service,
  32280. + (dir == VCHIQ_BULK_TRANSMIT) ?
  32281. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  32282. + } else {
  32283. + int payload[2] = { (int)bulk->data, bulk->size };
  32284. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  32285. +
  32286. + status = queue_message(state, NULL,
  32287. + VCHIQ_MAKE_MSG(dir_msgtype,
  32288. + service->localport, service->remoteport),
  32289. + &element, 1, sizeof(payload), 1);
  32290. + if (status != VCHIQ_SUCCESS) {
  32291. + vchiq_complete_bulk(bulk);
  32292. + goto unlock_error_exit;
  32293. + }
  32294. + queue->local_insert++;
  32295. + }
  32296. +
  32297. + mutex_unlock(&service->bulk_mutex);
  32298. +
  32299. + vchiq_log_trace(vchiq_core_log_level,
  32300. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  32301. + state->id,
  32302. + service->localport, dir_char,
  32303. + queue->local_insert, queue->remote_insert, queue->process);
  32304. +
  32305. +waiting:
  32306. + unlock_service(service);
  32307. +
  32308. + status = VCHIQ_SUCCESS;
  32309. +
  32310. + if (bulk_waiter) {
  32311. + bulk_waiter->bulk = bulk;
  32312. + if (down_interruptible(&bulk_waiter->event) != 0)
  32313. + status = VCHIQ_RETRY;
  32314. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  32315. + status = VCHIQ_ERROR;
  32316. + }
  32317. +
  32318. + return status;
  32319. +
  32320. +unlock_error_exit:
  32321. + mutex_unlock(&service->bulk_mutex);
  32322. +
  32323. +error_exit:
  32324. + if (service)
  32325. + unlock_service(service);
  32326. + return status;
  32327. +}
  32328. +
  32329. +VCHIQ_STATUS_T
  32330. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  32331. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  32332. +{
  32333. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32334. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  32335. +
  32336. + unsigned int size = 0;
  32337. + unsigned int i;
  32338. +
  32339. + if (!service ||
  32340. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  32341. + goto error_exit;
  32342. +
  32343. + for (i = 0; i < (unsigned int)count; i++) {
  32344. + if (elements[i].size) {
  32345. + if (elements[i].data == NULL) {
  32346. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  32347. + goto error_exit;
  32348. + }
  32349. + size += elements[i].size;
  32350. + }
  32351. + }
  32352. +
  32353. + if (size > VCHIQ_MAX_MSG_SIZE) {
  32354. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  32355. + goto error_exit;
  32356. + }
  32357. +
  32358. + switch (service->srvstate) {
  32359. + case VCHIQ_SRVSTATE_OPEN:
  32360. + status = queue_message(service->state, service,
  32361. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  32362. + service->localport,
  32363. + service->remoteport),
  32364. + elements, count, size, 1);
  32365. + break;
  32366. + case VCHIQ_SRVSTATE_OPENSYNC:
  32367. + status = queue_message_sync(service->state, service,
  32368. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  32369. + service->localport,
  32370. + service->remoteport),
  32371. + elements, count, size, 1);
  32372. + break;
  32373. + default:
  32374. + status = VCHIQ_ERROR;
  32375. + break;
  32376. + }
  32377. +
  32378. +error_exit:
  32379. + if (service)
  32380. + unlock_service(service);
  32381. +
  32382. + return status;
  32383. +}
  32384. +
  32385. +void
  32386. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  32387. +{
  32388. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32389. + VCHIQ_SHARED_STATE_T *remote;
  32390. + VCHIQ_STATE_T *state;
  32391. + int slot_index;
  32392. +
  32393. + if (!service)
  32394. + return;
  32395. +
  32396. + state = service->state;
  32397. + remote = state->remote;
  32398. +
  32399. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  32400. +
  32401. + if ((slot_index >= remote->slot_first) &&
  32402. + (slot_index <= remote->slot_last)) {
  32403. + int msgid = header->msgid;
  32404. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  32405. + VCHIQ_SLOT_INFO_T *slot_info =
  32406. + SLOT_INFO_FROM_INDEX(state, slot_index);
  32407. +
  32408. + release_slot(state, slot_info, header, service);
  32409. + }
  32410. + } else if (slot_index == remote->slot_sync)
  32411. + release_message_sync(state, header);
  32412. +
  32413. + unlock_service(service);
  32414. +}
  32415. +
  32416. +static void
  32417. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  32418. +{
  32419. + header->msgid = VCHIQ_MSGID_PADDING;
  32420. + wmb();
  32421. + remote_event_signal(&state->remote->sync_release);
  32422. +}
  32423. +
  32424. +VCHIQ_STATUS_T
  32425. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  32426. +{
  32427. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  32428. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32429. +
  32430. + if (!service ||
  32431. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  32432. + !peer_version)
  32433. + goto exit;
  32434. + *peer_version = service->peer_version;
  32435. + status = VCHIQ_SUCCESS;
  32436. +
  32437. +exit:
  32438. + if (service)
  32439. + unlock_service(service);
  32440. + return status;
  32441. +}
  32442. +
  32443. +VCHIQ_STATUS_T
  32444. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  32445. + int config_size, VCHIQ_CONFIG_T *pconfig)
  32446. +{
  32447. + VCHIQ_CONFIG_T config;
  32448. +
  32449. + (void)instance;
  32450. +
  32451. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  32452. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  32453. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  32454. + config.max_services = VCHIQ_MAX_SERVICES;
  32455. + config.version = VCHIQ_VERSION;
  32456. + config.version_min = VCHIQ_VERSION_MIN;
  32457. +
  32458. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  32459. + return VCHIQ_ERROR;
  32460. +
  32461. + memcpy(pconfig, &config,
  32462. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  32463. +
  32464. + return VCHIQ_SUCCESS;
  32465. +}
  32466. +
  32467. +VCHIQ_STATUS_T
  32468. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  32469. + VCHIQ_SERVICE_OPTION_T option, int value)
  32470. +{
  32471. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32472. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  32473. +
  32474. + if (service) {
  32475. + switch (option) {
  32476. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  32477. + service->auto_close = value;
  32478. + status = VCHIQ_SUCCESS;
  32479. + break;
  32480. +
  32481. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  32482. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  32483. + &service->state->service_quotas[
  32484. + service->localport];
  32485. + if (value == 0)
  32486. + value = service->state->default_slot_quota;
  32487. + if ((value >= service_quota->slot_use_count) &&
  32488. + (value < (unsigned short)~0)) {
  32489. + service_quota->slot_quota = value;
  32490. + if ((value >= service_quota->slot_use_count) &&
  32491. + (service_quota->message_quota >=
  32492. + service_quota->message_use_count)) {
  32493. + /* Signal the service that it may have
  32494. + ** dropped below its quota */
  32495. + up(&service_quota->quota_event);
  32496. + }
  32497. + status = VCHIQ_SUCCESS;
  32498. + }
  32499. + } break;
  32500. +
  32501. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  32502. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  32503. + &service->state->service_quotas[
  32504. + service->localport];
  32505. + if (value == 0)
  32506. + value = service->state->default_message_quota;
  32507. + if ((value >= service_quota->message_use_count) &&
  32508. + (value < (unsigned short)~0)) {
  32509. + service_quota->message_quota = value;
  32510. + if ((value >=
  32511. + service_quota->message_use_count) &&
  32512. + (service_quota->slot_quota >=
  32513. + service_quota->slot_use_count))
  32514. + /* Signal the service that it may have
  32515. + ** dropped below its quota */
  32516. + up(&service_quota->quota_event);
  32517. + status = VCHIQ_SUCCESS;
  32518. + }
  32519. + } break;
  32520. +
  32521. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  32522. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  32523. + (service->srvstate ==
  32524. + VCHIQ_SRVSTATE_LISTENING)) {
  32525. + service->sync = value;
  32526. + status = VCHIQ_SUCCESS;
  32527. + }
  32528. + break;
  32529. +
  32530. + case VCHIQ_SERVICE_OPTION_TRACE:
  32531. + service->trace = value;
  32532. + status = VCHIQ_SUCCESS;
  32533. + break;
  32534. +
  32535. + default:
  32536. + break;
  32537. + }
  32538. + unlock_service(service);
  32539. + }
  32540. +
  32541. + return status;
  32542. +}
  32543. +
  32544. +void
  32545. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  32546. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  32547. +{
  32548. + static const char *const debug_names[] = {
  32549. + "<entries>",
  32550. + "SLOT_HANDLER_COUNT",
  32551. + "SLOT_HANDLER_LINE",
  32552. + "PARSE_LINE",
  32553. + "PARSE_HEADER",
  32554. + "PARSE_MSGID",
  32555. + "AWAIT_COMPLETION_LINE",
  32556. + "DEQUEUE_MESSAGE_LINE",
  32557. + "SERVICE_CALLBACK_LINE",
  32558. + "MSG_QUEUE_FULL_COUNT",
  32559. + "COMPLETION_QUEUE_FULL_COUNT"
  32560. + };
  32561. + int i;
  32562. +
  32563. + char buf[80];
  32564. + int len;
  32565. + len = snprintf(buf, sizeof(buf),
  32566. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  32567. + label, shared->slot_first, shared->slot_last,
  32568. + shared->tx_pos, shared->slot_queue_recycle);
  32569. + vchiq_dump(dump_context, buf, len + 1);
  32570. +
  32571. + len = snprintf(buf, sizeof(buf),
  32572. + " Slots claimed:");
  32573. + vchiq_dump(dump_context, buf, len + 1);
  32574. +
  32575. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  32576. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  32577. + if (slot_info.use_count != slot_info.release_count) {
  32578. + len = snprintf(buf, sizeof(buf),
  32579. + " %d: %d/%d", i, slot_info.use_count,
  32580. + slot_info.release_count);
  32581. + vchiq_dump(dump_context, buf, len + 1);
  32582. + }
  32583. + }
  32584. +
  32585. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  32586. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  32587. + debug_names[i], shared->debug[i], shared->debug[i]);
  32588. + vchiq_dump(dump_context, buf, len + 1);
  32589. + }
  32590. +}
  32591. +
  32592. +void
  32593. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  32594. +{
  32595. + char buf[80];
  32596. + int len;
  32597. + int i;
  32598. +
  32599. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  32600. + conn_state_names[state->conn_state]);
  32601. + vchiq_dump(dump_context, buf, len + 1);
  32602. +
  32603. + len = snprintf(buf, sizeof(buf),
  32604. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  32605. + state->local->tx_pos,
  32606. + (uint32_t)state->tx_data +
  32607. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  32608. + state->rx_pos,
  32609. + (uint32_t)state->rx_data +
  32610. + (state->rx_pos & VCHIQ_SLOT_MASK));
  32611. + vchiq_dump(dump_context, buf, len + 1);
  32612. +
  32613. + len = snprintf(buf, sizeof(buf),
  32614. + " Version: %d (min %d)",
  32615. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  32616. + vchiq_dump(dump_context, buf, len + 1);
  32617. +
  32618. + if (VCHIQ_ENABLE_STATS) {
  32619. + len = snprintf(buf, sizeof(buf),
  32620. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  32621. + "error_count=%d",
  32622. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  32623. + state->stats.error_count);
  32624. + vchiq_dump(dump_context, buf, len + 1);
  32625. + }
  32626. +
  32627. + len = snprintf(buf, sizeof(buf),
  32628. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  32629. + "(%d data)",
  32630. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  32631. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  32632. + state->data_quota - state->data_use_count,
  32633. + state->local->slot_queue_recycle - state->slot_queue_available,
  32634. + state->stats.slot_stalls, state->stats.data_stalls);
  32635. + vchiq_dump(dump_context, buf, len + 1);
  32636. +
  32637. + vchiq_dump_platform_state(dump_context);
  32638. +
  32639. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  32640. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  32641. +
  32642. + vchiq_dump_platform_instances(dump_context);
  32643. +
  32644. + for (i = 0; i < state->unused_service; i++) {
  32645. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  32646. +
  32647. + if (service) {
  32648. + vchiq_dump_service_state(dump_context, service);
  32649. + unlock_service(service);
  32650. + }
  32651. + }
  32652. +}
  32653. +
  32654. +void
  32655. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  32656. +{
  32657. + char buf[80];
  32658. + int len;
  32659. +
  32660. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  32661. + service->localport, srvstate_names[service->srvstate],
  32662. + service->ref_count - 1); /*Don't include the lock just taken*/
  32663. +
  32664. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  32665. + char remoteport[30];
  32666. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  32667. + &service->state->service_quotas[service->localport];
  32668. + int fourcc = service->base.fourcc;
  32669. + int tx_pending, rx_pending;
  32670. + if (service->remoteport != VCHIQ_PORT_FREE) {
  32671. + int len2 = snprintf(remoteport, sizeof(remoteport),
  32672. + "%d", service->remoteport);
  32673. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  32674. + snprintf(remoteport + len2,
  32675. + sizeof(remoteport) - len2,
  32676. + " (client %x)", service->client_id);
  32677. + } else
  32678. + strcpy(remoteport, "n/a");
  32679. +
  32680. + len += snprintf(buf + len, sizeof(buf) - len,
  32681. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  32682. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  32683. + remoteport,
  32684. + service_quota->message_use_count,
  32685. + service_quota->message_quota,
  32686. + service_quota->slot_use_count,
  32687. + service_quota->slot_quota);
  32688. +
  32689. + vchiq_dump(dump_context, buf, len + 1);
  32690. +
  32691. + tx_pending = service->bulk_tx.local_insert -
  32692. + service->bulk_tx.remote_insert;
  32693. +
  32694. + rx_pending = service->bulk_rx.local_insert -
  32695. + service->bulk_rx.remote_insert;
  32696. +
  32697. + len = snprintf(buf, sizeof(buf),
  32698. + " Bulk: tx_pending=%d (size %d),"
  32699. + " rx_pending=%d (size %d)",
  32700. + tx_pending,
  32701. + tx_pending ? service->bulk_tx.bulks[
  32702. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  32703. + rx_pending,
  32704. + rx_pending ? service->bulk_rx.bulks[
  32705. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  32706. +
  32707. + if (VCHIQ_ENABLE_STATS) {
  32708. + vchiq_dump(dump_context, buf, len + 1);
  32709. +
  32710. + len = snprintf(buf, sizeof(buf),
  32711. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  32712. + "rx_count=%d, rx_bytes=%llu",
  32713. + service->stats.ctrl_tx_count,
  32714. + service->stats.ctrl_tx_bytes,
  32715. + service->stats.ctrl_rx_count,
  32716. + service->stats.ctrl_rx_bytes);
  32717. + vchiq_dump(dump_context, buf, len + 1);
  32718. +
  32719. + len = snprintf(buf, sizeof(buf),
  32720. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  32721. + "rx_count=%d, rx_bytes=%llu",
  32722. + service->stats.bulk_tx_count,
  32723. + service->stats.bulk_tx_bytes,
  32724. + service->stats.bulk_rx_count,
  32725. + service->stats.bulk_rx_bytes);
  32726. + vchiq_dump(dump_context, buf, len + 1);
  32727. +
  32728. + len = snprintf(buf, sizeof(buf),
  32729. + " %d quota stalls, %d slot stalls, "
  32730. + "%d bulk stalls, %d aborted, %d errors",
  32731. + service->stats.quota_stalls,
  32732. + service->stats.slot_stalls,
  32733. + service->stats.bulk_stalls,
  32734. + service->stats.bulk_aborted_count,
  32735. + service->stats.error_count);
  32736. + }
  32737. + }
  32738. +
  32739. + vchiq_dump(dump_context, buf, len + 1);
  32740. +
  32741. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  32742. + vchiq_dump_platform_service_state(dump_context, service);
  32743. +}
  32744. +
  32745. +
  32746. +void
  32747. +vchiq_loud_error_header(void)
  32748. +{
  32749. + vchiq_log_error(vchiq_core_log_level,
  32750. + "============================================================"
  32751. + "================");
  32752. + vchiq_log_error(vchiq_core_log_level,
  32753. + "============================================================"
  32754. + "================");
  32755. + vchiq_log_error(vchiq_core_log_level, "=====");
  32756. +}
  32757. +
  32758. +void
  32759. +vchiq_loud_error_footer(void)
  32760. +{
  32761. + vchiq_log_error(vchiq_core_log_level, "=====");
  32762. + vchiq_log_error(vchiq_core_log_level,
  32763. + "============================================================"
  32764. + "================");
  32765. + vchiq_log_error(vchiq_core_log_level,
  32766. + "============================================================"
  32767. + "================");
  32768. +}
  32769. +
  32770. +
  32771. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  32772. +{
  32773. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  32774. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  32775. + status = queue_message(state, NULL,
  32776. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  32777. + NULL, 0, 0, 0);
  32778. + return status;
  32779. +}
  32780. +
  32781. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  32782. +{
  32783. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  32784. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  32785. + status = queue_message(state, NULL,
  32786. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  32787. + NULL, 0, 0, 0);
  32788. + return status;
  32789. +}
  32790. +
  32791. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  32792. +{
  32793. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  32794. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  32795. + status = queue_message(state, NULL,
  32796. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  32797. + NULL, 0, 0, 0);
  32798. + return status;
  32799. +}
  32800. +
  32801. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  32802. + size_t numBytes)
  32803. +{
  32804. + const uint8_t *mem = (const uint8_t *)voidMem;
  32805. + size_t offset;
  32806. + char lineBuf[100];
  32807. + char *s;
  32808. +
  32809. + while (numBytes > 0) {
  32810. + s = lineBuf;
  32811. +
  32812. + for (offset = 0; offset < 16; offset++) {
  32813. + if (offset < numBytes)
  32814. + s += snprintf(s, 4, "%02x ", mem[offset]);
  32815. + else
  32816. + s += snprintf(s, 4, " ");
  32817. + }
  32818. +
  32819. + for (offset = 0; offset < 16; offset++) {
  32820. + if (offset < numBytes) {
  32821. + uint8_t ch = mem[offset];
  32822. +
  32823. + if ((ch < ' ') || (ch > '~'))
  32824. + ch = '.';
  32825. + *s++ = (char)ch;
  32826. + }
  32827. + }
  32828. + *s++ = '\0';
  32829. +
  32830. + if ((label != NULL) && (*label != '\0'))
  32831. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  32832. + "%s: %08x: %s", label, addr, lineBuf);
  32833. + else
  32834. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  32835. + "%08x: %s", addr, lineBuf);
  32836. +
  32837. + addr += 16;
  32838. + mem += 16;
  32839. + if (numBytes > 16)
  32840. + numBytes -= 16;
  32841. + else
  32842. + numBytes = 0;
  32843. + }
  32844. +}
  32845. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  32846. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 1969-12-31 18:00:00.000000000 -0600
  32847. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 2014-12-11 14:02:53.548418001 -0600
  32848. @@ -0,0 +1,711 @@
  32849. +/**
  32850. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  32851. + *
  32852. + * Redistribution and use in source and binary forms, with or without
  32853. + * modification, are permitted provided that the following conditions
  32854. + * are met:
  32855. + * 1. Redistributions of source code must retain the above copyright
  32856. + * notice, this list of conditions, and the following disclaimer,
  32857. + * without modification.
  32858. + * 2. Redistributions in binary form must reproduce the above copyright
  32859. + * notice, this list of conditions and the following disclaimer in the
  32860. + * documentation and/or other materials provided with the distribution.
  32861. + * 3. The names of the above-listed copyright holders may not be used
  32862. + * to endorse or promote products derived from this software without
  32863. + * specific prior written permission.
  32864. + *
  32865. + * ALTERNATIVELY, this software may be distributed under the terms of the
  32866. + * GNU General Public License ("GPL") version 2, as published by the Free
  32867. + * Software Foundation.
  32868. + *
  32869. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  32870. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  32871. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32872. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32873. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32874. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32875. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32876. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32877. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32878. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32879. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32880. + */
  32881. +
  32882. +#ifndef VCHIQ_CORE_H
  32883. +#define VCHIQ_CORE_H
  32884. +
  32885. +#include <linux/mutex.h>
  32886. +#include <linux/semaphore.h>
  32887. +#include <linux/kthread.h>
  32888. +
  32889. +#include "vchiq_cfg.h"
  32890. +
  32891. +#include "vchiq.h"
  32892. +
  32893. +/* Run time control of log level, based on KERN_XXX level. */
  32894. +#define VCHIQ_LOG_DEFAULT 4
  32895. +#define VCHIQ_LOG_ERROR 3
  32896. +#define VCHIQ_LOG_WARNING 4
  32897. +#define VCHIQ_LOG_INFO 6
  32898. +#define VCHIQ_LOG_TRACE 7
  32899. +
  32900. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  32901. +
  32902. +#ifndef vchiq_log_error
  32903. +#define vchiq_log_error(cat, fmt, ...) \
  32904. + do { if (cat >= VCHIQ_LOG_ERROR) \
  32905. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32906. +#endif
  32907. +#ifndef vchiq_log_warning
  32908. +#define vchiq_log_warning(cat, fmt, ...) \
  32909. + do { if (cat >= VCHIQ_LOG_WARNING) \
  32910. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32911. +#endif
  32912. +#ifndef vchiq_log_info
  32913. +#define vchiq_log_info(cat, fmt, ...) \
  32914. + do { if (cat >= VCHIQ_LOG_INFO) \
  32915. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32916. +#endif
  32917. +#ifndef vchiq_log_trace
  32918. +#define vchiq_log_trace(cat, fmt, ...) \
  32919. + do { if (cat >= VCHIQ_LOG_TRACE) \
  32920. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32921. +#endif
  32922. +
  32923. +#define vchiq_loud_error(...) \
  32924. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  32925. +
  32926. +#ifndef vchiq_static_assert
  32927. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  32928. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  32929. +#endif
  32930. +
  32931. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  32932. +
  32933. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  32934. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  32935. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  32936. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  32937. +
  32938. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  32939. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  32940. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  32941. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  32942. +
  32943. +#define VCHIQ_MSG_PADDING 0 /* - */
  32944. +#define VCHIQ_MSG_CONNECT 1 /* - */
  32945. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  32946. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  32947. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  32948. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  32949. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  32950. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  32951. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  32952. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  32953. +#define VCHIQ_MSG_PAUSE 10 /* - */
  32954. +#define VCHIQ_MSG_RESUME 11 /* - */
  32955. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  32956. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  32957. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  32958. +
  32959. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  32960. +#define VCHIQ_PORT_FREE 0x1000
  32961. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  32962. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  32963. + ((type<<24) | (srcport<<12) | (dstport<<0))
  32964. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  32965. +#define VCHIQ_MSG_SRCPORT(msgid) \
  32966. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  32967. +#define VCHIQ_MSG_DSTPORT(msgid) \
  32968. + ((unsigned short)msgid & 0xfff)
  32969. +
  32970. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  32971. + ((fourcc) >> 24) & 0xff, \
  32972. + ((fourcc) >> 16) & 0xff, \
  32973. + ((fourcc) >> 8) & 0xff, \
  32974. + (fourcc) & 0xff
  32975. +
  32976. +/* Ensure the fields are wide enough */
  32977. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  32978. + == 0);
  32979. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  32980. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  32981. + (unsigned int)VCHIQ_PORT_FREE);
  32982. +
  32983. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  32984. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  32985. +
  32986. +#define VCHIQ_FOURCC_INVALID 0x00000000
  32987. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  32988. +
  32989. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  32990. +
  32991. +typedef uint32_t BITSET_T;
  32992. +
  32993. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  32994. +
  32995. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  32996. +#define BITSET_WORD(b) (b >> 5)
  32997. +#define BITSET_BIT(b) (1 << (b & 31))
  32998. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  32999. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  33000. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  33001. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  33002. +
  33003. +#if VCHIQ_ENABLE_STATS
  33004. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  33005. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  33006. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  33007. + (service->stats. stat += addend)
  33008. +#else
  33009. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  33010. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  33011. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  33012. +#endif
  33013. +
  33014. +enum {
  33015. + DEBUG_ENTRIES,
  33016. +#if VCHIQ_ENABLE_DEBUG
  33017. + DEBUG_SLOT_HANDLER_COUNT,
  33018. + DEBUG_SLOT_HANDLER_LINE,
  33019. + DEBUG_PARSE_LINE,
  33020. + DEBUG_PARSE_HEADER,
  33021. + DEBUG_PARSE_MSGID,
  33022. + DEBUG_AWAIT_COMPLETION_LINE,
  33023. + DEBUG_DEQUEUE_MESSAGE_LINE,
  33024. + DEBUG_SERVICE_CALLBACK_LINE,
  33025. + DEBUG_MSG_QUEUE_FULL_COUNT,
  33026. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  33027. +#endif
  33028. + DEBUG_MAX
  33029. +};
  33030. +
  33031. +#if VCHIQ_ENABLE_DEBUG
  33032. +
  33033. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  33034. +#define DEBUG_TRACE(d) \
  33035. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  33036. +#define DEBUG_VALUE(d, v) \
  33037. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  33038. +#define DEBUG_COUNT(d) \
  33039. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  33040. +
  33041. +#else /* VCHIQ_ENABLE_DEBUG */
  33042. +
  33043. +#define DEBUG_INITIALISE(local)
  33044. +#define DEBUG_TRACE(d)
  33045. +#define DEBUG_VALUE(d, v)
  33046. +#define DEBUG_COUNT(d)
  33047. +
  33048. +#endif /* VCHIQ_ENABLE_DEBUG */
  33049. +
  33050. +typedef enum {
  33051. + VCHIQ_CONNSTATE_DISCONNECTED,
  33052. + VCHIQ_CONNSTATE_CONNECTING,
  33053. + VCHIQ_CONNSTATE_CONNECTED,
  33054. + VCHIQ_CONNSTATE_PAUSING,
  33055. + VCHIQ_CONNSTATE_PAUSE_SENT,
  33056. + VCHIQ_CONNSTATE_PAUSED,
  33057. + VCHIQ_CONNSTATE_RESUMING,
  33058. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  33059. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  33060. +} VCHIQ_CONNSTATE_T;
  33061. +
  33062. +enum {
  33063. + VCHIQ_SRVSTATE_FREE,
  33064. + VCHIQ_SRVSTATE_HIDDEN,
  33065. + VCHIQ_SRVSTATE_LISTENING,
  33066. + VCHIQ_SRVSTATE_OPENING,
  33067. + VCHIQ_SRVSTATE_OPEN,
  33068. + VCHIQ_SRVSTATE_OPENSYNC,
  33069. + VCHIQ_SRVSTATE_CLOSESENT,
  33070. + VCHIQ_SRVSTATE_CLOSERECVD,
  33071. + VCHIQ_SRVSTATE_CLOSEWAIT,
  33072. + VCHIQ_SRVSTATE_CLOSED
  33073. +};
  33074. +
  33075. +enum {
  33076. + VCHIQ_POLL_TERMINATE,
  33077. + VCHIQ_POLL_REMOVE,
  33078. + VCHIQ_POLL_TXNOTIFY,
  33079. + VCHIQ_POLL_RXNOTIFY,
  33080. + VCHIQ_POLL_COUNT
  33081. +};
  33082. +
  33083. +typedef enum {
  33084. + VCHIQ_BULK_TRANSMIT,
  33085. + VCHIQ_BULK_RECEIVE
  33086. +} VCHIQ_BULK_DIR_T;
  33087. +
  33088. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  33089. +
  33090. +typedef struct vchiq_bulk_struct {
  33091. + short mode;
  33092. + short dir;
  33093. + void *userdata;
  33094. + VCHI_MEM_HANDLE_T handle;
  33095. + void *data;
  33096. + int size;
  33097. + void *remote_data;
  33098. + int remote_size;
  33099. + int actual;
  33100. +} VCHIQ_BULK_T;
  33101. +
  33102. +typedef struct vchiq_bulk_queue_struct {
  33103. + int local_insert; /* Where to insert the next local bulk */
  33104. + int remote_insert; /* Where to insert the next remote bulk (master) */
  33105. + int process; /* Bulk to transfer next */
  33106. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  33107. + int remove; /* Bulk to notify the local client of, and remove,
  33108. + ** next */
  33109. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  33110. +} VCHIQ_BULK_QUEUE_T;
  33111. +
  33112. +typedef struct remote_event_struct {
  33113. + int armed;
  33114. + int fired;
  33115. + struct semaphore *event;
  33116. +} REMOTE_EVENT_T;
  33117. +
  33118. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  33119. +
  33120. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  33121. +
  33122. +typedef struct vchiq_slot_struct {
  33123. + char data[VCHIQ_SLOT_SIZE];
  33124. +} VCHIQ_SLOT_T;
  33125. +
  33126. +typedef struct vchiq_slot_info_struct {
  33127. + /* Use two counters rather than one to avoid the need for a mutex. */
  33128. + short use_count;
  33129. + short release_count;
  33130. +} VCHIQ_SLOT_INFO_T;
  33131. +
  33132. +typedef struct vchiq_service_struct {
  33133. + VCHIQ_SERVICE_BASE_T base;
  33134. + VCHIQ_SERVICE_HANDLE_T handle;
  33135. + unsigned int ref_count;
  33136. + int srvstate;
  33137. + VCHIQ_USERDATA_TERM_T userdata_term;
  33138. + unsigned int localport;
  33139. + unsigned int remoteport;
  33140. + int public_fourcc;
  33141. + int client_id;
  33142. + char auto_close;
  33143. + char sync;
  33144. + char closing;
  33145. + char trace;
  33146. + atomic_t poll_flags;
  33147. + short version;
  33148. + short version_min;
  33149. + short peer_version;
  33150. +
  33151. + VCHIQ_STATE_T *state;
  33152. + VCHIQ_INSTANCE_T instance;
  33153. +
  33154. + int service_use_count;
  33155. +
  33156. + VCHIQ_BULK_QUEUE_T bulk_tx;
  33157. + VCHIQ_BULK_QUEUE_T bulk_rx;
  33158. +
  33159. + struct semaphore remove_event;
  33160. + struct semaphore bulk_remove_event;
  33161. + struct mutex bulk_mutex;
  33162. +
  33163. + struct service_stats_struct {
  33164. + int quota_stalls;
  33165. + int slot_stalls;
  33166. + int bulk_stalls;
  33167. + int error_count;
  33168. + int ctrl_tx_count;
  33169. + int ctrl_rx_count;
  33170. + int bulk_tx_count;
  33171. + int bulk_rx_count;
  33172. + int bulk_aborted_count;
  33173. + uint64_t ctrl_tx_bytes;
  33174. + uint64_t ctrl_rx_bytes;
  33175. + uint64_t bulk_tx_bytes;
  33176. + uint64_t bulk_rx_bytes;
  33177. + } stats;
  33178. +} VCHIQ_SERVICE_T;
  33179. +
  33180. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  33181. + statically allocated, since for accounting reasons a service's slot
  33182. + usage is carried over between users of the same port number.
  33183. + */
  33184. +typedef struct vchiq_service_quota_struct {
  33185. + unsigned short slot_quota;
  33186. + unsigned short slot_use_count;
  33187. + unsigned short message_quota;
  33188. + unsigned short message_use_count;
  33189. + struct semaphore quota_event;
  33190. + int previous_tx_index;
  33191. +} VCHIQ_SERVICE_QUOTA_T;
  33192. +
  33193. +typedef struct vchiq_shared_state_struct {
  33194. +
  33195. + /* A non-zero value here indicates that the content is valid. */
  33196. + int initialised;
  33197. +
  33198. + /* The first and last (inclusive) slots allocated to the owner. */
  33199. + int slot_first;
  33200. + int slot_last;
  33201. +
  33202. + /* The slot allocated to synchronous messages from the owner. */
  33203. + int slot_sync;
  33204. +
  33205. + /* Signalling this event indicates that owner's slot handler thread
  33206. + ** should run. */
  33207. + REMOTE_EVENT_T trigger;
  33208. +
  33209. + /* Indicates the byte position within the stream where the next message
  33210. + ** will be written. The least significant bits are an index into the
  33211. + ** slot. The next bits are the index of the slot in slot_queue. */
  33212. + int tx_pos;
  33213. +
  33214. + /* This event should be signalled when a slot is recycled. */
  33215. + REMOTE_EVENT_T recycle;
  33216. +
  33217. + /* The slot_queue index where the next recycled slot will be written. */
  33218. + int slot_queue_recycle;
  33219. +
  33220. + /* This event should be signalled when a synchronous message is sent. */
  33221. + REMOTE_EVENT_T sync_trigger;
  33222. +
  33223. + /* This event should be signalled when a synchronous message has been
  33224. + ** released. */
  33225. + REMOTE_EVENT_T sync_release;
  33226. +
  33227. + /* A circular buffer of slot indexes. */
  33228. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  33229. +
  33230. + /* Debugging state */
  33231. + int debug[DEBUG_MAX];
  33232. +} VCHIQ_SHARED_STATE_T;
  33233. +
  33234. +typedef struct vchiq_slot_zero_struct {
  33235. + int magic;
  33236. + short version;
  33237. + short version_min;
  33238. + int slot_zero_size;
  33239. + int slot_size;
  33240. + int max_slots;
  33241. + int max_slots_per_side;
  33242. + int platform_data[2];
  33243. + VCHIQ_SHARED_STATE_T master;
  33244. + VCHIQ_SHARED_STATE_T slave;
  33245. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  33246. +} VCHIQ_SLOT_ZERO_T;
  33247. +
  33248. +struct vchiq_state_struct {
  33249. + int id;
  33250. + int initialised;
  33251. + VCHIQ_CONNSTATE_T conn_state;
  33252. + int is_master;
  33253. +
  33254. + VCHIQ_SHARED_STATE_T *local;
  33255. + VCHIQ_SHARED_STATE_T *remote;
  33256. + VCHIQ_SLOT_T *slot_data;
  33257. +
  33258. + unsigned short default_slot_quota;
  33259. + unsigned short default_message_quota;
  33260. +
  33261. + /* Event indicating connect message received */
  33262. + struct semaphore connect;
  33263. +
  33264. + /* Mutex protecting services */
  33265. + struct mutex mutex;
  33266. + VCHIQ_INSTANCE_T *instance;
  33267. +
  33268. + /* Processes incoming messages */
  33269. + struct task_struct *slot_handler_thread;
  33270. +
  33271. + /* Processes recycled slots */
  33272. + struct task_struct *recycle_thread;
  33273. +
  33274. + /* Processes synchronous messages */
  33275. + struct task_struct *sync_thread;
  33276. +
  33277. + /* Local implementation of the trigger remote event */
  33278. + struct semaphore trigger_event;
  33279. +
  33280. + /* Local implementation of the recycle remote event */
  33281. + struct semaphore recycle_event;
  33282. +
  33283. + /* Local implementation of the sync trigger remote event */
  33284. + struct semaphore sync_trigger_event;
  33285. +
  33286. + /* Local implementation of the sync release remote event */
  33287. + struct semaphore sync_release_event;
  33288. +
  33289. + char *tx_data;
  33290. + char *rx_data;
  33291. + VCHIQ_SLOT_INFO_T *rx_info;
  33292. +
  33293. + struct mutex slot_mutex;
  33294. +
  33295. + struct mutex recycle_mutex;
  33296. +
  33297. + struct mutex sync_mutex;
  33298. +
  33299. + struct mutex bulk_transfer_mutex;
  33300. +
  33301. + /* Indicates the byte position within the stream from where the next
  33302. + ** message will be read. The least significant bits are an index into
  33303. + ** the slot.The next bits are the index of the slot in
  33304. + ** remote->slot_queue. */
  33305. + int rx_pos;
  33306. +
  33307. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  33308. + from remote->tx_pos. */
  33309. + int local_tx_pos;
  33310. +
  33311. + /* The slot_queue index of the slot to become available next. */
  33312. + int slot_queue_available;
  33313. +
  33314. + /* A flag to indicate if any poll has been requested */
  33315. + int poll_needed;
  33316. +
  33317. + /* Ths index of the previous slot used for data messages. */
  33318. + int previous_data_index;
  33319. +
  33320. + /* The number of slots occupied by data messages. */
  33321. + unsigned short data_use_count;
  33322. +
  33323. + /* The maximum number of slots to be occupied by data messages. */
  33324. + unsigned short data_quota;
  33325. +
  33326. + /* An array of bit sets indicating which services must be polled. */
  33327. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  33328. +
  33329. + /* The number of the first unused service */
  33330. + int unused_service;
  33331. +
  33332. + /* Signalled when a free slot becomes available. */
  33333. + struct semaphore slot_available_event;
  33334. +
  33335. + struct semaphore slot_remove_event;
  33336. +
  33337. + /* Signalled when a free data slot becomes available. */
  33338. + struct semaphore data_quota_event;
  33339. +
  33340. + /* Incremented when there are bulk transfers which cannot be processed
  33341. + * whilst paused and must be processed on resume */
  33342. + int deferred_bulks;
  33343. +
  33344. + struct state_stats_struct {
  33345. + int slot_stalls;
  33346. + int data_stalls;
  33347. + int ctrl_tx_count;
  33348. + int ctrl_rx_count;
  33349. + int error_count;
  33350. + } stats;
  33351. +
  33352. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  33353. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  33354. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  33355. +
  33356. + VCHIQ_PLATFORM_STATE_T platform_state;
  33357. +};
  33358. +
  33359. +struct bulk_waiter {
  33360. + VCHIQ_BULK_T *bulk;
  33361. + struct semaphore event;
  33362. + int actual;
  33363. +};
  33364. +
  33365. +extern spinlock_t bulk_waiter_spinlock;
  33366. +
  33367. +extern int vchiq_core_log_level;
  33368. +extern int vchiq_core_msg_log_level;
  33369. +extern int vchiq_sync_log_level;
  33370. +
  33371. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  33372. +
  33373. +extern const char *
  33374. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  33375. +
  33376. +extern VCHIQ_SLOT_ZERO_T *
  33377. +vchiq_init_slots(void *mem_base, int mem_size);
  33378. +
  33379. +extern VCHIQ_STATUS_T
  33380. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  33381. + int is_master);
  33382. +
  33383. +extern VCHIQ_STATUS_T
  33384. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  33385. +
  33386. +extern VCHIQ_SERVICE_T *
  33387. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  33388. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  33389. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  33390. +
  33391. +extern VCHIQ_STATUS_T
  33392. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  33393. +
  33394. +extern VCHIQ_STATUS_T
  33395. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  33396. +
  33397. +extern void
  33398. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  33399. +
  33400. +extern void
  33401. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  33402. +
  33403. +extern VCHIQ_STATUS_T
  33404. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  33405. +
  33406. +extern VCHIQ_STATUS_T
  33407. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  33408. +
  33409. +extern VCHIQ_STATUS_T
  33410. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  33411. +
  33412. +extern void
  33413. +remote_event_pollall(VCHIQ_STATE_T *state);
  33414. +
  33415. +extern VCHIQ_STATUS_T
  33416. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  33417. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  33418. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  33419. +
  33420. +extern void
  33421. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  33422. +
  33423. +extern void
  33424. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  33425. +
  33426. +extern void
  33427. +vchiq_loud_error_header(void);
  33428. +
  33429. +extern void
  33430. +vchiq_loud_error_footer(void);
  33431. +
  33432. +extern void
  33433. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  33434. +
  33435. +static inline VCHIQ_SERVICE_T *
  33436. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  33437. +{
  33438. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  33439. + (VCHIQ_MAX_STATES - 1)];
  33440. + if (!state)
  33441. + return NULL;
  33442. +
  33443. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  33444. +}
  33445. +
  33446. +extern VCHIQ_SERVICE_T *
  33447. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  33448. +
  33449. +extern VCHIQ_SERVICE_T *
  33450. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  33451. +
  33452. +extern VCHIQ_SERVICE_T *
  33453. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  33454. + VCHIQ_SERVICE_HANDLE_T handle);
  33455. +
  33456. +extern VCHIQ_SERVICE_T *
  33457. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  33458. + VCHIQ_SERVICE_HANDLE_T handle);
  33459. +
  33460. +extern VCHIQ_SERVICE_T *
  33461. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  33462. + int *pidx);
  33463. +
  33464. +extern void
  33465. +lock_service(VCHIQ_SERVICE_T *service);
  33466. +
  33467. +extern void
  33468. +unlock_service(VCHIQ_SERVICE_T *service);
  33469. +
  33470. +/* The following functions are called from vchiq_core, and external
  33471. +** implementations must be provided. */
  33472. +
  33473. +extern VCHIQ_STATUS_T
  33474. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  33475. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  33476. +
  33477. +extern void
  33478. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  33479. +
  33480. +extern void
  33481. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  33482. +
  33483. +extern VCHIQ_STATUS_T
  33484. +vchiq_copy_from_user(void *dst, const void *src, int size);
  33485. +
  33486. +extern void
  33487. +remote_event_signal(REMOTE_EVENT_T *event);
  33488. +
  33489. +void
  33490. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  33491. +
  33492. +extern void
  33493. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  33494. +
  33495. +extern VCHIQ_STATUS_T
  33496. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  33497. +
  33498. +extern void
  33499. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  33500. +
  33501. +extern void
  33502. +vchiq_dump(void *dump_context, const char *str, int len);
  33503. +
  33504. +extern void
  33505. +vchiq_dump_platform_state(void *dump_context);
  33506. +
  33507. +extern void
  33508. +vchiq_dump_platform_instances(void *dump_context);
  33509. +
  33510. +extern void
  33511. +vchiq_dump_platform_service_state(void *dump_context,
  33512. + VCHIQ_SERVICE_T *service);
  33513. +
  33514. +extern VCHIQ_STATUS_T
  33515. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  33516. +
  33517. +extern VCHIQ_STATUS_T
  33518. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  33519. +
  33520. +extern void
  33521. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  33522. +
  33523. +extern void
  33524. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  33525. +
  33526. +extern VCHIQ_STATUS_T
  33527. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  33528. +
  33529. +extern VCHIQ_STATUS_T
  33530. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  33531. +
  33532. +extern void
  33533. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  33534. +
  33535. +extern VCHIQ_STATUS_T
  33536. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  33537. +
  33538. +extern VCHIQ_STATUS_T
  33539. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  33540. +
  33541. +extern VCHIQ_STATUS_T
  33542. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  33543. +
  33544. +extern void
  33545. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  33546. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  33547. +
  33548. +extern void
  33549. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  33550. +
  33551. +extern void
  33552. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  33553. +
  33554. +
  33555. +extern void
  33556. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  33557. + size_t numBytes);
  33558. +
  33559. +#endif
  33560. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  33561. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c 1969-12-31 18:00:00.000000000 -0600
  33562. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c 2014-12-11 14:02:53.548418001 -0600
  33563. @@ -0,0 +1,383 @@
  33564. +/**
  33565. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  33566. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33567. + *
  33568. + * Redistribution and use in source and binary forms, with or without
  33569. + * modification, are permitted provided that the following conditions
  33570. + * are met:
  33571. + * 1. Redistributions of source code must retain the above copyright
  33572. + * notice, this list of conditions, and the following disclaimer,
  33573. + * without modification.
  33574. + * 2. Redistributions in binary form must reproduce the above copyright
  33575. + * notice, this list of conditions and the following disclaimer in the
  33576. + * documentation and/or other materials provided with the distribution.
  33577. + * 3. The names of the above-listed copyright holders may not be used
  33578. + * to endorse or promote products derived from this software without
  33579. + * specific prior written permission.
  33580. + *
  33581. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33582. + * GNU General Public License ("GPL") version 2, as published by the Free
  33583. + * Software Foundation.
  33584. + *
  33585. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33586. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33587. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33588. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33589. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33590. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33591. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33592. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33593. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33594. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33595. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33596. + */
  33597. +
  33598. +
  33599. +#include <linux/debugfs.h>
  33600. +#include "vchiq_core.h"
  33601. +#include "vchiq_arm.h"
  33602. +#include "vchiq_debugfs.h"
  33603. +
  33604. +#ifdef CONFIG_DEBUG_FS
  33605. +
  33606. +/****************************************************************************
  33607. +*
  33608. +* log category entries
  33609. +*
  33610. +***************************************************************************/
  33611. +#define DEBUGFS_WRITE_BUF_SIZE 256
  33612. +
  33613. +#define VCHIQ_LOG_ERROR_STR "error"
  33614. +#define VCHIQ_LOG_WARNING_STR "warning"
  33615. +#define VCHIQ_LOG_INFO_STR "info"
  33616. +#define VCHIQ_LOG_TRACE_STR "trace"
  33617. +
  33618. +
  33619. +/* Top-level debug info */
  33620. +struct vchiq_debugfs_info {
  33621. + /* Global 'vchiq' debugfs entry used by all instances */
  33622. + struct dentry *vchiq_cfg_dir;
  33623. +
  33624. + /* one entry per client process */
  33625. + struct dentry *clients;
  33626. +
  33627. + /* log categories */
  33628. + struct dentry *log_categories;
  33629. +};
  33630. +
  33631. +static struct vchiq_debugfs_info debugfs_info;
  33632. +
  33633. +/* Log category debugfs entries */
  33634. +struct vchiq_debugfs_log_entry {
  33635. + const char *name;
  33636. + int *plevel;
  33637. + struct dentry *dir;
  33638. +};
  33639. +
  33640. +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
  33641. + { "core", &vchiq_core_log_level },
  33642. + { "msg", &vchiq_core_msg_log_level },
  33643. + { "sync", &vchiq_sync_log_level },
  33644. + { "susp", &vchiq_susp_log_level },
  33645. + { "arm", &vchiq_arm_log_level },
  33646. +};
  33647. +static int n_log_entries =
  33648. + sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
  33649. +
  33650. +
  33651. +static struct dentry *vchiq_clients_top(void);
  33652. +static struct dentry *vchiq_debugfs_top(void);
  33653. +
  33654. +static int debugfs_log_show(struct seq_file *f, void *offset)
  33655. +{
  33656. + int *levp = f->private;
  33657. + char *log_value = NULL;
  33658. +
  33659. + switch (*levp) {
  33660. + case VCHIQ_LOG_ERROR:
  33661. + log_value = VCHIQ_LOG_ERROR_STR;
  33662. + break;
  33663. + case VCHIQ_LOG_WARNING:
  33664. + log_value = VCHIQ_LOG_WARNING_STR;
  33665. + break;
  33666. + case VCHIQ_LOG_INFO:
  33667. + log_value = VCHIQ_LOG_INFO_STR;
  33668. + break;
  33669. + case VCHIQ_LOG_TRACE:
  33670. + log_value = VCHIQ_LOG_TRACE_STR;
  33671. + break;
  33672. + default:
  33673. + break;
  33674. + }
  33675. +
  33676. + seq_printf(f, "%s\n", log_value ? log_value : "(null)");
  33677. +
  33678. + return 0;
  33679. +}
  33680. +
  33681. +static int debugfs_log_open(struct inode *inode, struct file *file)
  33682. +{
  33683. + return single_open(file, debugfs_log_show, inode->i_private);
  33684. +}
  33685. +
  33686. +static int debugfs_log_write(struct file *file,
  33687. + const char __user *buffer,
  33688. + size_t count, loff_t *ppos)
  33689. +{
  33690. + struct seq_file *f = (struct seq_file *)file->private_data;
  33691. + int *levp = f->private;
  33692. + char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
  33693. +
  33694. + memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
  33695. + if (count >= DEBUGFS_WRITE_BUF_SIZE)
  33696. + count = DEBUGFS_WRITE_BUF_SIZE;
  33697. +
  33698. + if (copy_from_user(kbuf, buffer, count) != 0)
  33699. + return -EFAULT;
  33700. + kbuf[count - 1] = 0;
  33701. +
  33702. + if (strncmp("error", kbuf, strlen("error")) == 0)
  33703. + *levp = VCHIQ_LOG_ERROR;
  33704. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  33705. + *levp = VCHIQ_LOG_WARNING;
  33706. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  33707. + *levp = VCHIQ_LOG_INFO;
  33708. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  33709. + *levp = VCHIQ_LOG_TRACE;
  33710. + else
  33711. + *levp = VCHIQ_LOG_DEFAULT;
  33712. +
  33713. + *ppos += count;
  33714. +
  33715. + return count;
  33716. +}
  33717. +
  33718. +static const struct file_operations debugfs_log_fops = {
  33719. + .owner = THIS_MODULE,
  33720. + .open = debugfs_log_open,
  33721. + .write = debugfs_log_write,
  33722. + .read = seq_read,
  33723. + .llseek = seq_lseek,
  33724. + .release = single_release,
  33725. +};
  33726. +
  33727. +/* create an entry under <debugfs>/vchiq/log for each log category */
  33728. +static int vchiq_debugfs_create_log_entries(struct dentry *top)
  33729. +{
  33730. + struct dentry *dir;
  33731. + size_t i;
  33732. + int ret = 0;
  33733. + dir = debugfs_create_dir("log", vchiq_debugfs_top());
  33734. + if (!dir)
  33735. + return -ENOMEM;
  33736. + debugfs_info.log_categories = dir;
  33737. +
  33738. + for (i = 0; i < n_log_entries; i++) {
  33739. + void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
  33740. + dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
  33741. + 0644,
  33742. + debugfs_info.log_categories,
  33743. + levp,
  33744. + &debugfs_log_fops);
  33745. + if (!dir) {
  33746. + ret = -ENOMEM;
  33747. + break;
  33748. + }
  33749. +
  33750. + vchiq_debugfs_log_entries[i].dir = dir;
  33751. + }
  33752. + return ret;
  33753. +}
  33754. +
  33755. +static int debugfs_usecount_show(struct seq_file *f, void *offset)
  33756. +{
  33757. + VCHIQ_INSTANCE_T instance = f->private;
  33758. + int use_count;
  33759. +
  33760. + use_count = vchiq_instance_get_use_count(instance);
  33761. + seq_printf(f, "%d\n", use_count);
  33762. +
  33763. + return 0;
  33764. +}
  33765. +
  33766. +static int debugfs_usecount_open(struct inode *inode, struct file *file)
  33767. +{
  33768. + return single_open(file, debugfs_usecount_show, inode->i_private);
  33769. +}
  33770. +
  33771. +static const struct file_operations debugfs_usecount_fops = {
  33772. + .owner = THIS_MODULE,
  33773. + .open = debugfs_usecount_open,
  33774. + .read = seq_read,
  33775. + .llseek = seq_lseek,
  33776. + .release = single_release,
  33777. +};
  33778. +
  33779. +static int debugfs_trace_show(struct seq_file *f, void *offset)
  33780. +{
  33781. + VCHIQ_INSTANCE_T instance = f->private;
  33782. + int trace;
  33783. +
  33784. + trace = vchiq_instance_get_trace(instance);
  33785. + seq_printf(f, "%s\n", trace ? "Y" : "N");
  33786. +
  33787. + return 0;
  33788. +}
  33789. +
  33790. +static int debugfs_trace_open(struct inode *inode, struct file *file)
  33791. +{
  33792. + return single_open(file, debugfs_trace_show, inode->i_private);
  33793. +}
  33794. +
  33795. +static int debugfs_trace_write(struct file *file,
  33796. + const char __user *buffer,
  33797. + size_t count, loff_t *ppos)
  33798. +{
  33799. + struct seq_file *f = (struct seq_file *)file->private_data;
  33800. + VCHIQ_INSTANCE_T instance = f->private;
  33801. + char firstchar;
  33802. +
  33803. + if (copy_from_user(&firstchar, buffer, 1) != 0)
  33804. + return -EFAULT;
  33805. +
  33806. + switch (firstchar) {
  33807. + case 'Y':
  33808. + case 'y':
  33809. + case '1':
  33810. + vchiq_instance_set_trace(instance, 1);
  33811. + break;
  33812. + case 'N':
  33813. + case 'n':
  33814. + case '0':
  33815. + vchiq_instance_set_trace(instance, 0);
  33816. + break;
  33817. + default:
  33818. + break;
  33819. + }
  33820. +
  33821. + *ppos += count;
  33822. +
  33823. + return count;
  33824. +}
  33825. +
  33826. +static const struct file_operations debugfs_trace_fops = {
  33827. + .owner = THIS_MODULE,
  33828. + .open = debugfs_trace_open,
  33829. + .write = debugfs_trace_write,
  33830. + .read = seq_read,
  33831. + .llseek = seq_lseek,
  33832. + .release = single_release,
  33833. +};
  33834. +
  33835. +/* add an instance (process) to the debugfs entries */
  33836. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  33837. +{
  33838. + char pidstr[16];
  33839. + struct dentry *top, *use_count, *trace;
  33840. + struct dentry *clients = vchiq_clients_top();
  33841. +
  33842. + snprintf(pidstr, sizeof(pidstr), "%d",
  33843. + vchiq_instance_get_pid(instance));
  33844. +
  33845. + top = debugfs_create_dir(pidstr, clients);
  33846. + if (!top)
  33847. + goto fail_top;
  33848. +
  33849. + use_count = debugfs_create_file("use_count",
  33850. + 0444, top,
  33851. + instance,
  33852. + &debugfs_usecount_fops);
  33853. + if (!use_count)
  33854. + goto fail_use_count;
  33855. +
  33856. + trace = debugfs_create_file("trace",
  33857. + 0644, top,
  33858. + instance,
  33859. + &debugfs_trace_fops);
  33860. + if (!trace)
  33861. + goto fail_trace;
  33862. +
  33863. + vchiq_instance_get_debugfs_node(instance)->dentry = top;
  33864. +
  33865. + return 0;
  33866. +
  33867. +fail_trace:
  33868. + debugfs_remove(use_count);
  33869. +fail_use_count:
  33870. + debugfs_remove(top);
  33871. +fail_top:
  33872. + return -ENOMEM;
  33873. +}
  33874. +
  33875. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  33876. +{
  33877. + VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
  33878. + debugfs_remove_recursive(node->dentry);
  33879. +}
  33880. +
  33881. +
  33882. +int vchiq_debugfs_init(void)
  33883. +{
  33884. + BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
  33885. +
  33886. + debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
  33887. + if (debugfs_info.vchiq_cfg_dir == NULL)
  33888. + goto fail;
  33889. +
  33890. + debugfs_info.clients = debugfs_create_dir("clients",
  33891. + vchiq_debugfs_top());
  33892. + if (!debugfs_info.clients)
  33893. + goto fail;
  33894. +
  33895. + if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
  33896. + goto fail;
  33897. +
  33898. + return 0;
  33899. +
  33900. +fail:
  33901. + vchiq_debugfs_deinit();
  33902. + vchiq_log_error(vchiq_arm_log_level,
  33903. + "%s: failed to create debugfs directory",
  33904. + __func__);
  33905. +
  33906. + return -ENOMEM;
  33907. +}
  33908. +
  33909. +/* remove all the debugfs entries */
  33910. +void vchiq_debugfs_deinit(void)
  33911. +{
  33912. + debugfs_remove_recursive(vchiq_debugfs_top());
  33913. +}
  33914. +
  33915. +static struct dentry *vchiq_clients_top(void)
  33916. +{
  33917. + return debugfs_info.clients;
  33918. +}
  33919. +
  33920. +static struct dentry *vchiq_debugfs_top(void)
  33921. +{
  33922. + BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
  33923. + return debugfs_info.vchiq_cfg_dir;
  33924. +}
  33925. +
  33926. +#else /* CONFIG_DEBUG_FS */
  33927. +
  33928. +int vchiq_debugfs_init(void)
  33929. +{
  33930. + return 0;
  33931. +}
  33932. +
  33933. +void vchiq_debugfs_deinit(void)
  33934. +{
  33935. +}
  33936. +
  33937. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  33938. +{
  33939. + return 0;
  33940. +}
  33941. +
  33942. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  33943. +{
  33944. +}
  33945. +
  33946. +#endif /* CONFIG_DEBUG_FS */
  33947. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  33948. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h 1969-12-31 18:00:00.000000000 -0600
  33949. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h 2014-12-11 14:02:53.548418001 -0600
  33950. @@ -0,0 +1,52 @@
  33951. +/**
  33952. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  33953. + *
  33954. + * Redistribution and use in source and binary forms, with or without
  33955. + * modification, are permitted provided that the following conditions
  33956. + * are met:
  33957. + * 1. Redistributions of source code must retain the above copyright
  33958. + * notice, this list of conditions, and the following disclaimer,
  33959. + * without modification.
  33960. + * 2. Redistributions in binary form must reproduce the above copyright
  33961. + * notice, this list of conditions and the following disclaimer in the
  33962. + * documentation and/or other materials provided with the distribution.
  33963. + * 3. The names of the above-listed copyright holders may not be used
  33964. + * to endorse or promote products derived from this software without
  33965. + * specific prior written permission.
  33966. + *
  33967. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33968. + * GNU General Public License ("GPL") version 2, as published by the Free
  33969. + * Software Foundation.
  33970. + *
  33971. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33972. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33973. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33974. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33975. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33976. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33977. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33978. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33979. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33980. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33981. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33982. + */
  33983. +
  33984. +#ifndef VCHIQ_DEBUGFS_H
  33985. +#define VCHIQ_DEBUGFS_H
  33986. +
  33987. +#include "vchiq_core.h"
  33988. +
  33989. +typedef struct vchiq_debugfs_node_struct
  33990. +{
  33991. + struct dentry *dentry;
  33992. +} VCHIQ_DEBUGFS_NODE_T;
  33993. +
  33994. +int vchiq_debugfs_init(void);
  33995. +
  33996. +void vchiq_debugfs_deinit(void);
  33997. +
  33998. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
  33999. +
  34000. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
  34001. +
  34002. +#endif /* VCHIQ_DEBUGFS_H */
  34003. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  34004. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 1969-12-31 18:00:00.000000000 -0600
  34005. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 2014-12-11 14:02:53.548418001 -0600
  34006. @@ -0,0 +1,87 @@
  34007. +#!/usr/bin/perl -w
  34008. +
  34009. +use strict;
  34010. +
  34011. +#
  34012. +# Generate a version from available information
  34013. +#
  34014. +
  34015. +my $prefix = shift @ARGV;
  34016. +my $root = shift @ARGV;
  34017. +
  34018. +
  34019. +if ( not defined $root ) {
  34020. + die "usage: $0 prefix root-dir\n";
  34021. +}
  34022. +
  34023. +if ( ! -d $root ) {
  34024. + die "root directory $root not found\n";
  34025. +}
  34026. +
  34027. +my $version = "unknown";
  34028. +my $tainted = "";
  34029. +
  34030. +if ( -d "$root/.git" ) {
  34031. + # attempt to work out git version. only do so
  34032. + # on a linux build host, as cygwin builds are
  34033. + # already slow enough
  34034. +
  34035. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  34036. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  34037. + $version = "no git version";
  34038. + }
  34039. + else {
  34040. + $version = <F>;
  34041. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  34042. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  34043. + }
  34044. +
  34045. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  34046. + $tainted = <G>;
  34047. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  34048. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  34049. + if (length $tainted) {
  34050. + $version = join ' ', $version, "(tainted)";
  34051. + }
  34052. + else {
  34053. + $version = join ' ', $version, "(clean)";
  34054. + }
  34055. + }
  34056. + }
  34057. +}
  34058. +
  34059. +my $hostname = `hostname`;
  34060. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  34061. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  34062. +
  34063. +
  34064. +print STDERR "Version $version\n";
  34065. +print <<EOF;
  34066. +#include "${prefix}_build_info.h"
  34067. +#include <linux/broadcom/vc_debug_sym.h>
  34068. +
  34069. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  34070. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  34071. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  34072. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  34073. +
  34074. +const char *vchiq_get_build_hostname( void )
  34075. +{
  34076. + return vchiq_build_hostname;
  34077. +}
  34078. +
  34079. +const char *vchiq_get_build_version( void )
  34080. +{
  34081. + return vchiq_build_version;
  34082. +}
  34083. +
  34084. +const char *vchiq_get_build_date( void )
  34085. +{
  34086. + return vchiq_build_date;
  34087. +}
  34088. +
  34089. +const char *vchiq_get_build_time( void )
  34090. +{
  34091. + return vchiq_build_time;
  34092. +}
  34093. +EOF
  34094. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  34095. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 1969-12-31 18:00:00.000000000 -0600
  34096. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 2014-12-11 14:02:53.548418001 -0600
  34097. @@ -0,0 +1,40 @@
  34098. +/**
  34099. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34100. + *
  34101. + * Redistribution and use in source and binary forms, with or without
  34102. + * modification, are permitted provided that the following conditions
  34103. + * are met:
  34104. + * 1. Redistributions of source code must retain the above copyright
  34105. + * notice, this list of conditions, and the following disclaimer,
  34106. + * without modification.
  34107. + * 2. Redistributions in binary form must reproduce the above copyright
  34108. + * notice, this list of conditions and the following disclaimer in the
  34109. + * documentation and/or other materials provided with the distribution.
  34110. + * 3. The names of the above-listed copyright holders may not be used
  34111. + * to endorse or promote products derived from this software without
  34112. + * specific prior written permission.
  34113. + *
  34114. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34115. + * GNU General Public License ("GPL") version 2, as published by the Free
  34116. + * Software Foundation.
  34117. + *
  34118. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34119. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34120. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34121. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34122. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34123. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34124. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34125. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34126. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34127. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34128. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34129. + */
  34130. +
  34131. +#ifndef VCHIQ_VCHIQ_H
  34132. +#define VCHIQ_VCHIQ_H
  34133. +
  34134. +#include "vchiq_if.h"
  34135. +#include "vchiq_util.h"
  34136. +
  34137. +#endif
  34138. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  34139. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 1969-12-31 18:00:00.000000000 -0600
  34140. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 2014-12-11 14:02:53.548418001 -0600
  34141. @@ -0,0 +1,189 @@
  34142. +/**
  34143. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34144. + *
  34145. + * Redistribution and use in source and binary forms, with or without
  34146. + * modification, are permitted provided that the following conditions
  34147. + * are met:
  34148. + * 1. Redistributions of source code must retain the above copyright
  34149. + * notice, this list of conditions, and the following disclaimer,
  34150. + * without modification.
  34151. + * 2. Redistributions in binary form must reproduce the above copyright
  34152. + * notice, this list of conditions and the following disclaimer in the
  34153. + * documentation and/or other materials provided with the distribution.
  34154. + * 3. The names of the above-listed copyright holders may not be used
  34155. + * to endorse or promote products derived from this software without
  34156. + * specific prior written permission.
  34157. + *
  34158. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34159. + * GNU General Public License ("GPL") version 2, as published by the Free
  34160. + * Software Foundation.
  34161. + *
  34162. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34163. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34164. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34165. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34166. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34167. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34168. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34169. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34170. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34171. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34172. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34173. + */
  34174. +
  34175. +#ifndef VCHIQ_IF_H
  34176. +#define VCHIQ_IF_H
  34177. +
  34178. +#include "interface/vchi/vchi_mh.h"
  34179. +
  34180. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  34181. +
  34182. +#define VCHIQ_SLOT_SIZE 4096
  34183. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  34184. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  34185. +
  34186. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  34187. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  34188. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  34189. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  34190. +
  34191. +typedef enum {
  34192. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  34193. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  34194. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  34195. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  34196. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  34197. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  34198. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  34199. +} VCHIQ_REASON_T;
  34200. +
  34201. +typedef enum {
  34202. + VCHIQ_ERROR = -1,
  34203. + VCHIQ_SUCCESS = 0,
  34204. + VCHIQ_RETRY = 1
  34205. +} VCHIQ_STATUS_T;
  34206. +
  34207. +typedef enum {
  34208. + VCHIQ_BULK_MODE_CALLBACK,
  34209. + VCHIQ_BULK_MODE_BLOCKING,
  34210. + VCHIQ_BULK_MODE_NOCALLBACK,
  34211. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  34212. +} VCHIQ_BULK_MODE_T;
  34213. +
  34214. +typedef enum {
  34215. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  34216. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  34217. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  34218. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
  34219. + VCHIQ_SERVICE_OPTION_TRACE
  34220. +} VCHIQ_SERVICE_OPTION_T;
  34221. +
  34222. +typedef struct vchiq_header_struct {
  34223. + /* The message identifier - opaque to applications. */
  34224. + int msgid;
  34225. +
  34226. + /* Size of message data. */
  34227. + unsigned int size;
  34228. +
  34229. + char data[0]; /* message */
  34230. +} VCHIQ_HEADER_T;
  34231. +
  34232. +typedef struct {
  34233. + const void *data;
  34234. + unsigned int size;
  34235. +} VCHIQ_ELEMENT_T;
  34236. +
  34237. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  34238. +
  34239. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  34240. + VCHIQ_SERVICE_HANDLE_T, void *);
  34241. +
  34242. +typedef struct vchiq_service_base_struct {
  34243. + int fourcc;
  34244. + VCHIQ_CALLBACK_T callback;
  34245. + void *userdata;
  34246. +} VCHIQ_SERVICE_BASE_T;
  34247. +
  34248. +typedef struct vchiq_service_params_struct {
  34249. + int fourcc;
  34250. + VCHIQ_CALLBACK_T callback;
  34251. + void *userdata;
  34252. + short version; /* Increment for non-trivial changes */
  34253. + short version_min; /* Update for incompatible changes */
  34254. +} VCHIQ_SERVICE_PARAMS_T;
  34255. +
  34256. +typedef struct vchiq_config_struct {
  34257. + unsigned int max_msg_size;
  34258. + unsigned int bulk_threshold; /* The message size above which it
  34259. + is better to use a bulk transfer
  34260. + (<= max_msg_size) */
  34261. + unsigned int max_outstanding_bulks;
  34262. + unsigned int max_services;
  34263. + short version; /* The version of VCHIQ */
  34264. + short version_min; /* The minimum compatible version of VCHIQ */
  34265. +} VCHIQ_CONFIG_T;
  34266. +
  34267. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  34268. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  34269. +
  34270. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  34271. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  34272. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  34273. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  34274. + const VCHIQ_SERVICE_PARAMS_T *params,
  34275. + VCHIQ_SERVICE_HANDLE_T *pservice);
  34276. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  34277. + const VCHIQ_SERVICE_PARAMS_T *params,
  34278. + VCHIQ_SERVICE_HANDLE_T *pservice);
  34279. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  34280. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  34281. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  34282. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  34283. + VCHIQ_SERVICE_HANDLE_T service);
  34284. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  34285. +
  34286. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  34287. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  34288. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  34289. + VCHIQ_HEADER_T *header);
  34290. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  34291. + const void *data, unsigned int size, void *userdata);
  34292. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  34293. + void *data, unsigned int size, void *userdata);
  34294. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  34295. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  34296. + const void *offset, unsigned int size, void *userdata);
  34297. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  34298. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  34299. + void *offset, unsigned int size, void *userdata);
  34300. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  34301. + const void *data, unsigned int size, void *userdata,
  34302. + VCHIQ_BULK_MODE_T mode);
  34303. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  34304. + void *data, unsigned int size, void *userdata,
  34305. + VCHIQ_BULK_MODE_T mode);
  34306. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  34307. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  34308. + void *userdata, VCHIQ_BULK_MODE_T mode);
  34309. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  34310. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  34311. + void *userdata, VCHIQ_BULK_MODE_T mode);
  34312. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  34313. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  34314. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  34315. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  34316. + int config_size, VCHIQ_CONFIG_T *pconfig);
  34317. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  34318. + VCHIQ_SERVICE_OPTION_T option, int value);
  34319. +
  34320. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  34321. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  34322. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  34323. +
  34324. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  34325. + void *ptr, size_t num_bytes);
  34326. +
  34327. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  34328. + short *peer_version);
  34329. +
  34330. +#endif /* VCHIQ_IF_H */
  34331. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  34332. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 1969-12-31 18:00:00.000000000 -0600
  34333. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 2014-12-11 14:02:53.548418001 -0600
  34334. @@ -0,0 +1,131 @@
  34335. +/**
  34336. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34337. + *
  34338. + * Redistribution and use in source and binary forms, with or without
  34339. + * modification, are permitted provided that the following conditions
  34340. + * are met:
  34341. + * 1. Redistributions of source code must retain the above copyright
  34342. + * notice, this list of conditions, and the following disclaimer,
  34343. + * without modification.
  34344. + * 2. Redistributions in binary form must reproduce the above copyright
  34345. + * notice, this list of conditions and the following disclaimer in the
  34346. + * documentation and/or other materials provided with the distribution.
  34347. + * 3. The names of the above-listed copyright holders may not be used
  34348. + * to endorse or promote products derived from this software without
  34349. + * specific prior written permission.
  34350. + *
  34351. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34352. + * GNU General Public License ("GPL") version 2, as published by the Free
  34353. + * Software Foundation.
  34354. + *
  34355. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34356. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34357. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34358. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34359. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34360. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34361. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34362. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34363. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34364. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34365. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34366. + */
  34367. +
  34368. +#ifndef VCHIQ_IOCTLS_H
  34369. +#define VCHIQ_IOCTLS_H
  34370. +
  34371. +#include <linux/ioctl.h>
  34372. +#include "vchiq_if.h"
  34373. +
  34374. +#define VCHIQ_IOC_MAGIC 0xc4
  34375. +#define VCHIQ_INVALID_HANDLE (~0)
  34376. +
  34377. +typedef struct {
  34378. + VCHIQ_SERVICE_PARAMS_T params;
  34379. + int is_open;
  34380. + int is_vchi;
  34381. + unsigned int handle; /* OUT */
  34382. +} VCHIQ_CREATE_SERVICE_T;
  34383. +
  34384. +typedef struct {
  34385. + unsigned int handle;
  34386. + unsigned int count;
  34387. + const VCHIQ_ELEMENT_T *elements;
  34388. +} VCHIQ_QUEUE_MESSAGE_T;
  34389. +
  34390. +typedef struct {
  34391. + unsigned int handle;
  34392. + void *data;
  34393. + unsigned int size;
  34394. + void *userdata;
  34395. + VCHIQ_BULK_MODE_T mode;
  34396. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  34397. +
  34398. +typedef struct {
  34399. + VCHIQ_REASON_T reason;
  34400. + VCHIQ_HEADER_T *header;
  34401. + void *service_userdata;
  34402. + void *bulk_userdata;
  34403. +} VCHIQ_COMPLETION_DATA_T;
  34404. +
  34405. +typedef struct {
  34406. + unsigned int count;
  34407. + VCHIQ_COMPLETION_DATA_T *buf;
  34408. + unsigned int msgbufsize;
  34409. + unsigned int msgbufcount; /* IN/OUT */
  34410. + void **msgbufs;
  34411. +} VCHIQ_AWAIT_COMPLETION_T;
  34412. +
  34413. +typedef struct {
  34414. + unsigned int handle;
  34415. + int blocking;
  34416. + unsigned int bufsize;
  34417. + void *buf;
  34418. +} VCHIQ_DEQUEUE_MESSAGE_T;
  34419. +
  34420. +typedef struct {
  34421. + unsigned int config_size;
  34422. + VCHIQ_CONFIG_T *pconfig;
  34423. +} VCHIQ_GET_CONFIG_T;
  34424. +
  34425. +typedef struct {
  34426. + unsigned int handle;
  34427. + VCHIQ_SERVICE_OPTION_T option;
  34428. + int value;
  34429. +} VCHIQ_SET_SERVICE_OPTION_T;
  34430. +
  34431. +typedef struct {
  34432. + void *virt_addr;
  34433. + size_t num_bytes;
  34434. +} VCHIQ_DUMP_MEM_T;
  34435. +
  34436. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  34437. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  34438. +#define VCHIQ_IOC_CREATE_SERVICE \
  34439. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  34440. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  34441. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  34442. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  34443. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  34444. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  34445. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  34446. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  34447. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  34448. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  34449. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  34450. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  34451. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  34452. +#define VCHIQ_IOC_GET_CONFIG \
  34453. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  34454. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  34455. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  34456. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  34457. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  34458. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  34459. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  34460. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  34461. +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
  34462. +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
  34463. +#define VCHIQ_IOC_MAX 17
  34464. +
  34465. +#endif
  34466. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  34467. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 1969-12-31 18:00:00.000000000 -0600
  34468. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 2014-12-11 14:02:53.548418001 -0600
  34469. @@ -0,0 +1,458 @@
  34470. +/**
  34471. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34472. + *
  34473. + * Redistribution and use in source and binary forms, with or without
  34474. + * modification, are permitted provided that the following conditions
  34475. + * are met:
  34476. + * 1. Redistributions of source code must retain the above copyright
  34477. + * notice, this list of conditions, and the following disclaimer,
  34478. + * without modification.
  34479. + * 2. Redistributions in binary form must reproduce the above copyright
  34480. + * notice, this list of conditions and the following disclaimer in the
  34481. + * documentation and/or other materials provided with the distribution.
  34482. + * 3. The names of the above-listed copyright holders may not be used
  34483. + * to endorse or promote products derived from this software without
  34484. + * specific prior written permission.
  34485. + *
  34486. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34487. + * GNU General Public License ("GPL") version 2, as published by the Free
  34488. + * Software Foundation.
  34489. + *
  34490. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34491. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34492. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34493. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34494. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34495. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34496. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34497. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34498. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34499. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34500. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34501. + */
  34502. +
  34503. +/* ---- Include Files ---------------------------------------------------- */
  34504. +
  34505. +#include <linux/kernel.h>
  34506. +#include <linux/module.h>
  34507. +#include <linux/mutex.h>
  34508. +
  34509. +#include "vchiq_core.h"
  34510. +#include "vchiq_arm.h"
  34511. +#include "vchiq_killable.h"
  34512. +
  34513. +/* ---- Public Variables ------------------------------------------------- */
  34514. +
  34515. +/* ---- Private Constants and Types -------------------------------------- */
  34516. +
  34517. +struct bulk_waiter_node {
  34518. + struct bulk_waiter bulk_waiter;
  34519. + int pid;
  34520. + struct list_head list;
  34521. +};
  34522. +
  34523. +struct vchiq_instance_struct {
  34524. + VCHIQ_STATE_T *state;
  34525. +
  34526. + int connected;
  34527. +
  34528. + struct list_head bulk_waiter_list;
  34529. + struct mutex bulk_waiter_list_mutex;
  34530. +};
  34531. +
  34532. +static VCHIQ_STATUS_T
  34533. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  34534. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  34535. +
  34536. +/****************************************************************************
  34537. +*
  34538. +* vchiq_initialise
  34539. +*
  34540. +***************************************************************************/
  34541. +#define VCHIQ_INIT_RETRIES 10
  34542. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  34543. +{
  34544. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  34545. + VCHIQ_STATE_T *state;
  34546. + VCHIQ_INSTANCE_T instance = NULL;
  34547. + int i;
  34548. +
  34549. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  34550. +
  34551. + /* VideoCore may not be ready due to boot up timing.
  34552. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  34553. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  34554. + state = vchiq_get_state();
  34555. + if (state)
  34556. + break;
  34557. + udelay(500);
  34558. + }
  34559. + if (i==VCHIQ_INIT_RETRIES) {
  34560. + vchiq_log_error(vchiq_core_log_level,
  34561. + "%s: videocore not initialized\n", __func__);
  34562. + goto failed;
  34563. + } else if (i>0) {
  34564. + vchiq_log_warning(vchiq_core_log_level,
  34565. + "%s: videocore initialized after %d retries\n", __func__, i);
  34566. + }
  34567. +
  34568. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  34569. + if (!instance) {
  34570. + vchiq_log_error(vchiq_core_log_level,
  34571. + "%s: error allocating vchiq instance\n", __func__);
  34572. + goto failed;
  34573. + }
  34574. +
  34575. + instance->connected = 0;
  34576. + instance->state = state;
  34577. + mutex_init(&instance->bulk_waiter_list_mutex);
  34578. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  34579. +
  34580. + *instanceOut = instance;
  34581. +
  34582. + status = VCHIQ_SUCCESS;
  34583. +
  34584. +failed:
  34585. + vchiq_log_trace(vchiq_core_log_level,
  34586. + "%s(%p): returning %d", __func__, instance, status);
  34587. +
  34588. + return status;
  34589. +}
  34590. +EXPORT_SYMBOL(vchiq_initialise);
  34591. +
  34592. +/****************************************************************************
  34593. +*
  34594. +* vchiq_shutdown
  34595. +*
  34596. +***************************************************************************/
  34597. +
  34598. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  34599. +{
  34600. + VCHIQ_STATUS_T status;
  34601. + VCHIQ_STATE_T *state = instance->state;
  34602. +
  34603. + vchiq_log_trace(vchiq_core_log_level,
  34604. + "%s(%p) called", __func__, instance);
  34605. +
  34606. + if (mutex_lock_interruptible(&state->mutex) != 0)
  34607. + return VCHIQ_RETRY;
  34608. +
  34609. + /* Remove all services */
  34610. + status = vchiq_shutdown_internal(state, instance);
  34611. +
  34612. + mutex_unlock(&state->mutex);
  34613. +
  34614. + vchiq_log_trace(vchiq_core_log_level,
  34615. + "%s(%p): returning %d", __func__, instance, status);
  34616. +
  34617. + if (status == VCHIQ_SUCCESS) {
  34618. + struct list_head *pos, *next;
  34619. + list_for_each_safe(pos, next,
  34620. + &instance->bulk_waiter_list) {
  34621. + struct bulk_waiter_node *waiter;
  34622. + waiter = list_entry(pos,
  34623. + struct bulk_waiter_node,
  34624. + list);
  34625. + list_del(pos);
  34626. + vchiq_log_info(vchiq_arm_log_level,
  34627. + "bulk_waiter - cleaned up %x "
  34628. + "for pid %d",
  34629. + (unsigned int)waiter, waiter->pid);
  34630. + kfree(waiter);
  34631. + }
  34632. + kfree(instance);
  34633. + }
  34634. +
  34635. + return status;
  34636. +}
  34637. +EXPORT_SYMBOL(vchiq_shutdown);
  34638. +
  34639. +/****************************************************************************
  34640. +*
  34641. +* vchiq_is_connected
  34642. +*
  34643. +***************************************************************************/
  34644. +
  34645. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  34646. +{
  34647. + return instance->connected;
  34648. +}
  34649. +
  34650. +/****************************************************************************
  34651. +*
  34652. +* vchiq_connect
  34653. +*
  34654. +***************************************************************************/
  34655. +
  34656. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  34657. +{
  34658. + VCHIQ_STATUS_T status;
  34659. + VCHIQ_STATE_T *state = instance->state;
  34660. +
  34661. + vchiq_log_trace(vchiq_core_log_level,
  34662. + "%s(%p) called", __func__, instance);
  34663. +
  34664. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  34665. + vchiq_log_trace(vchiq_core_log_level,
  34666. + "%s: call to mutex_lock failed", __func__);
  34667. + status = VCHIQ_RETRY;
  34668. + goto failed;
  34669. + }
  34670. + status = vchiq_connect_internal(state, instance);
  34671. +
  34672. + if (status == VCHIQ_SUCCESS)
  34673. + instance->connected = 1;
  34674. +
  34675. + mutex_unlock(&state->mutex);
  34676. +
  34677. +failed:
  34678. + vchiq_log_trace(vchiq_core_log_level,
  34679. + "%s(%p): returning %d", __func__, instance, status);
  34680. +
  34681. + return status;
  34682. +}
  34683. +EXPORT_SYMBOL(vchiq_connect);
  34684. +
  34685. +/****************************************************************************
  34686. +*
  34687. +* vchiq_add_service
  34688. +*
  34689. +***************************************************************************/
  34690. +
  34691. +VCHIQ_STATUS_T vchiq_add_service(
  34692. + VCHIQ_INSTANCE_T instance,
  34693. + const VCHIQ_SERVICE_PARAMS_T *params,
  34694. + VCHIQ_SERVICE_HANDLE_T *phandle)
  34695. +{
  34696. + VCHIQ_STATUS_T status;
  34697. + VCHIQ_STATE_T *state = instance->state;
  34698. + VCHIQ_SERVICE_T *service = NULL;
  34699. + int srvstate;
  34700. +
  34701. + vchiq_log_trace(vchiq_core_log_level,
  34702. + "%s(%p) called", __func__, instance);
  34703. +
  34704. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  34705. +
  34706. + srvstate = vchiq_is_connected(instance)
  34707. + ? VCHIQ_SRVSTATE_LISTENING
  34708. + : VCHIQ_SRVSTATE_HIDDEN;
  34709. +
  34710. + service = vchiq_add_service_internal(
  34711. + state,
  34712. + params,
  34713. + srvstate,
  34714. + instance,
  34715. + NULL);
  34716. +
  34717. + if (service) {
  34718. + *phandle = service->handle;
  34719. + status = VCHIQ_SUCCESS;
  34720. + } else
  34721. + status = VCHIQ_ERROR;
  34722. +
  34723. + vchiq_log_trace(vchiq_core_log_level,
  34724. + "%s(%p): returning %d", __func__, instance, status);
  34725. +
  34726. + return status;
  34727. +}
  34728. +EXPORT_SYMBOL(vchiq_add_service);
  34729. +
  34730. +/****************************************************************************
  34731. +*
  34732. +* vchiq_open_service
  34733. +*
  34734. +***************************************************************************/
  34735. +
  34736. +VCHIQ_STATUS_T vchiq_open_service(
  34737. + VCHIQ_INSTANCE_T instance,
  34738. + const VCHIQ_SERVICE_PARAMS_T *params,
  34739. + VCHIQ_SERVICE_HANDLE_T *phandle)
  34740. +{
  34741. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  34742. + VCHIQ_STATE_T *state = instance->state;
  34743. + VCHIQ_SERVICE_T *service = NULL;
  34744. +
  34745. + vchiq_log_trace(vchiq_core_log_level,
  34746. + "%s(%p) called", __func__, instance);
  34747. +
  34748. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  34749. +
  34750. + if (!vchiq_is_connected(instance))
  34751. + goto failed;
  34752. +
  34753. + service = vchiq_add_service_internal(state,
  34754. + params,
  34755. + VCHIQ_SRVSTATE_OPENING,
  34756. + instance,
  34757. + NULL);
  34758. +
  34759. + if (service) {
  34760. + *phandle = service->handle;
  34761. + status = vchiq_open_service_internal(service, current->pid);
  34762. + if (status != VCHIQ_SUCCESS) {
  34763. + vchiq_remove_service(service->handle);
  34764. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  34765. + }
  34766. + }
  34767. +
  34768. +failed:
  34769. + vchiq_log_trace(vchiq_core_log_level,
  34770. + "%s(%p): returning %d", __func__, instance, status);
  34771. +
  34772. + return status;
  34773. +}
  34774. +EXPORT_SYMBOL(vchiq_open_service);
  34775. +
  34776. +VCHIQ_STATUS_T
  34777. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  34778. + const void *data, unsigned int size, void *userdata)
  34779. +{
  34780. + return vchiq_bulk_transfer(handle,
  34781. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  34782. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  34783. +}
  34784. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  34785. +
  34786. +VCHIQ_STATUS_T
  34787. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  34788. + unsigned int size, void *userdata)
  34789. +{
  34790. + return vchiq_bulk_transfer(handle,
  34791. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  34792. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  34793. +}
  34794. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  34795. +
  34796. +VCHIQ_STATUS_T
  34797. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  34798. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  34799. +{
  34800. + VCHIQ_STATUS_T status;
  34801. +
  34802. + switch (mode) {
  34803. + case VCHIQ_BULK_MODE_NOCALLBACK:
  34804. + case VCHIQ_BULK_MODE_CALLBACK:
  34805. + status = vchiq_bulk_transfer(handle,
  34806. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  34807. + mode, VCHIQ_BULK_TRANSMIT);
  34808. + break;
  34809. + case VCHIQ_BULK_MODE_BLOCKING:
  34810. + status = vchiq_blocking_bulk_transfer(handle,
  34811. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  34812. + break;
  34813. + default:
  34814. + return VCHIQ_ERROR;
  34815. + }
  34816. +
  34817. + return status;
  34818. +}
  34819. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  34820. +
  34821. +VCHIQ_STATUS_T
  34822. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  34823. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  34824. +{
  34825. + VCHIQ_STATUS_T status;
  34826. +
  34827. + switch (mode) {
  34828. + case VCHIQ_BULK_MODE_NOCALLBACK:
  34829. + case VCHIQ_BULK_MODE_CALLBACK:
  34830. + status = vchiq_bulk_transfer(handle,
  34831. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  34832. + mode, VCHIQ_BULK_RECEIVE);
  34833. + break;
  34834. + case VCHIQ_BULK_MODE_BLOCKING:
  34835. + status = vchiq_blocking_bulk_transfer(handle,
  34836. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  34837. + break;
  34838. + default:
  34839. + return VCHIQ_ERROR;
  34840. + }
  34841. +
  34842. + return status;
  34843. +}
  34844. +EXPORT_SYMBOL(vchiq_bulk_receive);
  34845. +
  34846. +static VCHIQ_STATUS_T
  34847. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  34848. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  34849. +{
  34850. + VCHIQ_INSTANCE_T instance;
  34851. + VCHIQ_SERVICE_T *service;
  34852. + VCHIQ_STATUS_T status;
  34853. + struct bulk_waiter_node *waiter = NULL;
  34854. + struct list_head *pos;
  34855. +
  34856. + service = find_service_by_handle(handle);
  34857. + if (!service)
  34858. + return VCHIQ_ERROR;
  34859. +
  34860. + instance = service->instance;
  34861. +
  34862. + unlock_service(service);
  34863. +
  34864. + mutex_lock(&instance->bulk_waiter_list_mutex);
  34865. + list_for_each(pos, &instance->bulk_waiter_list) {
  34866. + if (list_entry(pos, struct bulk_waiter_node,
  34867. + list)->pid == current->pid) {
  34868. + waiter = list_entry(pos,
  34869. + struct bulk_waiter_node,
  34870. + list);
  34871. + list_del(pos);
  34872. + break;
  34873. + }
  34874. + }
  34875. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  34876. +
  34877. + if (waiter) {
  34878. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  34879. + if (bulk) {
  34880. + /* This thread has an outstanding bulk transfer. */
  34881. + if ((bulk->data != data) ||
  34882. + (bulk->size != size)) {
  34883. + /* This is not a retry of the previous one.
  34884. + ** Cancel the signal when the transfer
  34885. + ** completes. */
  34886. + spin_lock(&bulk_waiter_spinlock);
  34887. + bulk->userdata = NULL;
  34888. + spin_unlock(&bulk_waiter_spinlock);
  34889. + }
  34890. + }
  34891. + }
  34892. +
  34893. + if (!waiter) {
  34894. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  34895. + if (!waiter) {
  34896. + vchiq_log_error(vchiq_core_log_level,
  34897. + "%s - out of memory", __func__);
  34898. + return VCHIQ_ERROR;
  34899. + }
  34900. + }
  34901. +
  34902. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  34903. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  34904. + dir);
  34905. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  34906. + !waiter->bulk_waiter.bulk) {
  34907. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  34908. + if (bulk) {
  34909. + /* Cancel the signal when the transfer
  34910. + ** completes. */
  34911. + spin_lock(&bulk_waiter_spinlock);
  34912. + bulk->userdata = NULL;
  34913. + spin_unlock(&bulk_waiter_spinlock);
  34914. + }
  34915. + kfree(waiter);
  34916. + } else {
  34917. + waiter->pid = current->pid;
  34918. + mutex_lock(&instance->bulk_waiter_list_mutex);
  34919. + list_add(&waiter->list, &instance->bulk_waiter_list);
  34920. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  34921. + vchiq_log_info(vchiq_arm_log_level,
  34922. + "saved bulk_waiter %x for pid %d",
  34923. + (unsigned int)waiter, current->pid);
  34924. + }
  34925. +
  34926. + return status;
  34927. +}
  34928. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h
  34929. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h 1969-12-31 18:00:00.000000000 -0600
  34930. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h 2014-12-11 14:02:53.548418001 -0600
  34931. @@ -0,0 +1,69 @@
  34932. +/**
  34933. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34934. + *
  34935. + * Redistribution and use in source and binary forms, with or without
  34936. + * modification, are permitted provided that the following conditions
  34937. + * are met:
  34938. + * 1. Redistributions of source code must retain the above copyright
  34939. + * notice, this list of conditions, and the following disclaimer,
  34940. + * without modification.
  34941. + * 2. Redistributions in binary form must reproduce the above copyright
  34942. + * notice, this list of conditions and the following disclaimer in the
  34943. + * documentation and/or other materials provided with the distribution.
  34944. + * 3. The names of the above-listed copyright holders may not be used
  34945. + * to endorse or promote products derived from this software without
  34946. + * specific prior written permission.
  34947. + *
  34948. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34949. + * GNU General Public License ("GPL") version 2, as published by the Free
  34950. + * Software Foundation.
  34951. + *
  34952. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34953. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34954. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34955. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34956. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34957. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34958. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34959. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34960. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34961. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34962. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34963. + */
  34964. +
  34965. +#ifndef VCHIQ_KILLABLE_H
  34966. +#define VCHIQ_KILLABLE_H
  34967. +
  34968. +#include <linux/mutex.h>
  34969. +#include <linux/semaphore.h>
  34970. +
  34971. +#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTRAP) | sigmask(SIGSTOP) | sigmask(SIGCONT))
  34972. +
  34973. +static inline int __must_check down_interruptible_killable(struct semaphore *sem)
  34974. +{
  34975. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  34976. + int ret;
  34977. + sigset_t blocked, oldset;
  34978. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  34979. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  34980. + ret = down_interruptible(sem);
  34981. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  34982. + return ret;
  34983. +}
  34984. +#define down_interruptible down_interruptible_killable
  34985. +
  34986. +
  34987. +static inline int __must_check mutex_lock_interruptible_killable(struct mutex *lock)
  34988. +{
  34989. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  34990. + int ret;
  34991. + sigset_t blocked, oldset;
  34992. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  34993. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  34994. + ret = mutex_lock_interruptible(lock);
  34995. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  34996. + return ret;
  34997. +}
  34998. +#define mutex_lock_interruptible mutex_lock_interruptible_killable
  34999. +
  35000. +#endif
  35001. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  35002. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 1969-12-31 18:00:00.000000000 -0600
  35003. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 2014-12-11 14:02:53.548418001 -0600
  35004. @@ -0,0 +1,71 @@
  35005. +/**
  35006. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35007. + *
  35008. + * Redistribution and use in source and binary forms, with or without
  35009. + * modification, are permitted provided that the following conditions
  35010. + * are met:
  35011. + * 1. Redistributions of source code must retain the above copyright
  35012. + * notice, this list of conditions, and the following disclaimer,
  35013. + * without modification.
  35014. + * 2. Redistributions in binary form must reproduce the above copyright
  35015. + * notice, this list of conditions and the following disclaimer in the
  35016. + * documentation and/or other materials provided with the distribution.
  35017. + * 3. The names of the above-listed copyright holders may not be used
  35018. + * to endorse or promote products derived from this software without
  35019. + * specific prior written permission.
  35020. + *
  35021. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35022. + * GNU General Public License ("GPL") version 2, as published by the Free
  35023. + * Software Foundation.
  35024. + *
  35025. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35026. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35027. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35028. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35029. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35030. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35031. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35032. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35033. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35034. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35035. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35036. + */
  35037. +
  35038. +#ifndef VCHIQ_MEMDRV_H
  35039. +#define VCHIQ_MEMDRV_H
  35040. +
  35041. +/* ---- Include Files ----------------------------------------------------- */
  35042. +
  35043. +#include <linux/kernel.h>
  35044. +#include "vchiq_if.h"
  35045. +
  35046. +/* ---- Constants and Types ---------------------------------------------- */
  35047. +
  35048. +typedef struct {
  35049. + void *armSharedMemVirt;
  35050. + dma_addr_t armSharedMemPhys;
  35051. + size_t armSharedMemSize;
  35052. +
  35053. + void *vcSharedMemVirt;
  35054. + dma_addr_t vcSharedMemPhys;
  35055. + size_t vcSharedMemSize;
  35056. +} VCHIQ_SHARED_MEM_INFO_T;
  35057. +
  35058. +/* ---- Variable Externs ------------------------------------------------- */
  35059. +
  35060. +/* ---- Function Prototypes ---------------------------------------------- */
  35061. +
  35062. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  35063. +
  35064. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  35065. +
  35066. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  35067. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  35068. +
  35069. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  35070. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  35071. +
  35072. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  35073. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  35074. +
  35075. +#endif
  35076. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  35077. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 1969-12-31 18:00:00.000000000 -0600
  35078. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 2014-12-11 14:02:53.548418001 -0600
  35079. @@ -0,0 +1,58 @@
  35080. +/**
  35081. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35082. + *
  35083. + * Redistribution and use in source and binary forms, with or without
  35084. + * modification, are permitted provided that the following conditions
  35085. + * are met:
  35086. + * 1. Redistributions of source code must retain the above copyright
  35087. + * notice, this list of conditions, and the following disclaimer,
  35088. + * without modification.
  35089. + * 2. Redistributions in binary form must reproduce the above copyright
  35090. + * notice, this list of conditions and the following disclaimer in the
  35091. + * documentation and/or other materials provided with the distribution.
  35092. + * 3. The names of the above-listed copyright holders may not be used
  35093. + * to endorse or promote products derived from this software without
  35094. + * specific prior written permission.
  35095. + *
  35096. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35097. + * GNU General Public License ("GPL") version 2, as published by the Free
  35098. + * Software Foundation.
  35099. + *
  35100. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35101. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35102. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35103. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35104. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35105. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35106. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35107. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35108. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35109. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35110. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35111. + */
  35112. +
  35113. +#ifndef VCHIQ_PAGELIST_H
  35114. +#define VCHIQ_PAGELIST_H
  35115. +
  35116. +#ifndef PAGE_SIZE
  35117. +#define PAGE_SIZE 4096
  35118. +#endif
  35119. +#define CACHE_LINE_SIZE 32
  35120. +#define PAGELIST_WRITE 0
  35121. +#define PAGELIST_READ 1
  35122. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  35123. +
  35124. +typedef struct pagelist_struct {
  35125. + unsigned long length;
  35126. + unsigned short type;
  35127. + unsigned short offset;
  35128. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  35129. + pages at consecutive addresses. */
  35130. +} PAGELIST_T;
  35131. +
  35132. +typedef struct fragments_struct {
  35133. + char headbuf[CACHE_LINE_SIZE];
  35134. + char tailbuf[CACHE_LINE_SIZE];
  35135. +} FRAGMENTS_T;
  35136. +
  35137. +#endif /* VCHIQ_PAGELIST_H */
  35138. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  35139. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 1969-12-31 18:00:00.000000000 -0600
  35140. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 2014-12-11 14:02:53.548418001 -0600
  35141. @@ -0,0 +1,857 @@
  35142. +/**
  35143. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35144. + *
  35145. + * Redistribution and use in source and binary forms, with or without
  35146. + * modification, are permitted provided that the following conditions
  35147. + * are met:
  35148. + * 1. Redistributions of source code must retain the above copyright
  35149. + * notice, this list of conditions, and the following disclaimer,
  35150. + * without modification.
  35151. + * 2. Redistributions in binary form must reproduce the above copyright
  35152. + * notice, this list of conditions and the following disclaimer in the
  35153. + * documentation and/or other materials provided with the distribution.
  35154. + * 3. The names of the above-listed copyright holders may not be used
  35155. + * to endorse or promote products derived from this software without
  35156. + * specific prior written permission.
  35157. + *
  35158. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35159. + * GNU General Public License ("GPL") version 2, as published by the Free
  35160. + * Software Foundation.
  35161. + *
  35162. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35163. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35164. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35165. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35166. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35167. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35168. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35169. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35170. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35171. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35172. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35173. + */
  35174. +#include <linux/module.h>
  35175. +#include <linux/types.h>
  35176. +
  35177. +#include "interface/vchi/vchi.h"
  35178. +#include "vchiq.h"
  35179. +#include "vchiq_core.h"
  35180. +
  35181. +#include "vchiq_util.h"
  35182. +
  35183. +#include <stddef.h>
  35184. +
  35185. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  35186. +
  35187. +typedef struct {
  35188. + VCHIQ_SERVICE_HANDLE_T handle;
  35189. +
  35190. + VCHIU_QUEUE_T queue;
  35191. +
  35192. + VCHI_CALLBACK_T callback;
  35193. + void *callback_param;
  35194. +} SHIM_SERVICE_T;
  35195. +
  35196. +/* ----------------------------------------------------------------------
  35197. + * return pointer to the mphi message driver function table
  35198. + * -------------------------------------------------------------------- */
  35199. +const VCHI_MESSAGE_DRIVER_T *
  35200. +vchi_mphi_message_driver_func_table(void)
  35201. +{
  35202. + return NULL;
  35203. +}
  35204. +
  35205. +/* ----------------------------------------------------------------------
  35206. + * return a pointer to the 'single' connection driver fops
  35207. + * -------------------------------------------------------------------- */
  35208. +const VCHI_CONNECTION_API_T *
  35209. +single_get_func_table(void)
  35210. +{
  35211. + return NULL;
  35212. +}
  35213. +
  35214. +VCHI_CONNECTION_T *vchi_create_connection(
  35215. + const VCHI_CONNECTION_API_T *function_table,
  35216. + const VCHI_MESSAGE_DRIVER_T *low_level)
  35217. +{
  35218. + (void)function_table;
  35219. + (void)low_level;
  35220. + return NULL;
  35221. +}
  35222. +
  35223. +/***********************************************************
  35224. + * Name: vchi_msg_peek
  35225. + *
  35226. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  35227. + * void **data,
  35228. + * uint32_t *msg_size,
  35229. +
  35230. +
  35231. + * VCHI_FLAGS_T flags
  35232. + *
  35233. + * Description: Routine to return a pointer to the current message (to allow in
  35234. + * place processing). The message can be removed using
  35235. + * vchi_msg_remove when you're finished
  35236. + *
  35237. + * Returns: int32_t - success == 0
  35238. + *
  35239. + ***********************************************************/
  35240. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  35241. + void **data,
  35242. + uint32_t *msg_size,
  35243. + VCHI_FLAGS_T flags)
  35244. +{
  35245. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35246. + VCHIQ_HEADER_T *header;
  35247. +
  35248. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  35249. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  35250. +
  35251. + if (flags == VCHI_FLAGS_NONE)
  35252. + if (vchiu_queue_is_empty(&service->queue))
  35253. + return -1;
  35254. +
  35255. + header = vchiu_queue_peek(&service->queue);
  35256. +
  35257. + *data = header->data;
  35258. + *msg_size = header->size;
  35259. +
  35260. + return 0;
  35261. +}
  35262. +EXPORT_SYMBOL(vchi_msg_peek);
  35263. +
  35264. +/***********************************************************
  35265. + * Name: vchi_msg_remove
  35266. + *
  35267. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  35268. + *
  35269. + * Description: Routine to remove a message (after it has been read with
  35270. + * vchi_msg_peek)
  35271. + *
  35272. + * Returns: int32_t - success == 0
  35273. + *
  35274. + ***********************************************************/
  35275. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  35276. +{
  35277. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35278. + VCHIQ_HEADER_T *header;
  35279. +
  35280. + header = vchiu_queue_pop(&service->queue);
  35281. +
  35282. + vchiq_release_message(service->handle, header);
  35283. +
  35284. + return 0;
  35285. +}
  35286. +EXPORT_SYMBOL(vchi_msg_remove);
  35287. +
  35288. +/***********************************************************
  35289. + * Name: vchi_msg_queue
  35290. + *
  35291. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  35292. + * const void *data,
  35293. + * uint32_t data_size,
  35294. + * VCHI_FLAGS_T flags,
  35295. + * void *msg_handle,
  35296. + *
  35297. + * Description: Thin wrapper to queue a message onto a connection
  35298. + *
  35299. + * Returns: int32_t - success == 0
  35300. + *
  35301. + ***********************************************************/
  35302. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  35303. + const void *data,
  35304. + uint32_t data_size,
  35305. + VCHI_FLAGS_T flags,
  35306. + void *msg_handle)
  35307. +{
  35308. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35309. + VCHIQ_ELEMENT_T element = {data, data_size};
  35310. + VCHIQ_STATUS_T status;
  35311. +
  35312. + (void)msg_handle;
  35313. +
  35314. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  35315. +
  35316. + status = vchiq_queue_message(service->handle, &element, 1);
  35317. +
  35318. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  35319. + ** implement a retry mechanism since this function is supposed
  35320. + ** to block until queued
  35321. + */
  35322. + while (status == VCHIQ_RETRY) {
  35323. + msleep(1);
  35324. + status = vchiq_queue_message(service->handle, &element, 1);
  35325. + }
  35326. +
  35327. + return vchiq_status_to_vchi(status);
  35328. +}
  35329. +EXPORT_SYMBOL(vchi_msg_queue);
  35330. +
  35331. +/***********************************************************
  35332. + * Name: vchi_bulk_queue_receive
  35333. + *
  35334. + * Arguments: VCHI_BULK_HANDLE_T handle,
  35335. + * void *data_dst,
  35336. + * const uint32_t data_size,
  35337. + * VCHI_FLAGS_T flags
  35338. + * void *bulk_handle
  35339. + *
  35340. + * Description: Routine to setup a rcv buffer
  35341. + *
  35342. + * Returns: int32_t - success == 0
  35343. + *
  35344. + ***********************************************************/
  35345. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  35346. + void *data_dst,
  35347. + uint32_t data_size,
  35348. + VCHI_FLAGS_T flags,
  35349. + void *bulk_handle)
  35350. +{
  35351. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35352. + VCHIQ_BULK_MODE_T mode;
  35353. + VCHIQ_STATUS_T status;
  35354. +
  35355. + switch ((int)flags) {
  35356. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  35357. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  35358. + WARN_ON(!service->callback);
  35359. + mode = VCHIQ_BULK_MODE_CALLBACK;
  35360. + break;
  35361. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  35362. + mode = VCHIQ_BULK_MODE_BLOCKING;
  35363. + break;
  35364. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  35365. + case VCHI_FLAGS_NONE:
  35366. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  35367. + break;
  35368. + default:
  35369. + WARN(1, "unsupported message\n");
  35370. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  35371. + }
  35372. +
  35373. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  35374. + bulk_handle, mode);
  35375. +
  35376. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  35377. + ** implement a retry mechanism since this function is supposed
  35378. + ** to block until queued
  35379. + */
  35380. + while (status == VCHIQ_RETRY) {
  35381. + msleep(1);
  35382. + status = vchiq_bulk_receive(service->handle, data_dst,
  35383. + data_size, bulk_handle, mode);
  35384. + }
  35385. +
  35386. + return vchiq_status_to_vchi(status);
  35387. +}
  35388. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  35389. +
  35390. +/***********************************************************
  35391. + * Name: vchi_bulk_queue_transmit
  35392. + *
  35393. + * Arguments: VCHI_BULK_HANDLE_T handle,
  35394. + * const void *data_src,
  35395. + * uint32_t data_size,
  35396. + * VCHI_FLAGS_T flags,
  35397. + * void *bulk_handle
  35398. + *
  35399. + * Description: Routine to transmit some data
  35400. + *
  35401. + * Returns: int32_t - success == 0
  35402. + *
  35403. + ***********************************************************/
  35404. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  35405. + const void *data_src,
  35406. + uint32_t data_size,
  35407. + VCHI_FLAGS_T flags,
  35408. + void *bulk_handle)
  35409. +{
  35410. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35411. + VCHIQ_BULK_MODE_T mode;
  35412. + VCHIQ_STATUS_T status;
  35413. +
  35414. + switch ((int)flags) {
  35415. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  35416. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  35417. + WARN_ON(!service->callback);
  35418. + mode = VCHIQ_BULK_MODE_CALLBACK;
  35419. + break;
  35420. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  35421. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  35422. + mode = VCHIQ_BULK_MODE_BLOCKING;
  35423. + break;
  35424. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  35425. + case VCHI_FLAGS_NONE:
  35426. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  35427. + break;
  35428. + default:
  35429. + WARN(1, "unsupported message\n");
  35430. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  35431. + }
  35432. +
  35433. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  35434. + bulk_handle, mode);
  35435. +
  35436. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  35437. + ** implement a retry mechanism since this function is supposed
  35438. + ** to block until queued
  35439. + */
  35440. + while (status == VCHIQ_RETRY) {
  35441. + msleep(1);
  35442. + status = vchiq_bulk_transmit(service->handle, data_src,
  35443. + data_size, bulk_handle, mode);
  35444. + }
  35445. +
  35446. + return vchiq_status_to_vchi(status);
  35447. +}
  35448. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  35449. +
  35450. +/***********************************************************
  35451. + * Name: vchi_msg_dequeue
  35452. + *
  35453. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  35454. + * void *data,
  35455. + * uint32_t max_data_size_to_read,
  35456. + * uint32_t *actual_msg_size
  35457. + * VCHI_FLAGS_T flags
  35458. + *
  35459. + * Description: Routine to dequeue a message into the supplied buffer
  35460. + *
  35461. + * Returns: int32_t - success == 0
  35462. + *
  35463. + ***********************************************************/
  35464. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  35465. + void *data,
  35466. + uint32_t max_data_size_to_read,
  35467. + uint32_t *actual_msg_size,
  35468. + VCHI_FLAGS_T flags)
  35469. +{
  35470. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35471. + VCHIQ_HEADER_T *header;
  35472. +
  35473. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  35474. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  35475. +
  35476. + if (flags == VCHI_FLAGS_NONE)
  35477. + if (vchiu_queue_is_empty(&service->queue))
  35478. + return -1;
  35479. +
  35480. + header = vchiu_queue_pop(&service->queue);
  35481. +
  35482. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  35483. + header->size : max_data_size_to_read);
  35484. +
  35485. + *actual_msg_size = header->size;
  35486. +
  35487. + vchiq_release_message(service->handle, header);
  35488. +
  35489. + return 0;
  35490. +}
  35491. +EXPORT_SYMBOL(vchi_msg_dequeue);
  35492. +
  35493. +/***********************************************************
  35494. + * Name: vchi_msg_queuev
  35495. + *
  35496. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  35497. + * VCHI_MSG_VECTOR_T *vector,
  35498. + * uint32_t count,
  35499. + * VCHI_FLAGS_T flags,
  35500. + * void *msg_handle
  35501. + *
  35502. + * Description: Thin wrapper to queue a message onto a connection
  35503. + *
  35504. + * Returns: int32_t - success == 0
  35505. + *
  35506. + ***********************************************************/
  35507. +
  35508. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  35509. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  35510. + offsetof(VCHIQ_ELEMENT_T, data));
  35511. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  35512. + offsetof(VCHIQ_ELEMENT_T, size));
  35513. +
  35514. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  35515. + VCHI_MSG_VECTOR_T *vector,
  35516. + uint32_t count,
  35517. + VCHI_FLAGS_T flags,
  35518. + void *msg_handle)
  35519. +{
  35520. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35521. +
  35522. + (void)msg_handle;
  35523. +
  35524. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  35525. +
  35526. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  35527. + (const VCHIQ_ELEMENT_T *)vector, count));
  35528. +}
  35529. +EXPORT_SYMBOL(vchi_msg_queuev);
  35530. +
  35531. +/***********************************************************
  35532. + * Name: vchi_held_msg_release
  35533. + *
  35534. + * Arguments: VCHI_HELD_MSG_T *message
  35535. + *
  35536. + * Description: Routine to release a held message (after it has been read with
  35537. + * vchi_msg_hold)
  35538. + *
  35539. + * Returns: int32_t - success == 0
  35540. + *
  35541. + ***********************************************************/
  35542. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  35543. +{
  35544. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  35545. + (VCHIQ_HEADER_T *)message->message);
  35546. +
  35547. + return 0;
  35548. +}
  35549. +EXPORT_SYMBOL(vchi_held_msg_release);
  35550. +
  35551. +/***********************************************************
  35552. + * Name: vchi_msg_hold
  35553. + *
  35554. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  35555. + * void **data,
  35556. + * uint32_t *msg_size,
  35557. + * VCHI_FLAGS_T flags,
  35558. + * VCHI_HELD_MSG_T *message_handle
  35559. + *
  35560. + * Description: Routine to return a pointer to the current message (to allow
  35561. + * in place processing). The message is dequeued - don't forget
  35562. + * to release the message using vchi_held_msg_release when you're
  35563. + * finished.
  35564. + *
  35565. + * Returns: int32_t - success == 0
  35566. + *
  35567. + ***********************************************************/
  35568. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  35569. + void **data,
  35570. + uint32_t *msg_size,
  35571. + VCHI_FLAGS_T flags,
  35572. + VCHI_HELD_MSG_T *message_handle)
  35573. +{
  35574. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35575. + VCHIQ_HEADER_T *header;
  35576. +
  35577. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  35578. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  35579. +
  35580. + if (flags == VCHI_FLAGS_NONE)
  35581. + if (vchiu_queue_is_empty(&service->queue))
  35582. + return -1;
  35583. +
  35584. + header = vchiu_queue_pop(&service->queue);
  35585. +
  35586. + *data = header->data;
  35587. + *msg_size = header->size;
  35588. +
  35589. + message_handle->service =
  35590. + (struct opaque_vchi_service_t *)service->handle;
  35591. + message_handle->message = header;
  35592. +
  35593. + return 0;
  35594. +}
  35595. +EXPORT_SYMBOL(vchi_msg_hold);
  35596. +
  35597. +/***********************************************************
  35598. + * Name: vchi_initialise
  35599. + *
  35600. + * Arguments: VCHI_INSTANCE_T *instance_handle
  35601. + *
  35602. + * Description: Initialises the hardware but does not transmit anything
  35603. + * When run as a Host App this will be called twice hence the need
  35604. + * to malloc the state information
  35605. + *
  35606. + * Returns: 0 if successful, failure otherwise
  35607. + *
  35608. + ***********************************************************/
  35609. +
  35610. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  35611. +{
  35612. + VCHIQ_INSTANCE_T instance;
  35613. + VCHIQ_STATUS_T status;
  35614. +
  35615. + status = vchiq_initialise(&instance);
  35616. +
  35617. + *instance_handle = (VCHI_INSTANCE_T)instance;
  35618. +
  35619. + return vchiq_status_to_vchi(status);
  35620. +}
  35621. +EXPORT_SYMBOL(vchi_initialise);
  35622. +
  35623. +/***********************************************************
  35624. + * Name: vchi_connect
  35625. + *
  35626. + * Arguments: VCHI_CONNECTION_T **connections
  35627. + * const uint32_t num_connections
  35628. + * VCHI_INSTANCE_T instance_handle)
  35629. + *
  35630. + * Description: Starts the command service on each connection,
  35631. + * causing INIT messages to be pinged back and forth
  35632. + *
  35633. + * Returns: 0 if successful, failure otherwise
  35634. + *
  35635. + ***********************************************************/
  35636. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  35637. + const uint32_t num_connections,
  35638. + VCHI_INSTANCE_T instance_handle)
  35639. +{
  35640. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  35641. +
  35642. + (void)connections;
  35643. + (void)num_connections;
  35644. +
  35645. + return vchiq_connect(instance);
  35646. +}
  35647. +EXPORT_SYMBOL(vchi_connect);
  35648. +
  35649. +
  35650. +/***********************************************************
  35651. + * Name: vchi_disconnect
  35652. + *
  35653. + * Arguments: VCHI_INSTANCE_T instance_handle
  35654. + *
  35655. + * Description: Stops the command service on each connection,
  35656. + * causing DE-INIT messages to be pinged back and forth
  35657. + *
  35658. + * Returns: 0 if successful, failure otherwise
  35659. + *
  35660. + ***********************************************************/
  35661. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  35662. +{
  35663. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  35664. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  35665. +}
  35666. +EXPORT_SYMBOL(vchi_disconnect);
  35667. +
  35668. +
  35669. +/***********************************************************
  35670. + * Name: vchi_service_open
  35671. + * Name: vchi_service_create
  35672. + *
  35673. + * Arguments: VCHI_INSTANCE_T *instance_handle
  35674. + * SERVICE_CREATION_T *setup,
  35675. + * VCHI_SERVICE_HANDLE_T *handle
  35676. + *
  35677. + * Description: Routine to open a service
  35678. + *
  35679. + * Returns: int32_t - success == 0
  35680. + *
  35681. + ***********************************************************/
  35682. +
  35683. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  35684. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  35685. +{
  35686. + SHIM_SERVICE_T *service =
  35687. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  35688. +
  35689. + if (!service->callback)
  35690. + goto release;
  35691. +
  35692. + switch (reason) {
  35693. + case VCHIQ_MESSAGE_AVAILABLE:
  35694. + vchiu_queue_push(&service->queue, header);
  35695. +
  35696. + service->callback(service->callback_param,
  35697. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  35698. +
  35699. + goto done;
  35700. + break;
  35701. +
  35702. + case VCHIQ_BULK_TRANSMIT_DONE:
  35703. + service->callback(service->callback_param,
  35704. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  35705. + break;
  35706. +
  35707. + case VCHIQ_BULK_RECEIVE_DONE:
  35708. + service->callback(service->callback_param,
  35709. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  35710. + break;
  35711. +
  35712. + case VCHIQ_SERVICE_CLOSED:
  35713. + service->callback(service->callback_param,
  35714. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  35715. + break;
  35716. +
  35717. + case VCHIQ_SERVICE_OPENED:
  35718. + /* No equivalent VCHI reason */
  35719. + break;
  35720. +
  35721. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  35722. + service->callback(service->callback_param,
  35723. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  35724. + bulk_user);
  35725. + break;
  35726. +
  35727. + case VCHIQ_BULK_RECEIVE_ABORTED:
  35728. + service->callback(service->callback_param,
  35729. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  35730. + bulk_user);
  35731. + break;
  35732. +
  35733. + default:
  35734. + WARN(1, "not supported\n");
  35735. + break;
  35736. + }
  35737. +
  35738. +release:
  35739. + vchiq_release_message(service->handle, header);
  35740. +done:
  35741. + return VCHIQ_SUCCESS;
  35742. +}
  35743. +
  35744. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  35745. + SERVICE_CREATION_T *setup)
  35746. +{
  35747. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  35748. +
  35749. + (void)instance;
  35750. +
  35751. + if (service) {
  35752. + if (vchiu_queue_init(&service->queue, 64)) {
  35753. + service->callback = setup->callback;
  35754. + service->callback_param = setup->callback_param;
  35755. + } else {
  35756. + kfree(service);
  35757. + service = NULL;
  35758. + }
  35759. + }
  35760. +
  35761. + return service;
  35762. +}
  35763. +
  35764. +static void service_free(SHIM_SERVICE_T *service)
  35765. +{
  35766. + if (service) {
  35767. + vchiu_queue_delete(&service->queue);
  35768. + kfree(service);
  35769. + }
  35770. +}
  35771. +
  35772. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  35773. + SERVICE_CREATION_T *setup,
  35774. + VCHI_SERVICE_HANDLE_T *handle)
  35775. +{
  35776. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  35777. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  35778. +
  35779. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  35780. +
  35781. + if (service) {
  35782. + VCHIQ_SERVICE_PARAMS_T params;
  35783. + VCHIQ_STATUS_T status;
  35784. +
  35785. + memset(&params, 0, sizeof(params));
  35786. + params.fourcc = setup->service_id;
  35787. + params.callback = shim_callback;
  35788. + params.userdata = service;
  35789. + params.version = setup->version.version;
  35790. + params.version_min = setup->version.version_min;
  35791. +
  35792. + status = vchiq_open_service(instance, &params,
  35793. + &service->handle);
  35794. + if (status != VCHIQ_SUCCESS) {
  35795. + service_free(service);
  35796. + service = NULL;
  35797. + *handle = NULL;
  35798. + }
  35799. + }
  35800. +
  35801. + return (service != NULL) ? 0 : -1;
  35802. +}
  35803. +EXPORT_SYMBOL(vchi_service_open);
  35804. +
  35805. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  35806. + SERVICE_CREATION_T *setup,
  35807. + VCHI_SERVICE_HANDLE_T *handle)
  35808. +{
  35809. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  35810. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  35811. +
  35812. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  35813. +
  35814. + if (service) {
  35815. + VCHIQ_SERVICE_PARAMS_T params;
  35816. + VCHIQ_STATUS_T status;
  35817. +
  35818. + memset(&params, 0, sizeof(params));
  35819. + params.fourcc = setup->service_id;
  35820. + params.callback = shim_callback;
  35821. + params.userdata = service;
  35822. + params.version = setup->version.version;
  35823. + params.version_min = setup->version.version_min;
  35824. + status = vchiq_add_service(instance, &params, &service->handle);
  35825. +
  35826. + if (status != VCHIQ_SUCCESS) {
  35827. + service_free(service);
  35828. + service = NULL;
  35829. + *handle = NULL;
  35830. + }
  35831. + }
  35832. +
  35833. + return (service != NULL) ? 0 : -1;
  35834. +}
  35835. +EXPORT_SYMBOL(vchi_service_create);
  35836. +
  35837. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  35838. +{
  35839. + int32_t ret = -1;
  35840. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35841. + if (service) {
  35842. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  35843. + if (status == VCHIQ_SUCCESS) {
  35844. + service_free(service);
  35845. + service = NULL;
  35846. + }
  35847. +
  35848. + ret = vchiq_status_to_vchi(status);
  35849. + }
  35850. + return ret;
  35851. +}
  35852. +EXPORT_SYMBOL(vchi_service_close);
  35853. +
  35854. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  35855. +{
  35856. + int32_t ret = -1;
  35857. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35858. + if (service) {
  35859. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  35860. + if (status == VCHIQ_SUCCESS) {
  35861. + service_free(service);
  35862. + service = NULL;
  35863. + }
  35864. +
  35865. + ret = vchiq_status_to_vchi(status);
  35866. + }
  35867. + return ret;
  35868. +}
  35869. +EXPORT_SYMBOL(vchi_service_destroy);
  35870. +
  35871. +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
  35872. + VCHI_SERVICE_OPTION_T option,
  35873. + int value)
  35874. +{
  35875. + int32_t ret = -1;
  35876. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35877. + VCHIQ_SERVICE_OPTION_T vchiq_option;
  35878. + switch (option) {
  35879. + case VCHI_SERVICE_OPTION_TRACE:
  35880. + vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
  35881. + break;
  35882. + default:
  35883. + service = NULL;
  35884. + break;
  35885. + }
  35886. + if (service) {
  35887. + VCHIQ_STATUS_T status =
  35888. + vchiq_set_service_option(service->handle,
  35889. + vchiq_option,
  35890. + value);
  35891. +
  35892. + ret = vchiq_status_to_vchi(status);
  35893. + }
  35894. + return ret;
  35895. +}
  35896. +EXPORT_SYMBOL(vchi_service_set_option);
  35897. +
  35898. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  35899. +{
  35900. + int32_t ret = -1;
  35901. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35902. + if(service)
  35903. + {
  35904. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  35905. + ret = vchiq_status_to_vchi( status );
  35906. + }
  35907. + return ret;
  35908. +}
  35909. +EXPORT_SYMBOL(vchi_get_peer_version);
  35910. +
  35911. +/* ----------------------------------------------------------------------
  35912. + * read a uint32_t from buffer.
  35913. + * network format is defined to be little endian
  35914. + * -------------------------------------------------------------------- */
  35915. +uint32_t
  35916. +vchi_readbuf_uint32(const void *_ptr)
  35917. +{
  35918. + const unsigned char *ptr = _ptr;
  35919. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  35920. +}
  35921. +
  35922. +/* ----------------------------------------------------------------------
  35923. + * write a uint32_t to buffer.
  35924. + * network format is defined to be little endian
  35925. + * -------------------------------------------------------------------- */
  35926. +void
  35927. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  35928. +{
  35929. + unsigned char *ptr = _ptr;
  35930. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  35931. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  35932. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  35933. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  35934. +}
  35935. +
  35936. +/* ----------------------------------------------------------------------
  35937. + * read a uint16_t from buffer.
  35938. + * network format is defined to be little endian
  35939. + * -------------------------------------------------------------------- */
  35940. +uint16_t
  35941. +vchi_readbuf_uint16(const void *_ptr)
  35942. +{
  35943. + const unsigned char *ptr = _ptr;
  35944. + return ptr[0] | (ptr[1] << 8);
  35945. +}
  35946. +
  35947. +/* ----------------------------------------------------------------------
  35948. + * write a uint16_t into the buffer.
  35949. + * network format is defined to be little endian
  35950. + * -------------------------------------------------------------------- */
  35951. +void
  35952. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  35953. +{
  35954. + unsigned char *ptr = _ptr;
  35955. + ptr[0] = (value >> 0) & 0xFF;
  35956. + ptr[1] = (value >> 8) & 0xFF;
  35957. +}
  35958. +
  35959. +/***********************************************************
  35960. + * Name: vchi_service_use
  35961. + *
  35962. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  35963. + *
  35964. + * Description: Routine to increment refcount on a service
  35965. + *
  35966. + * Returns: void
  35967. + *
  35968. + ***********************************************************/
  35969. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  35970. +{
  35971. + int32_t ret = -1;
  35972. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35973. + if (service)
  35974. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  35975. + return ret;
  35976. +}
  35977. +EXPORT_SYMBOL(vchi_service_use);
  35978. +
  35979. +/***********************************************************
  35980. + * Name: vchi_service_release
  35981. + *
  35982. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  35983. + *
  35984. + * Description: Routine to decrement refcount on a service
  35985. + *
  35986. + * Returns: void
  35987. + *
  35988. + ***********************************************************/
  35989. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  35990. +{
  35991. + int32_t ret = -1;
  35992. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35993. + if (service)
  35994. + ret = vchiq_status_to_vchi(
  35995. + vchiq_release_service(service->handle));
  35996. + return ret;
  35997. +}
  35998. +EXPORT_SYMBOL(vchi_service_release);
  35999. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  36000. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 1969-12-31 18:00:00.000000000 -0600
  36001. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 2014-12-11 14:02:53.548418001 -0600
  36002. @@ -0,0 +1,152 @@
  36003. +/**
  36004. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  36005. + *
  36006. + * Redistribution and use in source and binary forms, with or without
  36007. + * modification, are permitted provided that the following conditions
  36008. + * are met:
  36009. + * 1. Redistributions of source code must retain the above copyright
  36010. + * notice, this list of conditions, and the following disclaimer,
  36011. + * without modification.
  36012. + * 2. Redistributions in binary form must reproduce the above copyright
  36013. + * notice, this list of conditions and the following disclaimer in the
  36014. + * documentation and/or other materials provided with the distribution.
  36015. + * 3. The names of the above-listed copyright holders may not be used
  36016. + * to endorse or promote products derived from this software without
  36017. + * specific prior written permission.
  36018. + *
  36019. + * ALTERNATIVELY, this software may be distributed under the terms of the
  36020. + * GNU General Public License ("GPL") version 2, as published by the Free
  36021. + * Software Foundation.
  36022. + *
  36023. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  36024. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  36025. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36026. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36027. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  36028. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  36029. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36030. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36031. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36032. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36033. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36034. + */
  36035. +
  36036. +#include "vchiq_util.h"
  36037. +#include "vchiq_killable.h"
  36038. +
  36039. +static inline int is_pow2(int i)
  36040. +{
  36041. + return i && !(i & (i - 1));
  36042. +}
  36043. +
  36044. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  36045. +{
  36046. + WARN_ON(!is_pow2(size));
  36047. +
  36048. + queue->size = size;
  36049. + queue->read = 0;
  36050. + queue->write = 0;
  36051. +
  36052. + sema_init(&queue->pop, 0);
  36053. + sema_init(&queue->push, 0);
  36054. +
  36055. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  36056. + if (queue->storage == NULL) {
  36057. + vchiu_queue_delete(queue);
  36058. + return 0;
  36059. + }
  36060. + return 1;
  36061. +}
  36062. +
  36063. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  36064. +{
  36065. + if (queue->storage != NULL)
  36066. + kfree(queue->storage);
  36067. +}
  36068. +
  36069. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  36070. +{
  36071. + return queue->read == queue->write;
  36072. +}
  36073. +
  36074. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  36075. +{
  36076. + return queue->write == queue->read + queue->size;
  36077. +}
  36078. +
  36079. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  36080. +{
  36081. + while (queue->write == queue->read + queue->size) {
  36082. + if (down_interruptible(&queue->pop) != 0) {
  36083. + flush_signals(current);
  36084. + }
  36085. + }
  36086. +
  36087. + /*
  36088. + * Write to queue->storage must be visible after read from
  36089. + * queue->read
  36090. + */
  36091. + smp_mb();
  36092. +
  36093. + queue->storage[queue->write & (queue->size - 1)] = header;
  36094. +
  36095. + /*
  36096. + * Write to queue->storage must be visible before write to
  36097. + * queue->write
  36098. + */
  36099. + smp_wmb();
  36100. +
  36101. + queue->write++;
  36102. +
  36103. + up(&queue->push);
  36104. +}
  36105. +
  36106. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  36107. +{
  36108. + while (queue->write == queue->read) {
  36109. + if (down_interruptible(&queue->push) != 0) {
  36110. + flush_signals(current);
  36111. + }
  36112. + }
  36113. +
  36114. + up(&queue->push); // We haven't removed anything from the queue.
  36115. +
  36116. + /*
  36117. + * Read from queue->storage must be visible after read from
  36118. + * queue->write
  36119. + */
  36120. + smp_rmb();
  36121. +
  36122. + return queue->storage[queue->read & (queue->size - 1)];
  36123. +}
  36124. +
  36125. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  36126. +{
  36127. + VCHIQ_HEADER_T *header;
  36128. +
  36129. + while (queue->write == queue->read) {
  36130. + if (down_interruptible(&queue->push) != 0) {
  36131. + flush_signals(current);
  36132. + }
  36133. + }
  36134. +
  36135. + /*
  36136. + * Read from queue->storage must be visible after read from
  36137. + * queue->write
  36138. + */
  36139. + smp_rmb();
  36140. +
  36141. + header = queue->storage[queue->read & (queue->size - 1)];
  36142. +
  36143. + /*
  36144. + * Read from queue->storage must be visible before write to
  36145. + * queue->read
  36146. + */
  36147. + smp_mb();
  36148. +
  36149. + queue->read++;
  36150. +
  36151. + up(&queue->pop);
  36152. +
  36153. + return header;
  36154. +}
  36155. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  36156. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 1969-12-31 18:00:00.000000000 -0600
  36157. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 2014-12-11 14:02:53.548418001 -0600
  36158. @@ -0,0 +1,81 @@
  36159. +/**
  36160. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  36161. + *
  36162. + * Redistribution and use in source and binary forms, with or without
  36163. + * modification, are permitted provided that the following conditions
  36164. + * are met:
  36165. + * 1. Redistributions of source code must retain the above copyright
  36166. + * notice, this list of conditions, and the following disclaimer,
  36167. + * without modification.
  36168. + * 2. Redistributions in binary form must reproduce the above copyright
  36169. + * notice, this list of conditions and the following disclaimer in the
  36170. + * documentation and/or other materials provided with the distribution.
  36171. + * 3. The names of the above-listed copyright holders may not be used
  36172. + * to endorse or promote products derived from this software without
  36173. + * specific prior written permission.
  36174. + *
  36175. + * ALTERNATIVELY, this software may be distributed under the terms of the
  36176. + * GNU General Public License ("GPL") version 2, as published by the Free
  36177. + * Software Foundation.
  36178. + *
  36179. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  36180. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  36181. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36182. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36183. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  36184. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  36185. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36186. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36187. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36188. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36189. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36190. + */
  36191. +
  36192. +#ifndef VCHIQ_UTIL_H
  36193. +#define VCHIQ_UTIL_H
  36194. +
  36195. +#include <linux/types.h>
  36196. +#include <linux/semaphore.h>
  36197. +#include <linux/mutex.h>
  36198. +#include <linux/bitops.h>
  36199. +#include <linux/kthread.h>
  36200. +#include <linux/wait.h>
  36201. +#include <linux/vmalloc.h>
  36202. +#include <linux/jiffies.h>
  36203. +#include <linux/delay.h>
  36204. +#include <linux/string.h>
  36205. +#include <linux/types.h>
  36206. +#include <linux/interrupt.h>
  36207. +#include <linux/random.h>
  36208. +#include <linux/sched.h>
  36209. +#include <linux/ctype.h>
  36210. +#include <linux/uaccess.h>
  36211. +#include <linux/time.h> /* for time_t */
  36212. +#include <linux/slab.h>
  36213. +#include <linux/vmalloc.h>
  36214. +
  36215. +#include "vchiq_if.h"
  36216. +
  36217. +typedef struct {
  36218. + int size;
  36219. + int read;
  36220. + int write;
  36221. +
  36222. + struct semaphore pop;
  36223. + struct semaphore push;
  36224. +
  36225. + VCHIQ_HEADER_T **storage;
  36226. +} VCHIU_QUEUE_T;
  36227. +
  36228. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  36229. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  36230. +
  36231. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  36232. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  36233. +
  36234. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  36235. +
  36236. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  36237. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  36238. +
  36239. +#endif
  36240. diff -Nur linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  36241. --- linux-3.17.5/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 1969-12-31 18:00:00.000000000 -0600
  36242. +++ linux-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 2014-12-11 14:02:53.548418001 -0600
  36243. @@ -0,0 +1,59 @@
  36244. +/**
  36245. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  36246. + *
  36247. + * Redistribution and use in source and binary forms, with or without
  36248. + * modification, are permitted provided that the following conditions
  36249. + * are met:
  36250. + * 1. Redistributions of source code must retain the above copyright
  36251. + * notice, this list of conditions, and the following disclaimer,
  36252. + * without modification.
  36253. + * 2. Redistributions in binary form must reproduce the above copyright
  36254. + * notice, this list of conditions and the following disclaimer in the
  36255. + * documentation and/or other materials provided with the distribution.
  36256. + * 3. The names of the above-listed copyright holders may not be used
  36257. + * to endorse or promote products derived from this software without
  36258. + * specific prior written permission.
  36259. + *
  36260. + * ALTERNATIVELY, this software may be distributed under the terms of the
  36261. + * GNU General Public License ("GPL") version 2, as published by the Free
  36262. + * Software Foundation.
  36263. + *
  36264. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  36265. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  36266. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36267. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36268. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  36269. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  36270. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36271. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36272. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36273. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36274. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36275. + */
  36276. +#include "vchiq_build_info.h"
  36277. +#include <linux/broadcom/vc_debug_sym.h>
  36278. +
  36279. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  36280. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  36281. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  36282. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  36283. +
  36284. +const char *vchiq_get_build_hostname( void )
  36285. +{
  36286. + return vchiq_build_hostname;
  36287. +}
  36288. +
  36289. +const char *vchiq_get_build_version( void )
  36290. +{
  36291. + return vchiq_build_version;
  36292. +}
  36293. +
  36294. +const char *vchiq_get_build_date( void )
  36295. +{
  36296. + return vchiq_build_date;
  36297. +}
  36298. +
  36299. +const char *vchiq_get_build_time( void )
  36300. +{
  36301. + return vchiq_build_time;
  36302. +}
  36303. diff -Nur linux-3.17.5/drivers/misc/vc04_services/Kconfig linux-rpi/drivers/misc/vc04_services/Kconfig
  36304. --- linux-3.17.5/drivers/misc/vc04_services/Kconfig 1969-12-31 18:00:00.000000000 -0600
  36305. +++ linux-rpi/drivers/misc/vc04_services/Kconfig 2014-12-11 14:02:53.544418001 -0600
  36306. @@ -0,0 +1,9 @@
  36307. +config BCM2708_VCHIQ
  36308. + tristate "Videocore VCHIQ"
  36309. + depends on MACH_BCM2708
  36310. + default y
  36311. + help
  36312. + Kernel to VideoCore communication interface for the
  36313. + BCM2708 family of products.
  36314. + Defaults to Y when the Broadcom Videocore services
  36315. + are included in the build, N otherwise.
  36316. diff -Nur linux-3.17.5/drivers/misc/vc04_services/Makefile linux-rpi/drivers/misc/vc04_services/Makefile
  36317. --- linux-3.17.5/drivers/misc/vc04_services/Makefile 1969-12-31 18:00:00.000000000 -0600
  36318. +++ linux-rpi/drivers/misc/vc04_services/Makefile 2014-12-11 14:02:53.544418001 -0600
  36319. @@ -0,0 +1,17 @@
  36320. +ifeq ($(CONFIG_MACH_BCM2708),y)
  36321. +
  36322. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  36323. +
  36324. +vchiq-objs := \
  36325. + interface/vchiq_arm/vchiq_core.o \
  36326. + interface/vchiq_arm/vchiq_arm.o \
  36327. + interface/vchiq_arm/vchiq_kern_lib.o \
  36328. + interface/vchiq_arm/vchiq_2835_arm.o \
  36329. + interface/vchiq_arm/vchiq_debugfs.o \
  36330. + interface/vchiq_arm/vchiq_shim.o \
  36331. + interface/vchiq_arm/vchiq_util.o \
  36332. + interface/vchiq_arm/vchiq_connected.o \
  36333. +
  36334. +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  36335. +
  36336. +endif
  36337. diff -Nur linux-3.17.5/drivers/mmc/host/bcm2835-mmc.c linux-rpi/drivers/mmc/host/bcm2835-mmc.c
  36338. --- linux-3.17.5/drivers/mmc/host/bcm2835-mmc.c 1969-12-31 18:00:00.000000000 -0600
  36339. +++ linux-rpi/drivers/mmc/host/bcm2835-mmc.c 2014-12-11 14:05:38.172418001 -0600
  36340. @@ -0,0 +1,1547 @@
  36341. +/*
  36342. + * BCM2835 MMC host driver.
  36343. + *
  36344. + * Author: Gellert Weisz <gellert@raspberrypi.org>
  36345. + * Copyright 2014
  36346. + *
  36347. + * Based on
  36348. + * sdhci-bcm2708.c by Broadcom
  36349. + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  36350. + * sdhci.c and sdhci-pci.c by Pierre Ossman
  36351. + *
  36352. + * This program is free software; you can redistribute it and/or modify it
  36353. + * under the terms and conditions of the GNU General Public License,
  36354. + * version 2, as published by the Free Software Foundation.
  36355. + *
  36356. + * This program is distributed in the hope it will be useful, but WITHOUT
  36357. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  36358. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  36359. + * more details.
  36360. + *
  36361. + * You should have received a copy of the GNU General Public License
  36362. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  36363. + */
  36364. +
  36365. +#include <linux/delay.h>
  36366. +#include <linux/module.h>
  36367. +#include <linux/io.h>
  36368. +#include <linux/mmc/mmc.h>
  36369. +#include <linux/mmc/host.h>
  36370. +#include <linux/mmc/sd.h>
  36371. +#include <linux/scatterlist.h>
  36372. +#include <linux/of_address.h>
  36373. +#include <linux/of_irq.h>
  36374. +#include <linux/clk.h>
  36375. +#include <linux/platform_device.h>
  36376. +#include <linux/err.h>
  36377. +#include <linux/blkdev.h>
  36378. +#include <linux/dmaengine.h>
  36379. +#include <linux/dma-mapping.h>
  36380. +#include <linux/of_dma.h>
  36381. +
  36382. +#include "sdhci.h"
  36383. +
  36384. +
  36385. +#ifndef CONFIG_ARCH_BCM2835
  36386. + #define BCM2835_CLOCK_FREQ 250000000
  36387. +#endif
  36388. +
  36389. +#define DRIVER_NAME "mmc-bcm2835"
  36390. +
  36391. +#define DBG(f, x...) \
  36392. +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
  36393. +
  36394. +#ifndef CONFIG_MMC_BCM2835_DMA
  36395. + #define FORCE_PIO
  36396. +#endif
  36397. +
  36398. +
  36399. +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
  36400. +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  36401. +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  36402. +#else
  36403. +#define PIO_DMA_BARRIER 00
  36404. +#endif
  36405. +
  36406. +#define MIN_FREQ 400000
  36407. +#define TIMEOUT_VAL 0xE
  36408. +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
  36409. +
  36410. +#ifndef BCM2708_PERI_BASE
  36411. + #define BCM2708_PERI_BASE 0x20000000
  36412. +#endif
  36413. +
  36414. +/* FIXME: Needs IOMMU support */
  36415. +#define BCM2835_VCMMU_SHIFT (0x7E000000 - BCM2708_PERI_BASE)
  36416. +
  36417. +
  36418. +struct bcm2835_host {
  36419. + spinlock_t lock;
  36420. +
  36421. + void __iomem *ioaddr;
  36422. + u32 phys_addr;
  36423. +
  36424. + struct mmc_host *mmc;
  36425. +
  36426. + u32 timeout;
  36427. +
  36428. + int clock; /* Current clock speed */
  36429. + u8 pwr; /* Current voltage */
  36430. +
  36431. + unsigned int max_clk; /* Max possible freq */
  36432. + unsigned int timeout_clk; /* Timeout freq (KHz) */
  36433. + unsigned int clk_mul; /* Clock Muliplier value */
  36434. +
  36435. + struct tasklet_struct finish_tasklet; /* Tasklet structures */
  36436. +
  36437. + struct timer_list timer; /* Timer for timeouts */
  36438. +
  36439. + struct sg_mapping_iter sg_miter; /* SG state for PIO */
  36440. + unsigned int blocks; /* remaining PIO blocks */
  36441. +
  36442. + int irq; /* Device IRQ */
  36443. +
  36444. +
  36445. + u32 ier; /* cached registers */
  36446. +
  36447. + struct mmc_request *mrq; /* Current request */
  36448. + struct mmc_command *cmd; /* Current command */
  36449. + struct mmc_data *data; /* Current data request */
  36450. + unsigned int data_early:1; /* Data finished before cmd */
  36451. +
  36452. + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
  36453. +
  36454. + u32 thread_isr;
  36455. +
  36456. + u32 shadow;
  36457. +
  36458. + /*DMA part*/
  36459. + struct dma_chan *dma_chan_rx; /* DMA channel for reads */
  36460. + struct dma_chan *dma_chan_tx; /* DMA channel for writes */
  36461. + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
  36462. +
  36463. + bool have_dma;
  36464. + bool use_dma;
  36465. + /*end of DMA part*/
  36466. +
  36467. + int max_delay; /* maximum length of time spent waiting */
  36468. +
  36469. + int flags; /* Host attributes */
  36470. +#define SDHCI_USE_SDMA (1<<0) /* Host is SDMA capable */
  36471. +#define SDHCI_USE_ADMA (1<<1) /* Host is ADMA capable */
  36472. +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
  36473. +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
  36474. +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
  36475. +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
  36476. +#define SDHCI_PV_ENABLED (1<<8) /* Preset value enabled */
  36477. +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  36478. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  36479. +};
  36480. +
  36481. +
  36482. +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg)
  36483. +{
  36484. + writel(val, host->ioaddr + reg);
  36485. + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
  36486. +}
  36487. +
  36488. +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
  36489. +{
  36490. + writel(val, host->ioaddr + reg);
  36491. +}
  36492. +
  36493. +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
  36494. +{
  36495. + return readl(host->ioaddr + reg);
  36496. +}
  36497. +
  36498. +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
  36499. +{
  36500. + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
  36501. + bcm2835_mmc_readl(host, reg & ~3);
  36502. + u32 word_num = (reg >> 1) & 1;
  36503. + u32 word_shift = word_num * 16;
  36504. + u32 mask = 0xffff << word_shift;
  36505. + u32 newval = (oldval & ~mask) | (val << word_shift);
  36506. +
  36507. + if (reg == SDHCI_TRANSFER_MODE)
  36508. + host->shadow = newval;
  36509. + else
  36510. + bcm2835_mmc_writel(host, newval, reg & ~3);
  36511. +
  36512. +}
  36513. +
  36514. +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
  36515. +{
  36516. + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
  36517. + u32 byte_num = reg & 3;
  36518. + u32 byte_shift = byte_num * 8;
  36519. + u32 mask = 0xff << byte_shift;
  36520. + u32 newval = (oldval & ~mask) | (val << byte_shift);
  36521. +
  36522. + bcm2835_mmc_writel(host, newval, reg & ~3);
  36523. +}
  36524. +
  36525. +
  36526. +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
  36527. +{
  36528. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  36529. + u32 word_num = (reg >> 1) & 1;
  36530. + u32 word_shift = word_num * 16;
  36531. + u32 word = (val >> word_shift) & 0xffff;
  36532. +
  36533. + return word;
  36534. +}
  36535. +
  36536. +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
  36537. +{
  36538. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  36539. + u32 byte_num = reg & 3;
  36540. + u32 byte_shift = byte_num * 8;
  36541. + u32 byte = (val >> byte_shift) & 0xff;
  36542. +
  36543. + return byte;
  36544. +}
  36545. +
  36546. +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
  36547. +{
  36548. + u32 ier;
  36549. +
  36550. + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
  36551. + ier &= ~clear;
  36552. + /* change which requests generate IRQs - makes no difference to
  36553. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  36554. + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  36555. +}
  36556. +
  36557. +
  36558. +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
  36559. +{
  36560. + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  36561. + mmc_hostname(host->mmc));
  36562. +
  36563. + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  36564. + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
  36565. + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
  36566. + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  36567. + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
  36568. + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
  36569. + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  36570. + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
  36571. + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
  36572. + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  36573. + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
  36574. + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
  36575. + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  36576. + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
  36577. + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  36578. + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  36579. + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
  36580. + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
  36581. + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  36582. + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
  36583. + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
  36584. + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  36585. + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
  36586. + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
  36587. + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  36588. + bcm2835_mmc_readw(host, SDHCI_ACMD12_ERR),
  36589. + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
  36590. + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  36591. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
  36592. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
  36593. + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  36594. + bcm2835_mmc_readw(host, SDHCI_COMMAND),
  36595. + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
  36596. + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  36597. + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
  36598. +
  36599. + pr_debug(DRIVER_NAME ": ===========================================\n");
  36600. +}
  36601. +
  36602. +
  36603. +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
  36604. +{
  36605. + unsigned long timeout;
  36606. +
  36607. + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  36608. +
  36609. + if (mask & SDHCI_RESET_ALL)
  36610. + host->clock = 0;
  36611. +
  36612. + /* Wait max 100 ms */
  36613. + timeout = 100;
  36614. +
  36615. + /* hw clears the bit when it's done */
  36616. + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  36617. + if (timeout == 0) {
  36618. + pr_err("%s: Reset 0x%x never completed.\n",
  36619. + mmc_hostname(host->mmc), (int)mask);
  36620. + bcm2835_mmc_dumpregs(host);
  36621. + return;
  36622. + }
  36623. + timeout--;
  36624. + mdelay(1);
  36625. + }
  36626. +
  36627. + if (100-timeout > 10 && 100-timeout > host->max_delay) {
  36628. + host->max_delay = 100-timeout;
  36629. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  36630. + }
  36631. +}
  36632. +
  36633. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  36634. +
  36635. +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
  36636. +{
  36637. + if (soft)
  36638. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  36639. + else
  36640. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  36641. +
  36642. + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  36643. + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  36644. + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  36645. + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  36646. + SDHCI_INT_RESPONSE;
  36647. +
  36648. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  36649. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  36650. +
  36651. + if (soft) {
  36652. + /* force clock reconfiguration */
  36653. + host->clock = 0;
  36654. + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
  36655. + }
  36656. +}
  36657. +
  36658. +
  36659. +
  36660. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
  36661. +
  36662. +static void bcm2835_mmc_dma_complete(void *param)
  36663. +{
  36664. + struct bcm2835_host *host = param;
  36665. + struct dma_chan *dma_chan;
  36666. + unsigned long flags;
  36667. + u32 dir_data;
  36668. +
  36669. + spin_lock_irqsave(&host->lock, flags);
  36670. +
  36671. + if (host->data && !(host->data->flags & MMC_DATA_WRITE)) {
  36672. + /* otherwise handled in SDHCI IRQ */
  36673. + dma_chan = host->dma_chan_rx;
  36674. + dir_data = DMA_FROM_DEVICE;
  36675. +
  36676. + dma_unmap_sg(dma_chan->device->dev,
  36677. + host->data->sg, host->data->sg_len,
  36678. + dir_data);
  36679. +
  36680. + bcm2835_mmc_finish_data(host);
  36681. + }
  36682. +
  36683. + spin_unlock_irqrestore(&host->lock, flags);
  36684. +}
  36685. +
  36686. +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
  36687. +{
  36688. + unsigned long flags;
  36689. + size_t blksize, len, chunk;
  36690. +
  36691. + u32 uninitialized_var(scratch);
  36692. + u8 *buf;
  36693. +
  36694. + blksize = host->data->blksz;
  36695. + chunk = 0;
  36696. +
  36697. + local_irq_save(flags);
  36698. +
  36699. + while (blksize) {
  36700. + if (!sg_miter_next(&host->sg_miter))
  36701. + BUG();
  36702. +
  36703. + len = min(host->sg_miter.length, blksize);
  36704. +
  36705. + blksize -= len;
  36706. + host->sg_miter.consumed = len;
  36707. +
  36708. + buf = host->sg_miter.addr;
  36709. +
  36710. + while (len) {
  36711. + if (chunk == 0) {
  36712. + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
  36713. + chunk = 4;
  36714. + }
  36715. +
  36716. + *buf = scratch & 0xFF;
  36717. +
  36718. + buf++;
  36719. + scratch >>= 8;
  36720. + chunk--;
  36721. + len--;
  36722. + }
  36723. + }
  36724. +
  36725. + sg_miter_stop(&host->sg_miter);
  36726. +
  36727. + local_irq_restore(flags);
  36728. +}
  36729. +
  36730. +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
  36731. +{
  36732. + unsigned long flags;
  36733. + size_t blksize, len, chunk;
  36734. + u32 scratch;
  36735. + u8 *buf;
  36736. +
  36737. + blksize = host->data->blksz;
  36738. + chunk = 0;
  36739. + chunk = 0;
  36740. + scratch = 0;
  36741. +
  36742. + local_irq_save(flags);
  36743. +
  36744. + while (blksize) {
  36745. + if (!sg_miter_next(&host->sg_miter))
  36746. + BUG();
  36747. +
  36748. + len = min(host->sg_miter.length, blksize);
  36749. +
  36750. + blksize -= len;
  36751. + host->sg_miter.consumed = len;
  36752. +
  36753. + buf = host->sg_miter.addr;
  36754. +
  36755. + while (len) {
  36756. + scratch |= (u32)*buf << (chunk * 8);
  36757. +
  36758. + buf++;
  36759. + chunk++;
  36760. + len--;
  36761. +
  36762. + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  36763. + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
  36764. + chunk = 0;
  36765. + scratch = 0;
  36766. + }
  36767. + }
  36768. + }
  36769. +
  36770. + sg_miter_stop(&host->sg_miter);
  36771. +
  36772. + local_irq_restore(flags);
  36773. +}
  36774. +
  36775. +
  36776. +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
  36777. +{
  36778. + u32 mask;
  36779. +
  36780. + BUG_ON(!host->data);
  36781. +
  36782. + if (host->blocks == 0)
  36783. + return;
  36784. +
  36785. + if (host->data->flags & MMC_DATA_READ)
  36786. + mask = SDHCI_DATA_AVAILABLE;
  36787. + else
  36788. + mask = SDHCI_SPACE_AVAILABLE;
  36789. +
  36790. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  36791. +
  36792. + if (host->data->flags & MMC_DATA_READ)
  36793. + bcm2835_bcm2835_mmc_read_block_pio(host);
  36794. + else
  36795. + bcm2835_bcm2835_mmc_write_block_pio(host);
  36796. +
  36797. + host->blocks--;
  36798. +
  36799. + /* QUIRK used in sdhci.c removes the 'if' */
  36800. + /* but it seems this is unnecessary */
  36801. + if (host->blocks == 0)
  36802. + break;
  36803. +
  36804. +
  36805. + }
  36806. +}
  36807. +
  36808. +
  36809. +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
  36810. +{
  36811. + u32 len, dir_data, dir_slave;
  36812. + struct dma_async_tx_descriptor *desc = NULL;
  36813. + struct dma_chan *dma_chan;
  36814. +
  36815. +
  36816. + WARN_ON(!host->data);
  36817. +
  36818. + if (!host->data)
  36819. + return;
  36820. +
  36821. + if (host->blocks == 0)
  36822. + return;
  36823. +
  36824. + if (host->data->flags & MMC_DATA_READ) {
  36825. + dma_chan = host->dma_chan_rx;
  36826. + dir_data = DMA_FROM_DEVICE;
  36827. + dir_slave = DMA_DEV_TO_MEM;
  36828. + } else {
  36829. + dma_chan = host->dma_chan_tx;
  36830. + dir_data = DMA_TO_DEVICE;
  36831. + dir_slave = DMA_MEM_TO_DEV;
  36832. + }
  36833. +
  36834. + BUG_ON(!dma_chan->device);
  36835. + BUG_ON(!dma_chan->device->dev);
  36836. + BUG_ON(!host->data->sg);
  36837. +
  36838. + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
  36839. + host->data->sg_len, dir_data);
  36840. + if (len > 0) {
  36841. + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
  36842. + len, dir_slave,
  36843. + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  36844. + } else {
  36845. + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  36846. + }
  36847. + if (desc) {
  36848. + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  36849. + SDHCI_INT_SPACE_AVAIL);
  36850. + host->tx_desc = desc;
  36851. + desc->callback = bcm2835_mmc_dma_complete;
  36852. + desc->callback_param = host;
  36853. + dmaengine_submit(desc);
  36854. + dma_async_issue_pending(dma_chan);
  36855. + }
  36856. +
  36857. +}
  36858. +
  36859. +
  36860. +
  36861. +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
  36862. +{
  36863. + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  36864. + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  36865. +
  36866. + if (host->use_dma)
  36867. + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  36868. + else
  36869. + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  36870. +
  36871. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  36872. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  36873. +}
  36874. +
  36875. +
  36876. +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  36877. +{
  36878. + u8 count;
  36879. + struct mmc_data *data = cmd->data;
  36880. +
  36881. + WARN_ON(host->data);
  36882. +
  36883. + if (data || (cmd->flags & MMC_RSP_BUSY)) {
  36884. + count = TIMEOUT_VAL;
  36885. + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  36886. + }
  36887. +
  36888. + if (!data)
  36889. + return;
  36890. +
  36891. + /* Sanity checks */
  36892. + BUG_ON(data->blksz * data->blocks > 524288);
  36893. + BUG_ON(data->blksz > host->mmc->max_blk_size);
  36894. + BUG_ON(data->blocks > 65535);
  36895. +
  36896. + host->data = data;
  36897. + host->data_early = 0;
  36898. + host->data->bytes_xfered = 0;
  36899. +
  36900. +
  36901. + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  36902. + int flags;
  36903. +
  36904. + flags = SG_MITER_ATOMIC;
  36905. + if (host->data->flags & MMC_DATA_READ)
  36906. + flags |= SG_MITER_TO_SG;
  36907. + else
  36908. + flags |= SG_MITER_FROM_SG;
  36909. + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  36910. + host->blocks = data->blocks;
  36911. + }
  36912. +
  36913. + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
  36914. +
  36915. + bcm2835_mmc_set_transfer_irqs(host);
  36916. +
  36917. + /* Set the DMA boundary value and block size */
  36918. + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  36919. + data->blksz), SDHCI_BLOCK_SIZE);
  36920. + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  36921. +
  36922. + BUG_ON(!host->data);
  36923. +}
  36924. +
  36925. +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
  36926. + struct mmc_command *cmd)
  36927. +{
  36928. + u16 mode;
  36929. + struct mmc_data *data = cmd->data;
  36930. +
  36931. + if (data == NULL) {
  36932. + /* clear Auto CMD settings for no data CMDs */
  36933. + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
  36934. + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  36935. + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  36936. + return;
  36937. + }
  36938. +
  36939. + WARN_ON(!host->data);
  36940. +
  36941. + mode = SDHCI_TRNS_BLK_CNT_EN;
  36942. +
  36943. + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
  36944. + mode |= SDHCI_TRNS_MULTI;
  36945. +
  36946. + /*
  36947. + * If we are sending CMD23, CMD12 never gets sent
  36948. + * on successful completion (so no Auto-CMD12).
  36949. + */
  36950. + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  36951. + mode |= SDHCI_TRNS_AUTO_CMD12;
  36952. + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  36953. + mode |= SDHCI_TRNS_AUTO_CMD23;
  36954. + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
  36955. + }
  36956. + }
  36957. +
  36958. + if (data->flags & MMC_DATA_READ)
  36959. + mode |= SDHCI_TRNS_READ;
  36960. + if (host->flags & SDHCI_REQ_USE_DMA)
  36961. + mode |= SDHCI_TRNS_DMA;
  36962. +
  36963. + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
  36964. +}
  36965. +
  36966. +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  36967. +{
  36968. + int flags;
  36969. + u32 mask;
  36970. + unsigned long timeout;
  36971. +
  36972. + WARN_ON(host->cmd);
  36973. +
  36974. + /* Wait max 10 ms */
  36975. + timeout = 1000;
  36976. +
  36977. + mask = SDHCI_CMD_INHIBIT;
  36978. + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  36979. + mask |= SDHCI_DATA_INHIBIT;
  36980. +
  36981. + /* We shouldn't wait for data inihibit for stop commands, even
  36982. + though they might use busy signaling */
  36983. + if (host->mrq->data && (cmd == host->mrq->data->stop))
  36984. + mask &= ~SDHCI_DATA_INHIBIT;
  36985. +
  36986. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  36987. + if (timeout == 0) {
  36988. + pr_err("%s: Controller never released inhibit bit(s).\n",
  36989. + mmc_hostname(host->mmc));
  36990. + bcm2835_mmc_dumpregs(host);
  36991. + cmd->error = -EIO;
  36992. + tasklet_schedule(&host->finish_tasklet);
  36993. + return;
  36994. + }
  36995. + timeout--;
  36996. + udelay(10);
  36997. + }
  36998. +
  36999. + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
  37000. + host->max_delay = (1000-timeout)/100;
  37001. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  37002. + }
  37003. +
  37004. + timeout = jiffies;
  37005. +#ifdef CONFIG_ARCH_BCM2835
  37006. + if (!cmd->data && cmd->busy_timeout > 9000)
  37007. + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  37008. + else
  37009. +#endif
  37010. + timeout += 10 * HZ;
  37011. + mod_timer(&host->timer, timeout);
  37012. +
  37013. + host->cmd = cmd;
  37014. +
  37015. + bcm2835_mmc_prepare_data(host, cmd);
  37016. +
  37017. + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT);
  37018. +
  37019. + bcm2835_mmc_set_transfer_mode(host, cmd);
  37020. +
  37021. + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  37022. + pr_err("%s: Unsupported response type!\n",
  37023. + mmc_hostname(host->mmc));
  37024. + cmd->error = -EINVAL;
  37025. + tasklet_schedule(&host->finish_tasklet);
  37026. + return;
  37027. + }
  37028. +
  37029. + if (!(cmd->flags & MMC_RSP_PRESENT))
  37030. + flags = SDHCI_CMD_RESP_NONE;
  37031. + else if (cmd->flags & MMC_RSP_136)
  37032. + flags = SDHCI_CMD_RESP_LONG;
  37033. + else if (cmd->flags & MMC_RSP_BUSY)
  37034. + flags = SDHCI_CMD_RESP_SHORT_BUSY;
  37035. + else
  37036. + flags = SDHCI_CMD_RESP_SHORT;
  37037. +
  37038. + if (cmd->flags & MMC_RSP_CRC)
  37039. + flags |= SDHCI_CMD_CRC;
  37040. + if (cmd->flags & MMC_RSP_OPCODE)
  37041. + flags |= SDHCI_CMD_INDEX;
  37042. +
  37043. + if (cmd->data)
  37044. + flags |= SDHCI_CMD_DATA;
  37045. +
  37046. + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  37047. +}
  37048. +
  37049. +
  37050. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
  37051. +{
  37052. + struct mmc_data *data;
  37053. +
  37054. + BUG_ON(!host->data);
  37055. +
  37056. + data = host->data;
  37057. + host->data = NULL;
  37058. +
  37059. + if (data->error)
  37060. + data->bytes_xfered = 0;
  37061. + else
  37062. + data->bytes_xfered = data->blksz * data->blocks;
  37063. +
  37064. + /*
  37065. + * Need to send CMD12 if -
  37066. + * a) open-ended multiblock transfer (no CMD23)
  37067. + * b) error in multiblock transfer
  37068. + */
  37069. + if (data->stop &&
  37070. + (data->error ||
  37071. + !host->mrq->sbc)) {
  37072. +
  37073. + /*
  37074. + * The controller needs a reset of internal state machines
  37075. + * upon error conditions.
  37076. + */
  37077. + if (data->error) {
  37078. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  37079. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  37080. + }
  37081. +
  37082. + bcm2835_mmc_send_command(host, data->stop);
  37083. + } else
  37084. + tasklet_schedule(&host->finish_tasklet);
  37085. +}
  37086. +
  37087. +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
  37088. +{
  37089. + int i;
  37090. +
  37091. + BUG_ON(host->cmd == NULL);
  37092. +
  37093. + if (host->cmd->flags & MMC_RSP_PRESENT) {
  37094. + if (host->cmd->flags & MMC_RSP_136) {
  37095. + /* CRC is stripped so we need to do some shifting. */
  37096. + for (i = 0; i < 4; i++) {
  37097. + host->cmd->resp[i] = bcm2835_mmc_readl(host,
  37098. + SDHCI_RESPONSE + (3-i)*4) << 8;
  37099. + if (i != 3)
  37100. + host->cmd->resp[i] |=
  37101. + bcm2835_mmc_readb(host,
  37102. + SDHCI_RESPONSE + (3-i)*4-1);
  37103. + }
  37104. + } else {
  37105. + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
  37106. + }
  37107. + }
  37108. +
  37109. + host->cmd->error = 0;
  37110. +
  37111. + /* Finished CMD23, now send actual command. */
  37112. + if (host->cmd == host->mrq->sbc) {
  37113. + host->cmd = NULL;
  37114. + bcm2835_mmc_send_command(host, host->mrq->cmd);
  37115. + } else {
  37116. +
  37117. + /* Processed actual command. */
  37118. + if (host->data && host->data_early)
  37119. + bcm2835_mmc_finish_data(host);
  37120. +
  37121. + if (!host->cmd->data)
  37122. + tasklet_schedule(&host->finish_tasklet);
  37123. +
  37124. + host->cmd = NULL;
  37125. + }
  37126. +}
  37127. +
  37128. +
  37129. +static void bcm2835_mmc_timeout_timer(unsigned long data)
  37130. +{
  37131. + struct bcm2835_host *host;
  37132. + unsigned long flags;
  37133. +
  37134. + host = (struct bcm2835_host *)data;
  37135. +
  37136. + spin_lock_irqsave(&host->lock, flags);
  37137. +
  37138. + if (host->mrq) {
  37139. + pr_err("%s: Timeout waiting for hardware interrupt.\n",
  37140. + mmc_hostname(host->mmc));
  37141. + bcm2835_mmc_dumpregs(host);
  37142. +
  37143. + if (host->data) {
  37144. + host->data->error = -ETIMEDOUT;
  37145. + bcm2835_mmc_finish_data(host);
  37146. + } else {
  37147. + if (host->cmd)
  37148. + host->cmd->error = -ETIMEDOUT;
  37149. + else
  37150. + host->mrq->cmd->error = -ETIMEDOUT;
  37151. +
  37152. + tasklet_schedule(&host->finish_tasklet);
  37153. + }
  37154. + }
  37155. +
  37156. + mmiowb();
  37157. + spin_unlock_irqrestore(&host->lock, flags);
  37158. +}
  37159. +
  37160. +
  37161. +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
  37162. +{
  37163. + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  37164. + if (enable)
  37165. + host->ier |= SDHCI_INT_CARD_INT;
  37166. + else
  37167. + host->ier &= ~SDHCI_INT_CARD_INT;
  37168. +
  37169. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  37170. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  37171. + mmiowb();
  37172. + }
  37173. +}
  37174. +
  37175. +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  37176. +{
  37177. + struct bcm2835_host *host = mmc_priv(mmc);
  37178. + unsigned long flags;
  37179. +
  37180. + spin_lock_irqsave(&host->lock, flags);
  37181. + if (enable)
  37182. + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  37183. + else
  37184. + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  37185. +
  37186. + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
  37187. + spin_unlock_irqrestore(&host->lock, flags);
  37188. +}
  37189. +
  37190. +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
  37191. +{
  37192. +
  37193. + BUG_ON(intmask == 0);
  37194. +
  37195. + if (!host->cmd) {
  37196. + pr_err("%s: Got command interrupt 0x%08x even "
  37197. + "though no command operation was in progress.\n",
  37198. + mmc_hostname(host->mmc), (unsigned)intmask);
  37199. + bcm2835_mmc_dumpregs(host);
  37200. + return;
  37201. + }
  37202. +
  37203. + if (intmask & SDHCI_INT_TIMEOUT)
  37204. + host->cmd->error = -ETIMEDOUT;
  37205. + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  37206. + SDHCI_INT_INDEX)) {
  37207. + host->cmd->error = -EILSEQ;
  37208. + }
  37209. +
  37210. + if (host->cmd->error) {
  37211. + tasklet_schedule(&host->finish_tasklet);
  37212. + return;
  37213. + }
  37214. +
  37215. + if (intmask & SDHCI_INT_RESPONSE)
  37216. + bcm2835_mmc_finish_command(host);
  37217. +
  37218. +}
  37219. +
  37220. +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
  37221. +{
  37222. + struct dma_chan *dma_chan;
  37223. + u32 dir_data;
  37224. +
  37225. + BUG_ON(intmask == 0);
  37226. +
  37227. + if (!host->data) {
  37228. + /*
  37229. + * The "data complete" interrupt is also used to
  37230. + * indicate that a busy state has ended. See comment
  37231. + * above in sdhci_cmd_irq().
  37232. + */
  37233. + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  37234. + if (intmask & SDHCI_INT_DATA_END) {
  37235. + bcm2835_mmc_finish_command(host);
  37236. + return;
  37237. + }
  37238. + }
  37239. +
  37240. + pr_debug("%s: Got data interrupt 0x%08x even "
  37241. + "though no data operation was in progress.\n",
  37242. + mmc_hostname(host->mmc), (unsigned)intmask);
  37243. + bcm2835_mmc_dumpregs(host);
  37244. +
  37245. + return;
  37246. + }
  37247. +
  37248. + if (intmask & SDHCI_INT_DATA_TIMEOUT)
  37249. + host->data->error = -ETIMEDOUT;
  37250. + else if (intmask & SDHCI_INT_DATA_END_BIT)
  37251. + host->data->error = -EILSEQ;
  37252. + else if ((intmask & SDHCI_INT_DATA_CRC) &&
  37253. + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
  37254. + != MMC_BUS_TEST_R)
  37255. + host->data->error = -EILSEQ;
  37256. +
  37257. + if (host->use_dma) {
  37258. + if (host->data->flags & MMC_DATA_WRITE) {
  37259. + /* IRQ handled here */
  37260. +
  37261. + dma_chan = host->dma_chan_tx;
  37262. + dir_data = DMA_TO_DEVICE;
  37263. + dma_unmap_sg(dma_chan->device->dev,
  37264. + host->data->sg, host->data->sg_len,
  37265. + dir_data);
  37266. +
  37267. + bcm2835_mmc_finish_data(host);
  37268. + }
  37269. +
  37270. + } else {
  37271. + if (host->data->error)
  37272. + bcm2835_mmc_finish_data(host);
  37273. + else {
  37274. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  37275. + bcm2835_mmc_transfer_pio(host);
  37276. +
  37277. + if (intmask & SDHCI_INT_DATA_END) {
  37278. + if (host->cmd) {
  37279. + /*
  37280. + * Data managed to finish before the
  37281. + * command completed. Make sure we do
  37282. + * things in the proper order.
  37283. + */
  37284. + host->data_early = 1;
  37285. + } else {
  37286. + bcm2835_mmc_finish_data(host);
  37287. + }
  37288. + }
  37289. + }
  37290. + }
  37291. +}
  37292. +
  37293. +
  37294. +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
  37295. +{
  37296. + irqreturn_t result = IRQ_NONE;
  37297. + struct bcm2835_host *host = dev_id;
  37298. + u32 intmask, mask, unexpected = 0;
  37299. + int max_loops = 16;
  37300. +#ifndef CONFIG_ARCH_BCM2835
  37301. + int cardint = 0;
  37302. +#endif
  37303. +
  37304. + spin_lock(&host->lock);
  37305. +
  37306. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  37307. +
  37308. + if (!intmask || intmask == 0xffffffff) {
  37309. + result = IRQ_NONE;
  37310. + goto out;
  37311. + }
  37312. +
  37313. + do {
  37314. + /* Clear selected interrupts. */
  37315. + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  37316. + SDHCI_INT_BUS_POWER);
  37317. + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS);
  37318. +
  37319. +
  37320. + if (intmask & SDHCI_INT_CMD_MASK)
  37321. + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  37322. +
  37323. + if (intmask & SDHCI_INT_DATA_MASK)
  37324. + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  37325. +
  37326. + if (intmask & SDHCI_INT_BUS_POWER)
  37327. + pr_err("%s: Card is consuming too much power!\n",
  37328. + mmc_hostname(host->mmc));
  37329. +
  37330. + if (intmask & SDHCI_INT_CARD_INT) {
  37331. +#ifndef CONFIG_ARCH_BCM2835
  37332. + cardint = 1;
  37333. +#else
  37334. + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
  37335. + host->thread_isr |= SDHCI_INT_CARD_INT;
  37336. + result = IRQ_WAKE_THREAD;
  37337. +#endif
  37338. + }
  37339. +
  37340. + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  37341. + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  37342. + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  37343. + SDHCI_INT_CARD_INT);
  37344. +
  37345. + if (intmask) {
  37346. + unexpected |= intmask;
  37347. + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS);
  37348. + }
  37349. +
  37350. + if (result == IRQ_NONE)
  37351. + result = IRQ_HANDLED;
  37352. +
  37353. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  37354. + } while (intmask && --max_loops);
  37355. +out:
  37356. + spin_unlock(&host->lock);
  37357. +
  37358. + if (unexpected) {
  37359. + pr_err("%s: Unexpected interrupt 0x%08x.\n",
  37360. + mmc_hostname(host->mmc), unexpected);
  37361. + bcm2835_mmc_dumpregs(host);
  37362. + }
  37363. +
  37364. +#ifndef CONFIG_ARCH_BCM2835
  37365. + if (cardint)
  37366. + mmc_signal_sdio_irq(host->mmc);
  37367. +#endif
  37368. +
  37369. + return result;
  37370. +}
  37371. +
  37372. +#ifdef CONFIG_ARCH_BCM2835
  37373. +static irqreturn_t bcm2835_mmc_thread_irq(int irq, void *dev_id)
  37374. +{
  37375. + struct bcm2835_host *host = dev_id;
  37376. + unsigned long flags;
  37377. + u32 isr;
  37378. +
  37379. + spin_lock_irqsave(&host->lock, flags);
  37380. + isr = host->thread_isr;
  37381. + host->thread_isr = 0;
  37382. + spin_unlock_irqrestore(&host->lock, flags);
  37383. +
  37384. + if (isr & SDHCI_INT_CARD_INT) {
  37385. + sdio_run_irqs(host->mmc);
  37386. +
  37387. + spin_lock_irqsave(&host->lock, flags);
  37388. + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  37389. + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
  37390. + spin_unlock_irqrestore(&host->lock, flags);
  37391. + }
  37392. +
  37393. + return isr ? IRQ_HANDLED : IRQ_NONE;
  37394. +}
  37395. +#endif
  37396. +
  37397. +
  37398. +
  37399. +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
  37400. +{
  37401. + int div = 0; /* Initialized for compiler warning */
  37402. + int real_div = div, clk_mul = 1;
  37403. + u16 clk = 0;
  37404. + unsigned long timeout;
  37405. +
  37406. +
  37407. + host->mmc->actual_clock = 0;
  37408. +
  37409. + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
  37410. +
  37411. + if (clock == 0)
  37412. + return;
  37413. +
  37414. + /* Version 3.00 divisors must be a multiple of 2. */
  37415. + if (host->max_clk <= clock)
  37416. + div = 1;
  37417. + else {
  37418. + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  37419. + div += 2) {
  37420. + if ((host->max_clk / div) <= clock)
  37421. + break;
  37422. + }
  37423. + }
  37424. +
  37425. + real_div = div;
  37426. + div >>= 1;
  37427. +
  37428. + if (real_div)
  37429. + host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
  37430. +
  37431. + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  37432. + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  37433. + << SDHCI_DIVIDER_HI_SHIFT;
  37434. + clk |= SDHCI_CLOCK_INT_EN;
  37435. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  37436. +
  37437. + /* Wait max 20 ms */
  37438. + timeout = 20;
  37439. + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
  37440. + & SDHCI_CLOCK_INT_STABLE)) {
  37441. + if (timeout == 0) {
  37442. + pr_err("%s: Internal clock never "
  37443. + "stabilised.\n", mmc_hostname(host->mmc));
  37444. + bcm2835_mmc_dumpregs(host);
  37445. + return;
  37446. + }
  37447. + timeout--;
  37448. + mdelay(1);
  37449. + }
  37450. +
  37451. + if (20-timeout > 10 && 20-timeout > host->max_delay) {
  37452. + host->max_delay = 20-timeout;
  37453. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  37454. + }
  37455. +
  37456. + clk |= SDHCI_CLOCK_CARD_EN;
  37457. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  37458. +}
  37459. +
  37460. +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  37461. +{
  37462. + struct bcm2835_host *host;
  37463. + unsigned long flags;
  37464. +
  37465. + host = mmc_priv(mmc);
  37466. +
  37467. + spin_lock_irqsave(&host->lock, flags);
  37468. +
  37469. + WARN_ON(host->mrq != NULL);
  37470. +
  37471. + host->mrq = mrq;
  37472. + bcm2835_mmc_send_command(host, mrq->cmd);
  37473. + mmiowb();
  37474. + spin_unlock_irqrestore(&host->lock, flags);
  37475. +
  37476. + if (mrq->cmd->data && host->use_dma) {
  37477. + /* DMA transfer starts now, PIO starts after interrupt */
  37478. + bcm2835_mmc_transfer_dma(host);
  37479. + }
  37480. +}
  37481. +
  37482. +
  37483. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  37484. +{
  37485. +
  37486. + struct bcm2835_host *host = mmc_priv(mmc);
  37487. + unsigned long flags;
  37488. + u8 ctrl;
  37489. + u16 clk, ctrl_2;
  37490. +
  37491. +
  37492. + spin_lock_irqsave(&host->lock, flags);
  37493. +
  37494. + if (!ios->clock || ios->clock != host->clock) {
  37495. + bcm2835_mmc_set_clock(host, ios->clock);
  37496. + host->clock = ios->clock;
  37497. + }
  37498. +
  37499. + if (host->pwr != SDHCI_POWER_330) {
  37500. + host->pwr = SDHCI_POWER_330;
  37501. + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  37502. + }
  37503. +
  37504. + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
  37505. +
  37506. + /* set bus width */
  37507. + ctrl &= ~SDHCI_CTRL_8BITBUS;
  37508. + if (ios->bus_width == MMC_BUS_WIDTH_4)
  37509. + ctrl |= SDHCI_CTRL_4BITBUS;
  37510. + else
  37511. + ctrl &= ~SDHCI_CTRL_4BITBUS;
  37512. +
  37513. + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
  37514. +
  37515. +
  37516. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  37517. + /*
  37518. + * We only need to set Driver Strength if the
  37519. + * preset value enable is not set.
  37520. + */
  37521. + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
  37522. + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  37523. + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  37524. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  37525. + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  37526. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  37527. +
  37528. + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  37529. +
  37530. + /* Reset SD Clock Enable */
  37531. + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
  37532. + clk &= ~SDHCI_CLOCK_CARD_EN;
  37533. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  37534. +
  37535. + /* Re-enable SD Clock */
  37536. + bcm2835_mmc_set_clock(host, host->clock);
  37537. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  37538. +
  37539. + mmiowb();
  37540. +
  37541. + spin_unlock_irqrestore(&host->lock, flags);
  37542. +}
  37543. +
  37544. +
  37545. +static struct mmc_host_ops bcm2835_ops = {
  37546. + .request = bcm2835_mmc_request,
  37547. + .set_ios = bcm2835_mmc_set_ios,
  37548. + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
  37549. +};
  37550. +
  37551. +
  37552. +static void bcm2835_mmc_tasklet_finish(unsigned long param)
  37553. +{
  37554. + struct bcm2835_host *host;
  37555. + unsigned long flags;
  37556. + struct mmc_request *mrq;
  37557. +
  37558. + host = (struct bcm2835_host *)param;
  37559. +
  37560. + spin_lock_irqsave(&host->lock, flags);
  37561. +
  37562. + /*
  37563. + * If this tasklet gets rescheduled while running, it will
  37564. + * be run again afterwards but without any active request.
  37565. + */
  37566. + if (!host->mrq) {
  37567. + spin_unlock_irqrestore(&host->lock, flags);
  37568. + return;
  37569. + }
  37570. +
  37571. + del_timer(&host->timer);
  37572. +
  37573. + mrq = host->mrq;
  37574. +
  37575. + /*
  37576. + * The controller needs a reset of internal state machines
  37577. + * upon error conditions.
  37578. + */
  37579. + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  37580. + ((mrq->cmd && mrq->cmd->error) ||
  37581. + (mrq->data && (mrq->data->error ||
  37582. + (mrq->data->stop && mrq->data->stop->error))))) {
  37583. +
  37584. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  37585. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  37586. + }
  37587. +
  37588. + host->mrq = NULL;
  37589. + host->cmd = NULL;
  37590. + host->data = NULL;
  37591. +
  37592. + mmiowb();
  37593. +
  37594. + spin_unlock_irqrestore(&host->lock, flags);
  37595. + mmc_request_done(host->mmc, mrq);
  37596. +}
  37597. +
  37598. +
  37599. +
  37600. +int bcm2835_mmc_add_host(struct bcm2835_host *host)
  37601. +{
  37602. + struct mmc_host *mmc;
  37603. +#ifndef FORCE_PIO
  37604. + struct dma_slave_config cfg;
  37605. +#endif
  37606. + int ret;
  37607. +
  37608. + mmc = host->mmc;
  37609. +
  37610. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  37611. +
  37612. + host->clk_mul = 0;
  37613. +
  37614. + mmc->ops = &bcm2835_ops;
  37615. + mmc->f_max = host->max_clk;
  37616. + mmc->f_max = host->max_clk;
  37617. + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  37618. +
  37619. + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
  37620. + host->timeout_clk = mmc->f_max / 1000;
  37621. +#ifdef CONFIG_ARCH_BCM2835
  37622. + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
  37623. +#endif
  37624. + /* host controller capabilities */
  37625. + mmc->caps = MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL | MMC_CAP_SDIO_IRQ |
  37626. + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | MMC_CAP_4_BIT_DATA;
  37627. +
  37628. + host->flags = SDHCI_AUTO_CMD23;
  37629. +
  37630. + spin_lock_init(&host->lock);
  37631. +
  37632. +
  37633. +#ifdef FORCE_PIO
  37634. + pr_info("Forcing PIO mode\n");
  37635. + host->have_dma = false;
  37636. +#else
  37637. + if (!host->dma_chan_tx || !host->dma_chan_rx ||
  37638. + IS_ERR(host->dma_chan_tx) || IS_ERR(host->dma_chan_rx)) {
  37639. + pr_err("%s: Unable to initialise DMA channels. Falling back to PIO\n", DRIVER_NAME);
  37640. + host->have_dma = false;
  37641. + } else {
  37642. + pr_info("DMA channels allocated for the MMC driver");
  37643. + host->have_dma = true;
  37644. +
  37645. + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  37646. + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  37647. + cfg.slave_id = 11; /* DREQ channel */
  37648. +
  37649. + cfg.direction = DMA_MEM_TO_DEV;
  37650. + cfg.src_addr = 0;
  37651. + cfg.dst_addr = host->phys_addr + SDHCI_BUFFER;
  37652. + ret = dmaengine_slave_config(host->dma_chan_tx, &cfg);
  37653. +
  37654. + cfg.direction = DMA_DEV_TO_MEM;
  37655. + cfg.src_addr = host->phys_addr + SDHCI_BUFFER;
  37656. + cfg.dst_addr = 0;
  37657. + ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
  37658. + }
  37659. +#endif
  37660. +
  37661. +
  37662. + mmc->max_segs = 128;
  37663. + mmc->max_req_size = 524288;
  37664. + mmc->max_seg_size = mmc->max_req_size;
  37665. + mmc->max_blk_size = 512;
  37666. + mmc->max_blk_count = 65535;
  37667. +
  37668. + /* report supported voltage ranges */
  37669. + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  37670. +
  37671. + tasklet_init(&host->finish_tasklet,
  37672. + bcm2835_mmc_tasklet_finish, (unsigned long)host);
  37673. +
  37674. + setup_timer(&host->timer, bcm2835_mmc_timeout_timer, (unsigned long)host);
  37675. + init_waitqueue_head(&host->buf_ready_int);
  37676. +
  37677. + bcm2835_mmc_init(host, 0);
  37678. +#ifndef CONFIG_ARCH_BCM2835
  37679. + ret = request_irq(host->irq, bcm2835_mmc_irq, 0 /*IRQF_SHARED*/,
  37680. + mmc_hostname(mmc), host);
  37681. +#else
  37682. + ret = request_threaded_irq(host->irq, bcm2835_mmc_irq, bcm2835_mmc_thread_irq,
  37683. + IRQF_SHARED, mmc_hostname(mmc), host);
  37684. +#endif
  37685. + if (ret) {
  37686. + pr_err("%s: Failed to request IRQ %d: %d\n",
  37687. + mmc_hostname(mmc), host->irq, ret);
  37688. + goto untasklet;
  37689. + }
  37690. +
  37691. + mmiowb();
  37692. + mmc_add_host(mmc);
  37693. +
  37694. + pr_info("Load BCM2835 MMC driver\n");
  37695. +
  37696. + return 0;
  37697. +
  37698. +untasklet:
  37699. + tasklet_kill(&host->finish_tasklet);
  37700. +
  37701. + return ret;
  37702. +}
  37703. +
  37704. +static int bcm2835_mmc_probe(struct platform_device *pdev)
  37705. +{
  37706. + struct device *dev = &pdev->dev;
  37707. +#ifdef CONFIG_ARCH_BCM2835
  37708. + struct device_node *node = dev->of_node;
  37709. + struct clk *clk;
  37710. +#endif
  37711. + struct resource *iomem;
  37712. + struct bcm2835_host *host = NULL;
  37713. +
  37714. + int ret;
  37715. + struct mmc_host *mmc;
  37716. +#if !defined(CONFIG_ARCH_BCM2835) && !defined(FORCE_PIO)
  37717. + dma_cap_mask_t mask;
  37718. +#endif
  37719. +
  37720. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  37721. + if (!iomem) {
  37722. + ret = -ENOMEM;
  37723. + goto err;
  37724. + }
  37725. +
  37726. + if (resource_size(iomem) < 0x100)
  37727. + dev_err(&pdev->dev, "Invalid iomem size!\n");
  37728. +
  37729. + mmc = mmc_alloc_host(sizeof(struct bcm2835_host), dev);
  37730. + host = mmc_priv(mmc);
  37731. + host->mmc = mmc;
  37732. +
  37733. +
  37734. + if (IS_ERR(host)) {
  37735. + ret = PTR_ERR(host);
  37736. + goto err;
  37737. + }
  37738. +
  37739. + host->phys_addr = iomem->start + BCM2835_VCMMU_SHIFT;
  37740. +
  37741. +#ifndef CONFIG_ARCH_BCM2835
  37742. +#ifndef FORCE_PIO
  37743. + dma_cap_zero(mask);
  37744. + /* we don't care about the channel, any would work */
  37745. + dma_cap_set(DMA_SLAVE, mask);
  37746. +
  37747. + host->dma_chan_tx = dma_request_channel(mask, NULL, NULL);
  37748. + host->dma_chan_rx = dma_request_channel(mask, NULL, NULL);
  37749. +#endif
  37750. + host->max_clk = BCM2835_CLOCK_FREQ;
  37751. +
  37752. +#else
  37753. +#ifndef FORCE_PIO
  37754. + host->dma_chan_tx = of_dma_request_slave_channel(node, "tx");
  37755. + host->dma_chan_rx = of_dma_request_slave_channel(node, "rx");
  37756. +#endif
  37757. + clk = of_clk_get(node, 0);
  37758. + if (IS_ERR(clk)) {
  37759. + dev_err(dev, "get CLOCK failed\n");
  37760. + ret = PTR_ERR(clk);
  37761. + goto out;
  37762. + }
  37763. + host->max_clk = (clk_get_rate(clk));
  37764. +#endif
  37765. + host->irq = platform_get_irq(pdev, 0);
  37766. +
  37767. + if (!request_mem_region(iomem->start, resource_size(iomem),
  37768. + mmc_hostname(host->mmc))) {
  37769. + dev_err(&pdev->dev, "cannot request region\n");
  37770. + ret = -EBUSY;
  37771. + goto err_request;
  37772. + }
  37773. +
  37774. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  37775. + if (!host->ioaddr) {
  37776. + dev_err(&pdev->dev, "failed to remap registers\n");
  37777. + ret = -ENOMEM;
  37778. + goto err_remap;
  37779. + }
  37780. +
  37781. + platform_set_drvdata(pdev, host);
  37782. +
  37783. +
  37784. + if (host->irq <= 0) {
  37785. + dev_err(dev, "get IRQ failed\n");
  37786. + ret = -EINVAL;
  37787. + goto out;
  37788. + }
  37789. +
  37790. +
  37791. +#ifndef CONFIG_ARCH_BCM2835
  37792. + mmc->caps |= MMC_CAP_4_BIT_DATA;
  37793. +#else
  37794. + mmc_of_parse(mmc);
  37795. +#endif
  37796. + host->timeout = msecs_to_jiffies(1000);
  37797. + spin_lock_init(&host->lock);
  37798. + mmc->ops = &bcm2835_ops;
  37799. + return bcm2835_mmc_add_host(host);
  37800. +
  37801. +
  37802. +err_remap:
  37803. + release_mem_region(iomem->start, resource_size(iomem));
  37804. +err_request:
  37805. + mmc_free_host(host->mmc);
  37806. +err:
  37807. + dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  37808. + return ret;
  37809. +out:
  37810. + if (mmc)
  37811. + mmc_free_host(mmc);
  37812. + return ret;
  37813. +}
  37814. +
  37815. +static int bcm2835_mmc_remove(struct platform_device *pdev)
  37816. +{
  37817. + struct bcm2835_host *host = platform_get_drvdata(pdev);
  37818. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  37819. + unsigned long flags;
  37820. + int dead;
  37821. + u32 scratch;
  37822. +
  37823. + dead = 0;
  37824. + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  37825. + if (scratch == (u32)-1)
  37826. + dead = 1;
  37827. +
  37828. +
  37829. + if (dead) {
  37830. + spin_lock_irqsave(&host->lock, flags);
  37831. +
  37832. + host->flags |= SDHCI_DEVICE_DEAD;
  37833. +
  37834. + if (host->mrq) {
  37835. + pr_err("%s: Controller removed during "
  37836. + " transfer!\n", mmc_hostname(host->mmc));
  37837. +
  37838. + host->mrq->cmd->error = -ENOMEDIUM;
  37839. + tasklet_schedule(&host->finish_tasklet);
  37840. + }
  37841. +
  37842. + spin_unlock_irqrestore(&host->lock, flags);
  37843. + }
  37844. +
  37845. + mmc_remove_host(host->mmc);
  37846. +
  37847. + if (!dead)
  37848. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  37849. +
  37850. + free_irq(host->irq, host);
  37851. +
  37852. + del_timer_sync(&host->timer);
  37853. +
  37854. + tasklet_kill(&host->finish_tasklet);
  37855. +
  37856. + iounmap(host->ioaddr);
  37857. + release_mem_region(iomem->start, resource_size(iomem));
  37858. + mmc_free_host(host->mmc);
  37859. + platform_set_drvdata(pdev, NULL);
  37860. +
  37861. + return 0;
  37862. +}
  37863. +
  37864. +
  37865. +static const struct of_device_id bcm2835_mmc_match[] = {
  37866. + { .compatible = "brcm,bcm2835-mmc" },
  37867. + { }
  37868. +};
  37869. +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
  37870. +
  37871. +
  37872. +
  37873. +static struct platform_driver bcm2835_mmc_driver = {
  37874. + .probe = bcm2835_mmc_probe,
  37875. + .remove = bcm2835_mmc_remove,
  37876. + .driver = {
  37877. + .name = DRIVER_NAME,
  37878. + .owner = THIS_MODULE,
  37879. + .of_match_table = bcm2835_mmc_match,
  37880. + },
  37881. +};
  37882. +module_platform_driver(bcm2835_mmc_driver);
  37883. +
  37884. +MODULE_ALIAS("platform:mmc-bcm2835");
  37885. +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
  37886. +MODULE_LICENSE("GPL v2");
  37887. +MODULE_AUTHOR("Gellert Weisz");
  37888. diff -Nur linux-3.17.5/drivers/mmc/host/Kconfig linux-rpi/drivers/mmc/host/Kconfig
  37889. --- linux-3.17.5/drivers/mmc/host/Kconfig 2014-12-06 17:57:59.000000000 -0600
  37890. +++ linux-rpi/drivers/mmc/host/Kconfig 2014-12-11 14:05:38.172418001 -0600
  37891. @@ -270,17 +270,6 @@
  37892. If you have a controller with this interface, say Y or M here.
  37893. -config MMC_SDHCI_BCM2835
  37894. - tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
  37895. - depends on ARCH_BCM2835
  37896. - depends on MMC_SDHCI_PLTFM
  37897. - select MMC_SDHCI_IO_ACCESSORS
  37898. - help
  37899. - This selects the BCM2835 SD/MMC controller. If you have a BCM2835
  37900. - platform with SD or MMC devices, say Y or M here.
  37901. -
  37902. - If unsure, say N.
  37903. -
  37904. config MMC_MOXART
  37905. tristate "MOXART SD/MMC Host Controller support"
  37906. depends on ARCH_MOXART && MMC
  37907. @@ -302,6 +291,35 @@
  37908. If you have a controller with this interface, say Y or M here.
  37909. If unsure, say N.
  37910. +config MMC_BCM2835
  37911. + tristate "MMC support on BCM2835"
  37912. + depends on MACH_BCM2708
  37913. + help
  37914. + This selects the MMC Interface on BCM2835.
  37915. +
  37916. + If you have a controller with this interface, say Y or M here.
  37917. +
  37918. + If unsure, say N.
  37919. +
  37920. +config MMC_BCM2835_DMA
  37921. + bool "DMA support on BCM2835 Arasan controller"
  37922. + depends on MMC_BCM2835
  37923. + help
  37924. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  37925. + based chips.
  37926. +
  37927. + If unsure, say N.
  37928. +
  37929. +config MMC_BCM2835_PIO_DMA_BARRIER
  37930. + int "Block count limit for PIO transfers"
  37931. + depends on MMC_BCM2835 && MMC_BCM2835_DMA
  37932. + range 0 256
  37933. + default 2
  37934. + help
  37935. + The inclusive limit in bytes under which PIO will be used instead of DMA
  37936. +
  37937. + If unsure, say 2 here.
  37938. +
  37939. config MMC_OMAP
  37940. tristate "TI OMAP Multimedia Card Interface support"
  37941. depends on ARCH_OMAP
  37942. diff -Nur linux-3.17.5/drivers/mmc/host/Makefile linux-rpi/drivers/mmc/host/Makefile
  37943. --- linux-3.17.5/drivers/mmc/host/Makefile 2014-12-06 17:57:59.000000000 -0600
  37944. +++ linux-rpi/drivers/mmc/host/Makefile 2014-12-11 14:05:38.172418001 -0600
  37945. @@ -16,6 +16,7 @@
  37946. obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
  37947. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  37948. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  37949. +obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
  37950. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  37951. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  37952. obj-$(CONFIG_MMC_OMAP) += omap.o
  37953. diff -Nur linux-3.17.5/drivers/net/usb/smsc95xx.c linux-rpi/drivers/net/usb/smsc95xx.c
  37954. --- linux-3.17.5/drivers/net/usb/smsc95xx.c 2014-12-06 17:57:59.000000000 -0600
  37955. +++ linux-rpi/drivers/net/usb/smsc95xx.c 2014-12-11 14:05:38.456418001 -0600
  37956. @@ -59,6 +59,7 @@
  37957. #define SUSPEND_SUSPEND3 (0x08)
  37958. #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
  37959. SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
  37960. +#define MAC_ADDR_LEN (6)
  37961. struct smsc95xx_priv {
  37962. u32 mac_cr;
  37963. @@ -74,6 +75,10 @@
  37964. module_param(turbo_mode, bool, 0644);
  37965. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  37966. +static char *macaddr = ":";
  37967. +module_param(macaddr, charp, 0);
  37968. +MODULE_PARM_DESC(macaddr, "MAC address");
  37969. +
  37970. static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
  37971. u32 *data, int in_pm)
  37972. {
  37973. @@ -763,8 +768,59 @@
  37974. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  37975. }
  37976. +/* Check the macaddr module parameter for a MAC address */
  37977. +static int smsc95xx_is_macaddr_param(struct usbnet *dev, u8 *dev_mac)
  37978. +{
  37979. + int i, j, got_num, num;
  37980. + u8 mtbl[MAC_ADDR_LEN];
  37981. +
  37982. + if (macaddr[0] == ':')
  37983. + return 0;
  37984. +
  37985. + i = 0;
  37986. + j = 0;
  37987. + num = 0;
  37988. + got_num = 0;
  37989. + while (j < MAC_ADDR_LEN) {
  37990. + if (macaddr[i] && macaddr[i] != ':') {
  37991. + got_num++;
  37992. + if ('0' <= macaddr[i] && macaddr[i] <= '9')
  37993. + num = num * 16 + macaddr[i] - '0';
  37994. + else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
  37995. + num = num * 16 + 10 + macaddr[i] - 'A';
  37996. + else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
  37997. + num = num * 16 + 10 + macaddr[i] - 'a';
  37998. + else
  37999. + break;
  38000. + i++;
  38001. + } else if (got_num == 2) {
  38002. + mtbl[j++] = (u8) num;
  38003. + num = 0;
  38004. + got_num = 0;
  38005. + i++;
  38006. + } else {
  38007. + break;
  38008. + }
  38009. + }
  38010. +
  38011. + if (j == MAC_ADDR_LEN) {
  38012. + netif_dbg(dev, ifup, dev->net, "Overriding MAC address with: "
  38013. + "%02x:%02x:%02x:%02x:%02x:%02x\n", mtbl[0], mtbl[1], mtbl[2],
  38014. + mtbl[3], mtbl[4], mtbl[5]);
  38015. + for (i = 0; i < MAC_ADDR_LEN; i++)
  38016. + dev_mac[i] = mtbl[i];
  38017. + return 1;
  38018. + } else {
  38019. + return 0;
  38020. + }
  38021. +}
  38022. +
  38023. static void smsc95xx_init_mac_address(struct usbnet *dev)
  38024. {
  38025. + /* Check module parameters */
  38026. + if (smsc95xx_is_macaddr_param(dev, dev->net->dev_addr))
  38027. + return;
  38028. +
  38029. /* try reading mac address from EEPROM */
  38030. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  38031. dev->net->dev_addr) == 0) {
  38032. diff -Nur linux-3.17.5/drivers/of/fdt.c linux-rpi/drivers/of/fdt.c
  38033. --- linux-3.17.5/drivers/of/fdt.c 2014-12-06 17:57:59.000000000 -0600
  38034. +++ linux-rpi/drivers/of/fdt.c 2014-12-11 14:05:38.700418001 -0600
  38035. @@ -901,19 +901,38 @@
  38036. /* Retrieve command line */
  38037. p = of_get_flat_dt_prop(node, "bootargs", &l);
  38038. - if (p != NULL && l > 0)
  38039. - strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  38040. /*
  38041. * CONFIG_CMDLINE is meant to be a default in case nothing else
  38042. * managed to set the command line, unless CONFIG_CMDLINE_FORCE
  38043. * is set in which case we override whatever was found earlier.
  38044. + *
  38045. + * However, it can be useful to be able to treat the default as
  38046. + * a starting point to be extended using CONFIG_CMDLINE_EXTEND.
  38047. */
  38048. + ((char *)data)[0] = '\0';
  38049. +
  38050. #ifdef CONFIG_CMDLINE
  38051. -#ifndef CONFIG_CMDLINE_FORCE
  38052. - if (!((char *)data)[0])
  38053. + strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  38054. +
  38055. + if (p != NULL && l > 0) {
  38056. +#if defined(CONFIG_CMDLINE_EXTEND)
  38057. + int len = strlen(data);
  38058. + if (len > 0) {
  38059. + strlcat(data, " ", COMMAND_LINE_SIZE);
  38060. + len++;
  38061. + }
  38062. + strlcpy((char *)data + len, p, min((int)l, COMMAND_LINE_SIZE - len));
  38063. +#elif defined(CONFIG_CMDLINE_FORCE)
  38064. + pr_warning("Ignoring bootargs property (using the default kernel command line)\n");
  38065. +#else
  38066. + /* Neither extend nor force - just override */
  38067. + strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  38068. #endif
  38069. - strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  38070. + }
  38071. +#else /* CONFIG_CMDLINE */
  38072. + if (p != NULL && l > 0) {
  38073. + strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  38074. #endif /* CONFIG_CMDLINE */
  38075. pr_debug("Command line is: %s\n", (char*)data);
  38076. @@ -1085,8 +1104,12 @@
  38077. static int __init of_flat_dt_debugfs_export_fdt(void)
  38078. {
  38079. - struct dentry *d = debugfs_create_dir("device-tree", NULL);
  38080. + struct dentry *d;
  38081. +
  38082. + if (!initial_boot_params)
  38083. + return -ENOENT;
  38084. + d = debugfs_create_dir("device-tree", NULL);
  38085. if (!d)
  38086. return -ENOENT;
  38087. diff -Nur linux-3.17.5/drivers/pinctrl/Kconfig linux-rpi/drivers/pinctrl/Kconfig
  38088. --- linux-3.17.5/drivers/pinctrl/Kconfig 2014-12-06 17:57:59.000000000 -0600
  38089. +++ linux-rpi/drivers/pinctrl/Kconfig 2014-12-11 14:05:38.724418001 -0600
  38090. @@ -79,6 +79,11 @@
  38091. Requires ACPI device enumeration code to set up a platform device.
  38092. +config PINCTRL_BCM2708
  38093. + bool
  38094. + select PINMUX
  38095. + select PINCONF
  38096. +
  38097. config PINCTRL_BCM2835
  38098. bool
  38099. select PINMUX
  38100. diff -Nur linux-3.17.5/drivers/pinctrl/Makefile linux-rpi/drivers/pinctrl/Makefile
  38101. --- linux-3.17.5/drivers/pinctrl/Makefile 2014-12-06 17:57:59.000000000 -0600
  38102. +++ linux-rpi/drivers/pinctrl/Makefile 2014-12-11 14:05:38.724418001 -0600
  38103. @@ -14,6 +14,7 @@
  38104. obj-$(CONFIG_PINCTRL_BF54x) += pinctrl-adi2-bf54x.o
  38105. obj-$(CONFIG_PINCTRL_BF60x) += pinctrl-adi2-bf60x.o
  38106. obj-$(CONFIG_PINCTRL_AT91) += pinctrl-at91.o
  38107. +obj-$(CONFIG_PINCTRL_BCM2708) += pinctrl-bcm2708.o
  38108. obj-$(CONFIG_PINCTRL_BCM2835) += pinctrl-bcm2835.o
  38109. obj-$(CONFIG_PINCTRL_BAYTRAIL) += pinctrl-baytrail.o
  38110. obj-$(CONFIG_PINCTRL_BCM281XX) += pinctrl-bcm281xx.o
  38111. diff -Nur linux-3.17.5/drivers/pinctrl/pinctrl-bcm2708.c linux-rpi/drivers/pinctrl/pinctrl-bcm2708.c
  38112. --- linux-3.17.5/drivers/pinctrl/pinctrl-bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  38113. +++ linux-rpi/drivers/pinctrl/pinctrl-bcm2708.c 2014-12-11 14:05:38.732418001 -0600
  38114. @@ -0,0 +1,762 @@
  38115. +/*
  38116. + * Driver for Broadcom BCM2708 GPIO unit (pinctrl only)
  38117. + *
  38118. + * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
  38119. + * Copyright (C) 2014 Noralf Tronnes
  38120. + *
  38121. + * This driver is a verbatim copy of the pinctrl-bcm2835 driver, except for:
  38122. + * - changed 2835 to 2708
  38123. + * - gpio_chip and IRQ part are removed
  38124. + * - Probing function is changed.
  38125. + *
  38126. + * Because armctrl sets up the gpio irqs, we use the bcm2708_gpio driver.
  38127. + * This hack is used to be able to support both DT and non-DT builds.
  38128. + * It's not possible to set trigger type and level flags for IRQs in the DT.
  38129. + *
  38130. + * This driver is inspired by:
  38131. + * pinctrl-nomadik.c, please see original file for copyright information
  38132. + * pinctrl-tegra.c, please see original file for copyright information
  38133. + *
  38134. + * This program is free software; you can redistribute it and/or modify
  38135. + * it under the terms of the GNU General Public License as published by
  38136. + * the Free Software Foundation; either version 2 of the License, or
  38137. + * (at your option) any later version.
  38138. + *
  38139. + * This program is distributed in the hope that it will be useful,
  38140. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38141. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38142. + * GNU General Public License for more details.
  38143. + */
  38144. +
  38145. +#include <linux/bitmap.h>
  38146. +#include <linux/bug.h>
  38147. +#include <linux/delay.h>
  38148. +#include <linux/device.h>
  38149. +#include <linux/err.h>
  38150. +#include <linux/gpio.h>
  38151. +#include <linux/interrupt.h>
  38152. +#include <linux/io.h>
  38153. +#include <linux/irq.h>
  38154. +#include <linux/irqdesc.h>
  38155. +#include <linux/irqdomain.h>
  38156. +#include <linux/module.h>
  38157. +#include <linux/of_address.h>
  38158. +#include <linux/of.h>
  38159. +#include <linux/of_gpio.h>
  38160. +#include <linux/of_irq.h>
  38161. +#include <linux/pinctrl/consumer.h>
  38162. +#include <linux/pinctrl/machine.h>
  38163. +#include <linux/pinctrl/pinconf.h>
  38164. +#include <linux/pinctrl/pinctrl.h>
  38165. +#include <linux/pinctrl/pinmux.h>
  38166. +#include <linux/platform_device.h>
  38167. +#include <linux/seq_file.h>
  38168. +#include <linux/slab.h>
  38169. +#include <linux/spinlock.h>
  38170. +#include <linux/types.h>
  38171. +
  38172. +#define MODULE_NAME "pinctrl-bcm2708"
  38173. +#define BCM2708_NUM_GPIOS 54
  38174. +#define BCM2708_NUM_BANKS 2
  38175. +
  38176. +#define BCM2708_PIN_BITMAP_SZ \
  38177. + DIV_ROUND_UP(BCM2708_NUM_GPIOS, sizeof(unsigned long) * 8)
  38178. +
  38179. +/* GPIO register offsets */
  38180. +#define GPFSEL0 0x0 /* Function Select */
  38181. +#define GPSET0 0x1c /* Pin Output Set */
  38182. +#define GPCLR0 0x28 /* Pin Output Clear */
  38183. +#define GPLEV0 0x34 /* Pin Level */
  38184. +#define GPEDS0 0x40 /* Pin Event Detect Status */
  38185. +#define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
  38186. +#define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
  38187. +#define GPHEN0 0x64 /* Pin High Detect Enable */
  38188. +#define GPLEN0 0x70 /* Pin Low Detect Enable */
  38189. +#define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
  38190. +#define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
  38191. +#define GPPUD 0x94 /* Pin Pull-up/down Enable */
  38192. +#define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
  38193. +
  38194. +#define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
  38195. +#define FSEL_SHIFT(p) (((p) % 10) * 3)
  38196. +#define GPIO_REG_OFFSET(p) ((p) / 32)
  38197. +#define GPIO_REG_SHIFT(p) ((p) % 32)
  38198. +
  38199. +enum bcm2708_pinconf_param {
  38200. + /* argument: bcm2708_pinconf_pull */
  38201. + BCM2708_PINCONF_PARAM_PULL,
  38202. +};
  38203. +
  38204. +enum bcm2708_pinconf_pull {
  38205. + BCM2708_PINCONFIG_PULL_NONE,
  38206. + BCM2708_PINCONFIG_PULL_DOWN,
  38207. + BCM2708_PINCONFIG_PULL_UP,
  38208. +};
  38209. +
  38210. +#define BCM2708_PINCONF_PACK(_param_, _arg_) ((_param_) << 16 | (_arg_))
  38211. +#define BCM2708_PINCONF_UNPACK_PARAM(_conf_) ((_conf_) >> 16)
  38212. +#define BCM2708_PINCONF_UNPACK_ARG(_conf_) ((_conf_) & 0xffff)
  38213. +
  38214. +struct bcm2708_gpio_irqdata {
  38215. + struct bcm2708_pinctrl *pc;
  38216. + int bank;
  38217. +};
  38218. +
  38219. +struct bcm2708_pinctrl {
  38220. + struct device *dev;
  38221. + void __iomem *base;
  38222. + int irq[BCM2708_NUM_BANKS];
  38223. +
  38224. + /* note: locking assumes each bank will have its own unsigned long */
  38225. + unsigned long enabled_irq_map[BCM2708_NUM_BANKS];
  38226. + unsigned int irq_type[BCM2708_NUM_GPIOS];
  38227. +
  38228. + struct pinctrl_dev *pctl_dev;
  38229. + struct irq_domain *irq_domain;
  38230. + struct gpio_chip gpio_chip;
  38231. + struct pinctrl_gpio_range gpio_range;
  38232. +
  38233. + struct bcm2708_gpio_irqdata irq_data[BCM2708_NUM_BANKS];
  38234. + spinlock_t irq_lock[BCM2708_NUM_BANKS];
  38235. +};
  38236. +
  38237. +/* pins are just named GPIO0..GPIO53 */
  38238. +#define BCM2708_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
  38239. +static struct pinctrl_pin_desc bcm2708_gpio_pins[] = {
  38240. + BCM2708_GPIO_PIN(0),
  38241. + BCM2708_GPIO_PIN(1),
  38242. + BCM2708_GPIO_PIN(2),
  38243. + BCM2708_GPIO_PIN(3),
  38244. + BCM2708_GPIO_PIN(4),
  38245. + BCM2708_GPIO_PIN(5),
  38246. + BCM2708_GPIO_PIN(6),
  38247. + BCM2708_GPIO_PIN(7),
  38248. + BCM2708_GPIO_PIN(8),
  38249. + BCM2708_GPIO_PIN(9),
  38250. + BCM2708_GPIO_PIN(10),
  38251. + BCM2708_GPIO_PIN(11),
  38252. + BCM2708_GPIO_PIN(12),
  38253. + BCM2708_GPIO_PIN(13),
  38254. + BCM2708_GPIO_PIN(14),
  38255. + BCM2708_GPIO_PIN(15),
  38256. + BCM2708_GPIO_PIN(16),
  38257. + BCM2708_GPIO_PIN(17),
  38258. + BCM2708_GPIO_PIN(18),
  38259. + BCM2708_GPIO_PIN(19),
  38260. + BCM2708_GPIO_PIN(20),
  38261. + BCM2708_GPIO_PIN(21),
  38262. + BCM2708_GPIO_PIN(22),
  38263. + BCM2708_GPIO_PIN(23),
  38264. + BCM2708_GPIO_PIN(24),
  38265. + BCM2708_GPIO_PIN(25),
  38266. + BCM2708_GPIO_PIN(26),
  38267. + BCM2708_GPIO_PIN(27),
  38268. + BCM2708_GPIO_PIN(28),
  38269. + BCM2708_GPIO_PIN(29),
  38270. + BCM2708_GPIO_PIN(30),
  38271. + BCM2708_GPIO_PIN(31),
  38272. + BCM2708_GPIO_PIN(32),
  38273. + BCM2708_GPIO_PIN(33),
  38274. + BCM2708_GPIO_PIN(34),
  38275. + BCM2708_GPIO_PIN(35),
  38276. + BCM2708_GPIO_PIN(36),
  38277. + BCM2708_GPIO_PIN(37),
  38278. + BCM2708_GPIO_PIN(38),
  38279. + BCM2708_GPIO_PIN(39),
  38280. + BCM2708_GPIO_PIN(40),
  38281. + BCM2708_GPIO_PIN(41),
  38282. + BCM2708_GPIO_PIN(42),
  38283. + BCM2708_GPIO_PIN(43),
  38284. + BCM2708_GPIO_PIN(44),
  38285. + BCM2708_GPIO_PIN(45),
  38286. + BCM2708_GPIO_PIN(46),
  38287. + BCM2708_GPIO_PIN(47),
  38288. + BCM2708_GPIO_PIN(48),
  38289. + BCM2708_GPIO_PIN(49),
  38290. + BCM2708_GPIO_PIN(50),
  38291. + BCM2708_GPIO_PIN(51),
  38292. + BCM2708_GPIO_PIN(52),
  38293. + BCM2708_GPIO_PIN(53),
  38294. +};
  38295. +
  38296. +/* one pin per group */
  38297. +static const char * const bcm2708_gpio_groups[] = {
  38298. + "gpio0",
  38299. + "gpio1",
  38300. + "gpio2",
  38301. + "gpio3",
  38302. + "gpio4",
  38303. + "gpio5",
  38304. + "gpio6",
  38305. + "gpio7",
  38306. + "gpio8",
  38307. + "gpio9",
  38308. + "gpio10",
  38309. + "gpio11",
  38310. + "gpio12",
  38311. + "gpio13",
  38312. + "gpio14",
  38313. + "gpio15",
  38314. + "gpio16",
  38315. + "gpio17",
  38316. + "gpio18",
  38317. + "gpio19",
  38318. + "gpio20",
  38319. + "gpio21",
  38320. + "gpio22",
  38321. + "gpio23",
  38322. + "gpio24",
  38323. + "gpio25",
  38324. + "gpio26",
  38325. + "gpio27",
  38326. + "gpio28",
  38327. + "gpio29",
  38328. + "gpio30",
  38329. + "gpio31",
  38330. + "gpio32",
  38331. + "gpio33",
  38332. + "gpio34",
  38333. + "gpio35",
  38334. + "gpio36",
  38335. + "gpio37",
  38336. + "gpio38",
  38337. + "gpio39",
  38338. + "gpio40",
  38339. + "gpio41",
  38340. + "gpio42",
  38341. + "gpio43",
  38342. + "gpio44",
  38343. + "gpio45",
  38344. + "gpio46",
  38345. + "gpio47",
  38346. + "gpio48",
  38347. + "gpio49",
  38348. + "gpio50",
  38349. + "gpio51",
  38350. + "gpio52",
  38351. + "gpio53",
  38352. +};
  38353. +
  38354. +enum bcm2708_fsel {
  38355. + BCM2708_FSEL_GPIO_IN = 0,
  38356. + BCM2708_FSEL_GPIO_OUT = 1,
  38357. + BCM2708_FSEL_ALT0 = 4,
  38358. + BCM2708_FSEL_ALT1 = 5,
  38359. + BCM2708_FSEL_ALT2 = 6,
  38360. + BCM2708_FSEL_ALT3 = 7,
  38361. + BCM2708_FSEL_ALT4 = 3,
  38362. + BCM2708_FSEL_ALT5 = 2,
  38363. + BCM2708_FSEL_COUNT = 8,
  38364. + BCM2708_FSEL_MASK = 0x7,
  38365. +};
  38366. +
  38367. +static const char * const bcm2708_functions[BCM2708_FSEL_COUNT] = {
  38368. + [BCM2708_FSEL_GPIO_IN] = "gpio_in",
  38369. + [BCM2708_FSEL_GPIO_OUT] = "gpio_out",
  38370. + [BCM2708_FSEL_ALT0] = "alt0",
  38371. + [BCM2708_FSEL_ALT1] = "alt1",
  38372. + [BCM2708_FSEL_ALT2] = "alt2",
  38373. + [BCM2708_FSEL_ALT3] = "alt3",
  38374. + [BCM2708_FSEL_ALT4] = "alt4",
  38375. + [BCM2708_FSEL_ALT5] = "alt5",
  38376. +};
  38377. +
  38378. +static const char * const irq_type_names[] = {
  38379. + [IRQ_TYPE_NONE] = "none",
  38380. + [IRQ_TYPE_EDGE_RISING] = "edge-rising",
  38381. + [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
  38382. + [IRQ_TYPE_EDGE_BOTH] = "edge-both",
  38383. + [IRQ_TYPE_LEVEL_HIGH] = "level-high",
  38384. + [IRQ_TYPE_LEVEL_LOW] = "level-low",
  38385. +};
  38386. +
  38387. +static inline u32 bcm2708_gpio_rd(struct bcm2708_pinctrl *pc, unsigned reg)
  38388. +{
  38389. + return readl(pc->base + reg);
  38390. +}
  38391. +
  38392. +static inline void bcm2708_gpio_wr(struct bcm2708_pinctrl *pc, unsigned reg,
  38393. + u32 val)
  38394. +{
  38395. + writel(val, pc->base + reg);
  38396. +}
  38397. +
  38398. +static inline int bcm2708_gpio_get_bit(struct bcm2708_pinctrl *pc, unsigned reg,
  38399. + unsigned bit)
  38400. +{
  38401. + reg += GPIO_REG_OFFSET(bit) * 4;
  38402. + return (bcm2708_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
  38403. +}
  38404. +
  38405. +/* note NOT a read/modify/write cycle */
  38406. +static inline void bcm2708_gpio_set_bit(struct bcm2708_pinctrl *pc,
  38407. + unsigned reg, unsigned bit)
  38408. +{
  38409. + reg += GPIO_REG_OFFSET(bit) * 4;
  38410. + bcm2708_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
  38411. +}
  38412. +
  38413. +static inline enum bcm2708_fsel bcm2708_pinctrl_fsel_get(
  38414. + struct bcm2708_pinctrl *pc, unsigned pin)
  38415. +{
  38416. + u32 val = bcm2708_gpio_rd(pc, FSEL_REG(pin));
  38417. + enum bcm2708_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2708_FSEL_MASK;
  38418. +
  38419. + dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
  38420. + bcm2708_functions[status]);
  38421. +
  38422. + return status;
  38423. +}
  38424. +
  38425. +static inline void bcm2708_pinctrl_fsel_set(
  38426. + struct bcm2708_pinctrl *pc, unsigned pin,
  38427. + enum bcm2708_fsel fsel)
  38428. +{
  38429. + u32 val = bcm2708_gpio_rd(pc, FSEL_REG(pin));
  38430. + enum bcm2708_fsel cur = (val >> FSEL_SHIFT(pin)) & BCM2708_FSEL_MASK;
  38431. +
  38432. + dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
  38433. + bcm2708_functions[cur]);
  38434. +
  38435. + if (cur == fsel)
  38436. + return;
  38437. +
  38438. + if (cur != BCM2708_FSEL_GPIO_IN && fsel != BCM2708_FSEL_GPIO_IN) {
  38439. + /* always transition through GPIO_IN */
  38440. + val &= ~(BCM2708_FSEL_MASK << FSEL_SHIFT(pin));
  38441. + val |= BCM2708_FSEL_GPIO_IN << FSEL_SHIFT(pin);
  38442. +
  38443. + dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
  38444. + bcm2708_functions[BCM2708_FSEL_GPIO_IN]);
  38445. + bcm2708_gpio_wr(pc, FSEL_REG(pin), val);
  38446. + }
  38447. +
  38448. + val &= ~(BCM2708_FSEL_MASK << FSEL_SHIFT(pin));
  38449. + val |= fsel << FSEL_SHIFT(pin);
  38450. +
  38451. + dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
  38452. + bcm2708_functions[fsel]);
  38453. + bcm2708_gpio_wr(pc, FSEL_REG(pin), val);
  38454. +}
  38455. +
  38456. +static int bcm2708_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  38457. +{
  38458. + return ARRAY_SIZE(bcm2708_gpio_groups);
  38459. +}
  38460. +
  38461. +static const char *bcm2708_pctl_get_group_name(struct pinctrl_dev *pctldev,
  38462. + unsigned selector)
  38463. +{
  38464. + return bcm2708_gpio_groups[selector];
  38465. +}
  38466. +
  38467. +static int bcm2708_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  38468. + unsigned selector,
  38469. + const unsigned **pins,
  38470. + unsigned *num_pins)
  38471. +{
  38472. + *pins = &bcm2708_gpio_pins[selector].number;
  38473. + *num_pins = 1;
  38474. +
  38475. + return 0;
  38476. +}
  38477. +
  38478. +static void bcm2708_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  38479. + struct seq_file *s,
  38480. + unsigned offset)
  38481. +{
  38482. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  38483. + enum bcm2708_fsel fsel = bcm2708_pinctrl_fsel_get(pc, offset);
  38484. + const char *fname = bcm2708_functions[fsel];
  38485. + int value = bcm2708_gpio_get_bit(pc, GPLEV0, offset);
  38486. + int irq = irq_find_mapping(pc->irq_domain, offset);
  38487. +
  38488. + seq_printf(s, "function %s in %s; irq %d (%s)",
  38489. + fname, value ? "hi" : "lo",
  38490. + irq, irq_type_names[pc->irq_type[offset]]);
  38491. +}
  38492. +
  38493. +static void bcm2708_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  38494. + struct pinctrl_map *maps, unsigned num_maps)
  38495. +{
  38496. + int i;
  38497. +
  38498. + for (i = 0; i < num_maps; i++)
  38499. + if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  38500. + kfree(maps[i].data.configs.configs);
  38501. +
  38502. + kfree(maps);
  38503. +}
  38504. +
  38505. +static int bcm2708_pctl_dt_node_to_map_func(struct bcm2708_pinctrl *pc,
  38506. + struct device_node *np, u32 pin, u32 fnum,
  38507. + struct pinctrl_map **maps)
  38508. +{
  38509. + struct pinctrl_map *map = *maps;
  38510. +
  38511. + if (fnum >= ARRAY_SIZE(bcm2708_functions)) {
  38512. + dev_err(pc->dev, "%s: invalid brcm,function %d\n",
  38513. + of_node_full_name(np), fnum);
  38514. + return -EINVAL;
  38515. + }
  38516. +
  38517. + map->type = PIN_MAP_TYPE_MUX_GROUP;
  38518. + map->data.mux.group = bcm2708_gpio_groups[pin];
  38519. + map->data.mux.function = bcm2708_functions[fnum];
  38520. + (*maps)++;
  38521. +
  38522. + return 0;
  38523. +}
  38524. +
  38525. +static int bcm2708_pctl_dt_node_to_map_pull(struct bcm2708_pinctrl *pc,
  38526. + struct device_node *np, u32 pin, u32 pull,
  38527. + struct pinctrl_map **maps)
  38528. +{
  38529. + struct pinctrl_map *map = *maps;
  38530. + unsigned long *configs;
  38531. +
  38532. + if (pull > 2) {
  38533. + dev_err(pc->dev, "%s: invalid brcm,pull %d\n",
  38534. + of_node_full_name(np), pull);
  38535. + return -EINVAL;
  38536. + }
  38537. +
  38538. + configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  38539. + if (!configs)
  38540. + return -ENOMEM;
  38541. + configs[0] = BCM2708_PINCONF_PACK(BCM2708_PINCONF_PARAM_PULL, pull);
  38542. +
  38543. + map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  38544. + map->data.configs.group_or_pin = bcm2708_gpio_pins[pin].name;
  38545. + map->data.configs.configs = configs;
  38546. + map->data.configs.num_configs = 1;
  38547. + (*maps)++;
  38548. +
  38549. + return 0;
  38550. +}
  38551. +
  38552. +static int bcm2708_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  38553. + struct device_node *np,
  38554. + struct pinctrl_map **map, unsigned *num_maps)
  38555. +{
  38556. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  38557. + struct property *pins, *funcs, *pulls;
  38558. + int num_pins, num_funcs, num_pulls, maps_per_pin;
  38559. + struct pinctrl_map *maps, *cur_map;
  38560. + int i, err;
  38561. + u32 pin, func, pull;
  38562. +
  38563. + pins = of_find_property(np, "brcm,pins", NULL);
  38564. + if (!pins) {
  38565. + dev_err(pc->dev, "%s: missing brcm,pins property\n",
  38566. + of_node_full_name(np));
  38567. + return -EINVAL;
  38568. + }
  38569. +
  38570. + funcs = of_find_property(np, "brcm,function", NULL);
  38571. + pulls = of_find_property(np, "brcm,pull", NULL);
  38572. +
  38573. + if (!funcs && !pulls) {
  38574. + dev_err(pc->dev,
  38575. + "%s: neither brcm,function nor brcm,pull specified\n",
  38576. + of_node_full_name(np));
  38577. + return -EINVAL;
  38578. + }
  38579. +
  38580. + num_pins = pins->length / 4;
  38581. + num_funcs = funcs ? (funcs->length / 4) : 0;
  38582. + num_pulls = pulls ? (pulls->length / 4) : 0;
  38583. +
  38584. + if (num_funcs > 1 && num_funcs != num_pins) {
  38585. + dev_err(pc->dev,
  38586. + "%s: brcm,function must have 1 or %d entries\n",
  38587. + of_node_full_name(np), num_pins);
  38588. + return -EINVAL;
  38589. + }
  38590. +
  38591. + if (num_pulls > 1 && num_pulls != num_pins) {
  38592. + dev_err(pc->dev,
  38593. + "%s: brcm,pull must have 1 or %d entries\n",
  38594. + of_node_full_name(np), num_pins);
  38595. + return -EINVAL;
  38596. + }
  38597. +
  38598. + maps_per_pin = 0;
  38599. + if (num_funcs)
  38600. + maps_per_pin++;
  38601. + if (num_pulls)
  38602. + maps_per_pin++;
  38603. + cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
  38604. + GFP_KERNEL);
  38605. + if (!maps)
  38606. + return -ENOMEM;
  38607. +
  38608. + for (i = 0; i < num_pins; i++) {
  38609. + err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  38610. + if (err)
  38611. + goto out;
  38612. + if (pin >= ARRAY_SIZE(bcm2708_gpio_pins)) {
  38613. + dev_err(pc->dev, "%s: invalid brcm,pins value %d\n",
  38614. + of_node_full_name(np), pin);
  38615. + err = -EINVAL;
  38616. + goto out;
  38617. + }
  38618. +
  38619. + if (num_funcs) {
  38620. + err = of_property_read_u32_index(np, "brcm,function",
  38621. + (num_funcs > 1) ? i : 0, &func);
  38622. + if (err)
  38623. + goto out;
  38624. + err = bcm2708_pctl_dt_node_to_map_func(pc, np, pin,
  38625. + func, &cur_map);
  38626. + if (err)
  38627. + goto out;
  38628. + }
  38629. + if (num_pulls) {
  38630. + err = of_property_read_u32_index(np, "brcm,pull",
  38631. + (num_funcs > 1) ? i : 0, &pull);
  38632. + if (err)
  38633. + goto out;
  38634. + err = bcm2708_pctl_dt_node_to_map_pull(pc, np, pin,
  38635. + pull, &cur_map);
  38636. + if (err)
  38637. + goto out;
  38638. + }
  38639. + }
  38640. +
  38641. + *map = maps;
  38642. + *num_maps = num_pins * maps_per_pin;
  38643. +
  38644. + return 0;
  38645. +
  38646. +out:
  38647. + kfree(maps);
  38648. + return err;
  38649. +}
  38650. +
  38651. +static const struct pinctrl_ops bcm2708_pctl_ops = {
  38652. + .get_groups_count = bcm2708_pctl_get_groups_count,
  38653. + .get_group_name = bcm2708_pctl_get_group_name,
  38654. + .get_group_pins = bcm2708_pctl_get_group_pins,
  38655. + .pin_dbg_show = bcm2708_pctl_pin_dbg_show,
  38656. + .dt_node_to_map = bcm2708_pctl_dt_node_to_map,
  38657. + .dt_free_map = bcm2708_pctl_dt_free_map,
  38658. +};
  38659. +
  38660. +static int bcm2708_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  38661. +{
  38662. + return BCM2708_FSEL_COUNT;
  38663. +}
  38664. +
  38665. +static const char *bcm2708_pmx_get_function_name(struct pinctrl_dev *pctldev,
  38666. + unsigned selector)
  38667. +{
  38668. + return bcm2708_functions[selector];
  38669. +}
  38670. +
  38671. +static int bcm2708_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  38672. + unsigned selector,
  38673. + const char * const **groups,
  38674. + unsigned * const num_groups)
  38675. +{
  38676. + /* every pin can do every function */
  38677. + *groups = bcm2708_gpio_groups;
  38678. + *num_groups = ARRAY_SIZE(bcm2708_gpio_groups);
  38679. +
  38680. + return 0;
  38681. +}
  38682. +
  38683. +static int bcm2708_pmx_enable(struct pinctrl_dev *pctldev,
  38684. + unsigned func_selector,
  38685. + unsigned group_selector)
  38686. +{
  38687. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  38688. +
  38689. + bcm2708_pinctrl_fsel_set(pc, group_selector, func_selector);
  38690. +
  38691. + return 0;
  38692. +}
  38693. +
  38694. +static void bcm2708_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  38695. + struct pinctrl_gpio_range *range,
  38696. + unsigned offset)
  38697. +{
  38698. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  38699. +
  38700. + /* disable by setting to GPIO_IN */
  38701. + bcm2708_pinctrl_fsel_set(pc, offset, BCM2708_FSEL_GPIO_IN);
  38702. +}
  38703. +
  38704. +static int bcm2708_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  38705. + struct pinctrl_gpio_range *range,
  38706. + unsigned offset,
  38707. + bool input)
  38708. +{
  38709. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  38710. + enum bcm2708_fsel fsel = input ?
  38711. + BCM2708_FSEL_GPIO_IN : BCM2708_FSEL_GPIO_OUT;
  38712. +
  38713. + bcm2708_pinctrl_fsel_set(pc, offset, fsel);
  38714. +
  38715. + return 0;
  38716. +}
  38717. +
  38718. +static const struct pinmux_ops bcm2708_pmx_ops = {
  38719. + .get_functions_count = bcm2708_pmx_get_functions_count,
  38720. + .get_function_name = bcm2708_pmx_get_function_name,
  38721. + .get_function_groups = bcm2708_pmx_get_function_groups,
  38722. + .enable = bcm2708_pmx_enable,
  38723. + .gpio_disable_free = bcm2708_pmx_gpio_disable_free,
  38724. + .gpio_set_direction = bcm2708_pmx_gpio_set_direction,
  38725. +};
  38726. +
  38727. +static int bcm2708_pinconf_get(struct pinctrl_dev *pctldev,
  38728. + unsigned pin, unsigned long *config)
  38729. +{
  38730. + /* No way to read back config in HW */
  38731. + return -ENOTSUPP;
  38732. +}
  38733. +
  38734. +static int bcm2708_pinconf_set(struct pinctrl_dev *pctldev,
  38735. + unsigned pin, unsigned long *configs,
  38736. + unsigned num_configs)
  38737. +{
  38738. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  38739. + enum bcm2708_pinconf_param param;
  38740. + u16 arg;
  38741. + u32 off, bit;
  38742. + int i;
  38743. +
  38744. + for (i = 0; i < num_configs; i++) {
  38745. + param = BCM2708_PINCONF_UNPACK_PARAM(configs[i]);
  38746. + arg = BCM2708_PINCONF_UNPACK_ARG(configs[i]);
  38747. +
  38748. + dev_dbg(pc->dev, "configure pin %u (%s) = %04X\n", pin, bcm2708_gpio_groups[pin], arg);
  38749. + if (param != BCM2708_PINCONF_PARAM_PULL)
  38750. + return -EINVAL;
  38751. +
  38752. + off = GPIO_REG_OFFSET(pin);
  38753. + bit = GPIO_REG_SHIFT(pin);
  38754. +
  38755. + bcm2708_gpio_wr(pc, GPPUD, arg & 3);
  38756. + /*
  38757. + * Docs say to wait 150 cycles, but not of what. We assume a
  38758. + * 1 MHz clock here, which is pretty slow...
  38759. + */
  38760. + udelay(150);
  38761. + bcm2708_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  38762. + udelay(150);
  38763. + bcm2708_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  38764. + } /* for each config */
  38765. +
  38766. + return 0;
  38767. +}
  38768. +
  38769. +static const struct pinconf_ops bcm2708_pinconf_ops = {
  38770. + .pin_config_get = bcm2708_pinconf_get,
  38771. + .pin_config_set = bcm2708_pinconf_set,
  38772. +};
  38773. +
  38774. +static struct pinctrl_desc bcm2708_pinctrl_desc = {
  38775. + .name = MODULE_NAME,
  38776. + .pins = bcm2708_gpio_pins,
  38777. + .npins = ARRAY_SIZE(bcm2708_gpio_pins),
  38778. + .pctlops = &bcm2708_pctl_ops,
  38779. + .pmxops = &bcm2708_pmx_ops,
  38780. + .confops = &bcm2708_pinconf_ops,
  38781. + .owner = THIS_MODULE,
  38782. +};
  38783. +
  38784. +static struct pinctrl_gpio_range bcm2708_pinctrl_gpio_range = {
  38785. + .name = MODULE_NAME,
  38786. + .npins = BCM2708_NUM_GPIOS,
  38787. +};
  38788. +
  38789. +/* bcm2708_gpio has base=0 */
  38790. +static int bcm2708_pinctrl_gpiochip_find(struct gpio_chip *gc, void *data)
  38791. +{
  38792. + pr_debug("%s: base = %d\n", __func__, gc->base);
  38793. + return gc->base == 0 ? 1 : 0;
  38794. +}
  38795. +
  38796. +static int bcm2708_pinctrl_probe(struct platform_device *pdev)
  38797. +{
  38798. + struct device *dev = &pdev->dev;
  38799. + struct device_node *np = dev->of_node;
  38800. + struct bcm2708_pinctrl *pc;
  38801. + struct gpio_chip *gc;
  38802. + struct resource iomem;
  38803. + int err;
  38804. + BUILD_BUG_ON(ARRAY_SIZE(bcm2708_gpio_pins) != BCM2708_NUM_GPIOS);
  38805. + BUILD_BUG_ON(ARRAY_SIZE(bcm2708_gpio_groups) != BCM2708_NUM_GPIOS);
  38806. +
  38807. + /* use gpio_chip registered by the bcm2708_gpio driver */
  38808. + gc = gpiochip_find(NULL, bcm2708_pinctrl_gpiochip_find);
  38809. + if (!gc)
  38810. + return -EPROBE_DEFER;
  38811. +
  38812. + gc->of_node = np;
  38813. + gc->of_gpio_n_cells = 2;
  38814. + gc->of_xlate = of_gpio_simple_xlate;
  38815. +
  38816. + pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  38817. + if (!pc)
  38818. + return -ENOMEM;
  38819. +
  38820. + platform_set_drvdata(pdev, pc);
  38821. + pc->dev = dev;
  38822. +
  38823. + err = of_address_to_resource(np, 0, &iomem);
  38824. + if (err) {
  38825. + dev_err(dev, "could not get IO memory\n");
  38826. + return err;
  38827. + }
  38828. +
  38829. + pc->base = devm_ioremap_resource(dev, &iomem);
  38830. + if (IS_ERR(pc->base))
  38831. + return PTR_ERR(pc->base);
  38832. +
  38833. + pc->gpio_chip = *gc;
  38834. +
  38835. + pc->pctl_dev = pinctrl_register(&bcm2708_pinctrl_desc, dev, pc);
  38836. + if (!pc->pctl_dev)
  38837. + return -EINVAL;
  38838. +
  38839. + pc->gpio_range = bcm2708_pinctrl_gpio_range;
  38840. + pc->gpio_range.base = pc->gpio_chip.base;
  38841. + pc->gpio_range.gc = &pc->gpio_chip;
  38842. + pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  38843. +
  38844. + return 0;
  38845. +}
  38846. +
  38847. +static int bcm2708_pinctrl_remove(struct platform_device *pdev)
  38848. +{
  38849. + struct bcm2708_pinctrl *pc = platform_get_drvdata(pdev);
  38850. +
  38851. + pinctrl_unregister(pc->pctl_dev);
  38852. + gpiochip_remove(&pc->gpio_chip);
  38853. +
  38854. + return 0;
  38855. +}
  38856. +
  38857. +static struct of_device_id bcm2708_pinctrl_match[] = {
  38858. + { .compatible = "brcm,bcm2708-pinctrl" },
  38859. + {}
  38860. +};
  38861. +MODULE_DEVICE_TABLE(of, bcm2708_pinctrl_match);
  38862. +
  38863. +static struct platform_driver bcm2708_pinctrl_driver = {
  38864. + .probe = bcm2708_pinctrl_probe,
  38865. + .remove = bcm2708_pinctrl_remove,
  38866. + .driver = {
  38867. + .name = MODULE_NAME,
  38868. + .owner = THIS_MODULE,
  38869. + .of_match_table = bcm2708_pinctrl_match,
  38870. + },
  38871. +};
  38872. +module_platform_driver(bcm2708_pinctrl_driver);
  38873. +
  38874. +MODULE_AUTHOR("Chris Boot, Simon Arlott, Stephen Warren, Noralf Tronnes");
  38875. +MODULE_DESCRIPTION("BCM2708 Pin control driver");
  38876. +MODULE_LICENSE("GPL");
  38877. diff -Nur linux-3.17.5/drivers/spi/Kconfig linux-rpi/drivers/spi/Kconfig
  38878. --- linux-3.17.5/drivers/spi/Kconfig 2014-12-06 17:57:59.000000000 -0600
  38879. +++ linux-rpi/drivers/spi/Kconfig 2014-12-11 14:05:38.976418001 -0600
  38880. @@ -76,7 +76,7 @@
  38881. config SPI_BCM2835
  38882. tristate "BCM2835 SPI controller"
  38883. - depends on ARCH_BCM2835 || COMPILE_TEST
  38884. + depends on ARCH_BCM2835 || ARCH_BCM2708 || COMPILE_TEST
  38885. help
  38886. This selects a driver for the Broadcom BCM2835 SPI master.
  38887. @@ -85,6 +85,14 @@
  38888. is for the regular SPI controller. Slave mode operation is not also
  38889. not supported.
  38890. +config SPI_BCM2708
  38891. + tristate "BCM2708 SPI controller driver (SPI0)"
  38892. + depends on MACH_BCM2708
  38893. + help
  38894. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  38895. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  38896. + device.
  38897. +
  38898. config SPI_BFIN5XX
  38899. tristate "SPI controller driver for ADI Blackfin5xx"
  38900. depends on BLACKFIN && !BF60x
  38901. diff -Nur linux-3.17.5/drivers/spi/Makefile linux-rpi/drivers/spi/Makefile
  38902. --- linux-3.17.5/drivers/spi/Makefile 2014-12-06 17:57:59.000000000 -0600
  38903. +++ linux-rpi/drivers/spi/Makefile 2014-12-11 14:05:38.980418001 -0600
  38904. @@ -19,6 +19,7 @@
  38905. obj-$(CONFIG_SPI_BCM63XX_HSSPI) += spi-bcm63xx-hsspi.o
  38906. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  38907. obj-$(CONFIG_SPI_ADI_V3) += spi-adi-v3.o
  38908. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  38909. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  38910. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  38911. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  38912. diff -Nur linux-3.17.5/drivers/spi/spi-bcm2708.c linux-rpi/drivers/spi/spi-bcm2708.c
  38913. --- linux-3.17.5/drivers/spi/spi-bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  38914. +++ linux-rpi/drivers/spi/spi-bcm2708.c 2014-12-11 14:05:38.980418001 -0600
  38915. @@ -0,0 +1,635 @@
  38916. +/*
  38917. + * Driver for Broadcom BCM2708 SPI Controllers
  38918. + *
  38919. + * Copyright (C) 2012 Chris Boot
  38920. + *
  38921. + * This driver is inspired by:
  38922. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  38923. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  38924. + *
  38925. + * This program is free software; you can redistribute it and/or modify
  38926. + * it under the terms of the GNU General Public License as published by
  38927. + * the Free Software Foundation; either version 2 of the License, or
  38928. + * (at your option) any later version.
  38929. + *
  38930. + * This program is distributed in the hope that it will be useful,
  38931. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38932. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38933. + * GNU General Public License for more details.
  38934. + *
  38935. + * You should have received a copy of the GNU General Public License
  38936. + * along with this program; if not, write to the Free Software
  38937. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  38938. + */
  38939. +
  38940. +#include <linux/kernel.h>
  38941. +#include <linux/module.h>
  38942. +#include <linux/spinlock.h>
  38943. +#include <linux/clk.h>
  38944. +#include <linux/err.h>
  38945. +#include <linux/platform_device.h>
  38946. +#include <linux/io.h>
  38947. +#include <linux/spi/spi.h>
  38948. +#include <linux/interrupt.h>
  38949. +#include <linux/delay.h>
  38950. +#include <linux/log2.h>
  38951. +#include <linux/sched.h>
  38952. +#include <linux/wait.h>
  38953. +
  38954. +/* SPI register offsets */
  38955. +#define SPI_CS 0x00
  38956. +#define SPI_FIFO 0x04
  38957. +#define SPI_CLK 0x08
  38958. +#define SPI_DLEN 0x0c
  38959. +#define SPI_LTOH 0x10
  38960. +#define SPI_DC 0x14
  38961. +
  38962. +/* Bitfields in CS */
  38963. +#define SPI_CS_LEN_LONG 0x02000000
  38964. +#define SPI_CS_DMA_LEN 0x01000000
  38965. +#define SPI_CS_CSPOL2 0x00800000
  38966. +#define SPI_CS_CSPOL1 0x00400000
  38967. +#define SPI_CS_CSPOL0 0x00200000
  38968. +#define SPI_CS_RXF 0x00100000
  38969. +#define SPI_CS_RXR 0x00080000
  38970. +#define SPI_CS_TXD 0x00040000
  38971. +#define SPI_CS_RXD 0x00020000
  38972. +#define SPI_CS_DONE 0x00010000
  38973. +#define SPI_CS_LEN 0x00002000
  38974. +#define SPI_CS_REN 0x00001000
  38975. +#define SPI_CS_ADCS 0x00000800
  38976. +#define SPI_CS_INTR 0x00000400
  38977. +#define SPI_CS_INTD 0x00000200
  38978. +#define SPI_CS_DMAEN 0x00000100
  38979. +#define SPI_CS_TA 0x00000080
  38980. +#define SPI_CS_CSPOL 0x00000040
  38981. +#define SPI_CS_CLEAR_RX 0x00000020
  38982. +#define SPI_CS_CLEAR_TX 0x00000010
  38983. +#define SPI_CS_CPOL 0x00000008
  38984. +#define SPI_CS_CPHA 0x00000004
  38985. +#define SPI_CS_CS_10 0x00000002
  38986. +#define SPI_CS_CS_01 0x00000001
  38987. +
  38988. +#define SPI_TIMEOUT_MS 150
  38989. +
  38990. +#define DRV_NAME "bcm2708_spi"
  38991. +
  38992. +struct bcm2708_spi {
  38993. + spinlock_t lock;
  38994. + void __iomem *base;
  38995. + int irq;
  38996. + struct clk *clk;
  38997. + bool stopping;
  38998. +
  38999. + struct list_head queue;
  39000. + struct workqueue_struct *workq;
  39001. + struct work_struct work;
  39002. + struct completion done;
  39003. +
  39004. + const u8 *tx_buf;
  39005. + u8 *rx_buf;
  39006. + int len;
  39007. +};
  39008. +
  39009. +struct bcm2708_spi_state {
  39010. + u32 cs;
  39011. + u16 cdiv;
  39012. +};
  39013. +
  39014. +/*
  39015. + * This function sets the ALT mode on the SPI pins so that we can use them with
  39016. + * the SPI hardware.
  39017. + *
  39018. + * FIXME: This is a hack. Use pinmux / pinctrl.
  39019. + */
  39020. +static void bcm2708_init_pinmode(void)
  39021. +{
  39022. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  39023. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  39024. +
  39025. + int pin;
  39026. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  39027. +
  39028. + /* SPI is on GPIO 7..11 */
  39029. + for (pin = 7; pin <= 11; pin++) {
  39030. + INP_GPIO(pin); /* set mode to GPIO input first */
  39031. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  39032. + }
  39033. +
  39034. + iounmap(gpio);
  39035. +
  39036. +#undef INP_GPIO
  39037. +#undef SET_GPIO_ALT
  39038. +}
  39039. +
  39040. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  39041. +{
  39042. + return readl(bs->base + reg);
  39043. +}
  39044. +
  39045. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  39046. +{
  39047. + writel(val, bs->base + reg);
  39048. +}
  39049. +
  39050. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  39051. +{
  39052. + u8 byte;
  39053. +
  39054. + while (len--) {
  39055. + byte = bcm2708_rd(bs, SPI_FIFO);
  39056. + if (bs->rx_buf)
  39057. + *bs->rx_buf++ = byte;
  39058. + }
  39059. +}
  39060. +
  39061. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  39062. +{
  39063. + u8 byte;
  39064. + u16 val;
  39065. +
  39066. + if (len > bs->len)
  39067. + len = bs->len;
  39068. +
  39069. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  39070. + /* LoSSI mode */
  39071. + if (unlikely(len % 2)) {
  39072. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  39073. + bs->len = 0;
  39074. + return;
  39075. + }
  39076. + while (len) {
  39077. + if (bs->tx_buf) {
  39078. + val = *(const u16 *)bs->tx_buf;
  39079. + bs->tx_buf += 2;
  39080. + } else
  39081. + val = 0;
  39082. + bcm2708_wr(bs, SPI_FIFO, val);
  39083. + bs->len -= 2;
  39084. + len -= 2;
  39085. + }
  39086. + return;
  39087. + }
  39088. +
  39089. + while (len--) {
  39090. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  39091. + bcm2708_wr(bs, SPI_FIFO, byte);
  39092. + bs->len--;
  39093. + }
  39094. +}
  39095. +
  39096. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  39097. +{
  39098. + struct spi_master *master = dev_id;
  39099. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  39100. + u32 cs;
  39101. +
  39102. + spin_lock(&bs->lock);
  39103. +
  39104. + cs = bcm2708_rd(bs, SPI_CS);
  39105. +
  39106. + if (cs & SPI_CS_DONE) {
  39107. + if (bs->len) { /* first interrupt in a transfer */
  39108. + /* fill the TX fifo with up to 16 bytes */
  39109. + bcm2708_wr_fifo(bs, 16);
  39110. + } else { /* transfer complete */
  39111. + /* disable interrupts */
  39112. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  39113. + bcm2708_wr(bs, SPI_CS, cs);
  39114. +
  39115. + /* drain RX FIFO */
  39116. + while (cs & SPI_CS_RXD) {
  39117. + bcm2708_rd_fifo(bs, 1);
  39118. + cs = bcm2708_rd(bs, SPI_CS);
  39119. + }
  39120. +
  39121. + /* wake up our bh */
  39122. + complete(&bs->done);
  39123. + }
  39124. + } else if (cs & SPI_CS_RXR) {
  39125. + /* read 12 bytes of data */
  39126. + bcm2708_rd_fifo(bs, 12);
  39127. +
  39128. + /* write up to 12 bytes */
  39129. + bcm2708_wr_fifo(bs, 12);
  39130. + }
  39131. +
  39132. + spin_unlock(&bs->lock);
  39133. +
  39134. + return IRQ_HANDLED;
  39135. +}
  39136. +
  39137. +static int bcm2708_setup_state(struct spi_master *master,
  39138. + struct device *dev, struct bcm2708_spi_state *state,
  39139. + u32 hz, u8 csel, u8 mode, u8 bpw)
  39140. +{
  39141. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  39142. + int cdiv;
  39143. + unsigned long bus_hz;
  39144. + u32 cs = 0;
  39145. +
  39146. + bus_hz = clk_get_rate(bs->clk);
  39147. +
  39148. + if (hz >= bus_hz) {
  39149. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  39150. + } else if (hz) {
  39151. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  39152. +
  39153. + /* CDIV must be a power of 2, so round up */
  39154. + cdiv = roundup_pow_of_two(cdiv);
  39155. +
  39156. + if (cdiv > 65536) {
  39157. + dev_dbg(dev,
  39158. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  39159. + hz, cdiv, bus_hz / 65536);
  39160. + return -EINVAL;
  39161. + } else if (cdiv == 65536) {
  39162. + cdiv = 0;
  39163. + } else if (cdiv == 1) {
  39164. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  39165. + }
  39166. + } else {
  39167. + cdiv = 0;
  39168. + }
  39169. +
  39170. + switch (bpw) {
  39171. + case 8:
  39172. + break;
  39173. + case 9:
  39174. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  39175. + cs |= SPI_CS_LEN;
  39176. + break;
  39177. + default:
  39178. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  39179. + bpw);
  39180. + return -EINVAL;
  39181. + }
  39182. +
  39183. + if (mode & SPI_CPOL)
  39184. + cs |= SPI_CS_CPOL;
  39185. + if (mode & SPI_CPHA)
  39186. + cs |= SPI_CS_CPHA;
  39187. +
  39188. + if (!(mode & SPI_NO_CS)) {
  39189. + if (mode & SPI_CS_HIGH) {
  39190. + cs |= SPI_CS_CSPOL;
  39191. + cs |= SPI_CS_CSPOL0 << csel;
  39192. + }
  39193. +
  39194. + cs |= csel;
  39195. + } else {
  39196. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  39197. + }
  39198. +
  39199. + if (state) {
  39200. + state->cs = cs;
  39201. + state->cdiv = cdiv;
  39202. + dev_dbg(dev, "setup: want %d Hz; "
  39203. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  39204. + "mode %u: cs 0x%08X\n",
  39205. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  39206. + }
  39207. +
  39208. + return 0;
  39209. +}
  39210. +
  39211. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  39212. + struct spi_message *msg, struct spi_transfer *xfer)
  39213. +{
  39214. + struct spi_device *spi = msg->spi;
  39215. + struct bcm2708_spi_state state, *stp;
  39216. + int ret;
  39217. + u32 cs;
  39218. +
  39219. + if (bs->stopping)
  39220. + return -ESHUTDOWN;
  39221. +
  39222. + if (xfer->bits_per_word || xfer->speed_hz) {
  39223. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  39224. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  39225. + spi->chip_select, spi->mode,
  39226. + xfer->bits_per_word ? xfer->bits_per_word :
  39227. + spi->bits_per_word);
  39228. + if (ret)
  39229. + return ret;
  39230. +
  39231. + stp = &state;
  39232. + } else {
  39233. + stp = spi->controller_state;
  39234. + }
  39235. +
  39236. + reinit_completion(&bs->done);
  39237. + bs->tx_buf = xfer->tx_buf;
  39238. + bs->rx_buf = xfer->rx_buf;
  39239. + bs->len = xfer->len;
  39240. +
  39241. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  39242. +
  39243. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  39244. + bcm2708_wr(bs, SPI_CS, cs);
  39245. +
  39246. + ret = wait_for_completion_timeout(&bs->done,
  39247. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  39248. + if (ret == 0) {
  39249. + dev_err(&spi->dev, "transfer timed out\n");
  39250. + return -ETIMEDOUT;
  39251. + }
  39252. +
  39253. + if (xfer->delay_usecs)
  39254. + udelay(xfer->delay_usecs);
  39255. +
  39256. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  39257. + xfer->cs_change) {
  39258. + /* clear TA and interrupt flags */
  39259. + bcm2708_wr(bs, SPI_CS, stp->cs);
  39260. + }
  39261. +
  39262. + msg->actual_length += (xfer->len - bs->len);
  39263. +
  39264. + return 0;
  39265. +}
  39266. +
  39267. +static void bcm2708_work(struct work_struct *work)
  39268. +{
  39269. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  39270. + unsigned long flags;
  39271. + struct spi_message *msg;
  39272. + struct spi_transfer *xfer;
  39273. + int status = 0;
  39274. +
  39275. + spin_lock_irqsave(&bs->lock, flags);
  39276. + while (!list_empty(&bs->queue)) {
  39277. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  39278. + list_del_init(&msg->queue);
  39279. + spin_unlock_irqrestore(&bs->lock, flags);
  39280. +
  39281. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  39282. + status = bcm2708_process_transfer(bs, msg, xfer);
  39283. + if (status)
  39284. + break;
  39285. + }
  39286. +
  39287. + msg->status = status;
  39288. + msg->complete(msg->context);
  39289. +
  39290. + spin_lock_irqsave(&bs->lock, flags);
  39291. + }
  39292. + spin_unlock_irqrestore(&bs->lock, flags);
  39293. +}
  39294. +
  39295. +static int bcm2708_spi_setup(struct spi_device *spi)
  39296. +{
  39297. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  39298. + struct bcm2708_spi_state *state;
  39299. + int ret;
  39300. +
  39301. + if (bs->stopping)
  39302. + return -ESHUTDOWN;
  39303. +
  39304. + if (!(spi->mode & SPI_NO_CS) &&
  39305. + (spi->chip_select > spi->master->num_chipselect)) {
  39306. + dev_dbg(&spi->dev,
  39307. + "setup: invalid chipselect %u (%u defined)\n",
  39308. + spi->chip_select, spi->master->num_chipselect);
  39309. + return -EINVAL;
  39310. + }
  39311. +
  39312. + state = spi->controller_state;
  39313. + if (!state) {
  39314. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  39315. + if (!state)
  39316. + return -ENOMEM;
  39317. +
  39318. + spi->controller_state = state;
  39319. + }
  39320. +
  39321. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  39322. + spi->max_speed_hz, spi->chip_select, spi->mode,
  39323. + spi->bits_per_word);
  39324. + if (ret < 0) {
  39325. + kfree(state);
  39326. + spi->controller_state = NULL;
  39327. + return ret;
  39328. + }
  39329. +
  39330. + dev_dbg(&spi->dev,
  39331. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  39332. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  39333. + spi->mode, state->cs, state->cdiv);
  39334. +
  39335. + return 0;
  39336. +}
  39337. +
  39338. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  39339. +{
  39340. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  39341. + struct spi_transfer *xfer;
  39342. + int ret;
  39343. + unsigned long flags;
  39344. +
  39345. + if (unlikely(list_empty(&msg->transfers)))
  39346. + return -EINVAL;
  39347. +
  39348. + if (bs->stopping)
  39349. + return -ESHUTDOWN;
  39350. +
  39351. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  39352. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  39353. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  39354. + return -EINVAL;
  39355. + }
  39356. +
  39357. + if (!xfer->bits_per_word || xfer->speed_hz)
  39358. + continue;
  39359. +
  39360. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  39361. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  39362. + spi->chip_select, spi->mode,
  39363. + xfer->bits_per_word ? xfer->bits_per_word :
  39364. + spi->bits_per_word);
  39365. + if (ret)
  39366. + return ret;
  39367. + }
  39368. +
  39369. + msg->status = -EINPROGRESS;
  39370. + msg->actual_length = 0;
  39371. +
  39372. + spin_lock_irqsave(&bs->lock, flags);
  39373. + list_add_tail(&msg->queue, &bs->queue);
  39374. + queue_work(bs->workq, &bs->work);
  39375. + spin_unlock_irqrestore(&bs->lock, flags);
  39376. +
  39377. + return 0;
  39378. +}
  39379. +
  39380. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  39381. +{
  39382. + if (spi->controller_state) {
  39383. + kfree(spi->controller_state);
  39384. + spi->controller_state = NULL;
  39385. + }
  39386. +}
  39387. +
  39388. +static int bcm2708_spi_probe(struct platform_device *pdev)
  39389. +{
  39390. + struct resource *regs;
  39391. + int irq, err = -ENOMEM;
  39392. + struct clk *clk;
  39393. + struct spi_master *master;
  39394. + struct bcm2708_spi *bs;
  39395. +
  39396. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  39397. + if (!regs) {
  39398. + dev_err(&pdev->dev, "could not get IO memory\n");
  39399. + return -ENXIO;
  39400. + }
  39401. +
  39402. + irq = platform_get_irq(pdev, 0);
  39403. + if (irq < 0) {
  39404. + dev_err(&pdev->dev, "could not get IRQ\n");
  39405. + return irq;
  39406. + }
  39407. +
  39408. + clk = clk_get(&pdev->dev, NULL);
  39409. + if (IS_ERR(clk)) {
  39410. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  39411. + return PTR_ERR(clk);
  39412. + }
  39413. +
  39414. + bcm2708_init_pinmode();
  39415. +
  39416. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  39417. + if (!master) {
  39418. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  39419. + goto out_clk_put;
  39420. + }
  39421. +
  39422. + /* the spi->mode bits understood by this driver: */
  39423. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  39424. +
  39425. + master->bus_num = pdev->id;
  39426. + master->num_chipselect = 3;
  39427. + master->setup = bcm2708_spi_setup;
  39428. + master->transfer = bcm2708_spi_transfer;
  39429. + master->cleanup = bcm2708_spi_cleanup;
  39430. + master->dev.of_node = pdev->dev.of_node;
  39431. + platform_set_drvdata(pdev, master);
  39432. +
  39433. + bs = spi_master_get_devdata(master);
  39434. +
  39435. + spin_lock_init(&bs->lock);
  39436. + INIT_LIST_HEAD(&bs->queue);
  39437. + init_completion(&bs->done);
  39438. + INIT_WORK(&bs->work, bcm2708_work);
  39439. +
  39440. + bs->base = ioremap(regs->start, resource_size(regs));
  39441. + if (!bs->base) {
  39442. + dev_err(&pdev->dev, "could not remap memory\n");
  39443. + goto out_master_put;
  39444. + }
  39445. +
  39446. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  39447. + if (!bs->workq) {
  39448. + dev_err(&pdev->dev, "could not create workqueue\n");
  39449. + goto out_iounmap;
  39450. + }
  39451. +
  39452. + bs->irq = irq;
  39453. + bs->clk = clk;
  39454. + bs->stopping = false;
  39455. +
  39456. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  39457. + master);
  39458. + if (err) {
  39459. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  39460. + goto out_workqueue;
  39461. + }
  39462. +
  39463. + /* initialise the hardware */
  39464. + clk_prepare_enable(clk);
  39465. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  39466. +
  39467. + err = spi_register_master(master);
  39468. + if (err) {
  39469. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  39470. + goto out_free_irq;
  39471. + }
  39472. +
  39473. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  39474. + (unsigned long)regs->start, irq);
  39475. +
  39476. + return 0;
  39477. +
  39478. +out_free_irq:
  39479. + free_irq(bs->irq, master);
  39480. + clk_disable_unprepare(bs->clk);
  39481. +out_workqueue:
  39482. + destroy_workqueue(bs->workq);
  39483. +out_iounmap:
  39484. + iounmap(bs->base);
  39485. +out_master_put:
  39486. + spi_master_put(master);
  39487. +out_clk_put:
  39488. + clk_put(clk);
  39489. + return err;
  39490. +}
  39491. +
  39492. +static int bcm2708_spi_remove(struct platform_device *pdev)
  39493. +{
  39494. + struct spi_master *master = platform_get_drvdata(pdev);
  39495. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  39496. +
  39497. + /* reset the hardware and block queue progress */
  39498. + spin_lock_irq(&bs->lock);
  39499. + bs->stopping = true;
  39500. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  39501. + spin_unlock_irq(&bs->lock);
  39502. +
  39503. + flush_work(&bs->work);
  39504. +
  39505. + clk_disable_unprepare(bs->clk);
  39506. + clk_put(bs->clk);
  39507. + free_irq(bs->irq, master);
  39508. + iounmap(bs->base);
  39509. +
  39510. + spi_unregister_master(master);
  39511. +
  39512. + return 0;
  39513. +}
  39514. +
  39515. +static const struct of_device_id bcm2708_spi_match[] = {
  39516. + { .compatible = "brcm,bcm2708-spi", },
  39517. + {}
  39518. +};
  39519. +MODULE_DEVICE_TABLE(of, bcm2708_spi_match);
  39520. +
  39521. +static struct platform_driver bcm2708_spi_driver = {
  39522. + .driver = {
  39523. + .name = DRV_NAME,
  39524. + .owner = THIS_MODULE,
  39525. + .of_match_table = bcm2708_spi_match,
  39526. + },
  39527. + .probe = bcm2708_spi_probe,
  39528. + .remove = bcm2708_spi_remove,
  39529. +};
  39530. +
  39531. +
  39532. +static int __init bcm2708_spi_init(void)
  39533. +{
  39534. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  39535. +}
  39536. +module_init(bcm2708_spi_init);
  39537. +
  39538. +static void __exit bcm2708_spi_exit(void)
  39539. +{
  39540. + platform_driver_unregister(&bcm2708_spi_driver);
  39541. +}
  39542. +module_exit(bcm2708_spi_exit);
  39543. +
  39544. +
  39545. +//module_platform_driver(bcm2708_spi_driver);
  39546. +
  39547. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  39548. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  39549. +MODULE_LICENSE("GPL v2");
  39550. +MODULE_ALIAS("platform:" DRV_NAME);
  39551. diff -Nur linux-3.17.5/drivers/staging/media/lirc/Kconfig linux-rpi/drivers/staging/media/lirc/Kconfig
  39552. --- linux-3.17.5/drivers/staging/media/lirc/Kconfig 2014-12-06 17:57:59.000000000 -0600
  39553. +++ linux-rpi/drivers/staging/media/lirc/Kconfig 2014-12-11 14:02:55.024418001 -0600
  39554. @@ -38,6 +38,12 @@
  39555. help
  39556. Driver for Homebrew Parallel Port Receivers
  39557. +config LIRC_RPI
  39558. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  39559. + depends on LIRC
  39560. + help
  39561. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  39562. +
  39563. config LIRC_SASEM
  39564. tristate "Sasem USB IR Remote"
  39565. depends on LIRC && USB
  39566. diff -Nur linux-3.17.5/drivers/staging/media/lirc/lirc_rpi.c linux-rpi/drivers/staging/media/lirc/lirc_rpi.c
  39567. --- linux-3.17.5/drivers/staging/media/lirc/lirc_rpi.c 1969-12-31 18:00:00.000000000 -0600
  39568. +++ linux-rpi/drivers/staging/media/lirc/lirc_rpi.c 2014-12-11 14:05:39.156418001 -0600
  39569. @@ -0,0 +1,689 @@
  39570. +/*
  39571. + * lirc_rpi.c
  39572. + *
  39573. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  39574. + * (space-lengths) (just like the lirc_serial driver does)
  39575. + * between GPIO interrupt events on the Raspberry Pi.
  39576. + * Lots of code has been taken from the lirc_serial module,
  39577. + * so I would like say thanks to the authors.
  39578. + *
  39579. + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
  39580. + * Michael Bishop <cleverca22@gmail.com>
  39581. + * This program is free software; you can redistribute it and/or modify
  39582. + * it under the terms of the GNU General Public License as published by
  39583. + * the Free Software Foundation; either version 2 of the License, or
  39584. + * (at your option) any later version.
  39585. + *
  39586. + * This program is distributed in the hope that it will be useful,
  39587. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39588. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39589. + * GNU General Public License for more details.
  39590. + *
  39591. + * You should have received a copy of the GNU General Public License
  39592. + * along with this program; if not, write to the Free Software
  39593. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39594. + */
  39595. +
  39596. +#include <linux/module.h>
  39597. +#include <linux/errno.h>
  39598. +#include <linux/interrupt.h>
  39599. +#include <linux/sched.h>
  39600. +#include <linux/kernel.h>
  39601. +#include <linux/time.h>
  39602. +#include <linux/timex.h>
  39603. +#include <linux/string.h>
  39604. +#include <linux/delay.h>
  39605. +#include <linux/platform_device.h>
  39606. +#include <linux/irq.h>
  39607. +#include <linux/spinlock.h>
  39608. +#include <media/lirc.h>
  39609. +#include <media/lirc_dev.h>
  39610. +#include <mach/gpio.h>
  39611. +#include <linux/gpio.h>
  39612. +
  39613. +#include <linux/platform_data/bcm2708.h>
  39614. +
  39615. +#define LIRC_DRIVER_NAME "lirc_rpi"
  39616. +#define RBUF_LEN 256
  39617. +#define LIRC_TRANSMITTER_LATENCY 50
  39618. +
  39619. +#ifndef MAX_UDELAY_MS
  39620. +#define MAX_UDELAY_US 5000
  39621. +#else
  39622. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  39623. +#endif
  39624. +
  39625. +#define dprintk(fmt, args...) \
  39626. + do { \
  39627. + if (debug) \
  39628. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  39629. + fmt, ## args); \
  39630. + } while (0)
  39631. +
  39632. +/* module parameters */
  39633. +
  39634. +/* set the default GPIO input pin */
  39635. +static int gpio_in_pin = 18;
  39636. +/* set the default pull behaviour for input pin */
  39637. +static int gpio_in_pull = BCM2708_PULL_DOWN;
  39638. +/* set the default GPIO output pin */
  39639. +static int gpio_out_pin = 17;
  39640. +/* enable debugging messages */
  39641. +static bool debug;
  39642. +/* -1 = auto, 0 = active high, 1 = active low */
  39643. +static int sense = -1;
  39644. +/* use softcarrier by default */
  39645. +static bool softcarrier = 1;
  39646. +/* 0 = do not invert output, 1 = invert output */
  39647. +static bool invert = 0;
  39648. +
  39649. +struct gpio_chip *gpiochip;
  39650. +struct irq_chip *irqchip;
  39651. +struct irq_data *irqdata;
  39652. +
  39653. +/* forward declarations */
  39654. +static long send_pulse(unsigned long length);
  39655. +static void send_space(long length);
  39656. +static void lirc_rpi_exit(void);
  39657. +
  39658. +static struct platform_device *lirc_rpi_dev;
  39659. +static struct timeval lasttv = { 0, 0 };
  39660. +static struct lirc_buffer rbuf;
  39661. +static spinlock_t lock;
  39662. +
  39663. +/* initialized/set in init_timing_params() */
  39664. +static unsigned int freq = 38000;
  39665. +static unsigned int duty_cycle = 50;
  39666. +static unsigned long period;
  39667. +static unsigned long pulse_width;
  39668. +static unsigned long space_width;
  39669. +
  39670. +static void safe_udelay(unsigned long usecs)
  39671. +{
  39672. + while (usecs > MAX_UDELAY_US) {
  39673. + udelay(MAX_UDELAY_US);
  39674. + usecs -= MAX_UDELAY_US;
  39675. + }
  39676. + udelay(usecs);
  39677. +}
  39678. +
  39679. +static int init_timing_params(unsigned int new_duty_cycle,
  39680. + unsigned int new_freq)
  39681. +{
  39682. + if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
  39683. + LIRC_TRANSMITTER_LATENCY)
  39684. + return -EINVAL;
  39685. + if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  39686. + LIRC_TRANSMITTER_LATENCY)
  39687. + return -EINVAL;
  39688. + duty_cycle = new_duty_cycle;
  39689. + freq = new_freq;
  39690. + period = 1000 * 1000000L / freq;
  39691. + pulse_width = period * duty_cycle / 100;
  39692. + space_width = period - pulse_width;
  39693. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  39694. + "space=%ld\n", freq, pulse_width, space_width);
  39695. + return 0;
  39696. +}
  39697. +
  39698. +static long send_pulse_softcarrier(unsigned long length)
  39699. +{
  39700. + int flag;
  39701. + unsigned long actual, target;
  39702. + unsigned long actual_us, initial_us, target_us;
  39703. +
  39704. + length *= 1000;
  39705. +
  39706. + actual = 0; target = 0; flag = 0;
  39707. + read_current_timer(&actual_us);
  39708. +
  39709. + while (actual < length) {
  39710. + if (flag) {
  39711. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  39712. + target += space_width;
  39713. + } else {
  39714. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  39715. + target += pulse_width;
  39716. + }
  39717. + initial_us = actual_us;
  39718. + target_us = actual_us + (target - actual) / 1000;
  39719. + /*
  39720. + * Note - we've checked in ioctl that the pulse/space
  39721. + * widths are big enough so that d is > 0
  39722. + */
  39723. + if ((int)(target_us - actual_us) > 0)
  39724. + udelay(target_us - actual_us);
  39725. + read_current_timer(&actual_us);
  39726. + actual += (actual_us - initial_us) * 1000;
  39727. + flag = !flag;
  39728. + }
  39729. + return (actual-length) / 1000;
  39730. +}
  39731. +
  39732. +static long send_pulse(unsigned long length)
  39733. +{
  39734. + if (length <= 0)
  39735. + return 0;
  39736. +
  39737. + if (softcarrier) {
  39738. + return send_pulse_softcarrier(length);
  39739. + } else {
  39740. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  39741. + safe_udelay(length);
  39742. + return 0;
  39743. + }
  39744. +}
  39745. +
  39746. +static void send_space(long length)
  39747. +{
  39748. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  39749. + if (length <= 0)
  39750. + return;
  39751. + safe_udelay(length);
  39752. +}
  39753. +
  39754. +static void rbwrite(int l)
  39755. +{
  39756. + if (lirc_buffer_full(&rbuf)) {
  39757. + /* no new signals will be accepted */
  39758. + dprintk("Buffer overrun\n");
  39759. + return;
  39760. + }
  39761. + lirc_buffer_write(&rbuf, (void *)&l);
  39762. +}
  39763. +
  39764. +static void frbwrite(int l)
  39765. +{
  39766. + /* simple noise filter */
  39767. + static int pulse, space;
  39768. + static unsigned int ptr;
  39769. +
  39770. + if (ptr > 0 && (l & PULSE_BIT)) {
  39771. + pulse += l & PULSE_MASK;
  39772. + if (pulse > 250) {
  39773. + rbwrite(space);
  39774. + rbwrite(pulse | PULSE_BIT);
  39775. + ptr = 0;
  39776. + pulse = 0;
  39777. + }
  39778. + return;
  39779. + }
  39780. + if (!(l & PULSE_BIT)) {
  39781. + if (ptr == 0) {
  39782. + if (l > 20000) {
  39783. + space = l;
  39784. + ptr++;
  39785. + return;
  39786. + }
  39787. + } else {
  39788. + if (l > 20000) {
  39789. + space += pulse;
  39790. + if (space > PULSE_MASK)
  39791. + space = PULSE_MASK;
  39792. + space += l;
  39793. + if (space > PULSE_MASK)
  39794. + space = PULSE_MASK;
  39795. + pulse = 0;
  39796. + return;
  39797. + }
  39798. + rbwrite(space);
  39799. + rbwrite(pulse | PULSE_BIT);
  39800. + ptr = 0;
  39801. + pulse = 0;
  39802. + }
  39803. + }
  39804. + rbwrite(l);
  39805. +}
  39806. +
  39807. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  39808. +{
  39809. + struct timeval tv;
  39810. + long deltv;
  39811. + int data;
  39812. + int signal;
  39813. +
  39814. + /* use the GPIO signal level */
  39815. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  39816. +
  39817. + /* unmask the irq */
  39818. + irqchip->irq_unmask(irqdata);
  39819. +
  39820. + if (sense != -1) {
  39821. + /* get current time */
  39822. + do_gettimeofday(&tv);
  39823. +
  39824. + /* calc time since last interrupt in microseconds */
  39825. + deltv = tv.tv_sec-lasttv.tv_sec;
  39826. + if (tv.tv_sec < lasttv.tv_sec ||
  39827. + (tv.tv_sec == lasttv.tv_sec &&
  39828. + tv.tv_usec < lasttv.tv_usec)) {
  39829. + printk(KERN_WARNING LIRC_DRIVER_NAME
  39830. + ": AIEEEE: your clock just jumped backwards\n");
  39831. + printk(KERN_WARNING LIRC_DRIVER_NAME
  39832. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  39833. + tv.tv_sec, lasttv.tv_sec,
  39834. + tv.tv_usec, lasttv.tv_usec);
  39835. + data = PULSE_MASK;
  39836. + } else if (deltv > 15) {
  39837. + data = PULSE_MASK; /* really long time */
  39838. + if (!(signal^sense)) {
  39839. + /* sanity check */
  39840. + printk(KERN_WARNING LIRC_DRIVER_NAME
  39841. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  39842. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  39843. + tv.tv_usec, lasttv.tv_usec);
  39844. + /*
  39845. + * detecting pulse while this
  39846. + * MUST be a space!
  39847. + */
  39848. + sense = sense ? 0 : 1;
  39849. + }
  39850. + } else {
  39851. + data = (int) (deltv*1000000 +
  39852. + (tv.tv_usec - lasttv.tv_usec));
  39853. + }
  39854. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  39855. + lasttv = tv;
  39856. + wake_up_interruptible(&rbuf.wait_poll);
  39857. + }
  39858. +
  39859. + return IRQ_HANDLED;
  39860. +}
  39861. +
  39862. +static int is_right_chip(struct gpio_chip *chip, void *data)
  39863. +{
  39864. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  39865. +
  39866. + if (strcmp(data, chip->label) == 0)
  39867. + return 1;
  39868. + return 0;
  39869. +}
  39870. +
  39871. +static int init_port(void)
  39872. +{
  39873. + int i, nlow, nhigh, ret, irq;
  39874. +
  39875. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  39876. +
  39877. + if (!gpiochip)
  39878. + return -ENODEV;
  39879. +
  39880. + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
  39881. + printk(KERN_ALERT LIRC_DRIVER_NAME
  39882. + ": cant claim gpio pin %d\n", gpio_out_pin);
  39883. + ret = -ENODEV;
  39884. + goto exit_init_port;
  39885. + }
  39886. +
  39887. + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
  39888. + printk(KERN_ALERT LIRC_DRIVER_NAME
  39889. + ": cant claim gpio pin %d\n", gpio_in_pin);
  39890. + ret = -ENODEV;
  39891. + goto exit_gpio_free_out_pin;
  39892. + }
  39893. +
  39894. + bcm2708_gpio_setpull(gpiochip, gpio_in_pin, gpio_in_pull);
  39895. + gpiochip->direction_input(gpiochip, gpio_in_pin);
  39896. + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
  39897. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  39898. +
  39899. + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
  39900. + dprintk("to_irq %d\n", irq);
  39901. + irqdata = irq_get_irq_data(irq);
  39902. +
  39903. + if (irqdata && irqdata->chip) {
  39904. + irqchip = irqdata->chip;
  39905. + } else {
  39906. + ret = -ENODEV;
  39907. + goto exit_gpio_free_in_pin;
  39908. + }
  39909. +
  39910. + /* if pin is high, then this must be an active low receiver. */
  39911. + if (sense == -1) {
  39912. + /* wait 1/2 sec for the power supply */
  39913. + msleep(500);
  39914. +
  39915. + /*
  39916. + * probe 9 times every 0.04s, collect "votes" for
  39917. + * active high/low
  39918. + */
  39919. + nlow = 0;
  39920. + nhigh = 0;
  39921. + for (i = 0; i < 9; i++) {
  39922. + if (gpiochip->get(gpiochip, gpio_in_pin))
  39923. + nlow++;
  39924. + else
  39925. + nhigh++;
  39926. + msleep(40);
  39927. + }
  39928. + sense = (nlow >= nhigh ? 1 : 0);
  39929. + printk(KERN_INFO LIRC_DRIVER_NAME
  39930. + ": auto-detected active %s receiver on GPIO pin %d\n",
  39931. + sense ? "low" : "high", gpio_in_pin);
  39932. + } else {
  39933. + printk(KERN_INFO LIRC_DRIVER_NAME
  39934. + ": manually using active %s receiver on GPIO pin %d\n",
  39935. + sense ? "low" : "high", gpio_in_pin);
  39936. + }
  39937. +
  39938. + return 0;
  39939. +
  39940. + exit_gpio_free_in_pin:
  39941. + gpio_free(gpio_in_pin);
  39942. +
  39943. + exit_gpio_free_out_pin:
  39944. + gpio_free(gpio_out_pin);
  39945. +
  39946. + exit_init_port:
  39947. + return ret;
  39948. +}
  39949. +
  39950. +// called when the character device is opened
  39951. +static int set_use_inc(void *data)
  39952. +{
  39953. + int result;
  39954. + unsigned long flags;
  39955. +
  39956. + /* initialize timestamp */
  39957. + do_gettimeofday(&lasttv);
  39958. +
  39959. + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
  39960. + (irq_handler_t) irq_handler, 0,
  39961. + LIRC_DRIVER_NAME, (void*) 0);
  39962. +
  39963. + switch (result) {
  39964. + case -EBUSY:
  39965. + printk(KERN_ERR LIRC_DRIVER_NAME
  39966. + ": IRQ %d is busy\n",
  39967. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  39968. + return -EBUSY;
  39969. + case -EINVAL:
  39970. + printk(KERN_ERR LIRC_DRIVER_NAME
  39971. + ": Bad irq number or handler\n");
  39972. + return -EINVAL;
  39973. + default:
  39974. + dprintk("Interrupt %d obtained\n",
  39975. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  39976. + break;
  39977. + };
  39978. +
  39979. + /* initialize pulse/space widths */
  39980. + init_timing_params(duty_cycle, freq);
  39981. +
  39982. + spin_lock_irqsave(&lock, flags);
  39983. +
  39984. + /* GPIO Pin Falling/Rising Edge Detect Enable */
  39985. + irqchip->irq_set_type(irqdata,
  39986. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
  39987. +
  39988. + /* unmask the irq */
  39989. + irqchip->irq_unmask(irqdata);
  39990. +
  39991. + spin_unlock_irqrestore(&lock, flags);
  39992. +
  39993. + return 0;
  39994. +}
  39995. +
  39996. +static void set_use_dec(void *data)
  39997. +{
  39998. + unsigned long flags;
  39999. +
  40000. + spin_lock_irqsave(&lock, flags);
  40001. +
  40002. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  40003. + irqchip->irq_set_type(irqdata, 0);
  40004. + irqchip->irq_mask(irqdata);
  40005. +
  40006. + spin_unlock_irqrestore(&lock, flags);
  40007. +
  40008. + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
  40009. +
  40010. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  40011. + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
  40012. +}
  40013. +
  40014. +static ssize_t lirc_write(struct file *file, const char *buf,
  40015. + size_t n, loff_t *ppos)
  40016. +{
  40017. + int i, count;
  40018. + unsigned long flags;
  40019. + long delta = 0;
  40020. + int *wbuf;
  40021. +
  40022. + count = n / sizeof(int);
  40023. + if (n % sizeof(int) || count % 2 == 0)
  40024. + return -EINVAL;
  40025. + wbuf = memdup_user(buf, n);
  40026. + if (IS_ERR(wbuf))
  40027. + return PTR_ERR(wbuf);
  40028. + spin_lock_irqsave(&lock, flags);
  40029. +
  40030. + for (i = 0; i < count; i++) {
  40031. + if (i%2)
  40032. + send_space(wbuf[i] - delta);
  40033. + else
  40034. + delta = send_pulse(wbuf[i]);
  40035. + }
  40036. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  40037. +
  40038. + spin_unlock_irqrestore(&lock, flags);
  40039. + kfree(wbuf);
  40040. + return n;
  40041. +}
  40042. +
  40043. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  40044. +{
  40045. + int result;
  40046. + __u32 value;
  40047. +
  40048. + switch (cmd) {
  40049. + case LIRC_GET_SEND_MODE:
  40050. + return -ENOIOCTLCMD;
  40051. + break;
  40052. +
  40053. + case LIRC_SET_SEND_MODE:
  40054. + result = get_user(value, (__u32 *) arg);
  40055. + if (result)
  40056. + return result;
  40057. + /* only LIRC_MODE_PULSE supported */
  40058. + if (value != LIRC_MODE_PULSE)
  40059. + return -ENOSYS;
  40060. + break;
  40061. +
  40062. + case LIRC_GET_LENGTH:
  40063. + return -ENOSYS;
  40064. + break;
  40065. +
  40066. + case LIRC_SET_SEND_DUTY_CYCLE:
  40067. + dprintk("SET_SEND_DUTY_CYCLE\n");
  40068. + result = get_user(value, (__u32 *) arg);
  40069. + if (result)
  40070. + return result;
  40071. + if (value <= 0 || value > 100)
  40072. + return -EINVAL;
  40073. + return init_timing_params(value, freq);
  40074. + break;
  40075. +
  40076. + case LIRC_SET_SEND_CARRIER:
  40077. + dprintk("SET_SEND_CARRIER\n");
  40078. + result = get_user(value, (__u32 *) arg);
  40079. + if (result)
  40080. + return result;
  40081. + if (value > 500000 || value < 20000)
  40082. + return -EINVAL;
  40083. + return init_timing_params(duty_cycle, value);
  40084. + break;
  40085. +
  40086. + default:
  40087. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  40088. + }
  40089. + return 0;
  40090. +}
  40091. +
  40092. +static const struct file_operations lirc_fops = {
  40093. + .owner = THIS_MODULE,
  40094. + .write = lirc_write,
  40095. + .unlocked_ioctl = lirc_ioctl,
  40096. + .read = lirc_dev_fop_read,
  40097. + .poll = lirc_dev_fop_poll,
  40098. + .open = lirc_dev_fop_open,
  40099. + .release = lirc_dev_fop_close,
  40100. + .llseek = no_llseek,
  40101. +};
  40102. +
  40103. +static struct lirc_driver driver = {
  40104. + .name = LIRC_DRIVER_NAME,
  40105. + .minor = -1,
  40106. + .code_length = 1,
  40107. + .sample_rate = 0,
  40108. + .data = NULL,
  40109. + .add_to_buf = NULL,
  40110. + .rbuf = &rbuf,
  40111. + .set_use_inc = set_use_inc,
  40112. + .set_use_dec = set_use_dec,
  40113. + .fops = &lirc_fops,
  40114. + .dev = NULL,
  40115. + .owner = THIS_MODULE,
  40116. +};
  40117. +
  40118. +static struct platform_driver lirc_rpi_driver = {
  40119. + .driver = {
  40120. + .name = LIRC_DRIVER_NAME,
  40121. + .owner = THIS_MODULE,
  40122. + },
  40123. +};
  40124. +
  40125. +static int __init lirc_rpi_init(void)
  40126. +{
  40127. + int result;
  40128. +
  40129. + /* Init read buffer. */
  40130. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  40131. + if (result < 0)
  40132. + return -ENOMEM;
  40133. +
  40134. + result = platform_driver_register(&lirc_rpi_driver);
  40135. + if (result) {
  40136. + printk(KERN_ERR LIRC_DRIVER_NAME
  40137. + ": lirc register returned %d\n", result);
  40138. + goto exit_buffer_free;
  40139. + }
  40140. +
  40141. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  40142. + if (!lirc_rpi_dev) {
  40143. + result = -ENOMEM;
  40144. + goto exit_driver_unregister;
  40145. + }
  40146. +
  40147. + result = platform_device_add(lirc_rpi_dev);
  40148. + if (result)
  40149. + goto exit_device_put;
  40150. +
  40151. + return 0;
  40152. +
  40153. + exit_device_put:
  40154. + platform_device_put(lirc_rpi_dev);
  40155. +
  40156. + exit_driver_unregister:
  40157. + platform_driver_unregister(&lirc_rpi_driver);
  40158. +
  40159. + exit_buffer_free:
  40160. + lirc_buffer_free(&rbuf);
  40161. +
  40162. + return result;
  40163. +}
  40164. +
  40165. +static void lirc_rpi_exit(void)
  40166. +{
  40167. + platform_device_unregister(lirc_rpi_dev);
  40168. + platform_driver_unregister(&lirc_rpi_driver);
  40169. + lirc_buffer_free(&rbuf);
  40170. +}
  40171. +
  40172. +static int __init lirc_rpi_init_module(void)
  40173. +{
  40174. + int result;
  40175. +
  40176. + result = lirc_rpi_init();
  40177. + if (result)
  40178. + return result;
  40179. +
  40180. + if (gpio_in_pin >= BCM2708_NR_GPIOS || gpio_out_pin >= BCM2708_NR_GPIOS) {
  40181. + result = -EINVAL;
  40182. + printk(KERN_ERR LIRC_DRIVER_NAME
  40183. + ": invalid GPIO pin(s) specified!\n");
  40184. + goto exit_rpi;
  40185. + }
  40186. +
  40187. + result = init_port();
  40188. + if (result < 0)
  40189. + goto exit_rpi;
  40190. +
  40191. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  40192. + LIRC_CAN_SET_SEND_CARRIER |
  40193. + LIRC_CAN_SEND_PULSE |
  40194. + LIRC_CAN_REC_MODE2;
  40195. +
  40196. + driver.dev = &lirc_rpi_dev->dev;
  40197. + driver.minor = lirc_register_driver(&driver);
  40198. +
  40199. + if (driver.minor < 0) {
  40200. + printk(KERN_ERR LIRC_DRIVER_NAME
  40201. + ": device registration failed with %d\n", result);
  40202. + result = -EIO;
  40203. + goto exit_rpi;
  40204. + }
  40205. +
  40206. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  40207. +
  40208. + return 0;
  40209. +
  40210. + exit_rpi:
  40211. + lirc_rpi_exit();
  40212. +
  40213. + return result;
  40214. +}
  40215. +
  40216. +static void __exit lirc_rpi_exit_module(void)
  40217. +{
  40218. + gpio_free(gpio_out_pin);
  40219. + gpio_free(gpio_in_pin);
  40220. +
  40221. + lirc_rpi_exit();
  40222. +
  40223. + lirc_unregister_driver(driver.minor);
  40224. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  40225. +}
  40226. +
  40227. +module_init(lirc_rpi_init_module);
  40228. +module_exit(lirc_rpi_exit_module);
  40229. +
  40230. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  40231. +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
  40232. +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
  40233. +MODULE_LICENSE("GPL");
  40234. +
  40235. +module_param(gpio_out_pin, int, S_IRUGO);
  40236. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  40237. + " processor. (default 17");
  40238. +
  40239. +module_param(gpio_in_pin, int, S_IRUGO);
  40240. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  40241. + " (default 18");
  40242. +
  40243. +module_param(gpio_in_pull, int, S_IRUGO);
  40244. +MODULE_PARM_DESC(gpio_in_pull, "GPIO input pin pull configuration."
  40245. + " (0 = off, 1 = up, 2 = down, default down)");
  40246. +
  40247. +module_param(sense, int, S_IRUGO);
  40248. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  40249. + " (0 = active high, 1 = active low )");
  40250. +
  40251. +module_param(softcarrier, bool, S_IRUGO);
  40252. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  40253. +
  40254. +module_param(invert, bool, S_IRUGO);
  40255. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  40256. +
  40257. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  40258. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  40259. diff -Nur linux-3.17.5/drivers/staging/media/lirc/Makefile linux-rpi/drivers/staging/media/lirc/Makefile
  40260. --- linux-3.17.5/drivers/staging/media/lirc/Makefile 2014-12-06 17:57:59.000000000 -0600
  40261. +++ linux-rpi/drivers/staging/media/lirc/Makefile 2014-12-11 14:02:55.024418001 -0600
  40262. @@ -7,6 +7,7 @@
  40263. obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
  40264. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  40265. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  40266. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  40267. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  40268. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  40269. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  40270. diff -Nur linux-3.17.5/drivers/thermal/bcm2835-thermal.c linux-rpi/drivers/thermal/bcm2835-thermal.c
  40271. --- linux-3.17.5/drivers/thermal/bcm2835-thermal.c 1969-12-31 18:00:00.000000000 -0600
  40272. +++ linux-rpi/drivers/thermal/bcm2835-thermal.c 2014-12-11 14:02:55.244418001 -0600
  40273. @@ -0,0 +1,184 @@
  40274. +/*****************************************************************************
  40275. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  40276. +*
  40277. +* Unless you and Broadcom execute a separate written software license
  40278. +* agreement governing use of this software, this software is licensed to you
  40279. +* under the terms of the GNU General Public License version 2, available at
  40280. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  40281. +*
  40282. +* Notwithstanding the above, under no circumstances may you combine this
  40283. +* software in any way with any other Broadcom software provided under a
  40284. +* license other than the GPL, without Broadcom's express prior written
  40285. +* consent.
  40286. +*****************************************************************************/
  40287. +
  40288. +#include <linux/kernel.h>
  40289. +#include <linux/module.h>
  40290. +#include <linux/init.h>
  40291. +#include <linux/platform_device.h>
  40292. +#include <linux/slab.h>
  40293. +#include <linux/sysfs.h>
  40294. +#include <mach/vcio.h>
  40295. +#include <linux/thermal.h>
  40296. +
  40297. +
  40298. +/* --- DEFINITIONS --- */
  40299. +#define MODULE_NAME "bcm2835_thermal"
  40300. +
  40301. +/*#define THERMAL_DEBUG_ENABLE*/
  40302. +
  40303. +#ifdef THERMAL_DEBUG_ENABLE
  40304. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  40305. +#else
  40306. +#define print_debug(fmt,...)
  40307. +#endif
  40308. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  40309. +
  40310. +#define VC_TAG_GET_TEMP 0x00030006
  40311. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  40312. +
  40313. +typedef enum {
  40314. + TEMP,
  40315. + MAX_TEMP,
  40316. +} temp_type;
  40317. +
  40318. +/* --- STRUCTS --- */
  40319. +/* tag part of the message */
  40320. +struct vc_msg_tag {
  40321. + uint32_t tag_id; /* the tag ID for the temperature */
  40322. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  40323. + uint32_t request_code; /* identifies message as a request (should be 0) */
  40324. + uint32_t id; /* extra ID field (should be 0) */
  40325. + uint32_t val; /* returned value of the temperature */
  40326. +};
  40327. +
  40328. +/* message structure to be sent to videocore */
  40329. +struct vc_msg {
  40330. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  40331. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  40332. + struct vc_msg_tag tag; /* the tag structure above to make */
  40333. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  40334. +};
  40335. +
  40336. +struct bcm2835_thermal_data {
  40337. + struct thermal_zone_device *thermal_dev;
  40338. + struct vc_msg msg;
  40339. +};
  40340. +
  40341. +/* --- GLOBALS --- */
  40342. +static struct bcm2835_thermal_data bcm2835_data;
  40343. +
  40344. +/* Thermal Device Operations */
  40345. +static struct thermal_zone_device_ops ops;
  40346. +
  40347. +/* --- FUNCTIONS --- */
  40348. +
  40349. +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
  40350. +{
  40351. + int result = -1, retry = 3;
  40352. + print_debug("IN");
  40353. +
  40354. + *temp = 0;
  40355. + while (result != 0 && retry-- > 0) {
  40356. + /* wipe all previous message data */
  40357. + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
  40358. +
  40359. + /* prepare message */
  40360. + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
  40361. + bcm2835_data.msg.tag.buffer_size = 8;
  40362. + bcm2835_data.msg.tag.tag_id = tag_id;
  40363. +
  40364. + /* send the message */
  40365. + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
  40366. + 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);
  40367. + if (!(bcm2835_data.msg.request_code & 0x80000000))
  40368. + result = -1;
  40369. + }
  40370. +
  40371. + /* check if it was all ok and return the rate in milli degrees C */
  40372. + if (result == 0)
  40373. + *temp = (uint)bcm2835_data.msg.tag.val;
  40374. + else
  40375. + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
  40376. + print_debug("OUT");
  40377. + return result;
  40378. +}
  40379. +
  40380. +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
  40381. +{
  40382. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
  40383. +}
  40384. +
  40385. +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
  40386. +{
  40387. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
  40388. +}
  40389. +
  40390. +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
  40391. +{
  40392. + *trip_type = THERMAL_TRIP_HOT;
  40393. + return 0;
  40394. +}
  40395. +
  40396. +
  40397. +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
  40398. +{
  40399. + *dev_mode = THERMAL_DEVICE_ENABLED;
  40400. + return 0;
  40401. +}
  40402. +
  40403. +
  40404. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  40405. +{
  40406. + print_debug("IN");
  40407. + print_debug("THERMAL Driver has been probed!");
  40408. +
  40409. + /* check that the device isn't null!*/
  40410. + if(pdev == NULL)
  40411. + {
  40412. + print_debug("Platform device is empty!");
  40413. + return -ENODEV;
  40414. + }
  40415. +
  40416. + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
  40417. + {
  40418. + print_debug("Unable to register the thermal device!");
  40419. + return -EFAULT;
  40420. + }
  40421. + return 0;
  40422. +}
  40423. +
  40424. +
  40425. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  40426. +{
  40427. + print_debug("IN");
  40428. +
  40429. + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
  40430. +
  40431. + print_debug("OUT");
  40432. +
  40433. + return 0;
  40434. +}
  40435. +
  40436. +static struct thermal_zone_device_ops ops = {
  40437. + .get_temp = bcm2835_get_temp,
  40438. + .get_trip_temp = bcm2835_get_max_temp,
  40439. + .get_trip_type = bcm2835_get_trip_type,
  40440. + .get_mode = bcm2835_get_mode,
  40441. +};
  40442. +
  40443. +/* Thermal Driver */
  40444. +static struct platform_driver bcm2835_thermal_driver = {
  40445. + .probe = bcm2835_thermal_probe,
  40446. + .remove = bcm2835_thermal_remove,
  40447. + .driver = {
  40448. + .name = "bcm2835_thermal",
  40449. + .owner = THIS_MODULE,
  40450. + },
  40451. +};
  40452. +
  40453. +MODULE_LICENSE("GPL");
  40454. +MODULE_AUTHOR("Dorian Peake");
  40455. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  40456. +
  40457. +module_platform_driver(bcm2835_thermal_driver);
  40458. diff -Nur linux-3.17.5/drivers/thermal/Kconfig linux-rpi/drivers/thermal/Kconfig
  40459. --- linux-3.17.5/drivers/thermal/Kconfig 2014-12-06 17:57:59.000000000 -0600
  40460. +++ linux-rpi/drivers/thermal/Kconfig 2014-12-11 14:05:39.436418001 -0600
  40461. @@ -196,6 +196,12 @@
  40462. enforce idle time which results in more package C-state residency. The
  40463. user interface is exposed via generic thermal framework.
  40464. +config THERMAL_BCM2835
  40465. + tristate "BCM2835 Thermal Driver"
  40466. + help
  40467. + This will enable temperature monitoring for the Broadcom BCM2835
  40468. + chip. If built as a module, it will be called 'bcm2835-thermal'.
  40469. +
  40470. config X86_PKG_TEMP_THERMAL
  40471. tristate "X86 package temperature thermal driver"
  40472. depends on X86_THERMAL_VECTOR
  40473. diff -Nur linux-3.17.5/drivers/thermal/Makefile linux-rpi/drivers/thermal/Makefile
  40474. --- linux-3.17.5/drivers/thermal/Makefile 2014-12-06 17:57:59.000000000 -0600
  40475. +++ linux-rpi/drivers/thermal/Makefile 2014-12-11 14:05:39.436418001 -0600
  40476. @@ -28,6 +28,7 @@
  40477. obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
  40478. obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
  40479. obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
  40480. +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
  40481. obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
  40482. obj-$(CONFIG_INTEL_SOC_DTS_THERMAL) += intel_soc_dts_thermal.o
  40483. obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
  40484. diff -Nur linux-3.17.5/drivers/tty/serial/amba-pl011.c linux-rpi/drivers/tty/serial/amba-pl011.c
  40485. --- linux-3.17.5/drivers/tty/serial/amba-pl011.c 2014-12-06 17:57:59.000000000 -0600
  40486. +++ linux-rpi/drivers/tty/serial/amba-pl011.c 2014-12-11 14:05:39.460418001 -0600
  40487. @@ -84,7 +84,7 @@
  40488. static unsigned int get_fifosize_arm(struct amba_device *dev)
  40489. {
  40490. - return amba_rev(dev) < 3 ? 16 : 32;
  40491. + return 16; //TODO: fix: amba_rev(dev) < 3 ? 16 : 32;
  40492. }
  40493. static struct vendor_data vendor_arm = {
  40494. diff -Nur linux-3.17.5/drivers/usb/core/generic.c linux-rpi/drivers/usb/core/generic.c
  40495. --- linux-3.17.5/drivers/usb/core/generic.c 2014-12-06 17:57:59.000000000 -0600
  40496. +++ linux-rpi/drivers/usb/core/generic.c 2014-12-11 14:05:39.488418001 -0600
  40497. @@ -152,6 +152,7 @@
  40498. dev_warn(&udev->dev,
  40499. "no configuration chosen from %d choice%s\n",
  40500. num_configs, plural(num_configs));
  40501. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  40502. }
  40503. return i;
  40504. }
  40505. diff -Nur linux-3.17.5/drivers/usb/core/hub.c linux-rpi/drivers/usb/core/hub.c
  40506. --- linux-3.17.5/drivers/usb/core/hub.c 2014-12-06 17:57:59.000000000 -0600
  40507. +++ linux-rpi/drivers/usb/core/hub.c 2014-12-11 14:05:39.492418001 -0600
  40508. @@ -4918,7 +4918,7 @@
  40509. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  40510. u16 status = 0, unused;
  40511. - dev_dbg(&port_dev->dev, "over-current change\n");
  40512. + dev_notice(&port_dev->dev, "over-current change\n");
  40513. usb_clear_port_feature(hdev, port1,
  40514. USB_PORT_FEAT_C_OVER_CURRENT);
  40515. msleep(100); /* Cool down */
  40516. diff -Nur linux-3.17.5/drivers/usb/core/message.c linux-rpi/drivers/usb/core/message.c
  40517. --- linux-3.17.5/drivers/usb/core/message.c 2014-12-06 17:57:59.000000000 -0600
  40518. +++ linux-rpi/drivers/usb/core/message.c 2014-12-11 14:05:39.492418001 -0600
  40519. @@ -1891,6 +1891,85 @@
  40520. if (cp->string == NULL &&
  40521. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  40522. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  40523. +/* Uncomment this define to enable the HS Electrical Test support */
  40524. +#define DWC_HS_ELECT_TST 1
  40525. +#ifdef DWC_HS_ELECT_TST
  40526. + /* Here we implement the HS Electrical Test support. The
  40527. + * tester uses a vendor ID of 0x1A0A to indicate we should
  40528. + * run a special test sequence. The product ID tells us
  40529. + * which sequence to run. We invoke the test sequence by
  40530. + * sending a non-standard SetFeature command to our root
  40531. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  40532. + * recognize the command and perform the desired test
  40533. + * sequence.
  40534. + */
  40535. + if (dev->descriptor.idVendor == 0x1A0A) {
  40536. + /* HSOTG Electrical Test */
  40537. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  40538. +
  40539. + if (dev->bus && dev->bus->root_hub) {
  40540. + struct usb_device *hdev = dev->bus->root_hub;
  40541. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  40542. +
  40543. + switch (dev->descriptor.idProduct) {
  40544. + case 0x0101: /* TEST_SE0_NAK */
  40545. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  40546. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40547. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40548. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  40549. + break;
  40550. +
  40551. + case 0x0102: /* TEST_J */
  40552. + dev_warn(&dev->dev, "TEST_J\n");
  40553. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40554. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40555. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  40556. + break;
  40557. +
  40558. + case 0x0103: /* TEST_K */
  40559. + dev_warn(&dev->dev, "TEST_K\n");
  40560. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40561. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40562. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  40563. + break;
  40564. +
  40565. + case 0x0104: /* TEST_PACKET */
  40566. + dev_warn(&dev->dev, "TEST_PACKET\n");
  40567. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40568. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40569. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  40570. + break;
  40571. +
  40572. + case 0x0105: /* TEST_FORCE_ENABLE */
  40573. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  40574. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40575. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40576. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  40577. + break;
  40578. +
  40579. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  40580. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  40581. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40582. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40583. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  40584. + break;
  40585. +
  40586. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  40587. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  40588. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40589. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40590. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  40591. + break;
  40592. +
  40593. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  40594. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  40595. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  40596. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  40597. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  40598. + }
  40599. + }
  40600. + }
  40601. +#endif /* DWC_HS_ELECT_TST */
  40602. /* Now that the interfaces are installed, re-enable LPM. */
  40603. usb_unlocked_enable_lpm(dev);
  40604. diff -Nur linux-3.17.5/drivers/usb/core/otg_whitelist.h linux-rpi/drivers/usb/core/otg_whitelist.h
  40605. --- linux-3.17.5/drivers/usb/core/otg_whitelist.h 2014-12-06 17:57:59.000000000 -0600
  40606. +++ linux-rpi/drivers/usb/core/otg_whitelist.h 2014-12-11 14:02:55.304418001 -0600
  40607. @@ -19,33 +19,82 @@
  40608. static struct usb_device_id whitelist_table [] = {
  40609. /* hubs are optional in OTG, but very handy ... */
  40610. +#define CERT_WITHOUT_HUBS
  40611. +#if defined(CERT_WITHOUT_HUBS)
  40612. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  40613. +#else
  40614. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  40615. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  40616. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  40617. +#endif
  40618. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  40619. /* FIXME actually, printers are NOT supposed to use device classes;
  40620. * they're supposed to use interface classes...
  40621. */
  40622. -{ USB_DEVICE_INFO(7, 1, 1) },
  40623. -{ USB_DEVICE_INFO(7, 1, 2) },
  40624. -{ USB_DEVICE_INFO(7, 1, 3) },
  40625. +//{ USB_DEVICE_INFO(7, 1, 1) },
  40626. +//{ USB_DEVICE_INFO(7, 1, 2) },
  40627. +//{ USB_DEVICE_INFO(7, 1, 3) },
  40628. #endif
  40629. #ifdef CONFIG_USB_NET_CDCETHER
  40630. /* Linux-USB CDC Ethernet gadget */
  40631. -{ USB_DEVICE(0x0525, 0xa4a1), },
  40632. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  40633. /* Linux-USB CDC Ethernet + RNDIS gadget */
  40634. -{ USB_DEVICE(0x0525, 0xa4a2), },
  40635. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  40636. #endif
  40637. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  40638. /* gadget zero, for testing */
  40639. -{ USB_DEVICE(0x0525, 0xa4a0), },
  40640. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  40641. #endif
  40642. +/* OPT Tester */
  40643. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  40644. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  40645. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  40646. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  40647. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  40648. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  40649. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  40650. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  40651. +
  40652. +/* Sony cameras */
  40653. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  40654. +
  40655. +/* Memory Devices */
  40656. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  40657. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  40658. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  40659. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  40660. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  40661. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  40662. +
  40663. +/* HP Printers */
  40664. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  40665. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  40666. +
  40667. +/* Speakers */
  40668. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  40669. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  40670. +
  40671. { } /* Terminating entry */
  40672. };
  40673. +static inline void report_errors(struct usb_device *dev)
  40674. +{
  40675. + /* OTG MESSAGE: report errors here, customize to match your product */
  40676. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  40677. + le16_to_cpu(dev->descriptor.idVendor),
  40678. + le16_to_cpu(dev->descriptor.idProduct));
  40679. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  40680. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  40681. + } else {
  40682. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  40683. + }
  40684. +}
  40685. +
  40686. +
  40687. static int is_targeted(struct usb_device *dev)
  40688. {
  40689. struct usb_device_id *id = whitelist_table;
  40690. @@ -55,58 +104,83 @@
  40691. return 1;
  40692. /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
  40693. - if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
  40694. - le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
  40695. - return 0;
  40696. + if (dev->descriptor.idVendor == 0x1a0a &&
  40697. + dev->descriptor.idProduct == 0xbadd) {
  40698. + return 0;
  40699. + } else if (!enable_whitelist) {
  40700. + return 1;
  40701. + } else {
  40702. - /* NOTE: can't use usb_match_id() since interface caches
  40703. - * aren't set up yet. this is cut/paste from that code.
  40704. - */
  40705. - for (id = whitelist_table; id->match_flags; id++) {
  40706. - if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  40707. - id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  40708. - continue;
  40709. -
  40710. - if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  40711. - id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  40712. - continue;
  40713. -
  40714. - /* No need to test id->bcdDevice_lo != 0, since 0 is never
  40715. - greater than any unsigned number. */
  40716. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  40717. - (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  40718. - continue;
  40719. -
  40720. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  40721. - (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  40722. - continue;
  40723. -
  40724. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  40725. - (id->bDeviceClass != dev->descriptor.bDeviceClass))
  40726. - continue;
  40727. -
  40728. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  40729. - (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  40730. - continue;
  40731. -
  40732. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  40733. - (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  40734. - continue;
  40735. +#ifdef DEBUG
  40736. + dev_dbg(&dev->dev, "device V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  40737. + dev->descriptor.idVendor,
  40738. + dev->descriptor.idProduct,
  40739. + dev->descriptor.bDeviceClass,
  40740. + dev->descriptor.bDeviceSubClass,
  40741. + dev->descriptor.bDeviceProtocol);
  40742. +#endif
  40743. return 1;
  40744. + /* NOTE: can't use usb_match_id() since interface caches
  40745. + * aren't set up yet. this is cut/paste from that code.
  40746. + */
  40747. + for (id = whitelist_table; id->match_flags; id++) {
  40748. +#ifdef DEBUG
  40749. + dev_dbg(&dev->dev,
  40750. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  40751. + id->idVendor,
  40752. + id->idProduct,
  40753. + id->bDeviceClass,
  40754. + id->bDeviceSubClass,
  40755. + id->bDeviceProtocol);
  40756. +#endif
  40757. +
  40758. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  40759. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  40760. + continue;
  40761. +
  40762. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  40763. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  40764. + continue;
  40765. +
  40766. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  40767. + greater than any unsigned number. */
  40768. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  40769. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  40770. + continue;
  40771. +
  40772. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  40773. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  40774. + continue;
  40775. +
  40776. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  40777. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  40778. + continue;
  40779. +
  40780. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  40781. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  40782. + continue;
  40783. +
  40784. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  40785. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  40786. + continue;
  40787. +
  40788. + return 1;
  40789. + }
  40790. }
  40791. /* add other match criteria here ... */
  40792. -
  40793. - /* OTG MESSAGE: report errors here, customize to match your product */
  40794. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  40795. - le16_to_cpu(dev->descriptor.idVendor),
  40796. - le16_to_cpu(dev->descriptor.idProduct));
  40797. #ifdef CONFIG_USB_OTG_WHITELIST
  40798. + report_errors(dev);
  40799. return 0;
  40800. #else
  40801. - return 1;
  40802. + if (enable_whitelist) {
  40803. + report_errors(dev);
  40804. + return 0;
  40805. + } else {
  40806. + return 1;
  40807. + }
  40808. #endif
  40809. }
  40810. diff -Nur linux-3.17.5/drivers/usb/gadget/file_storage.c linux-rpi/drivers/usb/gadget/file_storage.c
  40811. --- linux-3.17.5/drivers/usb/gadget/file_storage.c 1969-12-31 18:00:00.000000000 -0600
  40812. +++ linux-rpi/drivers/usb/gadget/file_storage.c 2014-12-11 14:02:55.316418001 -0600
  40813. @@ -0,0 +1,3676 @@
  40814. +/*
  40815. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  40816. + *
  40817. + * Copyright (C) 2003-2008 Alan Stern
  40818. + * All rights reserved.
  40819. + *
  40820. + * Redistribution and use in source and binary forms, with or without
  40821. + * modification, are permitted provided that the following conditions
  40822. + * are met:
  40823. + * 1. Redistributions of source code must retain the above copyright
  40824. + * notice, this list of conditions, and the following disclaimer,
  40825. + * without modification.
  40826. + * 2. Redistributions in binary form must reproduce the above copyright
  40827. + * notice, this list of conditions and the following disclaimer in the
  40828. + * documentation and/or other materials provided with the distribution.
  40829. + * 3. The names of the above-listed copyright holders may not be used
  40830. + * to endorse or promote products derived from this software without
  40831. + * specific prior written permission.
  40832. + *
  40833. + * ALTERNATIVELY, this software may be distributed under the terms of the
  40834. + * GNU General Public License ("GPL") as published by the Free Software
  40835. + * Foundation, either version 2 of that License or (at your option) any
  40836. + * later version.
  40837. + *
  40838. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  40839. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  40840. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40841. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  40842. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  40843. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  40844. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  40845. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  40846. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40847. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  40848. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40849. + */
  40850. +
  40851. +
  40852. +/*
  40853. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  40854. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  40855. + * to providing an example of a genuinely useful gadget driver for a USB
  40856. + * device, it also illustrates a technique of double-buffering for increased
  40857. + * throughput. Last but not least, it gives an easy way to probe the
  40858. + * behavior of the Mass Storage drivers in a USB host.
  40859. + *
  40860. + * Backing storage is provided by a regular file or a block device, specified
  40861. + * by the "file" module parameter. Access can be limited to read-only by
  40862. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  40863. + * access is always read-only.) The gadget will indicate that it has
  40864. + * removable media if the optional "removable" module parameter is set.
  40865. + *
  40866. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  40867. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  40868. + * by the optional "transport" module parameter. It also supports the
  40869. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  40870. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  40871. + * the optional "protocol" module parameter. In addition, the default
  40872. + * Vendor ID, Product ID, release number and serial number can be overridden.
  40873. + *
  40874. + * There is support for multiple logical units (LUNs), each of which has
  40875. + * its own backing file. The number of LUNs can be set using the optional
  40876. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  40877. + * files are specified using comma-separated lists for "file" and "ro".
  40878. + * The default number of LUNs is taken from the number of "file" elements;
  40879. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  40880. + * file must be specified for each LUN. If it is set, then an unspecified
  40881. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  40882. + * each LUN would be settable independently as a disk drive or a CD-ROM
  40883. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  40884. + * emulation includes a single data track and no audio tracks; hence there
  40885. + * need be only one backing file per LUN.
  40886. + *
  40887. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  40888. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  40889. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  40890. + * Support is included for both full-speed and high-speed operation.
  40891. + *
  40892. + * Note that the driver is slightly non-portable in that it assumes a
  40893. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  40894. + * interrupt-in endpoints. With most device controllers this isn't an
  40895. + * issue, but there may be some with hardware restrictions that prevent
  40896. + * a buffer from being used by more than one endpoint.
  40897. + *
  40898. + * Module options:
  40899. + *
  40900. + * file=filename[,filename...]
  40901. + * Required if "removable" is not set, names of
  40902. + * the files or block devices used for
  40903. + * backing storage
  40904. + * serial=HHHH... Required serial number (string of hex chars)
  40905. + * ro=b[,b...] Default false, booleans for read-only access
  40906. + * removable Default false, boolean for removable media
  40907. + * luns=N Default N = number of filenames, number of
  40908. + * LUNs to support
  40909. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  40910. + * in SCSI WRITE(10,12) commands
  40911. + * stall Default determined according to the type of
  40912. + * USB device controller (usually true),
  40913. + * boolean to permit the driver to halt
  40914. + * bulk endpoints
  40915. + * cdrom Default false, boolean for whether to emulate
  40916. + * a CD-ROM drive
  40917. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  40918. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  40919. + * ATAPI, QIC, UFI, 8070, or SCSI;
  40920. + * also 1 - 6)
  40921. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  40922. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  40923. + * release=0xRRRR Override the USB release number (bcdDevice)
  40924. + * buflen=N Default N=16384, buffer size used (will be
  40925. + * rounded down to a multiple of
  40926. + * PAGE_CACHE_SIZE)
  40927. + *
  40928. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  40929. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  40930. + * default values are used for everything else.
  40931. + *
  40932. + * The pathnames of the backing files and the ro settings are available in
  40933. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  40934. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  40935. + * these files will simulate ejecting/loading the medium (writing an empty
  40936. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  40937. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  40938. + * is being used.
  40939. + *
  40940. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  40941. + * The driver's SCSI command interface was based on the "Information
  40942. + * technology - Small Computer System Interface - 2" document from
  40943. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  40944. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  40945. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  40946. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  40947. + * document, Revision 1.0, December 14, 1998, available at
  40948. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  40949. + */
  40950. +
  40951. +
  40952. +/*
  40953. + * Driver Design
  40954. + *
  40955. + * The FSG driver is fairly straightforward. There is a main kernel
  40956. + * thread that handles most of the work. Interrupt routines field
  40957. + * callbacks from the controller driver: bulk- and interrupt-request
  40958. + * completion notifications, endpoint-0 events, and disconnect events.
  40959. + * Completion events are passed to the main thread by wakeup calls. Many
  40960. + * ep0 requests are handled at interrupt time, but SetInterface,
  40961. + * SetConfiguration, and device reset requests are forwarded to the
  40962. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  40963. + * should interrupt any ongoing file I/O operations).
  40964. + *
  40965. + * The thread's main routine implements the standard command/data/status
  40966. + * parts of a SCSI interaction. It and its subroutines are full of tests
  40967. + * for pending signals/exceptions -- all this polling is necessary since
  40968. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  40969. + * indication that the driver really wants to be running in userspace.)
  40970. + * An important point is that so long as the thread is alive it keeps an
  40971. + * open reference to the backing file. This will prevent unmounting
  40972. + * the backing file's underlying filesystem and could cause problems
  40973. + * during system shutdown, for example. To prevent such problems, the
  40974. + * thread catches INT, TERM, and KILL signals and converts them into
  40975. + * an EXIT exception.
  40976. + *
  40977. + * In normal operation the main thread is started during the gadget's
  40978. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  40979. + * exit when it receives a signal, and there's no point leaving the
  40980. + * gadget running when the thread is dead. So just before the thread
  40981. + * exits, it deregisters the gadget driver. This makes things a little
  40982. + * tricky: The driver is deregistered at two places, and the exiting
  40983. + * thread can indirectly call fsg_unbind() which in turn can tell the
  40984. + * thread to exit. The first problem is resolved through the use of the
  40985. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  40986. + * The second problem is resolved by having fsg_unbind() check
  40987. + * fsg->state; it won't try to stop the thread if the state is already
  40988. + * FSG_STATE_TERMINATED.
  40989. + *
  40990. + * To provide maximum throughput, the driver uses a circular pipeline of
  40991. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  40992. + * arbitrarily long; in practice the benefits don't justify having more
  40993. + * than 2 stages (i.e., double buffering). But it helps to think of the
  40994. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  40995. + * a bulk-out request pointer (since the buffer can be used for both
  40996. + * output and input -- directions always are given from the host's
  40997. + * point of view) as well as a pointer to the buffer and various state
  40998. + * variables.
  40999. + *
  41000. + * Use of the pipeline follows a simple protocol. There is a variable
  41001. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  41002. + * At any time that buffer head may still be in use from an earlier
  41003. + * request, so each buffer head has a state variable indicating whether
  41004. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  41005. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  41006. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  41007. + * head FULL when the I/O is complete. Then the buffer will be emptied
  41008. + * (again possibly by USB I/O, during which it is marked BUSY) and
  41009. + * finally marked EMPTY again (possibly by a completion routine).
  41010. + *
  41011. + * A module parameter tells the driver to avoid stalling the bulk
  41012. + * endpoints wherever the transport specification allows. This is
  41013. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  41014. + * halt on a bulk endpoint. However, under certain circumstances the
  41015. + * Bulk-only specification requires a stall. In such cases the driver
  41016. + * will halt the endpoint and set a flag indicating that it should clear
  41017. + * the halt in software during the next device reset. Hopefully this
  41018. + * will permit everything to work correctly. Furthermore, although the
  41019. + * specification allows the bulk-out endpoint to halt when the host sends
  41020. + * too much data, implementing this would cause an unavoidable race.
  41021. + * The driver will always use the "no-stall" approach for OUT transfers.
  41022. + *
  41023. + * One subtle point concerns sending status-stage responses for ep0
  41024. + * requests. Some of these requests, such as device reset, can involve
  41025. + * interrupting an ongoing file I/O operation, which might take an
  41026. + * arbitrarily long time. During that delay the host might give up on
  41027. + * the original ep0 request and issue a new one. When that happens the
  41028. + * driver should not notify the host about completion of the original
  41029. + * request, as the host will no longer be waiting for it. So the driver
  41030. + * assigns to each ep0 request a unique tag, and it keeps track of the
  41031. + * tag value of the request associated with a long-running exception
  41032. + * (device-reset, interface-change, or configuration-change). When the
  41033. + * exception handler is finished, the status-stage response is submitted
  41034. + * only if the current ep0 request tag is equal to the exception request
  41035. + * tag. Thus only the most recently received ep0 request will get a
  41036. + * status-stage response.
  41037. + *
  41038. + * Warning: This driver source file is too long. It ought to be split up
  41039. + * into a header file plus about 3 separate .c files, to handle the details
  41040. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  41041. + */
  41042. +
  41043. +
  41044. +/* #define VERBOSE_DEBUG */
  41045. +/* #define DUMP_MSGS */
  41046. +
  41047. +
  41048. +#include <linux/blkdev.h>
  41049. +#include <linux/completion.h>
  41050. +#include <linux/dcache.h>
  41051. +#include <linux/delay.h>
  41052. +#include <linux/device.h>
  41053. +#include <linux/fcntl.h>
  41054. +#include <linux/file.h>
  41055. +#include <linux/fs.h>
  41056. +#include <linux/kref.h>
  41057. +#include <linux/kthread.h>
  41058. +#include <linux/limits.h>
  41059. +#include <linux/module.h>
  41060. +#include <linux/rwsem.h>
  41061. +#include <linux/slab.h>
  41062. +#include <linux/spinlock.h>
  41063. +#include <linux/string.h>
  41064. +#include <linux/freezer.h>
  41065. +#include <linux/utsname.h>
  41066. +
  41067. +#include <linux/usb/ch9.h>
  41068. +#include <linux/usb/gadget.h>
  41069. +
  41070. +#include "gadget_chips.h"
  41071. +
  41072. +
  41073. +
  41074. +/*
  41075. + * Kbuild is not very cooperative with respect to linking separately
  41076. + * compiled library objects into one module. So for now we won't use
  41077. + * separate compilation ... ensuring init/exit sections work to shrink
  41078. + * the runtime footprint, and giving us at least some parts of what
  41079. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  41080. + */
  41081. +#include "usbstring.c"
  41082. +#include "config.c"
  41083. +#include "epautoconf.c"
  41084. +
  41085. +/*-------------------------------------------------------------------------*/
  41086. +
  41087. +#define DRIVER_DESC "File-backed Storage Gadget"
  41088. +#define DRIVER_NAME "g_file_storage"
  41089. +#define DRIVER_VERSION "1 September 2010"
  41090. +
  41091. +static char fsg_string_manufacturer[64];
  41092. +static const char fsg_string_product[] = DRIVER_DESC;
  41093. +static const char fsg_string_config[] = "Self-powered";
  41094. +static const char fsg_string_interface[] = "Mass Storage";
  41095. +
  41096. +
  41097. +#include "storage_common.c"
  41098. +
  41099. +
  41100. +MODULE_DESCRIPTION(DRIVER_DESC);
  41101. +MODULE_AUTHOR("Alan Stern");
  41102. +MODULE_LICENSE("Dual BSD/GPL");
  41103. +
  41104. +/*
  41105. + * This driver assumes self-powered hardware and has no way for users to
  41106. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  41107. + * and endpoint addresses.
  41108. + */
  41109. +
  41110. +
  41111. +/*-------------------------------------------------------------------------*/
  41112. +
  41113. +
  41114. +/* Encapsulate the module parameter settings */
  41115. +
  41116. +static struct {
  41117. + char *file[FSG_MAX_LUNS];
  41118. + char *serial;
  41119. + bool ro[FSG_MAX_LUNS];
  41120. + bool nofua[FSG_MAX_LUNS];
  41121. + unsigned int num_filenames;
  41122. + unsigned int num_ros;
  41123. + unsigned int num_nofuas;
  41124. + unsigned int nluns;
  41125. +
  41126. + bool removable;
  41127. + bool can_stall;
  41128. + bool cdrom;
  41129. +
  41130. + char *transport_parm;
  41131. + char *protocol_parm;
  41132. + unsigned short vendor;
  41133. + unsigned short product;
  41134. + unsigned short release;
  41135. + unsigned int buflen;
  41136. +
  41137. + int transport_type;
  41138. + char *transport_name;
  41139. + int protocol_type;
  41140. + char *protocol_name;
  41141. +
  41142. +} mod_data = { // Default values
  41143. + .transport_parm = "BBB",
  41144. + .protocol_parm = "SCSI",
  41145. + .removable = 0,
  41146. + .can_stall = 1,
  41147. + .cdrom = 0,
  41148. + .vendor = FSG_VENDOR_ID,
  41149. + .product = FSG_PRODUCT_ID,
  41150. + .release = 0xffff, // Use controller chip type
  41151. + .buflen = 16384,
  41152. + };
  41153. +
  41154. +
  41155. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  41156. + S_IRUGO);
  41157. +MODULE_PARM_DESC(file, "names of backing files or devices");
  41158. +
  41159. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  41160. +MODULE_PARM_DESC(serial, "USB serial number");
  41161. +
  41162. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  41163. +MODULE_PARM_DESC(ro, "true to force read-only");
  41164. +
  41165. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  41166. + S_IRUGO);
  41167. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  41168. +
  41169. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  41170. +MODULE_PARM_DESC(luns, "number of LUNs");
  41171. +
  41172. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  41173. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  41174. +
  41175. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  41176. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  41177. +
  41178. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  41179. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  41180. +
  41181. +/* In the non-TEST version, only the module parameters listed above
  41182. + * are available. */
  41183. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  41184. +
  41185. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  41186. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  41187. +
  41188. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  41189. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  41190. + "8070, or SCSI)");
  41191. +
  41192. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  41193. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  41194. +
  41195. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  41196. +MODULE_PARM_DESC(product, "USB Product ID");
  41197. +
  41198. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  41199. +MODULE_PARM_DESC(release, "USB release number");
  41200. +
  41201. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  41202. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  41203. +
  41204. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  41205. +
  41206. +
  41207. +/*
  41208. + * These definitions will permit the compiler to avoid generating code for
  41209. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  41210. + * can recognize when a test of a constant expression yields a dead code
  41211. + * path.
  41212. + */
  41213. +
  41214. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  41215. +
  41216. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  41217. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  41218. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  41219. +
  41220. +#else
  41221. +
  41222. +#define transport_is_bbb() 1
  41223. +#define transport_is_cbi() 0
  41224. +#define protocol_is_scsi() 1
  41225. +
  41226. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  41227. +
  41228. +
  41229. +/*-------------------------------------------------------------------------*/
  41230. +
  41231. +
  41232. +struct fsg_dev {
  41233. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  41234. + spinlock_t lock;
  41235. + struct usb_gadget *gadget;
  41236. +
  41237. + /* filesem protects: backing files in use */
  41238. + struct rw_semaphore filesem;
  41239. +
  41240. + /* reference counting: wait until all LUNs are released */
  41241. + struct kref ref;
  41242. +
  41243. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  41244. + struct usb_request *ep0req; // For control responses
  41245. + unsigned int ep0_req_tag;
  41246. + const char *ep0req_name;
  41247. +
  41248. + struct usb_request *intreq; // For interrupt responses
  41249. + int intreq_busy;
  41250. + struct fsg_buffhd *intr_buffhd;
  41251. +
  41252. + unsigned int bulk_out_maxpacket;
  41253. + enum fsg_state state; // For exception handling
  41254. + unsigned int exception_req_tag;
  41255. +
  41256. + u8 config, new_config;
  41257. +
  41258. + unsigned int running : 1;
  41259. + unsigned int bulk_in_enabled : 1;
  41260. + unsigned int bulk_out_enabled : 1;
  41261. + unsigned int intr_in_enabled : 1;
  41262. + unsigned int phase_error : 1;
  41263. + unsigned int short_packet_received : 1;
  41264. + unsigned int bad_lun_okay : 1;
  41265. +
  41266. + unsigned long atomic_bitflags;
  41267. +#define REGISTERED 0
  41268. +#define IGNORE_BULK_OUT 1
  41269. +#define SUSPENDED 2
  41270. +
  41271. + struct usb_ep *bulk_in;
  41272. + struct usb_ep *bulk_out;
  41273. + struct usb_ep *intr_in;
  41274. +
  41275. + struct fsg_buffhd *next_buffhd_to_fill;
  41276. + struct fsg_buffhd *next_buffhd_to_drain;
  41277. +
  41278. + int thread_wakeup_needed;
  41279. + struct completion thread_notifier;
  41280. + struct task_struct *thread_task;
  41281. +
  41282. + int cmnd_size;
  41283. + u8 cmnd[MAX_COMMAND_SIZE];
  41284. + enum data_direction data_dir;
  41285. + u32 data_size;
  41286. + u32 data_size_from_cmnd;
  41287. + u32 tag;
  41288. + unsigned int lun;
  41289. + u32 residue;
  41290. + u32 usb_amount_left;
  41291. +
  41292. + /* The CB protocol offers no way for a host to know when a command
  41293. + * has completed. As a result the next command may arrive early,
  41294. + * and we will still have to handle it. For that reason we need
  41295. + * a buffer to store new commands when using CB (or CBI, which
  41296. + * does not oblige a host to wait for command completion either). */
  41297. + int cbbuf_cmnd_size;
  41298. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  41299. +
  41300. + unsigned int nluns;
  41301. + struct fsg_lun *luns;
  41302. + struct fsg_lun *curlun;
  41303. + /* Must be the last entry */
  41304. + struct fsg_buffhd buffhds[];
  41305. +};
  41306. +
  41307. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  41308. +
  41309. +static int exception_in_progress(struct fsg_dev *fsg)
  41310. +{
  41311. + return (fsg->state > FSG_STATE_IDLE);
  41312. +}
  41313. +
  41314. +/* Make bulk-out requests be divisible by the maxpacket size */
  41315. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  41316. + struct fsg_buffhd *bh, unsigned int length)
  41317. +{
  41318. + unsigned int rem;
  41319. +
  41320. + bh->bulk_out_intended_length = length;
  41321. + rem = length % fsg->bulk_out_maxpacket;
  41322. + if (rem > 0)
  41323. + length += fsg->bulk_out_maxpacket - rem;
  41324. + bh->outreq->length = length;
  41325. +}
  41326. +
  41327. +static struct fsg_dev *the_fsg;
  41328. +static struct usb_gadget_driver fsg_driver;
  41329. +
  41330. +
  41331. +/*-------------------------------------------------------------------------*/
  41332. +
  41333. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  41334. +{
  41335. + const char *name;
  41336. +
  41337. + if (ep == fsg->bulk_in)
  41338. + name = "bulk-in";
  41339. + else if (ep == fsg->bulk_out)
  41340. + name = "bulk-out";
  41341. + else
  41342. + name = ep->name;
  41343. + DBG(fsg, "%s set halt\n", name);
  41344. + return usb_ep_set_halt(ep);
  41345. +}
  41346. +
  41347. +
  41348. +/*-------------------------------------------------------------------------*/
  41349. +
  41350. +/*
  41351. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  41352. + * descriptors are built on demand. Also the (static) config and interface
  41353. + * descriptors are adjusted during fsg_bind().
  41354. + */
  41355. +
  41356. +/* There is only one configuration. */
  41357. +#define CONFIG_VALUE 1
  41358. +
  41359. +static struct usb_device_descriptor
  41360. +device_desc = {
  41361. + .bLength = sizeof device_desc,
  41362. + .bDescriptorType = USB_DT_DEVICE,
  41363. +
  41364. + .bcdUSB = cpu_to_le16(0x0200),
  41365. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  41366. +
  41367. + /* The next three values can be overridden by module parameters */
  41368. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  41369. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  41370. + .bcdDevice = cpu_to_le16(0xffff),
  41371. +
  41372. + .iManufacturer = FSG_STRING_MANUFACTURER,
  41373. + .iProduct = FSG_STRING_PRODUCT,
  41374. + .iSerialNumber = FSG_STRING_SERIAL,
  41375. + .bNumConfigurations = 1,
  41376. +};
  41377. +
  41378. +static struct usb_config_descriptor
  41379. +config_desc = {
  41380. + .bLength = sizeof config_desc,
  41381. + .bDescriptorType = USB_DT_CONFIG,
  41382. +
  41383. + /* wTotalLength computed by usb_gadget_config_buf() */
  41384. + .bNumInterfaces = 1,
  41385. + .bConfigurationValue = CONFIG_VALUE,
  41386. + .iConfiguration = FSG_STRING_CONFIG,
  41387. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  41388. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  41389. +};
  41390. +
  41391. +
  41392. +static struct usb_qualifier_descriptor
  41393. +dev_qualifier = {
  41394. + .bLength = sizeof dev_qualifier,
  41395. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  41396. +
  41397. + .bcdUSB = cpu_to_le16(0x0200),
  41398. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  41399. +
  41400. + .bNumConfigurations = 1,
  41401. +};
  41402. +
  41403. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  41404. +{
  41405. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  41406. + buf += USB_DT_BOS_SIZE;
  41407. +
  41408. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  41409. + buf += USB_DT_USB_EXT_CAP_SIZE;
  41410. +
  41411. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  41412. +
  41413. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  41414. + + USB_DT_USB_EXT_CAP_SIZE;
  41415. +}
  41416. +
  41417. +/*
  41418. + * Config descriptors must agree with the code that sets configurations
  41419. + * and with code managing interfaces and their altsettings. They must
  41420. + * also handle different speeds and other-speed requests.
  41421. + */
  41422. +static int populate_config_buf(struct usb_gadget *gadget,
  41423. + u8 *buf, u8 type, unsigned index)
  41424. +{
  41425. + enum usb_device_speed speed = gadget->speed;
  41426. + int len;
  41427. + const struct usb_descriptor_header **function;
  41428. +
  41429. + if (index > 0)
  41430. + return -EINVAL;
  41431. +
  41432. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  41433. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  41434. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  41435. + ? (const struct usb_descriptor_header **)fsg_hs_function
  41436. + : (const struct usb_descriptor_header **)fsg_fs_function;
  41437. +
  41438. + /* for now, don't advertise srp-only devices */
  41439. + if (!gadget_is_otg(gadget))
  41440. + function++;
  41441. +
  41442. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  41443. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  41444. + return len;
  41445. +}
  41446. +
  41447. +
  41448. +/*-------------------------------------------------------------------------*/
  41449. +
  41450. +/* These routines may be called in process context or in_irq */
  41451. +
  41452. +/* Caller must hold fsg->lock */
  41453. +static void wakeup_thread(struct fsg_dev *fsg)
  41454. +{
  41455. + /* Tell the main thread that something has happened */
  41456. + fsg->thread_wakeup_needed = 1;
  41457. + if (fsg->thread_task)
  41458. + wake_up_process(fsg->thread_task);
  41459. +}
  41460. +
  41461. +
  41462. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  41463. +{
  41464. + unsigned long flags;
  41465. +
  41466. + /* Do nothing if a higher-priority exception is already in progress.
  41467. + * If a lower-or-equal priority exception is in progress, preempt it
  41468. + * and notify the main thread by sending it a signal. */
  41469. + spin_lock_irqsave(&fsg->lock, flags);
  41470. + if (fsg->state <= new_state) {
  41471. + fsg->exception_req_tag = fsg->ep0_req_tag;
  41472. + fsg->state = new_state;
  41473. + if (fsg->thread_task)
  41474. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  41475. + fsg->thread_task);
  41476. + }
  41477. + spin_unlock_irqrestore(&fsg->lock, flags);
  41478. +}
  41479. +
  41480. +
  41481. +/*-------------------------------------------------------------------------*/
  41482. +
  41483. +/* The disconnect callback and ep0 routines. These always run in_irq,
  41484. + * except that ep0_queue() is called in the main thread to acknowledge
  41485. + * completion of various requests: set config, set interface, and
  41486. + * Bulk-only device reset. */
  41487. +
  41488. +static void fsg_disconnect(struct usb_gadget *gadget)
  41489. +{
  41490. + struct fsg_dev *fsg = get_gadget_data(gadget);
  41491. +
  41492. + DBG(fsg, "disconnect or port reset\n");
  41493. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  41494. +}
  41495. +
  41496. +
  41497. +static int ep0_queue(struct fsg_dev *fsg)
  41498. +{
  41499. + int rc;
  41500. +
  41501. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  41502. + if (rc != 0 && rc != -ESHUTDOWN) {
  41503. +
  41504. + /* We can't do much more than wait for a reset */
  41505. + WARNING(fsg, "error in submission: %s --> %d\n",
  41506. + fsg->ep0->name, rc);
  41507. + }
  41508. + return rc;
  41509. +}
  41510. +
  41511. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  41512. +{
  41513. + struct fsg_dev *fsg = ep->driver_data;
  41514. +
  41515. + if (req->actual > 0)
  41516. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  41517. + if (req->status || req->actual != req->length)
  41518. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  41519. + req->status, req->actual, req->length);
  41520. + if (req->status == -ECONNRESET) // Request was cancelled
  41521. + usb_ep_fifo_flush(ep);
  41522. +
  41523. + if (req->status == 0 && req->context)
  41524. + ((fsg_routine_t) (req->context))(fsg);
  41525. +}
  41526. +
  41527. +
  41528. +/*-------------------------------------------------------------------------*/
  41529. +
  41530. +/* Bulk and interrupt endpoint completion handlers.
  41531. + * These always run in_irq. */
  41532. +
  41533. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  41534. +{
  41535. + struct fsg_dev *fsg = ep->driver_data;
  41536. + struct fsg_buffhd *bh = req->context;
  41537. +
  41538. + if (req->status || req->actual != req->length)
  41539. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  41540. + req->status, req->actual, req->length);
  41541. + if (req->status == -ECONNRESET) // Request was cancelled
  41542. + usb_ep_fifo_flush(ep);
  41543. +
  41544. + /* Hold the lock while we update the request and buffer states */
  41545. + smp_wmb();
  41546. + spin_lock(&fsg->lock);
  41547. + bh->inreq_busy = 0;
  41548. + bh->state = BUF_STATE_EMPTY;
  41549. + wakeup_thread(fsg);
  41550. + spin_unlock(&fsg->lock);
  41551. +}
  41552. +
  41553. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  41554. +{
  41555. + struct fsg_dev *fsg = ep->driver_data;
  41556. + struct fsg_buffhd *bh = req->context;
  41557. +
  41558. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  41559. + if (req->status || req->actual != bh->bulk_out_intended_length)
  41560. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  41561. + req->status, req->actual,
  41562. + bh->bulk_out_intended_length);
  41563. + if (req->status == -ECONNRESET) // Request was cancelled
  41564. + usb_ep_fifo_flush(ep);
  41565. +
  41566. + /* Hold the lock while we update the request and buffer states */
  41567. + smp_wmb();
  41568. + spin_lock(&fsg->lock);
  41569. + bh->outreq_busy = 0;
  41570. + bh->state = BUF_STATE_FULL;
  41571. + wakeup_thread(fsg);
  41572. + spin_unlock(&fsg->lock);
  41573. +}
  41574. +
  41575. +
  41576. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  41577. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  41578. +{
  41579. + struct fsg_dev *fsg = ep->driver_data;
  41580. + struct fsg_buffhd *bh = req->context;
  41581. +
  41582. + if (req->status || req->actual != req->length)
  41583. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  41584. + req->status, req->actual, req->length);
  41585. + if (req->status == -ECONNRESET) // Request was cancelled
  41586. + usb_ep_fifo_flush(ep);
  41587. +
  41588. + /* Hold the lock while we update the request and buffer states */
  41589. + smp_wmb();
  41590. + spin_lock(&fsg->lock);
  41591. + fsg->intreq_busy = 0;
  41592. + bh->state = BUF_STATE_EMPTY;
  41593. + wakeup_thread(fsg);
  41594. + spin_unlock(&fsg->lock);
  41595. +}
  41596. +
  41597. +#else
  41598. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  41599. +{}
  41600. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  41601. +
  41602. +
  41603. +/*-------------------------------------------------------------------------*/
  41604. +
  41605. +/* Ep0 class-specific handlers. These always run in_irq. */
  41606. +
  41607. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  41608. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  41609. +{
  41610. + struct usb_request *req = fsg->ep0req;
  41611. + static u8 cbi_reset_cmnd[6] = {
  41612. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  41613. +
  41614. + /* Error in command transfer? */
  41615. + if (req->status || req->length != req->actual ||
  41616. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  41617. +
  41618. + /* Not all controllers allow a protocol stall after
  41619. + * receiving control-out data, but we'll try anyway. */
  41620. + fsg_set_halt(fsg, fsg->ep0);
  41621. + return; // Wait for reset
  41622. + }
  41623. +
  41624. + /* Is it the special reset command? */
  41625. + if (req->actual >= sizeof cbi_reset_cmnd &&
  41626. + memcmp(req->buf, cbi_reset_cmnd,
  41627. + sizeof cbi_reset_cmnd) == 0) {
  41628. +
  41629. + /* Raise an exception to stop the current operation
  41630. + * and reinitialize our state. */
  41631. + DBG(fsg, "cbi reset request\n");
  41632. + raise_exception(fsg, FSG_STATE_RESET);
  41633. + return;
  41634. + }
  41635. +
  41636. + VDBG(fsg, "CB[I] accept device-specific command\n");
  41637. + spin_lock(&fsg->lock);
  41638. +
  41639. + /* Save the command for later */
  41640. + if (fsg->cbbuf_cmnd_size)
  41641. + WARNING(fsg, "CB[I] overwriting previous command\n");
  41642. + fsg->cbbuf_cmnd_size = req->actual;
  41643. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  41644. +
  41645. + wakeup_thread(fsg);
  41646. + spin_unlock(&fsg->lock);
  41647. +}
  41648. +
  41649. +#else
  41650. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  41651. +{}
  41652. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  41653. +
  41654. +
  41655. +static int class_setup_req(struct fsg_dev *fsg,
  41656. + const struct usb_ctrlrequest *ctrl)
  41657. +{
  41658. + struct usb_request *req = fsg->ep0req;
  41659. + int value = -EOPNOTSUPP;
  41660. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  41661. + u16 w_value = le16_to_cpu(ctrl->wValue);
  41662. + u16 w_length = le16_to_cpu(ctrl->wLength);
  41663. +
  41664. + if (!fsg->config)
  41665. + return value;
  41666. +
  41667. + /* Handle Bulk-only class-specific requests */
  41668. + if (transport_is_bbb()) {
  41669. + switch (ctrl->bRequest) {
  41670. +
  41671. + case US_BULK_RESET_REQUEST:
  41672. + if (ctrl->bRequestType != (USB_DIR_OUT |
  41673. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  41674. + break;
  41675. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  41676. + value = -EDOM;
  41677. + break;
  41678. + }
  41679. +
  41680. + /* Raise an exception to stop the current operation
  41681. + * and reinitialize our state. */
  41682. + DBG(fsg, "bulk reset request\n");
  41683. + raise_exception(fsg, FSG_STATE_RESET);
  41684. + value = DELAYED_STATUS;
  41685. + break;
  41686. +
  41687. + case US_BULK_GET_MAX_LUN:
  41688. + if (ctrl->bRequestType != (USB_DIR_IN |
  41689. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  41690. + break;
  41691. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  41692. + value = -EDOM;
  41693. + break;
  41694. + }
  41695. + VDBG(fsg, "get max LUN\n");
  41696. + *(u8 *) req->buf = fsg->nluns - 1;
  41697. + value = 1;
  41698. + break;
  41699. + }
  41700. + }
  41701. +
  41702. + /* Handle CBI class-specific requests */
  41703. + else {
  41704. + switch (ctrl->bRequest) {
  41705. +
  41706. + case USB_CBI_ADSC_REQUEST:
  41707. + if (ctrl->bRequestType != (USB_DIR_OUT |
  41708. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  41709. + break;
  41710. + if (w_index != 0 || w_value != 0) {
  41711. + value = -EDOM;
  41712. + break;
  41713. + }
  41714. + if (w_length > MAX_COMMAND_SIZE) {
  41715. + value = -EOVERFLOW;
  41716. + break;
  41717. + }
  41718. + value = w_length;
  41719. + fsg->ep0req->context = received_cbi_adsc;
  41720. + break;
  41721. + }
  41722. + }
  41723. +
  41724. + if (value == -EOPNOTSUPP)
  41725. + VDBG(fsg,
  41726. + "unknown class-specific control req "
  41727. + "%02x.%02x v%04x i%04x l%u\n",
  41728. + ctrl->bRequestType, ctrl->bRequest,
  41729. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  41730. + return value;
  41731. +}
  41732. +
  41733. +
  41734. +/*-------------------------------------------------------------------------*/
  41735. +
  41736. +/* Ep0 standard request handlers. These always run in_irq. */
  41737. +
  41738. +static int standard_setup_req(struct fsg_dev *fsg,
  41739. + const struct usb_ctrlrequest *ctrl)
  41740. +{
  41741. + struct usb_request *req = fsg->ep0req;
  41742. + int value = -EOPNOTSUPP;
  41743. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  41744. + u16 w_value = le16_to_cpu(ctrl->wValue);
  41745. +
  41746. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  41747. + * but config change events will also reconfigure hardware. */
  41748. + switch (ctrl->bRequest) {
  41749. +
  41750. + case USB_REQ_GET_DESCRIPTOR:
  41751. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  41752. + USB_RECIP_DEVICE))
  41753. + break;
  41754. + switch (w_value >> 8) {
  41755. +
  41756. + case USB_DT_DEVICE:
  41757. + VDBG(fsg, "get device descriptor\n");
  41758. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  41759. + value = sizeof device_desc;
  41760. + memcpy(req->buf, &device_desc, value);
  41761. + break;
  41762. + case USB_DT_DEVICE_QUALIFIER:
  41763. + VDBG(fsg, "get device qualifier\n");
  41764. + if (!gadget_is_dualspeed(fsg->gadget) ||
  41765. + fsg->gadget->speed == USB_SPEED_SUPER)
  41766. + break;
  41767. + /*
  41768. + * Assume ep0 uses the same maxpacket value for both
  41769. + * speeds
  41770. + */
  41771. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  41772. + value = sizeof dev_qualifier;
  41773. + memcpy(req->buf, &dev_qualifier, value);
  41774. + break;
  41775. +
  41776. + case USB_DT_OTHER_SPEED_CONFIG:
  41777. + VDBG(fsg, "get other-speed config descriptor\n");
  41778. + if (!gadget_is_dualspeed(fsg->gadget) ||
  41779. + fsg->gadget->speed == USB_SPEED_SUPER)
  41780. + break;
  41781. + goto get_config;
  41782. + case USB_DT_CONFIG:
  41783. + VDBG(fsg, "get configuration descriptor\n");
  41784. +get_config:
  41785. + value = populate_config_buf(fsg->gadget,
  41786. + req->buf,
  41787. + w_value >> 8,
  41788. + w_value & 0xff);
  41789. + break;
  41790. +
  41791. + case USB_DT_STRING:
  41792. + VDBG(fsg, "get string descriptor\n");
  41793. +
  41794. + /* wIndex == language code */
  41795. + value = usb_gadget_get_string(&fsg_stringtab,
  41796. + w_value & 0xff, req->buf);
  41797. + break;
  41798. +
  41799. + case USB_DT_BOS:
  41800. + VDBG(fsg, "get bos descriptor\n");
  41801. +
  41802. + if (gadget_is_superspeed(fsg->gadget))
  41803. + value = populate_bos(fsg, req->buf);
  41804. + break;
  41805. + }
  41806. +
  41807. + break;
  41808. +
  41809. + /* One config, two speeds */
  41810. + case USB_REQ_SET_CONFIGURATION:
  41811. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  41812. + USB_RECIP_DEVICE))
  41813. + break;
  41814. + VDBG(fsg, "set configuration\n");
  41815. + if (w_value == CONFIG_VALUE || w_value == 0) {
  41816. + fsg->new_config = w_value;
  41817. +
  41818. + /* Raise an exception to wipe out previous transaction
  41819. + * state (queued bufs, etc) and set the new config. */
  41820. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  41821. + value = DELAYED_STATUS;
  41822. + }
  41823. + break;
  41824. + case USB_REQ_GET_CONFIGURATION:
  41825. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  41826. + USB_RECIP_DEVICE))
  41827. + break;
  41828. + VDBG(fsg, "get configuration\n");
  41829. + *(u8 *) req->buf = fsg->config;
  41830. + value = 1;
  41831. + break;
  41832. +
  41833. + case USB_REQ_SET_INTERFACE:
  41834. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  41835. + USB_RECIP_INTERFACE))
  41836. + break;
  41837. + if (fsg->config && w_index == 0) {
  41838. +
  41839. + /* Raise an exception to wipe out previous transaction
  41840. + * state (queued bufs, etc) and install the new
  41841. + * interface altsetting. */
  41842. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  41843. + value = DELAYED_STATUS;
  41844. + }
  41845. + break;
  41846. + case USB_REQ_GET_INTERFACE:
  41847. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  41848. + USB_RECIP_INTERFACE))
  41849. + break;
  41850. + if (!fsg->config)
  41851. + break;
  41852. + if (w_index != 0) {
  41853. + value = -EDOM;
  41854. + break;
  41855. + }
  41856. + VDBG(fsg, "get interface\n");
  41857. + *(u8 *) req->buf = 0;
  41858. + value = 1;
  41859. + break;
  41860. +
  41861. + default:
  41862. + VDBG(fsg,
  41863. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  41864. + ctrl->bRequestType, ctrl->bRequest,
  41865. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  41866. + }
  41867. +
  41868. + return value;
  41869. +}
  41870. +
  41871. +
  41872. +static int fsg_setup(struct usb_gadget *gadget,
  41873. + const struct usb_ctrlrequest *ctrl)
  41874. +{
  41875. + struct fsg_dev *fsg = get_gadget_data(gadget);
  41876. + int rc;
  41877. + int w_length = le16_to_cpu(ctrl->wLength);
  41878. +
  41879. + ++fsg->ep0_req_tag; // Record arrival of a new request
  41880. + fsg->ep0req->context = NULL;
  41881. + fsg->ep0req->length = 0;
  41882. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  41883. +
  41884. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  41885. + rc = class_setup_req(fsg, ctrl);
  41886. + else
  41887. + rc = standard_setup_req(fsg, ctrl);
  41888. +
  41889. + /* Respond with data/status or defer until later? */
  41890. + if (rc >= 0 && rc != DELAYED_STATUS) {
  41891. + rc = min(rc, w_length);
  41892. + fsg->ep0req->length = rc;
  41893. + fsg->ep0req->zero = rc < w_length;
  41894. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  41895. + "ep0-in" : "ep0-out");
  41896. + rc = ep0_queue(fsg);
  41897. + }
  41898. +
  41899. + /* Device either stalls (rc < 0) or reports success */
  41900. + return rc;
  41901. +}
  41902. +
  41903. +
  41904. +/*-------------------------------------------------------------------------*/
  41905. +
  41906. +/* All the following routines run in process context */
  41907. +
  41908. +
  41909. +/* Use this for bulk or interrupt transfers, not ep0 */
  41910. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  41911. + struct usb_request *req, int *pbusy,
  41912. + enum fsg_buffer_state *state)
  41913. +{
  41914. + int rc;
  41915. +
  41916. + if (ep == fsg->bulk_in)
  41917. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  41918. + else if (ep == fsg->intr_in)
  41919. + dump_msg(fsg, "intr-in", req->buf, req->length);
  41920. +
  41921. + spin_lock_irq(&fsg->lock);
  41922. + *pbusy = 1;
  41923. + *state = BUF_STATE_BUSY;
  41924. + spin_unlock_irq(&fsg->lock);
  41925. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  41926. + if (rc != 0) {
  41927. + *pbusy = 0;
  41928. + *state = BUF_STATE_EMPTY;
  41929. +
  41930. + /* We can't do much more than wait for a reset */
  41931. +
  41932. + /* Note: currently the net2280 driver fails zero-length
  41933. + * submissions if DMA is enabled. */
  41934. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  41935. + req->length == 0))
  41936. + WARNING(fsg, "error in submission: %s --> %d\n",
  41937. + ep->name, rc);
  41938. + }
  41939. +}
  41940. +
  41941. +
  41942. +static int sleep_thread(struct fsg_dev *fsg)
  41943. +{
  41944. + int rc = 0;
  41945. +
  41946. + /* Wait until a signal arrives or we are woken up */
  41947. + for (;;) {
  41948. + try_to_freeze();
  41949. + set_current_state(TASK_INTERRUPTIBLE);
  41950. + if (signal_pending(current)) {
  41951. + rc = -EINTR;
  41952. + break;
  41953. + }
  41954. + if (fsg->thread_wakeup_needed)
  41955. + break;
  41956. + schedule();
  41957. + }
  41958. + __set_current_state(TASK_RUNNING);
  41959. + fsg->thread_wakeup_needed = 0;
  41960. + return rc;
  41961. +}
  41962. +
  41963. +
  41964. +/*-------------------------------------------------------------------------*/
  41965. +
  41966. +static int do_read(struct fsg_dev *fsg)
  41967. +{
  41968. + struct fsg_lun *curlun = fsg->curlun;
  41969. + u32 lba;
  41970. + struct fsg_buffhd *bh;
  41971. + int rc;
  41972. + u32 amount_left;
  41973. + loff_t file_offset, file_offset_tmp;
  41974. + unsigned int amount;
  41975. + ssize_t nread;
  41976. +
  41977. + /* Get the starting Logical Block Address and check that it's
  41978. + * not too big */
  41979. + if (fsg->cmnd[0] == READ_6)
  41980. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  41981. + else {
  41982. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  41983. +
  41984. + /* We allow DPO (Disable Page Out = don't save data in the
  41985. + * cache) and FUA (Force Unit Access = don't read from the
  41986. + * cache), but we don't implement them. */
  41987. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  41988. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  41989. + return -EINVAL;
  41990. + }
  41991. + }
  41992. + if (lba >= curlun->num_sectors) {
  41993. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  41994. + return -EINVAL;
  41995. + }
  41996. + file_offset = ((loff_t) lba) << curlun->blkbits;
  41997. +
  41998. + /* Carry out the file reads */
  41999. + amount_left = fsg->data_size_from_cmnd;
  42000. + if (unlikely(amount_left == 0))
  42001. + return -EIO; // No default reply
  42002. +
  42003. + for (;;) {
  42004. +
  42005. + /* Figure out how much we need to read:
  42006. + * Try to read the remaining amount.
  42007. + * But don't read more than the buffer size.
  42008. + * And don't try to read past the end of the file.
  42009. + */
  42010. + amount = min((unsigned int) amount_left, mod_data.buflen);
  42011. + amount = min((loff_t) amount,
  42012. + curlun->file_length - file_offset);
  42013. +
  42014. + /* Wait for the next buffer to become available */
  42015. + bh = fsg->next_buffhd_to_fill;
  42016. + while (bh->state != BUF_STATE_EMPTY) {
  42017. + rc = sleep_thread(fsg);
  42018. + if (rc)
  42019. + return rc;
  42020. + }
  42021. +
  42022. + /* If we were asked to read past the end of file,
  42023. + * end with an empty buffer. */
  42024. + if (amount == 0) {
  42025. + curlun->sense_data =
  42026. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42027. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42028. + curlun->info_valid = 1;
  42029. + bh->inreq->length = 0;
  42030. + bh->state = BUF_STATE_FULL;
  42031. + break;
  42032. + }
  42033. +
  42034. + /* Perform the read */
  42035. + file_offset_tmp = file_offset;
  42036. + nread = vfs_read(curlun->filp,
  42037. + (char __user *) bh->buf,
  42038. + amount, &file_offset_tmp);
  42039. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  42040. + (unsigned long long) file_offset,
  42041. + (int) nread);
  42042. + if (signal_pending(current))
  42043. + return -EINTR;
  42044. +
  42045. + if (nread < 0) {
  42046. + LDBG(curlun, "error in file read: %d\n",
  42047. + (int) nread);
  42048. + nread = 0;
  42049. + } else if (nread < amount) {
  42050. + LDBG(curlun, "partial file read: %d/%u\n",
  42051. + (int) nread, amount);
  42052. + nread = round_down(nread, curlun->blksize);
  42053. + }
  42054. + file_offset += nread;
  42055. + amount_left -= nread;
  42056. + fsg->residue -= nread;
  42057. +
  42058. + /* Except at the end of the transfer, nread will be
  42059. + * equal to the buffer size, which is divisible by the
  42060. + * bulk-in maxpacket size.
  42061. + */
  42062. + bh->inreq->length = nread;
  42063. + bh->state = BUF_STATE_FULL;
  42064. +
  42065. + /* If an error occurred, report it and its position */
  42066. + if (nread < amount) {
  42067. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  42068. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42069. + curlun->info_valid = 1;
  42070. + break;
  42071. + }
  42072. +
  42073. + if (amount_left == 0)
  42074. + break; // No more left to read
  42075. +
  42076. + /* Send this buffer and go read some more */
  42077. + bh->inreq->zero = 0;
  42078. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  42079. + &bh->inreq_busy, &bh->state);
  42080. + fsg->next_buffhd_to_fill = bh->next;
  42081. + }
  42082. +
  42083. + return -EIO; // No default reply
  42084. +}
  42085. +
  42086. +
  42087. +/*-------------------------------------------------------------------------*/
  42088. +
  42089. +static int do_write(struct fsg_dev *fsg)
  42090. +{
  42091. + struct fsg_lun *curlun = fsg->curlun;
  42092. + u32 lba;
  42093. + struct fsg_buffhd *bh;
  42094. + int get_some_more;
  42095. + u32 amount_left_to_req, amount_left_to_write;
  42096. + loff_t usb_offset, file_offset, file_offset_tmp;
  42097. + unsigned int amount;
  42098. + ssize_t nwritten;
  42099. + int rc;
  42100. +
  42101. + if (curlun->ro) {
  42102. + curlun->sense_data = SS_WRITE_PROTECTED;
  42103. + return -EINVAL;
  42104. + }
  42105. + spin_lock(&curlun->filp->f_lock);
  42106. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  42107. + spin_unlock(&curlun->filp->f_lock);
  42108. +
  42109. + /* Get the starting Logical Block Address and check that it's
  42110. + * not too big */
  42111. + if (fsg->cmnd[0] == WRITE_6)
  42112. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  42113. + else {
  42114. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  42115. +
  42116. + /* We allow DPO (Disable Page Out = don't save data in the
  42117. + * cache) and FUA (Force Unit Access = write directly to the
  42118. + * medium). We don't implement DPO; we implement FUA by
  42119. + * performing synchronous output. */
  42120. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  42121. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42122. + return -EINVAL;
  42123. + }
  42124. + /* FUA */
  42125. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  42126. + spin_lock(&curlun->filp->f_lock);
  42127. + curlun->filp->f_flags |= O_DSYNC;
  42128. + spin_unlock(&curlun->filp->f_lock);
  42129. + }
  42130. + }
  42131. + if (lba >= curlun->num_sectors) {
  42132. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42133. + return -EINVAL;
  42134. + }
  42135. +
  42136. + /* Carry out the file writes */
  42137. + get_some_more = 1;
  42138. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  42139. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  42140. +
  42141. + while (amount_left_to_write > 0) {
  42142. +
  42143. + /* Queue a request for more data from the host */
  42144. + bh = fsg->next_buffhd_to_fill;
  42145. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  42146. +
  42147. + /* Figure out how much we want to get:
  42148. + * Try to get the remaining amount,
  42149. + * but not more than the buffer size.
  42150. + */
  42151. + amount = min(amount_left_to_req, mod_data.buflen);
  42152. +
  42153. + /* Beyond the end of the backing file? */
  42154. + if (usb_offset >= curlun->file_length) {
  42155. + get_some_more = 0;
  42156. + curlun->sense_data =
  42157. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42158. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  42159. + curlun->info_valid = 1;
  42160. + continue;
  42161. + }
  42162. +
  42163. + /* Get the next buffer */
  42164. + usb_offset += amount;
  42165. + fsg->usb_amount_left -= amount;
  42166. + amount_left_to_req -= amount;
  42167. + if (amount_left_to_req == 0)
  42168. + get_some_more = 0;
  42169. +
  42170. + /* Except at the end of the transfer, amount will be
  42171. + * equal to the buffer size, which is divisible by
  42172. + * the bulk-out maxpacket size.
  42173. + */
  42174. + set_bulk_out_req_length(fsg, bh, amount);
  42175. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  42176. + &bh->outreq_busy, &bh->state);
  42177. + fsg->next_buffhd_to_fill = bh->next;
  42178. + continue;
  42179. + }
  42180. +
  42181. + /* Write the received data to the backing file */
  42182. + bh = fsg->next_buffhd_to_drain;
  42183. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  42184. + break; // We stopped early
  42185. + if (bh->state == BUF_STATE_FULL) {
  42186. + smp_rmb();
  42187. + fsg->next_buffhd_to_drain = bh->next;
  42188. + bh->state = BUF_STATE_EMPTY;
  42189. +
  42190. + /* Did something go wrong with the transfer? */
  42191. + if (bh->outreq->status != 0) {
  42192. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  42193. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42194. + curlun->info_valid = 1;
  42195. + break;
  42196. + }
  42197. +
  42198. + amount = bh->outreq->actual;
  42199. + if (curlun->file_length - file_offset < amount) {
  42200. + LERROR(curlun,
  42201. + "write %u @ %llu beyond end %llu\n",
  42202. + amount, (unsigned long long) file_offset,
  42203. + (unsigned long long) curlun->file_length);
  42204. + amount = curlun->file_length - file_offset;
  42205. + }
  42206. +
  42207. + /* Don't accept excess data. The spec doesn't say
  42208. + * what to do in this case. We'll ignore the error.
  42209. + */
  42210. + amount = min(amount, bh->bulk_out_intended_length);
  42211. +
  42212. + /* Don't write a partial block */
  42213. + amount = round_down(amount, curlun->blksize);
  42214. + if (amount == 0)
  42215. + goto empty_write;
  42216. +
  42217. + /* Perform the write */
  42218. + file_offset_tmp = file_offset;
  42219. + nwritten = vfs_write(curlun->filp,
  42220. + (char __user *) bh->buf,
  42221. + amount, &file_offset_tmp);
  42222. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  42223. + (unsigned long long) file_offset,
  42224. + (int) nwritten);
  42225. + if (signal_pending(current))
  42226. + return -EINTR; // Interrupted!
  42227. +
  42228. + if (nwritten < 0) {
  42229. + LDBG(curlun, "error in file write: %d\n",
  42230. + (int) nwritten);
  42231. + nwritten = 0;
  42232. + } else if (nwritten < amount) {
  42233. + LDBG(curlun, "partial file write: %d/%u\n",
  42234. + (int) nwritten, amount);
  42235. + nwritten = round_down(nwritten, curlun->blksize);
  42236. + }
  42237. + file_offset += nwritten;
  42238. + amount_left_to_write -= nwritten;
  42239. + fsg->residue -= nwritten;
  42240. +
  42241. + /* If an error occurred, report it and its position */
  42242. + if (nwritten < amount) {
  42243. + curlun->sense_data = SS_WRITE_ERROR;
  42244. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42245. + curlun->info_valid = 1;
  42246. + break;
  42247. + }
  42248. +
  42249. + empty_write:
  42250. + /* Did the host decide to stop early? */
  42251. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  42252. + fsg->short_packet_received = 1;
  42253. + break;
  42254. + }
  42255. + continue;
  42256. + }
  42257. +
  42258. + /* Wait for something to happen */
  42259. + rc = sleep_thread(fsg);
  42260. + if (rc)
  42261. + return rc;
  42262. + }
  42263. +
  42264. + return -EIO; // No default reply
  42265. +}
  42266. +
  42267. +
  42268. +/*-------------------------------------------------------------------------*/
  42269. +
  42270. +static int do_synchronize_cache(struct fsg_dev *fsg)
  42271. +{
  42272. + struct fsg_lun *curlun = fsg->curlun;
  42273. + int rc;
  42274. +
  42275. + /* We ignore the requested LBA and write out all file's
  42276. + * dirty data buffers. */
  42277. + rc = fsg_lun_fsync_sub(curlun);
  42278. + if (rc)
  42279. + curlun->sense_data = SS_WRITE_ERROR;
  42280. + return 0;
  42281. +}
  42282. +
  42283. +
  42284. +/*-------------------------------------------------------------------------*/
  42285. +
  42286. +static void invalidate_sub(struct fsg_lun *curlun)
  42287. +{
  42288. + struct file *filp = curlun->filp;
  42289. + struct inode *inode = filp->f_path.dentry->d_inode;
  42290. + unsigned long rc;
  42291. +
  42292. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  42293. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  42294. +}
  42295. +
  42296. +static int do_verify(struct fsg_dev *fsg)
  42297. +{
  42298. + struct fsg_lun *curlun = fsg->curlun;
  42299. + u32 lba;
  42300. + u32 verification_length;
  42301. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  42302. + loff_t file_offset, file_offset_tmp;
  42303. + u32 amount_left;
  42304. + unsigned int amount;
  42305. + ssize_t nread;
  42306. +
  42307. + /* Get the starting Logical Block Address and check that it's
  42308. + * not too big */
  42309. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  42310. + if (lba >= curlun->num_sectors) {
  42311. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42312. + return -EINVAL;
  42313. + }
  42314. +
  42315. + /* We allow DPO (Disable Page Out = don't save data in the
  42316. + * cache) but we don't implement it. */
  42317. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  42318. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42319. + return -EINVAL;
  42320. + }
  42321. +
  42322. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  42323. + if (unlikely(verification_length == 0))
  42324. + return -EIO; // No default reply
  42325. +
  42326. + /* Prepare to carry out the file verify */
  42327. + amount_left = verification_length << curlun->blkbits;
  42328. + file_offset = ((loff_t) lba) << curlun->blkbits;
  42329. +
  42330. + /* Write out all the dirty buffers before invalidating them */
  42331. + fsg_lun_fsync_sub(curlun);
  42332. + if (signal_pending(current))
  42333. + return -EINTR;
  42334. +
  42335. + invalidate_sub(curlun);
  42336. + if (signal_pending(current))
  42337. + return -EINTR;
  42338. +
  42339. + /* Just try to read the requested blocks */
  42340. + while (amount_left > 0) {
  42341. +
  42342. + /* Figure out how much we need to read:
  42343. + * Try to read the remaining amount, but not more than
  42344. + * the buffer size.
  42345. + * And don't try to read past the end of the file.
  42346. + */
  42347. + amount = min((unsigned int) amount_left, mod_data.buflen);
  42348. + amount = min((loff_t) amount,
  42349. + curlun->file_length - file_offset);
  42350. + if (amount == 0) {
  42351. + curlun->sense_data =
  42352. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42353. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42354. + curlun->info_valid = 1;
  42355. + break;
  42356. + }
  42357. +
  42358. + /* Perform the read */
  42359. + file_offset_tmp = file_offset;
  42360. + nread = vfs_read(curlun->filp,
  42361. + (char __user *) bh->buf,
  42362. + amount, &file_offset_tmp);
  42363. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  42364. + (unsigned long long) file_offset,
  42365. + (int) nread);
  42366. + if (signal_pending(current))
  42367. + return -EINTR;
  42368. +
  42369. + if (nread < 0) {
  42370. + LDBG(curlun, "error in file verify: %d\n",
  42371. + (int) nread);
  42372. + nread = 0;
  42373. + } else if (nread < amount) {
  42374. + LDBG(curlun, "partial file verify: %d/%u\n",
  42375. + (int) nread, amount);
  42376. + nread = round_down(nread, curlun->blksize);
  42377. + }
  42378. + if (nread == 0) {
  42379. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  42380. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42381. + curlun->info_valid = 1;
  42382. + break;
  42383. + }
  42384. + file_offset += nread;
  42385. + amount_left -= nread;
  42386. + }
  42387. + return 0;
  42388. +}
  42389. +
  42390. +
  42391. +/*-------------------------------------------------------------------------*/
  42392. +
  42393. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42394. +{
  42395. + u8 *buf = (u8 *) bh->buf;
  42396. +
  42397. + static char vendor_id[] = "Linux ";
  42398. + static char product_disk_id[] = "File-Stor Gadget";
  42399. + static char product_cdrom_id[] = "File-CD Gadget ";
  42400. +
  42401. + if (!fsg->curlun) { // Unsupported LUNs are okay
  42402. + fsg->bad_lun_okay = 1;
  42403. + memset(buf, 0, 36);
  42404. + buf[0] = 0x7f; // Unsupported, no device-type
  42405. + buf[4] = 31; // Additional length
  42406. + return 36;
  42407. + }
  42408. +
  42409. + memset(buf, 0, 8);
  42410. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  42411. + if (mod_data.removable)
  42412. + buf[1] = 0x80;
  42413. + buf[2] = 2; // ANSI SCSI level 2
  42414. + buf[3] = 2; // SCSI-2 INQUIRY data format
  42415. + buf[4] = 31; // Additional length
  42416. + // No special options
  42417. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  42418. + (mod_data.cdrom ? product_cdrom_id :
  42419. + product_disk_id),
  42420. + mod_data.release);
  42421. + return 36;
  42422. +}
  42423. +
  42424. +
  42425. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42426. +{
  42427. + struct fsg_lun *curlun = fsg->curlun;
  42428. + u8 *buf = (u8 *) bh->buf;
  42429. + u32 sd, sdinfo;
  42430. + int valid;
  42431. +
  42432. + /*
  42433. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  42434. + *
  42435. + * If a REQUEST SENSE command is received from an initiator
  42436. + * with a pending unit attention condition (before the target
  42437. + * generates the contingent allegiance condition), then the
  42438. + * target shall either:
  42439. + * a) report any pending sense data and preserve the unit
  42440. + * attention condition on the logical unit, or,
  42441. + * b) report the unit attention condition, may discard any
  42442. + * pending sense data, and clear the unit attention
  42443. + * condition on the logical unit for that initiator.
  42444. + *
  42445. + * FSG normally uses option a); enable this code to use option b).
  42446. + */
  42447. +#if 0
  42448. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  42449. + curlun->sense_data = curlun->unit_attention_data;
  42450. + curlun->unit_attention_data = SS_NO_SENSE;
  42451. + }
  42452. +#endif
  42453. +
  42454. + if (!curlun) { // Unsupported LUNs are okay
  42455. + fsg->bad_lun_okay = 1;
  42456. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  42457. + sdinfo = 0;
  42458. + valid = 0;
  42459. + } else {
  42460. + sd = curlun->sense_data;
  42461. + sdinfo = curlun->sense_data_info;
  42462. + valid = curlun->info_valid << 7;
  42463. + curlun->sense_data = SS_NO_SENSE;
  42464. + curlun->sense_data_info = 0;
  42465. + curlun->info_valid = 0;
  42466. + }
  42467. +
  42468. + memset(buf, 0, 18);
  42469. + buf[0] = valid | 0x70; // Valid, current error
  42470. + buf[2] = SK(sd);
  42471. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  42472. + buf[7] = 18 - 8; // Additional sense length
  42473. + buf[12] = ASC(sd);
  42474. + buf[13] = ASCQ(sd);
  42475. + return 18;
  42476. +}
  42477. +
  42478. +
  42479. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42480. +{
  42481. + struct fsg_lun *curlun = fsg->curlun;
  42482. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  42483. + int pmi = fsg->cmnd[8];
  42484. + u8 *buf = (u8 *) bh->buf;
  42485. +
  42486. + /* Check the PMI and LBA fields */
  42487. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  42488. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42489. + return -EINVAL;
  42490. + }
  42491. +
  42492. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  42493. + /* Max logical block */
  42494. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  42495. + return 8;
  42496. +}
  42497. +
  42498. +
  42499. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42500. +{
  42501. + struct fsg_lun *curlun = fsg->curlun;
  42502. + int msf = fsg->cmnd[1] & 0x02;
  42503. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  42504. + u8 *buf = (u8 *) bh->buf;
  42505. +
  42506. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  42507. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42508. + return -EINVAL;
  42509. + }
  42510. + if (lba >= curlun->num_sectors) {
  42511. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42512. + return -EINVAL;
  42513. + }
  42514. +
  42515. + memset(buf, 0, 8);
  42516. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  42517. + store_cdrom_address(&buf[4], msf, lba);
  42518. + return 8;
  42519. +}
  42520. +
  42521. +
  42522. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42523. +{
  42524. + struct fsg_lun *curlun = fsg->curlun;
  42525. + int msf = fsg->cmnd[1] & 0x02;
  42526. + int start_track = fsg->cmnd[6];
  42527. + u8 *buf = (u8 *) bh->buf;
  42528. +
  42529. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  42530. + start_track > 1) {
  42531. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42532. + return -EINVAL;
  42533. + }
  42534. +
  42535. + memset(buf, 0, 20);
  42536. + buf[1] = (20-2); /* TOC data length */
  42537. + buf[2] = 1; /* First track number */
  42538. + buf[3] = 1; /* Last track number */
  42539. + buf[5] = 0x16; /* Data track, copying allowed */
  42540. + buf[6] = 0x01; /* Only track is number 1 */
  42541. + store_cdrom_address(&buf[8], msf, 0);
  42542. +
  42543. + buf[13] = 0x16; /* Lead-out track is data */
  42544. + buf[14] = 0xAA; /* Lead-out track number */
  42545. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  42546. + return 20;
  42547. +}
  42548. +
  42549. +
  42550. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42551. +{
  42552. + struct fsg_lun *curlun = fsg->curlun;
  42553. + int mscmnd = fsg->cmnd[0];
  42554. + u8 *buf = (u8 *) bh->buf;
  42555. + u8 *buf0 = buf;
  42556. + int pc, page_code;
  42557. + int changeable_values, all_pages;
  42558. + int valid_page = 0;
  42559. + int len, limit;
  42560. +
  42561. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  42562. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42563. + return -EINVAL;
  42564. + }
  42565. + pc = fsg->cmnd[2] >> 6;
  42566. + page_code = fsg->cmnd[2] & 0x3f;
  42567. + if (pc == 3) {
  42568. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  42569. + return -EINVAL;
  42570. + }
  42571. + changeable_values = (pc == 1);
  42572. + all_pages = (page_code == 0x3f);
  42573. +
  42574. + /* Write the mode parameter header. Fixed values are: default
  42575. + * medium type, no cache control (DPOFUA), and no block descriptors.
  42576. + * The only variable value is the WriteProtect bit. We will fill in
  42577. + * the mode data length later. */
  42578. + memset(buf, 0, 8);
  42579. + if (mscmnd == MODE_SENSE) {
  42580. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  42581. + buf += 4;
  42582. + limit = 255;
  42583. + } else { // MODE_SENSE_10
  42584. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  42585. + buf += 8;
  42586. + limit = 65535; // Should really be mod_data.buflen
  42587. + }
  42588. +
  42589. + /* No block descriptors */
  42590. +
  42591. + /* The mode pages, in numerical order. The only page we support
  42592. + * is the Caching page. */
  42593. + if (page_code == 0x08 || all_pages) {
  42594. + valid_page = 1;
  42595. + buf[0] = 0x08; // Page code
  42596. + buf[1] = 10; // Page length
  42597. + memset(buf+2, 0, 10); // None of the fields are changeable
  42598. +
  42599. + if (!changeable_values) {
  42600. + buf[2] = 0x04; // Write cache enable,
  42601. + // Read cache not disabled
  42602. + // No cache retention priorities
  42603. + put_unaligned_be16(0xffff, &buf[4]);
  42604. + /* Don't disable prefetch */
  42605. + /* Minimum prefetch = 0 */
  42606. + put_unaligned_be16(0xffff, &buf[8]);
  42607. + /* Maximum prefetch */
  42608. + put_unaligned_be16(0xffff, &buf[10]);
  42609. + /* Maximum prefetch ceiling */
  42610. + }
  42611. + buf += 12;
  42612. + }
  42613. +
  42614. + /* Check that a valid page was requested and the mode data length
  42615. + * isn't too long. */
  42616. + len = buf - buf0;
  42617. + if (!valid_page || len > limit) {
  42618. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42619. + return -EINVAL;
  42620. + }
  42621. +
  42622. + /* Store the mode data length */
  42623. + if (mscmnd == MODE_SENSE)
  42624. + buf0[0] = len - 1;
  42625. + else
  42626. + put_unaligned_be16(len - 2, buf0);
  42627. + return len;
  42628. +}
  42629. +
  42630. +
  42631. +static int do_start_stop(struct fsg_dev *fsg)
  42632. +{
  42633. + struct fsg_lun *curlun = fsg->curlun;
  42634. + int loej, start;
  42635. +
  42636. + if (!mod_data.removable) {
  42637. + curlun->sense_data = SS_INVALID_COMMAND;
  42638. + return -EINVAL;
  42639. + }
  42640. +
  42641. + // int immed = fsg->cmnd[1] & 0x01;
  42642. + loej = fsg->cmnd[4] & 0x02;
  42643. + start = fsg->cmnd[4] & 0x01;
  42644. +
  42645. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  42646. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  42647. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  42648. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42649. + return -EINVAL;
  42650. + }
  42651. +
  42652. + if (!start) {
  42653. +
  42654. + /* Are we allowed to unload the media? */
  42655. + if (curlun->prevent_medium_removal) {
  42656. + LDBG(curlun, "unload attempt prevented\n");
  42657. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  42658. + return -EINVAL;
  42659. + }
  42660. + if (loej) { // Simulate an unload/eject
  42661. + up_read(&fsg->filesem);
  42662. + down_write(&fsg->filesem);
  42663. + fsg_lun_close(curlun);
  42664. + up_write(&fsg->filesem);
  42665. + down_read(&fsg->filesem);
  42666. + }
  42667. + } else {
  42668. +
  42669. + /* Our emulation doesn't support mounting; the medium is
  42670. + * available for use as soon as it is loaded. */
  42671. + if (!fsg_lun_is_open(curlun)) {
  42672. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  42673. + return -EINVAL;
  42674. + }
  42675. + }
  42676. +#endif
  42677. + return 0;
  42678. +}
  42679. +
  42680. +
  42681. +static int do_prevent_allow(struct fsg_dev *fsg)
  42682. +{
  42683. + struct fsg_lun *curlun = fsg->curlun;
  42684. + int prevent;
  42685. +
  42686. + if (!mod_data.removable) {
  42687. + curlun->sense_data = SS_INVALID_COMMAND;
  42688. + return -EINVAL;
  42689. + }
  42690. +
  42691. + prevent = fsg->cmnd[4] & 0x01;
  42692. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  42693. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42694. + return -EINVAL;
  42695. + }
  42696. +
  42697. + if (curlun->prevent_medium_removal && !prevent)
  42698. + fsg_lun_fsync_sub(curlun);
  42699. + curlun->prevent_medium_removal = prevent;
  42700. + return 0;
  42701. +}
  42702. +
  42703. +
  42704. +static int do_read_format_capacities(struct fsg_dev *fsg,
  42705. + struct fsg_buffhd *bh)
  42706. +{
  42707. + struct fsg_lun *curlun = fsg->curlun;
  42708. + u8 *buf = (u8 *) bh->buf;
  42709. +
  42710. + buf[0] = buf[1] = buf[2] = 0;
  42711. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  42712. + buf += 4;
  42713. +
  42714. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  42715. + /* Number of blocks */
  42716. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  42717. + buf[4] = 0x02; /* Current capacity */
  42718. + return 12;
  42719. +}
  42720. +
  42721. +
  42722. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42723. +{
  42724. + struct fsg_lun *curlun = fsg->curlun;
  42725. +
  42726. + /* We don't support MODE SELECT */
  42727. + curlun->sense_data = SS_INVALID_COMMAND;
  42728. + return -EINVAL;
  42729. +}
  42730. +
  42731. +
  42732. +/*-------------------------------------------------------------------------*/
  42733. +
  42734. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  42735. +{
  42736. + int rc;
  42737. +
  42738. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  42739. + if (rc == -EAGAIN)
  42740. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  42741. + while (rc != 0) {
  42742. + if (rc != -EAGAIN) {
  42743. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  42744. + rc = 0;
  42745. + break;
  42746. + }
  42747. +
  42748. + /* Wait for a short time and then try again */
  42749. + if (msleep_interruptible(100) != 0)
  42750. + return -EINTR;
  42751. + rc = usb_ep_set_halt(fsg->bulk_in);
  42752. + }
  42753. + return rc;
  42754. +}
  42755. +
  42756. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  42757. +{
  42758. + int rc;
  42759. +
  42760. + DBG(fsg, "bulk-in set wedge\n");
  42761. + rc = usb_ep_set_wedge(fsg->bulk_in);
  42762. + if (rc == -EAGAIN)
  42763. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  42764. + while (rc != 0) {
  42765. + if (rc != -EAGAIN) {
  42766. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  42767. + rc = 0;
  42768. + break;
  42769. + }
  42770. +
  42771. + /* Wait for a short time and then try again */
  42772. + if (msleep_interruptible(100) != 0)
  42773. + return -EINTR;
  42774. + rc = usb_ep_set_wedge(fsg->bulk_in);
  42775. + }
  42776. + return rc;
  42777. +}
  42778. +
  42779. +static int throw_away_data(struct fsg_dev *fsg)
  42780. +{
  42781. + struct fsg_buffhd *bh;
  42782. + u32 amount;
  42783. + int rc;
  42784. +
  42785. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  42786. + fsg->usb_amount_left > 0) {
  42787. +
  42788. + /* Throw away the data in a filled buffer */
  42789. + if (bh->state == BUF_STATE_FULL) {
  42790. + smp_rmb();
  42791. + bh->state = BUF_STATE_EMPTY;
  42792. + fsg->next_buffhd_to_drain = bh->next;
  42793. +
  42794. + /* A short packet or an error ends everything */
  42795. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  42796. + bh->outreq->status != 0) {
  42797. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  42798. + return -EINTR;
  42799. + }
  42800. + continue;
  42801. + }
  42802. +
  42803. + /* Try to submit another request if we need one */
  42804. + bh = fsg->next_buffhd_to_fill;
  42805. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  42806. + amount = min(fsg->usb_amount_left,
  42807. + (u32) mod_data.buflen);
  42808. +
  42809. + /* Except at the end of the transfer, amount will be
  42810. + * equal to the buffer size, which is divisible by
  42811. + * the bulk-out maxpacket size.
  42812. + */
  42813. + set_bulk_out_req_length(fsg, bh, amount);
  42814. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  42815. + &bh->outreq_busy, &bh->state);
  42816. + fsg->next_buffhd_to_fill = bh->next;
  42817. + fsg->usb_amount_left -= amount;
  42818. + continue;
  42819. + }
  42820. +
  42821. + /* Otherwise wait for something to happen */
  42822. + rc = sleep_thread(fsg);
  42823. + if (rc)
  42824. + return rc;
  42825. + }
  42826. + return 0;
  42827. +}
  42828. +
  42829. +
  42830. +static int finish_reply(struct fsg_dev *fsg)
  42831. +{
  42832. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  42833. + int rc = 0;
  42834. +
  42835. + switch (fsg->data_dir) {
  42836. + case DATA_DIR_NONE:
  42837. + break; // Nothing to send
  42838. +
  42839. + /* If we don't know whether the host wants to read or write,
  42840. + * this must be CB or CBI with an unknown command. We mustn't
  42841. + * try to send or receive any data. So stall both bulk pipes
  42842. + * if we can and wait for a reset. */
  42843. + case DATA_DIR_UNKNOWN:
  42844. + if (mod_data.can_stall) {
  42845. + fsg_set_halt(fsg, fsg->bulk_out);
  42846. + rc = halt_bulk_in_endpoint(fsg);
  42847. + }
  42848. + break;
  42849. +
  42850. + /* All but the last buffer of data must have already been sent */
  42851. + case DATA_DIR_TO_HOST:
  42852. + if (fsg->data_size == 0)
  42853. + ; // Nothing to send
  42854. +
  42855. + /* If there's no residue, simply send the last buffer */
  42856. + else if (fsg->residue == 0) {
  42857. + bh->inreq->zero = 0;
  42858. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  42859. + &bh->inreq_busy, &bh->state);
  42860. + fsg->next_buffhd_to_fill = bh->next;
  42861. + }
  42862. +
  42863. + /* There is a residue. For CB and CBI, simply mark the end
  42864. + * of the data with a short packet. However, if we are
  42865. + * allowed to stall, there was no data at all (residue ==
  42866. + * data_size), and the command failed (invalid LUN or
  42867. + * sense data is set), then halt the bulk-in endpoint
  42868. + * instead. */
  42869. + else if (!transport_is_bbb()) {
  42870. + if (mod_data.can_stall &&
  42871. + fsg->residue == fsg->data_size &&
  42872. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  42873. + bh->state = BUF_STATE_EMPTY;
  42874. + rc = halt_bulk_in_endpoint(fsg);
  42875. + } else {
  42876. + bh->inreq->zero = 1;
  42877. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  42878. + &bh->inreq_busy, &bh->state);
  42879. + fsg->next_buffhd_to_fill = bh->next;
  42880. + }
  42881. + }
  42882. +
  42883. + /*
  42884. + * For Bulk-only, mark the end of the data with a short
  42885. + * packet. If we are allowed to stall, halt the bulk-in
  42886. + * endpoint. (Note: This violates the Bulk-Only Transport
  42887. + * specification, which requires us to pad the data if we
  42888. + * don't halt the endpoint. Presumably nobody will mind.)
  42889. + */
  42890. + else {
  42891. + bh->inreq->zero = 1;
  42892. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  42893. + &bh->inreq_busy, &bh->state);
  42894. + fsg->next_buffhd_to_fill = bh->next;
  42895. + if (mod_data.can_stall)
  42896. + rc = halt_bulk_in_endpoint(fsg);
  42897. + }
  42898. + break;
  42899. +
  42900. + /* We have processed all we want from the data the host has sent.
  42901. + * There may still be outstanding bulk-out requests. */
  42902. + case DATA_DIR_FROM_HOST:
  42903. + if (fsg->residue == 0)
  42904. + ; // Nothing to receive
  42905. +
  42906. + /* Did the host stop sending unexpectedly early? */
  42907. + else if (fsg->short_packet_received) {
  42908. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  42909. + rc = -EINTR;
  42910. + }
  42911. +
  42912. + /* We haven't processed all the incoming data. Even though
  42913. + * we may be allowed to stall, doing so would cause a race.
  42914. + * The controller may already have ACK'ed all the remaining
  42915. + * bulk-out packets, in which case the host wouldn't see a
  42916. + * STALL. Not realizing the endpoint was halted, it wouldn't
  42917. + * clear the halt -- leading to problems later on. */
  42918. +#if 0
  42919. + else if (mod_data.can_stall) {
  42920. + fsg_set_halt(fsg, fsg->bulk_out);
  42921. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  42922. + rc = -EINTR;
  42923. + }
  42924. +#endif
  42925. +
  42926. + /* We can't stall. Read in the excess data and throw it
  42927. + * all away. */
  42928. + else
  42929. + rc = throw_away_data(fsg);
  42930. + break;
  42931. + }
  42932. + return rc;
  42933. +}
  42934. +
  42935. +
  42936. +static int send_status(struct fsg_dev *fsg)
  42937. +{
  42938. + struct fsg_lun *curlun = fsg->curlun;
  42939. + struct fsg_buffhd *bh;
  42940. + int rc;
  42941. + u8 status = US_BULK_STAT_OK;
  42942. + u32 sd, sdinfo = 0;
  42943. +
  42944. + /* Wait for the next buffer to become available */
  42945. + bh = fsg->next_buffhd_to_fill;
  42946. + while (bh->state != BUF_STATE_EMPTY) {
  42947. + rc = sleep_thread(fsg);
  42948. + if (rc)
  42949. + return rc;
  42950. + }
  42951. +
  42952. + if (curlun) {
  42953. + sd = curlun->sense_data;
  42954. + sdinfo = curlun->sense_data_info;
  42955. + } else if (fsg->bad_lun_okay)
  42956. + sd = SS_NO_SENSE;
  42957. + else
  42958. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  42959. +
  42960. + if (fsg->phase_error) {
  42961. + DBG(fsg, "sending phase-error status\n");
  42962. + status = US_BULK_STAT_PHASE;
  42963. + sd = SS_INVALID_COMMAND;
  42964. + } else if (sd != SS_NO_SENSE) {
  42965. + DBG(fsg, "sending command-failure status\n");
  42966. + status = US_BULK_STAT_FAIL;
  42967. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  42968. + " info x%x\n",
  42969. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  42970. + }
  42971. +
  42972. + if (transport_is_bbb()) {
  42973. + struct bulk_cs_wrap *csw = bh->buf;
  42974. +
  42975. + /* Store and send the Bulk-only CSW */
  42976. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  42977. + csw->Tag = fsg->tag;
  42978. + csw->Residue = cpu_to_le32(fsg->residue);
  42979. + csw->Status = status;
  42980. +
  42981. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  42982. + bh->inreq->zero = 0;
  42983. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  42984. + &bh->inreq_busy, &bh->state);
  42985. +
  42986. + } else if (mod_data.transport_type == USB_PR_CB) {
  42987. +
  42988. + /* Control-Bulk transport has no status phase! */
  42989. + return 0;
  42990. +
  42991. + } else { // USB_PR_CBI
  42992. + struct interrupt_data *buf = bh->buf;
  42993. +
  42994. + /* Store and send the Interrupt data. UFI sends the ASC
  42995. + * and ASCQ bytes. Everything else sends a Type (which
  42996. + * is always 0) and the status Value. */
  42997. + if (mod_data.protocol_type == USB_SC_UFI) {
  42998. + buf->bType = ASC(sd);
  42999. + buf->bValue = ASCQ(sd);
  43000. + } else {
  43001. + buf->bType = 0;
  43002. + buf->bValue = status;
  43003. + }
  43004. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  43005. +
  43006. + fsg->intr_buffhd = bh; // Point to the right buffhd
  43007. + fsg->intreq->buf = bh->inreq->buf;
  43008. + fsg->intreq->context = bh;
  43009. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  43010. + &fsg->intreq_busy, &bh->state);
  43011. + }
  43012. +
  43013. + fsg->next_buffhd_to_fill = bh->next;
  43014. + return 0;
  43015. +}
  43016. +
  43017. +
  43018. +/*-------------------------------------------------------------------------*/
  43019. +
  43020. +/* Check whether the command is properly formed and whether its data size
  43021. + * and direction agree with the values we already have. */
  43022. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  43023. + enum data_direction data_dir, unsigned int mask,
  43024. + int needs_medium, const char *name)
  43025. +{
  43026. + int i;
  43027. + int lun = fsg->cmnd[1] >> 5;
  43028. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  43029. + char hdlen[20];
  43030. + struct fsg_lun *curlun;
  43031. +
  43032. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  43033. + * Transparent SCSI doesn't pad. */
  43034. + if (protocol_is_scsi())
  43035. + ;
  43036. +
  43037. + /* There's some disagreement as to whether RBC pads commands or not.
  43038. + * We'll play it safe and accept either form. */
  43039. + else if (mod_data.protocol_type == USB_SC_RBC) {
  43040. + if (fsg->cmnd_size == 12)
  43041. + cmnd_size = 12;
  43042. +
  43043. + /* All the other protocols pad to 12 bytes */
  43044. + } else
  43045. + cmnd_size = 12;
  43046. +
  43047. + hdlen[0] = 0;
  43048. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  43049. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  43050. + fsg->data_size);
  43051. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  43052. + name, cmnd_size, dirletter[(int) data_dir],
  43053. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  43054. +
  43055. + /* We can't reply at all until we know the correct data direction
  43056. + * and size. */
  43057. + if (fsg->data_size_from_cmnd == 0)
  43058. + data_dir = DATA_DIR_NONE;
  43059. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  43060. + fsg->data_dir = data_dir;
  43061. + fsg->data_size = fsg->data_size_from_cmnd;
  43062. +
  43063. + } else { // Bulk-only
  43064. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  43065. +
  43066. + /* Host data size < Device data size is a phase error.
  43067. + * Carry out the command, but only transfer as much
  43068. + * as we are allowed. */
  43069. + fsg->data_size_from_cmnd = fsg->data_size;
  43070. + fsg->phase_error = 1;
  43071. + }
  43072. + }
  43073. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  43074. +
  43075. + /* Conflicting data directions is a phase error */
  43076. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  43077. + fsg->phase_error = 1;
  43078. + return -EINVAL;
  43079. + }
  43080. +
  43081. + /* Verify the length of the command itself */
  43082. + if (cmnd_size != fsg->cmnd_size) {
  43083. +
  43084. + /* Special case workaround: There are plenty of buggy SCSI
  43085. + * implementations. Many have issues with cbw->Length
  43086. + * field passing a wrong command size. For those cases we
  43087. + * always try to work around the problem by using the length
  43088. + * sent by the host side provided it is at least as large
  43089. + * as the correct command length.
  43090. + * Examples of such cases would be MS-Windows, which issues
  43091. + * REQUEST SENSE with cbw->Length == 12 where it should
  43092. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  43093. + * REQUEST SENSE with cbw->Length == 10 where it should
  43094. + * be 6 as well.
  43095. + */
  43096. + if (cmnd_size <= fsg->cmnd_size) {
  43097. + DBG(fsg, "%s is buggy! Expected length %d "
  43098. + "but we got %d\n", name,
  43099. + cmnd_size, fsg->cmnd_size);
  43100. + cmnd_size = fsg->cmnd_size;
  43101. + } else {
  43102. + fsg->phase_error = 1;
  43103. + return -EINVAL;
  43104. + }
  43105. + }
  43106. +
  43107. + /* Check that the LUN values are consistent */
  43108. + if (transport_is_bbb()) {
  43109. + if (fsg->lun != lun)
  43110. + DBG(fsg, "using LUN %d from CBW, "
  43111. + "not LUN %d from CDB\n",
  43112. + fsg->lun, lun);
  43113. + }
  43114. +
  43115. + /* Check the LUN */
  43116. + curlun = fsg->curlun;
  43117. + if (curlun) {
  43118. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  43119. + curlun->sense_data = SS_NO_SENSE;
  43120. + curlun->sense_data_info = 0;
  43121. + curlun->info_valid = 0;
  43122. + }
  43123. + } else {
  43124. + fsg->bad_lun_okay = 0;
  43125. +
  43126. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  43127. + * to use unsupported LUNs; all others may not. */
  43128. + if (fsg->cmnd[0] != INQUIRY &&
  43129. + fsg->cmnd[0] != REQUEST_SENSE) {
  43130. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  43131. + return -EINVAL;
  43132. + }
  43133. + }
  43134. +
  43135. + /* If a unit attention condition exists, only INQUIRY and
  43136. + * REQUEST SENSE commands are allowed; anything else must fail. */
  43137. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  43138. + fsg->cmnd[0] != INQUIRY &&
  43139. + fsg->cmnd[0] != REQUEST_SENSE) {
  43140. + curlun->sense_data = curlun->unit_attention_data;
  43141. + curlun->unit_attention_data = SS_NO_SENSE;
  43142. + return -EINVAL;
  43143. + }
  43144. +
  43145. + /* Check that only command bytes listed in the mask are non-zero */
  43146. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  43147. + for (i = 1; i < cmnd_size; ++i) {
  43148. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  43149. + if (curlun)
  43150. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43151. + return -EINVAL;
  43152. + }
  43153. + }
  43154. +
  43155. + /* If the medium isn't mounted and the command needs to access
  43156. + * it, return an error. */
  43157. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  43158. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  43159. + return -EINVAL;
  43160. + }
  43161. +
  43162. + return 0;
  43163. +}
  43164. +
  43165. +/* wrapper of check_command for data size in blocks handling */
  43166. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  43167. + enum data_direction data_dir, unsigned int mask,
  43168. + int needs_medium, const char *name)
  43169. +{
  43170. + if (fsg->curlun)
  43171. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  43172. + return check_command(fsg, cmnd_size, data_dir,
  43173. + mask, needs_medium, name);
  43174. +}
  43175. +
  43176. +static int do_scsi_command(struct fsg_dev *fsg)
  43177. +{
  43178. + struct fsg_buffhd *bh;
  43179. + int rc;
  43180. + int reply = -EINVAL;
  43181. + int i;
  43182. + static char unknown[16];
  43183. +
  43184. + dump_cdb(fsg);
  43185. +
  43186. + /* Wait for the next buffer to become available for data or status */
  43187. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  43188. + while (bh->state != BUF_STATE_EMPTY) {
  43189. + rc = sleep_thread(fsg);
  43190. + if (rc)
  43191. + return rc;
  43192. + }
  43193. + fsg->phase_error = 0;
  43194. + fsg->short_packet_received = 0;
  43195. +
  43196. + down_read(&fsg->filesem); // We're using the backing file
  43197. + switch (fsg->cmnd[0]) {
  43198. +
  43199. + case INQUIRY:
  43200. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  43201. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  43202. + (1<<4), 0,
  43203. + "INQUIRY")) == 0)
  43204. + reply = do_inquiry(fsg, bh);
  43205. + break;
  43206. +
  43207. + case MODE_SELECT:
  43208. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  43209. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  43210. + (1<<1) | (1<<4), 0,
  43211. + "MODE SELECT(6)")) == 0)
  43212. + reply = do_mode_select(fsg, bh);
  43213. + break;
  43214. +
  43215. + case MODE_SELECT_10:
  43216. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43217. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  43218. + (1<<1) | (3<<7), 0,
  43219. + "MODE SELECT(10)")) == 0)
  43220. + reply = do_mode_select(fsg, bh);
  43221. + break;
  43222. +
  43223. + case MODE_SENSE:
  43224. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  43225. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  43226. + (1<<1) | (1<<2) | (1<<4), 0,
  43227. + "MODE SENSE(6)")) == 0)
  43228. + reply = do_mode_sense(fsg, bh);
  43229. + break;
  43230. +
  43231. + case MODE_SENSE_10:
  43232. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43233. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  43234. + (1<<1) | (1<<2) | (3<<7), 0,
  43235. + "MODE SENSE(10)")) == 0)
  43236. + reply = do_mode_sense(fsg, bh);
  43237. + break;
  43238. +
  43239. + case ALLOW_MEDIUM_REMOVAL:
  43240. + fsg->data_size_from_cmnd = 0;
  43241. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  43242. + (1<<4), 0,
  43243. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  43244. + reply = do_prevent_allow(fsg);
  43245. + break;
  43246. +
  43247. + case READ_6:
  43248. + i = fsg->cmnd[4];
  43249. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  43250. + if ((reply = check_command_size_in_blocks(fsg, 6,
  43251. + DATA_DIR_TO_HOST,
  43252. + (7<<1) | (1<<4), 1,
  43253. + "READ(6)")) == 0)
  43254. + reply = do_read(fsg);
  43255. + break;
  43256. +
  43257. + case READ_10:
  43258. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43259. + if ((reply = check_command_size_in_blocks(fsg, 10,
  43260. + DATA_DIR_TO_HOST,
  43261. + (1<<1) | (0xf<<2) | (3<<7), 1,
  43262. + "READ(10)")) == 0)
  43263. + reply = do_read(fsg);
  43264. + break;
  43265. +
  43266. + case READ_12:
  43267. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  43268. + if ((reply = check_command_size_in_blocks(fsg, 12,
  43269. + DATA_DIR_TO_HOST,
  43270. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  43271. + "READ(12)")) == 0)
  43272. + reply = do_read(fsg);
  43273. + break;
  43274. +
  43275. + case READ_CAPACITY:
  43276. + fsg->data_size_from_cmnd = 8;
  43277. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  43278. + (0xf<<2) | (1<<8), 1,
  43279. + "READ CAPACITY")) == 0)
  43280. + reply = do_read_capacity(fsg, bh);
  43281. + break;
  43282. +
  43283. + case READ_HEADER:
  43284. + if (!mod_data.cdrom)
  43285. + goto unknown_cmnd;
  43286. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43287. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  43288. + (3<<7) | (0x1f<<1), 1,
  43289. + "READ HEADER")) == 0)
  43290. + reply = do_read_header(fsg, bh);
  43291. + break;
  43292. +
  43293. + case READ_TOC:
  43294. + if (!mod_data.cdrom)
  43295. + goto unknown_cmnd;
  43296. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43297. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  43298. + (7<<6) | (1<<1), 1,
  43299. + "READ TOC")) == 0)
  43300. + reply = do_read_toc(fsg, bh);
  43301. + break;
  43302. +
  43303. + case READ_FORMAT_CAPACITIES:
  43304. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43305. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  43306. + (3<<7), 1,
  43307. + "READ FORMAT CAPACITIES")) == 0)
  43308. + reply = do_read_format_capacities(fsg, bh);
  43309. + break;
  43310. +
  43311. + case REQUEST_SENSE:
  43312. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  43313. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  43314. + (1<<4), 0,
  43315. + "REQUEST SENSE")) == 0)
  43316. + reply = do_request_sense(fsg, bh);
  43317. + break;
  43318. +
  43319. + case START_STOP:
  43320. + fsg->data_size_from_cmnd = 0;
  43321. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  43322. + (1<<1) | (1<<4), 0,
  43323. + "START-STOP UNIT")) == 0)
  43324. + reply = do_start_stop(fsg);
  43325. + break;
  43326. +
  43327. + case SYNCHRONIZE_CACHE:
  43328. + fsg->data_size_from_cmnd = 0;
  43329. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  43330. + (0xf<<2) | (3<<7), 1,
  43331. + "SYNCHRONIZE CACHE")) == 0)
  43332. + reply = do_synchronize_cache(fsg);
  43333. + break;
  43334. +
  43335. + case TEST_UNIT_READY:
  43336. + fsg->data_size_from_cmnd = 0;
  43337. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  43338. + 0, 1,
  43339. + "TEST UNIT READY");
  43340. + break;
  43341. +
  43342. + /* Although optional, this command is used by MS-Windows. We
  43343. + * support a minimal version: BytChk must be 0. */
  43344. + case VERIFY:
  43345. + fsg->data_size_from_cmnd = 0;
  43346. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  43347. + (1<<1) | (0xf<<2) | (3<<7), 1,
  43348. + "VERIFY")) == 0)
  43349. + reply = do_verify(fsg);
  43350. + break;
  43351. +
  43352. + case WRITE_6:
  43353. + i = fsg->cmnd[4];
  43354. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  43355. + if ((reply = check_command_size_in_blocks(fsg, 6,
  43356. + DATA_DIR_FROM_HOST,
  43357. + (7<<1) | (1<<4), 1,
  43358. + "WRITE(6)")) == 0)
  43359. + reply = do_write(fsg);
  43360. + break;
  43361. +
  43362. + case WRITE_10:
  43363. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  43364. + if ((reply = check_command_size_in_blocks(fsg, 10,
  43365. + DATA_DIR_FROM_HOST,
  43366. + (1<<1) | (0xf<<2) | (3<<7), 1,
  43367. + "WRITE(10)")) == 0)
  43368. + reply = do_write(fsg);
  43369. + break;
  43370. +
  43371. + case WRITE_12:
  43372. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  43373. + if ((reply = check_command_size_in_blocks(fsg, 12,
  43374. + DATA_DIR_FROM_HOST,
  43375. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  43376. + "WRITE(12)")) == 0)
  43377. + reply = do_write(fsg);
  43378. + break;
  43379. +
  43380. + /* Some mandatory commands that we recognize but don't implement.
  43381. + * They don't mean much in this setting. It's left as an exercise
  43382. + * for anyone interested to implement RESERVE and RELEASE in terms
  43383. + * of Posix locks. */
  43384. + case FORMAT_UNIT:
  43385. + case RELEASE:
  43386. + case RESERVE:
  43387. + case SEND_DIAGNOSTIC:
  43388. + // Fall through
  43389. +
  43390. + default:
  43391. + unknown_cmnd:
  43392. + fsg->data_size_from_cmnd = 0;
  43393. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  43394. + if ((reply = check_command(fsg, fsg->cmnd_size,
  43395. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  43396. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  43397. + reply = -EINVAL;
  43398. + }
  43399. + break;
  43400. + }
  43401. + up_read(&fsg->filesem);
  43402. +
  43403. + if (reply == -EINTR || signal_pending(current))
  43404. + return -EINTR;
  43405. +
  43406. + /* Set up the single reply buffer for finish_reply() */
  43407. + if (reply == -EINVAL)
  43408. + reply = 0; // Error reply length
  43409. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  43410. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  43411. + bh->inreq->length = reply;
  43412. + bh->state = BUF_STATE_FULL;
  43413. + fsg->residue -= reply;
  43414. + } // Otherwise it's already set
  43415. +
  43416. + return 0;
  43417. +}
  43418. +
  43419. +
  43420. +/*-------------------------------------------------------------------------*/
  43421. +
  43422. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43423. +{
  43424. + struct usb_request *req = bh->outreq;
  43425. + struct bulk_cb_wrap *cbw = req->buf;
  43426. +
  43427. + /* Was this a real packet? Should it be ignored? */
  43428. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  43429. + return -EINVAL;
  43430. +
  43431. + /* Is the CBW valid? */
  43432. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  43433. + cbw->Signature != cpu_to_le32(
  43434. + US_BULK_CB_SIGN)) {
  43435. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  43436. + req->actual,
  43437. + le32_to_cpu(cbw->Signature));
  43438. +
  43439. + /* The Bulk-only spec says we MUST stall the IN endpoint
  43440. + * (6.6.1), so it's unavoidable. It also says we must
  43441. + * retain this state until the next reset, but there's
  43442. + * no way to tell the controller driver it should ignore
  43443. + * Clear-Feature(HALT) requests.
  43444. + *
  43445. + * We aren't required to halt the OUT endpoint; instead
  43446. + * we can simply accept and discard any data received
  43447. + * until the next reset. */
  43448. + wedge_bulk_in_endpoint(fsg);
  43449. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  43450. + return -EINVAL;
  43451. + }
  43452. +
  43453. + /* Is the CBW meaningful? */
  43454. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  43455. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  43456. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  43457. + "cmdlen %u\n",
  43458. + cbw->Lun, cbw->Flags, cbw->Length);
  43459. +
  43460. + /* We can do anything we want here, so let's stall the
  43461. + * bulk pipes if we are allowed to. */
  43462. + if (mod_data.can_stall) {
  43463. + fsg_set_halt(fsg, fsg->bulk_out);
  43464. + halt_bulk_in_endpoint(fsg);
  43465. + }
  43466. + return -EINVAL;
  43467. + }
  43468. +
  43469. + /* Save the command for later */
  43470. + fsg->cmnd_size = cbw->Length;
  43471. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  43472. + if (cbw->Flags & US_BULK_FLAG_IN)
  43473. + fsg->data_dir = DATA_DIR_TO_HOST;
  43474. + else
  43475. + fsg->data_dir = DATA_DIR_FROM_HOST;
  43476. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  43477. + if (fsg->data_size == 0)
  43478. + fsg->data_dir = DATA_DIR_NONE;
  43479. + fsg->lun = cbw->Lun;
  43480. + fsg->tag = cbw->Tag;
  43481. + return 0;
  43482. +}
  43483. +
  43484. +
  43485. +static int get_next_command(struct fsg_dev *fsg)
  43486. +{
  43487. + struct fsg_buffhd *bh;
  43488. + int rc = 0;
  43489. +
  43490. + if (transport_is_bbb()) {
  43491. +
  43492. + /* Wait for the next buffer to become available */
  43493. + bh = fsg->next_buffhd_to_fill;
  43494. + while (bh->state != BUF_STATE_EMPTY) {
  43495. + rc = sleep_thread(fsg);
  43496. + if (rc)
  43497. + return rc;
  43498. + }
  43499. +
  43500. + /* Queue a request to read a Bulk-only CBW */
  43501. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  43502. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  43503. + &bh->outreq_busy, &bh->state);
  43504. +
  43505. + /* We will drain the buffer in software, which means we
  43506. + * can reuse it for the next filling. No need to advance
  43507. + * next_buffhd_to_fill. */
  43508. +
  43509. + /* Wait for the CBW to arrive */
  43510. + while (bh->state != BUF_STATE_FULL) {
  43511. + rc = sleep_thread(fsg);
  43512. + if (rc)
  43513. + return rc;
  43514. + }
  43515. + smp_rmb();
  43516. + rc = received_cbw(fsg, bh);
  43517. + bh->state = BUF_STATE_EMPTY;
  43518. +
  43519. + } else { // USB_PR_CB or USB_PR_CBI
  43520. +
  43521. + /* Wait for the next command to arrive */
  43522. + while (fsg->cbbuf_cmnd_size == 0) {
  43523. + rc = sleep_thread(fsg);
  43524. + if (rc)
  43525. + return rc;
  43526. + }
  43527. +
  43528. + /* Is the previous status interrupt request still busy?
  43529. + * The host is allowed to skip reading the status,
  43530. + * so we must cancel it. */
  43531. + if (fsg->intreq_busy)
  43532. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  43533. +
  43534. + /* Copy the command and mark the buffer empty */
  43535. + fsg->data_dir = DATA_DIR_UNKNOWN;
  43536. + spin_lock_irq(&fsg->lock);
  43537. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  43538. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  43539. + fsg->cbbuf_cmnd_size = 0;
  43540. + spin_unlock_irq(&fsg->lock);
  43541. +
  43542. + /* Use LUN from the command */
  43543. + fsg->lun = fsg->cmnd[1] >> 5;
  43544. + }
  43545. +
  43546. + /* Update current lun */
  43547. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  43548. + fsg->curlun = &fsg->luns[fsg->lun];
  43549. + else
  43550. + fsg->curlun = NULL;
  43551. +
  43552. + return rc;
  43553. +}
  43554. +
  43555. +
  43556. +/*-------------------------------------------------------------------------*/
  43557. +
  43558. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  43559. + const struct usb_endpoint_descriptor *d)
  43560. +{
  43561. + int rc;
  43562. +
  43563. + ep->driver_data = fsg;
  43564. + ep->desc = d;
  43565. + rc = usb_ep_enable(ep);
  43566. + if (rc)
  43567. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  43568. + return rc;
  43569. +}
  43570. +
  43571. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  43572. + struct usb_request **preq)
  43573. +{
  43574. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  43575. + if (*preq)
  43576. + return 0;
  43577. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  43578. + return -ENOMEM;
  43579. +}
  43580. +
  43581. +/*
  43582. + * Reset interface setting and re-init endpoint state (toggle etc).
  43583. + * Call with altsetting < 0 to disable the interface. The only other
  43584. + * available altsetting is 0, which enables the interface.
  43585. + */
  43586. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  43587. +{
  43588. + int rc = 0;
  43589. + int i;
  43590. + const struct usb_endpoint_descriptor *d;
  43591. +
  43592. + if (fsg->running)
  43593. + DBG(fsg, "reset interface\n");
  43594. +
  43595. +reset:
  43596. + /* Deallocate the requests */
  43597. + for (i = 0; i < fsg_num_buffers; ++i) {
  43598. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  43599. +
  43600. + if (bh->inreq) {
  43601. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  43602. + bh->inreq = NULL;
  43603. + }
  43604. + if (bh->outreq) {
  43605. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  43606. + bh->outreq = NULL;
  43607. + }
  43608. + }
  43609. + if (fsg->intreq) {
  43610. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  43611. + fsg->intreq = NULL;
  43612. + }
  43613. +
  43614. + /* Disable the endpoints */
  43615. + if (fsg->bulk_in_enabled) {
  43616. + usb_ep_disable(fsg->bulk_in);
  43617. + fsg->bulk_in_enabled = 0;
  43618. + }
  43619. + if (fsg->bulk_out_enabled) {
  43620. + usb_ep_disable(fsg->bulk_out);
  43621. + fsg->bulk_out_enabled = 0;
  43622. + }
  43623. + if (fsg->intr_in_enabled) {
  43624. + usb_ep_disable(fsg->intr_in);
  43625. + fsg->intr_in_enabled = 0;
  43626. + }
  43627. +
  43628. + fsg->running = 0;
  43629. + if (altsetting < 0 || rc != 0)
  43630. + return rc;
  43631. +
  43632. + DBG(fsg, "set interface %d\n", altsetting);
  43633. +
  43634. + /* Enable the endpoints */
  43635. + d = fsg_ep_desc(fsg->gadget,
  43636. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  43637. + &fsg_ss_bulk_in_desc);
  43638. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  43639. + goto reset;
  43640. + fsg->bulk_in_enabled = 1;
  43641. +
  43642. + d = fsg_ep_desc(fsg->gadget,
  43643. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  43644. + &fsg_ss_bulk_out_desc);
  43645. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  43646. + goto reset;
  43647. + fsg->bulk_out_enabled = 1;
  43648. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  43649. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  43650. +
  43651. + if (transport_is_cbi()) {
  43652. + d = fsg_ep_desc(fsg->gadget,
  43653. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  43654. + &fsg_ss_intr_in_desc);
  43655. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  43656. + goto reset;
  43657. + fsg->intr_in_enabled = 1;
  43658. + }
  43659. +
  43660. + /* Allocate the requests */
  43661. + for (i = 0; i < fsg_num_buffers; ++i) {
  43662. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  43663. +
  43664. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  43665. + goto reset;
  43666. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  43667. + goto reset;
  43668. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  43669. + bh->inreq->context = bh->outreq->context = bh;
  43670. + bh->inreq->complete = bulk_in_complete;
  43671. + bh->outreq->complete = bulk_out_complete;
  43672. + }
  43673. + if (transport_is_cbi()) {
  43674. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  43675. + goto reset;
  43676. + fsg->intreq->complete = intr_in_complete;
  43677. + }
  43678. +
  43679. + fsg->running = 1;
  43680. + for (i = 0; i < fsg->nluns; ++i)
  43681. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  43682. + return rc;
  43683. +}
  43684. +
  43685. +
  43686. +/*
  43687. + * Change our operational configuration. This code must agree with the code
  43688. + * that returns config descriptors, and with interface altsetting code.
  43689. + *
  43690. + * It's also responsible for power management interactions. Some
  43691. + * configurations might not work with our current power sources.
  43692. + * For now we just assume the gadget is always self-powered.
  43693. + */
  43694. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  43695. +{
  43696. + int rc = 0;
  43697. +
  43698. + /* Disable the single interface */
  43699. + if (fsg->config != 0) {
  43700. + DBG(fsg, "reset config\n");
  43701. + fsg->config = 0;
  43702. + rc = do_set_interface(fsg, -1);
  43703. + }
  43704. +
  43705. + /* Enable the interface */
  43706. + if (new_config != 0) {
  43707. + fsg->config = new_config;
  43708. + if ((rc = do_set_interface(fsg, 0)) != 0)
  43709. + fsg->config = 0; // Reset on errors
  43710. + else
  43711. + INFO(fsg, "%s config #%d\n",
  43712. + usb_speed_string(fsg->gadget->speed),
  43713. + fsg->config);
  43714. + }
  43715. + return rc;
  43716. +}
  43717. +
  43718. +
  43719. +/*-------------------------------------------------------------------------*/
  43720. +
  43721. +static void handle_exception(struct fsg_dev *fsg)
  43722. +{
  43723. + siginfo_t info;
  43724. + int sig;
  43725. + int i;
  43726. + int num_active;
  43727. + struct fsg_buffhd *bh;
  43728. + enum fsg_state old_state;
  43729. + u8 new_config;
  43730. + struct fsg_lun *curlun;
  43731. + unsigned int exception_req_tag;
  43732. + int rc;
  43733. +
  43734. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  43735. + * into a high-priority EXIT exception. */
  43736. + for (;;) {
  43737. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  43738. + if (!sig)
  43739. + break;
  43740. + if (sig != SIGUSR1) {
  43741. + if (fsg->state < FSG_STATE_EXIT)
  43742. + DBG(fsg, "Main thread exiting on signal\n");
  43743. + raise_exception(fsg, FSG_STATE_EXIT);
  43744. + }
  43745. + }
  43746. +
  43747. + /* Cancel all the pending transfers */
  43748. + if (fsg->intreq_busy)
  43749. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  43750. + for (i = 0; i < fsg_num_buffers; ++i) {
  43751. + bh = &fsg->buffhds[i];
  43752. + if (bh->inreq_busy)
  43753. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  43754. + if (bh->outreq_busy)
  43755. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  43756. + }
  43757. +
  43758. + /* Wait until everything is idle */
  43759. + for (;;) {
  43760. + num_active = fsg->intreq_busy;
  43761. + for (i = 0; i < fsg_num_buffers; ++i) {
  43762. + bh = &fsg->buffhds[i];
  43763. + num_active += bh->inreq_busy + bh->outreq_busy;
  43764. + }
  43765. + if (num_active == 0)
  43766. + break;
  43767. + if (sleep_thread(fsg))
  43768. + return;
  43769. + }
  43770. +
  43771. + /* Clear out the controller's fifos */
  43772. + if (fsg->bulk_in_enabled)
  43773. + usb_ep_fifo_flush(fsg->bulk_in);
  43774. + if (fsg->bulk_out_enabled)
  43775. + usb_ep_fifo_flush(fsg->bulk_out);
  43776. + if (fsg->intr_in_enabled)
  43777. + usb_ep_fifo_flush(fsg->intr_in);
  43778. +
  43779. + /* Reset the I/O buffer states and pointers, the SCSI
  43780. + * state, and the exception. Then invoke the handler. */
  43781. + spin_lock_irq(&fsg->lock);
  43782. +
  43783. + for (i = 0; i < fsg_num_buffers; ++i) {
  43784. + bh = &fsg->buffhds[i];
  43785. + bh->state = BUF_STATE_EMPTY;
  43786. + }
  43787. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  43788. + &fsg->buffhds[0];
  43789. +
  43790. + exception_req_tag = fsg->exception_req_tag;
  43791. + new_config = fsg->new_config;
  43792. + old_state = fsg->state;
  43793. +
  43794. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  43795. + fsg->state = FSG_STATE_STATUS_PHASE;
  43796. + else {
  43797. + for (i = 0; i < fsg->nluns; ++i) {
  43798. + curlun = &fsg->luns[i];
  43799. + curlun->prevent_medium_removal = 0;
  43800. + curlun->sense_data = curlun->unit_attention_data =
  43801. + SS_NO_SENSE;
  43802. + curlun->sense_data_info = 0;
  43803. + curlun->info_valid = 0;
  43804. + }
  43805. + fsg->state = FSG_STATE_IDLE;
  43806. + }
  43807. + spin_unlock_irq(&fsg->lock);
  43808. +
  43809. + /* Carry out any extra actions required for the exception */
  43810. + switch (old_state) {
  43811. + default:
  43812. + break;
  43813. +
  43814. + case FSG_STATE_ABORT_BULK_OUT:
  43815. + send_status(fsg);
  43816. + spin_lock_irq(&fsg->lock);
  43817. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  43818. + fsg->state = FSG_STATE_IDLE;
  43819. + spin_unlock_irq(&fsg->lock);
  43820. + break;
  43821. +
  43822. + case FSG_STATE_RESET:
  43823. + /* In case we were forced against our will to halt a
  43824. + * bulk endpoint, clear the halt now. (The SuperH UDC
  43825. + * requires this.) */
  43826. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  43827. + usb_ep_clear_halt(fsg->bulk_in);
  43828. +
  43829. + if (transport_is_bbb()) {
  43830. + if (fsg->ep0_req_tag == exception_req_tag)
  43831. + ep0_queue(fsg); // Complete the status stage
  43832. +
  43833. + } else if (transport_is_cbi())
  43834. + send_status(fsg); // Status by interrupt pipe
  43835. +
  43836. + /* Technically this should go here, but it would only be
  43837. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  43838. + * CONFIG_CHANGE cases. */
  43839. + // for (i = 0; i < fsg->nluns; ++i)
  43840. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  43841. + break;
  43842. +
  43843. + case FSG_STATE_INTERFACE_CHANGE:
  43844. + rc = do_set_interface(fsg, 0);
  43845. + if (fsg->ep0_req_tag != exception_req_tag)
  43846. + break;
  43847. + if (rc != 0) // STALL on errors
  43848. + fsg_set_halt(fsg, fsg->ep0);
  43849. + else // Complete the status stage
  43850. + ep0_queue(fsg);
  43851. + break;
  43852. +
  43853. + case FSG_STATE_CONFIG_CHANGE:
  43854. + rc = do_set_config(fsg, new_config);
  43855. + if (fsg->ep0_req_tag != exception_req_tag)
  43856. + break;
  43857. + if (rc != 0) // STALL on errors
  43858. + fsg_set_halt(fsg, fsg->ep0);
  43859. + else // Complete the status stage
  43860. + ep0_queue(fsg);
  43861. + break;
  43862. +
  43863. + case FSG_STATE_DISCONNECT:
  43864. + for (i = 0; i < fsg->nluns; ++i)
  43865. + fsg_lun_fsync_sub(fsg->luns + i);
  43866. + do_set_config(fsg, 0); // Unconfigured state
  43867. + break;
  43868. +
  43869. + case FSG_STATE_EXIT:
  43870. + case FSG_STATE_TERMINATED:
  43871. + do_set_config(fsg, 0); // Free resources
  43872. + spin_lock_irq(&fsg->lock);
  43873. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  43874. + spin_unlock_irq(&fsg->lock);
  43875. + break;
  43876. + }
  43877. +}
  43878. +
  43879. +
  43880. +/*-------------------------------------------------------------------------*/
  43881. +
  43882. +static int fsg_main_thread(void *fsg_)
  43883. +{
  43884. + struct fsg_dev *fsg = fsg_;
  43885. +
  43886. + /* Allow the thread to be killed by a signal, but set the signal mask
  43887. + * to block everything but INT, TERM, KILL, and USR1. */
  43888. + allow_signal(SIGINT);
  43889. + allow_signal(SIGTERM);
  43890. + allow_signal(SIGKILL);
  43891. + allow_signal(SIGUSR1);
  43892. +
  43893. + /* Allow the thread to be frozen */
  43894. + set_freezable();
  43895. +
  43896. + /* Arrange for userspace references to be interpreted as kernel
  43897. + * pointers. That way we can pass a kernel pointer to a routine
  43898. + * that expects a __user pointer and it will work okay. */
  43899. + set_fs(get_ds());
  43900. +
  43901. + /* The main loop */
  43902. + while (fsg->state != FSG_STATE_TERMINATED) {
  43903. + if (exception_in_progress(fsg) || signal_pending(current)) {
  43904. + handle_exception(fsg);
  43905. + continue;
  43906. + }
  43907. +
  43908. + if (!fsg->running) {
  43909. + sleep_thread(fsg);
  43910. + continue;
  43911. + }
  43912. +
  43913. + if (get_next_command(fsg))
  43914. + continue;
  43915. +
  43916. + spin_lock_irq(&fsg->lock);
  43917. + if (!exception_in_progress(fsg))
  43918. + fsg->state = FSG_STATE_DATA_PHASE;
  43919. + spin_unlock_irq(&fsg->lock);
  43920. +
  43921. + if (do_scsi_command(fsg) || finish_reply(fsg))
  43922. + continue;
  43923. +
  43924. + spin_lock_irq(&fsg->lock);
  43925. + if (!exception_in_progress(fsg))
  43926. + fsg->state = FSG_STATE_STATUS_PHASE;
  43927. + spin_unlock_irq(&fsg->lock);
  43928. +
  43929. + if (send_status(fsg))
  43930. + continue;
  43931. +
  43932. + spin_lock_irq(&fsg->lock);
  43933. + if (!exception_in_progress(fsg))
  43934. + fsg->state = FSG_STATE_IDLE;
  43935. + spin_unlock_irq(&fsg->lock);
  43936. + }
  43937. +
  43938. + spin_lock_irq(&fsg->lock);
  43939. + fsg->thread_task = NULL;
  43940. + spin_unlock_irq(&fsg->lock);
  43941. +
  43942. + /* If we are exiting because of a signal, unregister the
  43943. + * gadget driver. */
  43944. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  43945. + usb_gadget_unregister_driver(&fsg_driver);
  43946. +
  43947. + /* Let the unbind and cleanup routines know the thread has exited */
  43948. + complete_and_exit(&fsg->thread_notifier, 0);
  43949. +}
  43950. +
  43951. +
  43952. +/*-------------------------------------------------------------------------*/
  43953. +
  43954. +
  43955. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  43956. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  43957. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  43958. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  43959. +
  43960. +
  43961. +/*-------------------------------------------------------------------------*/
  43962. +
  43963. +static void fsg_release(struct kref *ref)
  43964. +{
  43965. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  43966. +
  43967. + kfree(fsg->luns);
  43968. + kfree(fsg);
  43969. +}
  43970. +
  43971. +static void lun_release(struct device *dev)
  43972. +{
  43973. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  43974. + struct fsg_dev *fsg =
  43975. + container_of(filesem, struct fsg_dev, filesem);
  43976. +
  43977. + kref_put(&fsg->ref, fsg_release);
  43978. +}
  43979. +
  43980. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  43981. +{
  43982. + struct fsg_dev *fsg = get_gadget_data(gadget);
  43983. + int i;
  43984. + struct fsg_lun *curlun;
  43985. + struct usb_request *req = fsg->ep0req;
  43986. +
  43987. + DBG(fsg, "unbind\n");
  43988. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  43989. +
  43990. + /* If the thread isn't already dead, tell it to exit now */
  43991. + if (fsg->state != FSG_STATE_TERMINATED) {
  43992. + raise_exception(fsg, FSG_STATE_EXIT);
  43993. + wait_for_completion(&fsg->thread_notifier);
  43994. +
  43995. + /* The cleanup routine waits for this completion also */
  43996. + complete(&fsg->thread_notifier);
  43997. + }
  43998. +
  43999. + /* Unregister the sysfs attribute files and the LUNs */
  44000. + for (i = 0; i < fsg->nluns; ++i) {
  44001. + curlun = &fsg->luns[i];
  44002. + if (curlun->registered) {
  44003. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  44004. + device_remove_file(&curlun->dev, &dev_attr_ro);
  44005. + device_remove_file(&curlun->dev, &dev_attr_file);
  44006. + fsg_lun_close(curlun);
  44007. + device_unregister(&curlun->dev);
  44008. + curlun->registered = 0;
  44009. + }
  44010. + }
  44011. +
  44012. + /* Free the data buffers */
  44013. + for (i = 0; i < fsg_num_buffers; ++i)
  44014. + kfree(fsg->buffhds[i].buf);
  44015. +
  44016. + /* Free the request and buffer for endpoint 0 */
  44017. + if (req) {
  44018. + kfree(req->buf);
  44019. + usb_ep_free_request(fsg->ep0, req);
  44020. + }
  44021. +
  44022. + set_gadget_data(gadget, NULL);
  44023. +}
  44024. +
  44025. +
  44026. +static int __init check_parameters(struct fsg_dev *fsg)
  44027. +{
  44028. + int prot;
  44029. + int gcnum;
  44030. +
  44031. + /* Store the default values */
  44032. + mod_data.transport_type = USB_PR_BULK;
  44033. + mod_data.transport_name = "Bulk-only";
  44034. + mod_data.protocol_type = USB_SC_SCSI;
  44035. + mod_data.protocol_name = "Transparent SCSI";
  44036. +
  44037. + /* Some peripheral controllers are known not to be able to
  44038. + * halt bulk endpoints correctly. If one of them is present,
  44039. + * disable stalls.
  44040. + */
  44041. + if (gadget_is_at91(fsg->gadget))
  44042. + mod_data.can_stall = 0;
  44043. +
  44044. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  44045. + gcnum = usb_gadget_controller_number(fsg->gadget);
  44046. + if (gcnum >= 0)
  44047. + mod_data.release = 0x0300 + gcnum;
  44048. + else {
  44049. + WARNING(fsg, "controller '%s' not recognized\n",
  44050. + fsg->gadget->name);
  44051. + mod_data.release = 0x0399;
  44052. + }
  44053. + }
  44054. +
  44055. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  44056. +
  44057. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  44058. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  44059. + ; // Use default setting
  44060. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  44061. + mod_data.transport_type = USB_PR_CB;
  44062. + mod_data.transport_name = "Control-Bulk";
  44063. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  44064. + mod_data.transport_type = USB_PR_CBI;
  44065. + mod_data.transport_name = "Control-Bulk-Interrupt";
  44066. + } else {
  44067. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  44068. + return -EINVAL;
  44069. + }
  44070. +
  44071. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  44072. + prot == USB_SC_SCSI) {
  44073. + ; // Use default setting
  44074. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  44075. + prot == USB_SC_RBC) {
  44076. + mod_data.protocol_type = USB_SC_RBC;
  44077. + mod_data.protocol_name = "RBC";
  44078. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  44079. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  44080. + prot == USB_SC_8020) {
  44081. + mod_data.protocol_type = USB_SC_8020;
  44082. + mod_data.protocol_name = "8020i (ATAPI)";
  44083. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  44084. + prot == USB_SC_QIC) {
  44085. + mod_data.protocol_type = USB_SC_QIC;
  44086. + mod_data.protocol_name = "QIC-157";
  44087. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  44088. + prot == USB_SC_UFI) {
  44089. + mod_data.protocol_type = USB_SC_UFI;
  44090. + mod_data.protocol_name = "UFI";
  44091. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  44092. + prot == USB_SC_8070) {
  44093. + mod_data.protocol_type = USB_SC_8070;
  44094. + mod_data.protocol_name = "8070i";
  44095. + } else {
  44096. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  44097. + return -EINVAL;
  44098. + }
  44099. +
  44100. + mod_data.buflen &= PAGE_CACHE_MASK;
  44101. + if (mod_data.buflen <= 0) {
  44102. + ERROR(fsg, "invalid buflen\n");
  44103. + return -ETOOSMALL;
  44104. + }
  44105. +
  44106. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  44107. +
  44108. + /* Serial string handling.
  44109. + * On a real device, the serial string would be loaded
  44110. + * from permanent storage. */
  44111. + if (mod_data.serial) {
  44112. + const char *ch;
  44113. + unsigned len = 0;
  44114. +
  44115. + /* Sanity check :
  44116. + * The CB[I] specification limits the serial string to
  44117. + * 12 uppercase hexadecimal characters.
  44118. + * BBB need at least 12 uppercase hexadecimal characters,
  44119. + * with a maximum of 126. */
  44120. + for (ch = mod_data.serial; *ch; ++ch) {
  44121. + ++len;
  44122. + if ((*ch < '0' || *ch > '9') &&
  44123. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  44124. + WARNING(fsg,
  44125. + "Invalid serial string character: %c\n",
  44126. + *ch);
  44127. + goto no_serial;
  44128. + }
  44129. + }
  44130. + if (len > 126 ||
  44131. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  44132. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  44133. + WARNING(fsg, "Invalid serial string length!\n");
  44134. + goto no_serial;
  44135. + }
  44136. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  44137. + } else {
  44138. + WARNING(fsg, "No serial-number string provided!\n");
  44139. + no_serial:
  44140. + device_desc.iSerialNumber = 0;
  44141. + }
  44142. +
  44143. + return 0;
  44144. +}
  44145. +
  44146. +
  44147. +static int __init fsg_bind(struct usb_gadget *gadget)
  44148. +{
  44149. + struct fsg_dev *fsg = the_fsg;
  44150. + int rc;
  44151. + int i;
  44152. + struct fsg_lun *curlun;
  44153. + struct usb_ep *ep;
  44154. + struct usb_request *req;
  44155. + char *pathbuf, *p;
  44156. +
  44157. + fsg->gadget = gadget;
  44158. + set_gadget_data(gadget, fsg);
  44159. + fsg->ep0 = gadget->ep0;
  44160. + fsg->ep0->driver_data = fsg;
  44161. +
  44162. + if ((rc = check_parameters(fsg)) != 0)
  44163. + goto out;
  44164. +
  44165. + if (mod_data.removable) { // Enable the store_xxx attributes
  44166. + dev_attr_file.attr.mode = 0644;
  44167. + dev_attr_file.store = fsg_store_file;
  44168. + if (!mod_data.cdrom) {
  44169. + dev_attr_ro.attr.mode = 0644;
  44170. + dev_attr_ro.store = fsg_store_ro;
  44171. + }
  44172. + }
  44173. +
  44174. + /* Only for removable media? */
  44175. + dev_attr_nofua.attr.mode = 0644;
  44176. + dev_attr_nofua.store = fsg_store_nofua;
  44177. +
  44178. + /* Find out how many LUNs there should be */
  44179. + i = mod_data.nluns;
  44180. + if (i == 0)
  44181. + i = max(mod_data.num_filenames, 1u);
  44182. + if (i > FSG_MAX_LUNS) {
  44183. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  44184. + rc = -EINVAL;
  44185. + goto out;
  44186. + }
  44187. +
  44188. + /* Create the LUNs, open their backing files, and register the
  44189. + * LUN devices in sysfs. */
  44190. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  44191. + if (!fsg->luns) {
  44192. + rc = -ENOMEM;
  44193. + goto out;
  44194. + }
  44195. + fsg->nluns = i;
  44196. +
  44197. + for (i = 0; i < fsg->nluns; ++i) {
  44198. + curlun = &fsg->luns[i];
  44199. + curlun->cdrom = !!mod_data.cdrom;
  44200. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  44201. + curlun->initially_ro = curlun->ro;
  44202. + curlun->removable = mod_data.removable;
  44203. + curlun->nofua = mod_data.nofua[i];
  44204. + curlun->dev.release = lun_release;
  44205. + curlun->dev.parent = &gadget->dev;
  44206. + curlun->dev.driver = &fsg_driver.driver;
  44207. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  44208. + dev_set_name(&curlun->dev,"%s-lun%d",
  44209. + dev_name(&gadget->dev), i);
  44210. +
  44211. + kref_get(&fsg->ref);
  44212. + rc = device_register(&curlun->dev);
  44213. + if (rc) {
  44214. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  44215. + put_device(&curlun->dev);
  44216. + goto out;
  44217. + }
  44218. + curlun->registered = 1;
  44219. +
  44220. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  44221. + if (rc)
  44222. + goto out;
  44223. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  44224. + if (rc)
  44225. + goto out;
  44226. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  44227. + if (rc)
  44228. + goto out;
  44229. +
  44230. + if (mod_data.file[i] && *mod_data.file[i]) {
  44231. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  44232. + if (rc)
  44233. + goto out;
  44234. + } else if (!mod_data.removable) {
  44235. + ERROR(fsg, "no file given for LUN%d\n", i);
  44236. + rc = -EINVAL;
  44237. + goto out;
  44238. + }
  44239. + }
  44240. +
  44241. + /* Find all the endpoints we will use */
  44242. + usb_ep_autoconfig_reset(gadget);
  44243. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  44244. + if (!ep)
  44245. + goto autoconf_fail;
  44246. + ep->driver_data = fsg; // claim the endpoint
  44247. + fsg->bulk_in = ep;
  44248. +
  44249. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  44250. + if (!ep)
  44251. + goto autoconf_fail;
  44252. + ep->driver_data = fsg; // claim the endpoint
  44253. + fsg->bulk_out = ep;
  44254. +
  44255. + if (transport_is_cbi()) {
  44256. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  44257. + if (!ep)
  44258. + goto autoconf_fail;
  44259. + ep->driver_data = fsg; // claim the endpoint
  44260. + fsg->intr_in = ep;
  44261. + }
  44262. +
  44263. + /* Fix up the descriptors */
  44264. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  44265. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  44266. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  44267. +
  44268. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  44269. + fsg_intf_desc.bNumEndpoints = i;
  44270. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  44271. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  44272. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  44273. +
  44274. + if (gadget_is_dualspeed(gadget)) {
  44275. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  44276. +
  44277. + /* Assume endpoint addresses are the same for both speeds */
  44278. + fsg_hs_bulk_in_desc.bEndpointAddress =
  44279. + fsg_fs_bulk_in_desc.bEndpointAddress;
  44280. + fsg_hs_bulk_out_desc.bEndpointAddress =
  44281. + fsg_fs_bulk_out_desc.bEndpointAddress;
  44282. + fsg_hs_intr_in_desc.bEndpointAddress =
  44283. + fsg_fs_intr_in_desc.bEndpointAddress;
  44284. + }
  44285. +
  44286. + if (gadget_is_superspeed(gadget)) {
  44287. + unsigned max_burst;
  44288. +
  44289. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  44290. +
  44291. + /* Calculate bMaxBurst, we know packet size is 1024 */
  44292. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  44293. +
  44294. + /* Assume endpoint addresses are the same for both speeds */
  44295. + fsg_ss_bulk_in_desc.bEndpointAddress =
  44296. + fsg_fs_bulk_in_desc.bEndpointAddress;
  44297. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  44298. +
  44299. + fsg_ss_bulk_out_desc.bEndpointAddress =
  44300. + fsg_fs_bulk_out_desc.bEndpointAddress;
  44301. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  44302. + }
  44303. +
  44304. + if (gadget_is_otg(gadget))
  44305. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  44306. +
  44307. + rc = -ENOMEM;
  44308. +
  44309. + /* Allocate the request and buffer for endpoint 0 */
  44310. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  44311. + if (!req)
  44312. + goto out;
  44313. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  44314. + if (!req->buf)
  44315. + goto out;
  44316. + req->complete = ep0_complete;
  44317. +
  44318. + /* Allocate the data buffers */
  44319. + for (i = 0; i < fsg_num_buffers; ++i) {
  44320. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  44321. +
  44322. + /* Allocate for the bulk-in endpoint. We assume that
  44323. + * the buffer will also work with the bulk-out (and
  44324. + * interrupt-in) endpoint. */
  44325. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  44326. + if (!bh->buf)
  44327. + goto out;
  44328. + bh->next = bh + 1;
  44329. + }
  44330. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  44331. +
  44332. + /* This should reflect the actual gadget power source */
  44333. + usb_gadget_set_selfpowered(gadget);
  44334. +
  44335. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  44336. + "%s %s with %s",
  44337. + init_utsname()->sysname, init_utsname()->release,
  44338. + gadget->name);
  44339. +
  44340. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  44341. + "file-storage-gadget");
  44342. + if (IS_ERR(fsg->thread_task)) {
  44343. + rc = PTR_ERR(fsg->thread_task);
  44344. + goto out;
  44345. + }
  44346. +
  44347. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  44348. + INFO(fsg, "NOTE: This driver is deprecated. "
  44349. + "Consider using g_mass_storage instead.\n");
  44350. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  44351. +
  44352. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  44353. + for (i = 0; i < fsg->nluns; ++i) {
  44354. + curlun = &fsg->luns[i];
  44355. + if (fsg_lun_is_open(curlun)) {
  44356. + p = NULL;
  44357. + if (pathbuf) {
  44358. + p = d_path(&curlun->filp->f_path,
  44359. + pathbuf, PATH_MAX);
  44360. + if (IS_ERR(p))
  44361. + p = NULL;
  44362. + }
  44363. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  44364. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  44365. + }
  44366. + }
  44367. + kfree(pathbuf);
  44368. +
  44369. + DBG(fsg, "transport=%s (x%02x)\n",
  44370. + mod_data.transport_name, mod_data.transport_type);
  44371. + DBG(fsg, "protocol=%s (x%02x)\n",
  44372. + mod_data.protocol_name, mod_data.protocol_type);
  44373. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  44374. + mod_data.vendor, mod_data.product, mod_data.release);
  44375. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  44376. + mod_data.removable, mod_data.can_stall,
  44377. + mod_data.cdrom, mod_data.buflen);
  44378. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  44379. +
  44380. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  44381. +
  44382. + /* Tell the thread to start working */
  44383. + wake_up_process(fsg->thread_task);
  44384. + return 0;
  44385. +
  44386. +autoconf_fail:
  44387. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  44388. + rc = -ENOTSUPP;
  44389. +
  44390. +out:
  44391. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  44392. + fsg_unbind(gadget);
  44393. + complete(&fsg->thread_notifier);
  44394. + return rc;
  44395. +}
  44396. +
  44397. +
  44398. +/*-------------------------------------------------------------------------*/
  44399. +
  44400. +static void fsg_suspend(struct usb_gadget *gadget)
  44401. +{
  44402. + struct fsg_dev *fsg = get_gadget_data(gadget);
  44403. +
  44404. + DBG(fsg, "suspend\n");
  44405. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  44406. +}
  44407. +
  44408. +static void fsg_resume(struct usb_gadget *gadget)
  44409. +{
  44410. + struct fsg_dev *fsg = get_gadget_data(gadget);
  44411. +
  44412. + DBG(fsg, "resume\n");
  44413. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  44414. +}
  44415. +
  44416. +
  44417. +/*-------------------------------------------------------------------------*/
  44418. +
  44419. +static struct usb_gadget_driver fsg_driver = {
  44420. + .max_speed = USB_SPEED_SUPER,
  44421. + .function = (char *) fsg_string_product,
  44422. + .unbind = fsg_unbind,
  44423. + .disconnect = fsg_disconnect,
  44424. + .setup = fsg_setup,
  44425. + .suspend = fsg_suspend,
  44426. + .resume = fsg_resume,
  44427. +
  44428. + .driver = {
  44429. + .name = DRIVER_NAME,
  44430. + .owner = THIS_MODULE,
  44431. + // .release = ...
  44432. + // .suspend = ...
  44433. + // .resume = ...
  44434. + },
  44435. +};
  44436. +
  44437. +
  44438. +static int __init fsg_alloc(void)
  44439. +{
  44440. + struct fsg_dev *fsg;
  44441. +
  44442. + fsg = kzalloc(sizeof *fsg +
  44443. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  44444. +
  44445. + if (!fsg)
  44446. + return -ENOMEM;
  44447. + spin_lock_init(&fsg->lock);
  44448. + init_rwsem(&fsg->filesem);
  44449. + kref_init(&fsg->ref);
  44450. + init_completion(&fsg->thread_notifier);
  44451. +
  44452. + the_fsg = fsg;
  44453. + return 0;
  44454. +}
  44455. +
  44456. +
  44457. +static int __init fsg_init(void)
  44458. +{
  44459. + int rc;
  44460. + struct fsg_dev *fsg;
  44461. +
  44462. + rc = fsg_num_buffers_validate();
  44463. + if (rc != 0)
  44464. + return rc;
  44465. +
  44466. + if ((rc = fsg_alloc()) != 0)
  44467. + return rc;
  44468. + fsg = the_fsg;
  44469. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  44470. + kref_put(&fsg->ref, fsg_release);
  44471. + return rc;
  44472. +}
  44473. +module_init(fsg_init);
  44474. +
  44475. +
  44476. +static void __exit fsg_cleanup(void)
  44477. +{
  44478. + struct fsg_dev *fsg = the_fsg;
  44479. +
  44480. + /* Unregister the driver iff the thread hasn't already done so */
  44481. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  44482. + usb_gadget_unregister_driver(&fsg_driver);
  44483. +
  44484. + /* Wait for the thread to finish up */
  44485. + wait_for_completion(&fsg->thread_notifier);
  44486. +
  44487. + kref_put(&fsg->ref, fsg_release);
  44488. +}
  44489. +module_exit(fsg_cleanup);
  44490. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/changes.txt linux-rpi/drivers/usb/host/dwc_common_port/changes.txt
  44491. --- linux-3.17.5/drivers/usb/host/dwc_common_port/changes.txt 1969-12-31 18:00:00.000000000 -0600
  44492. +++ linux-rpi/drivers/usb/host/dwc_common_port/changes.txt 2014-12-11 14:02:55.328418001 -0600
  44493. @@ -0,0 +1,174 @@
  44494. +
  44495. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  44496. +IO context struct. The IO context struct should live in an os-dependent struct
  44497. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  44498. +named 'os_dep' embedded in the main device struct. So there these calls look
  44499. +like this:
  44500. +
  44501. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  44502. +
  44503. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  44504. + &pcd->dev_global_regs->dcfg, 0);
  44505. +
  44506. +Note that for the existing Linux driver ports, it is not necessary to actually
  44507. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  44508. +require an IO context, its macros for dwc_read_reg32() and friends do not
  44509. +use the context pointer, so it is optimized away by the compiler. But it is
  44510. +necessary to add the pointer parameter to all of the call sites, to be ready
  44511. +for any future ports (such as FreeBSD) which do require an IO context.
  44512. +
  44513. +
  44514. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  44515. +take an additional parameter, a pointer to a memory context. Examples:
  44516. +
  44517. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  44518. +
  44519. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  44520. +
  44521. +Again, for the Linux ports, it is not necessary to actually define the memctx
  44522. +member, but it is necessary to add the pointer parameter to all of the call
  44523. +sites.
  44524. +
  44525. +
  44526. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  44527. +
  44528. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  44529. +
  44530. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  44531. +
  44532. +
  44533. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  44534. +
  44535. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  44536. +
  44537. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  44538. +
  44539. +
  44540. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  44541. +
  44542. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  44543. +
  44544. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  44545. +
  44546. +
  44547. +Same for dwc_timer_alloc(). Example:
  44548. +
  44549. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  44550. + cb_func, cb_data);
  44551. +
  44552. +
  44553. +Same for dwc_waitq_alloc(). Example:
  44554. +
  44555. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  44556. +
  44557. +
  44558. +Same for dwc_thread_run(). Example:
  44559. +
  44560. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  44561. + "dwc_usb3_thd1", data);
  44562. +
  44563. +
  44564. +Same for dwc_workq_alloc(). Example:
  44565. +
  44566. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  44567. +
  44568. +
  44569. +Same for dwc_task_alloc(). Example:
  44570. +
  44571. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  44572. + cb_func, cb_data);
  44573. +
  44574. +
  44575. +In addition to the context pointer additions, a few core functions have had
  44576. +other changes made to their parameters:
  44577. +
  44578. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  44579. +has been changed from a uint64_t to a dwc_irqflags_t.
  44580. +
  44581. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  44582. +FreeBSD equivalent of that function requires it.
  44583. +
  44584. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  44585. +'char *name' parameter, to be consistent with dwc_thread_run() and
  44586. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  44587. +requires a unique name.
  44588. +
  44589. +
  44590. +Here is a complete list of the core functions that now take a pointer to a
  44591. +context as their first parameter:
  44592. +
  44593. + dwc_read_reg32
  44594. + dwc_read_reg64
  44595. + dwc_write_reg32
  44596. + dwc_write_reg64
  44597. + dwc_modify_reg32
  44598. + dwc_modify_reg64
  44599. + dwc_alloc
  44600. + dwc_alloc_atomic
  44601. + dwc_strdup
  44602. + dwc_free
  44603. + dwc_dma_alloc
  44604. + dwc_dma_free
  44605. + dwc_mutex_alloc
  44606. + dwc_mutex_free
  44607. + dwc_spinlock_alloc
  44608. + dwc_spinlock_free
  44609. + dwc_timer_alloc
  44610. + dwc_waitq_alloc
  44611. + dwc_thread_run
  44612. + dwc_workq_alloc
  44613. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  44614. +
  44615. +And here are the core functions that have other changes to their parameters:
  44616. +
  44617. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  44618. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  44619. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  44620. +
  44621. +
  44622. +
  44623. +The changes to the core functions also require some of the other library
  44624. +functions to change:
  44625. +
  44626. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  44627. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  44628. + (for mutex allocation) as the 2nd param.
  44629. +
  44630. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  44631. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  44632. + 'void *memctx' as the 1st param.
  44633. +
  44634. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  44635. + 'void *memctx' as the 1st param.
  44636. +
  44637. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  44638. +
  44639. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  44640. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  44641. + param, and also now returns an integer value that is non-zero if
  44642. + allocation of its data structures or work queue fails.
  44643. +
  44644. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  44645. +
  44646. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  44647. + param, and also now returns an integer value that is non-zero if
  44648. + allocation of its data structures fails.
  44649. +
  44650. +
  44651. +
  44652. +Other miscellaneous changes:
  44653. +
  44654. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  44655. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  44656. +
  44657. +The following #define's have been added to allow selectively compiling library
  44658. +features:
  44659. +
  44660. + DWC_CCLIB
  44661. + DWC_CRYPTOLIB
  44662. + DWC_NOTIFYLIB
  44663. + DWC_UTFLIB
  44664. +
  44665. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  44666. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  44667. +library code directly into a driver module, instead of as a standalone module.
  44668. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/doc/doxygen.cfg linux-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  44669. --- linux-3.17.5/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 1969-12-31 18:00:00.000000000 -0600
  44670. +++ linux-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 2014-12-11 14:02:55.328418001 -0600
  44671. @@ -0,0 +1,270 @@
  44672. +# Doxyfile 1.4.5
  44673. +
  44674. +#---------------------------------------------------------------------------
  44675. +# Project related configuration options
  44676. +#---------------------------------------------------------------------------
  44677. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  44678. +PROJECT_NUMBER =
  44679. +OUTPUT_DIRECTORY = doc
  44680. +CREATE_SUBDIRS = NO
  44681. +OUTPUT_LANGUAGE = English
  44682. +BRIEF_MEMBER_DESC = YES
  44683. +REPEAT_BRIEF = YES
  44684. +ABBREVIATE_BRIEF = "The $name class" \
  44685. + "The $name widget" \
  44686. + "The $name file" \
  44687. + is \
  44688. + provides \
  44689. + specifies \
  44690. + contains \
  44691. + represents \
  44692. + a \
  44693. + an \
  44694. + the
  44695. +ALWAYS_DETAILED_SEC = YES
  44696. +INLINE_INHERITED_MEMB = NO
  44697. +FULL_PATH_NAMES = NO
  44698. +STRIP_FROM_PATH = ..
  44699. +STRIP_FROM_INC_PATH =
  44700. +SHORT_NAMES = NO
  44701. +JAVADOC_AUTOBRIEF = YES
  44702. +MULTILINE_CPP_IS_BRIEF = NO
  44703. +DETAILS_AT_TOP = YES
  44704. +INHERIT_DOCS = YES
  44705. +SEPARATE_MEMBER_PAGES = NO
  44706. +TAB_SIZE = 8
  44707. +ALIASES =
  44708. +OPTIMIZE_OUTPUT_FOR_C = YES
  44709. +OPTIMIZE_OUTPUT_JAVA = NO
  44710. +BUILTIN_STL_SUPPORT = NO
  44711. +DISTRIBUTE_GROUP_DOC = NO
  44712. +SUBGROUPING = NO
  44713. +#---------------------------------------------------------------------------
  44714. +# Build related configuration options
  44715. +#---------------------------------------------------------------------------
  44716. +EXTRACT_ALL = NO
  44717. +EXTRACT_PRIVATE = NO
  44718. +EXTRACT_STATIC = YES
  44719. +EXTRACT_LOCAL_CLASSES = NO
  44720. +EXTRACT_LOCAL_METHODS = NO
  44721. +HIDE_UNDOC_MEMBERS = NO
  44722. +HIDE_UNDOC_CLASSES = NO
  44723. +HIDE_FRIEND_COMPOUNDS = NO
  44724. +HIDE_IN_BODY_DOCS = NO
  44725. +INTERNAL_DOCS = NO
  44726. +CASE_SENSE_NAMES = YES
  44727. +HIDE_SCOPE_NAMES = NO
  44728. +SHOW_INCLUDE_FILES = NO
  44729. +INLINE_INFO = YES
  44730. +SORT_MEMBER_DOCS = NO
  44731. +SORT_BRIEF_DOCS = NO
  44732. +SORT_BY_SCOPE_NAME = NO
  44733. +GENERATE_TODOLIST = YES
  44734. +GENERATE_TESTLIST = YES
  44735. +GENERATE_BUGLIST = YES
  44736. +GENERATE_DEPRECATEDLIST= YES
  44737. +ENABLED_SECTIONS =
  44738. +MAX_INITIALIZER_LINES = 30
  44739. +SHOW_USED_FILES = YES
  44740. +SHOW_DIRECTORIES = YES
  44741. +FILE_VERSION_FILTER =
  44742. +#---------------------------------------------------------------------------
  44743. +# configuration options related to warning and progress messages
  44744. +#---------------------------------------------------------------------------
  44745. +QUIET = YES
  44746. +WARNINGS = YES
  44747. +WARN_IF_UNDOCUMENTED = NO
  44748. +WARN_IF_DOC_ERROR = YES
  44749. +WARN_NO_PARAMDOC = YES
  44750. +WARN_FORMAT = "$file:$line: $text"
  44751. +WARN_LOGFILE =
  44752. +#---------------------------------------------------------------------------
  44753. +# configuration options related to the input files
  44754. +#---------------------------------------------------------------------------
  44755. +INPUT = .
  44756. +FILE_PATTERNS = *.c \
  44757. + *.cc \
  44758. + *.cxx \
  44759. + *.cpp \
  44760. + *.c++ \
  44761. + *.d \
  44762. + *.java \
  44763. + *.ii \
  44764. + *.ixx \
  44765. + *.ipp \
  44766. + *.i++ \
  44767. + *.inl \
  44768. + *.h \
  44769. + *.hh \
  44770. + *.hxx \
  44771. + *.hpp \
  44772. + *.h++ \
  44773. + *.idl \
  44774. + *.odl \
  44775. + *.cs \
  44776. + *.php \
  44777. + *.php3 \
  44778. + *.inc \
  44779. + *.m \
  44780. + *.mm \
  44781. + *.dox \
  44782. + *.py \
  44783. + *.C \
  44784. + *.CC \
  44785. + *.C++ \
  44786. + *.II \
  44787. + *.I++ \
  44788. + *.H \
  44789. + *.HH \
  44790. + *.H++ \
  44791. + *.CS \
  44792. + *.PHP \
  44793. + *.PHP3 \
  44794. + *.M \
  44795. + *.MM \
  44796. + *.PY
  44797. +RECURSIVE = NO
  44798. +EXCLUDE =
  44799. +EXCLUDE_SYMLINKS = NO
  44800. +EXCLUDE_PATTERNS =
  44801. +EXAMPLE_PATH =
  44802. +EXAMPLE_PATTERNS = *
  44803. +EXAMPLE_RECURSIVE = NO
  44804. +IMAGE_PATH =
  44805. +INPUT_FILTER =
  44806. +FILTER_PATTERNS =
  44807. +FILTER_SOURCE_FILES = NO
  44808. +#---------------------------------------------------------------------------
  44809. +# configuration options related to source browsing
  44810. +#---------------------------------------------------------------------------
  44811. +SOURCE_BROWSER = NO
  44812. +INLINE_SOURCES = NO
  44813. +STRIP_CODE_COMMENTS = YES
  44814. +REFERENCED_BY_RELATION = YES
  44815. +REFERENCES_RELATION = YES
  44816. +USE_HTAGS = NO
  44817. +VERBATIM_HEADERS = NO
  44818. +#---------------------------------------------------------------------------
  44819. +# configuration options related to the alphabetical class index
  44820. +#---------------------------------------------------------------------------
  44821. +ALPHABETICAL_INDEX = NO
  44822. +COLS_IN_ALPHA_INDEX = 5
  44823. +IGNORE_PREFIX =
  44824. +#---------------------------------------------------------------------------
  44825. +# configuration options related to the HTML output
  44826. +#---------------------------------------------------------------------------
  44827. +GENERATE_HTML = YES
  44828. +HTML_OUTPUT = html
  44829. +HTML_FILE_EXTENSION = .html
  44830. +HTML_HEADER =
  44831. +HTML_FOOTER =
  44832. +HTML_STYLESHEET =
  44833. +HTML_ALIGN_MEMBERS = YES
  44834. +GENERATE_HTMLHELP = NO
  44835. +CHM_FILE =
  44836. +HHC_LOCATION =
  44837. +GENERATE_CHI = NO
  44838. +BINARY_TOC = NO
  44839. +TOC_EXPAND = NO
  44840. +DISABLE_INDEX = NO
  44841. +ENUM_VALUES_PER_LINE = 4
  44842. +GENERATE_TREEVIEW = YES
  44843. +TREEVIEW_WIDTH = 250
  44844. +#---------------------------------------------------------------------------
  44845. +# configuration options related to the LaTeX output
  44846. +#---------------------------------------------------------------------------
  44847. +GENERATE_LATEX = NO
  44848. +LATEX_OUTPUT = latex
  44849. +LATEX_CMD_NAME = latex
  44850. +MAKEINDEX_CMD_NAME = makeindex
  44851. +COMPACT_LATEX = NO
  44852. +PAPER_TYPE = a4wide
  44853. +EXTRA_PACKAGES =
  44854. +LATEX_HEADER =
  44855. +PDF_HYPERLINKS = NO
  44856. +USE_PDFLATEX = NO
  44857. +LATEX_BATCHMODE = NO
  44858. +LATEX_HIDE_INDICES = NO
  44859. +#---------------------------------------------------------------------------
  44860. +# configuration options related to the RTF output
  44861. +#---------------------------------------------------------------------------
  44862. +GENERATE_RTF = NO
  44863. +RTF_OUTPUT = rtf
  44864. +COMPACT_RTF = NO
  44865. +RTF_HYPERLINKS = NO
  44866. +RTF_STYLESHEET_FILE =
  44867. +RTF_EXTENSIONS_FILE =
  44868. +#---------------------------------------------------------------------------
  44869. +# configuration options related to the man page output
  44870. +#---------------------------------------------------------------------------
  44871. +GENERATE_MAN = NO
  44872. +MAN_OUTPUT = man
  44873. +MAN_EXTENSION = .3
  44874. +MAN_LINKS = NO
  44875. +#---------------------------------------------------------------------------
  44876. +# configuration options related to the XML output
  44877. +#---------------------------------------------------------------------------
  44878. +GENERATE_XML = NO
  44879. +XML_OUTPUT = xml
  44880. +XML_SCHEMA =
  44881. +XML_DTD =
  44882. +XML_PROGRAMLISTING = YES
  44883. +#---------------------------------------------------------------------------
  44884. +# configuration options for the AutoGen Definitions output
  44885. +#---------------------------------------------------------------------------
  44886. +GENERATE_AUTOGEN_DEF = NO
  44887. +#---------------------------------------------------------------------------
  44888. +# configuration options related to the Perl module output
  44889. +#---------------------------------------------------------------------------
  44890. +GENERATE_PERLMOD = NO
  44891. +PERLMOD_LATEX = NO
  44892. +PERLMOD_PRETTY = YES
  44893. +PERLMOD_MAKEVAR_PREFIX =
  44894. +#---------------------------------------------------------------------------
  44895. +# Configuration options related to the preprocessor
  44896. +#---------------------------------------------------------------------------
  44897. +ENABLE_PREPROCESSING = YES
  44898. +MACRO_EXPANSION = NO
  44899. +EXPAND_ONLY_PREDEF = NO
  44900. +SEARCH_INCLUDES = YES
  44901. +INCLUDE_PATH =
  44902. +INCLUDE_FILE_PATTERNS =
  44903. +PREDEFINED = DEBUG DEBUG_MEMORY
  44904. +EXPAND_AS_DEFINED =
  44905. +SKIP_FUNCTION_MACROS = YES
  44906. +#---------------------------------------------------------------------------
  44907. +# Configuration::additions related to external references
  44908. +#---------------------------------------------------------------------------
  44909. +TAGFILES =
  44910. +GENERATE_TAGFILE =
  44911. +ALLEXTERNALS = NO
  44912. +EXTERNAL_GROUPS = YES
  44913. +PERL_PATH = /usr/bin/perl
  44914. +#---------------------------------------------------------------------------
  44915. +# Configuration options related to the dot tool
  44916. +#---------------------------------------------------------------------------
  44917. +CLASS_DIAGRAMS = YES
  44918. +HIDE_UNDOC_RELATIONS = YES
  44919. +HAVE_DOT = NO
  44920. +CLASS_GRAPH = YES
  44921. +COLLABORATION_GRAPH = YES
  44922. +GROUP_GRAPHS = YES
  44923. +UML_LOOK = NO
  44924. +TEMPLATE_RELATIONS = NO
  44925. +INCLUDE_GRAPH = NO
  44926. +INCLUDED_BY_GRAPH = YES
  44927. +CALL_GRAPH = NO
  44928. +GRAPHICAL_HIERARCHY = YES
  44929. +DIRECTORY_GRAPH = YES
  44930. +DOT_IMAGE_FORMAT = png
  44931. +DOT_PATH =
  44932. +DOTFILE_DIRS =
  44933. +MAX_DOT_GRAPH_DEPTH = 1000
  44934. +DOT_TRANSPARENT = NO
  44935. +DOT_MULTI_TARGETS = NO
  44936. +GENERATE_LEGEND = YES
  44937. +DOT_CLEANUP = YES
  44938. +#---------------------------------------------------------------------------
  44939. +# Configuration::additions related to the search engine
  44940. +#---------------------------------------------------------------------------
  44941. +SEARCHENGINE = NO
  44942. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_cc.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c
  44943. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_cc.c 1969-12-31 18:00:00.000000000 -0600
  44944. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c 2014-12-11 14:02:55.328418001 -0600
  44945. @@ -0,0 +1,532 @@
  44946. +/* =========================================================================
  44947. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  44948. + * $Revision: #4 $
  44949. + * $Date: 2010/11/04 $
  44950. + * $Change: 1621692 $
  44951. + *
  44952. + * Synopsys Portability Library Software and documentation
  44953. + * (hereinafter, "Software") is an Unsupported proprietary work of
  44954. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  44955. + * between Synopsys and you.
  44956. + *
  44957. + * The Software IS NOT an item of Licensed Software or Licensed Product
  44958. + * under any End User Software License Agreement or Agreement for
  44959. + * Licensed Product with Synopsys or any supplement thereto. You are
  44960. + * permitted to use and redistribute this Software in source and binary
  44961. + * forms, with or without modification, provided that redistributions
  44962. + * of source code must retain this notice. You may not view, use,
  44963. + * disclose, copy or distribute this file or any information contained
  44964. + * herein except pursuant to this license grant from Synopsys. If you
  44965. + * do not agree with this notice, including the disclaimer below, then
  44966. + * you are not authorized to use the Software.
  44967. + *
  44968. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44969. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44970. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44971. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44972. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44973. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44974. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44975. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44976. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44977. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44978. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44979. + * DAMAGE.
  44980. + * ========================================================================= */
  44981. +#ifdef DWC_CCLIB
  44982. +
  44983. +#include "dwc_cc.h"
  44984. +
  44985. +typedef struct dwc_cc
  44986. +{
  44987. + uint32_t uid;
  44988. + uint8_t chid[16];
  44989. + uint8_t cdid[16];
  44990. + uint8_t ck[16];
  44991. + uint8_t *name;
  44992. + uint8_t length;
  44993. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  44994. +} dwc_cc_t;
  44995. +
  44996. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  44997. +
  44998. +/** The main structure for CC management. */
  44999. +struct dwc_cc_if
  45000. +{
  45001. + dwc_mutex_t *mutex;
  45002. + char *filename;
  45003. +
  45004. + unsigned is_host:1;
  45005. +
  45006. + dwc_notifier_t *notifier;
  45007. +
  45008. + struct context_list list;
  45009. +};
  45010. +
  45011. +#ifdef DEBUG
  45012. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  45013. +{
  45014. + int i;
  45015. + DWC_PRINTF("%s: ", name);
  45016. + for (i=0; i<len; i++) {
  45017. + DWC_PRINTF("%02x ", bytes[i]);
  45018. + }
  45019. + DWC_PRINTF("\n");
  45020. +}
  45021. +#else
  45022. +#define dump_bytes(x...)
  45023. +#endif
  45024. +
  45025. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  45026. +{
  45027. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  45028. + if (!cc) {
  45029. + return NULL;
  45030. + }
  45031. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  45032. +
  45033. + if (name) {
  45034. + cc->length = length;
  45035. + cc->name = dwc_alloc(mem_ctx, length);
  45036. + if (!cc->name) {
  45037. + dwc_free(mem_ctx, cc);
  45038. + return NULL;
  45039. + }
  45040. +
  45041. + DWC_MEMCPY(cc->name, name, length);
  45042. + }
  45043. +
  45044. + return cc;
  45045. +}
  45046. +
  45047. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  45048. +{
  45049. + if (cc->name) {
  45050. + dwc_free(mem_ctx, cc->name);
  45051. + }
  45052. + dwc_free(mem_ctx, cc);
  45053. +}
  45054. +
  45055. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  45056. +{
  45057. + uint32_t uid = 0;
  45058. + dwc_cc_t *cc;
  45059. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45060. + if (cc->uid > uid) {
  45061. + uid = cc->uid;
  45062. + }
  45063. + }
  45064. +
  45065. + if (uid == 0) {
  45066. + uid = 255;
  45067. + }
  45068. +
  45069. + return uid + 1;
  45070. +}
  45071. +
  45072. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  45073. +{
  45074. + dwc_cc_t *cc;
  45075. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45076. + if (cc->uid == uid) {
  45077. + return cc;
  45078. + }
  45079. + }
  45080. + return NULL;
  45081. +}
  45082. +
  45083. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  45084. +{
  45085. + unsigned int size = 0;
  45086. + dwc_cc_t *cc;
  45087. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45088. + size += (48 + 1);
  45089. + if (cc->name) {
  45090. + size += cc->length;
  45091. + }
  45092. + }
  45093. + return size;
  45094. +}
  45095. +
  45096. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  45097. +{
  45098. + uint32_t uid = 0;
  45099. + dwc_cc_t *cc;
  45100. +
  45101. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45102. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  45103. + uid = cc->uid;
  45104. + break;
  45105. + }
  45106. + }
  45107. + return uid;
  45108. +}
  45109. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  45110. +{
  45111. + uint32_t uid = 0;
  45112. + dwc_cc_t *cc;
  45113. +
  45114. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45115. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  45116. + uid = cc->uid;
  45117. + break;
  45118. + }
  45119. + }
  45120. + return uid;
  45121. +}
  45122. +
  45123. +/* Internal cc_add */
  45124. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  45125. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  45126. +{
  45127. + dwc_cc_t *cc;
  45128. + uint32_t uid;
  45129. +
  45130. + if (cc_if->is_host) {
  45131. + uid = cc_match_cdid(cc_if, cdid);
  45132. + }
  45133. + else {
  45134. + uid = cc_match_chid(cc_if, chid);
  45135. + }
  45136. +
  45137. + if (uid) {
  45138. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  45139. + cc = cc_find(cc_if, uid);
  45140. + }
  45141. + else {
  45142. + cc = alloc_cc(mem_ctx, name, length);
  45143. + cc->uid = next_uid(cc_if);
  45144. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  45145. + }
  45146. +
  45147. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  45148. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  45149. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  45150. +
  45151. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  45152. + dump_bytes("CHID", cc->chid, 16);
  45153. + dump_bytes("CDID", cc->cdid, 16);
  45154. + dump_bytes("CK", cc->ck, 16);
  45155. + return cc->uid;
  45156. +}
  45157. +
  45158. +/* Internal cc_clear */
  45159. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  45160. +{
  45161. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  45162. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  45163. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  45164. + free_cc(mem_ctx, cc);
  45165. + }
  45166. +}
  45167. +
  45168. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  45169. + dwc_notifier_t *notifier, unsigned is_host)
  45170. +{
  45171. + dwc_cc_if_t *cc_if = NULL;
  45172. +
  45173. + /* Allocate a common_cc_if structure */
  45174. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  45175. +
  45176. + if (!cc_if)
  45177. + return NULL;
  45178. +
  45179. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  45180. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  45181. +#else
  45182. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  45183. +#endif
  45184. + if (!cc_if->mutex) {
  45185. + dwc_free(mem_ctx, cc_if);
  45186. + return NULL;
  45187. + }
  45188. +
  45189. + DWC_CIRCLEQ_INIT(&cc_if->list);
  45190. + cc_if->is_host = is_host;
  45191. + cc_if->notifier = notifier;
  45192. + return cc_if;
  45193. +}
  45194. +
  45195. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  45196. +{
  45197. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  45198. + DWC_MUTEX_FREE(cc_if->mutex);
  45199. +#else
  45200. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  45201. +#endif
  45202. + cc_clear(mem_ctx, cc_if);
  45203. + dwc_free(mem_ctx, cc_if);
  45204. +}
  45205. +
  45206. +static void cc_changed(dwc_cc_if_t *cc_if)
  45207. +{
  45208. + if (cc_if->notifier) {
  45209. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  45210. + }
  45211. +}
  45212. +
  45213. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  45214. +{
  45215. + DWC_MUTEX_LOCK(cc_if->mutex);
  45216. + cc_clear(mem_ctx, cc_if);
  45217. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45218. + cc_changed(cc_if);
  45219. +}
  45220. +
  45221. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  45222. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  45223. +{
  45224. + uint32_t uid;
  45225. +
  45226. + DWC_MUTEX_LOCK(cc_if->mutex);
  45227. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  45228. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45229. + cc_changed(cc_if);
  45230. +
  45231. + return uid;
  45232. +}
  45233. +
  45234. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  45235. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  45236. +{
  45237. + dwc_cc_t* cc;
  45238. +
  45239. + DWC_DEBUGC("Change connection context %d", id);
  45240. +
  45241. + DWC_MUTEX_LOCK(cc_if->mutex);
  45242. + cc = cc_find(cc_if, id);
  45243. + if (!cc) {
  45244. + DWC_ERROR("Uid %d not found in cc list\n", id);
  45245. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45246. + return;
  45247. + }
  45248. +
  45249. + if (chid) {
  45250. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  45251. + }
  45252. + if (cdid) {
  45253. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  45254. + }
  45255. + if (ck) {
  45256. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  45257. + }
  45258. +
  45259. + if (name) {
  45260. + if (cc->name) {
  45261. + dwc_free(mem_ctx, cc->name);
  45262. + }
  45263. + cc->name = dwc_alloc(mem_ctx, length);
  45264. + if (!cc->name) {
  45265. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  45266. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45267. + return;
  45268. + }
  45269. + cc->length = length;
  45270. + DWC_MEMCPY(cc->name, name, length);
  45271. + }
  45272. +
  45273. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45274. +
  45275. + cc_changed(cc_if);
  45276. +
  45277. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  45278. + dump_bytes("New CHID", cc->chid, 16);
  45279. + dump_bytes("New CDID", cc->cdid, 16);
  45280. + dump_bytes("New CK", cc->ck, 16);
  45281. +}
  45282. +
  45283. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  45284. +{
  45285. + dwc_cc_t *cc;
  45286. +
  45287. + DWC_DEBUGC("Removing connection context %d", id);
  45288. +
  45289. + DWC_MUTEX_LOCK(cc_if->mutex);
  45290. + cc = cc_find(cc_if, id);
  45291. + if (!cc) {
  45292. + DWC_ERROR("Uid %d not found in cc list\n", id);
  45293. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45294. + return;
  45295. + }
  45296. +
  45297. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  45298. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45299. + free_cc(mem_ctx, cc);
  45300. +
  45301. + cc_changed(cc_if);
  45302. +}
  45303. +
  45304. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  45305. +{
  45306. + uint8_t *buf, *x;
  45307. + uint8_t zero = 0;
  45308. + dwc_cc_t *cc;
  45309. +
  45310. + DWC_MUTEX_LOCK(cc_if->mutex);
  45311. + *length = cc_data_size(cc_if);
  45312. + if (!(*length)) {
  45313. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45314. + return NULL;
  45315. + }
  45316. +
  45317. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  45318. +
  45319. + buf = dwc_alloc(mem_ctx, *length);
  45320. + if (!buf) {
  45321. + *length = 0;
  45322. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45323. + return NULL;
  45324. + }
  45325. +
  45326. + x = buf;
  45327. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45328. + DWC_MEMCPY(x, cc->chid, 16);
  45329. + x += 16;
  45330. + DWC_MEMCPY(x, cc->cdid, 16);
  45331. + x += 16;
  45332. + DWC_MEMCPY(x, cc->ck, 16);
  45333. + x += 16;
  45334. + if (cc->name) {
  45335. + DWC_MEMCPY(x, &cc->length, 1);
  45336. + x += 1;
  45337. + DWC_MEMCPY(x, cc->name, cc->length);
  45338. + x += cc->length;
  45339. + }
  45340. + else {
  45341. + DWC_MEMCPY(x, &zero, 1);
  45342. + x += 1;
  45343. + }
  45344. + }
  45345. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45346. +
  45347. + return buf;
  45348. +}
  45349. +
  45350. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  45351. +{
  45352. + uint8_t name_length;
  45353. + uint8_t *name;
  45354. + uint8_t *chid;
  45355. + uint8_t *cdid;
  45356. + uint8_t *ck;
  45357. + uint32_t i = 0;
  45358. +
  45359. + DWC_MUTEX_LOCK(cc_if->mutex);
  45360. + cc_clear(mem_ctx, cc_if);
  45361. +
  45362. + while (i < length) {
  45363. + chid = &data[i];
  45364. + i += 16;
  45365. + cdid = &data[i];
  45366. + i += 16;
  45367. + ck = &data[i];
  45368. + i += 16;
  45369. +
  45370. + name_length = data[i];
  45371. + i ++;
  45372. +
  45373. + if (name_length) {
  45374. + name = &data[i];
  45375. + i += name_length;
  45376. + }
  45377. + else {
  45378. + name = NULL;
  45379. + }
  45380. +
  45381. + /* check to see if we haven't overflown the buffer */
  45382. + if (i > length) {
  45383. + DWC_ERROR("Data format error while attempting to load CCs "
  45384. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  45385. + break;
  45386. + }
  45387. +
  45388. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  45389. + }
  45390. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45391. +
  45392. + cc_changed(cc_if);
  45393. +}
  45394. +
  45395. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  45396. +{
  45397. + uint32_t uid = 0;
  45398. +
  45399. + DWC_MUTEX_LOCK(cc_if->mutex);
  45400. + uid = cc_match_chid(cc_if, chid);
  45401. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45402. + return uid;
  45403. +}
  45404. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  45405. +{
  45406. + uint32_t uid = 0;
  45407. +
  45408. + DWC_MUTEX_LOCK(cc_if->mutex);
  45409. + uid = cc_match_cdid(cc_if, cdid);
  45410. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45411. + return uid;
  45412. +}
  45413. +
  45414. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  45415. +{
  45416. + uint8_t *ck = NULL;
  45417. + dwc_cc_t *cc;
  45418. +
  45419. + DWC_MUTEX_LOCK(cc_if->mutex);
  45420. + cc = cc_find(cc_if, id);
  45421. + if (cc) {
  45422. + ck = cc->ck;
  45423. + }
  45424. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45425. +
  45426. + return ck;
  45427. +
  45428. +}
  45429. +
  45430. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  45431. +{
  45432. + uint8_t *retval = NULL;
  45433. + dwc_cc_t *cc;
  45434. +
  45435. + DWC_MUTEX_LOCK(cc_if->mutex);
  45436. + cc = cc_find(cc_if, id);
  45437. + if (cc) {
  45438. + retval = cc->chid;
  45439. + }
  45440. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45441. +
  45442. + return retval;
  45443. +}
  45444. +
  45445. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  45446. +{
  45447. + uint8_t *retval = NULL;
  45448. + dwc_cc_t *cc;
  45449. +
  45450. + DWC_MUTEX_LOCK(cc_if->mutex);
  45451. + cc = cc_find(cc_if, id);
  45452. + if (cc) {
  45453. + retval = cc->cdid;
  45454. + }
  45455. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45456. +
  45457. + return retval;
  45458. +}
  45459. +
  45460. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  45461. +{
  45462. + uint8_t *retval = NULL;
  45463. + dwc_cc_t *cc;
  45464. +
  45465. + DWC_MUTEX_LOCK(cc_if->mutex);
  45466. + *length = 0;
  45467. + cc = cc_find(cc_if, id);
  45468. + if (cc) {
  45469. + *length = cc->length;
  45470. + retval = cc->name;
  45471. + }
  45472. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  45473. +
  45474. + return retval;
  45475. +}
  45476. +
  45477. +#endif /* DWC_CCLIB */
  45478. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_cc.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h
  45479. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_cc.h 1969-12-31 18:00:00.000000000 -0600
  45480. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h 2014-12-11 14:02:55.348418001 -0600
  45481. @@ -0,0 +1,224 @@
  45482. +/* =========================================================================
  45483. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  45484. + * $Revision: #4 $
  45485. + * $Date: 2010/09/28 $
  45486. + * $Change: 1596182 $
  45487. + *
  45488. + * Synopsys Portability Library Software and documentation
  45489. + * (hereinafter, "Software") is an Unsupported proprietary work of
  45490. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  45491. + * between Synopsys and you.
  45492. + *
  45493. + * The Software IS NOT an item of Licensed Software or Licensed Product
  45494. + * under any End User Software License Agreement or Agreement for
  45495. + * Licensed Product with Synopsys or any supplement thereto. You are
  45496. + * permitted to use and redistribute this Software in source and binary
  45497. + * forms, with or without modification, provided that redistributions
  45498. + * of source code must retain this notice. You may not view, use,
  45499. + * disclose, copy or distribute this file or any information contained
  45500. + * herein except pursuant to this license grant from Synopsys. If you
  45501. + * do not agree with this notice, including the disclaimer below, then
  45502. + * you are not authorized to use the Software.
  45503. + *
  45504. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45505. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45506. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  45507. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  45508. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  45509. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  45510. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  45511. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  45512. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45513. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  45514. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  45515. + * DAMAGE.
  45516. + * ========================================================================= */
  45517. +#ifndef _DWC_CC_H_
  45518. +#define _DWC_CC_H_
  45519. +
  45520. +#ifdef __cplusplus
  45521. +extern "C" {
  45522. +#endif
  45523. +
  45524. +/** @file
  45525. + *
  45526. + * This file defines the Context Context library.
  45527. + *
  45528. + * The main data structure is dwc_cc_if_t which is returned by either the
  45529. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  45530. + * function. The data structure is opaque and should only be manipulated via the
  45531. + * functions provied in this API.
  45532. + *
  45533. + * It manages a list of connection contexts and operations can be performed to
  45534. + * add, remove, query, search, and change, those contexts. Additionally,
  45535. + * a dwc_notifier_t object can be requested from the manager so that
  45536. + * the user can be notified whenever the context list has changed.
  45537. + */
  45538. +
  45539. +#include "dwc_os.h"
  45540. +#include "dwc_list.h"
  45541. +#include "dwc_notifier.h"
  45542. +
  45543. +
  45544. +/* Notifications */
  45545. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  45546. +
  45547. +struct dwc_cc_if;
  45548. +typedef struct dwc_cc_if dwc_cc_if_t;
  45549. +
  45550. +
  45551. +/** @name Connection Context Operations */
  45552. +/** @{ */
  45553. +
  45554. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  45555. + * fields to default values, and returns a pointer to the structure or NULL on
  45556. + * error. */
  45557. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  45558. + dwc_notifier_t *notifier, unsigned is_host);
  45559. +
  45560. +/** Frees the memory for the specified CC structure allocated from
  45561. + * dwc_cc_if_alloc(). */
  45562. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  45563. +
  45564. +/** Removes all contexts from the connection context list */
  45565. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  45566. +
  45567. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  45568. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  45569. + * not overwritten.
  45570. + *
  45571. + * @param cc_if The cc_if structure.
  45572. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  45573. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  45574. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  45575. + * @param name An optional host friendly name as defined in the association model
  45576. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  45577. + * @param length The length othe unicode string.
  45578. + * @return A unique identifier used to refer to this context that is valid for
  45579. + * as long as this context is still in the list. */
  45580. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  45581. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  45582. + uint8_t length);
  45583. +
  45584. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  45585. + * list, preserving any accumulated statistics. This would typically be called
  45586. + * if the host decideds to change the context with a SET_CONNECTION request.
  45587. + *
  45588. + * @param cc_if The cc_if structure.
  45589. + * @param id The identifier of the connection context.
  45590. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  45591. + * indicates no change.
  45592. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  45593. + * indicates no change.
  45594. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  45595. + * indicates no change.
  45596. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  45597. + * @param length Length of name. */
  45598. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  45599. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  45600. + uint8_t *name, uint8_t length);
  45601. +
  45602. +/** Remove the specified connection context.
  45603. + * @param cc_if The cc_if structure.
  45604. + * @param id The identifier of the connection context to remove. */
  45605. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  45606. +
  45607. +/** Get a binary block of data for the connection context list and attributes.
  45608. + * This data can be used by the OS specific driver to save the connection
  45609. + * context list into non-volatile memory.
  45610. + *
  45611. + * @param cc_if The cc_if structure.
  45612. + * @param length Return the length of the data buffer.
  45613. + * @return A pointer to the data buffer. The memory for this buffer should be
  45614. + * freed with DWC_FREE() after use. */
  45615. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  45616. + unsigned int *length);
  45617. +
  45618. +/** Restore the connection context list from the binary data that was previously
  45619. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  45620. + * driver to load a connection context list from non-volatile memory.
  45621. + *
  45622. + * @param cc_if The cc_if structure.
  45623. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  45624. + * @param length The length of the data. */
  45625. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  45626. + uint8_t *data, unsigned int length);
  45627. +
  45628. +/** Find the connection context from the specified CHID.
  45629. + *
  45630. + * @param cc_if The cc_if structure.
  45631. + * @param chid A pointer to the CHID data.
  45632. + * @return A non-zero identifier of the connection context if the CHID matches.
  45633. + * Otherwise returns 0. */
  45634. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  45635. +
  45636. +/** Find the connection context from the specified CDID.
  45637. + *
  45638. + * @param cc_if The cc_if structure.
  45639. + * @param cdid A pointer to the CDID data.
  45640. + * @return A non-zero identifier of the connection context if the CHID matches.
  45641. + * Otherwise returns 0. */
  45642. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  45643. +
  45644. +/** Retrieve the CK from the specified connection context.
  45645. + *
  45646. + * @param cc_if The cc_if structure.
  45647. + * @param id The identifier of the connection context.
  45648. + * @return A pointer to the CK data. The memory does not need to be freed. */
  45649. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  45650. +
  45651. +/** Retrieve the CHID from the specified connection context.
  45652. + *
  45653. + * @param cc_if The cc_if structure.
  45654. + * @param id The identifier of the connection context.
  45655. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  45656. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  45657. +
  45658. +/** Retrieve the CDID from the specified connection context.
  45659. + *
  45660. + * @param cc_if The cc_if structure.
  45661. + * @param id The identifier of the connection context.
  45662. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  45663. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  45664. +
  45665. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  45666. +
  45667. +/** Checks a buffer for non-zero.
  45668. + * @param id A pointer to a 16 byte buffer.
  45669. + * @return true if the 16 byte value is non-zero. */
  45670. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  45671. + int i;
  45672. + for (i=0; i<16; i++) {
  45673. + if (id[i]) return 1;
  45674. + }
  45675. + return 0;
  45676. +}
  45677. +
  45678. +/** Checks a buffer for zero.
  45679. + * @param id A pointer to a 16 byte buffer.
  45680. + * @return true if the 16 byte value is zero. */
  45681. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  45682. + return !dwc_assoc_is_not_zero_id(id);
  45683. +}
  45684. +
  45685. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  45686. + * buffer. */
  45687. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  45688. + char *ptr = buffer;
  45689. + int i;
  45690. + for (i=0; i<16; i++) {
  45691. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  45692. + if (i < 15) {
  45693. + ptr += DWC_SPRINTF(ptr, " ");
  45694. + }
  45695. + }
  45696. + return ptr - buffer;
  45697. +}
  45698. +
  45699. +/** @} */
  45700. +
  45701. +#ifdef __cplusplus
  45702. +}
  45703. +#endif
  45704. +
  45705. +#endif /* _DWC_CC_H_ */
  45706. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  45707. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 1969-12-31 18:00:00.000000000 -0600
  45708. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 2014-12-11 14:02:55.352418001 -0600
  45709. @@ -0,0 +1,1308 @@
  45710. +#include "dwc_os.h"
  45711. +#include "dwc_list.h"
  45712. +
  45713. +#ifdef DWC_CCLIB
  45714. +# include "dwc_cc.h"
  45715. +#endif
  45716. +
  45717. +#ifdef DWC_CRYPTOLIB
  45718. +# include "dwc_modpow.h"
  45719. +# include "dwc_dh.h"
  45720. +# include "dwc_crypto.h"
  45721. +#endif
  45722. +
  45723. +#ifdef DWC_NOTIFYLIB
  45724. +# include "dwc_notifier.h"
  45725. +#endif
  45726. +
  45727. +/* OS-Level Implementations */
  45728. +
  45729. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  45730. +
  45731. +
  45732. +/* MISC */
  45733. +
  45734. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  45735. +{
  45736. + return memset(dest, byte, size);
  45737. +}
  45738. +
  45739. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  45740. +{
  45741. + return memcpy(dest, src, size);
  45742. +}
  45743. +
  45744. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  45745. +{
  45746. + bcopy(src, dest, size);
  45747. + return dest;
  45748. +}
  45749. +
  45750. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  45751. +{
  45752. + return memcmp(m1, m2, size);
  45753. +}
  45754. +
  45755. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  45756. +{
  45757. + return strncmp(s1, s2, size);
  45758. +}
  45759. +
  45760. +int DWC_STRCMP(void *s1, void *s2)
  45761. +{
  45762. + return strcmp(s1, s2);
  45763. +}
  45764. +
  45765. +int DWC_STRLEN(char const *str)
  45766. +{
  45767. + return strlen(str);
  45768. +}
  45769. +
  45770. +char *DWC_STRCPY(char *to, char const *from)
  45771. +{
  45772. + return strcpy(to, from);
  45773. +}
  45774. +
  45775. +char *DWC_STRDUP(char const *str)
  45776. +{
  45777. + int len = DWC_STRLEN(str) + 1;
  45778. + char *new = DWC_ALLOC_ATOMIC(len);
  45779. +
  45780. + if (!new) {
  45781. + return NULL;
  45782. + }
  45783. +
  45784. + DWC_MEMCPY(new, str, len);
  45785. + return new;
  45786. +}
  45787. +
  45788. +int DWC_ATOI(char *str, int32_t *value)
  45789. +{
  45790. + char *end = NULL;
  45791. +
  45792. + *value = strtol(str, &end, 0);
  45793. + if (*end == '\0') {
  45794. + return 0;
  45795. + }
  45796. +
  45797. + return -1;
  45798. +}
  45799. +
  45800. +int DWC_ATOUI(char *str, uint32_t *value)
  45801. +{
  45802. + char *end = NULL;
  45803. +
  45804. + *value = strtoul(str, &end, 0);
  45805. + if (*end == '\0') {
  45806. + return 0;
  45807. + }
  45808. +
  45809. + return -1;
  45810. +}
  45811. +
  45812. +
  45813. +#ifdef DWC_UTFLIB
  45814. +/* From usbstring.c */
  45815. +
  45816. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  45817. +{
  45818. + int count = 0;
  45819. + u8 c;
  45820. + u16 uchar;
  45821. +
  45822. + /* this insists on correct encodings, though not minimal ones.
  45823. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  45824. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  45825. + */
  45826. + while (len != 0 && (c = (u8) *s++) != 0) {
  45827. + if (unlikely(c & 0x80)) {
  45828. + // 2-byte sequence:
  45829. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  45830. + if ((c & 0xe0) == 0xc0) {
  45831. + uchar = (c & 0x1f) << 6;
  45832. +
  45833. + c = (u8) *s++;
  45834. + if ((c & 0xc0) != 0xc0)
  45835. + goto fail;
  45836. + c &= 0x3f;
  45837. + uchar |= c;
  45838. +
  45839. + // 3-byte sequence (most CJKV characters):
  45840. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  45841. + } else if ((c & 0xf0) == 0xe0) {
  45842. + uchar = (c & 0x0f) << 12;
  45843. +
  45844. + c = (u8) *s++;
  45845. + if ((c & 0xc0) != 0xc0)
  45846. + goto fail;
  45847. + c &= 0x3f;
  45848. + uchar |= c << 6;
  45849. +
  45850. + c = (u8) *s++;
  45851. + if ((c & 0xc0) != 0xc0)
  45852. + goto fail;
  45853. + c &= 0x3f;
  45854. + uchar |= c;
  45855. +
  45856. + /* no bogus surrogates */
  45857. + if (0xd800 <= uchar && uchar <= 0xdfff)
  45858. + goto fail;
  45859. +
  45860. + // 4-byte sequence (surrogate pairs, currently rare):
  45861. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  45862. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  45863. + // (uuuuu = wwww + 1)
  45864. + // FIXME accept the surrogate code points (only)
  45865. + } else
  45866. + goto fail;
  45867. + } else
  45868. + uchar = c;
  45869. + put_unaligned (cpu_to_le16 (uchar), cp++);
  45870. + count++;
  45871. + len--;
  45872. + }
  45873. + return count;
  45874. +fail:
  45875. + return -1;
  45876. +}
  45877. +
  45878. +#endif /* DWC_UTFLIB */
  45879. +
  45880. +
  45881. +/* dwc_debug.h */
  45882. +
  45883. +dwc_bool_t DWC_IN_IRQ(void)
  45884. +{
  45885. +// return in_irq();
  45886. + return 0;
  45887. +}
  45888. +
  45889. +dwc_bool_t DWC_IN_BH(void)
  45890. +{
  45891. +// return in_softirq();
  45892. + return 0;
  45893. +}
  45894. +
  45895. +void DWC_VPRINTF(char *format, va_list args)
  45896. +{
  45897. + vprintf(format, args);
  45898. +}
  45899. +
  45900. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  45901. +{
  45902. + return vsnprintf(str, size, format, args);
  45903. +}
  45904. +
  45905. +void DWC_PRINTF(char *format, ...)
  45906. +{
  45907. + va_list args;
  45908. +
  45909. + va_start(args, format);
  45910. + DWC_VPRINTF(format, args);
  45911. + va_end(args);
  45912. +}
  45913. +
  45914. +int DWC_SPRINTF(char *buffer, char *format, ...)
  45915. +{
  45916. + int retval;
  45917. + va_list args;
  45918. +
  45919. + va_start(args, format);
  45920. + retval = vsprintf(buffer, format, args);
  45921. + va_end(args);
  45922. + return retval;
  45923. +}
  45924. +
  45925. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  45926. +{
  45927. + int retval;
  45928. + va_list args;
  45929. +
  45930. + va_start(args, format);
  45931. + retval = vsnprintf(buffer, size, format, args);
  45932. + va_end(args);
  45933. + return retval;
  45934. +}
  45935. +
  45936. +void __DWC_WARN(char *format, ...)
  45937. +{
  45938. + va_list args;
  45939. +
  45940. + va_start(args, format);
  45941. + DWC_VPRINTF(format, args);
  45942. + va_end(args);
  45943. +}
  45944. +
  45945. +void __DWC_ERROR(char *format, ...)
  45946. +{
  45947. + va_list args;
  45948. +
  45949. + va_start(args, format);
  45950. + DWC_VPRINTF(format, args);
  45951. + va_end(args);
  45952. +}
  45953. +
  45954. +void DWC_EXCEPTION(char *format, ...)
  45955. +{
  45956. + va_list args;
  45957. +
  45958. + va_start(args, format);
  45959. + DWC_VPRINTF(format, args);
  45960. + va_end(args);
  45961. +// BUG_ON(1); ???
  45962. +}
  45963. +
  45964. +#ifdef DEBUG
  45965. +void __DWC_DEBUG(char *format, ...)
  45966. +{
  45967. + va_list args;
  45968. +
  45969. + va_start(args, format);
  45970. + DWC_VPRINTF(format, args);
  45971. + va_end(args);
  45972. +}
  45973. +#endif
  45974. +
  45975. +
  45976. +/* dwc_mem.h */
  45977. +
  45978. +#if 0
  45979. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  45980. + uint32_t align,
  45981. + uint32_t alloc)
  45982. +{
  45983. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  45984. + size, align, alloc);
  45985. + return (dwc_pool_t *)pool;
  45986. +}
  45987. +
  45988. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  45989. +{
  45990. + dma_pool_destroy((struct dma_pool *)pool);
  45991. +}
  45992. +
  45993. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  45994. +{
  45995. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  45996. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  45997. +}
  45998. +
  45999. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  46000. +{
  46001. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  46002. + memset(..);
  46003. +}
  46004. +
  46005. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  46006. +{
  46007. + dma_pool_free(pool, vaddr, daddr);
  46008. +}
  46009. +#endif
  46010. +
  46011. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  46012. +{
  46013. + if (error)
  46014. + return;
  46015. + *(bus_addr_t *)arg = segs[0].ds_addr;
  46016. +}
  46017. +
  46018. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  46019. +{
  46020. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  46021. + int error;
  46022. +
  46023. + error = bus_dma_tag_create(
  46024. +#if __FreeBSD_version >= 700000
  46025. + bus_get_dma_tag(dma->dev), /* parent */
  46026. +#else
  46027. + NULL, /* parent */
  46028. +#endif
  46029. + 4, 0, /* alignment, bounds */
  46030. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  46031. + BUS_SPACE_MAXADDR, /* highaddr */
  46032. + NULL, NULL, /* filter, filterarg */
  46033. + size, /* maxsize */
  46034. + 1, /* nsegments */
  46035. + size, /* maxsegsize */
  46036. + 0, /* flags */
  46037. + NULL, /* lockfunc */
  46038. + NULL, /* lockarg */
  46039. + &dma->dma_tag);
  46040. + if (error) {
  46041. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  46042. + __func__, error);
  46043. + goto fail_0;
  46044. + }
  46045. +
  46046. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  46047. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  46048. + if (error) {
  46049. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  46050. + __func__, (uintmax_t)size, error);
  46051. + goto fail_1;
  46052. + }
  46053. +
  46054. + dma->dma_paddr = 0;
  46055. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  46056. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  46057. + if (error || dma->dma_paddr == 0) {
  46058. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  46059. + __func__, error);
  46060. + goto fail_2;
  46061. + }
  46062. +
  46063. + *dma_addr = dma->dma_paddr;
  46064. + return dma->dma_vaddr;
  46065. +
  46066. +fail_2:
  46067. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  46068. +fail_1:
  46069. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  46070. + bus_dma_tag_destroy(dma->dma_tag);
  46071. +fail_0:
  46072. + dma->dma_map = NULL;
  46073. + dma->dma_tag = NULL;
  46074. +
  46075. + return NULL;
  46076. +}
  46077. +
  46078. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  46079. +{
  46080. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  46081. +
  46082. + if (dma->dma_tag == NULL)
  46083. + return;
  46084. + if (dma->dma_map != NULL) {
  46085. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  46086. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  46087. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  46088. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  46089. + dma->dma_map = NULL;
  46090. + }
  46091. +
  46092. + bus_dma_tag_destroy(dma->dma_tag);
  46093. + dma->dma_tag = NULL;
  46094. +}
  46095. +
  46096. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  46097. +{
  46098. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  46099. +}
  46100. +
  46101. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  46102. +{
  46103. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  46104. +}
  46105. +
  46106. +void __DWC_FREE(void *mem_ctx, void *addr)
  46107. +{
  46108. + free(addr, M_DEVBUF);
  46109. +}
  46110. +
  46111. +
  46112. +#ifdef DWC_CRYPTOLIB
  46113. +/* dwc_crypto.h */
  46114. +
  46115. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  46116. +{
  46117. + get_random_bytes(buffer, length);
  46118. +}
  46119. +
  46120. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  46121. +{
  46122. + struct crypto_blkcipher *tfm;
  46123. + struct blkcipher_desc desc;
  46124. + struct scatterlist sgd;
  46125. + struct scatterlist sgs;
  46126. +
  46127. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  46128. + if (tfm == NULL) {
  46129. + printk("failed to load transform for aes CBC\n");
  46130. + return -1;
  46131. + }
  46132. +
  46133. + crypto_blkcipher_setkey(tfm, key, keylen);
  46134. + crypto_blkcipher_set_iv(tfm, iv, 16);
  46135. +
  46136. + sg_init_one(&sgd, out, messagelen);
  46137. + sg_init_one(&sgs, message, messagelen);
  46138. +
  46139. + desc.tfm = tfm;
  46140. + desc.flags = 0;
  46141. +
  46142. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  46143. + crypto_free_blkcipher(tfm);
  46144. + DWC_ERROR("AES CBC encryption failed");
  46145. + return -1;
  46146. + }
  46147. +
  46148. + crypto_free_blkcipher(tfm);
  46149. + return 0;
  46150. +}
  46151. +
  46152. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  46153. +{
  46154. + struct crypto_hash *tfm;
  46155. + struct hash_desc desc;
  46156. + struct scatterlist sg;
  46157. +
  46158. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  46159. + if (IS_ERR(tfm)) {
  46160. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  46161. + return 0;
  46162. + }
  46163. + desc.tfm = tfm;
  46164. + desc.flags = 0;
  46165. +
  46166. + sg_init_one(&sg, message, len);
  46167. + crypto_hash_digest(&desc, &sg, len, out);
  46168. + crypto_free_hash(tfm);
  46169. +
  46170. + return 1;
  46171. +}
  46172. +
  46173. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  46174. + uint8_t *key, uint32_t keylen, uint8_t *out)
  46175. +{
  46176. + struct crypto_hash *tfm;
  46177. + struct hash_desc desc;
  46178. + struct scatterlist sg;
  46179. +
  46180. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  46181. + if (IS_ERR(tfm)) {
  46182. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  46183. + return 0;
  46184. + }
  46185. + desc.tfm = tfm;
  46186. + desc.flags = 0;
  46187. +
  46188. + sg_init_one(&sg, message, messagelen);
  46189. + crypto_hash_setkey(tfm, key, keylen);
  46190. + crypto_hash_digest(&desc, &sg, messagelen, out);
  46191. + crypto_free_hash(tfm);
  46192. +
  46193. + return 1;
  46194. +}
  46195. +
  46196. +#endif /* DWC_CRYPTOLIB */
  46197. +
  46198. +
  46199. +/* Byte Ordering Conversions */
  46200. +
  46201. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  46202. +{
  46203. +#ifdef __LITTLE_ENDIAN
  46204. + return *p;
  46205. +#else
  46206. + uint8_t *u_p = (uint8_t *)p;
  46207. +
  46208. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  46209. +#endif
  46210. +}
  46211. +
  46212. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  46213. +{
  46214. +#ifdef __BIG_ENDIAN
  46215. + return *p;
  46216. +#else
  46217. + uint8_t *u_p = (uint8_t *)p;
  46218. +
  46219. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  46220. +#endif
  46221. +}
  46222. +
  46223. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  46224. +{
  46225. +#ifdef __LITTLE_ENDIAN
  46226. + return *p;
  46227. +#else
  46228. + uint8_t *u_p = (uint8_t *)p;
  46229. +
  46230. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  46231. +#endif
  46232. +}
  46233. +
  46234. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  46235. +{
  46236. +#ifdef __BIG_ENDIAN
  46237. + return *p;
  46238. +#else
  46239. + uint8_t *u_p = (uint8_t *)p;
  46240. +
  46241. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  46242. +#endif
  46243. +}
  46244. +
  46245. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  46246. +{
  46247. +#ifdef __LITTLE_ENDIAN
  46248. + return *p;
  46249. +#else
  46250. + uint8_t *u_p = (uint8_t *)p;
  46251. + return (u_p[1] | (u_p[0] << 8));
  46252. +#endif
  46253. +}
  46254. +
  46255. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  46256. +{
  46257. +#ifdef __BIG_ENDIAN
  46258. + return *p;
  46259. +#else
  46260. + uint8_t *u_p = (uint8_t *)p;
  46261. + return (u_p[1] | (u_p[0] << 8));
  46262. +#endif
  46263. +}
  46264. +
  46265. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  46266. +{
  46267. +#ifdef __LITTLE_ENDIAN
  46268. + return *p;
  46269. +#else
  46270. + uint8_t *u_p = (uint8_t *)p;
  46271. + return (u_p[1] | (u_p[0] << 8));
  46272. +#endif
  46273. +}
  46274. +
  46275. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  46276. +{
  46277. +#ifdef __BIG_ENDIAN
  46278. + return *p;
  46279. +#else
  46280. + uint8_t *u_p = (uint8_t *)p;
  46281. + return (u_p[1] | (u_p[0] << 8));
  46282. +#endif
  46283. +}
  46284. +
  46285. +
  46286. +/* Registers */
  46287. +
  46288. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  46289. +{
  46290. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  46291. + bus_size_t ior = (bus_size_t)reg;
  46292. +
  46293. + return bus_space_read_4(io->iot, io->ioh, ior);
  46294. +}
  46295. +
  46296. +#if 0
  46297. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  46298. +{
  46299. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  46300. + bus_size_t ior = (bus_size_t)reg;
  46301. +
  46302. + return bus_space_read_8(io->iot, io->ioh, ior);
  46303. +}
  46304. +#endif
  46305. +
  46306. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  46307. +{
  46308. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  46309. + bus_size_t ior = (bus_size_t)reg;
  46310. +
  46311. + bus_space_write_4(io->iot, io->ioh, ior, value);
  46312. +}
  46313. +
  46314. +#if 0
  46315. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  46316. +{
  46317. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  46318. + bus_size_t ior = (bus_size_t)reg;
  46319. +
  46320. + bus_space_write_8(io->iot, io->ioh, ior, value);
  46321. +}
  46322. +#endif
  46323. +
  46324. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  46325. + uint32_t set_mask)
  46326. +{
  46327. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  46328. + bus_size_t ior = (bus_size_t)reg;
  46329. +
  46330. + bus_space_write_4(io->iot, io->ioh, ior,
  46331. + (bus_space_read_4(io->iot, io->ioh, ior) &
  46332. + ~clear_mask) | set_mask);
  46333. +}
  46334. +
  46335. +#if 0
  46336. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  46337. + uint64_t set_mask)
  46338. +{
  46339. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  46340. + bus_size_t ior = (bus_size_t)reg;
  46341. +
  46342. + bus_space_write_8(io->iot, io->ioh, ior,
  46343. + (bus_space_read_8(io->iot, io->ioh, ior) &
  46344. + ~clear_mask) | set_mask);
  46345. +}
  46346. +#endif
  46347. +
  46348. +
  46349. +/* Locking */
  46350. +
  46351. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  46352. +{
  46353. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  46354. +
  46355. + if (!sl) {
  46356. + DWC_ERROR("Cannot allocate memory for spinlock");
  46357. + return NULL;
  46358. + }
  46359. +
  46360. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  46361. + return (dwc_spinlock_t *)sl;
  46362. +}
  46363. +
  46364. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  46365. +{
  46366. + struct mtx *sl = (struct mtx *)lock;
  46367. +
  46368. + mtx_destroy(sl);
  46369. + DWC_FREE(sl);
  46370. +}
  46371. +
  46372. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  46373. +{
  46374. + mtx_lock_spin((struct mtx *)lock); // ???
  46375. +}
  46376. +
  46377. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  46378. +{
  46379. + mtx_unlock_spin((struct mtx *)lock); // ???
  46380. +}
  46381. +
  46382. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  46383. +{
  46384. + mtx_lock_spin((struct mtx *)lock);
  46385. +}
  46386. +
  46387. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  46388. +{
  46389. + mtx_unlock_spin((struct mtx *)lock);
  46390. +}
  46391. +
  46392. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  46393. +{
  46394. + struct mtx *m;
  46395. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  46396. +
  46397. + if (!mutex) {
  46398. + DWC_ERROR("Cannot allocate memory for mutex");
  46399. + return NULL;
  46400. + }
  46401. +
  46402. + m = (struct mtx *)mutex;
  46403. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  46404. + return mutex;
  46405. +}
  46406. +
  46407. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  46408. +#else
  46409. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  46410. +{
  46411. + mtx_destroy((struct mtx *)mutex);
  46412. + DWC_FREE(mutex);
  46413. +}
  46414. +#endif
  46415. +
  46416. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  46417. +{
  46418. + struct mtx *m = (struct mtx *)mutex;
  46419. +
  46420. + mtx_lock(m);
  46421. +}
  46422. +
  46423. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  46424. +{
  46425. + struct mtx *m = (struct mtx *)mutex;
  46426. +
  46427. + return mtx_trylock(m);
  46428. +}
  46429. +
  46430. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  46431. +{
  46432. + struct mtx *m = (struct mtx *)mutex;
  46433. +
  46434. + mtx_unlock(m);
  46435. +}
  46436. +
  46437. +
  46438. +/* Timing */
  46439. +
  46440. +void DWC_UDELAY(uint32_t usecs)
  46441. +{
  46442. + DELAY(usecs);
  46443. +}
  46444. +
  46445. +void DWC_MDELAY(uint32_t msecs)
  46446. +{
  46447. + do {
  46448. + DELAY(1000);
  46449. + } while (--msecs);
  46450. +}
  46451. +
  46452. +void DWC_MSLEEP(uint32_t msecs)
  46453. +{
  46454. + struct timeval tv;
  46455. +
  46456. + tv.tv_sec = msecs / 1000;
  46457. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  46458. + pause("dw3slp", tvtohz(&tv));
  46459. +}
  46460. +
  46461. +uint32_t DWC_TIME(void)
  46462. +{
  46463. + struct timeval tv;
  46464. +
  46465. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  46466. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  46467. +}
  46468. +
  46469. +
  46470. +/* Timers */
  46471. +
  46472. +struct dwc_timer {
  46473. + struct callout t;
  46474. + char *name;
  46475. + dwc_spinlock_t *lock;
  46476. + dwc_timer_callback_t cb;
  46477. + void *data;
  46478. +};
  46479. +
  46480. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  46481. +{
  46482. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  46483. +
  46484. + if (!t) {
  46485. + DWC_ERROR("Cannot allocate memory for timer");
  46486. + return NULL;
  46487. + }
  46488. +
  46489. + callout_init(&t->t, 1);
  46490. +
  46491. + t->name = DWC_STRDUP(name);
  46492. + if (!t->name) {
  46493. + DWC_ERROR("Cannot allocate memory for timer->name");
  46494. + goto no_name;
  46495. + }
  46496. +
  46497. + t->lock = DWC_SPINLOCK_ALLOC();
  46498. + if (!t->lock) {
  46499. + DWC_ERROR("Cannot allocate memory for lock");
  46500. + goto no_lock;
  46501. + }
  46502. +
  46503. + t->cb = cb;
  46504. + t->data = data;
  46505. +
  46506. + return t;
  46507. +
  46508. + no_lock:
  46509. + DWC_FREE(t->name);
  46510. + no_name:
  46511. + DWC_FREE(t);
  46512. +
  46513. + return NULL;
  46514. +}
  46515. +
  46516. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  46517. +{
  46518. + callout_stop(&timer->t);
  46519. + DWC_SPINLOCK_FREE(timer->lock);
  46520. + DWC_FREE(timer->name);
  46521. + DWC_FREE(timer);
  46522. +}
  46523. +
  46524. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  46525. +{
  46526. + struct timeval tv;
  46527. +
  46528. + tv.tv_sec = time / 1000;
  46529. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  46530. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  46531. +}
  46532. +
  46533. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  46534. +{
  46535. + callout_stop(&timer->t);
  46536. +}
  46537. +
  46538. +
  46539. +/* Wait Queues */
  46540. +
  46541. +struct dwc_waitq {
  46542. + struct mtx lock;
  46543. + int abort;
  46544. +};
  46545. +
  46546. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  46547. +{
  46548. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  46549. +
  46550. + if (!wq) {
  46551. + DWC_ERROR("Cannot allocate memory for waitqueue");
  46552. + return NULL;
  46553. + }
  46554. +
  46555. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  46556. + wq->abort = 0;
  46557. +
  46558. + return wq;
  46559. +}
  46560. +
  46561. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  46562. +{
  46563. + mtx_destroy(&wq->lock);
  46564. + DWC_FREE(wq);
  46565. +}
  46566. +
  46567. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  46568. +{
  46569. +// intrmask_t ipl;
  46570. + int result = 0;
  46571. +
  46572. + mtx_lock(&wq->lock);
  46573. +// ipl = splbio();
  46574. +
  46575. + /* Skip the sleep if already aborted or triggered */
  46576. + if (!wq->abort && !cond(data)) {
  46577. +// splx(ipl);
  46578. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  46579. +// ipl = splbio();
  46580. + }
  46581. +
  46582. + if (result == ERESTART) { // signaled - restart
  46583. + result = -DWC_E_RESTART;
  46584. +
  46585. + } else if (result == EINTR) { // signaled - interrupt
  46586. + result = -DWC_E_ABORT;
  46587. +
  46588. + } else if (wq->abort) {
  46589. + result = -DWC_E_ABORT;
  46590. +
  46591. + } else {
  46592. + result = 0;
  46593. + }
  46594. +
  46595. + wq->abort = 0;
  46596. +// splx(ipl);
  46597. + mtx_unlock(&wq->lock);
  46598. + return result;
  46599. +}
  46600. +
  46601. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  46602. + void *data, int32_t msecs)
  46603. +{
  46604. + struct timeval tv, tv1, tv2;
  46605. +// intrmask_t ipl;
  46606. + int result = 0;
  46607. +
  46608. + tv.tv_sec = msecs / 1000;
  46609. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  46610. +
  46611. + mtx_lock(&wq->lock);
  46612. +// ipl = splbio();
  46613. +
  46614. + /* Skip the sleep if already aborted or triggered */
  46615. + if (!wq->abort && !cond(data)) {
  46616. +// splx(ipl);
  46617. + getmicrouptime(&tv1);
  46618. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  46619. + getmicrouptime(&tv2);
  46620. +// ipl = splbio();
  46621. + }
  46622. +
  46623. + if (result == 0) { // awoken
  46624. + if (wq->abort) {
  46625. + result = -DWC_E_ABORT;
  46626. + } else {
  46627. + tv2.tv_usec -= tv1.tv_usec;
  46628. + if (tv2.tv_usec < 0) {
  46629. + tv2.tv_usec += 1000000;
  46630. + tv2.tv_sec--;
  46631. + }
  46632. +
  46633. + tv2.tv_sec -= tv1.tv_sec;
  46634. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  46635. + result = msecs - result;
  46636. + if (result <= 0)
  46637. + result = 1;
  46638. + }
  46639. + } else if (result == ERESTART) { // signaled - restart
  46640. + result = -DWC_E_RESTART;
  46641. +
  46642. + } else if (result == EINTR) { // signaled - interrupt
  46643. + result = -DWC_E_ABORT;
  46644. +
  46645. + } else { // timed out
  46646. + result = -DWC_E_TIMEOUT;
  46647. + }
  46648. +
  46649. + wq->abort = 0;
  46650. +// splx(ipl);
  46651. + mtx_unlock(&wq->lock);
  46652. + return result;
  46653. +}
  46654. +
  46655. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  46656. +{
  46657. + wakeup(wq);
  46658. +}
  46659. +
  46660. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  46661. +{
  46662. +// intrmask_t ipl;
  46663. +
  46664. + mtx_lock(&wq->lock);
  46665. +// ipl = splbio();
  46666. + wq->abort = 1;
  46667. + wakeup(wq);
  46668. +// splx(ipl);
  46669. + mtx_unlock(&wq->lock);
  46670. +}
  46671. +
  46672. +
  46673. +/* Threading */
  46674. +
  46675. +struct dwc_thread {
  46676. + struct proc *proc;
  46677. + int abort;
  46678. +};
  46679. +
  46680. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  46681. +{
  46682. + int retval;
  46683. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  46684. +
  46685. + if (!thread) {
  46686. + return NULL;
  46687. + }
  46688. +
  46689. + thread->abort = 0;
  46690. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  46691. + RFPROC | RFNOWAIT, 0, "%s", name);
  46692. + if (retval) {
  46693. + DWC_FREE(thread);
  46694. + return NULL;
  46695. + }
  46696. +
  46697. + return thread;
  46698. +}
  46699. +
  46700. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  46701. +{
  46702. + int retval;
  46703. +
  46704. + thread->abort = 1;
  46705. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  46706. +
  46707. + if (retval == 0) {
  46708. + /* DWC_THREAD_EXIT() will free the thread struct */
  46709. + return 0;
  46710. + }
  46711. +
  46712. + /* NOTE: We leak the thread struct if thread doesn't die */
  46713. +
  46714. + if (retval == EWOULDBLOCK) {
  46715. + return -DWC_E_TIMEOUT;
  46716. + }
  46717. +
  46718. + return -DWC_E_UNKNOWN;
  46719. +}
  46720. +
  46721. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  46722. +{
  46723. + return thread->abort;
  46724. +}
  46725. +
  46726. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  46727. +{
  46728. + wakeup(&thread->abort);
  46729. + DWC_FREE(thread);
  46730. + kthread_exit(0);
  46731. +}
  46732. +
  46733. +
  46734. +/* tasklets
  46735. + - Runs in interrupt context (cannot sleep)
  46736. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  46737. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  46738. + */
  46739. +struct dwc_tasklet {
  46740. + struct task t;
  46741. + dwc_tasklet_callback_t cb;
  46742. + void *data;
  46743. +};
  46744. +
  46745. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  46746. +{
  46747. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  46748. +
  46749. + task->cb(task->data);
  46750. +}
  46751. +
  46752. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  46753. +{
  46754. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  46755. +
  46756. + if (task) {
  46757. + task->cb = cb;
  46758. + task->data = data;
  46759. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  46760. + } else {
  46761. + DWC_ERROR("Cannot allocate memory for tasklet");
  46762. + }
  46763. +
  46764. + return task;
  46765. +}
  46766. +
  46767. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  46768. +{
  46769. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  46770. + DWC_FREE(task);
  46771. +}
  46772. +
  46773. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  46774. +{
  46775. + /* Uses predefined system queue */
  46776. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  46777. +}
  46778. +
  46779. +
  46780. +/* workqueues
  46781. + - Runs in process context (can sleep)
  46782. + */
  46783. +typedef struct work_container {
  46784. + dwc_work_callback_t cb;
  46785. + void *data;
  46786. + dwc_workq_t *wq;
  46787. + char *name;
  46788. + int hz;
  46789. +
  46790. +#ifdef DEBUG
  46791. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  46792. +#endif
  46793. + struct task task;
  46794. +} work_container_t;
  46795. +
  46796. +#ifdef DEBUG
  46797. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  46798. +#endif
  46799. +
  46800. +struct dwc_workq {
  46801. + struct taskqueue *taskq;
  46802. + dwc_spinlock_t *lock;
  46803. + dwc_waitq_t *waitq;
  46804. + int pending;
  46805. +
  46806. +#ifdef DEBUG
  46807. + struct work_container_queue entries;
  46808. +#endif
  46809. +};
  46810. +
  46811. +static void do_work(void *data, int pending) // what to do with pending ???
  46812. +{
  46813. + work_container_t *container = (work_container_t *)data;
  46814. + dwc_workq_t *wq = container->wq;
  46815. + dwc_irqflags_t flags;
  46816. +
  46817. + if (container->hz) {
  46818. + pause("dw3wrk", container->hz);
  46819. + }
  46820. +
  46821. + container->cb(container->data);
  46822. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  46823. +
  46824. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  46825. +
  46826. +#ifdef DEBUG
  46827. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  46828. +#endif
  46829. + if (container->name)
  46830. + DWC_FREE(container->name);
  46831. + DWC_FREE(container);
  46832. + wq->pending--;
  46833. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  46834. + DWC_WAITQ_TRIGGER(wq->waitq);
  46835. +}
  46836. +
  46837. +static int work_done(void *data)
  46838. +{
  46839. + dwc_workq_t *workq = (dwc_workq_t *)data;
  46840. +
  46841. + return workq->pending == 0;
  46842. +}
  46843. +
  46844. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  46845. +{
  46846. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  46847. +}
  46848. +
  46849. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  46850. +{
  46851. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  46852. +
  46853. + if (!wq) {
  46854. + DWC_ERROR("Cannot allocate memory for workqueue");
  46855. + return NULL;
  46856. + }
  46857. +
  46858. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  46859. + if (!wq->taskq) {
  46860. + DWC_ERROR("Cannot allocate memory for taskqueue");
  46861. + goto no_taskq;
  46862. + }
  46863. +
  46864. + wq->pending = 0;
  46865. +
  46866. + wq->lock = DWC_SPINLOCK_ALLOC();
  46867. + if (!wq->lock) {
  46868. + DWC_ERROR("Cannot allocate memory for spinlock");
  46869. + goto no_lock;
  46870. + }
  46871. +
  46872. + wq->waitq = DWC_WAITQ_ALLOC();
  46873. + if (!wq->waitq) {
  46874. + DWC_ERROR("Cannot allocate memory for waitqueue");
  46875. + goto no_waitq;
  46876. + }
  46877. +
  46878. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  46879. +
  46880. +#ifdef DEBUG
  46881. + DWC_CIRCLEQ_INIT(&wq->entries);
  46882. +#endif
  46883. + return wq;
  46884. +
  46885. + no_waitq:
  46886. + DWC_SPINLOCK_FREE(wq->lock);
  46887. + no_lock:
  46888. + taskqueue_free(wq->taskq);
  46889. + no_taskq:
  46890. + DWC_FREE(wq);
  46891. +
  46892. + return NULL;
  46893. +}
  46894. +
  46895. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  46896. +{
  46897. +#ifdef DEBUG
  46898. + dwc_irqflags_t flags;
  46899. +
  46900. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  46901. +
  46902. + if (wq->pending != 0) {
  46903. + struct work_container *container;
  46904. +
  46905. + DWC_ERROR("Destroying work queue with pending work");
  46906. +
  46907. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  46908. + DWC_ERROR("Work %s still pending", container->name);
  46909. + }
  46910. + }
  46911. +
  46912. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  46913. +#endif
  46914. + DWC_WAITQ_FREE(wq->waitq);
  46915. + DWC_SPINLOCK_FREE(wq->lock);
  46916. + taskqueue_free(wq->taskq);
  46917. + DWC_FREE(wq);
  46918. +}
  46919. +
  46920. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  46921. + char *format, ...)
  46922. +{
  46923. + dwc_irqflags_t flags;
  46924. + work_container_t *container;
  46925. + static char name[128];
  46926. + va_list args;
  46927. +
  46928. + va_start(args, format);
  46929. + DWC_VSNPRINTF(name, 128, format, args);
  46930. + va_end(args);
  46931. +
  46932. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  46933. + wq->pending++;
  46934. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  46935. + DWC_WAITQ_TRIGGER(wq->waitq);
  46936. +
  46937. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  46938. + if (!container) {
  46939. + DWC_ERROR("Cannot allocate memory for container");
  46940. + return;
  46941. + }
  46942. +
  46943. + container->name = DWC_STRDUP(name);
  46944. + if (!container->name) {
  46945. + DWC_ERROR("Cannot allocate memory for container->name");
  46946. + DWC_FREE(container);
  46947. + return;
  46948. + }
  46949. +
  46950. + container->cb = cb;
  46951. + container->data = data;
  46952. + container->wq = wq;
  46953. + container->hz = 0;
  46954. +
  46955. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  46956. +
  46957. + TASK_INIT(&container->task, 0, do_work, container);
  46958. +
  46959. +#ifdef DEBUG
  46960. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  46961. +#endif
  46962. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  46963. +}
  46964. +
  46965. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  46966. + void *data, uint32_t time, char *format, ...)
  46967. +{
  46968. + dwc_irqflags_t flags;
  46969. + work_container_t *container;
  46970. + static char name[128];
  46971. + struct timeval tv;
  46972. + va_list args;
  46973. +
  46974. + va_start(args, format);
  46975. + DWC_VSNPRINTF(name, 128, format, args);
  46976. + va_end(args);
  46977. +
  46978. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  46979. + wq->pending++;
  46980. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  46981. + DWC_WAITQ_TRIGGER(wq->waitq);
  46982. +
  46983. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  46984. + if (!container) {
  46985. + DWC_ERROR("Cannot allocate memory for container");
  46986. + return;
  46987. + }
  46988. +
  46989. + container->name = DWC_STRDUP(name);
  46990. + if (!container->name) {
  46991. + DWC_ERROR("Cannot allocate memory for container->name");
  46992. + DWC_FREE(container);
  46993. + return;
  46994. + }
  46995. +
  46996. + container->cb = cb;
  46997. + container->data = data;
  46998. + container->wq = wq;
  46999. +
  47000. + tv.tv_sec = time / 1000;
  47001. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  47002. + container->hz = tvtohz(&tv);
  47003. +
  47004. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  47005. +
  47006. + TASK_INIT(&container->task, 0, do_work, container);
  47007. +
  47008. +#ifdef DEBUG
  47009. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  47010. +#endif
  47011. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  47012. +}
  47013. +
  47014. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  47015. +{
  47016. + return wq->pending;
  47017. +}
  47018. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_common_linux.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  47019. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_common_linux.c 1969-12-31 18:00:00.000000000 -0600
  47020. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c 2014-12-11 14:05:39.524418001 -0600
  47021. @@ -0,0 +1,1440 @@
  47022. +#include <linux/kernel.h>
  47023. +#include <linux/init.h>
  47024. +#include <linux/module.h>
  47025. +#include <linux/kthread.h>
  47026. +
  47027. +#ifdef DWC_CCLIB
  47028. +# include "dwc_cc.h"
  47029. +#endif
  47030. +
  47031. +#ifdef DWC_CRYPTOLIB
  47032. +# include "dwc_modpow.h"
  47033. +# include "dwc_dh.h"
  47034. +# include "dwc_crypto.h"
  47035. +#endif
  47036. +
  47037. +#ifdef DWC_NOTIFYLIB
  47038. +# include "dwc_notifier.h"
  47039. +#endif
  47040. +
  47041. +/* OS-Level Implementations */
  47042. +
  47043. +/* This is the Linux kernel implementation of the DWC platform library. */
  47044. +#include <linux/moduleparam.h>
  47045. +#include <linux/ctype.h>
  47046. +#include <linux/crypto.h>
  47047. +#include <linux/delay.h>
  47048. +#include <linux/device.h>
  47049. +#include <linux/dma-mapping.h>
  47050. +#include <linux/cdev.h>
  47051. +#include <linux/errno.h>
  47052. +#include <linux/interrupt.h>
  47053. +#include <linux/jiffies.h>
  47054. +#include <linux/list.h>
  47055. +#include <linux/pci.h>
  47056. +#include <linux/random.h>
  47057. +#include <linux/scatterlist.h>
  47058. +#include <linux/slab.h>
  47059. +#include <linux/stat.h>
  47060. +#include <linux/string.h>
  47061. +#include <linux/timer.h>
  47062. +#include <linux/usb.h>
  47063. +
  47064. +#include <linux/version.h>
  47065. +
  47066. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  47067. +# include <linux/usb/gadget.h>
  47068. +#else
  47069. +# include <linux/usb_gadget.h>
  47070. +#endif
  47071. +
  47072. +#include <asm/io.h>
  47073. +#include <asm/page.h>
  47074. +#include <asm/uaccess.h>
  47075. +#include <asm/unaligned.h>
  47076. +
  47077. +#include "dwc_os.h"
  47078. +#include "dwc_list.h"
  47079. +
  47080. +
  47081. +/* MISC */
  47082. +
  47083. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  47084. +{
  47085. + return memset(dest, byte, size);
  47086. +}
  47087. +
  47088. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  47089. +{
  47090. + return memcpy(dest, src, size);
  47091. +}
  47092. +
  47093. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  47094. +{
  47095. + return memmove(dest, src, size);
  47096. +}
  47097. +
  47098. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  47099. +{
  47100. + return memcmp(m1, m2, size);
  47101. +}
  47102. +
  47103. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  47104. +{
  47105. + return strncmp(s1, s2, size);
  47106. +}
  47107. +
  47108. +int DWC_STRCMP(void *s1, void *s2)
  47109. +{
  47110. + return strcmp(s1, s2);
  47111. +}
  47112. +
  47113. +int DWC_STRLEN(char const *str)
  47114. +{
  47115. + return strlen(str);
  47116. +}
  47117. +
  47118. +char *DWC_STRCPY(char *to, char const *from)
  47119. +{
  47120. + return strcpy(to, from);
  47121. +}
  47122. +
  47123. +char *DWC_STRDUP(char const *str)
  47124. +{
  47125. + int len = DWC_STRLEN(str) + 1;
  47126. + char *new = DWC_ALLOC_ATOMIC(len);
  47127. +
  47128. + if (!new) {
  47129. + return NULL;
  47130. + }
  47131. +
  47132. + DWC_MEMCPY(new, str, len);
  47133. + return new;
  47134. +}
  47135. +
  47136. +int DWC_ATOI(const char *str, int32_t *value)
  47137. +{
  47138. + char *end = NULL;
  47139. +
  47140. + *value = simple_strtol(str, &end, 0);
  47141. + if (*end == '\0') {
  47142. + return 0;
  47143. + }
  47144. +
  47145. + return -1;
  47146. +}
  47147. +
  47148. +int DWC_ATOUI(const char *str, uint32_t *value)
  47149. +{
  47150. + char *end = NULL;
  47151. +
  47152. + *value = simple_strtoul(str, &end, 0);
  47153. + if (*end == '\0') {
  47154. + return 0;
  47155. + }
  47156. +
  47157. + return -1;
  47158. +}
  47159. +
  47160. +
  47161. +#ifdef DWC_UTFLIB
  47162. +/* From usbstring.c */
  47163. +
  47164. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  47165. +{
  47166. + int count = 0;
  47167. + u8 c;
  47168. + u16 uchar;
  47169. +
  47170. + /* this insists on correct encodings, though not minimal ones.
  47171. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  47172. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  47173. + */
  47174. + while (len != 0 && (c = (u8) *s++) != 0) {
  47175. + if (unlikely(c & 0x80)) {
  47176. + // 2-byte sequence:
  47177. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  47178. + if ((c & 0xe0) == 0xc0) {
  47179. + uchar = (c & 0x1f) << 6;
  47180. +
  47181. + c = (u8) *s++;
  47182. + if ((c & 0xc0) != 0xc0)
  47183. + goto fail;
  47184. + c &= 0x3f;
  47185. + uchar |= c;
  47186. +
  47187. + // 3-byte sequence (most CJKV characters):
  47188. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  47189. + } else if ((c & 0xf0) == 0xe0) {
  47190. + uchar = (c & 0x0f) << 12;
  47191. +
  47192. + c = (u8) *s++;
  47193. + if ((c & 0xc0) != 0xc0)
  47194. + goto fail;
  47195. + c &= 0x3f;
  47196. + uchar |= c << 6;
  47197. +
  47198. + c = (u8) *s++;
  47199. + if ((c & 0xc0) != 0xc0)
  47200. + goto fail;
  47201. + c &= 0x3f;
  47202. + uchar |= c;
  47203. +
  47204. + /* no bogus surrogates */
  47205. + if (0xd800 <= uchar && uchar <= 0xdfff)
  47206. + goto fail;
  47207. +
  47208. + // 4-byte sequence (surrogate pairs, currently rare):
  47209. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  47210. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  47211. + // (uuuuu = wwww + 1)
  47212. + // FIXME accept the surrogate code points (only)
  47213. + } else
  47214. + goto fail;
  47215. + } else
  47216. + uchar = c;
  47217. + put_unaligned (cpu_to_le16 (uchar), cp++);
  47218. + count++;
  47219. + len--;
  47220. + }
  47221. + return count;
  47222. +fail:
  47223. + return -1;
  47224. +}
  47225. +#endif /* DWC_UTFLIB */
  47226. +
  47227. +
  47228. +/* dwc_debug.h */
  47229. +
  47230. +dwc_bool_t DWC_IN_IRQ(void)
  47231. +{
  47232. + return in_irq();
  47233. +}
  47234. +
  47235. +dwc_bool_t DWC_IN_BH(void)
  47236. +{
  47237. + return in_softirq();
  47238. +}
  47239. +
  47240. +void DWC_VPRINTF(char *format, va_list args)
  47241. +{
  47242. + vprintk(format, args);
  47243. +}
  47244. +
  47245. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  47246. +{
  47247. + return vsnprintf(str, size, format, args);
  47248. +}
  47249. +
  47250. +void DWC_PRINTF(char *format, ...)
  47251. +{
  47252. + va_list args;
  47253. +
  47254. + va_start(args, format);
  47255. + DWC_VPRINTF(format, args);
  47256. + va_end(args);
  47257. +}
  47258. +
  47259. +int DWC_SPRINTF(char *buffer, char *format, ...)
  47260. +{
  47261. + int retval;
  47262. + va_list args;
  47263. +
  47264. + va_start(args, format);
  47265. + retval = vsprintf(buffer, format, args);
  47266. + va_end(args);
  47267. + return retval;
  47268. +}
  47269. +
  47270. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  47271. +{
  47272. + int retval;
  47273. + va_list args;
  47274. +
  47275. + va_start(args, format);
  47276. + retval = vsnprintf(buffer, size, format, args);
  47277. + va_end(args);
  47278. + return retval;
  47279. +}
  47280. +
  47281. +void __DWC_WARN(char *format, ...)
  47282. +{
  47283. + va_list args;
  47284. +
  47285. + va_start(args, format);
  47286. + DWC_PRINTF(KERN_WARNING);
  47287. + DWC_VPRINTF(format, args);
  47288. + va_end(args);
  47289. +}
  47290. +
  47291. +void __DWC_ERROR(char *format, ...)
  47292. +{
  47293. + va_list args;
  47294. +
  47295. + va_start(args, format);
  47296. + DWC_PRINTF(KERN_ERR);
  47297. + DWC_VPRINTF(format, args);
  47298. + va_end(args);
  47299. +}
  47300. +
  47301. +void DWC_EXCEPTION(char *format, ...)
  47302. +{
  47303. + va_list args;
  47304. +
  47305. + va_start(args, format);
  47306. + DWC_PRINTF(KERN_ERR);
  47307. + DWC_VPRINTF(format, args);
  47308. + va_end(args);
  47309. + BUG_ON(1);
  47310. +}
  47311. +
  47312. +#ifdef DEBUG
  47313. +void __DWC_DEBUG(char *format, ...)
  47314. +{
  47315. + va_list args;
  47316. +
  47317. + va_start(args, format);
  47318. + DWC_PRINTF(KERN_DEBUG);
  47319. + DWC_VPRINTF(format, args);
  47320. + va_end(args);
  47321. +}
  47322. +#endif
  47323. +
  47324. +
  47325. +/* dwc_mem.h */
  47326. +
  47327. +#if 0
  47328. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  47329. + uint32_t align,
  47330. + uint32_t alloc)
  47331. +{
  47332. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  47333. + size, align, alloc);
  47334. + return (dwc_pool_t *)pool;
  47335. +}
  47336. +
  47337. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  47338. +{
  47339. + dma_pool_destroy((struct dma_pool *)pool);
  47340. +}
  47341. +
  47342. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  47343. +{
  47344. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  47345. +}
  47346. +
  47347. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  47348. +{
  47349. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  47350. + memset(..);
  47351. +}
  47352. +
  47353. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  47354. +{
  47355. + dma_pool_free(pool, vaddr, daddr);
  47356. +}
  47357. +#endif
  47358. +
  47359. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  47360. +{
  47361. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  47362. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  47363. +#else
  47364. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  47365. +#endif
  47366. + if (!buf) {
  47367. + return NULL;
  47368. + }
  47369. +
  47370. + memset(buf, 0, (size_t)size);
  47371. + return buf;
  47372. +}
  47373. +
  47374. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  47375. +{
  47376. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  47377. + if (!buf) {
  47378. + return NULL;
  47379. + }
  47380. + memset(buf, 0, (size_t)size);
  47381. + return buf;
  47382. +}
  47383. +
  47384. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  47385. +{
  47386. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  47387. +}
  47388. +
  47389. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  47390. +{
  47391. + return kzalloc(size, GFP_KERNEL);
  47392. +}
  47393. +
  47394. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  47395. +{
  47396. + return kzalloc(size, GFP_ATOMIC);
  47397. +}
  47398. +
  47399. +void __DWC_FREE(void *mem_ctx, void *addr)
  47400. +{
  47401. + kfree(addr);
  47402. +}
  47403. +
  47404. +
  47405. +#ifdef DWC_CRYPTOLIB
  47406. +/* dwc_crypto.h */
  47407. +
  47408. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  47409. +{
  47410. + get_random_bytes(buffer, length);
  47411. +}
  47412. +
  47413. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  47414. +{
  47415. + struct crypto_blkcipher *tfm;
  47416. + struct blkcipher_desc desc;
  47417. + struct scatterlist sgd;
  47418. + struct scatterlist sgs;
  47419. +
  47420. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  47421. + if (tfm == NULL) {
  47422. + printk("failed to load transform for aes CBC\n");
  47423. + return -1;
  47424. + }
  47425. +
  47426. + crypto_blkcipher_setkey(tfm, key, keylen);
  47427. + crypto_blkcipher_set_iv(tfm, iv, 16);
  47428. +
  47429. + sg_init_one(&sgd, out, messagelen);
  47430. + sg_init_one(&sgs, message, messagelen);
  47431. +
  47432. + desc.tfm = tfm;
  47433. + desc.flags = 0;
  47434. +
  47435. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  47436. + crypto_free_blkcipher(tfm);
  47437. + DWC_ERROR("AES CBC encryption failed");
  47438. + return -1;
  47439. + }
  47440. +
  47441. + crypto_free_blkcipher(tfm);
  47442. + return 0;
  47443. +}
  47444. +
  47445. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  47446. +{
  47447. + struct crypto_hash *tfm;
  47448. + struct hash_desc desc;
  47449. + struct scatterlist sg;
  47450. +
  47451. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  47452. + if (IS_ERR(tfm)) {
  47453. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  47454. + return 0;
  47455. + }
  47456. + desc.tfm = tfm;
  47457. + desc.flags = 0;
  47458. +
  47459. + sg_init_one(&sg, message, len);
  47460. + crypto_hash_digest(&desc, &sg, len, out);
  47461. + crypto_free_hash(tfm);
  47462. +
  47463. + return 1;
  47464. +}
  47465. +
  47466. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  47467. + uint8_t *key, uint32_t keylen, uint8_t *out)
  47468. +{
  47469. + struct crypto_hash *tfm;
  47470. + struct hash_desc desc;
  47471. + struct scatterlist sg;
  47472. +
  47473. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  47474. + if (IS_ERR(tfm)) {
  47475. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  47476. + return 0;
  47477. + }
  47478. + desc.tfm = tfm;
  47479. + desc.flags = 0;
  47480. +
  47481. + sg_init_one(&sg, message, messagelen);
  47482. + crypto_hash_setkey(tfm, key, keylen);
  47483. + crypto_hash_digest(&desc, &sg, messagelen, out);
  47484. + crypto_free_hash(tfm);
  47485. +
  47486. + return 1;
  47487. +}
  47488. +#endif /* DWC_CRYPTOLIB */
  47489. +
  47490. +
  47491. +/* Byte Ordering Conversions */
  47492. +
  47493. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  47494. +{
  47495. +#ifdef __LITTLE_ENDIAN
  47496. + return *p;
  47497. +#else
  47498. + uint8_t *u_p = (uint8_t *)p;
  47499. +
  47500. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47501. +#endif
  47502. +}
  47503. +
  47504. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  47505. +{
  47506. +#ifdef __BIG_ENDIAN
  47507. + return *p;
  47508. +#else
  47509. + uint8_t *u_p = (uint8_t *)p;
  47510. +
  47511. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47512. +#endif
  47513. +}
  47514. +
  47515. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  47516. +{
  47517. +#ifdef __LITTLE_ENDIAN
  47518. + return *p;
  47519. +#else
  47520. + uint8_t *u_p = (uint8_t *)p;
  47521. +
  47522. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47523. +#endif
  47524. +}
  47525. +
  47526. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  47527. +{
  47528. +#ifdef __BIG_ENDIAN
  47529. + return *p;
  47530. +#else
  47531. + uint8_t *u_p = (uint8_t *)p;
  47532. +
  47533. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47534. +#endif
  47535. +}
  47536. +
  47537. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  47538. +{
  47539. +#ifdef __LITTLE_ENDIAN
  47540. + return *p;
  47541. +#else
  47542. + uint8_t *u_p = (uint8_t *)p;
  47543. + return (u_p[1] | (u_p[0] << 8));
  47544. +#endif
  47545. +}
  47546. +
  47547. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  47548. +{
  47549. +#ifdef __BIG_ENDIAN
  47550. + return *p;
  47551. +#else
  47552. + uint8_t *u_p = (uint8_t *)p;
  47553. + return (u_p[1] | (u_p[0] << 8));
  47554. +#endif
  47555. +}
  47556. +
  47557. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  47558. +{
  47559. +#ifdef __LITTLE_ENDIAN
  47560. + return *p;
  47561. +#else
  47562. + uint8_t *u_p = (uint8_t *)p;
  47563. + return (u_p[1] | (u_p[0] << 8));
  47564. +#endif
  47565. +}
  47566. +
  47567. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  47568. +{
  47569. +#ifdef __BIG_ENDIAN
  47570. + return *p;
  47571. +#else
  47572. + uint8_t *u_p = (uint8_t *)p;
  47573. + return (u_p[1] | (u_p[0] << 8));
  47574. +#endif
  47575. +}
  47576. +
  47577. +
  47578. +/* Registers */
  47579. +
  47580. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  47581. +{
  47582. + return readl(reg);
  47583. +}
  47584. +
  47585. +#if 0
  47586. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  47587. +{
  47588. +}
  47589. +#endif
  47590. +
  47591. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  47592. +{
  47593. + writel(value, reg);
  47594. +}
  47595. +
  47596. +#if 0
  47597. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  47598. +{
  47599. +}
  47600. +#endif
  47601. +
  47602. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  47603. +{
  47604. + unsigned long flags;
  47605. +
  47606. + local_irq_save(flags);
  47607. + local_fiq_disable();
  47608. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  47609. + local_fiq_enable();
  47610. + local_irq_restore(flags);
  47611. +}
  47612. +
  47613. +#if 0
  47614. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  47615. +{
  47616. +}
  47617. +#endif
  47618. +
  47619. +
  47620. +/* Locking */
  47621. +
  47622. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  47623. +{
  47624. + spinlock_t *sl = (spinlock_t *)1;
  47625. +
  47626. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  47627. + sl = DWC_ALLOC(sizeof(*sl));
  47628. + if (!sl) {
  47629. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  47630. + return NULL;
  47631. + }
  47632. +
  47633. + spin_lock_init(sl);
  47634. +#endif
  47635. + return (dwc_spinlock_t *)sl;
  47636. +}
  47637. +
  47638. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  47639. +{
  47640. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  47641. + DWC_FREE(lock);
  47642. +#endif
  47643. +}
  47644. +
  47645. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  47646. +{
  47647. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  47648. + spin_lock((spinlock_t *)lock);
  47649. +#endif
  47650. +}
  47651. +
  47652. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  47653. +{
  47654. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  47655. + spin_unlock((spinlock_t *)lock);
  47656. +#endif
  47657. +}
  47658. +
  47659. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  47660. +{
  47661. + dwc_irqflags_t f;
  47662. +
  47663. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  47664. + spin_lock_irqsave((spinlock_t *)lock, f);
  47665. +#else
  47666. + local_irq_save(f);
  47667. +#endif
  47668. + *flags = f;
  47669. +}
  47670. +
  47671. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  47672. +{
  47673. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  47674. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  47675. +#else
  47676. + local_irq_restore(flags);
  47677. +#endif
  47678. +}
  47679. +
  47680. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  47681. +{
  47682. + struct mutex *m;
  47683. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  47684. +
  47685. + if (!mutex) {
  47686. + DWC_ERROR("Cannot allocate memory for mutex\n");
  47687. + return NULL;
  47688. + }
  47689. +
  47690. + m = (struct mutex *)mutex;
  47691. + mutex_init(m);
  47692. + return mutex;
  47693. +}
  47694. +
  47695. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  47696. +#else
  47697. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  47698. +{
  47699. + mutex_destroy((struct mutex *)mutex);
  47700. + DWC_FREE(mutex);
  47701. +}
  47702. +#endif
  47703. +
  47704. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  47705. +{
  47706. + struct mutex *m = (struct mutex *)mutex;
  47707. + mutex_lock(m);
  47708. +}
  47709. +
  47710. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  47711. +{
  47712. + struct mutex *m = (struct mutex *)mutex;
  47713. + return mutex_trylock(m);
  47714. +}
  47715. +
  47716. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  47717. +{
  47718. + struct mutex *m = (struct mutex *)mutex;
  47719. + mutex_unlock(m);
  47720. +}
  47721. +
  47722. +
  47723. +/* Timing */
  47724. +
  47725. +void DWC_UDELAY(uint32_t usecs)
  47726. +{
  47727. + udelay(usecs);
  47728. +}
  47729. +
  47730. +void DWC_MDELAY(uint32_t msecs)
  47731. +{
  47732. + mdelay(msecs);
  47733. +}
  47734. +
  47735. +void DWC_MSLEEP(uint32_t msecs)
  47736. +{
  47737. + msleep(msecs);
  47738. +}
  47739. +
  47740. +uint32_t DWC_TIME(void)
  47741. +{
  47742. + return jiffies_to_msecs(jiffies);
  47743. +}
  47744. +
  47745. +
  47746. +/* Timers */
  47747. +
  47748. +struct dwc_timer {
  47749. + struct timer_list *t;
  47750. + char *name;
  47751. + dwc_timer_callback_t cb;
  47752. + void *data;
  47753. + uint8_t scheduled;
  47754. + dwc_spinlock_t *lock;
  47755. +};
  47756. +
  47757. +static void timer_callback(unsigned long data)
  47758. +{
  47759. + dwc_timer_t *timer = (dwc_timer_t *)data;
  47760. + dwc_irqflags_t flags;
  47761. +
  47762. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  47763. + timer->scheduled = 0;
  47764. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  47765. + DWC_DEBUGC("Timer %s callback", timer->name);
  47766. + timer->cb(timer->data);
  47767. +}
  47768. +
  47769. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  47770. +{
  47771. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  47772. +
  47773. + if (!t) {
  47774. + DWC_ERROR("Cannot allocate memory for timer");
  47775. + return NULL;
  47776. + }
  47777. +
  47778. + t->t = DWC_ALLOC(sizeof(*t->t));
  47779. + if (!t->t) {
  47780. + DWC_ERROR("Cannot allocate memory for timer->t");
  47781. + goto no_timer;
  47782. + }
  47783. +
  47784. + t->name = DWC_STRDUP(name);
  47785. + if (!t->name) {
  47786. + DWC_ERROR("Cannot allocate memory for timer->name");
  47787. + goto no_name;
  47788. + }
  47789. +
  47790. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  47791. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
  47792. +#else
  47793. + t->lock = DWC_SPINLOCK_ALLOC();
  47794. +#endif
  47795. + if (!t->lock) {
  47796. + DWC_ERROR("Cannot allocate memory for lock");
  47797. + goto no_lock;
  47798. + }
  47799. +
  47800. + t->scheduled = 0;
  47801. + t->t->base = &boot_tvec_bases;
  47802. + t->t->expires = jiffies;
  47803. + setup_timer(t->t, timer_callback, (unsigned long)t);
  47804. +
  47805. + t->cb = cb;
  47806. + t->data = data;
  47807. +
  47808. + return t;
  47809. +
  47810. + no_lock:
  47811. + DWC_FREE(t->name);
  47812. + no_name:
  47813. + DWC_FREE(t->t);
  47814. + no_timer:
  47815. + DWC_FREE(t);
  47816. + return NULL;
  47817. +}
  47818. +
  47819. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  47820. +{
  47821. + dwc_irqflags_t flags;
  47822. +
  47823. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  47824. +
  47825. + if (timer->scheduled) {
  47826. + del_timer(timer->t);
  47827. + timer->scheduled = 0;
  47828. + }
  47829. +
  47830. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  47831. + DWC_SPINLOCK_FREE(timer->lock);
  47832. + DWC_FREE(timer->t);
  47833. + DWC_FREE(timer->name);
  47834. + DWC_FREE(timer);
  47835. +}
  47836. +
  47837. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  47838. +{
  47839. + dwc_irqflags_t flags;
  47840. +
  47841. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  47842. +
  47843. + if (!timer->scheduled) {
  47844. + timer->scheduled = 1;
  47845. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  47846. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  47847. + add_timer(timer->t);
  47848. + } else {
  47849. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  47850. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  47851. + }
  47852. +
  47853. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  47854. +}
  47855. +
  47856. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  47857. +{
  47858. + del_timer(timer->t);
  47859. +}
  47860. +
  47861. +
  47862. +/* Wait Queues */
  47863. +
  47864. +struct dwc_waitq {
  47865. + wait_queue_head_t queue;
  47866. + int abort;
  47867. +};
  47868. +
  47869. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  47870. +{
  47871. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  47872. +
  47873. + if (!wq) {
  47874. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  47875. + return NULL;
  47876. + }
  47877. +
  47878. + init_waitqueue_head(&wq->queue);
  47879. + wq->abort = 0;
  47880. + return wq;
  47881. +}
  47882. +
  47883. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  47884. +{
  47885. + DWC_FREE(wq);
  47886. +}
  47887. +
  47888. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  47889. +{
  47890. + int result = wait_event_interruptible(wq->queue,
  47891. + cond(data) || wq->abort);
  47892. + if (result == -ERESTARTSYS) {
  47893. + wq->abort = 0;
  47894. + return -DWC_E_RESTART;
  47895. + }
  47896. +
  47897. + if (wq->abort == 1) {
  47898. + wq->abort = 0;
  47899. + return -DWC_E_ABORT;
  47900. + }
  47901. +
  47902. + wq->abort = 0;
  47903. +
  47904. + if (result == 0) {
  47905. + return 0;
  47906. + }
  47907. +
  47908. + return -DWC_E_UNKNOWN;
  47909. +}
  47910. +
  47911. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  47912. + void *data, int32_t msecs)
  47913. +{
  47914. + int32_t tmsecs;
  47915. + int result = wait_event_interruptible_timeout(wq->queue,
  47916. + cond(data) || wq->abort,
  47917. + msecs_to_jiffies(msecs));
  47918. + if (result == -ERESTARTSYS) {
  47919. + wq->abort = 0;
  47920. + return -DWC_E_RESTART;
  47921. + }
  47922. +
  47923. + if (wq->abort == 1) {
  47924. + wq->abort = 0;
  47925. + return -DWC_E_ABORT;
  47926. + }
  47927. +
  47928. + wq->abort = 0;
  47929. +
  47930. + if (result > 0) {
  47931. + tmsecs = jiffies_to_msecs(result);
  47932. + if (!tmsecs) {
  47933. + return 1;
  47934. + }
  47935. +
  47936. + return tmsecs;
  47937. + }
  47938. +
  47939. + if (result == 0) {
  47940. + return -DWC_E_TIMEOUT;
  47941. + }
  47942. +
  47943. + return -DWC_E_UNKNOWN;
  47944. +}
  47945. +
  47946. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  47947. +{
  47948. + wq->abort = 0;
  47949. + wake_up_interruptible(&wq->queue);
  47950. +}
  47951. +
  47952. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  47953. +{
  47954. + wq->abort = 1;
  47955. + wake_up_interruptible(&wq->queue);
  47956. +}
  47957. +
  47958. +
  47959. +/* Threading */
  47960. +
  47961. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  47962. +{
  47963. + struct task_struct *thread = kthread_run(func, data, name);
  47964. +
  47965. + if (thread == ERR_PTR(-ENOMEM)) {
  47966. + return NULL;
  47967. + }
  47968. +
  47969. + return (dwc_thread_t *)thread;
  47970. +}
  47971. +
  47972. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  47973. +{
  47974. + return kthread_stop((struct task_struct *)thread);
  47975. +}
  47976. +
  47977. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  47978. +{
  47979. + return kthread_should_stop();
  47980. +}
  47981. +
  47982. +
  47983. +/* tasklets
  47984. + - run in interrupt context (cannot sleep)
  47985. + - each tasklet runs on a single CPU
  47986. + - different tasklets can be running simultaneously on different CPUs
  47987. + */
  47988. +struct dwc_tasklet {
  47989. + struct tasklet_struct t;
  47990. + dwc_tasklet_callback_t cb;
  47991. + void *data;
  47992. +};
  47993. +
  47994. +static void tasklet_callback(unsigned long data)
  47995. +{
  47996. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  47997. + t->cb(t->data);
  47998. +}
  47999. +
  48000. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  48001. +{
  48002. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  48003. +
  48004. + if (t) {
  48005. + t->cb = cb;
  48006. + t->data = data;
  48007. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  48008. + } else {
  48009. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  48010. + }
  48011. +
  48012. + return t;
  48013. +}
  48014. +
  48015. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  48016. +{
  48017. + DWC_FREE(task);
  48018. +}
  48019. +
  48020. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  48021. +{
  48022. + tasklet_schedule(&task->t);
  48023. +}
  48024. +
  48025. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  48026. +{
  48027. + tasklet_hi_schedule(&task->t);
  48028. +}
  48029. +
  48030. +
  48031. +/* workqueues
  48032. + - run in process context (can sleep)
  48033. + */
  48034. +typedef struct work_container {
  48035. + dwc_work_callback_t cb;
  48036. + void *data;
  48037. + dwc_workq_t *wq;
  48038. + char *name;
  48039. +
  48040. +#ifdef DEBUG
  48041. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  48042. +#endif
  48043. + struct delayed_work work;
  48044. +} work_container_t;
  48045. +
  48046. +#ifdef DEBUG
  48047. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  48048. +#endif
  48049. +
  48050. +struct dwc_workq {
  48051. + struct workqueue_struct *wq;
  48052. + dwc_spinlock_t *lock;
  48053. + dwc_waitq_t *waitq;
  48054. + int pending;
  48055. +
  48056. +#ifdef DEBUG
  48057. + struct work_container_queue entries;
  48058. +#endif
  48059. +};
  48060. +
  48061. +static void do_work(struct work_struct *work)
  48062. +{
  48063. + dwc_irqflags_t flags;
  48064. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  48065. + work_container_t *container = container_of(dw, struct work_container, work);
  48066. + dwc_workq_t *wq = container->wq;
  48067. +
  48068. + container->cb(container->data);
  48069. +
  48070. +#ifdef DEBUG
  48071. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  48072. +#endif
  48073. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  48074. + if (container->name) {
  48075. + DWC_FREE(container->name);
  48076. + }
  48077. + DWC_FREE(container);
  48078. +
  48079. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  48080. + wq->pending--;
  48081. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  48082. + DWC_WAITQ_TRIGGER(wq->waitq);
  48083. +}
  48084. +
  48085. +static int work_done(void *data)
  48086. +{
  48087. + dwc_workq_t *workq = (dwc_workq_t *)data;
  48088. + return workq->pending == 0;
  48089. +}
  48090. +
  48091. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  48092. +{
  48093. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  48094. +}
  48095. +
  48096. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  48097. +{
  48098. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  48099. +
  48100. + if (!wq) {
  48101. + return NULL;
  48102. + }
  48103. +
  48104. + wq->wq = create_singlethread_workqueue(name);
  48105. + if (!wq->wq) {
  48106. + goto no_wq;
  48107. + }
  48108. +
  48109. + wq->pending = 0;
  48110. +
  48111. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  48112. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
  48113. +#else
  48114. + wq->lock = DWC_SPINLOCK_ALLOC();
  48115. +#endif
  48116. + if (!wq->lock) {
  48117. + goto no_lock;
  48118. + }
  48119. +
  48120. + wq->waitq = DWC_WAITQ_ALLOC();
  48121. + if (!wq->waitq) {
  48122. + goto no_waitq;
  48123. + }
  48124. +
  48125. +#ifdef DEBUG
  48126. + DWC_CIRCLEQ_INIT(&wq->entries);
  48127. +#endif
  48128. + return wq;
  48129. +
  48130. + no_waitq:
  48131. + DWC_SPINLOCK_FREE(wq->lock);
  48132. + no_lock:
  48133. + destroy_workqueue(wq->wq);
  48134. + no_wq:
  48135. + DWC_FREE(wq);
  48136. +
  48137. + return NULL;
  48138. +}
  48139. +
  48140. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  48141. +{
  48142. +#ifdef DEBUG
  48143. + if (wq->pending != 0) {
  48144. + struct work_container *wc;
  48145. + DWC_ERROR("Destroying work queue with pending work");
  48146. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  48147. + DWC_ERROR("Work %s still pending", wc->name);
  48148. + }
  48149. + }
  48150. +#endif
  48151. + destroy_workqueue(wq->wq);
  48152. + DWC_SPINLOCK_FREE(wq->lock);
  48153. + DWC_WAITQ_FREE(wq->waitq);
  48154. + DWC_FREE(wq);
  48155. +}
  48156. +
  48157. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  48158. + char *format, ...)
  48159. +{
  48160. + dwc_irqflags_t flags;
  48161. + work_container_t *container;
  48162. + static char name[128];
  48163. + va_list args;
  48164. +
  48165. + va_start(args, format);
  48166. + DWC_VSNPRINTF(name, 128, format, args);
  48167. + va_end(args);
  48168. +
  48169. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  48170. + wq->pending++;
  48171. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  48172. + DWC_WAITQ_TRIGGER(wq->waitq);
  48173. +
  48174. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  48175. + if (!container) {
  48176. + DWC_ERROR("Cannot allocate memory for container\n");
  48177. + return;
  48178. + }
  48179. +
  48180. + container->name = DWC_STRDUP(name);
  48181. + if (!container->name) {
  48182. + DWC_ERROR("Cannot allocate memory for container->name\n");
  48183. + DWC_FREE(container);
  48184. + return;
  48185. + }
  48186. +
  48187. + container->cb = cb;
  48188. + container->data = data;
  48189. + container->wq = wq;
  48190. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  48191. + INIT_WORK(&container->work.work, do_work);
  48192. +
  48193. +#ifdef DEBUG
  48194. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  48195. +#endif
  48196. + queue_work(wq->wq, &container->work.work);
  48197. +}
  48198. +
  48199. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  48200. + void *data, uint32_t time, char *format, ...)
  48201. +{
  48202. + dwc_irqflags_t flags;
  48203. + work_container_t *container;
  48204. + static char name[128];
  48205. + va_list args;
  48206. +
  48207. + va_start(args, format);
  48208. + DWC_VSNPRINTF(name, 128, format, args);
  48209. + va_end(args);
  48210. +
  48211. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  48212. + wq->pending++;
  48213. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  48214. + DWC_WAITQ_TRIGGER(wq->waitq);
  48215. +
  48216. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  48217. + if (!container) {
  48218. + DWC_ERROR("Cannot allocate memory for container\n");
  48219. + return;
  48220. + }
  48221. +
  48222. + container->name = DWC_STRDUP(name);
  48223. + if (!container->name) {
  48224. + DWC_ERROR("Cannot allocate memory for container->name\n");
  48225. + DWC_FREE(container);
  48226. + return;
  48227. + }
  48228. +
  48229. + container->cb = cb;
  48230. + container->data = data;
  48231. + container->wq = wq;
  48232. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  48233. + INIT_DELAYED_WORK(&container->work, do_work);
  48234. +
  48235. +#ifdef DEBUG
  48236. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  48237. +#endif
  48238. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  48239. +}
  48240. +
  48241. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  48242. +{
  48243. + return wq->pending;
  48244. +}
  48245. +
  48246. +
  48247. +#ifdef DWC_LIBMODULE
  48248. +
  48249. +#ifdef DWC_CCLIB
  48250. +/* CC */
  48251. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  48252. +EXPORT_SYMBOL(dwc_cc_if_free);
  48253. +EXPORT_SYMBOL(dwc_cc_clear);
  48254. +EXPORT_SYMBOL(dwc_cc_add);
  48255. +EXPORT_SYMBOL(dwc_cc_remove);
  48256. +EXPORT_SYMBOL(dwc_cc_change);
  48257. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  48258. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  48259. +EXPORT_SYMBOL(dwc_cc_match_chid);
  48260. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  48261. +EXPORT_SYMBOL(dwc_cc_ck);
  48262. +EXPORT_SYMBOL(dwc_cc_chid);
  48263. +EXPORT_SYMBOL(dwc_cc_cdid);
  48264. +EXPORT_SYMBOL(dwc_cc_name);
  48265. +#endif /* DWC_CCLIB */
  48266. +
  48267. +#ifdef DWC_CRYPTOLIB
  48268. +# ifndef CONFIG_MACH_IPMATE
  48269. +/* Modpow */
  48270. +EXPORT_SYMBOL(dwc_modpow);
  48271. +
  48272. +/* DH */
  48273. +EXPORT_SYMBOL(dwc_dh_modpow);
  48274. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  48275. +EXPORT_SYMBOL(dwc_dh_pk);
  48276. +# endif /* CONFIG_MACH_IPMATE */
  48277. +
  48278. +/* Crypto */
  48279. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  48280. +EXPORT_SYMBOL(dwc_wusb_cmf);
  48281. +EXPORT_SYMBOL(dwc_wusb_prf);
  48282. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  48283. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  48284. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  48285. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  48286. +#endif /* DWC_CRYPTOLIB */
  48287. +
  48288. +/* Notification */
  48289. +#ifdef DWC_NOTIFYLIB
  48290. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  48291. +EXPORT_SYMBOL(dwc_free_notification_manager);
  48292. +EXPORT_SYMBOL(dwc_register_notifier);
  48293. +EXPORT_SYMBOL(dwc_unregister_notifier);
  48294. +EXPORT_SYMBOL(dwc_add_observer);
  48295. +EXPORT_SYMBOL(dwc_remove_observer);
  48296. +EXPORT_SYMBOL(dwc_notify);
  48297. +#endif
  48298. +
  48299. +/* Memory Debugging Routines */
  48300. +#ifdef DWC_DEBUG_MEMORY
  48301. +EXPORT_SYMBOL(dwc_alloc_debug);
  48302. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  48303. +EXPORT_SYMBOL(dwc_free_debug);
  48304. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  48305. +EXPORT_SYMBOL(dwc_dma_free_debug);
  48306. +#endif
  48307. +
  48308. +EXPORT_SYMBOL(DWC_MEMSET);
  48309. +EXPORT_SYMBOL(DWC_MEMCPY);
  48310. +EXPORT_SYMBOL(DWC_MEMMOVE);
  48311. +EXPORT_SYMBOL(DWC_MEMCMP);
  48312. +EXPORT_SYMBOL(DWC_STRNCMP);
  48313. +EXPORT_SYMBOL(DWC_STRCMP);
  48314. +EXPORT_SYMBOL(DWC_STRLEN);
  48315. +EXPORT_SYMBOL(DWC_STRCPY);
  48316. +EXPORT_SYMBOL(DWC_STRDUP);
  48317. +EXPORT_SYMBOL(DWC_ATOI);
  48318. +EXPORT_SYMBOL(DWC_ATOUI);
  48319. +
  48320. +#ifdef DWC_UTFLIB
  48321. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  48322. +#endif /* DWC_UTFLIB */
  48323. +
  48324. +EXPORT_SYMBOL(DWC_IN_IRQ);
  48325. +EXPORT_SYMBOL(DWC_IN_BH);
  48326. +EXPORT_SYMBOL(DWC_VPRINTF);
  48327. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  48328. +EXPORT_SYMBOL(DWC_PRINTF);
  48329. +EXPORT_SYMBOL(DWC_SPRINTF);
  48330. +EXPORT_SYMBOL(DWC_SNPRINTF);
  48331. +EXPORT_SYMBOL(__DWC_WARN);
  48332. +EXPORT_SYMBOL(__DWC_ERROR);
  48333. +EXPORT_SYMBOL(DWC_EXCEPTION);
  48334. +
  48335. +#ifdef DEBUG
  48336. +EXPORT_SYMBOL(__DWC_DEBUG);
  48337. +#endif
  48338. +
  48339. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  48340. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  48341. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  48342. +EXPORT_SYMBOL(__DWC_ALLOC);
  48343. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  48344. +EXPORT_SYMBOL(__DWC_FREE);
  48345. +
  48346. +#ifdef DWC_CRYPTOLIB
  48347. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  48348. +EXPORT_SYMBOL(DWC_AES_CBC);
  48349. +EXPORT_SYMBOL(DWC_SHA256);
  48350. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  48351. +#endif
  48352. +
  48353. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  48354. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  48355. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  48356. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  48357. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  48358. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  48359. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  48360. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  48361. +EXPORT_SYMBOL(DWC_READ_REG32);
  48362. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  48363. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  48364. +
  48365. +#if 0
  48366. +EXPORT_SYMBOL(DWC_READ_REG64);
  48367. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  48368. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  48369. +#endif
  48370. +
  48371. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  48372. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  48373. +EXPORT_SYMBOL(DWC_SPINLOCK);
  48374. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  48375. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  48376. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  48377. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  48378. +
  48379. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  48380. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  48381. +#endif
  48382. +
  48383. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  48384. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  48385. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  48386. +EXPORT_SYMBOL(DWC_UDELAY);
  48387. +EXPORT_SYMBOL(DWC_MDELAY);
  48388. +EXPORT_SYMBOL(DWC_MSLEEP);
  48389. +EXPORT_SYMBOL(DWC_TIME);
  48390. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  48391. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  48392. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  48393. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  48394. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  48395. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  48396. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  48397. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  48398. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  48399. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  48400. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  48401. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  48402. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  48403. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  48404. +EXPORT_SYMBOL(DWC_TASK_FREE);
  48405. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  48406. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  48407. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  48408. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  48409. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  48410. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  48411. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  48412. +
  48413. +static int dwc_common_port_init_module(void)
  48414. +{
  48415. + int result = 0;
  48416. +
  48417. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  48418. +
  48419. +#ifdef DWC_DEBUG_MEMORY
  48420. + result = dwc_memory_debug_start(NULL);
  48421. + if (result) {
  48422. + printk(KERN_ERR
  48423. + "dwc_memory_debug_start() failed with error %d\n",
  48424. + result);
  48425. + return result;
  48426. + }
  48427. +#endif
  48428. +
  48429. +#ifdef DWC_NOTIFYLIB
  48430. + result = dwc_alloc_notification_manager(NULL, NULL);
  48431. + if (result) {
  48432. + printk(KERN_ERR
  48433. + "dwc_alloc_notification_manager() failed with error %d\n",
  48434. + result);
  48435. + return result;
  48436. + }
  48437. +#endif
  48438. + return result;
  48439. +}
  48440. +
  48441. +static void dwc_common_port_exit_module(void)
  48442. +{
  48443. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  48444. +
  48445. +#ifdef DWC_NOTIFYLIB
  48446. + dwc_free_notification_manager();
  48447. +#endif
  48448. +
  48449. +#ifdef DWC_DEBUG_MEMORY
  48450. + dwc_memory_debug_stop();
  48451. +#endif
  48452. +}
  48453. +
  48454. +module_init(dwc_common_port_init_module);
  48455. +module_exit(dwc_common_port_exit_module);
  48456. +
  48457. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  48458. +MODULE_AUTHOR("Synopsys Inc.");
  48459. +MODULE_LICENSE ("GPL");
  48460. +
  48461. +#endif /* DWC_LIBMODULE */
  48462. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  48463. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 1969-12-31 18:00:00.000000000 -0600
  48464. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 2014-12-11 14:02:55.352418001 -0600
  48465. @@ -0,0 +1,1275 @@
  48466. +#include "dwc_os.h"
  48467. +#include "dwc_list.h"
  48468. +
  48469. +#ifdef DWC_CCLIB
  48470. +# include "dwc_cc.h"
  48471. +#endif
  48472. +
  48473. +#ifdef DWC_CRYPTOLIB
  48474. +# include "dwc_modpow.h"
  48475. +# include "dwc_dh.h"
  48476. +# include "dwc_crypto.h"
  48477. +#endif
  48478. +
  48479. +#ifdef DWC_NOTIFYLIB
  48480. +# include "dwc_notifier.h"
  48481. +#endif
  48482. +
  48483. +/* OS-Level Implementations */
  48484. +
  48485. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  48486. +
  48487. +
  48488. +/* MISC */
  48489. +
  48490. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  48491. +{
  48492. + return memset(dest, byte, size);
  48493. +}
  48494. +
  48495. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  48496. +{
  48497. + return memcpy(dest, src, size);
  48498. +}
  48499. +
  48500. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  48501. +{
  48502. + bcopy(src, dest, size);
  48503. + return dest;
  48504. +}
  48505. +
  48506. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  48507. +{
  48508. + return memcmp(m1, m2, size);
  48509. +}
  48510. +
  48511. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  48512. +{
  48513. + return strncmp(s1, s2, size);
  48514. +}
  48515. +
  48516. +int DWC_STRCMP(void *s1, void *s2)
  48517. +{
  48518. + return strcmp(s1, s2);
  48519. +}
  48520. +
  48521. +int DWC_STRLEN(char const *str)
  48522. +{
  48523. + return strlen(str);
  48524. +}
  48525. +
  48526. +char *DWC_STRCPY(char *to, char const *from)
  48527. +{
  48528. + return strcpy(to, from);
  48529. +}
  48530. +
  48531. +char *DWC_STRDUP(char const *str)
  48532. +{
  48533. + int len = DWC_STRLEN(str) + 1;
  48534. + char *new = DWC_ALLOC_ATOMIC(len);
  48535. +
  48536. + if (!new) {
  48537. + return NULL;
  48538. + }
  48539. +
  48540. + DWC_MEMCPY(new, str, len);
  48541. + return new;
  48542. +}
  48543. +
  48544. +int DWC_ATOI(char *str, int32_t *value)
  48545. +{
  48546. + char *end = NULL;
  48547. +
  48548. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  48549. + * should be equivalent on 2's complement machines
  48550. + */
  48551. + *value = strtoul(str, &end, 0);
  48552. + if (*end == '\0') {
  48553. + return 0;
  48554. + }
  48555. +
  48556. + return -1;
  48557. +}
  48558. +
  48559. +int DWC_ATOUI(char *str, uint32_t *value)
  48560. +{
  48561. + char *end = NULL;
  48562. +
  48563. + *value = strtoul(str, &end, 0);
  48564. + if (*end == '\0') {
  48565. + return 0;
  48566. + }
  48567. +
  48568. + return -1;
  48569. +}
  48570. +
  48571. +
  48572. +#ifdef DWC_UTFLIB
  48573. +/* From usbstring.c */
  48574. +
  48575. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  48576. +{
  48577. + int count = 0;
  48578. + u8 c;
  48579. + u16 uchar;
  48580. +
  48581. + /* this insists on correct encodings, though not minimal ones.
  48582. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  48583. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  48584. + */
  48585. + while (len != 0 && (c = (u8) *s++) != 0) {
  48586. + if (unlikely(c & 0x80)) {
  48587. + // 2-byte sequence:
  48588. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  48589. + if ((c & 0xe0) == 0xc0) {
  48590. + uchar = (c & 0x1f) << 6;
  48591. +
  48592. + c = (u8) *s++;
  48593. + if ((c & 0xc0) != 0xc0)
  48594. + goto fail;
  48595. + c &= 0x3f;
  48596. + uchar |= c;
  48597. +
  48598. + // 3-byte sequence (most CJKV characters):
  48599. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  48600. + } else if ((c & 0xf0) == 0xe0) {
  48601. + uchar = (c & 0x0f) << 12;
  48602. +
  48603. + c = (u8) *s++;
  48604. + if ((c & 0xc0) != 0xc0)
  48605. + goto fail;
  48606. + c &= 0x3f;
  48607. + uchar |= c << 6;
  48608. +
  48609. + c = (u8) *s++;
  48610. + if ((c & 0xc0) != 0xc0)
  48611. + goto fail;
  48612. + c &= 0x3f;
  48613. + uchar |= c;
  48614. +
  48615. + /* no bogus surrogates */
  48616. + if (0xd800 <= uchar && uchar <= 0xdfff)
  48617. + goto fail;
  48618. +
  48619. + // 4-byte sequence (surrogate pairs, currently rare):
  48620. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  48621. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  48622. + // (uuuuu = wwww + 1)
  48623. + // FIXME accept the surrogate code points (only)
  48624. + } else
  48625. + goto fail;
  48626. + } else
  48627. + uchar = c;
  48628. + put_unaligned (cpu_to_le16 (uchar), cp++);
  48629. + count++;
  48630. + len--;
  48631. + }
  48632. + return count;
  48633. +fail:
  48634. + return -1;
  48635. +}
  48636. +
  48637. +#endif /* DWC_UTFLIB */
  48638. +
  48639. +
  48640. +/* dwc_debug.h */
  48641. +
  48642. +dwc_bool_t DWC_IN_IRQ(void)
  48643. +{
  48644. +// return in_irq();
  48645. + return 0;
  48646. +}
  48647. +
  48648. +dwc_bool_t DWC_IN_BH(void)
  48649. +{
  48650. +// return in_softirq();
  48651. + return 0;
  48652. +}
  48653. +
  48654. +void DWC_VPRINTF(char *format, va_list args)
  48655. +{
  48656. + vprintf(format, args);
  48657. +}
  48658. +
  48659. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  48660. +{
  48661. + return vsnprintf(str, size, format, args);
  48662. +}
  48663. +
  48664. +void DWC_PRINTF(char *format, ...)
  48665. +{
  48666. + va_list args;
  48667. +
  48668. + va_start(args, format);
  48669. + DWC_VPRINTF(format, args);
  48670. + va_end(args);
  48671. +}
  48672. +
  48673. +int DWC_SPRINTF(char *buffer, char *format, ...)
  48674. +{
  48675. + int retval;
  48676. + va_list args;
  48677. +
  48678. + va_start(args, format);
  48679. + retval = vsprintf(buffer, format, args);
  48680. + va_end(args);
  48681. + return retval;
  48682. +}
  48683. +
  48684. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  48685. +{
  48686. + int retval;
  48687. + va_list args;
  48688. +
  48689. + va_start(args, format);
  48690. + retval = vsnprintf(buffer, size, format, args);
  48691. + va_end(args);
  48692. + return retval;
  48693. +}
  48694. +
  48695. +void __DWC_WARN(char *format, ...)
  48696. +{
  48697. + va_list args;
  48698. +
  48699. + va_start(args, format);
  48700. + DWC_VPRINTF(format, args);
  48701. + va_end(args);
  48702. +}
  48703. +
  48704. +void __DWC_ERROR(char *format, ...)
  48705. +{
  48706. + va_list args;
  48707. +
  48708. + va_start(args, format);
  48709. + DWC_VPRINTF(format, args);
  48710. + va_end(args);
  48711. +}
  48712. +
  48713. +void DWC_EXCEPTION(char *format, ...)
  48714. +{
  48715. + va_list args;
  48716. +
  48717. + va_start(args, format);
  48718. + DWC_VPRINTF(format, args);
  48719. + va_end(args);
  48720. +// BUG_ON(1); ???
  48721. +}
  48722. +
  48723. +#ifdef DEBUG
  48724. +void __DWC_DEBUG(char *format, ...)
  48725. +{
  48726. + va_list args;
  48727. +
  48728. + va_start(args, format);
  48729. + DWC_VPRINTF(format, args);
  48730. + va_end(args);
  48731. +}
  48732. +#endif
  48733. +
  48734. +
  48735. +/* dwc_mem.h */
  48736. +
  48737. +#if 0
  48738. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  48739. + uint32_t align,
  48740. + uint32_t alloc)
  48741. +{
  48742. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  48743. + size, align, alloc);
  48744. + return (dwc_pool_t *)pool;
  48745. +}
  48746. +
  48747. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  48748. +{
  48749. + dma_pool_destroy((struct dma_pool *)pool);
  48750. +}
  48751. +
  48752. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  48753. +{
  48754. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  48755. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  48756. +}
  48757. +
  48758. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  48759. +{
  48760. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  48761. + memset(..);
  48762. +}
  48763. +
  48764. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  48765. +{
  48766. + dma_pool_free(pool, vaddr, daddr);
  48767. +}
  48768. +#endif
  48769. +
  48770. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  48771. +{
  48772. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  48773. + int error;
  48774. +
  48775. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  48776. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  48777. + &dma->nsegs, BUS_DMA_NOWAIT);
  48778. + if (error) {
  48779. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  48780. + (uintmax_t)size, error);
  48781. + goto fail_0;
  48782. + }
  48783. +
  48784. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  48785. + (caddr_t *)&dma->dma_vaddr,
  48786. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  48787. + if (error) {
  48788. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  48789. + goto fail_1;
  48790. + }
  48791. +
  48792. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  48793. + BUS_DMA_NOWAIT, &dma->dma_map);
  48794. + if (error) {
  48795. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  48796. + goto fail_2;
  48797. + }
  48798. +
  48799. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  48800. + size, NULL, BUS_DMA_NOWAIT);
  48801. + if (error) {
  48802. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  48803. + goto fail_3;
  48804. + }
  48805. +
  48806. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  48807. + *dma_addr = dma->dma_paddr;
  48808. + return dma->dma_vaddr;
  48809. +
  48810. +fail_3:
  48811. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  48812. +fail_2:
  48813. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  48814. +fail_1:
  48815. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  48816. +fail_0:
  48817. + dma->dma_map = NULL;
  48818. + dma->dma_vaddr = NULL;
  48819. + dma->nsegs = 0;
  48820. +
  48821. + return NULL;
  48822. +}
  48823. +
  48824. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  48825. +{
  48826. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  48827. +
  48828. + if (dma->dma_map != NULL) {
  48829. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  48830. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  48831. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  48832. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  48833. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  48834. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  48835. + dma->dma_paddr = 0;
  48836. + dma->dma_map = NULL;
  48837. + dma->dma_vaddr = NULL;
  48838. + dma->nsegs = 0;
  48839. + }
  48840. +}
  48841. +
  48842. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  48843. +{
  48844. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  48845. +}
  48846. +
  48847. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  48848. +{
  48849. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  48850. +}
  48851. +
  48852. +void __DWC_FREE(void *mem_ctx, void *addr)
  48853. +{
  48854. + free(addr, M_DEVBUF);
  48855. +}
  48856. +
  48857. +
  48858. +#ifdef DWC_CRYPTOLIB
  48859. +/* dwc_crypto.h */
  48860. +
  48861. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  48862. +{
  48863. + get_random_bytes(buffer, length);
  48864. +}
  48865. +
  48866. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  48867. +{
  48868. + struct crypto_blkcipher *tfm;
  48869. + struct blkcipher_desc desc;
  48870. + struct scatterlist sgd;
  48871. + struct scatterlist sgs;
  48872. +
  48873. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  48874. + if (tfm == NULL) {
  48875. + printk("failed to load transform for aes CBC\n");
  48876. + return -1;
  48877. + }
  48878. +
  48879. + crypto_blkcipher_setkey(tfm, key, keylen);
  48880. + crypto_blkcipher_set_iv(tfm, iv, 16);
  48881. +
  48882. + sg_init_one(&sgd, out, messagelen);
  48883. + sg_init_one(&sgs, message, messagelen);
  48884. +
  48885. + desc.tfm = tfm;
  48886. + desc.flags = 0;
  48887. +
  48888. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  48889. + crypto_free_blkcipher(tfm);
  48890. + DWC_ERROR("AES CBC encryption failed");
  48891. + return -1;
  48892. + }
  48893. +
  48894. + crypto_free_blkcipher(tfm);
  48895. + return 0;
  48896. +}
  48897. +
  48898. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  48899. +{
  48900. + struct crypto_hash *tfm;
  48901. + struct hash_desc desc;
  48902. + struct scatterlist sg;
  48903. +
  48904. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  48905. + if (IS_ERR(tfm)) {
  48906. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  48907. + return 0;
  48908. + }
  48909. + desc.tfm = tfm;
  48910. + desc.flags = 0;
  48911. +
  48912. + sg_init_one(&sg, message, len);
  48913. + crypto_hash_digest(&desc, &sg, len, out);
  48914. + crypto_free_hash(tfm);
  48915. +
  48916. + return 1;
  48917. +}
  48918. +
  48919. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  48920. + uint8_t *key, uint32_t keylen, uint8_t *out)
  48921. +{
  48922. + struct crypto_hash *tfm;
  48923. + struct hash_desc desc;
  48924. + struct scatterlist sg;
  48925. +
  48926. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  48927. + if (IS_ERR(tfm)) {
  48928. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  48929. + return 0;
  48930. + }
  48931. + desc.tfm = tfm;
  48932. + desc.flags = 0;
  48933. +
  48934. + sg_init_one(&sg, message, messagelen);
  48935. + crypto_hash_setkey(tfm, key, keylen);
  48936. + crypto_hash_digest(&desc, &sg, messagelen, out);
  48937. + crypto_free_hash(tfm);
  48938. +
  48939. + return 1;
  48940. +}
  48941. +
  48942. +#endif /* DWC_CRYPTOLIB */
  48943. +
  48944. +
  48945. +/* Byte Ordering Conversions */
  48946. +
  48947. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  48948. +{
  48949. +#ifdef __LITTLE_ENDIAN
  48950. + return *p;
  48951. +#else
  48952. + uint8_t *u_p = (uint8_t *)p;
  48953. +
  48954. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48955. +#endif
  48956. +}
  48957. +
  48958. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  48959. +{
  48960. +#ifdef __BIG_ENDIAN
  48961. + return *p;
  48962. +#else
  48963. + uint8_t *u_p = (uint8_t *)p;
  48964. +
  48965. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48966. +#endif
  48967. +}
  48968. +
  48969. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  48970. +{
  48971. +#ifdef __LITTLE_ENDIAN
  48972. + return *p;
  48973. +#else
  48974. + uint8_t *u_p = (uint8_t *)p;
  48975. +
  48976. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48977. +#endif
  48978. +}
  48979. +
  48980. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  48981. +{
  48982. +#ifdef __BIG_ENDIAN
  48983. + return *p;
  48984. +#else
  48985. + uint8_t *u_p = (uint8_t *)p;
  48986. +
  48987. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48988. +#endif
  48989. +}
  48990. +
  48991. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  48992. +{
  48993. +#ifdef __LITTLE_ENDIAN
  48994. + return *p;
  48995. +#else
  48996. + uint8_t *u_p = (uint8_t *)p;
  48997. + return (u_p[1] | (u_p[0] << 8));
  48998. +#endif
  48999. +}
  49000. +
  49001. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  49002. +{
  49003. +#ifdef __BIG_ENDIAN
  49004. + return *p;
  49005. +#else
  49006. + uint8_t *u_p = (uint8_t *)p;
  49007. + return (u_p[1] | (u_p[0] << 8));
  49008. +#endif
  49009. +}
  49010. +
  49011. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  49012. +{
  49013. +#ifdef __LITTLE_ENDIAN
  49014. + return *p;
  49015. +#else
  49016. + uint8_t *u_p = (uint8_t *)p;
  49017. + return (u_p[1] | (u_p[0] << 8));
  49018. +#endif
  49019. +}
  49020. +
  49021. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  49022. +{
  49023. +#ifdef __BIG_ENDIAN
  49024. + return *p;
  49025. +#else
  49026. + uint8_t *u_p = (uint8_t *)p;
  49027. + return (u_p[1] | (u_p[0] << 8));
  49028. +#endif
  49029. +}
  49030. +
  49031. +
  49032. +/* Registers */
  49033. +
  49034. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  49035. +{
  49036. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49037. + bus_size_t ior = (bus_size_t)reg;
  49038. +
  49039. + return bus_space_read_4(io->iot, io->ioh, ior);
  49040. +}
  49041. +
  49042. +#if 0
  49043. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  49044. +{
  49045. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49046. + bus_size_t ior = (bus_size_t)reg;
  49047. +
  49048. + return bus_space_read_8(io->iot, io->ioh, ior);
  49049. +}
  49050. +#endif
  49051. +
  49052. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  49053. +{
  49054. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49055. + bus_size_t ior = (bus_size_t)reg;
  49056. +
  49057. + bus_space_write_4(io->iot, io->ioh, ior, value);
  49058. +}
  49059. +
  49060. +#if 0
  49061. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  49062. +{
  49063. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49064. + bus_size_t ior = (bus_size_t)reg;
  49065. +
  49066. + bus_space_write_8(io->iot, io->ioh, ior, value);
  49067. +}
  49068. +#endif
  49069. +
  49070. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  49071. + uint32_t set_mask)
  49072. +{
  49073. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49074. + bus_size_t ior = (bus_size_t)reg;
  49075. +
  49076. + bus_space_write_4(io->iot, io->ioh, ior,
  49077. + (bus_space_read_4(io->iot, io->ioh, ior) &
  49078. + ~clear_mask) | set_mask);
  49079. +}
  49080. +
  49081. +#if 0
  49082. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  49083. + uint64_t set_mask)
  49084. +{
  49085. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49086. + bus_size_t ior = (bus_size_t)reg;
  49087. +
  49088. + bus_space_write_8(io->iot, io->ioh, ior,
  49089. + (bus_space_read_8(io->iot, io->ioh, ior) &
  49090. + ~clear_mask) | set_mask);
  49091. +}
  49092. +#endif
  49093. +
  49094. +
  49095. +/* Locking */
  49096. +
  49097. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  49098. +{
  49099. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  49100. +
  49101. + if (!sl) {
  49102. + DWC_ERROR("Cannot allocate memory for spinlock");
  49103. + return NULL;
  49104. + }
  49105. +
  49106. + simple_lock_init(sl);
  49107. + return (dwc_spinlock_t *)sl;
  49108. +}
  49109. +
  49110. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  49111. +{
  49112. + struct simplelock *sl = (struct simplelock *)lock;
  49113. +
  49114. + DWC_FREE(sl);
  49115. +}
  49116. +
  49117. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  49118. +{
  49119. + simple_lock((struct simplelock *)lock);
  49120. +}
  49121. +
  49122. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  49123. +{
  49124. + simple_unlock((struct simplelock *)lock);
  49125. +}
  49126. +
  49127. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  49128. +{
  49129. + simple_lock((struct simplelock *)lock);
  49130. + *flags = splbio();
  49131. +}
  49132. +
  49133. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  49134. +{
  49135. + splx(flags);
  49136. + simple_unlock((struct simplelock *)lock);
  49137. +}
  49138. +
  49139. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  49140. +{
  49141. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  49142. +
  49143. + if (!mutex) {
  49144. + DWC_ERROR("Cannot allocate memory for mutex");
  49145. + return NULL;
  49146. + }
  49147. +
  49148. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  49149. + return mutex;
  49150. +}
  49151. +
  49152. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  49153. +#else
  49154. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  49155. +{
  49156. + DWC_FREE(mutex);
  49157. +}
  49158. +#endif
  49159. +
  49160. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  49161. +{
  49162. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  49163. +}
  49164. +
  49165. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  49166. +{
  49167. + int status;
  49168. +
  49169. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  49170. + return status == 0;
  49171. +}
  49172. +
  49173. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  49174. +{
  49175. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  49176. +}
  49177. +
  49178. +
  49179. +/* Timing */
  49180. +
  49181. +void DWC_UDELAY(uint32_t usecs)
  49182. +{
  49183. + DELAY(usecs);
  49184. +}
  49185. +
  49186. +void DWC_MDELAY(uint32_t msecs)
  49187. +{
  49188. + do {
  49189. + DELAY(1000);
  49190. + } while (--msecs);
  49191. +}
  49192. +
  49193. +void DWC_MSLEEP(uint32_t msecs)
  49194. +{
  49195. + struct timeval tv;
  49196. +
  49197. + tv.tv_sec = msecs / 1000;
  49198. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  49199. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  49200. +}
  49201. +
  49202. +uint32_t DWC_TIME(void)
  49203. +{
  49204. + struct timeval tv;
  49205. +
  49206. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  49207. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  49208. +}
  49209. +
  49210. +
  49211. +/* Timers */
  49212. +
  49213. +struct dwc_timer {
  49214. + struct callout t;
  49215. + char *name;
  49216. + dwc_spinlock_t *lock;
  49217. + dwc_timer_callback_t cb;
  49218. + void *data;
  49219. +};
  49220. +
  49221. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  49222. +{
  49223. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  49224. +
  49225. + if (!t) {
  49226. + DWC_ERROR("Cannot allocate memory for timer");
  49227. + return NULL;
  49228. + }
  49229. +
  49230. + callout_init(&t->t);
  49231. +
  49232. + t->name = DWC_STRDUP(name);
  49233. + if (!t->name) {
  49234. + DWC_ERROR("Cannot allocate memory for timer->name");
  49235. + goto no_name;
  49236. + }
  49237. +
  49238. + t->lock = DWC_SPINLOCK_ALLOC();
  49239. + if (!t->lock) {
  49240. + DWC_ERROR("Cannot allocate memory for timer->lock");
  49241. + goto no_lock;
  49242. + }
  49243. +
  49244. + t->cb = cb;
  49245. + t->data = data;
  49246. +
  49247. + return t;
  49248. +
  49249. + no_lock:
  49250. + DWC_FREE(t->name);
  49251. + no_name:
  49252. + DWC_FREE(t);
  49253. +
  49254. + return NULL;
  49255. +}
  49256. +
  49257. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  49258. +{
  49259. + callout_stop(&timer->t);
  49260. + DWC_SPINLOCK_FREE(timer->lock);
  49261. + DWC_FREE(timer->name);
  49262. + DWC_FREE(timer);
  49263. +}
  49264. +
  49265. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  49266. +{
  49267. + struct timeval tv;
  49268. +
  49269. + tv.tv_sec = time / 1000;
  49270. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  49271. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  49272. +}
  49273. +
  49274. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  49275. +{
  49276. + callout_stop(&timer->t);
  49277. +}
  49278. +
  49279. +
  49280. +/* Wait Queues */
  49281. +
  49282. +struct dwc_waitq {
  49283. + struct simplelock lock;
  49284. + int abort;
  49285. +};
  49286. +
  49287. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  49288. +{
  49289. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  49290. +
  49291. + if (!wq) {
  49292. + DWC_ERROR("Cannot allocate memory for waitqueue");
  49293. + return NULL;
  49294. + }
  49295. +
  49296. + simple_lock_init(&wq->lock);
  49297. + wq->abort = 0;
  49298. +
  49299. + return wq;
  49300. +}
  49301. +
  49302. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  49303. +{
  49304. + DWC_FREE(wq);
  49305. +}
  49306. +
  49307. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  49308. +{
  49309. + int ipl;
  49310. + int result = 0;
  49311. +
  49312. + simple_lock(&wq->lock);
  49313. + ipl = splbio();
  49314. +
  49315. + /* Skip the sleep if already aborted or triggered */
  49316. + if (!wq->abort && !cond(data)) {
  49317. + splx(ipl);
  49318. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  49319. + ipl = splbio();
  49320. + }
  49321. +
  49322. + if (result == 0) { // awoken
  49323. + if (wq->abort) {
  49324. + wq->abort = 0;
  49325. + result = -DWC_E_ABORT;
  49326. + } else {
  49327. + result = 0;
  49328. + }
  49329. +
  49330. + splx(ipl);
  49331. + simple_unlock(&wq->lock);
  49332. + } else {
  49333. + wq->abort = 0;
  49334. + splx(ipl);
  49335. + simple_unlock(&wq->lock);
  49336. +
  49337. + if (result == ERESTART) { // signaled - restart
  49338. + result = -DWC_E_RESTART;
  49339. + } else { // signaled - must be EINTR
  49340. + result = -DWC_E_ABORT;
  49341. + }
  49342. + }
  49343. +
  49344. + return result;
  49345. +}
  49346. +
  49347. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  49348. + void *data, int32_t msecs)
  49349. +{
  49350. + struct timeval tv, tv1, tv2;
  49351. + int ipl;
  49352. + int result = 0;
  49353. +
  49354. + tv.tv_sec = msecs / 1000;
  49355. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  49356. +
  49357. + simple_lock(&wq->lock);
  49358. + ipl = splbio();
  49359. +
  49360. + /* Skip the sleep if already aborted or triggered */
  49361. + if (!wq->abort && !cond(data)) {
  49362. + splx(ipl);
  49363. + getmicrouptime(&tv1);
  49364. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  49365. + getmicrouptime(&tv2);
  49366. + ipl = splbio();
  49367. + }
  49368. +
  49369. + if (result == 0) { // awoken
  49370. + if (wq->abort) {
  49371. + wq->abort = 0;
  49372. + splx(ipl);
  49373. + simple_unlock(&wq->lock);
  49374. + result = -DWC_E_ABORT;
  49375. + } else {
  49376. + splx(ipl);
  49377. + simple_unlock(&wq->lock);
  49378. +
  49379. + tv2.tv_usec -= tv1.tv_usec;
  49380. + if (tv2.tv_usec < 0) {
  49381. + tv2.tv_usec += 1000000;
  49382. + tv2.tv_sec--;
  49383. + }
  49384. +
  49385. + tv2.tv_sec -= tv1.tv_sec;
  49386. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  49387. + result = msecs - result;
  49388. + if (result <= 0)
  49389. + result = 1;
  49390. + }
  49391. + } else {
  49392. + wq->abort = 0;
  49393. + splx(ipl);
  49394. + simple_unlock(&wq->lock);
  49395. +
  49396. + if (result == ERESTART) { // signaled - restart
  49397. + result = -DWC_E_RESTART;
  49398. +
  49399. + } else if (result == EINTR) { // signaled - interrupt
  49400. + result = -DWC_E_ABORT;
  49401. +
  49402. + } else { // timed out
  49403. + result = -DWC_E_TIMEOUT;
  49404. + }
  49405. + }
  49406. +
  49407. + return result;
  49408. +}
  49409. +
  49410. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  49411. +{
  49412. + wakeup(wq);
  49413. +}
  49414. +
  49415. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  49416. +{
  49417. + int ipl;
  49418. +
  49419. + simple_lock(&wq->lock);
  49420. + ipl = splbio();
  49421. + wq->abort = 1;
  49422. + wakeup(wq);
  49423. + splx(ipl);
  49424. + simple_unlock(&wq->lock);
  49425. +}
  49426. +
  49427. +
  49428. +/* Threading */
  49429. +
  49430. +struct dwc_thread {
  49431. + struct proc *proc;
  49432. + int abort;
  49433. +};
  49434. +
  49435. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  49436. +{
  49437. + int retval;
  49438. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  49439. +
  49440. + if (!thread) {
  49441. + return NULL;
  49442. + }
  49443. +
  49444. + thread->abort = 0;
  49445. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  49446. + "%s", name);
  49447. + if (retval) {
  49448. + DWC_FREE(thread);
  49449. + return NULL;
  49450. + }
  49451. +
  49452. + return thread;
  49453. +}
  49454. +
  49455. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  49456. +{
  49457. + int retval;
  49458. +
  49459. + thread->abort = 1;
  49460. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  49461. +
  49462. + if (retval == 0) {
  49463. + /* DWC_THREAD_EXIT() will free the thread struct */
  49464. + return 0;
  49465. + }
  49466. +
  49467. + /* NOTE: We leak the thread struct if thread doesn't die */
  49468. +
  49469. + if (retval == EWOULDBLOCK) {
  49470. + return -DWC_E_TIMEOUT;
  49471. + }
  49472. +
  49473. + return -DWC_E_UNKNOWN;
  49474. +}
  49475. +
  49476. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  49477. +{
  49478. + return thread->abort;
  49479. +}
  49480. +
  49481. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  49482. +{
  49483. + wakeup(&thread->abort);
  49484. + DWC_FREE(thread);
  49485. + kthread_exit(0);
  49486. +}
  49487. +
  49488. +/* tasklets
  49489. + - Runs in interrupt context (cannot sleep)
  49490. + - Each tasklet runs on a single CPU
  49491. + - Different tasklets can be running simultaneously on different CPUs
  49492. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  49493. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  49494. + */
  49495. +struct dwc_tasklet {
  49496. + dwc_tasklet_callback_t cb;
  49497. + void *data;
  49498. +};
  49499. +
  49500. +static void tasklet_callback(void *data)
  49501. +{
  49502. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  49503. +
  49504. + task->cb(task->data);
  49505. +}
  49506. +
  49507. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  49508. +{
  49509. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  49510. +
  49511. + if (task) {
  49512. + task->cb = cb;
  49513. + task->data = data;
  49514. + } else {
  49515. + DWC_ERROR("Cannot allocate memory for tasklet");
  49516. + }
  49517. +
  49518. + return task;
  49519. +}
  49520. +
  49521. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  49522. +{
  49523. + DWC_FREE(task);
  49524. +}
  49525. +
  49526. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  49527. +{
  49528. + tasklet_callback(task);
  49529. +}
  49530. +
  49531. +
  49532. +/* workqueues
  49533. + - Runs in process context (can sleep)
  49534. + */
  49535. +typedef struct work_container {
  49536. + dwc_work_callback_t cb;
  49537. + void *data;
  49538. + dwc_workq_t *wq;
  49539. + char *name;
  49540. + int hz;
  49541. + struct work task;
  49542. +} work_container_t;
  49543. +
  49544. +struct dwc_workq {
  49545. + struct workqueue *taskq;
  49546. + dwc_spinlock_t *lock;
  49547. + dwc_waitq_t *waitq;
  49548. + int pending;
  49549. + struct work_container *container;
  49550. +};
  49551. +
  49552. +static void do_work(struct work *task, void *data)
  49553. +{
  49554. + dwc_workq_t *wq = (dwc_workq_t *)data;
  49555. + work_container_t *container = wq->container;
  49556. + dwc_irqflags_t flags;
  49557. +
  49558. + if (container->hz) {
  49559. + tsleep(container, 0, "dw3wrk", container->hz);
  49560. + }
  49561. +
  49562. + container->cb(container->data);
  49563. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  49564. +
  49565. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49566. + if (container->name)
  49567. + DWC_FREE(container->name);
  49568. + DWC_FREE(container);
  49569. + wq->pending--;
  49570. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49571. + DWC_WAITQ_TRIGGER(wq->waitq);
  49572. +}
  49573. +
  49574. +static int work_done(void *data)
  49575. +{
  49576. + dwc_workq_t *workq = (dwc_workq_t *)data;
  49577. +
  49578. + return workq->pending == 0;
  49579. +}
  49580. +
  49581. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  49582. +{
  49583. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  49584. +}
  49585. +
  49586. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  49587. +{
  49588. + int result;
  49589. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  49590. +
  49591. + if (!wq) {
  49592. + DWC_ERROR("Cannot allocate memory for workqueue");
  49593. + return NULL;
  49594. + }
  49595. +
  49596. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  49597. + IPL_BIO, 0);
  49598. + if (result) {
  49599. + DWC_ERROR("Cannot create workqueue");
  49600. + goto no_taskq;
  49601. + }
  49602. +
  49603. + wq->pending = 0;
  49604. +
  49605. + wq->lock = DWC_SPINLOCK_ALLOC();
  49606. + if (!wq->lock) {
  49607. + DWC_ERROR("Cannot allocate memory for spinlock");
  49608. + goto no_lock;
  49609. + }
  49610. +
  49611. + wq->waitq = DWC_WAITQ_ALLOC();
  49612. + if (!wq->waitq) {
  49613. + DWC_ERROR("Cannot allocate memory for waitqueue");
  49614. + goto no_waitq;
  49615. + }
  49616. +
  49617. + return wq;
  49618. +
  49619. + no_waitq:
  49620. + DWC_SPINLOCK_FREE(wq->lock);
  49621. + no_lock:
  49622. + workqueue_destroy(wq->taskq);
  49623. + no_taskq:
  49624. + DWC_FREE(wq);
  49625. +
  49626. + return NULL;
  49627. +}
  49628. +
  49629. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  49630. +{
  49631. +#ifdef DEBUG
  49632. + dwc_irqflags_t flags;
  49633. +
  49634. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49635. +
  49636. + if (wq->pending != 0) {
  49637. + struct work_container *container = wq->container;
  49638. +
  49639. + DWC_ERROR("Destroying work queue with pending work");
  49640. +
  49641. + if (container && container->name) {
  49642. + DWC_ERROR("Work %s still pending", container->name);
  49643. + }
  49644. + }
  49645. +
  49646. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49647. +#endif
  49648. + DWC_WAITQ_FREE(wq->waitq);
  49649. + DWC_SPINLOCK_FREE(wq->lock);
  49650. + workqueue_destroy(wq->taskq);
  49651. + DWC_FREE(wq);
  49652. +}
  49653. +
  49654. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  49655. + char *format, ...)
  49656. +{
  49657. + dwc_irqflags_t flags;
  49658. + work_container_t *container;
  49659. + static char name[128];
  49660. + va_list args;
  49661. +
  49662. + va_start(args, format);
  49663. + DWC_VSNPRINTF(name, 128, format, args);
  49664. + va_end(args);
  49665. +
  49666. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49667. + wq->pending++;
  49668. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49669. + DWC_WAITQ_TRIGGER(wq->waitq);
  49670. +
  49671. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  49672. + if (!container) {
  49673. + DWC_ERROR("Cannot allocate memory for container");
  49674. + return;
  49675. + }
  49676. +
  49677. + container->name = DWC_STRDUP(name);
  49678. + if (!container->name) {
  49679. + DWC_ERROR("Cannot allocate memory for container->name");
  49680. + DWC_FREE(container);
  49681. + return;
  49682. + }
  49683. +
  49684. + container->cb = cb;
  49685. + container->data = data;
  49686. + container->wq = wq;
  49687. + container->hz = 0;
  49688. + wq->container = container;
  49689. +
  49690. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  49691. + workqueue_enqueue(wq->taskq, &container->task);
  49692. +}
  49693. +
  49694. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  49695. + void *data, uint32_t time, char *format, ...)
  49696. +{
  49697. + dwc_irqflags_t flags;
  49698. + work_container_t *container;
  49699. + static char name[128];
  49700. + struct timeval tv;
  49701. + va_list args;
  49702. +
  49703. + va_start(args, format);
  49704. + DWC_VSNPRINTF(name, 128, format, args);
  49705. + va_end(args);
  49706. +
  49707. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49708. + wq->pending++;
  49709. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49710. + DWC_WAITQ_TRIGGER(wq->waitq);
  49711. +
  49712. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  49713. + if (!container) {
  49714. + DWC_ERROR("Cannot allocate memory for container");
  49715. + return;
  49716. + }
  49717. +
  49718. + container->name = DWC_STRDUP(name);
  49719. + if (!container->name) {
  49720. + DWC_ERROR("Cannot allocate memory for container->name");
  49721. + DWC_FREE(container);
  49722. + return;
  49723. + }
  49724. +
  49725. + container->cb = cb;
  49726. + container->data = data;
  49727. + container->wq = wq;
  49728. + tv.tv_sec = time / 1000;
  49729. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  49730. + container->hz = tvtohz(&tv);
  49731. + wq->container = container;
  49732. +
  49733. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  49734. + workqueue_enqueue(wq->taskq, &container->task);
  49735. +}
  49736. +
  49737. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  49738. +{
  49739. + return wq->pending;
  49740. +}
  49741. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_crypto.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c
  49742. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_crypto.c 1969-12-31 18:00:00.000000000 -0600
  49743. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c 2014-12-11 14:02:55.352418001 -0600
  49744. @@ -0,0 +1,308 @@
  49745. +/* =========================================================================
  49746. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  49747. + * $Revision: #5 $
  49748. + * $Date: 2010/09/28 $
  49749. + * $Change: 1596182 $
  49750. + *
  49751. + * Synopsys Portability Library Software and documentation
  49752. + * (hereinafter, "Software") is an Unsupported proprietary work of
  49753. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  49754. + * between Synopsys and you.
  49755. + *
  49756. + * The Software IS NOT an item of Licensed Software or Licensed Product
  49757. + * under any End User Software License Agreement or Agreement for
  49758. + * Licensed Product with Synopsys or any supplement thereto. You are
  49759. + * permitted to use and redistribute this Software in source and binary
  49760. + * forms, with or without modification, provided that redistributions
  49761. + * of source code must retain this notice. You may not view, use,
  49762. + * disclose, copy or distribute this file or any information contained
  49763. + * herein except pursuant to this license grant from Synopsys. If you
  49764. + * do not agree with this notice, including the disclaimer below, then
  49765. + * you are not authorized to use the Software.
  49766. + *
  49767. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  49768. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  49769. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  49770. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  49771. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  49772. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  49773. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  49774. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  49775. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49776. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  49777. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  49778. + * DAMAGE.
  49779. + * ========================================================================= */
  49780. +
  49781. +/** @file
  49782. + * This file contains the WUSB cryptographic routines.
  49783. + */
  49784. +
  49785. +#ifdef DWC_CRYPTOLIB
  49786. +
  49787. +#include "dwc_crypto.h"
  49788. +#include "usb.h"
  49789. +
  49790. +#ifdef DEBUG
  49791. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  49792. +{
  49793. + int i;
  49794. + DWC_PRINTF("%s: ", name);
  49795. + for (i=0; i<len; i++) {
  49796. + DWC_PRINTF("%02x ", bytes[i]);
  49797. + }
  49798. + DWC_PRINTF("\n");
  49799. +}
  49800. +#else
  49801. +#define dump_bytes(x...)
  49802. +#endif
  49803. +
  49804. +/* Display a block */
  49805. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  49806. +{
  49807. +#ifdef DWC_DEBUG_CRYPTO
  49808. + int i, blksize = 16;
  49809. +
  49810. + DWC_DEBUG("%s", prefix);
  49811. +
  49812. + if (suffix == NULL) {
  49813. + suffix = "\n";
  49814. + blksize = a;
  49815. + }
  49816. +
  49817. + for (i = 0; i < blksize; i++)
  49818. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  49819. + DWC_PRINT(suffix);
  49820. +#endif
  49821. +}
  49822. +
  49823. +/**
  49824. + * Encrypts an array of bytes using the AES encryption engine.
  49825. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  49826. + * in-place.
  49827. + *
  49828. + * @return 0 on success, negative error code on error.
  49829. + */
  49830. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  49831. +{
  49832. + u8 block_t[16];
  49833. + DWC_MEMSET(block_t, 0, 16);
  49834. +
  49835. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  49836. +}
  49837. +
  49838. +/**
  49839. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  49840. + * This function takes a data string and returns the encrypted CBC
  49841. + * Counter-mode MIC.
  49842. + *
  49843. + * @param key The 128-bit symmetric key.
  49844. + * @param nonce The CCM nonce.
  49845. + * @param label The unique 14-byte ASCII text label.
  49846. + * @param bytes The byte array to be encrypted.
  49847. + * @param len Length of the byte array.
  49848. + * @param result Byte array to receive the 8-byte encrypted MIC.
  49849. + */
  49850. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  49851. + char *label, u8 *bytes, int len, u8 *result)
  49852. +{
  49853. + u8 block_m[16];
  49854. + u8 block_x[16];
  49855. + u8 block_t[8];
  49856. + int idx, blkNum;
  49857. + u16 la = (u16)(len + 14);
  49858. +
  49859. + /* Set the AES-128 key */
  49860. + //dwc_aes_setkey(tfm, key, 16);
  49861. +
  49862. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  49863. + block_m[0] = 0x59;
  49864. + for (idx = 0; idx < 13; idx++)
  49865. + block_m[idx + 1] = nonce[idx];
  49866. + block_m[14] = 0;
  49867. + block_m[15] = 0;
  49868. +
  49869. + /* Produce the CBC IV */
  49870. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  49871. + show_block(block_m, "CBC IV in: ", "\n", 0);
  49872. + show_block(block_x, "CBC IV out:", "\n", 0);
  49873. +
  49874. + /* Fill block B1 from l(a) = Blen + 14, and A */
  49875. + block_x[0] ^= (u8)(la >> 8);
  49876. + block_x[1] ^= (u8)la;
  49877. + for (idx = 0; idx < 14; idx++)
  49878. + block_x[idx + 2] ^= label[idx];
  49879. + show_block(block_x, "After xor: ", "b1\n", 16);
  49880. +
  49881. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  49882. + show_block(block_x, "After AES: ", "b1\n", 16);
  49883. +
  49884. + idx = 0;
  49885. + blkNum = 0;
  49886. +
  49887. + /* Fill remaining blocks with B */
  49888. + while (len-- > 0) {
  49889. + block_x[idx] ^= *bytes++;
  49890. + if (++idx >= 16) {
  49891. + idx = 0;
  49892. + show_block(block_x, "After xor: ", "\n", blkNum);
  49893. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  49894. + show_block(block_x, "After AES: ", "\n", blkNum);
  49895. + blkNum++;
  49896. + }
  49897. + }
  49898. +
  49899. + /* Handle partial last block */
  49900. + if (idx > 0) {
  49901. + show_block(block_x, "After xor: ", "\n", blkNum);
  49902. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  49903. + show_block(block_x, "After AES: ", "\n", blkNum);
  49904. + }
  49905. +
  49906. + /* Save the MIC tag */
  49907. + DWC_MEMCPY(block_t, block_x, 8);
  49908. + show_block(block_t, "MIC tag : ", NULL, 8);
  49909. +
  49910. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  49911. + block_m[0] = 0x01;
  49912. + block_m[14] = 0;
  49913. + block_m[15] = 0;
  49914. +
  49915. + /* Encrypt the counter */
  49916. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  49917. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  49918. +
  49919. + /* XOR with MIC tag */
  49920. + for (idx = 0; idx < 8; idx++) {
  49921. + block_t[idx] ^= block_x[idx];
  49922. + }
  49923. +
  49924. + /* Return result to caller */
  49925. + DWC_MEMCPY(result, block_t, 8);
  49926. + show_block(result, "CCM-MIC : ", NULL, 8);
  49927. +
  49928. +}
  49929. +
  49930. +/**
  49931. + * The PRF function described in section 6.5 of the WUSB spec. This function
  49932. + * concatenates MIC values returned from dwc_cmf() to create a value of
  49933. + * the requested length.
  49934. + *
  49935. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  49936. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  49937. + * @param result Byte array to receive the result.
  49938. + */
  49939. +void dwc_wusb_prf(int prf_len, u8 *key,
  49940. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  49941. +{
  49942. + int i;
  49943. +
  49944. + nonce[0] = 0;
  49945. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  49946. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  49947. + result += 8;
  49948. + }
  49949. +}
  49950. +
  49951. +/**
  49952. + * Fills in CCM Nonce per the WUSB spec.
  49953. + *
  49954. + * @param[in] haddr Host address.
  49955. + * @param[in] daddr Device address.
  49956. + * @param[in] tkid Session Key(PTK) identifier.
  49957. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  49958. + */
  49959. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  49960. + uint8_t *nonce)
  49961. +{
  49962. +
  49963. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  49964. +
  49965. + DWC_MEMSET(&nonce[0], 0, 16);
  49966. +
  49967. + DWC_MEMCPY(&nonce[6], tkid, 3);
  49968. + nonce[9] = daddr & 0xFF;
  49969. + nonce[10] = (daddr >> 8) & 0xFF;
  49970. + nonce[11] = haddr & 0xFF;
  49971. + nonce[12] = (haddr >> 8) & 0xFF;
  49972. +
  49973. + dump_bytes("CCM nonce", nonce, 16);
  49974. +}
  49975. +
  49976. +/**
  49977. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  49978. + * Nonce.
  49979. + */
  49980. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  49981. +{
  49982. + uint8_t inonce[16];
  49983. + uint32_t temp[4];
  49984. +
  49985. + /* Fill in the Nonce */
  49986. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  49987. + inonce[9] = addr & 0xFF;
  49988. + inonce[10] = (addr >> 8) & 0xFF;
  49989. + inonce[11] = inonce[9];
  49990. + inonce[12] = inonce[10];
  49991. +
  49992. + /* Collect "randomness samples" */
  49993. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  49994. +
  49995. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  49996. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  49997. + nonce);
  49998. +}
  49999. +
  50000. +/**
  50001. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  50002. + * WUSB spec.
  50003. + *
  50004. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  50005. + * @param[in] mk Master Key to derive the session from
  50006. + * @param[in] hnonce Pointer to Host Nonce.
  50007. + * @param[in] dnonce Pointer to Device Nonce.
  50008. + * @param[out] kck Pointer to where the KCK output is to be written.
  50009. + * @param[out] ptk Pointer to where the PTK output is to be written.
  50010. + */
  50011. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  50012. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  50013. +{
  50014. + uint8_t idata[32];
  50015. + uint8_t odata[32];
  50016. +
  50017. + dump_bytes("ck", mk, 16);
  50018. + dump_bytes("hnonce", hnonce, 16);
  50019. + dump_bytes("dnonce", dnonce, 16);
  50020. +
  50021. + /* The data is the HNonce and DNonce concatenated */
  50022. + DWC_MEMCPY(&idata[0], hnonce, 16);
  50023. + DWC_MEMCPY(&idata[16], dnonce, 16);
  50024. +
  50025. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  50026. +
  50027. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  50028. + DWC_MEMCPY(kck, &odata[0], 16);
  50029. + DWC_MEMCPY(ptk, &odata[16], 16);
  50030. +
  50031. + dump_bytes("kck", kck, 16);
  50032. + dump_bytes("ptk", ptk, 16);
  50033. +}
  50034. +
  50035. +/**
  50036. + * Generates the Message Integrity Code over the Handshake data per the
  50037. + * WUSB spec.
  50038. + *
  50039. + * @param ccm_nonce Pointer to CCM Nonce.
  50040. + * @param kck Pointer to Key Confirmation Key.
  50041. + * @param data Pointer to Handshake data to be checked.
  50042. + * @param mic Pointer to where the MIC output is to be written.
  50043. + */
  50044. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  50045. + uint8_t *data, uint8_t *mic)
  50046. +{
  50047. +
  50048. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  50049. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  50050. +}
  50051. +
  50052. +#endif /* DWC_CRYPTOLIB */
  50053. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_crypto.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h
  50054. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_crypto.h 1969-12-31 18:00:00.000000000 -0600
  50055. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h 2014-12-11 14:02:55.352418001 -0600
  50056. @@ -0,0 +1,111 @@
  50057. +/* =========================================================================
  50058. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  50059. + * $Revision: #3 $
  50060. + * $Date: 2010/09/28 $
  50061. + * $Change: 1596182 $
  50062. + *
  50063. + * Synopsys Portability Library Software and documentation
  50064. + * (hereinafter, "Software") is an Unsupported proprietary work of
  50065. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  50066. + * between Synopsys and you.
  50067. + *
  50068. + * The Software IS NOT an item of Licensed Software or Licensed Product
  50069. + * under any End User Software License Agreement or Agreement for
  50070. + * Licensed Product with Synopsys or any supplement thereto. You are
  50071. + * permitted to use and redistribute this Software in source and binary
  50072. + * forms, with or without modification, provided that redistributions
  50073. + * of source code must retain this notice. You may not view, use,
  50074. + * disclose, copy or distribute this file or any information contained
  50075. + * herein except pursuant to this license grant from Synopsys. If you
  50076. + * do not agree with this notice, including the disclaimer below, then
  50077. + * you are not authorized to use the Software.
  50078. + *
  50079. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  50080. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50081. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  50082. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  50083. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50084. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  50085. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  50086. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  50087. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50088. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  50089. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50090. + * DAMAGE.
  50091. + * ========================================================================= */
  50092. +
  50093. +#ifndef _DWC_CRYPTO_H_
  50094. +#define _DWC_CRYPTO_H_
  50095. +
  50096. +#ifdef __cplusplus
  50097. +extern "C" {
  50098. +#endif
  50099. +
  50100. +/** @file
  50101. + *
  50102. + * This file contains declarations for the WUSB Cryptographic routines as
  50103. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  50104. + * modules.
  50105. + */
  50106. +
  50107. +#include "dwc_os.h"
  50108. +
  50109. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  50110. +
  50111. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  50112. + char *label, u8 *bytes, int len, u8 *result);
  50113. +void dwc_wusb_prf(int prf_len, u8 *key,
  50114. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  50115. +
  50116. +/**
  50117. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  50118. + *
  50119. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  50120. + */
  50121. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  50122. + char *label, u8 *bytes, int len, u8 *result)
  50123. +{
  50124. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  50125. +}
  50126. +
  50127. +/**
  50128. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  50129. + *
  50130. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  50131. + */
  50132. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  50133. + char *label, u8 *bytes, int len, u8 *result)
  50134. +{
  50135. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  50136. +}
  50137. +
  50138. +/**
  50139. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  50140. + *
  50141. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  50142. + */
  50143. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  50144. + char *label, u8 *bytes, int len, u8 *result)
  50145. +{
  50146. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  50147. +}
  50148. +
  50149. +
  50150. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  50151. + uint8_t *nonce);
  50152. +void dwc_wusb_gen_nonce(uint16_t addr,
  50153. + uint8_t *nonce);
  50154. +
  50155. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  50156. + uint8_t *hnonce, uint8_t *dnonce,
  50157. + uint8_t *kck, uint8_t *ptk);
  50158. +
  50159. +
  50160. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  50161. + *kck, uint8_t *data, uint8_t *mic);
  50162. +
  50163. +#ifdef __cplusplus
  50164. +}
  50165. +#endif
  50166. +
  50167. +#endif /* _DWC_CRYPTO_H_ */
  50168. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_dh.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c
  50169. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_dh.c 1969-12-31 18:00:00.000000000 -0600
  50170. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c 2014-12-11 14:02:55.352418001 -0600
  50171. @@ -0,0 +1,291 @@
  50172. +/* =========================================================================
  50173. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  50174. + * $Revision: #3 $
  50175. + * $Date: 2010/09/28 $
  50176. + * $Change: 1596182 $
  50177. + *
  50178. + * Synopsys Portability Library Software and documentation
  50179. + * (hereinafter, "Software") is an Unsupported proprietary work of
  50180. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  50181. + * between Synopsys and you.
  50182. + *
  50183. + * The Software IS NOT an item of Licensed Software or Licensed Product
  50184. + * under any End User Software License Agreement or Agreement for
  50185. + * Licensed Product with Synopsys or any supplement thereto. You are
  50186. + * permitted to use and redistribute this Software in source and binary
  50187. + * forms, with or without modification, provided that redistributions
  50188. + * of source code must retain this notice. You may not view, use,
  50189. + * disclose, copy or distribute this file or any information contained
  50190. + * herein except pursuant to this license grant from Synopsys. If you
  50191. + * do not agree with this notice, including the disclaimer below, then
  50192. + * you are not authorized to use the Software.
  50193. + *
  50194. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  50195. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50196. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  50197. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  50198. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50199. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  50200. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  50201. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  50202. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50203. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  50204. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50205. + * DAMAGE.
  50206. + * ========================================================================= */
  50207. +#ifdef DWC_CRYPTOLIB
  50208. +
  50209. +#ifndef CONFIG_MACH_IPMATE
  50210. +
  50211. +#include "dwc_dh.h"
  50212. +#include "dwc_modpow.h"
  50213. +
  50214. +#ifdef DEBUG
  50215. +/* This function prints out a buffer in the format described in the Association
  50216. + * Model specification. */
  50217. +static void dh_dump(char *str, void *_num, int len)
  50218. +{
  50219. + uint8_t *num = _num;
  50220. + int i;
  50221. + DWC_PRINTF("%s\n", str);
  50222. + for (i = 0; i < len; i ++) {
  50223. + DWC_PRINTF("%02x", num[i]);
  50224. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  50225. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  50226. + }
  50227. +
  50228. + DWC_PRINTF("\n");
  50229. +}
  50230. +#else
  50231. +#define dh_dump(_x...) do {; } while(0)
  50232. +#endif
  50233. +
  50234. +/* Constant g value */
  50235. +static __u32 dh_g[] = {
  50236. + 0x02000000,
  50237. +};
  50238. +
  50239. +/* Constant p value */
  50240. +static __u32 dh_p[] = {
  50241. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  50242. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  50243. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  50244. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  50245. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  50246. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  50247. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  50248. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  50249. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  50250. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  50251. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  50252. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  50253. +};
  50254. +
  50255. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  50256. +{
  50257. + uint8_t *in = _in;
  50258. + uint8_t *out = _out;
  50259. + int i;
  50260. + for (i=0; i<len; i++) {
  50261. + out[i] = in[len-1-i];
  50262. + }
  50263. +}
  50264. +
  50265. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  50266. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  50267. + * of 4. */
  50268. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  50269. + void *exp, uint32_t exp_len,
  50270. + void *mod, uint32_t mod_len,
  50271. + void *out)
  50272. +{
  50273. + /* modpow() takes little endian numbers. AM uses big-endian. This
  50274. + * function swaps bytes of numbers before passing onto modpow. */
  50275. +
  50276. + int retval = 0;
  50277. + uint32_t *result;
  50278. +
  50279. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  50280. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  50281. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  50282. +
  50283. + dh_swap_bytes(num, &bignum_num[1], num_len);
  50284. + bignum_num[0] = num_len / 4;
  50285. +
  50286. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  50287. + bignum_exp[0] = exp_len / 4;
  50288. +
  50289. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  50290. + bignum_mod[0] = mod_len / 4;
  50291. +
  50292. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  50293. + if (!result) {
  50294. + retval = -1;
  50295. + goto dh_modpow_nomem;
  50296. + }
  50297. +
  50298. + dh_swap_bytes(&result[1], out, result[0] * 4);
  50299. + dwc_free(mem_ctx, result);
  50300. +
  50301. + dh_modpow_nomem:
  50302. + dwc_free(mem_ctx, bignum_num);
  50303. + dwc_free(mem_ctx, bignum_exp);
  50304. + dwc_free(mem_ctx, bignum_mod);
  50305. + return retval;
  50306. +}
  50307. +
  50308. +
  50309. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  50310. +{
  50311. + int retval;
  50312. + uint8_t m3[385];
  50313. +
  50314. +#ifndef DH_TEST_VECTORS
  50315. + DWC_RANDOM_BYTES(exp, 32);
  50316. +#endif
  50317. +
  50318. + /* Compute the pkd */
  50319. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  50320. + exp, 32,
  50321. + dh_p, 384, pk))) {
  50322. + return retval;
  50323. + }
  50324. +
  50325. + m3[384] = nd;
  50326. + DWC_MEMCPY(&m3[0], pk, 384);
  50327. + DWC_SHA256(m3, 385, hash);
  50328. +
  50329. + dh_dump("PK", pk, 384);
  50330. + dh_dump("SHA-256(M3)", hash, 32);
  50331. + return 0;
  50332. +}
  50333. +
  50334. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  50335. + uint8_t *exp, int is_host,
  50336. + char *dd, uint8_t *ck, uint8_t *kdk)
  50337. +{
  50338. + int retval;
  50339. + uint8_t mv[784];
  50340. + uint8_t sha_result[32];
  50341. + uint8_t dhkey[384];
  50342. + uint8_t shared_secret[384];
  50343. + char *message;
  50344. + uint32_t vd;
  50345. +
  50346. + uint8_t *pk;
  50347. +
  50348. + if (is_host) {
  50349. + pk = pkd;
  50350. + }
  50351. + else {
  50352. + pk = pkh;
  50353. + }
  50354. +
  50355. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  50356. + exp, 32,
  50357. + dh_p, 384, shared_secret))) {
  50358. + return retval;
  50359. + }
  50360. + dh_dump("Shared Secret", shared_secret, 384);
  50361. +
  50362. + DWC_SHA256(shared_secret, 384, dhkey);
  50363. + dh_dump("DHKEY", dhkey, 384);
  50364. +
  50365. + DWC_MEMCPY(&mv[0], pkd, 384);
  50366. + DWC_MEMCPY(&mv[384], pkh, 384);
  50367. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  50368. + dh_dump("MV", mv, 784);
  50369. +
  50370. + DWC_SHA256(mv, 784, sha_result);
  50371. + dh_dump("SHA-256(MV)", sha_result, 32);
  50372. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  50373. +
  50374. + dh_swap_bytes(sha_result, &vd, 4);
  50375. +#ifdef DEBUG
  50376. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  50377. +#endif
  50378. +
  50379. + switch (nd) {
  50380. + case 2:
  50381. + vd = vd % 100;
  50382. + DWC_SPRINTF(dd, "%02d", vd);
  50383. + break;
  50384. + case 3:
  50385. + vd = vd % 1000;
  50386. + DWC_SPRINTF(dd, "%03d", vd);
  50387. + break;
  50388. + case 4:
  50389. + vd = vd % 10000;
  50390. + DWC_SPRINTF(dd, "%04d", vd);
  50391. + break;
  50392. + }
  50393. +#ifdef DEBUG
  50394. + DWC_PRINTF("Display Digits: %s\n", dd);
  50395. +#endif
  50396. +
  50397. + message = "connection key";
  50398. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  50399. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  50400. + DWC_MEMCPY(ck, sha_result, 16);
  50401. +
  50402. + message = "key derivation key";
  50403. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  50404. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  50405. + DWC_MEMCPY(kdk, sha_result, 32);
  50406. +
  50407. + return 0;
  50408. +}
  50409. +
  50410. +
  50411. +#ifdef DH_TEST_VECTORS
  50412. +
  50413. +static __u8 dh_a[] = {
  50414. + 0x44, 0x00, 0x51, 0xd6,
  50415. + 0xf0, 0xb5, 0x5e, 0xa9,
  50416. + 0x67, 0xab, 0x31, 0xc6,
  50417. + 0x8a, 0x8b, 0x5e, 0x37,
  50418. + 0xd9, 0x10, 0xda, 0xe0,
  50419. + 0xe2, 0xd4, 0x59, 0xa4,
  50420. + 0x86, 0x45, 0x9c, 0xaa,
  50421. + 0xdf, 0x36, 0x75, 0x16,
  50422. +};
  50423. +
  50424. +static __u8 dh_b[] = {
  50425. + 0x5d, 0xae, 0xc7, 0x86,
  50426. + 0x79, 0x80, 0xa3, 0x24,
  50427. + 0x8c, 0xe3, 0x57, 0x8f,
  50428. + 0xc7, 0x5f, 0x1b, 0x0f,
  50429. + 0x2d, 0xf8, 0x9d, 0x30,
  50430. + 0x6f, 0xa4, 0x52, 0xcd,
  50431. + 0xe0, 0x7a, 0x04, 0x8a,
  50432. + 0xde, 0xd9, 0x26, 0x56,
  50433. +};
  50434. +
  50435. +void dwc_run_dh_test_vectors(void *mem_ctx)
  50436. +{
  50437. + uint8_t pkd[384];
  50438. + uint8_t pkh[384];
  50439. + uint8_t hashd[32];
  50440. + uint8_t hashh[32];
  50441. + uint8_t ck[16];
  50442. + uint8_t kdk[32];
  50443. + char dd[5];
  50444. +
  50445. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  50446. +
  50447. + /* compute the PKd and SHA-256(PKd || Nd) */
  50448. + DWC_PRINTF("Computing PKd\n");
  50449. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  50450. +
  50451. + /* compute the PKd and SHA-256(PKh || Nd) */
  50452. + DWC_PRINTF("Computing PKh\n");
  50453. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  50454. +
  50455. + /* compute the dhkey */
  50456. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  50457. +}
  50458. +#endif /* DH_TEST_VECTORS */
  50459. +
  50460. +#endif /* !CONFIG_MACH_IPMATE */
  50461. +
  50462. +#endif /* DWC_CRYPTOLIB */
  50463. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_dh.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h
  50464. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_dh.h 1969-12-31 18:00:00.000000000 -0600
  50465. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h 2014-12-11 14:02:55.352418001 -0600
  50466. @@ -0,0 +1,106 @@
  50467. +/* =========================================================================
  50468. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  50469. + * $Revision: #4 $
  50470. + * $Date: 2010/09/28 $
  50471. + * $Change: 1596182 $
  50472. + *
  50473. + * Synopsys Portability Library Software and documentation
  50474. + * (hereinafter, "Software") is an Unsupported proprietary work of
  50475. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  50476. + * between Synopsys and you.
  50477. + *
  50478. + * The Software IS NOT an item of Licensed Software or Licensed Product
  50479. + * under any End User Software License Agreement or Agreement for
  50480. + * Licensed Product with Synopsys or any supplement thereto. You are
  50481. + * permitted to use and redistribute this Software in source and binary
  50482. + * forms, with or without modification, provided that redistributions
  50483. + * of source code must retain this notice. You may not view, use,
  50484. + * disclose, copy or distribute this file or any information contained
  50485. + * herein except pursuant to this license grant from Synopsys. If you
  50486. + * do not agree with this notice, including the disclaimer below, then
  50487. + * you are not authorized to use the Software.
  50488. + *
  50489. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  50490. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50491. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  50492. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  50493. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50494. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  50495. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  50496. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  50497. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50498. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  50499. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50500. + * DAMAGE.
  50501. + * ========================================================================= */
  50502. +#ifndef _DWC_DH_H_
  50503. +#define _DWC_DH_H_
  50504. +
  50505. +#ifdef __cplusplus
  50506. +extern "C" {
  50507. +#endif
  50508. +
  50509. +#include "dwc_os.h"
  50510. +
  50511. +/** @file
  50512. + *
  50513. + * This file defines the common functions on device and host for performing
  50514. + * numeric association as defined in the WUSB spec. They are only to be
  50515. + * used internally by the DWC UWB modules. */
  50516. +
  50517. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  50518. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  50519. + uint8_t *key, uint32_t keylen,
  50520. + uint8_t *out);
  50521. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  50522. + void *exp, uint32_t exp_len,
  50523. + void *mod, uint32_t mod_len,
  50524. + void *out);
  50525. +
  50526. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  50527. + *
  50528. + * PK = g^exp mod p.
  50529. + *
  50530. + * Input:
  50531. + * Nd = Number of digits on the device.
  50532. + *
  50533. + * Output:
  50534. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  50535. + * used as either A or B.
  50536. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  50537. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  50538. + */
  50539. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  50540. +
  50541. +/** Computes the DHKEY, and VD.
  50542. + *
  50543. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  50544. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  50545. + *
  50546. + * Input:
  50547. + * pkd = The PKD value.
  50548. + * pkh = The PKH value.
  50549. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  50550. + * is_host = Set to non zero if a WUSB host is calling this function.
  50551. + *
  50552. + * Output:
  50553. +
  50554. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  50555. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  50556. + * null termination character. This buffer can be used directly for display.
  50557. + * ck = A 16-byte buffer to be filled with the CK.
  50558. + * kdk = A 32-byte buffer to be filled with the KDK.
  50559. + */
  50560. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  50561. + uint8_t *exp, int is_host,
  50562. + char *dd, uint8_t *ck, uint8_t *kdk);
  50563. +
  50564. +#ifdef DH_TEST_VECTORS
  50565. +extern void dwc_run_dh_test_vectors(void);
  50566. +#endif
  50567. +
  50568. +#ifdef __cplusplus
  50569. +}
  50570. +#endif
  50571. +
  50572. +#endif /* _DWC_DH_H_ */
  50573. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_list.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_list.h
  50574. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_list.h 1969-12-31 18:00:00.000000000 -0600
  50575. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_list.h 2014-12-11 14:02:55.352418001 -0600
  50576. @@ -0,0 +1,594 @@
  50577. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  50578. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  50579. +
  50580. +/*
  50581. + * Copyright (c) 1991, 1993
  50582. + * The Regents of the University of California. All rights reserved.
  50583. + *
  50584. + * Redistribution and use in source and binary forms, with or without
  50585. + * modification, are permitted provided that the following conditions
  50586. + * are met:
  50587. + * 1. Redistributions of source code must retain the above copyright
  50588. + * notice, this list of conditions and the following disclaimer.
  50589. + * 2. Redistributions in binary form must reproduce the above copyright
  50590. + * notice, this list of conditions and the following disclaimer in the
  50591. + * documentation and/or other materials provided with the distribution.
  50592. + * 3. Neither the name of the University nor the names of its contributors
  50593. + * may be used to endorse or promote products derived from this software
  50594. + * without specific prior written permission.
  50595. + *
  50596. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50597. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50598. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50599. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  50600. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50601. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50602. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50603. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50604. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50605. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50606. + * SUCH DAMAGE.
  50607. + *
  50608. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  50609. + */
  50610. +
  50611. +#ifndef _DWC_LIST_H_
  50612. +#define _DWC_LIST_H_
  50613. +
  50614. +#ifdef __cplusplus
  50615. +extern "C" {
  50616. +#endif
  50617. +
  50618. +/** @file
  50619. + *
  50620. + * This file defines linked list operations. It is derived from BSD with
  50621. + * only the MACRO names being prefixed with DWC_. This is because a few of
  50622. + * these names conflict with those on Linux. For documentation on use, see the
  50623. + * inline comments in the source code. The original license for this source
  50624. + * code applies and is preserved in the dwc_list.h source file.
  50625. + */
  50626. +
  50627. +/*
  50628. + * This file defines five types of data structures: singly-linked lists,
  50629. + * lists, simple queues, tail queues, and circular queues.
  50630. + *
  50631. + *
  50632. + * A singly-linked list is headed by a single forward pointer. The elements
  50633. + * are singly linked for minimum space and pointer manipulation overhead at
  50634. + * the expense of O(n) removal for arbitrary elements. New elements can be
  50635. + * added to the list after an existing element or at the head of the list.
  50636. + * Elements being removed from the head of the list should use the explicit
  50637. + * macro for this purpose for optimum efficiency. A singly-linked list may
  50638. + * only be traversed in the forward direction. Singly-linked lists are ideal
  50639. + * for applications with large datasets and few or no removals or for
  50640. + * implementing a LIFO queue.
  50641. + *
  50642. + * A list is headed by a single forward pointer (or an array of forward
  50643. + * pointers for a hash table header). The elements are doubly linked
  50644. + * so that an arbitrary element can be removed without a need to
  50645. + * traverse the list. New elements can be added to the list before
  50646. + * or after an existing element or at the head of the list. A list
  50647. + * may only be traversed in the forward direction.
  50648. + *
  50649. + * A simple queue is headed by a pair of pointers, one the head of the
  50650. + * list and the other to the tail of the list. The elements are singly
  50651. + * linked to save space, so elements can only be removed from the
  50652. + * head of the list. New elements can be added to the list before or after
  50653. + * an existing element, at the head of the list, or at the end of the
  50654. + * list. A simple queue may only be traversed in the forward direction.
  50655. + *
  50656. + * A tail queue is headed by a pair of pointers, one to the head of the
  50657. + * list and the other to the tail of the list. The elements are doubly
  50658. + * linked so that an arbitrary element can be removed without a need to
  50659. + * traverse the list. New elements can be added to the list before or
  50660. + * after an existing element, at the head of the list, or at the end of
  50661. + * the list. A tail queue may be traversed in either direction.
  50662. + *
  50663. + * A circle queue is headed by a pair of pointers, one to the head of the
  50664. + * list and the other to the tail of the list. The elements are doubly
  50665. + * linked so that an arbitrary element can be removed without a need to
  50666. + * traverse the list. New elements can be added to the list before or after
  50667. + * an existing element, at the head of the list, or at the end of the list.
  50668. + * A circle queue may be traversed in either direction, but has a more
  50669. + * complex end of list detection.
  50670. + *
  50671. + * For details on the use of these macros, see the queue(3) manual page.
  50672. + */
  50673. +
  50674. +/*
  50675. + * Double-linked List.
  50676. + */
  50677. +
  50678. +typedef struct dwc_list_link {
  50679. + struct dwc_list_link *next;
  50680. + struct dwc_list_link *prev;
  50681. +} dwc_list_link_t;
  50682. +
  50683. +#define DWC_LIST_INIT(link) do { \
  50684. + (link)->next = (link); \
  50685. + (link)->prev = (link); \
  50686. +} while (0)
  50687. +
  50688. +#define DWC_LIST_FIRST(link) ((link)->next)
  50689. +#define DWC_LIST_LAST(link) ((link)->prev)
  50690. +#define DWC_LIST_END(link) (link)
  50691. +#define DWC_LIST_NEXT(link) ((link)->next)
  50692. +#define DWC_LIST_PREV(link) ((link)->prev)
  50693. +#define DWC_LIST_EMPTY(link) \
  50694. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  50695. +#define DWC_LIST_ENTRY(link, type, field) \
  50696. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  50697. +
  50698. +#if 0
  50699. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  50700. + (link)->next = (list)->next; \
  50701. + (link)->prev = (list); \
  50702. + (list)->next->prev = (link); \
  50703. + (list)->next = (link); \
  50704. +} while (0)
  50705. +
  50706. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  50707. + (link)->next = (list); \
  50708. + (link)->prev = (list)->prev; \
  50709. + (list)->prev->next = (link); \
  50710. + (list)->prev = (link); \
  50711. +} while (0)
  50712. +#else
  50713. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  50714. + dwc_list_link_t *__next__ = (list)->next; \
  50715. + __next__->prev = (link); \
  50716. + (link)->next = __next__; \
  50717. + (link)->prev = (list); \
  50718. + (list)->next = (link); \
  50719. +} while (0)
  50720. +
  50721. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  50722. + dwc_list_link_t *__prev__ = (list)->prev; \
  50723. + (list)->prev = (link); \
  50724. + (link)->next = (list); \
  50725. + (link)->prev = __prev__; \
  50726. + __prev__->next = (link); \
  50727. +} while (0)
  50728. +#endif
  50729. +
  50730. +#if 0
  50731. +static inline void __list_add(struct list_head *new,
  50732. + struct list_head *prev,
  50733. + struct list_head *next)
  50734. +{
  50735. + next->prev = new;
  50736. + new->next = next;
  50737. + new->prev = prev;
  50738. + prev->next = new;
  50739. +}
  50740. +
  50741. +static inline void list_add(struct list_head *new, struct list_head *head)
  50742. +{
  50743. + __list_add(new, head, head->next);
  50744. +}
  50745. +
  50746. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  50747. +{
  50748. + __list_add(new, head->prev, head);
  50749. +}
  50750. +
  50751. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  50752. +{
  50753. + next->prev = prev;
  50754. + prev->next = next;
  50755. +}
  50756. +
  50757. +static inline void list_del(struct list_head *entry)
  50758. +{
  50759. + __list_del(entry->prev, entry->next);
  50760. + entry->next = LIST_POISON1;
  50761. + entry->prev = LIST_POISON2;
  50762. +}
  50763. +#endif
  50764. +
  50765. +#define DWC_LIST_REMOVE(link) do { \
  50766. + (link)->next->prev = (link)->prev; \
  50767. + (link)->prev->next = (link)->next; \
  50768. +} while (0)
  50769. +
  50770. +#define DWC_LIST_REMOVE_INIT(link) do { \
  50771. + DWC_LIST_REMOVE(link); \
  50772. + DWC_LIST_INIT(link); \
  50773. +} while (0)
  50774. +
  50775. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  50776. + DWC_LIST_REMOVE(link); \
  50777. + DWC_LIST_INSERT_HEAD(list, link); \
  50778. +} while (0)
  50779. +
  50780. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  50781. + DWC_LIST_REMOVE(link); \
  50782. + DWC_LIST_INSERT_TAIL(list, link); \
  50783. +} while (0)
  50784. +
  50785. +#define DWC_LIST_FOREACH(var, list) \
  50786. + for((var) = DWC_LIST_FIRST(list); \
  50787. + (var) != DWC_LIST_END(list); \
  50788. + (var) = DWC_LIST_NEXT(var))
  50789. +
  50790. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  50791. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  50792. + (var) != DWC_LIST_END(list); \
  50793. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  50794. +
  50795. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  50796. + for((var) = DWC_LIST_LAST(list); \
  50797. + (var) != DWC_LIST_END(list); \
  50798. + (var) = DWC_LIST_PREV(var))
  50799. +
  50800. +/*
  50801. + * Singly-linked List definitions.
  50802. + */
  50803. +#define DWC_SLIST_HEAD(name, type) \
  50804. +struct name { \
  50805. + struct type *slh_first; /* first element */ \
  50806. +}
  50807. +
  50808. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  50809. + { NULL }
  50810. +
  50811. +#define DWC_SLIST_ENTRY(type) \
  50812. +struct { \
  50813. + struct type *sle_next; /* next element */ \
  50814. +}
  50815. +
  50816. +/*
  50817. + * Singly-linked List access methods.
  50818. + */
  50819. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  50820. +#define DWC_SLIST_END(head) NULL
  50821. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  50822. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  50823. +
  50824. +#define DWC_SLIST_FOREACH(var, head, field) \
  50825. + for((var) = SLIST_FIRST(head); \
  50826. + (var) != SLIST_END(head); \
  50827. + (var) = SLIST_NEXT(var, field))
  50828. +
  50829. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  50830. + for((varp) = &SLIST_FIRST((head)); \
  50831. + ((var) = *(varp)) != SLIST_END(head); \
  50832. + (varp) = &SLIST_NEXT((var), field))
  50833. +
  50834. +/*
  50835. + * Singly-linked List functions.
  50836. + */
  50837. +#define DWC_SLIST_INIT(head) { \
  50838. + SLIST_FIRST(head) = SLIST_END(head); \
  50839. +}
  50840. +
  50841. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  50842. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  50843. + (slistelm)->field.sle_next = (elm); \
  50844. +} while (0)
  50845. +
  50846. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  50847. + (elm)->field.sle_next = (head)->slh_first; \
  50848. + (head)->slh_first = (elm); \
  50849. +} while (0)
  50850. +
  50851. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  50852. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  50853. +} while (0)
  50854. +
  50855. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  50856. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  50857. +} while (0)
  50858. +
  50859. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  50860. + if ((head)->slh_first == (elm)) { \
  50861. + SLIST_REMOVE_HEAD((head), field); \
  50862. + } \
  50863. + else { \
  50864. + struct type *curelm = (head)->slh_first; \
  50865. + while( curelm->field.sle_next != (elm) ) \
  50866. + curelm = curelm->field.sle_next; \
  50867. + curelm->field.sle_next = \
  50868. + curelm->field.sle_next->field.sle_next; \
  50869. + } \
  50870. +} while (0)
  50871. +
  50872. +/*
  50873. + * Simple queue definitions.
  50874. + */
  50875. +#define DWC_SIMPLEQ_HEAD(name, type) \
  50876. +struct name { \
  50877. + struct type *sqh_first; /* first element */ \
  50878. + struct type **sqh_last; /* addr of last next element */ \
  50879. +}
  50880. +
  50881. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  50882. + { NULL, &(head).sqh_first }
  50883. +
  50884. +#define DWC_SIMPLEQ_ENTRY(type) \
  50885. +struct { \
  50886. + struct type *sqe_next; /* next element */ \
  50887. +}
  50888. +
  50889. +/*
  50890. + * Simple queue access methods.
  50891. + */
  50892. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  50893. +#define DWC_SIMPLEQ_END(head) NULL
  50894. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  50895. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  50896. +
  50897. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  50898. + for((var) = SIMPLEQ_FIRST(head); \
  50899. + (var) != SIMPLEQ_END(head); \
  50900. + (var) = SIMPLEQ_NEXT(var, field))
  50901. +
  50902. +/*
  50903. + * Simple queue functions.
  50904. + */
  50905. +#define DWC_SIMPLEQ_INIT(head) do { \
  50906. + (head)->sqh_first = NULL; \
  50907. + (head)->sqh_last = &(head)->sqh_first; \
  50908. +} while (0)
  50909. +
  50910. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  50911. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  50912. + (head)->sqh_last = &(elm)->field.sqe_next; \
  50913. + (head)->sqh_first = (elm); \
  50914. +} while (0)
  50915. +
  50916. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  50917. + (elm)->field.sqe_next = NULL; \
  50918. + *(head)->sqh_last = (elm); \
  50919. + (head)->sqh_last = &(elm)->field.sqe_next; \
  50920. +} while (0)
  50921. +
  50922. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  50923. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  50924. + (head)->sqh_last = &(elm)->field.sqe_next; \
  50925. + (listelm)->field.sqe_next = (elm); \
  50926. +} while (0)
  50927. +
  50928. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  50929. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  50930. + (head)->sqh_last = &(head)->sqh_first; \
  50931. +} while (0)
  50932. +
  50933. +/*
  50934. + * Tail queue definitions.
  50935. + */
  50936. +#define DWC_TAILQ_HEAD(name, type) \
  50937. +struct name { \
  50938. + struct type *tqh_first; /* first element */ \
  50939. + struct type **tqh_last; /* addr of last next element */ \
  50940. +}
  50941. +
  50942. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  50943. + { NULL, &(head).tqh_first }
  50944. +
  50945. +#define DWC_TAILQ_ENTRY(type) \
  50946. +struct { \
  50947. + struct type *tqe_next; /* next element */ \
  50948. + struct type **tqe_prev; /* address of previous next element */ \
  50949. +}
  50950. +
  50951. +/*
  50952. + * tail queue access methods
  50953. + */
  50954. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  50955. +#define DWC_TAILQ_END(head) NULL
  50956. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  50957. +#define DWC_TAILQ_LAST(head, headname) \
  50958. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  50959. +/* XXX */
  50960. +#define DWC_TAILQ_PREV(elm, headname, field) \
  50961. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  50962. +#define DWC_TAILQ_EMPTY(head) \
  50963. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  50964. +
  50965. +#define DWC_TAILQ_FOREACH(var, head, field) \
  50966. + for ((var) = DWC_TAILQ_FIRST(head); \
  50967. + (var) != DWC_TAILQ_END(head); \
  50968. + (var) = DWC_TAILQ_NEXT(var, field))
  50969. +
  50970. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  50971. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  50972. + (var) != DWC_TAILQ_END(head); \
  50973. + (var) = DWC_TAILQ_PREV(var, headname, field))
  50974. +
  50975. +/*
  50976. + * Tail queue functions.
  50977. + */
  50978. +#define DWC_TAILQ_INIT(head) do { \
  50979. + (head)->tqh_first = NULL; \
  50980. + (head)->tqh_last = &(head)->tqh_first; \
  50981. +} while (0)
  50982. +
  50983. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  50984. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  50985. + (head)->tqh_first->field.tqe_prev = \
  50986. + &(elm)->field.tqe_next; \
  50987. + else \
  50988. + (head)->tqh_last = &(elm)->field.tqe_next; \
  50989. + (head)->tqh_first = (elm); \
  50990. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  50991. +} while (0)
  50992. +
  50993. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  50994. + (elm)->field.tqe_next = NULL; \
  50995. + (elm)->field.tqe_prev = (head)->tqh_last; \
  50996. + *(head)->tqh_last = (elm); \
  50997. + (head)->tqh_last = &(elm)->field.tqe_next; \
  50998. +} while (0)
  50999. +
  51000. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  51001. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  51002. + (elm)->field.tqe_next->field.tqe_prev = \
  51003. + &(elm)->field.tqe_next; \
  51004. + else \
  51005. + (head)->tqh_last = &(elm)->field.tqe_next; \
  51006. + (listelm)->field.tqe_next = (elm); \
  51007. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  51008. +} while (0)
  51009. +
  51010. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  51011. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  51012. + (elm)->field.tqe_next = (listelm); \
  51013. + *(listelm)->field.tqe_prev = (elm); \
  51014. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  51015. +} while (0)
  51016. +
  51017. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  51018. + if (((elm)->field.tqe_next) != NULL) \
  51019. + (elm)->field.tqe_next->field.tqe_prev = \
  51020. + (elm)->field.tqe_prev; \
  51021. + else \
  51022. + (head)->tqh_last = (elm)->field.tqe_prev; \
  51023. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  51024. +} while (0)
  51025. +
  51026. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  51027. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  51028. + (elm2)->field.tqe_next->field.tqe_prev = \
  51029. + &(elm2)->field.tqe_next; \
  51030. + else \
  51031. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  51032. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  51033. + *(elm2)->field.tqe_prev = (elm2); \
  51034. +} while (0)
  51035. +
  51036. +/*
  51037. + * Circular queue definitions.
  51038. + */
  51039. +#define DWC_CIRCLEQ_HEAD(name, type) \
  51040. +struct name { \
  51041. + struct type *cqh_first; /* first element */ \
  51042. + struct type *cqh_last; /* last element */ \
  51043. +}
  51044. +
  51045. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  51046. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  51047. +
  51048. +#define DWC_CIRCLEQ_ENTRY(type) \
  51049. +struct { \
  51050. + struct type *cqe_next; /* next element */ \
  51051. + struct type *cqe_prev; /* previous element */ \
  51052. +}
  51053. +
  51054. +/*
  51055. + * Circular queue access methods
  51056. + */
  51057. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  51058. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  51059. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  51060. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  51061. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  51062. +#define DWC_CIRCLEQ_EMPTY(head) \
  51063. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  51064. +
  51065. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  51066. +
  51067. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  51068. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  51069. + (var) != DWC_CIRCLEQ_END(head); \
  51070. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  51071. +
  51072. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  51073. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  51074. + (var) != DWC_CIRCLEQ_END(head); \
  51075. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  51076. +
  51077. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  51078. + for((var) = DWC_CIRCLEQ_LAST(head); \
  51079. + (var) != DWC_CIRCLEQ_END(head); \
  51080. + (var) = DWC_CIRCLEQ_PREV(var, field))
  51081. +
  51082. +/*
  51083. + * Circular queue functions.
  51084. + */
  51085. +#define DWC_CIRCLEQ_INIT(head) do { \
  51086. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  51087. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  51088. +} while (0)
  51089. +
  51090. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  51091. + (elm)->field.cqe_next = NULL; \
  51092. + (elm)->field.cqe_prev = NULL; \
  51093. +} while (0)
  51094. +
  51095. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  51096. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  51097. + (elm)->field.cqe_prev = (listelm); \
  51098. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  51099. + (head)->cqh_last = (elm); \
  51100. + else \
  51101. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  51102. + (listelm)->field.cqe_next = (elm); \
  51103. +} while (0)
  51104. +
  51105. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  51106. + (elm)->field.cqe_next = (listelm); \
  51107. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  51108. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  51109. + (head)->cqh_first = (elm); \
  51110. + else \
  51111. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  51112. + (listelm)->field.cqe_prev = (elm); \
  51113. +} while (0)
  51114. +
  51115. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  51116. + (elm)->field.cqe_next = (head)->cqh_first; \
  51117. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  51118. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  51119. + (head)->cqh_last = (elm); \
  51120. + else \
  51121. + (head)->cqh_first->field.cqe_prev = (elm); \
  51122. + (head)->cqh_first = (elm); \
  51123. +} while (0)
  51124. +
  51125. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  51126. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  51127. + (elm)->field.cqe_prev = (head)->cqh_last; \
  51128. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  51129. + (head)->cqh_first = (elm); \
  51130. + else \
  51131. + (head)->cqh_last->field.cqe_next = (elm); \
  51132. + (head)->cqh_last = (elm); \
  51133. +} while (0)
  51134. +
  51135. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  51136. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  51137. + (head)->cqh_last = (elm)->field.cqe_prev; \
  51138. + else \
  51139. + (elm)->field.cqe_next->field.cqe_prev = \
  51140. + (elm)->field.cqe_prev; \
  51141. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  51142. + (head)->cqh_first = (elm)->field.cqe_next; \
  51143. + else \
  51144. + (elm)->field.cqe_prev->field.cqe_next = \
  51145. + (elm)->field.cqe_next; \
  51146. +} while (0)
  51147. +
  51148. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  51149. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  51150. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  51151. +} while (0)
  51152. +
  51153. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  51154. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  51155. + DWC_CIRCLEQ_END(head)) \
  51156. + (head).cqh_last = (elm2); \
  51157. + else \
  51158. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  51159. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  51160. + DWC_CIRCLEQ_END(head)) \
  51161. + (head).cqh_first = (elm2); \
  51162. + else \
  51163. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  51164. +} while (0)
  51165. +
  51166. +#ifdef __cplusplus
  51167. +}
  51168. +#endif
  51169. +
  51170. +#endif /* _DWC_LIST_H_ */
  51171. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_mem.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c
  51172. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_mem.c 1969-12-31 18:00:00.000000000 -0600
  51173. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c 2014-12-11 14:02:55.352418001 -0600
  51174. @@ -0,0 +1,245 @@
  51175. +/* Memory Debugging */
  51176. +#ifdef DWC_DEBUG_MEMORY
  51177. +
  51178. +#include "dwc_os.h"
  51179. +#include "dwc_list.h"
  51180. +
  51181. +struct allocation {
  51182. + void *addr;
  51183. + void *ctx;
  51184. + char *func;
  51185. + int line;
  51186. + uint32_t size;
  51187. + int dma;
  51188. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  51189. +};
  51190. +
  51191. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  51192. +
  51193. +struct allocation_manager {
  51194. + void *mem_ctx;
  51195. + struct allocation_queue allocations;
  51196. +
  51197. + /* statistics */
  51198. + int num;
  51199. + int num_freed;
  51200. + int num_active;
  51201. + uint32_t total;
  51202. + uint32_t cur;
  51203. + uint32_t max;
  51204. +};
  51205. +
  51206. +static struct allocation_manager *manager = NULL;
  51207. +
  51208. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  51209. + int dma)
  51210. +{
  51211. + struct allocation *a;
  51212. +
  51213. + DWC_ASSERT(manager != NULL, "manager not allocated");
  51214. +
  51215. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  51216. + if (!a) {
  51217. + return -DWC_E_NO_MEMORY;
  51218. + }
  51219. +
  51220. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  51221. + if (!a->func) {
  51222. + __DWC_FREE(manager->mem_ctx, a);
  51223. + return -DWC_E_NO_MEMORY;
  51224. + }
  51225. +
  51226. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  51227. + a->addr = addr;
  51228. + a->ctx = ctx;
  51229. + a->line = line;
  51230. + a->size = size;
  51231. + a->dma = dma;
  51232. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  51233. +
  51234. + /* Update stats */
  51235. + manager->num++;
  51236. + manager->num_active++;
  51237. + manager->total += size;
  51238. + manager->cur += size;
  51239. +
  51240. + if (manager->max < manager->cur) {
  51241. + manager->max = manager->cur;
  51242. + }
  51243. +
  51244. + return 0;
  51245. +}
  51246. +
  51247. +static struct allocation *find_allocation(void *ctx, void *addr)
  51248. +{
  51249. + struct allocation *a;
  51250. +
  51251. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  51252. + if (a->ctx == ctx && a->addr == addr) {
  51253. + return a;
  51254. + }
  51255. + }
  51256. +
  51257. + return NULL;
  51258. +}
  51259. +
  51260. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  51261. +{
  51262. + struct allocation *a = find_allocation(ctx, addr);
  51263. +
  51264. + if (!a) {
  51265. + DWC_ASSERT(0,
  51266. + "Free of address %p that was never allocated or already freed %s:%d",
  51267. + addr, func, line);
  51268. + return;
  51269. + }
  51270. +
  51271. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  51272. +
  51273. + manager->num_active--;
  51274. + manager->num_freed++;
  51275. + manager->cur -= a->size;
  51276. + __DWC_FREE(manager->mem_ctx, a->func);
  51277. + __DWC_FREE(manager->mem_ctx, a);
  51278. +}
  51279. +
  51280. +int dwc_memory_debug_start(void *mem_ctx)
  51281. +{
  51282. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  51283. +
  51284. + if (manager) {
  51285. + return -DWC_E_BUSY;
  51286. + }
  51287. +
  51288. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  51289. + if (!manager) {
  51290. + return -DWC_E_NO_MEMORY;
  51291. + }
  51292. +
  51293. + DWC_CIRCLEQ_INIT(&manager->allocations);
  51294. + manager->mem_ctx = mem_ctx;
  51295. + manager->num = 0;
  51296. + manager->num_freed = 0;
  51297. + manager->num_active = 0;
  51298. + manager->total = 0;
  51299. + manager->cur = 0;
  51300. + manager->max = 0;
  51301. +
  51302. + return 0;
  51303. +}
  51304. +
  51305. +void dwc_memory_debug_stop(void)
  51306. +{
  51307. + struct allocation *a;
  51308. +
  51309. + dwc_memory_debug_report();
  51310. +
  51311. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  51312. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  51313. + free_allocation(a->ctx, a->addr, NULL, -1);
  51314. + }
  51315. +
  51316. + __DWC_FREE(manager->mem_ctx, manager);
  51317. +}
  51318. +
  51319. +void dwc_memory_debug_report(void)
  51320. +{
  51321. + struct allocation *a;
  51322. +
  51323. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  51324. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  51325. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  51326. + DWC_PRINTF("Active = %d\n", manager->num_active);
  51327. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  51328. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  51329. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  51330. + DWC_PRINTF("Unfreed allocations:\n");
  51331. +
  51332. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  51333. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  51334. + a->addr, a->size, a->func, a->line, a->dma);
  51335. + }
  51336. +}
  51337. +
  51338. +/* The replacement functions */
  51339. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  51340. +{
  51341. + void *addr = __DWC_ALLOC(mem_ctx, size);
  51342. +
  51343. + if (!addr) {
  51344. + return NULL;
  51345. + }
  51346. +
  51347. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  51348. + __DWC_FREE(mem_ctx, addr);
  51349. + return NULL;
  51350. + }
  51351. +
  51352. + return addr;
  51353. +}
  51354. +
  51355. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  51356. + int line)
  51357. +{
  51358. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  51359. +
  51360. + if (!addr) {
  51361. + return NULL;
  51362. + }
  51363. +
  51364. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  51365. + __DWC_FREE(mem_ctx, addr);
  51366. + return NULL;
  51367. + }
  51368. +
  51369. + return addr;
  51370. +}
  51371. +
  51372. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  51373. +{
  51374. + free_allocation(mem_ctx, addr, func, line);
  51375. + __DWC_FREE(mem_ctx, addr);
  51376. +}
  51377. +
  51378. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  51379. + char const *func, int line)
  51380. +{
  51381. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  51382. +
  51383. + if (!addr) {
  51384. + return NULL;
  51385. + }
  51386. +
  51387. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  51388. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  51389. + return NULL;
  51390. + }
  51391. +
  51392. + return addr;
  51393. +}
  51394. +
  51395. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  51396. + dwc_dma_t *dma_addr, char const *func, int line)
  51397. +{
  51398. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  51399. +
  51400. + if (!addr) {
  51401. + return NULL;
  51402. + }
  51403. +
  51404. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  51405. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  51406. + return NULL;
  51407. + }
  51408. +
  51409. + return addr;
  51410. +}
  51411. +
  51412. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  51413. + dwc_dma_t dma_addr, char const *func, int line)
  51414. +{
  51415. + free_allocation(dma_ctx, virt_addr, func, line);
  51416. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  51417. +}
  51418. +
  51419. +#endif /* DWC_DEBUG_MEMORY */
  51420. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_modpow.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c
  51421. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_modpow.c 1969-12-31 18:00:00.000000000 -0600
  51422. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c 2014-12-11 14:02:55.356418001 -0600
  51423. @@ -0,0 +1,636 @@
  51424. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  51425. + *
  51426. + * PuTTY is copyright 1997-2007 Simon Tatham.
  51427. + *
  51428. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  51429. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  51430. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  51431. + * Kuhn, and CORE SDI S.A.
  51432. + *
  51433. + * Permission is hereby granted, free of charge, to any person
  51434. + * obtaining a copy of this software and associated documentation files
  51435. + * (the "Software"), to deal in the Software without restriction,
  51436. + * including without limitation the rights to use, copy, modify, merge,
  51437. + * publish, distribute, sublicense, and/or sell copies of the Software,
  51438. + * and to permit persons to whom the Software is furnished to do so,
  51439. + * subject to the following conditions:
  51440. + *
  51441. + * The above copyright notice and this permission notice shall be
  51442. + * included in all copies or substantial portions of the Software.
  51443. +
  51444. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  51445. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  51446. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  51447. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  51448. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  51449. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  51450. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  51451. + *
  51452. + */
  51453. +#ifdef DWC_CRYPTOLIB
  51454. +
  51455. +#ifndef CONFIG_MACH_IPMATE
  51456. +
  51457. +#include "dwc_modpow.h"
  51458. +
  51459. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  51460. +#define BIGNUM_TOP_BIT 0x80000000UL
  51461. +#define BIGNUM_INT_BITS 32
  51462. +
  51463. +
  51464. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  51465. +{
  51466. + void *p;
  51467. + size *= n;
  51468. + if (size == 0) size = 1;
  51469. + p = dwc_alloc(mem_ctx, size);
  51470. + return p;
  51471. +}
  51472. +
  51473. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  51474. +#define sfree dwc_free
  51475. +
  51476. +/*
  51477. + * Usage notes:
  51478. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  51479. + * subscripts, as some implementations object to this (see below).
  51480. + * * Note that none of the division methods below will cope if the
  51481. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  51482. + * to avoid this case.
  51483. + * If this condition occurs, in the case of the x86 DIV instruction,
  51484. + * an overflow exception will occur, which (according to a correspondent)
  51485. + * will manifest on Windows as something like
  51486. + * 0xC0000095: Integer overflow
  51487. + * The C variant won't give the right answer, either.
  51488. + */
  51489. +
  51490. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  51491. +
  51492. +#if defined __GNUC__ && defined __i386__
  51493. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  51494. + __asm__("div %2" : \
  51495. + "=d" (r), "=a" (q) : \
  51496. + "r" (w), "d" (hi), "a" (lo))
  51497. +#else
  51498. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  51499. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  51500. + q = n / w; \
  51501. + r = n % w; \
  51502. +} while (0)
  51503. +#endif
  51504. +
  51505. +// q = n / w;
  51506. +// r = n % w;
  51507. +
  51508. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  51509. +
  51510. +#define BIGNUM_INTERNAL
  51511. +
  51512. +static Bignum newbn(void *mem_ctx, int length)
  51513. +{
  51514. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  51515. + //if (!b)
  51516. + //abort(); /* FIXME */
  51517. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  51518. + b[0] = length;
  51519. + return b;
  51520. +}
  51521. +
  51522. +void freebn(void *mem_ctx, Bignum b)
  51523. +{
  51524. + /*
  51525. + * Burn the evidence, just in case.
  51526. + */
  51527. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  51528. + sfree(mem_ctx, b);
  51529. +}
  51530. +
  51531. +/*
  51532. + * Compute c = a * b.
  51533. + * Input is in the first len words of a and b.
  51534. + * Result is returned in the first 2*len words of c.
  51535. + */
  51536. +static void internal_mul(BignumInt *a, BignumInt *b,
  51537. + BignumInt *c, int len)
  51538. +{
  51539. + int i, j;
  51540. + BignumDblInt t;
  51541. +
  51542. + for (j = 0; j < 2 * len; j++)
  51543. + c[j] = 0;
  51544. +
  51545. + for (i = len - 1; i >= 0; i--) {
  51546. + t = 0;
  51547. + for (j = len - 1; j >= 0; j--) {
  51548. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  51549. + t += (BignumDblInt) c[i + j + 1];
  51550. + c[i + j + 1] = (BignumInt) t;
  51551. + t = t >> BIGNUM_INT_BITS;
  51552. + }
  51553. + c[i] = (BignumInt) t;
  51554. + }
  51555. +}
  51556. +
  51557. +static void internal_add_shifted(BignumInt *number,
  51558. + unsigned n, int shift)
  51559. +{
  51560. + int word = 1 + (shift / BIGNUM_INT_BITS);
  51561. + int bshift = shift % BIGNUM_INT_BITS;
  51562. + BignumDblInt addend;
  51563. +
  51564. + addend = (BignumDblInt)n << bshift;
  51565. +
  51566. + while (addend) {
  51567. + addend += number[word];
  51568. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  51569. + addend >>= BIGNUM_INT_BITS;
  51570. + word++;
  51571. + }
  51572. +}
  51573. +
  51574. +/*
  51575. + * Compute a = a % m.
  51576. + * Input in first alen words of a and first mlen words of m.
  51577. + * Output in first alen words of a
  51578. + * (of which first alen-mlen words will be zero).
  51579. + * The MSW of m MUST have its high bit set.
  51580. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  51581. + * rather than the internal bigendian format. Quotient parts are shifted
  51582. + * left by `qshift' before adding into quot.
  51583. + */
  51584. +static void internal_mod(BignumInt *a, int alen,
  51585. + BignumInt *m, int mlen,
  51586. + BignumInt *quot, int qshift)
  51587. +{
  51588. + BignumInt m0, m1;
  51589. + unsigned int h;
  51590. + int i, k;
  51591. +
  51592. + m0 = m[0];
  51593. + if (mlen > 1)
  51594. + m1 = m[1];
  51595. + else
  51596. + m1 = 0;
  51597. +
  51598. + for (i = 0; i <= alen - mlen; i++) {
  51599. + BignumDblInt t;
  51600. + unsigned int q, r, c, ai1;
  51601. +
  51602. + if (i == 0) {
  51603. + h = 0;
  51604. + } else {
  51605. + h = a[i - 1];
  51606. + a[i - 1] = 0;
  51607. + }
  51608. +
  51609. + if (i == alen - 1)
  51610. + ai1 = 0;
  51611. + else
  51612. + ai1 = a[i + 1];
  51613. +
  51614. + /* Find q = h:a[i] / m0 */
  51615. + if (h >= m0) {
  51616. + /*
  51617. + * Special case.
  51618. + *
  51619. + * To illustrate it, suppose a BignumInt is 8 bits, and
  51620. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  51621. + * our initial division will be 0xA123 / 0xA1, which
  51622. + * will give a quotient of 0x100 and a divide overflow.
  51623. + * However, the invariants in this division algorithm
  51624. + * are not violated, since the full number A1:23:... is
  51625. + * _less_ than the quotient prefix A1:B2:... and so the
  51626. + * following correction loop would have sorted it out.
  51627. + *
  51628. + * In this situation we set q to be the largest
  51629. + * quotient we _can_ stomach (0xFF, of course).
  51630. + */
  51631. + q = BIGNUM_INT_MASK;
  51632. + } else {
  51633. + /* Macro doesn't want an array subscript expression passed
  51634. + * into it (see definition), so use a temporary. */
  51635. + BignumInt tmplo = a[i];
  51636. + DIVMOD_WORD(q, r, h, tmplo, m0);
  51637. +
  51638. + /* Refine our estimate of q by looking at
  51639. + h:a[i]:a[i+1] / m0:m1 */
  51640. + t = MUL_WORD(m1, q);
  51641. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  51642. + q--;
  51643. + t -= m1;
  51644. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  51645. + if (r >= (BignumDblInt) m0 &&
  51646. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  51647. + }
  51648. + }
  51649. +
  51650. + /* Subtract q * m from a[i...] */
  51651. + c = 0;
  51652. + for (k = mlen - 1; k >= 0; k--) {
  51653. + t = MUL_WORD(q, m[k]);
  51654. + t += c;
  51655. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  51656. + if ((BignumInt) t > a[i + k])
  51657. + c++;
  51658. + a[i + k] -= (BignumInt) t;
  51659. + }
  51660. +
  51661. + /* Add back m in case of borrow */
  51662. + if (c != h) {
  51663. + t = 0;
  51664. + for (k = mlen - 1; k >= 0; k--) {
  51665. + t += m[k];
  51666. + t += a[i + k];
  51667. + a[i + k] = (BignumInt) t;
  51668. + t = t >> BIGNUM_INT_BITS;
  51669. + }
  51670. + q--;
  51671. + }
  51672. + if (quot)
  51673. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  51674. + }
  51675. +}
  51676. +
  51677. +/*
  51678. + * Compute p % mod.
  51679. + * The most significant word of mod MUST be non-zero.
  51680. + * We assume that the result array is the same size as the mod array.
  51681. + * We optionally write out a quotient if `quotient' is non-NULL.
  51682. + * We can avoid writing out the result if `result' is NULL.
  51683. + */
  51684. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  51685. +{
  51686. + BignumInt *n, *m;
  51687. + int mshift;
  51688. + int plen, mlen, i, j;
  51689. +
  51690. + /* Allocate m of size mlen, copy mod to m */
  51691. + /* We use big endian internally */
  51692. + mlen = mod[0];
  51693. + m = snewn(mem_ctx, mlen, BignumInt);
  51694. + //if (!m)
  51695. + //abort(); /* FIXME */
  51696. + for (j = 0; j < mlen; j++)
  51697. + m[j] = mod[mod[0] - j];
  51698. +
  51699. + /* Shift m left to make msb bit set */
  51700. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  51701. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  51702. + break;
  51703. + if (mshift) {
  51704. + for (i = 0; i < mlen - 1; i++)
  51705. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  51706. + m[mlen - 1] = m[mlen - 1] << mshift;
  51707. + }
  51708. +
  51709. + plen = p[0];
  51710. + /* Ensure plen > mlen */
  51711. + if (plen <= mlen)
  51712. + plen = mlen + 1;
  51713. +
  51714. + /* Allocate n of size plen, copy p to n */
  51715. + n = snewn(mem_ctx, plen, BignumInt);
  51716. + //if (!n)
  51717. + //abort(); /* FIXME */
  51718. + for (j = 0; j < plen; j++)
  51719. + n[j] = 0;
  51720. + for (j = 1; j <= (int)p[0]; j++)
  51721. + n[plen - j] = p[j];
  51722. +
  51723. + /* Main computation */
  51724. + internal_mod(n, plen, m, mlen, quotient, mshift);
  51725. +
  51726. + /* Fixup result in case the modulus was shifted */
  51727. + if (mshift) {
  51728. + for (i = plen - mlen - 1; i < plen - 1; i++)
  51729. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  51730. + n[plen - 1] = n[plen - 1] << mshift;
  51731. + internal_mod(n, plen, m, mlen, quotient, 0);
  51732. + for (i = plen - 1; i >= plen - mlen; i--)
  51733. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  51734. + }
  51735. +
  51736. + /* Copy result to buffer */
  51737. + if (result) {
  51738. + for (i = 1; i <= (int)result[0]; i++) {
  51739. + int j = plen - i;
  51740. + result[i] = j >= 0 ? n[j] : 0;
  51741. + }
  51742. + }
  51743. +
  51744. + /* Free temporary arrays */
  51745. + for (i = 0; i < mlen; i++)
  51746. + m[i] = 0;
  51747. + sfree(mem_ctx, m);
  51748. + for (i = 0; i < plen; i++)
  51749. + n[i] = 0;
  51750. + sfree(mem_ctx, n);
  51751. +}
  51752. +
  51753. +/*
  51754. + * Simple remainder.
  51755. + */
  51756. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  51757. +{
  51758. + Bignum r = newbn(mem_ctx, b[0]);
  51759. + bigdivmod(mem_ctx, a, b, r, NULL);
  51760. + return r;
  51761. +}
  51762. +
  51763. +/*
  51764. + * Compute (base ^ exp) % mod.
  51765. + */
  51766. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  51767. +{
  51768. + BignumInt *a, *b, *n, *m;
  51769. + int mshift;
  51770. + int mlen, i, j;
  51771. + Bignum base, result;
  51772. +
  51773. + /*
  51774. + * The most significant word of mod needs to be non-zero. It
  51775. + * should already be, but let's make sure.
  51776. + */
  51777. + //assert(mod[mod[0]] != 0);
  51778. +
  51779. + /*
  51780. + * Make sure the base is smaller than the modulus, by reducing
  51781. + * it modulo the modulus if not.
  51782. + */
  51783. + base = bigmod(mem_ctx, base_in, mod);
  51784. +
  51785. + /* Allocate m of size mlen, copy mod to m */
  51786. + /* We use big endian internally */
  51787. + mlen = mod[0];
  51788. + m = snewn(mem_ctx, mlen, BignumInt);
  51789. + //if (!m)
  51790. + //abort(); /* FIXME */
  51791. + for (j = 0; j < mlen; j++)
  51792. + m[j] = mod[mod[0] - j];
  51793. +
  51794. + /* Shift m left to make msb bit set */
  51795. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  51796. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  51797. + break;
  51798. + if (mshift) {
  51799. + for (i = 0; i < mlen - 1; i++)
  51800. + m[i] =
  51801. + (m[i] << mshift) | (m[i + 1] >>
  51802. + (BIGNUM_INT_BITS - mshift));
  51803. + m[mlen - 1] = m[mlen - 1] << mshift;
  51804. + }
  51805. +
  51806. + /* Allocate n of size mlen, copy base to n */
  51807. + n = snewn(mem_ctx, mlen, BignumInt);
  51808. + //if (!n)
  51809. + //abort(); /* FIXME */
  51810. + i = mlen - base[0];
  51811. + for (j = 0; j < i; j++)
  51812. + n[j] = 0;
  51813. + for (j = 0; j < base[0]; j++)
  51814. + n[i + j] = base[base[0] - j];
  51815. +
  51816. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  51817. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  51818. + //if (!a)
  51819. + //abort(); /* FIXME */
  51820. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  51821. + //if (!b)
  51822. + //abort(); /* FIXME */
  51823. + for (i = 0; i < 2 * mlen; i++)
  51824. + a[i] = 0;
  51825. + a[2 * mlen - 1] = 1;
  51826. +
  51827. + /* Skip leading zero bits of exp. */
  51828. + i = 0;
  51829. + j = BIGNUM_INT_BITS - 1;
  51830. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  51831. + j--;
  51832. + if (j < 0) {
  51833. + i++;
  51834. + j = BIGNUM_INT_BITS - 1;
  51835. + }
  51836. + }
  51837. +
  51838. + /* Main computation */
  51839. + while (i < exp[0]) {
  51840. + while (j >= 0) {
  51841. + internal_mul(a + mlen, a + mlen, b, mlen);
  51842. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  51843. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  51844. + internal_mul(b + mlen, n, a, mlen);
  51845. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  51846. + } else {
  51847. + BignumInt *t;
  51848. + t = a;
  51849. + a = b;
  51850. + b = t;
  51851. + }
  51852. + j--;
  51853. + }
  51854. + i++;
  51855. + j = BIGNUM_INT_BITS - 1;
  51856. + }
  51857. +
  51858. + /* Fixup result in case the modulus was shifted */
  51859. + if (mshift) {
  51860. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  51861. + a[i] =
  51862. + (a[i] << mshift) | (a[i + 1] >>
  51863. + (BIGNUM_INT_BITS - mshift));
  51864. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  51865. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  51866. + for (i = 2 * mlen - 1; i >= mlen; i--)
  51867. + a[i] =
  51868. + (a[i] >> mshift) | (a[i - 1] <<
  51869. + (BIGNUM_INT_BITS - mshift));
  51870. + }
  51871. +
  51872. + /* Copy result to buffer */
  51873. + result = newbn(mem_ctx, mod[0]);
  51874. + for (i = 0; i < mlen; i++)
  51875. + result[result[0] - i] = a[i + mlen];
  51876. + while (result[0] > 1 && result[result[0]] == 0)
  51877. + result[0]--;
  51878. +
  51879. + /* Free temporary arrays */
  51880. + for (i = 0; i < 2 * mlen; i++)
  51881. + a[i] = 0;
  51882. + sfree(mem_ctx, a);
  51883. + for (i = 0; i < 2 * mlen; i++)
  51884. + b[i] = 0;
  51885. + sfree(mem_ctx, b);
  51886. + for (i = 0; i < mlen; i++)
  51887. + m[i] = 0;
  51888. + sfree(mem_ctx, m);
  51889. + for (i = 0; i < mlen; i++)
  51890. + n[i] = 0;
  51891. + sfree(mem_ctx, n);
  51892. +
  51893. + freebn(mem_ctx, base);
  51894. +
  51895. + return result;
  51896. +}
  51897. +
  51898. +
  51899. +#ifdef UNITTEST
  51900. +
  51901. +static __u32 dh_p[] = {
  51902. + 96,
  51903. + 0xFFFFFFFF,
  51904. + 0xFFFFFFFF,
  51905. + 0xA93AD2CA,
  51906. + 0x4B82D120,
  51907. + 0xE0FD108E,
  51908. + 0x43DB5BFC,
  51909. + 0x74E5AB31,
  51910. + 0x08E24FA0,
  51911. + 0xBAD946E2,
  51912. + 0x770988C0,
  51913. + 0x7A615D6C,
  51914. + 0xBBE11757,
  51915. + 0x177B200C,
  51916. + 0x521F2B18,
  51917. + 0x3EC86A64,
  51918. + 0xD8760273,
  51919. + 0xD98A0864,
  51920. + 0xF12FFA06,
  51921. + 0x1AD2EE6B,
  51922. + 0xCEE3D226,
  51923. + 0x4A25619D,
  51924. + 0x1E8C94E0,
  51925. + 0xDB0933D7,
  51926. + 0xABF5AE8C,
  51927. + 0xA6E1E4C7,
  51928. + 0xB3970F85,
  51929. + 0x5D060C7D,
  51930. + 0x8AEA7157,
  51931. + 0x58DBEF0A,
  51932. + 0xECFB8504,
  51933. + 0xDF1CBA64,
  51934. + 0xA85521AB,
  51935. + 0x04507A33,
  51936. + 0xAD33170D,
  51937. + 0x8AAAC42D,
  51938. + 0x15728E5A,
  51939. + 0x98FA0510,
  51940. + 0x15D22618,
  51941. + 0xEA956AE5,
  51942. + 0x3995497C,
  51943. + 0x95581718,
  51944. + 0xDE2BCBF6,
  51945. + 0x6F4C52C9,
  51946. + 0xB5C55DF0,
  51947. + 0xEC07A28F,
  51948. + 0x9B2783A2,
  51949. + 0x180E8603,
  51950. + 0xE39E772C,
  51951. + 0x2E36CE3B,
  51952. + 0x32905E46,
  51953. + 0xCA18217C,
  51954. + 0xF1746C08,
  51955. + 0x4ABC9804,
  51956. + 0x670C354E,
  51957. + 0x7096966D,
  51958. + 0x9ED52907,
  51959. + 0x208552BB,
  51960. + 0x1C62F356,
  51961. + 0xDCA3AD96,
  51962. + 0x83655D23,
  51963. + 0xFD24CF5F,
  51964. + 0x69163FA8,
  51965. + 0x1C55D39A,
  51966. + 0x98DA4836,
  51967. + 0xA163BF05,
  51968. + 0xC2007CB8,
  51969. + 0xECE45B3D,
  51970. + 0x49286651,
  51971. + 0x7C4B1FE6,
  51972. + 0xAE9F2411,
  51973. + 0x5A899FA5,
  51974. + 0xEE386BFB,
  51975. + 0xF406B7ED,
  51976. + 0x0BFF5CB6,
  51977. + 0xA637ED6B,
  51978. + 0xF44C42E9,
  51979. + 0x625E7EC6,
  51980. + 0xE485B576,
  51981. + 0x6D51C245,
  51982. + 0x4FE1356D,
  51983. + 0xF25F1437,
  51984. + 0x302B0A6D,
  51985. + 0xCD3A431B,
  51986. + 0xEF9519B3,
  51987. + 0x8E3404DD,
  51988. + 0x514A0879,
  51989. + 0x3B139B22,
  51990. + 0x020BBEA6,
  51991. + 0x8A67CC74,
  51992. + 0x29024E08,
  51993. + 0x80DC1CD1,
  51994. + 0xC4C6628B,
  51995. + 0x2168C234,
  51996. + 0xC90FDAA2,
  51997. + 0xFFFFFFFF,
  51998. + 0xFFFFFFFF,
  51999. +};
  52000. +
  52001. +static __u32 dh_a[] = {
  52002. + 8,
  52003. + 0xdf367516,
  52004. + 0x86459caa,
  52005. + 0xe2d459a4,
  52006. + 0xd910dae0,
  52007. + 0x8a8b5e37,
  52008. + 0x67ab31c6,
  52009. + 0xf0b55ea9,
  52010. + 0x440051d6,
  52011. +};
  52012. +
  52013. +static __u32 dh_b[] = {
  52014. + 8,
  52015. + 0xded92656,
  52016. + 0xe07a048a,
  52017. + 0x6fa452cd,
  52018. + 0x2df89d30,
  52019. + 0xc75f1b0f,
  52020. + 0x8ce3578f,
  52021. + 0x7980a324,
  52022. + 0x5daec786,
  52023. +};
  52024. +
  52025. +static __u32 dh_g[] = {
  52026. + 1,
  52027. + 2,
  52028. +};
  52029. +
  52030. +int main(void)
  52031. +{
  52032. + int i;
  52033. + __u32 *k;
  52034. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  52035. +
  52036. + printf("\n\n");
  52037. + for (i=0; i<k[0]; i++) {
  52038. + __u32 word32 = k[k[0] - i];
  52039. + __u16 l = word32 & 0xffff;
  52040. + __u16 m = (word32 & 0xffff0000) >> 16;
  52041. + printf("%04x %04x ", m, l);
  52042. + if (!((i + 1)%13)) printf("\n");
  52043. + }
  52044. + printf("\n\n");
  52045. +
  52046. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  52047. + printf("PASS\n\n");
  52048. + }
  52049. + else {
  52050. + printf("FAIL\n\n");
  52051. + }
  52052. +
  52053. +}
  52054. +
  52055. +#endif /* UNITTEST */
  52056. +
  52057. +#endif /* CONFIG_MACH_IPMATE */
  52058. +
  52059. +#endif /*DWC_CRYPTOLIB */
  52060. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_modpow.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h
  52061. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_modpow.h 1969-12-31 18:00:00.000000000 -0600
  52062. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h 2014-12-11 14:02:55.356418001 -0600
  52063. @@ -0,0 +1,34 @@
  52064. +/*
  52065. + * dwc_modpow.h
  52066. + * See dwc_modpow.c for license and changes
  52067. + */
  52068. +#ifndef _DWC_MODPOW_H
  52069. +#define _DWC_MODPOW_H
  52070. +
  52071. +#ifdef __cplusplus
  52072. +extern "C" {
  52073. +#endif
  52074. +
  52075. +#include "dwc_os.h"
  52076. +
  52077. +/** @file
  52078. + *
  52079. + * This file defines the module exponentiation function which is only used
  52080. + * internally by the DWC UWB modules for calculation of PKs during numeric
  52081. + * association. The routine is taken from the PUTTY, an open source terminal
  52082. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  52083. + *
  52084. + */
  52085. +
  52086. +typedef uint32_t BignumInt;
  52087. +typedef uint64_t BignumDblInt;
  52088. +typedef BignumInt *Bignum;
  52089. +
  52090. +/* Compute modular exponentiaion */
  52091. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  52092. +
  52093. +#ifdef __cplusplus
  52094. +}
  52095. +#endif
  52096. +
  52097. +#endif /* _LINUX_BIGNUM_H */
  52098. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_notifier.c linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c
  52099. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_notifier.c 1969-12-31 18:00:00.000000000 -0600
  52100. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c 2014-12-11 14:02:55.356418001 -0600
  52101. @@ -0,0 +1,319 @@
  52102. +#ifdef DWC_NOTIFYLIB
  52103. +
  52104. +#include "dwc_notifier.h"
  52105. +#include "dwc_list.h"
  52106. +
  52107. +typedef struct dwc_observer {
  52108. + void *observer;
  52109. + dwc_notifier_callback_t callback;
  52110. + void *data;
  52111. + char *notification;
  52112. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  52113. +} observer_t;
  52114. +
  52115. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  52116. +
  52117. +typedef struct dwc_notifier {
  52118. + void *mem_ctx;
  52119. + void *object;
  52120. + struct observer_queue observers;
  52121. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  52122. +} notifier_t;
  52123. +
  52124. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  52125. +
  52126. +typedef struct manager {
  52127. + void *mem_ctx;
  52128. + void *wkq_ctx;
  52129. + dwc_workq_t *wq;
  52130. +// dwc_mutex_t *mutex;
  52131. + struct notifier_queue notifiers;
  52132. +} manager_t;
  52133. +
  52134. +static manager_t *manager = NULL;
  52135. +
  52136. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  52137. +{
  52138. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  52139. + if (!manager) {
  52140. + return -DWC_E_NO_MEMORY;
  52141. + }
  52142. +
  52143. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  52144. +
  52145. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  52146. + if (!manager->wq) {
  52147. + return -DWC_E_NO_MEMORY;
  52148. + }
  52149. +
  52150. + return 0;
  52151. +}
  52152. +
  52153. +static void free_manager(void)
  52154. +{
  52155. + dwc_workq_free(manager->wq);
  52156. +
  52157. + /* All notifiers must have unregistered themselves before this module
  52158. + * can be removed. Hitting this assertion indicates a programmer
  52159. + * error. */
  52160. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  52161. + "Notification manager being freed before all notifiers have been removed");
  52162. + dwc_free(manager->mem_ctx, manager);
  52163. +}
  52164. +
  52165. +#ifdef DEBUG
  52166. +static void dump_manager(void)
  52167. +{
  52168. + notifier_t *n;
  52169. + observer_t *o;
  52170. +
  52171. + DWC_ASSERT(manager, "Notification manager not found");
  52172. +
  52173. + DWC_DEBUG("List of all notifiers and observers:\n");
  52174. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  52175. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  52176. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  52177. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  52178. + }
  52179. + }
  52180. +}
  52181. +#else
  52182. +#define dump_manager(...)
  52183. +#endif
  52184. +
  52185. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  52186. + dwc_notifier_callback_t callback, void *data)
  52187. +{
  52188. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  52189. +
  52190. + if (!new_observer) {
  52191. + return NULL;
  52192. + }
  52193. +
  52194. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  52195. + new_observer->observer = observer;
  52196. + new_observer->notification = notification;
  52197. + new_observer->callback = callback;
  52198. + new_observer->data = data;
  52199. + return new_observer;
  52200. +}
  52201. +
  52202. +static void free_observer(void *mem_ctx, observer_t *observer)
  52203. +{
  52204. + dwc_free(mem_ctx, observer);
  52205. +}
  52206. +
  52207. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  52208. +{
  52209. + notifier_t *notifier;
  52210. +
  52211. + if (!object) {
  52212. + return NULL;
  52213. + }
  52214. +
  52215. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  52216. + if (!notifier) {
  52217. + return NULL;
  52218. + }
  52219. +
  52220. + DWC_CIRCLEQ_INIT(&notifier->observers);
  52221. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  52222. +
  52223. + notifier->mem_ctx = mem_ctx;
  52224. + notifier->object = object;
  52225. + return notifier;
  52226. +}
  52227. +
  52228. +static void free_notifier(notifier_t *notifier)
  52229. +{
  52230. + observer_t *observer;
  52231. +
  52232. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  52233. + free_observer(notifier->mem_ctx, observer);
  52234. + }
  52235. +
  52236. + dwc_free(notifier->mem_ctx, notifier);
  52237. +}
  52238. +
  52239. +static notifier_t *find_notifier(void *object)
  52240. +{
  52241. + notifier_t *notifier;
  52242. +
  52243. + DWC_ASSERT(manager, "Notification manager not found");
  52244. +
  52245. + if (!object) {
  52246. + return NULL;
  52247. + }
  52248. +
  52249. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  52250. + if (notifier->object == object) {
  52251. + return notifier;
  52252. + }
  52253. + }
  52254. +
  52255. + return NULL;
  52256. +}
  52257. +
  52258. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  52259. +{
  52260. + return create_manager(mem_ctx, wkq_ctx);
  52261. +}
  52262. +
  52263. +void dwc_free_notification_manager(void)
  52264. +{
  52265. + free_manager();
  52266. +}
  52267. +
  52268. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  52269. +{
  52270. + notifier_t *notifier;
  52271. +
  52272. + DWC_ASSERT(manager, "Notification manager not found");
  52273. +
  52274. + notifier = find_notifier(object);
  52275. + if (notifier) {
  52276. + DWC_ERROR("Notifier %p is already registered\n", object);
  52277. + return NULL;
  52278. + }
  52279. +
  52280. + notifier = alloc_notifier(mem_ctx, object);
  52281. + if (!notifier) {
  52282. + return NULL;
  52283. + }
  52284. +
  52285. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  52286. +
  52287. + DWC_INFO("Notifier %p registered", object);
  52288. + dump_manager();
  52289. +
  52290. + return notifier;
  52291. +}
  52292. +
  52293. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  52294. +{
  52295. + DWC_ASSERT(manager, "Notification manager not found");
  52296. +
  52297. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  52298. + observer_t *o;
  52299. +
  52300. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  52301. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  52302. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  52303. + }
  52304. +
  52305. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  52306. + "Notifier %p has active observers when removing", notifier);
  52307. + }
  52308. +
  52309. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  52310. + free_notifier(notifier);
  52311. +
  52312. + DWC_INFO("Notifier unregistered");
  52313. + dump_manager();
  52314. +}
  52315. +
  52316. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  52317. +int dwc_add_observer(void *observer, void *object, char *notification,
  52318. + dwc_notifier_callback_t callback, void *data)
  52319. +{
  52320. + notifier_t *notifier = find_notifier(object);
  52321. + observer_t *new_observer;
  52322. +
  52323. + if (!notifier) {
  52324. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  52325. + return -DWC_E_INVALID;
  52326. + }
  52327. +
  52328. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  52329. + if (!new_observer) {
  52330. + return -DWC_E_NO_MEMORY;
  52331. + }
  52332. +
  52333. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  52334. +
  52335. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  52336. + observer, object, notification, callback, data);
  52337. +
  52338. + dump_manager();
  52339. + return 0;
  52340. +}
  52341. +
  52342. +int dwc_remove_observer(void *observer)
  52343. +{
  52344. + notifier_t *n;
  52345. +
  52346. + DWC_ASSERT(manager, "Notification manager not found");
  52347. +
  52348. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  52349. + observer_t *o;
  52350. + observer_t *o2;
  52351. +
  52352. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  52353. + if (o->observer == observer) {
  52354. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  52355. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  52356. + o->observer, n->object, o->notification);
  52357. + free_observer(n->mem_ctx, o);
  52358. + }
  52359. + }
  52360. + }
  52361. +
  52362. + dump_manager();
  52363. + return 0;
  52364. +}
  52365. +
  52366. +typedef struct callback_data {
  52367. + void *mem_ctx;
  52368. + dwc_notifier_callback_t cb;
  52369. + void *observer;
  52370. + void *data;
  52371. + void *object;
  52372. + char *notification;
  52373. + void *notification_data;
  52374. +} cb_data_t;
  52375. +
  52376. +static void cb_task(void *data)
  52377. +{
  52378. + cb_data_t *cb = (cb_data_t *)data;
  52379. +
  52380. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  52381. + dwc_free(cb->mem_ctx, cb);
  52382. +}
  52383. +
  52384. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  52385. +{
  52386. + observer_t *o;
  52387. +
  52388. + DWC_ASSERT(manager, "Notification manager not found");
  52389. +
  52390. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  52391. + int len = DWC_STRLEN(notification);
  52392. +
  52393. + if (DWC_STRLEN(o->notification) != len) {
  52394. + continue;
  52395. + }
  52396. +
  52397. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  52398. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  52399. +
  52400. + if (!cb_data) {
  52401. + DWC_ERROR("Failed to allocate callback data\n");
  52402. + return;
  52403. + }
  52404. +
  52405. + cb_data->mem_ctx = notifier->mem_ctx;
  52406. + cb_data->cb = o->callback;
  52407. + cb_data->observer = o->observer;
  52408. + cb_data->data = o->data;
  52409. + cb_data->object = notifier->object;
  52410. + cb_data->notification = notification;
  52411. + cb_data->notification_data = notification_data;
  52412. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  52413. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  52414. + "Notify callback from %p for Notification %s, to observer %p",
  52415. + cb_data->object, notification, cb_data->observer);
  52416. + }
  52417. + }
  52418. +}
  52419. +
  52420. +#endif /* DWC_NOTIFYLIB */
  52421. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_notifier.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h
  52422. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_notifier.h 1969-12-31 18:00:00.000000000 -0600
  52423. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h 2014-12-11 14:02:55.356418001 -0600
  52424. @@ -0,0 +1,122 @@
  52425. +
  52426. +#ifndef __DWC_NOTIFIER_H__
  52427. +#define __DWC_NOTIFIER_H__
  52428. +
  52429. +#ifdef __cplusplus
  52430. +extern "C" {
  52431. +#endif
  52432. +
  52433. +#include "dwc_os.h"
  52434. +
  52435. +/** @file
  52436. + *
  52437. + * A simple implementation of the Observer pattern. Any "module" can
  52438. + * register as an observer or notifier. The notion of "module" is abstract and
  52439. + * can mean anything used to identify either an observer or notifier. Usually
  52440. + * it will be a pointer to a data structure which contains some state, ie an
  52441. + * object.
  52442. + *
  52443. + * Before any notifiers can be added, the global notification manager must be
  52444. + * brought up with dwc_alloc_notification_manager().
  52445. + * dwc_free_notification_manager() will bring it down and free all resources.
  52446. + * These would typically be called upon module load and unload. The
  52447. + * notification manager is a single global instance that handles all registered
  52448. + * observable modules and observers so this should be done only once.
  52449. + *
  52450. + * A module can be observable by using Notifications to publicize some general
  52451. + * information about it's state or operation. It does not care who listens, or
  52452. + * even if anyone listens, or what they do with the information. The observable
  52453. + * modules do not need to know any information about it's observers or their
  52454. + * interface, or their state or data.
  52455. + *
  52456. + * Any module can register to emit Notifications. It should publish a list of
  52457. + * notifications that it can emit and their behavior, such as when they will get
  52458. + * triggered, and what information will be provided to the observer. Then it
  52459. + * should register itself as an observable module. See dwc_register_notifier().
  52460. + *
  52461. + * Any module can observe any observable, registered module, provided it has a
  52462. + * handle to the other module and knows what notifications to observe. See
  52463. + * dwc_add_observer().
  52464. + *
  52465. + * A function of type dwc_notifier_callback_t is called whenever a notification
  52466. + * is triggered with one or more observers observing it. This function is
  52467. + * called in it's own process so it may sleep or block if needed. It is
  52468. + * guaranteed to be called sometime after the notification has occurred and will
  52469. + * be called once per each time the notification is triggered. It will NOT be
  52470. + * called in the same process context used to trigger the notification.
  52471. + *
  52472. + * @section Limitiations
  52473. + *
  52474. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  52475. + * schedule too many processes too handle. Be aware of this limitation when
  52476. + * designing to use notifications, and only add notifications for appropriate
  52477. + * observable information.
  52478. + *
  52479. + * Also Notification callbacks are not synchronous. If you need to synchronize
  52480. + * the behavior between module/observer you must use other means. And perhaps
  52481. + * that will mean Notifications are not the proper solution.
  52482. + */
  52483. +
  52484. +struct dwc_notifier;
  52485. +typedef struct dwc_notifier dwc_notifier_t;
  52486. +
  52487. +/** The callback function must be of this type.
  52488. + *
  52489. + * @param object This is the object that is being observed.
  52490. + * @param notification This is the notification that was triggered.
  52491. + * @param observer This is the observer
  52492. + * @param notification_data This is notification-specific data that the notifier
  52493. + * has included in this notification. The value of this should be published in
  52494. + * the documentation of the observable module with the notifications.
  52495. + * @param user_data This is any custom data that the observer provided when
  52496. + * adding itself as an observer to the notification. */
  52497. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  52498. + void *notification_data, void *user_data);
  52499. +
  52500. +/** Brings up the notification manager. */
  52501. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  52502. +/** Brings down the notification manager. */
  52503. +extern void dwc_free_notification_manager(void);
  52504. +
  52505. +/** This function registers an observable module. A dwc_notifier_t object is
  52506. + * returned to the observable module. This is an opaque object that is used by
  52507. + * the observable module to trigger notifications. This object should only be
  52508. + * accessible to functions that are authorized to trigger notifications for this
  52509. + * module. Observers do not need this object. */
  52510. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  52511. +
  52512. +/** This function unregisters an observable module. All observers have to be
  52513. + * removed prior to unregistration. */
  52514. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  52515. +
  52516. +/** Add a module as an observer to the observable module. The observable module
  52517. + * needs to have previously registered with the notification manager.
  52518. + *
  52519. + * @param observer The observer module
  52520. + * @param object The module to observe
  52521. + * @param notification The notification to observe
  52522. + * @param callback The callback function to call
  52523. + * @param user_data Any additional user data to pass into the callback function */
  52524. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  52525. + dwc_notifier_callback_t callback, void *user_data);
  52526. +
  52527. +/** Removes the specified observer from all notifications that it is currently
  52528. + * observing. */
  52529. +extern int dwc_remove_observer(void *observer);
  52530. +
  52531. +/** This function triggers a Notification. It should be called by the
  52532. + * observable module, or any module or library which the observable module
  52533. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  52534. + *
  52535. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  52536. + * their own process context for each trigger. Callbacks can be blocking.
  52537. + * dwc_notify can be called from interrupt context if needed.
  52538. + *
  52539. + */
  52540. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  52541. +
  52542. +#ifdef __cplusplus
  52543. +}
  52544. +#endif
  52545. +
  52546. +#endif /* __DWC_NOTIFIER_H__ */
  52547. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_os.h linux-rpi/drivers/usb/host/dwc_common_port/dwc_os.h
  52548. --- linux-3.17.5/drivers/usb/host/dwc_common_port/dwc_os.h 1969-12-31 18:00:00.000000000 -0600
  52549. +++ linux-rpi/drivers/usb/host/dwc_common_port/dwc_os.h 2014-12-11 14:05:39.524418001 -0600
  52550. @@ -0,0 +1,1276 @@
  52551. +/* =========================================================================
  52552. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  52553. + * $Revision: #14 $
  52554. + * $Date: 2010/11/04 $
  52555. + * $Change: 1621695 $
  52556. + *
  52557. + * Synopsys Portability Library Software and documentation
  52558. + * (hereinafter, "Software") is an Unsupported proprietary work of
  52559. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  52560. + * between Synopsys and you.
  52561. + *
  52562. + * The Software IS NOT an item of Licensed Software or Licensed Product
  52563. + * under any End User Software License Agreement or Agreement for
  52564. + * Licensed Product with Synopsys or any supplement thereto. You are
  52565. + * permitted to use and redistribute this Software in source and binary
  52566. + * forms, with or without modification, provided that redistributions
  52567. + * of source code must retain this notice. You may not view, use,
  52568. + * disclose, copy or distribute this file or any information contained
  52569. + * herein except pursuant to this license grant from Synopsys. If you
  52570. + * do not agree with this notice, including the disclaimer below, then
  52571. + * you are not authorized to use the Software.
  52572. + *
  52573. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  52574. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52575. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  52576. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  52577. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  52578. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  52579. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52580. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  52581. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52582. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  52583. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52584. + * DAMAGE.
  52585. + * ========================================================================= */
  52586. +#ifndef _DWC_OS_H_
  52587. +#define _DWC_OS_H_
  52588. +
  52589. +#ifdef __cplusplus
  52590. +extern "C" {
  52591. +#endif
  52592. +
  52593. +/** @file
  52594. + *
  52595. + * DWC portability library, low level os-wrapper functions
  52596. + *
  52597. + */
  52598. +
  52599. +/* These basic types need to be defined by some OS header file or custom header
  52600. + * file for your specific target architecture.
  52601. + *
  52602. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  52603. + *
  52604. + * Any custom or alternate header file must be added and enabled here.
  52605. + */
  52606. +
  52607. +#ifdef DWC_LINUX
  52608. +# include <linux/types.h>
  52609. +# ifdef CONFIG_DEBUG_MUTEXES
  52610. +# include <linux/mutex.h>
  52611. +# endif
  52612. +# include <linux/spinlock.h>
  52613. +# include <linux/errno.h>
  52614. +# include <stdarg.h>
  52615. +#endif
  52616. +
  52617. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  52618. +# include <os_dep.h>
  52619. +#endif
  52620. +
  52621. +
  52622. +/** @name Primitive Types and Values */
  52623. +
  52624. +/** We define a boolean type for consistency. Can be either YES or NO */
  52625. +typedef uint8_t dwc_bool_t;
  52626. +#define YES 1
  52627. +#define NO 0
  52628. +
  52629. +#ifdef DWC_LINUX
  52630. +
  52631. +/** @name Error Codes */
  52632. +#define DWC_E_INVALID EINVAL
  52633. +#define DWC_E_NO_MEMORY ENOMEM
  52634. +#define DWC_E_NO_DEVICE ENODEV
  52635. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  52636. +#define DWC_E_TIMEOUT ETIMEDOUT
  52637. +#define DWC_E_BUSY EBUSY
  52638. +#define DWC_E_AGAIN EAGAIN
  52639. +#define DWC_E_RESTART ERESTART
  52640. +#define DWC_E_ABORT ECONNABORTED
  52641. +#define DWC_E_SHUTDOWN ESHUTDOWN
  52642. +#define DWC_E_NO_DATA ENODATA
  52643. +#define DWC_E_DISCONNECT ECONNRESET
  52644. +#define DWC_E_UNKNOWN EINVAL
  52645. +#define DWC_E_NO_STREAM_RES ENOSR
  52646. +#define DWC_E_COMMUNICATION ECOMM
  52647. +#define DWC_E_OVERFLOW EOVERFLOW
  52648. +#define DWC_E_PROTOCOL EPROTO
  52649. +#define DWC_E_IN_PROGRESS EINPROGRESS
  52650. +#define DWC_E_PIPE EPIPE
  52651. +#define DWC_E_IO EIO
  52652. +#define DWC_E_NO_SPACE ENOSPC
  52653. +
  52654. +#else
  52655. +
  52656. +/** @name Error Codes */
  52657. +#define DWC_E_INVALID 1001
  52658. +#define DWC_E_NO_MEMORY 1002
  52659. +#define DWC_E_NO_DEVICE 1003
  52660. +#define DWC_E_NOT_SUPPORTED 1004
  52661. +#define DWC_E_TIMEOUT 1005
  52662. +#define DWC_E_BUSY 1006
  52663. +#define DWC_E_AGAIN 1007
  52664. +#define DWC_E_RESTART 1008
  52665. +#define DWC_E_ABORT 1009
  52666. +#define DWC_E_SHUTDOWN 1010
  52667. +#define DWC_E_NO_DATA 1011
  52668. +#define DWC_E_DISCONNECT 2000
  52669. +#define DWC_E_UNKNOWN 3000
  52670. +#define DWC_E_NO_STREAM_RES 4001
  52671. +#define DWC_E_COMMUNICATION 4002
  52672. +#define DWC_E_OVERFLOW 4003
  52673. +#define DWC_E_PROTOCOL 4004
  52674. +#define DWC_E_IN_PROGRESS 4005
  52675. +#define DWC_E_PIPE 4006
  52676. +#define DWC_E_IO 4007
  52677. +#define DWC_E_NO_SPACE 4008
  52678. +
  52679. +#endif
  52680. +
  52681. +
  52682. +/** @name Tracing/Logging Functions
  52683. + *
  52684. + * These function provide the capability to add tracing, debugging, and error
  52685. + * messages, as well exceptions as assertions. The WUDEV uses these
  52686. + * extensively. These could be logged to the main console, the serial port, an
  52687. + * internal buffer, etc. These functions could also be no-op if they are too
  52688. + * expensive on your system. By default undefining the DEBUG macro already
  52689. + * no-ops some of these functions. */
  52690. +
  52691. +/** Returns non-zero if in interrupt context. */
  52692. +extern dwc_bool_t DWC_IN_IRQ(void);
  52693. +#define dwc_in_irq DWC_IN_IRQ
  52694. +
  52695. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  52696. +static inline char *dwc_irq(void) {
  52697. + return DWC_IN_IRQ() ? "IRQ" : "";
  52698. +}
  52699. +
  52700. +/** Returns non-zero if in bottom-half context. */
  52701. +extern dwc_bool_t DWC_IN_BH(void);
  52702. +#define dwc_in_bh DWC_IN_BH
  52703. +
  52704. +/** Returns "BH" if DWC_IN_BH is true. */
  52705. +static inline char *dwc_bh(void) {
  52706. + return DWC_IN_BH() ? "BH" : "";
  52707. +}
  52708. +
  52709. +/**
  52710. + * A vprintf() clone. Just call vprintf if you've got it.
  52711. + */
  52712. +extern void DWC_VPRINTF(char *format, va_list args);
  52713. +#define dwc_vprintf DWC_VPRINTF
  52714. +
  52715. +/**
  52716. + * A vsnprintf() clone. Just call vprintf if you've got it.
  52717. + */
  52718. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  52719. +#define dwc_vsnprintf DWC_VSNPRINTF
  52720. +
  52721. +/**
  52722. + * printf() clone. Just call printf if you've go it.
  52723. + */
  52724. +extern void DWC_PRINTF(char *format, ...)
  52725. +/* This provides compiler level static checking of the parameters if you're
  52726. + * using GCC. */
  52727. +#ifdef __GNUC__
  52728. + __attribute__ ((format(printf, 1, 2)));
  52729. +#else
  52730. + ;
  52731. +#endif
  52732. +#define dwc_printf DWC_PRINTF
  52733. +
  52734. +/**
  52735. + * sprintf() clone. Just call sprintf if you've got it.
  52736. + */
  52737. +extern int DWC_SPRINTF(char *string, char *format, ...)
  52738. +#ifdef __GNUC__
  52739. + __attribute__ ((format(printf, 2, 3)));
  52740. +#else
  52741. + ;
  52742. +#endif
  52743. +#define dwc_sprintf DWC_SPRINTF
  52744. +
  52745. +/**
  52746. + * snprintf() clone. Just call snprintf if you've got it.
  52747. + */
  52748. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  52749. +#ifdef __GNUC__
  52750. + __attribute__ ((format(printf, 3, 4)));
  52751. +#else
  52752. + ;
  52753. +#endif
  52754. +#define dwc_snprintf DWC_SNPRINTF
  52755. +
  52756. +/**
  52757. + * Prints a WARNING message. On systems that don't differentiate between
  52758. + * warnings and regular log messages, just print it. Indicates that something
  52759. + * may be wrong with the driver. Works like printf().
  52760. + *
  52761. + * Use the DWC_WARN macro to call this function.
  52762. + */
  52763. +extern void __DWC_WARN(char *format, ...)
  52764. +#ifdef __GNUC__
  52765. + __attribute__ ((format(printf, 1, 2)));
  52766. +#else
  52767. + ;
  52768. +#endif
  52769. +
  52770. +/**
  52771. + * Prints an error message. On systems that don't differentiate between errors
  52772. + * and regular log messages, just print it. Indicates that something went wrong
  52773. + * with the driver. Works like printf().
  52774. + *
  52775. + * Use the DWC_ERROR macro to call this function.
  52776. + */
  52777. +extern void __DWC_ERROR(char *format, ...)
  52778. +#ifdef __GNUC__
  52779. + __attribute__ ((format(printf, 1, 2)));
  52780. +#else
  52781. + ;
  52782. +#endif
  52783. +
  52784. +/**
  52785. + * Prints an exception error message and takes some user-defined action such as
  52786. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  52787. + * abnormally wrong with the driver such as programmer error, or other
  52788. + * exceptional condition. It should not be ignored so even on systems without
  52789. + * printing capability, some action should be taken to notify the developer of
  52790. + * it. Works like printf().
  52791. + */
  52792. +extern void DWC_EXCEPTION(char *format, ...)
  52793. +#ifdef __GNUC__
  52794. + __attribute__ ((format(printf, 1, 2)));
  52795. +#else
  52796. + ;
  52797. +#endif
  52798. +#define dwc_exception DWC_EXCEPTION
  52799. +
  52800. +#ifndef DWC_OTG_DEBUG_LEV
  52801. +#define DWC_OTG_DEBUG_LEV 0
  52802. +#endif
  52803. +
  52804. +#ifdef DEBUG
  52805. +/**
  52806. + * Prints out a debug message. Used for logging/trace messages.
  52807. + *
  52808. + * Use the DWC_DEBUG macro to call this function
  52809. + */
  52810. +extern void __DWC_DEBUG(char *format, ...)
  52811. +#ifdef __GNUC__
  52812. + __attribute__ ((format(printf, 1, 2)));
  52813. +#else
  52814. + ;
  52815. +#endif
  52816. +#else
  52817. +#define __DWC_DEBUG printk
  52818. +#endif
  52819. +
  52820. +/**
  52821. + * Prints out a Debug message.
  52822. + */
  52823. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  52824. + __func__, dwc_irq(), ## _args)
  52825. +#define dwc_debug DWC_DEBUG
  52826. +/**
  52827. + * Prints out a Debug message if enabled at compile time.
  52828. + */
  52829. +#if DWC_OTG_DEBUG_LEV > 0
  52830. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  52831. +#else
  52832. +#define DWC_DEBUGC(_format, _args...)
  52833. +#endif
  52834. +#define dwc_debugc DWC_DEBUGC
  52835. +/**
  52836. + * Prints out an informative message.
  52837. + */
  52838. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  52839. + dwc_irq(), ## _args)
  52840. +#define dwc_info DWC_INFO
  52841. +/**
  52842. + * Prints out an informative message if enabled at compile time.
  52843. + */
  52844. +#if DWC_OTG_DEBUG_LEV > 1
  52845. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  52846. +#else
  52847. +#define DWC_INFOC(_format, _args...)
  52848. +#endif
  52849. +#define dwc_infoc DWC_INFOC
  52850. +/**
  52851. + * Prints out a warning message.
  52852. + */
  52853. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  52854. + dwc_irq(), __func__, __LINE__, ## _args)
  52855. +#define dwc_warn DWC_WARN
  52856. +/**
  52857. + * Prints out an error message.
  52858. + */
  52859. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  52860. + dwc_irq(), __func__, __LINE__, ## _args)
  52861. +#define dwc_error DWC_ERROR
  52862. +
  52863. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  52864. + dwc_irq(), __func__, __LINE__, ## _args)
  52865. +#define dwc_proto_error DWC_PROTO_ERROR
  52866. +
  52867. +#ifdef DEBUG
  52868. +/** Prints out a exception error message if the _expr expression fails. Disabled
  52869. + * if DEBUG is not enabled. */
  52870. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  52871. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  52872. + __FILE__, __LINE__, ## _args); } \
  52873. + } while (0)
  52874. +#else
  52875. +#define DWC_ASSERT(_x...)
  52876. +#endif
  52877. +#define dwc_assert DWC_ASSERT
  52878. +
  52879. +
  52880. +/** @name Byte Ordering
  52881. + * The following functions are for conversions between processor's byte ordering
  52882. + * and specific ordering you want.
  52883. + */
  52884. +
  52885. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  52886. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  52887. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  52888. +
  52889. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  52890. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  52891. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  52892. +
  52893. +/** Converts 32 bit little endian data to CPU byte ordering. */
  52894. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  52895. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  52896. +
  52897. +/** Converts 32 bit big endian data to CPU byte ordering. */
  52898. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  52899. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  52900. +
  52901. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  52902. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  52903. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  52904. +
  52905. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  52906. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  52907. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  52908. +
  52909. +/** Converts 16 bit little endian data to CPU byte ordering. */
  52910. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  52911. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  52912. +
  52913. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  52914. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  52915. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  52916. +
  52917. +
  52918. +/** @name Register Read/Write
  52919. + *
  52920. + * The following six functions should be implemented to read/write registers of
  52921. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  52922. + * The reg value is a pointer to the register calculated from the void *base
  52923. + * variable passed into the driver when it is started. */
  52924. +
  52925. +#ifdef DWC_LINUX
  52926. +/* Linux doesn't need any extra parameters for register read/write, so we
  52927. + * just throw away the IO context parameter.
  52928. + */
  52929. +/** Reads the content of a 32-bit register. */
  52930. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  52931. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  52932. +
  52933. +/** Reads the content of a 64-bit register. */
  52934. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  52935. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  52936. +
  52937. +/** Writes to a 32-bit register. */
  52938. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  52939. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  52940. +
  52941. +/** Writes to a 64-bit register. */
  52942. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  52943. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  52944. +
  52945. +/**
  52946. + * Modify bit values in a register. Using the
  52947. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  52948. + */
  52949. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  52950. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  52951. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  52952. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  52953. +
  52954. +#endif /* DWC_LINUX */
  52955. +
  52956. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  52957. +typedef struct dwc_ioctx {
  52958. + struct device *dev;
  52959. + bus_space_tag_t iot;
  52960. + bus_space_handle_t ioh;
  52961. +} dwc_ioctx_t;
  52962. +
  52963. +/** BSD needs two extra parameters for register read/write, so we pass
  52964. + * them in using the IO context parameter.
  52965. + */
  52966. +/** Reads the content of a 32-bit register. */
  52967. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  52968. +#define dwc_read_reg32 DWC_READ_REG32
  52969. +
  52970. +/** Reads the content of a 64-bit register. */
  52971. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  52972. +#define dwc_read_reg64 DWC_READ_REG64
  52973. +
  52974. +/** Writes to a 32-bit register. */
  52975. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  52976. +#define dwc_write_reg32 DWC_WRITE_REG32
  52977. +
  52978. +/** Writes to a 64-bit register. */
  52979. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  52980. +#define dwc_write_reg64 DWC_WRITE_REG64
  52981. +
  52982. +/**
  52983. + * Modify bit values in a register. Using the
  52984. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  52985. + */
  52986. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  52987. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  52988. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  52989. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  52990. +
  52991. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  52992. +
  52993. +/** @cond */
  52994. +
  52995. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  52996. + * register writes. */
  52997. +
  52998. +#ifdef DWC_LINUX
  52999. +
  53000. +# ifdef DWC_DEBUG_REGS
  53001. +
  53002. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53003. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  53004. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  53005. +} \
  53006. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  53007. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  53008. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  53009. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  53010. +}
  53011. +
  53012. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53013. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  53014. + return DWC_READ_REG32(&container->regs->_reg); \
  53015. +} \
  53016. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  53017. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  53018. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  53019. +}
  53020. +
  53021. +# else /* DWC_DEBUG_REGS */
  53022. +
  53023. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53024. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  53025. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  53026. +} \
  53027. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  53028. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  53029. +}
  53030. +
  53031. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53032. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  53033. + return DWC_READ_REG32(&container->regs->_reg); \
  53034. +} \
  53035. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  53036. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  53037. +}
  53038. +
  53039. +# endif /* DWC_DEBUG_REGS */
  53040. +
  53041. +#endif /* DWC_LINUX */
  53042. +
  53043. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53044. +
  53045. +# ifdef DWC_DEBUG_REGS
  53046. +
  53047. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53048. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  53049. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  53050. +} \
  53051. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  53052. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  53053. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  53054. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  53055. +}
  53056. +
  53057. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53058. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  53059. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  53060. +} \
  53061. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  53062. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  53063. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  53064. +}
  53065. +
  53066. +# else /* DWC_DEBUG_REGS */
  53067. +
  53068. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53069. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  53070. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  53071. +} \
  53072. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  53073. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  53074. +}
  53075. +
  53076. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53077. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  53078. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  53079. +} \
  53080. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  53081. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  53082. +}
  53083. +
  53084. +# endif /* DWC_DEBUG_REGS */
  53085. +
  53086. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  53087. +
  53088. +/** @endcond */
  53089. +
  53090. +
  53091. +#ifdef DWC_CRYPTOLIB
  53092. +/** @name Crypto Functions
  53093. + *
  53094. + * These are the low-level cryptographic functions used by the driver. */
  53095. +
  53096. +/** Perform AES CBC */
  53097. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  53098. +#define dwc_aes_cbc DWC_AES_CBC
  53099. +
  53100. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  53101. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  53102. +#define dwc_random_bytes DWC_RANDOM_BYTES
  53103. +
  53104. +/** Perform the SHA-256 hash function */
  53105. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  53106. +#define dwc_sha256 DWC_SHA256
  53107. +
  53108. +/** Calculated the HMAC-SHA256 */
  53109. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  53110. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  53111. +
  53112. +#endif /* DWC_CRYPTOLIB */
  53113. +
  53114. +
  53115. +/** @name Memory Allocation
  53116. + *
  53117. + * These function provide access to memory allocation. There are only 2 DMA
  53118. + * functions and 3 Regular memory functions that need to be implemented. None
  53119. + * of the memory debugging routines need to be implemented. The allocation
  53120. + * routines all ZERO the contents of the memory.
  53121. + *
  53122. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  53123. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  53124. + * keeps track of how much memory the driver is using at any given time. */
  53125. +
  53126. +#define DWC_PAGE_SIZE 4096
  53127. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  53128. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  53129. +
  53130. +#define DWC_INVALID_DMA_ADDR 0x0
  53131. +
  53132. +#ifdef DWC_LINUX
  53133. +/** Type for a DMA address */
  53134. +typedef dma_addr_t dwc_dma_t;
  53135. +#endif
  53136. +
  53137. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53138. +typedef bus_addr_t dwc_dma_t;
  53139. +#endif
  53140. +
  53141. +#ifdef DWC_FREEBSD
  53142. +typedef struct dwc_dmactx {
  53143. + struct device *dev;
  53144. + bus_dma_tag_t dma_tag;
  53145. + bus_dmamap_t dma_map;
  53146. + bus_addr_t dma_paddr;
  53147. + void *dma_vaddr;
  53148. +} dwc_dmactx_t;
  53149. +#endif
  53150. +
  53151. +#ifdef DWC_NETBSD
  53152. +typedef struct dwc_dmactx {
  53153. + struct device *dev;
  53154. + bus_dma_tag_t dma_tag;
  53155. + bus_dmamap_t dma_map;
  53156. + bus_dma_segment_t segs[1];
  53157. + int nsegs;
  53158. + bus_addr_t dma_paddr;
  53159. + void *dma_vaddr;
  53160. +} dwc_dmactx_t;
  53161. +#endif
  53162. +
  53163. +/* @todo these functions will be added in the future */
  53164. +#if 0
  53165. +/**
  53166. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  53167. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  53168. + * boundary requirements specified.
  53169. + *
  53170. + * @param[in] size Specifies the size of the buffers that will be allocated from
  53171. + * this pool.
  53172. + * @param[in] align Specifies the byte alignment requirements of the buffers
  53173. + * allocated from this pool. Must be a power of 2.
  53174. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  53175. + * this pool must not cross.
  53176. + *
  53177. + * @returns A pointer to an internal opaque structure which is not to be
  53178. + * accessed outside of these library functions. Use this handle to specify
  53179. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  53180. + * when you are done with it.
  53181. + */
  53182. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  53183. +
  53184. +/**
  53185. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  53186. + */
  53187. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  53188. +
  53189. +/**
  53190. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  53191. + */
  53192. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  53193. +
  53194. +/**
  53195. + * Free a previously allocated buffer from the DMA pool.
  53196. + */
  53197. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  53198. +#endif
  53199. +
  53200. +/** Allocates a DMA capable buffer and zeroes its contents. */
  53201. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  53202. +
  53203. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  53204. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  53205. +
  53206. +/** Frees a previously allocated buffer. */
  53207. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  53208. +
  53209. +/** Allocates a block of memory and zeroes its contents. */
  53210. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  53211. +
  53212. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  53213. + * which can be used inside interrupt context. The size should be sufficiently
  53214. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  53215. + * __DWC_ALLOC if it is atomic. */
  53216. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  53217. +
  53218. +/** Frees a previously allocated buffer. */
  53219. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  53220. +
  53221. +#ifndef DWC_DEBUG_MEMORY
  53222. +
  53223. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  53224. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  53225. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  53226. +
  53227. +# ifdef DWC_LINUX
  53228. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  53229. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  53230. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  53231. +# endif
  53232. +
  53233. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53234. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  53235. +#define DWC_DMA_FREE __DWC_DMA_FREE
  53236. +# endif
  53237. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  53238. +
  53239. +#else /* DWC_DEBUG_MEMORY */
  53240. +
  53241. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  53242. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  53243. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  53244. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  53245. + char const *func, int line);
  53246. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  53247. + char const *func, int line);
  53248. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  53249. + dwc_dma_t dma_addr, char const *func, int line);
  53250. +
  53251. +extern int dwc_memory_debug_start(void *mem_ctx);
  53252. +extern void dwc_memory_debug_stop(void);
  53253. +extern void dwc_memory_debug_report(void);
  53254. +
  53255. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  53256. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  53257. + __func__, __LINE__)
  53258. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  53259. +
  53260. +# ifdef DWC_LINUX
  53261. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  53262. + _dma_, __func__, __LINE__)
  53263. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  53264. + _dma_, __func__, __LINE__)
  53265. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  53266. + _virt_, _dma_, __func__, __LINE__)
  53267. +# endif
  53268. +
  53269. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53270. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  53271. + _dma_, __func__, __LINE__)
  53272. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  53273. + _virt_, _dma_, __func__, __LINE__)
  53274. +# endif
  53275. +
  53276. +#endif /* DWC_DEBUG_MEMORY */
  53277. +
  53278. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  53279. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  53280. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  53281. +
  53282. +#ifdef DWC_LINUX
  53283. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  53284. + * just throw away the DMA context parameter.
  53285. + */
  53286. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  53287. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  53288. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  53289. +#endif
  53290. +
  53291. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53292. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  53293. + * them in using the DMA context parameter.
  53294. + */
  53295. +#define dwc_dma_alloc DWC_DMA_ALLOC
  53296. +#define dwc_dma_free DWC_DMA_FREE
  53297. +#endif
  53298. +
  53299. +
  53300. +/** @name Memory and String Processing */
  53301. +
  53302. +/** memset() clone */
  53303. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  53304. +#define dwc_memset DWC_MEMSET
  53305. +
  53306. +/** memcpy() clone */
  53307. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  53308. +#define dwc_memcpy DWC_MEMCPY
  53309. +
  53310. +/** memmove() clone */
  53311. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  53312. +#define dwc_memmove DWC_MEMMOVE
  53313. +
  53314. +/** memcmp() clone */
  53315. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  53316. +#define dwc_memcmp DWC_MEMCMP
  53317. +
  53318. +/** strcmp() clone */
  53319. +extern int DWC_STRCMP(void *s1, void *s2);
  53320. +#define dwc_strcmp DWC_STRCMP
  53321. +
  53322. +/** strncmp() clone */
  53323. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  53324. +#define dwc_strncmp DWC_STRNCMP
  53325. +
  53326. +/** strlen() clone, for NULL terminated ASCII strings */
  53327. +extern int DWC_STRLEN(char const *str);
  53328. +#define dwc_strlen DWC_STRLEN
  53329. +
  53330. +/** strcpy() clone, for NULL terminated ASCII strings */
  53331. +extern char *DWC_STRCPY(char *to, const char *from);
  53332. +#define dwc_strcpy DWC_STRCPY
  53333. +
  53334. +/** strdup() clone. If you wish to use memory allocation debugging, this
  53335. + * implementation of strdup should use the DWC_* memory routines instead of
  53336. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  53337. + * will not be seen by the debugging routines. */
  53338. +extern char *DWC_STRDUP(char const *str);
  53339. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  53340. +
  53341. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  53342. + * converted from the string str in base 10 unless the string begins with a "0x"
  53343. + * in which case it is base 16. String must be a NULL terminated sequence of
  53344. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  53345. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  53346. + * the number and end with a NULL character. If any invalid characters are
  53347. + * encountered or it returns with a negative error code and the results of the
  53348. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  53349. + * undefined. An example implementation using atoi() can be referenced from the
  53350. + * Linux implementation. */
  53351. +extern int DWC_ATOI(const char *str, int32_t *value);
  53352. +#define dwc_atoi DWC_ATOI
  53353. +
  53354. +/** Same as above but for unsigned. */
  53355. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  53356. +#define dwc_atoui DWC_ATOUI
  53357. +
  53358. +#ifdef DWC_UTFLIB
  53359. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  53360. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  53361. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  53362. +#endif
  53363. +
  53364. +
  53365. +/** @name Wait queues
  53366. + *
  53367. + * Wait queues provide a means of synchronizing between threads or processes. A
  53368. + * process can block on a waitq if some condition is not true, waiting for it to
  53369. + * become true. When the waitq is triggered all waiting process will get
  53370. + * unblocked and the condition will be check again. Waitqs should be triggered
  53371. + * every time a condition can potentially change.*/
  53372. +struct dwc_waitq;
  53373. +
  53374. +/** Type for a waitq */
  53375. +typedef struct dwc_waitq dwc_waitq_t;
  53376. +
  53377. +/** The type of waitq condition callback function. This is called every time
  53378. + * condition is evaluated. */
  53379. +typedef int (*dwc_waitq_condition_t)(void *data);
  53380. +
  53381. +/** Allocate a waitq */
  53382. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  53383. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  53384. +
  53385. +/** Free a waitq */
  53386. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  53387. +#define dwc_waitq_free DWC_WAITQ_FREE
  53388. +
  53389. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  53390. + * condition again. The function returns when the condition becomes true. The return value
  53391. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  53392. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  53393. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  53394. +
  53395. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  53396. + * check the condition again. The function returns when the condition become
  53397. + * true or the timeout has passed. The return value is 0 on condition true or
  53398. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  53399. + * error. */
  53400. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  53401. + void *data, int32_t msecs);
  53402. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  53403. +
  53404. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  53405. + * has potentially changed. */
  53406. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  53407. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  53408. +
  53409. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  53410. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  53411. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  53412. +
  53413. +
  53414. +/** @name Threads
  53415. + *
  53416. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  53417. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  53418. + * returns the value from the thread.
  53419. + */
  53420. +
  53421. +struct dwc_thread;
  53422. +
  53423. +/** Type for a thread */
  53424. +typedef struct dwc_thread dwc_thread_t;
  53425. +
  53426. +/** The thread function */
  53427. +typedef int (*dwc_thread_function_t)(void *data);
  53428. +
  53429. +/** Create a thread and start it running the thread_function. Returns a handle
  53430. + * to the thread */
  53431. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  53432. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  53433. +
  53434. +/** Stops a thread. Return the value returned by the thread. Or will return
  53435. + * DWC_ABORT if the thread never started. */
  53436. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  53437. +#define dwc_thread_stop DWC_THREAD_STOP
  53438. +
  53439. +/** Signifies to the thread that it must stop. */
  53440. +#ifdef DWC_LINUX
  53441. +/* Linux doesn't need any parameters for kthread_should_stop() */
  53442. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  53443. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  53444. +
  53445. +/* No thread_exit function in Linux */
  53446. +#define dwc_thread_exit(_thrd_)
  53447. +#endif
  53448. +
  53449. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53450. +/** BSD needs the thread pointer for kthread_suspend_check() */
  53451. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  53452. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  53453. +
  53454. +/** The thread must call this to exit. */
  53455. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  53456. +#define dwc_thread_exit DWC_THREAD_EXIT
  53457. +#endif
  53458. +
  53459. +
  53460. +/** @name Work queues
  53461. + *
  53462. + * Workqs are used to queue a callback function to be called at some later time,
  53463. + * in another thread. */
  53464. +struct dwc_workq;
  53465. +
  53466. +/** Type for a workq */
  53467. +typedef struct dwc_workq dwc_workq_t;
  53468. +
  53469. +/** The type of the callback function to be called. */
  53470. +typedef void (*dwc_work_callback_t)(void *data);
  53471. +
  53472. +/** Allocate a workq */
  53473. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  53474. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  53475. +
  53476. +/** Free a workq. All work must be completed before being freed. */
  53477. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  53478. +#define dwc_workq_free DWC_WORKQ_FREE
  53479. +
  53480. +/** Schedule a callback on the workq, passing in data. The function will be
  53481. + * scheduled at some later time. */
  53482. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  53483. + void *data, char *format, ...)
  53484. +#ifdef __GNUC__
  53485. + __attribute__ ((format(printf, 4, 5)));
  53486. +#else
  53487. + ;
  53488. +#endif
  53489. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  53490. +
  53491. +/** Schedule a callback on the workq, that will be called until at least
  53492. + * given number miliseconds have passed. */
  53493. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  53494. + void *data, uint32_t time, char *format, ...)
  53495. +#ifdef __GNUC__
  53496. + __attribute__ ((format(printf, 5, 6)));
  53497. +#else
  53498. + ;
  53499. +#endif
  53500. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  53501. +
  53502. +/** The number of processes in the workq */
  53503. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  53504. +#define dwc_workq_pending DWC_WORKQ_PENDING
  53505. +
  53506. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  53507. + * 0 on timeout. */
  53508. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  53509. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  53510. +
  53511. +
  53512. +/** @name Tasklets
  53513. + *
  53514. + */
  53515. +struct dwc_tasklet;
  53516. +
  53517. +/** Type for a tasklet */
  53518. +typedef struct dwc_tasklet dwc_tasklet_t;
  53519. +
  53520. +/** The type of the callback function to be called */
  53521. +typedef void (*dwc_tasklet_callback_t)(void *data);
  53522. +
  53523. +/** Allocates a tasklet */
  53524. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  53525. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  53526. +
  53527. +/** Frees a tasklet */
  53528. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  53529. +#define dwc_task_free DWC_TASK_FREE
  53530. +
  53531. +/** Schedules a tasklet to run */
  53532. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  53533. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  53534. +
  53535. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  53536. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  53537. +
  53538. +/** @name Timer
  53539. + *
  53540. + * Callbacks must be small and atomic.
  53541. + */
  53542. +struct dwc_timer;
  53543. +
  53544. +/** Type for a timer */
  53545. +typedef struct dwc_timer dwc_timer_t;
  53546. +
  53547. +/** The type of the callback function to be called */
  53548. +typedef void (*dwc_timer_callback_t)(void *data);
  53549. +
  53550. +/** Allocates a timer */
  53551. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  53552. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  53553. +
  53554. +/** Frees a timer */
  53555. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  53556. +#define dwc_timer_free DWC_TIMER_FREE
  53557. +
  53558. +/** Schedules the timer to run at time ms from now. And will repeat at every
  53559. + * repeat_interval msec therafter
  53560. + *
  53561. + * Modifies a timer that is still awaiting execution to a new expiration time.
  53562. + * The mod_time is added to the old time. */
  53563. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  53564. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  53565. +
  53566. +/** Disables the timer from execution. */
  53567. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  53568. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  53569. +
  53570. +
  53571. +/** @name Spinlocks
  53572. + *
  53573. + * These locks are used when the work between the lock/unlock is atomic and
  53574. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  53575. + * suitable to lock between interrupt/non-interrupt context. They also lock
  53576. + * between processes if you have multiple CPUs or Preemption. If you don't have
  53577. + * multiple CPUS or Preemption, then the you can simply implement the
  53578. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  53579. + * the work between the lock/unlock is atomic, the process context will never
  53580. + * change, and so you never have to lock between processes. */
  53581. +
  53582. +struct dwc_spinlock;
  53583. +
  53584. +/** Type for a spinlock */
  53585. +typedef struct dwc_spinlock dwc_spinlock_t;
  53586. +
  53587. +/** Type for the 'flags' argument to spinlock funtions */
  53588. +typedef unsigned long dwc_irqflags_t;
  53589. +
  53590. +/** Returns an initialized lock variable. This function should allocate and
  53591. + * initialize the OS-specific data structure used for locking. This data
  53592. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  53593. + * be freed by the DWC_FREE_LOCK when it is no longer used.
  53594. + *
  53595. + * For Linux Spinlock Debugging make it macro because the debugging routines use
  53596. + * the symbol name to determine recursive locking. Using a wrapper function
  53597. + * makes it falsely think recursive locking occurs. */
  53598. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
  53599. +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
  53600. + lock = DWC_ALLOC(sizeof(spinlock_t)); \
  53601. + if (lock) { \
  53602. + spin_lock_init((spinlock_t *)lock); \
  53603. + } \
  53604. +})
  53605. +#else
  53606. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  53607. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  53608. +#endif
  53609. +
  53610. +/** Frees an initialized lock variable. */
  53611. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  53612. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  53613. +
  53614. +/** Disables interrupts and blocks until it acquires the lock.
  53615. + *
  53616. + * @param lock Pointer to the spinlock.
  53617. + * @param flags Unsigned long for irq flags storage.
  53618. + */
  53619. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  53620. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  53621. +
  53622. +/** Re-enables the interrupt and releases the lock.
  53623. + *
  53624. + * @param lock Pointer to the spinlock.
  53625. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  53626. + * passed into DWC_LOCK.
  53627. + */
  53628. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  53629. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  53630. +
  53631. +/** Blocks until it acquires the lock.
  53632. + *
  53633. + * @param lock Pointer to the spinlock.
  53634. + */
  53635. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  53636. +#define dwc_spinlock DWC_SPINLOCK
  53637. +
  53638. +/** Releases the lock.
  53639. + *
  53640. + * @param lock Pointer to the spinlock.
  53641. + */
  53642. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  53643. +#define dwc_spinunlock DWC_SPINUNLOCK
  53644. +
  53645. +
  53646. +/** @name Mutexes
  53647. + *
  53648. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  53649. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  53650. + */
  53651. +
  53652. +struct dwc_mutex;
  53653. +
  53654. +/** Type for a mutex */
  53655. +typedef struct dwc_mutex dwc_mutex_t;
  53656. +
  53657. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  53658. + * the symbol to determine recursive locking. This makes it falsely think
  53659. + * recursive locking occurs. */
  53660. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  53661. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  53662. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  53663. + mutex_init((struct mutex *)__mutexp); \
  53664. +})
  53665. +#endif
  53666. +
  53667. +/** Allocate a mutex */
  53668. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  53669. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  53670. +
  53671. +/* For memory leak debugging when using Linux Mutex Debugging */
  53672. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  53673. +#define DWC_MUTEX_FREE(__mutexp) do { \
  53674. + mutex_destroy((struct mutex *)__mutexp); \
  53675. + DWC_FREE(__mutexp); \
  53676. +} while(0)
  53677. +#else
  53678. +/** Free a mutex */
  53679. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  53680. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  53681. +#endif
  53682. +
  53683. +/** Lock a mutex */
  53684. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  53685. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  53686. +
  53687. +/** Non-blocking lock returns 1 on successful lock. */
  53688. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  53689. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  53690. +
  53691. +/** Unlock a mutex */
  53692. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  53693. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  53694. +
  53695. +
  53696. +/** @name Time */
  53697. +
  53698. +/** Microsecond delay.
  53699. + *
  53700. + * @param usecs Microseconds to delay.
  53701. + */
  53702. +extern void DWC_UDELAY(uint32_t usecs);
  53703. +#define dwc_udelay DWC_UDELAY
  53704. +
  53705. +/** Millisecond delay.
  53706. + *
  53707. + * @param msecs Milliseconds to delay.
  53708. + */
  53709. +extern void DWC_MDELAY(uint32_t msecs);
  53710. +#define dwc_mdelay DWC_MDELAY
  53711. +
  53712. +/** Non-busy waiting.
  53713. + * Sleeps for specified number of milliseconds.
  53714. + *
  53715. + * @param msecs Milliseconds to sleep.
  53716. + */
  53717. +extern void DWC_MSLEEP(uint32_t msecs);
  53718. +#define dwc_msleep DWC_MSLEEP
  53719. +
  53720. +/**
  53721. + * Returns number of milliseconds since boot.
  53722. + */
  53723. +extern uint32_t DWC_TIME(void);
  53724. +#define dwc_time DWC_TIME
  53725. +
  53726. +
  53727. +
  53728. +
  53729. +/* @mainpage DWC Portability and Common Library
  53730. + *
  53731. + * This is the documentation for the DWC Portability and Common Library.
  53732. + *
  53733. + * @section intro Introduction
  53734. + *
  53735. + * The DWC Portability library consists of wrapper calls and data structures to
  53736. + * all low-level functions which are typically provided by the OS. The WUDEV
  53737. + * driver uses only these functions. In order to port the WUDEV driver, only
  53738. + * the functions in this library need to be re-implemented, with the same
  53739. + * behavior as documented here.
  53740. + *
  53741. + * The Common library consists of higher level functions, which rely only on
  53742. + * calling the functions from the DWC Portability library. These common
  53743. + * routines are shared across modules. Some of the common libraries need to be
  53744. + * used directly by the driver programmer when porting WUDEV. Such as the
  53745. + * parameter and notification libraries.
  53746. + *
  53747. + * @section low Portability Library OS Wrapper Functions
  53748. + *
  53749. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  53750. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  53751. + * these functions are included in the dwc_os.h file.
  53752. + *
  53753. + * There are many functions here covering a wide array of OS services. Please
  53754. + * see dwc_os.h for details, and implementation notes for each function.
  53755. + *
  53756. + * @section common Common Library Functions
  53757. + *
  53758. + * Any function starting with dwc and in all lowercase is a common library
  53759. + * routine. These functions have a portable implementation and do not need to
  53760. + * be reimplemented when porting. The common routines can be used by any
  53761. + * driver, and some must be used by the end user to control the drivers. For
  53762. + * example, you must use the Parameter common library in order to set the
  53763. + * parameters in the WUDEV module.
  53764. + *
  53765. + * The common libraries consist of the following:
  53766. + *
  53767. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  53768. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  53769. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  53770. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  53771. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  53772. + * - Modpow - Used internally only. See dwc_modpow.h
  53773. + * - DH - Used internally only. See dwc_dh.h
  53774. + * - Crypto - Used internally only. See dwc_crypto.h
  53775. + *
  53776. + *
  53777. + * @section prereq Prerequistes For dwc_os.h
  53778. + * @subsection types Data Types
  53779. + *
  53780. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  53781. + * compilation environment. These data types are:
  53782. + *
  53783. + * - uint8_t - unsigned 8-bit data type
  53784. + * - int8_t - signed 8-bit data type
  53785. + * - uint16_t - unsigned 16-bit data type
  53786. + * - int16_t - signed 16-bit data type
  53787. + * - uint32_t - unsigned 32-bit data type
  53788. + * - int32_t - signed 32-bit data type
  53789. + * - uint64_t - unsigned 64-bit data type
  53790. + * - int64_t - signed 64-bit data type
  53791. + *
  53792. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  53793. + * that is to modify the top of the file to include the appropriate header.
  53794. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  53795. + * defined, the correct header will be added. A standard header <stdint.h> is
  53796. + * also used for environments where standard C headers are available.
  53797. + *
  53798. + * @subsection stdarg Variable Arguments
  53799. + *
  53800. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  53801. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  53802. + * provided in your enviornment in order to use dwc_os.h with the debug and
  53803. + * tracing message functionality.
  53804. + *
  53805. + * @subsection thread Threading
  53806. + *
  53807. + * WUDEV Core must be run on an operating system that provides for multiple
  53808. + * threads/processes. Threading can be implemented in many ways, even in
  53809. + * embedded systems without an operating system. At the bare minimum, the
  53810. + * system should be able to start any number of processes at any time to handle
  53811. + * special work. It need not be a pre-emptive system. Process context can
  53812. + * change upon a call to a blocking function. The hardware interrupt context
  53813. + * that calls the module's ISR() function must be differentiable from process
  53814. + * context, even if your processes are impemented via a hardware interrupt.
  53815. + * Further locking mechanism between process must exist (or be implemented), and
  53816. + * process context must have a way to disable interrupts for a period of time to
  53817. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  53818. + * threading should be able to be implemented with the defined behavior.
  53819. + *
  53820. + */
  53821. +
  53822. +#ifdef __cplusplus
  53823. +}
  53824. +#endif
  53825. +
  53826. +#endif /* _DWC_OS_H_ */
  53827. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/Makefile linux-rpi/drivers/usb/host/dwc_common_port/Makefile
  53828. --- linux-3.17.5/drivers/usb/host/dwc_common_port/Makefile 1969-12-31 18:00:00.000000000 -0600
  53829. +++ linux-rpi/drivers/usb/host/dwc_common_port/Makefile 2014-12-11 14:02:55.328418001 -0600
  53830. @@ -0,0 +1,58 @@
  53831. +#
  53832. +# Makefile for DWC_common library
  53833. +#
  53834. +
  53835. +ifneq ($(KERNELRELEASE),)
  53836. +
  53837. +ccflags-y += -DDWC_LINUX
  53838. +#ccflags-y += -DDEBUG
  53839. +#ccflags-y += -DDWC_DEBUG_REGS
  53840. +#ccflags-y += -DDWC_DEBUG_MEMORY
  53841. +
  53842. +ccflags-y += -DDWC_LIBMODULE
  53843. +ccflags-y += -DDWC_CCLIB
  53844. +#ccflags-y += -DDWC_CRYPTOLIB
  53845. +ccflags-y += -DDWC_NOTIFYLIB
  53846. +ccflags-y += -DDWC_UTFLIB
  53847. +
  53848. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  53849. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  53850. + dwc_crypto.o dwc_notifier.o \
  53851. + dwc_common_linux.o dwc_mem.o
  53852. +
  53853. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  53854. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  53855. +
  53856. +ifneq ($(kernrel3),2.6.20)
  53857. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  53858. +ccflags-y += $(CPPFLAGS)
  53859. +endif
  53860. +
  53861. +else
  53862. +
  53863. +#ifeq ($(KDIR),)
  53864. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  53865. +#endif
  53866. +
  53867. +ifeq ($(ARCH),)
  53868. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  53869. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  53870. +endif
  53871. +
  53872. +ifeq ($(DOXYGEN),)
  53873. +DOXYGEN := doxygen
  53874. +endif
  53875. +
  53876. +default:
  53877. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  53878. +
  53879. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  53880. + $(DOXYGEN) doc/doxygen.cfg
  53881. +
  53882. +tags: $(wildcard *.[hc])
  53883. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  53884. +
  53885. +endif
  53886. +
  53887. +clean:
  53888. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  53889. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/Makefile.fbsd linux-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd
  53890. --- linux-3.17.5/drivers/usb/host/dwc_common_port/Makefile.fbsd 1969-12-31 18:00:00.000000000 -0600
  53891. +++ linux-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd 2014-12-11 14:02:55.328418001 -0600
  53892. @@ -0,0 +1,17 @@
  53893. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  53894. +CFLAGS += -DDWC_FREEBSD
  53895. +CFLAGS += -DDEBUG
  53896. +#CFLAGS += -DDWC_DEBUG_REGS
  53897. +#CFLAGS += -DDWC_DEBUG_MEMORY
  53898. +
  53899. +#CFLAGS += -DDWC_LIBMODULE
  53900. +#CFLAGS += -DDWC_CCLIB
  53901. +#CFLAGS += -DDWC_CRYPTOLIB
  53902. +#CFLAGS += -DDWC_NOTIFYLIB
  53903. +#CFLAGS += -DDWC_UTFLIB
  53904. +
  53905. +KMOD = dwc_common_port_lib
  53906. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  53907. + dwc_common_fbsd.c dwc_mem.c
  53908. +
  53909. +.include <bsd.kmod.mk>
  53910. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/Makefile.linux linux-rpi/drivers/usb/host/dwc_common_port/Makefile.linux
  53911. --- linux-3.17.5/drivers/usb/host/dwc_common_port/Makefile.linux 1969-12-31 18:00:00.000000000 -0600
  53912. +++ linux-rpi/drivers/usb/host/dwc_common_port/Makefile.linux 2014-12-11 14:02:55.328418001 -0600
  53913. @@ -0,0 +1,49 @@
  53914. +#
  53915. +# Makefile for DWC_common library
  53916. +#
  53917. +ifneq ($(KERNELRELEASE),)
  53918. +
  53919. +ccflags-y += -DDWC_LINUX
  53920. +#ccflags-y += -DDEBUG
  53921. +#ccflags-y += -DDWC_DEBUG_REGS
  53922. +#ccflags-y += -DDWC_DEBUG_MEMORY
  53923. +
  53924. +ccflags-y += -DDWC_LIBMODULE
  53925. +ccflags-y += -DDWC_CCLIB
  53926. +ccflags-y += -DDWC_CRYPTOLIB
  53927. +ccflags-y += -DDWC_NOTIFYLIB
  53928. +ccflags-y += -DDWC_UTFLIB
  53929. +
  53930. +obj-m := dwc_common_port_lib.o
  53931. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  53932. + dwc_crypto.o dwc_notifier.o \
  53933. + dwc_common_linux.o dwc_mem.o
  53934. +
  53935. +else
  53936. +
  53937. +ifeq ($(KDIR),)
  53938. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  53939. +endif
  53940. +
  53941. +ifeq ($(ARCH),)
  53942. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  53943. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  53944. +endif
  53945. +
  53946. +ifeq ($(DOXYGEN),)
  53947. +DOXYGEN := doxygen
  53948. +endif
  53949. +
  53950. +default:
  53951. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  53952. +
  53953. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  53954. + $(DOXYGEN) doc/doxygen.cfg
  53955. +
  53956. +tags: $(wildcard *.[hc])
  53957. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  53958. +
  53959. +endif
  53960. +
  53961. +clean:
  53962. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  53963. diff -Nur linux-3.17.5/drivers/usb/host/dwc_common_port/usb.h linux-rpi/drivers/usb/host/dwc_common_port/usb.h
  53964. --- linux-3.17.5/drivers/usb/host/dwc_common_port/usb.h 1969-12-31 18:00:00.000000000 -0600
  53965. +++ linux-rpi/drivers/usb/host/dwc_common_port/usb.h 2014-12-11 14:02:55.356418001 -0600
  53966. @@ -0,0 +1,946 @@
  53967. +/*
  53968. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  53969. + * All rights reserved.
  53970. + *
  53971. + * This code is derived from software contributed to The NetBSD Foundation
  53972. + * by Lennart Augustsson (lennart@augustsson.net) at
  53973. + * Carlstedt Research & Technology.
  53974. + *
  53975. + * Redistribution and use in source and binary forms, with or without
  53976. + * modification, are permitted provided that the following conditions
  53977. + * are met:
  53978. + * 1. Redistributions of source code must retain the above copyright
  53979. + * notice, this list of conditions and the following disclaimer.
  53980. + * 2. Redistributions in binary form must reproduce the above copyright
  53981. + * notice, this list of conditions and the following disclaimer in the
  53982. + * documentation and/or other materials provided with the distribution.
  53983. + * 3. All advertising materials mentioning features or use of this software
  53984. + * must display the following acknowledgement:
  53985. + * This product includes software developed by the NetBSD
  53986. + * Foundation, Inc. and its contributors.
  53987. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  53988. + * contributors may be used to endorse or promote products derived
  53989. + * from this software without specific prior written permission.
  53990. + *
  53991. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  53992. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  53993. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  53994. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  53995. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  53996. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53997. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  53998. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  53999. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  54000. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54001. + * POSSIBILITY OF SUCH DAMAGE.
  54002. + */
  54003. +
  54004. +/* Modified by Synopsys, Inc, 12/12/2007 */
  54005. +
  54006. +
  54007. +#ifndef _USB_H_
  54008. +#define _USB_H_
  54009. +
  54010. +#ifdef __cplusplus
  54011. +extern "C" {
  54012. +#endif
  54013. +
  54014. +/*
  54015. + * The USB records contain some unaligned little-endian word
  54016. + * components. The U[SG]ETW macros take care of both the alignment
  54017. + * and endian problem and should always be used to access non-byte
  54018. + * values.
  54019. + */
  54020. +typedef u_int8_t uByte;
  54021. +typedef u_int8_t uWord[2];
  54022. +typedef u_int8_t uDWord[4];
  54023. +
  54024. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  54025. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  54026. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  54027. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  54028. +
  54029. +#if 1
  54030. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  54031. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  54032. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  54033. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  54034. + (w)[1] = (u_int8_t)((v) >> 8), \
  54035. + (w)[2] = (u_int8_t)((v) >> 16), \
  54036. + (w)[3] = (u_int8_t)((v) >> 24))
  54037. +#else
  54038. +/*
  54039. + * On little-endian machines that can handle unanliged accesses
  54040. + * (e.g. i386) these macros can be replaced by the following.
  54041. + */
  54042. +#define UGETW(w) (*(u_int16_t *)(w))
  54043. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  54044. +#define UGETDW(w) (*(u_int32_t *)(w))
  54045. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  54046. +#endif
  54047. +
  54048. +/*
  54049. + * Macros for accessing UAS IU fields, which are big-endian
  54050. + */
  54051. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  54052. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  54053. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  54054. + ((x) >> 8) & 0xff, (x) & 0xff }
  54055. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  54056. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  54057. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  54058. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  54059. + (w)[1] = (u_int8_t)((v) >> 16), \
  54060. + (w)[2] = (u_int8_t)((v) >> 8), \
  54061. + (w)[3] = (u_int8_t)(v))
  54062. +
  54063. +#define UPACKED __attribute__((__packed__))
  54064. +
  54065. +typedef struct {
  54066. + uByte bmRequestType;
  54067. + uByte bRequest;
  54068. + uWord wValue;
  54069. + uWord wIndex;
  54070. + uWord wLength;
  54071. +} UPACKED usb_device_request_t;
  54072. +
  54073. +#define UT_GET_DIR(a) ((a) & 0x80)
  54074. +#define UT_WRITE 0x00
  54075. +#define UT_READ 0x80
  54076. +
  54077. +#define UT_GET_TYPE(a) ((a) & 0x60)
  54078. +#define UT_STANDARD 0x00
  54079. +#define UT_CLASS 0x20
  54080. +#define UT_VENDOR 0x40
  54081. +
  54082. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  54083. +#define UT_DEVICE 0x00
  54084. +#define UT_INTERFACE 0x01
  54085. +#define UT_ENDPOINT 0x02
  54086. +#define UT_OTHER 0x03
  54087. +
  54088. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  54089. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  54090. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  54091. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  54092. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  54093. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  54094. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  54095. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  54096. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  54097. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  54098. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  54099. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  54100. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  54101. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  54102. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  54103. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  54104. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  54105. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  54106. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  54107. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  54108. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  54109. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  54110. +
  54111. +/* Requests */
  54112. +#define UR_GET_STATUS 0x00
  54113. +#define USTAT_STANDARD_STATUS 0x00
  54114. +#define WUSTAT_WUSB_FEATURE 0x01
  54115. +#define WUSTAT_CHANNEL_INFO 0x02
  54116. +#define WUSTAT_RECEIVED_DATA 0x03
  54117. +#define WUSTAT_MAS_AVAILABILITY 0x04
  54118. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  54119. +#define UR_CLEAR_FEATURE 0x01
  54120. +#define UR_SET_FEATURE 0x03
  54121. +#define UR_SET_AND_TEST_FEATURE 0x0c
  54122. +#define UR_SET_ADDRESS 0x05
  54123. +#define UR_GET_DESCRIPTOR 0x06
  54124. +#define UDESC_DEVICE 0x01
  54125. +#define UDESC_CONFIG 0x02
  54126. +#define UDESC_STRING 0x03
  54127. +#define UDESC_INTERFACE 0x04
  54128. +#define UDESC_ENDPOINT 0x05
  54129. +#define UDESC_SS_USB_COMPANION 0x30
  54130. +#define UDESC_DEVICE_QUALIFIER 0x06
  54131. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  54132. +#define UDESC_INTERFACE_POWER 0x08
  54133. +#define UDESC_OTG 0x09
  54134. +#define WUDESC_SECURITY 0x0c
  54135. +#define WUDESC_KEY 0x0d
  54136. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  54137. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  54138. +#define WUD_KEY_TYPE_ASSOC 0x01
  54139. +#define WUD_KEY_TYPE_GTK 0x02
  54140. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  54141. +#define WUD_KEY_ORIGIN_HOST 0x00
  54142. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  54143. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  54144. +#define WUDESC_BOS 0x0f
  54145. +#define WUDESC_DEVICE_CAPABILITY 0x10
  54146. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  54147. +#define UDESC_BOS 0x0f
  54148. +#define UDESC_DEVICE_CAPABILITY 0x10
  54149. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  54150. +#define UDESC_CS_CONFIG 0x22
  54151. +#define UDESC_CS_STRING 0x23
  54152. +#define UDESC_CS_INTERFACE 0x24
  54153. +#define UDESC_CS_ENDPOINT 0x25
  54154. +#define UDESC_HUB 0x29
  54155. +#define UR_SET_DESCRIPTOR 0x07
  54156. +#define UR_GET_CONFIG 0x08
  54157. +#define UR_SET_CONFIG 0x09
  54158. +#define UR_GET_INTERFACE 0x0a
  54159. +#define UR_SET_INTERFACE 0x0b
  54160. +#define UR_SYNCH_FRAME 0x0c
  54161. +#define WUR_SET_ENCRYPTION 0x0d
  54162. +#define WUR_GET_ENCRYPTION 0x0e
  54163. +#define WUR_SET_HANDSHAKE 0x0f
  54164. +#define WUR_GET_HANDSHAKE 0x10
  54165. +#define WUR_SET_CONNECTION 0x11
  54166. +#define WUR_SET_SECURITY_DATA 0x12
  54167. +#define WUR_GET_SECURITY_DATA 0x13
  54168. +#define WUR_SET_WUSB_DATA 0x14
  54169. +#define WUDATA_DRPIE_INFO 0x01
  54170. +#define WUDATA_TRANSMIT_DATA 0x02
  54171. +#define WUDATA_TRANSMIT_PARAMS 0x03
  54172. +#define WUDATA_RECEIVE_PARAMS 0x04
  54173. +#define WUDATA_TRANSMIT_POWER 0x05
  54174. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  54175. +#define WUR_LOOPBACK_DATA_READ 0x16
  54176. +#define WUR_SET_INTERFACE_DS 0x17
  54177. +
  54178. +/* Feature numbers */
  54179. +#define UF_ENDPOINT_HALT 0
  54180. +#define UF_DEVICE_REMOTE_WAKEUP 1
  54181. +#define UF_TEST_MODE 2
  54182. +#define UF_DEVICE_B_HNP_ENABLE 3
  54183. +#define UF_DEVICE_A_HNP_SUPPORT 4
  54184. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  54185. +#define WUF_WUSB 3
  54186. +#define WUF_TX_DRPIE 0x0
  54187. +#define WUF_DEV_XMIT_PACKET 0x1
  54188. +#define WUF_COUNT_PACKETS 0x2
  54189. +#define WUF_CAPTURE_PACKETS 0x3
  54190. +#define UF_FUNCTION_SUSPEND 0
  54191. +#define UF_U1_ENABLE 48
  54192. +#define UF_U2_ENABLE 49
  54193. +#define UF_LTM_ENABLE 50
  54194. +
  54195. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  54196. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  54197. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  54198. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  54199. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  54200. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  54201. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  54202. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  54203. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  54204. +
  54205. +#ifdef _MSC_VER
  54206. +#include <pshpack1.h>
  54207. +#endif
  54208. +
  54209. +typedef struct {
  54210. + uByte bLength;
  54211. + uByte bDescriptorType;
  54212. + uByte bDescriptorSubtype;
  54213. +} UPACKED usb_descriptor_t;
  54214. +
  54215. +typedef struct {
  54216. + uByte bLength;
  54217. + uByte bDescriptorType;
  54218. +} UPACKED usb_descriptor_header_t;
  54219. +
  54220. +typedef struct {
  54221. + uByte bLength;
  54222. + uByte bDescriptorType;
  54223. + uWord bcdUSB;
  54224. +#define UD_USB_2_0 0x0200
  54225. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  54226. + uByte bDeviceClass;
  54227. + uByte bDeviceSubClass;
  54228. + uByte bDeviceProtocol;
  54229. + uByte bMaxPacketSize;
  54230. + /* The fields below are not part of the initial descriptor. */
  54231. + uWord idVendor;
  54232. + uWord idProduct;
  54233. + uWord bcdDevice;
  54234. + uByte iManufacturer;
  54235. + uByte iProduct;
  54236. + uByte iSerialNumber;
  54237. + uByte bNumConfigurations;
  54238. +} UPACKED usb_device_descriptor_t;
  54239. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  54240. +
  54241. +typedef struct {
  54242. + uByte bLength;
  54243. + uByte bDescriptorType;
  54244. + uWord wTotalLength;
  54245. + uByte bNumInterface;
  54246. + uByte bConfigurationValue;
  54247. + uByte iConfiguration;
  54248. +#define UC_ATT_ONE (1 << 7) /* must be set */
  54249. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  54250. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  54251. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  54252. + uByte bmAttributes;
  54253. +#define UC_BUS_POWERED 0x80
  54254. +#define UC_SELF_POWERED 0x40
  54255. +#define UC_REMOTE_WAKEUP 0x20
  54256. + uByte bMaxPower; /* max current in 2 mA units */
  54257. +#define UC_POWER_FACTOR 2
  54258. +} UPACKED usb_config_descriptor_t;
  54259. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  54260. +
  54261. +typedef struct {
  54262. + uByte bLength;
  54263. + uByte bDescriptorType;
  54264. + uByte bInterfaceNumber;
  54265. + uByte bAlternateSetting;
  54266. + uByte bNumEndpoints;
  54267. + uByte bInterfaceClass;
  54268. + uByte bInterfaceSubClass;
  54269. + uByte bInterfaceProtocol;
  54270. + uByte iInterface;
  54271. +} UPACKED usb_interface_descriptor_t;
  54272. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  54273. +
  54274. +typedef struct {
  54275. + uByte bLength;
  54276. + uByte bDescriptorType;
  54277. + uByte bEndpointAddress;
  54278. +#define UE_GET_DIR(a) ((a) & 0x80)
  54279. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  54280. +#define UE_DIR_IN 0x80
  54281. +#define UE_DIR_OUT 0x00
  54282. +#define UE_ADDR 0x0f
  54283. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  54284. + uByte bmAttributes;
  54285. +#define UE_XFERTYPE 0x03
  54286. +#define UE_CONTROL 0x00
  54287. +#define UE_ISOCHRONOUS 0x01
  54288. +#define UE_BULK 0x02
  54289. +#define UE_INTERRUPT 0x03
  54290. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  54291. +#define UE_ISO_TYPE 0x0c
  54292. +#define UE_ISO_ASYNC 0x04
  54293. +#define UE_ISO_ADAPT 0x08
  54294. +#define UE_ISO_SYNC 0x0c
  54295. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  54296. + uWord wMaxPacketSize;
  54297. + uByte bInterval;
  54298. +} UPACKED usb_endpoint_descriptor_t;
  54299. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  54300. +
  54301. +typedef struct ss_endpoint_companion_descriptor {
  54302. + uByte bLength;
  54303. + uByte bDescriptorType;
  54304. + uByte bMaxBurst;
  54305. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  54306. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  54307. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  54308. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  54309. + uByte bmAttributes;
  54310. + uWord wBytesPerInterval;
  54311. +} UPACKED ss_endpoint_companion_descriptor_t;
  54312. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  54313. +
  54314. +typedef struct {
  54315. + uByte bLength;
  54316. + uByte bDescriptorType;
  54317. + uWord bString[127];
  54318. +} UPACKED usb_string_descriptor_t;
  54319. +#define USB_MAX_STRING_LEN 128
  54320. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  54321. +
  54322. +/* Hub specific request */
  54323. +#define UR_GET_BUS_STATE 0x02
  54324. +#define UR_CLEAR_TT_BUFFER 0x08
  54325. +#define UR_RESET_TT 0x09
  54326. +#define UR_GET_TT_STATE 0x0a
  54327. +#define UR_STOP_TT 0x0b
  54328. +
  54329. +/* Hub features */
  54330. +#define UHF_C_HUB_LOCAL_POWER 0
  54331. +#define UHF_C_HUB_OVER_CURRENT 1
  54332. +#define UHF_PORT_CONNECTION 0
  54333. +#define UHF_PORT_ENABLE 1
  54334. +#define UHF_PORT_SUSPEND 2
  54335. +#define UHF_PORT_OVER_CURRENT 3
  54336. +#define UHF_PORT_RESET 4
  54337. +#define UHF_PORT_L1 5
  54338. +#define UHF_PORT_POWER 8
  54339. +#define UHF_PORT_LOW_SPEED 9
  54340. +#define UHF_PORT_HIGH_SPEED 10
  54341. +#define UHF_C_PORT_CONNECTION 16
  54342. +#define UHF_C_PORT_ENABLE 17
  54343. +#define UHF_C_PORT_SUSPEND 18
  54344. +#define UHF_C_PORT_OVER_CURRENT 19
  54345. +#define UHF_C_PORT_RESET 20
  54346. +#define UHF_C_PORT_L1 23
  54347. +#define UHF_PORT_TEST 21
  54348. +#define UHF_PORT_INDICATOR 22
  54349. +
  54350. +typedef struct {
  54351. + uByte bDescLength;
  54352. + uByte bDescriptorType;
  54353. + uByte bNbrPorts;
  54354. + uWord wHubCharacteristics;
  54355. +#define UHD_PWR 0x0003
  54356. +#define UHD_PWR_GANGED 0x0000
  54357. +#define UHD_PWR_INDIVIDUAL 0x0001
  54358. +#define UHD_PWR_NO_SWITCH 0x0002
  54359. +#define UHD_COMPOUND 0x0004
  54360. +#define UHD_OC 0x0018
  54361. +#define UHD_OC_GLOBAL 0x0000
  54362. +#define UHD_OC_INDIVIDUAL 0x0008
  54363. +#define UHD_OC_NONE 0x0010
  54364. +#define UHD_TT_THINK 0x0060
  54365. +#define UHD_TT_THINK_8 0x0000
  54366. +#define UHD_TT_THINK_16 0x0020
  54367. +#define UHD_TT_THINK_24 0x0040
  54368. +#define UHD_TT_THINK_32 0x0060
  54369. +#define UHD_PORT_IND 0x0080
  54370. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  54371. +#define UHD_PWRON_FACTOR 2
  54372. + uByte bHubContrCurrent;
  54373. + uByte DeviceRemovable[32]; /* max 255 ports */
  54374. +#define UHD_NOT_REMOV(desc, i) \
  54375. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  54376. + /* deprecated */ uByte PortPowerCtrlMask[1];
  54377. +} UPACKED usb_hub_descriptor_t;
  54378. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  54379. +
  54380. +typedef struct {
  54381. + uByte bLength;
  54382. + uByte bDescriptorType;
  54383. + uWord bcdUSB;
  54384. + uByte bDeviceClass;
  54385. + uByte bDeviceSubClass;
  54386. + uByte bDeviceProtocol;
  54387. + uByte bMaxPacketSize0;
  54388. + uByte bNumConfigurations;
  54389. + uByte bReserved;
  54390. +} UPACKED usb_device_qualifier_t;
  54391. +#define USB_DEVICE_QUALIFIER_SIZE 10
  54392. +
  54393. +typedef struct {
  54394. + uByte bLength;
  54395. + uByte bDescriptorType;
  54396. + uByte bmAttributes;
  54397. +#define UOTG_SRP 0x01
  54398. +#define UOTG_HNP 0x02
  54399. +} UPACKED usb_otg_descriptor_t;
  54400. +
  54401. +/* OTG feature selectors */
  54402. +#define UOTG_B_HNP_ENABLE 3
  54403. +#define UOTG_A_HNP_SUPPORT 4
  54404. +#define UOTG_A_ALT_HNP_SUPPORT 5
  54405. +
  54406. +typedef struct {
  54407. + uWord wStatus;
  54408. +/* Device status flags */
  54409. +#define UDS_SELF_POWERED 0x0001
  54410. +#define UDS_REMOTE_WAKEUP 0x0002
  54411. +/* Endpoint status flags */
  54412. +#define UES_HALT 0x0001
  54413. +} UPACKED usb_status_t;
  54414. +
  54415. +typedef struct {
  54416. + uWord wHubStatus;
  54417. +#define UHS_LOCAL_POWER 0x0001
  54418. +#define UHS_OVER_CURRENT 0x0002
  54419. + uWord wHubChange;
  54420. +} UPACKED usb_hub_status_t;
  54421. +
  54422. +typedef struct {
  54423. + uWord wPortStatus;
  54424. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  54425. +#define UPS_PORT_ENABLED 0x0002
  54426. +#define UPS_SUSPEND 0x0004
  54427. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  54428. +#define UPS_RESET 0x0010
  54429. +#define UPS_PORT_POWER 0x0100
  54430. +#define UPS_LOW_SPEED 0x0200
  54431. +#define UPS_HIGH_SPEED 0x0400
  54432. +#define UPS_PORT_TEST 0x0800
  54433. +#define UPS_PORT_INDICATOR 0x1000
  54434. + uWord wPortChange;
  54435. +#define UPS_C_CONNECT_STATUS 0x0001
  54436. +#define UPS_C_PORT_ENABLED 0x0002
  54437. +#define UPS_C_SUSPEND 0x0004
  54438. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  54439. +#define UPS_C_PORT_RESET 0x0010
  54440. +} UPACKED usb_port_status_t;
  54441. +
  54442. +#ifdef _MSC_VER
  54443. +#include <poppack.h>
  54444. +#endif
  54445. +
  54446. +/* Device class codes */
  54447. +#define UDCLASS_IN_INTERFACE 0x00
  54448. +#define UDCLASS_COMM 0x02
  54449. +#define UDCLASS_HUB 0x09
  54450. +#define UDSUBCLASS_HUB 0x00
  54451. +#define UDPROTO_FSHUB 0x00
  54452. +#define UDPROTO_HSHUBSTT 0x01
  54453. +#define UDPROTO_HSHUBMTT 0x02
  54454. +#define UDCLASS_DIAGNOSTIC 0xdc
  54455. +#define UDCLASS_WIRELESS 0xe0
  54456. +#define UDSUBCLASS_RF 0x01
  54457. +#define UDPROTO_BLUETOOTH 0x01
  54458. +#define UDCLASS_VENDOR 0xff
  54459. +
  54460. +/* Interface class codes */
  54461. +#define UICLASS_UNSPEC 0x00
  54462. +
  54463. +#define UICLASS_AUDIO 0x01
  54464. +#define UISUBCLASS_AUDIOCONTROL 1
  54465. +#define UISUBCLASS_AUDIOSTREAM 2
  54466. +#define UISUBCLASS_MIDISTREAM 3
  54467. +
  54468. +#define UICLASS_CDC 0x02 /* communication */
  54469. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  54470. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  54471. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  54472. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  54473. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  54474. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  54475. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  54476. +#define UIPROTO_CDC_AT 1
  54477. +
  54478. +#define UICLASS_HID 0x03
  54479. +#define UISUBCLASS_BOOT 1
  54480. +#define UIPROTO_BOOT_KEYBOARD 1
  54481. +
  54482. +#define UICLASS_PHYSICAL 0x05
  54483. +
  54484. +#define UICLASS_IMAGE 0x06
  54485. +
  54486. +#define UICLASS_PRINTER 0x07
  54487. +#define UISUBCLASS_PRINTER 1
  54488. +#define UIPROTO_PRINTER_UNI 1
  54489. +#define UIPROTO_PRINTER_BI 2
  54490. +#define UIPROTO_PRINTER_1284 3
  54491. +
  54492. +#define UICLASS_MASS 0x08
  54493. +#define UISUBCLASS_RBC 1
  54494. +#define UISUBCLASS_SFF8020I 2
  54495. +#define UISUBCLASS_QIC157 3
  54496. +#define UISUBCLASS_UFI 4
  54497. +#define UISUBCLASS_SFF8070I 5
  54498. +#define UISUBCLASS_SCSI 6
  54499. +#define UIPROTO_MASS_CBI_I 0
  54500. +#define UIPROTO_MASS_CBI 1
  54501. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  54502. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  54503. +
  54504. +#define UICLASS_HUB 0x09
  54505. +#define UISUBCLASS_HUB 0
  54506. +#define UIPROTO_FSHUB 0
  54507. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  54508. +#define UIPROTO_HSHUBMTT 1
  54509. +
  54510. +#define UICLASS_CDC_DATA 0x0a
  54511. +#define UISUBCLASS_DATA 0
  54512. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  54513. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  54514. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  54515. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  54516. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  54517. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  54518. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  54519. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  54520. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  54521. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  54522. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  54523. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  54524. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  54525. +
  54526. +#define UICLASS_SMARTCARD 0x0b
  54527. +
  54528. +/*#define UICLASS_FIRM_UPD 0x0c*/
  54529. +
  54530. +#define UICLASS_SECURITY 0x0d
  54531. +
  54532. +#define UICLASS_DIAGNOSTIC 0xdc
  54533. +
  54534. +#define UICLASS_WIRELESS 0xe0
  54535. +#define UISUBCLASS_RF 0x01
  54536. +#define UIPROTO_BLUETOOTH 0x01
  54537. +
  54538. +#define UICLASS_APPL_SPEC 0xfe
  54539. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  54540. +#define UISUBCLASS_IRDA 2
  54541. +#define UIPROTO_IRDA 0
  54542. +
  54543. +#define UICLASS_VENDOR 0xff
  54544. +
  54545. +#define USB_HUB_MAX_DEPTH 5
  54546. +
  54547. +/*
  54548. + * Minimum time a device needs to be powered down to go through
  54549. + * a power cycle. XXX Are these time in the spec?
  54550. + */
  54551. +#define USB_POWER_DOWN_TIME 200 /* ms */
  54552. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  54553. +
  54554. +#if 0
  54555. +/* These are the values from the spec. */
  54556. +#define USB_PORT_RESET_DELAY 10 /* ms */
  54557. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  54558. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  54559. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  54560. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  54561. +#define USB_RESUME_DELAY (20*5) /* ms */
  54562. +#define USB_RESUME_WAIT 10 /* ms */
  54563. +#define USB_RESUME_RECOVERY 10 /* ms */
  54564. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  54565. +#else
  54566. +/* Allow for marginal (i.e. non-conforming) devices. */
  54567. +#define USB_PORT_RESET_DELAY 50 /* ms */
  54568. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  54569. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  54570. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  54571. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  54572. +#define USB_RESUME_DELAY (50*5) /* ms */
  54573. +#define USB_RESUME_WAIT 50 /* ms */
  54574. +#define USB_RESUME_RECOVERY 50 /* ms */
  54575. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  54576. +#endif
  54577. +
  54578. +#define USB_MIN_POWER 100 /* mA */
  54579. +#define USB_MAX_POWER 500 /* mA */
  54580. +
  54581. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  54582. +
  54583. +#define USB_UNCONFIG_NO 0
  54584. +#define USB_UNCONFIG_INDEX (-1)
  54585. +
  54586. +/*** ioctl() related stuff ***/
  54587. +
  54588. +struct usb_ctl_request {
  54589. + int ucr_addr;
  54590. + usb_device_request_t ucr_request;
  54591. + void *ucr_data;
  54592. + int ucr_flags;
  54593. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  54594. + int ucr_actlen; /* actual length transferred */
  54595. +};
  54596. +
  54597. +struct usb_alt_interface {
  54598. + int uai_config_index;
  54599. + int uai_interface_index;
  54600. + int uai_alt_no;
  54601. +};
  54602. +
  54603. +#define USB_CURRENT_CONFIG_INDEX (-1)
  54604. +#define USB_CURRENT_ALT_INDEX (-1)
  54605. +
  54606. +struct usb_config_desc {
  54607. + int ucd_config_index;
  54608. + usb_config_descriptor_t ucd_desc;
  54609. +};
  54610. +
  54611. +struct usb_interface_desc {
  54612. + int uid_config_index;
  54613. + int uid_interface_index;
  54614. + int uid_alt_index;
  54615. + usb_interface_descriptor_t uid_desc;
  54616. +};
  54617. +
  54618. +struct usb_endpoint_desc {
  54619. + int ued_config_index;
  54620. + int ued_interface_index;
  54621. + int ued_alt_index;
  54622. + int ued_endpoint_index;
  54623. + usb_endpoint_descriptor_t ued_desc;
  54624. +};
  54625. +
  54626. +struct usb_full_desc {
  54627. + int ufd_config_index;
  54628. + u_int ufd_size;
  54629. + u_char *ufd_data;
  54630. +};
  54631. +
  54632. +struct usb_string_desc {
  54633. + int usd_string_index;
  54634. + int usd_language_id;
  54635. + usb_string_descriptor_t usd_desc;
  54636. +};
  54637. +
  54638. +struct usb_ctl_report_desc {
  54639. + int ucrd_size;
  54640. + u_char ucrd_data[1024]; /* filled data size will vary */
  54641. +};
  54642. +
  54643. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  54644. +
  54645. +#define USB_MAX_DEVNAMES 4
  54646. +#define USB_MAX_DEVNAMELEN 16
  54647. +struct usb_device_info {
  54648. + u_int8_t udi_bus;
  54649. + u_int8_t udi_addr; /* device address */
  54650. + usb_event_cookie_t udi_cookie;
  54651. + char udi_product[USB_MAX_STRING_LEN];
  54652. + char udi_vendor[USB_MAX_STRING_LEN];
  54653. + char udi_release[8];
  54654. + u_int16_t udi_productNo;
  54655. + u_int16_t udi_vendorNo;
  54656. + u_int16_t udi_releaseNo;
  54657. + u_int8_t udi_class;
  54658. + u_int8_t udi_subclass;
  54659. + u_int8_t udi_protocol;
  54660. + u_int8_t udi_config;
  54661. + u_int8_t udi_speed;
  54662. +#define USB_SPEED_UNKNOWN 0
  54663. +#define USB_SPEED_LOW 1
  54664. +#define USB_SPEED_FULL 2
  54665. +#define USB_SPEED_HIGH 3
  54666. +#define USB_SPEED_VARIABLE 4
  54667. +#define USB_SPEED_SUPER 5
  54668. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  54669. + int udi_nports;
  54670. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  54671. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  54672. +#define USB_PORT_ENABLED 0xff
  54673. +#define USB_PORT_SUSPENDED 0xfe
  54674. +#define USB_PORT_POWERED 0xfd
  54675. +#define USB_PORT_DISABLED 0xfc
  54676. +};
  54677. +
  54678. +struct usb_ctl_report {
  54679. + int ucr_report;
  54680. + u_char ucr_data[1024]; /* filled data size will vary */
  54681. +};
  54682. +
  54683. +struct usb_device_stats {
  54684. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  54685. +};
  54686. +
  54687. +#define WUSB_MIN_IE 0x80
  54688. +#define WUSB_WCTA_IE 0x80
  54689. +#define WUSB_WCONNECTACK_IE 0x81
  54690. +#define WUSB_WHOSTINFO_IE 0x82
  54691. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  54692. +#define WUHI_CA_RECONN 0x00
  54693. +#define WUHI_CA_LIMITED 0x01
  54694. +#define WUHI_CA_ALL 0x03
  54695. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  54696. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  54697. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  54698. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  54699. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  54700. +#define WUSB_WWORK_IE 0x87
  54701. +#define WUSB_WCHANNEL_STOP_IE 0x88
  54702. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  54703. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  54704. +#define WUSB_WRESETDEVICE_IE 0x8B
  54705. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  54706. +#define WUSB_MAX_IE 0x8C
  54707. +
  54708. +/* Device Notification Types */
  54709. +
  54710. +#define WUSB_DN_MIN 0x01
  54711. +#define WUSB_DN_CONNECT 0x01
  54712. +# define WUSB_DA_OLDCONN 0x00
  54713. +# define WUSB_DA_NEWCONN 0x01
  54714. +# define WUSB_DA_SELF_BEACON 0x02
  54715. +# define WUSB_DA_DIR_BEACON 0x04
  54716. +# define WUSB_DA_NO_BEACON 0x06
  54717. +#define WUSB_DN_DISCONNECT 0x02
  54718. +#define WUSB_DN_EPRDY 0x03
  54719. +#define WUSB_DN_MASAVAILCHANGED 0x04
  54720. +#define WUSB_DN_REMOTEWAKEUP 0x05
  54721. +#define WUSB_DN_SLEEP 0x06
  54722. +#define WUSB_DN_ALIVE 0x07
  54723. +#define WUSB_DN_MAX 0x07
  54724. +
  54725. +#ifdef _MSC_VER
  54726. +#include <pshpack1.h>
  54727. +#endif
  54728. +
  54729. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  54730. +typedef struct wusb_hndshk_data {
  54731. + uByte bMessageNumber;
  54732. + uByte bStatus;
  54733. + uByte tTKID[3];
  54734. + uByte bReserved;
  54735. + uByte CDID[16];
  54736. + uByte Nonce[16];
  54737. + uByte MIC[8];
  54738. +} UPACKED wusb_hndshk_data_t;
  54739. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  54740. +
  54741. +/* WUSB Connection Context */
  54742. +typedef struct wusb_conn_context {
  54743. + uByte CHID [16];
  54744. + uByte CDID [16];
  54745. + uByte CK [16];
  54746. +} UPACKED wusb_conn_context_t;
  54747. +
  54748. +/* WUSB Security Descriptor */
  54749. +typedef struct wusb_security_desc {
  54750. + uByte bLength;
  54751. + uByte bDescriptorType;
  54752. + uWord wTotalLength;
  54753. + uByte bNumEncryptionTypes;
  54754. +} UPACKED wusb_security_desc_t;
  54755. +
  54756. +/* WUSB Encryption Type Descriptor */
  54757. +typedef struct wusb_encrypt_type_desc {
  54758. + uByte bLength;
  54759. + uByte bDescriptorType;
  54760. +
  54761. + uByte bEncryptionType;
  54762. +#define WUETD_UNSECURE 0
  54763. +#define WUETD_WIRED 1
  54764. +#define WUETD_CCM_1 2
  54765. +#define WUETD_RSA_1 3
  54766. +
  54767. + uByte bEncryptionValue;
  54768. + uByte bAuthKeyIndex;
  54769. +} UPACKED wusb_encrypt_type_desc_t;
  54770. +
  54771. +/* WUSB Key Descriptor */
  54772. +typedef struct wusb_key_desc {
  54773. + uByte bLength;
  54774. + uByte bDescriptorType;
  54775. + uByte tTKID[3];
  54776. + uByte bReserved;
  54777. + uByte KeyData[1]; /* variable length */
  54778. +} UPACKED wusb_key_desc_t;
  54779. +
  54780. +/* WUSB BOS Descriptor (Binary device Object Store) */
  54781. +typedef struct wusb_bos_desc {
  54782. + uByte bLength;
  54783. + uByte bDescriptorType;
  54784. + uWord wTotalLength;
  54785. + uByte bNumDeviceCaps;
  54786. +} UPACKED wusb_bos_desc_t;
  54787. +
  54788. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  54789. +typedef struct usb_dev_cap_20_ext_desc {
  54790. + uByte bLength;
  54791. + uByte bDescriptorType;
  54792. + uByte bDevCapabilityType;
  54793. +#define USB_20_EXT_LPM 0x02
  54794. + uDWord bmAttributes;
  54795. +} UPACKED usb_dev_cap_20_ext_desc_t;
  54796. +
  54797. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  54798. +typedef struct usb_dev_cap_ss_usb {
  54799. + uByte bLength;
  54800. + uByte bDescriptorType;
  54801. + uByte bDevCapabilityType;
  54802. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  54803. + uByte bmAttributes;
  54804. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  54805. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  54806. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  54807. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  54808. + uWord wSpeedsSupported;
  54809. + uByte bFunctionalitySupport;
  54810. + uByte bU1DevExitLat;
  54811. + uWord wU2DevExitLat;
  54812. +} UPACKED usb_dev_cap_ss_usb_t;
  54813. +
  54814. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  54815. +typedef struct usb_dev_cap_container_id {
  54816. + uByte bLength;
  54817. + uByte bDescriptorType;
  54818. + uByte bDevCapabilityType;
  54819. + uByte bReserved;
  54820. + uByte containerID[16];
  54821. +} UPACKED usb_dev_cap_container_id_t;
  54822. +
  54823. +/* Device Capability Type Codes */
  54824. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  54825. +
  54826. +/* Device Capability Descriptor */
  54827. +typedef struct wusb_dev_cap_desc {
  54828. + uByte bLength;
  54829. + uByte bDescriptorType;
  54830. + uByte bDevCapabilityType;
  54831. + uByte caps[1]; /* Variable length */
  54832. +} UPACKED wusb_dev_cap_desc_t;
  54833. +
  54834. +/* Device Capability Descriptor */
  54835. +typedef struct wusb_dev_cap_uwb_desc {
  54836. + uByte bLength;
  54837. + uByte bDescriptorType;
  54838. + uByte bDevCapabilityType;
  54839. + uByte bmAttributes;
  54840. + uWord wPHYRates; /* Bitmap */
  54841. + uByte bmTFITXPowerInfo;
  54842. + uByte bmFFITXPowerInfo;
  54843. + uWord bmBandGroup;
  54844. + uByte bReserved;
  54845. +} UPACKED wusb_dev_cap_uwb_desc_t;
  54846. +
  54847. +/* Wireless USB Endpoint Companion Descriptor */
  54848. +typedef struct wusb_endpoint_companion_desc {
  54849. + uByte bLength;
  54850. + uByte bDescriptorType;
  54851. + uByte bMaxBurst;
  54852. + uByte bMaxSequence;
  54853. + uWord wMaxStreamDelay;
  54854. + uWord wOverTheAirPacketSize;
  54855. + uByte bOverTheAirInterval;
  54856. + uByte bmCompAttributes;
  54857. +} UPACKED wusb_endpoint_companion_desc_t;
  54858. +
  54859. +/* Wireless USB Numeric Association M1 Data Structure */
  54860. +typedef struct wusb_m1_data {
  54861. + uByte version;
  54862. + uWord langId;
  54863. + uByte deviceFriendlyNameLength;
  54864. + uByte sha_256_m3[32];
  54865. + uByte deviceFriendlyName[256];
  54866. +} UPACKED wusb_m1_data_t;
  54867. +
  54868. +typedef struct wusb_m2_data {
  54869. + uByte version;
  54870. + uWord langId;
  54871. + uByte hostFriendlyNameLength;
  54872. + uByte pkh[384];
  54873. + uByte hostFriendlyName[256];
  54874. +} UPACKED wusb_m2_data_t;
  54875. +
  54876. +typedef struct wusb_m3_data {
  54877. + uByte pkd[384];
  54878. + uByte nd;
  54879. +} UPACKED wusb_m3_data_t;
  54880. +
  54881. +typedef struct wusb_m4_data {
  54882. + uDWord _attributeTypeIdAndLength_1;
  54883. + uWord associationTypeId;
  54884. +
  54885. + uDWord _attributeTypeIdAndLength_2;
  54886. + uWord associationSubTypeId;
  54887. +
  54888. + uDWord _attributeTypeIdAndLength_3;
  54889. + uDWord length;
  54890. +
  54891. + uDWord _attributeTypeIdAndLength_4;
  54892. + uDWord associationStatus;
  54893. +
  54894. + uDWord _attributeTypeIdAndLength_5;
  54895. + uByte chid[16];
  54896. +
  54897. + uDWord _attributeTypeIdAndLength_6;
  54898. + uByte cdid[16];
  54899. +
  54900. + uDWord _attributeTypeIdAndLength_7;
  54901. + uByte bandGroups[2];
  54902. +} UPACKED wusb_m4_data_t;
  54903. +
  54904. +#ifdef _MSC_VER
  54905. +#include <poppack.h>
  54906. +#endif
  54907. +
  54908. +#ifdef __cplusplus
  54909. +}
  54910. +#endif
  54911. +
  54912. +#endif /* _USB_H_ */
  54913. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/doc/doxygen.cfg linux-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  54914. --- linux-3.17.5/drivers/usb/host/dwc_otg/doc/doxygen.cfg 1969-12-31 18:00:00.000000000 -0600
  54915. +++ linux-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg 2014-12-11 14:02:55.356418001 -0600
  54916. @@ -0,0 +1,224 @@
  54917. +# Doxyfile 1.3.9.1
  54918. +
  54919. +#---------------------------------------------------------------------------
  54920. +# Project related configuration options
  54921. +#---------------------------------------------------------------------------
  54922. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  54923. +PROJECT_NUMBER = v3.00a
  54924. +OUTPUT_DIRECTORY = ./doc/
  54925. +CREATE_SUBDIRS = NO
  54926. +OUTPUT_LANGUAGE = English
  54927. +BRIEF_MEMBER_DESC = YES
  54928. +REPEAT_BRIEF = YES
  54929. +ABBREVIATE_BRIEF = "The $name class" \
  54930. + "The $name widget" \
  54931. + "The $name file" \
  54932. + is \
  54933. + provides \
  54934. + specifies \
  54935. + contains \
  54936. + represents \
  54937. + a \
  54938. + an \
  54939. + the
  54940. +ALWAYS_DETAILED_SEC = NO
  54941. +INLINE_INHERITED_MEMB = NO
  54942. +FULL_PATH_NAMES = NO
  54943. +STRIP_FROM_PATH =
  54944. +STRIP_FROM_INC_PATH =
  54945. +SHORT_NAMES = NO
  54946. +JAVADOC_AUTOBRIEF = YES
  54947. +MULTILINE_CPP_IS_BRIEF = NO
  54948. +INHERIT_DOCS = YES
  54949. +DISTRIBUTE_GROUP_DOC = NO
  54950. +TAB_SIZE = 8
  54951. +ALIASES =
  54952. +OPTIMIZE_OUTPUT_FOR_C = YES
  54953. +OPTIMIZE_OUTPUT_JAVA = NO
  54954. +SUBGROUPING = YES
  54955. +#---------------------------------------------------------------------------
  54956. +# Build related configuration options
  54957. +#---------------------------------------------------------------------------
  54958. +EXTRACT_ALL = NO
  54959. +EXTRACT_PRIVATE = YES
  54960. +EXTRACT_STATIC = YES
  54961. +EXTRACT_LOCAL_CLASSES = YES
  54962. +EXTRACT_LOCAL_METHODS = NO
  54963. +HIDE_UNDOC_MEMBERS = NO
  54964. +HIDE_UNDOC_CLASSES = NO
  54965. +HIDE_FRIEND_COMPOUNDS = NO
  54966. +HIDE_IN_BODY_DOCS = NO
  54967. +INTERNAL_DOCS = NO
  54968. +CASE_SENSE_NAMES = NO
  54969. +HIDE_SCOPE_NAMES = NO
  54970. +SHOW_INCLUDE_FILES = YES
  54971. +INLINE_INFO = YES
  54972. +SORT_MEMBER_DOCS = NO
  54973. +SORT_BRIEF_DOCS = NO
  54974. +SORT_BY_SCOPE_NAME = NO
  54975. +GENERATE_TODOLIST = YES
  54976. +GENERATE_TESTLIST = YES
  54977. +GENERATE_BUGLIST = YES
  54978. +GENERATE_DEPRECATEDLIST= YES
  54979. +ENABLED_SECTIONS =
  54980. +MAX_INITIALIZER_LINES = 30
  54981. +SHOW_USED_FILES = YES
  54982. +SHOW_DIRECTORIES = YES
  54983. +#---------------------------------------------------------------------------
  54984. +# configuration options related to warning and progress messages
  54985. +#---------------------------------------------------------------------------
  54986. +QUIET = YES
  54987. +WARNINGS = YES
  54988. +WARN_IF_UNDOCUMENTED = NO
  54989. +WARN_IF_DOC_ERROR = YES
  54990. +WARN_FORMAT = "$file:$line: $text"
  54991. +WARN_LOGFILE =
  54992. +#---------------------------------------------------------------------------
  54993. +# configuration options related to the input files
  54994. +#---------------------------------------------------------------------------
  54995. +INPUT = .
  54996. +FILE_PATTERNS = *.c \
  54997. + *.h \
  54998. + ./linux/*.c \
  54999. + ./linux/*.h
  55000. +RECURSIVE = NO
  55001. +EXCLUDE = ./test/ \
  55002. + ./dwc_otg/.AppleDouble/
  55003. +EXCLUDE_SYMLINKS = YES
  55004. +EXCLUDE_PATTERNS = *.mod.*
  55005. +EXAMPLE_PATH =
  55006. +EXAMPLE_PATTERNS = *
  55007. +EXAMPLE_RECURSIVE = NO
  55008. +IMAGE_PATH =
  55009. +INPUT_FILTER =
  55010. +FILTER_PATTERNS =
  55011. +FILTER_SOURCE_FILES = NO
  55012. +#---------------------------------------------------------------------------
  55013. +# configuration options related to source browsing
  55014. +#---------------------------------------------------------------------------
  55015. +SOURCE_BROWSER = YES
  55016. +INLINE_SOURCES = NO
  55017. +STRIP_CODE_COMMENTS = YES
  55018. +REFERENCED_BY_RELATION = NO
  55019. +REFERENCES_RELATION = NO
  55020. +VERBATIM_HEADERS = NO
  55021. +#---------------------------------------------------------------------------
  55022. +# configuration options related to the alphabetical class index
  55023. +#---------------------------------------------------------------------------
  55024. +ALPHABETICAL_INDEX = NO
  55025. +COLS_IN_ALPHA_INDEX = 5
  55026. +IGNORE_PREFIX =
  55027. +#---------------------------------------------------------------------------
  55028. +# configuration options related to the HTML output
  55029. +#---------------------------------------------------------------------------
  55030. +GENERATE_HTML = YES
  55031. +HTML_OUTPUT = html
  55032. +HTML_FILE_EXTENSION = .html
  55033. +HTML_HEADER =
  55034. +HTML_FOOTER =
  55035. +HTML_STYLESHEET =
  55036. +HTML_ALIGN_MEMBERS = YES
  55037. +GENERATE_HTMLHELP = NO
  55038. +CHM_FILE =
  55039. +HHC_LOCATION =
  55040. +GENERATE_CHI = NO
  55041. +BINARY_TOC = NO
  55042. +TOC_EXPAND = NO
  55043. +DISABLE_INDEX = NO
  55044. +ENUM_VALUES_PER_LINE = 4
  55045. +GENERATE_TREEVIEW = YES
  55046. +TREEVIEW_WIDTH = 250
  55047. +#---------------------------------------------------------------------------
  55048. +# configuration options related to the LaTeX output
  55049. +#---------------------------------------------------------------------------
  55050. +GENERATE_LATEX = NO
  55051. +LATEX_OUTPUT = latex
  55052. +LATEX_CMD_NAME = latex
  55053. +MAKEINDEX_CMD_NAME = makeindex
  55054. +COMPACT_LATEX = NO
  55055. +PAPER_TYPE = a4wide
  55056. +EXTRA_PACKAGES =
  55057. +LATEX_HEADER =
  55058. +PDF_HYPERLINKS = NO
  55059. +USE_PDFLATEX = NO
  55060. +LATEX_BATCHMODE = NO
  55061. +LATEX_HIDE_INDICES = NO
  55062. +#---------------------------------------------------------------------------
  55063. +# configuration options related to the RTF output
  55064. +#---------------------------------------------------------------------------
  55065. +GENERATE_RTF = NO
  55066. +RTF_OUTPUT = rtf
  55067. +COMPACT_RTF = NO
  55068. +RTF_HYPERLINKS = NO
  55069. +RTF_STYLESHEET_FILE =
  55070. +RTF_EXTENSIONS_FILE =
  55071. +#---------------------------------------------------------------------------
  55072. +# configuration options related to the man page output
  55073. +#---------------------------------------------------------------------------
  55074. +GENERATE_MAN = NO
  55075. +MAN_OUTPUT = man
  55076. +MAN_EXTENSION = .3
  55077. +MAN_LINKS = NO
  55078. +#---------------------------------------------------------------------------
  55079. +# configuration options related to the XML output
  55080. +#---------------------------------------------------------------------------
  55081. +GENERATE_XML = NO
  55082. +XML_OUTPUT = xml
  55083. +XML_SCHEMA =
  55084. +XML_DTD =
  55085. +XML_PROGRAMLISTING = YES
  55086. +#---------------------------------------------------------------------------
  55087. +# configuration options for the AutoGen Definitions output
  55088. +#---------------------------------------------------------------------------
  55089. +GENERATE_AUTOGEN_DEF = NO
  55090. +#---------------------------------------------------------------------------
  55091. +# configuration options related to the Perl module output
  55092. +#---------------------------------------------------------------------------
  55093. +GENERATE_PERLMOD = NO
  55094. +PERLMOD_LATEX = NO
  55095. +PERLMOD_PRETTY = YES
  55096. +PERLMOD_MAKEVAR_PREFIX =
  55097. +#---------------------------------------------------------------------------
  55098. +# Configuration options related to the preprocessor
  55099. +#---------------------------------------------------------------------------
  55100. +ENABLE_PREPROCESSING = YES
  55101. +MACRO_EXPANSION = YES
  55102. +EXPAND_ONLY_PREDEF = YES
  55103. +SEARCH_INCLUDES = YES
  55104. +INCLUDE_PATH =
  55105. +INCLUDE_FILE_PATTERNS =
  55106. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  55107. +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
  55108. +SKIP_FUNCTION_MACROS = NO
  55109. +#---------------------------------------------------------------------------
  55110. +# Configuration::additions related to external references
  55111. +#---------------------------------------------------------------------------
  55112. +TAGFILES =
  55113. +GENERATE_TAGFILE =
  55114. +ALLEXTERNALS = NO
  55115. +EXTERNAL_GROUPS = YES
  55116. +PERL_PATH = /usr/bin/perl
  55117. +#---------------------------------------------------------------------------
  55118. +# Configuration options related to the dot tool
  55119. +#---------------------------------------------------------------------------
  55120. +CLASS_DIAGRAMS = YES
  55121. +HIDE_UNDOC_RELATIONS = YES
  55122. +HAVE_DOT = NO
  55123. +CLASS_GRAPH = YES
  55124. +COLLABORATION_GRAPH = YES
  55125. +UML_LOOK = NO
  55126. +TEMPLATE_RELATIONS = NO
  55127. +INCLUDE_GRAPH = YES
  55128. +INCLUDED_BY_GRAPH = YES
  55129. +CALL_GRAPH = NO
  55130. +GRAPHICAL_HIERARCHY = YES
  55131. +DOT_IMAGE_FORMAT = png
  55132. +DOT_PATH =
  55133. +DOTFILE_DIRS =
  55134. +MAX_DOT_GRAPH_DEPTH = 1000
  55135. +GENERATE_LEGEND = YES
  55136. +DOT_CLEANUP = YES
  55137. +#---------------------------------------------------------------------------
  55138. +# Configuration::additions related to the search engine
  55139. +#---------------------------------------------------------------------------
  55140. +SEARCHENGINE = NO
  55141. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dummy_audio.c linux-rpi/drivers/usb/host/dwc_otg/dummy_audio.c
  55142. --- linux-3.17.5/drivers/usb/host/dwc_otg/dummy_audio.c 1969-12-31 18:00:00.000000000 -0600
  55143. +++ linux-rpi/drivers/usb/host/dwc_otg/dummy_audio.c 2014-12-11 14:02:55.388418001 -0600
  55144. @@ -0,0 +1,1575 @@
  55145. +/*
  55146. + * zero.c -- Gadget Zero, for USB development
  55147. + *
  55148. + * Copyright (C) 2003-2004 David Brownell
  55149. + * All rights reserved.
  55150. + *
  55151. + * Redistribution and use in source and binary forms, with or without
  55152. + * modification, are permitted provided that the following conditions
  55153. + * are met:
  55154. + * 1. Redistributions of source code must retain the above copyright
  55155. + * notice, this list of conditions, and the following disclaimer,
  55156. + * without modification.
  55157. + * 2. Redistributions in binary form must reproduce the above copyright
  55158. + * notice, this list of conditions and the following disclaimer in the
  55159. + * documentation and/or other materials provided with the distribution.
  55160. + * 3. The names of the above-listed copyright holders may not be used
  55161. + * to endorse or promote products derived from this software without
  55162. + * specific prior written permission.
  55163. + *
  55164. + * ALTERNATIVELY, this software may be distributed under the terms of the
  55165. + * GNU General Public License ("GPL") as published by the Free Software
  55166. + * Foundation, either version 2 of that License or (at your option) any
  55167. + * later version.
  55168. + *
  55169. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  55170. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  55171. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  55172. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  55173. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55174. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55175. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55176. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  55177. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  55178. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55179. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55180. + */
  55181. +
  55182. +
  55183. +/*
  55184. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  55185. + * can write a hardware-agnostic gadget driver running inside a USB device.
  55186. + *
  55187. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  55188. + * affect most of the driver.
  55189. + *
  55190. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  55191. + * functional test of your device-side usb stack, or with "usb-skeleton".
  55192. + *
  55193. + * It supports two similar configurations. One sinks whatever the usb host
  55194. + * writes, and in return sources zeroes. The other loops whatever the host
  55195. + * writes back, so the host can read it. Module options include:
  55196. + *
  55197. + * buflen=N default N=4096, buffer size used
  55198. + * qlen=N default N=32, how many buffers in the loopback queue
  55199. + * loopdefault default false, list loopback config first
  55200. + *
  55201. + * Many drivers will only have one configuration, letting them be much
  55202. + * simpler if they also don't support high speed operation (like this
  55203. + * driver does).
  55204. + */
  55205. +
  55206. +#include <linux/config.h>
  55207. +#include <linux/module.h>
  55208. +#include <linux/kernel.h>
  55209. +#include <linux/delay.h>
  55210. +#include <linux/ioport.h>
  55211. +#include <linux/sched.h>
  55212. +#include <linux/slab.h>
  55213. +#include <linux/smp_lock.h>
  55214. +#include <linux/errno.h>
  55215. +#include <linux/init.h>
  55216. +#include <linux/timer.h>
  55217. +#include <linux/list.h>
  55218. +#include <linux/interrupt.h>
  55219. +#include <linux/uts.h>
  55220. +#include <linux/version.h>
  55221. +#include <linux/device.h>
  55222. +#include <linux/moduleparam.h>
  55223. +#include <linux/proc_fs.h>
  55224. +
  55225. +#include <asm/byteorder.h>
  55226. +#include <asm/io.h>
  55227. +#include <asm/irq.h>
  55228. +#include <asm/system.h>
  55229. +#include <asm/unaligned.h>
  55230. +
  55231. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  55232. +# include <linux/usb/ch9.h>
  55233. +#else
  55234. +# include <linux/usb_ch9.h>
  55235. +#endif
  55236. +
  55237. +#include <linux/usb_gadget.h>
  55238. +
  55239. +
  55240. +/*-------------------------------------------------------------------------*/
  55241. +/*-------------------------------------------------------------------------*/
  55242. +
  55243. +
  55244. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  55245. +{
  55246. + int count = 0;
  55247. + u8 c;
  55248. + u16 uchar;
  55249. +
  55250. + /* this insists on correct encodings, though not minimal ones.
  55251. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  55252. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  55253. + */
  55254. + while (len != 0 && (c = (u8) *s++) != 0) {
  55255. + if (unlikely(c & 0x80)) {
  55256. + // 2-byte sequence:
  55257. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  55258. + if ((c & 0xe0) == 0xc0) {
  55259. + uchar = (c & 0x1f) << 6;
  55260. +
  55261. + c = (u8) *s++;
  55262. + if ((c & 0xc0) != 0xc0)
  55263. + goto fail;
  55264. + c &= 0x3f;
  55265. + uchar |= c;
  55266. +
  55267. + // 3-byte sequence (most CJKV characters):
  55268. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  55269. + } else if ((c & 0xf0) == 0xe0) {
  55270. + uchar = (c & 0x0f) << 12;
  55271. +
  55272. + c = (u8) *s++;
  55273. + if ((c & 0xc0) != 0xc0)
  55274. + goto fail;
  55275. + c &= 0x3f;
  55276. + uchar |= c << 6;
  55277. +
  55278. + c = (u8) *s++;
  55279. + if ((c & 0xc0) != 0xc0)
  55280. + goto fail;
  55281. + c &= 0x3f;
  55282. + uchar |= c;
  55283. +
  55284. + /* no bogus surrogates */
  55285. + if (0xd800 <= uchar && uchar <= 0xdfff)
  55286. + goto fail;
  55287. +
  55288. + // 4-byte sequence (surrogate pairs, currently rare):
  55289. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  55290. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  55291. + // (uuuuu = wwww + 1)
  55292. + // FIXME accept the surrogate code points (only)
  55293. +
  55294. + } else
  55295. + goto fail;
  55296. + } else
  55297. + uchar = c;
  55298. + put_unaligned (cpu_to_le16 (uchar), cp++);
  55299. + count++;
  55300. + len--;
  55301. + }
  55302. + return count;
  55303. +fail:
  55304. + return -1;
  55305. +}
  55306. +
  55307. +
  55308. +/**
  55309. + * usb_gadget_get_string - fill out a string descriptor
  55310. + * @table: of c strings encoded using UTF-8
  55311. + * @id: string id, from low byte of wValue in get string descriptor
  55312. + * @buf: at least 256 bytes
  55313. + *
  55314. + * Finds the UTF-8 string matching the ID, and converts it into a
  55315. + * string descriptor in utf16-le.
  55316. + * Returns length of descriptor (always even) or negative errno
  55317. + *
  55318. + * If your driver needs stings in multiple languages, you'll probably
  55319. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  55320. + * using this routine after choosing which set of UTF-8 strings to use.
  55321. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  55322. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  55323. + * characters (which are also widely used in C strings).
  55324. + */
  55325. +int
  55326. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  55327. +{
  55328. + struct usb_string *s;
  55329. + int len;
  55330. +
  55331. + /* descriptor 0 has the language id */
  55332. + if (id == 0) {
  55333. + buf [0] = 4;
  55334. + buf [1] = USB_DT_STRING;
  55335. + buf [2] = (u8) table->language;
  55336. + buf [3] = (u8) (table->language >> 8);
  55337. + return 4;
  55338. + }
  55339. + for (s = table->strings; s && s->s; s++)
  55340. + if (s->id == id)
  55341. + break;
  55342. +
  55343. + /* unrecognized: stall. */
  55344. + if (!s || !s->s)
  55345. + return -EINVAL;
  55346. +
  55347. + /* string descriptors have length, tag, then UTF16-LE text */
  55348. + len = min ((size_t) 126, strlen (s->s));
  55349. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  55350. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  55351. + if (len < 0)
  55352. + return -EINVAL;
  55353. + buf [0] = (len + 1) * 2;
  55354. + buf [1] = USB_DT_STRING;
  55355. + return buf [0];
  55356. +}
  55357. +
  55358. +
  55359. +/*-------------------------------------------------------------------------*/
  55360. +/*-------------------------------------------------------------------------*/
  55361. +
  55362. +
  55363. +/**
  55364. + * usb_descriptor_fillbuf - fill buffer with descriptors
  55365. + * @buf: Buffer to be filled
  55366. + * @buflen: Size of buf
  55367. + * @src: Array of descriptor pointers, terminated by null pointer.
  55368. + *
  55369. + * Copies descriptors into the buffer, returning the length or a
  55370. + * negative error code if they can't all be copied. Useful when
  55371. + * assembling descriptors for an associated set of interfaces used
  55372. + * as part of configuring a composite device; or in other cases where
  55373. + * sets of descriptors need to be marshaled.
  55374. + */
  55375. +int
  55376. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  55377. + const struct usb_descriptor_header **src)
  55378. +{
  55379. + u8 *dest = buf;
  55380. +
  55381. + if (!src)
  55382. + return -EINVAL;
  55383. +
  55384. + /* fill buffer from src[] until null descriptor ptr */
  55385. + for (; 0 != *src; src++) {
  55386. + unsigned len = (*src)->bLength;
  55387. +
  55388. + if (len > buflen)
  55389. + return -EINVAL;
  55390. + memcpy(dest, *src, len);
  55391. + buflen -= len;
  55392. + dest += len;
  55393. + }
  55394. + return dest - (u8 *)buf;
  55395. +}
  55396. +
  55397. +
  55398. +/**
  55399. + * usb_gadget_config_buf - builts a complete configuration descriptor
  55400. + * @config: Header for the descriptor, including characteristics such
  55401. + * as power requirements and number of interfaces.
  55402. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  55403. + * endpoint, etc) defining all functions in this device configuration.
  55404. + * @buf: Buffer for the resulting configuration descriptor.
  55405. + * @length: Length of buffer. If this is not big enough to hold the
  55406. + * entire configuration descriptor, an error code will be returned.
  55407. + *
  55408. + * This copies descriptors into the response buffer, building a descriptor
  55409. + * for that configuration. It returns the buffer length or a negative
  55410. + * status code. The config.wTotalLength field is set to match the length
  55411. + * of the result, but other descriptor fields (including power usage and
  55412. + * interface count) must be set by the caller.
  55413. + *
  55414. + * Gadget drivers could use this when constructing a config descriptor
  55415. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  55416. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  55417. + */
  55418. +int usb_gadget_config_buf(
  55419. + const struct usb_config_descriptor *config,
  55420. + void *buf,
  55421. + unsigned length,
  55422. + const struct usb_descriptor_header **desc
  55423. +)
  55424. +{
  55425. + struct usb_config_descriptor *cp = buf;
  55426. + int len;
  55427. +
  55428. + /* config descriptor first */
  55429. + if (length < USB_DT_CONFIG_SIZE || !desc)
  55430. + return -EINVAL;
  55431. + *cp = *config;
  55432. +
  55433. + /* then interface/endpoint/class/vendor/... */
  55434. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  55435. + length - USB_DT_CONFIG_SIZE, desc);
  55436. + if (len < 0)
  55437. + return len;
  55438. + len += USB_DT_CONFIG_SIZE;
  55439. + if (len > 0xffff)
  55440. + return -EINVAL;
  55441. +
  55442. + /* patch up the config descriptor */
  55443. + cp->bLength = USB_DT_CONFIG_SIZE;
  55444. + cp->bDescriptorType = USB_DT_CONFIG;
  55445. + cp->wTotalLength = cpu_to_le16(len);
  55446. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  55447. + return len;
  55448. +}
  55449. +
  55450. +/*-------------------------------------------------------------------------*/
  55451. +/*-------------------------------------------------------------------------*/
  55452. +
  55453. +
  55454. +#define RBUF_LEN (1024*1024)
  55455. +static int rbuf_start;
  55456. +static int rbuf_len;
  55457. +static __u8 rbuf[RBUF_LEN];
  55458. +
  55459. +/*-------------------------------------------------------------------------*/
  55460. +
  55461. +#define DRIVER_VERSION "St Patrick's Day 2004"
  55462. +
  55463. +static const char shortname [] = "zero";
  55464. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  55465. +
  55466. +static const char source_sink [] = "source and sink data";
  55467. +static const char loopback [] = "loop input to output";
  55468. +
  55469. +/*-------------------------------------------------------------------------*/
  55470. +
  55471. +/*
  55472. + * driver assumes self-powered hardware, and
  55473. + * has no way for users to trigger remote wakeup.
  55474. + *
  55475. + * this version autoconfigures as much as possible,
  55476. + * which is reasonable for most "bulk-only" drivers.
  55477. + */
  55478. +static const char *EP_IN_NAME; /* source */
  55479. +static const char *EP_OUT_NAME; /* sink */
  55480. +
  55481. +/*-------------------------------------------------------------------------*/
  55482. +
  55483. +/* big enough to hold our biggest descriptor */
  55484. +#define USB_BUFSIZ 512
  55485. +
  55486. +struct zero_dev {
  55487. + spinlock_t lock;
  55488. + struct usb_gadget *gadget;
  55489. + struct usb_request *req; /* for control responses */
  55490. +
  55491. + /* when configured, we have one of two configs:
  55492. + * - source data (in to host) and sink it (out from host)
  55493. + * - or loop it back (out from host back in to host)
  55494. + */
  55495. + u8 config;
  55496. + struct usb_ep *in_ep, *out_ep;
  55497. +
  55498. + /* autoresume timer */
  55499. + struct timer_list resume;
  55500. +};
  55501. +
  55502. +#define xprintk(d,level,fmt,args...) \
  55503. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  55504. +
  55505. +#ifdef DEBUG
  55506. +#define DBG(dev,fmt,args...) \
  55507. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  55508. +#else
  55509. +#define DBG(dev,fmt,args...) \
  55510. + do { } while (0)
  55511. +#endif /* DEBUG */
  55512. +
  55513. +#ifdef VERBOSE
  55514. +#define VDBG DBG
  55515. +#else
  55516. +#define VDBG(dev,fmt,args...) \
  55517. + do { } while (0)
  55518. +#endif /* VERBOSE */
  55519. +
  55520. +#define ERROR(dev,fmt,args...) \
  55521. + xprintk(dev , KERN_ERR , fmt , ## args)
  55522. +#define WARN(dev,fmt,args...) \
  55523. + xprintk(dev , KERN_WARNING , fmt , ## args)
  55524. +#define INFO(dev,fmt,args...) \
  55525. + xprintk(dev , KERN_INFO , fmt , ## args)
  55526. +
  55527. +/*-------------------------------------------------------------------------*/
  55528. +
  55529. +static unsigned buflen = 4096;
  55530. +static unsigned qlen = 32;
  55531. +static unsigned pattern = 0;
  55532. +
  55533. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  55534. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  55535. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  55536. +
  55537. +/*
  55538. + * if it's nonzero, autoresume says how many seconds to wait
  55539. + * before trying to wake up the host after suspend.
  55540. + */
  55541. +static unsigned autoresume = 0;
  55542. +module_param (autoresume, uint, 0);
  55543. +
  55544. +/*
  55545. + * Normally the "loopback" configuration is second (index 1) so
  55546. + * it's not the default. Here's where to change that order, to
  55547. + * work better with hosts where config changes are problematic.
  55548. + * Or controllers (like superh) that only support one config.
  55549. + */
  55550. +static int loopdefault = 0;
  55551. +
  55552. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  55553. +
  55554. +/*-------------------------------------------------------------------------*/
  55555. +
  55556. +/* Thanks to NetChip Technologies for donating this product ID.
  55557. + *
  55558. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  55559. + * Instead: allocate your own, using normal USB-IF procedures.
  55560. + */
  55561. +#ifndef CONFIG_USB_ZERO_HNPTEST
  55562. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  55563. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  55564. +#else
  55565. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  55566. +#define DRIVER_PRODUCT_NUM 0xbadd
  55567. +#endif
  55568. +
  55569. +/*-------------------------------------------------------------------------*/
  55570. +
  55571. +/*
  55572. + * DESCRIPTORS ... most are static, but strings and (full)
  55573. + * configuration descriptors are built on demand.
  55574. + */
  55575. +
  55576. +/*
  55577. +#define STRING_MANUFACTURER 25
  55578. +#define STRING_PRODUCT 42
  55579. +#define STRING_SERIAL 101
  55580. +*/
  55581. +#define STRING_MANUFACTURER 1
  55582. +#define STRING_PRODUCT 2
  55583. +#define STRING_SERIAL 3
  55584. +
  55585. +#define STRING_SOURCE_SINK 250
  55586. +#define STRING_LOOPBACK 251
  55587. +
  55588. +/*
  55589. + * This device advertises two configurations; these numbers work
  55590. + * on a pxa250 as well as more flexible hardware.
  55591. + */
  55592. +#define CONFIG_SOURCE_SINK 3
  55593. +#define CONFIG_LOOPBACK 2
  55594. +
  55595. +/*
  55596. +static struct usb_device_descriptor
  55597. +device_desc = {
  55598. + .bLength = sizeof device_desc,
  55599. + .bDescriptorType = USB_DT_DEVICE,
  55600. +
  55601. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  55602. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  55603. +
  55604. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  55605. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  55606. + .iManufacturer = STRING_MANUFACTURER,
  55607. + .iProduct = STRING_PRODUCT,
  55608. + .iSerialNumber = STRING_SERIAL,
  55609. + .bNumConfigurations = 2,
  55610. +};
  55611. +*/
  55612. +static struct usb_device_descriptor
  55613. +device_desc = {
  55614. + .bLength = sizeof device_desc,
  55615. + .bDescriptorType = USB_DT_DEVICE,
  55616. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  55617. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  55618. + .bDeviceSubClass = 0,
  55619. + .bDeviceProtocol = 0,
  55620. + .bMaxPacketSize0 = 64,
  55621. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  55622. + .idVendor = __constant_cpu_to_le16 (0x0499),
  55623. + .idProduct = __constant_cpu_to_le16 (0x3002),
  55624. + .iManufacturer = STRING_MANUFACTURER,
  55625. + .iProduct = STRING_PRODUCT,
  55626. + .iSerialNumber = STRING_SERIAL,
  55627. + .bNumConfigurations = 1,
  55628. +};
  55629. +
  55630. +static struct usb_config_descriptor
  55631. +z_config = {
  55632. + .bLength = sizeof z_config,
  55633. + .bDescriptorType = USB_DT_CONFIG,
  55634. +
  55635. + /* compute wTotalLength on the fly */
  55636. + .bNumInterfaces = 2,
  55637. + .bConfigurationValue = 1,
  55638. + .iConfiguration = 0,
  55639. + .bmAttributes = 0x40,
  55640. + .bMaxPower = 0, /* self-powered */
  55641. +};
  55642. +
  55643. +
  55644. +static struct usb_otg_descriptor
  55645. +otg_descriptor = {
  55646. + .bLength = sizeof otg_descriptor,
  55647. + .bDescriptorType = USB_DT_OTG,
  55648. +
  55649. + .bmAttributes = USB_OTG_SRP,
  55650. +};
  55651. +
  55652. +/* one interface in each configuration */
  55653. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  55654. +
  55655. +/*
  55656. + * usb 2.0 devices need to expose both high speed and full speed
  55657. + * descriptors, unless they only run at full speed.
  55658. + *
  55659. + * that means alternate endpoint descriptors (bigger packets)
  55660. + * and a "device qualifier" ... plus more construction options
  55661. + * for the config descriptor.
  55662. + */
  55663. +
  55664. +static struct usb_qualifier_descriptor
  55665. +dev_qualifier = {
  55666. + .bLength = sizeof dev_qualifier,
  55667. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  55668. +
  55669. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  55670. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  55671. +
  55672. + .bNumConfigurations = 2,
  55673. +};
  55674. +
  55675. +
  55676. +struct usb_cs_as_general_descriptor {
  55677. + __u8 bLength;
  55678. + __u8 bDescriptorType;
  55679. +
  55680. + __u8 bDescriptorSubType;
  55681. + __u8 bTerminalLink;
  55682. + __u8 bDelay;
  55683. + __u16 wFormatTag;
  55684. +} __attribute__ ((packed));
  55685. +
  55686. +struct usb_cs_as_format_descriptor {
  55687. + __u8 bLength;
  55688. + __u8 bDescriptorType;
  55689. +
  55690. + __u8 bDescriptorSubType;
  55691. + __u8 bFormatType;
  55692. + __u8 bNrChannels;
  55693. + __u8 bSubframeSize;
  55694. + __u8 bBitResolution;
  55695. + __u8 bSamfreqType;
  55696. + __u8 tLowerSamFreq[3];
  55697. + __u8 tUpperSamFreq[3];
  55698. +} __attribute__ ((packed));
  55699. +
  55700. +static const struct usb_interface_descriptor
  55701. +z_audio_control_if_desc = {
  55702. + .bLength = sizeof z_audio_control_if_desc,
  55703. + .bDescriptorType = USB_DT_INTERFACE,
  55704. + .bInterfaceNumber = 0,
  55705. + .bAlternateSetting = 0,
  55706. + .bNumEndpoints = 0,
  55707. + .bInterfaceClass = USB_CLASS_AUDIO,
  55708. + .bInterfaceSubClass = 0x1,
  55709. + .bInterfaceProtocol = 0,
  55710. + .iInterface = 0,
  55711. +};
  55712. +
  55713. +static const struct usb_interface_descriptor
  55714. +z_audio_if_desc = {
  55715. + .bLength = sizeof z_audio_if_desc,
  55716. + .bDescriptorType = USB_DT_INTERFACE,
  55717. + .bInterfaceNumber = 1,
  55718. + .bAlternateSetting = 0,
  55719. + .bNumEndpoints = 0,
  55720. + .bInterfaceClass = USB_CLASS_AUDIO,
  55721. + .bInterfaceSubClass = 0x2,
  55722. + .bInterfaceProtocol = 0,
  55723. + .iInterface = 0,
  55724. +};
  55725. +
  55726. +static const struct usb_interface_descriptor
  55727. +z_audio_if_desc2 = {
  55728. + .bLength = sizeof z_audio_if_desc,
  55729. + .bDescriptorType = USB_DT_INTERFACE,
  55730. + .bInterfaceNumber = 1,
  55731. + .bAlternateSetting = 1,
  55732. + .bNumEndpoints = 1,
  55733. + .bInterfaceClass = USB_CLASS_AUDIO,
  55734. + .bInterfaceSubClass = 0x2,
  55735. + .bInterfaceProtocol = 0,
  55736. + .iInterface = 0,
  55737. +};
  55738. +
  55739. +static const struct usb_cs_as_general_descriptor
  55740. +z_audio_cs_as_if_desc = {
  55741. + .bLength = 7,
  55742. + .bDescriptorType = 0x24,
  55743. +
  55744. + .bDescriptorSubType = 0x01,
  55745. + .bTerminalLink = 0x01,
  55746. + .bDelay = 0x0,
  55747. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  55748. +};
  55749. +
  55750. +
  55751. +static const struct usb_cs_as_format_descriptor
  55752. +z_audio_cs_as_format_desc = {
  55753. + .bLength = 0xe,
  55754. + .bDescriptorType = 0x24,
  55755. +
  55756. + .bDescriptorSubType = 2,
  55757. + .bFormatType = 1,
  55758. + .bNrChannels = 1,
  55759. + .bSubframeSize = 1,
  55760. + .bBitResolution = 8,
  55761. + .bSamfreqType = 0,
  55762. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  55763. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  55764. +};
  55765. +
  55766. +static const struct usb_endpoint_descriptor
  55767. +z_iso_ep = {
  55768. + .bLength = 0x09,
  55769. + .bDescriptorType = 0x05,
  55770. + .bEndpointAddress = 0x04,
  55771. + .bmAttributes = 0x09,
  55772. + .wMaxPacketSize = 0x0038,
  55773. + .bInterval = 0x01,
  55774. + .bRefresh = 0x00,
  55775. + .bSynchAddress = 0x00,
  55776. +};
  55777. +
  55778. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  55779. +
  55780. +// 9 bytes
  55781. +static char z_ac_interface_header_desc[] =
  55782. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  55783. +
  55784. +// 12 bytes
  55785. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  55786. + 0x03, 0x00, 0x00, 0x00};
  55787. +// 13 bytes
  55788. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  55789. + 0x02, 0x00, 0x02, 0x00, 0x00};
  55790. +// 9 bytes
  55791. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  55792. + 0x00};
  55793. +
  55794. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  55795. + 0x00};
  55796. +
  55797. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  55798. +
  55799. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  55800. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  55801. +
  55802. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  55803. + 0x00};
  55804. +
  55805. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  55806. +
  55807. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  55808. + 0x00};
  55809. +
  55810. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  55811. +
  55812. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  55813. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  55814. +
  55815. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  55816. + 0x00};
  55817. +
  55818. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  55819. +
  55820. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  55821. + 0x00};
  55822. +
  55823. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  55824. +
  55825. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  55826. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  55827. +
  55828. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  55829. + 0x00};
  55830. +
  55831. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  55832. +
  55833. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  55834. + 0x00};
  55835. +
  55836. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  55837. +
  55838. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  55839. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  55840. +
  55841. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  55842. + 0x00};
  55843. +
  55844. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  55845. +
  55846. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  55847. + 0x00};
  55848. +
  55849. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  55850. +
  55851. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  55852. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  55853. +
  55854. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  55855. + 0x00};
  55856. +
  55857. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  55858. +
  55859. +
  55860. +
  55861. +static const struct usb_descriptor_header *z_function [] = {
  55862. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  55863. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  55864. + (struct usb_descriptor_header *) &z_0,
  55865. + (struct usb_descriptor_header *) &z_1,
  55866. + (struct usb_descriptor_header *) &z_2,
  55867. + (struct usb_descriptor_header *) &z_audio_if_desc,
  55868. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  55869. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  55870. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  55871. + (struct usb_descriptor_header *) &z_iso_ep,
  55872. + (struct usb_descriptor_header *) &z_iso_ep2,
  55873. + (struct usb_descriptor_header *) &za_0,
  55874. + (struct usb_descriptor_header *) &za_1,
  55875. + (struct usb_descriptor_header *) &za_2,
  55876. + (struct usb_descriptor_header *) &za_3,
  55877. + (struct usb_descriptor_header *) &za_4,
  55878. + (struct usb_descriptor_header *) &za_5,
  55879. + (struct usb_descriptor_header *) &za_6,
  55880. + (struct usb_descriptor_header *) &za_7,
  55881. + (struct usb_descriptor_header *) &za_8,
  55882. + (struct usb_descriptor_header *) &za_9,
  55883. + (struct usb_descriptor_header *) &za_10,
  55884. + (struct usb_descriptor_header *) &za_11,
  55885. + (struct usb_descriptor_header *) &za_12,
  55886. + (struct usb_descriptor_header *) &za_13,
  55887. + (struct usb_descriptor_header *) &za_14,
  55888. + (struct usb_descriptor_header *) &za_15,
  55889. + (struct usb_descriptor_header *) &za_16,
  55890. + (struct usb_descriptor_header *) &za_17,
  55891. + (struct usb_descriptor_header *) &za_18,
  55892. + (struct usb_descriptor_header *) &za_19,
  55893. + (struct usb_descriptor_header *) &za_20,
  55894. + (struct usb_descriptor_header *) &za_21,
  55895. + (struct usb_descriptor_header *) &za_22,
  55896. + (struct usb_descriptor_header *) &za_23,
  55897. + (struct usb_descriptor_header *) &za_24,
  55898. + NULL,
  55899. +};
  55900. +
  55901. +/* maxpacket and other transfer characteristics vary by speed. */
  55902. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  55903. +
  55904. +#else
  55905. +
  55906. +/* if there's no high speed support, maxpacket doesn't change. */
  55907. +#define ep_desc(g,hs,fs) fs
  55908. +
  55909. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  55910. +
  55911. +static char manufacturer [40];
  55912. +//static char serial [40];
  55913. +static char serial [] = "Ser 00 em";
  55914. +
  55915. +/* static strings, in UTF-8 */
  55916. +static struct usb_string strings [] = {
  55917. + { STRING_MANUFACTURER, manufacturer, },
  55918. + { STRING_PRODUCT, longname, },
  55919. + { STRING_SERIAL, serial, },
  55920. + { STRING_LOOPBACK, loopback, },
  55921. + { STRING_SOURCE_SINK, source_sink, },
  55922. + { } /* end of list */
  55923. +};
  55924. +
  55925. +static struct usb_gadget_strings stringtab = {
  55926. + .language = 0x0409, /* en-us */
  55927. + .strings = strings,
  55928. +};
  55929. +
  55930. +/*
  55931. + * config descriptors are also handcrafted. these must agree with code
  55932. + * that sets configurations, and with code managing interfaces and their
  55933. + * altsettings. other complexity may come from:
  55934. + *
  55935. + * - high speed support, including "other speed config" rules
  55936. + * - multiple configurations
  55937. + * - interfaces with alternate settings
  55938. + * - embedded class or vendor-specific descriptors
  55939. + *
  55940. + * this handles high speed, and has a second config that could as easily
  55941. + * have been an alternate interface setting (on most hardware).
  55942. + *
  55943. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  55944. + * should include an altsetting to test interrupt transfers, including
  55945. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  55946. + * device?)
  55947. + */
  55948. +static int
  55949. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  55950. +{
  55951. + int len;
  55952. + const struct usb_descriptor_header **function;
  55953. +
  55954. + function = z_function;
  55955. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  55956. + if (len < 0)
  55957. + return len;
  55958. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  55959. + return len;
  55960. +}
  55961. +
  55962. +/*-------------------------------------------------------------------------*/
  55963. +
  55964. +static struct usb_request *
  55965. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  55966. +{
  55967. + struct usb_request *req;
  55968. +
  55969. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  55970. + if (req) {
  55971. + req->length = length;
  55972. + req->buf = usb_ep_alloc_buffer (ep, length,
  55973. + &req->dma, GFP_ATOMIC);
  55974. + if (!req->buf) {
  55975. + usb_ep_free_request (ep, req);
  55976. + req = NULL;
  55977. + }
  55978. + }
  55979. + return req;
  55980. +}
  55981. +
  55982. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  55983. +{
  55984. + if (req->buf)
  55985. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  55986. + usb_ep_free_request (ep, req);
  55987. +}
  55988. +
  55989. +/*-------------------------------------------------------------------------*/
  55990. +
  55991. +/* optionally require specific source/sink data patterns */
  55992. +
  55993. +static int
  55994. +check_read_data (
  55995. + struct zero_dev *dev,
  55996. + struct usb_ep *ep,
  55997. + struct usb_request *req
  55998. +)
  55999. +{
  56000. + unsigned i;
  56001. + u8 *buf = req->buf;
  56002. +
  56003. + for (i = 0; i < req->actual; i++, buf++) {
  56004. + switch (pattern) {
  56005. + /* all-zeroes has no synchronization issues */
  56006. + case 0:
  56007. + if (*buf == 0)
  56008. + continue;
  56009. + break;
  56010. + /* mod63 stays in sync with short-terminated transfers,
  56011. + * or otherwise when host and gadget agree on how large
  56012. + * each usb transfer request should be. resync is done
  56013. + * with set_interface or set_config.
  56014. + */
  56015. + case 1:
  56016. + if (*buf == (u8)(i % 63))
  56017. + continue;
  56018. + break;
  56019. + }
  56020. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  56021. + usb_ep_set_halt (ep);
  56022. + return -EINVAL;
  56023. + }
  56024. + return 0;
  56025. +}
  56026. +
  56027. +/*-------------------------------------------------------------------------*/
  56028. +
  56029. +static void zero_reset_config (struct zero_dev *dev)
  56030. +{
  56031. + if (dev->config == 0)
  56032. + return;
  56033. +
  56034. + DBG (dev, "reset config\n");
  56035. +
  56036. + /* just disable endpoints, forcing completion of pending i/o.
  56037. + * all our completion handlers free their requests in this case.
  56038. + */
  56039. + if (dev->in_ep) {
  56040. + usb_ep_disable (dev->in_ep);
  56041. + dev->in_ep = NULL;
  56042. + }
  56043. + if (dev->out_ep) {
  56044. + usb_ep_disable (dev->out_ep);
  56045. + dev->out_ep = NULL;
  56046. + }
  56047. + dev->config = 0;
  56048. + del_timer (&dev->resume);
  56049. +}
  56050. +
  56051. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  56052. +
  56053. +static void
  56054. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  56055. +{
  56056. + struct zero_dev *dev = ep->driver_data;
  56057. + int status = req->status;
  56058. + int i, j;
  56059. +
  56060. + switch (status) {
  56061. +
  56062. + case 0: /* normal completion? */
  56063. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  56064. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  56065. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  56066. + rbuf[j] = ((__u8*)req->buf)[i];
  56067. + j++;
  56068. + if (j >= RBUF_LEN) j=0;
  56069. + }
  56070. + rbuf_start = j;
  56071. + //printk ("\n\n");
  56072. +
  56073. + if (rbuf_len < RBUF_LEN) {
  56074. + rbuf_len += req->actual;
  56075. + if (rbuf_len > RBUF_LEN) {
  56076. + rbuf_len = RBUF_LEN;
  56077. + }
  56078. + }
  56079. +
  56080. + break;
  56081. +
  56082. + /* this endpoint is normally active while we're configured */
  56083. + case -ECONNABORTED: /* hardware forced ep reset */
  56084. + case -ECONNRESET: /* request dequeued */
  56085. + case -ESHUTDOWN: /* disconnect from host */
  56086. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  56087. + req->actual, req->length);
  56088. + if (ep == dev->out_ep)
  56089. + check_read_data (dev, ep, req);
  56090. + free_ep_req (ep, req);
  56091. + return;
  56092. +
  56093. + case -EOVERFLOW: /* buffer overrun on read means that
  56094. + * we didn't provide a big enough
  56095. + * buffer.
  56096. + */
  56097. + default:
  56098. +#if 1
  56099. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  56100. + status, req->actual, req->length);
  56101. +#endif
  56102. + case -EREMOTEIO: /* short read */
  56103. + break;
  56104. + }
  56105. +
  56106. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  56107. + if (status) {
  56108. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  56109. + ep->name, req->length, status);
  56110. + usb_ep_set_halt (ep);
  56111. + /* FIXME recover later ... somehow */
  56112. + }
  56113. +}
  56114. +
  56115. +static struct usb_request *
  56116. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  56117. +{
  56118. + struct usb_request *req;
  56119. + int status;
  56120. +
  56121. + req = alloc_ep_req (ep, 512);
  56122. + if (!req)
  56123. + return NULL;
  56124. +
  56125. + req->complete = zero_isoc_complete;
  56126. +
  56127. + status = usb_ep_queue (ep, req, gfp_flags);
  56128. + if (status) {
  56129. + struct zero_dev *dev = ep->driver_data;
  56130. +
  56131. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  56132. + free_ep_req (ep, req);
  56133. + req = NULL;
  56134. + }
  56135. +
  56136. + return req;
  56137. +}
  56138. +
  56139. +/* change our operational config. this code must agree with the code
  56140. + * that returns config descriptors, and altsetting code.
  56141. + *
  56142. + * it's also responsible for power management interactions. some
  56143. + * configurations might not work with our current power sources.
  56144. + *
  56145. + * note that some device controller hardware will constrain what this
  56146. + * code can do, perhaps by disallowing more than one configuration or
  56147. + * by limiting configuration choices (like the pxa2xx).
  56148. + */
  56149. +static int
  56150. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  56151. +{
  56152. + int result = 0;
  56153. + struct usb_gadget *gadget = dev->gadget;
  56154. + const struct usb_endpoint_descriptor *d;
  56155. + struct usb_ep *ep;
  56156. +
  56157. + if (number == dev->config)
  56158. + return 0;
  56159. +
  56160. + zero_reset_config (dev);
  56161. +
  56162. + gadget_for_each_ep (ep, gadget) {
  56163. +
  56164. + if (strcmp (ep->name, "ep4") == 0) {
  56165. +
  56166. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  56167. + result = usb_ep_enable (ep, d);
  56168. +
  56169. + if (result == 0) {
  56170. + ep->driver_data = dev;
  56171. + dev->in_ep = ep;
  56172. +
  56173. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  56174. +
  56175. + dev->in_ep = ep;
  56176. + continue;
  56177. + }
  56178. +
  56179. + usb_ep_disable (ep);
  56180. + result = -EIO;
  56181. + }
  56182. + }
  56183. +
  56184. + }
  56185. +
  56186. + dev->config = number;
  56187. + return result;
  56188. +}
  56189. +
  56190. +/*-------------------------------------------------------------------------*/
  56191. +
  56192. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  56193. +{
  56194. + if (req->status || req->actual != req->length)
  56195. + DBG ((struct zero_dev *) ep->driver_data,
  56196. + "setup complete --> %d, %d/%d\n",
  56197. + req->status, req->actual, req->length);
  56198. +}
  56199. +
  56200. +/*
  56201. + * The setup() callback implements all the ep0 functionality that's
  56202. + * not handled lower down, in hardware or the hardware driver (like
  56203. + * device and endpoint feature flags, and their status). It's all
  56204. + * housekeeping for the gadget function we're implementing. Most of
  56205. + * the work is in config-specific setup.
  56206. + */
  56207. +static int
  56208. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  56209. +{
  56210. + struct zero_dev *dev = get_gadget_data (gadget);
  56211. + struct usb_request *req = dev->req;
  56212. + int value = -EOPNOTSUPP;
  56213. +
  56214. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  56215. + * but config change events will reconfigure hardware.
  56216. + */
  56217. + req->zero = 0;
  56218. + switch (ctrl->bRequest) {
  56219. +
  56220. + case USB_REQ_GET_DESCRIPTOR:
  56221. +
  56222. + switch (ctrl->wValue >> 8) {
  56223. +
  56224. + case USB_DT_DEVICE:
  56225. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  56226. + memcpy (req->buf, &device_desc, value);
  56227. + break;
  56228. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  56229. + case USB_DT_DEVICE_QUALIFIER:
  56230. + if (!gadget->is_dualspeed)
  56231. + break;
  56232. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  56233. + memcpy (req->buf, &dev_qualifier, value);
  56234. + break;
  56235. +
  56236. + case USB_DT_OTHER_SPEED_CONFIG:
  56237. + if (!gadget->is_dualspeed)
  56238. + break;
  56239. + // FALLTHROUGH
  56240. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  56241. + case USB_DT_CONFIG:
  56242. + value = config_buf (gadget, req->buf,
  56243. + ctrl->wValue >> 8,
  56244. + ctrl->wValue & 0xff);
  56245. + if (value >= 0)
  56246. + value = min (ctrl->wLength, (u16) value);
  56247. + break;
  56248. +
  56249. + case USB_DT_STRING:
  56250. + /* wIndex == language code.
  56251. + * this driver only handles one language, you can
  56252. + * add string tables for other languages, using
  56253. + * any UTF-8 characters
  56254. + */
  56255. + value = usb_gadget_get_string (&stringtab,
  56256. + ctrl->wValue & 0xff, req->buf);
  56257. + if (value >= 0) {
  56258. + value = min (ctrl->wLength, (u16) value);
  56259. + }
  56260. + break;
  56261. + }
  56262. + break;
  56263. +
  56264. + /* currently two configs, two speeds */
  56265. + case USB_REQ_SET_CONFIGURATION:
  56266. + if (ctrl->bRequestType != 0)
  56267. + goto unknown;
  56268. +
  56269. + spin_lock (&dev->lock);
  56270. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  56271. + spin_unlock (&dev->lock);
  56272. + break;
  56273. + case USB_REQ_GET_CONFIGURATION:
  56274. + if (ctrl->bRequestType != USB_DIR_IN)
  56275. + goto unknown;
  56276. + *(u8 *)req->buf = dev->config;
  56277. + value = min (ctrl->wLength, (u16) 1);
  56278. + break;
  56279. +
  56280. + /* until we add altsetting support, or other interfaces,
  56281. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  56282. + * and already killed pending endpoint I/O.
  56283. + */
  56284. + case USB_REQ_SET_INTERFACE:
  56285. +
  56286. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  56287. + goto unknown;
  56288. + spin_lock (&dev->lock);
  56289. + if (dev->config) {
  56290. + u8 config = dev->config;
  56291. +
  56292. + /* resets interface configuration, forgets about
  56293. + * previous transaction state (queued bufs, etc)
  56294. + * and re-inits endpoint state (toggle etc)
  56295. + * no response queued, just zero status == success.
  56296. + * if we had more than one interface we couldn't
  56297. + * use this "reset the config" shortcut.
  56298. + */
  56299. + zero_reset_config (dev);
  56300. + zero_set_config (dev, config, GFP_ATOMIC);
  56301. + value = 0;
  56302. + }
  56303. + spin_unlock (&dev->lock);
  56304. + break;
  56305. + case USB_REQ_GET_INTERFACE:
  56306. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  56307. + value = ctrl->wLength;
  56308. + break;
  56309. + }
  56310. + else {
  56311. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  56312. + goto unknown;
  56313. + if (!dev->config)
  56314. + break;
  56315. + if (ctrl->wIndex != 0) {
  56316. + value = -EDOM;
  56317. + break;
  56318. + }
  56319. + *(u8 *)req->buf = 0;
  56320. + value = min (ctrl->wLength, (u16) 1);
  56321. + }
  56322. + break;
  56323. +
  56324. + /*
  56325. + * These are the same vendor-specific requests supported by
  56326. + * Intel's USB 2.0 compliance test devices. We exceed that
  56327. + * device spec by allowing multiple-packet requests.
  56328. + */
  56329. + case 0x5b: /* control WRITE test -- fill the buffer */
  56330. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  56331. + goto unknown;
  56332. + if (ctrl->wValue || ctrl->wIndex)
  56333. + break;
  56334. + /* just read that many bytes into the buffer */
  56335. + if (ctrl->wLength > USB_BUFSIZ)
  56336. + break;
  56337. + value = ctrl->wLength;
  56338. + break;
  56339. + case 0x5c: /* control READ test -- return the buffer */
  56340. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  56341. + goto unknown;
  56342. + if (ctrl->wValue || ctrl->wIndex)
  56343. + break;
  56344. + /* expect those bytes are still in the buffer; send back */
  56345. + if (ctrl->wLength > USB_BUFSIZ
  56346. + || ctrl->wLength != req->length)
  56347. + break;
  56348. + value = ctrl->wLength;
  56349. + break;
  56350. +
  56351. + case 0x01: // SET_CUR
  56352. + case 0x02:
  56353. + case 0x03:
  56354. + case 0x04:
  56355. + case 0x05:
  56356. + value = ctrl->wLength;
  56357. + break;
  56358. + case 0x81:
  56359. + switch (ctrl->wValue) {
  56360. + case 0x0201:
  56361. + case 0x0202:
  56362. + ((u8*)req->buf)[0] = 0x00;
  56363. + ((u8*)req->buf)[1] = 0xe3;
  56364. + break;
  56365. + case 0x0300:
  56366. + case 0x0500:
  56367. + ((u8*)req->buf)[0] = 0x00;
  56368. + break;
  56369. + }
  56370. + //((u8*)req->buf)[0] = 0x81;
  56371. + //((u8*)req->buf)[1] = 0x81;
  56372. + value = ctrl->wLength;
  56373. + break;
  56374. + case 0x82:
  56375. + switch (ctrl->wValue) {
  56376. + case 0x0201:
  56377. + case 0x0202:
  56378. + ((u8*)req->buf)[0] = 0x00;
  56379. + ((u8*)req->buf)[1] = 0xc3;
  56380. + break;
  56381. + case 0x0300:
  56382. + case 0x0500:
  56383. + ((u8*)req->buf)[0] = 0x00;
  56384. + break;
  56385. + }
  56386. + //((u8*)req->buf)[0] = 0x82;
  56387. + //((u8*)req->buf)[1] = 0x82;
  56388. + value = ctrl->wLength;
  56389. + break;
  56390. + case 0x83:
  56391. + switch (ctrl->wValue) {
  56392. + case 0x0201:
  56393. + case 0x0202:
  56394. + ((u8*)req->buf)[0] = 0x00;
  56395. + ((u8*)req->buf)[1] = 0x00;
  56396. + break;
  56397. + case 0x0300:
  56398. + ((u8*)req->buf)[0] = 0x60;
  56399. + break;
  56400. + case 0x0500:
  56401. + ((u8*)req->buf)[0] = 0x18;
  56402. + break;
  56403. + }
  56404. + //((u8*)req->buf)[0] = 0x83;
  56405. + //((u8*)req->buf)[1] = 0x83;
  56406. + value = ctrl->wLength;
  56407. + break;
  56408. + case 0x84:
  56409. + switch (ctrl->wValue) {
  56410. + case 0x0201:
  56411. + case 0x0202:
  56412. + ((u8*)req->buf)[0] = 0x00;
  56413. + ((u8*)req->buf)[1] = 0x01;
  56414. + break;
  56415. + case 0x0300:
  56416. + case 0x0500:
  56417. + ((u8*)req->buf)[0] = 0x08;
  56418. + break;
  56419. + }
  56420. + //((u8*)req->buf)[0] = 0x84;
  56421. + //((u8*)req->buf)[1] = 0x84;
  56422. + value = ctrl->wLength;
  56423. + break;
  56424. + case 0x85:
  56425. + ((u8*)req->buf)[0] = 0x85;
  56426. + ((u8*)req->buf)[1] = 0x85;
  56427. + value = ctrl->wLength;
  56428. + break;
  56429. +
  56430. +
  56431. + default:
  56432. +unknown:
  56433. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  56434. + ctrl->bRequestType, ctrl->bRequest,
  56435. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  56436. + }
  56437. +
  56438. + /* respond with data transfer before status phase? */
  56439. + if (value >= 0) {
  56440. + req->length = value;
  56441. + req->zero = value < ctrl->wLength
  56442. + && (value % gadget->ep0->maxpacket) == 0;
  56443. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  56444. + if (value < 0) {
  56445. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  56446. + req->status = 0;
  56447. + zero_setup_complete (gadget->ep0, req);
  56448. + }
  56449. + }
  56450. +
  56451. + /* device either stalls (value < 0) or reports success */
  56452. + return value;
  56453. +}
  56454. +
  56455. +static void
  56456. +zero_disconnect (struct usb_gadget *gadget)
  56457. +{
  56458. + struct zero_dev *dev = get_gadget_data (gadget);
  56459. + unsigned long flags;
  56460. +
  56461. + spin_lock_irqsave (&dev->lock, flags);
  56462. + zero_reset_config (dev);
  56463. +
  56464. + /* a more significant application might have some non-usb
  56465. + * activities to quiesce here, saving resources like power
  56466. + * or pushing the notification up a network stack.
  56467. + */
  56468. + spin_unlock_irqrestore (&dev->lock, flags);
  56469. +
  56470. + /* next we may get setup() calls to enumerate new connections;
  56471. + * or an unbind() during shutdown (including removing module).
  56472. + */
  56473. +}
  56474. +
  56475. +static void
  56476. +zero_autoresume (unsigned long _dev)
  56477. +{
  56478. + struct zero_dev *dev = (struct zero_dev *) _dev;
  56479. + int status;
  56480. +
  56481. + /* normally the host would be woken up for something
  56482. + * more significant than just a timer firing...
  56483. + */
  56484. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  56485. + status = usb_gadget_wakeup (dev->gadget);
  56486. + DBG (dev, "wakeup --> %d\n", status);
  56487. + }
  56488. +}
  56489. +
  56490. +/*-------------------------------------------------------------------------*/
  56491. +
  56492. +static void
  56493. +zero_unbind (struct usb_gadget *gadget)
  56494. +{
  56495. + struct zero_dev *dev = get_gadget_data (gadget);
  56496. +
  56497. + DBG (dev, "unbind\n");
  56498. +
  56499. + /* we've already been disconnected ... no i/o is active */
  56500. + if (dev->req)
  56501. + free_ep_req (gadget->ep0, dev->req);
  56502. + del_timer_sync (&dev->resume);
  56503. + kfree (dev);
  56504. + set_gadget_data (gadget, NULL);
  56505. +}
  56506. +
  56507. +static int
  56508. +zero_bind (struct usb_gadget *gadget)
  56509. +{
  56510. + struct zero_dev *dev;
  56511. + //struct usb_ep *ep;
  56512. +
  56513. + printk("binding\n");
  56514. + /*
  56515. + * DRIVER POLICY CHOICE: you may want to do this differently.
  56516. + * One thing to avoid is reusing a bcdDevice revision code
  56517. + * with different host-visible configurations or behavior
  56518. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  56519. + */
  56520. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  56521. +
  56522. +
  56523. + /* ok, we made sense of the hardware ... */
  56524. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  56525. + if (!dev)
  56526. + return -ENOMEM;
  56527. + memset (dev, 0, sizeof *dev);
  56528. + spin_lock_init (&dev->lock);
  56529. + dev->gadget = gadget;
  56530. + set_gadget_data (gadget, dev);
  56531. +
  56532. + /* preallocate control response and buffer */
  56533. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  56534. + if (!dev->req)
  56535. + goto enomem;
  56536. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  56537. + &dev->req->dma, GFP_KERNEL);
  56538. + if (!dev->req->buf)
  56539. + goto enomem;
  56540. +
  56541. + dev->req->complete = zero_setup_complete;
  56542. +
  56543. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  56544. +
  56545. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  56546. + /* assume ep0 uses the same value for both speeds ... */
  56547. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  56548. +
  56549. + /* and that all endpoints are dual-speed */
  56550. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  56551. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  56552. +#endif
  56553. +
  56554. + usb_gadget_set_selfpowered (gadget);
  56555. +
  56556. + init_timer (&dev->resume);
  56557. + dev->resume.function = zero_autoresume;
  56558. + dev->resume.data = (unsigned long) dev;
  56559. +
  56560. + gadget->ep0->driver_data = dev;
  56561. +
  56562. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  56563. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  56564. + EP_OUT_NAME, EP_IN_NAME);
  56565. +
  56566. + snprintf (manufacturer, sizeof manufacturer,
  56567. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  56568. + gadget->name);
  56569. +
  56570. + return 0;
  56571. +
  56572. +enomem:
  56573. + zero_unbind (gadget);
  56574. + return -ENOMEM;
  56575. +}
  56576. +
  56577. +/*-------------------------------------------------------------------------*/
  56578. +
  56579. +static void
  56580. +zero_suspend (struct usb_gadget *gadget)
  56581. +{
  56582. + struct zero_dev *dev = get_gadget_data (gadget);
  56583. +
  56584. + if (gadget->speed == USB_SPEED_UNKNOWN)
  56585. + return;
  56586. +
  56587. + if (autoresume) {
  56588. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  56589. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  56590. + } else
  56591. + DBG (dev, "suspend\n");
  56592. +}
  56593. +
  56594. +static void
  56595. +zero_resume (struct usb_gadget *gadget)
  56596. +{
  56597. + struct zero_dev *dev = get_gadget_data (gadget);
  56598. +
  56599. + DBG (dev, "resume\n");
  56600. + del_timer (&dev->resume);
  56601. +}
  56602. +
  56603. +
  56604. +/*-------------------------------------------------------------------------*/
  56605. +
  56606. +static struct usb_gadget_driver zero_driver = {
  56607. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  56608. + .speed = USB_SPEED_HIGH,
  56609. +#else
  56610. + .speed = USB_SPEED_FULL,
  56611. +#endif
  56612. + .function = (char *) longname,
  56613. + .bind = zero_bind,
  56614. + .unbind = zero_unbind,
  56615. +
  56616. + .setup = zero_setup,
  56617. + .disconnect = zero_disconnect,
  56618. +
  56619. + .suspend = zero_suspend,
  56620. + .resume = zero_resume,
  56621. +
  56622. + .driver = {
  56623. + .name = (char *) shortname,
  56624. + // .shutdown = ...
  56625. + // .suspend = ...
  56626. + // .resume = ...
  56627. + },
  56628. +};
  56629. +
  56630. +MODULE_AUTHOR ("David Brownell");
  56631. +MODULE_LICENSE ("Dual BSD/GPL");
  56632. +
  56633. +static struct proc_dir_entry *pdir, *pfile;
  56634. +
  56635. +static int isoc_read_data (char *page, char **start,
  56636. + off_t off, int count,
  56637. + int *eof, void *data)
  56638. +{
  56639. + int i;
  56640. + static int c = 0;
  56641. + static int done = 0;
  56642. + static int s = 0;
  56643. +
  56644. +/*
  56645. + printk ("\ncount: %d\n", count);
  56646. + printk ("rbuf_start: %d\n", rbuf_start);
  56647. + printk ("rbuf_len: %d\n", rbuf_len);
  56648. + printk ("off: %d\n", off);
  56649. + printk ("start: %p\n\n", *start);
  56650. +*/
  56651. + if (done) {
  56652. + c = 0;
  56653. + done = 0;
  56654. + *eof = 1;
  56655. + return 0;
  56656. + }
  56657. +
  56658. + if (c == 0) {
  56659. + if (rbuf_len == RBUF_LEN)
  56660. + s = rbuf_start;
  56661. + else s = 0;
  56662. + }
  56663. +
  56664. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  56665. + page[i] = rbuf[(c+s) % RBUF_LEN];
  56666. + }
  56667. + *start = page;
  56668. +
  56669. + if (c >= rbuf_len) {
  56670. + *eof = 1;
  56671. + done = 1;
  56672. + }
  56673. +
  56674. +
  56675. + return i;
  56676. +}
  56677. +
  56678. +static int __init init (void)
  56679. +{
  56680. +
  56681. + int retval = 0;
  56682. +
  56683. + pdir = proc_mkdir("isoc_test", NULL);
  56684. + if(pdir == NULL) {
  56685. + retval = -ENOMEM;
  56686. + printk("Error creating dir\n");
  56687. + goto done;
  56688. + }
  56689. + pdir->owner = THIS_MODULE;
  56690. +
  56691. + pfile = create_proc_read_entry("isoc_data",
  56692. + 0444, pdir,
  56693. + isoc_read_data,
  56694. + NULL);
  56695. + if (pfile == NULL) {
  56696. + retval = -ENOMEM;
  56697. + printk("Error creating file\n");
  56698. + goto no_file;
  56699. + }
  56700. + pfile->owner = THIS_MODULE;
  56701. +
  56702. + return usb_gadget_register_driver (&zero_driver);
  56703. +
  56704. + no_file:
  56705. + remove_proc_entry("isoc_data", NULL);
  56706. + done:
  56707. + return retval;
  56708. +}
  56709. +module_init (init);
  56710. +
  56711. +static void __exit cleanup (void)
  56712. +{
  56713. +
  56714. + usb_gadget_unregister_driver (&zero_driver);
  56715. +
  56716. + remove_proc_entry("isoc_data", pdir);
  56717. + remove_proc_entry("isoc_test", NULL);
  56718. +}
  56719. +module_exit (cleanup);
  56720. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_cfi_common.h linux-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  56721. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_cfi_common.h 1969-12-31 18:00:00.000000000 -0600
  56722. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h 2014-12-11 14:02:55.388418001 -0600
  56723. @@ -0,0 +1,142 @@
  56724. +/* ==========================================================================
  56725. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  56726. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  56727. + * otherwise expressly agreed to in writing between Synopsys and you.
  56728. + *
  56729. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  56730. + * any End User Software License Agreement or Agreement for Licensed Product
  56731. + * with Synopsys or any supplement thereto. You are permitted to use and
  56732. + * redistribute this Software in source and binary forms, with or without
  56733. + * modification, provided that redistributions of source code must retain this
  56734. + * notice. You may not view, use, disclose, copy or distribute this file or
  56735. + * any information contained herein except pursuant to this license grant from
  56736. + * Synopsys. If you do not agree with this notice, including the disclaimer
  56737. + * below, then you are not authorized to use the Software.
  56738. + *
  56739. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  56740. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56741. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56742. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  56743. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56744. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56745. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56746. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56747. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56748. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  56749. + * DAMAGE.
  56750. + * ========================================================================== */
  56751. +
  56752. +#if !defined(__DWC_CFI_COMMON_H__)
  56753. +#define __DWC_CFI_COMMON_H__
  56754. +
  56755. +//#include <linux/types.h>
  56756. +
  56757. +/**
  56758. + * @file
  56759. + *
  56760. + * This file contains the CFI specific common constants, interfaces
  56761. + * (functions and macros) and structures for Linux. No PCD specific
  56762. + * data structure or definition is to be included in this file.
  56763. + *
  56764. + */
  56765. +
  56766. +/** This is a request for all Core Features */
  56767. +#define VEN_CORE_GET_FEATURES 0xB1
  56768. +
  56769. +/** This is a request to get the value of a specific Core Feature */
  56770. +#define VEN_CORE_GET_FEATURE 0xB2
  56771. +
  56772. +/** This command allows the host to set the value of a specific Core Feature */
  56773. +#define VEN_CORE_SET_FEATURE 0xB3
  56774. +
  56775. +/** This command allows the host to set the default values of
  56776. + * either all or any specific Core Feature
  56777. + */
  56778. +#define VEN_CORE_RESET_FEATURES 0xB4
  56779. +
  56780. +/** This command forces the PCD to write the deferred values of a Core Features */
  56781. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  56782. +
  56783. +/** This request reads a DWORD value from a register at the specified offset */
  56784. +#define VEN_CORE_READ_REGISTER 0xB6
  56785. +
  56786. +/** This request writes a DWORD value into a register at the specified offset */
  56787. +#define VEN_CORE_WRITE_REGISTER 0xB7
  56788. +
  56789. +/** This structure is the header of the Core Features dataset returned to
  56790. + * the Host
  56791. + */
  56792. +struct cfi_all_features_header {
  56793. +/** The features header structure length is */
  56794. +#define CFI_ALL_FEATURES_HDR_LEN 8
  56795. + /**
  56796. + * The total length of the features dataset returned to the Host
  56797. + */
  56798. + uint16_t wTotalLen;
  56799. +
  56800. + /**
  56801. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  56802. + * This field identifies the version of the CFI Specification with which
  56803. + * the device is compliant.
  56804. + */
  56805. + uint16_t wVersion;
  56806. +
  56807. + /** The ID of the Core */
  56808. + uint16_t wCoreID;
  56809. +#define CFI_CORE_ID_UDC 1
  56810. +#define CFI_CORE_ID_OTG 2
  56811. +#define CFI_CORE_ID_WUDEV 3
  56812. +
  56813. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  56814. + uint16_t wNumFeatures;
  56815. +} UPACKED;
  56816. +
  56817. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  56818. +
  56819. +/** This structure is a header of the Core Feature descriptor dataset returned to
  56820. + * the Host after the VEN_CORE_GET_FEATURES request
  56821. + */
  56822. +struct cfi_feature_desc_header {
  56823. +#define CFI_FEATURE_DESC_HDR_LEN 8
  56824. +
  56825. + /** The feature ID */
  56826. + uint16_t wFeatureID;
  56827. +
  56828. + /** Length of this feature descriptor in bytes - including the
  56829. + * length of the feature name string
  56830. + */
  56831. + uint16_t wLength;
  56832. +
  56833. + /** The data length of this feature in bytes */
  56834. + uint16_t wDataLength;
  56835. +
  56836. + /**
  56837. + * Attributes of this features
  56838. + * D0: Access rights
  56839. + * 0 - Read/Write
  56840. + * 1 - Read only
  56841. + */
  56842. + uint8_t bmAttributes;
  56843. +#define CFI_FEATURE_ATTR_RO 1
  56844. +#define CFI_FEATURE_ATTR_RW 0
  56845. +
  56846. + /** Length of the feature name in bytes */
  56847. + uint8_t bNameLen;
  56848. +
  56849. + /** The feature name buffer */
  56850. + //uint8_t *name;
  56851. +} UPACKED;
  56852. +
  56853. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  56854. +
  56855. +/**
  56856. + * This structure describes a NULL terminated string referenced by its id field.
  56857. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  56858. + */
  56859. +struct cfi_string {
  56860. + uint16_t id;
  56861. + const uint8_t *s;
  56862. +};
  56863. +typedef struct cfi_string cfi_string_t;
  56864. +
  56865. +#endif
  56866. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_adp.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  56867. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_adp.c 1969-12-31 18:00:00.000000000 -0600
  56868. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c 2014-12-11 14:02:55.388418001 -0600
  56869. @@ -0,0 +1,854 @@
  56870. +/* ==========================================================================
  56871. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  56872. + * $Revision: #12 $
  56873. + * $Date: 2011/10/26 $
  56874. + * $Change: 1873028 $
  56875. + *
  56876. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  56877. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  56878. + * otherwise expressly agreed to in writing between Synopsys and you.
  56879. + *
  56880. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  56881. + * any End User Software License Agreement or Agreement for Licensed Product
  56882. + * with Synopsys or any supplement thereto. You are permitted to use and
  56883. + * redistribute this Software in source and binary forms, with or without
  56884. + * modification, provided that redistributions of source code must retain this
  56885. + * notice. You may not view, use, disclose, copy or distribute this file or
  56886. + * any information contained herein except pursuant to this license grant from
  56887. + * Synopsys. If you do not agree with this notice, including the disclaimer
  56888. + * below, then you are not authorized to use the Software.
  56889. + *
  56890. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  56891. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56892. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56893. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  56894. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56895. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56896. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56897. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56898. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56899. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  56900. + * DAMAGE.
  56901. + * ========================================================================== */
  56902. +
  56903. +#include "dwc_os.h"
  56904. +#include "dwc_otg_regs.h"
  56905. +#include "dwc_otg_cil.h"
  56906. +#include "dwc_otg_adp.h"
  56907. +
  56908. +/** @file
  56909. + *
  56910. + * This file contains the most of the Attach Detect Protocol implementation for
  56911. + * the driver to support OTG Rev2.0.
  56912. + *
  56913. + */
  56914. +
  56915. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  56916. +{
  56917. + adpctl_data_t adpctl;
  56918. +
  56919. + adpctl.d32 = value;
  56920. + adpctl.b.ar = 0x2;
  56921. +
  56922. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  56923. +
  56924. + while (adpctl.b.ar) {
  56925. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  56926. + }
  56927. +
  56928. +}
  56929. +
  56930. +/**
  56931. + * Function is called to read ADP registers
  56932. + */
  56933. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  56934. +{
  56935. + adpctl_data_t adpctl;
  56936. +
  56937. + adpctl.d32 = 0;
  56938. + adpctl.b.ar = 0x1;
  56939. +
  56940. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  56941. +
  56942. + while (adpctl.b.ar) {
  56943. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  56944. + }
  56945. +
  56946. + return adpctl.d32;
  56947. +}
  56948. +
  56949. +/**
  56950. + * Function is called to read ADPCTL register and filter Write-clear bits
  56951. + */
  56952. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  56953. +{
  56954. + adpctl_data_t adpctl;
  56955. +
  56956. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  56957. + adpctl.b.adp_tmout_int = 0;
  56958. + adpctl.b.adp_prb_int = 0;
  56959. + adpctl.b.adp_tmout_int = 0;
  56960. +
  56961. + return adpctl.d32;
  56962. +}
  56963. +
  56964. +/**
  56965. + * Function is called to write ADP registers
  56966. + */
  56967. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  56968. + uint32_t set)
  56969. +{
  56970. + dwc_otg_adp_write_reg(core_if,
  56971. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  56972. +}
  56973. +
  56974. +static void adp_sense_timeout(void *ptr)
  56975. +{
  56976. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  56977. + core_if->adp.sense_timer_started = 0;
  56978. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  56979. + if (core_if->adp_enable) {
  56980. + dwc_otg_adp_sense_stop(core_if);
  56981. + dwc_otg_adp_probe_start(core_if);
  56982. + }
  56983. +}
  56984. +
  56985. +/**
  56986. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  56987. + */
  56988. +static void adp_vbuson_timeout(void *ptr)
  56989. +{
  56990. + gpwrdn_data_t gpwrdn;
  56991. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  56992. + hprt0_data_t hprt0 = {.d32 = 0 };
  56993. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  56994. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  56995. + if (core_if) {
  56996. + core_if->adp.vbuson_timer_started = 0;
  56997. + /* Turn off vbus */
  56998. + hprt0.b.prtpwr = 1;
  56999. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  57000. + gpwrdn.d32 = 0;
  57001. +
  57002. + /* Power off the core */
  57003. + if (core_if->power_down == 2) {
  57004. + /* Enable Wakeup Logic */
  57005. +// gpwrdn.b.wkupactiv = 1;
  57006. + gpwrdn.b.pmuactv = 0;
  57007. + gpwrdn.b.pwrdnrstn = 1;
  57008. + gpwrdn.b.pwrdnclmp = 1;
  57009. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  57010. + gpwrdn.d32);
  57011. +
  57012. + /* Suspend the Phy Clock */
  57013. + pcgcctl.b.stoppclk = 1;
  57014. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  57015. +
  57016. + /* Switch on VDD */
  57017. +// gpwrdn.b.wkupactiv = 1;
  57018. + gpwrdn.b.pmuactv = 1;
  57019. + gpwrdn.b.pwrdnrstn = 1;
  57020. + gpwrdn.b.pwrdnclmp = 1;
  57021. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  57022. + gpwrdn.d32);
  57023. + } else {
  57024. + /* Enable Power Down Logic */
  57025. + gpwrdn.b.pmuintsel = 1;
  57026. + gpwrdn.b.pmuactv = 1;
  57027. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57028. + }
  57029. +
  57030. + /* Power off the core */
  57031. + if (core_if->power_down == 2) {
  57032. + gpwrdn.d32 = 0;
  57033. + gpwrdn.b.pwrdnswtch = 1;
  57034. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  57035. + gpwrdn.d32, 0);
  57036. + }
  57037. +
  57038. + /* Unmask SRP detected interrupt from Power Down Logic */
  57039. + gpwrdn.d32 = 0;
  57040. + gpwrdn.b.srp_det_msk = 1;
  57041. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57042. +
  57043. + dwc_otg_adp_probe_start(core_if);
  57044. + dwc_otg_dump_global_registers(core_if);
  57045. + dwc_otg_dump_host_registers(core_if);
  57046. + }
  57047. +
  57048. +}
  57049. +
  57050. +/**
  57051. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  57052. + * not asserted within 1.1 seconds.
  57053. + *
  57054. + * @param core_if the pointer to core_if strucure.
  57055. + */
  57056. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  57057. +{
  57058. + core_if->adp.vbuson_timer_started = 1;
  57059. + if (core_if->adp.vbuson_timer)
  57060. + {
  57061. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  57062. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  57063. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  57064. + } else {
  57065. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  57066. + }
  57067. +}
  57068. +
  57069. +#if 0
  57070. +/**
  57071. + * Masks all DWC OTG core interrupts
  57072. + *
  57073. + */
  57074. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  57075. +{
  57076. + int i;
  57077. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  57078. +
  57079. + /* Mask Host Interrupts */
  57080. +
  57081. + /* Clear and disable HCINTs */
  57082. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  57083. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  57084. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  57085. +
  57086. + }
  57087. +
  57088. + /* Clear and disable HAINT */
  57089. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  57090. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  57091. +
  57092. + /* Mask Device Interrupts */
  57093. + if (!core_if->multiproc_int_enable) {
  57094. + /* Clear and disable IN Endpoint interrupts */
  57095. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  57096. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  57097. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  57098. + diepint, 0xFFFFFFFF);
  57099. + }
  57100. +
  57101. + /* Clear and disable OUT Endpoint interrupts */
  57102. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  57103. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  57104. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  57105. + doepint, 0xFFFFFFFF);
  57106. + }
  57107. +
  57108. + /* Clear and disable DAINT */
  57109. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  57110. + 0xFFFFFFFF);
  57111. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  57112. + } else {
  57113. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  57114. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  57115. + diepeachintmsk[i], 0);
  57116. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  57117. + diepint, 0xFFFFFFFF);
  57118. + }
  57119. +
  57120. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  57121. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  57122. + doepeachintmsk[i], 0);
  57123. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  57124. + doepint, 0xFFFFFFFF);
  57125. + }
  57126. +
  57127. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  57128. + 0);
  57129. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  57130. + 0xFFFFFFFF);
  57131. +
  57132. + }
  57133. +
  57134. + /* Disable interrupts */
  57135. + ahbcfg.b.glblintrmsk = 1;
  57136. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  57137. +
  57138. + /* Disable all interrupts. */
  57139. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  57140. +
  57141. + /* Clear any pending interrupts */
  57142. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  57143. +
  57144. + /* Clear any pending OTG Interrupts */
  57145. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  57146. +}
  57147. +
  57148. +/**
  57149. + * Unmask Port Connection Detected interrupt
  57150. + *
  57151. + */
  57152. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  57153. +{
  57154. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  57155. +
  57156. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  57157. +}
  57158. +#endif
  57159. +
  57160. +/**
  57161. + * Starts the ADP Probing
  57162. + *
  57163. + * @param core_if the pointer to core_if structure.
  57164. + */
  57165. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  57166. +{
  57167. +
  57168. + adpctl_data_t adpctl = {.d32 = 0};
  57169. + gpwrdn_data_t gpwrdn;
  57170. +#if 0
  57171. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  57172. + .b.adp_sns_int = 1, b.adp_tmout_int};
  57173. +#endif
  57174. + dwc_otg_disable_global_interrupts(core_if);
  57175. + DWC_PRINTF("ADP Probe Start\n");
  57176. + core_if->adp.probe_enabled = 1;
  57177. +
  57178. + adpctl.b.adpres = 1;
  57179. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57180. +
  57181. + while (adpctl.b.adpres) {
  57182. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57183. + }
  57184. +
  57185. + adpctl.d32 = 0;
  57186. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  57187. +
  57188. + /* In Host mode unmask SRP detected interrupt */
  57189. + gpwrdn.d32 = 0;
  57190. + gpwrdn.b.sts_chngint_msk = 1;
  57191. + if (!gpwrdn.b.idsts) {
  57192. + gpwrdn.b.srp_det_msk = 1;
  57193. + }
  57194. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57195. +
  57196. + adpctl.b.adp_tmout_int_msk = 1;
  57197. + adpctl.b.adp_prb_int_msk = 1;
  57198. + adpctl.b.prb_dschg = 1;
  57199. + adpctl.b.prb_delta = 1;
  57200. + adpctl.b.prb_per = 1;
  57201. + adpctl.b.adpen = 1;
  57202. + adpctl.b.enaprb = 1;
  57203. +
  57204. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57205. + DWC_PRINTF("ADP Probe Finish\n");
  57206. + return 0;
  57207. +}
  57208. +
  57209. +/**
  57210. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  57211. + * within 3 seconds.
  57212. + *
  57213. + * @param core_if the pointer to core_if strucure.
  57214. + */
  57215. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  57216. +{
  57217. + core_if->adp.sense_timer_started = 1;
  57218. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  57219. +}
  57220. +
  57221. +/**
  57222. + * Starts the ADP Sense
  57223. + *
  57224. + * @param core_if the pointer to core_if strucure.
  57225. + */
  57226. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  57227. +{
  57228. + adpctl_data_t adpctl;
  57229. +
  57230. + DWC_PRINTF("ADP Sense Start\n");
  57231. +
  57232. + /* Unmask ADP sense interrupt and mask all other from the core */
  57233. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  57234. + adpctl.b.adp_sns_int_msk = 1;
  57235. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57236. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  57237. +
  57238. + /* Set ADP reset bit*/
  57239. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  57240. + adpctl.b.adpres = 1;
  57241. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57242. +
  57243. + while (adpctl.b.adpres) {
  57244. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57245. + }
  57246. +
  57247. + adpctl.b.adpres = 0;
  57248. + adpctl.b.adpen = 1;
  57249. + adpctl.b.enasns = 1;
  57250. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57251. +
  57252. + dwc_otg_adp_sense_timer_start(core_if);
  57253. +
  57254. + return 0;
  57255. +}
  57256. +
  57257. +/**
  57258. + * Stops the ADP Probing
  57259. + *
  57260. + * @param core_if the pointer to core_if strucure.
  57261. + */
  57262. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  57263. +{
  57264. +
  57265. + adpctl_data_t adpctl;
  57266. + DWC_PRINTF("Stop ADP probe\n");
  57267. + core_if->adp.probe_enabled = 0;
  57268. + core_if->adp.probe_counter = 0;
  57269. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57270. +
  57271. + adpctl.b.adpen = 0;
  57272. + adpctl.b.adp_prb_int = 1;
  57273. + adpctl.b.adp_tmout_int = 1;
  57274. + adpctl.b.adp_sns_int = 1;
  57275. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57276. +
  57277. + return 0;
  57278. +}
  57279. +
  57280. +/**
  57281. + * Stops the ADP Sensing
  57282. + *
  57283. + * @param core_if the pointer to core_if strucure.
  57284. + */
  57285. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  57286. +{
  57287. + adpctl_data_t adpctl;
  57288. +
  57289. + core_if->adp.sense_enabled = 0;
  57290. +
  57291. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  57292. + adpctl.b.enasns = 0;
  57293. + adpctl.b.adp_sns_int = 1;
  57294. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57295. +
  57296. + return 0;
  57297. +}
  57298. +
  57299. +/**
  57300. + * Called to turn on the VBUS after initial ADP probe in host mode.
  57301. + * If port power was already enabled in cil_hcd_start function then
  57302. + * only schedule a timer.
  57303. + *
  57304. + * @param core_if the pointer to core_if structure.
  57305. + */
  57306. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  57307. +{
  57308. + hprt0_data_t hprt0 = {.d32 = 0 };
  57309. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  57310. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  57311. +
  57312. + if (hprt0.b.prtpwr == 0) {
  57313. + hprt0.b.prtpwr = 1;
  57314. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  57315. + }
  57316. +
  57317. + dwc_otg_adp_vbuson_timer_start(core_if);
  57318. +}
  57319. +
  57320. +/**
  57321. + * Called right after driver is loaded
  57322. + * to perform initial actions for ADP
  57323. + *
  57324. + * @param core_if the pointer to core_if structure.
  57325. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  57326. + */
  57327. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  57328. +{
  57329. + gpwrdn_data_t gpwrdn;
  57330. +
  57331. + DWC_PRINTF("ADP Initial Start\n");
  57332. + core_if->adp.adp_started = 1;
  57333. +
  57334. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  57335. + dwc_otg_disable_global_interrupts(core_if);
  57336. + if (is_host) {
  57337. + DWC_PRINTF("HOST MODE\n");
  57338. + /* Enable Power Down Logic Interrupt*/
  57339. + gpwrdn.d32 = 0;
  57340. + gpwrdn.b.pmuintsel = 1;
  57341. + gpwrdn.b.pmuactv = 1;
  57342. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57343. + /* Initialize first ADP probe to obtain Ramp Time value */
  57344. + core_if->adp.initial_probe = 1;
  57345. + dwc_otg_adp_probe_start(core_if);
  57346. + } else {
  57347. + gotgctl_data_t gotgctl;
  57348. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  57349. + DWC_PRINTF("DEVICE MODE\n");
  57350. + if (gotgctl.b.bsesvld == 0) {
  57351. + /* Enable Power Down Logic Interrupt*/
  57352. + gpwrdn.d32 = 0;
  57353. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  57354. + gpwrdn.b.pmuintsel = 1;
  57355. + gpwrdn.b.pmuactv = 1;
  57356. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57357. + core_if->adp.initial_probe = 1;
  57358. + dwc_otg_adp_probe_start(core_if);
  57359. + } else {
  57360. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  57361. + core_if->op_state = B_PERIPHERAL;
  57362. + dwc_otg_core_init(core_if);
  57363. + dwc_otg_enable_global_interrupts(core_if);
  57364. + cil_pcd_start(core_if);
  57365. + dwc_otg_dump_global_registers(core_if);
  57366. + dwc_otg_dump_dev_registers(core_if);
  57367. + }
  57368. + }
  57369. +}
  57370. +
  57371. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  57372. +{
  57373. + core_if->adp.adp_started = 0;
  57374. + core_if->adp.initial_probe = 0;
  57375. + core_if->adp.probe_timer_values[0] = -1;
  57376. + core_if->adp.probe_timer_values[1] = -1;
  57377. + core_if->adp.probe_enabled = 0;
  57378. + core_if->adp.sense_enabled = 0;
  57379. + core_if->adp.sense_timer_started = 0;
  57380. + core_if->adp.vbuson_timer_started = 0;
  57381. + core_if->adp.probe_counter = 0;
  57382. + core_if->adp.gpwrdn = 0;
  57383. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  57384. + /* Initialize timers */
  57385. + core_if->adp.sense_timer =
  57386. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  57387. + core_if->adp.vbuson_timer =
  57388. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  57389. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  57390. + {
  57391. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  57392. + }
  57393. +}
  57394. +
  57395. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  57396. +{
  57397. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  57398. + gpwrdn.b.pmuintsel = 1;
  57399. + gpwrdn.b.pmuactv = 1;
  57400. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  57401. +
  57402. + if (core_if->adp.probe_enabled)
  57403. + dwc_otg_adp_probe_stop(core_if);
  57404. + if (core_if->adp.sense_enabled)
  57405. + dwc_otg_adp_sense_stop(core_if);
  57406. + if (core_if->adp.sense_timer_started)
  57407. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  57408. + if (core_if->adp.vbuson_timer_started)
  57409. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  57410. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  57411. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  57412. +}
  57413. +
  57414. +/////////////////////////////////////////////////////////////////////
  57415. +////////////// ADP Interrupt Handlers ///////////////////////////////
  57416. +/////////////////////////////////////////////////////////////////////
  57417. +/**
  57418. + * This function sets Ramp Timer values
  57419. + */
  57420. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  57421. +{
  57422. + if (core_if->adp.probe_timer_values[0] == -1) {
  57423. + core_if->adp.probe_timer_values[0] = val;
  57424. + core_if->adp.probe_timer_values[1] = -1;
  57425. + return 1;
  57426. + } else {
  57427. + core_if->adp.probe_timer_values[1] =
  57428. + core_if->adp.probe_timer_values[0];
  57429. + core_if->adp.probe_timer_values[0] = val;
  57430. + return 0;
  57431. + }
  57432. +}
  57433. +
  57434. +/**
  57435. + * This function compares Ramp Timer values
  57436. + */
  57437. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  57438. +{
  57439. + uint32_t diff;
  57440. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  57441. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  57442. + else
  57443. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  57444. + if(diff < 2) {
  57445. + return 0;
  57446. + } else {
  57447. + return 1;
  57448. + }
  57449. +}
  57450. +
  57451. +/**
  57452. + * This function handles ADP Probe Interrupts
  57453. + */
  57454. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  57455. + uint32_t val)
  57456. +{
  57457. + adpctl_data_t adpctl = {.d32 = 0 };
  57458. + gpwrdn_data_t gpwrdn, temp;
  57459. + adpctl.d32 = val;
  57460. +
  57461. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  57462. + core_if->adp.probe_counter++;
  57463. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  57464. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  57465. + DWC_PRINTF("RTIM value is 0\n");
  57466. + goto exit;
  57467. + }
  57468. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  57469. + core_if->adp.initial_probe) {
  57470. + core_if->adp.initial_probe = 0;
  57471. + dwc_otg_adp_probe_stop(core_if);
  57472. + gpwrdn.d32 = 0;
  57473. + gpwrdn.b.pmuactv = 1;
  57474. + gpwrdn.b.pmuintsel = 1;
  57475. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  57476. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  57477. +
  57478. + /* check which value is for device mode and which for Host mode */
  57479. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  57480. + /*
  57481. + * Turn on VBUS after initial ADP probe.
  57482. + */
  57483. + core_if->op_state = A_HOST;
  57484. + dwc_otg_enable_global_interrupts(core_if);
  57485. + DWC_SPINUNLOCK(core_if->lock);
  57486. + cil_hcd_start(core_if);
  57487. + dwc_otg_adp_turnon_vbus(core_if);
  57488. + DWC_SPINLOCK(core_if->lock);
  57489. + } else {
  57490. + /*
  57491. + * Initiate SRP after initial ADP probe.
  57492. + */
  57493. + dwc_otg_enable_global_interrupts(core_if);
  57494. + dwc_otg_initiate_srp(core_if);
  57495. + }
  57496. + } else if (core_if->adp.probe_counter > 2){
  57497. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  57498. + if (compare_timer_values(core_if)) {
  57499. + DWC_PRINTF("Difference in timer values !!! \n");
  57500. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  57501. + dwc_otg_adp_probe_stop(core_if);
  57502. +
  57503. + /* Power on the core */
  57504. + if (core_if->power_down == 2) {
  57505. + gpwrdn.b.pwrdnswtch = 1;
  57506. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  57507. + gpwrdn, 0, gpwrdn.d32);
  57508. + }
  57509. +
  57510. + /* check which value is for device mode and which for Host mode */
  57511. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  57512. + /* Disable Interrupt from Power Down Logic */
  57513. + gpwrdn.d32 = 0;
  57514. + gpwrdn.b.pmuintsel = 1;
  57515. + gpwrdn.b.pmuactv = 1;
  57516. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  57517. + gpwrdn, gpwrdn.d32, 0);
  57518. +
  57519. + /*
  57520. + * Initialize the Core for Host mode.
  57521. + */
  57522. + core_if->op_state = A_HOST;
  57523. + dwc_otg_core_init(core_if);
  57524. + dwc_otg_enable_global_interrupts(core_if);
  57525. + cil_hcd_start(core_if);
  57526. + } else {
  57527. + gotgctl_data_t gotgctl;
  57528. + /* Mask SRP detected interrupt from Power Down Logic */
  57529. + gpwrdn.d32 = 0;
  57530. + gpwrdn.b.srp_det_msk = 1;
  57531. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  57532. + gpwrdn, gpwrdn.d32, 0);
  57533. +
  57534. + /* Disable Power Down Logic */
  57535. + gpwrdn.d32 = 0;
  57536. + gpwrdn.b.pmuintsel = 1;
  57537. + gpwrdn.b.pmuactv = 1;
  57538. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  57539. + gpwrdn, gpwrdn.d32, 0);
  57540. +
  57541. + /*
  57542. + * Initialize the Core for Device mode.
  57543. + */
  57544. + core_if->op_state = B_PERIPHERAL;
  57545. + dwc_otg_core_init(core_if);
  57546. + dwc_otg_enable_global_interrupts(core_if);
  57547. + cil_pcd_start(core_if);
  57548. +
  57549. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  57550. + if (!gotgctl.b.bsesvld) {
  57551. + dwc_otg_initiate_srp(core_if);
  57552. + }
  57553. + }
  57554. + }
  57555. + if (core_if->power_down == 2) {
  57556. + if (gpwrdn.b.bsessvld) {
  57557. + /* Mask SRP detected interrupt from Power Down Logic */
  57558. + gpwrdn.d32 = 0;
  57559. + gpwrdn.b.srp_det_msk = 1;
  57560. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  57561. +
  57562. + /* Disable Power Down Logic */
  57563. + gpwrdn.d32 = 0;
  57564. + gpwrdn.b.pmuactv = 1;
  57565. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  57566. +
  57567. + /*
  57568. + * Initialize the Core for Device mode.
  57569. + */
  57570. + core_if->op_state = B_PERIPHERAL;
  57571. + dwc_otg_core_init(core_if);
  57572. + dwc_otg_enable_global_interrupts(core_if);
  57573. + cil_pcd_start(core_if);
  57574. + }
  57575. + }
  57576. + }
  57577. +exit:
  57578. + /* Clear interrupt */
  57579. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57580. + adpctl.b.adp_prb_int = 1;
  57581. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57582. +
  57583. + return 0;
  57584. +}
  57585. +
  57586. +/**
  57587. + * This function hadles ADP Sense Interrupt
  57588. + */
  57589. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  57590. +{
  57591. + adpctl_data_t adpctl;
  57592. + /* Stop ADP Sense timer */
  57593. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  57594. +
  57595. + /* Restart ADP Sense timer */
  57596. + dwc_otg_adp_sense_timer_start(core_if);
  57597. +
  57598. + /* Clear interrupt */
  57599. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57600. + adpctl.b.adp_sns_int = 1;
  57601. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57602. +
  57603. + return 0;
  57604. +}
  57605. +
  57606. +/**
  57607. + * This function handles ADP Probe Interrupts
  57608. + */
  57609. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  57610. + uint32_t val)
  57611. +{
  57612. + adpctl_data_t adpctl = {.d32 = 0 };
  57613. + adpctl.d32 = val;
  57614. + set_timer_value(core_if, adpctl.b.rtim);
  57615. +
  57616. + /* Clear interrupt */
  57617. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57618. + adpctl.b.adp_tmout_int = 1;
  57619. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57620. +
  57621. + return 0;
  57622. +}
  57623. +
  57624. +/**
  57625. + * ADP Interrupt handler.
  57626. + *
  57627. + */
  57628. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  57629. +{
  57630. + int retval = 0;
  57631. + adpctl_data_t adpctl = {.d32 = 0};
  57632. +
  57633. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57634. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  57635. +
  57636. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  57637. + DWC_PRINTF("ADP Sense interrupt\n");
  57638. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  57639. + }
  57640. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  57641. + DWC_PRINTF("ADP timeout interrupt\n");
  57642. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  57643. + }
  57644. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  57645. + DWC_PRINTF("ADP Probe interrupt\n");
  57646. + adpctl.b.adp_prb_int = 1;
  57647. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  57648. + }
  57649. +
  57650. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  57651. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  57652. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  57653. +
  57654. + return retval;
  57655. +}
  57656. +
  57657. +/**
  57658. + *
  57659. + * @param core_if Programming view of DWC_otg controller.
  57660. + */
  57661. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  57662. +{
  57663. +
  57664. +#ifndef DWC_HOST_ONLY
  57665. + hprt0_data_t hprt0;
  57666. + gpwrdn_data_t gpwrdn;
  57667. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  57668. +
  57669. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  57670. + /* check which value is for device mode and which for Host mode */
  57671. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  57672. + DWC_PRINTF("SRP: Host mode\n");
  57673. +
  57674. + if (core_if->adp_enable) {
  57675. + dwc_otg_adp_probe_stop(core_if);
  57676. +
  57677. + /* Power on the core */
  57678. + if (core_if->power_down == 2) {
  57679. + gpwrdn.b.pwrdnswtch = 1;
  57680. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  57681. + gpwrdn, 0, gpwrdn.d32);
  57682. + }
  57683. +
  57684. + core_if->op_state = A_HOST;
  57685. + dwc_otg_core_init(core_if);
  57686. + dwc_otg_enable_global_interrupts(core_if);
  57687. + cil_hcd_start(core_if);
  57688. + }
  57689. +
  57690. + /* Turn on the port power bit. */
  57691. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  57692. + hprt0.b.prtpwr = 1;
  57693. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  57694. +
  57695. + /* Start the Connection timer. So a message can be displayed
  57696. + * if connect does not occur within 10 seconds. */
  57697. + cil_hcd_session_start(core_if);
  57698. + } else {
  57699. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  57700. + if (core_if->adp_enable) {
  57701. + dwc_otg_adp_probe_stop(core_if);
  57702. +
  57703. + /* Power on the core */
  57704. + if (core_if->power_down == 2) {
  57705. + gpwrdn.b.pwrdnswtch = 1;
  57706. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  57707. + gpwrdn, 0, gpwrdn.d32);
  57708. + }
  57709. +
  57710. + gpwrdn.d32 = 0;
  57711. + gpwrdn.b.pmuactv = 0;
  57712. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  57713. + gpwrdn.d32);
  57714. +
  57715. + core_if->op_state = B_PERIPHERAL;
  57716. + dwc_otg_core_init(core_if);
  57717. + dwc_otg_enable_global_interrupts(core_if);
  57718. + cil_pcd_start(core_if);
  57719. + }
  57720. + }
  57721. +#endif
  57722. + return 1;
  57723. +}
  57724. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_adp.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  57725. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_adp.h 1969-12-31 18:00:00.000000000 -0600
  57726. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h 2014-12-11 14:02:55.388418001 -0600
  57727. @@ -0,0 +1,80 @@
  57728. +/* ==========================================================================
  57729. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  57730. + * $Revision: #7 $
  57731. + * $Date: 2011/10/24 $
  57732. + * $Change: 1871159 $
  57733. + *
  57734. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57735. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57736. + * otherwise expressly agreed to in writing between Synopsys and you.
  57737. + *
  57738. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57739. + * any End User Software License Agreement or Agreement for Licensed Product
  57740. + * with Synopsys or any supplement thereto. You are permitted to use and
  57741. + * redistribute this Software in source and binary forms, with or without
  57742. + * modification, provided that redistributions of source code must retain this
  57743. + * notice. You may not view, use, disclose, copy or distribute this file or
  57744. + * any information contained herein except pursuant to this license grant from
  57745. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57746. + * below, then you are not authorized to use the Software.
  57747. + *
  57748. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57749. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57750. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57751. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57752. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57753. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57754. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57755. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57756. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57757. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57758. + * DAMAGE.
  57759. + * ========================================================================== */
  57760. +
  57761. +#ifndef __DWC_OTG_ADP_H__
  57762. +#define __DWC_OTG_ADP_H__
  57763. +
  57764. +/**
  57765. + * @file
  57766. + *
  57767. + * This file contains the Attach Detect Protocol interfaces and defines
  57768. + * (functions) and structures for Linux.
  57769. + *
  57770. + */
  57771. +
  57772. +#define DWC_OTG_ADP_UNATTACHED 0
  57773. +#define DWC_OTG_ADP_ATTACHED 1
  57774. +#define DWC_OTG_ADP_UNKOWN 2
  57775. +
  57776. +typedef struct dwc_otg_adp {
  57777. + uint32_t adp_started;
  57778. + uint32_t initial_probe;
  57779. + int32_t probe_timer_values[2];
  57780. + uint32_t probe_enabled;
  57781. + uint32_t sense_enabled;
  57782. + dwc_timer_t *sense_timer;
  57783. + uint32_t sense_timer_started;
  57784. + dwc_timer_t *vbuson_timer;
  57785. + uint32_t vbuson_timer_started;
  57786. + uint32_t attached;
  57787. + uint32_t probe_counter;
  57788. + uint32_t gpwrdn;
  57789. +} dwc_otg_adp_t;
  57790. +
  57791. +/**
  57792. + * Attach Detect Protocol functions
  57793. + */
  57794. +
  57795. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  57796. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  57797. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  57798. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  57799. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  57800. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  57801. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  57802. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  57803. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  57804. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  57805. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  57806. +
  57807. +#endif //__DWC_OTG_ADP_H__
  57808. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_attr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  57809. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_attr.c 1969-12-31 18:00:00.000000000 -0600
  57810. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c 2014-12-11 14:02:55.388418001 -0600
  57811. @@ -0,0 +1,1210 @@
  57812. +/* ==========================================================================
  57813. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  57814. + * $Revision: #44 $
  57815. + * $Date: 2010/11/29 $
  57816. + * $Change: 1636033 $
  57817. + *
  57818. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57819. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57820. + * otherwise expressly agreed to in writing between Synopsys and you.
  57821. + *
  57822. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57823. + * any End User Software License Agreement or Agreement for Licensed Product
  57824. + * with Synopsys or any supplement thereto. You are permitted to use and
  57825. + * redistribute this Software in source and binary forms, with or without
  57826. + * modification, provided that redistributions of source code must retain this
  57827. + * notice. You may not view, use, disclose, copy or distribute this file or
  57828. + * any information contained herein except pursuant to this license grant from
  57829. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57830. + * below, then you are not authorized to use the Software.
  57831. + *
  57832. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57833. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57834. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57835. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57836. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57837. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57838. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57839. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57840. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57841. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57842. + * DAMAGE.
  57843. + * ========================================================================== */
  57844. +
  57845. +/** @file
  57846. + *
  57847. + * The diagnostic interface will provide access to the controller for
  57848. + * bringing up the hardware and testing. The Linux driver attributes
  57849. + * feature will be used to provide the Linux Diagnostic
  57850. + * Interface. These attributes are accessed through sysfs.
  57851. + */
  57852. +
  57853. +/** @page "Linux Module Attributes"
  57854. + *
  57855. + * The Linux module attributes feature is used to provide the Linux
  57856. + * Diagnostic Interface. These attributes are accessed through sysfs.
  57857. + * The diagnostic interface will provide access to the controller for
  57858. + * bringing up the hardware and testing.
  57859. +
  57860. + The following table shows the attributes.
  57861. + <table>
  57862. + <tr>
  57863. + <td><b> Name</b></td>
  57864. + <td><b> Description</b></td>
  57865. + <td><b> Access</b></td>
  57866. + </tr>
  57867. +
  57868. + <tr>
  57869. + <td> mode </td>
  57870. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  57871. + <td> Read</td>
  57872. + </tr>
  57873. +
  57874. + <tr>
  57875. + <td> hnpcapable </td>
  57876. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  57877. + Read returns the current value.</td>
  57878. + <td> Read/Write</td>
  57879. + </tr>
  57880. +
  57881. + <tr>
  57882. + <td> srpcapable </td>
  57883. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  57884. + Read returns the current value.</td>
  57885. + <td> Read/Write</td>
  57886. + </tr>
  57887. +
  57888. + <tr>
  57889. + <td> hsic_connect </td>
  57890. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  57891. + Read returns the current value.</td>
  57892. + <td> Read/Write</td>
  57893. + </tr>
  57894. +
  57895. + <tr>
  57896. + <td> inv_sel_hsic </td>
  57897. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  57898. + Read returns the current value.</td>
  57899. + <td> Read/Write</td>
  57900. + </tr>
  57901. +
  57902. + <tr>
  57903. + <td> hnp </td>
  57904. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  57905. + <td> Read/Write</td>
  57906. + </tr>
  57907. +
  57908. + <tr>
  57909. + <td> srp </td>
  57910. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  57911. + <td> Read/Write</td>
  57912. + </tr>
  57913. +
  57914. + <tr>
  57915. + <td> buspower </td>
  57916. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  57917. + <td> Read/Write</td>
  57918. + </tr>
  57919. +
  57920. + <tr>
  57921. + <td> bussuspend </td>
  57922. + <td> Suspends the USB bus.</td>
  57923. + <td> Read/Write</td>
  57924. + </tr>
  57925. +
  57926. + <tr>
  57927. + <td> busconnected </td>
  57928. + <td> Gets the connection status of the bus</td>
  57929. + <td> Read</td>
  57930. + </tr>
  57931. +
  57932. + <tr>
  57933. + <td> gotgctl </td>
  57934. + <td> Gets or sets the Core Control Status Register.</td>
  57935. + <td> Read/Write</td>
  57936. + </tr>
  57937. +
  57938. + <tr>
  57939. + <td> gusbcfg </td>
  57940. + <td> Gets or sets the Core USB Configuration Register</td>
  57941. + <td> Read/Write</td>
  57942. + </tr>
  57943. +
  57944. + <tr>
  57945. + <td> grxfsiz </td>
  57946. + <td> Gets or sets the Receive FIFO Size Register</td>
  57947. + <td> Read/Write</td>
  57948. + </tr>
  57949. +
  57950. + <tr>
  57951. + <td> gnptxfsiz </td>
  57952. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  57953. + <td> Read/Write</td>
  57954. + </tr>
  57955. +
  57956. + <tr>
  57957. + <td> gpvndctl </td>
  57958. + <td> Gets or sets the PHY Vendor Control Register</td>
  57959. + <td> Read/Write</td>
  57960. + </tr>
  57961. +
  57962. + <tr>
  57963. + <td> ggpio </td>
  57964. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  57965. + or sets the upper 16 bits.</td>
  57966. + <td> Read/Write</td>
  57967. + </tr>
  57968. +
  57969. + <tr>
  57970. + <td> guid </td>
  57971. + <td> Gets or sets the value of the User ID Register</td>
  57972. + <td> Read/Write</td>
  57973. + </tr>
  57974. +
  57975. + <tr>
  57976. + <td> gsnpsid </td>
  57977. + <td> Gets the value of the Synopsys ID Regester</td>
  57978. + <td> Read</td>
  57979. + </tr>
  57980. +
  57981. + <tr>
  57982. + <td> devspeed </td>
  57983. + <td> Gets or sets the device speed setting in the DCFG register</td>
  57984. + <td> Read/Write</td>
  57985. + </tr>
  57986. +
  57987. + <tr>
  57988. + <td> enumspeed </td>
  57989. + <td> Gets the device enumeration Speed.</td>
  57990. + <td> Read</td>
  57991. + </tr>
  57992. +
  57993. + <tr>
  57994. + <td> hptxfsiz </td>
  57995. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  57996. + <td> Read</td>
  57997. + </tr>
  57998. +
  57999. + <tr>
  58000. + <td> hprt0 </td>
  58001. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  58002. + <td> Read/Write</td>
  58003. + </tr>
  58004. +
  58005. + <tr>
  58006. + <td> regoffset </td>
  58007. + <td> Sets the register offset for the next Register Access</td>
  58008. + <td> Read/Write</td>
  58009. + </tr>
  58010. +
  58011. + <tr>
  58012. + <td> regvalue </td>
  58013. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  58014. + <td> Read/Write</td>
  58015. + </tr>
  58016. +
  58017. + <tr>
  58018. + <td> remote_wakeup </td>
  58019. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  58020. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  58021. + Wakeup signalling bit in the Device Control Register is set for 1
  58022. + milli-second.</td>
  58023. + <td> Read/Write</td>
  58024. + </tr>
  58025. +
  58026. + <tr>
  58027. + <td> rem_wakeup_pwrdn </td>
  58028. + <td> On read, shows the status core - hibernated or not. On write, initiates
  58029. + a remote wakeup of the device from Hibernation. </td>
  58030. + <td> Read/Write</td>
  58031. + </tr>
  58032. +
  58033. + <tr>
  58034. + <td> mode_ch_tim_en </td>
  58035. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  58036. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  58037. + after Suspend or LPM. </td>
  58038. + <td> Read/Write</td>
  58039. + </tr>
  58040. +
  58041. + <tr>
  58042. + <td> fr_interval </td>
  58043. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  58044. + reload HFIR register during runtime. The application can write a value to this
  58045. + register only after the Port Enable bit of the Host Port Control and Status
  58046. + register (HPRT.PrtEnaPort) has been set </td>
  58047. + <td> Read/Write</td>
  58048. + </tr>
  58049. +
  58050. + <tr>
  58051. + <td> disconnect_us </td>
  58052. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  58053. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  58054. + <td> Read/Write</td>
  58055. + </tr>
  58056. +
  58057. + <tr>
  58058. + <td> regdump </td>
  58059. + <td> Dumps the contents of core registers.</td>
  58060. + <td> Read</td>
  58061. + </tr>
  58062. +
  58063. + <tr>
  58064. + <td> spramdump </td>
  58065. + <td> Dumps the contents of core registers.</td>
  58066. + <td> Read</td>
  58067. + </tr>
  58068. +
  58069. + <tr>
  58070. + <td> hcddump </td>
  58071. + <td> Dumps the current HCD state.</td>
  58072. + <td> Read</td>
  58073. + </tr>
  58074. +
  58075. + <tr>
  58076. + <td> hcd_frrem </td>
  58077. + <td> Shows the average value of the Frame Remaining
  58078. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  58079. + occurs. This can be used to determine the average interrupt latency. Also
  58080. + shows the average Frame Remaining value for start_transfer and the "a" and
  58081. + "b" sample points. The "a" and "b" sample points may be used during debugging
  58082. + bto determine how long it takes to execute a section of the HCD code.</td>
  58083. + <td> Read</td>
  58084. + </tr>
  58085. +
  58086. + <tr>
  58087. + <td> rd_reg_test </td>
  58088. + <td> Displays the time required to read the GNPTXFSIZ register many times
  58089. + (the output shows the number of times the register is read).
  58090. + <td> Read</td>
  58091. + </tr>
  58092. +
  58093. + <tr>
  58094. + <td> wr_reg_test </td>
  58095. + <td> Displays the time required to write the GNPTXFSIZ register many times
  58096. + (the output shows the number of times the register is written).
  58097. + <td> Read</td>
  58098. + </tr>
  58099. +
  58100. + <tr>
  58101. + <td> lpm_response </td>
  58102. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  58103. + <td> Write</td>
  58104. + </tr>
  58105. +
  58106. + <tr>
  58107. + <td> sleep_status </td>
  58108. + <td> Shows sleep status of device.
  58109. + <td> Read</td>
  58110. + </tr>
  58111. +
  58112. + </table>
  58113. +
  58114. + Example usage:
  58115. + To get the current mode:
  58116. + cat /sys/devices/lm0/mode
  58117. +
  58118. + To power down the USB:
  58119. + echo 0 > /sys/devices/lm0/buspower
  58120. + */
  58121. +
  58122. +#include "dwc_otg_os_dep.h"
  58123. +#include "dwc_os.h"
  58124. +#include "dwc_otg_driver.h"
  58125. +#include "dwc_otg_attr.h"
  58126. +#include "dwc_otg_core_if.h"
  58127. +#include "dwc_otg_pcd_if.h"
  58128. +#include "dwc_otg_hcd_if.h"
  58129. +
  58130. +/*
  58131. + * MACROs for defining sysfs attribute
  58132. + */
  58133. +#ifdef LM_INTERFACE
  58134. +
  58135. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  58136. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  58137. +{ \
  58138. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  58139. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  58140. + uint32_t val; \
  58141. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  58142. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  58143. +}
  58144. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  58145. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  58146. + const char *buf, size_t count) \
  58147. +{ \
  58148. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  58149. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  58150. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  58151. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  58152. + return count; \
  58153. +}
  58154. +
  58155. +#elif defined(PCI_INTERFACE)
  58156. +
  58157. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  58158. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  58159. +{ \
  58160. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  58161. + uint32_t val; \
  58162. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  58163. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  58164. +}
  58165. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  58166. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  58167. + const char *buf, size_t count) \
  58168. +{ \
  58169. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  58170. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  58171. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  58172. + return count; \
  58173. +}
  58174. +
  58175. +#elif defined(PLATFORM_INTERFACE)
  58176. +
  58177. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  58178. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  58179. +{ \
  58180. + struct platform_device *platform_dev = \
  58181. + container_of(_dev, struct platform_device, dev); \
  58182. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  58183. + uint32_t val; \
  58184. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  58185. + __func__, _dev, platform_dev, otg_dev); \
  58186. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  58187. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  58188. +}
  58189. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  58190. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  58191. + const char *buf, size_t count) \
  58192. +{ \
  58193. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  58194. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  58195. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  58196. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  58197. + return count; \
  58198. +}
  58199. +#endif
  58200. +
  58201. +/*
  58202. + * MACROs for defining sysfs attribute for 32-bit registers
  58203. + */
  58204. +#ifdef LM_INTERFACE
  58205. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  58206. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  58207. +{ \
  58208. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  58209. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  58210. + uint32_t val; \
  58211. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  58212. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  58213. +}
  58214. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  58215. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  58216. + const char *buf, size_t count) \
  58217. +{ \
  58218. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  58219. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  58220. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  58221. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  58222. + return count; \
  58223. +}
  58224. +#elif defined(PCI_INTERFACE)
  58225. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  58226. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  58227. +{ \
  58228. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  58229. + uint32_t val; \
  58230. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  58231. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  58232. +}
  58233. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  58234. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  58235. + const char *buf, size_t count) \
  58236. +{ \
  58237. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  58238. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  58239. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  58240. + return count; \
  58241. +}
  58242. +
  58243. +#elif defined(PLATFORM_INTERFACE)
  58244. +#include "dwc_otg_dbg.h"
  58245. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  58246. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  58247. +{ \
  58248. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  58249. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  58250. + uint32_t val; \
  58251. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  58252. + __func__, _dev, platform_dev, otg_dev); \
  58253. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  58254. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  58255. +}
  58256. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  58257. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  58258. + const char *buf, size_t count) \
  58259. +{ \
  58260. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  58261. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  58262. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  58263. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  58264. + return count; \
  58265. +}
  58266. +
  58267. +#endif
  58268. +
  58269. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  58270. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  58271. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  58272. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  58273. +
  58274. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  58275. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  58276. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  58277. +
  58278. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  58279. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  58280. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  58281. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  58282. +
  58283. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  58284. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  58285. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  58286. +
  58287. +/** @name Functions for Show/Store of Attributes */
  58288. +/**@{*/
  58289. +
  58290. +/**
  58291. + * Helper function returning the otg_device structure of the given device
  58292. + */
  58293. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  58294. +{
  58295. + dwc_otg_device_t *otg_dev;
  58296. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  58297. + return otg_dev;
  58298. +}
  58299. +
  58300. +/**
  58301. + * Show the register offset of the Register Access.
  58302. + */
  58303. +static ssize_t regoffset_show(struct device *_dev,
  58304. + struct device_attribute *attr, char *buf)
  58305. +{
  58306. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58307. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  58308. + otg_dev->os_dep.reg_offset);
  58309. +}
  58310. +
  58311. +/**
  58312. + * Set the register offset for the next Register Access Read/Write
  58313. + */
  58314. +static ssize_t regoffset_store(struct device *_dev,
  58315. + struct device_attribute *attr,
  58316. + const char *buf, size_t count)
  58317. +{
  58318. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58319. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  58320. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  58321. + if (offset < SZ_256K) {
  58322. +#elif defined(PCI_INTERFACE)
  58323. + if (offset < 0x00040000) {
  58324. +#endif
  58325. + otg_dev->os_dep.reg_offset = offset;
  58326. + } else {
  58327. + dev_err(_dev, "invalid offset\n");
  58328. + }
  58329. +
  58330. + return count;
  58331. +}
  58332. +
  58333. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  58334. +
  58335. +/**
  58336. + * Show the value of the register at the offset in the reg_offset
  58337. + * attribute.
  58338. + */
  58339. +static ssize_t regvalue_show(struct device *_dev,
  58340. + struct device_attribute *attr, char *buf)
  58341. +{
  58342. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58343. + uint32_t val;
  58344. + volatile uint32_t *addr;
  58345. +
  58346. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  58347. + /* Calculate the address */
  58348. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  58349. + (uint8_t *) otg_dev->os_dep.base);
  58350. + val = DWC_READ_REG32(addr);
  58351. + return snprintf(buf,
  58352. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  58353. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  58354. + val);
  58355. + } else {
  58356. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  58357. + return sprintf(buf, "invalid offset\n");
  58358. + }
  58359. +}
  58360. +
  58361. +/**
  58362. + * Store the value in the register at the offset in the reg_offset
  58363. + * attribute.
  58364. + *
  58365. + */
  58366. +static ssize_t regvalue_store(struct device *_dev,
  58367. + struct device_attribute *attr,
  58368. + const char *buf, size_t count)
  58369. +{
  58370. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58371. + volatile uint32_t *addr;
  58372. + uint32_t val = simple_strtoul(buf, NULL, 16);
  58373. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  58374. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  58375. + /* Calculate the address */
  58376. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  58377. + (uint8_t *) otg_dev->os_dep.base);
  58378. + DWC_WRITE_REG32(addr, val);
  58379. + } else {
  58380. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  58381. + otg_dev->os_dep.reg_offset);
  58382. + }
  58383. + return count;
  58384. +}
  58385. +
  58386. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  58387. +
  58388. +/*
  58389. + * Attributes
  58390. + */
  58391. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  58392. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  58393. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  58394. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  58395. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  58396. +
  58397. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  58398. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  58399. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  58400. +
  58401. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  58402. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  58403. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  58404. + "GUSBCFG");
  58405. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  58406. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  58407. + "GRXFSIZ");
  58408. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  58409. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  58410. + "GNPTXFSIZ");
  58411. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  58412. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  58413. + "GPVNDCTL");
  58414. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  58415. + &(otg_dev->core_if->core_global_regs->ggpio),
  58416. + "GGPIO");
  58417. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  58418. + "GUID");
  58419. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  58420. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  58421. + "GSNPSID");
  58422. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  58423. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  58424. +
  58425. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  58426. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  58427. + "HPTXFSIZ");
  58428. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  58429. +
  58430. +/**
  58431. + * @todo Add code to initiate the HNP.
  58432. + */
  58433. +/**
  58434. + * Show the HNP status bit
  58435. + */
  58436. +static ssize_t hnp_show(struct device *_dev,
  58437. + struct device_attribute *attr, char *buf)
  58438. +{
  58439. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58440. + return sprintf(buf, "HstNegScs = 0x%x\n",
  58441. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  58442. +}
  58443. +
  58444. +/**
  58445. + * Set the HNP Request bit
  58446. + */
  58447. +static ssize_t hnp_store(struct device *_dev,
  58448. + struct device_attribute *attr,
  58449. + const char *buf, size_t count)
  58450. +{
  58451. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58452. + uint32_t in = simple_strtoul(buf, NULL, 16);
  58453. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  58454. + return count;
  58455. +}
  58456. +
  58457. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  58458. +
  58459. +/**
  58460. + * @todo Add code to initiate the SRP.
  58461. + */
  58462. +/**
  58463. + * Show the SRP status bit
  58464. + */
  58465. +static ssize_t srp_show(struct device *_dev,
  58466. + struct device_attribute *attr, char *buf)
  58467. +{
  58468. +#ifndef DWC_HOST_ONLY
  58469. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58470. + return sprintf(buf, "SesReqScs = 0x%x\n",
  58471. + dwc_otg_get_srpstatus(otg_dev->core_if));
  58472. +#else
  58473. + return sprintf(buf, "Host Only Mode!\n");
  58474. +#endif
  58475. +}
  58476. +
  58477. +/**
  58478. + * Set the SRP Request bit
  58479. + */
  58480. +static ssize_t srp_store(struct device *_dev,
  58481. + struct device_attribute *attr,
  58482. + const char *buf, size_t count)
  58483. +{
  58484. +#ifndef DWC_HOST_ONLY
  58485. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58486. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  58487. +#endif
  58488. + return count;
  58489. +}
  58490. +
  58491. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  58492. +
  58493. +/**
  58494. + * @todo Need to do more for power on/off?
  58495. + */
  58496. +/**
  58497. + * Show the Bus Power status
  58498. + */
  58499. +static ssize_t buspower_show(struct device *_dev,
  58500. + struct device_attribute *attr, char *buf)
  58501. +{
  58502. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58503. + return sprintf(buf, "Bus Power = 0x%x\n",
  58504. + dwc_otg_get_prtpower(otg_dev->core_if));
  58505. +}
  58506. +
  58507. +/**
  58508. + * Set the Bus Power status
  58509. + */
  58510. +static ssize_t buspower_store(struct device *_dev,
  58511. + struct device_attribute *attr,
  58512. + const char *buf, size_t count)
  58513. +{
  58514. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58515. + uint32_t on = simple_strtoul(buf, NULL, 16);
  58516. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  58517. + return count;
  58518. +}
  58519. +
  58520. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  58521. +
  58522. +/**
  58523. + * @todo Need to do more for suspend?
  58524. + */
  58525. +/**
  58526. + * Show the Bus Suspend status
  58527. + */
  58528. +static ssize_t bussuspend_show(struct device *_dev,
  58529. + struct device_attribute *attr, char *buf)
  58530. +{
  58531. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58532. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  58533. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  58534. +}
  58535. +
  58536. +/**
  58537. + * Set the Bus Suspend status
  58538. + */
  58539. +static ssize_t bussuspend_store(struct device *_dev,
  58540. + struct device_attribute *attr,
  58541. + const char *buf, size_t count)
  58542. +{
  58543. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58544. + uint32_t in = simple_strtoul(buf, NULL, 16);
  58545. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  58546. + return count;
  58547. +}
  58548. +
  58549. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  58550. +
  58551. +/**
  58552. + * Show the Mode Change Ready Timer status
  58553. + */
  58554. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  58555. + struct device_attribute *attr, char *buf)
  58556. +{
  58557. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58558. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  58559. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  58560. +}
  58561. +
  58562. +/**
  58563. + * Set the Mode Change Ready Timer status
  58564. + */
  58565. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  58566. + struct device_attribute *attr,
  58567. + const char *buf, size_t count)
  58568. +{
  58569. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58570. + uint32_t in = simple_strtoul(buf, NULL, 16);
  58571. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  58572. + return count;
  58573. +}
  58574. +
  58575. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  58576. +
  58577. +/**
  58578. + * Show the value of HFIR Frame Interval bitfield
  58579. + */
  58580. +static ssize_t fr_interval_show(struct device *_dev,
  58581. + struct device_attribute *attr, char *buf)
  58582. +{
  58583. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58584. + return sprintf(buf, "Frame Interval = 0x%x\n",
  58585. + dwc_otg_get_fr_interval(otg_dev->core_if));
  58586. +}
  58587. +
  58588. +/**
  58589. + * Set the HFIR Frame Interval value
  58590. + */
  58591. +static ssize_t fr_interval_store(struct device *_dev,
  58592. + struct device_attribute *attr,
  58593. + const char *buf, size_t count)
  58594. +{
  58595. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58596. + uint32_t in = simple_strtoul(buf, NULL, 10);
  58597. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  58598. + return count;
  58599. +}
  58600. +
  58601. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  58602. +
  58603. +/**
  58604. + * Show the status of Remote Wakeup.
  58605. + */
  58606. +static ssize_t remote_wakeup_show(struct device *_dev,
  58607. + struct device_attribute *attr, char *buf)
  58608. +{
  58609. +#ifndef DWC_HOST_ONLY
  58610. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58611. +
  58612. + return sprintf(buf,
  58613. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  58614. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  58615. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  58616. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  58617. +#else
  58618. + return sprintf(buf, "Host Only Mode!\n");
  58619. +#endif /* DWC_HOST_ONLY */
  58620. +}
  58621. +
  58622. +/**
  58623. + * Initiate a remote wakeup of the host. The Device control register
  58624. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  58625. + * flag is set.
  58626. + *
  58627. + */
  58628. +static ssize_t remote_wakeup_store(struct device *_dev,
  58629. + struct device_attribute *attr,
  58630. + const char *buf, size_t count)
  58631. +{
  58632. +#ifndef DWC_HOST_ONLY
  58633. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58634. + uint32_t val = simple_strtoul(buf, NULL, 16);
  58635. +
  58636. + if (val & 1) {
  58637. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  58638. + } else {
  58639. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  58640. + }
  58641. +#endif /* DWC_HOST_ONLY */
  58642. + return count;
  58643. +}
  58644. +
  58645. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  58646. + remote_wakeup_store);
  58647. +
  58648. +/**
  58649. + * Show the whether core is hibernated or not.
  58650. + */
  58651. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  58652. + struct device_attribute *attr, char *buf)
  58653. +{
  58654. +#ifndef DWC_HOST_ONLY
  58655. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58656. +
  58657. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  58658. + DWC_PRINTF("Core is in hibernation\n");
  58659. + } else {
  58660. + DWC_PRINTF("Core is not in hibernation\n");
  58661. + }
  58662. +#endif /* DWC_HOST_ONLY */
  58663. + return 0;
  58664. +}
  58665. +
  58666. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  58667. + int rem_wakeup, int reset);
  58668. +
  58669. +/**
  58670. + * Initiate a remote wakeup of the device to exit from hibernation.
  58671. + */
  58672. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  58673. + struct device_attribute *attr,
  58674. + const char *buf, size_t count)
  58675. +{
  58676. +#ifndef DWC_HOST_ONLY
  58677. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58678. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  58679. +#endif
  58680. + return count;
  58681. +}
  58682. +
  58683. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  58684. + rem_wakeup_pwrdn_store);
  58685. +
  58686. +static ssize_t disconnect_us(struct device *_dev,
  58687. + struct device_attribute *attr,
  58688. + const char *buf, size_t count)
  58689. +{
  58690. +
  58691. +#ifndef DWC_HOST_ONLY
  58692. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58693. + uint32_t val = simple_strtoul(buf, NULL, 16);
  58694. + DWC_PRINTF("The Passed value is %04x\n", val);
  58695. +
  58696. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  58697. +
  58698. +#endif /* DWC_HOST_ONLY */
  58699. + return count;
  58700. +}
  58701. +
  58702. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  58703. +
  58704. +/**
  58705. + * Dump global registers and either host or device registers (depending on the
  58706. + * current mode of the core).
  58707. + */
  58708. +static ssize_t regdump_show(struct device *_dev,
  58709. + struct device_attribute *attr, char *buf)
  58710. +{
  58711. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58712. +
  58713. + dwc_otg_dump_global_registers(otg_dev->core_if);
  58714. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  58715. + dwc_otg_dump_host_registers(otg_dev->core_if);
  58716. + } else {
  58717. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  58718. +
  58719. + }
  58720. + return sprintf(buf, "Register Dump\n");
  58721. +}
  58722. +
  58723. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  58724. +
  58725. +/**
  58726. + * Dump global registers and either host or device registers (depending on the
  58727. + * current mode of the core).
  58728. + */
  58729. +static ssize_t spramdump_show(struct device *_dev,
  58730. + struct device_attribute *attr, char *buf)
  58731. +{
  58732. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58733. +
  58734. + //dwc_otg_dump_spram(otg_dev->core_if);
  58735. +
  58736. + return sprintf(buf, "SPRAM Dump\n");
  58737. +}
  58738. +
  58739. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  58740. +
  58741. +/**
  58742. + * Dump the current hcd state.
  58743. + */
  58744. +static ssize_t hcddump_show(struct device *_dev,
  58745. + struct device_attribute *attr, char *buf)
  58746. +{
  58747. +#ifndef DWC_DEVICE_ONLY
  58748. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58749. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  58750. +#endif /* DWC_DEVICE_ONLY */
  58751. + return sprintf(buf, "HCD Dump\n");
  58752. +}
  58753. +
  58754. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  58755. +
  58756. +/**
  58757. + * Dump the average frame remaining at SOF. This can be used to
  58758. + * determine average interrupt latency. Frame remaining is also shown for
  58759. + * start transfer and two additional sample points.
  58760. + */
  58761. +static ssize_t hcd_frrem_show(struct device *_dev,
  58762. + struct device_attribute *attr, char *buf)
  58763. +{
  58764. +#ifndef DWC_DEVICE_ONLY
  58765. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58766. +
  58767. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  58768. +#endif /* DWC_DEVICE_ONLY */
  58769. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  58770. +}
  58771. +
  58772. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  58773. +
  58774. +/**
  58775. + * Displays the time required to read the GNPTXFSIZ register many times (the
  58776. + * output shows the number of times the register is read).
  58777. + */
  58778. +#define RW_REG_COUNT 10000000
  58779. +#define MSEC_PER_JIFFIE 1000/HZ
  58780. +static ssize_t rd_reg_test_show(struct device *_dev,
  58781. + struct device_attribute *attr, char *buf)
  58782. +{
  58783. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58784. + int i;
  58785. + int time;
  58786. + int start_jiffies;
  58787. +
  58788. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  58789. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  58790. + start_jiffies = jiffies;
  58791. + for (i = 0; i < RW_REG_COUNT; i++) {
  58792. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  58793. + }
  58794. + time = jiffies - start_jiffies;
  58795. + return sprintf(buf,
  58796. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  58797. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  58798. +}
  58799. +
  58800. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  58801. +
  58802. +/**
  58803. + * Displays the time required to write the GNPTXFSIZ register many times (the
  58804. + * output shows the number of times the register is written).
  58805. + */
  58806. +static ssize_t wr_reg_test_show(struct device *_dev,
  58807. + struct device_attribute *attr, char *buf)
  58808. +{
  58809. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58810. + uint32_t reg_val;
  58811. + int i;
  58812. + int time;
  58813. + int start_jiffies;
  58814. +
  58815. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  58816. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  58817. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  58818. + start_jiffies = jiffies;
  58819. + for (i = 0; i < RW_REG_COUNT; i++) {
  58820. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  58821. + }
  58822. + time = jiffies - start_jiffies;
  58823. + return sprintf(buf,
  58824. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  58825. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  58826. +}
  58827. +
  58828. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  58829. +
  58830. +#ifdef CONFIG_USB_DWC_OTG_LPM
  58831. +
  58832. +/**
  58833. +* Show the lpm_response attribute.
  58834. +*/
  58835. +static ssize_t lpmresp_show(struct device *_dev,
  58836. + struct device_attribute *attr, char *buf)
  58837. +{
  58838. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58839. +
  58840. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  58841. + return sprintf(buf, "** LPM is DISABLED **\n");
  58842. +
  58843. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  58844. + return sprintf(buf, "** Current mode is not device mode\n");
  58845. + }
  58846. + return sprintf(buf, "lpm_response = %d\n",
  58847. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  58848. +}
  58849. +
  58850. +/**
  58851. +* Store the lpm_response attribute.
  58852. +*/
  58853. +static ssize_t lpmresp_store(struct device *_dev,
  58854. + struct device_attribute *attr,
  58855. + const char *buf, size_t count)
  58856. +{
  58857. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58858. + uint32_t val = simple_strtoul(buf, NULL, 16);
  58859. +
  58860. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  58861. + return 0;
  58862. + }
  58863. +
  58864. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  58865. + return 0;
  58866. + }
  58867. +
  58868. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  58869. + return count;
  58870. +}
  58871. +
  58872. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  58873. +
  58874. +/**
  58875. +* Show the sleep_status attribute.
  58876. +*/
  58877. +static ssize_t sleepstatus_show(struct device *_dev,
  58878. + struct device_attribute *attr, char *buf)
  58879. +{
  58880. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58881. + return sprintf(buf, "Sleep Status = %d\n",
  58882. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  58883. +}
  58884. +
  58885. +/**
  58886. + * Store the sleep_status attribure.
  58887. + */
  58888. +static ssize_t sleepstatus_store(struct device *_dev,
  58889. + struct device_attribute *attr,
  58890. + const char *buf, size_t count)
  58891. +{
  58892. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  58893. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  58894. +
  58895. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  58896. + if (dwc_otg_is_host_mode(core_if)) {
  58897. +
  58898. + DWC_PRINTF("Host initiated resume\n");
  58899. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  58900. + }
  58901. + }
  58902. +
  58903. + return count;
  58904. +}
  58905. +
  58906. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  58907. + sleepstatus_store);
  58908. +
  58909. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  58910. +
  58911. +/**@}*/
  58912. +
  58913. +/**
  58914. + * Create the device files
  58915. + */
  58916. +void dwc_otg_attr_create(
  58917. +#ifdef LM_INTERFACE
  58918. + struct lm_device *dev
  58919. +#elif defined(PCI_INTERFACE)
  58920. + struct pci_dev *dev
  58921. +#elif defined(PLATFORM_INTERFACE)
  58922. + struct platform_device *dev
  58923. +#endif
  58924. + )
  58925. +{
  58926. + int error;
  58927. +
  58928. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  58929. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  58930. + error = device_create_file(&dev->dev, &dev_attr_mode);
  58931. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  58932. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  58933. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  58934. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  58935. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  58936. + error = device_create_file(&dev->dev, &dev_attr_srp);
  58937. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  58938. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  58939. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  58940. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  58941. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  58942. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  58943. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  58944. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  58945. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  58946. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  58947. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  58948. + error = device_create_file(&dev->dev, &dev_attr_guid);
  58949. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  58950. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  58951. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  58952. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  58953. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  58954. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  58955. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  58956. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  58957. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  58958. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  58959. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  58960. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  58961. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  58962. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  58963. +#ifdef CONFIG_USB_DWC_OTG_LPM
  58964. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  58965. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  58966. +#endif
  58967. +}
  58968. +
  58969. +/**
  58970. + * Remove the device files
  58971. + */
  58972. +void dwc_otg_attr_remove(
  58973. +#ifdef LM_INTERFACE
  58974. + struct lm_device *dev
  58975. +#elif defined(PCI_INTERFACE)
  58976. + struct pci_dev *dev
  58977. +#elif defined(PLATFORM_INTERFACE)
  58978. + struct platform_device *dev
  58979. +#endif
  58980. + )
  58981. +{
  58982. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  58983. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  58984. + device_remove_file(&dev->dev, &dev_attr_mode);
  58985. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  58986. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  58987. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  58988. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  58989. + device_remove_file(&dev->dev, &dev_attr_hnp);
  58990. + device_remove_file(&dev->dev, &dev_attr_srp);
  58991. + device_remove_file(&dev->dev, &dev_attr_buspower);
  58992. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  58993. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  58994. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  58995. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  58996. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  58997. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  58998. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  58999. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  59000. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  59001. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  59002. + device_remove_file(&dev->dev, &dev_attr_guid);
  59003. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  59004. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  59005. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  59006. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  59007. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  59008. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  59009. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  59010. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  59011. + device_remove_file(&dev->dev, &dev_attr_regdump);
  59012. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  59013. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  59014. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  59015. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  59016. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  59017. +#ifdef CONFIG_USB_DWC_OTG_LPM
  59018. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  59019. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  59020. +#endif
  59021. +}
  59022. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_attr.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  59023. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_attr.h 1969-12-31 18:00:00.000000000 -0600
  59024. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h 2014-12-11 14:02:55.388418001 -0600
  59025. @@ -0,0 +1,89 @@
  59026. +/* ==========================================================================
  59027. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  59028. + * $Revision: #13 $
  59029. + * $Date: 2010/06/21 $
  59030. + * $Change: 1532021 $
  59031. + *
  59032. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  59033. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  59034. + * otherwise expressly agreed to in writing between Synopsys and you.
  59035. + *
  59036. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  59037. + * any End User Software License Agreement or Agreement for Licensed Product
  59038. + * with Synopsys or any supplement thereto. You are permitted to use and
  59039. + * redistribute this Software in source and binary forms, with or without
  59040. + * modification, provided that redistributions of source code must retain this
  59041. + * notice. You may not view, use, disclose, copy or distribute this file or
  59042. + * any information contained herein except pursuant to this license grant from
  59043. + * Synopsys. If you do not agree with this notice, including the disclaimer
  59044. + * below, then you are not authorized to use the Software.
  59045. + *
  59046. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  59047. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59048. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59049. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  59050. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  59051. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59052. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  59053. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  59054. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59055. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  59056. + * DAMAGE.
  59057. + * ========================================================================== */
  59058. +
  59059. +#if !defined(__DWC_OTG_ATTR_H__)
  59060. +#define __DWC_OTG_ATTR_H__
  59061. +
  59062. +/** @file
  59063. + * This file contains the interface to the Linux device attributes.
  59064. + */
  59065. +extern struct device_attribute dev_attr_regoffset;
  59066. +extern struct device_attribute dev_attr_regvalue;
  59067. +
  59068. +extern struct device_attribute dev_attr_mode;
  59069. +extern struct device_attribute dev_attr_hnpcapable;
  59070. +extern struct device_attribute dev_attr_srpcapable;
  59071. +extern struct device_attribute dev_attr_hnp;
  59072. +extern struct device_attribute dev_attr_srp;
  59073. +extern struct device_attribute dev_attr_buspower;
  59074. +extern struct device_attribute dev_attr_bussuspend;
  59075. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  59076. +extern struct device_attribute dev_attr_fr_interval;
  59077. +extern struct device_attribute dev_attr_busconnected;
  59078. +extern struct device_attribute dev_attr_gotgctl;
  59079. +extern struct device_attribute dev_attr_gusbcfg;
  59080. +extern struct device_attribute dev_attr_grxfsiz;
  59081. +extern struct device_attribute dev_attr_gnptxfsiz;
  59082. +extern struct device_attribute dev_attr_gpvndctl;
  59083. +extern struct device_attribute dev_attr_ggpio;
  59084. +extern struct device_attribute dev_attr_guid;
  59085. +extern struct device_attribute dev_attr_gsnpsid;
  59086. +extern struct device_attribute dev_attr_devspeed;
  59087. +extern struct device_attribute dev_attr_enumspeed;
  59088. +extern struct device_attribute dev_attr_hptxfsiz;
  59089. +extern struct device_attribute dev_attr_hprt0;
  59090. +#ifdef CONFIG_USB_DWC_OTG_LPM
  59091. +extern struct device_attribute dev_attr_lpm_response;
  59092. +extern struct device_attribute devi_attr_sleep_status;
  59093. +#endif
  59094. +
  59095. +void dwc_otg_attr_create(
  59096. +#ifdef LM_INTERFACE
  59097. + struct lm_device *dev
  59098. +#elif defined(PCI_INTERFACE)
  59099. + struct pci_dev *dev
  59100. +#elif defined(PLATFORM_INTERFACE)
  59101. + struct platform_device *dev
  59102. +#endif
  59103. + );
  59104. +
  59105. +void dwc_otg_attr_remove(
  59106. +#ifdef LM_INTERFACE
  59107. + struct lm_device *dev
  59108. +#elif defined(PCI_INTERFACE)
  59109. + struct pci_dev *dev
  59110. +#elif defined(PLATFORM_INTERFACE)
  59111. + struct platform_device *dev
  59112. +#endif
  59113. + );
  59114. +#endif
  59115. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cfi.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  59116. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 1969-12-31 18:00:00.000000000 -0600
  59117. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 2014-12-11 14:02:55.388418001 -0600
  59118. @@ -0,0 +1,1876 @@
  59119. +/* ==========================================================================
  59120. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  59121. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  59122. + * otherwise expressly agreed to in writing between Synopsys and you.
  59123. + *
  59124. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  59125. + * any End User Software License Agreement or Agreement for Licensed Product
  59126. + * with Synopsys or any supplement thereto. You are permitted to use and
  59127. + * redistribute this Software in source and binary forms, with or without
  59128. + * modification, provided that redistributions of source code must retain this
  59129. + * notice. You may not view, use, disclose, copy or distribute this file or
  59130. + * any information contained herein except pursuant to this license grant from
  59131. + * Synopsys. If you do not agree with this notice, including the disclaimer
  59132. + * below, then you are not authorized to use the Software.
  59133. + *
  59134. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  59135. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59136. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59137. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  59138. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  59139. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59140. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  59141. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  59142. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59143. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  59144. + * DAMAGE.
  59145. + * ========================================================================== */
  59146. +
  59147. +/** @file
  59148. + *
  59149. + * This file contains the most of the CFI(Core Feature Interface)
  59150. + * implementation for the OTG.
  59151. + */
  59152. +
  59153. +#ifdef DWC_UTE_CFI
  59154. +
  59155. +#include "dwc_otg_pcd.h"
  59156. +#include "dwc_otg_cfi.h"
  59157. +
  59158. +/** This definition should actually migrate to the Portability Library */
  59159. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  59160. +
  59161. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  59162. +
  59163. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  59164. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  59165. + struct dwc_otg_pcd *pcd,
  59166. + struct cfi_usb_ctrlrequest *ctrl_req);
  59167. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  59168. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  59169. + struct cfi_usb_ctrlrequest *req);
  59170. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  59171. + struct cfi_usb_ctrlrequest *req);
  59172. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  59173. + struct cfi_usb_ctrlrequest *req);
  59174. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  59175. + struct cfi_usb_ctrlrequest *req);
  59176. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  59177. +
  59178. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  59179. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  59180. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  59181. +
  59182. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  59183. +
  59184. +/** This is the header of the all features descriptor */
  59185. +static cfi_all_features_header_t all_props_desc_header = {
  59186. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  59187. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  59188. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  59189. +};
  59190. +
  59191. +/** This is an array of statically allocated feature descriptors */
  59192. +static cfi_feature_desc_header_t prop_descs[] = {
  59193. +
  59194. + /* FT_ID_DMA_MODE */
  59195. + {
  59196. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  59197. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59198. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  59199. + },
  59200. +
  59201. + /* FT_ID_DMA_BUFFER_SETUP */
  59202. + {
  59203. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  59204. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59205. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  59206. + },
  59207. +
  59208. + /* FT_ID_DMA_BUFF_ALIGN */
  59209. + {
  59210. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  59211. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59212. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  59213. + },
  59214. +
  59215. + /* FT_ID_DMA_CONCAT_SETUP */
  59216. + {
  59217. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  59218. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59219. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  59220. + },
  59221. +
  59222. + /* FT_ID_DMA_CIRCULAR */
  59223. + {
  59224. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  59225. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59226. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  59227. + },
  59228. +
  59229. + /* FT_ID_THRESHOLD_SETUP */
  59230. + {
  59231. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  59232. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59233. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  59234. + },
  59235. +
  59236. + /* FT_ID_DFIFO_DEPTH */
  59237. + {
  59238. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  59239. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  59240. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  59241. + },
  59242. +
  59243. + /* FT_ID_TX_FIFO_DEPTH */
  59244. + {
  59245. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  59246. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59247. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  59248. + },
  59249. +
  59250. + /* FT_ID_RX_FIFO_DEPTH */
  59251. + {
  59252. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  59253. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  59254. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  59255. + }
  59256. +};
  59257. +
  59258. +/** The table of feature names */
  59259. +cfi_string_t prop_name_table[] = {
  59260. + {FT_ID_DMA_MODE, "dma_mode"},
  59261. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  59262. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  59263. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  59264. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  59265. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  59266. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  59267. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  59268. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  59269. + {}
  59270. +};
  59271. +
  59272. +/************************************************************************/
  59273. +
  59274. +/**
  59275. + * Returns the name of the feature by its ID
  59276. + * or NULL if no featute ID matches.
  59277. + *
  59278. + */
  59279. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  59280. +{
  59281. + cfi_string_t *pstr;
  59282. + *len = 0;
  59283. +
  59284. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  59285. + if (pstr->id == prop_id) {
  59286. + *len = DWC_STRLEN(pstr->s);
  59287. + return pstr->s;
  59288. + }
  59289. + }
  59290. + return NULL;
  59291. +}
  59292. +
  59293. +/**
  59294. + * This function handles all CFI specific control requests.
  59295. + *
  59296. + * Return a negative value to stall the DCE.
  59297. + */
  59298. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  59299. +{
  59300. + int retval = 0;
  59301. + dwc_otg_pcd_ep_t *ep = NULL;
  59302. + cfiobject_t *cfi = pcd->cfi;
  59303. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  59304. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  59305. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  59306. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  59307. + uint32_t regaddr = 0;
  59308. + uint32_t regval = 0;
  59309. +
  59310. + /* Save this Control Request in the CFI object.
  59311. + * The data field will be assigned in the data stage completion CB function.
  59312. + */
  59313. + cfi->ctrl_req = *ctrl;
  59314. + cfi->ctrl_req.data = NULL;
  59315. +
  59316. + cfi->need_gadget_att = 0;
  59317. + cfi->need_status_in_complete = 0;
  59318. +
  59319. + switch (ctrl->bRequest) {
  59320. + case VEN_CORE_GET_FEATURES:
  59321. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  59322. + if (retval >= 0) {
  59323. + //dump_msg(cfi->buf_in.buf, retval);
  59324. + ep = &pcd->ep0;
  59325. +
  59326. + retval = min((uint16_t) retval, wLen);
  59327. + /* Transfer this buffer to the host through the EP0-IN EP */
  59328. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  59329. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  59330. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  59331. + ep->dwc_ep.xfer_len = retval;
  59332. + ep->dwc_ep.xfer_count = 0;
  59333. + ep->dwc_ep.sent_zlp = 0;
  59334. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  59335. +
  59336. + pcd->ep0_pending = 1;
  59337. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  59338. + }
  59339. + retval = 0;
  59340. + break;
  59341. +
  59342. + case VEN_CORE_GET_FEATURE:
  59343. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  59344. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  59345. + pcd, ctrl);
  59346. + if (retval >= 0) {
  59347. + ep = &pcd->ep0;
  59348. +
  59349. + retval = min((uint16_t) retval, wLen);
  59350. + /* Transfer this buffer to the host through the EP0-IN EP */
  59351. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  59352. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  59353. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  59354. + ep->dwc_ep.xfer_len = retval;
  59355. + ep->dwc_ep.xfer_count = 0;
  59356. + ep->dwc_ep.sent_zlp = 0;
  59357. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  59358. +
  59359. + pcd->ep0_pending = 1;
  59360. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  59361. + }
  59362. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  59363. + dump_msg(cfi->buf_in.buf, retval);
  59364. + break;
  59365. +
  59366. + case VEN_CORE_SET_FEATURE:
  59367. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  59368. + /* Set up an XFER to get the data stage of the control request,
  59369. + * which is the new value of the feature to be modified.
  59370. + */
  59371. + ep = &pcd->ep0;
  59372. + ep->dwc_ep.is_in = 0;
  59373. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  59374. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  59375. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  59376. + ep->dwc_ep.xfer_len = wLen;
  59377. + ep->dwc_ep.xfer_count = 0;
  59378. + ep->dwc_ep.sent_zlp = 0;
  59379. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  59380. +
  59381. + pcd->ep0_pending = 1;
  59382. + /* Read the control write's data stage */
  59383. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  59384. + retval = 0;
  59385. + break;
  59386. +
  59387. + case VEN_CORE_RESET_FEATURES:
  59388. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  59389. + cfi->need_gadget_att = 1;
  59390. + cfi->need_status_in_complete = 1;
  59391. + retval = cfi_preproc_reset(pcd, ctrl);
  59392. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  59393. + break;
  59394. +
  59395. + case VEN_CORE_ACTIVATE_FEATURES:
  59396. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  59397. + break;
  59398. +
  59399. + case VEN_CORE_READ_REGISTER:
  59400. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  59401. + /* wValue optionally contains the HI WORD of the register offset and
  59402. + * wIndex contains the LOW WORD of the register offset
  59403. + */
  59404. + if (wValue == 0) {
  59405. + /* @TODO - MAS - fix the access to the base field */
  59406. + regaddr = 0;
  59407. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  59408. + //GET_CORE_IF(pcd)->co
  59409. + regaddr |= wIndex;
  59410. + } else {
  59411. + regaddr = (wValue << 16) | wIndex;
  59412. + }
  59413. +
  59414. + /* Read a 32-bit value of the memory at the regaddr */
  59415. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  59416. +
  59417. + ep = &pcd->ep0;
  59418. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  59419. + ep->dwc_ep.is_in = 1;
  59420. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  59421. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  59422. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  59423. + ep->dwc_ep.xfer_len = wLen;
  59424. + ep->dwc_ep.xfer_count = 0;
  59425. + ep->dwc_ep.sent_zlp = 0;
  59426. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  59427. +
  59428. + pcd->ep0_pending = 1;
  59429. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  59430. + cfi->need_gadget_att = 0;
  59431. + retval = 0;
  59432. + break;
  59433. +
  59434. + case VEN_CORE_WRITE_REGISTER:
  59435. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  59436. + /* Set up an XFER to get the data stage of the control request,
  59437. + * which is the new value of the register to be modified.
  59438. + */
  59439. + ep = &pcd->ep0;
  59440. + ep->dwc_ep.is_in = 0;
  59441. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  59442. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  59443. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  59444. + ep->dwc_ep.xfer_len = wLen;
  59445. + ep->dwc_ep.xfer_count = 0;
  59446. + ep->dwc_ep.sent_zlp = 0;
  59447. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  59448. +
  59449. + pcd->ep0_pending = 1;
  59450. + /* Read the control write's data stage */
  59451. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  59452. + retval = 0;
  59453. + break;
  59454. +
  59455. + default:
  59456. + retval = -DWC_E_NOT_SUPPORTED;
  59457. + break;
  59458. + }
  59459. +
  59460. + return retval;
  59461. +}
  59462. +
  59463. +/**
  59464. + * This function prepares the core features descriptors and copies its
  59465. + * raw representation into the buffer <buf>.
  59466. + *
  59467. + * The buffer structure is as follows:
  59468. + * all_features_header (8 bytes)
  59469. + * features_#1 (8 bytes + feature name string length)
  59470. + * features_#2 (8 bytes + feature name string length)
  59471. + * .....
  59472. + * features_#n - where n=the total count of feature descriptors
  59473. + */
  59474. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  59475. +{
  59476. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  59477. + cfi_feature_desc_header_t *prop;
  59478. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  59479. + cfi_all_features_header_t *tmp;
  59480. + uint8_t *tmpbuf = buf;
  59481. + const uint8_t *pname = NULL;
  59482. + int i, j, namelen = 0, totlen;
  59483. +
  59484. + /* Prepare and copy the core features into the buffer */
  59485. + CFI_INFO("%s:\n", __func__);
  59486. +
  59487. + tmp = (cfi_all_features_header_t *) tmpbuf;
  59488. + *tmp = *all_props_hdr;
  59489. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  59490. +
  59491. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  59492. + for (i = 0; i < j; i++, prop_hdr++) {
  59493. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  59494. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  59495. + *prop = *prop_hdr;
  59496. +
  59497. + prop->bNameLen = namelen;
  59498. + prop->wLength =
  59499. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  59500. + namelen);
  59501. +
  59502. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  59503. + dwc_memcpy(tmpbuf, pname, namelen);
  59504. + tmpbuf += namelen;
  59505. + }
  59506. +
  59507. + totlen = tmpbuf - buf;
  59508. +
  59509. + if (totlen > 0) {
  59510. + tmp = (cfi_all_features_header_t *) buf;
  59511. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  59512. + }
  59513. +
  59514. + return totlen;
  59515. +}
  59516. +
  59517. +/**
  59518. + * This function releases all the dynamic memory in the CFI object.
  59519. + */
  59520. +static void cfi_release(cfiobject_t * cfiobj)
  59521. +{
  59522. + cfi_ep_t *cfiep;
  59523. + dwc_list_link_t *tmp;
  59524. +
  59525. + CFI_INFO("%s\n", __func__);
  59526. +
  59527. + if (cfiobj->buf_in.buf) {
  59528. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  59529. + cfiobj->buf_in.addr);
  59530. + cfiobj->buf_in.buf = NULL;
  59531. + }
  59532. +
  59533. + if (cfiobj->buf_out.buf) {
  59534. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  59535. + cfiobj->buf_out.addr);
  59536. + cfiobj->buf_out.buf = NULL;
  59537. + }
  59538. +
  59539. + /* Free the Buffer Setup values for each EP */
  59540. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  59541. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  59542. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  59543. + cfi_free_ep_bs_dyn_data(cfiep);
  59544. + }
  59545. +}
  59546. +
  59547. +/**
  59548. + * This function frees the dynamically allocated EP buffer setup data.
  59549. + */
  59550. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  59551. +{
  59552. + if (cfiep->bm_sg) {
  59553. + DWC_FREE(cfiep->bm_sg);
  59554. + cfiep->bm_sg = NULL;
  59555. + }
  59556. +
  59557. + if (cfiep->bm_align) {
  59558. + DWC_FREE(cfiep->bm_align);
  59559. + cfiep->bm_align = NULL;
  59560. + }
  59561. +
  59562. + if (cfiep->bm_concat) {
  59563. + if (NULL != cfiep->bm_concat->wTxBytes) {
  59564. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  59565. + cfiep->bm_concat->wTxBytes = NULL;
  59566. + }
  59567. + DWC_FREE(cfiep->bm_concat);
  59568. + cfiep->bm_concat = NULL;
  59569. + }
  59570. +}
  59571. +
  59572. +/**
  59573. + * This function initializes the default values of the features
  59574. + * for a specific endpoint and should be called only once when
  59575. + * the EP is enabled first time.
  59576. + */
  59577. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  59578. +{
  59579. + int retval = 0;
  59580. +
  59581. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  59582. + if (NULL == cfiep->bm_sg) {
  59583. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  59584. + return -DWC_E_NO_MEMORY;
  59585. + }
  59586. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  59587. +
  59588. + /* For the Concatenation feature's default value we do not allocate
  59589. + * memory for the wTxBytes field - it will be done in the set_feature_value
  59590. + * request handler.
  59591. + */
  59592. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  59593. + if (NULL == cfiep->bm_concat) {
  59594. + CFI_INFO
  59595. + ("Failed to allocate memory for CONCATENATION feature value\n");
  59596. + DWC_FREE(cfiep->bm_sg);
  59597. + return -DWC_E_NO_MEMORY;
  59598. + }
  59599. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  59600. +
  59601. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  59602. + if (NULL == cfiep->bm_align) {
  59603. + CFI_INFO
  59604. + ("Failed to allocate memory for Alignment feature value\n");
  59605. + DWC_FREE(cfiep->bm_sg);
  59606. + DWC_FREE(cfiep->bm_concat);
  59607. + return -DWC_E_NO_MEMORY;
  59608. + }
  59609. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  59610. +
  59611. + return retval;
  59612. +}
  59613. +
  59614. +/**
  59615. + * The callback function that notifies the CFI on the activation of
  59616. + * an endpoint in the PCD. The following steps are done in this function:
  59617. + *
  59618. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  59619. + * active endpoint)
  59620. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  59621. + * Set the Buffer Mode to standard
  59622. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  59623. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  59624. + */
  59625. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  59626. + struct dwc_otg_pcd_ep *ep)
  59627. +{
  59628. + cfi_ep_t *cfiep;
  59629. + int retval = -DWC_E_NOT_SUPPORTED;
  59630. +
  59631. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  59632. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  59633. + /* MAS - Check whether this endpoint already is in the list */
  59634. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  59635. +
  59636. + if (NULL == cfiep) {
  59637. + /* Allocate a cfi_ep_t object */
  59638. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  59639. + if (NULL == cfiep) {
  59640. + CFI_INFO
  59641. + ("Unable to allocate memory for <cfiep> in function %s\n",
  59642. + __func__);
  59643. + return -DWC_E_NO_MEMORY;
  59644. + }
  59645. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  59646. +
  59647. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  59648. + cfiep->ep = ep;
  59649. +
  59650. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  59651. + ep->dwc_ep.descs =
  59652. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  59653. + sizeof(dwc_otg_dma_desc_t),
  59654. + &ep->dwc_ep.descs_dma_addr);
  59655. +
  59656. + if (NULL == ep->dwc_ep.descs) {
  59657. + DWC_FREE(cfiep);
  59658. + return -DWC_E_NO_MEMORY;
  59659. + }
  59660. +
  59661. + DWC_LIST_INIT(&cfiep->lh);
  59662. +
  59663. + /* Set the buffer mode to BM_STANDARD. It will be modified
  59664. + * when building descriptors for a specific buffer mode */
  59665. + ep->dwc_ep.buff_mode = BM_STANDARD;
  59666. +
  59667. + /* Create and initialize the default values for this EP's Buffer modes */
  59668. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  59669. + return retval;
  59670. +
  59671. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  59672. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  59673. + retval = 0;
  59674. + } else { /* The sought EP already is in the list */
  59675. + CFI_INFO("%s: The sought EP already is in the list\n",
  59676. + __func__);
  59677. + }
  59678. +
  59679. + return retval;
  59680. +}
  59681. +
  59682. +/**
  59683. + * This function is called when the data stage of a 3-stage Control Write request
  59684. + * is complete.
  59685. + *
  59686. + */
  59687. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  59688. + struct dwc_otg_pcd *pcd)
  59689. +{
  59690. + uint32_t addr, reg_value;
  59691. + uint16_t wIndex, wValue;
  59692. + uint8_t bRequest;
  59693. + uint8_t *buf = cfi->buf_out.buf;
  59694. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  59695. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  59696. + int retval = -DWC_E_NOT_SUPPORTED;
  59697. +
  59698. + CFI_INFO("%s\n", __func__);
  59699. +
  59700. + bRequest = ctrl_req->bRequest;
  59701. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  59702. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  59703. +
  59704. + /*
  59705. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  59706. + * The request should be already saved in the command stage by now.
  59707. + */
  59708. + ctrl_req->data = cfi->buf_out.buf;
  59709. + cfi->need_status_in_complete = 0;
  59710. + cfi->need_gadget_att = 0;
  59711. +
  59712. + switch (bRequest) {
  59713. + case VEN_CORE_WRITE_REGISTER:
  59714. + /* The buffer contains raw data of the new value for the register */
  59715. + reg_value = *((uint32_t *) buf);
  59716. + if (wValue == 0) {
  59717. + addr = 0;
  59718. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  59719. + addr += wIndex;
  59720. + } else {
  59721. + addr = (wValue << 16) | wIndex;
  59722. + }
  59723. +
  59724. + //writel(reg_value, addr);
  59725. +
  59726. + retval = 0;
  59727. + cfi->need_status_in_complete = 1;
  59728. + break;
  59729. +
  59730. + case VEN_CORE_SET_FEATURE:
  59731. + /* The buffer contains raw data of the new value of the feature */
  59732. + retval = cfi_set_feature_value(pcd);
  59733. + if (retval < 0)
  59734. + return retval;
  59735. +
  59736. + cfi->need_status_in_complete = 1;
  59737. + break;
  59738. +
  59739. + default:
  59740. + break;
  59741. + }
  59742. +
  59743. + return retval;
  59744. +}
  59745. +
  59746. +/**
  59747. + * This function builds the DMA descriptors for the SG buffer mode.
  59748. + */
  59749. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  59750. + dwc_otg_pcd_request_t * req)
  59751. +{
  59752. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  59753. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  59754. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  59755. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  59756. + dma_addr_t buff_addr = req->dma;
  59757. + int i;
  59758. + uint32_t txsize, off;
  59759. +
  59760. + txsize = sgval->wSize;
  59761. + off = sgval->bOffset;
  59762. +
  59763. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  59764. +// __func__, cfiep->ep->ep.name, txsize, off);
  59765. +
  59766. + for (i = 0; i < sgval->bCount; i++) {
  59767. + desc->status.b.bs = BS_HOST_BUSY;
  59768. + desc->buf = buff_addr;
  59769. + desc->status.b.l = 0;
  59770. + desc->status.b.ioc = 0;
  59771. + desc->status.b.sp = 0;
  59772. + desc->status.b.bytes = txsize;
  59773. + desc->status.b.bs = BS_HOST_READY;
  59774. +
  59775. + /* Set the next address of the buffer */
  59776. + buff_addr += txsize + off;
  59777. + desc_last = desc;
  59778. + desc++;
  59779. + }
  59780. +
  59781. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  59782. + desc_last->status.b.l = 1;
  59783. + desc_last->status.b.ioc = 1;
  59784. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  59785. + /* Save the last DMA descriptor pointer */
  59786. + cfiep->dma_desc_last = desc_last;
  59787. + cfiep->desc_count = sgval->bCount;
  59788. +}
  59789. +
  59790. +/**
  59791. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  59792. + */
  59793. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  59794. + dwc_otg_pcd_request_t * req)
  59795. +{
  59796. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  59797. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  59798. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  59799. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  59800. + dma_addr_t buff_addr = req->dma;
  59801. + int i;
  59802. + uint16_t *txsize;
  59803. +
  59804. + txsize = concatval->wTxBytes;
  59805. +
  59806. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  59807. + desc->buf = buff_addr;
  59808. + desc->status.b.bs = BS_HOST_BUSY;
  59809. + desc->status.b.l = 0;
  59810. + desc->status.b.ioc = 0;
  59811. + desc->status.b.sp = 0;
  59812. + desc->status.b.bytes = *txsize;
  59813. + desc->status.b.bs = BS_HOST_READY;
  59814. +
  59815. + txsize++;
  59816. + /* Set the next address of the buffer */
  59817. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  59818. + desc_last = desc;
  59819. + desc++;
  59820. + }
  59821. +
  59822. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  59823. + desc_last->status.b.l = 1;
  59824. + desc_last->status.b.ioc = 1;
  59825. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  59826. + cfiep->dma_desc_last = desc_last;
  59827. + cfiep->desc_count = concatval->hdr.bDescCount;
  59828. +}
  59829. +
  59830. +/**
  59831. + * This function builds the DMA descriptors for the Circular buffer mode
  59832. + */
  59833. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  59834. + dwc_otg_pcd_request_t * req)
  59835. +{
  59836. + /* @todo: MAS - add implementation when this feature needs to be tested */
  59837. +}
  59838. +
  59839. +/**
  59840. + * This function builds the DMA descriptors for the Alignment buffer mode
  59841. + */
  59842. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  59843. + dwc_otg_pcd_request_t * req)
  59844. +{
  59845. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  59846. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  59847. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  59848. + dma_addr_t buff_addr = req->dma;
  59849. +
  59850. + desc->status.b.bs = BS_HOST_BUSY;
  59851. + desc->status.b.l = 1;
  59852. + desc->status.b.ioc = 1;
  59853. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  59854. + desc->status.b.bytes = req->length;
  59855. + /* Adjust the buffer alignment */
  59856. + desc->buf = (buff_addr + alignval->bAlign);
  59857. + desc->status.b.bs = BS_HOST_READY;
  59858. + cfiep->dma_desc_last = desc;
  59859. + cfiep->desc_count = 1;
  59860. +}
  59861. +
  59862. +/**
  59863. + * This function builds the DMA descriptors chain for different modes of the
  59864. + * buffer setup of an endpoint.
  59865. + */
  59866. +static void cfi_build_descriptors(struct cfiobject *cfi,
  59867. + struct dwc_otg_pcd *pcd,
  59868. + struct dwc_otg_pcd_ep *ep,
  59869. + dwc_otg_pcd_request_t * req)
  59870. +{
  59871. + cfi_ep_t *cfiep;
  59872. +
  59873. + /* Get the cfiep by the dwc_otg_pcd_ep */
  59874. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  59875. + if (NULL == cfiep) {
  59876. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  59877. + __func__);
  59878. + return;
  59879. + }
  59880. +
  59881. + cfiep->xfer_len = req->length;
  59882. +
  59883. + /* Iterate through all the DMA descriptors */
  59884. + switch (cfiep->ep->dwc_ep.buff_mode) {
  59885. + case BM_SG:
  59886. + cfi_build_sg_descs(cfi, cfiep, req);
  59887. + break;
  59888. +
  59889. + case BM_CONCAT:
  59890. + cfi_build_concat_descs(cfi, cfiep, req);
  59891. + break;
  59892. +
  59893. + case BM_CIRCULAR:
  59894. + cfi_build_circ_descs(cfi, cfiep, req);
  59895. + break;
  59896. +
  59897. + case BM_ALIGN:
  59898. + cfi_build_align_descs(cfi, cfiep, req);
  59899. + break;
  59900. +
  59901. + default:
  59902. + break;
  59903. + }
  59904. +}
  59905. +
  59906. +/**
  59907. + * Allocate DMA buffer for different Buffer modes.
  59908. + */
  59909. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  59910. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  59911. + unsigned size, gfp_t flags)
  59912. +{
  59913. + return DWC_DMA_ALLOC(size, dma);
  59914. +}
  59915. +
  59916. +/**
  59917. + * This function initializes the CFI object.
  59918. + */
  59919. +int init_cfi(cfiobject_t * cfiobj)
  59920. +{
  59921. + CFI_INFO("%s\n", __func__);
  59922. +
  59923. + /* Allocate a buffer for IN XFERs */
  59924. + cfiobj->buf_in.buf =
  59925. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  59926. + if (NULL == cfiobj->buf_in.buf) {
  59927. + CFI_INFO("Unable to allocate buffer for INs\n");
  59928. + return -DWC_E_NO_MEMORY;
  59929. + }
  59930. +
  59931. + /* Allocate a buffer for OUT XFERs */
  59932. + cfiobj->buf_out.buf =
  59933. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  59934. + if (NULL == cfiobj->buf_out.buf) {
  59935. + CFI_INFO("Unable to allocate buffer for OUT\n");
  59936. + return -DWC_E_NO_MEMORY;
  59937. + }
  59938. +
  59939. + /* Initialize the callback function pointers */
  59940. + cfiobj->ops.release = cfi_release;
  59941. + cfiobj->ops.ep_enable = cfi_ep_enable;
  59942. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  59943. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  59944. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  59945. +
  59946. + /* Initialize the list of active endpoints in the CFI object */
  59947. + DWC_LIST_INIT(&cfiobj->active_eps);
  59948. +
  59949. + return 0;
  59950. +}
  59951. +
  59952. +/**
  59953. + * This function reads the required feature's current value into the buffer
  59954. + *
  59955. + * @retval: Returns negative as error, or the data length of the feature
  59956. + */
  59957. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  59958. + struct dwc_otg_pcd *pcd,
  59959. + struct cfi_usb_ctrlrequest *ctrl_req)
  59960. +{
  59961. + int retval = -DWC_E_NOT_SUPPORTED;
  59962. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  59963. + uint16_t dfifo, rxfifo, txfifo;
  59964. +
  59965. + switch (ctrl_req->wIndex) {
  59966. + /* Whether the DDMA is enabled or not */
  59967. + case FT_ID_DMA_MODE:
  59968. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  59969. + retval = 1;
  59970. + break;
  59971. +
  59972. + case FT_ID_DMA_BUFFER_SETUP:
  59973. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  59974. + break;
  59975. +
  59976. + case FT_ID_DMA_BUFF_ALIGN:
  59977. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  59978. + break;
  59979. +
  59980. + case FT_ID_DMA_CONCAT_SETUP:
  59981. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  59982. + break;
  59983. +
  59984. + case FT_ID_DMA_CIRCULAR:
  59985. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  59986. + break;
  59987. +
  59988. + case FT_ID_THRESHOLD_SETUP:
  59989. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  59990. + break;
  59991. +
  59992. + case FT_ID_DFIFO_DEPTH:
  59993. + dfifo = get_dfifo_size(coreif);
  59994. + *((uint16_t *) buf) = dfifo;
  59995. + retval = sizeof(uint16_t);
  59996. + break;
  59997. +
  59998. + case FT_ID_TX_FIFO_DEPTH:
  59999. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  60000. + if (retval >= 0) {
  60001. + txfifo = retval;
  60002. + *((uint16_t *) buf) = txfifo;
  60003. + retval = sizeof(uint16_t);
  60004. + }
  60005. + break;
  60006. +
  60007. + case FT_ID_RX_FIFO_DEPTH:
  60008. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  60009. + if (retval >= 0) {
  60010. + rxfifo = retval;
  60011. + *((uint16_t *) buf) = rxfifo;
  60012. + retval = sizeof(uint16_t);
  60013. + }
  60014. + break;
  60015. + }
  60016. +
  60017. + return retval;
  60018. +}
  60019. +
  60020. +/**
  60021. + * This function resets the SG for the specified EP to its default value
  60022. + */
  60023. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  60024. +{
  60025. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  60026. + return 0;
  60027. +}
  60028. +
  60029. +/**
  60030. + * This function resets the Alignment for the specified EP to its default value
  60031. + */
  60032. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  60033. +{
  60034. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  60035. + return 0;
  60036. +}
  60037. +
  60038. +/**
  60039. + * This function resets the Concatenation for the specified EP to its default value
  60040. + * This function will also set the value of the wTxBytes field to NULL after
  60041. + * freeing the memory previously allocated for this field.
  60042. + */
  60043. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  60044. +{
  60045. + /* First we need to free the wTxBytes field */
  60046. + if (cfiep->bm_concat->wTxBytes) {
  60047. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  60048. + cfiep->bm_concat->wTxBytes = NULL;
  60049. + }
  60050. +
  60051. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  60052. + return 0;
  60053. +}
  60054. +
  60055. +/**
  60056. + * This function resets all the buffer setups of the specified endpoint
  60057. + */
  60058. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  60059. +{
  60060. + cfi_reset_sg_val(cfiep);
  60061. + cfi_reset_align_val(cfiep);
  60062. + cfi_reset_concat_val(cfiep);
  60063. + return 0;
  60064. +}
  60065. +
  60066. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  60067. + uint8_t rx_rst, uint8_t tx_rst)
  60068. +{
  60069. + int retval = -DWC_E_INVALID;
  60070. + uint16_t tx_siz[15];
  60071. + uint16_t rx_siz = 0;
  60072. + dwc_otg_pcd_ep_t *ep = NULL;
  60073. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  60074. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  60075. +
  60076. + if (rx_rst) {
  60077. + rx_siz = params->dev_rx_fifo_size;
  60078. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  60079. + }
  60080. +
  60081. + if (tx_rst) {
  60082. + if (ep_addr == 0) {
  60083. + int i;
  60084. +
  60085. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60086. + tx_siz[i] =
  60087. + core_if->core_params->dev_tx_fifo_size[i];
  60088. + core_if->core_params->dev_tx_fifo_size[i] =
  60089. + core_if->init_txfsiz[i];
  60090. + }
  60091. + } else {
  60092. +
  60093. + ep = get_ep_by_addr(pcd, ep_addr);
  60094. +
  60095. + if (NULL == ep) {
  60096. + CFI_INFO
  60097. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  60098. + __func__, ep_addr);
  60099. + return -DWC_E_INVALID;
  60100. + }
  60101. +
  60102. + tx_siz[0] =
  60103. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  60104. + 1];
  60105. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  60106. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  60107. + dwc_ep.tx_fifo_num -
  60108. + 1];
  60109. + }
  60110. + }
  60111. +
  60112. + if (resize_fifos(GET_CORE_IF(pcd))) {
  60113. + retval = 0;
  60114. + } else {
  60115. + CFI_INFO
  60116. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  60117. + __func__);
  60118. + if (rx_rst) {
  60119. + params->dev_rx_fifo_size = rx_siz;
  60120. + }
  60121. +
  60122. + if (tx_rst) {
  60123. + if (ep_addr == 0) {
  60124. + int i;
  60125. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  60126. + i++) {
  60127. + core_if->
  60128. + core_params->dev_tx_fifo_size[i] =
  60129. + tx_siz[i];
  60130. + }
  60131. + } else {
  60132. + params->dev_tx_fifo_size[ep->
  60133. + dwc_ep.tx_fifo_num -
  60134. + 1] = tx_siz[0];
  60135. + }
  60136. + }
  60137. + retval = -DWC_E_INVALID;
  60138. + }
  60139. + return retval;
  60140. +}
  60141. +
  60142. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  60143. +{
  60144. + int retval = 0;
  60145. + cfi_ep_t *cfiep;
  60146. + cfiobject_t *cfi = pcd->cfi;
  60147. + dwc_list_link_t *tmp;
  60148. +
  60149. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  60150. + if (retval < 0) {
  60151. + return retval;
  60152. + }
  60153. +
  60154. + /* If the EP address is known then reset the features for only that EP */
  60155. + if (addr) {
  60156. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60157. + if (NULL == cfiep) {
  60158. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  60159. + __func__, addr);
  60160. + return -DWC_E_INVALID;
  60161. + }
  60162. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  60163. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  60164. + }
  60165. + /* Otherwise (wValue == 0), reset all features of all EP's */
  60166. + else {
  60167. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  60168. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  60169. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  60170. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  60171. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  60172. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  60173. + if (retval < 0) {
  60174. + CFI_INFO
  60175. + ("%s: Error resetting the feature Reset All\n",
  60176. + __func__);
  60177. + return retval;
  60178. + }
  60179. + }
  60180. + }
  60181. + return retval;
  60182. +}
  60183. +
  60184. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  60185. + uint8_t addr)
  60186. +{
  60187. + int retval = 0;
  60188. + cfi_ep_t *cfiep;
  60189. + cfiobject_t *cfi = pcd->cfi;
  60190. + dwc_list_link_t *tmp;
  60191. +
  60192. + /* If the EP address is known then reset the features for only that EP */
  60193. + if (addr) {
  60194. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60195. + if (NULL == cfiep) {
  60196. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  60197. + __func__, addr);
  60198. + return -DWC_E_INVALID;
  60199. + }
  60200. + retval = cfi_reset_sg_val(cfiep);
  60201. + }
  60202. + /* Otherwise (wValue == 0), reset all features of all EP's */
  60203. + else {
  60204. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  60205. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  60206. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  60207. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  60208. + retval = cfi_reset_sg_val(cfiep);
  60209. + if (retval < 0) {
  60210. + CFI_INFO
  60211. + ("%s: Error resetting the feature Buffer Setup\n",
  60212. + __func__);
  60213. + return retval;
  60214. + }
  60215. + }
  60216. + }
  60217. + return retval;
  60218. +}
  60219. +
  60220. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  60221. +{
  60222. + int retval = 0;
  60223. + cfi_ep_t *cfiep;
  60224. + cfiobject_t *cfi = pcd->cfi;
  60225. + dwc_list_link_t *tmp;
  60226. +
  60227. + /* If the EP address is known then reset the features for only that EP */
  60228. + if (addr) {
  60229. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60230. + if (NULL == cfiep) {
  60231. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  60232. + __func__, addr);
  60233. + return -DWC_E_INVALID;
  60234. + }
  60235. + retval = cfi_reset_concat_val(cfiep);
  60236. + }
  60237. + /* Otherwise (wValue == 0), reset all features of all EP's */
  60238. + else {
  60239. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  60240. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  60241. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  60242. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  60243. + retval = cfi_reset_concat_val(cfiep);
  60244. + if (retval < 0) {
  60245. + CFI_INFO
  60246. + ("%s: Error resetting the feature Concatenation Value\n",
  60247. + __func__);
  60248. + return retval;
  60249. + }
  60250. + }
  60251. + }
  60252. + return retval;
  60253. +}
  60254. +
  60255. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  60256. +{
  60257. + int retval = 0;
  60258. + cfi_ep_t *cfiep;
  60259. + cfiobject_t *cfi = pcd->cfi;
  60260. + dwc_list_link_t *tmp;
  60261. +
  60262. + /* If the EP address is known then reset the features for only that EP */
  60263. + if (addr) {
  60264. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60265. + if (NULL == cfiep) {
  60266. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  60267. + __func__, addr);
  60268. + return -DWC_E_INVALID;
  60269. + }
  60270. + retval = cfi_reset_align_val(cfiep);
  60271. + }
  60272. + /* Otherwise (wValue == 0), reset all features of all EP's */
  60273. + else {
  60274. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  60275. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  60276. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  60277. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  60278. + retval = cfi_reset_align_val(cfiep);
  60279. + if (retval < 0) {
  60280. + CFI_INFO
  60281. + ("%s: Error resetting the feature Aliignment Value\n",
  60282. + __func__);
  60283. + return retval;
  60284. + }
  60285. + }
  60286. + }
  60287. + return retval;
  60288. +
  60289. +}
  60290. +
  60291. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  60292. + struct cfi_usb_ctrlrequest *req)
  60293. +{
  60294. + int retval = 0;
  60295. +
  60296. + switch (req->wIndex) {
  60297. + case 0:
  60298. + /* Reset all features */
  60299. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  60300. + break;
  60301. +
  60302. + case FT_ID_DMA_BUFFER_SETUP:
  60303. + /* Reset the SG buffer setup */
  60304. + retval =
  60305. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  60306. + break;
  60307. +
  60308. + case FT_ID_DMA_CONCAT_SETUP:
  60309. + /* Reset the Concatenation buffer setup */
  60310. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  60311. + break;
  60312. +
  60313. + case FT_ID_DMA_BUFF_ALIGN:
  60314. + /* Reset the Alignment buffer setup */
  60315. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  60316. + break;
  60317. +
  60318. + case FT_ID_TX_FIFO_DEPTH:
  60319. + retval =
  60320. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  60321. + pcd->cfi->need_gadget_att = 0;
  60322. + break;
  60323. +
  60324. + case FT_ID_RX_FIFO_DEPTH:
  60325. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  60326. + pcd->cfi->need_gadget_att = 0;
  60327. + break;
  60328. + default:
  60329. + break;
  60330. + }
  60331. + return retval;
  60332. +}
  60333. +
  60334. +/**
  60335. + * This function sets a new value for the SG buffer setup.
  60336. + */
  60337. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  60338. +{
  60339. + uint8_t inaddr, outaddr;
  60340. + cfi_ep_t *epin, *epout;
  60341. + ddma_sg_buffer_setup_t *psgval;
  60342. + uint32_t desccount, size;
  60343. +
  60344. + CFI_INFO("%s\n", __func__);
  60345. +
  60346. + psgval = (ddma_sg_buffer_setup_t *) buf;
  60347. + desccount = (uint32_t) psgval->bCount;
  60348. + size = (uint32_t) psgval->wSize;
  60349. +
  60350. + /* Check the DMA descriptor count */
  60351. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  60352. + CFI_INFO
  60353. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  60354. + __func__, MAX_DMA_DESCS_PER_EP);
  60355. + return -DWC_E_INVALID;
  60356. + }
  60357. +
  60358. + /* Check the DMA descriptor count */
  60359. +
  60360. + if (size == 0) {
  60361. +
  60362. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  60363. + __func__);
  60364. +
  60365. + return -DWC_E_INVALID;
  60366. +
  60367. + }
  60368. +
  60369. + inaddr = psgval->bInEndpointAddress;
  60370. + outaddr = psgval->bOutEndpointAddress;
  60371. +
  60372. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  60373. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  60374. +
  60375. + if (NULL == epin || NULL == epout) {
  60376. + CFI_INFO
  60377. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  60378. + __func__, inaddr, outaddr);
  60379. + return -DWC_E_INVALID;
  60380. + }
  60381. +
  60382. + epin->ep->dwc_ep.buff_mode = BM_SG;
  60383. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  60384. +
  60385. + epout->ep->dwc_ep.buff_mode = BM_SG;
  60386. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  60387. +
  60388. + return 0;
  60389. +}
  60390. +
  60391. +/**
  60392. + * This function sets a new value for the buffer Alignment setup.
  60393. + */
  60394. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  60395. +{
  60396. + cfi_ep_t *ep;
  60397. + uint8_t addr;
  60398. + ddma_align_buffer_setup_t *palignval;
  60399. +
  60400. + palignval = (ddma_align_buffer_setup_t *) buf;
  60401. + addr = palignval->bEndpointAddress;
  60402. +
  60403. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60404. +
  60405. + if (NULL == ep) {
  60406. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  60407. + __func__, addr);
  60408. + return -DWC_E_INVALID;
  60409. + }
  60410. +
  60411. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  60412. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  60413. +
  60414. + return 0;
  60415. +}
  60416. +
  60417. +/**
  60418. + * This function sets a new value for the Concatenation buffer setup.
  60419. + */
  60420. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  60421. +{
  60422. + uint8_t addr;
  60423. + cfi_ep_t *ep;
  60424. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  60425. + uint16_t *pVals;
  60426. + uint32_t desccount;
  60427. + int i;
  60428. + uint16_t mps;
  60429. +
  60430. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  60431. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  60432. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  60433. +
  60434. + /* Check the DMA descriptor count */
  60435. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  60436. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  60437. + __func__, MAX_DMA_DESCS_PER_EP);
  60438. + return -DWC_E_INVALID;
  60439. + }
  60440. +
  60441. + addr = pConcatValHdr->bEndpointAddress;
  60442. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60443. + if (NULL == ep) {
  60444. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  60445. + __func__, addr);
  60446. + return -DWC_E_INVALID;
  60447. + }
  60448. +
  60449. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  60450. +
  60451. +#if 0
  60452. + for (i = 0; i < desccount; i++) {
  60453. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  60454. + }
  60455. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  60456. +#endif
  60457. +
  60458. + /* Check the wTxSizes to be less than or equal to the mps */
  60459. + for (i = 0; i < desccount; i++) {
  60460. + if (pVals[i] > mps) {
  60461. + CFI_INFO
  60462. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  60463. + __func__, i, pVals[i]);
  60464. + return -DWC_E_INVALID;
  60465. + }
  60466. + }
  60467. +
  60468. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  60469. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  60470. +
  60471. + /* Free the previously allocated storage for the wTxBytes */
  60472. + if (ep->bm_concat->wTxBytes) {
  60473. + DWC_FREE(ep->bm_concat->wTxBytes);
  60474. + }
  60475. +
  60476. + /* Allocate a new storage for the wTxBytes field */
  60477. + ep->bm_concat->wTxBytes =
  60478. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  60479. + if (NULL == ep->bm_concat->wTxBytes) {
  60480. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  60481. + return -DWC_E_NO_MEMORY;
  60482. + }
  60483. +
  60484. + /* Copy the new values into the wTxBytes filed */
  60485. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  60486. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  60487. +
  60488. + return 0;
  60489. +}
  60490. +
  60491. +/**
  60492. + * This function calculates the total of all FIFO sizes
  60493. + *
  60494. + * @param core_if Programming view of DWC_otg controller
  60495. + *
  60496. + * @return The total of data FIFO sizes.
  60497. + *
  60498. + */
  60499. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  60500. +{
  60501. + dwc_otg_core_params_t *params = core_if->core_params;
  60502. + uint16_t dfifo_total = 0;
  60503. + int i;
  60504. +
  60505. + /* The shared RxFIFO size */
  60506. + dfifo_total =
  60507. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  60508. +
  60509. + /* Add up each TxFIFO size to the total */
  60510. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60511. + dfifo_total += params->dev_tx_fifo_size[i];
  60512. + }
  60513. +
  60514. + return dfifo_total;
  60515. +}
  60516. +
  60517. +/**
  60518. + * This function returns Rx FIFO size
  60519. + *
  60520. + * @param core_if Programming view of DWC_otg controller
  60521. + *
  60522. + * @return The total of data FIFO sizes.
  60523. + *
  60524. + */
  60525. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  60526. +{
  60527. + switch (wValue >> 8) {
  60528. + case 0:
  60529. + return (core_if->pwron_rxfsiz <
  60530. + 32768) ? core_if->pwron_rxfsiz : 32768;
  60531. + break;
  60532. + case 1:
  60533. + return core_if->core_params->dev_rx_fifo_size;
  60534. + break;
  60535. + default:
  60536. + return -DWC_E_INVALID;
  60537. + break;
  60538. + }
  60539. +}
  60540. +
  60541. +/**
  60542. + * This function returns Tx FIFO size for IN EP
  60543. + *
  60544. + * @param core_if Programming view of DWC_otg controller
  60545. + *
  60546. + * @return The total of data FIFO sizes.
  60547. + *
  60548. + */
  60549. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  60550. +{
  60551. + dwc_otg_pcd_ep_t *ep;
  60552. +
  60553. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  60554. +
  60555. + if (NULL == ep) {
  60556. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  60557. + __func__, wValue & 0xff);
  60558. + return -DWC_E_INVALID;
  60559. + }
  60560. +
  60561. + if (!ep->dwc_ep.is_in) {
  60562. + CFI_INFO
  60563. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  60564. + __func__, wValue & 0xff);
  60565. + return -DWC_E_INVALID;
  60566. + }
  60567. +
  60568. + switch (wValue >> 8) {
  60569. + case 0:
  60570. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  60571. + [ep->dwc_ep.tx_fifo_num - 1] <
  60572. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  60573. + dwc_ep.tx_fifo_num
  60574. + - 1] : 32768;
  60575. + break;
  60576. + case 1:
  60577. + return GET_CORE_IF(pcd)->core_params->
  60578. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  60579. + break;
  60580. + default:
  60581. + return -DWC_E_INVALID;
  60582. + break;
  60583. + }
  60584. +}
  60585. +
  60586. +/**
  60587. + * This function checks if the submitted combination of
  60588. + * device mode FIFO sizes is possible or not.
  60589. + *
  60590. + * @param core_if Programming view of DWC_otg controller
  60591. + *
  60592. + * @return 1 if possible, 0 otherwise.
  60593. + *
  60594. + */
  60595. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  60596. +{
  60597. + uint16_t dfifo_actual = 0;
  60598. + dwc_otg_core_params_t *params = core_if->core_params;
  60599. + uint16_t start_addr = 0;
  60600. + int i;
  60601. +
  60602. + dfifo_actual =
  60603. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  60604. +
  60605. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60606. + dfifo_actual += params->dev_tx_fifo_size[i];
  60607. + }
  60608. +
  60609. + if (dfifo_actual > core_if->total_fifo_size) {
  60610. + return 0;
  60611. + }
  60612. +
  60613. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  60614. + return 0;
  60615. +
  60616. + if (params->dev_nperio_tx_fifo_size > 32768
  60617. + || params->dev_nperio_tx_fifo_size < 16)
  60618. + return 0;
  60619. +
  60620. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60621. +
  60622. + if (params->dev_tx_fifo_size[i] > 768
  60623. + || params->dev_tx_fifo_size[i] < 4)
  60624. + return 0;
  60625. + }
  60626. +
  60627. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  60628. + return 0;
  60629. + start_addr = params->dev_rx_fifo_size;
  60630. +
  60631. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  60632. + return 0;
  60633. + start_addr += params->dev_nperio_tx_fifo_size;
  60634. +
  60635. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60636. +
  60637. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  60638. + return 0;
  60639. + start_addr += params->dev_tx_fifo_size[i];
  60640. + }
  60641. +
  60642. + return 1;
  60643. +}
  60644. +
  60645. +/**
  60646. + * This function resizes Device mode FIFOs
  60647. + *
  60648. + * @param core_if Programming view of DWC_otg controller
  60649. + *
  60650. + * @return 1 if successful, 0 otherwise
  60651. + *
  60652. + */
  60653. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  60654. +{
  60655. + int i = 0;
  60656. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  60657. + dwc_otg_core_params_t *params = core_if->core_params;
  60658. + uint32_t rx_fifo_size;
  60659. + fifosize_data_t nptxfifosize;
  60660. + fifosize_data_t txfifosize[15];
  60661. +
  60662. + uint32_t rx_fsz_bak;
  60663. + uint32_t nptxfsz_bak;
  60664. + uint32_t txfsz_bak[15];
  60665. +
  60666. + uint16_t start_address;
  60667. + uint8_t retval = 1;
  60668. +
  60669. + if (!check_fifo_sizes(core_if)) {
  60670. + return 0;
  60671. + }
  60672. +
  60673. + /* Configure data FIFO sizes */
  60674. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  60675. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  60676. + rx_fifo_size = params->dev_rx_fifo_size;
  60677. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  60678. +
  60679. + /*
  60680. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  60681. + * Indexes of the FIFO size module parameters in the
  60682. + * dev_tx_fifo_size array and the FIFO size registers in
  60683. + * the dtxfsiz array run from 0 to 14.
  60684. + */
  60685. +
  60686. + /* Non-periodic Tx FIFO */
  60687. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  60688. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  60689. + start_address = params->dev_rx_fifo_size;
  60690. + nptxfifosize.b.startaddr = start_address;
  60691. +
  60692. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  60693. +
  60694. + start_address += nptxfifosize.b.depth;
  60695. +
  60696. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60697. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  60698. +
  60699. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  60700. + txfifosize[i].b.startaddr = start_address;
  60701. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  60702. + txfifosize[i].d32);
  60703. +
  60704. + start_address += txfifosize[i].b.depth;
  60705. + }
  60706. +
  60707. + /** Check if register values are set correctly */
  60708. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  60709. + retval = 0;
  60710. + }
  60711. +
  60712. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  60713. + retval = 0;
  60714. + }
  60715. +
  60716. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60717. + if (txfifosize[i].d32 !=
  60718. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  60719. + retval = 0;
  60720. + }
  60721. + }
  60722. +
  60723. + /** If register values are not set correctly, reset old values */
  60724. + if (retval == 0) {
  60725. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  60726. +
  60727. + /* Non-periodic Tx FIFO */
  60728. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  60729. +
  60730. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  60731. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  60732. + txfsz_bak[i]);
  60733. + }
  60734. + }
  60735. + } else {
  60736. + return 0;
  60737. + }
  60738. +
  60739. + /* Flush the FIFOs */
  60740. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  60741. + dwc_otg_flush_rx_fifo(core_if);
  60742. +
  60743. + return retval;
  60744. +}
  60745. +
  60746. +/**
  60747. + * This function sets a new value for the buffer Alignment setup.
  60748. + */
  60749. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  60750. +{
  60751. + int retval;
  60752. + uint32_t fsiz;
  60753. + uint16_t size;
  60754. + uint16_t ep_addr;
  60755. + dwc_otg_pcd_ep_t *ep;
  60756. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  60757. + tx_fifo_size_setup_t *ptxfifoval;
  60758. +
  60759. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  60760. + ep_addr = ptxfifoval->bEndpointAddress;
  60761. + size = ptxfifoval->wDepth;
  60762. +
  60763. + ep = get_ep_by_addr(pcd, ep_addr);
  60764. +
  60765. + CFI_INFO
  60766. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  60767. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  60768. +
  60769. + if (NULL == ep) {
  60770. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  60771. + __func__, ep_addr);
  60772. + return -DWC_E_INVALID;
  60773. + }
  60774. +
  60775. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  60776. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  60777. +
  60778. + if (resize_fifos(GET_CORE_IF(pcd))) {
  60779. + retval = 0;
  60780. + } else {
  60781. + CFI_INFO
  60782. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  60783. + __func__, ep_addr);
  60784. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  60785. + retval = -DWC_E_INVALID;
  60786. + }
  60787. +
  60788. + return retval;
  60789. +}
  60790. +
  60791. +/**
  60792. + * This function sets a new value for the buffer Alignment setup.
  60793. + */
  60794. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  60795. +{
  60796. + int retval;
  60797. + uint32_t fsiz;
  60798. + uint16_t size;
  60799. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  60800. + rx_fifo_size_setup_t *prxfifoval;
  60801. +
  60802. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  60803. + size = prxfifoval->wDepth;
  60804. +
  60805. + fsiz = params->dev_rx_fifo_size;
  60806. + params->dev_rx_fifo_size = size;
  60807. +
  60808. + if (resize_fifos(GET_CORE_IF(pcd))) {
  60809. + retval = 0;
  60810. + } else {
  60811. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  60812. + __func__);
  60813. + params->dev_rx_fifo_size = fsiz;
  60814. + retval = -DWC_E_INVALID;
  60815. + }
  60816. +
  60817. + return retval;
  60818. +}
  60819. +
  60820. +/**
  60821. + * This function reads the SG of an EP's buffer setup into the buffer buf
  60822. + */
  60823. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  60824. + struct cfi_usb_ctrlrequest *req)
  60825. +{
  60826. + int retval = -DWC_E_INVALID;
  60827. + uint8_t addr;
  60828. + cfi_ep_t *ep;
  60829. +
  60830. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  60831. + addr = req->wValue & 0xFF;
  60832. + if (addr == 0) /* The address should be non-zero */
  60833. + return retval;
  60834. +
  60835. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60836. + if (NULL == ep) {
  60837. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  60838. + __func__, addr);
  60839. + return retval;
  60840. + }
  60841. +
  60842. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  60843. + retval = BS_SG_VAL_DESC_LEN;
  60844. + return retval;
  60845. +}
  60846. +
  60847. +/**
  60848. + * This function reads the Concatenation value of an EP's buffer mode into
  60849. + * the buffer buf
  60850. + */
  60851. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  60852. + struct cfi_usb_ctrlrequest *req)
  60853. +{
  60854. + int retval = -DWC_E_INVALID;
  60855. + uint8_t addr;
  60856. + cfi_ep_t *ep;
  60857. + uint8_t desc_count;
  60858. +
  60859. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  60860. + addr = req->wValue & 0xFF;
  60861. + if (addr == 0) /* The address should be non-zero */
  60862. + return retval;
  60863. +
  60864. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60865. + if (NULL == ep) {
  60866. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  60867. + __func__, addr);
  60868. + return retval;
  60869. + }
  60870. +
  60871. + /* Copy the header to the buffer */
  60872. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  60873. + /* Advance the buffer pointer by the header size */
  60874. + buf += BS_CONCAT_VAL_HDR_LEN;
  60875. +
  60876. + desc_count = ep->bm_concat->hdr.bDescCount;
  60877. + /* Copy alll the wTxBytes to the buffer */
  60878. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  60879. +
  60880. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  60881. + return retval;
  60882. +}
  60883. +
  60884. +/**
  60885. + * This function reads the buffer Alignment value of an EP's buffer mode into
  60886. + * the buffer buf
  60887. + *
  60888. + * @return The total number of bytes copied to the buffer or negative error code.
  60889. + */
  60890. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  60891. + struct cfi_usb_ctrlrequest *req)
  60892. +{
  60893. + int retval = -DWC_E_INVALID;
  60894. + uint8_t addr;
  60895. + cfi_ep_t *ep;
  60896. +
  60897. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  60898. + addr = req->wValue & 0xFF;
  60899. + if (addr == 0) /* The address should be non-zero */
  60900. + return retval;
  60901. +
  60902. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  60903. + if (NULL == ep) {
  60904. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  60905. + __func__, addr);
  60906. + return retval;
  60907. + }
  60908. +
  60909. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  60910. + retval = BS_ALIGN_VAL_HDR_LEN;
  60911. +
  60912. + return retval;
  60913. +}
  60914. +
  60915. +/**
  60916. + * This function sets a new value for the specified feature
  60917. + *
  60918. + * @param pcd A pointer to the PCD object
  60919. + *
  60920. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  60921. + */
  60922. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  60923. +{
  60924. + int retval = -DWC_E_NOT_SUPPORTED;
  60925. + uint16_t wIndex, wValue;
  60926. + uint8_t bRequest;
  60927. + struct dwc_otg_core_if *coreif;
  60928. + cfiobject_t *cfi = pcd->cfi;
  60929. + struct cfi_usb_ctrlrequest *ctrl_req;
  60930. + uint8_t *buf;
  60931. + ctrl_req = &cfi->ctrl_req;
  60932. +
  60933. + buf = pcd->cfi->ctrl_req.data;
  60934. +
  60935. + coreif = GET_CORE_IF(pcd);
  60936. + bRequest = ctrl_req->bRequest;
  60937. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  60938. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  60939. +
  60940. + /* See which feature is to be modified */
  60941. + switch (wIndex) {
  60942. + case FT_ID_DMA_BUFFER_SETUP:
  60943. + /* Modify the feature */
  60944. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  60945. + return retval;
  60946. +
  60947. + /* And send this request to the gadget */
  60948. + cfi->need_gadget_att = 1;
  60949. + break;
  60950. +
  60951. + case FT_ID_DMA_BUFF_ALIGN:
  60952. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  60953. + return retval;
  60954. + cfi->need_gadget_att = 1;
  60955. + break;
  60956. +
  60957. + case FT_ID_DMA_CONCAT_SETUP:
  60958. + /* Modify the feature */
  60959. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  60960. + return retval;
  60961. + cfi->need_gadget_att = 1;
  60962. + break;
  60963. +
  60964. + case FT_ID_DMA_CIRCULAR:
  60965. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  60966. + break;
  60967. +
  60968. + case FT_ID_THRESHOLD_SETUP:
  60969. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  60970. + break;
  60971. +
  60972. + case FT_ID_DFIFO_DEPTH:
  60973. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  60974. + break;
  60975. +
  60976. + case FT_ID_TX_FIFO_DEPTH:
  60977. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  60978. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  60979. + return retval;
  60980. + cfi->need_gadget_att = 0;
  60981. + break;
  60982. +
  60983. + case FT_ID_RX_FIFO_DEPTH:
  60984. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  60985. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  60986. + return retval;
  60987. + cfi->need_gadget_att = 0;
  60988. + break;
  60989. + }
  60990. +
  60991. + return retval;
  60992. +}
  60993. +
  60994. +#endif //DWC_UTE_CFI
  60995. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cfi.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  60996. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 1969-12-31 18:00:00.000000000 -0600
  60997. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 2014-12-11 14:02:55.388418001 -0600
  60998. @@ -0,0 +1,320 @@
  60999. +/* ==========================================================================
  61000. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  61001. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  61002. + * otherwise expressly agreed to in writing between Synopsys and you.
  61003. + *
  61004. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  61005. + * any End User Software License Agreement or Agreement for Licensed Product
  61006. + * with Synopsys or any supplement thereto. You are permitted to use and
  61007. + * redistribute this Software in source and binary forms, with or without
  61008. + * modification, provided that redistributions of source code must retain this
  61009. + * notice. You may not view, use, disclose, copy or distribute this file or
  61010. + * any information contained herein except pursuant to this license grant from
  61011. + * Synopsys. If you do not agree with this notice, including the disclaimer
  61012. + * below, then you are not authorized to use the Software.
  61013. + *
  61014. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  61015. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61016. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61017. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  61018. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  61019. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61020. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  61021. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  61022. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  61023. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  61024. + * DAMAGE.
  61025. + * ========================================================================== */
  61026. +
  61027. +#if !defined(__DWC_OTG_CFI_H__)
  61028. +#define __DWC_OTG_CFI_H__
  61029. +
  61030. +#include "dwc_otg_pcd.h"
  61031. +#include "dwc_cfi_common.h"
  61032. +
  61033. +/**
  61034. + * @file
  61035. + * This file contains the CFI related OTG PCD specific common constants,
  61036. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  61037. + * optional interface for internal testing purposes that a DUT may implement to
  61038. + * support testing of configurable features.
  61039. + *
  61040. + */
  61041. +
  61042. +struct dwc_otg_pcd;
  61043. +struct dwc_otg_pcd_ep;
  61044. +
  61045. +/** OTG CFI Features (properties) ID constants */
  61046. +/** This is a request for all Core Features */
  61047. +#define FT_ID_DMA_MODE 0x0001
  61048. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  61049. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  61050. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  61051. +#define FT_ID_DMA_CIRCULAR 0x0005
  61052. +#define FT_ID_THRESHOLD_SETUP 0x0006
  61053. +#define FT_ID_DFIFO_DEPTH 0x0007
  61054. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  61055. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  61056. +
  61057. +/**********************************************************/
  61058. +#define CFI_INFO_DEF
  61059. +
  61060. +#ifdef CFI_INFO_DEF
  61061. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  61062. +#else
  61063. +#define CFI_INFO(fmt...)
  61064. +#endif
  61065. +
  61066. +#define min(x,y) ({ \
  61067. + x < y ? x : y; })
  61068. +
  61069. +#define max(x,y) ({ \
  61070. + x > y ? x : y; })
  61071. +
  61072. +/**
  61073. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  61074. + * also used for setting up a buffer for Circular DDMA.
  61075. + */
  61076. +struct _ddma_sg_buffer_setup {
  61077. +#define BS_SG_VAL_DESC_LEN 6
  61078. + /* The OUT EP address */
  61079. + uint8_t bOutEndpointAddress;
  61080. + /* The IN EP address */
  61081. + uint8_t bInEndpointAddress;
  61082. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  61083. + uint8_t bOffset;
  61084. + /* The number of transfer segments (a DMA descriptors per each segment) */
  61085. + uint8_t bCount;
  61086. + /* Size (in byte) of each transfer segment */
  61087. + uint16_t wSize;
  61088. +} __attribute__ ((packed));
  61089. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  61090. +
  61091. +/** Descriptor DMA Concatenation Buffer setup structure */
  61092. +struct _ddma_concat_buffer_setup_hdr {
  61093. +#define BS_CONCAT_VAL_HDR_LEN 4
  61094. + /* The endpoint for which the buffer is to be set up */
  61095. + uint8_t bEndpointAddress;
  61096. + /* The count of descriptors to be used */
  61097. + uint8_t bDescCount;
  61098. + /* The total size of the transfer */
  61099. + uint16_t wSize;
  61100. +} __attribute__ ((packed));
  61101. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  61102. +
  61103. +/** Descriptor DMA Concatenation Buffer setup structure */
  61104. +struct _ddma_concat_buffer_setup {
  61105. + /* The SG header */
  61106. + ddma_concat_buffer_setup_hdr_t hdr;
  61107. +
  61108. + /* The XFER sizes pointer (allocated dynamically) */
  61109. + uint16_t *wTxBytes;
  61110. +} __attribute__ ((packed));
  61111. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  61112. +
  61113. +/** Descriptor DMA Alignment Buffer setup structure */
  61114. +struct _ddma_align_buffer_setup {
  61115. +#define BS_ALIGN_VAL_HDR_LEN 2
  61116. + uint8_t bEndpointAddress;
  61117. + uint8_t bAlign;
  61118. +} __attribute__ ((packed));
  61119. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  61120. +
  61121. +/** Transmit FIFO Size setup structure */
  61122. +struct _tx_fifo_size_setup {
  61123. + uint8_t bEndpointAddress;
  61124. + uint16_t wDepth;
  61125. +} __attribute__ ((packed));
  61126. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  61127. +
  61128. +/** Transmit FIFO Size setup structure */
  61129. +struct _rx_fifo_size_setup {
  61130. + uint16_t wDepth;
  61131. +} __attribute__ ((packed));
  61132. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  61133. +
  61134. +/**
  61135. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  61136. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  61137. + * to the data returned in the data stage of a 3-stage Control Write requests.
  61138. + */
  61139. +struct cfi_usb_ctrlrequest {
  61140. + uint8_t bRequestType;
  61141. + uint8_t bRequest;
  61142. + uint16_t wValue;
  61143. + uint16_t wIndex;
  61144. + uint16_t wLength;
  61145. + uint8_t *data;
  61146. +} UPACKED;
  61147. +
  61148. +/*---------------------------------------------------------------------------*/
  61149. +
  61150. +/**
  61151. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  61152. + * This structure is used to store the buffer setup data for any
  61153. + * enabled endpoint in the PCD.
  61154. + */
  61155. +struct cfi_ep {
  61156. + /* Entry for the list container */
  61157. + dwc_list_link_t lh;
  61158. + /* Pointer to the active PCD endpoint structure */
  61159. + struct dwc_otg_pcd_ep *ep;
  61160. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  61161. + struct dwc_otg_dma_desc *dma_desc_last;
  61162. + /* The SG feature value */
  61163. + ddma_sg_buffer_setup_t *bm_sg;
  61164. + /* The Circular feature value */
  61165. + ddma_sg_buffer_setup_t *bm_circ;
  61166. + /* The Concatenation feature value */
  61167. + ddma_concat_buffer_setup_t *bm_concat;
  61168. + /* The Alignment feature value */
  61169. + ddma_align_buffer_setup_t *bm_align;
  61170. + /* XFER length */
  61171. + uint32_t xfer_len;
  61172. + /*
  61173. + * Count of DMA descriptors currently used.
  61174. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  61175. + * defined in the dwc_otg_cil.h
  61176. + */
  61177. + uint32_t desc_count;
  61178. +};
  61179. +typedef struct cfi_ep cfi_ep_t;
  61180. +
  61181. +typedef struct cfi_dma_buff {
  61182. +#define CFI_IN_BUF_LEN 1024
  61183. +#define CFI_OUT_BUF_LEN 1024
  61184. + dma_addr_t addr;
  61185. + uint8_t *buf;
  61186. +} cfi_dma_buff_t;
  61187. +
  61188. +struct cfiobject;
  61189. +
  61190. +/**
  61191. + * This is the interface for the CFI operations.
  61192. + *
  61193. + * @param ep_enable Called when any endpoint is enabled and activated.
  61194. + * @param release Called when the CFI object is released and it needs to correctly
  61195. + * deallocate the dynamic memory
  61196. + * @param ctrl_write_complete Called when the data stage of the request is complete
  61197. + */
  61198. +typedef struct cfi_ops {
  61199. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  61200. + struct dwc_otg_pcd_ep * ep);
  61201. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  61202. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  61203. + unsigned size, gfp_t flags);
  61204. + void (*release) (struct cfiobject * cfi);
  61205. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  61206. + struct dwc_otg_pcd * pcd);
  61207. + void (*build_descriptors) (struct cfiobject * cfi,
  61208. + struct dwc_otg_pcd * pcd,
  61209. + struct dwc_otg_pcd_ep * ep,
  61210. + dwc_otg_pcd_request_t * req);
  61211. +} cfi_ops_t;
  61212. +
  61213. +struct cfiobject {
  61214. + cfi_ops_t ops;
  61215. + struct dwc_otg_pcd *pcd;
  61216. + struct usb_gadget *gadget;
  61217. +
  61218. + /* Buffers used to send/receive CFI-related request data */
  61219. + cfi_dma_buff_t buf_in;
  61220. + cfi_dma_buff_t buf_out;
  61221. +
  61222. + /* CFI specific Control request wrapper */
  61223. + struct cfi_usb_ctrlrequest ctrl_req;
  61224. +
  61225. + /* The list of active EP's in the PCD of type cfi_ep_t */
  61226. + dwc_list_link_t active_eps;
  61227. +
  61228. + /* This flag shall control the propagation of a specific request
  61229. + * to the gadget's processing routines.
  61230. + * 0 - no gadget handling
  61231. + * 1 - the gadget needs to know about this request (w/o completing a status
  61232. + * phase - just return a 0 to the _setup callback)
  61233. + */
  61234. + uint8_t need_gadget_att;
  61235. +
  61236. + /* Flag indicating whether the status IN phase needs to be
  61237. + * completed by the PCD
  61238. + */
  61239. + uint8_t need_status_in_complete;
  61240. +};
  61241. +typedef struct cfiobject cfiobject_t;
  61242. +
  61243. +#define DUMP_MSG
  61244. +
  61245. +#if defined(DUMP_MSG)
  61246. +static inline void dump_msg(const u8 * buf, unsigned int length)
  61247. +{
  61248. + unsigned int start, num, i;
  61249. + char line[52], *p;
  61250. +
  61251. + if (length >= 512)
  61252. + return;
  61253. +
  61254. + start = 0;
  61255. + while (length > 0) {
  61256. + num = min(length, 16u);
  61257. + p = line;
  61258. + for (i = 0; i < num; ++i) {
  61259. + if (i == 8)
  61260. + *p++ = ' ';
  61261. + DWC_SPRINTF(p, " %02x", buf[i]);
  61262. + p += 3;
  61263. + }
  61264. + *p = 0;
  61265. + DWC_DEBUG("%6x: %s\n", start, line);
  61266. + buf += num;
  61267. + start += num;
  61268. + length -= num;
  61269. + }
  61270. +}
  61271. +#else
  61272. +static inline void dump_msg(const u8 * buf, unsigned int length)
  61273. +{
  61274. +}
  61275. +#endif
  61276. +
  61277. +/**
  61278. + * This function returns a pointer to cfi_ep_t object with the addr address.
  61279. + */
  61280. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  61281. + uint8_t addr)
  61282. +{
  61283. + struct cfi_ep *pcfiep;
  61284. + dwc_list_link_t *tmp;
  61285. +
  61286. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  61287. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61288. +
  61289. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  61290. + return pcfiep;
  61291. + }
  61292. + }
  61293. +
  61294. + return NULL;
  61295. +}
  61296. +
  61297. +/**
  61298. + * This function returns a pointer to cfi_ep_t object that matches
  61299. + * the dwc_otg_pcd_ep object.
  61300. + */
  61301. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  61302. + struct dwc_otg_pcd_ep *ep)
  61303. +{
  61304. + struct cfi_ep *pcfiep = NULL;
  61305. + dwc_list_link_t *tmp;
  61306. +
  61307. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  61308. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61309. + if (pcfiep->ep == ep) {
  61310. + return pcfiep;
  61311. + }
  61312. + }
  61313. + return NULL;
  61314. +}
  61315. +
  61316. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  61317. +
  61318. +#endif /* (__DWC_OTG_CFI_H__) */
  61319. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cil.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  61320. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cil.c 1969-12-31 18:00:00.000000000 -0600
  61321. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c 2014-12-11 14:02:55.392418001 -0600
  61322. @@ -0,0 +1,7151 @@
  61323. +/* ==========================================================================
  61324. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  61325. + * $Revision: #191 $
  61326. + * $Date: 2012/08/10 $
  61327. + * $Change: 2047372 $
  61328. + *
  61329. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  61330. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  61331. + * otherwise expressly agreed to in writing between Synopsys and you.
  61332. + *
  61333. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  61334. + * any End User Software License Agreement or Agreement for Licensed Product
  61335. + * with Synopsys or any supplement thereto. You are permitted to use and
  61336. + * redistribute this Software in source and binary forms, with or without
  61337. + * modification, provided that redistributions of source code must retain this
  61338. + * notice. You may not view, use, disclose, copy or distribute this file or
  61339. + * any information contained herein except pursuant to this license grant from
  61340. + * Synopsys. If you do not agree with this notice, including the disclaimer
  61341. + * below, then you are not authorized to use the Software.
  61342. + *
  61343. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  61344. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61345. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61346. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  61347. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  61348. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61349. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  61350. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  61351. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  61352. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  61353. + * DAMAGE.
  61354. + * ========================================================================== */
  61355. +
  61356. +/** @file
  61357. + *
  61358. + * The Core Interface Layer provides basic services for accessing and
  61359. + * managing the DWC_otg hardware. These services are used by both the
  61360. + * Host Controller Driver and the Peripheral Controller Driver.
  61361. + *
  61362. + * The CIL manages the memory map for the core so that the HCD and PCD
  61363. + * don't have to do this separately. It also handles basic tasks like
  61364. + * reading/writing the registers and data FIFOs in the controller.
  61365. + * Some of the data access functions provide encapsulation of several
  61366. + * operations required to perform a task, such as writing multiple
  61367. + * registers to start a transfer. Finally, the CIL performs basic
  61368. + * services that are not specific to either the host or device modes
  61369. + * of operation. These services include management of the OTG Host
  61370. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  61371. + * Diagnostic API is also provided to allow testing of the controller
  61372. + * hardware.
  61373. + *
  61374. + * The Core Interface Layer has the following requirements:
  61375. + * - Provides basic controller operations.
  61376. + * - Minimal use of OS services.
  61377. + * - The OS services used will be abstracted by using inline functions
  61378. + * or macros.
  61379. + *
  61380. + */
  61381. +
  61382. +#include "dwc_os.h"
  61383. +#include "dwc_otg_regs.h"
  61384. +#include "dwc_otg_cil.h"
  61385. +
  61386. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  61387. +
  61388. +/**
  61389. + * This function is called to initialize the DWC_otg CSR data
  61390. + * structures. The register addresses in the device and host
  61391. + * structures are initialized from the base address supplied by the
  61392. + * caller. The calling function must make the OS calls to get the
  61393. + * base address of the DWC_otg controller registers. The core_params
  61394. + * argument holds the parameters that specify how the core should be
  61395. + * configured.
  61396. + *
  61397. + * @param reg_base_addr Base address of DWC_otg core registers
  61398. + *
  61399. + */
  61400. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  61401. +{
  61402. + dwc_otg_core_if_t *core_if = 0;
  61403. + dwc_otg_dev_if_t *dev_if = 0;
  61404. + dwc_otg_host_if_t *host_if = 0;
  61405. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  61406. + int i = 0;
  61407. +
  61408. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  61409. +
  61410. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  61411. +
  61412. + if (core_if == NULL) {
  61413. + DWC_DEBUGPL(DBG_CIL,
  61414. + "Allocation of dwc_otg_core_if_t failed\n");
  61415. + return 0;
  61416. + }
  61417. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  61418. +
  61419. + /*
  61420. + * Allocate the Device Mode structures.
  61421. + */
  61422. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  61423. +
  61424. + if (dev_if == NULL) {
  61425. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  61426. + DWC_FREE(core_if);
  61427. + return 0;
  61428. + }
  61429. +
  61430. + dev_if->dev_global_regs =
  61431. + (dwc_otg_device_global_regs_t *) (reg_base +
  61432. + DWC_DEV_GLOBAL_REG_OFFSET);
  61433. +
  61434. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  61435. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  61436. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  61437. + (i * DWC_EP_REG_OFFSET));
  61438. +
  61439. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  61440. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  61441. + (i * DWC_EP_REG_OFFSET));
  61442. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  61443. + i, &dev_if->in_ep_regs[i]->diepctl);
  61444. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  61445. + i, &dev_if->out_ep_regs[i]->doepctl);
  61446. + }
  61447. +
  61448. + dev_if->speed = 0; // unknown
  61449. +
  61450. + core_if->dev_if = dev_if;
  61451. +
  61452. + /*
  61453. + * Allocate the Host Mode structures.
  61454. + */
  61455. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  61456. +
  61457. + if (host_if == NULL) {
  61458. + DWC_DEBUGPL(DBG_CIL,
  61459. + "Allocation of dwc_otg_host_if_t failed\n");
  61460. + DWC_FREE(dev_if);
  61461. + DWC_FREE(core_if);
  61462. + return 0;
  61463. + }
  61464. +
  61465. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  61466. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  61467. +
  61468. + host_if->hprt0 =
  61469. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  61470. +
  61471. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  61472. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  61473. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  61474. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  61475. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  61476. + i, &host_if->hc_regs[i]->hcchar);
  61477. + }
  61478. +
  61479. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  61480. + core_if->host_if = host_if;
  61481. +
  61482. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  61483. + core_if->data_fifo[i] =
  61484. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  61485. + (i * DWC_OTG_DATA_FIFO_SIZE));
  61486. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  61487. + i, (unsigned long)core_if->data_fifo[i]);
  61488. + }
  61489. +
  61490. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  61491. +
  61492. + /* Initiate lx_state to L3 disconnected state */
  61493. + core_if->lx_state = DWC_OTG_L3;
  61494. + /*
  61495. + * Store the contents of the hardware configuration registers here for
  61496. + * easy access later.
  61497. + */
  61498. + core_if->hwcfg1.d32 =
  61499. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  61500. + core_if->hwcfg2.d32 =
  61501. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  61502. + core_if->hwcfg3.d32 =
  61503. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  61504. + core_if->hwcfg4.d32 =
  61505. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  61506. +
  61507. + /* Force host mode to get HPTXFSIZ exact power on value */
  61508. + {
  61509. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  61510. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61511. + gusbcfg.b.force_host_mode = 1;
  61512. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  61513. + dwc_mdelay(100);
  61514. + core_if->hptxfsiz.d32 =
  61515. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  61516. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61517. + gusbcfg.b.force_host_mode = 0;
  61518. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  61519. + dwc_mdelay(100);
  61520. + }
  61521. +
  61522. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  61523. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  61524. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  61525. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  61526. +
  61527. + core_if->hcfg.d32 =
  61528. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  61529. + core_if->dcfg.d32 =
  61530. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  61531. +
  61532. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  61533. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  61534. +
  61535. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  61536. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  61537. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  61538. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  61539. + core_if->hwcfg2.b.num_host_chan);
  61540. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  61541. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  61542. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  61543. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  61544. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  61545. + core_if->hwcfg2.b.dev_token_q_depth);
  61546. +
  61547. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  61548. + core_if->hwcfg3.b.dfifo_depth);
  61549. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  61550. + core_if->hwcfg3.b.xfer_size_cntr_width);
  61551. +
  61552. + /*
  61553. + * Set the SRP sucess bit for FS-I2c
  61554. + */
  61555. + core_if->srp_success = 0;
  61556. + core_if->srp_timer_started = 0;
  61557. +
  61558. + /*
  61559. + * Create new workqueue and init works
  61560. + */
  61561. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  61562. + if (core_if->wq_otg == 0) {
  61563. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  61564. + DWC_FREE(host_if);
  61565. + DWC_FREE(dev_if);
  61566. + DWC_FREE(core_if);
  61567. + return 0;
  61568. + }
  61569. +
  61570. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  61571. +
  61572. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  61573. + (core_if->snpsid >> 12 & 0xF),
  61574. + (core_if->snpsid >> 8 & 0xF),
  61575. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  61576. +
  61577. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  61578. + w_wakeup_detected, core_if);
  61579. + if (core_if->wkp_timer == 0) {
  61580. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  61581. + DWC_FREE(host_if);
  61582. + DWC_FREE(dev_if);
  61583. + DWC_WORKQ_FREE(core_if->wq_otg);
  61584. + DWC_FREE(core_if);
  61585. + return 0;
  61586. + }
  61587. +
  61588. + if (dwc_otg_setup_params(core_if)) {
  61589. + DWC_WARN("Error while setting core params\n");
  61590. + }
  61591. +
  61592. + core_if->hibernation_suspend = 0;
  61593. +
  61594. + /** ADP initialization */
  61595. + dwc_otg_adp_init(core_if);
  61596. +
  61597. + return core_if;
  61598. +}
  61599. +
  61600. +/**
  61601. + * This function frees the structures allocated by dwc_otg_cil_init().
  61602. + *
  61603. + * @param core_if The core interface pointer returned from
  61604. + * dwc_otg_cil_init().
  61605. + *
  61606. + */
  61607. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  61608. +{
  61609. + dctl_data_t dctl = {.d32 = 0 };
  61610. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  61611. +
  61612. + /* Disable all interrupts */
  61613. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  61614. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  61615. +
  61616. + dctl.b.sftdiscon = 1;
  61617. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  61618. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  61619. + dctl.d32);
  61620. + }
  61621. +
  61622. + if (core_if->wq_otg) {
  61623. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  61624. + DWC_WORKQ_FREE(core_if->wq_otg);
  61625. + }
  61626. + if (core_if->dev_if) {
  61627. + DWC_FREE(core_if->dev_if);
  61628. + }
  61629. + if (core_if->host_if) {
  61630. + DWC_FREE(core_if->host_if);
  61631. + }
  61632. +
  61633. + /** Remove ADP Stuff */
  61634. + dwc_otg_adp_remove(core_if);
  61635. + if (core_if->core_params) {
  61636. + DWC_FREE(core_if->core_params);
  61637. + }
  61638. + if (core_if->wkp_timer) {
  61639. + DWC_TIMER_FREE(core_if->wkp_timer);
  61640. + }
  61641. + if (core_if->srp_timer) {
  61642. + DWC_TIMER_FREE(core_if->srp_timer);
  61643. + }
  61644. + DWC_FREE(core_if);
  61645. +}
  61646. +
  61647. +/**
  61648. + * This function enables the controller's Global Interrupt in the AHB Config
  61649. + * register.
  61650. + *
  61651. + * @param core_if Programming view of DWC_otg controller.
  61652. + */
  61653. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  61654. +{
  61655. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  61656. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  61657. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  61658. +}
  61659. +
  61660. +/**
  61661. + * This function disables the controller's Global Interrupt in the AHB Config
  61662. + * register.
  61663. + *
  61664. + * @param core_if Programming view of DWC_otg controller.
  61665. + */
  61666. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  61667. +{
  61668. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  61669. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  61670. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  61671. +}
  61672. +
  61673. +/**
  61674. + * This function initializes the commmon interrupts, used in both
  61675. + * device and host modes.
  61676. + *
  61677. + * @param core_if Programming view of the DWC_otg controller
  61678. + *
  61679. + */
  61680. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  61681. +{
  61682. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  61683. + gintmsk_data_t intr_mask = {.d32 = 0 };
  61684. +
  61685. + /* Clear any pending OTG Interrupts */
  61686. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  61687. +
  61688. + /* Clear any pending interrupts */
  61689. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  61690. +
  61691. + /*
  61692. + * Enable the interrupts in the GINTMSK.
  61693. + */
  61694. + intr_mask.b.modemismatch = 1;
  61695. + intr_mask.b.otgintr = 1;
  61696. +
  61697. + if (!core_if->dma_enable) {
  61698. + intr_mask.b.rxstsqlvl = 1;
  61699. + }
  61700. +
  61701. + intr_mask.b.conidstschng = 1;
  61702. + intr_mask.b.wkupintr = 1;
  61703. + intr_mask.b.disconnect = 0;
  61704. + intr_mask.b.usbsuspend = 1;
  61705. + intr_mask.b.sessreqintr = 1;
  61706. +#ifdef CONFIG_USB_DWC_OTG_LPM
  61707. + if (core_if->core_params->lpm_enable) {
  61708. + intr_mask.b.lpmtranrcvd = 1;
  61709. + }
  61710. +#endif
  61711. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  61712. +}
  61713. +
  61714. +/*
  61715. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  61716. + * Hibernation. This function is for exiting from Device mode hibernation by
  61717. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  61718. + * @param core_if Programming view of DWC_otg controller.
  61719. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  61720. + * @param reset - indicates whether resume is initiated by Reset.
  61721. + */
  61722. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  61723. + int rem_wakeup, int reset)
  61724. +{
  61725. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  61726. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  61727. + dctl_data_t dctl = {.d32 = 0 };
  61728. +
  61729. + int timeout = 2000;
  61730. +
  61731. + if (!core_if->hibernation_suspend) {
  61732. + DWC_PRINTF("Already exited from Hibernation\n");
  61733. + return 1;
  61734. + }
  61735. +
  61736. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  61737. + /* Switch-on voltage to the core */
  61738. + gpwrdn.b.pwrdnswtch = 1;
  61739. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61740. + dwc_udelay(10);
  61741. +
  61742. + /* Reset core */
  61743. + gpwrdn.d32 = 0;
  61744. + gpwrdn.b.pwrdnrstn = 1;
  61745. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61746. + dwc_udelay(10);
  61747. +
  61748. + /* Assert Restore signal */
  61749. + gpwrdn.d32 = 0;
  61750. + gpwrdn.b.restore = 1;
  61751. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  61752. + dwc_udelay(10);
  61753. +
  61754. + /* Disable power clamps */
  61755. + gpwrdn.d32 = 0;
  61756. + gpwrdn.b.pwrdnclmp = 1;
  61757. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61758. +
  61759. + if (rem_wakeup) {
  61760. + dwc_udelay(70);
  61761. + }
  61762. +
  61763. + /* Deassert Reset core */
  61764. + gpwrdn.d32 = 0;
  61765. + gpwrdn.b.pwrdnrstn = 1;
  61766. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  61767. + dwc_udelay(10);
  61768. +
  61769. + /* Disable PMU interrupt */
  61770. + gpwrdn.d32 = 0;
  61771. + gpwrdn.b.pmuintsel = 1;
  61772. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61773. +
  61774. + /* Mask interrupts from gpwrdn */
  61775. + gpwrdn.d32 = 0;
  61776. + gpwrdn.b.connect_det_msk = 1;
  61777. + gpwrdn.b.srp_det_msk = 1;
  61778. + gpwrdn.b.disconn_det_msk = 1;
  61779. + gpwrdn.b.rst_det_msk = 1;
  61780. + gpwrdn.b.lnstchng_msk = 1;
  61781. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61782. +
  61783. + /* Indicates that we are going out from hibernation */
  61784. + core_if->hibernation_suspend = 0;
  61785. +
  61786. + /*
  61787. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  61788. + * indicates restore from remote_wakeup
  61789. + */
  61790. + restore_essential_regs(core_if, rem_wakeup, 0);
  61791. +
  61792. + /*
  61793. + * Wait a little for seeing new value of variable hibernation_suspend if
  61794. + * Restore done interrupt received before polling
  61795. + */
  61796. + dwc_udelay(10);
  61797. +
  61798. + if (core_if->hibernation_suspend == 0) {
  61799. + /*
  61800. + * Wait For Restore_done Interrupt. This mechanism of polling the
  61801. + * interrupt is introduced to avoid any possible race conditions
  61802. + */
  61803. + do {
  61804. + gintsts_data_t gintsts;
  61805. + gintsts.d32 =
  61806. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  61807. + if (gintsts.b.restoredone) {
  61808. + gintsts.d32 = 0;
  61809. + gintsts.b.restoredone = 1;
  61810. + DWC_WRITE_REG32(&core_if->core_global_regs->
  61811. + gintsts, gintsts.d32);
  61812. + DWC_PRINTF("Restore Done Interrupt seen\n");
  61813. + break;
  61814. + }
  61815. + dwc_udelay(10);
  61816. + } while (--timeout);
  61817. + if (!timeout) {
  61818. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  61819. + }
  61820. + }
  61821. + /* Clear all pending interupts */
  61822. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  61823. +
  61824. + /* De-assert Restore */
  61825. + gpwrdn.d32 = 0;
  61826. + gpwrdn.b.restore = 1;
  61827. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61828. + dwc_udelay(10);
  61829. +
  61830. + if (!rem_wakeup) {
  61831. + pcgcctl.d32 = 0;
  61832. + pcgcctl.b.rstpdwnmodule = 1;
  61833. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  61834. + }
  61835. +
  61836. + /* Restore GUSBCFG and DCFG */
  61837. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  61838. + core_if->gr_backup->gusbcfg_local);
  61839. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  61840. + core_if->dr_backup->dcfg);
  61841. +
  61842. + /* De-assert Wakeup Logic */
  61843. + gpwrdn.d32 = 0;
  61844. + gpwrdn.b.pmuactv = 1;
  61845. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61846. + dwc_udelay(10);
  61847. +
  61848. + if (!rem_wakeup) {
  61849. + /* Set Device programming done bit */
  61850. + dctl.b.pwronprgdone = 1;
  61851. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  61852. + } else {
  61853. + /* Start Remote Wakeup Signaling */
  61854. + dctl.d32 = core_if->dr_backup->dctl;
  61855. + dctl.b.rmtwkupsig = 1;
  61856. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  61857. + }
  61858. +
  61859. + dwc_mdelay(2);
  61860. + /* Clear all pending interupts */
  61861. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  61862. +
  61863. + /* Restore global registers */
  61864. + dwc_otg_restore_global_regs(core_if);
  61865. + /* Restore device global registers */
  61866. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  61867. +
  61868. + if (rem_wakeup) {
  61869. + dwc_mdelay(7);
  61870. + dctl.d32 = 0;
  61871. + dctl.b.rmtwkupsig = 1;
  61872. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  61873. + }
  61874. +
  61875. + core_if->hibernation_suspend = 0;
  61876. + /* The core will be in ON STATE */
  61877. + core_if->lx_state = DWC_OTG_L0;
  61878. + DWC_PRINTF("Hibernation recovery completes here\n");
  61879. +
  61880. + return 1;
  61881. +}
  61882. +
  61883. +/*
  61884. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  61885. + * Hibernation. This function is for exiting from Host mode hibernation by
  61886. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  61887. + * @param core_if Programming view of DWC_otg controller.
  61888. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  61889. + * @param reset - indicates whether resume is initiated by Reset.
  61890. + */
  61891. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  61892. + int rem_wakeup, int reset)
  61893. +{
  61894. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  61895. + hprt0_data_t hprt0 = {.d32 = 0 };
  61896. +
  61897. + int timeout = 2000;
  61898. +
  61899. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  61900. + /* Switch-on voltage to the core */
  61901. + gpwrdn.b.pwrdnswtch = 1;
  61902. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61903. + dwc_udelay(10);
  61904. +
  61905. + /* Reset core */
  61906. + gpwrdn.d32 = 0;
  61907. + gpwrdn.b.pwrdnrstn = 1;
  61908. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61909. + dwc_udelay(10);
  61910. +
  61911. + /* Assert Restore signal */
  61912. + gpwrdn.d32 = 0;
  61913. + gpwrdn.b.restore = 1;
  61914. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  61915. + dwc_udelay(10);
  61916. +
  61917. + /* Disable power clamps */
  61918. + gpwrdn.d32 = 0;
  61919. + gpwrdn.b.pwrdnclmp = 1;
  61920. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61921. +
  61922. + if (!rem_wakeup) {
  61923. + dwc_udelay(50);
  61924. + }
  61925. +
  61926. + /* Deassert Reset core */
  61927. + gpwrdn.d32 = 0;
  61928. + gpwrdn.b.pwrdnrstn = 1;
  61929. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  61930. + dwc_udelay(10);
  61931. +
  61932. + /* Disable PMU interrupt */
  61933. + gpwrdn.d32 = 0;
  61934. + gpwrdn.b.pmuintsel = 1;
  61935. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61936. +
  61937. + gpwrdn.d32 = 0;
  61938. + gpwrdn.b.connect_det_msk = 1;
  61939. + gpwrdn.b.srp_det_msk = 1;
  61940. + gpwrdn.b.disconn_det_msk = 1;
  61941. + gpwrdn.b.rst_det_msk = 1;
  61942. + gpwrdn.b.lnstchng_msk = 1;
  61943. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61944. +
  61945. + /* Indicates that we are going out from hibernation */
  61946. + core_if->hibernation_suspend = 0;
  61947. +
  61948. + /* Set Restore Essential Regs bit in PCGCCTL register */
  61949. + restore_essential_regs(core_if, rem_wakeup, 1);
  61950. +
  61951. + /* Wait a little for seeing new value of variable hibernation_suspend if
  61952. + * Restore done interrupt received before polling */
  61953. + dwc_udelay(10);
  61954. +
  61955. + if (core_if->hibernation_suspend == 0) {
  61956. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  61957. + * interrupt is introduced to avoid any possible race conditions
  61958. + */
  61959. + do {
  61960. + gintsts_data_t gintsts;
  61961. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  61962. + if (gintsts.b.restoredone) {
  61963. + gintsts.d32 = 0;
  61964. + gintsts.b.restoredone = 1;
  61965. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  61966. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  61967. + break;
  61968. + }
  61969. + dwc_udelay(10);
  61970. + } while (--timeout);
  61971. + if (!timeout) {
  61972. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  61973. + }
  61974. + }
  61975. +
  61976. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  61977. + core_if->hibernation_suspend = 0;
  61978. +
  61979. + /* This step is not described in functional spec but if not wait for this
  61980. + * delay, mismatch interrupts occurred because just after restore core is
  61981. + * in Device mode(gintsts.curmode == 0) */
  61982. + dwc_mdelay(100);
  61983. +
  61984. + /* Clear all pending interrupts */
  61985. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  61986. +
  61987. + /* De-assert Restore */
  61988. + gpwrdn.d32 = 0;
  61989. + gpwrdn.b.restore = 1;
  61990. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  61991. + dwc_udelay(10);
  61992. +
  61993. + /* Restore GUSBCFG and HCFG */
  61994. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  61995. + core_if->gr_backup->gusbcfg_local);
  61996. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  61997. + core_if->hr_backup->hcfg_local);
  61998. +
  61999. + /* De-assert Wakeup Logic */
  62000. + gpwrdn.d32 = 0;
  62001. + gpwrdn.b.pmuactv = 1;
  62002. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62003. + dwc_udelay(10);
  62004. +
  62005. + /* Start the Resume operation by programming HPRT0 */
  62006. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  62007. + hprt0.b.prtpwr = 1;
  62008. + hprt0.b.prtena = 0;
  62009. + hprt0.b.prtsusp = 0;
  62010. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62011. +
  62012. + DWC_PRINTF("Resume Starts Now\n");
  62013. + if (!reset) { // Indicates it is Resume Operation
  62014. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  62015. + hprt0.b.prtres = 1;
  62016. + hprt0.b.prtpwr = 1;
  62017. + hprt0.b.prtena = 0;
  62018. + hprt0.b.prtsusp = 0;
  62019. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62020. +
  62021. + if (!rem_wakeup)
  62022. + hprt0.b.prtres = 0;
  62023. + /* Wait for Resume time and then program HPRT again */
  62024. + dwc_mdelay(100);
  62025. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62026. +
  62027. + } else { // Indicates it is Reset Operation
  62028. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  62029. + hprt0.b.prtrst = 1;
  62030. + hprt0.b.prtpwr = 1;
  62031. + hprt0.b.prtena = 0;
  62032. + hprt0.b.prtsusp = 0;
  62033. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62034. + /* Wait for Reset time and then program HPRT again */
  62035. + dwc_mdelay(60);
  62036. + hprt0.b.prtrst = 0;
  62037. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62038. + }
  62039. + /* Clear all interrupt status */
  62040. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62041. + hprt0.b.prtconndet = 1;
  62042. + hprt0.b.prtenchng = 1;
  62043. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62044. +
  62045. + /* Clear all pending interupts */
  62046. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62047. +
  62048. + /* Restore global registers */
  62049. + dwc_otg_restore_global_regs(core_if);
  62050. + /* Restore host global registers */
  62051. + dwc_otg_restore_host_regs(core_if, reset);
  62052. +
  62053. + /* The core will be in ON STATE */
  62054. + core_if->lx_state = DWC_OTG_L0;
  62055. + DWC_PRINTF("Hibernation recovery is complete here\n");
  62056. + return 0;
  62057. +}
  62058. +
  62059. +/** Saves some register values into system memory. */
  62060. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  62061. +{
  62062. + struct dwc_otg_global_regs_backup *gr;
  62063. + int i;
  62064. +
  62065. + gr = core_if->gr_backup;
  62066. + if (!gr) {
  62067. + gr = DWC_ALLOC(sizeof(*gr));
  62068. + if (!gr) {
  62069. + return -DWC_E_NO_MEMORY;
  62070. + }
  62071. + core_if->gr_backup = gr;
  62072. + }
  62073. +
  62074. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  62075. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  62076. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  62077. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62078. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  62079. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  62080. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  62081. +#ifdef CONFIG_USB_DWC_OTG_LPM
  62082. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  62083. +#endif
  62084. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  62085. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  62086. + gr->gdfifocfg_local =
  62087. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  62088. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  62089. + gr->dtxfsiz_local[i] =
  62090. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  62091. + }
  62092. +
  62093. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  62094. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  62095. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  62096. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  62097. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  62098. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  62099. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  62100. + gr->gnptxfsiz_local);
  62101. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  62102. + gr->hptxfsiz_local);
  62103. +#ifdef CONFIG_USB_DWC_OTG_LPM
  62104. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  62105. +#endif
  62106. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  62107. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  62108. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  62109. +
  62110. + return 0;
  62111. +}
  62112. +
  62113. +/** Saves GINTMSK register before setting the msk bits. */
  62114. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  62115. +{
  62116. + struct dwc_otg_global_regs_backup *gr;
  62117. +
  62118. + gr = core_if->gr_backup;
  62119. + if (!gr) {
  62120. + gr = DWC_ALLOC(sizeof(*gr));
  62121. + if (!gr) {
  62122. + return -DWC_E_NO_MEMORY;
  62123. + }
  62124. + core_if->gr_backup = gr;
  62125. + }
  62126. +
  62127. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  62128. +
  62129. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  62130. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  62131. +
  62132. + return 0;
  62133. +}
  62134. +
  62135. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  62136. +{
  62137. + struct dwc_otg_dev_regs_backup *dr;
  62138. + int i;
  62139. +
  62140. + dr = core_if->dr_backup;
  62141. + if (!dr) {
  62142. + dr = DWC_ALLOC(sizeof(*dr));
  62143. + if (!dr) {
  62144. + return -DWC_E_NO_MEMORY;
  62145. + }
  62146. + core_if->dr_backup = dr;
  62147. + }
  62148. +
  62149. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62150. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  62151. + dr->daintmsk =
  62152. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  62153. + dr->diepmsk =
  62154. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  62155. + dr->doepmsk =
  62156. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  62157. +
  62158. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  62159. + dr->diepctl[i] =
  62160. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  62161. + dr->dieptsiz[i] =
  62162. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  62163. + dr->diepdma[i] =
  62164. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  62165. + }
  62166. +
  62167. + DWC_DEBUGPL(DBG_ANY,
  62168. + "=============Backing Host registers==============\n");
  62169. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  62170. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  62171. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  62172. + dr->daintmsk);
  62173. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  62174. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  62175. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  62176. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  62177. + dr->diepctl[i]);
  62178. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  62179. + i, dr->dieptsiz[i]);
  62180. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  62181. + dr->diepdma[i]);
  62182. + }
  62183. +
  62184. + return 0;
  62185. +}
  62186. +
  62187. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  62188. +{
  62189. + struct dwc_otg_host_regs_backup *hr;
  62190. + int i;
  62191. +
  62192. + hr = core_if->hr_backup;
  62193. + if (!hr) {
  62194. + hr = DWC_ALLOC(sizeof(*hr));
  62195. + if (!hr) {
  62196. + return -DWC_E_NO_MEMORY;
  62197. + }
  62198. + core_if->hr_backup = hr;
  62199. + }
  62200. +
  62201. + hr->hcfg_local =
  62202. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62203. + hr->haintmsk_local =
  62204. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  62205. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  62206. + hr->hcintmsk_local[i] =
  62207. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  62208. + }
  62209. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  62210. + hr->hfir_local =
  62211. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  62212. +
  62213. + DWC_DEBUGPL(DBG_ANY,
  62214. + "=============Backing Host registers===============\n");
  62215. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  62216. + hr->hcfg_local);
  62217. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  62218. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  62219. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  62220. + hr->hcintmsk_local[i]);
  62221. + }
  62222. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  62223. + hr->hprt0_local);
  62224. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  62225. + hr->hfir_local);
  62226. +
  62227. + return 0;
  62228. +}
  62229. +
  62230. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  62231. +{
  62232. + struct dwc_otg_global_regs_backup *gr;
  62233. + int i;
  62234. +
  62235. + gr = core_if->gr_backup;
  62236. + if (!gr) {
  62237. + return -DWC_E_INVALID;
  62238. + }
  62239. +
  62240. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  62241. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  62242. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  62243. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  62244. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  62245. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  62246. + gr->gnptxfsiz_local);
  62247. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  62248. + gr->hptxfsiz_local);
  62249. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  62250. + gr->gdfifocfg_local);
  62251. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  62252. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  62253. + gr->dtxfsiz_local[i]);
  62254. + }
  62255. +
  62256. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62257. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  62258. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  62259. + (gr->gahbcfg_local));
  62260. + return 0;
  62261. +}
  62262. +
  62263. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  62264. +{
  62265. + struct dwc_otg_dev_regs_backup *dr;
  62266. + int i;
  62267. +
  62268. + dr = core_if->dr_backup;
  62269. +
  62270. + if (!dr) {
  62271. + return -DWC_E_INVALID;
  62272. + }
  62273. +
  62274. + if (!rem_wakeup) {
  62275. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  62276. + dr->dctl);
  62277. + }
  62278. +
  62279. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  62280. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  62281. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  62282. +
  62283. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  62284. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  62285. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  62286. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  62287. + }
  62288. +
  62289. + return 0;
  62290. +}
  62291. +
  62292. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  62293. +{
  62294. + struct dwc_otg_host_regs_backup *hr;
  62295. + int i;
  62296. + hr = core_if->hr_backup;
  62297. +
  62298. + if (!hr) {
  62299. + return -DWC_E_INVALID;
  62300. + }
  62301. +
  62302. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  62303. + //if (!reset)
  62304. + //{
  62305. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  62306. + //}
  62307. +
  62308. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  62309. + hr->haintmsk_local);
  62310. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  62311. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  62312. + hr->hcintmsk_local[i]);
  62313. + }
  62314. +
  62315. + return 0;
  62316. +}
  62317. +
  62318. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  62319. +{
  62320. + struct dwc_otg_global_regs_backup *gr;
  62321. +
  62322. + gr = core_if->gr_backup;
  62323. +
  62324. + /* Restore values for LPM and I2C */
  62325. +#ifdef CONFIG_USB_DWC_OTG_LPM
  62326. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  62327. +#endif
  62328. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  62329. +
  62330. + return 0;
  62331. +}
  62332. +
  62333. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  62334. +{
  62335. + struct dwc_otg_global_regs_backup *gr;
  62336. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  62337. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  62338. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  62339. + gintmsk_data_t gintmsk = {.d32 = 0 };
  62340. +
  62341. + /* Restore LPM and I2C registers */
  62342. + restore_lpm_i2c_regs(core_if);
  62343. +
  62344. + /* Set PCGCCTL to 0 */
  62345. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  62346. +
  62347. + gr = core_if->gr_backup;
  62348. + /* Load restore values for [31:14] bits */
  62349. + DWC_WRITE_REG32(core_if->pcgcctl,
  62350. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  62351. +
  62352. + /* Umnask global Interrupt in GAHBCFG and restore it */
  62353. + gahbcfg.d32 = gr->gahbcfg_local;
  62354. + gahbcfg.b.glblintrmsk = 1;
  62355. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  62356. +
  62357. + /* Clear all pending interupts */
  62358. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62359. +
  62360. + /* Unmask restore done interrupt */
  62361. + gintmsk.b.restoredone = 1;
  62362. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  62363. +
  62364. + /* Restore GUSBCFG and HCFG/DCFG */
  62365. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  62366. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  62367. +
  62368. + if (is_host) {
  62369. + hcfg_data_t hcfg = {.d32 = 0 };
  62370. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  62371. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  62372. + hcfg.d32);
  62373. +
  62374. + /* Load restore values for [31:14] bits */
  62375. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  62376. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  62377. +
  62378. + if (rmode)
  62379. + pcgcctl.b.restoremode = 1;
  62380. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  62381. + dwc_udelay(10);
  62382. +
  62383. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  62384. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  62385. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  62386. + pcgcctl.b.ess_reg_restored = 1;
  62387. + if (rmode)
  62388. + pcgcctl.b.restoremode = 1;
  62389. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  62390. + } else {
  62391. + dcfg_data_t dcfg = {.d32 = 0 };
  62392. + dcfg.d32 = core_if->dr_backup->dcfg;
  62393. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  62394. +
  62395. + /* Load restore values for [31:14] bits */
  62396. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  62397. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  62398. + if (!rmode) {
  62399. + pcgcctl.d32 |= 0x208;
  62400. + }
  62401. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  62402. + dwc_udelay(10);
  62403. +
  62404. + /* Load restore values for [31:14] bits */
  62405. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  62406. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  62407. + pcgcctl.b.ess_reg_restored = 1;
  62408. + if (!rmode)
  62409. + pcgcctl.d32 |= 0x208;
  62410. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  62411. + }
  62412. +
  62413. + return 0;
  62414. +}
  62415. +
  62416. +/**
  62417. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  62418. + * type.
  62419. + */
  62420. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  62421. +{
  62422. + uint32_t val;
  62423. + hcfg_data_t hcfg;
  62424. +
  62425. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  62426. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  62427. + (core_if->core_params->ulpi_fs_ls)) ||
  62428. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  62429. + /* Full speed PHY */
  62430. + val = DWC_HCFG_48_MHZ;
  62431. + } else {
  62432. + /* High speed PHY running at full speed or high speed */
  62433. + val = DWC_HCFG_30_60_MHZ;
  62434. + }
  62435. +
  62436. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  62437. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62438. + hcfg.b.fslspclksel = val;
  62439. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  62440. +}
  62441. +
  62442. +/**
  62443. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  62444. + * and the enumeration speed of the device.
  62445. + */
  62446. +static void init_devspd(dwc_otg_core_if_t * core_if)
  62447. +{
  62448. + uint32_t val;
  62449. + dcfg_data_t dcfg;
  62450. +
  62451. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  62452. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  62453. + (core_if->core_params->ulpi_fs_ls)) ||
  62454. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  62455. + /* Full speed PHY */
  62456. + val = 0x3;
  62457. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  62458. + /* High speed PHY running at full speed */
  62459. + val = 0x1;
  62460. + } else {
  62461. + /* High speed PHY running at high speed */
  62462. + val = 0x0;
  62463. + }
  62464. +
  62465. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  62466. +
  62467. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62468. + dcfg.b.devspd = val;
  62469. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  62470. +}
  62471. +
  62472. +/**
  62473. + * This function calculates the number of IN EPS
  62474. + * using GHWCFG1 and GHWCFG2 registers values
  62475. + *
  62476. + * @param core_if Programming view of the DWC_otg controller
  62477. + */
  62478. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  62479. +{
  62480. + uint32_t num_in_eps = 0;
  62481. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  62482. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  62483. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  62484. + int i;
  62485. +
  62486. + for (i = 0; i < num_eps; ++i) {
  62487. + if (!(hwcfg1 & 0x1))
  62488. + num_in_eps++;
  62489. +
  62490. + hwcfg1 >>= 2;
  62491. + }
  62492. +
  62493. + if (core_if->hwcfg4.b.ded_fifo_en) {
  62494. + num_in_eps =
  62495. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  62496. + }
  62497. +
  62498. + return num_in_eps;
  62499. +}
  62500. +
  62501. +/**
  62502. + * This function calculates the number of OUT EPS
  62503. + * using GHWCFG1 and GHWCFG2 registers values
  62504. + *
  62505. + * @param core_if Programming view of the DWC_otg controller
  62506. + */
  62507. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  62508. +{
  62509. + uint32_t num_out_eps = 0;
  62510. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  62511. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  62512. + int i;
  62513. +
  62514. + for (i = 0; i < num_eps; ++i) {
  62515. + if (!(hwcfg1 & 0x1))
  62516. + num_out_eps++;
  62517. +
  62518. + hwcfg1 >>= 2;
  62519. + }
  62520. + return num_out_eps;
  62521. +}
  62522. +
  62523. +/**
  62524. + * This function initializes the DWC_otg controller registers and
  62525. + * prepares the core for device mode or host mode operation.
  62526. + *
  62527. + * @param core_if Programming view of the DWC_otg controller
  62528. + *
  62529. + */
  62530. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  62531. +{
  62532. + int i = 0;
  62533. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  62534. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  62535. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  62536. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  62537. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  62538. +
  62539. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  62540. + core_if, global_regs);
  62541. +
  62542. + /* Common Initialization */
  62543. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  62544. +
  62545. + /* Program the ULPI External VBUS bit if needed */
  62546. + usbcfg.b.ulpi_ext_vbus_drv =
  62547. + (core_if->core_params->phy_ulpi_ext_vbus ==
  62548. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  62549. +
  62550. + /* Set external TS Dline pulsing */
  62551. + usbcfg.b.term_sel_dl_pulse =
  62552. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  62553. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62554. +
  62555. + /* Reset the Controller */
  62556. + dwc_otg_core_reset(core_if);
  62557. +
  62558. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  62559. + core_if->power_down = core_if->core_params->power_down;
  62560. + core_if->otg_sts = 0;
  62561. +
  62562. + /* Initialize parameters from Hardware configuration registers. */
  62563. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  62564. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  62565. +
  62566. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  62567. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  62568. +
  62569. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  62570. + dev_if->perio_tx_fifo_size[i] =
  62571. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  62572. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  62573. + i, dev_if->perio_tx_fifo_size[i]);
  62574. + }
  62575. +
  62576. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62577. + dev_if->tx_fifo_size[i] =
  62578. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  62579. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  62580. + i, dev_if->tx_fifo_size[i]);
  62581. + }
  62582. +
  62583. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  62584. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  62585. + core_if->nperio_tx_fifo_size =
  62586. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  62587. +
  62588. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  62589. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  62590. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  62591. + core_if->nperio_tx_fifo_size);
  62592. +
  62593. + /* This programming sequence needs to happen in FS mode before any other
  62594. + * programming occurs */
  62595. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  62596. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  62597. + /* If FS mode with FS PHY */
  62598. +
  62599. + /* core_init() is now called on every switch so only call the
  62600. + * following for the first time through. */
  62601. + if (!core_if->phy_init_done) {
  62602. + core_if->phy_init_done = 1;
  62603. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  62604. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  62605. + usbcfg.b.physel = 1;
  62606. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62607. +
  62608. + /* Reset after a PHY select */
  62609. + dwc_otg_core_reset(core_if);
  62610. + }
  62611. +
  62612. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  62613. + * do this on HNP Dev/Host mode switches (done in dev_init and
  62614. + * host_init). */
  62615. + if (dwc_otg_is_host_mode(core_if)) {
  62616. + init_fslspclksel(core_if);
  62617. + } else {
  62618. + init_devspd(core_if);
  62619. + }
  62620. +
  62621. + if (core_if->core_params->i2c_enable) {
  62622. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  62623. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  62624. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  62625. + usbcfg.b.otgutmifssel = 1;
  62626. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62627. +
  62628. + /* Program GI2CCTL.I2CEn */
  62629. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  62630. + i2cctl.b.i2cdevaddr = 1;
  62631. + i2cctl.b.i2cen = 0;
  62632. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  62633. + i2cctl.b.i2cen = 1;
  62634. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  62635. + }
  62636. +
  62637. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  62638. + else {
  62639. + /* High speed PHY. */
  62640. + if (!core_if->phy_init_done) {
  62641. + core_if->phy_init_done = 1;
  62642. + /* HS PHY parameters. These parameters are preserved
  62643. + * during soft reset so only program the first time. Do
  62644. + * a soft reset immediately after setting phyif. */
  62645. +
  62646. + if (core_if->core_params->phy_type == 2) {
  62647. + /* ULPI interface */
  62648. + usbcfg.b.ulpi_utmi_sel = 1;
  62649. + usbcfg.b.phyif = 0;
  62650. + usbcfg.b.ddrsel =
  62651. + core_if->core_params->phy_ulpi_ddr;
  62652. + } else if (core_if->core_params->phy_type == 1) {
  62653. + /* UTMI+ interface */
  62654. + usbcfg.b.ulpi_utmi_sel = 0;
  62655. + if (core_if->core_params->phy_utmi_width == 16) {
  62656. + usbcfg.b.phyif = 1;
  62657. +
  62658. + } else {
  62659. + usbcfg.b.phyif = 0;
  62660. + }
  62661. + } else {
  62662. + DWC_ERROR("FS PHY TYPE\n");
  62663. + }
  62664. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62665. + /* Reset after setting the PHY parameters */
  62666. + dwc_otg_core_reset(core_if);
  62667. + }
  62668. + }
  62669. +
  62670. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  62671. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  62672. + (core_if->core_params->ulpi_fs_ls)) {
  62673. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  62674. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  62675. + usbcfg.b.ulpi_fsls = 1;
  62676. + usbcfg.b.ulpi_clk_sus_m = 1;
  62677. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62678. + } else {
  62679. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  62680. + usbcfg.b.ulpi_fsls = 0;
  62681. + usbcfg.b.ulpi_clk_sus_m = 0;
  62682. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62683. + }
  62684. +
  62685. + /* Program the GAHBCFG Register. */
  62686. + switch (core_if->hwcfg2.b.architecture) {
  62687. +
  62688. + case DWC_SLAVE_ONLY_ARCH:
  62689. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  62690. + ahbcfg.b.nptxfemplvl_txfemplvl =
  62691. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  62692. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  62693. + core_if->dma_enable = 0;
  62694. + core_if->dma_desc_enable = 0;
  62695. + break;
  62696. +
  62697. + case DWC_EXT_DMA_ARCH:
  62698. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  62699. + {
  62700. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  62701. + ahbcfg.b.hburstlen = 0;
  62702. + while (brst_sz > 1) {
  62703. + ahbcfg.b.hburstlen++;
  62704. + brst_sz >>= 1;
  62705. + }
  62706. + }
  62707. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  62708. + core_if->dma_desc_enable =
  62709. + (core_if->core_params->dma_desc_enable != 0);
  62710. + break;
  62711. +
  62712. + case DWC_INT_DMA_ARCH:
  62713. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  62714. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  62715. + Host mode ISOC in issue fix - vahrama */
  62716. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  62717. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  62718. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  62719. + core_if->dma_desc_enable =
  62720. + (core_if->core_params->dma_desc_enable != 0);
  62721. + break;
  62722. +
  62723. + }
  62724. + if (core_if->dma_enable) {
  62725. + if (core_if->dma_desc_enable) {
  62726. + DWC_PRINTF("Using Descriptor DMA mode\n");
  62727. + } else {
  62728. + DWC_PRINTF("Using Buffer DMA mode\n");
  62729. +
  62730. + }
  62731. + } else {
  62732. + DWC_PRINTF("Using Slave mode\n");
  62733. + core_if->dma_desc_enable = 0;
  62734. + }
  62735. +
  62736. + if (core_if->core_params->ahb_single) {
  62737. + ahbcfg.b.ahbsingle = 1;
  62738. + }
  62739. +
  62740. + ahbcfg.b.dmaenable = core_if->dma_enable;
  62741. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  62742. +
  62743. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  62744. +
  62745. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  62746. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  62747. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  62748. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  62749. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  62750. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  62751. +
  62752. + /*
  62753. + * Program the GUSBCFG register.
  62754. + */
  62755. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  62756. +
  62757. + switch (core_if->hwcfg2.b.op_mode) {
  62758. + case DWC_MODE_HNP_SRP_CAPABLE:
  62759. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  62760. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  62761. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  62762. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  62763. + break;
  62764. +
  62765. + case DWC_MODE_SRP_ONLY_CAPABLE:
  62766. + usbcfg.b.hnpcap = 0;
  62767. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  62768. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  62769. + break;
  62770. +
  62771. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  62772. + usbcfg.b.hnpcap = 0;
  62773. + usbcfg.b.srpcap = 0;
  62774. + break;
  62775. +
  62776. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  62777. + usbcfg.b.hnpcap = 0;
  62778. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  62779. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  62780. + break;
  62781. +
  62782. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  62783. + usbcfg.b.hnpcap = 0;
  62784. + usbcfg.b.srpcap = 0;
  62785. + break;
  62786. +
  62787. + case DWC_MODE_SRP_CAPABLE_HOST:
  62788. + usbcfg.b.hnpcap = 0;
  62789. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  62790. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  62791. + break;
  62792. +
  62793. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  62794. + usbcfg.b.hnpcap = 0;
  62795. + usbcfg.b.srpcap = 0;
  62796. + break;
  62797. + }
  62798. +
  62799. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  62800. +
  62801. +#ifdef CONFIG_USB_DWC_OTG_LPM
  62802. + if (core_if->core_params->lpm_enable) {
  62803. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  62804. +
  62805. + /* To enable LPM support set lpm_cap_en bit */
  62806. + lpmcfg.b.lpm_cap_en = 1;
  62807. +
  62808. + /* Make AppL1Res ACK */
  62809. + lpmcfg.b.appl_resp = 1;
  62810. +
  62811. + /* Retry 3 times */
  62812. + lpmcfg.b.retry_count = 3;
  62813. +
  62814. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  62815. + 0, lpmcfg.d32);
  62816. +
  62817. + }
  62818. +#endif
  62819. + if (core_if->core_params->ic_usb_cap) {
  62820. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  62821. + gusbcfg.b.ic_usb_cap = 1;
  62822. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  62823. + 0, gusbcfg.d32);
  62824. + }
  62825. + {
  62826. + gotgctl_data_t gotgctl = {.d32 = 0 };
  62827. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  62828. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  62829. + gotgctl.d32);
  62830. + /* Set OTG version supported */
  62831. + core_if->otg_ver = core_if->core_params->otg_ver;
  62832. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  62833. + core_if->core_params->otg_ver, core_if->otg_ver);
  62834. + }
  62835. +
  62836. +
  62837. + /* Enable common interrupts */
  62838. + dwc_otg_enable_common_interrupts(core_if);
  62839. +
  62840. + /* Do device or host intialization based on mode during PCD
  62841. + * and HCD initialization */
  62842. + if (dwc_otg_is_host_mode(core_if)) {
  62843. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  62844. + core_if->op_state = A_HOST;
  62845. + } else {
  62846. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  62847. + core_if->op_state = B_PERIPHERAL;
  62848. +#ifdef DWC_DEVICE_ONLY
  62849. + dwc_otg_core_dev_init(core_if);
  62850. +#endif
  62851. + }
  62852. +}
  62853. +
  62854. +/**
  62855. + * This function enables the Device mode interrupts.
  62856. + *
  62857. + * @param core_if Programming view of DWC_otg controller
  62858. + */
  62859. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  62860. +{
  62861. + gintmsk_data_t intr_mask = {.d32 = 0 };
  62862. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  62863. +
  62864. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  62865. +
  62866. + /* Disable all interrupts. */
  62867. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  62868. +
  62869. + /* Clear any pending interrupts */
  62870. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  62871. +
  62872. + /* Enable the common interrupts */
  62873. + dwc_otg_enable_common_interrupts(core_if);
  62874. +
  62875. + /* Enable interrupts */
  62876. + intr_mask.b.usbreset = 1;
  62877. + intr_mask.b.enumdone = 1;
  62878. + /* Disable Disconnect interrupt in Device mode */
  62879. + intr_mask.b.disconnect = 0;
  62880. +
  62881. + if (!core_if->multiproc_int_enable) {
  62882. + intr_mask.b.inepintr = 1;
  62883. + intr_mask.b.outepintr = 1;
  62884. + }
  62885. +
  62886. + intr_mask.b.erlysuspend = 1;
  62887. +
  62888. + if (core_if->en_multiple_tx_fifo == 0) {
  62889. + intr_mask.b.epmismatch = 1;
  62890. + }
  62891. +
  62892. + //intr_mask.b.incomplisoout = 1;
  62893. + intr_mask.b.incomplisoin = 1;
  62894. +
  62895. +/* Enable the ignore frame number for ISOC xfers - MAS */
  62896. +/* Disable to support high bandwith ISOC transfers - manukz */
  62897. +#if 0
  62898. +#ifdef DWC_UTE_PER_IO
  62899. + if (core_if->dma_enable) {
  62900. + if (core_if->dma_desc_enable) {
  62901. + dctl_data_t dctl1 = {.d32 = 0 };
  62902. + dctl1.b.ifrmnum = 1;
  62903. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  62904. + dctl, 0, dctl1.d32);
  62905. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  62906. + DWC_READ_REG32(&core_if->dev_if->
  62907. + dev_global_regs->dctl));
  62908. + }
  62909. + }
  62910. +#endif
  62911. +#endif
  62912. +#ifdef DWC_EN_ISOC
  62913. + if (core_if->dma_enable) {
  62914. + if (core_if->dma_desc_enable == 0) {
  62915. + if (core_if->pti_enh_enable) {
  62916. + dctl_data_t dctl = {.d32 = 0 };
  62917. + dctl.b.ifrmnum = 1;
  62918. + DWC_MODIFY_REG32(&core_if->
  62919. + dev_if->dev_global_regs->dctl,
  62920. + 0, dctl.d32);
  62921. + } else {
  62922. + intr_mask.b.incomplisoin = 1;
  62923. + intr_mask.b.incomplisoout = 1;
  62924. + }
  62925. + }
  62926. + } else {
  62927. + intr_mask.b.incomplisoin = 1;
  62928. + intr_mask.b.incomplisoout = 1;
  62929. + }
  62930. +#endif /* DWC_EN_ISOC */
  62931. +
  62932. + /** @todo NGS: Should this be a module parameter? */
  62933. +#ifdef USE_PERIODIC_EP
  62934. + intr_mask.b.isooutdrop = 1;
  62935. + intr_mask.b.eopframe = 1;
  62936. + intr_mask.b.incomplisoin = 1;
  62937. + intr_mask.b.incomplisoout = 1;
  62938. +#endif
  62939. +
  62940. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  62941. +
  62942. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  62943. + DWC_READ_REG32(&global_regs->gintmsk));
  62944. +}
  62945. +
  62946. +/**
  62947. + * This function initializes the DWC_otg controller registers for
  62948. + * device mode.
  62949. + *
  62950. + * @param core_if Programming view of DWC_otg controller
  62951. + *
  62952. + */
  62953. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  62954. +{
  62955. + int i;
  62956. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  62957. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  62958. + dwc_otg_core_params_t *params = core_if->core_params;
  62959. + dcfg_data_t dcfg = {.d32 = 0 };
  62960. + depctl_data_t diepctl = {.d32 = 0 };
  62961. + grstctl_t resetctl = {.d32 = 0 };
  62962. + uint32_t rx_fifo_size;
  62963. + fifosize_data_t nptxfifosize;
  62964. + fifosize_data_t txfifosize;
  62965. + dthrctl_data_t dthrctl;
  62966. + fifosize_data_t ptxfifosize;
  62967. + uint16_t rxfsiz, nptxfsiz;
  62968. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  62969. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  62970. +
  62971. + /* Restart the Phy Clock */
  62972. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  62973. +
  62974. + /* Device configuration register */
  62975. + init_devspd(core_if);
  62976. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  62977. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  62978. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  62979. + /* Enable Device OUT NAK in case of DDMA mode*/
  62980. + if (core_if->core_params->dev_out_nak) {
  62981. + dcfg.b.endevoutnak = 1;
  62982. + }
  62983. +
  62984. + if (core_if->core_params->cont_on_bna) {
  62985. + dctl_data_t dctl = {.d32 = 0 };
  62986. + dctl.b.encontonbna = 1;
  62987. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  62988. + }
  62989. +
  62990. +
  62991. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  62992. +
  62993. + /* Configure data FIFO sizes */
  62994. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  62995. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  62996. + core_if->total_fifo_size);
  62997. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  62998. + params->dev_rx_fifo_size);
  62999. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  63000. + params->dev_nperio_tx_fifo_size);
  63001. +
  63002. + /* Rx FIFO */
  63003. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  63004. + DWC_READ_REG32(&global_regs->grxfsiz));
  63005. +
  63006. +#ifdef DWC_UTE_CFI
  63007. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  63008. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  63009. +#endif
  63010. + rx_fifo_size = params->dev_rx_fifo_size;
  63011. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  63012. +
  63013. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  63014. + DWC_READ_REG32(&global_regs->grxfsiz));
  63015. +
  63016. + /** Set Periodic Tx FIFO Mask all bits 0 */
  63017. + core_if->p_tx_msk = 0;
  63018. +
  63019. + /** Set Tx FIFO Mask all bits 0 */
  63020. + core_if->tx_msk = 0;
  63021. +
  63022. + if (core_if->en_multiple_tx_fifo == 0) {
  63023. + /* Non-periodic Tx FIFO */
  63024. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  63025. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63026. +
  63027. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  63028. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  63029. +
  63030. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  63031. + nptxfifosize.d32);
  63032. +
  63033. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  63034. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63035. +
  63036. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  63037. + /*
  63038. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  63039. + * Indexes of the FIFO size module parameters in the
  63040. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  63041. + * the dptxfsiz array run from 0 to 14.
  63042. + */
  63043. + /** @todo Finish debug of this */
  63044. + ptxfifosize.b.startaddr =
  63045. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  63046. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  63047. + ptxfifosize.b.depth =
  63048. + params->dev_perio_tx_fifo_size[i];
  63049. + DWC_DEBUGPL(DBG_CIL,
  63050. + "initial dtxfsiz[%d]=%08x\n", i,
  63051. + DWC_READ_REG32(&global_regs->dtxfsiz
  63052. + [i]));
  63053. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  63054. + ptxfifosize.d32);
  63055. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  63056. + i,
  63057. + DWC_READ_REG32(&global_regs->dtxfsiz
  63058. + [i]));
  63059. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  63060. + }
  63061. + } else {
  63062. + /*
  63063. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  63064. + * Indexes of the FIFO size module parameters in the
  63065. + * dev_tx_fifo_size array and the FIFO size registers in
  63066. + * the dtxfsiz array run from 0 to 14.
  63067. + */
  63068. +
  63069. + /* Non-periodic Tx FIFO */
  63070. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  63071. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63072. +
  63073. +#ifdef DWC_UTE_CFI
  63074. + core_if->pwron_gnptxfsiz =
  63075. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  63076. + core_if->init_gnptxfsiz =
  63077. + params->dev_nperio_tx_fifo_size;
  63078. +#endif
  63079. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  63080. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  63081. +
  63082. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  63083. + nptxfifosize.d32);
  63084. +
  63085. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  63086. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63087. +
  63088. + txfifosize.b.startaddr =
  63089. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  63090. +
  63091. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  63092. +
  63093. + txfifosize.b.depth =
  63094. + params->dev_tx_fifo_size[i];
  63095. +
  63096. + DWC_DEBUGPL(DBG_CIL,
  63097. + "initial dtxfsiz[%d]=%08x\n",
  63098. + i,
  63099. + DWC_READ_REG32(&global_regs->dtxfsiz
  63100. + [i]));
  63101. +
  63102. +#ifdef DWC_UTE_CFI
  63103. + core_if->pwron_txfsiz[i] =
  63104. + (DWC_READ_REG32
  63105. + (&global_regs->dtxfsiz[i]) >> 16);
  63106. + core_if->init_txfsiz[i] =
  63107. + params->dev_tx_fifo_size[i];
  63108. +#endif
  63109. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  63110. + txfifosize.d32);
  63111. +
  63112. + DWC_DEBUGPL(DBG_CIL,
  63113. + "new dtxfsiz[%d]=%08x\n",
  63114. + i,
  63115. + DWC_READ_REG32(&global_regs->dtxfsiz
  63116. + [i]));
  63117. +
  63118. + txfifosize.b.startaddr += txfifosize.b.depth;
  63119. + }
  63120. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  63121. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  63122. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  63123. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  63124. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  63125. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  63126. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  63127. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  63128. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  63129. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  63130. + }
  63131. + }
  63132. +
  63133. + /* Flush the FIFOs */
  63134. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  63135. + dwc_otg_flush_rx_fifo(core_if);
  63136. +
  63137. + /* Flush the Learning Queue. */
  63138. + resetctl.b.intknqflsh = 1;
  63139. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  63140. +
  63141. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  63142. + core_if->start_predict = 0;
  63143. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  63144. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  63145. + }
  63146. + core_if->nextep_seq[0] = 0;
  63147. + core_if->first_in_nextep_seq = 0;
  63148. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  63149. + diepctl.b.nextep = 0;
  63150. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  63151. +
  63152. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  63153. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  63154. + dcfg.b.epmscnt = 2;
  63155. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  63156. +
  63157. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  63158. + __func__, core_if->first_in_nextep_seq);
  63159. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  63160. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  63161. + }
  63162. + DWC_DEBUGPL(DBG_CILV,"\n");
  63163. + }
  63164. +
  63165. + /* Clear all pending Device Interrupts */
  63166. + /** @todo - if the condition needed to be checked
  63167. + * or in any case all pending interrutps should be cleared?
  63168. + */
  63169. + if (core_if->multiproc_int_enable) {
  63170. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  63171. + DWC_WRITE_REG32(&dev_if->
  63172. + dev_global_regs->diepeachintmsk[i], 0);
  63173. + }
  63174. + }
  63175. +
  63176. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  63177. + DWC_WRITE_REG32(&dev_if->
  63178. + dev_global_regs->doepeachintmsk[i], 0);
  63179. + }
  63180. +
  63181. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  63182. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  63183. + } else {
  63184. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  63185. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  63186. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  63187. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  63188. + }
  63189. +
  63190. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  63191. + depctl_data_t depctl;
  63192. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  63193. + if (depctl.b.epena) {
  63194. + depctl.d32 = 0;
  63195. + depctl.b.epdis = 1;
  63196. + depctl.b.snak = 1;
  63197. + } else {
  63198. + depctl.d32 = 0;
  63199. + }
  63200. +
  63201. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  63202. +
  63203. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  63204. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  63205. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  63206. + }
  63207. +
  63208. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  63209. + depctl_data_t depctl;
  63210. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  63211. + if (depctl.b.epena) {
  63212. + dctl_data_t dctl = {.d32 = 0 };
  63213. + gintmsk_data_t gintsts = {.d32 = 0 };
  63214. + doepint_data_t doepint = {.d32 = 0 };
  63215. + dctl.b.sgoutnak = 1;
  63216. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  63217. + do {
  63218. + dwc_udelay(10);
  63219. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  63220. + } while (!gintsts.b.goutnakeff);
  63221. + gintsts.d32 = 0;
  63222. + gintsts.b.goutnakeff = 1;
  63223. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  63224. +
  63225. + depctl.d32 = 0;
  63226. + depctl.b.epdis = 1;
  63227. + depctl.b.snak = 1;
  63228. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  63229. + do {
  63230. + dwc_udelay(10);
  63231. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  63232. + out_ep_regs[i]->doepint);
  63233. + } while (!doepint.b.epdisabled);
  63234. +
  63235. + doepint.b.epdisabled = 1;
  63236. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  63237. +
  63238. + dctl.d32 = 0;
  63239. + dctl.b.cgoutnak = 1;
  63240. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  63241. + } else {
  63242. + depctl.d32 = 0;
  63243. + }
  63244. +
  63245. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  63246. +
  63247. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  63248. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  63249. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  63250. + }
  63251. +
  63252. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  63253. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  63254. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  63255. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  63256. +
  63257. + dev_if->rx_thr_length = params->rx_thr_length;
  63258. + dev_if->tx_thr_length = params->tx_thr_length;
  63259. +
  63260. + dev_if->setup_desc_index = 0;
  63261. +
  63262. + dthrctl.d32 = 0;
  63263. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  63264. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  63265. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  63266. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  63267. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  63268. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  63269. +
  63270. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  63271. + dthrctl.d32);
  63272. +
  63273. + DWC_DEBUGPL(DBG_CIL,
  63274. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  63275. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  63276. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  63277. + dthrctl.b.rx_thr_len);
  63278. +
  63279. + }
  63280. +
  63281. + dwc_otg_enable_device_interrupts(core_if);
  63282. +
  63283. + {
  63284. + diepmsk_data_t msk = {.d32 = 0 };
  63285. + msk.b.txfifoundrn = 1;
  63286. + if (core_if->multiproc_int_enable) {
  63287. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  63288. + diepeachintmsk[0], msk.d32, msk.d32);
  63289. + } else {
  63290. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  63291. + msk.d32, msk.d32);
  63292. + }
  63293. + }
  63294. +
  63295. + if (core_if->multiproc_int_enable) {
  63296. + /* Set NAK on Babble */
  63297. + dctl_data_t dctl = {.d32 = 0 };
  63298. + dctl.b.nakonbble = 1;
  63299. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  63300. + }
  63301. +
  63302. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  63303. + dctl_data_t dctl = {.d32 = 0 };
  63304. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  63305. + dctl.b.sftdiscon = 0;
  63306. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  63307. + }
  63308. +}
  63309. +
  63310. +/**
  63311. + * This function enables the Host mode interrupts.
  63312. + *
  63313. + * @param core_if Programming view of DWC_otg controller
  63314. + */
  63315. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  63316. +{
  63317. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63318. + gintmsk_data_t intr_mask = {.d32 = 0 };
  63319. +
  63320. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  63321. +
  63322. + /* Disable all interrupts. */
  63323. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  63324. +
  63325. + /* Clear any pending interrupts. */
  63326. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  63327. +
  63328. + /* Enable the common interrupts */
  63329. + dwc_otg_enable_common_interrupts(core_if);
  63330. +
  63331. + /*
  63332. + * Enable host mode interrupts without disturbing common
  63333. + * interrupts.
  63334. + */
  63335. +
  63336. + intr_mask.b.disconnect = 1;
  63337. + intr_mask.b.portintr = 1;
  63338. + intr_mask.b.hcintr = 1;
  63339. +
  63340. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  63341. +}
  63342. +
  63343. +/**
  63344. + * This function disables the Host Mode interrupts.
  63345. + *
  63346. + * @param core_if Programming view of DWC_otg controller
  63347. + */
  63348. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  63349. +{
  63350. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63351. + gintmsk_data_t intr_mask = {.d32 = 0 };
  63352. +
  63353. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  63354. +
  63355. + /*
  63356. + * Disable host mode interrupts without disturbing common
  63357. + * interrupts.
  63358. + */
  63359. + intr_mask.b.sofintr = 1;
  63360. + intr_mask.b.portintr = 1;
  63361. + intr_mask.b.hcintr = 1;
  63362. + intr_mask.b.ptxfempty = 1;
  63363. + intr_mask.b.nptxfempty = 1;
  63364. +
  63365. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  63366. +}
  63367. +
  63368. +/**
  63369. + * This function initializes the DWC_otg controller registers for
  63370. + * host mode.
  63371. + *
  63372. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  63373. + * request queues. Host channels are reset to ensure that they are ready for
  63374. + * performing transfers.
  63375. + *
  63376. + * @param core_if Programming view of DWC_otg controller
  63377. + *
  63378. + */
  63379. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  63380. +{
  63381. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63382. + dwc_otg_host_if_t *host_if = core_if->host_if;
  63383. + dwc_otg_core_params_t *params = core_if->core_params;
  63384. + hprt0_data_t hprt0 = {.d32 = 0 };
  63385. + fifosize_data_t nptxfifosize;
  63386. + fifosize_data_t ptxfifosize;
  63387. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  63388. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  63389. + int i;
  63390. + hcchar_data_t hcchar;
  63391. + hcfg_data_t hcfg;
  63392. + hfir_data_t hfir;
  63393. + dwc_otg_hc_regs_t *hc_regs;
  63394. + int num_channels;
  63395. + gotgctl_data_t gotgctl = {.d32 = 0 };
  63396. +
  63397. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  63398. +
  63399. + /* Restart the Phy Clock */
  63400. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  63401. +
  63402. + /* Initialize Host Configuration Register */
  63403. + init_fslspclksel(core_if);
  63404. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  63405. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  63406. + hcfg.b.fslssupp = 1;
  63407. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  63408. +
  63409. + }
  63410. +
  63411. + /* This bit allows dynamic reloading of the HFIR register
  63412. + * during runtime. This bit needs to be programmed during
  63413. + * initial configuration and its value must not be changed
  63414. + * during runtime.*/
  63415. + if (core_if->core_params->reload_ctl == 1) {
  63416. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  63417. + hfir.b.hfirrldctrl = 1;
  63418. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  63419. + }
  63420. +
  63421. + if (core_if->core_params->dma_desc_enable) {
  63422. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  63423. + if (!
  63424. + (core_if->hwcfg4.b.desc_dma
  63425. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  63426. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  63427. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  63428. + || (op_mode ==
  63429. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  63430. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  63431. + || (op_mode ==
  63432. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  63433. +
  63434. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  63435. + "Either core version is below 2.90a or "
  63436. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  63437. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  63438. + "module parameter to 0.\n");
  63439. + return;
  63440. + }
  63441. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  63442. + hcfg.b.descdma = 1;
  63443. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  63444. + }
  63445. +
  63446. + /* Configure data FIFO sizes */
  63447. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  63448. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  63449. + core_if->total_fifo_size);
  63450. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  63451. + params->host_rx_fifo_size);
  63452. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  63453. + params->host_nperio_tx_fifo_size);
  63454. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  63455. + params->host_perio_tx_fifo_size);
  63456. +
  63457. + /* Rx FIFO */
  63458. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  63459. + DWC_READ_REG32(&global_regs->grxfsiz));
  63460. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  63461. + params->host_rx_fifo_size);
  63462. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  63463. + DWC_READ_REG32(&global_regs->grxfsiz));
  63464. +
  63465. + /* Non-periodic Tx FIFO */
  63466. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  63467. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63468. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  63469. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  63470. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  63471. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  63472. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63473. +
  63474. + /* Periodic Tx FIFO */
  63475. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  63476. + DWC_READ_REG32(&global_regs->hptxfsiz));
  63477. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  63478. + ptxfifosize.b.startaddr =
  63479. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  63480. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  63481. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  63482. + DWC_READ_REG32(&global_regs->hptxfsiz));
  63483. +
  63484. + if (core_if->en_multiple_tx_fifo
  63485. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  63486. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  63487. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  63488. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  63489. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  63490. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  63491. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  63492. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  63493. + }
  63494. + }
  63495. +
  63496. + /* TODO - check this */
  63497. + /* Clear Host Set HNP Enable in the OTG Control Register */
  63498. + gotgctl.b.hstsethnpen = 1;
  63499. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  63500. + /* Make sure the FIFOs are flushed. */
  63501. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  63502. + dwc_otg_flush_rx_fifo(core_if);
  63503. +
  63504. + /* Clear Host Set HNP Enable in the OTG Control Register */
  63505. + gotgctl.b.hstsethnpen = 1;
  63506. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  63507. +
  63508. + if (!core_if->core_params->dma_desc_enable) {
  63509. + /* Flush out any leftover queued requests. */
  63510. + num_channels = core_if->core_params->host_channels;
  63511. +
  63512. + for (i = 0; i < num_channels; i++) {
  63513. + hc_regs = core_if->host_if->hc_regs[i];
  63514. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  63515. + hcchar.b.chen = 0;
  63516. + hcchar.b.chdis = 1;
  63517. + hcchar.b.epdir = 0;
  63518. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  63519. + }
  63520. +
  63521. + /* Halt all channels to put them into a known state. */
  63522. + for (i = 0; i < num_channels; i++) {
  63523. + int count = 0;
  63524. + hc_regs = core_if->host_if->hc_regs[i];
  63525. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  63526. + hcchar.b.chen = 1;
  63527. + hcchar.b.chdis = 1;
  63528. + hcchar.b.epdir = 0;
  63529. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  63530. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  63531. + do {
  63532. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  63533. + if (++count > 1000) {
  63534. + DWC_ERROR
  63535. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  63536. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  63537. + break;
  63538. + }
  63539. + dwc_udelay(1);
  63540. + } while (hcchar.b.chen);
  63541. + }
  63542. + }
  63543. +
  63544. + /* Turn on the vbus power. */
  63545. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  63546. + if (core_if->op_state == A_HOST) {
  63547. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  63548. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  63549. + if (hprt0.b.prtpwr == 0) {
  63550. + hprt0.b.prtpwr = 1;
  63551. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  63552. + }
  63553. + }
  63554. +
  63555. + dwc_otg_enable_host_interrupts(core_if);
  63556. +}
  63557. +
  63558. +/**
  63559. + * Prepares a host channel for transferring packets to/from a specific
  63560. + * endpoint. The HCCHARn register is set up with the characteristics specified
  63561. + * in _hc. Host channel interrupts that may need to be serviced while this
  63562. + * transfer is in progress are enabled.
  63563. + *
  63564. + * @param core_if Programming view of DWC_otg controller
  63565. + * @param hc Information needed to initialize the host channel
  63566. + */
  63567. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  63568. +{
  63569. + uint32_t intr_enable;
  63570. + hcintmsk_data_t hc_intr_mask;
  63571. + gintmsk_data_t gintmsk = {.d32 = 0 };
  63572. + hcchar_data_t hcchar;
  63573. + hcsplt_data_t hcsplt;
  63574. +
  63575. + uint8_t hc_num = hc->hc_num;
  63576. + dwc_otg_host_if_t *host_if = core_if->host_if;
  63577. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  63578. +
  63579. + /* Clear old interrupt conditions for this host channel. */
  63580. + hc_intr_mask.d32 = 0xFFFFFFFF;
  63581. + hc_intr_mask.b.reserved14_31 = 0;
  63582. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  63583. +
  63584. + /* Enable channel interrupts required for this transfer. */
  63585. + hc_intr_mask.d32 = 0;
  63586. + hc_intr_mask.b.chhltd = 1;
  63587. + if (core_if->dma_enable) {
  63588. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  63589. + if (!core_if->dma_desc_enable)
  63590. + hc_intr_mask.b.ahberr = 1;
  63591. + else {
  63592. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  63593. + hc_intr_mask.b.xfercompl = 1;
  63594. + }
  63595. +
  63596. + if (hc->error_state && !hc->do_split &&
  63597. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  63598. + hc_intr_mask.b.ack = 1;
  63599. + if (hc->ep_is_in) {
  63600. + hc_intr_mask.b.datatglerr = 1;
  63601. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  63602. + hc_intr_mask.b.nak = 1;
  63603. + }
  63604. + }
  63605. + }
  63606. + } else {
  63607. + switch (hc->ep_type) {
  63608. + case DWC_OTG_EP_TYPE_CONTROL:
  63609. + case DWC_OTG_EP_TYPE_BULK:
  63610. + hc_intr_mask.b.xfercompl = 1;
  63611. + hc_intr_mask.b.stall = 1;
  63612. + hc_intr_mask.b.xacterr = 1;
  63613. + hc_intr_mask.b.datatglerr = 1;
  63614. + if (hc->ep_is_in) {
  63615. + hc_intr_mask.b.bblerr = 1;
  63616. + } else {
  63617. + hc_intr_mask.b.nak = 1;
  63618. + hc_intr_mask.b.nyet = 1;
  63619. + if (hc->do_ping) {
  63620. + hc_intr_mask.b.ack = 1;
  63621. + }
  63622. + }
  63623. +
  63624. + if (hc->do_split) {
  63625. + hc_intr_mask.b.nak = 1;
  63626. + if (hc->complete_split) {
  63627. + hc_intr_mask.b.nyet = 1;
  63628. + } else {
  63629. + hc_intr_mask.b.ack = 1;
  63630. + }
  63631. + }
  63632. +
  63633. + if (hc->error_state) {
  63634. + hc_intr_mask.b.ack = 1;
  63635. + }
  63636. + break;
  63637. + case DWC_OTG_EP_TYPE_INTR:
  63638. + hc_intr_mask.b.xfercompl = 1;
  63639. + hc_intr_mask.b.nak = 1;
  63640. + hc_intr_mask.b.stall = 1;
  63641. + hc_intr_mask.b.xacterr = 1;
  63642. + hc_intr_mask.b.datatglerr = 1;
  63643. + hc_intr_mask.b.frmovrun = 1;
  63644. +
  63645. + if (hc->ep_is_in) {
  63646. + hc_intr_mask.b.bblerr = 1;
  63647. + }
  63648. + if (hc->error_state) {
  63649. + hc_intr_mask.b.ack = 1;
  63650. + }
  63651. + if (hc->do_split) {
  63652. + if (hc->complete_split) {
  63653. + hc_intr_mask.b.nyet = 1;
  63654. + } else {
  63655. + hc_intr_mask.b.ack = 1;
  63656. + }
  63657. + }
  63658. + break;
  63659. + case DWC_OTG_EP_TYPE_ISOC:
  63660. + hc_intr_mask.b.xfercompl = 1;
  63661. + hc_intr_mask.b.frmovrun = 1;
  63662. + hc_intr_mask.b.ack = 1;
  63663. +
  63664. + if (hc->ep_is_in) {
  63665. + hc_intr_mask.b.xacterr = 1;
  63666. + hc_intr_mask.b.bblerr = 1;
  63667. + }
  63668. + break;
  63669. + }
  63670. + }
  63671. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  63672. +
  63673. + /* Enable the top level host channel interrupt. */
  63674. + intr_enable = (1 << hc_num);
  63675. + DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  63676. +
  63677. + /* Make sure host channel interrupts are enabled. */
  63678. + gintmsk.b.hcintr = 1;
  63679. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  63680. +
  63681. + /*
  63682. + * Program the HCCHARn register with the endpoint characteristics for
  63683. + * the current transfer.
  63684. + */
  63685. + hcchar.d32 = 0;
  63686. + hcchar.b.devaddr = hc->dev_addr;
  63687. + hcchar.b.epnum = hc->ep_num;
  63688. + hcchar.b.epdir = hc->ep_is_in;
  63689. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  63690. + hcchar.b.eptype = hc->ep_type;
  63691. + hcchar.b.mps = hc->max_packet;
  63692. +
  63693. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  63694. +
  63695. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  63696. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  63697. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  63698. + "Max Pkt %d, Multi Cnt %d\n",
  63699. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  63700. + hcchar.b.mps, hcchar.b.multicnt);
  63701. +
  63702. + /*
  63703. + * Program the HCSPLIT register for SPLITs
  63704. + */
  63705. + hcsplt.d32 = 0;
  63706. + if (hc->do_split) {
  63707. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  63708. + hc->hc_num,
  63709. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  63710. + hcsplt.b.compsplt = hc->complete_split;
  63711. + hcsplt.b.xactpos = hc->xact_pos;
  63712. + hcsplt.b.hubaddr = hc->hub_addr;
  63713. + hcsplt.b.prtaddr = hc->port_addr;
  63714. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  63715. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  63716. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  63717. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  63718. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  63719. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  63720. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  63721. + }
  63722. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  63723. +
  63724. +}
  63725. +
  63726. +/**
  63727. + * Attempts to halt a host channel. This function should only be called in
  63728. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  63729. + * normal circumstances in DMA mode, the controller halts the channel when the
  63730. + * transfer is complete or a condition occurs that requires application
  63731. + * intervention.
  63732. + *
  63733. + * In slave mode, checks for a free request queue entry, then sets the Channel
  63734. + * Enable and Channel Disable bits of the Host Channel Characteristics
  63735. + * register of the specified channel to intiate the halt. If there is no free
  63736. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  63737. + * register to flush requests for this channel. In the latter case, sets a
  63738. + * flag to indicate that the host channel needs to be halted when a request
  63739. + * queue slot is open.
  63740. + *
  63741. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  63742. + * HCCHARn register. The controller ensures there is space in the request
  63743. + * queue before submitting the halt request.
  63744. + *
  63745. + * Some time may elapse before the core flushes any posted requests for this
  63746. + * host channel and halts. The Channel Halted interrupt handler completes the
  63747. + * deactivation of the host channel.
  63748. + *
  63749. + * @param core_if Controller register interface.
  63750. + * @param hc Host channel to halt.
  63751. + * @param halt_status Reason for halting the channel.
  63752. + */
  63753. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  63754. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  63755. +{
  63756. + gnptxsts_data_t nptxsts;
  63757. + hptxsts_data_t hptxsts;
  63758. + hcchar_data_t hcchar;
  63759. + dwc_otg_hc_regs_t *hc_regs;
  63760. + dwc_otg_core_global_regs_t *global_regs;
  63761. + dwc_otg_host_global_regs_t *host_global_regs;
  63762. +
  63763. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  63764. + global_regs = core_if->core_global_regs;
  63765. + host_global_regs = core_if->host_if->host_global_regs;
  63766. +
  63767. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  63768. + "halt_status = %d\n", halt_status);
  63769. +
  63770. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  63771. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  63772. + /*
  63773. + * Disable all channel interrupts except Ch Halted. The QTD
  63774. + * and QH state associated with this transfer has been cleared
  63775. + * (in the case of URB_DEQUEUE), so the channel needs to be
  63776. + * shut down carefully to prevent crashes.
  63777. + */
  63778. + hcintmsk_data_t hcintmsk;
  63779. + hcintmsk.d32 = 0;
  63780. + hcintmsk.b.chhltd = 1;
  63781. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  63782. +
  63783. + /*
  63784. + * Make sure no other interrupts besides halt are currently
  63785. + * pending. Handling another interrupt could cause a crash due
  63786. + * to the QTD and QH state.
  63787. + */
  63788. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  63789. +
  63790. + /*
  63791. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  63792. + * even if the channel was already halted for some other
  63793. + * reason.
  63794. + */
  63795. + hc->halt_status = halt_status;
  63796. +
  63797. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  63798. + if (hcchar.b.chen == 0) {
  63799. + /*
  63800. + * The channel is either already halted or it hasn't
  63801. + * started yet. In DMA mode, the transfer may halt if
  63802. + * it finishes normally or a condition occurs that
  63803. + * requires driver intervention. Don't want to halt
  63804. + * the channel again. In either Slave or DMA mode,
  63805. + * it's possible that the transfer has been assigned
  63806. + * to a channel, but not started yet when an URB is
  63807. + * dequeued. Don't want to halt a channel that hasn't
  63808. + * started yet.
  63809. + */
  63810. + return;
  63811. + }
  63812. + }
  63813. + if (hc->halt_pending) {
  63814. + /*
  63815. + * A halt has already been issued for this channel. This might
  63816. + * happen when a transfer is aborted by a higher level in
  63817. + * the stack.
  63818. + */
  63819. +#ifdef DEBUG
  63820. + DWC_PRINTF
  63821. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  63822. + __func__, hc->hc_num);
  63823. +
  63824. +#endif
  63825. + return;
  63826. + }
  63827. +
  63828. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  63829. +
  63830. + /* No need to set the bit in DDMA for disabling the channel */
  63831. + //TODO check it everywhere channel is disabled
  63832. + if (!core_if->core_params->dma_desc_enable)
  63833. + hcchar.b.chen = 1;
  63834. + hcchar.b.chdis = 1;
  63835. +
  63836. + if (!core_if->dma_enable) {
  63837. + /* Check for space in the request queue to issue the halt. */
  63838. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  63839. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  63840. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  63841. + if (nptxsts.b.nptxqspcavail == 0) {
  63842. + hcchar.b.chen = 0;
  63843. + }
  63844. + } else {
  63845. + hptxsts.d32 =
  63846. + DWC_READ_REG32(&host_global_regs->hptxsts);
  63847. + if ((hptxsts.b.ptxqspcavail == 0)
  63848. + || (core_if->queuing_high_bandwidth)) {
  63849. + hcchar.b.chen = 0;
  63850. + }
  63851. + }
  63852. + }
  63853. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  63854. +
  63855. + hc->halt_status = halt_status;
  63856. +
  63857. + if (hcchar.b.chen) {
  63858. + hc->halt_pending = 1;
  63859. + hc->halt_on_queue = 0;
  63860. + } else {
  63861. + hc->halt_on_queue = 1;
  63862. + }
  63863. +
  63864. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  63865. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  63866. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  63867. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  63868. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  63869. +
  63870. + return;
  63871. +}
  63872. +
  63873. +/**
  63874. + * Clears the transfer state for a host channel. This function is normally
  63875. + * called after a transfer is done and the host channel is being released.
  63876. + *
  63877. + * @param core_if Programming view of DWC_otg controller.
  63878. + * @param hc Identifies the host channel to clean up.
  63879. + */
  63880. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  63881. +{
  63882. + dwc_otg_hc_regs_t *hc_regs;
  63883. +
  63884. + hc->xfer_started = 0;
  63885. +
  63886. + /*
  63887. + * Clear channel interrupt enables and any unhandled channel interrupt
  63888. + * conditions.
  63889. + */
  63890. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  63891. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  63892. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  63893. +#ifdef DEBUG
  63894. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  63895. +#endif
  63896. +}
  63897. +
  63898. +/**
  63899. + * Sets the channel property that indicates in which frame a periodic transfer
  63900. + * should occur. This is always set to the _next_ frame. This function has no
  63901. + * effect on non-periodic transfers.
  63902. + *
  63903. + * @param core_if Programming view of DWC_otg controller.
  63904. + * @param hc Identifies the host channel to set up and its properties.
  63905. + * @param hcchar Current value of the HCCHAR register for the specified host
  63906. + * channel.
  63907. + */
  63908. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  63909. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  63910. +{
  63911. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  63912. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  63913. + hfnum_data_t hfnum;
  63914. + hfnum.d32 =
  63915. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  63916. +
  63917. + /* 1 if _next_ frame is odd, 0 if it's even */
  63918. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  63919. +#ifdef DEBUG
  63920. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  63921. + && !hc->complete_split) {
  63922. + switch (hfnum.b.frnum & 0x7) {
  63923. + case 7:
  63924. + core_if->hfnum_7_samples++;
  63925. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  63926. + break;
  63927. + case 0:
  63928. + core_if->hfnum_0_samples++;
  63929. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  63930. + break;
  63931. + default:
  63932. + core_if->hfnum_other_samples++;
  63933. + core_if->hfnum_other_frrem_accum +=
  63934. + hfnum.b.frrem;
  63935. + break;
  63936. + }
  63937. + }
  63938. +#endif
  63939. + }
  63940. +}
  63941. +
  63942. +#ifdef DEBUG
  63943. +void hc_xfer_timeout(void *ptr)
  63944. +{
  63945. + hc_xfer_info_t *xfer_info = NULL;
  63946. + int hc_num = 0;
  63947. +
  63948. + if (ptr)
  63949. + xfer_info = (hc_xfer_info_t *) ptr;
  63950. +
  63951. + if (!xfer_info->hc) {
  63952. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  63953. + return;
  63954. + }
  63955. +
  63956. + hc_num = xfer_info->hc->hc_num;
  63957. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  63958. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  63959. + xfer_info->core_if->start_hcchar_val[hc_num]);
  63960. +}
  63961. +#endif
  63962. +
  63963. +void ep_xfer_timeout(void *ptr)
  63964. +{
  63965. + ep_xfer_info_t *xfer_info = NULL;
  63966. + int ep_num = 0;
  63967. + dctl_data_t dctl = {.d32 = 0 };
  63968. + gintsts_data_t gintsts = {.d32 = 0 };
  63969. + gintmsk_data_t gintmsk = {.d32 = 0 };
  63970. +
  63971. + if (ptr)
  63972. + xfer_info = (ep_xfer_info_t *) ptr;
  63973. +
  63974. + if (!xfer_info->ep) {
  63975. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  63976. + return;
  63977. + }
  63978. +
  63979. + ep_num = xfer_info->ep->num;
  63980. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  63981. + /* Put the sate to 2 as it was time outed */
  63982. + xfer_info->state = 2;
  63983. +
  63984. + dctl.d32 =
  63985. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  63986. + gintsts.d32 =
  63987. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  63988. + gintmsk.d32 =
  63989. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  63990. +
  63991. + if (!gintmsk.b.goutnakeff) {
  63992. + /* Unmask it */
  63993. + gintmsk.b.goutnakeff = 1;
  63994. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  63995. + gintmsk.d32);
  63996. +
  63997. + }
  63998. +
  63999. + if (!gintsts.b.goutnakeff) {
  64000. + dctl.b.sgoutnak = 1;
  64001. + }
  64002. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  64003. + dctl.d32);
  64004. +
  64005. +}
  64006. +
  64007. +void set_pid_isoc(dwc_hc_t * hc)
  64008. +{
  64009. + /* Set up the initial PID for the transfer. */
  64010. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  64011. + if (hc->ep_is_in) {
  64012. + if (hc->multi_count == 1) {
  64013. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  64014. + } else if (hc->multi_count == 2) {
  64015. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  64016. + } else {
  64017. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  64018. + }
  64019. + } else {
  64020. + if (hc->multi_count == 1) {
  64021. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  64022. + } else {
  64023. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  64024. + }
  64025. + }
  64026. + } else {
  64027. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  64028. + }
  64029. +}
  64030. +
  64031. +/**
  64032. + * This function does the setup for a data transfer for a host channel and
  64033. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  64034. + * Slave mode, the caller must ensure that there is sufficient space in the
  64035. + * request queue and Tx Data FIFO.
  64036. + *
  64037. + * For an OUT transfer in Slave mode, it loads a data packet into the
  64038. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  64039. + * the Host ISR.
  64040. + *
  64041. + * For an IN transfer in Slave mode, a data packet is requested. The data
  64042. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  64043. + * additional data packets are requested in the Host ISR.
  64044. + *
  64045. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  64046. + * register along with a packet count of 1 and the channel is enabled. This
  64047. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  64048. + * simply set to 0 since no data transfer occurs in this case.
  64049. + *
  64050. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  64051. + * all the information required to perform the subsequent data transfer. In
  64052. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  64053. + * controller performs the entire PING protocol, then starts the data
  64054. + * transfer.
  64055. + *
  64056. + * @param core_if Programming view of DWC_otg controller.
  64057. + * @param hc Information needed to initialize the host channel. The xfer_len
  64058. + * value may be reduced to accommodate the max widths of the XferSize and
  64059. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  64060. + * to reflect the final xfer_len value.
  64061. + */
  64062. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64063. +{
  64064. + hcchar_data_t hcchar;
  64065. + hctsiz_data_t hctsiz;
  64066. + uint16_t num_packets;
  64067. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  64068. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  64069. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  64070. +
  64071. + hctsiz.d32 = 0;
  64072. +
  64073. + if (hc->do_ping) {
  64074. + if (!core_if->dma_enable) {
  64075. + dwc_otg_hc_do_ping(core_if, hc);
  64076. + hc->xfer_started = 1;
  64077. + return;
  64078. + } else {
  64079. + hctsiz.b.dopng = 1;
  64080. + }
  64081. + }
  64082. +
  64083. + if (hc->do_split) {
  64084. + num_packets = 1;
  64085. +
  64086. + if (hc->complete_split && !hc->ep_is_in) {
  64087. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  64088. + * core doesn't expect any data written to the FIFO */
  64089. + hc->xfer_len = 0;
  64090. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  64091. + hc->xfer_len = hc->max_packet;
  64092. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  64093. + hc->xfer_len = 188;
  64094. + }
  64095. +
  64096. + hctsiz.b.xfersize = hc->xfer_len;
  64097. + } else {
  64098. + /*
  64099. + * Ensure that the transfer length and packet count will fit
  64100. + * in the widths allocated for them in the HCTSIZn register.
  64101. + */
  64102. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  64103. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  64104. + /*
  64105. + * Make sure the transfer size is no larger than one
  64106. + * (micro)frame's worth of data. (A check was done
  64107. + * when the periodic transfer was accepted to ensure
  64108. + * that a (micro)frame's worth of data can be
  64109. + * programmed into a channel.)
  64110. + */
  64111. + uint32_t max_periodic_len =
  64112. + hc->multi_count * hc->max_packet;
  64113. + if (hc->xfer_len > max_periodic_len) {
  64114. + hc->xfer_len = max_periodic_len;
  64115. + } else {
  64116. + }
  64117. + } else if (hc->xfer_len > max_hc_xfer_size) {
  64118. + /* Make sure that xfer_len is a multiple of max packet size. */
  64119. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  64120. + }
  64121. +
  64122. + if (hc->xfer_len > 0) {
  64123. + num_packets =
  64124. + (hc->xfer_len + hc->max_packet -
  64125. + 1) / hc->max_packet;
  64126. + if (num_packets > max_hc_pkt_count) {
  64127. + num_packets = max_hc_pkt_count;
  64128. + hc->xfer_len = num_packets * hc->max_packet;
  64129. + }
  64130. + } else {
  64131. + /* Need 1 packet for transfer length of 0. */
  64132. + num_packets = 1;
  64133. + }
  64134. +
  64135. + if (hc->ep_is_in) {
  64136. + /* Always program an integral # of max packets for IN transfers. */
  64137. + hc->xfer_len = num_packets * hc->max_packet;
  64138. + }
  64139. +
  64140. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  64141. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  64142. + /*
  64143. + * Make sure that the multi_count field matches the
  64144. + * actual transfer length.
  64145. + */
  64146. + hc->multi_count = num_packets;
  64147. + }
  64148. +
  64149. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  64150. + set_pid_isoc(hc);
  64151. +
  64152. + hctsiz.b.xfersize = hc->xfer_len;
  64153. + }
  64154. +
  64155. + hc->start_pkt_count = num_packets;
  64156. + hctsiz.b.pktcnt = num_packets;
  64157. + hctsiz.b.pid = hc->data_pid_start;
  64158. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  64159. +
  64160. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  64161. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  64162. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  64163. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  64164. +
  64165. + if (core_if->dma_enable) {
  64166. + dwc_dma_t dma_addr;
  64167. + if (hc->align_buff) {
  64168. + dma_addr = hc->align_buff;
  64169. + } else {
  64170. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  64171. + }
  64172. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  64173. + }
  64174. +
  64175. + /* Start the split */
  64176. + if (hc->do_split) {
  64177. + hcsplt_data_t hcsplt;
  64178. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  64179. + hcsplt.b.spltena = 1;
  64180. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  64181. + }
  64182. +
  64183. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64184. + hcchar.b.multicnt = hc->multi_count;
  64185. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  64186. +#ifdef DEBUG
  64187. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  64188. + if (hcchar.b.chdis) {
  64189. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  64190. + __func__, hc->hc_num, hcchar.d32);
  64191. + }
  64192. +#endif
  64193. +
  64194. + /* Set host channel enable after all other setup is complete. */
  64195. + hcchar.b.chen = 1;
  64196. + hcchar.b.chdis = 0;
  64197. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64198. +
  64199. + hc->xfer_started = 1;
  64200. + hc->requests++;
  64201. +
  64202. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  64203. + /* Load OUT packet into the appropriate Tx FIFO. */
  64204. + dwc_otg_hc_write_packet(core_if, hc);
  64205. + }
  64206. +#ifdef DEBUG
  64207. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  64208. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  64209. + hc->hc_num, core_if);//GRAYG
  64210. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  64211. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  64212. +
  64213. + /* Start a timer for this transfer. */
  64214. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  64215. + }
  64216. +#endif
  64217. +}
  64218. +
  64219. +/**
  64220. + * This function does the setup for a data transfer for a host channel
  64221. + * and starts the transfer in Descriptor DMA mode.
  64222. + *
  64223. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  64224. + * Sets PID and NTD values. For periodic transfers
  64225. + * initializes SCHED_INFO field with micro-frame bitmap.
  64226. + *
  64227. + * Initializes HCDMA register with descriptor list address and CTD value
  64228. + * then starts the transfer via enabling the channel.
  64229. + *
  64230. + * @param core_if Programming view of DWC_otg controller.
  64231. + * @param hc Information needed to initialize the host channel.
  64232. + */
  64233. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64234. +{
  64235. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  64236. + hcchar_data_t hcchar;
  64237. + hctsiz_data_t hctsiz;
  64238. + hcdma_data_t hcdma;
  64239. +
  64240. + hctsiz.d32 = 0;
  64241. +
  64242. + if (hc->do_ping)
  64243. + hctsiz.b_ddma.dopng = 1;
  64244. +
  64245. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  64246. + set_pid_isoc(hc);
  64247. +
  64248. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  64249. + hctsiz.b_ddma.pid = hc->data_pid_start;
  64250. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  64251. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  64252. +
  64253. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  64254. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  64255. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  64256. +
  64257. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  64258. +
  64259. + hcdma.d32 = 0;
  64260. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  64261. +
  64262. + /* Always start from first descriptor. */
  64263. + hcdma.b.ctd = 0;
  64264. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  64265. +
  64266. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64267. + hcchar.b.multicnt = hc->multi_count;
  64268. +
  64269. +#ifdef DEBUG
  64270. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  64271. + if (hcchar.b.chdis) {
  64272. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  64273. + __func__, hc->hc_num, hcchar.d32);
  64274. + }
  64275. +#endif
  64276. +
  64277. + /* Set host channel enable after all other setup is complete. */
  64278. + hcchar.b.chen = 1;
  64279. + hcchar.b.chdis = 0;
  64280. +
  64281. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64282. +
  64283. + hc->xfer_started = 1;
  64284. + hc->requests++;
  64285. +
  64286. +#ifdef DEBUG
  64287. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  64288. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  64289. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  64290. + hc->hc_num, core_if);//GRAYG
  64291. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  64292. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  64293. + /* Start a timer for this transfer. */
  64294. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  64295. + }
  64296. +#endif
  64297. +
  64298. +}
  64299. +
  64300. +/**
  64301. + * This function continues a data transfer that was started by previous call
  64302. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  64303. + * sufficient space in the request queue and Tx Data FIFO. This function
  64304. + * should only be called in Slave mode. In DMA mode, the controller acts
  64305. + * autonomously to complete transfers programmed to a host channel.
  64306. + *
  64307. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  64308. + * if there is any data remaining to be queued. For an IN transfer, another
  64309. + * data packet is always requested. For the SETUP phase of a control transfer,
  64310. + * this function does nothing.
  64311. + *
  64312. + * @return 1 if a new request is queued, 0 if no more requests are required
  64313. + * for this transfer.
  64314. + */
  64315. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64316. +{
  64317. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  64318. +
  64319. + if (hc->do_split) {
  64320. + /* SPLITs always queue just once per channel */
  64321. + return 0;
  64322. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  64323. + /* SETUPs are queued only once since they can't be NAKed. */
  64324. + return 0;
  64325. + } else if (hc->ep_is_in) {
  64326. + /*
  64327. + * Always queue another request for other IN transfers. If
  64328. + * back-to-back INs are issued and NAKs are received for both,
  64329. + * the driver may still be processing the first NAK when the
  64330. + * second NAK is received. When the interrupt handler clears
  64331. + * the NAK interrupt for the first NAK, the second NAK will
  64332. + * not be seen. So we can't depend on the NAK interrupt
  64333. + * handler to requeue a NAKed request. Instead, IN requests
  64334. + * are issued each time this function is called. When the
  64335. + * transfer completes, the extra requests for the channel will
  64336. + * be flushed.
  64337. + */
  64338. + hcchar_data_t hcchar;
  64339. + dwc_otg_hc_regs_t *hc_regs =
  64340. + core_if->host_if->hc_regs[hc->hc_num];
  64341. +
  64342. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64343. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  64344. + hcchar.b.chen = 1;
  64345. + hcchar.b.chdis = 0;
  64346. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  64347. + hcchar.d32);
  64348. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64349. + hc->requests++;
  64350. + return 1;
  64351. + } else {
  64352. + /* OUT transfers. */
  64353. + if (hc->xfer_count < hc->xfer_len) {
  64354. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  64355. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  64356. + hcchar_data_t hcchar;
  64357. + dwc_otg_hc_regs_t *hc_regs;
  64358. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  64359. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64360. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  64361. + }
  64362. +
  64363. + /* Load OUT packet into the appropriate Tx FIFO. */
  64364. + dwc_otg_hc_write_packet(core_if, hc);
  64365. + hc->requests++;
  64366. + return 1;
  64367. + } else {
  64368. + return 0;
  64369. + }
  64370. + }
  64371. +}
  64372. +
  64373. +/**
  64374. + * Starts a PING transfer. This function should only be called in Slave mode.
  64375. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  64376. + */
  64377. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64378. +{
  64379. + hcchar_data_t hcchar;
  64380. + hctsiz_data_t hctsiz;
  64381. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  64382. +
  64383. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  64384. +
  64385. + hctsiz.d32 = 0;
  64386. + hctsiz.b.dopng = 1;
  64387. + hctsiz.b.pktcnt = 1;
  64388. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  64389. +
  64390. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64391. + hcchar.b.chen = 1;
  64392. + hcchar.b.chdis = 0;
  64393. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64394. +}
  64395. +
  64396. +/*
  64397. + * This function writes a packet into the Tx FIFO associated with the Host
  64398. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  64399. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  64400. + * periodic Tx FIFO is written. This function should only be called in Slave
  64401. + * mode.
  64402. + *
  64403. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  64404. + * then number of bytes written to the Tx FIFO.
  64405. + */
  64406. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64407. +{
  64408. + uint32_t i;
  64409. + uint32_t remaining_count;
  64410. + uint32_t byte_count;
  64411. + uint32_t dword_count;
  64412. +
  64413. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  64414. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  64415. +
  64416. + remaining_count = hc->xfer_len - hc->xfer_count;
  64417. + if (remaining_count > hc->max_packet) {
  64418. + byte_count = hc->max_packet;
  64419. + } else {
  64420. + byte_count = remaining_count;
  64421. + }
  64422. +
  64423. + dword_count = (byte_count + 3) / 4;
  64424. +
  64425. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  64426. + /* xfer_buff is DWORD aligned. */
  64427. + for (i = 0; i < dword_count; i++, data_buff++) {
  64428. + DWC_WRITE_REG32(data_fifo, *data_buff);
  64429. + }
  64430. + } else {
  64431. + /* xfer_buff is not DWORD aligned. */
  64432. + for (i = 0; i < dword_count; i++, data_buff++) {
  64433. + uint32_t data;
  64434. + data =
  64435. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  64436. + 16 | data_buff[3] << 24);
  64437. + DWC_WRITE_REG32(data_fifo, data);
  64438. + }
  64439. + }
  64440. +
  64441. + hc->xfer_count += byte_count;
  64442. + hc->xfer_buff += byte_count;
  64443. +}
  64444. +
  64445. +/**
  64446. + * Gets the current USB frame number. This is the frame number from the last
  64447. + * SOF packet.
  64448. + */
  64449. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  64450. +{
  64451. + dsts_data_t dsts;
  64452. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  64453. +
  64454. + /* read current frame/microframe number from DSTS register */
  64455. + return dsts.b.soffn;
  64456. +}
  64457. +
  64458. +/**
  64459. + * Calculates and gets the frame Interval value of HFIR register according PHY
  64460. + * type and speed.The application can modify a value of HFIR register only after
  64461. + * the Port Enable bit of the Host Port Control and Status register
  64462. + * (HPRT.PrtEnaPort) has been set.
  64463. +*/
  64464. +
  64465. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  64466. +{
  64467. + gusbcfg_data_t usbcfg;
  64468. + hwcfg2_data_t hwcfg2;
  64469. + hprt0_data_t hprt0;
  64470. + int clock = 60; // default value
  64471. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  64472. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  64473. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  64474. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  64475. + clock = 60;
  64476. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  64477. + clock = 48;
  64478. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  64479. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  64480. + clock = 30;
  64481. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  64482. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  64483. + clock = 60;
  64484. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  64485. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  64486. + clock = 48;
  64487. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  64488. + clock = 48;
  64489. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  64490. + clock = 48;
  64491. + if (hprt0.b.prtspd == 0)
  64492. + /* High speed case */
  64493. + return 125 * clock;
  64494. + else
  64495. + /* FS/LS case */
  64496. + return 1000 * clock;
  64497. +}
  64498. +
  64499. +/**
  64500. + * This function reads a setup packet from the Rx FIFO into the destination
  64501. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  64502. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  64503. + *
  64504. + * @param core_if Programming view of DWC_otg controller.
  64505. + * @param dest Destination buffer for packet data.
  64506. + */
  64507. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  64508. +{
  64509. + device_grxsts_data_t status;
  64510. + /* Get the 8 bytes of a setup transaction data */
  64511. +
  64512. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  64513. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  64514. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  64515. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  64516. + status.d32 =
  64517. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  64518. + DWC_DEBUGPL(DBG_ANY,
  64519. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  64520. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  64521. + status.b.fn, status.b.fn);
  64522. + }
  64523. +}
  64524. +
  64525. +/**
  64526. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  64527. + * IN for transmitting packets. It is normally called when the
  64528. + * "Enumeration Done" interrupt occurs.
  64529. + *
  64530. + * @param core_if Programming view of DWC_otg controller.
  64531. + * @param ep The EP0 data.
  64532. + */
  64533. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  64534. +{
  64535. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  64536. + dsts_data_t dsts;
  64537. + depctl_data_t diepctl;
  64538. + depctl_data_t doepctl;
  64539. + dctl_data_t dctl = {.d32 = 0 };
  64540. +
  64541. + ep->stp_rollover = 0;
  64542. + /* Read the Device Status and Endpoint 0 Control registers */
  64543. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  64544. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  64545. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  64546. +
  64547. + /* Set the MPS of the IN EP based on the enumeration speed */
  64548. + switch (dsts.b.enumspd) {
  64549. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  64550. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  64551. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  64552. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  64553. + break;
  64554. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  64555. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  64556. + break;
  64557. + }
  64558. +
  64559. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  64560. +
  64561. + /* Enable OUT EP for receive */
  64562. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  64563. + doepctl.b.epena = 1;
  64564. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  64565. + }
  64566. +#ifdef VERBOSE
  64567. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  64568. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  64569. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  64570. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  64571. +#endif
  64572. + dctl.b.cgnpinnak = 1;
  64573. +
  64574. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  64575. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  64576. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  64577. +
  64578. +}
  64579. +
  64580. +/**
  64581. + * This function activates an EP. The Device EP control register for
  64582. + * the EP is configured as defined in the ep structure. Note: This
  64583. + * function is not used for EP0.
  64584. + *
  64585. + * @param core_if Programming view of DWC_otg controller.
  64586. + * @param ep The EP to activate.
  64587. + */
  64588. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  64589. +{
  64590. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  64591. + depctl_data_t depctl;
  64592. + volatile uint32_t *addr;
  64593. + daint_data_t daintmsk = {.d32 = 0 };
  64594. + dcfg_data_t dcfg;
  64595. + uint8_t i;
  64596. +
  64597. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  64598. + (ep->is_in ? "IN" : "OUT"));
  64599. +
  64600. +#ifdef DWC_UTE_PER_IO
  64601. + ep->xiso_frame_num = 0xFFFFFFFF;
  64602. + ep->xiso_active_xfers = 0;
  64603. + ep->xiso_queued_xfers = 0;
  64604. +#endif
  64605. + /* Read DEPCTLn register */
  64606. + if (ep->is_in == 1) {
  64607. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  64608. + daintmsk.ep.in = 1 << ep->num;
  64609. + } else {
  64610. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  64611. + daintmsk.ep.out = 1 << ep->num;
  64612. + }
  64613. +
  64614. + /* If the EP is already active don't change the EP Control
  64615. + * register. */
  64616. + depctl.d32 = DWC_READ_REG32(addr);
  64617. + if (!depctl.b.usbactep) {
  64618. + depctl.b.mps = ep->maxpacket;
  64619. + depctl.b.eptype = ep->type;
  64620. + depctl.b.txfnum = ep->tx_fifo_num;
  64621. +
  64622. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  64623. + depctl.b.setd0pid = 1; // ???
  64624. + } else {
  64625. + depctl.b.setd0pid = 1;
  64626. + }
  64627. + depctl.b.usbactep = 1;
  64628. +
  64629. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  64630. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  64631. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  64632. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  64633. + break;
  64634. + }
  64635. + core_if->nextep_seq[i] = ep->num;
  64636. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  64637. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  64638. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  64639. + dcfg.b.epmscnt++;
  64640. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  64641. +
  64642. + DWC_DEBUGPL(DBG_PCDV,
  64643. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  64644. + __func__, core_if->first_in_nextep_seq);
  64645. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  64646. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  64647. + core_if->nextep_seq[i]);
  64648. + }
  64649. +
  64650. + }
  64651. +
  64652. +
  64653. + DWC_WRITE_REG32(addr, depctl.d32);
  64654. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  64655. + }
  64656. +
  64657. + /* Enable the Interrupt for this EP */
  64658. + if (core_if->multiproc_int_enable) {
  64659. + if (ep->is_in == 1) {
  64660. + diepmsk_data_t diepmsk = {.d32 = 0 };
  64661. + diepmsk.b.xfercompl = 1;
  64662. + diepmsk.b.timeout = 1;
  64663. + diepmsk.b.epdisabled = 1;
  64664. + diepmsk.b.ahberr = 1;
  64665. + diepmsk.b.intknepmis = 1;
  64666. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  64667. + diepmsk.b.intknepmis = 0;
  64668. + diepmsk.b.txfifoundrn = 1; //?????
  64669. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  64670. + diepmsk.b.nak = 1;
  64671. + }
  64672. +
  64673. +
  64674. +
  64675. +/*
  64676. + if (core_if->dma_desc_enable) {
  64677. + diepmsk.b.bna = 1;
  64678. + }
  64679. +*/
  64680. +/*
  64681. + if (core_if->dma_enable) {
  64682. + doepmsk.b.nak = 1;
  64683. + }
  64684. +*/
  64685. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  64686. + diepeachintmsk[ep->num], diepmsk.d32);
  64687. +
  64688. + } else {
  64689. + doepmsk_data_t doepmsk = {.d32 = 0 };
  64690. + doepmsk.b.xfercompl = 1;
  64691. + doepmsk.b.ahberr = 1;
  64692. + doepmsk.b.epdisabled = 1;
  64693. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  64694. + doepmsk.b.outtknepdis = 1;
  64695. +
  64696. +/*
  64697. +
  64698. + if (core_if->dma_desc_enable) {
  64699. + doepmsk.b.bna = 1;
  64700. + }
  64701. +*/
  64702. +/*
  64703. + doepmsk.b.babble = 1;
  64704. + doepmsk.b.nyet = 1;
  64705. + doepmsk.b.nak = 1;
  64706. +*/
  64707. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  64708. + doepeachintmsk[ep->num], doepmsk.d32);
  64709. + }
  64710. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  64711. + 0, daintmsk.d32);
  64712. + } else {
  64713. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  64714. + if (ep->is_in) {
  64715. + diepmsk_data_t diepmsk = {.d32 = 0 };
  64716. + diepmsk.b.nak = 1;
  64717. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  64718. + } else {
  64719. + doepmsk_data_t doepmsk = {.d32 = 0 };
  64720. + doepmsk.b.outtknepdis = 1;
  64721. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  64722. + }
  64723. + }
  64724. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  64725. + 0, daintmsk.d32);
  64726. + }
  64727. +
  64728. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  64729. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  64730. +
  64731. + ep->stall_clear_flag = 0;
  64732. +
  64733. + return;
  64734. +}
  64735. +
  64736. +/**
  64737. + * This function deactivates an EP. This is done by clearing the USB Active
  64738. + * EP bit in the Device EP control register. Note: This function is not used
  64739. + * for EP0. EP0 cannot be deactivated.
  64740. + *
  64741. + * @param core_if Programming view of DWC_otg controller.
  64742. + * @param ep The EP to deactivate.
  64743. + */
  64744. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  64745. +{
  64746. + depctl_data_t depctl = {.d32 = 0 };
  64747. + volatile uint32_t *addr;
  64748. + daint_data_t daintmsk = {.d32 = 0 };
  64749. + dcfg_data_t dcfg;
  64750. + uint8_t i = 0;
  64751. +
  64752. +#ifdef DWC_UTE_PER_IO
  64753. + ep->xiso_frame_num = 0xFFFFFFFF;
  64754. + ep->xiso_active_xfers = 0;
  64755. + ep->xiso_queued_xfers = 0;
  64756. +#endif
  64757. +
  64758. + /* Read DEPCTLn register */
  64759. + if (ep->is_in == 1) {
  64760. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  64761. + daintmsk.ep.in = 1 << ep->num;
  64762. + } else {
  64763. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  64764. + daintmsk.ep.out = 1 << ep->num;
  64765. + }
  64766. +
  64767. + depctl.d32 = DWC_READ_REG32(addr);
  64768. +
  64769. + depctl.b.usbactep = 0;
  64770. +
  64771. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  64772. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  64773. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  64774. + if (core_if->nextep_seq[i] == ep->num)
  64775. + break;
  64776. + }
  64777. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  64778. + if (core_if->first_in_nextep_seq == ep->num)
  64779. + core_if->first_in_nextep_seq = i;
  64780. + core_if->nextep_seq[ep->num] = 0xff;
  64781. + depctl.b.nextep = 0;
  64782. + dcfg.d32 =
  64783. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  64784. + dcfg.b.epmscnt--;
  64785. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  64786. + dcfg.d32);
  64787. +
  64788. + DWC_DEBUGPL(DBG_PCDV,
  64789. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  64790. + __func__, core_if->first_in_nextep_seq);
  64791. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  64792. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  64793. + }
  64794. + }
  64795. +
  64796. + if (ep->is_in == 1)
  64797. + depctl.b.txfnum = 0;
  64798. +
  64799. + if (core_if->dma_desc_enable)
  64800. + depctl.b.epdis = 1;
  64801. +
  64802. + DWC_WRITE_REG32(addr, depctl.d32);
  64803. + depctl.d32 = DWC_READ_REG32(addr);
  64804. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  64805. + && depctl.b.epena) {
  64806. + depctl_data_t depctl = {.d32 = 0};
  64807. + if (ep->is_in) {
  64808. + diepint_data_t diepint = {.d32 = 0};
  64809. +
  64810. + depctl.b.snak = 1;
  64811. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  64812. + diepctl, depctl.d32);
  64813. + do {
  64814. + dwc_udelay(10);
  64815. + diepint.d32 =
  64816. + DWC_READ_REG32(&core_if->
  64817. + dev_if->in_ep_regs[ep->num]->
  64818. + diepint);
  64819. + } while (!diepint.b.inepnakeff);
  64820. + diepint.b.inepnakeff = 1;
  64821. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  64822. + diepint, diepint.d32);
  64823. + depctl.d32 = 0;
  64824. + depctl.b.epdis = 1;
  64825. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  64826. + diepctl, depctl.d32);
  64827. + do {
  64828. + dwc_udelay(10);
  64829. + diepint.d32 =
  64830. + DWC_READ_REG32(&core_if->
  64831. + dev_if->in_ep_regs[ep->num]->
  64832. + diepint);
  64833. + } while (!diepint.b.epdisabled);
  64834. + diepint.b.epdisabled = 1;
  64835. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  64836. + diepint, diepint.d32);
  64837. + } else {
  64838. + dctl_data_t dctl = {.d32 = 0};
  64839. + gintmsk_data_t gintsts = {.d32 = 0};
  64840. + doepint_data_t doepint = {.d32 = 0};
  64841. + dctl.b.sgoutnak = 1;
  64842. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  64843. + dctl, 0, dctl.d32);
  64844. + do {
  64845. + dwc_udelay(10);
  64846. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  64847. + } while (!gintsts.b.goutnakeff);
  64848. + gintsts.d32 = 0;
  64849. + gintsts.b.goutnakeff = 1;
  64850. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64851. +
  64852. + depctl.d32 = 0;
  64853. + depctl.b.epdis = 1;
  64854. + depctl.b.snak = 1;
  64855. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  64856. + do
  64857. + {
  64858. + dwc_udelay(10);
  64859. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  64860. + out_ep_regs[ep->num]->doepint);
  64861. + } while (!doepint.b.epdisabled);
  64862. +
  64863. + doepint.b.epdisabled = 1;
  64864. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  64865. +
  64866. + dctl.d32 = 0;
  64867. + dctl.b.cgoutnak = 1;
  64868. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  64869. + }
  64870. + }
  64871. +
  64872. + /* Disable the Interrupt for this EP */
  64873. + if (core_if->multiproc_int_enable) {
  64874. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  64875. + daintmsk.d32, 0);
  64876. +
  64877. + if (ep->is_in == 1) {
  64878. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  64879. + diepeachintmsk[ep->num], 0);
  64880. + } else {
  64881. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  64882. + doepeachintmsk[ep->num], 0);
  64883. + }
  64884. + } else {
  64885. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  64886. + daintmsk.d32, 0);
  64887. + }
  64888. +
  64889. +}
  64890. +
  64891. +/**
  64892. + * This function initializes dma descriptor chain.
  64893. + *
  64894. + * @param core_if Programming view of DWC_otg controller.
  64895. + * @param ep The EP to start the transfer on.
  64896. + */
  64897. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  64898. +{
  64899. + dwc_otg_dev_dma_desc_t *dma_desc;
  64900. + uint32_t offset;
  64901. + uint32_t xfer_est;
  64902. + int i;
  64903. + unsigned maxxfer_local, total_len;
  64904. +
  64905. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  64906. + (ep->maxpacket%4)) {
  64907. + maxxfer_local = ep->maxpacket;
  64908. + total_len = ep->xfer_len;
  64909. + } else {
  64910. + maxxfer_local = ep->maxxfer;
  64911. + total_len = ep->total_len;
  64912. + }
  64913. +
  64914. + ep->desc_cnt = (total_len / maxxfer_local) +
  64915. + ((total_len % maxxfer_local) ? 1 : 0);
  64916. +
  64917. + if (!ep->desc_cnt)
  64918. + ep->desc_cnt = 1;
  64919. +
  64920. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  64921. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  64922. +
  64923. + dma_desc = ep->desc_addr;
  64924. + if (maxxfer_local == ep->maxpacket) {
  64925. + if ((total_len % maxxfer_local) &&
  64926. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  64927. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  64928. + (total_len % maxxfer_local);
  64929. + } else
  64930. + xfer_est = ep->desc_cnt * maxxfer_local;
  64931. + } else
  64932. + xfer_est = total_len;
  64933. + offset = 0;
  64934. + for (i = 0; i < ep->desc_cnt; ++i) {
  64935. + /** DMA Descriptor Setup */
  64936. + if (xfer_est > maxxfer_local) {
  64937. + dma_desc->status.b.bs = BS_HOST_BUSY;
  64938. + dma_desc->status.b.l = 0;
  64939. + dma_desc->status.b.ioc = 0;
  64940. + dma_desc->status.b.sp = 0;
  64941. + dma_desc->status.b.bytes = maxxfer_local;
  64942. + dma_desc->buf = ep->dma_addr + offset;
  64943. + dma_desc->status.b.sts = 0;
  64944. + dma_desc->status.b.bs = BS_HOST_READY;
  64945. +
  64946. + xfer_est -= maxxfer_local;
  64947. + offset += maxxfer_local;
  64948. + } else {
  64949. + dma_desc->status.b.bs = BS_HOST_BUSY;
  64950. + dma_desc->status.b.l = 1;
  64951. + dma_desc->status.b.ioc = 1;
  64952. + if (ep->is_in) {
  64953. + dma_desc->status.b.sp =
  64954. + (xfer_est %
  64955. + ep->maxpacket) ? 1 : ((ep->
  64956. + sent_zlp) ? 1 : 0);
  64957. + dma_desc->status.b.bytes = xfer_est;
  64958. + } else {
  64959. + if (maxxfer_local == ep->maxpacket)
  64960. + dma_desc->status.b.bytes = xfer_est;
  64961. + else
  64962. + dma_desc->status.b.bytes =
  64963. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  64964. + }
  64965. +
  64966. + dma_desc->buf = ep->dma_addr + offset;
  64967. + dma_desc->status.b.sts = 0;
  64968. + dma_desc->status.b.bs = BS_HOST_READY;
  64969. + }
  64970. + dma_desc++;
  64971. + }
  64972. +}
  64973. +/**
  64974. + * This function is called when to write ISOC data into appropriate dedicated
  64975. + * periodic FIFO.
  64976. + */
  64977. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  64978. +{
  64979. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  64980. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  64981. + dtxfsts_data_t txstatus = {.d32 = 0 };
  64982. + uint32_t len = 0;
  64983. + int epnum = dwc_ep->num;
  64984. + int dwords;
  64985. +
  64986. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  64987. +
  64988. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  64989. +
  64990. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  64991. +
  64992. + if (len > dwc_ep->maxpacket) {
  64993. + len = dwc_ep->maxpacket;
  64994. + }
  64995. +
  64996. + dwords = (len + 3) / 4;
  64997. +
  64998. + /* While there is space in the queue and space in the FIFO and
  64999. + * More data to tranfer, Write packets to the Tx FIFO */
  65000. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  65001. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  65002. +
  65003. + while (txstatus.b.txfspcavail > dwords &&
  65004. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  65005. + /* Write the FIFO */
  65006. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  65007. +
  65008. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  65009. + if (len > dwc_ep->maxpacket) {
  65010. + len = dwc_ep->maxpacket;
  65011. + }
  65012. +
  65013. + dwords = (len + 3) / 4;
  65014. + txstatus.d32 =
  65015. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  65016. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  65017. + txstatus.d32);
  65018. + }
  65019. +
  65020. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  65021. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  65022. +
  65023. + return 1;
  65024. +}
  65025. +/**
  65026. + * This function does the setup for a data transfer for an EP and
  65027. + * starts the transfer. For an IN transfer, the packets will be
  65028. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  65029. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  65030. + *
  65031. + * @param core_if Programming view of DWC_otg controller.
  65032. + * @param ep The EP to start the transfer on.
  65033. + */
  65034. +
  65035. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65036. +{
  65037. + depctl_data_t depctl;
  65038. + deptsiz_data_t deptsiz;
  65039. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65040. +
  65041. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  65042. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  65043. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  65044. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  65045. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  65046. + ep->total_len);
  65047. + /* IN endpoint */
  65048. + if (ep->is_in == 1) {
  65049. + dwc_otg_dev_in_ep_regs_t *in_regs =
  65050. + core_if->dev_if->in_ep_regs[ep->num];
  65051. +
  65052. + gnptxsts_data_t gtxstatus;
  65053. +
  65054. + gtxstatus.d32 =
  65055. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  65056. +
  65057. + if (core_if->en_multiple_tx_fifo == 0
  65058. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  65059. +#ifdef DEBUG
  65060. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  65061. +#endif
  65062. + return;
  65063. + }
  65064. +
  65065. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  65066. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  65067. +
  65068. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  65069. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  65070. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  65071. + else
  65072. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  65073. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  65074. +
  65075. +
  65076. + /* Zero Length Packet? */
  65077. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  65078. + deptsiz.b.xfersize = 0;
  65079. + deptsiz.b.pktcnt = 1;
  65080. + } else {
  65081. + /* Program the transfer size and packet count
  65082. + * as follows: xfersize = N * maxpacket +
  65083. + * short_packet pktcnt = N + (short_packet
  65084. + * exist ? 1 : 0)
  65085. + */
  65086. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  65087. + deptsiz.b.pktcnt =
  65088. + (ep->xfer_len - ep->xfer_count - 1 +
  65089. + ep->maxpacket) / ep->maxpacket;
  65090. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  65091. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  65092. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  65093. + }
  65094. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  65095. + deptsiz.b.mc = deptsiz.b.pktcnt;
  65096. + }
  65097. +
  65098. + /* Write the DMA register */
  65099. + if (core_if->dma_enable) {
  65100. + if (core_if->dma_desc_enable == 0) {
  65101. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  65102. + deptsiz.b.mc = 1;
  65103. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  65104. + deptsiz.d32);
  65105. + DWC_WRITE_REG32(&(in_regs->diepdma),
  65106. + (uint32_t) ep->dma_addr);
  65107. + } else {
  65108. +#ifdef DWC_UTE_CFI
  65109. + /* The descriptor chain should be already initialized by now */
  65110. + if (ep->buff_mode != BM_STANDARD) {
  65111. + DWC_WRITE_REG32(&in_regs->diepdma,
  65112. + ep->descs_dma_addr);
  65113. + } else {
  65114. +#endif
  65115. + init_dma_desc_chain(core_if, ep);
  65116. + /** DIEPDMAn Register write */
  65117. + DWC_WRITE_REG32(&in_regs->diepdma,
  65118. + ep->dma_desc_addr);
  65119. +#ifdef DWC_UTE_CFI
  65120. + }
  65121. +#endif
  65122. + }
  65123. + } else {
  65124. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  65125. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  65126. + /**
  65127. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  65128. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  65129. + * the data will be written into the fifo by the ISR.
  65130. + */
  65131. + if (core_if->en_multiple_tx_fifo == 0) {
  65132. + intr_mask.b.nptxfempty = 1;
  65133. + DWC_MODIFY_REG32
  65134. + (&core_if->core_global_regs->gintmsk,
  65135. + intr_mask.d32, intr_mask.d32);
  65136. + } else {
  65137. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  65138. + if (ep->xfer_len > 0) {
  65139. + uint32_t fifoemptymsk = 0;
  65140. + fifoemptymsk = 1 << ep->num;
  65141. + DWC_MODIFY_REG32
  65142. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  65143. + 0, fifoemptymsk);
  65144. +
  65145. + }
  65146. + }
  65147. + } else {
  65148. + write_isoc_tx_fifo(core_if, ep);
  65149. + }
  65150. + }
  65151. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  65152. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  65153. +
  65154. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  65155. + dsts_data_t dsts = {.d32 = 0};
  65156. + if (ep->bInterval == 1) {
  65157. + dsts.d32 =
  65158. + DWC_READ_REG32(&core_if->dev_if->
  65159. + dev_global_regs->dsts);
  65160. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  65161. + if (ep->frame_num > 0x3FFF) {
  65162. + ep->frm_overrun = 1;
  65163. + ep->frame_num &= 0x3FFF;
  65164. + } else
  65165. + ep->frm_overrun = 0;
  65166. + if (ep->frame_num & 0x1) {
  65167. + depctl.b.setd1pid = 1;
  65168. + } else {
  65169. + depctl.b.setd0pid = 1;
  65170. + }
  65171. + }
  65172. + }
  65173. + /* EP enable, IN data in FIFO */
  65174. + depctl.b.cnak = 1;
  65175. + depctl.b.epena = 1;
  65176. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  65177. +
  65178. + } else {
  65179. + /* OUT endpoint */
  65180. + dwc_otg_dev_out_ep_regs_t *out_regs =
  65181. + core_if->dev_if->out_ep_regs[ep->num];
  65182. +
  65183. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  65184. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  65185. +
  65186. + if (!core_if->dma_desc_enable) {
  65187. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  65188. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  65189. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  65190. + else
  65191. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  65192. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  65193. + }
  65194. +
  65195. + /* Program the transfer size and packet count as follows:
  65196. + *
  65197. + * pktcnt = N
  65198. + * xfersize = N * maxpacket
  65199. + */
  65200. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  65201. + /* Zero Length Packet */
  65202. + deptsiz.b.xfersize = ep->maxpacket;
  65203. + deptsiz.b.pktcnt = 1;
  65204. + } else {
  65205. + deptsiz.b.pktcnt =
  65206. + (ep->xfer_len - ep->xfer_count +
  65207. + (ep->maxpacket - 1)) / ep->maxpacket;
  65208. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  65209. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  65210. + }
  65211. + if (!core_if->dma_desc_enable) {
  65212. + ep->xfer_len =
  65213. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  65214. + }
  65215. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  65216. + }
  65217. +
  65218. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  65219. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  65220. +
  65221. + if (core_if->dma_enable) {
  65222. + if (!core_if->dma_desc_enable) {
  65223. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  65224. + deptsiz.d32);
  65225. +
  65226. + DWC_WRITE_REG32(&(out_regs->doepdma),
  65227. + (uint32_t) ep->dma_addr);
  65228. + } else {
  65229. +#ifdef DWC_UTE_CFI
  65230. + /* The descriptor chain should be already initialized by now */
  65231. + if (ep->buff_mode != BM_STANDARD) {
  65232. + DWC_WRITE_REG32(&out_regs->doepdma,
  65233. + ep->descs_dma_addr);
  65234. + } else {
  65235. +#endif
  65236. + /** This is used for interrupt out transfers*/
  65237. + if (!ep->xfer_len)
  65238. + ep->xfer_len = ep->total_len;
  65239. + init_dma_desc_chain(core_if, ep);
  65240. +
  65241. + if (core_if->core_params->dev_out_nak) {
  65242. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  65243. + deptsiz.b.pktcnt = (ep->total_len +
  65244. + (ep->maxpacket - 1)) / ep->maxpacket;
  65245. + deptsiz.b.xfersize = ep->total_len;
  65246. + /* Remember initial value of doeptsiz */
  65247. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  65248. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  65249. + deptsiz.d32);
  65250. + }
  65251. + }
  65252. + /** DOEPDMAn Register write */
  65253. + DWC_WRITE_REG32(&out_regs->doepdma,
  65254. + ep->dma_desc_addr);
  65255. +#ifdef DWC_UTE_CFI
  65256. + }
  65257. +#endif
  65258. + }
  65259. + } else {
  65260. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  65261. + }
  65262. +
  65263. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  65264. + dsts_data_t dsts = {.d32 = 0};
  65265. + if (ep->bInterval == 1) {
  65266. + dsts.d32 =
  65267. + DWC_READ_REG32(&core_if->dev_if->
  65268. + dev_global_regs->dsts);
  65269. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  65270. + if (ep->frame_num > 0x3FFF) {
  65271. + ep->frm_overrun = 1;
  65272. + ep->frame_num &= 0x3FFF;
  65273. + } else
  65274. + ep->frm_overrun = 0;
  65275. +
  65276. + if (ep->frame_num & 0x1) {
  65277. + depctl.b.setd1pid = 1;
  65278. + } else {
  65279. + depctl.b.setd0pid = 1;
  65280. + }
  65281. + }
  65282. + }
  65283. +
  65284. + /* EP enable */
  65285. + depctl.b.cnak = 1;
  65286. + depctl.b.epena = 1;
  65287. +
  65288. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  65289. +
  65290. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  65291. + DWC_READ_REG32(&out_regs->doepctl),
  65292. + DWC_READ_REG32(&out_regs->doeptsiz));
  65293. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  65294. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  65295. + daintmsk),
  65296. + DWC_READ_REG32(&core_if->core_global_regs->
  65297. + gintmsk));
  65298. +
  65299. + /* Timer is scheduling only for out bulk transfers for
  65300. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  65301. + * about received data payload in case of timeout
  65302. + */
  65303. + if (core_if->core_params->dev_out_nak) {
  65304. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  65305. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  65306. + core_if->ep_xfer_info[ep->num].ep = ep;
  65307. + core_if->ep_xfer_info[ep->num].state = 1;
  65308. +
  65309. + /* Start a timer for this transfer. */
  65310. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  65311. + }
  65312. + }
  65313. + }
  65314. +}
  65315. +
  65316. +/**
  65317. + * This function setup a zero length transfer in Buffer DMA and
  65318. + * Slave modes for usb requests with zero field set
  65319. + *
  65320. + * @param core_if Programming view of DWC_otg controller.
  65321. + * @param ep The EP to start the transfer on.
  65322. + *
  65323. + */
  65324. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65325. +{
  65326. +
  65327. + depctl_data_t depctl;
  65328. + deptsiz_data_t deptsiz;
  65329. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65330. +
  65331. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  65332. + DWC_PRINTF("zero length transfer is called\n");
  65333. +
  65334. + /* IN endpoint */
  65335. + if (ep->is_in == 1) {
  65336. + dwc_otg_dev_in_ep_regs_t *in_regs =
  65337. + core_if->dev_if->in_ep_regs[ep->num];
  65338. +
  65339. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  65340. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  65341. +
  65342. + deptsiz.b.xfersize = 0;
  65343. + deptsiz.b.pktcnt = 1;
  65344. +
  65345. + /* Write the DMA register */
  65346. + if (core_if->dma_enable) {
  65347. + if (core_if->dma_desc_enable == 0) {
  65348. + deptsiz.b.mc = 1;
  65349. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  65350. + deptsiz.d32);
  65351. + DWC_WRITE_REG32(&(in_regs->diepdma),
  65352. + (uint32_t) ep->dma_addr);
  65353. + }
  65354. + } else {
  65355. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  65356. + /**
  65357. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  65358. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  65359. + * the data will be written into the fifo by the ISR.
  65360. + */
  65361. + if (core_if->en_multiple_tx_fifo == 0) {
  65362. + intr_mask.b.nptxfempty = 1;
  65363. + DWC_MODIFY_REG32(&core_if->
  65364. + core_global_regs->gintmsk,
  65365. + intr_mask.d32, intr_mask.d32);
  65366. + } else {
  65367. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  65368. + if (ep->xfer_len > 0) {
  65369. + uint32_t fifoemptymsk = 0;
  65370. + fifoemptymsk = 1 << ep->num;
  65371. + DWC_MODIFY_REG32(&core_if->
  65372. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  65373. + 0, fifoemptymsk);
  65374. + }
  65375. + }
  65376. + }
  65377. +
  65378. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  65379. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  65380. + /* EP enable, IN data in FIFO */
  65381. + depctl.b.cnak = 1;
  65382. + depctl.b.epena = 1;
  65383. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  65384. +
  65385. + } else {
  65386. + /* OUT endpoint */
  65387. + dwc_otg_dev_out_ep_regs_t *out_regs =
  65388. + core_if->dev_if->out_ep_regs[ep->num];
  65389. +
  65390. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  65391. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  65392. +
  65393. + /* Zero Length Packet */
  65394. + deptsiz.b.xfersize = ep->maxpacket;
  65395. + deptsiz.b.pktcnt = 1;
  65396. +
  65397. + if (core_if->dma_enable) {
  65398. + if (!core_if->dma_desc_enable) {
  65399. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  65400. + deptsiz.d32);
  65401. +
  65402. + DWC_WRITE_REG32(&(out_regs->doepdma),
  65403. + (uint32_t) ep->dma_addr);
  65404. + }
  65405. + } else {
  65406. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  65407. + }
  65408. +
  65409. + /* EP enable */
  65410. + depctl.b.cnak = 1;
  65411. + depctl.b.epena = 1;
  65412. +
  65413. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  65414. +
  65415. + }
  65416. +}
  65417. +
  65418. +/**
  65419. + * This function does the setup for a data transfer for EP0 and starts
  65420. + * the transfer. For an IN transfer, the packets will be loaded into
  65421. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  65422. + * unloaded from the Rx FIFO in the ISR.
  65423. + *
  65424. + * @param core_if Programming view of DWC_otg controller.
  65425. + * @param ep The EP0 data.
  65426. + */
  65427. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65428. +{
  65429. + depctl_data_t depctl;
  65430. + deptsiz0_data_t deptsiz;
  65431. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65432. + dwc_otg_dev_dma_desc_t *dma_desc;
  65433. +
  65434. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  65435. + "xfer_buff=%p start_xfer_buff=%p \n",
  65436. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  65437. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  65438. +
  65439. + ep->total_len = ep->xfer_len;
  65440. +
  65441. + /* IN endpoint */
  65442. + if (ep->is_in == 1) {
  65443. + dwc_otg_dev_in_ep_regs_t *in_regs =
  65444. + core_if->dev_if->in_ep_regs[0];
  65445. +
  65446. + gnptxsts_data_t gtxstatus;
  65447. +
  65448. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  65449. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  65450. + if (depctl.b.epena)
  65451. + return;
  65452. + }
  65453. +
  65454. + gtxstatus.d32 =
  65455. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  65456. +
  65457. + /* If dedicated FIFO every time flush fifo before enable ep*/
  65458. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  65459. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  65460. +
  65461. + if (core_if->en_multiple_tx_fifo == 0
  65462. + && gtxstatus.b.nptxqspcavail == 0
  65463. + && !core_if->dma_enable) {
  65464. +#ifdef DEBUG
  65465. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  65466. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  65467. + DWC_READ_REG32(&in_regs->diepctl));
  65468. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  65469. + deptsiz.d32,
  65470. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  65471. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  65472. + gtxstatus.d32);
  65473. +#endif
  65474. + return;
  65475. + }
  65476. +
  65477. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  65478. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  65479. +
  65480. + /* Zero Length Packet? */
  65481. + if (ep->xfer_len == 0) {
  65482. + deptsiz.b.xfersize = 0;
  65483. + deptsiz.b.pktcnt = 1;
  65484. + } else {
  65485. + /* Program the transfer size and packet count
  65486. + * as follows: xfersize = N * maxpacket +
  65487. + * short_packet pktcnt = N + (short_packet
  65488. + * exist ? 1 : 0)
  65489. + */
  65490. + if (ep->xfer_len > ep->maxpacket) {
  65491. + ep->xfer_len = ep->maxpacket;
  65492. + deptsiz.b.xfersize = ep->maxpacket;
  65493. + } else {
  65494. + deptsiz.b.xfersize = ep->xfer_len;
  65495. + }
  65496. + deptsiz.b.pktcnt = 1;
  65497. +
  65498. + }
  65499. + DWC_DEBUGPL(DBG_PCDV,
  65500. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  65501. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  65502. + deptsiz.d32);
  65503. +
  65504. + /* Write the DMA register */
  65505. + if (core_if->dma_enable) {
  65506. + if (core_if->dma_desc_enable == 0) {
  65507. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  65508. + deptsiz.d32);
  65509. +
  65510. + DWC_WRITE_REG32(&(in_regs->diepdma),
  65511. + (uint32_t) ep->dma_addr);
  65512. + } else {
  65513. + dma_desc = core_if->dev_if->in_desc_addr;
  65514. +
  65515. + /** DMA Descriptor Setup */
  65516. + dma_desc->status.b.bs = BS_HOST_BUSY;
  65517. + dma_desc->status.b.l = 1;
  65518. + dma_desc->status.b.ioc = 1;
  65519. + dma_desc->status.b.sp =
  65520. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  65521. + dma_desc->status.b.bytes = ep->xfer_len;
  65522. + dma_desc->buf = ep->dma_addr;
  65523. + dma_desc->status.b.sts = 0;
  65524. + dma_desc->status.b.bs = BS_HOST_READY;
  65525. +
  65526. + /** DIEPDMA0 Register write */
  65527. + DWC_WRITE_REG32(&in_regs->diepdma,
  65528. + core_if->
  65529. + dev_if->dma_in_desc_addr);
  65530. + }
  65531. + } else {
  65532. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  65533. + }
  65534. +
  65535. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  65536. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  65537. + /* EP enable, IN data in FIFO */
  65538. + depctl.b.cnak = 1;
  65539. + depctl.b.epena = 1;
  65540. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  65541. +
  65542. + /**
  65543. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  65544. + * data will be written into the fifo by the ISR.
  65545. + */
  65546. + if (!core_if->dma_enable) {
  65547. + if (core_if->en_multiple_tx_fifo == 0) {
  65548. + intr_mask.b.nptxfempty = 1;
  65549. + DWC_MODIFY_REG32(&core_if->
  65550. + core_global_regs->gintmsk,
  65551. + intr_mask.d32, intr_mask.d32);
  65552. + } else {
  65553. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  65554. + if (ep->xfer_len > 0) {
  65555. + uint32_t fifoemptymsk = 0;
  65556. + fifoemptymsk |= 1 << ep->num;
  65557. + DWC_MODIFY_REG32(&core_if->
  65558. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  65559. + 0, fifoemptymsk);
  65560. + }
  65561. + }
  65562. + }
  65563. + } else {
  65564. + /* OUT endpoint */
  65565. + dwc_otg_dev_out_ep_regs_t *out_regs =
  65566. + core_if->dev_if->out_ep_regs[0];
  65567. +
  65568. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  65569. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  65570. +
  65571. + /* Program the transfer size and packet count as follows:
  65572. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  65573. + * pktcnt = N */
  65574. + /* Zero Length Packet */
  65575. + deptsiz.b.xfersize = ep->maxpacket;
  65576. + deptsiz.b.pktcnt = 1;
  65577. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  65578. + deptsiz.b.supcnt = 3;
  65579. +
  65580. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  65581. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  65582. +
  65583. + if (core_if->dma_enable) {
  65584. + if (!core_if->dma_desc_enable) {
  65585. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  65586. + deptsiz.d32);
  65587. +
  65588. + DWC_WRITE_REG32(&(out_regs->doepdma),
  65589. + (uint32_t) ep->dma_addr);
  65590. + } else {
  65591. + dma_desc = core_if->dev_if->out_desc_addr;
  65592. +
  65593. + /** DMA Descriptor Setup */
  65594. + dma_desc->status.b.bs = BS_HOST_BUSY;
  65595. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  65596. + dma_desc->status.b.mtrf = 0;
  65597. + dma_desc->status.b.sr = 0;
  65598. + }
  65599. + dma_desc->status.b.l = 1;
  65600. + dma_desc->status.b.ioc = 1;
  65601. + dma_desc->status.b.bytes = ep->maxpacket;
  65602. + dma_desc->buf = ep->dma_addr;
  65603. + dma_desc->status.b.sts = 0;
  65604. + dma_desc->status.b.bs = BS_HOST_READY;
  65605. +
  65606. + /** DOEPDMA0 Register write */
  65607. + DWC_WRITE_REG32(&out_regs->doepdma,
  65608. + core_if->dev_if->
  65609. + dma_out_desc_addr);
  65610. + }
  65611. + } else {
  65612. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  65613. + }
  65614. +
  65615. + /* EP enable */
  65616. + depctl.b.cnak = 1;
  65617. + depctl.b.epena = 1;
  65618. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  65619. + }
  65620. +}
  65621. +
  65622. +/**
  65623. + * This function continues control IN transfers started by
  65624. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  65625. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  65626. + * bit for the packet count.
  65627. + *
  65628. + * @param core_if Programming view of DWC_otg controller.
  65629. + * @param ep The EP0 data.
  65630. + */
  65631. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65632. +{
  65633. + depctl_data_t depctl;
  65634. + deptsiz0_data_t deptsiz;
  65635. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65636. + dwc_otg_dev_dma_desc_t *dma_desc;
  65637. +
  65638. + if (ep->is_in == 1) {
  65639. + dwc_otg_dev_in_ep_regs_t *in_regs =
  65640. + core_if->dev_if->in_ep_regs[0];
  65641. + gnptxsts_data_t tx_status = {.d32 = 0 };
  65642. +
  65643. + tx_status.d32 =
  65644. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  65645. + /** @todo Should there be check for room in the Tx
  65646. + * Status Queue. If not remove the code above this comment. */
  65647. +
  65648. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  65649. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  65650. +
  65651. + /* Program the transfer size and packet count
  65652. + * as follows: xfersize = N * maxpacket +
  65653. + * short_packet pktcnt = N + (short_packet
  65654. + * exist ? 1 : 0)
  65655. + */
  65656. +
  65657. + if (core_if->dma_desc_enable == 0) {
  65658. + deptsiz.b.xfersize =
  65659. + (ep->total_len - ep->xfer_count) >
  65660. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  65661. + ep->xfer_count);
  65662. + deptsiz.b.pktcnt = 1;
  65663. + if (core_if->dma_enable == 0) {
  65664. + ep->xfer_len += deptsiz.b.xfersize;
  65665. + } else {
  65666. + ep->xfer_len = deptsiz.b.xfersize;
  65667. + }
  65668. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  65669. + } else {
  65670. + ep->xfer_len =
  65671. + (ep->total_len - ep->xfer_count) >
  65672. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  65673. + ep->xfer_count);
  65674. +
  65675. + dma_desc = core_if->dev_if->in_desc_addr;
  65676. +
  65677. + /** DMA Descriptor Setup */
  65678. + dma_desc->status.b.bs = BS_HOST_BUSY;
  65679. + dma_desc->status.b.l = 1;
  65680. + dma_desc->status.b.ioc = 1;
  65681. + dma_desc->status.b.sp =
  65682. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  65683. + dma_desc->status.b.bytes = ep->xfer_len;
  65684. + dma_desc->buf = ep->dma_addr;
  65685. + dma_desc->status.b.sts = 0;
  65686. + dma_desc->status.b.bs = BS_HOST_READY;
  65687. +
  65688. + /** DIEPDMA0 Register write */
  65689. + DWC_WRITE_REG32(&in_regs->diepdma,
  65690. + core_if->dev_if->dma_in_desc_addr);
  65691. + }
  65692. +
  65693. + DWC_DEBUGPL(DBG_PCDV,
  65694. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  65695. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  65696. + deptsiz.d32);
  65697. +
  65698. + /* Write the DMA register */
  65699. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  65700. + if (core_if->dma_desc_enable == 0)
  65701. + DWC_WRITE_REG32(&(in_regs->diepdma),
  65702. + (uint32_t) ep->dma_addr);
  65703. + }
  65704. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  65705. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  65706. + /* EP enable, IN data in FIFO */
  65707. + depctl.b.cnak = 1;
  65708. + depctl.b.epena = 1;
  65709. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  65710. +
  65711. + /**
  65712. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  65713. + * data will be written into the fifo by the ISR.
  65714. + */
  65715. + if (!core_if->dma_enable) {
  65716. + if (core_if->en_multiple_tx_fifo == 0) {
  65717. + /* First clear it from GINTSTS */
  65718. + intr_mask.b.nptxfempty = 1;
  65719. + DWC_MODIFY_REG32(&core_if->
  65720. + core_global_regs->gintmsk,
  65721. + intr_mask.d32, intr_mask.d32);
  65722. +
  65723. + } else {
  65724. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  65725. + if (ep->xfer_len > 0) {
  65726. + uint32_t fifoemptymsk = 0;
  65727. + fifoemptymsk |= 1 << ep->num;
  65728. + DWC_MODIFY_REG32(&core_if->
  65729. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  65730. + 0, fifoemptymsk);
  65731. + }
  65732. + }
  65733. + }
  65734. + } else {
  65735. + dwc_otg_dev_out_ep_regs_t *out_regs =
  65736. + core_if->dev_if->out_ep_regs[0];
  65737. +
  65738. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  65739. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  65740. +
  65741. + /* Program the transfer size and packet count
  65742. + * as follows: xfersize = N * maxpacket +
  65743. + * short_packet pktcnt = N + (short_packet
  65744. + * exist ? 1 : 0)
  65745. + */
  65746. + deptsiz.b.xfersize = ep->maxpacket;
  65747. + deptsiz.b.pktcnt = 1;
  65748. +
  65749. + if (core_if->dma_desc_enable == 0) {
  65750. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  65751. + } else {
  65752. + dma_desc = core_if->dev_if->out_desc_addr;
  65753. +
  65754. + /** DMA Descriptor Setup */
  65755. + dma_desc->status.b.bs = BS_HOST_BUSY;
  65756. + dma_desc->status.b.l = 1;
  65757. + dma_desc->status.b.ioc = 1;
  65758. + dma_desc->status.b.bytes = ep->maxpacket;
  65759. + dma_desc->buf = ep->dma_addr;
  65760. + dma_desc->status.b.sts = 0;
  65761. + dma_desc->status.b.bs = BS_HOST_READY;
  65762. +
  65763. + /** DOEPDMA0 Register write */
  65764. + DWC_WRITE_REG32(&out_regs->doepdma,
  65765. + core_if->dev_if->dma_out_desc_addr);
  65766. + }
  65767. +
  65768. + DWC_DEBUGPL(DBG_PCDV,
  65769. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  65770. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  65771. + deptsiz.d32);
  65772. +
  65773. + /* Write the DMA register */
  65774. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  65775. + if (core_if->dma_desc_enable == 0)
  65776. + DWC_WRITE_REG32(&(out_regs->doepdma),
  65777. + (uint32_t) ep->dma_addr);
  65778. +
  65779. + }
  65780. +
  65781. + /* EP enable, IN data in FIFO */
  65782. + depctl.b.cnak = 1;
  65783. + depctl.b.epena = 1;
  65784. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  65785. +
  65786. + }
  65787. +}
  65788. +
  65789. +#ifdef DEBUG
  65790. +void dump_msg(const u8 * buf, unsigned int length)
  65791. +{
  65792. + unsigned int start, num, i;
  65793. + char line[52], *p;
  65794. +
  65795. + if (length >= 512)
  65796. + return;
  65797. + start = 0;
  65798. + while (length > 0) {
  65799. + num = length < 16u ? length : 16u;
  65800. + p = line;
  65801. + for (i = 0; i < num; ++i) {
  65802. + if (i == 8)
  65803. + *p++ = ' ';
  65804. + DWC_SPRINTF(p, " %02x", buf[i]);
  65805. + p += 3;
  65806. + }
  65807. + *p = 0;
  65808. + DWC_PRINTF("%6x: %s\n", start, line);
  65809. + buf += num;
  65810. + start += num;
  65811. + length -= num;
  65812. + }
  65813. +}
  65814. +#else
  65815. +static inline void dump_msg(const u8 * buf, unsigned int length)
  65816. +{
  65817. +}
  65818. +#endif
  65819. +
  65820. +/**
  65821. + * This function writes a packet into the Tx FIFO associated with the
  65822. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  65823. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  65824. + * with all packets for the next micro-frame.
  65825. + *
  65826. + * @param core_if Programming view of DWC_otg controller.
  65827. + * @param ep The EP to write packet for.
  65828. + * @param dma Indicates if DMA is being used.
  65829. + */
  65830. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  65831. + int dma)
  65832. +{
  65833. + /**
  65834. + * The buffer is padded to DWORD on a per packet basis in
  65835. + * slave/dma mode if the MPS is not DWORD aligned. The last
  65836. + * packet, if short, is also padded to a multiple of DWORD.
  65837. + *
  65838. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  65839. + * multiple of DWORD in length
  65840. + *
  65841. + * ep->xfer_len can be any number of bytes
  65842. + *
  65843. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  65844. + * packet
  65845. + *
  65846. + * FIFO access is DWORD */
  65847. +
  65848. + uint32_t i;
  65849. + uint32_t byte_count;
  65850. + uint32_t dword_count;
  65851. + uint32_t *fifo;
  65852. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  65853. +
  65854. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  65855. + ep);
  65856. + if (ep->xfer_count >= ep->xfer_len) {
  65857. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  65858. + return;
  65859. + }
  65860. +
  65861. + /* Find the byte length of the packet either short packet or MPS */
  65862. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  65863. + byte_count = ep->xfer_len - ep->xfer_count;
  65864. + } else {
  65865. + byte_count = ep->maxpacket;
  65866. + }
  65867. +
  65868. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  65869. + * is not a multiple of DWORD */
  65870. + dword_count = (byte_count + 3) / 4;
  65871. +
  65872. +#ifdef VERBOSE
  65873. + dump_msg(ep->xfer_buff, byte_count);
  65874. +#endif
  65875. +
  65876. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  65877. + * intialized? What should this be? */
  65878. +
  65879. + fifo = core_if->data_fifo[ep->num];
  65880. +
  65881. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  65882. + fifo, data_buff, *data_buff, byte_count);
  65883. +
  65884. + if (!dma) {
  65885. + for (i = 0; i < dword_count; i++, data_buff++) {
  65886. + DWC_WRITE_REG32(fifo, *data_buff);
  65887. + }
  65888. + }
  65889. +
  65890. + ep->xfer_count += byte_count;
  65891. + ep->xfer_buff += byte_count;
  65892. + ep->dma_addr += byte_count;
  65893. +}
  65894. +
  65895. +/**
  65896. + * Set the EP STALL.
  65897. + *
  65898. + * @param core_if Programming view of DWC_otg controller.
  65899. + * @param ep The EP to set the stall on.
  65900. + */
  65901. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65902. +{
  65903. + depctl_data_t depctl;
  65904. + volatile uint32_t *depctl_addr;
  65905. +
  65906. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  65907. + (ep->is_in ? "IN" : "OUT"));
  65908. +
  65909. + if (ep->is_in == 1) {
  65910. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  65911. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  65912. +
  65913. + /* set the disable and stall bits */
  65914. + if (depctl.b.epena) {
  65915. + depctl.b.epdis = 1;
  65916. + }
  65917. + depctl.b.stall = 1;
  65918. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  65919. + } else {
  65920. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  65921. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  65922. +
  65923. + /* set the stall bit */
  65924. + depctl.b.stall = 1;
  65925. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  65926. + }
  65927. +
  65928. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  65929. +
  65930. + return;
  65931. +}
  65932. +
  65933. +/**
  65934. + * Clear the EP STALL.
  65935. + *
  65936. + * @param core_if Programming view of DWC_otg controller.
  65937. + * @param ep The EP to clear stall from.
  65938. + */
  65939. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65940. +{
  65941. + depctl_data_t depctl;
  65942. + volatile uint32_t *depctl_addr;
  65943. +
  65944. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  65945. + (ep->is_in ? "IN" : "OUT"));
  65946. +
  65947. + if (ep->is_in == 1) {
  65948. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  65949. + } else {
  65950. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  65951. + }
  65952. +
  65953. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  65954. +
  65955. + /* clear the stall bits */
  65956. + depctl.b.stall = 0;
  65957. +
  65958. + /*
  65959. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  65960. + * of whether an endpoint has the Halt feature set, a
  65961. + * ClearFeature(ENDPOINT_HALT) request always results in the
  65962. + * data toggle being reinitialized to DATA0.
  65963. + */
  65964. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  65965. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  65966. + depctl.b.setd0pid = 1; /* DATA0 */
  65967. + }
  65968. +
  65969. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  65970. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  65971. + return;
  65972. +}
  65973. +
  65974. +/**
  65975. + * This function reads a packet from the Rx FIFO into the destination
  65976. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  65977. + *
  65978. + * @param core_if Programming view of DWC_otg controller.
  65979. + * @param dest Destination buffer for the packet.
  65980. + * @param bytes Number of bytes to copy to the destination.
  65981. + */
  65982. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  65983. + uint8_t * dest, uint16_t bytes)
  65984. +{
  65985. + int i;
  65986. + int word_count = (bytes + 3) / 4;
  65987. +
  65988. + volatile uint32_t *fifo = core_if->data_fifo[0];
  65989. + uint32_t *data_buff = (uint32_t *) dest;
  65990. +
  65991. + /**
  65992. + * @todo Account for the case where _dest is not dword aligned. This
  65993. + * requires reading data from the FIFO into a uint32_t temp buffer,
  65994. + * then moving it into the data buffer.
  65995. + */
  65996. +
  65997. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  65998. + core_if, dest, bytes);
  65999. +
  66000. + for (i = 0; i < word_count; i++, data_buff++) {
  66001. + *data_buff = DWC_READ_REG32(fifo);
  66002. + }
  66003. +
  66004. + return;
  66005. +}
  66006. +
  66007. +/**
  66008. + * This functions reads the device registers and prints them
  66009. + *
  66010. + * @param core_if Programming view of DWC_otg controller.
  66011. + */
  66012. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  66013. +{
  66014. + int i;
  66015. + volatile uint32_t *addr;
  66016. +
  66017. + DWC_PRINTF("Device Global Registers\n");
  66018. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  66019. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  66020. + (unsigned long)addr, DWC_READ_REG32(addr));
  66021. + addr = &core_if->dev_if->dev_global_regs->dctl;
  66022. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  66023. + (unsigned long)addr, DWC_READ_REG32(addr));
  66024. + addr = &core_if->dev_if->dev_global_regs->dsts;
  66025. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  66026. + (unsigned long)addr, DWC_READ_REG32(addr));
  66027. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  66028. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66029. + DWC_READ_REG32(addr));
  66030. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  66031. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66032. + DWC_READ_REG32(addr));
  66033. + addr = &core_if->dev_if->dev_global_regs->daint;
  66034. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66035. + DWC_READ_REG32(addr));
  66036. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  66037. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66038. + DWC_READ_REG32(addr));
  66039. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  66040. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66041. + DWC_READ_REG32(addr));
  66042. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  66043. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  66044. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  66045. + (unsigned long)addr, DWC_READ_REG32(addr));
  66046. + }
  66047. +
  66048. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  66049. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66050. + DWC_READ_REG32(addr));
  66051. +
  66052. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  66053. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  66054. + (unsigned long)addr, DWC_READ_REG32(addr));
  66055. +
  66056. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  66057. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  66058. + (unsigned long)addr, DWC_READ_REG32(addr));
  66059. +
  66060. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  66061. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  66062. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  66063. + (unsigned long)addr, DWC_READ_REG32(addr));
  66064. + }
  66065. +
  66066. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  66067. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66068. + DWC_READ_REG32(addr));
  66069. +
  66070. + if (core_if->hwcfg2.b.multi_proc_int) {
  66071. +
  66072. + addr = &core_if->dev_if->dev_global_regs->deachint;
  66073. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  66074. + (unsigned long)addr, DWC_READ_REG32(addr));
  66075. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  66076. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  66077. + (unsigned long)addr, DWC_READ_REG32(addr));
  66078. +
  66079. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  66080. + addr =
  66081. + &core_if->dev_if->
  66082. + dev_global_regs->diepeachintmsk[i];
  66083. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  66084. + i, (unsigned long)addr,
  66085. + DWC_READ_REG32(addr));
  66086. + }
  66087. +
  66088. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  66089. + addr =
  66090. + &core_if->dev_if->
  66091. + dev_global_regs->doepeachintmsk[i];
  66092. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  66093. + i, (unsigned long)addr,
  66094. + DWC_READ_REG32(addr));
  66095. + }
  66096. + }
  66097. +
  66098. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  66099. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  66100. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  66101. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  66102. + (unsigned long)addr, DWC_READ_REG32(addr));
  66103. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  66104. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  66105. + (unsigned long)addr, DWC_READ_REG32(addr));
  66106. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  66107. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  66108. + (unsigned long)addr, DWC_READ_REG32(addr));
  66109. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  66110. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  66111. + (unsigned long)addr, DWC_READ_REG32(addr));
  66112. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  66113. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  66114. + (unsigned long)addr, DWC_READ_REG32(addr));
  66115. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  66116. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  66117. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  66118. + }
  66119. +
  66120. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  66121. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  66122. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  66123. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  66124. + (unsigned long)addr, DWC_READ_REG32(addr));
  66125. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  66126. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  66127. + (unsigned long)addr, DWC_READ_REG32(addr));
  66128. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  66129. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  66130. + (unsigned long)addr, DWC_READ_REG32(addr));
  66131. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  66132. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  66133. + (unsigned long)addr, DWC_READ_REG32(addr));
  66134. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  66135. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  66136. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  66137. + (unsigned long)addr, DWC_READ_REG32(addr));
  66138. + }
  66139. +
  66140. + }
  66141. +}
  66142. +
  66143. +/**
  66144. + * This functions reads the SPRAM and prints its content
  66145. + *
  66146. + * @param core_if Programming view of DWC_otg controller.
  66147. + */
  66148. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  66149. +{
  66150. + volatile uint8_t *addr, *start_addr, *end_addr;
  66151. +
  66152. + DWC_PRINTF("SPRAM Data:\n");
  66153. + start_addr = (void *)core_if->core_global_regs;
  66154. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  66155. + start_addr += 0x00028000;
  66156. + end_addr = (void *)core_if->core_global_regs;
  66157. + end_addr += 0x000280e0;
  66158. +
  66159. + for (addr = start_addr; addr < end_addr; addr += 16) {
  66160. + DWC_PRINTF
  66161. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  66162. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  66163. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  66164. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  66165. + );
  66166. + }
  66167. +
  66168. + return;
  66169. +}
  66170. +
  66171. +/**
  66172. + * This function reads the host registers and prints them
  66173. + *
  66174. + * @param core_if Programming view of DWC_otg controller.
  66175. + */
  66176. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  66177. +{
  66178. + int i;
  66179. + volatile uint32_t *addr;
  66180. +
  66181. + DWC_PRINTF("Host Global Registers\n");
  66182. + addr = &core_if->host_if->host_global_regs->hcfg;
  66183. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  66184. + (unsigned long)addr, DWC_READ_REG32(addr));
  66185. + addr = &core_if->host_if->host_global_regs->hfir;
  66186. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  66187. + (unsigned long)addr, DWC_READ_REG32(addr));
  66188. + addr = &core_if->host_if->host_global_regs->hfnum;
  66189. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66190. + DWC_READ_REG32(addr));
  66191. + addr = &core_if->host_if->host_global_regs->hptxsts;
  66192. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66193. + DWC_READ_REG32(addr));
  66194. + addr = &core_if->host_if->host_global_regs->haint;
  66195. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66196. + DWC_READ_REG32(addr));
  66197. + addr = &core_if->host_if->host_global_regs->haintmsk;
  66198. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66199. + DWC_READ_REG32(addr));
  66200. + if (core_if->dma_desc_enable) {
  66201. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  66202. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  66203. + (unsigned long)addr, DWC_READ_REG32(addr));
  66204. + }
  66205. +
  66206. + addr = core_if->host_if->hprt0;
  66207. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66208. + DWC_READ_REG32(addr));
  66209. +
  66210. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  66211. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  66212. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  66213. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  66214. + (unsigned long)addr, DWC_READ_REG32(addr));
  66215. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  66216. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  66217. + (unsigned long)addr, DWC_READ_REG32(addr));
  66218. + addr = &core_if->host_if->hc_regs[i]->hcint;
  66219. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  66220. + (unsigned long)addr, DWC_READ_REG32(addr));
  66221. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  66222. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  66223. + (unsigned long)addr, DWC_READ_REG32(addr));
  66224. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  66225. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  66226. + (unsigned long)addr, DWC_READ_REG32(addr));
  66227. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  66228. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  66229. + (unsigned long)addr, DWC_READ_REG32(addr));
  66230. + if (core_if->dma_desc_enable) {
  66231. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  66232. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  66233. + (unsigned long)addr, DWC_READ_REG32(addr));
  66234. + }
  66235. +
  66236. + }
  66237. + return;
  66238. +}
  66239. +
  66240. +/**
  66241. + * This function reads the core global registers and prints them
  66242. + *
  66243. + * @param core_if Programming view of DWC_otg controller.
  66244. + */
  66245. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  66246. +{
  66247. + int i, ep_num;
  66248. + volatile uint32_t *addr;
  66249. + char *txfsiz;
  66250. +
  66251. + DWC_PRINTF("Core Global Registers\n");
  66252. + addr = &core_if->core_global_regs->gotgctl;
  66253. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66254. + DWC_READ_REG32(addr));
  66255. + addr = &core_if->core_global_regs->gotgint;
  66256. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66257. + DWC_READ_REG32(addr));
  66258. + addr = &core_if->core_global_regs->gahbcfg;
  66259. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66260. + DWC_READ_REG32(addr));
  66261. + addr = &core_if->core_global_regs->gusbcfg;
  66262. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66263. + DWC_READ_REG32(addr));
  66264. + addr = &core_if->core_global_regs->grstctl;
  66265. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66266. + DWC_READ_REG32(addr));
  66267. + addr = &core_if->core_global_regs->gintsts;
  66268. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66269. + DWC_READ_REG32(addr));
  66270. + addr = &core_if->core_global_regs->gintmsk;
  66271. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66272. + DWC_READ_REG32(addr));
  66273. + addr = &core_if->core_global_regs->grxstsr;
  66274. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66275. + DWC_READ_REG32(addr));
  66276. + addr = &core_if->core_global_regs->grxfsiz;
  66277. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66278. + DWC_READ_REG32(addr));
  66279. + addr = &core_if->core_global_regs->gnptxfsiz;
  66280. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66281. + DWC_READ_REG32(addr));
  66282. + addr = &core_if->core_global_regs->gnptxsts;
  66283. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66284. + DWC_READ_REG32(addr));
  66285. + addr = &core_if->core_global_regs->gi2cctl;
  66286. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66287. + DWC_READ_REG32(addr));
  66288. + addr = &core_if->core_global_regs->gpvndctl;
  66289. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66290. + DWC_READ_REG32(addr));
  66291. + addr = &core_if->core_global_regs->ggpio;
  66292. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66293. + DWC_READ_REG32(addr));
  66294. + addr = &core_if->core_global_regs->guid;
  66295. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  66296. + (unsigned long)addr, DWC_READ_REG32(addr));
  66297. + addr = &core_if->core_global_regs->gsnpsid;
  66298. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66299. + DWC_READ_REG32(addr));
  66300. + addr = &core_if->core_global_regs->ghwcfg1;
  66301. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66302. + DWC_READ_REG32(addr));
  66303. + addr = &core_if->core_global_regs->ghwcfg2;
  66304. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66305. + DWC_READ_REG32(addr));
  66306. + addr = &core_if->core_global_regs->ghwcfg3;
  66307. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66308. + DWC_READ_REG32(addr));
  66309. + addr = &core_if->core_global_regs->ghwcfg4;
  66310. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66311. + DWC_READ_REG32(addr));
  66312. + addr = &core_if->core_global_regs->glpmcfg;
  66313. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66314. + DWC_READ_REG32(addr));
  66315. + addr = &core_if->core_global_regs->gpwrdn;
  66316. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66317. + DWC_READ_REG32(addr));
  66318. + addr = &core_if->core_global_regs->gdfifocfg;
  66319. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66320. + DWC_READ_REG32(addr));
  66321. + addr = &core_if->core_global_regs->adpctl;
  66322. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66323. + dwc_otg_adp_read_reg(core_if));
  66324. + addr = &core_if->core_global_regs->hptxfsiz;
  66325. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66326. + DWC_READ_REG32(addr));
  66327. +
  66328. + if (core_if->en_multiple_tx_fifo == 0) {
  66329. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  66330. + txfsiz = "DPTXFSIZ";
  66331. + } else {
  66332. + ep_num = core_if->hwcfg4.b.num_in_eps;
  66333. + txfsiz = "DIENPTXF";
  66334. + }
  66335. + for (i = 0; i < ep_num; i++) {
  66336. + addr = &core_if->core_global_regs->dtxfsiz[i];
  66337. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  66338. + (unsigned long)addr, DWC_READ_REG32(addr));
  66339. + }
  66340. + addr = core_if->pcgcctl;
  66341. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66342. + DWC_READ_REG32(addr));
  66343. +}
  66344. +
  66345. +/**
  66346. + * Flush a Tx FIFO.
  66347. + *
  66348. + * @param core_if Programming view of DWC_otg controller.
  66349. + * @param num Tx FIFO to flush.
  66350. + */
  66351. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  66352. +{
  66353. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  66354. + volatile grstctl_t greset = {.d32 = 0 };
  66355. + int count = 0;
  66356. +
  66357. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  66358. +
  66359. + greset.b.txfflsh = 1;
  66360. + greset.b.txfnum = num;
  66361. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  66362. +
  66363. + do {
  66364. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  66365. + if (++count > 10000) {
  66366. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  66367. + __func__, greset.d32,
  66368. + DWC_READ_REG32(&global_regs->gnptxsts));
  66369. + break;
  66370. + }
  66371. + dwc_udelay(1);
  66372. + } while (greset.b.txfflsh == 1);
  66373. +
  66374. + /* Wait for 3 PHY Clocks */
  66375. + dwc_udelay(1);
  66376. +}
  66377. +
  66378. +/**
  66379. + * Flush Rx FIFO.
  66380. + *
  66381. + * @param core_if Programming view of DWC_otg controller.
  66382. + */
  66383. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  66384. +{
  66385. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  66386. + volatile grstctl_t greset = {.d32 = 0 };
  66387. + int count = 0;
  66388. +
  66389. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  66390. + /*
  66391. + *
  66392. + */
  66393. + greset.b.rxfflsh = 1;
  66394. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  66395. +
  66396. + do {
  66397. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  66398. + if (++count > 10000) {
  66399. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  66400. + greset.d32);
  66401. + break;
  66402. + }
  66403. + dwc_udelay(1);
  66404. + } while (greset.b.rxfflsh == 1);
  66405. +
  66406. + /* Wait for 3 PHY Clocks */
  66407. + dwc_udelay(1);
  66408. +}
  66409. +
  66410. +/**
  66411. + * Do core a soft reset of the core. Be careful with this because it
  66412. + * resets all the internal state machines of the core.
  66413. + */
  66414. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  66415. +{
  66416. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  66417. + volatile grstctl_t greset = {.d32 = 0 };
  66418. + int count = 0;
  66419. +
  66420. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  66421. + /* Wait for AHB master IDLE state. */
  66422. + do {
  66423. + dwc_udelay(10);
  66424. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  66425. + if (++count > 100000) {
  66426. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  66427. + greset.d32);
  66428. + return;
  66429. + }
  66430. + }
  66431. + while (greset.b.ahbidle == 0);
  66432. +
  66433. + /* Core Soft Reset */
  66434. + count = 0;
  66435. + greset.b.csftrst = 1;
  66436. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  66437. + do {
  66438. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  66439. + if (++count > 10000) {
  66440. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  66441. + __func__, greset.d32);
  66442. + break;
  66443. + }
  66444. + dwc_udelay(1);
  66445. + }
  66446. + while (greset.b.csftrst == 1);
  66447. +
  66448. + /* Wait for 3 PHY Clocks */
  66449. + dwc_mdelay(100);
  66450. +}
  66451. +
  66452. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  66453. +{
  66454. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  66455. +}
  66456. +
  66457. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  66458. +{
  66459. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  66460. +}
  66461. +
  66462. +/**
  66463. + * Register HCD callbacks. The callbacks are used to start and stop
  66464. + * the HCD for interrupt processing.
  66465. + *
  66466. + * @param core_if Programming view of DWC_otg controller.
  66467. + * @param cb the HCD callback structure.
  66468. + * @param p pointer to be passed to callback function (usb_hcd*).
  66469. + */
  66470. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  66471. + dwc_otg_cil_callbacks_t * cb, void *p)
  66472. +{
  66473. + core_if->hcd_cb = cb;
  66474. + cb->p = p;
  66475. +}
  66476. +
  66477. +/**
  66478. + * Register PCD callbacks. The callbacks are used to start and stop
  66479. + * the PCD for interrupt processing.
  66480. + *
  66481. + * @param core_if Programming view of DWC_otg controller.
  66482. + * @param cb the PCD callback structure.
  66483. + * @param p pointer to be passed to callback function (pcd*).
  66484. + */
  66485. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  66486. + dwc_otg_cil_callbacks_t * cb, void *p)
  66487. +{
  66488. + core_if->pcd_cb = cb;
  66489. + cb->p = p;
  66490. +}
  66491. +
  66492. +#ifdef DWC_EN_ISOC
  66493. +
  66494. +/**
  66495. + * This function writes isoc data per 1 (micro)frame into tx fifo
  66496. + *
  66497. + * @param core_if Programming view of DWC_otg controller.
  66498. + * @param ep The EP to start the transfer on.
  66499. + *
  66500. + */
  66501. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66502. +{
  66503. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  66504. + dtxfsts_data_t txstatus = {.d32 = 0 };
  66505. + uint32_t len = 0;
  66506. + uint32_t dwords;
  66507. +
  66508. + ep->xfer_len = ep->data_per_frame;
  66509. + ep->xfer_count = 0;
  66510. +
  66511. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  66512. +
  66513. + len = ep->xfer_len - ep->xfer_count;
  66514. +
  66515. + if (len > ep->maxpacket) {
  66516. + len = ep->maxpacket;
  66517. + }
  66518. +
  66519. + dwords = (len + 3) / 4;
  66520. +
  66521. + /* While there is space in the queue and space in the FIFO and
  66522. + * More data to tranfer, Write packets to the Tx FIFO */
  66523. + txstatus.d32 =
  66524. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  66525. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  66526. +
  66527. + while (txstatus.b.txfspcavail > dwords &&
  66528. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  66529. + /* Write the FIFO */
  66530. + dwc_otg_ep_write_packet(core_if, ep, 0);
  66531. +
  66532. + len = ep->xfer_len - ep->xfer_count;
  66533. + if (len > ep->maxpacket) {
  66534. + len = ep->maxpacket;
  66535. + }
  66536. +
  66537. + dwords = (len + 3) / 4;
  66538. + txstatus.d32 =
  66539. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  66540. + dtxfsts);
  66541. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  66542. + txstatus.d32);
  66543. + }
  66544. +}
  66545. +
  66546. +/**
  66547. + * This function initializes a descriptor chain for Isochronous transfer
  66548. + *
  66549. + * @param core_if Programming view of DWC_otg controller.
  66550. + * @param ep The EP to start the transfer on.
  66551. + *
  66552. + */
  66553. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  66554. + dwc_ep_t * ep)
  66555. +{
  66556. + deptsiz_data_t deptsiz = {.d32 = 0 };
  66557. + depctl_data_t depctl = {.d32 = 0 };
  66558. + dsts_data_t dsts = {.d32 = 0 };
  66559. + volatile uint32_t *addr;
  66560. +
  66561. + if (ep->is_in) {
  66562. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  66563. + } else {
  66564. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  66565. + }
  66566. +
  66567. + ep->xfer_len = ep->data_per_frame;
  66568. + ep->xfer_count = 0;
  66569. + ep->xfer_buff = ep->cur_pkt_addr;
  66570. + ep->dma_addr = ep->cur_pkt_dma_addr;
  66571. +
  66572. + if (ep->is_in) {
  66573. + /* Program the transfer size and packet count
  66574. + * as follows: xfersize = N * maxpacket +
  66575. + * short_packet pktcnt = N + (short_packet
  66576. + * exist ? 1 : 0)
  66577. + */
  66578. + deptsiz.b.xfersize = ep->xfer_len;
  66579. + deptsiz.b.pktcnt =
  66580. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  66581. + deptsiz.b.mc = deptsiz.b.pktcnt;
  66582. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  66583. + deptsiz.d32);
  66584. +
  66585. + /* Write the DMA register */
  66586. + if (core_if->dma_enable) {
  66587. + DWC_WRITE_REG32(&
  66588. + (core_if->dev_if->in_ep_regs[ep->num]->
  66589. + diepdma), (uint32_t) ep->dma_addr);
  66590. + }
  66591. + } else {
  66592. + deptsiz.b.pktcnt =
  66593. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  66594. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  66595. +
  66596. + DWC_WRITE_REG32(&core_if->dev_if->
  66597. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  66598. +
  66599. + if (core_if->dma_enable) {
  66600. + DWC_WRITE_REG32(&
  66601. + (core_if->dev_if->
  66602. + out_ep_regs[ep->num]->doepdma),
  66603. + (uint32_t) ep->dma_addr);
  66604. + }
  66605. + }
  66606. +
  66607. + /** Enable endpoint, clear nak */
  66608. +
  66609. + depctl.d32 = 0;
  66610. + if (ep->bInterval == 1) {
  66611. + dsts.d32 =
  66612. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  66613. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  66614. +
  66615. + if (ep->next_frame & 0x1) {
  66616. + depctl.b.setd1pid = 1;
  66617. + } else {
  66618. + depctl.b.setd0pid = 1;
  66619. + }
  66620. + } else {
  66621. + ep->next_frame += ep->bInterval;
  66622. +
  66623. + if (ep->next_frame & 0x1) {
  66624. + depctl.b.setd1pid = 1;
  66625. + } else {
  66626. + depctl.b.setd0pid = 1;
  66627. + }
  66628. + }
  66629. + depctl.b.epena = 1;
  66630. + depctl.b.cnak = 1;
  66631. +
  66632. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  66633. + depctl.d32 = DWC_READ_REG32(addr);
  66634. +
  66635. + if (ep->is_in && core_if->dma_enable == 0) {
  66636. + write_isoc_frame_data(core_if, ep);
  66637. + }
  66638. +
  66639. +}
  66640. +#endif /* DWC_EN_ISOC */
  66641. +
  66642. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  66643. +{
  66644. + int i;
  66645. + for (i = 0; i < size; i++) {
  66646. + p[i] = -1;
  66647. + }
  66648. +}
  66649. +
  66650. +static int dwc_otg_param_initialized(int32_t val)
  66651. +{
  66652. + return val != -1;
  66653. +}
  66654. +
  66655. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  66656. +{
  66657. + int i;
  66658. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  66659. + if (!core_if->core_params) {
  66660. + return -DWC_E_NO_MEMORY;
  66661. + }
  66662. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  66663. + sizeof(*core_if->core_params) /
  66664. + sizeof(int32_t));
  66665. + DWC_PRINTF("Setting default values for core params\n");
  66666. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  66667. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  66668. + dwc_otg_set_param_dma_desc_enable(core_if,
  66669. + dwc_param_dma_desc_enable_default);
  66670. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  66671. + dwc_otg_set_param_dma_burst_size(core_if,
  66672. + dwc_param_dma_burst_size_default);
  66673. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  66674. + dwc_param_host_support_fs_ls_low_power_default);
  66675. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  66676. + dwc_param_enable_dynamic_fifo_default);
  66677. + dwc_otg_set_param_data_fifo_size(core_if,
  66678. + dwc_param_data_fifo_size_default);
  66679. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  66680. + dwc_param_dev_rx_fifo_size_default);
  66681. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  66682. + dwc_param_dev_nperio_tx_fifo_size_default);
  66683. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  66684. + dwc_param_host_rx_fifo_size_default);
  66685. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  66686. + dwc_param_host_nperio_tx_fifo_size_default);
  66687. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  66688. + dwc_param_host_perio_tx_fifo_size_default);
  66689. + dwc_otg_set_param_max_transfer_size(core_if,
  66690. + dwc_param_max_transfer_size_default);
  66691. + dwc_otg_set_param_max_packet_count(core_if,
  66692. + dwc_param_max_packet_count_default);
  66693. + dwc_otg_set_param_host_channels(core_if,
  66694. + dwc_param_host_channels_default);
  66695. + dwc_otg_set_param_dev_endpoints(core_if,
  66696. + dwc_param_dev_endpoints_default);
  66697. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  66698. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  66699. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  66700. + dwc_param_host_ls_low_power_phy_clk_default);
  66701. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  66702. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  66703. + dwc_param_phy_ulpi_ext_vbus_default);
  66704. + dwc_otg_set_param_phy_utmi_width(core_if,
  66705. + dwc_param_phy_utmi_width_default);
  66706. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  66707. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  66708. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  66709. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  66710. + dwc_param_en_multiple_tx_fifo_default);
  66711. + for (i = 0; i < 15; i++) {
  66712. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  66713. + dwc_param_dev_perio_tx_fifo_size_default,
  66714. + i);
  66715. + }
  66716. +
  66717. + for (i = 0; i < 15; i++) {
  66718. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  66719. + dwc_param_dev_tx_fifo_size_default,
  66720. + i);
  66721. + }
  66722. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  66723. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  66724. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  66725. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  66726. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  66727. + dwc_otg_set_param_tx_thr_length(core_if,
  66728. + dwc_param_tx_thr_length_default);
  66729. + dwc_otg_set_param_rx_thr_length(core_if,
  66730. + dwc_param_rx_thr_length_default);
  66731. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  66732. + dwc_param_ahb_thr_ratio_default);
  66733. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  66734. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  66735. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  66736. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  66737. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  66738. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  66739. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  66740. + DWC_PRINTF("Finished setting default values for core params\n");
  66741. +
  66742. + return 0;
  66743. +}
  66744. +
  66745. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  66746. +{
  66747. + return core_if->dma_enable;
  66748. +}
  66749. +
  66750. +/* Checks if the parameter is outside of its valid range of values */
  66751. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  66752. + (((_param_) < (_low_)) || \
  66753. + ((_param_) > (_high_)))
  66754. +
  66755. +/* Parameter access functions */
  66756. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  66757. +{
  66758. + int valid;
  66759. + int retval = 0;
  66760. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  66761. + DWC_WARN("Wrong value for otg_cap parameter\n");
  66762. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  66763. + retval = -DWC_E_INVALID;
  66764. + goto out;
  66765. + }
  66766. +
  66767. + valid = 1;
  66768. + switch (val) {
  66769. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  66770. + if (core_if->hwcfg2.b.op_mode !=
  66771. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  66772. + valid = 0;
  66773. + break;
  66774. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  66775. + if ((core_if->hwcfg2.b.op_mode !=
  66776. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  66777. + && (core_if->hwcfg2.b.op_mode !=
  66778. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  66779. + && (core_if->hwcfg2.b.op_mode !=
  66780. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  66781. + && (core_if->hwcfg2.b.op_mode !=
  66782. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  66783. + valid = 0;
  66784. + }
  66785. + break;
  66786. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  66787. + /* always valid */
  66788. + break;
  66789. + }
  66790. + if (!valid) {
  66791. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  66792. + DWC_ERROR
  66793. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  66794. + val);
  66795. + }
  66796. + val =
  66797. + (((core_if->hwcfg2.b.op_mode ==
  66798. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  66799. + || (core_if->hwcfg2.b.op_mode ==
  66800. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  66801. + || (core_if->hwcfg2.b.op_mode ==
  66802. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  66803. + || (core_if->hwcfg2.b.op_mode ==
  66804. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  66805. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  66806. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  66807. + retval = -DWC_E_INVALID;
  66808. + }
  66809. +
  66810. + core_if->core_params->otg_cap = val;
  66811. +out:
  66812. + return retval;
  66813. +}
  66814. +
  66815. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  66816. +{
  66817. + return core_if->core_params->otg_cap;
  66818. +}
  66819. +
  66820. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  66821. +{
  66822. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  66823. + DWC_WARN("Wrong value for opt parameter\n");
  66824. + return -DWC_E_INVALID;
  66825. + }
  66826. + core_if->core_params->opt = val;
  66827. + return 0;
  66828. +}
  66829. +
  66830. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  66831. +{
  66832. + return core_if->core_params->opt;
  66833. +}
  66834. +
  66835. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  66836. +{
  66837. + int retval = 0;
  66838. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  66839. + DWC_WARN("Wrong value for dma enable\n");
  66840. + return -DWC_E_INVALID;
  66841. + }
  66842. +
  66843. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  66844. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  66845. + DWC_ERROR
  66846. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  66847. + val);
  66848. + }
  66849. + val = 0;
  66850. + retval = -DWC_E_INVALID;
  66851. + }
  66852. +
  66853. + core_if->core_params->dma_enable = val;
  66854. + if (val == 0) {
  66855. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  66856. + }
  66857. + return retval;
  66858. +}
  66859. +
  66860. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  66861. +{
  66862. + return core_if->core_params->dma_enable;
  66863. +}
  66864. +
  66865. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  66866. +{
  66867. + int retval = 0;
  66868. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  66869. + DWC_WARN("Wrong value for dma_enable\n");
  66870. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  66871. + return -DWC_E_INVALID;
  66872. + }
  66873. +
  66874. + if ((val == 1)
  66875. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  66876. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  66877. + if (dwc_otg_param_initialized
  66878. + (core_if->core_params->dma_desc_enable)) {
  66879. + DWC_ERROR
  66880. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  66881. + val);
  66882. + }
  66883. + val = 0;
  66884. + retval = -DWC_E_INVALID;
  66885. + }
  66886. + core_if->core_params->dma_desc_enable = val;
  66887. + return retval;
  66888. +}
  66889. +
  66890. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  66891. +{
  66892. + return core_if->core_params->dma_desc_enable;
  66893. +}
  66894. +
  66895. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  66896. + int32_t val)
  66897. +{
  66898. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  66899. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  66900. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  66901. + return -DWC_E_INVALID;
  66902. + }
  66903. + core_if->core_params->host_support_fs_ls_low_power = val;
  66904. + return 0;
  66905. +}
  66906. +
  66907. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  66908. + core_if)
  66909. +{
  66910. + return core_if->core_params->host_support_fs_ls_low_power;
  66911. +}
  66912. +
  66913. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  66914. + int32_t val)
  66915. +{
  66916. + int retval = 0;
  66917. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  66918. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  66919. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  66920. + return -DWC_E_INVALID;
  66921. + }
  66922. +
  66923. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  66924. + if (dwc_otg_param_initialized
  66925. + (core_if->core_params->enable_dynamic_fifo)) {
  66926. + DWC_ERROR
  66927. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  66928. + val);
  66929. + }
  66930. + val = 0;
  66931. + retval = -DWC_E_INVALID;
  66932. + }
  66933. + core_if->core_params->enable_dynamic_fifo = val;
  66934. + return retval;
  66935. +}
  66936. +
  66937. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  66938. +{
  66939. + return core_if->core_params->enable_dynamic_fifo;
  66940. +}
  66941. +
  66942. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  66943. +{
  66944. + int retval = 0;
  66945. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  66946. + DWC_WARN("Wrong value for data_fifo_size\n");
  66947. + DWC_WARN("data_fifo_size must be 32-32768\n");
  66948. + return -DWC_E_INVALID;
  66949. + }
  66950. +
  66951. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  66952. + if (dwc_otg_param_initialized
  66953. + (core_if->core_params->data_fifo_size)) {
  66954. + DWC_ERROR
  66955. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  66956. + val);
  66957. + }
  66958. + val = core_if->hwcfg3.b.dfifo_depth;
  66959. + retval = -DWC_E_INVALID;
  66960. + }
  66961. +
  66962. + core_if->core_params->data_fifo_size = val;
  66963. + return retval;
  66964. +}
  66965. +
  66966. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  66967. +{
  66968. + return core_if->core_params->data_fifo_size;
  66969. +}
  66970. +
  66971. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  66972. +{
  66973. + int retval = 0;
  66974. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  66975. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  66976. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  66977. + return -DWC_E_INVALID;
  66978. + }
  66979. +
  66980. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  66981. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  66982. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  66983. + }
  66984. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  66985. + retval = -DWC_E_INVALID;
  66986. + }
  66987. +
  66988. + core_if->core_params->dev_rx_fifo_size = val;
  66989. + return retval;
  66990. +}
  66991. +
  66992. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  66993. +{
  66994. + return core_if->core_params->dev_rx_fifo_size;
  66995. +}
  66996. +
  66997. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  66998. + int32_t val)
  66999. +{
  67000. + int retval = 0;
  67001. +
  67002. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67003. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  67004. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  67005. + return -DWC_E_INVALID;
  67006. + }
  67007. +
  67008. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  67009. + if (dwc_otg_param_initialized
  67010. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  67011. + DWC_ERROR
  67012. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  67013. + val);
  67014. + }
  67015. + val =
  67016. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  67017. + 16);
  67018. + retval = -DWC_E_INVALID;
  67019. + }
  67020. +
  67021. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  67022. + return retval;
  67023. +}
  67024. +
  67025. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  67026. +{
  67027. + return core_if->core_params->dev_nperio_tx_fifo_size;
  67028. +}
  67029. +
  67030. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  67031. + int32_t val)
  67032. +{
  67033. + int retval = 0;
  67034. +
  67035. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67036. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  67037. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  67038. + return -DWC_E_INVALID;
  67039. + }
  67040. +
  67041. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  67042. + if (dwc_otg_param_initialized
  67043. + (core_if->core_params->host_rx_fifo_size)) {
  67044. + DWC_ERROR
  67045. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  67046. + val);
  67047. + }
  67048. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  67049. + retval = -DWC_E_INVALID;
  67050. + }
  67051. +
  67052. + core_if->core_params->host_rx_fifo_size = val;
  67053. + return retval;
  67054. +
  67055. +}
  67056. +
  67057. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  67058. +{
  67059. + return core_if->core_params->host_rx_fifo_size;
  67060. +}
  67061. +
  67062. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67063. + int32_t val)
  67064. +{
  67065. + int retval = 0;
  67066. +
  67067. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67068. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  67069. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  67070. + return -DWC_E_INVALID;
  67071. + }
  67072. +
  67073. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  67074. + if (dwc_otg_param_initialized
  67075. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  67076. + DWC_ERROR
  67077. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  67078. + val);
  67079. + }
  67080. + val =
  67081. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  67082. + 16);
  67083. + retval = -DWC_E_INVALID;
  67084. + }
  67085. +
  67086. + core_if->core_params->host_nperio_tx_fifo_size = val;
  67087. + return retval;
  67088. +}
  67089. +
  67090. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  67091. +{
  67092. + return core_if->core_params->host_nperio_tx_fifo_size;
  67093. +}
  67094. +
  67095. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67096. + int32_t val)
  67097. +{
  67098. + int retval = 0;
  67099. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67100. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  67101. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  67102. + return -DWC_E_INVALID;
  67103. + }
  67104. +
  67105. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  67106. + if (dwc_otg_param_initialized
  67107. + (core_if->core_params->host_perio_tx_fifo_size)) {
  67108. + DWC_ERROR
  67109. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  67110. + val);
  67111. + }
  67112. + val = (core_if->hptxfsiz.d32) >> 16;
  67113. + retval = -DWC_E_INVALID;
  67114. + }
  67115. +
  67116. + core_if->core_params->host_perio_tx_fifo_size = val;
  67117. + return retval;
  67118. +}
  67119. +
  67120. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  67121. +{
  67122. + return core_if->core_params->host_perio_tx_fifo_size;
  67123. +}
  67124. +
  67125. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  67126. + int32_t val)
  67127. +{
  67128. + int retval = 0;
  67129. +
  67130. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  67131. + DWC_WARN("Wrong value for max_transfer_size\n");
  67132. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  67133. + return -DWC_E_INVALID;
  67134. + }
  67135. +
  67136. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  67137. + if (dwc_otg_param_initialized
  67138. + (core_if->core_params->max_transfer_size)) {
  67139. + DWC_ERROR
  67140. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  67141. + val);
  67142. + }
  67143. + val =
  67144. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  67145. + 1);
  67146. + retval = -DWC_E_INVALID;
  67147. + }
  67148. +
  67149. + core_if->core_params->max_transfer_size = val;
  67150. + return retval;
  67151. +}
  67152. +
  67153. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  67154. +{
  67155. + return core_if->core_params->max_transfer_size;
  67156. +}
  67157. +
  67158. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  67159. +{
  67160. + int retval = 0;
  67161. +
  67162. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  67163. + DWC_WARN("Wrong value for max_packet_count\n");
  67164. + DWC_WARN("max_packet_count must be 15-511\n");
  67165. + return -DWC_E_INVALID;
  67166. + }
  67167. +
  67168. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  67169. + if (dwc_otg_param_initialized
  67170. + (core_if->core_params->max_packet_count)) {
  67171. + DWC_ERROR
  67172. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  67173. + val);
  67174. + }
  67175. + val =
  67176. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  67177. + retval = -DWC_E_INVALID;
  67178. + }
  67179. +
  67180. + core_if->core_params->max_packet_count = val;
  67181. + return retval;
  67182. +}
  67183. +
  67184. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  67185. +{
  67186. + return core_if->core_params->max_packet_count;
  67187. +}
  67188. +
  67189. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  67190. +{
  67191. + int retval = 0;
  67192. +
  67193. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  67194. + DWC_WARN("Wrong value for host_channels\n");
  67195. + DWC_WARN("host_channels must be 1-16\n");
  67196. + return -DWC_E_INVALID;
  67197. + }
  67198. +
  67199. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  67200. + if (dwc_otg_param_initialized
  67201. + (core_if->core_params->host_channels)) {
  67202. + DWC_ERROR
  67203. + ("%d invalid for host_channels. Check HW configurations.\n",
  67204. + val);
  67205. + }
  67206. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  67207. + retval = -DWC_E_INVALID;
  67208. + }
  67209. +
  67210. + core_if->core_params->host_channels = val;
  67211. + return retval;
  67212. +}
  67213. +
  67214. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  67215. +{
  67216. + return core_if->core_params->host_channels;
  67217. +}
  67218. +
  67219. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  67220. +{
  67221. + int retval = 0;
  67222. +
  67223. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  67224. + DWC_WARN("Wrong value for dev_endpoints\n");
  67225. + DWC_WARN("dev_endpoints must be 1-15\n");
  67226. + return -DWC_E_INVALID;
  67227. + }
  67228. +
  67229. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  67230. + if (dwc_otg_param_initialized
  67231. + (core_if->core_params->dev_endpoints)) {
  67232. + DWC_ERROR
  67233. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  67234. + val);
  67235. + }
  67236. + val = core_if->hwcfg2.b.num_dev_ep;
  67237. + retval = -DWC_E_INVALID;
  67238. + }
  67239. +
  67240. + core_if->core_params->dev_endpoints = val;
  67241. + return retval;
  67242. +}
  67243. +
  67244. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  67245. +{
  67246. + return core_if->core_params->dev_endpoints;
  67247. +}
  67248. +
  67249. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  67250. +{
  67251. + int retval = 0;
  67252. + int valid = 0;
  67253. +
  67254. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  67255. + DWC_WARN("Wrong value for phy_type\n");
  67256. + DWC_WARN("phy_type must be 0,1 or 2\n");
  67257. + return -DWC_E_INVALID;
  67258. + }
  67259. +#ifndef NO_FS_PHY_HW_CHECKS
  67260. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  67261. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  67262. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  67263. + valid = 1;
  67264. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  67265. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  67266. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  67267. + valid = 1;
  67268. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  67269. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  67270. + valid = 1;
  67271. + }
  67272. + if (!valid) {
  67273. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  67274. + DWC_ERROR
  67275. + ("%d invalid for phy_type. Check HW configurations.\n",
  67276. + val);
  67277. + }
  67278. + if (core_if->hwcfg2.b.hs_phy_type) {
  67279. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  67280. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  67281. + val = DWC_PHY_TYPE_PARAM_UTMI;
  67282. + } else {
  67283. + val = DWC_PHY_TYPE_PARAM_ULPI;
  67284. + }
  67285. + }
  67286. + retval = -DWC_E_INVALID;
  67287. + }
  67288. +#endif
  67289. + core_if->core_params->phy_type = val;
  67290. + return retval;
  67291. +}
  67292. +
  67293. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  67294. +{
  67295. + return core_if->core_params->phy_type;
  67296. +}
  67297. +
  67298. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  67299. +{
  67300. + int retval = 0;
  67301. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67302. + DWC_WARN("Wrong value for speed parameter\n");
  67303. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  67304. + return -DWC_E_INVALID;
  67305. + }
  67306. + if ((val == 0)
  67307. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  67308. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  67309. + DWC_ERROR
  67310. + ("%d invalid for speed paremter. Check HW configuration.\n",
  67311. + val);
  67312. + }
  67313. + val =
  67314. + (dwc_otg_get_param_phy_type(core_if) ==
  67315. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  67316. + retval = -DWC_E_INVALID;
  67317. + }
  67318. + core_if->core_params->speed = val;
  67319. + return retval;
  67320. +}
  67321. +
  67322. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  67323. +{
  67324. + return core_if->core_params->speed;
  67325. +}
  67326. +
  67327. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  67328. + int32_t val)
  67329. +{
  67330. + int retval = 0;
  67331. +
  67332. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67333. + DWC_WARN
  67334. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  67335. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  67336. + return -DWC_E_INVALID;
  67337. + }
  67338. +
  67339. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  67340. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  67341. + if (dwc_otg_param_initialized
  67342. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  67343. + DWC_ERROR
  67344. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  67345. + val);
  67346. + }
  67347. + val =
  67348. + (dwc_otg_get_param_phy_type(core_if) ==
  67349. + DWC_PHY_TYPE_PARAM_FS) ?
  67350. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  67351. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  67352. + retval = -DWC_E_INVALID;
  67353. + }
  67354. +
  67355. + core_if->core_params->host_ls_low_power_phy_clk = val;
  67356. + return retval;
  67357. +}
  67358. +
  67359. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  67360. +{
  67361. + return core_if->core_params->host_ls_low_power_phy_clk;
  67362. +}
  67363. +
  67364. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  67365. +{
  67366. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67367. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  67368. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  67369. + return -DWC_E_INVALID;
  67370. + }
  67371. +
  67372. + core_if->core_params->phy_ulpi_ddr = val;
  67373. + return 0;
  67374. +}
  67375. +
  67376. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  67377. +{
  67378. + return core_if->core_params->phy_ulpi_ddr;
  67379. +}
  67380. +
  67381. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  67382. + int32_t val)
  67383. +{
  67384. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67385. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  67386. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  67387. + return -DWC_E_INVALID;
  67388. + }
  67389. +
  67390. + core_if->core_params->phy_ulpi_ext_vbus = val;
  67391. + return 0;
  67392. +}
  67393. +
  67394. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  67395. +{
  67396. + return core_if->core_params->phy_ulpi_ext_vbus;
  67397. +}
  67398. +
  67399. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  67400. +{
  67401. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  67402. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  67403. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  67404. + return -DWC_E_INVALID;
  67405. + }
  67406. +
  67407. + core_if->core_params->phy_utmi_width = val;
  67408. + return 0;
  67409. +}
  67410. +
  67411. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  67412. +{
  67413. + return core_if->core_params->phy_utmi_width;
  67414. +}
  67415. +
  67416. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  67417. +{
  67418. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67419. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  67420. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  67421. + return -DWC_E_INVALID;
  67422. + }
  67423. +
  67424. + core_if->core_params->ulpi_fs_ls = val;
  67425. + return 0;
  67426. +}
  67427. +
  67428. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  67429. +{
  67430. + return core_if->core_params->ulpi_fs_ls;
  67431. +}
  67432. +
  67433. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  67434. +{
  67435. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67436. + DWC_WARN("Wrong valaue for ts_dline\n");
  67437. + DWC_WARN("ts_dline must be 0 or 1\n");
  67438. + return -DWC_E_INVALID;
  67439. + }
  67440. +
  67441. + core_if->core_params->ts_dline = val;
  67442. + return 0;
  67443. +}
  67444. +
  67445. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  67446. +{
  67447. + return core_if->core_params->ts_dline;
  67448. +}
  67449. +
  67450. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67451. +{
  67452. + int retval = 0;
  67453. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67454. + DWC_WARN("Wrong valaue for i2c_enable\n");
  67455. + DWC_WARN("i2c_enable must be 0 or 1\n");
  67456. + return -DWC_E_INVALID;
  67457. + }
  67458. +#ifndef NO_FS_PHY_HW_CHECK
  67459. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  67460. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  67461. + DWC_ERROR
  67462. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  67463. + val);
  67464. + }
  67465. + val = 0;
  67466. + retval = -DWC_E_INVALID;
  67467. + }
  67468. +#endif
  67469. +
  67470. + core_if->core_params->i2c_enable = val;
  67471. + return retval;
  67472. +}
  67473. +
  67474. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  67475. +{
  67476. + return core_if->core_params->i2c_enable;
  67477. +}
  67478. +
  67479. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67480. + int32_t val, int fifo_num)
  67481. +{
  67482. + int retval = 0;
  67483. +
  67484. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  67485. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  67486. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  67487. + return -DWC_E_INVALID;
  67488. + }
  67489. +
  67490. + if (val >
  67491. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  67492. + if (dwc_otg_param_initialized
  67493. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  67494. + DWC_ERROR
  67495. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  67496. + val, fifo_num);
  67497. + }
  67498. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  67499. + retval = -DWC_E_INVALID;
  67500. + }
  67501. +
  67502. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  67503. + return retval;
  67504. +}
  67505. +
  67506. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67507. + int fifo_num)
  67508. +{
  67509. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  67510. +}
  67511. +
  67512. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  67513. + int32_t val)
  67514. +{
  67515. + int retval = 0;
  67516. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67517. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  67518. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  67519. + return -DWC_E_INVALID;
  67520. + }
  67521. +
  67522. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  67523. + if (dwc_otg_param_initialized
  67524. + (core_if->core_params->en_multiple_tx_fifo)) {
  67525. + DWC_ERROR
  67526. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  67527. + val);
  67528. + }
  67529. + val = 0;
  67530. + retval = -DWC_E_INVALID;
  67531. + }
  67532. +
  67533. + core_if->core_params->en_multiple_tx_fifo = val;
  67534. + return retval;
  67535. +}
  67536. +
  67537. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  67538. +{
  67539. + return core_if->core_params->en_multiple_tx_fifo;
  67540. +}
  67541. +
  67542. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  67543. + int fifo_num)
  67544. +{
  67545. + int retval = 0;
  67546. +
  67547. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  67548. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  67549. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  67550. + return -DWC_E_INVALID;
  67551. + }
  67552. +
  67553. + if (val >
  67554. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  67555. + if (dwc_otg_param_initialized
  67556. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  67557. + DWC_ERROR
  67558. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  67559. + val, fifo_num);
  67560. + }
  67561. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  67562. + retval = -DWC_E_INVALID;
  67563. + }
  67564. +
  67565. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  67566. + return retval;
  67567. +}
  67568. +
  67569. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67570. + int fifo_num)
  67571. +{
  67572. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  67573. +}
  67574. +
  67575. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  67576. +{
  67577. + int retval = 0;
  67578. +
  67579. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  67580. + DWC_WARN("Wrong value for thr_ctl\n");
  67581. + DWC_WARN("thr_ctl must be 0-7\n");
  67582. + return -DWC_E_INVALID;
  67583. + }
  67584. +
  67585. + if ((val != 0) &&
  67586. + (!dwc_otg_get_param_dma_enable(core_if) ||
  67587. + !core_if->hwcfg4.b.ded_fifo_en)) {
  67588. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  67589. + DWC_ERROR
  67590. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  67591. + val);
  67592. + }
  67593. + val = 0;
  67594. + retval = -DWC_E_INVALID;
  67595. + }
  67596. +
  67597. + core_if->core_params->thr_ctl = val;
  67598. + return retval;
  67599. +}
  67600. +
  67601. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  67602. +{
  67603. + return core_if->core_params->thr_ctl;
  67604. +}
  67605. +
  67606. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67607. +{
  67608. + int retval = 0;
  67609. +
  67610. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67611. + DWC_WARN("Wrong value for lpm_enable\n");
  67612. + DWC_WARN("lpm_enable must be 0 or 1\n");
  67613. + return -DWC_E_INVALID;
  67614. + }
  67615. +
  67616. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  67617. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  67618. + DWC_ERROR
  67619. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  67620. + val);
  67621. + }
  67622. + val = 0;
  67623. + retval = -DWC_E_INVALID;
  67624. + }
  67625. +
  67626. + core_if->core_params->lpm_enable = val;
  67627. + return retval;
  67628. +}
  67629. +
  67630. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  67631. +{
  67632. + return core_if->core_params->lpm_enable;
  67633. +}
  67634. +
  67635. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  67636. +{
  67637. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  67638. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  67639. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  67640. + return -DWC_E_INVALID;
  67641. + }
  67642. +
  67643. + core_if->core_params->tx_thr_length = val;
  67644. + return 0;
  67645. +}
  67646. +
  67647. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  67648. +{
  67649. + return core_if->core_params->tx_thr_length;
  67650. +}
  67651. +
  67652. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  67653. +{
  67654. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  67655. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  67656. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  67657. + return -DWC_E_INVALID;
  67658. + }
  67659. +
  67660. + core_if->core_params->rx_thr_length = val;
  67661. + return 0;
  67662. +}
  67663. +
  67664. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  67665. +{
  67666. + return core_if->core_params->rx_thr_length;
  67667. +}
  67668. +
  67669. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  67670. +{
  67671. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  67672. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  67673. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  67674. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  67675. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  67676. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  67677. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  67678. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  67679. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  67680. + return -DWC_E_INVALID;
  67681. + }
  67682. + core_if->core_params->dma_burst_size = val;
  67683. + return 0;
  67684. +}
  67685. +
  67686. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  67687. +{
  67688. + return core_if->core_params->dma_burst_size;
  67689. +}
  67690. +
  67691. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67692. +{
  67693. + int retval = 0;
  67694. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67695. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  67696. + return -DWC_E_INVALID;
  67697. + }
  67698. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  67699. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  67700. + DWC_ERROR
  67701. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  67702. + val);
  67703. + }
  67704. + retval = -DWC_E_INVALID;
  67705. + val = 0;
  67706. + }
  67707. + core_if->core_params->pti_enable = val;
  67708. + return retval;
  67709. +}
  67710. +
  67711. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  67712. +{
  67713. + return core_if->core_params->pti_enable;
  67714. +}
  67715. +
  67716. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67717. +{
  67718. + int retval = 0;
  67719. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67720. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  67721. + return -DWC_E_INVALID;
  67722. + }
  67723. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  67724. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  67725. + DWC_ERROR
  67726. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  67727. + val);
  67728. + }
  67729. + retval = -DWC_E_INVALID;
  67730. + val = 0;
  67731. + }
  67732. + core_if->core_params->mpi_enable = val;
  67733. + return retval;
  67734. +}
  67735. +
  67736. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  67737. +{
  67738. + return core_if->core_params->mpi_enable;
  67739. +}
  67740. +
  67741. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67742. +{
  67743. + int retval = 0;
  67744. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67745. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  67746. + return -DWC_E_INVALID;
  67747. + }
  67748. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  67749. + if (dwc_otg_param_initialized
  67750. + (core_if->core_params->adp_supp_enable)) {
  67751. + DWC_ERROR
  67752. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  67753. + val);
  67754. + }
  67755. + retval = -DWC_E_INVALID;
  67756. + val = 0;
  67757. + }
  67758. + core_if->core_params->adp_supp_enable = val;
  67759. + /*Set OTG version 2.0 in case of enabling ADP*/
  67760. + if (val)
  67761. + dwc_otg_set_param_otg_ver(core_if, 1);
  67762. +
  67763. + return retval;
  67764. +}
  67765. +
  67766. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  67767. +{
  67768. + return core_if->core_params->adp_supp_enable;
  67769. +}
  67770. +
  67771. +int dwc_otg_set_param_ic_usb_cap(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("`%d' invalid for parameter `ic_usb_cap'\n", val);
  67776. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  67777. + return -DWC_E_INVALID;
  67778. + }
  67779. +
  67780. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  67781. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  67782. + DWC_ERROR
  67783. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  67784. + val);
  67785. + }
  67786. + retval = -DWC_E_INVALID;
  67787. + val = 0;
  67788. + }
  67789. + core_if->core_params->ic_usb_cap = val;
  67790. + return retval;
  67791. +}
  67792. +
  67793. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  67794. +{
  67795. + return core_if->core_params->ic_usb_cap;
  67796. +}
  67797. +
  67798. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  67799. +{
  67800. + int retval = 0;
  67801. + int valid = 1;
  67802. +
  67803. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  67804. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  67805. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  67806. + return -DWC_E_INVALID;
  67807. + }
  67808. +
  67809. + if (val
  67810. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  67811. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  67812. + valid = 0;
  67813. + } else if (val
  67814. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  67815. + 4)) {
  67816. + valid = 0;
  67817. + }
  67818. + if (valid == 0) {
  67819. + if (dwc_otg_param_initialized
  67820. + (core_if->core_params->ahb_thr_ratio)) {
  67821. + DWC_ERROR
  67822. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  67823. + val);
  67824. + }
  67825. + retval = -DWC_E_INVALID;
  67826. + val = 0;
  67827. + }
  67828. +
  67829. + core_if->core_params->ahb_thr_ratio = val;
  67830. + return retval;
  67831. +}
  67832. +
  67833. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  67834. +{
  67835. + return core_if->core_params->ahb_thr_ratio;
  67836. +}
  67837. +
  67838. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  67839. +{
  67840. + int retval = 0;
  67841. + int valid = 1;
  67842. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  67843. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  67844. +
  67845. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  67846. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  67847. + DWC_WARN("power_down must be 0 - 2\n");
  67848. + return -DWC_E_INVALID;
  67849. + }
  67850. +
  67851. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  67852. + valid = 0;
  67853. + }
  67854. + if ((val == 3)
  67855. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  67856. + || (hwcfg4.b.xhiber == 0))) {
  67857. + valid = 0;
  67858. + }
  67859. + if (valid == 0) {
  67860. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  67861. + DWC_ERROR
  67862. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  67863. + val);
  67864. + }
  67865. + retval = -DWC_E_INVALID;
  67866. + val = 0;
  67867. + }
  67868. + core_if->core_params->power_down = val;
  67869. + return retval;
  67870. +}
  67871. +
  67872. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  67873. +{
  67874. + return core_if->core_params->power_down;
  67875. +}
  67876. +
  67877. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  67878. +{
  67879. + int retval = 0;
  67880. + int valid = 1;
  67881. +
  67882. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67883. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  67884. + DWC_WARN("reload_ctl must be 0 or 1\n");
  67885. + return -DWC_E_INVALID;
  67886. + }
  67887. +
  67888. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  67889. + valid = 0;
  67890. + }
  67891. + if (valid == 0) {
  67892. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  67893. + DWC_ERROR("%d invalid for parameter reload_ctl."
  67894. + "Check HW configuration.\n", val);
  67895. + }
  67896. + retval = -DWC_E_INVALID;
  67897. + val = 0;
  67898. + }
  67899. + core_if->core_params->reload_ctl = val;
  67900. + return retval;
  67901. +}
  67902. +
  67903. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  67904. +{
  67905. + return core_if->core_params->reload_ctl;
  67906. +}
  67907. +
  67908. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  67909. +{
  67910. + int retval = 0;
  67911. + int valid = 1;
  67912. +
  67913. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67914. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  67915. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  67916. + return -DWC_E_INVALID;
  67917. + }
  67918. +
  67919. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  67920. + !(core_if->core_params->dma_desc_enable))) {
  67921. + valid = 0;
  67922. + }
  67923. + if (valid == 0) {
  67924. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  67925. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  67926. + "Check HW configuration.\n", val);
  67927. + }
  67928. + retval = -DWC_E_INVALID;
  67929. + val = 0;
  67930. + }
  67931. + core_if->core_params->dev_out_nak = val;
  67932. + return retval;
  67933. +}
  67934. +
  67935. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  67936. +{
  67937. + return core_if->core_params->dev_out_nak;
  67938. +}
  67939. +
  67940. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  67941. +{
  67942. + int retval = 0;
  67943. + int valid = 1;
  67944. +
  67945. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67946. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  67947. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  67948. + return -DWC_E_INVALID;
  67949. + }
  67950. +
  67951. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  67952. + !(core_if->core_params->dma_desc_enable))) {
  67953. + valid = 0;
  67954. + }
  67955. + if (valid == 0) {
  67956. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  67957. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  67958. + "Check HW configuration.\n", val);
  67959. + }
  67960. + retval = -DWC_E_INVALID;
  67961. + val = 0;
  67962. + }
  67963. + core_if->core_params->cont_on_bna = val;
  67964. + return retval;
  67965. +}
  67966. +
  67967. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  67968. +{
  67969. + return core_if->core_params->cont_on_bna;
  67970. +}
  67971. +
  67972. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  67973. +{
  67974. + int retval = 0;
  67975. + int valid = 1;
  67976. +
  67977. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67978. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  67979. + DWC_WARN("ahb_single must be 0 or 1\n");
  67980. + return -DWC_E_INVALID;
  67981. + }
  67982. +
  67983. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  67984. + valid = 0;
  67985. + }
  67986. + if (valid == 0) {
  67987. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  67988. + DWC_ERROR("%d invalid for parameter ahb_single."
  67989. + "Check HW configuration.\n", val);
  67990. + }
  67991. + retval = -DWC_E_INVALID;
  67992. + val = 0;
  67993. + }
  67994. + core_if->core_params->ahb_single = val;
  67995. + return retval;
  67996. +}
  67997. +
  67998. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  67999. +{
  68000. + return core_if->core_params->ahb_single;
  68001. +}
  68002. +
  68003. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  68004. +{
  68005. + int retval = 0;
  68006. +
  68007. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68008. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  68009. + DWC_WARN
  68010. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  68011. + return -DWC_E_INVALID;
  68012. + }
  68013. +
  68014. + core_if->core_params->otg_ver = val;
  68015. + return retval;
  68016. +}
  68017. +
  68018. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  68019. +{
  68020. + return core_if->core_params->otg_ver;
  68021. +}
  68022. +
  68023. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  68024. +{
  68025. + gotgctl_data_t otgctl;
  68026. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68027. + return otgctl.b.hstnegscs;
  68028. +}
  68029. +
  68030. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  68031. +{
  68032. + gotgctl_data_t otgctl;
  68033. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68034. + return otgctl.b.sesreqscs;
  68035. +}
  68036. +
  68037. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  68038. +{
  68039. + if(core_if->otg_ver == 0) {
  68040. + gotgctl_data_t otgctl;
  68041. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68042. + otgctl.b.hnpreq = val;
  68043. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  68044. + } else {
  68045. + core_if->otg_sts = val;
  68046. + }
  68047. +}
  68048. +
  68049. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  68050. +{
  68051. + return core_if->snpsid;
  68052. +}
  68053. +
  68054. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  68055. +{
  68056. + gintsts_data_t gintsts;
  68057. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  68058. + return gintsts.b.curmode;
  68059. +}
  68060. +
  68061. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  68062. +{
  68063. + gusbcfg_data_t usbcfg;
  68064. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  68065. + return usbcfg.b.hnpcap;
  68066. +}
  68067. +
  68068. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  68069. +{
  68070. + gusbcfg_data_t usbcfg;
  68071. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  68072. + usbcfg.b.hnpcap = val;
  68073. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  68074. +}
  68075. +
  68076. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  68077. +{
  68078. + gusbcfg_data_t usbcfg;
  68079. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  68080. + return usbcfg.b.srpcap;
  68081. +}
  68082. +
  68083. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  68084. +{
  68085. + gusbcfg_data_t usbcfg;
  68086. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  68087. + usbcfg.b.srpcap = val;
  68088. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  68089. +}
  68090. +
  68091. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  68092. +{
  68093. + dcfg_data_t dcfg;
  68094. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  68095. +
  68096. + dcfg.d32 = -1; //GRAYG
  68097. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  68098. + if (NULL == core_if)
  68099. + DWC_ERROR("reg request with NULL core_if\n");
  68100. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  68101. + core_if, core_if->dev_if);
  68102. + if (NULL == core_if->dev_if)
  68103. + DWC_ERROR("reg request with NULL dev_if\n");
  68104. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  68105. + "dev_global_regs(%p)\n", __func__,
  68106. + core_if, core_if->dev_if,
  68107. + core_if->dev_if->dev_global_regs);
  68108. + if (NULL == core_if->dev_if->dev_global_regs)
  68109. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  68110. + else {
  68111. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  68112. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  68113. + core_if, core_if->dev_if,
  68114. + core_if->dev_if->dev_global_regs,
  68115. + &core_if->dev_if->dev_global_regs->dcfg);
  68116. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  68117. + }
  68118. + return dcfg.b.devspd;
  68119. +}
  68120. +
  68121. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  68122. +{
  68123. + dcfg_data_t dcfg;
  68124. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  68125. + dcfg.b.devspd = val;
  68126. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  68127. +}
  68128. +
  68129. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  68130. +{
  68131. + hprt0_data_t hprt0;
  68132. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  68133. + return hprt0.b.prtconnsts;
  68134. +}
  68135. +
  68136. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  68137. +{
  68138. + dsts_data_t dsts;
  68139. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  68140. + return dsts.b.enumspd;
  68141. +}
  68142. +
  68143. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  68144. +{
  68145. + hprt0_data_t hprt0;
  68146. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  68147. + return hprt0.b.prtpwr;
  68148. +
  68149. +}
  68150. +
  68151. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  68152. +{
  68153. + return core_if->hibernation_suspend;
  68154. +}
  68155. +
  68156. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  68157. +{
  68158. + hprt0_data_t hprt0;
  68159. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  68160. + hprt0.b.prtpwr = val;
  68161. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68162. +}
  68163. +
  68164. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  68165. +{
  68166. + hprt0_data_t hprt0;
  68167. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  68168. + return hprt0.b.prtsusp;
  68169. +
  68170. +}
  68171. +
  68172. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  68173. +{
  68174. + hprt0_data_t hprt0;
  68175. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  68176. + hprt0.b.prtsusp = val;
  68177. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68178. +}
  68179. +
  68180. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  68181. +{
  68182. + hfir_data_t hfir;
  68183. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  68184. + return hfir.b.frint;
  68185. +
  68186. +}
  68187. +
  68188. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  68189. +{
  68190. + hfir_data_t hfir;
  68191. + uint32_t fram_int;
  68192. + fram_int = calc_frame_interval(core_if);
  68193. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  68194. + if (!core_if->core_params->reload_ctl) {
  68195. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  68196. + "not set to 1.\nShould load driver with reload_ctl=1"
  68197. + " module parameter\n");
  68198. + return;
  68199. + }
  68200. + switch (fram_int) {
  68201. + case 3750:
  68202. + if ((val < 3350) || (val > 4150)) {
  68203. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  68204. + "clock freq should be from 3350 to 4150\n");
  68205. + return;
  68206. + }
  68207. + break;
  68208. + case 30000:
  68209. + if ((val < 26820) || (val > 33180)) {
  68210. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  68211. + "clock freq should be from 26820 to 33180\n");
  68212. + return;
  68213. + }
  68214. + break;
  68215. + case 6000:
  68216. + if ((val < 5360) || (val > 6640)) {
  68217. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  68218. + "clock freq should be from 5360 to 6640\n");
  68219. + return;
  68220. + }
  68221. + break;
  68222. + case 48000:
  68223. + if ((val < 42912) || (val > 53088)) {
  68224. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  68225. + "clock freq should be from 42912 to 53088\n");
  68226. + return;
  68227. + }
  68228. + break;
  68229. + case 7500:
  68230. + if ((val < 6700) || (val > 8300)) {
  68231. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  68232. + "clock freq should be from 6700 to 8300\n");
  68233. + return;
  68234. + }
  68235. + break;
  68236. + case 60000:
  68237. + if ((val < 53640) || (val > 65536)) {
  68238. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  68239. + "clock freq should be from 53640 to 65536\n");
  68240. + return;
  68241. + }
  68242. + break;
  68243. + default:
  68244. + DWC_WARN("Unknown frame interval\n");
  68245. + return;
  68246. + break;
  68247. +
  68248. + }
  68249. + hfir.b.frint = val;
  68250. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  68251. +}
  68252. +
  68253. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  68254. +{
  68255. + hcfg_data_t hcfg;
  68256. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  68257. + return hcfg.b.modechtimen;
  68258. +
  68259. +}
  68260. +
  68261. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  68262. +{
  68263. + hcfg_data_t hcfg;
  68264. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  68265. + hcfg.b.modechtimen = val;
  68266. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  68267. +}
  68268. +
  68269. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  68270. +{
  68271. + hprt0_data_t hprt0;
  68272. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  68273. + hprt0.b.prtres = val;
  68274. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68275. +}
  68276. +
  68277. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  68278. +{
  68279. + dctl_data_t dctl;
  68280. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  68281. + return dctl.b.rmtwkupsig;
  68282. +}
  68283. +
  68284. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  68285. +{
  68286. + glpmcfg_data_t lpmcfg;
  68287. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68288. +
  68289. + DWC_ASSERT(!
  68290. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  68291. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  68292. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  68293. +
  68294. + return lpmcfg.b.prt_sleep_sts;
  68295. +}
  68296. +
  68297. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  68298. +{
  68299. + glpmcfg_data_t lpmcfg;
  68300. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68301. + return lpmcfg.b.rem_wkup_en;
  68302. +}
  68303. +
  68304. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  68305. +{
  68306. + glpmcfg_data_t lpmcfg;
  68307. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68308. + return lpmcfg.b.appl_resp;
  68309. +}
  68310. +
  68311. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  68312. +{
  68313. + glpmcfg_data_t lpmcfg;
  68314. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68315. + lpmcfg.b.appl_resp = val;
  68316. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  68317. +}
  68318. +
  68319. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  68320. +{
  68321. + glpmcfg_data_t lpmcfg;
  68322. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68323. + return lpmcfg.b.hsic_connect;
  68324. +}
  68325. +
  68326. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  68327. +{
  68328. + glpmcfg_data_t lpmcfg;
  68329. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68330. + lpmcfg.b.hsic_connect = val;
  68331. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  68332. +}
  68333. +
  68334. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  68335. +{
  68336. + glpmcfg_data_t lpmcfg;
  68337. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68338. + return lpmcfg.b.inv_sel_hsic;
  68339. +
  68340. +}
  68341. +
  68342. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  68343. +{
  68344. + glpmcfg_data_t lpmcfg;
  68345. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  68346. + lpmcfg.b.inv_sel_hsic = val;
  68347. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  68348. +}
  68349. +
  68350. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  68351. +{
  68352. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68353. +}
  68354. +
  68355. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  68356. +{
  68357. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  68358. +}
  68359. +
  68360. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  68361. +{
  68362. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  68363. +}
  68364. +
  68365. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  68366. +{
  68367. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  68368. +}
  68369. +
  68370. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  68371. +{
  68372. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  68373. +}
  68374. +
  68375. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  68376. +{
  68377. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  68378. +}
  68379. +
  68380. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  68381. +{
  68382. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  68383. +}
  68384. +
  68385. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  68386. +{
  68387. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  68388. +}
  68389. +
  68390. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  68391. +{
  68392. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  68393. +}
  68394. +
  68395. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  68396. +{
  68397. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  68398. +}
  68399. +
  68400. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  68401. +{
  68402. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  68403. +}
  68404. +
  68405. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  68406. +{
  68407. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  68408. +}
  68409. +
  68410. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  68411. +{
  68412. + return DWC_READ_REG32(core_if->host_if->hprt0);
  68413. +
  68414. +}
  68415. +
  68416. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  68417. +{
  68418. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  68419. +}
  68420. +
  68421. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  68422. +{
  68423. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  68424. +}
  68425. +
  68426. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  68427. +{
  68428. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  68429. +}
  68430. +
  68431. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  68432. +{
  68433. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  68434. +}
  68435. +
  68436. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  68437. +{
  68438. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  68439. +}
  68440. +
  68441. +/**
  68442. + * Start the SRP timer to detect when the SRP does not complete within
  68443. + * 6 seconds.
  68444. + *
  68445. + * @param core_if the pointer to core_if strucure.
  68446. + */
  68447. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  68448. +{
  68449. + core_if->srp_timer_started = 1;
  68450. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  68451. +}
  68452. +
  68453. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  68454. +{
  68455. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  68456. + gotgctl_data_t mem;
  68457. + gotgctl_data_t val;
  68458. +
  68459. + val.d32 = DWC_READ_REG32(addr);
  68460. + if (val.b.sesreq) {
  68461. + DWC_ERROR("Session Request Already active!\n");
  68462. + return;
  68463. + }
  68464. +
  68465. + DWC_INFO("Session Request Initated\n"); //NOTICE
  68466. + mem.d32 = DWC_READ_REG32(addr);
  68467. + mem.b.sesreq = 1;
  68468. + DWC_WRITE_REG32(addr, mem.d32);
  68469. +
  68470. + /* Start the SRP timer */
  68471. + dwc_otg_pcd_start_srp_timer(core_if);
  68472. + return;
  68473. +}
  68474. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cil.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  68475. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cil.h 1969-12-31 18:00:00.000000000 -0600
  68476. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h 2014-12-11 14:02:55.392418001 -0600
  68477. @@ -0,0 +1,1464 @@
  68478. +/* ==========================================================================
  68479. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  68480. + * $Revision: #123 $
  68481. + * $Date: 2012/08/10 $
  68482. + * $Change: 2047372 $
  68483. + *
  68484. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  68485. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  68486. + * otherwise expressly agreed to in writing between Synopsys and you.
  68487. + *
  68488. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  68489. + * any End User Software License Agreement or Agreement for Licensed Product
  68490. + * with Synopsys or any supplement thereto. You are permitted to use and
  68491. + * redistribute this Software in source and binary forms, with or without
  68492. + * modification, provided that redistributions of source code must retain this
  68493. + * notice. You may not view, use, disclose, copy or distribute this file or
  68494. + * any information contained herein except pursuant to this license grant from
  68495. + * Synopsys. If you do not agree with this notice, including the disclaimer
  68496. + * below, then you are not authorized to use the Software.
  68497. + *
  68498. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  68499. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68500. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68501. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  68502. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  68503. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68504. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  68505. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  68506. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  68507. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  68508. + * DAMAGE.
  68509. + * ========================================================================== */
  68510. +
  68511. +#if !defined(__DWC_CIL_H__)
  68512. +#define __DWC_CIL_H__
  68513. +
  68514. +#include "dwc_list.h"
  68515. +#include "dwc_otg_dbg.h"
  68516. +#include "dwc_otg_regs.h"
  68517. +
  68518. +#include "dwc_otg_core_if.h"
  68519. +#include "dwc_otg_adp.h"
  68520. +
  68521. +/**
  68522. + * @file
  68523. + * This file contains the interface to the Core Interface Layer.
  68524. + */
  68525. +
  68526. +#ifdef DWC_UTE_CFI
  68527. +
  68528. +#define MAX_DMA_DESCS_PER_EP 256
  68529. +
  68530. +/**
  68531. + * Enumeration for the data buffer mode
  68532. + */
  68533. +typedef enum _data_buffer_mode {
  68534. + BM_STANDARD = 0, /* data buffer is in normal mode */
  68535. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  68536. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  68537. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  68538. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  68539. +} data_buffer_mode_e;
  68540. +#endif //DWC_UTE_CFI
  68541. +
  68542. +/** Macros defined for DWC OTG HW Release version */
  68543. +
  68544. +#define OTG_CORE_REV_2_60a 0x4F54260A
  68545. +#define OTG_CORE_REV_2_71a 0x4F54271A
  68546. +#define OTG_CORE_REV_2_72a 0x4F54272A
  68547. +#define OTG_CORE_REV_2_80a 0x4F54280A
  68548. +#define OTG_CORE_REV_2_81a 0x4F54281A
  68549. +#define OTG_CORE_REV_2_90a 0x4F54290A
  68550. +#define OTG_CORE_REV_2_91a 0x4F54291A
  68551. +#define OTG_CORE_REV_2_92a 0x4F54292A
  68552. +#define OTG_CORE_REV_2_93a 0x4F54293A
  68553. +#define OTG_CORE_REV_2_94a 0x4F54294A
  68554. +#define OTG_CORE_REV_3_00a 0x4F54300A
  68555. +
  68556. +/**
  68557. + * Information for each ISOC packet.
  68558. + */
  68559. +typedef struct iso_pkt_info {
  68560. + uint32_t offset;
  68561. + uint32_t length;
  68562. + int32_t status;
  68563. +} iso_pkt_info_t;
  68564. +
  68565. +/**
  68566. + * The <code>dwc_ep</code> structure represents the state of a single
  68567. + * endpoint when acting in device mode. It contains the data items
  68568. + * needed for an endpoint to be activated and transfer packets.
  68569. + */
  68570. +typedef struct dwc_ep {
  68571. + /** EP number used for register address lookup */
  68572. + uint8_t num;
  68573. + /** EP direction 0 = OUT */
  68574. + unsigned is_in:1;
  68575. + /** EP active. */
  68576. + unsigned active:1;
  68577. +
  68578. + /**
  68579. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  68580. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  68581. + unsigned tx_fifo_num:4;
  68582. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  68583. + unsigned type:2;
  68584. +#define DWC_OTG_EP_TYPE_CONTROL 0
  68585. +#define DWC_OTG_EP_TYPE_ISOC 1
  68586. +#define DWC_OTG_EP_TYPE_BULK 2
  68587. +#define DWC_OTG_EP_TYPE_INTR 3
  68588. +
  68589. + /** DATA start PID for INTR and BULK EP */
  68590. + unsigned data_pid_start:1;
  68591. + /** Frame (even/odd) for ISOC EP */
  68592. + unsigned even_odd_frame:1;
  68593. + /** Max Packet bytes */
  68594. + unsigned maxpacket:11;
  68595. +
  68596. + /** Max Transfer size */
  68597. + uint32_t maxxfer;
  68598. +
  68599. + /** @name Transfer state */
  68600. + /** @{ */
  68601. +
  68602. + /**
  68603. + * Pointer to the beginning of the transfer buffer -- do not modify
  68604. + * during transfer.
  68605. + */
  68606. +
  68607. + dwc_dma_t dma_addr;
  68608. +
  68609. + dwc_dma_t dma_desc_addr;
  68610. + dwc_otg_dev_dma_desc_t *desc_addr;
  68611. +
  68612. + uint8_t *start_xfer_buff;
  68613. + /** pointer to the transfer buffer */
  68614. + uint8_t *xfer_buff;
  68615. + /** Number of bytes to transfer */
  68616. + unsigned xfer_len:19;
  68617. + /** Number of bytes transferred. */
  68618. + unsigned xfer_count:19;
  68619. + /** Sent ZLP */
  68620. + unsigned sent_zlp:1;
  68621. + /** Total len for control transfer */
  68622. + unsigned total_len:19;
  68623. +
  68624. + /** stall clear flag */
  68625. + unsigned stall_clear_flag:1;
  68626. +
  68627. + /** SETUP pkt cnt rollover flag for EP0 out*/
  68628. + unsigned stp_rollover;
  68629. +
  68630. +#ifdef DWC_UTE_CFI
  68631. + /* The buffer mode */
  68632. + data_buffer_mode_e buff_mode;
  68633. +
  68634. + /* The chain of DMA descriptors.
  68635. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  68636. + */
  68637. + dwc_otg_dma_desc_t *descs;
  68638. +
  68639. + /* The DMA address of the descriptors chain start */
  68640. + dma_addr_t descs_dma_addr;
  68641. + /** This variable stores the length of the last enqueued request */
  68642. + uint32_t cfi_req_len;
  68643. +#endif //DWC_UTE_CFI
  68644. +
  68645. +/** Max DMA Descriptor count for any EP */
  68646. +#define MAX_DMA_DESC_CNT 256
  68647. + /** Allocated DMA Desc count */
  68648. + uint32_t desc_cnt;
  68649. +
  68650. + /** bInterval */
  68651. + uint32_t bInterval;
  68652. + /** Next frame num to setup next ISOC transfer */
  68653. + uint32_t frame_num;
  68654. + /** Indicates SOF number overrun in DSTS */
  68655. + uint8_t frm_overrun;
  68656. +
  68657. +#ifdef DWC_UTE_PER_IO
  68658. + /** Next frame num for which will be setup DMA Desc */
  68659. + uint32_t xiso_frame_num;
  68660. + /** bInterval */
  68661. + uint32_t xiso_bInterval;
  68662. + /** Count of currently active transfers - shall be either 0 or 1 */
  68663. + int xiso_active_xfers;
  68664. + int xiso_queued_xfers;
  68665. +#endif
  68666. +#ifdef DWC_EN_ISOC
  68667. + /**
  68668. + * Variables specific for ISOC EPs
  68669. + *
  68670. + */
  68671. + /** DMA addresses of ISOC buffers */
  68672. + dwc_dma_t dma_addr0;
  68673. + dwc_dma_t dma_addr1;
  68674. +
  68675. + dwc_dma_t iso_dma_desc_addr;
  68676. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  68677. +
  68678. + /** pointer to the transfer buffers */
  68679. + uint8_t *xfer_buff0;
  68680. + uint8_t *xfer_buff1;
  68681. +
  68682. + /** number of ISOC Buffer is processing */
  68683. + uint32_t proc_buf_num;
  68684. + /** Interval of ISOC Buffer processing */
  68685. + uint32_t buf_proc_intrvl;
  68686. + /** Data size for regular frame */
  68687. + uint32_t data_per_frame;
  68688. +
  68689. + /* todo - pattern data support is to be implemented in the future */
  68690. + /** Data size for pattern frame */
  68691. + uint32_t data_pattern_frame;
  68692. + /** Frame number of pattern data */
  68693. + uint32_t sync_frame;
  68694. +
  68695. + /** bInterval */
  68696. + uint32_t bInterval;
  68697. + /** ISO Packet number per frame */
  68698. + uint32_t pkt_per_frm;
  68699. + /** Next frame num for which will be setup DMA Desc */
  68700. + uint32_t next_frame;
  68701. + /** Number of packets per buffer processing */
  68702. + uint32_t pkt_cnt;
  68703. + /** Info for all isoc packets */
  68704. + iso_pkt_info_t *pkt_info;
  68705. + /** current pkt number */
  68706. + uint32_t cur_pkt;
  68707. + /** current pkt number */
  68708. + uint8_t *cur_pkt_addr;
  68709. + /** current pkt number */
  68710. + uint32_t cur_pkt_dma_addr;
  68711. +#endif /* DWC_EN_ISOC */
  68712. +
  68713. +/** @} */
  68714. +} dwc_ep_t;
  68715. +
  68716. +/*
  68717. + * Reasons for halting a host channel.
  68718. + */
  68719. +typedef enum dwc_otg_halt_status {
  68720. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  68721. + DWC_OTG_HC_XFER_COMPLETE,
  68722. + DWC_OTG_HC_XFER_URB_COMPLETE,
  68723. + DWC_OTG_HC_XFER_ACK,
  68724. + DWC_OTG_HC_XFER_NAK,
  68725. + DWC_OTG_HC_XFER_NYET,
  68726. + DWC_OTG_HC_XFER_STALL,
  68727. + DWC_OTG_HC_XFER_XACT_ERR,
  68728. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  68729. + DWC_OTG_HC_XFER_BABBLE_ERR,
  68730. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  68731. + DWC_OTG_HC_XFER_AHB_ERR,
  68732. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  68733. + DWC_OTG_HC_XFER_URB_DEQUEUE
  68734. +} dwc_otg_halt_status_e;
  68735. +
  68736. +/**
  68737. + * Host channel descriptor. This structure represents the state of a single
  68738. + * host channel when acting in host mode. It contains the data items needed to
  68739. + * transfer packets to an endpoint via a host channel.
  68740. + */
  68741. +typedef struct dwc_hc {
  68742. + /** Host channel number used for register address lookup */
  68743. + uint8_t hc_num;
  68744. +
  68745. + /** Device to access */
  68746. + unsigned dev_addr:7;
  68747. +
  68748. + /** EP to access */
  68749. + unsigned ep_num:4;
  68750. +
  68751. + /** EP direction. 0: OUT, 1: IN */
  68752. + unsigned ep_is_in:1;
  68753. +
  68754. + /**
  68755. + * EP speed.
  68756. + * One of the following values:
  68757. + * - DWC_OTG_EP_SPEED_LOW
  68758. + * - DWC_OTG_EP_SPEED_FULL
  68759. + * - DWC_OTG_EP_SPEED_HIGH
  68760. + */
  68761. + unsigned speed:2;
  68762. +#define DWC_OTG_EP_SPEED_LOW 0
  68763. +#define DWC_OTG_EP_SPEED_FULL 1
  68764. +#define DWC_OTG_EP_SPEED_HIGH 2
  68765. +
  68766. + /**
  68767. + * Endpoint type.
  68768. + * One of the following values:
  68769. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  68770. + * - DWC_OTG_EP_TYPE_ISOC: 1
  68771. + * - DWC_OTG_EP_TYPE_BULK: 2
  68772. + * - DWC_OTG_EP_TYPE_INTR: 3
  68773. + */
  68774. + unsigned ep_type:2;
  68775. +
  68776. + /** Max packet size in bytes */
  68777. + unsigned max_packet:11;
  68778. +
  68779. + /**
  68780. + * PID for initial transaction.
  68781. + * 0: DATA0,<br>
  68782. + * 1: DATA2,<br>
  68783. + * 2: DATA1,<br>
  68784. + * 3: MDATA (non-Control EP),
  68785. + * SETUP (Control EP)
  68786. + */
  68787. + unsigned data_pid_start:2;
  68788. +#define DWC_OTG_HC_PID_DATA0 0
  68789. +#define DWC_OTG_HC_PID_DATA2 1
  68790. +#define DWC_OTG_HC_PID_DATA1 2
  68791. +#define DWC_OTG_HC_PID_MDATA 3
  68792. +#define DWC_OTG_HC_PID_SETUP 3
  68793. +
  68794. + /** Number of periodic transactions per (micro)frame */
  68795. + unsigned multi_count:2;
  68796. +
  68797. + /** @name Transfer State */
  68798. + /** @{ */
  68799. +
  68800. + /** Pointer to the current transfer buffer position. */
  68801. + uint8_t *xfer_buff;
  68802. + /**
  68803. + * In Buffer DMA mode this buffer will be used
  68804. + * if xfer_buff is not DWORD aligned.
  68805. + */
  68806. + dwc_dma_t align_buff;
  68807. + /** Total number of bytes to transfer. */
  68808. + uint32_t xfer_len;
  68809. + /** Number of bytes transferred so far. */
  68810. + uint32_t xfer_count;
  68811. + /** Packet count at start of transfer.*/
  68812. + uint16_t start_pkt_count;
  68813. +
  68814. + /**
  68815. + * Flag to indicate whether the transfer has been started. Set to 1 if
  68816. + * it has been started, 0 otherwise.
  68817. + */
  68818. + uint8_t xfer_started;
  68819. +
  68820. + /**
  68821. + * Set to 1 to indicate that a PING request should be issued on this
  68822. + * channel. If 0, process normally.
  68823. + */
  68824. + uint8_t do_ping;
  68825. +
  68826. + /**
  68827. + * Set to 1 to indicate that the error count for this transaction is
  68828. + * non-zero. Set to 0 if the error count is 0.
  68829. + */
  68830. + uint8_t error_state;
  68831. +
  68832. + /**
  68833. + * Set to 1 to indicate that this channel should be halted the next
  68834. + * time a request is queued for the channel. This is necessary in
  68835. + * slave mode if no request queue space is available when an attempt
  68836. + * is made to halt the channel.
  68837. + */
  68838. + uint8_t halt_on_queue;
  68839. +
  68840. + /**
  68841. + * Set to 1 if the host channel has been halted, but the core is not
  68842. + * finished flushing queued requests. Otherwise 0.
  68843. + */
  68844. + uint8_t halt_pending;
  68845. +
  68846. + /**
  68847. + * Reason for halting the host channel.
  68848. + */
  68849. + dwc_otg_halt_status_e halt_status;
  68850. +
  68851. + /*
  68852. + * Split settings for the host channel
  68853. + */
  68854. + uint8_t do_split; /**< Enable split for the channel */
  68855. + uint8_t complete_split; /**< Enable complete split */
  68856. + uint8_t hub_addr; /**< Address of high speed hub */
  68857. +
  68858. + uint8_t port_addr; /**< Port of the low/full speed device */
  68859. + /** Split transaction position
  68860. + * One of the following values:
  68861. + * - DWC_HCSPLIT_XACTPOS_MID
  68862. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  68863. + * - DWC_HCSPLIT_XACTPOS_END
  68864. + * - DWC_HCSPLIT_XACTPOS_ALL */
  68865. + uint8_t xact_pos;
  68866. +
  68867. + /** Set when the host channel does a short read. */
  68868. + uint8_t short_read;
  68869. +
  68870. + /**
  68871. + * Number of requests issued for this channel since it was assigned to
  68872. + * the current transfer (not counting PINGs).
  68873. + */
  68874. + uint8_t requests;
  68875. +
  68876. + /**
  68877. + * Queue Head for the transfer being processed by this channel.
  68878. + */
  68879. + struct dwc_otg_qh *qh;
  68880. +
  68881. + /** @} */
  68882. +
  68883. + /** Entry in list of host channels. */
  68884. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  68885. +
  68886. + /** @name Descriptor DMA support */
  68887. + /** @{ */
  68888. +
  68889. + /** Number of Transfer Descriptors */
  68890. + uint16_t ntd;
  68891. +
  68892. + /** Descriptor List DMA address */
  68893. + dwc_dma_t desc_list_addr;
  68894. +
  68895. + /** Scheduling micro-frame bitmap. */
  68896. + uint8_t schinfo;
  68897. +
  68898. + /** @} */
  68899. +} dwc_hc_t;
  68900. +
  68901. +/**
  68902. + * The following parameters may be specified when starting the module. These
  68903. + * parameters define how the DWC_otg controller should be configured.
  68904. + */
  68905. +typedef struct dwc_otg_core_params {
  68906. + int32_t opt;
  68907. +
  68908. + /**
  68909. + * Specifies the OTG capabilities. The driver will automatically
  68910. + * detect the value for this parameter if none is specified.
  68911. + * 0 - HNP and SRP capable (default)
  68912. + * 1 - SRP Only capable
  68913. + * 2 - No HNP/SRP capable
  68914. + */
  68915. + int32_t otg_cap;
  68916. +
  68917. + /**
  68918. + * Specifies whether to use slave or DMA mode for accessing the data
  68919. + * FIFOs. The driver will automatically detect the value for this
  68920. + * parameter if none is specified.
  68921. + * 0 - Slave
  68922. + * 1 - DMA (default, if available)
  68923. + */
  68924. + int32_t dma_enable;
  68925. +
  68926. + /**
  68927. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  68928. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  68929. + * will automatically detect the value for this if none is specified.
  68930. + * 0 - address DMA
  68931. + * 1 - DMA Descriptor(default, if available)
  68932. + */
  68933. + int32_t dma_desc_enable;
  68934. + /** The DMA Burst size (applicable only for External DMA
  68935. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  68936. + */
  68937. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  68938. +
  68939. + /**
  68940. + * Specifies the maximum speed of operation in host and device mode.
  68941. + * The actual speed depends on the speed of the attached device and
  68942. + * the value of phy_type. The actual speed depends on the speed of the
  68943. + * attached device.
  68944. + * 0 - High Speed (default)
  68945. + * 1 - Full Speed
  68946. + */
  68947. + int32_t speed;
  68948. + /** Specifies whether low power mode is supported when attached
  68949. + * to a Full Speed or Low Speed device in host mode.
  68950. + * 0 - Don't support low power mode (default)
  68951. + * 1 - Support low power mode
  68952. + */
  68953. + int32_t host_support_fs_ls_low_power;
  68954. +
  68955. + /** Specifies the PHY clock rate in low power mode when connected to a
  68956. + * Low Speed device in host mode. This parameter is applicable only if
  68957. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  68958. + * then defaults to 6 MHZ otherwise 48 MHZ.
  68959. + *
  68960. + * 0 - 48 MHz
  68961. + * 1 - 6 MHz
  68962. + */
  68963. + int32_t host_ls_low_power_phy_clk;
  68964. +
  68965. + /**
  68966. + * 0 - Use cC FIFO size parameters
  68967. + * 1 - Allow dynamic FIFO sizing (default)
  68968. + */
  68969. + int32_t enable_dynamic_fifo;
  68970. +
  68971. + /** Total number of 4-byte words in the data FIFO memory. This
  68972. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  68973. + * Tx FIFOs.
  68974. + * 32 to 32768 (default 8192)
  68975. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  68976. + */
  68977. + int32_t data_fifo_size;
  68978. +
  68979. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  68980. + * FIFO sizing is enabled.
  68981. + * 16 to 32768 (default 1064)
  68982. + */
  68983. + int32_t dev_rx_fifo_size;
  68984. +
  68985. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  68986. + * when dynamic FIFO sizing is enabled.
  68987. + * 16 to 32768 (default 1024)
  68988. + */
  68989. + int32_t dev_nperio_tx_fifo_size;
  68990. +
  68991. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  68992. + * mode when dynamic FIFO sizing is enabled.
  68993. + * 4 to 768 (default 256)
  68994. + */
  68995. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  68996. +
  68997. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  68998. + * FIFO sizing is enabled.
  68999. + * 16 to 32768 (default 1024)
  69000. + */
  69001. + int32_t host_rx_fifo_size;
  69002. +
  69003. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  69004. + * when Dynamic FIFO sizing is enabled in the core.
  69005. + * 16 to 32768 (default 1024)
  69006. + */
  69007. + int32_t host_nperio_tx_fifo_size;
  69008. +
  69009. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  69010. + * FIFO sizing is enabled.
  69011. + * 16 to 32768 (default 1024)
  69012. + */
  69013. + int32_t host_perio_tx_fifo_size;
  69014. +
  69015. + /** The maximum transfer size supported in bytes.
  69016. + * 2047 to 65,535 (default 65,535)
  69017. + */
  69018. + int32_t max_transfer_size;
  69019. +
  69020. + /** The maximum number of packets in a transfer.
  69021. + * 15 to 511 (default 511)
  69022. + */
  69023. + int32_t max_packet_count;
  69024. +
  69025. + /** The number of host channel registers to use.
  69026. + * 1 to 16 (default 12)
  69027. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  69028. + */
  69029. + int32_t host_channels;
  69030. +
  69031. + /** The number of endpoints in addition to EP0 available for device
  69032. + * mode operations.
  69033. + * 1 to 15 (default 6 IN and OUT)
  69034. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  69035. + * endpoints in addition to EP0.
  69036. + */
  69037. + int32_t dev_endpoints;
  69038. +
  69039. + /**
  69040. + * Specifies the type of PHY interface to use. By default, the driver
  69041. + * will automatically detect the phy_type.
  69042. + *
  69043. + * 0 - Full Speed PHY
  69044. + * 1 - UTMI+ (default)
  69045. + * 2 - ULPI
  69046. + */
  69047. + int32_t phy_type;
  69048. +
  69049. + /**
  69050. + * Specifies the UTMI+ Data Width. This parameter is
  69051. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  69052. + * PHY_TYPE, this parameter indicates the data width between
  69053. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  69054. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  69055. + * to "8 and 16 bits", meaning that the core has been
  69056. + * configured to work at either data path width.
  69057. + *
  69058. + * 8 or 16 bits (default 16)
  69059. + */
  69060. + int32_t phy_utmi_width;
  69061. +
  69062. + /**
  69063. + * Specifies whether the ULPI operates at double or single
  69064. + * data rate. This parameter is only applicable if PHY_TYPE is
  69065. + * ULPI.
  69066. + *
  69067. + * 0 - single data rate ULPI interface with 8 bit wide data
  69068. + * bus (default)
  69069. + * 1 - double data rate ULPI interface with 4 bit wide data
  69070. + * bus
  69071. + */
  69072. + int32_t phy_ulpi_ddr;
  69073. +
  69074. + /**
  69075. + * Specifies whether to use the internal or external supply to
  69076. + * drive the vbus with a ULPI phy.
  69077. + */
  69078. + int32_t phy_ulpi_ext_vbus;
  69079. +
  69080. + /**
  69081. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  69082. + * parameter is only applicable if PHY_TYPE is FS.
  69083. + * 0 - No (default)
  69084. + * 1 - Yes
  69085. + */
  69086. + int32_t i2c_enable;
  69087. +
  69088. + int32_t ulpi_fs_ls;
  69089. +
  69090. + int32_t ts_dline;
  69091. +
  69092. + /**
  69093. + * Specifies whether dedicated transmit FIFOs are
  69094. + * enabled for non periodic IN endpoints in device mode
  69095. + * 0 - No
  69096. + * 1 - Yes
  69097. + */
  69098. + int32_t en_multiple_tx_fifo;
  69099. +
  69100. + /** Number of 4-byte words in each of the Tx FIFOs in device
  69101. + * mode when dynamic FIFO sizing is enabled.
  69102. + * 4 to 768 (default 256)
  69103. + */
  69104. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  69105. +
  69106. + /** Thresholding enable flag-
  69107. + * bit 0 - enable non-ISO Tx thresholding
  69108. + * bit 1 - enable ISO Tx thresholding
  69109. + * bit 2 - enable Rx thresholding
  69110. + */
  69111. + uint32_t thr_ctl;
  69112. +
  69113. + /** Thresholding length for Tx
  69114. + * FIFOs in 32 bit DWORDs
  69115. + */
  69116. + uint32_t tx_thr_length;
  69117. +
  69118. + /** Thresholding length for Rx
  69119. + * FIFOs in 32 bit DWORDs
  69120. + */
  69121. + uint32_t rx_thr_length;
  69122. +
  69123. + /**
  69124. + * Specifies whether LPM (Link Power Management) support is enabled
  69125. + */
  69126. + int32_t lpm_enable;
  69127. +
  69128. + /** Per Transfer Interrupt
  69129. + * mode enable flag
  69130. + * 1 - Enabled
  69131. + * 0 - Disabled
  69132. + */
  69133. + int32_t pti_enable;
  69134. +
  69135. + /** Multi Processor Interrupt
  69136. + * mode enable flag
  69137. + * 1 - Enabled
  69138. + * 0 - Disabled
  69139. + */
  69140. + int32_t mpi_enable;
  69141. +
  69142. + /** IS_USB Capability
  69143. + * 1 - Enabled
  69144. + * 0 - Disabled
  69145. + */
  69146. + int32_t ic_usb_cap;
  69147. +
  69148. + /** AHB Threshold Ratio
  69149. + * 2'b00 AHB Threshold = MAC Threshold
  69150. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  69151. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  69152. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  69153. + */
  69154. + int32_t ahb_thr_ratio;
  69155. +
  69156. + /** ADP Support
  69157. + * 1 - Enabled
  69158. + * 0 - Disabled
  69159. + */
  69160. + int32_t adp_supp_enable;
  69161. +
  69162. + /** HFIR Reload Control
  69163. + * 0 - The HFIR cannot be reloaded dynamically.
  69164. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  69165. + */
  69166. + int32_t reload_ctl;
  69167. +
  69168. + /** DCFG: Enable device Out NAK
  69169. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  69170. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  69171. + */
  69172. + int32_t dev_out_nak;
  69173. +
  69174. + /** DCFG: Enable Continue on BNA
  69175. + * After receiving BNA interrupt the core disables the endpoint,when the
  69176. + * endpoint is re-enabled by the application the core starts processing
  69177. + * 0 - from the DOEPDMA descriptor
  69178. + * 1 - from the descriptor which received the BNA.
  69179. + */
  69180. + int32_t cont_on_bna;
  69181. +
  69182. + /** GAHBCFG: AHB Single Support
  69183. + * This bit when programmed supports SINGLE transfers for remainder
  69184. + * data in a transfer for DMA mode of operation.
  69185. + * 0 - in this case the remainder data will be sent using INCR burst size.
  69186. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  69187. + */
  69188. + int32_t ahb_single;
  69189. +
  69190. + /** Core Power down mode
  69191. + * 0 - No Power Down is enabled
  69192. + * 1 - Reserved
  69193. + * 2 - Complete Power Down (Hibernation)
  69194. + */
  69195. + int32_t power_down;
  69196. +
  69197. + /** OTG revision supported
  69198. + * 0 - OTG 1.3 revision
  69199. + * 1 - OTG 2.0 revision
  69200. + */
  69201. + int32_t otg_ver;
  69202. +
  69203. +} dwc_otg_core_params_t;
  69204. +
  69205. +#ifdef DEBUG
  69206. +struct dwc_otg_core_if;
  69207. +typedef struct hc_xfer_info {
  69208. + struct dwc_otg_core_if *core_if;
  69209. + dwc_hc_t *hc;
  69210. +} hc_xfer_info_t;
  69211. +#endif
  69212. +
  69213. +typedef struct ep_xfer_info {
  69214. + struct dwc_otg_core_if *core_if;
  69215. + dwc_ep_t *ep;
  69216. + uint8_t state;
  69217. +} ep_xfer_info_t;
  69218. +/*
  69219. + * Device States
  69220. + */
  69221. +typedef enum dwc_otg_lx_state {
  69222. + /** On state */
  69223. + DWC_OTG_L0,
  69224. + /** LPM sleep state*/
  69225. + DWC_OTG_L1,
  69226. + /** USB suspend state*/
  69227. + DWC_OTG_L2,
  69228. + /** Off state*/
  69229. + DWC_OTG_L3
  69230. +} dwc_otg_lx_state_e;
  69231. +
  69232. +struct dwc_otg_global_regs_backup {
  69233. + uint32_t gotgctl_local;
  69234. + uint32_t gintmsk_local;
  69235. + uint32_t gahbcfg_local;
  69236. + uint32_t gusbcfg_local;
  69237. + uint32_t grxfsiz_local;
  69238. + uint32_t gnptxfsiz_local;
  69239. +#ifdef CONFIG_USB_DWC_OTG_LPM
  69240. + uint32_t glpmcfg_local;
  69241. +#endif
  69242. + uint32_t gi2cctl_local;
  69243. + uint32_t hptxfsiz_local;
  69244. + uint32_t pcgcctl_local;
  69245. + uint32_t gdfifocfg_local;
  69246. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  69247. + uint32_t gpwrdn_local;
  69248. + uint32_t xhib_pcgcctl;
  69249. + uint32_t xhib_gpwrdn;
  69250. +};
  69251. +
  69252. +struct dwc_otg_host_regs_backup {
  69253. + uint32_t hcfg_local;
  69254. + uint32_t haintmsk_local;
  69255. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  69256. + uint32_t hprt0_local;
  69257. + uint32_t hfir_local;
  69258. +};
  69259. +
  69260. +struct dwc_otg_dev_regs_backup {
  69261. + uint32_t dcfg;
  69262. + uint32_t dctl;
  69263. + uint32_t daintmsk;
  69264. + uint32_t diepmsk;
  69265. + uint32_t doepmsk;
  69266. + uint32_t diepctl[MAX_EPS_CHANNELS];
  69267. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  69268. + uint32_t diepdma[MAX_EPS_CHANNELS];
  69269. +};
  69270. +/**
  69271. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  69272. + * the DWC_otg controller acting in either host or device mode. It
  69273. + * represents the programming view of the controller as a whole.
  69274. + */
  69275. +struct dwc_otg_core_if {
  69276. + /** Parameters that define how the core should be configured.*/
  69277. + dwc_otg_core_params_t *core_params;
  69278. +
  69279. + /** Core Global registers starting at offset 000h. */
  69280. + dwc_otg_core_global_regs_t *core_global_regs;
  69281. +
  69282. + /** Device-specific information */
  69283. + dwc_otg_dev_if_t *dev_if;
  69284. + /** Host-specific information */
  69285. + dwc_otg_host_if_t *host_if;
  69286. +
  69287. + /** Value from SNPSID register */
  69288. + uint32_t snpsid;
  69289. +
  69290. + /*
  69291. + * Set to 1 if the core PHY interface bits in USBCFG have been
  69292. + * initialized.
  69293. + */
  69294. + uint8_t phy_init_done;
  69295. +
  69296. + /*
  69297. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  69298. + */
  69299. + uint8_t srp_success;
  69300. + uint8_t srp_timer_started;
  69301. + /** Timer for SRP. If it expires before SRP is successful
  69302. + * clear the SRP. */
  69303. + dwc_timer_t *srp_timer;
  69304. +
  69305. +#ifdef DWC_DEV_SRPCAP
  69306. + /* This timer is needed to power on the hibernated host core if SRP is not
  69307. + * initiated on connected SRP capable device for limited period of time
  69308. + */
  69309. + uint8_t pwron_timer_started;
  69310. + dwc_timer_t *pwron_timer;
  69311. +#endif
  69312. + /* Common configuration information */
  69313. + /** Power and Clock Gating Control Register */
  69314. + volatile uint32_t *pcgcctl;
  69315. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  69316. +
  69317. + /** Push/pop addresses for endpoints or host channels.*/
  69318. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  69319. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  69320. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  69321. +
  69322. + /** Total RAM for FIFOs (Bytes) */
  69323. + uint16_t total_fifo_size;
  69324. + /** Size of Rx FIFO (Bytes) */
  69325. + uint16_t rx_fifo_size;
  69326. + /** Size of Non-periodic Tx FIFO (Bytes) */
  69327. + uint16_t nperio_tx_fifo_size;
  69328. +
  69329. + /** 1 if DMA is enabled, 0 otherwise. */
  69330. + uint8_t dma_enable;
  69331. +
  69332. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  69333. + uint8_t dma_desc_enable;
  69334. +
  69335. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  69336. + uint8_t pti_enh_enable;
  69337. +
  69338. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  69339. + uint8_t multiproc_int_enable;
  69340. +
  69341. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  69342. + uint8_t en_multiple_tx_fifo;
  69343. +
  69344. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  69345. + * process of being queued */
  69346. + uint8_t queuing_high_bandwidth;
  69347. +
  69348. + /** Hardware Configuration -- stored here for convenience.*/
  69349. + hwcfg1_data_t hwcfg1;
  69350. + hwcfg2_data_t hwcfg2;
  69351. + hwcfg3_data_t hwcfg3;
  69352. + hwcfg4_data_t hwcfg4;
  69353. + fifosize_data_t hptxfsiz;
  69354. +
  69355. + /** Host and Device Configuration -- stored here for convenience.*/
  69356. + hcfg_data_t hcfg;
  69357. + dcfg_data_t dcfg;
  69358. +
  69359. + /** The operational State, during transations
  69360. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  69361. + * match the core but allows the software to determine
  69362. + * transitions.
  69363. + */
  69364. + uint8_t op_state;
  69365. +
  69366. + /**
  69367. + * Set to 1 if the HCD needs to be restarted on a session request
  69368. + * interrupt. This is required if no connector ID status change has
  69369. + * occurred since the HCD was last disconnected.
  69370. + */
  69371. + uint8_t restart_hcd_on_session_req;
  69372. +
  69373. + /** HCD callbacks */
  69374. + /** A-Device is a_host */
  69375. +#define A_HOST (1)
  69376. + /** A-Device is a_suspend */
  69377. +#define A_SUSPEND (2)
  69378. + /** A-Device is a_peripherial */
  69379. +#define A_PERIPHERAL (3)
  69380. + /** B-Device is operating as a Peripheral. */
  69381. +#define B_PERIPHERAL (4)
  69382. + /** B-Device is operating as a Host. */
  69383. +#define B_HOST (5)
  69384. +
  69385. + /** HCD callbacks */
  69386. + struct dwc_otg_cil_callbacks *hcd_cb;
  69387. + /** PCD callbacks */
  69388. + struct dwc_otg_cil_callbacks *pcd_cb;
  69389. +
  69390. + /** Device mode Periodic Tx FIFO Mask */
  69391. + uint32_t p_tx_msk;
  69392. + /** Device mode Periodic Tx FIFO Mask */
  69393. + uint32_t tx_msk;
  69394. +
  69395. + /** Workqueue object used for handling several interrupts */
  69396. + dwc_workq_t *wq_otg;
  69397. +
  69398. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  69399. + dwc_timer_t *wkp_timer;
  69400. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  69401. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  69402. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  69403. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  69404. +#ifdef DEBUG
  69405. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  69406. +
  69407. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  69408. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  69409. +
  69410. + uint32_t hfnum_7_samples;
  69411. + uint64_t hfnum_7_frrem_accum;
  69412. + uint32_t hfnum_0_samples;
  69413. + uint64_t hfnum_0_frrem_accum;
  69414. + uint32_t hfnum_other_samples;
  69415. + uint64_t hfnum_other_frrem_accum;
  69416. +#endif
  69417. +
  69418. +#ifdef DWC_UTE_CFI
  69419. + uint16_t pwron_rxfsiz;
  69420. + uint16_t pwron_gnptxfsiz;
  69421. + uint16_t pwron_txfsiz[15];
  69422. +
  69423. + uint16_t init_rxfsiz;
  69424. + uint16_t init_gnptxfsiz;
  69425. + uint16_t init_txfsiz[15];
  69426. +#endif
  69427. +
  69428. + /** Lx state of device */
  69429. + dwc_otg_lx_state_e lx_state;
  69430. +
  69431. + /** Saved Core Global registers */
  69432. + struct dwc_otg_global_regs_backup *gr_backup;
  69433. + /** Saved Host registers */
  69434. + struct dwc_otg_host_regs_backup *hr_backup;
  69435. + /** Saved Device registers */
  69436. + struct dwc_otg_dev_regs_backup *dr_backup;
  69437. +
  69438. + /** Power Down Enable */
  69439. + uint32_t power_down;
  69440. +
  69441. + /** ADP support Enable */
  69442. + uint32_t adp_enable;
  69443. +
  69444. + /** ADP structure object */
  69445. + dwc_otg_adp_t adp;
  69446. +
  69447. + /** hibernation/suspend flag */
  69448. + int hibernation_suspend;
  69449. +
  69450. + /** Device mode extended hibernation flag */
  69451. + int xhib;
  69452. +
  69453. + /** OTG revision supported */
  69454. + uint32_t otg_ver;
  69455. +
  69456. + /** OTG status flag used for HNP polling */
  69457. + uint8_t otg_sts;
  69458. +
  69459. + /** Pointer to either hcd->lock or pcd->lock */
  69460. + dwc_spinlock_t *lock;
  69461. +
  69462. + /** Start predict NextEP based on Learning Queue if equal 1,
  69463. + * also used as counter of disabled NP IN EP's */
  69464. + uint8_t start_predict;
  69465. +
  69466. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  69467. + * active, 0xff otherwise */
  69468. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  69469. +
  69470. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  69471. + uint8_t first_in_nextep_seq;
  69472. +
  69473. + /** Frame number while entering to ISR - needed for ISOCs **/
  69474. + uint32_t frame_num;
  69475. +
  69476. +};
  69477. +
  69478. +#ifdef DEBUG
  69479. +/*
  69480. + * This function is called when transfer is timed out.
  69481. + */
  69482. +extern void hc_xfer_timeout(void *ptr);
  69483. +#endif
  69484. +
  69485. +/*
  69486. + * This function is called when transfer is timed out on endpoint.
  69487. + */
  69488. +extern void ep_xfer_timeout(void *ptr);
  69489. +
  69490. +/*
  69491. + * The following functions are functions for works
  69492. + * using during handling some interrupts
  69493. + */
  69494. +extern void w_conn_id_status_change(void *p);
  69495. +
  69496. +extern void w_wakeup_detected(void *p);
  69497. +
  69498. +/** Saves global register values into system memory. */
  69499. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  69500. +/** Saves device register values into system memory. */
  69501. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  69502. +/** Saves host register values into system memory. */
  69503. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  69504. +/** Restore global register values. */
  69505. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  69506. +/** Restore host register values. */
  69507. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  69508. +/** Restore device register values. */
  69509. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  69510. + int rem_wakeup);
  69511. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  69512. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  69513. + int is_host);
  69514. +
  69515. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  69516. + int restore_mode, int reset);
  69517. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  69518. + int rem_wakeup, int reset);
  69519. +
  69520. +/*
  69521. + * The following functions support initialization of the CIL driver component
  69522. + * and the DWC_otg controller.
  69523. + */
  69524. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  69525. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  69526. +
  69527. +/** @name Device CIL Functions
  69528. + * The following functions support managing the DWC_otg controller in device
  69529. + * mode.
  69530. + */
  69531. +/**@{*/
  69532. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  69533. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  69534. + uint32_t * _dest);
  69535. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  69536. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  69537. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  69538. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  69539. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  69540. + dwc_ep_t * _ep);
  69541. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  69542. + dwc_ep_t * _ep);
  69543. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  69544. + dwc_ep_t * _ep);
  69545. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  69546. + dwc_ep_t * _ep);
  69547. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  69548. + dwc_ep_t * _ep, int _dma);
  69549. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  69550. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  69551. + dwc_ep_t * _ep);
  69552. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  69553. +
  69554. +#ifdef DWC_EN_ISOC
  69555. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  69556. + dwc_ep_t * ep);
  69557. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  69558. + dwc_ep_t * ep);
  69559. +#endif /* DWC_EN_ISOC */
  69560. +/**@}*/
  69561. +
  69562. +/** @name Host CIL Functions
  69563. + * The following functions support managing the DWC_otg controller in host
  69564. + * mode.
  69565. + */
  69566. +/**@{*/
  69567. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  69568. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  69569. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  69570. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  69571. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  69572. + dwc_hc_t * _hc);
  69573. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  69574. + dwc_hc_t * _hc);
  69575. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  69576. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  69577. + dwc_hc_t * _hc);
  69578. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  69579. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  69580. +
  69581. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  69582. + dwc_hc_t * hc);
  69583. +
  69584. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  69585. +
  69586. +/* Macro used to clear one channel interrupt */
  69587. +#define clear_hc_int(_hc_regs_, _intr_) \
  69588. +do { \
  69589. + hcint_data_t hcint_clear = {.d32 = 0}; \
  69590. + hcint_clear.b._intr_ = 1; \
  69591. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  69592. +} while (0)
  69593. +
  69594. +/*
  69595. + * Macro used to disable one channel interrupt. Channel interrupts are
  69596. + * disabled when the channel is halted or released by the interrupt handler.
  69597. + * There is no need to handle further interrupts of that type until the
  69598. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  69599. + * because the channel structures are cleaned up when the channel is released.
  69600. + */
  69601. +#define disable_hc_int(_hc_regs_, _intr_) \
  69602. +do { \
  69603. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  69604. + hcintmsk.b._intr_ = 1; \
  69605. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  69606. +} while (0)
  69607. +
  69608. +/**
  69609. + * This function Reads HPRT0 in preparation to modify. It keeps the
  69610. + * WC bits 0 so that if they are read as 1, they won't clear when you
  69611. + * write it back
  69612. + */
  69613. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  69614. +{
  69615. + hprt0_data_t hprt0;
  69616. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  69617. + hprt0.b.prtena = 0;
  69618. + hprt0.b.prtconndet = 0;
  69619. + hprt0.b.prtenchng = 0;
  69620. + hprt0.b.prtovrcurrchng = 0;
  69621. + return hprt0.d32;
  69622. +}
  69623. +
  69624. +/**@}*/
  69625. +
  69626. +/** @name Common CIL Functions
  69627. + * The following functions support managing the DWC_otg controller in either
  69628. + * device or host mode.
  69629. + */
  69630. +/**@{*/
  69631. +
  69632. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  69633. + uint8_t * dest, uint16_t bytes);
  69634. +
  69635. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  69636. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  69637. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  69638. +
  69639. +/**
  69640. + * This function returns the Core Interrupt register.
  69641. + */
  69642. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  69643. +{
  69644. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  69645. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  69646. +}
  69647. +
  69648. +/**
  69649. + * This function returns the OTG Interrupt register.
  69650. + */
  69651. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  69652. +{
  69653. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  69654. +}
  69655. +
  69656. +/**
  69657. + * This function reads the Device All Endpoints Interrupt register and
  69658. + * returns the IN endpoint interrupt bits.
  69659. + */
  69660. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  69661. + core_if)
  69662. +{
  69663. +
  69664. + uint32_t v;
  69665. +
  69666. + if (core_if->multiproc_int_enable) {
  69667. + v = DWC_READ_REG32(&core_if->dev_if->
  69668. + dev_global_regs->deachint) &
  69669. + DWC_READ_REG32(&core_if->
  69670. + dev_if->dev_global_regs->deachintmsk);
  69671. + } else {
  69672. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  69673. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  69674. + }
  69675. + return (v & 0xffff);
  69676. +}
  69677. +
  69678. +/**
  69679. + * This function reads the Device All Endpoints Interrupt register and
  69680. + * returns the OUT endpoint interrupt bits.
  69681. + */
  69682. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  69683. + core_if)
  69684. +{
  69685. + uint32_t v;
  69686. +
  69687. + if (core_if->multiproc_int_enable) {
  69688. + v = DWC_READ_REG32(&core_if->dev_if->
  69689. + dev_global_regs->deachint) &
  69690. + DWC_READ_REG32(&core_if->
  69691. + dev_if->dev_global_regs->deachintmsk);
  69692. + } else {
  69693. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  69694. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  69695. + }
  69696. +
  69697. + return ((v & 0xffff0000) >> 16);
  69698. +}
  69699. +
  69700. +/**
  69701. + * This function returns the Device IN EP Interrupt register
  69702. + */
  69703. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  69704. + dwc_ep_t * ep)
  69705. +{
  69706. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  69707. + uint32_t v, msk, emp;
  69708. +
  69709. + if (core_if->multiproc_int_enable) {
  69710. + msk =
  69711. + DWC_READ_REG32(&dev_if->
  69712. + dev_global_regs->diepeachintmsk[ep->num]);
  69713. + emp =
  69714. + DWC_READ_REG32(&dev_if->
  69715. + dev_global_regs->dtknqr4_fifoemptymsk);
  69716. + msk |= ((emp >> ep->num) & 0x1) << 7;
  69717. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  69718. + } else {
  69719. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  69720. + emp =
  69721. + DWC_READ_REG32(&dev_if->
  69722. + dev_global_regs->dtknqr4_fifoemptymsk);
  69723. + msk |= ((emp >> ep->num) & 0x1) << 7;
  69724. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  69725. + }
  69726. +
  69727. + return v;
  69728. +}
  69729. +
  69730. +/**
  69731. + * This function returns the Device OUT EP Interrupt register
  69732. + */
  69733. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  69734. + _core_if, dwc_ep_t * _ep)
  69735. +{
  69736. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  69737. + uint32_t v;
  69738. + doepmsk_data_t msk = {.d32 = 0 };
  69739. +
  69740. + if (_core_if->multiproc_int_enable) {
  69741. + msk.d32 =
  69742. + DWC_READ_REG32(&dev_if->
  69743. + dev_global_regs->doepeachintmsk[_ep->num]);
  69744. + if (_core_if->pti_enh_enable) {
  69745. + msk.b.pktdrpsts = 1;
  69746. + }
  69747. + v = DWC_READ_REG32(&dev_if->
  69748. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  69749. + } else {
  69750. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  69751. + if (_core_if->pti_enh_enable) {
  69752. + msk.b.pktdrpsts = 1;
  69753. + }
  69754. + v = DWC_READ_REG32(&dev_if->
  69755. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  69756. + }
  69757. + return v;
  69758. +}
  69759. +
  69760. +/**
  69761. + * This function returns the Host All Channel Interrupt register
  69762. + */
  69763. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  69764. + _core_if)
  69765. +{
  69766. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  69767. +}
  69768. +
  69769. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  69770. + _core_if, dwc_hc_t * _hc)
  69771. +{
  69772. + return (DWC_READ_REG32
  69773. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  69774. +}
  69775. +
  69776. +/**
  69777. + * This function returns the mode of the operation, host or device.
  69778. + *
  69779. + * @return 0 - Device Mode, 1 - Host Mode
  69780. + */
  69781. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  69782. +{
  69783. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  69784. +}
  69785. +
  69786. +/**@}*/
  69787. +
  69788. +/**
  69789. + * DWC_otg CIL callback structure. This structure allows the HCD and
  69790. + * PCD to register functions used for starting and stopping the PCD
  69791. + * and HCD for role change on for a DRD.
  69792. + */
  69793. +typedef struct dwc_otg_cil_callbacks {
  69794. + /** Start function for role change */
  69795. + int (*start) (void *_p);
  69796. + /** Stop Function for role change */
  69797. + int (*stop) (void *_p);
  69798. + /** Disconnect Function for role change */
  69799. + int (*disconnect) (void *_p);
  69800. + /** Resume/Remote wakeup Function */
  69801. + int (*resume_wakeup) (void *_p);
  69802. + /** Suspend function */
  69803. + int (*suspend) (void *_p);
  69804. + /** Session Start (SRP) */
  69805. + int (*session_start) (void *_p);
  69806. +#ifdef CONFIG_USB_DWC_OTG_LPM
  69807. + /** Sleep (switch to L0 state) */
  69808. + int (*sleep) (void *_p);
  69809. +#endif
  69810. + /** Pointer passed to start() and stop() */
  69811. + void *p;
  69812. +} dwc_otg_cil_callbacks_t;
  69813. +
  69814. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  69815. + dwc_otg_cil_callbacks_t * _cb,
  69816. + void *_p);
  69817. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  69818. + dwc_otg_cil_callbacks_t * _cb,
  69819. + void *_p);
  69820. +
  69821. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  69822. +
  69823. +//////////////////////////////////////////////////////////////////////
  69824. +/** Start the HCD. Helper function for using the HCD callbacks.
  69825. + *
  69826. + * @param core_if Programming view of DWC_otg controller.
  69827. + */
  69828. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  69829. +{
  69830. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  69831. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  69832. + }
  69833. +}
  69834. +
  69835. +/** Stop the HCD. Helper function for using the HCD callbacks.
  69836. + *
  69837. + * @param core_if Programming view of DWC_otg controller.
  69838. + */
  69839. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  69840. +{
  69841. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  69842. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  69843. + }
  69844. +}
  69845. +
  69846. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  69847. + *
  69848. + * @param core_if Programming view of DWC_otg controller.
  69849. + */
  69850. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  69851. +{
  69852. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  69853. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  69854. + }
  69855. +}
  69856. +
  69857. +/** Inform the HCD the a New Session has begun. Helper function for
  69858. + * using the HCD callbacks.
  69859. + *
  69860. + * @param core_if Programming view of DWC_otg controller.
  69861. + */
  69862. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  69863. +{
  69864. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  69865. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  69866. + }
  69867. +}
  69868. +
  69869. +#ifdef CONFIG_USB_DWC_OTG_LPM
  69870. +/**
  69871. + * Inform the HCD about LPM sleep.
  69872. + * Helper function for using the HCD callbacks.
  69873. + *
  69874. + * @param core_if Programming view of DWC_otg controller.
  69875. + */
  69876. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  69877. +{
  69878. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  69879. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  69880. + }
  69881. +}
  69882. +#endif
  69883. +
  69884. +/** Resume the HCD. Helper function for using the HCD callbacks.
  69885. + *
  69886. + * @param core_if Programming view of DWC_otg controller.
  69887. + */
  69888. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  69889. +{
  69890. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  69891. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  69892. + }
  69893. +}
  69894. +
  69895. +/** Start the PCD. Helper function for using the PCD callbacks.
  69896. + *
  69897. + * @param core_if Programming view of DWC_otg controller.
  69898. + */
  69899. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  69900. +{
  69901. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  69902. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  69903. + }
  69904. +}
  69905. +
  69906. +/** Stop the PCD. Helper function for using the PCD callbacks.
  69907. + *
  69908. + * @param core_if Programming view of DWC_otg controller.
  69909. + */
  69910. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  69911. +{
  69912. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  69913. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  69914. + }
  69915. +}
  69916. +
  69917. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  69918. + *
  69919. + * @param core_if Programming view of DWC_otg controller.
  69920. + */
  69921. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  69922. +{
  69923. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  69924. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  69925. + }
  69926. +}
  69927. +
  69928. +/** Resume the PCD. Helper function for using the PCD callbacks.
  69929. + *
  69930. + * @param core_if Programming view of DWC_otg controller.
  69931. + */
  69932. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  69933. +{
  69934. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  69935. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  69936. + }
  69937. +}
  69938. +
  69939. +//////////////////////////////////////////////////////////////////////
  69940. +
  69941. +#endif
  69942. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  69943. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 1969-12-31 18:00:00.000000000 -0600
  69944. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 2014-12-11 14:02:55.396418001 -0600
  69945. @@ -0,0 +1,1594 @@
  69946. +/* ==========================================================================
  69947. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  69948. + * $Revision: #32 $
  69949. + * $Date: 2012/08/10 $
  69950. + * $Change: 2047372 $
  69951. + *
  69952. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  69953. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  69954. + * otherwise expressly agreed to in writing between Synopsys and you.
  69955. + *
  69956. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  69957. + * any End User Software License Agreement or Agreement for Licensed Product
  69958. + * with Synopsys or any supplement thereto. You are permitted to use and
  69959. + * redistribute this Software in source and binary forms, with or without
  69960. + * modification, provided that redistributions of source code must retain this
  69961. + * notice. You may not view, use, disclose, copy or distribute this file or
  69962. + * any information contained herein except pursuant to this license grant from
  69963. + * Synopsys. If you do not agree with this notice, including the disclaimer
  69964. + * below, then you are not authorized to use the Software.
  69965. + *
  69966. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  69967. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  69968. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  69969. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  69970. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  69971. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  69972. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  69973. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  69974. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  69975. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  69976. + * DAMAGE.
  69977. + * ========================================================================== */
  69978. +
  69979. +/** @file
  69980. + *
  69981. + * The Core Interface Layer provides basic services for accessing and
  69982. + * managing the DWC_otg hardware. These services are used by both the
  69983. + * Host Controller Driver and the Peripheral Controller Driver.
  69984. + *
  69985. + * This file contains the Common Interrupt handlers.
  69986. + */
  69987. +#include "dwc_os.h"
  69988. +#include "dwc_otg_regs.h"
  69989. +#include "dwc_otg_cil.h"
  69990. +#include "dwc_otg_driver.h"
  69991. +#include "dwc_otg_pcd.h"
  69992. +#include "dwc_otg_hcd.h"
  69993. +
  69994. +#ifdef DEBUG
  69995. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  69996. +{
  69997. + return (core_if->op_state == A_HOST ? "a_host" :
  69998. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  69999. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  70000. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  70001. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  70002. +}
  70003. +#endif
  70004. +
  70005. +/** This function will log a debug message
  70006. + *
  70007. + * @param core_if Programming view of DWC_otg controller.
  70008. + */
  70009. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  70010. +{
  70011. + gintsts_data_t gintsts;
  70012. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  70013. + dwc_otg_mode(core_if) ? "Host" : "Device");
  70014. +
  70015. + /* Clear interrupt */
  70016. + gintsts.d32 = 0;
  70017. + gintsts.b.modemismatch = 1;
  70018. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  70019. + return 1;
  70020. +}
  70021. +
  70022. +/**
  70023. + * This function handles the OTG Interrupts. It reads the OTG
  70024. + * Interrupt Register (GOTGINT) to determine what interrupt has
  70025. + * occurred.
  70026. + *
  70027. + * @param core_if Programming view of DWC_otg controller.
  70028. + */
  70029. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  70030. +{
  70031. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  70032. + gotgint_data_t gotgint;
  70033. + gotgctl_data_t gotgctl;
  70034. + gintmsk_data_t gintmsk;
  70035. + gpwrdn_data_t gpwrdn;
  70036. +
  70037. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  70038. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  70039. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  70040. + op_state_str(core_if));
  70041. +
  70042. + if (gotgint.b.sesenddet) {
  70043. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  70044. + "Session End Detected++ (%s)\n",
  70045. + op_state_str(core_if));
  70046. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  70047. +
  70048. + if (core_if->op_state == B_HOST) {
  70049. + cil_pcd_start(core_if);
  70050. + core_if->op_state = B_PERIPHERAL;
  70051. + } else {
  70052. + /* If not B_HOST and Device HNP still set. HNP
  70053. + * Did not succeed!*/
  70054. + if (gotgctl.b.devhnpen) {
  70055. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  70056. + __DWC_ERROR("Device Not Connected/Responding!\n");
  70057. + }
  70058. +
  70059. + /* If Session End Detected the B-Cable has
  70060. + * been disconnected. */
  70061. + /* Reset PCD and Gadget driver to a
  70062. + * clean state. */
  70063. + core_if->lx_state = DWC_OTG_L0;
  70064. + DWC_SPINUNLOCK(core_if->lock);
  70065. + cil_pcd_stop(core_if);
  70066. + DWC_SPINLOCK(core_if->lock);
  70067. +
  70068. + if (core_if->adp_enable) {
  70069. + if (core_if->power_down == 2) {
  70070. + gpwrdn.d32 = 0;
  70071. + gpwrdn.b.pwrdnswtch = 1;
  70072. + DWC_MODIFY_REG32(&core_if->
  70073. + core_global_regs->
  70074. + gpwrdn, gpwrdn.d32, 0);
  70075. + }
  70076. +
  70077. + gpwrdn.d32 = 0;
  70078. + gpwrdn.b.pmuintsel = 1;
  70079. + gpwrdn.b.pmuactv = 1;
  70080. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70081. + gpwrdn, 0, gpwrdn.d32);
  70082. +
  70083. + dwc_otg_adp_sense_start(core_if);
  70084. + }
  70085. + }
  70086. +
  70087. + gotgctl.d32 = 0;
  70088. + gotgctl.b.devhnpen = 1;
  70089. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  70090. + }
  70091. + if (gotgint.b.sesreqsucstschng) {
  70092. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  70093. + "Session Reqeust Success Status Change++\n");
  70094. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  70095. + if (gotgctl.b.sesreqscs) {
  70096. +
  70097. + if ((core_if->core_params->phy_type ==
  70098. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  70099. + core_if->srp_success = 1;
  70100. + } else {
  70101. + DWC_SPINUNLOCK(core_if->lock);
  70102. + cil_pcd_resume(core_if);
  70103. + DWC_SPINLOCK(core_if->lock);
  70104. + /* Clear Session Request */
  70105. + gotgctl.d32 = 0;
  70106. + gotgctl.b.sesreq = 1;
  70107. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  70108. + gotgctl.d32, 0);
  70109. + }
  70110. + }
  70111. + }
  70112. + if (gotgint.b.hstnegsucstschng) {
  70113. + /* Print statements during the HNP interrupt handling
  70114. + * can cause it to fail.*/
  70115. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  70116. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  70117. + * this does not help*/
  70118. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  70119. + dwc_udelay(100);
  70120. + if (gotgctl.b.hstnegscs) {
  70121. + if (dwc_otg_is_host_mode(core_if)) {
  70122. + core_if->op_state = B_HOST;
  70123. + /*
  70124. + * Need to disable SOF interrupt immediately.
  70125. + * When switching from device to host, the PCD
  70126. + * interrupt handler won't handle the
  70127. + * interrupt if host mode is already set. The
  70128. + * HCD interrupt handler won't get called if
  70129. + * the HCD state is HALT. This means that the
  70130. + * interrupt does not get handled and Linux
  70131. + * complains loudly.
  70132. + */
  70133. + gintmsk.d32 = 0;
  70134. + gintmsk.b.sofintr = 1;
  70135. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  70136. + gintmsk.d32, 0);
  70137. + /* Call callback function with spin lock released */
  70138. + DWC_SPINUNLOCK(core_if->lock);
  70139. + cil_pcd_stop(core_if);
  70140. + /*
  70141. + * Initialize the Core for Host mode.
  70142. + */
  70143. + cil_hcd_start(core_if);
  70144. + DWC_SPINLOCK(core_if->lock);
  70145. + core_if->op_state = B_HOST;
  70146. + }
  70147. + } else {
  70148. + gotgctl.d32 = 0;
  70149. + gotgctl.b.hnpreq = 1;
  70150. + gotgctl.b.devhnpen = 1;
  70151. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  70152. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  70153. + __DWC_ERROR("Device Not Connected/Responding\n");
  70154. + }
  70155. + }
  70156. + if (gotgint.b.hstnegdet) {
  70157. + /* The disconnect interrupt is set at the same time as
  70158. + * Host Negotiation Detected. During the mode
  70159. + * switch all interrupts are cleared so the disconnect
  70160. + * interrupt handler will not get executed.
  70161. + */
  70162. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  70163. + "Host Negotiation Detected++ (%s)\n",
  70164. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  70165. + "Device"));
  70166. + if (dwc_otg_is_device_mode(core_if)) {
  70167. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  70168. + core_if->op_state);
  70169. + DWC_SPINUNLOCK(core_if->lock);
  70170. + cil_hcd_disconnect(core_if);
  70171. + cil_pcd_start(core_if);
  70172. + DWC_SPINLOCK(core_if->lock);
  70173. + core_if->op_state = A_PERIPHERAL;
  70174. + } else {
  70175. + /*
  70176. + * Need to disable SOF interrupt immediately. When
  70177. + * switching from device to host, the PCD interrupt
  70178. + * handler won't handle the interrupt if host mode is
  70179. + * already set. The HCD interrupt handler won't get
  70180. + * called if the HCD state is HALT. This means that
  70181. + * the interrupt does not get handled and Linux
  70182. + * complains loudly.
  70183. + */
  70184. + gintmsk.d32 = 0;
  70185. + gintmsk.b.sofintr = 1;
  70186. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  70187. + DWC_SPINUNLOCK(core_if->lock);
  70188. + cil_pcd_stop(core_if);
  70189. + cil_hcd_start(core_if);
  70190. + DWC_SPINLOCK(core_if->lock);
  70191. + core_if->op_state = A_HOST;
  70192. + }
  70193. + }
  70194. + if (gotgint.b.adevtoutchng) {
  70195. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  70196. + "A-Device Timeout Change++\n");
  70197. + }
  70198. + if (gotgint.b.debdone) {
  70199. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  70200. + }
  70201. +
  70202. + /* Clear GOTGINT */
  70203. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  70204. +
  70205. + return 1;
  70206. +}
  70207. +
  70208. +void w_conn_id_status_change(void *p)
  70209. +{
  70210. + dwc_otg_core_if_t *core_if = p;
  70211. + uint32_t count = 0;
  70212. + gotgctl_data_t gotgctl = {.d32 = 0 };
  70213. +
  70214. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  70215. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  70216. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  70217. +
  70218. + /* B-Device connector (Device Mode) */
  70219. + if (gotgctl.b.conidsts) {
  70220. + /* Wait for switch to device mode. */
  70221. + while (!dwc_otg_is_device_mode(core_if)) {
  70222. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  70223. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  70224. + "Peripheral"));
  70225. + dwc_mdelay(100);
  70226. + if (++count > 10000)
  70227. + break;
  70228. + }
  70229. + DWC_ASSERT(++count < 10000,
  70230. + "Connection id status change timed out");
  70231. + core_if->op_state = B_PERIPHERAL;
  70232. + dwc_otg_core_init(core_if);
  70233. + dwc_otg_enable_global_interrupts(core_if);
  70234. + cil_pcd_start(core_if);
  70235. + } else {
  70236. + /* A-Device connector (Host Mode) */
  70237. + while (!dwc_otg_is_host_mode(core_if)) {
  70238. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  70239. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  70240. + "Peripheral"));
  70241. + dwc_mdelay(100);
  70242. + if (++count > 10000)
  70243. + break;
  70244. + }
  70245. + DWC_ASSERT(++count < 10000,
  70246. + "Connection id status change timed out");
  70247. + core_if->op_state = A_HOST;
  70248. + /*
  70249. + * Initialize the Core for Host mode.
  70250. + */
  70251. + dwc_otg_core_init(core_if);
  70252. + dwc_otg_enable_global_interrupts(core_if);
  70253. + cil_hcd_start(core_if);
  70254. + }
  70255. +}
  70256. +
  70257. +/**
  70258. + * This function handles the Connector ID Status Change Interrupt. It
  70259. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  70260. + * is a Device to Host Mode transition or a Host Mode to Device
  70261. + * Transition.
  70262. + *
  70263. + * This only occurs when the cable is connected/removed from the PHY
  70264. + * connector.
  70265. + *
  70266. + * @param core_if Programming view of DWC_otg controller.
  70267. + */
  70268. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  70269. +{
  70270. +
  70271. + /*
  70272. + * Need to disable SOF interrupt immediately. If switching from device
  70273. + * to host, the PCD interrupt handler won't handle the interrupt if
  70274. + * host mode is already set. The HCD interrupt handler won't get
  70275. + * called if the HCD state is HALT. This means that the interrupt does
  70276. + * not get handled and Linux complains loudly.
  70277. + */
  70278. + gintmsk_data_t gintmsk = {.d32 = 0 };
  70279. + gintsts_data_t gintsts = {.d32 = 0 };
  70280. +
  70281. + gintmsk.b.sofintr = 1;
  70282. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  70283. +
  70284. + DWC_DEBUGPL(DBG_CIL,
  70285. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  70286. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  70287. +
  70288. + DWC_SPINUNLOCK(core_if->lock);
  70289. +
  70290. + /*
  70291. + * Need to schedule a work, as there are possible DELAY function calls
  70292. + * Release lock before scheduling workq as it holds spinlock during scheduling
  70293. + */
  70294. +
  70295. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  70296. + core_if, "connection id status change");
  70297. + DWC_SPINLOCK(core_if->lock);
  70298. +
  70299. + /* Set flag and clear interrupt */
  70300. + gintsts.b.conidstschng = 1;
  70301. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  70302. +
  70303. + return 1;
  70304. +}
  70305. +
  70306. +/**
  70307. + * This interrupt indicates that a device is initiating the Session
  70308. + * Request Protocol to request the host to turn on bus power so a new
  70309. + * session can begin. The handler responds by turning on bus power. If
  70310. + * the DWC_otg controller is in low power mode, the handler brings the
  70311. + * controller out of low power mode before turning on bus power.
  70312. + *
  70313. + * @param core_if Programming view of DWC_otg controller.
  70314. + */
  70315. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  70316. +{
  70317. + gintsts_data_t gintsts;
  70318. +
  70319. +#ifndef DWC_HOST_ONLY
  70320. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  70321. +
  70322. + if (dwc_otg_is_device_mode(core_if)) {
  70323. + DWC_PRINTF("SRP: Device mode\n");
  70324. + } else {
  70325. + hprt0_data_t hprt0;
  70326. + DWC_PRINTF("SRP: Host mode\n");
  70327. +
  70328. + /* Turn on the port power bit. */
  70329. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70330. + hprt0.b.prtpwr = 1;
  70331. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70332. +
  70333. + /* Start the Connection timer. So a message can be displayed
  70334. + * if connect does not occur within 10 seconds. */
  70335. + cil_hcd_session_start(core_if);
  70336. + }
  70337. +#endif
  70338. +
  70339. + /* Clear interrupt */
  70340. + gintsts.d32 = 0;
  70341. + gintsts.b.sessreqintr = 1;
  70342. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  70343. +
  70344. + return 1;
  70345. +}
  70346. +
  70347. +void w_wakeup_detected(void *p)
  70348. +{
  70349. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  70350. + /*
  70351. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  70352. + * so that OPT tests pass with all PHYs).
  70353. + */
  70354. + hprt0_data_t hprt0 = {.d32 = 0 };
  70355. +#if 0
  70356. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70357. + /* Restart the Phy Clock */
  70358. + pcgcctl.b.stoppclk = 1;
  70359. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  70360. + dwc_udelay(10);
  70361. +#endif //0
  70362. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70363. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  70364. +// dwc_mdelay(70);
  70365. + hprt0.b.prtres = 0; /* Resume */
  70366. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70367. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  70368. + DWC_READ_REG32(core_if->host_if->hprt0));
  70369. +
  70370. + cil_hcd_resume(core_if);
  70371. +
  70372. + /** Change to L0 state*/
  70373. + core_if->lx_state = DWC_OTG_L0;
  70374. +}
  70375. +
  70376. +/**
  70377. + * This interrupt indicates that the DWC_otg controller has detected a
  70378. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  70379. + * low power mode, the handler must brings the controller out of low
  70380. + * power mode. The controller automatically begins resume
  70381. + * signaling. The handler schedules a time to stop resume signaling.
  70382. + */
  70383. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  70384. +{
  70385. + gintsts_data_t gintsts;
  70386. +
  70387. + DWC_DEBUGPL(DBG_ANY,
  70388. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  70389. +
  70390. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  70391. +
  70392. + if (dwc_otg_is_device_mode(core_if)) {
  70393. + dctl_data_t dctl = {.d32 = 0 };
  70394. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  70395. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  70396. + dsts));
  70397. + if (core_if->lx_state == DWC_OTG_L2) {
  70398. +#ifdef PARTIAL_POWER_DOWN
  70399. + if (core_if->hwcfg4.b.power_optimiz) {
  70400. + pcgcctl_data_t power = {.d32 = 0 };
  70401. +
  70402. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  70403. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  70404. + power.d32);
  70405. +
  70406. + power.b.stoppclk = 0;
  70407. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  70408. +
  70409. + power.b.pwrclmp = 0;
  70410. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  70411. +
  70412. + power.b.rstpdwnmodule = 0;
  70413. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  70414. + }
  70415. +#endif
  70416. + /* Clear the Remote Wakeup Signaling */
  70417. + dctl.b.rmtwkupsig = 1;
  70418. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  70419. + dctl, dctl.d32, 0);
  70420. +
  70421. + DWC_SPINUNLOCK(core_if->lock);
  70422. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  70423. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  70424. + }
  70425. + DWC_SPINLOCK(core_if->lock);
  70426. + } else {
  70427. + glpmcfg_data_t lpmcfg;
  70428. + lpmcfg.d32 =
  70429. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70430. + lpmcfg.b.hird_thres &= (~(1 << 4));
  70431. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  70432. + lpmcfg.d32);
  70433. + }
  70434. + /** Change to L0 state*/
  70435. + core_if->lx_state = DWC_OTG_L0;
  70436. + } else {
  70437. + if (core_if->lx_state != DWC_OTG_L1) {
  70438. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70439. +
  70440. + /* Restart the Phy Clock */
  70441. + pcgcctl.b.stoppclk = 1;
  70442. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  70443. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  70444. + } else {
  70445. + /** Change to L0 state*/
  70446. + core_if->lx_state = DWC_OTG_L0;
  70447. + }
  70448. + }
  70449. +
  70450. + /* Clear interrupt */
  70451. + gintsts.d32 = 0;
  70452. + gintsts.b.wkupintr = 1;
  70453. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  70454. +
  70455. + return 1;
  70456. +}
  70457. +
  70458. +/**
  70459. + * This interrupt indicates that the Wakeup Logic has detected a
  70460. + * Device disconnect.
  70461. + */
  70462. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  70463. +{
  70464. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  70465. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  70466. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70467. +
  70468. + DWC_PRINTF("%s called\n", __FUNCTION__);
  70469. +
  70470. + if (!core_if->hibernation_suspend) {
  70471. + DWC_PRINTF("Already exited from Hibernation\n");
  70472. + return 1;
  70473. + }
  70474. +
  70475. + /* Switch on the voltage to the core */
  70476. + gpwrdn.b.pwrdnswtch = 1;
  70477. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70478. + dwc_udelay(10);
  70479. +
  70480. + /* Reset the core */
  70481. + gpwrdn.d32 = 0;
  70482. + gpwrdn.b.pwrdnrstn = 1;
  70483. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70484. + dwc_udelay(10);
  70485. +
  70486. + /* Disable power clamps*/
  70487. + gpwrdn.d32 = 0;
  70488. + gpwrdn.b.pwrdnclmp = 1;
  70489. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70490. +
  70491. + /* Remove reset the core signal */
  70492. + gpwrdn.d32 = 0;
  70493. + gpwrdn.b.pwrdnrstn = 1;
  70494. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  70495. + dwc_udelay(10);
  70496. +
  70497. + /* Disable PMU interrupt */
  70498. + gpwrdn.d32 = 0;
  70499. + gpwrdn.b.pmuintsel = 1;
  70500. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70501. +
  70502. + core_if->hibernation_suspend = 0;
  70503. +
  70504. + /* Disable PMU */
  70505. + gpwrdn.d32 = 0;
  70506. + gpwrdn.b.pmuactv = 1;
  70507. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70508. + dwc_udelay(10);
  70509. +
  70510. + if (gpwrdn_temp.b.idsts) {
  70511. + core_if->op_state = B_PERIPHERAL;
  70512. + dwc_otg_core_init(core_if);
  70513. + dwc_otg_enable_global_interrupts(core_if);
  70514. + cil_pcd_start(core_if);
  70515. + } else {
  70516. + core_if->op_state = A_HOST;
  70517. + dwc_otg_core_init(core_if);
  70518. + dwc_otg_enable_global_interrupts(core_if);
  70519. + cil_hcd_start(core_if);
  70520. + }
  70521. +
  70522. + return 1;
  70523. +}
  70524. +
  70525. +/**
  70526. + * This interrupt indicates that the Wakeup Logic has detected a
  70527. + * remote wakeup sequence.
  70528. + */
  70529. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  70530. +{
  70531. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70532. + DWC_DEBUGPL(DBG_ANY,
  70533. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  70534. +
  70535. + if (!core_if->hibernation_suspend) {
  70536. + DWC_PRINTF("Already exited from Hibernation\n");
  70537. + return 1;
  70538. + }
  70539. +
  70540. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70541. + if (gpwrdn.b.idsts) { // Device Mode
  70542. + if ((core_if->power_down == 2)
  70543. + && (core_if->hibernation_suspend == 1)) {
  70544. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  70545. + }
  70546. + } else {
  70547. + if ((core_if->power_down == 2)
  70548. + && (core_if->hibernation_suspend == 1)) {
  70549. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  70550. + }
  70551. + }
  70552. + return 1;
  70553. +}
  70554. +
  70555. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  70556. +{
  70557. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70558. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  70559. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  70560. +
  70561. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  70562. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70563. + if (core_if->power_down == 2) {
  70564. + if (!core_if->hibernation_suspend) {
  70565. + DWC_PRINTF("Already exited from Hibernation\n");
  70566. + return 1;
  70567. + }
  70568. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  70569. + /* Switch on the voltage to the core */
  70570. + gpwrdn.b.pwrdnswtch = 1;
  70571. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70572. + dwc_udelay(10);
  70573. +
  70574. + /* Reset the core */
  70575. + gpwrdn.d32 = 0;
  70576. + gpwrdn.b.pwrdnrstn = 1;
  70577. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70578. + dwc_udelay(10);
  70579. +
  70580. + /* Disable power clamps */
  70581. + gpwrdn.d32 = 0;
  70582. + gpwrdn.b.pwrdnclmp = 1;
  70583. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70584. +
  70585. + /* Remove reset the core signal */
  70586. + gpwrdn.d32 = 0;
  70587. + gpwrdn.b.pwrdnrstn = 1;
  70588. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  70589. + dwc_udelay(10);
  70590. +
  70591. + /* Disable PMU interrupt */
  70592. + gpwrdn.d32 = 0;
  70593. + gpwrdn.b.pmuintsel = 1;
  70594. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70595. +
  70596. + /*Indicates that we are exiting from hibernation */
  70597. + core_if->hibernation_suspend = 0;
  70598. +
  70599. + /* Disable PMU */
  70600. + gpwrdn.d32 = 0;
  70601. + gpwrdn.b.pmuactv = 1;
  70602. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70603. + dwc_udelay(10);
  70604. +
  70605. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  70606. + if (gpwrdn.b.dis_vbus == 1) {
  70607. + gpwrdn.d32 = 0;
  70608. + gpwrdn.b.dis_vbus = 1;
  70609. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70610. + }
  70611. +
  70612. + if (gpwrdn_temp.b.idsts) {
  70613. + core_if->op_state = B_PERIPHERAL;
  70614. + dwc_otg_core_init(core_if);
  70615. + dwc_otg_enable_global_interrupts(core_if);
  70616. + cil_pcd_start(core_if);
  70617. + } else {
  70618. + core_if->op_state = A_HOST;
  70619. + dwc_otg_core_init(core_if);
  70620. + dwc_otg_enable_global_interrupts(core_if);
  70621. + cil_hcd_start(core_if);
  70622. + }
  70623. + }
  70624. +
  70625. + if (core_if->adp_enable) {
  70626. + uint8_t is_host = 0;
  70627. + DWC_SPINUNLOCK(core_if->lock);
  70628. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  70629. +#ifndef DWC_HOST_ONLY
  70630. + if (gpwrdn_temp.b.idsts)
  70631. + core_if->lock = otg_dev->pcd->lock;
  70632. +#endif
  70633. +#ifndef DWC_DEVICE_ONLY
  70634. + if (!gpwrdn_temp.b.idsts) {
  70635. + core_if->lock = otg_dev->hcd->lock;
  70636. + is_host = 1;
  70637. + }
  70638. +#endif
  70639. + DWC_PRINTF("RESTART ADP\n");
  70640. + if (core_if->adp.probe_enabled)
  70641. + dwc_otg_adp_probe_stop(core_if);
  70642. + if (core_if->adp.sense_enabled)
  70643. + dwc_otg_adp_sense_stop(core_if);
  70644. + if (core_if->adp.sense_timer_started)
  70645. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  70646. + if (core_if->adp.vbuson_timer_started)
  70647. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  70648. + core_if->adp.probe_timer_values[0] = -1;
  70649. + core_if->adp.probe_timer_values[1] = -1;
  70650. + core_if->adp.sense_timer_started = 0;
  70651. + core_if->adp.vbuson_timer_started = 0;
  70652. + core_if->adp.probe_counter = 0;
  70653. + core_if->adp.gpwrdn = 0;
  70654. +
  70655. + /* Disable PMU and restart ADP */
  70656. + gpwrdn_temp.d32 = 0;
  70657. + gpwrdn_temp.b.pmuactv = 1;
  70658. + gpwrdn_temp.b.pmuintsel = 1;
  70659. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70660. + DWC_PRINTF("Check point 1\n");
  70661. + dwc_mdelay(110);
  70662. + dwc_otg_adp_start(core_if, is_host);
  70663. + DWC_SPINLOCK(core_if->lock);
  70664. + }
  70665. +
  70666. +
  70667. + return 1;
  70668. +}
  70669. +
  70670. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  70671. +{
  70672. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70673. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  70674. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  70675. +
  70676. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70677. + if (core_if->power_down == 2) {
  70678. + if (!core_if->hibernation_suspend) {
  70679. + DWC_PRINTF("Already exited from Hibernation\n");
  70680. + return 1;
  70681. + }
  70682. +
  70683. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  70684. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  70685. + gpwrdn.b.bsessvld == 0) {
  70686. + /* Save gpwrdn register for further usage if stschng interrupt */
  70687. + core_if->gr_backup->gpwrdn_local =
  70688. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70689. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  70690. + return 1;
  70691. + }
  70692. +
  70693. + /* Switch on the voltage to the core */
  70694. + gpwrdn.d32 = 0;
  70695. + gpwrdn.b.pwrdnswtch = 1;
  70696. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70697. + dwc_udelay(10);
  70698. +
  70699. + /* Reset the core */
  70700. + gpwrdn.d32 = 0;
  70701. + gpwrdn.b.pwrdnrstn = 1;
  70702. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70703. + dwc_udelay(10);
  70704. +
  70705. + /* Disable power clamps */
  70706. + gpwrdn.d32 = 0;
  70707. + gpwrdn.b.pwrdnclmp = 1;
  70708. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70709. +
  70710. + /* Remove reset the core signal */
  70711. + gpwrdn.d32 = 0;
  70712. + gpwrdn.b.pwrdnrstn = 1;
  70713. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  70714. + dwc_udelay(10);
  70715. +
  70716. + /* Disable PMU interrupt */
  70717. + gpwrdn.d32 = 0;
  70718. + gpwrdn.b.pmuintsel = 1;
  70719. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70720. + dwc_udelay(10);
  70721. +
  70722. + /*Indicates that we are exiting from hibernation */
  70723. + core_if->hibernation_suspend = 0;
  70724. +
  70725. + /* Disable PMU */
  70726. + gpwrdn.d32 = 0;
  70727. + gpwrdn.b.pmuactv = 1;
  70728. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70729. + dwc_udelay(10);
  70730. +
  70731. + core_if->op_state = B_PERIPHERAL;
  70732. + dwc_otg_core_init(core_if);
  70733. + dwc_otg_enable_global_interrupts(core_if);
  70734. + cil_pcd_start(core_if);
  70735. +
  70736. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  70737. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  70738. + /*
  70739. + * Initiate SRP after initial ADP probe.
  70740. + */
  70741. + dwc_otg_initiate_srp(core_if);
  70742. + }
  70743. + }
  70744. +
  70745. + return 1;
  70746. +}
  70747. +/**
  70748. + * This interrupt indicates that the Wakeup Logic has detected a
  70749. + * status change either on IDDIG or BSessVld.
  70750. + */
  70751. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  70752. +{
  70753. + int retval;
  70754. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70755. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  70756. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  70757. +
  70758. + DWC_PRINTF("%s called\n", __FUNCTION__);
  70759. +
  70760. + if (core_if->power_down == 2) {
  70761. + if (core_if->hibernation_suspend <= 0) {
  70762. + DWC_PRINTF("Already exited from Hibernation\n");
  70763. + return 1;
  70764. + } else
  70765. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  70766. +
  70767. + } else {
  70768. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  70769. + }
  70770. +
  70771. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70772. +
  70773. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  70774. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  70775. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  70776. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  70777. + }
  70778. +
  70779. + return retval;
  70780. +}
  70781. +
  70782. +/**
  70783. + * This interrupt indicates that the Wakeup Logic has detected a
  70784. + * SRP.
  70785. + */
  70786. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  70787. +{
  70788. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70789. +
  70790. + DWC_PRINTF("%s called\n", __FUNCTION__);
  70791. +
  70792. + if (!core_if->hibernation_suspend) {
  70793. + DWC_PRINTF("Already exited from Hibernation\n");
  70794. + return 1;
  70795. + }
  70796. +#ifdef DWC_DEV_SRPCAP
  70797. + if (core_if->pwron_timer_started) {
  70798. + core_if->pwron_timer_started = 0;
  70799. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  70800. + }
  70801. +#endif
  70802. +
  70803. + /* Switch on the voltage to the core */
  70804. + gpwrdn.b.pwrdnswtch = 1;
  70805. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70806. + dwc_udelay(10);
  70807. +
  70808. + /* Reset the core */
  70809. + gpwrdn.d32 = 0;
  70810. + gpwrdn.b.pwrdnrstn = 1;
  70811. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70812. + dwc_udelay(10);
  70813. +
  70814. + /* Disable power clamps */
  70815. + gpwrdn.d32 = 0;
  70816. + gpwrdn.b.pwrdnclmp = 1;
  70817. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70818. +
  70819. + /* Remove reset the core signal */
  70820. + gpwrdn.d32 = 0;
  70821. + gpwrdn.b.pwrdnrstn = 1;
  70822. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  70823. + dwc_udelay(10);
  70824. +
  70825. + /* Disable PMU interrupt */
  70826. + gpwrdn.d32 = 0;
  70827. + gpwrdn.b.pmuintsel = 1;
  70828. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70829. +
  70830. + /* Indicates that we are exiting from hibernation */
  70831. + core_if->hibernation_suspend = 0;
  70832. +
  70833. + /* Disable PMU */
  70834. + gpwrdn.d32 = 0;
  70835. + gpwrdn.b.pmuactv = 1;
  70836. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70837. + dwc_udelay(10);
  70838. +
  70839. + /* Programm Disable VBUS to 0 */
  70840. + gpwrdn.d32 = 0;
  70841. + gpwrdn.b.dis_vbus = 1;
  70842. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  70843. +
  70844. + /*Initialize the core as Host */
  70845. + core_if->op_state = A_HOST;
  70846. + dwc_otg_core_init(core_if);
  70847. + dwc_otg_enable_global_interrupts(core_if);
  70848. + cil_hcd_start(core_if);
  70849. +
  70850. + return 1;
  70851. +}
  70852. +
  70853. +/** This interrupt indicates that restore command after Hibernation
  70854. + * was completed by the core. */
  70855. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  70856. +{
  70857. + pcgcctl_data_t pcgcctl;
  70858. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  70859. +
  70860. + //TODO De-assert restore signal. 8.a
  70861. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  70862. + if (pcgcctl.b.restoremode == 1) {
  70863. + gintmsk_data_t gintmsk = {.d32 = 0 };
  70864. + /*
  70865. + * If restore mode is Remote Wakeup,
  70866. + * unmask Remote Wakeup interrupt.
  70867. + */
  70868. + gintmsk.b.wkupintr = 1;
  70869. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  70870. + 0, gintmsk.d32);
  70871. + }
  70872. +
  70873. + return 1;
  70874. +}
  70875. +
  70876. +/**
  70877. + * This interrupt indicates that a device has been disconnected from
  70878. + * the root port.
  70879. + */
  70880. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  70881. +{
  70882. + gintsts_data_t gintsts;
  70883. +
  70884. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  70885. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  70886. + op_state_str(core_if));
  70887. +
  70888. +/** @todo Consolidate this if statement. */
  70889. +#ifndef DWC_HOST_ONLY
  70890. + if (core_if->op_state == B_HOST) {
  70891. + /* If in device mode Disconnect and stop the HCD, then
  70892. + * start the PCD. */
  70893. + DWC_SPINUNLOCK(core_if->lock);
  70894. + cil_hcd_disconnect(core_if);
  70895. + cil_pcd_start(core_if);
  70896. + DWC_SPINLOCK(core_if->lock);
  70897. + core_if->op_state = B_PERIPHERAL;
  70898. + } else if (dwc_otg_is_device_mode(core_if)) {
  70899. + gotgctl_data_t gotgctl = {.d32 = 0 };
  70900. + gotgctl.d32 =
  70901. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  70902. + if (gotgctl.b.hstsethnpen == 1) {
  70903. + /* Do nothing, if HNP in process the OTG
  70904. + * interrupt "Host Negotiation Detected"
  70905. + * interrupt will do the mode switch.
  70906. + */
  70907. + } else if (gotgctl.b.devhnpen == 0) {
  70908. + /* If in device mode Disconnect and stop the HCD, then
  70909. + * start the PCD. */
  70910. + DWC_SPINUNLOCK(core_if->lock);
  70911. + cil_hcd_disconnect(core_if);
  70912. + cil_pcd_start(core_if);
  70913. + DWC_SPINLOCK(core_if->lock);
  70914. + core_if->op_state = B_PERIPHERAL;
  70915. + } else {
  70916. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  70917. + }
  70918. + } else {
  70919. + if (core_if->op_state == A_HOST) {
  70920. + /* A-Cable still connected but device disconnected. */
  70921. + cil_hcd_disconnect(core_if);
  70922. + if (core_if->adp_enable) {
  70923. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  70924. + cil_hcd_stop(core_if);
  70925. + /* Enable Power Down Logic */
  70926. + gpwrdn.b.pmuintsel = 1;
  70927. + gpwrdn.b.pmuactv = 1;
  70928. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70929. + gpwrdn, 0, gpwrdn.d32);
  70930. + dwc_otg_adp_probe_start(core_if);
  70931. +
  70932. + /* Power off the core */
  70933. + if (core_if->power_down == 2) {
  70934. + gpwrdn.d32 = 0;
  70935. + gpwrdn.b.pwrdnswtch = 1;
  70936. + DWC_MODIFY_REG32
  70937. + (&core_if->core_global_regs->gpwrdn,
  70938. + gpwrdn.d32, 0);
  70939. + }
  70940. + }
  70941. + }
  70942. + }
  70943. +#endif
  70944. + /* Change to L3(OFF) state */
  70945. + core_if->lx_state = DWC_OTG_L3;
  70946. +
  70947. + gintsts.d32 = 0;
  70948. + gintsts.b.disconnect = 1;
  70949. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  70950. + return 1;
  70951. +}
  70952. +
  70953. +/**
  70954. + * This interrupt indicates that SUSPEND state has been detected on
  70955. + * the USB.
  70956. + *
  70957. + * For HNP the USB Suspend interrupt signals the change from
  70958. + * "a_peripheral" to "a_host".
  70959. + *
  70960. + * When power management is enabled the core will be put in low power
  70961. + * mode.
  70962. + */
  70963. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  70964. +{
  70965. + dsts_data_t dsts;
  70966. + gintsts_data_t gintsts;
  70967. + dcfg_data_t dcfg;
  70968. +
  70969. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  70970. +
  70971. + if (dwc_otg_is_device_mode(core_if)) {
  70972. + /* Check the Device status register to determine if the Suspend
  70973. + * state is active. */
  70974. + dsts.d32 =
  70975. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  70976. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  70977. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  70978. + "HWCFG4.power Optimize=%d\n",
  70979. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  70980. +
  70981. +#ifdef PARTIAL_POWER_DOWN
  70982. +/** @todo Add a module parameter for power management. */
  70983. +
  70984. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  70985. + pcgcctl_data_t power = {.d32 = 0 };
  70986. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  70987. +
  70988. + power.b.pwrclmp = 1;
  70989. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  70990. +
  70991. + power.b.rstpdwnmodule = 1;
  70992. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  70993. +
  70994. + power.b.stoppclk = 1;
  70995. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  70996. +
  70997. + } else {
  70998. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  70999. + }
  71000. +#endif
  71001. + /* PCD callback for suspend. Release the lock inside of callback function */
  71002. + cil_pcd_suspend(core_if);
  71003. + if (core_if->power_down == 2)
  71004. + {
  71005. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  71006. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  71007. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  71008. +
  71009. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  71010. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71011. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71012. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  71013. +
  71014. + /* Change to L2(suspend) state */
  71015. + core_if->lx_state = DWC_OTG_L2;
  71016. +
  71017. + /* Clear interrupt in gintsts */
  71018. + gintsts.d32 = 0;
  71019. + gintsts.b.usbsuspend = 1;
  71020. + DWC_WRITE_REG32(&core_if->core_global_regs->
  71021. + gintsts, gintsts.d32);
  71022. + DWC_PRINTF("Start of hibernation completed\n");
  71023. + dwc_otg_save_global_regs(core_if);
  71024. + dwc_otg_save_dev_regs(core_if);
  71025. +
  71026. + gusbcfg.d32 =
  71027. + DWC_READ_REG32(&core_if->core_global_regs->
  71028. + gusbcfg);
  71029. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  71030. + /* ULPI interface */
  71031. + /* Suspend the Phy Clock */
  71032. + pcgcctl.d32 = 0;
  71033. + pcgcctl.b.stoppclk = 1;
  71034. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71035. + pcgcctl.d32);
  71036. + dwc_udelay(10);
  71037. + gpwrdn.b.pmuactv = 1;
  71038. + DWC_MODIFY_REG32(&core_if->
  71039. + core_global_regs->
  71040. + gpwrdn, 0, gpwrdn.d32);
  71041. + } else {
  71042. + /* UTMI+ Interface */
  71043. + gpwrdn.b.pmuactv = 1;
  71044. + DWC_MODIFY_REG32(&core_if->
  71045. + core_global_regs->
  71046. + gpwrdn, 0, gpwrdn.d32);
  71047. + dwc_udelay(10);
  71048. + pcgcctl.b.stoppclk = 1;
  71049. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71050. + pcgcctl.d32);
  71051. + dwc_udelay(10);
  71052. + }
  71053. +
  71054. + /* Set flag to indicate that we are in hibernation */
  71055. + core_if->hibernation_suspend = 1;
  71056. + /* Enable interrupts from wake up logic */
  71057. + gpwrdn.d32 = 0;
  71058. + gpwrdn.b.pmuintsel = 1;
  71059. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71060. + gpwrdn, 0, gpwrdn.d32);
  71061. + dwc_udelay(10);
  71062. +
  71063. + /* Unmask device mode interrupts in GPWRDN */
  71064. + gpwrdn.d32 = 0;
  71065. + gpwrdn.b.rst_det_msk = 1;
  71066. + gpwrdn.b.lnstchng_msk = 1;
  71067. + gpwrdn.b.sts_chngint_msk = 1;
  71068. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71069. + gpwrdn, 0, gpwrdn.d32);
  71070. + dwc_udelay(10);
  71071. +
  71072. + /* Enable Power Down Clamp */
  71073. + gpwrdn.d32 = 0;
  71074. + gpwrdn.b.pwrdnclmp = 1;
  71075. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71076. + gpwrdn, 0, gpwrdn.d32);
  71077. + dwc_udelay(10);
  71078. +
  71079. + /* Switch off VDD */
  71080. + gpwrdn.d32 = 0;
  71081. + gpwrdn.b.pwrdnswtch = 1;
  71082. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71083. + gpwrdn, 0, gpwrdn.d32);
  71084. +
  71085. + /* Save gpwrdn register for further usage if stschng interrupt */
  71086. + core_if->gr_backup->gpwrdn_local =
  71087. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71088. + DWC_PRINTF("Hibernation completed\n");
  71089. +
  71090. + return 1;
  71091. + }
  71092. + } else if (core_if->power_down == 3) {
  71093. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71094. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  71095. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  71096. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  71097. +
  71098. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  71099. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  71100. + core_if->xhib = 1;
  71101. +
  71102. + /* Clear interrupt in gintsts */
  71103. + gintsts.d32 = 0;
  71104. + gintsts.b.usbsuspend = 1;
  71105. + DWC_WRITE_REG32(&core_if->core_global_regs->
  71106. + gintsts, gintsts.d32);
  71107. +
  71108. + dwc_otg_save_global_regs(core_if);
  71109. + dwc_otg_save_dev_regs(core_if);
  71110. +
  71111. + /* Wait for 10 PHY clocks */
  71112. + dwc_udelay(10);
  71113. +
  71114. + /* Program GPIO register while entering to xHib */
  71115. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  71116. +
  71117. + pcgcctl.b.enbl_extnd_hiber = 1;
  71118. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  71119. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  71120. +
  71121. + pcgcctl.d32 = 0;
  71122. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  71123. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  71124. +
  71125. + pcgcctl.d32 = 0;
  71126. + pcgcctl.b.extnd_hiber_switch = 1;
  71127. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71128. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  71129. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  71130. +
  71131. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  71132. +
  71133. + return 1;
  71134. + }
  71135. + }
  71136. + } else {
  71137. + if (core_if->op_state == A_PERIPHERAL) {
  71138. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  71139. + /* Clear the a_peripheral flag, back to a_host. */
  71140. + DWC_SPINUNLOCK(core_if->lock);
  71141. + cil_pcd_stop(core_if);
  71142. + cil_hcd_start(core_if);
  71143. + DWC_SPINLOCK(core_if->lock);
  71144. + core_if->op_state = A_HOST;
  71145. + }
  71146. + }
  71147. +
  71148. + /* Change to L2(suspend) state */
  71149. + core_if->lx_state = DWC_OTG_L2;
  71150. +
  71151. + /* Clear interrupt */
  71152. + gintsts.d32 = 0;
  71153. + gintsts.b.usbsuspend = 1;
  71154. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  71155. +
  71156. + return 1;
  71157. +}
  71158. +
  71159. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  71160. +{
  71161. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71162. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71163. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  71164. +
  71165. + dwc_udelay(10);
  71166. +
  71167. + /* Program GPIO register while entering to xHib */
  71168. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  71169. +
  71170. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  71171. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  71172. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  71173. + dwc_udelay(10);
  71174. +
  71175. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  71176. + gpwrdn.b.restore = 1;
  71177. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  71178. + dwc_udelay(10);
  71179. +
  71180. + restore_lpm_i2c_regs(core_if);
  71181. +
  71182. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  71183. + pcgcctl.b.max_xcvrselect = 1;
  71184. + pcgcctl.b.ess_reg_restored = 0;
  71185. + pcgcctl.b.extnd_hiber_switch = 0;
  71186. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  71187. + pcgcctl.b.enbl_extnd_hiber = 1;
  71188. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  71189. +
  71190. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  71191. + gahbcfg.b.glblintrmsk = 1;
  71192. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  71193. +
  71194. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  71195. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  71196. +
  71197. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  71198. + core_if->gr_backup->gusbcfg_local);
  71199. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  71200. + core_if->dr_backup->dcfg);
  71201. +
  71202. + pcgcctl.d32 = 0;
  71203. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  71204. + pcgcctl.b.max_xcvrselect = 1;
  71205. + pcgcctl.d32 |= 0x608;
  71206. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  71207. + dwc_udelay(10);
  71208. +
  71209. + pcgcctl.d32 = 0;
  71210. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  71211. + pcgcctl.b.max_xcvrselect = 1;
  71212. + pcgcctl.b.ess_reg_restored = 1;
  71213. + pcgcctl.b.enbl_extnd_hiber = 1;
  71214. + pcgcctl.b.rstpdwnmodule = 1;
  71215. + pcgcctl.b.restoremode = 1;
  71216. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  71217. +
  71218. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  71219. +
  71220. + return 1;
  71221. +}
  71222. +
  71223. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71224. +/**
  71225. + * This function hadles LPM transaction received interrupt.
  71226. + */
  71227. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  71228. +{
  71229. + glpmcfg_data_t lpmcfg;
  71230. + gintsts_data_t gintsts;
  71231. +
  71232. + if (!core_if->core_params->lpm_enable) {
  71233. + DWC_PRINTF("Unexpected LPM interrupt\n");
  71234. + }
  71235. +
  71236. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71237. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  71238. +
  71239. + if (dwc_otg_is_host_mode(core_if)) {
  71240. + cil_hcd_sleep(core_if);
  71241. + } else {
  71242. + lpmcfg.b.hird_thres |= (1 << 4);
  71243. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  71244. + lpmcfg.d32);
  71245. + }
  71246. +
  71247. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  71248. + dwc_udelay(10);
  71249. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71250. + if (lpmcfg.b.prt_sleep_sts) {
  71251. + /* Save the current state */
  71252. + core_if->lx_state = DWC_OTG_L1;
  71253. + }
  71254. +
  71255. + /* Clear interrupt */
  71256. + gintsts.d32 = 0;
  71257. + gintsts.b.lpmtranrcvd = 1;
  71258. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  71259. + return 1;
  71260. +}
  71261. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  71262. +
  71263. +/**
  71264. + * This function returns the Core Interrupt register.
  71265. + */
  71266. +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)
  71267. +{
  71268. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  71269. + gintsts_data_t gintsts;
  71270. + gintmsk_data_t gintmsk;
  71271. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  71272. + gintmsk_common.b.wkupintr = 1;
  71273. + gintmsk_common.b.sessreqintr = 1;
  71274. + gintmsk_common.b.conidstschng = 1;
  71275. + gintmsk_common.b.otgintr = 1;
  71276. + gintmsk_common.b.modemismatch = 1;
  71277. + gintmsk_common.b.disconnect = 1;
  71278. + gintmsk_common.b.usbsuspend = 1;
  71279. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71280. + gintmsk_common.b.lpmtranrcvd = 1;
  71281. +#endif
  71282. + gintmsk_common.b.restoredone = 1;
  71283. + if(dwc_otg_is_device_mode(core_if))
  71284. + {
  71285. + /** @todo: The port interrupt occurs while in device
  71286. + * mode. Added code to CIL to clear the interrupt for now!
  71287. + */
  71288. + gintmsk_common.b.portintr = 1;
  71289. + }
  71290. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  71291. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  71292. + if(fiq_enable) {
  71293. + local_fiq_disable();
  71294. + /* Pull in the interrupts that the FIQ has masked */
  71295. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  71296. + gintmsk.d32 |= gintmsk_common.d32;
  71297. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  71298. + reenable_gintmsk->d32 = gintmsk.d32;
  71299. + local_fiq_enable();
  71300. + }
  71301. +
  71302. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  71303. +
  71304. +#ifdef DEBUG
  71305. + /* if any common interrupts set */
  71306. + if (gintsts.d32 & gintmsk_common.d32) {
  71307. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  71308. + gintsts.d32, gintmsk.d32);
  71309. + }
  71310. +#endif
  71311. + if (!fiq_enable){
  71312. + if (gahbcfg.b.glblintrmsk)
  71313. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  71314. + else
  71315. + return 0;
  71316. + } else {
  71317. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  71318. + * Can't trust the global interrupt mask bit in this case.
  71319. + */
  71320. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  71321. + }
  71322. +
  71323. +}
  71324. +
  71325. +/* MACRO for clearing interupt bits in GPWRDN register */
  71326. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  71327. +do { \
  71328. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  71329. + gpwrdn.b.__intr = 1; \
  71330. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  71331. + 0, gpwrdn.d32); \
  71332. +} while (0)
  71333. +
  71334. +/**
  71335. + * Common interrupt handler.
  71336. + *
  71337. + * The common interrupts are those that occur in both Host and Device mode.
  71338. + * This handler handles the following interrupts:
  71339. + * - Mode Mismatch Interrupt
  71340. + * - Disconnect Interrupt
  71341. + * - OTG Interrupt
  71342. + * - Connector ID Status Change Interrupt
  71343. + * - Session Request Interrupt.
  71344. + * - Resume / Remote Wakeup Detected Interrupt.
  71345. + * - LPM Transaction Received Interrupt
  71346. + * - ADP Transaction Received Interrupt
  71347. + *
  71348. + */
  71349. +int32_t dwc_otg_handle_common_intr(void *dev)
  71350. +{
  71351. + int retval = 0;
  71352. + gintsts_data_t gintsts;
  71353. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  71354. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71355. + dwc_otg_device_t *otg_dev = dev;
  71356. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  71357. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71358. + if (dwc_otg_is_device_mode(core_if))
  71359. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  71360. +
  71361. + if (core_if->lock)
  71362. + DWC_SPINLOCK(core_if->lock);
  71363. +
  71364. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  71365. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  71366. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  71367. + core_if->xhib = 2;
  71368. + if (core_if->lock)
  71369. + DWC_SPINUNLOCK(core_if->lock);
  71370. +
  71371. + return retval;
  71372. + }
  71373. +
  71374. + if (core_if->hibernation_suspend <= 0) {
  71375. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  71376. + * of this handler - god only knows why it's done like this
  71377. + */
  71378. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  71379. +
  71380. + if (gintsts.b.modemismatch) {
  71381. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  71382. + }
  71383. + if (gintsts.b.otgintr) {
  71384. + retval |= dwc_otg_handle_otg_intr(core_if);
  71385. + }
  71386. + if (gintsts.b.conidstschng) {
  71387. + retval |=
  71388. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  71389. + }
  71390. + if (gintsts.b.disconnect) {
  71391. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  71392. + }
  71393. + if (gintsts.b.sessreqintr) {
  71394. + retval |= dwc_otg_handle_session_req_intr(core_if);
  71395. + }
  71396. + if (gintsts.b.wkupintr) {
  71397. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  71398. + }
  71399. + if (gintsts.b.usbsuspend) {
  71400. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  71401. + }
  71402. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71403. + if (gintsts.b.lpmtranrcvd) {
  71404. + retval |= dwc_otg_handle_lpm_intr(core_if);
  71405. + }
  71406. +#endif
  71407. + if (gintsts.b.restoredone) {
  71408. + gintsts.d32 = 0;
  71409. + if (core_if->power_down == 2)
  71410. + core_if->hibernation_suspend = -1;
  71411. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  71412. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71413. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71414. + dctl_data_t dctl = {.d32 = 0 };
  71415. +
  71416. + DWC_WRITE_REG32(&core_if->core_global_regs->
  71417. + gintsts, 0xFFFFFFFF);
  71418. +
  71419. + DWC_DEBUGPL(DBG_ANY,
  71420. + "RESTORE DONE generated\n");
  71421. +
  71422. + gpwrdn.b.restore = 1;
  71423. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71424. + dwc_udelay(10);
  71425. +
  71426. + pcgcctl.b.rstpdwnmodule = 1;
  71427. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71428. +
  71429. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  71430. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  71431. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  71432. + dwc_udelay(50);
  71433. +
  71434. + dctl.b.pwronprgdone = 1;
  71435. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  71436. + dwc_udelay(10);
  71437. +
  71438. + dwc_otg_restore_global_regs(core_if);
  71439. + dwc_otg_restore_dev_regs(core_if, 0);
  71440. +
  71441. + dctl.d32 = 0;
  71442. + dctl.b.pwronprgdone = 1;
  71443. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  71444. + dwc_udelay(10);
  71445. +
  71446. + pcgcctl.d32 = 0;
  71447. + pcgcctl.b.enbl_extnd_hiber = 1;
  71448. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71449. +
  71450. + /* The core will be in ON STATE */
  71451. + core_if->lx_state = DWC_OTG_L0;
  71452. + core_if->xhib = 0;
  71453. +
  71454. + DWC_SPINUNLOCK(core_if->lock);
  71455. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  71456. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  71457. + }
  71458. + DWC_SPINLOCK(core_if->lock);
  71459. +
  71460. + }
  71461. +
  71462. + gintsts.b.restoredone = 1;
  71463. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  71464. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  71465. + retval |= 1;
  71466. + }
  71467. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  71468. + /* The port interrupt occurs while in device mode with HPRT0
  71469. + * Port Enable/Disable.
  71470. + */
  71471. + gintsts.d32 = 0;
  71472. + gintsts.b.portintr = 1;
  71473. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  71474. + retval |= 1;
  71475. + gintmsk_reenable.b.portintr = 1;
  71476. +
  71477. + }
  71478. + /* Did we actually handle anything? if so, unmask the interrupt */
  71479. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  71480. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  71481. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  71482. + if (retval && fiq_enable) {
  71483. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  71484. + }
  71485. +
  71486. + } else {
  71487. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  71488. +
  71489. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  71490. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  71491. + if (gpwrdn.b.linestate == 0) {
  71492. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  71493. + } else {
  71494. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  71495. + }
  71496. +
  71497. + retval |= 1;
  71498. + }
  71499. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  71500. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  71501. + /* remote wakeup from hibernation */
  71502. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  71503. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  71504. + } else {
  71505. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  71506. + }
  71507. + retval |= 1;
  71508. + }
  71509. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  71510. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  71511. + if (gpwrdn.b.linestate == 0) {
  71512. + DWC_PRINTF("Reset detected\n");
  71513. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  71514. + }
  71515. + }
  71516. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  71517. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  71518. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  71519. + retval |= 1;
  71520. + }
  71521. + }
  71522. + /* Handle ADP interrupt here */
  71523. + if (gpwrdn.b.adp_int) {
  71524. + DWC_PRINTF("ADP interrupt\n");
  71525. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  71526. + dwc_otg_adp_handle_intr(core_if);
  71527. + retval |= 1;
  71528. + }
  71529. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  71530. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  71531. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  71532. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  71533. +
  71534. + retval |= 1;
  71535. + }
  71536. + if (core_if->lock)
  71537. + DWC_SPINUNLOCK(core_if->lock);
  71538. + return retval;
  71539. +}
  71540. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_core_if.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  71541. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 1969-12-31 18:00:00.000000000 -0600
  71542. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 2014-12-11 14:02:55.396418001 -0600
  71543. @@ -0,0 +1,705 @@
  71544. +/* ==========================================================================
  71545. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  71546. + * $Revision: #13 $
  71547. + * $Date: 2012/08/10 $
  71548. + * $Change: 2047372 $
  71549. + *
  71550. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  71551. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  71552. + * otherwise expressly agreed to in writing between Synopsys and you.
  71553. + *
  71554. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  71555. + * any End User Software License Agreement or Agreement for Licensed Product
  71556. + * with Synopsys or any supplement thereto. You are permitted to use and
  71557. + * redistribute this Software in source and binary forms, with or without
  71558. + * modification, provided that redistributions of source code must retain this
  71559. + * notice. You may not view, use, disclose, copy or distribute this file or
  71560. + * any information contained herein except pursuant to this license grant from
  71561. + * Synopsys. If you do not agree with this notice, including the disclaimer
  71562. + * below, then you are not authorized to use the Software.
  71563. + *
  71564. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  71565. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  71566. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  71567. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  71568. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  71569. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  71570. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  71571. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  71572. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  71573. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  71574. + * DAMAGE.
  71575. + * ========================================================================== */
  71576. +#if !defined(__DWC_CORE_IF_H__)
  71577. +#define __DWC_CORE_IF_H__
  71578. +
  71579. +#include "dwc_os.h"
  71580. +
  71581. +/** @file
  71582. + * This file defines DWC_OTG Core API
  71583. + */
  71584. +
  71585. +struct dwc_otg_core_if;
  71586. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  71587. +
  71588. +/** Maximum number of Periodic FIFOs */
  71589. +#define MAX_PERIO_FIFOS 15
  71590. +/** Maximum number of Periodic FIFOs */
  71591. +#define MAX_TX_FIFOS 15
  71592. +
  71593. +/** Maximum number of Endpoints/HostChannels */
  71594. +#define MAX_EPS_CHANNELS 16
  71595. +
  71596. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  71597. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  71598. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  71599. +
  71600. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  71601. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  71602. +
  71603. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  71604. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  71605. +
  71606. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  71607. +
  71608. +/** This function should be called on every hardware interrupt. */
  71609. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  71610. +
  71611. +/** @name OTG Core Parameters */
  71612. +/** @{ */
  71613. +
  71614. +/**
  71615. + * Specifies the OTG capabilities. The driver will automatically
  71616. + * detect the value for this parameter if none is specified.
  71617. + * 0 - HNP and SRP capable (default)
  71618. + * 1 - SRP Only capable
  71619. + * 2 - No HNP/SRP capable
  71620. + */
  71621. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  71622. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  71623. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  71624. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  71625. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  71626. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  71627. +
  71628. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  71629. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  71630. +#define dwc_param_opt_default 1
  71631. +
  71632. +/**
  71633. + * Specifies whether to use slave or DMA mode for accessing the data
  71634. + * FIFOs. The driver will automatically detect the value for this
  71635. + * parameter if none is specified.
  71636. + * 0 - Slave
  71637. + * 1 - DMA (default, if available)
  71638. + */
  71639. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  71640. + int32_t val);
  71641. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  71642. +#define dwc_param_dma_enable_default 1
  71643. +
  71644. +/**
  71645. + * When DMA mode is enabled specifies whether to use
  71646. + * address DMA or DMA Descritor mode for accessing the data
  71647. + * FIFOs in device mode. The driver will automatically detect
  71648. + * the value for this parameter if none is specified.
  71649. + * 0 - address DMA
  71650. + * 1 - DMA Descriptor(default, if available)
  71651. + */
  71652. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  71653. + int32_t val);
  71654. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  71655. +//#define dwc_param_dma_desc_enable_default 1
  71656. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  71657. +
  71658. +/** The DMA Burst size (applicable only for External DMA
  71659. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  71660. + */
  71661. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  71662. + int32_t val);
  71663. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  71664. +#define dwc_param_dma_burst_size_default 32
  71665. +
  71666. +/**
  71667. + * Specifies the maximum speed of operation in host and device mode.
  71668. + * The actual speed depends on the speed of the attached device and
  71669. + * the value of phy_type. The actual speed depends on the speed of the
  71670. + * attached device.
  71671. + * 0 - High Speed (default)
  71672. + * 1 - Full Speed
  71673. + */
  71674. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  71675. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  71676. +#define dwc_param_speed_default 0
  71677. +#define DWC_SPEED_PARAM_HIGH 0
  71678. +#define DWC_SPEED_PARAM_FULL 1
  71679. +
  71680. +/** Specifies whether low power mode is supported when attached
  71681. + * to a Full Speed or Low Speed device in host mode.
  71682. + * 0 - Don't support low power mode (default)
  71683. + * 1 - Support low power mode
  71684. + */
  71685. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  71686. + core_if, int32_t val);
  71687. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  71688. + * core_if);
  71689. +#define dwc_param_host_support_fs_ls_low_power_default 0
  71690. +
  71691. +/** Specifies the PHY clock rate in low power mode when connected to a
  71692. + * Low Speed device in host mode. This parameter is applicable only if
  71693. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  71694. + * then defaults to 6 MHZ otherwise 48 MHZ.
  71695. + *
  71696. + * 0 - 48 MHz
  71697. + * 1 - 6 MHz
  71698. + */
  71699. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  71700. + core_if, int32_t val);
  71701. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  71702. + core_if);
  71703. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  71704. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  71705. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  71706. +
  71707. +/**
  71708. + * 0 - Use cC FIFO size parameters
  71709. + * 1 - Allow dynamic FIFO sizing (default)
  71710. + */
  71711. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  71712. + int32_t val);
  71713. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  71714. + core_if);
  71715. +#define dwc_param_enable_dynamic_fifo_default 1
  71716. +
  71717. +/** Total number of 4-byte words in the data FIFO memory. This
  71718. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  71719. + * Tx FIFOs.
  71720. + * 32 to 32768 (default 8192)
  71721. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  71722. + */
  71723. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  71724. + int32_t val);
  71725. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  71726. +//#define dwc_param_data_fifo_size_default 8192
  71727. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  71728. +
  71729. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  71730. + * FIFO sizing is enabled.
  71731. + * 16 to 32768 (default 1064)
  71732. + */
  71733. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  71734. + int32_t val);
  71735. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  71736. +#define dwc_param_dev_rx_fifo_size_default 1064
  71737. +
  71738. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  71739. + * when dynamic FIFO sizing is enabled.
  71740. + * 16 to 32768 (default 1024)
  71741. + */
  71742. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  71743. + core_if, int32_t val);
  71744. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  71745. + core_if);
  71746. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  71747. +
  71748. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  71749. + * mode when dynamic FIFO sizing is enabled.
  71750. + * 4 to 768 (default 256)
  71751. + */
  71752. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  71753. + int32_t val, int fifo_num);
  71754. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  71755. + core_if, int fifo_num);
  71756. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  71757. +
  71758. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  71759. + * FIFO sizing is enabled.
  71760. + * 16 to 32768 (default 1024)
  71761. + */
  71762. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  71763. + int32_t val);
  71764. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  71765. +//#define dwc_param_host_rx_fifo_size_default 1024
  71766. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  71767. +
  71768. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  71769. + * when Dynamic FIFO sizing is enabled in the core.
  71770. + * 16 to 32768 (default 1024)
  71771. + */
  71772. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  71773. + core_if, int32_t val);
  71774. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  71775. + core_if);
  71776. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  71777. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  71778. +
  71779. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  71780. + * FIFO sizing is enabled.
  71781. + * 16 to 32768 (default 1024)
  71782. + */
  71783. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  71784. + core_if, int32_t val);
  71785. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  71786. + core_if);
  71787. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  71788. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  71789. +
  71790. +/** The maximum transfer size supported in bytes.
  71791. + * 2047 to 65,535 (default 65,535)
  71792. + */
  71793. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  71794. + int32_t val);
  71795. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  71796. +#define dwc_param_max_transfer_size_default 65535
  71797. +
  71798. +/** The maximum number of packets in a transfer.
  71799. + * 15 to 511 (default 511)
  71800. + */
  71801. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  71802. + int32_t val);
  71803. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  71804. +#define dwc_param_max_packet_count_default 511
  71805. +
  71806. +/** The number of host channel registers to use.
  71807. + * 1 to 16 (default 12)
  71808. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  71809. + */
  71810. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  71811. + int32_t val);
  71812. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  71813. +//#define dwc_param_host_channels_default 12
  71814. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  71815. +
  71816. +/** The number of endpoints in addition to EP0 available for device
  71817. + * mode operations.
  71818. + * 1 to 15 (default 6 IN and OUT)
  71819. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  71820. + * endpoints in addition to EP0.
  71821. + */
  71822. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  71823. + int32_t val);
  71824. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  71825. +#define dwc_param_dev_endpoints_default 6
  71826. +
  71827. +/**
  71828. + * Specifies the type of PHY interface to use. By default, the driver
  71829. + * will automatically detect the phy_type.
  71830. + *
  71831. + * 0 - Full Speed PHY
  71832. + * 1 - UTMI+ (default)
  71833. + * 2 - ULPI
  71834. + */
  71835. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  71836. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  71837. +#define DWC_PHY_TYPE_PARAM_FS 0
  71838. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  71839. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  71840. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  71841. +
  71842. +/**
  71843. + * Specifies the UTMI+ Data Width. This parameter is
  71844. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  71845. + * PHY_TYPE, this parameter indicates the data width between
  71846. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  71847. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  71848. + * to "8 and 16 bits", meaning that the core has been
  71849. + * configured to work at either data path width.
  71850. + *
  71851. + * 8 or 16 bits (default 16)
  71852. + */
  71853. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  71854. + int32_t val);
  71855. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  71856. +//#define dwc_param_phy_utmi_width_default 16
  71857. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  71858. +
  71859. +/**
  71860. + * Specifies whether the ULPI operates at double or single
  71861. + * data rate. This parameter is only applicable if PHY_TYPE is
  71862. + * ULPI.
  71863. + *
  71864. + * 0 - single data rate ULPI interface with 8 bit wide data
  71865. + * bus (default)
  71866. + * 1 - double data rate ULPI interface with 4 bit wide data
  71867. + * bus
  71868. + */
  71869. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  71870. + int32_t val);
  71871. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  71872. +#define dwc_param_phy_ulpi_ddr_default 0
  71873. +
  71874. +/**
  71875. + * Specifies whether to use the internal or external supply to
  71876. + * drive the vbus with a ULPI phy.
  71877. + */
  71878. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  71879. + int32_t val);
  71880. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  71881. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  71882. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  71883. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  71884. +
  71885. +/**
  71886. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  71887. + * parameter is only applicable if PHY_TYPE is FS.
  71888. + * 0 - No (default)
  71889. + * 1 - Yes
  71890. + */
  71891. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  71892. + int32_t val);
  71893. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  71894. +#define dwc_param_i2c_enable_default 0
  71895. +
  71896. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  71897. + int32_t val);
  71898. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  71899. +#define dwc_param_ulpi_fs_ls_default 0
  71900. +
  71901. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  71902. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  71903. +#define dwc_param_ts_dline_default 0
  71904. +
  71905. +/**
  71906. + * Specifies whether dedicated transmit FIFOs are
  71907. + * enabled for non periodic IN endpoints in device mode
  71908. + * 0 - No
  71909. + * 1 - Yes
  71910. + */
  71911. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  71912. + int32_t val);
  71913. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  71914. + core_if);
  71915. +#define dwc_param_en_multiple_tx_fifo_default 1
  71916. +
  71917. +/** Number of 4-byte words in each of the Tx FIFOs in device
  71918. + * mode when dynamic FIFO sizing is enabled.
  71919. + * 4 to 768 (default 256)
  71920. + */
  71921. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  71922. + int fifo_num, int32_t val);
  71923. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  71924. + int fifo_num);
  71925. +#define dwc_param_dev_tx_fifo_size_default 768
  71926. +
  71927. +/** Thresholding enable flag-
  71928. + * bit 0 - enable non-ISO Tx thresholding
  71929. + * bit 1 - enable ISO Tx thresholding
  71930. + * bit 2 - enable Rx thresholding
  71931. + */
  71932. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  71933. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  71934. +#define dwc_param_thr_ctl_default 0
  71935. +
  71936. +/** Thresholding length for Tx
  71937. + * FIFOs in 32 bit DWORDs
  71938. + */
  71939. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  71940. + int32_t val);
  71941. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  71942. +#define dwc_param_tx_thr_length_default 64
  71943. +
  71944. +/** Thresholding length for Rx
  71945. + * FIFOs in 32 bit DWORDs
  71946. + */
  71947. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  71948. + int32_t val);
  71949. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  71950. +#define dwc_param_rx_thr_length_default 64
  71951. +
  71952. +/**
  71953. + * Specifies whether LPM (Link Power Management) support is enabled
  71954. + */
  71955. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  71956. + int32_t val);
  71957. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  71958. +#define dwc_param_lpm_enable_default 1
  71959. +
  71960. +/**
  71961. + * Specifies whether PTI enhancement is enabled
  71962. + */
  71963. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  71964. + int32_t val);
  71965. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  71966. +#define dwc_param_pti_enable_default 0
  71967. +
  71968. +/**
  71969. + * Specifies whether MPI enhancement is enabled
  71970. + */
  71971. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  71972. + int32_t val);
  71973. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  71974. +#define dwc_param_mpi_enable_default 0
  71975. +
  71976. +/**
  71977. + * Specifies whether ADP capability is enabled
  71978. + */
  71979. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  71980. + int32_t val);
  71981. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  71982. +#define dwc_param_adp_enable_default 0
  71983. +
  71984. +/**
  71985. + * Specifies whether IC_USB capability is enabled
  71986. + */
  71987. +
  71988. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  71989. + int32_t val);
  71990. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  71991. +#define dwc_param_ic_usb_cap_default 0
  71992. +
  71993. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  71994. + int32_t val);
  71995. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  71996. +#define dwc_param_ahb_thr_ratio_default 0
  71997. +
  71998. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  71999. + int32_t val);
  72000. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  72001. +#define dwc_param_power_down_default 0
  72002. +
  72003. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  72004. + int32_t val);
  72005. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  72006. +#define dwc_param_reload_ctl_default 0
  72007. +
  72008. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  72009. + int32_t val);
  72010. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  72011. +#define dwc_param_dev_out_nak_default 0
  72012. +
  72013. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  72014. + int32_t val);
  72015. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  72016. +#define dwc_param_cont_on_bna_default 0
  72017. +
  72018. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  72019. + int32_t val);
  72020. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  72021. +#define dwc_param_ahb_single_default 0
  72022. +
  72023. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  72024. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  72025. +#define dwc_param_otg_ver_default 0
  72026. +
  72027. +/** @} */
  72028. +
  72029. +/** @name Access to registers and bit-fields */
  72030. +
  72031. +/**
  72032. + * Dump core registers and SPRAM
  72033. + */
  72034. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  72035. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  72036. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  72037. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  72038. +
  72039. +/**
  72040. + * Get host negotiation status.
  72041. + */
  72042. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  72043. +
  72044. +/**
  72045. + * Get srp status
  72046. + */
  72047. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  72048. +
  72049. +/**
  72050. + * Set hnpreq bit in the GOTGCTL register.
  72051. + */
  72052. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  72053. +
  72054. +/**
  72055. + * Get Content of SNPSID register.
  72056. + */
  72057. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  72058. +
  72059. +/**
  72060. + * Get current mode.
  72061. + * Returns 0 if in device mode, and 1 if in host mode.
  72062. + */
  72063. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  72064. +
  72065. +/**
  72066. + * Get value of hnpcapable field in the GUSBCFG register
  72067. + */
  72068. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  72069. +/**
  72070. + * Set value of hnpcapable field in the GUSBCFG register
  72071. + */
  72072. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  72073. +
  72074. +/**
  72075. + * Get value of srpcapable field in the GUSBCFG register
  72076. + */
  72077. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  72078. +/**
  72079. + * Set value of srpcapable field in the GUSBCFG register
  72080. + */
  72081. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  72082. +
  72083. +/**
  72084. + * Get value of devspeed field in the DCFG register
  72085. + */
  72086. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  72087. +/**
  72088. + * Set value of devspeed field in the DCFG register
  72089. + */
  72090. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  72091. +
  72092. +/**
  72093. + * Get the value of busconnected field from the HPRT0 register
  72094. + */
  72095. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  72096. +
  72097. +/**
  72098. + * Gets the device enumeration Speed.
  72099. + */
  72100. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  72101. +
  72102. +/**
  72103. + * Get value of prtpwr field from the HPRT0 register
  72104. + */
  72105. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  72106. +
  72107. +/**
  72108. + * Get value of flag indicating core state - hibernated or not
  72109. + */
  72110. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  72111. +
  72112. +/**
  72113. + * Set value of prtpwr field from the HPRT0 register
  72114. + */
  72115. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  72116. +
  72117. +/**
  72118. + * Get value of prtsusp field from the HPRT0 regsiter
  72119. + */
  72120. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  72121. +/**
  72122. + * Set value of prtpwr field from the HPRT0 register
  72123. + */
  72124. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  72125. +
  72126. +/**
  72127. + * Get value of ModeChTimEn field from the HCFG regsiter
  72128. + */
  72129. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  72130. +/**
  72131. + * Set value of ModeChTimEn field from the HCFG regsiter
  72132. + */
  72133. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  72134. +
  72135. +/**
  72136. + * Get value of Fram Interval field from the HFIR regsiter
  72137. + */
  72138. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  72139. +/**
  72140. + * Set value of Frame Interval field from the HFIR regsiter
  72141. + */
  72142. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  72143. +
  72144. +/**
  72145. + * Set value of prtres field from the HPRT0 register
  72146. + *FIXME Remove?
  72147. + */
  72148. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  72149. +
  72150. +/**
  72151. + * Get value of rmtwkupsig bit in DCTL register
  72152. + */
  72153. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  72154. +
  72155. +/**
  72156. + * Get value of prt_sleep_sts field from the GLPMCFG register
  72157. + */
  72158. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  72159. +
  72160. +/**
  72161. + * Get value of rem_wkup_en field from the GLPMCFG register
  72162. + */
  72163. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  72164. +
  72165. +/**
  72166. + * Get value of appl_resp field from the GLPMCFG register
  72167. + */
  72168. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  72169. +/**
  72170. + * Set value of appl_resp field from the GLPMCFG register
  72171. + */
  72172. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  72173. +
  72174. +/**
  72175. + * Get value of hsic_connect field from the GLPMCFG register
  72176. + */
  72177. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  72178. +/**
  72179. + * Set value of hsic_connect field from the GLPMCFG register
  72180. + */
  72181. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  72182. +
  72183. +/**
  72184. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  72185. + */
  72186. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  72187. +/**
  72188. + * Set value of inv_sel_hsic field from the GLPMFG register.
  72189. + */
  72190. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  72191. +
  72192. +/*
  72193. + * Some functions for accessing registers
  72194. + */
  72195. +
  72196. +/**
  72197. + * GOTGCTL register
  72198. + */
  72199. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  72200. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  72201. +
  72202. +/**
  72203. + * GUSBCFG register
  72204. + */
  72205. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  72206. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  72207. +
  72208. +/**
  72209. + * GRXFSIZ register
  72210. + */
  72211. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  72212. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  72213. +
  72214. +/**
  72215. + * GNPTXFSIZ register
  72216. + */
  72217. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  72218. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  72219. +
  72220. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  72221. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  72222. +
  72223. +/**
  72224. + * GGPIO register
  72225. + */
  72226. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  72227. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  72228. +
  72229. +/**
  72230. + * GUID register
  72231. + */
  72232. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  72233. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  72234. +
  72235. +/**
  72236. + * HPRT0 register
  72237. + */
  72238. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  72239. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  72240. +
  72241. +/**
  72242. + * GHPTXFSIZE
  72243. + */
  72244. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  72245. +
  72246. +/** @} */
  72247. +
  72248. +#endif /* __DWC_CORE_IF_H__ */
  72249. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_dbg.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  72250. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 1969-12-31 18:00:00.000000000 -0600
  72251. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 2014-12-11 14:02:55.396418001 -0600
  72252. @@ -0,0 +1,117 @@
  72253. +/* ==========================================================================
  72254. + *
  72255. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72256. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72257. + * otherwise expressly agreed to in writing between Synopsys and you.
  72258. + *
  72259. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72260. + * any End User Software License Agreement or Agreement for Licensed Product
  72261. + * with Synopsys or any supplement thereto. You are permitted to use and
  72262. + * redistribute this Software in source and binary forms, with or without
  72263. + * modification, provided that redistributions of source code must retain this
  72264. + * notice. You may not view, use, disclose, copy or distribute this file or
  72265. + * any information contained herein except pursuant to this license grant from
  72266. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72267. + * below, then you are not authorized to use the Software.
  72268. + *
  72269. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72270. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72271. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72272. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72273. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72274. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72275. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72276. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72277. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72278. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72279. + * DAMAGE.
  72280. + * ========================================================================== */
  72281. +
  72282. +#ifndef __DWC_OTG_DBG_H__
  72283. +#define __DWC_OTG_DBG_H__
  72284. +
  72285. +/** @file
  72286. + * This file defines debug levels.
  72287. + * Debugging support vanishes in non-debug builds.
  72288. + */
  72289. +
  72290. +/**
  72291. + * The Debug Level bit-mask variable.
  72292. + */
  72293. +extern uint32_t g_dbg_lvl;
  72294. +/**
  72295. + * Set the Debug Level variable.
  72296. + */
  72297. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  72298. +{
  72299. + uint32_t old = g_dbg_lvl;
  72300. + g_dbg_lvl = new;
  72301. + return old;
  72302. +}
  72303. +
  72304. +#define DBG_USER (0x1)
  72305. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  72306. +#define DBG_CIL (0x2)
  72307. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  72308. + * messages */
  72309. +#define DBG_CILV (0x20)
  72310. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  72311. + * messages */
  72312. +#define DBG_PCD (0x4)
  72313. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  72314. + * messages */
  72315. +#define DBG_PCDV (0x40)
  72316. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  72317. +#define DBG_HCD (0x8)
  72318. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  72319. + * messages */
  72320. +#define DBG_HCDV (0x80)
  72321. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  72322. + * mode. */
  72323. +#define DBG_HCD_URB (0x800)
  72324. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  72325. + * messages. */
  72326. +#define DBG_HCDI (0x1000)
  72327. +
  72328. +/** When debug level has any bit set, display debug messages */
  72329. +#define DBG_ANY (0xFF)
  72330. +
  72331. +/** All debug messages off */
  72332. +#define DBG_OFF 0
  72333. +
  72334. +/** Prefix string for DWC_DEBUG print macros. */
  72335. +#define USB_DWC "DWC_otg: "
  72336. +
  72337. +/**
  72338. + * Print a debug message when the Global debug level variable contains
  72339. + * the bit defined in <code>lvl</code>.
  72340. + *
  72341. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  72342. + * @param[in] x - like printf
  72343. + *
  72344. + * Example:<p>
  72345. + * <code>
  72346. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  72347. + * </code>
  72348. + * <br>
  72349. + * results in:<br>
  72350. + * <code>
  72351. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  72352. + * </code>
  72353. + */
  72354. +#ifdef DEBUG
  72355. +
  72356. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  72357. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  72358. +
  72359. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  72360. +
  72361. +#else
  72362. +
  72363. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  72364. +# define DWC_DEBUGP(x...)
  72365. +
  72366. +# define CHK_DEBUG_LEVEL(level) (0)
  72367. +
  72368. +#endif /*DEBUG*/
  72369. +#endif
  72370. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_driver.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  72371. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_driver.c 1969-12-31 18:00:00.000000000 -0600
  72372. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c 2014-12-11 14:02:55.396418001 -0600
  72373. @@ -0,0 +1,1749 @@
  72374. +/* ==========================================================================
  72375. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  72376. + * $Revision: #92 $
  72377. + * $Date: 2012/08/10 $
  72378. + * $Change: 2047372 $
  72379. + *
  72380. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72381. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72382. + * otherwise expressly agreed to in writing between Synopsys and you.
  72383. + *
  72384. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72385. + * any End User Software License Agreement or Agreement for Licensed Product
  72386. + * with Synopsys or any supplement thereto. You are permitted to use and
  72387. + * redistribute this Software in source and binary forms, with or without
  72388. + * modification, provided that redistributions of source code must retain this
  72389. + * notice. You may not view, use, disclose, copy or distribute this file or
  72390. + * any information contained herein except pursuant to this license grant from
  72391. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72392. + * below, then you are not authorized to use the Software.
  72393. + *
  72394. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72395. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72396. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72397. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72398. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72399. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72400. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72401. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72402. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72403. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72404. + * DAMAGE.
  72405. + * ========================================================================== */
  72406. +
  72407. +/** @file
  72408. + * The dwc_otg_driver module provides the initialization and cleanup entry
  72409. + * points for the DWC_otg driver. This module will be dynamically installed
  72410. + * after Linux is booted using the insmod command. When the module is
  72411. + * installed, the dwc_otg_driver_init function is called. When the module is
  72412. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  72413. + *
  72414. + * This module also defines a data structure for the dwc_otg_driver, which is
  72415. + * used in conjunction with the standard ARM lm_device structure. These
  72416. + * structures allow the OTG driver to comply with the standard Linux driver
  72417. + * model in which devices and drivers are registered with a bus driver. This
  72418. + * has the benefit that Linux can expose attributes of the driver and device
  72419. + * in its special sysfs file system. Users can then read or write files in
  72420. + * this file system to perform diagnostics on the driver components or the
  72421. + * device.
  72422. + */
  72423. +
  72424. +#include "dwc_otg_os_dep.h"
  72425. +#include "dwc_os.h"
  72426. +#include "dwc_otg_dbg.h"
  72427. +#include "dwc_otg_driver.h"
  72428. +#include "dwc_otg_attr.h"
  72429. +#include "dwc_otg_core_if.h"
  72430. +#include "dwc_otg_pcd_if.h"
  72431. +#include "dwc_otg_hcd_if.h"
  72432. +#include "dwc_otg_fiq_fsm.h"
  72433. +
  72434. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  72435. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  72436. +
  72437. +bool microframe_schedule=true;
  72438. +
  72439. +static const char dwc_driver_name[] = "dwc_otg";
  72440. +
  72441. +
  72442. +extern int pcd_init(
  72443. +#ifdef LM_INTERFACE
  72444. + struct lm_device *_dev
  72445. +#elif defined(PCI_INTERFACE)
  72446. + struct pci_dev *_dev
  72447. +#elif defined(PLATFORM_INTERFACE)
  72448. + struct platform_device *dev
  72449. +#endif
  72450. + );
  72451. +extern int hcd_init(
  72452. +#ifdef LM_INTERFACE
  72453. + struct lm_device *_dev
  72454. +#elif defined(PCI_INTERFACE)
  72455. + struct pci_dev *_dev
  72456. +#elif defined(PLATFORM_INTERFACE)
  72457. + struct platform_device *dev
  72458. +#endif
  72459. + );
  72460. +
  72461. +extern int pcd_remove(
  72462. +#ifdef LM_INTERFACE
  72463. + struct lm_device *_dev
  72464. +#elif defined(PCI_INTERFACE)
  72465. + struct pci_dev *_dev
  72466. +#elif defined(PLATFORM_INTERFACE)
  72467. + struct platform_device *_dev
  72468. +#endif
  72469. + );
  72470. +
  72471. +extern void hcd_remove(
  72472. +#ifdef LM_INTERFACE
  72473. + struct lm_device *_dev
  72474. +#elif defined(PCI_INTERFACE)
  72475. + struct pci_dev *_dev
  72476. +#elif defined(PLATFORM_INTERFACE)
  72477. + struct platform_device *_dev
  72478. +#endif
  72479. + );
  72480. +
  72481. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  72482. +
  72483. +/*-------------------------------------------------------------------------*/
  72484. +/* Encapsulate the module parameter settings */
  72485. +
  72486. +struct dwc_otg_driver_module_params {
  72487. + int32_t opt;
  72488. + int32_t otg_cap;
  72489. + int32_t dma_enable;
  72490. + int32_t dma_desc_enable;
  72491. + int32_t dma_burst_size;
  72492. + int32_t speed;
  72493. + int32_t host_support_fs_ls_low_power;
  72494. + int32_t host_ls_low_power_phy_clk;
  72495. + int32_t enable_dynamic_fifo;
  72496. + int32_t data_fifo_size;
  72497. + int32_t dev_rx_fifo_size;
  72498. + int32_t dev_nperio_tx_fifo_size;
  72499. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  72500. + int32_t host_rx_fifo_size;
  72501. + int32_t host_nperio_tx_fifo_size;
  72502. + int32_t host_perio_tx_fifo_size;
  72503. + int32_t max_transfer_size;
  72504. + int32_t max_packet_count;
  72505. + int32_t host_channels;
  72506. + int32_t dev_endpoints;
  72507. + int32_t phy_type;
  72508. + int32_t phy_utmi_width;
  72509. + int32_t phy_ulpi_ddr;
  72510. + int32_t phy_ulpi_ext_vbus;
  72511. + int32_t i2c_enable;
  72512. + int32_t ulpi_fs_ls;
  72513. + int32_t ts_dline;
  72514. + int32_t en_multiple_tx_fifo;
  72515. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  72516. + uint32_t thr_ctl;
  72517. + uint32_t tx_thr_length;
  72518. + uint32_t rx_thr_length;
  72519. + int32_t pti_enable;
  72520. + int32_t mpi_enable;
  72521. + int32_t lpm_enable;
  72522. + int32_t ic_usb_cap;
  72523. + int32_t ahb_thr_ratio;
  72524. + int32_t power_down;
  72525. + int32_t reload_ctl;
  72526. + int32_t dev_out_nak;
  72527. + int32_t cont_on_bna;
  72528. + int32_t ahb_single;
  72529. + int32_t otg_ver;
  72530. + int32_t adp_enable;
  72531. +};
  72532. +
  72533. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  72534. + .opt = -1,
  72535. + .otg_cap = -1,
  72536. + .dma_enable = -1,
  72537. + .dma_desc_enable = -1,
  72538. + .dma_burst_size = -1,
  72539. + .speed = -1,
  72540. + .host_support_fs_ls_low_power = -1,
  72541. + .host_ls_low_power_phy_clk = -1,
  72542. + .enable_dynamic_fifo = -1,
  72543. + .data_fifo_size = -1,
  72544. + .dev_rx_fifo_size = -1,
  72545. + .dev_nperio_tx_fifo_size = -1,
  72546. + .dev_perio_tx_fifo_size = {
  72547. + /* dev_perio_tx_fifo_size_1 */
  72548. + -1,
  72549. + -1,
  72550. + -1,
  72551. + -1,
  72552. + -1,
  72553. + -1,
  72554. + -1,
  72555. + -1,
  72556. + -1,
  72557. + -1,
  72558. + -1,
  72559. + -1,
  72560. + -1,
  72561. + -1,
  72562. + -1
  72563. + /* 15 */
  72564. + },
  72565. + .host_rx_fifo_size = -1,
  72566. + .host_nperio_tx_fifo_size = -1,
  72567. + .host_perio_tx_fifo_size = -1,
  72568. + .max_transfer_size = -1,
  72569. + .max_packet_count = -1,
  72570. + .host_channels = -1,
  72571. + .dev_endpoints = -1,
  72572. + .phy_type = -1,
  72573. + .phy_utmi_width = -1,
  72574. + .phy_ulpi_ddr = -1,
  72575. + .phy_ulpi_ext_vbus = -1,
  72576. + .i2c_enable = -1,
  72577. + .ulpi_fs_ls = -1,
  72578. + .ts_dline = -1,
  72579. + .en_multiple_tx_fifo = -1,
  72580. + .dev_tx_fifo_size = {
  72581. + /* dev_tx_fifo_size */
  72582. + -1,
  72583. + -1,
  72584. + -1,
  72585. + -1,
  72586. + -1,
  72587. + -1,
  72588. + -1,
  72589. + -1,
  72590. + -1,
  72591. + -1,
  72592. + -1,
  72593. + -1,
  72594. + -1,
  72595. + -1,
  72596. + -1
  72597. + /* 15 */
  72598. + },
  72599. + .thr_ctl = -1,
  72600. + .tx_thr_length = -1,
  72601. + .rx_thr_length = -1,
  72602. + .pti_enable = -1,
  72603. + .mpi_enable = -1,
  72604. + .lpm_enable = 0,
  72605. + .ic_usb_cap = -1,
  72606. + .ahb_thr_ratio = -1,
  72607. + .power_down = -1,
  72608. + .reload_ctl = -1,
  72609. + .dev_out_nak = -1,
  72610. + .cont_on_bna = -1,
  72611. + .ahb_single = -1,
  72612. + .otg_ver = -1,
  72613. + .adp_enable = -1,
  72614. +};
  72615. +
  72616. +//Global variable to switch the fiq fix on or off
  72617. +bool fiq_enable = 1;
  72618. +// Global variable to enable the split transaction fix
  72619. +bool fiq_fsm_enable = true;
  72620. +//Bulk split-transaction NAK holdoff in microframes
  72621. +uint16_t nak_holdoff = 8;
  72622. +
  72623. +unsigned short fiq_fsm_mask = 0x07;
  72624. +
  72625. +/**
  72626. + * This function shows the Driver Version.
  72627. + */
  72628. +static ssize_t version_show(struct device_driver *dev, char *buf)
  72629. +{
  72630. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  72631. + DWC_DRIVER_VERSION);
  72632. +}
  72633. +
  72634. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  72635. +
  72636. +/**
  72637. + * Global Debug Level Mask.
  72638. + */
  72639. +uint32_t g_dbg_lvl = 0; /* OFF */
  72640. +
  72641. +/**
  72642. + * This function shows the driver Debug Level.
  72643. + */
  72644. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  72645. +{
  72646. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  72647. +}
  72648. +
  72649. +/**
  72650. + * This function stores the driver Debug Level.
  72651. + */
  72652. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  72653. + size_t count)
  72654. +{
  72655. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  72656. + return count;
  72657. +}
  72658. +
  72659. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  72660. + dbg_level_store);
  72661. +
  72662. +/**
  72663. + * This function is called during module intialization
  72664. + * to pass module parameters to the DWC_OTG CORE.
  72665. + */
  72666. +static int set_parameters(dwc_otg_core_if_t * core_if)
  72667. +{
  72668. + int retval = 0;
  72669. + int i;
  72670. +
  72671. + if (dwc_otg_module_params.otg_cap != -1) {
  72672. + retval +=
  72673. + dwc_otg_set_param_otg_cap(core_if,
  72674. + dwc_otg_module_params.otg_cap);
  72675. + }
  72676. + if (dwc_otg_module_params.dma_enable != -1) {
  72677. + retval +=
  72678. + dwc_otg_set_param_dma_enable(core_if,
  72679. + dwc_otg_module_params.
  72680. + dma_enable);
  72681. + }
  72682. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  72683. + retval +=
  72684. + dwc_otg_set_param_dma_desc_enable(core_if,
  72685. + dwc_otg_module_params.
  72686. + dma_desc_enable);
  72687. + }
  72688. + if (dwc_otg_module_params.opt != -1) {
  72689. + retval +=
  72690. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  72691. + }
  72692. + if (dwc_otg_module_params.dma_burst_size != -1) {
  72693. + retval +=
  72694. + dwc_otg_set_param_dma_burst_size(core_if,
  72695. + dwc_otg_module_params.
  72696. + dma_burst_size);
  72697. + }
  72698. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  72699. + retval +=
  72700. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  72701. + dwc_otg_module_params.
  72702. + host_support_fs_ls_low_power);
  72703. + }
  72704. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  72705. + retval +=
  72706. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  72707. + dwc_otg_module_params.
  72708. + enable_dynamic_fifo);
  72709. + }
  72710. + if (dwc_otg_module_params.data_fifo_size != -1) {
  72711. + retval +=
  72712. + dwc_otg_set_param_data_fifo_size(core_if,
  72713. + dwc_otg_module_params.
  72714. + data_fifo_size);
  72715. + }
  72716. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  72717. + retval +=
  72718. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  72719. + dwc_otg_module_params.
  72720. + dev_rx_fifo_size);
  72721. + }
  72722. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  72723. + retval +=
  72724. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  72725. + dwc_otg_module_params.
  72726. + dev_nperio_tx_fifo_size);
  72727. + }
  72728. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  72729. + retval +=
  72730. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  72731. + dwc_otg_module_params.host_rx_fifo_size);
  72732. + }
  72733. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  72734. + retval +=
  72735. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  72736. + dwc_otg_module_params.
  72737. + host_nperio_tx_fifo_size);
  72738. + }
  72739. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  72740. + retval +=
  72741. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  72742. + dwc_otg_module_params.
  72743. + host_perio_tx_fifo_size);
  72744. + }
  72745. + if (dwc_otg_module_params.max_transfer_size != -1) {
  72746. + retval +=
  72747. + dwc_otg_set_param_max_transfer_size(core_if,
  72748. + dwc_otg_module_params.
  72749. + max_transfer_size);
  72750. + }
  72751. + if (dwc_otg_module_params.max_packet_count != -1) {
  72752. + retval +=
  72753. + dwc_otg_set_param_max_packet_count(core_if,
  72754. + dwc_otg_module_params.
  72755. + max_packet_count);
  72756. + }
  72757. + if (dwc_otg_module_params.host_channels != -1) {
  72758. + retval +=
  72759. + dwc_otg_set_param_host_channels(core_if,
  72760. + dwc_otg_module_params.
  72761. + host_channels);
  72762. + }
  72763. + if (dwc_otg_module_params.dev_endpoints != -1) {
  72764. + retval +=
  72765. + dwc_otg_set_param_dev_endpoints(core_if,
  72766. + dwc_otg_module_params.
  72767. + dev_endpoints);
  72768. + }
  72769. + if (dwc_otg_module_params.phy_type != -1) {
  72770. + retval +=
  72771. + dwc_otg_set_param_phy_type(core_if,
  72772. + dwc_otg_module_params.phy_type);
  72773. + }
  72774. + if (dwc_otg_module_params.speed != -1) {
  72775. + retval +=
  72776. + dwc_otg_set_param_speed(core_if,
  72777. + dwc_otg_module_params.speed);
  72778. + }
  72779. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  72780. + retval +=
  72781. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  72782. + dwc_otg_module_params.
  72783. + host_ls_low_power_phy_clk);
  72784. + }
  72785. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  72786. + retval +=
  72787. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  72788. + dwc_otg_module_params.
  72789. + phy_ulpi_ddr);
  72790. + }
  72791. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  72792. + retval +=
  72793. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  72794. + dwc_otg_module_params.
  72795. + phy_ulpi_ext_vbus);
  72796. + }
  72797. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  72798. + retval +=
  72799. + dwc_otg_set_param_phy_utmi_width(core_if,
  72800. + dwc_otg_module_params.
  72801. + phy_utmi_width);
  72802. + }
  72803. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  72804. + retval +=
  72805. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  72806. + dwc_otg_module_params.ulpi_fs_ls);
  72807. + }
  72808. + if (dwc_otg_module_params.ts_dline != -1) {
  72809. + retval +=
  72810. + dwc_otg_set_param_ts_dline(core_if,
  72811. + dwc_otg_module_params.ts_dline);
  72812. + }
  72813. + if (dwc_otg_module_params.i2c_enable != -1) {
  72814. + retval +=
  72815. + dwc_otg_set_param_i2c_enable(core_if,
  72816. + dwc_otg_module_params.
  72817. + i2c_enable);
  72818. + }
  72819. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  72820. + retval +=
  72821. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  72822. + dwc_otg_module_params.
  72823. + en_multiple_tx_fifo);
  72824. + }
  72825. + for (i = 0; i < 15; i++) {
  72826. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  72827. + retval +=
  72828. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  72829. + dwc_otg_module_params.
  72830. + dev_perio_tx_fifo_size
  72831. + [i], i);
  72832. + }
  72833. + }
  72834. +
  72835. + for (i = 0; i < 15; i++) {
  72836. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  72837. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  72838. + dwc_otg_module_params.
  72839. + dev_tx_fifo_size
  72840. + [i], i);
  72841. + }
  72842. + }
  72843. + if (dwc_otg_module_params.thr_ctl != -1) {
  72844. + retval +=
  72845. + dwc_otg_set_param_thr_ctl(core_if,
  72846. + dwc_otg_module_params.thr_ctl);
  72847. + }
  72848. + if (dwc_otg_module_params.mpi_enable != -1) {
  72849. + retval +=
  72850. + dwc_otg_set_param_mpi_enable(core_if,
  72851. + dwc_otg_module_params.
  72852. + mpi_enable);
  72853. + }
  72854. + if (dwc_otg_module_params.pti_enable != -1) {
  72855. + retval +=
  72856. + dwc_otg_set_param_pti_enable(core_if,
  72857. + dwc_otg_module_params.
  72858. + pti_enable);
  72859. + }
  72860. + if (dwc_otg_module_params.lpm_enable != -1) {
  72861. + retval +=
  72862. + dwc_otg_set_param_lpm_enable(core_if,
  72863. + dwc_otg_module_params.
  72864. + lpm_enable);
  72865. + }
  72866. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  72867. + retval +=
  72868. + dwc_otg_set_param_ic_usb_cap(core_if,
  72869. + dwc_otg_module_params.
  72870. + ic_usb_cap);
  72871. + }
  72872. + if (dwc_otg_module_params.tx_thr_length != -1) {
  72873. + retval +=
  72874. + dwc_otg_set_param_tx_thr_length(core_if,
  72875. + dwc_otg_module_params.tx_thr_length);
  72876. + }
  72877. + if (dwc_otg_module_params.rx_thr_length != -1) {
  72878. + retval +=
  72879. + dwc_otg_set_param_rx_thr_length(core_if,
  72880. + dwc_otg_module_params.
  72881. + rx_thr_length);
  72882. + }
  72883. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  72884. + retval +=
  72885. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  72886. + dwc_otg_module_params.ahb_thr_ratio);
  72887. + }
  72888. + if (dwc_otg_module_params.power_down != -1) {
  72889. + retval +=
  72890. + dwc_otg_set_param_power_down(core_if,
  72891. + dwc_otg_module_params.power_down);
  72892. + }
  72893. + if (dwc_otg_module_params.reload_ctl != -1) {
  72894. + retval +=
  72895. + dwc_otg_set_param_reload_ctl(core_if,
  72896. + dwc_otg_module_params.reload_ctl);
  72897. + }
  72898. +
  72899. + if (dwc_otg_module_params.dev_out_nak != -1) {
  72900. + retval +=
  72901. + dwc_otg_set_param_dev_out_nak(core_if,
  72902. + dwc_otg_module_params.dev_out_nak);
  72903. + }
  72904. +
  72905. + if (dwc_otg_module_params.cont_on_bna != -1) {
  72906. + retval +=
  72907. + dwc_otg_set_param_cont_on_bna(core_if,
  72908. + dwc_otg_module_params.cont_on_bna);
  72909. + }
  72910. +
  72911. + if (dwc_otg_module_params.ahb_single != -1) {
  72912. + retval +=
  72913. + dwc_otg_set_param_ahb_single(core_if,
  72914. + dwc_otg_module_params.ahb_single);
  72915. + }
  72916. +
  72917. + if (dwc_otg_module_params.otg_ver != -1) {
  72918. + retval +=
  72919. + dwc_otg_set_param_otg_ver(core_if,
  72920. + dwc_otg_module_params.otg_ver);
  72921. + }
  72922. + if (dwc_otg_module_params.adp_enable != -1) {
  72923. + retval +=
  72924. + dwc_otg_set_param_adp_enable(core_if,
  72925. + dwc_otg_module_params.
  72926. + adp_enable);
  72927. + }
  72928. + return retval;
  72929. +}
  72930. +
  72931. +/**
  72932. + * This function is the top level interrupt handler for the Common
  72933. + * (Device and host modes) interrupts.
  72934. + */
  72935. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  72936. +{
  72937. + int32_t retval = IRQ_NONE;
  72938. +
  72939. + retval = dwc_otg_handle_common_intr(dev);
  72940. + if (retval != 0) {
  72941. + S3C2410X_CLEAR_EINTPEND();
  72942. + }
  72943. + return IRQ_RETVAL(retval);
  72944. +}
  72945. +
  72946. +/**
  72947. + * This function is called when a lm_device is unregistered with the
  72948. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  72949. + * executed. The device may or may not be electrically present. If it is
  72950. + * present, the driver stops device processing. Any resources used on behalf
  72951. + * of this device are freed.
  72952. + *
  72953. + * @param _dev
  72954. + */
  72955. +#ifdef LM_INTERFACE
  72956. +#define REM_RETVAL(n)
  72957. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  72958. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  72959. +#elif defined(PCI_INTERFACE)
  72960. +#define REM_RETVAL(n)
  72961. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  72962. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  72963. +#elif defined(PLATFORM_INTERFACE)
  72964. +#define REM_RETVAL(n) n
  72965. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  72966. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  72967. +#endif
  72968. +
  72969. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  72970. +
  72971. + if (!otg_dev) {
  72972. + /* Memory allocation for the dwc_otg_device failed. */
  72973. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  72974. + return REM_RETVAL(-ENOMEM);
  72975. + }
  72976. +#ifndef DWC_DEVICE_ONLY
  72977. + if (otg_dev->hcd) {
  72978. + hcd_remove(_dev);
  72979. + } else {
  72980. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  72981. + return REM_RETVAL(-EINVAL);
  72982. + }
  72983. +#endif
  72984. +
  72985. +#ifndef DWC_HOST_ONLY
  72986. + if (otg_dev->pcd) {
  72987. + pcd_remove(_dev);
  72988. + } else {
  72989. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  72990. + return REM_RETVAL(-EINVAL);
  72991. + }
  72992. +#endif
  72993. + /*
  72994. + * Free the IRQ
  72995. + */
  72996. + if (otg_dev->common_irq_installed) {
  72997. +#ifdef PLATFORM_INTERFACE
  72998. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  72999. +#else
  73000. + free_irq(_dev->irq, otg_dev);
  73001. +#endif
  73002. + } else {
  73003. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  73004. + return REM_RETVAL(-ENXIO);
  73005. + }
  73006. +
  73007. + if (otg_dev->core_if) {
  73008. + dwc_otg_cil_remove(otg_dev->core_if);
  73009. + } else {
  73010. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  73011. + return REM_RETVAL(-ENXIO);
  73012. + }
  73013. +
  73014. + /*
  73015. + * Remove the device attributes
  73016. + */
  73017. + dwc_otg_attr_remove(_dev);
  73018. +
  73019. + /*
  73020. + * Return the memory.
  73021. + */
  73022. + if (otg_dev->os_dep.base) {
  73023. + iounmap(otg_dev->os_dep.base);
  73024. + }
  73025. + DWC_FREE(otg_dev);
  73026. +
  73027. + /*
  73028. + * Clear the drvdata pointer.
  73029. + */
  73030. +#ifdef LM_INTERFACE
  73031. + lm_set_drvdata(_dev, 0);
  73032. +#elif defined(PCI_INTERFACE)
  73033. + release_mem_region(otg_dev->os_dep.rsrc_start,
  73034. + otg_dev->os_dep.rsrc_len);
  73035. + pci_set_drvdata(_dev, 0);
  73036. +#elif defined(PLATFORM_INTERFACE)
  73037. + platform_set_drvdata(_dev, 0);
  73038. +#endif
  73039. + return REM_RETVAL(0);
  73040. +}
  73041. +
  73042. +/**
  73043. + * This function is called when an lm_device is bound to a
  73044. + * dwc_otg_driver. It creates the driver components required to
  73045. + * control the device (CIL, HCD, and PCD) and it initializes the
  73046. + * device. The driver components are stored in a dwc_otg_device
  73047. + * structure. A reference to the dwc_otg_device is saved in the
  73048. + * lm_device. This allows the driver to access the dwc_otg_device
  73049. + * structure on subsequent calls to driver methods for this device.
  73050. + *
  73051. + * @param _dev Bus device
  73052. + */
  73053. +static int dwc_otg_driver_probe(
  73054. +#ifdef LM_INTERFACE
  73055. + struct lm_device *_dev
  73056. +#elif defined(PCI_INTERFACE)
  73057. + struct pci_dev *_dev,
  73058. + const struct pci_device_id *id
  73059. +#elif defined(PLATFORM_INTERFACE)
  73060. + struct platform_device *_dev
  73061. +#endif
  73062. + )
  73063. +{
  73064. + int retval = 0;
  73065. + dwc_otg_device_t *dwc_otg_device;
  73066. + int devirq;
  73067. +
  73068. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  73069. +#ifdef LM_INTERFACE
  73070. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  73071. +#elif defined(PCI_INTERFACE)
  73072. + if (!id) {
  73073. + DWC_ERROR("Invalid pci_device_id %p", id);
  73074. + return -EINVAL;
  73075. + }
  73076. +
  73077. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  73078. + DWC_ERROR("Invalid pci_device %p", _dev);
  73079. + return -ENODEV;
  73080. + }
  73081. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  73082. + /* other stuff needed as well? */
  73083. +
  73084. +#elif defined(PLATFORM_INTERFACE)
  73085. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  73086. + (unsigned)_dev->resource->start,
  73087. + (unsigned)(_dev->resource->end - _dev->resource->start));
  73088. +#endif
  73089. +
  73090. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  73091. +
  73092. + if (!dwc_otg_device) {
  73093. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  73094. + return -ENOMEM;
  73095. + }
  73096. +
  73097. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  73098. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  73099. +
  73100. + /*
  73101. + * Map the DWC_otg Core memory into virtual address space.
  73102. + */
  73103. +#ifdef LM_INTERFACE
  73104. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  73105. +
  73106. + if (!dwc_otg_device->os_dep.base) {
  73107. + dev_err(&_dev->dev, "ioremap() failed\n");
  73108. + DWC_FREE(dwc_otg_device);
  73109. + return -ENOMEM;
  73110. + }
  73111. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  73112. + (unsigned)dwc_otg_device->os_dep.base);
  73113. +#elif defined(PCI_INTERFACE)
  73114. + _dev->current_state = PCI_D0;
  73115. + _dev->dev.power.power_state = PMSG_ON;
  73116. +
  73117. + if (!_dev->irq) {
  73118. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  73119. + pci_name(_dev));
  73120. + iounmap(dwc_otg_device->os_dep.base);
  73121. + DWC_FREE(dwc_otg_device);
  73122. + return -ENODEV;
  73123. + }
  73124. +
  73125. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  73126. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  73127. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  73128. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  73129. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  73130. + if (!request_mem_region
  73131. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  73132. + "dwc_otg")) {
  73133. + dev_dbg(&_dev->dev, "error requesting memory\n");
  73134. + iounmap(dwc_otg_device->os_dep.base);
  73135. + DWC_FREE(dwc_otg_device);
  73136. + return -EFAULT;
  73137. + }
  73138. +
  73139. + dwc_otg_device->os_dep.base =
  73140. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  73141. + dwc_otg_device->os_dep.rsrc_len);
  73142. + if (dwc_otg_device->os_dep.base == NULL) {
  73143. + dev_dbg(&_dev->dev, "error mapping memory\n");
  73144. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  73145. + dwc_otg_device->os_dep.rsrc_len);
  73146. + iounmap(dwc_otg_device->os_dep.base);
  73147. + DWC_FREE(dwc_otg_device);
  73148. + return -EFAULT;
  73149. + }
  73150. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  73151. + dwc_otg_device->os_dep.base);
  73152. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  73153. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  73154. + dwc_otg_device->os_dep.base);
  73155. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  73156. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  73157. + dwc_otg_device->os_dep.base);
  73158. +
  73159. + pci_set_master(_dev);
  73160. + pci_set_drvdata(_dev, dwc_otg_device);
  73161. +#elif defined(PLATFORM_INTERFACE)
  73162. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  73163. + _dev->resource->start,
  73164. + _dev->resource->end - _dev->resource->start + 1);
  73165. +#if 1
  73166. + if (!request_mem_region(_dev->resource[0].start,
  73167. + _dev->resource[0].end - _dev->resource[0].start + 1,
  73168. + "dwc_otg")) {
  73169. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  73170. + retval = -EFAULT;
  73171. + goto fail;
  73172. + }
  73173. +
  73174. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  73175. + _dev->resource[0].end -
  73176. + _dev->resource[0].start+1);
  73177. + if (fiq_enable)
  73178. + {
  73179. + if (!request_mem_region(_dev->resource[1].start,
  73180. + _dev->resource[1].end - _dev->resource[1].start + 1,
  73181. + "dwc_otg")) {
  73182. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  73183. + retval = -EFAULT;
  73184. + goto fail;
  73185. + }
  73186. +
  73187. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  73188. + _dev->resource[1].end -
  73189. + _dev->resource[1].start + 1);
  73190. + }
  73191. +
  73192. +#else
  73193. + {
  73194. + struct map_desc desc = {
  73195. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  73196. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  73197. + .length = SZ_128K,
  73198. + .type = MT_DEVICE
  73199. + };
  73200. + iotable_init(&desc, 1);
  73201. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  73202. + }
  73203. +#endif
  73204. + if (!dwc_otg_device->os_dep.base) {
  73205. + dev_err(&_dev->dev, "ioremap() failed\n");
  73206. + retval = -ENOMEM;
  73207. + goto fail;
  73208. + }
  73209. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  73210. + (unsigned)dwc_otg_device->os_dep.base);
  73211. +#endif
  73212. +
  73213. + /*
  73214. + * Initialize driver data to point to the global DWC_otg
  73215. + * Device structure.
  73216. + */
  73217. +#ifdef LM_INTERFACE
  73218. + lm_set_drvdata(_dev, dwc_otg_device);
  73219. +#elif defined(PLATFORM_INTERFACE)
  73220. + platform_set_drvdata(_dev, dwc_otg_device);
  73221. +#endif
  73222. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  73223. +
  73224. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  73225. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  73226. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  73227. +
  73228. + if (!dwc_otg_device->core_if) {
  73229. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  73230. + retval = -ENOMEM;
  73231. + goto fail;
  73232. + }
  73233. +
  73234. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  73235. + /*
  73236. + * Attempt to ensure this device is really a DWC_otg Controller.
  73237. + * Read and verify the SNPSID register contents. The value should be
  73238. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  73239. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  73240. + */
  73241. +
  73242. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  73243. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  73244. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  73245. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  73246. + retval = -EINVAL;
  73247. + goto fail;
  73248. + }
  73249. +
  73250. + /*
  73251. + * Validate parameter values.
  73252. + */
  73253. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  73254. + if (set_parameters(dwc_otg_device->core_if)) {
  73255. + retval = -EINVAL;
  73256. + goto fail;
  73257. + }
  73258. +
  73259. + /*
  73260. + * Create Device Attributes in sysfs
  73261. + */
  73262. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  73263. + dwc_otg_attr_create(_dev);
  73264. +
  73265. + /*
  73266. + * Disable the global interrupt until all the interrupt
  73267. + * handlers are installed.
  73268. + */
  73269. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  73270. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  73271. +
  73272. + /*
  73273. + * Install the interrupt handler for the common interrupts before
  73274. + * enabling common interrupts in core_init below.
  73275. + */
  73276. +
  73277. +#if defined(PLATFORM_INTERFACE)
  73278. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  73279. +#else
  73280. + devirq = _dev->irq;
  73281. +#endif
  73282. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  73283. + devirq);
  73284. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  73285. + retval = request_irq(devirq, dwc_otg_common_irq,
  73286. + IRQF_SHARED,
  73287. + "dwc_otg", dwc_otg_device);
  73288. + if (retval) {
  73289. + DWC_ERROR("request of irq%d failed\n", devirq);
  73290. + retval = -EBUSY;
  73291. + goto fail;
  73292. + } else {
  73293. + dwc_otg_device->common_irq_installed = 1;
  73294. + }
  73295. +
  73296. +#ifndef IRQF_TRIGGER_LOW
  73297. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  73298. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  73299. + set_irq_type(devirq,
  73300. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  73301. + IRQT_LOW
  73302. +#else
  73303. + IRQ_TYPE_LEVEL_LOW
  73304. +#endif
  73305. + );
  73306. +#endif
  73307. +#endif /*IRQF_TRIGGER_LOW*/
  73308. +
  73309. + /*
  73310. + * Initialize the DWC_otg core.
  73311. + */
  73312. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  73313. + dwc_otg_core_init(dwc_otg_device->core_if);
  73314. +
  73315. +#ifndef DWC_HOST_ONLY
  73316. + /*
  73317. + * Initialize the PCD
  73318. + */
  73319. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  73320. + retval = pcd_init(_dev);
  73321. + if (retval != 0) {
  73322. + DWC_ERROR("pcd_init failed\n");
  73323. + dwc_otg_device->pcd = NULL;
  73324. + goto fail;
  73325. + }
  73326. +#endif
  73327. +#ifndef DWC_DEVICE_ONLY
  73328. + /*
  73329. + * Initialize the HCD
  73330. + */
  73331. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  73332. + retval = hcd_init(_dev);
  73333. + if (retval != 0) {
  73334. + DWC_ERROR("hcd_init failed\n");
  73335. + dwc_otg_device->hcd = NULL;
  73336. + goto fail;
  73337. + }
  73338. +#endif
  73339. + /* Recover from drvdata having been overwritten by hcd_init() */
  73340. +#ifdef LM_INTERFACE
  73341. + lm_set_drvdata(_dev, dwc_otg_device);
  73342. +#elif defined(PLATFORM_INTERFACE)
  73343. + platform_set_drvdata(_dev, dwc_otg_device);
  73344. +#elif defined(PCI_INTERFACE)
  73345. + pci_set_drvdata(_dev, dwc_otg_device);
  73346. + dwc_otg_device->os_dep.pcidev = _dev;
  73347. +#endif
  73348. +
  73349. + /*
  73350. + * Enable the global interrupt after all the interrupt
  73351. + * handlers are installed if there is no ADP support else
  73352. + * perform initial actions required for Internal ADP logic.
  73353. + */
  73354. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  73355. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  73356. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  73357. + dev_dbg(&_dev->dev, "Done\n");
  73358. + } else
  73359. + dwc_otg_adp_start(dwc_otg_device->core_if,
  73360. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  73361. +
  73362. + return 0;
  73363. +
  73364. +fail:
  73365. + dwc_otg_driver_remove(_dev);
  73366. + return retval;
  73367. +}
  73368. +
  73369. +/**
  73370. + * This structure defines the methods to be called by a bus driver
  73371. + * during the lifecycle of a device on that bus. Both drivers and
  73372. + * devices are registered with a bus driver. The bus driver matches
  73373. + * devices to drivers based on information in the device and driver
  73374. + * structures.
  73375. + *
  73376. + * The probe function is called when the bus driver matches a device
  73377. + * to this driver. The remove function is called when a device is
  73378. + * unregistered with the bus driver.
  73379. + */
  73380. +#ifdef LM_INTERFACE
  73381. +static struct lm_driver dwc_otg_driver = {
  73382. + .drv = {.name = (char *)dwc_driver_name,},
  73383. + .probe = dwc_otg_driver_probe,
  73384. + .remove = dwc_otg_driver_remove,
  73385. + // 'suspend' and 'resume' absent
  73386. +};
  73387. +#elif defined(PCI_INTERFACE)
  73388. +static const struct pci_device_id pci_ids[] = { {
  73389. + PCI_DEVICE(0x16c3, 0xabcd),
  73390. + .driver_data =
  73391. + (unsigned long)0xdeadbeef,
  73392. + }, { /* end: all zeroes */ }
  73393. +};
  73394. +
  73395. +MODULE_DEVICE_TABLE(pci, pci_ids);
  73396. +
  73397. +/* pci driver glue; this is a "new style" PCI driver module */
  73398. +static struct pci_driver dwc_otg_driver = {
  73399. + .name = "dwc_otg",
  73400. + .id_table = pci_ids,
  73401. +
  73402. + .probe = dwc_otg_driver_probe,
  73403. + .remove = dwc_otg_driver_remove,
  73404. +
  73405. + .driver = {
  73406. + .name = (char *)dwc_driver_name,
  73407. + },
  73408. +};
  73409. +#elif defined(PLATFORM_INTERFACE)
  73410. +static struct platform_device_id platform_ids[] = {
  73411. + {
  73412. + .name = "bcm2708_usb",
  73413. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  73414. + },
  73415. + { /* end: all zeroes */ }
  73416. +};
  73417. +MODULE_DEVICE_TABLE(platform, platform_ids);
  73418. +
  73419. +static struct platform_driver dwc_otg_driver = {
  73420. + .driver = {
  73421. + .name = (char *)dwc_driver_name,
  73422. + },
  73423. + .id_table = platform_ids,
  73424. +
  73425. + .probe = dwc_otg_driver_probe,
  73426. + .remove = dwc_otg_driver_remove,
  73427. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  73428. +};
  73429. +#endif
  73430. +
  73431. +/**
  73432. + * This function is called when the dwc_otg_driver is installed with the
  73433. + * insmod command. It registers the dwc_otg_driver structure with the
  73434. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  73435. + * to be called. In addition, the bus driver will automatically expose
  73436. + * attributes defined for the device and driver in the special sysfs file
  73437. + * system.
  73438. + *
  73439. + * @return
  73440. + */
  73441. +static int __init dwc_otg_driver_init(void)
  73442. +{
  73443. + int retval = 0;
  73444. + int error;
  73445. + struct device_driver *drv;
  73446. +
  73447. + if(fiq_fsm_enable && !fiq_enable) {
  73448. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  73449. + fiq_enable = 1;
  73450. + }
  73451. +
  73452. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  73453. + DWC_DRIVER_VERSION,
  73454. +#ifdef LM_INTERFACE
  73455. + "logicmodule");
  73456. + retval = lm_driver_register(&dwc_otg_driver);
  73457. + drv = &dwc_otg_driver.drv;
  73458. +#elif defined(PCI_INTERFACE)
  73459. + "pci");
  73460. + retval = pci_register_driver(&dwc_otg_driver);
  73461. + drv = &dwc_otg_driver.driver;
  73462. +#elif defined(PLATFORM_INTERFACE)
  73463. + "platform");
  73464. + retval = platform_driver_register(&dwc_otg_driver);
  73465. + drv = &dwc_otg_driver.driver;
  73466. +#endif
  73467. + if (retval < 0) {
  73468. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  73469. + return retval;
  73470. + }
  73471. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  73472. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  73473. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  73474. +
  73475. + error = driver_create_file(drv, &driver_attr_version);
  73476. +#ifdef DEBUG
  73477. + error = driver_create_file(drv, &driver_attr_debuglevel);
  73478. +#endif
  73479. + return retval;
  73480. +}
  73481. +
  73482. +module_init(dwc_otg_driver_init);
  73483. +
  73484. +/**
  73485. + * This function is called when the driver is removed from the kernel
  73486. + * with the rmmod command. The driver unregisters itself with its bus
  73487. + * driver.
  73488. + *
  73489. + */
  73490. +static void __exit dwc_otg_driver_cleanup(void)
  73491. +{
  73492. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  73493. +
  73494. +#ifdef LM_INTERFACE
  73495. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  73496. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  73497. + lm_driver_unregister(&dwc_otg_driver);
  73498. +#elif defined(PCI_INTERFACE)
  73499. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  73500. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  73501. + pci_unregister_driver(&dwc_otg_driver);
  73502. +#elif defined(PLATFORM_INTERFACE)
  73503. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  73504. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  73505. + platform_driver_unregister(&dwc_otg_driver);
  73506. +#endif
  73507. +
  73508. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  73509. +}
  73510. +
  73511. +module_exit(dwc_otg_driver_cleanup);
  73512. +
  73513. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  73514. +MODULE_AUTHOR("Synopsys Inc.");
  73515. +MODULE_LICENSE("GPL");
  73516. +
  73517. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  73518. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  73519. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  73520. +MODULE_PARM_DESC(opt, "OPT Mode");
  73521. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  73522. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  73523. +
  73524. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  73525. + 0444);
  73526. +MODULE_PARM_DESC(dma_desc_enable,
  73527. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  73528. +
  73529. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  73530. + 0444);
  73531. +MODULE_PARM_DESC(dma_burst_size,
  73532. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  73533. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  73534. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  73535. +module_param_named(host_support_fs_ls_low_power,
  73536. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  73537. + 0444);
  73538. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  73539. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  73540. +module_param_named(host_ls_low_power_phy_clk,
  73541. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  73542. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  73543. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  73544. +module_param_named(enable_dynamic_fifo,
  73545. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  73546. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  73547. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  73548. + 0444);
  73549. +MODULE_PARM_DESC(data_fifo_size,
  73550. + "Total number of words in the data FIFO memory 32-32768");
  73551. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  73552. + int, 0444);
  73553. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  73554. +module_param_named(dev_nperio_tx_fifo_size,
  73555. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  73556. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  73557. + "Number of words in the non-periodic Tx FIFO 16-32768");
  73558. +module_param_named(dev_perio_tx_fifo_size_1,
  73559. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  73560. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  73561. + "Number of words in the periodic Tx FIFO 4-768");
  73562. +module_param_named(dev_perio_tx_fifo_size_2,
  73563. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  73564. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  73565. + "Number of words in the periodic Tx FIFO 4-768");
  73566. +module_param_named(dev_perio_tx_fifo_size_3,
  73567. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  73568. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  73569. + "Number of words in the periodic Tx FIFO 4-768");
  73570. +module_param_named(dev_perio_tx_fifo_size_4,
  73571. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  73572. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  73573. + "Number of words in the periodic Tx FIFO 4-768");
  73574. +module_param_named(dev_perio_tx_fifo_size_5,
  73575. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  73576. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  73577. + "Number of words in the periodic Tx FIFO 4-768");
  73578. +module_param_named(dev_perio_tx_fifo_size_6,
  73579. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  73580. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  73581. + "Number of words in the periodic Tx FIFO 4-768");
  73582. +module_param_named(dev_perio_tx_fifo_size_7,
  73583. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  73584. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  73585. + "Number of words in the periodic Tx FIFO 4-768");
  73586. +module_param_named(dev_perio_tx_fifo_size_8,
  73587. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  73588. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  73589. + "Number of words in the periodic Tx FIFO 4-768");
  73590. +module_param_named(dev_perio_tx_fifo_size_9,
  73591. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  73592. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  73593. + "Number of words in the periodic Tx FIFO 4-768");
  73594. +module_param_named(dev_perio_tx_fifo_size_10,
  73595. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  73596. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  73597. + "Number of words in the periodic Tx FIFO 4-768");
  73598. +module_param_named(dev_perio_tx_fifo_size_11,
  73599. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  73600. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  73601. + "Number of words in the periodic Tx FIFO 4-768");
  73602. +module_param_named(dev_perio_tx_fifo_size_12,
  73603. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  73604. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  73605. + "Number of words in the periodic Tx FIFO 4-768");
  73606. +module_param_named(dev_perio_tx_fifo_size_13,
  73607. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  73608. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  73609. + "Number of words in the periodic Tx FIFO 4-768");
  73610. +module_param_named(dev_perio_tx_fifo_size_14,
  73611. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  73612. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  73613. + "Number of words in the periodic Tx FIFO 4-768");
  73614. +module_param_named(dev_perio_tx_fifo_size_15,
  73615. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  73616. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  73617. + "Number of words in the periodic Tx FIFO 4-768");
  73618. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  73619. + int, 0444);
  73620. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  73621. +module_param_named(host_nperio_tx_fifo_size,
  73622. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  73623. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  73624. + "Number of words in the non-periodic Tx FIFO 16-32768");
  73625. +module_param_named(host_perio_tx_fifo_size,
  73626. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  73627. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  73628. + "Number of words in the host periodic Tx FIFO 16-32768");
  73629. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  73630. + int, 0444);
  73631. +/** @todo Set the max to 512K, modify checks */
  73632. +MODULE_PARM_DESC(max_transfer_size,
  73633. + "The maximum transfer size supported in bytes 2047-65535");
  73634. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  73635. + int, 0444);
  73636. +MODULE_PARM_DESC(max_packet_count,
  73637. + "The maximum number of packets in a transfer 15-511");
  73638. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  73639. + 0444);
  73640. +MODULE_PARM_DESC(host_channels,
  73641. + "The number of host channel registers to use 1-16");
  73642. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  73643. + 0444);
  73644. +MODULE_PARM_DESC(dev_endpoints,
  73645. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  73646. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  73647. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  73648. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  73649. + 0444);
  73650. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  73651. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  73652. +MODULE_PARM_DESC(phy_ulpi_ddr,
  73653. + "ULPI at double or single data rate 0=Single 1=Double");
  73654. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  73655. + int, 0444);
  73656. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  73657. + "ULPI PHY using internal or external vbus 0=Internal");
  73658. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  73659. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  73660. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  73661. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  73662. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  73663. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  73664. +module_param_named(debug, g_dbg_lvl, int, 0444);
  73665. +MODULE_PARM_DESC(debug, "");
  73666. +
  73667. +module_param_named(en_multiple_tx_fifo,
  73668. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  73669. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  73670. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  73671. +module_param_named(dev_tx_fifo_size_1,
  73672. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  73673. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  73674. +module_param_named(dev_tx_fifo_size_2,
  73675. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  73676. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  73677. +module_param_named(dev_tx_fifo_size_3,
  73678. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  73679. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  73680. +module_param_named(dev_tx_fifo_size_4,
  73681. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  73682. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  73683. +module_param_named(dev_tx_fifo_size_5,
  73684. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  73685. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  73686. +module_param_named(dev_tx_fifo_size_6,
  73687. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  73688. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  73689. +module_param_named(dev_tx_fifo_size_7,
  73690. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  73691. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  73692. +module_param_named(dev_tx_fifo_size_8,
  73693. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  73694. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  73695. +module_param_named(dev_tx_fifo_size_9,
  73696. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  73697. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  73698. +module_param_named(dev_tx_fifo_size_10,
  73699. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  73700. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  73701. +module_param_named(dev_tx_fifo_size_11,
  73702. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  73703. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  73704. +module_param_named(dev_tx_fifo_size_12,
  73705. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  73706. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  73707. +module_param_named(dev_tx_fifo_size_13,
  73708. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  73709. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  73710. +module_param_named(dev_tx_fifo_size_14,
  73711. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  73712. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  73713. +module_param_named(dev_tx_fifo_size_15,
  73714. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  73715. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  73716. +
  73717. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  73718. +MODULE_PARM_DESC(thr_ctl,
  73719. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  73720. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  73721. + 0444);
  73722. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  73723. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  73724. + 0444);
  73725. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  73726. +
  73727. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  73728. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  73729. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  73730. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  73731. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  73732. +MODULE_PARM_DESC(ic_usb_cap,
  73733. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  73734. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  73735. + 0444);
  73736. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  73737. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  73738. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  73739. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  73740. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  73741. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  73742. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  73743. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  73744. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  73745. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  73746. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  73747. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  73748. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  73749. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  73750. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  73751. +module_param(microframe_schedule, bool, 0444);
  73752. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  73753. +
  73754. +module_param(fiq_enable, bool, 0444);
  73755. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  73756. +module_param(nak_holdoff, ushort, 0644);
  73757. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  73758. +module_param(fiq_fsm_enable, bool, 0444);
  73759. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  73760. +module_param(fiq_fsm_mask, ushort, 0444);
  73761. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  73762. + "Bit 0 : Non-periodic split transactions\n"
  73763. + "Bit 1 : Periodic split transactions\n"
  73764. + "Bit 2 : High-speed multi-transfer isochronous\n"
  73765. + "All other bits should be set 0.");
  73766. +
  73767. +
  73768. +/** @page "Module Parameters"
  73769. + *
  73770. + * The following parameters may be specified when starting the module.
  73771. + * These parameters define how the DWC_otg controller should be
  73772. + * configured. Parameter values are passed to the CIL initialization
  73773. + * function dwc_otg_cil_init
  73774. + *
  73775. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  73776. + *
  73777. +
  73778. + <table>
  73779. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  73780. +
  73781. + <tr>
  73782. + <td>otg_cap</td>
  73783. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  73784. + value for this parameter if none is specified.
  73785. + - 0: HNP and SRP capable (default, if available)
  73786. + - 1: SRP Only capable
  73787. + - 2: No HNP/SRP capable
  73788. + </td></tr>
  73789. +
  73790. + <tr>
  73791. + <td>dma_enable</td>
  73792. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  73793. + The driver will automatically detect the value for this parameter if none is
  73794. + specified.
  73795. + - 0: Slave
  73796. + - 1: DMA (default, if available)
  73797. + </td></tr>
  73798. +
  73799. + <tr>
  73800. + <td>dma_burst_size</td>
  73801. + <td>The DMA Burst size (applicable only for External DMA Mode).
  73802. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  73803. + </td></tr>
  73804. +
  73805. + <tr>
  73806. + <td>speed</td>
  73807. + <td>Specifies the maximum speed of operation in host and device mode. The
  73808. + actual speed depends on the speed of the attached device and the value of
  73809. + phy_type.
  73810. + - 0: High Speed (default)
  73811. + - 1: Full Speed
  73812. + </td></tr>
  73813. +
  73814. + <tr>
  73815. + <td>host_support_fs_ls_low_power</td>
  73816. + <td>Specifies whether low power mode is supported when attached to a Full
  73817. + Speed or Low Speed device in host mode.
  73818. + - 0: Don't support low power mode (default)
  73819. + - 1: Support low power mode
  73820. + </td></tr>
  73821. +
  73822. + <tr>
  73823. + <td>host_ls_low_power_phy_clk</td>
  73824. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  73825. + Speed device in host mode. This parameter is applicable only if
  73826. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  73827. + - 0: 48 MHz (default)
  73828. + - 1: 6 MHz
  73829. + </td></tr>
  73830. +
  73831. + <tr>
  73832. + <td>enable_dynamic_fifo</td>
  73833. + <td> Specifies whether FIFOs may be resized by the driver software.
  73834. + - 0: Use cC FIFO size parameters
  73835. + - 1: Allow dynamic FIFO sizing (default)
  73836. + </td></tr>
  73837. +
  73838. + <tr>
  73839. + <td>data_fifo_size</td>
  73840. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  73841. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  73842. + - Values: 32 to 32768 (default 8192)
  73843. +
  73844. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  73845. + </td></tr>
  73846. +
  73847. + <tr>
  73848. + <td>dev_rx_fifo_size</td>
  73849. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  73850. + FIFO sizing is enabled.
  73851. + - Values: 16 to 32768 (default 1064)
  73852. + </td></tr>
  73853. +
  73854. + <tr>
  73855. + <td>dev_nperio_tx_fifo_size</td>
  73856. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  73857. + dynamic FIFO sizing is enabled.
  73858. + - Values: 16 to 32768 (default 1024)
  73859. + </td></tr>
  73860. +
  73861. + <tr>
  73862. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  73863. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  73864. + when dynamic FIFO sizing is enabled.
  73865. + - Values: 4 to 768 (default 256)
  73866. + </td></tr>
  73867. +
  73868. + <tr>
  73869. + <td>host_rx_fifo_size</td>
  73870. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  73871. + sizing is enabled.
  73872. + - Values: 16 to 32768 (default 1024)
  73873. + </td></tr>
  73874. +
  73875. + <tr>
  73876. + <td>host_nperio_tx_fifo_size</td>
  73877. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  73878. + dynamic FIFO sizing is enabled in the core.
  73879. + - Values: 16 to 32768 (default 1024)
  73880. + </td></tr>
  73881. +
  73882. + <tr>
  73883. + <td>host_perio_tx_fifo_size</td>
  73884. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  73885. + sizing is enabled.
  73886. + - Values: 16 to 32768 (default 1024)
  73887. + </td></tr>
  73888. +
  73889. + <tr>
  73890. + <td>max_transfer_size</td>
  73891. + <td>The maximum transfer size supported in bytes.
  73892. + - Values: 2047 to 65,535 (default 65,535)
  73893. + </td></tr>
  73894. +
  73895. + <tr>
  73896. + <td>max_packet_count</td>
  73897. + <td>The maximum number of packets in a transfer.
  73898. + - Values: 15 to 511 (default 511)
  73899. + </td></tr>
  73900. +
  73901. + <tr>
  73902. + <td>host_channels</td>
  73903. + <td>The number of host channel registers to use.
  73904. + - Values: 1 to 16 (default 12)
  73905. +
  73906. + Note: The FPGA configuration supports a maximum of 12 host channels.
  73907. + </td></tr>
  73908. +
  73909. + <tr>
  73910. + <td>dev_endpoints</td>
  73911. + <td>The number of endpoints in addition to EP0 available for device mode
  73912. + operations.
  73913. + - Values: 1 to 15 (default 6 IN and OUT)
  73914. +
  73915. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  73916. + addition to EP0.
  73917. + </td></tr>
  73918. +
  73919. + <tr>
  73920. + <td>phy_type</td>
  73921. + <td>Specifies the type of PHY interface to use. By default, the driver will
  73922. + automatically detect the phy_type.
  73923. + - 0: Full Speed
  73924. + - 1: UTMI+ (default, if available)
  73925. + - 2: ULPI
  73926. + </td></tr>
  73927. +
  73928. + <tr>
  73929. + <td>phy_utmi_width</td>
  73930. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  73931. + phy_type of UTMI+. Also, this parameter is applicable only if the
  73932. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  73933. + core has been configured to work at either data path width.
  73934. + - Values: 8 or 16 bits (default 16)
  73935. + </td></tr>
  73936. +
  73937. + <tr>
  73938. + <td>phy_ulpi_ddr</td>
  73939. + <td>Specifies whether the ULPI operates at double or single data rate. This
  73940. + parameter is only applicable if phy_type is ULPI.
  73941. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  73942. + - 1: double data rate ULPI interface with 4 bit wide data bus
  73943. + </td></tr>
  73944. +
  73945. + <tr>
  73946. + <td>i2c_enable</td>
  73947. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  73948. + parameter is only applicable if PHY_TYPE is FS.
  73949. + - 0: Disabled (default)
  73950. + - 1: Enabled
  73951. + </td></tr>
  73952. +
  73953. + <tr>
  73954. + <td>ulpi_fs_ls</td>
  73955. + <td>Specifies whether to use ULPI FS/LS mode only.
  73956. + - 0: Disabled (default)
  73957. + - 1: Enabled
  73958. + </td></tr>
  73959. +
  73960. + <tr>
  73961. + <td>ts_dline</td>
  73962. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  73963. + - 0: Disabled (default)
  73964. + - 1: Enabled
  73965. + </td></tr>
  73966. +
  73967. + <tr>
  73968. + <td>en_multiple_tx_fifo</td>
  73969. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  73970. + The driver will automatically detect the value for this parameter if none is
  73971. + specified.
  73972. + - 0: Disabled
  73973. + - 1: Enabled (default, if available)
  73974. + </td></tr>
  73975. +
  73976. + <tr>
  73977. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  73978. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  73979. + when dynamic FIFO sizing is enabled.
  73980. + - Values: 4 to 768 (default 256)
  73981. + </td></tr>
  73982. +
  73983. + <tr>
  73984. + <td>tx_thr_length</td>
  73985. + <td>Transmit Threshold length in 32 bit double words
  73986. + - Values: 8 to 128 (default 64)
  73987. + </td></tr>
  73988. +
  73989. + <tr>
  73990. + <td>rx_thr_length</td>
  73991. + <td>Receive Threshold length in 32 bit double words
  73992. + - Values: 8 to 128 (default 64)
  73993. + </td></tr>
  73994. +
  73995. +<tr>
  73996. + <td>thr_ctl</td>
  73997. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  73998. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  73999. + Rx transfers accordingly.
  74000. + The driver will automatically detect the value for this parameter if none is
  74001. + specified.
  74002. + - Values: 0 to 7 (default 0)
  74003. + Bit values indicate:
  74004. + - 0: Thresholding disabled
  74005. + - 1: Thresholding enabled
  74006. + </td></tr>
  74007. +
  74008. +<tr>
  74009. + <td>dma_desc_enable</td>
  74010. + <td>Specifies whether to enable Descriptor DMA mode.
  74011. + The driver will automatically detect the value for this parameter if none is
  74012. + specified.
  74013. + - 0: Descriptor DMA disabled
  74014. + - 1: Descriptor DMA (default, if available)
  74015. + </td></tr>
  74016. +
  74017. +<tr>
  74018. + <td>mpi_enable</td>
  74019. + <td>Specifies whether to enable MPI enhancement mode.
  74020. + The driver will automatically detect the value for this parameter if none is
  74021. + specified.
  74022. + - 0: MPI disabled (default)
  74023. + - 1: MPI enable
  74024. + </td></tr>
  74025. +
  74026. +<tr>
  74027. + <td>pti_enable</td>
  74028. + <td>Specifies whether to enable PTI enhancement support.
  74029. + The driver will automatically detect the value for this parameter if none is
  74030. + specified.
  74031. + - 0: PTI disabled (default)
  74032. + - 1: PTI enable
  74033. + </td></tr>
  74034. +
  74035. +<tr>
  74036. + <td>lpm_enable</td>
  74037. + <td>Specifies whether to enable LPM support.
  74038. + The driver will automatically detect the value for this parameter if none is
  74039. + specified.
  74040. + - 0: LPM disabled
  74041. + - 1: LPM enable (default, if available)
  74042. + </td></tr>
  74043. +
  74044. +<tr>
  74045. + <td>ic_usb_cap</td>
  74046. + <td>Specifies whether to enable IC_USB capability.
  74047. + The driver will automatically detect the value for this parameter if none is
  74048. + specified.
  74049. + - 0: IC_USB disabled (default, if available)
  74050. + - 1: IC_USB enable
  74051. + </td></tr>
  74052. +
  74053. +<tr>
  74054. + <td>ahb_thr_ratio</td>
  74055. + <td>Specifies AHB Threshold ratio.
  74056. + - Values: 0 to 3 (default 0)
  74057. + </td></tr>
  74058. +
  74059. +<tr>
  74060. + <td>power_down</td>
  74061. + <td>Specifies Power Down(Hibernation) Mode.
  74062. + The driver will automatically detect the value for this parameter if none is
  74063. + specified.
  74064. + - 0: Power Down disabled (default)
  74065. + - 2: Power Down enabled
  74066. + </td></tr>
  74067. +
  74068. + <tr>
  74069. + <td>reload_ctl</td>
  74070. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  74071. + run time. The driver will automatically detect the value for this parameter if
  74072. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  74073. + the core might misbehave.
  74074. + - 0: Reload Control disabled (default)
  74075. + - 1: Reload Control enabled
  74076. + </td></tr>
  74077. +
  74078. + <tr>
  74079. + <td>dev_out_nak</td>
  74080. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  74081. + The driver will automatically detect the value for this parameter if
  74082. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  74083. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  74084. + - 1: The core sets NAK after Bulk OUT transfer complete
  74085. + </td></tr>
  74086. +
  74087. + <tr>
  74088. + <td>cont_on_bna</td>
  74089. + <td>Specifies whether Enable Continue on BNA enabled or no.
  74090. + After receiving BNA interrupt the core disables the endpoint,when the
  74091. + endpoint is re-enabled by the application the
  74092. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  74093. + - 1: Core starts processing from the descriptor which received the BNA.
  74094. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  74095. + </td></tr>
  74096. +
  74097. + <tr>
  74098. + <td>ahb_single</td>
  74099. + <td>This bit when programmed supports SINGLE transfers for remainder data
  74100. + in a transfer for DMA mode of operation.
  74101. + - 0: The remainder data will be sent using INCR burst size (default)
  74102. + - 1: The remainder data will be sent using SINGLE burst size.
  74103. + </td></tr>
  74104. +
  74105. +<tr>
  74106. + <td>adp_enable</td>
  74107. + <td>Specifies whether ADP feature is enabled.
  74108. + The driver will automatically detect the value for this parameter if none is
  74109. + specified.
  74110. + - 0: ADP feature disabled (default)
  74111. + - 1: ADP feature enabled
  74112. + </td></tr>
  74113. +
  74114. + <tr>
  74115. + <td>otg_ver</td>
  74116. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  74117. + USB OTG device.
  74118. + - 0: OTG 2.0 support disabled (default)
  74119. + - 1: OTG 2.0 support enabled
  74120. + </td></tr>
  74121. +
  74122. +*/
  74123. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_driver.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  74124. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_driver.h 1969-12-31 18:00:00.000000000 -0600
  74125. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h 2014-12-11 14:02:55.396418001 -0600
  74126. @@ -0,0 +1,86 @@
  74127. +/* ==========================================================================
  74128. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  74129. + * $Revision: #19 $
  74130. + * $Date: 2010/11/15 $
  74131. + * $Change: 1627671 $
  74132. + *
  74133. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74134. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74135. + * otherwise expressly agreed to in writing between Synopsys and you.
  74136. + *
  74137. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74138. + * any End User Software License Agreement or Agreement for Licensed Product
  74139. + * with Synopsys or any supplement thereto. You are permitted to use and
  74140. + * redistribute this Software in source and binary forms, with or without
  74141. + * modification, provided that redistributions of source code must retain this
  74142. + * notice. You may not view, use, disclose, copy or distribute this file or
  74143. + * any information contained herein except pursuant to this license grant from
  74144. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74145. + * below, then you are not authorized to use the Software.
  74146. + *
  74147. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74148. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74149. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74150. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74151. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74152. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74153. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74154. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74155. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74156. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74157. + * DAMAGE.
  74158. + * ========================================================================== */
  74159. +
  74160. +#ifndef __DWC_OTG_DRIVER_H__
  74161. +#define __DWC_OTG_DRIVER_H__
  74162. +
  74163. +/** @file
  74164. + * This file contains the interface to the Linux driver.
  74165. + */
  74166. +#include "dwc_otg_os_dep.h"
  74167. +#include "dwc_otg_core_if.h"
  74168. +
  74169. +/* Type declarations */
  74170. +struct dwc_otg_pcd;
  74171. +struct dwc_otg_hcd;
  74172. +
  74173. +/**
  74174. + * This structure is a wrapper that encapsulates the driver components used to
  74175. + * manage a single DWC_otg controller.
  74176. + */
  74177. +typedef struct dwc_otg_device {
  74178. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  74179. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  74180. + * require this. */
  74181. + struct os_dependent os_dep;
  74182. +
  74183. + /** Pointer to the core interface structure. */
  74184. + dwc_otg_core_if_t *core_if;
  74185. +
  74186. + /** Pointer to the PCD structure. */
  74187. + struct dwc_otg_pcd *pcd;
  74188. +
  74189. + /** Pointer to the HCD structure. */
  74190. + struct dwc_otg_hcd *hcd;
  74191. +
  74192. + /** Flag to indicate whether the common IRQ handler is installed. */
  74193. + uint8_t common_irq_installed;
  74194. +
  74195. +} dwc_otg_device_t;
  74196. +
  74197. +/*We must clear S3C24XX_EINTPEND external interrupt register
  74198. + * because after clearing in this register trigerred IRQ from
  74199. + * H/W core in kernel interrupt can be occured again before OTG
  74200. + * handlers clear all IRQ sources of Core registers because of
  74201. + * timing latencies and Low Level IRQ Type.
  74202. + */
  74203. +#ifdef CONFIG_MACH_IPMATE
  74204. +#define S3C2410X_CLEAR_EINTPEND() \
  74205. +do { \
  74206. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  74207. +} while (0)
  74208. +#else
  74209. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  74210. +#endif
  74211. +
  74212. +#endif
  74213. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  74214. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c 1969-12-31 18:00:00.000000000 -0600
  74215. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c 2014-12-11 14:02:55.396418001 -0600
  74216. @@ -0,0 +1,1294 @@
  74217. +/*
  74218. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  74219. + *
  74220. + * Copyright (c) 2013 Raspberry Pi Foundation
  74221. + *
  74222. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  74223. + * All rights reserved.
  74224. + *
  74225. + * Redistribution and use in source and binary forms, with or without
  74226. + * modification, are permitted provided that the following conditions are met:
  74227. + * * Redistributions of source code must retain the above copyright
  74228. + * notice, this list of conditions and the following disclaimer.
  74229. + * * Redistributions in binary form must reproduce the above copyright
  74230. + * notice, this list of conditions and the following disclaimer in the
  74231. + * documentation and/or other materials provided with the distribution.
  74232. + * * Neither the name of Raspberry Pi nor the
  74233. + * names of its contributors may be used to endorse or promote products
  74234. + * derived from this software without specific prior written permission.
  74235. + *
  74236. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  74237. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  74238. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  74239. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  74240. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74241. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  74242. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  74243. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  74244. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  74245. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  74246. + *
  74247. + * This FIQ implements functionality that performs split transactions on
  74248. + * the dwc_otg hardware without any outside intervention. A split transaction
  74249. + * is "queued" by nominating a specific host channel to perform the entirety
  74250. + * of a split transaction. This FIQ will then perform the microframe-precise
  74251. + * scheduling required in each phase of the transaction until completion.
  74252. + *
  74253. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  74254. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  74255. + * for a FSM-enabled channel.
  74256. + *
  74257. + * NB: Large parts of this implementation have architecture-specific code.
  74258. + * For porting this functionality to other ARM machines, the minimum is required:
  74259. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  74260. + * to the FIQ
  74261. + * - A method of forcing a software generated interrupt from FIQ mode that then
  74262. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  74263. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  74264. + * processor core - there is no locking between the FIQ and IRQ (aside from
  74265. + * local_fiq_disable)
  74266. + *
  74267. + */
  74268. +
  74269. +#include "dwc_otg_fiq_fsm.h"
  74270. +
  74271. +
  74272. +char buffer[1000*16];
  74273. +int wptr;
  74274. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  74275. +{
  74276. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  74277. + va_list args;
  74278. + char text[17];
  74279. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  74280. +
  74281. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  74282. + {
  74283. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  74284. + va_start(args, fmt);
  74285. + vsnprintf(text+8, 9, fmt, args);
  74286. + va_end(args);
  74287. +
  74288. + memcpy(buffer + wptr, text, 16);
  74289. + wptr = (wptr + 16) % sizeof(buffer);
  74290. + }
  74291. +}
  74292. +
  74293. +/**
  74294. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  74295. + * @channel: channel to re-enable
  74296. + */
  74297. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  74298. +{
  74299. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  74300. +
  74301. + hcchar.b.chen = 0;
  74302. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  74303. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  74304. + /* Hardware bug workaround: update the ssplit index */
  74305. + if (st->channel[n].hcsplt_copy.b.spltena)
  74306. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  74307. +
  74308. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  74309. + }
  74310. +
  74311. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  74312. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  74313. + hcchar.b.chen = 1;
  74314. +
  74315. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  74316. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  74317. +}
  74318. +
  74319. +/**
  74320. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  74321. + * @st: Pointer to the channel's state
  74322. + * @n : channel number
  74323. + *
  74324. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  74325. + * endpoint direction, set control regs up correctly.
  74326. + */
  74327. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  74328. +{
  74329. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  74330. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  74331. +
  74332. + hcsplt.b.compsplt = 1;
  74333. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  74334. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  74335. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  74336. + } else {
  74337. + // If OUT, the CSPLIT result contains handshake only.
  74338. + hctsiz.b.xfersize = 0;
  74339. + }
  74340. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  74341. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  74342. + mb();
  74343. +}
  74344. +
  74345. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  74346. +{
  74347. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  74348. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  74349. +
  74350. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  74351. + return st->channel[n].hctsiz_copy.b.xfersize;
  74352. + } else {
  74353. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  74354. + }
  74355. +
  74356. +}
  74357. +
  74358. +
  74359. +/**
  74360. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  74361. + *
  74362. + * Of use only for IN periodic transfers.
  74363. + */
  74364. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  74365. +{
  74366. + hcdma_data_t hcdma;
  74367. + int i = st->channel[n].dma_info.index;
  74368. + int len;
  74369. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  74370. +
  74371. + len = fiq_get_xfer_len(st, n);
  74372. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  74373. + st->channel[n].dma_info.slot_len[i] = len;
  74374. + i++;
  74375. + if (i > 6)
  74376. + BUG();
  74377. +
  74378. + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
  74379. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  74380. + st->channel[n].dma_info.index = i;
  74381. + return 0;
  74382. +}
  74383. +
  74384. +/**
  74385. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  74386. + */
  74387. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  74388. +{
  74389. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  74390. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  74391. + hctsiz.b.pktcnt = 1;
  74392. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  74393. +}
  74394. +
  74395. +/**
  74396. + * fiq_iso_out_advance() - update DMA address and split position bits
  74397. + * for isochronous OUT transactions.
  74398. + *
  74399. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  74400. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  74401. + *
  74402. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  74403. + */
  74404. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  74405. +{
  74406. + hcsplt_data_t hcsplt;
  74407. + hctsiz_data_t hctsiz;
  74408. + hcdma_data_t hcdma;
  74409. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  74410. + int last = 0;
  74411. + int i = st->channel[n].dma_info.index;
  74412. +
  74413. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  74414. + i++;
  74415. + if (i == 4)
  74416. + last = 1;
  74417. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  74418. + last = 1;
  74419. +
  74420. + /* New DMA address - address of bounce buffer referred to in index */
  74421. + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
  74422. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
  74423. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  74424. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  74425. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  74426. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  74427. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  74428. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  74429. + /* Set up new packet length */
  74430. + hctsiz.b.pktcnt = 1;
  74431. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  74432. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  74433. +
  74434. + st->channel[n].dma_info.index++;
  74435. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  74436. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  74437. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  74438. + return last;
  74439. +}
  74440. +
  74441. +/**
  74442. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  74443. + *
  74444. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  74445. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  74446. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  74447. + * is very unlikely that filling the start-split FIFO will cause data loss.
  74448. + * This allows much better interleaving of transactions in an order-independent way-
  74449. + * there is no requirement to prioritise isochronous, just a state-space search has
  74450. + * to be performed on each periodic start-split complete interrupt.
  74451. + */
  74452. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  74453. +{
  74454. + int hub_addr = st->channel[n].hub_addr;
  74455. + int port_addr = st->channel[n].port_addr;
  74456. + int i, poked = 0;
  74457. + for (i = 0; i < num_channels; i++) {
  74458. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  74459. + continue;
  74460. + if (st->channel[i].hub_addr == hub_addr &&
  74461. + st->channel[i].port_addr == port_addr) {
  74462. + switch (st->channel[i].fsm) {
  74463. + case FIQ_PER_ISO_OUT_PENDING:
  74464. + if (st->channel[i].nrpackets == 1) {
  74465. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  74466. + } else {
  74467. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  74468. + }
  74469. + fiq_fsm_restart_channel(st, i, 0);
  74470. + poked = 1;
  74471. + break;
  74472. +
  74473. + default:
  74474. + break;
  74475. + }
  74476. + }
  74477. + if (poked)
  74478. + break;
  74479. + }
  74480. + return poked;
  74481. +}
  74482. +
  74483. +/**
  74484. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  74485. + * @n: Channel to use as reference
  74486. + *
  74487. + */
  74488. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  74489. +{
  74490. + int hub_addr = st->channel[n].hub_addr;
  74491. + int port_addr = st->channel[n].port_addr;
  74492. + int i, in_use = 0;
  74493. + for (i = 0; i < num_channels; i++) {
  74494. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  74495. + continue;
  74496. + switch (st->channel[i].fsm) {
  74497. + /* TT is reserved for channels that are in the middle of a periodic
  74498. + * split transaction.
  74499. + */
  74500. + case FIQ_PER_SSPLIT_STARTED:
  74501. + case FIQ_PER_CSPLIT_WAIT:
  74502. + case FIQ_PER_CSPLIT_NYET1:
  74503. + //case FIQ_PER_CSPLIT_POLL:
  74504. + case FIQ_PER_ISO_OUT_ACTIVE:
  74505. + case FIQ_PER_ISO_OUT_LAST:
  74506. + if (st->channel[i].hub_addr == hub_addr &&
  74507. + st->channel[i].port_addr == port_addr) {
  74508. + in_use = 1;
  74509. + }
  74510. + break;
  74511. + default:
  74512. + break;
  74513. + }
  74514. + if (in_use)
  74515. + break;
  74516. + }
  74517. + return in_use;
  74518. +}
  74519. +
  74520. +/**
  74521. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  74522. + * to be issued for this IN transaction.
  74523. + *
  74524. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  74525. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  74526. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  74527. + * size, but for endpoints that give variable-length data then we have to resort
  74528. + * to heuristics.
  74529. + *
  74530. + * We also return whether this is the last CSPLIT to be queued, again based on
  74531. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  74532. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  74533. + */
  74534. +
  74535. +/*
  74536. + * We need some way of guaranteeing if a returned periodic packet of size X
  74537. + * has a DATA0 PID.
  74538. + * The heuristic value of 144 bytes assumes that the received data has maximal
  74539. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  74540. + * permissible limit. If the transfer length results in a final packet size
  74541. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  74542. + * Also used to ensure that an endpoint will nominally only return a single
  74543. + * complete-split worth of data.
  74544. + */
  74545. +#define DATA0_PID_HEURISTIC 144
  74546. +
  74547. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  74548. +{
  74549. +
  74550. + int i;
  74551. + int total_len = 0;
  74552. + int more_needed = 1;
  74553. + struct fiq_channel_state *st = &state->channel[n];
  74554. +
  74555. + for (i = 0; i < st->dma_info.index; i++) {
  74556. + total_len += st->dma_info.slot_len[i];
  74557. + }
  74558. +
  74559. + *probably_last = 0;
  74560. +
  74561. + if (st->hcchar_copy.b.eptype == 0x3) {
  74562. + /*
  74563. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  74564. + * then this is definitely the last CSPLIT.
  74565. + */
  74566. + *probably_last = 1;
  74567. + } else {
  74568. + /* Isoc IN. This is a bit risky if we are the first transaction:
  74569. + * we may have been held off slightly. */
  74570. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  74571. + more_needed = 0;
  74572. + }
  74573. + /* If in the next uframe we will receive enough data to fill the endpoint,
  74574. + * then only issue 1 more csplit.
  74575. + */
  74576. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  74577. + *probably_last = 1;
  74578. + }
  74579. +
  74580. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  74581. + i == 6 || total_len == 0)
  74582. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  74583. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  74584. + * - in these extreme cases we will pass through a truncated packet.
  74585. + */
  74586. + more_needed = 0;
  74587. +
  74588. + return more_needed;
  74589. +}
  74590. +
  74591. +/**
  74592. + * fiq_fsm_too_late() - Test transaction for lateness
  74593. + *
  74594. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  74595. + * the hub will disable the port to the device and respond with ERR handshakes.
  74596. + * The hub status endpoint will not reflect this change.
  74597. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  74598. + */
  74599. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  74600. +{
  74601. + int uframe;
  74602. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  74603. + uframe = hfnum.b.frnum & 0x7;
  74604. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  74605. + return 1;
  74606. + } else {
  74607. + return 0;
  74608. + }
  74609. +}
  74610. +
  74611. +
  74612. +/**
  74613. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  74614. + *
  74615. + * Search pending transactions in the start-split pending state and queue them.
  74616. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  74617. + * Note: we specifically don't do isochronous OUT transactions first because better
  74618. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  74619. + */
  74620. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  74621. +{
  74622. + int n;
  74623. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  74624. + if ((hfnum.b.frnum & 0x7) == 5)
  74625. + return;
  74626. + for (n = 0; n < num_channels; n++) {
  74627. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  74628. + /* Check to see if any other transactions are using this TT */
  74629. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  74630. + if (!fiq_fsm_too_late(st, n)) {
  74631. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  74632. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  74633. + fiq_fsm_restart_channel(st, n, 0);
  74634. + } else {
  74635. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  74636. + }
  74637. + break;
  74638. + }
  74639. + }
  74640. + }
  74641. + for (n = 0; n < num_channels; n++) {
  74642. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  74643. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  74644. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  74645. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  74646. + fiq_fsm_restart_channel(st, n, 0);
  74647. + break;
  74648. + }
  74649. + }
  74650. + }
  74651. +}
  74652. +
  74653. +/**
  74654. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  74655. + * @state: Pointer to fiq_state
  74656. + * @n: Channel transaction is active on
  74657. + * @hcint: Copy of host channel interrupt register
  74658. + *
  74659. + * Returns 0 if there are no more transactions for this HC to do, 1
  74660. + * otherwise.
  74661. + */
  74662. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  74663. +{
  74664. + struct fiq_channel_state *st = &state->channel[n];
  74665. + int xfer_len = 0, nrpackets = 0;
  74666. + hcdma_data_t hcdma;
  74667. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  74668. +
  74669. + xfer_len = fiq_get_xfer_len(state, n);
  74670. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  74671. +
  74672. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  74673. +
  74674. + st->hs_isoc_info.index++;
  74675. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  74676. + return 0;
  74677. + }
  74678. +
  74679. + /* grab the next DMA address offset from the array */
  74680. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  74681. + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  74682. +
  74683. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  74684. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  74685. + * this is always set to the maximum size of the endpoint. */
  74686. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  74687. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  74688. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  74689. + if (nrpackets == 0)
  74690. + nrpackets = 1;
  74691. + st->hcchar_copy.b.multicnt = nrpackets;
  74692. + st->hctsiz_copy.b.pktcnt = nrpackets;
  74693. +
  74694. + /* Initial PID also needs to be set */
  74695. + if (st->hcchar_copy.b.epdir == 0) {
  74696. + st->hctsiz_copy.b.xfersize = xfer_len;
  74697. + switch (st->hcchar_copy.b.multicnt) {
  74698. + case 1:
  74699. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  74700. + break;
  74701. + case 2:
  74702. + case 3:
  74703. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  74704. + break;
  74705. + }
  74706. +
  74707. + } else {
  74708. + switch (st->hcchar_copy.b.multicnt) {
  74709. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  74710. + case 1:
  74711. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  74712. + break;
  74713. + case 2:
  74714. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  74715. + break;
  74716. + case 3:
  74717. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  74718. + break;
  74719. + }
  74720. + }
  74721. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  74722. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  74723. + /* Channel is enabled on hcint handler exit */
  74724. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  74725. + return 1;
  74726. +}
  74727. +
  74728. +
  74729. +/**
  74730. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  74731. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  74732. + * @num_channels: set according to the DWC hardware configuration
  74733. + *
  74734. + * The SOF handler in FSM mode has two functions
  74735. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  74736. + * nothing to do
  74737. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  74738. + * of holdoff.
  74739. + *
  74740. + * The second part is architecture-specific to mach-bcm2835 -
  74741. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  74742. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  74743. + * number (USB) can be enabled. This means that certain parts of the USB specification
  74744. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  74745. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  74746. + * the SOF "timer" (125uS) to perform this task.
  74747. + */
  74748. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  74749. +{
  74750. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  74751. + int n;
  74752. + int kick_irq = 0;
  74753. +
  74754. + if ((hfnum.b.frnum & 0x7) == 1) {
  74755. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  74756. + * Check to see if there are any transactions that are stale.
  74757. + * Boot them out.
  74758. + */
  74759. + for (n = 0; n < num_channels; n++) {
  74760. + switch (state->channel[n].fsm) {
  74761. + case FIQ_PER_CSPLIT_WAIT:
  74762. + case FIQ_PER_CSPLIT_NYET1:
  74763. + case FIQ_PER_CSPLIT_POLL:
  74764. + case FIQ_PER_CSPLIT_LAST:
  74765. + /* Check if we are no longer in the same full-speed frame. */
  74766. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  74767. + (hfnum.b.frnum & ~0x7))
  74768. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  74769. + break;
  74770. + default:
  74771. + break;
  74772. + }
  74773. + }
  74774. + }
  74775. +
  74776. + for (n = 0; n < num_channels; n++) {
  74777. + switch (state->channel[n].fsm) {
  74778. +
  74779. + case FIQ_NP_SSPLIT_RETRY:
  74780. + case FIQ_NP_IN_CSPLIT_RETRY:
  74781. + case FIQ_NP_OUT_CSPLIT_RETRY:
  74782. + fiq_fsm_restart_channel(state, n, 0);
  74783. + break;
  74784. +
  74785. + case FIQ_HS_ISOC_SLEEPING:
  74786. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  74787. + fiq_fsm_restart_channel(state, n, 0);
  74788. + break;
  74789. +
  74790. + case FIQ_PER_SSPLIT_QUEUED:
  74791. + if ((hfnum.b.frnum & 0x7) == 5)
  74792. + break;
  74793. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  74794. + if (!fiq_fsm_too_late(state, n)) {
  74795. + fiq_print(FIQDBG_INT, st, "SOF GO %01d", n);
  74796. + fiq_fsm_restart_channel(state, n, 0);
  74797. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  74798. + } else {
  74799. + /* Transaction cannot be started without risking a device babble error */
  74800. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  74801. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  74802. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  74803. + kick_irq |= 1;
  74804. + }
  74805. + }
  74806. + break;
  74807. +
  74808. + case FIQ_PER_ISO_OUT_PENDING:
  74809. + /* Ordinarily, this should be poked after the SSPLIT
  74810. + * complete interrupt for a competing transfer on the same
  74811. + * TT. Doesn't happen for aborted transactions though.
  74812. + */
  74813. + if ((hfnum.b.frnum & 0x7) >= 5)
  74814. + break;
  74815. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  74816. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  74817. + * that caused this.
  74818. + */
  74819. + fiq_fsm_restart_channel(state, n, 0);
  74820. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  74821. + if (state->channel[n].nrpackets == 1) {
  74822. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  74823. + } else {
  74824. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  74825. + }
  74826. + }
  74827. + break;
  74828. +
  74829. + case FIQ_PER_CSPLIT_WAIT:
  74830. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  74831. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  74832. + * will utterly bugger this up though.
  74833. + */
  74834. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  74835. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  74836. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  74837. + fiq_fsm_restart_channel(state, n, 0);
  74838. + fiq_fsm_start_next_periodic(state, num_channels);
  74839. +
  74840. + }
  74841. + break;
  74842. +
  74843. + case FIQ_PER_SPLIT_TIMEOUT:
  74844. + case FIQ_DEQUEUE_ISSUED:
  74845. + /* Ugly: we have to force a HCD interrupt.
  74846. + * Poke the mask for the channel in question.
  74847. + * We will take a fake SOF because of this, but
  74848. + * that's OK.
  74849. + */
  74850. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  74851. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  74852. + kick_irq |= 1;
  74853. + break;
  74854. +
  74855. + default:
  74856. + break;
  74857. + }
  74858. + }
  74859. +
  74860. + if (state->kick_np_queues ||
  74861. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  74862. + kick_irq |= 1;
  74863. +
  74864. + return !kick_irq;
  74865. +}
  74866. +
  74867. +
  74868. +/**
  74869. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  74870. + * @state: Pointer to the FIQ state struct
  74871. + * @num_channels: Number of channels as per hardware config
  74872. + * @n: channel for which HAINT(i) was raised
  74873. + *
  74874. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  74875. + */
  74876. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  74877. +{
  74878. + hcint_data_t hcint;
  74879. + hcintmsk_data_t hcintmsk;
  74880. + hcint_data_t hcint_probe;
  74881. + hcchar_data_t hcchar;
  74882. + int handled = 0;
  74883. + int restart = 0;
  74884. + int last_csplit = 0;
  74885. + int start_next_periodic = 0;
  74886. + struct fiq_channel_state *st = &state->channel[n];
  74887. + hfnum_data_t hfnum;
  74888. +
  74889. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  74890. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  74891. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  74892. +
  74893. + if (st->fsm != FIQ_PASSTHROUGH) {
  74894. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  74895. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  74896. + }
  74897. +
  74898. + switch (st->fsm) {
  74899. +
  74900. + case FIQ_PASSTHROUGH:
  74901. + case FIQ_DEQUEUE_ISSUED:
  74902. + /* doesn't belong to us, kick it upstairs */
  74903. + break;
  74904. +
  74905. + case FIQ_PASSTHROUGH_ERRORSTATE:
  74906. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  74907. + * Several interrupts are unmasked if a previous transaction failed - it's
  74908. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  74909. + * Emulate what the HCD does in this situation: mask and continue.
  74910. + * The FSM has no other state setup so this has to be handled out-of-band.
  74911. + */
  74912. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  74913. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  74914. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  74915. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  74916. + * interrupt, after the device has disappeared.
  74917. + */
  74918. + if (!hcint.b.xacterr)
  74919. + st->nr_errors = 0;
  74920. + hcintmsk.b.nak = 0;
  74921. + hcintmsk.b.ack = 0;
  74922. + hcintmsk.b.datatglerr = 0;
  74923. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  74924. + return 1;
  74925. + }
  74926. + if (hcint_probe.b.chhltd) {
  74927. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  74928. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  74929. + return 0;
  74930. + }
  74931. + break;
  74932. +
  74933. + /* Non-periodic state groups */
  74934. + case FIQ_NP_SSPLIT_STARTED:
  74935. + case FIQ_NP_SSPLIT_RETRY:
  74936. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  74937. + if (hcint.b.ack) {
  74938. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  74939. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  74940. + */
  74941. + if(st->hcchar_copy.b.epdir == 1)
  74942. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  74943. + else
  74944. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  74945. + st->nr_errors = 0;
  74946. + handled = 1;
  74947. + fiq_fsm_setup_csplit(state, n);
  74948. + } else if (hcint.b.nak) {
  74949. + // No buffer space in TT. Retry on a uframe boundary.
  74950. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  74951. + handled = 1;
  74952. + } else if (hcint.b.xacterr) {
  74953. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  74954. + st->nr_errors++;
  74955. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  74956. + if (st->nr_errors >= 3) {
  74957. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  74958. + } else {
  74959. + handled = 1;
  74960. + restart = 1;
  74961. + }
  74962. + } else {
  74963. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  74964. + handled = 0;
  74965. + restart = 0;
  74966. + }
  74967. + break;
  74968. +
  74969. + case FIQ_NP_IN_CSPLIT_RETRY:
  74970. + /* Received a CSPLIT done interrupt.
  74971. + * Expected Data/NAK/STALL/NYET for IN.
  74972. + */
  74973. + if (hcint.b.xfercomp) {
  74974. + /* For IN, data is present. */
  74975. + st->fsm = FIQ_NP_SPLIT_DONE;
  74976. + } else if (hcint.b.nak) {
  74977. + /* no endpoint data. Punt it upstairs */
  74978. + st->fsm = FIQ_NP_SPLIT_DONE;
  74979. + } else if (hcint.b.nyet) {
  74980. + /* CSPLIT NYET - retry on a uframe boundary. */
  74981. + handled = 1;
  74982. + st->nr_errors = 0;
  74983. + } else if (hcint.b.datatglerr) {
  74984. + /* data toggle errors do not set the xfercomp bit. */
  74985. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  74986. + } else if (hcint.b.xacterr) {
  74987. + /* HS error. Retry immediate */
  74988. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  74989. + st->nr_errors++;
  74990. + if (st->nr_errors >= 3) {
  74991. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  74992. + } else {
  74993. + handled = 1;
  74994. + restart = 1;
  74995. + }
  74996. + } else if (hcint.b.stall || hcint.b.bblerr) {
  74997. + /* A STALL implies either a LS bus error or a genuine STALL. */
  74998. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  74999. + } else {
  75000. + /* Hardware bug. It's possible in some cases to
  75001. + * get a channel halt with nothing else set when
  75002. + * the response was a NYET. Treat as local 3-strikes retry.
  75003. + */
  75004. + hcint_data_t hcint_test = hcint;
  75005. + hcint_test.b.chhltd = 0;
  75006. + if (!hcint_test.d32) {
  75007. + st->nr_errors++;
  75008. + if (st->nr_errors >= 3) {
  75009. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75010. + } else {
  75011. + handled = 1;
  75012. + }
  75013. + } else {
  75014. + /* Bail out if something unexpected happened */
  75015. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75016. + }
  75017. + }
  75018. + break;
  75019. +
  75020. + case FIQ_NP_OUT_CSPLIT_RETRY:
  75021. + /* Received a CSPLIT done interrupt.
  75022. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  75023. + if (hcint.b.xfercomp) {
  75024. + st->fsm = FIQ_NP_SPLIT_DONE;
  75025. + } else if (hcint.b.nak) {
  75026. + // The HCD will implement the holdoff on frame boundaries.
  75027. + st->fsm = FIQ_NP_SPLIT_DONE;
  75028. + } else if (hcint.b.nyet) {
  75029. + // Hub still processing.
  75030. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  75031. + handled = 1;
  75032. + st->nr_errors = 0;
  75033. + //restart = 1;
  75034. + } else if (hcint.b.xacterr) {
  75035. + /* HS error. retry immediate */
  75036. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  75037. + st->nr_errors++;
  75038. + if (st->nr_errors >= 3) {
  75039. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75040. + } else {
  75041. + handled = 1;
  75042. + restart = 1;
  75043. + }
  75044. + } else if (hcint.b.stall) {
  75045. + /* LS bus error or genuine stall */
  75046. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  75047. + } else {
  75048. + /*
  75049. + * Hardware bug. It's possible in some cases to get a
  75050. + * channel halt with nothing else set when the response was a NYET.
  75051. + * Treat as local 3-strikes retry.
  75052. + */
  75053. + hcint_data_t hcint_test = hcint;
  75054. + hcint_test.b.chhltd = 0;
  75055. + if (!hcint_test.d32) {
  75056. + st->nr_errors++;
  75057. + if (st->nr_errors >= 3) {
  75058. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75059. + } else {
  75060. + handled = 1;
  75061. + }
  75062. + } else {
  75063. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  75064. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75065. + }
  75066. + }
  75067. + break;
  75068. +
  75069. + /* Periodic split states (except isoc out) */
  75070. + case FIQ_PER_SSPLIT_STARTED:
  75071. + /* Expect an ACK or failure for SSPLIT */
  75072. + if (hcint.b.ack) {
  75073. + /*
  75074. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  75075. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  75076. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  75077. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  75078. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  75079. + * coincident with SOF for n+1.
  75080. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  75081. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  75082. + * State machine workaround.
  75083. + */
  75084. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  75085. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  75086. + fiq_fsm_setup_csplit(state, n);
  75087. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  75088. + * time. If not, then we're in the next SOF.
  75089. + */
  75090. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  75091. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  75092. + st->expected_uframe = hfnum.b.frnum;
  75093. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  75094. + } else {
  75095. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  75096. + /* For isochronous IN endpoints,
  75097. + * we need to hold off if we are expecting a lot of data */
  75098. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  75099. + start_next_periodic = 1;
  75100. + }
  75101. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  75102. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  75103. + * lag. Unmask the NYET interrupt.
  75104. + */
  75105. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  75106. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  75107. + restart = 1;
  75108. + }
  75109. + handled = 1;
  75110. + } else if (hcint.b.xacterr) {
  75111. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  75112. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  75113. + start_next_periodic = 1;
  75114. + } else {
  75115. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  75116. + start_next_periodic = 1;
  75117. + }
  75118. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  75119. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  75120. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  75121. + }
  75122. + break;
  75123. +
  75124. + case FIQ_PER_CSPLIT_NYET1:
  75125. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  75126. + * we are too late and the TT has dropped its CSPLIT fifo.
  75127. + */
  75128. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  75129. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  75130. + start_next_periodic = 1;
  75131. + if (hcint.b.nak) {
  75132. + st->fsm = FIQ_PER_SPLIT_DONE;
  75133. + } else if (hcint.b.xfercomp) {
  75134. + fiq_increment_dma_buf(state, num_channels, n);
  75135. + st->fsm = FIQ_PER_CSPLIT_POLL;
  75136. + st->nr_errors = 0;
  75137. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  75138. + handled = 1;
  75139. + restart = 1;
  75140. + if (!last_csplit)
  75141. + start_next_periodic = 0;
  75142. + } else {
  75143. + st->fsm = FIQ_PER_SPLIT_DONE;
  75144. + }
  75145. + } else if (hcint.b.nyet) {
  75146. + /* Doh. Data lost. */
  75147. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  75148. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  75149. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  75150. + } else {
  75151. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  75152. + }
  75153. + break;
  75154. +
  75155. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  75156. + /*
  75157. + * we got here because our host channel is in the delayed-interrupt
  75158. + * state and we cannot take a NYET interrupt any later than when it
  75159. + * occurred. Disable then re-enable the channel if this happens to force
  75160. + * CSPLITs to occur at the right time.
  75161. + */
  75162. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  75163. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  75164. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  75165. + if (hcint.b.nak) {
  75166. + st->fsm = FIQ_PER_SPLIT_DONE;
  75167. + start_next_periodic = 1;
  75168. + } else if (hcint.b.xfercomp) {
  75169. + fiq_increment_dma_buf(state, num_channels, n);
  75170. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  75171. + st->fsm = FIQ_PER_CSPLIT_POLL;
  75172. + handled = 1;
  75173. + restart = 1;
  75174. + start_next_periodic = 1;
  75175. + /* Reload HCTSIZ for the next transfer */
  75176. + fiq_fsm_reload_hctsiz(state, n);
  75177. + if (!last_csplit)
  75178. + start_next_periodic = 0;
  75179. + } else {
  75180. + st->fsm = FIQ_PER_SPLIT_DONE;
  75181. + }
  75182. + } else if (hcint.b.nyet) {
  75183. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  75184. + start_next_periodic = 1;
  75185. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  75186. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  75187. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  75188. + } else {
  75189. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  75190. + }
  75191. + break;
  75192. +
  75193. + case FIQ_PER_CSPLIT_POLL:
  75194. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  75195. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  75196. + start_next_periodic = 1;
  75197. + if (hcint.b.nak) {
  75198. + st->fsm = FIQ_PER_SPLIT_DONE;
  75199. + } else if (hcint.b.xfercomp) {
  75200. + fiq_increment_dma_buf(state, num_channels, n);
  75201. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  75202. + handled = 1;
  75203. + restart = 1;
  75204. + /* Reload HCTSIZ for the next transfer */
  75205. + fiq_fsm_reload_hctsiz(state, n);
  75206. + if (!last_csplit)
  75207. + start_next_periodic = 0;
  75208. + } else {
  75209. + st->fsm = FIQ_PER_SPLIT_DONE;
  75210. + }
  75211. + } else if (hcint.b.nyet) {
  75212. + /* Are we a NYET after the first data packet? */
  75213. + if (st->nrpackets == 0) {
  75214. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  75215. + handled = 1;
  75216. + restart = 1;
  75217. + } else {
  75218. + /* We got a NYET when polling CSPLITs. Can happen
  75219. + * if our heuristic fails, or if someone disables us
  75220. + * for any significant length of time.
  75221. + */
  75222. + if (st->nr_errors >= 3) {
  75223. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  75224. + } else {
  75225. + st->fsm = FIQ_PER_SPLIT_DONE;
  75226. + }
  75227. + }
  75228. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  75229. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  75230. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  75231. + } else {
  75232. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  75233. + }
  75234. + break;
  75235. +
  75236. + case FIQ_HS_ISOC_TURBO:
  75237. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  75238. + /* more transactions to come */
  75239. + handled = 1;
  75240. + restart = 1;
  75241. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  75242. + } else {
  75243. + st->fsm = FIQ_HS_ISOC_DONE;
  75244. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  75245. + }
  75246. + break;
  75247. +
  75248. + case FIQ_HS_ISOC_ABORTED:
  75249. + /* This abort is called by the driver rewriting the state mid-transaction
  75250. + * which allows the dequeue mechanism to work more effectively.
  75251. + */
  75252. + break;
  75253. +
  75254. + case FIQ_PER_ISO_OUT_ACTIVE:
  75255. + if (hcint.b.ack) {
  75256. + if(fiq_iso_out_advance(state, num_channels, n)) {
  75257. + /* last OUT transfer */
  75258. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  75259. + /*
  75260. + * Assuming the periodic FIFO in the dwc core
  75261. + * actually does its job properly, we can queue
  75262. + * the next ssplit now and in theory, the wire
  75263. + * transactions will be in-order.
  75264. + */
  75265. + // No it doesn't. It appears to process requests in host channel order.
  75266. + //start_next_periodic = 1;
  75267. + }
  75268. + handled = 1;
  75269. + restart = 1;
  75270. + } else {
  75271. + /*
  75272. + * Isochronous transactions carry on regardless. Log the error
  75273. + * and continue.
  75274. + */
  75275. + //explode += 1;
  75276. + st->nr_errors++;
  75277. + if(fiq_iso_out_advance(state, num_channels, n)) {
  75278. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  75279. + //start_next_periodic = 1;
  75280. + }
  75281. + handled = 1;
  75282. + restart = 1;
  75283. + }
  75284. + break;
  75285. +
  75286. + case FIQ_PER_ISO_OUT_LAST:
  75287. + if (hcint.b.ack) {
  75288. + /* All done here */
  75289. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  75290. + } else {
  75291. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  75292. + st->nr_errors++;
  75293. + }
  75294. + start_next_periodic = 1;
  75295. + break;
  75296. +
  75297. + case FIQ_PER_SPLIT_TIMEOUT:
  75298. + /* SOF kicked us because we overran. */
  75299. + start_next_periodic = 1;
  75300. + break;
  75301. +
  75302. + default:
  75303. + break;
  75304. + }
  75305. +
  75306. + if (handled) {
  75307. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  75308. + } else {
  75309. + /* Copy the regs into the state so the IRQ knows what to do */
  75310. + st->hcint_copy.d32 = hcint.d32;
  75311. + }
  75312. +
  75313. + if (restart) {
  75314. + /* Restart always implies handled. */
  75315. + if (restart == 2) {
  75316. + /* For complete-split INs, the show must go on.
  75317. + * Force a channel restart */
  75318. + fiq_fsm_restart_channel(state, n, 1);
  75319. + } else {
  75320. + fiq_fsm_restart_channel(state, n, 0);
  75321. + }
  75322. + }
  75323. + if (start_next_periodic) {
  75324. + fiq_fsm_start_next_periodic(state, num_channels);
  75325. + }
  75326. + if (st->fsm != FIQ_PASSTHROUGH)
  75327. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  75328. +
  75329. + return handled;
  75330. +}
  75331. +
  75332. +
  75333. +/**
  75334. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  75335. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  75336. + * @num_channels: set according to the DWC hardware configuration
  75337. + * @dma: pointer to DMA bounce buffers for split transaction slots
  75338. + *
  75339. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  75340. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  75341. + * interrupts each and every time a split transaction packet is received or sent successfully.
  75342. + * This results in either an interrupt storm when everything is working "properly", or
  75343. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  75344. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  75345. + * solves these problems.
  75346. + *
  75347. + * Return: void
  75348. + */
  75349. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  75350. +{
  75351. + gintsts_data_t gintsts, gintsts_handled;
  75352. + gintmsk_data_t gintmsk;
  75353. + //hfnum_data_t hfnum;
  75354. + haint_data_t haint, haint_handled;
  75355. + haintmsk_data_t haintmsk;
  75356. + int kick_irq = 0;
  75357. +
  75358. + gintsts_handled.d32 = 0;
  75359. + haint_handled.d32 = 0;
  75360. +
  75361. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  75362. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  75363. + gintsts.d32 &= gintmsk.d32;
  75364. +
  75365. + if (gintsts.b.sofintr) {
  75366. + /* For FSM mode, SOF is required to keep the state machine advance for
  75367. + * certain stages of the periodic pipeline. It's death to mask this
  75368. + * interrupt in that case.
  75369. + */
  75370. +
  75371. + if (!fiq_fsm_do_sof(state, num_channels)) {
  75372. + /* Kick IRQ once. Queue advancement means that all pending transactions
  75373. + * will get serviced when the IRQ finally executes.
  75374. + */
  75375. + if (state->gintmsk_saved.b.sofintr == 1)
  75376. + kick_irq |= 1;
  75377. + state->gintmsk_saved.b.sofintr = 0;
  75378. + }
  75379. + gintsts_handled.b.sofintr = 1;
  75380. + }
  75381. +
  75382. + if (gintsts.b.hcintr) {
  75383. + int i;
  75384. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  75385. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  75386. + haint.d32 &= haintmsk.d32;
  75387. + haint_handled.d32 = 0;
  75388. + for (i=0; i<num_channels; i++) {
  75389. + if (haint.b2.chint & (1 << i)) {
  75390. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  75391. + /* HCINT was not handled in FIQ
  75392. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  75393. + * Mask HAINT(i) but keep top-level hcint unmasked.
  75394. + */
  75395. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  75396. + } else {
  75397. + /* do_hcintr cleaned up after itself, but clear haint */
  75398. + haint_handled.b2.chint |= (1 << i);
  75399. + }
  75400. + }
  75401. + }
  75402. +
  75403. + if (haint_handled.b2.chint) {
  75404. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  75405. + }
  75406. +
  75407. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  75408. + /*
  75409. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  75410. + * where interrupts are held off and HCINTs start to pile up.
  75411. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  75412. + * masked.
  75413. + */
  75414. + haintmsk.d32 &= state->haintmsk_saved.d32;
  75415. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  75416. + kick_irq |= 1;
  75417. + }
  75418. + /* Top-Level interrupt - always handled because it's level-sensitive */
  75419. + gintsts_handled.b.hcintr = 1;
  75420. + }
  75421. +
  75422. +
  75423. + /* Clear the bits in the saved register that were not handled but were triggered. */
  75424. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  75425. +
  75426. + /* FIQ didn't handle something - mask has changed - write new mask */
  75427. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  75428. + gintmsk.d32 &= state->gintmsk_saved.d32;
  75429. + gintmsk.b.sofintr = 1;
  75430. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  75431. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  75432. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  75433. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  75434. + kick_irq |= 1;
  75435. + }
  75436. +
  75437. + if (gintsts_handled.d32) {
  75438. + /* Only applies to edge-sensitive bits in GINTSTS */
  75439. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  75440. + }
  75441. +
  75442. + /* We got an interrupt, didn't handle it. */
  75443. + if (kick_irq) {
  75444. + state->mphi_int_count++;
  75445. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  75446. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  75447. +
  75448. + }
  75449. + state->fiq_done++;
  75450. + mb();
  75451. +}
  75452. +
  75453. +
  75454. +/**
  75455. + * dwc_otg_fiq_nop() - FIQ "lite"
  75456. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  75457. + *
  75458. + * The "nop" handler does not intervene on any interrupts other than SOF.
  75459. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  75460. + * with non-periodic/periodic queues) needs to be kicked.
  75461. + *
  75462. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  75463. + *
  75464. + * Return: void
  75465. + */
  75466. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  75467. +{
  75468. + gintsts_data_t gintsts, gintsts_handled;
  75469. + gintmsk_data_t gintmsk;
  75470. + hfnum_data_t hfnum;
  75471. +
  75472. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  75473. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  75474. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  75475. + gintsts.d32 &= gintmsk.d32;
  75476. + gintsts_handled.d32 = 0;
  75477. +
  75478. + if (gintsts.b.sofintr) {
  75479. + if (!state->kick_np_queues &&
  75480. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  75481. + /* SOF handled, no work to do, just ACK interrupt */
  75482. + gintsts_handled.b.sofintr = 1;
  75483. + } else {
  75484. + /* Kick IRQ */
  75485. + state->gintmsk_saved.b.sofintr = 0;
  75486. + }
  75487. + }
  75488. +
  75489. + /* Reset handled interrupts */
  75490. + if(gintsts_handled.d32) {
  75491. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  75492. + }
  75493. +
  75494. + /* Clear the bits in the saved register that were not handled but were triggered. */
  75495. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  75496. +
  75497. + /* We got an interrupt, didn't handle it and want to mask it */
  75498. + if (~(state->gintmsk_saved.d32)) {
  75499. + state->mphi_int_count++;
  75500. + gintmsk.d32 &= state->gintmsk_saved.d32;
  75501. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  75502. + /* Force a clear before another dummy send */
  75503. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  75504. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  75505. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  75506. +
  75507. + }
  75508. + state->fiq_done++;
  75509. + mb();
  75510. +}
  75511. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  75512. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h 1969-12-31 18:00:00.000000000 -0600
  75513. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h 2014-12-11 14:02:55.396418001 -0600
  75514. @@ -0,0 +1,353 @@
  75515. +/*
  75516. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  75517. + *
  75518. + * Copyright (c) 2013 Raspberry Pi Foundation
  75519. + *
  75520. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  75521. + * All rights reserved.
  75522. + *
  75523. + * Redistribution and use in source and binary forms, with or without
  75524. + * modification, are permitted provided that the following conditions are met:
  75525. + * * Redistributions of source code must retain the above copyright
  75526. + * notice, this list of conditions and the following disclaimer.
  75527. + * * Redistributions in binary form must reproduce the above copyright
  75528. + * notice, this list of conditions and the following disclaimer in the
  75529. + * documentation and/or other materials provided with the distribution.
  75530. + * * Neither the name of Raspberry Pi nor the
  75531. + * names of its contributors may be used to endorse or promote products
  75532. + * derived from this software without specific prior written permission.
  75533. + *
  75534. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  75535. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  75536. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  75537. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  75538. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  75539. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  75540. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  75541. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  75542. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  75543. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  75544. + *
  75545. + * This FIQ implements functionality that performs split transactions on
  75546. + * the dwc_otg hardware without any outside intervention. A split transaction
  75547. + * is "queued" by nominating a specific host channel to perform the entirety
  75548. + * of a split transaction. This FIQ will then perform the microframe-precise
  75549. + * scheduling required in each phase of the transaction until completion.
  75550. + *
  75551. + * The FIQ functionality has been surgically implanted into the Synopsys
  75552. + * vendor-provided driver.
  75553. + *
  75554. + */
  75555. +
  75556. +#ifndef DWC_OTG_FIQ_FSM_H_
  75557. +#define DWC_OTG_FIQ_FSM_H_
  75558. +
  75559. +#include "dwc_otg_regs.h"
  75560. +#include "dwc_otg_cil.h"
  75561. +#include "dwc_otg_hcd.h"
  75562. +#include <linux/kernel.h>
  75563. +#include <linux/irqflags.h>
  75564. +#include <linux/string.h>
  75565. +#include <asm/barrier.h>
  75566. +
  75567. +#if 0
  75568. +#define FLAME_ON(x) \
  75569. +do { \
  75570. + int gpioreg; \
  75571. + \
  75572. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  75573. + gpioreg &= ~(7 << (x-20)*3); \
  75574. + gpioreg |= 0x1 << (x-20)*3; \
  75575. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  75576. + \
  75577. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  75578. +} while (0)
  75579. +
  75580. +#define FLAME_OFF(x) \
  75581. +do { \
  75582. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  75583. +} while (0)
  75584. +#else
  75585. +#define FLAME_ON(x) do { } while (0)
  75586. +#define FLAME_OFF(X) do { } while (0)
  75587. +#endif
  75588. +
  75589. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  75590. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  75591. + * reads and writes are executed in-order therefore the need for memory barriers
  75592. + * is obviated if we're only talking to USB.
  75593. + */
  75594. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  75595. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  75596. +
  75597. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  75598. +#define GINTSTS 0x014
  75599. +#define GINTMSK 0x018
  75600. +/* Debug register. Poll the top of the received packets FIFO. */
  75601. +#define GRXSTSR 0x01C
  75602. +#define HFNUM 0x408
  75603. +#define HAINT 0x414
  75604. +#define HAINTMSK 0x418
  75605. +#define HPRT0 0x440
  75606. +
  75607. +/* HC_regs start from an offset of 0x500 */
  75608. +#define HC_START 0x500
  75609. +#define HC_OFFSET 0x020
  75610. +
  75611. +#define HC_DMA 0x514
  75612. +
  75613. +#define HCCHAR 0x00
  75614. +#define HCSPLT 0x04
  75615. +#define HCINT 0x08
  75616. +#define HCINTMSK 0x0C
  75617. +#define HCTSIZ 0x10
  75618. +
  75619. +#define ISOC_XACTPOS_ALL 0b11
  75620. +#define ISOC_XACTPOS_BEGIN 0b10
  75621. +#define ISOC_XACTPOS_MID 0b00
  75622. +#define ISOC_XACTPOS_END 0b01
  75623. +
  75624. +#define DWC_PID_DATA2 0b01
  75625. +#define DWC_PID_MDATA 0b11
  75626. +#define DWC_PID_DATA1 0b10
  75627. +#define DWC_PID_DATA0 0b00
  75628. +
  75629. +typedef struct {
  75630. + volatile void* base;
  75631. + volatile void* ctrl;
  75632. + volatile void* outdda;
  75633. + volatile void* outddb;
  75634. + volatile void* intstat;
  75635. +} mphi_regs_t;
  75636. +
  75637. +
  75638. +enum fiq_debug_level {
  75639. + FIQDBG_SCHED = (1 << 0),
  75640. + FIQDBG_INT = (1 << 1),
  75641. + FIQDBG_ERR = (1 << 2),
  75642. + FIQDBG_PORTHUB = (1 << 3),
  75643. +};
  75644. +
  75645. +struct fiq_state;
  75646. +
  75647. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  75648. +#if 0
  75649. +#define fiq_print _fiq_print
  75650. +#else
  75651. +#define fiq_print(x, y, ...)
  75652. +#endif
  75653. +
  75654. +extern bool fiq_enable, fiq_fsm_enable;
  75655. +extern ushort nak_holdoff;
  75656. +
  75657. +/**
  75658. + * enum fiq_fsm_state - The FIQ FSM states.
  75659. + *
  75660. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  75661. + * USB2.0 specification for host responses to various transaction states.
  75662. + * There are modifications to this host state machine because of a variety of
  75663. + * quirks and limitations in the dwc_otg hardware.
  75664. + *
  75665. + * The fsm state is also used to communicate back to the driver on completion of
  75666. + * a split transaction. The end states are used in conjunction with the interrupts
  75667. + * raised by the final transaction.
  75668. + */
  75669. +enum fiq_fsm_state {
  75670. + /* FIQ isn't enabled for this host channel */
  75671. + FIQ_PASSTHROUGH = 0,
  75672. + /* For the first interrupt received for this channel,
  75673. + * the FIQ has to ack any interrupts indicating success. */
  75674. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  75675. + /* Nonperiodic state groups */
  75676. + FIQ_NP_SSPLIT_STARTED = 1,
  75677. + FIQ_NP_SSPLIT_RETRY = 2,
  75678. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  75679. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  75680. + FIQ_NP_SPLIT_DONE = 5,
  75681. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  75682. + /* This differentiates a HS transaction error from a LS one
  75683. + * (handling the hub state is different) */
  75684. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  75685. +
  75686. + /* Periodic state groups */
  75687. + /* Periodic transactions are either started directly by the IRQ handler
  75688. + * or deferred if the TT is already in use.
  75689. + */
  75690. + FIQ_PER_SSPLIT_QUEUED = 8,
  75691. + FIQ_PER_SSPLIT_STARTED = 9,
  75692. + FIQ_PER_SSPLIT_LAST = 10,
  75693. +
  75694. +
  75695. + FIQ_PER_ISO_OUT_PENDING = 11,
  75696. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  75697. + FIQ_PER_ISO_OUT_LAST = 13,
  75698. + FIQ_PER_ISO_OUT_DONE = 27,
  75699. +
  75700. + FIQ_PER_CSPLIT_WAIT = 14,
  75701. + FIQ_PER_CSPLIT_NYET1 = 15,
  75702. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  75703. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  75704. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  75705. + FIQ_PER_CSPLIT_POLL = 16,
  75706. + /* The last CSPLIT for a transaction has been issued, differentiates
  75707. + * for the state machine to queue the next packet.
  75708. + */
  75709. + FIQ_PER_CSPLIT_LAST = 17,
  75710. +
  75711. + FIQ_PER_SPLIT_DONE = 18,
  75712. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  75713. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  75714. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  75715. + /* Frame rollover has occurred without the transaction finishing. */
  75716. + FIQ_PER_SPLIT_TIMEOUT = 22,
  75717. +
  75718. + /* FIQ-accelerated HS Isochronous state groups */
  75719. + FIQ_HS_ISOC_TURBO = 23,
  75720. + /* For interval > 1, SOF wakes up the isochronous FSM */
  75721. + FIQ_HS_ISOC_SLEEPING = 24,
  75722. + FIQ_HS_ISOC_DONE = 25,
  75723. + FIQ_HS_ISOC_ABORTED = 26,
  75724. + FIQ_DEQUEUE_ISSUED = 30,
  75725. + FIQ_TEST = 32,
  75726. +};
  75727. +
  75728. +struct fiq_stack {
  75729. + int magic1;
  75730. + uint8_t stack[2048];
  75731. + int magic2;
  75732. +};
  75733. +
  75734. +
  75735. +/**
  75736. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  75737. + * @index: Number of slots reported used for IN transactions / number of slots
  75738. + * transmitted for an OUT transaction
  75739. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  75740. + *
  75741. + * Split transaction transfers can have variable length depending on other bus
  75742. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  75743. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  75744. + * can happen per-frame.
  75745. + */
  75746. +struct fiq_dma_info {
  75747. + u8 index;
  75748. + u8 slot_len[6];
  75749. +};
  75750. +
  75751. +struct __attribute__((packed)) fiq_split_dma_slot {
  75752. + u8 buf[188];
  75753. +};
  75754. +
  75755. +struct fiq_dma_channel {
  75756. + struct __attribute__((packed)) fiq_split_dma_slot index[6];
  75757. +};
  75758. +
  75759. +struct fiq_dma_blob {
  75760. + struct __attribute__((packed)) fiq_dma_channel channel[0];
  75761. +};
  75762. +
  75763. +/**
  75764. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  75765. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  75766. + * @nrframes: Total length of iso_frame_desc array
  75767. + * @index: Current index (FIQ-maintained)
  75768. + *
  75769. + */
  75770. +struct fiq_hs_isoc_info {
  75771. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  75772. + unsigned int nrframes;
  75773. + unsigned int index;
  75774. +};
  75775. +
  75776. +/**
  75777. + * struct fiq_channel_state - FIQ state machine storage
  75778. + * @fsm: Current state of the channel as understood by the FIQ
  75779. + * @nr_errors: Number of transaction errors on this split-transaction
  75780. + * @hub_addr: SSPLIT/CSPLIT destination hub
  75781. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  75782. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  75783. + * split-IN, number of CSPLIT data packets that were received.
  75784. + * @hcchar_copy:
  75785. + * @hcsplt_copy:
  75786. + * @hcintmsk_copy:
  75787. + * @hctsiz_copy: Copies of the host channel registers.
  75788. + * For use as scratch, or for returning state.
  75789. + *
  75790. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  75791. + * FSM state is stored here. Members of this structure must only be set up by the
  75792. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  75793. + * has updated the state to either a COMPLETE state group or ABORT state group.
  75794. + */
  75795. +
  75796. +struct fiq_channel_state {
  75797. + enum fiq_fsm_state fsm;
  75798. + unsigned int nr_errors;
  75799. + unsigned int hub_addr;
  75800. + unsigned int port_addr;
  75801. + /* Hardware bug workaround: sometimes channel halt interrupts are
  75802. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  75803. + unsigned int expected_uframe;
  75804. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  75805. + unsigned int nrpackets;
  75806. + struct fiq_dma_info dma_info;
  75807. + struct fiq_hs_isoc_info hs_isoc_info;
  75808. + /* Copies of HC registers - in/out communication from/to IRQ handler
  75809. + * and for ease of channel setup. A bit of mungeing is performed - for
  75810. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  75811. + */
  75812. + hcchar_data_t hcchar_copy;
  75813. + hcsplt_data_t hcsplt_copy;
  75814. + hcint_data_t hcint_copy;
  75815. + hcintmsk_data_t hcintmsk_copy;
  75816. + hctsiz_data_t hctsiz_copy;
  75817. + hcdma_data_t hcdma_copy;
  75818. +};
  75819. +
  75820. +/**
  75821. + * struct fiq_state - top-level FIQ state machine storage
  75822. + * @mphi_regs: virtual address of the MPHI peripheral register file
  75823. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  75824. + * @dma_base: physical address for the base of the DMA bounce buffers
  75825. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  75826. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  75827. + * Used for determining which interrupts fired to set off the IRQ handler.
  75828. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  75829. + * @np_count: Non-periodic transactions in the active queue
  75830. + * @np_sent: Count of non-periodic transactions that have completed
  75831. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  75832. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  75833. + * passing SOF through to the driver until necessary.
  75834. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  75835. + * channels configured into the core logic.
  75836. + *
  75837. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  75838. + * It contains top-level state information.
  75839. + */
  75840. +struct fiq_state {
  75841. + mphi_regs_t mphi_regs;
  75842. + void *dwc_regs_base;
  75843. + dma_addr_t dma_base;
  75844. + struct fiq_dma_blob *fiq_dmab;
  75845. + void *dummy_send;
  75846. + gintmsk_data_t gintmsk_saved;
  75847. + haintmsk_data_t haintmsk_saved;
  75848. + int mphi_int_count;
  75849. + unsigned int fiq_done;
  75850. + unsigned int kick_np_queues;
  75851. + unsigned int next_sched_frame;
  75852. +#ifdef FIQ_DEBUG
  75853. + char * buffer;
  75854. + unsigned int bufsiz;
  75855. +#endif
  75856. + struct fiq_channel_state channel[0];
  75857. +};
  75858. +
  75859. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  75860. +
  75861. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  75862. +
  75863. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  75864. +
  75865. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  75866. +
  75867. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  75868. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  75869. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S 1969-12-31 18:00:00.000000000 -0600
  75870. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S 2014-12-11 14:02:55.396418001 -0600
  75871. @@ -0,0 +1,81 @@
  75872. +/*
  75873. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  75874. + *
  75875. + * Copyright (c) 2013 Raspberry Pi Foundation
  75876. + *
  75877. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  75878. + * All rights reserved.
  75879. + *
  75880. + * Redistribution and use in source and binary forms, with or without
  75881. + * modification, are permitted provided that the following conditions are met:
  75882. + * * Redistributions of source code must retain the above copyright
  75883. + * notice, this list of conditions and the following disclaimer.
  75884. + * * Redistributions in binary form must reproduce the above copyright
  75885. + * notice, this list of conditions and the following disclaimer in the
  75886. + * documentation and/or other materials provided with the distribution.
  75887. + * * Neither the name of Raspberry Pi nor the
  75888. + * names of its contributors may be used to endorse or promote products
  75889. + * derived from this software without specific prior written permission.
  75890. + *
  75891. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  75892. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  75893. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  75894. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  75895. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  75896. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  75897. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  75898. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  75899. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  75900. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  75901. + */
  75902. +
  75903. +
  75904. +#include <asm/assembler.h>
  75905. +#include <linux/linkage.h>
  75906. +
  75907. +
  75908. +.text
  75909. +
  75910. +.global _dwc_otg_fiq_stub_end;
  75911. +
  75912. +/**
  75913. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  75914. + * a C-style function call with arguments from the FIQ banked registers.
  75915. + * r0 = &hcd->fiq_state
  75916. + * r1 = &hcd->num_channels
  75917. + * r2 = &hcd->dma_buffers
  75918. + * Tramples: r0, r1, r2, r4, fp, ip
  75919. + */
  75920. +
  75921. +ENTRY(_dwc_otg_fiq_stub)
  75922. + /* Stash unbanked regs - SP will have been set up for us */
  75923. + mov ip, sp;
  75924. + stmdb sp!, {r0-r12, lr};
  75925. +#ifdef FIQ_DEBUG
  75926. + // Cycle profiling - read cycle counter at start
  75927. + mrc p15, 0, r5, c15, c12, 1;
  75928. +#endif
  75929. + /* r11 = fp, don't trample it */
  75930. + mov r4, fp;
  75931. + /* set EABI frame size */
  75932. + sub fp, ip, #512;
  75933. +
  75934. + /* for fiq NOP mode - just need state */
  75935. + mov r0, r8;
  75936. + /* r9 = num_channels */
  75937. + mov r1, r9;
  75938. + /* r10 = struct *dma_bufs */
  75939. +// mov r2, r10;
  75940. +
  75941. + /* r4 = &fiq_c_function */
  75942. + blx r4;
  75943. +#ifdef FIQ_DEBUG
  75944. + mrc p15, 0, r4, c15, c12, 1;
  75945. + subs r5, r5, r4;
  75946. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  75947. +#endif
  75948. + ldmia sp!, {r0-r12, lr};
  75949. + subs pc, lr, #4;
  75950. +_dwc_otg_fiq_stub_end:
  75951. +END(_dwc_otg_fiq_stub)
  75952. +
  75953. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  75954. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 1969-12-31 18:00:00.000000000 -0600
  75955. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 2014-12-11 14:05:39.524418001 -0600
  75956. @@ -0,0 +1,4217 @@
  75957. +
  75958. +/* ==========================================================================
  75959. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  75960. + * $Revision: #104 $
  75961. + * $Date: 2011/10/24 $
  75962. + * $Change: 1871159 $
  75963. + *
  75964. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  75965. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  75966. + * otherwise expressly agreed to in writing between Synopsys and you.
  75967. + *
  75968. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  75969. + * any End User Software License Agreement or Agreement for Licensed Product
  75970. + * with Synopsys or any supplement thereto. You are permitted to use and
  75971. + * redistribute this Software in source and binary forms, with or without
  75972. + * modification, provided that redistributions of source code must retain this
  75973. + * notice. You may not view, use, disclose, copy or distribute this file or
  75974. + * any information contained herein except pursuant to this license grant from
  75975. + * Synopsys. If you do not agree with this notice, including the disclaimer
  75976. + * below, then you are not authorized to use the Software.
  75977. + *
  75978. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  75979. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75980. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  75981. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  75982. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  75983. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  75984. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75985. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  75986. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  75987. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  75988. + * DAMAGE.
  75989. + * ========================================================================== */
  75990. +#ifndef DWC_DEVICE_ONLY
  75991. +
  75992. +/** @file
  75993. + * This file implements HCD Core. All code in this file is portable and doesn't
  75994. + * use any OS specific functions.
  75995. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  75996. + * header file.
  75997. + */
  75998. +
  75999. +#include <linux/usb.h>
  76000. +#include <linux/usb/hcd.h>
  76001. +
  76002. +#include "dwc_otg_hcd.h"
  76003. +#include "dwc_otg_regs.h"
  76004. +#include "dwc_otg_fiq_fsm.h"
  76005. +
  76006. +extern bool microframe_schedule;
  76007. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  76008. +
  76009. +//#define DEBUG_HOST_CHANNELS
  76010. +#ifdef DEBUG_HOST_CHANNELS
  76011. +static int last_sel_trans_num_per_scheduled = 0;
  76012. +static int last_sel_trans_num_nonper_scheduled = 0;
  76013. +static int last_sel_trans_num_avail_hc_at_start = 0;
  76014. +static int last_sel_trans_num_avail_hc_at_end = 0;
  76015. +#endif /* DEBUG_HOST_CHANNELS */
  76016. +
  76017. +
  76018. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  76019. +{
  76020. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  76021. +}
  76022. +
  76023. +/**
  76024. + * Connection timeout function. An OTG host is required to display a
  76025. + * message if the device does not connect within 10 seconds.
  76026. + */
  76027. +void dwc_otg_hcd_connect_timeout(void *ptr)
  76028. +{
  76029. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  76030. + DWC_PRINTF("Connect Timeout\n");
  76031. + __DWC_ERROR("Device Not Connected/Responding\n");
  76032. +}
  76033. +
  76034. +#if defined(DEBUG)
  76035. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  76036. +{
  76037. + if (qh->channel != NULL) {
  76038. + dwc_hc_t *hc = qh->channel;
  76039. + dwc_list_link_t *item;
  76040. + dwc_otg_qh_t *qh_item;
  76041. + int num_channels = hcd->core_if->core_params->host_channels;
  76042. + int i;
  76043. +
  76044. + dwc_otg_hc_regs_t *hc_regs;
  76045. + hcchar_data_t hcchar;
  76046. + hcsplt_data_t hcsplt;
  76047. + hctsiz_data_t hctsiz;
  76048. + uint32_t hcdma;
  76049. +
  76050. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  76051. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76052. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76053. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76054. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  76055. +
  76056. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  76057. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  76058. + hcsplt.d32);
  76059. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  76060. + hcdma);
  76061. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  76062. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  76063. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  76064. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  76065. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  76066. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  76067. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  76068. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  76069. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  76070. + DWC_PRINTF(" qh: %p\n", hc->qh);
  76071. + DWC_PRINTF(" NP inactive sched:\n");
  76072. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  76073. + qh_item =
  76074. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  76075. + DWC_PRINTF(" %p\n", qh_item);
  76076. + }
  76077. + DWC_PRINTF(" NP active sched:\n");
  76078. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  76079. + qh_item =
  76080. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  76081. + DWC_PRINTF(" %p\n", qh_item);
  76082. + }
  76083. + DWC_PRINTF(" Channels: \n");
  76084. + for (i = 0; i < num_channels; i++) {
  76085. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  76086. + DWC_PRINTF(" %2d: %p\n", i, hc);
  76087. + }
  76088. + }
  76089. +}
  76090. +#else
  76091. +#define dump_channel_info(hcd, qh)
  76092. +#endif /* DEBUG */
  76093. +
  76094. +/**
  76095. + * Work queue function for starting the HCD when A-Cable is connected.
  76096. + * The hcd_start() must be called in a process context.
  76097. + */
  76098. +static void hcd_start_func(void *_vp)
  76099. +{
  76100. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  76101. +
  76102. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  76103. + if (hcd) {
  76104. + hcd->fops->start(hcd);
  76105. + }
  76106. +}
  76107. +
  76108. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  76109. +{
  76110. +#ifdef DEBUG
  76111. + int i;
  76112. + int num_channels = hcd->core_if->core_params->host_channels;
  76113. + for (i = 0; i < num_channels; i++) {
  76114. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  76115. + }
  76116. +#endif
  76117. +}
  76118. +
  76119. +static void del_timers(dwc_otg_hcd_t * hcd)
  76120. +{
  76121. + del_xfer_timers(hcd);
  76122. + DWC_TIMER_CANCEL(hcd->conn_timer);
  76123. +}
  76124. +
  76125. +/**
  76126. + * Processes all the URBs in a single list of QHs. Completes them with
  76127. + * -ESHUTDOWN and frees the QTD.
  76128. + */
  76129. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  76130. +{
  76131. + dwc_list_link_t *qh_item, *qh_tmp;
  76132. + dwc_otg_qh_t *qh;
  76133. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  76134. +
  76135. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  76136. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  76137. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  76138. + &qh->qtd_list, qtd_list_entry) {
  76139. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  76140. + if (qtd->urb != NULL) {
  76141. + hcd->fops->complete(hcd, qtd->urb->priv,
  76142. + qtd->urb, -DWC_E_SHUTDOWN);
  76143. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  76144. + }
  76145. +
  76146. + }
  76147. + if(qh->channel) {
  76148. + /* Using hcchar.chen == 1 is not a reliable test.
  76149. + * It is possible that the channel has already halted
  76150. + * but not yet been through the IRQ handler.
  76151. + */
  76152. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  76153. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  76154. + if(microframe_schedule)
  76155. + hcd->available_host_channels++;
  76156. + qh->channel = NULL;
  76157. + }
  76158. + dwc_otg_hcd_qh_remove(hcd, qh);
  76159. + }
  76160. +}
  76161. +
  76162. +/**
  76163. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  76164. + * and periodic schedules. The QTD associated with each URB is removed from
  76165. + * the schedule and freed. This function may be called when a disconnect is
  76166. + * detected or when the HCD is being stopped.
  76167. + */
  76168. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  76169. +{
  76170. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  76171. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  76172. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  76173. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  76174. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  76175. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  76176. +}
  76177. +
  76178. +/**
  76179. + * Start the connection timer. An OTG host is required to display a
  76180. + * message if the device does not connect within 10 seconds. The
  76181. + * timer is deleted if a port connect interrupt occurs before the
  76182. + * timer expires.
  76183. + */
  76184. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  76185. +{
  76186. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  76187. +}
  76188. +
  76189. +/**
  76190. + * HCD Callback function for disconnect of the HCD.
  76191. + *
  76192. + * @param p void pointer to the <code>struct usb_hcd</code>
  76193. + */
  76194. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  76195. +{
  76196. + dwc_otg_hcd_t *dwc_otg_hcd;
  76197. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  76198. + dwc_otg_hcd = p;
  76199. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  76200. + return 1;
  76201. +}
  76202. +
  76203. +/**
  76204. + * HCD Callback function for starting the HCD when A-Cable is
  76205. + * connected.
  76206. + *
  76207. + * @param p void pointer to the <code>struct usb_hcd</code>
  76208. + */
  76209. +static int32_t dwc_otg_hcd_start_cb(void *p)
  76210. +{
  76211. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  76212. + dwc_otg_core_if_t *core_if;
  76213. + hprt0_data_t hprt0;
  76214. +
  76215. + core_if = dwc_otg_hcd->core_if;
  76216. +
  76217. + if (core_if->op_state == B_HOST) {
  76218. + /*
  76219. + * Reset the port. During a HNP mode switch the reset
  76220. + * needs to occur within 1ms and have a duration of at
  76221. + * least 50ms.
  76222. + */
  76223. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  76224. + hprt0.b.prtrst = 1;
  76225. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  76226. + }
  76227. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  76228. + hcd_start_func, dwc_otg_hcd, 50,
  76229. + "start hcd");
  76230. +
  76231. + return 1;
  76232. +}
  76233. +
  76234. +/**
  76235. + * HCD Callback function for disconnect of the HCD.
  76236. + *
  76237. + * @param p void pointer to the <code>struct usb_hcd</code>
  76238. + */
  76239. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  76240. +{
  76241. + gintsts_data_t intr;
  76242. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  76243. +
  76244. + /*
  76245. + * Set status flags for the hub driver.
  76246. + */
  76247. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  76248. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  76249. + if(fiq_enable)
  76250. + local_fiq_disable();
  76251. + /*
  76252. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  76253. + * interrupt mask and status bits and disabling subsequent host
  76254. + * channel interrupts.
  76255. + */
  76256. + intr.d32 = 0;
  76257. + intr.b.nptxfempty = 1;
  76258. + intr.b.ptxfempty = 1;
  76259. + intr.b.hcintr = 1;
  76260. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  76261. + intr.d32, 0);
  76262. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  76263. + intr.d32, 0);
  76264. +
  76265. + del_timers(dwc_otg_hcd);
  76266. +
  76267. + /*
  76268. + * Turn off the vbus power only if the core has transitioned to device
  76269. + * mode. If still in host mode, need to keep power on to detect a
  76270. + * reconnection.
  76271. + */
  76272. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  76273. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  76274. + hprt0_data_t hprt0 = {.d32 = 0 };
  76275. + DWC_PRINTF("Disconnect: PortPower off\n");
  76276. + hprt0.b.prtpwr = 0;
  76277. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  76278. + hprt0.d32);
  76279. + }
  76280. +
  76281. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  76282. + }
  76283. +
  76284. + /* Respond with an error status to all URBs in the schedule. */
  76285. + kill_all_urbs(dwc_otg_hcd);
  76286. +
  76287. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  76288. + /* Clean up any host channels that were in use. */
  76289. + int num_channels;
  76290. + int i;
  76291. + dwc_hc_t *channel;
  76292. + dwc_otg_hc_regs_t *hc_regs;
  76293. + hcchar_data_t hcchar;
  76294. +
  76295. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  76296. +
  76297. + if (!dwc_otg_hcd->core_if->dma_enable) {
  76298. + /* Flush out any channel requests in slave mode. */
  76299. + for (i = 0; i < num_channels; i++) {
  76300. + channel = dwc_otg_hcd->hc_ptr_array[i];
  76301. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  76302. + (channel, hc_list_entry)) {
  76303. + hc_regs =
  76304. + dwc_otg_hcd->core_if->
  76305. + host_if->hc_regs[i];
  76306. + hcchar.d32 =
  76307. + DWC_READ_REG32(&hc_regs->hcchar);
  76308. + if (hcchar.b.chen) {
  76309. + hcchar.b.chen = 0;
  76310. + hcchar.b.chdis = 1;
  76311. + hcchar.b.epdir = 0;
  76312. + DWC_WRITE_REG32
  76313. + (&hc_regs->hcchar,
  76314. + hcchar.d32);
  76315. + }
  76316. + }
  76317. + }
  76318. + }
  76319. +
  76320. + for (i = 0; i < num_channels; i++) {
  76321. + channel = dwc_otg_hcd->hc_ptr_array[i];
  76322. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  76323. + hc_regs =
  76324. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  76325. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76326. + if (hcchar.b.chen) {
  76327. + /* Halt the channel. */
  76328. + hcchar.b.chdis = 1;
  76329. + DWC_WRITE_REG32(&hc_regs->hcchar,
  76330. + hcchar.d32);
  76331. + }
  76332. +
  76333. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  76334. + channel);
  76335. + DWC_CIRCLEQ_INSERT_TAIL
  76336. + (&dwc_otg_hcd->free_hc_list, channel,
  76337. + hc_list_entry);
  76338. + /*
  76339. + * Added for Descriptor DMA to prevent channel double cleanup
  76340. + * in release_channel_ddma(). Which called from ep_disable
  76341. + * when device disconnect.
  76342. + */
  76343. + channel->qh = NULL;
  76344. + }
  76345. + }
  76346. + if(fiq_fsm_enable) {
  76347. + for(i=0; i < 128; i++) {
  76348. + dwc_otg_hcd->hub_port[i] = 0;
  76349. + }
  76350. + }
  76351. +
  76352. + }
  76353. +
  76354. + if(fiq_enable)
  76355. + local_fiq_enable();
  76356. +
  76357. + if (dwc_otg_hcd->fops->disconnect) {
  76358. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  76359. + }
  76360. +
  76361. + return 1;
  76362. +}
  76363. +
  76364. +/**
  76365. + * HCD Callback function for stopping the HCD.
  76366. + *
  76367. + * @param p void pointer to the <code>struct usb_hcd</code>
  76368. + */
  76369. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  76370. +{
  76371. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  76372. +
  76373. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  76374. + dwc_otg_hcd_stop(dwc_otg_hcd);
  76375. + return 1;
  76376. +}
  76377. +
  76378. +#ifdef CONFIG_USB_DWC_OTG_LPM
  76379. +/**
  76380. + * HCD Callback function for sleep of HCD.
  76381. + *
  76382. + * @param p void pointer to the <code>struct usb_hcd</code>
  76383. + */
  76384. +static int dwc_otg_hcd_sleep_cb(void *p)
  76385. +{
  76386. + dwc_otg_hcd_t *hcd = p;
  76387. +
  76388. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  76389. +
  76390. + return 0;
  76391. +}
  76392. +#endif
  76393. +
  76394. +
  76395. +/**
  76396. + * HCD Callback function for Remote Wakeup.
  76397. + *
  76398. + * @param p void pointer to the <code>struct usb_hcd</code>
  76399. + */
  76400. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  76401. +{
  76402. + dwc_otg_hcd_t *hcd = p;
  76403. +
  76404. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  76405. + hcd->flags.b.port_suspend_change = 1;
  76406. + }
  76407. +#ifdef CONFIG_USB_DWC_OTG_LPM
  76408. + else {
  76409. + hcd->flags.b.port_l1_change = 1;
  76410. + }
  76411. +#endif
  76412. + return 0;
  76413. +}
  76414. +
  76415. +/**
  76416. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  76417. + * stopped.
  76418. + */
  76419. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  76420. +{
  76421. + hprt0_data_t hprt0 = {.d32 = 0 };
  76422. +
  76423. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  76424. +
  76425. + /*
  76426. + * The root hub should be disconnected before this function is called.
  76427. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  76428. + * and the QH lists (via ..._hcd_endpoint_disable).
  76429. + */
  76430. +
  76431. + /* Turn off all host-specific interrupts. */
  76432. + dwc_otg_disable_host_interrupts(hcd->core_if);
  76433. +
  76434. + /* Turn off the vbus power */
  76435. + DWC_PRINTF("PortPower off\n");
  76436. + hprt0.b.prtpwr = 0;
  76437. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  76438. + dwc_mdelay(1);
  76439. +}
  76440. +
  76441. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  76442. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  76443. + int atomic_alloc)
  76444. +{
  76445. + int retval = 0;
  76446. + uint8_t needs_scheduling = 0;
  76447. + dwc_otg_transaction_type_e tr_type;
  76448. + dwc_otg_qtd_t *qtd;
  76449. + gintmsk_data_t intr_mask = {.d32 = 0 };
  76450. + hprt0_data_t hprt0 = { .d32 = 0 };
  76451. +
  76452. +#ifdef DEBUG /* integrity checks (Broadcom) */
  76453. + if (NULL == hcd->core_if) {
  76454. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  76455. + /* No longer connected. */
  76456. + return -DWC_E_INVALID;
  76457. + }
  76458. +#endif
  76459. + if (!hcd->flags.b.port_connect_status) {
  76460. + /* No longer connected. */
  76461. + DWC_ERROR("Not connected\n");
  76462. + return -DWC_E_NO_DEVICE;
  76463. + }
  76464. +
  76465. + /* Some core configurations cannot support LS traffic on a FS root port */
  76466. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  76467. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  76468. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  76469. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  76470. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  76471. + return -DWC_E_NO_DEVICE;
  76472. + }
  76473. + }
  76474. +
  76475. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  76476. + if (qtd == NULL) {
  76477. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  76478. + return -DWC_E_NO_MEMORY;
  76479. + }
  76480. +#ifdef DEBUG /* integrity checks (Broadcom) */
  76481. + if (qtd->urb == NULL) {
  76482. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  76483. + return -DWC_E_NO_MEMORY;
  76484. + }
  76485. + if (qtd->urb->priv == NULL) {
  76486. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  76487. + return -DWC_E_NO_MEMORY;
  76488. + }
  76489. +#endif
  76490. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  76491. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  76492. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  76493. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  76494. + needs_scheduling = 0;
  76495. +
  76496. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  76497. + // creates a new queue in ep_handle if it doesn't exist already
  76498. + if (retval < 0) {
  76499. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  76500. + "Error status %d\n", retval);
  76501. + dwc_otg_hcd_qtd_free(qtd);
  76502. + return retval;
  76503. + }
  76504. +
  76505. + if(needs_scheduling) {
  76506. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  76507. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  76508. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  76509. + }
  76510. + }
  76511. + return retval;
  76512. +}
  76513. +
  76514. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  76515. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  76516. +{
  76517. + dwc_otg_qh_t *qh;
  76518. + dwc_otg_qtd_t *urb_qtd;
  76519. + BUG_ON(!hcd);
  76520. + BUG_ON(!dwc_otg_urb);
  76521. +
  76522. +#ifdef DEBUG /* integrity checks (Broadcom) */
  76523. +
  76524. + if (hcd == NULL) {
  76525. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  76526. + return -DWC_E_INVALID;
  76527. + }
  76528. + if (dwc_otg_urb == NULL) {
  76529. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  76530. + return -DWC_E_INVALID;
  76531. + }
  76532. + if (dwc_otg_urb->qtd == NULL) {
  76533. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  76534. + return -DWC_E_INVALID;
  76535. + }
  76536. + urb_qtd = dwc_otg_urb->qtd;
  76537. + BUG_ON(!urb_qtd);
  76538. + if (urb_qtd->qh == NULL) {
  76539. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  76540. + return -DWC_E_INVALID;
  76541. + }
  76542. +#else
  76543. + urb_qtd = dwc_otg_urb->qtd;
  76544. + BUG_ON(!urb_qtd);
  76545. +#endif
  76546. + qh = urb_qtd->qh;
  76547. + BUG_ON(!qh);
  76548. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  76549. + if (urb_qtd->in_process) {
  76550. + dump_channel_info(hcd, qh);
  76551. + }
  76552. + }
  76553. +#ifdef DEBUG /* integrity checks (Broadcom) */
  76554. + if (hcd->core_if == NULL) {
  76555. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  76556. + return -DWC_E_INVALID;
  76557. + }
  76558. +#endif
  76559. + if (urb_qtd->in_process && qh->channel) {
  76560. + /* The QTD is in process (it has been assigned to a channel). */
  76561. + if (hcd->flags.b.port_connect_status) {
  76562. + int n = qh->channel->hc_num;
  76563. + /*
  76564. + * If still connected (i.e. in host mode), halt the
  76565. + * channel so it can be used for other transfers. If
  76566. + * no longer connected, the host registers can't be
  76567. + * written to halt the channel since the core is in
  76568. + * device mode.
  76569. + */
  76570. + /* In FIQ FSM mode, we need to shut down carefully.
  76571. + * The FIQ may attempt to restart a disabled channel */
  76572. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  76573. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  76574. + qh->channel->halt_pending = 1;
  76575. + hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
  76576. + } else {
  76577. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  76578. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  76579. + }
  76580. + }
  76581. + }
  76582. +
  76583. + /*
  76584. + * Free the QTD and clean up the associated QH. Leave the QH in the
  76585. + * schedule if it has any remaining QTDs.
  76586. + */
  76587. +
  76588. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  76589. + "delete %sQueue handler\n",
  76590. + hcd->core_if->dma_desc_enable?"DMA ":"");
  76591. + if (!hcd->core_if->dma_desc_enable) {
  76592. + uint8_t b = urb_qtd->in_process;
  76593. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  76594. + if (b) {
  76595. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  76596. + qh->channel = NULL;
  76597. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  76598. + dwc_otg_hcd_qh_remove(hcd, qh);
  76599. + }
  76600. + } else {
  76601. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  76602. + }
  76603. + return 0;
  76604. +}
  76605. +
  76606. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  76607. + int retry)
  76608. +{
  76609. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  76610. + int retval = 0;
  76611. + dwc_irqflags_t flags;
  76612. +
  76613. + if (retry < 0) {
  76614. + retval = -DWC_E_INVALID;
  76615. + goto done;
  76616. + }
  76617. +
  76618. + if (!qh) {
  76619. + retval = -DWC_E_INVALID;
  76620. + goto done;
  76621. + }
  76622. +
  76623. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  76624. +
  76625. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  76626. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  76627. + retry--;
  76628. + dwc_msleep(5);
  76629. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  76630. + }
  76631. +
  76632. + dwc_otg_hcd_qh_remove(hcd, qh);
  76633. +
  76634. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  76635. + /*
  76636. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  76637. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  76638. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  76639. + * and dwc_otg_hcd_frame_list_alloc().
  76640. + */
  76641. + dwc_otg_hcd_qh_free(hcd, qh);
  76642. +
  76643. +done:
  76644. + return retval;
  76645. +}
  76646. +
  76647. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  76648. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  76649. +{
  76650. + int retval = 0;
  76651. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  76652. + if (!qh)
  76653. + return -DWC_E_INVALID;
  76654. +
  76655. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  76656. + return retval;
  76657. +}
  76658. +#endif
  76659. +
  76660. +/**
  76661. + * HCD Callback structure for handling mode switching.
  76662. + */
  76663. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  76664. + .start = dwc_otg_hcd_start_cb,
  76665. + .stop = dwc_otg_hcd_stop_cb,
  76666. + .disconnect = dwc_otg_hcd_disconnect_cb,
  76667. + .session_start = dwc_otg_hcd_session_start_cb,
  76668. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  76669. +#ifdef CONFIG_USB_DWC_OTG_LPM
  76670. + .sleep = dwc_otg_hcd_sleep_cb,
  76671. +#endif
  76672. + .p = 0,
  76673. +};
  76674. +
  76675. +/**
  76676. + * Reset tasklet function
  76677. + */
  76678. +static void reset_tasklet_func(void *data)
  76679. +{
  76680. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  76681. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  76682. + hprt0_data_t hprt0;
  76683. +
  76684. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  76685. +
  76686. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  76687. + hprt0.b.prtrst = 1;
  76688. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  76689. + dwc_mdelay(60);
  76690. +
  76691. + hprt0.b.prtrst = 0;
  76692. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  76693. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  76694. +}
  76695. +
  76696. +static void completion_tasklet_func(void *ptr)
  76697. +{
  76698. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  76699. + struct urb *urb;
  76700. + urb_tq_entry_t *item;
  76701. + dwc_irqflags_t flags;
  76702. +
  76703. + /* This could just be spin_lock_irq */
  76704. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  76705. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  76706. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  76707. + urb = item->urb;
  76708. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  76709. + urb_tq_entries);
  76710. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  76711. + DWC_FREE(item);
  76712. +
  76713. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  76714. +
  76715. +
  76716. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  76717. + }
  76718. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  76719. + return;
  76720. +}
  76721. +
  76722. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  76723. +{
  76724. + dwc_list_link_t *item;
  76725. + dwc_otg_qh_t *qh;
  76726. + dwc_irqflags_t flags;
  76727. +
  76728. + if (!qh_list->next) {
  76729. + /* The list hasn't been initialized yet. */
  76730. + return;
  76731. + }
  76732. + /*
  76733. + * Hold spinlock here. Not needed in that case if bellow
  76734. + * function is being called from ISR
  76735. + */
  76736. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  76737. + /* Ensure there are no QTDs or URBs left. */
  76738. + kill_urbs_in_qh_list(hcd, qh_list);
  76739. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  76740. +
  76741. + DWC_LIST_FOREACH(item, qh_list) {
  76742. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  76743. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  76744. + }
  76745. +}
  76746. +
  76747. +/**
  76748. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  76749. + * Device during SRP time by host power up.
  76750. + */
  76751. +void dwc_otg_hcd_power_up(void *ptr)
  76752. +{
  76753. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  76754. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  76755. +
  76756. + DWC_PRINTF("%s called\n", __FUNCTION__);
  76757. +
  76758. + if (!core_if->hibernation_suspend) {
  76759. + DWC_PRINTF("Already exited from Hibernation\n");
  76760. + return;
  76761. + }
  76762. +
  76763. + /* Switch on the voltage to the core */
  76764. + gpwrdn.b.pwrdnswtch = 1;
  76765. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  76766. + dwc_udelay(10);
  76767. +
  76768. + /* Reset the core */
  76769. + gpwrdn.d32 = 0;
  76770. + gpwrdn.b.pwrdnrstn = 1;
  76771. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  76772. + dwc_udelay(10);
  76773. +
  76774. + /* Disable power clamps */
  76775. + gpwrdn.d32 = 0;
  76776. + gpwrdn.b.pwrdnclmp = 1;
  76777. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  76778. +
  76779. + /* Remove reset the core signal */
  76780. + gpwrdn.d32 = 0;
  76781. + gpwrdn.b.pwrdnrstn = 1;
  76782. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  76783. + dwc_udelay(10);
  76784. +
  76785. + /* Disable PMU interrupt */
  76786. + gpwrdn.d32 = 0;
  76787. + gpwrdn.b.pmuintsel = 1;
  76788. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  76789. +
  76790. + core_if->hibernation_suspend = 0;
  76791. +
  76792. + /* Disable PMU */
  76793. + gpwrdn.d32 = 0;
  76794. + gpwrdn.b.pmuactv = 1;
  76795. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  76796. + dwc_udelay(10);
  76797. +
  76798. + /* Enable VBUS */
  76799. + gpwrdn.d32 = 0;
  76800. + gpwrdn.b.dis_vbus = 1;
  76801. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  76802. +
  76803. + core_if->op_state = A_HOST;
  76804. + dwc_otg_core_init(core_if);
  76805. + dwc_otg_enable_global_interrupts(core_if);
  76806. + cil_hcd_start(core_if);
  76807. +}
  76808. +
  76809. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  76810. +{
  76811. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  76812. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  76813. + int i;
  76814. +
  76815. + st->fsm = FIQ_PASSTHROUGH;
  76816. + st->hcchar_copy.d32 = 0;
  76817. + st->hcsplt_copy.d32 = 0;
  76818. + st->hcint_copy.d32 = 0;
  76819. + st->hcintmsk_copy.d32 = 0;
  76820. + st->hctsiz_copy.d32 = 0;
  76821. + st->hcdma_copy.d32 = 0;
  76822. + st->nr_errors = 0;
  76823. + st->hub_addr = 0;
  76824. + st->port_addr = 0;
  76825. + st->expected_uframe = 0;
  76826. + st->nrpackets = 0;
  76827. + st->dma_info.index = 0;
  76828. + for (i = 0; i < 6; i++)
  76829. + st->dma_info.slot_len[i] = 255;
  76830. + st->hs_isoc_info.index = 0;
  76831. + st->hs_isoc_info.iso_desc = NULL;
  76832. + st->hs_isoc_info.nrframes = 0;
  76833. +
  76834. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  76835. +}
  76836. +
  76837. +/**
  76838. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  76839. + * in the struct usb_hcd field.
  76840. + */
  76841. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  76842. +{
  76843. + int i;
  76844. +
  76845. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  76846. +
  76847. + del_timers(dwc_otg_hcd);
  76848. +
  76849. + /* Free memory for QH/QTD lists */
  76850. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  76851. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  76852. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  76853. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  76854. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  76855. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  76856. +
  76857. + /* Free memory for the host channels. */
  76858. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  76859. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  76860. +
  76861. +#ifdef DEBUG
  76862. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  76863. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  76864. + }
  76865. +#endif
  76866. + if (hc != NULL) {
  76867. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  76868. + i, hc);
  76869. + DWC_FREE(hc);
  76870. + }
  76871. + }
  76872. +
  76873. + if (dwc_otg_hcd->core_if->dma_enable) {
  76874. + if (dwc_otg_hcd->status_buf_dma) {
  76875. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  76876. + dwc_otg_hcd->status_buf,
  76877. + dwc_otg_hcd->status_buf_dma);
  76878. + }
  76879. + } else if (dwc_otg_hcd->status_buf != NULL) {
  76880. + DWC_FREE(dwc_otg_hcd->status_buf);
  76881. + }
  76882. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  76883. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  76884. + /* Set core_if's lock pointer to NULL */
  76885. + dwc_otg_hcd->core_if->lock = NULL;
  76886. +
  76887. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  76888. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  76889. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  76890. + DWC_FREE(dwc_otg_hcd->fiq_state);
  76891. +
  76892. +#ifdef DWC_DEV_SRPCAP
  76893. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  76894. + dwc_otg_hcd->core_if->pwron_timer) {
  76895. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  76896. + }
  76897. +#endif
  76898. + DWC_FREE(dwc_otg_hcd);
  76899. +}
  76900. +
  76901. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  76902. +
  76903. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  76904. +{
  76905. + int retval = 0;
  76906. + int num_channels;
  76907. + int i;
  76908. + dwc_hc_t *channel;
  76909. +
  76910. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  76911. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
  76912. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->channel_lock);
  76913. +#else
  76914. + hcd->lock = DWC_SPINLOCK_ALLOC();
  76915. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  76916. +#endif
  76917. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  76918. + hcd, core_if);
  76919. + if (!hcd->lock) {
  76920. + DWC_ERROR("Could not allocate lock for pcd");
  76921. + DWC_FREE(hcd);
  76922. + retval = -DWC_E_NO_MEMORY;
  76923. + goto out;
  76924. + }
  76925. + hcd->core_if = core_if;
  76926. +
  76927. + /* Register the HCD CIL Callbacks */
  76928. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  76929. + &hcd_cil_callbacks, hcd);
  76930. +
  76931. + /* Initialize the non-periodic schedule. */
  76932. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  76933. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  76934. +
  76935. + /* Initialize the periodic schedule. */
  76936. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  76937. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  76938. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  76939. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  76940. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  76941. + /*
  76942. + * Create a host channel descriptor for each host channel implemented
  76943. + * in the controller. Initialize the channel descriptor array.
  76944. + */
  76945. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  76946. + num_channels = hcd->core_if->core_params->host_channels;
  76947. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  76948. + for (i = 0; i < num_channels; i++) {
  76949. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  76950. + if (channel == NULL) {
  76951. + retval = -DWC_E_NO_MEMORY;
  76952. + DWC_ERROR("%s: host channel allocation failed\n",
  76953. + __func__);
  76954. + dwc_otg_hcd_free(hcd);
  76955. + goto out;
  76956. + }
  76957. + channel->hc_num = i;
  76958. + hcd->hc_ptr_array[i] = channel;
  76959. +#ifdef DEBUG
  76960. + hcd->core_if->hc_xfer_timer[i] =
  76961. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  76962. + &hcd->core_if->hc_xfer_info[i]);
  76963. +#endif
  76964. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  76965. + channel);
  76966. + }
  76967. +
  76968. + if (fiq_enable) {
  76969. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  76970. + if (!hcd->fiq_state) {
  76971. + retval = -DWC_E_NO_MEMORY;
  76972. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  76973. + dwc_otg_hcd_free(hcd);
  76974. + goto out;
  76975. + }
  76976. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  76977. +
  76978. + for (i = 0; i < num_channels; i++) {
  76979. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  76980. + }
  76981. + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
  76982. +
  76983. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  76984. + if (!hcd->fiq_stack) {
  76985. + retval = -DWC_E_NO_MEMORY;
  76986. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  76987. + dwc_otg_hcd_free(hcd);
  76988. + goto out;
  76989. + }
  76990. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  76991. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  76992. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  76993. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  76994. +
  76995. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  76996. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  76997. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  76998. + * moderately readable array casts.
  76999. + */
  77000. + hcd->fiq_dmab = DWC_DMA_ALLOC((sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  77001. + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
  77002. + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
  77003. + sizeof(struct fiq_dma_channel) * num_channels);
  77004. +
  77005. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  77006. +
  77007. + /* pointer for debug in fiq_print */
  77008. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  77009. + if (fiq_fsm_enable) {
  77010. + int i;
  77011. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  77012. + dwc_otg_cleanup_fiq_channel(hcd, i);
  77013. + }
  77014. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  77015. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  77016. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  77017. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  77018. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  77019. + }
  77020. + }
  77021. +
  77022. + /* Initialize the Connection timeout timer. */
  77023. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  77024. + dwc_otg_hcd_connect_timeout, 0);
  77025. +
  77026. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  77027. + if (microframe_schedule)
  77028. + init_hcd_usecs(hcd);
  77029. +
  77030. + /* Initialize reset tasklet. */
  77031. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  77032. +
  77033. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  77034. + completion_tasklet_func, hcd);
  77035. +#ifdef DWC_DEV_SRPCAP
  77036. + if (hcd->core_if->power_down == 2) {
  77037. + /* Initialize Power on timer for Host power up in case hibernation */
  77038. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  77039. + dwc_otg_hcd_power_up, core_if);
  77040. + }
  77041. +#endif
  77042. +
  77043. + /*
  77044. + * Allocate space for storing data on status transactions. Normally no
  77045. + * data is sent, but this space acts as a bit bucket. This must be
  77046. + * done after usb_add_hcd since that function allocates the DMA buffer
  77047. + * pool.
  77048. + */
  77049. + if (hcd->core_if->dma_enable) {
  77050. + hcd->status_buf =
  77051. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  77052. + &hcd->status_buf_dma);
  77053. + } else {
  77054. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  77055. + }
  77056. + if (!hcd->status_buf) {
  77057. + retval = -DWC_E_NO_MEMORY;
  77058. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  77059. + dwc_otg_hcd_free(hcd);
  77060. + goto out;
  77061. + }
  77062. +
  77063. + hcd->otg_port = 1;
  77064. + hcd->frame_list = NULL;
  77065. + hcd->frame_list_dma = 0;
  77066. + hcd->periodic_qh_count = 0;
  77067. +
  77068. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  77069. +#ifdef FIQ_DEBUG
  77070. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  77071. +#endif
  77072. +
  77073. +out:
  77074. + return retval;
  77075. +}
  77076. +
  77077. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  77078. +{
  77079. + /* Turn off all host-specific interrupts. */
  77080. + dwc_otg_disable_host_interrupts(hcd->core_if);
  77081. +
  77082. + dwc_otg_hcd_free(hcd);
  77083. +}
  77084. +
  77085. +/**
  77086. + * Initializes dynamic portions of the DWC_otg HCD state.
  77087. + */
  77088. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  77089. +{
  77090. + int num_channels;
  77091. + int i;
  77092. + dwc_hc_t *channel;
  77093. + dwc_hc_t *channel_tmp;
  77094. +
  77095. + hcd->flags.d32 = 0;
  77096. +
  77097. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  77098. + if (!microframe_schedule) {
  77099. + hcd->non_periodic_channels = 0;
  77100. + hcd->periodic_channels = 0;
  77101. + } else {
  77102. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  77103. + }
  77104. + /*
  77105. + * Put all channels in the free channel list and clean up channel
  77106. + * states.
  77107. + */
  77108. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  77109. + &hcd->free_hc_list, hc_list_entry) {
  77110. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  77111. + }
  77112. +
  77113. + num_channels = hcd->core_if->core_params->host_channels;
  77114. + for (i = 0; i < num_channels; i++) {
  77115. + channel = hcd->hc_ptr_array[i];
  77116. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  77117. + hc_list_entry);
  77118. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  77119. + }
  77120. +
  77121. + /* Initialize the DWC core for host mode operation. */
  77122. + dwc_otg_core_host_init(hcd->core_if);
  77123. +
  77124. + /* Set core_if's lock pointer to the hcd->lock */
  77125. + hcd->core_if->lock = hcd->lock;
  77126. +}
  77127. +
  77128. +/**
  77129. + * Assigns transactions from a QTD to a free host channel and initializes the
  77130. + * host channel to perform the transactions. The host channel is removed from
  77131. + * the free list.
  77132. + *
  77133. + * @param hcd The HCD state structure.
  77134. + * @param qh Transactions from the first QTD for this QH are selected and
  77135. + * assigned to a free host channel.
  77136. + */
  77137. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  77138. +{
  77139. + dwc_hc_t *hc;
  77140. + dwc_otg_qtd_t *qtd;
  77141. + dwc_otg_hcd_urb_t *urb;
  77142. + void* ptr = NULL;
  77143. +
  77144. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  77145. +
  77146. + urb = qtd->urb;
  77147. +
  77148. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  77149. +
  77150. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  77151. + urb->actual_length = urb->length;
  77152. +
  77153. +
  77154. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  77155. +
  77156. + /* Remove the host channel from the free list. */
  77157. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  77158. +
  77159. + qh->channel = hc;
  77160. +
  77161. + qtd->in_process = 1;
  77162. +
  77163. + /*
  77164. + * Use usb_pipedevice to determine device address. This address is
  77165. + * 0 before the SET_ADDRESS command and the correct address afterward.
  77166. + */
  77167. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  77168. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  77169. + hc->speed = qh->dev_speed;
  77170. + hc->max_packet = dwc_max_packet(qh->maxp);
  77171. +
  77172. + hc->xfer_started = 0;
  77173. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  77174. + hc->error_state = (qtd->error_count > 0);
  77175. + hc->halt_on_queue = 0;
  77176. + hc->halt_pending = 0;
  77177. + hc->requests = 0;
  77178. +
  77179. + /*
  77180. + * The following values may be modified in the transfer type section
  77181. + * below. The xfer_len value may be reduced when the transfer is
  77182. + * started to accommodate the max widths of the XferSize and PktCnt
  77183. + * fields in the HCTSIZn register.
  77184. + */
  77185. +
  77186. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  77187. + if (hc->ep_is_in) {
  77188. + hc->do_ping = 0;
  77189. + } else {
  77190. + hc->do_ping = qh->ping_state;
  77191. + }
  77192. +
  77193. + hc->data_pid_start = qh->data_toggle;
  77194. + hc->multi_count = 1;
  77195. +
  77196. + if (hcd->core_if->dma_enable) {
  77197. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  77198. +
  77199. + /* For non-dword aligned case */
  77200. + if (((unsigned long)hc->xfer_buff & 0x3)
  77201. + && !hcd->core_if->dma_desc_enable) {
  77202. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  77203. + }
  77204. + } else {
  77205. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  77206. + }
  77207. + hc->xfer_len = urb->length - urb->actual_length;
  77208. + hc->xfer_count = 0;
  77209. +
  77210. + /*
  77211. + * Set the split attributes
  77212. + */
  77213. + hc->do_split = 0;
  77214. + if (qh->do_split) {
  77215. + uint32_t hub_addr, port_addr;
  77216. + hc->do_split = 1;
  77217. + hc->xact_pos = qtd->isoc_split_pos;
  77218. + /* We don't need to do complete splits anymore */
  77219. +// if(fiq_fsm_enable)
  77220. + if (0)
  77221. + hc->complete_split = qtd->complete_split = 0;
  77222. + else
  77223. + hc->complete_split = qtd->complete_split;
  77224. +
  77225. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  77226. + hc->hub_addr = (uint8_t) hub_addr;
  77227. + hc->port_addr = (uint8_t) port_addr;
  77228. + }
  77229. +
  77230. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  77231. + case UE_CONTROL:
  77232. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  77233. + switch (qtd->control_phase) {
  77234. + case DWC_OTG_CONTROL_SETUP:
  77235. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  77236. + hc->do_ping = 0;
  77237. + hc->ep_is_in = 0;
  77238. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  77239. + if (hcd->core_if->dma_enable) {
  77240. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  77241. + } else {
  77242. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  77243. + }
  77244. + hc->xfer_len = 8;
  77245. + ptr = NULL;
  77246. + break;
  77247. + case DWC_OTG_CONTROL_DATA:
  77248. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  77249. + hc->data_pid_start = qtd->data_toggle;
  77250. + break;
  77251. + case DWC_OTG_CONTROL_STATUS:
  77252. + /*
  77253. + * Direction is opposite of data direction or IN if no
  77254. + * data.
  77255. + */
  77256. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  77257. + if (urb->length == 0) {
  77258. + hc->ep_is_in = 1;
  77259. + } else {
  77260. + hc->ep_is_in =
  77261. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  77262. + }
  77263. + if (hc->ep_is_in) {
  77264. + hc->do_ping = 0;
  77265. + }
  77266. +
  77267. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  77268. +
  77269. + hc->xfer_len = 0;
  77270. + if (hcd->core_if->dma_enable) {
  77271. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  77272. + } else {
  77273. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  77274. + }
  77275. + ptr = NULL;
  77276. + break;
  77277. + }
  77278. + break;
  77279. + case UE_BULK:
  77280. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  77281. + break;
  77282. + case UE_INTERRUPT:
  77283. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  77284. + break;
  77285. + case UE_ISOCHRONOUS:
  77286. + {
  77287. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  77288. +
  77289. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  77290. +
  77291. + if (hcd->core_if->dma_desc_enable)
  77292. + break;
  77293. +
  77294. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  77295. +
  77296. + frame_desc->status = 0;
  77297. +
  77298. + if (hcd->core_if->dma_enable) {
  77299. + hc->xfer_buff = (uint8_t *) urb->dma;
  77300. + } else {
  77301. + hc->xfer_buff = (uint8_t *) urb->buf;
  77302. + }
  77303. + hc->xfer_buff +=
  77304. + frame_desc->offset + qtd->isoc_split_offset;
  77305. + hc->xfer_len =
  77306. + frame_desc->length - qtd->isoc_split_offset;
  77307. +
  77308. + /* For non-dword aligned buffers */
  77309. + if (((unsigned long)hc->xfer_buff & 0x3)
  77310. + && hcd->core_if->dma_enable) {
  77311. + ptr =
  77312. + (uint8_t *) urb->buf + frame_desc->offset +
  77313. + qtd->isoc_split_offset;
  77314. + } else
  77315. + ptr = NULL;
  77316. +
  77317. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  77318. + if (hc->xfer_len <= 188) {
  77319. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  77320. + } else {
  77321. + hc->xact_pos =
  77322. + DWC_HCSPLIT_XACTPOS_BEGIN;
  77323. + }
  77324. + }
  77325. + }
  77326. + break;
  77327. + }
  77328. + /* non DWORD-aligned buffer case */
  77329. + if (ptr) {
  77330. + uint32_t buf_size;
  77331. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  77332. + buf_size = hcd->core_if->core_params->max_transfer_size;
  77333. + } else {
  77334. + buf_size = 4096;
  77335. + }
  77336. + if (!qh->dw_align_buf) {
  77337. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  77338. + &qh->dw_align_buf_dma);
  77339. + if (!qh->dw_align_buf) {
  77340. + DWC_ERROR
  77341. + ("%s: Failed to allocate memory to handle "
  77342. + "non-dword aligned buffer case\n",
  77343. + __func__);
  77344. + return;
  77345. + }
  77346. + }
  77347. + if (!hc->ep_is_in) {
  77348. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  77349. + }
  77350. + hc->align_buff = qh->dw_align_buf_dma;
  77351. + } else {
  77352. + hc->align_buff = 0;
  77353. + }
  77354. +
  77355. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  77356. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  77357. + /*
  77358. + * This value may be modified when the transfer is started to
  77359. + * reflect the actual transfer length.
  77360. + */
  77361. + hc->multi_count = dwc_hb_mult(qh->maxp);
  77362. + }
  77363. +
  77364. + if (hcd->core_if->dma_desc_enable)
  77365. + hc->desc_list_addr = qh->desc_list_dma;
  77366. +
  77367. + dwc_otg_hc_init(hcd->core_if, hc);
  77368. + hc->qh = qh;
  77369. +}
  77370. +
  77371. +
  77372. +/**
  77373. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  77374. + * @qh: pointer to the endpoint's queue head
  77375. + *
  77376. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  77377. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  77378. + * This function's eligibility check is altered by debug parameter.
  77379. + *
  77380. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  77381. + */
  77382. +
  77383. +int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh)
  77384. +{
  77385. + if (qh->do_split) {
  77386. + switch (qh->ep_type) {
  77387. + case UE_CONTROL:
  77388. + case UE_BULK:
  77389. + if (fiq_fsm_mask & (1 << 0))
  77390. + return 1;
  77391. + break;
  77392. + case UE_INTERRUPT:
  77393. + case UE_ISOCHRONOUS:
  77394. + if (fiq_fsm_mask & (1 << 1))
  77395. + return 1;
  77396. + break;
  77397. + default:
  77398. + break;
  77399. + }
  77400. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  77401. + if (fiq_fsm_mask & (1 << 2)) {
  77402. + /* HS ISOCH support. We test for compatibility:
  77403. + * - DWORD aligned buffers
  77404. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  77405. + * If yes, then the fsm enqueue function will handle the state machine setup.
  77406. + */
  77407. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  77408. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  77409. + struct dwc_otg_hcd_iso_packet_desc (*iso_descs)[0] = &urb->iso_descs;
  77410. + int nr_iso_frames = urb->packet_count;
  77411. + int i;
  77412. + uint32_t ptr;
  77413. +
  77414. + if (nr_iso_frames < 2)
  77415. + return 0;
  77416. + for (i = 0; i < nr_iso_frames; i++) {
  77417. + ptr = urb->dma + iso_descs[i]->offset;
  77418. + if (ptr & 0x3) {
  77419. + printk_ratelimited("%s: Non-Dword aligned isochronous frame offset."
  77420. + " Cannot queue FIQ-accelerated transfer to device %d endpoint %d\n",
  77421. + __FUNCTION__, qh->channel->dev_addr, qh->channel->ep_num);
  77422. + return 0;
  77423. + }
  77424. + }
  77425. + return 1;
  77426. + }
  77427. + }
  77428. + return 0;
  77429. +}
  77430. +
  77431. +/**
  77432. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  77433. + * @hcd: Pointer to the dwc_otg_hcd struct
  77434. + * @qh: Pointer to the endpoint's queue head
  77435. + *
  77436. + * Periodic split transactions are transmitted modulo 188 bytes.
  77437. + * This necessitates slicing data up into buckets for isochronous out
  77438. + * and fixing up the DMA address for all IN transfers.
  77439. + *
  77440. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  77441. + * HC buffer has been used.
  77442. + */
  77443. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  77444. + {
  77445. + int frame_length, i = 0;
  77446. + uint8_t *ptr = NULL;
  77447. + dwc_hc_t *hc = qh->channel;
  77448. + struct fiq_dma_blob *blob;
  77449. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  77450. +
  77451. + for (i = 0; i < 6; i++) {
  77452. + st->dma_info.slot_len[i] = 255;
  77453. + }
  77454. + st->dma_info.index = 0;
  77455. + i = 0;
  77456. + if (hc->ep_is_in) {
  77457. + /*
  77458. + * Set dma_regs to bounce buffer. FIQ will update the
  77459. + * state depending on transaction progress.
  77460. + */
  77461. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  77462. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  77463. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  77464. + * a transaction if it fails.
  77465. + */
  77466. + frame_length = st->hcchar_copy.b.mps;
  77467. + do {
  77468. + i++;
  77469. + frame_length -= 188;
  77470. + } while (frame_length >= 0);
  77471. + st->nrpackets = i;
  77472. + return 1;
  77473. + } else {
  77474. + if (qh->ep_type == UE_ISOCHRONOUS) {
  77475. +
  77476. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  77477. +
  77478. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  77479. + frame_length = frame_desc->length;
  77480. +
  77481. + /* Virtual address for bounce buffers */
  77482. + blob = hcd->fiq_dmab;
  77483. +
  77484. + ptr = qtd->urb->buf + frame_desc->offset;
  77485. + if (frame_length == 0) {
  77486. + /*
  77487. + * for isochronous transactions, we must still transmit a packet
  77488. + * even if the length is zero.
  77489. + */
  77490. + st->dma_info.slot_len[0] = 0;
  77491. + st->nrpackets = 1;
  77492. + } else {
  77493. + do {
  77494. + if (frame_length <= 188) {
  77495. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  77496. + st->dma_info.slot_len[i] = frame_length;
  77497. + ptr += frame_length;
  77498. + } else {
  77499. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  77500. + st->dma_info.slot_len[i] = 188;
  77501. + ptr += 188;
  77502. + }
  77503. + i++;
  77504. + frame_length -= 188;
  77505. + } while (frame_length > 0);
  77506. + st->nrpackets = i;
  77507. + }
  77508. + ptr = qtd->urb->buf + frame_desc->offset;
  77509. + /* Point the HC at the DMA address of the bounce buffers */
  77510. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  77511. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  77512. +
  77513. + /* fixup xfersize to the actual packet size */
  77514. + st->hctsiz_copy.b.pid = 0;
  77515. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  77516. + return 1;
  77517. + } else {
  77518. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  77519. + return 0;
  77520. + }
  77521. + }
  77522. +}
  77523. +
  77524. +/*
  77525. + * Pushing a periodic request into the queue near the EOF1 point
  77526. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  77527. + * Usually, the request goes out on the bus causing a transfer but
  77528. + * the core does not transfer the data to memory.
  77529. + * This guard interval (in number of 60MHz clocks) is required which
  77530. + * must cater for CPU latency between reading the value and enabling
  77531. + * the channel.
  77532. + */
  77533. +#define PERIODIC_FRREM_BACKOFF 1000
  77534. +
  77535. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  77536. +{
  77537. + dwc_hc_t *hc = qh->channel;
  77538. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  77539. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  77540. + int frame;
  77541. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  77542. + int xfer_len, nrpackets;
  77543. + hcdma_data_t hcdma;
  77544. + hfnum_data_t hfnum;
  77545. +
  77546. + if (st->fsm != FIQ_PASSTHROUGH)
  77547. + return 0;
  77548. +
  77549. + st->nr_errors = 0;
  77550. +
  77551. + st->hcchar_copy.d32 = 0;
  77552. + st->hcchar_copy.b.mps = hc->max_packet;
  77553. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  77554. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  77555. + st->hcchar_copy.b.epnum = hc->ep_num;
  77556. + st->hcchar_copy.b.eptype = hc->ep_type;
  77557. +
  77558. + st->hcintmsk_copy.b.chhltd = 1;
  77559. +
  77560. + frame = dwc_otg_hcd_get_frame_number(hcd);
  77561. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  77562. +
  77563. + st->hcchar_copy.b.lspddev = 0;
  77564. + /* Enable the channel later as a final register write. */
  77565. +
  77566. + st->hcsplt_copy.d32 = 0;
  77567. +
  77568. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  77569. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  77570. + /* grab the next DMA address offset from the array */
  77571. + st->hcdma_copy.d32 = qtd->urb->dma;
  77572. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  77573. +
  77574. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  77575. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  77576. + * this is always set to the maximum size of the endpoint. */
  77577. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  77578. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  77579. + if (nrpackets == 0)
  77580. + nrpackets = 1;
  77581. + st->hcchar_copy.b.multicnt = nrpackets;
  77582. + st->hctsiz_copy.b.pktcnt = nrpackets;
  77583. +
  77584. + /* Initial PID also needs to be set */
  77585. + if (st->hcchar_copy.b.epdir == 0) {
  77586. + st->hctsiz_copy.b.xfersize = xfer_len;
  77587. + switch (st->hcchar_copy.b.multicnt) {
  77588. + case 1:
  77589. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  77590. + break;
  77591. + case 2:
  77592. + case 3:
  77593. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  77594. + break;
  77595. + }
  77596. +
  77597. + } else {
  77598. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  77599. + switch (st->hcchar_copy.b.multicnt) {
  77600. + case 1:
  77601. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  77602. + break;
  77603. + case 2:
  77604. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  77605. + break;
  77606. + case 3:
  77607. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  77608. + break;
  77609. + }
  77610. + }
  77611. +
  77612. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  77613. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  77614. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  77615. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  77616. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  77617. + local_fiq_disable();
  77618. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  77619. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  77620. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  77621. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  77622. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  77623. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  77624. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  77625. + * split transaction is queued very close to EOF.
  77626. + */
  77627. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  77628. + } else {
  77629. + st->fsm = FIQ_HS_ISOC_TURBO;
  77630. + st->hcchar_copy.b.chen = 1;
  77631. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  77632. + }
  77633. + mb();
  77634. + st->hcchar_copy.b.chen = 0;
  77635. + local_fiq_enable();
  77636. + return 0;
  77637. +}
  77638. +
  77639. +
  77640. +/**
  77641. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  77642. + * @hcd: Pointer to the dwc_otg_hcd struct
  77643. + * @qh: Pointer to the endpoint's queue head
  77644. + *
  77645. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  77646. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  77647. + * for the nominated host channel.
  77648. + *
  77649. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  77650. + * start is possible. If not, then the FIQ is left to start the transfer.
  77651. + */
  77652. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  77653. +{
  77654. + int start_immediate = 1, i;
  77655. + hfnum_data_t hfnum;
  77656. + dwc_hc_t *hc = qh->channel;
  77657. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  77658. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  77659. + int hub_addr, port_addr, frame, uframe;
  77660. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  77661. +
  77662. + if (st->fsm != FIQ_PASSTHROUGH)
  77663. + return 0;
  77664. + st->nr_errors = 0;
  77665. +
  77666. + st->hcchar_copy.d32 = 0;
  77667. + st->hcchar_copy.b.mps = hc->max_packet;
  77668. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  77669. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  77670. + st->hcchar_copy.b.epnum = hc->ep_num;
  77671. + st->hcchar_copy.b.eptype = hc->ep_type;
  77672. + if (hc->ep_type & 0x1) {
  77673. + if (hc->ep_is_in)
  77674. + st->hcchar_copy.b.multicnt = 3;
  77675. + else
  77676. + /* Docs say set this to 1, but driver sets to 0! */
  77677. + st->hcchar_copy.b.multicnt = 0;
  77678. + } else {
  77679. + st->hcchar_copy.b.multicnt = 1;
  77680. + st->hcchar_copy.b.oddfrm = 0;
  77681. + }
  77682. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  77683. + /* Enable the channel later as a final register write. */
  77684. +
  77685. + st->hcsplt_copy.d32 = 0;
  77686. + if(qh->do_split) {
  77687. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  77688. + st->hcsplt_copy.b.compsplt = 0;
  77689. + st->hcsplt_copy.b.spltena = 1;
  77690. + // XACTPOS is for isoc-out only but needs initialising anyway.
  77691. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  77692. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  77693. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  77694. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  77695. + * will update as necessary.
  77696. + */
  77697. + if (hc->xfer_len > 188) {
  77698. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  77699. + }
  77700. + }
  77701. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  77702. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  77703. + st->hub_addr = hub_addr;
  77704. + st->port_addr = port_addr;
  77705. + }
  77706. +
  77707. + st->hctsiz_copy.d32 = 0;
  77708. + st->hctsiz_copy.b.dopng = 0;
  77709. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  77710. +
  77711. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  77712. + hc->xfer_len = hc->max_packet;
  77713. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  77714. + hc->xfer_len = 188;
  77715. + }
  77716. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  77717. +
  77718. + st->hctsiz_copy.b.pktcnt = 1;
  77719. +
  77720. + if (hc->ep_type & 0x1) {
  77721. + /*
  77722. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  77723. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  77724. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  77725. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  77726. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  77727. + * must not touch internal driver state.
  77728. + */
  77729. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  77730. + if (hc->align_buff) {
  77731. + st->hcdma_copy.d32 = hc->align_buff;
  77732. + } else {
  77733. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  77734. + }
  77735. + }
  77736. + } else {
  77737. + if (hc->align_buff) {
  77738. + st->hcdma_copy.d32 = hc->align_buff;
  77739. + } else {
  77740. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  77741. + }
  77742. + }
  77743. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  77744. + * Fixup channel interrupt mask. */
  77745. + st->hcintmsk_copy.d32 = 0;
  77746. + st->hcintmsk_copy.b.chhltd = 1;
  77747. + st->hcintmsk_copy.b.ahberr = 1;
  77748. +
  77749. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  77750. + * as Control puts the transfer into the non-periodic request queue and the
  77751. + * non-periodic handler in the hub. Makes things lots easier.
  77752. + */
  77753. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  77754. + st->hcchar_copy.b.multicnt = 0;
  77755. + st->hcchar_copy.b.oddfrm = 0;
  77756. + st->hcchar_copy.b.eptype = UE_CONTROL;
  77757. + if (hc->align_buff) {
  77758. + st->hcdma_copy.d32 = hc->align_buff;
  77759. + } else {
  77760. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  77761. + }
  77762. + }
  77763. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  77764. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  77765. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  77766. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  77767. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  77768. +
  77769. + local_fiq_disable();
  77770. + mb();
  77771. +
  77772. + if (hc->ep_type & 0x1) {
  77773. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  77774. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  77775. + uframe = hfnum.b.frnum & 0x7;
  77776. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  77777. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  77778. + * split transaction is queued very close to EOF.
  77779. + */
  77780. + start_immediate = 0;
  77781. + } else if (uframe == 5) {
  77782. + start_immediate = 0;
  77783. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  77784. + start_immediate = 0;
  77785. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  77786. + start_immediate = 0;
  77787. + } else {
  77788. + /* Search through all host channels to determine if a transaction
  77789. + * is currently in progress */
  77790. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  77791. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  77792. + continue;
  77793. + switch (hcd->fiq_state->channel[i].fsm) {
  77794. + /* TT is reserved for channels that are in the middle of a periodic
  77795. + * split transaction.
  77796. + */
  77797. + case FIQ_PER_SSPLIT_STARTED:
  77798. + case FIQ_PER_CSPLIT_WAIT:
  77799. + case FIQ_PER_CSPLIT_NYET1:
  77800. + case FIQ_PER_CSPLIT_POLL:
  77801. + case FIQ_PER_ISO_OUT_ACTIVE:
  77802. + case FIQ_PER_ISO_OUT_LAST:
  77803. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  77804. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  77805. + start_immediate = 0;
  77806. + }
  77807. + break;
  77808. + default:
  77809. + break;
  77810. + }
  77811. + if (!start_immediate)
  77812. + break;
  77813. + }
  77814. + }
  77815. + }
  77816. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  77817. + start_immediate = 1;
  77818. +
  77819. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  77820. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  77821. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  77822. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  77823. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  77824. + switch (hc->ep_type) {
  77825. + case UE_CONTROL:
  77826. + case UE_BULK:
  77827. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  77828. + break;
  77829. + case UE_ISOCHRONOUS:
  77830. + if (hc->ep_is_in) {
  77831. + if (start_immediate) {
  77832. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  77833. + } else {
  77834. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  77835. + }
  77836. + } else {
  77837. + if (start_immediate) {
  77838. + /* Single-isoc OUT packets don't require FIQ involvement */
  77839. + if (st->nrpackets == 1) {
  77840. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  77841. + } else {
  77842. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  77843. + }
  77844. + } else {
  77845. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  77846. + }
  77847. + }
  77848. + break;
  77849. + case UE_INTERRUPT:
  77850. + if (fiq_fsm_mask & 0x8) {
  77851. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  77852. + } else if (start_immediate) {
  77853. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  77854. + } else {
  77855. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  77856. + }
  77857. + default:
  77858. + break;
  77859. + }
  77860. + if (start_immediate) {
  77861. + /* Set the oddfrm bit as close as possible to actual queueing */
  77862. + frame = dwc_otg_hcd_get_frame_number(hcd);
  77863. + st->expected_uframe = (frame + 1) & 0x3FFF;
  77864. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  77865. + st->hcchar_copy.b.chen = 1;
  77866. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  77867. + }
  77868. + mb();
  77869. + local_fiq_enable();
  77870. + return 0;
  77871. +}
  77872. +
  77873. +
  77874. +/**
  77875. + * This function selects transactions from the HCD transfer schedule and
  77876. + * assigns them to available host channels. It is called from HCD interrupt
  77877. + * handler functions.
  77878. + *
  77879. + * @param hcd The HCD state structure.
  77880. + *
  77881. + * @return The types of new transactions that were assigned to host channels.
  77882. + */
  77883. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  77884. +{
  77885. + dwc_list_link_t *qh_ptr;
  77886. + dwc_otg_qh_t *qh;
  77887. + int num_channels;
  77888. + dwc_irqflags_t flags;
  77889. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  77890. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  77891. +
  77892. +#ifdef DEBUG_HOST_CHANNELS
  77893. + last_sel_trans_num_per_scheduled = 0;
  77894. + last_sel_trans_num_nonper_scheduled = 0;
  77895. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  77896. +#endif /* DEBUG_HOST_CHANNELS */
  77897. +
  77898. + /* Process entries in the periodic ready list. */
  77899. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  77900. +
  77901. + while (qh_ptr != &hcd->periodic_sched_ready &&
  77902. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  77903. +
  77904. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  77905. +
  77906. + if (microframe_schedule) {
  77907. + // Make sure we leave one channel for non periodic transactions.
  77908. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  77909. + if (hcd->available_host_channels <= 1) {
  77910. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  77911. + break;
  77912. + }
  77913. + hcd->available_host_channels--;
  77914. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  77915. +#ifdef DEBUG_HOST_CHANNELS
  77916. + last_sel_trans_num_per_scheduled++;
  77917. +#endif /* DEBUG_HOST_CHANNELS */
  77918. + }
  77919. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  77920. + assign_and_init_hc(hcd, qh);
  77921. +
  77922. + /*
  77923. + * Move the QH from the periodic ready schedule to the
  77924. + * periodic assigned schedule.
  77925. + */
  77926. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  77927. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  77928. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  77929. + &qh->qh_list_entry);
  77930. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  77931. + }
  77932. +
  77933. + /*
  77934. + * Process entries in the inactive portion of the non-periodic
  77935. + * schedule. Some free host channels may not be used if they are
  77936. + * reserved for periodic transfers.
  77937. + */
  77938. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  77939. + num_channels = hcd->core_if->core_params->host_channels;
  77940. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  77941. + (microframe_schedule || hcd->non_periodic_channels <
  77942. + num_channels - hcd->periodic_channels) &&
  77943. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  77944. +
  77945. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  77946. + /*
  77947. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  77948. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  77949. + * cheeky devices that just hold off using NAKs
  77950. + */
  77951. + if (nak_holdoff && qh->do_split) {
  77952. + if (qh->nak_frame != 0xffff) {
  77953. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  77954. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  77955. + if (dwc_frame_num_le(frame, next_frame)) {
  77956. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  77957. + hcd->fiq_state->next_sched_frame = next_frame;
  77958. + }
  77959. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  77960. + continue;
  77961. + } else {
  77962. + qh->nak_frame = 0xFFFF;
  77963. + }
  77964. + }
  77965. + }
  77966. +
  77967. + if (microframe_schedule) {
  77968. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  77969. + if (hcd->available_host_channels < 1) {
  77970. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  77971. + break;
  77972. + }
  77973. + hcd->available_host_channels--;
  77974. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  77975. +#ifdef DEBUG_HOST_CHANNELS
  77976. + last_sel_trans_num_nonper_scheduled++;
  77977. +#endif /* DEBUG_HOST_CHANNELS */
  77978. + }
  77979. +
  77980. + assign_and_init_hc(hcd, qh);
  77981. +
  77982. + /*
  77983. + * Move the QH from the non-periodic inactive schedule to the
  77984. + * non-periodic active schedule.
  77985. + */
  77986. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  77987. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  77988. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  77989. + &qh->qh_list_entry);
  77990. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  77991. +
  77992. +
  77993. + if (!microframe_schedule)
  77994. + hcd->non_periodic_channels++;
  77995. + }
  77996. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  77997. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  77998. + * ran out of host channels.
  77999. + */
  78000. + if (fiq_enable) {
  78001. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  78002. + hcd->fiq_state->kick_np_queues = 0;
  78003. + } else {
  78004. + /* For each entry remaining in the NP inactive queue,
  78005. + * if this a NAK'd retransmit then don't set the kick flag.
  78006. + */
  78007. + if(nak_holdoff) {
  78008. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  78009. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  78010. + if (qh->nak_frame == 0xFFFF) {
  78011. + hcd->fiq_state->kick_np_queues = 1;
  78012. + }
  78013. + }
  78014. + }
  78015. + }
  78016. + }
  78017. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  78018. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  78019. +
  78020. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  78021. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  78022. +
  78023. +
  78024. +#ifdef DEBUG_HOST_CHANNELS
  78025. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  78026. +#endif /* DEBUG_HOST_CHANNELS */
  78027. + return ret_val;
  78028. +}
  78029. +
  78030. +/**
  78031. + * Attempts to queue a single transaction request for a host channel
  78032. + * associated with either a periodic or non-periodic transfer. This function
  78033. + * assumes that there is space available in the appropriate request queue. For
  78034. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  78035. + * is available in the appropriate Tx FIFO.
  78036. + *
  78037. + * @param hcd The HCD state structure.
  78038. + * @param hc Host channel descriptor associated with either a periodic or
  78039. + * non-periodic transfer.
  78040. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  78041. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  78042. + * transfers.
  78043. + *
  78044. + * @return 1 if a request is queued and more requests may be needed to
  78045. + * complete the transfer, 0 if no more requests are required for this
  78046. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  78047. + */
  78048. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  78049. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  78050. +{
  78051. + int retval;
  78052. +
  78053. + if (hcd->core_if->dma_enable) {
  78054. + if (hcd->core_if->dma_desc_enable) {
  78055. + if (!hc->xfer_started
  78056. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  78057. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  78058. + hc->qh->ping_state = 0;
  78059. + }
  78060. + } else if (!hc->xfer_started) {
  78061. + if (fiq_fsm_enable && hc->error_state) {
  78062. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  78063. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  78064. + hcd->fiq_state->channel[hc->hc_num].fsm =
  78065. + FIQ_PASSTHROUGH_ERRORSTATE;
  78066. + }
  78067. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  78068. + hc->qh->ping_state = 0;
  78069. + }
  78070. + retval = 0;
  78071. + } else if (hc->halt_pending) {
  78072. + /* Don't queue a request if the channel has been halted. */
  78073. + retval = 0;
  78074. + } else if (hc->halt_on_queue) {
  78075. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  78076. + retval = 0;
  78077. + } else if (hc->do_ping) {
  78078. + if (!hc->xfer_started) {
  78079. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  78080. + }
  78081. + retval = 0;
  78082. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  78083. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  78084. + if (!hc->xfer_started) {
  78085. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  78086. + retval = 1;
  78087. + } else {
  78088. + retval =
  78089. + dwc_otg_hc_continue_transfer(hcd->core_if,
  78090. + hc);
  78091. + }
  78092. + } else {
  78093. + retval = -1;
  78094. + }
  78095. + } else {
  78096. + if (!hc->xfer_started) {
  78097. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  78098. + retval = 1;
  78099. + } else {
  78100. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  78101. + }
  78102. + }
  78103. +
  78104. + return retval;
  78105. +}
  78106. +
  78107. +/**
  78108. + * Processes periodic channels for the next frame and queues transactions for
  78109. + * these channels to the DWC_otg controller. After queueing transactions, the
  78110. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  78111. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  78112. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  78113. + */
  78114. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  78115. +{
  78116. + hptxsts_data_t tx_status;
  78117. + dwc_list_link_t *qh_ptr;
  78118. + dwc_otg_qh_t *qh;
  78119. + int status = 0;
  78120. + int no_queue_space = 0;
  78121. + int no_fifo_space = 0;
  78122. +
  78123. + dwc_otg_host_global_regs_t *host_regs;
  78124. + host_regs = hcd->core_if->host_if->host_global_regs;
  78125. +
  78126. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  78127. +#ifdef DEBUG
  78128. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  78129. + DWC_DEBUGPL(DBG_HCDV,
  78130. + " P Tx Req Queue Space Avail (before queue): %d\n",
  78131. + tx_status.b.ptxqspcavail);
  78132. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  78133. + tx_status.b.ptxfspcavail);
  78134. +#endif
  78135. +
  78136. + qh_ptr = hcd->periodic_sched_assigned.next;
  78137. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  78138. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  78139. + if (tx_status.b.ptxqspcavail == 0) {
  78140. + no_queue_space = 1;
  78141. + break;
  78142. + }
  78143. +
  78144. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  78145. +
  78146. + // Do not send a split start transaction any later than frame .6
  78147. + // Note, we have to schedule a periodic in .5 to make it go in .6
  78148. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  78149. + {
  78150. + qh_ptr = qh_ptr->next;
  78151. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  78152. + continue;
  78153. + }
  78154. +
  78155. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  78156. + if (qh->do_split)
  78157. + fiq_fsm_queue_split_transaction(hcd, qh);
  78158. + else
  78159. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  78160. + } else {
  78161. +
  78162. + /*
  78163. + * Set a flag if we're queueing high-bandwidth in slave mode.
  78164. + * The flag prevents any halts to get into the request queue in
  78165. + * the middle of multiple high-bandwidth packets getting queued.
  78166. + */
  78167. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  78168. + hcd->core_if->queuing_high_bandwidth = 1;
  78169. + }
  78170. + status = queue_transaction(hcd, qh->channel,
  78171. + tx_status.b.ptxfspcavail);
  78172. + if (status < 0) {
  78173. + no_fifo_space = 1;
  78174. + break;
  78175. + }
  78176. + }
  78177. +
  78178. + /*
  78179. + * In Slave mode, stay on the current transfer until there is
  78180. + * nothing more to do or the high-bandwidth request count is
  78181. + * reached. In DMA mode, only need to queue one request. The
  78182. + * controller automatically handles multiple packets for
  78183. + * high-bandwidth transfers.
  78184. + */
  78185. + if (hcd->core_if->dma_enable || status == 0 ||
  78186. + qh->channel->requests == qh->channel->multi_count) {
  78187. + qh_ptr = qh_ptr->next;
  78188. + /*
  78189. + * Move the QH from the periodic assigned schedule to
  78190. + * the periodic queued schedule.
  78191. + */
  78192. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  78193. + &qh->qh_list_entry);
  78194. +
  78195. + /* done queuing high bandwidth */
  78196. + hcd->core_if->queuing_high_bandwidth = 0;
  78197. + }
  78198. + }
  78199. +
  78200. + if (!hcd->core_if->dma_enable) {
  78201. + dwc_otg_core_global_regs_t *global_regs;
  78202. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78203. +
  78204. + global_regs = hcd->core_if->core_global_regs;
  78205. + intr_mask.b.ptxfempty = 1;
  78206. +#ifdef DEBUG
  78207. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  78208. + DWC_DEBUGPL(DBG_HCDV,
  78209. + " P Tx Req Queue Space Avail (after queue): %d\n",
  78210. + tx_status.b.ptxqspcavail);
  78211. + DWC_DEBUGPL(DBG_HCDV,
  78212. + " P Tx FIFO Space Avail (after queue): %d\n",
  78213. + tx_status.b.ptxfspcavail);
  78214. +#endif
  78215. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  78216. + no_queue_space || no_fifo_space) {
  78217. + /*
  78218. + * May need to queue more transactions as the request
  78219. + * queue or Tx FIFO empties. Enable the periodic Tx
  78220. + * FIFO empty interrupt. (Always use the half-empty
  78221. + * level to ensure that new requests are loaded as
  78222. + * soon as possible.)
  78223. + */
  78224. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  78225. + intr_mask.d32);
  78226. + } else {
  78227. + /*
  78228. + * Disable the Tx FIFO empty interrupt since there are
  78229. + * no more transactions that need to be queued right
  78230. + * now. This function is called from interrupt
  78231. + * handlers to queue more transactions as transfer
  78232. + * states change.
  78233. + */
  78234. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  78235. + 0);
  78236. + }
  78237. + }
  78238. +}
  78239. +
  78240. +/**
  78241. + * Processes active non-periodic channels and queues transactions for these
  78242. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  78243. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  78244. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  78245. + * FIFO Empty interrupt is disabled.
  78246. + */
  78247. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  78248. +{
  78249. + gnptxsts_data_t tx_status;
  78250. + dwc_list_link_t *orig_qh_ptr;
  78251. + dwc_otg_qh_t *qh;
  78252. + int status;
  78253. + int no_queue_space = 0;
  78254. + int no_fifo_space = 0;
  78255. + int more_to_do = 0;
  78256. +
  78257. + dwc_otg_core_global_regs_t *global_regs =
  78258. + hcd->core_if->core_global_regs;
  78259. +
  78260. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  78261. +#ifdef DEBUG
  78262. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  78263. + DWC_DEBUGPL(DBG_HCDV,
  78264. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  78265. + tx_status.b.nptxqspcavail);
  78266. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  78267. + tx_status.b.nptxfspcavail);
  78268. +#endif
  78269. + /*
  78270. + * Keep track of the starting point. Skip over the start-of-list
  78271. + * entry.
  78272. + */
  78273. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  78274. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  78275. + }
  78276. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  78277. +
  78278. + /*
  78279. + * Process once through the active list or until no more space is
  78280. + * available in the request queue or the Tx FIFO.
  78281. + */
  78282. + do {
  78283. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  78284. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  78285. + no_queue_space = 1;
  78286. + break;
  78287. + }
  78288. +
  78289. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  78290. + qh_list_entry);
  78291. +
  78292. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  78293. + fiq_fsm_queue_split_transaction(hcd, qh);
  78294. + } else {
  78295. + status = queue_transaction(hcd, qh->channel,
  78296. + tx_status.b.nptxfspcavail);
  78297. +
  78298. + if (status > 0) {
  78299. + more_to_do = 1;
  78300. + } else if (status < 0) {
  78301. + no_fifo_space = 1;
  78302. + break;
  78303. + }
  78304. + }
  78305. + /* Advance to next QH, skipping start-of-list entry. */
  78306. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  78307. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  78308. + hcd->non_periodic_qh_ptr =
  78309. + hcd->non_periodic_qh_ptr->next;
  78310. + }
  78311. +
  78312. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  78313. +
  78314. + if (!hcd->core_if->dma_enable) {
  78315. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78316. + intr_mask.b.nptxfempty = 1;
  78317. +
  78318. +#ifdef DEBUG
  78319. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  78320. + DWC_DEBUGPL(DBG_HCDV,
  78321. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  78322. + tx_status.b.nptxqspcavail);
  78323. + DWC_DEBUGPL(DBG_HCDV,
  78324. + " NP Tx FIFO Space Avail (after queue): %d\n",
  78325. + tx_status.b.nptxfspcavail);
  78326. +#endif
  78327. + if (more_to_do || no_queue_space || no_fifo_space) {
  78328. + /*
  78329. + * May need to queue more transactions as the request
  78330. + * queue or Tx FIFO empties. Enable the non-periodic
  78331. + * Tx FIFO empty interrupt. (Always use the half-empty
  78332. + * level to ensure that new requests are loaded as
  78333. + * soon as possible.)
  78334. + */
  78335. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  78336. + intr_mask.d32);
  78337. + } else {
  78338. + /*
  78339. + * Disable the Tx FIFO empty interrupt since there are
  78340. + * no more transactions that need to be queued right
  78341. + * now. This function is called from interrupt
  78342. + * handlers to queue more transactions as transfer
  78343. + * states change.
  78344. + */
  78345. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  78346. + 0);
  78347. + }
  78348. + }
  78349. +}
  78350. +
  78351. +/**
  78352. + * This function processes the currently active host channels and queues
  78353. + * transactions for these channels to the DWC_otg controller. It is called
  78354. + * from HCD interrupt handler functions.
  78355. + *
  78356. + * @param hcd The HCD state structure.
  78357. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  78358. + * periodic, or both).
  78359. + */
  78360. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  78361. + dwc_otg_transaction_type_e tr_type)
  78362. +{
  78363. +#ifdef DEBUG_SOF
  78364. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  78365. +#endif
  78366. + /* Process host channels associated with periodic transfers. */
  78367. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  78368. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  78369. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  78370. +
  78371. + process_periodic_channels(hcd);
  78372. + }
  78373. +
  78374. + /* Process host channels associated with non-periodic transfers. */
  78375. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  78376. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  78377. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  78378. + process_non_periodic_channels(hcd);
  78379. + } else {
  78380. + /*
  78381. + * Ensure NP Tx FIFO empty interrupt is disabled when
  78382. + * there are no non-periodic transfers to process.
  78383. + */
  78384. + gintmsk_data_t gintmsk = {.d32 = 0 };
  78385. + gintmsk.b.nptxfempty = 1;
  78386. + DWC_MODIFY_REG32(&hcd->core_if->
  78387. + core_global_regs->gintmsk, gintmsk.d32,
  78388. + 0);
  78389. + }
  78390. + }
  78391. +}
  78392. +
  78393. +#ifdef DWC_HS_ELECT_TST
  78394. +/*
  78395. + * Quick and dirty hack to implement the HS Electrical Test
  78396. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  78397. + *
  78398. + * This code was copied from our userspace app "hset". It sends a
  78399. + * Get Device Descriptor control sequence in two parts, first the
  78400. + * Setup packet by itself, followed some time later by the In and
  78401. + * Ack packets. Rather than trying to figure out how to add this
  78402. + * functionality to the normal driver code, we just hijack the
  78403. + * hardware, using these two function to drive the hardware
  78404. + * directly.
  78405. + */
  78406. +
  78407. +static dwc_otg_core_global_regs_t *global_regs;
  78408. +static dwc_otg_host_global_regs_t *hc_global_regs;
  78409. +static dwc_otg_hc_regs_t *hc_regs;
  78410. +static uint32_t *data_fifo;
  78411. +
  78412. +static void do_setup(void)
  78413. +{
  78414. + gintsts_data_t gintsts;
  78415. + hctsiz_data_t hctsiz;
  78416. + hcchar_data_t hcchar;
  78417. + haint_data_t haint;
  78418. + hcint_data_t hcint;
  78419. +
  78420. + /* Enable HAINTs */
  78421. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  78422. +
  78423. + /* Enable HCINTs */
  78424. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  78425. +
  78426. + /* Read GINTSTS */
  78427. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78428. +
  78429. + /* Read HAINT */
  78430. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78431. +
  78432. + /* Read HCINT */
  78433. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78434. +
  78435. + /* Read HCCHAR */
  78436. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78437. +
  78438. + /* Clear HCINT */
  78439. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78440. +
  78441. + /* Clear HAINT */
  78442. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78443. +
  78444. + /* Clear GINTSTS */
  78445. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78446. +
  78447. + /* Read GINTSTS */
  78448. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78449. +
  78450. + /*
  78451. + * Send Setup packet (Get Device Descriptor)
  78452. + */
  78453. +
  78454. + /* Make sure channel is disabled */
  78455. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78456. + if (hcchar.b.chen) {
  78457. + hcchar.b.chdis = 1;
  78458. +// hcchar.b.chen = 1;
  78459. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  78460. + //sleep(1);
  78461. + dwc_mdelay(1000);
  78462. +
  78463. + /* Read GINTSTS */
  78464. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78465. +
  78466. + /* Read HAINT */
  78467. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78468. +
  78469. + /* Read HCINT */
  78470. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78471. +
  78472. + /* Read HCCHAR */
  78473. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78474. +
  78475. + /* Clear HCINT */
  78476. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78477. +
  78478. + /* Clear HAINT */
  78479. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78480. +
  78481. + /* Clear GINTSTS */
  78482. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78483. +
  78484. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78485. + }
  78486. +
  78487. + /* Set HCTSIZ */
  78488. + hctsiz.d32 = 0;
  78489. + hctsiz.b.xfersize = 8;
  78490. + hctsiz.b.pktcnt = 1;
  78491. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  78492. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  78493. +
  78494. + /* Set HCCHAR */
  78495. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78496. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  78497. + hcchar.b.epdir = 0;
  78498. + hcchar.b.epnum = 0;
  78499. + hcchar.b.mps = 8;
  78500. + hcchar.b.chen = 1;
  78501. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  78502. +
  78503. + /* Fill FIFO with Setup data for Get Device Descriptor */
  78504. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  78505. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  78506. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  78507. +
  78508. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78509. +
  78510. + /* Wait for host channel interrupt */
  78511. + do {
  78512. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78513. + } while (gintsts.b.hcintr == 0);
  78514. +
  78515. + /* Disable HCINTs */
  78516. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  78517. +
  78518. + /* Disable HAINTs */
  78519. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  78520. +
  78521. + /* Read HAINT */
  78522. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78523. +
  78524. + /* Read HCINT */
  78525. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78526. +
  78527. + /* Read HCCHAR */
  78528. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78529. +
  78530. + /* Clear HCINT */
  78531. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78532. +
  78533. + /* Clear HAINT */
  78534. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78535. +
  78536. + /* Clear GINTSTS */
  78537. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78538. +
  78539. + /* Read GINTSTS */
  78540. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78541. +}
  78542. +
  78543. +static void do_in_ack(void)
  78544. +{
  78545. + gintsts_data_t gintsts;
  78546. + hctsiz_data_t hctsiz;
  78547. + hcchar_data_t hcchar;
  78548. + haint_data_t haint;
  78549. + hcint_data_t hcint;
  78550. + host_grxsts_data_t grxsts;
  78551. +
  78552. + /* Enable HAINTs */
  78553. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  78554. +
  78555. + /* Enable HCINTs */
  78556. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  78557. +
  78558. + /* Read GINTSTS */
  78559. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78560. +
  78561. + /* Read HAINT */
  78562. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78563. +
  78564. + /* Read HCINT */
  78565. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78566. +
  78567. + /* Read HCCHAR */
  78568. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78569. +
  78570. + /* Clear HCINT */
  78571. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78572. +
  78573. + /* Clear HAINT */
  78574. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78575. +
  78576. + /* Clear GINTSTS */
  78577. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78578. +
  78579. + /* Read GINTSTS */
  78580. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78581. +
  78582. + /*
  78583. + * Receive Control In packet
  78584. + */
  78585. +
  78586. + /* Make sure channel is disabled */
  78587. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78588. + if (hcchar.b.chen) {
  78589. + hcchar.b.chdis = 1;
  78590. + hcchar.b.chen = 1;
  78591. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  78592. + //sleep(1);
  78593. + dwc_mdelay(1000);
  78594. +
  78595. + /* Read GINTSTS */
  78596. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78597. +
  78598. + /* Read HAINT */
  78599. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78600. +
  78601. + /* Read HCINT */
  78602. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78603. +
  78604. + /* Read HCCHAR */
  78605. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78606. +
  78607. + /* Clear HCINT */
  78608. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78609. +
  78610. + /* Clear HAINT */
  78611. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78612. +
  78613. + /* Clear GINTSTS */
  78614. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78615. +
  78616. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78617. + }
  78618. +
  78619. + /* Set HCTSIZ */
  78620. + hctsiz.d32 = 0;
  78621. + hctsiz.b.xfersize = 8;
  78622. + hctsiz.b.pktcnt = 1;
  78623. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  78624. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  78625. +
  78626. + /* Set HCCHAR */
  78627. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78628. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  78629. + hcchar.b.epdir = 1;
  78630. + hcchar.b.epnum = 0;
  78631. + hcchar.b.mps = 8;
  78632. + hcchar.b.chen = 1;
  78633. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  78634. +
  78635. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78636. +
  78637. + /* Wait for receive status queue interrupt */
  78638. + do {
  78639. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78640. + } while (gintsts.b.rxstsqlvl == 0);
  78641. +
  78642. + /* Read RXSTS */
  78643. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  78644. +
  78645. + /* Clear RXSTSQLVL in GINTSTS */
  78646. + gintsts.d32 = 0;
  78647. + gintsts.b.rxstsqlvl = 1;
  78648. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78649. +
  78650. + switch (grxsts.b.pktsts) {
  78651. + case DWC_GRXSTS_PKTSTS_IN:
  78652. + /* Read the data into the host buffer */
  78653. + if (grxsts.b.bcnt > 0) {
  78654. + int i;
  78655. + int word_count = (grxsts.b.bcnt + 3) / 4;
  78656. +
  78657. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  78658. +
  78659. + for (i = 0; i < word_count; i++) {
  78660. + (void)DWC_READ_REG32(data_fifo++);
  78661. + }
  78662. + }
  78663. + break;
  78664. +
  78665. + default:
  78666. + break;
  78667. + }
  78668. +
  78669. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78670. +
  78671. + /* Wait for receive status queue interrupt */
  78672. + do {
  78673. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78674. + } while (gintsts.b.rxstsqlvl == 0);
  78675. +
  78676. + /* Read RXSTS */
  78677. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  78678. +
  78679. + /* Clear RXSTSQLVL in GINTSTS */
  78680. + gintsts.d32 = 0;
  78681. + gintsts.b.rxstsqlvl = 1;
  78682. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78683. +
  78684. + switch (grxsts.b.pktsts) {
  78685. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  78686. + break;
  78687. +
  78688. + default:
  78689. + break;
  78690. + }
  78691. +
  78692. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78693. +
  78694. + /* Wait for host channel interrupt */
  78695. + do {
  78696. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78697. + } while (gintsts.b.hcintr == 0);
  78698. +
  78699. + /* Read HAINT */
  78700. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78701. +
  78702. + /* Read HCINT */
  78703. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78704. +
  78705. + /* Read HCCHAR */
  78706. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78707. +
  78708. + /* Clear HCINT */
  78709. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78710. +
  78711. + /* Clear HAINT */
  78712. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78713. +
  78714. + /* Clear GINTSTS */
  78715. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78716. +
  78717. + /* Read GINTSTS */
  78718. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78719. +
  78720. +// usleep(100000);
  78721. +// mdelay(100);
  78722. + dwc_mdelay(1);
  78723. +
  78724. + /*
  78725. + * Send handshake packet
  78726. + */
  78727. +
  78728. + /* Read HAINT */
  78729. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78730. +
  78731. + /* Read HCINT */
  78732. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78733. +
  78734. + /* Read HCCHAR */
  78735. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78736. +
  78737. + /* Clear HCINT */
  78738. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78739. +
  78740. + /* Clear HAINT */
  78741. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78742. +
  78743. + /* Clear GINTSTS */
  78744. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78745. +
  78746. + /* Read GINTSTS */
  78747. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78748. +
  78749. + /* Make sure channel is disabled */
  78750. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78751. + if (hcchar.b.chen) {
  78752. + hcchar.b.chdis = 1;
  78753. + hcchar.b.chen = 1;
  78754. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  78755. + //sleep(1);
  78756. + dwc_mdelay(1000);
  78757. +
  78758. + /* Read GINTSTS */
  78759. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78760. +
  78761. + /* Read HAINT */
  78762. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78763. +
  78764. + /* Read HCINT */
  78765. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78766. +
  78767. + /* Read HCCHAR */
  78768. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78769. +
  78770. + /* Clear HCINT */
  78771. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78772. +
  78773. + /* Clear HAINT */
  78774. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78775. +
  78776. + /* Clear GINTSTS */
  78777. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78778. +
  78779. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78780. + }
  78781. +
  78782. + /* Set HCTSIZ */
  78783. + hctsiz.d32 = 0;
  78784. + hctsiz.b.xfersize = 0;
  78785. + hctsiz.b.pktcnt = 1;
  78786. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  78787. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  78788. +
  78789. + /* Set HCCHAR */
  78790. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78791. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  78792. + hcchar.b.epdir = 0;
  78793. + hcchar.b.epnum = 0;
  78794. + hcchar.b.mps = 8;
  78795. + hcchar.b.chen = 1;
  78796. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  78797. +
  78798. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78799. +
  78800. + /* Wait for host channel interrupt */
  78801. + do {
  78802. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78803. + } while (gintsts.b.hcintr == 0);
  78804. +
  78805. + /* Disable HCINTs */
  78806. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  78807. +
  78808. + /* Disable HAINTs */
  78809. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  78810. +
  78811. + /* Read HAINT */
  78812. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  78813. +
  78814. + /* Read HCINT */
  78815. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  78816. +
  78817. + /* Read HCCHAR */
  78818. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78819. +
  78820. + /* Clear HCINT */
  78821. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  78822. +
  78823. + /* Clear HAINT */
  78824. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  78825. +
  78826. + /* Clear GINTSTS */
  78827. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  78828. +
  78829. + /* Read GINTSTS */
  78830. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  78831. +}
  78832. +#endif
  78833. +
  78834. +/** Handles hub class-specific requests. */
  78835. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  78836. + uint16_t typeReq,
  78837. + uint16_t wValue,
  78838. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  78839. +{
  78840. + int retval = 0;
  78841. +
  78842. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  78843. + usb_hub_descriptor_t *hub_desc;
  78844. + hprt0_data_t hprt0 = {.d32 = 0 };
  78845. +
  78846. + uint32_t port_status;
  78847. +
  78848. + switch (typeReq) {
  78849. + case UCR_CLEAR_HUB_FEATURE:
  78850. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78851. + "ClearHubFeature 0x%x\n", wValue);
  78852. + switch (wValue) {
  78853. + case UHF_C_HUB_LOCAL_POWER:
  78854. + case UHF_C_HUB_OVER_CURRENT:
  78855. + /* Nothing required here */
  78856. + break;
  78857. + default:
  78858. + retval = -DWC_E_INVALID;
  78859. + DWC_ERROR("DWC OTG HCD - "
  78860. + "ClearHubFeature request %xh unknown\n",
  78861. + wValue);
  78862. + }
  78863. + break;
  78864. + case UCR_CLEAR_PORT_FEATURE:
  78865. +#ifdef CONFIG_USB_DWC_OTG_LPM
  78866. + if (wValue != UHF_PORT_L1)
  78867. +#endif
  78868. + if (!wIndex || wIndex > 1)
  78869. + goto error;
  78870. +
  78871. + switch (wValue) {
  78872. + case UHF_PORT_ENABLE:
  78873. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  78874. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  78875. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  78876. + hprt0.b.prtena = 1;
  78877. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78878. + break;
  78879. + case UHF_PORT_SUSPEND:
  78880. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78881. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  78882. +
  78883. + if (core_if->power_down == 2) {
  78884. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  78885. + } else {
  78886. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  78887. + dwc_mdelay(5);
  78888. +
  78889. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  78890. + hprt0.b.prtres = 1;
  78891. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78892. + hprt0.b.prtsusp = 0;
  78893. + /* Clear Resume bit */
  78894. + dwc_mdelay(100);
  78895. + hprt0.b.prtres = 0;
  78896. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78897. + }
  78898. + break;
  78899. +#ifdef CONFIG_USB_DWC_OTG_LPM
  78900. + case UHF_PORT_L1:
  78901. + {
  78902. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  78903. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  78904. +
  78905. + lpmcfg.d32 =
  78906. + DWC_READ_REG32(&core_if->
  78907. + core_global_regs->glpmcfg);
  78908. + lpmcfg.b.en_utmi_sleep = 0;
  78909. + lpmcfg.b.hird_thres &= (~(1 << 4));
  78910. + lpmcfg.b.prt_sleep_sts = 1;
  78911. + DWC_WRITE_REG32(&core_if->
  78912. + core_global_regs->glpmcfg,
  78913. + lpmcfg.d32);
  78914. +
  78915. + /* Clear Enbl_L1Gating bit. */
  78916. + pcgcctl.b.enbl_sleep_gating = 1;
  78917. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  78918. + 0);
  78919. +
  78920. + dwc_mdelay(5);
  78921. +
  78922. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  78923. + hprt0.b.prtres = 1;
  78924. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  78925. + hprt0.d32);
  78926. + /* This bit will be cleared in wakeup interrupt handle */
  78927. + break;
  78928. + }
  78929. +#endif
  78930. + case UHF_PORT_POWER:
  78931. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78932. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  78933. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  78934. + hprt0.b.prtpwr = 0;
  78935. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78936. + break;
  78937. + case UHF_PORT_INDICATOR:
  78938. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78939. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  78940. + /* Port inidicator not supported */
  78941. + break;
  78942. + case UHF_C_PORT_CONNECTION:
  78943. + /* Clears drivers internal connect status change
  78944. + * flag */
  78945. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78946. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  78947. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  78948. + break;
  78949. + case UHF_C_PORT_RESET:
  78950. + /* Clears the driver's internal Port Reset Change
  78951. + * flag */
  78952. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78953. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  78954. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  78955. + break;
  78956. + case UHF_C_PORT_ENABLE:
  78957. + /* Clears the driver's internal Port
  78958. + * Enable/Disable Change flag */
  78959. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78960. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  78961. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  78962. + break;
  78963. + case UHF_C_PORT_SUSPEND:
  78964. + /* Clears the driver's internal Port Suspend
  78965. + * Change flag, which is set when resume signaling on
  78966. + * the host port is complete */
  78967. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78968. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  78969. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  78970. + break;
  78971. +#ifdef CONFIG_USB_DWC_OTG_LPM
  78972. + case UHF_C_PORT_L1:
  78973. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  78974. + break;
  78975. +#endif
  78976. + case UHF_C_PORT_OVER_CURRENT:
  78977. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78978. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  78979. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  78980. + break;
  78981. + default:
  78982. + retval = -DWC_E_INVALID;
  78983. + DWC_ERROR("DWC OTG HCD - "
  78984. + "ClearPortFeature request %xh "
  78985. + "unknown or unsupported\n", wValue);
  78986. + }
  78987. + break;
  78988. + case UCR_GET_HUB_DESCRIPTOR:
  78989. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  78990. + "GetHubDescriptor\n");
  78991. + hub_desc = (usb_hub_descriptor_t *) buf;
  78992. + hub_desc->bDescLength = 9;
  78993. + hub_desc->bDescriptorType = 0x29;
  78994. + hub_desc->bNbrPorts = 1;
  78995. + USETW(hub_desc->wHubCharacteristics, 0x08);
  78996. + hub_desc->bPwrOn2PwrGood = 1;
  78997. + hub_desc->bHubContrCurrent = 0;
  78998. + hub_desc->DeviceRemovable[0] = 0;
  78999. + hub_desc->DeviceRemovable[1] = 0xff;
  79000. + break;
  79001. + case UCR_GET_HUB_STATUS:
  79002. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79003. + "GetHubStatus\n");
  79004. + DWC_MEMSET(buf, 0, 4);
  79005. + break;
  79006. + case UCR_GET_PORT_STATUS:
  79007. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79008. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  79009. + wIndex, dwc_otg_hcd->flags.d32);
  79010. + if (!wIndex || wIndex > 1)
  79011. + goto error;
  79012. +
  79013. + port_status = 0;
  79014. +
  79015. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  79016. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  79017. +
  79018. + if (dwc_otg_hcd->flags.b.port_enable_change)
  79019. + port_status |= (1 << UHF_C_PORT_ENABLE);
  79020. +
  79021. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  79022. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  79023. +
  79024. + if (dwc_otg_hcd->flags.b.port_l1_change)
  79025. + port_status |= (1 << UHF_C_PORT_L1);
  79026. +
  79027. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  79028. + port_status |= (1 << UHF_C_PORT_RESET);
  79029. + }
  79030. +
  79031. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  79032. + DWC_WARN("Overcurrent change detected\n");
  79033. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  79034. + }
  79035. +
  79036. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  79037. + /*
  79038. + * The port is disconnected, which means the core is
  79039. + * either in device mode or it soon will be. Just
  79040. + * return 0's for the remainder of the port status
  79041. + * since the port register can't be read if the core
  79042. + * is in device mode.
  79043. + */
  79044. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  79045. + break;
  79046. + }
  79047. +
  79048. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  79049. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  79050. +
  79051. + if (hprt0.b.prtconnsts)
  79052. + port_status |= (1 << UHF_PORT_CONNECTION);
  79053. +
  79054. + if (hprt0.b.prtena)
  79055. + port_status |= (1 << UHF_PORT_ENABLE);
  79056. +
  79057. + if (hprt0.b.prtsusp)
  79058. + port_status |= (1 << UHF_PORT_SUSPEND);
  79059. +
  79060. + if (hprt0.b.prtovrcurract)
  79061. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  79062. +
  79063. + if (hprt0.b.prtrst)
  79064. + port_status |= (1 << UHF_PORT_RESET);
  79065. +
  79066. + if (hprt0.b.prtpwr)
  79067. + port_status |= (1 << UHF_PORT_POWER);
  79068. +
  79069. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  79070. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  79071. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  79072. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  79073. +
  79074. + if (hprt0.b.prttstctl)
  79075. + port_status |= (1 << UHF_PORT_TEST);
  79076. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  79077. + port_status |= (1 << UHF_PORT_L1);
  79078. + }
  79079. + /*
  79080. + For Synopsys HW emulation of Power down wkup_control asserts the
  79081. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  79082. + We intentionally tell the software that port is in L2Suspend state.
  79083. + Only for STE.
  79084. + */
  79085. + if ((core_if->power_down == 2)
  79086. + && (core_if->hibernation_suspend == 1)) {
  79087. + port_status |= (1 << UHF_PORT_SUSPEND);
  79088. + }
  79089. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  79090. +
  79091. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  79092. +
  79093. + break;
  79094. + case UCR_SET_HUB_FEATURE:
  79095. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79096. + "SetHubFeature\n");
  79097. + /* No HUB features supported */
  79098. + break;
  79099. + case UCR_SET_PORT_FEATURE:
  79100. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  79101. + goto error;
  79102. +
  79103. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  79104. + /*
  79105. + * The port is disconnected, which means the core is
  79106. + * either in device mode or it soon will be. Just
  79107. + * return without doing anything since the port
  79108. + * register can't be written if the core is in device
  79109. + * mode.
  79110. + */
  79111. + break;
  79112. + }
  79113. +
  79114. + switch (wValue) {
  79115. + case UHF_PORT_SUSPEND:
  79116. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79117. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  79118. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  79119. + goto error;
  79120. + }
  79121. + if (core_if->power_down == 2) {
  79122. + int timeout = 300;
  79123. + dwc_irqflags_t flags;
  79124. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  79125. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  79126. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  79127. +#ifdef DWC_DEV_SRPCAP
  79128. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  79129. +#endif
  79130. + DWC_PRINTF("Preparing for complete power-off\n");
  79131. +
  79132. + /* Save registers before hibernation */
  79133. + dwc_otg_save_global_regs(core_if);
  79134. + dwc_otg_save_host_regs(core_if);
  79135. +
  79136. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79137. + hprt0.b.prtsusp = 1;
  79138. + hprt0.b.prtena = 0;
  79139. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79140. + /* Spin hprt0.b.prtsusp to became 1 */
  79141. + do {
  79142. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79143. + if (hprt0.b.prtsusp) {
  79144. + break;
  79145. + }
  79146. + dwc_mdelay(1);
  79147. + } while (--timeout);
  79148. + if (!timeout) {
  79149. + DWC_WARN("Suspend wasn't genereted\n");
  79150. + }
  79151. + dwc_udelay(10);
  79152. +
  79153. + /*
  79154. + * We need to disable interrupts to prevent servicing of any IRQ
  79155. + * during going to hibernation
  79156. + */
  79157. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  79158. + core_if->lx_state = DWC_OTG_L2;
  79159. +#ifdef DWC_DEV_SRPCAP
  79160. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79161. + hprt0.b.prtpwr = 0;
  79162. + hprt0.b.prtena = 0;
  79163. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  79164. + hprt0.d32);
  79165. +#endif
  79166. + gusbcfg.d32 =
  79167. + DWC_READ_REG32(&core_if->core_global_regs->
  79168. + gusbcfg);
  79169. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  79170. + /* ULPI interface */
  79171. + /* Suspend the Phy Clock */
  79172. + pcgcctl.d32 = 0;
  79173. + pcgcctl.b.stoppclk = 1;
  79174. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  79175. + pcgcctl.d32);
  79176. + dwc_udelay(10);
  79177. + gpwrdn.b.pmuactv = 1;
  79178. + DWC_MODIFY_REG32(&core_if->
  79179. + core_global_regs->
  79180. + gpwrdn, 0, gpwrdn.d32);
  79181. + } else {
  79182. + /* UTMI+ Interface */
  79183. + gpwrdn.b.pmuactv = 1;
  79184. + DWC_MODIFY_REG32(&core_if->
  79185. + core_global_regs->
  79186. + gpwrdn, 0, gpwrdn.d32);
  79187. + dwc_udelay(10);
  79188. + pcgcctl.b.stoppclk = 1;
  79189. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  79190. + dwc_udelay(10);
  79191. + }
  79192. +#ifdef DWC_DEV_SRPCAP
  79193. + gpwrdn.d32 = 0;
  79194. + gpwrdn.b.dis_vbus = 1;
  79195. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79196. + gpwrdn, 0, gpwrdn.d32);
  79197. +#endif
  79198. + gpwrdn.d32 = 0;
  79199. + gpwrdn.b.pmuintsel = 1;
  79200. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79201. + gpwrdn, 0, gpwrdn.d32);
  79202. + dwc_udelay(10);
  79203. +
  79204. + gpwrdn.d32 = 0;
  79205. +#ifdef DWC_DEV_SRPCAP
  79206. + gpwrdn.b.srp_det_msk = 1;
  79207. +#endif
  79208. + gpwrdn.b.disconn_det_msk = 1;
  79209. + gpwrdn.b.lnstchng_msk = 1;
  79210. + gpwrdn.b.sts_chngint_msk = 1;
  79211. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79212. + gpwrdn, 0, gpwrdn.d32);
  79213. + dwc_udelay(10);
  79214. +
  79215. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  79216. + gpwrdn.d32 = 0;
  79217. + gpwrdn.b.pwrdnclmp = 1;
  79218. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79219. + gpwrdn, 0, gpwrdn.d32);
  79220. + dwc_udelay(10);
  79221. +
  79222. + /* Switch off VDD */
  79223. + gpwrdn.d32 = 0;
  79224. + gpwrdn.b.pwrdnswtch = 1;
  79225. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79226. + gpwrdn, 0, gpwrdn.d32);
  79227. +
  79228. +#ifdef DWC_DEV_SRPCAP
  79229. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  79230. + {
  79231. + core_if->pwron_timer_started = 1;
  79232. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  79233. + }
  79234. +#endif
  79235. + /* Save gpwrdn register for further usage if stschng interrupt */
  79236. + core_if->gr_backup->gpwrdn_local =
  79237. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  79238. +
  79239. + /* Set flag to indicate that we are in hibernation */
  79240. + core_if->hibernation_suspend = 1;
  79241. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  79242. +
  79243. + DWC_PRINTF("Host hibernation completed\n");
  79244. + // Exit from case statement
  79245. + break;
  79246. +
  79247. + }
  79248. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  79249. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  79250. + gotgctl_data_t gotgctl = {.d32 = 0 };
  79251. + gotgctl.b.hstsethnpen = 1;
  79252. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79253. + gotgctl, 0, gotgctl.d32);
  79254. + core_if->op_state = A_SUSPEND;
  79255. + }
  79256. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79257. + hprt0.b.prtsusp = 1;
  79258. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79259. + {
  79260. + dwc_irqflags_t flags;
  79261. + /* Update lx_state */
  79262. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  79263. + core_if->lx_state = DWC_OTG_L2;
  79264. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  79265. + }
  79266. + /* Suspend the Phy Clock */
  79267. + {
  79268. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  79269. + pcgcctl.b.stoppclk = 1;
  79270. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  79271. + pcgcctl.d32);
  79272. + dwc_udelay(10);
  79273. + }
  79274. +
  79275. + /* For HNP the bus must be suspended for at least 200ms. */
  79276. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  79277. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  79278. + pcgcctl.b.stoppclk = 1;
  79279. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  79280. + dwc_mdelay(200);
  79281. + }
  79282. +
  79283. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  79284. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  79285. + if (core_if->adp_enable) {
  79286. + gotgctl_data_t gotgctl = {.d32 = 0 };
  79287. + gpwrdn_data_t gpwrdn;
  79288. +
  79289. + while (gotgctl.b.asesvld == 1) {
  79290. + gotgctl.d32 =
  79291. + DWC_READ_REG32(&core_if->
  79292. + core_global_regs->
  79293. + gotgctl);
  79294. + dwc_mdelay(100);
  79295. + }
  79296. +
  79297. + /* Enable Power Down Logic */
  79298. + gpwrdn.d32 = 0;
  79299. + gpwrdn.b.pmuactv = 1;
  79300. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79301. + gpwrdn, 0, gpwrdn.d32);
  79302. +
  79303. + /* Unmask SRP detected interrupt from Power Down Logic */
  79304. + gpwrdn.d32 = 0;
  79305. + gpwrdn.b.srp_det_msk = 1;
  79306. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  79307. + gpwrdn, 0, gpwrdn.d32);
  79308. +
  79309. + dwc_otg_adp_probe_start(core_if);
  79310. + }
  79311. +#endif
  79312. + break;
  79313. + case UHF_PORT_POWER:
  79314. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79315. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  79316. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79317. + hprt0.b.prtpwr = 1;
  79318. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79319. + break;
  79320. + case UHF_PORT_RESET:
  79321. + if ((core_if->power_down == 2)
  79322. + && (core_if->hibernation_suspend == 1)) {
  79323. + /* If we are going to exit from Hibernated
  79324. + * state via USB RESET.
  79325. + */
  79326. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  79327. + } else {
  79328. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79329. +
  79330. + DWC_DEBUGPL(DBG_HCD,
  79331. + "DWC OTG HCD HUB CONTROL - "
  79332. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  79333. + {
  79334. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  79335. + pcgcctl.b.enbl_sleep_gating = 1;
  79336. + pcgcctl.b.stoppclk = 1;
  79337. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  79338. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  79339. + }
  79340. +#ifdef CONFIG_USB_DWC_OTG_LPM
  79341. + {
  79342. + glpmcfg_data_t lpmcfg;
  79343. + lpmcfg.d32 =
  79344. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  79345. + if (lpmcfg.b.prt_sleep_sts) {
  79346. + lpmcfg.b.en_utmi_sleep = 0;
  79347. + lpmcfg.b.hird_thres &= (~(1 << 4));
  79348. + DWC_WRITE_REG32
  79349. + (&core_if->core_global_regs->glpmcfg,
  79350. + lpmcfg.d32);
  79351. + dwc_mdelay(1);
  79352. + }
  79353. + }
  79354. +#endif
  79355. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79356. + /* Clear suspend bit if resetting from suspended state. */
  79357. + hprt0.b.prtsusp = 0;
  79358. + /* When B-Host the Port reset bit is set in
  79359. + * the Start HCD Callback function, so that
  79360. + * the reset is started within 1ms of the HNP
  79361. + * success interrupt. */
  79362. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  79363. + hprt0.b.prtpwr = 1;
  79364. + hprt0.b.prtrst = 1;
  79365. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  79366. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  79367. + hprt0.d32);
  79368. + }
  79369. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  79370. + dwc_mdelay(60);
  79371. + hprt0.b.prtrst = 0;
  79372. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79373. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  79374. + }
  79375. + break;
  79376. +#ifdef DWC_HS_ELECT_TST
  79377. + case UHF_PORT_TEST:
  79378. + {
  79379. + uint32_t t;
  79380. + gintmsk_data_t gintmsk;
  79381. +
  79382. + t = (wIndex >> 8); /* MSB wIndex USB */
  79383. + DWC_DEBUGPL(DBG_HCD,
  79384. + "DWC OTG HCD HUB CONTROL - "
  79385. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  79386. + t);
  79387. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  79388. + if (t < 6) {
  79389. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79390. + hprt0.b.prttstctl = t;
  79391. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  79392. + hprt0.d32);
  79393. + } else {
  79394. + /* Setup global vars with reg addresses (quick and
  79395. + * dirty hack, should be cleaned up)
  79396. + */
  79397. + global_regs = core_if->core_global_regs;
  79398. + hc_global_regs =
  79399. + core_if->host_if->host_global_regs;
  79400. + hc_regs =
  79401. + (dwc_otg_hc_regs_t *) ((char *)
  79402. + global_regs +
  79403. + 0x500);
  79404. + data_fifo =
  79405. + (uint32_t *) ((char *)global_regs +
  79406. + 0x1000);
  79407. +
  79408. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  79409. + /* Save current interrupt mask */
  79410. + gintmsk.d32 =
  79411. + DWC_READ_REG32
  79412. + (&global_regs->gintmsk);
  79413. +
  79414. + /* Disable all interrupts while we muck with
  79415. + * the hardware directly
  79416. + */
  79417. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  79418. +
  79419. + /* 15 second delay per the test spec */
  79420. + dwc_mdelay(15000);
  79421. +
  79422. + /* Drive suspend on the root port */
  79423. + hprt0.d32 =
  79424. + dwc_otg_read_hprt0(core_if);
  79425. + hprt0.b.prtsusp = 1;
  79426. + hprt0.b.prtres = 0;
  79427. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79428. +
  79429. + /* 15 second delay per the test spec */
  79430. + dwc_mdelay(15000);
  79431. +
  79432. + /* Drive resume on the root port */
  79433. + hprt0.d32 =
  79434. + dwc_otg_read_hprt0(core_if);
  79435. + hprt0.b.prtsusp = 0;
  79436. + hprt0.b.prtres = 1;
  79437. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79438. + dwc_mdelay(100);
  79439. +
  79440. + /* Clear the resume bit */
  79441. + hprt0.b.prtres = 0;
  79442. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79443. +
  79444. + /* Restore interrupts */
  79445. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  79446. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  79447. + /* Save current interrupt mask */
  79448. + gintmsk.d32 =
  79449. + DWC_READ_REG32
  79450. + (&global_regs->gintmsk);
  79451. +
  79452. + /* Disable all interrupts while we muck with
  79453. + * the hardware directly
  79454. + */
  79455. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  79456. +
  79457. + /* 15 second delay per the test spec */
  79458. + dwc_mdelay(15000);
  79459. +
  79460. + /* Send the Setup packet */
  79461. + do_setup();
  79462. +
  79463. + /* 15 second delay so nothing else happens for awhile */
  79464. + dwc_mdelay(15000);
  79465. +
  79466. + /* Restore interrupts */
  79467. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  79468. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  79469. + /* Save current interrupt mask */
  79470. + gintmsk.d32 =
  79471. + DWC_READ_REG32
  79472. + (&global_regs->gintmsk);
  79473. +
  79474. + /* Disable all interrupts while we muck with
  79475. + * the hardware directly
  79476. + */
  79477. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  79478. +
  79479. + /* Send the Setup packet */
  79480. + do_setup();
  79481. +
  79482. + /* 15 second delay so nothing else happens for awhile */
  79483. + dwc_mdelay(15000);
  79484. +
  79485. + /* Send the In and Ack packets */
  79486. + do_in_ack();
  79487. +
  79488. + /* 15 second delay so nothing else happens for awhile */
  79489. + dwc_mdelay(15000);
  79490. +
  79491. + /* Restore interrupts */
  79492. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  79493. + }
  79494. + }
  79495. + break;
  79496. + }
  79497. +#endif /* DWC_HS_ELECT_TST */
  79498. +
  79499. + case UHF_PORT_INDICATOR:
  79500. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79501. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  79502. + /* Not supported */
  79503. + break;
  79504. + default:
  79505. + retval = -DWC_E_INVALID;
  79506. + DWC_ERROR("DWC OTG HCD - "
  79507. + "SetPortFeature request %xh "
  79508. + "unknown or unsupported\n", wValue);
  79509. + break;
  79510. + }
  79511. + break;
  79512. +#ifdef CONFIG_USB_DWC_OTG_LPM
  79513. + case UCR_SET_AND_TEST_PORT_FEATURE:
  79514. + if (wValue != UHF_PORT_L1) {
  79515. + goto error;
  79516. + }
  79517. + {
  79518. + int portnum, hird, devaddr, remwake;
  79519. + glpmcfg_data_t lpmcfg;
  79520. + uint32_t time_usecs;
  79521. + gintsts_data_t gintsts;
  79522. + gintmsk_data_t gintmsk;
  79523. +
  79524. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  79525. + goto error;
  79526. + }
  79527. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  79528. + goto error;
  79529. + }
  79530. + /* Check if the port currently is in SLEEP state */
  79531. + lpmcfg.d32 =
  79532. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  79533. + if (lpmcfg.b.prt_sleep_sts) {
  79534. + DWC_INFO("Port is already in sleep mode\n");
  79535. + buf[0] = 0; /* Return success */
  79536. + break;
  79537. + }
  79538. +
  79539. + portnum = wIndex & 0xf;
  79540. + hird = (wIndex >> 4) & 0xf;
  79541. + devaddr = (wIndex >> 8) & 0x7f;
  79542. + remwake = (wIndex >> 15);
  79543. +
  79544. + if (portnum != 1) {
  79545. + retval = -DWC_E_INVALID;
  79546. + DWC_WARN
  79547. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  79548. + portnum);
  79549. + break;
  79550. + }
  79551. +
  79552. + DWC_PRINTF
  79553. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  79554. + portnum, hird, devaddr, remwake);
  79555. + /* Disable LPM interrupt */
  79556. + gintmsk.d32 = 0;
  79557. + gintmsk.b.lpmtranrcvd = 1;
  79558. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  79559. + gintmsk.d32, 0);
  79560. +
  79561. + if (dwc_otg_hcd_send_lpm
  79562. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  79563. + retval = -DWC_E_INVALID;
  79564. + break;
  79565. + }
  79566. +
  79567. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  79568. + /* We will consider timeout if time_usecs microseconds pass,
  79569. + * and we don't receive LPM transaction status.
  79570. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  79571. + * core will set lpmtranrcvd bit.
  79572. + */
  79573. + do {
  79574. + gintsts.d32 =
  79575. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  79576. + if (gintsts.b.lpmtranrcvd) {
  79577. + break;
  79578. + }
  79579. + dwc_udelay(1);
  79580. + } while (--time_usecs);
  79581. + /* lpm_int bit will be cleared in LPM interrupt handler */
  79582. +
  79583. + /* Now fill status
  79584. + * 0x00 - Success
  79585. + * 0x10 - NYET
  79586. + * 0x11 - Timeout
  79587. + */
  79588. + if (!gintsts.b.lpmtranrcvd) {
  79589. + buf[0] = 0x3; /* Completion code is Timeout */
  79590. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  79591. + } else {
  79592. + lpmcfg.d32 =
  79593. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  79594. + if (lpmcfg.b.lpm_resp == 0x3) {
  79595. + /* ACK responce from the device */
  79596. + buf[0] = 0x00; /* Success */
  79597. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  79598. + /* NYET responce from the device */
  79599. + buf[0] = 0x2;
  79600. + } else {
  79601. + /* Otherwise responce with Timeout */
  79602. + buf[0] = 0x3;
  79603. + }
  79604. + }
  79605. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  79606. + lpmcfg.b.lpm_resp);
  79607. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  79608. + gintmsk.d32);
  79609. +
  79610. + break;
  79611. + }
  79612. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  79613. + default:
  79614. +error:
  79615. + retval = -DWC_E_INVALID;
  79616. + DWC_WARN("DWC OTG HCD - "
  79617. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  79618. + typeReq, wIndex, wValue);
  79619. + break;
  79620. + }
  79621. +
  79622. + return retval;
  79623. +}
  79624. +
  79625. +#ifdef CONFIG_USB_DWC_OTG_LPM
  79626. +/** Returns index of host channel to perform LPM transaction. */
  79627. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  79628. +{
  79629. + dwc_otg_core_if_t *core_if = hcd->core_if;
  79630. + dwc_hc_t *hc;
  79631. + hcchar_data_t hcchar;
  79632. + gintmsk_data_t gintmsk = {.d32 = 0 };
  79633. +
  79634. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  79635. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  79636. + return -1;
  79637. + }
  79638. +
  79639. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  79640. +
  79641. + /* Mask host channel interrupts. */
  79642. + gintmsk.b.hcintr = 1;
  79643. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  79644. +
  79645. + /* Fill fields that core needs for LPM transaction */
  79646. + hcchar.b.devaddr = devaddr;
  79647. + hcchar.b.epnum = 0;
  79648. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  79649. + hcchar.b.mps = 64;
  79650. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  79651. + hcchar.b.epdir = 0; /* OUT */
  79652. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  79653. + hcchar.d32);
  79654. +
  79655. + /* Remove the host channel from the free list. */
  79656. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  79657. +
  79658. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  79659. +
  79660. + return hc->hc_num;
  79661. +}
  79662. +
  79663. +/** Release hc after performing LPM transaction */
  79664. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  79665. +{
  79666. + dwc_hc_t *hc;
  79667. + glpmcfg_data_t lpmcfg;
  79668. + uint8_t hc_num;
  79669. +
  79670. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  79671. + hc_num = lpmcfg.b.lpm_chan_index;
  79672. +
  79673. + hc = hcd->hc_ptr_array[hc_num];
  79674. +
  79675. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  79676. + /* Return host channel to free list */
  79677. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  79678. +}
  79679. +
  79680. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  79681. + uint8_t bRemoteWake)
  79682. +{
  79683. + glpmcfg_data_t lpmcfg;
  79684. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  79685. + int channel;
  79686. +
  79687. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  79688. + if (channel < 0) {
  79689. + return channel;
  79690. + }
  79691. +
  79692. + pcgcctl.b.enbl_sleep_gating = 1;
  79693. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  79694. +
  79695. + /* Read LPM config register */
  79696. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  79697. +
  79698. + /* Program LPM transaction fields */
  79699. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  79700. + lpmcfg.b.hird = hird;
  79701. + lpmcfg.b.hird_thres = 0x1c;
  79702. + lpmcfg.b.lpm_chan_index = channel;
  79703. + lpmcfg.b.en_utmi_sleep = 1;
  79704. + /* Program LPM config register */
  79705. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  79706. +
  79707. + /* Send LPM transaction */
  79708. + lpmcfg.b.send_lpm = 1;
  79709. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  79710. +
  79711. + return 0;
  79712. +}
  79713. +
  79714. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  79715. +
  79716. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  79717. +{
  79718. + int retval;
  79719. +
  79720. + if (port != 1) {
  79721. + return -DWC_E_INVALID;
  79722. + }
  79723. +
  79724. + retval = (hcd->flags.b.port_connect_status_change ||
  79725. + hcd->flags.b.port_reset_change ||
  79726. + hcd->flags.b.port_enable_change ||
  79727. + hcd->flags.b.port_suspend_change ||
  79728. + hcd->flags.b.port_over_current_change);
  79729. +#ifdef DEBUG
  79730. + if (retval) {
  79731. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  79732. + " Root port status changed\n");
  79733. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  79734. + hcd->flags.b.port_connect_status_change);
  79735. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  79736. + hcd->flags.b.port_reset_change);
  79737. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  79738. + hcd->flags.b.port_enable_change);
  79739. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  79740. + hcd->flags.b.port_suspend_change);
  79741. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  79742. + hcd->flags.b.port_over_current_change);
  79743. + }
  79744. +#endif
  79745. + return retval;
  79746. +}
  79747. +
  79748. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  79749. +{
  79750. + hfnum_data_t hfnum;
  79751. + hfnum.d32 =
  79752. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  79753. + hfnum);
  79754. +
  79755. +#ifdef DEBUG_SOF
  79756. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  79757. + hfnum.b.frnum);
  79758. +#endif
  79759. + return hfnum.b.frnum;
  79760. +}
  79761. +
  79762. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  79763. + struct dwc_otg_hcd_function_ops *fops)
  79764. +{
  79765. + int retval = 0;
  79766. +
  79767. + hcd->fops = fops;
  79768. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  79769. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  79770. + dwc_otg_hcd_reinit(hcd);
  79771. + } else {
  79772. + retval = -DWC_E_NO_DEVICE;
  79773. + }
  79774. +
  79775. + return retval;
  79776. +}
  79777. +
  79778. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  79779. +{
  79780. + return hcd->priv;
  79781. +}
  79782. +
  79783. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  79784. +{
  79785. + hcd->priv = priv_data;
  79786. +}
  79787. +
  79788. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  79789. +{
  79790. + return hcd->otg_port;
  79791. +}
  79792. +
  79793. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  79794. +{
  79795. + uint32_t is_b_host;
  79796. + if (hcd->core_if->op_state == B_HOST) {
  79797. + is_b_host = 1;
  79798. + } else {
  79799. + is_b_host = 0;
  79800. + }
  79801. +
  79802. + return is_b_host;
  79803. +}
  79804. +
  79805. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  79806. + int iso_desc_count, int atomic_alloc)
  79807. +{
  79808. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  79809. + uint32_t size;
  79810. +
  79811. + size =
  79812. + sizeof(*dwc_otg_urb) +
  79813. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  79814. + if (atomic_alloc)
  79815. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  79816. + else
  79817. + dwc_otg_urb = DWC_ALLOC(size);
  79818. +
  79819. + if (dwc_otg_urb)
  79820. + dwc_otg_urb->packet_count = iso_desc_count;
  79821. + else {
  79822. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  79823. + "%salloc of %db failed\n",
  79824. + atomic_alloc?"atomic ":"", size);
  79825. + }
  79826. + return dwc_otg_urb;
  79827. +}
  79828. +
  79829. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  79830. + uint8_t dev_addr, uint8_t ep_num,
  79831. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  79832. +{
  79833. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  79834. + ep_type, ep_dir, mps);
  79835. +#if 0
  79836. + DWC_PRINTF
  79837. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  79838. + dev_addr, ep_num, ep_dir, ep_type, mps);
  79839. +#endif
  79840. +}
  79841. +
  79842. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  79843. + void *urb_handle, void *buf, dwc_dma_t dma,
  79844. + uint32_t buflen, void *setup_packet,
  79845. + dwc_dma_t setup_dma, uint32_t flags,
  79846. + uint16_t interval)
  79847. +{
  79848. + dwc_otg_urb->priv = urb_handle;
  79849. + dwc_otg_urb->buf = buf;
  79850. + dwc_otg_urb->dma = dma;
  79851. + dwc_otg_urb->length = buflen;
  79852. + dwc_otg_urb->setup_packet = setup_packet;
  79853. + dwc_otg_urb->setup_dma = setup_dma;
  79854. + dwc_otg_urb->flags = flags;
  79855. + dwc_otg_urb->interval = interval;
  79856. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  79857. +}
  79858. +
  79859. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  79860. +{
  79861. + return dwc_otg_urb->status;
  79862. +}
  79863. +
  79864. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  79865. +{
  79866. + return dwc_otg_urb->actual_length;
  79867. +}
  79868. +
  79869. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  79870. +{
  79871. + return dwc_otg_urb->error_count;
  79872. +}
  79873. +
  79874. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  79875. + int desc_num, uint32_t offset,
  79876. + uint32_t length)
  79877. +{
  79878. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  79879. + dwc_otg_urb->iso_descs[desc_num].length = length;
  79880. +}
  79881. +
  79882. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  79883. + int desc_num)
  79884. +{
  79885. + return dwc_otg_urb->iso_descs[desc_num].status;
  79886. +}
  79887. +
  79888. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  79889. + dwc_otg_urb, int desc_num)
  79890. +{
  79891. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  79892. +}
  79893. +
  79894. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  79895. +{
  79896. + int allocated = 0;
  79897. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  79898. +
  79899. + if (qh) {
  79900. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  79901. + allocated = 1;
  79902. + }
  79903. + }
  79904. + return allocated;
  79905. +}
  79906. +
  79907. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  79908. +{
  79909. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  79910. + int freed = 0;
  79911. + DWC_ASSERT(qh, "qh is not allocated\n");
  79912. +
  79913. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  79914. + freed = 1;
  79915. + }
  79916. +
  79917. + return freed;
  79918. +}
  79919. +
  79920. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  79921. +{
  79922. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  79923. + DWC_ASSERT(qh, "qh is not allocated\n");
  79924. + return qh->usecs;
  79925. +}
  79926. +
  79927. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  79928. +{
  79929. +#ifdef DEBUG
  79930. + int num_channels;
  79931. + int i;
  79932. + gnptxsts_data_t np_tx_status;
  79933. + hptxsts_data_t p_tx_status;
  79934. +
  79935. + num_channels = hcd->core_if->core_params->host_channels;
  79936. + DWC_PRINTF("\n");
  79937. + DWC_PRINTF
  79938. + ("************************************************************\n");
  79939. + DWC_PRINTF("HCD State:\n");
  79940. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  79941. + for (i = 0; i < num_channels; i++) {
  79942. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  79943. + DWC_PRINTF(" Channel %d:\n", i);
  79944. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  79945. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  79946. + DWC_PRINTF(" speed: %d\n", hc->speed);
  79947. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  79948. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  79949. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  79950. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  79951. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  79952. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  79953. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  79954. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  79955. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  79956. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  79957. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  79958. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  79959. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  79960. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  79961. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  79962. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  79963. + DWC_PRINTF(" requests: %d\n", hc->requests);
  79964. + DWC_PRINTF(" qh: %p\n", hc->qh);
  79965. + if (hc->xfer_started) {
  79966. + hfnum_data_t hfnum;
  79967. + hcchar_data_t hcchar;
  79968. + hctsiz_data_t hctsiz;
  79969. + hcint_data_t hcint;
  79970. + hcintmsk_data_t hcintmsk;
  79971. + hfnum.d32 =
  79972. + DWC_READ_REG32(&hcd->core_if->
  79973. + host_if->host_global_regs->hfnum);
  79974. + hcchar.d32 =
  79975. + DWC_READ_REG32(&hcd->core_if->host_if->
  79976. + hc_regs[i]->hcchar);
  79977. + hctsiz.d32 =
  79978. + DWC_READ_REG32(&hcd->core_if->host_if->
  79979. + hc_regs[i]->hctsiz);
  79980. + hcint.d32 =
  79981. + DWC_READ_REG32(&hcd->core_if->host_if->
  79982. + hc_regs[i]->hcint);
  79983. + hcintmsk.d32 =
  79984. + DWC_READ_REG32(&hcd->core_if->host_if->
  79985. + hc_regs[i]->hcintmsk);
  79986. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  79987. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  79988. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  79989. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  79990. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  79991. + }
  79992. + if (hc->xfer_started && hc->qh) {
  79993. + dwc_otg_qtd_t *qtd;
  79994. + dwc_otg_hcd_urb_t *urb;
  79995. +
  79996. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  79997. + if (!qtd->in_process)
  79998. + break;
  79999. +
  80000. + urb = qtd->urb;
  80001. + DWC_PRINTF(" URB Info:\n");
  80002. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  80003. + if (urb) {
  80004. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  80005. + dwc_otg_hcd_get_dev_addr(&urb->
  80006. + pipe_info),
  80007. + dwc_otg_hcd_get_ep_num(&urb->
  80008. + pipe_info),
  80009. + dwc_otg_hcd_is_pipe_in(&urb->
  80010. + pipe_info) ?
  80011. + "IN" : "OUT");
  80012. + DWC_PRINTF(" Max packet size: %d\n",
  80013. + dwc_otg_hcd_get_mps(&urb->
  80014. + pipe_info));
  80015. + DWC_PRINTF(" transfer_buffer: %p\n",
  80016. + urb->buf);
  80017. + DWC_PRINTF(" transfer_dma: %p\n",
  80018. + (void *)urb->dma);
  80019. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  80020. + urb->length);
  80021. + DWC_PRINTF(" actual_length: %d\n",
  80022. + urb->actual_length);
  80023. + }
  80024. + }
  80025. + }
  80026. + }
  80027. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  80028. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  80029. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  80030. + np_tx_status.d32 =
  80031. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  80032. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  80033. + np_tx_status.b.nptxqspcavail);
  80034. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  80035. + np_tx_status.b.nptxfspcavail);
  80036. + p_tx_status.d32 =
  80037. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  80038. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  80039. + p_tx_status.b.ptxqspcavail);
  80040. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  80041. + dwc_otg_hcd_dump_frrem(hcd);
  80042. + dwc_otg_dump_global_registers(hcd->core_if);
  80043. + dwc_otg_dump_host_registers(hcd->core_if);
  80044. + DWC_PRINTF
  80045. + ("************************************************************\n");
  80046. + DWC_PRINTF("\n");
  80047. +#endif
  80048. +}
  80049. +
  80050. +#ifdef DEBUG
  80051. +void dwc_print_setup_data(uint8_t * setup)
  80052. +{
  80053. + int i;
  80054. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  80055. + DWC_PRINTF("Setup Data = MSB ");
  80056. + for (i = 7; i >= 0; i--)
  80057. + DWC_PRINTF("%02x ", setup[i]);
  80058. + DWC_PRINTF("\n");
  80059. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  80060. + (setup[0] & 0x80) ? "Device-to-Host" :
  80061. + "Host-to-Device");
  80062. + DWC_PRINTF(" bmRequestType Type = ");
  80063. + switch ((setup[0] & 0x60) >> 5) {
  80064. + case 0:
  80065. + DWC_PRINTF("Standard\n");
  80066. + break;
  80067. + case 1:
  80068. + DWC_PRINTF("Class\n");
  80069. + break;
  80070. + case 2:
  80071. + DWC_PRINTF("Vendor\n");
  80072. + break;
  80073. + case 3:
  80074. + DWC_PRINTF("Reserved\n");
  80075. + break;
  80076. + }
  80077. + DWC_PRINTF(" bmRequestType Recipient = ");
  80078. + switch (setup[0] & 0x1f) {
  80079. + case 0:
  80080. + DWC_PRINTF("Device\n");
  80081. + break;
  80082. + case 1:
  80083. + DWC_PRINTF("Interface\n");
  80084. + break;
  80085. + case 2:
  80086. + DWC_PRINTF("Endpoint\n");
  80087. + break;
  80088. + case 3:
  80089. + DWC_PRINTF("Other\n");
  80090. + break;
  80091. + default:
  80092. + DWC_PRINTF("Reserved\n");
  80093. + break;
  80094. + }
  80095. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  80096. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  80097. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  80098. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  80099. + }
  80100. +}
  80101. +#endif
  80102. +
  80103. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  80104. +{
  80105. +#if 0
  80106. + DWC_PRINTF("Frame remaining at SOF:\n");
  80107. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80108. + hcd->frrem_samples, hcd->frrem_accum,
  80109. + (hcd->frrem_samples > 0) ?
  80110. + hcd->frrem_accum / hcd->frrem_samples : 0);
  80111. +
  80112. + DWC_PRINTF("\n");
  80113. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  80114. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80115. + hcd->core_if->hfnum_7_samples,
  80116. + hcd->core_if->hfnum_7_frrem_accum,
  80117. + (hcd->core_if->hfnum_7_samples >
  80118. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  80119. + hcd->core_if->hfnum_7_samples : 0);
  80120. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  80121. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80122. + hcd->core_if->hfnum_0_samples,
  80123. + hcd->core_if->hfnum_0_frrem_accum,
  80124. + (hcd->core_if->hfnum_0_samples >
  80125. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  80126. + hcd->core_if->hfnum_0_samples : 0);
  80127. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  80128. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80129. + hcd->core_if->hfnum_other_samples,
  80130. + hcd->core_if->hfnum_other_frrem_accum,
  80131. + (hcd->core_if->hfnum_other_samples >
  80132. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  80133. + hcd->core_if->hfnum_other_samples : 0);
  80134. +
  80135. + DWC_PRINTF("\n");
  80136. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  80137. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80138. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  80139. + (hcd->hfnum_7_samples_a > 0) ?
  80140. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  80141. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  80142. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80143. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  80144. + (hcd->hfnum_0_samples_a > 0) ?
  80145. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  80146. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  80147. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80148. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  80149. + (hcd->hfnum_other_samples_a > 0) ?
  80150. + hcd->hfnum_other_frrem_accum_a /
  80151. + hcd->hfnum_other_samples_a : 0);
  80152. +
  80153. + DWC_PRINTF("\n");
  80154. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  80155. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80156. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  80157. + (hcd->hfnum_7_samples_b > 0) ?
  80158. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  80159. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  80160. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80161. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  80162. + (hcd->hfnum_0_samples_b > 0) ?
  80163. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  80164. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  80165. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  80166. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  80167. + (hcd->hfnum_other_samples_b > 0) ?
  80168. + hcd->hfnum_other_frrem_accum_b /
  80169. + hcd->hfnum_other_samples_b : 0);
  80170. +#endif
  80171. +}
  80172. +
  80173. +#endif /* DWC_DEVICE_ONLY */
  80174. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  80175. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 1969-12-31 18:00:00.000000000 -0600
  80176. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 2014-12-11 14:02:55.396418001 -0600
  80177. @@ -0,0 +1,1132 @@
  80178. +/*==========================================================================
  80179. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  80180. + * $Revision: #10 $
  80181. + * $Date: 2011/10/20 $
  80182. + * $Change: 1869464 $
  80183. + *
  80184. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  80185. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  80186. + * otherwise expressly agreed to in writing between Synopsys and you.
  80187. + *
  80188. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  80189. + * any End User Software License Agreement or Agreement for Licensed Product
  80190. + * with Synopsys or any supplement thereto. You are permitted to use and
  80191. + * redistribute this Software in source and binary forms, with or without
  80192. + * modification, provided that redistributions of source code must retain this
  80193. + * notice. You may not view, use, disclose, copy or distribute this file or
  80194. + * any information contained herein except pursuant to this license grant from
  80195. + * Synopsys. If you do not agree with this notice, including the disclaimer
  80196. + * below, then you are not authorized to use the Software.
  80197. + *
  80198. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  80199. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80200. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  80201. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  80202. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  80203. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  80204. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  80205. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  80206. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  80207. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  80208. + * DAMAGE.
  80209. + * ========================================================================== */
  80210. +#ifndef DWC_DEVICE_ONLY
  80211. +
  80212. +/** @file
  80213. + * This file contains Descriptor DMA support implementation for host mode.
  80214. + */
  80215. +
  80216. +#include "dwc_otg_hcd.h"
  80217. +#include "dwc_otg_regs.h"
  80218. +
  80219. +extern bool microframe_schedule;
  80220. +
  80221. +static inline uint8_t frame_list_idx(uint16_t frame)
  80222. +{
  80223. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  80224. +}
  80225. +
  80226. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  80227. +{
  80228. + return (idx + inc) &
  80229. + (((speed ==
  80230. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  80231. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  80232. +}
  80233. +
  80234. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  80235. +{
  80236. + return (idx - inc) &
  80237. + (((speed ==
  80238. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  80239. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  80240. +}
  80241. +
  80242. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  80243. +{
  80244. + return (((qh->ep_type == UE_ISOCHRONOUS)
  80245. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  80246. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  80247. +}
  80248. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  80249. +{
  80250. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  80251. + ? ((qh->interval + 8 - 1) / 8)
  80252. + : qh->interval);
  80253. +}
  80254. +
  80255. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  80256. +{
  80257. + int retval = 0;
  80258. +
  80259. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  80260. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  80261. + &qh->desc_list_dma);
  80262. +
  80263. + if (!qh->desc_list) {
  80264. + retval = -DWC_E_NO_MEMORY;
  80265. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  80266. +
  80267. + }
  80268. +
  80269. + dwc_memset(qh->desc_list, 0x00,
  80270. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  80271. +
  80272. + qh->n_bytes =
  80273. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  80274. +
  80275. + if (!qh->n_bytes) {
  80276. + retval = -DWC_E_NO_MEMORY;
  80277. + DWC_ERROR
  80278. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  80279. + __func__);
  80280. +
  80281. + }
  80282. + return retval;
  80283. +
  80284. +}
  80285. +
  80286. +static void desc_list_free(dwc_otg_qh_t * qh)
  80287. +{
  80288. + if (qh->desc_list) {
  80289. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  80290. + qh->desc_list_dma);
  80291. + qh->desc_list = NULL;
  80292. + }
  80293. +
  80294. + if (qh->n_bytes) {
  80295. + DWC_FREE(qh->n_bytes);
  80296. + qh->n_bytes = NULL;
  80297. + }
  80298. +}
  80299. +
  80300. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  80301. +{
  80302. + int retval = 0;
  80303. + if (hcd->frame_list)
  80304. + return 0;
  80305. +
  80306. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  80307. + &hcd->frame_list_dma);
  80308. + if (!hcd->frame_list) {
  80309. + retval = -DWC_E_NO_MEMORY;
  80310. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  80311. + }
  80312. +
  80313. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  80314. +
  80315. + return retval;
  80316. +}
  80317. +
  80318. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  80319. +{
  80320. + if (!hcd->frame_list)
  80321. + return;
  80322. +
  80323. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  80324. + hcd->frame_list = NULL;
  80325. +}
  80326. +
  80327. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  80328. +{
  80329. +
  80330. + hcfg_data_t hcfg;
  80331. +
  80332. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  80333. +
  80334. + if (hcfg.b.perschedena) {
  80335. + /* already enabled */
  80336. + return;
  80337. + }
  80338. +
  80339. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  80340. + hcd->frame_list_dma);
  80341. +
  80342. + switch (fr_list_en) {
  80343. + case 64:
  80344. + hcfg.b.frlisten = 3;
  80345. + break;
  80346. + case 32:
  80347. + hcfg.b.frlisten = 2;
  80348. + break;
  80349. + case 16:
  80350. + hcfg.b.frlisten = 1;
  80351. + break;
  80352. + case 8:
  80353. + hcfg.b.frlisten = 0;
  80354. + break;
  80355. + default:
  80356. + break;
  80357. + }
  80358. +
  80359. + hcfg.b.perschedena = 1;
  80360. +
  80361. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  80362. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  80363. +
  80364. +}
  80365. +
  80366. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  80367. +{
  80368. + hcfg_data_t hcfg;
  80369. +
  80370. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  80371. +
  80372. + if (!hcfg.b.perschedena) {
  80373. + /* already disabled */
  80374. + return;
  80375. + }
  80376. + hcfg.b.perschedena = 0;
  80377. +
  80378. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  80379. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  80380. +}
  80381. +
  80382. +/*
  80383. + * Activates/Deactivates FrameList entries for the channel
  80384. + * based on endpoint servicing period.
  80385. + */
  80386. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  80387. +{
  80388. + uint16_t i, j, inc;
  80389. + dwc_hc_t *hc = NULL;
  80390. +
  80391. + if (!qh->channel) {
  80392. + DWC_ERROR("qh->channel = %p", qh->channel);
  80393. + return;
  80394. + }
  80395. +
  80396. + if (!hcd) {
  80397. + DWC_ERROR("------hcd = %p", hcd);
  80398. + return;
  80399. + }
  80400. +
  80401. + if (!hcd->frame_list) {
  80402. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  80403. + return;
  80404. + }
  80405. +
  80406. + hc = qh->channel;
  80407. + inc = frame_incr_val(qh);
  80408. + if (qh->ep_type == UE_ISOCHRONOUS)
  80409. + i = frame_list_idx(qh->sched_frame);
  80410. + else
  80411. + i = 0;
  80412. +
  80413. + j = i;
  80414. + do {
  80415. + if (enable)
  80416. + hcd->frame_list[j] |= (1 << hc->hc_num);
  80417. + else
  80418. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  80419. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  80420. + }
  80421. + while (j != i);
  80422. + if (!enable)
  80423. + return;
  80424. + hc->schinfo = 0;
  80425. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  80426. + j = 1;
  80427. + /* TODO - check this */
  80428. + inc = (8 + qh->interval - 1) / qh->interval;
  80429. + for (i = 0; i < inc; i++) {
  80430. + hc->schinfo |= j;
  80431. + j = j << qh->interval;
  80432. + }
  80433. + } else {
  80434. + hc->schinfo = 0xff;
  80435. + }
  80436. +}
  80437. +
  80438. +#if 1
  80439. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  80440. +{
  80441. + int i = 0;
  80442. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  80443. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  80444. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  80445. + if (!(i % 8) && i)
  80446. + DWC_PRINTF("\n");
  80447. + }
  80448. + DWC_PRINTF("\n----\n");
  80449. +
  80450. +}
  80451. +#endif
  80452. +
  80453. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  80454. +{
  80455. + dwc_irqflags_t flags;
  80456. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  80457. +
  80458. + dwc_hc_t *hc = qh->channel;
  80459. + if (dwc_qh_is_non_per(qh)) {
  80460. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  80461. + if (!microframe_schedule)
  80462. + hcd->non_periodic_channels--;
  80463. + else
  80464. + hcd->available_host_channels++;
  80465. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80466. + } else
  80467. + update_frame_list(hcd, qh, 0);
  80468. +
  80469. + /*
  80470. + * The condition is added to prevent double cleanup try in case of device
  80471. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  80472. + */
  80473. + if (hc->qh) {
  80474. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  80475. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  80476. + hc->qh = NULL;
  80477. + }
  80478. +
  80479. + qh->channel = NULL;
  80480. + qh->ntd = 0;
  80481. +
  80482. + if (qh->desc_list) {
  80483. + dwc_memset(qh->desc_list, 0x00,
  80484. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  80485. + }
  80486. +}
  80487. +
  80488. +/**
  80489. + * Initializes a QH structure's Descriptor DMA related members.
  80490. + * Allocates memory for descriptor list.
  80491. + * On first periodic QH, allocates memory for FrameList
  80492. + * and enables periodic scheduling.
  80493. + *
  80494. + * @param hcd The HCD state structure for the DWC OTG controller.
  80495. + * @param qh The QH to init.
  80496. + *
  80497. + * @return 0 if successful, negative error code otherwise.
  80498. + */
  80499. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  80500. +{
  80501. + int retval = 0;
  80502. +
  80503. + if (qh->do_split) {
  80504. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  80505. + return -1;
  80506. + }
  80507. +
  80508. + retval = desc_list_alloc(qh);
  80509. +
  80510. + if ((retval == 0)
  80511. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  80512. + if (!hcd->frame_list) {
  80513. + retval = frame_list_alloc(hcd);
  80514. + /* Enable periodic schedule on first periodic QH */
  80515. + if (retval == 0)
  80516. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  80517. + }
  80518. + }
  80519. +
  80520. + qh->ntd = 0;
  80521. +
  80522. + return retval;
  80523. +}
  80524. +
  80525. +/**
  80526. + * Frees descriptor list memory associated with the QH.
  80527. + * If QH is periodic and the last, frees FrameList memory
  80528. + * and disables periodic scheduling.
  80529. + *
  80530. + * @param hcd The HCD state structure for the DWC OTG controller.
  80531. + * @param qh The QH to init.
  80532. + */
  80533. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  80534. +{
  80535. + desc_list_free(qh);
  80536. +
  80537. + /*
  80538. + * Channel still assigned due to some reasons.
  80539. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  80540. + * ChHalted interrupt to release the channel. Afterwards
  80541. + * when it comes here from endpoint disable routine
  80542. + * channel remains assigned.
  80543. + */
  80544. + if (qh->channel)
  80545. + release_channel_ddma(hcd, qh);
  80546. +
  80547. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  80548. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  80549. +
  80550. + per_sched_disable(hcd);
  80551. + frame_list_free(hcd);
  80552. + }
  80553. +}
  80554. +
  80555. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  80556. +{
  80557. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  80558. + /*
  80559. + * Descriptor set(8 descriptors) index
  80560. + * which is 8-aligned.
  80561. + */
  80562. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  80563. + } else {
  80564. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  80565. + }
  80566. +}
  80567. +
  80568. +/*
  80569. + * Determine starting frame for Isochronous transfer.
  80570. + * Few frames skipped to prevent race condition with HC.
  80571. + */
  80572. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  80573. + uint8_t * skip_frames)
  80574. +{
  80575. + uint16_t frame = 0;
  80576. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  80577. +
  80578. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  80579. +
  80580. + /*
  80581. + * skip_frames is used to limit activated descriptors number
  80582. + * to avoid the situation when HC services the last activated
  80583. + * descriptor firstly.
  80584. + * Example for FS:
  80585. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  80586. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  80587. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  80588. + * list will be fully programmed with Active descriptors and it is possible
  80589. + * case(rare) that the latest descriptor(considering rollback) corresponding
  80590. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  80591. + * up to 11 uframes(16 in the code) may be skipped.
  80592. + */
  80593. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  80594. + /*
  80595. + * Consider uframe counter also, to start xfer asap.
  80596. + * If half of the frame elapsed skip 2 frames otherwise
  80597. + * just 1 frame.
  80598. + * Starting descriptor index must be 8-aligned, so
  80599. + * if the current frame is near to complete the next one
  80600. + * is skipped as well.
  80601. + */
  80602. +
  80603. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  80604. + *skip_frames = 2 * 8;
  80605. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  80606. + } else {
  80607. + *skip_frames = 1 * 8;
  80608. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  80609. + }
  80610. +
  80611. + frame = dwc_full_frame_num(frame);
  80612. + } else {
  80613. + /*
  80614. + * Two frames are skipped for FS - the current and the next.
  80615. + * But for descriptor programming, 1 frame(descriptor) is enough,
  80616. + * see example above.
  80617. + */
  80618. + *skip_frames = 1;
  80619. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  80620. + }
  80621. +
  80622. + return frame;
  80623. +}
  80624. +
  80625. +/*
  80626. + * Calculate initial descriptor index for isochronous transfer
  80627. + * based on scheduled frame.
  80628. + */
  80629. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  80630. +{
  80631. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  80632. + uint8_t skip_frames = 0;
  80633. + /*
  80634. + * With current ISOC processing algorithm the channel is being
  80635. + * released when no more QTDs in the list(qh->ntd == 0).
  80636. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  80637. + *
  80638. + * So qh->channel != NULL branch is not used and just not removed from the
  80639. + * source file. It is required for another possible approach which is,
  80640. + * do not disable and release the channel when ISOC session completed,
  80641. + * just move QH to inactive schedule until new QTD arrives.
  80642. + * On new QTD, the QH moved back to 'ready' schedule,
  80643. + * starting frame and therefore starting desc_index are recalculated.
  80644. + * In this case channel is released only on ep_disable.
  80645. + */
  80646. +
  80647. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  80648. + if (qh->channel) {
  80649. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  80650. + /*
  80651. + * Calculate initial descriptor index based on FrameList current bitmap
  80652. + * and servicing period.
  80653. + */
  80654. + fr_idx_tmp = frame_list_idx(frame);
  80655. + fr_idx =
  80656. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  80657. + fr_idx_tmp)
  80658. + % frame_incr_val(qh);
  80659. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  80660. + } else {
  80661. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  80662. + fr_idx = frame_list_idx(qh->sched_frame);
  80663. + }
  80664. +
  80665. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  80666. +
  80667. + return skip_frames;
  80668. +}
  80669. +
  80670. +#define ISOC_URB_GIVEBACK_ASAP
  80671. +
  80672. +#define MAX_ISOC_XFER_SIZE_FS 1023
  80673. +#define MAX_ISOC_XFER_SIZE_HS 3072
  80674. +#define DESCNUM_THRESHOLD 4
  80675. +
  80676. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  80677. + uint8_t skip_frames)
  80678. +{
  80679. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  80680. + dwc_otg_qtd_t *qtd;
  80681. + dwc_otg_host_dma_desc_t *dma_desc;
  80682. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  80683. +
  80684. + idx = qh->td_last;
  80685. + inc = qh->interval;
  80686. + n_desc = 0;
  80687. +
  80688. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  80689. + if (skip_frames && !qh->channel)
  80690. + ntd_max = ntd_max - skip_frames / qh->interval;
  80691. +
  80692. + max_xfer_size =
  80693. + (qh->dev_speed ==
  80694. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  80695. + MAX_ISOC_XFER_SIZE_FS;
  80696. +
  80697. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  80698. + while ((qh->ntd < ntd_max)
  80699. + && (qtd->isoc_frame_index_last <
  80700. + qtd->urb->packet_count)) {
  80701. +
  80702. + dma_desc = &qh->desc_list[idx];
  80703. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  80704. +
  80705. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  80706. +
  80707. + if (frame_desc->length > max_xfer_size)
  80708. + qh->n_bytes[idx] = max_xfer_size;
  80709. + else
  80710. + qh->n_bytes[idx] = frame_desc->length;
  80711. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  80712. + dma_desc->status.b_isoc.a = 1;
  80713. + dma_desc->status.b_isoc.sts = 0;
  80714. +
  80715. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  80716. +
  80717. + qh->ntd++;
  80718. +
  80719. + qtd->isoc_frame_index_last++;
  80720. +
  80721. +#ifdef ISOC_URB_GIVEBACK_ASAP
  80722. + /*
  80723. + * Set IOC for each descriptor corresponding to the
  80724. + * last frame of the URB.
  80725. + */
  80726. + if (qtd->isoc_frame_index_last ==
  80727. + qtd->urb->packet_count)
  80728. + dma_desc->status.b_isoc.ioc = 1;
  80729. +
  80730. +#endif
  80731. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  80732. + n_desc++;
  80733. +
  80734. + }
  80735. + qtd->in_process = 1;
  80736. + }
  80737. +
  80738. + qh->td_last = idx;
  80739. +
  80740. +#ifdef ISOC_URB_GIVEBACK_ASAP
  80741. + /* Set IOC for the last descriptor if descriptor list is full */
  80742. + if (qh->ntd == ntd_max) {
  80743. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  80744. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  80745. + }
  80746. +#else
  80747. + /*
  80748. + * Set IOC bit only for one descriptor.
  80749. + * Always try to be ahead of HW processing,
  80750. + * i.e. on IOC generation driver activates next descriptors but
  80751. + * core continues to process descriptors followed the one with IOC set.
  80752. + */
  80753. +
  80754. + if (n_desc > DESCNUM_THRESHOLD) {
  80755. + /*
  80756. + * Move IOC "up". Required even if there is only one QTD
  80757. + * in the list, cause QTDs migth continue to be queued,
  80758. + * but during the activation it was only one queued.
  80759. + * Actually more than one QTD might be in the list if this function called
  80760. + * from XferCompletion - QTDs was queued during HW processing of the previous
  80761. + * descriptor chunk.
  80762. + */
  80763. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  80764. + } else {
  80765. + /*
  80766. + * Set the IOC for the latest descriptor
  80767. + * if either number of descriptor is not greather than threshold
  80768. + * or no more new descriptors activated.
  80769. + */
  80770. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  80771. + }
  80772. +
  80773. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  80774. +#endif
  80775. +}
  80776. +
  80777. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  80778. +{
  80779. +
  80780. + dwc_hc_t *hc;
  80781. + dwc_otg_host_dma_desc_t *dma_desc;
  80782. + dwc_otg_qtd_t *qtd;
  80783. + int num_packets, len, n_desc = 0;
  80784. +
  80785. + hc = qh->channel;
  80786. +
  80787. + /*
  80788. + * Start with hc->xfer_buff initialized in
  80789. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  80790. + * this pointer re-assigned to the buffer of the currently processed QTD.
  80791. + * For non-SG request there is always one QTD active.
  80792. + */
  80793. +
  80794. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  80795. +
  80796. + if (n_desc) {
  80797. + /* SG request - more than 1 QTDs */
  80798. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  80799. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  80800. + }
  80801. +
  80802. + qtd->n_desc = 0;
  80803. +
  80804. + do {
  80805. + dma_desc = &qh->desc_list[n_desc];
  80806. + len = hc->xfer_len;
  80807. +
  80808. + if (len > MAX_DMA_DESC_SIZE)
  80809. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  80810. +
  80811. + if (hc->ep_is_in) {
  80812. + if (len > 0) {
  80813. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  80814. + } else {
  80815. + /* Need 1 packet for transfer length of 0. */
  80816. + num_packets = 1;
  80817. + }
  80818. + /* Always program an integral # of max packets for IN transfers. */
  80819. + len = num_packets * hc->max_packet;
  80820. + }
  80821. +
  80822. + dma_desc->status.b.n_bytes = len;
  80823. +
  80824. + qh->n_bytes[n_desc] = len;
  80825. +
  80826. + if ((qh->ep_type == UE_CONTROL)
  80827. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  80828. + dma_desc->status.b.sup = 1; /* Setup Packet */
  80829. +
  80830. + dma_desc->status.b.a = 1; /* Active descriptor */
  80831. + dma_desc->status.b.sts = 0;
  80832. +
  80833. + dma_desc->buf =
  80834. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  80835. +
  80836. + /*
  80837. + * Last descriptor(or single) of IN transfer
  80838. + * with actual size less than MaxPacket.
  80839. + */
  80840. + if (len > hc->xfer_len) {
  80841. + hc->xfer_len = 0;
  80842. + } else {
  80843. + hc->xfer_buff += len;
  80844. + hc->xfer_len -= len;
  80845. + }
  80846. +
  80847. + qtd->n_desc++;
  80848. + n_desc++;
  80849. + }
  80850. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  80851. +
  80852. +
  80853. + qtd->in_process = 1;
  80854. +
  80855. + if (qh->ep_type == UE_CONTROL)
  80856. + break;
  80857. +
  80858. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  80859. + break;
  80860. + }
  80861. +
  80862. + if (n_desc) {
  80863. + /* Request Transfer Complete interrupt for the last descriptor */
  80864. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  80865. + /* End of List indicator */
  80866. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  80867. +
  80868. + hc->ntd = n_desc;
  80869. + }
  80870. +}
  80871. +
  80872. +/**
  80873. + * For Control and Bulk endpoints initializes descriptor list
  80874. + * and starts the transfer.
  80875. + *
  80876. + * For Interrupt and Isochronous endpoints initializes descriptor list
  80877. + * then updates FrameList, marking appropriate entries as active.
  80878. + * In case of Isochronous, the starting descriptor index is calculated based
  80879. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  80880. + * Then starts the transfer via enabling the channel.
  80881. + * For Isochronous endpoint the channel is not halted on XferComplete
  80882. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  80883. + *
  80884. + * @param hcd The HCD state structure for the DWC OTG controller.
  80885. + * @param qh The QH to init.
  80886. + *
  80887. + * @return 0 if successful, negative error code otherwise.
  80888. + */
  80889. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  80890. +{
  80891. + /* Channel is already assigned */
  80892. + dwc_hc_t *hc = qh->channel;
  80893. + uint8_t skip_frames = 0;
  80894. +
  80895. + switch (hc->ep_type) {
  80896. + case DWC_OTG_EP_TYPE_CONTROL:
  80897. + case DWC_OTG_EP_TYPE_BULK:
  80898. + init_non_isoc_dma_desc(hcd, qh);
  80899. +
  80900. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  80901. + break;
  80902. + case DWC_OTG_EP_TYPE_INTR:
  80903. + init_non_isoc_dma_desc(hcd, qh);
  80904. +
  80905. + update_frame_list(hcd, qh, 1);
  80906. +
  80907. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  80908. + break;
  80909. + case DWC_OTG_EP_TYPE_ISOC:
  80910. +
  80911. + if (!qh->ntd)
  80912. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  80913. +
  80914. + init_isoc_dma_desc(hcd, qh, skip_frames);
  80915. +
  80916. + if (!hc->xfer_started) {
  80917. +
  80918. + update_frame_list(hcd, qh, 1);
  80919. +
  80920. + /*
  80921. + * Always set to max, instead of actual size.
  80922. + * Otherwise ntd will be changed with
  80923. + * channel being enabled. Not recommended.
  80924. + *
  80925. + */
  80926. + hc->ntd = max_desc_num(qh);
  80927. + /* Enable channel only once for ISOC */
  80928. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  80929. + }
  80930. +
  80931. + break;
  80932. + default:
  80933. +
  80934. + break;
  80935. + }
  80936. +}
  80937. +
  80938. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  80939. + dwc_hc_t * hc,
  80940. + dwc_otg_hc_regs_t * hc_regs,
  80941. + dwc_otg_halt_status_e halt_status)
  80942. +{
  80943. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  80944. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  80945. + dwc_otg_qh_t *qh;
  80946. + dwc_otg_host_dma_desc_t *dma_desc;
  80947. + uint16_t idx, remain;
  80948. + uint8_t urb_compl;
  80949. +
  80950. + qh = hc->qh;
  80951. + idx = qh->td_first;
  80952. +
  80953. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  80954. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  80955. + qtd->in_process = 0;
  80956. + return;
  80957. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  80958. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  80959. + /*
  80960. + * Channel is halted in these error cases.
  80961. + * Considered as serious issues.
  80962. + * Complete all URBs marking all frames as failed,
  80963. + * irrespective whether some of the descriptors(frames) succeeded or no.
  80964. + * Pass error code to completion routine as well, to
  80965. + * update urb->status, some of class drivers might use it to stop
  80966. + * queing transfer requests.
  80967. + */
  80968. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  80969. + ? (-DWC_E_IO)
  80970. + : (-DWC_E_OVERFLOW);
  80971. +
  80972. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  80973. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  80974. + frame_desc = &qtd->urb->iso_descs[idx];
  80975. + frame_desc->status = err;
  80976. + }
  80977. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  80978. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  80979. + }
  80980. + return;
  80981. + }
  80982. +
  80983. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  80984. +
  80985. + if (!qtd->in_process)
  80986. + break;
  80987. +
  80988. + urb_compl = 0;
  80989. +
  80990. + do {
  80991. +
  80992. + dma_desc = &qh->desc_list[idx];
  80993. +
  80994. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  80995. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  80996. +
  80997. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  80998. + /*
  80999. + * XactError or, unable to complete all the transactions
  81000. + * in the scheduled micro-frame/frame,
  81001. + * both indicated by DMA_DESC_STS_PKTERR.
  81002. + */
  81003. + qtd->urb->error_count++;
  81004. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  81005. + frame_desc->status = -DWC_E_PROTOCOL;
  81006. + } else {
  81007. + /* Success */
  81008. +
  81009. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  81010. + frame_desc->status = 0;
  81011. + }
  81012. +
  81013. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  81014. + /*
  81015. + * urb->status is not used for isoc transfers here.
  81016. + * The individual frame_desc status are used instead.
  81017. + */
  81018. +
  81019. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  81020. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  81021. +
  81022. + /*
  81023. + * This check is necessary because urb_dequeue can be called
  81024. + * from urb complete callback(sound driver example).
  81025. + * All pending URBs are dequeued there, so no need for
  81026. + * further processing.
  81027. + */
  81028. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  81029. + return;
  81030. + }
  81031. +
  81032. + urb_compl = 1;
  81033. +
  81034. + }
  81035. +
  81036. + qh->ntd--;
  81037. +
  81038. + /* Stop if IOC requested descriptor reached */
  81039. + if (dma_desc->status.b_isoc.ioc) {
  81040. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  81041. + goto stop_scan;
  81042. + }
  81043. +
  81044. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  81045. +
  81046. + if (urb_compl)
  81047. + break;
  81048. + }
  81049. + while (idx != qh->td_first);
  81050. + }
  81051. +stop_scan:
  81052. + qh->td_first = idx;
  81053. +}
  81054. +
  81055. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  81056. + dwc_hc_t * hc,
  81057. + dwc_otg_qtd_t * qtd,
  81058. + dwc_otg_host_dma_desc_t * dma_desc,
  81059. + dwc_otg_halt_status_e halt_status,
  81060. + uint32_t n_bytes, uint8_t * xfer_done)
  81061. +{
  81062. +
  81063. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  81064. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  81065. +
  81066. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  81067. + urb->status = -DWC_E_IO;
  81068. + return 1;
  81069. + }
  81070. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  81071. + switch (halt_status) {
  81072. + case DWC_OTG_HC_XFER_STALL:
  81073. + urb->status = -DWC_E_PIPE;
  81074. + break;
  81075. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  81076. + urb->status = -DWC_E_OVERFLOW;
  81077. + break;
  81078. + case DWC_OTG_HC_XFER_XACT_ERR:
  81079. + urb->status = -DWC_E_PROTOCOL;
  81080. + break;
  81081. + default:
  81082. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  81083. + halt_status);
  81084. + break;
  81085. + }
  81086. + return 1;
  81087. + }
  81088. +
  81089. + if (dma_desc->status.b.a == 1) {
  81090. + DWC_DEBUGPL(DBG_HCDV,
  81091. + "Active descriptor encountered on channel %d\n",
  81092. + hc->hc_num);
  81093. + return 0;
  81094. + }
  81095. +
  81096. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  81097. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  81098. + urb->actual_length += n_bytes - remain;
  81099. + if (remain || urb->actual_length == urb->length) {
  81100. + /*
  81101. + * For Control Data stage do not set urb->status=0 to prevent
  81102. + * URB callback. Set it when Status phase done. See below.
  81103. + */
  81104. + *xfer_done = 1;
  81105. + }
  81106. +
  81107. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  81108. + urb->status = 0;
  81109. + *xfer_done = 1;
  81110. + }
  81111. + /* No handling for SETUP stage */
  81112. + } else {
  81113. + /* BULK and INTR */
  81114. + urb->actual_length += n_bytes - remain;
  81115. + if (remain || urb->actual_length == urb->length) {
  81116. + urb->status = 0;
  81117. + *xfer_done = 1;
  81118. + }
  81119. + }
  81120. +
  81121. + return 0;
  81122. +}
  81123. +
  81124. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  81125. + dwc_hc_t * hc,
  81126. + dwc_otg_hc_regs_t * hc_regs,
  81127. + dwc_otg_halt_status_e halt_status)
  81128. +{
  81129. + dwc_otg_hcd_urb_t *urb = NULL;
  81130. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  81131. + dwc_otg_qh_t *qh;
  81132. + dwc_otg_host_dma_desc_t *dma_desc;
  81133. + uint32_t n_bytes, n_desc, i;
  81134. + uint8_t failed = 0, xfer_done;
  81135. +
  81136. + n_desc = 0;
  81137. +
  81138. + qh = hc->qh;
  81139. +
  81140. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  81141. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  81142. + qtd->in_process = 0;
  81143. + }
  81144. + return;
  81145. + }
  81146. +
  81147. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  81148. +
  81149. + urb = qtd->urb;
  81150. +
  81151. + n_bytes = 0;
  81152. + xfer_done = 0;
  81153. +
  81154. + for (i = 0; i < qtd->n_desc; i++) {
  81155. + dma_desc = &qh->desc_list[n_desc];
  81156. +
  81157. + n_bytes = qh->n_bytes[n_desc];
  81158. +
  81159. + failed =
  81160. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  81161. + dma_desc,
  81162. + halt_status, n_bytes,
  81163. + &xfer_done);
  81164. +
  81165. + if (failed
  81166. + || (xfer_done
  81167. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  81168. +
  81169. + hcd->fops->complete(hcd, urb->priv, urb,
  81170. + urb->status);
  81171. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  81172. +
  81173. + if (failed)
  81174. + goto stop_scan;
  81175. + } else if (qh->ep_type == UE_CONTROL) {
  81176. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  81177. + if (urb->length > 0) {
  81178. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  81179. + } else {
  81180. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  81181. + }
  81182. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  81183. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  81184. + if (xfer_done) {
  81185. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  81186. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  81187. + } else if (i + 1 == qtd->n_desc) {
  81188. + /*
  81189. + * Last descriptor for Control data stage which is
  81190. + * not completed yet.
  81191. + */
  81192. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  81193. + }
  81194. + }
  81195. + }
  81196. +
  81197. + n_desc++;
  81198. + }
  81199. +
  81200. + }
  81201. +
  81202. +stop_scan:
  81203. +
  81204. + if (qh->ep_type != UE_CONTROL) {
  81205. + /*
  81206. + * Resetting the data toggle for bulk
  81207. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  81208. + */
  81209. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  81210. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  81211. + else
  81212. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  81213. + }
  81214. +
  81215. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  81216. + hcint_data_t hcint;
  81217. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  81218. + if (hcint.b.nyet) {
  81219. + /*
  81220. + * Got a NYET on the last transaction of the transfer. It
  81221. + * means that the endpoint should be in the PING state at the
  81222. + * beginning of the next transfer.
  81223. + */
  81224. + qh->ping_state = 1;
  81225. + clear_hc_int(hc_regs, nyet);
  81226. + }
  81227. +
  81228. + }
  81229. +
  81230. +}
  81231. +
  81232. +/**
  81233. + * This function is called from interrupt handlers.
  81234. + * Scans the descriptor list, updates URB's status and
  81235. + * calls completion routine for the URB if it's done.
  81236. + * Releases the channel to be used by other transfers.
  81237. + * In case of Isochronous endpoint the channel is not halted until
  81238. + * the end of the session, i.e. QTD list is empty.
  81239. + * If periodic channel released the FrameList is updated accordingly.
  81240. + *
  81241. + * Calls transaction selection routines to activate pending transfers.
  81242. + *
  81243. + * @param hcd The HCD state structure for the DWC OTG controller.
  81244. + * @param hc Host channel, the transfer is completed on.
  81245. + * @param hc_regs Host channel registers.
  81246. + * @param halt_status Reason the channel is being halted,
  81247. + * or just XferComplete for isochronous transfer
  81248. + */
  81249. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  81250. + dwc_hc_t * hc,
  81251. + dwc_otg_hc_regs_t * hc_regs,
  81252. + dwc_otg_halt_status_e halt_status)
  81253. +{
  81254. + uint8_t continue_isoc_xfer = 0;
  81255. + dwc_otg_transaction_type_e tr_type;
  81256. + dwc_otg_qh_t *qh = hc->qh;
  81257. +
  81258. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  81259. +
  81260. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  81261. +
  81262. + /* Release the channel if halted or session completed */
  81263. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  81264. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  81265. +
  81266. + /* Halt the channel if session completed */
  81267. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  81268. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  81269. + }
  81270. +
  81271. + release_channel_ddma(hcd, qh);
  81272. + dwc_otg_hcd_qh_remove(hcd, qh);
  81273. + } else {
  81274. + /* Keep in assigned schedule to continue transfer */
  81275. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  81276. + &qh->qh_list_entry);
  81277. + continue_isoc_xfer = 1;
  81278. +
  81279. + }
  81280. + /** @todo Consider the case when period exceeds FrameList size.
  81281. + * Frame Rollover interrupt should be used.
  81282. + */
  81283. + } else {
  81284. + /* Scan descriptor list to complete the URB(s), then release the channel */
  81285. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  81286. +
  81287. + release_channel_ddma(hcd, qh);
  81288. + dwc_otg_hcd_qh_remove(hcd, qh);
  81289. +
  81290. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  81291. + /* Add back to inactive non-periodic schedule on normal completion */
  81292. + dwc_otg_hcd_qh_add(hcd, qh);
  81293. + }
  81294. +
  81295. + }
  81296. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  81297. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  81298. + if (continue_isoc_xfer) {
  81299. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  81300. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  81301. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  81302. + tr_type = DWC_OTG_TRANSACTION_ALL;
  81303. + }
  81304. + }
  81305. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  81306. + }
  81307. +}
  81308. +
  81309. +#endif /* DWC_DEVICE_ONLY */
  81310. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  81311. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 1969-12-31 18:00:00.000000000 -0600
  81312. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 2014-12-11 14:02:55.396418001 -0600
  81313. @@ -0,0 +1,862 @@
  81314. +/* ==========================================================================
  81315. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  81316. + * $Revision: #58 $
  81317. + * $Date: 2011/09/15 $
  81318. + * $Change: 1846647 $
  81319. + *
  81320. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  81321. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  81322. + * otherwise expressly agreed to in writing between Synopsys and you.
  81323. + *
  81324. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  81325. + * any End User Software License Agreement or Agreement for Licensed Product
  81326. + * with Synopsys or any supplement thereto. You are permitted to use and
  81327. + * redistribute this Software in source and binary forms, with or without
  81328. + * modification, provided that redistributions of source code must retain this
  81329. + * notice. You may not view, use, disclose, copy or distribute this file or
  81330. + * any information contained herein except pursuant to this license grant from
  81331. + * Synopsys. If you do not agree with this notice, including the disclaimer
  81332. + * below, then you are not authorized to use the Software.
  81333. + *
  81334. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  81335. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  81336. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  81337. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  81338. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  81339. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  81340. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  81341. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  81342. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  81343. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  81344. + * DAMAGE.
  81345. + * ========================================================================== */
  81346. +#ifndef DWC_DEVICE_ONLY
  81347. +#ifndef __DWC_HCD_H__
  81348. +#define __DWC_HCD_H__
  81349. +
  81350. +#include "dwc_otg_os_dep.h"
  81351. +#include "usb.h"
  81352. +#include "dwc_otg_hcd_if.h"
  81353. +#include "dwc_otg_core_if.h"
  81354. +#include "dwc_list.h"
  81355. +#include "dwc_otg_cil.h"
  81356. +#include "dwc_otg_fiq_fsm.h"
  81357. +
  81358. +
  81359. +/**
  81360. + * @file
  81361. + *
  81362. + * This file contains the structures, constants, and interfaces for
  81363. + * the Host Contoller Driver (HCD).
  81364. + *
  81365. + * The Host Controller Driver (HCD) is responsible for translating requests
  81366. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  81367. + * It isolates the USBD from the specifics of the controller by providing an
  81368. + * API to the USBD.
  81369. + */
  81370. +
  81371. +struct dwc_otg_hcd_pipe_info {
  81372. + uint8_t dev_addr;
  81373. + uint8_t ep_num;
  81374. + uint8_t pipe_type;
  81375. + uint8_t pipe_dir;
  81376. + uint16_t mps;
  81377. +};
  81378. +
  81379. +struct dwc_otg_hcd_iso_packet_desc {
  81380. + uint32_t offset;
  81381. + uint32_t length;
  81382. + uint32_t actual_length;
  81383. + uint32_t status;
  81384. +};
  81385. +
  81386. +struct dwc_otg_qtd;
  81387. +
  81388. +struct dwc_otg_hcd_urb {
  81389. + void *priv;
  81390. + struct dwc_otg_qtd *qtd;
  81391. + void *buf;
  81392. + dwc_dma_t dma;
  81393. + void *setup_packet;
  81394. + dwc_dma_t setup_dma;
  81395. + uint32_t length;
  81396. + uint32_t actual_length;
  81397. + uint32_t status;
  81398. + uint32_t error_count;
  81399. + uint32_t packet_count;
  81400. + uint32_t flags;
  81401. + uint16_t interval;
  81402. + struct dwc_otg_hcd_pipe_info pipe_info;
  81403. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  81404. +};
  81405. +
  81406. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  81407. +{
  81408. + return pipe->ep_num;
  81409. +}
  81410. +
  81411. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  81412. + *pipe)
  81413. +{
  81414. + return pipe->pipe_type;
  81415. +}
  81416. +
  81417. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  81418. +{
  81419. + return pipe->mps;
  81420. +}
  81421. +
  81422. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  81423. + *pipe)
  81424. +{
  81425. + return pipe->dev_addr;
  81426. +}
  81427. +
  81428. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  81429. + *pipe)
  81430. +{
  81431. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  81432. +}
  81433. +
  81434. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  81435. + *pipe)
  81436. +{
  81437. + return (pipe->pipe_type == UE_INTERRUPT);
  81438. +}
  81439. +
  81440. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  81441. + *pipe)
  81442. +{
  81443. + return (pipe->pipe_type == UE_BULK);
  81444. +}
  81445. +
  81446. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  81447. + *pipe)
  81448. +{
  81449. + return (pipe->pipe_type == UE_CONTROL);
  81450. +}
  81451. +
  81452. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  81453. +{
  81454. + return (pipe->pipe_dir == UE_DIR_IN);
  81455. +}
  81456. +
  81457. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  81458. + *pipe)
  81459. +{
  81460. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  81461. +}
  81462. +
  81463. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  81464. + uint8_t devaddr, uint8_t ep_num,
  81465. + uint8_t pipe_type, uint8_t pipe_dir,
  81466. + uint16_t mps)
  81467. +{
  81468. + pipe->dev_addr = devaddr;
  81469. + pipe->ep_num = ep_num;
  81470. + pipe->pipe_type = pipe_type;
  81471. + pipe->pipe_dir = pipe_dir;
  81472. + pipe->mps = mps;
  81473. +}
  81474. +
  81475. +/**
  81476. + * Phases for control transfers.
  81477. + */
  81478. +typedef enum dwc_otg_control_phase {
  81479. + DWC_OTG_CONTROL_SETUP,
  81480. + DWC_OTG_CONTROL_DATA,
  81481. + DWC_OTG_CONTROL_STATUS
  81482. +} dwc_otg_control_phase_e;
  81483. +
  81484. +/** Transaction types. */
  81485. +typedef enum dwc_otg_transaction_type {
  81486. + DWC_OTG_TRANSACTION_NONE = 0,
  81487. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  81488. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  81489. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  81490. +} dwc_otg_transaction_type_e;
  81491. +
  81492. +struct dwc_otg_qh;
  81493. +
  81494. +/**
  81495. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  81496. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  81497. + * (of one of these types) submitted to the HCD. The transfer associated with
  81498. + * a QTD may require one or multiple transactions.
  81499. + *
  81500. + * A QTD is linked to a Queue Head, which is entered in either the
  81501. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  81502. + * execution, some or all of its transactions may be executed. After
  81503. + * execution, the state of the QTD is updated. The QTD may be retired if all
  81504. + * its transactions are complete or if an error occurred. Otherwise, it
  81505. + * remains in the schedule so more transactions can be executed later.
  81506. + */
  81507. +typedef struct dwc_otg_qtd {
  81508. + /**
  81509. + * Determines the PID of the next data packet for the data phase of
  81510. + * control transfers. Ignored for other transfer types.<br>
  81511. + * One of the following values:
  81512. + * - DWC_OTG_HC_PID_DATA0
  81513. + * - DWC_OTG_HC_PID_DATA1
  81514. + */
  81515. + uint8_t data_toggle;
  81516. +
  81517. + /** Current phase for control transfers (Setup, Data, or Status). */
  81518. + dwc_otg_control_phase_e control_phase;
  81519. +
  81520. + /** Keep track of the current split type
  81521. + * for FS/LS endpoints on a HS Hub */
  81522. + uint8_t complete_split;
  81523. +
  81524. + /** How many bytes transferred during SSPLIT OUT */
  81525. + uint32_t ssplit_out_xfer_count;
  81526. +
  81527. + /**
  81528. + * Holds the number of bus errors that have occurred for a transaction
  81529. + * within this transfer.
  81530. + */
  81531. + uint8_t error_count;
  81532. +
  81533. + /**
  81534. + * Index of the next frame descriptor for an isochronous transfer. A
  81535. + * frame descriptor describes the buffer position and length of the
  81536. + * data to be transferred in the next scheduled (micro)frame of an
  81537. + * isochronous transfer. It also holds status for that transaction.
  81538. + * The frame index starts at 0.
  81539. + */
  81540. + uint16_t isoc_frame_index;
  81541. +
  81542. + /** Position of the ISOC split on full/low speed */
  81543. + uint8_t isoc_split_pos;
  81544. +
  81545. + /** Position of the ISOC split in the buffer for the current frame */
  81546. + uint16_t isoc_split_offset;
  81547. +
  81548. + /** URB for this transfer */
  81549. + struct dwc_otg_hcd_urb *urb;
  81550. +
  81551. + struct dwc_otg_qh *qh;
  81552. +
  81553. + /** This list of QTDs */
  81554. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  81555. +
  81556. + /** Indicates if this QTD is currently processed by HW. */
  81557. + uint8_t in_process;
  81558. +
  81559. + /** Number of DMA descriptors for this QTD */
  81560. + uint8_t n_desc;
  81561. +
  81562. + /**
  81563. + * Last activated frame(packet) index.
  81564. + * Used in Descriptor DMA mode only.
  81565. + */
  81566. + uint16_t isoc_frame_index_last;
  81567. +
  81568. +} dwc_otg_qtd_t;
  81569. +
  81570. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  81571. +
  81572. +/**
  81573. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  81574. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  81575. + * be entered in either the non-periodic or periodic schedule.
  81576. + */
  81577. +typedef struct dwc_otg_qh {
  81578. + /**
  81579. + * Endpoint type.
  81580. + * One of the following values:
  81581. + * - UE_CONTROL
  81582. + * - UE_BULK
  81583. + * - UE_INTERRUPT
  81584. + * - UE_ISOCHRONOUS
  81585. + */
  81586. + uint8_t ep_type;
  81587. + uint8_t ep_is_in;
  81588. +
  81589. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  81590. + uint16_t maxp;
  81591. +
  81592. + /**
  81593. + * Device speed.
  81594. + * One of the following values:
  81595. + * - DWC_OTG_EP_SPEED_LOW
  81596. + * - DWC_OTG_EP_SPEED_FULL
  81597. + * - DWC_OTG_EP_SPEED_HIGH
  81598. + */
  81599. + uint8_t dev_speed;
  81600. +
  81601. + /**
  81602. + * Determines the PID of the next data packet for non-control
  81603. + * transfers. Ignored for control transfers.<br>
  81604. + * One of the following values:
  81605. + * - DWC_OTG_HC_PID_DATA0
  81606. + * - DWC_OTG_HC_PID_DATA1
  81607. + */
  81608. + uint8_t data_toggle;
  81609. +
  81610. + /** Ping state if 1. */
  81611. + uint8_t ping_state;
  81612. +
  81613. + /**
  81614. + * List of QTDs for this QH.
  81615. + */
  81616. + struct dwc_otg_qtd_list qtd_list;
  81617. +
  81618. + /** Host channel currently processing transfers for this QH. */
  81619. + struct dwc_hc *channel;
  81620. +
  81621. + /** Full/low speed endpoint on high-speed hub requires split. */
  81622. + uint8_t do_split;
  81623. +
  81624. + /** @name Periodic schedule information */
  81625. + /** @{ */
  81626. +
  81627. + /** Bandwidth in microseconds per (micro)frame. */
  81628. + uint16_t usecs;
  81629. +
  81630. + /** Interval between transfers in (micro)frames. */
  81631. + uint16_t interval;
  81632. +
  81633. + /**
  81634. + * (micro)frame to initialize a periodic transfer. The transfer
  81635. + * executes in the following (micro)frame.
  81636. + */
  81637. + uint16_t sched_frame;
  81638. +
  81639. + /*
  81640. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  81641. + */
  81642. + uint16_t nak_frame;
  81643. +
  81644. + /** (micro)frame at which last start split was initialized. */
  81645. + uint16_t start_split_frame;
  81646. +
  81647. + /** @} */
  81648. +
  81649. + /**
  81650. + * Used instead of original buffer if
  81651. + * it(physical address) is not dword-aligned.
  81652. + */
  81653. + uint8_t *dw_align_buf;
  81654. + dwc_dma_t dw_align_buf_dma;
  81655. +
  81656. + /** Entry for QH in either the periodic or non-periodic schedule. */
  81657. + dwc_list_link_t qh_list_entry;
  81658. +
  81659. + /** @name Descriptor DMA support */
  81660. + /** @{ */
  81661. +
  81662. + /** Descriptor List. */
  81663. + dwc_otg_host_dma_desc_t *desc_list;
  81664. +
  81665. + /** Descriptor List physical address. */
  81666. + dwc_dma_t desc_list_dma;
  81667. +
  81668. + /**
  81669. + * Xfer Bytes array.
  81670. + * Each element corresponds to a descriptor and indicates
  81671. + * original XferSize size value for the descriptor.
  81672. + */
  81673. + uint32_t *n_bytes;
  81674. +
  81675. + /** Actual number of transfer descriptors in a list. */
  81676. + uint16_t ntd;
  81677. +
  81678. + /** First activated isochronous transfer descriptor index. */
  81679. + uint8_t td_first;
  81680. + /** Last activated isochronous transfer descriptor index. */
  81681. + uint8_t td_last;
  81682. +
  81683. + /** @} */
  81684. +
  81685. +
  81686. + uint16_t speed;
  81687. + uint16_t frame_usecs[8];
  81688. +
  81689. + uint32_t skip_count;
  81690. +} dwc_otg_qh_t;
  81691. +
  81692. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  81693. +
  81694. +typedef struct urb_tq_entry {
  81695. + struct urb *urb;
  81696. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  81697. +} urb_tq_entry_t;
  81698. +
  81699. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  81700. +
  81701. +/**
  81702. + * This structure holds the state of the HCD, including the non-periodic and
  81703. + * periodic schedules.
  81704. + */
  81705. +struct dwc_otg_hcd {
  81706. + /** The DWC otg device pointer */
  81707. + struct dwc_otg_device *otg_dev;
  81708. + /** DWC OTG Core Interface Layer */
  81709. + dwc_otg_core_if_t *core_if;
  81710. +
  81711. + /** Function HCD driver callbacks */
  81712. + struct dwc_otg_hcd_function_ops *fops;
  81713. +
  81714. + /** Internal DWC HCD Flags */
  81715. + volatile union dwc_otg_hcd_internal_flags {
  81716. + uint32_t d32;
  81717. + struct {
  81718. + unsigned port_connect_status_change:1;
  81719. + unsigned port_connect_status:1;
  81720. + unsigned port_reset_change:1;
  81721. + unsigned port_enable_change:1;
  81722. + unsigned port_suspend_change:1;
  81723. + unsigned port_over_current_change:1;
  81724. + unsigned port_l1_change:1;
  81725. + unsigned reserved:26;
  81726. + } b;
  81727. + } flags;
  81728. +
  81729. + /**
  81730. + * Inactive items in the non-periodic schedule. This is a list of
  81731. + * Queue Heads. Transfers associated with these Queue Heads are not
  81732. + * currently assigned to a host channel.
  81733. + */
  81734. + dwc_list_link_t non_periodic_sched_inactive;
  81735. +
  81736. + /**
  81737. + * Active items in the non-periodic schedule. This is a list of
  81738. + * Queue Heads. Transfers associated with these Queue Heads are
  81739. + * currently assigned to a host channel.
  81740. + */
  81741. + dwc_list_link_t non_periodic_sched_active;
  81742. +
  81743. + /**
  81744. + * Pointer to the next Queue Head to process in the active
  81745. + * non-periodic schedule.
  81746. + */
  81747. + dwc_list_link_t *non_periodic_qh_ptr;
  81748. +
  81749. + /**
  81750. + * Inactive items in the periodic schedule. This is a list of QHs for
  81751. + * periodic transfers that are _not_ scheduled for the next frame.
  81752. + * Each QH in the list has an interval counter that determines when it
  81753. + * needs to be scheduled for execution. This scheduling mechanism
  81754. + * allows only a simple calculation for periodic bandwidth used (i.e.
  81755. + * must assume that all periodic transfers may need to execute in the
  81756. + * same frame). However, it greatly simplifies scheduling and should
  81757. + * be sufficient for the vast majority of OTG hosts, which need to
  81758. + * connect to a small number of peripherals at one time.
  81759. + *
  81760. + * Items move from this list to periodic_sched_ready when the QH
  81761. + * interval counter is 0 at SOF.
  81762. + */
  81763. + dwc_list_link_t periodic_sched_inactive;
  81764. +
  81765. + /**
  81766. + * List of periodic QHs that are ready for execution in the next
  81767. + * frame, but have not yet been assigned to host channels.
  81768. + *
  81769. + * Items move from this list to periodic_sched_assigned as host
  81770. + * channels become available during the current frame.
  81771. + */
  81772. + dwc_list_link_t periodic_sched_ready;
  81773. +
  81774. + /**
  81775. + * List of periodic QHs to be executed in the next frame that are
  81776. + * assigned to host channels.
  81777. + *
  81778. + * Items move from this list to periodic_sched_queued as the
  81779. + * transactions for the QH are queued to the DWC_otg controller.
  81780. + */
  81781. + dwc_list_link_t periodic_sched_assigned;
  81782. +
  81783. + /**
  81784. + * List of periodic QHs that have been queued for execution.
  81785. + *
  81786. + * Items move from this list to either periodic_sched_inactive or
  81787. + * periodic_sched_ready when the channel associated with the transfer
  81788. + * is released. If the interval for the QH is 1, the item moves to
  81789. + * periodic_sched_ready because it must be rescheduled for the next
  81790. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  81791. + */
  81792. + dwc_list_link_t periodic_sched_queued;
  81793. +
  81794. + /**
  81795. + * Total bandwidth claimed so far for periodic transfers. This value
  81796. + * is in microseconds per (micro)frame. The assumption is that all
  81797. + * periodic transfers may occur in the same (micro)frame.
  81798. + */
  81799. + uint16_t periodic_usecs;
  81800. +
  81801. + /**
  81802. + * Total bandwidth claimed so far for all periodic transfers
  81803. + * in a frame.
  81804. + * This will include a mixture of HS and FS transfers.
  81805. + * Units are microseconds per (micro)frame.
  81806. + * We have a budget per frame and have to schedule
  81807. + * transactions accordingly.
  81808. + * Watch out for the fact that things are actually scheduled for the
  81809. + * "next frame".
  81810. + */
  81811. + uint16_t frame_usecs[8];
  81812. +
  81813. +
  81814. + /**
  81815. + * Frame number read from the core at SOF. The value ranges from 0 to
  81816. + * DWC_HFNUM_MAX_FRNUM.
  81817. + */
  81818. + uint16_t frame_number;
  81819. +
  81820. + /**
  81821. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  81822. + */
  81823. + uint16_t periodic_qh_count;
  81824. +
  81825. + /**
  81826. + * Free host channels in the controller. This is a list of
  81827. + * dwc_hc_t items.
  81828. + */
  81829. + struct hc_list free_hc_list;
  81830. + /**
  81831. + * Number of host channels assigned to periodic transfers. Currently
  81832. + * assuming that there is a dedicated host channel for each periodic
  81833. + * transaction and at least one host channel available for
  81834. + * non-periodic transactions.
  81835. + */
  81836. + int periodic_channels; /* microframe_schedule==0 */
  81837. +
  81838. + /**
  81839. + * Number of host channels assigned to non-periodic transfers.
  81840. + */
  81841. + int non_periodic_channels; /* microframe_schedule==0 */
  81842. +
  81843. + /**
  81844. + * Number of host channels assigned to non-periodic transfers.
  81845. + */
  81846. + int available_host_channels;
  81847. +
  81848. + /**
  81849. + * Array of pointers to the host channel descriptors. Allows accessing
  81850. + * a host channel descriptor given the host channel number. This is
  81851. + * useful in interrupt handlers.
  81852. + */
  81853. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  81854. +
  81855. + /**
  81856. + * Buffer to use for any data received during the status phase of a
  81857. + * control transfer. Normally no data is transferred during the status
  81858. + * phase. This buffer is used as a bit bucket.
  81859. + */
  81860. + uint8_t *status_buf;
  81861. +
  81862. + /**
  81863. + * DMA address for status_buf.
  81864. + */
  81865. + dma_addr_t status_buf_dma;
  81866. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  81867. +
  81868. + /**
  81869. + * Connection timer. An OTG host must display a message if the device
  81870. + * does not connect. Started when the VBus power is turned on via
  81871. + * sysfs attribute "buspower".
  81872. + */
  81873. + dwc_timer_t *conn_timer;
  81874. +
  81875. + /* Tasket to do a reset */
  81876. + dwc_tasklet_t *reset_tasklet;
  81877. +
  81878. + dwc_tasklet_t *completion_tasklet;
  81879. + struct urb_list completed_urb_list;
  81880. +
  81881. + /* */
  81882. + dwc_spinlock_t *lock;
  81883. + dwc_spinlock_t *channel_lock;
  81884. + /**
  81885. + * Private data that could be used by OS wrapper.
  81886. + */
  81887. + void *priv;
  81888. +
  81889. + uint8_t otg_port;
  81890. +
  81891. + /** Frame List */
  81892. + uint32_t *frame_list;
  81893. +
  81894. + /** Hub - Port assignment */
  81895. + int hub_port[128];
  81896. +#ifdef FIQ_DEBUG
  81897. + int hub_port_alloc[2048];
  81898. +#endif
  81899. +
  81900. + /** Frame List DMA address */
  81901. + dma_addr_t frame_list_dma;
  81902. +
  81903. + struct fiq_stack *fiq_stack;
  81904. + struct fiq_state *fiq_state;
  81905. +
  81906. + /** Virtual address for split transaction DMA bounce buffers */
  81907. + struct fiq_dma_blob *fiq_dmab;
  81908. +
  81909. +#ifdef DEBUG
  81910. + uint32_t frrem_samples;
  81911. + uint64_t frrem_accum;
  81912. +
  81913. + uint32_t hfnum_7_samples_a;
  81914. + uint64_t hfnum_7_frrem_accum_a;
  81915. + uint32_t hfnum_0_samples_a;
  81916. + uint64_t hfnum_0_frrem_accum_a;
  81917. + uint32_t hfnum_other_samples_a;
  81918. + uint64_t hfnum_other_frrem_accum_a;
  81919. +
  81920. + uint32_t hfnum_7_samples_b;
  81921. + uint64_t hfnum_7_frrem_accum_b;
  81922. + uint32_t hfnum_0_samples_b;
  81923. + uint64_t hfnum_0_frrem_accum_b;
  81924. + uint32_t hfnum_other_samples_b;
  81925. + uint64_t hfnum_other_frrem_accum_b;
  81926. +#endif
  81927. +};
  81928. +
  81929. +/** @name Transaction Execution Functions */
  81930. +/** @{ */
  81931. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  81932. + * hcd);
  81933. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  81934. + dwc_otg_transaction_type_e tr_type);
  81935. +
  81936. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  81937. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  81938. +
  81939. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  81940. +extern int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh);
  81941. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  81942. +
  81943. +/** @} */
  81944. +
  81945. +/** @name Interrupt Handler Functions */
  81946. +/** @{ */
  81947. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  81948. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  81949. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  81950. + dwc_otg_hcd);
  81951. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  81952. + dwc_otg_hcd);
  81953. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  81954. + dwc_otg_hcd);
  81955. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  81956. + dwc_otg_hcd);
  81957. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  81958. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  81959. + dwc_otg_hcd);
  81960. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  81961. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  81962. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  81963. + uint32_t num);
  81964. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  81965. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  81966. + dwc_otg_hcd);
  81967. +/** @} */
  81968. +
  81969. +/** @name Schedule Queue Functions */
  81970. +/** @{ */
  81971. +
  81972. +/* Implemented in dwc_otg_hcd_queue.c */
  81973. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  81974. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  81975. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  81976. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  81977. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  81978. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  81979. + int sched_csplit);
  81980. +
  81981. +/** Remove and free a QH */
  81982. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  81983. + dwc_otg_qh_t * qh)
  81984. +{
  81985. + dwc_irqflags_t flags;
  81986. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  81987. + dwc_otg_hcd_qh_remove(hcd, qh);
  81988. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  81989. + dwc_otg_hcd_qh_free(hcd, qh);
  81990. +}
  81991. +
  81992. +/** Allocates memory for a QH structure.
  81993. + * @return Returns the memory allocate or NULL on error. */
  81994. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  81995. +{
  81996. + if (atomic_alloc)
  81997. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  81998. + else
  81999. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  82000. +}
  82001. +
  82002. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  82003. + int atomic_alloc);
  82004. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  82005. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  82006. + dwc_otg_qh_t ** qh, int atomic_alloc);
  82007. +
  82008. +/** Allocates memory for a QTD structure.
  82009. + * @return Returns the memory allocate or NULL on error. */
  82010. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  82011. +{
  82012. + if (atomic_alloc)
  82013. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  82014. + else
  82015. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  82016. +}
  82017. +
  82018. +/** Frees the memory for a QTD structure. QTD should already be removed from
  82019. + * list.
  82020. + * @param qtd QTD to free.*/
  82021. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  82022. +{
  82023. + DWC_FREE(qtd);
  82024. +}
  82025. +
  82026. +/** Removes a QTD from list.
  82027. + * @param hcd HCD instance.
  82028. + * @param qtd QTD to remove from list.
  82029. + * @param qh QTD belongs to.
  82030. + */
  82031. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  82032. + dwc_otg_qtd_t * qtd,
  82033. + dwc_otg_qh_t * qh)
  82034. +{
  82035. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  82036. +}
  82037. +
  82038. +/** Remove and free a QTD
  82039. + * Need to disable IRQ and hold hcd lock while calling this function out of
  82040. + * interrupt servicing chain */
  82041. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  82042. + dwc_otg_qtd_t * qtd,
  82043. + dwc_otg_qh_t * qh)
  82044. +{
  82045. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  82046. + dwc_otg_hcd_qtd_free(qtd);
  82047. +}
  82048. +
  82049. +/** @} */
  82050. +
  82051. +/** @name Descriptor DMA Supporting Functions */
  82052. +/** @{ */
  82053. +
  82054. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82055. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  82056. + dwc_hc_t * hc,
  82057. + dwc_otg_hc_regs_t * hc_regs,
  82058. + dwc_otg_halt_status_e halt_status);
  82059. +
  82060. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82061. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82062. +
  82063. +/** @} */
  82064. +
  82065. +/** @name Internal Functions */
  82066. +/** @{ */
  82067. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  82068. +/** @} */
  82069. +
  82070. +#ifdef CONFIG_USB_DWC_OTG_LPM
  82071. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  82072. + uint8_t devaddr);
  82073. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  82074. +#endif
  82075. +
  82076. +/** Gets the QH that contains the list_head */
  82077. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  82078. +
  82079. +/** Gets the QTD that contains the list_head */
  82080. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  82081. +
  82082. +/** Check if QH is non-periodic */
  82083. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  82084. + (_qh_ptr_->ep_type == UE_CONTROL))
  82085. +
  82086. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  82087. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  82088. +
  82089. +/** Packet size for any kind of endpoint descriptor */
  82090. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  82091. +
  82092. +/**
  82093. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  82094. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  82095. + * frame number when the max frame number is reached.
  82096. + */
  82097. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  82098. +{
  82099. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  82100. + (DWC_HFNUM_MAX_FRNUM >> 1);
  82101. +}
  82102. +
  82103. +/**
  82104. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  82105. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  82106. + * number when the max frame number is reached.
  82107. + */
  82108. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  82109. +{
  82110. + return (frame1 != frame2) &&
  82111. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  82112. + (DWC_HFNUM_MAX_FRNUM >> 1));
  82113. +}
  82114. +
  82115. +/**
  82116. + * Increments _frame by the amount specified by _inc. The addition is done
  82117. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  82118. + */
  82119. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  82120. +{
  82121. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  82122. +}
  82123. +
  82124. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  82125. +{
  82126. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  82127. +}
  82128. +
  82129. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  82130. +{
  82131. + return frame & 0x7;
  82132. +}
  82133. +
  82134. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  82135. + dwc_otg_hc_regs_t * hc_regs,
  82136. + dwc_otg_qtd_t * qtd);
  82137. +
  82138. +#ifdef DEBUG
  82139. +/**
  82140. + * Macro to sample the remaining PHY clocks left in the current frame. This
  82141. + * may be used during debugging to determine the average time it takes to
  82142. + * execute sections of code. There are two possible sample points, "a" and
  82143. + * "b", so the _letter argument must be one of these values.
  82144. + *
  82145. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  82146. + * example, "cat /sys/devices/lm0/hcd_frrem".
  82147. + */
  82148. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  82149. +{ \
  82150. + hfnum_data_t hfnum; \
  82151. + dwc_otg_qtd_t *qtd; \
  82152. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  82153. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  82154. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  82155. + switch (hfnum.b.frnum & 0x7) { \
  82156. + case 7: \
  82157. + _hcd->hfnum_7_samples_##_letter++; \
  82158. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  82159. + break; \
  82160. + case 0: \
  82161. + _hcd->hfnum_0_samples_##_letter++; \
  82162. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  82163. + break; \
  82164. + default: \
  82165. + _hcd->hfnum_other_samples_##_letter++; \
  82166. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  82167. + break; \
  82168. + } \
  82169. + } \
  82170. +}
  82171. +#else
  82172. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  82173. +#endif
  82174. +#endif
  82175. +#endif /* DWC_DEVICE_ONLY */
  82176. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  82177. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 1969-12-31 18:00:00.000000000 -0600
  82178. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 2014-12-11 14:02:55.400418001 -0600
  82179. @@ -0,0 +1,417 @@
  82180. +/* ==========================================================================
  82181. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  82182. + * $Revision: #12 $
  82183. + * $Date: 2011/10/26 $
  82184. + * $Change: 1873028 $
  82185. + *
  82186. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82187. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82188. + * otherwise expressly agreed to in writing between Synopsys and you.
  82189. + *
  82190. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82191. + * any End User Software License Agreement or Agreement for Licensed Product
  82192. + * with Synopsys or any supplement thereto. You are permitted to use and
  82193. + * redistribute this Software in source and binary forms, with or without
  82194. + * modification, provided that redistributions of source code must retain this
  82195. + * notice. You may not view, use, disclose, copy or distribute this file or
  82196. + * any information contained herein except pursuant to this license grant from
  82197. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82198. + * below, then you are not authorized to use the Software.
  82199. + *
  82200. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82201. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82202. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82203. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82204. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82205. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82206. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82207. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82208. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82209. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82210. + * DAMAGE.
  82211. + * ========================================================================== */
  82212. +#ifndef DWC_DEVICE_ONLY
  82213. +#ifndef __DWC_HCD_IF_H__
  82214. +#define __DWC_HCD_IF_H__
  82215. +
  82216. +#include "dwc_otg_core_if.h"
  82217. +
  82218. +/** @file
  82219. + * This file defines DWC_OTG HCD Core API.
  82220. + */
  82221. +
  82222. +struct dwc_otg_hcd;
  82223. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  82224. +
  82225. +struct dwc_otg_hcd_urb;
  82226. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  82227. +
  82228. +/** @name HCD Function Driver Callbacks */
  82229. +/** @{ */
  82230. +
  82231. +/** This function is called whenever core switches to host mode. */
  82232. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  82233. +
  82234. +/** This function is called when device has been disconnected */
  82235. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  82236. +
  82237. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  82238. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  82239. + void *urb_handle,
  82240. + uint32_t * hub_addr,
  82241. + uint32_t * port_addr);
  82242. +/** Via this function HCD core gets device speed */
  82243. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  82244. + void *urb_handle);
  82245. +
  82246. +/** This function is called when urb is completed */
  82247. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  82248. + void *urb_handle,
  82249. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  82250. + int32_t status);
  82251. +
  82252. +/** Via this function HCD core gets b_hnp_enable parameter */
  82253. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  82254. +
  82255. +struct dwc_otg_hcd_function_ops {
  82256. + dwc_otg_hcd_start_cb_t start;
  82257. + dwc_otg_hcd_disconnect_cb_t disconnect;
  82258. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  82259. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  82260. + dwc_otg_hcd_complete_urb_cb_t complete;
  82261. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  82262. +};
  82263. +/** @} */
  82264. +
  82265. +/** @name HCD Core API */
  82266. +/** @{ */
  82267. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  82268. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  82269. +
  82270. +/** This function should be called to initiate HCD Core.
  82271. + *
  82272. + * @param hcd The HCD
  82273. + * @param core_if The DWC_OTG Core
  82274. + *
  82275. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  82276. + * Returns 0 on success
  82277. + */
  82278. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  82279. +
  82280. +/** Frees HCD
  82281. + *
  82282. + * @param hcd The HCD
  82283. + */
  82284. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  82285. +
  82286. +/** This function should be called on every hardware interrupt.
  82287. + *
  82288. + * @param dwc_otg_hcd The HCD
  82289. + *
  82290. + * Returns non zero if interrupt is handled
  82291. + * Return 0 if interrupt is not handled
  82292. + */
  82293. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82294. +
  82295. +/** This function is used to handle the fast interrupt
  82296. + *
  82297. + */
  82298. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  82299. +
  82300. +/**
  82301. + * Returns private data set by
  82302. + * dwc_otg_hcd_set_priv_data function.
  82303. + *
  82304. + * @param hcd The HCD
  82305. + */
  82306. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  82307. +
  82308. +/**
  82309. + * Set private data.
  82310. + *
  82311. + * @param hcd The HCD
  82312. + * @param priv_data pointer to be stored in private data
  82313. + */
  82314. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  82315. +
  82316. +/**
  82317. + * This function initializes the HCD Core.
  82318. + *
  82319. + * @param hcd The HCD
  82320. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  82321. + *
  82322. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  82323. + * Returns 0 on success
  82324. + */
  82325. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  82326. + struct dwc_otg_hcd_function_ops *fops);
  82327. +
  82328. +/**
  82329. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  82330. + * stopped.
  82331. + *
  82332. + * @param hcd The HCD
  82333. + */
  82334. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  82335. +
  82336. +/**
  82337. + * Handles hub class-specific requests.
  82338. + *
  82339. + * @param dwc_otg_hcd The HCD
  82340. + * @param typeReq Request Type
  82341. + * @param wValue wValue from control request
  82342. + * @param wIndex wIndex from control request
  82343. + * @param buf data buffer
  82344. + * @param wLength data buffer length
  82345. + *
  82346. + * Returns -DWC_E_INVALID if invalid argument is passed
  82347. + * Returns 0 on success
  82348. + */
  82349. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  82350. + uint16_t typeReq, uint16_t wValue,
  82351. + uint16_t wIndex, uint8_t * buf,
  82352. + uint16_t wLength);
  82353. +
  82354. +/**
  82355. + * Returns otg port number.
  82356. + *
  82357. + * @param hcd The HCD
  82358. + */
  82359. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  82360. +
  82361. +/**
  82362. + * Returns OTG version - either 1.3 or 2.0.
  82363. + *
  82364. + * @param core_if The core_if structure pointer
  82365. + */
  82366. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  82367. +
  82368. +/**
  82369. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  82370. + *
  82371. + * @param hcd The HCD
  82372. + */
  82373. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  82374. +
  82375. +/**
  82376. + * Returns current frame number.
  82377. + *
  82378. + * @param hcd The HCD
  82379. + */
  82380. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  82381. +
  82382. +/**
  82383. + * Dumps hcd state.
  82384. + *
  82385. + * @param hcd The HCD
  82386. + */
  82387. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  82388. +
  82389. +/**
  82390. + * Dump the average frame remaining at SOF. This can be used to
  82391. + * determine average interrupt latency. Frame remaining is also shown for
  82392. + * start transfer and two additional sample points.
  82393. + * Currently this function is not implemented.
  82394. + *
  82395. + * @param hcd The HCD
  82396. + */
  82397. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  82398. +
  82399. +/**
  82400. + * Sends LPM transaction to the local device.
  82401. + *
  82402. + * @param hcd The HCD
  82403. + * @param devaddr Device Address
  82404. + * @param hird Host initiated resume duration
  82405. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  82406. + *
  82407. + * Returns negative value if sending LPM transaction was not succeeded.
  82408. + * Returns 0 on success.
  82409. + */
  82410. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  82411. + uint8_t hird, uint8_t bRemoteWake);
  82412. +
  82413. +/* URB interface */
  82414. +
  82415. +/**
  82416. + * Allocates memory for dwc_otg_hcd_urb structure.
  82417. + * Allocated memory should be freed by call of DWC_FREE.
  82418. + *
  82419. + * @param hcd The HCD
  82420. + * @param iso_desc_count Count of ISOC descriptors
  82421. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  82422. + */
  82423. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  82424. + int iso_desc_count,
  82425. + int atomic_alloc);
  82426. +
  82427. +/**
  82428. + * Set pipe information in URB.
  82429. + *
  82430. + * @param hcd_urb DWC_OTG URB
  82431. + * @param devaddr Device Address
  82432. + * @param ep_num Endpoint Number
  82433. + * @param ep_type Endpoint Type
  82434. + * @param ep_dir Endpoint Direction
  82435. + * @param mps Max Packet Size
  82436. + */
  82437. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  82438. + uint8_t devaddr, uint8_t ep_num,
  82439. + uint8_t ep_type, uint8_t ep_dir,
  82440. + uint16_t mps);
  82441. +
  82442. +/* Transfer flags */
  82443. +#define URB_GIVEBACK_ASAP 0x1
  82444. +#define URB_SEND_ZERO_PACKET 0x2
  82445. +
  82446. +/**
  82447. + * Sets dwc_otg_hcd_urb parameters.
  82448. + *
  82449. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  82450. + * @param urb_handle Unique handle for request, this will be passed back
  82451. + * to function driver in completion callback.
  82452. + * @param buf The buffer for the data
  82453. + * @param dma The DMA buffer for the data
  82454. + * @param buflen Transfer length
  82455. + * @param sp Buffer for setup data
  82456. + * @param sp_dma DMA address of setup data buffer
  82457. + * @param flags Transfer flags
  82458. + * @param interval Polling interval for interrupt or isochronous transfers.
  82459. + */
  82460. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  82461. + void *urb_handle, void *buf,
  82462. + dwc_dma_t dma, uint32_t buflen, void *sp,
  82463. + dwc_dma_t sp_dma, uint32_t flags,
  82464. + uint16_t interval);
  82465. +
  82466. +/** Gets status from dwc_otg_hcd_urb
  82467. + *
  82468. + * @param dwc_otg_urb DWC_OTG URB
  82469. + */
  82470. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  82471. +
  82472. +/** Gets actual length from dwc_otg_hcd_urb
  82473. + *
  82474. + * @param dwc_otg_urb DWC_OTG URB
  82475. + */
  82476. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  82477. + dwc_otg_urb);
  82478. +
  82479. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  82480. + *
  82481. + * @param dwc_otg_urb DWC_OTG URB
  82482. + */
  82483. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  82484. + dwc_otg_urb);
  82485. +
  82486. +/** Set ISOC descriptor offset and length
  82487. + *
  82488. + * @param dwc_otg_urb DWC_OTG URB
  82489. + * @param desc_num ISOC descriptor number
  82490. + * @param offset Offset from beginig of buffer.
  82491. + * @param length Transaction length
  82492. + */
  82493. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  82494. + int desc_num, uint32_t offset,
  82495. + uint32_t length);
  82496. +
  82497. +/** Get status of ISOC descriptor, specified by desc_num
  82498. + *
  82499. + * @param dwc_otg_urb DWC_OTG URB
  82500. + * @param desc_num ISOC descriptor number
  82501. + */
  82502. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  82503. + dwc_otg_urb, int desc_num);
  82504. +
  82505. +/** Get actual length of ISOC descriptor, specified by desc_num
  82506. + *
  82507. + * @param dwc_otg_urb DWC_OTG URB
  82508. + * @param desc_num ISOC descriptor number
  82509. + */
  82510. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  82511. + dwc_otg_urb,
  82512. + int desc_num);
  82513. +
  82514. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  82515. + *
  82516. + * @param dwc_otg_hcd The HCD
  82517. + * @param dwc_otg_urb DWC_OTG URB
  82518. + * @param ep_handle Out parameter for returning endpoint handle
  82519. + * @param atomic_alloc Flag to do atomic allocation if needed
  82520. + *
  82521. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  82522. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  82523. + * Returns 0 on success.
  82524. + */
  82525. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  82526. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  82527. + void **ep_handle, int atomic_alloc);
  82528. +
  82529. +/** De-queue the specified URB
  82530. + *
  82531. + * @param dwc_otg_hcd The HCD
  82532. + * @param dwc_otg_urb DWC_OTG URB
  82533. + */
  82534. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  82535. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  82536. +
  82537. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  82538. + * Any URBs for the endpoint must already be dequeued.
  82539. + *
  82540. + * @param hcd The HCD
  82541. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  82542. + * @param retry Number of retries if there are queued transfers.
  82543. + *
  82544. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  82545. + * Returns 0 on success
  82546. + */
  82547. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  82548. + int retry);
  82549. +
  82550. +/* Resets the data toggle in qh structure. This function can be called from
  82551. + * usb_clear_halt routine.
  82552. + *
  82553. + * @param hcd The HCD
  82554. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  82555. + *
  82556. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  82557. + * Returns 0 on success
  82558. + */
  82559. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  82560. +
  82561. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  82562. + *
  82563. + * @param hcd The HCD
  82564. + * @param port Port number
  82565. + */
  82566. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  82567. +
  82568. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  82569. + * Only for ISOC and INTERRUPT endpoints.
  82570. + *
  82571. + * @param hcd The HCD
  82572. + * @param ep_handle Endpoint handle
  82573. + */
  82574. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  82575. + void *ep_handle);
  82576. +
  82577. +/** Call this function to check if bandwidth was freed for specified endpoint.
  82578. + *
  82579. + * @param hcd The HCD
  82580. + * @param ep_handle Endpoint handle
  82581. + */
  82582. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  82583. +
  82584. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  82585. + * Only for ISOC and INTERRUPT endpoints.
  82586. + *
  82587. + * @param hcd The HCD
  82588. + * @param ep_handle Endpoint handle
  82589. + */
  82590. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  82591. + void *ep_handle);
  82592. +
  82593. +/** @} */
  82594. +
  82595. +#endif /* __DWC_HCD_IF_H__ */
  82596. +#endif /* DWC_DEVICE_ONLY */
  82597. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  82598. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 1969-12-31 18:00:00.000000000 -0600
  82599. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 2014-12-11 14:05:39.524418001 -0600
  82600. @@ -0,0 +1,2688 @@
  82601. +/* ==========================================================================
  82602. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  82603. + * $Revision: #89 $
  82604. + * $Date: 2011/10/20 $
  82605. + * $Change: 1869487 $
  82606. + *
  82607. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82608. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82609. + * otherwise expressly agreed to in writing between Synopsys and you.
  82610. + *
  82611. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82612. + * any End User Software License Agreement or Agreement for Licensed Product
  82613. + * with Synopsys or any supplement thereto. You are permitted to use and
  82614. + * redistribute this Software in source and binary forms, with or without
  82615. + * modification, provided that redistributions of source code must retain this
  82616. + * notice. You may not view, use, disclose, copy or distribute this file or
  82617. + * any information contained herein except pursuant to this license grant from
  82618. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82619. + * below, then you are not authorized to use the Software.
  82620. + *
  82621. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82622. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82623. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82624. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82625. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82626. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82627. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82628. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82629. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82630. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82631. + * DAMAGE.
  82632. + * ========================================================================== */
  82633. +#ifndef DWC_DEVICE_ONLY
  82634. +
  82635. +#include "dwc_otg_hcd.h"
  82636. +#include "dwc_otg_regs.h"
  82637. +
  82638. +#include <linux/jiffies.h>
  82639. +#include <mach/hardware.h>
  82640. +#include <asm/fiq.h>
  82641. +
  82642. +
  82643. +extern bool microframe_schedule;
  82644. +
  82645. +/** @file
  82646. + * This file contains the implementation of the HCD Interrupt handlers.
  82647. + */
  82648. +
  82649. +int fiq_done, int_done;
  82650. +
  82651. +#ifdef FIQ_DEBUG
  82652. +char buffer[1000*16];
  82653. +int wptr;
  82654. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  82655. +{
  82656. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  82657. + va_list args;
  82658. + char text[17];
  82659. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  82660. +
  82661. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  82662. + {
  82663. + local_fiq_disable();
  82664. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  82665. + va_start(args, fmt);
  82666. + vsnprintf(text+8, 9, fmt, args);
  82667. + va_end(args);
  82668. +
  82669. + memcpy(buffer + wptr, text, 16);
  82670. + wptr = (wptr + 16) % sizeof(buffer);
  82671. + local_fiq_enable();
  82672. + }
  82673. +}
  82674. +#endif
  82675. +
  82676. +/** This function handles interrupts for the HCD. */
  82677. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  82678. +{
  82679. + int retval = 0;
  82680. + static int last_time;
  82681. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  82682. + gintsts_data_t gintsts;
  82683. + gintmsk_data_t gintmsk;
  82684. + hfnum_data_t hfnum;
  82685. + haintmsk_data_t haintmsk;
  82686. +
  82687. +#ifdef DEBUG
  82688. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  82689. +
  82690. +#endif
  82691. +
  82692. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  82693. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  82694. +
  82695. + /* Exit from ISR if core is hibernated */
  82696. + if (core_if->hibernation_suspend == 1) {
  82697. + goto exit_handler_routine;
  82698. + }
  82699. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  82700. + /* Check if HOST Mode */
  82701. + if (dwc_otg_is_host_mode(core_if)) {
  82702. + if (fiq_enable) {
  82703. + local_fiq_disable();
  82704. + /* Pull in from the FIQ's disabled mask */
  82705. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  82706. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  82707. + }
  82708. +
  82709. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  82710. + gintsts.b.hcintr = 1;
  82711. + }
  82712. +
  82713. + /* Danger will robinson: fake a SOF if necessary */
  82714. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  82715. + gintsts.b.sofintr = 1;
  82716. + }
  82717. + gintsts.d32 &= gintmsk.d32;
  82718. +
  82719. + if (fiq_enable)
  82720. + local_fiq_enable();
  82721. +
  82722. + if (!gintsts.d32) {
  82723. + goto exit_handler_routine;
  82724. + }
  82725. +
  82726. +#ifdef DEBUG
  82727. + // We should be OK doing this because the common interrupts should already have been serviced
  82728. + /* Don't print debug message in the interrupt handler on SOF */
  82729. +#ifndef DEBUG_SOF
  82730. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  82731. +#endif
  82732. + DWC_DEBUGPL(DBG_HCDI, "\n");
  82733. +#endif
  82734. +
  82735. +#ifdef DEBUG
  82736. +#ifndef DEBUG_SOF
  82737. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  82738. +#endif
  82739. + DWC_DEBUGPL(DBG_HCDI,
  82740. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  82741. + gintsts.d32, core_if);
  82742. +#endif
  82743. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  82744. + if (gintsts.b.sofintr) {
  82745. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  82746. + }
  82747. +
  82748. + if (gintsts.b.rxstsqlvl) {
  82749. + retval |=
  82750. + dwc_otg_hcd_handle_rx_status_q_level_intr
  82751. + (dwc_otg_hcd);
  82752. + }
  82753. + if (gintsts.b.nptxfempty) {
  82754. + retval |=
  82755. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  82756. + (dwc_otg_hcd);
  82757. + }
  82758. + if (gintsts.b.i2cintr) {
  82759. + /** @todo Implement i2cintr handler. */
  82760. + }
  82761. + if (gintsts.b.portintr) {
  82762. +
  82763. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  82764. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  82765. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  82766. + }
  82767. + if (gintsts.b.hcintr) {
  82768. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  82769. + }
  82770. + if (gintsts.b.ptxfempty) {
  82771. + retval |=
  82772. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  82773. + (dwc_otg_hcd);
  82774. + }
  82775. +#ifdef DEBUG
  82776. +#ifndef DEBUG_SOF
  82777. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  82778. +#endif
  82779. + {
  82780. + DWC_DEBUGPL(DBG_HCDI,
  82781. + "DWC OTG HCD Finished Servicing Interrupts\n");
  82782. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  82783. + DWC_READ_REG32(&global_regs->gintsts));
  82784. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  82785. + DWC_READ_REG32(&global_regs->gintmsk));
  82786. + }
  82787. +#endif
  82788. +
  82789. +#ifdef DEBUG
  82790. +#ifndef DEBUG_SOF
  82791. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  82792. +#endif
  82793. + DWC_DEBUGPL(DBG_HCDI, "\n");
  82794. +#endif
  82795. +
  82796. + }
  82797. +
  82798. +exit_handler_routine:
  82799. + if (fiq_enable) {
  82800. + gintmsk_data_t gintmsk_new;
  82801. + haintmsk_data_t haintmsk_new;
  82802. + local_fiq_disable();
  82803. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  82804. + if(fiq_fsm_enable)
  82805. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  82806. + else
  82807. + haintmsk_new.d32 = 0x0000FFFF;
  82808. +
  82809. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  82810. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  82811. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  82812. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  82813. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  82814. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  82815. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  82816. + ;
  82817. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  82818. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  82819. + }
  82820. + int_done++;
  82821. + }
  82822. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  82823. + /* Re-enable interrupts that the FIQ masked (first time round) */
  82824. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  82825. + local_fiq_enable();
  82826. +
  82827. + if ((jiffies / HZ) > last_time) {
  82828. + //dwc_otg_qh_t *qh;
  82829. + //dwc_list_link_t *cur;
  82830. + /* Once a second output the fiq and irq numbers, useful for debug */
  82831. + last_time = jiffies / HZ;
  82832. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  82833. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  82834. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  82835. + //printk(KERN_WARNING "Periodic queues:\n");
  82836. + }
  82837. + }
  82838. +
  82839. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  82840. + return retval;
  82841. +}
  82842. +
  82843. +#ifdef DWC_TRACK_MISSED_SOFS
  82844. +
  82845. +#warning Compiling code to track missed SOFs
  82846. +#define FRAME_NUM_ARRAY_SIZE 1000
  82847. +/**
  82848. + * This function is for debug only.
  82849. + */
  82850. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  82851. +{
  82852. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  82853. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  82854. + static int frame_num_idx = 0;
  82855. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  82856. + static int dumped_frame_num_array = 0;
  82857. +
  82858. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  82859. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  82860. + curr_frame_number) {
  82861. + frame_num_array[frame_num_idx] = curr_frame_number;
  82862. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  82863. + }
  82864. + } else if (!dumped_frame_num_array) {
  82865. + int i;
  82866. + DWC_PRINTF("Frame Last Frame\n");
  82867. + DWC_PRINTF("----- ----------\n");
  82868. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  82869. + DWC_PRINTF("0x%04x 0x%04x\n",
  82870. + frame_num_array[i], last_frame_num_array[i]);
  82871. + }
  82872. + dumped_frame_num_array = 1;
  82873. + }
  82874. + last_frame_num = curr_frame_number;
  82875. +}
  82876. +#endif
  82877. +
  82878. +/**
  82879. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  82880. + * transactions may be queued to the DWC_otg controller for the current
  82881. + * (micro)frame. Periodic transactions may be queued to the controller for the
  82882. + * next (micro)frame.
  82883. + */
  82884. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  82885. +{
  82886. + hfnum_data_t hfnum;
  82887. + gintsts_data_t gintsts = { .d32 = 0 };
  82888. + dwc_list_link_t *qh_entry;
  82889. + dwc_otg_qh_t *qh;
  82890. + dwc_otg_transaction_type_e tr_type;
  82891. + int did_something = 0;
  82892. + int32_t next_sched_frame = -1;
  82893. +
  82894. + hfnum.d32 =
  82895. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  82896. +
  82897. +#ifdef DEBUG_SOF
  82898. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  82899. +#endif
  82900. + hcd->frame_number = hfnum.b.frnum;
  82901. +
  82902. +#ifdef DEBUG
  82903. + hcd->frrem_accum += hfnum.b.frrem;
  82904. + hcd->frrem_samples++;
  82905. +#endif
  82906. +
  82907. +#ifdef DWC_TRACK_MISSED_SOFS
  82908. + track_missed_sofs(hcd->frame_number);
  82909. +#endif
  82910. + /* Determine whether any periodic QHs should be executed. */
  82911. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  82912. + while (qh_entry != &hcd->periodic_sched_inactive) {
  82913. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  82914. + qh_entry = qh_entry->next;
  82915. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  82916. +
  82917. + /*
  82918. + * Move QH to the ready list to be executed next
  82919. + * (micro)frame.
  82920. + */
  82921. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  82922. + &qh->qh_list_entry);
  82923. +
  82924. + did_something = 1;
  82925. + }
  82926. + else
  82927. + {
  82928. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  82929. + {
  82930. + next_sched_frame = qh->sched_frame;
  82931. + }
  82932. + }
  82933. + }
  82934. + if (fiq_enable)
  82935. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  82936. +
  82937. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  82938. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  82939. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  82940. + did_something = 1;
  82941. + }
  82942. +
  82943. + /* Clear interrupt - but do not trample on the FIQ sof */
  82944. + if (!fiq_fsm_enable) {
  82945. + gintsts.b.sofintr = 1;
  82946. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  82947. + }
  82948. + return 1;
  82949. +}
  82950. +
  82951. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  82952. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  82953. + * memory if the DWC_otg controller is operating in Slave mode. */
  82954. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  82955. +{
  82956. + host_grxsts_data_t grxsts;
  82957. + dwc_hc_t *hc = NULL;
  82958. +
  82959. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  82960. +
  82961. + grxsts.d32 =
  82962. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  82963. +
  82964. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  82965. + if (!hc) {
  82966. + DWC_ERROR("Unable to get corresponding channel\n");
  82967. + return 0;
  82968. + }
  82969. +
  82970. + /* Packet Status */
  82971. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  82972. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  82973. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  82974. + hc->data_pid_start);
  82975. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  82976. +
  82977. + switch (grxsts.b.pktsts) {
  82978. + case DWC_GRXSTS_PKTSTS_IN:
  82979. + /* Read the data into the host buffer. */
  82980. + if (grxsts.b.bcnt > 0) {
  82981. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  82982. + hc->xfer_buff, grxsts.b.bcnt);
  82983. +
  82984. + /* Update the HC fields for the next packet received. */
  82985. + hc->xfer_count += grxsts.b.bcnt;
  82986. + hc->xfer_buff += grxsts.b.bcnt;
  82987. + }
  82988. +
  82989. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  82990. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  82991. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  82992. + /* Handled in interrupt, just ignore data */
  82993. + break;
  82994. + default:
  82995. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  82996. + grxsts.b.pktsts);
  82997. + break;
  82998. + }
  82999. +
  83000. + return 1;
  83001. +}
  83002. +
  83003. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  83004. + * data packets may be written to the FIFO for OUT transfers. More requests
  83005. + * may be written to the non-periodic request queue for IN transfers. This
  83006. + * interrupt is enabled only in Slave mode. */
  83007. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83008. +{
  83009. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  83010. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  83011. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  83012. + return 1;
  83013. +}
  83014. +
  83015. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  83016. + * packets may be written to the FIFO for OUT transfers. More requests may be
  83017. + * written to the periodic request queue for IN transfers. This interrupt is
  83018. + * enabled only in Slave mode. */
  83019. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83020. +{
  83021. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  83022. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  83023. + DWC_OTG_TRANSACTION_PERIODIC);
  83024. + return 1;
  83025. +}
  83026. +
  83027. +/** There are multiple conditions that can cause a port interrupt. This function
  83028. + * determines which interrupt conditions have occurred and handles them
  83029. + * appropriately. */
  83030. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83031. +{
  83032. + int retval = 0;
  83033. + hprt0_data_t hprt0;
  83034. + hprt0_data_t hprt0_modify;
  83035. +
  83036. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  83037. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  83038. +
  83039. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  83040. + * GINTSTS */
  83041. +
  83042. + hprt0_modify.b.prtena = 0;
  83043. + hprt0_modify.b.prtconndet = 0;
  83044. + hprt0_modify.b.prtenchng = 0;
  83045. + hprt0_modify.b.prtovrcurrchng = 0;
  83046. +
  83047. + /* Port Connect Detected
  83048. + * Set flag and clear if detected */
  83049. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  83050. + // Dont modify port status if we are in hibernation state
  83051. + hprt0_modify.b.prtconndet = 1;
  83052. + hprt0_modify.b.prtenchng = 1;
  83053. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  83054. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  83055. + return retval;
  83056. + }
  83057. +
  83058. + if (hprt0.b.prtconndet) {
  83059. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  83060. + if (dwc_otg_hcd->core_if->adp_enable &&
  83061. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  83062. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  83063. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  83064. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  83065. + /* TODO - check if this is required, as
  83066. + * host initialization was already performed
  83067. + * after initial ADP probing
  83068. + */
  83069. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  83070. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  83071. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  83072. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  83073. + } else {
  83074. +
  83075. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  83076. + "Port Connect Detected--\n", hprt0.d32);
  83077. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  83078. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  83079. + hprt0_modify.b.prtconndet = 1;
  83080. +
  83081. + /* B-Device has connected, Delete the connection timer. */
  83082. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  83083. + }
  83084. + /* The Hub driver asserts a reset when it sees port connect
  83085. + * status change flag */
  83086. + retval |= 1;
  83087. + }
  83088. +
  83089. + /* Port Enable Changed
  83090. + * Clear if detected - Set internal flag if disabled */
  83091. + if (hprt0.b.prtenchng) {
  83092. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  83093. + "Port Enable Changed--\n", hprt0.d32);
  83094. + hprt0_modify.b.prtenchng = 1;
  83095. + if (hprt0.b.prtena == 1) {
  83096. + hfir_data_t hfir;
  83097. + int do_reset = 0;
  83098. + dwc_otg_core_params_t *params =
  83099. + dwc_otg_hcd->core_if->core_params;
  83100. + dwc_otg_core_global_regs_t *global_regs =
  83101. + dwc_otg_hcd->core_if->core_global_regs;
  83102. + dwc_otg_host_if_t *host_if =
  83103. + dwc_otg_hcd->core_if->host_if;
  83104. +
  83105. + /* Every time when port enables calculate
  83106. + * HFIR.FrInterval
  83107. + */
  83108. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  83109. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  83110. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  83111. +
  83112. + /* Check if we need to adjust the PHY clock speed for
  83113. + * low power and adjust it */
  83114. + if (params->host_support_fs_ls_low_power) {
  83115. + gusbcfg_data_t usbcfg;
  83116. +
  83117. + usbcfg.d32 =
  83118. + DWC_READ_REG32(&global_regs->gusbcfg);
  83119. +
  83120. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  83121. + || hprt0.b.prtspd ==
  83122. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  83123. + /*
  83124. + * Low power
  83125. + */
  83126. + hcfg_data_t hcfg;
  83127. + if (usbcfg.b.phylpwrclksel == 0) {
  83128. + /* Set PHY low power clock select for FS/LS devices */
  83129. + usbcfg.b.phylpwrclksel = 1;
  83130. + DWC_WRITE_REG32
  83131. + (&global_regs->gusbcfg,
  83132. + usbcfg.d32);
  83133. + do_reset = 1;
  83134. + }
  83135. +
  83136. + hcfg.d32 =
  83137. + DWC_READ_REG32
  83138. + (&host_if->host_global_regs->hcfg);
  83139. +
  83140. + if (hprt0.b.prtspd ==
  83141. + DWC_HPRT0_PRTSPD_LOW_SPEED
  83142. + && params->host_ls_low_power_phy_clk
  83143. + ==
  83144. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  83145. + {
  83146. + /* 6 MHZ */
  83147. + DWC_DEBUGPL(DBG_CIL,
  83148. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  83149. + if (hcfg.b.fslspclksel !=
  83150. + DWC_HCFG_6_MHZ) {
  83151. + hcfg.b.fslspclksel =
  83152. + DWC_HCFG_6_MHZ;
  83153. + DWC_WRITE_REG32
  83154. + (&host_if->host_global_regs->hcfg,
  83155. + hcfg.d32);
  83156. + do_reset = 1;
  83157. + }
  83158. + } else {
  83159. + /* 48 MHZ */
  83160. + DWC_DEBUGPL(DBG_CIL,
  83161. + "FS_PHY programming HCFG to 48 MHz ()\n");
  83162. + if (hcfg.b.fslspclksel !=
  83163. + DWC_HCFG_48_MHZ) {
  83164. + hcfg.b.fslspclksel =
  83165. + DWC_HCFG_48_MHZ;
  83166. + DWC_WRITE_REG32
  83167. + (&host_if->host_global_regs->hcfg,
  83168. + hcfg.d32);
  83169. + do_reset = 1;
  83170. + }
  83171. + }
  83172. + } else {
  83173. + /*
  83174. + * Not low power
  83175. + */
  83176. + if (usbcfg.b.phylpwrclksel == 1) {
  83177. + usbcfg.b.phylpwrclksel = 0;
  83178. + DWC_WRITE_REG32
  83179. + (&global_regs->gusbcfg,
  83180. + usbcfg.d32);
  83181. + do_reset = 1;
  83182. + }
  83183. + }
  83184. +
  83185. + if (do_reset) {
  83186. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  83187. + }
  83188. + }
  83189. +
  83190. + if (!do_reset) {
  83191. + /* Port has been enabled set the reset change flag */
  83192. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  83193. + }
  83194. + } else {
  83195. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  83196. + }
  83197. + retval |= 1;
  83198. + }
  83199. +
  83200. + /** Overcurrent Change Interrupt */
  83201. + if (hprt0.b.prtovrcurrchng) {
  83202. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  83203. + "Port Overcurrent Changed--\n", hprt0.d32);
  83204. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  83205. + hprt0_modify.b.prtovrcurrchng = 1;
  83206. + retval |= 1;
  83207. + }
  83208. +
  83209. + /* Clear Port Interrupts */
  83210. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  83211. +
  83212. + return retval;
  83213. +}
  83214. +
  83215. +/** This interrupt indicates that one or more host channels has a pending
  83216. + * interrupt. There are multiple conditions that can cause each host channel
  83217. + * interrupt. This function determines which conditions have occurred for each
  83218. + * host channel interrupt and handles them appropriately. */
  83219. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83220. +{
  83221. + int i;
  83222. + int retval = 0;
  83223. + haint_data_t haint = { .d32 = 0 } ;
  83224. +
  83225. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  83226. + * GINTSTS */
  83227. +
  83228. + if (!fiq_fsm_enable)
  83229. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  83230. +
  83231. + // Overwrite with saved interrupts from fiq handler
  83232. + if(fiq_fsm_enable)
  83233. + {
  83234. + /* check the mask? */
  83235. + local_fiq_disable();
  83236. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  83237. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  83238. + local_fiq_enable();
  83239. + }
  83240. +
  83241. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  83242. + if (haint.b2.chint & (1 << i)) {
  83243. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  83244. + }
  83245. + }
  83246. +
  83247. + return retval;
  83248. +}
  83249. +
  83250. +/**
  83251. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  83252. + * holds the reason for the halt.
  83253. + *
  83254. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  83255. + * *short_read is set to 1 upon return if less than the requested
  83256. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  83257. + * return. short_read may also be NULL on entry, in which case it remains
  83258. + * unchanged.
  83259. + */
  83260. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  83261. + dwc_otg_hc_regs_t * hc_regs,
  83262. + dwc_otg_qtd_t * qtd,
  83263. + dwc_otg_halt_status_e halt_status,
  83264. + int *short_read)
  83265. +{
  83266. + hctsiz_data_t hctsiz;
  83267. + uint32_t length;
  83268. +
  83269. + if (short_read != NULL) {
  83270. + *short_read = 0;
  83271. + }
  83272. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  83273. +
  83274. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  83275. + if (hc->ep_is_in) {
  83276. + length = hc->xfer_len - hctsiz.b.xfersize;
  83277. + if (short_read != NULL) {
  83278. + *short_read = (hctsiz.b.xfersize != 0);
  83279. + }
  83280. + } else if (hc->qh->do_split) {
  83281. + //length = split_out_xfersize[hc->hc_num];
  83282. + length = qtd->ssplit_out_xfer_count;
  83283. + } else {
  83284. + length = hc->xfer_len;
  83285. + }
  83286. + } else {
  83287. + /*
  83288. + * Must use the hctsiz.pktcnt field to determine how much data
  83289. + * has been transferred. This field reflects the number of
  83290. + * packets that have been transferred via the USB. This is
  83291. + * always an integral number of packets if the transfer was
  83292. + * halted before its normal completion. (Can't use the
  83293. + * hctsiz.xfersize field because that reflects the number of
  83294. + * bytes transferred via the AHB, not the USB).
  83295. + */
  83296. + length =
  83297. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  83298. + }
  83299. +
  83300. + return length;
  83301. +}
  83302. +
  83303. +/**
  83304. + * Updates the state of the URB after a Transfer Complete interrupt on the
  83305. + * host channel. Updates the actual_length field of the URB based on the
  83306. + * number of bytes transferred via the host channel. Sets the URB status
  83307. + * if the data transfer is finished.
  83308. + *
  83309. + * @return 1 if the data transfer specified by the URB is completely finished,
  83310. + * 0 otherwise.
  83311. + */
  83312. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  83313. + dwc_otg_hc_regs_t * hc_regs,
  83314. + dwc_otg_hcd_urb_t * urb,
  83315. + dwc_otg_qtd_t * qtd)
  83316. +{
  83317. + int xfer_done = 0;
  83318. + int short_read = 0;
  83319. +
  83320. + int xfer_length;
  83321. +
  83322. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  83323. + DWC_OTG_HC_XFER_COMPLETE,
  83324. + &short_read);
  83325. +
  83326. + /* non DWORD-aligned buffer case handling. */
  83327. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  83328. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  83329. + xfer_length);
  83330. + }
  83331. +
  83332. + urb->actual_length += xfer_length;
  83333. +
  83334. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  83335. + (urb->flags & URB_SEND_ZERO_PACKET)
  83336. + && (urb->actual_length == urb->length)
  83337. + && !(urb->length % hc->max_packet)) {
  83338. + xfer_done = 0;
  83339. + } else if (short_read || urb->actual_length >= urb->length) {
  83340. + xfer_done = 1;
  83341. + urb->status = 0;
  83342. + }
  83343. +
  83344. +#ifdef DEBUG
  83345. + {
  83346. + hctsiz_data_t hctsiz;
  83347. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  83348. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  83349. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  83350. + hc->hc_num);
  83351. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  83352. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  83353. + hctsiz.b.xfersize);
  83354. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  83355. + urb->length);
  83356. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  83357. + urb->actual_length);
  83358. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  83359. + short_read, xfer_done);
  83360. + }
  83361. +#endif
  83362. +
  83363. + return xfer_done;
  83364. +}
  83365. +
  83366. +/*
  83367. + * Save the starting data toggle for the next transfer. The data toggle is
  83368. + * saved in the QH for non-control transfers and it's saved in the QTD for
  83369. + * control transfers.
  83370. + */
  83371. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  83372. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  83373. +{
  83374. + hctsiz_data_t hctsiz;
  83375. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  83376. +
  83377. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  83378. + dwc_otg_qh_t *qh = hc->qh;
  83379. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  83380. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  83381. + } else {
  83382. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  83383. + }
  83384. + } else {
  83385. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  83386. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  83387. + } else {
  83388. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  83389. + }
  83390. + }
  83391. +}
  83392. +
  83393. +/**
  83394. + * Updates the state of an Isochronous URB when the transfer is stopped for
  83395. + * any reason. The fields of the current entry in the frame descriptor array
  83396. + * are set based on the transfer state and the input _halt_status. Completes
  83397. + * the Isochronous URB if all the URB frames have been completed.
  83398. + *
  83399. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  83400. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  83401. + */
  83402. +static dwc_otg_halt_status_e
  83403. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  83404. + dwc_hc_t * hc,
  83405. + dwc_otg_hc_regs_t * hc_regs,
  83406. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  83407. +{
  83408. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  83409. + dwc_otg_halt_status_e ret_val = halt_status;
  83410. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  83411. +
  83412. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  83413. + switch (halt_status) {
  83414. + case DWC_OTG_HC_XFER_COMPLETE:
  83415. + frame_desc->status = 0;
  83416. + frame_desc->actual_length =
  83417. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  83418. +
  83419. + /* non DWORD-aligned buffer case handling. */
  83420. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  83421. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  83422. + hc->qh->dw_align_buf, frame_desc->actual_length);
  83423. + }
  83424. +
  83425. + break;
  83426. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  83427. + urb->error_count++;
  83428. + if (hc->ep_is_in) {
  83429. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  83430. + } else {
  83431. + frame_desc->status = -DWC_E_COMMUNICATION;
  83432. + }
  83433. + frame_desc->actual_length = 0;
  83434. + break;
  83435. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  83436. + urb->error_count++;
  83437. + frame_desc->status = -DWC_E_OVERFLOW;
  83438. + /* Don't need to update actual_length in this case. */
  83439. + break;
  83440. + case DWC_OTG_HC_XFER_XACT_ERR:
  83441. + urb->error_count++;
  83442. + frame_desc->status = -DWC_E_PROTOCOL;
  83443. + frame_desc->actual_length =
  83444. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  83445. +
  83446. + /* non DWORD-aligned buffer case handling. */
  83447. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  83448. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  83449. + hc->qh->dw_align_buf, frame_desc->actual_length);
  83450. + }
  83451. + /* Skip whole frame */
  83452. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  83453. + hc->ep_is_in && hcd->core_if->dma_enable) {
  83454. + qtd->complete_split = 0;
  83455. + qtd->isoc_split_offset = 0;
  83456. + }
  83457. +
  83458. + break;
  83459. + default:
  83460. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  83461. + break;
  83462. + }
  83463. + if (++qtd->isoc_frame_index == urb->packet_count) {
  83464. + /*
  83465. + * urb->status is not used for isoc transfers.
  83466. + * The individual frame_desc statuses are used instead.
  83467. + */
  83468. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  83469. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  83470. + } else {
  83471. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  83472. + }
  83473. + return ret_val;
  83474. +}
  83475. +
  83476. +/**
  83477. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  83478. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  83479. + * still linked to the QH, the QH is added to the end of the inactive
  83480. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  83481. + * schedule if no more QTDs are linked to the QH.
  83482. + */
  83483. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  83484. +{
  83485. + int continue_split = 0;
  83486. + dwc_otg_qtd_t *qtd;
  83487. +
  83488. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  83489. +
  83490. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  83491. +
  83492. + if (qtd->complete_split) {
  83493. + continue_split = 1;
  83494. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  83495. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  83496. + continue_split = 1;
  83497. + }
  83498. +
  83499. + if (free_qtd) {
  83500. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  83501. + continue_split = 0;
  83502. + }
  83503. +
  83504. + qh->channel = NULL;
  83505. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  83506. +}
  83507. +
  83508. +/**
  83509. + * Releases a host channel for use by other transfers. Attempts to select and
  83510. + * queue more transactions since at least one host channel is available.
  83511. + *
  83512. + * @param hcd The HCD state structure.
  83513. + * @param hc The host channel to release.
  83514. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  83515. + * if the transfer is complete or an error has occurred.
  83516. + * @param halt_status Reason the channel is being released. This status
  83517. + * determines the actions taken by this function.
  83518. + */
  83519. +static void release_channel(dwc_otg_hcd_t * hcd,
  83520. + dwc_hc_t * hc,
  83521. + dwc_otg_qtd_t * qtd,
  83522. + dwc_otg_halt_status_e halt_status)
  83523. +{
  83524. + dwc_otg_transaction_type_e tr_type;
  83525. + int free_qtd;
  83526. + dwc_irqflags_t flags;
  83527. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  83528. +
  83529. + int hog_port = 0;
  83530. +
  83531. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  83532. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  83533. +
  83534. + if(fiq_fsm_enable && hc->do_split) {
  83535. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  83536. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  83537. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  83538. + hog_port = 0;
  83539. + }
  83540. + }
  83541. + }
  83542. +
  83543. + switch (halt_status) {
  83544. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  83545. + free_qtd = 1;
  83546. + break;
  83547. + case DWC_OTG_HC_XFER_AHB_ERR:
  83548. + case DWC_OTG_HC_XFER_STALL:
  83549. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  83550. + free_qtd = 1;
  83551. + break;
  83552. + case DWC_OTG_HC_XFER_XACT_ERR:
  83553. + if (qtd->error_count >= 3) {
  83554. + DWC_DEBUGPL(DBG_HCDV,
  83555. + " Complete URB with transaction error\n");
  83556. + free_qtd = 1;
  83557. + qtd->urb->status = -DWC_E_PROTOCOL;
  83558. + hcd->fops->complete(hcd, qtd->urb->priv,
  83559. + qtd->urb, -DWC_E_PROTOCOL);
  83560. + } else {
  83561. + free_qtd = 0;
  83562. + }
  83563. + break;
  83564. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  83565. + /*
  83566. + * The QTD has already been removed and the QH has been
  83567. + * deactivated. Don't want to do anything except release the
  83568. + * host channel and try to queue more transfers.
  83569. + */
  83570. + goto cleanup;
  83571. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  83572. + free_qtd = 0;
  83573. + break;
  83574. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  83575. + DWC_DEBUGPL(DBG_HCDV,
  83576. + " Complete URB with I/O error\n");
  83577. + free_qtd = 1;
  83578. + qtd->urb->status = -DWC_E_IO;
  83579. + hcd->fops->complete(hcd, qtd->urb->priv,
  83580. + qtd->urb, -DWC_E_IO);
  83581. + break;
  83582. + default:
  83583. + free_qtd = 0;
  83584. + break;
  83585. + }
  83586. +
  83587. + deactivate_qh(hcd, hc->qh, free_qtd);
  83588. +
  83589. +cleanup:
  83590. + /*
  83591. + * Release the host channel for use by other transfers. The cleanup
  83592. + * function clears the channel interrupt enables and conditions, so
  83593. + * there's no need to clear the Channel Halted interrupt separately.
  83594. + */
  83595. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  83596. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  83597. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  83598. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  83599. +
  83600. + if (!microframe_schedule) {
  83601. + switch (hc->ep_type) {
  83602. + case DWC_OTG_EP_TYPE_CONTROL:
  83603. + case DWC_OTG_EP_TYPE_BULK:
  83604. + hcd->non_periodic_channels--;
  83605. + break;
  83606. +
  83607. + default:
  83608. + /*
  83609. + * Don't release reservations for periodic channels here.
  83610. + * That's done when a periodic transfer is descheduled (i.e.
  83611. + * when the QH is removed from the periodic schedule).
  83612. + */
  83613. + break;
  83614. + }
  83615. + } else {
  83616. +
  83617. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  83618. + hcd->available_host_channels++;
  83619. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  83620. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  83621. + }
  83622. +
  83623. + /* Try to queue more transfers now that there's a free channel. */
  83624. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  83625. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  83626. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  83627. + }
  83628. +}
  83629. +
  83630. +/**
  83631. + * Halts a host channel. If the channel cannot be halted immediately because
  83632. + * the request queue is full, this function ensures that the FIFO empty
  83633. + * interrupt for the appropriate queue is enabled so that the halt request can
  83634. + * be queued when there is space in the request queue.
  83635. + *
  83636. + * This function may also be called in DMA mode. In that case, the channel is
  83637. + * simply released since the core always halts the channel automatically in
  83638. + * DMA mode.
  83639. + */
  83640. +static void halt_channel(dwc_otg_hcd_t * hcd,
  83641. + dwc_hc_t * hc,
  83642. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  83643. +{
  83644. + if (hcd->core_if->dma_enable) {
  83645. + release_channel(hcd, hc, qtd, halt_status);
  83646. + return;
  83647. + }
  83648. +
  83649. + /* Slave mode processing... */
  83650. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  83651. +
  83652. + if (hc->halt_on_queue) {
  83653. + gintmsk_data_t gintmsk = {.d32 = 0 };
  83654. + dwc_otg_core_global_regs_t *global_regs;
  83655. + global_regs = hcd->core_if->core_global_regs;
  83656. +
  83657. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  83658. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  83659. + /*
  83660. + * Make sure the Non-periodic Tx FIFO empty interrupt
  83661. + * is enabled so that the non-periodic schedule will
  83662. + * be processed.
  83663. + */
  83664. + gintmsk.b.nptxfempty = 1;
  83665. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  83666. + } else {
  83667. + /*
  83668. + * Move the QH from the periodic queued schedule to
  83669. + * the periodic assigned schedule. This allows the
  83670. + * halt to be queued when the periodic schedule is
  83671. + * processed.
  83672. + */
  83673. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  83674. + &hc->qh->qh_list_entry);
  83675. +
  83676. + /*
  83677. + * Make sure the Periodic Tx FIFO Empty interrupt is
  83678. + * enabled so that the periodic schedule will be
  83679. + * processed.
  83680. + */
  83681. + gintmsk.b.ptxfempty = 1;
  83682. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  83683. + }
  83684. + }
  83685. +}
  83686. +
  83687. +/**
  83688. + * Performs common cleanup for non-periodic transfers after a Transfer
  83689. + * Complete interrupt. This function should be called after any endpoint type
  83690. + * specific handling is finished to release the host channel.
  83691. + */
  83692. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  83693. + dwc_hc_t * hc,
  83694. + dwc_otg_hc_regs_t * hc_regs,
  83695. + dwc_otg_qtd_t * qtd,
  83696. + dwc_otg_halt_status_e halt_status)
  83697. +{
  83698. + hcint_data_t hcint;
  83699. +
  83700. + qtd->error_count = 0;
  83701. +
  83702. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  83703. + if (hcint.b.nyet) {
  83704. + /*
  83705. + * Got a NYET on the last transaction of the transfer. This
  83706. + * means that the endpoint should be in the PING state at the
  83707. + * beginning of the next transfer.
  83708. + */
  83709. + hc->qh->ping_state = 1;
  83710. + clear_hc_int(hc_regs, nyet);
  83711. + }
  83712. +
  83713. + /*
  83714. + * Always halt and release the host channel to make it available for
  83715. + * more transfers. There may still be more phases for a control
  83716. + * transfer or more data packets for a bulk transfer at this point,
  83717. + * but the host channel is still halted. A channel will be reassigned
  83718. + * to the transfer when the non-periodic schedule is processed after
  83719. + * the channel is released. This allows transactions to be queued
  83720. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  83721. + * Tx FIFO Empty interrupt if necessary.
  83722. + */
  83723. + if (hc->ep_is_in) {
  83724. + /*
  83725. + * IN transfers in Slave mode require an explicit disable to
  83726. + * halt the channel. (In DMA mode, this call simply releases
  83727. + * the channel.)
  83728. + */
  83729. + halt_channel(hcd, hc, qtd, halt_status);
  83730. + } else {
  83731. + /*
  83732. + * The channel is automatically disabled by the core for OUT
  83733. + * transfers in Slave mode.
  83734. + */
  83735. + release_channel(hcd, hc, qtd, halt_status);
  83736. + }
  83737. +}
  83738. +
  83739. +/**
  83740. + * Performs common cleanup for periodic transfers after a Transfer Complete
  83741. + * interrupt. This function should be called after any endpoint type specific
  83742. + * handling is finished to release the host channel.
  83743. + */
  83744. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  83745. + dwc_hc_t * hc,
  83746. + dwc_otg_hc_regs_t * hc_regs,
  83747. + dwc_otg_qtd_t * qtd,
  83748. + dwc_otg_halt_status_e halt_status)
  83749. +{
  83750. + hctsiz_data_t hctsiz;
  83751. + qtd->error_count = 0;
  83752. +
  83753. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  83754. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  83755. + /* Core halts channel in these cases. */
  83756. + release_channel(hcd, hc, qtd, halt_status);
  83757. + } else {
  83758. + /* Flush any outstanding requests from the Tx queue. */
  83759. + halt_channel(hcd, hc, qtd, halt_status);
  83760. + }
  83761. +}
  83762. +
  83763. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  83764. + dwc_hc_t * hc,
  83765. + dwc_otg_hc_regs_t * hc_regs,
  83766. + dwc_otg_qtd_t * qtd)
  83767. +{
  83768. + uint32_t len;
  83769. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  83770. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  83771. +
  83772. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  83773. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  83774. +
  83775. + if (!len) {
  83776. + qtd->complete_split = 0;
  83777. + qtd->isoc_split_offset = 0;
  83778. + return 0;
  83779. + }
  83780. + frame_desc->actual_length += len;
  83781. +
  83782. + if (hc->align_buff && len)
  83783. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  83784. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  83785. + qtd->isoc_split_offset += len;
  83786. +
  83787. + if (frame_desc->length == frame_desc->actual_length) {
  83788. + frame_desc->status = 0;
  83789. + qtd->isoc_frame_index++;
  83790. + qtd->complete_split = 0;
  83791. + qtd->isoc_split_offset = 0;
  83792. + }
  83793. +
  83794. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  83795. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  83796. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  83797. + } else {
  83798. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  83799. + }
  83800. +
  83801. + return 1; /* Indicates that channel released */
  83802. +}
  83803. +
  83804. +/**
  83805. + * Handles a host channel Transfer Complete interrupt. This handler may be
  83806. + * called in either DMA mode or Slave mode.
  83807. + */
  83808. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  83809. + dwc_hc_t * hc,
  83810. + dwc_otg_hc_regs_t * hc_regs,
  83811. + dwc_otg_qtd_t * qtd)
  83812. +{
  83813. + int urb_xfer_done;
  83814. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  83815. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  83816. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  83817. +
  83818. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  83819. + "Transfer Complete--\n", hc->hc_num);
  83820. +
  83821. + if (hcd->core_if->dma_desc_enable) {
  83822. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  83823. + if (pipe_type == UE_ISOCHRONOUS) {
  83824. + /* Do not disable the interrupt, just clear it */
  83825. + clear_hc_int(hc_regs, xfercomp);
  83826. + return 1;
  83827. + }
  83828. + goto handle_xfercomp_done;
  83829. + }
  83830. +
  83831. + /*
  83832. + * Handle xfer complete on CSPLIT.
  83833. + */
  83834. +
  83835. + if (hc->qh->do_split) {
  83836. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  83837. + && hcd->core_if->dma_enable) {
  83838. + if (qtd->complete_split
  83839. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  83840. + qtd))
  83841. + goto handle_xfercomp_done;
  83842. + } else {
  83843. + qtd->complete_split = 0;
  83844. + }
  83845. + }
  83846. +
  83847. + /* Update the QTD and URB states. */
  83848. + switch (pipe_type) {
  83849. + case UE_CONTROL:
  83850. + switch (qtd->control_phase) {
  83851. + case DWC_OTG_CONTROL_SETUP:
  83852. + if (urb->length > 0) {
  83853. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  83854. + } else {
  83855. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  83856. + }
  83857. + DWC_DEBUGPL(DBG_HCDV,
  83858. + " Control setup transaction done\n");
  83859. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  83860. + break;
  83861. + case DWC_OTG_CONTROL_DATA:{
  83862. + urb_xfer_done =
  83863. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  83864. + qtd);
  83865. + if (urb_xfer_done) {
  83866. + qtd->control_phase =
  83867. + DWC_OTG_CONTROL_STATUS;
  83868. + DWC_DEBUGPL(DBG_HCDV,
  83869. + " Control data transfer done\n");
  83870. + } else {
  83871. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  83872. + }
  83873. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  83874. + break;
  83875. + }
  83876. + case DWC_OTG_CONTROL_STATUS:
  83877. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  83878. + if (urb->status == -DWC_E_IN_PROGRESS) {
  83879. + urb->status = 0;
  83880. + }
  83881. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  83882. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  83883. + break;
  83884. + }
  83885. +
  83886. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  83887. + break;
  83888. + case UE_BULK:
  83889. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  83890. + urb_xfer_done =
  83891. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  83892. + if (urb_xfer_done) {
  83893. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  83894. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  83895. + } else {
  83896. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  83897. + }
  83898. +
  83899. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  83900. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  83901. + break;
  83902. + case UE_INTERRUPT:
  83903. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  83904. + urb_xfer_done =
  83905. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  83906. +
  83907. + /*
  83908. + * Interrupt URB is done on the first transfer complete
  83909. + * interrupt.
  83910. + */
  83911. + if (urb_xfer_done) {
  83912. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  83913. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  83914. + } else {
  83915. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  83916. + }
  83917. +
  83918. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  83919. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  83920. + break;
  83921. + case UE_ISOCHRONOUS:
  83922. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  83923. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  83924. + halt_status =
  83925. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  83926. + DWC_OTG_HC_XFER_COMPLETE);
  83927. + }
  83928. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  83929. + break;
  83930. + }
  83931. +
  83932. +handle_xfercomp_done:
  83933. + disable_hc_int(hc_regs, xfercompl);
  83934. +
  83935. + return 1;
  83936. +}
  83937. +
  83938. +/**
  83939. + * Handles a host channel STALL interrupt. This handler may be called in
  83940. + * either DMA mode or Slave mode.
  83941. + */
  83942. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  83943. + dwc_hc_t * hc,
  83944. + dwc_otg_hc_regs_t * hc_regs,
  83945. + dwc_otg_qtd_t * qtd)
  83946. +{
  83947. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  83948. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  83949. +
  83950. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  83951. + "STALL Received--\n", hc->hc_num);
  83952. +
  83953. + if (hcd->core_if->dma_desc_enable) {
  83954. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  83955. + goto handle_stall_done;
  83956. + }
  83957. +
  83958. + if (pipe_type == UE_CONTROL) {
  83959. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  83960. + }
  83961. +
  83962. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  83963. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  83964. + /*
  83965. + * USB protocol requires resetting the data toggle for bulk
  83966. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  83967. + * setup command is issued to the endpoint. Anticipate the
  83968. + * CLEAR_FEATURE command since a STALL has occurred and reset
  83969. + * the data toggle now.
  83970. + */
  83971. + hc->qh->data_toggle = 0;
  83972. + }
  83973. +
  83974. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  83975. +
  83976. +handle_stall_done:
  83977. + disable_hc_int(hc_regs, stall);
  83978. +
  83979. + return 1;
  83980. +}
  83981. +
  83982. +/*
  83983. + * Updates the state of the URB when a transfer has been stopped due to an
  83984. + * abnormal condition before the transfer completes. Modifies the
  83985. + * actual_length field of the URB to reflect the number of bytes that have
  83986. + * actually been transferred via the host channel.
  83987. + */
  83988. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  83989. + dwc_otg_hc_regs_t * hc_regs,
  83990. + dwc_otg_hcd_urb_t * urb,
  83991. + dwc_otg_qtd_t * qtd,
  83992. + dwc_otg_halt_status_e halt_status)
  83993. +{
  83994. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  83995. + halt_status, NULL);
  83996. + /* non DWORD-aligned buffer case handling. */
  83997. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  83998. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  83999. + bytes_transferred);
  84000. + }
  84001. +
  84002. + urb->actual_length += bytes_transferred;
  84003. +
  84004. +#ifdef DEBUG
  84005. + {
  84006. + hctsiz_data_t hctsiz;
  84007. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84008. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  84009. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  84010. + hc->hc_num);
  84011. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  84012. + hc->start_pkt_count);
  84013. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  84014. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  84015. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  84016. + bytes_transferred);
  84017. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  84018. + urb->actual_length);
  84019. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  84020. + urb->length);
  84021. + }
  84022. +#endif
  84023. +}
  84024. +
  84025. +/**
  84026. + * Handles a host channel NAK interrupt. This handler may be called in either
  84027. + * DMA mode or Slave mode.
  84028. + */
  84029. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  84030. + dwc_hc_t * hc,
  84031. + dwc_otg_hc_regs_t * hc_regs,
  84032. + dwc_otg_qtd_t * qtd)
  84033. +{
  84034. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84035. + "NAK Received--\n", hc->hc_num);
  84036. +
  84037. + /*
  84038. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  84039. + * the beginning of the next frame
  84040. + */
  84041. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  84042. + case UE_BULK:
  84043. + case UE_CONTROL:
  84044. + if (nak_holdoff && qtd->qh->do_split)
  84045. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  84046. + }
  84047. +
  84048. + /*
  84049. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  84050. + * interrupt. Re-start the SSPLIT transfer.
  84051. + */
  84052. + if (hc->do_split) {
  84053. + if (hc->complete_split) {
  84054. + qtd->error_count = 0;
  84055. + }
  84056. + qtd->complete_split = 0;
  84057. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  84058. + goto handle_nak_done;
  84059. + }
  84060. +
  84061. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  84062. + case UE_CONTROL:
  84063. + case UE_BULK:
  84064. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  84065. + /*
  84066. + * NAK interrupts are enabled on bulk/control IN
  84067. + * transfers in DMA mode for the sole purpose of
  84068. + * resetting the error count after a transaction error
  84069. + * occurs. The core will continue transferring data.
  84070. + * Disable other interrupts unmasked for the same
  84071. + * reason.
  84072. + */
  84073. + disable_hc_int(hc_regs, datatglerr);
  84074. + disable_hc_int(hc_regs, ack);
  84075. + qtd->error_count = 0;
  84076. + goto handle_nak_done;
  84077. + }
  84078. +
  84079. + /*
  84080. + * NAK interrupts normally occur during OUT transfers in DMA
  84081. + * or Slave mode. For IN transfers, more requests will be
  84082. + * queued as request queue space is available.
  84083. + */
  84084. + qtd->error_count = 0;
  84085. +
  84086. + if (!hc->qh->ping_state) {
  84087. + update_urb_state_xfer_intr(hc, hc_regs,
  84088. + qtd->urb, qtd,
  84089. + DWC_OTG_HC_XFER_NAK);
  84090. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84091. +
  84092. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  84093. + hc->qh->ping_state = 1;
  84094. + }
  84095. +
  84096. + /*
  84097. + * Halt the channel so the transfer can be re-started from
  84098. + * the appropriate point or the PING protocol will
  84099. + * start/continue.
  84100. + */
  84101. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  84102. + break;
  84103. + case UE_INTERRUPT:
  84104. + qtd->error_count = 0;
  84105. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  84106. + break;
  84107. + case UE_ISOCHRONOUS:
  84108. + /* Should never get called for isochronous transfers. */
  84109. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  84110. + break;
  84111. + }
  84112. +
  84113. +handle_nak_done:
  84114. + disable_hc_int(hc_regs, nak);
  84115. +
  84116. + return 1;
  84117. +}
  84118. +
  84119. +/**
  84120. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  84121. + * performing the PING protocol in Slave mode, when errors occur during
  84122. + * either Slave mode or DMA mode, and during Start Split transactions.
  84123. + */
  84124. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  84125. + dwc_hc_t * hc,
  84126. + dwc_otg_hc_regs_t * hc_regs,
  84127. + dwc_otg_qtd_t * qtd)
  84128. +{
  84129. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84130. + "ACK Received--\n", hc->hc_num);
  84131. +
  84132. + if (hc->do_split) {
  84133. + /*
  84134. + * Handle ACK on SSPLIT.
  84135. + * ACK should not occur in CSPLIT.
  84136. + */
  84137. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  84138. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  84139. + }
  84140. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  84141. + /* Don't need complete for isochronous out transfers. */
  84142. + qtd->complete_split = 1;
  84143. + }
  84144. +
  84145. + /* ISOC OUT */
  84146. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  84147. + switch (hc->xact_pos) {
  84148. + case DWC_HCSPLIT_XACTPOS_ALL:
  84149. + break;
  84150. + case DWC_HCSPLIT_XACTPOS_END:
  84151. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  84152. + qtd->isoc_split_offset = 0;
  84153. + break;
  84154. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  84155. + case DWC_HCSPLIT_XACTPOS_MID:
  84156. + /*
  84157. + * For BEGIN or MID, calculate the length for
  84158. + * the next microframe to determine the correct
  84159. + * SSPLIT token, either MID or END.
  84160. + */
  84161. + {
  84162. + struct dwc_otg_hcd_iso_packet_desc
  84163. + *frame_desc;
  84164. +
  84165. + frame_desc =
  84166. + &qtd->urb->
  84167. + iso_descs[qtd->isoc_frame_index];
  84168. + qtd->isoc_split_offset += 188;
  84169. +
  84170. + if ((frame_desc->length -
  84171. + qtd->isoc_split_offset) <= 188) {
  84172. + qtd->isoc_split_pos =
  84173. + DWC_HCSPLIT_XACTPOS_END;
  84174. + } else {
  84175. + qtd->isoc_split_pos =
  84176. + DWC_HCSPLIT_XACTPOS_MID;
  84177. + }
  84178. +
  84179. + }
  84180. + break;
  84181. + }
  84182. + } else {
  84183. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  84184. + }
  84185. + } else {
  84186. + /*
  84187. + * An unmasked ACK on a non-split DMA transaction is
  84188. + * for the sole purpose of resetting error counts. Disable other
  84189. + * interrupts unmasked for the same reason.
  84190. + */
  84191. + if(hcd->core_if->dma_enable) {
  84192. + disable_hc_int(hc_regs, datatglerr);
  84193. + disable_hc_int(hc_regs, nak);
  84194. + }
  84195. + qtd->error_count = 0;
  84196. +
  84197. + if (hc->qh->ping_state) {
  84198. + hc->qh->ping_state = 0;
  84199. + /*
  84200. + * Halt the channel so the transfer can be re-started
  84201. + * from the appropriate point. This only happens in
  84202. + * Slave mode. In DMA mode, the ping_state is cleared
  84203. + * when the transfer is started because the core
  84204. + * automatically executes the PING, then the transfer.
  84205. + */
  84206. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  84207. + }
  84208. + }
  84209. +
  84210. + /*
  84211. + * If the ACK occurred when _not_ in the PING state, let the channel
  84212. + * continue transferring data after clearing the error count.
  84213. + */
  84214. +
  84215. + disable_hc_int(hc_regs, ack);
  84216. +
  84217. + return 1;
  84218. +}
  84219. +
  84220. +/**
  84221. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  84222. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  84223. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  84224. + * handled in the xfercomp interrupt handler, not here. This handler may be
  84225. + * called in either DMA mode or Slave mode.
  84226. + */
  84227. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  84228. + dwc_hc_t * hc,
  84229. + dwc_otg_hc_regs_t * hc_regs,
  84230. + dwc_otg_qtd_t * qtd)
  84231. +{
  84232. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84233. + "NYET Received--\n", hc->hc_num);
  84234. +
  84235. + /*
  84236. + * NYET on CSPLIT
  84237. + * re-do the CSPLIT immediately on non-periodic
  84238. + */
  84239. + if (hc->do_split && hc->complete_split) {
  84240. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  84241. + && hcd->core_if->dma_enable) {
  84242. + qtd->complete_split = 0;
  84243. + qtd->isoc_split_offset = 0;
  84244. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  84245. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  84246. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  84247. + }
  84248. + else
  84249. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  84250. + goto handle_nyet_done;
  84251. + }
  84252. +
  84253. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  84254. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  84255. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  84256. +
  84257. + // With the FIQ running we only ever see the failed NYET
  84258. + if (dwc_full_frame_num(frnum) !=
  84259. + dwc_full_frame_num(hc->qh->sched_frame) ||
  84260. + fiq_fsm_enable) {
  84261. + /*
  84262. + * No longer in the same full speed frame.
  84263. + * Treat this as a transaction error.
  84264. + */
  84265. +#if 0
  84266. + /** @todo Fix system performance so this can
  84267. + * be treated as an error. Right now complete
  84268. + * splits cannot be scheduled precisely enough
  84269. + * due to other system activity, so this error
  84270. + * occurs regularly in Slave mode.
  84271. + */
  84272. + qtd->error_count++;
  84273. +#endif
  84274. + qtd->complete_split = 0;
  84275. + halt_channel(hcd, hc, qtd,
  84276. + DWC_OTG_HC_XFER_XACT_ERR);
  84277. + /** @todo add support for isoc release */
  84278. + goto handle_nyet_done;
  84279. + }
  84280. + }
  84281. +
  84282. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  84283. + goto handle_nyet_done;
  84284. + }
  84285. +
  84286. + hc->qh->ping_state = 1;
  84287. + qtd->error_count = 0;
  84288. +
  84289. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  84290. + DWC_OTG_HC_XFER_NYET);
  84291. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84292. +
  84293. + /*
  84294. + * Halt the channel and re-start the transfer so the PING
  84295. + * protocol will start.
  84296. + */
  84297. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  84298. +
  84299. +handle_nyet_done:
  84300. + disable_hc_int(hc_regs, nyet);
  84301. + return 1;
  84302. +}
  84303. +
  84304. +/**
  84305. + * Handles a host channel babble interrupt. This handler may be called in
  84306. + * either DMA mode or Slave mode.
  84307. + */
  84308. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  84309. + dwc_hc_t * hc,
  84310. + dwc_otg_hc_regs_t * hc_regs,
  84311. + dwc_otg_qtd_t * qtd)
  84312. +{
  84313. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84314. + "Babble Error--\n", hc->hc_num);
  84315. +
  84316. + if (hcd->core_if->dma_desc_enable) {
  84317. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  84318. + DWC_OTG_HC_XFER_BABBLE_ERR);
  84319. + goto handle_babble_done;
  84320. + }
  84321. +
  84322. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  84323. + hcd->fops->complete(hcd, qtd->urb->priv,
  84324. + qtd->urb, -DWC_E_OVERFLOW);
  84325. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  84326. + } else {
  84327. + dwc_otg_halt_status_e halt_status;
  84328. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  84329. + DWC_OTG_HC_XFER_BABBLE_ERR);
  84330. + halt_channel(hcd, hc, qtd, halt_status);
  84331. + }
  84332. +
  84333. +handle_babble_done:
  84334. + disable_hc_int(hc_regs, bblerr);
  84335. + return 1;
  84336. +}
  84337. +
  84338. +/**
  84339. + * Handles a host channel AHB error interrupt. This handler is only called in
  84340. + * DMA mode.
  84341. + */
  84342. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  84343. + dwc_hc_t * hc,
  84344. + dwc_otg_hc_regs_t * hc_regs,
  84345. + dwc_otg_qtd_t * qtd)
  84346. +{
  84347. + hcchar_data_t hcchar;
  84348. + hcsplt_data_t hcsplt;
  84349. + hctsiz_data_t hctsiz;
  84350. + uint32_t hcdma;
  84351. + char *pipetype, *speed;
  84352. +
  84353. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  84354. +
  84355. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84356. + "AHB Error--\n", hc->hc_num);
  84357. +
  84358. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  84359. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  84360. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84361. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  84362. +
  84363. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  84364. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  84365. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  84366. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  84367. + DWC_ERROR(" Device address: %d\n",
  84368. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  84369. + DWC_ERROR(" Endpoint: %d, %s\n",
  84370. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  84371. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  84372. +
  84373. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  84374. + case UE_CONTROL:
  84375. + pipetype = "CONTROL";
  84376. + break;
  84377. + case UE_BULK:
  84378. + pipetype = "BULK";
  84379. + break;
  84380. + case UE_INTERRUPT:
  84381. + pipetype = "INTERRUPT";
  84382. + break;
  84383. + case UE_ISOCHRONOUS:
  84384. + pipetype = "ISOCHRONOUS";
  84385. + break;
  84386. + default:
  84387. + pipetype = "UNKNOWN";
  84388. + break;
  84389. + }
  84390. +
  84391. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  84392. +
  84393. + switch (hc->speed) {
  84394. + case DWC_OTG_EP_SPEED_HIGH:
  84395. + speed = "HIGH";
  84396. + break;
  84397. + case DWC_OTG_EP_SPEED_FULL:
  84398. + speed = "FULL";
  84399. + break;
  84400. + case DWC_OTG_EP_SPEED_LOW:
  84401. + speed = "LOW";
  84402. + break;
  84403. + default:
  84404. + speed = "UNKNOWN";
  84405. + break;
  84406. + };
  84407. +
  84408. + DWC_ERROR(" Speed: %s\n", speed);
  84409. +
  84410. + DWC_ERROR(" Max packet size: %d\n",
  84411. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  84412. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  84413. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  84414. + urb->buf, (void *)urb->dma);
  84415. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  84416. + urb->setup_packet, (void *)urb->setup_dma);
  84417. + DWC_ERROR(" Interval: %d\n", urb->interval);
  84418. +
  84419. + /* Core haltes the channel for Descriptor DMA mode */
  84420. + if (hcd->core_if->dma_desc_enable) {
  84421. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  84422. + DWC_OTG_HC_XFER_AHB_ERR);
  84423. + goto handle_ahberr_done;
  84424. + }
  84425. +
  84426. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  84427. +
  84428. + /*
  84429. + * Force a channel halt. Don't call halt_channel because that won't
  84430. + * write to the HCCHARn register in DMA mode to force the halt.
  84431. + */
  84432. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  84433. +handle_ahberr_done:
  84434. + disable_hc_int(hc_regs, ahberr);
  84435. + return 1;
  84436. +}
  84437. +
  84438. +/**
  84439. + * Handles a host channel transaction error interrupt. This handler may be
  84440. + * called in either DMA mode or Slave mode.
  84441. + */
  84442. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  84443. + dwc_hc_t * hc,
  84444. + dwc_otg_hc_regs_t * hc_regs,
  84445. + dwc_otg_qtd_t * qtd)
  84446. +{
  84447. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84448. + "Transaction Error--\n", hc->hc_num);
  84449. +
  84450. + if (hcd->core_if->dma_desc_enable) {
  84451. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  84452. + DWC_OTG_HC_XFER_XACT_ERR);
  84453. + goto handle_xacterr_done;
  84454. + }
  84455. +
  84456. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  84457. + case UE_CONTROL:
  84458. + case UE_BULK:
  84459. + qtd->error_count++;
  84460. + if (!hc->qh->ping_state) {
  84461. +
  84462. + update_urb_state_xfer_intr(hc, hc_regs,
  84463. + qtd->urb, qtd,
  84464. + DWC_OTG_HC_XFER_XACT_ERR);
  84465. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84466. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  84467. + hc->qh->ping_state = 1;
  84468. + }
  84469. + }
  84470. +
  84471. + /*
  84472. + * Halt the channel so the transfer can be re-started from
  84473. + * the appropriate point or the PING protocol will start.
  84474. + */
  84475. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84476. + break;
  84477. + case UE_INTERRUPT:
  84478. + qtd->error_count++;
  84479. + if (hc->do_split && hc->complete_split) {
  84480. + qtd->complete_split = 0;
  84481. + }
  84482. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84483. + break;
  84484. + case UE_ISOCHRONOUS:
  84485. + {
  84486. + dwc_otg_halt_status_e halt_status;
  84487. + halt_status =
  84488. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  84489. + DWC_OTG_HC_XFER_XACT_ERR);
  84490. +
  84491. + halt_channel(hcd, hc, qtd, halt_status);
  84492. + }
  84493. + break;
  84494. + }
  84495. +handle_xacterr_done:
  84496. + disable_hc_int(hc_regs, xacterr);
  84497. +
  84498. + return 1;
  84499. +}
  84500. +
  84501. +/**
  84502. + * Handles a host channel frame overrun interrupt. This handler may be called
  84503. + * in either DMA mode or Slave mode.
  84504. + */
  84505. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  84506. + dwc_hc_t * hc,
  84507. + dwc_otg_hc_regs_t * hc_regs,
  84508. + dwc_otg_qtd_t * qtd)
  84509. +{
  84510. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84511. + "Frame Overrun--\n", hc->hc_num);
  84512. +
  84513. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  84514. + case UE_CONTROL:
  84515. + case UE_BULK:
  84516. + break;
  84517. + case UE_INTERRUPT:
  84518. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  84519. + break;
  84520. + case UE_ISOCHRONOUS:
  84521. + {
  84522. + dwc_otg_halt_status_e halt_status;
  84523. + halt_status =
  84524. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  84525. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  84526. +
  84527. + halt_channel(hcd, hc, qtd, halt_status);
  84528. + }
  84529. + break;
  84530. + }
  84531. +
  84532. + disable_hc_int(hc_regs, frmovrun);
  84533. +
  84534. + return 1;
  84535. +}
  84536. +
  84537. +/**
  84538. + * Handles a host channel data toggle error interrupt. This handler may be
  84539. + * called in either DMA mode or Slave mode.
  84540. + */
  84541. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  84542. + dwc_hc_t * hc,
  84543. + dwc_otg_hc_regs_t * hc_regs,
  84544. + dwc_otg_qtd_t * qtd)
  84545. +{
  84546. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84547. + "Data Toggle Error on %s transfer--\n",
  84548. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  84549. +
  84550. + /* Data toggles on split transactions cause the hc to halt.
  84551. + * restart transfer */
  84552. + if(hc->qh->do_split)
  84553. + {
  84554. + qtd->error_count++;
  84555. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84556. + update_urb_state_xfer_intr(hc, hc_regs,
  84557. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84558. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84559. + } else if (hc->ep_is_in) {
  84560. + /* An unmasked data toggle error on a non-split DMA transaction is
  84561. + * for the sole purpose of resetting error counts. Disable other
  84562. + * interrupts unmasked for the same reason.
  84563. + */
  84564. + if(hcd->core_if->dma_enable) {
  84565. + disable_hc_int(hc_regs, ack);
  84566. + disable_hc_int(hc_regs, nak);
  84567. + }
  84568. + qtd->error_count = 0;
  84569. + }
  84570. +
  84571. + disable_hc_int(hc_regs, datatglerr);
  84572. +
  84573. + return 1;
  84574. +}
  84575. +
  84576. +#ifdef DEBUG
  84577. +/**
  84578. + * This function is for debug only. It checks that a valid halt status is set
  84579. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  84580. + * taken and a warning is issued.
  84581. + * @return 1 if halt status is ok, 0 otherwise.
  84582. + */
  84583. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  84584. + dwc_hc_t * hc,
  84585. + dwc_otg_hc_regs_t * hc_regs,
  84586. + dwc_otg_qtd_t * qtd)
  84587. +{
  84588. + hcchar_data_t hcchar;
  84589. + hctsiz_data_t hctsiz;
  84590. + hcint_data_t hcint;
  84591. + hcintmsk_data_t hcintmsk;
  84592. + hcsplt_data_t hcsplt;
  84593. +
  84594. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  84595. + /*
  84596. + * This code is here only as a check. This condition should
  84597. + * never happen. Ignore the halt if it does occur.
  84598. + */
  84599. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  84600. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84601. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  84602. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  84603. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  84604. + DWC_WARN
  84605. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  84606. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  84607. + "hcint 0x%08x, hcintmsk 0x%08x, "
  84608. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  84609. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  84610. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  84611. +
  84612. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  84613. + __func__, hc->hc_num);
  84614. + DWC_WARN("\n");
  84615. + clear_hc_int(hc_regs, chhltd);
  84616. + return 0;
  84617. + }
  84618. +
  84619. + /*
  84620. + * This code is here only as a check. hcchar.chdis should
  84621. + * never be set when the halt interrupt occurs. Halt the
  84622. + * channel again if it does occur.
  84623. + */
  84624. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  84625. + if (hcchar.b.chdis) {
  84626. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  84627. + "hcchar 0x%08x, trying to halt again\n",
  84628. + __func__, hcchar.d32);
  84629. + clear_hc_int(hc_regs, chhltd);
  84630. + hc->halt_pending = 0;
  84631. + halt_channel(hcd, hc, qtd, hc->halt_status);
  84632. + return 0;
  84633. + }
  84634. +
  84635. + return 1;
  84636. +}
  84637. +#endif
  84638. +
  84639. +/**
  84640. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  84641. + * determines the reason the channel halted and proceeds accordingly.
  84642. + */
  84643. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  84644. + dwc_hc_t * hc,
  84645. + dwc_otg_hc_regs_t * hc_regs,
  84646. + dwc_otg_qtd_t * qtd)
  84647. +{
  84648. + int out_nak_enh = 0;
  84649. + hcint_data_t hcint;
  84650. + hcintmsk_data_t hcintmsk;
  84651. + /* For core with OUT NAK enhancement, the flow for high-
  84652. + * speed CONTROL/BULK OUT is handled a little differently.
  84653. + */
  84654. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  84655. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  84656. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  84657. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  84658. + out_nak_enh = 1;
  84659. + }
  84660. + }
  84661. +
  84662. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  84663. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  84664. + && !hcd->core_if->dma_desc_enable)) {
  84665. + /*
  84666. + * Just release the channel. A dequeue can happen on a
  84667. + * transfer timeout. In the case of an AHB Error, the channel
  84668. + * was forced to halt because there's no way to gracefully
  84669. + * recover.
  84670. + */
  84671. + if (hcd->core_if->dma_desc_enable)
  84672. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  84673. + hc->halt_status);
  84674. + else
  84675. + release_channel(hcd, hc, qtd, hc->halt_status);
  84676. + return;
  84677. + }
  84678. +
  84679. + /* Read the HCINTn register to determine the cause for the halt. */
  84680. +
  84681. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  84682. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  84683. +
  84684. + if (hcint.b.xfercomp) {
  84685. + /** @todo This is here because of a possible hardware bug. Spec
  84686. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  84687. + * interrupt w/ACK bit set should occur, but I only see the
  84688. + * XFERCOMP bit, even with it masked out. This is a workaround
  84689. + * for that behavior. Should fix this when hardware is fixed.
  84690. + */
  84691. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  84692. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  84693. + }
  84694. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  84695. + } else if (hcint.b.stall) {
  84696. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  84697. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  84698. + if (out_nak_enh) {
  84699. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  84700. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  84701. + qtd->error_count = 0;
  84702. + } else {
  84703. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  84704. + }
  84705. + }
  84706. +
  84707. + /*
  84708. + * Must handle xacterr before nak or ack. Could get a xacterr
  84709. + * at the same time as either of these on a BULK/CONTROL OUT
  84710. + * that started with a PING. The xacterr takes precedence.
  84711. + */
  84712. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  84713. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  84714. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  84715. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  84716. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  84717. + } else if (hcint.b.bblerr) {
  84718. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  84719. + } else if (hcint.b.frmovrun) {
  84720. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  84721. + } else if (hcint.b.datatglerr) {
  84722. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  84723. + } else if (!out_nak_enh) {
  84724. + if (hcint.b.nyet) {
  84725. + /*
  84726. + * Must handle nyet before nak or ack. Could get a nyet at the
  84727. + * same time as either of those on a BULK/CONTROL OUT that
  84728. + * started with a PING. The nyet takes precedence.
  84729. + */
  84730. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  84731. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  84732. + /*
  84733. + * If nak is not masked, it's because a non-split IN transfer
  84734. + * is in an error state. In that case, the nak is handled by
  84735. + * the nak interrupt handler, not here. Handle nak here for
  84736. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  84737. + * rewinding the buffer pointer.
  84738. + */
  84739. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  84740. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  84741. + /*
  84742. + * If ack is not masked, it's because a non-split IN transfer
  84743. + * is in an error state. In that case, the ack is handled by
  84744. + * the ack interrupt handler, not here. Handle ack here for
  84745. + * split transfers. Start splits halt on ACK.
  84746. + */
  84747. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  84748. + } else {
  84749. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  84750. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  84751. + /*
  84752. + * A periodic transfer halted with no other channel
  84753. + * interrupts set. Assume it was halted by the core
  84754. + * because it could not be completed in its scheduled
  84755. + * (micro)frame.
  84756. + */
  84757. +#ifdef DEBUG
  84758. + DWC_PRINTF
  84759. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  84760. + __func__, hc->hc_num);
  84761. +#endif
  84762. + halt_channel(hcd, hc, qtd,
  84763. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  84764. + } else {
  84765. + DWC_ERROR
  84766. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  84767. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  84768. + __func__, hc->hc_num, hcint.d32,
  84769. + DWC_READ_REG32(&hcd->
  84770. + core_if->core_global_regs->
  84771. + gintsts));
  84772. + /* Failthrough: use 3-strikes rule */
  84773. + qtd->error_count++;
  84774. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84775. + update_urb_state_xfer_intr(hc, hc_regs,
  84776. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84777. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84778. + }
  84779. +
  84780. + }
  84781. + } else {
  84782. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  84783. + hcint.d32);
  84784. + /* Failthrough: use 3-strikes rule */
  84785. + qtd->error_count++;
  84786. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84787. + update_urb_state_xfer_intr(hc, hc_regs,
  84788. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84789. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  84790. + }
  84791. +}
  84792. +
  84793. +/**
  84794. + * Handles a host channel Channel Halted interrupt.
  84795. + *
  84796. + * In slave mode, this handler is called only when the driver specifically
  84797. + * requests a halt. This occurs during handling other host channel interrupts
  84798. + * (e.g. nak, xacterr, stall, nyet, etc.).
  84799. + *
  84800. + * In DMA mode, this is the interrupt that occurs when the core has finished
  84801. + * processing a transfer on a channel. Other host channel interrupts (except
  84802. + * ahberr) are disabled in DMA mode.
  84803. + */
  84804. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  84805. + dwc_hc_t * hc,
  84806. + dwc_otg_hc_regs_t * hc_regs,
  84807. + dwc_otg_qtd_t * qtd)
  84808. +{
  84809. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84810. + "Channel Halted--\n", hc->hc_num);
  84811. +
  84812. + if (hcd->core_if->dma_enable) {
  84813. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  84814. + } else {
  84815. +#ifdef DEBUG
  84816. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  84817. + return 1;
  84818. + }
  84819. +#endif
  84820. + release_channel(hcd, hc, qtd, hc->halt_status);
  84821. + }
  84822. +
  84823. + return 1;
  84824. +}
  84825. +
  84826. +
  84827. +/**
  84828. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  84829. + * FIQ transfer completion
  84830. + * @hcd: Pointer to dwc_otg_hcd struct
  84831. + * @num: Host channel number
  84832. + *
  84833. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  84834. + * 2. Copy it from the dwc_otg_urb into the real URB
  84835. + */
  84836. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  84837. +{
  84838. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  84839. + int nr_frames = dwc_urb->packet_count;
  84840. + int i;
  84841. + hcint_data_t frame_hcint;
  84842. +
  84843. + for (i = 0; i < nr_frames; i++) {
  84844. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  84845. + if (frame_hcint.b.xfercomp) {
  84846. + dwc_urb->iso_descs[i].status = 0;
  84847. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  84848. + } else if (frame_hcint.b.frmovrun) {
  84849. + if (qh->ep_is_in)
  84850. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  84851. + else
  84852. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  84853. + dwc_urb->error_count++;
  84854. + dwc_urb->iso_descs[i].actual_length = 0;
  84855. + } else if (frame_hcint.b.xacterr) {
  84856. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  84857. + dwc_urb->error_count++;
  84858. + dwc_urb->iso_descs[i].actual_length = 0;
  84859. + } else if (frame_hcint.b.bblerr) {
  84860. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  84861. + dwc_urb->error_count++;
  84862. + dwc_urb->iso_descs[i].actual_length = 0;
  84863. + } else {
  84864. + /* Something went wrong */
  84865. + dwc_urb->iso_descs[i].status = -1;
  84866. + dwc_urb->iso_descs[i].actual_length = 0;
  84867. + dwc_urb->error_count++;
  84868. + }
  84869. + }
  84870. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  84871. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  84872. + hcd->fops->complete(hcd, dwc_urb->priv, dwc_urb, 0);
  84873. + release_channel(hcd, qh->channel, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  84874. +}
  84875. +
  84876. +/**
  84877. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  84878. + * @hcd: Pointer to dwc_otg_hcd struct
  84879. + * @num: Host channel number
  84880. + *
  84881. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  84882. + * Returns total length of data or -1 if the buffers were not used.
  84883. + *
  84884. + */
  84885. +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)
  84886. +{
  84887. + dwc_hc_t *hc = qh->channel;
  84888. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  84889. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  84890. + uint8_t *ptr = NULL;
  84891. + int index = 0, len = 0;
  84892. + int i = 0;
  84893. + if (hc->ep_is_in) {
  84894. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  84895. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  84896. + ptr = qtd->urb->buf;
  84897. + if (qh->ep_type == UE_ISOCHRONOUS) {
  84898. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  84899. + index = qtd->isoc_frame_index;
  84900. + ptr += qtd->urb->iso_descs[index].offset;
  84901. + } else {
  84902. + /* Need to increment by actual_length for interrupt IN */
  84903. + ptr += qtd->urb->actual_length;
  84904. + }
  84905. +
  84906. + for (i = 0; i < st->dma_info.index; i++) {
  84907. + len += st->dma_info.slot_len[i];
  84908. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  84909. + ptr += st->dma_info.slot_len[i];
  84910. + }
  84911. + return len;
  84912. + } else {
  84913. + /* OUT endpoints - nothing to do. */
  84914. + return -1;
  84915. + }
  84916. +
  84917. +}
  84918. +/**
  84919. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  84920. + * from a channel handled in the FIQ
  84921. + * @hcd: Pointer to dwc_otg_hcd struct
  84922. + * @num: Host channel number
  84923. + *
  84924. + * If a host channel interrupt was received by the IRQ and this was a channel
  84925. + * used by the FIQ, the execution flow for transfer completion is substantially
  84926. + * different from the normal (messy) path. This function and its friends handles
  84927. + * channel cleanup and transaction completion from a FIQ transaction.
  84928. + */
  84929. +int32_t dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  84930. +{
  84931. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  84932. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  84933. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  84934. + dwc_otg_qh_t *qh = hc->qh;
  84935. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  84936. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  84937. + int hostchannels = 0;
  84938. + int ret = 0;
  84939. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  84940. +
  84941. + hostchannels = hcd->available_host_channels;
  84942. + switch (st->fsm) {
  84943. + case FIQ_TEST:
  84944. + break;
  84945. +
  84946. + case FIQ_DEQUEUE_ISSUED:
  84947. + /* hc_halt was called. QTD no longer exists. */
  84948. + /* TODO: for a nonperiodic split transaction, need to issue a
  84949. + * CLEAR_TT_BUFFER hub command if we were in the start-split phase.
  84950. + */
  84951. + release_channel(hcd, hc, NULL, hc->halt_status);
  84952. + ret = 1;
  84953. + break;
  84954. +
  84955. + case FIQ_NP_SPLIT_DONE:
  84956. + /* Nonperiodic transaction complete. */
  84957. + if (!hc->ep_is_in) {
  84958. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  84959. + }
  84960. + if (hcint.b.xfercomp) {
  84961. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  84962. + } else if (hcint.b.nak) {
  84963. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  84964. + }
  84965. + ret = 1;
  84966. + break;
  84967. +
  84968. + case FIQ_NP_SPLIT_HS_ABORTED:
  84969. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  84970. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  84971. + * because there's no guarantee which order a non-periodic split happened in.
  84972. + * We could end up clearing a perfectly good transaction out of the buffer.
  84973. + */
  84974. + if (hcint.b.xacterr) {
  84975. + qtd->error_count += st->nr_errors;
  84976. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  84977. + } else if (hcint.b.ahberr) {
  84978. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  84979. + } else {
  84980. + local_fiq_disable();
  84981. + BUG();
  84982. + }
  84983. + break;
  84984. +
  84985. + case FIQ_NP_SPLIT_LS_ABORTED:
  84986. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  84987. + * STALL/data toggle error response on a CSPLIT */
  84988. + if (hcint.b.stall) {
  84989. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  84990. + } else if (hcint.b.datatglerr) {
  84991. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  84992. + } else if (hcint.b.bblerr) {
  84993. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  84994. + } else if (hcint.b.ahberr) {
  84995. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  84996. + } else {
  84997. + local_fiq_disable();
  84998. + BUG();
  84999. + }
  85000. + break;
  85001. +
  85002. + case FIQ_PER_SPLIT_DONE:
  85003. + /* Isoc IN or Interrupt IN/OUT */
  85004. +
  85005. + /* Flow control here is different from the normal execution by the driver.
  85006. + * We need to completely ignore most of the driver's method of handling
  85007. + * split transactions and do it ourselves.
  85008. + */
  85009. + if (hc->ep_type == UE_INTERRUPT) {
  85010. + if (hcint.b.nak) {
  85011. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  85012. + } else if (hc->ep_is_in) {
  85013. + int len;
  85014. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  85015. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  85016. + qtd->urb->actual_length += len;
  85017. + if (qtd->urb->actual_length >= qtd->urb->length) {
  85018. + qtd->urb->status = 0;
  85019. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  85020. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85021. + } else {
  85022. + /* Interrupt transfer not complete yet - is it a short read? */
  85023. + if (len < hc->max_packet) {
  85024. + /* Interrupt transaction complete */
  85025. + qtd->urb->status = 0;
  85026. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  85027. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85028. + } else {
  85029. + /* Further transactions required */
  85030. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85031. + }
  85032. + }
  85033. + } else {
  85034. + /* Interrupt OUT complete. */
  85035. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85036. + qtd->urb->actual_length += hc->xfer_len;
  85037. + if (qtd->urb->actual_length >= qtd->urb->length) {
  85038. + qtd->urb->status = 0;
  85039. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  85040. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85041. + } else {
  85042. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85043. + }
  85044. + }
  85045. + } else {
  85046. + /* ISOC IN complete. */
  85047. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  85048. + int len = 0;
  85049. + /* Record errors, update qtd. */
  85050. + if (st->nr_errors) {
  85051. + frame_desc->actual_length = 0;
  85052. + frame_desc->status = -DWC_E_PROTOCOL;
  85053. + } else {
  85054. + frame_desc->status = 0;
  85055. + /* Unswizzle dma */
  85056. + len = dwc_otg_fiq_unsetup_per_dma(hcd, qh, qtd, num);
  85057. + frame_desc->actual_length = len;
  85058. + }
  85059. + qtd->isoc_frame_index++;
  85060. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  85061. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  85062. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85063. + } else {
  85064. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85065. + }
  85066. + }
  85067. + break;
  85068. +
  85069. + case FIQ_PER_ISO_OUT_DONE: {
  85070. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  85071. + /* Record errors, update qtd. */
  85072. + if (st->nr_errors) {
  85073. + frame_desc->actual_length = 0;
  85074. + frame_desc->status = -DWC_E_PROTOCOL;
  85075. + } else {
  85076. + frame_desc->status = 0;
  85077. + frame_desc->actual_length = frame_desc->length;
  85078. + }
  85079. + qtd->isoc_frame_index++;
  85080. + qtd->isoc_split_offset = 0;
  85081. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  85082. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  85083. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85084. + } else {
  85085. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85086. + }
  85087. + }
  85088. + break;
  85089. +
  85090. + case FIQ_PER_SPLIT_NYET_ABORTED:
  85091. + /* Doh. lost the data. */
  85092. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  85093. + "- FIQ reported NYET. Data may have been lost.\n",
  85094. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  85095. + if (hc->ep_type == UE_ISOCHRONOUS) {
  85096. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  85097. + /* Record errors, update qtd. */
  85098. + frame_desc->actual_length = 0;
  85099. + frame_desc->status = -DWC_E_PROTOCOL;
  85100. + qtd->isoc_frame_index++;
  85101. + qtd->isoc_split_offset = 0;
  85102. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  85103. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  85104. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85105. + } else {
  85106. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85107. + }
  85108. + } else {
  85109. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  85110. + }
  85111. + break;
  85112. +
  85113. + case FIQ_HS_ISOC_DONE:
  85114. + /* The FIQ has performed a whole pile of isochronous transactions.
  85115. + * The status is recorded as the interrupt state should the transaction
  85116. + * fail.
  85117. + */
  85118. + dwc_otg_fiq_unmangle_isoc(hcd, qh, qtd, num);
  85119. + break;
  85120. +
  85121. + case FIQ_PER_SPLIT_LS_ABORTED:
  85122. + if (hcint.b.xacterr) {
  85123. + /* Hub has responded with an ERR packet. Device
  85124. + * has been unplugged or the port has been disabled.
  85125. + * TODO: need to issue a reset to the hub port. */
  85126. + qtd->error_count += 3;
  85127. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  85128. + } else if (hcint.b.stall) {
  85129. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  85130. + } else if (hcint.b.bblerr) {
  85131. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  85132. + } else {
  85133. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  85134. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  85135. + st->fsm, hc->dev_addr, hc->ep_num);
  85136. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  85137. + }
  85138. + break;
  85139. +
  85140. + case FIQ_PER_SPLIT_HS_ABORTED:
  85141. + /* Either the SSPLIT phase suffered transaction errors or something
  85142. + * unexpected happened.
  85143. + */
  85144. + qtd->error_count += 3;
  85145. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  85146. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  85147. + break;
  85148. +
  85149. + case FIQ_PER_SPLIT_TIMEOUT:
  85150. + /* Couldn't complete in the nominated frame */
  85151. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  85152. + "- FIQ timed out. Data may have been lost.\n",
  85153. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  85154. + if (hc->ep_type == UE_ISOCHRONOUS) {
  85155. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  85156. + /* Record errors, update qtd. */
  85157. + frame_desc->actual_length = 0;
  85158. + if (hc->ep_is_in) {
  85159. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  85160. + } else {
  85161. + frame_desc->status = -DWC_E_COMMUNICATION;
  85162. + }
  85163. + qtd->isoc_frame_index++;
  85164. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  85165. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  85166. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85167. + } else {
  85168. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85169. + }
  85170. + } else {
  85171. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  85172. + }
  85173. + break;
  85174. +
  85175. + default:
  85176. + local_fiq_disable();
  85177. + DWC_WARN("unexpected state received on hc=%d fsm=%d", hc->hc_num, st->fsm);
  85178. + BUG();
  85179. + }
  85180. + //if (hostchannels != hcd->available_host_channels) {
  85181. + /* should have incremented by now! */
  85182. + // BUG();
  85183. +// }
  85184. + return ret;
  85185. +}
  85186. +
  85187. +/** Handles interrupt for a specific Host Channel */
  85188. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  85189. +{
  85190. + int retval = 0;
  85191. + hcint_data_t hcint;
  85192. + hcintmsk_data_t hcintmsk;
  85193. + dwc_hc_t *hc;
  85194. + dwc_otg_hc_regs_t *hc_regs;
  85195. + dwc_otg_qtd_t *qtd;
  85196. +
  85197. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  85198. +
  85199. + hc = dwc_otg_hcd->hc_ptr_array[num];
  85200. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  85201. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  85202. + /* We are responding to a channel disable. Driver
  85203. + * state is cleared - our qtd has gone away.
  85204. + */
  85205. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  85206. + return 1;
  85207. + }
  85208. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  85209. +
  85210. + /*
  85211. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  85212. + * Execution path is fundamentally different for the channels after a FIQ has completed
  85213. + * a split transaction.
  85214. + */
  85215. + if (fiq_fsm_enable) {
  85216. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  85217. + case FIQ_PASSTHROUGH:
  85218. + break;
  85219. + case FIQ_PASSTHROUGH_ERRORSTATE:
  85220. + /* Hook into the error count */
  85221. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  85222. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  85223. + qtd->error_count = 0;
  85224. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  85225. + }
  85226. + break;
  85227. + default:
  85228. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  85229. + return 1;
  85230. + }
  85231. + }
  85232. +
  85233. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  85234. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  85235. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  85236. + if (!dwc_otg_hcd->core_if->dma_enable) {
  85237. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  85238. + hcint.b.chhltd = 0;
  85239. + }
  85240. + }
  85241. +
  85242. + if (hcint.b.xfercomp) {
  85243. + retval |=
  85244. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85245. + /*
  85246. + * If NYET occurred at same time as Xfer Complete, the NYET is
  85247. + * handled by the Xfer Complete interrupt handler. Don't want
  85248. + * to call the NYET interrupt handler in this case.
  85249. + */
  85250. + hcint.b.nyet = 0;
  85251. + }
  85252. + if (hcint.b.chhltd) {
  85253. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85254. + }
  85255. + if (hcint.b.ahberr) {
  85256. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85257. + }
  85258. + if (hcint.b.stall) {
  85259. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85260. + }
  85261. + if (hcint.b.nak) {
  85262. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85263. + }
  85264. + if (hcint.b.ack) {
  85265. + if(!hcint.b.chhltd)
  85266. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85267. + }
  85268. + if (hcint.b.nyet) {
  85269. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85270. + }
  85271. + if (hcint.b.xacterr) {
  85272. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85273. + }
  85274. + if (hcint.b.bblerr) {
  85275. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85276. + }
  85277. + if (hcint.b.frmovrun) {
  85278. + retval |=
  85279. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85280. + }
  85281. + if (hcint.b.datatglerr) {
  85282. + retval |=
  85283. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  85284. + }
  85285. +
  85286. + return retval;
  85287. +}
  85288. +#endif /* DWC_DEVICE_ONLY */
  85289. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  85290. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 1969-12-31 18:00:00.000000000 -0600
  85291. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 2014-12-11 14:05:39.556418001 -0600
  85292. @@ -0,0 +1,985 @@
  85293. +
  85294. +/* ==========================================================================
  85295. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  85296. + * $Revision: #20 $
  85297. + * $Date: 2011/10/26 $
  85298. + * $Change: 1872981 $
  85299. + *
  85300. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  85301. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  85302. + * otherwise expressly agreed to in writing between Synopsys and you.
  85303. + *
  85304. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  85305. + * any End User Software License Agreement or Agreement for Licensed Product
  85306. + * with Synopsys or any supplement thereto. You are permitted to use and
  85307. + * redistribute this Software in source and binary forms, with or without
  85308. + * modification, provided that redistributions of source code must retain this
  85309. + * notice. You may not view, use, disclose, copy or distribute this file or
  85310. + * any information contained herein except pursuant to this license grant from
  85311. + * Synopsys. If you do not agree with this notice, including the disclaimer
  85312. + * below, then you are not authorized to use the Software.
  85313. + *
  85314. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  85315. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  85316. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  85317. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  85318. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  85319. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  85320. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  85321. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  85322. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  85323. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  85324. + * DAMAGE.
  85325. + * ========================================================================== */
  85326. +#ifndef DWC_DEVICE_ONLY
  85327. +
  85328. +/**
  85329. + * @file
  85330. + *
  85331. + * This file contains the implementation of the HCD. In Linux, the HCD
  85332. + * implements the hc_driver API.
  85333. + */
  85334. +#include <linux/kernel.h>
  85335. +#include <linux/module.h>
  85336. +#include <linux/moduleparam.h>
  85337. +#include <linux/init.h>
  85338. +#include <linux/device.h>
  85339. +#include <linux/errno.h>
  85340. +#include <linux/list.h>
  85341. +#include <linux/interrupt.h>
  85342. +#include <linux/string.h>
  85343. +#include <linux/dma-mapping.h>
  85344. +#include <linux/version.h>
  85345. +#include <asm/io.h>
  85346. +#include <asm/fiq.h>
  85347. +#include <linux/usb.h>
  85348. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  85349. +#include <../drivers/usb/core/hcd.h>
  85350. +#else
  85351. +#include <linux/usb/hcd.h>
  85352. +#endif
  85353. +#include <asm/bug.h>
  85354. +
  85355. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  85356. +#define USB_URB_EP_LINKING 1
  85357. +#else
  85358. +#define USB_URB_EP_LINKING 0
  85359. +#endif
  85360. +
  85361. +#include "dwc_otg_hcd_if.h"
  85362. +#include "dwc_otg_dbg.h"
  85363. +#include "dwc_otg_driver.h"
  85364. +#include "dwc_otg_hcd.h"
  85365. +
  85366. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  85367. +
  85368. +/**
  85369. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  85370. + * qualified with its direction (possible 32 endpoints per device).
  85371. + */
  85372. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  85373. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  85374. +
  85375. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  85376. +
  85377. +extern bool fiq_enable;
  85378. +
  85379. +/** @name Linux HC Driver API Functions */
  85380. +/** @{ */
  85381. +/* manage i/o requests, device state */
  85382. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  85383. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  85384. + struct usb_host_endpoint *ep,
  85385. +#endif
  85386. + struct urb *urb, gfp_t mem_flags);
  85387. +
  85388. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  85389. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  85390. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  85391. +#endif
  85392. +#else /* kernels at or post 2.6.30 */
  85393. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  85394. + struct urb *urb, int status);
  85395. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  85396. +
  85397. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  85398. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  85399. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  85400. +#endif
  85401. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  85402. +extern int hcd_start(struct usb_hcd *hcd);
  85403. +extern void hcd_stop(struct usb_hcd *hcd);
  85404. +static int get_frame_number(struct usb_hcd *hcd);
  85405. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  85406. +extern int hub_control(struct usb_hcd *hcd,
  85407. + u16 typeReq,
  85408. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  85409. +
  85410. +struct wrapper_priv_data {
  85411. + dwc_otg_hcd_t *dwc_otg_hcd;
  85412. +};
  85413. +
  85414. +/** @} */
  85415. +
  85416. +static struct hc_driver dwc_otg_hc_driver = {
  85417. +
  85418. + .description = dwc_otg_hcd_name,
  85419. + .product_desc = "DWC OTG Controller",
  85420. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  85421. +
  85422. + .irq = dwc_otg_hcd_irq,
  85423. +
  85424. + .flags = HCD_MEMORY | HCD_USB2,
  85425. +
  85426. + //.reset =
  85427. + .start = hcd_start,
  85428. + //.suspend =
  85429. + //.resume =
  85430. + .stop = hcd_stop,
  85431. +
  85432. + .urb_enqueue = dwc_otg_urb_enqueue,
  85433. + .urb_dequeue = dwc_otg_urb_dequeue,
  85434. + .endpoint_disable = endpoint_disable,
  85435. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  85436. + .endpoint_reset = endpoint_reset,
  85437. +#endif
  85438. + .get_frame_number = get_frame_number,
  85439. +
  85440. + .hub_status_data = hub_status_data,
  85441. + .hub_control = hub_control,
  85442. + //.bus_suspend =
  85443. + //.bus_resume =
  85444. +};
  85445. +
  85446. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  85447. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  85448. +{
  85449. + struct wrapper_priv_data *p;
  85450. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  85451. + return p->dwc_otg_hcd;
  85452. +}
  85453. +
  85454. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  85455. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  85456. +{
  85457. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  85458. +}
  85459. +
  85460. +/** Gets the usb_host_endpoint associated with an URB. */
  85461. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  85462. +{
  85463. + struct usb_device *dev = urb->dev;
  85464. + int ep_num = usb_pipeendpoint(urb->pipe);
  85465. +
  85466. + if (usb_pipein(urb->pipe))
  85467. + return dev->ep_in[ep_num];
  85468. + else
  85469. + return dev->ep_out[ep_num];
  85470. +}
  85471. +
  85472. +static int _disconnect(dwc_otg_hcd_t * hcd)
  85473. +{
  85474. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  85475. +
  85476. + usb_hcd->self.is_b_host = 0;
  85477. + return 0;
  85478. +}
  85479. +
  85480. +static int _start(dwc_otg_hcd_t * hcd)
  85481. +{
  85482. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  85483. +
  85484. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  85485. + hcd_start(usb_hcd);
  85486. +
  85487. + return 0;
  85488. +}
  85489. +
  85490. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  85491. + uint32_t * port_addr)
  85492. +{
  85493. + struct urb *urb = (struct urb *)urb_handle;
  85494. + struct usb_bus *bus;
  85495. +#if 1 //GRAYG - temporary
  85496. + if (NULL == urb_handle)
  85497. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  85498. + if (NULL == urb->dev)
  85499. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  85500. + if (NULL == port_addr)
  85501. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  85502. +#endif
  85503. + if (urb->dev->tt) {
  85504. + if (NULL == urb->dev->tt->hub) {
  85505. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  85506. + __func__); //GRAYG
  85507. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  85508. + *hub_addr = 0; //GRAYG
  85509. + // we probably shouldn't have a transaction translator if
  85510. + // there's no associated hub?
  85511. + } else {
  85512. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  85513. + if (urb->dev->tt->hub == bus->root_hub)
  85514. + *hub_addr = 0;
  85515. + else
  85516. + *hub_addr = urb->dev->tt->hub->devnum;
  85517. + }
  85518. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  85519. + } else {
  85520. + *hub_addr = 0;
  85521. + *port_addr = urb->dev->ttport;
  85522. + }
  85523. + return 0;
  85524. +}
  85525. +
  85526. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  85527. +{
  85528. + struct urb *urb = (struct urb *)urb_handle;
  85529. + return urb->dev->speed;
  85530. +}
  85531. +
  85532. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  85533. +{
  85534. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  85535. + return usb_hcd->self.b_hnp_enable;
  85536. +}
  85537. +
  85538. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  85539. + struct urb *urb)
  85540. +{
  85541. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  85542. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  85543. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  85544. + } else {
  85545. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  85546. + }
  85547. +}
  85548. +
  85549. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  85550. + struct urb *urb)
  85551. +{
  85552. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  85553. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  85554. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  85555. + } else {
  85556. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  85557. + }
  85558. +}
  85559. +
  85560. +/**
  85561. + * Sets the final status of an URB and returns it to the device driver. Any
  85562. + * required cleanup of the URB is performed. The HCD lock should be held on
  85563. + * entry.
  85564. + */
  85565. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  85566. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  85567. +{
  85568. + struct urb *urb = (struct urb *)urb_handle;
  85569. + urb_tq_entry_t *new_entry;
  85570. + int rc = 0;
  85571. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  85572. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  85573. + __func__, urb, usb_pipedevice(urb->pipe),
  85574. + usb_pipeendpoint(urb->pipe),
  85575. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  85576. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  85577. + int i;
  85578. + for (i = 0; i < urb->number_of_packets; i++) {
  85579. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  85580. + i, urb->iso_frame_desc[i].status);
  85581. + }
  85582. + }
  85583. + }
  85584. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  85585. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  85586. + /* Convert status value. */
  85587. + switch (status) {
  85588. + case -DWC_E_PROTOCOL:
  85589. + status = -EPROTO;
  85590. + break;
  85591. + case -DWC_E_IN_PROGRESS:
  85592. + status = -EINPROGRESS;
  85593. + break;
  85594. + case -DWC_E_PIPE:
  85595. + status = -EPIPE;
  85596. + break;
  85597. + case -DWC_E_IO:
  85598. + status = -EIO;
  85599. + break;
  85600. + case -DWC_E_TIMEOUT:
  85601. + status = -ETIMEDOUT;
  85602. + break;
  85603. + case -DWC_E_OVERFLOW:
  85604. + status = -EOVERFLOW;
  85605. + break;
  85606. + case -DWC_E_SHUTDOWN:
  85607. + status = -ESHUTDOWN;
  85608. + break;
  85609. + default:
  85610. + if (status) {
  85611. + DWC_PRINTF("Uknown urb status %d\n", status);
  85612. +
  85613. + }
  85614. + }
  85615. +
  85616. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  85617. + int i;
  85618. +
  85619. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  85620. + for (i = 0; i < urb->number_of_packets; ++i) {
  85621. + urb->iso_frame_desc[i].actual_length =
  85622. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  85623. + (dwc_otg_urb, i);
  85624. + urb->iso_frame_desc[i].status =
  85625. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  85626. + }
  85627. + }
  85628. +
  85629. + urb->status = status;
  85630. + urb->hcpriv = NULL;
  85631. + if (!status) {
  85632. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  85633. + (urb->actual_length < urb->transfer_buffer_length)) {
  85634. + urb->status = -EREMOTEIO;
  85635. + }
  85636. + }
  85637. +
  85638. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  85639. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  85640. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  85641. + if (ep) {
  85642. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  85643. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  85644. + ep->hcpriv),
  85645. + urb);
  85646. + }
  85647. + }
  85648. + DWC_FREE(dwc_otg_urb);
  85649. + if (!new_entry) {
  85650. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  85651. + urb->status = -EPROTO;
  85652. + /* don't schedule the tasklet -
  85653. + * directly return the packet here with error. */
  85654. +#if USB_URB_EP_LINKING
  85655. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  85656. +#endif
  85657. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  85658. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  85659. +#else
  85660. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  85661. +#endif
  85662. + } else {
  85663. + new_entry->urb = urb;
  85664. +#if USB_URB_EP_LINKING
  85665. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  85666. + if(0 == rc) {
  85667. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  85668. + }
  85669. +#endif
  85670. + if(0 == rc) {
  85671. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  85672. + urb_tq_entries);
  85673. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  85674. + }
  85675. + }
  85676. + return 0;
  85677. +}
  85678. +
  85679. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  85680. + .start = _start,
  85681. + .disconnect = _disconnect,
  85682. + .hub_info = _hub_info,
  85683. + .speed = _speed,
  85684. + .complete = _complete,
  85685. + .get_b_hnp_enable = _get_b_hnp_enable,
  85686. +};
  85687. +
  85688. +static struct fiq_handler fh = {
  85689. + .name = "usb_fiq",
  85690. +};
  85691. +
  85692. +
  85693. +
  85694. +/**
  85695. + * Initializes the HCD. This function allocates memory for and initializes the
  85696. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  85697. + * USB bus with the core and calls the hc_driver->start() function. It returns
  85698. + * a negative error on failure.
  85699. + */
  85700. +int hcd_init(dwc_bus_dev_t *_dev)
  85701. +{
  85702. + struct usb_hcd *hcd = NULL;
  85703. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  85704. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  85705. + int retval = 0;
  85706. + u64 dmamask;
  85707. + struct pt_regs regs;
  85708. +
  85709. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  85710. +
  85711. + /* Set device flags indicating whether the HCD supports DMA. */
  85712. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  85713. + dmamask = DMA_BIT_MASK(32);
  85714. + else
  85715. + dmamask = 0;
  85716. +
  85717. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  85718. + dma_set_mask(&_dev->dev, dmamask);
  85719. + dma_set_coherent_mask(&_dev->dev, dmamask);
  85720. +#elif defined(PCI_INTERFACE)
  85721. + pci_set_dma_mask(_dev, dmamask);
  85722. + pci_set_consistent_dma_mask(_dev, dmamask);
  85723. +#endif
  85724. +
  85725. + /*
  85726. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  85727. + * Initialize the base HCD.
  85728. + */
  85729. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  85730. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  85731. +#else
  85732. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  85733. + hcd->has_tt = 1;
  85734. +// hcd->uses_new_polling = 1;
  85735. +// hcd->poll_rh = 0;
  85736. +#endif
  85737. + if (!hcd) {
  85738. + retval = -ENOMEM;
  85739. + goto error1;
  85740. + }
  85741. +
  85742. + hcd->regs = otg_dev->os_dep.base;
  85743. +
  85744. +
  85745. + /* Initialize the DWC OTG HCD. */
  85746. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  85747. + if (!dwc_otg_hcd) {
  85748. + goto error2;
  85749. + }
  85750. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  85751. + dwc_otg_hcd;
  85752. + otg_dev->hcd = dwc_otg_hcd;
  85753. +
  85754. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  85755. + goto error2;
  85756. + }
  85757. +
  85758. + if (fiq_enable)
  85759. + {
  85760. + if (claim_fiq(&fh)) {
  85761. + DWC_ERROR("Can't claim FIQ");
  85762. + goto error2;
  85763. + }
  85764. +
  85765. + DWC_WARN("FIQ at 0x%08x", (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
  85766. + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  85767. +
  85768. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  85769. + memset(&regs,0,sizeof(regs));
  85770. +
  85771. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  85772. + if (fiq_fsm_enable) {
  85773. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  85774. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  85775. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  85776. + } else {
  85777. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  85778. + }
  85779. +
  85780. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  85781. +
  85782. +// __show_regs(&regs);
  85783. + set_fiq_regs(&regs);
  85784. +
  85785. + //Set the mphi periph to the required registers
  85786. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  85787. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  85788. + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  85789. + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  85790. + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  85791. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  85792. + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
  85793. + //Enable mphi peripheral
  85794. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  85795. +#ifdef DEBUG
  85796. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  85797. + DWC_WARN("MPHI periph has been enabled");
  85798. + else
  85799. + DWC_WARN("MPHI periph has NOT been enabled");
  85800. +#endif
  85801. + // Enable FIQ interrupt from USB peripheral
  85802. + enable_fiq(INTERRUPT_VC_USB);
  85803. + local_fiq_enable();
  85804. + }
  85805. +
  85806. +
  85807. + otg_dev->hcd->otg_dev = otg_dev;
  85808. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  85809. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  85810. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  85811. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  85812. +#endif
  85813. + /* Don't support SG list at this point */
  85814. + hcd->self.sg_tablesize = 0;
  85815. +#endif
  85816. + /*
  85817. + * Finish generic HCD initialization and start the HCD. This function
  85818. + * allocates the DMA buffer pool, registers the USB bus, requests the
  85819. + * IRQ line, and calls hcd_start method.
  85820. + */
  85821. +#ifdef PLATFORM_INTERFACE
  85822. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED | IRQF_DISABLED);
  85823. +#else
  85824. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED | IRQF_DISABLED);
  85825. +#endif
  85826. + if (retval < 0) {
  85827. + goto error2;
  85828. + }
  85829. +
  85830. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  85831. + return 0;
  85832. +
  85833. +error2:
  85834. + usb_put_hcd(hcd);
  85835. +error1:
  85836. + return retval;
  85837. +}
  85838. +
  85839. +/**
  85840. + * Removes the HCD.
  85841. + * Frees memory and resources associated with the HCD and deregisters the bus.
  85842. + */
  85843. +void hcd_remove(dwc_bus_dev_t *_dev)
  85844. +{
  85845. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  85846. + dwc_otg_hcd_t *dwc_otg_hcd;
  85847. + struct usb_hcd *hcd;
  85848. +
  85849. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  85850. +
  85851. + if (!otg_dev) {
  85852. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  85853. + return;
  85854. + }
  85855. +
  85856. + dwc_otg_hcd = otg_dev->hcd;
  85857. +
  85858. + if (!dwc_otg_hcd) {
  85859. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  85860. + return;
  85861. + }
  85862. +
  85863. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  85864. +
  85865. + if (!hcd) {
  85866. + DWC_DEBUGPL(DBG_ANY,
  85867. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  85868. + __func__);
  85869. + return;
  85870. + }
  85871. + usb_remove_hcd(hcd);
  85872. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  85873. + dwc_otg_hcd_remove(dwc_otg_hcd);
  85874. + usb_put_hcd(hcd);
  85875. +}
  85876. +
  85877. +/* =========================================================================
  85878. + * Linux HC Driver Functions
  85879. + * ========================================================================= */
  85880. +
  85881. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  85882. + * mode operation. Activates the root port. Returns 0 on success and a negative
  85883. + * error code on failure. */
  85884. +int hcd_start(struct usb_hcd *hcd)
  85885. +{
  85886. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  85887. + struct usb_bus *bus;
  85888. +
  85889. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  85890. + bus = hcd_to_bus(hcd);
  85891. +
  85892. + hcd->state = HC_STATE_RUNNING;
  85893. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  85894. + return 0;
  85895. + }
  85896. +
  85897. + /* Initialize and connect root hub if one is not already attached */
  85898. + if (bus->root_hub) {
  85899. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  85900. + /* Inform the HUB driver to resume. */
  85901. + usb_hcd_resume_root_hub(hcd);
  85902. + }
  85903. +
  85904. + return 0;
  85905. +}
  85906. +
  85907. +/**
  85908. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  85909. + * stopped.
  85910. + */
  85911. +void hcd_stop(struct usb_hcd *hcd)
  85912. +{
  85913. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  85914. +
  85915. + dwc_otg_hcd_stop(dwc_otg_hcd);
  85916. +}
  85917. +
  85918. +/** Returns the current frame number. */
  85919. +static int get_frame_number(struct usb_hcd *hcd)
  85920. +{
  85921. + hprt0_data_t hprt0;
  85922. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  85923. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  85924. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  85925. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  85926. + else
  85927. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  85928. +}
  85929. +
  85930. +#ifdef DEBUG
  85931. +static void dump_urb_info(struct urb *urb, char *fn_name)
  85932. +{
  85933. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  85934. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  85935. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  85936. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  85937. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  85938. + char *pipetype;
  85939. + switch (usb_pipetype(urb->pipe)) {
  85940. +case PIPE_CONTROL:
  85941. +pipetype = "CONTROL"; break; case PIPE_BULK:
  85942. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  85943. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  85944. +pipetype = "ISOCHRONOUS"; break; default:
  85945. + pipetype = "UNKNOWN"; break;};
  85946. + pipetype;}
  85947. + )) ;
  85948. + DWC_PRINTF(" Speed: %s\n", ( {
  85949. + char *speed; switch (urb->dev->speed) {
  85950. +case USB_SPEED_HIGH:
  85951. +speed = "HIGH"; break; case USB_SPEED_FULL:
  85952. +speed = "FULL"; break; case USB_SPEED_LOW:
  85953. +speed = "LOW"; break; default:
  85954. + speed = "UNKNOWN"; break;};
  85955. + speed;}
  85956. + )) ;
  85957. + DWC_PRINTF(" Max packet size: %d\n",
  85958. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  85959. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  85960. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  85961. + urb->transfer_buffer, (void *)urb->transfer_dma);
  85962. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  85963. + urb->setup_packet, (void *)urb->setup_dma);
  85964. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  85965. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  85966. + int i;
  85967. + for (i = 0; i < urb->number_of_packets; i++) {
  85968. + DWC_PRINTF(" ISO Desc %d:\n", i);
  85969. + DWC_PRINTF(" offset: %d, length %d\n",
  85970. + urb->iso_frame_desc[i].offset,
  85971. + urb->iso_frame_desc[i].length);
  85972. + }
  85973. + }
  85974. +}
  85975. +#endif
  85976. +
  85977. +/** Starts processing a USB transfer request specified by a USB Request Block
  85978. + * (URB). mem_flags indicates the type of memory allocation to use while
  85979. + * processing this URB. */
  85980. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  85981. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  85982. + struct usb_host_endpoint *ep,
  85983. +#endif
  85984. + struct urb *urb, gfp_t mem_flags)
  85985. +{
  85986. + int retval = 0;
  85987. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  85988. + struct usb_host_endpoint *ep = urb->ep;
  85989. +#endif
  85990. + dwc_irqflags_t irqflags;
  85991. + void **ref_ep_hcpriv = &ep->hcpriv;
  85992. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  85993. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  85994. + int i;
  85995. + int alloc_bandwidth = 0;
  85996. + uint8_t ep_type = 0;
  85997. + uint32_t flags = 0;
  85998. + void *buf;
  85999. +
  86000. +#ifdef DEBUG
  86001. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  86002. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  86003. + }
  86004. +#endif
  86005. +
  86006. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  86007. + return -EINVAL;
  86008. +
  86009. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  86010. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  86011. + if (!dwc_otg_hcd_is_bandwidth_allocated
  86012. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  86013. + alloc_bandwidth = 1;
  86014. + }
  86015. + }
  86016. +
  86017. + switch (usb_pipetype(urb->pipe)) {
  86018. + case PIPE_CONTROL:
  86019. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  86020. + break;
  86021. + case PIPE_ISOCHRONOUS:
  86022. + ep_type = USB_ENDPOINT_XFER_ISOC;
  86023. + break;
  86024. + case PIPE_BULK:
  86025. + ep_type = USB_ENDPOINT_XFER_BULK;
  86026. + break;
  86027. + case PIPE_INTERRUPT:
  86028. + ep_type = USB_ENDPOINT_XFER_INT;
  86029. + break;
  86030. + default:
  86031. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  86032. + }
  86033. +
  86034. + /* # of packets is often 0 - do we really need to call this then? */
  86035. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  86036. + urb->number_of_packets,
  86037. + mem_flags == GFP_ATOMIC ? 1 : 0);
  86038. +
  86039. + if(dwc_otg_urb == NULL)
  86040. + return -ENOMEM;
  86041. +
  86042. + if (!dwc_otg_urb && urb->number_of_packets)
  86043. + return -ENOMEM;
  86044. +
  86045. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  86046. + usb_pipeendpoint(urb->pipe), ep_type,
  86047. + usb_pipein(urb->pipe),
  86048. + usb_maxpacket(urb->dev, urb->pipe,
  86049. + !(usb_pipein(urb->pipe))));
  86050. +
  86051. + buf = urb->transfer_buffer;
  86052. + if (hcd->self.uses_dma) {
  86053. + /*
  86054. + * Calculate virtual address from physical address,
  86055. + * because some class driver may not fill transfer_buffer.
  86056. + * In Buffer DMA mode virual address is used,
  86057. + * when handling non DWORD aligned buffers.
  86058. + */
  86059. + //buf = phys_to_virt(urb->transfer_dma);
  86060. + // DMA addresses are bus addresses not physical addresses!
  86061. + buf = dma_to_virt(&urb->dev->dev, urb->transfer_dma);
  86062. + }
  86063. +
  86064. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  86065. + flags |= URB_GIVEBACK_ASAP;
  86066. + if (urb->transfer_flags & URB_ZERO_PACKET)
  86067. + flags |= URB_SEND_ZERO_PACKET;
  86068. +
  86069. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  86070. + urb->transfer_dma,
  86071. + urb->transfer_buffer_length,
  86072. + urb->setup_packet,
  86073. + urb->setup_dma, flags, urb->interval);
  86074. +
  86075. + for (i = 0; i < urb->number_of_packets; ++i) {
  86076. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  86077. + urb->
  86078. + iso_frame_desc[i].offset,
  86079. + urb->
  86080. + iso_frame_desc[i].length);
  86081. + }
  86082. +
  86083. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  86084. + urb->hcpriv = dwc_otg_urb;
  86085. +#if USB_URB_EP_LINKING
  86086. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  86087. + if (0 == retval)
  86088. +#endif
  86089. + {
  86090. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  86091. + /*(dwc_otg_qh_t **)*/
  86092. + ref_ep_hcpriv, 1);
  86093. + if (0 == retval) {
  86094. + if (alloc_bandwidth) {
  86095. + allocate_bus_bandwidth(hcd,
  86096. + dwc_otg_hcd_get_ep_bandwidth(
  86097. + dwc_otg_hcd, *ref_ep_hcpriv),
  86098. + urb);
  86099. + }
  86100. + } else {
  86101. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  86102. +#if USB_URB_EP_LINKING
  86103. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  86104. +#endif
  86105. + DWC_FREE(dwc_otg_urb);
  86106. + urb->hcpriv = NULL;
  86107. + if (retval == -DWC_E_NO_DEVICE)
  86108. + retval = -ENODEV;
  86109. + }
  86110. + }
  86111. +#if USB_URB_EP_LINKING
  86112. + else
  86113. + {
  86114. + DWC_FREE(dwc_otg_urb);
  86115. + urb->hcpriv = NULL;
  86116. + }
  86117. +#endif
  86118. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  86119. + return retval;
  86120. +}
  86121. +
  86122. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  86123. + * success. */
  86124. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  86125. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  86126. +#else
  86127. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  86128. +#endif
  86129. +{
  86130. + dwc_irqflags_t flags;
  86131. + dwc_otg_hcd_t *dwc_otg_hcd;
  86132. + int rc;
  86133. +
  86134. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  86135. +
  86136. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86137. +
  86138. +#ifdef DEBUG
  86139. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  86140. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  86141. + }
  86142. +#endif
  86143. +
  86144. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  86145. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  86146. + if (0 == rc) {
  86147. + if(urb->hcpriv != NULL) {
  86148. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  86149. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  86150. +
  86151. + DWC_FREE(urb->hcpriv);
  86152. + urb->hcpriv = NULL;
  86153. + }
  86154. + }
  86155. +
  86156. + if (0 == rc) {
  86157. + /* Higher layer software sets URB status. */
  86158. +#if USB_URB_EP_LINKING
  86159. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  86160. +#endif
  86161. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  86162. +
  86163. +
  86164. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  86165. + usb_hcd_giveback_urb(hcd, urb);
  86166. +#else
  86167. + usb_hcd_giveback_urb(hcd, urb, status);
  86168. +#endif
  86169. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  86170. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  86171. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  86172. + }
  86173. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  86174. + } else {
  86175. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  86176. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  86177. + rc);
  86178. + }
  86179. +
  86180. + return rc;
  86181. +}
  86182. +
  86183. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  86184. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  86185. + * must already be dequeued. */
  86186. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  86187. +{
  86188. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86189. +
  86190. + DWC_DEBUGPL(DBG_HCD,
  86191. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  86192. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  86193. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  86194. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  86195. + ep->hcpriv = NULL;
  86196. +}
  86197. +
  86198. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  86199. +/* Resets endpoint specific parameter values, in current version used to reset
  86200. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  86201. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  86202. +{
  86203. + dwc_irqflags_t flags;
  86204. + struct usb_device *udev = NULL;
  86205. + int epnum = usb_endpoint_num(&ep->desc);
  86206. + int is_out = usb_endpoint_dir_out(&ep->desc);
  86207. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  86208. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86209. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  86210. +
  86211. + if (dev)
  86212. + udev = to_usb_device(dev);
  86213. + else
  86214. + return;
  86215. +
  86216. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  86217. +
  86218. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  86219. + usb_settoggle(udev, epnum, is_out, 0);
  86220. + if (is_control)
  86221. + usb_settoggle(udev, epnum, !is_out, 0);
  86222. +
  86223. + if (ep->hcpriv) {
  86224. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  86225. + }
  86226. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  86227. +}
  86228. +#endif
  86229. +
  86230. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  86231. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  86232. + * interrupt.
  86233. + *
  86234. + * This function is called by the USB core when an interrupt occurs */
  86235. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  86236. +{
  86237. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86238. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  86239. + if (retval != 0) {
  86240. + S3C2410X_CLEAR_EINTPEND();
  86241. + }
  86242. + return IRQ_RETVAL(retval);
  86243. +}
  86244. +
  86245. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  86246. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  86247. + * is the status change indicator for the single root port. Returns 1 if either
  86248. + * change indicator is 1, otherwise returns 0. */
  86249. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  86250. +{
  86251. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86252. +
  86253. + buf[0] = 0;
  86254. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  86255. +
  86256. + return (buf[0] != 0);
  86257. +}
  86258. +
  86259. +/** Handles hub class-specific requests. */
  86260. +int hub_control(struct usb_hcd *hcd,
  86261. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  86262. +{
  86263. + int retval;
  86264. +
  86265. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  86266. + typeReq, wValue, wIndex, buf, wLength);
  86267. +
  86268. + switch (retval) {
  86269. + case -DWC_E_INVALID:
  86270. + retval = -EINVAL;
  86271. + break;
  86272. + }
  86273. +
  86274. + return retval;
  86275. +}
  86276. +
  86277. +#endif /* DWC_DEVICE_ONLY */
  86278. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  86279. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 1969-12-31 18:00:00.000000000 -0600
  86280. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 2014-12-11 14:05:39.556418001 -0600
  86281. @@ -0,0 +1,943 @@
  86282. +/* ==========================================================================
  86283. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  86284. + * $Revision: #44 $
  86285. + * $Date: 2011/10/26 $
  86286. + * $Change: 1873028 $
  86287. + *
  86288. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  86289. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  86290. + * otherwise expressly agreed to in writing between Synopsys and you.
  86291. + *
  86292. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  86293. + * any End User Software License Agreement or Agreement for Licensed Product
  86294. + * with Synopsys or any supplement thereto. You are permitted to use and
  86295. + * redistribute this Software in source and binary forms, with or without
  86296. + * modification, provided that redistributions of source code must retain this
  86297. + * notice. You may not view, use, disclose, copy or distribute this file or
  86298. + * any information contained herein except pursuant to this license grant from
  86299. + * Synopsys. If you do not agree with this notice, including the disclaimer
  86300. + * below, then you are not authorized to use the Software.
  86301. + *
  86302. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  86303. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  86304. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  86305. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  86306. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  86307. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86308. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  86309. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  86310. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  86311. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  86312. + * DAMAGE.
  86313. + * ========================================================================== */
  86314. +#ifndef DWC_DEVICE_ONLY
  86315. +
  86316. +/**
  86317. + * @file
  86318. + *
  86319. + * This file contains the functions to manage Queue Heads and Queue
  86320. + * Transfer Descriptors.
  86321. + */
  86322. +
  86323. +#include "dwc_otg_hcd.h"
  86324. +#include "dwc_otg_regs.h"
  86325. +
  86326. +extern bool microframe_schedule;
  86327. +
  86328. +/**
  86329. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  86330. + * removed from a list. QTD list should already be empty if called from URB
  86331. + * Dequeue.
  86332. + *
  86333. + * @param hcd HCD instance.
  86334. + * @param qh The QH to free.
  86335. + */
  86336. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  86337. +{
  86338. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  86339. + dwc_irqflags_t flags;
  86340. +
  86341. + /* Free each QTD in the QTD list */
  86342. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  86343. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  86344. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  86345. + dwc_otg_hcd_qtd_free(qtd);
  86346. + }
  86347. +
  86348. + if (hcd->core_if->dma_desc_enable) {
  86349. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  86350. + } else if (qh->dw_align_buf) {
  86351. + uint32_t buf_size;
  86352. + if (qh->ep_type == UE_ISOCHRONOUS) {
  86353. + buf_size = 4096;
  86354. + } else {
  86355. + buf_size = hcd->core_if->core_params->max_transfer_size;
  86356. + }
  86357. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  86358. + }
  86359. +
  86360. + DWC_FREE(qh);
  86361. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  86362. + return;
  86363. +}
  86364. +
  86365. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  86366. +#define HS_HOST_DELAY 5 /* nanoseconds */
  86367. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  86368. +#define HUB_LS_SETUP 333 /* nanoseconds */
  86369. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  86370. + /* convert & round nanoseconds to microseconds */
  86371. +
  86372. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  86373. +{
  86374. + unsigned long retval;
  86375. +
  86376. + switch (speed) {
  86377. + case USB_SPEED_HIGH:
  86378. + if (is_isoc) {
  86379. + retval =
  86380. + ((38 * 8 * 2083) +
  86381. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  86382. + HS_HOST_DELAY;
  86383. + } else {
  86384. + retval =
  86385. + ((55 * 8 * 2083) +
  86386. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  86387. + HS_HOST_DELAY;
  86388. + }
  86389. + break;
  86390. + case USB_SPEED_FULL:
  86391. + if (is_isoc) {
  86392. + retval =
  86393. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  86394. + if (is_in) {
  86395. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  86396. + } else {
  86397. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  86398. + }
  86399. + } else {
  86400. + retval =
  86401. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  86402. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  86403. + }
  86404. + break;
  86405. + case USB_SPEED_LOW:
  86406. + if (is_in) {
  86407. + retval =
  86408. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  86409. + 1000;
  86410. + retval =
  86411. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  86412. + retval;
  86413. + } else {
  86414. + retval =
  86415. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  86416. + 1000;
  86417. + retval =
  86418. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  86419. + retval;
  86420. + }
  86421. + break;
  86422. + default:
  86423. + DWC_WARN("Unknown device speed\n");
  86424. + retval = -1;
  86425. + }
  86426. +
  86427. + return NS_TO_US(retval);
  86428. +}
  86429. +
  86430. +/**
  86431. + * Initializes a QH structure.
  86432. + *
  86433. + * @param hcd The HCD state structure for the DWC OTG controller.
  86434. + * @param qh The QH to init.
  86435. + * @param urb Holds the information about the device/endpoint that we need
  86436. + * to initialize the QH.
  86437. + */
  86438. +#define SCHEDULE_SLOP 10
  86439. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  86440. +{
  86441. + char *speed, *type;
  86442. + int dev_speed;
  86443. + uint32_t hub_addr, hub_port;
  86444. +
  86445. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  86446. +
  86447. + /* Initialize QH */
  86448. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  86449. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  86450. +
  86451. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  86452. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  86453. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  86454. + DWC_LIST_INIT(&qh->qh_list_entry);
  86455. + qh->channel = NULL;
  86456. +
  86457. + /* FS/LS Enpoint on HS Hub
  86458. + * NOT virtual root hub */
  86459. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  86460. +
  86461. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  86462. + qh->do_split = 0;
  86463. + if (microframe_schedule)
  86464. + qh->speed = dev_speed;
  86465. +
  86466. + qh->nak_frame = 0xffff;
  86467. +
  86468. + if (((dev_speed == USB_SPEED_LOW) ||
  86469. + (dev_speed == USB_SPEED_FULL)) &&
  86470. + (hub_addr != 0 && hub_addr != 1)) {
  86471. + DWC_DEBUGPL(DBG_HCD,
  86472. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  86473. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  86474. + hub_port);
  86475. + qh->do_split = 1;
  86476. + qh->skip_count = 0;
  86477. + }
  86478. +
  86479. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  86480. + /* Compute scheduling parameters once and save them. */
  86481. + hprt0_data_t hprt;
  86482. +
  86483. + /** @todo Account for split transfers in the bus time. */
  86484. + int bytecount =
  86485. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  86486. +
  86487. + qh->usecs =
  86488. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  86489. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  86490. + bytecount);
  86491. + /* Start in a slightly future (micro)frame. */
  86492. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  86493. + SCHEDULE_SLOP);
  86494. + qh->interval = urb->interval;
  86495. +
  86496. +#if 0
  86497. + /* Increase interrupt polling rate for debugging. */
  86498. + if (qh->ep_type == UE_INTERRUPT) {
  86499. + qh->interval = 8;
  86500. + }
  86501. +#endif
  86502. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  86503. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  86504. + ((dev_speed == USB_SPEED_LOW) ||
  86505. + (dev_speed == USB_SPEED_FULL))) {
  86506. + qh->interval *= 8;
  86507. + qh->sched_frame |= 0x7;
  86508. + qh->start_split_frame = qh->sched_frame;
  86509. + }
  86510. +
  86511. + }
  86512. +
  86513. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  86514. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  86515. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  86516. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  86517. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  86518. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  86519. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  86520. + switch (dev_speed) {
  86521. + case USB_SPEED_LOW:
  86522. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  86523. + speed = "low";
  86524. + break;
  86525. + case USB_SPEED_FULL:
  86526. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  86527. + speed = "full";
  86528. + break;
  86529. + case USB_SPEED_HIGH:
  86530. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  86531. + speed = "high";
  86532. + break;
  86533. + default:
  86534. + speed = "?";
  86535. + break;
  86536. + }
  86537. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  86538. +
  86539. + switch (qh->ep_type) {
  86540. + case UE_ISOCHRONOUS:
  86541. + type = "isochronous";
  86542. + break;
  86543. + case UE_INTERRUPT:
  86544. + type = "interrupt";
  86545. + break;
  86546. + case UE_CONTROL:
  86547. + type = "control";
  86548. + break;
  86549. + case UE_BULK:
  86550. + type = "bulk";
  86551. + break;
  86552. + default:
  86553. + type = "?";
  86554. + break;
  86555. + }
  86556. +
  86557. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  86558. +
  86559. +#ifdef DEBUG
  86560. + if (qh->ep_type == UE_INTERRUPT) {
  86561. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  86562. + qh->usecs);
  86563. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  86564. + qh->interval);
  86565. + }
  86566. +#endif
  86567. +
  86568. +}
  86569. +
  86570. +/**
  86571. + * This function allocates and initializes a QH.
  86572. + *
  86573. + * @param hcd The HCD state structure for the DWC OTG controller.
  86574. + * @param urb Holds the information about the device/endpoint that we need
  86575. + * to initialize the QH.
  86576. + * @param atomic_alloc Flag to do atomic allocation if needed
  86577. + *
  86578. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  86579. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  86580. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  86581. +{
  86582. + dwc_otg_qh_t *qh;
  86583. +
  86584. + /* Allocate memory */
  86585. + /** @todo add memflags argument */
  86586. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  86587. + if (qh == NULL) {
  86588. + DWC_ERROR("qh allocation failed");
  86589. + return NULL;
  86590. + }
  86591. +
  86592. + qh_init(hcd, qh, urb);
  86593. +
  86594. + if (hcd->core_if->dma_desc_enable
  86595. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  86596. + dwc_otg_hcd_qh_free(hcd, qh);
  86597. + return NULL;
  86598. + }
  86599. +
  86600. + return qh;
  86601. +}
  86602. +
  86603. +/* microframe_schedule=0 start */
  86604. +
  86605. +/**
  86606. + * Checks that a channel is available for a periodic transfer.
  86607. + *
  86608. + * @return 0 if successful, negative error code otherise.
  86609. + */
  86610. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  86611. +{
  86612. + /*
  86613. + * Currently assuming that there is a dedicated host channnel for each
  86614. + * periodic transaction plus at least one host channel for
  86615. + * non-periodic transactions.
  86616. + */
  86617. + int status;
  86618. + int num_channels;
  86619. +
  86620. + num_channels = hcd->core_if->core_params->host_channels;
  86621. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  86622. + && (hcd->periodic_channels < num_channels - 1)) {
  86623. + status = 0;
  86624. + } else {
  86625. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  86626. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  86627. + status = -DWC_E_NO_SPACE;
  86628. + }
  86629. +
  86630. + return status;
  86631. +}
  86632. +
  86633. +/**
  86634. + * Checks that there is sufficient bandwidth for the specified QH in the
  86635. + * periodic schedule. For simplicity, this calculation assumes that all the
  86636. + * transfers in the periodic schedule may occur in the same (micro)frame.
  86637. + *
  86638. + * @param hcd The HCD state structure for the DWC OTG controller.
  86639. + * @param qh QH containing periodic bandwidth required.
  86640. + *
  86641. + * @return 0 if successful, negative error code otherwise.
  86642. + */
  86643. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  86644. +{
  86645. + int status;
  86646. + int16_t max_claimed_usecs;
  86647. +
  86648. + status = 0;
  86649. +
  86650. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  86651. + /*
  86652. + * High speed mode.
  86653. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  86654. + */
  86655. +
  86656. + max_claimed_usecs = 100 - qh->usecs;
  86657. + } else {
  86658. + /*
  86659. + * Full speed mode.
  86660. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  86661. + */
  86662. + max_claimed_usecs = 900 - qh->usecs;
  86663. + }
  86664. +
  86665. + if (hcd->periodic_usecs > max_claimed_usecs) {
  86666. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  86667. + status = -DWC_E_NO_SPACE;
  86668. + }
  86669. +
  86670. + return status;
  86671. +}
  86672. +
  86673. +/* microframe_schedule=0 end */
  86674. +
  86675. +/**
  86676. + * Microframe scheduler
  86677. + * track the total use in hcd->frame_usecs
  86678. + * keep each qh use in qh->frame_usecs
  86679. + * when surrendering the qh then donate the time back
  86680. + */
  86681. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  86682. +
  86683. +/*
  86684. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  86685. + */
  86686. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  86687. +{
  86688. + int i;
  86689. + for (i=0; i<8; i++) {
  86690. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  86691. + }
  86692. + return 0;
  86693. +}
  86694. +
  86695. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  86696. +{
  86697. + int i;
  86698. + unsigned short utime;
  86699. + int t_left;
  86700. + int ret;
  86701. + int done;
  86702. +
  86703. + ret = -1;
  86704. + utime = _qh->usecs;
  86705. + t_left = utime;
  86706. + i = 0;
  86707. + done = 0;
  86708. + while (done == 0) {
  86709. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  86710. + if (utime <= _hcd->frame_usecs[i]) {
  86711. + _hcd->frame_usecs[i] -= utime;
  86712. + _qh->frame_usecs[i] += utime;
  86713. + t_left -= utime;
  86714. + ret = i;
  86715. + done = 1;
  86716. + return ret;
  86717. + } else {
  86718. + i++;
  86719. + if (i == 8) {
  86720. + done = 1;
  86721. + ret = -1;
  86722. + }
  86723. + }
  86724. + }
  86725. + return ret;
  86726. + }
  86727. +
  86728. +/*
  86729. + * use this for FS apps that can span multiple uframes
  86730. + */
  86731. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  86732. +{
  86733. + int i;
  86734. + int j;
  86735. + unsigned short utime;
  86736. + int t_left;
  86737. + int ret;
  86738. + int done;
  86739. + unsigned short xtime;
  86740. +
  86741. + ret = -1;
  86742. + utime = _qh->usecs;
  86743. + t_left = utime;
  86744. + i = 0;
  86745. + done = 0;
  86746. +loop:
  86747. + while (done == 0) {
  86748. + if(_hcd->frame_usecs[i] <= 0) {
  86749. + i++;
  86750. + if (i == 8) {
  86751. + done = 1;
  86752. + ret = -1;
  86753. + }
  86754. + goto loop;
  86755. + }
  86756. +
  86757. + /*
  86758. + * we need n consecutive slots
  86759. + * so use j as a start slot j plus j+1 must be enough time (for now)
  86760. + */
  86761. + xtime= _hcd->frame_usecs[i];
  86762. + for (j = i+1 ; j < 8 ; j++ ) {
  86763. + /*
  86764. + * if we add this frame remaining time to xtime we may
  86765. + * be OK, if not we need to test j for a complete frame
  86766. + */
  86767. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  86768. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  86769. + j = 8;
  86770. + ret = -1;
  86771. + continue;
  86772. + }
  86773. + }
  86774. + if (xtime >= utime) {
  86775. + ret = i;
  86776. + j = 8; /* stop loop with a good value ret */
  86777. + continue;
  86778. + }
  86779. + /* add the frame time to x time */
  86780. + xtime += _hcd->frame_usecs[j];
  86781. + /* we must have a fully available next frame or break */
  86782. + if ((xtime < utime)
  86783. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  86784. + ret = -1;
  86785. + j = 8; /* stop loop with a bad value ret */
  86786. + continue;
  86787. + }
  86788. + }
  86789. + if (ret >= 0) {
  86790. + t_left = utime;
  86791. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  86792. + t_left -= _hcd->frame_usecs[j];
  86793. + if ( t_left <= 0 ) {
  86794. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  86795. + _hcd->frame_usecs[j]= -t_left;
  86796. + ret = i;
  86797. + done = 1;
  86798. + } else {
  86799. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  86800. + _hcd->frame_usecs[j] = 0;
  86801. + }
  86802. + }
  86803. + } else {
  86804. + i++;
  86805. + if (i == 8) {
  86806. + done = 1;
  86807. + ret = -1;
  86808. + }
  86809. + }
  86810. + }
  86811. + return ret;
  86812. +}
  86813. +
  86814. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  86815. +{
  86816. + int ret;
  86817. + ret = -1;
  86818. +
  86819. + if (_qh->speed == USB_SPEED_HIGH) {
  86820. + /* if this is a hs transaction we need a full frame */
  86821. + ret = find_single_uframe(_hcd, _qh);
  86822. + } else {
  86823. + /* if this is a fs transaction we may need a sequence of frames */
  86824. + ret = find_multi_uframe(_hcd, _qh);
  86825. + }
  86826. + return ret;
  86827. +}
  86828. +
  86829. +/**
  86830. + * Checks that the max transfer size allowed in a host channel is large enough
  86831. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  86832. + * transfer.
  86833. + *
  86834. + * @param hcd The HCD state structure for the DWC OTG controller.
  86835. + * @param qh QH for a periodic endpoint.
  86836. + *
  86837. + * @return 0 if successful, negative error code otherwise.
  86838. + */
  86839. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  86840. +{
  86841. + int status;
  86842. + uint32_t max_xfer_size;
  86843. + uint32_t max_channel_xfer_size;
  86844. +
  86845. + status = 0;
  86846. +
  86847. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  86848. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  86849. +
  86850. + if (max_xfer_size > max_channel_xfer_size) {
  86851. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  86852. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  86853. + status = -DWC_E_NO_SPACE;
  86854. + }
  86855. +
  86856. + return status;
  86857. +}
  86858. +
  86859. +
  86860. +
  86861. +/**
  86862. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  86863. + *
  86864. + * @param hcd The HCD state structure for the DWC OTG controller.
  86865. + * @param qh QH for the periodic transfer. The QH should already contain the
  86866. + * scheduling information.
  86867. + *
  86868. + * @return 0 if successful, negative error code otherwise.
  86869. + */
  86870. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  86871. +{
  86872. + int status = 0;
  86873. +
  86874. + if (microframe_schedule) {
  86875. + int frame;
  86876. + status = find_uframe(hcd, qh);
  86877. + frame = -1;
  86878. + if (status == 0) {
  86879. + frame = 7;
  86880. + } else {
  86881. + if (status > 0 )
  86882. + frame = status-1;
  86883. + }
  86884. +
  86885. + /* Set the new frame up */
  86886. + if (frame > -1) {
  86887. + qh->sched_frame &= ~0x7;
  86888. + qh->sched_frame |= (frame & 7);
  86889. + }
  86890. +
  86891. + if (status != -1)
  86892. + status = 0;
  86893. + } else {
  86894. + status = periodic_channel_available(hcd);
  86895. + if (status) {
  86896. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  86897. + return status;
  86898. + }
  86899. +
  86900. + status = check_periodic_bandwidth(hcd, qh);
  86901. + }
  86902. + if (status) {
  86903. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  86904. + "periodic transfer.\n", __func__);
  86905. + return status;
  86906. + }
  86907. + status = check_max_xfer_size(hcd, qh);
  86908. + if (status) {
  86909. + DWC_INFO("%s: Channel max transfer size too small "
  86910. + "for periodic transfer.\n", __func__);
  86911. + return status;
  86912. + }
  86913. +
  86914. + if (hcd->core_if->dma_desc_enable) {
  86915. + /* Don't rely on SOF and start in ready schedule */
  86916. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  86917. + }
  86918. + else {
  86919. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  86920. + {
  86921. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  86922. +
  86923. + }
  86924. + /* Always start in the inactive schedule. */
  86925. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  86926. + }
  86927. +
  86928. + if (!microframe_schedule) {
  86929. + /* Reserve the periodic channel. */
  86930. + hcd->periodic_channels++;
  86931. + }
  86932. +
  86933. + /* Update claimed usecs per (micro)frame. */
  86934. + hcd->periodic_usecs += qh->usecs;
  86935. +
  86936. + return status;
  86937. +}
  86938. +
  86939. +
  86940. +/**
  86941. + * This function adds a QH to either the non periodic or periodic schedule if
  86942. + * it is not already in the schedule. If the QH is already in the schedule, no
  86943. + * action is taken.
  86944. + *
  86945. + * @return 0 if successful, negative error code otherwise.
  86946. + */
  86947. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  86948. +{
  86949. + int status = 0;
  86950. + gintmsk_data_t intr_mask = {.d32 = 0 };
  86951. +
  86952. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  86953. + /* QH already in a schedule. */
  86954. + return status;
  86955. + }
  86956. +
  86957. + /* Add the new QH to the appropriate schedule */
  86958. + if (dwc_qh_is_non_per(qh)) {
  86959. + /* Always start in the inactive schedule. */
  86960. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  86961. + &qh->qh_list_entry);
  86962. + //hcd->fiq_state->kick_np_queues = 1;
  86963. + } else {
  86964. + status = schedule_periodic(hcd, qh);
  86965. + if ( !hcd->periodic_qh_count ) {
  86966. + intr_mask.b.sofintr = 1;
  86967. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  86968. + intr_mask.d32, intr_mask.d32);
  86969. + }
  86970. + hcd->periodic_qh_count++;
  86971. + }
  86972. +
  86973. + return status;
  86974. +}
  86975. +
  86976. +/**
  86977. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  86978. + *
  86979. + * @param hcd The HCD state structure for the DWC OTG controller.
  86980. + * @param qh QH for the periodic transfer.
  86981. + */
  86982. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  86983. +{
  86984. + int i;
  86985. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  86986. +
  86987. + /* Update claimed usecs per (micro)frame. */
  86988. + hcd->periodic_usecs -= qh->usecs;
  86989. +
  86990. + if (!microframe_schedule) {
  86991. + /* Release the periodic channel reservation. */
  86992. + hcd->periodic_channels--;
  86993. + } else {
  86994. + for (i = 0; i < 8; i++) {
  86995. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  86996. + qh->frame_usecs[i] = 0;
  86997. + }
  86998. + }
  86999. +}
  87000. +
  87001. +/**
  87002. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  87003. + * not freed.
  87004. + *
  87005. + * @param hcd The HCD state structure.
  87006. + * @param qh QH to remove from schedule. */
  87007. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87008. +{
  87009. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87010. +
  87011. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  87012. + /* QH is not in a schedule. */
  87013. + return;
  87014. + }
  87015. +
  87016. + if (dwc_qh_is_non_per(qh)) {
  87017. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  87018. + hcd->non_periodic_qh_ptr =
  87019. + hcd->non_periodic_qh_ptr->next;
  87020. + }
  87021. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  87022. + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
  87023. + // hcd->fiq_state->kick_np_queues = 1;
  87024. + } else {
  87025. + deschedule_periodic(hcd, qh);
  87026. + hcd->periodic_qh_count--;
  87027. + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
  87028. + intr_mask.b.sofintr = 1;
  87029. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  87030. + intr_mask.d32, 0);
  87031. + }
  87032. + }
  87033. +}
  87034. +
  87035. +/**
  87036. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  87037. + * non-periodic schedule. The QH is added to the inactive non-periodic
  87038. + * schedule if any QTDs are still attached to the QH.
  87039. + *
  87040. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  87041. + * there are any QTDs still attached to the QH, the QH is added to either the
  87042. + * periodic inactive schedule or the periodic ready schedule and its next
  87043. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  87044. + * the scheduled frame has been reached already. Otherwise it's placed in the
  87045. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  87046. + * completely removed from the periodic schedule.
  87047. + */
  87048. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  87049. + int sched_next_periodic_split)
  87050. +{
  87051. + if (dwc_qh_is_non_per(qh)) {
  87052. + dwc_otg_hcd_qh_remove(hcd, qh);
  87053. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  87054. + /* Add back to inactive non-periodic schedule. */
  87055. + dwc_otg_hcd_qh_add(hcd, qh);
  87056. + //hcd->fiq_state->kick_np_queues = 1;
  87057. + }
  87058. + } else {
  87059. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  87060. +
  87061. + if (qh->do_split) {
  87062. + /* Schedule the next continuing periodic split transfer */
  87063. + if (sched_next_periodic_split) {
  87064. +
  87065. + qh->sched_frame = frame_number;
  87066. +
  87067. + if (dwc_frame_num_le(frame_number,
  87068. + dwc_frame_num_inc
  87069. + (qh->start_split_frame,
  87070. + 1))) {
  87071. + /*
  87072. + * Allow one frame to elapse after start
  87073. + * split microframe before scheduling
  87074. + * complete split, but DONT if we are
  87075. + * doing the next start split in the
  87076. + * same frame for an ISOC out.
  87077. + */
  87078. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  87079. + (qh->ep_is_in != 0)) {
  87080. + qh->sched_frame =
  87081. + dwc_frame_num_inc(qh->sched_frame, 1);
  87082. + }
  87083. + }
  87084. + } else {
  87085. + qh->sched_frame =
  87086. + dwc_frame_num_inc(qh->start_split_frame,
  87087. + qh->interval);
  87088. + if (dwc_frame_num_le
  87089. + (qh->sched_frame, frame_number)) {
  87090. + qh->sched_frame = frame_number;
  87091. + }
  87092. + qh->sched_frame |= 0x7;
  87093. + qh->start_split_frame = qh->sched_frame;
  87094. + }
  87095. + } else {
  87096. + qh->sched_frame =
  87097. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  87098. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  87099. + qh->sched_frame = frame_number;
  87100. + }
  87101. + }
  87102. +
  87103. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  87104. + dwc_otg_hcd_qh_remove(hcd, qh);
  87105. + } else {
  87106. + /*
  87107. + * Remove from periodic_sched_queued and move to
  87108. + * appropriate queue.
  87109. + */
  87110. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  87111. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  87112. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  87113. + &qh->qh_list_entry);
  87114. + } else {
  87115. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  87116. + {
  87117. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  87118. + }
  87119. +
  87120. + DWC_LIST_MOVE_HEAD
  87121. + (&hcd->periodic_sched_inactive,
  87122. + &qh->qh_list_entry);
  87123. + }
  87124. + }
  87125. + }
  87126. +}
  87127. +
  87128. +/**
  87129. + * This function allocates and initializes a QTD.
  87130. + *
  87131. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  87132. + * pointing to each other so each pair should have a unique correlation.
  87133. + * @param atomic_alloc Flag to do atomic alloc if needed
  87134. + *
  87135. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  87136. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  87137. +{
  87138. + dwc_otg_qtd_t *qtd;
  87139. +
  87140. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  87141. + if (qtd == NULL) {
  87142. + return NULL;
  87143. + }
  87144. +
  87145. + dwc_otg_hcd_qtd_init(qtd, urb);
  87146. + return qtd;
  87147. +}
  87148. +
  87149. +/**
  87150. + * Initializes a QTD structure.
  87151. + *
  87152. + * @param qtd The QTD to initialize.
  87153. + * @param urb The URB to use for initialization. */
  87154. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  87155. +{
  87156. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  87157. + qtd->urb = urb;
  87158. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  87159. + /*
  87160. + * The only time the QTD data toggle is used is on the data
  87161. + * phase of control transfers. This phase always starts with
  87162. + * DATA1.
  87163. + */
  87164. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  87165. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  87166. + }
  87167. +
  87168. + /* start split */
  87169. + qtd->complete_split = 0;
  87170. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  87171. + qtd->isoc_split_offset = 0;
  87172. + qtd->in_process = 0;
  87173. +
  87174. + /* Store the qtd ptr in the urb to reference what QTD. */
  87175. + urb->qtd = qtd;
  87176. + return;
  87177. +}
  87178. +
  87179. +/**
  87180. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  87181. + * QH to place the QTD into. If it does not find a QH, then it will create a
  87182. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  87183. + * is placed into the proper schedule based on its EP type.
  87184. + * HCD lock must be held and interrupts must be disabled on entry
  87185. + *
  87186. + * @param[in] qtd The QTD to add
  87187. + * @param[in] hcd The DWC HCD structure
  87188. + * @param[out] qh out parameter to return queue head
  87189. + * @param atomic_alloc Flag to do atomic alloc if needed
  87190. + *
  87191. + * @return 0 if successful, negative error code otherwise.
  87192. + */
  87193. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  87194. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  87195. +{
  87196. + int retval = 0;
  87197. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  87198. +
  87199. + /*
  87200. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  87201. + * doesn't exist.
  87202. + */
  87203. + if (*qh == NULL) {
  87204. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  87205. + if (*qh == NULL) {
  87206. + retval = -DWC_E_NO_MEMORY;
  87207. + goto done;
  87208. + } else {
  87209. + if (fiq_enable)
  87210. + hcd->fiq_state->kick_np_queues = 1;
  87211. + }
  87212. + }
  87213. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  87214. + if (retval == 0) {
  87215. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  87216. + qtd_list_entry);
  87217. + qtd->qh = *qh;
  87218. + }
  87219. +done:
  87220. +
  87221. + return retval;
  87222. +}
  87223. +
  87224. +#endif /* DWC_DEVICE_ONLY */
  87225. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  87226. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 1969-12-31 18:00:00.000000000 -0600
  87227. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 2014-12-11 14:02:55.400418001 -0600
  87228. @@ -0,0 +1,188 @@
  87229. +#ifndef _DWC_OS_DEP_H_
  87230. +#define _DWC_OS_DEP_H_
  87231. +
  87232. +/**
  87233. + * @file
  87234. + *
  87235. + * This file contains OS dependent structures.
  87236. + *
  87237. + */
  87238. +
  87239. +#include <linux/kernel.h>
  87240. +#include <linux/module.h>
  87241. +#include <linux/moduleparam.h>
  87242. +#include <linux/init.h>
  87243. +#include <linux/device.h>
  87244. +#include <linux/errno.h>
  87245. +#include <linux/types.h>
  87246. +#include <linux/slab.h>
  87247. +#include <linux/list.h>
  87248. +#include <linux/interrupt.h>
  87249. +#include <linux/ctype.h>
  87250. +#include <linux/string.h>
  87251. +#include <linux/dma-mapping.h>
  87252. +#include <linux/jiffies.h>
  87253. +#include <linux/delay.h>
  87254. +#include <linux/timer.h>
  87255. +#include <linux/workqueue.h>
  87256. +#include <linux/stat.h>
  87257. +#include <linux/pci.h>
  87258. +
  87259. +#include <linux/version.h>
  87260. +
  87261. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  87262. +# include <linux/irq.h>
  87263. +#endif
  87264. +
  87265. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  87266. +# include <linux/usb/ch9.h>
  87267. +#else
  87268. +# include <linux/usb_ch9.h>
  87269. +#endif
  87270. +
  87271. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  87272. +# include <linux/usb/gadget.h>
  87273. +#else
  87274. +# include <linux/usb_gadget.h>
  87275. +#endif
  87276. +
  87277. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  87278. +# include <asm/irq.h>
  87279. +#endif
  87280. +
  87281. +#ifdef PCI_INTERFACE
  87282. +# include <asm/io.h>
  87283. +#endif
  87284. +
  87285. +#ifdef LM_INTERFACE
  87286. +# include <asm/unaligned.h>
  87287. +# include <asm/sizes.h>
  87288. +# include <asm/param.h>
  87289. +# include <asm/io.h>
  87290. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  87291. +# include <asm/arch/hardware.h>
  87292. +# include <asm/arch/lm.h>
  87293. +# include <asm/arch/irqs.h>
  87294. +# include <asm/arch/regs-irq.h>
  87295. +# else
  87296. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  87297. + here we assume that the machine architecture provides definitions
  87298. + in its own header
  87299. +*/
  87300. +# include <mach/lm.h>
  87301. +# include <mach/hardware.h>
  87302. +# endif
  87303. +#endif
  87304. +
  87305. +#ifdef PLATFORM_INTERFACE
  87306. +#include <linux/platform_device.h>
  87307. +#include <asm/mach/map.h>
  87308. +#endif
  87309. +
  87310. +/** The OS page size */
  87311. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  87312. +
  87313. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  87314. +typedef int gfp_t;
  87315. +#endif
  87316. +
  87317. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  87318. +# define IRQF_SHARED SA_SHIRQ
  87319. +#endif
  87320. +
  87321. +typedef struct os_dependent {
  87322. + /** Base address returned from ioremap() */
  87323. + void *base;
  87324. +
  87325. + /** Register offset for Diagnostic API */
  87326. + uint32_t reg_offset;
  87327. +
  87328. + /** Base address for MPHI peripheral */
  87329. + void *mphi_base;
  87330. +
  87331. +#ifdef LM_INTERFACE
  87332. + struct lm_device *lmdev;
  87333. +#elif defined(PCI_INTERFACE)
  87334. + struct pci_dev *pcidev;
  87335. +
  87336. + /** Start address of a PCI region */
  87337. + resource_size_t rsrc_start;
  87338. +
  87339. + /** Length address of a PCI region */
  87340. + resource_size_t rsrc_len;
  87341. +#elif defined(PLATFORM_INTERFACE)
  87342. + struct platform_device *platformdev;
  87343. +#endif
  87344. +
  87345. +} os_dependent_t;
  87346. +
  87347. +#ifdef __cplusplus
  87348. +}
  87349. +#endif
  87350. +
  87351. +
  87352. +
  87353. +/* Type for the our device on the chosen bus */
  87354. +#if defined(LM_INTERFACE)
  87355. +typedef struct lm_device dwc_bus_dev_t;
  87356. +#elif defined(PCI_INTERFACE)
  87357. +typedef struct pci_dev dwc_bus_dev_t;
  87358. +#elif defined(PLATFORM_INTERFACE)
  87359. +typedef struct platform_device dwc_bus_dev_t;
  87360. +#endif
  87361. +
  87362. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  87363. +#if defined(LM_INTERFACE)
  87364. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  87365. +#elif defined(PCI_INTERFACE)
  87366. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  87367. +#elif defined(PLATFORM_INTERFACE)
  87368. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  87369. +#endif
  87370. +
  87371. +/**
  87372. + * Helper macro returning the otg_device structure of a given struct device
  87373. + *
  87374. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  87375. + */
  87376. +#ifdef LM_INTERFACE
  87377. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  87378. + struct lm_device *lm_dev = \
  87379. + container_of(_dev, struct lm_device, dev); \
  87380. + _var = lm_get_drvdata(lm_dev); \
  87381. + } while (0)
  87382. +
  87383. +#elif defined(PCI_INTERFACE)
  87384. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  87385. + _var = dev_get_drvdata(_dev); \
  87386. + } while (0)
  87387. +
  87388. +#elif defined(PLATFORM_INTERFACE)
  87389. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  87390. + struct platform_device *platform_dev = \
  87391. + container_of(_dev, struct platform_device, dev); \
  87392. + _var = platform_get_drvdata(platform_dev); \
  87393. + } while (0)
  87394. +#endif
  87395. +
  87396. +
  87397. +/**
  87398. + * Helper macro returning the struct dev of the given struct os_dependent
  87399. + *
  87400. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  87401. + */
  87402. +#ifdef LM_INTERFACE
  87403. +#define DWC_OTG_OS_GETDEV(_osdep) \
  87404. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  87405. +#elif defined(PCI_INTERFACE)
  87406. +#define DWC_OTG_OS_GETDEV(_osdep) \
  87407. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  87408. +#elif defined(PLATFORM_INTERFACE)
  87409. +#define DWC_OTG_OS_GETDEV(_osdep) \
  87410. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  87411. +#endif
  87412. +
  87413. +
  87414. +
  87415. +
  87416. +#endif /* _DWC_OS_DEP_H_ */
  87417. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  87418. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 1969-12-31 18:00:00.000000000 -0600
  87419. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 2014-12-11 14:05:39.556418001 -0600
  87420. @@ -0,0 +1,2712 @@
  87421. +/* ==========================================================================
  87422. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  87423. + * $Revision: #101 $
  87424. + * $Date: 2012/08/10 $
  87425. + * $Change: 2047372 $
  87426. + *
  87427. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  87428. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  87429. + * otherwise expressly agreed to in writing between Synopsys and you.
  87430. + *
  87431. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  87432. + * any End User Software License Agreement or Agreement for Licensed Product
  87433. + * with Synopsys or any supplement thereto. You are permitted to use and
  87434. + * redistribute this Software in source and binary forms, with or without
  87435. + * modification, provided that redistributions of source code must retain this
  87436. + * notice. You may not view, use, disclose, copy or distribute this file or
  87437. + * any information contained herein except pursuant to this license grant from
  87438. + * Synopsys. If you do not agree with this notice, including the disclaimer
  87439. + * below, then you are not authorized to use the Software.
  87440. + *
  87441. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  87442. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  87443. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  87444. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  87445. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  87446. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87447. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87448. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  87449. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  87450. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  87451. + * DAMAGE.
  87452. + * ========================================================================== */
  87453. +#ifndef DWC_HOST_ONLY
  87454. +
  87455. +/** @file
  87456. + * This file implements PCD Core. All code in this file is portable and doesn't
  87457. + * use any OS specific functions.
  87458. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  87459. + * header file, which can be used to implement OS specific PCD interface.
  87460. + *
  87461. + * An important function of the PCD is managing interrupts generated
  87462. + * by the DWC_otg controller. The implementation of the DWC_otg device
  87463. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  87464. + *
  87465. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  87466. + * @todo Does it work when the request size is greater than DEPTSIZ
  87467. + * transfer size
  87468. + *
  87469. + */
  87470. +
  87471. +#include "dwc_otg_pcd.h"
  87472. +
  87473. +#ifdef DWC_UTE_CFI
  87474. +#include "dwc_otg_cfi.h"
  87475. +
  87476. +extern int init_cfi(cfiobject_t * cfiobj);
  87477. +#endif
  87478. +
  87479. +/**
  87480. + * Choose endpoint from ep arrays using usb_ep structure.
  87481. + */
  87482. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  87483. +{
  87484. + int i;
  87485. + if (pcd->ep0.priv == handle) {
  87486. + return &pcd->ep0;
  87487. + }
  87488. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  87489. + if (pcd->in_ep[i].priv == handle)
  87490. + return &pcd->in_ep[i];
  87491. + if (pcd->out_ep[i].priv == handle)
  87492. + return &pcd->out_ep[i];
  87493. + }
  87494. +
  87495. + return NULL;
  87496. +}
  87497. +
  87498. +/**
  87499. + * This function completes a request. It call's the request call back.
  87500. + */
  87501. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  87502. + int32_t status)
  87503. +{
  87504. + unsigned stopped = ep->stopped;
  87505. +
  87506. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  87507. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  87508. +
  87509. + /* don't modify queue heads during completion callback */
  87510. + ep->stopped = 1;
  87511. + /* spin_unlock/spin_lock now done in fops->complete() */
  87512. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  87513. + req->actual);
  87514. +
  87515. + if (ep->pcd->request_pending > 0) {
  87516. + --ep->pcd->request_pending;
  87517. + }
  87518. +
  87519. + ep->stopped = stopped;
  87520. + DWC_FREE(req);
  87521. +}
  87522. +
  87523. +/**
  87524. + * This function terminates all the requsts in the EP request queue.
  87525. + */
  87526. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  87527. +{
  87528. + dwc_otg_pcd_request_t *req;
  87529. +
  87530. + ep->stopped = 1;
  87531. +
  87532. + /* called with irqs blocked?? */
  87533. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  87534. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  87535. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  87536. + }
  87537. +}
  87538. +
  87539. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  87540. + const struct dwc_otg_pcd_function_ops *fops)
  87541. +{
  87542. + pcd->fops = fops;
  87543. +}
  87544. +
  87545. +/**
  87546. + * PCD Callback function for initializing the PCD when switching to
  87547. + * device mode.
  87548. + *
  87549. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  87550. + */
  87551. +static int32_t dwc_otg_pcd_start_cb(void *p)
  87552. +{
  87553. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  87554. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87555. +
  87556. + /*
  87557. + * Initialized the Core for Device mode.
  87558. + */
  87559. + if (dwc_otg_is_device_mode(core_if)) {
  87560. + dwc_otg_core_dev_init(core_if);
  87561. + /* Set core_if's lock pointer to the pcd->lock */
  87562. + core_if->lock = pcd->lock;
  87563. + }
  87564. + return 1;
  87565. +}
  87566. +
  87567. +/** CFI-specific buffer allocation function for EP */
  87568. +#ifdef DWC_UTE_CFI
  87569. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  87570. + size_t buflen, int flags)
  87571. +{
  87572. + dwc_otg_pcd_ep_t *ep;
  87573. + ep = get_ep_from_handle(pcd, pep);
  87574. + if (!ep) {
  87575. + DWC_WARN("bad ep\n");
  87576. + return -DWC_E_INVALID;
  87577. + }
  87578. +
  87579. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  87580. + flags);
  87581. +}
  87582. +#else
  87583. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  87584. + size_t buflen, int flags);
  87585. +#endif
  87586. +
  87587. +/**
  87588. + * PCD Callback function for notifying the PCD when resuming from
  87589. + * suspend.
  87590. + *
  87591. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  87592. + */
  87593. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  87594. +{
  87595. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  87596. +
  87597. + if (pcd->fops->resume) {
  87598. + pcd->fops->resume(pcd);
  87599. + }
  87600. +
  87601. + /* Stop the SRP timeout timer. */
  87602. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  87603. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  87604. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  87605. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  87606. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  87607. + }
  87608. + }
  87609. + return 1;
  87610. +}
  87611. +
  87612. +/**
  87613. + * PCD Callback function for notifying the PCD device is suspended.
  87614. + *
  87615. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  87616. + */
  87617. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  87618. +{
  87619. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  87620. +
  87621. + if (pcd->fops->suspend) {
  87622. + DWC_SPINUNLOCK(pcd->lock);
  87623. + pcd->fops->suspend(pcd);
  87624. + DWC_SPINLOCK(pcd->lock);
  87625. + }
  87626. +
  87627. + return 1;
  87628. +}
  87629. +
  87630. +/**
  87631. + * PCD Callback function for stopping the PCD when switching to Host
  87632. + * mode.
  87633. + *
  87634. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  87635. + */
  87636. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  87637. +{
  87638. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  87639. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  87640. +
  87641. + dwc_otg_pcd_stop(pcd);
  87642. + return 1;
  87643. +}
  87644. +
  87645. +/**
  87646. + * PCD Callback structure for handling mode switching.
  87647. + */
  87648. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  87649. + .start = dwc_otg_pcd_start_cb,
  87650. + .stop = dwc_otg_pcd_stop_cb,
  87651. + .suspend = dwc_otg_pcd_suspend_cb,
  87652. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  87653. + .p = 0, /* Set at registration */
  87654. +};
  87655. +
  87656. +/**
  87657. + * This function allocates a DMA Descriptor chain for the Endpoint
  87658. + * buffer to be used for a transfer to/from the specified endpoint.
  87659. + */
  87660. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  87661. + uint32_t count)
  87662. +{
  87663. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  87664. + dma_desc_addr);
  87665. +}
  87666. +
  87667. +/**
  87668. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  87669. + */
  87670. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  87671. + uint32_t dma_desc_addr, uint32_t count)
  87672. +{
  87673. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  87674. + dma_desc_addr);
  87675. +}
  87676. +
  87677. +#ifdef DWC_EN_ISOC
  87678. +
  87679. +/**
  87680. + * This function initializes a descriptor chain for Isochronous transfer
  87681. + *
  87682. + * @param core_if Programming view of DWC_otg controller.
  87683. + * @param dwc_ep The EP to start the transfer on.
  87684. + *
  87685. + */
  87686. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  87687. + dwc_ep_t * dwc_ep)
  87688. +{
  87689. +
  87690. + dsts_data_t dsts = {.d32 = 0 };
  87691. + depctl_data_t depctl = {.d32 = 0 };
  87692. + volatile uint32_t *addr;
  87693. + int i, j;
  87694. + uint32_t len;
  87695. +
  87696. + if (dwc_ep->is_in)
  87697. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  87698. + else
  87699. + dwc_ep->desc_cnt =
  87700. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  87701. + dwc_ep->bInterval;
  87702. +
  87703. + /** Allocate descriptors for double buffering */
  87704. + dwc_ep->iso_desc_addr =
  87705. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  87706. + dwc_ep->desc_cnt * 2);
  87707. + if (dwc_ep->desc_addr) {
  87708. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  87709. + return;
  87710. + }
  87711. +
  87712. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  87713. +
  87714. + /** ISO OUT EP */
  87715. + if (dwc_ep->is_in == 0) {
  87716. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  87717. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  87718. + dma_addr_t dma_ad;
  87719. + uint32_t data_per_desc;
  87720. + dwc_otg_dev_out_ep_regs_t *out_regs =
  87721. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  87722. + int offset;
  87723. +
  87724. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  87725. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  87726. +
  87727. + /** Buffer 0 descriptors setup */
  87728. + dma_ad = dwc_ep->dma_addr0;
  87729. +
  87730. + sts.b_iso_out.bs = BS_HOST_READY;
  87731. + sts.b_iso_out.rxsts = 0;
  87732. + sts.b_iso_out.l = 0;
  87733. + sts.b_iso_out.sp = 0;
  87734. + sts.b_iso_out.ioc = 0;
  87735. + sts.b_iso_out.pid = 0;
  87736. + sts.b_iso_out.framenum = 0;
  87737. +
  87738. + offset = 0;
  87739. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  87740. + i += dwc_ep->pkt_per_frm) {
  87741. +
  87742. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  87743. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  87744. + if (len > dwc_ep->data_per_frame)
  87745. + data_per_desc =
  87746. + dwc_ep->data_per_frame -
  87747. + j * dwc_ep->maxpacket;
  87748. + else
  87749. + data_per_desc = dwc_ep->maxpacket;
  87750. + len = data_per_desc % 4;
  87751. + if (len)
  87752. + data_per_desc += 4 - len;
  87753. +
  87754. + sts.b_iso_out.rxbytes = data_per_desc;
  87755. + dma_desc->buf = dma_ad;
  87756. + dma_desc->status.d32 = sts.d32;
  87757. +
  87758. + offset += data_per_desc;
  87759. + dma_desc++;
  87760. + dma_ad += data_per_desc;
  87761. + }
  87762. + }
  87763. +
  87764. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  87765. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  87766. + if (len > dwc_ep->data_per_frame)
  87767. + data_per_desc =
  87768. + dwc_ep->data_per_frame -
  87769. + j * dwc_ep->maxpacket;
  87770. + else
  87771. + data_per_desc = dwc_ep->maxpacket;
  87772. + len = data_per_desc % 4;
  87773. + if (len)
  87774. + data_per_desc += 4 - len;
  87775. + sts.b_iso_out.rxbytes = data_per_desc;
  87776. + dma_desc->buf = dma_ad;
  87777. + dma_desc->status.d32 = sts.d32;
  87778. +
  87779. + offset += data_per_desc;
  87780. + dma_desc++;
  87781. + dma_ad += data_per_desc;
  87782. + }
  87783. +
  87784. + sts.b_iso_out.ioc = 1;
  87785. + len = (j + 1) * dwc_ep->maxpacket;
  87786. + if (len > dwc_ep->data_per_frame)
  87787. + data_per_desc =
  87788. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  87789. + else
  87790. + data_per_desc = dwc_ep->maxpacket;
  87791. + len = data_per_desc % 4;
  87792. + if (len)
  87793. + data_per_desc += 4 - len;
  87794. + sts.b_iso_out.rxbytes = data_per_desc;
  87795. +
  87796. + dma_desc->buf = dma_ad;
  87797. + dma_desc->status.d32 = sts.d32;
  87798. + dma_desc++;
  87799. +
  87800. + /** Buffer 1 descriptors setup */
  87801. + sts.b_iso_out.ioc = 0;
  87802. + dma_ad = dwc_ep->dma_addr1;
  87803. +
  87804. + offset = 0;
  87805. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  87806. + i += dwc_ep->pkt_per_frm) {
  87807. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  87808. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  87809. + if (len > dwc_ep->data_per_frame)
  87810. + data_per_desc =
  87811. + dwc_ep->data_per_frame -
  87812. + j * dwc_ep->maxpacket;
  87813. + else
  87814. + data_per_desc = dwc_ep->maxpacket;
  87815. + len = data_per_desc % 4;
  87816. + if (len)
  87817. + data_per_desc += 4 - len;
  87818. +
  87819. + data_per_desc =
  87820. + sts.b_iso_out.rxbytes = data_per_desc;
  87821. + dma_desc->buf = dma_ad;
  87822. + dma_desc->status.d32 = sts.d32;
  87823. +
  87824. + offset += data_per_desc;
  87825. + dma_desc++;
  87826. + dma_ad += data_per_desc;
  87827. + }
  87828. + }
  87829. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  87830. + data_per_desc =
  87831. + ((j + 1) * dwc_ep->maxpacket >
  87832. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  87833. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  87834. + data_per_desc +=
  87835. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  87836. + sts.b_iso_out.rxbytes = data_per_desc;
  87837. + dma_desc->buf = dma_ad;
  87838. + dma_desc->status.d32 = sts.d32;
  87839. +
  87840. + offset += data_per_desc;
  87841. + dma_desc++;
  87842. + dma_ad += data_per_desc;
  87843. + }
  87844. +
  87845. + sts.b_iso_out.ioc = 1;
  87846. + sts.b_iso_out.l = 1;
  87847. + data_per_desc =
  87848. + ((j + 1) * dwc_ep->maxpacket >
  87849. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  87850. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  87851. + data_per_desc +=
  87852. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  87853. + sts.b_iso_out.rxbytes = data_per_desc;
  87854. +
  87855. + dma_desc->buf = dma_ad;
  87856. + dma_desc->status.d32 = sts.d32;
  87857. +
  87858. + dwc_ep->next_frame = 0;
  87859. +
  87860. + /** Write dma_ad into DOEPDMA register */
  87861. + DWC_WRITE_REG32(&(out_regs->doepdma),
  87862. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  87863. +
  87864. + }
  87865. + /** ISO IN EP */
  87866. + else {
  87867. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  87868. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  87869. + dma_addr_t dma_ad;
  87870. + dwc_otg_dev_in_ep_regs_t *in_regs =
  87871. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  87872. + unsigned int frmnumber;
  87873. + fifosize_data_t txfifosize, rxfifosize;
  87874. +
  87875. + txfifosize.d32 =
  87876. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  87877. + dtxfsts);
  87878. + rxfifosize.d32 =
  87879. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  87880. +
  87881. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  87882. +
  87883. + dma_ad = dwc_ep->dma_addr0;
  87884. +
  87885. + dsts.d32 =
  87886. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  87887. +
  87888. + sts.b_iso_in.bs = BS_HOST_READY;
  87889. + sts.b_iso_in.txsts = 0;
  87890. + sts.b_iso_in.sp =
  87891. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  87892. + sts.b_iso_in.ioc = 0;
  87893. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  87894. +
  87895. + frmnumber = dwc_ep->next_frame;
  87896. +
  87897. + sts.b_iso_in.framenum = frmnumber;
  87898. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  87899. + sts.b_iso_in.l = 0;
  87900. +
  87901. + /** Buffer 0 descriptors setup */
  87902. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  87903. + dma_desc->buf = dma_ad;
  87904. + dma_desc->status.d32 = sts.d32;
  87905. + dma_desc++;
  87906. +
  87907. + dma_ad += dwc_ep->data_per_frame;
  87908. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  87909. + }
  87910. +
  87911. + sts.b_iso_in.ioc = 1;
  87912. + dma_desc->buf = dma_ad;
  87913. + dma_desc->status.d32 = sts.d32;
  87914. + ++dma_desc;
  87915. +
  87916. + /** Buffer 1 descriptors setup */
  87917. + sts.b_iso_in.ioc = 0;
  87918. + dma_ad = dwc_ep->dma_addr1;
  87919. +
  87920. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  87921. + i += dwc_ep->pkt_per_frm) {
  87922. + dma_desc->buf = dma_ad;
  87923. + dma_desc->status.d32 = sts.d32;
  87924. + dma_desc++;
  87925. +
  87926. + dma_ad += dwc_ep->data_per_frame;
  87927. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  87928. +
  87929. + sts.b_iso_in.ioc = 0;
  87930. + }
  87931. + sts.b_iso_in.ioc = 1;
  87932. + sts.b_iso_in.l = 1;
  87933. +
  87934. + dma_desc->buf = dma_ad;
  87935. + dma_desc->status.d32 = sts.d32;
  87936. +
  87937. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  87938. +
  87939. + /** Write dma_ad into diepdma register */
  87940. + DWC_WRITE_REG32(&(in_regs->diepdma),
  87941. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  87942. + }
  87943. + /** Enable endpoint, clear nak */
  87944. + depctl.d32 = 0;
  87945. + depctl.b.epena = 1;
  87946. + depctl.b.usbactep = 1;
  87947. + depctl.b.cnak = 1;
  87948. +
  87949. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  87950. + depctl.d32 = DWC_READ_REG32(addr);
  87951. +}
  87952. +
  87953. +/**
  87954. + * This function initializes a descriptor chain for Isochronous transfer
  87955. + *
  87956. + * @param core_if Programming view of DWC_otg controller.
  87957. + * @param ep The EP to start the transfer on.
  87958. + *
  87959. + */
  87960. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  87961. + dwc_ep_t * ep)
  87962. +{
  87963. + depctl_data_t depctl = {.d32 = 0 };
  87964. + volatile uint32_t *addr;
  87965. +
  87966. + if (ep->is_in) {
  87967. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  87968. + } else {
  87969. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  87970. + }
  87971. +
  87972. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  87973. + return;
  87974. + } else {
  87975. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87976. +
  87977. + ep->xfer_len =
  87978. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  87979. + ep->pkt_cnt =
  87980. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  87981. + ep->xfer_count = 0;
  87982. + ep->xfer_buff =
  87983. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  87984. + ep->dma_addr =
  87985. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  87986. +
  87987. + if (ep->is_in) {
  87988. + /* Program the transfer size and packet count
  87989. + * as follows: xfersize = N * maxpacket +
  87990. + * short_packet pktcnt = N + (short_packet
  87991. + * exist ? 1 : 0)
  87992. + */
  87993. + deptsiz.b.mc = ep->pkt_per_frm;
  87994. + deptsiz.b.xfersize = ep->xfer_len;
  87995. + deptsiz.b.pktcnt =
  87996. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  87997. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  87998. + dieptsiz, deptsiz.d32);
  87999. +
  88000. + /* Write the DMA register */
  88001. + DWC_WRITE_REG32(&
  88002. + (core_if->dev_if->in_ep_regs[ep->num]->
  88003. + diepdma), (uint32_t) ep->dma_addr);
  88004. +
  88005. + } else {
  88006. + deptsiz.b.pktcnt =
  88007. + (ep->xfer_len + (ep->maxpacket - 1)) /
  88008. + ep->maxpacket;
  88009. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  88010. +
  88011. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  88012. + doeptsiz, deptsiz.d32);
  88013. +
  88014. + /* Write the DMA register */
  88015. + DWC_WRITE_REG32(&
  88016. + (core_if->dev_if->out_ep_regs[ep->num]->
  88017. + doepdma), (uint32_t) ep->dma_addr);
  88018. +
  88019. + }
  88020. + /** Enable endpoint, clear nak */
  88021. + depctl.d32 = 0;
  88022. + depctl.b.epena = 1;
  88023. + depctl.b.cnak = 1;
  88024. +
  88025. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  88026. + }
  88027. +}
  88028. +
  88029. +/**
  88030. + * This function does the setup for a data transfer for an EP and
  88031. + * starts the transfer. For an IN transfer, the packets will be
  88032. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  88033. + * the packets are unloaded from the Rx FIFO in the ISR.
  88034. + *
  88035. + * @param core_if Programming view of DWC_otg controller.
  88036. + * @param ep The EP to start the transfer on.
  88037. + */
  88038. +
  88039. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  88040. + dwc_ep_t * ep)
  88041. +{
  88042. + if (core_if->dma_enable) {
  88043. + if (core_if->dma_desc_enable) {
  88044. + if (ep->is_in) {
  88045. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  88046. + } else {
  88047. + ep->desc_cnt = ep->pkt_cnt;
  88048. + }
  88049. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  88050. + } else {
  88051. + if (core_if->pti_enh_enable) {
  88052. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  88053. + } else {
  88054. + ep->cur_pkt_addr =
  88055. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  88056. + xfer_buff0;
  88057. + ep->cur_pkt_dma_addr =
  88058. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  88059. + dma_addr0;
  88060. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  88061. + }
  88062. + }
  88063. + } else {
  88064. + ep->cur_pkt_addr =
  88065. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  88066. + ep->cur_pkt_dma_addr =
  88067. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  88068. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  88069. + }
  88070. +}
  88071. +
  88072. +/**
  88073. + * This function stops transfer for an EP and
  88074. + * resets the ep's variables.
  88075. + *
  88076. + * @param core_if Programming view of DWC_otg controller.
  88077. + * @param ep The EP to start the transfer on.
  88078. + */
  88079. +
  88080. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  88081. +{
  88082. + depctl_data_t depctl = {.d32 = 0 };
  88083. + volatile uint32_t *addr;
  88084. +
  88085. + if (ep->is_in == 1) {
  88086. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  88087. + } else {
  88088. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  88089. + }
  88090. +
  88091. + /* disable the ep */
  88092. + depctl.d32 = DWC_READ_REG32(addr);
  88093. +
  88094. + depctl.b.epdis = 1;
  88095. + depctl.b.snak = 1;
  88096. +
  88097. + DWC_WRITE_REG32(addr, depctl.d32);
  88098. +
  88099. + if (core_if->dma_desc_enable &&
  88100. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  88101. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  88102. + ep->iso_dma_desc_addr,
  88103. + ep->desc_cnt * 2);
  88104. + }
  88105. +
  88106. + /* reset varibales */
  88107. + ep->dma_addr0 = 0;
  88108. + ep->dma_addr1 = 0;
  88109. + ep->xfer_buff0 = 0;
  88110. + ep->xfer_buff1 = 0;
  88111. + ep->data_per_frame = 0;
  88112. + ep->data_pattern_frame = 0;
  88113. + ep->sync_frame = 0;
  88114. + ep->buf_proc_intrvl = 0;
  88115. + ep->bInterval = 0;
  88116. + ep->proc_buf_num = 0;
  88117. + ep->pkt_per_frm = 0;
  88118. + ep->pkt_per_frm = 0;
  88119. + ep->desc_cnt = 0;
  88120. + ep->iso_desc_addr = 0;
  88121. + ep->iso_dma_desc_addr = 0;
  88122. +}
  88123. +
  88124. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  88125. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  88126. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  88127. + int data_per_frame, int start_frame,
  88128. + int buf_proc_intrvl, void *req_handle,
  88129. + int atomic_alloc)
  88130. +{
  88131. + dwc_otg_pcd_ep_t *ep;
  88132. + dwc_irqflags_t flags = 0;
  88133. + dwc_ep_t *dwc_ep;
  88134. + int32_t frm_data;
  88135. + dsts_data_t dsts;
  88136. + dwc_otg_core_if_t *core_if;
  88137. +
  88138. + ep = get_ep_from_handle(pcd, ep_handle);
  88139. +
  88140. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  88141. + DWC_WARN("bad ep\n");
  88142. + return -DWC_E_INVALID;
  88143. + }
  88144. +
  88145. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  88146. + core_if = GET_CORE_IF(pcd);
  88147. + dwc_ep = &ep->dwc_ep;
  88148. +
  88149. + if (ep->iso_req_handle) {
  88150. + DWC_WARN("ISO request in progress\n");
  88151. + }
  88152. +
  88153. + dwc_ep->dma_addr0 = dma0;
  88154. + dwc_ep->dma_addr1 = dma1;
  88155. +
  88156. + dwc_ep->xfer_buff0 = buf0;
  88157. + dwc_ep->xfer_buff1 = buf1;
  88158. +
  88159. + dwc_ep->data_per_frame = data_per_frame;
  88160. +
  88161. + /** @todo - pattern data support is to be implemented in the future */
  88162. + dwc_ep->data_pattern_frame = dp_frame;
  88163. + dwc_ep->sync_frame = sync_frame;
  88164. +
  88165. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  88166. +
  88167. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  88168. +
  88169. + dwc_ep->proc_buf_num = 0;
  88170. +
  88171. + dwc_ep->pkt_per_frm = 0;
  88172. + frm_data = ep->dwc_ep.data_per_frame;
  88173. + while (frm_data > 0) {
  88174. + dwc_ep->pkt_per_frm++;
  88175. + frm_data -= ep->dwc_ep.maxpacket;
  88176. + }
  88177. +
  88178. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  88179. +
  88180. + if (start_frame == -1) {
  88181. + dwc_ep->next_frame = dsts.b.soffn + 1;
  88182. + if (dwc_ep->bInterval != 1) {
  88183. + dwc_ep->next_frame =
  88184. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  88185. + dwc_ep->next_frame %
  88186. + dwc_ep->bInterval);
  88187. + }
  88188. + } else {
  88189. + dwc_ep->next_frame = start_frame;
  88190. + }
  88191. +
  88192. + if (!core_if->pti_enh_enable) {
  88193. + dwc_ep->pkt_cnt =
  88194. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  88195. + dwc_ep->bInterval;
  88196. + } else {
  88197. + dwc_ep->pkt_cnt =
  88198. + (dwc_ep->data_per_frame *
  88199. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  88200. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  88201. + }
  88202. +
  88203. + if (core_if->dma_desc_enable) {
  88204. + dwc_ep->desc_cnt =
  88205. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  88206. + dwc_ep->bInterval;
  88207. + }
  88208. +
  88209. + if (atomic_alloc) {
  88210. + dwc_ep->pkt_info =
  88211. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  88212. + } else {
  88213. + dwc_ep->pkt_info =
  88214. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  88215. + }
  88216. + if (!dwc_ep->pkt_info) {
  88217. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  88218. + return -DWC_E_NO_MEMORY;
  88219. + }
  88220. + if (core_if->pti_enh_enable) {
  88221. + dwc_memset(dwc_ep->pkt_info, 0,
  88222. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  88223. + }
  88224. +
  88225. + dwc_ep->cur_pkt = 0;
  88226. + ep->iso_req_handle = req_handle;
  88227. +
  88228. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  88229. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  88230. + return 0;
  88231. +}
  88232. +
  88233. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  88234. + void *req_handle)
  88235. +{
  88236. + dwc_irqflags_t flags = 0;
  88237. + dwc_otg_pcd_ep_t *ep;
  88238. + dwc_ep_t *dwc_ep;
  88239. +
  88240. + ep = get_ep_from_handle(pcd, ep_handle);
  88241. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  88242. + DWC_WARN("bad ep\n");
  88243. + return -DWC_E_INVALID;
  88244. + }
  88245. + dwc_ep = &ep->dwc_ep;
  88246. +
  88247. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  88248. +
  88249. + DWC_FREE(dwc_ep->pkt_info);
  88250. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  88251. + if (ep->iso_req_handle != req_handle) {
  88252. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  88253. + return -DWC_E_INVALID;
  88254. + }
  88255. +
  88256. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  88257. +
  88258. + ep->iso_req_handle = 0;
  88259. + return 0;
  88260. +}
  88261. +
  88262. +/**
  88263. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  88264. + * for Isochronous EPs
  88265. + *
  88266. + * - Every time a sync period completes this function is called to
  88267. + * perform data exchange between PCD and gadget
  88268. + */
  88269. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  88270. + void *req_handle)
  88271. +{
  88272. + int i;
  88273. + dwc_ep_t *dwc_ep;
  88274. +
  88275. + dwc_ep = &ep->dwc_ep;
  88276. +
  88277. + DWC_SPINUNLOCK(ep->pcd->lock);
  88278. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  88279. + dwc_ep->proc_buf_num ^ 0x1);
  88280. + DWC_SPINLOCK(ep->pcd->lock);
  88281. +
  88282. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  88283. + dwc_ep->pkt_info[i].status = 0;
  88284. + dwc_ep->pkt_info[i].offset = 0;
  88285. + dwc_ep->pkt_info[i].length = 0;
  88286. + }
  88287. +}
  88288. +
  88289. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  88290. + void *iso_req_handle)
  88291. +{
  88292. + dwc_otg_pcd_ep_t *ep;
  88293. + dwc_ep_t *dwc_ep;
  88294. +
  88295. + ep = get_ep_from_handle(pcd, ep_handle);
  88296. + if (!ep->desc || ep->dwc_ep.num == 0) {
  88297. + DWC_WARN("bad ep\n");
  88298. + return -DWC_E_INVALID;
  88299. + }
  88300. + dwc_ep = &ep->dwc_ep;
  88301. +
  88302. + return dwc_ep->pkt_cnt;
  88303. +}
  88304. +
  88305. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  88306. + void *iso_req_handle, int packet,
  88307. + int *status, int *actual, int *offset)
  88308. +{
  88309. + dwc_otg_pcd_ep_t *ep;
  88310. + dwc_ep_t *dwc_ep;
  88311. +
  88312. + ep = get_ep_from_handle(pcd, ep_handle);
  88313. + if (!ep)
  88314. + DWC_WARN("bad ep\n");
  88315. +
  88316. + dwc_ep = &ep->dwc_ep;
  88317. +
  88318. + *status = dwc_ep->pkt_info[packet].status;
  88319. + *actual = dwc_ep->pkt_info[packet].length;
  88320. + *offset = dwc_ep->pkt_info[packet].offset;
  88321. +}
  88322. +
  88323. +#endif /* DWC_EN_ISOC */
  88324. +
  88325. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  88326. + uint32_t is_in, uint32_t ep_num)
  88327. +{
  88328. + /* Init EP structure */
  88329. + pcd_ep->desc = 0;
  88330. + pcd_ep->pcd = pcd;
  88331. + pcd_ep->stopped = 1;
  88332. + pcd_ep->queue_sof = 0;
  88333. +
  88334. + /* Init DWC ep structure */
  88335. + pcd_ep->dwc_ep.is_in = is_in;
  88336. + pcd_ep->dwc_ep.num = ep_num;
  88337. + pcd_ep->dwc_ep.active = 0;
  88338. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  88339. + /* Control until ep is actvated */
  88340. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  88341. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  88342. + pcd_ep->dwc_ep.dma_addr = 0;
  88343. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  88344. + pcd_ep->dwc_ep.xfer_buff = 0;
  88345. + pcd_ep->dwc_ep.xfer_len = 0;
  88346. + pcd_ep->dwc_ep.xfer_count = 0;
  88347. + pcd_ep->dwc_ep.sent_zlp = 0;
  88348. + pcd_ep->dwc_ep.total_len = 0;
  88349. + pcd_ep->dwc_ep.desc_addr = 0;
  88350. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  88351. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  88352. +}
  88353. +
  88354. +/**
  88355. + * Initialize ep's
  88356. + */
  88357. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  88358. +{
  88359. + int i;
  88360. + uint32_t hwcfg1;
  88361. + dwc_otg_pcd_ep_t *ep;
  88362. + int in_ep_cntr, out_ep_cntr;
  88363. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  88364. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  88365. +
  88366. + /**
  88367. + * Initialize the EP0 structure.
  88368. + */
  88369. + ep = &pcd->ep0;
  88370. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  88371. +
  88372. + in_ep_cntr = 0;
  88373. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  88374. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  88375. + if ((hwcfg1 & 0x1) == 0) {
  88376. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  88377. + in_ep_cntr++;
  88378. + /**
  88379. + * @todo NGS: Add direction to EP, based on contents
  88380. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  88381. + * sprintf(";r
  88382. + */
  88383. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  88384. +
  88385. + DWC_CIRCLEQ_INIT(&ep->queue);
  88386. + }
  88387. + hwcfg1 >>= 2;
  88388. + }
  88389. +
  88390. + out_ep_cntr = 0;
  88391. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  88392. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  88393. + if ((hwcfg1 & 0x1) == 0) {
  88394. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  88395. + out_ep_cntr++;
  88396. + /**
  88397. + * @todo NGS: Add direction to EP, based on contents
  88398. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  88399. + * sprintf(";r
  88400. + */
  88401. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  88402. + DWC_CIRCLEQ_INIT(&ep->queue);
  88403. + }
  88404. + hwcfg1 >>= 2;
  88405. + }
  88406. +
  88407. + pcd->ep0state = EP0_DISCONNECT;
  88408. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  88409. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  88410. +}
  88411. +
  88412. +/**
  88413. + * This function is called when the SRP timer expires. The SRP should
  88414. + * complete within 6 seconds.
  88415. + */
  88416. +static void srp_timeout(void *ptr)
  88417. +{
  88418. + gotgctl_data_t gotgctl;
  88419. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  88420. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  88421. +
  88422. + gotgctl.d32 = DWC_READ_REG32(addr);
  88423. +
  88424. + core_if->srp_timer_started = 0;
  88425. +
  88426. + if (core_if->adp_enable) {
  88427. + if (gotgctl.b.bsesvld == 0) {
  88428. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  88429. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  88430. + /* Power off the core */
  88431. + if (core_if->power_down == 2) {
  88432. + gpwrdn.b.pwrdnswtch = 1;
  88433. + DWC_MODIFY_REG32(&core_if->
  88434. + core_global_regs->gpwrdn,
  88435. + gpwrdn.d32, 0);
  88436. + }
  88437. +
  88438. + gpwrdn.d32 = 0;
  88439. + gpwrdn.b.pmuintsel = 1;
  88440. + gpwrdn.b.pmuactv = 1;
  88441. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  88442. + gpwrdn.d32);
  88443. + dwc_otg_adp_probe_start(core_if);
  88444. + } else {
  88445. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  88446. + core_if->op_state = B_PERIPHERAL;
  88447. + dwc_otg_core_init(core_if);
  88448. + dwc_otg_enable_global_interrupts(core_if);
  88449. + cil_pcd_start(core_if);
  88450. + }
  88451. + }
  88452. +
  88453. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  88454. + (core_if->core_params->i2c_enable)) {
  88455. + DWC_PRINTF("SRP Timeout\n");
  88456. +
  88457. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  88458. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  88459. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  88460. + }
  88461. +
  88462. + /* Clear Session Request */
  88463. + gotgctl.d32 = 0;
  88464. + gotgctl.b.sesreq = 1;
  88465. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  88466. + gotgctl.d32, 0);
  88467. +
  88468. + core_if->srp_success = 0;
  88469. + } else {
  88470. + __DWC_ERROR("Device not connected/responding\n");
  88471. + gotgctl.b.sesreq = 0;
  88472. + DWC_WRITE_REG32(addr, gotgctl.d32);
  88473. + }
  88474. + } else if (gotgctl.b.sesreq) {
  88475. + DWC_PRINTF("SRP Timeout\n");
  88476. +
  88477. + __DWC_ERROR("Device not connected/responding\n");
  88478. + gotgctl.b.sesreq = 0;
  88479. + DWC_WRITE_REG32(addr, gotgctl.d32);
  88480. + } else {
  88481. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  88482. + }
  88483. +}
  88484. +
  88485. +/**
  88486. + * Tasklet
  88487. + *
  88488. + */
  88489. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  88490. +
  88491. +static void start_xfer_tasklet_func(void *data)
  88492. +{
  88493. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  88494. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  88495. +
  88496. + int i;
  88497. + depctl_data_t diepctl;
  88498. +
  88499. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  88500. +
  88501. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  88502. +
  88503. + if (pcd->ep0.queue_sof) {
  88504. + pcd->ep0.queue_sof = 0;
  88505. + start_next_request(&pcd->ep0);
  88506. + // break;
  88507. + }
  88508. +
  88509. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  88510. + depctl_data_t diepctl;
  88511. + diepctl.d32 =
  88512. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  88513. +
  88514. + if (pcd->in_ep[i].queue_sof) {
  88515. + pcd->in_ep[i].queue_sof = 0;
  88516. + start_next_request(&pcd->in_ep[i]);
  88517. + // break;
  88518. + }
  88519. + }
  88520. +
  88521. + return;
  88522. +}
  88523. +
  88524. +/**
  88525. + * This function initialized the PCD portion of the driver.
  88526. + *
  88527. + */
  88528. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  88529. +{
  88530. + dwc_otg_pcd_t *pcd = NULL;
  88531. + dwc_otg_dev_if_t *dev_if;
  88532. + int i;
  88533. +
  88534. + /*
  88535. + * Allocate PCD structure
  88536. + */
  88537. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  88538. +
  88539. + if (pcd == NULL) {
  88540. + return NULL;
  88541. + }
  88542. +
  88543. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  88544. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
  88545. +#else
  88546. + pcd->lock = DWC_SPINLOCK_ALLOC();
  88547. +#endif
  88548. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  88549. + pcd, core_if);//GRAYG
  88550. + if (!pcd->lock) {
  88551. + DWC_ERROR("Could not allocate lock for pcd");
  88552. + DWC_FREE(pcd);
  88553. + return NULL;
  88554. + }
  88555. + /* Set core_if's lock pointer to hcd->lock */
  88556. + core_if->lock = pcd->lock;
  88557. + pcd->core_if = core_if;
  88558. +
  88559. + dev_if = core_if->dev_if;
  88560. + dev_if->isoc_ep = NULL;
  88561. +
  88562. + if (core_if->hwcfg4.b.ded_fifo_en) {
  88563. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  88564. + } else {
  88565. + DWC_PRINTF("Shared Tx FIFO mode\n");
  88566. + }
  88567. +
  88568. + /*
  88569. + * Initialized the Core for Device mode here if there is nod ADP support.
  88570. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  88571. + */
  88572. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  88573. + dwc_otg_core_dev_init(core_if);
  88574. + }
  88575. +
  88576. + /*
  88577. + * Register the PCD Callbacks.
  88578. + */
  88579. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  88580. +
  88581. + /*
  88582. + * Initialize the DMA buffer for SETUP packets
  88583. + */
  88584. + if (GET_CORE_IF(pcd)->dma_enable) {
  88585. + pcd->setup_pkt =
  88586. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  88587. + &pcd->setup_pkt_dma_handle);
  88588. + if (pcd->setup_pkt == NULL) {
  88589. + DWC_FREE(pcd);
  88590. + return NULL;
  88591. + }
  88592. +
  88593. + pcd->status_buf =
  88594. + DWC_DMA_ALLOC(sizeof(uint16_t),
  88595. + &pcd->status_buf_dma_handle);
  88596. + if (pcd->status_buf == NULL) {
  88597. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  88598. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  88599. + DWC_FREE(pcd);
  88600. + return NULL;
  88601. + }
  88602. +
  88603. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  88604. + dev_if->setup_desc_addr[0] =
  88605. + dwc_otg_ep_alloc_desc_chain
  88606. + (&dev_if->dma_setup_desc_addr[0], 1);
  88607. + dev_if->setup_desc_addr[1] =
  88608. + dwc_otg_ep_alloc_desc_chain
  88609. + (&dev_if->dma_setup_desc_addr[1], 1);
  88610. + dev_if->in_desc_addr =
  88611. + dwc_otg_ep_alloc_desc_chain
  88612. + (&dev_if->dma_in_desc_addr, 1);
  88613. + dev_if->out_desc_addr =
  88614. + dwc_otg_ep_alloc_desc_chain
  88615. + (&dev_if->dma_out_desc_addr, 1);
  88616. + pcd->data_terminated = 0;
  88617. +
  88618. + if (dev_if->setup_desc_addr[0] == 0
  88619. + || dev_if->setup_desc_addr[1] == 0
  88620. + || dev_if->in_desc_addr == 0
  88621. + || dev_if->out_desc_addr == 0) {
  88622. +
  88623. + if (dev_if->out_desc_addr)
  88624. + dwc_otg_ep_free_desc_chain
  88625. + (dev_if->out_desc_addr,
  88626. + dev_if->dma_out_desc_addr, 1);
  88627. + if (dev_if->in_desc_addr)
  88628. + dwc_otg_ep_free_desc_chain
  88629. + (dev_if->in_desc_addr,
  88630. + dev_if->dma_in_desc_addr, 1);
  88631. + if (dev_if->setup_desc_addr[1])
  88632. + dwc_otg_ep_free_desc_chain
  88633. + (dev_if->setup_desc_addr[1],
  88634. + dev_if->dma_setup_desc_addr[1], 1);
  88635. + if (dev_if->setup_desc_addr[0])
  88636. + dwc_otg_ep_free_desc_chain
  88637. + (dev_if->setup_desc_addr[0],
  88638. + dev_if->dma_setup_desc_addr[0], 1);
  88639. +
  88640. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  88641. + pcd->setup_pkt,
  88642. + pcd->setup_pkt_dma_handle);
  88643. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  88644. + pcd->status_buf,
  88645. + pcd->status_buf_dma_handle);
  88646. +
  88647. + DWC_FREE(pcd);
  88648. +
  88649. + return NULL;
  88650. + }
  88651. + }
  88652. + } else {
  88653. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  88654. + if (pcd->setup_pkt == NULL) {
  88655. + DWC_FREE(pcd);
  88656. + return NULL;
  88657. + }
  88658. +
  88659. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  88660. + if (pcd->status_buf == NULL) {
  88661. + DWC_FREE(pcd->setup_pkt);
  88662. + DWC_FREE(pcd);
  88663. + return NULL;
  88664. + }
  88665. + }
  88666. +
  88667. + dwc_otg_pcd_reinit(pcd);
  88668. +
  88669. + /* Allocate the cfi object for the PCD */
  88670. +#ifdef DWC_UTE_CFI
  88671. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  88672. + if (NULL == pcd->cfi)
  88673. + goto fail;
  88674. + if (init_cfi(pcd->cfi)) {
  88675. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  88676. + goto fail;
  88677. + }
  88678. +#endif
  88679. +
  88680. + /* Initialize tasklets */
  88681. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  88682. + start_xfer_tasklet_func, pcd);
  88683. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  88684. + do_test_mode, pcd);
  88685. +
  88686. + /* Initialize SRP timer */
  88687. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  88688. +
  88689. + if (core_if->core_params->dev_out_nak) {
  88690. + /**
  88691. + * Initialize xfer timeout timer. Implemented for
  88692. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  88693. + */
  88694. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  88695. + pcd->core_if->ep_xfer_timer[i] =
  88696. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  88697. + &pcd->core_if->ep_xfer_info[i]);
  88698. + }
  88699. + }
  88700. +
  88701. + return pcd;
  88702. +#ifdef DWC_UTE_CFI
  88703. +fail:
  88704. +#endif
  88705. + if (pcd->setup_pkt)
  88706. + DWC_FREE(pcd->setup_pkt);
  88707. + if (pcd->status_buf)
  88708. + DWC_FREE(pcd->status_buf);
  88709. +#ifdef DWC_UTE_CFI
  88710. + if (pcd->cfi)
  88711. + DWC_FREE(pcd->cfi);
  88712. +#endif
  88713. + if (pcd)
  88714. + DWC_FREE(pcd);
  88715. + return NULL;
  88716. +
  88717. +}
  88718. +
  88719. +/**
  88720. + * Remove PCD specific data
  88721. + */
  88722. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  88723. +{
  88724. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  88725. + int i;
  88726. + if (pcd->core_if->core_params->dev_out_nak) {
  88727. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  88728. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  88729. + pcd->core_if->ep_xfer_info[i].state = 0;
  88730. + }
  88731. + }
  88732. +
  88733. + if (GET_CORE_IF(pcd)->dma_enable) {
  88734. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  88735. + pcd->setup_pkt_dma_handle);
  88736. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  88737. + pcd->status_buf_dma_handle);
  88738. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  88739. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  88740. + dev_if->dma_setup_desc_addr
  88741. + [0], 1);
  88742. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  88743. + dev_if->dma_setup_desc_addr
  88744. + [1], 1);
  88745. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  88746. + dev_if->dma_in_desc_addr, 1);
  88747. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  88748. + dev_if->dma_out_desc_addr,
  88749. + 1);
  88750. + }
  88751. + } else {
  88752. + DWC_FREE(pcd->setup_pkt);
  88753. + DWC_FREE(pcd->status_buf);
  88754. + }
  88755. + DWC_SPINLOCK_FREE(pcd->lock);
  88756. + /* Set core_if's lock pointer to NULL */
  88757. + pcd->core_if->lock = NULL;
  88758. +
  88759. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  88760. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  88761. + if (pcd->core_if->core_params->dev_out_nak) {
  88762. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  88763. + if (pcd->core_if->ep_xfer_timer[i]) {
  88764. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  88765. + }
  88766. + }
  88767. + }
  88768. +
  88769. +/* Release the CFI object's dynamic memory */
  88770. +#ifdef DWC_UTE_CFI
  88771. + if (pcd->cfi->ops.release) {
  88772. + pcd->cfi->ops.release(pcd->cfi);
  88773. + }
  88774. +#endif
  88775. +
  88776. + DWC_FREE(pcd);
  88777. +}
  88778. +
  88779. +/**
  88780. + * Returns whether registered pcd is dual speed or not
  88781. + */
  88782. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  88783. +{
  88784. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  88785. +
  88786. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  88787. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  88788. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  88789. + (core_if->core_params->ulpi_fs_ls))) {
  88790. + return 0;
  88791. + }
  88792. +
  88793. + return 1;
  88794. +}
  88795. +
  88796. +/**
  88797. + * Returns whether registered pcd is OTG capable or not
  88798. + */
  88799. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  88800. +{
  88801. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  88802. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  88803. +
  88804. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  88805. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  88806. + return 0;
  88807. + }
  88808. +
  88809. + return 1;
  88810. +}
  88811. +
  88812. +/**
  88813. + * This function assigns periodic Tx FIFO to an periodic EP
  88814. + * in shared Tx FIFO mode
  88815. + */
  88816. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  88817. +{
  88818. + uint32_t TxMsk = 1;
  88819. + int i;
  88820. +
  88821. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  88822. + if ((TxMsk & core_if->tx_msk) == 0) {
  88823. + core_if->tx_msk |= TxMsk;
  88824. + return i + 1;
  88825. + }
  88826. + TxMsk <<= 1;
  88827. + }
  88828. + return 0;
  88829. +}
  88830. +
  88831. +/**
  88832. + * This function assigns periodic Tx FIFO to an periodic EP
  88833. + * in shared Tx FIFO mode
  88834. + */
  88835. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  88836. +{
  88837. + uint32_t PerTxMsk = 1;
  88838. + int i;
  88839. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  88840. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  88841. + core_if->p_tx_msk |= PerTxMsk;
  88842. + return i + 1;
  88843. + }
  88844. + PerTxMsk <<= 1;
  88845. + }
  88846. + return 0;
  88847. +}
  88848. +
  88849. +/**
  88850. + * This function releases periodic Tx FIFO
  88851. + * in shared Tx FIFO mode
  88852. + */
  88853. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  88854. + uint32_t fifo_num)
  88855. +{
  88856. + core_if->p_tx_msk =
  88857. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  88858. +}
  88859. +
  88860. +/**
  88861. + * This function releases periodic Tx FIFO
  88862. + * in shared Tx FIFO mode
  88863. + */
  88864. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  88865. +{
  88866. + core_if->tx_msk =
  88867. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  88868. +}
  88869. +
  88870. +/**
  88871. + * This function is being called from gadget
  88872. + * to enable PCD endpoint.
  88873. + */
  88874. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  88875. + const uint8_t * ep_desc, void *usb_ep)
  88876. +{
  88877. + int num, dir;
  88878. + dwc_otg_pcd_ep_t *ep = NULL;
  88879. + const usb_endpoint_descriptor_t *desc;
  88880. + dwc_irqflags_t flags;
  88881. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  88882. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  88883. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  88884. + int retval = 0;
  88885. + int i, epcount;
  88886. +
  88887. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  88888. +
  88889. + if (!desc) {
  88890. + pcd->ep0.priv = usb_ep;
  88891. + ep = &pcd->ep0;
  88892. + retval = -DWC_E_INVALID;
  88893. + goto out;
  88894. + }
  88895. +
  88896. + num = UE_GET_ADDR(desc->bEndpointAddress);
  88897. + dir = UE_GET_DIR(desc->bEndpointAddress);
  88898. +
  88899. + if (!desc->wMaxPacketSize) {
  88900. + DWC_WARN("bad maxpacketsize\n");
  88901. + retval = -DWC_E_INVALID;
  88902. + goto out;
  88903. + }
  88904. +
  88905. + if (dir == UE_DIR_IN) {
  88906. + epcount = pcd->core_if->dev_if->num_in_eps;
  88907. + for (i = 0; i < epcount; i++) {
  88908. + if (num == pcd->in_ep[i].dwc_ep.num) {
  88909. + ep = &pcd->in_ep[i];
  88910. + break;
  88911. + }
  88912. + }
  88913. + } else {
  88914. + epcount = pcd->core_if->dev_if->num_out_eps;
  88915. + for (i = 0; i < epcount; i++) {
  88916. + if (num == pcd->out_ep[i].dwc_ep.num) {
  88917. + ep = &pcd->out_ep[i];
  88918. + break;
  88919. + }
  88920. + }
  88921. + }
  88922. +
  88923. + if (!ep) {
  88924. + DWC_WARN("bad address\n");
  88925. + retval = -DWC_E_INVALID;
  88926. + goto out;
  88927. + }
  88928. +
  88929. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  88930. +
  88931. + ep->desc = desc;
  88932. + ep->priv = usb_ep;
  88933. +
  88934. + /*
  88935. + * Activate the EP
  88936. + */
  88937. + ep->stopped = 0;
  88938. +
  88939. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  88940. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  88941. +
  88942. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  88943. +
  88944. + if (ep->dwc_ep.is_in) {
  88945. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  88946. + ep->dwc_ep.tx_fifo_num = 0;
  88947. +
  88948. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  88949. + /*
  88950. + * if ISOC EP then assign a Periodic Tx FIFO.
  88951. + */
  88952. + ep->dwc_ep.tx_fifo_num =
  88953. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  88954. + }
  88955. + } else {
  88956. + /*
  88957. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  88958. + */
  88959. + ep->dwc_ep.tx_fifo_num =
  88960. + assign_tx_fifo(GET_CORE_IF(pcd));
  88961. + }
  88962. +
  88963. + /* Calculating EP info controller base address */
  88964. + if (ep->dwc_ep.tx_fifo_num
  88965. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  88966. + gdfifocfg.d32 =
  88967. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  88968. + core_global_regs->gdfifocfg);
  88969. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  88970. + dptxfsiz.d32 =
  88971. + (DWC_READ_REG32
  88972. + (&GET_CORE_IF(pcd)->core_global_regs->
  88973. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  88974. + gdfifocfg.b.epinfobase =
  88975. + gdfifocfgbase.d32 + dptxfsiz.d32;
  88976. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  88977. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  88978. + core_global_regs->gdfifocfg,
  88979. + gdfifocfg.d32);
  88980. + }
  88981. + }
  88982. + }
  88983. + /* Set initial data PID. */
  88984. + if (ep->dwc_ep.type == UE_BULK) {
  88985. + ep->dwc_ep.data_pid_start = 0;
  88986. + }
  88987. +
  88988. + /* Alloc DMA Descriptors */
  88989. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  88990. +#ifndef DWC_UTE_PER_IO
  88991. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  88992. +#endif
  88993. + ep->dwc_ep.desc_addr =
  88994. + dwc_otg_ep_alloc_desc_chain(&ep->
  88995. + dwc_ep.dma_desc_addr,
  88996. + MAX_DMA_DESC_CNT);
  88997. + if (!ep->dwc_ep.desc_addr) {
  88998. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  88999. + __func__);
  89000. + retval = -DWC_E_SHUTDOWN;
  89001. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89002. + goto out;
  89003. + }
  89004. +#ifndef DWC_UTE_PER_IO
  89005. + }
  89006. +#endif
  89007. + }
  89008. +
  89009. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  89010. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  89011. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  89012. +#ifdef DWC_UTE_PER_IO
  89013. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  89014. +#endif
  89015. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  89016. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  89017. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  89018. + }
  89019. +
  89020. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  89021. +
  89022. +#ifdef DWC_UTE_CFI
  89023. + if (pcd->cfi->ops.ep_enable) {
  89024. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  89025. + }
  89026. +#endif
  89027. +
  89028. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89029. +
  89030. +out:
  89031. + return retval;
  89032. +}
  89033. +
  89034. +/**
  89035. + * This function is being called from gadget
  89036. + * to disable PCD endpoint.
  89037. + */
  89038. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  89039. +{
  89040. + dwc_otg_pcd_ep_t *ep;
  89041. + dwc_irqflags_t flags;
  89042. + dwc_otg_dev_dma_desc_t *desc_addr;
  89043. + dwc_dma_t dma_desc_addr;
  89044. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  89045. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  89046. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  89047. +
  89048. + ep = get_ep_from_handle(pcd, ep_handle);
  89049. +
  89050. + if (!ep || !ep->desc) {
  89051. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  89052. + return -DWC_E_INVALID;
  89053. + }
  89054. +
  89055. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89056. +
  89057. + dwc_otg_request_nuke(ep);
  89058. +
  89059. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  89060. + if (pcd->core_if->core_params->dev_out_nak) {
  89061. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  89062. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  89063. + }
  89064. + ep->desc = NULL;
  89065. + ep->stopped = 1;
  89066. +
  89067. + gdfifocfg.d32 =
  89068. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  89069. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  89070. +
  89071. + if (ep->dwc_ep.is_in) {
  89072. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  89073. + /* Flush the Tx FIFO */
  89074. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  89075. + ep->dwc_ep.tx_fifo_num);
  89076. + }
  89077. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  89078. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  89079. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  89080. + /* Decreasing EPinfo Base Addr */
  89081. + dptxfsiz.d32 =
  89082. + (DWC_READ_REG32
  89083. + (&GET_CORE_IF(pcd)->
  89084. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  89085. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  89086. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  89087. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  89088. + gdfifocfg.d32);
  89089. + }
  89090. + }
  89091. + }
  89092. +
  89093. + /* Free DMA Descriptors */
  89094. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  89095. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  89096. + desc_addr = ep->dwc_ep.desc_addr;
  89097. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  89098. +
  89099. + /* Cannot call dma_free_coherent() with IRQs disabled */
  89100. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89101. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  89102. + MAX_DMA_DESC_CNT);
  89103. +
  89104. + goto out_unlocked;
  89105. + }
  89106. + }
  89107. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89108. +
  89109. +out_unlocked:
  89110. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  89111. + ep->dwc_ep.is_in ? "IN" : "OUT");
  89112. + return 0;
  89113. +
  89114. +}
  89115. +
  89116. +/******************************************************************************/
  89117. +#ifdef DWC_UTE_PER_IO
  89118. +
  89119. +/**
  89120. + * Free the request and its extended parts
  89121. + *
  89122. + */
  89123. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  89124. +{
  89125. + DWC_FREE(req->ext_req.per_io_frame_descs);
  89126. + DWC_FREE(req);
  89127. +}
  89128. +
  89129. +/**
  89130. + * Start the next request in the endpoint's queue.
  89131. + *
  89132. + */
  89133. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  89134. + dwc_otg_pcd_ep_t * ep)
  89135. +{
  89136. + int i;
  89137. + dwc_otg_pcd_request_t *req = NULL;
  89138. + dwc_ep_t *dwcep = NULL;
  89139. + struct dwc_iso_xreq_port *ereq = NULL;
  89140. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  89141. + uint16_t nat;
  89142. + depctl_data_t diepctl;
  89143. +
  89144. + dwcep = &ep->dwc_ep;
  89145. +
  89146. + if (dwcep->xiso_active_xfers > 0) {
  89147. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  89148. + DWC_WARN("There are currently active transfers for EP%d \
  89149. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  89150. + dwcep->xiso_queued_xfers);
  89151. +#endif
  89152. + return 0;
  89153. + }
  89154. +
  89155. + nat = UGETW(ep->desc->wMaxPacketSize);
  89156. + nat = (nat >> 11) & 0x03;
  89157. +
  89158. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  89159. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  89160. + ereq = &req->ext_req;
  89161. + ep->stopped = 0;
  89162. +
  89163. + /* Get the frame number */
  89164. + dwcep->xiso_frame_num =
  89165. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  89166. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  89167. +
  89168. + ddesc_iso = ereq->per_io_frame_descs;
  89169. +
  89170. + if (dwcep->is_in) {
  89171. + /* Setup DMA Descriptor chain for IN Isoc request */
  89172. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  89173. + //if ((i % (nat + 1)) == 0)
  89174. + if ( i > 0 )
  89175. + dwcep->xiso_frame_num =
  89176. + (dwcep->xiso_bInterval +
  89177. + dwcep->xiso_frame_num) & 0x3FFF;
  89178. + dwcep->desc_addr[i].buf =
  89179. + req->dma + ddesc_iso[i].offset;
  89180. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  89181. + ddesc_iso[i].length;
  89182. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  89183. + dwcep->xiso_frame_num;
  89184. + dwcep->desc_addr[i].status.b_iso_in.bs =
  89185. + BS_HOST_READY;
  89186. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  89187. + dwcep->desc_addr[i].status.b_iso_in.sp =
  89188. + (ddesc_iso[i].length %
  89189. + dwcep->maxpacket) ? 1 : 0;
  89190. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  89191. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  89192. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  89193. +
  89194. + /* Process the last descriptor */
  89195. + if (i == ereq->pio_pkt_count - 1) {
  89196. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  89197. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  89198. + }
  89199. + }
  89200. +
  89201. + /* Setup and start the transfer for this endpoint */
  89202. + dwcep->xiso_active_xfers++;
  89203. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  89204. + in_ep_regs[dwcep->num]->diepdma,
  89205. + dwcep->dma_desc_addr);
  89206. + diepctl.d32 = 0;
  89207. + diepctl.b.epena = 1;
  89208. + diepctl.b.cnak = 1;
  89209. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  89210. + in_ep_regs[dwcep->num]->diepctl, 0,
  89211. + diepctl.d32);
  89212. + } else {
  89213. + /* Setup DMA Descriptor chain for OUT Isoc request */
  89214. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  89215. + //if ((i % (nat + 1)) == 0)
  89216. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  89217. + dwcep->xiso_frame_num) & 0x3FFF;
  89218. + dwcep->desc_addr[i].buf =
  89219. + req->dma + ddesc_iso[i].offset;
  89220. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  89221. + ddesc_iso[i].length;
  89222. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  89223. + dwcep->xiso_frame_num;
  89224. + dwcep->desc_addr[i].status.b_iso_out.bs =
  89225. + BS_HOST_READY;
  89226. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  89227. + dwcep->desc_addr[i].status.b_iso_out.sp =
  89228. + (ddesc_iso[i].length %
  89229. + dwcep->maxpacket) ? 1 : 0;
  89230. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  89231. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  89232. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  89233. +
  89234. + /* Process the last descriptor */
  89235. + if (i == ereq->pio_pkt_count - 1) {
  89236. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  89237. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  89238. + }
  89239. + }
  89240. +
  89241. + /* Setup and start the transfer for this endpoint */
  89242. + dwcep->xiso_active_xfers++;
  89243. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  89244. + dev_if->out_ep_regs[dwcep->num]->
  89245. + doepdma, dwcep->dma_desc_addr);
  89246. + diepctl.d32 = 0;
  89247. + diepctl.b.epena = 1;
  89248. + diepctl.b.cnak = 1;
  89249. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  89250. + dev_if->out_ep_regs[dwcep->num]->
  89251. + doepctl, 0, diepctl.d32);
  89252. + }
  89253. +
  89254. + } else {
  89255. + ep->stopped = 1;
  89256. + }
  89257. +
  89258. + return 0;
  89259. +}
  89260. +
  89261. +/**
  89262. + * - Remove the request from the queue
  89263. + */
  89264. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  89265. +{
  89266. + dwc_otg_pcd_request_t *req = NULL;
  89267. + struct dwc_iso_xreq_port *ereq = NULL;
  89268. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  89269. + dwc_ep_t *dwcep = NULL;
  89270. + int i;
  89271. +
  89272. + //DWC_DEBUG();
  89273. + dwcep = &ep->dwc_ep;
  89274. +
  89275. + /* Get the first pending request from the queue */
  89276. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  89277. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  89278. + if (!req) {
  89279. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  89280. + return;
  89281. + }
  89282. + dwcep->xiso_active_xfers--;
  89283. + dwcep->xiso_queued_xfers--;
  89284. + /* Remove this request from the queue */
  89285. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  89286. + } else {
  89287. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  89288. + return;
  89289. + }
  89290. +
  89291. + ep->stopped = 1;
  89292. + ereq = &req->ext_req;
  89293. + ddesc_iso = ereq->per_io_frame_descs;
  89294. +
  89295. + if (dwcep->xiso_active_xfers < 0) {
  89296. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  89297. + dwcep->xiso_active_xfers);
  89298. + }
  89299. +
  89300. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  89301. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  89302. + if (dwcep->is_in) { /* IN endpoints */
  89303. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  89304. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  89305. + ddesc_iso[i].status =
  89306. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  89307. + } else { /* OUT endpoints */
  89308. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  89309. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  89310. + ddesc_iso[i].status =
  89311. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  89312. + }
  89313. + }
  89314. +
  89315. + DWC_SPINUNLOCK(ep->pcd->lock);
  89316. +
  89317. + /* Call the completion function in the non-portable logic */
  89318. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  89319. + &req->ext_req);
  89320. +
  89321. + DWC_SPINLOCK(ep->pcd->lock);
  89322. +
  89323. + /* Free the request - specific freeing needed for extended request object */
  89324. + dwc_pcd_xiso_ereq_free(ep, req);
  89325. +
  89326. + /* Start the next request */
  89327. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  89328. +
  89329. + return;
  89330. +}
  89331. +
  89332. +/**
  89333. + * Create and initialize the Isoc pkt descriptors of the extended request.
  89334. + *
  89335. + */
  89336. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  89337. + void *ereq_nonport,
  89338. + int atomic_alloc)
  89339. +{
  89340. + struct dwc_iso_xreq_port *ereq = NULL;
  89341. + struct dwc_iso_xreq_port *req_mapped = NULL;
  89342. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  89343. + uint32_t pkt_count;
  89344. + int i;
  89345. +
  89346. + ereq = &req->ext_req;
  89347. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  89348. + pkt_count = req_mapped->pio_pkt_count;
  89349. +
  89350. + /* Create the isoc descs */
  89351. + if (atomic_alloc) {
  89352. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  89353. + } else {
  89354. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  89355. + }
  89356. +
  89357. + if (!ipds) {
  89358. + DWC_ERROR("Failed to allocate isoc descriptors");
  89359. + return -DWC_E_NO_MEMORY;
  89360. + }
  89361. +
  89362. + /* Initialize the extended request fields */
  89363. + ereq->per_io_frame_descs = ipds;
  89364. + ereq->error_count = 0;
  89365. + ereq->pio_alloc_pkt_count = pkt_count;
  89366. + ereq->pio_pkt_count = pkt_count;
  89367. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  89368. +
  89369. + /* Init the Isoc descriptors */
  89370. + for (i = 0; i < pkt_count; i++) {
  89371. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  89372. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  89373. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  89374. + ipds[i].actual_length =
  89375. + req_mapped->per_io_frame_descs[i].actual_length;
  89376. + }
  89377. +
  89378. + return 0;
  89379. +}
  89380. +
  89381. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  89382. +{
  89383. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  89384. + int i;
  89385. +
  89386. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  89387. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  89388. + DWC_DEBUG("error_count=%d", ereq->error_count);
  89389. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  89390. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  89391. + DWC_DEBUG("res=%d", ereq->res);
  89392. +
  89393. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  89394. + xfd = &ereq->per_io_frame_descs[0];
  89395. + DWC_DEBUG("FD #%d", i);
  89396. +
  89397. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  89398. + DWC_DEBUG("xfd->length=%d", xfd->length);
  89399. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  89400. + DWC_DEBUG("xfd->status=%d", xfd->status);
  89401. + }
  89402. +}
  89403. +
  89404. +/**
  89405. + *
  89406. + */
  89407. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  89408. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  89409. + int zero, void *req_handle, int atomic_alloc,
  89410. + void *ereq_nonport)
  89411. +{
  89412. + dwc_otg_pcd_request_t *req = NULL;
  89413. + dwc_otg_pcd_ep_t *ep;
  89414. + dwc_irqflags_t flags;
  89415. + int res;
  89416. +
  89417. + ep = get_ep_from_handle(pcd, ep_handle);
  89418. + if (!ep) {
  89419. + DWC_WARN("bad ep\n");
  89420. + return -DWC_E_INVALID;
  89421. + }
  89422. +
  89423. + /* We support this extension only for DDMA mode */
  89424. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  89425. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  89426. + return -DWC_E_INVALID;
  89427. +
  89428. + /* Create a dwc_otg_pcd_request_t object */
  89429. + if (atomic_alloc) {
  89430. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  89431. + } else {
  89432. + req = DWC_ALLOC(sizeof(*req));
  89433. + }
  89434. +
  89435. + if (!req) {
  89436. + return -DWC_E_NO_MEMORY;
  89437. + }
  89438. +
  89439. + /* Create the Isoc descs for this request which shall be the exact match
  89440. + * of the structure sent to us from the non-portable logic */
  89441. + res =
  89442. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  89443. + if (res) {
  89444. + DWC_WARN("Failed to init the Isoc descriptors");
  89445. + DWC_FREE(req);
  89446. + return res;
  89447. + }
  89448. +
  89449. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89450. +
  89451. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  89452. + req->buf = buf;
  89453. + req->dma = dma_buf;
  89454. + req->length = buflen;
  89455. + req->sent_zlp = zero;
  89456. + req->priv = req_handle;
  89457. +
  89458. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89459. + ep->dwc_ep.dma_addr = dma_buf;
  89460. + ep->dwc_ep.start_xfer_buff = buf;
  89461. + ep->dwc_ep.xfer_buff = buf;
  89462. + ep->dwc_ep.xfer_len = 0;
  89463. + ep->dwc_ep.xfer_count = 0;
  89464. + ep->dwc_ep.sent_zlp = 0;
  89465. + ep->dwc_ep.total_len = buflen;
  89466. +
  89467. + /* Add this request to the tail */
  89468. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  89469. + ep->dwc_ep.xiso_queued_xfers++;
  89470. +
  89471. +//DWC_DEBUG("CP_0");
  89472. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  89473. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  89474. +//prn_ext_request(&req->ext_req);
  89475. +
  89476. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89477. +
  89478. + /* If the req->status == ASAP then check if there is any active transfer
  89479. + * for this endpoint. If no active transfers, then get the first entry
  89480. + * from the queue and start that transfer
  89481. + */
  89482. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  89483. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  89484. + if (res) {
  89485. + DWC_WARN("Failed to start the next Isoc transfer");
  89486. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89487. + DWC_FREE(req);
  89488. + return res;
  89489. + }
  89490. + }
  89491. +
  89492. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89493. + return 0;
  89494. +}
  89495. +
  89496. +#endif
  89497. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  89498. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  89499. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  89500. + int zero, void *req_handle, int atomic_alloc)
  89501. +{
  89502. + dwc_irqflags_t flags;
  89503. + dwc_otg_pcd_request_t *req;
  89504. + dwc_otg_pcd_ep_t *ep;
  89505. + uint32_t max_transfer;
  89506. +
  89507. + ep = get_ep_from_handle(pcd, ep_handle);
  89508. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  89509. + DWC_WARN("bad ep\n");
  89510. + return -DWC_E_INVALID;
  89511. + }
  89512. +
  89513. + if (atomic_alloc) {
  89514. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  89515. + } else {
  89516. + req = DWC_ALLOC(sizeof(*req));
  89517. + }
  89518. +
  89519. + if (!req) {
  89520. + return -DWC_E_NO_MEMORY;
  89521. + }
  89522. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  89523. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  89524. + if (ep->dwc_ep.num != 0) {
  89525. + DWC_ERROR("queue req %p, len %d buf %p\n",
  89526. + req_handle, buflen, buf);
  89527. + }
  89528. + }
  89529. +
  89530. + req->buf = buf;
  89531. + req->dma = dma_buf;
  89532. + req->length = buflen;
  89533. + req->sent_zlp = zero;
  89534. + req->priv = req_handle;
  89535. + req->dw_align_buf = NULL;
  89536. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  89537. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  89538. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  89539. + &req->dw_align_buf_dma);
  89540. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89541. +
  89542. + /*
  89543. + * After adding request to the queue for IN ISOC wait for In Token Received
  89544. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  89545. + * Received when EP is disabled interrupt to obtain starting microframe
  89546. + * (odd/even) start transfer
  89547. + */
  89548. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  89549. + if (req != 0) {
  89550. + depctl_data_t depctl = {.d32 =
  89551. + DWC_READ_REG32(&pcd->core_if->dev_if->
  89552. + in_ep_regs[ep->dwc_ep.num]->
  89553. + diepctl) };
  89554. + ++pcd->request_pending;
  89555. +
  89556. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  89557. + if (ep->dwc_ep.is_in) {
  89558. + depctl.b.cnak = 1;
  89559. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  89560. + in_ep_regs[ep->dwc_ep.num]->
  89561. + diepctl, depctl.d32);
  89562. + }
  89563. +
  89564. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89565. + }
  89566. + return 0;
  89567. + }
  89568. +
  89569. + /*
  89570. + * For EP0 IN without premature status, zlp is required?
  89571. + */
  89572. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  89573. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  89574. + //_req->zero = 1;
  89575. + }
  89576. +
  89577. + /* Start the transfer */
  89578. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  89579. + /* EP0 Transfer? */
  89580. + if (ep->dwc_ep.num == 0) {
  89581. + switch (pcd->ep0state) {
  89582. + case EP0_IN_DATA_PHASE:
  89583. + DWC_DEBUGPL(DBG_PCD,
  89584. + "%s ep0: EP0_IN_DATA_PHASE\n",
  89585. + __func__);
  89586. + break;
  89587. +
  89588. + case EP0_OUT_DATA_PHASE:
  89589. + DWC_DEBUGPL(DBG_PCD,
  89590. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  89591. + __func__);
  89592. + if (pcd->request_config) {
  89593. + /* Complete STATUS PHASE */
  89594. + ep->dwc_ep.is_in = 1;
  89595. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  89596. + }
  89597. + break;
  89598. +
  89599. + case EP0_IN_STATUS_PHASE:
  89600. + DWC_DEBUGPL(DBG_PCD,
  89601. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  89602. + __func__);
  89603. + break;
  89604. +
  89605. + default:
  89606. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  89607. + pcd->ep0state);
  89608. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89609. + return -DWC_E_SHUTDOWN;
  89610. + }
  89611. +
  89612. + ep->dwc_ep.dma_addr = dma_buf;
  89613. + ep->dwc_ep.start_xfer_buff = buf;
  89614. + ep->dwc_ep.xfer_buff = buf;
  89615. + ep->dwc_ep.xfer_len = buflen;
  89616. + ep->dwc_ep.xfer_count = 0;
  89617. + ep->dwc_ep.sent_zlp = 0;
  89618. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  89619. +
  89620. + if (zero) {
  89621. + if ((ep->dwc_ep.xfer_len %
  89622. + ep->dwc_ep.maxpacket == 0)
  89623. + && (ep->dwc_ep.xfer_len != 0)) {
  89624. + ep->dwc_ep.sent_zlp = 1;
  89625. + }
  89626. +
  89627. + }
  89628. +
  89629. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  89630. + &ep->dwc_ep);
  89631. + } // non-ep0 endpoints
  89632. + else {
  89633. +#ifdef DWC_UTE_CFI
  89634. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  89635. + /* store the request length */
  89636. + ep->dwc_ep.cfi_req_len = buflen;
  89637. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  89638. + ep, req);
  89639. + } else {
  89640. +#endif
  89641. + max_transfer =
  89642. + GET_CORE_IF(ep->pcd)->core_params->
  89643. + max_transfer_size;
  89644. +
  89645. + /* Setup and start the Transfer */
  89646. + if (req->dw_align_buf){
  89647. + if (ep->dwc_ep.is_in)
  89648. + dwc_memcpy(req->dw_align_buf,
  89649. + buf, buflen);
  89650. + ep->dwc_ep.dma_addr =
  89651. + req->dw_align_buf_dma;
  89652. + ep->dwc_ep.start_xfer_buff =
  89653. + req->dw_align_buf;
  89654. + ep->dwc_ep.xfer_buff =
  89655. + req->dw_align_buf;
  89656. + } else {
  89657. + ep->dwc_ep.dma_addr = dma_buf;
  89658. + ep->dwc_ep.start_xfer_buff = buf;
  89659. + ep->dwc_ep.xfer_buff = buf;
  89660. + }
  89661. + ep->dwc_ep.xfer_len = 0;
  89662. + ep->dwc_ep.xfer_count = 0;
  89663. + ep->dwc_ep.sent_zlp = 0;
  89664. + ep->dwc_ep.total_len = buflen;
  89665. +
  89666. + ep->dwc_ep.maxxfer = max_transfer;
  89667. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  89668. + uint32_t out_max_xfer =
  89669. + DDMA_MAX_TRANSFER_SIZE -
  89670. + (DDMA_MAX_TRANSFER_SIZE % 4);
  89671. + if (ep->dwc_ep.is_in) {
  89672. + if (ep->dwc_ep.maxxfer >
  89673. + DDMA_MAX_TRANSFER_SIZE) {
  89674. + ep->dwc_ep.maxxfer =
  89675. + DDMA_MAX_TRANSFER_SIZE;
  89676. + }
  89677. + } else {
  89678. + if (ep->dwc_ep.maxxfer >
  89679. + out_max_xfer) {
  89680. + ep->dwc_ep.maxxfer =
  89681. + out_max_xfer;
  89682. + }
  89683. + }
  89684. + }
  89685. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  89686. + ep->dwc_ep.maxxfer -=
  89687. + (ep->dwc_ep.maxxfer %
  89688. + ep->dwc_ep.maxpacket);
  89689. + }
  89690. +
  89691. + if (zero) {
  89692. + if ((ep->dwc_ep.total_len %
  89693. + ep->dwc_ep.maxpacket == 0)
  89694. + && (ep->dwc_ep.total_len != 0)) {
  89695. + ep->dwc_ep.sent_zlp = 1;
  89696. + }
  89697. + }
  89698. +#ifdef DWC_UTE_CFI
  89699. + }
  89700. +#endif
  89701. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  89702. + &ep->dwc_ep);
  89703. + }
  89704. + }
  89705. +
  89706. + if (req != 0) {
  89707. + ++pcd->request_pending;
  89708. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  89709. + if (ep->dwc_ep.is_in && ep->stopped
  89710. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  89711. + /** @todo NGS Create a function for this. */
  89712. + diepmsk_data_t diepmsk = {.d32 = 0 };
  89713. + diepmsk.b.intktxfemp = 1;
  89714. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  89715. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  89716. + dev_if->dev_global_regs->diepeachintmsk
  89717. + [ep->dwc_ep.num], 0,
  89718. + diepmsk.d32);
  89719. + } else {
  89720. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  89721. + dev_if->dev_global_regs->
  89722. + diepmsk, 0, diepmsk.d32);
  89723. + }
  89724. +
  89725. + }
  89726. + }
  89727. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89728. +
  89729. + return 0;
  89730. +}
  89731. +
  89732. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  89733. + void *req_handle)
  89734. +{
  89735. + dwc_irqflags_t flags;
  89736. + dwc_otg_pcd_request_t *req;
  89737. + dwc_otg_pcd_ep_t *ep;
  89738. +
  89739. + ep = get_ep_from_handle(pcd, ep_handle);
  89740. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  89741. + DWC_WARN("bad argument\n");
  89742. + return -DWC_E_INVALID;
  89743. + }
  89744. +
  89745. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89746. +
  89747. + /* make sure it's actually queued on this endpoint */
  89748. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  89749. + if (req->priv == (void *)req_handle) {
  89750. + break;
  89751. + }
  89752. + }
  89753. +
  89754. + if (req->priv != (void *)req_handle) {
  89755. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89756. + return -DWC_E_INVALID;
  89757. + }
  89758. +
  89759. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  89760. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  89761. + } else {
  89762. + req = NULL;
  89763. + }
  89764. +
  89765. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89766. +
  89767. + return req ? 0 : -DWC_E_SHUTDOWN;
  89768. +
  89769. +}
  89770. +
  89771. +/**
  89772. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  89773. + *
  89774. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  89775. + * requests. If the gadget driver clears the halt status, it will
  89776. + * automatically unwedge the endpoint.
  89777. + *
  89778. + * Returns zero on success, else negative DWC error code.
  89779. + */
  89780. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  89781. +{
  89782. + dwc_otg_pcd_ep_t *ep;
  89783. + dwc_irqflags_t flags;
  89784. + int retval = 0;
  89785. +
  89786. + ep = get_ep_from_handle(pcd, ep_handle);
  89787. +
  89788. + if ((!ep->desc && ep != &pcd->ep0) ||
  89789. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  89790. + DWC_WARN("%s, bad ep\n", __func__);
  89791. + return -DWC_E_INVALID;
  89792. + }
  89793. +
  89794. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89795. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  89796. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  89797. + ep->dwc_ep.is_in ? "IN" : "OUT");
  89798. + retval = -DWC_E_AGAIN;
  89799. + } else {
  89800. + /* This code needs to be reviewed */
  89801. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  89802. + dtxfsts_data_t txstatus;
  89803. + fifosize_data_t txfifosize;
  89804. +
  89805. + txfifosize.d32 =
  89806. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  89807. + core_global_regs->dtxfsiz[ep->dwc_ep.
  89808. + tx_fifo_num]);
  89809. + txstatus.d32 =
  89810. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  89811. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  89812. + dtxfsts);
  89813. +
  89814. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  89815. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  89816. + retval = -DWC_E_AGAIN;
  89817. + } else {
  89818. + if (ep->dwc_ep.num == 0) {
  89819. + pcd->ep0state = EP0_STALL;
  89820. + }
  89821. +
  89822. + ep->stopped = 1;
  89823. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  89824. + &ep->dwc_ep);
  89825. + }
  89826. + } else {
  89827. + if (ep->dwc_ep.num == 0) {
  89828. + pcd->ep0state = EP0_STALL;
  89829. + }
  89830. +
  89831. + ep->stopped = 1;
  89832. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  89833. + }
  89834. + }
  89835. +
  89836. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89837. +
  89838. + return retval;
  89839. +}
  89840. +
  89841. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  89842. +{
  89843. + dwc_otg_pcd_ep_t *ep;
  89844. + dwc_irqflags_t flags;
  89845. + int retval = 0;
  89846. +
  89847. + ep = get_ep_from_handle(pcd, ep_handle);
  89848. +
  89849. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  89850. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  89851. + DWC_WARN("%s, bad ep\n", __func__);
  89852. + return -DWC_E_INVALID;
  89853. + }
  89854. +
  89855. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89856. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  89857. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  89858. + ep->dwc_ep.is_in ? "IN" : "OUT");
  89859. + retval = -DWC_E_AGAIN;
  89860. + } else if (value == 0) {
  89861. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  89862. + } else if (value == 1) {
  89863. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  89864. + dtxfsts_data_t txstatus;
  89865. + fifosize_data_t txfifosize;
  89866. +
  89867. + txfifosize.d32 =
  89868. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  89869. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  89870. + txstatus.d32 =
  89871. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  89872. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  89873. +
  89874. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  89875. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  89876. + retval = -DWC_E_AGAIN;
  89877. + } else {
  89878. + if (ep->dwc_ep.num == 0) {
  89879. + pcd->ep0state = EP0_STALL;
  89880. + }
  89881. +
  89882. + ep->stopped = 1;
  89883. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  89884. + &ep->dwc_ep);
  89885. + }
  89886. + } else {
  89887. + if (ep->dwc_ep.num == 0) {
  89888. + pcd->ep0state = EP0_STALL;
  89889. + }
  89890. +
  89891. + ep->stopped = 1;
  89892. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  89893. + }
  89894. + } else if (value == 2) {
  89895. + ep->dwc_ep.stall_clear_flag = 0;
  89896. + } else if (value == 3) {
  89897. + ep->dwc_ep.stall_clear_flag = 1;
  89898. + }
  89899. +
  89900. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89901. +
  89902. + return retval;
  89903. +}
  89904. +
  89905. +/**
  89906. + * This function initiates remote wakeup of the host from suspend state.
  89907. + */
  89908. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  89909. +{
  89910. + dctl_data_t dctl = { 0 };
  89911. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  89912. + dsts_data_t dsts;
  89913. +
  89914. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  89915. + if (!dsts.b.suspsts) {
  89916. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  89917. + }
  89918. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  89919. + if (pcd->remote_wakeup_enable) {
  89920. + if (set) {
  89921. +
  89922. + if (core_if->adp_enable) {
  89923. + gpwrdn_data_t gpwrdn;
  89924. +
  89925. + dwc_otg_adp_probe_stop(core_if);
  89926. +
  89927. + /* Mask SRP detected interrupt from Power Down Logic */
  89928. + gpwrdn.d32 = 0;
  89929. + gpwrdn.b.srp_det_msk = 1;
  89930. + DWC_MODIFY_REG32(&core_if->
  89931. + core_global_regs->gpwrdn,
  89932. + gpwrdn.d32, 0);
  89933. +
  89934. + /* Disable Power Down Logic */
  89935. + gpwrdn.d32 = 0;
  89936. + gpwrdn.b.pmuactv = 1;
  89937. + DWC_MODIFY_REG32(&core_if->
  89938. + core_global_regs->gpwrdn,
  89939. + gpwrdn.d32, 0);
  89940. +
  89941. + /*
  89942. + * Initialize the Core for Device mode.
  89943. + */
  89944. + core_if->op_state = B_PERIPHERAL;
  89945. + dwc_otg_core_init(core_if);
  89946. + dwc_otg_enable_global_interrupts(core_if);
  89947. + cil_pcd_start(core_if);
  89948. +
  89949. + dwc_otg_initiate_srp(core_if);
  89950. + }
  89951. +
  89952. + dctl.b.rmtwkupsig = 1;
  89953. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  89954. + dctl, 0, dctl.d32);
  89955. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  89956. +
  89957. + dwc_mdelay(2);
  89958. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  89959. + dctl, dctl.d32, 0);
  89960. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  89961. + }
  89962. + } else {
  89963. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  89964. + }
  89965. +}
  89966. +
  89967. +#ifdef CONFIG_USB_DWC_OTG_LPM
  89968. +/**
  89969. + * This function initiates remote wakeup of the host from L1 sleep state.
  89970. + */
  89971. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  89972. +{
  89973. + glpmcfg_data_t lpmcfg;
  89974. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  89975. +
  89976. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  89977. +
  89978. + /* Check if we are in L1 state */
  89979. + if (!lpmcfg.b.prt_sleep_sts) {
  89980. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  89981. + return;
  89982. + }
  89983. +
  89984. + /* Check if host allows remote wakeup */
  89985. + if (!lpmcfg.b.rem_wkup_en) {
  89986. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  89987. + return;
  89988. + }
  89989. +
  89990. + /* Check if Resume OK */
  89991. + if (!lpmcfg.b.sleep_state_resumeok) {
  89992. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  89993. + return;
  89994. + }
  89995. +
  89996. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  89997. + lpmcfg.b.en_utmi_sleep = 0;
  89998. + lpmcfg.b.hird_thres &= (~(1 << 4));
  89999. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  90000. +
  90001. + if (set) {
  90002. + dctl_data_t dctl = {.d32 = 0 };
  90003. + dctl.b.rmtwkupsig = 1;
  90004. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  90005. + * Hardware will automatically clear this bit.
  90006. + */
  90007. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  90008. + 0, dctl.d32);
  90009. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  90010. + }
  90011. +
  90012. +}
  90013. +#endif
  90014. +
  90015. +/**
  90016. + * Performs remote wakeup.
  90017. + */
  90018. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  90019. +{
  90020. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90021. + dwc_irqflags_t flags;
  90022. + if (dwc_otg_is_device_mode(core_if)) {
  90023. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90024. +#ifdef CONFIG_USB_DWC_OTG_LPM
  90025. + if (core_if->lx_state == DWC_OTG_L1) {
  90026. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  90027. + } else {
  90028. +#endif
  90029. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  90030. +#ifdef CONFIG_USB_DWC_OTG_LPM
  90031. + }
  90032. +#endif
  90033. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90034. + }
  90035. + return;
  90036. +}
  90037. +
  90038. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  90039. +{
  90040. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90041. + dctl_data_t dctl = { 0 };
  90042. +
  90043. + if (dwc_otg_is_device_mode(core_if)) {
  90044. + dctl.b.sftdiscon = 1;
  90045. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  90046. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  90047. + dwc_udelay(no_of_usecs);
  90048. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  90049. +
  90050. + } else{
  90051. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  90052. + }
  90053. + return;
  90054. +
  90055. +}
  90056. +
  90057. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  90058. +{
  90059. + dsts_data_t dsts;
  90060. + gotgctl_data_t gotgctl;
  90061. +
  90062. + /*
  90063. + * This function starts the Protocol if no session is in progress. If
  90064. + * a session is already in progress, but the device is suspended,
  90065. + * remote wakeup signaling is started.
  90066. + */
  90067. +
  90068. + /* Check if valid session */
  90069. + gotgctl.d32 =
  90070. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  90071. + if (gotgctl.b.bsesvld) {
  90072. + /* Check if suspend state */
  90073. + dsts.d32 =
  90074. + DWC_READ_REG32(&
  90075. + (GET_CORE_IF(pcd)->dev_if->
  90076. + dev_global_regs->dsts));
  90077. + if (dsts.b.suspsts) {
  90078. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  90079. + }
  90080. + } else {
  90081. + dwc_otg_pcd_initiate_srp(pcd);
  90082. + }
  90083. +
  90084. + return 0;
  90085. +
  90086. +}
  90087. +
  90088. +/**
  90089. + * Start the SRP timer to detect when the SRP does not complete within
  90090. + * 6 seconds.
  90091. + *
  90092. + * @param pcd the pcd structure.
  90093. + */
  90094. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  90095. +{
  90096. + dwc_irqflags_t flags;
  90097. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90098. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  90099. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90100. +}
  90101. +
  90102. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  90103. +{
  90104. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  90105. +}
  90106. +
  90107. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  90108. +{
  90109. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  90110. +}
  90111. +
  90112. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  90113. +{
  90114. + return pcd->b_hnp_enable;
  90115. +}
  90116. +
  90117. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  90118. +{
  90119. + return pcd->a_hnp_support;
  90120. +}
  90121. +
  90122. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  90123. +{
  90124. + return pcd->a_alt_hnp_support;
  90125. +}
  90126. +
  90127. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  90128. +{
  90129. + return pcd->remote_wakeup_enable;
  90130. +}
  90131. +
  90132. +#endif /* DWC_HOST_ONLY */
  90133. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  90134. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 1969-12-31 18:00:00.000000000 -0600
  90135. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 2014-12-11 14:02:55.400418001 -0600
  90136. @@ -0,0 +1,266 @@
  90137. +/* ==========================================================================
  90138. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  90139. + * $Revision: #48 $
  90140. + * $Date: 2012/08/10 $
  90141. + * $Change: 2047372 $
  90142. + *
  90143. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  90144. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  90145. + * otherwise expressly agreed to in writing between Synopsys and you.
  90146. + *
  90147. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  90148. + * any End User Software License Agreement or Agreement for Licensed Product
  90149. + * with Synopsys or any supplement thereto. You are permitted to use and
  90150. + * redistribute this Software in source and binary forms, with or without
  90151. + * modification, provided that redistributions of source code must retain this
  90152. + * notice. You may not view, use, disclose, copy or distribute this file or
  90153. + * any information contained herein except pursuant to this license grant from
  90154. + * Synopsys. If you do not agree with this notice, including the disclaimer
  90155. + * below, then you are not authorized to use the Software.
  90156. + *
  90157. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  90158. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  90159. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  90160. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  90161. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  90162. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  90163. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90164. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  90165. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  90166. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  90167. + * DAMAGE.
  90168. + * ========================================================================== */
  90169. +#ifndef DWC_HOST_ONLY
  90170. +#if !defined(__DWC_PCD_H__)
  90171. +#define __DWC_PCD_H__
  90172. +
  90173. +#include "dwc_otg_os_dep.h"
  90174. +#include "usb.h"
  90175. +#include "dwc_otg_cil.h"
  90176. +#include "dwc_otg_pcd_if.h"
  90177. +struct cfiobject;
  90178. +
  90179. +/**
  90180. + * @file
  90181. + *
  90182. + * This file contains the structures, constants, and interfaces for
  90183. + * the Perpherial Contoller Driver (PCD).
  90184. + *
  90185. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  90186. + * Gadget API, so that the existing Gadget drivers can be used. For
  90187. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  90188. + * (FBS) driver will be used. The FBS driver supports the
  90189. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  90190. + * transports.
  90191. + *
  90192. + */
  90193. +
  90194. +/** Invalid DMA Address */
  90195. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  90196. +
  90197. +/** Max Transfer size for any EP */
  90198. +#define DDMA_MAX_TRANSFER_SIZE 65535
  90199. +
  90200. +/**
  90201. + * Get the pointer to the core_if from the pcd pointer.
  90202. + */
  90203. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  90204. +
  90205. +/**
  90206. + * States of EP0.
  90207. + */
  90208. +typedef enum ep0_state {
  90209. + EP0_DISCONNECT, /* no host */
  90210. + EP0_IDLE,
  90211. + EP0_IN_DATA_PHASE,
  90212. + EP0_OUT_DATA_PHASE,
  90213. + EP0_IN_STATUS_PHASE,
  90214. + EP0_OUT_STATUS_PHASE,
  90215. + EP0_STALL,
  90216. +} ep0state_e;
  90217. +
  90218. +/** Fordward declaration.*/
  90219. +struct dwc_otg_pcd;
  90220. +
  90221. +/** DWC_otg iso request structure.
  90222. + *
  90223. + */
  90224. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  90225. +
  90226. +#ifdef DWC_UTE_PER_IO
  90227. +
  90228. +/**
  90229. + * This shall be the exact analogy of the same type structure defined in the
  90230. + * usb_gadget.h. Each descriptor contains
  90231. + */
  90232. +struct dwc_iso_pkt_desc_port {
  90233. + uint32_t offset;
  90234. + uint32_t length; /* expected length */
  90235. + uint32_t actual_length;
  90236. + uint32_t status;
  90237. +};
  90238. +
  90239. +struct dwc_iso_xreq_port {
  90240. + /** transfer/submission flag */
  90241. + uint32_t tr_sub_flags;
  90242. + /** Start the request ASAP */
  90243. +#define DWC_EREQ_TF_ASAP 0x00000002
  90244. + /** Just enqueue the request w/o initiating a transfer */
  90245. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  90246. +
  90247. + /**
  90248. + * count of ISO packets attached to this request - shall
  90249. + * not exceed the pio_alloc_pkt_count
  90250. + */
  90251. + uint32_t pio_pkt_count;
  90252. + /** count of ISO packets allocated for this request */
  90253. + uint32_t pio_alloc_pkt_count;
  90254. + /** number of ISO packet errors */
  90255. + uint32_t error_count;
  90256. + /** reserved for future extension */
  90257. + uint32_t res;
  90258. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  90259. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  90260. +};
  90261. +#endif
  90262. +/** DWC_otg request structure.
  90263. + * This structure is a list of requests.
  90264. + */
  90265. +typedef struct dwc_otg_pcd_request {
  90266. + void *priv;
  90267. + void *buf;
  90268. + dwc_dma_t dma;
  90269. + uint32_t length;
  90270. + uint32_t actual;
  90271. + unsigned sent_zlp:1;
  90272. + /**
  90273. + * Used instead of original buffer if
  90274. + * it(physical address) is not dword-aligned.
  90275. + **/
  90276. + uint8_t *dw_align_buf;
  90277. + dwc_dma_t dw_align_buf_dma;
  90278. +
  90279. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  90280. +#ifdef DWC_UTE_PER_IO
  90281. + struct dwc_iso_xreq_port ext_req;
  90282. + //void *priv_ereq_nport; /* */
  90283. +#endif
  90284. +} dwc_otg_pcd_request_t;
  90285. +
  90286. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  90287. +
  90288. +/** PCD EP structure.
  90289. + * This structure describes an EP, there is an array of EPs in the PCD
  90290. + * structure.
  90291. + */
  90292. +typedef struct dwc_otg_pcd_ep {
  90293. + /** USB EP Descriptor */
  90294. + const usb_endpoint_descriptor_t *desc;
  90295. +
  90296. + /** queue of dwc_otg_pcd_requests. */
  90297. + struct req_list queue;
  90298. + unsigned stopped:1;
  90299. + unsigned disabling:1;
  90300. + unsigned dma:1;
  90301. + unsigned queue_sof:1;
  90302. +
  90303. +#ifdef DWC_EN_ISOC
  90304. + /** ISOC req handle passed */
  90305. + void *iso_req_handle;
  90306. +#endif //_EN_ISOC_
  90307. +
  90308. + /** DWC_otg ep data. */
  90309. + dwc_ep_t dwc_ep;
  90310. +
  90311. + /** Pointer to PCD */
  90312. + struct dwc_otg_pcd *pcd;
  90313. +
  90314. + void *priv;
  90315. +} dwc_otg_pcd_ep_t;
  90316. +
  90317. +/** DWC_otg PCD Structure.
  90318. + * This structure encapsulates the data for the dwc_otg PCD.
  90319. + */
  90320. +struct dwc_otg_pcd {
  90321. + const struct dwc_otg_pcd_function_ops *fops;
  90322. + /** The DWC otg device pointer */
  90323. + struct dwc_otg_device *otg_dev;
  90324. + /** Core Interface */
  90325. + dwc_otg_core_if_t *core_if;
  90326. + /** State of EP0 */
  90327. + ep0state_e ep0state;
  90328. + /** EP0 Request is pending */
  90329. + unsigned ep0_pending:1;
  90330. + /** Indicates when SET CONFIGURATION Request is in process */
  90331. + unsigned request_config:1;
  90332. + /** The state of the Remote Wakeup Enable. */
  90333. + unsigned remote_wakeup_enable:1;
  90334. + /** The state of the B-Device HNP Enable. */
  90335. + unsigned b_hnp_enable:1;
  90336. + /** The state of A-Device HNP Support. */
  90337. + unsigned a_hnp_support:1;
  90338. + /** The state of the A-Device Alt HNP support. */
  90339. + unsigned a_alt_hnp_support:1;
  90340. + /** Count of pending Requests */
  90341. + unsigned request_pending;
  90342. +
  90343. + /** SETUP packet for EP0
  90344. + * This structure is allocated as a DMA buffer on PCD initialization
  90345. + * with enough space for up to 3 setup packets.
  90346. + */
  90347. + union {
  90348. + usb_device_request_t req;
  90349. + uint32_t d32[2];
  90350. + } *setup_pkt;
  90351. +
  90352. + dwc_dma_t setup_pkt_dma_handle;
  90353. +
  90354. + /* Additional buffer and flag for CTRL_WR premature case */
  90355. + uint8_t *backup_buf;
  90356. + unsigned data_terminated;
  90357. +
  90358. + /** 2-byte dma buffer used to return status from GET_STATUS */
  90359. + uint16_t *status_buf;
  90360. + dwc_dma_t status_buf_dma_handle;
  90361. +
  90362. + /** EP0 */
  90363. + dwc_otg_pcd_ep_t ep0;
  90364. +
  90365. + /** Array of IN EPs. */
  90366. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  90367. + /** Array of OUT EPs. */
  90368. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  90369. + /** number of valid EPs in the above array. */
  90370. +// unsigned num_eps : 4;
  90371. + dwc_spinlock_t *lock;
  90372. +
  90373. + /** Tasklet to defer starting of TEST mode transmissions until
  90374. + * Status Phase has been completed.
  90375. + */
  90376. + dwc_tasklet_t *test_mode_tasklet;
  90377. +
  90378. + /** Tasklet to delay starting of xfer in DMA mode */
  90379. + dwc_tasklet_t *start_xfer_tasklet;
  90380. +
  90381. + /** The test mode to enter when the tasklet is executed. */
  90382. + unsigned test_mode;
  90383. + /** The cfi_api structure that implements most of the CFI API
  90384. + * and OTG specific core configuration functionality
  90385. + */
  90386. +#ifdef DWC_UTE_CFI
  90387. + struct cfiobject *cfi;
  90388. +#endif
  90389. +
  90390. +};
  90391. +
  90392. +//FIXME this functions should be static, and this prototypes should be removed
  90393. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  90394. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  90395. + dwc_otg_pcd_request_t * req, int32_t status);
  90396. +
  90397. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  90398. + void *req_handle);
  90399. +
  90400. +extern void do_test_mode(void *data);
  90401. +#endif
  90402. +#endif /* DWC_HOST_ONLY */
  90403. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  90404. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 1969-12-31 18:00:00.000000000 -0600
  90405. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 2014-12-11 14:02:55.400418001 -0600
  90406. @@ -0,0 +1,360 @@
  90407. +/* ==========================================================================
  90408. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  90409. + * $Revision: #11 $
  90410. + * $Date: 2011/10/26 $
  90411. + * $Change: 1873028 $
  90412. + *
  90413. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  90414. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  90415. + * otherwise expressly agreed to in writing between Synopsys and you.
  90416. + *
  90417. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  90418. + * any End User Software License Agreement or Agreement for Licensed Product
  90419. + * with Synopsys or any supplement thereto. You are permitted to use and
  90420. + * redistribute this Software in source and binary forms, with or without
  90421. + * modification, provided that redistributions of source code must retain this
  90422. + * notice. You may not view, use, disclose, copy or distribute this file or
  90423. + * any information contained herein except pursuant to this license grant from
  90424. + * Synopsys. If you do not agree with this notice, including the disclaimer
  90425. + * below, then you are not authorized to use the Software.
  90426. + *
  90427. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  90428. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  90429. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  90430. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  90431. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  90432. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  90433. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90434. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  90435. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  90436. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  90437. + * DAMAGE.
  90438. + * ========================================================================== */
  90439. +#ifndef DWC_HOST_ONLY
  90440. +
  90441. +#if !defined(__DWC_PCD_IF_H__)
  90442. +#define __DWC_PCD_IF_H__
  90443. +
  90444. +//#include "dwc_os.h"
  90445. +#include "dwc_otg_core_if.h"
  90446. +
  90447. +/** @file
  90448. + * This file defines DWC_OTG PCD Core API.
  90449. + */
  90450. +
  90451. +struct dwc_otg_pcd;
  90452. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  90453. +
  90454. +/** Maxpacket size for EP0 */
  90455. +#define MAX_EP0_SIZE 64
  90456. +/** Maxpacket size for any EP */
  90457. +#define MAX_PACKET_SIZE 1024
  90458. +
  90459. +/** @name Function Driver Callbacks */
  90460. +/** @{ */
  90461. +
  90462. +/** This function will be called whenever a previously queued request has
  90463. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  90464. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  90465. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  90466. + * parameters. */
  90467. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  90468. + void *req_handle, int32_t status,
  90469. + uint32_t actual);
  90470. +/**
  90471. + * This function will be called whenever a previousle queued ISOC request has
  90472. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  90473. + * function.
  90474. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  90475. + * functions.
  90476. + */
  90477. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  90478. + void *req_handle, int proc_buf_num);
  90479. +/** This function should handle any SETUP request that cannot be handled by the
  90480. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  90481. + * class-specific requests, etc. The function must non-blocking.
  90482. + *
  90483. + * Returns 0 on success.
  90484. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  90485. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  90486. + * Returns -DWC_E_SHUTDOWN on any other error. */
  90487. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  90488. +/** This is called whenever the device has been disconnected. The function
  90489. + * driver should take appropriate action to clean up all pending requests in the
  90490. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  90491. + * state. */
  90492. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  90493. +/** This function is called when device has been connected. */
  90494. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  90495. +/** This function is called when device has been suspended */
  90496. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  90497. +/** This function is called when device has received LPM tokens, i.e.
  90498. + * device has been sent to sleep state. */
  90499. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  90500. +/** This function is called when device has been resumed
  90501. + * from suspend(L2) or L1 sleep state. */
  90502. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  90503. +/** This function is called whenever hnp params has been changed.
  90504. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  90505. + * to get hnp parameters. */
  90506. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  90507. +/** This function is called whenever USB RESET is detected. */
  90508. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  90509. +
  90510. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  90511. +
  90512. +/**
  90513. + *
  90514. + * @param ep_handle Void pointer to the usb_ep structure
  90515. + * @param ereq_port Pointer to the extended request structure created in the
  90516. + * portable part.
  90517. + */
  90518. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  90519. + void *req_handle, int32_t status,
  90520. + void *ereq_port);
  90521. +/** Function Driver Ops Data Structure */
  90522. +struct dwc_otg_pcd_function_ops {
  90523. + dwc_connect_cb_t connect;
  90524. + dwc_disconnect_cb_t disconnect;
  90525. + dwc_setup_cb_t setup;
  90526. + dwc_completion_cb_t complete;
  90527. + dwc_isoc_completion_cb_t isoc_complete;
  90528. + dwc_suspend_cb_t suspend;
  90529. + dwc_sleep_cb_t sleep;
  90530. + dwc_resume_cb_t resume;
  90531. + dwc_reset_cb_t reset;
  90532. + dwc_hnp_params_changed_cb_t hnp_changed;
  90533. + cfi_setup_cb_t cfi_setup;
  90534. +#ifdef DWC_UTE_PER_IO
  90535. + xiso_completion_cb_t xisoc_complete;
  90536. +#endif
  90537. +};
  90538. +/** @} */
  90539. +
  90540. +/** @name Function Driver Functions */
  90541. +/** @{ */
  90542. +
  90543. +/** Call this function to get pointer on dwc_otg_pcd_t,
  90544. + * this pointer will be used for all PCD API functions.
  90545. + *
  90546. + * @param core_if The DWC_OTG Core
  90547. + */
  90548. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  90549. +
  90550. +/** Frees PCD allocated by dwc_otg_pcd_init
  90551. + *
  90552. + * @param pcd The PCD
  90553. + */
  90554. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  90555. +
  90556. +/** Call this to bind the function driver to the PCD Core.
  90557. + *
  90558. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  90559. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  90560. + */
  90561. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  90562. + const struct dwc_otg_pcd_function_ops *fops);
  90563. +
  90564. +/** Enables an endpoint for use. This function enables an endpoint in
  90565. + * the PCD. The endpoint is described by the ep_desc which has the
  90566. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  90567. + * to the endpoint from other API functions and in callbacks. Normally this
  90568. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  90569. + * core for that interface.
  90570. + *
  90571. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  90572. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  90573. + * Returns 0 on success.
  90574. + *
  90575. + * @param pcd The PCD
  90576. + * @param ep_desc Endpoint descriptor
  90577. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  90578. + */
  90579. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  90580. + const uint8_t * ep_desc, void *usb_ep);
  90581. +
  90582. +/** Disable the endpoint referenced by ep_handle.
  90583. + *
  90584. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  90585. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  90586. + * Returns 0 on success. */
  90587. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  90588. +
  90589. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  90590. + * After the transfer is completes, the complete callback will be called with
  90591. + * the request status.
  90592. + *
  90593. + * @param pcd The PCD
  90594. + * @param ep_handle The handle of the endpoint
  90595. + * @param buf The buffer for the data
  90596. + * @param dma_buf The DMA buffer for the data
  90597. + * @param buflen The length of the data transfer
  90598. + * @param zero Specifies whether to send zero length last packet.
  90599. + * @param req_handle Set this handle to any value to use to reference this
  90600. + * request in the ep_dequeue function or from the complete callback
  90601. + * @param atomic_alloc If driver need to perform atomic allocations
  90602. + * for internal data structures.
  90603. + *
  90604. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  90605. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  90606. + * Returns 0 on success. */
  90607. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  90608. + uint8_t * buf, dwc_dma_t dma_buf,
  90609. + uint32_t buflen, int zero, void *req_handle,
  90610. + int atomic_alloc);
  90611. +#ifdef DWC_UTE_PER_IO
  90612. +/**
  90613. + *
  90614. + * @param ereq_nonport Pointer to the extended request part of the
  90615. + * usb_request structure defined in usb_gadget.h file.
  90616. + */
  90617. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  90618. + uint8_t * buf, dwc_dma_t dma_buf,
  90619. + uint32_t buflen, int zero,
  90620. + void *req_handle, int atomic_alloc,
  90621. + void *ereq_nonport);
  90622. +
  90623. +#endif
  90624. +
  90625. +/** De-queue the specified data transfer that has not yet completed.
  90626. + *
  90627. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  90628. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  90629. + * Returns 0 on success. */
  90630. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  90631. + void *req_handle);
  90632. +
  90633. +/** Halt (STALL) an endpoint or clear it.
  90634. + *
  90635. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  90636. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  90637. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  90638. + * Returns 0 on success. */
  90639. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  90640. +
  90641. +/** This function */
  90642. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  90643. +
  90644. +/** This function should be called on every hardware interrupt */
  90645. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  90646. +
  90647. +/** This function returns current frame number */
  90648. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  90649. +
  90650. +/**
  90651. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  90652. + * For isochronous transfers duble buffering is used.
  90653. + * After processing each of buffers comlete callback will be called with
  90654. + * status for each transaction.
  90655. + *
  90656. + * @param pcd The PCD
  90657. + * @param ep_handle The handle of the endpoint
  90658. + * @param buf0 The virtual address of first data buffer
  90659. + * @param buf1 The virtual address of second data buffer
  90660. + * @param dma0 The DMA address of first data buffer
  90661. + * @param dma1 The DMA address of second data buffer
  90662. + * @param sync_frame Data pattern frame number
  90663. + * @param dp_frame Data size for pattern frame
  90664. + * @param data_per_frame Data size for regular frame
  90665. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  90666. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  90667. + * @param req_handle Handle of ISOC request
  90668. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  90669. + * internal data structures.
  90670. + *
  90671. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  90672. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  90673. + * Returns -DW_E_SHUTDOWN for any other error.
  90674. + * Returns 0 on success
  90675. + */
  90676. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  90677. + uint8_t * buf0, uint8_t * buf1,
  90678. + dwc_dma_t dma0, dwc_dma_t dma1,
  90679. + int sync_frame, int dp_frame,
  90680. + int data_per_frame, int start_frame,
  90681. + int buf_proc_intrvl, void *req_handle,
  90682. + int atomic_alloc);
  90683. +
  90684. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  90685. + *
  90686. + * @param pcd The PCD
  90687. + * @param ep_handle The handle of the endpoint
  90688. + * @param req_handle Handle of ISOC request
  90689. + *
  90690. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  90691. + * Returns 0 on success
  90692. + */
  90693. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  90694. + void *req_handle);
  90695. +
  90696. +/** Get ISOC packet status.
  90697. + *
  90698. + * @param pcd The PCD
  90699. + * @param ep_handle The handle of the endpoint
  90700. + * @param iso_req_handle Isochronoush request handle
  90701. + * @param packet Number of packet
  90702. + * @param status Out parameter for returning status
  90703. + * @param actual Out parameter for returning actual length
  90704. + * @param offset Out parameter for returning offset
  90705. + *
  90706. + */
  90707. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  90708. + void *ep_handle,
  90709. + void *iso_req_handle, int packet,
  90710. + int *status, int *actual,
  90711. + int *offset);
  90712. +
  90713. +/** Get ISOC packet count.
  90714. + *
  90715. + * @param pcd The PCD
  90716. + * @param ep_handle The handle of the endpoint
  90717. + * @param iso_req_handle
  90718. + */
  90719. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  90720. + void *ep_handle,
  90721. + void *iso_req_handle);
  90722. +
  90723. +/** This function starts the SRP Protocol if no session is in progress. If
  90724. + * a session is already in progress, but the device is suspended,
  90725. + * remote wakeup signaling is started.
  90726. + */
  90727. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  90728. +
  90729. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  90730. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  90731. +
  90732. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  90733. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  90734. +
  90735. +/** Initiate SRP */
  90736. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  90737. +
  90738. +/** Starts remote wakeup signaling. */
  90739. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  90740. +
  90741. +/** Starts micorsecond soft disconnect. */
  90742. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  90743. +/** This function returns whether device is dualspeed.*/
  90744. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  90745. +
  90746. +/** This function returns whether device is otg. */
  90747. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  90748. +
  90749. +/** These functions allow to get hnp parameters */
  90750. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  90751. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  90752. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  90753. +
  90754. +/** CFI specific Interface functions */
  90755. +/** Allocate a cfi buffer */
  90756. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  90757. + dwc_dma_t * addr, size_t buflen,
  90758. + int flags);
  90759. +
  90760. +/******************************************************************************/
  90761. +
  90762. +/** @} */
  90763. +
  90764. +#endif /* __DWC_PCD_IF_H__ */
  90765. +
  90766. +#endif /* DWC_HOST_ONLY */
  90767. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  90768. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 1969-12-31 18:00:00.000000000 -0600
  90769. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 2014-12-11 14:02:55.400418001 -0600
  90770. @@ -0,0 +1,5147 @@
  90771. +/* ==========================================================================
  90772. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  90773. + * $Revision: #116 $
  90774. + * $Date: 2012/08/10 $
  90775. + * $Change: 2047372 $
  90776. + *
  90777. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  90778. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  90779. + * otherwise expressly agreed to in writing between Synopsys and you.
  90780. + *
  90781. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  90782. + * any End User Software License Agreement or Agreement for Licensed Product
  90783. + * with Synopsys or any supplement thereto. You are permitted to use and
  90784. + * redistribute this Software in source and binary forms, with or without
  90785. + * modification, provided that redistributions of source code must retain this
  90786. + * notice. You may not view, use, disclose, copy or distribute this file or
  90787. + * any information contained herein except pursuant to this license grant from
  90788. + * Synopsys. If you do not agree with this notice, including the disclaimer
  90789. + * below, then you are not authorized to use the Software.
  90790. + *
  90791. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  90792. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  90793. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  90794. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  90795. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  90796. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  90797. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  90798. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  90799. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  90800. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  90801. + * DAMAGE.
  90802. + * ========================================================================== */
  90803. +#ifndef DWC_HOST_ONLY
  90804. +
  90805. +#include "dwc_otg_pcd.h"
  90806. +
  90807. +#ifdef DWC_UTE_CFI
  90808. +#include "dwc_otg_cfi.h"
  90809. +#endif
  90810. +
  90811. +#ifdef DWC_UTE_PER_IO
  90812. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  90813. +#endif
  90814. +//#define PRINT_CFI_DMA_DESCS
  90815. +
  90816. +#define DEBUG_EP0
  90817. +
  90818. +/**
  90819. + * This function updates OTG.
  90820. + */
  90821. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  90822. +{
  90823. +
  90824. + if (reset) {
  90825. + pcd->b_hnp_enable = 0;
  90826. + pcd->a_hnp_support = 0;
  90827. + pcd->a_alt_hnp_support = 0;
  90828. + }
  90829. +
  90830. + if (pcd->fops->hnp_changed) {
  90831. + pcd->fops->hnp_changed(pcd);
  90832. + }
  90833. +}
  90834. +
  90835. +/** @file
  90836. + * This file contains the implementation of the PCD Interrupt handlers.
  90837. + *
  90838. + * The PCD handles the device interrupts. Many conditions can cause a
  90839. + * device interrupt. When an interrupt occurs, the device interrupt
  90840. + * service routine determines the cause of the interrupt and
  90841. + * dispatches handling to the appropriate function. These interrupt
  90842. + * handling functions are described below.
  90843. + * All interrupt registers are processed from LSB to MSB.
  90844. + */
  90845. +
  90846. +/**
  90847. + * This function prints the ep0 state for debug purposes.
  90848. + */
  90849. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  90850. +{
  90851. +#ifdef DEBUG
  90852. + char str[40];
  90853. +
  90854. + switch (pcd->ep0state) {
  90855. + case EP0_DISCONNECT:
  90856. + dwc_strcpy(str, "EP0_DISCONNECT");
  90857. + break;
  90858. + case EP0_IDLE:
  90859. + dwc_strcpy(str, "EP0_IDLE");
  90860. + break;
  90861. + case EP0_IN_DATA_PHASE:
  90862. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  90863. + break;
  90864. + case EP0_OUT_DATA_PHASE:
  90865. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  90866. + break;
  90867. + case EP0_IN_STATUS_PHASE:
  90868. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  90869. + break;
  90870. + case EP0_OUT_STATUS_PHASE:
  90871. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  90872. + break;
  90873. + case EP0_STALL:
  90874. + dwc_strcpy(str, "EP0_STALL");
  90875. + break;
  90876. + default:
  90877. + dwc_strcpy(str, "EP0_INVALID");
  90878. + }
  90879. +
  90880. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  90881. +#endif
  90882. +}
  90883. +
  90884. +/**
  90885. + * This function calculate the size of the payload in the memory
  90886. + * for out endpoints and prints size for debug purposes(used in
  90887. + * 2.93a DevOutNak feature).
  90888. + */
  90889. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  90890. +{
  90891. +#ifdef DEBUG
  90892. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  90893. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  90894. + int pack_num;
  90895. + unsigned payload;
  90896. +
  90897. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  90898. + deptsiz_updt.d32 =
  90899. + DWC_READ_REG32(&pcd->core_if->dev_if->
  90900. + out_ep_regs[ep->num]->doeptsiz);
  90901. + /* Payload will be */
  90902. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  90903. + /* Packet count is decremented every time a packet
  90904. + * is written to the RxFIFO not in to the external memory
  90905. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  90906. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  90907. + DWC_DEBUGPL(DBG_PCDV,
  90908. + "Payload for EP%d-%s\n",
  90909. + ep->num, (ep->is_in ? "IN" : "OUT"));
  90910. + DWC_DEBUGPL(DBG_PCDV,
  90911. + "Number of transfered bytes = 0x%08x\n", payload);
  90912. + DWC_DEBUGPL(DBG_PCDV,
  90913. + "Number of transfered packets = %d\n", pack_num);
  90914. +#endif
  90915. +}
  90916. +
  90917. +
  90918. +#ifdef DWC_UTE_CFI
  90919. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  90920. + const uint8_t * epname, int descnum)
  90921. +{
  90922. + CFI_INFO
  90923. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  90924. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  90925. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  90926. + ddesc->status.b.bs);
  90927. +}
  90928. +#endif
  90929. +
  90930. +/**
  90931. + * This function returns pointer to in ep struct with number ep_num
  90932. + */
  90933. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  90934. +{
  90935. + int i;
  90936. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  90937. + if (ep_num == 0) {
  90938. + return &pcd->ep0;
  90939. + } else {
  90940. + for (i = 0; i < num_in_eps; ++i) {
  90941. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  90942. + return &pcd->in_ep[i];
  90943. + }
  90944. + return 0;
  90945. + }
  90946. +}
  90947. +
  90948. +/**
  90949. + * This function returns pointer to out ep struct with number ep_num
  90950. + */
  90951. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  90952. +{
  90953. + int i;
  90954. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  90955. + if (ep_num == 0) {
  90956. + return &pcd->ep0;
  90957. + } else {
  90958. + for (i = 0; i < num_out_eps; ++i) {
  90959. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  90960. + return &pcd->out_ep[i];
  90961. + }
  90962. + return 0;
  90963. + }
  90964. +}
  90965. +
  90966. +/**
  90967. + * This functions gets a pointer to an EP from the wIndex address
  90968. + * value of the control request.
  90969. + */
  90970. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  90971. +{
  90972. + dwc_otg_pcd_ep_t *ep;
  90973. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  90974. +
  90975. + if (ep_num == 0) {
  90976. + ep = &pcd->ep0;
  90977. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  90978. + ep = &pcd->in_ep[ep_num - 1];
  90979. + } else {
  90980. + ep = &pcd->out_ep[ep_num - 1];
  90981. + }
  90982. +
  90983. + return ep;
  90984. +}
  90985. +
  90986. +/**
  90987. + * This function checks the EP request queue, if the queue is not
  90988. + * empty the next request is started.
  90989. + */
  90990. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  90991. +{
  90992. + dwc_otg_pcd_request_t *req = 0;
  90993. + uint32_t max_transfer =
  90994. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  90995. +
  90996. +#ifdef DWC_UTE_CFI
  90997. + struct dwc_otg_pcd *pcd;
  90998. + pcd = ep->pcd;
  90999. +#endif
  91000. +
  91001. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  91002. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  91003. +
  91004. +#ifdef DWC_UTE_CFI
  91005. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  91006. + ep->dwc_ep.cfi_req_len = req->length;
  91007. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  91008. + } else {
  91009. +#endif
  91010. + /* Setup and start the Transfer */
  91011. + if (req->dw_align_buf) {
  91012. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  91013. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  91014. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  91015. + } else {
  91016. + ep->dwc_ep.dma_addr = req->dma;
  91017. + ep->dwc_ep.start_xfer_buff = req->buf;
  91018. + ep->dwc_ep.xfer_buff = req->buf;
  91019. + }
  91020. + ep->dwc_ep.sent_zlp = 0;
  91021. + ep->dwc_ep.total_len = req->length;
  91022. + ep->dwc_ep.xfer_len = 0;
  91023. + ep->dwc_ep.xfer_count = 0;
  91024. +
  91025. + ep->dwc_ep.maxxfer = max_transfer;
  91026. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  91027. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  91028. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  91029. + if (ep->dwc_ep.is_in) {
  91030. + if (ep->dwc_ep.maxxfer >
  91031. + DDMA_MAX_TRANSFER_SIZE) {
  91032. + ep->dwc_ep.maxxfer =
  91033. + DDMA_MAX_TRANSFER_SIZE;
  91034. + }
  91035. + } else {
  91036. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  91037. + ep->dwc_ep.maxxfer =
  91038. + out_max_xfer;
  91039. + }
  91040. + }
  91041. + }
  91042. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  91043. + ep->dwc_ep.maxxfer -=
  91044. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  91045. + }
  91046. + if (req->sent_zlp) {
  91047. + if ((ep->dwc_ep.total_len %
  91048. + ep->dwc_ep.maxpacket == 0)
  91049. + && (ep->dwc_ep.total_len != 0)) {
  91050. + ep->dwc_ep.sent_zlp = 1;
  91051. + }
  91052. +
  91053. + }
  91054. +#ifdef DWC_UTE_CFI
  91055. + }
  91056. +#endif
  91057. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  91058. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  91059. + DWC_PRINTF("There are no more ISOC requests \n");
  91060. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  91061. + }
  91062. +}
  91063. +
  91064. +/**
  91065. + * This function handles the SOF Interrupts. At this time the SOF
  91066. + * Interrupt is disabled.
  91067. + */
  91068. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  91069. +{
  91070. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91071. +
  91072. + gintsts_data_t gintsts;
  91073. +
  91074. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  91075. +
  91076. + /* Clear interrupt */
  91077. + gintsts.d32 = 0;
  91078. + gintsts.b.sofintr = 1;
  91079. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  91080. +
  91081. + return 1;
  91082. +}
  91083. +
  91084. +/**
  91085. + * This function handles the Rx Status Queue Level Interrupt, which
  91086. + * indicates that there is a least one packet in the Rx FIFO. The
  91087. + * packets are moved from the FIFO to memory, where they will be
  91088. + * processed when the Endpoint Interrupt Register indicates Transfer
  91089. + * Complete or SETUP Phase Done.
  91090. + *
  91091. + * Repeat the following until the Rx Status Queue is empty:
  91092. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  91093. + * info
  91094. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  91095. + * and exit
  91096. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  91097. + * SETUP data to the buffer
  91098. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  91099. + * to the destination buffer
  91100. + */
  91101. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  91102. +{
  91103. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91104. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  91105. + gintmsk_data_t gintmask = {.d32 = 0 };
  91106. + device_grxsts_data_t status;
  91107. + dwc_otg_pcd_ep_t *ep;
  91108. + gintsts_data_t gintsts;
  91109. +#ifdef DEBUG
  91110. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  91111. +#endif
  91112. +
  91113. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  91114. + /* Disable the Rx Status Queue Level interrupt */
  91115. + gintmask.b.rxstsqlvl = 1;
  91116. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  91117. +
  91118. + /* Get the Status from the top of the FIFO */
  91119. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  91120. +
  91121. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  91122. + "pktsts:%x Frame:%d(0x%0x)\n",
  91123. + status.b.epnum, status.b.bcnt,
  91124. + dpid_str[status.b.dpid],
  91125. + status.b.pktsts, status.b.fn, status.b.fn);
  91126. + /* Get pointer to EP structure */
  91127. + ep = get_out_ep(pcd, status.b.epnum);
  91128. +
  91129. + switch (status.b.pktsts) {
  91130. + case DWC_DSTS_GOUT_NAK:
  91131. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  91132. + break;
  91133. + case DWC_STS_DATA_UPDT:
  91134. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  91135. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  91136. + /** @todo NGS Check for buffer overflow? */
  91137. + dwc_otg_read_packet(core_if,
  91138. + ep->dwc_ep.xfer_buff,
  91139. + status.b.bcnt);
  91140. + ep->dwc_ep.xfer_count += status.b.bcnt;
  91141. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  91142. + }
  91143. + break;
  91144. + case DWC_STS_XFER_COMP:
  91145. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  91146. + break;
  91147. + case DWC_DSTS_SETUP_COMP:
  91148. +#ifdef DEBUG_EP0
  91149. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  91150. +#endif
  91151. + break;
  91152. + case DWC_DSTS_SETUP_UPDT:
  91153. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  91154. +#ifdef DEBUG_EP0
  91155. + DWC_DEBUGPL(DBG_PCD,
  91156. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  91157. + pcd->setup_pkt->req.bmRequestType,
  91158. + pcd->setup_pkt->req.bRequest,
  91159. + UGETW(pcd->setup_pkt->req.wValue),
  91160. + UGETW(pcd->setup_pkt->req.wIndex),
  91161. + UGETW(pcd->setup_pkt->req.wLength));
  91162. +#endif
  91163. + ep->dwc_ep.xfer_count += status.b.bcnt;
  91164. + break;
  91165. + default:
  91166. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  91167. + status.b.pktsts);
  91168. + break;
  91169. + }
  91170. +
  91171. + /* Enable the Rx Status Queue Level interrupt */
  91172. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  91173. + /* Clear interrupt */
  91174. + gintsts.d32 = 0;
  91175. + gintsts.b.rxstsqlvl = 1;
  91176. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  91177. +
  91178. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  91179. + return 1;
  91180. +}
  91181. +
  91182. +/**
  91183. + * This function examines the Device IN Token Learning Queue to
  91184. + * determine the EP number of the last IN token received. This
  91185. + * implementation is for the Mass Storage device where there are only
  91186. + * 2 IN EPs (Control-IN and BULK-IN).
  91187. + *
  91188. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  91189. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  91190. + *
  91191. + * @param core_if Programming view of DWC_otg controller.
  91192. + *
  91193. + */
  91194. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  91195. +{
  91196. + dwc_otg_device_global_regs_t *dev_global_regs =
  91197. + core_if->dev_if->dev_global_regs;
  91198. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  91199. + /* Number of Token Queue Registers */
  91200. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  91201. + dtknq1_data_t dtknqr1;
  91202. + uint32_t in_tkn_epnums[4];
  91203. + int ndx = 0;
  91204. + int i = 0;
  91205. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  91206. + int epnum = 0;
  91207. +
  91208. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  91209. +
  91210. + /* Read the DTKNQ Registers */
  91211. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  91212. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  91213. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  91214. + in_tkn_epnums[i]);
  91215. + if (addr == &dev_global_regs->dvbusdis) {
  91216. + addr = &dev_global_regs->dtknqr3_dthrctl;
  91217. + } else {
  91218. + ++addr;
  91219. + }
  91220. +
  91221. + }
  91222. +
  91223. + /* Copy the DTKNQR1 data to the bit field. */
  91224. + dtknqr1.d32 = in_tkn_epnums[0];
  91225. + /* Get the EP numbers */
  91226. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  91227. + ndx = dtknqr1.b.intknwptr - 1;
  91228. +
  91229. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  91230. + if (ndx == -1) {
  91231. + /** @todo Find a simpler way to calculate the max
  91232. + * queue position.*/
  91233. + int cnt = TOKEN_Q_DEPTH;
  91234. + if (TOKEN_Q_DEPTH <= 6) {
  91235. + cnt = TOKEN_Q_DEPTH - 1;
  91236. + } else if (TOKEN_Q_DEPTH <= 14) {
  91237. + cnt = TOKEN_Q_DEPTH - 7;
  91238. + } else if (TOKEN_Q_DEPTH <= 22) {
  91239. + cnt = TOKEN_Q_DEPTH - 15;
  91240. + } else {
  91241. + cnt = TOKEN_Q_DEPTH - 23;
  91242. + }
  91243. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  91244. + } else {
  91245. + if (ndx <= 5) {
  91246. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  91247. + } else if (ndx <= 13) {
  91248. + ndx -= 6;
  91249. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  91250. + } else if (ndx <= 21) {
  91251. + ndx -= 14;
  91252. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  91253. + } else if (ndx <= 29) {
  91254. + ndx -= 22;
  91255. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  91256. + }
  91257. + }
  91258. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  91259. + return epnum;
  91260. +}
  91261. +
  91262. +/**
  91263. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  91264. + * The active request is checked for the next packet to be loaded into
  91265. + * the non-periodic Tx FIFO.
  91266. + */
  91267. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  91268. +{
  91269. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91270. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  91271. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  91272. + gnptxsts_data_t txstatus = {.d32 = 0 };
  91273. + gintsts_data_t gintsts;
  91274. +
  91275. + int epnum = 0;
  91276. + dwc_otg_pcd_ep_t *ep = 0;
  91277. + uint32_t len = 0;
  91278. + int dwords;
  91279. +
  91280. + /* Get the epnum from the IN Token Learning Queue. */
  91281. + epnum = get_ep_of_last_in_token(core_if);
  91282. + ep = get_in_ep(pcd, epnum);
  91283. +
  91284. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  91285. +
  91286. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  91287. +
  91288. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  91289. + if (len > ep->dwc_ep.maxpacket) {
  91290. + len = ep->dwc_ep.maxpacket;
  91291. + }
  91292. + dwords = (len + 3) / 4;
  91293. +
  91294. + /* While there is space in the queue and space in the FIFO and
  91295. + * More data to tranfer, Write packets to the Tx FIFO */
  91296. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  91297. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  91298. +
  91299. + while (txstatus.b.nptxqspcavail > 0 &&
  91300. + txstatus.b.nptxfspcavail > dwords &&
  91301. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  91302. + /* Write the FIFO */
  91303. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  91304. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  91305. +
  91306. + if (len > ep->dwc_ep.maxpacket) {
  91307. + len = ep->dwc_ep.maxpacket;
  91308. + }
  91309. +
  91310. + dwords = (len + 3) / 4;
  91311. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  91312. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  91313. + }
  91314. +
  91315. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  91316. + DWC_READ_REG32(&global_regs->gnptxsts));
  91317. +
  91318. + /* Clear interrupt */
  91319. + gintsts.d32 = 0;
  91320. + gintsts.b.nptxfempty = 1;
  91321. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  91322. +
  91323. + return 1;
  91324. +}
  91325. +
  91326. +/**
  91327. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  91328. + * The active request is checked for the next packet to be loaded into
  91329. + * apropriate Tx FIFO.
  91330. + */
  91331. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  91332. +{
  91333. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91334. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  91335. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  91336. + dtxfsts_data_t txstatus = {.d32 = 0 };
  91337. + dwc_otg_pcd_ep_t *ep = 0;
  91338. + uint32_t len = 0;
  91339. + int dwords;
  91340. +
  91341. + ep = get_in_ep(pcd, epnum);
  91342. +
  91343. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  91344. +
  91345. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  91346. +
  91347. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  91348. +
  91349. + if (len > ep->dwc_ep.maxpacket) {
  91350. + len = ep->dwc_ep.maxpacket;
  91351. + }
  91352. +
  91353. + dwords = (len + 3) / 4;
  91354. +
  91355. + /* While there is space in the queue and space in the FIFO and
  91356. + * More data to tranfer, Write packets to the Tx FIFO */
  91357. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  91358. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  91359. +
  91360. + while (txstatus.b.txfspcavail > dwords &&
  91361. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  91362. + ep->dwc_ep.xfer_len != 0) {
  91363. + /* Write the FIFO */
  91364. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  91365. +
  91366. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  91367. + if (len > ep->dwc_ep.maxpacket) {
  91368. + len = ep->dwc_ep.maxpacket;
  91369. + }
  91370. +
  91371. + dwords = (len + 3) / 4;
  91372. + txstatus.d32 =
  91373. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  91374. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  91375. + txstatus.d32);
  91376. + }
  91377. +
  91378. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  91379. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  91380. +
  91381. + return 1;
  91382. +}
  91383. +
  91384. +/**
  91385. + * This function is called when the Device is disconnected. It stops
  91386. + * any active requests and informs the Gadget driver of the
  91387. + * disconnect.
  91388. + */
  91389. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  91390. +{
  91391. + int i, num_in_eps, num_out_eps;
  91392. + dwc_otg_pcd_ep_t *ep;
  91393. +
  91394. + gintmsk_data_t intr_mask = {.d32 = 0 };
  91395. +
  91396. + DWC_SPINLOCK(pcd->lock);
  91397. +
  91398. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  91399. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  91400. +
  91401. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  91402. + /* don't disconnect drivers more than once */
  91403. + if (pcd->ep0state == EP0_DISCONNECT) {
  91404. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  91405. + DWC_SPINUNLOCK(pcd->lock);
  91406. + return;
  91407. + }
  91408. + pcd->ep0state = EP0_DISCONNECT;
  91409. +
  91410. + /* Reset the OTG state. */
  91411. + dwc_otg_pcd_update_otg(pcd, 1);
  91412. +
  91413. + /* Disable the NP Tx Fifo Empty Interrupt. */
  91414. + intr_mask.b.nptxfempty = 1;
  91415. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  91416. + intr_mask.d32, 0);
  91417. +
  91418. + /* Flush the FIFOs */
  91419. + /**@todo NGS Flush Periodic FIFOs */
  91420. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  91421. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  91422. +
  91423. + /* prevent new request submissions, kill any outstanding requests */
  91424. + ep = &pcd->ep0;
  91425. + dwc_otg_request_nuke(ep);
  91426. + /* prevent new request submissions, kill any outstanding requests */
  91427. + for (i = 0; i < num_in_eps; i++) {
  91428. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  91429. + dwc_otg_request_nuke(ep);
  91430. + }
  91431. + /* prevent new request submissions, kill any outstanding requests */
  91432. + for (i = 0; i < num_out_eps; i++) {
  91433. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  91434. + dwc_otg_request_nuke(ep);
  91435. + }
  91436. +
  91437. + /* report disconnect; the driver is already quiesced */
  91438. + if (pcd->fops->disconnect) {
  91439. + DWC_SPINUNLOCK(pcd->lock);
  91440. + pcd->fops->disconnect(pcd);
  91441. + DWC_SPINLOCK(pcd->lock);
  91442. + }
  91443. + DWC_SPINUNLOCK(pcd->lock);
  91444. +}
  91445. +
  91446. +/**
  91447. + * This interrupt indicates that ...
  91448. + */
  91449. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  91450. +{
  91451. + gintmsk_data_t intr_mask = {.d32 = 0 };
  91452. + gintsts_data_t gintsts;
  91453. +
  91454. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  91455. + intr_mask.b.i2cintr = 1;
  91456. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  91457. + intr_mask.d32, 0);
  91458. +
  91459. + /* Clear interrupt */
  91460. + gintsts.d32 = 0;
  91461. + gintsts.b.i2cintr = 1;
  91462. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  91463. + gintsts.d32);
  91464. + return 1;
  91465. +}
  91466. +
  91467. +/**
  91468. + * This interrupt indicates that ...
  91469. + */
  91470. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  91471. +{
  91472. + gintsts_data_t gintsts;
  91473. +#if defined(VERBOSE)
  91474. + DWC_PRINTF("Early Suspend Detected\n");
  91475. +#endif
  91476. +
  91477. + /* Clear interrupt */
  91478. + gintsts.d32 = 0;
  91479. + gintsts.b.erlysuspend = 1;
  91480. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  91481. + gintsts.d32);
  91482. + return 1;
  91483. +}
  91484. +
  91485. +/**
  91486. + * This function configures EPO to receive SETUP packets.
  91487. + *
  91488. + * @todo NGS: Update the comments from the HW FS.
  91489. + *
  91490. + * -# Program the following fields in the endpoint specific registers
  91491. + * for Control OUT EP 0, in order to receive a setup packet
  91492. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  91493. + * setup packets)
  91494. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  91495. + * to back setup packets)
  91496. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  91497. + * store any setup packets received
  91498. + *
  91499. + * @param core_if Programming view of DWC_otg controller.
  91500. + * @param pcd Programming view of the PCD.
  91501. + */
  91502. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  91503. + dwc_otg_pcd_t * pcd)
  91504. +{
  91505. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  91506. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  91507. + dwc_otg_dev_dma_desc_t *dma_desc;
  91508. + depctl_data_t doepctl = {.d32 = 0 };
  91509. +
  91510. +#ifdef VERBOSE
  91511. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  91512. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  91513. +#endif
  91514. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  91515. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  91516. + if (doepctl.b.epena) {
  91517. + return;
  91518. + }
  91519. + }
  91520. +
  91521. + doeptsize0.b.supcnt = 3;
  91522. + doeptsize0.b.pktcnt = 1;
  91523. + doeptsize0.b.xfersize = 8 * 3;
  91524. +
  91525. + if (core_if->dma_enable) {
  91526. + if (!core_if->dma_desc_enable) {
  91527. + /** put here as for Hermes mode deptisz register should not be written */
  91528. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  91529. + doeptsize0.d32);
  91530. +
  91531. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  91532. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  91533. + pcd->setup_pkt_dma_handle);
  91534. + } else {
  91535. + dev_if->setup_desc_index =
  91536. + (dev_if->setup_desc_index + 1) & 1;
  91537. + dma_desc =
  91538. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  91539. +
  91540. + /** DMA Descriptor Setup */
  91541. + dma_desc->status.b.bs = BS_HOST_BUSY;
  91542. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  91543. + dma_desc->status.b.sr = 0;
  91544. + dma_desc->status.b.mtrf = 0;
  91545. + }
  91546. + dma_desc->status.b.l = 1;
  91547. + dma_desc->status.b.ioc = 1;
  91548. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  91549. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  91550. + dma_desc->status.b.sts = 0;
  91551. + dma_desc->status.b.bs = BS_HOST_READY;
  91552. +
  91553. + /** DOEPDMA0 Register write */
  91554. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  91555. + dev_if->dma_setup_desc_addr
  91556. + [dev_if->setup_desc_index]);
  91557. + }
  91558. +
  91559. + } else {
  91560. + /** put here as for Hermes mode deptisz register should not be written */
  91561. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  91562. + doeptsize0.d32);
  91563. + }
  91564. +
  91565. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  91566. + doepctl.d32 = 0;
  91567. + doepctl.b.epena = 1;
  91568. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  91569. + doepctl.b.cnak = 1;
  91570. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  91571. + } else {
  91572. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  91573. + }
  91574. +
  91575. +#ifdef VERBOSE
  91576. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  91577. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  91578. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  91579. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  91580. +#endif
  91581. +}
  91582. +
  91583. +/**
  91584. + * This interrupt occurs when a USB Reset is detected. When the USB
  91585. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  91586. + * EP0 state is set to IDLE.
  91587. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  91588. + * -# Unmask the following interrupt bits
  91589. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  91590. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  91591. + * - DOEPMSK.SETUP = 1
  91592. + * - DOEPMSK.XferCompl = 1
  91593. + * - DIEPMSK.XferCompl = 1
  91594. + * - DIEPMSK.TimeOut = 1
  91595. + * -# Program the following fields in the endpoint specific registers
  91596. + * for Control OUT EP 0, in order to receive a setup packet
  91597. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  91598. + * setup packets)
  91599. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  91600. + * to back setup packets)
  91601. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  91602. + * store any setup packets received
  91603. + * At this point, all the required initialization, except for enabling
  91604. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  91605. + */
  91606. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  91607. +{
  91608. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91609. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  91610. + depctl_data_t doepctl = {.d32 = 0 };
  91611. + depctl_data_t diepctl = {.d32 = 0 };
  91612. + daint_data_t daintmsk = {.d32 = 0 };
  91613. + doepmsk_data_t doepmsk = {.d32 = 0 };
  91614. + diepmsk_data_t diepmsk = {.d32 = 0 };
  91615. + dcfg_data_t dcfg = {.d32 = 0 };
  91616. + grstctl_t resetctl = {.d32 = 0 };
  91617. + dctl_data_t dctl = {.d32 = 0 };
  91618. + int i = 0;
  91619. + gintsts_data_t gintsts;
  91620. + pcgcctl_data_t power = {.d32 = 0 };
  91621. +
  91622. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  91623. + if (power.b.stoppclk) {
  91624. + power.d32 = 0;
  91625. + power.b.stoppclk = 1;
  91626. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  91627. +
  91628. + power.b.pwrclmp = 1;
  91629. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  91630. +
  91631. + power.b.rstpdwnmodule = 1;
  91632. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  91633. + }
  91634. +
  91635. + core_if->lx_state = DWC_OTG_L0;
  91636. +
  91637. + DWC_PRINTF("USB RESET\n");
  91638. +#ifdef DWC_EN_ISOC
  91639. + for (i = 1; i < 16; ++i) {
  91640. + dwc_otg_pcd_ep_t *ep;
  91641. + dwc_ep_t *dwc_ep;
  91642. + ep = get_in_ep(pcd, i);
  91643. + if (ep != 0) {
  91644. + dwc_ep = &ep->dwc_ep;
  91645. + dwc_ep->next_frame = 0xffffffff;
  91646. + }
  91647. + }
  91648. +#endif /* DWC_EN_ISOC */
  91649. +
  91650. + /* reset the HNP settings */
  91651. + dwc_otg_pcd_update_otg(pcd, 1);
  91652. +
  91653. + /* Clear the Remote Wakeup Signalling */
  91654. + dctl.b.rmtwkupsig = 1;
  91655. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  91656. +
  91657. + /* Set NAK for all OUT EPs */
  91658. + doepctl.b.snak = 1;
  91659. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  91660. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  91661. + }
  91662. +
  91663. + /* Flush the NP Tx FIFO */
  91664. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  91665. + /* Flush the Learning Queue */
  91666. + resetctl.b.intknqflsh = 1;
  91667. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  91668. +
  91669. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  91670. + core_if->start_predict = 0;
  91671. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  91672. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  91673. + }
  91674. + core_if->nextep_seq[0] = 0;
  91675. + core_if->first_in_nextep_seq = 0;
  91676. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  91677. + diepctl.b.nextep = 0;
  91678. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  91679. +
  91680. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  91681. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  91682. + dcfg.b.epmscnt = 2;
  91683. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  91684. +
  91685. + DWC_DEBUGPL(DBG_PCDV,
  91686. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  91687. + __func__, core_if->first_in_nextep_seq);
  91688. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  91689. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  91690. + }
  91691. + }
  91692. +
  91693. + if (core_if->multiproc_int_enable) {
  91694. + daintmsk.b.inep0 = 1;
  91695. + daintmsk.b.outep0 = 1;
  91696. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  91697. + daintmsk.d32);
  91698. +
  91699. + doepmsk.b.setup = 1;
  91700. + doepmsk.b.xfercompl = 1;
  91701. + doepmsk.b.ahberr = 1;
  91702. + doepmsk.b.epdisabled = 1;
  91703. +
  91704. + if ((core_if->dma_desc_enable) ||
  91705. + (core_if->dma_enable
  91706. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  91707. + doepmsk.b.stsphsercvd = 1;
  91708. + }
  91709. + if (core_if->dma_desc_enable)
  91710. + doepmsk.b.bna = 1;
  91711. +/*
  91712. + doepmsk.b.babble = 1;
  91713. + doepmsk.b.nyet = 1;
  91714. +
  91715. + if (core_if->dma_enable) {
  91716. + doepmsk.b.nak = 1;
  91717. + }
  91718. +*/
  91719. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  91720. + doepmsk.d32);
  91721. +
  91722. + diepmsk.b.xfercompl = 1;
  91723. + diepmsk.b.timeout = 1;
  91724. + diepmsk.b.epdisabled = 1;
  91725. + diepmsk.b.ahberr = 1;
  91726. + diepmsk.b.intknepmis = 1;
  91727. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  91728. + diepmsk.b.intknepmis = 0;
  91729. +
  91730. +/* if (core_if->dma_desc_enable) {
  91731. + diepmsk.b.bna = 1;
  91732. + }
  91733. +*/
  91734. +/*
  91735. + if (core_if->dma_enable) {
  91736. + diepmsk.b.nak = 1;
  91737. + }
  91738. +*/
  91739. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  91740. + diepmsk.d32);
  91741. + } else {
  91742. + daintmsk.b.inep0 = 1;
  91743. + daintmsk.b.outep0 = 1;
  91744. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  91745. + daintmsk.d32);
  91746. +
  91747. + doepmsk.b.setup = 1;
  91748. + doepmsk.b.xfercompl = 1;
  91749. + doepmsk.b.ahberr = 1;
  91750. + doepmsk.b.epdisabled = 1;
  91751. +
  91752. + if ((core_if->dma_desc_enable) ||
  91753. + (core_if->dma_enable
  91754. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  91755. + doepmsk.b.stsphsercvd = 1;
  91756. + }
  91757. + if (core_if->dma_desc_enable)
  91758. + doepmsk.b.bna = 1;
  91759. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  91760. +
  91761. + diepmsk.b.xfercompl = 1;
  91762. + diepmsk.b.timeout = 1;
  91763. + diepmsk.b.epdisabled = 1;
  91764. + diepmsk.b.ahberr = 1;
  91765. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  91766. + diepmsk.b.intknepmis = 0;
  91767. +/*
  91768. + if (core_if->dma_desc_enable) {
  91769. + diepmsk.b.bna = 1;
  91770. + }
  91771. +*/
  91772. +
  91773. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  91774. + }
  91775. +
  91776. + /* Reset Device Address */
  91777. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  91778. + dcfg.b.devaddr = 0;
  91779. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  91780. +
  91781. + /* setup EP0 to receive SETUP packets */
  91782. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  91783. + ep0_out_start(core_if, pcd);
  91784. +
  91785. + /* Clear interrupt */
  91786. + gintsts.d32 = 0;
  91787. + gintsts.b.usbreset = 1;
  91788. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  91789. +
  91790. + return 1;
  91791. +}
  91792. +
  91793. +/**
  91794. + * Get the device speed from the device status register and convert it
  91795. + * to USB speed constant.
  91796. + *
  91797. + * @param core_if Programming view of DWC_otg controller.
  91798. + */
  91799. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  91800. +{
  91801. + dsts_data_t dsts;
  91802. + int speed = 0;
  91803. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  91804. +
  91805. + switch (dsts.b.enumspd) {
  91806. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  91807. + speed = USB_SPEED_HIGH;
  91808. + break;
  91809. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  91810. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  91811. + speed = USB_SPEED_FULL;
  91812. + break;
  91813. +
  91814. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  91815. + speed = USB_SPEED_LOW;
  91816. + break;
  91817. + }
  91818. +
  91819. + return speed;
  91820. +}
  91821. +
  91822. +/**
  91823. + * Read the device status register and set the device speed in the
  91824. + * data structure.
  91825. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  91826. + */
  91827. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  91828. +{
  91829. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  91830. + gintsts_data_t gintsts;
  91831. + gusbcfg_data_t gusbcfg;
  91832. + dwc_otg_core_global_regs_t *global_regs =
  91833. + GET_CORE_IF(pcd)->core_global_regs;
  91834. + uint8_t utmi16b, utmi8b;
  91835. + int speed;
  91836. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  91837. +
  91838. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  91839. + utmi16b = 6; //vahrama old value was 6;
  91840. + utmi8b = 9;
  91841. + } else {
  91842. + utmi16b = 4;
  91843. + utmi8b = 8;
  91844. + }
  91845. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  91846. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  91847. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  91848. + }
  91849. +
  91850. +#ifdef DEBUG_EP0
  91851. + print_ep0_state(pcd);
  91852. +#endif
  91853. +
  91854. + if (pcd->ep0state == EP0_DISCONNECT) {
  91855. + pcd->ep0state = EP0_IDLE;
  91856. + } else if (pcd->ep0state == EP0_STALL) {
  91857. + pcd->ep0state = EP0_IDLE;
  91858. + }
  91859. +
  91860. + pcd->ep0state = EP0_IDLE;
  91861. +
  91862. + ep0->stopped = 0;
  91863. +
  91864. + speed = get_device_speed(GET_CORE_IF(pcd));
  91865. + pcd->fops->connect(pcd, speed);
  91866. +
  91867. + /* Set USB turnaround time based on device speed and PHY interface. */
  91868. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  91869. + if (speed == USB_SPEED_HIGH) {
  91870. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  91871. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  91872. + /* ULPI interface */
  91873. + gusbcfg.b.usbtrdtim = 9;
  91874. + }
  91875. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  91876. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  91877. + /* UTMI+ interface */
  91878. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  91879. + gusbcfg.b.usbtrdtim = utmi8b;
  91880. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  91881. + b.utmi_phy_data_width == 1) {
  91882. + gusbcfg.b.usbtrdtim = utmi16b;
  91883. + } else if (GET_CORE_IF(pcd)->
  91884. + core_params->phy_utmi_width == 8) {
  91885. + gusbcfg.b.usbtrdtim = utmi8b;
  91886. + } else {
  91887. + gusbcfg.b.usbtrdtim = utmi16b;
  91888. + }
  91889. + }
  91890. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  91891. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  91892. + /* UTMI+ OR ULPI interface */
  91893. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  91894. + /* ULPI interface */
  91895. + gusbcfg.b.usbtrdtim = 9;
  91896. + } else {
  91897. + /* UTMI+ interface */
  91898. + if (GET_CORE_IF(pcd)->
  91899. + core_params->phy_utmi_width == 16) {
  91900. + gusbcfg.b.usbtrdtim = utmi16b;
  91901. + } else {
  91902. + gusbcfg.b.usbtrdtim = utmi8b;
  91903. + }
  91904. + }
  91905. + }
  91906. + } else {
  91907. + /* Full or low speed */
  91908. + gusbcfg.b.usbtrdtim = 9;
  91909. + }
  91910. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  91911. +
  91912. + /* Clear interrupt */
  91913. + gintsts.d32 = 0;
  91914. + gintsts.b.enumdone = 1;
  91915. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  91916. + gintsts.d32);
  91917. + return 1;
  91918. +}
  91919. +
  91920. +/**
  91921. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  91922. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  91923. + * read all the data from the Rx FIFO.
  91924. + */
  91925. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  91926. +{
  91927. + gintmsk_data_t intr_mask = {.d32 = 0 };
  91928. + gintsts_data_t gintsts;
  91929. +
  91930. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  91931. + "ISOC Out Dropped");
  91932. +
  91933. + intr_mask.b.isooutdrop = 1;
  91934. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  91935. + intr_mask.d32, 0);
  91936. +
  91937. + /* Clear interrupt */
  91938. + gintsts.d32 = 0;
  91939. + gintsts.b.isooutdrop = 1;
  91940. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  91941. + gintsts.d32);
  91942. +
  91943. + return 1;
  91944. +}
  91945. +
  91946. +/**
  91947. + * This interrupt indicates the end of the portion of the micro-frame
  91948. + * for periodic transactions. If there is a periodic transaction for
  91949. + * the next frame, load the packets into the EP periodic Tx FIFO.
  91950. + */
  91951. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  91952. +{
  91953. + gintmsk_data_t intr_mask = {.d32 = 0 };
  91954. + gintsts_data_t gintsts;
  91955. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  91956. +
  91957. + intr_mask.b.eopframe = 1;
  91958. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  91959. + intr_mask.d32, 0);
  91960. +
  91961. + /* Clear interrupt */
  91962. + gintsts.d32 = 0;
  91963. + gintsts.b.eopframe = 1;
  91964. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  91965. + gintsts.d32);
  91966. +
  91967. + return 1;
  91968. +}
  91969. +
  91970. +/**
  91971. + * This interrupt indicates that EP of the packet on the top of the
  91972. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  91973. + *
  91974. + * The "Device IN Token Queue" Registers are read to determine the
  91975. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  91976. + * is flushed, so it can be reloaded in the order seen in the IN Token
  91977. + * Queue.
  91978. + */
  91979. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  91980. +{
  91981. + gintsts_data_t gintsts;
  91982. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91983. + dctl_data_t dctl;
  91984. + gintmsk_data_t intr_mask = {.d32 = 0 };
  91985. +
  91986. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  91987. + core_if->start_predict = 1;
  91988. +
  91989. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  91990. +
  91991. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  91992. + if (!gintsts.b.ginnakeff) {
  91993. + /* Disable EP Mismatch interrupt */
  91994. + intr_mask.d32 = 0;
  91995. + intr_mask.b.epmismatch = 1;
  91996. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  91997. + /* Enable the Global IN NAK Effective Interrupt */
  91998. + intr_mask.d32 = 0;
  91999. + intr_mask.b.ginnakeff = 1;
  92000. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  92001. + /* Set the global non-periodic IN NAK handshake */
  92002. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  92003. + dctl.b.sgnpinnak = 1;
  92004. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  92005. + } else {
  92006. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  92007. + }
  92008. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  92009. + * handler after Global IN NAK Effective interrupt will be asserted */
  92010. + }
  92011. + /* Clear interrupt */
  92012. + gintsts.d32 = 0;
  92013. + gintsts.b.epmismatch = 1;
  92014. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  92015. +
  92016. + return 1;
  92017. +}
  92018. +
  92019. +/**
  92020. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  92021. + * core has stopped fetching data for IN endpoints due to the unavailability of
  92022. + * TxFIFO space or Request Queue space. This interrupt is used by the
  92023. + * application for an endpoint mismatch algorithm.
  92024. + *
  92025. + * @param pcd The PCD
  92026. + */
  92027. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  92028. +{
  92029. + gintsts_data_t gintsts;
  92030. + gintmsk_data_t gintmsk_data;
  92031. + dctl_data_t dctl;
  92032. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92033. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  92034. +
  92035. + /* Clear the global non-periodic IN NAK handshake */
  92036. + dctl.d32 = 0;
  92037. + dctl.b.cgnpinnak = 1;
  92038. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  92039. +
  92040. + /* Mask GINTSTS.FETSUSP interrupt */
  92041. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  92042. + gintmsk_data.b.fetsusp = 0;
  92043. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  92044. +
  92045. + /* Clear interrupt */
  92046. + gintsts.d32 = 0;
  92047. + gintsts.b.fetsusp = 1;
  92048. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  92049. +
  92050. + return 1;
  92051. +}
  92052. +/**
  92053. + * This funcion stalls EP0.
  92054. + */
  92055. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  92056. +{
  92057. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92058. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  92059. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  92060. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  92061. +
  92062. + ep0->dwc_ep.is_in = 1;
  92063. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  92064. + pcd->ep0.stopped = 1;
  92065. + pcd->ep0state = EP0_IDLE;
  92066. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  92067. +}
  92068. +
  92069. +/**
  92070. + * This functions delegates the setup command to the gadget driver.
  92071. + */
  92072. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  92073. + usb_device_request_t * ctrl)
  92074. +{
  92075. + int ret = 0;
  92076. + DWC_SPINUNLOCK(pcd->lock);
  92077. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  92078. + DWC_SPINLOCK(pcd->lock);
  92079. + if (ret < 0) {
  92080. + ep0_do_stall(pcd, ret);
  92081. + }
  92082. +
  92083. + /** @todo This is a g_file_storage gadget driver specific
  92084. + * workaround: a DELAYED_STATUS result from the fsg_setup
  92085. + * routine will result in the gadget queueing a EP0 IN status
  92086. + * phase for a two-stage control transfer. Exactly the same as
  92087. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  92088. + * specific request. Need a generic way to know when the gadget
  92089. + * driver will queue the status phase. Can we assume when we
  92090. + * call the gadget driver setup() function that it will always
  92091. + * queue and require the following flag? Need to look into
  92092. + * this.
  92093. + */
  92094. +
  92095. + if (ret == 256 + 999) {
  92096. + pcd->request_config = 1;
  92097. + }
  92098. +}
  92099. +
  92100. +#ifdef DWC_UTE_CFI
  92101. +/**
  92102. + * This functions delegates the CFI setup commands to the gadget driver.
  92103. + * This function will return a negative value to indicate a failure.
  92104. + */
  92105. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  92106. + struct cfi_usb_ctrlrequest *ctrl_req)
  92107. +{
  92108. + int ret = 0;
  92109. +
  92110. + if (pcd->fops && pcd->fops->cfi_setup) {
  92111. + DWC_SPINUNLOCK(pcd->lock);
  92112. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  92113. + DWC_SPINLOCK(pcd->lock);
  92114. + if (ret < 0) {
  92115. + ep0_do_stall(pcd, ret);
  92116. + return ret;
  92117. + }
  92118. + }
  92119. +
  92120. + return ret;
  92121. +}
  92122. +#endif
  92123. +
  92124. +/**
  92125. + * This function starts the Zero-Length Packet for the IN status phase
  92126. + * of a 2 stage control transfer.
  92127. + */
  92128. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  92129. +{
  92130. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92131. + if (pcd->ep0state == EP0_STALL) {
  92132. + return;
  92133. + }
  92134. +
  92135. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  92136. +
  92137. + /* Prepare for more SETUP Packets */
  92138. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  92139. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  92140. + && (pcd->core_if->dma_desc_enable)
  92141. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  92142. + DWC_DEBUGPL(DBG_PCDV,
  92143. + "Data terminated wait next packet in out_desc_addr\n");
  92144. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  92145. + pcd->data_terminated = 1;
  92146. + }
  92147. + ep0->dwc_ep.xfer_len = 0;
  92148. + ep0->dwc_ep.xfer_count = 0;
  92149. + ep0->dwc_ep.is_in = 1;
  92150. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  92151. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  92152. +
  92153. + /* Prepare for more SETUP Packets */
  92154. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  92155. +}
  92156. +
  92157. +/**
  92158. + * This function starts the Zero-Length Packet for the OUT status phase
  92159. + * of a 2 stage control transfer.
  92160. + */
  92161. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  92162. +{
  92163. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92164. + if (pcd->ep0state == EP0_STALL) {
  92165. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  92166. + return;
  92167. + }
  92168. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  92169. +
  92170. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  92171. + ep0->dwc_ep.xfer_len = 0;
  92172. + ep0->dwc_ep.xfer_count = 0;
  92173. + ep0->dwc_ep.is_in = 0;
  92174. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  92175. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  92176. +
  92177. + /* Prepare for more SETUP Packets */
  92178. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  92179. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  92180. + }
  92181. +}
  92182. +
  92183. +/**
  92184. + * Clear the EP halt (STALL) and if pending requests start the
  92185. + * transfer.
  92186. + */
  92187. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  92188. +{
  92189. + if (ep->dwc_ep.stall_clear_flag == 0)
  92190. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  92191. +
  92192. + /* Reactive the EP */
  92193. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  92194. + if (ep->stopped) {
  92195. + ep->stopped = 0;
  92196. + /* If there is a request in the EP queue start it */
  92197. +
  92198. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  92199. + * epmismatch not yet implemented. */
  92200. +
  92201. + /*
  92202. + * Above fixme is solved by implmenting a tasklet to call the
  92203. + * start_next_request(), outside of interrupt context at some
  92204. + * time after the current time, after a clear-halt setup packet.
  92205. + * Still need to implement ep mismatch in the future if a gadget
  92206. + * ever uses more than one endpoint at once
  92207. + */
  92208. + ep->queue_sof = 1;
  92209. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  92210. + }
  92211. + /* Start Control Status Phase */
  92212. + do_setup_in_status_phase(pcd);
  92213. +}
  92214. +
  92215. +/**
  92216. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  92217. + * is sent from the host. The Device Control register is written with
  92218. + * the Test Mode bits set to the specified Test Mode. This is done as
  92219. + * a tasklet so that the "Status" phase of the control transfer
  92220. + * completes before transmitting the TEST packets.
  92221. + *
  92222. + * @todo This has not been tested since the tasklet struct was put
  92223. + * into the PCD struct!
  92224. + *
  92225. + */
  92226. +void do_test_mode(void *data)
  92227. +{
  92228. + dctl_data_t dctl;
  92229. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  92230. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92231. + int test_mode = pcd->test_mode;
  92232. +
  92233. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  92234. +
  92235. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  92236. + switch (test_mode) {
  92237. + case 1: // TEST_J
  92238. + dctl.b.tstctl = 1;
  92239. + break;
  92240. +
  92241. + case 2: // TEST_K
  92242. + dctl.b.tstctl = 2;
  92243. + break;
  92244. +
  92245. + case 3: // TEST_SE0_NAK
  92246. + dctl.b.tstctl = 3;
  92247. + break;
  92248. +
  92249. + case 4: // TEST_PACKET
  92250. + dctl.b.tstctl = 4;
  92251. + break;
  92252. +
  92253. + case 5: // TEST_FORCE_ENABLE
  92254. + dctl.b.tstctl = 5;
  92255. + break;
  92256. + }
  92257. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  92258. +}
  92259. +
  92260. +/**
  92261. + * This function process the GET_STATUS Setup Commands.
  92262. + */
  92263. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  92264. +{
  92265. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  92266. + dwc_otg_pcd_ep_t *ep;
  92267. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92268. + uint16_t *status = pcd->status_buf;
  92269. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92270. +
  92271. +#ifdef DEBUG_EP0
  92272. + DWC_DEBUGPL(DBG_PCD,
  92273. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  92274. + ctrl.bmRequestType, ctrl.bRequest,
  92275. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  92276. + UGETW(ctrl.wLength));
  92277. +#endif
  92278. +
  92279. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  92280. + case UT_DEVICE:
  92281. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  92282. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  92283. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  92284. + DWC_PRINTF("OTG CAP - %d, %d\n",
  92285. + core_if->core_params->otg_cap,
  92286. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  92287. + if (core_if->otg_ver == 1
  92288. + && core_if->core_params->otg_cap ==
  92289. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  92290. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  92291. + *otgsts = (core_if->otg_sts & 0x1);
  92292. + pcd->ep0_pending = 1;
  92293. + ep0->dwc_ep.start_xfer_buff =
  92294. + (uint8_t *) otgsts;
  92295. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  92296. + ep0->dwc_ep.dma_addr =
  92297. + pcd->status_buf_dma_handle;
  92298. + ep0->dwc_ep.xfer_len = 1;
  92299. + ep0->dwc_ep.xfer_count = 0;
  92300. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  92301. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  92302. + &ep0->dwc_ep);
  92303. + return;
  92304. + } else {
  92305. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92306. + return;
  92307. + }
  92308. + break;
  92309. + } else {
  92310. + *status = 0x1; /* Self powered */
  92311. + *status |= pcd->remote_wakeup_enable << 1;
  92312. + break;
  92313. + }
  92314. + case UT_INTERFACE:
  92315. + *status = 0;
  92316. + break;
  92317. +
  92318. + case UT_ENDPOINT:
  92319. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  92320. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  92321. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92322. + return;
  92323. + }
  92324. + /** @todo check for EP stall */
  92325. + *status = ep->stopped;
  92326. + break;
  92327. + }
  92328. + pcd->ep0_pending = 1;
  92329. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  92330. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  92331. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  92332. + ep0->dwc_ep.xfer_len = 2;
  92333. + ep0->dwc_ep.xfer_count = 0;
  92334. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  92335. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  92336. +}
  92337. +
  92338. +/**
  92339. + * This function process the SET_FEATURE Setup Commands.
  92340. + */
  92341. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  92342. +{
  92343. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92344. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  92345. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  92346. + dwc_otg_pcd_ep_t *ep = 0;
  92347. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  92348. + gotgctl_data_t gotgctl = {.d32 = 0 };
  92349. +
  92350. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  92351. + ctrl.bmRequestType, ctrl.bRequest,
  92352. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  92353. + UGETW(ctrl.wLength));
  92354. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  92355. +
  92356. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  92357. + case UT_DEVICE:
  92358. + switch (UGETW(ctrl.wValue)) {
  92359. + case UF_DEVICE_REMOTE_WAKEUP:
  92360. + pcd->remote_wakeup_enable = 1;
  92361. + break;
  92362. +
  92363. + case UF_TEST_MODE:
  92364. + /* Setup the Test Mode tasklet to do the Test
  92365. + * Packet generation after the SETUP Status
  92366. + * phase has completed. */
  92367. +
  92368. + /** @todo This has not been tested since the
  92369. + * tasklet struct was put into the PCD
  92370. + * struct! */
  92371. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  92372. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  92373. + break;
  92374. +
  92375. + case UF_DEVICE_B_HNP_ENABLE:
  92376. + DWC_DEBUGPL(DBG_PCDV,
  92377. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  92378. +
  92379. + /* dev may initiate HNP */
  92380. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  92381. + pcd->b_hnp_enable = 1;
  92382. + dwc_otg_pcd_update_otg(pcd, 0);
  92383. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  92384. + /**@todo Is the gotgctl.devhnpen cleared
  92385. + * by a USB Reset? */
  92386. + gotgctl.b.devhnpen = 1;
  92387. + gotgctl.b.hnpreq = 1;
  92388. + DWC_WRITE_REG32(&global_regs->gotgctl,
  92389. + gotgctl.d32);
  92390. + } else {
  92391. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92392. + return;
  92393. + }
  92394. + break;
  92395. +
  92396. + case UF_DEVICE_A_HNP_SUPPORT:
  92397. + /* RH port supports HNP */
  92398. + DWC_DEBUGPL(DBG_PCDV,
  92399. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  92400. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  92401. + pcd->a_hnp_support = 1;
  92402. + dwc_otg_pcd_update_otg(pcd, 0);
  92403. + } else {
  92404. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92405. + return;
  92406. + }
  92407. + break;
  92408. +
  92409. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  92410. + /* other RH port does */
  92411. + DWC_DEBUGPL(DBG_PCDV,
  92412. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  92413. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  92414. + pcd->a_alt_hnp_support = 1;
  92415. + dwc_otg_pcd_update_otg(pcd, 0);
  92416. + } else {
  92417. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92418. + return;
  92419. + }
  92420. + break;
  92421. +
  92422. + default:
  92423. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92424. + return;
  92425. +
  92426. + }
  92427. + do_setup_in_status_phase(pcd);
  92428. + break;
  92429. +
  92430. + case UT_INTERFACE:
  92431. + do_gadget_setup(pcd, &ctrl);
  92432. + break;
  92433. +
  92434. + case UT_ENDPOINT:
  92435. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  92436. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  92437. + if (ep == 0) {
  92438. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92439. + return;
  92440. + }
  92441. + ep->stopped = 1;
  92442. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  92443. + }
  92444. + do_setup_in_status_phase(pcd);
  92445. + break;
  92446. + }
  92447. +}
  92448. +
  92449. +/**
  92450. + * This function process the CLEAR_FEATURE Setup Commands.
  92451. + */
  92452. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  92453. +{
  92454. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  92455. + dwc_otg_pcd_ep_t *ep = 0;
  92456. +
  92457. + DWC_DEBUGPL(DBG_PCD,
  92458. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  92459. + ctrl.bmRequestType, ctrl.bRequest,
  92460. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  92461. + UGETW(ctrl.wLength));
  92462. +
  92463. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  92464. + case UT_DEVICE:
  92465. + switch (UGETW(ctrl.wValue)) {
  92466. + case UF_DEVICE_REMOTE_WAKEUP:
  92467. + pcd->remote_wakeup_enable = 0;
  92468. + break;
  92469. +
  92470. + case UF_TEST_MODE:
  92471. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  92472. + break;
  92473. +
  92474. + default:
  92475. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92476. + return;
  92477. + }
  92478. + do_setup_in_status_phase(pcd);
  92479. + break;
  92480. +
  92481. + case UT_ENDPOINT:
  92482. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  92483. + if (ep == 0) {
  92484. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  92485. + return;
  92486. + }
  92487. +
  92488. + pcd_clear_halt(pcd, ep);
  92489. +
  92490. + break;
  92491. + }
  92492. +}
  92493. +
  92494. +/**
  92495. + * This function process the SET_ADDRESS Setup Commands.
  92496. + */
  92497. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  92498. +{
  92499. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  92500. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  92501. +
  92502. + if (ctrl.bmRequestType == UT_DEVICE) {
  92503. + dcfg_data_t dcfg = {.d32 = 0 };
  92504. +
  92505. +#ifdef DEBUG_EP0
  92506. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  92507. +#endif
  92508. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  92509. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  92510. + do_setup_in_status_phase(pcd);
  92511. + }
  92512. +}
  92513. +
  92514. +/**
  92515. + * This function processes SETUP commands. In Linux, the USB Command
  92516. + * processing is done in two places - the first being the PCD and the
  92517. + * second in the Gadget Driver (for example, the File-Backed Storage
  92518. + * Gadget Driver).
  92519. + *
  92520. + * <table>
  92521. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  92522. + *
  92523. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  92524. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  92525. + * </td></tr>
  92526. + *
  92527. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  92528. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  92529. + * interface requests are ignored.</td></tr>
  92530. + *
  92531. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  92532. + * requests are processed by the PCD. Interface requests are passed
  92533. + * to the Gadget Driver.</td></tr>
  92534. + *
  92535. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  92536. + * with device address received </td></tr>
  92537. + *
  92538. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  92539. + * requested descriptor</td></tr>
  92540. + *
  92541. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  92542. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  92543. + *
  92544. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  92545. + * all EPs and enable EPs for new configuration.</td></tr>
  92546. + *
  92547. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  92548. + * the current configuration</td></tr>
  92549. + *
  92550. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  92551. + * EPs and enable EPs for new configuration.</td></tr>
  92552. + *
  92553. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  92554. + * current interface.</td></tr>
  92555. + *
  92556. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  92557. + * message.</td></tr>
  92558. + * </table>
  92559. + *
  92560. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  92561. + * processed by pcd_setup. Calling the Function Driver's setup function from
  92562. + * pcd_setup processes the gadget SETUP commands.
  92563. + */
  92564. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  92565. +{
  92566. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92567. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  92568. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  92569. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92570. +
  92571. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  92572. +
  92573. +#ifdef DWC_UTE_CFI
  92574. + int retval = 0;
  92575. + struct cfi_usb_ctrlrequest cfi_req;
  92576. +#endif
  92577. +
  92578. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  92579. +
  92580. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  92581. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  92582. + && (doeptsize0.b.supcnt < 2)
  92583. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  92584. + DWC_ERROR
  92585. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  92586. + }
  92587. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  92588. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  92589. + ctrl =
  92590. + (pcd->setup_pkt +
  92591. + (3 - doeptsize0.b.supcnt - 1 +
  92592. + ep0->dwc_ep.stp_rollover))->req;
  92593. + }
  92594. +#ifdef DEBUG_EP0
  92595. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  92596. + ctrl.bmRequestType, ctrl.bRequest,
  92597. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  92598. + UGETW(ctrl.wLength));
  92599. +#endif
  92600. +
  92601. + /* Clean up the request queue */
  92602. + dwc_otg_request_nuke(ep0);
  92603. + ep0->stopped = 0;
  92604. +
  92605. + if (ctrl.bmRequestType & UE_DIR_IN) {
  92606. + ep0->dwc_ep.is_in = 1;
  92607. + pcd->ep0state = EP0_IN_DATA_PHASE;
  92608. + } else {
  92609. + ep0->dwc_ep.is_in = 0;
  92610. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  92611. + }
  92612. +
  92613. + if (UGETW(ctrl.wLength) == 0) {
  92614. + ep0->dwc_ep.is_in = 1;
  92615. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  92616. + }
  92617. +
  92618. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  92619. +
  92620. +#ifdef DWC_UTE_CFI
  92621. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  92622. +
  92623. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  92624. + ctrl.bRequestType, ctrl.bRequest);
  92625. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  92626. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  92627. + retval = cfi_setup(pcd, &cfi_req);
  92628. + if (retval < 0) {
  92629. + ep0_do_stall(pcd, retval);
  92630. + pcd->ep0_pending = 0;
  92631. + return;
  92632. + }
  92633. +
  92634. + /* if need gadget setup then call it and check the retval */
  92635. + if (pcd->cfi->need_gadget_att) {
  92636. + retval =
  92637. + cfi_gadget_setup(pcd,
  92638. + &pcd->
  92639. + cfi->ctrl_req);
  92640. + if (retval < 0) {
  92641. + pcd->ep0_pending = 0;
  92642. + return;
  92643. + }
  92644. + }
  92645. +
  92646. + if (pcd->cfi->need_status_in_complete) {
  92647. + do_setup_in_status_phase(pcd);
  92648. + }
  92649. + return;
  92650. + }
  92651. + }
  92652. +#endif
  92653. +
  92654. + /* handle non-standard (class/vendor) requests in the gadget driver */
  92655. + do_gadget_setup(pcd, &ctrl);
  92656. + return;
  92657. + }
  92658. +
  92659. + /** @todo NGS: Handle bad setup packet? */
  92660. +
  92661. +///////////////////////////////////////////
  92662. +//// --- Standard Request handling --- ////
  92663. +
  92664. + switch (ctrl.bRequest) {
  92665. + case UR_GET_STATUS:
  92666. + do_get_status(pcd);
  92667. + break;
  92668. +
  92669. + case UR_CLEAR_FEATURE:
  92670. + do_clear_feature(pcd);
  92671. + break;
  92672. +
  92673. + case UR_SET_FEATURE:
  92674. + do_set_feature(pcd);
  92675. + break;
  92676. +
  92677. + case UR_SET_ADDRESS:
  92678. + do_set_address(pcd);
  92679. + break;
  92680. +
  92681. + case UR_SET_INTERFACE:
  92682. + case UR_SET_CONFIG:
  92683. +// _pcd->request_config = 1; /* Configuration changed */
  92684. + do_gadget_setup(pcd, &ctrl);
  92685. + break;
  92686. +
  92687. + case UR_SYNCH_FRAME:
  92688. + do_gadget_setup(pcd, &ctrl);
  92689. + break;
  92690. +
  92691. + default:
  92692. + /* Call the Gadget Driver's setup functions */
  92693. + do_gadget_setup(pcd, &ctrl);
  92694. + break;
  92695. + }
  92696. +}
  92697. +
  92698. +/**
  92699. + * This function completes the ep0 control transfer.
  92700. + */
  92701. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  92702. +{
  92703. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  92704. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  92705. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  92706. + dev_if->in_ep_regs[ep->dwc_ep.num];
  92707. +#ifdef DEBUG_EP0
  92708. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  92709. + dev_if->out_ep_regs[ep->dwc_ep.num];
  92710. +#endif
  92711. + deptsiz0_data_t deptsiz;
  92712. + dev_dma_desc_sts_t desc_sts;
  92713. + dwc_otg_pcd_request_t *req;
  92714. + int is_last = 0;
  92715. + dwc_otg_pcd_t *pcd = ep->pcd;
  92716. +
  92717. +#ifdef DWC_UTE_CFI
  92718. + struct cfi_usb_ctrlrequest *ctrlreq;
  92719. + int retval = -DWC_E_NOT_SUPPORTED;
  92720. +#endif
  92721. +
  92722. + desc_sts.b.bytes = 0;
  92723. +
  92724. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  92725. + if (ep->dwc_ep.is_in) {
  92726. +#ifdef DEBUG_EP0
  92727. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  92728. +#endif
  92729. + do_setup_out_status_phase(pcd);
  92730. + } else {
  92731. +#ifdef DEBUG_EP0
  92732. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  92733. +#endif
  92734. +
  92735. +#ifdef DWC_UTE_CFI
  92736. + ctrlreq = &pcd->cfi->ctrl_req;
  92737. +
  92738. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  92739. + if (ctrlreq->bRequest > 0xB0
  92740. + && ctrlreq->bRequest < 0xBF) {
  92741. +
  92742. + /* Return if the PCD failed to handle the request */
  92743. + if ((retval =
  92744. + pcd->cfi->ops.
  92745. + ctrl_write_complete(pcd->cfi,
  92746. + pcd)) < 0) {
  92747. + CFI_INFO
  92748. + ("ERROR setting a new value in the PCD(%d)\n",
  92749. + retval);
  92750. + ep0_do_stall(pcd, retval);
  92751. + pcd->ep0_pending = 0;
  92752. + return 0;
  92753. + }
  92754. +
  92755. + /* If the gadget needs to be notified on the request */
  92756. + if (pcd->cfi->need_gadget_att == 1) {
  92757. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  92758. + retval =
  92759. + cfi_gadget_setup(pcd,
  92760. + &pcd->cfi->
  92761. + ctrl_req);
  92762. +
  92763. + /* Return from the function if the gadget failed to process
  92764. + * the request properly - this should never happen !!!
  92765. + */
  92766. + if (retval < 0) {
  92767. + CFI_INFO
  92768. + ("ERROR setting a new value in the gadget(%d)\n",
  92769. + retval);
  92770. + pcd->ep0_pending = 0;
  92771. + return 0;
  92772. + }
  92773. + }
  92774. +
  92775. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  92776. + retval);
  92777. + /* If we hit here then the PCD and the gadget has properly
  92778. + * handled the request - so send the ZLP IN to the host.
  92779. + */
  92780. + /* @todo: MAS - decide whether we need to start the setup
  92781. + * stage based on the need_setup value of the cfi object
  92782. + */
  92783. + do_setup_in_status_phase(pcd);
  92784. + pcd->ep0_pending = 0;
  92785. + return 1;
  92786. + }
  92787. + }
  92788. +#endif
  92789. +
  92790. + do_setup_in_status_phase(pcd);
  92791. + }
  92792. + pcd->ep0_pending = 0;
  92793. + return 1;
  92794. + }
  92795. +
  92796. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  92797. + return 0;
  92798. + }
  92799. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  92800. +
  92801. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  92802. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  92803. + is_last = 1;
  92804. + } else if (ep->dwc_ep.is_in) {
  92805. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  92806. + if (core_if->dma_desc_enable != 0)
  92807. + desc_sts = dev_if->in_desc_addr->status;
  92808. +#ifdef DEBUG_EP0
  92809. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  92810. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  92811. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  92812. +#endif
  92813. +
  92814. + if (((core_if->dma_desc_enable == 0)
  92815. + && (deptsiz.b.xfersize == 0))
  92816. + || ((core_if->dma_desc_enable != 0)
  92817. + && (desc_sts.b.bytes == 0))) {
  92818. + req->actual = ep->dwc_ep.xfer_count;
  92819. + /* Is a Zero Len Packet needed? */
  92820. + if (req->sent_zlp) {
  92821. +#ifdef DEBUG_EP0
  92822. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  92823. +#endif
  92824. + req->sent_zlp = 0;
  92825. + }
  92826. + do_setup_out_status_phase(pcd);
  92827. + }
  92828. + } else {
  92829. + /* ep0-OUT */
  92830. +#ifdef DEBUG_EP0
  92831. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  92832. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  92833. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  92834. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  92835. +#endif
  92836. + req->actual = ep->dwc_ep.xfer_count;
  92837. +
  92838. + /* Is a Zero Len Packet needed? */
  92839. + if (req->sent_zlp) {
  92840. +#ifdef DEBUG_EP0
  92841. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  92842. +#endif
  92843. + req->sent_zlp = 0;
  92844. + }
  92845. + /* For older cores do setup in status phase in Slave/BDMA modes,
  92846. + * starting from 3.00 do that only in slave, and for DMA modes
  92847. + * just re-enable ep 0 OUT here*/
  92848. + if (core_if->dma_enable == 0
  92849. + || (core_if->dma_desc_enable == 0
  92850. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  92851. + do_setup_in_status_phase(pcd);
  92852. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  92853. + DWC_DEBUGPL(DBG_PCDV,
  92854. + "Enable out ep before in status phase\n");
  92855. + ep0_out_start(core_if, pcd);
  92856. + }
  92857. + }
  92858. +
  92859. + /* Complete the request */
  92860. + if (is_last) {
  92861. + dwc_otg_request_done(ep, req, 0);
  92862. + ep->dwc_ep.start_xfer_buff = 0;
  92863. + ep->dwc_ep.xfer_buff = 0;
  92864. + ep->dwc_ep.xfer_len = 0;
  92865. + return 1;
  92866. + }
  92867. + return 0;
  92868. +}
  92869. +
  92870. +#ifdef DWC_UTE_CFI
  92871. +/**
  92872. + * This function calculates traverses all the CFI DMA descriptors and
  92873. + * and accumulates the bytes that are left to be transfered.
  92874. + *
  92875. + * @return The total bytes left to transfered, or a negative value as failure
  92876. + */
  92877. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  92878. +{
  92879. + int32_t ret = 0;
  92880. + int i;
  92881. + struct dwc_otg_dma_desc *ddesc = NULL;
  92882. + struct cfi_ep *cfiep;
  92883. +
  92884. + /* See if the pcd_ep has its respective cfi_ep mapped */
  92885. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  92886. + if (!cfiep) {
  92887. + CFI_INFO("%s: Failed to find ep\n", __func__);
  92888. + return -1;
  92889. + }
  92890. +
  92891. + ddesc = ep->dwc_ep.descs;
  92892. +
  92893. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  92894. +
  92895. +#if defined(PRINT_CFI_DMA_DESCS)
  92896. + print_desc(ddesc, ep->ep.name, i);
  92897. +#endif
  92898. + ret += ddesc->status.b.bytes;
  92899. + ddesc++;
  92900. + }
  92901. +
  92902. + if (ret)
  92903. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  92904. + ret);
  92905. +
  92906. + return ret;
  92907. +}
  92908. +#endif
  92909. +
  92910. +/**
  92911. + * This function completes the request for the EP. If there are
  92912. + * additional requests for the EP in the queue they will be started.
  92913. + */
  92914. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  92915. +{
  92916. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  92917. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  92918. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  92919. + dev_if->in_ep_regs[ep->dwc_ep.num];
  92920. + deptsiz_data_t deptsiz;
  92921. + dev_dma_desc_sts_t desc_sts;
  92922. + dwc_otg_pcd_request_t *req = 0;
  92923. + dwc_otg_dev_dma_desc_t *dma_desc;
  92924. + uint32_t byte_count = 0;
  92925. + int is_last = 0;
  92926. + int i;
  92927. +
  92928. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  92929. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  92930. +
  92931. + /* Get any pending requests */
  92932. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  92933. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  92934. + if (!req) {
  92935. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  92936. + return;
  92937. + }
  92938. + } else {
  92939. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  92940. + return;
  92941. + }
  92942. +
  92943. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  92944. +
  92945. + if (ep->dwc_ep.is_in) {
  92946. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  92947. +
  92948. + if (core_if->dma_enable) {
  92949. + if (core_if->dma_desc_enable == 0) {
  92950. + if (deptsiz.b.xfersize == 0
  92951. + && deptsiz.b.pktcnt == 0) {
  92952. + byte_count =
  92953. + ep->dwc_ep.xfer_len -
  92954. + ep->dwc_ep.xfer_count;
  92955. +
  92956. + ep->dwc_ep.xfer_buff += byte_count;
  92957. + ep->dwc_ep.dma_addr += byte_count;
  92958. + ep->dwc_ep.xfer_count += byte_count;
  92959. +
  92960. + DWC_DEBUGPL(DBG_PCDV,
  92961. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  92962. + ep->dwc_ep.num,
  92963. + (ep->dwc_ep.
  92964. + is_in ? "IN" : "OUT"),
  92965. + ep->dwc_ep.xfer_len,
  92966. + deptsiz.b.xfersize,
  92967. + deptsiz.b.pktcnt);
  92968. +
  92969. + if (ep->dwc_ep.xfer_len <
  92970. + ep->dwc_ep.total_len) {
  92971. + dwc_otg_ep_start_transfer
  92972. + (core_if, &ep->dwc_ep);
  92973. + } else if (ep->dwc_ep.sent_zlp) {
  92974. + /*
  92975. + * This fragment of code should initiate 0
  92976. + * length transfer in case if it is queued
  92977. + * a transfer with size divisible to EPs max
  92978. + * packet size and with usb_request zero field
  92979. + * is set, which means that after data is transfered,
  92980. + * it is also should be transfered
  92981. + * a 0 length packet at the end. For Slave and
  92982. + * Buffer DMA modes in this case SW has
  92983. + * to initiate 2 transfers one with transfer size,
  92984. + * and the second with 0 size. For Descriptor
  92985. + * DMA mode SW is able to initiate a transfer,
  92986. + * which will handle all the packets including
  92987. + * the last 0 length.
  92988. + */
  92989. + ep->dwc_ep.sent_zlp = 0;
  92990. + dwc_otg_ep_start_zl_transfer
  92991. + (core_if, &ep->dwc_ep);
  92992. + } else {
  92993. + is_last = 1;
  92994. + }
  92995. + } else {
  92996. + if (ep->dwc_ep.type ==
  92997. + DWC_OTG_EP_TYPE_ISOC) {
  92998. + req->actual = 0;
  92999. + dwc_otg_request_done(ep, req, 0);
  93000. +
  93001. + ep->dwc_ep.start_xfer_buff = 0;
  93002. + ep->dwc_ep.xfer_buff = 0;
  93003. + ep->dwc_ep.xfer_len = 0;
  93004. +
  93005. + /* If there is a request in the queue start it. */
  93006. + start_next_request(ep);
  93007. + } else
  93008. + DWC_WARN
  93009. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  93010. + ep->dwc_ep.num,
  93011. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  93012. + deptsiz.b.xfersize,
  93013. + deptsiz.b.pktcnt);
  93014. + }
  93015. + } else {
  93016. + dma_desc = ep->dwc_ep.desc_addr;
  93017. + byte_count = 0;
  93018. + ep->dwc_ep.sent_zlp = 0;
  93019. +
  93020. +#ifdef DWC_UTE_CFI
  93021. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  93022. + ep->dwc_ep.buff_mode);
  93023. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  93024. + int residue;
  93025. +
  93026. + residue = cfi_calc_desc_residue(ep);
  93027. + if (residue < 0)
  93028. + return;
  93029. +
  93030. + byte_count = residue;
  93031. + } else {
  93032. +#endif
  93033. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  93034. + ++i) {
  93035. + desc_sts = dma_desc->status;
  93036. + byte_count += desc_sts.b.bytes;
  93037. + dma_desc++;
  93038. + }
  93039. +#ifdef DWC_UTE_CFI
  93040. + }
  93041. +#endif
  93042. + if (byte_count == 0) {
  93043. + ep->dwc_ep.xfer_count =
  93044. + ep->dwc_ep.total_len;
  93045. + is_last = 1;
  93046. + } else {
  93047. + DWC_WARN("Incomplete transfer\n");
  93048. + }
  93049. + }
  93050. + } else {
  93051. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  93052. + DWC_DEBUGPL(DBG_PCDV,
  93053. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  93054. + ep->dwc_ep.num,
  93055. + ep->dwc_ep.is_in ? "IN" : "OUT",
  93056. + ep->dwc_ep.xfer_len,
  93057. + deptsiz.b.xfersize,
  93058. + deptsiz.b.pktcnt);
  93059. +
  93060. + /* Check if the whole transfer was completed,
  93061. + * if no, setup transfer for next portion of data
  93062. + */
  93063. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  93064. + dwc_otg_ep_start_transfer(core_if,
  93065. + &ep->dwc_ep);
  93066. + } else if (ep->dwc_ep.sent_zlp) {
  93067. + /*
  93068. + * This fragment of code should initiate 0
  93069. + * length trasfer in case if it is queued
  93070. + * a trasfer with size divisible to EPs max
  93071. + * packet size and with usb_request zero field
  93072. + * is set, which means that after data is transfered,
  93073. + * it is also should be transfered
  93074. + * a 0 length packet at the end. For Slave and
  93075. + * Buffer DMA modes in this case SW has
  93076. + * to initiate 2 transfers one with transfer size,
  93077. + * and the second with 0 size. For Desriptor
  93078. + * DMA mode SW is able to initiate a transfer,
  93079. + * which will handle all the packets including
  93080. + * the last 0 legth.
  93081. + */
  93082. + ep->dwc_ep.sent_zlp = 0;
  93083. + dwc_otg_ep_start_zl_transfer(core_if,
  93084. + &ep->dwc_ep);
  93085. + } else {
  93086. + is_last = 1;
  93087. + }
  93088. + } else {
  93089. + DWC_WARN
  93090. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  93091. + ep->dwc_ep.num,
  93092. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  93093. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  93094. + }
  93095. + }
  93096. + } else {
  93097. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  93098. + dev_if->out_ep_regs[ep->dwc_ep.num];
  93099. + desc_sts.d32 = 0;
  93100. + if (core_if->dma_enable) {
  93101. + if (core_if->dma_desc_enable) {
  93102. + dma_desc = ep->dwc_ep.desc_addr;
  93103. + byte_count = 0;
  93104. + ep->dwc_ep.sent_zlp = 0;
  93105. +
  93106. +#ifdef DWC_UTE_CFI
  93107. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  93108. + ep->dwc_ep.buff_mode);
  93109. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  93110. + int residue;
  93111. + residue = cfi_calc_desc_residue(ep);
  93112. + if (residue < 0)
  93113. + return;
  93114. + byte_count = residue;
  93115. + } else {
  93116. +#endif
  93117. +
  93118. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  93119. + ++i) {
  93120. + desc_sts = dma_desc->status;
  93121. + byte_count += desc_sts.b.bytes;
  93122. + dma_desc++;
  93123. + }
  93124. +
  93125. +#ifdef DWC_UTE_CFI
  93126. + }
  93127. +#endif
  93128. + /* Checking for interrupt Out transfers with not
  93129. + * dword aligned mps sizes
  93130. + */
  93131. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  93132. + (ep->dwc_ep.maxpacket%4)) {
  93133. + ep->dwc_ep.xfer_count =
  93134. + ep->dwc_ep.total_len - byte_count;
  93135. + if ((ep->dwc_ep.xfer_len %
  93136. + ep->dwc_ep.maxpacket)
  93137. + && (ep->dwc_ep.xfer_len /
  93138. + ep->dwc_ep.maxpacket <
  93139. + MAX_DMA_DESC_CNT))
  93140. + ep->dwc_ep.xfer_len -=
  93141. + (ep->dwc_ep.desc_cnt -
  93142. + 1) * ep->dwc_ep.maxpacket +
  93143. + ep->dwc_ep.xfer_len %
  93144. + ep->dwc_ep.maxpacket;
  93145. + else
  93146. + ep->dwc_ep.xfer_len -=
  93147. + ep->dwc_ep.desc_cnt *
  93148. + ep->dwc_ep.maxpacket;
  93149. + if (ep->dwc_ep.xfer_len > 0) {
  93150. + dwc_otg_ep_start_transfer
  93151. + (core_if, &ep->dwc_ep);
  93152. + } else {
  93153. + is_last = 1;
  93154. + }
  93155. + } else {
  93156. + ep->dwc_ep.xfer_count =
  93157. + ep->dwc_ep.total_len - byte_count +
  93158. + ((4 -
  93159. + (ep->dwc_ep.
  93160. + total_len & 0x3)) & 0x3);
  93161. + is_last = 1;
  93162. + }
  93163. + } else {
  93164. + deptsiz.d32 = 0;
  93165. + deptsiz.d32 =
  93166. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  93167. +
  93168. + byte_count = (ep->dwc_ep.xfer_len -
  93169. + ep->dwc_ep.xfer_count -
  93170. + deptsiz.b.xfersize);
  93171. + ep->dwc_ep.xfer_buff += byte_count;
  93172. + ep->dwc_ep.dma_addr += byte_count;
  93173. + ep->dwc_ep.xfer_count += byte_count;
  93174. +
  93175. + /* Check if the whole transfer was completed,
  93176. + * if no, setup transfer for next portion of data
  93177. + */
  93178. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  93179. + dwc_otg_ep_start_transfer(core_if,
  93180. + &ep->dwc_ep);
  93181. + } else if (ep->dwc_ep.sent_zlp) {
  93182. + /*
  93183. + * This fragment of code should initiate 0
  93184. + * length trasfer in case if it is queued
  93185. + * a trasfer with size divisible to EPs max
  93186. + * packet size and with usb_request zero field
  93187. + * is set, which means that after data is transfered,
  93188. + * it is also should be transfered
  93189. + * a 0 length packet at the end. For Slave and
  93190. + * Buffer DMA modes in this case SW has
  93191. + * to initiate 2 transfers one with transfer size,
  93192. + * and the second with 0 size. For Desriptor
  93193. + * DMA mode SW is able to initiate a transfer,
  93194. + * which will handle all the packets including
  93195. + * the last 0 legth.
  93196. + */
  93197. + ep->dwc_ep.sent_zlp = 0;
  93198. + dwc_otg_ep_start_zl_transfer(core_if,
  93199. + &ep->dwc_ep);
  93200. + } else {
  93201. + is_last = 1;
  93202. + }
  93203. + }
  93204. + } else {
  93205. + /* Check if the whole transfer was completed,
  93206. + * if no, setup transfer for next portion of data
  93207. + */
  93208. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  93209. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  93210. + } else if (ep->dwc_ep.sent_zlp) {
  93211. + /*
  93212. + * This fragment of code should initiate 0
  93213. + * length transfer in case if it is queued
  93214. + * a transfer with size divisible to EPs max
  93215. + * packet size and with usb_request zero field
  93216. + * is set, which means that after data is transfered,
  93217. + * it is also should be transfered
  93218. + * a 0 length packet at the end. For Slave and
  93219. + * Buffer DMA modes in this case SW has
  93220. + * to initiate 2 transfers one with transfer size,
  93221. + * and the second with 0 size. For Descriptor
  93222. + * DMA mode SW is able to initiate a transfer,
  93223. + * which will handle all the packets including
  93224. + * the last 0 length.
  93225. + */
  93226. + ep->dwc_ep.sent_zlp = 0;
  93227. + dwc_otg_ep_start_zl_transfer(core_if,
  93228. + &ep->dwc_ep);
  93229. + } else {
  93230. + is_last = 1;
  93231. + }
  93232. + }
  93233. +
  93234. + DWC_DEBUGPL(DBG_PCDV,
  93235. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  93236. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  93237. + ep->dwc_ep.is_in ? "IN" : "OUT",
  93238. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  93239. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  93240. + }
  93241. +
  93242. + /* Complete the request */
  93243. + if (is_last) {
  93244. +#ifdef DWC_UTE_CFI
  93245. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  93246. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  93247. + } else {
  93248. +#endif
  93249. + req->actual = ep->dwc_ep.xfer_count;
  93250. +#ifdef DWC_UTE_CFI
  93251. + }
  93252. +#endif
  93253. + if (req->dw_align_buf) {
  93254. + if (!ep->dwc_ep.is_in) {
  93255. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  93256. + }
  93257. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  93258. + req->dw_align_buf_dma);
  93259. + }
  93260. +
  93261. + dwc_otg_request_done(ep, req, 0);
  93262. +
  93263. + ep->dwc_ep.start_xfer_buff = 0;
  93264. + ep->dwc_ep.xfer_buff = 0;
  93265. + ep->dwc_ep.xfer_len = 0;
  93266. +
  93267. + /* If there is a request in the queue start it. */
  93268. + start_next_request(ep);
  93269. + }
  93270. +}
  93271. +
  93272. +#ifdef DWC_EN_ISOC
  93273. +
  93274. +/**
  93275. + * This function BNA interrupt for Isochronous EPs
  93276. + *
  93277. + */
  93278. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  93279. +{
  93280. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  93281. + volatile uint32_t *addr;
  93282. + depctl_data_t depctl = {.d32 = 0 };
  93283. + dwc_otg_pcd_t *pcd = ep->pcd;
  93284. + dwc_otg_dev_dma_desc_t *dma_desc;
  93285. + int i;
  93286. +
  93287. + dma_desc =
  93288. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  93289. +
  93290. + if (dwc_ep->is_in) {
  93291. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  93292. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  93293. + sts.d32 = dma_desc->status.d32;
  93294. + sts.b_iso_in.bs = BS_HOST_READY;
  93295. + dma_desc->status.d32 = sts.d32;
  93296. + }
  93297. + } else {
  93298. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  93299. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  93300. + sts.d32 = dma_desc->status.d32;
  93301. + sts.b_iso_out.bs = BS_HOST_READY;
  93302. + dma_desc->status.d32 = sts.d32;
  93303. + }
  93304. + }
  93305. +
  93306. + if (dwc_ep->is_in == 0) {
  93307. + addr =
  93308. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  93309. + num]->doepctl;
  93310. + } else {
  93311. + addr =
  93312. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  93313. + }
  93314. + depctl.b.epena = 1;
  93315. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  93316. +}
  93317. +
  93318. +/**
  93319. + * This function sets latest iso packet information(non-PTI mode)
  93320. + *
  93321. + * @param core_if Programming view of DWC_otg controller.
  93322. + * @param ep The EP to start the transfer on.
  93323. + *
  93324. + */
  93325. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  93326. +{
  93327. + deptsiz_data_t deptsiz = {.d32 = 0 };
  93328. + dma_addr_t dma_addr;
  93329. + uint32_t offset;
  93330. +
  93331. + if (ep->proc_buf_num)
  93332. + dma_addr = ep->dma_addr1;
  93333. + else
  93334. + dma_addr = ep->dma_addr0;
  93335. +
  93336. + if (ep->is_in) {
  93337. + deptsiz.d32 =
  93338. + DWC_READ_REG32(&core_if->dev_if->
  93339. + in_ep_regs[ep->num]->dieptsiz);
  93340. + offset = ep->data_per_frame;
  93341. + } else {
  93342. + deptsiz.d32 =
  93343. + DWC_READ_REG32(&core_if->dev_if->
  93344. + out_ep_regs[ep->num]->doeptsiz);
  93345. + offset =
  93346. + ep->data_per_frame +
  93347. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  93348. + }
  93349. +
  93350. + if (!deptsiz.b.xfersize) {
  93351. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  93352. + ep->pkt_info[ep->cur_pkt].offset =
  93353. + ep->cur_pkt_dma_addr - dma_addr;
  93354. + ep->pkt_info[ep->cur_pkt].status = 0;
  93355. + } else {
  93356. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  93357. + ep->pkt_info[ep->cur_pkt].offset =
  93358. + ep->cur_pkt_dma_addr - dma_addr;
  93359. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  93360. + }
  93361. + ep->cur_pkt_addr += offset;
  93362. + ep->cur_pkt_dma_addr += offset;
  93363. + ep->cur_pkt++;
  93364. +}
  93365. +
  93366. +/**
  93367. + * This function sets latest iso packet information(DDMA mode)
  93368. + *
  93369. + * @param core_if Programming view of DWC_otg controller.
  93370. + * @param dwc_ep The EP to start the transfer on.
  93371. + *
  93372. + */
  93373. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  93374. + dwc_ep_t * dwc_ep)
  93375. +{
  93376. + dwc_otg_dev_dma_desc_t *dma_desc;
  93377. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  93378. + iso_pkt_info_t *iso_packet;
  93379. + uint32_t data_per_desc;
  93380. + uint32_t offset;
  93381. + int i, j;
  93382. +
  93383. + iso_packet = dwc_ep->pkt_info;
  93384. +
  93385. + /** Reinit closed DMA Descriptors*/
  93386. + /** ISO OUT EP */
  93387. + if (dwc_ep->is_in == 0) {
  93388. + dma_desc =
  93389. + dwc_ep->iso_desc_addr +
  93390. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  93391. + offset = 0;
  93392. +
  93393. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  93394. + i += dwc_ep->pkt_per_frm) {
  93395. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  93396. + data_per_desc =
  93397. + ((j + 1) * dwc_ep->maxpacket >
  93398. + dwc_ep->
  93399. + data_per_frame) ? dwc_ep->data_per_frame -
  93400. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  93401. + data_per_desc +=
  93402. + (data_per_desc % 4) ? (4 -
  93403. + data_per_desc %
  93404. + 4) : 0;
  93405. +
  93406. + sts.d32 = dma_desc->status.d32;
  93407. +
  93408. + /* Write status in iso_packet_decsriptor */
  93409. + iso_packet->status =
  93410. + sts.b_iso_out.rxsts +
  93411. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  93412. + if (iso_packet->status) {
  93413. + iso_packet->status = -DWC_E_NO_DATA;
  93414. + }
  93415. +
  93416. + /* Received data length */
  93417. + if (!sts.b_iso_out.rxbytes) {
  93418. + iso_packet->length =
  93419. + data_per_desc -
  93420. + sts.b_iso_out.rxbytes;
  93421. + } else {
  93422. + iso_packet->length =
  93423. + data_per_desc -
  93424. + sts.b_iso_out.rxbytes + (4 -
  93425. + dwc_ep->data_per_frame
  93426. + % 4);
  93427. + }
  93428. +
  93429. + iso_packet->offset = offset;
  93430. +
  93431. + offset += data_per_desc;
  93432. + dma_desc++;
  93433. + iso_packet++;
  93434. + }
  93435. + }
  93436. +
  93437. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  93438. + data_per_desc =
  93439. + ((j + 1) * dwc_ep->maxpacket >
  93440. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  93441. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  93442. + data_per_desc +=
  93443. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  93444. +
  93445. + sts.d32 = dma_desc->status.d32;
  93446. +
  93447. + /* Write status in iso_packet_decsriptor */
  93448. + iso_packet->status =
  93449. + sts.b_iso_out.rxsts +
  93450. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  93451. + if (iso_packet->status) {
  93452. + iso_packet->status = -DWC_E_NO_DATA;
  93453. + }
  93454. +
  93455. + /* Received data length */
  93456. + iso_packet->length =
  93457. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  93458. +
  93459. + iso_packet->offset = offset;
  93460. +
  93461. + offset += data_per_desc;
  93462. + iso_packet++;
  93463. + dma_desc++;
  93464. + }
  93465. +
  93466. + sts.d32 = dma_desc->status.d32;
  93467. +
  93468. + /* Write status in iso_packet_decsriptor */
  93469. + iso_packet->status =
  93470. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  93471. + if (iso_packet->status) {
  93472. + iso_packet->status = -DWC_E_NO_DATA;
  93473. + }
  93474. + /* Received data length */
  93475. + if (!sts.b_iso_out.rxbytes) {
  93476. + iso_packet->length =
  93477. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  93478. + } else {
  93479. + iso_packet->length =
  93480. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  93481. + (4 - dwc_ep->data_per_frame % 4);
  93482. + }
  93483. +
  93484. + iso_packet->offset = offset;
  93485. + } else {
  93486. +/** ISO IN EP */
  93487. +
  93488. + dma_desc =
  93489. + dwc_ep->iso_desc_addr +
  93490. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  93491. +
  93492. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  93493. + sts.d32 = dma_desc->status.d32;
  93494. +
  93495. + /* Write status in iso packet descriptor */
  93496. + iso_packet->status =
  93497. + sts.b_iso_in.txsts +
  93498. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  93499. + if (iso_packet->status != 0) {
  93500. + iso_packet->status = -DWC_E_NO_DATA;
  93501. +
  93502. + }
  93503. + /* Bytes has been transfered */
  93504. + iso_packet->length =
  93505. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  93506. +
  93507. + dma_desc++;
  93508. + iso_packet++;
  93509. + }
  93510. +
  93511. + sts.d32 = dma_desc->status.d32;
  93512. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  93513. + sts.d32 = dma_desc->status.d32;
  93514. + }
  93515. +
  93516. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  93517. + iso_packet->status =
  93518. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  93519. + if (iso_packet->status != 0) {
  93520. + iso_packet->status = -DWC_E_NO_DATA;
  93521. + }
  93522. +
  93523. + /* Bytes has been transfered */
  93524. + iso_packet->length =
  93525. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  93526. + }
  93527. +}
  93528. +
  93529. +/**
  93530. + * This function reinitialize DMA Descriptors for Isochronous transfer
  93531. + *
  93532. + * @param core_if Programming view of DWC_otg controller.
  93533. + * @param dwc_ep The EP to start the transfer on.
  93534. + *
  93535. + */
  93536. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  93537. +{
  93538. + int i, j;
  93539. + dwc_otg_dev_dma_desc_t *dma_desc;
  93540. + dma_addr_t dma_ad;
  93541. + volatile uint32_t *addr;
  93542. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  93543. + uint32_t data_per_desc;
  93544. +
  93545. + if (dwc_ep->is_in == 0) {
  93546. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  93547. + } else {
  93548. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  93549. + }
  93550. +
  93551. + if (dwc_ep->proc_buf_num == 0) {
  93552. + /** Buffer 0 descriptors setup */
  93553. + dma_ad = dwc_ep->dma_addr0;
  93554. + } else {
  93555. + /** Buffer 1 descriptors setup */
  93556. + dma_ad = dwc_ep->dma_addr1;
  93557. + }
  93558. +
  93559. + /** Reinit closed DMA Descriptors*/
  93560. + /** ISO OUT EP */
  93561. + if (dwc_ep->is_in == 0) {
  93562. + dma_desc =
  93563. + dwc_ep->iso_desc_addr +
  93564. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  93565. +
  93566. + sts.b_iso_out.bs = BS_HOST_READY;
  93567. + sts.b_iso_out.rxsts = 0;
  93568. + sts.b_iso_out.l = 0;
  93569. + sts.b_iso_out.sp = 0;
  93570. + sts.b_iso_out.ioc = 0;
  93571. + sts.b_iso_out.pid = 0;
  93572. + sts.b_iso_out.framenum = 0;
  93573. +
  93574. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  93575. + i += dwc_ep->pkt_per_frm) {
  93576. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  93577. + data_per_desc =
  93578. + ((j + 1) * dwc_ep->maxpacket >
  93579. + dwc_ep->
  93580. + data_per_frame) ? dwc_ep->data_per_frame -
  93581. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  93582. + data_per_desc +=
  93583. + (data_per_desc % 4) ? (4 -
  93584. + data_per_desc %
  93585. + 4) : 0;
  93586. + sts.b_iso_out.rxbytes = data_per_desc;
  93587. + dma_desc->buf = dma_ad;
  93588. + dma_desc->status.d32 = sts.d32;
  93589. +
  93590. + dma_ad += data_per_desc;
  93591. + dma_desc++;
  93592. + }
  93593. + }
  93594. +
  93595. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  93596. +
  93597. + data_per_desc =
  93598. + ((j + 1) * dwc_ep->maxpacket >
  93599. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  93600. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  93601. + data_per_desc +=
  93602. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  93603. + sts.b_iso_out.rxbytes = data_per_desc;
  93604. +
  93605. + dma_desc->buf = dma_ad;
  93606. + dma_desc->status.d32 = sts.d32;
  93607. +
  93608. + dma_desc++;
  93609. + dma_ad += data_per_desc;
  93610. + }
  93611. +
  93612. + sts.b_iso_out.ioc = 1;
  93613. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  93614. +
  93615. + data_per_desc =
  93616. + ((j + 1) * dwc_ep->maxpacket >
  93617. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  93618. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  93619. + data_per_desc +=
  93620. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  93621. + sts.b_iso_out.rxbytes = data_per_desc;
  93622. +
  93623. + dma_desc->buf = dma_ad;
  93624. + dma_desc->status.d32 = sts.d32;
  93625. + } else {
  93626. +/** ISO IN EP */
  93627. +
  93628. + dma_desc =
  93629. + dwc_ep->iso_desc_addr +
  93630. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  93631. +
  93632. + sts.b_iso_in.bs = BS_HOST_READY;
  93633. + sts.b_iso_in.txsts = 0;
  93634. + sts.b_iso_in.sp = 0;
  93635. + sts.b_iso_in.ioc = 0;
  93636. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  93637. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  93638. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  93639. + sts.b_iso_in.l = 0;
  93640. +
  93641. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  93642. + dma_desc->buf = dma_ad;
  93643. + dma_desc->status.d32 = sts.d32;
  93644. +
  93645. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  93646. + dma_ad += dwc_ep->data_per_frame;
  93647. + dma_desc++;
  93648. + }
  93649. +
  93650. + sts.b_iso_in.ioc = 1;
  93651. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  93652. +
  93653. + dma_desc->buf = dma_ad;
  93654. + dma_desc->status.d32 = sts.d32;
  93655. +
  93656. + dwc_ep->next_frame =
  93657. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  93658. + }
  93659. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  93660. +}
  93661. +
  93662. +/**
  93663. + * This function is to handle Iso EP transfer complete interrupt
  93664. + * in case Iso out packet was dropped
  93665. + *
  93666. + * @param core_if Programming view of DWC_otg controller.
  93667. + * @param dwc_ep The EP for wihich transfer complete was asserted
  93668. + *
  93669. + */
  93670. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  93671. + dwc_ep_t * dwc_ep)
  93672. +{
  93673. + uint32_t dma_addr;
  93674. + uint32_t drp_pkt;
  93675. + uint32_t drp_pkt_cnt;
  93676. + deptsiz_data_t deptsiz = {.d32 = 0 };
  93677. + depctl_data_t depctl = {.d32 = 0 };
  93678. + int i;
  93679. +
  93680. + deptsiz.d32 =
  93681. + DWC_READ_REG32(&core_if->dev_if->
  93682. + out_ep_regs[dwc_ep->num]->doeptsiz);
  93683. +
  93684. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  93685. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  93686. +
  93687. + /* Setting dropped packets status */
  93688. + for (i = 0; i < drp_pkt_cnt; ++i) {
  93689. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  93690. + drp_pkt++;
  93691. + deptsiz.b.pktcnt--;
  93692. + }
  93693. +
  93694. + if (deptsiz.b.pktcnt > 0) {
  93695. + deptsiz.b.xfersize =
  93696. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  93697. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  93698. + } else {
  93699. + deptsiz.b.xfersize = 0;
  93700. + deptsiz.b.pktcnt = 0;
  93701. + }
  93702. +
  93703. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  93704. + deptsiz.d32);
  93705. +
  93706. + if (deptsiz.b.pktcnt > 0) {
  93707. + if (dwc_ep->proc_buf_num) {
  93708. + dma_addr =
  93709. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  93710. + deptsiz.b.xfersize;
  93711. + } else {
  93712. + dma_addr =
  93713. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  93714. + deptsiz.b.xfersize;;
  93715. + }
  93716. +
  93717. + DWC_WRITE_REG32(&core_if->dev_if->
  93718. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  93719. +
  93720. + /** Re-enable endpoint, clear nak */
  93721. + depctl.d32 = 0;
  93722. + depctl.b.epena = 1;
  93723. + depctl.b.cnak = 1;
  93724. +
  93725. + DWC_MODIFY_REG32(&core_if->dev_if->
  93726. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  93727. + depctl.d32);
  93728. + return 0;
  93729. + } else {
  93730. + return 1;
  93731. + }
  93732. +}
  93733. +
  93734. +/**
  93735. + * This function sets iso packets information(PTI mode)
  93736. + *
  93737. + * @param core_if Programming view of DWC_otg controller.
  93738. + * @param ep The EP to start the transfer on.
  93739. + *
  93740. + */
  93741. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  93742. +{
  93743. + int i, j;
  93744. + dma_addr_t dma_ad;
  93745. + iso_pkt_info_t *packet_info = ep->pkt_info;
  93746. + uint32_t offset;
  93747. + uint32_t frame_data;
  93748. + deptsiz_data_t deptsiz;
  93749. +
  93750. + if (ep->proc_buf_num == 0) {
  93751. + /** Buffer 0 descriptors setup */
  93752. + dma_ad = ep->dma_addr0;
  93753. + } else {
  93754. + /** Buffer 1 descriptors setup */
  93755. + dma_ad = ep->dma_addr1;
  93756. + }
  93757. +
  93758. + if (ep->is_in) {
  93759. + deptsiz.d32 =
  93760. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  93761. + dieptsiz);
  93762. + } else {
  93763. + deptsiz.d32 =
  93764. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  93765. + doeptsiz);
  93766. + }
  93767. +
  93768. + if (!deptsiz.b.xfersize) {
  93769. + offset = 0;
  93770. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  93771. + frame_data = ep->data_per_frame;
  93772. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  93773. +
  93774. + /* Packet status - is not set as initially
  93775. + * it is set to 0 and if packet was sent
  93776. + successfully, status field will remain 0*/
  93777. +
  93778. + /* Bytes has been transfered */
  93779. + packet_info->length =
  93780. + (ep->maxpacket <
  93781. + frame_data) ? ep->maxpacket : frame_data;
  93782. +
  93783. + /* Received packet offset */
  93784. + packet_info->offset = offset;
  93785. + offset += packet_info->length;
  93786. + frame_data -= packet_info->length;
  93787. +
  93788. + packet_info++;
  93789. + }
  93790. + }
  93791. + return 1;
  93792. + } else {
  93793. + /* This is a workaround for in case of Transfer Complete with
  93794. + * PktDrpSts interrupts merging - in this case Transfer complete
  93795. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  93796. + * set and with DOEPTSIZ register non zero. Investigations showed,
  93797. + * that this happens when Out packet is dropped, but because of
  93798. + * interrupts merging during first interrupt handling PktDrpSts
  93799. + * bit is cleared and for next merged interrupts it is not reset.
  93800. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  93801. + */
  93802. + if (ep->is_in) {
  93803. + return 1;
  93804. + } else {
  93805. + return handle_iso_out_pkt_dropped(core_if, ep);
  93806. + }
  93807. + }
  93808. +}
  93809. +
  93810. +/**
  93811. + * This function is to handle Iso EP transfer complete interrupt
  93812. + *
  93813. + * @param pcd The PCD
  93814. + * @param ep The EP for which transfer complete was asserted
  93815. + *
  93816. + */
  93817. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  93818. +{
  93819. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  93820. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  93821. + uint8_t is_last = 0;
  93822. +
  93823. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  93824. + DWC_WARN("Next frame is not set!\n");
  93825. + return;
  93826. + }
  93827. +
  93828. + if (core_if->dma_enable) {
  93829. + if (core_if->dma_desc_enable) {
  93830. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  93831. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  93832. + is_last = 1;
  93833. + } else {
  93834. + if (core_if->pti_enh_enable) {
  93835. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  93836. + dwc_ep->proc_buf_num =
  93837. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  93838. + dwc_otg_iso_ep_start_buf_transfer
  93839. + (core_if, dwc_ep);
  93840. + is_last = 1;
  93841. + }
  93842. + } else {
  93843. + set_current_pkt_info(core_if, dwc_ep);
  93844. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  93845. + is_last = 1;
  93846. + dwc_ep->cur_pkt = 0;
  93847. + dwc_ep->proc_buf_num =
  93848. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  93849. + if (dwc_ep->proc_buf_num) {
  93850. + dwc_ep->cur_pkt_addr =
  93851. + dwc_ep->xfer_buff1;
  93852. + dwc_ep->cur_pkt_dma_addr =
  93853. + dwc_ep->dma_addr1;
  93854. + } else {
  93855. + dwc_ep->cur_pkt_addr =
  93856. + dwc_ep->xfer_buff0;
  93857. + dwc_ep->cur_pkt_dma_addr =
  93858. + dwc_ep->dma_addr0;
  93859. + }
  93860. +
  93861. + }
  93862. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  93863. + dwc_ep);
  93864. + }
  93865. + }
  93866. + } else {
  93867. + set_current_pkt_info(core_if, dwc_ep);
  93868. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  93869. + is_last = 1;
  93870. + dwc_ep->cur_pkt = 0;
  93871. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  93872. + if (dwc_ep->proc_buf_num) {
  93873. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  93874. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  93875. + } else {
  93876. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  93877. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  93878. + }
  93879. +
  93880. + }
  93881. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  93882. + }
  93883. + if (is_last)
  93884. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  93885. +}
  93886. +#endif /* DWC_EN_ISOC */
  93887. +
  93888. +/**
  93889. + * This function handle BNA interrupt for Non Isochronous EPs
  93890. + *
  93891. + */
  93892. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  93893. +{
  93894. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  93895. + volatile uint32_t *addr;
  93896. + depctl_data_t depctl = {.d32 = 0 };
  93897. + dwc_otg_pcd_t *pcd = ep->pcd;
  93898. + dwc_otg_dev_dma_desc_t *dma_desc;
  93899. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  93900. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  93901. + int i, start;
  93902. +
  93903. + if (!dwc_ep->desc_cnt)
  93904. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  93905. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  93906. +
  93907. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  93908. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  93909. + uint32_t doepdma;
  93910. + dwc_otg_dev_out_ep_regs_t *out_regs =
  93911. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  93912. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  93913. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  93914. + dma_desc = &(dwc_ep->desc_addr[start]);
  93915. + } else {
  93916. + start = 0;
  93917. + dma_desc = dwc_ep->desc_addr;
  93918. + }
  93919. +
  93920. +
  93921. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  93922. + sts.d32 = dma_desc->status.d32;
  93923. + sts.b.bs = BS_HOST_READY;
  93924. + dma_desc->status.d32 = sts.d32;
  93925. + }
  93926. +
  93927. + if (dwc_ep->is_in == 0) {
  93928. + addr =
  93929. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  93930. + doepctl;
  93931. + } else {
  93932. + addr =
  93933. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  93934. + }
  93935. + depctl.b.epena = 1;
  93936. + depctl.b.cnak = 1;
  93937. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  93938. +}
  93939. +
  93940. +/**
  93941. + * This function handles EP0 Control transfers.
  93942. + *
  93943. + * The state of the control transfers are tracked in
  93944. + * <code>ep0state</code>.
  93945. + */
  93946. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  93947. +{
  93948. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93949. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  93950. + dev_dma_desc_sts_t desc_sts;
  93951. + deptsiz0_data_t deptsiz;
  93952. + uint32_t byte_count;
  93953. +
  93954. +#ifdef DEBUG_EP0
  93955. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  93956. + print_ep0_state(pcd);
  93957. +#endif
  93958. +
  93959. +// DWC_PRINTF("HANDLE EP0\n");
  93960. +
  93961. + switch (pcd->ep0state) {
  93962. + case EP0_DISCONNECT:
  93963. + break;
  93964. +
  93965. + case EP0_IDLE:
  93966. + pcd->request_config = 0;
  93967. +
  93968. + pcd_setup(pcd);
  93969. + break;
  93970. +
  93971. + case EP0_IN_DATA_PHASE:
  93972. +#ifdef DEBUG_EP0
  93973. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  93974. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  93975. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  93976. +#endif
  93977. +
  93978. + if (core_if->dma_enable != 0) {
  93979. + /*
  93980. + * For EP0 we can only program 1 packet at a time so we
  93981. + * need to do the make calculations after each complete.
  93982. + * Call write_packet to make the calculations, as in
  93983. + * slave mode, and use those values to determine if we
  93984. + * can complete.
  93985. + */
  93986. + if (core_if->dma_desc_enable == 0) {
  93987. + deptsiz.d32 =
  93988. + DWC_READ_REG32(&core_if->
  93989. + dev_if->in_ep_regs[0]->
  93990. + dieptsiz);
  93991. + byte_count =
  93992. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  93993. + } else {
  93994. + desc_sts =
  93995. + core_if->dev_if->in_desc_addr->status;
  93996. + byte_count =
  93997. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  93998. + }
  93999. + ep0->dwc_ep.xfer_count += byte_count;
  94000. + ep0->dwc_ep.xfer_buff += byte_count;
  94001. + ep0->dwc_ep.dma_addr += byte_count;
  94002. + }
  94003. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  94004. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94005. + &ep0->dwc_ep);
  94006. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  94007. + } else if (ep0->dwc_ep.sent_zlp) {
  94008. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94009. + &ep0->dwc_ep);
  94010. + ep0->dwc_ep.sent_zlp = 0;
  94011. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  94012. + } else {
  94013. + ep0_complete_request(ep0);
  94014. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  94015. + }
  94016. + break;
  94017. + case EP0_OUT_DATA_PHASE:
  94018. +#ifdef DEBUG_EP0
  94019. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  94020. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  94021. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  94022. +#endif
  94023. + if (core_if->dma_enable != 0) {
  94024. + if (core_if->dma_desc_enable == 0) {
  94025. + deptsiz.d32 =
  94026. + DWC_READ_REG32(&core_if->
  94027. + dev_if->out_ep_regs[0]->
  94028. + doeptsiz);
  94029. + byte_count =
  94030. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  94031. + } else {
  94032. + desc_sts =
  94033. + core_if->dev_if->out_desc_addr->status;
  94034. + byte_count =
  94035. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  94036. + }
  94037. + ep0->dwc_ep.xfer_count += byte_count;
  94038. + ep0->dwc_ep.xfer_buff += byte_count;
  94039. + ep0->dwc_ep.dma_addr += byte_count;
  94040. + }
  94041. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  94042. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94043. + &ep0->dwc_ep);
  94044. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  94045. + } else if (ep0->dwc_ep.sent_zlp) {
  94046. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94047. + &ep0->dwc_ep);
  94048. + ep0->dwc_ep.sent_zlp = 0;
  94049. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  94050. + } else {
  94051. + ep0_complete_request(ep0);
  94052. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  94053. + }
  94054. + break;
  94055. +
  94056. + case EP0_IN_STATUS_PHASE:
  94057. + case EP0_OUT_STATUS_PHASE:
  94058. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  94059. + ep0_complete_request(ep0);
  94060. + pcd->ep0state = EP0_IDLE;
  94061. + ep0->stopped = 1;
  94062. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  94063. +
  94064. + /* Prepare for more SETUP Packets */
  94065. + if (core_if->dma_enable) {
  94066. + ep0_out_start(core_if, pcd);
  94067. + }
  94068. + break;
  94069. +
  94070. + case EP0_STALL:
  94071. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  94072. + break;
  94073. + }
  94074. +#ifdef DEBUG_EP0
  94075. + print_ep0_state(pcd);
  94076. +#endif
  94077. +}
  94078. +
  94079. +/**
  94080. + * Restart transfer
  94081. + */
  94082. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  94083. +{
  94084. + dwc_otg_core_if_t *core_if;
  94085. + dwc_otg_dev_if_t *dev_if;
  94086. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  94087. + dwc_otg_pcd_ep_t *ep;
  94088. +
  94089. + ep = get_in_ep(pcd, epnum);
  94090. +
  94091. +#ifdef DWC_EN_ISOC
  94092. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  94093. + return;
  94094. + }
  94095. +#endif /* DWC_EN_ISOC */
  94096. +
  94097. + core_if = GET_CORE_IF(pcd);
  94098. + dev_if = core_if->dev_if;
  94099. +
  94100. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  94101. +
  94102. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  94103. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  94104. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  94105. + /*
  94106. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  94107. + */
  94108. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  94109. + ep->dwc_ep.start_xfer_buff != 0) {
  94110. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  94111. + ep->dwc_ep.xfer_count = 0;
  94112. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  94113. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  94114. + } else {
  94115. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  94116. + /* convert packet size to dwords. */
  94117. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  94118. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  94119. + }
  94120. + ep->stopped = 0;
  94121. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  94122. + "xfer_len=%0x stopped=%d\n",
  94123. + ep->dwc_ep.xfer_buff,
  94124. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  94125. + ep->stopped);
  94126. + if (epnum == 0) {
  94127. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  94128. + } else {
  94129. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  94130. + }
  94131. + }
  94132. +}
  94133. +
  94134. +/*
  94135. + * This function create new nextep sequnce based on Learn Queue.
  94136. + *
  94137. + * @param core_if Programming view of DWC_otg controller
  94138. + */
  94139. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  94140. +{
  94141. + dwc_otg_device_global_regs_t *dev_global_regs =
  94142. + core_if->dev_if->dev_global_regs;
  94143. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  94144. + /* Number of Token Queue Registers */
  94145. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  94146. + dtknq1_data_t dtknqr1;
  94147. + uint32_t in_tkn_epnums[4];
  94148. + uint8_t seqnum[MAX_EPS_CHANNELS];
  94149. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  94150. + grstctl_t resetctl = {.d32 = 0 };
  94151. + uint8_t temp;
  94152. + int ndx = 0;
  94153. + int start = 0;
  94154. + int end = 0;
  94155. + int sort_done = 0;
  94156. + int i = 0;
  94157. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  94158. +
  94159. +
  94160. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  94161. +
  94162. + /* Read the DTKNQ Registers */
  94163. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  94164. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  94165. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  94166. + in_tkn_epnums[i]);
  94167. + if (addr == &dev_global_regs->dvbusdis) {
  94168. + addr = &dev_global_regs->dtknqr3_dthrctl;
  94169. + } else {
  94170. + ++addr;
  94171. + }
  94172. +
  94173. + }
  94174. +
  94175. + /* Copy the DTKNQR1 data to the bit field. */
  94176. + dtknqr1.d32 = in_tkn_epnums[0];
  94177. + if (dtknqr1.b.wrap_bit) {
  94178. + ndx = dtknqr1.b.intknwptr;
  94179. + end = ndx -1;
  94180. + if (end < 0)
  94181. + end = TOKEN_Q_DEPTH -1;
  94182. + } else {
  94183. + ndx = 0;
  94184. + end = dtknqr1.b.intknwptr -1;
  94185. + if (end < 0)
  94186. + end = 0;
  94187. + }
  94188. + start = ndx;
  94189. +
  94190. + /* Fill seqnum[] by initial values: EP number + 31 */
  94191. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  94192. + seqnum[i] = i +31;
  94193. + }
  94194. +
  94195. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  94196. + for (i=0; i < 6; i++)
  94197. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  94198. +
  94199. + if (TOKEN_Q_DEPTH > 6) {
  94200. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  94201. + for (i=6; i < 14; i++)
  94202. + intkn_seq[i] =
  94203. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  94204. + }
  94205. +
  94206. + if (TOKEN_Q_DEPTH > 14) {
  94207. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  94208. + for (i=14; i < 22; i++)
  94209. + intkn_seq[i] =
  94210. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  94211. + }
  94212. +
  94213. + if (TOKEN_Q_DEPTH > 22) {
  94214. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  94215. + for (i=22; i < 30; i++)
  94216. + intkn_seq[i] =
  94217. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  94218. + }
  94219. +
  94220. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  94221. + start, end);
  94222. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  94223. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  94224. +
  94225. + /* Update seqnum based on intkn_seq[] */
  94226. + i = 0;
  94227. + do {
  94228. + seqnum[intkn_seq[ndx]] = i;
  94229. + ndx++;
  94230. + i++;
  94231. + if (ndx == TOKEN_Q_DEPTH)
  94232. + ndx = 0;
  94233. + } while ( i < TOKEN_Q_DEPTH );
  94234. +
  94235. + /* Mark non active EP's in seqnum[] by 0xff */
  94236. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  94237. + if (core_if->nextep_seq[i] == 0xff )
  94238. + seqnum[i] = 0xff;
  94239. + }
  94240. +
  94241. + /* Sort seqnum[] */
  94242. + sort_done = 0;
  94243. + while (!sort_done) {
  94244. + sort_done = 1;
  94245. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  94246. + if (seqnum[i] > seqnum[i+1]) {
  94247. + temp = seqnum[i];
  94248. + seqnum[i] = seqnum[i+1];
  94249. + seqnum[i+1] = temp;
  94250. + sort_done = 0;
  94251. + }
  94252. + }
  94253. + }
  94254. +
  94255. + ndx = start + seqnum[0];
  94256. + if (ndx >= TOKEN_Q_DEPTH)
  94257. + ndx = ndx % TOKEN_Q_DEPTH;
  94258. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  94259. +
  94260. + /* Update seqnum[] by EP numbers */
  94261. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  94262. + ndx = start + i;
  94263. + if (seqnum[i] < 31) {
  94264. + ndx = start + seqnum[i];
  94265. + if (ndx >= TOKEN_Q_DEPTH)
  94266. + ndx = ndx % TOKEN_Q_DEPTH;
  94267. + seqnum[i] = intkn_seq[ndx];
  94268. + } else {
  94269. + if (seqnum[i] < 0xff) {
  94270. + seqnum[i] = seqnum[i] - 31;
  94271. + } else {
  94272. + break;
  94273. + }
  94274. + }
  94275. + }
  94276. +
  94277. + /* Update nextep_seq[] based on seqnum[] */
  94278. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  94279. + if (seqnum[i] != 0xff) {
  94280. + if (seqnum[i+1] != 0xff) {
  94281. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  94282. + } else {
  94283. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  94284. + break;
  94285. + }
  94286. + } else {
  94287. + break;
  94288. + }
  94289. + }
  94290. +
  94291. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  94292. + __func__, core_if->first_in_nextep_seq);
  94293. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  94294. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  94295. + }
  94296. +
  94297. + /* Flush the Learning Queue */
  94298. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  94299. + resetctl.b.intknqflsh = 1;
  94300. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  94301. +
  94302. +
  94303. +}
  94304. +
  94305. +/**
  94306. + * handle the IN EP disable interrupt.
  94307. + */
  94308. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  94309. + const uint32_t epnum)
  94310. +{
  94311. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94312. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  94313. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  94314. + dctl_data_t dctl = {.d32 = 0 };
  94315. + dwc_otg_pcd_ep_t *ep;
  94316. + dwc_ep_t *dwc_ep;
  94317. + gintmsk_data_t gintmsk_data;
  94318. + depctl_data_t depctl;
  94319. + uint32_t diepdma;
  94320. + uint32_t remain_to_transfer = 0;
  94321. + uint8_t i;
  94322. + uint32_t xfer_size;
  94323. +
  94324. + ep = get_in_ep(pcd, epnum);
  94325. + dwc_ep = &ep->dwc_ep;
  94326. +
  94327. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  94328. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  94329. + complete_ep(ep);
  94330. + return;
  94331. + }
  94332. +
  94333. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  94334. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  94335. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  94336. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  94337. +
  94338. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  94339. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  94340. +
  94341. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  94342. + if (ep->stopped) {
  94343. + if (core_if->en_multiple_tx_fifo)
  94344. + /* Flush the Tx FIFO */
  94345. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  94346. + /* Clear the Global IN NP NAK */
  94347. + dctl.d32 = 0;
  94348. + dctl.b.cgnpinnak = 1;
  94349. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  94350. + /* Restart the transaction */
  94351. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  94352. + restart_transfer(pcd, epnum);
  94353. + }
  94354. + } else {
  94355. + /* Restart the transaction */
  94356. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  94357. + restart_transfer(pcd, epnum);
  94358. + }
  94359. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  94360. + }
  94361. + return;
  94362. + }
  94363. +
  94364. + if (core_if->start_predict > 2) { // NP IN EP
  94365. + core_if->start_predict--;
  94366. + return;
  94367. + }
  94368. +
  94369. + core_if->start_predict--;
  94370. +
  94371. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  94372. +
  94373. + predict_nextep_seq(core_if);
  94374. +
  94375. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  94376. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  94377. + depctl.d32 =
  94378. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  94379. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  94380. + depctl.b.nextep = core_if->nextep_seq[i];
  94381. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  94382. + }
  94383. + }
  94384. + /* Flush Shared NP TxFIFO */
  94385. + dwc_otg_flush_tx_fifo(core_if, 0);
  94386. + /* Rewind buffers */
  94387. + if (!core_if->dma_desc_enable) {
  94388. + i = core_if->first_in_nextep_seq;
  94389. + do {
  94390. + ep = get_in_ep(pcd, i);
  94391. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  94392. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  94393. + if (xfer_size > ep->dwc_ep.maxxfer)
  94394. + xfer_size = ep->dwc_ep.maxxfer;
  94395. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  94396. + if (dieptsiz.b.pktcnt != 0) {
  94397. + if (xfer_size == 0) {
  94398. + remain_to_transfer = 0;
  94399. + } else {
  94400. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  94401. + remain_to_transfer =
  94402. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  94403. + } else {
  94404. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  94405. + + (xfer_size % ep->dwc_ep.maxpacket);
  94406. + }
  94407. + }
  94408. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  94409. + dieptsiz.b.xfersize = remain_to_transfer;
  94410. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  94411. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  94412. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  94413. + }
  94414. + i = core_if->nextep_seq[i];
  94415. + } while (i != core_if->first_in_nextep_seq);
  94416. + } else { // dma_desc_enable
  94417. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  94418. + }
  94419. +
  94420. + /* Restart transfers in predicted sequences */
  94421. + i = core_if->first_in_nextep_seq;
  94422. + do {
  94423. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  94424. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  94425. + if (dieptsiz.b.pktcnt != 0) {
  94426. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  94427. + depctl.b.epena = 1;
  94428. + depctl.b.cnak = 1;
  94429. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  94430. + }
  94431. + i = core_if->nextep_seq[i];
  94432. + } while (i != core_if->first_in_nextep_seq);
  94433. +
  94434. + /* Clear the global non-periodic IN NAK handshake */
  94435. + dctl.d32 = 0;
  94436. + dctl.b.cgnpinnak = 1;
  94437. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  94438. +
  94439. + /* Unmask EP Mismatch interrupt */
  94440. + gintmsk_data.d32 = 0;
  94441. + gintmsk_data.b.epmismatch = 1;
  94442. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  94443. +
  94444. + core_if->start_predict = 0;
  94445. +
  94446. + }
  94447. +}
  94448. +
  94449. +/**
  94450. + * Handler for the IN EP timeout handshake interrupt.
  94451. + */
  94452. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  94453. + const uint32_t epnum)
  94454. +{
  94455. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94456. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  94457. +
  94458. +#ifdef DEBUG
  94459. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  94460. + uint32_t num = 0;
  94461. +#endif
  94462. + dctl_data_t dctl = {.d32 = 0 };
  94463. + dwc_otg_pcd_ep_t *ep;
  94464. +
  94465. + gintmsk_data_t intr_mask = {.d32 = 0 };
  94466. +
  94467. + ep = get_in_ep(pcd, epnum);
  94468. +
  94469. + /* Disable the NP Tx Fifo Empty Interrrupt */
  94470. + if (!core_if->dma_enable) {
  94471. + intr_mask.b.nptxfempty = 1;
  94472. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  94473. + intr_mask.d32, 0);
  94474. + }
  94475. + /** @todo NGS Check EP type.
  94476. + * Implement for Periodic EPs */
  94477. + /*
  94478. + * Non-periodic EP
  94479. + */
  94480. + /* Enable the Global IN NAK Effective Interrupt */
  94481. + intr_mask.b.ginnakeff = 1;
  94482. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  94483. +
  94484. + /* Set Global IN NAK */
  94485. + dctl.b.sgnpinnak = 1;
  94486. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  94487. +
  94488. + ep->stopped = 1;
  94489. +
  94490. +#ifdef DEBUG
  94491. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  94492. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  94493. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  94494. +#endif
  94495. +
  94496. +#ifdef DISABLE_PERIODIC_EP
  94497. + /*
  94498. + * Set the NAK bit for this EP to
  94499. + * start the disable process.
  94500. + */
  94501. + diepctl.d32 = 0;
  94502. + diepctl.b.snak = 1;
  94503. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  94504. + diepctl.d32);
  94505. + ep->disabling = 1;
  94506. + ep->stopped = 1;
  94507. +#endif
  94508. +}
  94509. +
  94510. +/**
  94511. + * Handler for the IN EP NAK interrupt.
  94512. + */
  94513. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  94514. + const uint32_t epnum)
  94515. +{
  94516. + /** @todo implement ISR */
  94517. + dwc_otg_core_if_t *core_if;
  94518. + diepmsk_data_t intr_mask = {.d32 = 0 };
  94519. +
  94520. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  94521. + core_if = GET_CORE_IF(pcd);
  94522. + intr_mask.b.nak = 1;
  94523. +
  94524. + if (core_if->multiproc_int_enable) {
  94525. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  94526. + diepeachintmsk[epnum], intr_mask.d32, 0);
  94527. + } else {
  94528. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  94529. + intr_mask.d32, 0);
  94530. + }
  94531. +
  94532. + return 1;
  94533. +}
  94534. +
  94535. +/**
  94536. + * Handler for the OUT EP Babble interrupt.
  94537. + */
  94538. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  94539. + const uint32_t epnum)
  94540. +{
  94541. + /** @todo implement ISR */
  94542. + dwc_otg_core_if_t *core_if;
  94543. + doepmsk_data_t intr_mask = {.d32 = 0 };
  94544. +
  94545. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  94546. + "OUT EP Babble");
  94547. + core_if = GET_CORE_IF(pcd);
  94548. + intr_mask.b.babble = 1;
  94549. +
  94550. + if (core_if->multiproc_int_enable) {
  94551. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  94552. + doepeachintmsk[epnum], intr_mask.d32, 0);
  94553. + } else {
  94554. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  94555. + intr_mask.d32, 0);
  94556. + }
  94557. +
  94558. + return 1;
  94559. +}
  94560. +
  94561. +/**
  94562. + * Handler for the OUT EP NAK interrupt.
  94563. + */
  94564. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  94565. + const uint32_t epnum)
  94566. +{
  94567. + /** @todo implement ISR */
  94568. + dwc_otg_core_if_t *core_if;
  94569. + doepmsk_data_t intr_mask = {.d32 = 0 };
  94570. +
  94571. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  94572. + core_if = GET_CORE_IF(pcd);
  94573. + intr_mask.b.nak = 1;
  94574. +
  94575. + if (core_if->multiproc_int_enable) {
  94576. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  94577. + doepeachintmsk[epnum], intr_mask.d32, 0);
  94578. + } else {
  94579. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  94580. + intr_mask.d32, 0);
  94581. + }
  94582. +
  94583. + return 1;
  94584. +}
  94585. +
  94586. +/**
  94587. + * Handler for the OUT EP NYET interrupt.
  94588. + */
  94589. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  94590. + const uint32_t epnum)
  94591. +{
  94592. + /** @todo implement ISR */
  94593. + dwc_otg_core_if_t *core_if;
  94594. + doepmsk_data_t intr_mask = {.d32 = 0 };
  94595. +
  94596. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  94597. + core_if = GET_CORE_IF(pcd);
  94598. + intr_mask.b.nyet = 1;
  94599. +
  94600. + if (core_if->multiproc_int_enable) {
  94601. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  94602. + doepeachintmsk[epnum], intr_mask.d32, 0);
  94603. + } else {
  94604. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  94605. + intr_mask.d32, 0);
  94606. + }
  94607. +
  94608. + return 1;
  94609. +}
  94610. +
  94611. +/**
  94612. + * This interrupt indicates that an IN EP has a pending Interrupt.
  94613. + * The sequence for handling the IN EP interrupt is shown below:
  94614. + * -# Read the Device All Endpoint Interrupt register
  94615. + * -# Repeat the following for each IN EP interrupt bit set (from
  94616. + * LSB to MSB).
  94617. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  94618. + * -# If "Transfer Complete" call the request complete function
  94619. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  94620. + * -# If "AHB Error Interrupt" log error
  94621. + * -# If "Time-out Handshake" log error
  94622. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  94623. + * FIFO.
  94624. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  94625. + * Mismatch Interrupt)
  94626. + */
  94627. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  94628. +{
  94629. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  94630. +do { \
  94631. + diepint_data_t diepint = {.d32=0}; \
  94632. + diepint.b.__intr = 1; \
  94633. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  94634. + diepint.d32); \
  94635. +} while (0)
  94636. +
  94637. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94638. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  94639. + diepint_data_t diepint = {.d32 = 0 };
  94640. + depctl_data_t depctl = {.d32 = 0 };
  94641. + uint32_t ep_intr;
  94642. + uint32_t epnum = 0;
  94643. + dwc_otg_pcd_ep_t *ep;
  94644. + dwc_ep_t *dwc_ep;
  94645. + gintmsk_data_t intr_mask = {.d32 = 0 };
  94646. +
  94647. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  94648. +
  94649. + /* Read in the device interrupt bits */
  94650. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  94651. +
  94652. + /* Service the Device IN interrupts for each endpoint */
  94653. + while (ep_intr) {
  94654. + if (ep_intr & 0x1) {
  94655. + uint32_t empty_msk;
  94656. + /* Get EP pointer */
  94657. + ep = get_in_ep(pcd, epnum);
  94658. + dwc_ep = &ep->dwc_ep;
  94659. +
  94660. + depctl.d32 =
  94661. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  94662. + empty_msk =
  94663. + DWC_READ_REG32(&dev_if->
  94664. + dev_global_regs->dtknqr4_fifoemptymsk);
  94665. +
  94666. + DWC_DEBUGPL(DBG_PCDV,
  94667. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  94668. + epnum, empty_msk, depctl.d32);
  94669. +
  94670. + DWC_DEBUGPL(DBG_PCD,
  94671. + "EP%d-%s: type=%d, mps=%d\n",
  94672. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  94673. + dwc_ep->type, dwc_ep->maxpacket);
  94674. +
  94675. + diepint.d32 =
  94676. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  94677. +
  94678. + DWC_DEBUGPL(DBG_PCDV,
  94679. + "EP %d Interrupt Register - 0x%x\n", epnum,
  94680. + diepint.d32);
  94681. + /* Transfer complete */
  94682. + if (diepint.b.xfercompl) {
  94683. + /* Disable the NP Tx FIFO Empty
  94684. + * Interrupt */
  94685. + if (core_if->en_multiple_tx_fifo == 0) {
  94686. + intr_mask.b.nptxfempty = 1;
  94687. + DWC_MODIFY_REG32
  94688. + (&core_if->core_global_regs->gintmsk,
  94689. + intr_mask.d32, 0);
  94690. + } else {
  94691. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  94692. + uint32_t fifoemptymsk =
  94693. + 0x1 << dwc_ep->num;
  94694. + DWC_MODIFY_REG32(&core_if->
  94695. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  94696. + fifoemptymsk, 0);
  94697. + }
  94698. + /* Clear the bit in DIEPINTn for this interrupt */
  94699. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  94700. +
  94701. + /* Complete the transfer */
  94702. + if (epnum == 0) {
  94703. + handle_ep0(pcd);
  94704. + }
  94705. +#ifdef DWC_EN_ISOC
  94706. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  94707. + if (!ep->stopped)
  94708. + complete_iso_ep(pcd, ep);
  94709. + }
  94710. +#endif /* DWC_EN_ISOC */
  94711. +#ifdef DWC_UTE_PER_IO
  94712. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  94713. + if (!ep->stopped)
  94714. + complete_xiso_ep(ep);
  94715. + }
  94716. +#endif /* DWC_UTE_PER_IO */
  94717. + else {
  94718. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  94719. + dwc_ep->bInterval > 1) {
  94720. + dwc_ep->frame_num += dwc_ep->bInterval;
  94721. + if (dwc_ep->frame_num > 0x3FFF)
  94722. + {
  94723. + dwc_ep->frm_overrun = 1;
  94724. + dwc_ep->frame_num &= 0x3FFF;
  94725. + } else
  94726. + dwc_ep->frm_overrun = 0;
  94727. + }
  94728. + complete_ep(ep);
  94729. + if(diepint.b.nak)
  94730. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  94731. + }
  94732. + }
  94733. + /* Endpoint disable */
  94734. + if (diepint.b.epdisabled) {
  94735. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  94736. + epnum);
  94737. + handle_in_ep_disable_intr(pcd, epnum);
  94738. +
  94739. + /* Clear the bit in DIEPINTn for this interrupt */
  94740. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  94741. + }
  94742. + /* AHB Error */
  94743. + if (diepint.b.ahberr) {
  94744. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  94745. + /* Clear the bit in DIEPINTn for this interrupt */
  94746. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  94747. + }
  94748. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  94749. + if (diepint.b.timeout) {
  94750. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  94751. + handle_in_ep_timeout_intr(pcd, epnum);
  94752. +
  94753. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  94754. + }
  94755. + /** IN Token received with TxF Empty */
  94756. + if (diepint.b.intktxfemp) {
  94757. + DWC_DEBUGPL(DBG_ANY,
  94758. + "EP%d IN TKN TxFifo Empty\n",
  94759. + epnum);
  94760. + if (!ep->stopped && epnum != 0) {
  94761. +
  94762. + diepmsk_data_t diepmsk = {.d32 = 0 };
  94763. + diepmsk.b.intktxfemp = 1;
  94764. +
  94765. + if (core_if->multiproc_int_enable) {
  94766. + DWC_MODIFY_REG32
  94767. + (&dev_if->dev_global_regs->diepeachintmsk
  94768. + [epnum], diepmsk.d32, 0);
  94769. + } else {
  94770. + DWC_MODIFY_REG32
  94771. + (&dev_if->dev_global_regs->diepmsk,
  94772. + diepmsk.d32, 0);
  94773. + }
  94774. + } else if (core_if->dma_desc_enable
  94775. + && epnum == 0
  94776. + && pcd->ep0state ==
  94777. + EP0_OUT_STATUS_PHASE) {
  94778. + // EP0 IN set STALL
  94779. + depctl.d32 =
  94780. + DWC_READ_REG32(&dev_if->in_ep_regs
  94781. + [epnum]->diepctl);
  94782. +
  94783. + /* set the disable and stall bits */
  94784. + if (depctl.b.epena) {
  94785. + depctl.b.epdis = 1;
  94786. + }
  94787. + depctl.b.stall = 1;
  94788. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  94789. + [epnum]->diepctl,
  94790. + depctl.d32);
  94791. + }
  94792. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  94793. + }
  94794. + /** IN Token Received with EP mismatch */
  94795. + if (diepint.b.intknepmis) {
  94796. + DWC_DEBUGPL(DBG_ANY,
  94797. + "EP%d IN TKN EP Mismatch\n", epnum);
  94798. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  94799. + }
  94800. + /** IN Endpoint NAK Effective */
  94801. + if (diepint.b.inepnakeff) {
  94802. + DWC_DEBUGPL(DBG_ANY,
  94803. + "EP%d IN EP NAK Effective\n",
  94804. + epnum);
  94805. + /* Periodic EP */
  94806. + if (ep->disabling) {
  94807. + depctl.d32 = 0;
  94808. + depctl.b.snak = 1;
  94809. + depctl.b.epdis = 1;
  94810. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  94811. + [epnum]->diepctl,
  94812. + depctl.d32,
  94813. + depctl.d32);
  94814. + }
  94815. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  94816. +
  94817. + }
  94818. +
  94819. + /** IN EP Tx FIFO Empty Intr */
  94820. + if (diepint.b.emptyintr) {
  94821. + DWC_DEBUGPL(DBG_ANY,
  94822. + "EP%d Tx FIFO Empty Intr \n",
  94823. + epnum);
  94824. + write_empty_tx_fifo(pcd, epnum);
  94825. +
  94826. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  94827. +
  94828. + }
  94829. +
  94830. + /** IN EP BNA Intr */
  94831. + if (diepint.b.bna) {
  94832. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  94833. + if (core_if->dma_desc_enable) {
  94834. +#ifdef DWC_EN_ISOC
  94835. + if (dwc_ep->type ==
  94836. + DWC_OTG_EP_TYPE_ISOC) {
  94837. + /*
  94838. + * This checking is performed to prevent first "false" BNA
  94839. + * handling occuring right after reconnect
  94840. + */
  94841. + if (dwc_ep->next_frame !=
  94842. + 0xffffffff)
  94843. + dwc_otg_pcd_handle_iso_bna(ep);
  94844. + } else
  94845. +#endif /* DWC_EN_ISOC */
  94846. + {
  94847. + dwc_otg_pcd_handle_noniso_bna(ep);
  94848. + }
  94849. + }
  94850. + }
  94851. + /* NAK Interrutp */
  94852. + if (diepint.b.nak) {
  94853. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  94854. + epnum);
  94855. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  94856. + depctl_data_t depctl;
  94857. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  94858. + ep->dwc_ep.frame_num = core_if->frame_num;
  94859. + if (ep->dwc_ep.bInterval > 1) {
  94860. + depctl.d32 = 0;
  94861. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  94862. + if (ep->dwc_ep.frame_num & 0x1) {
  94863. + depctl.b.setd1pid = 1;
  94864. + depctl.b.setd0pid = 0;
  94865. + } else {
  94866. + depctl.b.setd0pid = 1;
  94867. + depctl.b.setd1pid = 0;
  94868. + }
  94869. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  94870. + }
  94871. + start_next_request(ep);
  94872. + }
  94873. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  94874. + if (dwc_ep->frame_num > 0x3FFF) {
  94875. + dwc_ep->frm_overrun = 1;
  94876. + dwc_ep->frame_num &= 0x3FFF;
  94877. + } else
  94878. + dwc_ep->frm_overrun = 0;
  94879. + }
  94880. +
  94881. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  94882. + }
  94883. + }
  94884. + epnum++;
  94885. + ep_intr >>= 1;
  94886. + }
  94887. +
  94888. + return 1;
  94889. +#undef CLEAR_IN_EP_INTR
  94890. +}
  94891. +
  94892. +/**
  94893. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  94894. + * The sequence for handling the OUT EP interrupt is shown below:
  94895. + * -# Read the Device All Endpoint Interrupt register
  94896. + * -# Repeat the following for each OUT EP interrupt bit set (from
  94897. + * LSB to MSB).
  94898. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  94899. + * -# If "Transfer Complete" call the request complete function
  94900. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  94901. + * -# If "AHB Error Interrupt" log error
  94902. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  94903. + * Command Processing)
  94904. + */
  94905. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  94906. +{
  94907. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  94908. +do { \
  94909. + doepint_data_t doepint = {.d32=0}; \
  94910. + doepint.b.__intr = 1; \
  94911. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  94912. + doepint.d32); \
  94913. +} while (0)
  94914. +
  94915. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94916. + uint32_t ep_intr;
  94917. + doepint_data_t doepint = {.d32 = 0 };
  94918. + uint32_t epnum = 0;
  94919. + dwc_otg_pcd_ep_t *ep;
  94920. + dwc_ep_t *dwc_ep;
  94921. + dctl_data_t dctl = {.d32 = 0 };
  94922. + gintmsk_data_t gintmsk = {.d32 = 0 };
  94923. +
  94924. +
  94925. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  94926. +
  94927. + /* Read in the device interrupt bits */
  94928. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  94929. +
  94930. + while (ep_intr) {
  94931. + if (ep_intr & 0x1) {
  94932. + /* Get EP pointer */
  94933. + ep = get_out_ep(pcd, epnum);
  94934. + dwc_ep = &ep->dwc_ep;
  94935. +
  94936. +#ifdef VERBOSE
  94937. + DWC_DEBUGPL(DBG_PCDV,
  94938. + "EP%d-%s: type=%d, mps=%d\n",
  94939. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  94940. + dwc_ep->type, dwc_ep->maxpacket);
  94941. +#endif
  94942. + doepint.d32 =
  94943. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  94944. + /* Moved this interrupt upper due to core deffect of asserting
  94945. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  94946. + if (doepint.b.stsphsercvd) {
  94947. + deptsiz0_data_t deptsiz;
  94948. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  94949. + deptsiz.d32 =
  94950. + DWC_READ_REG32(&core_if->dev_if->
  94951. + out_ep_regs[0]->doeptsiz);
  94952. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  94953. + && core_if->dma_enable
  94954. + && core_if->dma_desc_enable == 0
  94955. + && doepint.b.xfercompl
  94956. + && deptsiz.b.xfersize == 24) {
  94957. + CLEAR_OUT_EP_INTR(core_if, epnum,
  94958. + xfercompl);
  94959. + doepint.b.xfercompl = 0;
  94960. + ep0_out_start(core_if, pcd);
  94961. + }
  94962. + if ((core_if->dma_desc_enable) ||
  94963. + (core_if->dma_enable
  94964. + && core_if->snpsid >=
  94965. + OTG_CORE_REV_3_00a)) {
  94966. + do_setup_in_status_phase(pcd);
  94967. + }
  94968. + }
  94969. + /* Transfer complete */
  94970. + if (doepint.b.xfercompl) {
  94971. +
  94972. + if (epnum == 0) {
  94973. + /* Clear the bit in DOEPINTn for this interrupt */
  94974. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  94975. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  94976. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  94977. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  94978. + doepint.d32);
  94979. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  94980. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  94981. +
  94982. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  94983. + && core_if->dma_enable == 0) {
  94984. + doepint_data_t doepint;
  94985. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  94986. + out_ep_regs[0]->doepint);
  94987. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  94988. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  94989. + goto exit_xfercompl;
  94990. + }
  94991. + }
  94992. + /* In case of DDMA look at SR bit to go to the Data Stage */
  94993. + if (core_if->dma_desc_enable) {
  94994. + dev_dma_desc_sts_t status = {.d32 = 0};
  94995. + if (pcd->ep0state == EP0_IDLE) {
  94996. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  94997. + dev_if->setup_desc_index]->status.d32;
  94998. + if(pcd->data_terminated) {
  94999. + pcd->data_terminated = 0;
  95000. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  95001. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  95002. + }
  95003. + if (status.b.sr) {
  95004. + if (doepint.b.setup) {
  95005. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  95006. + /* Already started data stage, clear setup */
  95007. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  95008. + doepint.b.setup = 0;
  95009. + handle_ep0(pcd);
  95010. + /* Prepare for more setup packets */
  95011. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  95012. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  95013. + ep0_out_start(core_if, pcd);
  95014. + }
  95015. +
  95016. + goto exit_xfercompl;
  95017. + } else {
  95018. + /* Prepare for more setup packets */
  95019. + DWC_DEBUGPL(DBG_PCDV,
  95020. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  95021. + ep0_out_start(core_if, pcd);
  95022. + }
  95023. + }
  95024. + } else {
  95025. + dwc_otg_pcd_request_t *req;
  95026. + dev_dma_desc_sts_t status = {.d32 = 0};
  95027. + diepint_data_t diepint0;
  95028. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  95029. + in_ep_regs[0]->diepint);
  95030. +
  95031. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  95032. + DWC_ERROR("EP0 is stalled/disconnected\n");
  95033. + }
  95034. +
  95035. + /* Clear IN xfercompl if set */
  95036. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  95037. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  95038. + DWC_WRITE_REG32(&core_if->dev_if->
  95039. + in_ep_regs[0]->diepint, diepint0.d32);
  95040. + }
  95041. +
  95042. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  95043. + dev_if->setup_desc_index]->status.d32;
  95044. +
  95045. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  95046. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  95047. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  95048. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  95049. + status.d32 = core_if->dev_if->
  95050. + out_desc_addr->status.d32;
  95051. +
  95052. + if (status.b.sr) {
  95053. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  95054. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  95055. + } else {
  95056. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  95057. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  95058. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  95059. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  95060. + /* Read arrived setup packet from req->buf */
  95061. + dwc_memcpy(&pcd->setup_pkt->req,
  95062. + req->buf + ep->dwc_ep.xfer_count, 8);
  95063. + }
  95064. + req->actual = ep->dwc_ep.xfer_count;
  95065. + dwc_otg_request_done(ep, req, -ECONNRESET);
  95066. + ep->dwc_ep.start_xfer_buff = 0;
  95067. + ep->dwc_ep.xfer_buff = 0;
  95068. + ep->dwc_ep.xfer_len = 0;
  95069. + }
  95070. + pcd->ep0state = EP0_IDLE;
  95071. + if (doepint.b.setup) {
  95072. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  95073. + /* Data stage started, clear setup */
  95074. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  95075. + doepint.b.setup = 0;
  95076. + handle_ep0(pcd);
  95077. + /* Prepare for setup packets if ep0in was enabled*/
  95078. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  95079. + ep0_out_start(core_if, pcd);
  95080. + }
  95081. +
  95082. + goto exit_xfercompl;
  95083. + } else {
  95084. + /* Prepare for more setup packets */
  95085. + DWC_DEBUGPL(DBG_PCDV,
  95086. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  95087. + ep0_out_start(core_if, pcd);
  95088. + }
  95089. + }
  95090. + }
  95091. + }
  95092. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  95093. + && core_if->dma_desc_enable == 0) {
  95094. + doepint_data_t doepint_temp = {.d32 = 0};
  95095. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  95096. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  95097. + out_ep_regs[ep->dwc_ep.num]->doepint);
  95098. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  95099. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  95100. + if (pcd->ep0state == EP0_IDLE) {
  95101. + if (doepint_temp.b.sr) {
  95102. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  95103. + }
  95104. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  95105. + out_ep_regs[0]->doepint);
  95106. + if (doeptsize0.b.supcnt == 3) {
  95107. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  95108. + ep->dwc_ep.stp_rollover = 1;
  95109. + }
  95110. + if (doepint.b.setup) {
  95111. +retry:
  95112. + /* Already started data stage, clear setup */
  95113. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  95114. + doepint.b.setup = 0;
  95115. + handle_ep0(pcd);
  95116. + ep->dwc_ep.stp_rollover = 0;
  95117. + /* Prepare for more setup packets */
  95118. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  95119. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  95120. + ep0_out_start(core_if, pcd);
  95121. + }
  95122. + goto exit_xfercompl;
  95123. + } else {
  95124. + /* Prepare for more setup packets */
  95125. + DWC_DEBUGPL(DBG_ANY,
  95126. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  95127. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  95128. + out_ep_regs[0]->doepint);
  95129. + if(doepint.b.setup)
  95130. + goto retry;
  95131. + ep0_out_start(core_if, pcd);
  95132. + }
  95133. + } else {
  95134. + dwc_otg_pcd_request_t *req;
  95135. + diepint_data_t diepint0 = {.d32 = 0};
  95136. + doepint_data_t doepint_temp = {.d32 = 0};
  95137. + depctl_data_t diepctl0;
  95138. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  95139. + in_ep_regs[0]->diepint);
  95140. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  95141. + in_ep_regs[0]->diepctl);
  95142. +
  95143. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  95144. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  95145. + if (diepint0.b.xfercompl) {
  95146. + DWC_WRITE_REG32(&core_if->dev_if->
  95147. + in_ep_regs[0]->diepint, diepint0.d32);
  95148. + }
  95149. + if (diepctl0.b.epena) {
  95150. + diepint_data_t diepint = {.d32 = 0};
  95151. + diepctl0.b.snak = 1;
  95152. + DWC_WRITE_REG32(&core_if->dev_if->
  95153. + in_ep_regs[0]->diepctl, diepctl0.d32);
  95154. + do {
  95155. + dwc_udelay(10);
  95156. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  95157. + in_ep_regs[0]->diepint);
  95158. + } while (!diepint.b.inepnakeff);
  95159. + diepint.b.inepnakeff = 1;
  95160. + DWC_WRITE_REG32(&core_if->dev_if->
  95161. + in_ep_regs[0]->diepint, diepint.d32);
  95162. + diepctl0.d32 = 0;
  95163. + diepctl0.b.epdis = 1;
  95164. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  95165. + diepctl0.d32);
  95166. + do {
  95167. + dwc_udelay(10);
  95168. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  95169. + in_ep_regs[0]->diepint);
  95170. + } while (!diepint.b.epdisabled);
  95171. + diepint.b.epdisabled = 1;
  95172. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  95173. + diepint.d32);
  95174. + }
  95175. + }
  95176. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  95177. + out_ep_regs[ep->dwc_ep.num]->doepint);
  95178. + if (doepint_temp.b.sr) {
  95179. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  95180. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  95181. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  95182. + } else {
  95183. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  95184. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  95185. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  95186. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  95187. + /* Read arrived setup packet from req->buf */
  95188. + dwc_memcpy(&pcd->setup_pkt->req,
  95189. + req->buf + ep->dwc_ep.xfer_count, 8);
  95190. + }
  95191. + req->actual = ep->dwc_ep.xfer_count;
  95192. + dwc_otg_request_done(ep, req, -ECONNRESET);
  95193. + ep->dwc_ep.start_xfer_buff = 0;
  95194. + ep->dwc_ep.xfer_buff = 0;
  95195. + ep->dwc_ep.xfer_len = 0;
  95196. + }
  95197. + pcd->ep0state = EP0_IDLE;
  95198. + if (doepint.b.setup) {
  95199. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  95200. + /* Data stage started, clear setup */
  95201. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  95202. + doepint.b.setup = 0;
  95203. + handle_ep0(pcd);
  95204. + /* Prepare for setup packets if ep0in was enabled*/
  95205. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  95206. + ep0_out_start(core_if, pcd);
  95207. + }
  95208. + goto exit_xfercompl;
  95209. + } else {
  95210. + /* Prepare for more setup packets */
  95211. + DWC_DEBUGPL(DBG_PCDV,
  95212. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  95213. + ep0_out_start(core_if, pcd);
  95214. + }
  95215. + }
  95216. + }
  95217. + }
  95218. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  95219. + handle_ep0(pcd);
  95220. +exit_xfercompl:
  95221. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  95222. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  95223. + } else {
  95224. + if (core_if->dma_desc_enable == 0
  95225. + || pcd->ep0state != EP0_IDLE)
  95226. + handle_ep0(pcd);
  95227. + }
  95228. +#ifdef DWC_EN_ISOC
  95229. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95230. + if (doepint.b.pktdrpsts == 0) {
  95231. + /* Clear the bit in DOEPINTn for this interrupt */
  95232. + CLEAR_OUT_EP_INTR(core_if,
  95233. + epnum,
  95234. + xfercompl);
  95235. + complete_iso_ep(pcd, ep);
  95236. + } else {
  95237. +
  95238. + doepint_data_t doepint = {.d32 = 0 };
  95239. + doepint.b.xfercompl = 1;
  95240. + doepint.b.pktdrpsts = 1;
  95241. + DWC_WRITE_REG32
  95242. + (&core_if->dev_if->out_ep_regs
  95243. + [epnum]->doepint,
  95244. + doepint.d32);
  95245. + if (handle_iso_out_pkt_dropped
  95246. + (core_if, dwc_ep)) {
  95247. + complete_iso_ep(pcd,
  95248. + ep);
  95249. + }
  95250. + }
  95251. +#endif /* DWC_EN_ISOC */
  95252. +#ifdef DWC_UTE_PER_IO
  95253. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95254. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  95255. + if (!ep->stopped)
  95256. + complete_xiso_ep(ep);
  95257. +#endif /* DWC_UTE_PER_IO */
  95258. + } else {
  95259. + /* Clear the bit in DOEPINTn for this interrupt */
  95260. + CLEAR_OUT_EP_INTR(core_if, epnum,
  95261. + xfercompl);
  95262. +
  95263. + if (core_if->core_params->dev_out_nak) {
  95264. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  95265. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  95266. +#ifdef DEBUG
  95267. + print_memory_payload(pcd, dwc_ep);
  95268. +#endif
  95269. + }
  95270. + complete_ep(ep);
  95271. + }
  95272. +
  95273. + }
  95274. +
  95275. + /* Endpoint disable */
  95276. + if (doepint.b.epdisabled) {
  95277. +
  95278. + /* Clear the bit in DOEPINTn for this interrupt */
  95279. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  95280. + if (core_if->core_params->dev_out_nak) {
  95281. +#ifdef DEBUG
  95282. + print_memory_payload(pcd, dwc_ep);
  95283. +#endif
  95284. + /* In case of timeout condition */
  95285. + if (core_if->ep_xfer_info[epnum].state == 2) {
  95286. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  95287. + dev_global_regs->dctl);
  95288. + dctl.b.cgoutnak = 1;
  95289. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  95290. + dctl.d32);
  95291. + /* Unmask goutnakeff interrupt which was masked
  95292. + * during handle nak out interrupt */
  95293. + gintmsk.b.goutnakeff = 1;
  95294. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  95295. + 0, gintmsk.d32);
  95296. +
  95297. + complete_ep(ep);
  95298. + }
  95299. + }
  95300. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  95301. + {
  95302. + dctl_data_t dctl;
  95303. + gintmsk_data_t intr_mask = {.d32 = 0};
  95304. + dwc_otg_pcd_request_t *req = 0;
  95305. +
  95306. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  95307. + dev_global_regs->dctl);
  95308. + dctl.b.cgoutnak = 1;
  95309. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  95310. + dctl.d32);
  95311. +
  95312. + intr_mask.d32 = 0;
  95313. + intr_mask.b.incomplisoout = 1;
  95314. +
  95315. + /* Get any pending requests */
  95316. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  95317. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  95318. + if (!req) {
  95319. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  95320. + } else {
  95321. + dwc_otg_request_done(ep, req, 0);
  95322. + start_next_request(ep);
  95323. + }
  95324. + } else {
  95325. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  95326. + }
  95327. + }
  95328. + }
  95329. + /* AHB Error */
  95330. + if (doepint.b.ahberr) {
  95331. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  95332. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  95333. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  95334. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  95335. + }
  95336. + /* Setup Phase Done (contorl EPs) */
  95337. + if (doepint.b.setup) {
  95338. +#ifdef DEBUG_EP0
  95339. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  95340. +#endif
  95341. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  95342. +
  95343. + handle_ep0(pcd);
  95344. + }
  95345. +
  95346. + /** OUT EP BNA Intr */
  95347. + if (doepint.b.bna) {
  95348. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  95349. + if (core_if->dma_desc_enable) {
  95350. +#ifdef DWC_EN_ISOC
  95351. + if (dwc_ep->type ==
  95352. + DWC_OTG_EP_TYPE_ISOC) {
  95353. + /*
  95354. + * This checking is performed to prevent first "false" BNA
  95355. + * handling occuring right after reconnect
  95356. + */
  95357. + if (dwc_ep->next_frame !=
  95358. + 0xffffffff)
  95359. + dwc_otg_pcd_handle_iso_bna(ep);
  95360. + } else
  95361. +#endif /* DWC_EN_ISOC */
  95362. + {
  95363. + dwc_otg_pcd_handle_noniso_bna(ep);
  95364. + }
  95365. + }
  95366. + }
  95367. + /* Babble Interrupt */
  95368. + if (doepint.b.babble) {
  95369. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  95370. + epnum);
  95371. + handle_out_ep_babble_intr(pcd, epnum);
  95372. +
  95373. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  95374. + }
  95375. + if (doepint.b.outtknepdis) {
  95376. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  95377. + disabled\n",epnum);
  95378. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  95379. + doepmsk_data_t doepmsk = {.d32 = 0};
  95380. + ep->dwc_ep.frame_num = core_if->frame_num;
  95381. + if (ep->dwc_ep.bInterval > 1) {
  95382. + depctl_data_t depctl;
  95383. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  95384. + out_ep_regs[epnum]->doepctl);
  95385. + if (ep->dwc_ep.frame_num & 0x1) {
  95386. + depctl.b.setd1pid = 1;
  95387. + depctl.b.setd0pid = 0;
  95388. + } else {
  95389. + depctl.b.setd0pid = 1;
  95390. + depctl.b.setd1pid = 0;
  95391. + }
  95392. + DWC_WRITE_REG32(&core_if->dev_if->
  95393. + out_ep_regs[epnum]->doepctl, depctl.d32);
  95394. + }
  95395. + start_next_request(ep);
  95396. + doepmsk.b.outtknepdis = 1;
  95397. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  95398. + doepmsk.d32, 0);
  95399. + }
  95400. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  95401. + }
  95402. +
  95403. + /* NAK Interrutp */
  95404. + if (doepint.b.nak) {
  95405. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  95406. + handle_out_ep_nak_intr(pcd, epnum);
  95407. +
  95408. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  95409. + }
  95410. + /* NYET Interrutp */
  95411. + if (doepint.b.nyet) {
  95412. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  95413. + handle_out_ep_nyet_intr(pcd, epnum);
  95414. +
  95415. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  95416. + }
  95417. + }
  95418. +
  95419. + epnum++;
  95420. + ep_intr >>= 1;
  95421. + }
  95422. +
  95423. + return 1;
  95424. +
  95425. +#undef CLEAR_OUT_EP_INTR
  95426. +}
  95427. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  95428. +{
  95429. + int retval = 0;
  95430. + if(!frm_overrun && curr_fr >= trgt_fr)
  95431. + retval = 1;
  95432. + else if (frm_overrun
  95433. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  95434. + retval = 1;
  95435. + return retval;
  95436. +}
  95437. +/**
  95438. + * Incomplete ISO IN Transfer Interrupt.
  95439. + * This interrupt indicates one of the following conditions occurred
  95440. + * while transmitting an ISOC transaction.
  95441. + * - Corrupted IN Token for ISOC EP.
  95442. + * - Packet not complete in FIFO.
  95443. + * The follow actions will be taken:
  95444. + * -# Determine the EP
  95445. + * -# Set incomplete flag in dwc_ep structure
  95446. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  95447. + * Flush FIFO
  95448. + */
  95449. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  95450. +{
  95451. + gintsts_data_t gintsts;
  95452. +
  95453. +#ifdef DWC_EN_ISOC
  95454. + dwc_otg_dev_if_t *dev_if;
  95455. + deptsiz_data_t deptsiz = {.d32 = 0 };
  95456. + depctl_data_t depctl = {.d32 = 0 };
  95457. + dsts_data_t dsts = {.d32 = 0 };
  95458. + dwc_ep_t *dwc_ep;
  95459. + int i;
  95460. +
  95461. + dev_if = GET_CORE_IF(pcd)->dev_if;
  95462. +
  95463. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  95464. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  95465. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95466. + deptsiz.d32 =
  95467. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  95468. + depctl.d32 =
  95469. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95470. +
  95471. + if (depctl.b.epdis && deptsiz.d32) {
  95472. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  95473. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  95474. + dwc_ep->cur_pkt = 0;
  95475. + dwc_ep->proc_buf_num =
  95476. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  95477. +
  95478. + if (dwc_ep->proc_buf_num) {
  95479. + dwc_ep->cur_pkt_addr =
  95480. + dwc_ep->xfer_buff1;
  95481. + dwc_ep->cur_pkt_dma_addr =
  95482. + dwc_ep->dma_addr1;
  95483. + } else {
  95484. + dwc_ep->cur_pkt_addr =
  95485. + dwc_ep->xfer_buff0;
  95486. + dwc_ep->cur_pkt_dma_addr =
  95487. + dwc_ep->dma_addr0;
  95488. + }
  95489. +
  95490. + }
  95491. +
  95492. + dsts.d32 =
  95493. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  95494. + dev_global_regs->dsts);
  95495. + dwc_ep->next_frame = dsts.b.soffn;
  95496. +
  95497. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  95498. + (pcd),
  95499. + dwc_ep);
  95500. + }
  95501. + }
  95502. + }
  95503. +
  95504. +#else
  95505. + depctl_data_t depctl = {.d32 = 0 };
  95506. + dwc_ep_t *dwc_ep;
  95507. + dwc_otg_dev_if_t *dev_if;
  95508. + int i;
  95509. + dev_if = GET_CORE_IF(pcd)->dev_if;
  95510. +
  95511. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  95512. +
  95513. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  95514. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  95515. + depctl.d32 =
  95516. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95517. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95518. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  95519. + dwc_ep->frm_overrun))
  95520. + {
  95521. + depctl.d32 =
  95522. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95523. + depctl.b.snak = 1;
  95524. + depctl.b.epdis = 1;
  95525. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  95526. + }
  95527. + }
  95528. + }
  95529. +
  95530. + /*intr_mask.b.incomplisoin = 1;
  95531. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  95532. + intr_mask.d32, 0); */
  95533. +#endif //DWC_EN_ISOC
  95534. +
  95535. + /* Clear interrupt */
  95536. + gintsts.d32 = 0;
  95537. + gintsts.b.incomplisoin = 1;
  95538. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  95539. + gintsts.d32);
  95540. +
  95541. + return 1;
  95542. +}
  95543. +
  95544. +/**
  95545. + * Incomplete ISO OUT Transfer Interrupt.
  95546. + *
  95547. + * This interrupt indicates that the core has dropped an ISO OUT
  95548. + * packet. The following conditions can be the cause:
  95549. + * - FIFO Full, the entire packet would not fit in the FIFO.
  95550. + * - CRC Error
  95551. + * - Corrupted Token
  95552. + * The follow actions will be taken:
  95553. + * -# Determine the EP
  95554. + * -# Set incomplete flag in dwc_ep structure
  95555. + * -# Read any data from the FIFO
  95556. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  95557. + * re-enable EP.
  95558. + */
  95559. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  95560. +{
  95561. +
  95562. + gintsts_data_t gintsts;
  95563. +
  95564. +#ifdef DWC_EN_ISOC
  95565. + dwc_otg_dev_if_t *dev_if;
  95566. + deptsiz_data_t deptsiz = {.d32 = 0 };
  95567. + depctl_data_t depctl = {.d32 = 0 };
  95568. + dsts_data_t dsts = {.d32 = 0 };
  95569. + dwc_ep_t *dwc_ep;
  95570. + int i;
  95571. +
  95572. + dev_if = GET_CORE_IF(pcd)->dev_if;
  95573. +
  95574. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  95575. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  95576. + if (pcd->out_ep[i].dwc_ep.active &&
  95577. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  95578. + deptsiz.d32 =
  95579. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  95580. + depctl.d32 =
  95581. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  95582. +
  95583. + if (depctl.b.epdis && deptsiz.d32) {
  95584. + set_current_pkt_info(GET_CORE_IF(pcd),
  95585. + &pcd->out_ep[i].dwc_ep);
  95586. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  95587. + dwc_ep->cur_pkt = 0;
  95588. + dwc_ep->proc_buf_num =
  95589. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  95590. +
  95591. + if (dwc_ep->proc_buf_num) {
  95592. + dwc_ep->cur_pkt_addr =
  95593. + dwc_ep->xfer_buff1;
  95594. + dwc_ep->cur_pkt_dma_addr =
  95595. + dwc_ep->dma_addr1;
  95596. + } else {
  95597. + dwc_ep->cur_pkt_addr =
  95598. + dwc_ep->xfer_buff0;
  95599. + dwc_ep->cur_pkt_dma_addr =
  95600. + dwc_ep->dma_addr0;
  95601. + }
  95602. +
  95603. + }
  95604. +
  95605. + dsts.d32 =
  95606. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  95607. + dev_global_regs->dsts);
  95608. + dwc_ep->next_frame = dsts.b.soffn;
  95609. +
  95610. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  95611. + (pcd),
  95612. + dwc_ep);
  95613. + }
  95614. + }
  95615. + }
  95616. +#else
  95617. + /** @todo implement ISR */
  95618. + gintmsk_data_t intr_mask = {.d32 = 0 };
  95619. + dwc_otg_core_if_t *core_if;
  95620. + deptsiz_data_t deptsiz = {.d32 = 0 };
  95621. + depctl_data_t depctl = {.d32 = 0 };
  95622. + dctl_data_t dctl = {.d32 = 0 };
  95623. + dwc_ep_t *dwc_ep = NULL;
  95624. + int i;
  95625. + core_if = GET_CORE_IF(pcd);
  95626. +
  95627. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  95628. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  95629. + depctl.d32 =
  95630. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  95631. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  95632. + core_if->dev_if->isoc_ep = dwc_ep;
  95633. + deptsiz.d32 =
  95634. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  95635. + break;
  95636. + }
  95637. + }
  95638. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  95639. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  95640. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  95641. +
  95642. + if (!intr_mask.b.goutnakeff) {
  95643. + /* Unmask it */
  95644. + intr_mask.b.goutnakeff = 1;
  95645. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  95646. + }
  95647. + if (!gintsts.b.goutnakeff) {
  95648. + dctl.b.sgoutnak = 1;
  95649. + }
  95650. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  95651. +
  95652. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  95653. + if (depctl.b.epena) {
  95654. + depctl.b.epdis = 1;
  95655. + depctl.b.snak = 1;
  95656. + }
  95657. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  95658. +
  95659. + intr_mask.d32 = 0;
  95660. + intr_mask.b.incomplisoout = 1;
  95661. +
  95662. +#endif /* DWC_EN_ISOC */
  95663. +
  95664. + /* Clear interrupt */
  95665. + gintsts.d32 = 0;
  95666. + gintsts.b.incomplisoout = 1;
  95667. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  95668. + gintsts.d32);
  95669. +
  95670. + return 1;
  95671. +}
  95672. +
  95673. +/**
  95674. + * This function handles the Global IN NAK Effective interrupt.
  95675. + *
  95676. + */
  95677. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  95678. +{
  95679. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  95680. + depctl_data_t diepctl = {.d32 = 0 };
  95681. + gintmsk_data_t intr_mask = {.d32 = 0 };
  95682. + gintsts_data_t gintsts;
  95683. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  95684. + int i;
  95685. +
  95686. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  95687. +
  95688. + /* Disable all active IN EPs */
  95689. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  95690. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95691. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  95692. + if (core_if->start_predict > 0)
  95693. + core_if->start_predict++;
  95694. + diepctl.b.epdis = 1;
  95695. + diepctl.b.snak = 1;
  95696. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  95697. + }
  95698. + }
  95699. +
  95700. +
  95701. + /* Disable the Global IN NAK Effective Interrupt */
  95702. + intr_mask.b.ginnakeff = 1;
  95703. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  95704. + intr_mask.d32, 0);
  95705. +
  95706. + /* Clear interrupt */
  95707. + gintsts.d32 = 0;
  95708. + gintsts.b.ginnakeff = 1;
  95709. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  95710. + gintsts.d32);
  95711. +
  95712. + return 1;
  95713. +}
  95714. +
  95715. +/**
  95716. + * OUT NAK Effective.
  95717. + *
  95718. + */
  95719. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  95720. +{
  95721. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  95722. + gintmsk_data_t intr_mask = {.d32 = 0 };
  95723. + gintsts_data_t gintsts;
  95724. + depctl_data_t doepctl;
  95725. + int i;
  95726. +
  95727. + /* Disable the Global OUT NAK Effective Interrupt */
  95728. + intr_mask.b.goutnakeff = 1;
  95729. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  95730. + intr_mask.d32, 0);
  95731. +
  95732. + /* If DEV OUT NAK enabled*/
  95733. + if (pcd->core_if->core_params->dev_out_nak) {
  95734. + /* Run over all out endpoints to determine the ep number on
  95735. + * which the timeout has happened
  95736. + */
  95737. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  95738. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  95739. + break;
  95740. + }
  95741. + if (i > dev_if->num_out_eps) {
  95742. + dctl_data_t dctl;
  95743. + dctl.d32 =
  95744. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  95745. + dctl.b.cgoutnak = 1;
  95746. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  95747. + dctl.d32);
  95748. + goto out;
  95749. + }
  95750. +
  95751. + /* Disable the endpoint */
  95752. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  95753. + if (doepctl.b.epena) {
  95754. + doepctl.b.epdis = 1;
  95755. + doepctl.b.snak = 1;
  95756. + }
  95757. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  95758. + return 1;
  95759. + }
  95760. + /* We come here from Incomplete ISO OUT handler */
  95761. + if (dev_if->isoc_ep) {
  95762. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  95763. + uint32_t epnum = dwc_ep->num;
  95764. + doepint_data_t doepint;
  95765. + doepint.d32 =
  95766. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  95767. + dev_if->isoc_ep = NULL;
  95768. + doepctl.d32 =
  95769. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  95770. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  95771. + if (doepctl.b.epena) {
  95772. + doepctl.b.epdis = 1;
  95773. + doepctl.b.snak = 1;
  95774. + }
  95775. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  95776. + doepctl.d32);
  95777. + return 1;
  95778. + } else
  95779. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  95780. + "Global OUT NAK Effective\n");
  95781. +
  95782. +out:
  95783. + /* Clear interrupt */
  95784. + gintsts.d32 = 0;
  95785. + gintsts.b.goutnakeff = 1;
  95786. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  95787. + gintsts.d32);
  95788. +
  95789. + return 1;
  95790. +}
  95791. +
  95792. +/**
  95793. + * PCD interrupt handler.
  95794. + *
  95795. + * The PCD handles the device interrupts. Many conditions can cause a
  95796. + * device interrupt. When an interrupt occurs, the device interrupt
  95797. + * service routine determines the cause of the interrupt and
  95798. + * dispatches handling to the appropriate function. These interrupt
  95799. + * handling functions are described below.
  95800. + *
  95801. + * All interrupt registers are processed from LSB to MSB.
  95802. + *
  95803. + */
  95804. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  95805. +{
  95806. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  95807. +#ifdef VERBOSE
  95808. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  95809. +#endif
  95810. + gintsts_data_t gintr_status;
  95811. + int32_t retval = 0;
  95812. +
  95813. + /* Exit from ISR if core is hibernated */
  95814. + if (core_if->hibernation_suspend == 1) {
  95815. + return retval;
  95816. + }
  95817. +#ifdef VERBOSE
  95818. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  95819. + __func__,
  95820. + DWC_READ_REG32(&global_regs->gintsts),
  95821. + DWC_READ_REG32(&global_regs->gintmsk));
  95822. +#endif
  95823. +
  95824. + if (dwc_otg_is_device_mode(core_if)) {
  95825. + DWC_SPINLOCK(pcd->lock);
  95826. +#ifdef VERBOSE
  95827. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  95828. + __func__,
  95829. + DWC_READ_REG32(&global_regs->gintsts),
  95830. + DWC_READ_REG32(&global_regs->gintmsk));
  95831. +#endif
  95832. +
  95833. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  95834. +
  95835. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  95836. + __func__, gintr_status.d32);
  95837. +
  95838. + if (gintr_status.b.sofintr) {
  95839. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  95840. + }
  95841. + if (gintr_status.b.rxstsqlvl) {
  95842. + retval |=
  95843. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  95844. + }
  95845. + if (gintr_status.b.nptxfempty) {
  95846. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  95847. + }
  95848. + if (gintr_status.b.goutnakeff) {
  95849. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  95850. + }
  95851. + if (gintr_status.b.i2cintr) {
  95852. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  95853. + }
  95854. + if (gintr_status.b.erlysuspend) {
  95855. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  95856. + }
  95857. + if (gintr_status.b.usbreset) {
  95858. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  95859. + }
  95860. + if (gintr_status.b.enumdone) {
  95861. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  95862. + }
  95863. + if (gintr_status.b.isooutdrop) {
  95864. + retval |=
  95865. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  95866. + (pcd);
  95867. + }
  95868. + if (gintr_status.b.eopframe) {
  95869. + retval |=
  95870. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  95871. + }
  95872. + if (gintr_status.b.inepint) {
  95873. + if (!core_if->multiproc_int_enable) {
  95874. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  95875. + }
  95876. + }
  95877. + if (gintr_status.b.outepintr) {
  95878. + if (!core_if->multiproc_int_enable) {
  95879. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  95880. + }
  95881. + }
  95882. + if (gintr_status.b.epmismatch) {
  95883. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  95884. + }
  95885. + if (gintr_status.b.fetsusp) {
  95886. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  95887. + }
  95888. + if (gintr_status.b.ginnakeff) {
  95889. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  95890. + }
  95891. + if (gintr_status.b.incomplisoin) {
  95892. + retval |=
  95893. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  95894. + }
  95895. + if (gintr_status.b.incomplisoout) {
  95896. + retval |=
  95897. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  95898. + }
  95899. +
  95900. + /* In MPI mode Device Endpoints interrupts are asserted
  95901. + * without setting outepintr and inepint bits set, so these
  95902. + * Interrupt handlers are called without checking these bit-fields
  95903. + */
  95904. + if (core_if->multiproc_int_enable) {
  95905. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  95906. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  95907. + }
  95908. +#ifdef VERBOSE
  95909. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  95910. + DWC_READ_REG32(&global_regs->gintsts));
  95911. +#endif
  95912. + DWC_SPINUNLOCK(pcd->lock);
  95913. + }
  95914. + return retval;
  95915. +}
  95916. +
  95917. +#endif /* DWC_HOST_ONLY */
  95918. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  95919. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 1969-12-31 18:00:00.000000000 -0600
  95920. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 2014-12-11 14:02:55.400418001 -0600
  95921. @@ -0,0 +1,1360 @@
  95922. + /* ==========================================================================
  95923. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  95924. + * $Revision: #21 $
  95925. + * $Date: 2012/08/10 $
  95926. + * $Change: 2047372 $
  95927. + *
  95928. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  95929. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  95930. + * otherwise expressly agreed to in writing between Synopsys and you.
  95931. + *
  95932. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  95933. + * any End User Software License Agreement or Agreement for Licensed Product
  95934. + * with Synopsys or any supplement thereto. You are permitted to use and
  95935. + * redistribute this Software in source and binary forms, with or without
  95936. + * modification, provided that redistributions of source code must retain this
  95937. + * notice. You may not view, use, disclose, copy or distribute this file or
  95938. + * any information contained herein except pursuant to this license grant from
  95939. + * Synopsys. If you do not agree with this notice, including the disclaimer
  95940. + * below, then you are not authorized to use the Software.
  95941. + *
  95942. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  95943. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  95944. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  95945. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  95946. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  95947. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  95948. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  95949. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  95950. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  95951. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  95952. + * DAMAGE.
  95953. + * ========================================================================== */
  95954. +#ifndef DWC_HOST_ONLY
  95955. +
  95956. +/** @file
  95957. + * This file implements the Peripheral Controller Driver.
  95958. + *
  95959. + * The Peripheral Controller Driver (PCD) is responsible for
  95960. + * translating requests from the Function Driver into the appropriate
  95961. + * actions on the DWC_otg controller. It isolates the Function Driver
  95962. + * from the specifics of the controller by providing an API to the
  95963. + * Function Driver.
  95964. + *
  95965. + * The Peripheral Controller Driver for Linux will implement the
  95966. + * Gadget API, so that the existing Gadget drivers can be used.
  95967. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  95968. + *
  95969. + * The Linux Gadget API is defined in the header file
  95970. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  95971. + * defined in the structure <code>usb_ep_ops</code> and the USB
  95972. + * Controller API is defined in the structure
  95973. + * <code>usb_gadget_ops</code>.
  95974. + *
  95975. + */
  95976. +
  95977. +#include "dwc_otg_os_dep.h"
  95978. +#include "dwc_otg_pcd_if.h"
  95979. +#include "dwc_otg_pcd.h"
  95980. +#include "dwc_otg_driver.h"
  95981. +#include "dwc_otg_dbg.h"
  95982. +
  95983. +extern bool fiq_enable;
  95984. +
  95985. +static struct gadget_wrapper {
  95986. + dwc_otg_pcd_t *pcd;
  95987. +
  95988. + struct usb_gadget gadget;
  95989. + struct usb_gadget_driver *driver;
  95990. +
  95991. + struct usb_ep ep0;
  95992. + struct usb_ep in_ep[16];
  95993. + struct usb_ep out_ep[16];
  95994. +
  95995. +} *gadget_wrapper;
  95996. +
  95997. +/* Display the contents of the buffer */
  95998. +extern void dump_msg(const u8 * buf, unsigned int length);
  95999. +/**
  96000. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  96001. + * if the endpoint is not found
  96002. + */
  96003. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  96004. +{
  96005. + int i;
  96006. + if (pcd->ep0.priv == handle) {
  96007. + return &pcd->ep0;
  96008. + }
  96009. +
  96010. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  96011. + if (pcd->in_ep[i].priv == handle)
  96012. + return &pcd->in_ep[i];
  96013. + if (pcd->out_ep[i].priv == handle)
  96014. + return &pcd->out_ep[i];
  96015. + }
  96016. +
  96017. + return NULL;
  96018. +}
  96019. +
  96020. +/* USB Endpoint Operations */
  96021. +/*
  96022. + * The following sections briefly describe the behavior of the Gadget
  96023. + * API endpoint operations implemented in the DWC_otg driver
  96024. + * software. Detailed descriptions of the generic behavior of each of
  96025. + * these functions can be found in the Linux header file
  96026. + * include/linux/usb_gadget.h.
  96027. + *
  96028. + * The Gadget API provides wrapper functions for each of the function
  96029. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  96030. + * function, which then calls the underlying PCD function. The
  96031. + * following sections are named according to the wrapper
  96032. + * functions. Within each section, the corresponding DWC_otg PCD
  96033. + * function name is specified.
  96034. + *
  96035. + */
  96036. +
  96037. +/**
  96038. + * This function is called by the Gadget Driver for each EP to be
  96039. + * configured for the current configuration (SET_CONFIGURATION).
  96040. + *
  96041. + * This function initializes the dwc_otg_ep_t data structure, and then
  96042. + * calls dwc_otg_ep_activate.
  96043. + */
  96044. +static int ep_enable(struct usb_ep *usb_ep,
  96045. + const struct usb_endpoint_descriptor *ep_desc)
  96046. +{
  96047. + int retval;
  96048. +
  96049. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  96050. +
  96051. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  96052. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  96053. + return -EINVAL;
  96054. + }
  96055. + if (usb_ep == &gadget_wrapper->ep0) {
  96056. + DWC_WARN("%s, bad ep(0)\n", __func__);
  96057. + return -EINVAL;
  96058. + }
  96059. +
  96060. + /* Check FIFO size? */
  96061. + if (!ep_desc->wMaxPacketSize) {
  96062. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  96063. + return -ERANGE;
  96064. + }
  96065. +
  96066. + if (!gadget_wrapper->driver ||
  96067. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  96068. + DWC_WARN("%s, bogus device state\n", __func__);
  96069. + return -ESHUTDOWN;
  96070. + }
  96071. +
  96072. + /* Delete after check - MAS */
  96073. +#if 0
  96074. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  96075. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  96076. + nat = (nat >> 11) & 0x03;
  96077. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  96078. +#endif
  96079. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  96080. + (const uint8_t *)ep_desc,
  96081. + (void *)usb_ep);
  96082. + if (retval) {
  96083. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  96084. + return -EINVAL;
  96085. + }
  96086. +
  96087. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  96088. +
  96089. + return 0;
  96090. +}
  96091. +
  96092. +/**
  96093. + * This function is called when an EP is disabled due to disconnect or
  96094. + * change in configuration. Any pending requests will terminate with a
  96095. + * status of -ESHUTDOWN.
  96096. + *
  96097. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  96098. + * and then calls dwc_otg_ep_deactivate.
  96099. + */
  96100. +static int ep_disable(struct usb_ep *usb_ep)
  96101. +{
  96102. + int retval;
  96103. +
  96104. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  96105. + if (!usb_ep) {
  96106. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  96107. + usb_ep ? usb_ep->name : NULL);
  96108. + return -EINVAL;
  96109. + }
  96110. +
  96111. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  96112. + if (retval) {
  96113. + retval = -EINVAL;
  96114. + }
  96115. +
  96116. + return retval;
  96117. +}
  96118. +
  96119. +/**
  96120. + * This function allocates a request object to use with the specified
  96121. + * endpoint.
  96122. + *
  96123. + * @param ep The endpoint to be used with with the request
  96124. + * @param gfp_flags the GFP_* flags to use.
  96125. + */
  96126. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  96127. + gfp_t gfp_flags)
  96128. +{
  96129. + struct usb_request *usb_req;
  96130. +
  96131. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  96132. + if (0 == ep) {
  96133. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  96134. + return 0;
  96135. + }
  96136. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  96137. + if (0 == usb_req) {
  96138. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  96139. + return 0;
  96140. + }
  96141. + memset(usb_req, 0, sizeof(*usb_req));
  96142. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  96143. +
  96144. + return usb_req;
  96145. +}
  96146. +
  96147. +/**
  96148. + * This function frees a request object.
  96149. + *
  96150. + * @param ep The endpoint associated with the request
  96151. + * @param req The request being freed
  96152. + */
  96153. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  96154. +{
  96155. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  96156. +
  96157. + if (0 == ep || 0 == req) {
  96158. + DWC_WARN("%s() %s\n", __func__,
  96159. + "Invalid ep or req argument!\n");
  96160. + return;
  96161. + }
  96162. +
  96163. + kfree(req);
  96164. +}
  96165. +
  96166. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  96167. +/**
  96168. + * This function allocates an I/O buffer to be used for a transfer
  96169. + * to/from the specified endpoint.
  96170. + *
  96171. + * @param usb_ep The endpoint to be used with with the request
  96172. + * @param bytes The desired number of bytes for the buffer
  96173. + * @param dma Pointer to the buffer's DMA address; must be valid
  96174. + * @param gfp_flags the GFP_* flags to use.
  96175. + * @return address of a new buffer or null is buffer could not be allocated.
  96176. + */
  96177. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  96178. + dma_addr_t * dma, gfp_t gfp_flags)
  96179. +{
  96180. + void *buf;
  96181. + dwc_otg_pcd_t *pcd = 0;
  96182. +
  96183. + pcd = gadget_wrapper->pcd;
  96184. +
  96185. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  96186. + dma, gfp_flags);
  96187. +
  96188. + /* Check dword alignment */
  96189. + if ((bytes & 0x3UL) != 0) {
  96190. + DWC_WARN("%s() Buffer size is not a multiple of"
  96191. + "DWORD size (%d)", __func__, bytes);
  96192. + }
  96193. +
  96194. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  96195. +
  96196. + /* Check dword alignment */
  96197. + if (((int)buf & 0x3UL) != 0) {
  96198. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  96199. + __func__, buf);
  96200. + }
  96201. +
  96202. + return buf;
  96203. +}
  96204. +
  96205. +/**
  96206. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  96207. + *
  96208. + * @param usb_ep the endpoint associated with the buffer
  96209. + * @param buf address of the buffer
  96210. + * @param dma The buffer's DMA address
  96211. + * @param bytes The number of bytes of the buffer
  96212. + */
  96213. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  96214. + dma_addr_t dma, unsigned bytes)
  96215. +{
  96216. + dwc_otg_pcd_t *pcd = 0;
  96217. +
  96218. + pcd = gadget_wrapper->pcd;
  96219. +
  96220. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  96221. +
  96222. + dma_free_coherent(NULL, bytes, buf, dma);
  96223. +}
  96224. +#endif
  96225. +
  96226. +/**
  96227. + * This function is used to submit an I/O Request to an EP.
  96228. + *
  96229. + * - When the request completes the request's completion callback
  96230. + * is called to return the request to the driver.
  96231. + * - An EP, except control EPs, may have multiple requests
  96232. + * pending.
  96233. + * - Once submitted the request cannot be examined or modified.
  96234. + * - Each request is turned into one or more packets.
  96235. + * - A BULK EP can queue any amount of data; the transfer is
  96236. + * packetized.
  96237. + * - Zero length Packets are specified with the request 'zero'
  96238. + * flag.
  96239. + */
  96240. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  96241. + gfp_t gfp_flags)
  96242. +{
  96243. + dwc_otg_pcd_t *pcd;
  96244. + struct dwc_otg_pcd_ep *ep = NULL;
  96245. + int retval = 0, is_isoc_ep = 0;
  96246. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  96247. +
  96248. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  96249. + __func__, usb_ep, usb_req, gfp_flags);
  96250. +
  96251. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  96252. + DWC_WARN("bad params\n");
  96253. + return -EINVAL;
  96254. + }
  96255. +
  96256. + if (!usb_ep) {
  96257. + DWC_WARN("bad ep\n");
  96258. + return -EINVAL;
  96259. + }
  96260. +
  96261. + pcd = gadget_wrapper->pcd;
  96262. + if (!gadget_wrapper->driver ||
  96263. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  96264. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  96265. + gadget_wrapper->gadget.speed);
  96266. + DWC_WARN("bogus device state\n");
  96267. + return -ESHUTDOWN;
  96268. + }
  96269. +
  96270. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  96271. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  96272. +
  96273. + usb_req->status = -EINPROGRESS;
  96274. + usb_req->actual = 0;
  96275. +
  96276. + ep = ep_from_handle(pcd, usb_ep);
  96277. + if (ep == NULL)
  96278. + is_isoc_ep = 0;
  96279. + else
  96280. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  96281. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  96282. + dma_addr = usb_req->dma;
  96283. +#else
  96284. + if (GET_CORE_IF(pcd)->dma_enable) {
  96285. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  96286. + struct device *dev = NULL;
  96287. +
  96288. + if (otg_dev != NULL)
  96289. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  96290. +
  96291. + if (usb_req->length != 0 &&
  96292. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  96293. + dma_addr = dma_map_single(dev, usb_req->buf,
  96294. + usb_req->length,
  96295. + ep->dwc_ep.is_in ?
  96296. + DMA_TO_DEVICE:
  96297. + DMA_FROM_DEVICE);
  96298. + }
  96299. + }
  96300. +#endif
  96301. +
  96302. +#ifdef DWC_UTE_PER_IO
  96303. + if (is_isoc_ep == 1) {
  96304. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  96305. + usb_req->length, usb_req->zero, usb_req,
  96306. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  96307. + if (retval)
  96308. + return -EINVAL;
  96309. +
  96310. + return 0;
  96311. + }
  96312. +#endif
  96313. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  96314. + usb_req->length, usb_req->zero, usb_req,
  96315. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  96316. + if (retval) {
  96317. + return -EINVAL;
  96318. + }
  96319. +
  96320. + return 0;
  96321. +}
  96322. +
  96323. +/**
  96324. + * This function cancels an I/O request from an EP.
  96325. + */
  96326. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  96327. +{
  96328. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  96329. +
  96330. + if (!usb_ep || !usb_req) {
  96331. + DWC_WARN("bad argument\n");
  96332. + return -EINVAL;
  96333. + }
  96334. + if (!gadget_wrapper->driver ||
  96335. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  96336. + DWC_WARN("bogus device state\n");
  96337. + return -ESHUTDOWN;
  96338. + }
  96339. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  96340. + return -EINVAL;
  96341. + }
  96342. +
  96343. + return 0;
  96344. +}
  96345. +
  96346. +/**
  96347. + * usb_ep_set_halt stalls an endpoint.
  96348. + *
  96349. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  96350. + * toggle.
  96351. + *
  96352. + * Both of these functions are implemented with the same underlying
  96353. + * function. The behavior depends on the value argument.
  96354. + *
  96355. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  96356. + * @param[in] value
  96357. + * - 0 means clear_halt.
  96358. + * - 1 means set_halt,
  96359. + * - 2 means clear stall lock flag.
  96360. + * - 3 means set stall lock flag.
  96361. + */
  96362. +static int ep_halt(struct usb_ep *usb_ep, int value)
  96363. +{
  96364. + int retval = 0;
  96365. +
  96366. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  96367. +
  96368. + if (!usb_ep) {
  96369. + DWC_WARN("bad ep\n");
  96370. + return -EINVAL;
  96371. + }
  96372. +
  96373. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  96374. + if (retval == -DWC_E_AGAIN) {
  96375. + return -EAGAIN;
  96376. + } else if (retval) {
  96377. + retval = -EINVAL;
  96378. + }
  96379. +
  96380. + return retval;
  96381. +}
  96382. +
  96383. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  96384. +#if 0
  96385. +/**
  96386. + * ep_wedge: sets the halt feature and ignores clear requests
  96387. + *
  96388. + * @usb_ep: the endpoint being wedged
  96389. + *
  96390. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  96391. + * requests. If the gadget driver clears the halt status, it will
  96392. + * automatically unwedge the endpoint.
  96393. + *
  96394. + * Returns zero on success, else negative errno. *
  96395. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  96396. + */
  96397. +static int ep_wedge(struct usb_ep *usb_ep)
  96398. +{
  96399. + int retval = 0;
  96400. +
  96401. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  96402. +
  96403. + if (!usb_ep) {
  96404. + DWC_WARN("bad ep\n");
  96405. + return -EINVAL;
  96406. + }
  96407. +
  96408. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  96409. + if (retval == -DWC_E_AGAIN) {
  96410. + retval = -EAGAIN;
  96411. + } else if (retval) {
  96412. + retval = -EINVAL;
  96413. + }
  96414. +
  96415. + return retval;
  96416. +}
  96417. +#endif
  96418. +
  96419. +#ifdef DWC_EN_ISOC
  96420. +/**
  96421. + * This function is used to submit an ISOC Transfer Request to an EP.
  96422. + *
  96423. + * - Every time a sync period completes the request's completion callback
  96424. + * is called to provide data to the gadget driver.
  96425. + * - Once submitted the request cannot be modified.
  96426. + * - Each request is turned into periodic data packets untill ISO
  96427. + * Transfer is stopped..
  96428. + */
  96429. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  96430. + gfp_t gfp_flags)
  96431. +{
  96432. + int retval = 0;
  96433. +
  96434. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  96435. + DWC_WARN("bad params\n");
  96436. + return -EINVAL;
  96437. + }
  96438. +
  96439. + if (!usb_ep) {
  96440. + DWC_PRINTF("bad params\n");
  96441. + return -EINVAL;
  96442. + }
  96443. +
  96444. + req->status = -EINPROGRESS;
  96445. +
  96446. + retval =
  96447. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  96448. + req->buf1, req->dma0, req->dma1,
  96449. + req->sync_frame, req->data_pattern_frame,
  96450. + req->data_per_frame,
  96451. + req->
  96452. + flags & USB_REQ_ISO_ASAP ? -1 :
  96453. + req->start_frame, req->buf_proc_intrvl,
  96454. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  96455. +
  96456. + if (retval) {
  96457. + return -EINVAL;
  96458. + }
  96459. +
  96460. + return retval;
  96461. +}
  96462. +
  96463. +/**
  96464. + * This function stops ISO EP Periodic Data Transfer.
  96465. + */
  96466. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  96467. +{
  96468. + int retval = 0;
  96469. + if (!usb_ep) {
  96470. + DWC_WARN("bad ep\n");
  96471. + }
  96472. +
  96473. + if (!gadget_wrapper->driver ||
  96474. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  96475. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  96476. + gadget_wrapper->gadget.speed);
  96477. + DWC_WARN("bogus device state\n");
  96478. + }
  96479. +
  96480. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  96481. + if (retval) {
  96482. + retval = -EINVAL;
  96483. + }
  96484. +
  96485. + return retval;
  96486. +}
  96487. +
  96488. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  96489. + int packets, gfp_t gfp_flags)
  96490. +{
  96491. + struct usb_iso_request *pReq = NULL;
  96492. + uint32_t req_size;
  96493. +
  96494. + req_size = sizeof(struct usb_iso_request);
  96495. + req_size +=
  96496. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  96497. +
  96498. + pReq = kmalloc(req_size, gfp_flags);
  96499. + if (!pReq) {
  96500. + DWC_WARN("Can't allocate Iso Request\n");
  96501. + return 0;
  96502. + }
  96503. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  96504. +
  96505. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  96506. +
  96507. + return pReq;
  96508. +}
  96509. +
  96510. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  96511. +{
  96512. + kfree(req);
  96513. +}
  96514. +
  96515. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  96516. + .ep_ops = {
  96517. + .enable = ep_enable,
  96518. + .disable = ep_disable,
  96519. +
  96520. + .alloc_request = dwc_otg_pcd_alloc_request,
  96521. + .free_request = dwc_otg_pcd_free_request,
  96522. +
  96523. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  96524. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  96525. + .free_buffer = dwc_otg_pcd_free_buffer,
  96526. +#endif
  96527. +
  96528. + .queue = ep_queue,
  96529. + .dequeue = ep_dequeue,
  96530. +
  96531. + .set_halt = ep_halt,
  96532. + .fifo_status = 0,
  96533. + .fifo_flush = 0,
  96534. + },
  96535. + .iso_ep_start = iso_ep_start,
  96536. + .iso_ep_stop = iso_ep_stop,
  96537. + .alloc_iso_request = alloc_iso_request,
  96538. + .free_iso_request = free_iso_request,
  96539. +};
  96540. +
  96541. +#else
  96542. +
  96543. + int (*enable) (struct usb_ep *ep,
  96544. + const struct usb_endpoint_descriptor *desc);
  96545. + int (*disable) (struct usb_ep *ep);
  96546. +
  96547. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  96548. + gfp_t gfp_flags);
  96549. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  96550. +
  96551. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  96552. + gfp_t gfp_flags);
  96553. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  96554. +
  96555. + int (*set_halt) (struct usb_ep *ep, int value);
  96556. + int (*set_wedge) (struct usb_ep *ep);
  96557. +
  96558. + int (*fifo_status) (struct usb_ep *ep);
  96559. + void (*fifo_flush) (struct usb_ep *ep);
  96560. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  96561. + .enable = ep_enable,
  96562. + .disable = ep_disable,
  96563. +
  96564. + .alloc_request = dwc_otg_pcd_alloc_request,
  96565. + .free_request = dwc_otg_pcd_free_request,
  96566. +
  96567. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  96568. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  96569. + .free_buffer = dwc_otg_pcd_free_buffer,
  96570. +#else
  96571. + /* .set_wedge = ep_wedge, */
  96572. + .set_wedge = NULL, /* uses set_halt instead */
  96573. +#endif
  96574. +
  96575. + .queue = ep_queue,
  96576. + .dequeue = ep_dequeue,
  96577. +
  96578. + .set_halt = ep_halt,
  96579. + .fifo_status = 0,
  96580. + .fifo_flush = 0,
  96581. +
  96582. +};
  96583. +
  96584. +#endif /* _EN_ISOC_ */
  96585. +/* Gadget Operations */
  96586. +/**
  96587. + * The following gadget operations will be implemented in the DWC_otg
  96588. + * PCD. Functions in the API that are not described below are not
  96589. + * implemented.
  96590. + *
  96591. + * The Gadget API provides wrapper functions for each of the function
  96592. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  96593. + * wrapper function, which then calls the underlying PCD function. The
  96594. + * following sections are named according to the wrapper functions
  96595. + * (except for ioctl, which doesn't have a wrapper function). Within
  96596. + * each section, the corresponding DWC_otg PCD function name is
  96597. + * specified.
  96598. + *
  96599. + */
  96600. +
  96601. +/**
  96602. + *Gets the USB Frame number of the last SOF.
  96603. + */
  96604. +static int get_frame_number(struct usb_gadget *gadget)
  96605. +{
  96606. + struct gadget_wrapper *d;
  96607. +
  96608. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  96609. +
  96610. + if (gadget == 0) {
  96611. + return -ENODEV;
  96612. + }
  96613. +
  96614. + d = container_of(gadget, struct gadget_wrapper, gadget);
  96615. + return dwc_otg_pcd_get_frame_number(d->pcd);
  96616. +}
  96617. +
  96618. +#ifdef CONFIG_USB_DWC_OTG_LPM
  96619. +static int test_lpm_enabled(struct usb_gadget *gadget)
  96620. +{
  96621. + struct gadget_wrapper *d;
  96622. +
  96623. + d = container_of(gadget, struct gadget_wrapper, gadget);
  96624. +
  96625. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  96626. +}
  96627. +#endif
  96628. +
  96629. +/**
  96630. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  96631. + * session is in progress. If a session is already in progress, but
  96632. + * the device is suspended, remote wakeup signaling is started.
  96633. + *
  96634. + */
  96635. +static int wakeup(struct usb_gadget *gadget)
  96636. +{
  96637. + struct gadget_wrapper *d;
  96638. +
  96639. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  96640. +
  96641. + if (gadget == 0) {
  96642. + return -ENODEV;
  96643. + } else {
  96644. + d = container_of(gadget, struct gadget_wrapper, gadget);
  96645. + }
  96646. + dwc_otg_pcd_wakeup(d->pcd);
  96647. + return 0;
  96648. +}
  96649. +
  96650. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  96651. + .get_frame = get_frame_number,
  96652. + .wakeup = wakeup,
  96653. +#ifdef CONFIG_USB_DWC_OTG_LPM
  96654. + .lpm_support = test_lpm_enabled,
  96655. +#endif
  96656. + // current versions must always be self-powered
  96657. +};
  96658. +
  96659. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  96660. +{
  96661. + int retval = -DWC_E_NOT_SUPPORTED;
  96662. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  96663. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  96664. + (struct usb_ctrlrequest
  96665. + *)bytes);
  96666. + }
  96667. +
  96668. + if (retval == -ENOTSUPP) {
  96669. + retval = -DWC_E_NOT_SUPPORTED;
  96670. + } else if (retval < 0) {
  96671. + retval = -DWC_E_INVALID;
  96672. + }
  96673. +
  96674. + return retval;
  96675. +}
  96676. +
  96677. +#ifdef DWC_EN_ISOC
  96678. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  96679. + void *req_handle, int proc_buf_num)
  96680. +{
  96681. + int i, packet_count;
  96682. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  96683. + struct usb_iso_request *iso_req = req_handle;
  96684. +
  96685. + if (proc_buf_num) {
  96686. + iso_packet = iso_req->iso_packet_desc1;
  96687. + } else {
  96688. + iso_packet = iso_req->iso_packet_desc0;
  96689. + }
  96690. + packet_count =
  96691. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  96692. + for (i = 0; i < packet_count; ++i) {
  96693. + int status;
  96694. + int actual;
  96695. + int offset;
  96696. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  96697. + i, &status, &actual, &offset);
  96698. + switch (status) {
  96699. + case -DWC_E_NO_DATA:
  96700. + status = -ENODATA;
  96701. + break;
  96702. + default:
  96703. + if (status) {
  96704. + DWC_PRINTF("unknown status in isoc packet\n");
  96705. + }
  96706. +
  96707. + }
  96708. + iso_packet[i].status = status;
  96709. + iso_packet[i].offset = offset;
  96710. + iso_packet[i].actual_length = actual;
  96711. + }
  96712. +
  96713. + iso_req->status = 0;
  96714. + iso_req->process_buffer(ep_handle, iso_req);
  96715. +
  96716. + return 0;
  96717. +}
  96718. +#endif /* DWC_EN_ISOC */
  96719. +
  96720. +#ifdef DWC_UTE_PER_IO
  96721. +/**
  96722. + * Copy the contents of the extended request to the Linux usb_request's
  96723. + * extended part and call the gadget's completion.
  96724. + *
  96725. + * @param pcd Pointer to the pcd structure
  96726. + * @param ep_handle Void pointer to the usb_ep structure
  96727. + * @param req_handle Void pointer to the usb_request structure
  96728. + * @param status Request status returned from the portable logic
  96729. + * @param ereq_port Void pointer to the extended request structure
  96730. + * created in the the portable part that contains the
  96731. + * results of the processed iso packets.
  96732. + */
  96733. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  96734. + void *req_handle, int32_t status, void *ereq_port)
  96735. +{
  96736. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  96737. + struct dwc_iso_xreq_port *ereqport = NULL;
  96738. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  96739. + int i;
  96740. + struct usb_request *req;
  96741. + //struct dwc_ute_iso_packet_descriptor *
  96742. + //int status = 0;
  96743. +
  96744. + req = (struct usb_request *)req_handle;
  96745. + ereqorg = &req->ext_req;
  96746. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  96747. + desc_org = ereqorg->per_io_frame_descs;
  96748. +
  96749. + if (req && req->complete) {
  96750. + /* Copy the request data from the portable logic to our request */
  96751. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  96752. + desc_org[i].actual_length =
  96753. + ereqport->per_io_frame_descs[i].actual_length;
  96754. + desc_org[i].status =
  96755. + ereqport->per_io_frame_descs[i].status;
  96756. + }
  96757. +
  96758. + switch (status) {
  96759. + case -DWC_E_SHUTDOWN:
  96760. + req->status = -ESHUTDOWN;
  96761. + break;
  96762. + case -DWC_E_RESTART:
  96763. + req->status = -ECONNRESET;
  96764. + break;
  96765. + case -DWC_E_INVALID:
  96766. + req->status = -EINVAL;
  96767. + break;
  96768. + case -DWC_E_TIMEOUT:
  96769. + req->status = -ETIMEDOUT;
  96770. + break;
  96771. + default:
  96772. + req->status = status;
  96773. + }
  96774. +
  96775. + /* And call the gadget's completion */
  96776. + req->complete(ep_handle, req);
  96777. + }
  96778. +
  96779. + return 0;
  96780. +}
  96781. +#endif /* DWC_UTE_PER_IO */
  96782. +
  96783. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  96784. + void *req_handle, int32_t status, uint32_t actual)
  96785. +{
  96786. + struct usb_request *req = (struct usb_request *)req_handle;
  96787. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  96788. + struct dwc_otg_pcd_ep *ep = NULL;
  96789. +#endif
  96790. +
  96791. + if (req && req->complete) {
  96792. + switch (status) {
  96793. + case -DWC_E_SHUTDOWN:
  96794. + req->status = -ESHUTDOWN;
  96795. + break;
  96796. + case -DWC_E_RESTART:
  96797. + req->status = -ECONNRESET;
  96798. + break;
  96799. + case -DWC_E_INVALID:
  96800. + req->status = -EINVAL;
  96801. + break;
  96802. + case -DWC_E_TIMEOUT:
  96803. + req->status = -ETIMEDOUT;
  96804. + break;
  96805. + default:
  96806. + req->status = status;
  96807. +
  96808. + }
  96809. +
  96810. + req->actual = actual;
  96811. + DWC_SPINUNLOCK(pcd->lock);
  96812. + req->complete(ep_handle, req);
  96813. + DWC_SPINLOCK(pcd->lock);
  96814. + }
  96815. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  96816. + ep = ep_from_handle(pcd, ep_handle);
  96817. + if (GET_CORE_IF(pcd)->dma_enable) {
  96818. + if (req->length != 0) {
  96819. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  96820. + struct device *dev = NULL;
  96821. +
  96822. + if (otg_dev != NULL)
  96823. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  96824. +
  96825. + dma_unmap_single(dev, req->dma, req->length,
  96826. + ep->dwc_ep.is_in ?
  96827. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  96828. + }
  96829. + }
  96830. +#endif
  96831. +
  96832. + return 0;
  96833. +}
  96834. +
  96835. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  96836. +{
  96837. + gadget_wrapper->gadget.speed = speed;
  96838. + return 0;
  96839. +}
  96840. +
  96841. +static int _disconnect(dwc_otg_pcd_t * pcd)
  96842. +{
  96843. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  96844. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  96845. + }
  96846. + return 0;
  96847. +}
  96848. +
  96849. +static int _resume(dwc_otg_pcd_t * pcd)
  96850. +{
  96851. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  96852. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  96853. + }
  96854. +
  96855. + return 0;
  96856. +}
  96857. +
  96858. +static int _suspend(dwc_otg_pcd_t * pcd)
  96859. +{
  96860. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  96861. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  96862. + }
  96863. + return 0;
  96864. +}
  96865. +
  96866. +/**
  96867. + * This function updates the otg values in the gadget structure.
  96868. + */
  96869. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  96870. +{
  96871. +
  96872. + if (!gadget_wrapper->gadget.is_otg)
  96873. + return 0;
  96874. +
  96875. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  96876. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  96877. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  96878. + return 0;
  96879. +}
  96880. +
  96881. +static int _reset(dwc_otg_pcd_t * pcd)
  96882. +{
  96883. + return 0;
  96884. +}
  96885. +
  96886. +#ifdef DWC_UTE_CFI
  96887. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  96888. +{
  96889. + int retval = -DWC_E_INVALID;
  96890. + if (gadget_wrapper->driver->cfi_feature_setup) {
  96891. + retval =
  96892. + gadget_wrapper->driver->
  96893. + cfi_feature_setup(&gadget_wrapper->gadget,
  96894. + (struct cfi_usb_ctrlrequest *)cfi_req);
  96895. + }
  96896. +
  96897. + return retval;
  96898. +}
  96899. +#endif
  96900. +
  96901. +static const struct dwc_otg_pcd_function_ops fops = {
  96902. + .complete = _complete,
  96903. +#ifdef DWC_EN_ISOC
  96904. + .isoc_complete = _isoc_complete,
  96905. +#endif
  96906. + .setup = _setup,
  96907. + .disconnect = _disconnect,
  96908. + .connect = _connect,
  96909. + .resume = _resume,
  96910. + .suspend = _suspend,
  96911. + .hnp_changed = _hnp_changed,
  96912. + .reset = _reset,
  96913. +#ifdef DWC_UTE_CFI
  96914. + .cfi_setup = _cfi_setup,
  96915. +#endif
  96916. +#ifdef DWC_UTE_PER_IO
  96917. + .xisoc_complete = _xisoc_complete,
  96918. +#endif
  96919. +};
  96920. +
  96921. +/**
  96922. + * This function is the top level PCD interrupt handler.
  96923. + */
  96924. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  96925. +{
  96926. + dwc_otg_pcd_t *pcd = dev;
  96927. + int32_t retval = IRQ_NONE;
  96928. +
  96929. + retval = dwc_otg_pcd_handle_intr(pcd);
  96930. + if (retval != 0) {
  96931. + S3C2410X_CLEAR_EINTPEND();
  96932. + }
  96933. + return IRQ_RETVAL(retval);
  96934. +}
  96935. +
  96936. +/**
  96937. + * This function initialized the usb_ep structures to there default
  96938. + * state.
  96939. + *
  96940. + * @param d Pointer on gadget_wrapper.
  96941. + */
  96942. +void gadget_add_eps(struct gadget_wrapper *d)
  96943. +{
  96944. + static const char *names[] = {
  96945. +
  96946. + "ep0",
  96947. + "ep1in",
  96948. + "ep2in",
  96949. + "ep3in",
  96950. + "ep4in",
  96951. + "ep5in",
  96952. + "ep6in",
  96953. + "ep7in",
  96954. + "ep8in",
  96955. + "ep9in",
  96956. + "ep10in",
  96957. + "ep11in",
  96958. + "ep12in",
  96959. + "ep13in",
  96960. + "ep14in",
  96961. + "ep15in",
  96962. + "ep1out",
  96963. + "ep2out",
  96964. + "ep3out",
  96965. + "ep4out",
  96966. + "ep5out",
  96967. + "ep6out",
  96968. + "ep7out",
  96969. + "ep8out",
  96970. + "ep9out",
  96971. + "ep10out",
  96972. + "ep11out",
  96973. + "ep12out",
  96974. + "ep13out",
  96975. + "ep14out",
  96976. + "ep15out"
  96977. + };
  96978. +
  96979. + int i;
  96980. + struct usb_ep *ep;
  96981. + int8_t dev_endpoints;
  96982. +
  96983. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  96984. +
  96985. + INIT_LIST_HEAD(&d->gadget.ep_list);
  96986. + d->gadget.ep0 = &d->ep0;
  96987. + d->gadget.speed = USB_SPEED_UNKNOWN;
  96988. +
  96989. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  96990. +
  96991. + /**
  96992. + * Initialize the EP0 structure.
  96993. + */
  96994. + ep = &d->ep0;
  96995. +
  96996. + /* Init the usb_ep structure. */
  96997. + ep->name = names[0];
  96998. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  96999. +
  97000. + /**
  97001. + * @todo NGS: What should the max packet size be set to
  97002. + * here? Before EP type is set?
  97003. + */
  97004. + ep->maxpacket = MAX_PACKET_SIZE;
  97005. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  97006. +
  97007. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  97008. +
  97009. + /**
  97010. + * Initialize the EP structures.
  97011. + */
  97012. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  97013. +
  97014. + for (i = 0; i < dev_endpoints; i++) {
  97015. + ep = &d->in_ep[i];
  97016. +
  97017. + /* Init the usb_ep structure. */
  97018. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  97019. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  97020. +
  97021. + /**
  97022. + * @todo NGS: What should the max packet size be set to
  97023. + * here? Before EP type is set?
  97024. + */
  97025. + ep->maxpacket = MAX_PACKET_SIZE;
  97026. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  97027. + }
  97028. +
  97029. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  97030. +
  97031. + for (i = 0; i < dev_endpoints; i++) {
  97032. + ep = &d->out_ep[i];
  97033. +
  97034. + /* Init the usb_ep structure. */
  97035. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  97036. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  97037. +
  97038. + /**
  97039. + * @todo NGS: What should the max packet size be set to
  97040. + * here? Before EP type is set?
  97041. + */
  97042. + ep->maxpacket = MAX_PACKET_SIZE;
  97043. +
  97044. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  97045. + }
  97046. +
  97047. + /* remove ep0 from the list. There is a ep0 pointer. */
  97048. + list_del_init(&d->ep0.ep_list);
  97049. +
  97050. + d->ep0.maxpacket = MAX_EP0_SIZE;
  97051. +}
  97052. +
  97053. +/**
  97054. + * This function releases the Gadget device.
  97055. + * required by device_unregister().
  97056. + *
  97057. + * @todo Should this do something? Should it free the PCD?
  97058. + */
  97059. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  97060. +{
  97061. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  97062. +}
  97063. +
  97064. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  97065. +{
  97066. + static char pcd_name[] = "dwc_otg_pcd";
  97067. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  97068. + struct gadget_wrapper *d;
  97069. + int retval;
  97070. +
  97071. + d = DWC_ALLOC(sizeof(*d));
  97072. + if (d == NULL) {
  97073. + return NULL;
  97074. + }
  97075. +
  97076. + memset(d, 0, sizeof(*d));
  97077. +
  97078. + d->gadget.name = pcd_name;
  97079. + d->pcd = otg_dev->pcd;
  97080. +
  97081. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  97082. + strcpy(d->gadget.dev.bus_id, "gadget");
  97083. +#else
  97084. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  97085. +#endif
  97086. +
  97087. + d->gadget.dev.parent = &_dev->dev;
  97088. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  97089. + d->gadget.ops = &dwc_otg_pcd_ops;
  97090. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  97091. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  97092. +
  97093. + d->driver = 0;
  97094. + /* Register the gadget device */
  97095. + retval = device_register(&d->gadget.dev);
  97096. + if (retval != 0) {
  97097. + DWC_ERROR("device_register failed\n");
  97098. + DWC_FREE(d);
  97099. + return NULL;
  97100. + }
  97101. +
  97102. + return d;
  97103. +}
  97104. +
  97105. +static void free_wrapper(struct gadget_wrapper *d)
  97106. +{
  97107. + if (d->driver) {
  97108. + /* should have been done already by driver model core */
  97109. + DWC_WARN("driver '%s' is still registered\n",
  97110. + d->driver->driver.name);
  97111. + usb_gadget_unregister_driver(d->driver);
  97112. + }
  97113. +
  97114. + device_unregister(&d->gadget.dev);
  97115. + DWC_FREE(d);
  97116. +}
  97117. +
  97118. +/**
  97119. + * This function initialized the PCD portion of the driver.
  97120. + *
  97121. + */
  97122. +int pcd_init(dwc_bus_dev_t *_dev)
  97123. +{
  97124. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  97125. + int retval = 0;
  97126. +
  97127. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  97128. +
  97129. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  97130. +
  97131. + if (!otg_dev->pcd) {
  97132. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  97133. + return -ENOMEM;
  97134. + }
  97135. +
  97136. + otg_dev->pcd->otg_dev = otg_dev;
  97137. + gadget_wrapper = alloc_wrapper(_dev);
  97138. +
  97139. + /*
  97140. + * Initialize EP structures
  97141. + */
  97142. + gadget_add_eps(gadget_wrapper);
  97143. + /*
  97144. + * Setup interupt handler
  97145. + */
  97146. +#ifdef PLATFORM_INTERFACE
  97147. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  97148. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  97149. + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
  97150. + IRQF_SHARED, gadget_wrapper->gadget.name,
  97151. + otg_dev->pcd);
  97152. + if (retval != 0) {
  97153. + DWC_ERROR("request of irq%d failed\n",
  97154. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  97155. + free_wrapper(gadget_wrapper);
  97156. + return -EBUSY;
  97157. + }
  97158. +#else
  97159. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  97160. + _dev->irq);
  97161. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  97162. + IRQF_SHARED | IRQF_DISABLED,
  97163. + gadget_wrapper->gadget.name, otg_dev->pcd);
  97164. + if (retval != 0) {
  97165. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  97166. + free_wrapper(gadget_wrapper);
  97167. + return -EBUSY;
  97168. + }
  97169. +#endif
  97170. +
  97171. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  97172. +
  97173. + return retval;
  97174. +}
  97175. +
  97176. +/**
  97177. + * Cleanup the PCD.
  97178. + */
  97179. +void pcd_remove(dwc_bus_dev_t *_dev)
  97180. +{
  97181. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  97182. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  97183. +
  97184. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  97185. +
  97186. + /*
  97187. + * Free the IRQ
  97188. + */
  97189. +#ifdef PLATFORM_INTERFACE
  97190. + free_irq(platform_get_irq(_dev, 0), pcd);
  97191. +#else
  97192. + free_irq(_dev->irq, pcd);
  97193. +#endif
  97194. + dwc_otg_pcd_remove(otg_dev->pcd);
  97195. + free_wrapper(gadget_wrapper);
  97196. + otg_dev->pcd = 0;
  97197. +}
  97198. +
  97199. +/**
  97200. + * This function registers a gadget driver with the PCD.
  97201. + *
  97202. + * When a driver is successfully registered, it will receive control
  97203. + * requests including set_configuration(), which enables non-control
  97204. + * requests. then usb traffic follows until a disconnect is reported.
  97205. + * then a host may connect again, or the driver might get unbound.
  97206. + *
  97207. + * @param driver The driver being registered
  97208. + * @param bind The bind function of gadget driver
  97209. + */
  97210. +
  97211. +int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  97212. +{
  97213. + int retval;
  97214. +
  97215. + DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  97216. + driver->driver.name);
  97217. +
  97218. + if (!driver || driver->max_speed == USB_SPEED_UNKNOWN ||
  97219. + !driver->bind ||
  97220. + !driver->unbind || !driver->disconnect || !driver->setup) {
  97221. + DWC_DEBUGPL(DBG_PCDV, "EINVAL\n");
  97222. + return -EINVAL;
  97223. + }
  97224. + if (gadget_wrapper == 0) {
  97225. + DWC_DEBUGPL(DBG_PCDV, "ENODEV\n");
  97226. + return -ENODEV;
  97227. + }
  97228. + if (gadget_wrapper->driver != 0) {
  97229. + DWC_DEBUGPL(DBG_PCDV, "EBUSY (%p)\n", gadget_wrapper->driver);
  97230. + return -EBUSY;
  97231. + }
  97232. +
  97233. + /* hook up the driver */
  97234. + gadget_wrapper->driver = driver;
  97235. + gadget_wrapper->gadget.dev.driver = &driver->driver;
  97236. +
  97237. + DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  97238. + retval = driver->bind(&gadget_wrapper->gadget, gadget_wrapper->driver);
  97239. + if (retval) {
  97240. + DWC_ERROR("bind to driver %s --> error %d\n",
  97241. + driver->driver.name, retval);
  97242. + gadget_wrapper->driver = 0;
  97243. + gadget_wrapper->gadget.dev.driver = 0;
  97244. + return retval;
  97245. + }
  97246. + DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  97247. + driver->driver.name);
  97248. + return 0;
  97249. +}
  97250. +EXPORT_SYMBOL(usb_gadget_probe_driver);
  97251. +
  97252. +/**
  97253. + * This function unregisters a gadget driver
  97254. + *
  97255. + * @param driver The driver being unregistered
  97256. + */
  97257. +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  97258. +{
  97259. + //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  97260. +
  97261. + if (gadget_wrapper == 0) {
  97262. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  97263. + -ENODEV);
  97264. + return -ENODEV;
  97265. + }
  97266. + if (driver == 0 || driver != gadget_wrapper->driver) {
  97267. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  97268. + -EINVAL);
  97269. + return -EINVAL;
  97270. + }
  97271. +
  97272. + driver->unbind(&gadget_wrapper->gadget);
  97273. + gadget_wrapper->driver = 0;
  97274. +
  97275. + DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  97276. + return 0;
  97277. +}
  97278. +
  97279. +EXPORT_SYMBOL(usb_gadget_unregister_driver);
  97280. +
  97281. +#endif /* DWC_HOST_ONLY */
  97282. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_regs.h linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  97283. --- linux-3.17.5/drivers/usb/host/dwc_otg/dwc_otg_regs.h 1969-12-31 18:00:00.000000000 -0600
  97284. +++ linux-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h 2014-12-11 14:02:55.400418001 -0600
  97285. @@ -0,0 +1,2550 @@
  97286. +/* ==========================================================================
  97287. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  97288. + * $Revision: #98 $
  97289. + * $Date: 2012/08/10 $
  97290. + * $Change: 2047372 $
  97291. + *
  97292. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  97293. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  97294. + * otherwise expressly agreed to in writing between Synopsys and you.
  97295. + *
  97296. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  97297. + * any End User Software License Agreement or Agreement for Licensed Product
  97298. + * with Synopsys or any supplement thereto. You are permitted to use and
  97299. + * redistribute this Software in source and binary forms, with or without
  97300. + * modification, provided that redistributions of source code must retain this
  97301. + * notice. You may not view, use, disclose, copy or distribute this file or
  97302. + * any information contained herein except pursuant to this license grant from
  97303. + * Synopsys. If you do not agree with this notice, including the disclaimer
  97304. + * below, then you are not authorized to use the Software.
  97305. + *
  97306. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  97307. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  97308. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  97309. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  97310. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  97311. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  97312. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  97313. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  97314. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  97315. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  97316. + * DAMAGE.
  97317. + * ========================================================================== */
  97318. +
  97319. +#ifndef __DWC_OTG_REGS_H__
  97320. +#define __DWC_OTG_REGS_H__
  97321. +
  97322. +#include "dwc_otg_core_if.h"
  97323. +
  97324. +/**
  97325. + * @file
  97326. + *
  97327. + * This file contains the data structures for accessing the DWC_otg core registers.
  97328. + *
  97329. + * The application interfaces with the HS OTG core by reading from and
  97330. + * writing to the Control and Status Register (CSR) space through the
  97331. + * AHB Slave interface. These registers are 32 bits wide, and the
  97332. + * addresses are 32-bit-block aligned.
  97333. + * CSRs are classified as follows:
  97334. + * - Core Global Registers
  97335. + * - Device Mode Registers
  97336. + * - Device Global Registers
  97337. + * - Device Endpoint Specific Registers
  97338. + * - Host Mode Registers
  97339. + * - Host Global Registers
  97340. + * - Host Port CSRs
  97341. + * - Host Channel Specific Registers
  97342. + *
  97343. + * Only the Core Global registers can be accessed in both Device and
  97344. + * Host modes. When the HS OTG core is operating in one mode, either
  97345. + * Device or Host, the application must not access registers from the
  97346. + * other mode. When the core switches from one mode to another, the
  97347. + * registers in the new mode of operation must be reprogrammed as they
  97348. + * would be after a power-on reset.
  97349. + */
  97350. +
  97351. +/****************************************************************************/
  97352. +/** DWC_otg Core registers .
  97353. + * The dwc_otg_core_global_regs structure defines the size
  97354. + * and relative field offsets for the Core Global registers.
  97355. + */
  97356. +typedef struct dwc_otg_core_global_regs {
  97357. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  97358. + volatile uint32_t gotgctl;
  97359. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  97360. + volatile uint32_t gotgint;
  97361. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  97362. + volatile uint32_t gahbcfg;
  97363. +
  97364. +#define DWC_GLBINTRMASK 0x0001
  97365. +#define DWC_DMAENABLE 0x0020
  97366. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  97367. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  97368. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  97369. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  97370. +
  97371. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  97372. + volatile uint32_t gusbcfg;
  97373. + /**Core Reset Register. <i>Offset: 010h</i> */
  97374. + volatile uint32_t grstctl;
  97375. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  97376. + volatile uint32_t gintsts;
  97377. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  97378. + volatile uint32_t gintmsk;
  97379. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  97380. + volatile uint32_t grxstsr;
  97381. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  97382. + volatile uint32_t grxstsp;
  97383. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  97384. + volatile uint32_t grxfsiz;
  97385. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  97386. + volatile uint32_t gnptxfsiz;
  97387. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  97388. + * Only). <i>Offset: 02Ch</i> */
  97389. + volatile uint32_t gnptxsts;
  97390. + /**I2C Access Register. <i>Offset: 030h</i> */
  97391. + volatile uint32_t gi2cctl;
  97392. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  97393. + volatile uint32_t gpvndctl;
  97394. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  97395. + volatile uint32_t ggpio;
  97396. + /**User ID Register. <i>Offset: 03Ch</i> */
  97397. + volatile uint32_t guid;
  97398. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  97399. + volatile uint32_t gsnpsid;
  97400. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  97401. + volatile uint32_t ghwcfg1;
  97402. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  97403. + volatile uint32_t ghwcfg2;
  97404. +#define DWC_SLAVE_ONLY_ARCH 0
  97405. +#define DWC_EXT_DMA_ARCH 1
  97406. +#define DWC_INT_DMA_ARCH 2
  97407. +
  97408. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  97409. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  97410. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  97411. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  97412. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  97413. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  97414. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  97415. +
  97416. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  97417. + volatile uint32_t ghwcfg3;
  97418. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  97419. + volatile uint32_t ghwcfg4;
  97420. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  97421. + volatile uint32_t glpmcfg;
  97422. + /** Global PowerDn Register <i>Offset: 058h</i> */
  97423. + volatile uint32_t gpwrdn;
  97424. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  97425. + volatile uint32_t gdfifocfg;
  97426. + /** ADP Control Register <i>Offset: 060h</i> */
  97427. + volatile uint32_t adpctl;
  97428. + /** Reserved <i>Offset: 064h-0FFh</i> */
  97429. + volatile uint32_t reserved39[39];
  97430. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  97431. + volatile uint32_t hptxfsiz;
  97432. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  97433. + otherwise Device Transmit FIFO#n Register.
  97434. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  97435. + volatile uint32_t dtxfsiz[15];
  97436. +} dwc_otg_core_global_regs_t;
  97437. +
  97438. +/**
  97439. + * This union represents the bit fields of the Core OTG Control
  97440. + * and Status Register (GOTGCTL). Set the bits using the bit
  97441. + * fields then write the <i>d32</i> value to the register.
  97442. + */
  97443. +typedef union gotgctl_data {
  97444. + /** raw register data */
  97445. + uint32_t d32;
  97446. + /** register bits */
  97447. + struct {
  97448. + unsigned sesreqscs:1;
  97449. + unsigned sesreq:1;
  97450. + unsigned vbvalidoven:1;
  97451. + unsigned vbvalidovval:1;
  97452. + unsigned avalidoven:1;
  97453. + unsigned avalidovval:1;
  97454. + unsigned bvalidoven:1;
  97455. + unsigned bvalidovval:1;
  97456. + unsigned hstnegscs:1;
  97457. + unsigned hnpreq:1;
  97458. + unsigned hstsethnpen:1;
  97459. + unsigned devhnpen:1;
  97460. + unsigned reserved12_15:4;
  97461. + unsigned conidsts:1;
  97462. + unsigned dbnctime:1;
  97463. + unsigned asesvld:1;
  97464. + unsigned bsesvld:1;
  97465. + unsigned otgver:1;
  97466. + unsigned reserved1:1;
  97467. + unsigned multvalidbc:5;
  97468. + unsigned chirpen:1;
  97469. + unsigned reserved28_31:4;
  97470. + } b;
  97471. +} gotgctl_data_t;
  97472. +
  97473. +/**
  97474. + * This union represents the bit fields of the Core OTG Interrupt Register
  97475. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  97476. + * value to the register.
  97477. + */
  97478. +typedef union gotgint_data {
  97479. + /** raw register data */
  97480. + uint32_t d32;
  97481. + /** register bits */
  97482. + struct {
  97483. + /** Current Mode */
  97484. + unsigned reserved0_1:2;
  97485. +
  97486. + /** Session End Detected */
  97487. + unsigned sesenddet:1;
  97488. +
  97489. + unsigned reserved3_7:5;
  97490. +
  97491. + /** Session Request Success Status Change */
  97492. + unsigned sesreqsucstschng:1;
  97493. + /** Host Negotiation Success Status Change */
  97494. + unsigned hstnegsucstschng:1;
  97495. +
  97496. + unsigned reserved10_16:7;
  97497. +
  97498. + /** Host Negotiation Detected */
  97499. + unsigned hstnegdet:1;
  97500. + /** A-Device Timeout Change */
  97501. + unsigned adevtoutchng:1;
  97502. + /** Debounce Done */
  97503. + unsigned debdone:1;
  97504. + /** Multi-Valued input changed */
  97505. + unsigned mvic:1;
  97506. +
  97507. + unsigned reserved31_21:11;
  97508. +
  97509. + } b;
  97510. +} gotgint_data_t;
  97511. +
  97512. +/**
  97513. + * This union represents the bit fields of the Core AHB Configuration
  97514. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  97515. + * write the <i>d32</i> value to the register.
  97516. + */
  97517. +typedef union gahbcfg_data {
  97518. + /** raw register data */
  97519. + uint32_t d32;
  97520. + /** register bits */
  97521. + struct {
  97522. + unsigned glblintrmsk:1;
  97523. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  97524. +
  97525. + unsigned hburstlen:4;
  97526. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  97527. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  97528. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  97529. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  97530. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  97531. +
  97532. + unsigned dmaenable:1;
  97533. +#define DWC_GAHBCFG_DMAENABLE 1
  97534. + unsigned reserved:1;
  97535. + unsigned nptxfemplvl_txfemplvl:1;
  97536. + unsigned ptxfemplvl:1;
  97537. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  97538. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  97539. + unsigned reserved9_20:12;
  97540. + unsigned remmemsupp:1;
  97541. + unsigned notialldmawrit:1;
  97542. + unsigned ahbsingle:1;
  97543. + unsigned reserved24_31:8;
  97544. + } b;
  97545. +} gahbcfg_data_t;
  97546. +
  97547. +/**
  97548. + * This union represents the bit fields of the Core USB Configuration
  97549. + * Register (GUSBCFG). Set the bits using the bit fields then write
  97550. + * the <i>d32</i> value to the register.
  97551. + */
  97552. +typedef union gusbcfg_data {
  97553. + /** raw register data */
  97554. + uint32_t d32;
  97555. + /** register bits */
  97556. + struct {
  97557. + unsigned toutcal:3;
  97558. + unsigned phyif:1;
  97559. + unsigned ulpi_utmi_sel:1;
  97560. + unsigned fsintf:1;
  97561. + unsigned physel:1;
  97562. + unsigned ddrsel:1;
  97563. + unsigned srpcap:1;
  97564. + unsigned hnpcap:1;
  97565. + unsigned usbtrdtim:4;
  97566. + unsigned reserved1:1;
  97567. + unsigned phylpwrclksel:1;
  97568. + unsigned otgutmifssel:1;
  97569. + unsigned ulpi_fsls:1;
  97570. + unsigned ulpi_auto_res:1;
  97571. + unsigned ulpi_clk_sus_m:1;
  97572. + unsigned ulpi_ext_vbus_drv:1;
  97573. + unsigned ulpi_int_vbus_indicator:1;
  97574. + unsigned term_sel_dl_pulse:1;
  97575. + unsigned indicator_complement:1;
  97576. + unsigned indicator_pass_through:1;
  97577. + unsigned ulpi_int_prot_dis:1;
  97578. + unsigned ic_usb_cap:1;
  97579. + unsigned ic_traffic_pull_remove:1;
  97580. + unsigned tx_end_delay:1;
  97581. + unsigned force_host_mode:1;
  97582. + unsigned force_dev_mode:1;
  97583. + unsigned reserved31:1;
  97584. + } b;
  97585. +} gusbcfg_data_t;
  97586. +
  97587. +/**
  97588. + * This union represents the bit fields of the Core Reset Register
  97589. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  97590. + * <i>d32</i> value to the register.
  97591. + */
  97592. +typedef union grstctl_data {
  97593. + /** raw register data */
  97594. + uint32_t d32;
  97595. + /** register bits */
  97596. + struct {
  97597. + /** Core Soft Reset (CSftRst) (Device and Host)
  97598. + *
  97599. + * The application can flush the control logic in the
  97600. + * entire core using this bit. This bit resets the
  97601. + * pipelines in the AHB Clock domain as well as the
  97602. + * PHY Clock domain.
  97603. + *
  97604. + * The state machines are reset to an IDLE state, the
  97605. + * control bits in the CSRs are cleared, all the
  97606. + * transmit FIFOs and the receive FIFO are flushed.
  97607. + *
  97608. + * The status mask bits that control the generation of
  97609. + * the interrupt, are cleared, to clear the
  97610. + * interrupt. The interrupt status bits are not
  97611. + * cleared, so the application can get the status of
  97612. + * any events that occurred in the core after it has
  97613. + * set this bit.
  97614. + *
  97615. + * Any transactions on the AHB are terminated as soon
  97616. + * as possible following the protocol. Any
  97617. + * transactions on the USB are terminated immediately.
  97618. + *
  97619. + * The configuration settings in the CSRs are
  97620. + * unchanged, so the software doesn't have to
  97621. + * reprogram these registers (Device
  97622. + * Configuration/Host Configuration/Core System
  97623. + * Configuration/Core PHY Configuration).
  97624. + *
  97625. + * The application can write to this bit, any time it
  97626. + * wants to reset the core. This is a self clearing
  97627. + * bit and the core clears this bit after all the
  97628. + * necessary logic is reset in the core, which may
  97629. + * take several clocks, depending on the current state
  97630. + * of the core.
  97631. + */
  97632. + unsigned csftrst:1;
  97633. + /** Hclk Soft Reset
  97634. + *
  97635. + * The application uses this bit to reset the control logic in
  97636. + * the AHB clock domain. Only AHB clock domain pipelines are
  97637. + * reset.
  97638. + */
  97639. + unsigned hsftrst:1;
  97640. + /** Host Frame Counter Reset (Host Only)<br>
  97641. + *
  97642. + * The application can reset the (micro)frame number
  97643. + * counter inside the core, using this bit. When the
  97644. + * (micro)frame counter is reset, the subsequent SOF
  97645. + * sent out by the core, will have a (micro)frame
  97646. + * number of 0.
  97647. + */
  97648. + unsigned hstfrm:1;
  97649. + /** In Token Sequence Learning Queue Flush
  97650. + * (INTknQFlsh) (Device Only)
  97651. + */
  97652. + unsigned intknqflsh:1;
  97653. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  97654. + *
  97655. + * The application can flush the entire Receive FIFO
  97656. + * using this bit. The application must first
  97657. + * ensure that the core is not in the middle of a
  97658. + * transaction. The application should write into
  97659. + * this bit, only after making sure that neither the
  97660. + * DMA engine is reading from the RxFIFO nor the MAC
  97661. + * is writing the data in to the FIFO. The
  97662. + * application should wait until the bit is cleared
  97663. + * before performing any other operations. This bit
  97664. + * will takes 8 clocks (slowest of PHY or AHB clock)
  97665. + * to clear.
  97666. + */
  97667. + unsigned rxfflsh:1;
  97668. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  97669. + *
  97670. + * This bit is used to selectively flush a single or
  97671. + * all transmit FIFOs. The application must first
  97672. + * ensure that the core is not in the middle of a
  97673. + * transaction. The application should write into
  97674. + * this bit, only after making sure that neither the
  97675. + * DMA engine is writing into the TxFIFO nor the MAC
  97676. + * is reading the data out of the FIFO. The
  97677. + * application should wait until the core clears this
  97678. + * bit, before performing any operations. This bit
  97679. + * will takes 8 clocks (slowest of PHY or AHB clock)
  97680. + * to clear.
  97681. + */
  97682. + unsigned txfflsh:1;
  97683. +
  97684. + /** TxFIFO Number (TxFNum) (Device and Host).
  97685. + *
  97686. + * This is the FIFO number which needs to be flushed,
  97687. + * using the TxFIFO Flush bit. This field should not
  97688. + * be changed until the TxFIFO Flush bit is cleared by
  97689. + * the core.
  97690. + * - 0x0 : Non Periodic TxFIFO Flush
  97691. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  97692. + * or Periodic TxFIFO in host mode
  97693. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  97694. + * - ...
  97695. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  97696. + * - 0x10: Flush all the Transmit NonPeriodic and
  97697. + * Transmit Periodic FIFOs in the core
  97698. + */
  97699. + unsigned txfnum:5;
  97700. + /** Reserved */
  97701. + unsigned reserved11_29:19;
  97702. + /** DMA Request Signal. Indicated DMA request is in
  97703. + * probress. Used for debug purpose. */
  97704. + unsigned dmareq:1;
  97705. + /** AHB Master Idle. Indicates the AHB Master State
  97706. + * Machine is in IDLE condition. */
  97707. + unsigned ahbidle:1;
  97708. + } b;
  97709. +} grstctl_t;
  97710. +
  97711. +/**
  97712. + * This union represents the bit fields of the Core Interrupt Mask
  97713. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  97714. + * write the <i>d32</i> value to the register.
  97715. + */
  97716. +typedef union gintmsk_data {
  97717. + /** raw register data */
  97718. + uint32_t d32;
  97719. + /** register bits */
  97720. + struct {
  97721. + unsigned reserved0:1;
  97722. + unsigned modemismatch:1;
  97723. + unsigned otgintr:1;
  97724. + unsigned sofintr:1;
  97725. + unsigned rxstsqlvl:1;
  97726. + unsigned nptxfempty:1;
  97727. + unsigned ginnakeff:1;
  97728. + unsigned goutnakeff:1;
  97729. + unsigned ulpickint:1;
  97730. + unsigned i2cintr:1;
  97731. + unsigned erlysuspend:1;
  97732. + unsigned usbsuspend:1;
  97733. + unsigned usbreset:1;
  97734. + unsigned enumdone:1;
  97735. + unsigned isooutdrop:1;
  97736. + unsigned eopframe:1;
  97737. + unsigned restoredone:1;
  97738. + unsigned epmismatch:1;
  97739. + unsigned inepintr:1;
  97740. + unsigned outepintr:1;
  97741. + unsigned incomplisoin:1;
  97742. + unsigned incomplisoout:1;
  97743. + unsigned fetsusp:1;
  97744. + unsigned resetdet:1;
  97745. + unsigned portintr:1;
  97746. + unsigned hcintr:1;
  97747. + unsigned ptxfempty:1;
  97748. + unsigned lpmtranrcvd:1;
  97749. + unsigned conidstschng:1;
  97750. + unsigned disconnect:1;
  97751. + unsigned sessreqintr:1;
  97752. + unsigned wkupintr:1;
  97753. + } b;
  97754. +} gintmsk_data_t;
  97755. +/**
  97756. + * This union represents the bit fields of the Core Interrupt Register
  97757. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  97758. + * <i>d32</i> value to the register.
  97759. + */
  97760. +typedef union gintsts_data {
  97761. + /** raw register data */
  97762. + uint32_t d32;
  97763. +#define DWC_SOF_INTR_MASK 0x0008
  97764. + /** register bits */
  97765. + struct {
  97766. +#define DWC_HOST_MODE 1
  97767. + unsigned curmode:1;
  97768. + unsigned modemismatch:1;
  97769. + unsigned otgintr:1;
  97770. + unsigned sofintr:1;
  97771. + unsigned rxstsqlvl:1;
  97772. + unsigned nptxfempty:1;
  97773. + unsigned ginnakeff:1;
  97774. + unsigned goutnakeff:1;
  97775. + unsigned ulpickint:1;
  97776. + unsigned i2cintr:1;
  97777. + unsigned erlysuspend:1;
  97778. + unsigned usbsuspend:1;
  97779. + unsigned usbreset:1;
  97780. + unsigned enumdone:1;
  97781. + unsigned isooutdrop:1;
  97782. + unsigned eopframe:1;
  97783. + unsigned restoredone:1;
  97784. + unsigned epmismatch:1;
  97785. + unsigned inepint:1;
  97786. + unsigned outepintr:1;
  97787. + unsigned incomplisoin:1;
  97788. + unsigned incomplisoout:1;
  97789. + unsigned fetsusp:1;
  97790. + unsigned resetdet:1;
  97791. + unsigned portintr:1;
  97792. + unsigned hcintr:1;
  97793. + unsigned ptxfempty:1;
  97794. + unsigned lpmtranrcvd:1;
  97795. + unsigned conidstschng:1;
  97796. + unsigned disconnect:1;
  97797. + unsigned sessreqintr:1;
  97798. + unsigned wkupintr:1;
  97799. + } b;
  97800. +} gintsts_data_t;
  97801. +
  97802. +/**
  97803. + * This union represents the bit fields in the Device Receive Status Read and
  97804. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  97805. + * element then read out the bits using the <i>b</i>it elements.
  97806. + */
  97807. +typedef union device_grxsts_data {
  97808. + /** raw register data */
  97809. + uint32_t d32;
  97810. + /** register bits */
  97811. + struct {
  97812. + unsigned epnum:4;
  97813. + unsigned bcnt:11;
  97814. + unsigned dpid:2;
  97815. +
  97816. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  97817. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  97818. +
  97819. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  97820. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  97821. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  97822. + unsigned pktsts:4;
  97823. + unsigned fn:4;
  97824. + unsigned reserved25_31:7;
  97825. + } b;
  97826. +} device_grxsts_data_t;
  97827. +
  97828. +/**
  97829. + * This union represents the bit fields in the Host Receive Status Read and
  97830. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  97831. + * element then read out the bits using the <i>b</i>it elements.
  97832. + */
  97833. +typedef union host_grxsts_data {
  97834. + /** raw register data */
  97835. + uint32_t d32;
  97836. + /** register bits */
  97837. + struct {
  97838. + unsigned chnum:4;
  97839. + unsigned bcnt:11;
  97840. + unsigned dpid:2;
  97841. +
  97842. + unsigned pktsts:4;
  97843. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  97844. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  97845. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  97846. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  97847. +
  97848. + unsigned reserved21_31:11;
  97849. + } b;
  97850. +} host_grxsts_data_t;
  97851. +
  97852. +/**
  97853. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  97854. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  97855. + * then read out the bits using the <i>b</i>it elements.
  97856. + */
  97857. +typedef union fifosize_data {
  97858. + /** raw register data */
  97859. + uint32_t d32;
  97860. + /** register bits */
  97861. + struct {
  97862. + unsigned startaddr:16;
  97863. + unsigned depth:16;
  97864. + } b;
  97865. +} fifosize_data_t;
  97866. +
  97867. +/**
  97868. + * This union represents the bit fields in the Non-Periodic Transmit
  97869. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  97870. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  97871. + * elements.
  97872. + */
  97873. +typedef union gnptxsts_data {
  97874. + /** raw register data */
  97875. + uint32_t d32;
  97876. + /** register bits */
  97877. + struct {
  97878. + unsigned nptxfspcavail:16;
  97879. + unsigned nptxqspcavail:8;
  97880. + /** Top of the Non-Periodic Transmit Request Queue
  97881. + * - bit 24 - Terminate (Last entry for the selected
  97882. + * channel/EP)
  97883. + * - bits 26:25 - Token Type
  97884. + * - 2'b00 - IN/OUT
  97885. + * - 2'b01 - Zero Length OUT
  97886. + * - 2'b10 - PING/Complete Split
  97887. + * - 2'b11 - Channel Halt
  97888. + * - bits 30:27 - Channel/EP Number
  97889. + */
  97890. + unsigned nptxqtop_terminate:1;
  97891. + unsigned nptxqtop_token:2;
  97892. + unsigned nptxqtop_chnep:4;
  97893. + unsigned reserved:1;
  97894. + } b;
  97895. +} gnptxsts_data_t;
  97896. +
  97897. +/**
  97898. + * This union represents the bit fields in the Transmit
  97899. + * FIFO Status Register (DTXFSTS). Read the register into the
  97900. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  97901. + * elements.
  97902. + */
  97903. +typedef union dtxfsts_data {
  97904. + /** raw register data */
  97905. + uint32_t d32;
  97906. + /** register bits */
  97907. + struct {
  97908. + unsigned txfspcavail:16;
  97909. + unsigned reserved:16;
  97910. + } b;
  97911. +} dtxfsts_data_t;
  97912. +
  97913. +/**
  97914. + * This union represents the bit fields in the I2C Control Register
  97915. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  97916. + * bits using the <i>b</i>it elements.
  97917. + */
  97918. +typedef union gi2cctl_data {
  97919. + /** raw register data */
  97920. + uint32_t d32;
  97921. + /** register bits */
  97922. + struct {
  97923. + unsigned rwdata:8;
  97924. + unsigned regaddr:8;
  97925. + unsigned addr:7;
  97926. + unsigned i2cen:1;
  97927. + unsigned ack:1;
  97928. + unsigned i2csuspctl:1;
  97929. + unsigned i2cdevaddr:2;
  97930. + unsigned i2cdatse0:1;
  97931. + unsigned reserved:1;
  97932. + unsigned rw:1;
  97933. + unsigned bsydne:1;
  97934. + } b;
  97935. +} gi2cctl_data_t;
  97936. +
  97937. +/**
  97938. + * This union represents the bit fields in the PHY Vendor Control Register
  97939. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  97940. + * bits using the <i>b</i>it elements.
  97941. + */
  97942. +typedef union gpvndctl_data {
  97943. + /** raw register data */
  97944. + uint32_t d32;
  97945. + /** register bits */
  97946. + struct {
  97947. + unsigned regdata:8;
  97948. + unsigned vctrl:8;
  97949. + unsigned regaddr16_21:6;
  97950. + unsigned regwr:1;
  97951. + unsigned reserved23_24:2;
  97952. + unsigned newregreq:1;
  97953. + unsigned vstsbsy:1;
  97954. + unsigned vstsdone:1;
  97955. + unsigned reserved28_30:3;
  97956. + unsigned disulpidrvr:1;
  97957. + } b;
  97958. +} gpvndctl_data_t;
  97959. +
  97960. +/**
  97961. + * This union represents the bit fields in the General Purpose
  97962. + * Input/Output Register (GGPIO).
  97963. + * Read the register into the <i>d32</i> element then read out the
  97964. + * bits using the <i>b</i>it elements.
  97965. + */
  97966. +typedef union ggpio_data {
  97967. + /** raw register data */
  97968. + uint32_t d32;
  97969. + /** register bits */
  97970. + struct {
  97971. + unsigned gpi:16;
  97972. + unsigned gpo:16;
  97973. + } b;
  97974. +} ggpio_data_t;
  97975. +
  97976. +/**
  97977. + * This union represents the bit fields in the User ID Register
  97978. + * (GUID). Read the register into the <i>d32</i> element then read out the
  97979. + * bits using the <i>b</i>it elements.
  97980. + */
  97981. +typedef union guid_data {
  97982. + /** raw register data */
  97983. + uint32_t d32;
  97984. + /** register bits */
  97985. + struct {
  97986. + unsigned rwdata:32;
  97987. + } b;
  97988. +} guid_data_t;
  97989. +
  97990. +/**
  97991. + * This union represents the bit fields in the Synopsys ID Register
  97992. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  97993. + * bits using the <i>b</i>it elements.
  97994. + */
  97995. +typedef union gsnpsid_data {
  97996. + /** raw register data */
  97997. + uint32_t d32;
  97998. + /** register bits */
  97999. + struct {
  98000. + unsigned rwdata:32;
  98001. + } b;
  98002. +} gsnpsid_data_t;
  98003. +
  98004. +/**
  98005. + * This union represents the bit fields in the User HW Config1
  98006. + * Register. Read the register into the <i>d32</i> element then read
  98007. + * out the bits using the <i>b</i>it elements.
  98008. + */
  98009. +typedef union hwcfg1_data {
  98010. + /** raw register data */
  98011. + uint32_t d32;
  98012. + /** register bits */
  98013. + struct {
  98014. + unsigned ep_dir0:2;
  98015. + unsigned ep_dir1:2;
  98016. + unsigned ep_dir2:2;
  98017. + unsigned ep_dir3:2;
  98018. + unsigned ep_dir4:2;
  98019. + unsigned ep_dir5:2;
  98020. + unsigned ep_dir6:2;
  98021. + unsigned ep_dir7:2;
  98022. + unsigned ep_dir8:2;
  98023. + unsigned ep_dir9:2;
  98024. + unsigned ep_dir10:2;
  98025. + unsigned ep_dir11:2;
  98026. + unsigned ep_dir12:2;
  98027. + unsigned ep_dir13:2;
  98028. + unsigned ep_dir14:2;
  98029. + unsigned ep_dir15:2;
  98030. + } b;
  98031. +} hwcfg1_data_t;
  98032. +
  98033. +/**
  98034. + * This union represents the bit fields in the User HW Config2
  98035. + * Register. Read the register into the <i>d32</i> element then read
  98036. + * out the bits using the <i>b</i>it elements.
  98037. + */
  98038. +typedef union hwcfg2_data {
  98039. + /** raw register data */
  98040. + uint32_t d32;
  98041. + /** register bits */
  98042. + struct {
  98043. + /* GHWCFG2 */
  98044. + unsigned op_mode:3;
  98045. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  98046. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  98047. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  98048. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  98049. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  98050. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  98051. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  98052. +
  98053. + unsigned architecture:2;
  98054. + unsigned point2point:1;
  98055. + unsigned hs_phy_type:2;
  98056. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  98057. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  98058. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  98059. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  98060. +
  98061. + unsigned fs_phy_type:2;
  98062. + unsigned num_dev_ep:4;
  98063. + unsigned num_host_chan:4;
  98064. + unsigned perio_ep_supported:1;
  98065. + unsigned dynamic_fifo:1;
  98066. + unsigned multi_proc_int:1;
  98067. + unsigned reserved21:1;
  98068. + unsigned nonperio_tx_q_depth:2;
  98069. + unsigned host_perio_tx_q_depth:2;
  98070. + unsigned dev_token_q_depth:5;
  98071. + unsigned otg_enable_ic_usb:1;
  98072. + } b;
  98073. +} hwcfg2_data_t;
  98074. +
  98075. +/**
  98076. + * This union represents the bit fields in the User HW Config3
  98077. + * Register. Read the register into the <i>d32</i> element then read
  98078. + * out the bits using the <i>b</i>it elements.
  98079. + */
  98080. +typedef union hwcfg3_data {
  98081. + /** raw register data */
  98082. + uint32_t d32;
  98083. + /** register bits */
  98084. + struct {
  98085. + /* GHWCFG3 */
  98086. + unsigned xfer_size_cntr_width:4;
  98087. + unsigned packet_size_cntr_width:3;
  98088. + unsigned otg_func:1;
  98089. + unsigned i2c:1;
  98090. + unsigned vendor_ctrl_if:1;
  98091. + unsigned optional_features:1;
  98092. + unsigned synch_reset_type:1;
  98093. + unsigned adp_supp:1;
  98094. + unsigned otg_enable_hsic:1;
  98095. + unsigned bc_support:1;
  98096. + unsigned otg_lpm_en:1;
  98097. + unsigned dfifo_depth:16;
  98098. + } b;
  98099. +} hwcfg3_data_t;
  98100. +
  98101. +/**
  98102. + * This union represents the bit fields in the User HW Config4
  98103. + * Register. Read the register into the <i>d32</i> element then read
  98104. + * out the bits using the <i>b</i>it elements.
  98105. + */
  98106. +typedef union hwcfg4_data {
  98107. + /** raw register data */
  98108. + uint32_t d32;
  98109. + /** register bits */
  98110. + struct {
  98111. + unsigned num_dev_perio_in_ep:4;
  98112. + unsigned power_optimiz:1;
  98113. + unsigned min_ahb_freq:1;
  98114. + unsigned hiber:1;
  98115. + unsigned xhiber:1;
  98116. + unsigned reserved:6;
  98117. + unsigned utmi_phy_data_width:2;
  98118. + unsigned num_dev_mode_ctrl_ep:4;
  98119. + unsigned iddig_filt_en:1;
  98120. + unsigned vbus_valid_filt_en:1;
  98121. + unsigned a_valid_filt_en:1;
  98122. + unsigned b_valid_filt_en:1;
  98123. + unsigned session_end_filt_en:1;
  98124. + unsigned ded_fifo_en:1;
  98125. + unsigned num_in_eps:4;
  98126. + unsigned desc_dma:1;
  98127. + unsigned desc_dma_dyn:1;
  98128. + } b;
  98129. +} hwcfg4_data_t;
  98130. +
  98131. +/**
  98132. + * This union represents the bit fields of the Core LPM Configuration
  98133. + * Register (GLPMCFG). Set the bits using bit fields then write
  98134. + * the <i>d32</i> value to the register.
  98135. + */
  98136. +typedef union glpmctl_data {
  98137. + /** raw register data */
  98138. + uint32_t d32;
  98139. + /** register bits */
  98140. + struct {
  98141. + /** LPM-Capable (LPMCap) (Device and Host)
  98142. + * The application uses this bit to control
  98143. + * the DWC_otg core LPM capabilities.
  98144. + */
  98145. + unsigned lpm_cap_en:1;
  98146. + /** LPM response programmed by application (AppL1Res) (Device)
  98147. + * Handshake response to LPM token pre-programmed
  98148. + * by device application software.
  98149. + */
  98150. + unsigned appl_resp:1;
  98151. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  98152. + * In Host mode this field indicates the value of HIRD
  98153. + * to be sent in an LPM transaction.
  98154. + * In Device mode this field is updated with the
  98155. + * Received LPM Token HIRD bmAttribute
  98156. + * when an ACK/NYET/STALL response is sent
  98157. + * to an LPM transaction.
  98158. + */
  98159. + unsigned hird:4;
  98160. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  98161. + * In Host mode this bit indicates the value of remote
  98162. + * wake up to be sent in wIndex field of LPM transaction.
  98163. + * In Device mode this field is updated with the
  98164. + * Received LPM Token bRemoteWake bmAttribute
  98165. + * when an ACK/NYET/STALL response is sent
  98166. + * to an LPM transaction.
  98167. + */
  98168. + unsigned rem_wkup_en:1;
  98169. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  98170. + * The application uses this bit to control
  98171. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  98172. + */
  98173. + unsigned en_utmi_sleep:1;
  98174. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  98175. + */
  98176. + unsigned hird_thres:5;
  98177. + /** LPM Response (CoreL1Res) (Device and Host)
  98178. + * In Host mode this bit contains handsake response to
  98179. + * LPM transaction.
  98180. + * In Device mode the response of the core to
  98181. + * LPM transaction received is reflected in these two bits.
  98182. + - 0x0 : ERROR (No handshake response)
  98183. + - 0x1 : STALL
  98184. + - 0x2 : NYET
  98185. + - 0x3 : ACK
  98186. + */
  98187. + unsigned lpm_resp:2;
  98188. + /** Port Sleep Status (SlpSts) (Device and Host)
  98189. + * This bit is set as long as a Sleep condition
  98190. + * is present on the USB bus.
  98191. + */
  98192. + unsigned prt_sleep_sts:1;
  98193. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  98194. + * Indicates that the application or host
  98195. + * can start resume from Sleep state.
  98196. + */
  98197. + unsigned sleep_state_resumeok:1;
  98198. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  98199. + * The channel number on which the LPM transaction
  98200. + * has to be applied while sending
  98201. + * an LPM transaction to the local device.
  98202. + */
  98203. + unsigned lpm_chan_index:4;
  98204. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  98205. + * Number host retries that would be performed
  98206. + * if the device response was not valid response.
  98207. + */
  98208. + unsigned retry_count:3;
  98209. + /** Send LPM Transaction (SndLPM) (Host)
  98210. + * When set by application software,
  98211. + * an LPM transaction containing two tokens
  98212. + * is sent.
  98213. + */
  98214. + unsigned send_lpm:1;
  98215. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  98216. + * Number of LPM Host Retries still remaining
  98217. + * to be transmitted for the current LPM sequence
  98218. + */
  98219. + unsigned retry_count_sts:3;
  98220. + unsigned reserved28_29:2;
  98221. + /** In host mode once this bit is set, the host
  98222. + * configures to drive the HSIC Idle state on the bus.
  98223. + * It then waits for the device to initiate the Connect sequence.
  98224. + * In device mode once this bit is set, the device waits for
  98225. + * the HSIC Idle line state on the bus. Upon receving the Idle
  98226. + * line state, it initiates the HSIC Connect sequence.
  98227. + */
  98228. + unsigned hsic_connect:1;
  98229. + /** This bit overrides and functionally inverts
  98230. + * the if_select_hsic input port signal.
  98231. + */
  98232. + unsigned inv_sel_hsic:1;
  98233. + } b;
  98234. +} glpmcfg_data_t;
  98235. +
  98236. +/**
  98237. + * This union represents the bit fields of the Core ADP Timer, Control and
  98238. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  98239. + * the <i>d32</i> value to the register.
  98240. + */
  98241. +typedef union adpctl_data {
  98242. + /** raw register data */
  98243. + uint32_t d32;
  98244. + /** register bits */
  98245. + struct {
  98246. + /** Probe Discharge (PRB_DSCHG)
  98247. + * These bits set the times for TADP_DSCHG.
  98248. + * These bits are defined as follows:
  98249. + * 2'b00 - 4 msec
  98250. + * 2'b01 - 8 msec
  98251. + * 2'b10 - 16 msec
  98252. + * 2'b11 - 32 msec
  98253. + */
  98254. + unsigned prb_dschg:2;
  98255. + /** Probe Delta (PRB_DELTA)
  98256. + * These bits set the resolution for RTIM value.
  98257. + * The bits are defined in units of 32 kHz clock cycles as follows:
  98258. + * 2'b00 - 1 cycles
  98259. + * 2'b01 - 2 cycles
  98260. + * 2'b10 - 3 cycles
  98261. + * 2'b11 - 4 cycles
  98262. + * For example if this value is chosen to 2'b01, it means that RTIM
  98263. + * increments for every 3(three) 32Khz clock cycles.
  98264. + */
  98265. + unsigned prb_delta:2;
  98266. + /** Probe Period (PRB_PER)
  98267. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  98268. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  98269. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  98270. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  98271. + * 2'b11 - Reserved
  98272. + */
  98273. + unsigned prb_per:2;
  98274. + /** These bits capture the latest time it took for VBUS to ramp from
  98275. + * VADP_SINK to VADP_PRB.
  98276. + * 0x000 - 1 cycles
  98277. + * 0x001 - 2 cycles
  98278. + * 0x002 - 3 cycles
  98279. + * etc
  98280. + * 0x7FF - 2048 cycles
  98281. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  98282. + */
  98283. + unsigned rtim:11;
  98284. + /** Enable Probe (EnaPrb)
  98285. + * When programmed to 1'b1, the core performs a probe operation.
  98286. + * This bit is valid only if OTG_Ver = 1'b1.
  98287. + */
  98288. + unsigned enaprb:1;
  98289. + /** Enable Sense (EnaSns)
  98290. + * When programmed to 1'b1, the core performs a Sense operation.
  98291. + * This bit is valid only if OTG_Ver = 1'b1.
  98292. + */
  98293. + unsigned enasns:1;
  98294. + /** ADP Reset (ADPRes)
  98295. + * When set, ADP controller is reset.
  98296. + * This bit is valid only if OTG_Ver = 1'b1.
  98297. + */
  98298. + unsigned adpres:1;
  98299. + /** ADP Enable (ADPEn)
  98300. + * When set, the core performs either ADP probing or sensing
  98301. + * based on EnaPrb or EnaSns.
  98302. + * This bit is valid only if OTG_Ver = 1'b1.
  98303. + */
  98304. + unsigned adpen:1;
  98305. + /** ADP Probe Interrupt (ADP_PRB_INT)
  98306. + * When this bit is set, it means that the VBUS
  98307. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  98308. + * This bit is valid only if OTG_Ver = 1'b1.
  98309. + */
  98310. + unsigned adp_prb_int:1;
  98311. + /**
  98312. + * ADP Sense Interrupt (ADP_SNS_INT)
  98313. + * When this bit is set, it means that the VBUS voltage is greater than
  98314. + * VADP_SNS value or VADP_SNS is reached.
  98315. + * This bit is valid only if OTG_Ver = 1'b1.
  98316. + */
  98317. + unsigned adp_sns_int:1;
  98318. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  98319. + * This bit is relevant only for an ADP probe.
  98320. + * When this bit is set, it means that the ramp time has
  98321. + * completed ie ADPCTL.RTIM has reached its terminal value
  98322. + * of 0x7FF. This is a debug feature that allows software
  98323. + * to read the ramp time after each cycle.
  98324. + * This bit is valid only if OTG_Ver = 1'b1.
  98325. + */
  98326. + unsigned adp_tmout_int:1;
  98327. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  98328. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  98329. + * This bit is valid only if OTG_Ver = 1'b1.
  98330. + */
  98331. + unsigned adp_prb_int_msk:1;
  98332. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  98333. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  98334. + * This bit is valid only if OTG_Ver = 1'b1.
  98335. + */
  98336. + unsigned adp_sns_int_msk:1;
  98337. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  98338. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  98339. + * This bit is valid only if OTG_Ver = 1'b1.
  98340. + */
  98341. + unsigned adp_tmout_int_msk:1;
  98342. + /** Access Request
  98343. + * 2'b00 - Read/Write Valid (updated by the core)
  98344. + * 2'b01 - Read
  98345. + * 2'b00 - Write
  98346. + * 2'b00 - Reserved
  98347. + */
  98348. + unsigned ar:2;
  98349. + /** Reserved */
  98350. + unsigned reserved29_31:3;
  98351. + } b;
  98352. +} adpctl_data_t;
  98353. +
  98354. +////////////////////////////////////////////
  98355. +// Device Registers
  98356. +/**
  98357. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  98358. + *
  98359. + * The following structures define the size and relative field offsets
  98360. + * for the Device Mode Registers.
  98361. + *
  98362. + * <i>These registers are visible only in Device mode and must not be
  98363. + * accessed in Host mode, as the results are unknown.</i>
  98364. + */
  98365. +typedef struct dwc_otg_dev_global_regs {
  98366. + /** Device Configuration Register. <i>Offset 800h</i> */
  98367. + volatile uint32_t dcfg;
  98368. + /** Device Control Register. <i>Offset: 804h</i> */
  98369. + volatile uint32_t dctl;
  98370. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  98371. + volatile uint32_t dsts;
  98372. + /** Reserved. <i>Offset: 80Ch</i> */
  98373. + uint32_t unused;
  98374. + /** Device IN Endpoint Common Interrupt Mask
  98375. + * Register. <i>Offset: 810h</i> */
  98376. + volatile uint32_t diepmsk;
  98377. + /** Device OUT Endpoint Common Interrupt Mask
  98378. + * Register. <i>Offset: 814h</i> */
  98379. + volatile uint32_t doepmsk;
  98380. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  98381. + volatile uint32_t daint;
  98382. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  98383. + * 81Ch</i> */
  98384. + volatile uint32_t daintmsk;
  98385. + /** Device IN Token Queue Read Register-1 (Read Only).
  98386. + * <i>Offset: 820h</i> */
  98387. + volatile uint32_t dtknqr1;
  98388. + /** Device IN Token Queue Read Register-2 (Read Only).
  98389. + * <i>Offset: 824h</i> */
  98390. + volatile uint32_t dtknqr2;
  98391. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  98392. + volatile uint32_t dvbusdis;
  98393. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  98394. + volatile uint32_t dvbuspulse;
  98395. + /** Device IN Token Queue Read Register-3 (Read Only). /
  98396. + * Device Thresholding control register (Read/Write)
  98397. + * <i>Offset: 830h</i> */
  98398. + volatile uint32_t dtknqr3_dthrctl;
  98399. + /** Device IN Token Queue Read Register-4 (Read Only). /
  98400. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  98401. + * <i>Offset: 834h</i> */
  98402. + volatile uint32_t dtknqr4_fifoemptymsk;
  98403. + /** Device Each Endpoint Interrupt Register (Read Only). /
  98404. + * <i>Offset: 838h</i> */
  98405. + volatile uint32_t deachint;
  98406. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  98407. + * <i>Offset: 83Ch</i> */
  98408. + volatile uint32_t deachintmsk;
  98409. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  98410. + * <i>Offset: 840h</i> */
  98411. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  98412. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  98413. + * <i>Offset: 880h</i> */
  98414. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  98415. +} dwc_otg_device_global_regs_t;
  98416. +
  98417. +/**
  98418. + * This union represents the bit fields in the Device Configuration
  98419. + * Register. Read the register into the <i>d32</i> member then
  98420. + * set/clear the bits using the <i>b</i>it elements. Write the
  98421. + * <i>d32</i> member to the dcfg register.
  98422. + */
  98423. +typedef union dcfg_data {
  98424. + /** raw register data */
  98425. + uint32_t d32;
  98426. + /** register bits */
  98427. + struct {
  98428. + /** Device Speed */
  98429. + unsigned devspd:2;
  98430. + /** Non Zero Length Status OUT Handshake */
  98431. + unsigned nzstsouthshk:1;
  98432. +#define DWC_DCFG_SEND_STALL 1
  98433. +
  98434. + unsigned ena32khzs:1;
  98435. + /** Device Addresses */
  98436. + unsigned devaddr:7;
  98437. + /** Periodic Frame Interval */
  98438. + unsigned perfrint:2;
  98439. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  98440. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  98441. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  98442. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  98443. +
  98444. + /** Enable Device OUT NAK for bulk in DDMA mode */
  98445. + unsigned endevoutnak:1;
  98446. +
  98447. + unsigned reserved14_17:4;
  98448. + /** In Endpoint Mis-match count */
  98449. + unsigned epmscnt:5;
  98450. + /** Enable Descriptor DMA in Device mode */
  98451. + unsigned descdma:1;
  98452. + unsigned perschintvl:2;
  98453. + unsigned resvalid:6;
  98454. + } b;
  98455. +} dcfg_data_t;
  98456. +
  98457. +/**
  98458. + * This union represents the bit fields in the Device Control
  98459. + * Register. Read the register into the <i>d32</i> member then
  98460. + * set/clear the bits using the <i>b</i>it elements.
  98461. + */
  98462. +typedef union dctl_data {
  98463. + /** raw register data */
  98464. + uint32_t d32;
  98465. + /** register bits */
  98466. + struct {
  98467. + /** Remote Wakeup */
  98468. + unsigned rmtwkupsig:1;
  98469. + /** Soft Disconnect */
  98470. + unsigned sftdiscon:1;
  98471. + /** Global Non-Periodic IN NAK Status */
  98472. + unsigned gnpinnaksts:1;
  98473. + /** Global OUT NAK Status */
  98474. + unsigned goutnaksts:1;
  98475. + /** Test Control */
  98476. + unsigned tstctl:3;
  98477. + /** Set Global Non-Periodic IN NAK */
  98478. + unsigned sgnpinnak:1;
  98479. + /** Clear Global Non-Periodic IN NAK */
  98480. + unsigned cgnpinnak:1;
  98481. + /** Set Global OUT NAK */
  98482. + unsigned sgoutnak:1;
  98483. + /** Clear Global OUT NAK */
  98484. + unsigned cgoutnak:1;
  98485. + /** Power-On Programming Done */
  98486. + unsigned pwronprgdone:1;
  98487. + /** Reserved */
  98488. + unsigned reserved:1;
  98489. + /** Global Multi Count */
  98490. + unsigned gmc:2;
  98491. + /** Ignore Frame Number for ISOC EPs */
  98492. + unsigned ifrmnum:1;
  98493. + /** NAK on Babble */
  98494. + unsigned nakonbble:1;
  98495. + /** Enable Continue on BNA */
  98496. + unsigned encontonbna:1;
  98497. +
  98498. + unsigned reserved18_31:14;
  98499. + } b;
  98500. +} dctl_data_t;
  98501. +
  98502. +/**
  98503. + * This union represents the bit fields in the Device Status
  98504. + * Register. Read the register into the <i>d32</i> member then
  98505. + * set/clear the bits using the <i>b</i>it elements.
  98506. + */
  98507. +typedef union dsts_data {
  98508. + /** raw register data */
  98509. + uint32_t d32;
  98510. + /** register bits */
  98511. + struct {
  98512. + /** Suspend Status */
  98513. + unsigned suspsts:1;
  98514. + /** Enumerated Speed */
  98515. + unsigned enumspd:2;
  98516. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  98517. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  98518. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  98519. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  98520. + /** Erratic Error */
  98521. + unsigned errticerr:1;
  98522. + unsigned reserved4_7:4;
  98523. + /** Frame or Microframe Number of the received SOF */
  98524. + unsigned soffn:14;
  98525. + unsigned reserved22_31:10;
  98526. + } b;
  98527. +} dsts_data_t;
  98528. +
  98529. +/**
  98530. + * This union represents the bit fields in the Device IN EP Interrupt
  98531. + * Register and the Device IN EP Common Mask Register.
  98532. + *
  98533. + * - Read the register into the <i>d32</i> member then set/clear the
  98534. + * bits using the <i>b</i>it elements.
  98535. + */
  98536. +typedef union diepint_data {
  98537. + /** raw register data */
  98538. + uint32_t d32;
  98539. + /** register bits */
  98540. + struct {
  98541. + /** Transfer complete mask */
  98542. + unsigned xfercompl:1;
  98543. + /** Endpoint disable mask */
  98544. + unsigned epdisabled:1;
  98545. + /** AHB Error mask */
  98546. + unsigned ahberr:1;
  98547. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  98548. + unsigned timeout:1;
  98549. + /** IN Token received with TxF Empty mask */
  98550. + unsigned intktxfemp:1;
  98551. + /** IN Token Received with EP mismatch mask */
  98552. + unsigned intknepmis:1;
  98553. + /** IN Endpoint NAK Effective mask */
  98554. + unsigned inepnakeff:1;
  98555. + /** Reserved */
  98556. + unsigned emptyintr:1;
  98557. +
  98558. + unsigned txfifoundrn:1;
  98559. +
  98560. + /** BNA Interrupt mask */
  98561. + unsigned bna:1;
  98562. +
  98563. + unsigned reserved10_12:3;
  98564. + /** BNA Interrupt mask */
  98565. + unsigned nak:1;
  98566. +
  98567. + unsigned reserved14_31:18;
  98568. + } b;
  98569. +} diepint_data_t;
  98570. +
  98571. +/**
  98572. + * This union represents the bit fields in the Device IN EP
  98573. + * Common/Dedicated Interrupt Mask Register.
  98574. + */
  98575. +typedef union diepint_data diepmsk_data_t;
  98576. +
  98577. +/**
  98578. + * This union represents the bit fields in the Device OUT EP Interrupt
  98579. + * Registerand Device OUT EP Common Interrupt Mask Register.
  98580. + *
  98581. + * - Read the register into the <i>d32</i> member then set/clear the
  98582. + * bits using the <i>b</i>it elements.
  98583. + */
  98584. +typedef union doepint_data {
  98585. + /** raw register data */
  98586. + uint32_t d32;
  98587. + /** register bits */
  98588. + struct {
  98589. + /** Transfer complete */
  98590. + unsigned xfercompl:1;
  98591. + /** Endpoint disable */
  98592. + unsigned epdisabled:1;
  98593. + /** AHB Error */
  98594. + unsigned ahberr:1;
  98595. + /** Setup Phase Done (contorl EPs) */
  98596. + unsigned setup:1;
  98597. + /** OUT Token Received when Endpoint Disabled */
  98598. + unsigned outtknepdis:1;
  98599. +
  98600. + unsigned stsphsercvd:1;
  98601. + /** Back-to-Back SETUP Packets Received */
  98602. + unsigned back2backsetup:1;
  98603. +
  98604. + unsigned reserved7:1;
  98605. + /** OUT packet Error */
  98606. + unsigned outpkterr:1;
  98607. + /** BNA Interrupt */
  98608. + unsigned bna:1;
  98609. +
  98610. + unsigned reserved10:1;
  98611. + /** Packet Drop Status */
  98612. + unsigned pktdrpsts:1;
  98613. + /** Babble Interrupt */
  98614. + unsigned babble:1;
  98615. + /** NAK Interrupt */
  98616. + unsigned nak:1;
  98617. + /** NYET Interrupt */
  98618. + unsigned nyet:1;
  98619. + /** Bit indicating setup packet received */
  98620. + unsigned sr:1;
  98621. +
  98622. + unsigned reserved16_31:16;
  98623. + } b;
  98624. +} doepint_data_t;
  98625. +
  98626. +/**
  98627. + * This union represents the bit fields in the Device OUT EP
  98628. + * Common/Dedicated Interrupt Mask Register.
  98629. + */
  98630. +typedef union doepint_data doepmsk_data_t;
  98631. +
  98632. +/**
  98633. + * This union represents the bit fields in the Device All EP Interrupt
  98634. + * and Mask Registers.
  98635. + * - Read the register into the <i>d32</i> member then set/clear the
  98636. + * bits using the <i>b</i>it elements.
  98637. + */
  98638. +typedef union daint_data {
  98639. + /** raw register data */
  98640. + uint32_t d32;
  98641. + /** register bits */
  98642. + struct {
  98643. + /** IN Endpoint bits */
  98644. + unsigned in:16;
  98645. + /** OUT Endpoint bits */
  98646. + unsigned out:16;
  98647. + } ep;
  98648. + struct {
  98649. + /** IN Endpoint bits */
  98650. + unsigned inep0:1;
  98651. + unsigned inep1:1;
  98652. + unsigned inep2:1;
  98653. + unsigned inep3:1;
  98654. + unsigned inep4:1;
  98655. + unsigned inep5:1;
  98656. + unsigned inep6:1;
  98657. + unsigned inep7:1;
  98658. + unsigned inep8:1;
  98659. + unsigned inep9:1;
  98660. + unsigned inep10:1;
  98661. + unsigned inep11:1;
  98662. + unsigned inep12:1;
  98663. + unsigned inep13:1;
  98664. + unsigned inep14:1;
  98665. + unsigned inep15:1;
  98666. + /** OUT Endpoint bits */
  98667. + unsigned outep0:1;
  98668. + unsigned outep1:1;
  98669. + unsigned outep2:1;
  98670. + unsigned outep3:1;
  98671. + unsigned outep4:1;
  98672. + unsigned outep5:1;
  98673. + unsigned outep6:1;
  98674. + unsigned outep7:1;
  98675. + unsigned outep8:1;
  98676. + unsigned outep9:1;
  98677. + unsigned outep10:1;
  98678. + unsigned outep11:1;
  98679. + unsigned outep12:1;
  98680. + unsigned outep13:1;
  98681. + unsigned outep14:1;
  98682. + unsigned outep15:1;
  98683. + } b;
  98684. +} daint_data_t;
  98685. +
  98686. +/**
  98687. + * This union represents the bit fields in the Device IN Token Queue
  98688. + * Read Registers.
  98689. + * - Read the register into the <i>d32</i> member.
  98690. + * - READ-ONLY Register
  98691. + */
  98692. +typedef union dtknq1_data {
  98693. + /** raw register data */
  98694. + uint32_t d32;
  98695. + /** register bits */
  98696. + struct {
  98697. + /** In Token Queue Write Pointer */
  98698. + unsigned intknwptr:5;
  98699. + /** Reserved */
  98700. + unsigned reserved05_06:2;
  98701. + /** write pointer has wrapped. */
  98702. + unsigned wrap_bit:1;
  98703. + /** EP Numbers of IN Tokens 0 ... 4 */
  98704. + unsigned epnums0_5:24;
  98705. + } b;
  98706. +} dtknq1_data_t;
  98707. +
  98708. +/**
  98709. + * This union represents Threshold control Register
  98710. + * - Read and write the register into the <i>d32</i> member.
  98711. + * - READ-WRITABLE Register
  98712. + */
  98713. +typedef union dthrctl_data {
  98714. + /** raw register data */
  98715. + uint32_t d32;
  98716. + /** register bits */
  98717. + struct {
  98718. + /** non ISO Tx Thr. Enable */
  98719. + unsigned non_iso_thr_en:1;
  98720. + /** ISO Tx Thr. Enable */
  98721. + unsigned iso_thr_en:1;
  98722. + /** Tx Thr. Length */
  98723. + unsigned tx_thr_len:9;
  98724. + /** AHB Threshold ratio */
  98725. + unsigned ahb_thr_ratio:2;
  98726. + /** Reserved */
  98727. + unsigned reserved13_15:3;
  98728. + /** Rx Thr. Enable */
  98729. + unsigned rx_thr_en:1;
  98730. + /** Rx Thr. Length */
  98731. + unsigned rx_thr_len:9;
  98732. + unsigned reserved26:1;
  98733. + /** Arbiter Parking Enable*/
  98734. + unsigned arbprken:1;
  98735. + /** Reserved */
  98736. + unsigned reserved28_31:4;
  98737. + } b;
  98738. +} dthrctl_data_t;
  98739. +
  98740. +/**
  98741. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  98742. + * 900h-AFCh</i>
  98743. + *
  98744. + * There will be one set of endpoint registers per logical endpoint
  98745. + * implemented.
  98746. + *
  98747. + * <i>These registers are visible only in Device mode and must not be
  98748. + * accessed in Host mode, as the results are unknown.</i>
  98749. + */
  98750. +typedef struct dwc_otg_dev_in_ep_regs {
  98751. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  98752. + * (ep_num * 20h) + 00h</i> */
  98753. + volatile uint32_t diepctl;
  98754. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  98755. + uint32_t reserved04;
  98756. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  98757. + * (ep_num * 20h) + 08h</i> */
  98758. + volatile uint32_t diepint;
  98759. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  98760. + uint32_t reserved0C;
  98761. + /** Device IN Endpoint Transfer Size
  98762. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  98763. + volatile uint32_t dieptsiz;
  98764. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  98765. + * (ep_num * 20h) + 14h</i> */
  98766. + volatile uint32_t diepdma;
  98767. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  98768. + * (ep_num * 20h) + 18h</i> */
  98769. + volatile uint32_t dtxfsts;
  98770. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  98771. + * (ep_num * 20h) + 1Ch</i> */
  98772. + volatile uint32_t diepdmab;
  98773. +} dwc_otg_dev_in_ep_regs_t;
  98774. +
  98775. +/**
  98776. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  98777. + * B00h-CFCh</i>
  98778. + *
  98779. + * There will be one set of endpoint registers per logical endpoint
  98780. + * implemented.
  98781. + *
  98782. + * <i>These registers are visible only in Device mode and must not be
  98783. + * accessed in Host mode, as the results are unknown.</i>
  98784. + */
  98785. +typedef struct dwc_otg_dev_out_ep_regs {
  98786. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  98787. + * (ep_num * 20h) + 00h</i> */
  98788. + volatile uint32_t doepctl;
  98789. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  98790. + uint32_t reserved04;
  98791. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  98792. + * (ep_num * 20h) + 08h</i> */
  98793. + volatile uint32_t doepint;
  98794. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  98795. + uint32_t reserved0C;
  98796. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  98797. + * B00h + (ep_num * 20h) + 10h</i> */
  98798. + volatile uint32_t doeptsiz;
  98799. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  98800. + * + (ep_num * 20h) + 14h</i> */
  98801. + volatile uint32_t doepdma;
  98802. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  98803. + uint32_t unused;
  98804. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  98805. + * + (ep_num * 20h) + 1Ch</i> */
  98806. + uint32_t doepdmab;
  98807. +} dwc_otg_dev_out_ep_regs_t;
  98808. +
  98809. +/**
  98810. + * This union represents the bit fields in the Device EP Control
  98811. + * Register. Read the register into the <i>d32</i> member then
  98812. + * set/clear the bits using the <i>b</i>it elements.
  98813. + */
  98814. +typedef union depctl_data {
  98815. + /** raw register data */
  98816. + uint32_t d32;
  98817. + /** register bits */
  98818. + struct {
  98819. + /** Maximum Packet Size
  98820. + * IN/OUT EPn
  98821. + * IN/OUT EP0 - 2 bits
  98822. + * 2'b00: 64 Bytes
  98823. + * 2'b01: 32
  98824. + * 2'b10: 16
  98825. + * 2'b11: 8 */
  98826. + unsigned mps:11;
  98827. +#define DWC_DEP0CTL_MPS_64 0
  98828. +#define DWC_DEP0CTL_MPS_32 1
  98829. +#define DWC_DEP0CTL_MPS_16 2
  98830. +#define DWC_DEP0CTL_MPS_8 3
  98831. +
  98832. + /** Next Endpoint
  98833. + * IN EPn/IN EP0
  98834. + * OUT EPn/OUT EP0 - reserved */
  98835. + unsigned nextep:4;
  98836. +
  98837. + /** USB Active Endpoint */
  98838. + unsigned usbactep:1;
  98839. +
  98840. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  98841. + * This field contains the PID of the packet going to
  98842. + * be received or transmitted on this endpoint. The
  98843. + * application should program the PID of the first
  98844. + * packet going to be received or transmitted on this
  98845. + * endpoint , after the endpoint is
  98846. + * activated. Application use the SetD1PID and
  98847. + * SetD0PID fields of this register to program either
  98848. + * D0 or D1 PID.
  98849. + *
  98850. + * The encoding for this field is
  98851. + * - 0: D0
  98852. + * - 1: D1
  98853. + */
  98854. + unsigned dpid:1;
  98855. +
  98856. + /** NAK Status */
  98857. + unsigned naksts:1;
  98858. +
  98859. + /** Endpoint Type
  98860. + * 2'b00: Control
  98861. + * 2'b01: Isochronous
  98862. + * 2'b10: Bulk
  98863. + * 2'b11: Interrupt */
  98864. + unsigned eptype:2;
  98865. +
  98866. + /** Snoop Mode
  98867. + * OUT EPn/OUT EP0
  98868. + * IN EPn/IN EP0 - reserved */
  98869. + unsigned snp:1;
  98870. +
  98871. + /** Stall Handshake */
  98872. + unsigned stall:1;
  98873. +
  98874. + /** Tx Fifo Number
  98875. + * IN EPn/IN EP0
  98876. + * OUT EPn/OUT EP0 - reserved */
  98877. + unsigned txfnum:4;
  98878. +
  98879. + /** Clear NAK */
  98880. + unsigned cnak:1;
  98881. + /** Set NAK */
  98882. + unsigned snak:1;
  98883. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  98884. + * Writing to this field sets the Endpoint DPID (DPID)
  98885. + * field in this register to DATA0. Set Even
  98886. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  98887. + * Writing to this field sets the Even/Odd
  98888. + * (micro)frame (EO_FrNum) field to even (micro)
  98889. + * frame.
  98890. + */
  98891. + unsigned setd0pid:1;
  98892. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  98893. + * Writing to this field sets the Endpoint DPID (DPID)
  98894. + * field in this register to DATA1 Set Odd
  98895. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  98896. + * Writing to this field sets the Even/Odd
  98897. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  98898. + */
  98899. + unsigned setd1pid:1;
  98900. +
  98901. + /** Endpoint Disable */
  98902. + unsigned epdis:1;
  98903. + /** Endpoint Enable */
  98904. + unsigned epena:1;
  98905. + } b;
  98906. +} depctl_data_t;
  98907. +
  98908. +/**
  98909. + * This union represents the bit fields in the Device EP Transfer
  98910. + * Size Register. Read the register into the <i>d32</i> member then
  98911. + * set/clear the bits using the <i>b</i>it elements.
  98912. + */
  98913. +typedef union deptsiz_data {
  98914. + /** raw register data */
  98915. + uint32_t d32;
  98916. + /** register bits */
  98917. + struct {
  98918. + /** Transfer size */
  98919. + unsigned xfersize:19;
  98920. +/** Max packet count for EP (pow(2,10)-1) */
  98921. +#define MAX_PKT_CNT 1023
  98922. + /** Packet Count */
  98923. + unsigned pktcnt:10;
  98924. + /** Multi Count - Periodic IN endpoints */
  98925. + unsigned mc:2;
  98926. + unsigned reserved:1;
  98927. + } b;
  98928. +} deptsiz_data_t;
  98929. +
  98930. +/**
  98931. + * This union represents the bit fields in the Device EP 0 Transfer
  98932. + * Size Register. Read the register into the <i>d32</i> member then
  98933. + * set/clear the bits using the <i>b</i>it elements.
  98934. + */
  98935. +typedef union deptsiz0_data {
  98936. + /** raw register data */
  98937. + uint32_t d32;
  98938. + /** register bits */
  98939. + struct {
  98940. + /** Transfer size */
  98941. + unsigned xfersize:7;
  98942. + /** Reserved */
  98943. + unsigned reserved7_18:12;
  98944. + /** Packet Count */
  98945. + unsigned pktcnt:2;
  98946. + /** Reserved */
  98947. + unsigned reserved21_28:8;
  98948. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  98949. + unsigned supcnt:2;
  98950. + unsigned reserved31;
  98951. + } b;
  98952. +} deptsiz0_data_t;
  98953. +
  98954. +/////////////////////////////////////////////////
  98955. +// DMA Descriptor Specific Structures
  98956. +//
  98957. +
  98958. +/** Buffer status definitions */
  98959. +
  98960. +#define BS_HOST_READY 0x0
  98961. +#define BS_DMA_BUSY 0x1
  98962. +#define BS_DMA_DONE 0x2
  98963. +#define BS_HOST_BUSY 0x3
  98964. +
  98965. +/** Receive/Transmit status definitions */
  98966. +
  98967. +#define RTS_SUCCESS 0x0
  98968. +#define RTS_BUFFLUSH 0x1
  98969. +#define RTS_RESERVED 0x2
  98970. +#define RTS_BUFERR 0x3
  98971. +
  98972. +/**
  98973. + * This union represents the bit fields in the DMA Descriptor
  98974. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  98975. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  98976. + * <i>b_iso_in</i> elements.
  98977. + */
  98978. +typedef union dev_dma_desc_sts {
  98979. + /** raw register data */
  98980. + uint32_t d32;
  98981. + /** quadlet bits */
  98982. + struct {
  98983. + /** Received number of bytes */
  98984. + unsigned bytes:16;
  98985. + /** NAK bit - only for OUT EPs */
  98986. + unsigned nak:1;
  98987. + unsigned reserved17_22:6;
  98988. + /** Multiple Transfer - only for OUT EPs */
  98989. + unsigned mtrf:1;
  98990. + /** Setup Packet received - only for OUT EPs */
  98991. + unsigned sr:1;
  98992. + /** Interrupt On Complete */
  98993. + unsigned ioc:1;
  98994. + /** Short Packet */
  98995. + unsigned sp:1;
  98996. + /** Last */
  98997. + unsigned l:1;
  98998. + /** Receive Status */
  98999. + unsigned sts:2;
  99000. + /** Buffer Status */
  99001. + unsigned bs:2;
  99002. + } b;
  99003. +
  99004. +//#ifdef DWC_EN_ISOC
  99005. + /** iso out quadlet bits */
  99006. + struct {
  99007. + /** Received number of bytes */
  99008. + unsigned rxbytes:11;
  99009. +
  99010. + unsigned reserved11:1;
  99011. + /** Frame Number */
  99012. + unsigned framenum:11;
  99013. + /** Received ISO Data PID */
  99014. + unsigned pid:2;
  99015. + /** Interrupt On Complete */
  99016. + unsigned ioc:1;
  99017. + /** Short Packet */
  99018. + unsigned sp:1;
  99019. + /** Last */
  99020. + unsigned l:1;
  99021. + /** Receive Status */
  99022. + unsigned rxsts:2;
  99023. + /** Buffer Status */
  99024. + unsigned bs:2;
  99025. + } b_iso_out;
  99026. +
  99027. + /** iso in quadlet bits */
  99028. + struct {
  99029. + /** Transmited number of bytes */
  99030. + unsigned txbytes:12;
  99031. + /** Frame Number */
  99032. + unsigned framenum:11;
  99033. + /** Transmited ISO Data PID */
  99034. + unsigned pid:2;
  99035. + /** Interrupt On Complete */
  99036. + unsigned ioc:1;
  99037. + /** Short Packet */
  99038. + unsigned sp:1;
  99039. + /** Last */
  99040. + unsigned l:1;
  99041. + /** Transmit Status */
  99042. + unsigned txsts:2;
  99043. + /** Buffer Status */
  99044. + unsigned bs:2;
  99045. + } b_iso_in;
  99046. +//#endif /* DWC_EN_ISOC */
  99047. +} dev_dma_desc_sts_t;
  99048. +
  99049. +/**
  99050. + * DMA Descriptor structure
  99051. + *
  99052. + * DMA Descriptor structure contains two quadlets:
  99053. + * Status quadlet and Data buffer pointer.
  99054. + */
  99055. +typedef struct dwc_otg_dev_dma_desc {
  99056. + /** DMA Descriptor status quadlet */
  99057. + dev_dma_desc_sts_t status;
  99058. + /** DMA Descriptor data buffer pointer */
  99059. + uint32_t buf;
  99060. +} dwc_otg_dev_dma_desc_t;
  99061. +
  99062. +/**
  99063. + * The dwc_otg_dev_if structure contains information needed to manage
  99064. + * the DWC_otg controller acting in device mode. It represents the
  99065. + * programming view of the device-specific aspects of the controller.
  99066. + */
  99067. +typedef struct dwc_otg_dev_if {
  99068. + /** Pointer to device Global registers.
  99069. + * Device Global Registers starting at offset 800h
  99070. + */
  99071. + dwc_otg_device_global_regs_t *dev_global_regs;
  99072. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  99073. +
  99074. + /**
  99075. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  99076. + */
  99077. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  99078. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  99079. +#define DWC_EP_REG_OFFSET 0x20
  99080. +
  99081. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  99082. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  99083. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  99084. +
  99085. + /* Device configuration information */
  99086. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  99087. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  99088. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  99089. +
  99090. + /** Size of periodic FIFOs (Bytes) */
  99091. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  99092. +
  99093. + /** Size of Tx FIFOs (Bytes) */
  99094. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  99095. +
  99096. + /** Thresholding enable flags and length varaiables **/
  99097. + uint16_t rx_thr_en;
  99098. + uint16_t iso_tx_thr_en;
  99099. + uint16_t non_iso_tx_thr_en;
  99100. +
  99101. + uint16_t rx_thr_length;
  99102. + uint16_t tx_thr_length;
  99103. +
  99104. + /**
  99105. + * Pointers to the DMA Descriptors for EP0 Control
  99106. + * transfers (virtual and physical)
  99107. + */
  99108. +
  99109. + /** 2 descriptors for SETUP packets */
  99110. + dwc_dma_t dma_setup_desc_addr[2];
  99111. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  99112. +
  99113. + /** Pointer to Descriptor with latest SETUP packet */
  99114. + dwc_otg_dev_dma_desc_t *psetup;
  99115. +
  99116. + /** Index of current SETUP handler descriptor */
  99117. + uint32_t setup_desc_index;
  99118. +
  99119. + /** Descriptor for Data In or Status In phases */
  99120. + dwc_dma_t dma_in_desc_addr;
  99121. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  99122. +
  99123. + /** Descriptor for Data Out or Status Out phases */
  99124. + dwc_dma_t dma_out_desc_addr;
  99125. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  99126. +
  99127. + /** Setup Packet Detected - if set clear NAK when queueing */
  99128. + uint32_t spd;
  99129. + /** Isoc ep pointer on which incomplete happens */
  99130. + void *isoc_ep;
  99131. +
  99132. +} dwc_otg_dev_if_t;
  99133. +
  99134. +/////////////////////////////////////////////////
  99135. +// Host Mode Register Structures
  99136. +//
  99137. +/**
  99138. + * The Host Global Registers structure defines the size and relative
  99139. + * field offsets for the Host Mode Global Registers. Host Global
  99140. + * Registers offsets 400h-7FFh.
  99141. +*/
  99142. +typedef struct dwc_otg_host_global_regs {
  99143. + /** Host Configuration Register. <i>Offset: 400h</i> */
  99144. + volatile uint32_t hcfg;
  99145. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  99146. + volatile uint32_t hfir;
  99147. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  99148. + volatile uint32_t hfnum;
  99149. + /** Reserved. <i>Offset: 40Ch</i> */
  99150. + uint32_t reserved40C;
  99151. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  99152. + volatile uint32_t hptxsts;
  99153. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  99154. + volatile uint32_t haint;
  99155. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  99156. + volatile uint32_t haintmsk;
  99157. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  99158. + volatile uint32_t hflbaddr;
  99159. +} dwc_otg_host_global_regs_t;
  99160. +
  99161. +/**
  99162. + * This union represents the bit fields in the Host Configuration Register.
  99163. + * Read the register into the <i>d32</i> member then set/clear the bits using
  99164. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  99165. + */
  99166. +typedef union hcfg_data {
  99167. + /** raw register data */
  99168. + uint32_t d32;
  99169. +
  99170. + /** register bits */
  99171. + struct {
  99172. + /** FS/LS Phy Clock Select */
  99173. + unsigned fslspclksel:2;
  99174. +#define DWC_HCFG_30_60_MHZ 0
  99175. +#define DWC_HCFG_48_MHZ 1
  99176. +#define DWC_HCFG_6_MHZ 2
  99177. +
  99178. + /** FS/LS Only Support */
  99179. + unsigned fslssupp:1;
  99180. + unsigned reserved3_6:4;
  99181. + /** Enable 32-KHz Suspend Mode */
  99182. + unsigned ena32khzs:1;
  99183. + /** Resume Validation Periiod */
  99184. + unsigned resvalid:8;
  99185. + unsigned reserved16_22:7;
  99186. + /** Enable Scatter/gather DMA in Host mode */
  99187. + unsigned descdma:1;
  99188. + /** Frame List Entries */
  99189. + unsigned frlisten:2;
  99190. + /** Enable Periodic Scheduling */
  99191. + unsigned perschedena:1;
  99192. + unsigned reserved27_30:4;
  99193. + unsigned modechtimen:1;
  99194. + } b;
  99195. +} hcfg_data_t;
  99196. +
  99197. +/**
  99198. + * This union represents the bit fields in the Host Frame Remaing/Number
  99199. + * Register.
  99200. + */
  99201. +typedef union hfir_data {
  99202. + /** raw register data */
  99203. + uint32_t d32;
  99204. +
  99205. + /** register bits */
  99206. + struct {
  99207. + unsigned frint:16;
  99208. + unsigned hfirrldctrl:1;
  99209. + unsigned reserved:15;
  99210. + } b;
  99211. +} hfir_data_t;
  99212. +
  99213. +/**
  99214. + * This union represents the bit fields in the Host Frame Remaing/Number
  99215. + * Register.
  99216. + */
  99217. +typedef union hfnum_data {
  99218. + /** raw register data */
  99219. + uint32_t d32;
  99220. +
  99221. + /** register bits */
  99222. + struct {
  99223. + unsigned frnum:16;
  99224. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  99225. + unsigned frrem:16;
  99226. + } b;
  99227. +} hfnum_data_t;
  99228. +
  99229. +typedef union hptxsts_data {
  99230. + /** raw register data */
  99231. + uint32_t d32;
  99232. +
  99233. + /** register bits */
  99234. + struct {
  99235. + unsigned ptxfspcavail:16;
  99236. + unsigned ptxqspcavail:8;
  99237. + /** Top of the Periodic Transmit Request Queue
  99238. + * - bit 24 - Terminate (last entry for the selected channel)
  99239. + * - bits 26:25 - Token Type
  99240. + * - 2'b00 - Zero length
  99241. + * - 2'b01 - Ping
  99242. + * - 2'b10 - Disable
  99243. + * - bits 30:27 - Channel Number
  99244. + * - bit 31 - Odd/even microframe
  99245. + */
  99246. + unsigned ptxqtop_terminate:1;
  99247. + unsigned ptxqtop_token:2;
  99248. + unsigned ptxqtop_chnum:4;
  99249. + unsigned ptxqtop_odd:1;
  99250. + } b;
  99251. +} hptxsts_data_t;
  99252. +
  99253. +/**
  99254. + * This union represents the bit fields in the Host Port Control and Status
  99255. + * Register. Read the register into the <i>d32</i> member then set/clear the
  99256. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  99257. + * hprt0 register.
  99258. + */
  99259. +typedef union hprt0_data {
  99260. + /** raw register data */
  99261. + uint32_t d32;
  99262. + /** register bits */
  99263. + struct {
  99264. + unsigned prtconnsts:1;
  99265. + unsigned prtconndet:1;
  99266. + unsigned prtena:1;
  99267. + unsigned prtenchng:1;
  99268. + unsigned prtovrcurract:1;
  99269. + unsigned prtovrcurrchng:1;
  99270. + unsigned prtres:1;
  99271. + unsigned prtsusp:1;
  99272. + unsigned prtrst:1;
  99273. + unsigned reserved9:1;
  99274. + unsigned prtlnsts:2;
  99275. + unsigned prtpwr:1;
  99276. + unsigned prttstctl:4;
  99277. + unsigned prtspd:2;
  99278. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  99279. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  99280. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  99281. + unsigned reserved19_31:13;
  99282. + } b;
  99283. +} hprt0_data_t;
  99284. +
  99285. +/**
  99286. + * This union represents the bit fields in the Host All Interrupt
  99287. + * Register.
  99288. + */
  99289. +typedef union haint_data {
  99290. + /** raw register data */
  99291. + uint32_t d32;
  99292. + /** register bits */
  99293. + struct {
  99294. + unsigned ch0:1;
  99295. + unsigned ch1:1;
  99296. + unsigned ch2:1;
  99297. + unsigned ch3:1;
  99298. + unsigned ch4:1;
  99299. + unsigned ch5:1;
  99300. + unsigned ch6:1;
  99301. + unsigned ch7:1;
  99302. + unsigned ch8:1;
  99303. + unsigned ch9:1;
  99304. + unsigned ch10:1;
  99305. + unsigned ch11:1;
  99306. + unsigned ch12:1;
  99307. + unsigned ch13:1;
  99308. + unsigned ch14:1;
  99309. + unsigned ch15:1;
  99310. + unsigned reserved:16;
  99311. + } b;
  99312. +
  99313. + struct {
  99314. + unsigned chint:16;
  99315. + unsigned reserved:16;
  99316. + } b2;
  99317. +} haint_data_t;
  99318. +
  99319. +/**
  99320. + * This union represents the bit fields in the Host All Interrupt
  99321. + * Register.
  99322. + */
  99323. +typedef union haintmsk_data {
  99324. + /** raw register data */
  99325. + uint32_t d32;
  99326. + /** register bits */
  99327. + struct {
  99328. + unsigned ch0:1;
  99329. + unsigned ch1:1;
  99330. + unsigned ch2:1;
  99331. + unsigned ch3:1;
  99332. + unsigned ch4:1;
  99333. + unsigned ch5:1;
  99334. + unsigned ch6:1;
  99335. + unsigned ch7:1;
  99336. + unsigned ch8:1;
  99337. + unsigned ch9:1;
  99338. + unsigned ch10:1;
  99339. + unsigned ch11:1;
  99340. + unsigned ch12:1;
  99341. + unsigned ch13:1;
  99342. + unsigned ch14:1;
  99343. + unsigned ch15:1;
  99344. + unsigned reserved:16;
  99345. + } b;
  99346. +
  99347. + struct {
  99348. + unsigned chint:16;
  99349. + unsigned reserved:16;
  99350. + } b2;
  99351. +} haintmsk_data_t;
  99352. +
  99353. +/**
  99354. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  99355. + */
  99356. +typedef struct dwc_otg_hc_regs {
  99357. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  99358. + volatile uint32_t hcchar;
  99359. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  99360. + volatile uint32_t hcsplt;
  99361. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  99362. + volatile uint32_t hcint;
  99363. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  99364. + volatile uint32_t hcintmsk;
  99365. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  99366. + volatile uint32_t hctsiz;
  99367. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  99368. + volatile uint32_t hcdma;
  99369. + volatile uint32_t reserved;
  99370. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  99371. + volatile uint32_t hcdmab;
  99372. +} dwc_otg_hc_regs_t;
  99373. +
  99374. +/**
  99375. + * This union represents the bit fields in the Host Channel Characteristics
  99376. + * Register. Read the register into the <i>d32</i> member then set/clear the
  99377. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  99378. + * hcchar register.
  99379. + */
  99380. +typedef union hcchar_data {
  99381. + /** raw register data */
  99382. + uint32_t d32;
  99383. +
  99384. + /** register bits */
  99385. + struct {
  99386. + /** Maximum packet size in bytes */
  99387. + unsigned mps:11;
  99388. +
  99389. + /** Endpoint number */
  99390. + unsigned epnum:4;
  99391. +
  99392. + /** 0: OUT, 1: IN */
  99393. + unsigned epdir:1;
  99394. +
  99395. + unsigned reserved:1;
  99396. +
  99397. + /** 0: Full/high speed device, 1: Low speed device */
  99398. + unsigned lspddev:1;
  99399. +
  99400. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  99401. + unsigned eptype:2;
  99402. +
  99403. + /** Packets per frame for periodic transfers. 0 is reserved. */
  99404. + unsigned multicnt:2;
  99405. +
  99406. + /** Device address */
  99407. + unsigned devaddr:7;
  99408. +
  99409. + /**
  99410. + * Frame to transmit periodic transaction.
  99411. + * 0: even, 1: odd
  99412. + */
  99413. + unsigned oddfrm:1;
  99414. +
  99415. + /** Channel disable */
  99416. + unsigned chdis:1;
  99417. +
  99418. + /** Channel enable */
  99419. + unsigned chen:1;
  99420. + } b;
  99421. +} hcchar_data_t;
  99422. +
  99423. +typedef union hcsplt_data {
  99424. + /** raw register data */
  99425. + uint32_t d32;
  99426. +
  99427. + /** register bits */
  99428. + struct {
  99429. + /** Port Address */
  99430. + unsigned prtaddr:7;
  99431. +
  99432. + /** Hub Address */
  99433. + unsigned hubaddr:7;
  99434. +
  99435. + /** Transaction Position */
  99436. + unsigned xactpos:2;
  99437. +#define DWC_HCSPLIT_XACTPOS_MID 0
  99438. +#define DWC_HCSPLIT_XACTPOS_END 1
  99439. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  99440. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  99441. +
  99442. + /** Do Complete Split */
  99443. + unsigned compsplt:1;
  99444. +
  99445. + /** Reserved */
  99446. + unsigned reserved:14;
  99447. +
  99448. + /** Split Enble */
  99449. + unsigned spltena:1;
  99450. + } b;
  99451. +} hcsplt_data_t;
  99452. +
  99453. +/**
  99454. + * This union represents the bit fields in the Host All Interrupt
  99455. + * Register.
  99456. + */
  99457. +typedef union hcint_data {
  99458. + /** raw register data */
  99459. + uint32_t d32;
  99460. + /** register bits */
  99461. + struct {
  99462. + /** Transfer Complete */
  99463. + unsigned xfercomp:1;
  99464. + /** Channel Halted */
  99465. + unsigned chhltd:1;
  99466. + /** AHB Error */
  99467. + unsigned ahberr:1;
  99468. + /** STALL Response Received */
  99469. + unsigned stall:1;
  99470. + /** NAK Response Received */
  99471. + unsigned nak:1;
  99472. + /** ACK Response Received */
  99473. + unsigned ack:1;
  99474. + /** NYET Response Received */
  99475. + unsigned nyet:1;
  99476. + /** Transaction Err */
  99477. + unsigned xacterr:1;
  99478. + /** Babble Error */
  99479. + unsigned bblerr:1;
  99480. + /** Frame Overrun */
  99481. + unsigned frmovrun:1;
  99482. + /** Data Toggle Error */
  99483. + unsigned datatglerr:1;
  99484. + /** Buffer Not Available (only for DDMA mode) */
  99485. + unsigned bna:1;
  99486. + /** Exessive transaction error (only for DDMA mode) */
  99487. + unsigned xcs_xact:1;
  99488. + /** Frame List Rollover interrupt */
  99489. + unsigned frm_list_roll:1;
  99490. + /** Reserved */
  99491. + unsigned reserved14_31:18;
  99492. + } b;
  99493. +} hcint_data_t;
  99494. +
  99495. +/**
  99496. + * This union represents the bit fields in the Host Channel Interrupt Mask
  99497. + * Register. Read the register into the <i>d32</i> member then set/clear the
  99498. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  99499. + * hcintmsk register.
  99500. + */
  99501. +typedef union hcintmsk_data {
  99502. + /** raw register data */
  99503. + uint32_t d32;
  99504. +
  99505. + /** register bits */
  99506. + struct {
  99507. + unsigned xfercompl:1;
  99508. + unsigned chhltd:1;
  99509. + unsigned ahberr:1;
  99510. + unsigned stall:1;
  99511. + unsigned nak:1;
  99512. + unsigned ack:1;
  99513. + unsigned nyet:1;
  99514. + unsigned xacterr:1;
  99515. + unsigned bblerr:1;
  99516. + unsigned frmovrun:1;
  99517. + unsigned datatglerr:1;
  99518. + unsigned bna:1;
  99519. + unsigned xcs_xact:1;
  99520. + unsigned frm_list_roll:1;
  99521. + unsigned reserved14_31:18;
  99522. + } b;
  99523. +} hcintmsk_data_t;
  99524. +
  99525. +/**
  99526. + * This union represents the bit fields in the Host Channel Transfer Size
  99527. + * Register. Read the register into the <i>d32</i> member then set/clear the
  99528. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  99529. + * hcchar register.
  99530. + */
  99531. +
  99532. +typedef union hctsiz_data {
  99533. + /** raw register data */
  99534. + uint32_t d32;
  99535. +
  99536. + /** register bits */
  99537. + struct {
  99538. + /** Total transfer size in bytes */
  99539. + unsigned xfersize:19;
  99540. +
  99541. + /** Data packets to transfer */
  99542. + unsigned pktcnt:10;
  99543. +
  99544. + /**
  99545. + * Packet ID for next data packet
  99546. + * 0: DATA0
  99547. + * 1: DATA2
  99548. + * 2: DATA1
  99549. + * 3: MDATA (non-Control), SETUP (Control)
  99550. + */
  99551. + unsigned pid:2;
  99552. +#define DWC_HCTSIZ_DATA0 0
  99553. +#define DWC_HCTSIZ_DATA1 2
  99554. +#define DWC_HCTSIZ_DATA2 1
  99555. +#define DWC_HCTSIZ_MDATA 3
  99556. +#define DWC_HCTSIZ_SETUP 3
  99557. +
  99558. + /** Do PING protocol when 1 */
  99559. + unsigned dopng:1;
  99560. + } b;
  99561. +
  99562. + /** register bits */
  99563. + struct {
  99564. + /** Scheduling information */
  99565. + unsigned schinfo:8;
  99566. +
  99567. + /** Number of transfer descriptors.
  99568. + * Max value:
  99569. + * 64 in general,
  99570. + * 256 only for HS isochronous endpoint.
  99571. + */
  99572. + unsigned ntd:8;
  99573. +
  99574. + /** Data packets to transfer */
  99575. + unsigned reserved16_28:13;
  99576. +
  99577. + /**
  99578. + * Packet ID for next data packet
  99579. + * 0: DATA0
  99580. + * 1: DATA2
  99581. + * 2: DATA1
  99582. + * 3: MDATA (non-Control)
  99583. + */
  99584. + unsigned pid:2;
  99585. +
  99586. + /** Do PING protocol when 1 */
  99587. + unsigned dopng:1;
  99588. + } b_ddma;
  99589. +} hctsiz_data_t;
  99590. +
  99591. +/**
  99592. + * This union represents the bit fields in the Host DMA Address
  99593. + * Register used in Descriptor DMA mode.
  99594. + */
  99595. +typedef union hcdma_data {
  99596. + /** raw register data */
  99597. + uint32_t d32;
  99598. + /** register bits */
  99599. + struct {
  99600. + unsigned reserved0_2:3;
  99601. + /** Current Transfer Descriptor. Not used for ISOC */
  99602. + unsigned ctd:8;
  99603. + /** Start Address of Descriptor List */
  99604. + unsigned dma_addr:21;
  99605. + } b;
  99606. +} hcdma_data_t;
  99607. +
  99608. +/**
  99609. + * This union represents the bit fields in the DMA Descriptor
  99610. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  99611. + * set/clear the bits using the <i>b</i>it elements.
  99612. + */
  99613. +typedef union host_dma_desc_sts {
  99614. + /** raw register data */
  99615. + uint32_t d32;
  99616. + /** quadlet bits */
  99617. +
  99618. + /* for non-isochronous */
  99619. + struct {
  99620. + /** Number of bytes */
  99621. + unsigned n_bytes:17;
  99622. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  99623. + unsigned qtd_offset:6;
  99624. + /**
  99625. + * Set to request the core to jump to alternate QTD if
  99626. + * Short Packet received - only for IN EPs
  99627. + */
  99628. + unsigned a_qtd:1;
  99629. + /**
  99630. + * Setup Packet bit. When set indicates that buffer contains
  99631. + * setup packet.
  99632. + */
  99633. + unsigned sup:1;
  99634. + /** Interrupt On Complete */
  99635. + unsigned ioc:1;
  99636. + /** End of List */
  99637. + unsigned eol:1;
  99638. + unsigned reserved27:1;
  99639. + /** Rx/Tx Status */
  99640. + unsigned sts:2;
  99641. +#define DMA_DESC_STS_PKTERR 1
  99642. + unsigned reserved30:1;
  99643. + /** Active Bit */
  99644. + unsigned a:1;
  99645. + } b;
  99646. + /* for isochronous */
  99647. + struct {
  99648. + /** Number of bytes */
  99649. + unsigned n_bytes:12;
  99650. + unsigned reserved12_24:13;
  99651. + /** Interrupt On Complete */
  99652. + unsigned ioc:1;
  99653. + unsigned reserved26_27:2;
  99654. + /** Rx/Tx Status */
  99655. + unsigned sts:2;
  99656. + unsigned reserved30:1;
  99657. + /** Active Bit */
  99658. + unsigned a:1;
  99659. + } b_isoc;
  99660. +} host_dma_desc_sts_t;
  99661. +
  99662. +#define MAX_DMA_DESC_SIZE 131071
  99663. +#define MAX_DMA_DESC_NUM_GENERIC 64
  99664. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  99665. +#define MAX_FRLIST_EN_NUM 64
  99666. +/**
  99667. + * Host-mode DMA Descriptor structure
  99668. + *
  99669. + * DMA Descriptor structure contains two quadlets:
  99670. + * Status quadlet and Data buffer pointer.
  99671. + */
  99672. +typedef struct dwc_otg_host_dma_desc {
  99673. + /** DMA Descriptor status quadlet */
  99674. + host_dma_desc_sts_t status;
  99675. + /** DMA Descriptor data buffer pointer */
  99676. + uint32_t buf;
  99677. +} dwc_otg_host_dma_desc_t;
  99678. +
  99679. +/** OTG Host Interface Structure.
  99680. + *
  99681. + * The OTG Host Interface Structure structure contains information
  99682. + * needed to manage the DWC_otg controller acting in host mode. It
  99683. + * represents the programming view of the host-specific aspects of the
  99684. + * controller.
  99685. + */
  99686. +typedef struct dwc_otg_host_if {
  99687. + /** Host Global Registers starting at offset 400h.*/
  99688. + dwc_otg_host_global_regs_t *host_global_regs;
  99689. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  99690. +
  99691. + /** Host Port 0 Control and Status Register */
  99692. + volatile uint32_t *hprt0;
  99693. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  99694. +
  99695. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  99696. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  99697. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  99698. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  99699. +
  99700. + /* Host configuration information */
  99701. + /** Number of Host Channels (range: 1-16) */
  99702. + uint8_t num_host_channels;
  99703. + /** Periodic EPs supported (0: no, 1: yes) */
  99704. + uint8_t perio_eps_supported;
  99705. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  99706. + uint16_t perio_tx_fifo_size;
  99707. +
  99708. +} dwc_otg_host_if_t;
  99709. +
  99710. +/**
  99711. + * This union represents the bit fields in the Power and Clock Gating Control
  99712. + * Register. Read the register into the <i>d32</i> member then set/clear the
  99713. + * bits using the <i>b</i>it elements.
  99714. + */
  99715. +typedef union pcgcctl_data {
  99716. + /** raw register data */
  99717. + uint32_t d32;
  99718. +
  99719. + /** register bits */
  99720. + struct {
  99721. + /** Stop Pclk */
  99722. + unsigned stoppclk:1;
  99723. + /** Gate Hclk */
  99724. + unsigned gatehclk:1;
  99725. + /** Power Clamp */
  99726. + unsigned pwrclmp:1;
  99727. + /** Reset Power Down Modules */
  99728. + unsigned rstpdwnmodule:1;
  99729. + /** Reserved */
  99730. + unsigned reserved:1;
  99731. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  99732. + unsigned enbl_sleep_gating:1;
  99733. + /** PHY In Sleep (PhySleep) */
  99734. + unsigned phy_in_sleep:1;
  99735. + /** Deep Sleep*/
  99736. + unsigned deep_sleep:1;
  99737. + unsigned resetaftsusp:1;
  99738. + unsigned restoremode:1;
  99739. + unsigned enbl_extnd_hiber:1;
  99740. + unsigned extnd_hiber_pwrclmp:1;
  99741. + unsigned extnd_hiber_switch:1;
  99742. + unsigned ess_reg_restored:1;
  99743. + unsigned prt_clk_sel:2;
  99744. + unsigned port_power:1;
  99745. + unsigned max_xcvrselect:2;
  99746. + unsigned max_termsel:1;
  99747. + unsigned mac_dev_addr:7;
  99748. + unsigned p2hd_dev_enum_spd:2;
  99749. + unsigned p2hd_prt_spd:2;
  99750. + unsigned if_dev_mode:1;
  99751. + } b;
  99752. +} pcgcctl_data_t;
  99753. +
  99754. +/**
  99755. + * This union represents the bit fields in the Global Data FIFO Software
  99756. + * Configuration Register. Read the register into the <i>d32</i> member then
  99757. + * set/clear the bits using the <i>b</i>it elements.
  99758. + */
  99759. +typedef union gdfifocfg_data {
  99760. + /* raw register data */
  99761. + uint32_t d32;
  99762. + /** register bits */
  99763. + struct {
  99764. + /** OTG Data FIFO depth */
  99765. + unsigned gdfifocfg:16;
  99766. + /** Start address of EP info controller */
  99767. + unsigned epinfobase:16;
  99768. + } b;
  99769. +} gdfifocfg_data_t;
  99770. +
  99771. +/**
  99772. + * This union represents the bit fields in the Global Power Down Register
  99773. + * Register. Read the register into the <i>d32</i> member then set/clear the
  99774. + * bits using the <i>b</i>it elements.
  99775. + */
  99776. +typedef union gpwrdn_data {
  99777. + /* raw register data */
  99778. + uint32_t d32;
  99779. +
  99780. + /** register bits */
  99781. + struct {
  99782. + /** PMU Interrupt Select */
  99783. + unsigned pmuintsel:1;
  99784. + /** PMU Active */
  99785. + unsigned pmuactv:1;
  99786. + /** Restore */
  99787. + unsigned restore:1;
  99788. + /** Power Down Clamp */
  99789. + unsigned pwrdnclmp:1;
  99790. + /** Power Down Reset */
  99791. + unsigned pwrdnrstn:1;
  99792. + /** Power Down Switch */
  99793. + unsigned pwrdnswtch:1;
  99794. + /** Disable VBUS */
  99795. + unsigned dis_vbus:1;
  99796. + /** Line State Change */
  99797. + unsigned lnstschng:1;
  99798. + /** Line state change mask */
  99799. + unsigned lnstchng_msk:1;
  99800. + /** Reset Detected */
  99801. + unsigned rst_det:1;
  99802. + /** Reset Detect mask */
  99803. + unsigned rst_det_msk:1;
  99804. + /** Disconnect Detected */
  99805. + unsigned disconn_det:1;
  99806. + /** Disconnect Detect mask */
  99807. + unsigned disconn_det_msk:1;
  99808. + /** Connect Detected*/
  99809. + unsigned connect_det:1;
  99810. + /** Connect Detected Mask*/
  99811. + unsigned connect_det_msk:1;
  99812. + /** SRP Detected */
  99813. + unsigned srp_det:1;
  99814. + /** SRP Detect mask */
  99815. + unsigned srp_det_msk:1;
  99816. + /** Status Change Interrupt */
  99817. + unsigned sts_chngint:1;
  99818. + /** Status Change Interrupt Mask */
  99819. + unsigned sts_chngint_msk:1;
  99820. + /** Line State */
  99821. + unsigned linestate:2;
  99822. + /** Indicates current mode(status of IDDIG signal) */
  99823. + unsigned idsts:1;
  99824. + /** B Session Valid signal status*/
  99825. + unsigned bsessvld:1;
  99826. + /** ADP Event Detected */
  99827. + unsigned adp_int:1;
  99828. + /** Multi Valued ID pin */
  99829. + unsigned mult_val_id_bc:5;
  99830. + /** Reserved 24_31 */
  99831. + unsigned reserved29_31:3;
  99832. + } b;
  99833. +} gpwrdn_data_t;
  99834. +
  99835. +#endif
  99836. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/Makefile linux-rpi/drivers/usb/host/dwc_otg/Makefile
  99837. --- linux-3.17.5/drivers/usb/host/dwc_otg/Makefile 1969-12-31 18:00:00.000000000 -0600
  99838. +++ linux-rpi/drivers/usb/host/dwc_otg/Makefile 2014-12-11 14:02:55.356418001 -0600
  99839. @@ -0,0 +1,82 @@
  99840. +#
  99841. +# Makefile for DWC_otg Highspeed USB controller driver
  99842. +#
  99843. +
  99844. +ifneq ($(KERNELRELEASE),)
  99845. +
  99846. +# Use the BUS_INTERFACE variable to compile the software for either
  99847. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  99848. +ifeq ($(BUS_INTERFACE),)
  99849. +# BUS_INTERFACE = -DPCI_INTERFACE
  99850. +# BUS_INTERFACE = -DLM_INTERFACE
  99851. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  99852. +endif
  99853. +
  99854. +#ccflags-y += -DDEBUG
  99855. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  99856. +
  99857. +# Use one of the following flags to compile the software in host-only or
  99858. +# device-only mode.
  99859. +#ccflags-y += -DDWC_HOST_ONLY
  99860. +#ccflags-y += -DDWC_DEVICE_ONLY
  99861. +
  99862. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  99863. +#ccflags-y += -DDWC_EN_ISOC
  99864. +ccflags-y += -I$(obj)/../dwc_common_port
  99865. +#ccflags-y += -I$(PORTLIB)
  99866. +ccflags-y += -DDWC_LINUX
  99867. +ccflags-y += $(CFI)
  99868. +ccflags-y += $(BUS_INTERFACE)
  99869. +#ccflags-y += -DDWC_DEV_SRPCAP
  99870. +
  99871. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  99872. +
  99873. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  99874. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  99875. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  99876. +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
  99877. +dwc_otg-objs += dwc_otg_adp.o
  99878. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  99879. +dwc_otg-objs += dwc_otg_fiq_stub.o
  99880. +ifneq ($(CFI),)
  99881. +dwc_otg-objs += dwc_otg_cfi.o
  99882. +endif
  99883. +
  99884. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  99885. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  99886. +
  99887. +ifneq ($(kernrel3),2.6.20)
  99888. +ccflags-y += $(CPPFLAGS)
  99889. +endif
  99890. +
  99891. +else
  99892. +
  99893. +PWD := $(shell pwd)
  99894. +PORTLIB := $(PWD)/../dwc_common_port
  99895. +
  99896. +# Command paths
  99897. +CTAGS := $(CTAGS)
  99898. +DOXYGEN := $(DOXYGEN)
  99899. +
  99900. +default: portlib
  99901. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  99902. +
  99903. +install: default
  99904. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  99905. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  99906. +
  99907. +portlib:
  99908. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  99909. + cp $(PORTLIB)/Module.symvers $(PWD)/
  99910. +
  99911. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  99912. + $(DOXYGEN) doc/doxygen.cfg
  99913. +
  99914. +tags: $(wildcard *.[hc])
  99915. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  99916. +
  99917. +
  99918. +clean:
  99919. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  99920. +
  99921. +endif
  99922. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm linux-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  99923. --- linux-3.17.5/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 1969-12-31 18:00:00.000000000 -0600
  99924. +++ linux-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 2014-12-11 14:02:55.400418001 -0600
  99925. @@ -0,0 +1,337 @@
  99926. +package dwc_otg_test;
  99927. +
  99928. +use strict;
  99929. +use Exporter ();
  99930. +
  99931. +use vars qw(@ISA @EXPORT
  99932. +$sysfsdir $paramdir $errors $params
  99933. +);
  99934. +
  99935. +@ISA = qw(Exporter);
  99936. +
  99937. +#
  99938. +# Globals
  99939. +#
  99940. +$sysfsdir = "/sys/devices/lm0";
  99941. +$paramdir = "/sys/module/dwc_otg";
  99942. +$errors = 0;
  99943. +
  99944. +$params = [
  99945. + {
  99946. + NAME => "otg_cap",
  99947. + DEFAULT => 0,
  99948. + ENUM => [],
  99949. + LOW => 0,
  99950. + HIGH => 2
  99951. + },
  99952. + {
  99953. + NAME => "dma_enable",
  99954. + DEFAULT => 0,
  99955. + ENUM => [],
  99956. + LOW => 0,
  99957. + HIGH => 1
  99958. + },
  99959. + {
  99960. + NAME => "dma_burst_size",
  99961. + DEFAULT => 32,
  99962. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  99963. + LOW => 1,
  99964. + HIGH => 256
  99965. + },
  99966. + {
  99967. + NAME => "host_speed",
  99968. + DEFAULT => 0,
  99969. + ENUM => [],
  99970. + LOW => 0,
  99971. + HIGH => 1
  99972. + },
  99973. + {
  99974. + NAME => "host_support_fs_ls_low_power",
  99975. + DEFAULT => 0,
  99976. + ENUM => [],
  99977. + LOW => 0,
  99978. + HIGH => 1
  99979. + },
  99980. + {
  99981. + NAME => "host_ls_low_power_phy_clk",
  99982. + DEFAULT => 0,
  99983. + ENUM => [],
  99984. + LOW => 0,
  99985. + HIGH => 1
  99986. + },
  99987. + {
  99988. + NAME => "dev_speed",
  99989. + DEFAULT => 0,
  99990. + ENUM => [],
  99991. + LOW => 0,
  99992. + HIGH => 1
  99993. + },
  99994. + {
  99995. + NAME => "enable_dynamic_fifo",
  99996. + DEFAULT => 1,
  99997. + ENUM => [],
  99998. + LOW => 0,
  99999. + HIGH => 1
  100000. + },
  100001. + {
  100002. + NAME => "data_fifo_size",
  100003. + DEFAULT => 8192,
  100004. + ENUM => [],
  100005. + LOW => 32,
  100006. + HIGH => 32768
  100007. + },
  100008. + {
  100009. + NAME => "dev_rx_fifo_size",
  100010. + DEFAULT => 1064,
  100011. + ENUM => [],
  100012. + LOW => 16,
  100013. + HIGH => 32768
  100014. + },
  100015. + {
  100016. + NAME => "dev_nperio_tx_fifo_size",
  100017. + DEFAULT => 1024,
  100018. + ENUM => [],
  100019. + LOW => 16,
  100020. + HIGH => 32768
  100021. + },
  100022. + {
  100023. + NAME => "dev_perio_tx_fifo_size_1",
  100024. + DEFAULT => 256,
  100025. + ENUM => [],
  100026. + LOW => 4,
  100027. + HIGH => 768
  100028. + },
  100029. + {
  100030. + NAME => "dev_perio_tx_fifo_size_2",
  100031. + DEFAULT => 256,
  100032. + ENUM => [],
  100033. + LOW => 4,
  100034. + HIGH => 768
  100035. + },
  100036. + {
  100037. + NAME => "dev_perio_tx_fifo_size_3",
  100038. + DEFAULT => 256,
  100039. + ENUM => [],
  100040. + LOW => 4,
  100041. + HIGH => 768
  100042. + },
  100043. + {
  100044. + NAME => "dev_perio_tx_fifo_size_4",
  100045. + DEFAULT => 256,
  100046. + ENUM => [],
  100047. + LOW => 4,
  100048. + HIGH => 768
  100049. + },
  100050. + {
  100051. + NAME => "dev_perio_tx_fifo_size_5",
  100052. + DEFAULT => 256,
  100053. + ENUM => [],
  100054. + LOW => 4,
  100055. + HIGH => 768
  100056. + },
  100057. + {
  100058. + NAME => "dev_perio_tx_fifo_size_6",
  100059. + DEFAULT => 256,
  100060. + ENUM => [],
  100061. + LOW => 4,
  100062. + HIGH => 768
  100063. + },
  100064. + {
  100065. + NAME => "dev_perio_tx_fifo_size_7",
  100066. + DEFAULT => 256,
  100067. + ENUM => [],
  100068. + LOW => 4,
  100069. + HIGH => 768
  100070. + },
  100071. + {
  100072. + NAME => "dev_perio_tx_fifo_size_8",
  100073. + DEFAULT => 256,
  100074. + ENUM => [],
  100075. + LOW => 4,
  100076. + HIGH => 768
  100077. + },
  100078. + {
  100079. + NAME => "dev_perio_tx_fifo_size_9",
  100080. + DEFAULT => 256,
  100081. + ENUM => [],
  100082. + LOW => 4,
  100083. + HIGH => 768
  100084. + },
  100085. + {
  100086. + NAME => "dev_perio_tx_fifo_size_10",
  100087. + DEFAULT => 256,
  100088. + ENUM => [],
  100089. + LOW => 4,
  100090. + HIGH => 768
  100091. + },
  100092. + {
  100093. + NAME => "dev_perio_tx_fifo_size_11",
  100094. + DEFAULT => 256,
  100095. + ENUM => [],
  100096. + LOW => 4,
  100097. + HIGH => 768
  100098. + },
  100099. + {
  100100. + NAME => "dev_perio_tx_fifo_size_12",
  100101. + DEFAULT => 256,
  100102. + ENUM => [],
  100103. + LOW => 4,
  100104. + HIGH => 768
  100105. + },
  100106. + {
  100107. + NAME => "dev_perio_tx_fifo_size_13",
  100108. + DEFAULT => 256,
  100109. + ENUM => [],
  100110. + LOW => 4,
  100111. + HIGH => 768
  100112. + },
  100113. + {
  100114. + NAME => "dev_perio_tx_fifo_size_14",
  100115. + DEFAULT => 256,
  100116. + ENUM => [],
  100117. + LOW => 4,
  100118. + HIGH => 768
  100119. + },
  100120. + {
  100121. + NAME => "dev_perio_tx_fifo_size_15",
  100122. + DEFAULT => 256,
  100123. + ENUM => [],
  100124. + LOW => 4,
  100125. + HIGH => 768
  100126. + },
  100127. + {
  100128. + NAME => "host_rx_fifo_size",
  100129. + DEFAULT => 1024,
  100130. + ENUM => [],
  100131. + LOW => 16,
  100132. + HIGH => 32768
  100133. + },
  100134. + {
  100135. + NAME => "host_nperio_tx_fifo_size",
  100136. + DEFAULT => 1024,
  100137. + ENUM => [],
  100138. + LOW => 16,
  100139. + HIGH => 32768
  100140. + },
  100141. + {
  100142. + NAME => "host_perio_tx_fifo_size",
  100143. + DEFAULT => 1024,
  100144. + ENUM => [],
  100145. + LOW => 16,
  100146. + HIGH => 32768
  100147. + },
  100148. + {
  100149. + NAME => "max_transfer_size",
  100150. + DEFAULT => 65535,
  100151. + ENUM => [],
  100152. + LOW => 2047,
  100153. + HIGH => 65535
  100154. + },
  100155. + {
  100156. + NAME => "max_packet_count",
  100157. + DEFAULT => 511,
  100158. + ENUM => [],
  100159. + LOW => 15,
  100160. + HIGH => 511
  100161. + },
  100162. + {
  100163. + NAME => "host_channels",
  100164. + DEFAULT => 12,
  100165. + ENUM => [],
  100166. + LOW => 1,
  100167. + HIGH => 16
  100168. + },
  100169. + {
  100170. + NAME => "dev_endpoints",
  100171. + DEFAULT => 6,
  100172. + ENUM => [],
  100173. + LOW => 1,
  100174. + HIGH => 15
  100175. + },
  100176. + {
  100177. + NAME => "phy_type",
  100178. + DEFAULT => 1,
  100179. + ENUM => [],
  100180. + LOW => 0,
  100181. + HIGH => 2
  100182. + },
  100183. + {
  100184. + NAME => "phy_utmi_width",
  100185. + DEFAULT => 16,
  100186. + ENUM => [8, 16],
  100187. + LOW => 8,
  100188. + HIGH => 16
  100189. + },
  100190. + {
  100191. + NAME => "phy_ulpi_ddr",
  100192. + DEFAULT => 0,
  100193. + ENUM => [],
  100194. + LOW => 0,
  100195. + HIGH => 1
  100196. + },
  100197. + ];
  100198. +
  100199. +
  100200. +#
  100201. +#
  100202. +sub check_arch {
  100203. + $_ = `uname -m`;
  100204. + chomp;
  100205. + unless (m/armv4tl/) {
  100206. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  100207. + return 0;
  100208. + }
  100209. + return 1;
  100210. +}
  100211. +
  100212. +#
  100213. +#
  100214. +sub load_module {
  100215. + my $params = shift;
  100216. + print "\nRemoving Module\n";
  100217. + system "rmmod dwc_otg";
  100218. + print "Loading Module\n";
  100219. + if ($params ne "") {
  100220. + print "Module Parameters: $params\n";
  100221. + }
  100222. + if (system("modprobe dwc_otg $params")) {
  100223. + warn "Unable to load module\n";
  100224. + return 0;
  100225. + }
  100226. + return 1;
  100227. +}
  100228. +
  100229. +#
  100230. +#
  100231. +sub test_status {
  100232. + my $arg = shift;
  100233. +
  100234. + print "\n";
  100235. +
  100236. + if (defined $arg) {
  100237. + warn "WARNING: $arg\n";
  100238. + }
  100239. +
  100240. + if ($errors > 0) {
  100241. + warn "TEST FAILED with $errors errors\n";
  100242. + return 0;
  100243. + } else {
  100244. + print "TEST PASSED\n";
  100245. + return 0 if (defined $arg);
  100246. + }
  100247. + return 1;
  100248. +}
  100249. +
  100250. +#
  100251. +#
  100252. +@EXPORT = qw(
  100253. +$sysfsdir
  100254. +$paramdir
  100255. +$params
  100256. +$errors
  100257. +check_arch
  100258. +load_module
  100259. +test_status
  100260. +);
  100261. +
  100262. +1;
  100263. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/test/Makefile linux-rpi/drivers/usb/host/dwc_otg/test/Makefile
  100264. --- linux-3.17.5/drivers/usb/host/dwc_otg/test/Makefile 1969-12-31 18:00:00.000000000 -0600
  100265. +++ linux-rpi/drivers/usb/host/dwc_otg/test/Makefile 2014-12-11 14:02:55.400418001 -0600
  100266. @@ -0,0 +1,16 @@
  100267. +
  100268. +PERL=/usr/bin/perl
  100269. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  100270. +
  100271. +.PHONY : test
  100272. +test : perl_tests
  100273. +
  100274. +perl_tests :
  100275. + @echo
  100276. + @echo Running perl tests
  100277. + @for test in $(PL_TESTS); do \
  100278. + if $(PERL) ./$$test ; then \
  100279. + echo "=======> $$test, PASSED" ; \
  100280. + else echo "=======> $$test, FAILED" ; \
  100281. + fi \
  100282. + done
  100283. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/test/test_mod_param.pl linux-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  100284. --- linux-3.17.5/drivers/usb/host/dwc_otg/test/test_mod_param.pl 1969-12-31 18:00:00.000000000 -0600
  100285. +++ linux-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl 2014-12-11 14:02:55.400418001 -0600
  100286. @@ -0,0 +1,133 @@
  100287. +#!/usr/bin/perl -w
  100288. +#
  100289. +# Run this program on the integrator.
  100290. +#
  100291. +# - Tests module parameter default values.
  100292. +# - Tests setting of valid module parameter values via modprobe.
  100293. +# - Tests invalid module parameter values.
  100294. +# -----------------------------------------------------------------------------
  100295. +use strict;
  100296. +use dwc_otg_test;
  100297. +
  100298. +check_arch() or die;
  100299. +
  100300. +#
  100301. +#
  100302. +sub test {
  100303. + my ($param,$expected) = @_;
  100304. + my $value = get($param);
  100305. +
  100306. + if ($value == $expected) {
  100307. + print "$param = $value, okay\n";
  100308. + }
  100309. +
  100310. + else {
  100311. + warn "ERROR: value of $param != $expected, $value\n";
  100312. + $errors ++;
  100313. + }
  100314. +}
  100315. +
  100316. +#
  100317. +#
  100318. +sub get {
  100319. + my $param = shift;
  100320. + my $tmp = `cat $paramdir/$param`;
  100321. + chomp $tmp;
  100322. + return $tmp;
  100323. +}
  100324. +
  100325. +#
  100326. +#
  100327. +sub test_main {
  100328. +
  100329. + print "\nTesting Module Parameters\n";
  100330. +
  100331. + load_module("") or die;
  100332. +
  100333. + # Test initial values
  100334. + print "\nTesting Default Values\n";
  100335. + foreach (@{$params}) {
  100336. + test ($_->{NAME}, $_->{DEFAULT});
  100337. + }
  100338. +
  100339. + # Test low value
  100340. + print "\nTesting Low Value\n";
  100341. + my $cmd_params = "";
  100342. + foreach (@{$params}) {
  100343. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  100344. + }
  100345. + load_module($cmd_params) or die;
  100346. +
  100347. + foreach (@{$params}) {
  100348. + test ($_->{NAME}, $_->{LOW});
  100349. + }
  100350. +
  100351. + # Test high value
  100352. + print "\nTesting High Value\n";
  100353. + $cmd_params = "";
  100354. + foreach (@{$params}) {
  100355. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  100356. + }
  100357. + load_module($cmd_params) or die;
  100358. +
  100359. + foreach (@{$params}) {
  100360. + test ($_->{NAME}, $_->{HIGH});
  100361. + }
  100362. +
  100363. + # Test Enum
  100364. + print "\nTesting Enumerated\n";
  100365. + foreach (@{$params}) {
  100366. + if (defined $_->{ENUM}) {
  100367. + my $value;
  100368. + foreach $value (@{$_->{ENUM}}) {
  100369. + $cmd_params = "$_->{NAME}=$value";
  100370. + load_module($cmd_params) or die;
  100371. + test ($_->{NAME}, $value);
  100372. + }
  100373. + }
  100374. + }
  100375. +
  100376. + # Test Invalid Values
  100377. + print "\nTesting Invalid Values\n";
  100378. + $cmd_params = "";
  100379. + foreach (@{$params}) {
  100380. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  100381. + }
  100382. + load_module($cmd_params) or die;
  100383. +
  100384. + foreach (@{$params}) {
  100385. + test ($_->{NAME}, $_->{DEFAULT});
  100386. + }
  100387. +
  100388. + $cmd_params = "";
  100389. + foreach (@{$params}) {
  100390. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  100391. + }
  100392. + load_module($cmd_params) or die;
  100393. +
  100394. + foreach (@{$params}) {
  100395. + test ($_->{NAME}, $_->{DEFAULT});
  100396. + }
  100397. +
  100398. + print "\nTesting Enumerated\n";
  100399. + foreach (@{$params}) {
  100400. + if (defined $_->{ENUM}) {
  100401. + my $value;
  100402. + foreach $value (@{$_->{ENUM}}) {
  100403. + $value = $value + 1;
  100404. + $cmd_params = "$_->{NAME}=$value";
  100405. + load_module($cmd_params) or die;
  100406. + test ($_->{NAME}, $_->{DEFAULT});
  100407. + $value = $value - 2;
  100408. + $cmd_params = "$_->{NAME}=$value";
  100409. + load_module($cmd_params) or die;
  100410. + test ($_->{NAME}, $_->{DEFAULT});
  100411. + }
  100412. + }
  100413. + }
  100414. +
  100415. + test_status() or die;
  100416. +}
  100417. +
  100418. +test_main();
  100419. +0;
  100420. diff -Nur linux-3.17.5/drivers/usb/host/dwc_otg/test/test_sysfs.pl linux-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  100421. --- linux-3.17.5/drivers/usb/host/dwc_otg/test/test_sysfs.pl 1969-12-31 18:00:00.000000000 -0600
  100422. +++ linux-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl 2014-12-11 14:02:55.400418001 -0600
  100423. @@ -0,0 +1,193 @@
  100424. +#!/usr/bin/perl -w
  100425. +#
  100426. +# Run this program on the integrator
  100427. +# - Tests select sysfs attributes.
  100428. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  100429. +# -----------------------------------------------------------------------------
  100430. +use strict;
  100431. +use dwc_otg_test;
  100432. +
  100433. +check_arch() or die;
  100434. +
  100435. +#
  100436. +#
  100437. +sub test {
  100438. + my ($attr,$expected) = @_;
  100439. + my $string = get($attr);
  100440. +
  100441. + if ($string eq $expected) {
  100442. + printf("$attr = $string, okay\n");
  100443. + }
  100444. + else {
  100445. + warn "ERROR: value of $attr != $expected, $string\n";
  100446. + $errors ++;
  100447. + }
  100448. +}
  100449. +
  100450. +#
  100451. +#
  100452. +sub set {
  100453. + my ($reg, $value) = @_;
  100454. + system "echo $value > $sysfsdir/$reg";
  100455. +}
  100456. +
  100457. +#
  100458. +#
  100459. +sub get {
  100460. + my $attr = shift;
  100461. + my $string = `cat $sysfsdir/$attr`;
  100462. + chomp $string;
  100463. + if ($string =~ m/\s\=\s/) {
  100464. + my $tmp;
  100465. + ($tmp, $string) = split /\s=\s/, $string;
  100466. + }
  100467. + return $string;
  100468. +}
  100469. +
  100470. +#
  100471. +#
  100472. +sub test_main {
  100473. + print("\nTesting Sysfs Attributes\n");
  100474. +
  100475. + load_module("") or die;
  100476. +
  100477. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  100478. + print("\nTesting Default Values\n");
  100479. +
  100480. + test("regoffset", "0xffffffff");
  100481. + test("regvalue", "invalid offset");
  100482. + test("guid", "0x12345678"); # this will fail if it has been changed
  100483. + test("gsnpsid", "0x4f54200a");
  100484. +
  100485. + # Test operation of regoffset/regvalue
  100486. + print("\nTesting regoffset\n");
  100487. + set('regoffset', '5a5a5a5a');
  100488. + test("regoffset", "0xffffffff");
  100489. +
  100490. + set('regoffset', '0');
  100491. + test("regoffset", "0x00000000");
  100492. +
  100493. + set('regoffset', '40000');
  100494. + test("regoffset", "0x00000000");
  100495. +
  100496. + set('regoffset', '3ffff');
  100497. + test("regoffset", "0x0003ffff");
  100498. +
  100499. + set('regoffset', '1');
  100500. + test("regoffset", "0x00000001");
  100501. +
  100502. + print("\nTesting regvalue\n");
  100503. + set('regoffset', '3c');
  100504. + test("regvalue", "0x12345678");
  100505. + set('regvalue', '5a5a5a5a');
  100506. + test("regvalue", "0x5a5a5a5a");
  100507. + set('regvalue','a5a5a5a5');
  100508. + test("regvalue", "0xa5a5a5a5");
  100509. + set('guid','12345678');
  100510. +
  100511. + # Test HNP Capable
  100512. + print("\nTesting HNP Capable bit\n");
  100513. + set('hnpcapable', '1');
  100514. + test("hnpcapable", "0x1");
  100515. + set('hnpcapable','0');
  100516. + test("hnpcapable", "0x0");
  100517. +
  100518. + set('regoffset','0c');
  100519. +
  100520. + my $old = get('gusbcfg');
  100521. + print("setting hnpcapable\n");
  100522. + set('hnpcapable', '1');
  100523. + test("hnpcapable", "0x1");
  100524. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  100525. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  100526. +
  100527. + $old = get('gusbcfg');
  100528. + print("clearing hnpcapable\n");
  100529. + set('hnpcapable', '0');
  100530. + test("hnpcapable", "0x0");
  100531. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  100532. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  100533. +
  100534. + # Test SRP Capable
  100535. + print("\nTesting SRP Capable bit\n");
  100536. + set('srpcapable', '1');
  100537. + test("srpcapable", "0x1");
  100538. + set('srpcapable','0');
  100539. + test("srpcapable", "0x0");
  100540. +
  100541. + set('regoffset','0c');
  100542. +
  100543. + $old = get('gusbcfg');
  100544. + print("setting srpcapable\n");
  100545. + set('srpcapable', '1');
  100546. + test("srpcapable", "0x1");
  100547. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  100548. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  100549. +
  100550. + $old = get('gusbcfg');
  100551. + print("clearing srpcapable\n");
  100552. + set('srpcapable', '0');
  100553. + test("srpcapable", "0x0");
  100554. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  100555. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  100556. +
  100557. + # Test GGPIO
  100558. + print("\nTesting GGPIO\n");
  100559. + set('ggpio','5a5a5a5a');
  100560. + test('ggpio','0x5a5a0000');
  100561. + set('ggpio','a5a5a5a5');
  100562. + test('ggpio','0xa5a50000');
  100563. + set('ggpio','11110000');
  100564. + test('ggpio','0x11110000');
  100565. + set('ggpio','00001111');
  100566. + test('ggpio','0x00000000');
  100567. +
  100568. + # Test DEVSPEED
  100569. + print("\nTesting DEVSPEED\n");
  100570. + set('regoffset','800');
  100571. + $old = get('regvalue');
  100572. + set('devspeed','0');
  100573. + test('devspeed','0x0');
  100574. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  100575. + set('devspeed','1');
  100576. + test('devspeed','0x1');
  100577. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  100578. + set('devspeed','2');
  100579. + test('devspeed','0x2');
  100580. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  100581. + set('devspeed','3');
  100582. + test('devspeed','0x3');
  100583. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  100584. + set('devspeed','4');
  100585. + test('devspeed','0x0');
  100586. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  100587. + set('devspeed','5');
  100588. + test('devspeed','0x1');
  100589. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  100590. +
  100591. +
  100592. + # mode Returns the current mode:0 for device mode1 for host mode Read
  100593. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  100594. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  100595. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  100596. + # bussuspend Suspend the USB bus. Read/Write
  100597. + # busconnected Get the connection status of the bus Read
  100598. +
  100599. + # gotgctl Get or set the Core Control Status Register. Read/Write
  100600. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  100601. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  100602. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  100603. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  100604. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  100605. + ## guid Get or set the value of the User ID Register Read/Write
  100606. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  100607. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  100608. + # enumspeed Gets the device enumeration Speed. Read
  100609. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  100610. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  100611. +
  100612. + test_status("TEST NYI") or die;
  100613. +}
  100614. +
  100615. +test_main();
  100616. +0;
  100617. diff -Nur linux-3.17.5/drivers/usb/host/Kconfig linux-rpi/drivers/usb/host/Kconfig
  100618. --- linux-3.17.5/drivers/usb/host/Kconfig 2014-12-06 17:57:59.000000000 -0600
  100619. +++ linux-rpi/drivers/usb/host/Kconfig 2014-12-11 14:05:39.520418001 -0600
  100620. @@ -721,6 +721,19 @@
  100621. To compile this driver a module, choose M here: the module
  100622. will be called "hwa-hc".
  100623. +config USB_DWCOTG
  100624. + tristate "Synopsis DWC host support"
  100625. + depends on USB
  100626. + help
  100627. + The Synopsis DWC controller is a dual-role
  100628. + host/peripheral/OTG ("On The Go") USB controllers.
  100629. +
  100630. + Enable this option to support this IP in host controller mode.
  100631. + If unsure, say N.
  100632. +
  100633. + To compile this driver as a module, choose M here: the
  100634. + modules built will be called dwc_otg and dwc_common_port.
  100635. +
  100636. config USB_IMX21_HCD
  100637. tristate "i.MX21 HCD support"
  100638. depends on ARM && ARCH_MXC
  100639. diff -Nur linux-3.17.5/drivers/usb/host/Makefile linux-rpi/drivers/usb/host/Makefile
  100640. --- linux-3.17.5/drivers/usb/host/Makefile 2014-12-06 17:57:59.000000000 -0600
  100641. +++ linux-rpi/drivers/usb/host/Makefile 2014-12-11 14:05:39.524418001 -0600
  100642. @@ -69,6 +69,8 @@
  100643. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  100644. obj-$(CONFIG_USB_ISP1760_HCD) += isp1760.o
  100645. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  100646. +
  100647. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  100648. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  100649. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  100650. obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
  100651. diff -Nur linux-3.17.5/drivers/usb/Makefile linux-rpi/drivers/usb/Makefile
  100652. --- linux-3.17.5/drivers/usb/Makefile 2014-12-06 17:57:59.000000000 -0600
  100653. +++ linux-rpi/drivers/usb/Makefile 2014-12-11 14:05:39.484418001 -0600
  100654. @@ -24,6 +24,7 @@
  100655. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  100656. obj-$(CONFIG_USB_HWA_HCD) += host/
  100657. obj-$(CONFIG_USB_ISP1760_HCD) += host/
  100658. +obj-$(CONFIG_USB_DWCOTG) += host/
  100659. obj-$(CONFIG_USB_IMX21_HCD) += host/
  100660. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += host/
  100661. obj-$(CONFIG_USB_FUSBH200_HCD) += host/
  100662. diff -Nur linux-3.17.5/drivers/video/fbdev/bcm2708_fb.c linux-rpi/drivers/video/fbdev/bcm2708_fb.c
  100663. --- linux-3.17.5/drivers/video/fbdev/bcm2708_fb.c 1969-12-31 18:00:00.000000000 -0600
  100664. +++ linux-rpi/drivers/video/fbdev/bcm2708_fb.c 2014-12-11 14:05:39.640418001 -0600
  100665. @@ -0,0 +1,823 @@
  100666. +/*
  100667. + * linux/drivers/video/bcm2708_fb.c
  100668. + *
  100669. + * Copyright (C) 2010 Broadcom
  100670. + *
  100671. + * This file is subject to the terms and conditions of the GNU General Public
  100672. + * License. See the file COPYING in the main directory of this archive
  100673. + * for more details.
  100674. + *
  100675. + * Broadcom simple framebuffer driver
  100676. + *
  100677. + * This file is derived from cirrusfb.c
  100678. + * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  100679. + *
  100680. + */
  100681. +#include <linux/module.h>
  100682. +#include <linux/kernel.h>
  100683. +#include <linux/errno.h>
  100684. +#include <linux/string.h>
  100685. +#include <linux/slab.h>
  100686. +#include <linux/mm.h>
  100687. +#include <linux/fb.h>
  100688. +#include <linux/init.h>
  100689. +#include <linux/interrupt.h>
  100690. +#include <linux/ioport.h>
  100691. +#include <linux/list.h>
  100692. +#include <linux/platform_device.h>
  100693. +#include <linux/clk.h>
  100694. +#include <linux/printk.h>
  100695. +#include <linux/console.h>
  100696. +#include <linux/debugfs.h>
  100697. +
  100698. +#include <mach/dma.h>
  100699. +#include <mach/platform.h>
  100700. +#include <mach/vcio.h>
  100701. +
  100702. +#include <asm/sizes.h>
  100703. +#include <linux/io.h>
  100704. +#include <linux/dma-mapping.h>
  100705. +
  100706. +//#define BCM2708_FB_DEBUG
  100707. +#define MODULE_NAME "bcm2708_fb"
  100708. +
  100709. +#ifdef BCM2708_FB_DEBUG
  100710. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  100711. +#else
  100712. +#define print_debug(fmt,...)
  100713. +#endif
  100714. +
  100715. +/* This is limited to 16 characters when displayed by X startup */
  100716. +static const char *bcm2708_name = "BCM2708 FB";
  100717. +
  100718. +#define DRIVER_NAME "bcm2708_fb"
  100719. +
  100720. +static int fbwidth = 800; /* module parameter */
  100721. +static int fbheight = 480; /* module parameter */
  100722. +static int fbdepth = 16; /* module parameter */
  100723. +static int fbswap = 0; /* module parameter */
  100724. +
  100725. +static u32 dma_busy_wait_threshold = 1<<15;
  100726. +module_param(dma_busy_wait_threshold, int, 0644);
  100727. +MODULE_PARM_DESC(dma_busy_wait_threshold, "Busy-wait for DMA completion below this area");
  100728. +
  100729. +/* this data structure describes each frame buffer device we find */
  100730. +
  100731. +struct fbinfo_s {
  100732. + u32 xres, yres, xres_virtual, yres_virtual;
  100733. + u32 pitch, bpp;
  100734. + u32 xoffset, yoffset;
  100735. + u32 base;
  100736. + u32 screen_size;
  100737. + u16 cmap[256];
  100738. +};
  100739. +
  100740. +struct bcm2708_fb_stats {
  100741. + struct debugfs_regset32 regset;
  100742. + u32 dma_copies;
  100743. + u32 dma_irqs;
  100744. +};
  100745. +
  100746. +struct bcm2708_fb {
  100747. + struct fb_info fb;
  100748. + struct platform_device *dev;
  100749. + struct fbinfo_s *info;
  100750. + dma_addr_t dma;
  100751. + u32 cmap[16];
  100752. + int dma_chan;
  100753. + int dma_irq;
  100754. + void __iomem *dma_chan_base;
  100755. + void *cb_base; /* DMA control blocks */
  100756. + dma_addr_t cb_handle;
  100757. + struct dentry *debugfs_dir;
  100758. + wait_queue_head_t dma_waitq;
  100759. + struct bcm2708_fb_stats stats;
  100760. + unsigned long fb_bus_address;
  100761. +};
  100762. +
  100763. +#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  100764. +
  100765. +static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
  100766. +{
  100767. + debugfs_remove_recursive(fb->debugfs_dir);
  100768. + fb->debugfs_dir = NULL;
  100769. +}
  100770. +
  100771. +static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
  100772. +{
  100773. + static struct debugfs_reg32 stats_registers[] = {
  100774. + {
  100775. + "dma_copies",
  100776. + offsetof(struct bcm2708_fb_stats, dma_copies)
  100777. + },
  100778. + {
  100779. + "dma_irqs",
  100780. + offsetof(struct bcm2708_fb_stats, dma_irqs)
  100781. + },
  100782. + };
  100783. +
  100784. + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
  100785. + if (!fb->debugfs_dir) {
  100786. + pr_warn("%s: could not create debugfs entry\n",
  100787. + __func__);
  100788. + return -EFAULT;
  100789. + }
  100790. +
  100791. + fb->stats.regset.regs = stats_registers;
  100792. + fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
  100793. + fb->stats.regset.base = &fb->stats;
  100794. +
  100795. + if (!debugfs_create_regset32(
  100796. + "stats", 0444, fb->debugfs_dir, &fb->stats.regset)) {
  100797. + pr_warn("%s: could not create statistics registers\n",
  100798. + __func__);
  100799. + goto fail;
  100800. + }
  100801. + return 0;
  100802. +
  100803. +fail:
  100804. + bcm2708_fb_debugfs_deinit(fb);
  100805. + return -EFAULT;
  100806. +}
  100807. +
  100808. +static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
  100809. +{
  100810. + int ret = 0;
  100811. +
  100812. + memset(&var->transp, 0, sizeof(var->transp));
  100813. +
  100814. + var->red.msb_right = 0;
  100815. + var->green.msb_right = 0;
  100816. + var->blue.msb_right = 0;
  100817. +
  100818. + switch (var->bits_per_pixel) {
  100819. + case 1:
  100820. + case 2:
  100821. + case 4:
  100822. + case 8:
  100823. + var->red.length = var->bits_per_pixel;
  100824. + var->red.offset = 0;
  100825. + var->green.length = var->bits_per_pixel;
  100826. + var->green.offset = 0;
  100827. + var->blue.length = var->bits_per_pixel;
  100828. + var->blue.offset = 0;
  100829. + break;
  100830. + case 16:
  100831. + var->red.length = 5;
  100832. + var->blue.length = 5;
  100833. + /*
  100834. + * Green length can be 5 or 6 depending whether
  100835. + * we're operating in RGB555 or RGB565 mode.
  100836. + */
  100837. + if (var->green.length != 5 && var->green.length != 6)
  100838. + var->green.length = 6;
  100839. + break;
  100840. + case 24:
  100841. + var->red.length = 8;
  100842. + var->blue.length = 8;
  100843. + var->green.length = 8;
  100844. + break;
  100845. + case 32:
  100846. + var->red.length = 8;
  100847. + var->green.length = 8;
  100848. + var->blue.length = 8;
  100849. + var->transp.length = 8;
  100850. + break;
  100851. + default:
  100852. + ret = -EINVAL;
  100853. + break;
  100854. + }
  100855. +
  100856. + /*
  100857. + * >= 16bpp displays have separate colour component bitfields
  100858. + * encoded in the pixel data. Calculate their position from
  100859. + * the bitfield length defined above.
  100860. + */
  100861. + if (ret == 0 && var->bits_per_pixel >= 24 && fbswap) {
  100862. + var->blue.offset = 0;
  100863. + var->green.offset = var->blue.offset + var->blue.length;
  100864. + var->red.offset = var->green.offset + var->green.length;
  100865. + var->transp.offset = var->red.offset + var->red.length;
  100866. + } else if (ret == 0 && var->bits_per_pixel >= 24) {
  100867. + var->red.offset = 0;
  100868. + var->green.offset = var->red.offset + var->red.length;
  100869. + var->blue.offset = var->green.offset + var->green.length;
  100870. + var->transp.offset = var->blue.offset + var->blue.length;
  100871. + } else if (ret == 0 && var->bits_per_pixel >= 16) {
  100872. + var->blue.offset = 0;
  100873. + var->green.offset = var->blue.offset + var->blue.length;
  100874. + var->red.offset = var->green.offset + var->green.length;
  100875. + var->transp.offset = var->red.offset + var->red.length;
  100876. + }
  100877. +
  100878. + return ret;
  100879. +}
  100880. +
  100881. +static int bcm2708_fb_check_var(struct fb_var_screeninfo *var,
  100882. + struct fb_info *info)
  100883. +{
  100884. + /* info input, var output */
  100885. + int yres;
  100886. +
  100887. + /* info input, var output */
  100888. + print_debug("bcm2708_fb_check_var info(%p) %dx%d (%dx%d), %d, %d\n", info,
  100889. + info->var.xres, info->var.yres, info->var.xres_virtual,
  100890. + info->var.yres_virtual, (int)info->screen_size,
  100891. + info->var.bits_per_pixel);
  100892. + print_debug("bcm2708_fb_check_var var(%p) %dx%d (%dx%d), %d\n", var,
  100893. + var->xres, var->yres, var->xres_virtual, var->yres_virtual,
  100894. + var->bits_per_pixel);
  100895. +
  100896. + if (!var->bits_per_pixel)
  100897. + var->bits_per_pixel = 16;
  100898. +
  100899. + if (bcm2708_fb_set_bitfields(var) != 0) {
  100900. + pr_err("bcm2708_fb_check_var: invalid bits_per_pixel %d\n",
  100901. + var->bits_per_pixel);
  100902. + return -EINVAL;
  100903. + }
  100904. +
  100905. +
  100906. + if (var->xres_virtual < var->xres)
  100907. + var->xres_virtual = var->xres;
  100908. + /* use highest possible virtual resolution */
  100909. + if (var->yres_virtual == -1) {
  100910. + var->yres_virtual = 480;
  100911. +
  100912. + pr_err
  100913. + ("bcm2708_fb_check_var: virtual resolution set to maximum of %dx%d\n",
  100914. + var->xres_virtual, var->yres_virtual);
  100915. + }
  100916. + if (var->yres_virtual < var->yres)
  100917. + var->yres_virtual = var->yres;
  100918. +
  100919. + if (var->xoffset < 0)
  100920. + var->xoffset = 0;
  100921. + if (var->yoffset < 0)
  100922. + var->yoffset = 0;
  100923. +
  100924. + /* truncate xoffset and yoffset to maximum if too high */
  100925. + if (var->xoffset > var->xres_virtual - var->xres)
  100926. + var->xoffset = var->xres_virtual - var->xres - 1;
  100927. + if (var->yoffset > var->yres_virtual - var->yres)
  100928. + var->yoffset = var->yres_virtual - var->yres - 1;
  100929. +
  100930. + yres = var->yres;
  100931. + if (var->vmode & FB_VMODE_DOUBLE)
  100932. + yres *= 2;
  100933. + else if (var->vmode & FB_VMODE_INTERLACED)
  100934. + yres = (yres + 1) / 2;
  100935. +
  100936. + if (var->xres * yres > 1920 * 1200) {
  100937. + pr_err("bcm2708_fb_check_var: ERROR: Pixel size >= 1920x1200; "
  100938. + "special treatment required! (TODO)\n");
  100939. + return -EINVAL;
  100940. + }
  100941. +
  100942. + return 0;
  100943. +}
  100944. +
  100945. +static int bcm2708_fb_set_par(struct fb_info *info)
  100946. +{
  100947. + uint32_t val = 0;
  100948. + struct bcm2708_fb *fb = to_bcm2708(info);
  100949. + volatile struct fbinfo_s *fbinfo = fb->info;
  100950. + fbinfo->xres = info->var.xres;
  100951. + fbinfo->yres = info->var.yres;
  100952. + fbinfo->xres_virtual = info->var.xres_virtual;
  100953. + fbinfo->yres_virtual = info->var.yres_virtual;
  100954. + fbinfo->bpp = info->var.bits_per_pixel;
  100955. + fbinfo->xoffset = info->var.xoffset;
  100956. + fbinfo->yoffset = info->var.yoffset;
  100957. + fbinfo->base = 0; /* filled in by VC */
  100958. + fbinfo->pitch = 0; /* filled in by VC */
  100959. +
  100960. + print_debug("bcm2708_fb_set_par info(%p) %dx%d (%dx%d), %d, %d\n", info,
  100961. + info->var.xres, info->var.yres, info->var.xres_virtual,
  100962. + info->var.yres_virtual, (int)info->screen_size,
  100963. + info->var.bits_per_pixel);
  100964. +
  100965. + /* ensure last write to fbinfo is visible to GPU */
  100966. + wmb();
  100967. +
  100968. + /* inform vc about new framebuffer */
  100969. + bcm_mailbox_write(MBOX_CHAN_FB, fb->dma);
  100970. +
  100971. + /* TODO: replace fb driver with vchiq version */
  100972. + /* wait for response */
  100973. + bcm_mailbox_read(MBOX_CHAN_FB, &val);
  100974. +
  100975. + /* ensure GPU writes are visible to us */
  100976. + rmb();
  100977. +
  100978. + if (val == 0) {
  100979. + fb->fb.fix.line_length = fbinfo->pitch;
  100980. +
  100981. + if (info->var.bits_per_pixel <= 8)
  100982. + fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  100983. + else
  100984. + fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  100985. +
  100986. + fb->fb_bus_address = fbinfo->base;
  100987. + fbinfo->base &= ~0xc0000000;
  100988. + fb->fb.fix.smem_start = fbinfo->base;
  100989. + fb->fb.fix.smem_len = fbinfo->pitch * fbinfo->yres_virtual;
  100990. + fb->fb.screen_size = fbinfo->screen_size;
  100991. + if (fb->fb.screen_base)
  100992. + iounmap(fb->fb.screen_base);
  100993. + fb->fb.screen_base =
  100994. + (void *)ioremap_wc(fbinfo->base, fb->fb.screen_size);
  100995. + if (!fb->fb.screen_base) {
  100996. + /* the console may currently be locked */
  100997. + console_trylock();
  100998. + console_unlock();
  100999. +
  101000. + BUG(); /* what can we do here */
  101001. + }
  101002. + }
  101003. + print_debug
  101004. + ("BCM2708FB: start = %p,%p width=%d, height=%d, bpp=%d, pitch=%d size=%d success=%d\n",
  101005. + (void *)fb->fb.screen_base, (void *)fb->fb_bus_address,
  101006. + fbinfo->xres, fbinfo->yres, fbinfo->bpp,
  101007. + fbinfo->pitch, (int)fb->fb.screen_size, val);
  101008. +
  101009. + return val;
  101010. +}
  101011. +
  101012. +static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  101013. +{
  101014. + unsigned int mask = (1 << bf->length) - 1;
  101015. +
  101016. + return (val >> (16 - bf->length) & mask) << bf->offset;
  101017. +}
  101018. +
  101019. +
  101020. +static int bcm2708_fb_setcolreg(unsigned int regno, unsigned int red,
  101021. + unsigned int green, unsigned int blue,
  101022. + unsigned int transp, struct fb_info *info)
  101023. +{
  101024. + struct bcm2708_fb *fb = to_bcm2708(info);
  101025. +
  101026. + /*print_debug("BCM2708FB: setcolreg %d:(%02x,%02x,%02x,%02x) %x\n", regno, red, green, blue, transp, fb->fb.fix.visual);*/
  101027. + if (fb->fb.var.bits_per_pixel <= 8) {
  101028. + if (regno < 256) {
  101029. + /* blue [0:4], green [5:10], red [11:15] */
  101030. + fb->info->cmap[regno] = ((red >> (16-5)) & 0x1f) << 11 |
  101031. + ((green >> (16-6)) & 0x3f) << 5 |
  101032. + ((blue >> (16-5)) & 0x1f) << 0;
  101033. + }
  101034. + /* 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 */
  101035. + /* So just call it for what looks like the last colour in a list for now. */
  101036. + if (regno == 15 || regno == 255)
  101037. + bcm2708_fb_set_par(info);
  101038. + } else if (regno < 16) {
  101039. + fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  101040. + convert_bitfield(blue, &fb->fb.var.blue) |
  101041. + convert_bitfield(green, &fb->fb.var.green) |
  101042. + convert_bitfield(red, &fb->fb.var.red);
  101043. + }
  101044. + return regno > 255;
  101045. +}
  101046. +
  101047. +static int bcm2708_fb_blank(int blank_mode, struct fb_info *info)
  101048. +{
  101049. + s32 result = -1;
  101050. + u32 p[7];
  101051. + if ( (blank_mode == FB_BLANK_NORMAL) ||
  101052. + (blank_mode == FB_BLANK_UNBLANK)) {
  101053. +
  101054. + p[0] = 28; // size = sizeof u32 * length of p
  101055. + p[1] = VCMSG_PROCESS_REQUEST; // process request
  101056. + p[2] = VCMSG_SET_BLANK_SCREEN; // (the tag id)
  101057. + p[3] = 4; // (size of the response buffer)
  101058. + p[4] = 4; // (size of the request data)
  101059. + p[5] = blank_mode;
  101060. + p[6] = VCMSG_PROPERTY_END; // end tag
  101061. +
  101062. + bcm_mailbox_property(&p, p[0]);
  101063. +
  101064. + if ( p[1] == VCMSG_REQUEST_SUCCESSFUL )
  101065. + result = 0;
  101066. + else
  101067. + pr_err("bcm2708_fb_blank(%d) returns=%d p[1]=0x%x\n", blank_mode, p[5], p[1]);
  101068. + }
  101069. + return result;
  101070. +}
  101071. +
  101072. +static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  101073. +{
  101074. + s32 result = -1;
  101075. + info->var.xoffset = var->xoffset;
  101076. + info->var.yoffset = var->yoffset;
  101077. + result = bcm2708_fb_set_par(info);
  101078. + if (result != 0)
  101079. + pr_err("bcm2708_fb_pan_display(%d,%d) returns=%d\n", var->xoffset, var->yoffset, result);
  101080. + return result;
  101081. +}
  101082. +
  101083. +static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  101084. +{
  101085. + s32 result = -1;
  101086. + u32 p[7];
  101087. + if (cmd == FBIO_WAITFORVSYNC) {
  101088. + p[0] = 28; // size = sizeof u32 * length of p
  101089. + p[1] = VCMSG_PROCESS_REQUEST; // process request
  101090. + p[2] = VCMSG_SET_VSYNC; // (the tag id)
  101091. + p[3] = 4; // (size of the response buffer)
  101092. + p[4] = 4; // (size of the request data)
  101093. + p[5] = 0; // dummy
  101094. + p[6] = VCMSG_PROPERTY_END; // end tag
  101095. +
  101096. + bcm_mailbox_property(&p, p[0]);
  101097. +
  101098. + pr_info("bcm2708_fb_ioctl %x,%lx returns=%d p[1]=0x%x\n", cmd, arg, p[5], p[1]);
  101099. +
  101100. + if ( p[1] == VCMSG_REQUEST_SUCCESSFUL )
  101101. + result = 0;
  101102. + }
  101103. + return result;
  101104. +}
  101105. +static void bcm2708_fb_fillrect(struct fb_info *info,
  101106. + const struct fb_fillrect *rect)
  101107. +{
  101108. + /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
  101109. + cfb_fillrect(info, rect);
  101110. +}
  101111. +
  101112. +/* A helper function for configuring dma control block */
  101113. +static void set_dma_cb(struct bcm2708_dma_cb *cb,
  101114. + int burst_size,
  101115. + dma_addr_t dst,
  101116. + int dst_stride,
  101117. + dma_addr_t src,
  101118. + int src_stride,
  101119. + int w,
  101120. + int h)
  101121. +{
  101122. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  101123. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  101124. + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
  101125. + cb->dst = dst;
  101126. + cb->src = src;
  101127. + /*
  101128. + * This is not really obvious from the DMA documentation,
  101129. + * but the top 16 bits must be programmmed to "height -1"
  101130. + * and not "height" in 2D mode.
  101131. + */
  101132. + cb->length = ((h - 1) << 16) | w;
  101133. + cb->stride = ((dst_stride - w) << 16) | (u16)(src_stride - w);
  101134. + cb->pad[0] = 0;
  101135. + cb->pad[1] = 0;
  101136. +}
  101137. +
  101138. +static void bcm2708_fb_copyarea(struct fb_info *info,
  101139. + const struct fb_copyarea *region)
  101140. +{
  101141. + struct bcm2708_fb *fb = to_bcm2708(info);
  101142. + struct bcm2708_dma_cb *cb = fb->cb_base;
  101143. + int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
  101144. + /* Channel 0 supports larger bursts and is a bit faster */
  101145. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  101146. + int pixels = region->width * region->height;
  101147. +
  101148. + /* Fallback to cfb_copyarea() if we don't like something */
  101149. + if (bytes_per_pixel > 4 ||
  101150. + info->var.xres * info->var.yres > 1920 * 1200 ||
  101151. + region->width <= 0 || region->width > info->var.xres ||
  101152. + region->height <= 0 || region->height > info->var.yres ||
  101153. + region->sx < 0 || region->sx >= info->var.xres ||
  101154. + region->sy < 0 || region->sy >= info->var.yres ||
  101155. + region->dx < 0 || region->dx >= info->var.xres ||
  101156. + region->dy < 0 || region->dy >= info->var.yres ||
  101157. + region->sx + region->width > info->var.xres ||
  101158. + region->dx + region->width > info->var.xres ||
  101159. + region->sy + region->height > info->var.yres ||
  101160. + region->dy + region->height > info->var.yres) {
  101161. + cfb_copyarea(info, region);
  101162. + return;
  101163. + }
  101164. +
  101165. + if (region->dy == region->sy && region->dx > region->sx) {
  101166. + /*
  101167. + * A difficult case of overlapped copy. Because DMA can't
  101168. + * copy individual scanlines in backwards direction, we need
  101169. + * two-pass processing. We do it by programming a chain of dma
  101170. + * control blocks in the first 16K part of the buffer and use
  101171. + * the remaining 48K as the intermediate temporary scratch
  101172. + * buffer. The buffer size is sufficient to handle up to
  101173. + * 1920x1200 resolution at 32bpp pixel depth.
  101174. + */
  101175. + int y;
  101176. + dma_addr_t control_block_pa = fb->cb_handle;
  101177. + dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
  101178. + int scanline_size = bytes_per_pixel * region->width;
  101179. + int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
  101180. +
  101181. + for (y = 0; y < region->height; y += scanlines_per_cb) {
  101182. + dma_addr_t src =
  101183. + fb->fb_bus_address +
  101184. + bytes_per_pixel * region->sx +
  101185. + (region->sy + y) * fb->fb.fix.line_length;
  101186. + dma_addr_t dst =
  101187. + fb->fb_bus_address +
  101188. + bytes_per_pixel * region->dx +
  101189. + (region->dy + y) * fb->fb.fix.line_length;
  101190. +
  101191. + if (region->height - y < scanlines_per_cb)
  101192. + scanlines_per_cb = region->height - y;
  101193. +
  101194. + set_dma_cb(cb, burst_size, scratchbuf, scanline_size,
  101195. + src, fb->fb.fix.line_length,
  101196. + scanline_size, scanlines_per_cb);
  101197. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  101198. + cb->next = control_block_pa;
  101199. + cb++;
  101200. +
  101201. + set_dma_cb(cb, burst_size, dst, fb->fb.fix.line_length,
  101202. + scratchbuf, scanline_size,
  101203. + scanline_size, scanlines_per_cb);
  101204. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  101205. + cb->next = control_block_pa;
  101206. + cb++;
  101207. + }
  101208. + /* move the pointer back to the last dma control block */
  101209. + cb--;
  101210. + } else {
  101211. + /* A single dma control block is enough. */
  101212. + int sy, dy, stride;
  101213. + if (region->dy <= region->sy) {
  101214. + /* processing from top to bottom */
  101215. + dy = region->dy;
  101216. + sy = region->sy;
  101217. + stride = fb->fb.fix.line_length;
  101218. + } else {
  101219. + /* processing from bottom to top */
  101220. + dy = region->dy + region->height - 1;
  101221. + sy = region->sy + region->height - 1;
  101222. + stride = -fb->fb.fix.line_length;
  101223. + }
  101224. + set_dma_cb(cb, burst_size,
  101225. + fb->fb_bus_address + dy * fb->fb.fix.line_length +
  101226. + bytes_per_pixel * region->dx,
  101227. + stride,
  101228. + fb->fb_bus_address + sy * fb->fb.fix.line_length +
  101229. + bytes_per_pixel * region->sx,
  101230. + stride,
  101231. + region->width * bytes_per_pixel,
  101232. + region->height);
  101233. + }
  101234. +
  101235. + /* end of dma control blocks chain */
  101236. + cb->next = 0;
  101237. +
  101238. +
  101239. + if (pixels < dma_busy_wait_threshold) {
  101240. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  101241. + bcm_dma_wait_idle(fb->dma_chan_base);
  101242. + } else {
  101243. + void __iomem *dma_chan = fb->dma_chan_base;
  101244. + cb->info |= BCM2708_DMA_INT_EN;
  101245. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  101246. + while (bcm_dma_is_busy(dma_chan)) {
  101247. + wait_event_interruptible(
  101248. + fb->dma_waitq,
  101249. + !bcm_dma_is_busy(dma_chan));
  101250. + }
  101251. + fb->stats.dma_irqs++;
  101252. + }
  101253. + fb->stats.dma_copies++;
  101254. +}
  101255. +
  101256. +static void bcm2708_fb_imageblit(struct fb_info *info,
  101257. + const struct fb_image *image)
  101258. +{
  101259. + /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
  101260. + cfb_imageblit(info, image);
  101261. +}
  101262. +
  101263. +static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
  101264. +{
  101265. + struct bcm2708_fb *fb = cxt;
  101266. +
  101267. + /* FIXME: should read status register to check if this is
  101268. + * actually interrupting us or not, in case this interrupt
  101269. + * ever becomes shared amongst several DMA channels
  101270. + *
  101271. + * readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_IRQ;
  101272. + */
  101273. +
  101274. + /* acknowledge the interrupt */
  101275. + writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
  101276. +
  101277. + wake_up(&fb->dma_waitq);
  101278. + return IRQ_HANDLED;
  101279. +}
  101280. +
  101281. +static struct fb_ops bcm2708_fb_ops = {
  101282. + .owner = THIS_MODULE,
  101283. + .fb_check_var = bcm2708_fb_check_var,
  101284. + .fb_set_par = bcm2708_fb_set_par,
  101285. + .fb_setcolreg = bcm2708_fb_setcolreg,
  101286. + .fb_blank = bcm2708_fb_blank,
  101287. + .fb_fillrect = bcm2708_fb_fillrect,
  101288. + .fb_copyarea = bcm2708_fb_copyarea,
  101289. + .fb_imageblit = bcm2708_fb_imageblit,
  101290. + .fb_pan_display = bcm2708_fb_pan_display,
  101291. + .fb_ioctl = bcm2708_ioctl,
  101292. +};
  101293. +
  101294. +static int bcm2708_fb_register(struct bcm2708_fb *fb)
  101295. +{
  101296. + int ret;
  101297. + dma_addr_t dma;
  101298. + void *mem;
  101299. +
  101300. + mem =
  101301. + dma_alloc_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), &dma,
  101302. + GFP_KERNEL);
  101303. +
  101304. + if (NULL == mem) {
  101305. + pr_err(": unable to allocate fbinfo buffer\n");
  101306. + ret = -ENOMEM;
  101307. + } else {
  101308. + fb->info = (struct fbinfo_s *)mem;
  101309. + fb->dma = dma;
  101310. + }
  101311. + fb->fb.fbops = &bcm2708_fb_ops;
  101312. + fb->fb.flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_COPYAREA;
  101313. + fb->fb.pseudo_palette = fb->cmap;
  101314. +
  101315. + strncpy(fb->fb.fix.id, bcm2708_name, sizeof(fb->fb.fix.id));
  101316. + fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  101317. + fb->fb.fix.type_aux = 0;
  101318. + fb->fb.fix.xpanstep = 1;
  101319. + fb->fb.fix.ypanstep = 1;
  101320. + fb->fb.fix.ywrapstep = 0;
  101321. + fb->fb.fix.accel = FB_ACCEL_NONE;
  101322. +
  101323. + fb->fb.var.xres = fbwidth;
  101324. + fb->fb.var.yres = fbheight;
  101325. + fb->fb.var.xres_virtual = fbwidth;
  101326. + fb->fb.var.yres_virtual = fbheight;
  101327. + fb->fb.var.bits_per_pixel = fbdepth;
  101328. + fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  101329. + fb->fb.var.activate = FB_ACTIVATE_NOW;
  101330. + fb->fb.var.nonstd = 0;
  101331. + fb->fb.var.height = -1; /* height of picture in mm */
  101332. + fb->fb.var.width = -1; /* width of picture in mm */
  101333. + fb->fb.var.accel_flags = 0;
  101334. +
  101335. + fb->fb.monspecs.hfmin = 0;
  101336. + fb->fb.monspecs.hfmax = 100000;
  101337. + fb->fb.monspecs.vfmin = 0;
  101338. + fb->fb.monspecs.vfmax = 400;
  101339. + fb->fb.monspecs.dclkmin = 1000000;
  101340. + fb->fb.monspecs.dclkmax = 100000000;
  101341. +
  101342. + bcm2708_fb_set_bitfields(&fb->fb.var);
  101343. + init_waitqueue_head(&fb->dma_waitq);
  101344. +
  101345. + /*
  101346. + * Allocate colourmap.
  101347. + */
  101348. +
  101349. + fb_set_var(&fb->fb, &fb->fb.var);
  101350. + bcm2708_fb_set_par(&fb->fb);
  101351. +
  101352. + print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n", fbwidth
  101353. + fbheight, fbdepth, fbswap);
  101354. +
  101355. + ret = register_framebuffer(&fb->fb);
  101356. + print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
  101357. + if (ret == 0)
  101358. + goto out;
  101359. +
  101360. + print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
  101361. +out:
  101362. + return ret;
  101363. +}
  101364. +
  101365. +static int bcm2708_fb_probe(struct platform_device *dev)
  101366. +{
  101367. + struct bcm2708_fb *fb;
  101368. + int ret;
  101369. +
  101370. + fb = kzalloc(sizeof(struct bcm2708_fb), GFP_KERNEL);
  101371. + if (!fb) {
  101372. + dev_err(&dev->dev,
  101373. + "could not allocate new bcm2708_fb struct\n");
  101374. + ret = -ENOMEM;
  101375. + goto free_region;
  101376. + }
  101377. +
  101378. + bcm2708_fb_debugfs_init(fb);
  101379. +
  101380. + fb->cb_base = dma_alloc_writecombine(&dev->dev, SZ_64K,
  101381. + &fb->cb_handle, GFP_KERNEL);
  101382. + if (!fb->cb_base) {
  101383. + dev_err(&dev->dev, "cannot allocate DMA CBs\n");
  101384. + ret = -ENOMEM;
  101385. + goto free_fb;
  101386. + }
  101387. +
  101388. + pr_info("BCM2708FB: allocated DMA memory %08x\n",
  101389. + fb->cb_handle);
  101390. +
  101391. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
  101392. + &fb->dma_chan_base, &fb->dma_irq);
  101393. + if (ret < 0) {
  101394. + dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
  101395. + goto free_cb;
  101396. + }
  101397. + fb->dma_chan = ret;
  101398. +
  101399. + ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
  101400. + 0, "bcm2708_fb dma", fb);
  101401. + if (ret) {
  101402. + pr_err("%s: failed to request DMA irq\n", __func__);
  101403. + goto free_dma_chan;
  101404. + }
  101405. +
  101406. +
  101407. + pr_info("BCM2708FB: allocated DMA channel %d @ %p\n",
  101408. + fb->dma_chan, fb->dma_chan_base);
  101409. +
  101410. + fb->dev = dev;
  101411. +
  101412. + ret = bcm2708_fb_register(fb);
  101413. + if (ret == 0) {
  101414. + platform_set_drvdata(dev, fb);
  101415. + goto out;
  101416. + }
  101417. +
  101418. +free_dma_chan:
  101419. + bcm_dma_chan_free(fb->dma_chan);
  101420. +free_cb:
  101421. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  101422. +free_fb:
  101423. + kfree(fb);
  101424. +free_region:
  101425. + dev_err(&dev->dev, "probe failed, err %d\n", ret);
  101426. +out:
  101427. + return ret;
  101428. +}
  101429. +
  101430. +static int bcm2708_fb_remove(struct platform_device *dev)
  101431. +{
  101432. + struct bcm2708_fb *fb = platform_get_drvdata(dev);
  101433. +
  101434. + platform_set_drvdata(dev, NULL);
  101435. +
  101436. + if (fb->fb.screen_base)
  101437. + iounmap(fb->fb.screen_base);
  101438. + unregister_framebuffer(&fb->fb);
  101439. +
  101440. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  101441. + bcm_dma_chan_free(fb->dma_chan);
  101442. +
  101443. + dma_free_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), (void *)fb->info,
  101444. + fb->dma);
  101445. + bcm2708_fb_debugfs_deinit(fb);
  101446. +
  101447. + free_irq(fb->dma_irq, fb);
  101448. +
  101449. + kfree(fb);
  101450. +
  101451. + return 0;
  101452. +}
  101453. +
  101454. +static struct platform_driver bcm2708_fb_driver = {
  101455. + .probe = bcm2708_fb_probe,
  101456. + .remove = bcm2708_fb_remove,
  101457. + .driver = {
  101458. + .name = DRIVER_NAME,
  101459. + .owner = THIS_MODULE,
  101460. + },
  101461. +};
  101462. +
  101463. +static int __init bcm2708_fb_init(void)
  101464. +{
  101465. + return platform_driver_register(&bcm2708_fb_driver);
  101466. +}
  101467. +
  101468. +module_init(bcm2708_fb_init);
  101469. +
  101470. +static void __exit bcm2708_fb_exit(void)
  101471. +{
  101472. + platform_driver_unregister(&bcm2708_fb_driver);
  101473. +}
  101474. +
  101475. +module_exit(bcm2708_fb_exit);
  101476. +
  101477. +module_param(fbwidth, int, 0644);
  101478. +module_param(fbheight, int, 0644);
  101479. +module_param(fbdepth, int, 0644);
  101480. +module_param(fbswap, int, 0644);
  101481. +
  101482. +MODULE_DESCRIPTION("BCM2708 framebuffer driver");
  101483. +MODULE_LICENSE("GPL");
  101484. +
  101485. +MODULE_PARM_DESC(fbwidth, "Width of ARM Framebuffer");
  101486. +MODULE_PARM_DESC(fbheight, "Height of ARM Framebuffer");
  101487. +MODULE_PARM_DESC(fbdepth, "Bit depth of ARM Framebuffer");
  101488. +MODULE_PARM_DESC(fbswap, "Swap order of red and blue in 24 and 32 bit modes");
  101489. diff -Nur linux-3.17.5/drivers/video/fbdev/core/cfbimgblt.c linux-rpi/drivers/video/fbdev/core/cfbimgblt.c
  101490. --- linux-3.17.5/drivers/video/fbdev/core/cfbimgblt.c 2014-12-06 17:57:59.000000000 -0600
  101491. +++ linux-rpi/drivers/video/fbdev/core/cfbimgblt.c 2014-12-11 14:05:39.644418001 -0600
  101492. @@ -28,6 +28,11 @@
  101493. *
  101494. * Also need to add code to deal with cards endians that are different than
  101495. * the native cpu endians. I also need to deal with MSB position in the word.
  101496. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  101497. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  101498. + * significantly faster than the previous implementation.
  101499. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  101500. + * divides.
  101501. */
  101502. #include <linux/module.h>
  101503. #include <linux/string.h>
  101504. @@ -262,6 +267,133 @@
  101505. }
  101506. }
  101507. +/*
  101508. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  101509. + * into the code, main loop unrolled.
  101510. + */
  101511. +
  101512. +static inline void fast_imageblit16(const struct fb_image *image,
  101513. + struct fb_info *p, u8 __iomem * dst1,
  101514. + u32 fgcolor, u32 bgcolor)
  101515. +{
  101516. + u32 fgx = fgcolor, bgx = bgcolor;
  101517. + u32 spitch = (image->width + 7) / 8;
  101518. + u32 end_mask, eorx;
  101519. + const char *s = image->data, *src;
  101520. + u32 __iomem *dst;
  101521. + const u32 *tab = NULL;
  101522. + int i, j, k;
  101523. +
  101524. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  101525. +
  101526. + fgx <<= 16;
  101527. + bgx <<= 16;
  101528. + fgx |= fgcolor;
  101529. + bgx |= bgcolor;
  101530. +
  101531. + eorx = fgx ^ bgx;
  101532. + k = image->width / 2;
  101533. +
  101534. + for (i = image->height; i--;) {
  101535. + dst = (u32 __iomem *) dst1;
  101536. + src = s;
  101537. +
  101538. + j = k;
  101539. + while (j >= 4) {
  101540. + u8 bits = *src;
  101541. + end_mask = tab[(bits >> 6) & 3];
  101542. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101543. + end_mask = tab[(bits >> 4) & 3];
  101544. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101545. + end_mask = tab[(bits >> 2) & 3];
  101546. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101547. + end_mask = tab[bits & 3];
  101548. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101549. + src++;
  101550. + j -= 4;
  101551. + }
  101552. + if (j != 0) {
  101553. + u8 bits = *src;
  101554. + end_mask = tab[(bits >> 6) & 3];
  101555. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101556. + if (j >= 2) {
  101557. + end_mask = tab[(bits >> 4) & 3];
  101558. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101559. + if (j == 3) {
  101560. + end_mask = tab[(bits >> 2) & 3];
  101561. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  101562. + }
  101563. + }
  101564. + }
  101565. + dst1 += p->fix.line_length;
  101566. + s += spitch;
  101567. + }
  101568. +}
  101569. +
  101570. +/*
  101571. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  101572. + * into the code, main loop unrolled.
  101573. + */
  101574. +
  101575. +static inline void fast_imageblit32(const struct fb_image *image,
  101576. + struct fb_info *p, u8 __iomem * dst1,
  101577. + u32 fgcolor, u32 bgcolor)
  101578. +{
  101579. + u32 fgx = fgcolor, bgx = bgcolor;
  101580. + u32 spitch = (image->width + 7) / 8;
  101581. + u32 end_mask, eorx;
  101582. + const char *s = image->data, *src;
  101583. + u32 __iomem *dst;
  101584. + const u32 *tab = NULL;
  101585. + int i, j, k;
  101586. +
  101587. + tab = cfb_tab32;
  101588. +
  101589. + eorx = fgx ^ bgx;
  101590. + k = image->width;
  101591. +
  101592. + for (i = image->height; i--;) {
  101593. + dst = (u32 __iomem *) dst1;
  101594. + src = s;
  101595. +
  101596. + j = k;
  101597. + while (j >= 8) {
  101598. + u8 bits = *src;
  101599. + end_mask = tab[(bits >> 7) & 1];
  101600. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101601. + end_mask = tab[(bits >> 6) & 1];
  101602. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101603. + end_mask = tab[(bits >> 5) & 1];
  101604. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101605. + end_mask = tab[(bits >> 4) & 1];
  101606. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101607. + end_mask = tab[(bits >> 3) & 1];
  101608. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101609. + end_mask = tab[(bits >> 2) & 1];
  101610. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101611. + end_mask = tab[(bits >> 1) & 1];
  101612. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101613. + end_mask = tab[bits & 1];
  101614. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101615. + src++;
  101616. + j -= 8;
  101617. + }
  101618. + if (j != 0) {
  101619. + u32 bits = (u32) * src;
  101620. + while (j > 1) {
  101621. + end_mask = tab[(bits >> 7) & 1];
  101622. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  101623. + bits <<= 1;
  101624. + j--;
  101625. + }
  101626. + end_mask = tab[(bits >> 7) & 1];
  101627. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  101628. + }
  101629. + dst1 += p->fix.line_length;
  101630. + s += spitch;
  101631. + }
  101632. +}
  101633. +
  101634. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  101635. {
  101636. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  101637. @@ -294,11 +426,21 @@
  101638. bgcolor = image->bg_color;
  101639. }
  101640. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  101641. - ((width & (32/bpp-1)) == 0) &&
  101642. - bpp >= 8 && bpp <= 32)
  101643. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  101644. - else
  101645. + if (!start_index && !pitch_index) {
  101646. + if (bpp == 32)
  101647. + fast_imageblit32(image, p, dst1, fgcolor,
  101648. + bgcolor);
  101649. + else if (bpp == 16 && (width & 1) == 0)
  101650. + fast_imageblit16(image, p, dst1, fgcolor,
  101651. + bgcolor);
  101652. + else if (bpp == 8 && (width & 3) == 0)
  101653. + fast_imageblit(image, p, dst1, fgcolor,
  101654. + bgcolor);
  101655. + else
  101656. + slow_imageblit(image, p, dst1, fgcolor,
  101657. + bgcolor,
  101658. + start_index, pitch_index);
  101659. + } else
  101660. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  101661. start_index, pitch_index);
  101662. } else
  101663. diff -Nur linux-3.17.5/drivers/video/fbdev/core/fbmem.c linux-rpi/drivers/video/fbdev/core/fbmem.c
  101664. --- linux-3.17.5/drivers/video/fbdev/core/fbmem.c 2014-12-06 17:57:59.000000000 -0600
  101665. +++ linux-rpi/drivers/video/fbdev/core/fbmem.c 2014-12-11 14:05:39.644418001 -0600
  101666. @@ -1084,6 +1084,25 @@
  101667. }
  101668. EXPORT_SYMBOL(fb_blank);
  101669. +static int fb_copyarea_user(struct fb_info *info,
  101670. + struct fb_copyarea *copy)
  101671. +{
  101672. + int ret = 0;
  101673. + if (!lock_fb_info(info))
  101674. + return -ENODEV;
  101675. + if (copy->dx + copy->width > info->var.xres ||
  101676. + copy->sx + copy->width > info->var.xres ||
  101677. + copy->dy + copy->height > info->var.yres ||
  101678. + copy->sy + copy->height > info->var.yres) {
  101679. + ret = -EINVAL;
  101680. + goto out;
  101681. + }
  101682. + info->fbops->fb_copyarea(info, copy);
  101683. +out:
  101684. + unlock_fb_info(info);
  101685. + return ret;
  101686. +}
  101687. +
  101688. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  101689. unsigned long arg)
  101690. {
  101691. @@ -1094,6 +1113,7 @@
  101692. struct fb_cmap cmap_from;
  101693. struct fb_cmap_user cmap;
  101694. struct fb_event event;
  101695. + struct fb_copyarea copy;
  101696. void __user *argp = (void __user *)arg;
  101697. long ret = 0;
  101698. @@ -1211,6 +1231,15 @@
  101699. unlock_fb_info(info);
  101700. console_unlock();
  101701. break;
  101702. + case FBIOCOPYAREA:
  101703. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  101704. + /* only provide this ioctl if it is accelerated */
  101705. + if (copy_from_user(&copy, argp, sizeof(copy)))
  101706. + return -EFAULT;
  101707. + ret = fb_copyarea_user(info, &copy);
  101708. + break;
  101709. + }
  101710. + /* fall through */
  101711. default:
  101712. if (!lock_fb_info(info))
  101713. return -ENODEV;
  101714. @@ -1365,6 +1394,7 @@
  101715. case FBIOPAN_DISPLAY:
  101716. case FBIOGET_CON2FBMAP:
  101717. case FBIOPUT_CON2FBMAP:
  101718. + case FBIOCOPYAREA:
  101719. arg = (unsigned long) compat_ptr(arg);
  101720. case FBIOBLANK:
  101721. ret = do_fb_ioctl(info, cmd, arg);
  101722. diff -Nur linux-3.17.5/drivers/video/fbdev/Kconfig linux-rpi/drivers/video/fbdev/Kconfig
  101723. --- linux-3.17.5/drivers/video/fbdev/Kconfig 2014-12-06 17:57:59.000000000 -0600
  101724. +++ linux-rpi/drivers/video/fbdev/Kconfig 2014-12-11 14:05:39.632418001 -0600
  101725. @@ -220,6 +220,20 @@
  101726. comment "Frame buffer hardware drivers"
  101727. depends on FB
  101728. +config FB_BCM2708
  101729. + tristate "BCM2708 framebuffer support"
  101730. + depends on FB && ARM
  101731. + select FB_CFB_FILLRECT
  101732. + select FB_CFB_COPYAREA
  101733. + select FB_CFB_IMAGEBLIT
  101734. + help
  101735. + This framebuffer device driver is for the BCM2708 framebuffer.
  101736. +
  101737. + If you want to compile this as a module (=code which can be
  101738. + inserted into and removed from the running kernel), say M
  101739. + here and read <file:Documentation/kbuild/modules.txt>. The module
  101740. + will be called bcm2708_fb.
  101741. +
  101742. config FB_GRVGA
  101743. tristate "Aeroflex Gaisler framebuffer support"
  101744. depends on FB && SPARC
  101745. diff -Nur linux-3.17.5/drivers/video/fbdev/Makefile linux-rpi/drivers/video/fbdev/Makefile
  101746. --- linux-3.17.5/drivers/video/fbdev/Makefile 2014-12-06 17:57:59.000000000 -0600
  101747. +++ linux-rpi/drivers/video/fbdev/Makefile 2014-12-11 14:05:39.632418001 -0600
  101748. @@ -12,6 +12,7 @@
  101749. obj-$(CONFIG_FB_WMT_GE_ROPS) += wmt_ge_rops.o
  101750. # Hardware specific drivers go first
  101751. +obj-$(CONFIG_FB_BCM2708) += bcm2708_fb.o
  101752. obj-$(CONFIG_FB_AMIGA) += amifb.o c2p_planar.o
  101753. obj-$(CONFIG_FB_ARC) += arcfb.o
  101754. obj-$(CONFIG_FB_CLPS711X) += clps711x-fb.o
  101755. diff -Nur linux-3.17.5/drivers/video/logo/logo_linux_clut224.ppm linux-rpi/drivers/video/logo/logo_linux_clut224.ppm
  101756. --- linux-3.17.5/drivers/video/logo/logo_linux_clut224.ppm 2014-12-06 17:57:59.000000000 -0600
  101757. +++ linux-rpi/drivers/video/logo/logo_linux_clut224.ppm 2014-12-11 14:02:55.836418071 -0600
  101758. @@ -1,1604 +1,883 @@
  101759. P3
  101760. -# Standard 224-color Linux logo
  101761. -80 80
  101762. +63 80
  101763. 255
  101764. - 0 0 0 0 0 0 0 0 0 0 0 0
  101765. - 0 0 0 0 0 0 0 0 0 0 0 0
  101766. - 0 0 0 0 0 0 0 0 0 0 0 0
  101767. - 0 0 0 0 0 0 0 0 0 0 0 0
  101768. - 0 0 0 0 0 0 0 0 0 0 0 0
  101769. - 0 0 0 0 0 0 0 0 0 0 0 0
  101770. - 0 0 0 0 0 0 0 0 0 0 0 0
  101771. - 0 0 0 0 0 0 0 0 0 0 0 0
  101772. - 0 0 0 0 0 0 0 0 0 0 0 0
  101773. - 6 6 6 6 6 6 10 10 10 10 10 10
  101774. - 10 10 10 6 6 6 6 6 6 6 6 6
  101775. - 0 0 0 0 0 0 0 0 0 0 0 0
  101776. - 0 0 0 0 0 0 0 0 0 0 0 0
  101777. - 0 0 0 0 0 0 0 0 0 0 0 0
  101778. - 0 0 0 0 0 0 0 0 0 0 0 0
  101779. - 0 0 0 0 0 0 0 0 0 0 0 0
  101780. - 0 0 0 0 0 0 0 0 0 0 0 0
  101781. - 0 0 0 0 0 0 0 0 0 0 0 0
  101782. - 0 0 0 0 0 0 0 0 0 0 0 0
  101783. - 0 0 0 0 0 0 0 0 0 0 0 0
  101784. - 0 0 0 0 0 0 0 0 0 0 0 0
  101785. - 0 0 0 0 0 0 0 0 0 0 0 0
  101786. - 0 0 0 0 0 0 0 0 0 0 0 0
  101787. - 0 0 0 0 0 0 0 0 0 0 0 0
  101788. - 0 0 0 0 0 0 0 0 0 0 0 0
  101789. - 0 0 0 0 0 0 0 0 0 0 0 0
  101790. - 0 0 0 0 0 0 0 0 0 0 0 0
  101791. - 0 0 0 0 0 0 0 0 0 0 0 0
  101792. - 0 0 0 6 6 6 10 10 10 14 14 14
  101793. - 22 22 22 26 26 26 30 30 30 34 34 34
  101794. - 30 30 30 30 30 30 26 26 26 18 18 18
  101795. - 14 14 14 10 10 10 6 6 6 0 0 0
  101796. - 0 0 0 0 0 0 0 0 0 0 0 0
  101797. - 0 0 0 0 0 0 0 0 0 0 0 0
  101798. - 0 0 0 0 0 0 0 0 0 0 0 0
  101799. - 0 0 0 0 0 0 0 0 0 0 0 0
  101800. - 0 0 0 0 0 0 0 0 0 0 0 0
  101801. - 0 0 0 0 0 0 0 0 0 0 0 0
  101802. - 0 0 0 0 0 0 0 0 0 0 0 0
  101803. - 0 0 0 0 0 0 0 0 0 0 0 0
  101804. - 0 0 0 0 0 0 0 0 0 0 0 0
  101805. - 0 0 0 0 0 1 0 0 1 0 0 0
  101806. - 0 0 0 0 0 0 0 0 0 0 0 0
  101807. - 0 0 0 0 0 0 0 0 0 0 0 0
  101808. - 0 0 0 0 0 0 0 0 0 0 0 0
  101809. - 0 0 0 0 0 0 0 0 0 0 0 0
  101810. - 0 0 0 0 0 0 0 0 0 0 0 0
  101811. - 0 0 0 0 0 0 0 0 0 0 0 0
  101812. - 6 6 6 14 14 14 26 26 26 42 42 42
  101813. - 54 54 54 66 66 66 78 78 78 78 78 78
  101814. - 78 78 78 74 74 74 66 66 66 54 54 54
  101815. - 42 42 42 26 26 26 18 18 18 10 10 10
  101816. - 6 6 6 0 0 0 0 0 0 0 0 0
  101817. - 0 0 0 0 0 0 0 0 0 0 0 0
  101818. - 0 0 0 0 0 0 0 0 0 0 0 0
  101819. - 0 0 0 0 0 0 0 0 0 0 0 0
  101820. - 0 0 0 0 0 0 0 0 0 0 0 0
  101821. - 0 0 0 0 0 0 0 0 0 0 0 0
  101822. - 0 0 0 0 0 0 0 0 0 0 0 0
  101823. - 0 0 0 0 0 0 0 0 0 0 0 0
  101824. - 0 0 0 0 0 0 0 0 0 0 0 0
  101825. - 0 0 1 0 0 0 0 0 0 0 0 0
  101826. - 0 0 0 0 0 0 0 0 0 0 0 0
  101827. - 0 0 0 0 0 0 0 0 0 0 0 0
  101828. - 0 0 0 0 0 0 0 0 0 0 0 0
  101829. - 0 0 0 0 0 0 0 0 0 0 0 0
  101830. - 0 0 0 0 0 0 0 0 0 0 0 0
  101831. - 0 0 0 0 0 0 0 0 0 10 10 10
  101832. - 22 22 22 42 42 42 66 66 66 86 86 86
  101833. - 66 66 66 38 38 38 38 38 38 22 22 22
  101834. - 26 26 26 34 34 34 54 54 54 66 66 66
  101835. - 86 86 86 70 70 70 46 46 46 26 26 26
  101836. - 14 14 14 6 6 6 0 0 0 0 0 0
  101837. - 0 0 0 0 0 0 0 0 0 0 0 0
  101838. - 0 0 0 0 0 0 0 0 0 0 0 0
  101839. - 0 0 0 0 0 0 0 0 0 0 0 0
  101840. - 0 0 0 0 0 0 0 0 0 0 0 0
  101841. - 0 0 0 0 0 0 0 0 0 0 0 0
  101842. - 0 0 0 0 0 0 0 0 0 0 0 0
  101843. - 0 0 0 0 0 0 0 0 0 0 0 0
  101844. - 0 0 0 0 0 0 0 0 0 0 0 0
  101845. - 0 0 1 0 0 1 0 0 1 0 0 0
  101846. - 0 0 0 0 0 0 0 0 0 0 0 0
  101847. - 0 0 0 0 0 0 0 0 0 0 0 0
  101848. - 0 0 0 0 0 0 0 0 0 0 0 0
  101849. - 0 0 0 0 0 0 0 0 0 0 0 0
  101850. - 0 0 0 0 0 0 0 0 0 0 0 0
  101851. - 0 0 0 0 0 0 10 10 10 26 26 26
  101852. - 50 50 50 82 82 82 58 58 58 6 6 6
  101853. - 2 2 6 2 2 6 2 2 6 2 2 6
  101854. - 2 2 6 2 2 6 2 2 6 2 2 6
  101855. - 6 6 6 54 54 54 86 86 86 66 66 66
  101856. - 38 38 38 18 18 18 6 6 6 0 0 0
  101857. - 0 0 0 0 0 0 0 0 0 0 0 0
  101858. - 0 0 0 0 0 0 0 0 0 0 0 0
  101859. - 0 0 0 0 0 0 0 0 0 0 0 0
  101860. - 0 0 0 0 0 0 0 0 0 0 0 0
  101861. - 0 0 0 0 0 0 0 0 0 0 0 0
  101862. - 0 0 0 0 0 0 0 0 0 0 0 0
  101863. - 0 0 0 0 0 0 0 0 0 0 0 0
  101864. - 0 0 0 0 0 0 0 0 0 0 0 0
  101865. - 0 0 0 0 0 0 0 0 0 0 0 0
  101866. - 0 0 0 0 0 0 0 0 0 0 0 0
  101867. - 0 0 0 0 0 0 0 0 0 0 0 0
  101868. - 0 0 0 0 0 0 0 0 0 0 0 0
  101869. - 0 0 0 0 0 0 0 0 0 0 0 0
  101870. - 0 0 0 0 0 0 0 0 0 0 0 0
  101871. - 0 0 0 6 6 6 22 22 22 50 50 50
  101872. - 78 78 78 34 34 34 2 2 6 2 2 6
  101873. - 2 2 6 2 2 6 2 2 6 2 2 6
  101874. - 2 2 6 2 2 6 2 2 6 2 2 6
  101875. - 2 2 6 2 2 6 6 6 6 70 70 70
  101876. - 78 78 78 46 46 46 22 22 22 6 6 6
  101877. - 0 0 0 0 0 0 0 0 0 0 0 0
  101878. - 0 0 0 0 0 0 0 0 0 0 0 0
  101879. - 0 0 0 0 0 0 0 0 0 0 0 0
  101880. - 0 0 0 0 0 0 0 0 0 0 0 0
  101881. - 0 0 0 0 0 0 0 0 0 0 0 0
  101882. - 0 0 0 0 0 0 0 0 0 0 0 0
  101883. - 0 0 0 0 0 0 0 0 0 0 0 0
  101884. - 0 0 0 0 0 0 0 0 0 0 0 0
  101885. - 0 0 1 0 0 1 0 0 1 0 0 0
  101886. - 0 0 0 0 0 0 0 0 0 0 0 0
  101887. - 0 0 0 0 0 0 0 0 0 0 0 0
  101888. - 0 0 0 0 0 0 0 0 0 0 0 0
  101889. - 0 0 0 0 0 0 0 0 0 0 0 0
  101890. - 0 0 0 0 0 0 0 0 0 0 0 0
  101891. - 6 6 6 18 18 18 42 42 42 82 82 82
  101892. - 26 26 26 2 2 6 2 2 6 2 2 6
  101893. - 2 2 6 2 2 6 2 2 6 2 2 6
  101894. - 2 2 6 2 2 6 2 2 6 14 14 14
  101895. - 46 46 46 34 34 34 6 6 6 2 2 6
  101896. - 42 42 42 78 78 78 42 42 42 18 18 18
  101897. - 6 6 6 0 0 0 0 0 0 0 0 0
  101898. - 0 0 0 0 0 0 0 0 0 0 0 0
  101899. - 0 0 0 0 0 0 0 0 0 0 0 0
  101900. - 0 0 0 0 0 0 0 0 0 0 0 0
  101901. - 0 0 0 0 0 0 0 0 0 0 0 0
  101902. - 0 0 0 0 0 0 0 0 0 0 0 0
  101903. - 0 0 0 0 0 0 0 0 0 0 0 0
  101904. - 0 0 0 0 0 0 0 0 0 0 0 0
  101905. - 0 0 1 0 0 0 0 0 1 0 0 0
  101906. - 0 0 0 0 0 0 0 0 0 0 0 0
  101907. - 0 0 0 0 0 0 0 0 0 0 0 0
  101908. - 0 0 0 0 0 0 0 0 0 0 0 0
  101909. - 0 0 0 0 0 0 0 0 0 0 0 0
  101910. - 0 0 0 0 0 0 0 0 0 0 0 0
  101911. - 10 10 10 30 30 30 66 66 66 58 58 58
  101912. - 2 2 6 2 2 6 2 2 6 2 2 6
  101913. - 2 2 6 2 2 6 2 2 6 2 2 6
  101914. - 2 2 6 2 2 6 2 2 6 26 26 26
  101915. - 86 86 86 101 101 101 46 46 46 10 10 10
  101916. - 2 2 6 58 58 58 70 70 70 34 34 34
  101917. - 10 10 10 0 0 0 0 0 0 0 0 0
  101918. - 0 0 0 0 0 0 0 0 0 0 0 0
  101919. - 0 0 0 0 0 0 0 0 0 0 0 0
  101920. - 0 0 0 0 0 0 0 0 0 0 0 0
  101921. - 0 0 0 0 0 0 0 0 0 0 0 0
  101922. - 0 0 0 0 0 0 0 0 0 0 0 0
  101923. - 0 0 0 0 0 0 0 0 0 0 0 0
  101924. - 0 0 0 0 0 0 0 0 0 0 0 0
  101925. - 0 0 1 0 0 1 0 0 1 0 0 0
  101926. - 0 0 0 0 0 0 0 0 0 0 0 0
  101927. - 0 0 0 0 0 0 0 0 0 0 0 0
  101928. - 0 0 0 0 0 0 0 0 0 0 0 0
  101929. - 0 0 0 0 0 0 0 0 0 0 0 0
  101930. - 0 0 0 0 0 0 0 0 0 0 0 0
  101931. - 14 14 14 42 42 42 86 86 86 10 10 10
  101932. - 2 2 6 2 2 6 2 2 6 2 2 6
  101933. - 2 2 6 2 2 6 2 2 6 2 2 6
  101934. - 2 2 6 2 2 6 2 2 6 30 30 30
  101935. - 94 94 94 94 94 94 58 58 58 26 26 26
  101936. - 2 2 6 6 6 6 78 78 78 54 54 54
  101937. - 22 22 22 6 6 6 0 0 0 0 0 0
  101938. - 0 0 0 0 0 0 0 0 0 0 0 0
  101939. - 0 0 0 0 0 0 0 0 0 0 0 0
  101940. - 0 0 0 0 0 0 0 0 0 0 0 0
  101941. - 0 0 0 0 0 0 0 0 0 0 0 0
  101942. - 0 0 0 0 0 0 0 0 0 0 0 0
  101943. - 0 0 0 0 0 0 0 0 0 0 0 0
  101944. - 0 0 0 0 0 0 0 0 0 0 0 0
  101945. - 0 0 0 0 0 0 0 0 0 0 0 0
  101946. - 0 0 0 0 0 0 0 0 0 0 0 0
  101947. - 0 0 0 0 0 0 0 0 0 0 0 0
  101948. - 0 0 0 0 0 0 0 0 0 0 0 0
  101949. - 0 0 0 0 0 0 0 0 0 0 0 0
  101950. - 0 0 0 0 0 0 0 0 0 6 6 6
  101951. - 22 22 22 62 62 62 62 62 62 2 2 6
  101952. - 2 2 6 2 2 6 2 2 6 2 2 6
  101953. - 2 2 6 2 2 6 2 2 6 2 2 6
  101954. - 2 2 6 2 2 6 2 2 6 26 26 26
  101955. - 54 54 54 38 38 38 18 18 18 10 10 10
  101956. - 2 2 6 2 2 6 34 34 34 82 82 82
  101957. - 38 38 38 14 14 14 0 0 0 0 0 0
  101958. - 0 0 0 0 0 0 0 0 0 0 0 0
  101959. - 0 0 0 0 0 0 0 0 0 0 0 0
  101960. - 0 0 0 0 0 0 0 0 0 0 0 0
  101961. - 0 0 0 0 0 0 0 0 0 0 0 0
  101962. - 0 0 0 0 0 0 0 0 0 0 0 0
  101963. - 0 0 0 0 0 0 0 0 0 0 0 0
  101964. - 0 0 0 0 0 0 0 0 0 0 0 0
  101965. - 0 0 0 0 0 1 0 0 1 0 0 0
  101966. - 0 0 0 0 0 0 0 0 0 0 0 0
  101967. - 0 0 0 0 0 0 0 0 0 0 0 0
  101968. - 0 0 0 0 0 0 0 0 0 0 0 0
  101969. - 0 0 0 0 0 0 0 0 0 0 0 0
  101970. - 0 0 0 0 0 0 0 0 0 6 6 6
  101971. - 30 30 30 78 78 78 30 30 30 2 2 6
  101972. - 2 2 6 2 2 6 2 2 6 2 2 6
  101973. - 2 2 6 2 2 6 2 2 6 2 2 6
  101974. - 2 2 6 2 2 6 2 2 6 10 10 10
  101975. - 10 10 10 2 2 6 2 2 6 2 2 6
  101976. - 2 2 6 2 2 6 2 2 6 78 78 78
  101977. - 50 50 50 18 18 18 6 6 6 0 0 0
  101978. - 0 0 0 0 0 0 0 0 0 0 0 0
  101979. - 0 0 0 0 0 0 0 0 0 0 0 0
  101980. - 0 0 0 0 0 0 0 0 0 0 0 0
  101981. - 0 0 0 0 0 0 0 0 0 0 0 0
  101982. - 0 0 0 0 0 0 0 0 0 0 0 0
  101983. - 0 0 0 0 0 0 0 0 0 0 0 0
  101984. - 0 0 0 0 0 0 0 0 0 0 0 0
  101985. - 0 0 1 0 0 0 0 0 0 0 0 0
  101986. - 0 0 0 0 0 0 0 0 0 0 0 0
  101987. - 0 0 0 0 0 0 0 0 0 0 0 0
  101988. - 0 0 0 0 0 0 0 0 0 0 0 0
  101989. - 0 0 0 0 0 0 0 0 0 0 0 0
  101990. - 0 0 0 0 0 0 0 0 0 10 10 10
  101991. - 38 38 38 86 86 86 14 14 14 2 2 6
  101992. - 2 2 6 2 2 6 2 2 6 2 2 6
  101993. - 2 2 6 2 2 6 2 2 6 2 2 6
  101994. - 2 2 6 2 2 6 2 2 6 2 2 6
  101995. - 2 2 6 2 2 6 2 2 6 2 2 6
  101996. - 2 2 6 2 2 6 2 2 6 54 54 54
  101997. - 66 66 66 26 26 26 6 6 6 0 0 0
  101998. - 0 0 0 0 0 0 0 0 0 0 0 0
  101999. - 0 0 0 0 0 0 0 0 0 0 0 0
  102000. - 0 0 0 0 0 0 0 0 0 0 0 0
  102001. - 0 0 0 0 0 0 0 0 0 0 0 0
  102002. - 0 0 0 0 0 0 0 0 0 0 0 0
  102003. - 0 0 0 0 0 0 0 0 0 0 0 0
  102004. - 0 0 0 0 0 0 0 0 0 0 0 0
  102005. - 0 0 0 0 0 1 0 0 1 0 0 0
  102006. - 0 0 0 0 0 0 0 0 0 0 0 0
  102007. - 0 0 0 0 0 0 0 0 0 0 0 0
  102008. - 0 0 0 0 0 0 0 0 0 0 0 0
  102009. - 0 0 0 0 0 0 0 0 0 0 0 0
  102010. - 0 0 0 0 0 0 0 0 0 14 14 14
  102011. - 42 42 42 82 82 82 2 2 6 2 2 6
  102012. - 2 2 6 6 6 6 10 10 10 2 2 6
  102013. - 2 2 6 2 2 6 2 2 6 2 2 6
  102014. - 2 2 6 2 2 6 2 2 6 6 6 6
  102015. - 14 14 14 10 10 10 2 2 6 2 2 6
  102016. - 2 2 6 2 2 6 2 2 6 18 18 18
  102017. - 82 82 82 34 34 34 10 10 10 0 0 0
  102018. - 0 0 0 0 0 0 0 0 0 0 0 0
  102019. - 0 0 0 0 0 0 0 0 0 0 0 0
  102020. - 0 0 0 0 0 0 0 0 0 0 0 0
  102021. - 0 0 0 0 0 0 0 0 0 0 0 0
  102022. - 0 0 0 0 0 0 0 0 0 0 0 0
  102023. - 0 0 0 0 0 0 0 0 0 0 0 0
  102024. - 0 0 0 0 0 0 0 0 0 0 0 0
  102025. - 0 0 1 0 0 0 0 0 0 0 0 0
  102026. - 0 0 0 0 0 0 0 0 0 0 0 0
  102027. - 0 0 0 0 0 0 0 0 0 0 0 0
  102028. - 0 0 0 0 0 0 0 0 0 0 0 0
  102029. - 0 0 0 0 0 0 0 0 0 0 0 0
  102030. - 0 0 0 0 0 0 0 0 0 14 14 14
  102031. - 46 46 46 86 86 86 2 2 6 2 2 6
  102032. - 6 6 6 6 6 6 22 22 22 34 34 34
  102033. - 6 6 6 2 2 6 2 2 6 2 2 6
  102034. - 2 2 6 2 2 6 18 18 18 34 34 34
  102035. - 10 10 10 50 50 50 22 22 22 2 2 6
  102036. - 2 2 6 2 2 6 2 2 6 10 10 10
  102037. - 86 86 86 42 42 42 14 14 14 0 0 0
  102038. - 0 0 0 0 0 0 0 0 0 0 0 0
  102039. - 0 0 0 0 0 0 0 0 0 0 0 0
  102040. - 0 0 0 0 0 0 0 0 0 0 0 0
  102041. - 0 0 0 0 0 0 0 0 0 0 0 0
  102042. - 0 0 0 0 0 0 0 0 0 0 0 0
  102043. - 0 0 0 0 0 0 0 0 0 0 0 0
  102044. - 0 0 0 0 0 0 0 0 0 0 0 0
  102045. - 0 0 1 0 0 1 0 0 1 0 0 0
  102046. - 0 0 0 0 0 0 0 0 0 0 0 0
  102047. - 0 0 0 0 0 0 0 0 0 0 0 0
  102048. - 0 0 0 0 0 0 0 0 0 0 0 0
  102049. - 0 0 0 0 0 0 0 0 0 0 0 0
  102050. - 0 0 0 0 0 0 0 0 0 14 14 14
  102051. - 46 46 46 86 86 86 2 2 6 2 2 6
  102052. - 38 38 38 116 116 116 94 94 94 22 22 22
  102053. - 22 22 22 2 2 6 2 2 6 2 2 6
  102054. - 14 14 14 86 86 86 138 138 138 162 162 162
  102055. -154 154 154 38 38 38 26 26 26 6 6 6
  102056. - 2 2 6 2 2 6 2 2 6 2 2 6
  102057. - 86 86 86 46 46 46 14 14 14 0 0 0
  102058. - 0 0 0 0 0 0 0 0 0 0 0 0
  102059. - 0 0 0 0 0 0 0 0 0 0 0 0
  102060. - 0 0 0 0 0 0 0 0 0 0 0 0
  102061. - 0 0 0 0 0 0 0 0 0 0 0 0
  102062. - 0 0 0 0 0 0 0 0 0 0 0 0
  102063. - 0 0 0 0 0 0 0 0 0 0 0 0
  102064. - 0 0 0 0 0 0 0 0 0 0 0 0
  102065. - 0 0 0 0 0 0 0 0 0 0 0 0
  102066. - 0 0 0 0 0 0 0 0 0 0 0 0
  102067. - 0 0 0 0 0 0 0 0 0 0 0 0
  102068. - 0 0 0 0 0 0 0 0 0 0 0 0
  102069. - 0 0 0 0 0 0 0 0 0 0 0 0
  102070. - 0 0 0 0 0 0 0 0 0 14 14 14
  102071. - 46 46 46 86 86 86 2 2 6 14 14 14
  102072. -134 134 134 198 198 198 195 195 195 116 116 116
  102073. - 10 10 10 2 2 6 2 2 6 6 6 6
  102074. -101 98 89 187 187 187 210 210 210 218 218 218
  102075. -214 214 214 134 134 134 14 14 14 6 6 6
  102076. - 2 2 6 2 2 6 2 2 6 2 2 6
  102077. - 86 86 86 50 50 50 18 18 18 6 6 6
  102078. - 0 0 0 0 0 0 0 0 0 0 0 0
  102079. - 0 0 0 0 0 0 0 0 0 0 0 0
  102080. - 0 0 0 0 0 0 0 0 0 0 0 0
  102081. - 0 0 0 0 0 0 0 0 0 0 0 0
  102082. - 0 0 0 0 0 0 0 0 0 0 0 0
  102083. - 0 0 0 0 0 0 0 0 0 0 0 0
  102084. - 0 0 0 0 0 0 0 0 1 0 0 0
  102085. - 0 0 1 0 0 1 0 0 1 0 0 0
  102086. - 0 0 0 0 0 0 0 0 0 0 0 0
  102087. - 0 0 0 0 0 0 0 0 0 0 0 0
  102088. - 0 0 0 0 0 0 0 0 0 0 0 0
  102089. - 0 0 0 0 0 0 0 0 0 0 0 0
  102090. - 0 0 0 0 0 0 0 0 0 14 14 14
  102091. - 46 46 46 86 86 86 2 2 6 54 54 54
  102092. -218 218 218 195 195 195 226 226 226 246 246 246
  102093. - 58 58 58 2 2 6 2 2 6 30 30 30
  102094. -210 210 210 253 253 253 174 174 174 123 123 123
  102095. -221 221 221 234 234 234 74 74 74 2 2 6
  102096. - 2 2 6 2 2 6 2 2 6 2 2 6
  102097. - 70 70 70 58 58 58 22 22 22 6 6 6
  102098. - 0 0 0 0 0 0 0 0 0 0 0 0
  102099. - 0 0 0 0 0 0 0 0 0 0 0 0
  102100. - 0 0 0 0 0 0 0 0 0 0 0 0
  102101. - 0 0 0 0 0 0 0 0 0 0 0 0
  102102. - 0 0 0 0 0 0 0 0 0 0 0 0
  102103. - 0 0 0 0 0 0 0 0 0 0 0 0
  102104. - 0 0 0 0 0 0 0 0 0 0 0 0
  102105. - 0 0 0 0 0 0 0 0 0 0 0 0
  102106. - 0 0 0 0 0 0 0 0 0 0 0 0
  102107. - 0 0 0 0 0 0 0 0 0 0 0 0
  102108. - 0 0 0 0 0 0 0 0 0 0 0 0
  102109. - 0 0 0 0 0 0 0 0 0 0 0 0
  102110. - 0 0 0 0 0 0 0 0 0 14 14 14
  102111. - 46 46 46 82 82 82 2 2 6 106 106 106
  102112. -170 170 170 26 26 26 86 86 86 226 226 226
  102113. -123 123 123 10 10 10 14 14 14 46 46 46
  102114. -231 231 231 190 190 190 6 6 6 70 70 70
  102115. - 90 90 90 238 238 238 158 158 158 2 2 6
  102116. - 2 2 6 2 2 6 2 2 6 2 2 6
  102117. - 70 70 70 58 58 58 22 22 22 6 6 6
  102118. - 0 0 0 0 0 0 0 0 0 0 0 0
  102119. - 0 0 0 0 0 0 0 0 0 0 0 0
  102120. - 0 0 0 0 0 0 0 0 0 0 0 0
  102121. - 0 0 0 0 0 0 0 0 0 0 0 0
  102122. - 0 0 0 0 0 0 0 0 0 0 0 0
  102123. - 0 0 0 0 0 0 0 0 0 0 0 0
  102124. - 0 0 0 0 0 0 0 0 1 0 0 0
  102125. - 0 0 1 0 0 1 0 0 1 0 0 0
  102126. - 0 0 0 0 0 0 0 0 0 0 0 0
  102127. - 0 0 0 0 0 0 0 0 0 0 0 0
  102128. - 0 0 0 0 0 0 0 0 0 0 0 0
  102129. - 0 0 0 0 0 0 0 0 0 0 0 0
  102130. - 0 0 0 0 0 0 0 0 0 14 14 14
  102131. - 42 42 42 86 86 86 6 6 6 116 116 116
  102132. -106 106 106 6 6 6 70 70 70 149 149 149
  102133. -128 128 128 18 18 18 38 38 38 54 54 54
  102134. -221 221 221 106 106 106 2 2 6 14 14 14
  102135. - 46 46 46 190 190 190 198 198 198 2 2 6
  102136. - 2 2 6 2 2 6 2 2 6 2 2 6
  102137. - 74 74 74 62 62 62 22 22 22 6 6 6
  102138. - 0 0 0 0 0 0 0 0 0 0 0 0
  102139. - 0 0 0 0 0 0 0 0 0 0 0 0
  102140. - 0 0 0 0 0 0 0 0 0 0 0 0
  102141. - 0 0 0 0 0 0 0 0 0 0 0 0
  102142. - 0 0 0 0 0 0 0 0 0 0 0 0
  102143. - 0 0 0 0 0 0 0 0 0 0 0 0
  102144. - 0 0 0 0 0 0 0 0 1 0 0 0
  102145. - 0 0 1 0 0 0 0 0 1 0 0 0
  102146. - 0 0 0 0 0 0 0 0 0 0 0 0
  102147. - 0 0 0 0 0 0 0 0 0 0 0 0
  102148. - 0 0 0 0 0 0 0 0 0 0 0 0
  102149. - 0 0 0 0 0 0 0 0 0 0 0 0
  102150. - 0 0 0 0 0 0 0 0 0 14 14 14
  102151. - 42 42 42 94 94 94 14 14 14 101 101 101
  102152. -128 128 128 2 2 6 18 18 18 116 116 116
  102153. -118 98 46 121 92 8 121 92 8 98 78 10
  102154. -162 162 162 106 106 106 2 2 6 2 2 6
  102155. - 2 2 6 195 195 195 195 195 195 6 6 6
  102156. - 2 2 6 2 2 6 2 2 6 2 2 6
  102157. - 74 74 74 62 62 62 22 22 22 6 6 6
  102158. - 0 0 0 0 0 0 0 0 0 0 0 0
  102159. - 0 0 0 0 0 0 0 0 0 0 0 0
  102160. - 0 0 0 0 0 0 0 0 0 0 0 0
  102161. - 0 0 0 0 0 0 0 0 0 0 0 0
  102162. - 0 0 0 0 0 0 0 0 0 0 0 0
  102163. - 0 0 0 0 0 0 0 0 0 0 0 0
  102164. - 0 0 0 0 0 0 0 0 1 0 0 1
  102165. - 0 0 1 0 0 0 0 0 1 0 0 0
  102166. - 0 0 0 0 0 0 0 0 0 0 0 0
  102167. - 0 0 0 0 0 0 0 0 0 0 0 0
  102168. - 0 0 0 0 0 0 0 0 0 0 0 0
  102169. - 0 0 0 0 0 0 0 0 0 0 0 0
  102170. - 0 0 0 0 0 0 0 0 0 10 10 10
  102171. - 38 38 38 90 90 90 14 14 14 58 58 58
  102172. -210 210 210 26 26 26 54 38 6 154 114 10
  102173. -226 170 11 236 186 11 225 175 15 184 144 12
  102174. -215 174 15 175 146 61 37 26 9 2 2 6
  102175. - 70 70 70 246 246 246 138 138 138 2 2 6
  102176. - 2 2 6 2 2 6 2 2 6 2 2 6
  102177. - 70 70 70 66 66 66 26 26 26 6 6 6
  102178. - 0 0 0 0 0 0 0 0 0 0 0 0
  102179. - 0 0 0 0 0 0 0 0 0 0 0 0
  102180. - 0 0 0 0 0 0 0 0 0 0 0 0
  102181. - 0 0 0 0 0 0 0 0 0 0 0 0
  102182. - 0 0 0 0 0 0 0 0 0 0 0 0
  102183. - 0 0 0 0 0 0 0 0 0 0 0 0
  102184. - 0 0 0 0 0 0 0 0 0 0 0 0
  102185. - 0 0 0 0 0 0 0 0 0 0 0 0
  102186. - 0 0 0 0 0 0 0 0 0 0 0 0
  102187. - 0 0 0 0 0 0 0 0 0 0 0 0
  102188. - 0 0 0 0 0 0 0 0 0 0 0 0
  102189. - 0 0 0 0 0 0 0 0 0 0 0 0
  102190. - 0 0 0 0 0 0 0 0 0 10 10 10
  102191. - 38 38 38 86 86 86 14 14 14 10 10 10
  102192. -195 195 195 188 164 115 192 133 9 225 175 15
  102193. -239 182 13 234 190 10 232 195 16 232 200 30
  102194. -245 207 45 241 208 19 232 195 16 184 144 12
  102195. -218 194 134 211 206 186 42 42 42 2 2 6
  102196. - 2 2 6 2 2 6 2 2 6 2 2 6
  102197. - 50 50 50 74 74 74 30 30 30 6 6 6
  102198. - 0 0 0 0 0 0 0 0 0 0 0 0
  102199. - 0 0 0 0 0 0 0 0 0 0 0 0
  102200. - 0 0 0 0 0 0 0 0 0 0 0 0
  102201. - 0 0 0 0 0 0 0 0 0 0 0 0
  102202. - 0 0 0 0 0 0 0 0 0 0 0 0
  102203. - 0 0 0 0 0 0 0 0 0 0 0 0
  102204. - 0 0 0 0 0 0 0 0 0 0 0 0
  102205. - 0 0 0 0 0 0 0 0 0 0 0 0
  102206. - 0 0 0 0 0 0 0 0 0 0 0 0
  102207. - 0 0 0 0 0 0 0 0 0 0 0 0
  102208. - 0 0 0 0 0 0 0 0 0 0 0 0
  102209. - 0 0 0 0 0 0 0 0 0 0 0 0
  102210. - 0 0 0 0 0 0 0 0 0 10 10 10
  102211. - 34 34 34 86 86 86 14 14 14 2 2 6
  102212. -121 87 25 192 133 9 219 162 10 239 182 13
  102213. -236 186 11 232 195 16 241 208 19 244 214 54
  102214. -246 218 60 246 218 38 246 215 20 241 208 19
  102215. -241 208 19 226 184 13 121 87 25 2 2 6
  102216. - 2 2 6 2 2 6 2 2 6 2 2 6
  102217. - 50 50 50 82 82 82 34 34 34 10 10 10
  102218. - 0 0 0 0 0 0 0 0 0 0 0 0
  102219. - 0 0 0 0 0 0 0 0 0 0 0 0
  102220. - 0 0 0 0 0 0 0 0 0 0 0 0
  102221. - 0 0 0 0 0 0 0 0 0 0 0 0
  102222. - 0 0 0 0 0 0 0 0 0 0 0 0
  102223. - 0 0 0 0 0 0 0 0 0 0 0 0
  102224. - 0 0 0 0 0 0 0 0 0 0 0 0
  102225. - 0 0 0 0 0 0 0 0 0 0 0 0
  102226. - 0 0 0 0 0 0 0 0 0 0 0 0
  102227. - 0 0 0 0 0 0 0 0 0 0 0 0
  102228. - 0 0 0 0 0 0 0 0 0 0 0 0
  102229. - 0 0 0 0 0 0 0 0 0 0 0 0
  102230. - 0 0 0 0 0 0 0 0 0 10 10 10
  102231. - 34 34 34 82 82 82 30 30 30 61 42 6
  102232. -180 123 7 206 145 10 230 174 11 239 182 13
  102233. -234 190 10 238 202 15 241 208 19 246 218 74
  102234. -246 218 38 246 215 20 246 215 20 246 215 20
  102235. -226 184 13 215 174 15 184 144 12 6 6 6
  102236. - 2 2 6 2 2 6 2 2 6 2 2 6
  102237. - 26 26 26 94 94 94 42 42 42 14 14 14
  102238. - 0 0 0 0 0 0 0 0 0 0 0 0
  102239. - 0 0 0 0 0 0 0 0 0 0 0 0
  102240. - 0 0 0 0 0 0 0 0 0 0 0 0
  102241. - 0 0 0 0 0 0 0 0 0 0 0 0
  102242. - 0 0 0 0 0 0 0 0 0 0 0 0
  102243. - 0 0 0 0 0 0 0 0 0 0 0 0
  102244. - 0 0 0 0 0 0 0 0 0 0 0 0
  102245. - 0 0 0 0 0 0 0 0 0 0 0 0
  102246. - 0 0 0 0 0 0 0 0 0 0 0 0
  102247. - 0 0 0 0 0 0 0 0 0 0 0 0
  102248. - 0 0 0 0 0 0 0 0 0 0 0 0
  102249. - 0 0 0 0 0 0 0 0 0 0 0 0
  102250. - 0 0 0 0 0 0 0 0 0 10 10 10
  102251. - 30 30 30 78 78 78 50 50 50 104 69 6
  102252. -192 133 9 216 158 10 236 178 12 236 186 11
  102253. -232 195 16 241 208 19 244 214 54 245 215 43
  102254. -246 215 20 246 215 20 241 208 19 198 155 10
  102255. -200 144 11 216 158 10 156 118 10 2 2 6
  102256. - 2 2 6 2 2 6 2 2 6 2 2 6
  102257. - 6 6 6 90 90 90 54 54 54 18 18 18
  102258. - 6 6 6 0 0 0 0 0 0 0 0 0
  102259. - 0 0 0 0 0 0 0 0 0 0 0 0
  102260. - 0 0 0 0 0 0 0 0 0 0 0 0
  102261. - 0 0 0 0 0 0 0 0 0 0 0 0
  102262. - 0 0 0 0 0 0 0 0 0 0 0 0
  102263. - 0 0 0 0 0 0 0 0 0 0 0 0
  102264. - 0 0 0 0 0 0 0 0 0 0 0 0
  102265. - 0 0 0 0 0 0 0 0 0 0 0 0
  102266. - 0 0 0 0 0 0 0 0 0 0 0 0
  102267. - 0 0 0 0 0 0 0 0 0 0 0 0
  102268. - 0 0 0 0 0 0 0 0 0 0 0 0
  102269. - 0 0 0 0 0 0 0 0 0 0 0 0
  102270. - 0 0 0 0 0 0 0 0 0 10 10 10
  102271. - 30 30 30 78 78 78 46 46 46 22 22 22
  102272. -137 92 6 210 162 10 239 182 13 238 190 10
  102273. -238 202 15 241 208 19 246 215 20 246 215 20
  102274. -241 208 19 203 166 17 185 133 11 210 150 10
  102275. -216 158 10 210 150 10 102 78 10 2 2 6
  102276. - 6 6 6 54 54 54 14 14 14 2 2 6
  102277. - 2 2 6 62 62 62 74 74 74 30 30 30
  102278. - 10 10 10 0 0 0 0 0 0 0 0 0
  102279. - 0 0 0 0 0 0 0 0 0 0 0 0
  102280. - 0 0 0 0 0 0 0 0 0 0 0 0
  102281. - 0 0 0 0 0 0 0 0 0 0 0 0
  102282. - 0 0 0 0 0 0 0 0 0 0 0 0
  102283. - 0 0 0 0 0 0 0 0 0 0 0 0
  102284. - 0 0 0 0 0 0 0 0 0 0 0 0
  102285. - 0 0 0 0 0 0 0 0 0 0 0 0
  102286. - 0 0 0 0 0 0 0 0 0 0 0 0
  102287. - 0 0 0 0 0 0 0 0 0 0 0 0
  102288. - 0 0 0 0 0 0 0 0 0 0 0 0
  102289. - 0 0 0 0 0 0 0 0 0 0 0 0
  102290. - 0 0 0 0 0 0 0 0 0 10 10 10
  102291. - 34 34 34 78 78 78 50 50 50 6 6 6
  102292. - 94 70 30 139 102 15 190 146 13 226 184 13
  102293. -232 200 30 232 195 16 215 174 15 190 146 13
  102294. -168 122 10 192 133 9 210 150 10 213 154 11
  102295. -202 150 34 182 157 106 101 98 89 2 2 6
  102296. - 2 2 6 78 78 78 116 116 116 58 58 58
  102297. - 2 2 6 22 22 22 90 90 90 46 46 46
  102298. - 18 18 18 6 6 6 0 0 0 0 0 0
  102299. - 0 0 0 0 0 0 0 0 0 0 0 0
  102300. - 0 0 0 0 0 0 0 0 0 0 0 0
  102301. - 0 0 0 0 0 0 0 0 0 0 0 0
  102302. - 0 0 0 0 0 0 0 0 0 0 0 0
  102303. - 0 0 0 0 0 0 0 0 0 0 0 0
  102304. - 0 0 0 0 0 0 0 0 0 0 0 0
  102305. - 0 0 0 0 0 0 0 0 0 0 0 0
  102306. - 0 0 0 0 0 0 0 0 0 0 0 0
  102307. - 0 0 0 0 0 0 0 0 0 0 0 0
  102308. - 0 0 0 0 0 0 0 0 0 0 0 0
  102309. - 0 0 0 0 0 0 0 0 0 0 0 0
  102310. - 0 0 0 0 0 0 0 0 0 10 10 10
  102311. - 38 38 38 86 86 86 50 50 50 6 6 6
  102312. -128 128 128 174 154 114 156 107 11 168 122 10
  102313. -198 155 10 184 144 12 197 138 11 200 144 11
  102314. -206 145 10 206 145 10 197 138 11 188 164 115
  102315. -195 195 195 198 198 198 174 174 174 14 14 14
  102316. - 2 2 6 22 22 22 116 116 116 116 116 116
  102317. - 22 22 22 2 2 6 74 74 74 70 70 70
  102318. - 30 30 30 10 10 10 0 0 0 0 0 0
  102319. - 0 0 0 0 0 0 0 0 0 0 0 0
  102320. - 0 0 0 0 0 0 0 0 0 0 0 0
  102321. - 0 0 0 0 0 0 0 0 0 0 0 0
  102322. - 0 0 0 0 0 0 0 0 0 0 0 0
  102323. - 0 0 0 0 0 0 0 0 0 0 0 0
  102324. - 0 0 0 0 0 0 0 0 0 0 0 0
  102325. - 0 0 0 0 0 0 0 0 0 0 0 0
  102326. - 0 0 0 0 0 0 0 0 0 0 0 0
  102327. - 0 0 0 0 0 0 0 0 0 0 0 0
  102328. - 0 0 0 0 0 0 0 0 0 0 0 0
  102329. - 0 0 0 0 0 0 0 0 0 0 0 0
  102330. - 0 0 0 0 0 0 6 6 6 18 18 18
  102331. - 50 50 50 101 101 101 26 26 26 10 10 10
  102332. -138 138 138 190 190 190 174 154 114 156 107 11
  102333. -197 138 11 200 144 11 197 138 11 192 133 9
  102334. -180 123 7 190 142 34 190 178 144 187 187 187
  102335. -202 202 202 221 221 221 214 214 214 66 66 66
  102336. - 2 2 6 2 2 6 50 50 50 62 62 62
  102337. - 6 6 6 2 2 6 10 10 10 90 90 90
  102338. - 50 50 50 18 18 18 6 6 6 0 0 0
  102339. - 0 0 0 0 0 0 0 0 0 0 0 0
  102340. - 0 0 0 0 0 0 0 0 0 0 0 0
  102341. - 0 0 0 0 0 0 0 0 0 0 0 0
  102342. - 0 0 0 0 0 0 0 0 0 0 0 0
  102343. - 0 0 0 0 0 0 0 0 0 0 0 0
  102344. - 0 0 0 0 0 0 0 0 0 0 0 0
  102345. - 0 0 0 0 0 0 0 0 0 0 0 0
  102346. - 0 0 0 0 0 0 0 0 0 0 0 0
  102347. - 0 0 0 0 0 0 0 0 0 0 0 0
  102348. - 0 0 0 0 0 0 0 0 0 0 0 0
  102349. - 0 0 0 0 0 0 0 0 0 0 0 0
  102350. - 0 0 0 0 0 0 10 10 10 34 34 34
  102351. - 74 74 74 74 74 74 2 2 6 6 6 6
  102352. -144 144 144 198 198 198 190 190 190 178 166 146
  102353. -154 121 60 156 107 11 156 107 11 168 124 44
  102354. -174 154 114 187 187 187 190 190 190 210 210 210
  102355. -246 246 246 253 253 253 253 253 253 182 182 182
  102356. - 6 6 6 2 2 6 2 2 6 2 2 6
  102357. - 2 2 6 2 2 6 2 2 6 62 62 62
  102358. - 74 74 74 34 34 34 14 14 14 0 0 0
  102359. - 0 0 0 0 0 0 0 0 0 0 0 0
  102360. - 0 0 0 0 0 0 0 0 0 0 0 0
  102361. - 0 0 0 0 0 0 0 0 0 0 0 0
  102362. - 0 0 0 0 0 0 0 0 0 0 0 0
  102363. - 0 0 0 0 0 0 0 0 0 0 0 0
  102364. - 0 0 0 0 0 0 0 0 0 0 0 0
  102365. - 0 0 0 0 0 0 0 0 0 0 0 0
  102366. - 0 0 0 0 0 0 0 0 0 0 0 0
  102367. - 0 0 0 0 0 0 0 0 0 0 0 0
  102368. - 0 0 0 0 0 0 0 0 0 0 0 0
  102369. - 0 0 0 0 0 0 0 0 0 0 0 0
  102370. - 0 0 0 10 10 10 22 22 22 54 54 54
  102371. - 94 94 94 18 18 18 2 2 6 46 46 46
  102372. -234 234 234 221 221 221 190 190 190 190 190 190
  102373. -190 190 190 187 187 187 187 187 187 190 190 190
  102374. -190 190 190 195 195 195 214 214 214 242 242 242
  102375. -253 253 253 253 253 253 253 253 253 253 253 253
  102376. - 82 82 82 2 2 6 2 2 6 2 2 6
  102377. - 2 2 6 2 2 6 2 2 6 14 14 14
  102378. - 86 86 86 54 54 54 22 22 22 6 6 6
  102379. - 0 0 0 0 0 0 0 0 0 0 0 0
  102380. - 0 0 0 0 0 0 0 0 0 0 0 0
  102381. - 0 0 0 0 0 0 0 0 0 0 0 0
  102382. - 0 0 0 0 0 0 0 0 0 0 0 0
  102383. - 0 0 0 0 0 0 0 0 0 0 0 0
  102384. - 0 0 0 0 0 0 0 0 0 0 0 0
  102385. - 0 0 0 0 0 0 0 0 0 0 0 0
  102386. - 0 0 0 0 0 0 0 0 0 0 0 0
  102387. - 0 0 0 0 0 0 0 0 0 0 0 0
  102388. - 0 0 0 0 0 0 0 0 0 0 0 0
  102389. - 0 0 0 0 0 0 0 0 0 0 0 0
  102390. - 6 6 6 18 18 18 46 46 46 90 90 90
  102391. - 46 46 46 18 18 18 6 6 6 182 182 182
  102392. -253 253 253 246 246 246 206 206 206 190 190 190
  102393. -190 190 190 190 190 190 190 190 190 190 190 190
  102394. -206 206 206 231 231 231 250 250 250 253 253 253
  102395. -253 253 253 253 253 253 253 253 253 253 253 253
  102396. -202 202 202 14 14 14 2 2 6 2 2 6
  102397. - 2 2 6 2 2 6 2 2 6 2 2 6
  102398. - 42 42 42 86 86 86 42 42 42 18 18 18
  102399. - 6 6 6 0 0 0 0 0 0 0 0 0
  102400. - 0 0 0 0 0 0 0 0 0 0 0 0
  102401. - 0 0 0 0 0 0 0 0 0 0 0 0
  102402. - 0 0 0 0 0 0 0 0 0 0 0 0
  102403. - 0 0 0 0 0 0 0 0 0 0 0 0
  102404. - 0 0 0 0 0 0 0 0 0 0 0 0
  102405. - 0 0 0 0 0 0 0 0 0 0 0 0
  102406. - 0 0 0 0 0 0 0 0 0 0 0 0
  102407. - 0 0 0 0 0 0 0 0 0 0 0 0
  102408. - 0 0 0 0 0 0 0 0 0 0 0 0
  102409. - 0 0 0 0 0 0 0 0 0 6 6 6
  102410. - 14 14 14 38 38 38 74 74 74 66 66 66
  102411. - 2 2 6 6 6 6 90 90 90 250 250 250
  102412. -253 253 253 253 253 253 238 238 238 198 198 198
  102413. -190 190 190 190 190 190 195 195 195 221 221 221
  102414. -246 246 246 253 253 253 253 253 253 253 253 253
  102415. -253 253 253 253 253 253 253 253 253 253 253 253
  102416. -253 253 253 82 82 82 2 2 6 2 2 6
  102417. - 2 2 6 2 2 6 2 2 6 2 2 6
  102418. - 2 2 6 78 78 78 70 70 70 34 34 34
  102419. - 14 14 14 6 6 6 0 0 0 0 0 0
  102420. - 0 0 0 0 0 0 0 0 0 0 0 0
  102421. - 0 0 0 0 0 0 0 0 0 0 0 0
  102422. - 0 0 0 0 0 0 0 0 0 0 0 0
  102423. - 0 0 0 0 0 0 0 0 0 0 0 0
  102424. - 0 0 0 0 0 0 0 0 0 0 0 0
  102425. - 0 0 0 0 0 0 0 0 0 0 0 0
  102426. - 0 0 0 0 0 0 0 0 0 0 0 0
  102427. - 0 0 0 0 0 0 0 0 0 0 0 0
  102428. - 0 0 0 0 0 0 0 0 0 0 0 0
  102429. - 0 0 0 0 0 0 0 0 0 14 14 14
  102430. - 34 34 34 66 66 66 78 78 78 6 6 6
  102431. - 2 2 6 18 18 18 218 218 218 253 253 253
  102432. -253 253 253 253 253 253 253 253 253 246 246 246
  102433. -226 226 226 231 231 231 246 246 246 253 253 253
  102434. -253 253 253 253 253 253 253 253 253 253 253 253
  102435. -253 253 253 253 253 253 253 253 253 253 253 253
  102436. -253 253 253 178 178 178 2 2 6 2 2 6
  102437. - 2 2 6 2 2 6 2 2 6 2 2 6
  102438. - 2 2 6 18 18 18 90 90 90 62 62 62
  102439. - 30 30 30 10 10 10 0 0 0 0 0 0
  102440. - 0 0 0 0 0 0 0 0 0 0 0 0
  102441. - 0 0 0 0 0 0 0 0 0 0 0 0
  102442. - 0 0 0 0 0 0 0 0 0 0 0 0
  102443. - 0 0 0 0 0 0 0 0 0 0 0 0
  102444. - 0 0 0 0 0 0 0 0 0 0 0 0
  102445. - 0 0 0 0 0 0 0 0 0 0 0 0
  102446. - 0 0 0 0 0 0 0 0 0 0 0 0
  102447. - 0 0 0 0 0 0 0 0 0 0 0 0
  102448. - 0 0 0 0 0 0 0 0 0 0 0 0
  102449. - 0 0 0 0 0 0 10 10 10 26 26 26
  102450. - 58 58 58 90 90 90 18 18 18 2 2 6
  102451. - 2 2 6 110 110 110 253 253 253 253 253 253
  102452. -253 253 253 253 253 253 253 253 253 253 253 253
  102453. -250 250 250 253 253 253 253 253 253 253 253 253
  102454. -253 253 253 253 253 253 253 253 253 253 253 253
  102455. -253 253 253 253 253 253 253 253 253 253 253 253
  102456. -253 253 253 231 231 231 18 18 18 2 2 6
  102457. - 2 2 6 2 2 6 2 2 6 2 2 6
  102458. - 2 2 6 2 2 6 18 18 18 94 94 94
  102459. - 54 54 54 26 26 26 10 10 10 0 0 0
  102460. - 0 0 0 0 0 0 0 0 0 0 0 0
  102461. - 0 0 0 0 0 0 0 0 0 0 0 0
  102462. - 0 0 0 0 0 0 0 0 0 0 0 0
  102463. - 0 0 0 0 0 0 0 0 0 0 0 0
  102464. - 0 0 0 0 0 0 0 0 0 0 0 0
  102465. - 0 0 0 0 0 0 0 0 0 0 0 0
  102466. - 0 0 0 0 0 0 0 0 0 0 0 0
  102467. - 0 0 0 0 0 0 0 0 0 0 0 0
  102468. - 0 0 0 0 0 0 0 0 0 0 0 0
  102469. - 0 0 0 6 6 6 22 22 22 50 50 50
  102470. - 90 90 90 26 26 26 2 2 6 2 2 6
  102471. - 14 14 14 195 195 195 250 250 250 253 253 253
  102472. -253 253 253 253 253 253 253 253 253 253 253 253
  102473. -253 253 253 253 253 253 253 253 253 253 253 253
  102474. -253 253 253 253 253 253 253 253 253 253 253 253
  102475. -253 253 253 253 253 253 253 253 253 253 253 253
  102476. -250 250 250 242 242 242 54 54 54 2 2 6
  102477. - 2 2 6 2 2 6 2 2 6 2 2 6
  102478. - 2 2 6 2 2 6 2 2 6 38 38 38
  102479. - 86 86 86 50 50 50 22 22 22 6 6 6
  102480. - 0 0 0 0 0 0 0 0 0 0 0 0
  102481. - 0 0 0 0 0 0 0 0 0 0 0 0
  102482. - 0 0 0 0 0 0 0 0 0 0 0 0
  102483. - 0 0 0 0 0 0 0 0 0 0 0 0
  102484. - 0 0 0 0 0 0 0 0 0 0 0 0
  102485. - 0 0 0 0 0 0 0 0 0 0 0 0
  102486. - 0 0 0 0 0 0 0 0 0 0 0 0
  102487. - 0 0 0 0 0 0 0 0 0 0 0 0
  102488. - 0 0 0 0 0 0 0 0 0 0 0 0
  102489. - 6 6 6 14 14 14 38 38 38 82 82 82
  102490. - 34 34 34 2 2 6 2 2 6 2 2 6
  102491. - 42 42 42 195 195 195 246 246 246 253 253 253
  102492. -253 253 253 253 253 253 253 253 253 250 250 250
  102493. -242 242 242 242 242 242 250 250 250 253 253 253
  102494. -253 253 253 253 253 253 253 253 253 253 253 253
  102495. -253 253 253 250 250 250 246 246 246 238 238 238
  102496. -226 226 226 231 231 231 101 101 101 6 6 6
  102497. - 2 2 6 2 2 6 2 2 6 2 2 6
  102498. - 2 2 6 2 2 6 2 2 6 2 2 6
  102499. - 38 38 38 82 82 82 42 42 42 14 14 14
  102500. - 6 6 6 0 0 0 0 0 0 0 0 0
  102501. - 0 0 0 0 0 0 0 0 0 0 0 0
  102502. - 0 0 0 0 0 0 0 0 0 0 0 0
  102503. - 0 0 0 0 0 0 0 0 0 0 0 0
  102504. - 0 0 0 0 0 0 0 0 0 0 0 0
  102505. - 0 0 0 0 0 0 0 0 0 0 0 0
  102506. - 0 0 0 0 0 0 0 0 0 0 0 0
  102507. - 0 0 0 0 0 0 0 0 0 0 0 0
  102508. - 0 0 0 0 0 0 0 0 0 0 0 0
  102509. - 10 10 10 26 26 26 62 62 62 66 66 66
  102510. - 2 2 6 2 2 6 2 2 6 6 6 6
  102511. - 70 70 70 170 170 170 206 206 206 234 234 234
  102512. -246 246 246 250 250 250 250 250 250 238 238 238
  102513. -226 226 226 231 231 231 238 238 238 250 250 250
  102514. -250 250 250 250 250 250 246 246 246 231 231 231
  102515. -214 214 214 206 206 206 202 202 202 202 202 202
  102516. -198 198 198 202 202 202 182 182 182 18 18 18
  102517. - 2 2 6 2 2 6 2 2 6 2 2 6
  102518. - 2 2 6 2 2 6 2 2 6 2 2 6
  102519. - 2 2 6 62 62 62 66 66 66 30 30 30
  102520. - 10 10 10 0 0 0 0 0 0 0 0 0
  102521. - 0 0 0 0 0 0 0 0 0 0 0 0
  102522. - 0 0 0 0 0 0 0 0 0 0 0 0
  102523. - 0 0 0 0 0 0 0 0 0 0 0 0
  102524. - 0 0 0 0 0 0 0 0 0 0 0 0
  102525. - 0 0 0 0 0 0 0 0 0 0 0 0
  102526. - 0 0 0 0 0 0 0 0 0 0 0 0
  102527. - 0 0 0 0 0 0 0 0 0 0 0 0
  102528. - 0 0 0 0 0 0 0 0 0 0 0 0
  102529. - 14 14 14 42 42 42 82 82 82 18 18 18
  102530. - 2 2 6 2 2 6 2 2 6 10 10 10
  102531. - 94 94 94 182 182 182 218 218 218 242 242 242
  102532. -250 250 250 253 253 253 253 253 253 250 250 250
  102533. -234 234 234 253 253 253 253 253 253 253 253 253
  102534. -253 253 253 253 253 253 253 253 253 246 246 246
  102535. -238 238 238 226 226 226 210 210 210 202 202 202
  102536. -195 195 195 195 195 195 210 210 210 158 158 158
  102537. - 6 6 6 14 14 14 50 50 50 14 14 14
  102538. - 2 2 6 2 2 6 2 2 6 2 2 6
  102539. - 2 2 6 6 6 6 86 86 86 46 46 46
  102540. - 18 18 18 6 6 6 0 0 0 0 0 0
  102541. - 0 0 0 0 0 0 0 0 0 0 0 0
  102542. - 0 0 0 0 0 0 0 0 0 0 0 0
  102543. - 0 0 0 0 0 0 0 0 0 0 0 0
  102544. - 0 0 0 0 0 0 0 0 0 0 0 0
  102545. - 0 0 0 0 0 0 0 0 0 0 0 0
  102546. - 0 0 0 0 0 0 0 0 0 0 0 0
  102547. - 0 0 0 0 0 0 0 0 0 0 0 0
  102548. - 0 0 0 0 0 0 0 0 0 6 6 6
  102549. - 22 22 22 54 54 54 70 70 70 2 2 6
  102550. - 2 2 6 10 10 10 2 2 6 22 22 22
  102551. -166 166 166 231 231 231 250 250 250 253 253 253
  102552. -253 253 253 253 253 253 253 253 253 250 250 250
  102553. -242 242 242 253 253 253 253 253 253 253 253 253
  102554. -253 253 253 253 253 253 253 253 253 253 253 253
  102555. -253 253 253 253 253 253 253 253 253 246 246 246
  102556. -231 231 231 206 206 206 198 198 198 226 226 226
  102557. - 94 94 94 2 2 6 6 6 6 38 38 38
  102558. - 30 30 30 2 2 6 2 2 6 2 2 6
  102559. - 2 2 6 2 2 6 62 62 62 66 66 66
  102560. - 26 26 26 10 10 10 0 0 0 0 0 0
  102561. - 0 0 0 0 0 0 0 0 0 0 0 0
  102562. - 0 0 0 0 0 0 0 0 0 0 0 0
  102563. - 0 0 0 0 0 0 0 0 0 0 0 0
  102564. - 0 0 0 0 0 0 0 0 0 0 0 0
  102565. - 0 0 0 0 0 0 0 0 0 0 0 0
  102566. - 0 0 0 0 0 0 0 0 0 0 0 0
  102567. - 0 0 0 0 0 0 0 0 0 0 0 0
  102568. - 0 0 0 0 0 0 0 0 0 10 10 10
  102569. - 30 30 30 74 74 74 50 50 50 2 2 6
  102570. - 26 26 26 26 26 26 2 2 6 106 106 106
  102571. -238 238 238 253 253 253 253 253 253 253 253 253
  102572. -253 253 253 253 253 253 253 253 253 253 253 253
  102573. -253 253 253 253 253 253 253 253 253 253 253 253
  102574. -253 253 253 253 253 253 253 253 253 253 253 253
  102575. -253 253 253 253 253 253 253 253 253 253 253 253
  102576. -253 253 253 246 246 246 218 218 218 202 202 202
  102577. -210 210 210 14 14 14 2 2 6 2 2 6
  102578. - 30 30 30 22 22 22 2 2 6 2 2 6
  102579. - 2 2 6 2 2 6 18 18 18 86 86 86
  102580. - 42 42 42 14 14 14 0 0 0 0 0 0
  102581. - 0 0 0 0 0 0 0 0 0 0 0 0
  102582. - 0 0 0 0 0 0 0 0 0 0 0 0
  102583. - 0 0 0 0 0 0 0 0 0 0 0 0
  102584. - 0 0 0 0 0 0 0 0 0 0 0 0
  102585. - 0 0 0 0 0 0 0 0 0 0 0 0
  102586. - 0 0 0 0 0 0 0 0 0 0 0 0
  102587. - 0 0 0 0 0 0 0 0 0 0 0 0
  102588. - 0 0 0 0 0 0 0 0 0 14 14 14
  102589. - 42 42 42 90 90 90 22 22 22 2 2 6
  102590. - 42 42 42 2 2 6 18 18 18 218 218 218
  102591. -253 253 253 253 253 253 253 253 253 253 253 253
  102592. -253 253 253 253 253 253 253 253 253 253 253 253
  102593. -253 253 253 253 253 253 253 253 253 253 253 253
  102594. -253 253 253 253 253 253 253 253 253 253 253 253
  102595. -253 253 253 253 253 253 253 253 253 253 253 253
  102596. -253 253 253 253 253 253 250 250 250 221 221 221
  102597. -218 218 218 101 101 101 2 2 6 14 14 14
  102598. - 18 18 18 38 38 38 10 10 10 2 2 6
  102599. - 2 2 6 2 2 6 2 2 6 78 78 78
  102600. - 58 58 58 22 22 22 6 6 6 0 0 0
  102601. - 0 0 0 0 0 0 0 0 0 0 0 0
  102602. - 0 0 0 0 0 0 0 0 0 0 0 0
  102603. - 0 0 0 0 0 0 0 0 0 0 0 0
  102604. - 0 0 0 0 0 0 0 0 0 0 0 0
  102605. - 0 0 0 0 0 0 0 0 0 0 0 0
  102606. - 0 0 0 0 0 0 0 0 0 0 0 0
  102607. - 0 0 0 0 0 0 0 0 0 0 0 0
  102608. - 0 0 0 0 0 0 6 6 6 18 18 18
  102609. - 54 54 54 82 82 82 2 2 6 26 26 26
  102610. - 22 22 22 2 2 6 123 123 123 253 253 253
  102611. -253 253 253 253 253 253 253 253 253 253 253 253
  102612. -253 253 253 253 253 253 253 253 253 253 253 253
  102613. -253 253 253 253 253 253 253 253 253 253 253 253
  102614. -253 253 253 253 253 253 253 253 253 253 253 253
  102615. -253 253 253 253 253 253 253 253 253 253 253 253
  102616. -253 253 253 253 253 253 253 253 253 250 250 250
  102617. -238 238 238 198 198 198 6 6 6 38 38 38
  102618. - 58 58 58 26 26 26 38 38 38 2 2 6
  102619. - 2 2 6 2 2 6 2 2 6 46 46 46
  102620. - 78 78 78 30 30 30 10 10 10 0 0 0
  102621. - 0 0 0 0 0 0 0 0 0 0 0 0
  102622. - 0 0 0 0 0 0 0 0 0 0 0 0
  102623. - 0 0 0 0 0 0 0 0 0 0 0 0
  102624. - 0 0 0 0 0 0 0 0 0 0 0 0
  102625. - 0 0 0 0 0 0 0 0 0 0 0 0
  102626. - 0 0 0 0 0 0 0 0 0 0 0 0
  102627. - 0 0 0 0 0 0 0 0 0 0 0 0
  102628. - 0 0 0 0 0 0 10 10 10 30 30 30
  102629. - 74 74 74 58 58 58 2 2 6 42 42 42
  102630. - 2 2 6 22 22 22 231 231 231 253 253 253
  102631. -253 253 253 253 253 253 253 253 253 253 253 253
  102632. -253 253 253 253 253 253 253 253 253 250 250 250
  102633. -253 253 253 253 253 253 253 253 253 253 253 253
  102634. -253 253 253 253 253 253 253 253 253 253 253 253
  102635. -253 253 253 253 253 253 253 253 253 253 253 253
  102636. -253 253 253 253 253 253 253 253 253 253 253 253
  102637. -253 253 253 246 246 246 46 46 46 38 38 38
  102638. - 42 42 42 14 14 14 38 38 38 14 14 14
  102639. - 2 2 6 2 2 6 2 2 6 6 6 6
  102640. - 86 86 86 46 46 46 14 14 14 0 0 0
  102641. - 0 0 0 0 0 0 0 0 0 0 0 0
  102642. - 0 0 0 0 0 0 0 0 0 0 0 0
  102643. - 0 0 0 0 0 0 0 0 0 0 0 0
  102644. - 0 0 0 0 0 0 0 0 0 0 0 0
  102645. - 0 0 0 0 0 0 0 0 0 0 0 0
  102646. - 0 0 0 0 0 0 0 0 0 0 0 0
  102647. - 0 0 0 0 0 0 0 0 0 0 0 0
  102648. - 0 0 0 6 6 6 14 14 14 42 42 42
  102649. - 90 90 90 18 18 18 18 18 18 26 26 26
  102650. - 2 2 6 116 116 116 253 253 253 253 253 253
  102651. -253 253 253 253 253 253 253 253 253 253 253 253
  102652. -253 253 253 253 253 253 250 250 250 238 238 238
  102653. -253 253 253 253 253 253 253 253 253 253 253 253
  102654. -253 253 253 253 253 253 253 253 253 253 253 253
  102655. -253 253 253 253 253 253 253 253 253 253 253 253
  102656. -253 253 253 253 253 253 253 253 253 253 253 253
  102657. -253 253 253 253 253 253 94 94 94 6 6 6
  102658. - 2 2 6 2 2 6 10 10 10 34 34 34
  102659. - 2 2 6 2 2 6 2 2 6 2 2 6
  102660. - 74 74 74 58 58 58 22 22 22 6 6 6
  102661. - 0 0 0 0 0 0 0 0 0 0 0 0
  102662. - 0 0 0 0 0 0 0 0 0 0 0 0
  102663. - 0 0 0 0 0 0 0 0 0 0 0 0
  102664. - 0 0 0 0 0 0 0 0 0 0 0 0
  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 10 10 10 26 26 26 66 66 66
  102669. - 82 82 82 2 2 6 38 38 38 6 6 6
  102670. - 14 14 14 210 210 210 253 253 253 253 253 253
  102671. -253 253 253 253 253 253 253 253 253 253 253 253
  102672. -253 253 253 253 253 253 246 246 246 242 242 242
  102673. -253 253 253 253 253 253 253 253 253 253 253 253
  102674. -253 253 253 253 253 253 253 253 253 253 253 253
  102675. -253 253 253 253 253 253 253 253 253 253 253 253
  102676. -253 253 253 253 253 253 253 253 253 253 253 253
  102677. -253 253 253 253 253 253 144 144 144 2 2 6
  102678. - 2 2 6 2 2 6 2 2 6 46 46 46
  102679. - 2 2 6 2 2 6 2 2 6 2 2 6
  102680. - 42 42 42 74 74 74 30 30 30 10 10 10
  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. - 6 6 6 14 14 14 42 42 42 90 90 90
  102689. - 26 26 26 6 6 6 42 42 42 2 2 6
  102690. - 74 74 74 250 250 250 253 253 253 253 253 253
  102691. -253 253 253 253 253 253 253 253 253 253 253 253
  102692. -253 253 253 253 253 253 242 242 242 242 242 242
  102693. -253 253 253 253 253 253 253 253 253 253 253 253
  102694. -253 253 253 253 253 253 253 253 253 253 253 253
  102695. -253 253 253 253 253 253 253 253 253 253 253 253
  102696. -253 253 253 253 253 253 253 253 253 253 253 253
  102697. -253 253 253 253 253 253 182 182 182 2 2 6
  102698. - 2 2 6 2 2 6 2 2 6 46 46 46
  102699. - 2 2 6 2 2 6 2 2 6 2 2 6
  102700. - 10 10 10 86 86 86 38 38 38 10 10 10
  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 0 0 0 0 0 0 0
  102707. - 0 0 0 0 0 0 0 0 0 0 0 0
  102708. - 10 10 10 26 26 26 66 66 66 82 82 82
  102709. - 2 2 6 22 22 22 18 18 18 2 2 6
  102710. -149 149 149 253 253 253 253 253 253 253 253 253
  102711. -253 253 253 253 253 253 253 253 253 253 253 253
  102712. -253 253 253 253 253 253 234 234 234 242 242 242
  102713. -253 253 253 253 253 253 253 253 253 253 253 253
  102714. -253 253 253 253 253 253 253 253 253 253 253 253
  102715. -253 253 253 253 253 253 253 253 253 253 253 253
  102716. -253 253 253 253 253 253 253 253 253 253 253 253
  102717. -253 253 253 253 253 253 206 206 206 2 2 6
  102718. - 2 2 6 2 2 6 2 2 6 38 38 38
  102719. - 2 2 6 2 2 6 2 2 6 2 2 6
  102720. - 6 6 6 86 86 86 46 46 46 14 14 14
  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 0 0 0 0 0 0 0 0 0 0
  102727. - 0 0 0 0 0 0 0 0 0 6 6 6
  102728. - 18 18 18 46 46 46 86 86 86 18 18 18
  102729. - 2 2 6 34 34 34 10 10 10 6 6 6
  102730. -210 210 210 253 253 253 253 253 253 253 253 253
  102731. -253 253 253 253 253 253 253 253 253 253 253 253
  102732. -253 253 253 253 253 253 234 234 234 242 242 242
  102733. -253 253 253 253 253 253 253 253 253 253 253 253
  102734. -253 253 253 253 253 253 253 253 253 253 253 253
  102735. -253 253 253 253 253 253 253 253 253 253 253 253
  102736. -253 253 253 253 253 253 253 253 253 253 253 253
  102737. -253 253 253 253 253 253 221 221 221 6 6 6
  102738. - 2 2 6 2 2 6 6 6 6 30 30 30
  102739. - 2 2 6 2 2 6 2 2 6 2 2 6
  102740. - 2 2 6 82 82 82 54 54 54 18 18 18
  102741. - 6 6 6 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 0 0 0 0 0 0 0 0 0 0
  102747. - 0 0 0 0 0 0 0 0 0 10 10 10
  102748. - 26 26 26 66 66 66 62 62 62 2 2 6
  102749. - 2 2 6 38 38 38 10 10 10 26 26 26
  102750. -238 238 238 253 253 253 253 253 253 253 253 253
  102751. -253 253 253 253 253 253 253 253 253 253 253 253
  102752. -253 253 253 253 253 253 231 231 231 238 238 238
  102753. -253 253 253 253 253 253 253 253 253 253 253 253
  102754. -253 253 253 253 253 253 253 253 253 253 253 253
  102755. -253 253 253 253 253 253 253 253 253 253 253 253
  102756. -253 253 253 253 253 253 253 253 253 253 253 253
  102757. -253 253 253 253 253 253 231 231 231 6 6 6
  102758. - 2 2 6 2 2 6 10 10 10 30 30 30
  102759. - 2 2 6 2 2 6 2 2 6 2 2 6
  102760. - 2 2 6 66 66 66 58 58 58 22 22 22
  102761. - 6 6 6 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 10 10 10
  102768. - 38 38 38 78 78 78 6 6 6 2 2 6
  102769. - 2 2 6 46 46 46 14 14 14 42 42 42
  102770. -246 246 246 253 253 253 253 253 253 253 253 253
  102771. -253 253 253 253 253 253 253 253 253 253 253 253
  102772. -253 253 253 253 253 253 231 231 231 242 242 242
  102773. -253 253 253 253 253 253 253 253 253 253 253 253
  102774. -253 253 253 253 253 253 253 253 253 253 253 253
  102775. -253 253 253 253 253 253 253 253 253 253 253 253
  102776. -253 253 253 253 253 253 253 253 253 253 253 253
  102777. -253 253 253 253 253 253 234 234 234 10 10 10
  102778. - 2 2 6 2 2 6 22 22 22 14 14 14
  102779. - 2 2 6 2 2 6 2 2 6 2 2 6
  102780. - 2 2 6 66 66 66 62 62 62 22 22 22
  102781. - 6 6 6 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 0 0 0 0 0 0 0 0 0 0
  102787. - 0 0 0 0 0 0 6 6 6 18 18 18
  102788. - 50 50 50 74 74 74 2 2 6 2 2 6
  102789. - 14 14 14 70 70 70 34 34 34 62 62 62
  102790. -250 250 250 253 253 253 253 253 253 253 253 253
  102791. -253 253 253 253 253 253 253 253 253 253 253 253
  102792. -253 253 253 253 253 253 231 231 231 246 246 246
  102793. -253 253 253 253 253 253 253 253 253 253 253 253
  102794. -253 253 253 253 253 253 253 253 253 253 253 253
  102795. -253 253 253 253 253 253 253 253 253 253 253 253
  102796. -253 253 253 253 253 253 253 253 253 253 253 253
  102797. -253 253 253 253 253 253 234 234 234 14 14 14
  102798. - 2 2 6 2 2 6 30 30 30 2 2 6
  102799. - 2 2 6 2 2 6 2 2 6 2 2 6
  102800. - 2 2 6 66 66 66 62 62 62 22 22 22
  102801. - 6 6 6 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 0 0 0 0 0 0 0 0 0 0
  102807. - 0 0 0 0 0 0 6 6 6 18 18 18
  102808. - 54 54 54 62 62 62 2 2 6 2 2 6
  102809. - 2 2 6 30 30 30 46 46 46 70 70 70
  102810. -250 250 250 253 253 253 253 253 253 253 253 253
  102811. -253 253 253 253 253 253 253 253 253 253 253 253
  102812. -253 253 253 253 253 253 231 231 231 246 246 246
  102813. -253 253 253 253 253 253 253 253 253 253 253 253
  102814. -253 253 253 253 253 253 253 253 253 253 253 253
  102815. -253 253 253 253 253 253 253 253 253 253 253 253
  102816. -253 253 253 253 253 253 253 253 253 253 253 253
  102817. -253 253 253 253 253 253 226 226 226 10 10 10
  102818. - 2 2 6 6 6 6 30 30 30 2 2 6
  102819. - 2 2 6 2 2 6 2 2 6 2 2 6
  102820. - 2 2 6 66 66 66 58 58 58 22 22 22
  102821. - 6 6 6 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 0 0 0 0 0 0 0 0 0 0
  102827. - 0 0 0 0 0 0 6 6 6 22 22 22
  102828. - 58 58 58 62 62 62 2 2 6 2 2 6
  102829. - 2 2 6 2 2 6 30 30 30 78 78 78
  102830. -250 250 250 253 253 253 253 253 253 253 253 253
  102831. -253 253 253 253 253 253 253 253 253 253 253 253
  102832. -253 253 253 253 253 253 231 231 231 246 246 246
  102833. -253 253 253 253 253 253 253 253 253 253 253 253
  102834. -253 253 253 253 253 253 253 253 253 253 253 253
  102835. -253 253 253 253 253 253 253 253 253 253 253 253
  102836. -253 253 253 253 253 253 253 253 253 253 253 253
  102837. -253 253 253 253 253 253 206 206 206 2 2 6
  102838. - 22 22 22 34 34 34 18 14 6 22 22 22
  102839. - 26 26 26 18 18 18 6 6 6 2 2 6
  102840. - 2 2 6 82 82 82 54 54 54 18 18 18
  102841. - 6 6 6 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 6 6 6 26 26 26
  102848. - 62 62 62 106 106 106 74 54 14 185 133 11
  102849. -210 162 10 121 92 8 6 6 6 62 62 62
  102850. -238 238 238 253 253 253 253 253 253 253 253 253
  102851. -253 253 253 253 253 253 253 253 253 253 253 253
  102852. -253 253 253 253 253 253 231 231 231 246 246 246
  102853. -253 253 253 253 253 253 253 253 253 253 253 253
  102854. -253 253 253 253 253 253 253 253 253 253 253 253
  102855. -253 253 253 253 253 253 253 253 253 253 253 253
  102856. -253 253 253 253 253 253 253 253 253 253 253 253
  102857. -253 253 253 253 253 253 158 158 158 18 18 18
  102858. - 14 14 14 2 2 6 2 2 6 2 2 6
  102859. - 6 6 6 18 18 18 66 66 66 38 38 38
  102860. - 6 6 6 94 94 94 50 50 50 18 18 18
  102861. - 6 6 6 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 0 0 0 0 6 6 6
  102867. - 10 10 10 10 10 10 18 18 18 38 38 38
  102868. - 78 78 78 142 134 106 216 158 10 242 186 14
  102869. -246 190 14 246 190 14 156 118 10 10 10 10
  102870. - 90 90 90 238 238 238 253 253 253 253 253 253
  102871. -253 253 253 253 253 253 253 253 253 253 253 253
  102872. -253 253 253 253 253 253 231 231 231 250 250 250
  102873. -253 253 253 253 253 253 253 253 253 253 253 253
  102874. -253 253 253 253 253 253 253 253 253 253 253 253
  102875. -253 253 253 253 253 253 253 253 253 253 253 253
  102876. -253 253 253 253 253 253 253 253 253 246 230 190
  102877. -238 204 91 238 204 91 181 142 44 37 26 9
  102878. - 2 2 6 2 2 6 2 2 6 2 2 6
  102879. - 2 2 6 2 2 6 38 38 38 46 46 46
  102880. - 26 26 26 106 106 106 54 54 54 18 18 18
  102881. - 6 6 6 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 0 6 6 6 14 14 14 22 22 22
  102887. - 30 30 30 38 38 38 50 50 50 70 70 70
  102888. -106 106 106 190 142 34 226 170 11 242 186 14
  102889. -246 190 14 246 190 14 246 190 14 154 114 10
  102890. - 6 6 6 74 74 74 226 226 226 253 253 253
  102891. -253 253 253 253 253 253 253 253 253 253 253 253
  102892. -253 253 253 253 253 253 231 231 231 250 250 250
  102893. -253 253 253 253 253 253 253 253 253 253 253 253
  102894. -253 253 253 253 253 253 253 253 253 253 253 253
  102895. -253 253 253 253 253 253 253 253 253 253 253 253
  102896. -253 253 253 253 253 253 253 253 253 228 184 62
  102897. -241 196 14 241 208 19 232 195 16 38 30 10
  102898. - 2 2 6 2 2 6 2 2 6 2 2 6
  102899. - 2 2 6 6 6 6 30 30 30 26 26 26
  102900. -203 166 17 154 142 90 66 66 66 26 26 26
  102901. - 6 6 6 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. - 6 6 6 18 18 18 38 38 38 58 58 58
  102907. - 78 78 78 86 86 86 101 101 101 123 123 123
  102908. -175 146 61 210 150 10 234 174 13 246 186 14
  102909. -246 190 14 246 190 14 246 190 14 238 190 10
  102910. -102 78 10 2 2 6 46 46 46 198 198 198
  102911. -253 253 253 253 253 253 253 253 253 253 253 253
  102912. -253 253 253 253 253 253 234 234 234 242 242 242
  102913. -253 253 253 253 253 253 253 253 253 253 253 253
  102914. -253 253 253 253 253 253 253 253 253 253 253 253
  102915. -253 253 253 253 253 253 253 253 253 253 253 253
  102916. -253 253 253 253 253 253 253 253 253 224 178 62
  102917. -242 186 14 241 196 14 210 166 10 22 18 6
  102918. - 2 2 6 2 2 6 2 2 6 2 2 6
  102919. - 2 2 6 2 2 6 6 6 6 121 92 8
  102920. -238 202 15 232 195 16 82 82 82 34 34 34
  102921. - 10 10 10 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. - 14 14 14 38 38 38 70 70 70 154 122 46
  102927. -190 142 34 200 144 11 197 138 11 197 138 11
  102928. -213 154 11 226 170 11 242 186 14 246 190 14
  102929. -246 190 14 246 190 14 246 190 14 246 190 14
  102930. -225 175 15 46 32 6 2 2 6 22 22 22
  102931. -158 158 158 250 250 250 253 253 253 253 253 253
  102932. -253 253 253 253 253 253 253 253 253 253 253 253
  102933. -253 253 253 253 253 253 253 253 253 253 253 253
  102934. -253 253 253 253 253 253 253 253 253 253 253 253
  102935. -253 253 253 253 253 253 253 253 253 253 253 253
  102936. -253 253 253 250 250 250 242 242 242 224 178 62
  102937. -239 182 13 236 186 11 213 154 11 46 32 6
  102938. - 2 2 6 2 2 6 2 2 6 2 2 6
  102939. - 2 2 6 2 2 6 61 42 6 225 175 15
  102940. -238 190 10 236 186 11 112 100 78 42 42 42
  102941. - 14 14 14 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 6 6 6
  102946. - 22 22 22 54 54 54 154 122 46 213 154 11
  102947. -226 170 11 230 174 11 226 170 11 226 170 11
  102948. -236 178 12 242 186 14 246 190 14 246 190 14
  102949. -246 190 14 246 190 14 246 190 14 246 190 14
  102950. -241 196 14 184 144 12 10 10 10 2 2 6
  102951. - 6 6 6 116 116 116 242 242 242 253 253 253
  102952. -253 253 253 253 253 253 253 253 253 253 253 253
  102953. -253 253 253 253 253 253 253 253 253 253 253 253
  102954. -253 253 253 253 253 253 253 253 253 253 253 253
  102955. -253 253 253 253 253 253 253 253 253 253 253 253
  102956. -253 253 253 231 231 231 198 198 198 214 170 54
  102957. -236 178 12 236 178 12 210 150 10 137 92 6
  102958. - 18 14 6 2 2 6 2 2 6 2 2 6
  102959. - 6 6 6 70 47 6 200 144 11 236 178 12
  102960. -239 182 13 239 182 13 124 112 88 58 58 58
  102961. - 22 22 22 6 6 6 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 10 10 10
  102966. - 30 30 30 70 70 70 180 133 36 226 170 11
  102967. -239 182 13 242 186 14 242 186 14 246 186 14
  102968. -246 190 14 246 190 14 246 190 14 246 190 14
  102969. -246 190 14 246 190 14 246 190 14 246 190 14
  102970. -246 190 14 232 195 16 98 70 6 2 2 6
  102971. - 2 2 6 2 2 6 66 66 66 221 221 221
  102972. -253 253 253 253 253 253 253 253 253 253 253 253
  102973. -253 253 253 253 253 253 253 253 253 253 253 253
  102974. -253 253 253 253 253 253 253 253 253 253 253 253
  102975. -253 253 253 253 253 253 253 253 253 253 253 253
  102976. -253 253 253 206 206 206 198 198 198 214 166 58
  102977. -230 174 11 230 174 11 216 158 10 192 133 9
  102978. -163 110 8 116 81 8 102 78 10 116 81 8
  102979. -167 114 7 197 138 11 226 170 11 239 182 13
  102980. -242 186 14 242 186 14 162 146 94 78 78 78
  102981. - 34 34 34 14 14 14 6 6 6 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 0 6 6 6
  102986. - 30 30 30 78 78 78 190 142 34 226 170 11
  102987. -239 182 13 246 190 14 246 190 14 246 190 14
  102988. -246 190 14 246 190 14 246 190 14 246 190 14
  102989. -246 190 14 246 190 14 246 190 14 246 190 14
  102990. -246 190 14 241 196 14 203 166 17 22 18 6
  102991. - 2 2 6 2 2 6 2 2 6 38 38 38
  102992. -218 218 218 253 253 253 253 253 253 253 253 253
  102993. -253 253 253 253 253 253 253 253 253 253 253 253
  102994. -253 253 253 253 253 253 253 253 253 253 253 253
  102995. -253 253 253 253 253 253 253 253 253 253 253 253
  102996. -250 250 250 206 206 206 198 198 198 202 162 69
  102997. -226 170 11 236 178 12 224 166 10 210 150 10
  102998. -200 144 11 197 138 11 192 133 9 197 138 11
  102999. -210 150 10 226 170 11 242 186 14 246 190 14
  103000. -246 190 14 246 186 14 225 175 15 124 112 88
  103001. - 62 62 62 30 30 30 14 14 14 6 6 6
  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 10 10 10
  103006. - 30 30 30 78 78 78 174 135 50 224 166 10
  103007. -239 182 13 246 190 14 246 190 14 246 190 14
  103008. -246 190 14 246 190 14 246 190 14 246 190 14
  103009. -246 190 14 246 190 14 246 190 14 246 190 14
  103010. -246 190 14 246 190 14 241 196 14 139 102 15
  103011. - 2 2 6 2 2 6 2 2 6 2 2 6
  103012. - 78 78 78 250 250 250 253 253 253 253 253 253
  103013. -253 253 253 253 253 253 253 253 253 253 253 253
  103014. -253 253 253 253 253 253 253 253 253 253 253 253
  103015. -253 253 253 253 253 253 253 253 253 253 253 253
  103016. -250 250 250 214 214 214 198 198 198 190 150 46
  103017. -219 162 10 236 178 12 234 174 13 224 166 10
  103018. -216 158 10 213 154 11 213 154 11 216 158 10
  103019. -226 170 11 239 182 13 246 190 14 246 190 14
  103020. -246 190 14 246 190 14 242 186 14 206 162 42
  103021. -101 101 101 58 58 58 30 30 30 14 14 14
  103022. - 6 6 6 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 0 10 10 10
  103026. - 30 30 30 74 74 74 174 135 50 216 158 10
  103027. -236 178 12 246 190 14 246 190 14 246 190 14
  103028. -246 190 14 246 190 14 246 190 14 246 190 14
  103029. -246 190 14 246 190 14 246 190 14 246 190 14
  103030. -246 190 14 246 190 14 241 196 14 226 184 13
  103031. - 61 42 6 2 2 6 2 2 6 2 2 6
  103032. - 22 22 22 238 238 238 253 253 253 253 253 253
  103033. -253 253 253 253 253 253 253 253 253 253 253 253
  103034. -253 253 253 253 253 253 253 253 253 253 253 253
  103035. -253 253 253 253 253 253 253 253 253 253 253 253
  103036. -253 253 253 226 226 226 187 187 187 180 133 36
  103037. -216 158 10 236 178 12 239 182 13 236 178 12
  103038. -230 174 11 226 170 11 226 170 11 230 174 11
  103039. -236 178 12 242 186 14 246 190 14 246 190 14
  103040. -246 190 14 246 190 14 246 186 14 239 182 13
  103041. -206 162 42 106 106 106 66 66 66 34 34 34
  103042. - 14 14 14 6 6 6 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 0 6 6 6
  103046. - 26 26 26 70 70 70 163 133 67 213 154 11
  103047. -236 178 12 246 190 14 246 190 14 246 190 14
  103048. -246 190 14 246 190 14 246 190 14 246 190 14
  103049. -246 190 14 246 190 14 246 190 14 246 190 14
  103050. -246 190 14 246 190 14 246 190 14 241 196 14
  103051. -190 146 13 18 14 6 2 2 6 2 2 6
  103052. - 46 46 46 246 246 246 253 253 253 253 253 253
  103053. -253 253 253 253 253 253 253 253 253 253 253 253
  103054. -253 253 253 253 253 253 253 253 253 253 253 253
  103055. -253 253 253 253 253 253 253 253 253 253 253 253
  103056. -253 253 253 221 221 221 86 86 86 156 107 11
  103057. -216 158 10 236 178 12 242 186 14 246 186 14
  103058. -242 186 14 239 182 13 239 182 13 242 186 14
  103059. -242 186 14 246 186 14 246 190 14 246 190 14
  103060. -246 190 14 246 190 14 246 190 14 246 190 14
  103061. -242 186 14 225 175 15 142 122 72 66 66 66
  103062. - 30 30 30 10 10 10 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 0 6 6 6
  103066. - 26 26 26 70 70 70 163 133 67 210 150 10
  103067. -236 178 12 246 190 14 246 190 14 246 190 14
  103068. -246 190 14 246 190 14 246 190 14 246 190 14
  103069. -246 190 14 246 190 14 246 190 14 246 190 14
  103070. -246 190 14 246 190 14 246 190 14 246 190 14
  103071. -232 195 16 121 92 8 34 34 34 106 106 106
  103072. -221 221 221 253 253 253 253 253 253 253 253 253
  103073. -253 253 253 253 253 253 253 253 253 253 253 253
  103074. -253 253 253 253 253 253 253 253 253 253 253 253
  103075. -253 253 253 253 253 253 253 253 253 253 253 253
  103076. -242 242 242 82 82 82 18 14 6 163 110 8
  103077. -216 158 10 236 178 12 242 186 14 246 190 14
  103078. -246 190 14 246 190 14 246 190 14 246 190 14
  103079. -246 190 14 246 190 14 246 190 14 246 190 14
  103080. -246 190 14 246 190 14 246 190 14 246 190 14
  103081. -246 190 14 246 190 14 242 186 14 163 133 67
  103082. - 46 46 46 18 18 18 6 6 6 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 10 10 10
  103086. - 30 30 30 78 78 78 163 133 67 210 150 10
  103087. -236 178 12 246 186 14 246 190 14 246 190 14
  103088. -246 190 14 246 190 14 246 190 14 246 190 14
  103089. -246 190 14 246 190 14 246 190 14 246 190 14
  103090. -246 190 14 246 190 14 246 190 14 246 190 14
  103091. -241 196 14 215 174 15 190 178 144 253 253 253
  103092. -253 253 253 253 253 253 253 253 253 253 253 253
  103093. -253 253 253 253 253 253 253 253 253 253 253 253
  103094. -253 253 253 253 253 253 253 253 253 253 253 253
  103095. -253 253 253 253 253 253 253 253 253 218 218 218
  103096. - 58 58 58 2 2 6 22 18 6 167 114 7
  103097. -216 158 10 236 178 12 246 186 14 246 190 14
  103098. -246 190 14 246 190 14 246 190 14 246 190 14
  103099. -246 190 14 246 190 14 246 190 14 246 190 14
  103100. -246 190 14 246 190 14 246 190 14 246 190 14
  103101. -246 190 14 246 186 14 242 186 14 190 150 46
  103102. - 54 54 54 22 22 22 6 6 6 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 14 14 14
  103106. - 38 38 38 86 86 86 180 133 36 213 154 11
  103107. -236 178 12 246 186 14 246 190 14 246 190 14
  103108. -246 190 14 246 190 14 246 190 14 246 190 14
  103109. -246 190 14 246 190 14 246 190 14 246 190 14
  103110. -246 190 14 246 190 14 246 190 14 246 190 14
  103111. -246 190 14 232 195 16 190 146 13 214 214 214
  103112. -253 253 253 253 253 253 253 253 253 253 253 253
  103113. -253 253 253 253 253 253 253 253 253 253 253 253
  103114. -253 253 253 253 253 253 253 253 253 253 253 253
  103115. -253 253 253 250 250 250 170 170 170 26 26 26
  103116. - 2 2 6 2 2 6 37 26 9 163 110 8
  103117. -219 162 10 239 182 13 246 186 14 246 190 14
  103118. -246 190 14 246 190 14 246 190 14 246 190 14
  103119. -246 190 14 246 190 14 246 190 14 246 190 14
  103120. -246 190 14 246 190 14 246 190 14 246 190 14
  103121. -246 186 14 236 178 12 224 166 10 142 122 72
  103122. - 46 46 46 18 18 18 6 6 6 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 6 6 6 18 18 18
  103126. - 50 50 50 109 106 95 192 133 9 224 166 10
  103127. -242 186 14 246 190 14 246 190 14 246 190 14
  103128. -246 190 14 246 190 14 246 190 14 246 190 14
  103129. -246 190 14 246 190 14 246 190 14 246 190 14
  103130. -246 190 14 246 190 14 246 190 14 246 190 14
  103131. -242 186 14 226 184 13 210 162 10 142 110 46
  103132. -226 226 226 253 253 253 253 253 253 253 253 253
  103133. -253 253 253 253 253 253 253 253 253 253 253 253
  103134. -253 253 253 253 253 253 253 253 253 253 253 253
  103135. -198 198 198 66 66 66 2 2 6 2 2 6
  103136. - 2 2 6 2 2 6 50 34 6 156 107 11
  103137. -219 162 10 239 182 13 246 186 14 246 190 14
  103138. -246 190 14 246 190 14 246 190 14 246 190 14
  103139. -246 190 14 246 190 14 246 190 14 246 190 14
  103140. -246 190 14 246 190 14 246 190 14 242 186 14
  103141. -234 174 13 213 154 11 154 122 46 66 66 66
  103142. - 30 30 30 10 10 10 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 6 6 6 22 22 22
  103146. - 58 58 58 154 121 60 206 145 10 234 174 13
  103147. -242 186 14 246 186 14 246 190 14 246 190 14
  103148. -246 190 14 246 190 14 246 190 14 246 190 14
  103149. -246 190 14 246 190 14 246 190 14 246 190 14
  103150. -246 190 14 246 190 14 246 190 14 246 190 14
  103151. -246 186 14 236 178 12 210 162 10 163 110 8
  103152. - 61 42 6 138 138 138 218 218 218 250 250 250
  103153. -253 253 253 253 253 253 253 253 253 250 250 250
  103154. -242 242 242 210 210 210 144 144 144 66 66 66
  103155. - 6 6 6 2 2 6 2 2 6 2 2 6
  103156. - 2 2 6 2 2 6 61 42 6 163 110 8
  103157. -216 158 10 236 178 12 246 190 14 246 190 14
  103158. -246 190 14 246 190 14 246 190 14 246 190 14
  103159. -246 190 14 246 190 14 246 190 14 246 190 14
  103160. -246 190 14 239 182 13 230 174 11 216 158 10
  103161. -190 142 34 124 112 88 70 70 70 38 38 38
  103162. - 18 18 18 6 6 6 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 6 6 6 22 22 22
  103166. - 62 62 62 168 124 44 206 145 10 224 166 10
  103167. -236 178 12 239 182 13 242 186 14 242 186 14
  103168. -246 186 14 246 190 14 246 190 14 246 190 14
  103169. -246 190 14 246 190 14 246 190 14 246 190 14
  103170. -246 190 14 246 190 14 246 190 14 246 190 14
  103171. -246 190 14 236 178 12 216 158 10 175 118 6
  103172. - 80 54 7 2 2 6 6 6 6 30 30 30
  103173. - 54 54 54 62 62 62 50 50 50 38 38 38
  103174. - 14 14 14 2 2 6 2 2 6 2 2 6
  103175. - 2 2 6 2 2 6 2 2 6 2 2 6
  103176. - 2 2 6 6 6 6 80 54 7 167 114 7
  103177. -213 154 11 236 178 12 246 190 14 246 190 14
  103178. -246 190 14 246 190 14 246 190 14 246 190 14
  103179. -246 190 14 242 186 14 239 182 13 239 182 13
  103180. -230 174 11 210 150 10 174 135 50 124 112 88
  103181. - 82 82 82 54 54 54 34 34 34 18 18 18
  103182. - 6 6 6 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 6 6 6 18 18 18
  103186. - 50 50 50 158 118 36 192 133 9 200 144 11
  103187. -216 158 10 219 162 10 224 166 10 226 170 11
  103188. -230 174 11 236 178 12 239 182 13 239 182 13
  103189. -242 186 14 246 186 14 246 190 14 246 190 14
  103190. -246 190 14 246 190 14 246 190 14 246 190 14
  103191. -246 186 14 230 174 11 210 150 10 163 110 8
  103192. -104 69 6 10 10 10 2 2 6 2 2 6
  103193. - 2 2 6 2 2 6 2 2 6 2 2 6
  103194. - 2 2 6 2 2 6 2 2 6 2 2 6
  103195. - 2 2 6 2 2 6 2 2 6 2 2 6
  103196. - 2 2 6 6 6 6 91 60 6 167 114 7
  103197. -206 145 10 230 174 11 242 186 14 246 190 14
  103198. -246 190 14 246 190 14 246 186 14 242 186 14
  103199. -239 182 13 230 174 11 224 166 10 213 154 11
  103200. -180 133 36 124 112 88 86 86 86 58 58 58
  103201. - 38 38 38 22 22 22 10 10 10 6 6 6
  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 14 14 14
  103206. - 34 34 34 70 70 70 138 110 50 158 118 36
  103207. -167 114 7 180 123 7 192 133 9 197 138 11
  103208. -200 144 11 206 145 10 213 154 11 219 162 10
  103209. -224 166 10 230 174 11 239 182 13 242 186 14
  103210. -246 186 14 246 186 14 246 186 14 246 186 14
  103211. -239 182 13 216 158 10 185 133 11 152 99 6
  103212. -104 69 6 18 14 6 2 2 6 2 2 6
  103213. - 2 2 6 2 2 6 2 2 6 2 2 6
  103214. - 2 2 6 2 2 6 2 2 6 2 2 6
  103215. - 2 2 6 2 2 6 2 2 6 2 2 6
  103216. - 2 2 6 6 6 6 80 54 7 152 99 6
  103217. -192 133 9 219 162 10 236 178 12 239 182 13
  103218. -246 186 14 242 186 14 239 182 13 236 178 12
  103219. -224 166 10 206 145 10 192 133 9 154 121 60
  103220. - 94 94 94 62 62 62 42 42 42 22 22 22
  103221. - 14 14 14 6 6 6 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 6 6 6
  103226. - 18 18 18 34 34 34 58 58 58 78 78 78
  103227. -101 98 89 124 112 88 142 110 46 156 107 11
  103228. -163 110 8 167 114 7 175 118 6 180 123 7
  103229. -185 133 11 197 138 11 210 150 10 219 162 10
  103230. -226 170 11 236 178 12 236 178 12 234 174 13
  103231. -219 162 10 197 138 11 163 110 8 130 83 6
  103232. - 91 60 6 10 10 10 2 2 6 2 2 6
  103233. - 18 18 18 38 38 38 38 38 38 38 38 38
  103234. - 38 38 38 38 38 38 38 38 38 38 38 38
  103235. - 38 38 38 38 38 38 26 26 26 2 2 6
  103236. - 2 2 6 6 6 6 70 47 6 137 92 6
  103237. -175 118 6 200 144 11 219 162 10 230 174 11
  103238. -234 174 13 230 174 11 219 162 10 210 150 10
  103239. -192 133 9 163 110 8 124 112 88 82 82 82
  103240. - 50 50 50 30 30 30 14 14 14 6 6 6
  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. - 6 6 6 14 14 14 22 22 22 34 34 34
  103247. - 42 42 42 58 58 58 74 74 74 86 86 86
  103248. -101 98 89 122 102 70 130 98 46 121 87 25
  103249. -137 92 6 152 99 6 163 110 8 180 123 7
  103250. -185 133 11 197 138 11 206 145 10 200 144 11
  103251. -180 123 7 156 107 11 130 83 6 104 69 6
  103252. - 50 34 6 54 54 54 110 110 110 101 98 89
  103253. - 86 86 86 82 82 82 78 78 78 78 78 78
  103254. - 78 78 78 78 78 78 78 78 78 78 78 78
  103255. - 78 78 78 82 82 82 86 86 86 94 94 94
  103256. -106 106 106 101 101 101 86 66 34 124 80 6
  103257. -156 107 11 180 123 7 192 133 9 200 144 11
  103258. -206 145 10 200 144 11 192 133 9 175 118 6
  103259. -139 102 15 109 106 95 70 70 70 42 42 42
  103260. - 22 22 22 10 10 10 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 6 6 6 10 10 10
  103267. - 14 14 14 22 22 22 30 30 30 38 38 38
  103268. - 50 50 50 62 62 62 74 74 74 90 90 90
  103269. -101 98 89 112 100 78 121 87 25 124 80 6
  103270. -137 92 6 152 99 6 152 99 6 152 99 6
  103271. -138 86 6 124 80 6 98 70 6 86 66 30
  103272. -101 98 89 82 82 82 58 58 58 46 46 46
  103273. - 38 38 38 34 34 34 34 34 34 34 34 34
  103274. - 34 34 34 34 34 34 34 34 34 34 34 34
  103275. - 34 34 34 34 34 34 38 38 38 42 42 42
  103276. - 54 54 54 82 82 82 94 86 76 91 60 6
  103277. -134 86 6 156 107 11 167 114 7 175 118 6
  103278. -175 118 6 167 114 7 152 99 6 121 87 25
  103279. -101 98 89 62 62 62 34 34 34 18 18 18
  103280. - 6 6 6 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 6 6 6 6 6 6 10 10 10
  103288. - 18 18 18 22 22 22 30 30 30 42 42 42
  103289. - 50 50 50 66 66 66 86 86 86 101 98 89
  103290. -106 86 58 98 70 6 104 69 6 104 69 6
  103291. -104 69 6 91 60 6 82 62 34 90 90 90
  103292. - 62 62 62 38 38 38 22 22 22 14 14 14
  103293. - 10 10 10 10 10 10 10 10 10 10 10 10
  103294. - 10 10 10 10 10 10 6 6 6 10 10 10
  103295. - 10 10 10 10 10 10 10 10 10 14 14 14
  103296. - 22 22 22 42 42 42 70 70 70 89 81 66
  103297. - 80 54 7 104 69 6 124 80 6 137 92 6
  103298. -134 86 6 116 81 8 100 82 52 86 86 86
  103299. - 58 58 58 30 30 30 14 14 14 6 6 6
  103300. - 0 0 0 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 6 6 6 10 10 10 14 14 14
  103309. - 18 18 18 26 26 26 38 38 38 54 54 54
  103310. - 70 70 70 86 86 86 94 86 76 89 81 66
  103311. - 89 81 66 86 86 86 74 74 74 50 50 50
  103312. - 30 30 30 14 14 14 6 6 6 0 0 0
  103313. - 0 0 0 0 0 0 0 0 0 0 0 0
  103314. - 0 0 0 0 0 0 0 0 0 0 0 0
  103315. - 0 0 0 0 0 0 0 0 0 0 0 0
  103316. - 6 6 6 18 18 18 34 34 34 58 58 58
  103317. - 82 82 82 89 81 66 89 81 66 89 81 66
  103318. - 94 86 66 94 86 76 74 74 74 50 50 50
  103319. - 26 26 26 14 14 14 6 6 6 0 0 0
  103320. - 0 0 0 0 0 0 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. - 6 6 6 6 6 6 14 14 14 18 18 18
  103330. - 30 30 30 38 38 38 46 46 46 54 54 54
  103331. - 50 50 50 42 42 42 30 30 30 18 18 18
  103332. - 10 10 10 0 0 0 0 0 0 0 0 0
  103333. - 0 0 0 0 0 0 0 0 0 0 0 0
  103334. - 0 0 0 0 0 0 0 0 0 0 0 0
  103335. - 0 0 0 0 0 0 0 0 0 0 0 0
  103336. - 0 0 0 6 6 6 14 14 14 26 26 26
  103337. - 38 38 38 50 50 50 58 58 58 58 58 58
  103338. - 54 54 54 42 42 42 30 30 30 18 18 18
  103339. - 10 10 10 0 0 0 0 0 0 0 0 0
  103340. - 0 0 0 0 0 0 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 6 6 6
  103350. - 6 6 6 10 10 10 14 14 14 18 18 18
  103351. - 18 18 18 14 14 14 10 10 10 6 6 6
  103352. - 0 0 0 0 0 0 0 0 0 0 0 0
  103353. - 0 0 0 0 0 0 0 0 0 0 0 0
  103354. - 0 0 0 0 0 0 0 0 0 0 0 0
  103355. - 0 0 0 0 0 0 0 0 0 0 0 0
  103356. - 0 0 0 0 0 0 0 0 0 6 6 6
  103357. - 14 14 14 18 18 18 22 22 22 22 22 22
  103358. - 18 18 18 14 14 14 10 10 10 6 6 6
  103359. - 0 0 0 0 0 0 0 0 0 0 0 0
  103360. - 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0
  103365. +0 0 0 0 0 0 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 0 0 0 0 0 0
  103367. +0 0 0 0 0 0 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 0 0 0 0 0 0
  103369. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103370. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103371. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103372. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103373. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103374. +0 0 0 0 0 0 0 0 0
  103375. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103376. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103377. +0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
  103378. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103379. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103380. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103381. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103382. +0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
  103383. +0 1 0 0 0 0 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 0 0 0 0 0 0
  103385. +0 0 0 0 0 0 0 0 0
  103386. +0 0 0 0 0 0 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 0 0 0 1 1 0
  103388. +10 15 3 2 3 1 12 18 4 42 61 14 19 27 6 11 16 4
  103389. +38 55 13 10 15 3 3 4 1 10 15 3 0 0 0 0 0 0
  103390. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103391. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103392. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1
  103393. +12 18 4 1 1 0 23 34 8 31 45 11 10 15 3 32 47 11
  103394. +34 49 12 3 4 1 3 4 1 3 4 1 0 0 0 0 0 0
  103395. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103396. +0 0 0 0 0 0 0 0 0
  103397. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103398. +0 0 0 0 0 0 10 15 3 29 42 10 26 37 9 12 18 4
  103399. +55 80 19 81 118 28 55 80 19 92 132 31 106 153 36 69 100 23
  103400. +100 144 34 80 116 27 42 61 14 81 118 28 23 34 8 27 40 9
  103401. +15 21 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103402. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103403. +0 0 0 0 0 0 1 1 0 29 42 10 15 21 5 50 72 17
  103404. +74 107 25 45 64 15 102 148 35 80 116 27 84 121 28 111 160 38
  103405. +69 100 23 65 94 22 81 118 28 29 42 10 17 25 6 29 42 10
  103406. +23 34 8 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  103407. +0 0 0 0 0 0 0 0 0
  103408. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  103409. +15 21 5 15 21 5 34 49 12 101 146 34 111 161 38 97 141 33
  103410. +97 141 33 119 172 41 117 170 40 116 167 40 118 170 40 118 171 40
  103411. +117 169 40 118 170 40 111 160 38 118 170 40 96 138 32 89 128 30
  103412. +81 118 28 11 16 4 10 15 3 1 1 0 0 0 0 0 0 0
  103413. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103414. +3 4 1 3 4 1 34 49 12 101 146 34 79 115 27 111 160 38
  103415. +114 165 39 113 163 39 118 170 40 117 169 40 118 171 40 117 169 40
  103416. +116 167 40 119 172 41 113 163 39 92 132 31 105 151 36 113 163 39
  103417. +75 109 26 19 27 6 16 23 5 11 16 4 0 1 0 0 0 0
  103418. +0 0 0 0 0 0 0 0 0
  103419. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  103420. +80 116 27 106 153 36 105 151 36 114 165 39 118 170 40 118 171 40
  103421. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103422. +117 169 40 117 169 40 117 170 40 117 169 40 118 170 40 118 170 40
  103423. +117 170 40 75 109 26 75 109 26 34 49 12 0 0 0 0 0 0
  103424. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  103425. +64 92 22 65 94 22 100 144 34 118 171 40 118 170 40 117 169 40
  103426. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103427. +117 169 40 117 169 40 117 169 40 118 171 41 118 170 40 117 169 40
  103428. +109 158 37 105 151 36 104 150 35 47 69 16 0 0 0 0 0 0
  103429. +0 0 0 0 0 0 0 0 0
  103430. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103431. +42 61 14 115 167 39 118 170 40 117 169 40 117 169 40 117 169 40
  103432. +117 170 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  103433. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103434. +117 169 40 117 169 40 118 170 40 96 138 32 17 25 6 0 0 0
  103435. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47 69 16
  103436. +114 165 39 117 168 40 117 170 40 117 169 40 117 169 40 117 169 40
  103437. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103438. +117 169 40 117 169 40 118 170 40 117 169 40 117 169 40 117 169 40
  103439. +117 170 40 119 172 41 96 138 32 12 18 4 0 0 0 0 0 0
  103440. +0 0 0 0 0 0 0 0 0
  103441. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  103442. +32 47 11 105 151 36 118 170 40 117 169 40 117 169 40 116 168 40
  103443. +109 157 37 111 160 38 117 169 40 118 171 40 117 169 40 117 169 40
  103444. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103445. +117 169 40 117 169 40 117 169 40 118 171 40 69 100 23 2 3 1
  103446. +0 0 0 0 0 0 0 0 0 0 0 0 19 27 6 101 146 34
  103447. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103448. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 170 40
  103449. +118 171 40 115 166 39 107 154 36 111 161 38 117 169 40 117 169 40
  103450. +117 169 40 118 171 40 75 109 26 19 27 6 2 3 1 0 0 0
  103451. +0 0 0 0 0 0 0 0 0
  103452. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 23 5
  103453. +89 128 30 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103454. +111 160 38 92 132 31 79 115 27 96 138 32 115 166 39 119 171 41
  103455. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103456. +117 169 40 117 169 40 117 169 40 118 170 40 109 157 37 26 37 9
  103457. +0 0 0 0 0 0 0 0 0 0 0 0 64 92 22 118 171 40
  103458. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103459. +117 169 40 117 169 40 117 169 40 118 170 40 118 171 40 109 157 37
  103460. +89 128 30 81 118 28 100 144 34 115 166 39 117 169 40 117 169 40
  103461. +117 169 40 117 170 40 113 163 39 60 86 20 1 1 0 0 0 0
  103462. +0 0 0 0 0 0 0 0 0
  103463. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103464. +27 40 9 96 138 32 118 170 40 117 169 40 117 169 40 117 169 40
  103465. +117 170 40 117 169 40 101 146 34 67 96 23 55 80 19 84 121 28
  103466. +113 163 39 119 171 41 117 169 40 117 169 40 117 169 40 117 169 40
  103467. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 65 94 22
  103468. +0 0 0 0 0 0 0 0 0 15 21 5 101 146 34 118 171 40
  103469. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103470. +117 169 40 118 170 40 118 171 40 104 150 35 69 100 23 53 76 18
  103471. +81 118 28 111 160 38 118 170 40 117 169 40 117 169 40 117 169 40
  103472. +117 169 40 114 165 39 69 100 23 10 15 3 0 0 0 0 0 0
  103473. +0 0 0 0 0 0 0 0 0
  103474. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  103475. +31 45 11 77 111 26 117 169 40 117 169 40 117 169 40 117 169 40
  103476. +117 169 40 117 169 40 118 170 40 116 168 40 92 132 31 47 69 16
  103477. +38 55 13 81 118 28 113 163 39 119 171 41 117 169 40 117 169 40
  103478. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 41 92 132 31
  103479. +10 15 3 0 0 0 0 0 0 36 52 12 115 166 39 117 169 40
  103480. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  103481. +118 171 40 102 148 35 64 92 22 34 49 12 65 94 22 106 153 36
  103482. +118 171 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  103483. +118 170 40 107 154 36 55 80 19 15 21 5 0 0 0 0 0 0
  103484. +0 0 0 0 0 0 0 0 0
  103485. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103486. +29 42 10 101 146 34 118 171 40 117 169 40 117 169 40 117 169 40
  103487. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 113 163 39
  103488. +75 109 26 27 40 9 36 52 12 89 128 30 116 167 40 118 171 40
  103489. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 104 150 35
  103490. +16 23 5 0 0 0 0 0 0 53 76 18 118 171 40 117 169 40
  103491. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 109 157 37
  103492. +67 96 23 23 34 8 42 61 14 96 138 32 118 170 40 118 170 40
  103493. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103494. +117 169 40 117 169 40 74 107 25 10 15 3 0 0 0 0 0 0
  103495. +0 0 0 0 0 0 0 0 0
  103496. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103497. +0 0 0 31 45 11 101 146 34 118 170 40 117 169 40 117 169 40
  103498. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103499. +119 171 41 102 148 35 47 69 16 14 20 5 50 72 17 102 148 35
  103500. +118 171 40 117 169 40 117 169 40 117 169 40 118 170 40 102 148 35
  103501. +15 21 5 0 0 0 0 0 0 50 72 17 118 170 40 117 169 40
  103502. +117 169 40 117 169 40 118 170 40 116 167 40 84 121 28 27 40 9
  103503. +19 27 6 74 107 25 114 165 39 118 171 40 117 169 40 117 169 40
  103504. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103505. +117 169 40 75 109 26 10 15 4 0 0 0 0 0 0 0 0 0
  103506. +0 0 0 0 0 0 0 0 0
  103507. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103508. +0 0 0 38 55 13 102 148 35 118 171 40 117 169 40 117 169 40
  103509. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103510. +117 169 40 118 170 40 115 167 39 77 111 26 17 25 6 19 27 6
  103511. +77 111 26 115 166 39 118 170 40 117 169 40 119 172 41 81 118 28
  103512. +3 4 1 0 0 0 0 0 0 27 40 9 111 160 38 118 170 40
  103513. +117 169 40 118 171 40 105 151 36 50 72 17 10 15 3 38 55 13
  103514. +100 144 34 118 171 40 117 169 40 117 169 40 117 169 40 117 169 40
  103515. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103516. +117 169 40 79 115 27 15 21 5 0 0 0 0 0 0 0 0 0
  103517. +0 0 0 0 0 0 0 0 0
  103518. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103519. +0 0 0 10 15 3 64 92 22 111 160 38 117 169 40 117 169 40
  103520. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103521. +117 169 40 117 169 40 117 169 40 118 171 40 96 138 32 32 47 11
  103522. +3 4 1 50 72 17 107 154 36 120 173 41 105 151 36 31 45 11
  103523. +0 0 0 0 0 0 0 0 0 3 4 1 65 94 22 117 169 40
  103524. +118 170 40 89 128 30 26 37 9 3 4 1 60 86 20 111 161 38
  103525. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103526. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103527. +97 141 33 36 52 12 1 1 0 0 0 0 0 0 0 0 0 0
  103528. +0 0 0 0 0 0 0 0 0
  103529. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103530. +0 0 0 0 0 0 14 20 5 75 109 26 117 168 40 117 169 40
  103531. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103532. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 107 154 36
  103533. +45 64 15 2 3 1 31 45 11 75 109 26 32 47 11 0 1 0
  103534. +0 0 0 0 0 0 0 0 0 0 0 0 10 15 3 55 80 19
  103535. +65 94 22 11 16 4 11 16 4 75 109 26 116 168 40 118 170 40
  103536. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103537. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 107 154 36
  103538. +47 69 16 3 4 1 0 0 0 0 0 0 0 0 0 0 0 0
  103539. +0 0 0 0 0 0 0 0 0
  103540. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103541. +0 0 0 0 0 0 12 18 4 69 100 23 111 161 38 118 171 40
  103542. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103543. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  103544. +111 160 38 50 72 17 2 3 1 2 3 1 0 0 0 0 0 0
  103545. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  103546. +1 1 0 12 18 4 81 118 28 118 170 40 117 169 40 117 169 40
  103547. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103548. +117 169 40 117 169 40 117 169 40 117 170 40 118 171 40 101 146 34
  103549. +42 61 14 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  103550. +0 0 0 0 0 0 0 0 0
  103551. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103552. +0 0 0 0 0 0 0 0 0 3 4 1 36 52 12 89 128 30
  103553. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103554. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103555. +118 171 41 101 146 34 14 20 5 0 0 0 0 0 0 0 0 0
  103556. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103557. +0 0 0 47 69 16 118 170 40 117 169 40 117 169 40 117 169 40
  103558. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103559. +117 169 40 117 169 40 117 170 40 111 160 38 69 100 23 19 27 6
  103560. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103561. +0 0 0 0 0 0 0 0 0
  103562. +0 0 0 0 0 0 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 11 16 4 69 100 23
  103564. +115 167 39 119 172 41 117 169 40 117 169 40 117 169 40 117 169 40
  103565. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103566. +119 172 41 75 109 26 3 4 1 0 0 0 0 0 0 0 0 0
  103567. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103568. +0 0 0 23 34 8 106 153 36 118 170 40 117 169 40 117 169 40
  103569. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103570. +117 169 40 118 170 40 119 172 41 105 151 36 42 61 14 2 3 1
  103571. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103572. +0 0 0 0 0 0 0 0 0
  103573. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103574. +0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 15 21 5
  103575. +45 64 15 80 116 27 114 165 39 118 170 40 117 169 40 117 169 40
  103576. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 119 172 41
  103577. +97 141 33 20 30 7 0 0 0 0 0 0 0 0 0 0 0 0
  103578. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103579. +0 0 0 1 1 0 53 76 18 114 165 39 118 171 40 117 169 40
  103580. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  103581. +118 171 40 104 150 35 64 92 22 31 45 11 10 15 3 0 0 0
  103582. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103583. +0 0 0 0 0 0 0 0 0
  103584. +0 0 0 0 0 0 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 0 0 0 0 0 0
  103586. +0 0 0 36 52 12 97 141 33 109 158 37 113 163 39 116 168 40
  103587. +117 169 40 117 170 40 118 170 40 119 172 41 115 167 39 84 121 28
  103588. +23 34 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103589. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103590. +0 0 0 0 0 0 3 4 1 50 72 17 102 148 35 118 171 40
  103591. +119 171 41 118 170 40 117 169 40 117 169 40 115 166 39 111 161 38
  103592. +109 157 37 79 115 27 12 18 4 0 0 0 0 0 0 0 0 0
  103593. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103594. +0 0 0 0 0 0 0 0 0
  103595. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103596. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103597. +0 0 0 3 4 1 15 21 5 23 34 8 45 64 15 106 153 36
  103598. +116 167 40 111 160 38 101 146 34 79 115 27 42 61 14 10 15 3
  103599. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103600. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103601. +0 0 0 0 0 0 0 0 0 1 1 0 20 30 7 60 86 20
  103602. +89 128 30 106 153 36 113 163 39 117 169 40 84 121 28 29 42 10
  103603. +19 27 6 10 15 3 2 3 1 0 0 0 0 0 0 0 0 0
  103604. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103605. +0 0 0 0 0 0 0 0 0
  103606. +0 0 0 0 0 0 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 0 0 0 0 0 0
  103608. +0 0 0 0 0 0 0 0 0 0 0 0 16 23 5 38 55 13
  103609. +36 52 12 26 37 9 12 18 4 2 3 1 0 0 0 0 0 0
  103610. +0 0 0 0 0 0 0 0 0 1 0 0 19 2 7 52 5 18
  103611. +78 7 27 88 8 31 81 7 29 56 5 19 25 2 9 3 0 1
  103612. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103613. +3 4 1 19 27 6 31 45 11 38 55 13 32 47 11 3 4 1
  103614. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103615. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103616. +0 0 0 0 0 0 0 0 0
  103617. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103618. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103619. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  103620. +9 0 3 12 1 4 9 0 3 4 0 1 0 0 0 0 0 0
  103621. +0 0 0 0 0 0 28 3 10 99 9 35 156 14 55 182 16 64
  103622. +189 17 66 190 17 67 189 17 66 184 17 65 166 15 58 118 13 41
  103623. +45 4 16 3 0 1 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 0 0 0 0 0 0
  103625. +0 0 0 0 0 0 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 0 0 0 0 0 0
  103627. +0 0 0 0 0 0 0 0 0
  103628. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103629. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103630. +0 0 0 0 0 0 11 1 4 52 5 18 101 9 35 134 12 47
  103631. +151 14 53 154 14 54 151 14 53 113 10 40 11 1 4 0 0 0
  103632. +3 0 1 67 6 24 159 14 56 190 17 67 190 17 67 188 17 66
  103633. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 191 17 67
  103634. +174 16 61 101 9 35 14 1 5 0 0 0 35 3 12 108 10 38
  103635. +122 11 43 122 11 43 112 10 39 87 8 30 50 5 17 13 1 5
  103636. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103637. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103638. +0 0 0 0 0 0 0 0 0
  103639. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103640. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103641. +3 0 1 56 5 19 141 13 49 182 16 64 191 17 67 191 17 67
  103642. +190 17 67 190 17 67 191 17 67 113 10 40 3 0 1 1 0 0
  103643. +79 7 28 180 16 63 190 17 67 188 17 66 188 17 66 188 17 66
  103644. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103645. +189 17 66 188 17 66 122 11 43 11 1 4 41 4 14 176 16 62
  103646. +191 17 67 191 17 67 191 17 67 190 17 67 181 16 63 146 13 51
  103647. +75 7 26 10 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  103648. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103649. +0 0 0 0 0 0 0 0 0
  103650. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103651. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 1 2
  103652. +90 8 32 178 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  103653. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 41 4 14
  103654. +173 16 61 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  103655. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103656. +188 17 66 188 17 66 188 17 66 88 8 31 1 0 0 89 8 31
  103657. +185 17 65 189 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  103658. +186 17 65 124 11 43 25 2 9 0 0 0 0 0 0 0 0 0
  103659. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103660. +0 0 0 0 0 0 0 0 0
  103661. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103662. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 89 8 31
  103663. +184 17 65 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103664. +190 17 67 151 14 53 34 3 12 0 0 0 0 0 0 79 7 28
  103665. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103666. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103667. +188 17 66 188 17 66 191 17 67 146 13 51 9 1 3 7 1 2
  103668. +108 10 38 187 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  103669. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 0 0 0
  103670. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103671. +0 0 0 0 0 0 0 0 0
  103672. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103673. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 176 16 62
  103674. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  103675. +151 14 53 38 3 13 0 0 0 0 0 0 0 0 0 50 5 17
  103676. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103677. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103678. +188 17 66 188 17 66 191 17 67 141 13 49 7 1 3 0 0 0
  103679. +11 1 4 112 10 39 187 17 66 189 17 66 188 17 66 188 17 66
  103680. +188 17 66 188 17 66 190 17 67 113 10 40 5 0 2 0 0 0
  103681. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103682. +0 0 0 0 0 0 0 0 0
  103683. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103684. +0 0 0 0 0 0 0 0 0 7 1 3 132 12 46 191 17 67
  103685. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 146 13 51
  103686. +35 3 12 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  103687. +101 9 35 185 17 65 190 17 67 188 17 66 188 17 66 188 17 66
  103688. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103689. +188 17 66 190 17 67 180 16 63 67 6 24 0 0 0 0 0 0
  103690. +0 0 0 11 1 4 108 10 38 186 17 65 189 17 66 188 17 66
  103691. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  103692. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103693. +0 0 0 0 0 0 0 0 0
  103694. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103695. +0 0 0 0 0 0 0 0 0 44 4 15 177 16 62 189 17 66
  103696. +188 17 66 188 17 66 189 17 66 189 17 66 134 12 47 28 3 10
  103697. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103698. +8 1 3 79 7 28 159 14 56 188 17 66 191 17 67 190 17 67
  103699. +189 17 66 189 17 66 189 17 66 189 17 66 190 17 67 191 17 67
  103700. +188 17 66 158 14 55 72 7 25 4 0 1 0 0 0 0 0 0
  103701. +0 0 0 0 0 0 8 1 3 95 9 33 182 16 64 189 17 67
  103702. +188 17 66 188 17 66 188 17 66 191 17 67 122 11 43 3 0 1
  103703. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103704. +0 0 0 0 0 0 0 0 0
  103705. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103706. +0 0 0 0 0 0 0 0 0 88 8 31 190 17 67 188 17 66
  103707. +188 17 66 189 17 66 185 17 65 113 10 40 18 2 6 0 0 0
  103708. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103709. +0 0 0 1 0 0 24 2 8 77 7 27 124 11 43 154 14 54
  103710. +168 15 59 173 16 61 173 16 61 168 15 59 154 14 54 124 11 43
  103711. +77 7 27 22 2 8 0 0 0 0 0 0 0 0 0 0 0 0
  103712. +0 0 0 0 0 0 0 0 0 5 0 2 77 7 27 173 16 61
  103713. +190 17 67 188 17 66 188 17 66 190 17 67 164 15 57 23 2 8
  103714. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103715. +0 0 0 0 0 0 0 0 0
  103716. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103717. +0 0 0 0 0 0 1 0 0 118 13 41 191 17 67 188 17 66
  103718. +190 17 67 174 16 61 87 8 30 8 1 3 0 0 0 0 0 0
  103719. +0 0 0 0 0 0 10 1 4 29 3 10 40 4 14 36 3 13
  103720. +18 2 6 2 0 1 0 0 0 0 0 0 3 0 1 14 1 5
  103721. +26 2 9 33 3 11 32 3 11 25 2 9 13 1 5 3 0 1
  103722. +0 0 0 14 1 5 56 5 19 95 9 33 109 10 38 101 9 35
  103723. +77 7 27 35 3 12 5 0 2 0 0 0 1 0 0 56 5 19
  103724. +156 14 55 190 17 67 188 17 66 188 17 66 182 16 64 50 5 17
  103725. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103726. +0 0 0 0 0 0 0 0 0
  103727. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103728. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 189 17 66
  103729. +151 14 53 52 5 18 2 0 1 0 0 0 0 0 0 1 0 0
  103730. +28 3 10 90 8 32 146 13 51 170 15 60 178 16 62 174 16 61
  103731. +158 14 55 112 10 39 40 4 14 1 0 0 0 0 0 0 0 0
  103732. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  103733. +56 5 19 146 13 51 183 17 64 191 17 67 191 17 67 191 17 67
  103734. +188 17 66 173 16 61 122 11 43 41 4 14 1 0 0 0 0 0
  103735. +30 3 10 124 11 43 185 17 65 190 17 67 187 17 66 67 6 24
  103736. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103737. +0 0 0 0 0 0 0 0 0
  103738. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103739. +0 0 0 0 0 0 6 1 2 134 12 47 168 15 59 99 9 35
  103740. +21 2 7 0 0 0 0 0 0 0 0 0 6 1 2 77 7 27
  103741. +162 15 57 190 17 67 191 17 67 189 17 66 189 17 66 189 17 66
  103742. +190 17 67 191 17 67 169 15 59 75 7 26 3 0 1 0 0 0
  103743. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 79 7 28
  103744. +178 16 62 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  103745. +188 17 66 189 17 66 191 17 67 170 15 60 79 7 28 5 0 2
  103746. +0 0 0 10 1 3 78 7 27 159 14 56 188 17 66 75 7 26
  103747. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103748. +0 0 0 0 0 0 0 0 0
  103749. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103750. +0 0 0 0 0 0 1 0 0 35 3 12 29 3 10 2 0 1
  103751. +0 0 0 0 0 0 0 0 0 9 1 3 101 9 35 183 17 64
  103752. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103753. +188 17 66 188 17 66 190 17 67 178 16 63 67 6 23 0 0 0
  103754. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 174 16 61
  103755. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103756. +188 17 66 188 17 66 188 17 66 190 17 67 182 16 64 89 8 31
  103757. +4 0 1 0 0 0 0 0 0 25 2 9 73 7 26 31 3 11
  103758. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103759. +0 0 0 0 0 0 0 0 0
  103760. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103761. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103762. +0 0 0 0 0 0 4 0 1 98 9 34 187 17 66 189 17 66
  103763. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103764. +188 17 66 188 17 66 188 17 66 190 17 67 158 14 55 25 2 9
  103765. +0 0 0 0 0 0 0 0 0 8 1 3 134 12 47 191 17 67
  103766. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103767. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 180 16 63
  103768. +68 6 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103769. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103770. +0 0 0 0 0 0 0 0 0
  103771. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103772. +0 0 0 6 1 2 19 2 7 3 0 1 0 0 0 0 0 0
  103773. +0 0 0 0 0 0 65 6 23 180 16 63 189 17 66 188 17 66
  103774. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103775. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 83 8 29
  103776. +0 0 0 0 0 0 0 0 0 41 4 14 177 16 62 189 17 66
  103777. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103778. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  103779. +159 14 56 28 3 10 0 0 0 0 0 0 0 0 0 23 2 8
  103780. +41 4 14 5 0 2 0 0 0 0 0 0 0 0 0 0 0 0
  103781. +0 0 0 0 0 0 0 0 0
  103782. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103783. +23 2 8 113 10 40 159 14 56 65 6 23 0 0 0 0 0 0
  103784. +0 0 0 16 1 6 146 13 51 191 17 67 188 17 66 188 17 66
  103785. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103786. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 132 12 46
  103787. +5 0 2 0 0 0 0 0 0 77 7 27 189 17 66 188 17 66
  103788. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103789. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103790. +190 17 67 98 9 34 0 0 0 0 0 0 12 1 4 134 12 47
  103791. +178 16 63 108 10 38 16 1 6 0 0 0 0 0 0 0 0 0
  103792. +0 0 0 0 0 0 0 0 0
  103793. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 3 10
  103794. +141 13 49 190 17 67 191 17 67 134 12 47 6 1 2 0 0 0
  103795. +0 0 0 68 6 24 186 17 65 188 17 66 188 17 66 188 17 66
  103796. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103797. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 156 14 55
  103798. +14 1 5 0 0 0 0 0 0 98 9 34 191 17 67 188 17 66
  103799. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103800. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103801. +190 17 67 156 14 55 19 2 7 0 0 0 47 4 16 181 16 63
  103802. +190 17 67 189 17 66 126 14 44 17 2 6 0 0 0 0 0 0
  103803. +0 0 0 0 0 0 0 0 0
  103804. +0 0 0 0 0 0 0 0 0 0 0 0 16 1 6 134 12 47
  103805. +191 17 67 188 17 66 190 17 67 162 15 57 19 2 7 0 0 0
  103806. +3 0 1 123 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  103807. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103808. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 163 15 57
  103809. +20 2 7 0 0 0 0 0 0 101 9 35 191 17 67 188 17 66
  103810. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103811. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103812. +188 17 66 182 16 64 52 5 18 0 0 0 73 7 26 188 17 66
  103813. +188 17 66 188 17 66 189 17 66 109 10 38 5 0 2 0 0 0
  103814. +0 0 0 0 0 0 0 0 0
  103815. +0 0 0 0 0 0 0 0 0 0 0 0 95 9 33 189 17 66
  103816. +188 17 66 188 17 66 189 17 66 171 15 60 29 3 10 0 0 0
  103817. +16 1 6 156 14 55 190 17 67 188 17 66 188 17 66 188 17 66
  103818. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103819. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 158 14 55
  103820. +17 2 6 0 0 0 0 0 0 85 8 30 190 17 67 188 17 66
  103821. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103822. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103823. +188 17 66 189 17 66 81 7 29 0 0 0 85 8 30 190 17 67
  103824. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  103825. +0 0 0 0 0 0 0 0 0
  103826. +0 0 0 0 0 0 0 0 0 25 2 9 162 15 57 190 17 67
  103827. +188 17 66 188 17 66 189 17 66 173 16 61 31 3 11 0 0 0
  103828. +30 3 10 171 15 60 189 17 66 188 17 66 188 17 66 188 17 66
  103829. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103830. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 141 13 49
  103831. +7 1 2 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  103832. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103833. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103834. +188 17 66 191 17 67 98 9 34 0 0 0 88 8 31 190 17 67
  103835. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 5 0 2
  103836. +0 0 0 0 0 0 0 0 0
  103837. +0 0 0 0 0 0 0 0 0 68 6 24 187 17 66 188 17 66
  103838. +188 17 66 188 17 66 189 17 66 170 15 60 28 3 10 0 0 0
  103839. +34 3 12 174 16 61 189 17 66 188 17 66 188 17 66 188 17 66
  103840. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103841. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 101 9 35
  103842. +0 0 0 0 0 0 0 0 0 21 2 7 159 14 56 190 17 67
  103843. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103844. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103845. +188 17 66 191 17 67 98 9 34 0 0 0 81 7 29 189 17 66
  103846. +188 17 66 188 17 66 188 17 66 189 17 66 168 15 59 28 3 10
  103847. +0 0 0 0 0 0 0 0 0
  103848. +0 0 0 0 0 0 0 0 0 109 10 38 191 17 67 188 17 66
  103849. +188 17 66 188 17 66 190 17 67 163 15 57 21 2 7 0 0 0
  103850. +26 2 9 168 15 59 189 17 66 188 17 66 188 17 66 188 17 66
  103851. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103852. +188 17 66 188 17 66 188 17 66 189 17 66 180 16 63 47 4 16
  103853. +0 0 0 0 0 0 0 0 0 0 0 0 108 10 38 190 17 67
  103854. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103855. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103856. +188 17 66 189 17 66 78 7 27 0 0 0 68 6 24 187 17 66
  103857. +188 17 66 188 17 66 188 17 66 188 17 66 183 17 64 56 5 19
  103858. +0 0 0 0 0 0 0 0 0
  103859. +0 0 0 0 0 0 3 0 1 131 12 46 191 17 67 188 17 66
  103860. +188 17 66 188 17 66 190 17 67 151 14 53 12 1 4 0 0 0
  103861. +11 1 4 146 13 51 190 17 67 188 17 66 188 17 66 188 17 66
  103862. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103863. +188 17 66 188 17 66 188 17 66 191 17 67 126 14 44 7 1 2
  103864. +0 0 0 0 0 0 0 0 0 0 0 0 32 3 11 164 15 58
  103865. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103866. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103867. +189 17 66 178 16 62 44 4 15 0 0 0 50 5 17 182 16 64
  103868. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  103869. +0 0 0 0 0 0 0 0 0
  103870. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 188 17 66
  103871. +188 17 66 188 17 66 191 17 67 131 12 46 3 0 1 0 0 0
  103872. +0 0 0 101 9 35 190 17 67 188 17 66 188 17 66 188 17 66
  103873. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103874. +188 17 66 188 17 66 190 17 67 170 15 60 44 4 15 0 0 0
  103875. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 7 27
  103876. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103877. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103878. +191 17 67 134 12 47 9 1 3 0 0 0 31 3 11 171 15 60
  103879. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  103880. +0 0 0 0 0 0 0 0 0
  103881. +0 0 0 0 0 0 2 0 1 124 11 43 191 17 67 188 17 66
  103882. +188 17 66 188 17 66 191 17 67 101 9 35 0 0 0 0 0 0
  103883. +0 0 0 35 3 12 168 15 59 190 17 67 188 17 66 188 17 66
  103884. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103885. +188 17 66 189 17 66 182 16 64 77 7 27 0 0 0 0 0 0
  103886. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 2
  103887. +99 9 35 185 17 65 189 17 66 188 17 66 188 17 66 188 17 66
  103888. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  103889. +177 16 62 56 5 19 0 0 0 0 0 0 13 1 5 151 14 53
  103890. +190 17 67 188 17 66 188 17 66 188 17 66 185 17 65 56 5 19
  103891. +0 0 0 0 0 0 0 0 0
  103892. +0 0 0 0 0 0 0 0 0 99 9 35 191 17 67 188 17 66
  103893. +188 17 66 188 17 66 186 17 65 65 6 23 0 0 0 0 0 0
  103894. +0 0 0 0 0 0 79 7 28 182 16 64 190 17 67 188 17 66
  103895. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103896. +191 17 67 177 16 62 83 8 29 4 0 1 0 0 0 0 0 0
  103897. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103898. +8 1 3 89 8 31 175 16 62 191 17 67 189 17 66 188 17 66
  103899. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 181 16 63
  103900. +85 8 30 3 0 1 0 0 0 0 0 0 1 0 0 118 13 41
  103901. +191 17 67 188 17 66 188 17 66 189 17 66 173 16 61 34 3 12
  103902. +0 0 0 0 0 0 0 0 0
  103903. +0 0 0 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  103904. +188 17 66 189 17 66 169 15 59 30 3 10 0 0 0 0 0 0
  103905. +0 0 0 0 0 0 5 0 2 83 8 29 173 16 61 191 17 67
  103906. +190 17 67 189 17 66 189 17 66 190 17 67 191 17 67 187 17 66
  103907. +151 14 53 56 5 19 3 0 1 0 0 0 16 1 6 50 5 17
  103908. +79 7 28 95 9 33 95 9 33 75 7 26 41 4 14 10 1 4
  103909. +0 0 0 2 0 1 50 5 17 132 12 46 178 16 62 190 17 67
  103910. +191 17 67 191 17 67 191 17 67 186 17 65 154 14 54 68 6 24
  103911. +4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  103912. +187 17 66 188 17 66 188 17 66 191 17 67 141 13 49 9 1 3
  103913. +0 0 0 0 0 0 0 0 0
  103914. +0 0 0 0 0 0 0 0 0 14 1 5 151 14 53 190 17 67
  103915. +188 17 66 191 17 67 131 12 46 5 0 2 0 0 0 0 0 0
  103916. +0 0 0 0 0 0 0 0 0 2 0 1 44 4 15 113 10 40
  103917. +156 14 55 173 16 61 174 16 61 164 15 58 134 12 47 77 7 27
  103918. +18 2 6 0 0 0 16 1 6 85 8 30 151 14 53 182 16 64
  103919. +189 17 66 191 17 67 190 17 67 188 17 66 177 16 62 141 13 49
  103920. +68 6 24 8 1 3 0 0 0 8 1 3 44 4 15 88 8 31
  103921. +113 10 40 122 11 43 108 10 38 67 6 24 20 2 7 0 0 0
  103922. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 3 10
  103923. +166 15 58 190 17 67 188 17 66 187 17 66 79 7 28 0 0 0
  103924. +0 0 0 0 0 0 0 0 0
  103925. +0 0 0 0 0 0 0 0 0 0 0 0 73 7 26 185 17 65
  103926. +189 17 66 184 17 65 65 6 23 0 0 0 0 0 0 0 0 0
  103927. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
  103928. +17 2 6 32 3 11 34 3 12 22 2 8 6 1 2 0 0 0
  103929. +0 0 0 38 3 13 141 13 49 188 17 66 190 17 67 188 17 66
  103930. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  103931. +184 17 65 122 11 43 21 2 7 0 0 0 0 0 0 0 0 0
  103932. +0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103933. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  103934. +108 10 38 191 17 67 191 17 67 141 13 49 16 1 6 0 0 0
  103935. +0 0 0 0 0 0 0 0 0
  103936. +0 0 0 0 0 0 0 0 0 0 0 0 8 1 3 112 10 39
  103937. +186 17 65 124 11 43 10 1 4 0 0 0 0 0 0 0 0 0
  103938. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103939. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103940. +36 3 13 156 14 55 191 17 67 188 17 66 188 17 66 188 17 66
  103941. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103942. +189 17 66 190 17 67 134 12 47 18 2 6 0 0 0 0 0 0
  103943. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103944. +0 0 0 7 1 2 41 4 14 75 7 26 66 5 23 19 2 7
  103945. +26 2 9 144 13 50 154 14 54 40 4 14 0 0 0 0 0 0
  103946. +0 0 0 0 0 0 0 0 0
  103947. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  103948. +56 5 19 19 2 7 0 0 0 7 1 2 29 3 10 35 3 12
  103949. +19 2 7 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  103950. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  103951. +134 12 47 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  103952. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103953. +188 17 66 188 17 66 189 17 67 108 10 38 3 0 1 0 0 0
  103954. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  103955. +40 4 14 124 11 43 177 16 62 188 17 66 187 17 66 144 13 50
  103956. +24 2 8 17 2 6 22 2 8 0 0 0 0 0 0 0 0 0
  103957. +0 0 0 0 0 0 0 0 0
  103958. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103959. +0 0 0 0 0 0 19 2 7 122 11 43 171 15 60 175 16 62
  103960. +159 14 56 112 10 39 40 4 14 2 0 1 0 0 0 0 0 0
  103961. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  103962. +186 17 65 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103963. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103964. +188 17 66 188 17 66 189 17 66 174 16 61 41 4 14 0 0 0
  103965. +0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 72 7 25
  103966. +168 15 59 191 17 67 189 17 66 188 17 66 188 17 66 190 17 67
  103967. +95 9 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103968. +0 0 0 0 0 0 0 0 0
  103969. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103970. +0 0 0 0 0 0 95 9 33 191 17 67 189 17 66 189 17 66
  103971. +190 17 67 191 17 67 171 15 60 90 8 32 12 1 4 0 0 0
  103972. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 132 12 46
  103973. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103974. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103975. +188 17 66 188 17 66 188 17 66 190 17 67 98 9 34 0 0 0
  103976. +0 0 0 0 0 0 0 0 0 5 0 2 88 8 31 180 16 63
  103977. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 191 17 67
  103978. +146 13 51 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  103979. +0 0 0 0 0 0 0 0 0
  103980. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103981. +0 0 0 9 1 3 144 13 50 191 17 67 188 17 66 188 17 66
  103982. +188 17 66 188 17 66 189 17 66 187 17 66 123 11 43 20 2 7
  103983. +0 0 0 0 0 0 0 0 0 0 0 0 21 2 7 163 15 57
  103984. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103985. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103986. +188 17 66 188 17 66 188 17 66 191 17 67 134 12 47 5 0 2
  103987. +0 0 0 0 0 0 3 0 1 88 8 31 182 16 64 189 17 66
  103988. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  103989. +171 15 60 31 3 11 0 0 0 0 0 0 0 0 0 0 0 0
  103990. +0 0 0 0 0 0 0 0 0
  103991. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  103992. +0 0 0 20 2 7 162 15 57 190 17 67 188 17 66 188 17 66
  103993. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 132 12 46
  103994. +20 2 7 0 0 0 0 0 0 0 0 0 32 3 11 173 16 61
  103995. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103996. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  103997. +188 17 66 188 17 66 188 17 66 190 17 67 151 14 53 12 1 4
  103998. +0 0 0 0 0 0 72 7 25 180 16 63 189 17 66 188 17 66
  103999. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104000. +181 16 63 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  104001. +0 0 0 0 0 0 0 0 0
  104002. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104003. +0 0 0 21 2 7 163 15 57 190 17 67 188 17 66 188 17 66
  104004. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104005. +122 11 43 9 1 3 0 0 0 0 0 0 30 3 10 171 15 60
  104006. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104007. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104008. +188 17 66 188 17 66 188 17 66 190 17 67 146 13 51 10 1 4
  104009. +0 0 0 38 3 13 166 15 58 190 17 67 188 17 66 188 17 66
  104010. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104011. +183 17 64 52 5 18 0 0 0 0 0 0 0 0 0 0 0 0
  104012. +0 0 0 0 0 0 0 0 0
  104013. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104014. +0 0 0 13 1 5 154 14 54 190 17 67 188 17 66 188 17 66
  104015. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104016. +186 17 65 79 7 28 0 0 0 0 0 0 14 1 5 156 14 54
  104017. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104018. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104019. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 2 0 1
  104020. +5 0 2 122 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  104021. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104022. +182 16 64 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  104023. +0 0 0 0 0 0 0 0 0
  104024. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104025. +0 0 0 3 0 1 126 14 44 191 17 67 188 17 66 188 17 66
  104026. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104027. +190 17 67 158 14 55 23 2 8 0 0 0 1 0 0 113 10 40
  104028. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104029. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104030. +188 17 66 188 17 66 188 17 66 188 17 66 78 7 27 0 0 0
  104031. +47 4 16 177 16 62 189 17 66 188 17 66 188 17 66 188 17 66
  104032. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  104033. +173 16 61 34 3 12 0 0 0 0 0 0 0 0 0 0 0 0
  104034. +0 0 0 0 0 0 0 0 0
  104035. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104036. +0 0 0 0 0 0 85 8 30 189 17 66 188 17 66 188 17 66
  104037. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104038. +188 17 66 188 17 66 79 7 28 0 0 0 0 0 0 47 4 16
  104039. +175 16 62 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104040. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104041. +188 17 66 188 17 66 190 17 67 156 14 55 22 2 8 0 0 0
  104042. +109 10 38 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104043. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104044. +151 14 53 13 1 5 0 0 0 0 0 0 0 0 0 0 0 0
  104045. +0 0 0 0 0 0 0 0 0
  104046. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104047. +0 0 0 0 0 0 35 3 12 173 16 61 189 17 66 188 17 66
  104048. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104049. +188 17 66 191 17 67 134 12 47 7 1 2 0 0 0 3 0 1
  104050. +99 9 35 188 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  104051. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104052. +188 17 66 189 17 66 181 16 63 68 6 24 0 0 0 18 2 6
  104053. +156 14 55 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104054. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104055. +101 9 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104056. +0 0 0 0 0 0 0 0 0
  104057. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104058. +0 0 0 0 0 0 3 0 1 118 13 41 191 17 67 188 17 66
  104059. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104060. +188 17 66 189 17 66 168 15 59 28 3 10 0 0 0 0 0 0
  104061. +12 1 4 113 10 40 187 17 66 189 17 67 188 17 66 188 17 66
  104062. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104063. +190 17 67 180 16 63 88 8 31 4 0 1 0 0 0 47 4 16
  104064. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104065. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 168 15 59
  104066. +36 3 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104067. +0 0 0 0 0 0 0 0 0
  104068. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104069. +0 0 0 0 0 0 0 0 0 38 3 13 164 15 58 190 17 67
  104070. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104071. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  104072. +0 0 0 11 1 4 90 8 32 169 15 59 190 17 67 190 17 67
  104073. +189 17 66 189 17 66 189 17 66 189 17 66 191 17 67 189 17 66
  104074. +158 14 55 68 6 24 4 0 1 0 0 0 0 0 0 73 7 26
  104075. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104076. +188 17 66 188 17 66 188 17 66 189 17 66 185 17 65 83 8 29
  104077. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104078. +0 0 0 0 0 0 0 0 0
  104079. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104080. +0 0 0 0 0 0 0 0 0 0 0 0 65 6 23 174 16 61
  104081. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104082. +188 17 66 188 17 66 185 17 65 56 5 19 0 0 0 0 0 0
  104083. +0 0 0 0 0 0 2 0 1 35 3 12 99 9 35 146 13 51
  104084. +170 15 60 177 16 62 177 16 62 166 15 58 141 13 49 85 8 30
  104085. +24 2 8 0 0 0 0 0 0 0 0 0 0 0 0 85 8 30
  104086. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104087. +188 17 66 188 17 66 188 17 66 189 17 66 112 10 39 8 1 3
  104088. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104089. +0 0 0 0 0 0 0 0 0
  104090. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104091. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 68 6 24
  104092. +170 15 60 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104093. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  104094. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 11 1 4
  104095. +28 3 10 40 4 14 38 3 13 25 2 9 8 1 3 0 0 0
  104096. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 7 27
  104097. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104098. +188 17 66 189 17 66 187 17 66 113 10 40 14 1 5 0 0 0
  104099. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104100. +0 0 0 0 0 0 0 0 0
  104101. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104102. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  104103. +47 4 16 141 13 49 186 17 65 191 17 67 190 17 67 189 17 66
  104104. +189 17 66 191 17 67 156 14 55 20 2 7 0 0 0 0 0 0
  104105. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104106. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104107. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 4 15
  104108. +178 16 62 190 17 67 188 17 66 188 17 66 188 17 66 190 17 67
  104109. +191 17 67 173 16 61 90 8 32 10 1 4 0 0 0 0 0 0
  104110. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104111. +0 0 0 0 0 0 0 0 0
  104112. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104113. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104114. +0 0 0 14 1 5 68 6 24 131 12 46 162 15 57 174 16 61
  104115. +171 15 60 146 13 51 56 5 19 0 0 0 0 0 0 0 0 0
  104116. +0 0 0 0 0 0 0 0 0 3 0 1 14 1 5 29 3 10
  104117. +41 4 14 47 4 16 50 5 17 45 4 16 34 3 12 18 2 6
  104118. +5 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  104119. +90 8 32 169 15 59 185 17 65 187 17 66 182 16 64 163 15 57
  104120. +113 10 40 41 4 14 2 0 1 0 0 0 0 0 0 0 0 0
  104121. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104122. +0 0 0 0 0 0 0 0 0
  104123. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104125. +0 0 0 0 0 0 0 0 0 5 0 2 21 2 7 34 3 12
  104126. +29 3 10 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  104127. +3 0 1 32 3 11 79 7 28 124 11 43 154 14 54 171 15 60
  104128. +180 16 63 182 16 64 182 16 64 180 16 63 174 16 61 159 14 56
  104129. +132 12 46 88 8 31 34 3 12 3 0 1 0 0 0 0 0 0
  104130. +3 0 1 29 3 10 56 5 19 65 6 23 50 5 17 23 2 8
  104131. +3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104132. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104133. +0 0 0 0 0 0 0 0 0
  104134. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104135. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104136. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104137. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 2 9
  104138. +109 10 38 169 15 59 189 17 66 191 17 67 190 17 67 189 17 66
  104139. +189 17 66 188 17 66 188 17 66 188 17 66 189 17 66 190 17 67
  104140. +191 17 67 190 17 67 171 15 60 98 9 34 10 1 3 0 0 0
  104141. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104142. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104144. +0 0 0 0 0 0 0 0 0
  104145. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104147. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104148. +0 0 0 0 0 0 0 0 0 0 0 0 14 1 5 141 13 49
  104149. +191 17 67 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104150. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104151. +188 17 66 188 17 66 189 17 67 186 17 65 65 6 23 0 0 0
  104152. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104153. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104154. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104155. +0 0 0 0 0 0 0 0 0
  104156. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104157. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104158. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104159. +0 0 0 0 0 0 0 0 0 0 0 0 23 2 8 166 15 58
  104160. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104161. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104162. +188 17 66 188 17 66 189 17 66 176 16 62 45 4 16 0 0 0
  104163. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104165. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104166. +0 0 0 0 0 0 0 0 0
  104167. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104168. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104169. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104170. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 83 8 29
  104171. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104172. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104173. +188 17 66 189 17 66 185 17 65 95 9 33 3 0 1 0 0 0
  104174. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104175. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104176. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104177. +0 0 0 0 0 0 0 0 0
  104178. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104179. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104180. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104181. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  104182. +85 8 30 176 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  104183. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104184. +191 17 67 180 16 63 95 9 33 7 1 3 0 0 0 0 0 0
  104185. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104187. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104188. +0 0 0 0 0 0 0 0 0
  104189. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104190. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104191. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104192. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104193. +2 0 1 52 5 18 141 13 49 185 17 65 191 17 67 189 17 67
  104194. +189 17 66 188 17 66 188 17 66 189 17 66 191 17 67 187 17 66
  104195. +146 13 51 56 5 19 4 0 1 0 0 0 0 0 0 0 0 0
  104196. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104197. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104198. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104199. +0 0 0 0 0 0 0 0 0
  104200. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104201. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104203. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104204. +0 0 0 0 0 0 14 1 5 68 6 24 131 12 46 166 15 58
  104205. +180 16 63 183 17 64 180 16 63 168 15 59 134 12 47 75 7 26
  104206. +17 2 6 0 0 0 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 0 0 0 0 0 0
  104208. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104209. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104210. +0 0 0 0 0 0 0 0 0
  104211. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104212. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104213. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104214. +0 0 0 0 0 0 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 5 0 2 24 2 8
  104216. +44 4 15 52 5 18 45 4 16 26 2 9 6 1 2 0 0 0
  104217. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104218. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104219. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104220. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104221. +0 0 0 0 0 0 0 0 0
  104222. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104224. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104226. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104228. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104230. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104231. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104232. +0 0 0 0 0 0 0 0 0
  104233. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104234. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104236. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104237. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104238. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104239. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104240. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104241. +0 0 0 0 0 0 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 0 0 0 0 0 0
  104243. +0 0 0 0 0 0 0 0 0
  104244. diff -Nur linux-3.17.5/drivers/w1/masters/w1-gpio.c linux-rpi/drivers/w1/masters/w1-gpio.c
  104245. --- linux-3.17.5/drivers/w1/masters/w1-gpio.c 2014-12-06 17:57:59.000000000 -0600
  104246. +++ linux-rpi/drivers/w1/masters/w1-gpio.c 2014-12-11 14:05:39.700418001 -0600
  104247. @@ -23,6 +23,15 @@
  104248. #include "../w1.h"
  104249. #include "../w1_int.h"
  104250. +static int w1_gpio_pullup = -1;
  104251. +static int w1_gpio_pullup_orig = -1;
  104252. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  104253. +MODULE_PARM_DESC(pullup, "GPIO pin pullup number");
  104254. +static int w1_gpio_pin = -1;
  104255. +static int w1_gpio_pin_orig = -1;
  104256. +module_param_named(gpiopin, w1_gpio_pin, int, 0);
  104257. +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
  104258. +
  104259. static u8 w1_gpio_set_pullup(void *data, int delay)
  104260. {
  104261. struct w1_gpio_platform_data *pdata = data;
  104262. @@ -67,6 +76,16 @@
  104263. return gpio_get_value(pdata->pin) ? 1 : 0;
  104264. }
  104265. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  104266. +{
  104267. + struct w1_gpio_platform_data *pdata = data;
  104268. +
  104269. + if (on)
  104270. + gpio_direction_output(pdata->pin, 1);
  104271. + else
  104272. + gpio_direction_input(pdata->pin);
  104273. +}
  104274. +
  104275. #if defined(CONFIG_OF)
  104276. static struct of_device_id w1_gpio_dt_ids[] = {
  104277. { .compatible = "w1-gpio" },
  104278. @@ -113,13 +132,15 @@
  104279. static int w1_gpio_probe(struct platform_device *pdev)
  104280. {
  104281. struct w1_bus_master *master;
  104282. - struct w1_gpio_platform_data *pdata;
  104283. + struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  104284. int err;
  104285. - if (of_have_populated_dt()) {
  104286. - err = w1_gpio_probe_dt(pdev);
  104287. - if (err < 0)
  104288. - return err;
  104289. + if(pdata == NULL) {
  104290. + if (of_have_populated_dt()) {
  104291. + err = w1_gpio_probe_dt(pdev);
  104292. + if (err < 0)
  104293. + return err;
  104294. + }
  104295. }
  104296. pdata = dev_get_platdata(&pdev->dev);
  104297. @@ -136,6 +157,19 @@
  104298. return -ENOMEM;
  104299. }
  104300. + w1_gpio_pin_orig = pdata->pin;
  104301. + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
  104302. +
  104303. + if(gpio_is_valid(w1_gpio_pin)) {
  104304. + pdata->pin = w1_gpio_pin;
  104305. + pdata->ext_pullup_enable_pin = -1;
  104306. + }
  104307. + if(gpio_is_valid(w1_gpio_pullup)) {
  104308. + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
  104309. + }
  104310. +
  104311. + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
  104312. +
  104313. err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
  104314. if (err) {
  104315. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  104316. @@ -165,6 +199,14 @@
  104317. master->set_pullup = w1_gpio_set_pullup;
  104318. }
  104319. + if (gpio_is_valid(w1_gpio_pullup)) {
  104320. + if (pdata->is_open_drain)
  104321. + printk(KERN_ERR "w1-gpio 'pullup' option "
  104322. + "doesn't work with open drain GPIO\n");
  104323. + else
  104324. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  104325. + }
  104326. +
  104327. err = w1_add_master_device(master);
  104328. if (err) {
  104329. dev_err(&pdev->dev, "w1_add_master device failed\n");
  104330. @@ -195,6 +237,9 @@
  104331. w1_remove_master_device(master);
  104332. + pdata->pin = w1_gpio_pin_orig;
  104333. + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
  104334. +
  104335. return 0;
  104336. }
  104337. diff -Nur linux-3.17.5/drivers/w1/w1.h linux-rpi/drivers/w1/w1.h
  104338. --- linux-3.17.5/drivers/w1/w1.h 2014-12-06 17:57:59.000000000 -0600
  104339. +++ linux-rpi/drivers/w1/w1.h 2014-12-11 14:05:39.700418001 -0600
  104340. @@ -171,6 +171,12 @@
  104341. u8 (*set_pullup)(void *, int);
  104342. + /**
  104343. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  104344. + * @return -1=Error, 0=completed
  104345. + */
  104346. + void (*bitbang_pullup) (void *, u8);
  104347. +
  104348. void (*search)(void *, struct w1_master *,
  104349. u8, w1_slave_found_callback);
  104350. };
  104351. diff -Nur linux-3.17.5/drivers/w1/w1_int.c linux-rpi/drivers/w1/w1_int.c
  104352. --- linux-3.17.5/drivers/w1/w1_int.c 2014-12-06 17:57:59.000000000 -0600
  104353. +++ linux-rpi/drivers/w1/w1_int.c 2014-12-11 14:05:39.700418001 -0600
  104354. @@ -123,6 +123,20 @@
  104355. return(-EINVAL);
  104356. }
  104357. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  104358. + * and takes care of timing itself */
  104359. + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  104360. + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  104361. + "write_byte or touch_bit, disabling\n");
  104362. + master->set_pullup = NULL;
  104363. + }
  104364. +
  104365. + if (master->set_pullup && master->bitbang_pullup) {
  104366. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  104367. + "be set when bitbang_pullup is used, disabling\n");
  104368. + master->set_pullup = NULL;
  104369. + }
  104370. +
  104371. /* Lock until the device is added (or not) to w1_masters. */
  104372. mutex_lock(&w1_mlock);
  104373. /* Search for the first available id (starting at 1). */
  104374. diff -Nur linux-3.17.5/drivers/w1/w1_io.c linux-rpi/drivers/w1/w1_io.c
  104375. --- linux-3.17.5/drivers/w1/w1_io.c 2014-12-06 17:57:59.000000000 -0600
  104376. +++ linux-rpi/drivers/w1/w1_io.c 2014-12-11 14:05:39.700418001 -0600
  104377. @@ -134,10 +134,22 @@
  104378. static void w1_post_write(struct w1_master *dev)
  104379. {
  104380. if (dev->pullup_duration) {
  104381. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  104382. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  104383. - else
  104384. + if (dev->enable_pullup) {
  104385. + if (dev->bus_master->set_pullup) {
  104386. + dev->bus_master->set_pullup(dev->
  104387. + bus_master->data,
  104388. + 0);
  104389. + } else if (dev->bus_master->bitbang_pullup) {
  104390. + dev->bus_master->
  104391. + bitbang_pullup(dev->bus_master->data, 1);
  104392. msleep(dev->pullup_duration);
  104393. + dev->bus_master->
  104394. + bitbang_pullup(dev->bus_master->data, 0);
  104395. + }
  104396. + } else {
  104397. + msleep(dev->pullup_duration);
  104398. + }
  104399. +
  104400. dev->pullup_duration = 0;
  104401. }
  104402. }
  104403. diff -Nur linux-3.17.5/drivers/watchdog/bcm2708_wdog.c linux-rpi/drivers/watchdog/bcm2708_wdog.c
  104404. --- linux-3.17.5/drivers/watchdog/bcm2708_wdog.c 1969-12-31 18:00:00.000000000 -0600
  104405. +++ linux-rpi/drivers/watchdog/bcm2708_wdog.c 2014-12-11 14:05:39.700418001 -0600
  104406. @@ -0,0 +1,382 @@
  104407. +/*
  104408. + * Broadcom BCM2708 watchdog driver.
  104409. + *
  104410. + * (c) Copyright 2010 Broadcom Europe Ltd
  104411. + *
  104412. + * This program is free software; you can redistribute it and/or
  104413. + * modify it under the terms of the GNU General Public License
  104414. + * as published by the Free Software Foundation; either version
  104415. + * 2 of the License, or (at your option) any later version.
  104416. + *
  104417. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  104418. + */
  104419. +
  104420. +#include <linux/interrupt.h>
  104421. +#include <linux/module.h>
  104422. +#include <linux/moduleparam.h>
  104423. +#include <linux/types.h>
  104424. +#include <linux/miscdevice.h>
  104425. +#include <linux/watchdog.h>
  104426. +#include <linux/fs.h>
  104427. +#include <linux/ioport.h>
  104428. +#include <linux/notifier.h>
  104429. +#include <linux/reboot.h>
  104430. +#include <linux/init.h>
  104431. +#include <linux/io.h>
  104432. +#include <linux/uaccess.h>
  104433. +#include <mach/platform.h>
  104434. +
  104435. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  104436. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  104437. +
  104438. +static unsigned long wdog_is_open;
  104439. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  104440. +static char expect_close;
  104441. +
  104442. +/*
  104443. + * Module parameters
  104444. + */
  104445. +
  104446. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  104447. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  104448. +
  104449. +module_param(heartbeat, int, 0);
  104450. +MODULE_PARM_DESC(heartbeat,
  104451. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  104452. + __MODULE_STRING(WD_TIMO) ")");
  104453. +
  104454. +static int nowayout = WATCHDOG_NOWAYOUT;
  104455. +module_param(nowayout, int, 0);
  104456. +MODULE_PARM_DESC(nowayout,
  104457. + "Watchdog cannot be stopped once started (default="
  104458. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  104459. +
  104460. +static DEFINE_SPINLOCK(wdog_lock);
  104461. +
  104462. +/**
  104463. + * Start the watchdog driver.
  104464. + */
  104465. +
  104466. +static int wdog_start(unsigned long timeout)
  104467. +{
  104468. + uint32_t cur;
  104469. + unsigned long flags;
  104470. + spin_lock_irqsave(&wdog_lock, flags);
  104471. +
  104472. + /* enable the watchdog */
  104473. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  104474. + __io_address(PM_WDOG));
  104475. + cur = ioread32(__io_address(PM_RSTC));
  104476. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  104477. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  104478. +
  104479. + spin_unlock_irqrestore(&wdog_lock, flags);
  104480. + return 0;
  104481. +}
  104482. +
  104483. +/**
  104484. + * Stop the watchdog driver.
  104485. + */
  104486. +
  104487. +static int wdog_stop(void)
  104488. +{
  104489. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  104490. + printk(KERN_INFO "watchdog stopped\n");
  104491. + return 0;
  104492. +}
  104493. +
  104494. +/**
  104495. + * Reload counter one with the watchdog heartbeat. We don't bother
  104496. + * reloading the cascade counter.
  104497. + */
  104498. +
  104499. +static void wdog_ping(void)
  104500. +{
  104501. + wdog_start(wdog_ticks);
  104502. +}
  104503. +
  104504. +/**
  104505. + * @t: the new heartbeat value that needs to be set.
  104506. + *
  104507. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  104508. + * value is incorrect we keep the old value and return -EINVAL. If
  104509. + * successful we return 0.
  104510. + */
  104511. +
  104512. +static int wdog_set_heartbeat(int t)
  104513. +{
  104514. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  104515. + return -EINVAL;
  104516. +
  104517. + heartbeat = t;
  104518. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  104519. + return 0;
  104520. +}
  104521. +
  104522. +/**
  104523. + * @file: file handle to the watchdog
  104524. + * @buf: buffer to write (unused as data does not matter here
  104525. + * @count: count of bytes
  104526. + * @ppos: pointer to the position to write. No seeks allowed
  104527. + *
  104528. + * A write to a watchdog device is defined as a keepalive signal.
  104529. + *
  104530. + * if 'nowayout' is set then normally a close() is ignored. But
  104531. + * if you write 'V' first then the close() will stop the timer.
  104532. + */
  104533. +
  104534. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  104535. + size_t count, loff_t *ppos)
  104536. +{
  104537. + if (count) {
  104538. + if (!nowayout) {
  104539. + size_t i;
  104540. +
  104541. + /* In case it was set long ago */
  104542. + expect_close = 0;
  104543. +
  104544. + for (i = 0; i != count; i++) {
  104545. + char c;
  104546. + if (get_user(c, buf + i))
  104547. + return -EFAULT;
  104548. + if (c == 'V')
  104549. + expect_close = 42;
  104550. + }
  104551. + }
  104552. + wdog_ping();
  104553. + }
  104554. + return count;
  104555. +}
  104556. +
  104557. +static int wdog_get_status(void)
  104558. +{
  104559. + unsigned long flags;
  104560. + int status = 0;
  104561. + spin_lock_irqsave(&wdog_lock, flags);
  104562. + /* FIXME: readback reset reason */
  104563. + spin_unlock_irqrestore(&wdog_lock, flags);
  104564. + return status;
  104565. +}
  104566. +
  104567. +static uint32_t wdog_get_remaining(void)
  104568. +{
  104569. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  104570. + return ret & PM_WDOG_TIME_SET;
  104571. +}
  104572. +
  104573. +/**
  104574. + * @file: file handle to the device
  104575. + * @cmd: watchdog command
  104576. + * @arg: argument pointer
  104577. + *
  104578. + * The watchdog API defines a common set of functions for all watchdogs
  104579. + * according to their available features. We only actually usefully support
  104580. + * querying capabilities and current status.
  104581. + */
  104582. +
  104583. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  104584. +{
  104585. + void __user *argp = (void __user *)arg;
  104586. + int __user *p = argp;
  104587. + int new_heartbeat;
  104588. + int status;
  104589. + int options;
  104590. + uint32_t remaining;
  104591. +
  104592. + struct watchdog_info ident = {
  104593. + .options = WDIOF_SETTIMEOUT|
  104594. + WDIOF_MAGICCLOSE|
  104595. + WDIOF_KEEPALIVEPING,
  104596. + .firmware_version = 1,
  104597. + .identity = "BCM2708",
  104598. + };
  104599. +
  104600. + switch (cmd) {
  104601. + case WDIOC_GETSUPPORT:
  104602. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  104603. + case WDIOC_GETSTATUS:
  104604. + status = wdog_get_status();
  104605. + return put_user(status, p);
  104606. + case WDIOC_GETBOOTSTATUS:
  104607. + return put_user(0, p);
  104608. + case WDIOC_KEEPALIVE:
  104609. + wdog_ping();
  104610. + return 0;
  104611. + case WDIOC_SETTIMEOUT:
  104612. + if (get_user(new_heartbeat, p))
  104613. + return -EFAULT;
  104614. + if (wdog_set_heartbeat(new_heartbeat))
  104615. + return -EINVAL;
  104616. + wdog_ping();
  104617. + /* Fall */
  104618. + case WDIOC_GETTIMEOUT:
  104619. + return put_user(heartbeat, p);
  104620. + case WDIOC_GETTIMELEFT:
  104621. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  104622. + return put_user(remaining, p);
  104623. + case WDIOC_SETOPTIONS:
  104624. + if (get_user(options, p))
  104625. + return -EFAULT;
  104626. + if (options & WDIOS_DISABLECARD)
  104627. + wdog_stop();
  104628. + if (options & WDIOS_ENABLECARD)
  104629. + wdog_start(wdog_ticks);
  104630. + return 0;
  104631. + default:
  104632. + return -ENOTTY;
  104633. + }
  104634. +}
  104635. +
  104636. +/**
  104637. + * @inode: inode of device
  104638. + * @file: file handle to device
  104639. + *
  104640. + * The watchdog device has been opened. The watchdog device is single
  104641. + * open and on opening we load the counters.
  104642. + */
  104643. +
  104644. +static int wdog_open(struct inode *inode, struct file *file)
  104645. +{
  104646. + if (test_and_set_bit(0, &wdog_is_open))
  104647. + return -EBUSY;
  104648. + /*
  104649. + * Activate
  104650. + */
  104651. + wdog_start(wdog_ticks);
  104652. + return nonseekable_open(inode, file);
  104653. +}
  104654. +
  104655. +/**
  104656. + * @inode: inode to board
  104657. + * @file: file handle to board
  104658. + *
  104659. + * The watchdog has a configurable API. There is a religious dispute
  104660. + * between people who want their watchdog to be able to shut down and
  104661. + * those who want to be sure if the watchdog manager dies the machine
  104662. + * reboots. In the former case we disable the counters, in the latter
  104663. + * case you have to open it again very soon.
  104664. + */
  104665. +
  104666. +static int wdog_release(struct inode *inode, struct file *file)
  104667. +{
  104668. + if (expect_close == 42) {
  104669. + wdog_stop();
  104670. + } else {
  104671. + printk(KERN_CRIT
  104672. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  104673. + wdog_ping();
  104674. + }
  104675. + clear_bit(0, &wdog_is_open);
  104676. + expect_close = 0;
  104677. + return 0;
  104678. +}
  104679. +
  104680. +/**
  104681. + * @this: our notifier block
  104682. + * @code: the event being reported
  104683. + * @unused: unused
  104684. + *
  104685. + * Our notifier is called on system shutdowns. Turn the watchdog
  104686. + * off so that it does not fire during the next reboot.
  104687. + */
  104688. +
  104689. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  104690. + void *unused)
  104691. +{
  104692. + if (code == SYS_DOWN || code == SYS_HALT)
  104693. + wdog_stop();
  104694. + return NOTIFY_DONE;
  104695. +}
  104696. +
  104697. +/*
  104698. + * Kernel Interfaces
  104699. + */
  104700. +
  104701. +
  104702. +static const struct file_operations wdog_fops = {
  104703. + .owner = THIS_MODULE,
  104704. + .llseek = no_llseek,
  104705. + .write = wdog_write,
  104706. + .unlocked_ioctl = wdog_ioctl,
  104707. + .open = wdog_open,
  104708. + .release = wdog_release,
  104709. +};
  104710. +
  104711. +static struct miscdevice wdog_miscdev = {
  104712. + .minor = WATCHDOG_MINOR,
  104713. + .name = "watchdog",
  104714. + .fops = &wdog_fops,
  104715. +};
  104716. +
  104717. +/*
  104718. + * The WDT card needs to learn about soft shutdowns in order to
  104719. + * turn the timebomb registers off.
  104720. + */
  104721. +
  104722. +static struct notifier_block wdog_notifier = {
  104723. + .notifier_call = wdog_notify_sys,
  104724. +};
  104725. +
  104726. +/**
  104727. + * cleanup_module:
  104728. + *
  104729. + * Unload the watchdog. You cannot do this with any file handles open.
  104730. + * If your watchdog is set to continue ticking on close and you unload
  104731. + * it, well it keeps ticking. We won't get the interrupt but the board
  104732. + * will not touch PC memory so all is fine. You just have to load a new
  104733. + * module in 60 seconds or reboot.
  104734. + */
  104735. +
  104736. +static void __exit wdog_exit(void)
  104737. +{
  104738. + misc_deregister(&wdog_miscdev);
  104739. + unregister_reboot_notifier(&wdog_notifier);
  104740. +}
  104741. +
  104742. +static int __init wdog_init(void)
  104743. +{
  104744. + int ret;
  104745. +
  104746. + /* Check that the heartbeat value is within it's range;
  104747. + if not reset to the default */
  104748. + if (wdog_set_heartbeat(heartbeat)) {
  104749. + wdog_set_heartbeat(WD_TIMO);
  104750. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  104751. + "0 < heartbeat < %d, using %d\n",
  104752. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  104753. + WD_TIMO);
  104754. + }
  104755. +
  104756. + ret = register_reboot_notifier(&wdog_notifier);
  104757. + if (ret) {
  104758. + printk(KERN_ERR
  104759. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  104760. + goto out_reboot;
  104761. + }
  104762. +
  104763. + ret = misc_register(&wdog_miscdev);
  104764. + if (ret) {
  104765. + printk(KERN_ERR
  104766. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  104767. + WATCHDOG_MINOR, ret);
  104768. + goto out_misc;
  104769. + }
  104770. +
  104771. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  104772. + heartbeat, nowayout);
  104773. + return 0;
  104774. +
  104775. +out_misc:
  104776. + unregister_reboot_notifier(&wdog_notifier);
  104777. +out_reboot:
  104778. + return ret;
  104779. +}
  104780. +
  104781. +module_init(wdog_init);
  104782. +module_exit(wdog_exit);
  104783. +
  104784. +MODULE_AUTHOR("Luke Diamand");
  104785. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  104786. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  104787. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  104788. +MODULE_LICENSE("GPL");
  104789. diff -Nur linux-3.17.5/drivers/watchdog/Kconfig linux-rpi/drivers/watchdog/Kconfig
  104790. --- linux-3.17.5/drivers/watchdog/Kconfig 2014-12-06 17:57:59.000000000 -0600
  104791. +++ linux-rpi/drivers/watchdog/Kconfig 2014-12-11 14:05:39.700418001 -0600
  104792. @@ -413,6 +413,12 @@
  104793. To compile this driver as a module, choose M here: the
  104794. module will be called retu_wdt.
  104795. +config BCM2708_WDT
  104796. + tristate "BCM2708 Watchdog"
  104797. + depends on ARCH_BCM2708
  104798. + help
  104799. + Enables BCM2708 watchdog support.
  104800. +
  104801. config MOXART_WDT
  104802. tristate "MOXART watchdog"
  104803. depends on ARCH_MOXART
  104804. diff -Nur linux-3.17.5/drivers/watchdog/Makefile linux-rpi/drivers/watchdog/Makefile
  104805. --- linux-3.17.5/drivers/watchdog/Makefile 2014-12-06 17:57:59.000000000 -0600
  104806. +++ linux-rpi/drivers/watchdog/Makefile 2014-12-11 14:05:39.700418001 -0600
  104807. @@ -54,6 +54,7 @@
  104808. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  104809. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  104810. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  104811. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  104812. obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
  104813. obj-$(CONFIG_MOXART_WDT) += moxart_wdt.o
  104814. obj-$(CONFIG_SIRFSOC_WATCHDOG) += sirfsoc_wdt.o
  104815. diff -Nur linux-3.17.5/include/linux/broadcom/vc_cma.h linux-rpi/include/linux/broadcom/vc_cma.h
  104816. --- linux-3.17.5/include/linux/broadcom/vc_cma.h 1969-12-31 18:00:00.000000000 -0600
  104817. +++ linux-rpi/include/linux/broadcom/vc_cma.h 2014-12-11 14:02:56.672418001 -0600
  104818. @@ -0,0 +1,29 @@
  104819. +/*****************************************************************************
  104820. +* Copyright 2012 Broadcom Corporation. All rights reserved.
  104821. +*
  104822. +* Unless you and Broadcom execute a separate written software license
  104823. +* agreement governing use of this software, this software is licensed to you
  104824. +* under the terms of the GNU General Public License version 2, available at
  104825. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  104826. +*
  104827. +* Notwithstanding the above, under no circumstances may you combine this
  104828. +* software in any way with any other Broadcom software provided under a
  104829. +* license other than the GPL, without Broadcom's express prior written
  104830. +* consent.
  104831. +*****************************************************************************/
  104832. +
  104833. +#if !defined( VC_CMA_H )
  104834. +#define VC_CMA_H
  104835. +
  104836. +#include <linux/ioctl.h>
  104837. +
  104838. +#define VC_CMA_IOC_MAGIC 0xc5
  104839. +
  104840. +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
  104841. +
  104842. +#ifdef __KERNEL__
  104843. +extern void __init vc_cma_early_init(void);
  104844. +extern void __init vc_cma_reserve(void);
  104845. +#endif
  104846. +
  104847. +#endif /* VC_CMA_H */
  104848. diff -Nur linux-3.17.5/include/linux/mmc/host.h linux-rpi/include/linux/mmc/host.h
  104849. --- linux-3.17.5/include/linux/mmc/host.h 2014-12-06 17:57:59.000000000 -0600
  104850. +++ linux-rpi/include/linux/mmc/host.h 2014-12-11 14:05:40.016418001 -0600
  104851. @@ -283,6 +283,7 @@
  104852. #define MMC_CAP2_HS400 (MMC_CAP2_HS400_1_8V | \
  104853. MMC_CAP2_HS400_1_2V)
  104854. #define MMC_CAP2_SDIO_IRQ_NOTHREAD (1 << 17)
  104855. +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
  104856. mmc_pm_flag_t pm_caps; /* supported pm features */
  104857. diff -Nur linux-3.17.5/include/linux/mmc/sdhci.h linux-rpi/include/linux/mmc/sdhci.h
  104858. --- linux-3.17.5/include/linux/mmc/sdhci.h 2014-12-06 17:57:59.000000000 -0600
  104859. +++ linux-rpi/include/linux/mmc/sdhci.h 2014-12-11 14:05:40.016418001 -0600
  104860. @@ -128,6 +128,7 @@
  104861. #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  104862. #define SDHCI_SDR104_NEEDS_TUNING (1<<10) /* SDR104/HS200 needs tuning */
  104863. #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
  104864. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  104865. unsigned int version; /* SDHCI spec. version */
  104866. diff -Nur linux-3.17.5/include/linux/platform_data/bcm2708.h linux-rpi/include/linux/platform_data/bcm2708.h
  104867. --- linux-3.17.5/include/linux/platform_data/bcm2708.h 1969-12-31 18:00:00.000000000 -0600
  104868. +++ linux-rpi/include/linux/platform_data/bcm2708.h 2014-12-11 14:02:56.904418001 -0600
  104869. @@ -0,0 +1,23 @@
  104870. +/*
  104871. + * include/linux/platform_data/bcm2708.h
  104872. + *
  104873. + * This program is free software; you can redistribute it and/or modify
  104874. + * it under the terms of the GNU General Public License version 2 as
  104875. + * published by the Free Software Foundation.
  104876. + *
  104877. + * (C) 2014 Julian Scheel <julian@jusst.de>
  104878. + *
  104879. + */
  104880. +#ifndef __BCM2708_H_
  104881. +#define __BCM2708_H_
  104882. +
  104883. +typedef enum {
  104884. + BCM2708_PULL_OFF,
  104885. + BCM2708_PULL_UP,
  104886. + BCM2708_PULL_DOWN
  104887. +} bcm2708_gpio_pull_t;
  104888. +
  104889. +extern int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
  104890. + bcm2708_gpio_pull_t value);
  104891. +
  104892. +#endif
  104893. diff -Nur linux-3.17.5/include/linux/vmstat.h linux-rpi/include/linux/vmstat.h
  104894. --- linux-3.17.5/include/linux/vmstat.h 2014-12-06 17:57:59.000000000 -0600
  104895. +++ linux-rpi/include/linux/vmstat.h 2014-12-11 14:05:40.056418001 -0600
  104896. @@ -241,7 +241,11 @@
  104897. static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  104898. {
  104899. atomic_long_dec(&zone->vm_stat[item]);
  104900. + if (item == NR_FILE_DIRTY && unlikely(atomic_long_read(&zone->vm_stat[item]) < 0))
  104901. + atomic_long_set(&zone->vm_stat[item], 0);
  104902. atomic_long_dec(&vm_stat[item]);
  104903. + if (item == NR_FILE_DIRTY && unlikely(atomic_long_read(&vm_stat[item]) < 0))
  104904. + atomic_long_set(&vm_stat[item], 0);
  104905. }
  104906. static inline void __inc_zone_page_state(struct page *page,
  104907. diff -Nur linux-3.17.5/include/uapi/linux/fb.h linux-rpi/include/uapi/linux/fb.h
  104908. --- linux-3.17.5/include/uapi/linux/fb.h 2014-12-06 17:57:59.000000000 -0600
  104909. +++ linux-rpi/include/uapi/linux/fb.h 2014-12-11 14:02:57.044418001 -0600
  104910. @@ -34,6 +34,11 @@
  104911. #define FBIOPUT_MODEINFO 0x4617
  104912. #define FBIOGET_DISPINFO 0x4618
  104913. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  104914. +/*
  104915. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  104916. + * be concurrently added to the mainline kernel
  104917. + */
  104918. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  104919. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  104920. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  104921. diff -Nur linux-3.17.5/kernel/cgroup.c linux-rpi/kernel/cgroup.c
  104922. --- linux-3.17.5/kernel/cgroup.c 2014-12-06 17:57:59.000000000 -0600
  104923. +++ linux-rpi/kernel/cgroup.c 2014-12-11 14:05:40.120418001 -0600
  104924. @@ -5400,6 +5400,29 @@
  104925. }
  104926. __setup("cgroup_disable=", cgroup_disable);
  104927. +static int __init cgroup_enable(char *str)
  104928. +{
  104929. + struct cgroup_subsys *ss;
  104930. + char *token;
  104931. + int i;
  104932. +
  104933. + while ((token = strsep(&str, ",")) != NULL) {
  104934. + if (!*token)
  104935. + continue;
  104936. +
  104937. + for_each_subsys(ss, i) {
  104938. + if (!strcmp(token, ss->name)) {
  104939. + ss->disabled = 0;
  104940. + printk(KERN_INFO "Enabling %s control group"
  104941. + " subsystem\n", ss->name);
  104942. + break;
  104943. + }
  104944. + }
  104945. + }
  104946. + return 1;
  104947. +}
  104948. +__setup("cgroup_enable=", cgroup_enable);
  104949. +
  104950. static int __init cgroup_set_legacy_files_on_dfl(char *str)
  104951. {
  104952. printk("cgroup: using legacy files on the default hierarchy\n");
  104953. diff -Nur linux-3.17.5/mm/memcontrol.c linux-rpi/mm/memcontrol.c
  104954. --- linux-3.17.5/mm/memcontrol.c 2014-12-06 17:57:59.000000000 -0600
  104955. +++ linux-rpi/mm/memcontrol.c 2014-12-11 14:05:40.188418001 -0600
  104956. @@ -6343,6 +6343,7 @@
  104957. .bind = mem_cgroup_bind,
  104958. .legacy_cftypes = mem_cgroup_files,
  104959. .early_init = 0,
  104960. + .disabled = 1,
  104961. };
  104962. #ifdef CONFIG_MEMCG_SWAP
  104963. diff -Nur linux-3.17.5/sound/arm/bcm2835.c linux-rpi/sound/arm/bcm2835.c
  104964. --- linux-3.17.5/sound/arm/bcm2835.c 1969-12-31 18:00:00.000000000 -0600
  104965. +++ linux-rpi/sound/arm/bcm2835.c 2014-12-11 14:05:40.436418001 -0600
  104966. @@ -0,0 +1,420 @@
  104967. +/*****************************************************************************
  104968. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  104969. +*
  104970. +* Unless you and Broadcom execute a separate written software license
  104971. +* agreement governing use of this software, this software is licensed to you
  104972. +* under the terms of the GNU General Public License version 2, available at
  104973. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  104974. +*
  104975. +* Notwithstanding the above, under no circumstances may you combine this
  104976. +* software in any way with any other Broadcom software provided under a
  104977. +* license other than the GPL, without Broadcom's express prior written
  104978. +* consent.
  104979. +*****************************************************************************/
  104980. +
  104981. +#include <linux/platform_device.h>
  104982. +
  104983. +#include <linux/init.h>
  104984. +#include <linux/slab.h>
  104985. +#include <linux/module.h>
  104986. +
  104987. +#include "bcm2835.h"
  104988. +
  104989. +/* module parameters (see "Module Parameters") */
  104990. +/* SNDRV_CARDS: maximum number of cards supported by this module */
  104991. +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
  104992. +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
  104993. +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
  104994. +
  104995. +/* HACKY global pointers needed for successive probes to work : ssp
  104996. + * But compared against the changes we will have to do in VC audio_ipc code
  104997. + * to export 8 audio_ipc devices as a single IPC device and then monitor all
  104998. + * four devices in a thread, this gets things done quickly and should be easier
  104999. + * to debug if we run into issues
  105000. + */
  105001. +
  105002. +static struct snd_card *g_card = NULL;
  105003. +static bcm2835_chip_t *g_chip = NULL;
  105004. +
  105005. +static int snd_bcm2835_free(bcm2835_chip_t * chip)
  105006. +{
  105007. + kfree(chip);
  105008. + return 0;
  105009. +}
  105010. +
  105011. +/* component-destructor
  105012. + * (see "Management of Cards and Components")
  105013. + */
  105014. +static int snd_bcm2835_dev_free(struct snd_device *device)
  105015. +{
  105016. + return snd_bcm2835_free(device->device_data);
  105017. +}
  105018. +
  105019. +/* chip-specific constructor
  105020. + * (see "Management of Cards and Components")
  105021. + */
  105022. +static int snd_bcm2835_create(struct snd_card *card,
  105023. + struct platform_device *pdev,
  105024. + bcm2835_chip_t ** rchip)
  105025. +{
  105026. + bcm2835_chip_t *chip;
  105027. + int err;
  105028. + static struct snd_device_ops ops = {
  105029. + .dev_free = snd_bcm2835_dev_free,
  105030. + };
  105031. +
  105032. + *rchip = NULL;
  105033. +
  105034. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  105035. + if (chip == NULL)
  105036. + return -ENOMEM;
  105037. +
  105038. + chip->card = card;
  105039. +
  105040. + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  105041. + if (err < 0) {
  105042. + snd_bcm2835_free(chip);
  105043. + return err;
  105044. + }
  105045. +
  105046. + *rchip = chip;
  105047. + return 0;
  105048. +}
  105049. +
  105050. +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
  105051. +{
  105052. + static int dev;
  105053. + bcm2835_chip_t *chip;
  105054. + struct snd_card *card;
  105055. + int err;
  105056. +
  105057. + if (dev >= MAX_SUBSTREAMS)
  105058. + return -ENODEV;
  105059. +
  105060. + if (!enable[dev]) {
  105061. + dev++;
  105062. + return -ENOENT;
  105063. + }
  105064. +
  105065. + if (dev > 0)
  105066. + goto add_register_map;
  105067. +
  105068. + err = snd_card_new(NULL, index[dev], id[dev], THIS_MODULE, 0, &g_card);
  105069. + if (err < 0)
  105070. + goto out;
  105071. +
  105072. + snd_card_set_dev(g_card, &pdev->dev);
  105073. + strcpy(g_card->driver, "bcm2835");
  105074. + strcpy(g_card->shortname, "bcm2835 ALSA");
  105075. + sprintf(g_card->longname, "%s", g_card->shortname);
  105076. +
  105077. + err = snd_bcm2835_create(g_card, pdev, &chip);
  105078. + if (err < 0) {
  105079. + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
  105080. + goto out_bcm2835_create;
  105081. + }
  105082. +
  105083. + g_chip = chip;
  105084. + err = snd_bcm2835_new_pcm(chip);
  105085. + if (err < 0) {
  105086. + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
  105087. + goto out_bcm2835_new_pcm;
  105088. + }
  105089. +
  105090. + err = snd_bcm2835_new_spdif_pcm(chip);
  105091. + if (err < 0) {
  105092. + dev_err(&pdev->dev, "Failed to create new BCM2835 spdif pcm device\n");
  105093. + goto out_bcm2835_new_spdif;
  105094. + }
  105095. +
  105096. + err = snd_bcm2835_new_ctl(chip);
  105097. + if (err < 0) {
  105098. + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
  105099. + goto out_bcm2835_new_ctl;
  105100. + }
  105101. +
  105102. +add_register_map:
  105103. + card = g_card;
  105104. + chip = g_chip;
  105105. +
  105106. + BUG_ON(!(card && chip));
  105107. +
  105108. + chip->avail_substreams |= (1 << dev);
  105109. + chip->pdev[dev] = pdev;
  105110. +
  105111. + if (dev == 0) {
  105112. + err = snd_card_register(card);
  105113. + if (err < 0) {
  105114. + dev_err(&pdev->dev,
  105115. + "Failed to register bcm2835 ALSA card \n");
  105116. + goto out_card_register;
  105117. + }
  105118. + platform_set_drvdata(pdev, card);
  105119. + audio_info("bcm2835 ALSA card created!\n");
  105120. + } else {
  105121. + audio_info("bcm2835 ALSA chip created!\n");
  105122. + platform_set_drvdata(pdev, (void *)dev);
  105123. + }
  105124. +
  105125. + dev++;
  105126. +
  105127. + return 0;
  105128. +
  105129. +out_card_register:
  105130. +out_bcm2835_new_ctl:
  105131. +out_bcm2835_new_spdif:
  105132. +out_bcm2835_new_pcm:
  105133. +out_bcm2835_create:
  105134. + BUG_ON(!g_card);
  105135. + if (snd_card_free(g_card))
  105136. + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
  105137. + g_card = NULL;
  105138. +out:
  105139. + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
  105140. + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
  105141. + return err;
  105142. +}
  105143. +
  105144. +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
  105145. +{
  105146. + uint32_t idx;
  105147. + void *drv_data;
  105148. +
  105149. + drv_data = platform_get_drvdata(pdev);
  105150. +
  105151. + if (drv_data == (void *)g_card) {
  105152. + /* This is the card device */
  105153. + snd_card_free((struct snd_card *)drv_data);
  105154. + g_card = NULL;
  105155. + g_chip = NULL;
  105156. + } else {
  105157. + idx = (uint32_t) drv_data;
  105158. + if (g_card != NULL) {
  105159. + BUG_ON(!g_chip);
  105160. + /* We pass chip device numbers in audio ipc devices
  105161. + * other than the one we registered our card with
  105162. + */
  105163. + idx = (uint32_t) drv_data;
  105164. + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
  105165. + g_chip->avail_substreams &= ~(1 << idx);
  105166. + /* There should be atleast one substream registered
  105167. + * after we are done here, as it wil be removed when
  105168. + * the *remove* is called for the card device
  105169. + */
  105170. + BUG_ON(!g_chip->avail_substreams);
  105171. + }
  105172. + }
  105173. +
  105174. + platform_set_drvdata(pdev, NULL);
  105175. +
  105176. + return 0;
  105177. +}
  105178. +
  105179. +#ifdef CONFIG_PM
  105180. +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
  105181. + pm_message_t state)
  105182. +{
  105183. + return 0;
  105184. +}
  105185. +
  105186. +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
  105187. +{
  105188. + return 0;
  105189. +}
  105190. +
  105191. +#endif
  105192. +
  105193. +static struct platform_driver bcm2835_alsa0_driver = {
  105194. + .probe = snd_bcm2835_alsa_probe,
  105195. + .remove = snd_bcm2835_alsa_remove,
  105196. +#ifdef CONFIG_PM
  105197. + .suspend = snd_bcm2835_alsa_suspend,
  105198. + .resume = snd_bcm2835_alsa_resume,
  105199. +#endif
  105200. + .driver = {
  105201. + .name = "bcm2835_AUD0",
  105202. + .owner = THIS_MODULE,
  105203. + },
  105204. +};
  105205. +
  105206. +static struct platform_driver bcm2835_alsa1_driver = {
  105207. + .probe = snd_bcm2835_alsa_probe,
  105208. + .remove = snd_bcm2835_alsa_remove,
  105209. +#ifdef CONFIG_PM
  105210. + .suspend = snd_bcm2835_alsa_suspend,
  105211. + .resume = snd_bcm2835_alsa_resume,
  105212. +#endif
  105213. + .driver = {
  105214. + .name = "bcm2835_AUD1",
  105215. + .owner = THIS_MODULE,
  105216. + },
  105217. +};
  105218. +
  105219. +static struct platform_driver bcm2835_alsa2_driver = {
  105220. + .probe = snd_bcm2835_alsa_probe,
  105221. + .remove = snd_bcm2835_alsa_remove,
  105222. +#ifdef CONFIG_PM
  105223. + .suspend = snd_bcm2835_alsa_suspend,
  105224. + .resume = snd_bcm2835_alsa_resume,
  105225. +#endif
  105226. + .driver = {
  105227. + .name = "bcm2835_AUD2",
  105228. + .owner = THIS_MODULE,
  105229. + },
  105230. +};
  105231. +
  105232. +static struct platform_driver bcm2835_alsa3_driver = {
  105233. + .probe = snd_bcm2835_alsa_probe,
  105234. + .remove = snd_bcm2835_alsa_remove,
  105235. +#ifdef CONFIG_PM
  105236. + .suspend = snd_bcm2835_alsa_suspend,
  105237. + .resume = snd_bcm2835_alsa_resume,
  105238. +#endif
  105239. + .driver = {
  105240. + .name = "bcm2835_AUD3",
  105241. + .owner = THIS_MODULE,
  105242. + },
  105243. +};
  105244. +
  105245. +static struct platform_driver bcm2835_alsa4_driver = {
  105246. + .probe = snd_bcm2835_alsa_probe,
  105247. + .remove = snd_bcm2835_alsa_remove,
  105248. +#ifdef CONFIG_PM
  105249. + .suspend = snd_bcm2835_alsa_suspend,
  105250. + .resume = snd_bcm2835_alsa_resume,
  105251. +#endif
  105252. + .driver = {
  105253. + .name = "bcm2835_AUD4",
  105254. + .owner = THIS_MODULE,
  105255. + },
  105256. +};
  105257. +
  105258. +static struct platform_driver bcm2835_alsa5_driver = {
  105259. + .probe = snd_bcm2835_alsa_probe,
  105260. + .remove = snd_bcm2835_alsa_remove,
  105261. +#ifdef CONFIG_PM
  105262. + .suspend = snd_bcm2835_alsa_suspend,
  105263. + .resume = snd_bcm2835_alsa_resume,
  105264. +#endif
  105265. + .driver = {
  105266. + .name = "bcm2835_AUD5",
  105267. + .owner = THIS_MODULE,
  105268. + },
  105269. +};
  105270. +
  105271. +static struct platform_driver bcm2835_alsa6_driver = {
  105272. + .probe = snd_bcm2835_alsa_probe,
  105273. + .remove = snd_bcm2835_alsa_remove,
  105274. +#ifdef CONFIG_PM
  105275. + .suspend = snd_bcm2835_alsa_suspend,
  105276. + .resume = snd_bcm2835_alsa_resume,
  105277. +#endif
  105278. + .driver = {
  105279. + .name = "bcm2835_AUD6",
  105280. + .owner = THIS_MODULE,
  105281. + },
  105282. +};
  105283. +
  105284. +static struct platform_driver bcm2835_alsa7_driver = {
  105285. + .probe = snd_bcm2835_alsa_probe,
  105286. + .remove = snd_bcm2835_alsa_remove,
  105287. +#ifdef CONFIG_PM
  105288. + .suspend = snd_bcm2835_alsa_suspend,
  105289. + .resume = snd_bcm2835_alsa_resume,
  105290. +#endif
  105291. + .driver = {
  105292. + .name = "bcm2835_AUD7",
  105293. + .owner = THIS_MODULE,
  105294. + },
  105295. +};
  105296. +
  105297. +static int bcm2835_alsa_device_init(void)
  105298. +{
  105299. + int err;
  105300. + err = platform_driver_register(&bcm2835_alsa0_driver);
  105301. + if (err) {
  105302. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105303. + goto out;
  105304. + }
  105305. +
  105306. + err = platform_driver_register(&bcm2835_alsa1_driver);
  105307. + if (err) {
  105308. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105309. + goto unregister_0;
  105310. + }
  105311. +
  105312. + err = platform_driver_register(&bcm2835_alsa2_driver);
  105313. + if (err) {
  105314. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105315. + goto unregister_1;
  105316. + }
  105317. +
  105318. + err = platform_driver_register(&bcm2835_alsa3_driver);
  105319. + if (err) {
  105320. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105321. + goto unregister_2;
  105322. + }
  105323. +
  105324. + err = platform_driver_register(&bcm2835_alsa4_driver);
  105325. + if (err) {
  105326. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105327. + goto unregister_3;
  105328. + }
  105329. +
  105330. + err = platform_driver_register(&bcm2835_alsa5_driver);
  105331. + if (err) {
  105332. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105333. + goto unregister_4;
  105334. + }
  105335. +
  105336. + err = platform_driver_register(&bcm2835_alsa6_driver);
  105337. + if (err) {
  105338. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105339. + goto unregister_5;
  105340. + }
  105341. +
  105342. + err = platform_driver_register(&bcm2835_alsa7_driver);
  105343. + if (err) {
  105344. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  105345. + goto unregister_6;
  105346. + }
  105347. +
  105348. + return 0;
  105349. +
  105350. +unregister_6:
  105351. + platform_driver_unregister(&bcm2835_alsa6_driver);
  105352. +unregister_5:
  105353. + platform_driver_unregister(&bcm2835_alsa5_driver);
  105354. +unregister_4:
  105355. + platform_driver_unregister(&bcm2835_alsa4_driver);
  105356. +unregister_3:
  105357. + platform_driver_unregister(&bcm2835_alsa3_driver);
  105358. +unregister_2:
  105359. + platform_driver_unregister(&bcm2835_alsa2_driver);
  105360. +unregister_1:
  105361. + platform_driver_unregister(&bcm2835_alsa1_driver);
  105362. +unregister_0:
  105363. + platform_driver_unregister(&bcm2835_alsa0_driver);
  105364. +out:
  105365. + return err;
  105366. +}
  105367. +
  105368. +static void bcm2835_alsa_device_exit(void)
  105369. +{
  105370. + platform_driver_unregister(&bcm2835_alsa0_driver);
  105371. + platform_driver_unregister(&bcm2835_alsa1_driver);
  105372. + platform_driver_unregister(&bcm2835_alsa2_driver);
  105373. + platform_driver_unregister(&bcm2835_alsa3_driver);
  105374. + platform_driver_unregister(&bcm2835_alsa4_driver);
  105375. + platform_driver_unregister(&bcm2835_alsa5_driver);
  105376. + platform_driver_unregister(&bcm2835_alsa6_driver);
  105377. + platform_driver_unregister(&bcm2835_alsa7_driver);
  105378. +}
  105379. +
  105380. +late_initcall(bcm2835_alsa_device_init);
  105381. +module_exit(bcm2835_alsa_device_exit);
  105382. +
  105383. +MODULE_AUTHOR("Dom Cobley");
  105384. +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
  105385. +MODULE_LICENSE("GPL");
  105386. +MODULE_ALIAS("platform:bcm2835_alsa");
  105387. diff -Nur linux-3.17.5/sound/arm/bcm2835-ctl.c linux-rpi/sound/arm/bcm2835-ctl.c
  105388. --- linux-3.17.5/sound/arm/bcm2835-ctl.c 1969-12-31 18:00:00.000000000 -0600
  105389. +++ linux-rpi/sound/arm/bcm2835-ctl.c 2014-12-11 14:02:57.788418001 -0600
  105390. @@ -0,0 +1,323 @@
  105391. +/*****************************************************************************
  105392. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  105393. +*
  105394. +* Unless you and Broadcom execute a separate written software license
  105395. +* agreement governing use of this software, this software is licensed to you
  105396. +* under the terms of the GNU General Public License version 2, available at
  105397. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  105398. +*
  105399. +* Notwithstanding the above, under no circumstances may you combine this
  105400. +* software in any way with any other Broadcom software provided under a
  105401. +* license other than the GPL, without Broadcom's express prior written
  105402. +* consent.
  105403. +*****************************************************************************/
  105404. +
  105405. +#include <linux/platform_device.h>
  105406. +#include <linux/init.h>
  105407. +#include <linux/io.h>
  105408. +#include <linux/jiffies.h>
  105409. +#include <linux/slab.h>
  105410. +#include <linux/time.h>
  105411. +#include <linux/wait.h>
  105412. +#include <linux/delay.h>
  105413. +#include <linux/moduleparam.h>
  105414. +#include <linux/sched.h>
  105415. +
  105416. +#include <sound/core.h>
  105417. +#include <sound/control.h>
  105418. +#include <sound/pcm.h>
  105419. +#include <sound/pcm_params.h>
  105420. +#include <sound/rawmidi.h>
  105421. +#include <sound/initval.h>
  105422. +#include <sound/tlv.h>
  105423. +#include <sound/asoundef.h>
  105424. +
  105425. +#include "bcm2835.h"
  105426. +
  105427. +/* volume maximum and minimum in terms of 0.01dB */
  105428. +#define CTRL_VOL_MAX 400
  105429. +#define CTRL_VOL_MIN -10239 /* originally -10240 */
  105430. +
  105431. +
  105432. +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
  105433. + struct snd_ctl_elem_info *uinfo)
  105434. +{
  105435. + audio_info(" ... IN\n");
  105436. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  105437. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  105438. + uinfo->count = 1;
  105439. + uinfo->value.integer.min = CTRL_VOL_MIN;
  105440. + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
  105441. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  105442. + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  105443. + uinfo->count = 1;
  105444. + uinfo->value.integer.min = 0;
  105445. + uinfo->value.integer.max = 1;
  105446. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  105447. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  105448. + uinfo->count = 1;
  105449. + uinfo->value.integer.min = 0;
  105450. + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
  105451. + }
  105452. + audio_info(" ... OUT\n");
  105453. + return 0;
  105454. +}
  105455. +
  105456. +/* toggles mute on or off depending on the value of nmute, and returns
  105457. + * 1 if the mute value was changed, otherwise 0
  105458. + */
  105459. +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
  105460. +{
  105461. + /* if settings are ok, just return 0 */
  105462. + if(chip->mute == nmute)
  105463. + return 0;
  105464. +
  105465. + /* if the sound is muted then we need to unmute */
  105466. + if(chip->mute == CTRL_VOL_MUTE)
  105467. + {
  105468. + chip->volume = chip->old_volume; /* copy the old volume back */
  105469. + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  105470. + }
  105471. + else /* otherwise we mute */
  105472. + {
  105473. + chip->old_volume = chip->volume;
  105474. + chip->volume = 26214; /* set volume to minimum level AKA mute */
  105475. + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  105476. + }
  105477. +
  105478. + chip->mute = nmute;
  105479. + return 1;
  105480. +}
  105481. +
  105482. +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
  105483. + struct snd_ctl_elem_value *ucontrol)
  105484. +{
  105485. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  105486. +
  105487. + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
  105488. +
  105489. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
  105490. + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
  105491. + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
  105492. + ucontrol->value.integer.value[0] = chip->mute;
  105493. + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
  105494. + ucontrol->value.integer.value[0] = chip->dest;
  105495. +
  105496. + return 0;
  105497. +}
  105498. +
  105499. +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
  105500. + struct snd_ctl_elem_value *ucontrol)
  105501. +{
  105502. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  105503. + int changed = 0;
  105504. +
  105505. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  105506. + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
  105507. + if (chip->mute == CTRL_VOL_MUTE) {
  105508. + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
  105509. + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
  105510. + }
  105511. + if (changed
  105512. + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
  105513. +
  105514. + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
  105515. + changed = 1;
  105516. + }
  105517. +
  105518. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  105519. + /* Now implemented */
  105520. + audio_info(" Mute attempted\n");
  105521. + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
  105522. +
  105523. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  105524. + if (ucontrol->value.integer.value[0] != chip->dest) {
  105525. + chip->dest = ucontrol->value.integer.value[0];
  105526. + changed = 1;
  105527. + }
  105528. + }
  105529. +
  105530. + if (changed) {
  105531. + if (bcm2835_audio_set_ctls(chip))
  105532. + printk(KERN_ERR "Failed to set ALSA controls..\n");
  105533. + }
  105534. +
  105535. + return changed;
  105536. +}
  105537. +
  105538. +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
  105539. +
  105540. +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
  105541. + {
  105542. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  105543. + .name = "PCM Playback Volume",
  105544. + .index = 0,
  105545. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  105546. + .private_value = PCM_PLAYBACK_VOLUME,
  105547. + .info = snd_bcm2835_ctl_info,
  105548. + .get = snd_bcm2835_ctl_get,
  105549. + .put = snd_bcm2835_ctl_put,
  105550. + .count = 1,
  105551. + .tlv = {.p = snd_bcm2835_db_scale}
  105552. + },
  105553. + {
  105554. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  105555. + .name = "PCM Playback Switch",
  105556. + .index = 0,
  105557. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  105558. + .private_value = PCM_PLAYBACK_MUTE,
  105559. + .info = snd_bcm2835_ctl_info,
  105560. + .get = snd_bcm2835_ctl_get,
  105561. + .put = snd_bcm2835_ctl_put,
  105562. + .count = 1,
  105563. + },
  105564. + {
  105565. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  105566. + .name = "PCM Playback Route",
  105567. + .index = 0,
  105568. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  105569. + .private_value = PCM_PLAYBACK_DEVICE,
  105570. + .info = snd_bcm2835_ctl_info,
  105571. + .get = snd_bcm2835_ctl_get,
  105572. + .put = snd_bcm2835_ctl_put,
  105573. + .count = 1,
  105574. + },
  105575. +};
  105576. +
  105577. +static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol,
  105578. + struct snd_ctl_elem_info *uinfo)
  105579. +{
  105580. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  105581. + uinfo->count = 1;
  105582. + return 0;
  105583. +}
  105584. +
  105585. +static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol,
  105586. + struct snd_ctl_elem_value *ucontrol)
  105587. +{
  105588. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  105589. + int i;
  105590. +
  105591. + for (i = 0; i < 4; i++)
  105592. + ucontrol->value.iec958.status[i] =
  105593. + (chip->spdif_status >> (i * 8)) && 0xff;
  105594. +
  105595. + return 0;
  105596. +}
  105597. +
  105598. +static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol,
  105599. + struct snd_ctl_elem_value *ucontrol)
  105600. +{
  105601. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  105602. + unsigned int val = 0;
  105603. + int i, change;
  105604. +
  105605. + for (i = 0; i < 4; i++)
  105606. + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  105607. +
  105608. + change = val != chip->spdif_status;
  105609. + chip->spdif_status = val;
  105610. +
  105611. + return change;
  105612. +}
  105613. +
  105614. +static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol,
  105615. + struct snd_ctl_elem_info *uinfo)
  105616. +{
  105617. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  105618. + uinfo->count = 1;
  105619. + return 0;
  105620. +}
  105621. +
  105622. +static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol,
  105623. + struct snd_ctl_elem_value *ucontrol)
  105624. +{
  105625. + /* bcm2835 supports only consumer mode and sets all other format flags
  105626. + * automatically. So the only thing left is signalling non-audio
  105627. + * content */
  105628. + ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO;
  105629. + return 0;
  105630. +}
  105631. +
  105632. +static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol,
  105633. + struct snd_ctl_elem_info *uinfo)
  105634. +{
  105635. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  105636. + uinfo->count = 1;
  105637. + return 0;
  105638. +}
  105639. +
  105640. +static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol,
  105641. + struct snd_ctl_elem_value *ucontrol)
  105642. +{
  105643. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  105644. + int i;
  105645. +
  105646. + for (i = 0; i < 4; i++)
  105647. + ucontrol->value.iec958.status[i] =
  105648. + (chip->spdif_status >> (i * 8)) & 0xff;
  105649. + return 0;
  105650. +}
  105651. +
  105652. +static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol,
  105653. + struct snd_ctl_elem_value *ucontrol)
  105654. +{
  105655. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  105656. + unsigned int val = 0;
  105657. + int i, change;
  105658. +
  105659. + for (i = 0; i < 4; i++)
  105660. + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  105661. + change = val != chip->spdif_status;
  105662. + chip->spdif_status = val;
  105663. +
  105664. + return change;
  105665. +}
  105666. +
  105667. +static struct snd_kcontrol_new snd_bcm2835_spdif[] = {
  105668. + {
  105669. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  105670. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  105671. + .info = snd_bcm2835_spdif_default_info,
  105672. + .get = snd_bcm2835_spdif_default_get,
  105673. + .put = snd_bcm2835_spdif_default_put
  105674. + },
  105675. + {
  105676. + .access = SNDRV_CTL_ELEM_ACCESS_READ,
  105677. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  105678. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  105679. + .info = snd_bcm2835_spdif_mask_info,
  105680. + .get = snd_bcm2835_spdif_mask_get,
  105681. + },
  105682. + {
  105683. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  105684. + SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  105685. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  105686. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  105687. + .info = snd_bcm2835_spdif_stream_info,
  105688. + .get = snd_bcm2835_spdif_stream_get,
  105689. + .put = snd_bcm2835_spdif_stream_put,
  105690. + },
  105691. +};
  105692. +
  105693. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
  105694. +{
  105695. + int err;
  105696. + unsigned int idx;
  105697. +
  105698. + strcpy(chip->card->mixername, "Broadcom Mixer");
  105699. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
  105700. + err =
  105701. + snd_ctl_add(chip->card,
  105702. + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
  105703. + if (err < 0)
  105704. + return err;
  105705. + }
  105706. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
  105707. + err = snd_ctl_add(chip->card,
  105708. + snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
  105709. + if (err < 0)
  105710. + return err;
  105711. + }
  105712. + return 0;
  105713. +}
  105714. diff -Nur linux-3.17.5/sound/arm/bcm2835.h linux-rpi/sound/arm/bcm2835.h
  105715. --- linux-3.17.5/sound/arm/bcm2835.h 1969-12-31 18:00:00.000000000 -0600
  105716. +++ linux-rpi/sound/arm/bcm2835.h 2014-12-11 14:02:57.788418001 -0600
  105717. @@ -0,0 +1,167 @@
  105718. +/*****************************************************************************
  105719. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  105720. +*
  105721. +* Unless you and Broadcom execute a separate written software license
  105722. +* agreement governing use of this software, this software is licensed to you
  105723. +* under the terms of the GNU General Public License version 2, available at
  105724. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  105725. +*
  105726. +* Notwithstanding the above, under no circumstances may you combine this
  105727. +* software in any way with any other Broadcom software provided under a
  105728. +* license other than the GPL, without Broadcom's express prior written
  105729. +* consent.
  105730. +*****************************************************************************/
  105731. +
  105732. +#ifndef __SOUND_ARM_BCM2835_H
  105733. +#define __SOUND_ARM_BCM2835_H
  105734. +
  105735. +#include <linux/device.h>
  105736. +#include <linux/list.h>
  105737. +#include <linux/interrupt.h>
  105738. +#include <linux/wait.h>
  105739. +#include <sound/core.h>
  105740. +#include <sound/initval.h>
  105741. +#include <sound/pcm.h>
  105742. +#include <sound/pcm_params.h>
  105743. +#include <sound/pcm-indirect.h>
  105744. +#include <linux/workqueue.h>
  105745. +
  105746. +/*
  105747. +#define AUDIO_DEBUG_ENABLE
  105748. +#define AUDIO_VERBOSE_DEBUG_ENABLE
  105749. +*/
  105750. +
  105751. +/* Debug macros */
  105752. +
  105753. +#ifdef AUDIO_DEBUG_ENABLE
  105754. +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
  105755. +
  105756. +#define audio_debug(fmt, arg...) \
  105757. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  105758. +
  105759. +#define audio_info(fmt, arg...) \
  105760. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  105761. +
  105762. +#else
  105763. +
  105764. +#define audio_debug(fmt, arg...)
  105765. +
  105766. +#define audio_info(fmt, arg...)
  105767. +
  105768. +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
  105769. +
  105770. +#else
  105771. +
  105772. +#define audio_debug(fmt, arg...)
  105773. +
  105774. +#define audio_info(fmt, arg...)
  105775. +
  105776. +#endif /* AUDIO_DEBUG_ENABLE */
  105777. +
  105778. +#define audio_error(fmt, arg...) \
  105779. + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
  105780. +
  105781. +#define audio_warning(fmt, arg...) \
  105782. + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
  105783. +
  105784. +#define audio_alert(fmt, arg...) \
  105785. + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
  105786. +
  105787. +#define MAX_SUBSTREAMS (8)
  105788. +#define AVAIL_SUBSTREAMS_MASK (0xff)
  105789. +enum {
  105790. + CTRL_VOL_MUTE,
  105791. + CTRL_VOL_UNMUTE
  105792. +};
  105793. +
  105794. +/* macros for alsa2chip and chip2alsa, instead of functions */
  105795. +
  105796. +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
  105797. +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
  105798. +
  105799. +/* Some constants for values .. */
  105800. +typedef enum {
  105801. + AUDIO_DEST_AUTO = 0,
  105802. + AUDIO_DEST_HEADPHONES = 1,
  105803. + AUDIO_DEST_HDMI = 2,
  105804. + AUDIO_DEST_MAX,
  105805. +} SND_BCM2835_ROUTE_T;
  105806. +
  105807. +typedef enum {
  105808. + PCM_PLAYBACK_VOLUME,
  105809. + PCM_PLAYBACK_MUTE,
  105810. + PCM_PLAYBACK_DEVICE,
  105811. +} SND_BCM2835_CTRL_T;
  105812. +
  105813. +/* definition of the chip-specific record */
  105814. +typedef struct bcm2835_chip {
  105815. + struct snd_card *card;
  105816. + struct snd_pcm *pcm;
  105817. + struct snd_pcm *pcm_spdif;
  105818. + /* Bitmat for valid reg_base and irq numbers */
  105819. + uint32_t avail_substreams;
  105820. + struct platform_device *pdev[MAX_SUBSTREAMS];
  105821. + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
  105822. +
  105823. + int volume;
  105824. + int old_volume; /* stores the volume value whist muted */
  105825. + int dest;
  105826. + int mute;
  105827. +
  105828. + unsigned int opened;
  105829. + unsigned int spdif_status;
  105830. + struct mutex audio_mutex;
  105831. +} bcm2835_chip_t;
  105832. +
  105833. +typedef struct bcm2835_alsa_stream {
  105834. + bcm2835_chip_t *chip;
  105835. + struct snd_pcm_substream *substream;
  105836. + struct snd_pcm_indirect pcm_indirect;
  105837. +
  105838. + struct semaphore buffers_update_sem;
  105839. + struct semaphore control_sem;
  105840. + spinlock_t lock;
  105841. + volatile uint32_t control;
  105842. + volatile uint32_t status;
  105843. +
  105844. + int open;
  105845. + int running;
  105846. + int draining;
  105847. +
  105848. + int channels;
  105849. + int params_rate;
  105850. + int pcm_format_width;
  105851. +
  105852. + unsigned int pos;
  105853. + unsigned int buffer_size;
  105854. + unsigned int period_size;
  105855. +
  105856. + uint32_t enable_fifo_irq;
  105857. + irq_handler_t fifo_irq_handler;
  105858. +
  105859. + atomic_t retrieved;
  105860. + struct opaque_AUDIO_INSTANCE_T *instance;
  105861. + struct workqueue_struct *my_wq;
  105862. + int idx;
  105863. +} bcm2835_alsa_stream_t;
  105864. +
  105865. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
  105866. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
  105867. +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
  105868. +
  105869. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
  105870. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
  105871. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  105872. + uint32_t channels, uint32_t samplerate,
  105873. + uint32_t bps);
  105874. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
  105875. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
  105876. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
  105877. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
  105878. +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
  105879. + void *src);
  105880. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
  105881. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
  105882. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
  105883. +
  105884. +#endif /* __SOUND_ARM_BCM2835_H */
  105885. diff -Nur linux-3.17.5/sound/arm/bcm2835-pcm.c linux-rpi/sound/arm/bcm2835-pcm.c
  105886. --- linux-3.17.5/sound/arm/bcm2835-pcm.c 1969-12-31 18:00:00.000000000 -0600
  105887. +++ linux-rpi/sound/arm/bcm2835-pcm.c 2014-12-11 14:02:57.788418001 -0600
  105888. @@ -0,0 +1,552 @@
  105889. +/*****************************************************************************
  105890. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  105891. +*
  105892. +* Unless you and Broadcom execute a separate written software license
  105893. +* agreement governing use of this software, this software is licensed to you
  105894. +* under the terms of the GNU General Public License version 2, available at
  105895. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  105896. +*
  105897. +* Notwithstanding the above, under no circumstances may you combine this
  105898. +* software in any way with any other Broadcom software provided under a
  105899. +* license other than the GPL, without Broadcom's express prior written
  105900. +* consent.
  105901. +*****************************************************************************/
  105902. +
  105903. +#include <linux/interrupt.h>
  105904. +#include <linux/slab.h>
  105905. +
  105906. +#include <sound/asoundef.h>
  105907. +
  105908. +#include "bcm2835.h"
  105909. +
  105910. +/* hardware definition */
  105911. +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  105912. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  105913. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  105914. + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  105915. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  105916. + .rate_min = 8000,
  105917. + .rate_max = 48000,
  105918. + .channels_min = 1,
  105919. + .channels_max = 2,
  105920. + .buffer_bytes_max = 128 * 1024,
  105921. + .period_bytes_min = 1 * 1024,
  105922. + .period_bytes_max = 128 * 1024,
  105923. + .periods_min = 1,
  105924. + .periods_max = 128,
  105925. +};
  105926. +
  105927. +static struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
  105928. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  105929. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  105930. + .formats = SNDRV_PCM_FMTBIT_S16_LE,
  105931. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
  105932. + SNDRV_PCM_RATE_48000,
  105933. + .rate_min = 44100,
  105934. + .rate_max = 48000,
  105935. + .channels_min = 2,
  105936. + .channels_max = 2,
  105937. + .buffer_bytes_max = 128 * 1024,
  105938. + .period_bytes_min = 1 * 1024,
  105939. + .period_bytes_max = 128 * 1024,
  105940. + .periods_min = 1,
  105941. + .periods_max = 128,
  105942. +};
  105943. +
  105944. +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
  105945. +{
  105946. + audio_info("Freeing up alsa stream here ..\n");
  105947. + if (runtime->private_data)
  105948. + kfree(runtime->private_data);
  105949. + runtime->private_data = NULL;
  105950. +}
  105951. +
  105952. +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
  105953. +{
  105954. + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
  105955. + uint32_t consumed = 0;
  105956. + int new_period = 0;
  105957. +
  105958. + audio_info(" .. IN\n");
  105959. +
  105960. + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
  105961. + alsa_stream ? alsa_stream->substream : 0);
  105962. +
  105963. + if (alsa_stream->open)
  105964. + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
  105965. +
  105966. + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
  105967. + * each iteration are the buffers that have been played out already
  105968. + */
  105969. +
  105970. + if (alsa_stream->period_size) {
  105971. + if ((alsa_stream->pos / alsa_stream->period_size) !=
  105972. + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
  105973. + new_period = 1;
  105974. + }
  105975. + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
  105976. + alsa_stream->pos,
  105977. + consumed,
  105978. + alsa_stream->buffer_size,
  105979. + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
  105980. + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
  105981. + new_period);
  105982. + if (alsa_stream->buffer_size) {
  105983. + alsa_stream->pos += consumed &~ (1<<30);
  105984. + alsa_stream->pos %= alsa_stream->buffer_size;
  105985. + }
  105986. +
  105987. + if (alsa_stream->substream) {
  105988. + if (new_period)
  105989. + snd_pcm_period_elapsed(alsa_stream->substream);
  105990. + } else {
  105991. + audio_warning(" unexpected NULL substream\n");
  105992. + }
  105993. + audio_info(" .. OUT\n");
  105994. +
  105995. + return IRQ_HANDLED;
  105996. +}
  105997. +
  105998. +/* open callback */
  105999. +static int snd_bcm2835_playback_open_generic(
  106000. + struct snd_pcm_substream *substream, int spdif)
  106001. +{
  106002. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  106003. + struct snd_pcm_runtime *runtime = substream->runtime;
  106004. + bcm2835_alsa_stream_t *alsa_stream;
  106005. + int idx;
  106006. + int err;
  106007. +
  106008. + audio_info(" .. IN (%d)\n", substream->number);
  106009. +
  106010. + if(mutex_lock_interruptible(&chip->audio_mutex))
  106011. + {
  106012. + audio_error("Interrupted whilst waiting for lock\n");
  106013. + return -EINTR;
  106014. + }
  106015. + audio_info("Alsa open (%d)\n", substream->number);
  106016. + idx = substream->number;
  106017. +
  106018. + if (spdif && chip->opened != 0)
  106019. + return -EBUSY;
  106020. + else if (!spdif && (chip->opened & (1 << idx)))
  106021. + return -EBUSY;
  106022. +
  106023. + if (idx > MAX_SUBSTREAMS) {
  106024. + audio_error
  106025. + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
  106026. + idx, MAX_SUBSTREAMS);
  106027. + err = -ENODEV;
  106028. + goto out;
  106029. + }
  106030. +
  106031. + /* Check if we are ready */
  106032. + if (!(chip->avail_substreams & (1 << idx))) {
  106033. + /* We are not ready yet */
  106034. + audio_error("substream(%d) device is not ready yet\n", idx);
  106035. + err = -EAGAIN;
  106036. + goto out;
  106037. + }
  106038. +
  106039. + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
  106040. + if (alsa_stream == NULL) {
  106041. + err = -ENOMEM;
  106042. + goto out;
  106043. + }
  106044. +
  106045. + /* Initialise alsa_stream */
  106046. + alsa_stream->chip = chip;
  106047. + alsa_stream->substream = substream;
  106048. + alsa_stream->idx = idx;
  106049. +
  106050. + sema_init(&alsa_stream->buffers_update_sem, 0);
  106051. + sema_init(&alsa_stream->control_sem, 0);
  106052. + spin_lock_init(&alsa_stream->lock);
  106053. +
  106054. + /* Enabled in start trigger, called on each "fifo irq" after that */
  106055. + alsa_stream->enable_fifo_irq = 0;
  106056. + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
  106057. +
  106058. + err = bcm2835_audio_open(alsa_stream);
  106059. + if (err != 0) {
  106060. + kfree(alsa_stream);
  106061. + return err;
  106062. + }
  106063. + runtime->private_data = alsa_stream;
  106064. + runtime->private_free = snd_bcm2835_playback_free;
  106065. + if (spdif) {
  106066. + runtime->hw = snd_bcm2835_playback_spdif_hw;
  106067. + } else {
  106068. + /* clear spdif status, as we are not in spdif mode */
  106069. + chip->spdif_status = 0;
  106070. + runtime->hw = snd_bcm2835_playback_hw;
  106071. + }
  106072. + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
  106073. + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  106074. + 16);
  106075. +
  106076. + chip->alsa_stream[idx] = alsa_stream;
  106077. +
  106078. + chip->opened |= (1 << idx);
  106079. + alsa_stream->open = 1;
  106080. + alsa_stream->draining = 1;
  106081. +
  106082. +out:
  106083. + mutex_unlock(&chip->audio_mutex);
  106084. +
  106085. + audio_info(" .. OUT =%d\n", err);
  106086. +
  106087. + return err;
  106088. +}
  106089. +
  106090. +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
  106091. +{
  106092. + return snd_bcm2835_playback_open_generic(substream, 0);
  106093. +}
  106094. +
  106095. +static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
  106096. +{
  106097. + return snd_bcm2835_playback_open_generic(substream, 1);
  106098. +}
  106099. +
  106100. +/* close callback */
  106101. +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
  106102. +{
  106103. + /* the hardware-specific codes will be here */
  106104. +
  106105. + bcm2835_chip_t *chip;
  106106. + struct snd_pcm_runtime *runtime;
  106107. + bcm2835_alsa_stream_t *alsa_stream;
  106108. +
  106109. + audio_info(" .. IN\n");
  106110. +
  106111. + chip = snd_pcm_substream_chip(substream);
  106112. + if(mutex_lock_interruptible(&chip->audio_mutex))
  106113. + {
  106114. + audio_error("Interrupted whilst waiting for lock\n");
  106115. + return -EINTR;
  106116. + }
  106117. + runtime = substream->runtime;
  106118. + alsa_stream = runtime->private_data;
  106119. +
  106120. + audio_info("Alsa close\n");
  106121. +
  106122. + /*
  106123. + * Call stop if it's still running. This happens when app
  106124. + * is force killed and we don't get a stop trigger.
  106125. + */
  106126. + if (alsa_stream->running) {
  106127. + int err;
  106128. + err = bcm2835_audio_stop(alsa_stream);
  106129. + alsa_stream->running = 0;
  106130. + if (err != 0)
  106131. + audio_error(" Failed to STOP alsa device\n");
  106132. + }
  106133. +
  106134. + alsa_stream->period_size = 0;
  106135. + alsa_stream->buffer_size = 0;
  106136. +
  106137. + if (alsa_stream->open) {
  106138. + alsa_stream->open = 0;
  106139. + bcm2835_audio_close(alsa_stream);
  106140. + }
  106141. + if (alsa_stream->chip)
  106142. + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
  106143. + /*
  106144. + * Do not free up alsa_stream here, it will be freed up by
  106145. + * runtime->private_free callback we registered in *_open above
  106146. + */
  106147. +
  106148. + chip->opened &= ~(1 << substream->number);
  106149. +
  106150. + mutex_unlock(&chip->audio_mutex);
  106151. + audio_info(" .. OUT\n");
  106152. +
  106153. + return 0;
  106154. +}
  106155. +
  106156. +/* hw_params callback */
  106157. +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
  106158. + struct snd_pcm_hw_params *params)
  106159. +{
  106160. + struct snd_pcm_runtime *runtime = substream->runtime;
  106161. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106162. + int err;
  106163. +
  106164. + audio_info(" .. IN\n");
  106165. +
  106166. + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  106167. + if (err < 0) {
  106168. + audio_error
  106169. + (" pcm_lib_malloc failed to allocated pages for buffers\n");
  106170. + return err;
  106171. + }
  106172. +
  106173. + alsa_stream->channels = params_channels(params);
  106174. + alsa_stream->params_rate = params_rate(params);
  106175. + alsa_stream->pcm_format_width = snd_pcm_format_width(params_format (params));
  106176. + audio_info(" .. OUT\n");
  106177. +
  106178. + return err;
  106179. +}
  106180. +
  106181. +/* hw_free callback */
  106182. +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
  106183. +{
  106184. + audio_info(" .. IN\n");
  106185. + return snd_pcm_lib_free_pages(substream);
  106186. +}
  106187. +
  106188. +/* prepare callback */
  106189. +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
  106190. +{
  106191. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  106192. + struct snd_pcm_runtime *runtime = substream->runtime;
  106193. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106194. + int channels;
  106195. + int err;
  106196. +
  106197. + audio_info(" .. IN\n");
  106198. +
  106199. + /* notify the vchiq that it should enter spdif passthrough mode by
  106200. + * setting channels=0 (see
  106201. + * https://github.com/raspberrypi/linux/issues/528) */
  106202. + if (chip->spdif_status & IEC958_AES0_NONAUDIO)
  106203. + channels = 0;
  106204. + else
  106205. + channels = alsa_stream->channels;
  106206. +
  106207. + err = bcm2835_audio_set_params(alsa_stream, channels,
  106208. + alsa_stream->params_rate,
  106209. + alsa_stream->pcm_format_width);
  106210. + if (err < 0) {
  106211. + audio_error(" error setting hw params\n");
  106212. + }
  106213. +
  106214. + bcm2835_audio_setup(alsa_stream);
  106215. +
  106216. + /* in preparation of the stream, set the controls (volume level) of the stream */
  106217. + bcm2835_audio_set_ctls(alsa_stream->chip);
  106218. +
  106219. +
  106220. + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
  106221. +
  106222. + alsa_stream->pcm_indirect.hw_buffer_size =
  106223. + alsa_stream->pcm_indirect.sw_buffer_size =
  106224. + snd_pcm_lib_buffer_bytes(substream);
  106225. +
  106226. + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  106227. + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  106228. + alsa_stream->pos = 0;
  106229. +
  106230. + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  106231. + alsa_stream->buffer_size, alsa_stream->period_size,
  106232. + alsa_stream->pos, runtime->frame_bits);
  106233. +
  106234. + audio_info(" .. OUT\n");
  106235. + return 0;
  106236. +}
  106237. +
  106238. +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
  106239. + struct snd_pcm_indirect *rec, size_t bytes)
  106240. +{
  106241. + struct snd_pcm_runtime *runtime = substream->runtime;
  106242. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106243. + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
  106244. + int err;
  106245. +
  106246. + err = bcm2835_audio_write(alsa_stream, bytes, src);
  106247. + if (err)
  106248. + audio_error(" Failed to transfer to alsa device (%d)\n", err);
  106249. +
  106250. +}
  106251. +
  106252. +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
  106253. +{
  106254. + struct snd_pcm_runtime *runtime = substream->runtime;
  106255. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106256. + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
  106257. +
  106258. + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
  106259. + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
  106260. + snd_bcm2835_pcm_transfer);
  106261. + return 0;
  106262. +}
  106263. +
  106264. +/* trigger callback */
  106265. +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  106266. +{
  106267. + struct snd_pcm_runtime *runtime = substream->runtime;
  106268. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106269. + int err = 0;
  106270. +
  106271. + audio_info(" .. IN\n");
  106272. +
  106273. + switch (cmd) {
  106274. + case SNDRV_PCM_TRIGGER_START:
  106275. + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
  106276. + alsa_stream->running);
  106277. + if (!alsa_stream->running) {
  106278. + err = bcm2835_audio_start(alsa_stream);
  106279. + if (err == 0) {
  106280. + alsa_stream->pcm_indirect.hw_io =
  106281. + alsa_stream->pcm_indirect.hw_data =
  106282. + bytes_to_frames(runtime,
  106283. + alsa_stream->pos);
  106284. + substream->ops->ack(substream);
  106285. + alsa_stream->running = 1;
  106286. + alsa_stream->draining = 1;
  106287. + } else {
  106288. + audio_error(" Failed to START alsa device (%d)\n", err);
  106289. + }
  106290. + }
  106291. + break;
  106292. + case SNDRV_PCM_TRIGGER_STOP:
  106293. + audio_debug
  106294. + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
  106295. + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
  106296. + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  106297. + audio_info("DRAINING\n");
  106298. + alsa_stream->draining = 1;
  106299. + } else {
  106300. + audio_info("DROPPING\n");
  106301. + alsa_stream->draining = 0;
  106302. + }
  106303. + if (alsa_stream->running) {
  106304. + err = bcm2835_audio_stop(alsa_stream);
  106305. + if (err != 0)
  106306. + audio_error(" Failed to STOP alsa device (%d)\n", err);
  106307. + alsa_stream->running = 0;
  106308. + }
  106309. + break;
  106310. + default:
  106311. + err = -EINVAL;
  106312. + }
  106313. +
  106314. + audio_info(" .. OUT\n");
  106315. + return err;
  106316. +}
  106317. +
  106318. +/* pointer callback */
  106319. +static snd_pcm_uframes_t
  106320. +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
  106321. +{
  106322. + struct snd_pcm_runtime *runtime = substream->runtime;
  106323. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106324. +
  106325. + audio_info(" .. IN\n");
  106326. +
  106327. + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
  106328. + frames_to_bytes(runtime, runtime->status->hw_ptr),
  106329. + frames_to_bytes(runtime, runtime->control->appl_ptr),
  106330. + alsa_stream->pos);
  106331. +
  106332. + audio_info(" .. OUT\n");
  106333. + return snd_pcm_indirect_playback_pointer(substream,
  106334. + &alsa_stream->pcm_indirect,
  106335. + alsa_stream->pos);
  106336. +}
  106337. +
  106338. +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  106339. + unsigned int cmd, void *arg)
  106340. +{
  106341. + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  106342. + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
  106343. + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
  106344. + return ret;
  106345. +}
  106346. +
  106347. +/* operators */
  106348. +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
  106349. + .open = snd_bcm2835_playback_open,
  106350. + .close = snd_bcm2835_playback_close,
  106351. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  106352. + .hw_params = snd_bcm2835_pcm_hw_params,
  106353. + .hw_free = snd_bcm2835_pcm_hw_free,
  106354. + .prepare = snd_bcm2835_pcm_prepare,
  106355. + .trigger = snd_bcm2835_pcm_trigger,
  106356. + .pointer = snd_bcm2835_pcm_pointer,
  106357. + .ack = snd_bcm2835_pcm_ack,
  106358. +};
  106359. +
  106360. +static struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
  106361. + .open = snd_bcm2835_playback_spdif_open,
  106362. + .close = snd_bcm2835_playback_close,
  106363. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  106364. + .hw_params = snd_bcm2835_pcm_hw_params,
  106365. + .hw_free = snd_bcm2835_pcm_hw_free,
  106366. + .prepare = snd_bcm2835_pcm_prepare,
  106367. + .trigger = snd_bcm2835_pcm_trigger,
  106368. + .pointer = snd_bcm2835_pcm_pointer,
  106369. + .ack = snd_bcm2835_pcm_ack,
  106370. +};
  106371. +
  106372. +/* create a pcm device */
  106373. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
  106374. +{
  106375. + struct snd_pcm *pcm;
  106376. + int err;
  106377. +
  106378. + audio_info(" .. IN\n");
  106379. + mutex_init(&chip->audio_mutex);
  106380. + if(mutex_lock_interruptible(&chip->audio_mutex))
  106381. + {
  106382. + audio_error("Interrupted whilst waiting for lock\n");
  106383. + return -EINTR;
  106384. + }
  106385. + err =
  106386. + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
  106387. + if (err < 0)
  106388. + return err;
  106389. + pcm->private_data = chip;
  106390. + strcpy(pcm->name, "bcm2835 ALSA");
  106391. + chip->pcm = pcm;
  106392. + chip->dest = AUDIO_DEST_AUTO;
  106393. + chip->volume = alsa2chip(0);
  106394. + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
  106395. + /* set operators */
  106396. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  106397. + &snd_bcm2835_playback_ops);
  106398. +
  106399. + /* pre-allocation of buffers */
  106400. + /* NOTE: this may fail */
  106401. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  106402. + snd_dma_continuous_data
  106403. + (GFP_KERNEL), 64 * 1024,
  106404. + 64 * 1024);
  106405. +
  106406. + mutex_unlock(&chip->audio_mutex);
  106407. + audio_info(" .. OUT\n");
  106408. +
  106409. + return 0;
  106410. +}
  106411. +
  106412. +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip)
  106413. +{
  106414. + struct snd_pcm *pcm;
  106415. + int err;
  106416. +
  106417. + audio_info(" .. IN\n");
  106418. + if(mutex_lock_interruptible(&chip->audio_mutex))
  106419. + {
  106420. + audio_error("Interrupted whilst waiting for lock\n");
  106421. + return -EINTR;
  106422. + }
  106423. + err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
  106424. + if (err < 0)
  106425. + return err;
  106426. +
  106427. + pcm->private_data = chip;
  106428. + strcpy(pcm->name, "bcm2835 IEC958/HDMI");
  106429. + chip->pcm_spdif = pcm;
  106430. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  106431. + &snd_bcm2835_playback_spdif_ops);
  106432. +
  106433. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  106434. + snd_dma_continuous_data (GFP_KERNEL),
  106435. + 64 * 1024, 64 * 1024);
  106436. + mutex_unlock(&chip->audio_mutex);
  106437. + audio_info(" .. OUT\n");
  106438. +
  106439. + return 0;
  106440. +}
  106441. diff -Nur linux-3.17.5/sound/arm/bcm2835-vchiq.c linux-rpi/sound/arm/bcm2835-vchiq.c
  106442. --- linux-3.17.5/sound/arm/bcm2835-vchiq.c 1969-12-31 18:00:00.000000000 -0600
  106443. +++ linux-rpi/sound/arm/bcm2835-vchiq.c 2014-12-11 14:05:40.436418001 -0600
  106444. @@ -0,0 +1,902 @@
  106445. +/*****************************************************************************
  106446. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  106447. +*
  106448. +* Unless you and Broadcom execute a separate written software license
  106449. +* agreement governing use of this software, this software is licensed to you
  106450. +* under the terms of the GNU General Public License version 2, available at
  106451. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  106452. +*
  106453. +* Notwithstanding the above, under no circumstances may you combine this
  106454. +* software in any way with any other Broadcom software provided under a
  106455. +* license other than the GPL, without Broadcom's express prior written
  106456. +* consent.
  106457. +*****************************************************************************/
  106458. +
  106459. +#include <linux/device.h>
  106460. +#include <sound/core.h>
  106461. +#include <sound/initval.h>
  106462. +#include <sound/pcm.h>
  106463. +#include <linux/io.h>
  106464. +#include <linux/interrupt.h>
  106465. +#include <linux/fs.h>
  106466. +#include <linux/file.h>
  106467. +#include <linux/mm.h>
  106468. +#include <linux/syscalls.h>
  106469. +#include <asm/uaccess.h>
  106470. +#include <linux/slab.h>
  106471. +#include <linux/delay.h>
  106472. +#include <linux/atomic.h>
  106473. +#include <linux/module.h>
  106474. +#include <linux/completion.h>
  106475. +
  106476. +#include "bcm2835.h"
  106477. +
  106478. +/* ---- Include Files -------------------------------------------------------- */
  106479. +
  106480. +#include "interface/vchi/vchi.h"
  106481. +#include "vc_vchi_audioserv_defs.h"
  106482. +
  106483. +/* ---- Private Constants and Types ------------------------------------------ */
  106484. +
  106485. +#define BCM2835_AUDIO_STOP 0
  106486. +#define BCM2835_AUDIO_START 1
  106487. +#define BCM2835_AUDIO_WRITE 2
  106488. +
  106489. +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
  106490. +#ifdef AUDIO_DEBUG_ENABLE
  106491. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  106492. + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  106493. + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  106494. + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  106495. +#else
  106496. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  106497. + #define LOG_WARN( fmt, arg... )
  106498. + #define LOG_INFO( fmt, arg... )
  106499. + #define LOG_DBG( fmt, arg... )
  106500. +#endif
  106501. +
  106502. +typedef struct opaque_AUDIO_INSTANCE_T {
  106503. + uint32_t num_connections;
  106504. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  106505. + struct completion msg_avail_comp;
  106506. + struct mutex vchi_mutex;
  106507. + bcm2835_alsa_stream_t *alsa_stream;
  106508. + int32_t result;
  106509. + short peer_version;
  106510. +} AUDIO_INSTANCE_T;
  106511. +
  106512. +bool force_bulk = false;
  106513. +
  106514. +/* ---- Private Variables ---------------------------------------------------- */
  106515. +
  106516. +/* ---- Private Function Prototypes ------------------------------------------ */
  106517. +
  106518. +/* ---- Private Functions ---------------------------------------------------- */
  106519. +
  106520. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
  106521. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
  106522. +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  106523. + uint32_t count, void *src);
  106524. +
  106525. +typedef struct {
  106526. + struct work_struct my_work;
  106527. + bcm2835_alsa_stream_t *alsa_stream;
  106528. + int cmd;
  106529. + void *src;
  106530. + uint32_t count;
  106531. +} my_work_t;
  106532. +
  106533. +static void my_wq_function(struct work_struct *work)
  106534. +{
  106535. + my_work_t *w = (my_work_t *) work;
  106536. + int ret = -9;
  106537. + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
  106538. + switch (w->cmd) {
  106539. + case BCM2835_AUDIO_START:
  106540. + ret = bcm2835_audio_start_worker(w->alsa_stream);
  106541. + break;
  106542. + case BCM2835_AUDIO_STOP:
  106543. + ret = bcm2835_audio_stop_worker(w->alsa_stream);
  106544. + break;
  106545. + case BCM2835_AUDIO_WRITE:
  106546. + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
  106547. + w->src);
  106548. + break;
  106549. + default:
  106550. + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
  106551. + break;
  106552. + }
  106553. + kfree((void *)work);
  106554. + LOG_DBG(" .. OUT %d\n", ret);
  106555. +}
  106556. +
  106557. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
  106558. +{
  106559. + int ret = -1;
  106560. + LOG_DBG(" .. IN\n");
  106561. + if (alsa_stream->my_wq) {
  106562. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  106563. + /*--- Queue some work (item 1) ---*/
  106564. + if (work) {
  106565. + INIT_WORK((struct work_struct *)work, my_wq_function);
  106566. + work->alsa_stream = alsa_stream;
  106567. + work->cmd = BCM2835_AUDIO_START;
  106568. + if (queue_work
  106569. + (alsa_stream->my_wq, (struct work_struct *)work))
  106570. + ret = 0;
  106571. + } else
  106572. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  106573. + }
  106574. + LOG_DBG(" .. OUT %d\n", ret);
  106575. + return ret;
  106576. +}
  106577. +
  106578. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
  106579. +{
  106580. + int ret = -1;
  106581. + LOG_DBG(" .. IN\n");
  106582. + if (alsa_stream->my_wq) {
  106583. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  106584. + /*--- Queue some work (item 1) ---*/
  106585. + if (work) {
  106586. + INIT_WORK((struct work_struct *)work, my_wq_function);
  106587. + work->alsa_stream = alsa_stream;
  106588. + work->cmd = BCM2835_AUDIO_STOP;
  106589. + if (queue_work
  106590. + (alsa_stream->my_wq, (struct work_struct *)work))
  106591. + ret = 0;
  106592. + } else
  106593. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  106594. + }
  106595. + LOG_DBG(" .. OUT %d\n", ret);
  106596. + return ret;
  106597. +}
  106598. +
  106599. +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
  106600. + uint32_t count, void *src)
  106601. +{
  106602. + int ret = -1;
  106603. + LOG_DBG(" .. IN\n");
  106604. + if (alsa_stream->my_wq) {
  106605. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  106606. + /*--- Queue some work (item 1) ---*/
  106607. + if (work) {
  106608. + INIT_WORK((struct work_struct *)work, my_wq_function);
  106609. + work->alsa_stream = alsa_stream;
  106610. + work->cmd = BCM2835_AUDIO_WRITE;
  106611. + work->src = src;
  106612. + work->count = count;
  106613. + if (queue_work
  106614. + (alsa_stream->my_wq, (struct work_struct *)work))
  106615. + ret = 0;
  106616. + } else
  106617. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  106618. + }
  106619. + LOG_DBG(" .. OUT %d\n", ret);
  106620. + return ret;
  106621. +}
  106622. +
  106623. +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
  106624. +{
  106625. + alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
  106626. + return;
  106627. +}
  106628. +
  106629. +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
  106630. +{
  106631. + if (alsa_stream->my_wq) {
  106632. + flush_workqueue(alsa_stream->my_wq);
  106633. + destroy_workqueue(alsa_stream->my_wq);
  106634. + alsa_stream->my_wq = NULL;
  106635. + }
  106636. + return;
  106637. +}
  106638. +
  106639. +static void audio_vchi_callback(void *param,
  106640. + const VCHI_CALLBACK_REASON_T reason,
  106641. + void *msg_handle)
  106642. +{
  106643. + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
  106644. + int32_t status;
  106645. + int32_t msg_len;
  106646. + VC_AUDIO_MSG_T m;
  106647. + LOG_DBG(" .. IN instance=%p, handle=%p, alsa=%p, reason=%d, handle=%p\n",
  106648. + instance, instance ? instance->vchi_handle[0] : NULL, instance ? instance->alsa_stream : NULL, reason, msg_handle);
  106649. +
  106650. + if (reason != VCHI_CALLBACK_MSG_AVAILABLE) {
  106651. + return;
  106652. + }
  106653. + if (!instance) {
  106654. + LOG_ERR(" .. instance is null\n");
  106655. + BUG();
  106656. + return;
  106657. + }
  106658. + if (!instance->vchi_handle[0]) {
  106659. + LOG_ERR(" .. instance->vchi_handle[0] is null\n");
  106660. + BUG();
  106661. + return;
  106662. + }
  106663. + status = vchi_msg_dequeue(instance->vchi_handle[0],
  106664. + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
  106665. + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
  106666. + LOG_DBG
  106667. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
  106668. + instance, m.u.result.success);
  106669. + instance->result = m.u.result.success;
  106670. + complete(&instance->msg_avail_comp);
  106671. + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
  106672. + bcm2835_alsa_stream_t *alsa_stream = instance->alsa_stream;
  106673. + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
  106674. + LOG_DBG
  106675. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
  106676. + instance, m.u.complete.count);
  106677. + if (alsa_stream && callback) {
  106678. + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
  106679. + callback(0, alsa_stream);
  106680. + } else {
  106681. + LOG_ERR(" .. unexpected alsa_stream=%p, callback=%p\n",
  106682. + alsa_stream, callback);
  106683. + }
  106684. + } else {
  106685. + LOG_ERR(" .. unexpected m.type=%d\n", m.type);
  106686. + }
  106687. + LOG_DBG(" .. OUT\n");
  106688. +}
  106689. +
  106690. +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
  106691. + VCHI_CONNECTION_T **
  106692. + vchi_connections,
  106693. + uint32_t num_connections)
  106694. +{
  106695. + uint32_t i;
  106696. + AUDIO_INSTANCE_T *instance;
  106697. + int status;
  106698. +
  106699. + LOG_DBG("%s: start", __func__);
  106700. +
  106701. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  106702. + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
  106703. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  106704. +
  106705. + return NULL;
  106706. + }
  106707. + /* Allocate memory for this instance */
  106708. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  106709. + if (!instance)
  106710. + return NULL;
  106711. +
  106712. + memset(instance, 0, sizeof(*instance));
  106713. + instance->num_connections = num_connections;
  106714. +
  106715. + /* Create a lock for exclusive, serialized VCHI connection access */
  106716. + mutex_init(&instance->vchi_mutex);
  106717. + /* Open the VCHI service connections */
  106718. + for (i = 0; i < num_connections; i++) {
  106719. + SERVICE_CREATION_T params = {
  106720. + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
  106721. + VC_AUDIO_SERVER_NAME, // 4cc service code
  106722. + vchi_connections[i], // passed in fn pointers
  106723. + 0, // rx fifo size (unused)
  106724. + 0, // tx fifo size (unused)
  106725. + audio_vchi_callback, // service callback
  106726. + instance, // service callback parameter
  106727. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
  106728. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
  106729. + 0 // want crc check on bulk transfers
  106730. + };
  106731. +
  106732. + LOG_DBG("%s: about to open %i\n", __func__, i);
  106733. + status = vchi_service_open(vchi_instance, &params,
  106734. + &instance->vchi_handle[i]);
  106735. + LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
  106736. + if (status) {
  106737. + LOG_ERR
  106738. + ("%s: failed to open VCHI service connection (status=%d)\n",
  106739. + __func__, status);
  106740. +
  106741. + goto err_close_services;
  106742. + }
  106743. + /* Finished with the service for now */
  106744. + vchi_service_release(instance->vchi_handle[i]);
  106745. + }
  106746. +
  106747. + LOG_DBG("%s: okay\n", __func__);
  106748. + return instance;
  106749. +
  106750. +err_close_services:
  106751. + for (i = 0; i < instance->num_connections; i++) {
  106752. + LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
  106753. + if (instance->vchi_handle[i])
  106754. + vchi_service_close(instance->vchi_handle[i]);
  106755. + }
  106756. +
  106757. + kfree(instance);
  106758. + LOG_ERR("%s: error\n", __func__);
  106759. +
  106760. + return NULL;
  106761. +}
  106762. +
  106763. +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
  106764. +{
  106765. + uint32_t i;
  106766. +
  106767. + LOG_DBG(" .. IN\n");
  106768. +
  106769. + if (instance == NULL) {
  106770. + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
  106771. +
  106772. + return -1;
  106773. + }
  106774. +
  106775. + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
  106776. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  106777. + {
  106778. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  106779. + return -EINTR;
  106780. + }
  106781. +
  106782. + /* Close all VCHI service connections */
  106783. + for (i = 0; i < instance->num_connections; i++) {
  106784. + int32_t success;
  106785. + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
  106786. + vchi_service_use(instance->vchi_handle[i]);
  106787. +
  106788. + success = vchi_service_close(instance->vchi_handle[i]);
  106789. + if (success != 0) {
  106790. + LOG_ERR
  106791. + ("%s: failed to close VCHI service connection (status=%d)\n",
  106792. + __func__, success);
  106793. + }
  106794. + }
  106795. +
  106796. + mutex_unlock(&instance->vchi_mutex);
  106797. +
  106798. + kfree(instance);
  106799. +
  106800. + LOG_DBG(" .. OUT\n");
  106801. +
  106802. + return 0;
  106803. +}
  106804. +
  106805. +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
  106806. +{
  106807. + static VCHI_INSTANCE_T vchi_instance;
  106808. + static VCHI_CONNECTION_T *vchi_connection;
  106809. + static int initted;
  106810. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  106811. + int ret;
  106812. + LOG_DBG(" .. IN\n");
  106813. +
  106814. + LOG_INFO("%s: start\n", __func__);
  106815. + BUG_ON(instance);
  106816. + if (instance) {
  106817. + LOG_ERR("%s: VCHI instance already open (%p)\n",
  106818. + __func__, instance);
  106819. + instance->alsa_stream = alsa_stream;
  106820. + alsa_stream->instance = instance;
  106821. + ret = 0; // xxx todo -1;
  106822. + goto err_free_mem;
  106823. + }
  106824. +
  106825. + /* Initialize and create a VCHI connection */
  106826. + if (!initted) {
  106827. + ret = vchi_initialise(&vchi_instance);
  106828. + if (ret != 0) {
  106829. + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
  106830. + __func__, ret);
  106831. +
  106832. + ret = -EIO;
  106833. + goto err_free_mem;
  106834. + }
  106835. + ret = vchi_connect(NULL, 0, vchi_instance);
  106836. + if (ret != 0) {
  106837. + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
  106838. + __func__, ret);
  106839. +
  106840. + ret = -EIO;
  106841. + goto err_free_mem;
  106842. + }
  106843. + initted = 1;
  106844. + }
  106845. +
  106846. + /* Initialize an instance of the audio service */
  106847. + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
  106848. +
  106849. + if (instance == NULL) {
  106850. + LOG_ERR("%s: failed to initialize audio service\n", __func__);
  106851. +
  106852. + ret = -EPERM;
  106853. + goto err_free_mem;
  106854. + }
  106855. +
  106856. + instance->alsa_stream = alsa_stream;
  106857. + alsa_stream->instance = instance;
  106858. +
  106859. + LOG_DBG(" success !\n");
  106860. +err_free_mem:
  106861. + LOG_DBG(" .. OUT\n");
  106862. +
  106863. + return ret;
  106864. +}
  106865. +
  106866. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
  106867. +{
  106868. + AUDIO_INSTANCE_T *instance;
  106869. + VC_AUDIO_MSG_T m;
  106870. + int32_t success;
  106871. + int ret;
  106872. + LOG_DBG(" .. IN\n");
  106873. +
  106874. + my_workqueue_init(alsa_stream);
  106875. +
  106876. + ret = bcm2835_audio_open_connection(alsa_stream);
  106877. + if (ret != 0) {
  106878. + ret = -1;
  106879. + goto exit;
  106880. + }
  106881. + instance = alsa_stream->instance;
  106882. + LOG_DBG(" instance (%p)\n", instance);
  106883. +
  106884. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  106885. + {
  106886. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  106887. + return -EINTR;
  106888. + }
  106889. + vchi_service_use(instance->vchi_handle[0]);
  106890. +
  106891. + m.type = VC_AUDIO_MSG_TYPE_OPEN;
  106892. +
  106893. + /* Send the message to the videocore */
  106894. + success = vchi_msg_queue(instance->vchi_handle[0],
  106895. + &m, sizeof m,
  106896. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  106897. +
  106898. + if (success != 0) {
  106899. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  106900. + __func__, success);
  106901. +
  106902. + ret = -1;
  106903. + goto unlock;
  106904. + }
  106905. +
  106906. + ret = 0;
  106907. +
  106908. +unlock:
  106909. + vchi_service_release(instance->vchi_handle[0]);
  106910. + mutex_unlock(&instance->vchi_mutex);
  106911. +exit:
  106912. + LOG_DBG(" .. OUT\n");
  106913. + return ret;
  106914. +}
  106915. +
  106916. +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
  106917. + bcm2835_chip_t * chip)
  106918. +{
  106919. + VC_AUDIO_MSG_T m;
  106920. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  106921. + int32_t success;
  106922. + int ret;
  106923. + LOG_DBG(" .. IN\n");
  106924. +
  106925. + LOG_INFO
  106926. + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  106927. +
  106928. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  106929. + {
  106930. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  106931. + return -EINTR;
  106932. + }
  106933. + vchi_service_use(instance->vchi_handle[0]);
  106934. +
  106935. + instance->result = -1;
  106936. +
  106937. + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
  106938. + m.u.control.dest = chip->dest;
  106939. + m.u.control.volume = chip->volume;
  106940. +
  106941. + /* Create the message available completion */
  106942. + init_completion(&instance->msg_avail_comp);
  106943. +
  106944. + /* Send the message to the videocore */
  106945. + success = vchi_msg_queue(instance->vchi_handle[0],
  106946. + &m, sizeof m,
  106947. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  106948. +
  106949. + if (success != 0) {
  106950. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  106951. + __func__, success);
  106952. +
  106953. + ret = -1;
  106954. + goto unlock;
  106955. + }
  106956. +
  106957. + /* We are expecting a reply from the videocore */
  106958. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  106959. + if (ret) {
  106960. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  106961. + __func__, success);
  106962. + goto unlock;
  106963. + }
  106964. +
  106965. + if (instance->result != 0) {
  106966. + LOG_ERR("%s: result=%d\n", __func__, instance->result);
  106967. +
  106968. + ret = -1;
  106969. + goto unlock;
  106970. + }
  106971. +
  106972. + ret = 0;
  106973. +
  106974. +unlock:
  106975. + vchi_service_release(instance->vchi_handle[0]);
  106976. + mutex_unlock(&instance->vchi_mutex);
  106977. +
  106978. + LOG_DBG(" .. OUT\n");
  106979. + return ret;
  106980. +}
  106981. +
  106982. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
  106983. +{
  106984. + int i;
  106985. + int ret = 0;
  106986. + LOG_DBG(" .. IN\n");
  106987. + LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  106988. +
  106989. + /* change ctls for all substreams */
  106990. + for (i = 0; i < MAX_SUBSTREAMS; i++) {
  106991. + if (chip->avail_substreams & (1 << i)) {
  106992. + if (!chip->alsa_stream[i])
  106993. + {
  106994. + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
  106995. + ret = 0;
  106996. + }
  106997. + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
  106998. + (chip->alsa_stream[i], chip) != 0)
  106999. + {
  107000. + LOG_ERR("Couldn't set the controls for stream %d\n", i);
  107001. + ret = -1;
  107002. + }
  107003. + else LOG_DBG(" Controls set for stream %d\n", i);
  107004. + }
  107005. + }
  107006. + LOG_DBG(" .. OUT ret=%d\n", ret);
  107007. + return ret;
  107008. +}
  107009. +
  107010. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  107011. + uint32_t channels, uint32_t samplerate,
  107012. + uint32_t bps)
  107013. +{
  107014. + VC_AUDIO_MSG_T m;
  107015. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107016. + int32_t success;
  107017. + int ret;
  107018. + LOG_DBG(" .. IN\n");
  107019. +
  107020. + LOG_INFO
  107021. + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
  107022. + channels, samplerate, bps);
  107023. +
  107024. + /* resend ctls - alsa_stream may not have been open when first send */
  107025. + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
  107026. + if (ret != 0) {
  107027. + LOG_ERR(" Alsa controls not supported\n");
  107028. + return -EINVAL;
  107029. + }
  107030. +
  107031. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107032. + {
  107033. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107034. + return -EINTR;
  107035. + }
  107036. + vchi_service_use(instance->vchi_handle[0]);
  107037. +
  107038. + instance->result = -1;
  107039. +
  107040. + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
  107041. + m.u.config.channels = channels;
  107042. + m.u.config.samplerate = samplerate;
  107043. + m.u.config.bps = bps;
  107044. +
  107045. + /* Create the message available completion */
  107046. + init_completion(&instance->msg_avail_comp);
  107047. +
  107048. + /* Send the message to the videocore */
  107049. + success = vchi_msg_queue(instance->vchi_handle[0],
  107050. + &m, sizeof m,
  107051. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107052. +
  107053. + if (success != 0) {
  107054. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107055. + __func__, success);
  107056. +
  107057. + ret = -1;
  107058. + goto unlock;
  107059. + }
  107060. +
  107061. + /* We are expecting a reply from the videocore */
  107062. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  107063. + if (ret) {
  107064. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  107065. + __func__, success);
  107066. + goto unlock;
  107067. + }
  107068. +
  107069. + if (instance->result != 0) {
  107070. + LOG_ERR("%s: result=%d", __func__, instance->result);
  107071. +
  107072. + ret = -1;
  107073. + goto unlock;
  107074. + }
  107075. +
  107076. + ret = 0;
  107077. +
  107078. +unlock:
  107079. + vchi_service_release(instance->vchi_handle[0]);
  107080. + mutex_unlock(&instance->vchi_mutex);
  107081. +
  107082. + LOG_DBG(" .. OUT\n");
  107083. + return ret;
  107084. +}
  107085. +
  107086. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
  107087. +{
  107088. + LOG_DBG(" .. IN\n");
  107089. +
  107090. + LOG_DBG(" .. OUT\n");
  107091. +
  107092. + return 0;
  107093. +}
  107094. +
  107095. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
  107096. +{
  107097. + VC_AUDIO_MSG_T m;
  107098. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107099. + int32_t success;
  107100. + int ret;
  107101. + LOG_DBG(" .. IN\n");
  107102. +
  107103. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107104. + {
  107105. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107106. + return -EINTR;
  107107. + }
  107108. + vchi_service_use(instance->vchi_handle[0]);
  107109. +
  107110. + m.type = VC_AUDIO_MSG_TYPE_START;
  107111. +
  107112. + /* Send the message to the videocore */
  107113. + success = vchi_msg_queue(instance->vchi_handle[0],
  107114. + &m, sizeof m,
  107115. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107116. +
  107117. + if (success != 0) {
  107118. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107119. + __func__, success);
  107120. +
  107121. + ret = -1;
  107122. + goto unlock;
  107123. + }
  107124. +
  107125. + ret = 0;
  107126. +
  107127. +unlock:
  107128. + vchi_service_release(instance->vchi_handle[0]);
  107129. + mutex_unlock(&instance->vchi_mutex);
  107130. + LOG_DBG(" .. OUT\n");
  107131. + return ret;
  107132. +}
  107133. +
  107134. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
  107135. +{
  107136. + VC_AUDIO_MSG_T m;
  107137. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107138. + int32_t success;
  107139. + int ret;
  107140. + LOG_DBG(" .. IN\n");
  107141. +
  107142. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107143. + {
  107144. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107145. + return -EINTR;
  107146. + }
  107147. + vchi_service_use(instance->vchi_handle[0]);
  107148. +
  107149. + m.type = VC_AUDIO_MSG_TYPE_STOP;
  107150. + m.u.stop.draining = alsa_stream->draining;
  107151. +
  107152. + /* Send the message to the videocore */
  107153. + success = vchi_msg_queue(instance->vchi_handle[0],
  107154. + &m, sizeof m,
  107155. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107156. +
  107157. + if (success != 0) {
  107158. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107159. + __func__, success);
  107160. +
  107161. + ret = -1;
  107162. + goto unlock;
  107163. + }
  107164. +
  107165. + ret = 0;
  107166. +
  107167. +unlock:
  107168. + vchi_service_release(instance->vchi_handle[0]);
  107169. + mutex_unlock(&instance->vchi_mutex);
  107170. + LOG_DBG(" .. OUT\n");
  107171. + return ret;
  107172. +}
  107173. +
  107174. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
  107175. +{
  107176. + VC_AUDIO_MSG_T m;
  107177. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107178. + int32_t success;
  107179. + int ret;
  107180. + LOG_DBG(" .. IN\n");
  107181. +
  107182. + my_workqueue_quit(alsa_stream);
  107183. +
  107184. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107185. + {
  107186. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107187. + return -EINTR;
  107188. + }
  107189. + vchi_service_use(instance->vchi_handle[0]);
  107190. +
  107191. + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
  107192. +
  107193. + /* Create the message available completion */
  107194. + init_completion(&instance->msg_avail_comp);
  107195. +
  107196. + /* Send the message to the videocore */
  107197. + success = vchi_msg_queue(instance->vchi_handle[0],
  107198. + &m, sizeof m,
  107199. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107200. +
  107201. + if (success != 0) {
  107202. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107203. + __func__, success);
  107204. + ret = -1;
  107205. + goto unlock;
  107206. + }
  107207. +
  107208. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  107209. + if (ret) {
  107210. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  107211. + __func__, success);
  107212. + goto unlock;
  107213. + }
  107214. + if (instance->result != 0) {
  107215. + LOG_ERR("%s: failed result (status=%d)\n",
  107216. + __func__, instance->result);
  107217. +
  107218. + ret = -1;
  107219. + goto unlock;
  107220. + }
  107221. +
  107222. + ret = 0;
  107223. +
  107224. +unlock:
  107225. + vchi_service_release(instance->vchi_handle[0]);
  107226. + mutex_unlock(&instance->vchi_mutex);
  107227. +
  107228. + /* Stop the audio service */
  107229. + if (instance) {
  107230. + vc_vchi_audio_deinit(instance);
  107231. + alsa_stream->instance = NULL;
  107232. + }
  107233. + LOG_DBG(" .. OUT\n");
  107234. + return ret;
  107235. +}
  107236. +
  107237. +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  107238. + uint32_t count, void *src)
  107239. +{
  107240. + VC_AUDIO_MSG_T m;
  107241. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107242. + int32_t success;
  107243. + int ret;
  107244. +
  107245. + LOG_DBG(" .. IN\n");
  107246. +
  107247. + LOG_INFO(" Writing %d bytes from %p\n", count, src);
  107248. +
  107249. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107250. + {
  107251. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107252. + return -EINTR;
  107253. + }
  107254. + vchi_service_use(instance->vchi_handle[0]);
  107255. +
  107256. + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
  107257. + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
  107258. + }
  107259. + m.type = VC_AUDIO_MSG_TYPE_WRITE;
  107260. + m.u.write.count = count;
  107261. + // old version uses bulk, new version uses control
  107262. + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
  107263. + m.u.write.callback = alsa_stream->fifo_irq_handler;
  107264. + m.u.write.cookie = alsa_stream;
  107265. + m.u.write.silence = src == NULL;
  107266. +
  107267. + /* Send the message to the videocore */
  107268. + success = vchi_msg_queue(instance->vchi_handle[0],
  107269. + &m, sizeof m,
  107270. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107271. +
  107272. + if (success != 0) {
  107273. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107274. + __func__, success);
  107275. +
  107276. + ret = -1;
  107277. + goto unlock;
  107278. + }
  107279. + if (!m.u.write.silence) {
  107280. + if (m.u.write.max_packet == 0) {
  107281. + /* Send the message to the videocore */
  107282. + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
  107283. + src, count,
  107284. + 0 *
  107285. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
  107286. + +
  107287. + 1 *
  107288. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
  107289. + NULL);
  107290. + } else {
  107291. + while (count > 0) {
  107292. + int bytes = min((int)m.u.write.max_packet, (int)count);
  107293. + success = vchi_msg_queue(instance->vchi_handle[0],
  107294. + src, bytes,
  107295. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107296. + src = (char *)src + bytes;
  107297. + count -= bytes;
  107298. + }
  107299. + }
  107300. + if (success != 0) {
  107301. + LOG_ERR
  107302. + ("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
  107303. + __func__, success);
  107304. +
  107305. + ret = -1;
  107306. + goto unlock;
  107307. + }
  107308. + }
  107309. + ret = 0;
  107310. +
  107311. +unlock:
  107312. + vchi_service_release(instance->vchi_handle[0]);
  107313. + mutex_unlock(&instance->vchi_mutex);
  107314. + LOG_DBG(" .. OUT\n");
  107315. + return ret;
  107316. +}
  107317. +
  107318. +/**
  107319. + * Returns all buffers from arm->vc
  107320. + */
  107321. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
  107322. +{
  107323. + LOG_DBG(" .. IN\n");
  107324. + LOG_DBG(" .. OUT\n");
  107325. + return;
  107326. +}
  107327. +
  107328. +/**
  107329. + * Forces VC to flush(drop) its filled playback buffers and
  107330. + * return them the us. (VC->ARM)
  107331. + */
  107332. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
  107333. +{
  107334. + LOG_DBG(" .. IN\n");
  107335. + LOG_DBG(" .. OUT\n");
  107336. +}
  107337. +
  107338. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
  107339. +{
  107340. + uint32_t count = atomic_read(&alsa_stream->retrieved);
  107341. + atomic_sub(count, &alsa_stream->retrieved);
  107342. + return count;
  107343. +}
  107344. +
  107345. +module_param(force_bulk, bool, 0444);
  107346. +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
  107347. diff -Nur linux-3.17.5/sound/arm/Kconfig linux-rpi/sound/arm/Kconfig
  107348. --- linux-3.17.5/sound/arm/Kconfig 2014-12-06 17:57:59.000000000 -0600
  107349. +++ linux-rpi/sound/arm/Kconfig 2014-12-11 14:02:57.788418001 -0600
  107350. @@ -39,5 +39,12 @@
  107351. Say Y or M if you want to support any AC97 codec attached to
  107352. the PXA2xx AC97 interface.
  107353. +config SND_BCM2835
  107354. + tristate "BCM2835 ALSA driver"
  107355. + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
  107356. + select SND_PCM
  107357. + help
  107358. + Say Y or M if you want to support BCM2835 Alsa pcm card driver
  107359. +
  107360. endif # SND_ARM
  107361. diff -Nur linux-3.17.5/sound/arm/Makefile linux-rpi/sound/arm/Makefile
  107362. --- linux-3.17.5/sound/arm/Makefile 2014-12-06 17:57:59.000000000 -0600
  107363. +++ linux-rpi/sound/arm/Makefile 2014-12-11 14:02:57.788418001 -0600
  107364. @@ -14,3 +14,8 @@
  107365. obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
  107366. snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
  107367. +
  107368. +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
  107369. +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
  107370. +
  107371. +ccflags-y += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  107372. diff -Nur linux-3.17.5/sound/arm/vc_vchi_audioserv_defs.h linux-rpi/sound/arm/vc_vchi_audioserv_defs.h
  107373. --- linux-3.17.5/sound/arm/vc_vchi_audioserv_defs.h 1969-12-31 18:00:00.000000000 -0600
  107374. +++ linux-rpi/sound/arm/vc_vchi_audioserv_defs.h 2014-12-11 14:02:57.792418001 -0600
  107375. @@ -0,0 +1,116 @@
  107376. +/*****************************************************************************
  107377. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  107378. +*
  107379. +* Unless you and Broadcom execute a separate written software license
  107380. +* agreement governing use of this software, this software is licensed to you
  107381. +* under the terms of the GNU General Public License version 2, available at
  107382. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  107383. +*
  107384. +* Notwithstanding the above, under no circumstances may you combine this
  107385. +* software in any way with any other Broadcom software provided under a
  107386. +* license other than the GPL, without Broadcom's express prior written
  107387. +* consent.
  107388. +*****************************************************************************/
  107389. +
  107390. +#ifndef _VC_AUDIO_DEFS_H_
  107391. +#define _VC_AUDIO_DEFS_H_
  107392. +
  107393. +#define VC_AUDIOSERV_MIN_VER 1
  107394. +#define VC_AUDIOSERV_VER 2
  107395. +
  107396. +// FourCC code used for VCHI connection
  107397. +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
  107398. +
  107399. +// Maximum message length
  107400. +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
  107401. +
  107402. +// List of screens that are currently supported
  107403. +// All message types supported for HOST->VC direction
  107404. +typedef enum {
  107405. + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
  107406. + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
  107407. + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
  107408. + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
  107409. + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
  107410. + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
  107411. + VC_AUDIO_MSG_TYPE_START, // Configure audio
  107412. + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
  107413. + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
  107414. + VC_AUDIO_MSG_TYPE_MAX
  107415. +} VC_AUDIO_MSG_TYPE;
  107416. +
  107417. +// configure the audio
  107418. +typedef struct {
  107419. + uint32_t channels;
  107420. + uint32_t samplerate;
  107421. + uint32_t bps;
  107422. +
  107423. +} VC_AUDIO_CONFIG_T;
  107424. +
  107425. +typedef struct {
  107426. + uint32_t volume;
  107427. + uint32_t dest;
  107428. +
  107429. +} VC_AUDIO_CONTROL_T;
  107430. +
  107431. +// audio
  107432. +typedef struct {
  107433. + uint32_t dummy;
  107434. +
  107435. +} VC_AUDIO_OPEN_T;
  107436. +
  107437. +// audio
  107438. +typedef struct {
  107439. + uint32_t dummy;
  107440. +
  107441. +} VC_AUDIO_CLOSE_T;
  107442. +// audio
  107443. +typedef struct {
  107444. + uint32_t dummy;
  107445. +
  107446. +} VC_AUDIO_START_T;
  107447. +// audio
  107448. +typedef struct {
  107449. + uint32_t draining;
  107450. +
  107451. +} VC_AUDIO_STOP_T;
  107452. +
  107453. +// configure the write audio samples
  107454. +typedef struct {
  107455. + uint32_t count; // in bytes
  107456. + void *callback;
  107457. + void *cookie;
  107458. + uint16_t silence;
  107459. + uint16_t max_packet;
  107460. +} VC_AUDIO_WRITE_T;
  107461. +
  107462. +// Generic result for a request (VC->HOST)
  107463. +typedef struct {
  107464. + int32_t success; // Success value
  107465. +
  107466. +} VC_AUDIO_RESULT_T;
  107467. +
  107468. +// Generic result for a request (VC->HOST)
  107469. +typedef struct {
  107470. + int32_t count; // Success value
  107471. + void *callback;
  107472. + void *cookie;
  107473. +} VC_AUDIO_COMPLETE_T;
  107474. +
  107475. +// Message header for all messages in HOST->VC direction
  107476. +typedef struct {
  107477. + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
  107478. + union {
  107479. + VC_AUDIO_CONFIG_T config;
  107480. + VC_AUDIO_CONTROL_T control;
  107481. + VC_AUDIO_OPEN_T open;
  107482. + VC_AUDIO_CLOSE_T close;
  107483. + VC_AUDIO_START_T start;
  107484. + VC_AUDIO_STOP_T stop;
  107485. + VC_AUDIO_WRITE_T write;
  107486. + VC_AUDIO_RESULT_T result;
  107487. + VC_AUDIO_COMPLETE_T complete;
  107488. + } u;
  107489. +} VC_AUDIO_MSG_T;
  107490. +
  107491. +#endif // _VC_AUDIO_DEFS_H_
  107492. diff -Nur linux-3.17.5/sound/soc/bcm/bcm2708-i2s.c linux-rpi/sound/soc/bcm/bcm2708-i2s.c
  107493. --- linux-3.17.5/sound/soc/bcm/bcm2708-i2s.c 1969-12-31 18:00:00.000000000 -0600
  107494. +++ linux-rpi/sound/soc/bcm/bcm2708-i2s.c 2014-12-11 14:05:40.508418001 -0600
  107495. @@ -0,0 +1,1009 @@
  107496. +/*
  107497. + * ALSA SoC I2S Audio Layer for Broadcom BCM2708 SoC
  107498. + *
  107499. + * Author: Florian Meier <florian.meier@koalo.de>
  107500. + * Copyright 2013
  107501. + *
  107502. + * Based on
  107503. + * Raspberry Pi PCM I2S ALSA Driver
  107504. + * Copyright (c) by Phil Poole 2013
  107505. + *
  107506. + * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  107507. + * Vladimir Barinov, <vbarinov@embeddedalley.com>
  107508. + * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  107509. + *
  107510. + * OMAP ALSA SoC DAI driver using McBSP port
  107511. + * Copyright (C) 2008 Nokia Corporation
  107512. + * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  107513. + * Peter Ujfalusi <peter.ujfalusi@ti.com>
  107514. + *
  107515. + * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  107516. + * Author: Timur Tabi <timur@freescale.com>
  107517. + * Copyright 2007-2010 Freescale Semiconductor, Inc.
  107518. + *
  107519. + * This program is free software; you can redistribute it and/or
  107520. + * modify it under the terms of the GNU General Public License
  107521. + * version 2 as published by the Free Software Foundation.
  107522. + *
  107523. + * This program is distributed in the hope that it will be useful, but
  107524. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  107525. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  107526. + * General Public License for more details.
  107527. + */
  107528. +
  107529. +#include "bcm2708-i2s.h"
  107530. +
  107531. +#include <linux/init.h>
  107532. +#include <linux/module.h>
  107533. +#include <linux/device.h>
  107534. +#include <linux/slab.h>
  107535. +#include <linux/delay.h>
  107536. +#include <linux/io.h>
  107537. +#include <linux/clk.h>
  107538. +#include <mach/gpio.h>
  107539. +
  107540. +#include <sound/core.h>
  107541. +#include <sound/pcm.h>
  107542. +#include <sound/pcm_params.h>
  107543. +#include <sound/initval.h>
  107544. +#include <sound/soc.h>
  107545. +#include <sound/dmaengine_pcm.h>
  107546. +
  107547. +#include <asm/system_info.h>
  107548. +
  107549. +/* Clock registers */
  107550. +#define BCM2708_CLK_PCMCTL_REG 0x00
  107551. +#define BCM2708_CLK_PCMDIV_REG 0x04
  107552. +
  107553. +/* Clock register settings */
  107554. +#define BCM2708_CLK_PASSWD (0x5a000000)
  107555. +#define BCM2708_CLK_PASSWD_MASK (0xff000000)
  107556. +#define BCM2708_CLK_MASH(v) ((v) << 9)
  107557. +#define BCM2708_CLK_FLIP BIT(8)
  107558. +#define BCM2708_CLK_BUSY BIT(7)
  107559. +#define BCM2708_CLK_KILL BIT(5)
  107560. +#define BCM2708_CLK_ENAB BIT(4)
  107561. +#define BCM2708_CLK_SRC(v) (v)
  107562. +
  107563. +#define BCM2708_CLK_SHIFT (12)
  107564. +#define BCM2708_CLK_DIVI(v) ((v) << BCM2708_CLK_SHIFT)
  107565. +#define BCM2708_CLK_DIVF(v) (v)
  107566. +#define BCM2708_CLK_DIVF_MASK (0xFFF)
  107567. +
  107568. +enum {
  107569. + BCM2708_CLK_MASH_0 = 0,
  107570. + BCM2708_CLK_MASH_1,
  107571. + BCM2708_CLK_MASH_2,
  107572. + BCM2708_CLK_MASH_3,
  107573. +};
  107574. +
  107575. +enum {
  107576. + BCM2708_CLK_SRC_GND = 0,
  107577. + BCM2708_CLK_SRC_OSC,
  107578. + BCM2708_CLK_SRC_DBG0,
  107579. + BCM2708_CLK_SRC_DBG1,
  107580. + BCM2708_CLK_SRC_PLLA,
  107581. + BCM2708_CLK_SRC_PLLC,
  107582. + BCM2708_CLK_SRC_PLLD,
  107583. + BCM2708_CLK_SRC_HDMI,
  107584. +};
  107585. +
  107586. +/* Most clocks are not useable (freq = 0) */
  107587. +static const unsigned int bcm2708_clk_freq[BCM2708_CLK_SRC_HDMI+1] = {
  107588. + [BCM2708_CLK_SRC_GND] = 0,
  107589. + [BCM2708_CLK_SRC_OSC] = 19200000,
  107590. + [BCM2708_CLK_SRC_DBG0] = 0,
  107591. + [BCM2708_CLK_SRC_DBG1] = 0,
  107592. + [BCM2708_CLK_SRC_PLLA] = 0,
  107593. + [BCM2708_CLK_SRC_PLLC] = 0,
  107594. + [BCM2708_CLK_SRC_PLLD] = 500000000,
  107595. + [BCM2708_CLK_SRC_HDMI] = 0,
  107596. +};
  107597. +
  107598. +/* I2S registers */
  107599. +#define BCM2708_I2S_CS_A_REG 0x00
  107600. +#define BCM2708_I2S_FIFO_A_REG 0x04
  107601. +#define BCM2708_I2S_MODE_A_REG 0x08
  107602. +#define BCM2708_I2S_RXC_A_REG 0x0c
  107603. +#define BCM2708_I2S_TXC_A_REG 0x10
  107604. +#define BCM2708_I2S_DREQ_A_REG 0x14
  107605. +#define BCM2708_I2S_INTEN_A_REG 0x18
  107606. +#define BCM2708_I2S_INTSTC_A_REG 0x1c
  107607. +#define BCM2708_I2S_GRAY_REG 0x20
  107608. +
  107609. +/* I2S register settings */
  107610. +#define BCM2708_I2S_STBY BIT(25)
  107611. +#define BCM2708_I2S_SYNC BIT(24)
  107612. +#define BCM2708_I2S_RXSEX BIT(23)
  107613. +#define BCM2708_I2S_RXF BIT(22)
  107614. +#define BCM2708_I2S_TXE BIT(21)
  107615. +#define BCM2708_I2S_RXD BIT(20)
  107616. +#define BCM2708_I2S_TXD BIT(19)
  107617. +#define BCM2708_I2S_RXR BIT(18)
  107618. +#define BCM2708_I2S_TXW BIT(17)
  107619. +#define BCM2708_I2S_CS_RXERR BIT(16)
  107620. +#define BCM2708_I2S_CS_TXERR BIT(15)
  107621. +#define BCM2708_I2S_RXSYNC BIT(14)
  107622. +#define BCM2708_I2S_TXSYNC BIT(13)
  107623. +#define BCM2708_I2S_DMAEN BIT(9)
  107624. +#define BCM2708_I2S_RXTHR(v) ((v) << 7)
  107625. +#define BCM2708_I2S_TXTHR(v) ((v) << 5)
  107626. +#define BCM2708_I2S_RXCLR BIT(4)
  107627. +#define BCM2708_I2S_TXCLR BIT(3)
  107628. +#define BCM2708_I2S_TXON BIT(2)
  107629. +#define BCM2708_I2S_RXON BIT(1)
  107630. +#define BCM2708_I2S_EN (1)
  107631. +
  107632. +#define BCM2708_I2S_CLKDIS BIT(28)
  107633. +#define BCM2708_I2S_PDMN BIT(27)
  107634. +#define BCM2708_I2S_PDME BIT(26)
  107635. +#define BCM2708_I2S_FRXP BIT(25)
  107636. +#define BCM2708_I2S_FTXP BIT(24)
  107637. +#define BCM2708_I2S_CLKM BIT(23)
  107638. +#define BCM2708_I2S_CLKI BIT(22)
  107639. +#define BCM2708_I2S_FSM BIT(21)
  107640. +#define BCM2708_I2S_FSI BIT(20)
  107641. +#define BCM2708_I2S_FLEN(v) ((v) << 10)
  107642. +#define BCM2708_I2S_FSLEN(v) (v)
  107643. +
  107644. +#define BCM2708_I2S_CHWEX BIT(15)
  107645. +#define BCM2708_I2S_CHEN BIT(14)
  107646. +#define BCM2708_I2S_CHPOS(v) ((v) << 4)
  107647. +#define BCM2708_I2S_CHWID(v) (v)
  107648. +#define BCM2708_I2S_CH1(v) ((v) << 16)
  107649. +#define BCM2708_I2S_CH2(v) (v)
  107650. +
  107651. +#define BCM2708_I2S_TX_PANIC(v) ((v) << 24)
  107652. +#define BCM2708_I2S_RX_PANIC(v) ((v) << 16)
  107653. +#define BCM2708_I2S_TX(v) ((v) << 8)
  107654. +#define BCM2708_I2S_RX(v) (v)
  107655. +
  107656. +#define BCM2708_I2S_INT_RXERR BIT(3)
  107657. +#define BCM2708_I2S_INT_TXERR BIT(2)
  107658. +#define BCM2708_I2S_INT_RXR BIT(1)
  107659. +#define BCM2708_I2S_INT_TXW BIT(0)
  107660. +
  107661. +/* I2S DMA interface */
  107662. +#define BCM2708_I2S_FIFO_PHYSICAL_ADDR 0x7E203004
  107663. +#define BCM2708_DMA_DREQ_PCM_TX 2
  107664. +#define BCM2708_DMA_DREQ_PCM_RX 3
  107665. +
  107666. +/* I2S pin configuration */
  107667. +static int bcm2708_i2s_gpio=BCM2708_I2S_GPIO_AUTO;
  107668. +
  107669. +/* General device struct */
  107670. +struct bcm2708_i2s_dev {
  107671. + struct device *dev;
  107672. + struct snd_dmaengine_dai_dma_data dma_data[2];
  107673. + unsigned int fmt;
  107674. + unsigned int bclk_ratio;
  107675. +
  107676. + struct regmap *i2s_regmap;
  107677. + struct regmap *clk_regmap;
  107678. +};
  107679. +
  107680. +void bcm2708_i2s_set_gpio(int gpio) {
  107681. + bcm2708_i2s_gpio=gpio;
  107682. +}
  107683. +EXPORT_SYMBOL(bcm2708_i2s_set_gpio);
  107684. +
  107685. +
  107686. +static void bcm2708_i2s_start_clock(struct bcm2708_i2s_dev *dev)
  107687. +{
  107688. + /* Start the clock if in master mode */
  107689. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  107690. +
  107691. + switch (master) {
  107692. + case SND_SOC_DAIFMT_CBS_CFS:
  107693. + case SND_SOC_DAIFMT_CBS_CFM:
  107694. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  107695. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  107696. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  107697. + break;
  107698. + default:
  107699. + break;
  107700. + }
  107701. +}
  107702. +
  107703. +static void bcm2708_i2s_stop_clock(struct bcm2708_i2s_dev *dev)
  107704. +{
  107705. + uint32_t clkreg;
  107706. + int timeout = 1000;
  107707. +
  107708. + /* Stop clock */
  107709. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  107710. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  107711. + BCM2708_CLK_PASSWD);
  107712. +
  107713. + /* Wait for the BUSY flag going down */
  107714. + while (--timeout) {
  107715. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  107716. + if (!(clkreg & BCM2708_CLK_BUSY))
  107717. + break;
  107718. + }
  107719. +
  107720. + if (!timeout) {
  107721. + /* KILL the clock */
  107722. + dev_err(dev->dev, "I2S clock didn't stop. Kill the clock!\n");
  107723. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  107724. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD_MASK,
  107725. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD);
  107726. + }
  107727. +}
  107728. +
  107729. +static void bcm2708_i2s_clear_fifos(struct bcm2708_i2s_dev *dev,
  107730. + bool tx, bool rx)
  107731. +{
  107732. + int timeout = 1000;
  107733. + uint32_t syncval;
  107734. + uint32_t csreg;
  107735. + uint32_t i2s_active_state;
  107736. + uint32_t clkreg;
  107737. + uint32_t clk_active_state;
  107738. + uint32_t off;
  107739. + uint32_t clr;
  107740. +
  107741. + off = tx ? BCM2708_I2S_TXON : 0;
  107742. + off |= rx ? BCM2708_I2S_RXON : 0;
  107743. +
  107744. + clr = tx ? BCM2708_I2S_TXCLR : 0;
  107745. + clr |= rx ? BCM2708_I2S_RXCLR : 0;
  107746. +
  107747. + /* Backup the current state */
  107748. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  107749. + i2s_active_state = csreg & (BCM2708_I2S_RXON | BCM2708_I2S_TXON);
  107750. +
  107751. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  107752. + clk_active_state = clkreg & BCM2708_CLK_ENAB;
  107753. +
  107754. + /* Start clock if not running */
  107755. + if (!clk_active_state) {
  107756. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  107757. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  107758. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  107759. + }
  107760. +
  107761. + /* Stop I2S module */
  107762. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, off, 0);
  107763. +
  107764. + /*
  107765. + * Clear the FIFOs
  107766. + * Requires at least 2 PCM clock cycles to take effect
  107767. + */
  107768. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, clr, clr);
  107769. +
  107770. + /* Wait for 2 PCM clock cycles */
  107771. +
  107772. + /*
  107773. + * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  107774. + * FIXME: This does not seem to work for slave mode!
  107775. + */
  107776. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &syncval);
  107777. + syncval &= BCM2708_I2S_SYNC;
  107778. +
  107779. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  107780. + BCM2708_I2S_SYNC, ~syncval);
  107781. +
  107782. + /* Wait for the SYNC flag changing it's state */
  107783. + while (--timeout) {
  107784. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  107785. + if ((csreg & BCM2708_I2S_SYNC) != syncval)
  107786. + break;
  107787. + }
  107788. +
  107789. + if (!timeout)
  107790. + dev_err(dev->dev, "I2S SYNC error!\n");
  107791. +
  107792. + /* Stop clock if it was not running before */
  107793. + if (!clk_active_state)
  107794. + bcm2708_i2s_stop_clock(dev);
  107795. +
  107796. + /* Restore I2S state */
  107797. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  107798. + BCM2708_I2S_RXON | BCM2708_I2S_TXON, i2s_active_state);
  107799. +}
  107800. +
  107801. +static int bcm2708_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  107802. + unsigned int fmt)
  107803. +{
  107804. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  107805. + dev->fmt = fmt;
  107806. + return 0;
  107807. +}
  107808. +
  107809. +static int bcm2708_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  107810. + unsigned int ratio)
  107811. +{
  107812. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  107813. + dev->bclk_ratio = ratio;
  107814. + return 0;
  107815. +}
  107816. +
  107817. +
  107818. +static int bcm2708_i2s_set_function(unsigned offset, int function)
  107819. +{
  107820. + #define GPIOFSEL(x) (0x00+(x)*4)
  107821. + void __iomem *gpio = __io_address(GPIO_BASE);
  107822. + unsigned alt = function <= 3 ? function + 4: function == 4 ? 3 : 2;
  107823. + unsigned gpiodir;
  107824. + unsigned gpio_bank = offset / 10;
  107825. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  107826. +
  107827. + if (offset >= BCM2708_NR_GPIOS)
  107828. + return -EINVAL;
  107829. +
  107830. + gpiodir = readl(gpio + GPIOFSEL(gpio_bank));
  107831. + gpiodir &= ~(7 << gpio_field_offset);
  107832. + gpiodir |= alt << gpio_field_offset;
  107833. + writel(gpiodir, gpio + GPIOFSEL(gpio_bank));
  107834. + return 0;
  107835. +}
  107836. +
  107837. +static void bcm2708_i2s_setup_gpio(void)
  107838. +{
  107839. + /*
  107840. + * This is the common way to handle the GPIO pins for
  107841. + * the Raspberry Pi.
  107842. + * TODO Better way would be to handle
  107843. + * this in the device tree!
  107844. + */
  107845. + int pin,pinconfig,startpin,alt;
  107846. +
  107847. + /* SPI is on different GPIOs on different boards */
  107848. + /* for Raspberry Pi B+, this is pin GPIO18-21, for original on 28-31 */
  107849. + if (bcm2708_i2s_gpio==BCM2708_I2S_GPIO_AUTO) {
  107850. + if ((system_rev & 0xffffff) >= 0x10) {
  107851. + /* Model B+ */
  107852. + pinconfig=BCM2708_I2S_GPIO_PIN18;
  107853. + } else {
  107854. + /* original */
  107855. + pinconfig=BCM2708_I2S_GPIO_PIN28;
  107856. + }
  107857. + } else {
  107858. + pinconfig=bcm2708_i2s_gpio;
  107859. + }
  107860. +
  107861. + if (pinconfig==BCM2708_I2S_GPIO_PIN18) {
  107862. + startpin=18;
  107863. + alt=BCM2708_I2S_GPIO_PIN18_ALT;
  107864. + } else if (pinconfig==BCM2708_I2S_GPIO_PIN28) {
  107865. + startpin=28;
  107866. + alt=BCM2708_I2S_GPIO_PIN28_ALT;
  107867. + } else {
  107868. + printk(KERN_INFO "Can't configure I2S GPIOs, unknown pin mode for I2S: %i\n",pinconfig);
  107869. + return;
  107870. + }
  107871. +
  107872. + /* configure I2S pins to correct ALT mode */
  107873. + for (pin = startpin; pin <= startpin+3; pin++) {
  107874. + bcm2708_i2s_set_function(pin, alt);
  107875. + }
  107876. +}
  107877. +
  107878. +static int bcm2708_i2s_hw_params(struct snd_pcm_substream *substream,
  107879. + struct snd_pcm_hw_params *params,
  107880. + struct snd_soc_dai *dai)
  107881. +{
  107882. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  107883. +
  107884. + unsigned int sampling_rate = params_rate(params);
  107885. + unsigned int data_length, data_delay, bclk_ratio;
  107886. + unsigned int ch1pos, ch2pos, mode, format;
  107887. + unsigned int mash = BCM2708_CLK_MASH_1;
  107888. + unsigned int divi, divf, target_frequency;
  107889. + int clk_src = -1;
  107890. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  107891. + bool bit_master = (master == SND_SOC_DAIFMT_CBS_CFS
  107892. + || master == SND_SOC_DAIFMT_CBS_CFM);
  107893. +
  107894. + bool frame_master = (master == SND_SOC_DAIFMT_CBS_CFS
  107895. + || master == SND_SOC_DAIFMT_CBM_CFS);
  107896. + uint32_t csreg;
  107897. +
  107898. + /*
  107899. + * If a stream is already enabled,
  107900. + * the registers are already set properly.
  107901. + */
  107902. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  107903. +
  107904. + if (csreg & (BCM2708_I2S_TXON | BCM2708_I2S_RXON))
  107905. + return 0;
  107906. +
  107907. +
  107908. + bcm2708_i2s_setup_gpio();
  107909. +
  107910. + /*
  107911. + * Adjust the data length according to the format.
  107912. + * We prefill the half frame length with an integer
  107913. + * divider of 2400 as explained at the clock settings.
  107914. + * Maybe it is overwritten there, if the Integer mode
  107915. + * does not apply.
  107916. + */
  107917. + switch (params_format(params)) {
  107918. + case SNDRV_PCM_FORMAT_S16_LE:
  107919. + data_length = 16;
  107920. + bclk_ratio = 50;
  107921. + break;
  107922. + case SNDRV_PCM_FORMAT_S24_LE:
  107923. + data_length = 24;
  107924. + bclk_ratio = 50;
  107925. + break;
  107926. + case SNDRV_PCM_FORMAT_S32_LE:
  107927. + data_length = 32;
  107928. + bclk_ratio = 100;
  107929. + break;
  107930. + default:
  107931. + return -EINVAL;
  107932. + }
  107933. +
  107934. + /* If bclk_ratio already set, use that one. */
  107935. + if (dev->bclk_ratio)
  107936. + bclk_ratio = dev->bclk_ratio;
  107937. +
  107938. + /*
  107939. + * Clock Settings
  107940. + *
  107941. + * The target frequency of the bit clock is
  107942. + * sampling rate * frame length
  107943. + *
  107944. + * Integer mode:
  107945. + * Sampling rates that are multiples of 8000 kHz
  107946. + * can be driven by the oscillator of 19.2 MHz
  107947. + * with an integer divider as long as the frame length
  107948. + * is an integer divider of 19200000/8000=2400 as set up above.
  107949. + * This is no longer possible if the sampling rate
  107950. + * is too high (e.g. 192 kHz), because the oscillator is too slow.
  107951. + *
  107952. + * MASH mode:
  107953. + * For all other sampling rates, it is not possible to
  107954. + * have an integer divider. Approximate the clock
  107955. + * with the MASH module that induces a slight frequency
  107956. + * variance. To minimize that it is best to have the fastest
  107957. + * clock here. That is PLLD with 500 MHz.
  107958. + */
  107959. + target_frequency = sampling_rate * bclk_ratio;
  107960. + clk_src = BCM2708_CLK_SRC_OSC;
  107961. + mash = BCM2708_CLK_MASH_0;
  107962. +
  107963. + if (bcm2708_clk_freq[clk_src] % target_frequency == 0
  107964. + && bit_master && frame_master) {
  107965. + divi = bcm2708_clk_freq[clk_src] / target_frequency;
  107966. + divf = 0;
  107967. + } else {
  107968. + uint64_t dividend;
  107969. +
  107970. + if (!dev->bclk_ratio) {
  107971. + /*
  107972. + * Overwrite bclk_ratio, because the
  107973. + * above trick is not needed or can
  107974. + * not be used.
  107975. + */
  107976. + bclk_ratio = 2 * data_length;
  107977. + }
  107978. +
  107979. + target_frequency = sampling_rate * bclk_ratio;
  107980. +
  107981. + clk_src = BCM2708_CLK_SRC_PLLD;
  107982. + mash = BCM2708_CLK_MASH_1;
  107983. +
  107984. + dividend = bcm2708_clk_freq[clk_src];
  107985. + dividend <<= BCM2708_CLK_SHIFT;
  107986. + do_div(dividend, target_frequency);
  107987. + divi = dividend >> BCM2708_CLK_SHIFT;
  107988. + divf = dividend & BCM2708_CLK_DIVF_MASK;
  107989. + }
  107990. +
  107991. + /* Clock should only be set up here if CPU is clock master */
  107992. + if (((dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBS_CFS) ||
  107993. + ((dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBS_CFM)) {
  107994. + /* Set clock divider */
  107995. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMDIV_REG, BCM2708_CLK_PASSWD
  107996. + | BCM2708_CLK_DIVI(divi)
  107997. + | BCM2708_CLK_DIVF(divf));
  107998. +
  107999. + /* Setup clock, but don't start it yet */
  108000. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, BCM2708_CLK_PASSWD
  108001. + | BCM2708_CLK_MASH(mash)
  108002. + | BCM2708_CLK_SRC(clk_src));
  108003. + }
  108004. +
  108005. + /* Setup the frame format */
  108006. + format = BCM2708_I2S_CHEN;
  108007. +
  108008. + if (data_length >= 24)
  108009. + format |= BCM2708_I2S_CHWEX;
  108010. +
  108011. + format |= BCM2708_I2S_CHWID((data_length-8)&0xf);
  108012. +
  108013. + switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  108014. + case SND_SOC_DAIFMT_I2S:
  108015. + data_delay = 1;
  108016. + break;
  108017. + default:
  108018. + /*
  108019. + * TODO
  108020. + * Others are possible but are not implemented at the moment.
  108021. + */
  108022. + dev_err(dev->dev, "%s:bad format\n", __func__);
  108023. + return -EINVAL;
  108024. + }
  108025. +
  108026. + ch1pos = data_delay;
  108027. + ch2pos = bclk_ratio / 2 + data_delay;
  108028. +
  108029. + switch (params_channels(params)) {
  108030. + case 2:
  108031. + format = BCM2708_I2S_CH1(format) | BCM2708_I2S_CH2(format);
  108032. + format |= BCM2708_I2S_CH1(BCM2708_I2S_CHPOS(ch1pos));
  108033. + format |= BCM2708_I2S_CH2(BCM2708_I2S_CHPOS(ch2pos));
  108034. + break;
  108035. + default:
  108036. + return -EINVAL;
  108037. + }
  108038. +
  108039. + /*
  108040. + * Set format for both streams.
  108041. + * We cannot set another frame length
  108042. + * (and therefore word length) anyway,
  108043. + * so the format will be the same.
  108044. + */
  108045. + regmap_write(dev->i2s_regmap, BCM2708_I2S_RXC_A_REG, format);
  108046. + regmap_write(dev->i2s_regmap, BCM2708_I2S_TXC_A_REG, format);
  108047. +
  108048. + /* Setup the I2S mode */
  108049. + mode = 0;
  108050. +
  108051. + if (data_length <= 16) {
  108052. + /*
  108053. + * Use frame packed mode (2 channels per 32 bit word)
  108054. + * We cannot set another frame length in the second stream
  108055. + * (and therefore word length) anyway,
  108056. + * so the format will be the same.
  108057. + */
  108058. + mode |= BCM2708_I2S_FTXP | BCM2708_I2S_FRXP;
  108059. + }
  108060. +
  108061. + mode |= BCM2708_I2S_FLEN(bclk_ratio - 1);
  108062. + mode |= BCM2708_I2S_FSLEN(bclk_ratio / 2);
  108063. +
  108064. + /* Master or slave? */
  108065. + switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  108066. + case SND_SOC_DAIFMT_CBS_CFS:
  108067. + /* CPU is master */
  108068. + break;
  108069. + case SND_SOC_DAIFMT_CBM_CFS:
  108070. + /*
  108071. + * CODEC is bit clock master
  108072. + * CPU is frame master
  108073. + */
  108074. + mode |= BCM2708_I2S_CLKM;
  108075. + break;
  108076. + case SND_SOC_DAIFMT_CBS_CFM:
  108077. + /*
  108078. + * CODEC is frame master
  108079. + * CPU is bit clock master
  108080. + */
  108081. + mode |= BCM2708_I2S_FSM;
  108082. + break;
  108083. + case SND_SOC_DAIFMT_CBM_CFM:
  108084. + /* CODEC is master */
  108085. + mode |= BCM2708_I2S_CLKM;
  108086. + mode |= BCM2708_I2S_FSM;
  108087. + break;
  108088. + default:
  108089. + dev_err(dev->dev, "%s:bad master\n", __func__);
  108090. + return -EINVAL;
  108091. + }
  108092. +
  108093. + /*
  108094. + * Invert clocks?
  108095. + *
  108096. + * The BCM approach seems to be inverted to the classical I2S approach.
  108097. + */
  108098. + switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  108099. + case SND_SOC_DAIFMT_NB_NF:
  108100. + /* None. Therefore, both for BCM */
  108101. + mode |= BCM2708_I2S_CLKI;
  108102. + mode |= BCM2708_I2S_FSI;
  108103. + break;
  108104. + case SND_SOC_DAIFMT_IB_IF:
  108105. + /* Both. Therefore, none for BCM */
  108106. + break;
  108107. + case SND_SOC_DAIFMT_NB_IF:
  108108. + /*
  108109. + * Invert only frame sync. Therefore,
  108110. + * invert only bit clock for BCM
  108111. + */
  108112. + mode |= BCM2708_I2S_CLKI;
  108113. + break;
  108114. + case SND_SOC_DAIFMT_IB_NF:
  108115. + /*
  108116. + * Invert only bit clock. Therefore,
  108117. + * invert only frame sync for BCM
  108118. + */
  108119. + mode |= BCM2708_I2S_FSI;
  108120. + break;
  108121. + default:
  108122. + return -EINVAL;
  108123. + }
  108124. +
  108125. + regmap_write(dev->i2s_regmap, BCM2708_I2S_MODE_A_REG, mode);
  108126. +
  108127. + /* Setup the DMA parameters */
  108128. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108129. + BCM2708_I2S_RXTHR(1)
  108130. + | BCM2708_I2S_TXTHR(1)
  108131. + | BCM2708_I2S_DMAEN, 0xffffffff);
  108132. +
  108133. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_DREQ_A_REG,
  108134. + BCM2708_I2S_TX_PANIC(0x10)
  108135. + | BCM2708_I2S_RX_PANIC(0x30)
  108136. + | BCM2708_I2S_TX(0x30)
  108137. + | BCM2708_I2S_RX(0x20), 0xffffffff);
  108138. +
  108139. + /* Clear FIFOs */
  108140. + bcm2708_i2s_clear_fifos(dev, true, true);
  108141. +
  108142. + return 0;
  108143. +}
  108144. +
  108145. +static int bcm2708_i2s_prepare(struct snd_pcm_substream *substream,
  108146. + struct snd_soc_dai *dai)
  108147. +{
  108148. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108149. + uint32_t cs_reg;
  108150. +
  108151. + bcm2708_i2s_start_clock(dev);
  108152. +
  108153. + /*
  108154. + * Clear both FIFOs if the one that should be started
  108155. + * is not empty at the moment. This should only happen
  108156. + * after overrun. Otherwise, hw_params would have cleared
  108157. + * the FIFO.
  108158. + */
  108159. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &cs_reg);
  108160. +
  108161. + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  108162. + && !(cs_reg & BCM2708_I2S_TXE))
  108163. + bcm2708_i2s_clear_fifos(dev, true, false);
  108164. + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  108165. + && (cs_reg & BCM2708_I2S_RXD))
  108166. + bcm2708_i2s_clear_fifos(dev, false, true);
  108167. +
  108168. + return 0;
  108169. +}
  108170. +
  108171. +static void bcm2708_i2s_stop(struct bcm2708_i2s_dev *dev,
  108172. + struct snd_pcm_substream *substream,
  108173. + struct snd_soc_dai *dai)
  108174. +{
  108175. + uint32_t mask;
  108176. +
  108177. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  108178. + mask = BCM2708_I2S_RXON;
  108179. + else
  108180. + mask = BCM2708_I2S_TXON;
  108181. +
  108182. + regmap_update_bits(dev->i2s_regmap,
  108183. + BCM2708_I2S_CS_A_REG, mask, 0);
  108184. +
  108185. + /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  108186. + if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  108187. + bcm2708_i2s_stop_clock(dev);
  108188. +}
  108189. +
  108190. +static int bcm2708_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  108191. + struct snd_soc_dai *dai)
  108192. +{
  108193. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108194. + uint32_t mask;
  108195. +
  108196. + switch (cmd) {
  108197. + case SNDRV_PCM_TRIGGER_START:
  108198. + case SNDRV_PCM_TRIGGER_RESUME:
  108199. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  108200. + bcm2708_i2s_start_clock(dev);
  108201. +
  108202. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  108203. + mask = BCM2708_I2S_RXON;
  108204. + else
  108205. + mask = BCM2708_I2S_TXON;
  108206. +
  108207. + regmap_update_bits(dev->i2s_regmap,
  108208. + BCM2708_I2S_CS_A_REG, mask, mask);
  108209. + break;
  108210. +
  108211. + case SNDRV_PCM_TRIGGER_STOP:
  108212. + case SNDRV_PCM_TRIGGER_SUSPEND:
  108213. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  108214. + bcm2708_i2s_stop(dev, substream, dai);
  108215. + break;
  108216. + default:
  108217. + return -EINVAL;
  108218. + }
  108219. +
  108220. + return 0;
  108221. +}
  108222. +
  108223. +static int bcm2708_i2s_startup(struct snd_pcm_substream *substream,
  108224. + struct snd_soc_dai *dai)
  108225. +{
  108226. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108227. +
  108228. + if (dai->active)
  108229. + return 0;
  108230. +
  108231. + /* Should this still be running stop it */
  108232. + bcm2708_i2s_stop_clock(dev);
  108233. +
  108234. + /* Enable PCM block */
  108235. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108236. + BCM2708_I2S_EN, BCM2708_I2S_EN);
  108237. +
  108238. + /*
  108239. + * Disable STBY.
  108240. + * Requires at least 4 PCM clock cycles to take effect.
  108241. + */
  108242. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108243. + BCM2708_I2S_STBY, BCM2708_I2S_STBY);
  108244. +
  108245. + return 0;
  108246. +}
  108247. +
  108248. +static void bcm2708_i2s_shutdown(struct snd_pcm_substream *substream,
  108249. + struct snd_soc_dai *dai)
  108250. +{
  108251. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108252. +
  108253. + bcm2708_i2s_stop(dev, substream, dai);
  108254. +
  108255. + /* If both streams are stopped, disable module and clock */
  108256. + if (dai->active)
  108257. + return;
  108258. +
  108259. + /* Disable the module */
  108260. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108261. + BCM2708_I2S_EN, 0);
  108262. +
  108263. + /*
  108264. + * Stopping clock is necessary, because stop does
  108265. + * not stop the clock when SND_SOC_DAIFMT_CONT
  108266. + */
  108267. + bcm2708_i2s_stop_clock(dev);
  108268. +}
  108269. +
  108270. +static const struct snd_soc_dai_ops bcm2708_i2s_dai_ops = {
  108271. + .startup = bcm2708_i2s_startup,
  108272. + .shutdown = bcm2708_i2s_shutdown,
  108273. + .prepare = bcm2708_i2s_prepare,
  108274. + .trigger = bcm2708_i2s_trigger,
  108275. + .hw_params = bcm2708_i2s_hw_params,
  108276. + .set_fmt = bcm2708_i2s_set_dai_fmt,
  108277. + .set_bclk_ratio = bcm2708_i2s_set_dai_bclk_ratio
  108278. +};
  108279. +
  108280. +static int bcm2708_i2s_dai_probe(struct snd_soc_dai *dai)
  108281. +{
  108282. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108283. +
  108284. + dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  108285. + dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  108286. +
  108287. + return 0;
  108288. +}
  108289. +
  108290. +static struct snd_soc_dai_driver bcm2708_i2s_dai = {
  108291. + .name = "bcm2708-i2s",
  108292. + .probe = bcm2708_i2s_dai_probe,
  108293. + .playback = {
  108294. + .channels_min = 2,
  108295. + .channels_max = 2,
  108296. + .rates = SNDRV_PCM_RATE_8000_192000,
  108297. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  108298. + | SNDRV_PCM_FMTBIT_S24_LE
  108299. + | SNDRV_PCM_FMTBIT_S32_LE
  108300. + },
  108301. + .capture = {
  108302. + .channels_min = 2,
  108303. + .channels_max = 2,
  108304. + .rates = SNDRV_PCM_RATE_8000_192000,
  108305. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  108306. + | SNDRV_PCM_FMTBIT_S24_LE
  108307. + | SNDRV_PCM_FMTBIT_S32_LE
  108308. + },
  108309. + .ops = &bcm2708_i2s_dai_ops,
  108310. + .symmetric_rates = 1
  108311. +};
  108312. +
  108313. +static bool bcm2708_i2s_volatile_reg(struct device *dev, unsigned int reg)
  108314. +{
  108315. + switch (reg) {
  108316. + case BCM2708_I2S_CS_A_REG:
  108317. + case BCM2708_I2S_FIFO_A_REG:
  108318. + case BCM2708_I2S_INTSTC_A_REG:
  108319. + case BCM2708_I2S_GRAY_REG:
  108320. + return true;
  108321. + default:
  108322. + return false;
  108323. + };
  108324. +}
  108325. +
  108326. +static bool bcm2708_i2s_precious_reg(struct device *dev, unsigned int reg)
  108327. +{
  108328. + switch (reg) {
  108329. + case BCM2708_I2S_FIFO_A_REG:
  108330. + return true;
  108331. + default:
  108332. + return false;
  108333. + };
  108334. +}
  108335. +
  108336. +static bool bcm2708_clk_volatile_reg(struct device *dev, unsigned int reg)
  108337. +{
  108338. + switch (reg) {
  108339. + case BCM2708_CLK_PCMCTL_REG:
  108340. + return true;
  108341. + default:
  108342. + return false;
  108343. + };
  108344. +}
  108345. +
  108346. +static const struct regmap_config bcm2708_regmap_config[] = {
  108347. + {
  108348. + .reg_bits = 32,
  108349. + .reg_stride = 4,
  108350. + .val_bits = 32,
  108351. + .max_register = BCM2708_I2S_GRAY_REG,
  108352. + .precious_reg = bcm2708_i2s_precious_reg,
  108353. + .volatile_reg = bcm2708_i2s_volatile_reg,
  108354. + .cache_type = REGCACHE_RBTREE,
  108355. + .name = "i2s",
  108356. + },
  108357. + {
  108358. + .reg_bits = 32,
  108359. + .reg_stride = 4,
  108360. + .val_bits = 32,
  108361. + .max_register = BCM2708_CLK_PCMDIV_REG,
  108362. + .volatile_reg = bcm2708_clk_volatile_reg,
  108363. + .cache_type = REGCACHE_RBTREE,
  108364. + .name = "clk",
  108365. + },
  108366. +};
  108367. +
  108368. +static const struct snd_soc_component_driver bcm2708_i2s_component = {
  108369. + .name = "bcm2708-i2s-comp",
  108370. +};
  108371. +
  108372. +static const struct snd_pcm_hardware bcm2708_pcm_hardware = {
  108373. + .info = SNDRV_PCM_INFO_INTERLEAVED |
  108374. + SNDRV_PCM_INFO_JOINT_DUPLEX,
  108375. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  108376. + SNDRV_PCM_FMTBIT_S24_LE |
  108377. + SNDRV_PCM_FMTBIT_S32_LE,
  108378. + .period_bytes_min = 32,
  108379. + .period_bytes_max = 64 * PAGE_SIZE,
  108380. + .periods_min = 2,
  108381. + .periods_max = 255,
  108382. + .buffer_bytes_max = 128 * PAGE_SIZE,
  108383. +};
  108384. +
  108385. +static const struct snd_dmaengine_pcm_config bcm2708_dmaengine_pcm_config = {
  108386. + .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  108387. + .pcm_hardware = &bcm2708_pcm_hardware,
  108388. + .prealloc_buffer_size = 256 * PAGE_SIZE,
  108389. +};
  108390. +
  108391. +
  108392. +static int bcm2708_i2s_probe(struct platform_device *pdev)
  108393. +{
  108394. + struct bcm2708_i2s_dev *dev;
  108395. + int i;
  108396. + int ret;
  108397. + struct regmap *regmap[2];
  108398. + struct resource *mem[2];
  108399. +
  108400. + /* Request both ioareas */
  108401. + for (i = 0; i <= 1; i++) {
  108402. + void __iomem *base;
  108403. +
  108404. + mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
  108405. + base = devm_ioremap_resource(&pdev->dev, mem[i]);
  108406. + if (IS_ERR(base))
  108407. + return PTR_ERR(base);
  108408. +
  108409. + regmap[i] = devm_regmap_init_mmio(&pdev->dev, base,
  108410. + &bcm2708_regmap_config[i]);
  108411. + if (IS_ERR(regmap[i])) {
  108412. + dev_err(&pdev->dev, "I2S probe: regmap init failed\n");
  108413. + return PTR_ERR(regmap[i]);
  108414. + }
  108415. + }
  108416. +
  108417. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  108418. + GFP_KERNEL);
  108419. + if (IS_ERR(dev))
  108420. + return PTR_ERR(dev);
  108421. +
  108422. + dev->i2s_regmap = regmap[0];
  108423. + dev->clk_regmap = regmap[1];
  108424. +
  108425. + /* Set the DMA address */
  108426. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  108427. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  108428. +
  108429. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  108430. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  108431. +
  108432. + /* Set the DREQ */
  108433. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
  108434. + BCM2708_DMA_DREQ_PCM_TX;
  108435. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
  108436. + BCM2708_DMA_DREQ_PCM_RX;
  108437. +
  108438. + /* Set the bus width */
  108439. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  108440. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  108441. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  108442. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  108443. +
  108444. + /* Set burst */
  108445. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  108446. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  108447. +
  108448. + /* BCLK ratio - use default */
  108449. + dev->bclk_ratio = 0;
  108450. +
  108451. + /* Store the pdev */
  108452. + dev->dev = &pdev->dev;
  108453. + dev_set_drvdata(&pdev->dev, dev);
  108454. +
  108455. + ret = snd_soc_register_component(&pdev->dev,
  108456. + &bcm2708_i2s_component, &bcm2708_i2s_dai, 1);
  108457. +
  108458. + if (ret) {
  108459. + dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  108460. + ret = -ENOMEM;
  108461. + return ret;
  108462. + }
  108463. +
  108464. + ret = snd_dmaengine_pcm_register(&pdev->dev,
  108465. + &bcm2708_dmaengine_pcm_config,
  108466. + SND_DMAENGINE_PCM_FLAG_COMPAT);
  108467. + if (ret) {
  108468. + dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  108469. + snd_soc_unregister_component(&pdev->dev);
  108470. + return ret;
  108471. + }
  108472. +
  108473. + return 0;
  108474. +}
  108475. +
  108476. +static int bcm2708_i2s_remove(struct platform_device *pdev)
  108477. +{
  108478. + snd_dmaengine_pcm_unregister(&pdev->dev);
  108479. + snd_soc_unregister_component(&pdev->dev);
  108480. + return 0;
  108481. +}
  108482. +
  108483. +static const struct of_device_id bcm2708_i2s_of_match[] = {
  108484. + { .compatible = "brcm,bcm2708-i2s", },
  108485. + {},
  108486. +};
  108487. +MODULE_DEVICE_TABLE(of, bcm2708_i2s_of_match);
  108488. +
  108489. +static struct platform_driver bcm2708_i2s_driver = {
  108490. + .probe = bcm2708_i2s_probe,
  108491. + .remove = bcm2708_i2s_remove,
  108492. + .driver = {
  108493. + .name = "bcm2708-i2s",
  108494. + .owner = THIS_MODULE,
  108495. + .of_match_table = bcm2708_i2s_of_match,
  108496. + },
  108497. +};
  108498. +
  108499. +module_platform_driver(bcm2708_i2s_driver);
  108500. +
  108501. +MODULE_ALIAS("platform:bcm2708-i2s");
  108502. +MODULE_DESCRIPTION("BCM2708 I2S interface");
  108503. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  108504. +MODULE_LICENSE("GPL v2");
  108505. diff -Nur linux-3.17.5/sound/soc/bcm/bcm2708-i2s.h linux-rpi/sound/soc/bcm/bcm2708-i2s.h
  108506. --- linux-3.17.5/sound/soc/bcm/bcm2708-i2s.h 1969-12-31 18:00:00.000000000 -0600
  108507. +++ linux-rpi/sound/soc/bcm/bcm2708-i2s.h 2014-12-11 14:02:58.108418001 -0600
  108508. @@ -0,0 +1,35 @@
  108509. +/*
  108510. + * I2S configuration for sound cards.
  108511. + *
  108512. + * Copyright (c) 2014 Daniel Matuschek <daniel@hifiberry.com>
  108513. + *
  108514. + * This program is free software; you can redistribute it and/or modify
  108515. + * it under the terms of the GNU General Public License as published by
  108516. + * the Free Software Foundation; either version 2 of the License, or
  108517. + * (at your option) any later version.
  108518. + *
  108519. + * This program is distributed in the hope that it will be useful,
  108520. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  108521. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  108522. + * GNU General Public License for more details.
  108523. + *
  108524. + * You should have received a copy of the GNU General Public License
  108525. + * along with this program; if not, write to the Free Software
  108526. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  108527. + */
  108528. +
  108529. +#ifndef BCM2708_I2S_H
  108530. +#define BCM2708_I2S_H
  108531. +
  108532. +/* I2S pin assignment */
  108533. +#define BCM2708_I2S_GPIO_AUTO 0
  108534. +#define BCM2708_I2S_GPIO_PIN18 1
  108535. +#define BCM2708_I2S_GPIO_PIN28 2
  108536. +
  108537. +/* Alt mode to enable I2S */
  108538. +#define BCM2708_I2S_GPIO_PIN18_ALT 0
  108539. +#define BCM2708_I2S_GPIO_PIN28_ALT 2
  108540. +
  108541. +extern void bcm2708_i2s_set_gpio(int gpio);
  108542. +
  108543. +#endif
  108544. diff -Nur linux-3.17.5/sound/soc/bcm/bcm2835-i2s.c linux-rpi/sound/soc/bcm/bcm2835-i2s.c
  108545. --- linux-3.17.5/sound/soc/bcm/bcm2835-i2s.c 2014-12-06 17:57:59.000000000 -0600
  108546. +++ linux-rpi/sound/soc/bcm/bcm2835-i2s.c 2014-12-11 14:05:40.508418001 -0600
  108547. @@ -861,6 +861,7 @@
  108548. { .compatible = "brcm,bcm2835-i2s", },
  108549. {},
  108550. };
  108551. +MODULE_DEVICE_TABLE(of, bcm2835_i2s_of_match);
  108552. static struct platform_driver bcm2835_i2s_driver = {
  108553. .probe = bcm2835_i2s_probe,
  108554. diff -Nur linux-3.17.5/sound/soc/bcm/hifiberry_amp.c linux-rpi/sound/soc/bcm/hifiberry_amp.c
  108555. --- linux-3.17.5/sound/soc/bcm/hifiberry_amp.c 1969-12-31 18:00:00.000000000 -0600
  108556. +++ linux-rpi/sound/soc/bcm/hifiberry_amp.c 2014-12-11 14:05:40.508418001 -0600
  108557. @@ -0,0 +1,106 @@
  108558. +/*
  108559. + * ASoC Driver for HifiBerry AMP
  108560. + *
  108561. + * Author: Sebastian Eickhoff <basti.eickhoff@googlemail.com>
  108562. + * Copyright 2014
  108563. + *
  108564. + * This program is free software; you can redistribute it and/or
  108565. + * modify it under the terms of the GNU General Public License
  108566. + * version 2 as published by the Free Software Foundation.
  108567. + *
  108568. + * This program is distributed in the hope that it will be useful, but
  108569. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  108570. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  108571. + * General Public License for more details.
  108572. + */
  108573. +
  108574. +#include <linux/module.h>
  108575. +#include <linux/platform_device.h>
  108576. +
  108577. +#include <sound/core.h>
  108578. +#include <sound/pcm.h>
  108579. +#include <sound/pcm_params.h>
  108580. +#include <sound/soc.h>
  108581. +#include <sound/jack.h>
  108582. +
  108583. +static int snd_rpi_hifiberry_amp_init(struct snd_soc_pcm_runtime *rtd)
  108584. +{
  108585. + // ToDo: init of the dsp-registers.
  108586. + return 0;
  108587. +}
  108588. +
  108589. +static int snd_rpi_hifiberry_amp_hw_params( struct snd_pcm_substream *substream,
  108590. + struct snd_pcm_hw_params *params )
  108591. +{
  108592. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108593. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  108594. +
  108595. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
  108596. +}
  108597. +
  108598. +static struct snd_soc_ops snd_rpi_hifiberry_amp_ops = {
  108599. + .hw_params = snd_rpi_hifiberry_amp_hw_params,
  108600. +};
  108601. +
  108602. +static struct snd_soc_dai_link snd_rpi_hifiberry_amp_dai[] = {
  108603. + {
  108604. + .name = "HifiBerry AMP",
  108605. + .stream_name = "HifiBerry AMP HiFi",
  108606. + .cpu_dai_name = "bcm2708-i2s.0",
  108607. + .codec_dai_name = "tas5713-hifi",
  108608. + .platform_name = "bcm2708-i2s.0",
  108609. + .codec_name = "tas5713.1-001b",
  108610. + .dai_fmt = SND_SOC_DAIFMT_I2S |
  108611. + SND_SOC_DAIFMT_NB_NF |
  108612. + SND_SOC_DAIFMT_CBS_CFS,
  108613. + .ops = &snd_rpi_hifiberry_amp_ops,
  108614. + .init = snd_rpi_hifiberry_amp_init,
  108615. + },
  108616. +};
  108617. +
  108618. +
  108619. +static struct snd_soc_card snd_rpi_hifiberry_amp = {
  108620. + .name = "snd_rpi_hifiberry_amp",
  108621. + .dai_link = snd_rpi_hifiberry_amp_dai,
  108622. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_amp_dai),
  108623. +};
  108624. +
  108625. +
  108626. +static int snd_rpi_hifiberry_amp_probe(struct platform_device *pdev)
  108627. +{
  108628. + int ret = 0;
  108629. +
  108630. + snd_rpi_hifiberry_amp.dev = &pdev->dev;
  108631. +
  108632. + ret = snd_soc_register_card(&snd_rpi_hifiberry_amp);
  108633. +
  108634. + if (ret != 0) {
  108635. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  108636. + }
  108637. +
  108638. + return ret;
  108639. +}
  108640. +
  108641. +
  108642. +static int snd_rpi_hifiberry_amp_remove(struct platform_device *pdev)
  108643. +{
  108644. + return snd_soc_unregister_card(&snd_rpi_hifiberry_amp);
  108645. +}
  108646. +
  108647. +
  108648. +static struct platform_driver snd_rpi_hifiberry_amp_driver = {
  108649. + .driver = {
  108650. + .name = "snd-hifiberry-amp",
  108651. + .owner = THIS_MODULE,
  108652. + },
  108653. + .probe = snd_rpi_hifiberry_amp_probe,
  108654. + .remove = snd_rpi_hifiberry_amp_remove,
  108655. +};
  108656. +
  108657. +
  108658. +module_platform_driver(snd_rpi_hifiberry_amp_driver);
  108659. +
  108660. +
  108661. +MODULE_AUTHOR("Sebastian Eickhoff <basti.eickhoff@googlemail.com>");
  108662. +MODULE_DESCRIPTION("ASoC driver for HiFiBerry-AMP");
  108663. +MODULE_LICENSE("GPL v2");
  108664. diff -Nur linux-3.17.5/sound/soc/bcm/hifiberry_dac.c linux-rpi/sound/soc/bcm/hifiberry_dac.c
  108665. --- linux-3.17.5/sound/soc/bcm/hifiberry_dac.c 1969-12-31 18:00:00.000000000 -0600
  108666. +++ linux-rpi/sound/soc/bcm/hifiberry_dac.c 2014-12-11 14:05:40.508418001 -0600
  108667. @@ -0,0 +1,122 @@
  108668. +/*
  108669. + * ASoC Driver for HifiBerry DAC
  108670. + *
  108671. + * Author: Florian Meier <florian.meier@koalo.de>
  108672. + * Copyright 2013
  108673. + *
  108674. + * This program is free software; you can redistribute it and/or
  108675. + * modify it under the terms of the GNU General Public License
  108676. + * version 2 as published by the Free Software Foundation.
  108677. + *
  108678. + * This program is distributed in the hope that it will be useful, but
  108679. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  108680. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  108681. + * General Public License for more details.
  108682. + */
  108683. +
  108684. +#include <linux/module.h>
  108685. +#include <linux/platform_device.h>
  108686. +
  108687. +#include <sound/core.h>
  108688. +#include <sound/pcm.h>
  108689. +#include <sound/pcm_params.h>
  108690. +#include <sound/soc.h>
  108691. +#include <sound/jack.h>
  108692. +
  108693. +static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
  108694. +{
  108695. + return 0;
  108696. +}
  108697. +
  108698. +static int snd_rpi_hifiberry_dac_hw_params(struct snd_pcm_substream *substream,
  108699. + struct snd_pcm_hw_params *params)
  108700. +{
  108701. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108702. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  108703. +
  108704. + unsigned int sample_bits =
  108705. + snd_pcm_format_physical_width(params_format(params));
  108706. +
  108707. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  108708. +}
  108709. +
  108710. +/* machine stream operations */
  108711. +static struct snd_soc_ops snd_rpi_hifiberry_dac_ops = {
  108712. + .hw_params = snd_rpi_hifiberry_dac_hw_params,
  108713. +};
  108714. +
  108715. +static struct snd_soc_dai_link snd_rpi_hifiberry_dac_dai[] = {
  108716. +{
  108717. + .name = "HifiBerry DAC",
  108718. + .stream_name = "HifiBerry DAC HiFi",
  108719. + .cpu_dai_name = "bcm2708-i2s.0",
  108720. + .codec_dai_name = "pcm5102a-hifi",
  108721. + .platform_name = "bcm2708-i2s.0",
  108722. + .codec_name = "pcm5102a-codec",
  108723. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  108724. + SND_SOC_DAIFMT_CBS_CFS,
  108725. + .ops = &snd_rpi_hifiberry_dac_ops,
  108726. + .init = snd_rpi_hifiberry_dac_init,
  108727. +},
  108728. +};
  108729. +
  108730. +/* audio machine driver */
  108731. +static struct snd_soc_card snd_rpi_hifiberry_dac = {
  108732. + .name = "snd_rpi_hifiberry_dac",
  108733. + .dai_link = snd_rpi_hifiberry_dac_dai,
  108734. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dac_dai),
  108735. +};
  108736. +
  108737. +static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
  108738. +{
  108739. + int ret = 0;
  108740. +
  108741. + snd_rpi_hifiberry_dac.dev = &pdev->dev;
  108742. +
  108743. + if (pdev->dev.of_node) {
  108744. + struct device_node *i2s_node;
  108745. + struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_dac_dai[0];
  108746. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  108747. + "i2s-controller", 0);
  108748. +
  108749. + if (i2s_node) {
  108750. + dai->cpu_dai_name = NULL;
  108751. + dai->cpu_of_node = i2s_node;
  108752. + dai->platform_name = NULL;
  108753. + dai->platform_of_node = i2s_node;
  108754. + }
  108755. + }
  108756. +
  108757. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);
  108758. + if (ret)
  108759. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  108760. +
  108761. + return ret;
  108762. +}
  108763. +
  108764. +static int snd_rpi_hifiberry_dac_remove(struct platform_device *pdev)
  108765. +{
  108766. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dac);
  108767. +}
  108768. +
  108769. +static const struct of_device_id snd_rpi_hifiberry_dac_of_match[] = {
  108770. + { .compatible = "hifiberry,hifiberry-dac", },
  108771. + {},
  108772. +};
  108773. +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dac_of_match);
  108774. +
  108775. +static struct platform_driver snd_rpi_hifiberry_dac_driver = {
  108776. + .driver = {
  108777. + .name = "snd-hifiberry-dac",
  108778. + .owner = THIS_MODULE,
  108779. + .of_match_table = snd_rpi_hifiberry_dac_of_match,
  108780. + },
  108781. + .probe = snd_rpi_hifiberry_dac_probe,
  108782. + .remove = snd_rpi_hifiberry_dac_remove,
  108783. +};
  108784. +
  108785. +module_platform_driver(snd_rpi_hifiberry_dac_driver);
  108786. +
  108787. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  108788. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry DAC");
  108789. +MODULE_LICENSE("GPL v2");
  108790. diff -Nur linux-3.17.5/sound/soc/bcm/hifiberry_dacplus.c linux-rpi/sound/soc/bcm/hifiberry_dacplus.c
  108791. --- linux-3.17.5/sound/soc/bcm/hifiberry_dacplus.c 1969-12-31 18:00:00.000000000 -0600
  108792. +++ linux-rpi/sound/soc/bcm/hifiberry_dacplus.c 2014-12-11 14:05:40.508418001 -0600
  108793. @@ -0,0 +1,141 @@
  108794. +/*
  108795. + * ASoC Driver for HiFiBerry DAC+
  108796. + *
  108797. + * Author: Daniel Matuschek
  108798. + * Copyright 2014
  108799. + * based on code by Florian Meier <florian.meier@koalo.de>
  108800. + *
  108801. + * This program is free software; you can redistribute it and/or
  108802. + * modify it under the terms of the GNU General Public License
  108803. + * version 2 as published by the Free Software Foundation.
  108804. + *
  108805. + * This program is distributed in the hope that it will be useful, but
  108806. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  108807. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  108808. + * General Public License for more details.
  108809. + */
  108810. +
  108811. +#include <linux/module.h>
  108812. +#include <linux/platform_device.h>
  108813. +
  108814. +#include <sound/core.h>
  108815. +#include <sound/pcm.h>
  108816. +#include <sound/pcm_params.h>
  108817. +#include <sound/soc.h>
  108818. +#include <sound/jack.h>
  108819. +
  108820. +#include "../codecs/pcm512x.h"
  108821. +
  108822. +static int snd_rpi_hifiberry_dacplus_init(struct snd_soc_pcm_runtime *rtd)
  108823. +{
  108824. + struct snd_soc_codec *codec = rtd->codec;
  108825. + snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
  108826. + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02);
  108827. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
  108828. + return 0;
  108829. +}
  108830. +
  108831. +static int snd_rpi_hifiberry_dacplus_hw_params(struct snd_pcm_substream *substream,
  108832. + struct snd_pcm_hw_params *params)
  108833. +{
  108834. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108835. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  108836. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
  108837. +}
  108838. +
  108839. +static int snd_rpi_hifiberry_dacplus_startup(struct snd_pcm_substream *substream) {
  108840. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108841. + struct snd_soc_codec *codec = rtd->codec;
  108842. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
  108843. + return 0;
  108844. +}
  108845. +
  108846. +static void snd_rpi_hifiberry_dacplus_shutdown(struct snd_pcm_substream *substream) {
  108847. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108848. + struct snd_soc_codec *codec = rtd->codec;
  108849. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x00);
  108850. +}
  108851. +
  108852. +/* machine stream operations */
  108853. +static struct snd_soc_ops snd_rpi_hifiberry_dacplus_ops = {
  108854. + .hw_params = snd_rpi_hifiberry_dacplus_hw_params,
  108855. + .startup = snd_rpi_hifiberry_dacplus_startup,
  108856. + .shutdown = snd_rpi_hifiberry_dacplus_shutdown,
  108857. +};
  108858. +
  108859. +static struct snd_soc_dai_link snd_rpi_hifiberry_dacplus_dai[] = {
  108860. +{
  108861. + .name = "HiFiBerry DAC+",
  108862. + .stream_name = "HiFiBerry DAC+ HiFi",
  108863. + .cpu_dai_name = "bcm2708-i2s.0",
  108864. + .codec_dai_name = "pcm512x-hifi",
  108865. + .platform_name = "bcm2708-i2s.0",
  108866. + .codec_name = "pcm512x.1-004d",
  108867. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  108868. + SND_SOC_DAIFMT_CBS_CFS,
  108869. + .ops = &snd_rpi_hifiberry_dacplus_ops,
  108870. + .init = snd_rpi_hifiberry_dacplus_init,
  108871. +},
  108872. +};
  108873. +
  108874. +/* audio machine driver */
  108875. +static struct snd_soc_card snd_rpi_hifiberry_dacplus = {
  108876. + .name = "snd_rpi_hifiberry_dacplus",
  108877. + .dai_link = snd_rpi_hifiberry_dacplus_dai,
  108878. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplus_dai),
  108879. +};
  108880. +
  108881. +static int snd_rpi_hifiberry_dacplus_probe(struct platform_device *pdev)
  108882. +{
  108883. + int ret = 0;
  108884. +
  108885. + snd_rpi_hifiberry_dacplus.dev = &pdev->dev;
  108886. +
  108887. + if (pdev->dev.of_node) {
  108888. + struct device_node *i2s_node;
  108889. + struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_dacplus_dai[0];
  108890. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  108891. + "i2s-controller", 0);
  108892. +
  108893. + if (i2s_node) {
  108894. + dai->cpu_dai_name = NULL;
  108895. + dai->cpu_of_node = i2s_node;
  108896. + dai->platform_name = NULL;
  108897. + dai->platform_of_node = i2s_node;
  108898. + }
  108899. + }
  108900. +
  108901. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dacplus);
  108902. + if (ret)
  108903. + dev_err(&pdev->dev,
  108904. + "snd_soc_register_card() failed: %d\n", ret);
  108905. +
  108906. + return ret;
  108907. +}
  108908. +
  108909. +static int snd_rpi_hifiberry_dacplus_remove(struct platform_device *pdev)
  108910. +{
  108911. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dacplus);
  108912. +}
  108913. +
  108914. +static const struct of_device_id snd_rpi_hifiberry_dacplus_of_match[] = {
  108915. + { .compatible = "hifiberry,hifiberry-dacplus", },
  108916. + {},
  108917. +};
  108918. +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dacplus_of_match);
  108919. +
  108920. +static struct platform_driver snd_rpi_hifiberry_dacplus_driver = {
  108921. + .driver = {
  108922. + .name = "snd-rpi-hifiberry-dacplus",
  108923. + .owner = THIS_MODULE,
  108924. + .of_match_table = snd_rpi_hifiberry_dacplus_of_match,
  108925. + },
  108926. + .probe = snd_rpi_hifiberry_dacplus_probe,
  108927. + .remove = snd_rpi_hifiberry_dacplus_remove,
  108928. +};
  108929. +
  108930. +module_platform_driver(snd_rpi_hifiberry_dacplus_driver);
  108931. +
  108932. +MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
  108933. +MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+");
  108934. +MODULE_LICENSE("GPL v2");
  108935. diff -Nur linux-3.17.5/sound/soc/bcm/hifiberry_digi.c linux-rpi/sound/soc/bcm/hifiberry_digi.c
  108936. --- linux-3.17.5/sound/soc/bcm/hifiberry_digi.c 1969-12-31 18:00:00.000000000 -0600
  108937. +++ linux-rpi/sound/soc/bcm/hifiberry_digi.c 2014-12-11 14:05:40.508418001 -0600
  108938. @@ -0,0 +1,175 @@
  108939. +/*
  108940. + * ASoC Driver for HifiBerry Digi
  108941. + *
  108942. + * Author: Daniel Matuschek <info@crazy-audio.com>
  108943. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  108944. + * Copyright 2013
  108945. + *
  108946. + * This program is free software; you can redistribute it and/or
  108947. + * modify it under the terms of the GNU General Public License
  108948. + * version 2 as published by the Free Software Foundation.
  108949. + *
  108950. + * This program is distributed in the hope that it will be useful, but
  108951. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  108952. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  108953. + * General Public License for more details.
  108954. + */
  108955. +
  108956. +#include <linux/module.h>
  108957. +#include <linux/platform_device.h>
  108958. +
  108959. +#include <sound/core.h>
  108960. +#include <sound/pcm.h>
  108961. +#include <sound/pcm_params.h>
  108962. +#include <sound/soc.h>
  108963. +#include <sound/jack.h>
  108964. +
  108965. +#include "../codecs/wm8804.h"
  108966. +
  108967. +static int samplerate=44100;
  108968. +
  108969. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  108970. +{
  108971. + struct snd_soc_codec *codec = rtd->codec;
  108972. +
  108973. + /* enable TX output */
  108974. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  108975. +
  108976. + return 0;
  108977. +}
  108978. +
  108979. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  108980. + struct snd_pcm_hw_params *params)
  108981. +{
  108982. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  108983. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  108984. + struct snd_soc_codec *codec = rtd->codec;
  108985. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  108986. +
  108987. + int sysclk = 27000000; /* This is fixed on this board */
  108988. +
  108989. + long mclk_freq=0;
  108990. + int mclk_div=1;
  108991. +
  108992. + int ret;
  108993. +
  108994. + samplerate = params_rate(params);
  108995. +
  108996. + switch (samplerate) {
  108997. + case 44100:
  108998. + case 48000:
  108999. + case 88200:
  109000. + case 96000:
  109001. + mclk_freq=samplerate*256;
  109002. + mclk_div=WM8804_MCLKDIV_256FS;
  109003. + break;
  109004. + case 176400:
  109005. + case 192000:
  109006. + mclk_freq=samplerate*128;
  109007. + mclk_div=WM8804_MCLKDIV_128FS;
  109008. + break;
  109009. + default:
  109010. + dev_err(substream->pcm->dev,
  109011. + "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
  109012. + }
  109013. +
  109014. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  109015. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  109016. +
  109017. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  109018. + sysclk, SND_SOC_CLOCK_OUT);
  109019. + if (ret < 0) {
  109020. + dev_err(substream->pcm->dev,
  109021. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  109022. + return ret;
  109023. + }
  109024. +
  109025. + /* Enable TX output */
  109026. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  109027. +
  109028. + /* Power on */
  109029. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  109030. +
  109031. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  109032. +}
  109033. +
  109034. +/* machine stream operations */
  109035. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  109036. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  109037. +};
  109038. +
  109039. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  109040. +{
  109041. + .name = "HifiBerry Digi",
  109042. + .stream_name = "HifiBerry Digi HiFi",
  109043. + .cpu_dai_name = "bcm2708-i2s.0",
  109044. + .codec_dai_name = "wm8804-spdif",
  109045. + .platform_name = "bcm2708-i2s.0",
  109046. + .codec_name = "wm8804.1-003b",
  109047. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109048. + SND_SOC_DAIFMT_CBM_CFM,
  109049. + .ops = &snd_rpi_hifiberry_digi_ops,
  109050. + .init = snd_rpi_hifiberry_digi_init,
  109051. +},
  109052. +};
  109053. +
  109054. +/* audio machine driver */
  109055. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  109056. + .name = "snd_rpi_hifiberry_digi",
  109057. + .dai_link = snd_rpi_hifiberry_digi_dai,
  109058. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  109059. +};
  109060. +
  109061. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  109062. +{
  109063. + int ret = 0;
  109064. +
  109065. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  109066. +
  109067. + if (pdev->dev.of_node) {
  109068. + struct device_node *i2s_node;
  109069. + struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_digi_dai[0];
  109070. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  109071. + "i2s-controller", 0);
  109072. +
  109073. + if (i2s_node) {
  109074. + dai->cpu_dai_name = NULL;
  109075. + dai->cpu_of_node = i2s_node;
  109076. + dai->platform_name = NULL;
  109077. + dai->platform_of_node = i2s_node;
  109078. + }
  109079. + }
  109080. +
  109081. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  109082. + if (ret)
  109083. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  109084. +
  109085. + return ret;
  109086. +}
  109087. +
  109088. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  109089. +{
  109090. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  109091. +}
  109092. +
  109093. +static const struct of_device_id snd_rpi_hifiberry_digi_of_match[] = {
  109094. + { .compatible = "hifiberry,hifiberry-digi", },
  109095. + {},
  109096. +};
  109097. +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_digi_of_match);
  109098. +
  109099. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  109100. + .driver = {
  109101. + .name = "snd-hifiberry-digi",
  109102. + .owner = THIS_MODULE,
  109103. + .of_match_table = snd_rpi_hifiberry_digi_of_match,
  109104. + },
  109105. + .probe = snd_rpi_hifiberry_digi_probe,
  109106. + .remove = snd_rpi_hifiberry_digi_remove,
  109107. +};
  109108. +
  109109. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  109110. +
  109111. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  109112. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  109113. +MODULE_LICENSE("GPL v2");
  109114. diff -Nur linux-3.17.5/sound/soc/bcm/iqaudio-dac.c linux-rpi/sound/soc/bcm/iqaudio-dac.c
  109115. --- linux-3.17.5/sound/soc/bcm/iqaudio-dac.c 1969-12-31 18:00:00.000000000 -0600
  109116. +++ linux-rpi/sound/soc/bcm/iqaudio-dac.c 2014-12-11 14:05:40.508418001 -0600
  109117. @@ -0,0 +1,127 @@
  109118. +/*
  109119. + * ASoC Driver for IQaudIO DAC
  109120. + *
  109121. + * Author: Florian Meier <florian.meier@koalo.de>
  109122. + * Copyright 2013
  109123. + *
  109124. + * This program is free software; you can redistribute it and/or
  109125. + * modify it under the terms of the GNU General Public License
  109126. + * version 2 as published by the Free Software Foundation.
  109127. + *
  109128. + * This program is distributed in the hope that it will be useful, but
  109129. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109130. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109131. + * General Public License for more details.
  109132. + */
  109133. +
  109134. +#include <linux/module.h>
  109135. +#include <linux/platform_device.h>
  109136. +
  109137. +#include <sound/core.h>
  109138. +#include <sound/pcm.h>
  109139. +#include <sound/pcm_params.h>
  109140. +#include <sound/soc.h>
  109141. +#include <sound/jack.h>
  109142. +
  109143. +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
  109144. +{
  109145. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  109146. +
  109147. + return 0;
  109148. +}
  109149. +
  109150. +static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
  109151. + struct snd_pcm_hw_params *params)
  109152. +{
  109153. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109154. +// NOT USED struct snd_soc_dai *codec_dai = rtd->codec_dai;
  109155. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  109156. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109157. +
  109158. + unsigned int sample_bits =
  109159. + snd_pcm_format_physical_width(params_format(params));
  109160. +
  109161. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  109162. +}
  109163. +
  109164. +/* machine stream operations */
  109165. +static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
  109166. + .hw_params = snd_rpi_iqaudio_dac_hw_params,
  109167. +};
  109168. +
  109169. +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
  109170. +{
  109171. + .name = "IQaudIO DAC",
  109172. + .stream_name = "IQaudIO DAC HiFi",
  109173. + .cpu_dai_name = "bcm2708-i2s.0",
  109174. + .codec_dai_name = "pcm512x-hifi",
  109175. + .platform_name = "bcm2708-i2s.0",
  109176. + .codec_name = "pcm512x.1-004c",
  109177. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109178. + SND_SOC_DAIFMT_CBS_CFS,
  109179. + .ops = &snd_rpi_iqaudio_dac_ops,
  109180. + .init = snd_rpi_iqaudio_dac_init,
  109181. +},
  109182. +};
  109183. +
  109184. +/* audio machine driver */
  109185. +static struct snd_soc_card snd_rpi_iqaudio_dac = {
  109186. + .name = "IQaudIODAC",
  109187. + .dai_link = snd_rpi_iqaudio_dac_dai,
  109188. + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
  109189. +};
  109190. +
  109191. +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
  109192. +{
  109193. + int ret = 0;
  109194. +
  109195. + snd_rpi_iqaudio_dac.dev = &pdev->dev;
  109196. +
  109197. + if (pdev->dev.of_node) {
  109198. + struct device_node *i2s_node;
  109199. + struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_dac_dai[0];
  109200. + i2s_node = of_parse_phandle(pdev->dev.of_node,
  109201. + "i2s-controller", 0);
  109202. +
  109203. + if (i2s_node) {
  109204. + dai->cpu_dai_name = NULL;
  109205. + dai->cpu_of_node = i2s_node;
  109206. + dai->platform_name = NULL;
  109207. + dai->platform_of_node = i2s_node;
  109208. + }
  109209. + }
  109210. +
  109211. + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
  109212. + if (ret)
  109213. + dev_err(&pdev->dev,
  109214. + "snd_soc_register_card() failed: %d\n", ret);
  109215. +
  109216. + return ret;
  109217. +}
  109218. +
  109219. +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
  109220. +{
  109221. + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
  109222. +}
  109223. +
  109224. +static const struct of_device_id iqaudio_of_match[] = {
  109225. + { .compatible = "iqaudio,iqaudio-dac", },
  109226. + {},
  109227. +};
  109228. +MODULE_DEVICE_TABLE(of, iqaudio_of_match);
  109229. +
  109230. +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
  109231. + .driver = {
  109232. + .name = "snd-rpi-iqaudio-dac",
  109233. + .owner = THIS_MODULE,
  109234. + .of_match_table = iqaudio_of_match,
  109235. + },
  109236. + .probe = snd_rpi_iqaudio_dac_probe,
  109237. + .remove = snd_rpi_iqaudio_dac_remove,
  109238. +};
  109239. +
  109240. +module_platform_driver(snd_rpi_iqaudio_dac_driver);
  109241. +
  109242. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109243. +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
  109244. +MODULE_LICENSE("GPL v2");
  109245. diff -Nur linux-3.17.5/sound/soc/bcm/Kconfig linux-rpi/sound/soc/bcm/Kconfig
  109246. --- linux-3.17.5/sound/soc/bcm/Kconfig 2014-12-06 17:57:59.000000000 -0600
  109247. +++ linux-rpi/sound/soc/bcm/Kconfig 2014-12-11 14:05:40.508418001 -0600
  109248. @@ -7,3 +7,56 @@
  109249. Say Y or M if you want to add support for codecs attached to
  109250. the BCM2835 I2S interface. You will also need
  109251. to select the audio interfaces to support below.
  109252. +
  109253. +config SND_BCM2708_SOC_I2S
  109254. + tristate "SoC Audio support for the Broadcom BCM2708 I2S module"
  109255. + depends on MACH_BCM2708
  109256. + select REGMAP_MMIO
  109257. + select SND_SOC_DMAENGINE_PCM
  109258. + select SND_SOC_GENERIC_DMAENGINE_PCM
  109259. + help
  109260. + Say Y or M if you want to add support for codecs attached to
  109261. + the BCM2708 I2S interface. You will also need
  109262. + to select the audio interfaces to support below.
  109263. +
  109264. +config SND_BCM2708_SOC_HIFIBERRY_DAC
  109265. + tristate "Support for HifiBerry DAC"
  109266. + depends on SND_BCM2708_SOC_I2S
  109267. + select SND_SOC_PCM5102A
  109268. + help
  109269. + Say Y or M if you want to add support for HifiBerry DAC.
  109270. +
  109271. +config SND_BCM2708_SOC_HIFIBERRY_DACPLUS
  109272. + tristate "Support for HifiBerry DAC+"
  109273. + depends on SND_BCM2708_SOC_I2S
  109274. + select SND_SOC_PCM512x
  109275. + help
  109276. + Say Y or M if you want to add support for HifiBerry DAC+.
  109277. +
  109278. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  109279. + tristate "Support for HifiBerry Digi"
  109280. + depends on SND_BCM2708_SOC_I2S
  109281. + select SND_SOC_WM8804
  109282. + help
  109283. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  109284. +
  109285. +config SND_BCM2708_SOC_HIFIBERRY_AMP
  109286. + tristate "Support for the HifiBerry Amp"
  109287. + depends on SND_BCM2708_SOC_I2S
  109288. + select SND_SOC_TAS5713
  109289. + help
  109290. + Say Y or M if you want to add support for the HifiBerry Amp amplifier board.
  109291. +
  109292. +config SND_BCM2708_SOC_RPI_DAC
  109293. + tristate "Support for RPi-DAC"
  109294. + depends on SND_BCM2708_SOC_I2S
  109295. + select SND_SOC_PCM1794A
  109296. + help
  109297. + Say Y or M if you want to add support for RPi-DAC.
  109298. +
  109299. +config SND_BCM2708_SOC_IQAUDIO_DAC
  109300. + tristate "Support for IQaudIO-DAC"
  109301. + depends on SND_BCM2708_SOC_I2S
  109302. + select SND_SOC_PCM512x_I2C
  109303. + help
  109304. + Say Y or M if you want to add support for IQaudIO-DAC.
  109305. diff -Nur linux-3.17.5/sound/soc/bcm/Makefile linux-rpi/sound/soc/bcm/Makefile
  109306. --- linux-3.17.5/sound/soc/bcm/Makefile 2014-12-06 17:57:59.000000000 -0600
  109307. +++ linux-rpi/sound/soc/bcm/Makefile 2014-12-11 14:05:40.508418001 -0600
  109308. @@ -3,3 +3,22 @@
  109309. obj-$(CONFIG_SND_BCM2835_SOC_I2S) += snd-soc-bcm2835-i2s.o
  109310. +# BCM2708 Platform Support
  109311. +snd-soc-bcm2708-i2s-objs := bcm2708-i2s.o
  109312. +
  109313. +obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
  109314. +
  109315. +# BCM2708 Machine Support
  109316. +snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  109317. +snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
  109318. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  109319. +snd-soc-hifiberry-amp-objs := hifiberry_amp.o
  109320. +snd-soc-rpi-dac-objs := rpi-dac.o
  109321. +snd-soc-iqaudio-dac-objs := iqaudio-dac.o
  109322. +
  109323. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  109324. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
  109325. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  109326. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) += snd-soc-hifiberry-amp.o
  109327. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  109328. +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
  109329. diff -Nur linux-3.17.5/sound/soc/bcm/rpi-dac.c linux-rpi/sound/soc/bcm/rpi-dac.c
  109330. --- linux-3.17.5/sound/soc/bcm/rpi-dac.c 1969-12-31 18:00:00.000000000 -0600
  109331. +++ linux-rpi/sound/soc/bcm/rpi-dac.c 2014-12-11 14:02:58.108418001 -0600
  109332. @@ -0,0 +1,97 @@
  109333. +/*
  109334. + * ASoC Driver for RPi-DAC.
  109335. + *
  109336. + * Author: Florian Meier <florian.meier@koalo.de>
  109337. + * Copyright 2013
  109338. + *
  109339. + * This program is free software; you can redistribute it and/or
  109340. + * modify it under the terms of the GNU General Public License
  109341. + * version 2 as published by the Free Software Foundation.
  109342. + *
  109343. + * This program is distributed in the hope that it will be useful, but
  109344. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109345. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109346. + * General Public License for more details.
  109347. + */
  109348. +
  109349. +#include <linux/module.h>
  109350. +#include <linux/platform_device.h>
  109351. +
  109352. +#include <sound/core.h>
  109353. +#include <sound/pcm.h>
  109354. +#include <sound/pcm_params.h>
  109355. +#include <sound/soc.h>
  109356. +#include <sound/jack.h>
  109357. +
  109358. +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd)
  109359. +{
  109360. + return 0;
  109361. +}
  109362. +
  109363. +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream,
  109364. + struct snd_pcm_hw_params *params)
  109365. +{
  109366. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109367. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109368. +
  109369. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 32*2);
  109370. +}
  109371. +
  109372. +/* machine stream operations */
  109373. +static struct snd_soc_ops snd_rpi_rpi_dac_ops = {
  109374. + .hw_params = snd_rpi_rpi_dac_hw_params,
  109375. +};
  109376. +
  109377. +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = {
  109378. +{
  109379. + .name = "RPi-DAC",
  109380. + .stream_name = "RPi-DAC HiFi",
  109381. + .cpu_dai_name = "bcm2708-i2s.0",
  109382. + .codec_dai_name = "pcm1794a-hifi",
  109383. + .platform_name = "bcm2708-i2s.0",
  109384. + .codec_name = "pcm1794a-codec",
  109385. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109386. + SND_SOC_DAIFMT_CBS_CFS,
  109387. + .ops = &snd_rpi_rpi_dac_ops,
  109388. + .init = snd_rpi_rpi_dac_init,
  109389. +},
  109390. +};
  109391. +
  109392. +/* audio machine driver */
  109393. +static struct snd_soc_card snd_rpi_rpi_dac = {
  109394. + .name = "snd_rpi_rpi_dac",
  109395. + .dai_link = snd_rpi_rpi_dac_dai,
  109396. + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai),
  109397. +};
  109398. +
  109399. +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev)
  109400. +{
  109401. + int ret = 0;
  109402. +
  109403. + snd_rpi_rpi_dac.dev = &pdev->dev;
  109404. + ret = snd_soc_register_card(&snd_rpi_rpi_dac);
  109405. + if (ret)
  109406. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  109407. +
  109408. + return ret;
  109409. +}
  109410. +
  109411. +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev)
  109412. +{
  109413. + return snd_soc_unregister_card(&snd_rpi_rpi_dac);
  109414. +}
  109415. +
  109416. +static struct platform_driver snd_rpi_rpi_dac_driver = {
  109417. + .driver = {
  109418. + .name = "snd-rpi-dac",
  109419. + .owner = THIS_MODULE,
  109420. + },
  109421. + .probe = snd_rpi_rpi_dac_probe,
  109422. + .remove = snd_rpi_rpi_dac_remove,
  109423. +};
  109424. +
  109425. +module_platform_driver(snd_rpi_rpi_dac_driver);
  109426. +
  109427. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109428. +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC");
  109429. +MODULE_LICENSE("GPL v2");
  109430. diff -Nur linux-3.17.5/sound/soc/codecs/Kconfig linux-rpi/sound/soc/codecs/Kconfig
  109431. --- linux-3.17.5/sound/soc/codecs/Kconfig 2014-12-06 17:57:59.000000000 -0600
  109432. +++ linux-rpi/sound/soc/codecs/Kconfig 2014-12-11 14:05:40.512418001 -0600
  109433. @@ -76,6 +76,8 @@
  109434. select SND_SOC_PCM512x_I2C if I2C
  109435. select SND_SOC_PCM512x_SPI if SPI_MASTER
  109436. select SND_SOC_RT286 if I2C
  109437. + select SND_SOC_PCM5102A if I2C
  109438. + select SND_SOC_PCM1794A if I2C
  109439. select SND_SOC_RT5631 if I2C
  109440. select SND_SOC_RT5640 if I2C
  109441. select SND_SOC_RT5645 if I2C
  109442. @@ -98,6 +100,7 @@
  109443. select SND_SOC_TAS5086 if I2C
  109444. select SND_SOC_TLV320AIC23_I2C if I2C
  109445. select SND_SOC_TLV320AIC23_SPI if SPI_MASTER
  109446. + select SND_SOC_TAS5713 if I2C
  109447. select SND_SOC_TLV320AIC26 if SPI_MASTER
  109448. select SND_SOC_TLV320AIC31XX if I2C
  109449. select SND_SOC_TLV320AIC32X4 if I2C
  109450. @@ -465,6 +468,12 @@
  109451. config SND_SOC_RT286
  109452. tristate
  109453. +config SND_SOC_PCM1794A
  109454. + tristate
  109455. +
  109456. +config SND_SOC_PCM5102A
  109457. + tristate
  109458. +
  109459. config SND_SOC_RT5631
  109460. tristate
  109461. @@ -548,6 +557,9 @@
  109462. tristate "Texas Instruments TAS5086 speaker amplifier"
  109463. depends on I2C
  109464. +config SND_SOC_TAS5713
  109465. + tristate
  109466. +
  109467. config SND_SOC_TLV320AIC23
  109468. tristate
  109469. diff -Nur linux-3.17.5/sound/soc/codecs/Makefile linux-rpi/sound/soc/codecs/Makefile
  109470. --- linux-3.17.5/sound/soc/codecs/Makefile 2014-12-06 17:57:59.000000000 -0600
  109471. +++ linux-rpi/sound/soc/codecs/Makefile 2014-12-11 14:05:40.512418001 -0600
  109472. @@ -70,6 +70,8 @@
  109473. snd-soc-pcm512x-spi-objs := pcm512x-spi.o
  109474. snd-soc-rl6231-objs := rl6231.o
  109475. snd-soc-rt286-objs := rt286.o
  109476. +snd-soc-pcm1794a-objs := pcm1794a.o
  109477. +snd-soc-pcm5102a-objs := pcm5102a.o
  109478. snd-soc-rt5631-objs := rt5631.o
  109479. snd-soc-rt5640-objs := rt5640.o
  109480. snd-soc-rt5645-objs := rt5645.o
  109481. @@ -96,6 +98,7 @@
  109482. snd-soc-sta529-objs := sta529.o
  109483. snd-soc-stac9766-objs := stac9766.o
  109484. snd-soc-tas5086-objs := tas5086.o
  109485. +snd-soc-tas5713-objs := tas5713.o
  109486. snd-soc-tlv320aic23-objs := tlv320aic23.o
  109487. snd-soc-tlv320aic23-i2c-objs := tlv320aic23-i2c.o
  109488. snd-soc-tlv320aic23-spi-objs := tlv320aic23-spi.o
  109489. @@ -241,6 +244,8 @@
  109490. obj-$(CONFIG_SND_SOC_PCM512x_SPI) += snd-soc-pcm512x-spi.o
  109491. obj-$(CONFIG_SND_SOC_RL6231) += snd-soc-rl6231.o
  109492. obj-$(CONFIG_SND_SOC_RT286) += snd-soc-rt286.o
  109493. +obj-$(CONFIG_SND_SOC_PCM1794A) += snd-soc-pcm1794a.o
  109494. +obj-$(CONFIG_SND_SOC_PCM5102A) += snd-soc-pcm5102a.o
  109495. obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o
  109496. obj-$(CONFIG_SND_SOC_RT5640) += snd-soc-rt5640.o
  109497. obj-$(CONFIG_SND_SOC_RT5645) += snd-soc-rt5645.o
  109498. @@ -264,6 +269,7 @@
  109499. obj-$(CONFIG_SND_SOC_STAC9766) += snd-soc-stac9766.o
  109500. obj-$(CONFIG_SND_SOC_TAS2552) += snd-soc-tas2552.o
  109501. obj-$(CONFIG_SND_SOC_TAS5086) += snd-soc-tas5086.o
  109502. +obj-$(CONFIG_SND_SOC_TAS5713) += snd-soc-tas5713.o
  109503. obj-$(CONFIG_SND_SOC_TLV320AIC23) += snd-soc-tlv320aic23.o
  109504. obj-$(CONFIG_SND_SOC_TLV320AIC23_I2C) += snd-soc-tlv320aic23-i2c.o
  109505. obj-$(CONFIG_SND_SOC_TLV320AIC23_SPI) += snd-soc-tlv320aic23-spi.o
  109506. diff -Nur linux-3.17.5/sound/soc/codecs/pcm1794a.c linux-rpi/sound/soc/codecs/pcm1794a.c
  109507. --- linux-3.17.5/sound/soc/codecs/pcm1794a.c 1969-12-31 18:00:00.000000000 -0600
  109508. +++ linux-rpi/sound/soc/codecs/pcm1794a.c 2014-12-11 14:02:58.120418001 -0600
  109509. @@ -0,0 +1,62 @@
  109510. +/*
  109511. + * Driver for the PCM1794A codec
  109512. + *
  109513. + * Author: Florian Meier <florian.meier@koalo.de>
  109514. + * Copyright 2013
  109515. + *
  109516. + * This program is free software; you can redistribute it and/or
  109517. + * modify it under the terms of the GNU General Public License
  109518. + * version 2 as published by the Free Software Foundation.
  109519. + *
  109520. + * This program is distributed in the hope that it will be useful, but
  109521. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109522. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109523. + * General Public License for more details.
  109524. + */
  109525. +
  109526. +
  109527. +#include <linux/init.h>
  109528. +#include <linux/module.h>
  109529. +#include <linux/platform_device.h>
  109530. +
  109531. +#include <sound/soc.h>
  109532. +
  109533. +static struct snd_soc_dai_driver pcm1794a_dai = {
  109534. + .name = "pcm1794a-hifi",
  109535. + .playback = {
  109536. + .channels_min = 2,
  109537. + .channels_max = 2,
  109538. + .rates = SNDRV_PCM_RATE_8000_192000,
  109539. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  109540. + SNDRV_PCM_FMTBIT_S24_LE
  109541. + },
  109542. +};
  109543. +
  109544. +static struct snd_soc_codec_driver soc_codec_dev_pcm1794a;
  109545. +
  109546. +static int pcm1794a_probe(struct platform_device *pdev)
  109547. +{
  109548. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm1794a,
  109549. + &pcm1794a_dai, 1);
  109550. +}
  109551. +
  109552. +static int pcm1794a_remove(struct platform_device *pdev)
  109553. +{
  109554. + snd_soc_unregister_codec(&pdev->dev);
  109555. + return 0;
  109556. +}
  109557. +
  109558. +static struct platform_driver pcm1794a_codec_driver = {
  109559. + .probe = pcm1794a_probe,
  109560. + .remove = pcm1794a_remove,
  109561. + .driver = {
  109562. + .name = "pcm1794a-codec",
  109563. + .owner = THIS_MODULE,
  109564. + },
  109565. +};
  109566. +
  109567. +module_platform_driver(pcm1794a_codec_driver);
  109568. +
  109569. +MODULE_DESCRIPTION("ASoC PCM1794A codec driver");
  109570. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109571. +MODULE_LICENSE("GPL v2");
  109572. diff -Nur linux-3.17.5/sound/soc/codecs/pcm5102a.c linux-rpi/sound/soc/codecs/pcm5102a.c
  109573. --- linux-3.17.5/sound/soc/codecs/pcm5102a.c 1969-12-31 18:00:00.000000000 -0600
  109574. +++ linux-rpi/sound/soc/codecs/pcm5102a.c 2014-12-11 14:05:40.524418001 -0600
  109575. @@ -0,0 +1,70 @@
  109576. +/*
  109577. + * Driver for the PCM5102A codec
  109578. + *
  109579. + * Author: Florian Meier <florian.meier@koalo.de>
  109580. + * Copyright 2013
  109581. + *
  109582. + * This program is free software; you can redistribute it and/or
  109583. + * modify it under the terms of the GNU General Public License
  109584. + * version 2 as published by the Free Software Foundation.
  109585. + *
  109586. + * This program is distributed in the hope that it will be useful, but
  109587. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109588. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109589. + * General Public License for more details.
  109590. + */
  109591. +
  109592. +
  109593. +#include <linux/init.h>
  109594. +#include <linux/module.h>
  109595. +#include <linux/platform_device.h>
  109596. +
  109597. +#include <sound/soc.h>
  109598. +
  109599. +static struct snd_soc_dai_driver pcm5102a_dai = {
  109600. + .name = "pcm5102a-hifi",
  109601. + .playback = {
  109602. + .channels_min = 2,
  109603. + .channels_max = 2,
  109604. + .rates = SNDRV_PCM_RATE_8000_192000,
  109605. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  109606. + SNDRV_PCM_FMTBIT_S24_LE |
  109607. + SNDRV_PCM_FMTBIT_S32_LE
  109608. + },
  109609. +};
  109610. +
  109611. +static struct snd_soc_codec_driver soc_codec_dev_pcm5102a;
  109612. +
  109613. +static int pcm5102a_probe(struct platform_device *pdev)
  109614. +{
  109615. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm5102a,
  109616. + &pcm5102a_dai, 1);
  109617. +}
  109618. +
  109619. +static int pcm5102a_remove(struct platform_device *pdev)
  109620. +{
  109621. + snd_soc_unregister_codec(&pdev->dev);
  109622. + return 0;
  109623. +}
  109624. +
  109625. +static const struct of_device_id pcm5102a_of_match[] = {
  109626. + { .compatible = "ti,pcm5102a", },
  109627. + { }
  109628. +};
  109629. +MODULE_DEVICE_TABLE(of, pcm5102a_of_match);
  109630. +
  109631. +static struct platform_driver pcm5102a_codec_driver = {
  109632. + .probe = pcm5102a_probe,
  109633. + .remove = pcm5102a_remove,
  109634. + .driver = {
  109635. + .name = "pcm5102a-codec",
  109636. + .owner = THIS_MODULE,
  109637. + .of_match_table = pcm5102a_of_match,
  109638. + },
  109639. +};
  109640. +
  109641. +module_platform_driver(pcm5102a_codec_driver);
  109642. +
  109643. +MODULE_DESCRIPTION("ASoC PCM5102A codec driver");
  109644. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109645. +MODULE_LICENSE("GPL v2");
  109646. diff -Nur linux-3.17.5/sound/soc/codecs/pcm512x.c linux-rpi/sound/soc/codecs/pcm512x.c
  109647. --- linux-3.17.5/sound/soc/codecs/pcm512x.c 2014-12-06 17:57:59.000000000 -0600
  109648. +++ linux-rpi/sound/soc/codecs/pcm512x.c 2014-12-11 14:05:40.524418001 -0600
  109649. @@ -259,8 +259,8 @@
  109650. pcm512x_ramp_step_text);
  109651. static const struct snd_kcontrol_new pcm512x_controls[] = {
  109652. -SOC_DOUBLE_R_TLV("Digital Playback Volume", PCM512x_DIGITAL_VOLUME_2,
  109653. - PCM512x_DIGITAL_VOLUME_3, 0, 255, 1, digital_tlv),
  109654. +SOC_DOUBLE_R_RANGE_TLV("PCM", PCM512x_DIGITAL_VOLUME_2,
  109655. + PCM512x_DIGITAL_VOLUME_3, 0, 40, 255, 1, digital_tlv),
  109656. SOC_DOUBLE_TLV("Playback Volume", PCM512x_ANALOG_GAIN_CTRL,
  109657. PCM512x_LAGN_SHIFT, PCM512x_RAGN_SHIFT, 1, 1, analog_tlv),
  109658. SOC_DOUBLE_TLV("Playback Boost Volume", PCM512x_ANALOG_GAIN_BOOST,
  109659. diff -Nur linux-3.17.5/sound/soc/codecs/tas5713.c linux-rpi/sound/soc/codecs/tas5713.c
  109660. --- linux-3.17.5/sound/soc/codecs/tas5713.c 1969-12-31 18:00:00.000000000 -0600
  109661. +++ linux-rpi/sound/soc/codecs/tas5713.c 2014-12-11 14:05:40.532418001 -0600
  109662. @@ -0,0 +1,362 @@
  109663. +/*
  109664. + * ASoC Driver for TAS5713
  109665. + *
  109666. + * Author: Sebastian Eickhoff <basti.eickhoff@googlemail.com>
  109667. + * Copyright 2014
  109668. + *
  109669. + * This program is free software; you can redistribute it and/or
  109670. + * modify it under the terms of the GNU General Public License
  109671. + * version 2 as published by the Free Software Foundation.
  109672. + *
  109673. + * This program is distributed in the hope that it will be useful, but
  109674. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109675. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109676. + * General Public License for more details.
  109677. + */
  109678. +
  109679. +#include <linux/module.h>
  109680. +#include <linux/moduleparam.h>
  109681. +#include <linux/init.h>
  109682. +#include <linux/delay.h>
  109683. +#include <linux/pm.h>
  109684. +#include <linux/i2c.h>
  109685. +#include <linux/of_device.h>
  109686. +#include <linux/spi/spi.h>
  109687. +#include <linux/regmap.h>
  109688. +#include <linux/regulator/consumer.h>
  109689. +#include <linux/slab.h>
  109690. +#include <sound/core.h>
  109691. +#include <sound/pcm.h>
  109692. +#include <sound/pcm_params.h>
  109693. +#include <sound/soc.h>
  109694. +#include <sound/initval.h>
  109695. +#include <sound/tlv.h>
  109696. +
  109697. +#include <linux/kernel.h>
  109698. +#include <linux/string.h>
  109699. +#include <linux/fs.h>
  109700. +#include <asm/uaccess.h>
  109701. +
  109702. +#include "tas5713.h"
  109703. +
  109704. +
  109705. +static struct i2c_client *i2c;
  109706. +
  109707. +struct tas5713_priv {
  109708. + struct regmap *regmap;
  109709. + int mclk_div;
  109710. + struct snd_soc_codec *codec;
  109711. +};
  109712. +
  109713. +static struct tas5713_priv *priv_data;
  109714. +
  109715. +
  109716. +
  109717. +
  109718. +/*
  109719. + * _ _ ___ _ ___ _ _
  109720. + * /_\ | | / __| /_\ / __|___ _ _| |_ _ _ ___| |___
  109721. + * / _ \| |__\__ \/ _ \ | (__/ _ \ ' \ _| '_/ _ \ (_-<
  109722. + * /_/ \_\____|___/_/ \_\ \___\___/_||_\__|_| \___/_/__/
  109723. + *
  109724. + */
  109725. +
  109726. +static const DECLARE_TLV_DB_SCALE(tas5713_vol_tlv, -10000, 50, 1);
  109727. +
  109728. +
  109729. +static const struct snd_kcontrol_new tas5713_snd_controls[] = {
  109730. + SOC_SINGLE_TLV ("Master" , TAS5713_VOL_MASTER, 0, 248, 1, tas5713_vol_tlv),
  109731. + SOC_DOUBLE_R_TLV("Channels" , TAS5713_VOL_CH1, TAS5713_VOL_CH2, 0, 248, 1, tas5713_vol_tlv)
  109732. +};
  109733. +
  109734. +
  109735. +
  109736. +
  109737. +/*
  109738. + * __ __ _ _ ___ _
  109739. + * | \/ |__ _ __| |_ (_)_ _ ___ | \ _ _(_)_ _____ _ _
  109740. + * | |\/| / _` / _| ' \| | ' \/ -_) | |) | '_| \ V / -_) '_|
  109741. + * |_| |_\__,_\__|_||_|_|_||_\___| |___/|_| |_|\_/\___|_|
  109742. + *
  109743. + */
  109744. +
  109745. +static int tas5713_hw_params(struct snd_pcm_substream *substream,
  109746. + struct snd_pcm_hw_params *params,
  109747. + struct snd_soc_dai *dai)
  109748. +{
  109749. + u16 blen = 0x00;
  109750. +
  109751. + struct snd_soc_codec *codec;
  109752. + codec = dai->codec;
  109753. + priv_data->codec = dai->codec;
  109754. +
  109755. + switch (params_format(params)) {
  109756. + case SNDRV_PCM_FORMAT_S16_LE:
  109757. + blen = 0x03;
  109758. + break;
  109759. + case SNDRV_PCM_FORMAT_S20_3LE:
  109760. + blen = 0x1;
  109761. + break;
  109762. + case SNDRV_PCM_FORMAT_S24_LE:
  109763. + blen = 0x04;
  109764. + break;
  109765. + case SNDRV_PCM_FORMAT_S32_LE:
  109766. + blen = 0x05;
  109767. + break;
  109768. + default:
  109769. + dev_err(dai->dev, "Unsupported word length: %u\n",
  109770. + params_format(params));
  109771. + return -EINVAL;
  109772. + }
  109773. +
  109774. + // set word length
  109775. + snd_soc_update_bits(codec, TAS5713_SERIAL_DATA_INTERFACE, 0x7, blen);
  109776. +
  109777. + return 0;
  109778. +}
  109779. +
  109780. +
  109781. +static int tas5713_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
  109782. +{
  109783. + unsigned int val = 0;
  109784. +
  109785. + struct tas5713_priv *tas5713;
  109786. + struct snd_soc_codec *codec = dai->codec;
  109787. + tas5713 = snd_soc_codec_get_drvdata(codec);
  109788. +
  109789. + if (mute) {
  109790. + val = TAS5713_SOFT_MUTE_ALL;
  109791. + }
  109792. +
  109793. + return regmap_write(tas5713->regmap, TAS5713_SOFT_MUTE, val);
  109794. +}
  109795. +
  109796. +
  109797. +static const struct snd_soc_dai_ops tas5713_dai_ops = {
  109798. + .hw_params = tas5713_hw_params,
  109799. + .mute_stream = tas5713_mute_stream,
  109800. +};
  109801. +
  109802. +
  109803. +static struct snd_soc_dai_driver tas5713_dai = {
  109804. + .name = "tas5713-hifi",
  109805. + .playback = {
  109806. + .stream_name = "Playback",
  109807. + .channels_min = 2,
  109808. + .channels_max = 2,
  109809. + .rates = SNDRV_PCM_RATE_8000_48000,
  109810. + .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE ),
  109811. + },
  109812. + .ops = &tas5713_dai_ops,
  109813. +};
  109814. +
  109815. +
  109816. +
  109817. +
  109818. +/*
  109819. + * ___ _ ___ _
  109820. + * / __|___ __| |___ __ | \ _ _(_)_ _____ _ _
  109821. + * | (__/ _ \/ _` / -_) _| | |) | '_| \ V / -_) '_|
  109822. + * \___\___/\__,_\___\__| |___/|_| |_|\_/\___|_|
  109823. + *
  109824. + */
  109825. +
  109826. +static int tas5713_remove(struct snd_soc_codec *codec)
  109827. +{
  109828. + struct tas5713_priv *tas5713;
  109829. +
  109830. + tas5713 = snd_soc_codec_get_drvdata(codec);
  109831. +
  109832. + return 0;
  109833. +}
  109834. +
  109835. +
  109836. +static int tas5713_probe(struct snd_soc_codec *codec)
  109837. +{
  109838. + struct tas5713_priv *tas5713;
  109839. + int i, ret;
  109840. +
  109841. + i2c = container_of(codec->dev, struct i2c_client, dev);
  109842. +
  109843. + tas5713 = snd_soc_codec_get_drvdata(codec);
  109844. +
  109845. + // Reset error
  109846. + ret = snd_soc_write(codec, TAS5713_ERROR_STATUS, 0x00);
  109847. +
  109848. + // Trim oscillator
  109849. + ret = snd_soc_write(codec, TAS5713_OSC_TRIM, 0x00);
  109850. + msleep(1000);
  109851. +
  109852. + // Reset error
  109853. + ret = snd_soc_write(codec, TAS5713_ERROR_STATUS, 0x00);
  109854. +
  109855. + // Clock mode: 44/48kHz, MCLK=64xfs
  109856. + ret = snd_soc_write(codec, TAS5713_CLOCK_CTRL, 0x60);
  109857. +
  109858. + // I2S 24bit
  109859. + ret = snd_soc_write(codec, TAS5713_SERIAL_DATA_INTERFACE, 0x05);
  109860. +
  109861. + // Unmute
  109862. + ret = snd_soc_write(codec, TAS5713_SYSTEM_CTRL2, 0x00);
  109863. + ret = snd_soc_write(codec, TAS5713_SOFT_MUTE, 0x00);
  109864. +
  109865. + // Set volume to 0db
  109866. + ret = snd_soc_write(codec, TAS5713_VOL_MASTER, 0x00);
  109867. +
  109868. + // Now start programming the default initialization sequence
  109869. + for (i = 0; i < ARRAY_SIZE(tas5713_init_sequence); ++i) {
  109870. + ret = i2c_master_send(i2c,
  109871. + tas5713_init_sequence[i].data,
  109872. + tas5713_init_sequence[i].size);
  109873. +
  109874. + if (ret < 0) {
  109875. + printk(KERN_INFO "TAS5713 CODEC PROBE: InitSeq returns: %d\n", ret);
  109876. + }
  109877. + }
  109878. +
  109879. + // Unmute
  109880. + ret = snd_soc_write(codec, TAS5713_SYSTEM_CTRL2, 0x00);
  109881. +
  109882. +
  109883. + return 0;
  109884. +}
  109885. +
  109886. +
  109887. +static struct snd_soc_codec_driver soc_codec_dev_tas5713 = {
  109888. + .probe = tas5713_probe,
  109889. + .remove = tas5713_remove,
  109890. + .controls = tas5713_snd_controls,
  109891. + .num_controls = ARRAY_SIZE(tas5713_snd_controls),
  109892. +};
  109893. +
  109894. +
  109895. +
  109896. +
  109897. +/*
  109898. + * ___ ___ ___ ___ _
  109899. + * |_ _|_ ) __| | \ _ _(_)_ _____ _ _
  109900. + * | | / / (__ | |) | '_| \ V / -_) '_|
  109901. + * |___/___\___| |___/|_| |_|\_/\___|_|
  109902. + *
  109903. + */
  109904. +
  109905. +static const struct reg_default tas5713_reg_defaults[] = {
  109906. + { 0x07 ,0x80 }, // R7 - VOL_MASTER - -40dB
  109907. + { 0x08 , 30 }, // R8 - VOL_CH1 - 0dB
  109908. + { 0x09 , 30 }, // R9 - VOL_CH2 - 0dB
  109909. + { 0x0A ,0x80 }, // R10 - VOL_HEADPHONE - -40dB
  109910. +};
  109911. +
  109912. +
  109913. +static bool tas5713_reg_volatile(struct device *dev, unsigned int reg)
  109914. +{
  109915. + switch (reg) {
  109916. + case TAS5713_DEVICE_ID:
  109917. + case TAS5713_ERROR_STATUS:
  109918. + return true;
  109919. + default:
  109920. + return false;
  109921. + }
  109922. +}
  109923. +
  109924. +
  109925. +static const struct of_device_id tas5713_of_match[] = {
  109926. + { .compatible = "ti,tas5713", },
  109927. + { }
  109928. +};
  109929. +MODULE_DEVICE_TABLE(of, tas5713_of_match);
  109930. +
  109931. +
  109932. +static struct regmap_config tas5713_regmap_config = {
  109933. + .reg_bits = 8,
  109934. + .val_bits = 8,
  109935. +
  109936. + .max_register = TAS5713_MAX_REGISTER,
  109937. + .volatile_reg = tas5713_reg_volatile,
  109938. +
  109939. + .cache_type = REGCACHE_RBTREE,
  109940. + .reg_defaults = tas5713_reg_defaults,
  109941. + .num_reg_defaults = ARRAY_SIZE(tas5713_reg_defaults),
  109942. +};
  109943. +
  109944. +
  109945. +static int tas5713_i2c_probe(struct i2c_client *i2c,
  109946. + const struct i2c_device_id *id)
  109947. +{
  109948. + int ret;
  109949. +
  109950. + priv_data = devm_kzalloc(&i2c->dev, sizeof *priv_data, GFP_KERNEL);
  109951. + if (!priv_data)
  109952. + return -ENOMEM;
  109953. +
  109954. + priv_data->regmap = devm_regmap_init_i2c(i2c, &tas5713_regmap_config);
  109955. + if (IS_ERR(priv_data->regmap)) {
  109956. + ret = PTR_ERR(priv_data->regmap);
  109957. + return ret;
  109958. + }
  109959. +
  109960. + i2c_set_clientdata(i2c, priv_data);
  109961. +
  109962. + ret = snd_soc_register_codec(&i2c->dev,
  109963. + &soc_codec_dev_tas5713, &tas5713_dai, 1);
  109964. +
  109965. + return ret;
  109966. +}
  109967. +
  109968. +
  109969. +static int tas5713_i2c_remove(struct i2c_client *i2c)
  109970. +{
  109971. + snd_soc_unregister_codec(&i2c->dev);
  109972. + i2c_set_clientdata(i2c, NULL);
  109973. +
  109974. + kfree(priv_data);
  109975. +
  109976. + return 0;
  109977. +}
  109978. +
  109979. +
  109980. +static const struct i2c_device_id tas5713_i2c_id[] = {
  109981. + { "tas5713", 0 },
  109982. + { }
  109983. +};
  109984. +
  109985. +MODULE_DEVICE_TABLE(i2c, tas5713_i2c_id);
  109986. +
  109987. +
  109988. +static struct i2c_driver tas5713_i2c_driver = {
  109989. + .driver = {
  109990. + .name = "tas5713",
  109991. + .owner = THIS_MODULE,
  109992. + .of_match_table = tas5713_of_match,
  109993. + },
  109994. + .probe = tas5713_i2c_probe,
  109995. + .remove = tas5713_i2c_remove,
  109996. + .id_table = tas5713_i2c_id
  109997. +};
  109998. +
  109999. +
  110000. +static int __init tas5713_modinit(void)
  110001. +{
  110002. + int ret = 0;
  110003. +
  110004. + ret = i2c_add_driver(&tas5713_i2c_driver);
  110005. + if (ret) {
  110006. + printk(KERN_ERR "Failed to register tas5713 I2C driver: %d\n",
  110007. + ret);
  110008. + }
  110009. +
  110010. + return ret;
  110011. +}
  110012. +module_init(tas5713_modinit);
  110013. +
  110014. +
  110015. +static void __exit tas5713_exit(void)
  110016. +{
  110017. + i2c_del_driver(&tas5713_i2c_driver);
  110018. +}
  110019. +module_exit(tas5713_exit);
  110020. +
  110021. +
  110022. +MODULE_AUTHOR("Sebastian Eickhoff <basti.eickhoff@googlemail.com>");
  110023. +MODULE_DESCRIPTION("ASoC driver for TAS5713");
  110024. +MODULE_LICENSE("GPL v2");
  110025. diff -Nur linux-3.17.5/sound/soc/codecs/tas5713.h linux-rpi/sound/soc/codecs/tas5713.h
  110026. --- linux-3.17.5/sound/soc/codecs/tas5713.h 1969-12-31 18:00:00.000000000 -0600
  110027. +++ linux-rpi/sound/soc/codecs/tas5713.h 2014-12-11 14:05:40.532418001 -0600
  110028. @@ -0,0 +1,210 @@
  110029. +/*
  110030. + * ASoC Driver for TAS5713
  110031. + *
  110032. + * Author: Sebastian Eickhoff <basti.eickhoff@googlemail.com>
  110033. + * Copyright 2014
  110034. + *
  110035. + * This program is free software; you can redistribute it and/or
  110036. + * modify it under the terms of the GNU General Public License
  110037. + * version 2 as published by the Free Software Foundation.
  110038. + *
  110039. + * This program is distributed in the hope that it will be useful, but
  110040. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  110041. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  110042. + * General Public License for more details.
  110043. + */
  110044. +
  110045. +#ifndef _TAS5713_H
  110046. +#define _TAS5713_H
  110047. +
  110048. +
  110049. +// TAS5713 I2C-bus register addresses
  110050. +
  110051. +#define TAS5713_CLOCK_CTRL 0x00
  110052. +#define TAS5713_DEVICE_ID 0x01
  110053. +#define TAS5713_ERROR_STATUS 0x02
  110054. +#define TAS5713_SYSTEM_CTRL1 0x03
  110055. +#define TAS5713_SERIAL_DATA_INTERFACE 0x04
  110056. +#define TAS5713_SYSTEM_CTRL2 0x05
  110057. +#define TAS5713_SOFT_MUTE 0x06
  110058. +#define TAS5713_VOL_MASTER 0x07
  110059. +#define TAS5713_VOL_CH1 0x08
  110060. +#define TAS5713_VOL_CH2 0x09
  110061. +#define TAS5713_VOL_HEADPHONE 0x0A
  110062. +#define TAS5713_VOL_CONFIG 0x0E
  110063. +#define TAS5713_MODULATION_LIMIT 0x10
  110064. +#define TAS5713_IC_DLY_CH1 0x11
  110065. +#define TAS5713_IC_DLY_CH2 0x12
  110066. +#define TAS5713_IC_DLY_CH3 0x13
  110067. +#define TAS5713_IC_DLY_CH4 0x14
  110068. +
  110069. +#define TAS5713_START_STOP_PERIOD 0x1A
  110070. +#define TAS5713_OSC_TRIM 0x1B
  110071. +#define TAS5713_BKND_ERR 0x1C
  110072. +
  110073. +#define TAS5713_INPUT_MUX 0x20
  110074. +#define TAS5713_SRC_SELECT_CH4 0x21
  110075. +#define TAS5713_PWM_MUX 0x25
  110076. +
  110077. +#define TAS5713_CH1_BQ0 0x29
  110078. +#define TAS5713_CH1_BQ1 0x2A
  110079. +#define TAS5713_CH1_BQ2 0x2B
  110080. +#define TAS5713_CH1_BQ3 0x2C
  110081. +#define TAS5713_CH1_BQ4 0x2D
  110082. +#define TAS5713_CH1_BQ5 0x2E
  110083. +#define TAS5713_CH1_BQ6 0x2F
  110084. +#define TAS5713_CH1_BQ7 0x58
  110085. +#define TAS5713_CH1_BQ8 0x59
  110086. +
  110087. +#define TAS5713_CH2_BQ0 0x30
  110088. +#define TAS5713_CH2_BQ1 0x31
  110089. +#define TAS5713_CH2_BQ2 0x32
  110090. +#define TAS5713_CH2_BQ3 0x33
  110091. +#define TAS5713_CH2_BQ4 0x34
  110092. +#define TAS5713_CH2_BQ5 0x35
  110093. +#define TAS5713_CH2_BQ6 0x36
  110094. +#define TAS5713_CH2_BQ7 0x5C
  110095. +#define TAS5713_CH2_BQ8 0x5D
  110096. +
  110097. +#define TAS5713_CH4_BQ0 0x5A
  110098. +#define TAS5713_CH4_BQ1 0x5B
  110099. +#define TAS5713_CH3_BQ0 0x5E
  110100. +#define TAS5713_CH3_BQ1 0x5F
  110101. +
  110102. +#define TAS5713_DRC1_SOFTENING_FILTER_ALPHA_OMEGA 0x3B
  110103. +#define TAS5713_DRC1_ATTACK_RELEASE_RATE 0x3C
  110104. +#define TAS5713_DRC2_SOFTENING_FILTER_ALPHA_OMEGA 0x3E
  110105. +#define TAS5713_DRC2_ATTACK_RELEASE_RATE 0x3F
  110106. +#define TAS5713_DRC1_ATTACK_RELEASE_THRES 0x40
  110107. +#define TAS5713_DRC2_ATTACK_RELEASE_THRES 0x43
  110108. +#define TAS5713_DRC_CTRL 0x46
  110109. +
  110110. +#define TAS5713_BANK_SW_CTRL 0x50
  110111. +#define TAS5713_CH1_OUTPUT_MIXER 0x51
  110112. +#define TAS5713_CH2_OUTPUT_MIXER 0x52
  110113. +#define TAS5713_CH1_INPUT_MIXER 0x53
  110114. +#define TAS5713_CH2_INPUT_MIXER 0x54
  110115. +#define TAS5713_OUTPUT_POST_SCALE 0x56
  110116. +#define TAS5713_OUTPUT_PRESCALE 0x57
  110117. +
  110118. +#define TAS5713_IDF_POST_SCALE 0x62
  110119. +
  110120. +#define TAS5713_CH1_INLINE_MIXER 0x70
  110121. +#define TAS5713_CH1_INLINE_DRC_EN_MIXER 0x71
  110122. +#define TAS5713_CH1_R_CHANNEL_MIXER 0x72
  110123. +#define TAS5713_CH1_L_CHANNEL_MIXER 0x73
  110124. +#define TAS5713_CH2_INLINE_MIXER 0x74
  110125. +#define TAS5713_CH2_INLINE_DRC_EN_MIXER 0x75
  110126. +#define TAS5713_CH2_L_CHANNEL_MIXER 0x76
  110127. +#define TAS5713_CH2_R_CHANNEL_MIXER 0x77
  110128. +
  110129. +#define TAS5713_UPDATE_DEV_ADDR_KEY 0xF8
  110130. +#define TAS5713_UPDATE_DEV_ADDR_REG 0xF9
  110131. +
  110132. +#define TAS5713_REGISTER_COUNT 0x46
  110133. +#define TAS5713_MAX_REGISTER 0xF9
  110134. +
  110135. +
  110136. +// Bitmasks for registers
  110137. +#define TAS5713_SOFT_MUTE_ALL 0x07
  110138. +
  110139. +
  110140. +
  110141. +struct tas5713_init_command {
  110142. + const int size;
  110143. + const char *const data;
  110144. +};
  110145. +
  110146. +static const struct tas5713_init_command tas5713_init_sequence[] = {
  110147. +
  110148. + // Trim oscillator
  110149. + { .size = 2, .data = "\x1B\x00" },
  110150. + // System control register 1 (0x03): block DC
  110151. + { .size = 2, .data = "\x03\x80" },
  110152. + // Mute everything
  110153. + { .size = 2, .data = "\x05\x40" },
  110154. + // Modulation limit register (0x10): 97.7%
  110155. + { .size = 2, .data = "\x10\x02" },
  110156. + // Interchannel delay registers
  110157. + // (0x11, 0x12, 0x13, and 0x14): BD mode
  110158. + { .size = 2, .data = "\x11\xB8" },
  110159. + { .size = 2, .data = "\x12\x60" },
  110160. + { .size = 2, .data = "\x13\xA0" },
  110161. + { .size = 2, .data = "\x14\x48" },
  110162. + // PWM shutdown group register (0x19): no shutdown
  110163. + { .size = 2, .data = "\x19\x00" },
  110164. + // Input multiplexer register (0x20): BD mode
  110165. + { .size = 2, .data = "\x20\x00\x89\x77\x72" },
  110166. + // PWM output mux register (0x25)
  110167. + // Channel 1 --> OUTA, channel 1 neg --> OUTB
  110168. + // Channel 2 --> OUTC, channel 2 neg --> OUTD
  110169. + { .size = 5, .data = "\x25\x01\x02\x13\x45" },
  110170. + // DRC control (0x46): DRC off
  110171. + { .size = 5, .data = "\x46\x00\x00\x00\x00" },
  110172. + // BKND_ERR register (0x1C): 299ms reset period
  110173. + { .size = 2, .data = "\x1C\x07" },
  110174. + // Mute channel 3
  110175. + { .size = 2, .data = "\x0A\xFF" },
  110176. + // Volume configuration register (0x0E): volume slew 512 steps
  110177. + { .size = 2, .data = "\x0E\x90" },
  110178. + // Clock control register (0x00): 44/48kHz, MCLK=64xfs
  110179. + { .size = 2, .data = "\x00\x60" },
  110180. + // Bank switch and eq control (0x50): no bank switching
  110181. + { .size = 5, .data = "\x50\x00\x00\x00\x00" },
  110182. + // Volume registers (0x07, 0x08, 0x09, 0x0A)
  110183. + { .size = 2, .data = "\x07\x20" },
  110184. + { .size = 2, .data = "\x08\x30" },
  110185. + { .size = 2, .data = "\x09\x30" },
  110186. + { .size = 2, .data = "\x0A\xFF" },
  110187. + // 0x72, 0x73, 0x76, 0x77 input mixer:
  110188. + // no intermix between channels
  110189. + { .size = 5, .data = "\x72\x00\x00\x00\x00" },
  110190. + { .size = 5, .data = "\x73\x00\x80\x00\x00" },
  110191. + { .size = 5, .data = "\x76\x00\x00\x00\x00" },
  110192. + { .size = 5, .data = "\x77\x00\x80\x00\x00" },
  110193. + // 0x70, 0x71, 0x74, 0x75 inline DRC mixer:
  110194. + // no inline DRC inmix
  110195. + { .size = 5, .data = "\x70\x00\x80\x00\x00" },
  110196. + { .size = 5, .data = "\x71\x00\x00\x00\x00" },
  110197. + { .size = 5, .data = "\x74\x00\x80\x00\x00" },
  110198. + { .size = 5, .data = "\x75\x00\x00\x00\x00" },
  110199. + // 0x56, 0x57 Output scale
  110200. + { .size = 5, .data = "\x56\x00\x80\x00\x00" },
  110201. + { .size = 5, .data = "\x57\x00\x02\x00\x00" },
  110202. + // 0x3B, 0x3c
  110203. + { .size = 9, .data = "\x3B\x00\x08\x00\x00\x00\x78\x00\x00" },
  110204. + { .size = 9, .data = "\x3C\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  110205. + { .size = 9, .data = "\x3E\x00\x08\x00\x00\x00\x78\x00\x00" },
  110206. + { .size = 9, .data = "\x3F\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  110207. + { .size = 9, .data = "\x40\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  110208. + { .size = 9, .data = "\x43\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  110209. + // 0x51, 0x52: output mixer
  110210. + { .size = 9, .data = "\x51\x00\x80\x00\x00\x00\x00\x00\x00" },
  110211. + { .size = 9, .data = "\x52\x00\x80\x00\x00\x00\x00\x00\x00" },
  110212. + // PEQ defaults
  110213. + { .size = 21, .data = "\x29\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110214. + { .size = 21, .data = "\x2A\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110215. + { .size = 21, .data = "\x2B\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110216. + { .size = 21, .data = "\x2C\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110217. + { .size = 21, .data = "\x2D\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110218. + { .size = 21, .data = "\x2E\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110219. + { .size = 21, .data = "\x2F\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110220. + { .size = 21, .data = "\x30\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110221. + { .size = 21, .data = "\x31\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110222. + { .size = 21, .data = "\x32\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110223. + { .size = 21, .data = "\x33\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110224. + { .size = 21, .data = "\x34\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110225. + { .size = 21, .data = "\x35\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110226. + { .size = 21, .data = "\x36\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110227. + { .size = 21, .data = "\x58\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110228. + { .size = 21, .data = "\x59\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110229. + { .size = 21, .data = "\x5C\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110230. + { .size = 21, .data = "\x5D\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110231. + { .size = 21, .data = "\x5E\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110232. + { .size = 21, .data = "\x5F\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110233. + { .size = 21, .data = "\x5A\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110234. + { .size = 21, .data = "\x5B\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  110235. +};
  110236. +
  110237. +
  110238. +#endif /* _TAS5713_H */
  110239. diff -Nur linux-3.17.5/sound/soc/codecs/wm8804.c linux-rpi/sound/soc/codecs/wm8804.c
  110240. --- linux-3.17.5/sound/soc/codecs/wm8804.c 2014-12-06 17:57:59.000000000 -0600
  110241. +++ linux-rpi/sound/soc/codecs/wm8804.c 2014-12-11 14:05:40.540418001 -0600
  110242. @@ -278,6 +278,7 @@
  110243. blen = 0x1;
  110244. break;
  110245. case 24:
  110246. + case 32:
  110247. blen = 0x2;
  110248. break;
  110249. default:
  110250. @@ -641,7 +642,7 @@
  110251. };
  110252. #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  110253. - SNDRV_PCM_FMTBIT_S24_LE)
  110254. + SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  110255. #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  110256. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  110257. @@ -674,7 +675,7 @@
  110258. .suspend = wm8804_suspend,
  110259. .resume = wm8804_resume,
  110260. .set_bias_level = wm8804_set_bias_level,
  110261. - .idle_bias_off = true,
  110262. + .idle_bias_off = false,
  110263. .controls = wm8804_snd_controls,
  110264. .num_controls = ARRAY_SIZE(wm8804_snd_controls),
  110265. diff -Nur linux-3.17.5/sound/soc/soc-core.c linux-rpi/sound/soc/soc-core.c
  110266. --- linux-3.17.5/sound/soc/soc-core.c 2014-12-06 17:57:59.000000000 -0600
  110267. +++ linux-rpi/sound/soc/soc-core.c 2014-12-11 14:05:40.572418001 -0600
  110268. @@ -2995,8 +2995,8 @@
  110269. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  110270. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  110271. - uinfo->value.integer.min = 0;
  110272. - uinfo->value.integer.max = platform_max - min;
  110273. + uinfo->value.integer.min = min;
  110274. + uinfo->value.integer.max = platform_max;
  110275. return 0;
  110276. }
  110277. @@ -3027,9 +3027,10 @@
  110278. unsigned int val, val_mask;
  110279. int ret;
  110280. - val = ((ucontrol->value.integer.value[0] + min) & mask);
  110281. if (invert)
  110282. - val = max - val;
  110283. + val = ((max - ucontrol->value.integer.value[0] + min) & mask);
  110284. + else
  110285. + val = (ucontrol->value.integer.value[0] & mask);
  110286. val_mask = mask << shift;
  110287. val = val << shift;
  110288. @@ -3038,9 +3039,10 @@
  110289. return ret;
  110290. if (snd_soc_volsw_is_stereo(mc)) {
  110291. - val = ((ucontrol->value.integer.value[1] + min) & mask);
  110292. if (invert)
  110293. - val = max - val;
  110294. + val = ((max - ucontrol->value.integer.value[1] + min) & mask);
  110295. + else
  110296. + val = (ucontrol->value.integer.value[1] & mask);
  110297. val_mask = mask << shift;
  110298. val = val << shift;
  110299. @@ -3084,9 +3086,7 @@
  110300. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  110301. if (invert)
  110302. ucontrol->value.integer.value[0] =
  110303. - max - ucontrol->value.integer.value[0];
  110304. - ucontrol->value.integer.value[0] =
  110305. - ucontrol->value.integer.value[0] - min;
  110306. + max - ucontrol->value.integer.value[0] + min;
  110307. if (snd_soc_volsw_is_stereo(mc)) {
  110308. ret = snd_soc_component_read(component, rreg, &val);
  110309. @@ -3096,9 +3096,7 @@
  110310. ucontrol->value.integer.value[1] = (val >> shift) & mask;
  110311. if (invert)
  110312. ucontrol->value.integer.value[1] =
  110313. - max - ucontrol->value.integer.value[1];
  110314. - ucontrol->value.integer.value[1] =
  110315. - ucontrol->value.integer.value[1] - min;
  110316. + max - ucontrol->value.integer.value[1] + min;
  110317. }
  110318. return 0;