pt_core.c 523 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include <linux/suspend.h>
  33. #include "pt_regs.h"
  34. #if defined(CONFIG_PANEL_NOTIFIER)
  35. #include <linux/soc/qcom/panel_event_notifier.h>
  36. #endif
  37. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  38. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  39. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  40. #define FT_VTG_MIN_UV 2800000
  41. #define FT_VTG_MAX_UV 2800000
  42. #define FT_I2C_VTG_MIN_UV 1800000
  43. #define FT_I2C_VTG_MAX_UV 1800000
  44. #define PWR_SUSPEND_LOAD_UA 165
  45. #define I2C_SUSPEND_LOAD_UA 100
  46. #define PWR_ACTIVE_LOAD_MA 12000
  47. #define I2C_ACTIVE_LOAD_MA 30000
  48. #define PT_CORE_STARTUP_RETRY_COUNT 3
  49. #define PT_STATUS_STR_LEN (50)
  50. #if defined(CONFIG_DRM) || defined(CONFIG_PANEL_NOTIFIER)
  51. static struct drm_panel *active_panel;
  52. #endif
  53. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  54. #define ENABLE_I2C_REG_ONLY
  55. #ifdef ENABLE_I2C_REG_ONLY
  56. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  57. #endif
  58. static const char *pt_driver_core_name = PT_CORE_NAME;
  59. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  60. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  61. struct pt_hid_field {
  62. int report_count;
  63. int report_size;
  64. int size; /* report_count * report_size */
  65. int offset;
  66. int data_type;
  67. int logical_min;
  68. int logical_max;
  69. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  70. u32 usage_page;
  71. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  72. struct pt_hid_report *report;
  73. bool record_field;
  74. };
  75. struct pt_hid_report {
  76. u8 id;
  77. u8 type;
  78. int size;
  79. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  80. int num_fields;
  81. int record_field_index;
  82. int header_size;
  83. int record_size;
  84. u32 usage_page;
  85. };
  86. struct atten_node {
  87. struct list_head node;
  88. char *id;
  89. struct device *dev;
  90. int (*func)(struct device *dev);
  91. int mode;
  92. };
  93. struct param_node {
  94. struct list_head node;
  95. u8 id;
  96. u32 value;
  97. u8 size;
  98. };
  99. struct module_node {
  100. struct list_head node;
  101. struct pt_module *module;
  102. void *data;
  103. };
  104. struct pt_hid_cmd {
  105. u8 opcode;
  106. u8 report_type;
  107. union {
  108. u8 report_id;
  109. u8 power_state;
  110. };
  111. u8 has_data_register;
  112. size_t write_length;
  113. u8 *write_buf;
  114. u8 *read_buf;
  115. u8 wait_interrupt;
  116. u8 reset_cmd;
  117. u16 timeout_ms;
  118. };
  119. struct pt_hid_output {
  120. u8 cmd_type;
  121. u16 length;
  122. u8 command_code;
  123. size_t write_length;
  124. u8 *write_buf;
  125. u8 novalidate;
  126. u8 reset_expected;
  127. u16 timeout_ms;
  128. };
  129. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  130. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  131. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  132. #define CREATE_PIP1_FW_CMD(command) \
  133. .cmd_type = PIP1_CMD_TYPE_FW, \
  134. .command_code = command
  135. #define CREATE_PIP1_BL_CMD(command) \
  136. .cmd_type = PIP1_CMD_TYPE_BL, \
  137. .command_code = command
  138. #define PT_MAX_PR_BUF_SIZE 2048
  139. /*******************************************************************************
  140. * FUNCTION: pt_pr_buf
  141. *
  142. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  143. *
  144. * RETURN: Void
  145. *
  146. * PARAMETERS:
  147. * *dev - pointer to Device structure
  148. * debug_level - requested debug level to print at
  149. * *buf - pointer to buffer to print
  150. * buf_len - size of buf
  151. * *data_name - Descriptive name of data prefixed to data
  152. ******************************************************************************/
  153. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  154. u16 buf_len, const char *data_name)
  155. {
  156. struct pt_core_data *cd = dev_get_drvdata(dev);
  157. int i;
  158. ssize_t pr_buf_index = 0;
  159. int max_size;
  160. /* only proceed if valid debug level and there is data to print */
  161. if (debug_level <= cd->debug_level && buf_len > 0) {
  162. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  163. if (!pr_buf)
  164. return;
  165. /*
  166. * With a space each printed char takes 3 bytes, subtract
  167. * the length of the data_name prefix as well as 11 bytes
  168. * for the " [0..xxx]: " printed before the data.
  169. */
  170. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  171. /* Ensure pr_buf_index stays within the 1018 size */
  172. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  173. data_name);
  174. for (i = 0; i < buf_len && i < max_size; i++)
  175. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  176. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  177. pt_debug(dev, debug_level, "%s\n", pr_buf);
  178. kfree(pr_buf);
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(pt_pr_buf);
  182. #ifdef TTHE_TUNER_SUPPORT
  183. /*******************************************************************************
  184. * FUNCTION: tthe_print
  185. *
  186. * SUMMARY: Format data name and time stamp as the header and format the
  187. * content of input buffer with hex base to "tthe_buf". And then wake up event
  188. * semaphore for tthe debugfs node.
  189. *
  190. * RETURN:
  191. * 0 = success
  192. * !0 = failure
  193. *
  194. * PARAMETERS:
  195. * *cd - pointer to core data
  196. * *buf - pointer to input buffer
  197. * buf_len - size of input buffer
  198. * *data_name - pointer to data name
  199. ******************************************************************************/
  200. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  201. const u8 *data_name)
  202. {
  203. int name_len = strlen(data_name);
  204. int i, n;
  205. u8 *p;
  206. int remain;
  207. u8 data_name_with_time_stamp[100];
  208. /* Prepend timestamp, if requested, to data_name */
  209. if (cd->show_timestamp) {
  210. scnprintf(data_name_with_time_stamp,
  211. sizeof(data_name_with_time_stamp),
  212. "[%u] %s", pt_get_time_stamp(), data_name);
  213. data_name = data_name_with_time_stamp;
  214. name_len = strlen(data_name);
  215. }
  216. mutex_lock(&cd->tthe_lock);
  217. if (!cd->tthe_buf)
  218. goto exit;
  219. /* Add 1 due to the '\n' that is appended at the end */
  220. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  221. goto exit;
  222. if (name_len + buf_len == 0)
  223. goto exit;
  224. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  225. if (remain < name_len)
  226. name_len = remain;
  227. p = cd->tthe_buf + cd->tthe_buf_len;
  228. memcpy(p, data_name, name_len);
  229. cd->tthe_buf_len += name_len;
  230. p += name_len;
  231. remain -= name_len;
  232. *p = 0;
  233. for (i = 0; i < buf_len; i++) {
  234. n = scnprintf(p, remain, "%02X ", buf[i]);
  235. if (n <= 0)
  236. break;
  237. p += n;
  238. remain -= n;
  239. cd->tthe_buf_len += n;
  240. }
  241. n = scnprintf(p, remain, "\n");
  242. cd->tthe_buf_len += n;
  243. exit:
  244. wake_up(&cd->wait_q);
  245. mutex_unlock(&cd->tthe_lock);
  246. return 0;
  247. }
  248. /*******************************************************************************
  249. * FUNCTION: _pt_request_tthe_print
  250. *
  251. * SUMMARY: Function pointer included in core_cmds to allow other modules
  252. * to request to print data to the "tthe_buffer".
  253. *
  254. * RETURN:
  255. * 0 = success
  256. * !0 = failure
  257. *
  258. * PARAMETERS:
  259. * *dev - pointer to device structure
  260. ******************************************************************************/
  261. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  262. int buf_len, const u8 *data_name)
  263. {
  264. struct pt_core_data *cd = dev_get_drvdata(dev);
  265. return tthe_print(cd, buf, buf_len, data_name);
  266. }
  267. #endif
  268. /*******************************************************************************
  269. * FUNCTION: pt_platform_detect_read
  270. *
  271. * SUMMARY: To be passed to platform dectect function to perform a read
  272. * operation.
  273. *
  274. * RETURN:
  275. * 0 = success
  276. * !0 = failure
  277. *
  278. * PARAMETERS:
  279. * *dev - pointer to Device structure
  280. * *buf - pointer to buffer where the data read will be stored
  281. * size - size to be read
  282. ******************************************************************************/
  283. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  284. {
  285. struct pt_core_data *cd = dev_get_drvdata(dev);
  286. return pt_adap_read_default(cd, buf, size);
  287. }
  288. /*******************************************************************************
  289. * FUNCTION: pt_add_parameter
  290. *
  291. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  292. * On every reset of the DUT this linked list is traversed and all
  293. * parameters in it are restored to the DUT.
  294. *
  295. * RETURN:
  296. * 0 = success
  297. * !0 = failure
  298. *
  299. * PARAMETERS:
  300. * *cd - pointer to core data
  301. * param_id - parameter ID to add
  302. * param_value - Value corresponding to the ID
  303. * param_size - Size of param_value
  304. ******************************************************************************/
  305. static int pt_add_parameter(struct pt_core_data *cd,
  306. u8 param_id, u32 param_value, u8 param_size)
  307. {
  308. struct param_node *param, *param_new;
  309. /* Check if parameter already exists in the list */
  310. spin_lock(&cd->spinlock);
  311. list_for_each_entry(param, &cd->param_list, node) {
  312. if (param->id == param_id) {
  313. /* Update parameter */
  314. param->value = param_value;
  315. pt_debug(cd->dev, DL_INFO,
  316. "%s: Update parameter id:%d value:%d size:%d\n",
  317. __func__, param_id, param_value, param_size);
  318. goto exit_unlock;
  319. }
  320. }
  321. spin_unlock(&cd->spinlock);
  322. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  323. if (!param_new)
  324. return -ENOMEM;
  325. param_new->id = param_id;
  326. param_new->value = param_value;
  327. param_new->size = param_size;
  328. pt_debug(cd->dev, DL_INFO,
  329. "%s: Add parameter id:%d value:%d size:%d\n",
  330. __func__, param_id, param_value, param_size);
  331. spin_lock(&cd->spinlock);
  332. list_add(&param_new->node, &cd->param_list);
  333. exit_unlock:
  334. spin_unlock(&cd->spinlock);
  335. return 0;
  336. }
  337. #ifdef TTDL_DIAGNOSTICS
  338. /*******************************************************************************
  339. * FUNCTION: pt_erase_parameter_list
  340. *
  341. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  342. * pairs. In some test cases this functionality is needed to ensure DUT
  343. * returns to a virgin state after a reset and no parameters are restored.
  344. *
  345. * RETURN:
  346. * 0 = success
  347. * !0 = failure
  348. *
  349. * PARAMETERS:
  350. * *cd - pointer to core data
  351. ******************************************************************************/
  352. static int pt_erase_parameter_list(struct pt_core_data *cd)
  353. {
  354. struct param_node *pos, *temp;
  355. spin_lock(&cd->spinlock);
  356. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  357. pt_debug(cd->dev, DL_INFO,
  358. "%s: Parameter Restore List - remove 0x%02x\n",
  359. __func__, pos->id);
  360. list_del(&pos->node);
  361. kfree(pos);
  362. }
  363. spin_unlock(&cd->spinlock);
  364. return 0;
  365. }
  366. /*******************************************************************************
  367. * FUNCTION: pt_count_parameter_list
  368. *
  369. * SUMMARY: Count the items in the RAM parameter restor list
  370. *
  371. * RETURN:
  372. * 0 = success
  373. * !0 = failure
  374. *
  375. * PARAMETERS:
  376. * *cd - pointer to core data
  377. ******************************************************************************/
  378. static int pt_count_parameter_list(struct pt_core_data *cd)
  379. {
  380. struct param_node *pos, *temp;
  381. int entries = 0;
  382. spin_lock(&cd->spinlock);
  383. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  384. entries++;
  385. spin_unlock(&cd->spinlock);
  386. return entries;
  387. }
  388. #endif /* TTDL_DIAGNOSTICS */
  389. /*******************************************************************************
  390. * FUNCTION: request_exclusive
  391. *
  392. * SUMMARY: Request exclusive access to the DUT
  393. *
  394. * RETURN:
  395. * 0 = success
  396. * !0 = failure
  397. *
  398. * PARAMETERS:
  399. * *cd - pointer to core data
  400. * *ownptr - pointer to device
  401. * timeout_ms - Timeout value
  402. ******************************************************************************/
  403. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  404. int timeout_ms)
  405. {
  406. int t = msecs_to_jiffies(timeout_ms);
  407. bool with_timeout = (timeout_ms != 0);
  408. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  409. __func__, timeout_ms);
  410. mutex_lock(&cd->system_lock);
  411. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  412. cd->exclusive_dev = ownptr;
  413. goto exit;
  414. }
  415. cd->exclusive_waits++;
  416. wait:
  417. mutex_unlock(&cd->system_lock);
  418. if (with_timeout) {
  419. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  420. if (IS_TMO(t)) {
  421. pt_debug(cd->dev, DL_ERROR,
  422. "%s: tmo waiting exclusive access\n", __func__);
  423. return -ETIME;
  424. }
  425. } else {
  426. wait_event(cd->wait_q, !cd->exclusive_dev);
  427. }
  428. mutex_lock(&cd->system_lock);
  429. if (cd->exclusive_dev)
  430. goto wait;
  431. cd->exclusive_dev = ownptr;
  432. cd->exclusive_waits--;
  433. exit:
  434. mutex_unlock(&cd->system_lock);
  435. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  436. __func__, ownptr);
  437. return 0;
  438. }
  439. /*******************************************************************************
  440. * FUNCTION: release_exclusive_
  441. *
  442. * SUMMARY: Release exclusive access to the DUT
  443. *
  444. * RETURN:
  445. * 0 = success
  446. *
  447. * PARAMETERS:
  448. * *cd - pointer to core data
  449. * *ownptr - pointer to device
  450. ******************************************************************************/
  451. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  452. {
  453. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  454. __func__);
  455. if (cd->exclusive_dev != ownptr)
  456. return -EINVAL;
  457. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  458. __func__, cd->exclusive_dev);
  459. cd->exclusive_dev = NULL;
  460. wake_up(&cd->wait_q);
  461. return 0;
  462. }
  463. /*******************************************************************************
  464. * FUNCTION: release_exclusive
  465. *
  466. * SUMMARY: Protected wrapper to release_exclusive_()
  467. *
  468. * RETURN:
  469. * 0 = success
  470. * !0 = failure
  471. *
  472. * PARAMETERS:
  473. * *cd - pointer to core data
  474. * *ownptr - pointer to device
  475. ******************************************************************************/
  476. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  477. {
  478. int rc;
  479. mutex_lock(&cd->system_lock);
  480. rc = release_exclusive_(cd, ownptr);
  481. mutex_unlock(&cd->system_lock);
  482. return rc;
  483. }
  484. /*******************************************************************************
  485. * FUNCTION: pt_hid_exec_cmd_
  486. *
  487. * SUMMARY: Send the HID command to the DUT
  488. *
  489. * RETURN:
  490. * 0 = success
  491. * !0 = failure
  492. *
  493. * PARAMETERS:
  494. * *cd - pointer to core data
  495. * *hid_cmd - pointer to the HID command to send
  496. ******************************************************************************/
  497. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  498. struct pt_hid_cmd *hid_cmd)
  499. {
  500. int rc = 0;
  501. u8 *cmd;
  502. u16 cmd_length;
  503. u8 cmd_offset = 0;
  504. cmd_length = 2 /* command register */
  505. + 2 /* command */
  506. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  507. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  508. + hid_cmd->write_length; /* Data length */
  509. if (cmd_length < 4 || cmd_length > PT_MAX_PIP1_MSG_SIZE)
  510. return -EPROTO;
  511. cmd = kzalloc(cmd_length, GFP_KERNEL);
  512. if (!cmd)
  513. return -ENOMEM;
  514. /* Set Command register */
  515. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  516. sizeof(cd->hid_desc.command_register));
  517. cmd_offset += sizeof(cd->hid_desc.command_register);
  518. /* Set Command */
  519. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  520. if (hid_cmd->report_id >= 0XF)
  521. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  522. else
  523. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  524. cmd_offset++;
  525. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  526. cmd_offset++;
  527. if (hid_cmd->report_id >= 0XF) {
  528. cmd[cmd_offset] = hid_cmd->report_id;
  529. cmd_offset++;
  530. }
  531. /* Set Data register */
  532. if (hid_cmd->has_data_register) {
  533. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  534. sizeof(cd->hid_desc.data_register));
  535. cmd_offset += sizeof(cd->hid_desc.data_register);
  536. }
  537. /* Set Data */
  538. if (hid_cmd->write_length && hid_cmd->write_buf) {
  539. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  540. hid_cmd->write_length);
  541. cmd_offset += hid_cmd->write_length;
  542. }
  543. pt_debug(cd->dev, DL_INFO,
  544. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  545. __func__, cmd_length, hid_cmd->report_id);
  546. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  547. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  548. hid_cmd->read_buf);
  549. if (rc)
  550. pt_debug(cd->dev, DL_ERROR,
  551. "%s: Fail pt_adap_transfer\n", __func__);
  552. kfree(cmd);
  553. return rc;
  554. }
  555. #ifdef TTDL_DIAGNOSTICS
  556. /*******************************************************************************
  557. * FUNCTION: pt_toggle_err_gpio
  558. *
  559. * SUMMARY: Toggles the pre-defined error GPIO
  560. *
  561. * RETURN: n/a
  562. *
  563. * PARAMETERS:
  564. * *cd - pointer to core data
  565. * type - type of err that occurred
  566. ******************************************************************************/
  567. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  568. {
  569. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  570. __func__, type);
  571. if (cd->err_gpio && type == cd->err_gpio_type) {
  572. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  573. gpio_direction_output(cd->err_gpio,
  574. !gpio_get_value(cd->err_gpio));
  575. }
  576. }
  577. /*******************************************************************************
  578. * FUNCTION: _pt_request_toggle_err_gpio
  579. *
  580. * SUMMARY: Function pointer included in core_cmds to allow other modules
  581. * to request to toggle the err_gpio
  582. *
  583. * RETURN: n/a
  584. *
  585. * PARAMETERS:
  586. * *cd - pointer to core data
  587. * type - type of err that occurred
  588. ******************************************************************************/
  589. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  590. {
  591. struct pt_core_data *cd = dev_get_drvdata(dev);
  592. pt_toggle_err_gpio(cd, type);
  593. }
  594. #endif /* TTDL_DIAGNOSTICS */
  595. /*******************************************************************************
  596. * FUNCTION: pt_hid_exec_cmd_and_wait_
  597. *
  598. * SUMMARY: Send the HID command to the DUT and wait for the response
  599. *
  600. * RETURN:
  601. * 0 = success
  602. * !0 = failure
  603. *
  604. * PARAMETERS:
  605. * *cd - pointer to core data
  606. * *hid_cmd - pointer to the HID command to send
  607. ******************************************************************************/
  608. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  609. struct pt_hid_cmd *hid_cmd)
  610. {
  611. int rc = 0;
  612. int t;
  613. u16 timeout_ms;
  614. int *cmd_state;
  615. if (hid_cmd->reset_cmd)
  616. cmd_state = &cd->hid_reset_cmd_state;
  617. else
  618. cmd_state = &cd->hid_cmd_state;
  619. if (hid_cmd->wait_interrupt) {
  620. mutex_lock(&cd->system_lock);
  621. *cmd_state = 1;
  622. mutex_unlock(&cd->system_lock);
  623. }
  624. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  625. if (rc) {
  626. if (hid_cmd->wait_interrupt)
  627. goto error;
  628. goto exit;
  629. }
  630. if (!hid_cmd->wait_interrupt)
  631. goto exit;
  632. if (hid_cmd->timeout_ms)
  633. timeout_ms = hid_cmd->timeout_ms;
  634. else
  635. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  636. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  637. msecs_to_jiffies(timeout_ms));
  638. if (IS_TMO(t)) {
  639. #ifdef TTDL_DIAGNOSTICS
  640. cd->bus_transmit_error_count++;
  641. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  642. #endif /* TTDL_DIAGNOSTICS */
  643. pt_debug(cd->dev, DL_ERROR,
  644. "%s: HID output cmd execution timed out\n",
  645. __func__);
  646. rc = -ETIME;
  647. goto error;
  648. }
  649. goto exit;
  650. error:
  651. mutex_lock(&cd->system_lock);
  652. *cmd_state = 0;
  653. mutex_unlock(&cd->system_lock);
  654. exit:
  655. return rc;
  656. }
  657. /*******************************************************************************
  658. * FUNCTION: pt_hid_cmd_reset_
  659. *
  660. * SUMMARY: Send the HID RESET command to the DUT
  661. *
  662. * RETURN:
  663. * 0 = success
  664. * !0 = failure
  665. *
  666. * PARAMETERS:
  667. * *cd - pointer to core data
  668. ******************************************************************************/
  669. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  670. {
  671. struct pt_hid_cmd hid_cmd = {
  672. .opcode = HID_CMD_RESET,
  673. .wait_interrupt = 1,
  674. .reset_cmd = 1,
  675. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  676. };
  677. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  678. }
  679. /*******************************************************************************
  680. * FUNCTION: pt_hid_cmd_reset
  681. *
  682. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  683. * access.
  684. *
  685. * RETURN:
  686. * 0 = success
  687. * !0 = failure
  688. *
  689. * PARAMETERS:
  690. * *cd - pointer to core data
  691. ******************************************************************************/
  692. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  693. {
  694. int rc = 0;
  695. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  696. if (rc < 0) {
  697. pt_debug(cd->dev, DL_ERROR,
  698. "%s: fail get exclusive ex=%p own=%p\n",
  699. __func__, cd->exclusive_dev, cd->dev);
  700. return rc;
  701. }
  702. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  703. rc = pt_hid_cmd_reset_(cd);
  704. if (release_exclusive(cd, cd->dev) < 0)
  705. pt_debug(cd->dev, DL_ERROR,
  706. "%s: fail to release exclusive\n", __func__);
  707. return rc;
  708. }
  709. /*******************************************************************************
  710. * FUNCTION: pt_hid_cmd_set_power_
  711. *
  712. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  713. *
  714. * RETURN:
  715. * 0 = success
  716. * !0 = failure
  717. *
  718. * PARAMETERS:
  719. * *cd - pointer to core data
  720. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  721. ******************************************************************************/
  722. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  723. u8 power_state)
  724. {
  725. int rc = 0;
  726. struct pt_hid_cmd hid_cmd = {
  727. .opcode = HID_CMD_SET_POWER,
  728. .wait_interrupt = 1,
  729. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  730. };
  731. hid_cmd.power_state = power_state;
  732. /* The chip won't give response if goes to Deep Standby */
  733. if (power_state == HID_POWER_STANDBY) {
  734. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  735. if (rc)
  736. pt_debug(cd->dev, DL_ERROR,
  737. "%s: Failed to set power to state:%d\n",
  738. __func__, power_state);
  739. else
  740. cd->fw_sys_mode_in_standby_state = true;
  741. return rc;
  742. }
  743. cd->fw_sys_mode_in_standby_state = false;
  744. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  745. if (rc) {
  746. pt_debug(cd->dev, DL_ERROR,
  747. "%s: Failed to set power to state:%d\n",
  748. __func__, power_state);
  749. return rc;
  750. }
  751. /* validate */
  752. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  753. || ((cd->response_buf[3] & 0x3) != power_state)
  754. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  755. rc = -EINVAL;
  756. return rc;
  757. }
  758. /*******************************************************************************
  759. * FUNCTION: pt_hid_cmd_set_power
  760. *
  761. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  762. * exclusive access.
  763. *
  764. * RETURN:
  765. * 0 = success
  766. * !0 = failure
  767. *
  768. * PARAMETERS:
  769. * *cd - pointer to core data
  770. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  771. ******************************************************************************/
  772. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  773. u8 power_state)
  774. {
  775. int rc;
  776. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  777. if (rc < 0) {
  778. pt_debug(cd->dev, DL_ERROR,
  779. "%s: fail get exclusive ex=%p own=%p\n",
  780. __func__, cd->exclusive_dev, cd->dev);
  781. return rc;
  782. }
  783. rc = pt_hid_cmd_set_power_(cd, power_state);
  784. if (release_exclusive(cd, cd->dev) < 0)
  785. pt_debug(cd->dev, DL_ERROR,
  786. "%s: fail to release exclusive\n", __func__);
  787. return rc;
  788. }
  789. static const u16 crc_table[16] = {
  790. 0x0000, 0x1021, 0x2042, 0x3063,
  791. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  792. 0x8108, 0x9129, 0xa14a, 0xb16b,
  793. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  794. };
  795. /*******************************************************************************
  796. * FUNCTION: _pt_compute_crc
  797. *
  798. * SUMMARY: Calculate CRC by CRC table.
  799. *
  800. * RETURN:
  801. * CRC calculation result
  802. *
  803. * PARAMETERS:
  804. * *buf - pointer to the data array to be calculated
  805. * size - size of data array
  806. ******************************************************************************/
  807. static u16 _pt_compute_crc(u8 *buf, u32 size)
  808. {
  809. u16 remainder = 0xFFFF;
  810. u16 xor_mask = 0x0000;
  811. u32 index;
  812. u32 byte_value;
  813. u32 table_index;
  814. u32 crc_bit_width = sizeof(u16) * 8;
  815. /* Divide the message by polynomial, via the table. */
  816. for (index = 0; index < size; index++) {
  817. byte_value = buf[index];
  818. table_index = ((byte_value >> 4) & 0x0F)
  819. ^ (remainder >> (crc_bit_width - 4));
  820. remainder = crc_table[table_index] ^ (remainder << 4);
  821. table_index = (byte_value & 0x0F)
  822. ^ (remainder >> (crc_bit_width - 4));
  823. remainder = crc_table[table_index] ^ (remainder << 4);
  824. }
  825. /* Perform the final remainder CRC. */
  826. return remainder ^ xor_mask;
  827. }
  828. u16 ccitt_Table[] = {
  829. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  830. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  831. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  832. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  833. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  834. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  835. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  836. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  837. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  838. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  839. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  840. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  841. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  842. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  843. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  844. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  845. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  846. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  847. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  848. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  849. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  850. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  851. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  852. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  853. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  854. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  855. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  856. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  857. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  858. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  859. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  860. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  861. };
  862. /*******************************************************************************
  863. * FUNCTION: crc_ccitt_calculate
  864. *
  865. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  866. *
  867. * RETURN:
  868. * CRC calculation result
  869. *
  870. * PARAMETERS:
  871. * *q - pointer to the data array to be calculated
  872. * len - size of data array
  873. ******************************************************************************/
  874. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  875. {
  876. unsigned short crc = 0xffff;
  877. while (len-- > 0)
  878. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  879. return crc;
  880. }
  881. /*******************************************************************************
  882. * FUNCTION: pt_pip2_cmd_calculate_crc
  883. *
  884. * SUMMARY: Calculate the CRC of a command packet
  885. *
  886. * RETURN: void
  887. *
  888. * PARAMETERS:
  889. * *cmd - pointer to command data
  890. * extra_bytes - Extra bytes included in command length
  891. ******************************************************************************/
  892. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  893. u8 extra_bytes)
  894. {
  895. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  896. unsigned short crc;
  897. buf[0] = cmd->len & 0xff;
  898. buf[1] = (cmd->len & 0xff00) >> 8;
  899. buf[2] = cmd->seq;
  900. buf[3] = cmd->id;
  901. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  902. /* Calculate the CRC for the first 4 bytes above and the data payload */
  903. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  904. cmd->crc[0] = (crc & 0xff00) >> 8;
  905. cmd->crc[1] = (crc & 0xff);
  906. }
  907. /*******************************************************************************
  908. * FUNCTION: pt_pip2_get_next_cmd_seq
  909. *
  910. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  911. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  912. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  913. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  914. * increment, just use the forced value.
  915. *
  916. * RETURN: Next command sequence number [0x08-0x0F]
  917. *
  918. * PARAMETERS:
  919. * *cd - pointer to core data
  920. ******************************************************************************/
  921. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  922. {
  923. #ifdef TTDL_DIAGNOSTICS
  924. if (cd->force_pip2_seq <= 0x07) {
  925. cd->pip2_cmd_tag_seq++;
  926. if (cd->pip2_cmd_tag_seq > 0x0F)
  927. cd->pip2_cmd_tag_seq = 0x08;
  928. } else {
  929. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  930. }
  931. #else
  932. cd->pip2_cmd_tag_seq++;
  933. if (cd->pip2_cmd_tag_seq > 0x0F)
  934. cd->pip2_cmd_tag_seq = 0x08;
  935. #endif
  936. return cd->pip2_cmd_tag_seq;
  937. }
  938. /*
  939. * Following macros are to define the response time (the interval between PIP2
  940. * command finishes sending and INT pin falls). The unit is in microsecond.
  941. * It has different time settings between the solution GPIO polling and Bus
  942. * polling due to the considration for system load.
  943. */
  944. #ifdef PT_POLL_RESP_BY_BUS
  945. #define POLL_RETRY_DEFAULT_INTERVAL 50
  946. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  947. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  948. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  949. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  950. #else
  951. #define POLL_RETRY_DEFAULT_INTERVAL 20
  952. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  953. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  954. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  955. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  956. #endif
  957. /*
  958. * id: the command id defined in PIP2
  959. * response_len: the (maximum) length of response.
  960. * response_time_min: minimum response time in microsecond
  961. * response_time_max: maximum response time in microsecond
  962. */
  963. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  964. {.id = PIP2_CMD_ID_PING,
  965. .response_len = 255,
  966. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  967. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  968. {.id = PIP2_CMD_ID_STATUS,
  969. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  970. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  971. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  972. {.id = PIP2_CMD_ID_CTRL,
  973. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  974. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  975. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  976. {.id = PIP2_CMD_ID_CONFIG,
  977. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  978. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  979. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  980. {.id = PIP2_CMD_ID_CLEAR,
  981. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  982. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  983. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  984. {.id = PIP2_CMD_ID_RESET,
  985. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  986. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  987. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  988. {.id = PIP2_CMD_ID_VERSION,
  989. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  990. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  991. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  992. {.id = PIP2_CMD_ID_FILE_OPEN,
  993. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  994. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  995. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  996. {.id = PIP2_CMD_ID_FILE_CLOSE,
  997. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  998. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  999. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1000. {.id = PIP2_CMD_ID_FILE_READ,
  1001. .response_len = 255,
  1002. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1003. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1004. {.id = PIP2_CMD_ID_FILE_WRITE,
  1005. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1006. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1007. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1008. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1009. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1010. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1011. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1012. {.id = PIP2_CMD_ID_FLASH_INFO,
  1013. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1014. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1015. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1016. {.id = PIP2_CMD_ID_EXECUTE,
  1017. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1018. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1019. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1020. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1021. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1022. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1023. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1024. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1025. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1026. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1027. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1028. {.id = PIP2_CMD_ID_READ_GPIO,
  1029. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1030. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1031. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1032. {.id = PIP2_CMD_EXECUTE_SCAN,
  1033. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1034. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1035. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1036. {.id = PIP2_CMD_SET_PARAMETER,
  1037. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1038. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1039. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1040. {.id = PIP2_CMD_GET_PARAMETER,
  1041. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1042. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1043. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1044. {.id = PIP2_CMD_SET_DDI_REG,
  1045. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1046. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1047. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1048. {.id = PIP2_CMD_GET_DDI_REG,
  1049. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1050. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1051. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1052. {.id = PIP2_CMD_ID_END,
  1053. .response_len = 255,
  1054. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1055. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1056. };
  1057. /*******************************************************************************
  1058. * FUNCTION: pt_pip2_get_cmd_response_len
  1059. *
  1060. * SUMMARY: Gets the expected response length based on the command ID
  1061. *
  1062. * RETURN: Expected response length
  1063. *
  1064. * PARAMETERS:
  1065. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1066. ******************************************************************************/
  1067. static int pt_pip2_get_cmd_response_len(u8 id)
  1068. {
  1069. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1070. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1071. p++;
  1072. if (p->id != PIP2_CMD_ID_END)
  1073. return p->response_len;
  1074. else
  1075. return -EPERM;
  1076. }
  1077. /*******************************************************************************
  1078. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1079. *
  1080. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1081. * finishes sending and INT pin falls) based on the command ID
  1082. *
  1083. * RETURN: Estimated minimum response time in microsecond
  1084. *
  1085. * PARAMETERS:
  1086. * id - Command ID
  1087. ******************************************************************************/
  1088. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1089. {
  1090. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1091. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1092. p++;
  1093. if (p->id != PIP2_CMD_ID_END)
  1094. return p->response_time_min;
  1095. else
  1096. return PIP2_RESP_DEFAULT_TIME_MIN;
  1097. }
  1098. /*******************************************************************************
  1099. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1100. *
  1101. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1102. * finishes sending and INT pin falls) based on the command ID
  1103. *
  1104. * RETURN: Estimated maximum response time in microsecond
  1105. *
  1106. * PARAMETERS:
  1107. * id - Command ID
  1108. ******************************************************************************/
  1109. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1110. {
  1111. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1112. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1113. p++;
  1114. if (p->id != PIP2_CMD_ID_END)
  1115. return p->response_time_max;
  1116. else
  1117. return PIP2_RESP_DEFAULT_TIME_MAX;
  1118. }
  1119. /*******************************************************************************
  1120. * FUNCTION: pt_pip2_validate_response
  1121. *
  1122. * SUMMARY: Validate the response of PIP2 command.
  1123. *
  1124. * RETURN:
  1125. * 0 = success
  1126. * !0 = failure
  1127. *
  1128. * PARAMETERS:
  1129. * *cd - pointer to core data
  1130. * *pip2_cmd - pointer to PIP2 command to send
  1131. * *read_buf - pointer to response buffer
  1132. * actual_read_len - actual read length of the response
  1133. ******************************************************************************/
  1134. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1135. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1136. u16 actual_read_len)
  1137. {
  1138. int rc = 0;
  1139. u8 response_seq = 0;
  1140. u8 reserved_bits = 0;
  1141. u8 cmd_id = 0;
  1142. u8 response_bit = 0;
  1143. unsigned short calc_crc = 0;
  1144. unsigned short resp_crc = 0;
  1145. /* Verify the length of response buffer */
  1146. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1147. pt_debug(cd->dev, DL_ERROR,
  1148. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1149. __func__, pip2_cmd->id, actual_read_len);
  1150. rc = -EINVAL;
  1151. goto exit;
  1152. }
  1153. /* Verify the CRC */
  1154. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1155. resp_crc = read_buf[actual_read_len - 2] << 8;
  1156. resp_crc |= read_buf[actual_read_len - 1];
  1157. if (resp_crc != calc_crc) {
  1158. pt_debug(cd->dev, DL_ERROR,
  1159. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1160. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1161. #ifdef TTDL_DIAGNOSTICS
  1162. cd->pip2_crc_error_count++;
  1163. #endif /* TTDL_DIAGNOSTICS */
  1164. rc = -EINVAL;
  1165. goto exit;
  1166. }
  1167. /* Verify the response bit is set */
  1168. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1169. if (!response_bit) {
  1170. pt_debug(cd->dev, DL_ERROR,
  1171. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1172. __func__, pip2_cmd->id, response_bit);
  1173. rc = -EINVAL;
  1174. goto exit;
  1175. }
  1176. /* Verify the command ID matches from command to response */
  1177. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1178. if (cmd_id != pip2_cmd->id) {
  1179. pt_debug(cd->dev, DL_ERROR,
  1180. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1181. __func__, pip2_cmd->id, cmd_id);
  1182. rc = -EINVAL;
  1183. goto exit;
  1184. }
  1185. /* Verify the SEQ number matches from command to response */
  1186. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1187. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1188. pt_debug(cd->dev, DL_ERROR,
  1189. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1190. __func__, pip2_cmd->id,
  1191. pip2_cmd->seq, response_seq);
  1192. rc = -EINVAL;
  1193. goto exit;
  1194. }
  1195. /* Verify the reserved bits are 0 */
  1196. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1197. if (reserved_bits)
  1198. pt_debug(cd->dev, DL_WARN,
  1199. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1200. __func__, pip2_cmd->id, reserved_bits);
  1201. exit:
  1202. if (rc)
  1203. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1204. "PIP RSP:");
  1205. return rc;
  1206. }
  1207. /*******************************************************************************
  1208. * FUNCTION: pt_hid_output_validate_bl_response
  1209. *
  1210. * SUMMARY: Validate the response of bootloader command.
  1211. *
  1212. * RETURN:
  1213. * 0 = success
  1214. * !0 = failure
  1215. *
  1216. * PARAMETERS:
  1217. * *cd - pointer to core data
  1218. * *hid_output - pointer to hid output data structure
  1219. ******************************************************************************/
  1220. static int pt_hid_output_validate_bl_response(
  1221. struct pt_core_data *cd,
  1222. struct pt_hid_output *hid_output)
  1223. {
  1224. u16 size;
  1225. u16 crc;
  1226. u8 status;
  1227. size = get_unaligned_le16(&cd->response_buf[0]);
  1228. if (hid_output->reset_expected && !size)
  1229. return 0;
  1230. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1231. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1232. pt_debug(cd->dev, DL_ERROR,
  1233. "%s: BL output response, wrong report_id\n", __func__);
  1234. return -EPROTO;
  1235. }
  1236. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1237. pt_debug(cd->dev, DL_ERROR,
  1238. "%s: BL output response, wrong SOP\n", __func__);
  1239. return -EPROTO;
  1240. }
  1241. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1242. pt_debug(cd->dev, DL_ERROR,
  1243. "%s: BL output response, wrong EOP\n", __func__);
  1244. return -EPROTO;
  1245. }
  1246. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1247. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1248. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1249. pt_debug(cd->dev, DL_ERROR,
  1250. "%s: BL output response, wrong CRC 0x%X\n",
  1251. __func__, crc);
  1252. return -EPROTO;
  1253. }
  1254. status = cd->response_buf[5];
  1255. if (status) {
  1256. pt_debug(cd->dev, DL_ERROR,
  1257. "%s: BL output response, ERROR:%d\n",
  1258. __func__, status);
  1259. return -EPROTO;
  1260. }
  1261. return 0;
  1262. }
  1263. /*******************************************************************************
  1264. * FUNCTION: pt_hid_output_validate_app_response
  1265. *
  1266. * SUMMARY: Validate the response of application command.
  1267. *
  1268. * RETURN:
  1269. * 0 = success
  1270. * !0 = failure
  1271. *
  1272. * PARAMETERS:
  1273. * *cd - pointer to core data
  1274. * *hid_output - pointer to hid output data structure
  1275. ******************************************************************************/
  1276. static int pt_hid_output_validate_app_response(
  1277. struct pt_core_data *cd,
  1278. struct pt_hid_output *hid_output)
  1279. {
  1280. int command_code;
  1281. u16 size;
  1282. size = get_unaligned_le16(&cd->response_buf[0]);
  1283. if (hid_output->reset_expected && !size)
  1284. return 0;
  1285. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1286. != PT_PIP_NON_HID_RESPONSE_ID) {
  1287. pt_debug(cd->dev, DL_ERROR,
  1288. "%s: APP output response, wrong report_id\n", __func__);
  1289. return -EPROTO;
  1290. }
  1291. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1292. & PIP1_RESP_COMMAND_ID_MASK;
  1293. if (command_code != hid_output->command_code) {
  1294. pt_debug(cd->dev, DL_ERROR,
  1295. "%s: APP output response, wrong command_code:%X\n",
  1296. __func__, command_code);
  1297. return -EPROTO;
  1298. }
  1299. return 0;
  1300. }
  1301. /*******************************************************************************
  1302. * FUNCTION: pt_check_set_parameter
  1303. *
  1304. * SUMMARY: Check command input and response for Set Parameter command.And
  1305. * store the parameter to the list for resume work if pass the check.
  1306. *
  1307. * PARAMETERS:
  1308. * *cd - pointer to core data
  1309. * *hid_output - pointer to hid output data structure
  1310. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1311. ******************************************************************************/
  1312. static void pt_check_set_parameter(struct pt_core_data *cd,
  1313. struct pt_hid_output *hid_output, bool raw)
  1314. {
  1315. u8 *param_buf;
  1316. u32 param_value = 0;
  1317. u8 param_size;
  1318. u8 param_id;
  1319. int i = 0;
  1320. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1321. return;
  1322. /* Check command input for Set Parameter command */
  1323. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1324. && !memcmp(&hid_output->write_buf[0],
  1325. &cd->hid_desc.output_register,
  1326. sizeof(cd->hid_desc.output_register))
  1327. && hid_output->write_buf[4] ==
  1328. PT_PIP_NON_HID_COMMAND_ID
  1329. && hid_output->write_buf[6] ==
  1330. PIP1_CMD_ID_SET_PARAM)
  1331. param_buf = &hid_output->write_buf[7];
  1332. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1333. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1334. && hid_output->write_length >= 3
  1335. && hid_output->write_length <= 6)
  1336. param_buf = &hid_output->write_buf[0];
  1337. else
  1338. return;
  1339. /* Get parameter ID, size and value */
  1340. param_id = param_buf[0];
  1341. param_size = param_buf[1];
  1342. if (param_size > 4) {
  1343. pt_debug(cd->dev, DL_ERROR,
  1344. "%s: Invalid parameter size\n", __func__);
  1345. return;
  1346. }
  1347. param_buf = &param_buf[2];
  1348. while (i < param_size)
  1349. param_value += *(param_buf++) << (8 * i++);
  1350. /* Check command response for Set Parameter command */
  1351. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1352. || (cd->response_buf[4] &
  1353. PIP1_RESP_COMMAND_ID_MASK) !=
  1354. PIP1_CMD_ID_SET_PARAM
  1355. || cd->response_buf[5] != param_id
  1356. || cd->response_buf[6] != param_size) {
  1357. pt_debug(cd->dev, DL_ERROR,
  1358. "%s: Set Parameter command not successful\n",
  1359. __func__);
  1360. return;
  1361. }
  1362. pt_add_parameter(cd, param_id, param_value, param_size);
  1363. }
  1364. /*******************************************************************************
  1365. * FUNCTION: pt_check_command
  1366. *
  1367. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1368. * called here to check output command and store parameter to the list.
  1369. *
  1370. * PARAMETERS:
  1371. * *cd - pointer to core data
  1372. * *hid_output - pointer to hid output data structure
  1373. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1374. ******************************************************************************/
  1375. static void pt_check_command(struct pt_core_data *cd,
  1376. struct pt_hid_output *hid_output, bool raw)
  1377. {
  1378. pt_check_set_parameter(cd, hid_output, raw);
  1379. }
  1380. /*******************************************************************************
  1381. * FUNCTION: pt_hid_output_validate_response
  1382. *
  1383. * SUMMARY: Validate the response of application or bootloader command.
  1384. *
  1385. * RETURN:
  1386. * 0 = success
  1387. * !0 = failure
  1388. *
  1389. * PARAMETERS:
  1390. * *cd - pointer to core data
  1391. * *hid_output - pointer to hid output data structure
  1392. ******************************************************************************/
  1393. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1394. struct pt_hid_output *hid_output)
  1395. {
  1396. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1397. return pt_hid_output_validate_bl_response(cd, hid_output);
  1398. return pt_hid_output_validate_app_response(cd, hid_output);
  1399. }
  1400. /*******************************************************************************
  1401. * FUNCTION: pt_hid_send_output_user_
  1402. *
  1403. * SUMMARY: Blindly send user data to the DUT.
  1404. *
  1405. * RETURN:
  1406. * 0 = success
  1407. * !0 = failure
  1408. *
  1409. * PARAMETERS:
  1410. * *cd - pointer to core data
  1411. * *hid_output - pointer to the command to send
  1412. ******************************************************************************/
  1413. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1414. struct pt_hid_output *hid_output)
  1415. {
  1416. int rc = 0;
  1417. int cmd;
  1418. if (!hid_output->length || !hid_output->write_buf)
  1419. return -EINVAL;
  1420. if (cd->pip2_prot_active) {
  1421. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1422. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1423. } else
  1424. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1425. pt_debug(cd->dev, DL_INFO,
  1426. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1427. __func__, hid_output->length, cmd);
  1428. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1429. hid_output->length, ">>> User CMD");
  1430. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1431. hid_output->write_buf, NULL);
  1432. if (rc)
  1433. pt_debug(cd->dev, DL_ERROR,
  1434. "%s: Fail pt_adap_transfer\n", __func__);
  1435. return rc;
  1436. }
  1437. /*******************************************************************************
  1438. * FUNCTION: pt_hid_send_output_user_and_wait_
  1439. *
  1440. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1441. *
  1442. * RETURN:
  1443. * 0 = success
  1444. * !0 = failure
  1445. *
  1446. * PARAMETERS:
  1447. * *cd - pointer to core data
  1448. * *hid_output - pointer to the command to send
  1449. ******************************************************************************/
  1450. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1451. struct pt_hid_output *hid_output)
  1452. {
  1453. int rc = 0;
  1454. int t;
  1455. mutex_lock(&cd->system_lock);
  1456. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1457. mutex_unlock(&cd->system_lock);
  1458. rc = pt_hid_send_output_user_(cd, hid_output);
  1459. if (rc)
  1460. goto error;
  1461. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1462. msecs_to_jiffies(cd->pip_cmd_timeout));
  1463. if (IS_TMO(t)) {
  1464. #ifdef TTDL_DIAGNOSTICS
  1465. cd->bus_transmit_error_count++;
  1466. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1467. #endif /* TTDL_DIAGNOSTICS */
  1468. pt_debug(cd->dev, DL_ERROR,
  1469. "%s: HID output cmd execution timed out\n",
  1470. __func__);
  1471. rc = -ETIME;
  1472. goto error;
  1473. }
  1474. pt_check_command(cd, hid_output, true);
  1475. goto exit;
  1476. error:
  1477. mutex_lock(&cd->system_lock);
  1478. cd->hid_cmd_state = 0;
  1479. mutex_unlock(&cd->system_lock);
  1480. exit:
  1481. return rc;
  1482. }
  1483. /*******************************************************************************
  1484. * FUNCTION: pt_check_irq_asserted
  1485. *
  1486. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1487. * the FW can hold the INT line low ~150us after the read is complete.
  1488. * NOTE: if irq_stat is not defined this function will return false
  1489. *
  1490. * RETURN:
  1491. * true = IRQ asserted
  1492. * false = IRQ not asserted
  1493. *
  1494. * PARAMETERS:
  1495. * *cd - pointer to core data
  1496. ******************************************************************************/
  1497. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1498. {
  1499. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1500. /*
  1501. * Workaround for FW defect, CDT165308
  1502. * bl_launch app creates a glitch in IRQ line
  1503. */
  1504. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1505. && cd->cpdata->irq_stat) {
  1506. /*
  1507. * in X1S panel and GC1546 panel, the width for the INT
  1508. * glitch is about 4us,the normal INT width of response
  1509. * will last more than 200us, so use 10us delay
  1510. * for distinguish the glitch the normal INT is enough.
  1511. */
  1512. udelay(10);
  1513. }
  1514. #endif
  1515. if (cd->cpdata->irq_stat) {
  1516. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1517. == PT_IRQ_ASSERTED_VALUE) {
  1518. /* Debounce to allow FW to release INT */
  1519. usleep_range(100, 200);
  1520. }
  1521. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1522. == PT_IRQ_ASSERTED_VALUE)
  1523. return true;
  1524. else
  1525. return false;
  1526. }
  1527. return true;
  1528. }
  1529. /*******************************************************************************
  1530. * FUNCTION: pt_flush_bus
  1531. *
  1532. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1533. * Used if IRQ is found to be stuck low
  1534. *
  1535. * RETURN: Length of bytes read from bus
  1536. *
  1537. * PARAMETERS:
  1538. * *cd - pointer to core data
  1539. * flush_type - type of flush
  1540. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1541. * - PT_FLUSH_BUS_FULL_256_READ
  1542. * *read_buf - pointer to store read data
  1543. ******************************************************************************/
  1544. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1545. u8 flush_type, u8 *read_buf)
  1546. {
  1547. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1548. u16 pip_len;
  1549. int bytes_read;
  1550. int rc = 0;
  1551. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1552. rc = pt_adap_read_default(cd, buf, 2);
  1553. if (rc) {
  1554. bytes_read = 0;
  1555. goto exit;
  1556. }
  1557. pip_len = get_unaligned_le16(&buf[0]);
  1558. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1559. #ifdef TTDL_DIAGNOSTICS
  1560. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1561. #endif
  1562. bytes_read = 2;
  1563. pt_debug(cd->dev, DL_INFO,
  1564. "%s: Empty buf detected - len=0x%04X\n",
  1565. __func__, pip_len);
  1566. } else if (pip_len == 0) {
  1567. bytes_read = 0;
  1568. pt_debug(cd->dev, DL_INFO,
  1569. "%s: Sentinel detected\n", __func__);
  1570. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1571. pt_debug(cd->dev, DL_ERROR,
  1572. "%s: Illegal len=0x%04x, force %d byte read\n",
  1573. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1574. rc = pt_adap_read_default(cd, buf,
  1575. PT_MAX_PIP2_MSG_SIZE);
  1576. if (!rc)
  1577. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1578. else
  1579. bytes_read = 0;
  1580. } else {
  1581. pt_debug(cd->dev, DL_INFO,
  1582. "%s: Flush read of %d bytes...\n",
  1583. __func__, pip_len);
  1584. rc = pt_adap_read_default(cd, buf, pip_len);
  1585. if (!rc)
  1586. bytes_read = pip_len;
  1587. else
  1588. bytes_read = 0;
  1589. }
  1590. } else {
  1591. pt_debug(cd->dev, DL_INFO,
  1592. "%s: Forced flush of max %d bytes...\n",
  1593. __func__, PT_MAX_PIP2_MSG_SIZE);
  1594. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1595. if (!rc)
  1596. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1597. else
  1598. bytes_read = 0;
  1599. }
  1600. if (read_buf && (bytes_read > 3))
  1601. memcpy(read_buf, buf, bytes_read);
  1602. exit:
  1603. return bytes_read;
  1604. }
  1605. /*******************************************************************************
  1606. * FUNCTION: pt_flush_bus_if_irq_asserted
  1607. *
  1608. * SUMMARY: This function will flush the active bus if the INT is found to be
  1609. * asserted.
  1610. *
  1611. * RETURN: bytes cleared from bus
  1612. *
  1613. * PARAMETERS:
  1614. * *cd - pointer the core data structure
  1615. * flush_type - type of flush
  1616. * - PT_FLUSH_BUS_BASED_ON_LEN
  1617. * - PT_FLUSH_BUS_FULL_256_READ
  1618. ******************************************************************************/
  1619. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1620. {
  1621. int count = 0;
  1622. int bytes_read = 0;
  1623. while (pt_check_irq_asserted(cd) && count < 5) {
  1624. count++;
  1625. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1626. if (bytes_read) {
  1627. pt_debug(cd->dev, DL_WARN,
  1628. "%s: Cleared %d bytes off bus\n",
  1629. __func__, bytes_read);
  1630. }
  1631. }
  1632. if (pt_check_irq_asserted(cd)) {
  1633. pt_debug(cd->dev, DL_ERROR,
  1634. "%s: IRQ still asserted, %d bytes read\n",
  1635. __func__, bytes_read);
  1636. } else {
  1637. pt_debug(cd->dev, DL_INFO,
  1638. "%s: IRQ cleared, %d bytes read\n",
  1639. __func__, bytes_read);
  1640. }
  1641. return bytes_read;
  1642. }
  1643. /*******************************************************************************
  1644. * FUNCTION: pt_hid_send_output_
  1645. *
  1646. * SUMMARY: Send a touch application command to the DUT
  1647. *
  1648. * RETURN:
  1649. * 0 = success
  1650. * !0 = failure
  1651. *
  1652. * PARAMETERS:
  1653. * *cd - pointer to core data
  1654. * *hid_output - pointer to the command to send
  1655. ******************************************************************************/
  1656. static int pt_hid_send_output_(struct pt_core_data *cd,
  1657. struct pt_hid_output *hid_output)
  1658. {
  1659. int rc = 0;
  1660. u8 *cmd;
  1661. u16 length;
  1662. u16 crc;
  1663. u8 report_id;
  1664. u8 cmd_offset = 0;
  1665. u8 cmd_allocated = 0;
  1666. switch (hid_output->cmd_type) {
  1667. case PIP1_CMD_TYPE_FW:
  1668. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1669. length = 5;
  1670. break;
  1671. case PIP1_CMD_TYPE_BL:
  1672. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1673. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1674. break;
  1675. default:
  1676. return -EINVAL;
  1677. }
  1678. length += hid_output->write_length;
  1679. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1680. cmd = kzalloc(length + 2, GFP_KERNEL);
  1681. if (!cmd)
  1682. return -ENOMEM;
  1683. cmd_allocated = 1;
  1684. } else {
  1685. cmd = cd->cmd_buf;
  1686. }
  1687. /* Set Output register */
  1688. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1689. sizeof(cd->hid_desc.output_register));
  1690. cmd_offset += sizeof(cd->hid_desc.output_register);
  1691. cmd[cmd_offset++] = LOW_BYTE(length);
  1692. cmd[cmd_offset++] = HI_BYTE(length);
  1693. cmd[cmd_offset++] = report_id;
  1694. cmd[cmd_offset++] = 0x0; /* reserved */
  1695. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1696. cmd[cmd_offset++] = PIP1_BL_SOP;
  1697. cmd[cmd_offset++] = hid_output->command_code;
  1698. /* Set Data Length for bootloader */
  1699. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1700. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1701. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1702. }
  1703. /* Set Data */
  1704. if (hid_output->write_length && hid_output->write_buf) {
  1705. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1706. hid_output->write_length);
  1707. cmd_offset += hid_output->write_length;
  1708. }
  1709. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1710. crc = _pt_compute_crc(&cmd[6],
  1711. hid_output->write_length + 4);
  1712. cmd[cmd_offset++] = LOW_BYTE(crc);
  1713. cmd[cmd_offset++] = HI_BYTE(crc);
  1714. cmd[cmd_offset++] = PIP1_BL_EOP;
  1715. }
  1716. pt_debug(cd->dev, DL_INFO,
  1717. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1718. __func__, length + 2, hid_output->command_code);
  1719. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1720. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1721. if (rc)
  1722. pt_debug(cd->dev, DL_ERROR,
  1723. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1724. if (cmd_allocated)
  1725. kfree(cmd);
  1726. return rc;
  1727. }
  1728. /*******************************************************************************
  1729. * FUNCTION: pt_pip1_send_output_and_wait_
  1730. *
  1731. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1732. *
  1733. * RETURN:
  1734. * 0 = success
  1735. * !0 = failure
  1736. *
  1737. * PARAMETERS:
  1738. * *cd - pointer to core data
  1739. * *hid_output - pointer to the command to send
  1740. ******************************************************************************/
  1741. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1742. struct pt_hid_output *hid_output)
  1743. {
  1744. int rc = 0;
  1745. int t;
  1746. u16 timeout_ms;
  1747. mutex_lock(&cd->system_lock);
  1748. cd->hid_cmd_state = hid_output->command_code + 1;
  1749. mutex_unlock(&cd->system_lock);
  1750. if (hid_output->timeout_ms)
  1751. timeout_ms = hid_output->timeout_ms;
  1752. else
  1753. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1754. rc = pt_hid_send_output_(cd, hid_output);
  1755. if (rc)
  1756. goto error;
  1757. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1758. msecs_to_jiffies(timeout_ms));
  1759. if (IS_TMO(t)) {
  1760. #ifdef TTDL_DIAGNOSTICS
  1761. cd->bus_transmit_error_count++;
  1762. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1763. #endif /* TTDL_DIAGNOSTICS */
  1764. pt_debug(cd->dev, DL_ERROR,
  1765. "%s: HID output cmd execution timed out (%dms)\n",
  1766. __func__, timeout_ms);
  1767. rc = -ETIME;
  1768. goto error;
  1769. }
  1770. if (!hid_output->novalidate)
  1771. rc = pt_hid_output_validate_response(cd, hid_output);
  1772. pt_check_command(cd, hid_output, false);
  1773. goto exit;
  1774. error:
  1775. mutex_lock(&cd->system_lock);
  1776. cd->hid_cmd_state = 0;
  1777. mutex_unlock(&cd->system_lock);
  1778. exit:
  1779. return rc;
  1780. }
  1781. /*******************************************************************************
  1782. * FUNCTION: pt_hid_output_user_cmd_
  1783. *
  1784. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1785. * to the DUT waiting for the response and loading it into the read buffer
  1786. *
  1787. * RETURN:
  1788. * 0 = success
  1789. * !0 = failure
  1790. *
  1791. * PARAMETERS:
  1792. * *cd - pointer to core data
  1793. * read_len - expected read length of the response
  1794. * *read_buf - pointer to where the response will be loaded
  1795. * write_len - length of the write buffer
  1796. * *write_buf - pointer to the write buffer
  1797. * *actual_read_len - pointer to the actual amount of data read back
  1798. ******************************************************************************/
  1799. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1800. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1801. u16 *actual_read_len)
  1802. {
  1803. int rc = 0;
  1804. u16 size;
  1805. struct pt_hid_output hid_output = {
  1806. .length = write_len,
  1807. .write_buf = write_buf,
  1808. };
  1809. #ifdef TTHE_TUNER_SUPPORT
  1810. if (!cd->pip2_send_user_cmd) {
  1811. int command_code = 0;
  1812. int len;
  1813. /* Print up to cmd ID */
  1814. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1815. if (write_len < len)
  1816. len = write_len;
  1817. else
  1818. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1819. & PIP1_CMD_COMMAND_ID_MASK;
  1820. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1821. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1822. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1823. tthe_print(cd, write_buf, len, "CMD=");
  1824. }
  1825. #endif
  1826. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1827. if (rc)
  1828. return rc;
  1829. /* Get the response size from the first 2 bytes in the response */
  1830. size = get_unaligned_le16(&cd->response_buf[0]);
  1831. /* Ensure size is not greater than max buffer size */
  1832. if (size > PT_MAX_PIP2_MSG_SIZE)
  1833. size = PT_MAX_PIP2_MSG_SIZE;
  1834. /* Minimum size to read is the 2 byte len field */
  1835. if (size == 0)
  1836. size = 2;
  1837. if (size > read_len) {
  1838. pt_debug(cd->dev, DL_ERROR,
  1839. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1840. __func__, size, read_len);
  1841. *actual_read_len = 0;
  1842. return -EIO;
  1843. }
  1844. memcpy(read_buf, cd->response_buf, size);
  1845. *actual_read_len = size;
  1846. return 0;
  1847. }
  1848. /*******************************************************************************
  1849. * FUNCTION: pt_hid_output_user_cmd
  1850. *
  1851. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1852. * the DUT.
  1853. *
  1854. * RETURN:
  1855. * 0 = success
  1856. * !0 = failure
  1857. *
  1858. * PARAMETERS:
  1859. * *cd - pointer to core data
  1860. * read_len - length of data to read
  1861. * *read_buf - pointer to store read data
  1862. * write_len - length of data to write
  1863. * *write_buf - pointer to buffer to write
  1864. * *actual_read_len - pointer to store data length actually read
  1865. ******************************************************************************/
  1866. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1867. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1868. u16 *actual_read_len)
  1869. {
  1870. int rc;
  1871. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1872. if (rc < 0) {
  1873. pt_debug(cd->dev, DL_ERROR,
  1874. "%s: fail get exclusive ex=%p own=%p\n",
  1875. __func__, cd->exclusive_dev, cd->dev);
  1876. return rc;
  1877. }
  1878. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1879. write_len, write_buf, actual_read_len);
  1880. if (release_exclusive(cd, cd->dev) < 0)
  1881. pt_debug(cd->dev, DL_ERROR,
  1882. "%s: fail to release exclusive\n", __func__);
  1883. return rc;
  1884. }
  1885. /*******************************************************************************
  1886. * FUNCTION: _pt_request_pip2_send_cmd
  1887. *
  1888. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1889. * interrupt and reads response data to read_buf
  1890. *
  1891. * RETURN:
  1892. * 0 = success
  1893. * !0 = failure
  1894. *
  1895. * PARAMETERS:
  1896. * *dev - pointer to device structure
  1897. * protect - flag to run in protected mode
  1898. * id - ID of PIP command
  1899. * *data - pointer to PIP data payload
  1900. * report_body_len - report length
  1901. * *read_buf - pointer to response buffer
  1902. * *actual_read_len - pointer to response buffer length
  1903. ******************************************************************************/
  1904. static int _pt_request_pip2_send_cmd(struct device *dev,
  1905. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1906. u16 *actual_read_len)
  1907. {
  1908. struct pt_core_data *cd = dev_get_drvdata(dev);
  1909. struct pip2_cmd_structure pip2_cmd;
  1910. int rc = 0;
  1911. u16 i = 0;
  1912. u16 j = 0;
  1913. u16 write_len;
  1914. u8 *write_buf = NULL;
  1915. u16 read_len;
  1916. u8 extra_bytes;
  1917. if (report_body_len > 247)
  1918. return -EPROTO;
  1919. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1920. /* Hard coded register for PIP2.x */
  1921. pip2_cmd.reg[0] = 0x01;
  1922. pip2_cmd.reg[1] = 0x01;
  1923. /*
  1924. * For PIP2.1+ the length field value includes itself:
  1925. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1926. *
  1927. * The overall write length must include only the register:
  1928. * ADD 2: 2 (Register)
  1929. */
  1930. extra_bytes = 6;
  1931. write_len = 2;
  1932. /* PIP2 the CMD ID is a 7bit field */
  1933. if (id > PIP2_CMD_ID_END) {
  1934. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1935. __func__, id);
  1936. rc = -EINVAL;
  1937. goto exit;
  1938. }
  1939. pip2_cmd.len = report_body_len + extra_bytes;
  1940. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1941. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1942. pip2_cmd.data = data;
  1943. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1944. /* Add the command length to the extra bytes based on PIP version */
  1945. write_len += pip2_cmd.len;
  1946. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1947. __func__, pip2_cmd.len, write_len);
  1948. write_buf = kzalloc(write_len, GFP_KERNEL);
  1949. if (write_buf == NULL) {
  1950. rc = -ENOMEM;
  1951. goto exit;
  1952. }
  1953. write_buf[i++] = pip2_cmd.reg[0];
  1954. write_buf[i++] = pip2_cmd.reg[1];
  1955. write_buf[i++] = pip2_cmd.len & 0xff;
  1956. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1957. write_buf[i++] = pip2_cmd.seq;
  1958. write_buf[i++] = pip2_cmd.id;
  1959. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1960. write_buf[j] = pip2_cmd.data[j-i];
  1961. write_buf[j++] = pip2_cmd.crc[0];
  1962. write_buf[j++] = pip2_cmd.crc[1];
  1963. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1964. if (read_len < 0)
  1965. read_len = 255;
  1966. pt_debug(dev, DL_INFO,
  1967. "%s cmd_id[0x%02X] expected response length:%d ",
  1968. __func__, pip2_cmd.id, read_len);
  1969. /*
  1970. * All PIP2 commands come through this function.
  1971. * Set flag for PIP2.x interface to allow response parsing to know
  1972. * how to decode the protocol header.
  1973. */
  1974. mutex_lock(&cd->system_lock);
  1975. cd->pip2_prot_active = true;
  1976. cd->pip2_send_user_cmd = true;
  1977. mutex_unlock(&cd->system_lock);
  1978. if (protect == PT_CORE_CMD_PROTECTED)
  1979. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1980. write_len, write_buf, actual_read_len);
  1981. else {
  1982. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1983. write_len, write_buf, actual_read_len);
  1984. }
  1985. if (rc) {
  1986. pt_debug(dev, DL_ERROR,
  1987. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1988. __func__, rc);
  1989. goto exit;
  1990. }
  1991. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1992. *actual_read_len);
  1993. exit:
  1994. mutex_lock(&cd->system_lock);
  1995. cd->pip2_prot_active = false;
  1996. cd->pip2_send_user_cmd = false;
  1997. mutex_unlock(&cd->system_lock);
  1998. kfree(write_buf);
  1999. return rc;
  2000. }
  2001. /*******************************************************************************
  2002. * FUNCTION: _pt_pip2_send_cmd_no_int
  2003. *
  2004. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2005. * reads response data to read_buf if response is available.
  2006. *
  2007. * NOTE:
  2008. * Interrupt MUST be disabled before to call this function.
  2009. *
  2010. * RETURN:
  2011. * 0 = success
  2012. * !0 = failure
  2013. *
  2014. * PARAMETERS:
  2015. * *dev - pointer to device structure
  2016. * protect - flag to run in protected mode
  2017. * id - ID of PIP command
  2018. * *data - pointer to PIP data payload
  2019. * report_body_len - report length
  2020. * *read_buf - pointer to response buffer
  2021. * *actual_read_len - pointer to response buffer length
  2022. ******************************************************************************/
  2023. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2024. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2025. u16 *actual_read_len)
  2026. {
  2027. int max_retry = 0;
  2028. int retry = 0;
  2029. int rc = 0;
  2030. u16 i = 0;
  2031. u16 j = 0;
  2032. u16 write_len;
  2033. u8 *write_buf = NULL;
  2034. u16 read_len;
  2035. u16 size = 0;
  2036. u8 response_seq = 0;
  2037. u8 extra_bytes;
  2038. u32 retry_interval = 0;
  2039. u32 retry_total_time = 0;
  2040. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2041. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2042. struct pt_core_data *cd = dev_get_drvdata(dev);
  2043. struct pip2_cmd_structure pip2_cmd;
  2044. if (report_body_len > 247)
  2045. return -EPROTO;
  2046. if (protect == PT_CORE_CMD_PROTECTED) {
  2047. rc = request_exclusive(cd,
  2048. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2049. if (rc < 0) {
  2050. pt_debug(cd->dev, DL_ERROR,
  2051. "%s: fail get exclusive ex=%p own=%p\n",
  2052. __func__, cd->exclusive_dev, cd->dev);
  2053. return rc;
  2054. }
  2055. }
  2056. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2057. /* Hard coded register for PIP2.x */
  2058. pip2_cmd.reg[0] = 0x01;
  2059. pip2_cmd.reg[1] = 0x01;
  2060. /*
  2061. * For PIP2.1+ the length field value includes itself:
  2062. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2063. *
  2064. * The overall write length must include only the register:
  2065. * ADD 2: 2 (Register)
  2066. */
  2067. extra_bytes = 6;
  2068. write_len = 2;
  2069. pip2_cmd.len = report_body_len + extra_bytes;
  2070. pip2_cmd.id = id;
  2071. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2072. pip2_cmd.data = data;
  2073. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2074. /* Add the command length to the extra bytes based on PIP version */
  2075. write_len += pip2_cmd.len;
  2076. write_buf = kzalloc(write_len, GFP_KERNEL);
  2077. if (write_buf == NULL) {
  2078. rc = -ENOMEM;
  2079. goto exit;
  2080. }
  2081. write_buf[i++] = pip2_cmd.reg[0];
  2082. write_buf[i++] = pip2_cmd.reg[1];
  2083. write_buf[i++] = pip2_cmd.len & 0xff;
  2084. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2085. write_buf[i++] = pip2_cmd.seq;
  2086. write_buf[i++] = pip2_cmd.id;
  2087. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2088. write_buf[j] = pip2_cmd.data[j-i];
  2089. write_buf[j++] = pip2_cmd.crc[0];
  2090. write_buf[j++] = pip2_cmd.crc[1];
  2091. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2092. if (read_len < 0)
  2093. read_len = 255;
  2094. pt_debug(dev, DL_INFO,
  2095. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2096. __func__, pip2_cmd.id, read_len);
  2097. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2098. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2099. if (rc) {
  2100. pt_debug(dev, DL_ERROR,
  2101. "%s: SPI write Error = %d\n",
  2102. __func__, rc);
  2103. goto exit;
  2104. }
  2105. #ifdef PT_POLL_RESP_BY_BUS
  2106. /*
  2107. * Frequent bus read can increase system load obviously. The expected
  2108. * first bus read should be valid and timely. The tollerance for
  2109. * usleep_range should be limited. The minimum response delay (between
  2110. * command finishes sending and INT pin falls) is less than 50
  2111. * microseconds. So the 10 microseconds should be maximum tollerance
  2112. * with the consideration that the unit to calculate the response delay
  2113. * is 10 microseconds and more precise is not necessary. Every
  2114. * additional 10 microseconds only contribute less than 3 milliseconds
  2115. * for whole BL.
  2116. */
  2117. usleep_range(resp_time_min, resp_time_min+10);
  2118. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2119. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2120. rc = pt_adap_read_default(cd, read_buf, read_len);
  2121. if (rc) {
  2122. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2123. __func__, rc);
  2124. break;
  2125. }
  2126. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2127. size = get_unaligned_le16(&read_buf[0]);
  2128. if ((size <= read_len) &&
  2129. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2130. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2131. break;
  2132. }
  2133. /*
  2134. * To reduce the bus and system load, increase the sleep
  2135. * step gradually:
  2136. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2137. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2138. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2139. */
  2140. retry++;
  2141. if (retry < 20) {
  2142. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2143. usleep_range(retry_interval,
  2144. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2145. } else if (retry < 40) {
  2146. retry_interval += 1000;
  2147. usleep_range(retry_interval, retry_interval + 1000);
  2148. } else {
  2149. retry_interval += 50000;
  2150. msleep(retry_interval/1000);
  2151. }
  2152. retry_total_time += retry_interval;
  2153. }
  2154. #else
  2155. /*
  2156. * Frequent GPIO read will not increase CPU/system load heavily if the
  2157. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2158. * fixed interval: 20 us.
  2159. */
  2160. usleep_range(resp_time_min, resp_time_min+10);
  2161. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2162. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2163. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2164. rc = pt_adap_read_default(cd, read_buf, read_len);
  2165. size = get_unaligned_le16(&read_buf[0]);
  2166. if (rc)
  2167. pt_debug(dev, DL_ERROR,
  2168. "%s: SPI read Error = %d\n",
  2169. __func__, rc);
  2170. else if (size > read_len) {
  2171. pt_debug(cd->dev, DL_ERROR,
  2172. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2173. __func__, size, read_len);
  2174. rc = -EIO;
  2175. }
  2176. break;
  2177. }
  2178. /*
  2179. * Poll GPIO with fixed interval 20 us, and tollerance is
  2180. * limited to 10 us to speed up the process.
  2181. */
  2182. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2183. usleep_range(retry_interval, retry_interval+10);
  2184. retry_total_time += retry_interval;
  2185. }
  2186. #endif
  2187. *actual_read_len = size;
  2188. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2189. pt_debug(dev, DL_ERROR,
  2190. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2191. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2192. *actual_read_len = 0;
  2193. rc = -EINVAL;
  2194. }
  2195. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2196. "<<< NO_INT Read");
  2197. exit:
  2198. kfree(write_buf);
  2199. if (protect == PT_CORE_CMD_PROTECTED) {
  2200. if (release_exclusive(cd, cd->dev) < 0)
  2201. pt_debug(cd->dev, DL_ERROR,
  2202. "%s: fail to release exclusive\n", __func__);
  2203. }
  2204. return rc;
  2205. }
  2206. /*******************************************************************************
  2207. * FUNCTION: pt_pip_null_
  2208. *
  2209. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2210. * This function is used by watchdog to check if the fw corrupts.
  2211. *
  2212. * RETURN:
  2213. * 0 = success
  2214. * !0 = failure
  2215. *
  2216. * PARAMETERS:
  2217. * *cd - pointer to core data
  2218. ******************************************************************************/
  2219. static int pt_pip_null_(struct pt_core_data *cd)
  2220. {
  2221. struct pt_hid_output hid_output = {
  2222. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2223. };
  2224. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2225. }
  2226. /*******************************************************************************
  2227. * FUNCTION: pt_pip_null
  2228. *
  2229. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2230. *
  2231. * RETURN:
  2232. * 0 = success
  2233. * !0 = failure
  2234. *
  2235. * PARAMETERS:
  2236. * *cd - pointer to core data
  2237. ******************************************************************************/
  2238. static int pt_pip_null(struct pt_core_data *cd)
  2239. {
  2240. int rc;
  2241. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2242. if (rc < 0) {
  2243. pt_debug(cd->dev, DL_ERROR,
  2244. "%s: fail get exclusive ex=%p own=%p\n",
  2245. __func__, cd->exclusive_dev, cd->dev);
  2246. return rc;
  2247. }
  2248. rc = pt_pip_null_(cd);
  2249. if (release_exclusive(cd, cd->dev) < 0)
  2250. pt_debug(cd->dev, DL_ERROR,
  2251. "%s: fail to release exclusive\n", __func__);
  2252. return rc;
  2253. }
  2254. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2255. /*******************************************************************************
  2256. * FUNCTION: pt_pip_start_bootloader_
  2257. *
  2258. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2259. *
  2260. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2261. * the WD active could cause this function to fail!
  2262. *
  2263. * RETURN:
  2264. * 0 = success
  2265. * !0 = failure
  2266. *
  2267. * PARAMETERS:
  2268. * *cd - pointer to core data
  2269. ******************************************************************************/
  2270. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2271. {
  2272. int rc = 0;
  2273. struct pt_hid_output hid_output = {
  2274. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2275. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2276. .reset_expected = 1,
  2277. };
  2278. if (cd->watchdog_enabled) {
  2279. pt_debug(cd->dev, DL_WARN,
  2280. "%s: watchdog isn't stopped before enter bl\n",
  2281. __func__);
  2282. goto exit;
  2283. }
  2284. /* Reset startup status after entering BL, new DUT enum required */
  2285. cd->startup_status = STARTUP_STATUS_START;
  2286. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2287. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2288. if (rc) {
  2289. pt_debug(cd->dev, DL_ERROR,
  2290. "%s: Start BL PIP cmd failed. rc = %d\n",
  2291. __func__, rc);
  2292. }
  2293. exit:
  2294. return rc;
  2295. }
  2296. /*******************************************************************************
  2297. * FUNCTION: pt_pip_start_bootloader
  2298. *
  2299. * SUMMARY: Protected function to force DUT to enter the BL
  2300. *
  2301. * RETURN:
  2302. * 0 = success
  2303. * !0 = failure
  2304. *
  2305. * PARAMETERS:
  2306. * *cd - pointer to core data structure
  2307. ******************************************************************************/
  2308. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2309. {
  2310. int rc;
  2311. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2312. if (rc < 0) {
  2313. pt_debug(cd->dev, DL_ERROR,
  2314. "%s: fail get exclusive ex=%p own=%p\n",
  2315. __func__, cd->exclusive_dev, cd->dev);
  2316. return rc;
  2317. }
  2318. rc = pt_pip_start_bootloader_(cd);
  2319. if (release_exclusive(cd, cd->dev) < 0)
  2320. pt_debug(cd->dev, DL_ERROR,
  2321. "%s: fail to release exclusive\n", __func__);
  2322. return rc;
  2323. }
  2324. /*******************************************************************************
  2325. * FUNCTION: _pt_request_pip_start_bl
  2326. *
  2327. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2328. * modules to request the DUT to enter the BL
  2329. *
  2330. * RETURN:
  2331. * 0 = success
  2332. * !0 = failure
  2333. *
  2334. * PARAMETERS:
  2335. * *dev - pointer to device structure
  2336. * protect - flag to run in protected mode
  2337. ******************************************************************************/
  2338. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2339. {
  2340. struct pt_core_data *cd = dev_get_drvdata(dev);
  2341. if (protect)
  2342. return pt_pip_start_bootloader(cd);
  2343. return pt_pip_start_bootloader_(cd);
  2344. }
  2345. /*******************************************************************************
  2346. * FUNCTION: pt_pip2_ver_load_ttdata
  2347. *
  2348. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2349. * command into the core data struct.
  2350. *
  2351. * RETURN: n/a
  2352. *
  2353. * PARAMETERS:
  2354. * *cd - pointer to core data structure
  2355. * len - Length of data in response_buf
  2356. ******************************************************************************/
  2357. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2358. {
  2359. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2360. struct pt_pip2_version_full *full_ver;
  2361. struct pt_pip2_version *ver;
  2362. /*
  2363. * The PIP2 VERSION command can return different lengths of data.
  2364. * The additional LOT fields are included when the packet
  2365. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2366. * NOTE:
  2367. * - The FW would swap the BL and FW versions when reporting
  2368. * the small packet.
  2369. * - Sub Lot bytes 16 and 17 are reserved.
  2370. */
  2371. if (len >= 0x1D) {
  2372. full_ver = (struct pt_pip2_version_full *)
  2373. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2374. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2375. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2376. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2377. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2378. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2379. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2380. /*
  2381. * BL PIP 2.02 and greater the version fields are
  2382. * swapped
  2383. */
  2384. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2385. ttdata->chip_rev =
  2386. get_unaligned_le16(&full_ver->chip_rev);
  2387. ttdata->chip_id =
  2388. get_unaligned_le16(&full_ver->chip_id);
  2389. } else {
  2390. ttdata->chip_rev =
  2391. get_unaligned_le16(&full_ver->chip_id);
  2392. ttdata->chip_id =
  2393. get_unaligned_le16(&full_ver->chip_rev);
  2394. }
  2395. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2396. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2397. sizeof(struct pt_pip2_version_full),
  2398. "PIP2 VERSION FULL");
  2399. } else {
  2400. ver = (struct pt_pip2_version *)
  2401. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2402. ttdata->pip_ver_major = ver->pip2_version_msb;
  2403. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2404. ttdata->bl_ver_major = ver->bl_version_msb;
  2405. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2406. ttdata->fw_ver_major = ver->fw_version_msb;
  2407. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2408. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2409. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2410. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2411. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2412. }
  2413. }
  2414. /*******************************************************************************
  2415. * FUNCTION: pt_si_get_ttdata
  2416. *
  2417. * SUMMARY: Function to load the version information from the system information
  2418. * PIP command into the core data struct.
  2419. *
  2420. * RETURN: n/a
  2421. *
  2422. * PARAMETERS:
  2423. * *cd - pointer to core data structure
  2424. ******************************************************************************/
  2425. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2426. {
  2427. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2428. struct pt_ttdata_dev *ttdata_dev =
  2429. (struct pt_ttdata_dev *)
  2430. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2431. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2432. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2433. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2434. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2435. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2436. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2437. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2438. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2439. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2440. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2441. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2442. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2443. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2444. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2445. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2446. }
  2447. /*******************************************************************************
  2448. * FUNCTION: pt_si_get_sensing_conf_data
  2449. *
  2450. * SUMMARY: Function to load the sensing information from the system information
  2451. * PIP command into the core data struct.
  2452. *
  2453. * RETURN: n/a
  2454. *
  2455. * PARAMETERS:
  2456. * *cd - pointer to core data structure
  2457. ******************************************************************************/
  2458. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2459. {
  2460. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2461. struct pt_sensing_conf_data_dev *scd_dev =
  2462. (struct pt_sensing_conf_data_dev *)
  2463. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2464. scd->electrodes_x = scd_dev->electrodes_x;
  2465. scd->electrodes_y = scd_dev->electrodes_y;
  2466. scd->origin_x = scd_dev->origin_x;
  2467. scd->origin_y = scd_dev->origin_y;
  2468. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2469. if (scd->origin_x & 0x02) {
  2470. scd->tx_num = scd->electrodes_x;
  2471. scd->rx_num = scd->electrodes_y;
  2472. } else {
  2473. scd->tx_num = scd->electrodes_y;
  2474. scd->rx_num = scd->electrodes_x;
  2475. }
  2476. /*
  2477. * When the Panel ID is coming from an XY pin and not a dedicated
  2478. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2479. * other DUTs as the loader will use cd->pid_for_loader to generate
  2480. * the bin file name but will ignore it if pid_for_loader is still
  2481. * set to PANEL_ID_NOT_ENABLED
  2482. */
  2483. if (cd->panel_id_support &
  2484. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2485. mutex_lock(&cd->system_lock);
  2486. cd->pid_for_loader = scd_dev->panel_id;
  2487. mutex_unlock(&cd->system_lock);
  2488. }
  2489. scd->panel_id = scd_dev->panel_id;
  2490. scd->btn = scd_dev->btn;
  2491. scd->scan_mode = scd_dev->scan_mode;
  2492. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2493. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2494. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2495. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2496. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2497. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2498. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2499. sizeof(struct pt_sensing_conf_data_dev),
  2500. "sensing_conf_data");
  2501. }
  2502. /*******************************************************************************
  2503. * FUNCTION: pt_si_setup
  2504. *
  2505. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2506. *
  2507. * RETURN:
  2508. * 0 = success
  2509. * !0 = failure
  2510. *
  2511. * PARAMETERS:
  2512. * *cd - pointer to core data structure
  2513. ******************************************************************************/
  2514. static int pt_si_setup(struct pt_core_data *cd)
  2515. {
  2516. struct pt_sysinfo *si = &cd->sysinfo;
  2517. int max_tch = si->sensing_conf_data.max_tch;
  2518. if (!si->xy_data)
  2519. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2520. GFP_KERNEL);
  2521. if (!si->xy_data)
  2522. return -ENOMEM;
  2523. if (!si->xy_mode)
  2524. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2525. if (!si->xy_mode) {
  2526. kfree(si->xy_data);
  2527. return -ENOMEM;
  2528. }
  2529. return 0;
  2530. }
  2531. /*******************************************************************************
  2532. * FUNCTION: pt_si_get_btn_data
  2533. *
  2534. * SUMMARY: Setup the core data button information based on the response of the
  2535. * System Information PIP command.
  2536. *
  2537. * RETURN:
  2538. * 0 = success
  2539. * !0 = failure
  2540. *
  2541. * PARAMETERS:
  2542. * *cd - pointer to core data structure
  2543. ******************************************************************************/
  2544. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2545. {
  2546. struct pt_sysinfo *si = &cd->sysinfo;
  2547. int num_btns = 0;
  2548. int num_defined_keys;
  2549. u16 *key_table;
  2550. int btn;
  2551. int i;
  2552. int rc = 0;
  2553. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2554. & PIP1_SYSINFO_BTN_MASK;
  2555. size_t btn_keys_size;
  2556. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2557. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2558. if (btns & (1 << i))
  2559. num_btns++;
  2560. }
  2561. si->num_btns = num_btns;
  2562. if (num_btns) {
  2563. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2564. if (!si->btn)
  2565. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2566. if (!si->btn)
  2567. return -ENOMEM;
  2568. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2569. num_defined_keys = 0;
  2570. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2571. num_defined_keys = 0;
  2572. else
  2573. num_defined_keys = cd->cpdata->sett
  2574. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2575. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2576. key_table = (u16 *)cd->cpdata->sett
  2577. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2578. si->btn[btn].key_code = key_table[btn];
  2579. si->btn[btn].enabled = true;
  2580. }
  2581. for (; btn < num_btns; btn++) {
  2582. si->btn[btn].key_code = KEY_RESERVED;
  2583. si->btn[btn].enabled = true;
  2584. }
  2585. return rc;
  2586. }
  2587. kfree(si->btn);
  2588. si->btn = NULL;
  2589. return rc;
  2590. }
  2591. /*******************************************************************************
  2592. * FUNCTION: pt_si_put_log_data
  2593. *
  2594. * SUMMARY: Prints all sys info data to kmsg log
  2595. *
  2596. * RETURN: n/a
  2597. *
  2598. * PARAMETERS:
  2599. * *cd - pointer to core data structure
  2600. ******************************************************************************/
  2601. static void pt_si_put_log_data(struct pt_core_data *cd)
  2602. {
  2603. struct pt_sysinfo *si = &cd->sysinfo;
  2604. struct pt_ttdata *ttdata = &si->ttdata;
  2605. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2606. int i;
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2608. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2610. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2611. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2612. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2614. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2616. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2618. __func__, ttdata->revctrl, ttdata->revctrl);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2620. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2622. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2624. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2626. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2628. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2629. for (i = 0; i < PT_NUM_MFGID; i++)
  2630. pt_debug(cd->dev, DL_DEBUG,
  2631. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2632. __func__, i, ttdata->mfg_id[i],
  2633. ttdata->mfg_id[i]);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2635. __func__, ttdata->post_code, ttdata->post_code);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2637. __func__, scd->electrodes_x, scd->electrodes_x);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2639. __func__, scd->electrodes_y, scd->electrodes_y);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2641. __func__, scd->len_x, scd->len_x);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2643. __func__, scd->len_y, scd->len_y);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2645. __func__, scd->res_x, scd->res_x);
  2646. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2647. __func__, scd->res_y, scd->res_y);
  2648. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2649. __func__, scd->max_z, scd->max_z);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2651. __func__, scd->origin_x, scd->origin_x);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2653. __func__, scd->origin_y, scd->origin_y);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2655. __func__, scd->panel_id, scd->panel_id);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2657. __func__, scd->btn, scd->btn);
  2658. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2659. __func__, scd->scan_mode, scd->scan_mode);
  2660. pt_debug(cd->dev, DL_DEBUG,
  2661. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2662. __func__, scd->max_tch, scd->max_tch);
  2663. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2664. __func__, si->xy_mode);
  2665. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2666. __func__, si->xy_data);
  2667. }
  2668. /*******************************************************************************
  2669. * FUNCTION: pt_get_sysinfo_regs
  2670. *
  2671. * SUMMARY: Setup all the core data System information based on the response
  2672. * of the System Information PIP command.
  2673. *
  2674. * RETURN:
  2675. * 0 = success
  2676. * !0 = failure
  2677. *
  2678. * PARAMETERS:
  2679. * *cd - pointer to core data structure
  2680. ******************************************************************************/
  2681. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2682. {
  2683. struct pt_sysinfo *si = &cd->sysinfo;
  2684. int rc;
  2685. rc = pt_si_get_btn_data(cd);
  2686. if (rc < 0)
  2687. return rc;
  2688. pt_si_get_ttdata(cd);
  2689. pt_si_get_sensing_conf_data(cd);
  2690. pt_si_setup(cd);
  2691. pt_si_put_log_data(cd);
  2692. si->ready = true;
  2693. return rc;
  2694. }
  2695. /*******************************************************************************
  2696. * FUNCTION: pt_free_si_ptrs
  2697. *
  2698. * SUMMARY: Frees all memory associated with the System Information within
  2699. * core data
  2700. *
  2701. * RETURN: n/a
  2702. *
  2703. * PARAMETERS:
  2704. * *cd - pointer to core data structure
  2705. ******************************************************************************/
  2706. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2707. {
  2708. struct pt_sysinfo *si = &cd->sysinfo;
  2709. kfree(si->btn);
  2710. kfree(si->xy_mode);
  2711. kfree(si->xy_data);
  2712. }
  2713. /*******************************************************************************
  2714. * FUNCTION: pt_hid_output_get_sysinfo_
  2715. *
  2716. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2717. * response.
  2718. *
  2719. * RETURN::
  2720. * 0 = success
  2721. * !0 = failure
  2722. *
  2723. * PARAMETERS:
  2724. * *cd - pointer to core data structure
  2725. ******************************************************************************/
  2726. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2727. {
  2728. int rc = 0;
  2729. struct pt_hid_output hid_output = {
  2730. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2731. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2732. };
  2733. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2734. if (rc)
  2735. return rc;
  2736. /* Parse the sysinfo data */
  2737. rc = pt_get_sysinfo_regs(cd);
  2738. if (rc)
  2739. pt_free_si_ptrs(cd);
  2740. return rc;
  2741. }
  2742. /*******************************************************************************
  2743. * FUNCTION: pt_hid_output_get_sysinfo
  2744. *
  2745. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2746. *
  2747. * RETURN::
  2748. * 0 = success
  2749. * !0 = failure
  2750. *
  2751. * PARAMETERS:
  2752. * *cd - pointer to core data structure
  2753. ******************************************************************************/
  2754. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2755. {
  2756. int rc;
  2757. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2758. if (rc < 0) {
  2759. pt_debug(cd->dev, DL_ERROR,
  2760. "%s: fail get exclusive ex=%p own=%p\n",
  2761. __func__, cd->exclusive_dev, cd->dev);
  2762. return rc;
  2763. }
  2764. rc = pt_hid_output_get_sysinfo_(cd);
  2765. if (release_exclusive(cd, cd->dev) < 0)
  2766. pt_debug(cd->dev, DL_ERROR,
  2767. "%s: fail to release exclusive\n", __func__);
  2768. return rc;
  2769. }
  2770. /*******************************************************************************
  2771. * FUNCTION: pt_pip_suspend_scanning_
  2772. *
  2773. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2774. *
  2775. * RETURN::
  2776. * 0 = success
  2777. * !0 = failure
  2778. *
  2779. * PARAMETERS:
  2780. * *cd - pointer to core data structure
  2781. ******************************************************************************/
  2782. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2783. {
  2784. int rc = 0;
  2785. struct pt_hid_output hid_output = {
  2786. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2787. };
  2788. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2789. if (rc) {
  2790. pt_debug(cd->dev, DL_ERROR,
  2791. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2792. __func__, rc);
  2793. }
  2794. return rc;
  2795. }
  2796. /*******************************************************************************
  2797. * FUNCTION: pt_pip_suspend_scanning
  2798. *
  2799. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2800. *
  2801. * RETURN::
  2802. * 0 = success
  2803. * !0 = failure
  2804. *
  2805. * PARAMETERS:
  2806. * *cd - pointer to core data structure
  2807. ******************************************************************************/
  2808. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2809. {
  2810. int rc;
  2811. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2812. if (rc < 0) {
  2813. pt_debug(cd->dev, DL_ERROR,
  2814. "%s: fail get exclusive ex=%p own=%p\n",
  2815. __func__, cd->exclusive_dev, cd->dev);
  2816. return rc;
  2817. }
  2818. rc = pt_pip_suspend_scanning_(cd);
  2819. if (release_exclusive(cd, cd->dev) < 0)
  2820. pt_debug(cd->dev, DL_ERROR,
  2821. "%s: fail to release exclusive\n", __func__);
  2822. return rc;
  2823. }
  2824. /*******************************************************************************
  2825. * FUNCTION: _pt_request_pip_suspend_scanning
  2826. *
  2827. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2828. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2829. *
  2830. * RETURN:
  2831. * 0 = success
  2832. * !0 = failure
  2833. *
  2834. * PARAMETERS:
  2835. * *dev - pointer to device structure
  2836. * protect - 0 = call non-protected function
  2837. * 1 = call protected function
  2838. ******************************************************************************/
  2839. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2840. int protect)
  2841. {
  2842. struct pt_core_data *cd = dev_get_drvdata(dev);
  2843. if (protect)
  2844. return pt_pip_suspend_scanning(cd);
  2845. return pt_pip_suspend_scanning_(cd);
  2846. }
  2847. /*******************************************************************************
  2848. * FUNCTION: pt_pip_resume_scanning_
  2849. *
  2850. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2851. *
  2852. * RETURN::
  2853. * 0 = success
  2854. * !0 = failure
  2855. *
  2856. * PARAMETERS:
  2857. * *cd - pointer to core data structure
  2858. ******************************************************************************/
  2859. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2860. {
  2861. int rc = 0;
  2862. struct pt_hid_output hid_output = {
  2863. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2864. };
  2865. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2866. if (rc) {
  2867. pt_debug(cd->dev, DL_ERROR,
  2868. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2869. __func__, rc);
  2870. }
  2871. return rc;
  2872. }
  2873. /*******************************************************************************
  2874. * FUNCTION: pt_pip_resume_scanning
  2875. *
  2876. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2877. *
  2878. * RETURN::
  2879. * 0 = success
  2880. * !0 = failure
  2881. *
  2882. * PARAMETERS:
  2883. * *cd - pointer to core data structure
  2884. ******************************************************************************/
  2885. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2886. {
  2887. int rc;
  2888. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2889. if (rc < 0) {
  2890. pt_debug(cd->dev, DL_ERROR,
  2891. "%s: fail get exclusive ex=%p own=%p\n",
  2892. __func__, cd->exclusive_dev, cd->dev);
  2893. return rc;
  2894. }
  2895. rc = pt_pip_resume_scanning_(cd);
  2896. if (release_exclusive(cd, cd->dev) < 0)
  2897. pt_debug(cd->dev, DL_ERROR,
  2898. "%s: fail to release exclusive\n", __func__);
  2899. return rc;
  2900. }
  2901. /*******************************************************************************
  2902. * FUNCTION: _pt_request_pip_resume_scanning
  2903. *
  2904. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2905. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2906. *
  2907. * RETURN::
  2908. * 0 = success
  2909. * !0 = failure
  2910. *
  2911. * PARAMETERS:
  2912. * *dev - pointer to device structure
  2913. * protect - 0 = call non-protected function
  2914. * 1 = call protected function
  2915. ******************************************************************************/
  2916. static int _pt_request_pip_resume_scanning(struct device *dev,
  2917. int protect)
  2918. {
  2919. struct pt_core_data *cd = dev_get_drvdata(dev);
  2920. if (protect)
  2921. return pt_pip_resume_scanning(cd);
  2922. return pt_pip_resume_scanning_(cd);
  2923. }
  2924. /*******************************************************************************
  2925. * FUNCTION: pt_pip_get_param_
  2926. *
  2927. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2928. * the 32bit parameter value
  2929. *
  2930. * RETURN:
  2931. * 0 = success
  2932. * !0 = failure
  2933. *
  2934. * PARAMETERS:
  2935. * *cd - pointer to core data
  2936. * param_id - parameter ID to retrieve
  2937. * *value - value of DUT parameter
  2938. ******************************************************************************/
  2939. static int pt_pip_get_param_(struct pt_core_data *cd,
  2940. u8 param_id, u32 *value)
  2941. {
  2942. int write_length = 1;
  2943. u8 param[1] = { param_id };
  2944. u8 read_param_id;
  2945. int param_size;
  2946. u8 *ptr;
  2947. int rc = 0;
  2948. int i;
  2949. struct pt_hid_output hid_output = {
  2950. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2951. .write_length = write_length,
  2952. .write_buf = param,
  2953. };
  2954. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2955. if (rc)
  2956. return rc;
  2957. read_param_id = cd->response_buf[5];
  2958. if (read_param_id != param_id)
  2959. return -EPROTO;
  2960. param_size = cd->response_buf[6];
  2961. ptr = &cd->response_buf[7];
  2962. *value = 0;
  2963. for (i = 0; i < param_size; i++)
  2964. *value += ptr[i] << (i * 8);
  2965. return 0;
  2966. }
  2967. /*******************************************************************************
  2968. * FUNCTION: pt_pip_get_param
  2969. *
  2970. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2971. *
  2972. * RETURN:
  2973. * 0 = success
  2974. * !0 = failure
  2975. *
  2976. * PARAMETERS:
  2977. * *cd - pointer to core data
  2978. * param_id - parameter ID to retrieve
  2979. * *value - value of DUT parameter
  2980. ******************************************************************************/
  2981. static int pt_pip_get_param(struct pt_core_data *cd,
  2982. u8 param_id, u32 *value)
  2983. {
  2984. int rc;
  2985. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2986. if (rc < 0) {
  2987. pt_debug(cd->dev, DL_ERROR,
  2988. "%s: fail get exclusive ex=%p own=%p\n",
  2989. __func__, cd->exclusive_dev, cd->dev);
  2990. return rc;
  2991. }
  2992. rc = pt_pip_get_param_(cd, param_id, value);
  2993. if (release_exclusive(cd, cd->dev) < 0)
  2994. pt_debug(cd->dev, DL_ERROR,
  2995. "%s: fail to release exclusive\n", __func__);
  2996. return rc;
  2997. }
  2998. /*******************************************************************************
  2999. * FUNCTION: _pt_request_pip_get_param
  3000. *
  3001. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3002. * calls to the protected or unprotected call to pt_pip_get_param
  3003. *
  3004. * RETURN:
  3005. * 0 = success
  3006. * !0 = failure
  3007. *
  3008. * PARAMETERS:
  3009. * *dev - pointer to device structure
  3010. * protect - flag to call protected or non protected function
  3011. * param_id - parameter ID to retrieve
  3012. * *value - value of DUT parameter
  3013. ******************************************************************************/
  3014. int _pt_request_pip_get_param(struct device *dev,
  3015. int protect, u8 param_id, u32 *value)
  3016. {
  3017. struct pt_core_data *cd = dev_get_drvdata(dev);
  3018. if (protect)
  3019. return pt_pip_get_param(cd, param_id, value);
  3020. return pt_pip_get_param_(cd, param_id, value);
  3021. }
  3022. /*******************************************************************************
  3023. * FUNCTION: pt_pip_set_param_
  3024. *
  3025. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3026. * passed in value to flash
  3027. *
  3028. * RETURN:
  3029. * 0 = success
  3030. * !0 = failure
  3031. *
  3032. * PARAMETERS:
  3033. * *cd - pointer to core data
  3034. * param_id - parameter ID to set
  3035. * value - value to write
  3036. * size - size to write
  3037. ******************************************************************************/
  3038. static int pt_pip_set_param_(struct pt_core_data *cd,
  3039. u8 param_id, u32 value, u8 size)
  3040. {
  3041. u8 write_buf[6];
  3042. u8 *ptr = &write_buf[2];
  3043. int rc = 0;
  3044. int i;
  3045. struct pt_hid_output hid_output = {
  3046. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3047. .write_buf = write_buf,
  3048. };
  3049. write_buf[0] = param_id;
  3050. write_buf[1] = size;
  3051. for (i = 0; i < size; i++) {
  3052. ptr[i] = value & 0xFF;
  3053. value = value >> 8;
  3054. }
  3055. hid_output.write_length = 2 + size;
  3056. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3057. if (rc)
  3058. return rc;
  3059. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3060. return -EPROTO;
  3061. return 0;
  3062. }
  3063. /*******************************************************************************
  3064. * FUNCTION: pt_pip_set_param
  3065. *
  3066. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3067. *
  3068. * RETURN:
  3069. * 0 = success
  3070. * !0 = failure
  3071. *
  3072. * PARAMETERS:
  3073. * *cd - pointer to core data
  3074. * param_id - parameter ID to set
  3075. * value - value to write
  3076. * size - size to write
  3077. ******************************************************************************/
  3078. static int pt_pip_set_param(struct pt_core_data *cd,
  3079. u8 param_id, u32 value, u8 size)
  3080. {
  3081. int rc;
  3082. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3083. if (rc < 0) {
  3084. pt_debug(cd->dev, DL_ERROR,
  3085. "%s: fail get exclusive ex=%p own=%p\n",
  3086. __func__, cd->exclusive_dev, cd->dev);
  3087. return rc;
  3088. }
  3089. rc = pt_pip_set_param_(cd, param_id, value, size);
  3090. if (release_exclusive(cd, cd->dev) < 0)
  3091. pt_debug(cd->dev, DL_ERROR,
  3092. "%s: fail to release exclusive\n", __func__);
  3093. return rc;
  3094. }
  3095. /*******************************************************************************
  3096. * FUNCTION: _pt_request_pip_set_param
  3097. *
  3098. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3099. * calls to the protected or unprotected call to pt_pip_set_param
  3100. *
  3101. * RETURN:
  3102. * 0 = success
  3103. * !0 = failure
  3104. *
  3105. * PARAMETERS:
  3106. * *dev - pointer to device structure
  3107. * protect - flag to call protected or non-protected
  3108. * param_id - parameter ID to set
  3109. * value - value to write
  3110. * size - size to write
  3111. ******************************************************************************/
  3112. int _pt_request_pip_set_param(struct device *dev, int protect,
  3113. u8 param_id, u32 value, u8 size)
  3114. {
  3115. struct pt_core_data *cd = dev_get_drvdata(dev);
  3116. if (protect)
  3117. return pt_pip_set_param(cd, param_id, value, size);
  3118. return pt_pip_set_param_(cd, param_id, value, size);
  3119. }
  3120. /*******************************************************************************
  3121. * FUNCTION: _pt_pip_enter_easywake_state_
  3122. *
  3123. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3124. *
  3125. * RETURN:
  3126. * 0 = success
  3127. * !0 = failure
  3128. *
  3129. * PARAMETERS:
  3130. * *dev - pointer to device structure
  3131. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3132. * *return_data - return status if easywake was entered
  3133. ******************************************************************************/
  3134. static int pt_hid_output_enter_easywake_state_(
  3135. struct pt_core_data *cd, u8 data, u8 *return_data)
  3136. {
  3137. int write_length = 1;
  3138. u8 param[1] = { data };
  3139. int rc = 0;
  3140. struct pt_hid_output hid_output = {
  3141. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3142. .write_length = write_length,
  3143. .write_buf = param,
  3144. };
  3145. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3146. if (rc)
  3147. return rc;
  3148. *return_data = cd->response_buf[5];
  3149. return rc;
  3150. }
  3151. /*******************************************************************************
  3152. * FUNCTION: pt_pip_verify_config_block_crc_
  3153. *
  3154. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3155. *
  3156. * RETURN:
  3157. * 0 = success
  3158. * !0 = failure
  3159. *
  3160. * PARAMETERS:
  3161. * *cd - pointer the core data structure
  3162. * ebid - enumerated block ID
  3163. * *status - PIP command status
  3164. * calculated_crc - calculated CRC
  3165. * stored_crc - stored CRC in config area
  3166. ******************************************************************************/
  3167. static int pt_pip_verify_config_block_crc_(
  3168. struct pt_core_data *cd, u8 ebid, u8 *status,
  3169. u16 *calculated_crc, u16 *stored_crc)
  3170. {
  3171. int write_length = 1;
  3172. u8 param[1] = { ebid };
  3173. u8 *ptr;
  3174. int rc = 0;
  3175. struct pt_hid_output hid_output = {
  3176. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3177. .write_length = write_length,
  3178. .write_buf = param,
  3179. };
  3180. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3181. if (rc)
  3182. return rc;
  3183. ptr = &cd->response_buf[5];
  3184. *status = ptr[0];
  3185. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3186. *stored_crc = get_unaligned_le16(&ptr[3]);
  3187. return 0;
  3188. }
  3189. /*******************************************************************************
  3190. * FUNCTION: pt_pip_verify_config_block_crc
  3191. *
  3192. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3193. * an exclusive access to the DUT.
  3194. *
  3195. * RETURN:
  3196. * 0 = success
  3197. * !0 = failure
  3198. *
  3199. * PARAMETERS:
  3200. * *cd - pointer the core data structure
  3201. * ebid - enumerated block ID
  3202. * *status - PIP command status
  3203. * calculated_crc - calculated CRC
  3204. * stored_crc - stored CRC in config area
  3205. ******************************************************************************/
  3206. static int pt_pip_verify_config_block_crc(
  3207. struct pt_core_data *cd, u8 ebid, u8 *status,
  3208. u16 *calculated_crc, u16 *stored_crc)
  3209. {
  3210. int rc;
  3211. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3212. if (rc < 0) {
  3213. pt_debug(cd->dev, DL_ERROR,
  3214. "%s: fail get exclusive ex=%p own=%p\n",
  3215. __func__, cd->exclusive_dev, cd->dev);
  3216. return rc;
  3217. }
  3218. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3219. calculated_crc, stored_crc);
  3220. if (release_exclusive(cd, cd->dev) < 0)
  3221. pt_debug(cd->dev, DL_ERROR,
  3222. "%s: fail to release exclusive\n", __func__);
  3223. return rc;
  3224. }
  3225. /*******************************************************************************
  3226. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3227. *
  3228. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3229. * calls to the protected or unprotected call to
  3230. * pt_pip_verify_config_block_crc_
  3231. *
  3232. * RETURN:
  3233. * 0 = success
  3234. * !0 = failure
  3235. *
  3236. * PARAMETERS:
  3237. * *dev - pointer to device structure
  3238. * protect - flag to call protected or non-protected
  3239. * ebid - enumerated block ID
  3240. * *status - PIP command status
  3241. * calculated_crc - calculated CRC
  3242. * stored_crc - stored CRC in config area
  3243. ******************************************************************************/
  3244. static int _pt_request_pip_verify_config_block_crc(
  3245. struct device *dev, int protect, u8 ebid, u8 *status,
  3246. u16 *calculated_crc, u16 *stored_crc)
  3247. {
  3248. struct pt_core_data *cd = dev_get_drvdata(dev);
  3249. if (protect)
  3250. return pt_pip_verify_config_block_crc(cd, ebid,
  3251. status, calculated_crc, stored_crc);
  3252. return pt_pip_verify_config_block_crc_(cd, ebid,
  3253. status, calculated_crc, stored_crc);
  3254. }
  3255. /*******************************************************************************
  3256. * FUNCTION: pt_pip_get_config_row_size_
  3257. *
  3258. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3259. *
  3260. * RETURN:
  3261. * 0 = success
  3262. * !0 = failure
  3263. *
  3264. * PARAMETERS:
  3265. * *cd - pointer to core data
  3266. * protect - flag to call protected or non-protected
  3267. * *row_size - pointer to store the retrieved row size
  3268. ******************************************************************************/
  3269. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3270. u16 *row_size)
  3271. {
  3272. int rc = 0;
  3273. struct pt_hid_output hid_output = {
  3274. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3275. };
  3276. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3277. if (rc)
  3278. return rc;
  3279. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3280. return 0;
  3281. }
  3282. /*******************************************************************************
  3283. * FUNCTION: pt_pip_get_config_row_size
  3284. *
  3285. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3286. * an exclusive access to the DUT.
  3287. *
  3288. * RETURN:
  3289. * 0 = success
  3290. * !0 = failure
  3291. *
  3292. * PARAMETERS:
  3293. * *cd - pointer to core data
  3294. * protect - flag to call protected or non-protected
  3295. * *row_size - pointer to store the retrieved row size
  3296. ******************************************************************************/
  3297. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3298. u16 *row_size)
  3299. {
  3300. int rc;
  3301. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3302. if (rc < 0) {
  3303. pt_debug(cd->dev, DL_ERROR,
  3304. "%s: fail get exclusive ex=%p own=%p\n",
  3305. __func__, cd->exclusive_dev, cd->dev);
  3306. return rc;
  3307. }
  3308. rc = pt_pip_get_config_row_size_(cd, row_size);
  3309. if (release_exclusive(cd, cd->dev) < 0)
  3310. pt_debug(cd->dev, DL_ERROR,
  3311. "%s: fail to release exclusive\n", __func__);
  3312. return rc;
  3313. }
  3314. /*******************************************************************************
  3315. * FUNCTION: _pt_request_pip_get_config_row_size
  3316. *
  3317. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3318. * calls to the protected or unprotected call to
  3319. * pt_pip_get_config_row_size_
  3320. *
  3321. * RETURN:
  3322. * 0 = success
  3323. * !0 = failure
  3324. *
  3325. * PARAMETERS:
  3326. * *dev - pointer to device structure
  3327. * protect - flag to call protected or non-protected
  3328. * *row_size - pointer to store the retrieved row size
  3329. ******************************************************************************/
  3330. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3331. int protect, u16 *row_size)
  3332. {
  3333. struct pt_core_data *cd = dev_get_drvdata(dev);
  3334. if (protect)
  3335. return pt_pip_get_config_row_size(cd, row_size);
  3336. return pt_pip_get_config_row_size_(cd, row_size);
  3337. }
  3338. /*******************************************************************************
  3339. * FUNCTION: pt_pip1_read_data_block_
  3340. *
  3341. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3342. * output data to the "read_buf" and update "crc".
  3343. *
  3344. * RETURN:
  3345. * 0 = success
  3346. * !0 = failure
  3347. *
  3348. * PARAMETERS:
  3349. * *cd - pointer to core data
  3350. * row_number - row number
  3351. * length - length of data to read
  3352. * ebid - block id
  3353. * *actual_read_len - Actual data length read
  3354. * *read_buf - pointer to the buffer to store read data
  3355. * read_buf_size - size of read_buf
  3356. * *crc - pointer to store CRC of row data
  3357. ******************************************************************************/
  3358. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3359. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3360. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3361. {
  3362. int read_ebid;
  3363. int status;
  3364. int rc = 0;
  3365. int write_length = 5;
  3366. u8 write_buf[5];
  3367. u8 cmd_offset = 0;
  3368. u16 calc_crc;
  3369. struct pt_hid_output hid_output = {
  3370. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3371. .write_length = write_length,
  3372. .write_buf = write_buf,
  3373. };
  3374. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3375. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3376. write_buf[cmd_offset++] = LOW_BYTE(length);
  3377. write_buf[cmd_offset++] = HI_BYTE(length);
  3378. write_buf[cmd_offset++] = ebid;
  3379. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3380. if (rc)
  3381. return rc;
  3382. status = cd->response_buf[5];
  3383. if (status)
  3384. return status;
  3385. read_ebid = cd->response_buf[6];
  3386. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3387. return -EPROTO;
  3388. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3389. if (length == 0 || *actual_read_len == 0)
  3390. return 0;
  3391. if (read_buf_size >= *actual_read_len &&
  3392. *actual_read_len < PT_MAX_PIP2_MSG_SIZE)
  3393. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3394. else
  3395. return -EPROTO;
  3396. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3397. /* Validate Row Data CRC */
  3398. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3399. if (*crc == calc_crc) {
  3400. return 0;
  3401. } else {
  3402. pt_debug(cd->dev, DL_ERROR,
  3403. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3404. __func__, *crc, calc_crc);
  3405. return -EPROTO;
  3406. }
  3407. }
  3408. /*******************************************************************************
  3409. * FUNCTION: _pt_request_pip_read_data_block
  3410. *
  3411. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3412. * calls to pt_pip1_read_data_block_
  3413. *
  3414. * RETURN:
  3415. * 0 = success
  3416. * !0 = failure
  3417. *
  3418. * PARAMETERS:
  3419. * *dev - pointer to device structure
  3420. * row_number - row number
  3421. * length - length of data to read
  3422. * ebid - block id
  3423. * *actual_read_len - Actual data length read
  3424. * *read_buf - pointer to the buffer to store read data
  3425. * *crc - pointer to store CRC of row data
  3426. ******************************************************************************/
  3427. static int _pt_request_pip_read_data_block(struct device *dev,
  3428. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3429. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3430. {
  3431. struct pt_core_data *cd = dev_get_drvdata(dev);
  3432. return pt_pip1_read_data_block_(cd, row_number, length,
  3433. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3434. }
  3435. /*******************************************************************************
  3436. * FUNCTION: pt_pip1_write_data_block_
  3437. *
  3438. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3439. * write data to the data block.
  3440. *
  3441. * RETURN:
  3442. * 0 = success
  3443. * !0 = failure
  3444. *
  3445. * PARAMETERS:
  3446. * *cd - pointer to core data
  3447. * row_number - row in config block to write to
  3448. * write_length - length of data to write
  3449. * ebid - enumerated block ID
  3450. * *write_buf - pointer to buffer to write
  3451. * *security_key - pointer to security key to allow write
  3452. * *actual_write_len - pointer to store data length actually written
  3453. ******************************************************************************/
  3454. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3455. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3456. u8 *security_key, u16 *actual_write_len)
  3457. {
  3458. /* row_number + write_len + ebid + security_key + crc */
  3459. u16 full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3460. u8 *full_write_buf;
  3461. u8 cmd_offset = 0;
  3462. u16 crc;
  3463. int status;
  3464. int rc = 0;
  3465. int read_ebid;
  3466. u8 *data;
  3467. struct pt_hid_output hid_output = {
  3468. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3469. .write_length = full_write_length,
  3470. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3471. };
  3472. if (write_length > PT_CAL_DATA_ROW_SIZE)
  3473. return -EINVAL;
  3474. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3475. if (!full_write_buf)
  3476. return -ENOMEM;
  3477. hid_output.write_buf = full_write_buf;
  3478. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3479. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3480. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3481. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3482. full_write_buf[cmd_offset++] = ebid;
  3483. data = &full_write_buf[cmd_offset];
  3484. memcpy(data, write_buf, write_length);
  3485. cmd_offset += write_length;
  3486. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3487. cmd_offset += 8;
  3488. crc = _pt_compute_crc(data, write_length);
  3489. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3490. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3491. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3492. if (rc)
  3493. goto exit;
  3494. status = cd->response_buf[5];
  3495. if (status) {
  3496. rc = -EINVAL;
  3497. goto exit;
  3498. }
  3499. read_ebid = cd->response_buf[6];
  3500. if (read_ebid != ebid) {
  3501. rc = -EPROTO;
  3502. goto exit;
  3503. }
  3504. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3505. exit:
  3506. kfree(full_write_buf);
  3507. return rc;
  3508. }
  3509. /*******************************************************************************
  3510. * FUNCTION: _pt_request_pip_write_data_block
  3511. *
  3512. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3513. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3514. *
  3515. * RETURN:
  3516. * 0 = success
  3517. * !0 = failure
  3518. *
  3519. * PARAMETERS:
  3520. * *dev - pointer to device structure
  3521. * protect - flag to call protected or non-protected
  3522. * row_number - row in config block to write to
  3523. * write_length - length of data to write
  3524. * ebid - enumerated block ID
  3525. * *write_buf - pointer to buffer to write
  3526. * *security_key - pointer to security key to allow write
  3527. * *actual_write_len - pointer to store data length actually written
  3528. ******************************************************************************/
  3529. static int _pt_request_pip_write_data_block(struct device *dev,
  3530. u16 row_number, u16 write_length, u8 ebid,
  3531. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3532. {
  3533. struct pt_core_data *cd = dev_get_drvdata(dev);
  3534. return pt_pip1_write_data_block_(cd, row_number,
  3535. write_length, ebid, write_buf, security_key,
  3536. actual_write_len);
  3537. }
  3538. /*******************************************************************************
  3539. * FUNCTION: pt_pip_get_data_structure_
  3540. *
  3541. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3542. * returning a structure of data defined by data_id
  3543. *
  3544. * RETURN:
  3545. * 0 = success
  3546. * !0 = failure
  3547. *
  3548. * PARAMETERS:
  3549. * *cd - pointer to core data
  3550. * read_offset - read pointer offset
  3551. * read_length - length of data to read
  3552. * data_id - data ID to read
  3553. * *status - pointer to store the read response status
  3554. * *data_format - pointer to store format of data read
  3555. * *actual_read_len - pointer to store data length actually read
  3556. * *data - pointer to store data read
  3557. ******************************************************************************/
  3558. static int pt_pip_get_data_structure_(
  3559. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3560. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3561. u8 *data)
  3562. {
  3563. int rc = 0;
  3564. u16 total_read_len = 0;
  3565. u16 read_len;
  3566. u16 off_buf = 0;
  3567. u8 write_buf[5];
  3568. u8 read_data_id;
  3569. struct pt_hid_output hid_output = {
  3570. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3571. .write_length = 5,
  3572. .write_buf = write_buf,
  3573. };
  3574. again:
  3575. write_buf[0] = LOW_BYTE(read_offset);
  3576. write_buf[1] = HI_BYTE(read_offset);
  3577. write_buf[2] = LOW_BYTE(read_length);
  3578. write_buf[3] = HI_BYTE(read_length);
  3579. write_buf[4] = data_id;
  3580. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3581. if (rc)
  3582. return rc;
  3583. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3584. goto set_status;
  3585. read_data_id = cd->response_buf[6];
  3586. if (read_data_id != data_id)
  3587. return -EPROTO;
  3588. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3589. if (read_len && data) {
  3590. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3591. total_read_len += read_len;
  3592. if (read_len < read_length) {
  3593. read_offset += read_len;
  3594. off_buf += read_len;
  3595. read_length -= read_len;
  3596. goto again;
  3597. }
  3598. }
  3599. if (data_format)
  3600. *data_format = cd->response_buf[9];
  3601. if (actual_read_len)
  3602. *actual_read_len = total_read_len;
  3603. set_status:
  3604. if (status)
  3605. *status = cd->response_buf[5];
  3606. return rc;
  3607. }
  3608. /*******************************************************************************
  3609. * FUNCTION: pt_pip_get_data_structure
  3610. *
  3611. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3612. * an exclusive access to the DUT.
  3613. *
  3614. * RETURN:
  3615. * 0 = success
  3616. * !0 = failure
  3617. *
  3618. * PARAMETERS:
  3619. * *cd - pointer to core data
  3620. * read_offset - read pointer offset
  3621. * read_length - length of data to read
  3622. * data_id - data ID to read
  3623. * *status - pointer to store the read response status
  3624. * *data_format - pointer to store format of data read
  3625. * *actual_read_len - pointer to store data length actually read
  3626. * *data - pointer to store data read
  3627. ******************************************************************************/
  3628. static int pt_pip_get_data_structure(
  3629. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3630. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3631. u8 *data)
  3632. {
  3633. int rc;
  3634. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3635. if (rc < 0) {
  3636. pt_debug(cd->dev, DL_ERROR,
  3637. "%s: fail get exclusive ex=%p own=%p\n",
  3638. __func__, cd->exclusive_dev, cd->dev);
  3639. return rc;
  3640. }
  3641. rc = pt_pip_get_data_structure_(cd, read_offset,
  3642. read_length, data_id, status, data_format,
  3643. actual_read_len, data);
  3644. if (release_exclusive(cd, cd->dev) < 0)
  3645. pt_debug(cd->dev, DL_ERROR,
  3646. "%s: fail to release exclusive\n", __func__);
  3647. return rc;
  3648. }
  3649. /*******************************************************************************
  3650. * FUNCTION: _pt_request_pip_get_data_structure
  3651. *
  3652. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3653. * calls to the protected or unprotected call to
  3654. * pt_pip_get_data_structure
  3655. *
  3656. * RETURN:
  3657. * 0 = success
  3658. * !0 = failure
  3659. *
  3660. * PARAMETERS:
  3661. * *dev - pointer to device structure
  3662. * protect - flag to call protected or non-protected
  3663. * read_offset - read pointer offset
  3664. * read_length - length of data to read
  3665. * data_id - data ID to read
  3666. * *status - pointer to store the read response status
  3667. * *data_format - pointer to store format of data read
  3668. * *actual_read_len - pointer to store data length actually read
  3669. * *data - pointer to store data read
  3670. ******************************************************************************/
  3671. static int _pt_request_pip_get_data_structure(struct device *dev,
  3672. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3673. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3674. {
  3675. struct pt_core_data *cd = dev_get_drvdata(dev);
  3676. if (protect)
  3677. return pt_pip_get_data_structure(cd,
  3678. read_offset, read_length, data_id, status,
  3679. data_format, actual_read_len, data);
  3680. return pt_pip_get_data_structure_(cd,
  3681. read_offset, read_length, data_id, status,
  3682. data_format, actual_read_len, data);
  3683. }
  3684. /*******************************************************************************
  3685. * FUNCTION: _pt_manage_local_cal_data
  3686. *
  3687. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3688. * CALIBRATION data. It stores it in a local static array and can be
  3689. * cleared, loaded or used to restore the CAL data back to the running FW.
  3690. * The CAL data is read or restored by use of the PIP1 commands:
  3691. * - READ_DATA_BLOCK (0x22)
  3692. * - WRITE_DATA_BLOCK (0x23)
  3693. *
  3694. * RETURN:
  3695. * 0 = success
  3696. * !0 = failure
  3697. *
  3698. * PARAMETERS:
  3699. * *dev - pointer to device structure
  3700. * action - One of the following actions:
  3701. * - PT_CAL_DATA_SAVE
  3702. * - PT_CAL_DATA_RESTORE
  3703. * - PT_CAL_DATA_CLEAR
  3704. * - PT_CAL_DATA_SIZE
  3705. * *size - pointer to the number of bytes transferred
  3706. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3707. ******************************************************************************/
  3708. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3709. unsigned short *crc)
  3710. {
  3711. struct pt_core_data *cd = dev_get_drvdata(dev);
  3712. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3713. unsigned short calc_id_crc = 0;
  3714. static u8 *cal_cache_data;
  3715. static u16 cal_cache_len;
  3716. static unsigned short cal_cache_chip_id;
  3717. int rc = 0;
  3718. u8 *tmp_data = NULL;
  3719. u8 row_number = 0;
  3720. u8 prefix[20];
  3721. u16 cal_size = 0;
  3722. u16 transfer_size;
  3723. u16 act_trans_len = 0;
  3724. u16 byte_offset = 0;
  3725. u16 cal_blk_size;
  3726. u16 total_rows;
  3727. u16 remain_bytes;
  3728. u16 data_block_crc;
  3729. u16 buf_size = 12;
  3730. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3731. __func__, action);
  3732. switch (action) {
  3733. case PT_CAL_DATA_SAVE:
  3734. /* Read the size of the CAL block and calculate # rows */
  3735. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3736. if (!tmp_data) {
  3737. rc = -ENOMEM;
  3738. goto exit;
  3739. }
  3740. /*
  3741. * Don't check rc as doing a read size will give a false
  3742. * error on the CRC check.
  3743. */
  3744. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3745. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3746. cal_blk_size = act_trans_len;
  3747. kfree(tmp_data);
  3748. pt_debug(dev, DL_INFO,
  3749. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3750. __func__, cal_cache_len, cal_blk_size);
  3751. /* Safety net to ensure we didn't read incorrect size */
  3752. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3753. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3754. __func__);
  3755. rc = 1;
  3756. goto exit;
  3757. }
  3758. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3759. if (cal_blk_size != cal_cache_len) {
  3760. kfree(cal_cache_data);
  3761. cal_cache_data = kzalloc(cal_blk_size + 2,
  3762. GFP_KERNEL);
  3763. if (!cal_cache_data) {
  3764. rc = -ENOMEM;
  3765. goto exit;
  3766. }
  3767. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3768. __func__);
  3769. }
  3770. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3771. /* Calculate how many rows [0-n] (PIP Transactions) */
  3772. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3773. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3774. /* Add row if we have a last partial row */
  3775. if (remain_bytes > 0)
  3776. total_rows++;
  3777. pt_debug(dev, DL_INFO,
  3778. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3779. __func__, cal_blk_size, total_rows, remain_bytes);
  3780. /* Read all rows unless an error occurs */
  3781. rc = 0;
  3782. while (rc == 0 && row_number <= total_rows) {
  3783. act_trans_len = 0;
  3784. if (remain_bytes > 0 && row_number == total_rows)
  3785. transfer_size = remain_bytes;
  3786. else
  3787. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3788. rc = pt_pip1_read_data_block_(cd, row_number,
  3789. transfer_size, PT_CAL_EBID,
  3790. &act_trans_len,
  3791. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3792. &data_block_crc);
  3793. if (rc) {
  3794. /* Error occurred, exit loop */
  3795. cal_size = 0;
  3796. break;
  3797. }
  3798. pt_debug(dev, DL_INFO,
  3799. "%s: CAL read rc=%d actual read len=%d\n",
  3800. __func__, rc, act_trans_len);
  3801. byte_offset += act_trans_len;
  3802. cal_size = byte_offset;
  3803. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3804. pt_pr_buf(dev, DL_INFO,
  3805. &cal_cache_data[byte_offset - act_trans_len],
  3806. act_trans_len, prefix);
  3807. row_number++;
  3808. }
  3809. if (cal_size > 0) {
  3810. /* Save a CRC of the chip info the CAL was saved from */
  3811. calc_id_crc = crc_ccitt_calculate(
  3812. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3813. cal_cache_chip_id = calc_id_crc;
  3814. cal_cache_len = cal_size;
  3815. pt_debug(dev, DL_INFO,
  3816. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3817. __func__, calc_id_crc, cal_size);
  3818. }
  3819. *size = cal_size;
  3820. *crc = calc_id_crc;
  3821. break;
  3822. case PT_CAL_DATA_RESTORE:
  3823. cal_size = cal_cache_len;
  3824. while ((rc == 0) && (byte_offset < cal_size)) {
  3825. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3826. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3827. else
  3828. transfer_size = cal_size - byte_offset;
  3829. rc = pt_pip1_write_data_block_(cd, row_number,
  3830. transfer_size, PT_CAL_EBID,
  3831. &cal_cache_data[byte_offset],
  3832. (u8 *)pt_data_block_security_key,
  3833. &act_trans_len);
  3834. byte_offset += act_trans_len;
  3835. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3836. __func__, byte_offset);
  3837. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3838. pt_pr_buf(dev, DL_INFO,
  3839. &cal_cache_data[byte_offset - act_trans_len],
  3840. act_trans_len, prefix);
  3841. if ((byte_offset > cal_size) ||
  3842. (act_trans_len != transfer_size))
  3843. rc = -EIO;
  3844. row_number++;
  3845. }
  3846. *size = byte_offset;
  3847. *crc = cal_cache_chip_id;
  3848. break;
  3849. case PT_CAL_DATA_CLEAR:
  3850. if (cal_cache_data)
  3851. memset(&cal_cache_data[0], 0, cal_cache_len);
  3852. cal_cache_len = 0;
  3853. cal_cache_chip_id = 0;
  3854. *size = 0;
  3855. *crc = 0;
  3856. break;
  3857. case PT_CAL_DATA_INFO:
  3858. default:
  3859. *size = cal_cache_len;
  3860. *crc = cal_cache_chip_id;
  3861. pt_debug(dev, DL_INFO,
  3862. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3863. __func__, cal_cache_chip_id,
  3864. cal_cache_len);
  3865. break;
  3866. }
  3867. exit:
  3868. pt_debug(dev, DL_INFO,
  3869. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3870. __func__, rc, *crc, *size);
  3871. return rc;
  3872. }
  3873. /*******************************************************************************
  3874. * FUNCTION: pt_pip_run_selftest_
  3875. *
  3876. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3877. * to execute a FW built in self test
  3878. *
  3879. * RETURN:
  3880. * 0 = success
  3881. * !0 = failure
  3882. *
  3883. * PARAMETERS:
  3884. * *cd - pointer to core data
  3885. * test_id - enumerated test ID to run
  3886. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3887. * *status - pointer to store the read response status
  3888. * *summary_results - pointer to store the results summary
  3889. * *results_available - pointer to store if results are available
  3890. *****************************************************************************/
  3891. static int pt_pip_run_selftest_(
  3892. struct pt_core_data *cd, u8 test_id,
  3893. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3894. u8 *results_available)
  3895. {
  3896. int rc = 0;
  3897. u8 write_buf[2];
  3898. struct pt_hid_output hid_output = {
  3899. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3900. .write_length = 2,
  3901. .write_buf = write_buf,
  3902. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3903. };
  3904. write_buf[0] = test_id;
  3905. write_buf[1] = write_idacs_to_flash;
  3906. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3907. hid_output.write_length = 1;
  3908. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3909. if (rc)
  3910. return rc;
  3911. if (status)
  3912. *status = cd->response_buf[5];
  3913. if (summary_result)
  3914. *summary_result = cd->response_buf[6];
  3915. /* results_available only available before PIP 1.03 */
  3916. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3917. if (results_available)
  3918. *results_available = cd->response_buf[7];
  3919. }
  3920. return rc;
  3921. }
  3922. /*******************************************************************************
  3923. * FUNCTION: pt_pip_run_selftest
  3924. *
  3925. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3926. * an exclusive access to the DUT.
  3927. *
  3928. * RETURN:
  3929. * 0 = success
  3930. * !0 = failure
  3931. *
  3932. * PARAMETERS:
  3933. * *cd - pointer to core data
  3934. * test_id - enumerated test ID to run
  3935. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3936. * *status - pointer to store the read response status
  3937. * *summary_results - pointer to store the results summary
  3938. * *results_available - pointer to store if results are available
  3939. ******************************************************************************/
  3940. static int pt_pip_run_selftest(
  3941. struct pt_core_data *cd, u8 test_id,
  3942. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3943. u8 *results_available)
  3944. {
  3945. int rc;
  3946. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3947. if (rc < 0) {
  3948. pt_debug(cd->dev, DL_ERROR,
  3949. "%s: fail get exclusive ex=%p own=%p\n",
  3950. __func__, cd->exclusive_dev, cd->dev);
  3951. return rc;
  3952. }
  3953. rc = pt_pip_run_selftest_(cd, test_id,
  3954. write_idacs_to_flash, status, summary_result,
  3955. results_available);
  3956. if (release_exclusive(cd, cd->dev) < 0)
  3957. pt_debug(cd->dev, DL_ERROR,
  3958. "%s: fail to release exclusive\n", __func__);
  3959. return rc;
  3960. }
  3961. /*******************************************************************************
  3962. * FUNCTION: _pt_request_pip_run_selftest
  3963. *
  3964. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3965. * calls to the protected or unprotected call to pt_pip_run_selftest
  3966. *
  3967. * RETURN:
  3968. * 0 = success
  3969. * !0 = failure
  3970. *
  3971. * PARAMETERS:
  3972. * *dev - pointer to device structure
  3973. * protect - flag to call protected or non-protected
  3974. * test_id - enumerated test ID to run
  3975. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3976. * *status - pointer to store the read response status
  3977. * *summary_results - pointer to store the results summary
  3978. * *results_available - pointer to store if results are available
  3979. ******************************************************************************/
  3980. static int _pt_request_pip_run_selftest(struct device *dev,
  3981. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3982. u8 *summary_result, u8 *results_available)
  3983. {
  3984. struct pt_core_data *cd = dev_get_drvdata(dev);
  3985. if (protect)
  3986. return pt_pip_run_selftest(cd, test_id,
  3987. write_idacs_to_flash, status, summary_result,
  3988. results_available);
  3989. return pt_pip_run_selftest_(cd, test_id,
  3990. write_idacs_to_flash, status, summary_result,
  3991. results_available);
  3992. }
  3993. /*******************************************************************************
  3994. * FUNCTION: _pt_pip_get_selftest_result_
  3995. *
  3996. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3997. * to retrieve the self test results from the self test already executed
  3998. *
  3999. * RETURN:
  4000. * 0 = success
  4001. * !0 = failure
  4002. *
  4003. * PARAMETERS:
  4004. * *cd - pointer to core data
  4005. * read_offset - read pointer offset
  4006. * read_length - length of data to read
  4007. * test_id - enumerated test ID to read selftest results from
  4008. * *status - pointer to store the read response status
  4009. * *actual_read_len - pointer to store data length actually read
  4010. * *status - pointer to where the cmd response statas is stored
  4011. ******************************************************************************/
  4012. static int pt_pip_get_selftest_result_(
  4013. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4014. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4015. {
  4016. int rc = 0;
  4017. u16 total_read_len = 0;
  4018. u16 read_len;
  4019. u16 off_buf = 0;
  4020. u8 write_buf[5];
  4021. u8 read_test_id;
  4022. bool repeat;
  4023. struct pt_hid_output hid_output = {
  4024. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4025. .write_length = 5,
  4026. .write_buf = write_buf,
  4027. };
  4028. /*
  4029. * Do not repeat reading for Auto Shorts test
  4030. * when PIP version < 1.3
  4031. */
  4032. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4033. || test_id != PT_ST_ID_AUTOSHORTS;
  4034. again:
  4035. write_buf[0] = LOW_BYTE(read_offset);
  4036. write_buf[1] = HI_BYTE(read_offset);
  4037. write_buf[2] = LOW_BYTE(read_length);
  4038. write_buf[3] = HI_BYTE(read_length);
  4039. write_buf[4] = test_id;
  4040. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4041. if (rc)
  4042. return rc;
  4043. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4044. goto set_status;
  4045. read_test_id = cd->response_buf[6];
  4046. if (read_test_id != test_id)
  4047. return -EPROTO;
  4048. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4049. if (read_len && data) {
  4050. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4051. total_read_len += read_len;
  4052. if (repeat && read_len < read_length) {
  4053. read_offset += read_len;
  4054. off_buf += read_len;
  4055. read_length -= read_len;
  4056. goto again;
  4057. }
  4058. }
  4059. if (actual_read_len)
  4060. *actual_read_len = total_read_len;
  4061. set_status:
  4062. if (status)
  4063. *status = cd->response_buf[5];
  4064. return rc;
  4065. }
  4066. /*******************************************************************************
  4067. * FUNCTION: _pt_pip_get_selftest_result
  4068. *
  4069. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4070. * access to the DUT
  4071. *
  4072. * RETURN:
  4073. * 0 = success
  4074. * !0 = failure
  4075. *
  4076. * PARAMETERS:
  4077. * *cd - pointer to core data
  4078. * read_offset - read pointer offset
  4079. * read_length - length of data to read
  4080. * test_id - enumerated test ID to read selftest results from
  4081. * *status - pointer to store the read response status
  4082. * *actual_read_len - pointer to store data length actually read
  4083. * *status - pointer to where the cmd response statas is stored
  4084. ******************************************************************************/
  4085. static int pt_pip_get_selftest_result(
  4086. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4087. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4088. {
  4089. int rc;
  4090. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4091. if (rc < 0) {
  4092. pt_debug(cd->dev, DL_ERROR,
  4093. "%s: fail get exclusive ex=%p own=%p\n",
  4094. __func__, cd->exclusive_dev, cd->dev);
  4095. return rc;
  4096. }
  4097. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4098. read_length, test_id, status, actual_read_len, data);
  4099. if (release_exclusive(cd, cd->dev) < 0)
  4100. pt_debug(cd->dev, DL_ERROR,
  4101. "%s: fail to release exclusive\n", __func__);
  4102. return rc;
  4103. }
  4104. /*******************************************************************************
  4105. * FUNCTION: _pt_request_pip_get_selftest_result
  4106. *
  4107. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4108. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4109. *
  4110. * RETURN:
  4111. * 0 = success
  4112. * !0 = failure
  4113. *
  4114. * PARAMETERS:
  4115. * *dev - pointer to device structure
  4116. * protect - flag to call protected or non-protected
  4117. * read_offset - read pointer offset
  4118. * read_length - length of data to read
  4119. * test_id - enumerated test ID to read selftest results from
  4120. * *status - pointer to store the read response status
  4121. * *actual_read_len - pointer to store data length actually read
  4122. * *data - pointer to where the data read is stored
  4123. ******************************************************************************/
  4124. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4125. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4126. u8 *status, u16 *actual_read_len, u8 *data)
  4127. {
  4128. struct pt_core_data *cd = dev_get_drvdata(dev);
  4129. if (protect)
  4130. return pt_pip_get_selftest_result(cd, read_offset,
  4131. read_length, test_id, status, actual_read_len,
  4132. data);
  4133. return pt_pip_get_selftest_result_(cd, read_offset,
  4134. read_length, test_id, status, actual_read_len,
  4135. data);
  4136. }
  4137. /*******************************************************************************
  4138. * FUNCTION: _pt_pip_load_self_test_param
  4139. *
  4140. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4141. * to load parameters needed by a self test
  4142. *
  4143. * RETURN:
  4144. * 0 = success
  4145. * !0 = failure
  4146. *
  4147. * PARAMETERS:
  4148. * *cd - pointer to core data
  4149. * self_test_id - enumerated test ID for which the parmeters belong
  4150. * load_offset - mem offset to where to load parameters
  4151. * load_length - length of parameter data to load
  4152. * *parameters - pointer to list of parameter data
  4153. * *status - pointer to store the response status
  4154. * *ret_test_id - pointer to returned test id the parameters were stored
  4155. * *act_load_len - pointer to store the actual load length that was writen
  4156. ******************************************************************************/
  4157. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4158. u8 self_test_id, u16 load_offset, u16 load_length,
  4159. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4160. {
  4161. int rc = 0;
  4162. int i;
  4163. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4164. struct pt_hid_output hid_output = {
  4165. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4166. .write_length = 5 + load_length,
  4167. .write_buf = write_buf,
  4168. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4169. };
  4170. write_buf[0] = LOW_BYTE(load_offset);
  4171. write_buf[1] = HI_BYTE(load_offset);
  4172. write_buf[2] = LOW_BYTE(load_length);
  4173. write_buf[3] = HI_BYTE(load_length);
  4174. write_buf[4] = self_test_id;
  4175. for (i = 0; i < load_length; i++)
  4176. write_buf[i + 5] = parameters[i];
  4177. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4178. if (rc)
  4179. return rc;
  4180. if (status)
  4181. *status = cd->response_buf[5];
  4182. if (ret_test_id)
  4183. *ret_test_id = cd->response_buf[6];
  4184. if (act_load_len)
  4185. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4186. return rc;
  4187. }
  4188. /*******************************************************************************
  4189. * FUNCTION: pt_pip_load_self_test_param
  4190. *
  4191. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4192. * access to the DUT.
  4193. *
  4194. * RETURN:
  4195. * 0 = success
  4196. * !0 = failure
  4197. *
  4198. * PARAMETERS:
  4199. * *cd - pointer to core data
  4200. * self_test_id - enumerated test ID for which the parmeters belong
  4201. * load_offset - mem offset to where to load parameters
  4202. * load_length - length of parameter data to load
  4203. * *parameters - pointer to list of parameter data
  4204. * *status - pointer to store the response status
  4205. * *ret_test_id - pointer to returned test id the parameters were stored
  4206. * *act_load_len - pointer to store the actual load length that was writen
  4207. ******************************************************************************/
  4208. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4209. u8 self_test_id, u16 load_offset, u16 load_length,
  4210. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4211. {
  4212. int rc;
  4213. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4214. if (rc < 0) {
  4215. pt_debug(cd->dev, DL_ERROR,
  4216. "%s: fail get exclusive ex=%p own=%p\n",
  4217. __func__, cd->exclusive_dev, cd->dev);
  4218. return rc;
  4219. }
  4220. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4221. load_length, parameters, status, ret_test_id, act_load_len);
  4222. if (release_exclusive(cd, cd->dev) < 0)
  4223. pt_debug(cd->dev, DL_ERROR,
  4224. "%s: fail to release exclusive\n", __func__);
  4225. return rc;
  4226. }
  4227. /*******************************************************************************
  4228. * FUNCTION: _pt_request_pip_load_self_test_param
  4229. *
  4230. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4231. * calls to the protected or unprotected call to
  4232. * pt_pip_load_self_test_param
  4233. *
  4234. * RETURN:
  4235. * 0 = success
  4236. * !0 = failure
  4237. *
  4238. * PARAMETERS:
  4239. * *dev - pointer to device structure
  4240. * protect - flag to call protected or non-protected
  4241. * self_test_id - enumerated test ID for which the parmeters belong
  4242. * load_offset - mem offset to where to load parameters
  4243. * load_length - length of parameter data to load
  4244. * *parameters - pointer to list of parameter data
  4245. * *status - pointer to store the response status
  4246. * *ret_test_id - pointer to returned test id the parameters were stored
  4247. * *act_load_len - pointer to store the actual load length that was writen
  4248. ******************************************************************************/
  4249. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4250. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4251. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4252. {
  4253. struct pt_core_data *cd = dev_get_drvdata(dev);
  4254. if (protect)
  4255. return pt_pip_load_self_test_param(cd, self_test_id,
  4256. load_offset, load_length, parameters, status, ret_test_id,
  4257. act_load_len);
  4258. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4259. load_length, parameters, status, ret_test_id, act_load_len);
  4260. }
  4261. /*******************************************************************************
  4262. * FUNCTION: pt_pip_calibrate_ext_
  4263. *
  4264. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4265. * for the response
  4266. *
  4267. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4268. *
  4269. * RETURN:
  4270. * 0 = success
  4271. * !0 = failure
  4272. *
  4273. * PARAMETERS:
  4274. * *cd - pointer to core data
  4275. * *cal_data - pointer to extended calibration data structure
  4276. * *status - pointer to where the command response status is stored
  4277. ******************************************************************************/
  4278. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4279. struct pt_cal_ext_data *cal_data, u8 *status)
  4280. {
  4281. int rc = 0;
  4282. int write_length = 4;
  4283. u8 write_buf[4];
  4284. u16 size = 0;
  4285. unsigned short crc = 0;
  4286. struct pt_hid_output hid_output = {
  4287. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4288. .write_length = write_length,
  4289. .write_buf = write_buf,
  4290. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4291. };
  4292. if (cal_data == NULL)
  4293. return -EINVAL;
  4294. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4295. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4296. if (rc)
  4297. return rc;
  4298. if (status)
  4299. *status = cd->response_buf[5];
  4300. /*
  4301. * When doing a calibration on a flashless DUT, save CAL data in
  4302. * the TTDL cache on any successful calibration
  4303. */
  4304. if (cd->response_buf[5] == 0 && cd->cal_cache_in_host) {
  4305. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4306. __func__);
  4307. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4308. &size, &crc);
  4309. if (rc)
  4310. pt_debug(cd->dev, DL_ERROR,
  4311. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4312. else
  4313. pt_debug(cd->dev, DL_INFO,
  4314. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4315. __func__, crc, size);
  4316. }
  4317. return 0;
  4318. }
  4319. /*******************************************************************************
  4320. * FUNCTION: pt_pip_calibrate_ext
  4321. *
  4322. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4323. * DUT
  4324. *
  4325. * RETURN:
  4326. * 0 = success
  4327. * !0 = failure
  4328. *
  4329. * PARAMETERS:
  4330. * *cd - pointer to core data
  4331. * *cal_data - pointer to extended calibration data structure
  4332. * *status - pointer to where the command response status is stored
  4333. ******************************************************************************/
  4334. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4335. struct pt_cal_ext_data *cal_data, u8 *status)
  4336. {
  4337. int rc;
  4338. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4339. if (rc < 0) {
  4340. pt_debug(cd->dev, DL_ERROR,
  4341. "%s: fail get exclusive ex=%p own=%p\n",
  4342. __func__, cd->exclusive_dev, cd->dev);
  4343. return rc;
  4344. }
  4345. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4346. if (release_exclusive(cd, cd->dev) < 0)
  4347. pt_debug(cd->dev, DL_ERROR,
  4348. "%s: fail to release exclusive\n", __func__);
  4349. return rc;
  4350. }
  4351. /*******************************************************************************
  4352. * FUNCTION: _pt_request_pip_calibrate_ext
  4353. *
  4354. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4355. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4356. *
  4357. * RETURN:
  4358. * 0 = success
  4359. * !0 = failure
  4360. *
  4361. * PARAMETERS:
  4362. * *dev - pointer to device structure
  4363. * protect - flag to call protected or non-protected
  4364. * *cal_data - pointer to extended calibration data structure
  4365. * *status - pointer to where the command response status is stored
  4366. ******************************************************************************/
  4367. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4368. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4369. {
  4370. struct pt_core_data *cd = dev_get_drvdata(dev);
  4371. if (protect)
  4372. return pt_pip_calibrate_ext(cd, cal_data, status);
  4373. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4374. }
  4375. /*******************************************************************************
  4376. * FUNCTION: pt_pip_calibrate_idacs_
  4377. *
  4378. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4379. * for the response
  4380. *
  4381. * RETURN:
  4382. * 0 = success
  4383. * !0 = failure
  4384. *
  4385. * PARAMETERS:
  4386. * *cd - pointer to core data
  4387. * mode - sense mode to calibrate (0-5)
  4388. * *status - pointer to where the command response status is stored
  4389. ******************************************************************************/
  4390. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4391. u8 mode, u8 *status)
  4392. {
  4393. int rc = 0;
  4394. int write_length = 1;
  4395. u8 write_buf[1];
  4396. u8 cmd_offset = 0;
  4397. struct pt_hid_output hid_output = {
  4398. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4399. .write_length = write_length,
  4400. .write_buf = write_buf,
  4401. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4402. };
  4403. write_buf[cmd_offset++] = mode;
  4404. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4405. if (rc)
  4406. return rc;
  4407. *status = cd->response_buf[5];
  4408. if (*status)
  4409. return -EINVAL;
  4410. return 0;
  4411. }
  4412. /*******************************************************************************
  4413. * FUNCTION: pt_pip_calibrate_idacs
  4414. *
  4415. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4416. * access to the DUT
  4417. *
  4418. * RETURN:
  4419. * 0 = success
  4420. * !0 = failure
  4421. *
  4422. * PARAMETERS:
  4423. * *cd - pointer to core data
  4424. * mode - sense mode to calibrate (0-5)
  4425. * *status - pointer to where the command response status is stored
  4426. ******************************************************************************/
  4427. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4428. u8 mode, u8 *status)
  4429. {
  4430. int rc;
  4431. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4432. if (rc < 0) {
  4433. pt_debug(cd->dev, DL_ERROR,
  4434. "%s: fail get exclusive ex=%p own=%p\n",
  4435. __func__, cd->exclusive_dev, cd->dev);
  4436. return rc;
  4437. }
  4438. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4439. if (release_exclusive(cd, cd->dev) < 0)
  4440. pt_debug(cd->dev, DL_ERROR,
  4441. "%s: fail to release exclusive\n", __func__);
  4442. return rc;
  4443. }
  4444. /*******************************************************************************
  4445. * FUNCTION: _pt_request_pip_calibrate_idacs
  4446. *
  4447. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4448. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4449. *
  4450. * RETURN:
  4451. * 0 = success
  4452. * !0 = failure
  4453. *
  4454. * PARAMETERS:
  4455. * *dev - pointer to device structure
  4456. * protect - flag to call protected or non-protected
  4457. * mode - sense mode to calibrate (0-5)
  4458. * *status - pointer to where the command response status is stored
  4459. ******************************************************************************/
  4460. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4461. int protect, u8 mode, u8 *status)
  4462. {
  4463. struct pt_core_data *cd = dev_get_drvdata(dev);
  4464. if (protect)
  4465. return pt_pip_calibrate_idacs(cd, mode, status);
  4466. return pt_pip_calibrate_idacs_(cd, mode, status);
  4467. }
  4468. /*******************************************************************************
  4469. * FUNCTION: pt_hid_output_initialize_baselines_
  4470. *
  4471. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4472. * waiting for the response.
  4473. *
  4474. * RETURN:
  4475. * 0 = success
  4476. * !0 = failure
  4477. *
  4478. * PARAMETERS:
  4479. * *cd - pointer to core data
  4480. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4481. * each or together with a single command.
  4482. * *status - pointer to where the command response status is stored
  4483. ******************************************************************************/
  4484. static int pt_hid_output_initialize_baselines_(
  4485. struct pt_core_data *cd, u8 test_id, u8 *status)
  4486. {
  4487. int rc = 0;
  4488. int write_length = 1;
  4489. u8 write_buf[1];
  4490. u8 cmd_offset = 0;
  4491. struct pt_hid_output hid_output = {
  4492. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4493. .write_length = write_length,
  4494. .write_buf = write_buf,
  4495. };
  4496. write_buf[cmd_offset++] = test_id;
  4497. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4498. if (rc)
  4499. return rc;
  4500. *status = cd->response_buf[5];
  4501. if (*status)
  4502. return -EINVAL;
  4503. return rc;
  4504. }
  4505. /*******************************************************************************
  4506. * FUNCTION: pt_hid_output_initialize_baselines
  4507. *
  4508. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4509. * access to the DUT
  4510. *
  4511. * RETURN:
  4512. * 0 = success
  4513. * !0 = failure
  4514. *
  4515. * PARAMETERS:
  4516. * *cd - pointer to core data
  4517. * test_id - enumerated ID against which to initialize the baseline
  4518. * *status - pointer to where the command response status is stored
  4519. ******************************************************************************/
  4520. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4521. u8 test_id, u8 *status)
  4522. {
  4523. int rc;
  4524. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4525. if (rc < 0) {
  4526. pt_debug(cd->dev, DL_ERROR,
  4527. "%s: fail get exclusive ex=%p own=%p\n",
  4528. __func__, cd->exclusive_dev, cd->dev);
  4529. return rc;
  4530. }
  4531. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4532. if (release_exclusive(cd, cd->dev) < 0)
  4533. pt_debug(cd->dev, DL_ERROR,
  4534. "%s: fail to release exclusive\n", __func__);
  4535. return rc;
  4536. }
  4537. /*******************************************************************************
  4538. * FUNCTION: _pt_request_pip_initialize_baselines
  4539. *
  4540. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4541. * calls to the protected or unprotected call to
  4542. * pt_pip_initialize_baselines
  4543. *
  4544. * RETURN:
  4545. * 0 = success
  4546. * !0 = failure
  4547. *
  4548. * PARAMETERS:
  4549. * *dev - pointer to device structure
  4550. * protect - flag to call protected or non-protected
  4551. * test_id - enumerated ID against which to initialize the baseline
  4552. * *status - pointer to where the command response status is stored
  4553. ******************************************************************************/
  4554. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4555. int protect, u8 test_id, u8 *status)
  4556. {
  4557. struct pt_core_data *cd = dev_get_drvdata(dev);
  4558. if (protect)
  4559. return pt_hid_output_initialize_baselines(cd, test_id,
  4560. status);
  4561. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4562. }
  4563. /*******************************************************************************
  4564. * FUNCTION: pt_hid_output_exec_panel_scan_
  4565. *
  4566. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4567. * the response
  4568. *
  4569. * RETURN:
  4570. * 0 = success
  4571. * !0 = failure
  4572. *
  4573. * PARAMETERS:
  4574. * *cd - pointer to core data
  4575. ******************************************************************************/
  4576. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4577. {
  4578. struct pt_hid_output hid_output = {
  4579. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4580. };
  4581. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4582. }
  4583. /*******************************************************************************
  4584. * FUNCTION: pt_hid_output_exec_panel_scan
  4585. *
  4586. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4587. * access to the DUT.
  4588. *
  4589. * RETURN:
  4590. * 0 = success
  4591. * !0 = failure
  4592. *
  4593. * PARAMETERS:
  4594. * *cd - pointer to core data
  4595. ******************************************************************************/
  4596. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4597. {
  4598. int rc;
  4599. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4600. if (rc < 0) {
  4601. pt_debug(cd->dev, DL_ERROR,
  4602. "%s: fail get exclusive ex=%p own=%p\n",
  4603. __func__, cd->exclusive_dev, cd->dev);
  4604. return rc;
  4605. }
  4606. rc = pt_hid_output_exec_panel_scan_(cd);
  4607. if (release_exclusive(cd, cd->dev) < 0)
  4608. pt_debug(cd->dev, DL_ERROR,
  4609. "%s: fail to release exclusive\n", __func__);
  4610. return rc;
  4611. }
  4612. /*******************************************************************************
  4613. * FUNCTION: pt_pip2_exec_panel_scan_
  4614. *
  4615. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4616. * the response
  4617. *
  4618. * RETURN:
  4619. * 0 = success
  4620. * !0 = failure
  4621. *
  4622. * PARAMETERS:
  4623. * *cd - pointer to core data
  4624. * scan_type - type of panel scan to perform (PIP2 only)
  4625. ******************************************************************************/
  4626. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4627. {
  4628. int rc = 0;
  4629. u8 data[2];
  4630. u8 read_buf[10];
  4631. u16 actual_read_len;
  4632. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4633. __func__, scan_type);
  4634. data[0] = scan_type;
  4635. rc = _pt_request_pip2_send_cmd(cd->dev,
  4636. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4637. data, 1, read_buf, &actual_read_len);
  4638. if (rc) {
  4639. pt_debug(cd->dev, DL_ERROR,
  4640. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4641. __func__, scan_type, rc);
  4642. }
  4643. return rc;
  4644. }
  4645. /*******************************************************************************
  4646. * FUNCTION: pt_pip2_exec_panel_scan
  4647. *
  4648. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4649. * access to the DUT.
  4650. *
  4651. * RETURN:
  4652. * 0 = success
  4653. * !0 = failure
  4654. *
  4655. * PARAMETERS:
  4656. * *cd - pointer to core data
  4657. * scan_type - type of panel scan to perform (PIP2 only)
  4658. ******************************************************************************/
  4659. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4660. {
  4661. int rc;
  4662. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4663. if (rc < 0) {
  4664. pt_debug(cd->dev, DL_ERROR,
  4665. "%s: fail get exclusive ex=%p own=%p\n",
  4666. __func__, cd->exclusive_dev, cd->dev);
  4667. return rc;
  4668. }
  4669. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4670. if (release_exclusive(cd, cd->dev) < 0)
  4671. pt_debug(cd->dev, DL_ERROR,
  4672. "%s: fail to release exclusive\n", __func__);
  4673. return rc;
  4674. }
  4675. /*******************************************************************************
  4676. * FUNCTION: _pt_request_pip_exec_panel_scan
  4677. *
  4678. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4679. * calls to the protected or unprotected call to
  4680. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4681. *
  4682. * RETURN:
  4683. * 0 = success
  4684. * !0 = failure
  4685. *
  4686. * PARAMETERS:
  4687. * *dev - pointer to device structure
  4688. * protect - flag to call protected or non-protected
  4689. * scan_type - type of panel scan to perform (PIP2 only)
  4690. ******************************************************************************/
  4691. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4692. int protect, u8 scan_type)
  4693. {
  4694. struct pt_core_data *cd = dev_get_drvdata(dev);
  4695. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4696. if (protect)
  4697. return pt_pip2_exec_panel_scan(cd, scan_type);
  4698. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4699. }
  4700. if (protect)
  4701. return pt_hid_output_exec_panel_scan(cd);
  4702. return pt_hid_output_exec_panel_scan_(cd);
  4703. }
  4704. /*******************************************************************************
  4705. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4706. *
  4707. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4708. * to retrieve the specified data type for a the last successful Execute
  4709. * Panel Scan command.
  4710. *
  4711. * RETURN:
  4712. * 0 = success
  4713. * !0 = failure
  4714. *
  4715. * PARAMETERS:
  4716. * *dev - pointer to device structure
  4717. * protect - flag to call protected or non-protected
  4718. * read_offset - read pointer offset
  4719. * read_count - length of data to read
  4720. * data_id - enumerated test ID to read selftest results from
  4721. * *response - pointer to store the read response status
  4722. * *config - pointer to store config data
  4723. * *actual_read_len - pointer to store data length actually read
  4724. * *read_buf - pointer to the read buffer
  4725. ******************************************************************************/
  4726. static int pt_hid_output_retrieve_panel_scan_(
  4727. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4728. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4729. u8 *read_buf)
  4730. {
  4731. int status;
  4732. u8 read_data_id;
  4733. int rc = 0;
  4734. int write_length = 5;
  4735. u8 write_buf[5];
  4736. u8 cmd_offset = 0;
  4737. u8 data_elem_size;
  4738. int size;
  4739. int data_size;
  4740. struct pt_hid_output hid_output = {
  4741. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4742. .write_length = write_length,
  4743. .write_buf = write_buf,
  4744. };
  4745. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4746. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4747. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4748. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4749. write_buf[cmd_offset++] = data_id;
  4750. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4751. if (rc)
  4752. return rc;
  4753. status = cd->response_buf[5];
  4754. if (status)
  4755. return -EINVAL;
  4756. read_data_id = cd->response_buf[6];
  4757. if (read_data_id != data_id)
  4758. return -EPROTO;
  4759. size = get_unaligned_le16(&cd->response_buf[0]);
  4760. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4761. *config = cd->response_buf[9];
  4762. data_elem_size = *config & 0x07;
  4763. data_size = *actual_read_len * data_elem_size;
  4764. if (read_buf)
  4765. memcpy(read_buf, &cd->response_buf[10], data_size);
  4766. if (response)
  4767. memcpy(response, cd->response_buf, size);
  4768. return rc;
  4769. }
  4770. /*******************************************************************************
  4771. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4772. *
  4773. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4774. * access to the DUT.
  4775. *
  4776. * RETURN:
  4777. * 0 = success
  4778. * !0 = failure
  4779. *
  4780. * PARAMETERS:
  4781. * *dev - pointer to device structure
  4782. * protect - flag to call protected or non-protected
  4783. * read_offset - read pointer offset
  4784. * read_count - length of data to read
  4785. * data_id - enumerated test ID to read selftest results from
  4786. * *response - pointer to store the read response status
  4787. * *config - pointer to store config data
  4788. * *actual_read_len - pointer to store data length actually read
  4789. * *read_buf - pointer to the read buffer
  4790. ******************************************************************************/
  4791. static int pt_hid_output_retrieve_panel_scan(
  4792. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4793. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4794. u8 *read_buf)
  4795. {
  4796. int rc;
  4797. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4798. if (rc < 0) {
  4799. pt_debug(cd->dev, DL_ERROR,
  4800. "%s: fail get exclusive ex=%p own=%p\n",
  4801. __func__, cd->exclusive_dev, cd->dev);
  4802. return rc;
  4803. }
  4804. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4805. read_count, data_id, response, config,
  4806. actual_read_len, read_buf);
  4807. if (release_exclusive(cd, cd->dev) < 0)
  4808. pt_debug(cd->dev, DL_ERROR,
  4809. "%s: fail to release exclusive\n", __func__);
  4810. return rc;
  4811. }
  4812. /*******************************************************************************
  4813. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4814. *
  4815. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4816. * calls to the protected or unprotected call to
  4817. * pt_hid_output_retrieve_panel_scan
  4818. *
  4819. * RETURN:
  4820. * 0 = success
  4821. * !0 = failure
  4822. *
  4823. * PARAMETERS:
  4824. * *dev - pointer to device structure
  4825. * protect - flag to call protected or non-protected
  4826. * read_offset - read pointer offset
  4827. * read_count - length of data to read
  4828. * data_id - enumerated test ID to read selftest results from
  4829. * *response - pointer to store the read response status
  4830. * *config - pointer to store config data
  4831. * *actual_read_len - pointer to store data length actually read
  4832. * *read_buf - pointer to the read buffer
  4833. ******************************************************************************/
  4834. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4835. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4836. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4837. {
  4838. struct pt_core_data *cd = dev_get_drvdata(dev);
  4839. if (protect)
  4840. return pt_hid_output_retrieve_panel_scan(cd,
  4841. read_offset, read_count, data_id, response,
  4842. config, actual_read_len, read_buf);
  4843. return pt_hid_output_retrieve_panel_scan_(cd,
  4844. read_offset, read_count, data_id, response,
  4845. config, actual_read_len, read_buf);
  4846. }
  4847. /*******************************************************************************
  4848. * FUNCTION: _pt_request_pip_user_cmd
  4849. *
  4850. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4851. * calls to the protected or unprotected call to
  4852. * pt_hid_output_user_cmd
  4853. *
  4854. * RETURN:
  4855. * 0 = success
  4856. * !0 = failure
  4857. *
  4858. * PARAMETERS:
  4859. * *dev - pointer to device structure
  4860. * protect - flag to call protected or non-protected
  4861. * read_len - length of data to read
  4862. * *read_buf - pointer to store read data
  4863. * write_len - length of data to write
  4864. * *write_buf - pointer to buffer to write
  4865. * *actual_read_len - pointer to store data length actually read
  4866. ******************************************************************************/
  4867. static int _pt_request_pip_user_cmd(struct device *dev,
  4868. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4869. u8 *write_buf, u16 *actual_read_len)
  4870. {
  4871. struct pt_core_data *cd = dev_get_drvdata(dev);
  4872. if (protect)
  4873. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4874. write_len, write_buf, actual_read_len);
  4875. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4876. write_len, write_buf, actual_read_len);
  4877. }
  4878. /*******************************************************************************
  4879. * FUNCTION: pt_hid_output_bl_get_information_
  4880. *
  4881. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4882. * DUT to retrieve bootloader version and chip identification information.
  4883. *
  4884. * RETURN:
  4885. * 0 = success
  4886. * !0 = failure
  4887. *
  4888. * PARAMETERS:
  4889. * *cd - pointer to core data
  4890. * *return_data - pointer to store the return data
  4891. *****************************************************************************/
  4892. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4893. u8 *return_data)
  4894. {
  4895. int rc;
  4896. int data_len;
  4897. struct pt_hid_output hid_output = {
  4898. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4899. };
  4900. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4901. if (rc)
  4902. return rc;
  4903. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4904. if (!data_len)
  4905. return -EPROTO;
  4906. memcpy(return_data, &cd->response_buf[8], data_len);
  4907. return 0;
  4908. }
  4909. /*******************************************************************************
  4910. * FUNCTION: pt_hid_output_bl_get_information
  4911. *
  4912. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4913. * access to the DUT.
  4914. *
  4915. * RETURN:
  4916. * 0 = success
  4917. * !0 = failure
  4918. *
  4919. * PARAMETERS:
  4920. * *cd - pointer to core data
  4921. * *return_data - pointer to store the return data
  4922. ******************************************************************************/
  4923. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4924. u8 *return_data)
  4925. {
  4926. int rc;
  4927. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4928. if (rc < 0) {
  4929. pt_debug(cd->dev, DL_ERROR,
  4930. "%s: fail get exclusive ex=%p own=%p\n",
  4931. __func__, cd->exclusive_dev, cd->dev);
  4932. return rc;
  4933. }
  4934. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4935. if (release_exclusive(cd, cd->dev) < 0)
  4936. pt_debug(cd->dev, DL_ERROR,
  4937. "%s: fail to release exclusive\n", __func__);
  4938. return rc;
  4939. }
  4940. /*******************************************************************************
  4941. * FUNCTION: _pt_request_pip_bl_get_information
  4942. *
  4943. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4944. * calls to the protected or unprotected call to
  4945. * pt_hid_output_bl_get_information
  4946. *
  4947. * RETURN:
  4948. * 0 = success
  4949. * !0 = failure
  4950. *
  4951. * PARAMETERS:
  4952. * *dev - pointer to device structure
  4953. * protect - flag to call protected or non-protected
  4954. * *return_data - pointer to store bl data
  4955. ******************************************************************************/
  4956. static int _pt_request_pip_bl_get_information(struct device *dev,
  4957. int protect, u8 *return_data)
  4958. {
  4959. struct pt_core_data *cd = dev_get_drvdata(dev);
  4960. if (protect)
  4961. return pt_hid_output_bl_get_information(cd, return_data);
  4962. return pt_hid_output_bl_get_information_(cd, return_data);
  4963. }
  4964. /*******************************************************************************
  4965. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4966. *
  4967. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4968. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4969. * and Design Data block in flash and enables the host to execute the Program
  4970. * and Verify Row command to bootload the application image and data.
  4971. *
  4972. * RETURN:
  4973. * 0 = success
  4974. * !0 = failure
  4975. *
  4976. * PARAMETERS:
  4977. * *cd - pointer to core data
  4978. * protect - flag to call protected or non-protected
  4979. * key_size - size of key
  4980. * *key_buf - pointer to key data to allow operation
  4981. * row_size - size of the meta data row
  4982. * *metadata_row_buf - pointer to meta data to write
  4983. ******************************************************************************/
  4984. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4985. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4986. {
  4987. u16 write_length = key_size + row_size;
  4988. u8 *write_buf;
  4989. int rc = 0;
  4990. struct pt_hid_output hid_output = {
  4991. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4992. .write_length = write_length,
  4993. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4994. };
  4995. write_buf = kzalloc(write_length, GFP_KERNEL);
  4996. if (!write_buf)
  4997. return -ENOMEM;
  4998. hid_output.write_buf = write_buf;
  4999. if (key_size)
  5000. memcpy(write_buf, key_buf, key_size);
  5001. if (row_size)
  5002. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5003. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5004. kfree(write_buf);
  5005. return rc;
  5006. }
  5007. /*******************************************************************************
  5008. * FUNCTION: pt_hid_output_bl_initiate_bl
  5009. *
  5010. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5011. * access to the DUT.
  5012. *
  5013. * RETURN:
  5014. * 0 = success
  5015. * !0 = failure
  5016. *
  5017. * PARAMETERS:
  5018. * *cd - pointer to core data
  5019. * key_size - size of key
  5020. * *key_buf - pointer to key data to allow operation
  5021. * row_size - size of the meta data row
  5022. * *metadata_row_buf - pointer to meta data to write
  5023. ******************************************************************************/
  5024. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5025. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5026. {
  5027. int rc;
  5028. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5029. if (rc < 0) {
  5030. pt_debug(cd->dev, DL_ERROR,
  5031. "%s: fail get exclusive ex=%p own=%p\n",
  5032. __func__, cd->exclusive_dev, cd->dev);
  5033. return rc;
  5034. }
  5035. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5036. row_size, metadata_row_buf);
  5037. if (release_exclusive(cd, cd->dev) < 0)
  5038. pt_debug(cd->dev, DL_ERROR,
  5039. "%s: fail to release exclusive\n", __func__);
  5040. return rc;
  5041. }
  5042. /*******************************************************************************
  5043. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5044. *
  5045. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5046. * calls to the protected or unprotected call to
  5047. * pt_hid_output_bl_initiate_bl
  5048. *
  5049. * RETURN:
  5050. * 0 = success
  5051. * !0 = failure
  5052. *
  5053. * PARAMETERS:
  5054. * *dev - pointer to device structure
  5055. * protect - flag to call protected or non-protected
  5056. * key_size - size of key
  5057. * *key_buf - pointer to key data to allow operation
  5058. * row_size - size of the meta data row
  5059. * *metadata_row_buf - pointer to meta data to write
  5060. ******************************************************************************/
  5061. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5062. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5063. u8 *metadata_row_buf)
  5064. {
  5065. struct pt_core_data *cd = dev_get_drvdata(dev);
  5066. if (protect)
  5067. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5068. row_size, metadata_row_buf);
  5069. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5070. row_size, metadata_row_buf);
  5071. }
  5072. /*******************************************************************************
  5073. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5074. *
  5075. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5076. * and program a 128-byte row into the flash, and then verifies written data.
  5077. *
  5078. * RETURN:
  5079. * 0 = success
  5080. * !0 = failure
  5081. *
  5082. * PARAMETERS:
  5083. * *cd - pointer to core data
  5084. * data_len - length of data_buf
  5085. * *data_buf - firmware image to program
  5086. ******************************************************************************/
  5087. static int pt_hid_output_bl_program_and_verify_(
  5088. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5089. {
  5090. struct pt_hid_output hid_output = {
  5091. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5092. .write_length = data_len,
  5093. .write_buf = data_buf,
  5094. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5095. };
  5096. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5097. }
  5098. /*******************************************************************************
  5099. * FUNCTION: pt_hid_output_bl_program_and_verify
  5100. *
  5101. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5102. * access to the DUT.
  5103. *
  5104. * RETURN:
  5105. * 0 = success
  5106. * !0 = failure
  5107. *
  5108. * PARAMETERS:
  5109. * *cd - pointer to core data
  5110. * data_len - length of data_buf
  5111. * *data_buf - firmware image to program
  5112. ******************************************************************************/
  5113. static int pt_hid_output_bl_program_and_verify(
  5114. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5115. {
  5116. int rc;
  5117. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5118. if (rc < 0) {
  5119. pt_debug(cd->dev, DL_ERROR,
  5120. "%s: fail get exclusive ex=%p own=%p\n",
  5121. __func__, cd->exclusive_dev, cd->dev);
  5122. return rc;
  5123. }
  5124. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5125. if (release_exclusive(cd, cd->dev) < 0)
  5126. pt_debug(cd->dev, DL_ERROR,
  5127. "%s: fail to release exclusive\n", __func__);
  5128. return rc;
  5129. }
  5130. /*******************************************************************************
  5131. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5132. *
  5133. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5134. * to request to have the BL program and verify a FW image
  5135. *
  5136. * RETURN:
  5137. * 0 = success
  5138. * !0 = failure
  5139. *
  5140. * PARAMETERS:
  5141. * *dev - pointer to device structure
  5142. * protect - boolean to determine to call the protected function
  5143. * data_len - length of data_buf
  5144. * *data_buf - firmware image to program
  5145. ******************************************************************************/
  5146. static int _pt_request_pip_bl_program_and_verify(
  5147. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5148. {
  5149. struct pt_core_data *cd = dev_get_drvdata(dev);
  5150. if (protect)
  5151. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5152. data_buf);
  5153. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5154. data_buf);
  5155. }
  5156. /*******************************************************************************
  5157. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5158. *
  5159. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5160. * perform a full verification of the application integrity by calculating the
  5161. * CRC of the image in flash and compare it to the expected CRC stored in the
  5162. * Metadata row.
  5163. *
  5164. * RETURN:
  5165. * 0 = success
  5166. * !0 = failure
  5167. *
  5168. * PARAMETERS:
  5169. * *cd - pointer to core data
  5170. * *result - pointer to store result
  5171. ******************************************************************************/
  5172. static int pt_hid_output_bl_verify_app_integrity_(
  5173. struct pt_core_data *cd, u8 *result)
  5174. {
  5175. int rc;
  5176. struct pt_hid_output hid_output = {
  5177. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5178. };
  5179. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5180. if (rc) {
  5181. *result = 0;
  5182. return rc;
  5183. }
  5184. *result = cd->response_buf[8];
  5185. return 0;
  5186. }
  5187. /*******************************************************************************
  5188. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5189. *
  5190. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5191. * exclusive access to the DUT.
  5192. *
  5193. * RETURN:
  5194. * 0 = success
  5195. * !0 = failure
  5196. *
  5197. * PARAMETERS:
  5198. * *cd - pointer to core data
  5199. * *result - pointer to store result
  5200. ******************************************************************************/
  5201. static int pt_hid_output_bl_verify_app_integrity(
  5202. struct pt_core_data *cd, u8 *result)
  5203. {
  5204. int rc;
  5205. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5206. if (rc < 0) {
  5207. pt_debug(cd->dev, DL_ERROR,
  5208. "%s: fail get exclusive ex=%p own=%p\n",
  5209. __func__, cd->exclusive_dev, cd->dev);
  5210. return rc;
  5211. }
  5212. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5213. if (release_exclusive(cd, cd->dev) < 0)
  5214. pt_debug(cd->dev, DL_ERROR,
  5215. "%s: fail to release exclusive\n", __func__);
  5216. return rc;
  5217. }
  5218. /*******************************************************************************
  5219. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5220. *
  5221. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5222. * to request to have the BL verify the application integrity (PIP1.x only)
  5223. *
  5224. * RETURN:
  5225. * 0 = success
  5226. * !0 = failure
  5227. *
  5228. * PARAMETERS:
  5229. * *dev - pointer to device structure
  5230. * protect - boolean to determine to call the protected function
  5231. * *result - pointer to store result
  5232. ******************************************************************************/
  5233. static int _pt_request_pip_bl_verify_app_integrity(
  5234. struct device *dev, int protect, u8 *result)
  5235. {
  5236. struct pt_core_data *cd = dev_get_drvdata(dev);
  5237. if (protect)
  5238. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5239. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5240. }
  5241. /*******************************************************************************
  5242. * FUNCTION: pt_hid_output_bl_launch_app_
  5243. *
  5244. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5245. * application from bootloader (PIP1.x only).
  5246. *
  5247. * RETURN:
  5248. * 0 = success
  5249. * !0 = failure
  5250. *
  5251. * PARAMETERS:
  5252. * *cd - pointer to core data
  5253. ******************************************************************************/
  5254. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5255. {
  5256. struct pt_hid_output hid_output = {
  5257. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5258. .reset_expected = 1,
  5259. };
  5260. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5261. }
  5262. /*******************************************************************************
  5263. * FUNCTION: pt_hid_output_bl_launch_app
  5264. *
  5265. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5266. * to the DUT.
  5267. *
  5268. * RETURN:
  5269. * 0 = success
  5270. * !0 = failure
  5271. *
  5272. * PARAMETERS:
  5273. * *cd - pointer to core data
  5274. ******************************************************************************/
  5275. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5276. {
  5277. int rc;
  5278. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5279. if (rc < 0) {
  5280. pt_debug(cd->dev, DL_ERROR,
  5281. "%s: fail get exclusive ex=%p own=%p\n",
  5282. __func__, cd->exclusive_dev, cd->dev);
  5283. return rc;
  5284. }
  5285. rc = pt_hid_output_bl_launch_app_(cd);
  5286. if (release_exclusive(cd, cd->dev) < 0)
  5287. pt_debug(cd->dev, DL_ERROR,
  5288. "%s: fail to release exclusive\n", __func__);
  5289. return rc;
  5290. }
  5291. /*******************************************************************************
  5292. * FUNCTION: _pt_request_pip_launch_app
  5293. *
  5294. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5295. * to request to have the BL launch the application. (PIP1.x only)
  5296. *
  5297. * RETURN:
  5298. * 0 = success
  5299. * !0 = failure
  5300. *
  5301. * PARAMETERS:
  5302. * *dev - pointer to device structure
  5303. * protect - boolean to determine to call the protected function
  5304. ******************************************************************************/
  5305. static int _pt_request_pip_launch_app(struct device *dev,
  5306. int protect)
  5307. {
  5308. struct pt_core_data *cd = dev_get_drvdata(dev);
  5309. if (protect)
  5310. return pt_hid_output_bl_launch_app(cd);
  5311. return pt_hid_output_bl_launch_app_(cd);
  5312. }
  5313. /*******************************************************************************
  5314. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5315. *
  5316. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5317. * value store in the System Information.
  5318. *
  5319. * RETURN:
  5320. * 0 = success
  5321. * !0 = failure
  5322. *
  5323. * PARAMETERS:
  5324. * *cd - pointer to core data
  5325. * *panel_id - pointer to where the panel ID will be stored
  5326. ******************************************************************************/
  5327. static int pt_hid_output_bl_get_panel_id_(
  5328. struct pt_core_data *cd, u8 *panel_id)
  5329. {
  5330. int rc;
  5331. struct pt_hid_output hid_output = {
  5332. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5333. };
  5334. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5335. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5336. pt_debug(cd->dev, DL_ERROR,
  5337. "%s: Get Panel ID command not supported\n",
  5338. __func__);
  5339. *panel_id = PANEL_ID_NOT_ENABLED;
  5340. return 0;
  5341. } else if (rc < 0) {
  5342. pt_debug(cd->dev, DL_ERROR,
  5343. "%s: Error on Get Panel ID command\n", __func__);
  5344. return rc;
  5345. }
  5346. *panel_id = cd->response_buf[8];
  5347. return 0;
  5348. }
  5349. /*******************************************************************************
  5350. * FUNCTION: pt_hid_output_bl_get_panel_id
  5351. *
  5352. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5353. * to the DUT.
  5354. *
  5355. * RETURN:
  5356. * 0 = success
  5357. * !0 = failure
  5358. *
  5359. * PARAMETERS:
  5360. * *cd - pointer to core data
  5361. * *panel_id - pointer to where the panel ID will be stored
  5362. ******************************************************************************/
  5363. static int pt_hid_output_bl_get_panel_id(
  5364. struct pt_core_data *cd, u8 *panel_id)
  5365. {
  5366. int rc;
  5367. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5368. if (rc < 0) {
  5369. pt_debug(cd->dev, DL_ERROR,
  5370. "%s: fail get exclusive ex=%p own=%p\n",
  5371. __func__, cd->exclusive_dev, cd->dev);
  5372. return rc;
  5373. }
  5374. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5375. if (release_exclusive(cd, cd->dev) < 0)
  5376. pt_debug(cd->dev, DL_ERROR,
  5377. "%s: fail to release exclusive\n", __func__);
  5378. return rc;
  5379. }
  5380. /*******************************************************************************
  5381. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5382. *
  5383. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5384. * to have the BL retrieve the panel ID
  5385. *
  5386. * RETURN:
  5387. * 0 = success
  5388. * !0 = failure
  5389. *
  5390. * PARAMETERS:
  5391. * *dev - pointer to device structure
  5392. * protect - flag to run in protected mode
  5393. * *panel_id - pointer to where the panel ID will be stored
  5394. ******************************************************************************/
  5395. static int _pt_request_pip_bl_get_panel_id(
  5396. struct device *dev, int protect, u8 *panel_id)
  5397. {
  5398. struct pt_core_data *cd = dev_get_drvdata(dev);
  5399. if (protect)
  5400. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5401. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5402. }
  5403. /*******************************************************************************
  5404. * FUNCTION: pt_pip2_get_mode_sysmode_
  5405. *
  5406. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5407. * PIP2 STATUS command.
  5408. *
  5409. * RETURN:
  5410. * 0 = success
  5411. * !0 = failure
  5412. *
  5413. * PARAMETERS:
  5414. * *cd - pointer to core data structure
  5415. * *mode - pointer to store the retrieved mode
  5416. * *sys_mode - pointer to store the FW system mode
  5417. ******************************************************************************/
  5418. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5419. u8 *mode, u8 *sys_mode)
  5420. {
  5421. u16 actual_read_len;
  5422. u8 read_buf[12];
  5423. u8 status, boot;
  5424. int rc = 0;
  5425. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5426. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5427. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5428. __func__, rc);
  5429. if (!rc) {
  5430. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5431. "PIP2 STATUS");
  5432. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5433. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5434. if (sys_mode) {
  5435. if (status == PIP2_RSP_ERR_NONE &&
  5436. boot == PIP2_STATUS_APP_EXEC)
  5437. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5438. else
  5439. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5440. }
  5441. if (mode) {
  5442. if (status == PIP2_RSP_ERR_NONE &&
  5443. boot == PIP2_STATUS_BOOT_EXEC)
  5444. *mode = PT_MODE_BOOTLOADER;
  5445. else if (status == PIP2_RSP_ERR_NONE &&
  5446. boot == PIP2_STATUS_APP_EXEC)
  5447. *mode = PT_MODE_OPERATIONAL;
  5448. else
  5449. *mode = PT_MODE_UNKNOWN;
  5450. }
  5451. } else {
  5452. if (mode)
  5453. *mode = PT_MODE_UNKNOWN;
  5454. if (sys_mode)
  5455. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5456. pt_debug(cd->dev, DL_WARN,
  5457. "%s: Mode and sys_mode could not be determined\n",
  5458. __func__);
  5459. }
  5460. return rc;
  5461. }
  5462. /*******************************************************************************
  5463. * FUNCTION: pt_pip2_get_mode_sysmode
  5464. *
  5465. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5466. * access to the DUT.
  5467. *
  5468. * RETURN:
  5469. * 0 = success
  5470. * !0 = failure
  5471. *
  5472. * PARAMETERS:
  5473. * *cd - pointer to core data structure
  5474. * *mode - pointer to store the retrieved mode
  5475. * *sys_mode - pointer to store the FW system mode
  5476. ******************************************************************************/
  5477. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5478. u8 *mode, u8 *sys_mode)
  5479. {
  5480. int rc;
  5481. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5482. if (rc < 0) {
  5483. pt_debug(cd->dev, DL_ERROR,
  5484. "%s: fail get exclusive ex=%p own=%p\n",
  5485. __func__, cd->exclusive_dev, cd->dev);
  5486. return rc;
  5487. }
  5488. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5489. if (release_exclusive(cd, cd->dev) < 0)
  5490. pt_debug(cd->dev, DL_ERROR,
  5491. "%s: fail to release exclusive\n", __func__);
  5492. return rc;
  5493. }
  5494. /*******************************************************************************
  5495. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5496. *
  5497. * SUMMARY: Function pointer included in core_commands struct for external
  5498. * calls to the protected or unprotected call to
  5499. * pt_pip2_get_mode_sysmode
  5500. *
  5501. * RETURN:
  5502. * 0 = success
  5503. * !0 = failure
  5504. *
  5505. * PARAMETERS:
  5506. * *dev - pointer to device structure
  5507. * *mode - pointer to store the retrieved mode
  5508. * *sys_mode - pointer to store the FW system mode
  5509. ******************************************************************************/
  5510. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5511. int protect, u8 *mode, u8 *sys_mode)
  5512. {
  5513. struct pt_core_data *cd = dev_get_drvdata(dev);
  5514. if (protect)
  5515. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5516. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5517. }
  5518. /*******************************************************************************
  5519. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5520. *
  5521. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5522. * mode only the following PIP commands will be serviced, any other PIP
  5523. * command the FW will respond with an "Invalid PIP Command" response.
  5524. * - Get HID Descriptor (Register 0x0001, no command ID)
  5525. * - Reset (Register 0x0005, RESET HID request)
  5526. * - Ping (Register 0x0004, Command ID 0x00
  5527. * - Get System Information (Register 0x0004, Command ID 0x02)
  5528. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5529. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5530. * This function will loop on the results of the STATUS command until
  5531. * the FW reports it is out of BOOT mode.
  5532. *
  5533. * NOTE:
  5534. * - This function will update cd->fw_system_mode
  5535. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5536. *
  5537. * RETURN:
  5538. * 0 = success
  5539. * !0 = failure
  5540. *
  5541. * PARAMETERS:
  5542. * *cd - pointer to core data
  5543. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5544. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5545. ******************************************************************************/
  5546. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5547. int *actual_wait)
  5548. {
  5549. int loop = 0;
  5550. u8 sys_mode = cd->fw_system_mode;
  5551. u8 pause = 10; /* in ms */
  5552. int rc = 0;
  5553. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5554. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5555. /*
  5556. * For PIP <1.11, no support for polling wait so do a hard
  5557. * coded wait and assume the FW is out of BOOT. Added 1 to
  5558. * timeout to make it clear in kmsg if non polling was done.
  5559. */
  5560. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5561. pt_debug(cd->dev, DL_ERROR,
  5562. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5563. __func__,
  5564. cd->sysinfo.ttdata.pip_ver_major,
  5565. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5566. msleep(*actual_wait);
  5567. sys_mode = FW_SYS_MODE_SCANNING;
  5568. }
  5569. if (sys_mode == FW_SYS_MODE_BOOT) {
  5570. while (!rc && loop <= max_loop &&
  5571. (sys_mode == FW_SYS_MODE_BOOT)) {
  5572. loop++;
  5573. usleep_range(9000, pause * 1000);
  5574. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5575. pt_debug(cd->dev, DL_DEBUG,
  5576. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5577. __func__, loop * pause, sys_mode);
  5578. }
  5579. *actual_wait = (int)(loop * pause);
  5580. pt_debug(cd->dev, DL_WARN,
  5581. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5582. __func__, *actual_wait, sys_mode);
  5583. if (rc)
  5584. sys_mode = FW_SYS_MODE_UNDEFINED;
  5585. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5586. sys_mode == FW_SYS_MODE_UNDEFINED)
  5587. rc = -EBUSY;
  5588. }
  5589. mutex_lock(&cd->system_lock);
  5590. cd->fw_system_mode = sys_mode;
  5591. mutex_unlock(&cd->system_lock);
  5592. return rc;
  5593. }
  5594. /*******************************************************************************
  5595. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5596. *
  5597. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5598. * access to the DUT.
  5599. *
  5600. * RETURN:
  5601. * 0 = success
  5602. * !0 = failure
  5603. *
  5604. * PARAMETERS:
  5605. * *cd - pointer to core data
  5606. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5607. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5608. ******************************************************************************/
  5609. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5610. int *actual_wait)
  5611. {
  5612. int rc;
  5613. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5614. if (rc < 0) {
  5615. pt_debug(cd->dev, DL_ERROR,
  5616. "%s: fail get exclusive ex=%p own=%p\n",
  5617. __func__, cd->exclusive_dev, cd->dev);
  5618. return rc;
  5619. }
  5620. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5621. if (release_exclusive(cd, cd->dev) < 0)
  5622. pt_debug(cd->dev, DL_ERROR,
  5623. "%s: fail to release exclusive\n", __func__);
  5624. return rc;
  5625. }
  5626. /*******************************************************************************
  5627. * FUNCTION: _pt_get_fw_sys_mode
  5628. *
  5629. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5630. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5631. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5632. * information but any PIP command above 0x1F requires scanning to be disabled
  5633. * before it will be operational. If scanning was not disabled before sending
  5634. * these PIP commands the FW will respond with a 6 byte error response. So to
  5635. * safely determine the scanning state, a PIP message that will not affect the
  5636. * operation of the FW was chosen:
  5637. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5638. * received scanning is active.
  5639. *
  5640. * RETURN:
  5641. * 0 = success
  5642. * !0 = failure
  5643. *
  5644. * PARAMETERS:
  5645. * *cd - pointer to core data
  5646. * *sys_mode - pointer to FW System mode
  5647. * *mode - pointer to mode (BL/FW)
  5648. ******************************************************************************/
  5649. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5650. {
  5651. int write_length = 1;
  5652. int report_length;
  5653. int rc = 0;
  5654. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5655. u8 tmp_mode = PT_MODE_UNKNOWN;
  5656. u8 param[1] = { PT_TCH_PARM_EBID };
  5657. struct pt_hid_output hid_output = {
  5658. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5659. .write_length = write_length,
  5660. .write_buf = param,
  5661. .novalidate = true,
  5662. };
  5663. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5664. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5665. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5666. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5667. __func__, tmp_sys_mode, tmp_mode);
  5668. if (!rc) {
  5669. if (tmp_mode != PT_MODE_OPERATIONAL)
  5670. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5671. }
  5672. goto exit;
  5673. }
  5674. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5675. if (cd->mode != PT_MODE_OPERATIONAL) {
  5676. tmp_mode = cd->mode;
  5677. goto exit;
  5678. }
  5679. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5680. if (rc)
  5681. goto exit;
  5682. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5683. if ((report_length == 0x06) &&
  5684. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5685. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5686. tmp_mode = PIP2_STATUS_APP_EXEC;
  5687. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5688. } else if ((report_length == 0x0A) &&
  5689. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5690. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5691. tmp_mode = PIP2_STATUS_APP_EXEC;
  5692. tmp_sys_mode = FW_SYS_MODE_TEST;
  5693. }
  5694. exit:
  5695. if (mode)
  5696. *mode = tmp_mode;
  5697. if (sys_mode)
  5698. *sys_mode = tmp_sys_mode;
  5699. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5700. __func__, tmp_mode, tmp_sys_mode);
  5701. return rc;
  5702. }
  5703. /*******************************************************************************
  5704. * FUNCTION: pt_get_fw_sys_mode
  5705. *
  5706. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5707. * is active or not.
  5708. *
  5709. * RETURN:
  5710. * 0 = success
  5711. * !0 = failure
  5712. *
  5713. * PARAMETERS:
  5714. * *cd - pointer to core data
  5715. * *sys_mode - pointer to fw system mode
  5716. * *mode - pointer to mode
  5717. ******************************************************************************/
  5718. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5719. {
  5720. int rc;
  5721. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5722. if (rc < 0) {
  5723. pt_debug(cd->dev, DL_ERROR,
  5724. "%s: fail get exclusive ex=%p own=%p\n",
  5725. __func__, cd->exclusive_dev, cd->dev);
  5726. return rc;
  5727. }
  5728. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5729. if (release_exclusive(cd, cd->dev) < 0)
  5730. pt_debug(cd->dev, DL_ERROR,
  5731. "%s: fail to release exclusive\n", __func__);
  5732. return rc;
  5733. }
  5734. /*******************************************************************************
  5735. * FUNCTION: _pt_request_get_fw_sys_mode
  5736. *
  5737. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5738. * to request to get scan state
  5739. *
  5740. * RETURN:
  5741. * 0 = success
  5742. * !0 = failure
  5743. *
  5744. * PARAMETERS:
  5745. * *dev - pointer to device structure
  5746. * protect - flag to call protected or non-protected
  5747. * *sys_mode - pointer to FW system mode
  5748. * *mode - pointer to mode
  5749. ******************************************************************************/
  5750. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5751. u8 *sys_mode, u8 *mode)
  5752. {
  5753. struct pt_core_data *cd = dev_get_drvdata(dev);
  5754. if (protect)
  5755. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5756. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5757. }
  5758. /* Default hid descriptor to provide basic register map */
  5759. const struct pt_hid_desc hid_desc_default = {
  5760. 230, /* hid_desc_len */
  5761. HID_APP_REPORT_ID, /* packet_id */
  5762. 0x00, /* reserved_byte */
  5763. 0x0100, /* bcd_version */
  5764. 0x00EC, /* report_desc_len */
  5765. 0x0002, /* report_desc_register */
  5766. 0x0003, /* input_register */
  5767. 0x00FE, /* max_input_len */
  5768. 0x0004, /* output_register */
  5769. 0x00FE, /* max_output_len */
  5770. 0x0005, /* command_register */
  5771. 0x0006, /* data_register */
  5772. 0x04B4, /* vendor_id */
  5773. 0xC101, /* product_id */
  5774. 0x0100, /* version_id */
  5775. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5776. };
  5777. /*******************************************************************************
  5778. * FUNCTION: pt_init_hid_descriptor
  5779. *
  5780. * SUMMARY: Setup default values for HID descriptor structure
  5781. *
  5782. *
  5783. * PARAMETERS:
  5784. * *desc - pointer to the HID descriptor data read back from DUT
  5785. ******************************************************************************/
  5786. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5787. {
  5788. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5789. }
  5790. /*******************************************************************************
  5791. * FUNCTION: pt_get_hid_descriptor_
  5792. *
  5793. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5794. * into the HID descriptor structure
  5795. *
  5796. * RETURN:
  5797. * 0 = success
  5798. * !0 = failure
  5799. *
  5800. * PARAMETERS:
  5801. * *cd - pointer to core data
  5802. * *desc - pointer to the HID descriptor data read back from DUT
  5803. ******************************************************************************/
  5804. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5805. struct pt_hid_desc *desc)
  5806. {
  5807. struct device *dev = cd->dev;
  5808. int rc = 0;
  5809. int t;
  5810. u8 cmd[2];
  5811. /*
  5812. * During startup the HID descriptor is required for all future
  5813. * processing. If IRQ is already asserted due to an early touch report
  5814. * the report must be cleared before sending command.
  5815. */
  5816. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5817. /* Read HID descriptor length and version */
  5818. mutex_lock(&cd->system_lock);
  5819. cd->hid_cmd_state = 1;
  5820. mutex_unlock(&cd->system_lock);
  5821. /* Set HID descriptor register */
  5822. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5823. sizeof(cd->hid_core.hid_desc_register));
  5824. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5825. __func__, sizeof(cmd));
  5826. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5827. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5828. if (rc) {
  5829. pt_debug(dev, DL_ERROR,
  5830. "%s: failed to get HID descriptor, rc=%d\n",
  5831. __func__, rc);
  5832. goto error;
  5833. }
  5834. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5835. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5836. if (IS_TMO(t)) {
  5837. #ifdef TTDL_DIAGNOSTICS
  5838. cd->bus_transmit_error_count++;
  5839. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5840. #endif /* TTDL_DIAGNOSTICS */
  5841. pt_debug(cd->dev, DL_ERROR,
  5842. "%s: HID get descriptor timed out\n", __func__);
  5843. rc = -ETIME;
  5844. goto error;
  5845. } else {
  5846. cd->hw_detected = true;
  5847. }
  5848. /* Load the HID descriptor including all registers */
  5849. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5850. /* Check HID descriptor length and version */
  5851. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5852. le16_to_cpu(desc->hid_desc_len),
  5853. le16_to_cpu(desc->bcd_version));
  5854. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5855. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5856. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5857. __func__);
  5858. return -ENODEV;
  5859. }
  5860. goto exit;
  5861. error:
  5862. mutex_lock(&cd->system_lock);
  5863. cd->hid_cmd_state = 0;
  5864. mutex_unlock(&cd->system_lock);
  5865. exit:
  5866. return rc;
  5867. }
  5868. /*******************************************************************************
  5869. * FUNCTION: pt_get_hid_descriptor
  5870. *
  5871. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5872. *
  5873. * RETURN:
  5874. * 0 = success
  5875. * !0 = failure
  5876. *
  5877. * PARAMETERS:
  5878. * *cd - pointer to core data
  5879. * *desc - pointer to the HID descriptor data read back from DUT
  5880. ******************************************************************************/
  5881. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5882. struct pt_hid_desc *desc)
  5883. {
  5884. int rc;
  5885. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5886. if (rc < 0) {
  5887. pt_debug(cd->dev, DL_ERROR,
  5888. "%s: fail get exclusive ex=%p own=%p\n",
  5889. __func__, cd->exclusive_dev, cd->dev);
  5890. return rc;
  5891. }
  5892. rc = pt_get_hid_descriptor_(cd, desc);
  5893. if (release_exclusive(cd, cd->dev) < 0)
  5894. pt_debug(cd->dev, DL_ERROR,
  5895. "%s: fail to release exclusive\n", __func__);
  5896. return rc;
  5897. }
  5898. /*******************************************************************************
  5899. * FUNCTION: pt_pip2_get_version_
  5900. *
  5901. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5902. * cd-ttdata
  5903. *
  5904. * RETURN:
  5905. * 0 = success
  5906. * !0 = failure
  5907. *
  5908. * PARAMETERS:
  5909. * *cd - pointer to core data
  5910. ******************************************************************************/
  5911. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5912. {
  5913. int rc = 0;
  5914. int status;
  5915. u8 read_buf[64];
  5916. u16 actual_read_len;
  5917. rc = _pt_request_pip2_send_cmd(cd->dev,
  5918. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5919. NULL, 0, read_buf, &actual_read_len);
  5920. if (rc) {
  5921. pt_debug(cd->dev, DL_ERROR,
  5922. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5923. __func__, rc);
  5924. return rc;
  5925. }
  5926. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5927. if (status == 0) {
  5928. /* Parse the PIP2 VERSION response into ttdata */
  5929. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5930. } else {
  5931. pt_debug(cd->dev, DL_ERROR,
  5932. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5933. __func__, status);
  5934. return status;
  5935. }
  5936. return rc;
  5937. }
  5938. /*******************************************************************************
  5939. * FUNCTION: pt_pip2_get_version
  5940. *
  5941. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5942. * access to the DUT.
  5943. *
  5944. * RETURN:
  5945. * 0 = success
  5946. * !0 = failure
  5947. *
  5948. * PARAMETERS:
  5949. * *cd - pointer to core data structure
  5950. ******************************************************************************/
  5951. static int pt_pip2_get_version(struct pt_core_data *cd)
  5952. {
  5953. int rc;
  5954. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5955. if (rc < 0) {
  5956. pt_debug(cd->dev, DL_ERROR,
  5957. "%s: fail get exclusive ex=%p own=%p\n",
  5958. __func__, cd->exclusive_dev, cd->dev);
  5959. return rc;
  5960. }
  5961. rc = pt_pip2_get_version_(cd);
  5962. if (release_exclusive(cd, cd->dev) < 0)
  5963. pt_debug(cd->dev, DL_ERROR,
  5964. "%s: fail to release exclusive\n", __func__);
  5965. return rc;
  5966. }
  5967. /*******************************************************************************
  5968. * FUNCTION: _pt_request_active_pip_protocol
  5969. *
  5970. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5971. * Function will return PIP version of BL or application based on
  5972. * when it's called.
  5973. *
  5974. * RETURN:
  5975. * 0 = success
  5976. * !0 = failure
  5977. *
  5978. * PARAMETERS:
  5979. * *dev - pointer to device structure
  5980. * protect - flag to run in protected mode
  5981. * *pip_version_major - pointer to store PIP major version
  5982. * *pip_version_minor - pointer to store PIP minor version
  5983. ******************************************************************************/
  5984. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5985. u8 *pip_version_major, u8 *pip_version_minor)
  5986. {
  5987. struct pt_core_data *cd = dev_get_drvdata(dev);
  5988. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5989. int rc = 0;
  5990. struct pt_hid_output sys_info = {
  5991. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5992. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5993. };
  5994. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5995. /* Skip PIP2 command if DUT generation is confirmed */
  5996. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5997. goto skip_pip2_command;
  5998. rc = pt_pip2_get_version_(cd);
  5999. if (!rc) {
  6000. *pip_version_major = ttdata->pip_ver_major;
  6001. *pip_version_minor = ttdata->pip_ver_minor;
  6002. pt_debug(dev, DL_INFO,
  6003. "%s: pip_version = %d.%d\n", __func__,
  6004. *pip_version_major, *pip_version_minor);
  6005. } else {
  6006. /*
  6007. * Legacy products do not support the pip2 protocol to get
  6008. * pip version. However, they do support the "get sysinfo"
  6009. * command to get pip version from FW, but the bootloader
  6010. * does not support it. This function will try "get sysinfo"
  6011. * command if the pip2 command failed but this cmd could also
  6012. * fail if DUT is stuck in bootloader mode.
  6013. */
  6014. pt_debug(dev, DL_INFO,
  6015. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6016. __func__, rc);
  6017. skip_pip2_command:
  6018. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6019. if (!rc) {
  6020. *pip_version_minor =
  6021. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6022. *pip_version_major =
  6023. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6024. pt_debug(dev, DL_INFO,
  6025. "%s: pip_version = %d.%d\n", __func__,
  6026. *pip_version_major, *pip_version_minor);
  6027. } else {
  6028. *pip_version_major = 0;
  6029. *pip_version_minor = 0;
  6030. pt_debug(dev, DL_ERROR,
  6031. "%s: pip_version Not Detected\n", __func__);
  6032. }
  6033. }
  6034. return rc;
  6035. }
  6036. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6037. /*******************************************************************************
  6038. * FUNCTION: _pt_detect_dut_generation
  6039. *
  6040. * SUMMARY: Determine the generation of device that we are communicating with:
  6041. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6042. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6043. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6044. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6045. * coming form the BL or the FW:
  6046. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6047. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6048. * This function will return a modified status if it detects the DUT
  6049. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6050. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6051. *
  6052. * RETURN:
  6053. * 0 = success
  6054. * !0 = failure
  6055. *
  6056. * PARAMETERS:
  6057. * *dev - pointer to device structure
  6058. * *status - pointer to status bitmask
  6059. * *dut_gen - pointer to store the dut_generation
  6060. * *mode - pointer to store the PT_MODE
  6061. ******************************************************************************/
  6062. static int _pt_detect_dut_generation(struct device *dev,
  6063. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6064. {
  6065. int rc = 0;
  6066. u8 dut_gen_tmp = DUT_UNKNOWN;
  6067. u8 mode_tmp = PT_MODE_UNKNOWN;
  6068. u8 attempt = 1;
  6069. u32 status_tmp = STARTUP_STATUS_START;
  6070. struct pt_core_data *cd = dev_get_drvdata(dev);
  6071. struct pt_hid_desc hid_desc;
  6072. memset(&hid_desc, 0, sizeof(hid_desc));
  6073. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6074. while (rc && attempt < 3) {
  6075. attempt++;
  6076. usleep_range(2000, 5000);
  6077. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6078. }
  6079. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6080. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6081. mode_tmp = PT_MODE_BOOTLOADER;
  6082. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6083. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6084. rc = pt_pip2_get_version_(cd);
  6085. if (!rc)
  6086. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6087. else
  6088. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6089. mode_tmp = PT_MODE_OPERATIONAL;
  6090. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6091. rc = 0; /* To return success instead of error code */
  6092. } else if (rc) {
  6093. rc = pt_pip2_get_version_(cd);
  6094. if (!rc) {
  6095. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6096. mode_tmp = PT_MODE_BOOTLOADER;
  6097. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6098. }
  6099. }
  6100. mutex_lock(&cd->system_lock);
  6101. if (dut_gen)
  6102. *dut_gen = dut_gen_tmp;
  6103. if (mode)
  6104. *mode = mode_tmp;
  6105. if (status)
  6106. *status = status_tmp;
  6107. mutex_unlock(&cd->system_lock);
  6108. #ifdef TTDL_DIAGNOSTICS
  6109. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6110. __func__, dut_gen_tmp, mode_tmp);
  6111. #endif /* TTDL_DIAGNOSTICS */
  6112. return rc;
  6113. }
  6114. /*******************************************************************************
  6115. * FUNCTION: _pt_request_dut_generation
  6116. *
  6117. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6118. * to get current dut generation.
  6119. *
  6120. * NOTE: This function WILL NOT try to determine dut generation.
  6121. *
  6122. * RETURN:
  6123. * The current dut generation.
  6124. *
  6125. * PARAMETERS:
  6126. * *dev - pointer to device structure
  6127. ******************************************************************************/
  6128. static int _pt_request_dut_generation(struct device *dev)
  6129. {
  6130. struct pt_core_data *cd = dev_get_drvdata(dev);
  6131. return cd->active_dut_generation;
  6132. }
  6133. #define HW_VERSION_LEN_MAX 13
  6134. /*******************************************************************************
  6135. * FUNCTION: _legacy_generate_hw_version
  6136. *
  6137. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6138. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6139. * command) to the hw_version.
  6140. *
  6141. * RETURN:
  6142. * 0 = success
  6143. * !0 = failure
  6144. *
  6145. * PARAMETERS:
  6146. * *cd - pointer to core data
  6147. * *hw_version - pointer to store the hardware version
  6148. ******************************************************************************/
  6149. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6150. char *hw_version)
  6151. {
  6152. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6153. if (cd->sysinfo.ready) {
  6154. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6155. ttdata->jtag_id_h, cd->pid_for_loader);
  6156. return 0;
  6157. } else if (cd->bl_info.ready) {
  6158. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6159. cd->bl_info.chip_id, cd->pid_for_loader);
  6160. return 0;
  6161. } else {
  6162. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6163. pt_debug(cd->dev, DL_ERROR,
  6164. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6165. return -ENODATA;
  6166. }
  6167. }
  6168. /*******************************************************************************
  6169. * FUNCTION: _pip2_generate_hw_version
  6170. *
  6171. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6172. * VERSION command) to the hw_version.
  6173. *
  6174. * RETURN:
  6175. * 0 = success
  6176. * !0 = failure
  6177. *
  6178. * PARAMETERS:
  6179. * *cd - pointer to core data
  6180. * *hw_version - pointer to store the hardware version
  6181. ******************************************************************************/
  6182. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6183. {
  6184. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6185. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6186. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6187. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6188. return 0;
  6189. } else {
  6190. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6191. pt_debug(cd->dev, DL_ERROR,
  6192. "%s: PIP Version are not ready\n", __func__);
  6193. return -ENODATA;
  6194. }
  6195. }
  6196. /*******************************************************************************
  6197. * FUNCTION: pt_generate_hw_version
  6198. *
  6199. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6200. * hw_version from static data.
  6201. *
  6202. * RETURN:
  6203. * 0 = success
  6204. * !0 = failure
  6205. *
  6206. * PARAMETERS:
  6207. * *cd - pointer to core data
  6208. * *hw_version - pointer to store the hardware version
  6209. ******************************************************************************/
  6210. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6211. {
  6212. int rc = 0;
  6213. if (!hw_version)
  6214. return -ENOMEM;
  6215. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6216. rc = _legacy_generate_hw_version(cd, hw_version);
  6217. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6218. rc = _pip2_generate_hw_version(cd, hw_version);
  6219. else {
  6220. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6221. rc = -ENODATA;
  6222. }
  6223. return rc;
  6224. }
  6225. /*******************************************************************************
  6226. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6227. *
  6228. * NOTE: The calling function must ensure to free *hw_version
  6229. *
  6230. * RETURN:
  6231. * 0 = success
  6232. * !0 = Failure
  6233. *
  6234. * PARAMETERS:
  6235. * *dev - pointer to device structure
  6236. * *hw_version - pointer to where the hw_version string will be stored
  6237. ******************************************************************************/
  6238. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6239. {
  6240. int rc = 0;
  6241. u16 actual_read_len;
  6242. u16 pip_ver;
  6243. u8 rd_buf[256];
  6244. u8 status;
  6245. u8 index = PIP2_RESP_STATUS_OFFSET;
  6246. u8 return_data[8];
  6247. u8 panel_id;
  6248. struct pt_core_data *cd = dev_get_drvdata(dev);
  6249. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6250. if (!hw_version)
  6251. return -ENOMEM;
  6252. if (!cd->hw_detected) {
  6253. /* No HW detected */
  6254. rc = -ENODEV;
  6255. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6256. __func__);
  6257. goto exit_error;
  6258. }
  6259. memset(return_data, 0, ARRAY_SIZE(return_data));
  6260. /* For Parade TC or TT parts */
  6261. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6262. rc = _pt_request_pip2_send_cmd(dev,
  6263. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6264. NULL, 0, rd_buf, &actual_read_len);
  6265. if (rc) {
  6266. pt_debug(dev, DL_ERROR,
  6267. "%s: Failed to send PIP2 VERSION cmd\n",
  6268. __func__);
  6269. goto exit_error;
  6270. }
  6271. status = rd_buf[index];
  6272. if (status == 0) {
  6273. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6274. /*
  6275. * BL PIP 2.02 and greater the version fields are
  6276. * swapped
  6277. */
  6278. if (pip_ver >= 0x0202) {
  6279. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6280. "%02X%02X.%02X%02X.FF",
  6281. rd_buf[index + 10], rd_buf[index + 9],
  6282. rd_buf[index + 8], rd_buf[index + 7]);
  6283. } else {
  6284. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6285. "%02X%02X.%02X%02X.FF",
  6286. rd_buf[index + 8], rd_buf[index + 7],
  6287. rd_buf[index + 10], rd_buf[index + 9]);
  6288. }
  6289. return STATUS_SUCCESS;
  6290. } else {
  6291. rc = status;
  6292. pt_debug(dev, DL_WARN,
  6293. "%s: PIP2 VERSION cmd response error\n",
  6294. __func__);
  6295. }
  6296. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6297. /*
  6298. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6299. * hard coded to FFFF
  6300. */
  6301. if (cd->mode == PT_MODE_OPERATIONAL) {
  6302. rc = pt_hid_output_get_sysinfo(cd);
  6303. if (!rc) {
  6304. panel_id =
  6305. cd->sysinfo.sensing_conf_data.panel_id;
  6306. } else {
  6307. panel_id = PANEL_ID_NOT_ENABLED;
  6308. }
  6309. /* In FW - simply retrieve from ttdata struct */
  6310. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6311. "%04X.FFFF.%02X",
  6312. ttdata->jtag_id_h,
  6313. panel_id);
  6314. return STATUS_SUCCESS;
  6315. } else {
  6316. /*
  6317. * Return the stored value if PT_PANEL_ID_BY_BL
  6318. * is not supported while other feature is.
  6319. */
  6320. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6321. rc = pt_hid_output_bl_get_information(
  6322. cd, return_data);
  6323. if (!rc) {
  6324. rc = pt_hid_output_bl_get_panel_id(
  6325. cd, &panel_id);
  6326. }
  6327. } else
  6328. panel_id = cd->pid_for_loader;
  6329. if (!rc) {
  6330. snprintf(hw_version,
  6331. HW_VERSION_LEN_MAX,
  6332. "%02X%02X.FFFF.%02X",
  6333. return_data[3], return_data[2],
  6334. panel_id);
  6335. return STATUS_SUCCESS;
  6336. }
  6337. }
  6338. } else {
  6339. /* Unknown generation */
  6340. rc = -ENODEV;
  6341. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6342. __func__);
  6343. }
  6344. exit_error:
  6345. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6346. return rc;
  6347. }
  6348. /*******************************************************************************
  6349. * FUNCTION: pt_start_wd_timer
  6350. *
  6351. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6352. *
  6353. * RETURN: void
  6354. *
  6355. * PARAMETERS:
  6356. * *cd - pointer to core data
  6357. ******************************************************************************/
  6358. static void pt_start_wd_timer(struct pt_core_data *cd)
  6359. {
  6360. if (cd->watchdog_interval < 100) {
  6361. pt_debug(cd->dev, DL_ERROR,
  6362. "%s: WARNING: Invalid watchdog interval: %d\n",
  6363. __func__, cd->watchdog_interval);
  6364. return;
  6365. }
  6366. if (cd->watchdog_force_stop) {
  6367. pt_debug(cd->dev, DL_INFO,
  6368. "%s: TTDL WD Forced stop\n", __func__);
  6369. return;
  6370. }
  6371. mod_timer(&cd->watchdog_timer, jiffies +
  6372. msecs_to_jiffies(cd->watchdog_interval));
  6373. cd->watchdog_enabled = 1;
  6374. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6375. }
  6376. /*******************************************************************************
  6377. * FUNCTION: pt_stop_wd_timer
  6378. *
  6379. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6380. *
  6381. * RETURN: void
  6382. *
  6383. * PARAMETERS:
  6384. * *cd - pointer to core data
  6385. ******************************************************************************/
  6386. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6387. {
  6388. if (!cd->watchdog_interval)
  6389. return;
  6390. /*
  6391. * Ensure we wait until the watchdog timer
  6392. * running on a different CPU finishes
  6393. */
  6394. del_timer_sync(&cd->watchdog_timer);
  6395. cancel_work_sync(&cd->watchdog_work);
  6396. del_timer_sync(&cd->watchdog_timer);
  6397. cd->watchdog_enabled = 0;
  6398. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6399. }
  6400. /*******************************************************************************
  6401. * FUNCTION: pt_hw_soft_reset
  6402. *
  6403. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6404. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6405. * schedule an enum if it fires when the DUT is being reset.
  6406. * This can cause a double reset.
  6407. *
  6408. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6409. * the WD active could cause this function to fail!
  6410. *
  6411. * RETURN:
  6412. * 0 = success
  6413. * !0 = failure
  6414. *
  6415. * PARAMETERS:
  6416. * *cd - pointer to core data struct
  6417. * protect - flag to call protected or non-protected
  6418. ******************************************************************************/
  6419. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6420. {
  6421. int rc = 0;
  6422. mutex_lock(&cd->system_lock);
  6423. cd->startup_status = STARTUP_STATUS_START;
  6424. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6425. mutex_unlock(&cd->system_lock);
  6426. if (protect)
  6427. rc = pt_hid_cmd_reset(cd);
  6428. else
  6429. rc = pt_hid_cmd_reset_(cd);
  6430. if (rc < 0) {
  6431. pt_debug(cd->dev, DL_ERROR,
  6432. "%s: FAILED to execute SOFT reset\n", __func__);
  6433. return rc;
  6434. }
  6435. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6436. __func__);
  6437. return 0;
  6438. }
  6439. /*******************************************************************************
  6440. * FUNCTION: pt_hw_hard_reset
  6441. *
  6442. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6443. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6444. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6445. * schedule an enum if it fires when the DUT is being reset.
  6446. * This can cause a double reset.
  6447. *
  6448. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6449. * the WD active could cause this function to fail!
  6450. *
  6451. * RETURN:
  6452. * 0 = success
  6453. * !0 = failure
  6454. *
  6455. * PARAMETERS:
  6456. * *cd - pointer to core data struct
  6457. ******************************************************************************/
  6458. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6459. {
  6460. if (cd->cpdata->xres) {
  6461. cd->startup_status = STARTUP_STATUS_START;
  6462. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6463. __func__);
  6464. cd->cpdata->xres(cd->cpdata, cd->dev);
  6465. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6466. __func__);
  6467. return 0;
  6468. }
  6469. pt_debug(cd->dev, DL_ERROR,
  6470. "%s: FAILED to execute HARD reset\n", __func__);
  6471. return -ENODEV;
  6472. }
  6473. /*******************************************************************************
  6474. * FUNCTION: pt_dut_reset
  6475. *
  6476. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6477. * soft reset.
  6478. *
  6479. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6480. * the WD active could cause this function to fail!
  6481. * NOTE: "protect" flag is only used for soft reset.
  6482. *
  6483. * RETURN:
  6484. * 0 = success
  6485. * !0 = failure
  6486. *
  6487. * PARAMETERS:
  6488. * *cd - pointer to core data structure
  6489. * protect - flag to call protected or non-protected
  6490. ******************************************************************************/
  6491. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6492. {
  6493. int rc = 0;
  6494. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6495. mutex_lock(&cd->system_lock);
  6496. cd->hid_reset_cmd_state = 1;
  6497. rc = pt_hw_hard_reset(cd);
  6498. mutex_unlock(&cd->system_lock);
  6499. if (rc == -ENODEV) {
  6500. mutex_lock(&cd->system_lock);
  6501. cd->hid_reset_cmd_state = 0;
  6502. mutex_unlock(&cd->system_lock);
  6503. pt_debug(cd->dev, DL_ERROR,
  6504. "%s: Hard reset failed, try soft reset\n", __func__);
  6505. rc = pt_hw_soft_reset(cd, protect);
  6506. }
  6507. if (rc)
  6508. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6509. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6510. return rc;
  6511. }
  6512. /*******************************************************************************
  6513. * FUNCTION: pt_dut_reset_and_wait
  6514. *
  6515. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6516. * complete
  6517. *
  6518. * RETURN:
  6519. * 0 = success
  6520. * !0 = failure
  6521. *
  6522. * PARAMETERS:
  6523. * *cd - pointer to core data structure
  6524. ******************************************************************************/
  6525. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6526. {
  6527. int rc = 0;
  6528. int t;
  6529. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6530. if (rc < 0)
  6531. goto exit;
  6532. t = wait_event_timeout(cd->wait_q,
  6533. (cd->hid_reset_cmd_state == 0),
  6534. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6535. if (IS_TMO(t)) {
  6536. #ifdef TTDL_DIAGNOSTICS
  6537. cd->bus_transmit_error_count++;
  6538. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6539. #endif /* TTDL_DIAGNOSTICS */
  6540. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6541. __func__);
  6542. rc = -ETIME;
  6543. goto exit;
  6544. }
  6545. exit:
  6546. return rc;
  6547. }
  6548. /*
  6549. * touch default parameters (from report descriptor) to resolve protocol for
  6550. * touch report
  6551. */
  6552. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6553. /* byte offset, size, min, max, bit offset, report */
  6554. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6555. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6556. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6557. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6558. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6559. };
  6560. /*
  6561. * button default parameters (from report descriptor) to resolve protocol for
  6562. * button report
  6563. */
  6564. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6565. /* byte offset, size, min, max, bit offset, report */
  6566. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6567. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6568. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6569. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6570. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6571. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6572. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6573. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6574. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6575. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6576. };
  6577. /*******************************************************************************
  6578. * FUNCTION: pt_init_pip_report_fields
  6579. *
  6580. * SUMMARY: Setup default values for touch/button report parsing.
  6581. *
  6582. * PARAMETERS:
  6583. * *cd - pointer to core data structure
  6584. ******************************************************************************/
  6585. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6586. {
  6587. struct pt_sysinfo *si = &cd->sysinfo;
  6588. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6589. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6590. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6591. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6592. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6593. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6594. cd->features.easywake = 1;
  6595. cd->features.noise_metric = 1;
  6596. cd->features.tracking_heatmap = 1;
  6597. cd->features.sensor_data = 1;
  6598. }
  6599. /*******************************************************************************
  6600. * FUNCTION: pt_get_mode
  6601. *
  6602. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6603. * message
  6604. *
  6605. * RETURN: Enum of the current mode
  6606. *
  6607. * PARAMETERS:
  6608. * *cd - pointer to the Core Data structure
  6609. * protect - run command in protected mode
  6610. * *mode - pointer to store the retrieved mode
  6611. ******************************************************************************/
  6612. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6613. {
  6614. if (desc->packet_id == HID_APP_REPORT_ID)
  6615. return PT_MODE_OPERATIONAL;
  6616. else if (desc->packet_id == HID_BL_REPORT_ID)
  6617. return PT_MODE_BOOTLOADER;
  6618. return PT_MODE_UNKNOWN;
  6619. }
  6620. /*******************************************************************************
  6621. * FUNCTION: _pt_request_get_mode
  6622. *
  6623. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6624. * to determine the current mode of the DUT by use of the Get HID
  6625. * Descriptor command.
  6626. *
  6627. * RETURN:
  6628. * 0 = success
  6629. * !0 = failure
  6630. *
  6631. * PARAMETERS:
  6632. * *dev - pointer to device structure
  6633. * protect - run command in protected mode
  6634. * *mode - pointer to store the retrieved mode
  6635. ******************************************************************************/
  6636. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6637. {
  6638. struct pt_core_data *cd = dev_get_drvdata(dev);
  6639. struct pt_hid_desc hid_desc;
  6640. int rc = 0;
  6641. memset(&hid_desc, 0, sizeof(hid_desc));
  6642. if (protect)
  6643. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6644. else
  6645. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6646. if (rc)
  6647. *mode = PT_MODE_UNKNOWN;
  6648. else
  6649. *mode = pt_get_mode(cd, &hid_desc);
  6650. return rc;
  6651. }
  6652. /*******************************************************************************
  6653. * FUNCTION: pt_queue_enum_
  6654. *
  6655. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6656. * function. It won't try to add/delete sysfs node or modules.
  6657. *
  6658. * RETURN: void
  6659. *
  6660. * PARAMETERS:
  6661. * *cd - pointer to core data
  6662. ******************************************************************************/
  6663. static void pt_queue_enum_(struct pt_core_data *cd)
  6664. {
  6665. if (cd->startup_state == STARTUP_NONE) {
  6666. cd->startup_state = STARTUP_QUEUED;
  6667. #ifdef TTDL_DIAGNOSTICS
  6668. if (!cd->bridge_mode)
  6669. schedule_work(&cd->enum_work);
  6670. else
  6671. cd->startup_state = STARTUP_NONE;
  6672. #else
  6673. schedule_work(&cd->enum_work);
  6674. #endif
  6675. pt_debug(cd->dev, DL_INFO,
  6676. "%s: enum_work queued\n", __func__);
  6677. } else {
  6678. pt_debug(cd->dev, DL_WARN,
  6679. "%s: Enum not queued - startup_state = %d\n",
  6680. __func__, cd->startup_state);
  6681. }
  6682. }
  6683. /*******************************************************************************
  6684. * FUNCTION: pt_queue_enum
  6685. *
  6686. * SUMMARY: Queues a TTDL enum within a mutex lock
  6687. *
  6688. * RETURN: void
  6689. *
  6690. * PARAMETERS:
  6691. * *cd - pointer to core data
  6692. ******************************************************************************/
  6693. static void pt_queue_enum(struct pt_core_data *cd)
  6694. {
  6695. mutex_lock(&cd->system_lock);
  6696. pt_queue_enum_(cd);
  6697. mutex_unlock(&cd->system_lock);
  6698. }
  6699. static void remove_sysfs_and_modules(struct device *dev);
  6700. /*******************************************************************************
  6701. * FUNCTION: pt_queue_restart
  6702. *
  6703. * SUMMARY: Queues a TTDL restart within a mutex lock
  6704. *
  6705. * RETURN: void
  6706. *
  6707. * PARAMETERS:
  6708. * *cd - pointer to core data
  6709. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6710. * False: will not perform remove action
  6711. ******************************************************************************/
  6712. static void pt_queue_restart(struct pt_core_data *cd)
  6713. {
  6714. mutex_lock(&cd->system_lock);
  6715. if (cd->startup_state == STARTUP_NONE) {
  6716. cd->startup_state = STARTUP_QUEUED;
  6717. schedule_work(&cd->ttdl_restart_work);
  6718. pt_debug(cd->dev, DL_INFO,
  6719. "%s: pt_ttdl_restart queued\n", __func__);
  6720. } else {
  6721. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6722. __func__, cd->startup_state);
  6723. }
  6724. mutex_unlock(&cd->system_lock);
  6725. }
  6726. /*******************************************************************************
  6727. * FUNCTION: call_atten_cb
  6728. *
  6729. * SUMMARY: Iterate over attention list call the function that registered.
  6730. *
  6731. * RETURN: void
  6732. *
  6733. * PARAMETERS:
  6734. * *cd - pointer to core data
  6735. * type - type of attention list
  6736. * mode - condition for execution
  6737. ******************************************************************************/
  6738. static void call_atten_cb(struct pt_core_data *cd,
  6739. enum pt_atten_type type, int mode)
  6740. {
  6741. struct atten_node *atten, *atten_n;
  6742. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6743. __func__, type, mode);
  6744. spin_lock(&cd->spinlock);
  6745. list_for_each_entry_safe(atten, atten_n,
  6746. &cd->atten_list[type], node) {
  6747. if (!mode || atten->mode & mode) {
  6748. spin_unlock(&cd->spinlock);
  6749. pt_debug(cd->dev, DL_DEBUG,
  6750. "%s: attention for '%s'",
  6751. __func__, dev_name(atten->dev));
  6752. atten->func(atten->dev);
  6753. spin_lock(&cd->spinlock);
  6754. }
  6755. }
  6756. spin_unlock(&cd->spinlock);
  6757. }
  6758. /*******************************************************************************
  6759. * FUNCTION: start_fw_upgrade
  6760. *
  6761. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6762. * start firmware upgrade.
  6763. *
  6764. * RETURN:
  6765. * 0 = success
  6766. *
  6767. * PARAMETERS:
  6768. * *data - pointer to core data
  6769. ******************************************************************************/
  6770. static int start_fw_upgrade(void *data)
  6771. {
  6772. struct pt_core_data *cd = (struct pt_core_data *)data;
  6773. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6774. return 0;
  6775. }
  6776. /*******************************************************************************
  6777. * FUNCTION: pt_put_device_into_easy_wakeup_
  6778. *
  6779. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6780. * wake up state.
  6781. *
  6782. * RETURN:
  6783. * 0 = success
  6784. * !0 = failure
  6785. *
  6786. * PARAMETERS:
  6787. * *cd - pointer to core data
  6788. ******************************************************************************/
  6789. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6790. {
  6791. int rc = 0;
  6792. u8 status = 0;
  6793. rc = pt_hid_output_enter_easywake_state_(cd,
  6794. cd->easy_wakeup_gesture, &status);
  6795. if (rc || status == 0)
  6796. return -EBUSY;
  6797. return rc;
  6798. }
  6799. /*******************************************************************************
  6800. * FUNCTION: pt_put_device_into_deep_sleep_
  6801. *
  6802. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6803. *
  6804. * RETURN:
  6805. * 0 = success
  6806. * !0 = error
  6807. *
  6808. * PARAMETERS:
  6809. * *cd - pointer to core data
  6810. ******************************************************************************/
  6811. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6812. {
  6813. int rc = 0;
  6814. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6815. if (rc)
  6816. rc = -EBUSY;
  6817. return rc;
  6818. }
  6819. /*******************************************************************************
  6820. * FUNCTION: pt_put_device_into_deep_standby_
  6821. *
  6822. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6823. *
  6824. * RETURN:
  6825. * 0 = success
  6826. * !0 = error
  6827. *
  6828. * PARAMETERS:
  6829. * *cd - pointer to core data
  6830. ******************************************************************************/
  6831. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6832. {
  6833. int rc = 0;
  6834. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6835. if (rc)
  6836. rc = -EBUSY;
  6837. return rc;
  6838. }
  6839. /*******************************************************************************
  6840. * FUNCTION: pt_core_poweroff_device_
  6841. *
  6842. * SUMMARY: Disable IRQ and HW power down the device.
  6843. *
  6844. * RETURN:
  6845. * 0 = success
  6846. * !0 = failure
  6847. *
  6848. * PARAMETERS:
  6849. * *cd - pointer to core data
  6850. ******************************************************************************/
  6851. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6852. {
  6853. int rc;
  6854. if (cd->irq_enabled) {
  6855. cd->irq_enabled = false;
  6856. disable_irq_nosync(cd->irq);
  6857. }
  6858. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6859. if (rc < 0)
  6860. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6861. __func__, rc);
  6862. return rc;
  6863. }
  6864. /*******************************************************************************
  6865. * FUNCTION: pt_core_sleep_
  6866. *
  6867. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6868. * configuration in the core platform data structure.
  6869. *
  6870. * RETURN:
  6871. * 0 = success
  6872. * !0 = failure
  6873. *
  6874. * PARAMETERS:
  6875. * *cd - pointer to core data
  6876. ******************************************************************************/
  6877. static int pt_core_sleep_(struct pt_core_data *cd)
  6878. {
  6879. int rc = 0;
  6880. mutex_lock(&cd->system_lock);
  6881. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6882. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6883. mutex_unlock(&cd->system_lock);
  6884. pt_debug(cd->dev, DL_INFO,
  6885. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6886. return 0;
  6887. } else {
  6888. cd->sleep_state = SS_SLEEPING;
  6889. }
  6890. mutex_unlock(&cd->system_lock);
  6891. /* Ensure watchdog and startup works stopped */
  6892. pt_stop_wd_timer(cd);
  6893. cancel_work_sync(&cd->enum_work);
  6894. pt_stop_wd_timer(cd);
  6895. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6896. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6897. rc = pt_core_poweroff_device_(cd);
  6898. if (rc)
  6899. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6900. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6901. pt_debug(cd->dev, DL_INFO,
  6902. "%s: Entering into deep standby mode:\n", __func__);
  6903. rc = pt_put_device_into_deep_standby_(cd);
  6904. if (rc)
  6905. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6906. } else {
  6907. pt_debug(cd->dev, DL_INFO,
  6908. "%s: Entering into deep sleep mode:\n", __func__);
  6909. rc = pt_put_device_into_deep_sleep_(cd);
  6910. if (rc)
  6911. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6912. }
  6913. mutex_lock(&cd->system_lock);
  6914. cd->sleep_state = SS_SLEEP_ON;
  6915. mutex_unlock(&cd->system_lock);
  6916. return rc;
  6917. }
  6918. /*******************************************************************************
  6919. * FUNCTION: pt_core_easywake_on_
  6920. *
  6921. * SUMMARY: Suspend the device with easy wake on the
  6922. * configuration in the core platform data structure.
  6923. *
  6924. * RETURN:
  6925. * 0 = success
  6926. * !0 = failure
  6927. *
  6928. * PARAMETERS:
  6929. * *cd - pointer to core data
  6930. ******************************************************************************/
  6931. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6932. {
  6933. int rc = 0;
  6934. mutex_lock(&cd->system_lock);
  6935. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6936. mutex_unlock(&cd->system_lock);
  6937. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6938. __func__, cd->sleep_state);
  6939. return 0;
  6940. }
  6941. mutex_unlock(&cd->system_lock);
  6942. /* Ensure watchdog and startup works stopped */
  6943. pt_stop_wd_timer(cd);
  6944. cancel_work_sync(&cd->enum_work);
  6945. pt_stop_wd_timer(cd);
  6946. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6947. rc = pt_put_device_into_easy_wakeup_(cd);
  6948. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6949. if (rc)
  6950. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6951. }
  6952. mutex_lock(&cd->system_lock);
  6953. cd->sleep_state = SS_EASY_WAKING_ON;
  6954. mutex_unlock(&cd->system_lock);
  6955. return rc;
  6956. }
  6957. /*******************************************************************************
  6958. * FUNCTION: pt_core_easywake_on
  6959. *
  6960. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6961. *
  6962. * RETURN:
  6963. * 0 = success
  6964. * !0 = failure
  6965. *
  6966. * PARAMETERS:
  6967. * *cd - pointer to core data
  6968. ******************************************************************************/
  6969. static int pt_core_easywake_on(struct pt_core_data *cd)
  6970. {
  6971. int rc = 0;
  6972. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6973. if (rc < 0) {
  6974. pt_debug(cd->dev, DL_ERROR,
  6975. "%s: fail get exclusive ex=%p own=%p\n",
  6976. __func__, cd->exclusive_dev, cd->dev);
  6977. return rc;
  6978. }
  6979. rc = pt_core_easywake_on_(cd);
  6980. if (release_exclusive(cd, cd->dev) < 0)
  6981. pt_debug(cd->dev, DL_ERROR,
  6982. "%s: fail to release exclusive\n", __func__);
  6983. else
  6984. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6985. __func__);
  6986. return rc;
  6987. }
  6988. /*******************************************************************************
  6989. * FUNCTION: pt_core_sleep
  6990. *
  6991. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6992. *
  6993. * RETURN:
  6994. * 0 = success
  6995. * !0 = failure
  6996. *
  6997. * PARAMETERS:
  6998. * *cd - pointer to core data
  6999. ******************************************************************************/
  7000. static int pt_core_sleep(struct pt_core_data *cd)
  7001. {
  7002. int rc = 0;
  7003. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7004. if (rc < 0) {
  7005. pt_debug(cd->dev, DL_ERROR,
  7006. "%s: fail get exclusive ex=%p own=%p\n",
  7007. __func__, cd->exclusive_dev, cd->dev);
  7008. return rc;
  7009. }
  7010. rc = pt_core_sleep_(cd);
  7011. if (release_exclusive(cd, cd->dev) < 0)
  7012. pt_debug(cd->dev, DL_ERROR,
  7013. "%s: fail to release exclusive\n", __func__);
  7014. else
  7015. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7016. __func__);
  7017. return rc;
  7018. }
  7019. /*******************************************************************************
  7020. * FUNCTION: pt_wakeup_host
  7021. *
  7022. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7023. *
  7024. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7025. *
  7026. * RETURN:
  7027. * 0 = success
  7028. * !0 = failure
  7029. *
  7030. * PARAMETERS:
  7031. * *cd - pointer to core data
  7032. ******************************************************************************/
  7033. static int pt_wakeup_host(struct pt_core_data *cd)
  7034. {
  7035. #ifndef EASYWAKE_TSG6
  7036. /* TSG5 EasyWake */
  7037. int rc = 0;
  7038. int event_id;
  7039. int size = get_unaligned_le16(&cd->input_buf[0]);
  7040. /* Validate report */
  7041. if (size != 4 || cd->input_buf[2] != 4)
  7042. rc = -EINVAL;
  7043. event_id = cd->input_buf[3];
  7044. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7045. __func__, event_id, rc);
  7046. if (rc) {
  7047. pt_core_sleep_(cd);
  7048. goto exit;
  7049. }
  7050. /* attention WAKE */
  7051. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7052. exit:
  7053. return rc;
  7054. #else
  7055. /* TSG6 FW1.3 EasyWake */
  7056. int rc = 0;
  7057. int i = 0;
  7058. int report_length;
  7059. /* Validate report */
  7060. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7061. rc = -EINVAL;
  7062. pt_core_sleep_(cd);
  7063. goto exit;
  7064. }
  7065. /* Get gesture id and gesture data length */
  7066. cd->gesture_id = cd->input_buf[3];
  7067. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7068. cd->gesture_data_length = report_length - 4;
  7069. pt_debug(cd->dev, DL_INFO,
  7070. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7071. __func__, cd->gesture_id, cd->gesture_data_length);
  7072. for (i = 0; i < cd->gesture_data_length; i++)
  7073. cd->gesture_data[i] = cd->input_buf[4 + i];
  7074. /* attention WAKE */
  7075. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7076. exit:
  7077. return rc;
  7078. #endif
  7079. }
  7080. /*******************************************************************************
  7081. * FUNCTION: pt_get_touch_axis
  7082. *
  7083. * SUMMARY: Function to calculate touch axis
  7084. *
  7085. * PARAMETERS:
  7086. * *cd - pointer to core data structure
  7087. * *axis - pointer to axis calculation result
  7088. * size - size in bytes
  7089. * max - max value of result
  7090. * *xy_data - pointer to input data to be parsed
  7091. * bofs - bit offset
  7092. ******************************************************************************/
  7093. static void pt_get_touch_axis(struct pt_core_data *cd,
  7094. int *axis, int size, int max, u8 *data, int bofs)
  7095. {
  7096. int nbyte;
  7097. int next;
  7098. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7099. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7100. next++;
  7101. }
  7102. *axis &= max - 1;
  7103. }
  7104. /*******************************************************************************
  7105. * FUNCTION: move_tracking_heatmap_data
  7106. *
  7107. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7108. * system information structure, xy_mode and xy_data.
  7109. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7110. * the tthe_tuner sysfs node under the label "THM"
  7111. *
  7112. * RETURN:
  7113. * 0 = success
  7114. * !0 = failure
  7115. *
  7116. * PARAMETERS:
  7117. * *cd - pointer to core data
  7118. * *si - pointer to the system information structure
  7119. ******************************************************************************/
  7120. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7121. struct pt_sysinfo *si)
  7122. {
  7123. #ifdef TTHE_TUNER_SUPPORT
  7124. int size = get_unaligned_le16(&cd->input_buf[0]);
  7125. if (size)
  7126. tthe_print(cd, cd->input_buf, size, "THM=");
  7127. #endif
  7128. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7129. return 0;
  7130. }
  7131. /*******************************************************************************
  7132. * FUNCTION: move_sensor_data
  7133. *
  7134. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7135. * information structure, xy_mode and xy_data.
  7136. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7137. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7138. *
  7139. * RETURN:
  7140. * 0 = success
  7141. * !0 = failure
  7142. *
  7143. * PARAMETERS:
  7144. * *cd - pointer to core data
  7145. * *si - pointer to the system information structure
  7146. ******************************************************************************/
  7147. static int move_sensor_data(struct pt_core_data *cd,
  7148. struct pt_sysinfo *si)
  7149. {
  7150. #ifdef TTHE_TUNER_SUPPORT
  7151. int size = get_unaligned_le16(&cd->input_buf[0]);
  7152. if (size)
  7153. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7154. #endif
  7155. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7156. return 0;
  7157. }
  7158. /*******************************************************************************
  7159. * FUNCTION: move_button_data
  7160. *
  7161. * SUMMARY: Move the valid button data from the input buffer into the system
  7162. * information structure, xy_mode and xy_data.
  7163. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7164. * the tthe_tuner sysfs node under the label "OpModeData"
  7165. *
  7166. * RETURN:
  7167. * 0 = success
  7168. * !0 = failure
  7169. *
  7170. * PARAMETERS:
  7171. * *cd - pointer to core data
  7172. * *si - pointer to the system information structure
  7173. ******************************************************************************/
  7174. static int move_button_data(struct pt_core_data *cd,
  7175. struct pt_sysinfo *si)
  7176. {
  7177. #ifdef TTHE_TUNER_SUPPORT
  7178. int size = get_unaligned_le16(&cd->input_buf[0]);
  7179. if (size)
  7180. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7181. #endif
  7182. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7183. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7184. "xy_mode");
  7185. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7186. BTN_REPORT_SIZE);
  7187. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7188. "xy_data");
  7189. return 0;
  7190. }
  7191. /*******************************************************************************
  7192. * FUNCTION: move_touch_data
  7193. *
  7194. * SUMMARY: Move the valid touch data from the input buffer into the system
  7195. * information structure, xy_mode and xy_data.
  7196. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7197. * the tthe_tuner sysfs node under the label "OpModeData"
  7198. *
  7199. * RETURN:
  7200. * 0 = success
  7201. * !0 = failure
  7202. *
  7203. * PARAMETERS:
  7204. * *cd - pointer to core data
  7205. * *si - pointer to the system information structure
  7206. ******************************************************************************/
  7207. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7208. {
  7209. int max_tch = si->sensing_conf_data.max_tch;
  7210. int num_cur_tch;
  7211. int length;
  7212. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7213. #ifdef TTHE_TUNER_SUPPORT
  7214. int size = get_unaligned_le16(&cd->input_buf[0]);
  7215. if (size)
  7216. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7217. #endif
  7218. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7219. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7220. si->desc.tch_header_size, "xy_mode");
  7221. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7222. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7223. if (unlikely(num_cur_tch > max_tch))
  7224. num_cur_tch = max_tch;
  7225. length = num_cur_tch * si->desc.tch_record_size;
  7226. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7227. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7228. return 0;
  7229. }
  7230. /*******************************************************************************
  7231. * FUNCTION: move_hid_pen_data
  7232. *
  7233. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7234. * information structure, xy_mode and xy_data.
  7235. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7236. * the tthe_tuner sysfs node under the label "HID" starting with the
  7237. * report ID.
  7238. *
  7239. * RETURN:
  7240. * 0 = success
  7241. * !0 = failure
  7242. *
  7243. * PARAMETERS:
  7244. * *cd - pointer to core data
  7245. * *si - pointer to the system information structure
  7246. ******************************************************************************/
  7247. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7248. {
  7249. #ifdef TTHE_TUNER_SUPPORT
  7250. int size = get_unaligned_le16(&cd->input_buf[0]);
  7251. if (size) {
  7252. /*
  7253. * HID over USB does not require the two byte length field, so
  7254. * this should print from input_buf[2] but to keep both finger
  7255. * and pen reports the same the length is included
  7256. */
  7257. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7258. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7259. "HID-USB=");
  7260. else
  7261. tthe_print(cd, &(cd->input_buf[0]), size,
  7262. "HID-I2C=");
  7263. }
  7264. #endif
  7265. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7266. return 0;
  7267. }
  7268. /*******************************************************************************
  7269. * FUNCTION: parse_touch_input
  7270. *
  7271. * SUMMARY: Parse the touch report and take action based on the touch
  7272. * report_id.
  7273. *
  7274. * RETURN:
  7275. * 0 = success
  7276. * !0 = failure
  7277. *
  7278. * PARAMETERS:
  7279. * *cd - pointer to core data
  7280. * size - size of touch record
  7281. ******************************************************************************/
  7282. static int parse_touch_input(struct pt_core_data *cd, int size)
  7283. {
  7284. struct pt_sysinfo *si = &cd->sysinfo;
  7285. int report_id = cd->input_buf[2];
  7286. int rc = -EINVAL;
  7287. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7288. __func__);
  7289. if (!si->ready) {
  7290. pt_debug(cd->dev, DL_ERROR,
  7291. "%s: Need system information to parse touches\n",
  7292. __func__);
  7293. return 0;
  7294. }
  7295. if (!si->xy_mode || !si->xy_data)
  7296. return rc;
  7297. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7298. rc = move_touch_data(cd, si);
  7299. else if (report_id == PT_HID_PEN_REPORT_ID)
  7300. rc = move_hid_pen_data(cd, si);
  7301. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7302. rc = move_button_data(cd, si);
  7303. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7304. rc = move_sensor_data(cd, si);
  7305. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7306. rc = move_tracking_heatmap_data(cd, si);
  7307. if (rc)
  7308. return rc;
  7309. /* attention IRQ */
  7310. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7311. return 0;
  7312. }
  7313. /*******************************************************************************
  7314. * FUNCTION: parse_command_input
  7315. *
  7316. * SUMMARY: Move the response data from the input buffer to the response buffer
  7317. *
  7318. * RETURN:
  7319. * 0 = success
  7320. * !0 = failure
  7321. *
  7322. * PARAMETERS:
  7323. * *cd - pointer to core data
  7324. * size - size of response data
  7325. ******************************************************************************/
  7326. static int parse_command_input(struct pt_core_data *cd, int size)
  7327. {
  7328. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7329. __func__);
  7330. memcpy(cd->response_buf, cd->input_buf, size);
  7331. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7332. if (size && cd->show_tt_data) {
  7333. if (cd->pip2_prot_active)
  7334. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7335. else
  7336. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7337. }
  7338. #endif
  7339. mutex_lock(&cd->system_lock);
  7340. cd->hid_cmd_state = 0;
  7341. mutex_unlock(&cd->system_lock);
  7342. wake_up(&cd->wait_q);
  7343. return 0;
  7344. }
  7345. /*******************************************************************************
  7346. * FUNCTION: pt_allow_enumeration
  7347. *
  7348. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7349. * FW sentinel is seen.
  7350. *
  7351. * RETURN:
  7352. * true = allow enumeration or fully re-probe
  7353. * false = skip enumeration and fully re-probe
  7354. *
  7355. * PARAMETERS:
  7356. * *cd - pointer to core data
  7357. ******************************************************************************/
  7358. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7359. {
  7360. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7361. (!cd->hid_reset_cmd_state) &&
  7362. (cd->core_probe_complete) &&
  7363. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7364. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7365. (cd->mode == PT_MODE_OPERATIONAL)) {
  7366. return true;
  7367. }
  7368. if ((!cd->hid_reset_cmd_state) &&
  7369. (cd->core_probe_complete) &&
  7370. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7371. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7372. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7373. return true;
  7374. }
  7375. pt_debug(cd->dev, DL_INFO,
  7376. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7377. __func__,
  7378. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7379. "core_probe_complete(1)", cd->core_probe_complete,
  7380. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7381. "active_dut_gen(0,2)", cd->active_dut_generation);
  7382. return false;
  7383. }
  7384. /*******************************************************************************
  7385. * FUNCTION: pt_is_touch_report
  7386. *
  7387. * SUMMARY: Determine if a report ID should be treated as a touch report
  7388. *
  7389. * RETURN:
  7390. * true = report ID is a touch report
  7391. * false = report ID is not a touch report
  7392. *
  7393. * PARAMETERS:
  7394. * id - Report ID
  7395. ******************************************************************************/
  7396. static bool pt_is_touch_report(int id)
  7397. {
  7398. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7399. id == PT_HID_PEN_REPORT_ID ||
  7400. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7401. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7402. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7403. return true;
  7404. else
  7405. return false;
  7406. }
  7407. /*******************************************************************************
  7408. * FUNCTION: pt_parse_input
  7409. *
  7410. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7411. * on if its a response to a command or asynchronous touch data / reset
  7412. * sentinel. PIP2.x messages have additional error checking that is
  7413. * parsed (SEQ match from cmd to rsp, CRC valid).
  7414. * Look for special packets based on unique lengths:
  7415. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7416. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7417. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7418. * 0xFFXX - Empty buffer (PIP 1.7+)
  7419. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7420. * the initial probe has not yet been done.
  7421. *
  7422. * RETURN:
  7423. * 0 = success
  7424. * !0 = failure
  7425. *
  7426. * PARAMETERS:
  7427. * *cd - pointer to core data
  7428. ******************************************************************************/
  7429. static int pt_parse_input(struct pt_core_data *cd)
  7430. {
  7431. int report_id;
  7432. int cmd_id;
  7433. int is_command = 0;
  7434. int size;
  7435. int print_size;
  7436. bool touch_report = true;
  7437. unsigned short calc_crc;
  7438. unsigned short resp_crc;
  7439. cd->fw_sys_mode_in_standby_state = false;
  7440. size = get_unaligned_le16(&cd->input_buf[0]);
  7441. print_size = size;
  7442. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7443. __func__, print_size);
  7444. if (print_size <= PT_MAX_INPUT)
  7445. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7446. "<<< Read buf");
  7447. if (size == 0 ||
  7448. (size == 11 &&
  7449. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7450. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7451. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7452. PIP2_CMD_COMMAND_ID_MASK) ==
  7453. PIP2_CMD_ID_STATUS)) {
  7454. touch_report = false;
  7455. cd->hw_detected = true;
  7456. cd->bl_pip_ver_ready = false;
  7457. cd->app_pip_ver_ready = false;
  7458. if (size == 0) {
  7459. mutex_lock(&cd->system_lock);
  7460. cd->pip2_prot_active = false;
  7461. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7462. /*
  7463. * For Gen5/6 this sentinel could be from
  7464. * the BL or FW. Attempt to set the correct
  7465. * mode based on the previous PIP command.
  7466. */
  7467. if (cd->hid_cmd_state ==
  7468. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7469. cd->mode = PT_MODE_OPERATIONAL;
  7470. cd->startup_status =
  7471. STARTUP_STATUS_FW_RESET_SENTINEL;
  7472. } else if (cd->hid_cmd_state ==
  7473. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7474. cd->hid_reset_cmd_state) {
  7475. cd->mode = PT_MODE_BOOTLOADER;
  7476. cd->startup_status =
  7477. STARTUP_STATUS_BL_RESET_SENTINEL;
  7478. } else {
  7479. cd->mode = PT_MODE_UNKNOWN;
  7480. cd->startup_status =
  7481. STARTUP_STATUS_START;
  7482. }
  7483. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7484. pt_debug(cd->dev, DL_INFO,
  7485. "%s: ATM Gen5/6 %s sentinel received\n",
  7486. __func__,
  7487. (cd->mode == PT_MODE_OPERATIONAL ?
  7488. "FW" :
  7489. (cd->mode == PT_MODE_BOOTLOADER ?
  7490. "BL" : "Unknown")));
  7491. } else {
  7492. cd->mode = PT_MODE_OPERATIONAL;
  7493. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7494. cd->startup_status =
  7495. STARTUP_STATUS_FW_RESET_SENTINEL;
  7496. pt_debug(cd->dev, DL_INFO,
  7497. "%s: ATM PT/TT FW sentinel received\n",
  7498. __func__);
  7499. }
  7500. mutex_unlock(&cd->system_lock);
  7501. if (pt_allow_enumeration(cd)) {
  7502. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7503. pt_debug(cd->dev, DL_INFO,
  7504. "%s: Queue Restart\n", __func__);
  7505. pt_queue_restart(cd);
  7506. } else {
  7507. pt_debug(cd->dev, DL_INFO,
  7508. "%s: Queue Enum\n", __func__);
  7509. pt_queue_enum(cd);
  7510. }
  7511. } else {
  7512. pt_debug(cd->dev, DL_INFO,
  7513. "%s: Sentinel - No Queued Action\n",
  7514. __func__);
  7515. }
  7516. } else {
  7517. /* Sentinel must be from TT/TC BL */
  7518. mutex_lock(&cd->system_lock);
  7519. cd->pip2_prot_active = true;
  7520. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7521. cd->mode = PT_MODE_BOOTLOADER;
  7522. cd->sysinfo.ready = false;
  7523. mutex_unlock(&cd->system_lock);
  7524. pt_debug(cd->dev, DL_INFO,
  7525. "%s: BL Reset sentinel received\n", __func__);
  7526. if (cd->flashless_dut &&
  7527. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7528. pt_debug(cd->dev, DL_INFO,
  7529. "%s: BL to RAM for flashless DUT\n",
  7530. __func__);
  7531. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7532. }
  7533. }
  7534. mutex_lock(&cd->system_lock);
  7535. memcpy(cd->response_buf, cd->input_buf, 2);
  7536. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7537. mutex_unlock(&cd->system_lock);
  7538. pt_debug(cd->dev, DL_WARN,
  7539. "%s: Device Initiated Reset\n", __func__);
  7540. wake_up(&cd->wait_q);
  7541. return 0;
  7542. }
  7543. cd->hid_reset_cmd_state = 0;
  7544. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7545. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7546. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7547. cd->hid_cmd_state = 0;
  7548. wake_up(&cd->wait_q);
  7549. mutex_unlock(&cd->system_lock);
  7550. return 0;
  7551. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7552. /*
  7553. * This debug message below is used by PBATS to calculate the
  7554. * time from the last lift off IRQ to when FW exits LFT mode.
  7555. */
  7556. touch_report = false;
  7557. pt_debug(cd->dev, DL_WARN,
  7558. "%s: DUT - Empty buffer detected\n", __func__);
  7559. return 0;
  7560. } else if (size > PT_MAX_INPUT || size < 0) {
  7561. pt_debug(cd->dev, DL_ERROR,
  7562. "%s: DUT - Unexpected len field in active bus data!\n",
  7563. __func__);
  7564. return -EINVAL;
  7565. }
  7566. if (cd->pip2_prot_active) {
  7567. pt_debug(cd->dev, DL_DEBUG,
  7568. "%s: Decode PIP2.x Response\n", __func__);
  7569. /* PIP2 does not have a report id, hard code it */
  7570. report_id = 0x00;
  7571. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7572. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7573. resp_crc = cd->input_buf[size - 2] << 8;
  7574. resp_crc |= cd->input_buf[size - 1];
  7575. if ((cd->pip2_cmd_tag_seq !=
  7576. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7577. (resp_crc != calc_crc) &&
  7578. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7579. == PT_PIP_TOUCH_REPORT_ID) ||
  7580. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7581. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7582. pt_debug(cd->dev, DL_WARN,
  7583. "%s: %s %d %s\n",
  7584. __func__,
  7585. "Received PIP1 report id =",
  7586. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7587. "when expecting a PIP2 report - IGNORE report");
  7588. return 0;
  7589. }
  7590. is_command = 1;
  7591. touch_report = false;
  7592. } else {
  7593. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7594. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7595. }
  7596. #ifdef TTDL_DIAGNOSTICS
  7597. pt_debug(cd->dev, DL_DEBUG,
  7598. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7599. __func__, cd->pip2_prot_active, report_id,
  7600. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7601. #endif /* TTDL_DIAGNOSTICS */
  7602. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7603. pt_wakeup_host(cd);
  7604. #ifdef TTHE_TUNER_SUPPORT
  7605. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7606. #endif
  7607. return 0;
  7608. }
  7609. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7610. msecs_to_jiffies(cd->watchdog_interval));
  7611. /*
  7612. * If it is PIP2 response, the report_id has been set to 0,
  7613. * so it will not be parsed as a touch packet.
  7614. */
  7615. if (!pt_is_touch_report(report_id)) {
  7616. is_command = 1;
  7617. touch_report = false;
  7618. }
  7619. if (unlikely(is_command)) {
  7620. parse_command_input(cd, size);
  7621. return 0;
  7622. }
  7623. if (touch_report)
  7624. parse_touch_input(cd, size);
  7625. return 0;
  7626. }
  7627. /*******************************************************************************
  7628. * FUNCTION: pt_read_input
  7629. *
  7630. * SUMMARY: Reads incoming data off of the active bus
  7631. *
  7632. * RETURN:
  7633. * 0 = success
  7634. * !0 = failure
  7635. *
  7636. * PARAMETERS:
  7637. * *cd - pointer to core data
  7638. ******************************************************************************/
  7639. static int pt_read_input(struct pt_core_data *cd)
  7640. {
  7641. struct device *dev = cd->dev;
  7642. int rc = 0;
  7643. int t;
  7644. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7645. /*
  7646. * Workaround for easywake failure
  7647. * Interrupt for easywake, wait for bus controller to wake
  7648. */
  7649. mutex_lock(&cd->system_lock);
  7650. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7651. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7652. mutex_unlock(&cd->system_lock);
  7653. if (!dev->power.is_suspended)
  7654. goto read;
  7655. t = wait_event_timeout(cd->wait_q,
  7656. (cd->wait_until_wake == 1),
  7657. msecs_to_jiffies(2000));
  7658. #ifdef TTDL_DIAGNOSTICS
  7659. if (IS_TMO(t)) {
  7660. cd->bus_transmit_error_count++;
  7661. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7662. pt_debug(dev, DL_ERROR,
  7663. "%s: !!!I2C Transmission Error %d\n",
  7664. __func__,
  7665. cd->bus_transmit_error_count);
  7666. }
  7667. #else
  7668. if (IS_TMO(t))
  7669. pt_queue_enum(cd);
  7670. #endif /* TTDL_DIAGNOSTICS */
  7671. goto read;
  7672. }
  7673. }
  7674. mutex_unlock(&cd->system_lock);
  7675. read:
  7676. /* Try reading up to 'retry' times */
  7677. while (retry-- != 0) {
  7678. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7679. PT_MAX_INPUT);
  7680. if (!rc) {
  7681. pt_debug(dev, DL_DEBUG,
  7682. "%s: Read input successfully\n", __func__);
  7683. goto read_exit;
  7684. }
  7685. usleep_range(1000, 2000);
  7686. }
  7687. pt_debug(dev, DL_ERROR,
  7688. "%s: Error getting report, rc=%d\n", __func__, rc);
  7689. read_exit:
  7690. return rc;
  7691. }
  7692. /*******************************************************************************
  7693. * FUNCTION: pt_irq
  7694. *
  7695. * SUMMARY: Process all detected interrupts
  7696. *
  7697. * RETURN:
  7698. * IRQ_HANDLED - Finished processing the interrupt
  7699. *
  7700. * PARAMETERS:
  7701. * irq - IRQ number
  7702. * *handle - pointer to core data struct
  7703. ******************************************************************************/
  7704. irqreturn_t pt_irq(int irq, void *handle)
  7705. {
  7706. struct pt_core_data *cd = handle;
  7707. int rc = 0;
  7708. if (!pt_check_irq_asserted(cd))
  7709. return IRQ_HANDLED;
  7710. rc = pt_read_input(cd);
  7711. #ifdef TTDL_DIAGNOSTICS
  7712. cd->irq_count++;
  7713. /* Used to calculate T-Refresh */
  7714. if (cd->t_refresh_active) {
  7715. if (cd->t_refresh_count == 0) {
  7716. cd->t_refresh_time = jiffies;
  7717. cd->t_refresh_count++;
  7718. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7719. cd->t_refresh_count++;
  7720. } else {
  7721. cd->t_refresh_active = 0;
  7722. cd->t_refresh_time =
  7723. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7724. }
  7725. }
  7726. #endif /* TTDL_DIAGNOSTICS */
  7727. if (!rc)
  7728. pt_parse_input(cd);
  7729. return IRQ_HANDLED;
  7730. }
  7731. /*******************************************************************************
  7732. * FUNCTION: _pt_subscribe_attention
  7733. *
  7734. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7735. * to subscribe themselves into the TTDL attention list
  7736. *
  7737. * RETURN:
  7738. * 0 = success
  7739. * !0 = failure
  7740. *
  7741. * PARAMETERS:
  7742. * *dev - pointer to device structure
  7743. * type - attention type enum
  7744. * *id - ID of the module calling this function
  7745. * *func - callback function pointer to be called when notified
  7746. * mode - attention mode
  7747. ******************************************************************************/
  7748. int _pt_subscribe_attention(struct device *dev,
  7749. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7750. int mode)
  7751. {
  7752. struct pt_core_data *cd = dev_get_drvdata(dev);
  7753. struct atten_node *atten, *atten_new;
  7754. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7755. if (!atten_new)
  7756. return -ENOMEM;
  7757. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7758. dev_name(cd->dev));
  7759. spin_lock(&cd->spinlock);
  7760. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7761. if (atten->id == id && atten->mode == mode) {
  7762. spin_unlock(&cd->spinlock);
  7763. kfree(atten_new);
  7764. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7765. __func__,
  7766. "already subscribed attention",
  7767. dev, "mode", mode);
  7768. return 0;
  7769. }
  7770. }
  7771. atten_new->id = id;
  7772. atten_new->dev = dev;
  7773. atten_new->mode = mode;
  7774. atten_new->func = func;
  7775. list_add(&atten_new->node, &cd->atten_list[type]);
  7776. spin_unlock(&cd->spinlock);
  7777. return 0;
  7778. }
  7779. /*******************************************************************************
  7780. * FUNCTION: _pt_unsubscribe_attention
  7781. *
  7782. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7783. * to unsubscribe themselves from the TTDL attention list
  7784. *
  7785. * RETURN:
  7786. * 0 = success
  7787. * !0 = failure
  7788. *
  7789. * PARAMETERS:
  7790. * *dev - pointer to device structure
  7791. * type - attention type enum
  7792. * *id - ID of the module calling this function
  7793. * *func - function pointer
  7794. * mode - attention mode
  7795. ******************************************************************************/
  7796. int _pt_unsubscribe_attention(struct device *dev,
  7797. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7798. int mode)
  7799. {
  7800. struct pt_core_data *cd = dev_get_drvdata(dev);
  7801. struct atten_node *atten, *atten_n;
  7802. spin_lock(&cd->spinlock);
  7803. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7804. if (atten->id == id && atten->mode == mode) {
  7805. list_del(&atten->node);
  7806. spin_unlock(&cd->spinlock);
  7807. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7808. __func__,
  7809. "unsub for atten->dev", atten->dev,
  7810. "atten->mode", atten->mode);
  7811. kfree(atten);
  7812. return 0;
  7813. }
  7814. }
  7815. spin_unlock(&cd->spinlock);
  7816. return -ENODEV;
  7817. }
  7818. /*******************************************************************************
  7819. * FUNCTION: _pt_request_exclusive
  7820. *
  7821. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7822. * to request exclusive access
  7823. *
  7824. * RETURN:
  7825. * 0 = success
  7826. * !0 = failure
  7827. *
  7828. * PARAMETERS:
  7829. * *dev - pointer to device structure
  7830. * timeout_ms - timeout to wait for exclusive access
  7831. ******************************************************************************/
  7832. static int _pt_request_exclusive(struct device *dev,
  7833. int timeout_ms)
  7834. {
  7835. struct pt_core_data *cd = dev_get_drvdata(dev);
  7836. return request_exclusive(cd, (void *)dev, timeout_ms);
  7837. }
  7838. /*******************************************************************************
  7839. * FUNCTION: _pt_release_exclusive
  7840. *
  7841. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7842. * to release exclusive access
  7843. *
  7844. * RETURN:
  7845. * 0 = success
  7846. * !0 = failure
  7847. *
  7848. * PARAMETERS:
  7849. * *dev - pointer to device structure
  7850. ******************************************************************************/
  7851. static int _pt_release_exclusive(struct device *dev)
  7852. {
  7853. struct pt_core_data *cd = dev_get_drvdata(dev);
  7854. return release_exclusive(cd, (void *)dev);
  7855. }
  7856. /*******************************************************************************
  7857. * FUNCTION: _pt_request_reset
  7858. *
  7859. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7860. * to request the DUT to be reset. Function returns err if refused or
  7861. * timeout occurs (Note: core uses fixed timeout period).
  7862. *
  7863. * NOTE: Function blocks until ISR occurs.
  7864. *
  7865. * RETURN:
  7866. * 0 = success
  7867. * !0 = failure
  7868. *
  7869. * PARAMETERS:
  7870. * *dev - pointer to device structure
  7871. * protect - flag to call protected or non-protected
  7872. ******************************************************************************/
  7873. static int _pt_request_reset(struct device *dev, int protect)
  7874. {
  7875. struct pt_core_data *cd = dev_get_drvdata(dev);
  7876. int rc;
  7877. rc = pt_dut_reset(cd, protect);
  7878. if (rc < 0) {
  7879. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7880. __func__, rc);
  7881. }
  7882. return rc;
  7883. }
  7884. /*******************************************************************************
  7885. * FUNCTION: _pt_request_enum
  7886. *
  7887. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7888. * to request TTDL to queue an enum. This function will return err
  7889. * if refused; if no error then enum has completed and system is in
  7890. * normal operation mode.
  7891. *
  7892. * RETURN:
  7893. * 0 = success
  7894. * !0 = failure
  7895. *
  7896. * PARAMETERS:
  7897. * *dev - pointer to device structure
  7898. * wait - boolean to determine if to wait for startup event
  7899. ******************************************************************************/
  7900. static int _pt_request_enum(struct device *dev, bool wait)
  7901. {
  7902. struct pt_core_data *cd = dev_get_drvdata(dev);
  7903. pt_queue_enum(cd);
  7904. if (wait)
  7905. wait_event_timeout(cd->wait_q,
  7906. cd->startup_state == STARTUP_NONE,
  7907. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7908. return 0;
  7909. }
  7910. /*******************************************************************************
  7911. * FUNCTION: _pt_request_sysinfo
  7912. *
  7913. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7914. * to request the pointer to the system information structure. This
  7915. * function will return NULL if sysinfo has not been acquired from the
  7916. * DUT yet.
  7917. *
  7918. * RETURN: Pointer to the system information struct
  7919. *
  7920. * PARAMETERS:
  7921. * *dev - pointer to device structure
  7922. ******************************************************************************/
  7923. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7924. {
  7925. struct pt_core_data *cd = dev_get_drvdata(dev);
  7926. int rc = 0;
  7927. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7928. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7929. !cd->sysinfo.ready) {
  7930. rc = pt_hid_output_get_sysinfo_(cd);
  7931. if (rc) {
  7932. pt_debug(cd->dev, DL_ERROR,
  7933. "%s: Error getting sysinfo rc=%d\n",
  7934. __func__, rc);
  7935. }
  7936. }
  7937. if (cd->sysinfo.ready)
  7938. return &cd->sysinfo;
  7939. return NULL;
  7940. }
  7941. /*******************************************************************************
  7942. * FUNCTION: _pt_request_loader_pdata
  7943. *
  7944. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7945. * to request the pointer to the loader platform data
  7946. *
  7947. * RETURN: Pointer to the loader platform data struct
  7948. *
  7949. * PARAMETERS:
  7950. * *dev - pointer to device structure
  7951. ******************************************************************************/
  7952. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7953. struct device *dev)
  7954. {
  7955. struct pt_core_data *cd = dev_get_drvdata(dev);
  7956. return cd->pdata->loader_pdata;
  7957. }
  7958. /*******************************************************************************
  7959. * FUNCTION: _pt_request_start_wd
  7960. *
  7961. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7962. * to request to start the TTDL watchdog
  7963. *
  7964. * RETURN:
  7965. * 0 = success
  7966. * !0 = failure
  7967. *
  7968. * PARAMETERS:
  7969. * *dev - pointer to device structure
  7970. ******************************************************************************/
  7971. static int _pt_request_start_wd(struct device *dev)
  7972. {
  7973. struct pt_core_data *cd = dev_get_drvdata(dev);
  7974. pt_start_wd_timer(cd);
  7975. return 0;
  7976. }
  7977. /*******************************************************************************
  7978. * FUNCTION: _pt_request_stop_wd
  7979. *
  7980. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7981. * to request to stop the TTDL watchdog
  7982. *
  7983. * RETURN:
  7984. * 0 = success
  7985. * !0 = failure
  7986. *
  7987. * PARAMETERS:
  7988. * *dev - pointer to device structure
  7989. ******************************************************************************/
  7990. static int _pt_request_stop_wd(struct device *dev)
  7991. {
  7992. struct pt_core_data *cd = dev_get_drvdata(dev);
  7993. pt_stop_wd_timer(cd);
  7994. return 0;
  7995. }
  7996. /*******************************************************************************
  7997. * FUNCTION: pt_pip2_launch_app
  7998. *
  7999. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  8000. * the FW reset sentinel to indicate the function succeeded.
  8001. *
  8002. * NOTE: Calling this function when the DUT is in Application mode WILL result
  8003. * in a timeout delay and with the DUT being reset with an XRES.
  8004. *
  8005. * RETURN:
  8006. * 0 = success
  8007. * !0 = failure
  8008. *
  8009. * PARAMETERS:
  8010. * *cd - pointer to core data structure
  8011. * protect - flag to call protected or non-protected
  8012. ******************************************************************************/
  8013. static int pt_pip2_launch_app(struct device *dev, int protect)
  8014. {
  8015. struct pt_core_data *cd = dev_get_drvdata(dev);
  8016. u16 actual_read_len;
  8017. u16 tmp_startup_status = cd->startup_status;
  8018. u8 read_buf[12];
  8019. u8 status;
  8020. int time = 0;
  8021. int rc = 0;
  8022. mutex_lock(&cd->system_lock);
  8023. cd->startup_status = STARTUP_STATUS_START;
  8024. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8025. mutex_unlock(&cd->system_lock);
  8026. rc = _pt_request_pip2_send_cmd(dev, protect,
  8027. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8028. &actual_read_len);
  8029. if (rc) {
  8030. pt_debug(dev, DL_ERROR,
  8031. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8032. __func__, rc);
  8033. } else {
  8034. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8035. /* Test for no or invalid image in FLASH, no point to reset */
  8036. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8037. rc = status;
  8038. goto exit;
  8039. }
  8040. /* Any other boot failure */
  8041. if (status != 0) {
  8042. pt_debug(dev, DL_ERROR,
  8043. "%s: FW did not EXECUTE, status = %d\n",
  8044. __func__, status);
  8045. rc = status;
  8046. }
  8047. }
  8048. if (rc) {
  8049. pt_debug(dev, DL_ERROR,
  8050. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8051. __func__, rc);
  8052. goto exit;
  8053. }
  8054. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8055. msleep(20);
  8056. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8057. __func__, time, cd->startup_status);
  8058. time += 20;
  8059. }
  8060. if (cd->startup_status == STARTUP_STATUS_START) {
  8061. pt_debug(cd->dev, DL_WARN,
  8062. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8063. rc = -ETIME;
  8064. }
  8065. exit:
  8066. if (cd->startup_status == STARTUP_STATUS_START) {
  8067. /* Reset to original state because we could be stuck in BL */
  8068. mutex_lock(&cd->system_lock);
  8069. cd->startup_status = tmp_startup_status;
  8070. mutex_unlock(&cd->system_lock);
  8071. }
  8072. return rc;
  8073. }
  8074. /*******************************************************************************
  8075. * FUNCTION: _pt_request_pip2_launch_app
  8076. *
  8077. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8078. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8079. * consumed.
  8080. *
  8081. * RETURN:
  8082. * 0 = success
  8083. * !0 = failure
  8084. *
  8085. * PARAMETERS:
  8086. * *cd - pointer to core data structure
  8087. ******************************************************************************/
  8088. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8089. {
  8090. return pt_pip2_launch_app(dev, protect);
  8091. }
  8092. /*******************************************************************************
  8093. * FUNCTION: _pt_request_wait_for_enum_state
  8094. *
  8095. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8096. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8097. *
  8098. * RETURN:
  8099. * 0 = success
  8100. * !0 = failure
  8101. *
  8102. * PARAMETERS:
  8103. * *cd - pointer to core data structure
  8104. * timeout - timeout for how long to wait
  8105. * state - enum state to wait for
  8106. ******************************************************************************/
  8107. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8108. int state)
  8109. {
  8110. int rc = 0;
  8111. int t;
  8112. struct pt_core_data *cd = dev_get_drvdata(dev);
  8113. t = wait_event_timeout(cd->wait_q,
  8114. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8115. msecs_to_jiffies(timeout));
  8116. if (IS_TMO(t)) {
  8117. pt_debug(cd->dev, DL_ERROR,
  8118. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8119. __func__, state, timeout);
  8120. pt_debug(cd->dev, DL_WARN,
  8121. "%s: enum state reached 0x%04X\n",
  8122. __func__, cd->startup_status);
  8123. rc = -ETIME;
  8124. } else if (cd->startup_status & state) {
  8125. pt_debug(cd->dev, DL_INFO,
  8126. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8127. __func__, cd->startup_status,
  8128. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8129. } else {
  8130. if (t == 1) {
  8131. pt_debug(
  8132. cd->dev, DL_ERROR,
  8133. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8134. __func__, state, timeout);
  8135. rc = -ETIME;
  8136. } else {
  8137. pt_debug(
  8138. cd->dev, DL_ERROR,
  8139. "%s: Enum state 0x%04X not reached in %dms\n",
  8140. __func__, state, timeout - jiffies_to_msecs(t));
  8141. rc = -EINVAL;
  8142. }
  8143. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8144. __func__, cd->startup_status);
  8145. }
  8146. return rc;
  8147. }
  8148. /*******************************************************************************
  8149. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8150. *
  8151. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8152. * deep sleep.
  8153. *
  8154. * RETURN:
  8155. * 0 = success
  8156. * !0 = error
  8157. *
  8158. * PARAMETERS:
  8159. * *cd - pointer to core data
  8160. ******************************************************************************/
  8161. static int pt_core_wake_device_from_deep_sleep_(
  8162. struct pt_core_data *cd)
  8163. {
  8164. int rc;
  8165. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8166. if (rc)
  8167. rc = -EAGAIN;
  8168. /* Prevent failure on sequential wake/sleep requests from OS */
  8169. msleep(20);
  8170. return rc;
  8171. }
  8172. /*******************************************************************************
  8173. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8174. *
  8175. * SUMMARY: Wake up device from Easy-Wake state.
  8176. *
  8177. * RETURN:
  8178. * 0 = success
  8179. * !0 = failure
  8180. *
  8181. * PARAMETERS:
  8182. * *cd - pointer to core data
  8183. ******************************************************************************/
  8184. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8185. {
  8186. int rc = 0;
  8187. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8188. return rc;
  8189. }
  8190. /*******************************************************************************
  8191. * FUNCTION: pt_restore_parameters_
  8192. *
  8193. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8194. * back to the DUT
  8195. *
  8196. * RETURN:
  8197. * 0 = success
  8198. * !0 = failure
  8199. *
  8200. * PARAMETERS:
  8201. * *cd - pointer the core data structure
  8202. ******************************************************************************/
  8203. static int pt_restore_parameters_(struct pt_core_data *cd)
  8204. {
  8205. struct param_node *param;
  8206. int rc = 0;
  8207. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8208. goto exit;
  8209. spin_lock(&cd->spinlock);
  8210. list_for_each_entry(param, &cd->param_list, node) {
  8211. spin_unlock(&cd->spinlock);
  8212. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8213. __func__, param->id, param->value);
  8214. rc = pt_pip_set_param_(cd, param->id,
  8215. param->value, param->size);
  8216. if (rc)
  8217. goto exit;
  8218. spin_lock(&cd->spinlock);
  8219. }
  8220. spin_unlock(&cd->spinlock);
  8221. exit:
  8222. return rc;
  8223. }
  8224. /*******************************************************************************
  8225. * FUNCTION: pt_pip2_exit_bl_
  8226. *
  8227. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8228. * a DUT that may not have flash and will need FW to be loaded into RAM
  8229. *
  8230. * RETURN:
  8231. * 0 = success
  8232. * !0 = failure
  8233. *
  8234. * PARAMETERS:
  8235. * *cd - pointer the core data structure
  8236. * *status_str - pointer to optional status string buffer
  8237. * buf_size - size of status_str buffer
  8238. ******************************************************************************/
  8239. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8240. {
  8241. int rc;
  8242. int wait_time = 0;
  8243. u8 mode = PT_MODE_UNKNOWN;
  8244. bool load_status_str = false;
  8245. /*
  8246. * Below function has protective call to ensure no enum is still on
  8247. * going, while this kind of protection should be applied widely in
  8248. * future (TODO).
  8249. */
  8250. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8251. if (status_str && buf_size <= 50)
  8252. load_status_str = true;
  8253. if (mode == PT_MODE_BOOTLOADER) {
  8254. if (cd->flashless_dut == 1) {
  8255. rc = pt_hw_hard_reset(cd);
  8256. } else {
  8257. rc = pt_pip2_launch_app(cd->dev,
  8258. PT_CORE_CMD_UNPROTECTED);
  8259. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8260. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8261. __func__, "Invalid image in FLASH rc", rc);
  8262. } else if (rc) {
  8263. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8264. __func__, "Failed to launch app rc", rc);
  8265. }
  8266. }
  8267. if (!rc) {
  8268. if (cd->flashless_dut == 1) {
  8269. /* Wait for BL to complete before enum */
  8270. rc = _pt_request_wait_for_enum_state(cd->dev,
  8271. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8272. if (rc && load_status_str) {
  8273. strlcpy(status_str, "No FW sentinel after BL",
  8274. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8275. goto exit;
  8276. }
  8277. }
  8278. /*
  8279. * If the host wants to interact with the FW or do a
  8280. * forced calibration, the FW must be out of BOOT mode
  8281. * and the system information must have been retrieved.
  8282. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8283. * If, however, the enumeration does not reach this
  8284. * point, the DUT may still be in APP mode so test
  8285. * for all conditions.
  8286. */
  8287. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8288. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8289. if (!rc || cd->startup_status >=
  8290. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8291. mutex_lock(&cd->system_lock);
  8292. cd->mode = PT_MODE_OPERATIONAL;
  8293. mutex_unlock(&cd->system_lock);
  8294. }
  8295. if (rc) {
  8296. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8297. __func__, "Failed to enum with DUT",
  8298. cd->startup_status);
  8299. if (load_status_str && !(cd->startup_status &
  8300. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8301. strlcpy(status_str, "FW Stuck in Boot mode",
  8302. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8303. goto exit;
  8304. }
  8305. }
  8306. /*
  8307. * The coming FW sentinel could wake up the event
  8308. * queue, which has chance to be taken by next command
  8309. * wrongly. Following delay is a workaround to avoid
  8310. * this issue for most situations.
  8311. */
  8312. msleep(20);
  8313. pt_start_wd_timer(cd);
  8314. }
  8315. if (load_status_str) {
  8316. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8317. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8318. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8319. else if (!rc)
  8320. strlcpy(status_str, "Entered APP from BL mode",
  8321. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8322. else
  8323. strlcpy(status_str, "Failed to enter APP from BL mode",
  8324. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8325. }
  8326. } else if (mode == PT_MODE_OPERATIONAL) {
  8327. mutex_lock(&cd->system_lock);
  8328. cd->mode = mode;
  8329. mutex_unlock(&cd->system_lock);
  8330. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8331. if (load_status_str) {
  8332. if (!rc)
  8333. strlcpy(status_str, "Already in APP mode",
  8334. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8335. else
  8336. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8337. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8338. }
  8339. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8340. mutex_lock(&cd->system_lock);
  8341. cd->mode = mode;
  8342. mutex_unlock(&cd->system_lock);
  8343. if (load_status_str)
  8344. strlcpy(status_str, "Failed to determine active mode",
  8345. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8346. }
  8347. exit:
  8348. if (!rc)
  8349. pt_start_wd_timer(cd);
  8350. return rc;
  8351. }
  8352. /*******************************************************************************
  8353. * FUNCTION: pt_pip2_exit_bl
  8354. *
  8355. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8356. * access.
  8357. *
  8358. * RETURN:
  8359. * 0 = success
  8360. * !0 = failure
  8361. *
  8362. * PARAMETERS:
  8363. * *cd - pointer the core data structure
  8364. * *status_str - pointer to optional status string buffer
  8365. * buf_size - size of status_str buffer
  8366. ******************************************************************************/
  8367. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8368. {
  8369. int rc;
  8370. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8371. if (rc < 0) {
  8372. pt_debug(cd->dev, DL_ERROR,
  8373. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8374. cd->exclusive_dev, cd->dev);
  8375. return rc;
  8376. }
  8377. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8378. if (release_exclusive(cd, cd->dev) < 0)
  8379. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8380. __func__);
  8381. return rc;
  8382. }
  8383. /*******************************************************************************
  8384. * FUNCTION: _fast_startup
  8385. *
  8386. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8387. *
  8388. * RETURN:
  8389. * 0 = success
  8390. * !0 = failure
  8391. *
  8392. * PARAMETERS:
  8393. * *cd - pointer the core data structure
  8394. ******************************************************************************/
  8395. static int _fast_startup(struct pt_core_data *cd)
  8396. {
  8397. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8398. int rc = 0;
  8399. u8 mode = PT_MODE_UNKNOWN;
  8400. struct pt_hid_desc hid_desc;
  8401. int wait_time = 0;
  8402. memset(&hid_desc, 0, sizeof(hid_desc));
  8403. reset:
  8404. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8405. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8406. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8407. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8408. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8409. __func__);
  8410. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8411. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8412. if (rc < 0) {
  8413. pt_debug(cd->dev, DL_ERROR,
  8414. "%s: Error on getting HID descriptor r=%d\n",
  8415. __func__, rc);
  8416. if (retry--)
  8417. goto reset;
  8418. goto exit;
  8419. }
  8420. cd->mode = pt_get_mode(cd, &hid_desc);
  8421. if (cd->mode == PT_MODE_BOOTLOADER) {
  8422. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8423. __func__);
  8424. rc = pt_hid_output_bl_launch_app_(cd);
  8425. if (rc < 0) {
  8426. pt_debug(cd->dev, DL_ERROR,
  8427. "%s: Error on launch app r=%d\n",
  8428. __func__, rc);
  8429. if (retry--)
  8430. goto reset;
  8431. goto exit;
  8432. }
  8433. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8434. if (rc < 0) {
  8435. pt_debug(cd->dev, DL_ERROR,
  8436. "%s: Error on getting HID descriptor r=%d\n",
  8437. __func__, rc);
  8438. if (retry--)
  8439. goto reset;
  8440. goto exit;
  8441. }
  8442. cd->mode = pt_get_mode(cd, &hid_desc);
  8443. if (cd->mode == PT_MODE_BOOTLOADER) {
  8444. if (retry--)
  8445. goto reset;
  8446. goto exit;
  8447. }
  8448. }
  8449. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8450. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8451. } else {
  8452. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8453. __func__);
  8454. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8455. /* Wait for any sentinel before first try */
  8456. rc = _pt_request_wait_for_enum_state(
  8457. cd->dev, 150,
  8458. STARTUP_STATUS_BL_RESET_SENTINEL |
  8459. STARTUP_STATUS_FW_RESET_SENTINEL);
  8460. if (rc)
  8461. pt_debug(cd->dev, DL_ERROR,
  8462. "%s: No Sentinel detected rc = %d\n",
  8463. __func__, rc);
  8464. } else
  8465. pt_flush_bus_if_irq_asserted(cd,
  8466. PT_FLUSH_BUS_BASED_ON_LEN);
  8467. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8468. if (rc) {
  8469. pt_debug(cd->dev, DL_ERROR,
  8470. "%s: Get mode failed, mode unknown\n",
  8471. __func__);
  8472. mode = PT_MODE_UNKNOWN;
  8473. }
  8474. cd->mode = mode;
  8475. if (cd->mode == PT_MODE_BOOTLOADER) {
  8476. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8477. __func__);
  8478. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8479. if (rc) {
  8480. pt_debug(cd->dev, DL_ERROR,
  8481. "%s Failed to exit bootloader\n",
  8482. __func__);
  8483. msleep(50);
  8484. rc = -ENODEV;
  8485. if (retry--)
  8486. goto reset;
  8487. goto exit;
  8488. } else {
  8489. pt_debug(cd->dev, DL_INFO,
  8490. "%s: Exit bootloader successfully\n",
  8491. __func__);
  8492. }
  8493. if (cd->mode != PT_MODE_OPERATIONAL) {
  8494. pt_debug(cd->dev, DL_WARN,
  8495. "%s: restore mode failure mode = %d\n",
  8496. __func__, cd->mode);
  8497. if (retry--)
  8498. goto reset;
  8499. goto exit;
  8500. }
  8501. }
  8502. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8503. }
  8504. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8505. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8506. if (!rc) {
  8507. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8508. pt_debug(cd->dev, DL_WARN,
  8509. "%s: Exit FW BOOT Mode after %dms\n",
  8510. __func__, wait_time);
  8511. } else {
  8512. pt_debug(cd->dev, DL_WARN,
  8513. "%s: FW stuck in BOOT Mode after %dms\n",
  8514. __func__, wait_time);
  8515. goto exit;
  8516. }
  8517. if (!cd->sysinfo.ready) {
  8518. rc = pt_hid_output_get_sysinfo_(cd);
  8519. if (rc) {
  8520. pt_debug(cd->dev, DL_ERROR,
  8521. "%s: Error on getting sysinfo r=%d\n",
  8522. __func__, rc);
  8523. if (retry--)
  8524. goto reset;
  8525. goto exit;
  8526. }
  8527. }
  8528. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8529. rc = pt_restore_parameters_(cd);
  8530. if (rc)
  8531. pt_debug(cd->dev, DL_ERROR,
  8532. "%s: failed to restore parameters rc=%d\n",
  8533. __func__, rc);
  8534. else
  8535. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8536. exit:
  8537. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8538. return rc;
  8539. }
  8540. /*******************************************************************************
  8541. * FUNCTION: pt_core_poweron_device_
  8542. *
  8543. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8544. *
  8545. * RETURN:
  8546. * 0 = success
  8547. * !0 = failure
  8548. *
  8549. * PARAMETERS:
  8550. * *cd - pointer to core data
  8551. ******************************************************************************/
  8552. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8553. {
  8554. struct device *dev = cd->dev;
  8555. int rc = 0;
  8556. /*
  8557. * After power on action, the chip can general FW sentinel. It can
  8558. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8559. * _fast_startup() can perform a quick enumeration too, here doesn't
  8560. * need another enumeration.
  8561. */
  8562. mutex_lock(&cd->system_lock);
  8563. cd->startup_status = STARTUP_STATUS_START;
  8564. cd->hid_reset_cmd_state = 1;
  8565. mutex_unlock(&cd->system_lock);
  8566. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8567. if (rc < 0) {
  8568. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8569. __func__, rc);
  8570. goto exit;
  8571. }
  8572. if (!cd->irq_enabled) {
  8573. cd->irq_enabled = true;
  8574. enable_irq(cd->irq);
  8575. }
  8576. /* TBD: following function doesn't update startup_status */
  8577. rc = _fast_startup(cd);
  8578. exit:
  8579. return rc;
  8580. }
  8581. /*******************************************************************************
  8582. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8583. *
  8584. * SUMMARY: Reset device, and then trigger a full enumeration.
  8585. *
  8586. * RETURN:
  8587. * 0 = success
  8588. * !0 = failure
  8589. *
  8590. * PARAMETERS:
  8591. * *cd - pointer to core data
  8592. ******************************************************************************/
  8593. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8594. {
  8595. int rc;
  8596. rc = pt_dut_reset_and_wait(cd);
  8597. if (rc < 0) {
  8598. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8599. __func__, rc);
  8600. goto exit;
  8601. }
  8602. rc = _fast_startup(cd);
  8603. exit:
  8604. return rc;
  8605. }
  8606. /*******************************************************************************
  8607. * FUNCTION: pt_core_easywake_off_
  8608. *
  8609. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8610. * the configuration in the core platform data structure.
  8611. *
  8612. * RETURN:
  8613. * 0 = success
  8614. * !0 = failure
  8615. *
  8616. * PARAMETERS:
  8617. * *cd - pointer to core data
  8618. ******************************************************************************/
  8619. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8620. {
  8621. int rc = 0;
  8622. mutex_lock(&cd->system_lock);
  8623. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8624. mutex_unlock(&cd->system_lock);
  8625. pt_debug(cd->dev, DL_INFO,
  8626. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8627. return 0;
  8628. }
  8629. mutex_unlock(&cd->system_lock);
  8630. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8631. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8632. rc = pt_core_wake_device_from_easy_wake_(cd);
  8633. if (rc < 0)
  8634. pt_debug(cd->dev, DL_ERROR,
  8635. "%s - %d failed %d\n", __func__, rc);
  8636. }
  8637. mutex_lock(&cd->system_lock);
  8638. cd->sleep_state = SS_EASY_WAKING_OFF;
  8639. mutex_unlock(&cd->system_lock);
  8640. pt_start_wd_timer(cd);
  8641. return rc;
  8642. }
  8643. /*******************************************************************************
  8644. * FUNCTION: pt_core_easywake_off
  8645. *
  8646. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8647. *
  8648. * RETURN:
  8649. * 0 = success
  8650. * !0 = failure
  8651. *
  8652. * PARAMETERS:
  8653. * *cd - pointer to core data
  8654. ******************************************************************************/
  8655. static int pt_core_easywake_off(struct pt_core_data *cd)
  8656. {
  8657. int rc;
  8658. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8659. if (rc < 0) {
  8660. pt_debug(cd->dev, DL_ERROR,
  8661. "%s: fail get exclusive ex=%p own=%p\n",
  8662. __func__, cd->exclusive_dev, cd->dev);
  8663. return rc;
  8664. }
  8665. rc = pt_core_easywake_off_(cd);
  8666. if (release_exclusive(cd, cd->dev) < 0)
  8667. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8668. __func__);
  8669. else
  8670. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8671. __func__);
  8672. return rc;
  8673. }
  8674. /*******************************************************************************
  8675. * FUNCTION: pt_core_wake_
  8676. *
  8677. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8678. * the configuration in the core platform data structure.
  8679. *
  8680. * RETURN:
  8681. * 0 = success
  8682. * !0 = failure
  8683. *
  8684. * PARAMETERS:
  8685. * *cd - pointer to core data
  8686. ******************************************************************************/
  8687. static int pt_core_wake_(struct pt_core_data *cd)
  8688. {
  8689. int rc = 0;
  8690. mutex_lock(&cd->system_lock);
  8691. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8692. mutex_unlock(&cd->system_lock);
  8693. pt_debug(cd->dev, DL_INFO,
  8694. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8695. return 0;
  8696. } else {
  8697. cd->sleep_state = SS_WAKING;
  8698. }
  8699. mutex_unlock(&cd->system_lock);
  8700. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8701. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8702. pt_debug(cd->dev, DL_INFO,
  8703. "%s: Entering into poweron mode:\n", __func__);
  8704. rc = pt_core_poweron_device_(cd);
  8705. if (rc < 0)
  8706. pr_err("%s: Poweron error detected: rc=%d\n",
  8707. __func__, rc);
  8708. }
  8709. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8710. rc = pt_core_wake_device_from_deep_standby_(cd);
  8711. else /* Default action to exit DeepSleep */
  8712. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8713. }
  8714. mutex_lock(&cd->system_lock);
  8715. cd->sleep_state = SS_SLEEP_OFF;
  8716. mutex_unlock(&cd->system_lock);
  8717. pt_start_wd_timer(cd);
  8718. return rc;
  8719. }
  8720. /*******************************************************************************
  8721. * FUNCTION: pt_core_wake_
  8722. *
  8723. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8724. *
  8725. * RETURN:
  8726. * 0 = success
  8727. * !0 = failure
  8728. *
  8729. * PARAMETERS:
  8730. * *cd - pointer to core data
  8731. ******************************************************************************/
  8732. static int pt_core_wake(struct pt_core_data *cd)
  8733. {
  8734. int rc = 0;
  8735. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8736. if (rc < 0) {
  8737. pt_debug(cd->dev, DL_ERROR,
  8738. "%s: fail get exclusive ex=%p own=%p\n",
  8739. __func__, cd->exclusive_dev, cd->dev);
  8740. return rc;
  8741. }
  8742. rc = pt_core_wake_(cd);
  8743. if (release_exclusive(cd, cd->dev) < 0)
  8744. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8745. __func__);
  8746. else
  8747. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8748. __func__);
  8749. return rc;
  8750. }
  8751. /*******************************************************************************
  8752. * FUNCTION: pt_get_ic_crc_
  8753. *
  8754. * SUMMARY: This function retrieves the config block CRC
  8755. *
  8756. * NOTE: The post condition of calling this function will be that the DUT will
  8757. * be in SCANNINING mode if no failures occur
  8758. *
  8759. * RETURN:
  8760. * 0 = success
  8761. * !0 = failure
  8762. *
  8763. * PARAMETERS:
  8764. * *cd - pointer the core data structure
  8765. * ebid - enumerated block ID
  8766. ******************************************************************************/
  8767. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8768. {
  8769. struct pt_sysinfo *si = &cd->sysinfo;
  8770. int rc = 0;
  8771. u8 status;
  8772. u16 calculated_crc = 0;
  8773. u16 stored_crc = 0;
  8774. rc = pt_pip_suspend_scanning_(cd);
  8775. if (rc)
  8776. goto error;
  8777. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8778. &calculated_crc, &stored_crc);
  8779. if (rc)
  8780. goto exit;
  8781. if (status) {
  8782. rc = -EINVAL;
  8783. goto exit;
  8784. }
  8785. si->ttconfig.crc = stored_crc;
  8786. exit:
  8787. pt_pip_resume_scanning_(cd);
  8788. error:
  8789. pt_debug(cd->dev, DL_INFO,
  8790. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8791. __func__, ebid, calculated_crc, stored_crc, rc);
  8792. return rc;
  8793. }
  8794. /*******************************************************************************
  8795. * FUNCTION: pt_pip2_read_gpio
  8796. *
  8797. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8798. * bits into the passed in variable
  8799. *
  8800. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8801. *
  8802. * RETURN:
  8803. * 0 = success
  8804. * !0 = failure
  8805. *
  8806. * PARAMETERS:
  8807. * *dev - pointer to device structure
  8808. * *status - pointer to where the command response status is stored
  8809. * *gpio - pointer to device attributes structure
  8810. ******************************************************************************/
  8811. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8812. {
  8813. int rc = 0;
  8814. u16 actual_read_len;
  8815. u8 read_buf[12];
  8816. u8 tmp_status = 0;
  8817. u8 index = PIP2_RESP_STATUS_OFFSET;
  8818. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8819. rc = _pt_request_pip2_send_cmd(dev,
  8820. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8821. NULL, 0, read_buf, &actual_read_len);
  8822. if (rc) {
  8823. pt_debug(dev, DL_ERROR,
  8824. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8825. rc = -ECOMM;
  8826. } else {
  8827. tmp_status = read_buf[index];
  8828. }
  8829. if (status)
  8830. *status = tmp_status;
  8831. if (!rc && gpio && (tmp_status == 0)) {
  8832. *gpio = ((read_buf[index + 4] << 24) |
  8833. (read_buf[index + 3] << 16) |
  8834. (read_buf[index + 2] << 8) |
  8835. (read_buf[index + 1]));
  8836. }
  8837. return rc;
  8838. }
  8839. /*******************************************************************************
  8840. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8841. *
  8842. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8843. *
  8844. * RETURN:
  8845. * 0 = success
  8846. * !0 = failure
  8847. *
  8848. * PARAMETERS:
  8849. * *cd - pointer the core data structure
  8850. * *pid - pointer to store panel ID
  8851. ******************************************************************************/
  8852. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8853. {
  8854. u32 gpio_value = 0;
  8855. u8 status = 0;
  8856. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8857. int rc = 0;
  8858. if (!pid)
  8859. return -ENOMEM;
  8860. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8861. if (!rc) {
  8862. if (status == 0) {
  8863. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8864. PT_PANEL_ID_SHIFT;
  8865. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8866. __func__,
  8867. "BL mode PID", panel_id, "gpio", gpio_value);
  8868. *pid = panel_id;
  8869. } else {
  8870. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8871. __func__,
  8872. "BL read gpio failed status", status);
  8873. }
  8874. } else {
  8875. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8876. __func__,
  8877. "BL read gpio failed status", status);
  8878. }
  8879. return rc;
  8880. }
  8881. /*******************************************************************************
  8882. * FUNCTION: pt_enum_with_dut_
  8883. *
  8884. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8885. * The core data (cd) startup_status will store, as a bitmask, each
  8886. * state of the enumeration process. The startup will be attempted
  8887. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8888. *
  8889. * RETURN:
  8890. * 0 = success
  8891. * !0 = failure
  8892. *
  8893. * PARAMETERS:
  8894. * *cd - pointer the core data structure
  8895. * reset - Flag to reset the DUT before attempting to enumerate
  8896. * *status - poionter to store the enum status bitmask flags
  8897. ******************************************************************************/
  8898. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8899. u32 *enum_status)
  8900. {
  8901. int try = 1;
  8902. int rc = 0;
  8903. int wait_time = 0;
  8904. bool detected = false;
  8905. u8 return_data[8];
  8906. u8 mode = PT_MODE_UNKNOWN;
  8907. u8 pid = PANEL_ID_NOT_ENABLED;
  8908. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8909. struct pt_hid_desc hid_desc;
  8910. memset(&hid_desc, 0, sizeof(hid_desc));
  8911. #ifdef TTHE_TUNER_SUPPORT
  8912. tthe_print(cd, NULL, 0, "enter startup");
  8913. #endif
  8914. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8915. __func__, cd->startup_status, reset);
  8916. pt_stop_wd_timer(cd);
  8917. reset:
  8918. if (try > 1)
  8919. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8920. __func__, try);
  8921. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8922. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8923. pt_debug(cd->dev, DL_INFO,
  8924. "%s: PIP1 Enumeration start\n", __func__);
  8925. /* Only reset the DUT after the first try */
  8926. if (reset || (try > 1)) {
  8927. /*
  8928. * Reset hardware only for Legacy parts. Skip for TT/TC
  8929. * parts because if the FW image was loaded directly
  8930. * to SRAM issueing a reset ill wipe out what was just
  8931. * loaded.
  8932. */
  8933. rc = pt_dut_reset_and_wait(cd);
  8934. if (rc < 0) {
  8935. pt_debug(cd->dev, DL_ERROR,
  8936. "%s: Error on h/w reset r=%d\n",
  8937. __func__, rc);
  8938. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8939. goto reset;
  8940. goto exit;
  8941. }
  8942. /* sleep to allow FW to be launched if available */
  8943. msleep(120);
  8944. }
  8945. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8946. if (rc < 0) {
  8947. pt_debug(cd->dev, DL_ERROR,
  8948. "%s: Error getting HID Descriptor r=%d\n",
  8949. __func__, rc);
  8950. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8951. goto reset;
  8952. rc = -EIO;
  8953. goto exit;
  8954. }
  8955. detected = true;
  8956. cd->mode = pt_get_mode(cd, &hid_desc);
  8957. /*
  8958. * Most systems do not use an XY pin as the panel_id and so
  8959. * the BL is used to retrieve the panel_id, however for
  8960. * systems that do use an XY pin, the panel_id MUST be
  8961. * retrieved from the system information when running FW
  8962. * (done below) and so this section of code is skipped.
  8963. * Entering the BL here is only needed on XY_PIN systems.
  8964. */
  8965. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8966. if (cd->mode == PT_MODE_OPERATIONAL) {
  8967. rc = pt_pip_start_bootloader_(cd);
  8968. if (rc < 0) {
  8969. pt_debug(cd->dev, DL_ERROR,
  8970. "%s: Error on start bootloader r=%d\n",
  8971. __func__, rc);
  8972. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8973. goto reset;
  8974. goto exit;
  8975. }
  8976. cd->mode = PT_MODE_BOOTLOADER;
  8977. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8978. __func__);
  8979. }
  8980. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8981. if (!rc) {
  8982. cd->bl_info.ready = true;
  8983. cd->bl_info.chip_id =
  8984. get_unaligned_le16(&return_data[2]);
  8985. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8986. __func__, cd->bl_info.chip_id);
  8987. } else {
  8988. pt_debug(cd->dev, DL_ERROR,
  8989. "%s: failed to get chip ID, r=%d\n",
  8990. __func__, rc);
  8991. }
  8992. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8993. mutex_lock(&cd->system_lock);
  8994. if (!rc) {
  8995. cd->pid_for_loader = pid;
  8996. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8997. __func__, cd->pid_for_loader);
  8998. } else {
  8999. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  9000. pt_debug(cd->dev, DL_WARN,
  9001. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9002. __func__, cd->pid_for_loader);
  9003. }
  9004. mutex_unlock(&cd->system_lock);
  9005. }
  9006. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9007. if (cd->mode == PT_MODE_BOOTLOADER) {
  9008. rc = pt_hid_output_bl_launch_app_(cd);
  9009. if (rc < 0) {
  9010. pt_debug(cd->dev, DL_ERROR,
  9011. "%s: Error on launch app r=%d\n",
  9012. __func__, rc);
  9013. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9014. goto reset;
  9015. rc = -ENODEV;
  9016. goto exit;
  9017. }
  9018. /* sleep to allow FW to be launched if available */
  9019. msleep(120);
  9020. /* Ensure the DUT is now in Application mode */
  9021. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9022. if (rc < 0) {
  9023. pt_debug(cd->dev, DL_ERROR,
  9024. "%s: Error getting HID Desc r=%d\n",
  9025. __func__, rc);
  9026. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9027. goto reset;
  9028. rc = -EIO;
  9029. goto exit;
  9030. }
  9031. cd->mode = pt_get_mode(cd, &hid_desc);
  9032. if (cd->mode == PT_MODE_BOOTLOADER) {
  9033. pt_debug(cd->dev, DL_WARN,
  9034. "%s: Error confiming exit BL\n",
  9035. __func__);
  9036. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9037. goto reset;
  9038. rc = -ENODEV;
  9039. goto exit;
  9040. }
  9041. }
  9042. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9043. cd->mode = PT_MODE_OPERATIONAL;
  9044. *enum_status |= STARTUP_STATUS_GET_DESC;
  9045. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9046. } else {
  9047. /* Generation is PIP2 Capable */
  9048. pt_debug(cd->dev, DL_INFO,
  9049. "%s: PIP2 Enumeration start\n", __func__);
  9050. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9051. if (rc) {
  9052. pt_debug(cd->dev, DL_ERROR,
  9053. "%s: Get mode failed, mode unknown\n",
  9054. __func__);
  9055. mode = PT_MODE_UNKNOWN;
  9056. } else
  9057. detected = true;
  9058. cd->mode = mode;
  9059. switch (cd->mode) {
  9060. case PT_MODE_OPERATIONAL:
  9061. pt_debug(cd->dev, DL_INFO,
  9062. "%s: Operational mode\n", __func__);
  9063. if (cd->app_pip_ver_ready == false) {
  9064. rc = pt_pip2_get_version_(cd);
  9065. if (!rc)
  9066. cd->app_pip_ver_ready = true;
  9067. else {
  9068. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9069. goto reset;
  9070. goto exit;
  9071. }
  9072. }
  9073. break;
  9074. case PT_MODE_BOOTLOADER:
  9075. pt_debug(cd->dev, DL_INFO,
  9076. "%s: Bootloader mode\n", __func__);
  9077. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9078. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9079. mutex_lock(&cd->system_lock);
  9080. if (!rc) {
  9081. cd->pid_for_loader = pid;
  9082. pt_debug(cd->dev, DL_INFO,
  9083. "%s: Panel ID: 0x%02X\n",
  9084. __func__, cd->pid_for_loader);
  9085. } else {
  9086. cd->pid_for_loader =
  9087. PANEL_ID_NOT_ENABLED;
  9088. pt_debug(cd->dev, DL_WARN,
  9089. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9090. __func__, cd->pid_for_loader);
  9091. }
  9092. mutex_unlock(&cd->system_lock);
  9093. }
  9094. if (cd->bl_pip_ver_ready == false) {
  9095. rc = pt_pip2_get_version_(cd);
  9096. if (!rc)
  9097. cd->bl_pip_ver_ready = true;
  9098. else {
  9099. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9100. goto reset;
  9101. goto exit;
  9102. }
  9103. }
  9104. /*
  9105. * Launch app command will fail in flashless mode.
  9106. * Skip launch app command here to save time for
  9107. * enumeration flow.
  9108. */
  9109. if (cd->flashless_dut)
  9110. goto exit;
  9111. /*
  9112. * pt_pip2_launch_app() is needed here instead of
  9113. * pt_pip2_exit_bl() because exit_bl will cause several
  9114. * hw_resets to occur and the auto BL on a flashless
  9115. * DUT will fail.
  9116. */
  9117. rc = pt_pip2_launch_app(cd->dev,
  9118. PT_CORE_CMD_UNPROTECTED);
  9119. if (rc) {
  9120. pt_debug(cd->dev, DL_ERROR,
  9121. "%s: Error on launch app r=%d\n",
  9122. __func__, rc);
  9123. msleep(50);
  9124. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9125. goto reset;
  9126. goto exit;
  9127. }
  9128. /*
  9129. * IRQ thread can be delayed if the serial log print is
  9130. * enabled. It causes next command to get wrong response
  9131. * Here the delay is to ensure pt_parse_input() to be
  9132. * finished.
  9133. */
  9134. msleep(60);
  9135. /* Check and update the mode */
  9136. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9137. if (rc) {
  9138. pt_debug(cd->dev, DL_ERROR,
  9139. "%s: Get mode failed, mode unknown\n",
  9140. __func__);
  9141. mode = PT_MODE_UNKNOWN;
  9142. }
  9143. cd->mode = mode;
  9144. if (cd->mode == PT_MODE_OPERATIONAL) {
  9145. pt_debug(cd->dev, DL_INFO,
  9146. "%s: Launched to Operational mode\n",
  9147. __func__);
  9148. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9149. pt_debug(cd->dev, DL_ERROR,
  9150. "%s: Launch failed, Bootloader mode\n",
  9151. __func__);
  9152. goto exit;
  9153. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9154. pt_debug(cd->dev, DL_ERROR,
  9155. "%s: Launch failed, Unknown mode\n",
  9156. __func__);
  9157. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9158. goto reset;
  9159. goto exit;
  9160. }
  9161. if (cd->app_pip_ver_ready == false) {
  9162. rc = pt_pip2_get_version_(cd);
  9163. if (!rc)
  9164. cd->app_pip_ver_ready = true;
  9165. else {
  9166. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9167. goto reset;
  9168. goto exit;
  9169. }
  9170. }
  9171. break;
  9172. default:
  9173. pt_debug(cd->dev, DL_ERROR,
  9174. "%s: Unknown mode\n", __func__);
  9175. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9176. goto reset;
  9177. goto exit;
  9178. }
  9179. *enum_status |= STARTUP_STATUS_GET_DESC;
  9180. }
  9181. pt_init_pip_report_fields(cd);
  9182. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9183. if (!cd->features.easywake)
  9184. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9185. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9186. rc = pt_hid_output_get_sysinfo_(cd);
  9187. if (rc) {
  9188. pt_debug(cd->dev, DL_ERROR,
  9189. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9190. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9191. goto reset;
  9192. goto exit;
  9193. }
  9194. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9195. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9196. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9197. if (!rc) {
  9198. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9199. pt_debug(cd->dev, DL_WARN,
  9200. "%s: Exit FW BOOT Mode after %dms\n",
  9201. __func__, wait_time);
  9202. } else {
  9203. pt_debug(cd->dev, DL_WARN,
  9204. "%s: FW stuck in BOOT Mode after %dms\n",
  9205. __func__, wait_time);
  9206. goto exit;
  9207. }
  9208. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9209. __func__,
  9210. cd->sysinfo.ttdata.pip_ver_major,
  9211. cd->sysinfo.ttdata.pip_ver_minor);
  9212. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9213. if (rc) {
  9214. pt_debug(cd->dev, DL_ERROR,
  9215. "%s: DUT Config block CRC failure rc=%d\n",
  9216. __func__, rc);
  9217. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9218. goto reset;
  9219. } else {
  9220. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9221. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9222. pt_debug(cd->dev, DL_ERROR,
  9223. "%s: scan state: %d, retry: %d\n",
  9224. __func__, sys_mode, try);
  9225. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9226. goto reset;
  9227. } else
  9228. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9229. }
  9230. rc = pt_restore_parameters_(cd);
  9231. if (rc) {
  9232. pt_debug(cd->dev, DL_ERROR,
  9233. "%s: Failed to restore parameters rc=%d\n",
  9234. __func__, rc);
  9235. } else
  9236. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9237. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9238. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9239. cd->startup_retry_count = 0;
  9240. exit:
  9241. /* Generate the HW Version of the connected DUT and store in cd */
  9242. pt_generate_hw_version(cd, cd->hw_version);
  9243. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9244. cd->hw_version);
  9245. pt_start_wd_timer(cd);
  9246. if (!detected)
  9247. rc = -ENODEV;
  9248. #ifdef TTHE_TUNER_SUPPORT
  9249. tthe_print(cd, NULL, 0, "exit startup");
  9250. #endif
  9251. pt_debug(cd->dev, DL_WARN,
  9252. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9253. __func__, rc, try);
  9254. return rc;
  9255. }
  9256. /*******************************************************************************
  9257. * FUNCTION: pt_enum_with_dut
  9258. *
  9259. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9260. * requesting exclusive access around the call.
  9261. *
  9262. * RETURN:
  9263. * 0 = success
  9264. * !0 = failure
  9265. *
  9266. * PARAMETERS:
  9267. * *cd - pointer the core data structure
  9268. * reset - Flag to reset the DUT before attempting to enumerate
  9269. * *status - pointer to store the enum status bitmask flags
  9270. ******************************************************************************/
  9271. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9272. {
  9273. int rc = 0;
  9274. mutex_lock(&cd->system_lock);
  9275. cd->startup_state = STARTUP_RUNNING;
  9276. mutex_unlock(&cd->system_lock);
  9277. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9278. if (rc) {
  9279. pt_debug(cd->dev, DL_ERROR,
  9280. "%s: fail get exclusive ex=%p own=%p\n",
  9281. __func__, cd->exclusive_dev, cd->dev);
  9282. goto exit;
  9283. }
  9284. rc = pt_enum_with_dut_(cd, reset, status);
  9285. if (release_exclusive(cd, cd->dev) < 0)
  9286. /* Don't return fail code, mode is already changed. */
  9287. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9288. __func__);
  9289. else
  9290. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9291. __func__);
  9292. exit:
  9293. mutex_lock(&cd->system_lock);
  9294. /* Clear startup state for any tasks waiting for startup completion */
  9295. cd->startup_state = STARTUP_NONE;
  9296. mutex_unlock(&cd->system_lock);
  9297. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9298. *status |= STARTUP_STATUS_COMPLETE;
  9299. /* Wake the waiters for end of startup */
  9300. wake_up(&cd->wait_q);
  9301. return rc;
  9302. }
  9303. static int add_sysfs_interfaces(struct device *dev);
  9304. static void remove_sysfs_interfaces(struct device *dev);
  9305. static void remove_sysfs_and_modules(struct device *dev);
  9306. static void pt_release_modules(struct pt_core_data *cd);
  9307. static void pt_probe_modules(struct pt_core_data *cd);
  9308. /*******************************************************************************
  9309. * FUNCTION: _pt_ttdl_restart
  9310. *
  9311. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9312. *
  9313. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9314. * existing sysfs nodes will produce an error.
  9315. *
  9316. * RETURN:
  9317. * 0 = success
  9318. * !0 = failure
  9319. *
  9320. * PARAMETERS:
  9321. * *dev - pointer to core device
  9322. ******************************************************************************/
  9323. static int _pt_ttdl_restart(struct device *dev)
  9324. {
  9325. int rc = 0;
  9326. struct pt_core_data *cd = dev_get_drvdata(dev);
  9327. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9328. struct i2c_client *client =
  9329. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9330. #endif
  9331. /*
  9332. * Make sure the device is awake, pt_mt_release function will
  9333. * cause pm sleep function and lead to deadlock.
  9334. */
  9335. pm_runtime_get_sync(dev);
  9336. /* Use ttdl_restart_lock to avoid reentry */
  9337. mutex_lock(&cd->ttdl_restart_lock);
  9338. remove_sysfs_and_modules(cd->dev);
  9339. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9340. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9341. pt_debug(dev, DL_ERROR,
  9342. "%s I2C functionality not Supported\n", __func__);
  9343. rc = -EIO;
  9344. goto ttdl_no_error;
  9345. }
  9346. #endif
  9347. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9348. rc = _pt_detect_dut_generation(cd->dev,
  9349. &cd->startup_status, &cd->active_dut_generation,
  9350. &cd->mode);
  9351. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9352. pt_debug(dev, DL_ERROR,
  9353. "%s: Error, Unknown DUT Generation rc=%d\n",
  9354. __func__, rc);
  9355. }
  9356. }
  9357. rc = add_sysfs_interfaces(cd->dev);
  9358. if (rc < 0)
  9359. pt_debug(cd->dev, DL_ERROR,
  9360. "%s: Error, failed sysfs nodes rc=%d\n",
  9361. __func__, rc);
  9362. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9363. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9364. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9365. if (rc < 0)
  9366. pt_debug(dev, DL_ERROR,
  9367. "%s: Error, Failed to Enumerate\n", __func__);
  9368. }
  9369. rc = pt_mt_probe(dev);
  9370. if (rc < 0) {
  9371. pt_debug(dev, DL_ERROR,
  9372. "%s: Error, fail mt probe\n", __func__);
  9373. }
  9374. rc = pt_btn_probe(dev);
  9375. if (rc < 0) {
  9376. pt_debug(dev, DL_ERROR,
  9377. "%s: Error, fail btn probe\n", __func__);
  9378. }
  9379. pt_probe_modules(cd);
  9380. pt_debug(cd->dev, DL_WARN,
  9381. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9382. rc = 0;
  9383. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9384. ttdl_no_error:
  9385. #endif
  9386. mutex_unlock(&cd->ttdl_restart_lock);
  9387. mutex_lock(&cd->system_lock);
  9388. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9389. cd->startup_state = STARTUP_NONE;
  9390. mutex_unlock(&cd->system_lock);
  9391. pm_runtime_put(dev);
  9392. return rc;
  9393. }
  9394. /*******************************************************************************
  9395. * FUNCTION: pt_restart_work_function
  9396. *
  9397. * SUMMARY: This is the wrapper function placed in a work queue to call
  9398. * _pt_ttdl_restart()
  9399. *
  9400. * RETURN: none
  9401. *
  9402. * PARAMETERS:
  9403. * *work - pointer to the work_struct
  9404. ******************************************************************************/
  9405. static void pt_restart_work_function(struct work_struct *work)
  9406. {
  9407. struct pt_core_data *cd = container_of(work,
  9408. struct pt_core_data, ttdl_restart_work);
  9409. int rc = 0;
  9410. rc = _pt_ttdl_restart(cd->dev);
  9411. if (rc < 0)
  9412. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9413. __func__, rc);
  9414. }
  9415. /*******************************************************************************
  9416. * FUNCTION: pt_enum_work_function
  9417. *
  9418. * SUMMARY: This is the wrapper function placed in a work queue to call
  9419. * pt_enum_with_dut()
  9420. *
  9421. * RETURN: none
  9422. *
  9423. * PARAMETERS:
  9424. * *work - pointer to the work_struct
  9425. ******************************************************************************/
  9426. static void pt_enum_work_function(struct work_struct *work)
  9427. {
  9428. struct pt_core_data *cd = container_of(work,
  9429. struct pt_core_data, enum_work);
  9430. int rc;
  9431. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9432. if (rc < 0)
  9433. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9434. __func__, rc);
  9435. }
  9436. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9437. {
  9438. int rc;
  9439. if (!get) {
  9440. rc = 0;
  9441. goto regulator_put;
  9442. }
  9443. cd->vdd = regulator_get(cd->dev, "vdd");
  9444. if (IS_ERR(cd->vdd)) {
  9445. rc = PTR_ERR(cd->vdd);
  9446. dev_err(cd->dev,
  9447. "Regulator get failed vdd rc=%d\n", rc);
  9448. goto regulator_put;
  9449. }
  9450. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9451. if (IS_ERR(cd->vcc_i2c)) {
  9452. rc = PTR_ERR(cd->vcc_i2c);
  9453. dev_err(cd->dev,
  9454. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9455. goto regulator_put;
  9456. }
  9457. return 0;
  9458. regulator_put:
  9459. if (cd->vdd) {
  9460. regulator_put(cd->vdd);
  9461. cd->vdd = NULL;
  9462. }
  9463. if (cd->vcc_i2c) {
  9464. regulator_put(cd->vcc_i2c);
  9465. cd->vcc_i2c = NULL;
  9466. }
  9467. return rc;
  9468. }
  9469. #ifdef ENABLE_I2C_REG_ONLY
  9470. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9471. {
  9472. int rc = 0;
  9473. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9474. if (!en) {
  9475. rc = 0;
  9476. goto disable_vcc_i2c_reg_only;
  9477. }
  9478. if (cd->vcc_i2c) {
  9479. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9480. if (rc < 0)
  9481. pt_debug(cd->dev, DL_INFO,
  9482. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9483. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9484. __func__, rc);
  9485. }
  9486. return 0;
  9487. disable_vcc_i2c_reg_only:
  9488. if (cd->vcc_i2c) {
  9489. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9490. if (rc < 0)
  9491. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9492. __func__, rc);
  9493. }
  9494. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9495. return rc;
  9496. }
  9497. #endif
  9498. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9499. {
  9500. int rc;
  9501. if (!en) {
  9502. rc = 0;
  9503. goto disable_vcc_i2c_reg;
  9504. }
  9505. if (cd->vdd) {
  9506. if (regulator_count_voltages(cd->vdd) > 0) {
  9507. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9508. FT_VTG_MAX_UV);
  9509. if (rc) {
  9510. dev_err(cd->dev,
  9511. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9512. goto exit;
  9513. }
  9514. }
  9515. rc = regulator_enable(cd->vdd);
  9516. if (rc) {
  9517. dev_err(cd->dev,
  9518. "Regulator vdd enable failed rc=%d\n", rc);
  9519. goto exit;
  9520. }
  9521. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9522. }
  9523. if (cd->vcc_i2c) {
  9524. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9525. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9526. FT_I2C_VTG_MAX_UV);
  9527. if (rc) {
  9528. dev_err(cd->dev,
  9529. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9530. goto disable_vdd_reg;
  9531. }
  9532. }
  9533. rc = regulator_enable(cd->vcc_i2c);
  9534. if (rc) {
  9535. dev_err(cd->dev,
  9536. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9537. goto disable_vdd_reg;
  9538. }
  9539. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9540. }
  9541. return 0;
  9542. disable_vcc_i2c_reg:
  9543. if (cd->vcc_i2c) {
  9544. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9545. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9546. FT_I2C_VTG_MAX_UV);
  9547. regulator_disable(cd->vcc_i2c);
  9548. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9549. }
  9550. disable_vdd_reg:
  9551. if (cd->vdd) {
  9552. if (regulator_count_voltages(cd->vdd) > 0)
  9553. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9554. FT_VTG_MAX_UV);
  9555. regulator_disable(cd->vdd);
  9556. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9557. }
  9558. exit:
  9559. return rc;
  9560. }
  9561. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9562. #define KERNEL_VER_GT_3_19
  9563. #endif
  9564. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9565. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9566. #if defined(CONFIG_PM_SLEEP)
  9567. /*******************************************************************************
  9568. * FUNCTION: pt_core_rt_suspend
  9569. *
  9570. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9571. *
  9572. * RETURN:
  9573. * 0 = success
  9574. * !0 = failure
  9575. *
  9576. * PARAMETERS:
  9577. * *dev - pointer to core device
  9578. ******************************************************************************/
  9579. static int pt_core_rt_suspend(struct device *dev)
  9580. {
  9581. struct pt_core_data *cd = dev_get_drvdata(dev);
  9582. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9583. __func__, cd->core_probe_complete);
  9584. return 0;
  9585. }
  9586. /*******************************************************************************
  9587. * FUNCTION: pt_core_rt_resume
  9588. *
  9589. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9590. *
  9591. * RETURN:
  9592. * 0 = success
  9593. * !0 = failure
  9594. *
  9595. * PARAMETERS:
  9596. * *dev - pointer to core device
  9597. ******************************************************************************/
  9598. static int pt_core_rt_resume(struct device *dev)
  9599. {
  9600. struct pt_core_data *cd = dev_get_drvdata(dev);
  9601. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9602. __func__, cd->core_probe_complete);
  9603. return 0;
  9604. }
  9605. #endif /* CONFIG_PM_SLEEP */
  9606. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9607. #if defined(CONFIG_PM_SLEEP)
  9608. /*******************************************************************************
  9609. * FUNCTION: pt_core_suspend_
  9610. *
  9611. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9612. * pt_core_sleep. This function may disable IRQ during sleep state.
  9613. *
  9614. * RETURN:
  9615. * 0 = success
  9616. * !0 = failure
  9617. *
  9618. * PARAMETERS:
  9619. * *dev - pointer to core device
  9620. ******************************************************************************/
  9621. static int pt_core_suspend_(struct device *dev)
  9622. {
  9623. int rc;
  9624. struct pt_core_data *cd = dev_get_drvdata(dev);
  9625. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9626. rc = pt_core_sleep(cd);
  9627. if (rc) {
  9628. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9629. __func__, rc);
  9630. return -EAGAIN;
  9631. }
  9632. rc = pt_enable_regulator(cd, false);
  9633. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9634. return 0;
  9635. /* Required to prevent interrupts before bus awake */
  9636. disable_irq(cd->irq);
  9637. cd->irq_disabled = 1;
  9638. if (device_may_wakeup(dev)) {
  9639. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9640. __func__);
  9641. if (!enable_irq_wake(cd->irq))
  9642. cd->irq_wake = 1;
  9643. } else {
  9644. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9645. __func__);
  9646. }
  9647. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9648. return rc;
  9649. }
  9650. /*******************************************************************************
  9651. * FUNCTION: pt_core_suspend
  9652. *
  9653. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9654. * woke up or put to sleep based on Kernel power state even when the display
  9655. * is off based on the check of TTDL core platform flag.
  9656. *
  9657. * RETURN:
  9658. * 0 = success
  9659. * !0 = failure
  9660. *
  9661. * PARAMETERS:
  9662. * *dev - pointer to core device
  9663. ******************************************************************************/
  9664. static int pt_core_suspend(struct device *dev)
  9665. {
  9666. struct pt_core_data *cd = dev_get_drvdata(dev);
  9667. int rc = 0;
  9668. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9669. return 0;
  9670. pt_debug(cd->dev, DL_INFO, "%s Suspend start\n", __func__);
  9671. cancel_work_sync(&cd->resume_work);
  9672. cancel_work_sync(&cd->suspend_work);
  9673. mutex_lock(&cd->system_lock);
  9674. cd->wait_until_wake = 0;
  9675. mutex_unlock(&cd->system_lock);
  9676. if (pm_suspend_via_firmware()) {
  9677. rc = pt_core_suspend_(cd->dev);
  9678. cd->quick_boot = true;
  9679. } else {
  9680. rc = pt_enable_i2c_regulator(cd, false);
  9681. if (rc < 0)
  9682. pt_debug(cd->dev, DL_ERROR,
  9683. "%s: Error on disabling i2c regulator\n", __func__);
  9684. }
  9685. pt_debug(cd->dev, DL_INFO, "%s Suspend exit - rc = %d\n", __func__, rc);
  9686. return rc;
  9687. }
  9688. /*******************************************************************************
  9689. * FUNCTION: pt_core_resume_
  9690. *
  9691. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9692. * pt_core_wake. This function may enable IRQ before wake up.
  9693. *
  9694. * RETURN:
  9695. * 0 = success
  9696. * !0 = failure
  9697. *
  9698. * PARAMETERS:
  9699. * *dev - pointer to core device
  9700. ******************************************************************************/
  9701. static int pt_core_resume_(struct device *dev)
  9702. {
  9703. int rc = 0;
  9704. struct pt_core_data *cd = dev_get_drvdata(dev);
  9705. dev_info(dev, "%s: Entering into resume mode:\n",
  9706. __func__);
  9707. rc = pt_enable_regulator(cd, true);
  9708. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9709. __func__, rc);
  9710. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9711. goto exit;
  9712. /*
  9713. * Bus pm does not call suspend if device runtime suspended
  9714. * This flag is covers that case
  9715. */
  9716. if (cd->irq_disabled) {
  9717. enable_irq(cd->irq);
  9718. cd->irq_disabled = 0;
  9719. }
  9720. if (device_may_wakeup(dev)) {
  9721. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9722. __func__);
  9723. if (cd->irq_wake) {
  9724. disable_irq_wake(cd->irq);
  9725. cd->irq_wake = 0;
  9726. }
  9727. } else {
  9728. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9729. __func__);
  9730. }
  9731. exit:
  9732. rc = pt_core_wake(cd);
  9733. if (rc) {
  9734. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9735. __func__, rc);
  9736. pt_enable_regulator(cd, false);
  9737. return -EAGAIN;
  9738. }
  9739. return rc;
  9740. }
  9741. /*******************************************************************************
  9742. * FUNCTION: pt_core_restore
  9743. *
  9744. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9745. * pt_core_wake. This function may enable IRQ before wake up.
  9746. *
  9747. * RETURN:
  9748. * 0 = success
  9749. * !0 = failure
  9750. *
  9751. * PARAMETERS:
  9752. * *dev - pointer to core device
  9753. ******************************************************************************/
  9754. static int pt_core_restore(struct device *dev)
  9755. {
  9756. int rc = 0;
  9757. struct pt_core_data *cd = dev_get_drvdata(dev);
  9758. dev_info(dev, "%s: Entering into resume mode:\n",
  9759. __func__);
  9760. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9761. return rc;
  9762. }
  9763. /*******************************************************************************
  9764. * FUNCTION: suspend_offload_work
  9765. *
  9766. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9767. * along with kernel power state even the display is off based on the check of
  9768. * TTDL core platform flag.
  9769. *
  9770. * RETURN:
  9771. * 0 = success
  9772. * !0 = failure
  9773. *
  9774. * PARAMETERS:
  9775. * *dev - pointer to core device
  9776. ******************************************************************************/
  9777. static void pt_suspend_offload_work(struct work_struct *work)
  9778. {
  9779. int rc = 0;
  9780. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9781. suspend_offload_work);
  9782. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9783. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9784. return;
  9785. rc = pt_core_suspend_(cd->dev);
  9786. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9787. }
  9788. /*******************************************************************************
  9789. * FUNCTION: resume_offload_work
  9790. *
  9791. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9792. * along with kernel power state even the display is off based on the check of
  9793. * TTDL core platform flag.
  9794. *
  9795. * RETURN:
  9796. * 0 = success
  9797. * !0 = failure
  9798. *
  9799. * PARAMETERS:
  9800. * *dev - pointer to core device
  9801. ******************************************************************************/
  9802. static void pt_resume_offload_work(struct work_struct *work)
  9803. {
  9804. int rc = 0;
  9805. int retry_count = 10;
  9806. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9807. resume_offload_work);
  9808. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9809. do {
  9810. retry_count--;
  9811. rc = pt_core_resume_(pt_data->dev);
  9812. if (rc < 0)
  9813. pt_debug(pt_data->dev, DL_ERROR,
  9814. "%s: Error on wake\n", __func__);
  9815. } while (retry_count && rc < 0);
  9816. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9817. rc = pt_core_easywake_on(pt_data);
  9818. if (rc < 0) {
  9819. pt_debug(pt_data->dev, DL_ERROR,
  9820. "%s: Error on enable touch to wake key\n",
  9821. __func__);
  9822. return;
  9823. }
  9824. pt_data->fb_state = FB_OFF;
  9825. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9826. #endif
  9827. pt_data->quick_boot = false;
  9828. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9829. }
  9830. /*******************************************************************************
  9831. * FUNCTION: pt_core_resume
  9832. *
  9833. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9834. * along with kernel power state even the display is off based on the check of
  9835. * TTDL core platform flag.
  9836. *
  9837. * RETURN:
  9838. * 0 = success
  9839. * !0 = failure
  9840. *
  9841. * PARAMETERS:
  9842. * *dev - pointer to core device
  9843. ******************************************************************************/
  9844. static int pt_core_resume(struct device *dev)
  9845. {
  9846. struct pt_core_data *cd = dev_get_drvdata(dev);
  9847. int rc = 0;
  9848. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9849. return 0;
  9850. if (pm_suspend_via_firmware()) {
  9851. rc = pt_core_restore(cd->dev);
  9852. } else {
  9853. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9854. rc = pt_enable_i2c_regulator(cd, true);
  9855. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9856. }
  9857. mutex_lock(&cd->system_lock);
  9858. cd->wait_until_wake = 1;
  9859. mutex_unlock(&cd->system_lock);
  9860. wake_up(&cd->wait_q);
  9861. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9862. return rc;
  9863. }
  9864. #endif
  9865. #ifdef NEED_SUSPEND_NOTIFIER
  9866. /*******************************************************************************
  9867. * FUNCTION: pt_pm_notifier
  9868. *
  9869. * SUMMARY: This function is registered to notifier chain and will perform
  9870. * suspend operation if match event PM_SUSPEND_PREPARE.
  9871. *
  9872. * RETURN:
  9873. * 0 = success
  9874. * !0 = failure
  9875. *
  9876. * PARAMETERS:
  9877. * *nb - pointer to notifier_block structure
  9878. * action - notifier event type
  9879. * *data - void pointer
  9880. ******************************************************************************/
  9881. static int pt_pm_notifier(struct notifier_block *nb,
  9882. unsigned long action, void *data)
  9883. {
  9884. struct pt_core_data *cd = container_of(nb,
  9885. struct pt_core_data, pm_notifier);
  9886. if (action == PM_SUSPEND_PREPARE) {
  9887. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9888. __func__);
  9889. /*
  9890. * If PM runtime is not suspended, either call runtime
  9891. * PM suspend callback or wait until it finishes
  9892. */
  9893. if (!pm_runtime_suspended(cd->dev))
  9894. pm_runtime_suspend(cd->dev);
  9895. (void) pt_core_suspend(cd->dev);
  9896. }
  9897. return NOTIFY_DONE;
  9898. }
  9899. #endif
  9900. const struct dev_pm_ops pt_pm_ops = {
  9901. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9902. .freeze = pt_core_suspend,
  9903. .restore = pt_core_restore,
  9904. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9905. NULL)
  9906. };
  9907. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9908. /*******************************************************************************
  9909. * FUNCTION: _pt_request_pip2_enter_bl
  9910. *
  9911. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9912. * XRES pin or a soft reset.
  9913. *
  9914. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9915. * the WD active could cause this function to fail!
  9916. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9917. * will not try to determine the current mode but will proceed with
  9918. * resetting the DUT and entering the BL.
  9919. *
  9920. * NOTE: The definition of result code:
  9921. * PT_ENTER_BL_PASS (0)
  9922. * PT_ENTER_BL_ERROR (1)
  9923. * PT_ENTER_BL_RESET_FAIL (2)
  9924. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9925. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9926. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9927. *
  9928. * RETURNS:
  9929. * 0 = success
  9930. * !0 = failure
  9931. *
  9932. *
  9933. * PARAMETERS:
  9934. * *dev - pointer to device structure
  9935. * *start_mode - pointer to the mode the DUT was in when this function
  9936. * starts
  9937. * *result - pointer to store the result when to enter BL
  9938. ******************************************************************************/
  9939. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9940. {
  9941. int rc = 0;
  9942. int t;
  9943. int tmp_result = PT_ENTER_BL_ERROR;
  9944. int flash_info_retry = 2;
  9945. u8 mode = PT_MODE_UNKNOWN;
  9946. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9947. u8 read_buf[32];
  9948. u16 actual_read_len;
  9949. struct pt_core_data *cd = dev_get_drvdata(dev);
  9950. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9951. u8 time = 0;
  9952. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9953. if (cd->watchdog_enabled) {
  9954. pt_debug(dev, DL_WARN,
  9955. "%s: Watchdog must be stopped before entering BL\n",
  9956. __func__);
  9957. goto exit;
  9958. }
  9959. cancel_work_sync(&cd->enum_work);
  9960. cancel_work_sync(&cd->watchdog_work);
  9961. /* if undefined assume operational/test to bypass all checks */
  9962. if (*start_mode == PT_MODE_IGNORE) {
  9963. mode = PT_MODE_OPERATIONAL;
  9964. sys_mode = FW_SYS_MODE_TEST;
  9965. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9966. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9967. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9968. if (rc) {
  9969. pt_debug(dev, DL_ERROR,
  9970. "%s: Get mode failed, mode unknown\n",
  9971. __func__);
  9972. }
  9973. *start_mode = mode;
  9974. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9975. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9976. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9977. sys_mode = FW_SYS_MODE_SCANNING;
  9978. }
  9979. _retry:
  9980. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9981. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9982. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9983. switch (mode) {
  9984. case PT_MODE_UNKNOWN:
  9985. /*
  9986. * When the mode could not be determined the DUT could be
  9987. * in App mode running corrupted FW or FW that is not
  9988. * responding to the mode request, assume no communication
  9989. * and do a hard reset
  9990. */
  9991. mutex_lock(&cd->system_lock);
  9992. cd->startup_status = STARTUP_STATUS_START;
  9993. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9994. mutex_unlock(&cd->system_lock);
  9995. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9996. if (rc) {
  9997. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9998. goto exit;
  9999. }
  10000. break;
  10001. case PT_MODE_OPERATIONAL:
  10002. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10003. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10004. __func__);
  10005. rc = pt_pip_suspend_scanning_(cd);
  10006. if (rc) {
  10007. /*
  10008. * Print to log but don't exit, the FW could be
  10009. * running but be hung or fail to respond to
  10010. * this request
  10011. */
  10012. pt_debug(dev, DL_ERROR,
  10013. "%s Suspend Scan Failed\n", __func__);
  10014. }
  10015. /* sleep to allow the suspend scan to be processed */
  10016. usleep_range(1000, 2000);
  10017. }
  10018. mutex_lock(&cd->system_lock);
  10019. cd->startup_status = STARTUP_STATUS_START;
  10020. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10021. mutex_unlock(&cd->system_lock);
  10022. /* Reset device to enter the BL */
  10023. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10024. if (rc) {
  10025. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10026. goto exit;
  10027. }
  10028. break;
  10029. case PT_MODE_BOOTLOADER:
  10030. /* Do nothing as we are already in the BL */
  10031. tmp_result = PT_ENTER_BL_PASS;
  10032. goto exit;
  10033. default:
  10034. /* Should NEVER get here */
  10035. tmp_result = PT_ENTER_BL_ERROR;
  10036. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10037. goto exit;
  10038. }
  10039. if (!cd->flashless_dut &&
  10040. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10041. /*
  10042. * Sending the special "Host Mode" command will instruct the
  10043. * BL to not execute the FW it has loaded into RAM.
  10044. * The command must be sent within a 40ms window from releasing
  10045. * the XRES pin. If the messages is sent too early it will NAK,
  10046. * so keep sending it every 2ms until it is accepted by the BL.
  10047. * A no-flash DUT does not require this command as there is no
  10048. * FW for the BL to load and execute.
  10049. */
  10050. usleep_range(4000, 6000);
  10051. pt_debug(cd->dev, DL_INFO,
  10052. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10053. __func__, (int)sizeof(host_mode_cmd));
  10054. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10055. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10056. rc = 1;
  10057. while (rc && time < 34) {
  10058. rc = pt_adap_write_read_specific(cd, 4,
  10059. host_mode_cmd, NULL);
  10060. usleep_range(1800, 2000);
  10061. time += 2;
  10062. }
  10063. /* Sleep to allow the BL to come up */
  10064. usleep_range(1000, 2000);
  10065. }
  10066. /*
  10067. * To avoid the case that next PIP command can be confused by BL/FW
  10068. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10069. * followed by "wakeup event" function can lower the failure rate.
  10070. */
  10071. t = wait_event_timeout(cd->wait_q,
  10072. ((cd->startup_status != STARTUP_STATUS_START)
  10073. && (cd->hid_reset_cmd_state == 0)),
  10074. msecs_to_jiffies(300));
  10075. if (IS_TMO(t)) {
  10076. pt_debug(cd->dev, DL_ERROR,
  10077. "%s: TMO waiting for BL sentinel\n", __func__);
  10078. }
  10079. /* Check if device is now in BL mode */
  10080. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10081. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10082. cd->startup_status);
  10083. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10084. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10085. mutex_lock(&cd->system_lock);
  10086. cd->pip2_prot_active = true;
  10087. cd->mode = PT_MODE_BOOTLOADER;
  10088. mutex_unlock(&cd->system_lock);
  10089. tmp_result = PT_ENTER_BL_PASS;
  10090. } else {
  10091. /*
  10092. * If the device doesn't enter BL mode as expected and rc is
  10093. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10094. * the function should return an error code to indicate this
  10095. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10096. */
  10097. if (!rc)
  10098. rc = -EINVAL;
  10099. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10100. mutex_lock(&cd->system_lock);
  10101. cd->mode = mode;
  10102. mutex_unlock(&cd->system_lock);
  10103. pt_debug(dev, DL_ERROR,
  10104. "%s ERROR: Not in BL as expected", __func__);
  10105. }
  10106. exit:
  10107. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10108. /* Check to get (buffered) flash information */
  10109. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10110. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10111. read_buf, &actual_read_len);
  10112. if (!rc) {
  10113. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10114. pt_debug(
  10115. dev, DL_WARN,
  10116. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10117. __func__,
  10118. read_buf[PIP2_RESP_BODY_OFFSET]);
  10119. /*
  10120. * If the BL was unable to cache the correct
  10121. * values when entering the first time due to
  10122. * the Flash part not having been powered up
  10123. * long enough, re-enter the BL to trigger the
  10124. * BL to re-attempt to cache the values.
  10125. */
  10126. if (flash_info_retry-- > 0) {
  10127. mode = PT_MODE_UNKNOWN;
  10128. pt_debug(dev, DL_WARN,
  10129. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10130. __func__, flash_info_retry);
  10131. goto _retry;
  10132. } else {
  10133. pt_debug(dev, DL_WARN,
  10134. "%s Manufacturer ID Unknown\n",
  10135. __func__);
  10136. tmp_result = PT_ENTER_BL_PASS;
  10137. }
  10138. }
  10139. } else {
  10140. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10141. pt_debug(
  10142. dev, DL_ERROR,
  10143. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10144. __func__);
  10145. }
  10146. }
  10147. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10148. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10149. "SUPPRESS");
  10150. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10151. if (result)
  10152. *result = tmp_result;
  10153. return rc;
  10154. }
  10155. /*******************************************************************************
  10156. * FUNCTION: _pt_pip2_file_open
  10157. *
  10158. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10159. *
  10160. * NOTE: The DUT must be in BL mode for this command to work
  10161. *
  10162. * RETURNS:
  10163. * <0 = Error
  10164. * >0 = file handle opened
  10165. *
  10166. * PARAMETERS:
  10167. * *dev - pointer to device structure
  10168. * file_no - PIP2 file number to open
  10169. ******************************************************************************/
  10170. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10171. {
  10172. int ret = 0;
  10173. u16 status;
  10174. u16 actual_read_len;
  10175. u8 file_handle;
  10176. u8 data[2];
  10177. u8 read_buf[10];
  10178. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10179. data[0] = file_no;
  10180. ret = _pt_request_pip2_send_cmd(dev,
  10181. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10182. data, 1, read_buf, &actual_read_len);
  10183. if (ret) {
  10184. pt_debug(dev, DL_ERROR,
  10185. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10186. __func__, file_no);
  10187. return -PIP2_RSP_ERR_NOT_OPEN;
  10188. }
  10189. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10190. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10191. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10192. (file_handle != file_no)) {
  10193. pt_debug(dev, DL_ERROR,
  10194. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10195. __func__, status, file_handle);
  10196. return -status;
  10197. }
  10198. return file_handle;
  10199. }
  10200. /*******************************************************************************
  10201. * FUNCTION: _pt_pip2_file_close
  10202. *
  10203. * SUMMARY: Using the BL PIP2 commands close a file
  10204. *
  10205. * NOTE: The DUT must be in BL mode for this command to work
  10206. *
  10207. * RETURNS:
  10208. * <0 = Error
  10209. * >0 = file handle closed
  10210. *
  10211. * PARAMETERS:
  10212. * *dev - pointer to device structure
  10213. * file_handle - handle to the file to be closed
  10214. ******************************************************************************/
  10215. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10216. {
  10217. int ret = 0;
  10218. u16 status;
  10219. u16 actual_read_len;
  10220. u8 data[2];
  10221. u8 read_buf[32];
  10222. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10223. data[0] = file_handle;
  10224. ret = _pt_request_pip2_send_cmd(dev,
  10225. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10226. data, 1, read_buf, &actual_read_len);
  10227. if (ret) {
  10228. pt_debug(dev, DL_ERROR,
  10229. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10230. __func__, file_handle);
  10231. return -ETIME;
  10232. }
  10233. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10234. if (status != 0x00) {
  10235. pt_debug(dev, DL_ERROR,
  10236. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10237. __func__, status, file_handle);
  10238. return -status;
  10239. }
  10240. return file_handle;
  10241. }
  10242. /*******************************************************************************
  10243. * FUNCTION: _pt_pip2_file_erase
  10244. *
  10245. * SUMMARY: Using the BL PIP2 commands erase a file
  10246. *
  10247. * NOTE: The DUT must be in BL mode for this command to work
  10248. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10249. * one retry is attempted for each sector in a file.
  10250. *
  10251. * RETURNS:
  10252. * <0 = Error
  10253. * >0 = file handle closed
  10254. *
  10255. * PARAMETERS:
  10256. * *dev - pointer to device structure
  10257. * file_handle - handle to the file to be erased
  10258. * *status - PIP2 erase status code
  10259. ******************************************************************************/
  10260. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10261. {
  10262. int ret = 0;
  10263. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10264. int retry = 1;
  10265. u16 actual_read_len;
  10266. u8 data[2];
  10267. u8 read_buf[10];
  10268. struct pt_core_data *cd = dev_get_drvdata(dev);
  10269. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10270. data[0] = file_handle;
  10271. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10272. *status = PIP2_RSP_ERR_TIMEOUT;
  10273. /* Increase waiting time for large file erase */
  10274. mutex_lock(&cd->system_lock);
  10275. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10276. mutex_unlock(&cd->system_lock);
  10277. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10278. ret = _pt_request_pip2_send_cmd(dev,
  10279. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10280. data, 2, read_buf, &actual_read_len);
  10281. if (ret)
  10282. break;
  10283. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10284. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10285. #ifdef TTDL_DIAGNOSTICS
  10286. cd->file_erase_timeout_count++;
  10287. #endif
  10288. pt_debug(dev, DL_WARN,
  10289. "%s: ERASE timeout %d for file = %d\n",
  10290. __func__, retry, file_handle);
  10291. }
  10292. retry++;
  10293. }
  10294. mutex_lock(&cd->system_lock);
  10295. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10296. mutex_unlock(&cd->system_lock);
  10297. if (ret) {
  10298. pt_debug(dev, DL_ERROR,
  10299. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10300. __func__, ret, file_handle);
  10301. return -EIO;
  10302. }
  10303. if (*status != 0x00) {
  10304. pt_debug(dev, DL_ERROR,
  10305. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10306. __func__, *status, file_handle);
  10307. return -EIO;
  10308. }
  10309. return file_handle;
  10310. }
  10311. /*******************************************************************************
  10312. * FUNCTION: _pt_pip2_file_read
  10313. *
  10314. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10315. *
  10316. * NOTE: The DUT must be in BL mode for this command to work
  10317. *
  10318. * RETURNS:
  10319. * <0 = Error
  10320. * >0 = number of bytes read
  10321. *
  10322. * PARAMETERS:
  10323. * *dev - pointer to device structure
  10324. * file_handle - File handle to read from
  10325. * num_bytes - number of bytes to read
  10326. ******************************************************************************/
  10327. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10328. u8 *read_buf)
  10329. {
  10330. int ret = 0;
  10331. u16 status;
  10332. u16 actual_read_len;
  10333. u8 data[3];
  10334. data[0] = file_handle;
  10335. data[1] = (num_bytes & 0x00FF);
  10336. data[2] = (num_bytes & 0xFF00) >> 8;
  10337. ret = _pt_request_pip2_send_cmd(dev,
  10338. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10339. data, 3, read_buf, &actual_read_len);
  10340. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10341. if (ret || ((status != 0x00) && (status != 0x03))) {
  10342. pt_debug(dev, DL_ERROR,
  10343. "%s File open failure with error code = %d\n",
  10344. __func__, status);
  10345. return -EPERM;
  10346. }
  10347. ret = num_bytes;
  10348. return ret;
  10349. }
  10350. /*******************************************************************************
  10351. * FUNCTION: _pt_read_us_file
  10352. *
  10353. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10354. * then read the entire file.
  10355. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10356. *
  10357. * RETURN:
  10358. * 0 = success
  10359. * !0 = failure
  10360. *
  10361. * PARAMETERS:
  10362. * *file_path - pointer to the file path
  10363. * *buf - pointer to the buffer to store the file contents
  10364. * *size - pointer to the size of the file
  10365. ******************************************************************************/
  10366. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10367. {
  10368. struct file *filp = NULL;
  10369. struct inode *inode = NULL;
  10370. unsigned int file_len = 0;
  10371. unsigned int read_len = 0;
  10372. mm_segment_t oldfs;
  10373. int rc = 0;
  10374. if (file_path == NULL || buf == NULL) {
  10375. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10376. return -EINVAL;
  10377. }
  10378. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10379. oldfs = force_uaccess_begin();
  10380. filp = filp_open_block(file_path, O_RDONLY, 0400);
  10381. if (IS_ERR(filp)) {
  10382. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10383. file_path);
  10384. rc = -ENOENT;
  10385. goto err;
  10386. }
  10387. if (filp->f_op == NULL) {
  10388. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10389. __func__);
  10390. rc = -EINVAL;
  10391. goto exit;
  10392. }
  10393. inode = filp->f_path.dentry->d_inode;
  10394. if (inode == NULL) {
  10395. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10396. __func__);
  10397. rc = -EINVAL;
  10398. goto exit;
  10399. }
  10400. file_len = i_size_read(inode->i_mapping->host);
  10401. if (file_len == 0) {
  10402. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10403. __func__, file_len);
  10404. rc = -EINVAL;
  10405. goto exit;
  10406. }
  10407. if (*size == 0)
  10408. read_len = file_len;
  10409. else
  10410. read_len = *size;
  10411. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10412. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10413. __func__, read_len);
  10414. rc = -EINVAL;
  10415. goto exit;
  10416. }
  10417. filp->private_data = inode->i_private;
  10418. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  10419. if (filp->f_op->read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10420. #else
  10421. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10422. #endif
  10423. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10424. rc = -EINVAL;
  10425. goto exit;
  10426. }
  10427. *size = read_len;
  10428. exit:
  10429. if (filp_close(filp, NULL) != 0)
  10430. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10431. err:
  10432. force_uaccess_end(oldfs);
  10433. return rc;
  10434. }
  10435. /*******************************************************************************
  10436. * FUNCTION: _pt_request_pip2_bin_hdr
  10437. *
  10438. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10439. * in the case of a flashless DUT, and parse the contents
  10440. *
  10441. * RETURNS:
  10442. * 0 = Success
  10443. * !0 = Error condition
  10444. *
  10445. * PARAMETERS:
  10446. * *dev - pointer to device structure
  10447. ******************************************************************************/
  10448. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10449. {
  10450. struct pt_core_data *cd = dev_get_drvdata(dev);
  10451. u8 file_handle;
  10452. u8 read_buf[255];
  10453. u8 hdr_len = 0;
  10454. u8 i;
  10455. int bytes_read;
  10456. int read_size;
  10457. int ret = 0;
  10458. int rc = 0;
  10459. bool load_hdr_struct = false;
  10460. if (cd->flashless_dut) {
  10461. read_size = sizeof(read_buf);
  10462. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10463. read_buf, &read_size);
  10464. if (rc) {
  10465. ret = rc;
  10466. pt_debug(dev, DL_ERROR,
  10467. "%s Failed to read fw image from US, rc=%d\n",
  10468. __func__, rc);
  10469. goto exit;
  10470. }
  10471. load_hdr_struct = true;
  10472. hdr_len = read_buf[0];
  10473. i = 0;
  10474. } else {
  10475. if (cd->mode != PT_MODE_BOOTLOADER) {
  10476. ret = -EPERM;
  10477. goto exit;
  10478. }
  10479. /* Open the bin file in Flash */
  10480. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10481. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10482. if (file_handle != PIP2_FW_FILE) {
  10483. ret = -ENOENT;
  10484. pt_debug(dev, DL_ERROR,
  10485. "%s Failed to open bin file\n", __func__);
  10486. goto exit;
  10487. }
  10488. /* Read the header length from the file */
  10489. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10490. read_size = 1;
  10491. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10492. read_buf);
  10493. if (bytes_read != read_size) {
  10494. ret = -EX_ERR_FREAD;
  10495. pt_debug(dev, DL_ERROR,
  10496. "%s Failed to bin file header len\n", __func__);
  10497. goto exit_close_file;
  10498. }
  10499. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10500. if (hdr_len == 0xFF) {
  10501. ret = -EX_ERR_FLEN;
  10502. pt_debug(dev, DL_ERROR,
  10503. "%s Bin header len is invalid\n", __func__);
  10504. goto exit_close_file;
  10505. }
  10506. /* Read the rest of the header from the bin file */
  10507. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10508. memset(read_buf, 0, sizeof(read_buf));
  10509. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10510. read_buf);
  10511. if (bytes_read != hdr_len) {
  10512. ret = -EX_ERR_FREAD;
  10513. pt_debug(dev, DL_ERROR,
  10514. "%s Failed to read bin file\n", __func__);
  10515. goto exit_close_file;
  10516. }
  10517. load_hdr_struct = true;
  10518. exit_close_file:
  10519. /* Close the file */
  10520. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10521. ret = -EX_ERR_FCLOSE;
  10522. pt_debug(dev, DL_ERROR,
  10523. "%s Failed to close bin file\n", __func__);
  10524. }
  10525. /*
  10526. * The length was already read so subtract 1 to make the rest of
  10527. * the offsets match the spec
  10528. */
  10529. i = PIP2_RESP_BODY_OFFSET - 1;
  10530. }
  10531. if (load_hdr_struct) {
  10532. hdr->length = hdr_len;
  10533. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10534. hdr->fw_major = (read_buf[i+3]);
  10535. hdr->fw_minor = (read_buf[i+4]);
  10536. hdr->fw_crc = (read_buf[i+5] << 24) |
  10537. (read_buf[i+6] << 16) |
  10538. (read_buf[i+7] << 8) |
  10539. (read_buf[i+8]);
  10540. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10541. (read_buf[i+10] << 16) |
  10542. (read_buf[i+11] << 8) |
  10543. (read_buf[i+12]);
  10544. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10545. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10546. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10547. if (hdr_len >= 22) {
  10548. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10549. (read_buf[i+20] << 16) |
  10550. (read_buf[i+21] << 8) |
  10551. (read_buf[i+22]);
  10552. } else {
  10553. hdr->hex_file_size = 0;
  10554. }
  10555. }
  10556. exit:
  10557. return ret;
  10558. }
  10559. /*******************************************************************************
  10560. * FUNCTION: _pt_pip2_file_get_stats
  10561. *
  10562. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10563. * opened file
  10564. *
  10565. * NOTE: The DUT must be in BL mode for this command to work
  10566. *
  10567. * RETURNS:
  10568. * !0 = Error
  10569. * 0 = Success
  10570. *
  10571. * PARAMETERS:
  10572. * *dev - pointer to device structure
  10573. * file_handle - File handle to read from
  10574. * *address - pointer to store address of file
  10575. * *file_size _ pointer to store size of file
  10576. ******************************************************************************/
  10577. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10578. u32 *file_size)
  10579. {
  10580. int ret = 1;
  10581. u16 status;
  10582. u16 actual_read_len;
  10583. u8 data[2];
  10584. u8 read_buf[16];
  10585. data[0] = file_handle;
  10586. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10587. ret = _pt_request_pip2_send_cmd(dev,
  10588. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10589. data, 2, read_buf, &actual_read_len);
  10590. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10591. if (ret || (status != 0x00)) {
  10592. pt_debug(dev, DL_ERROR,
  10593. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10594. __func__, status, file_handle, ret);
  10595. ret = -EIO;
  10596. goto exit;
  10597. }
  10598. pt_debug(dev, DL_DEBUG,
  10599. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10600. if (address) {
  10601. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10602. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10603. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10604. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10605. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10606. __func__, *address);
  10607. }
  10608. if (file_size) {
  10609. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10610. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10611. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10612. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10613. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10614. __func__, *file_size);
  10615. }
  10616. exit:
  10617. return ret;
  10618. }
  10619. /*******************************************************************************
  10620. * FUNCTION: _pt_pip2_file_seek_offset
  10621. *
  10622. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10623. * opened file
  10624. *
  10625. * NOTE: The DUT must be in BL mode for this command to work
  10626. * NOTE: File open/erase command can reset the offset
  10627. *
  10628. * RETURNS:
  10629. * !0 = Error
  10630. * 0 = Success
  10631. *
  10632. * PARAMETERS:
  10633. * *dev - pointer to device structure
  10634. * file_handle - File handle to read from
  10635. * read_offset - read offset of file
  10636. * write_offset - write offset of file
  10637. ******************************************************************************/
  10638. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10639. u32 read_offset, u32 write_offset)
  10640. {
  10641. int ret = 1;
  10642. u16 status;
  10643. u16 actual_read_len;
  10644. u8 data[10];
  10645. u8 read_buf[16];
  10646. data[0] = file_handle;
  10647. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10648. data[2] = 0xff & read_offset;
  10649. data[3] = 0xff & (read_offset >> 8);
  10650. data[4] = 0xff & (read_offset >> 16);
  10651. data[5] = 0xff & (read_offset >> 24);
  10652. data[6] = 0xff & write_offset;
  10653. data[7] = 0xff & (write_offset >> 8);
  10654. data[8] = 0xff & (write_offset >> 16);
  10655. data[9] = 0xff & (write_offset >> 24);
  10656. ret = _pt_request_pip2_send_cmd(dev,
  10657. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10658. data, 10, read_buf, &actual_read_len);
  10659. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10660. if (ret || (status != 0x00)) {
  10661. pt_debug(dev, DL_ERROR,
  10662. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10663. __func__, status, ret, file_handle);
  10664. ret = -EIO;
  10665. }
  10666. return ret;
  10667. }
  10668. /*******************************************************************************
  10669. * FUNCTION: _pt_pip2_file_crc
  10670. *
  10671. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10672. * the file.
  10673. *
  10674. * NOTE: The DUT must be in BL mode for this command to work
  10675. * NOTE: This command only can be used for BL version 1.8 or greater.
  10676. * BL version 1.8 added this change according to PGV-173.
  10677. *
  10678. * RETURNS:
  10679. * !0 = Error
  10680. * 0 = Success
  10681. *
  10682. * PARAMETERS:
  10683. * *dev - pointer to device structure
  10684. * file_handle - File handle to read from
  10685. * offset - start offset for CRC calculation
  10686. * length - number of bytes to calculate CRC over
  10687. * read_buf - pointer to the read buffer
  10688. ******************************************************************************/
  10689. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10690. u32 offset, u32 length, u8 *read_buf)
  10691. {
  10692. int rc = 1;
  10693. u16 actual_read_len;
  10694. u8 data[10];
  10695. data[0] = file_handle;
  10696. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10697. data[2] = 0xff & offset;
  10698. data[3] = 0xff & (offset >> 8);
  10699. data[4] = 0xff & (offset >> 16);
  10700. data[5] = 0xff & (offset >> 24);
  10701. data[6] = 0xff & length;
  10702. data[7] = 0xff & (length >> 8);
  10703. data[8] = 0xff & (length >> 16);
  10704. data[9] = 0xff & (length >> 24);
  10705. rc = _pt_request_pip2_send_cmd(dev,
  10706. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10707. data, 10, read_buf, &actual_read_len);
  10708. if (rc)
  10709. pt_debug(dev, DL_ERROR,
  10710. "%s Return FILE_CRC failure, rc = %d\n",
  10711. __func__, rc);
  10712. return rc;
  10713. }
  10714. /*******************************************************************************
  10715. * FUNCTION: pt_pip2_ping_test
  10716. *
  10717. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10718. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10719. * response payload matches what was sent.
  10720. * The max payload size is 247:
  10721. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10722. *
  10723. * RETURN:
  10724. * 0 = success
  10725. * !0 = failure
  10726. *
  10727. * PARAMETERS:
  10728. * *dev - pointer to device structure
  10729. * *attr - pointer to device attributes
  10730. * *buf - pointer to output buffer
  10731. ******************************************************************************/
  10732. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10733. {
  10734. u16 actual_read_len;
  10735. u8 *read_buf = NULL;
  10736. u8 *data = NULL;
  10737. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10738. int i = 1;
  10739. int j = 0;
  10740. int rc = 0;
  10741. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10742. if (!read_buf)
  10743. goto ping_test_exit;
  10744. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10745. if (!data)
  10746. goto ping_test_exit;
  10747. /* Load data payload with an array of walking 1's */
  10748. for (i = 0; i < 255; i++)
  10749. data[i] = 0x01 << (i % 8);
  10750. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10751. for (i = 0; i <= max_bytes; i++) {
  10752. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10753. PIP2_CMD_ID_PING, data, i, read_buf,
  10754. &actual_read_len);
  10755. if (rc) {
  10756. pt_debug(dev, DL_ERROR,
  10757. "%s: PING failed with %d byte payload\n",
  10758. __func__, i);
  10759. break;
  10760. }
  10761. /* Verify data returned matches data sent */
  10762. for (j = 0; j < i; j++) {
  10763. if (read_buf[data_offset + j] != data[j]) {
  10764. pt_debug(dev, DL_DEBUG,
  10765. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10766. __func__, i, j, data[j], j,
  10767. read_buf[data_offset + j]);
  10768. goto ping_test_exit;
  10769. }
  10770. }
  10771. }
  10772. ping_test_exit:
  10773. *last_packet_size = i - 1;
  10774. kfree(read_buf);
  10775. kfree(data);
  10776. return rc;
  10777. }
  10778. /*******************************************************************************
  10779. * FUNCTION: _pt_ic_parse_input_hex
  10780. *
  10781. * SUMMARY: Parse a char data array as space delimited hex values into
  10782. * an int array.
  10783. *
  10784. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10785. * buffer to store data is "u32". This function is still needed by the
  10786. * "command" sysfs node because the buffer type to store data is "u8".
  10787. *
  10788. * RETURN: Length of parsed data
  10789. *
  10790. * PARAMETERS:
  10791. * *dev - pointer to device structure
  10792. * *buf - pointer to buffer that holds the input array to parse
  10793. * buf_size - size of buf
  10794. * *ic_buf - pointer to array to store parsed data
  10795. * ic_buf_size - max size of ic_buf
  10796. ******************************************************************************/
  10797. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10798. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10799. {
  10800. const char *pbuf = buf;
  10801. unsigned long value;
  10802. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10803. u32 i = 0;
  10804. u32 j;
  10805. int last = 0;
  10806. int ret;
  10807. pt_debug(dev, DL_DEBUG,
  10808. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10809. __func__, pbuf, buf, buf_size, "scan buf size",
  10810. PT_MAX_PIP2_MSG_SIZE, buf);
  10811. while (pbuf <= (buf + buf_size)) {
  10812. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10813. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10814. __func__, "Max cmd size exceeded", i,
  10815. PT_MAX_PIP2_MSG_SIZE);
  10816. return -EINVAL;
  10817. }
  10818. if (i >= ic_buf_size) {
  10819. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10820. __func__, "Buffer size exceeded", i,
  10821. ic_buf_size);
  10822. return -EINVAL;
  10823. }
  10824. while (((*pbuf == ' ') || (*pbuf == ','))
  10825. && (pbuf < (buf + buf_size))) {
  10826. last = *pbuf;
  10827. pbuf++;
  10828. }
  10829. if (pbuf >= (buf + buf_size))
  10830. break;
  10831. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10832. if ((last == ',') && (*pbuf == ',')) {
  10833. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10834. __func__, "Invalid data format.");
  10835. return -EINVAL;
  10836. }
  10837. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10838. && (pbuf < (buf + buf_size))
  10839. && (*pbuf != ' ')
  10840. && (*pbuf != ','); j++) {
  10841. last = *pbuf;
  10842. scan_buf[j] = *pbuf++;
  10843. }
  10844. ret = kstrtoul(scan_buf, 16, &value);
  10845. if (ret < 0) {
  10846. pt_debug(dev, DL_ERROR,
  10847. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10848. "Invalid data format. ", scan_buf,
  10849. "Use \"0xHH,...,0xHH\"", " instead.",
  10850. i, ret);
  10851. return ret;
  10852. }
  10853. ic_buf[i] = value;
  10854. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10855. __func__, i, value);
  10856. i++;
  10857. }
  10858. return i;
  10859. }
  10860. /*******************************************************************************
  10861. * FUNCTION: _pt_ic_parse_input
  10862. *
  10863. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10864. * hex values or dec values into an int array with the following rules:
  10865. * 1) Hex values must have a "0x" prefix for each element or the first element
  10866. * only
  10867. * 2) Dec values do not have any prefix
  10868. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10869. * string
  10870. *
  10871. * RETURN: Number of values parsed
  10872. *
  10873. * PARAMETERS:
  10874. * *dev - pointer to device structure
  10875. * *buf - pointer to buffer that holds the input array to parse
  10876. * buf_size - size of buf
  10877. * *out_buf - pointer to array to store parsed data
  10878. * out_buf_size - max size of buffer to be stored
  10879. ******************************************************************************/
  10880. static int _pt_ic_parse_input(struct device *dev,
  10881. const char *buf, size_t buf_size,
  10882. u32 *out_buf, size_t out_buf_size)
  10883. {
  10884. const char *pbuf = buf;
  10885. unsigned long value;
  10886. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10887. u32 i = 0;
  10888. u32 j;
  10889. int last = 0;
  10890. int ret = 0;
  10891. u8 str_base = 0;
  10892. pt_debug(dev, DL_DEBUG,
  10893. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10894. __func__, buf_size, out_buf_size, "scan buf size",
  10895. PT_MAX_PIP2_MSG_SIZE, buf);
  10896. while (pbuf <= (buf + buf_size)) {
  10897. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10898. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10899. __func__, "Max cmd size exceeded", i,
  10900. PT_MAX_PIP2_MSG_SIZE);
  10901. ret = -EINVAL;
  10902. goto error;
  10903. }
  10904. if (i >= out_buf_size) {
  10905. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10906. __func__, "Buffer size exceeded", i,
  10907. out_buf_size);
  10908. ret = -EINVAL;
  10909. goto error;
  10910. }
  10911. while (((*pbuf == ' ') || (*pbuf == ','))
  10912. && (pbuf < (buf + buf_size))) {
  10913. last = *pbuf;
  10914. pbuf++;
  10915. }
  10916. if (pbuf >= (buf + buf_size))
  10917. break;
  10918. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10919. if ((last == ',') && (*pbuf == ',')) {
  10920. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10921. __func__, "Invalid data format.");
  10922. ret = -EINVAL;
  10923. goto error;
  10924. }
  10925. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10926. && (pbuf < (buf + buf_size))
  10927. && (*pbuf != ' ')
  10928. && (*pbuf != ','); j++) {
  10929. last = *pbuf;
  10930. scan_buf[j] = *pbuf++;
  10931. }
  10932. if (i == 0) {
  10933. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10934. (strncmp(scan_buf, "0X", 2) == 0))
  10935. str_base = 16;
  10936. else
  10937. str_base = 10;
  10938. } else {
  10939. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10940. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10941. (str_base == 10)) {
  10942. pt_debug(dev, DL_ERROR,
  10943. "%s: Decimal and Heximal data mixed\n",
  10944. __func__);
  10945. ret = -EINVAL;
  10946. goto error;
  10947. }
  10948. }
  10949. ret = kstrtoul(scan_buf, str_base, &value);
  10950. if (ret < 0) {
  10951. pt_debug(dev, DL_ERROR,
  10952. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10953. "Invalid data format. ", scan_buf,
  10954. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10955. " instead.", i, ret);
  10956. goto error;
  10957. }
  10958. out_buf[i] = value;
  10959. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10960. __func__, i, value, value);
  10961. i++;
  10962. }
  10963. ret = i;
  10964. error:
  10965. return ret;
  10966. }
  10967. #ifdef TTHE_TUNER_SUPPORT
  10968. /*******************************************************************************
  10969. * FUNCTION: tthe_debugfs_open
  10970. *
  10971. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10972. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10973. * printing a large number of fingers and sensor data without overflowing
  10974. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10975. * even multiple of PAGE_SIZE
  10976. *
  10977. * RETURN:
  10978. * 0 = success
  10979. * !0 = failure
  10980. *
  10981. * PARAMETERS:
  10982. * *inode - file inode number
  10983. * *filp - file pointer to debugfs file
  10984. ******************************************************************************/
  10985. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10986. {
  10987. struct pt_core_data *cd = inode->i_private;
  10988. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10989. filp->private_data = inode->i_private;
  10990. if (cd->tthe_buf)
  10991. return -EBUSY;
  10992. while (buf_size < 4096)
  10993. buf_size = buf_size << 1;
  10994. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10995. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10996. cd->tthe_buf_size = buf_size;
  10997. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10998. if (!cd->tthe_buf)
  10999. return -ENOMEM;
  11000. return 0;
  11001. }
  11002. /*******************************************************************************
  11003. * FUNCTION: tthe_debugfs_close
  11004. *
  11005. * SUMMARY: Close method for tthe_tuner debugfs node.
  11006. *
  11007. * RETURN:
  11008. * 0 = success
  11009. * !0 = failure
  11010. *
  11011. * PARAMETERS:
  11012. * *inode - file inode number
  11013. * *filp - file pointer to debugfs file
  11014. ******************************************************************************/
  11015. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11016. {
  11017. struct pt_core_data *cd = filp->private_data;
  11018. filp->private_data = NULL;
  11019. kfree(cd->tthe_buf);
  11020. cd->tthe_buf = NULL;
  11021. return 0;
  11022. }
  11023. /*******************************************************************************
  11024. * FUNCTION: tthe_debugfs_read
  11025. *
  11026. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11027. * tthe_buf to user buffer.
  11028. *
  11029. * RETURN: Size of debugfs data print
  11030. *
  11031. * PARAMETERS:
  11032. * *filp - file pointer to debugfs file
  11033. * *buf - the user space buffer to read to
  11034. * count - the maximum number of bytes to read
  11035. * *ppos - the current position in the buffer
  11036. ******************************************************************************/
  11037. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11038. size_t count, loff_t *ppos)
  11039. {
  11040. struct pt_core_data *cd = filp->private_data;
  11041. int size;
  11042. int ret;
  11043. static int partial_read;
  11044. wait_event_interruptible(cd->wait_q,
  11045. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11046. mutex_lock(&cd->tthe_lock);
  11047. if (cd->tthe_exit) {
  11048. mutex_unlock(&cd->tthe_lock);
  11049. return 0;
  11050. }
  11051. if (count > cd->tthe_buf_len)
  11052. size = cd->tthe_buf_len;
  11053. else
  11054. size = count;
  11055. if (!size) {
  11056. mutex_unlock(&cd->tthe_lock);
  11057. return 0;
  11058. }
  11059. if (partial_read) {
  11060. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11061. partial_read = 0;
  11062. } else {
  11063. ret = copy_to_user(buf, cd->tthe_buf, size);
  11064. }
  11065. if (size == count)
  11066. partial_read = count;
  11067. if (ret == size)
  11068. return -EFAULT;
  11069. size -= ret;
  11070. cd->tthe_buf_len -= size;
  11071. mutex_unlock(&cd->tthe_lock);
  11072. *ppos += size;
  11073. return size;
  11074. }
  11075. static const struct file_operations tthe_debugfs_fops = {
  11076. .open = tthe_debugfs_open,
  11077. .release = tthe_debugfs_close,
  11078. .read = tthe_debugfs_read,
  11079. };
  11080. #endif
  11081. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11082. .start_bl = _pt_request_pip_start_bl,
  11083. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11084. .resume_scanning = _pt_request_pip_resume_scanning,
  11085. .get_param = _pt_request_pip_get_param,
  11086. .set_param = _pt_request_pip_set_param,
  11087. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11088. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11089. .get_data_structure = _pt_request_pip_get_data_structure,
  11090. .run_selftest = _pt_request_pip_run_selftest,
  11091. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11092. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11093. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11094. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11095. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11096. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11097. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11098. .read_data_block = _pt_request_pip_read_data_block,
  11099. .write_data_block = _pt_request_pip_write_data_block,
  11100. .user_cmd = _pt_request_pip_user_cmd,
  11101. .get_bl_info = _pt_request_pip_bl_get_information,
  11102. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11103. .launch_app = _pt_request_pip_launch_app,
  11104. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11105. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11106. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11107. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11108. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11109. .pip2_file_open = _pt_pip2_file_open,
  11110. .pip2_file_close = _pt_pip2_file_close,
  11111. .pip2_file_erase = _pt_pip2_file_erase,
  11112. .read_us_file = _pt_read_us_file,
  11113. .manage_cal_data = _pt_manage_local_cal_data,
  11114. .calc_crc = crc_ccitt_calculate,
  11115. #ifdef TTDL_DIAGNOSTICS
  11116. .pip2_file_read = _pt_pip2_file_read,
  11117. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11118. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11119. .pip2_file_crc = _pt_pip2_file_crc,
  11120. #endif
  11121. };
  11122. static struct pt_core_commands _pt_core_commands = {
  11123. .subscribe_attention = _pt_subscribe_attention,
  11124. .unsubscribe_attention = _pt_unsubscribe_attention,
  11125. .request_exclusive = _pt_request_exclusive,
  11126. .release_exclusive = _pt_release_exclusive,
  11127. .request_reset = _pt_request_reset,
  11128. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11129. .request_enum = _pt_request_enum,
  11130. .request_sysinfo = _pt_request_sysinfo,
  11131. .request_loader_pdata = _pt_request_loader_pdata,
  11132. .request_stop_wd = _pt_request_stop_wd,
  11133. .request_start_wd = _pt_request_start_wd,
  11134. .request_get_mode = _pt_request_get_mode,
  11135. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11136. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11137. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11138. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11139. .request_dut_generation = _pt_request_dut_generation,
  11140. .request_hw_version = _pt_request_hw_version,
  11141. .parse_sysfs_input = _pt_ic_parse_input,
  11142. #ifdef TTHE_TUNER_SUPPORT
  11143. .request_tthe_print = _pt_request_tthe_print,
  11144. #endif
  11145. #ifdef TTDL_DIAGNOSTICS
  11146. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11147. #endif
  11148. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11149. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11150. };
  11151. struct pt_core_commands *pt_get_commands(void)
  11152. {
  11153. return &_pt_core_commands;
  11154. }
  11155. EXPORT_SYMBOL_GPL(pt_get_commands);
  11156. static DEFINE_MUTEX(core_list_lock);
  11157. static LIST_HEAD(core_list);
  11158. static DEFINE_MUTEX(module_list_lock);
  11159. static LIST_HEAD(module_list);
  11160. static int core_number;
  11161. /*******************************************************************************
  11162. * FUNCTION: pt_probe_module
  11163. *
  11164. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11165. *
  11166. * RETURN:
  11167. * 0 = success
  11168. * !0 = failure
  11169. *
  11170. * PARAMETERS:
  11171. * *cd - pointer to core data
  11172. * *module - pointer to module structure
  11173. ******************************************************************************/
  11174. static int pt_probe_module(struct pt_core_data *cd,
  11175. struct pt_module *module)
  11176. {
  11177. struct module_node *module_node;
  11178. int rc = 0;
  11179. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11180. if (!module_node)
  11181. return -ENOMEM;
  11182. module_node->module = module;
  11183. mutex_lock(&cd->module_list_lock);
  11184. list_add(&module_node->node, &cd->module_list);
  11185. mutex_unlock(&cd->module_list_lock);
  11186. rc = module->probe(cd->dev, &module_node->data);
  11187. if (rc) {
  11188. /*
  11189. * Remove from the list when probe fails
  11190. * in order not to call release
  11191. */
  11192. mutex_lock(&cd->module_list_lock);
  11193. list_del(&module_node->node);
  11194. mutex_unlock(&cd->module_list_lock);
  11195. kfree(module_node);
  11196. goto exit;
  11197. }
  11198. exit:
  11199. return rc;
  11200. }
  11201. /*******************************************************************************
  11202. * FUNCTION: pt_release_module
  11203. *
  11204. * SUMMARY: Call the release pointer and remove the module pointer from
  11205. * module_list.
  11206. *
  11207. * PARAMETERS:
  11208. * *cd - pointer to core data
  11209. * *module - pointer to module structure
  11210. ******************************************************************************/
  11211. static void pt_release_module(struct pt_core_data *cd,
  11212. struct pt_module *module)
  11213. {
  11214. struct module_node *m, *m_n;
  11215. mutex_lock(&cd->module_list_lock);
  11216. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11217. if (m->module == module) {
  11218. module->release(cd->dev, m->data);
  11219. list_del(&m->node);
  11220. kfree(m);
  11221. break;
  11222. }
  11223. mutex_unlock(&cd->module_list_lock);
  11224. }
  11225. /*******************************************************************************
  11226. * FUNCTION: pt_probe_modules
  11227. *
  11228. * SUMMARY: Iterate module_list and probe each module.
  11229. *
  11230. * PARAMETERS:
  11231. * *cd - pointer to core data
  11232. ******************************************************************************/
  11233. static void pt_probe_modules(struct pt_core_data *cd)
  11234. {
  11235. struct pt_module *m;
  11236. int rc = 0;
  11237. mutex_lock(&module_list_lock);
  11238. list_for_each_entry(m, &module_list, node) {
  11239. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11240. __func__, m->name);
  11241. rc = pt_probe_module(cd, m);
  11242. if (rc)
  11243. pt_debug(cd->dev, DL_ERROR,
  11244. "%s: Probe fails for module %s\n",
  11245. __func__, m->name);
  11246. }
  11247. mutex_unlock(&module_list_lock);
  11248. }
  11249. /*******************************************************************************
  11250. * FUNCTION: pt_release_modules
  11251. *
  11252. * SUMMARY: Iterate module_list and remove each module.
  11253. *
  11254. * PARAMETERS:
  11255. * *cd - pointer to core data
  11256. ******************************************************************************/
  11257. static void pt_release_modules(struct pt_core_data *cd)
  11258. {
  11259. struct pt_module *m;
  11260. mutex_lock(&module_list_lock);
  11261. list_for_each_entry(m, &module_list, node)
  11262. pt_release_module(cd, m);
  11263. mutex_unlock(&module_list_lock);
  11264. }
  11265. /*******************************************************************************
  11266. * FUNCTION: pt_get_core_data
  11267. *
  11268. * SUMMARY: Iterate core_list and get core data.
  11269. *
  11270. * RETURN:
  11271. * pointer to core data or null pointer if fail
  11272. *
  11273. * PARAMETERS:
  11274. * *id - pointer to core id
  11275. ******************************************************************************/
  11276. struct pt_core_data *pt_get_core_data(char *id)
  11277. {
  11278. struct pt_core_data *d;
  11279. list_for_each_entry(d, &core_list, node)
  11280. if (!strncmp(d->core_id, id, 20))
  11281. return d;
  11282. return NULL;
  11283. }
  11284. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11285. /*******************************************************************************
  11286. * FUNCTION: pt_add_core
  11287. *
  11288. * SUMMARY: Add core data to the core_list.
  11289. *
  11290. * PARAMETERS:
  11291. * *dev - pointer to device structure
  11292. ******************************************************************************/
  11293. static void pt_add_core(struct device *dev)
  11294. {
  11295. struct pt_core_data *d;
  11296. struct pt_core_data *cd = dev_get_drvdata(dev);
  11297. mutex_lock(&core_list_lock);
  11298. list_for_each_entry(d, &core_list, node)
  11299. if (d->dev == dev)
  11300. goto unlock;
  11301. list_add(&cd->node, &core_list);
  11302. unlock:
  11303. mutex_unlock(&core_list_lock);
  11304. }
  11305. /*******************************************************************************
  11306. * FUNCTION: pt_del_core
  11307. *
  11308. * SUMMARY: Remove core data from the core_list.
  11309. *
  11310. * PARAMETERS:
  11311. * *dev - pointer to device structure
  11312. ******************************************************************************/
  11313. static void pt_del_core(struct device *dev)
  11314. {
  11315. struct pt_core_data *d, *d_n;
  11316. mutex_lock(&core_list_lock);
  11317. list_for_each_entry_safe(d, d_n, &core_list, node)
  11318. if (d->dev == dev) {
  11319. list_del(&d->node);
  11320. goto unlock;
  11321. }
  11322. unlock:
  11323. mutex_unlock(&core_list_lock);
  11324. }
  11325. /*******************************************************************************
  11326. * FUNCTION: pt_register_module
  11327. *
  11328. * SUMMARY: Register the module to module_list and probe the module for each
  11329. * core.
  11330. *
  11331. * RETURN:
  11332. * 0 = success
  11333. * !0 = failure
  11334. *
  11335. * PARAMETERS:
  11336. * *module - pointer to module structure
  11337. ******************************************************************************/
  11338. int pt_register_module(struct pt_module *module)
  11339. {
  11340. struct pt_module *m;
  11341. struct pt_core_data *cd;
  11342. int rc = 0;
  11343. if (!module || !module->probe || !module->release)
  11344. return -EINVAL;
  11345. mutex_lock(&module_list_lock);
  11346. list_for_each_entry(m, &module_list, node)
  11347. if (m == module) {
  11348. rc = -EEXIST;
  11349. goto unlock;
  11350. }
  11351. list_add(&module->node, &module_list);
  11352. /* Probe the module for each core */
  11353. mutex_lock(&core_list_lock);
  11354. list_for_each_entry(cd, &core_list, node)
  11355. pt_probe_module(cd, module);
  11356. mutex_unlock(&core_list_lock);
  11357. unlock:
  11358. mutex_unlock(&module_list_lock);
  11359. return rc;
  11360. }
  11361. EXPORT_SYMBOL_GPL(pt_register_module);
  11362. /*******************************************************************************
  11363. * FUNCTION: pt_unregister_module
  11364. *
  11365. * SUMMARY: Release the module for each core and remove the module from
  11366. * module_list.
  11367. *
  11368. * RETURN:
  11369. * 0 = success
  11370. * !0 = failure
  11371. *
  11372. * PARAMETERS:
  11373. * *module - pointer to module structure
  11374. ******************************************************************************/
  11375. void pt_unregister_module(struct pt_module *module)
  11376. {
  11377. struct pt_module *m, *m_n;
  11378. struct pt_core_data *cd;
  11379. if (!module)
  11380. return;
  11381. mutex_lock(&module_list_lock);
  11382. /* Release the module for each core */
  11383. mutex_lock(&core_list_lock);
  11384. list_for_each_entry(cd, &core_list, node)
  11385. pt_release_module(cd, module);
  11386. mutex_unlock(&core_list_lock);
  11387. list_for_each_entry_safe(m, m_n, &module_list, node)
  11388. if (m == module) {
  11389. list_del(&m->node);
  11390. break;
  11391. }
  11392. mutex_unlock(&module_list_lock);
  11393. }
  11394. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11395. /*******************************************************************************
  11396. * FUNCTION: pt_get_module_data
  11397. *
  11398. * SUMMARY: Get module data from module_node by module_list.
  11399. *
  11400. * RETURN:
  11401. * pointer to module data
  11402. *
  11403. * PARAMETERS:
  11404. * *dev - pointer to device structure
  11405. * *module - pointer to module structure
  11406. ******************************************************************************/
  11407. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11408. {
  11409. struct pt_core_data *cd = dev_get_drvdata(dev);
  11410. struct module_node *m;
  11411. void *data = NULL;
  11412. mutex_lock(&cd->module_list_lock);
  11413. list_for_each_entry(m, &cd->module_list, node)
  11414. if (m->module == module) {
  11415. data = m->data;
  11416. break;
  11417. }
  11418. mutex_unlock(&cd->module_list_lock);
  11419. return data;
  11420. }
  11421. EXPORT_SYMBOL(pt_get_module_data);
  11422. #ifdef CONFIG_HAS_EARLYSUSPEND
  11423. /*******************************************************************************
  11424. * FUNCTION: pt_early_suspend
  11425. *
  11426. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11427. * attention list.
  11428. *
  11429. * PARAMETERS:
  11430. * *h - pointer to early_suspend structure
  11431. ******************************************************************************/
  11432. static void pt_early_suspend(struct early_suspend *h)
  11433. {
  11434. struct pt_core_data *cd =
  11435. container_of(h, struct pt_core_data, es);
  11436. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11437. }
  11438. /*******************************************************************************
  11439. * FUNCTION: pt_late_resume
  11440. *
  11441. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11442. * attention list.
  11443. *
  11444. * PARAMETERS:
  11445. * *h - pointer to early_suspend structure
  11446. ******************************************************************************/
  11447. static void pt_late_resume(struct early_suspend *h)
  11448. {
  11449. struct pt_core_data *cd =
  11450. container_of(h, struct pt_core_data, es);
  11451. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11452. }
  11453. /*******************************************************************************
  11454. * FUNCTION: pt_setup_early_suspend
  11455. *
  11456. * SUMMARY: Register early/suspend function to the system.
  11457. *
  11458. * PARAMETERS:
  11459. * *cd - pointer to core data
  11460. ******************************************************************************/
  11461. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11462. {
  11463. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11464. cd->es.suspend = pt_early_suspend;
  11465. cd->es.resume = pt_late_resume;
  11466. register_early_suspend(&cd->es);
  11467. }
  11468. #elif defined(CONFIG_DRM)
  11469. static void pt_resume_work(struct work_struct *work)
  11470. {
  11471. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11472. resume_work);
  11473. int rc = 0;
  11474. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11475. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11476. return;
  11477. rc = pt_core_easywake_off(pt_data);
  11478. if (rc < 0) {
  11479. pt_debug(pt_data->dev, DL_ERROR,
  11480. "%s: Error on wake\n", __func__);
  11481. }
  11482. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11483. return;
  11484. }
  11485. static void pt_suspend_work(struct work_struct *work)
  11486. {
  11487. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11488. suspend_work);
  11489. int rc = 0;
  11490. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11491. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11492. return;
  11493. rc = pt_core_easywake_on(pt_data);
  11494. if (rc < 0) {
  11495. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11496. return;
  11497. }
  11498. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11499. return;
  11500. }
  11501. #if defined(CONFIG_PANEL_NOTIFIER)
  11502. /*******************************************************************************
  11503. * FUNCTION: panel_event_notifier_callback
  11504. *
  11505. * SUMMARY: Call back function for Panel Event notifier to allow to call
  11506. * resume/suspend attention list.
  11507. *
  11508. * PARAMETERS:
  11509. * tag - type of input panel.
  11510. * *notification - pointer to notification details.
  11511. * *client_data - pointer to core data
  11512. ******************************************************************************/
  11513. static void panel_event_notifier_callback(enum panel_event_notifier_tag tag,
  11514. struct panel_event_notification *notification, void *client_data)
  11515. {
  11516. struct pt_core_data *cd = client_data;
  11517. if(!notification)
  11518. {
  11519. pt_debug(cd->dev,DL_INFO, "%s: Invalid notification\n", __func__);
  11520. return;
  11521. }
  11522. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11523. if (cd->quick_boot || cd->drv_debug_suspend)
  11524. goto exit;
  11525. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%d,fb_state %d",
  11526. __func__, notification->notif_type, cd->fb_state);
  11527. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11528. __func__, (notification->notif_type == DRM_PANEL_EVENT_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11529. if (notification->notif_type == DRM_PANEL_EVENT_UNBLANK) {
  11530. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11531. if (notification->notif_data.early_trigger) {
  11532. pr_err("%s: resume: event = %d, not care\n", __func__, notification->notif_type);
  11533. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %d, not care\n",
  11534. __func__, notification->notif_type);
  11535. } else {
  11536. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11537. __func__);
  11538. cancel_work_sync(&cd->resume_offload_work);
  11539. cancel_work_sync(&cd->suspend_offload_work);
  11540. cancel_work_sync(&cd->resume_work);
  11541. cancel_work_sync(&cd->suspend_work);
  11542. queue_work(cd->pt_workqueue, &cd->resume_work);
  11543. #if defined(CONFIG_PM_SLEEP)
  11544. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11545. __func__);
  11546. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11547. pt_core_resume_(cd->dev);
  11548. #endif
  11549. cd->fb_state = FB_ON;
  11550. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11551. }
  11552. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11553. pt_debug(cd->dev, DL_INFO, "%s: BLANK!\n", __func__);
  11554. if (notification->notif_data.early_trigger) {
  11555. #if defined(CONFIG_PM_SLEEP)
  11556. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11557. __func__);
  11558. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11559. pt_core_suspend_(cd->dev);
  11560. #endif
  11561. cancel_work_sync(&cd->resume_offload_work);
  11562. cancel_work_sync(&cd->suspend_offload_work);
  11563. cancel_work_sync(&cd->resume_work);
  11564. cancel_work_sync(&cd->suspend_work);
  11565. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11566. cd->fb_state = FB_OFF;
  11567. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11568. } else {
  11569. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11570. __func__, notification->notif_type);
  11571. }
  11572. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK_LP) {
  11573. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11574. if (notification->notif_data.early_trigger) {
  11575. #if defined(CONFIG_PM_SLEEP)
  11576. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n", __func__);
  11577. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11578. pt_core_suspend_(cd->dev);
  11579. #endif
  11580. cancel_work_sync(&cd->resume_offload_work);
  11581. cancel_work_sync(&cd->suspend_offload_work);
  11582. cancel_work_sync(&cd->resume_work);
  11583. cancel_work_sync(&cd->suspend_work);
  11584. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11585. cd->fb_state = FB_OFF;
  11586. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11587. } else {
  11588. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11589. __func__, notification->notif_type);
  11590. }
  11591. } else {
  11592. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11593. __func__, notification->notif_type);
  11594. }
  11595. exit:
  11596. return;
  11597. }
  11598. /*******************************************************************************
  11599. * FUNCTION: pt_setup_panel_event_notifier
  11600. *
  11601. * SUMMARY: Set up call back function into drm notifier.
  11602. *
  11603. * PARAMETERS:
  11604. * *cd - pointer to core data
  11605. ******************************************************************************/
  11606. static void pt_setup_panel_event_notifier(struct pt_core_data *cd)
  11607. {
  11608. void *cookie = NULL;
  11609. if (!active_panel)
  11610. pt_debug(cd->dev, DL_ERROR,
  11611. "%s: Active panel not registered!\n", __func__);
  11612. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11613. if (!cd->pt_workqueue) {
  11614. pt_debug(cd->dev, DL_ERROR,
  11615. "%s: worker thread creation failed !\n", __func__);
  11616. }
  11617. if (cd->pt_workqueue) {
  11618. INIT_WORK(&cd->resume_work, pt_resume_work);
  11619. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11620. }
  11621. cookie = panel_event_notifier_register(PANEL_EVENT_NOTIFICATION_PRIMARY,
  11622. PANEL_EVENT_NOTIFIER_CLIENT_PRIMARY_TOUCH,
  11623. active_panel,&panel_event_notifier_callback, cd);
  11624. if (active_panel && !cookie)
  11625. {
  11626. pt_debug(cd->dev, DL_ERROR,
  11627. "%s: Register notifier failed!\n", __func__);
  11628. }
  11629. }
  11630. #else
  11631. /*******************************************************************************
  11632. * FUNCTION: drm_notifier_callback
  11633. *
  11634. * SUMMARY: Call back function for DRM notifier to allow to call
  11635. * resume/suspend attention list.
  11636. *
  11637. * RETURN:
  11638. * 0 = success
  11639. *
  11640. * PARAMETERS:
  11641. * *self - pointer to notifier_block structure
  11642. * event - event type of fb notifier
  11643. * *data - pointer to fb_event structure
  11644. ******************************************************************************/
  11645. static int drm_notifier_callback(struct notifier_block *self,
  11646. unsigned long event, void *data)
  11647. {
  11648. struct pt_core_data *cd =
  11649. container_of(self, struct pt_core_data, fb_notifier);
  11650. struct drm_panel_notifier *evdata = data;
  11651. int *blank;
  11652. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11653. if (!evdata)
  11654. goto exit;
  11655. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11656. event == DRM_PANEL_EVENT_BLANK)) {
  11657. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11658. __func__, event);
  11659. goto exit;
  11660. }
  11661. if (cd->quick_boot || cd->drv_debug_suspend)
  11662. goto exit;
  11663. blank = evdata->data;
  11664. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11665. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11666. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11667. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11668. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11669. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11670. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11671. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11672. __func__, event);
  11673. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11674. if (cd->fb_state != FB_ON) {
  11675. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11676. __func__);
  11677. cancel_work_sync(&cd->resume_offload_work);
  11678. cancel_work_sync(&cd->suspend_offload_work);
  11679. cancel_work_sync(&cd->resume_work);
  11680. cancel_work_sync(&cd->suspend_work);
  11681. queue_work(cd->pt_workqueue, &cd->resume_work);
  11682. #if defined(CONFIG_PM_SLEEP)
  11683. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11684. __func__);
  11685. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11686. pt_core_resume_(cd->dev);
  11687. #endif
  11688. cd->fb_state = FB_ON;
  11689. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11690. }
  11691. }
  11692. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11693. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11694. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11695. if (cd->fb_state != FB_OFF) {
  11696. #if defined(CONFIG_PM_SLEEP)
  11697. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11698. __func__);
  11699. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11700. pt_core_suspend_(cd->dev);
  11701. #endif
  11702. cancel_work_sync(&cd->resume_offload_work);
  11703. cancel_work_sync(&cd->suspend_offload_work);
  11704. cancel_work_sync(&cd->resume_work);
  11705. cancel_work_sync(&cd->suspend_work);
  11706. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11707. cd->fb_state = FB_OFF;
  11708. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11709. }
  11710. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11711. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11712. __func__, event);
  11713. }
  11714. } else {
  11715. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11716. __func__, *blank);
  11717. }
  11718. exit:
  11719. return 0;
  11720. }
  11721. /*******************************************************************************
  11722. * FUNCTION: pt_setup_drm_notifier
  11723. *
  11724. * SUMMARY: Set up call back function into drm notifier.
  11725. *
  11726. * PARAMETERS:
  11727. * *cd - pointer to core data
  11728. ******************************************************************************/
  11729. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11730. {
  11731. cd->fb_state = FB_NONE;
  11732. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11733. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11734. if (!active_panel)
  11735. pt_debug(cd->dev, DL_ERROR,
  11736. "%s: Active panel not registered!\n", __func__);
  11737. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11738. if (!cd->pt_workqueue) {
  11739. pt_debug(cd->dev, DL_ERROR,
  11740. "%s: worker thread creation failed !\n", __func__);
  11741. }
  11742. if (cd->pt_workqueue) {
  11743. INIT_WORK(&cd->resume_work, pt_resume_work);
  11744. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11745. }
  11746. if (active_panel &&
  11747. drm_panel_notifier_register(active_panel,
  11748. &cd->fb_notifier) < 0)
  11749. pt_debug(cd->dev, DL_ERROR,
  11750. "%s: Register notifier failed!\n", __func__);
  11751. }
  11752. #endif
  11753. #elif defined(CONFIG_FB)
  11754. /*******************************************************************************
  11755. * FUNCTION: fb_notifier_callback
  11756. *
  11757. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11758. * resume/suspend attention list.
  11759. *
  11760. * RETURN:
  11761. * 0 = success
  11762. *
  11763. * PARAMETERS:
  11764. * *self - pointer to notifier_block structure
  11765. * event - event type of fb notifier
  11766. * *data - pointer to fb_event structure
  11767. ******************************************************************************/
  11768. static int fb_notifier_callback(struct notifier_block *self,
  11769. unsigned long event, void *data)
  11770. {
  11771. struct pt_core_data *cd =
  11772. container_of(self, struct pt_core_data, fb_notifier);
  11773. struct fb_event *evdata = data;
  11774. int *blank;
  11775. if (event != FB_EVENT_BLANK || !evdata)
  11776. goto exit;
  11777. blank = evdata->data;
  11778. if (*blank == FB_BLANK_UNBLANK) {
  11779. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11780. if (cd->fb_state != FB_ON) {
  11781. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11782. #if defined(CONFIG_PM_SLEEP)
  11783. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11784. pt_core_resume_(cd->dev);
  11785. #endif
  11786. cd->fb_state = FB_ON;
  11787. }
  11788. } else if (*blank == FB_BLANK_POWERDOWN) {
  11789. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11790. if (cd->fb_state != FB_OFF) {
  11791. #if defined(CONFIG_PM_SLEEP)
  11792. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11793. pt_core_suspend_(cd->dev);
  11794. #endif
  11795. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11796. cd->fb_state = FB_OFF;
  11797. }
  11798. }
  11799. exit:
  11800. return 0;
  11801. }
  11802. /*******************************************************************************
  11803. * FUNCTION: pt_setup_fb_notifier
  11804. *
  11805. * SUMMARY: Set up call back function into fb notifier.
  11806. *
  11807. * PARAMETERS:
  11808. * *cd - pointer to core data
  11809. ******************************************************************************/
  11810. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11811. {
  11812. int rc = 0;
  11813. cd->fb_state = FB_ON;
  11814. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11815. rc = fb_register_client(&cd->fb_notifier);
  11816. if (rc)
  11817. pt_debug(cd->dev, DL_ERROR,
  11818. "Unable to register fb_notifier: %d\n", rc);
  11819. }
  11820. #endif
  11821. /*******************************************************************************
  11822. * FUNCTION: pt_watchdog_work
  11823. *
  11824. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11825. * sleeping then this function simply returns. If the DUT is awake the
  11826. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11827. * somehow a response is waiting on the DUT that has not been read. If
  11828. * this occurs the message is simply consumed. If or once the IRQ is
  11829. * cleared, a PIP PING message is sent to the DUT and if the response
  11830. * is received the watchdog succeeds and exits, if no response is seen
  11831. * a startup is queued unless the maximum number of startups have already
  11832. * been attempted, in that case a BL is attempted.
  11833. *
  11834. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11835. * work thread
  11836. *
  11837. * RETURN: void
  11838. *
  11839. * PARAMETERS:
  11840. * *work - pointer to a work structure for the watchdog work queue
  11841. ******************************************************************************/
  11842. static void pt_watchdog_work(struct work_struct *work)
  11843. {
  11844. int rc = 0;
  11845. struct pt_core_data *cd = container_of(work,
  11846. struct pt_core_data, watchdog_work);
  11847. /*
  11848. * if found the current sleep_state is SS_SLEEPING
  11849. * then no need to request_exclusive, directly return
  11850. */
  11851. if (cd->sleep_state == SS_SLEEPING)
  11852. return;
  11853. #ifdef TTDL_DIAGNOSTICS
  11854. cd->watchdog_count++;
  11855. #endif /* TTDL_DIAGNOSTICS */
  11856. /*
  11857. * The first WD interval was extended to allow DDI to come up.
  11858. * If the WD interval is not the default then adjust timer to the
  11859. * current setting. The user can override value with drv_debug sysfs.
  11860. */
  11861. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11862. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11863. msecs_to_jiffies(cd->watchdog_interval));
  11864. }
  11865. if (pt_check_irq_asserted(cd)) {
  11866. #ifdef TTDL_DIAGNOSTICS
  11867. cd->watchdog_irq_stuck_count++;
  11868. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11869. #endif /* TTDL_DIAGNOSTICS */
  11870. pt_debug(cd->dev, DL_ERROR,
  11871. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11872. __func__);
  11873. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11874. }
  11875. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11876. if (rc < 0) {
  11877. pt_debug(cd->dev, DL_ERROR,
  11878. "%s: fail get exclusive ex=%p own=%p\n",
  11879. __func__, cd->exclusive_dev, cd->dev);
  11880. goto queue_startup;
  11881. }
  11882. rc = pt_pip_null_(cd);
  11883. if (release_exclusive(cd, cd->dev) < 0)
  11884. pt_debug(cd->dev, DL_ERROR,
  11885. "%s: fail to release exclusive\n", __func__);
  11886. queue_startup:
  11887. if (rc) {
  11888. #ifdef TTDL_DIAGNOSTICS
  11889. cd->watchdog_failed_access_count++;
  11890. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11891. #endif /* TTDL_DIAGNOSTICS */
  11892. pt_debug(cd->dev, DL_ERROR,
  11893. "%s: failed to access device in WD, retry count=%d\n",
  11894. __func__, cd->startup_retry_count);
  11895. /* Already tried FW upgrade because of watchdog but failed */
  11896. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11897. return;
  11898. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11899. /*
  11900. * Any wrapper function that trys to disable the
  11901. * WD killing this worker cannot be called here.
  11902. */
  11903. rc = request_exclusive(cd, cd->dev,
  11904. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11905. if (rc < 0) {
  11906. pt_debug(cd->dev, DL_ERROR,
  11907. "%s: fail get exclusive ex=%p own=%p\n",
  11908. __func__, cd->exclusive_dev, cd->dev);
  11909. goto exit;
  11910. }
  11911. cd->hw_detected = false;
  11912. cd->startup_status = STARTUP_STATUS_START;
  11913. pt_debug(cd->dev, DL_DEBUG,
  11914. "%s: Startup Status Reset\n", __func__);
  11915. rc = pt_dut_reset_and_wait(cd);
  11916. if (release_exclusive(cd, cd->dev) < 0)
  11917. pt_debug(cd->dev, DL_ERROR,
  11918. "%s: fail to release exclusive\n",
  11919. __func__);
  11920. if (!rc) {
  11921. cd->hw_detected = true;
  11922. if (!cd->flashless_dut)
  11923. pt_queue_enum(cd);
  11924. }
  11925. #ifdef TTDL_DIAGNOSTICS
  11926. cd->wd_xres_count++;
  11927. pt_debug(cd->dev, DL_ERROR,
  11928. "%s: Comm Failed - DUT reset [#%d]\n",
  11929. __func__, cd->wd_xres_count);
  11930. #endif /* TTDL_DIAGNOSTICS */
  11931. } else {
  11932. /*
  11933. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11934. * reset the part to regain communications, try to BL
  11935. */
  11936. pt_debug(cd->dev, DL_ERROR,
  11937. "%s: WD DUT access failure, Start FW Upgrade\n",
  11938. __func__);
  11939. #ifdef TTDL_DIAGNOSTICS
  11940. /*
  11941. * When diagnostics is enabled allow TTDL to keep
  11942. * trying to find the DUT. This allows the DUT to be
  11943. * hot swap-able while the host stays running. In
  11944. * production this may not be wanted as a customer
  11945. * may have several touch drivers and any driver
  11946. * that doesn't match the current DUT should give
  11947. * up trying and give up using the bus.
  11948. */
  11949. pt_debug(cd->dev, DL_INFO,
  11950. "%s: Resetting startup_retry_count\n",
  11951. __func__);
  11952. cd->startup_retry_count = 0;
  11953. #endif /* TTDL_DIAGNOSTICS */
  11954. /*
  11955. * Since fw may be broken,reset sysinfo ready flag
  11956. * to let upgrade function work.
  11957. */
  11958. mutex_lock(&cd->system_lock);
  11959. cd->sysinfo.ready = false;
  11960. mutex_unlock(&cd->system_lock);
  11961. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11962. pt_debug(cd->dev, DL_ERROR,
  11963. "%s: Queue Restart\n", __func__);
  11964. pt_queue_restart(cd);
  11965. } else
  11966. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11967. }
  11968. } else {
  11969. cd->hw_detected = true;
  11970. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11971. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11972. pt_debug(cd->dev, DL_ERROR,
  11973. "%s: HW detected but not enumerated\n",
  11974. __func__);
  11975. pt_queue_enum(cd);
  11976. }
  11977. }
  11978. exit:
  11979. pt_start_wd_timer(cd);
  11980. }
  11981. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11982. /*******************************************************************************
  11983. * FUNCTION: pt_watchdog_timer
  11984. *
  11985. * SUMMARY: The function that is called when the WD timer expires. If the
  11986. * watchdog work is not already busy schedule the watchdog work queue.
  11987. *
  11988. * RETURN: void
  11989. *
  11990. * PARAMETERS:
  11991. * handle - Handle to the watchdog timer
  11992. ******************************************************************************/
  11993. static void pt_watchdog_timer(unsigned long handle)
  11994. {
  11995. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11996. if (!cd)
  11997. return;
  11998. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11999. __func__);
  12000. if (!work_pending(&cd->watchdog_work))
  12001. schedule_work(&cd->watchdog_work);
  12002. }
  12003. #else
  12004. /*******************************************************************************
  12005. * FUNCTION: pt_watchdog_timer
  12006. *
  12007. * SUMMARY: The function that is called when the WD timer expires. If the
  12008. * watchdog work is not already busy schedule the watchdog work queue.
  12009. *
  12010. * RETURN: void
  12011. *
  12012. * PARAMETERS:
  12013. * *t - Pointer to timer list
  12014. ******************************************************************************/
  12015. static void pt_watchdog_timer(struct timer_list *t)
  12016. {
  12017. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  12018. if (!cd)
  12019. return;
  12020. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12021. __func__);
  12022. if (!work_pending(&cd->watchdog_work))
  12023. schedule_work(&cd->watchdog_work);
  12024. }
  12025. #endif
  12026. /*******************************************************************************
  12027. * Core sysfs show and store functions
  12028. ******************************************************************************/
  12029. /*******************************************************************************
  12030. * FUNCTION: pt_hw_version_show
  12031. *
  12032. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  12033. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  12034. *
  12035. * RETURN: size of data written to sysfs node
  12036. *
  12037. * PARAMETERS:
  12038. * *dev - pointer to device structure
  12039. * *attr - pointer to device attributes structure
  12040. * *buf - pointer to print output buffer
  12041. ******************************************************************************/
  12042. static ssize_t pt_hw_version_show(struct device *dev,
  12043. struct device_attribute *attr, char *buf)
  12044. {
  12045. struct pt_core_data *cd = dev_get_drvdata(dev);
  12046. _pt_request_hw_version(dev, cd->hw_version);
  12047. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  12048. }
  12049. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  12050. /*******************************************************************************
  12051. * FUNCTION: pt_drv_version_show
  12052. *
  12053. * SUMMARY: Show method for the drv_version sysfs node that will show the
  12054. * TTDL version information
  12055. *
  12056. * RETURN: Char buffer with printed TTDL version information
  12057. *
  12058. * PARAMETERS:
  12059. * *dev - pointer to device structure
  12060. * *attr - pointer to device attributes
  12061. * *buf - pointer to output buffer
  12062. ******************************************************************************/
  12063. static ssize_t pt_drv_version_show(struct device *dev,
  12064. struct device_attribute *attr, char *buf)
  12065. {
  12066. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12067. "Driver: %s\nVersion: %s\nDate: %s\n",
  12068. pt_driver_core_name, pt_driver_core_version,
  12069. pt_driver_core_date);
  12070. }
  12071. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  12072. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  12073. /*******************************************************************************
  12074. * FUNCTION: pt_fw_version_show
  12075. *
  12076. * SUMMARY: Show method for the fw_version sysfs node that will
  12077. * show the firmware, bootloader and PIP version information
  12078. *
  12079. * RETURN: Size of printed buffer
  12080. *
  12081. * PARAMETERS:
  12082. * *dev - pointer to device structure
  12083. * *attr - pointer to device attributes
  12084. * *buf - pointer to output buffer
  12085. ******************************************************************************/
  12086. static ssize_t pt_fw_version_show(struct device *dev,
  12087. struct device_attribute *attr, char *buf)
  12088. {
  12089. struct pt_core_data *cd = dev_get_drvdata(dev);
  12090. struct pt_ttdata *ttdata;
  12091. int rc = 0;
  12092. if (cd->mode == PT_MODE_OPERATIONAL)
  12093. rc = pt_hid_output_get_sysinfo_(cd);
  12094. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12095. __func__, cd->mode, cd->sysinfo.ready);
  12096. if (cd->sysinfo.ready)
  12097. ttdata = &cd->sysinfo.ttdata;
  12098. else
  12099. rc = -ENODATA;
  12100. if (!rc) {
  12101. return scnprintf(buf, strlen(buf),
  12102. "Status: %d\n"
  12103. "FW : %d.%d.%d\n"
  12104. "Config: %d\n"
  12105. "BL : %d.%d\n"
  12106. "PIP : %d.%d\n",
  12107. rc,
  12108. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  12109. ttdata->revctrl,
  12110. ttdata->fw_ver_conf,
  12111. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  12112. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  12113. } else {
  12114. return scnprintf(buf, strlen(buf),
  12115. "Status: %d\n"
  12116. "FW : n/a\n"
  12117. "Config: n/a\n"
  12118. "BL : n/a\n"
  12119. "PIP : n/a\n",
  12120. rc);
  12121. }
  12122. }
  12123. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  12124. /*******************************************************************************
  12125. * FUNCTION: pt_sysinfo_show
  12126. *
  12127. * SUMMARY: Show method for the sysinfo sysfs node that will
  12128. * show all the information from get system information command.
  12129. *
  12130. * RETURN: Size of printed buffer
  12131. *
  12132. * PARAMETERS:
  12133. * *dev - pointer to device structure
  12134. * *attr - pointer to device attributes
  12135. * *buf - pointer to output buffer
  12136. ******************************************************************************/
  12137. static ssize_t pt_sysinfo_show(struct device *dev,
  12138. struct device_attribute *attr, char *buf)
  12139. {
  12140. struct pt_core_data *cd = dev_get_drvdata(dev);
  12141. struct pt_sysinfo *si;
  12142. struct pt_ttdata *ttdata = NULL;
  12143. struct pt_sensing_conf_data *scd = NULL;
  12144. int rc = 0;
  12145. if (cd->mode == PT_MODE_OPERATIONAL) {
  12146. rc = pt_hid_output_get_sysinfo_(cd);
  12147. if (cd->sysinfo.ready) {
  12148. si = &cd->sysinfo;
  12149. ttdata = &si->ttdata;
  12150. scd = &si->sensing_conf_data;
  12151. } else
  12152. rc = -ENODATA;
  12153. } else
  12154. rc = -EPERM;
  12155. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12156. __func__, cd->mode, cd->sysinfo.ready);
  12157. if (!rc && ttdata && scd) {
  12158. return scnprintf(buf, strlen(buf),
  12159. "Status: %d\n"
  12160. "pip_ver_major: 0x%02X\n"
  12161. "pip_ver_minor: 0x%02X\n"
  12162. "fw_pid : 0x%04X\n"
  12163. "fw_ver_major : 0x%02X\n"
  12164. "fw_ver_minor : 0x%02X\n"
  12165. "revctrl : 0x%08X\n"
  12166. "fw_ver_conf : 0x%04X\n"
  12167. "bl_ver_major : 0x%02X\n"
  12168. "bl_ver_minor : 0x%02X\n"
  12169. "jtag_id_h : 0x%04X\n"
  12170. "jtag_id_l : 0x%04X\n"
  12171. "mfg_id[0] : 0x%02X\n"
  12172. "mfg_id[1] : 0x%02X\n"
  12173. "mfg_id[2] : 0x%02X\n"
  12174. "mfg_id[3] : 0x%02X\n"
  12175. "mfg_id[4] : 0x%02X\n"
  12176. "mfg_id[5] : 0x%02X\n"
  12177. "mfg_id[6] : 0x%02X\n"
  12178. "mfg_id[7] : 0x%02X\n"
  12179. "post_code : 0x%04X\n"
  12180. "electrodes_x : 0x%02X\n"
  12181. "electrodes_y : 0x%02X\n"
  12182. "len_x : 0x%04X\n"
  12183. "len_y : 0x%04X\n"
  12184. "res_x : 0x%04X\n"
  12185. "res_y : 0x%04X\n"
  12186. "max_z : 0x%04X\n"
  12187. "origin_x : 0x%02X\n"
  12188. "origin_y : 0x%02X\n"
  12189. "panel_id : 0x%02X\n"
  12190. "btn : 0x%02X\n"
  12191. "scan_mode : 0x%02X\n"
  12192. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12193. rc,
  12194. ttdata->pip_ver_major,
  12195. ttdata->pip_ver_minor,
  12196. ttdata->fw_pid,
  12197. ttdata->fw_ver_major,
  12198. ttdata->fw_ver_minor,
  12199. ttdata->revctrl,
  12200. ttdata->fw_ver_conf,
  12201. ttdata->bl_ver_major,
  12202. ttdata->bl_ver_minor,
  12203. ttdata->jtag_id_h,
  12204. ttdata->jtag_id_l,
  12205. ttdata->mfg_id[0],
  12206. ttdata->mfg_id[1],
  12207. ttdata->mfg_id[2],
  12208. ttdata->mfg_id[3],
  12209. ttdata->mfg_id[4],
  12210. ttdata->mfg_id[5],
  12211. ttdata->mfg_id[6],
  12212. ttdata->mfg_id[7],
  12213. ttdata->post_code,
  12214. scd->electrodes_x,
  12215. scd->electrodes_y,
  12216. scd->len_x,
  12217. scd->len_y,
  12218. scd->res_x,
  12219. scd->res_y,
  12220. scd->max_z,
  12221. scd->origin_x,
  12222. scd->origin_y,
  12223. scd->panel_id,
  12224. scd->btn,
  12225. scd->scan_mode,
  12226. scd->max_tch);
  12227. } else {
  12228. return scnprintf(buf, strlen(buf),
  12229. "Status: %d\n",
  12230. rc);
  12231. }
  12232. }
  12233. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12234. /*******************************************************************************
  12235. * FUNCTION: pt_hw_reset_show
  12236. *
  12237. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12238. * by toggling the XRES line and then calls the startup function to
  12239. * allow TTDL to re-enumerate the DUT.
  12240. * The printed value reflects the status of the full reset/enum.
  12241. *
  12242. * PARAMETERS:
  12243. * *dev - pointer to Device structure
  12244. * *attr - pointer to the device attribute structure
  12245. * *buf - pointer to buffer to print
  12246. ******************************************************************************/
  12247. static ssize_t pt_hw_reset_show(struct device *dev,
  12248. struct device_attribute *attr, char *buf)
  12249. {
  12250. struct pt_core_data *cd = dev_get_drvdata(dev);
  12251. int rc = 0;
  12252. int time = 0;
  12253. u8 reset_status = 0;
  12254. int t;
  12255. struct pt_hid_desc hid_desc;
  12256. memset(&hid_desc, 0, sizeof(hid_desc));
  12257. /* Only allow DUT reset if no active BL in progress */
  12258. mutex_lock(&cd->firmware_class_lock);
  12259. mutex_lock(&cd->system_lock);
  12260. cd->startup_state = STARTUP_NONE;
  12261. mutex_unlock(&(cd->system_lock));
  12262. pt_stop_wd_timer(cd);
  12263. /* ensure no left over exclusive access is still locked */
  12264. release_exclusive(cd, cd->dev);
  12265. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12266. if (rc) {
  12267. mutex_unlock(&cd->firmware_class_lock);
  12268. pt_debug(cd->dev, DL_ERROR,
  12269. "%s: HW reset failed rc = %d\n", __func__, rc);
  12270. goto exit_hw_reset;
  12271. }
  12272. reset_status |= 0x01 << 0;
  12273. if (cd->flashless_dut) {
  12274. mutex_unlock(&cd->firmware_class_lock);
  12275. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12276. msecs_to_jiffies(200));
  12277. if (IS_TMO(t)) {
  12278. pt_debug(dev, DL_ERROR,
  12279. "%s: Timeout waiting for FW update",
  12280. __func__);
  12281. rc = -ETIME;
  12282. goto exit_hw_reset;
  12283. } else {
  12284. pt_debug(dev, DL_INFO,
  12285. "%s: ----- Wait FW Loading ----",
  12286. __func__);
  12287. rc = _pt_request_wait_for_enum_state(
  12288. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12289. if (rc) {
  12290. pt_debug(cd->dev, DL_ERROR,
  12291. "%s: No FW Sentinel detected rc = %d\n",
  12292. __func__, rc);
  12293. goto exit_hw_reset;
  12294. }
  12295. reset_status |= 0x01 << 1;
  12296. }
  12297. } else {
  12298. /* Wait for any sentinel */
  12299. rc = _pt_request_wait_for_enum_state(dev, 150,
  12300. STARTUP_STATUS_BL_RESET_SENTINEL |
  12301. STARTUP_STATUS_FW_RESET_SENTINEL);
  12302. if (rc) {
  12303. mutex_unlock(&cd->firmware_class_lock);
  12304. pt_debug(cd->dev, DL_ERROR,
  12305. "%s: No Sentinel detected rc = %d\n",
  12306. __func__, rc);
  12307. goto exit_hw_reset;
  12308. }
  12309. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12310. msleep(30);
  12311. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12312. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12313. if (rc < 0) {
  12314. pt_debug(cd->dev, DL_ERROR,
  12315. "%s: Error on getting HID descriptor r=%d\n",
  12316. __func__, rc);
  12317. goto exit_hw_reset;
  12318. }
  12319. cd->mode = pt_get_mode(cd, &hid_desc);
  12320. if (cd->mode == PT_MODE_BOOTLOADER)
  12321. rc = pt_hid_output_bl_launch_app_(cd);
  12322. } else {
  12323. if (cd->mode == PT_MODE_BOOTLOADER)
  12324. rc = pt_pip2_launch_app(dev,
  12325. PT_CORE_CMD_UNPROTECTED);
  12326. }
  12327. if (rc) {
  12328. mutex_unlock(&cd->firmware_class_lock);
  12329. pt_debug(cd->dev, DL_ERROR,
  12330. "%s: PIP launch app failed rc = %d\n",
  12331. __func__, rc);
  12332. goto exit_hw_reset;
  12333. }
  12334. mutex_unlock(&cd->firmware_class_lock);
  12335. reset_status |= 0x01 << 1;
  12336. msleep(20);
  12337. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12338. (cd->mode != PT_MODE_OPERATIONAL))
  12339. pt_queue_restart(cd);
  12340. else
  12341. pt_queue_enum(cd);
  12342. }
  12343. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12344. msleep(50);
  12345. pt_debug(cd->dev, DL_INFO,
  12346. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12347. __func__, time, STARTUP_STATUS_COMPLETE,
  12348. cd->startup_status);
  12349. time += 50;
  12350. }
  12351. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12352. rc = -ETIME;
  12353. goto exit_hw_reset;
  12354. }
  12355. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12356. __func__, cd->startup_status);
  12357. reset_status |= 0x01 << 2;
  12358. pt_start_wd_timer(cd);
  12359. exit_hw_reset:
  12360. return scnprintf(buf, strlen(buf),
  12361. "Status: %d\n"
  12362. "Reset Status: 0x%02X\n", rc, reset_status);
  12363. }
  12364. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12365. /*******************************************************************************
  12366. * FUNCTION: pt_pip2_cmd_rsp_store
  12367. *
  12368. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12369. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12370. * Command byte order:
  12371. * Byte [0] - PIP2 command ID
  12372. * Byte [1-n] - PIP2 command payload
  12373. *
  12374. * RETURN: Size of passed in buffer
  12375. *
  12376. * PARAMETERS:
  12377. * *dev - pointer to device structure
  12378. * *attr - pointer to device attributes
  12379. * *buf - pointer to buffer that hold the command parameters
  12380. * size - size of buf
  12381. ******************************************************************************/
  12382. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12383. struct device_attribute *attr, const char *buf, size_t size)
  12384. {
  12385. struct pt_core_data *cd = dev_get_drvdata(dev);
  12386. u16 actual_read_len;
  12387. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12388. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12389. u8 pip2_cmd_id = 0x00;
  12390. u8 *pip2_cmd_data = NULL;
  12391. int data_len = 0;
  12392. int length;
  12393. int rc = 0;
  12394. /* clear shared data */
  12395. mutex_lock(&cd->sysfs_lock);
  12396. cd->raw_cmd_status = 0;
  12397. cd->cmd_rsp_buf_len = 0;
  12398. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12399. mutex_unlock(&cd->sysfs_lock);
  12400. length = _pt_ic_parse_input_hex(dev, buf, size,
  12401. input_data, PT_MAX_PIP2_MSG_SIZE);
  12402. if (length <= 0 || length > (PT_MAX_PIP2_MSG_SIZE - 2)) {
  12403. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12404. __func__);
  12405. rc = -EINVAL;
  12406. goto exit;
  12407. }
  12408. /* Send PIP2 command if enough data was provided */
  12409. if (length >= 1) {
  12410. pip2_cmd_id = input_data[0];
  12411. pip2_cmd_data = &input_data[1];
  12412. data_len = length - 1;
  12413. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12414. pip2_cmd_id, pip2_cmd_data, data_len,
  12415. read_buf, &actual_read_len);
  12416. cd->raw_cmd_status = rc;
  12417. if (rc) {
  12418. pt_debug(dev, DL_ERROR,
  12419. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12420. __func__, pip2_cmd_id, rc);
  12421. goto exit;
  12422. } else {
  12423. cd->cmd_rsp_buf_len = actual_read_len;
  12424. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12425. pt_debug(dev, DL_ERROR,
  12426. "%s: PIP2 actual_read_len = %d\n",
  12427. __func__, actual_read_len);
  12428. }
  12429. } else {
  12430. rc = -EINVAL;
  12431. pt_debug(dev, DL_ERROR,
  12432. "%s: Insufficient data provided for PIP2 cmd\n",
  12433. __func__);
  12434. }
  12435. exit:
  12436. if (rc)
  12437. return rc;
  12438. return size;
  12439. }
  12440. /*******************************************************************************
  12441. * FUNCTION: pt_pip2_cmd_rsp_show
  12442. *
  12443. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12444. * response generated after using the store method of the pip2_cmd_rsp
  12445. * sysfs node, are available to be read here.
  12446. *
  12447. * PARAMETERS:
  12448. * *dev - pointer to Device structure
  12449. * *attr - pointer to the device attribute structure
  12450. * *buf - pointer to buffer to print
  12451. ******************************************************************************/
  12452. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12453. struct device_attribute *attr, char *buf)
  12454. {
  12455. struct pt_core_data *cd = dev_get_drvdata(dev);
  12456. int i;
  12457. ssize_t data_len;
  12458. int index;
  12459. mutex_lock(&cd->sysfs_lock);
  12460. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12461. "Status: %d\n", cd->raw_cmd_status);
  12462. if (cd->raw_cmd_status)
  12463. goto error;
  12464. /* Remove the CRC from the length of the response */
  12465. data_len = cd->cmd_rsp_buf_len - 2;
  12466. /* Start printing from the data payload */
  12467. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12468. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12469. "%02X ", cd->cmd_rsp_buf[i]);
  12470. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12471. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12472. "\n(%zd bytes)\n",
  12473. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12474. } else {
  12475. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12476. "\n(%zd bytes)\n", 0);
  12477. }
  12478. error:
  12479. mutex_unlock(&cd->sysfs_lock);
  12480. return index;
  12481. }
  12482. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12483. pt_pip2_cmd_rsp_store);
  12484. /*******************************************************************************
  12485. * FUNCTION: pt_command_store
  12486. *
  12487. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12488. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12489. * TTDL will not parse the command.
  12490. *
  12491. * RETURN: Size of passed in buffer
  12492. *
  12493. * PARAMETERS:
  12494. * *dev - pointer to device structure
  12495. * *attr - pointer to device attributes
  12496. * *buf - pointer to buffer that hold the command parameters
  12497. * size - size of buf
  12498. ******************************************************************************/
  12499. static ssize_t pt_command_store(struct device *dev,
  12500. struct device_attribute *attr, const char *buf, size_t size)
  12501. {
  12502. struct pt_core_data *cd = dev_get_drvdata(dev);
  12503. unsigned short crc;
  12504. u16 actual_read_len;
  12505. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12506. int length;
  12507. int len_field;
  12508. int rc = 0;
  12509. mutex_lock(&cd->sysfs_lock);
  12510. cd->cmd_rsp_buf_len = 0;
  12511. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12512. mutex_unlock(&cd->sysfs_lock);
  12513. length = _pt_ic_parse_input_hex(dev, buf, size,
  12514. input_data, PT_MAX_PIP2_MSG_SIZE);
  12515. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12516. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12517. __func__);
  12518. rc = -EINVAL;
  12519. goto pt_command_store_exit;
  12520. }
  12521. /* PIP2 messages begin with 01 01 */
  12522. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12523. cd->pip2_prot_active = 1;
  12524. /* Override next seq tag with what was sent */
  12525. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12526. /* For PIP2 cmd if length does not include crc, add it */
  12527. len_field = (input_data[3] << 8) | input_data[2];
  12528. if (len_field == length && length <= 254) {
  12529. crc = crc_ccitt_calculate(&input_data[2],
  12530. length - 2);
  12531. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12532. __func__, length, crc);
  12533. input_data[length] = (crc & 0xFF00) >> 8;
  12534. input_data[length + 1] = crc & 0x00FF;
  12535. length = length + 2;
  12536. }
  12537. }
  12538. /* write PIP command to log */
  12539. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12540. pm_runtime_get_sync(dev);
  12541. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12542. length, input_data, &actual_read_len);
  12543. pm_runtime_put(dev);
  12544. mutex_lock(&cd->sysfs_lock);
  12545. cd->raw_cmd_status = rc;
  12546. if (rc) {
  12547. cd->cmd_rsp_buf_len = 0;
  12548. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12549. __func__, buf);
  12550. } else {
  12551. cd->cmd_rsp_buf_len = actual_read_len;
  12552. }
  12553. cd->pip2_prot_active = 0;
  12554. mutex_unlock(&cd->sysfs_lock);
  12555. pt_command_store_exit:
  12556. if (rc)
  12557. return rc;
  12558. return size;
  12559. }
  12560. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12561. /*******************************************************************************
  12562. * FUNCTION: pt_response_show
  12563. *
  12564. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12565. * response generated after using the pt_command_store sysfs node, are
  12566. * available to be read here.
  12567. *
  12568. * PARAMETERS:
  12569. * *dev - pointer to Device structure
  12570. * *attr - pointer to the device attribute structure
  12571. * *buf - pointer to buffer to print
  12572. ******************************************************************************/
  12573. static ssize_t pt_response_show(struct device *dev,
  12574. struct device_attribute *attr, char *buf)
  12575. {
  12576. struct pt_core_data *cd = dev_get_drvdata(dev);
  12577. int i;
  12578. ssize_t num_read;
  12579. int index;
  12580. mutex_lock(&cd->sysfs_lock);
  12581. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12582. "Status: %d\n", cd->raw_cmd_status);
  12583. if (cd->raw_cmd_status)
  12584. goto error;
  12585. num_read = cd->cmd_rsp_buf_len;
  12586. for (i = 0; i < num_read; i++)
  12587. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12588. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12589. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12590. "(%zd bytes)\n", num_read);
  12591. error:
  12592. mutex_unlock(&cd->sysfs_lock);
  12593. return index;
  12594. }
  12595. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12596. /*******************************************************************************
  12597. * FUNCTION: pt_dut_debug_show
  12598. *
  12599. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12600. * are available for the store method.
  12601. *
  12602. * RETURN:
  12603. * 0 = success
  12604. * !0 = failure
  12605. *
  12606. * PARAMETERS:
  12607. * *dev - pointer to device structure
  12608. * *attr - pointer to device attributes
  12609. * *buf - pointer to output buffer
  12610. ******************************************************************************/
  12611. static ssize_t pt_dut_debug_show(struct device *dev,
  12612. struct device_attribute *attr, char *buf)
  12613. {
  12614. ssize_t ret;
  12615. ret = scnprintf(buf, strlen(buf),
  12616. "Status: 0\n"
  12617. "dut_debug sends the following commands to the DUT:\n"
  12618. "%d %s \t- %s\n"
  12619. "%d %s \t- %s\n"
  12620. "%d %s \t- %s\n"
  12621. "%d %s \t- %s\n"
  12622. "%d %s \t- %s\n"
  12623. "%d %s \t- %s\n"
  12624. "%d %s \t- %s\n"
  12625. "%d %s \t- %s\n"
  12626. "%d %s \t- %s\n"
  12627. "%d %s \t- %s\n"
  12628. "%d %s \t- %s\n"
  12629. "%d %s \t- %s\n"
  12630. "%d %s \t- %s\n"
  12631. "%d %s \t- %s\n"
  12632. "%d %s \t- %s\n"
  12633. "%d %s \t- %s\n"
  12634. "%d %s \t- %s\n"
  12635. ,
  12636. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12637. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12638. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12639. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12640. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12641. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12642. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12643. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12644. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12645. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12646. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12647. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12648. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12649. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12650. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12651. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12652. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12653. );
  12654. return ret;
  12655. }
  12656. /*******************************************************************************
  12657. * FUNCTION: pt_drv_debug_show
  12658. *
  12659. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12660. * are available for the store method.
  12661. *
  12662. * RETURN:
  12663. * 0 = success
  12664. * !0 = failure
  12665. *
  12666. * PARAMETERS:
  12667. * *dev - pointer to device structure
  12668. * *attr - pointer to device attributes
  12669. * *buf - pointer to output buffer
  12670. ******************************************************************************/
  12671. static ssize_t pt_drv_debug_show(struct device *dev,
  12672. struct device_attribute *attr, char *buf)
  12673. {
  12674. ssize_t ret;
  12675. ret = scnprintf(buf, strlen(buf),
  12676. "Status: 0\n"
  12677. "drv_debug supports the following values:\n"
  12678. "%d %s \t- %s\n"
  12679. "%d %s \t- %s\n"
  12680. "%d %s \t- %s\n"
  12681. "%d %s \t- %s\n"
  12682. "%d %s \t- %s\n"
  12683. "%d %s \t- %s\n"
  12684. "%d %s \t- %s\n"
  12685. "%d %s \t- %s\n"
  12686. "%d %s \t- %s\n"
  12687. "%d %s - %s\n"
  12688. "%d %s \t- %s\n"
  12689. "%d %s \t- %s\n"
  12690. #ifdef TTDL_DIAGNOSTICS
  12691. "%d %s \t- %s\n"
  12692. "%d %s \t- %s\n"
  12693. "%d %s \t- %s\n"
  12694. #endif /* TTDL_DIAGNOSTICS */
  12695. "%d %s \t- %s\n"
  12696. #ifdef TTDL_DIAGNOSTICS
  12697. "%d %s \t- %s\n"
  12698. "%d %s \t- %s\n"
  12699. "%d %s \t- %s\n"
  12700. "%d %s \t- %s\n"
  12701. "%d %s \t- %s\n"
  12702. "%d %s \t- %s\n"
  12703. "%d %s \t- %s\n"
  12704. "%d %s \t- %s\n"
  12705. "%d %s \t- %s\n"
  12706. #endif /* TTDL_DIAGNOSTICS */
  12707. ,
  12708. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12709. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12710. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12711. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12712. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12713. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12714. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12715. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12716. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12717. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12718. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12719. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12720. #ifdef TTDL_DIAGNOSTICS
  12721. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12722. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12723. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12724. #endif /* TTDL_DIAGNOSTICS */
  12725. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12726. #ifdef TTDL_DIAGNOSTICS
  12727. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12728. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12729. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12730. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12731. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12732. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12733. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12734. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12735. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12736. "TTHE_TUNER HID USB Format"
  12737. #endif /* TTDL_DIAGNOSTICS */
  12738. );
  12739. return ret;
  12740. }
  12741. /*******************************************************************************
  12742. * FUNCTION: pt_drv_debug_store
  12743. *
  12744. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12745. * dut_debug. Drv_debug will contain all functionality that can be run
  12746. * without a DUT preset and is available anytime TTDL is running.
  12747. * Dut_debug requires a DUT to be available and will only be created after
  12748. * a DUT has been detected.
  12749. * This function will eventually be split into two but until the overlap
  12750. * has been depricated this function contains all commands that can be
  12751. * used for TTDL/DUT debugging status and control.
  12752. * All commands require at least one value to be passed in *buf with some
  12753. * requiring two.
  12754. *
  12755. * RETURN: Size of passed in buffer
  12756. *
  12757. * PARAMETERS:
  12758. * *dev - pointer to device structure
  12759. * *attr - pointer to device attributes
  12760. * *buf - pointer to buffer that hold the command parameters
  12761. * size - size of buf
  12762. ******************************************************************************/
  12763. static ssize_t pt_drv_debug_store(struct device *dev,
  12764. struct device_attribute *attr, const char *buf, size_t size)
  12765. {
  12766. struct pt_core_data *cd = dev_get_drvdata(dev);
  12767. unsigned long value;
  12768. int rc = 0;
  12769. u8 return_data[8];
  12770. static u8 wd_disabled;
  12771. u32 input_data[3];
  12772. int length;
  12773. #ifdef TTDL_DIAGNOSTICS
  12774. struct i2c_client *client = to_i2c_client(dev);
  12775. unsigned short crc = 0;
  12776. u16 cal_size;
  12777. #endif
  12778. input_data[0] = 0;
  12779. input_data[1] = 0;
  12780. /* Maximmum input is two elements */
  12781. length = _pt_ic_parse_input(dev, buf, size,
  12782. input_data, ARRAY_SIZE(input_data));
  12783. if (length < 1 || length > 2) {
  12784. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12785. __func__);
  12786. rc = -EINVAL;
  12787. goto pt_drv_debug_store_exit;
  12788. }
  12789. value = input_data[0];
  12790. if (length == 1) {
  12791. pt_debug(dev, DL_DEBUG,
  12792. "%s: Debug Cmd Received (id=%d)\n",
  12793. __func__, input_data[0]);
  12794. } else if (length == 2) {
  12795. pt_debug(dev, DL_DEBUG,
  12796. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12797. __func__, input_data[0], input_data[1]);
  12798. } else {
  12799. pt_debug(dev, DL_DEBUG,
  12800. "%s: Invalid arguments received\n", __func__);
  12801. rc = -EINVAL;
  12802. goto pt_drv_debug_store_exit;
  12803. }
  12804. /* Start watchdog timer command */
  12805. if (value == PT_DRV_DBG_START_WD) {
  12806. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12807. wd_disabled = 0;
  12808. cd->watchdog_force_stop = false;
  12809. pt_start_wd_timer(cd);
  12810. goto pt_drv_debug_store_exit;
  12811. }
  12812. /* Stop watchdog timer temporarily */
  12813. pt_stop_wd_timer(cd);
  12814. if (value == PT_DRV_DBG_STOP_WD) {
  12815. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12816. wd_disabled = 1;
  12817. cd->watchdog_force_stop = true;
  12818. goto pt_drv_debug_store_exit;
  12819. }
  12820. switch (value) {
  12821. case PT_DRV_DBG_SUSPEND: /* 4 */
  12822. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12823. wd_disabled = 1;
  12824. rc = pt_core_suspend_(cd->dev);
  12825. if (rc)
  12826. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12827. __func__, rc);
  12828. else {
  12829. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12830. __func__);
  12831. cd->drv_debug_suspend = true;
  12832. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12833. }
  12834. break;
  12835. case PT_DRV_DBG_RESUME: /* 5 */
  12836. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12837. rc = pt_core_resume_(cd->dev);
  12838. if (rc)
  12839. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12840. __func__, rc);
  12841. else {
  12842. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12843. __func__);
  12844. cd->drv_debug_suspend = false;
  12845. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12846. }
  12847. break;
  12848. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12849. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12850. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12851. break;
  12852. case PT_DUT_DBG_HID_RESET: /* 50 */
  12853. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12854. pt_hid_cmd_reset(cd);
  12855. break;
  12856. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12857. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12858. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12859. wd_disabled = 0;
  12860. break;
  12861. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12862. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12863. __func__);
  12864. wd_disabled = 1;
  12865. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12866. break;
  12867. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12868. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12869. __func__);
  12870. wd_disabled = 1;
  12871. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12872. break;
  12873. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12874. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12875. pt_hid_output_bl_get_information(cd, return_data);
  12876. break;
  12877. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12878. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12879. __func__);
  12880. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12881. break;
  12882. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12883. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12884. pt_hid_output_bl_launch_app(cd);
  12885. break;
  12886. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12887. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12888. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12889. break;
  12890. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12891. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12892. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12893. break;
  12894. case PT_DUT_DBG_RESET: /* 98 */
  12895. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12896. rc = pt_hw_hard_reset(cd);
  12897. break;
  12898. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12899. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12900. pt_pip_null(cd);
  12901. break;
  12902. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12903. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12904. rc = pt_pip_start_bootloader(cd);
  12905. if (!rc) {
  12906. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12907. cd->mode = PT_MODE_BOOTLOADER;
  12908. }
  12909. break;
  12910. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12911. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12912. pt_hid_output_get_sysinfo(cd);
  12913. break;
  12914. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12915. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12916. pt_pip_suspend_scanning(cd);
  12917. break;
  12918. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12919. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12920. pt_pip_resume_scanning(cd);
  12921. break;
  12922. #ifdef TTHE_TUNER_SUPPORT
  12923. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12924. cd->tthe_exit = 1;
  12925. wake_up(&cd->wait_q);
  12926. kfree(cd->tthe_buf);
  12927. cd->tthe_buf = NULL;
  12928. cd->tthe_exit = 0;
  12929. break;
  12930. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12931. if (cd->tthe_buf)
  12932. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12933. else
  12934. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12935. __func__);
  12936. break;
  12937. #endif
  12938. #ifdef TTDL_DIAGNOSTICS
  12939. case PT_DUT_DBG_HID_DESC: /* 109 */
  12940. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12941. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12942. break;
  12943. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12944. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12945. __func__);
  12946. pt_erase_parameter_list(cd);
  12947. break;
  12948. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12949. rc = pt_read_input(cd);
  12950. if (!rc)
  12951. pt_parse_input(cd);
  12952. break;
  12953. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12954. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12955. &cal_size, &crc);
  12956. if (rc)
  12957. pt_debug(dev, DL_ERROR,
  12958. "%s: CAL Data clear failed rc=%d\n",
  12959. __func__, rc);
  12960. else
  12961. pt_debug(dev, DL_INFO,
  12962. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12963. __func__, crc, size);
  12964. break;
  12965. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12966. mutex_lock(&cd->system_lock);
  12967. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12968. cd->debug_level = input_data[1];
  12969. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12970. __func__, cd->debug_level);
  12971. } else {
  12972. rc = -EINVAL;
  12973. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12974. __func__, input_data[1]);
  12975. }
  12976. mutex_unlock(&(cd->system_lock));
  12977. break;
  12978. #endif
  12979. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12980. mutex_lock(&cd->system_lock);
  12981. if (input_data[1] > 100) {
  12982. cd->watchdog_interval = input_data[1];
  12983. pt_debug(dev, DL_INFO,
  12984. "%s: Set watchdog_ interval to: %d\n",
  12985. __func__, cd->watchdog_interval);
  12986. pt_start_wd_timer(cd);
  12987. } else {
  12988. rc = -EINVAL;
  12989. pt_debug(dev, DL_ERROR,
  12990. "%s: Invalid watchdog interval: %d\n",
  12991. __func__, input_data[1]);
  12992. }
  12993. mutex_unlock(&(cd->system_lock));
  12994. break;
  12995. #ifdef TTDL_DIAGNOSTICS
  12996. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12997. mutex_lock(&cd->system_lock);
  12998. if (input_data[1] == 0) {
  12999. cd->show_timestamp = 0;
  13000. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  13001. __func__);
  13002. } else if (input_data[1] == 1) {
  13003. cd->show_timestamp = 1;
  13004. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  13005. __func__);
  13006. } else {
  13007. rc = -EINVAL;
  13008. pt_debug(dev, DL_ERROR,
  13009. "%s: Invalid parameter: %d\n",
  13010. __func__, input_data[1]);
  13011. }
  13012. mutex_unlock(&(cd->system_lock));
  13013. break;
  13014. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  13015. if (input_data[1] == 0) {
  13016. cd->cpdata->setup_power(cd->cpdata,
  13017. PT_MT_POWER_OFF, cd->dev);
  13018. pt_debug(dev, DL_INFO,
  13019. "%s: Initiate Power Off\n", __func__);
  13020. } else if (input_data[1] == 1) {
  13021. cd->cpdata->setup_power(cd->cpdata,
  13022. PT_MT_POWER_ON, cd->dev);
  13023. pt_debug(dev, DL_INFO,
  13024. "%s: Initiate Power On\n", __func__);
  13025. } else {
  13026. rc = -EINVAL;
  13027. pt_debug(dev, DL_ERROR,
  13028. "%s: Invalid parameter: %d\n",
  13029. __func__, input_data[1]);
  13030. }
  13031. break;
  13032. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  13033. if (input_data[1] == 0) {
  13034. pm_runtime_put(dev);
  13035. pt_debug(dev, DL_ERROR,
  13036. "%s: Force call pm_runtime_put()\n", __func__);
  13037. } else if (input_data[1] == 1) {
  13038. pm_runtime_get_sync(dev);
  13039. pt_debug(dev, DL_ERROR,
  13040. "%s: Force call pm_runtime_get_sync()\n",
  13041. __func__);
  13042. } else {
  13043. rc = -EINVAL;
  13044. pt_debug(dev, DL_ERROR,
  13045. "%s: WARNING: Invalid parameter: %d\n",
  13046. __func__, input_data[1]);
  13047. }
  13048. break;
  13049. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  13050. mutex_lock(&cd->system_lock);
  13051. if (input_data[1] == 0) {
  13052. cd->show_tt_data = false;
  13053. pt_debug(dev, DL_INFO,
  13054. "%s: Disable TT_DATA\n", __func__);
  13055. } else if (input_data[1] == 1) {
  13056. cd->show_tt_data = true;
  13057. pt_debug(dev, DL_INFO,
  13058. "%s: Enable TT_DATA\n", __func__);
  13059. } else {
  13060. rc = -EINVAL;
  13061. pt_debug(dev, DL_ERROR,
  13062. "%s: Invalid parameter: %d\n",
  13063. __func__, input_data[1]);
  13064. }
  13065. mutex_unlock(&(cd->system_lock));
  13066. break;
  13067. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  13068. if (input_data[1] == cd->active_dut_generation) {
  13069. mutex_lock(&cd->system_lock);
  13070. cd->set_dut_generation = true;
  13071. mutex_unlock(&(cd->system_lock));
  13072. } else {
  13073. mutex_lock(&cd->system_lock);
  13074. if (input_data[1] == 0) {
  13075. cd->active_dut_generation = DUT_UNKNOWN;
  13076. cd->set_dut_generation = false;
  13077. } else if (input_data[1] == 1) {
  13078. cd->active_dut_generation = DUT_PIP1_ONLY;
  13079. cd->set_dut_generation = true;
  13080. } else if (input_data[1] == 2) {
  13081. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  13082. cd->set_dut_generation = true;
  13083. } else {
  13084. rc = -EINVAL;
  13085. pt_debug(dev, DL_ERROR,
  13086. "%s: Invalid parameter: %d\n",
  13087. __func__, input_data[1]);
  13088. break;
  13089. }
  13090. cd->startup_status = STARTUP_STATUS_START;
  13091. pt_debug(cd->dev, DL_DEBUG,
  13092. "%s: Startup Status Reset\n", __func__);
  13093. mutex_unlock(&(cd->system_lock));
  13094. pt_debug(dev, DL_INFO,
  13095. "%s: Active DUT Generation Set to: %d\n",
  13096. __func__, cd->active_dut_generation);
  13097. /* Changing DUT generations full restart needed */
  13098. pt_queue_restart(cd);
  13099. }
  13100. break;
  13101. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  13102. mutex_lock(&cd->system_lock);
  13103. if (input_data[1] == 0) {
  13104. cd->bridge_mode = false;
  13105. pt_debug(dev, DL_INFO,
  13106. "%s: Disable Bridge Mode\n", __func__);
  13107. } else if (input_data[1] == 1) {
  13108. cd->bridge_mode = true;
  13109. pt_debug(dev, DL_INFO,
  13110. "%s: Enable Bridge Mode\n", __func__);
  13111. } else {
  13112. rc = -EINVAL;
  13113. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13114. __func__, input_data[1]);
  13115. }
  13116. mutex_unlock(&(cd->system_lock));
  13117. break;
  13118. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  13119. mutex_lock(&cd->system_lock);
  13120. /* Only a 7bit address is valid */
  13121. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  13122. client->addr = input_data[1];
  13123. pt_debug(dev, DL_INFO,
  13124. "%s: Set I2C Address: 0x%2X\n",
  13125. __func__, client->addr);
  13126. } else {
  13127. rc = -EINVAL;
  13128. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  13129. __func__, input_data[1]);
  13130. client->addr = 0x24;
  13131. }
  13132. mutex_unlock(&(cd->system_lock));
  13133. break;
  13134. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  13135. mutex_lock(&cd->system_lock);
  13136. if (input_data[1] == 0) {
  13137. cd->flashless_dut = 0;
  13138. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  13139. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  13140. __func__);
  13141. } else if (input_data[1] == 1) {
  13142. cd->flashless_dut = 1;
  13143. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  13144. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13145. __func__);
  13146. }
  13147. mutex_unlock(&(cd->system_lock));
  13148. break;
  13149. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13150. mutex_lock(&cd->system_lock);
  13151. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13152. cd->force_pip2_seq = input_data[1];
  13153. cd->pip2_cmd_tag_seq = input_data[1];
  13154. pt_debug(dev, DL_INFO,
  13155. "%s: Force PIP2 Seq to: 0x%02X\n",
  13156. __func__, input_data[1]);
  13157. } else {
  13158. cd->force_pip2_seq = 0;
  13159. pt_debug(dev, DL_INFO,
  13160. "%s: Clear Forced PIP2 Seq\n", __func__);
  13161. }
  13162. mutex_unlock(&(cd->system_lock));
  13163. break;
  13164. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13165. mutex_lock(&cd->system_lock);
  13166. if (input_data[1] == 0) {
  13167. cd->bl_with_no_int = 0;
  13168. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13169. } else if (input_data[1] == 1) {
  13170. cd->bl_with_no_int = 1;
  13171. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13172. __func__);
  13173. } else {
  13174. rc = -EINVAL;
  13175. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13176. __func__, input_data[1]);
  13177. }
  13178. mutex_unlock(&(cd->system_lock));
  13179. break;
  13180. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13181. mutex_lock(&cd->system_lock);
  13182. if (input_data[1] == 0) {
  13183. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13184. pt_debug(dev, DL_INFO,
  13185. "%s: Disable Calibration cache in host\n",
  13186. __func__);
  13187. } else if (input_data[1] == 1) {
  13188. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13189. pt_debug(dev, DL_INFO,
  13190. "%s: Enable Calibration cache in host\n",
  13191. __func__);
  13192. } else {
  13193. rc = -EINVAL;
  13194. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13195. __func__, input_data[1]);
  13196. }
  13197. mutex_unlock(&(cd->system_lock));
  13198. break;
  13199. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13200. mutex_lock(&cd->system_lock);
  13201. if (input_data[1] == 0) {
  13202. cd->multi_chip = PT_FEATURE_DISABLE;
  13203. cd->ttdl_bist_select = 0x07;
  13204. pt_debug(dev, DL_INFO,
  13205. "%s: Disable Multi-chip support\n", __func__);
  13206. } else if (input_data[1] == 1) {
  13207. cd->multi_chip = PT_FEATURE_ENABLE;
  13208. cd->ttdl_bist_select = 0x3F;
  13209. pt_debug(dev, DL_INFO,
  13210. "%s: Enable Multi-chip support\n", __func__);
  13211. } else {
  13212. rc = -EINVAL;
  13213. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13214. __func__, input_data[1]);
  13215. }
  13216. mutex_unlock(&(cd->system_lock));
  13217. break;
  13218. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13219. mutex_lock(&cd->system_lock);
  13220. if (input_data[1] <= 0x07) {
  13221. cd->panel_id_support = input_data[1];
  13222. pt_debug(dev, DL_INFO,
  13223. "%s: Set panel_id_support to %d\n",
  13224. __func__, cd->panel_id_support);
  13225. } else {
  13226. rc = -EINVAL;
  13227. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13228. __func__, input_data[1]);
  13229. }
  13230. mutex_unlock(&(cd->system_lock));
  13231. break;
  13232. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13233. mutex_lock(&cd->system_lock);
  13234. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13235. /*
  13236. * The timeout is changed for some cases so the
  13237. * pip_cmd_timeout_default is used to retore back to
  13238. * what the user requested as the new timeout.
  13239. */
  13240. cd->pip_cmd_timeout_default = input_data[1];
  13241. cd->pip_cmd_timeout = input_data[1];
  13242. pt_debug(dev, DL_INFO,
  13243. "%s: PIP Timeout = %d\n", __func__,
  13244. cd->pip_cmd_timeout_default);
  13245. } else {
  13246. rc = -EINVAL;
  13247. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13248. __func__, input_data[1]);
  13249. }
  13250. mutex_unlock(&(cd->system_lock));
  13251. break;
  13252. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13253. mutex_lock(&cd->system_lock);
  13254. if (input_data[1] == 0) {
  13255. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13256. pt_debug(dev, DL_INFO,
  13257. "%s: Disable tthe_tuner HID-USB format\n",
  13258. __func__);
  13259. } else if (input_data[1] == 1) {
  13260. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13261. pt_debug(dev, DL_INFO,
  13262. "%s: Enable tthe_tuner HID-USB format\n",
  13263. __func__);
  13264. } else {
  13265. rc = -EINVAL;
  13266. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13267. __func__, input_data[1]);
  13268. }
  13269. mutex_unlock(&(cd->system_lock));
  13270. break;
  13271. #endif /* TTDL_DIAGNOSTICS */
  13272. default:
  13273. rc = -EINVAL;
  13274. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13275. }
  13276. /* Enable watchdog timer */
  13277. if (!wd_disabled)
  13278. pt_start_wd_timer(cd);
  13279. pt_drv_debug_store_exit:
  13280. if (rc)
  13281. return rc;
  13282. return size;
  13283. }
  13284. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13285. pt_drv_debug_store);
  13286. /*******************************************************************************
  13287. * FUNCTION: pt_sleep_status_show
  13288. *
  13289. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13290. * sleep status as either ON or OFF
  13291. *
  13292. * RETURN:
  13293. * 0 = success
  13294. * !0 = failure
  13295. *
  13296. * PARAMETERS:
  13297. * *dev - pointer to device structure
  13298. * *attr - pointer to device attributes
  13299. * *buf - pointer to output buffer
  13300. ******************************************************************************/
  13301. static ssize_t pt_sleep_status_show(struct device *dev,
  13302. struct device_attribute *attr, char *buf)
  13303. {
  13304. struct pt_core_data *cd = dev_get_drvdata(dev);
  13305. ssize_t ret;
  13306. mutex_lock(&cd->system_lock);
  13307. if (cd->sleep_state == SS_SLEEP_ON)
  13308. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13309. else
  13310. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13311. mutex_unlock(&cd->system_lock);
  13312. return ret;
  13313. }
  13314. /*******************************************************************************
  13315. * FUNCTION: pt_panel_id_show
  13316. *
  13317. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13318. * detected panel ID from the DUT
  13319. *
  13320. * RETURN:
  13321. * 0 = success
  13322. * !0 = failure
  13323. *
  13324. * PARAMETERS:
  13325. * *dev - pointer to device structure
  13326. * *attr - pointer to device attributes
  13327. * *buf - pointer to output buffer
  13328. ******************************************************************************/
  13329. static ssize_t pt_panel_id_show(struct device *dev,
  13330. struct device_attribute *attr, char *buf)
  13331. {
  13332. struct pt_core_data *cd = dev_get_drvdata(dev);
  13333. ssize_t ret;
  13334. u8 pid = PANEL_ID_NOT_ENABLED;
  13335. int rc = 0;
  13336. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13337. /*
  13338. * The DUT should report the same panel ID from both the BL and
  13339. * the FW unless the panel_id feature is set to only
  13340. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13341. * to retrieve the panel_id.
  13342. */
  13343. if (cd->mode == PT_MODE_BOOTLOADER) {
  13344. /*
  13345. * Return the stored value if PT_PANEL_ID_BY_BL
  13346. * is not supported while other feature exits.
  13347. */
  13348. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13349. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13350. if (rc) {
  13351. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13352. "Failed to retrieve Panel ID. ",
  13353. "Using cached value\n",
  13354. __func__);
  13355. }
  13356. }
  13357. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13358. if (cd->panel_id_support &
  13359. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13360. /* For all systems sysinfo has the panel_id */
  13361. rc = pt_hid_output_get_sysinfo(cd);
  13362. if (!rc)
  13363. pid =
  13364. cd->sysinfo.sensing_conf_data.panel_id;
  13365. pt_debug(dev, DL_ERROR,
  13366. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13367. __func__, rc, pid);
  13368. }
  13369. } else {
  13370. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13371. __func__);
  13372. rc = -EPERM;
  13373. }
  13374. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13375. if (cd->mode == PT_MODE_BOOTLOADER) {
  13376. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13377. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13378. if (rc) {
  13379. pt_debug(dev, DL_ERROR,
  13380. "%s: BL get panel ID failed rc=%d\n",
  13381. __func__, rc);
  13382. }
  13383. }
  13384. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13385. if (cd->panel_id_support &
  13386. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13387. rc = pt_hid_output_get_sysinfo(cd);
  13388. if (!rc)
  13389. pid =
  13390. cd->sysinfo.sensing_conf_data.panel_id;
  13391. pt_debug(dev, DL_ERROR,
  13392. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13393. __func__, rc, pid);
  13394. }
  13395. } else {
  13396. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13397. __func__);
  13398. rc = -EPERM;
  13399. }
  13400. } else {
  13401. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13402. __func__);
  13403. rc = -EPERM;
  13404. }
  13405. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13406. rc, pid);
  13407. return ret;
  13408. }
  13409. /*******************************************************************************
  13410. * FUNCTION: pt_get_param_store
  13411. *
  13412. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13413. * ID to retrieve with the show method.
  13414. *
  13415. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13416. *
  13417. * RETURN: Size of passed in buffer if successful
  13418. *
  13419. * PARAMETERS:
  13420. * *dev - pointer to device structure
  13421. * *attr - pointer to device attributes
  13422. * *buf - pointer to output buffer
  13423. * size - size of buf
  13424. ******************************************************************************/
  13425. static ssize_t pt_get_param_store(struct device *dev,
  13426. struct device_attribute *attr, const char *buf, size_t size)
  13427. {
  13428. struct pt_core_data *cd = dev_get_drvdata(dev);
  13429. u32 input_data[2];
  13430. int length;
  13431. int rc = 0;
  13432. /* Maximum input of one value */
  13433. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13434. ARRAY_SIZE(input_data));
  13435. if (length != 1) {
  13436. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13437. __func__);
  13438. rc = -EINVAL;
  13439. goto exit;
  13440. }
  13441. mutex_lock(&cd->system_lock);
  13442. cd->get_param_id = input_data[0];
  13443. mutex_unlock(&(cd->system_lock));
  13444. exit:
  13445. if (rc)
  13446. return rc;
  13447. return size;
  13448. }
  13449. /*******************************************************************************
  13450. * FUNCTION: pt_get_param_show
  13451. *
  13452. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13453. * parameter data from the DUT based on the ID stored in the core
  13454. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13455. * communicate or some other error occures, an error status is returned
  13456. * with no value following.
  13457. * Output is in the form:
  13458. * Status: x
  13459. * 0xyyyyyyyy
  13460. * The 32bit data will only follow the status code if the status == 0
  13461. *
  13462. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13463. *
  13464. * RETURN:
  13465. * 0 = success
  13466. * !0 = failure
  13467. *
  13468. * PARAMETERS:
  13469. * *dev - pointer to device structure
  13470. * *attr - pointer to device attributes
  13471. * *buf - pointer to output buffer
  13472. ******************************************************************************/
  13473. static ssize_t pt_get_param_show(struct device *dev,
  13474. struct device_attribute *attr, char *buf)
  13475. {
  13476. struct pt_core_data *cd = dev_get_drvdata(dev);
  13477. ssize_t ret = 0;
  13478. int status;
  13479. u32 value = 0;
  13480. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13481. if (status) {
  13482. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13483. __func__, "pt_get_param", status);
  13484. ret = scnprintf(buf, strlen(buf),
  13485. "%s %d\n",
  13486. "Status:", status);
  13487. } else {
  13488. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13489. __func__, cd->get_param_id, value);
  13490. ret = scnprintf(buf, strlen(buf),
  13491. "Status: %d\n"
  13492. "0x%04X\n",
  13493. status, value);
  13494. }
  13495. return ret;
  13496. }
  13497. /*******************************************************************************
  13498. * FUNCTION: pt_ttdl_restart_show
  13499. *
  13500. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13501. * probed modules, calls startup() and then re-probes modules.
  13502. *
  13503. * RETURN: size of data written to sysfs node
  13504. *
  13505. * PARAMETERS:
  13506. * *dev - pointer to device structure
  13507. * *attr - pointer to device attributes structure
  13508. * *buf - pointer to print output buffer
  13509. ******************************************************************************/
  13510. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13511. struct device_attribute *attr, char *buf)
  13512. {
  13513. struct pt_core_data *cd = dev_get_drvdata(dev);
  13514. int t;
  13515. int rc = 0;
  13516. mutex_lock(&cd->system_lock);
  13517. cd->startup_state = STARTUP_NONE;
  13518. mutex_unlock(&(cd->system_lock));
  13519. /* ensure no left over exclusive access is still locked */
  13520. release_exclusive(cd, cd->dev);
  13521. pt_queue_restart(cd);
  13522. t = wait_event_timeout(cd->wait_q,
  13523. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13524. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13525. if (IS_TMO(t)) {
  13526. pt_debug(cd->dev, DL_ERROR,
  13527. "%s: TMO waiting for FW sentinel\n", __func__);
  13528. rc = -ETIME;
  13529. }
  13530. return scnprintf(buf, strlen(buf),
  13531. "Status: %d\n"
  13532. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13533. }
  13534. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13535. /*******************************************************************************
  13536. * FUNCTION: pt_pip2_gpio_read_show
  13537. *
  13538. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13539. * contents of the response to the passed in output buffer.
  13540. *
  13541. * RETURN: size of data written to sysfs node
  13542. *
  13543. * PARAMETERS:
  13544. * *dev - pointer to device structure
  13545. * *attr - pointer to device attributes structure
  13546. * *buf - pointer to print output buffer
  13547. ******************************************************************************/
  13548. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13549. struct device_attribute *attr, char *buf)
  13550. {
  13551. struct pt_core_data *cd = dev_get_drvdata(dev);
  13552. u8 status = 0;
  13553. u32 gpio_value = 0;
  13554. int rc = 0;
  13555. /* This functionality is only available in the BL */
  13556. if (cd->mode == PT_MODE_BOOTLOADER)
  13557. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13558. else
  13559. rc = -EPERM;
  13560. if (!rc) {
  13561. if (status == 0)
  13562. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13563. "Status: %d\n"
  13564. "DUT GPIO Reg: 0x%08X\n",
  13565. rc, gpio_value);
  13566. else
  13567. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13568. "Status: %d\n"
  13569. "DUT GPIO Reg: n/a\n",
  13570. status);
  13571. } else
  13572. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13573. "Status: %d\n"
  13574. "DUT GPIO Reg: n/a\n",
  13575. rc);
  13576. }
  13577. /*******************************************************************************
  13578. * FUNCTION: pt_pip2_version_show
  13579. *
  13580. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13581. * contents of the response to the passed in output buffer.
  13582. *
  13583. * RETURN: size of data written to sysfs node
  13584. *
  13585. * PARAMETERS:
  13586. * *dev - pointer to device structure
  13587. * *attr - pointer to device attributes structure
  13588. * *buf - pointer to print output buffer
  13589. ******************************************************************************/
  13590. static ssize_t pt_pip2_version_show(struct device *dev,
  13591. struct device_attribute *attr, char *buf)
  13592. {
  13593. int rc = 0;
  13594. struct pt_core_data *cd = dev_get_drvdata(dev);
  13595. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13596. rc = pt_pip2_get_version(cd);
  13597. if (!rc) {
  13598. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13599. "PIP VERSION : %02X.%02X\n"
  13600. "BL VERSION : %02X.%02X\n"
  13601. "FW VERSION : %02X.%02X\n"
  13602. "SILICON ID : %04X.%04X\n"
  13603. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13604. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13605. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13606. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13607. ttdata->chip_id, ttdata->chip_rev,
  13608. ttdata->uid[0], ttdata->uid[1],
  13609. ttdata->uid[2], ttdata->uid[3],
  13610. ttdata->uid[4], ttdata->uid[5],
  13611. ttdata->uid[6], ttdata->uid[7],
  13612. ttdata->uid[8], ttdata->uid[9],
  13613. ttdata->uid[10], ttdata->uid[11]);
  13614. } else {
  13615. pt_debug(dev, DL_ERROR,
  13616. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13617. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13618. "PIP VERSION : -\n"
  13619. "BL VERSION : -\n"
  13620. "FW VERSION : -\n"
  13621. "SILICON ID : -\n"
  13622. "UID : -\n");
  13623. }
  13624. }
  13625. #ifdef TTDL_DIAGNOSTICS
  13626. /*******************************************************************************
  13627. * FUNCTION: pt_ttdl_status_show
  13628. *
  13629. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13630. * variable states and GPIO levels. Additional information printed when
  13631. * TTDL_DIAGNOSTICS is enabled.
  13632. *
  13633. * NOTE: All counters will be reset to 0 when this function is called.
  13634. *
  13635. * RETURN:
  13636. * 0 = success
  13637. * !0 = failure
  13638. *
  13639. * PARAMETERS:
  13640. * *dev - pointer to device structure
  13641. * *attr - pointer to device attributes
  13642. * *buf - pointer to output buffer
  13643. ******************************************************************************/
  13644. static ssize_t pt_ttdl_status_show(struct device *dev,
  13645. struct device_attribute *attr, char *buf)
  13646. {
  13647. struct pt_core_data *cd = dev_get_drvdata(dev);
  13648. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13649. struct i2c_client *client = to_i2c_client(dev);
  13650. ssize_t ret;
  13651. u16 cal_size = 0;
  13652. unsigned short crc = 0;
  13653. if (cd->cal_cache_in_host)
  13654. _pt_manage_local_cal_data(dev,
  13655. PT_CAL_DATA_INFO, &cal_size, &crc);
  13656. ret = scnprintf(buf, strlen(buf),
  13657. "%s: 0x%04X\n"
  13658. "%s: %d\n"
  13659. "%s: %s\n"
  13660. "%s: %s %s\n"
  13661. "%s: %s\n"
  13662. "%s: 0x%02X\n"
  13663. "%s: %s\n"
  13664. "%s: %s\n"
  13665. "%s: %s\n"
  13666. "%s: %s\n"
  13667. "%s: %d\n"
  13668. "%s: %d\n"
  13669. "%s: %s\n"
  13670. "%s: %s\n"
  13671. "%s: %d\n"
  13672. #ifdef TTDL_DIAGNOSTICS
  13673. "%s: %d\n"
  13674. "%s: %d\n"
  13675. "%s: %d\n"
  13676. "%s: %d\n"
  13677. "%s: %d\n"
  13678. "%s: %d\n"
  13679. "%s: %d\n"
  13680. "%s: %d\n"
  13681. "%s: %d\n"
  13682. "%s: %d\n"
  13683. "%s: %s\n"
  13684. "%s: %s\n"
  13685. "%s: %s\n"
  13686. "%s: %d\n"
  13687. "%s: 0x%04X\n"
  13688. "%s: %s\n"
  13689. #endif /* TTDL_DIAGNOSTICS */
  13690. ,
  13691. "Startup Status ", cd->startup_status,
  13692. "TTDL Debug Level ", cd->debug_level,
  13693. "Mode ",
  13694. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13695. "Operational" : "BL") : "Unknown",
  13696. "DUT Generation ",
  13697. cd->active_dut_generation ?
  13698. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13699. "PT TC/TT" : "Gen5/6") : "Unknown",
  13700. cd->active_dut_generation ?
  13701. (cd->set_dut_generation == true ?
  13702. "(Set)" : "(Detected)") : "",
  13703. "HW Detected ",
  13704. cd->hw_detected ? "True" : "False",
  13705. "I2C Address ",
  13706. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13707. "Active Bus Module ",
  13708. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13709. "Flashless Mode ",
  13710. cd->flashless_dut == 1 ? "Yes" : "No",
  13711. "GPIO state - IRQ ",
  13712. cd->cpdata->irq_stat ?
  13713. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13714. "High" : "Low") : "not defined",
  13715. "GPIO state - TP_XRES ",
  13716. pdata->core_pdata->rst_gpio ?
  13717. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13718. "High" : "Low") : "not defined",
  13719. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13720. "Startup Retry Count ", cd->startup_retry_count,
  13721. "WD - Manual Force Stop ",
  13722. cd->watchdog_force_stop ? "True" : "False",
  13723. "WD - Enabled ",
  13724. cd->watchdog_enabled ? "True" : "False",
  13725. "WD - Interval (ms) ", cd->watchdog_interval
  13726. #ifdef TTDL_DIAGNOSTICS
  13727. , "WD - Triggered Count ", cd->watchdog_count,
  13728. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13729. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13730. "WD - XRES Count ", cd->wd_xres_count,
  13731. "IRQ Triggered Count ", cd->irq_count,
  13732. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13733. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13734. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13735. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13736. "Error GPIO trigger type ", cd->err_gpio_type,
  13737. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13738. "Suppress No-Flash Auto BL ",
  13739. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13740. "Yes" : "No",
  13741. "Calibration Cache on host ",
  13742. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13743. "Yes" : "No",
  13744. "Calibration Cache size ", cal_size,
  13745. "Calibration Cache chip ID ", crc,
  13746. "Multi-Chip Support ",
  13747. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13748. #endif /* TTDL_DIAGNOSTICS */
  13749. );
  13750. #ifdef TTDL_DIAGNOSTICS
  13751. /* Reset all diagnostic counters */
  13752. cd->watchdog_count = 0;
  13753. cd->watchdog_irq_stuck_count = 0;
  13754. cd->watchdog_failed_access_count = 0;
  13755. cd->wd_xres_count = 0;
  13756. cd->irq_count = 0;
  13757. cd->bl_retry_packet_count = 0;
  13758. cd->pip2_crc_error_count = 0;
  13759. cd->bus_transmit_error_count = 0;
  13760. #endif
  13761. return ret;
  13762. }
  13763. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13764. /*******************************************************************************
  13765. * FUNCTION: pt_pip2_enter_bl_show
  13766. *
  13767. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13768. * the DUT into the BL and show the success or failure of entering the BL
  13769. *
  13770. * RETURN:
  13771. * 0 = success
  13772. * !0 = failure
  13773. *
  13774. * PARAMETERS:
  13775. * *dev - pointer to device structure
  13776. * *attr - pointer to device attributes
  13777. * *buf - pointer to output buffer
  13778. ******************************************************************************/
  13779. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13780. struct device_attribute *attr, char *buf)
  13781. {
  13782. ssize_t ret = 0;
  13783. int rc = 0;
  13784. int result = 0;
  13785. u8 mode = PT_MODE_UNKNOWN;
  13786. struct pt_core_data *cd = dev_get_drvdata(dev);
  13787. /* Turn off the TTDL WD before enter bootloader */
  13788. pt_stop_wd_timer(cd);
  13789. /* Ensure NO enumeration work is queued or will be queued */
  13790. cancel_work_sync(&cd->enum_work);
  13791. mutex_lock(&cd->system_lock);
  13792. cd->bridge_mode = true;
  13793. mutex_unlock(&cd->system_lock);
  13794. /* set mode to operational to avoid any extra PIP traffic */
  13795. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13796. switch (result) {
  13797. case PT_ENTER_BL_PASS:
  13798. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13799. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13800. break;
  13801. case PT_ENTER_BL_ERROR:
  13802. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13803. rc,
  13804. " Unknown Error");
  13805. break;
  13806. case PT_ENTER_BL_RESET_FAIL:
  13807. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13808. rc,
  13809. " Soft Reset Failed");
  13810. break;
  13811. case PT_ENTER_BL_HID_START_BL_FAIL:
  13812. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13813. rc,
  13814. " PIP Start BL Cmd Failed");
  13815. break;
  13816. case PT_ENTER_BL_CONFIRM_FAIL:
  13817. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13818. rc,
  13819. " Error confirming DUT entered BL");
  13820. break;
  13821. default:
  13822. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13823. rc, " Unknown Error");
  13824. break;
  13825. };
  13826. /* Allow enumeration work to be queued again */
  13827. cd->bridge_mode = false;
  13828. return ret;
  13829. }
  13830. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13831. /*******************************************************************************
  13832. * FUNCTION: pt_pip2_exit_bl_show
  13833. *
  13834. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13835. * launch the APP and put the DUT Application mode
  13836. *
  13837. * RETURN:
  13838. * 0 = success
  13839. * !0 = failure
  13840. *
  13841. * PARAMETERS:
  13842. * *dev - pointer to device structure
  13843. * *attr - pointer to device attributes
  13844. * *buf - pointer to output buffer
  13845. ******************************************************************************/
  13846. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13847. struct device_attribute *attr, char *buf)
  13848. {
  13849. struct pt_core_data *cd = dev_get_drvdata(dev);
  13850. ssize_t ret = 0;
  13851. int rc = 0;
  13852. u8 status_str[PT_STATUS_STR_LEN];
  13853. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13854. /*
  13855. * Perform enum if startup_status doesn't reach to
  13856. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13857. */
  13858. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13859. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13860. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13861. strlcpy(status_str,
  13862. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13863. }
  13864. }
  13865. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13866. rc, status_str);
  13867. return ret;
  13868. }
  13869. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13870. #endif
  13871. #ifdef EASYWAKE_TSG6
  13872. /*******************************************************************************
  13873. * FUNCTION: pt_easy_wakeup_gesture_show
  13874. *
  13875. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13876. * current easy wakeup gesture
  13877. *
  13878. * RETURN:
  13879. * 0 = success
  13880. * !0 = failure
  13881. *
  13882. * PARAMETERS:
  13883. * *dev - pointer to device structure
  13884. * *attr - pointer to device attributes
  13885. * *buf - pointer to output buffer
  13886. ******************************************************************************/
  13887. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13888. struct device_attribute *attr, char *buf)
  13889. {
  13890. struct pt_core_data *cd = dev_get_drvdata(dev);
  13891. ssize_t ret;
  13892. mutex_lock(&cd->system_lock);
  13893. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13894. cd->easy_wakeup_gesture);
  13895. mutex_unlock(&cd->system_lock);
  13896. return ret;
  13897. }
  13898. /*******************************************************************************
  13899. * FUNCTION: pt_easy_wakeup_gesture_store
  13900. *
  13901. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13902. * allows the wake gesture to be set to a custom value.
  13903. *
  13904. * RETURN: Size of passed in buffer is success
  13905. *
  13906. * PARAMETERS:
  13907. * *dev - pointer to device structure
  13908. * *attr - pointer to device attributes
  13909. * *buf - pointer to buffer that hold the command parameters
  13910. * size - size of buf
  13911. ******************************************************************************/
  13912. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13913. struct device_attribute *attr, const char *buf, size_t size)
  13914. {
  13915. struct pt_core_data *cd = dev_get_drvdata(dev);
  13916. u32 input_data[2];
  13917. int length;
  13918. int rc = 0;
  13919. /* Maximum input of one value */
  13920. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13921. ARRAY_SIZE(input_data));
  13922. if (length != 1) {
  13923. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13924. __func__);
  13925. rc = -EINVAL;
  13926. goto exit;
  13927. }
  13928. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13929. __func__, cd->features.easywake);
  13930. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13931. rc = -EINVAL;
  13932. goto exit;
  13933. }
  13934. pm_runtime_get_sync(dev);
  13935. mutex_lock(&cd->system_lock);
  13936. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13937. cd->easy_wakeup_gesture = (u8)input_data[0];
  13938. pt_debug(dev, DL_INFO,
  13939. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13940. __func__, cd->easy_wakeup_gesture);
  13941. } else
  13942. rc = -ENODEV;
  13943. mutex_unlock(&cd->system_lock);
  13944. pm_runtime_put(dev);
  13945. exit:
  13946. if (rc)
  13947. return rc;
  13948. return size;
  13949. }
  13950. /*******************************************************************************
  13951. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13952. *
  13953. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13954. * show the TSG6 easywake gesture ID
  13955. *
  13956. * RETURN:
  13957. * 0 = success
  13958. * !0 = failure
  13959. *
  13960. * PARAMETERS:
  13961. * *dev - pointer to device structure
  13962. * *attr - pointer to device attributes
  13963. * *buf - pointer to output buffer
  13964. ******************************************************************************/
  13965. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13966. struct device_attribute *attr, char *buf)
  13967. {
  13968. struct pt_core_data *cd = dev_get_drvdata(dev);
  13969. ssize_t ret;
  13970. mutex_lock(&cd->system_lock);
  13971. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13972. cd->gesture_id);
  13973. mutex_unlock(&cd->system_lock);
  13974. return ret;
  13975. }
  13976. /*******************************************************************************
  13977. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13978. *
  13979. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13980. * show the TSG6 easywake gesture data in the following format:
  13981. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13982. *
  13983. * RETURN:
  13984. * 0 = success
  13985. * !0 = failure
  13986. *
  13987. * PARAMETERS:
  13988. * *dev - pointer to device structure
  13989. * *attr - pointer to device attributes
  13990. * *buf - pointer to output buffer
  13991. ******************************************************************************/
  13992. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13993. struct device_attribute *attr, char *buf)
  13994. {
  13995. struct pt_core_data *cd = dev_get_drvdata(dev);
  13996. ssize_t ret = 0;
  13997. int i;
  13998. mutex_lock(&cd->system_lock);
  13999. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  14000. for (i = 0; i < cd->gesture_data_length; i++)
  14001. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14002. "0x%02X\n", cd->gesture_data[i]);
  14003. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14004. "(%d bytes)\n", cd->gesture_data_length);
  14005. mutex_unlock(&cd->system_lock);
  14006. return ret;
  14007. }
  14008. #endif /* EASYWAKE_TSG6 */
  14009. #ifdef TTDL_DIAGNOSTICS
  14010. /*******************************************************************************
  14011. * FUNCTION: pt_err_gpio_show
  14012. *
  14013. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  14014. * setting up the gpio was successful
  14015. *
  14016. * RETURN: Char buffer with printed GPIO creation state
  14017. *
  14018. * PARAMETERS:
  14019. * *dev - pointer to device structure
  14020. * *attr - pointer to device attributes
  14021. * *buf - pointer to output buffer
  14022. ******************************************************************************/
  14023. static ssize_t pt_err_gpio_show(struct device *dev,
  14024. struct device_attribute *attr, char *buf)
  14025. {
  14026. struct pt_core_data *cd = dev_get_drvdata(dev);
  14027. return scnprintf(buf, strlen(buf), "Status: 0\n"
  14028. "Err GPIO (%d) : %s\n"
  14029. "Err GPIO trig type: %d\n",
  14030. cd->err_gpio,
  14031. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  14032. "HIGH" : "low") : "not defined"),
  14033. cd->err_gpio_type);
  14034. }
  14035. /*******************************************************************************
  14036. * FUNCTION: pt_err_gpio_store
  14037. *
  14038. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  14039. * available host GPIO to be used to trigger when TTDL detects a PIP
  14040. * command/response timeout.
  14041. *
  14042. * RETURN:
  14043. * 0 = success
  14044. * !0 = failure
  14045. *
  14046. * PARAMETERS:
  14047. * *dev - pointer to device structure
  14048. * *attr - pointer to device attributes
  14049. * *buf - pointer to buffer that hold the command parameters
  14050. * size - size of buf
  14051. ******************************************************************************/
  14052. static ssize_t pt_err_gpio_store(struct device *dev,
  14053. struct device_attribute *attr, const char *buf, size_t size)
  14054. {
  14055. struct pt_core_data *cd = dev_get_drvdata(dev);
  14056. unsigned long gpio;
  14057. int rc = 0;
  14058. u32 input_data[3];
  14059. int length;
  14060. u8 err_type;
  14061. input_data[0] = 0;
  14062. input_data[1] = 0;
  14063. /* Maximmum input is two elements */
  14064. length = _pt_ic_parse_input(dev, buf, size,
  14065. input_data, ARRAY_SIZE(input_data));
  14066. if (length < 1 || length > 2) {
  14067. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14068. __func__);
  14069. rc = -EINVAL;
  14070. goto exit;
  14071. }
  14072. gpio = input_data[0];
  14073. err_type = (u8)input_data[1];
  14074. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  14075. rc = -EINVAL;
  14076. goto exit;
  14077. }
  14078. mutex_lock(&cd->system_lock);
  14079. gpio_free(gpio);
  14080. rc = gpio_request(gpio, NULL);
  14081. if (rc) {
  14082. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  14083. rc = -ENODEV;
  14084. } else {
  14085. cd->err_gpio = gpio;
  14086. cd->err_gpio_type = err_type;
  14087. gpio_direction_output(gpio, 0);
  14088. }
  14089. mutex_unlock(&cd->system_lock);
  14090. exit:
  14091. if (rc)
  14092. return rc;
  14093. return size;
  14094. }
  14095. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  14096. pt_err_gpio_store);
  14097. /*******************************************************************************
  14098. * FUNCTION: pt_drv_irq_show
  14099. *
  14100. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  14101. * TTDL interrupt is enabled/disabled
  14102. *
  14103. * RETURN:
  14104. * 0 = success
  14105. * !0 = failure
  14106. *
  14107. * PARAMETERS:
  14108. * *dev - pointer to device structure
  14109. * *attr - pointer to device attributes
  14110. * *buf - pointer to output buffer
  14111. ******************************************************************************/
  14112. static ssize_t pt_drv_irq_show(struct device *dev,
  14113. struct device_attribute *attr, char *buf)
  14114. {
  14115. struct pt_core_data *cd = dev_get_drvdata(dev);
  14116. ssize_t ret = 0;
  14117. mutex_lock(&cd->system_lock);
  14118. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  14119. if (cd->irq_enabled)
  14120. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14121. "Driver interrupt: ENABLED\n");
  14122. else
  14123. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14124. "Driver interrupt: DISABLED\n");
  14125. mutex_unlock(&cd->system_lock);
  14126. return ret;
  14127. }
  14128. /*******************************************************************************
  14129. * FUNCTION: pt_drv_irq_store
  14130. *
  14131. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  14132. * IRQ to be enabled/disabled.
  14133. *
  14134. * RETURN: Size of passed in buffer
  14135. *
  14136. * PARAMETERS:
  14137. * *dev - pointer to device structure
  14138. * *attr - pointer to device attributes
  14139. * *buf - pointer to buffer that hold the command parameters
  14140. * size - size of buf
  14141. ******************************************************************************/
  14142. static ssize_t pt_drv_irq_store(struct device *dev,
  14143. struct device_attribute *attr, const char *buf, size_t size)
  14144. {
  14145. struct pt_core_data *cd = dev_get_drvdata(dev);
  14146. u32 input_data[2];
  14147. int length;
  14148. int rc = 0;
  14149. /* Maximum input of one value */
  14150. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14151. ARRAY_SIZE(input_data));
  14152. if (length != 1) {
  14153. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14154. __func__);
  14155. rc = -EINVAL;
  14156. goto exit;
  14157. }
  14158. mutex_lock(&cd->system_lock);
  14159. switch (input_data[0]) {
  14160. case 0:
  14161. if (cd->irq_enabled) {
  14162. cd->irq_enabled = false;
  14163. /* Disable IRQ has no return value to check */
  14164. disable_irq_nosync(cd->irq);
  14165. pt_debug(dev, DL_INFO,
  14166. "%s: Driver IRQ now disabled\n",
  14167. __func__);
  14168. } else
  14169. pt_debug(dev, DL_INFO,
  14170. "%s: Driver IRQ already disabled\n",
  14171. __func__);
  14172. break;
  14173. case 1:
  14174. if (cd->irq_enabled == false) {
  14175. cd->irq_enabled = true;
  14176. enable_irq(cd->irq);
  14177. pt_debug(dev, DL_INFO,
  14178. "%s: Driver IRQ now enabled\n",
  14179. __func__);
  14180. } else
  14181. pt_debug(dev, DL_INFO,
  14182. "%s: Driver IRQ already enabled\n",
  14183. __func__);
  14184. break;
  14185. default:
  14186. rc = -EINVAL;
  14187. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14188. }
  14189. mutex_unlock(&(cd->system_lock));
  14190. exit:
  14191. if (rc)
  14192. return rc;
  14193. return size;
  14194. }
  14195. /*******************************************************************************
  14196. * FUNCTION: pt_pip2_bin_hdr_show
  14197. *
  14198. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14199. * the bin file header from flash and show each field
  14200. *
  14201. * RETURN:
  14202. * 0 = success
  14203. * !0 = failure
  14204. *
  14205. * PARAMETERS:
  14206. * *dev - pointer to device structure
  14207. * *attr - pointer to device attributes
  14208. * *buf - pointer to output buffer
  14209. ******************************************************************************/
  14210. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14211. struct device_attribute *attr, char *buf)
  14212. {
  14213. ssize_t ret = 0;
  14214. struct pt_bin_file_hdr hdr = {0};
  14215. int rc;
  14216. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14217. ret = scnprintf(buf, strlen(buf),
  14218. "%s: %d\n"
  14219. "%s: %d\n"
  14220. "%s: 0x%04X\n"
  14221. "%s: %d\n"
  14222. "%s: %d\n"
  14223. "%s: %d\n"
  14224. "%s: 0x%08X\n"
  14225. "%s: 0x%04X\n"
  14226. "%s: 0x%04X\n"
  14227. "%s: %d\n"
  14228. "%s: %d\n",
  14229. "Status", rc,
  14230. "Header Length ", hdr.length,
  14231. "TTPID ", hdr.ttpid,
  14232. "FW Major Ver ", hdr.fw_major,
  14233. "FW Minor Ver ", hdr.fw_minor,
  14234. "FW Rev Control ", hdr.fw_rev_ctrl,
  14235. "FW CRC ", hdr.fw_crc,
  14236. "Silicon Rev ", hdr.si_rev,
  14237. "Silicon ID ", hdr.si_id,
  14238. "Config Ver ", hdr.config_ver,
  14239. "HEX File Size ", hdr.hex_file_size
  14240. );
  14241. return ret;
  14242. }
  14243. /*******************************************************************************
  14244. * FUNCTION: pt_platform_data_show
  14245. *
  14246. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14247. * active platform data including: GPIOs, Vendor and Product IDs,
  14248. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14249. * HID registers, and Easy wake gesture
  14250. *
  14251. * RETURN: Size of printed data
  14252. *
  14253. * PARAMETERS:
  14254. * *dev - pointer to device structure
  14255. * *attr - pointer to device attributes
  14256. * *buf - pointer to output buffer
  14257. ******************************************************************************/
  14258. static ssize_t pt_platform_data_show(struct device *dev,
  14259. struct device_attribute *attr, char *buf)
  14260. {
  14261. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14262. struct pt_core_data *cd = dev_get_drvdata(dev);
  14263. ssize_t ret;
  14264. ret = scnprintf(buf, strlen(buf),
  14265. "%s: %d\n"
  14266. "%s: %d\n"
  14267. "%s: %d\n"
  14268. "%s: %d\n"
  14269. "%s: %d\n"
  14270. "%s: %d\n"
  14271. "%s: %d\n"
  14272. "%s: %d\n"
  14273. "%s: %d\n"
  14274. "%s: %d\n"
  14275. "%s: %d\n"
  14276. "%s: %d\n"
  14277. "%s: %d\n"
  14278. "%s: %d\n"
  14279. "%s: %d\n"
  14280. "%s: %d\n"
  14281. "%s: %d\n"
  14282. "%s: %d\n"
  14283. "%s: %d\n"
  14284. "%s: %d\n"
  14285. "%s: %d\n"
  14286. "%s: %d\n"
  14287. "%s: %d\n"
  14288. "%s: %d\n"
  14289. "%s: %d\n"
  14290. "%s: %d\n"
  14291. "%s: %d\n"
  14292. "%s: %s\n"
  14293. "%s: %s\n"
  14294. "%s: %d\n",
  14295. "Status", 0,
  14296. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14297. "Interrupt GPIO Value ",
  14298. pdata->core_pdata->irq_gpio ?
  14299. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14300. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14301. "Reset GPIO Value ",
  14302. pdata->core_pdata->rst_gpio ?
  14303. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14304. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14305. "DDI Reset GPIO Value ",
  14306. pdata->core_pdata->ddi_rst_gpio ?
  14307. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14308. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14309. "VDDI GPIO Value ",
  14310. pdata->core_pdata->vddi_gpio ?
  14311. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14312. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14313. "VCC GPIO Value ",
  14314. pdata->core_pdata->vcc_gpio ?
  14315. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14316. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14317. "AVDD GPIO Value ",
  14318. pdata->core_pdata->avdd_gpio ?
  14319. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14320. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14321. "AVEE GPIO Value ",
  14322. pdata->core_pdata->avee_gpio ?
  14323. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14324. "Vendor ID ", pdata->core_pdata->vendor_id,
  14325. "Product ID ", pdata->core_pdata->product_id,
  14326. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14327. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14328. "Core data flags ", pdata->core_pdata->flags,
  14329. "MT data flags ", pdata->mt_pdata->flags,
  14330. "Loader data flags ", pdata->loader_pdata->flags,
  14331. "Level trigger delay (us) ",
  14332. pdata->core_pdata->level_irq_udelay,
  14333. "HID Descriptor Register ",
  14334. pdata->core_pdata->hid_desc_register,
  14335. "HID Output Register ",
  14336. cd->hid_desc.output_register,
  14337. "HID Command Register ",
  14338. cd->hid_desc.command_register,
  14339. "Easy wakeup gesture ",
  14340. pdata->core_pdata->easy_wakeup_gesture,
  14341. "Config DUT generation ",
  14342. pdata->core_pdata->config_dut_generation ?
  14343. (pdata->core_pdata->config_dut_generation ==
  14344. CONFIG_DUT_PIP2_CAPABLE ?
  14345. "PT TC/TT" : "Gen5/6") : "Auto",
  14346. "Watchdog Force Stop ",
  14347. pdata->core_pdata->watchdog_force_stop ?
  14348. "True" : "False",
  14349. "Panel ID Support ",
  14350. pdata->core_pdata->panel_id_support);
  14351. return ret;
  14352. }
  14353. #define PT_ERR_STR_SIZE 64
  14354. /*******************************************************************************
  14355. * FUNCTION: pt_bist_bus_test
  14356. *
  14357. * SUMMARY: Tests the connectivity of the active bus pins:
  14358. * I2C - SDA and SCL
  14359. * SPI - MOSI, MISO, CLK
  14360. *
  14361. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14362. * bus. If any data is read we know the I2C/SPI pins are connected
  14363. *
  14364. * RETURN:
  14365. * 0 = Success
  14366. * !0 = Failure
  14367. *
  14368. * PARAMETERS:
  14369. * *dev - pointer to device structure
  14370. * *net_toggled - pointer to where to store if bus toggled
  14371. * *err_str - pointer to error string buffer
  14372. ******************************************************************************/
  14373. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14374. {
  14375. struct pt_core_data *cd = dev_get_drvdata(dev);
  14376. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14377. u8 *read_buf = NULL;
  14378. int bytes_read = 0;
  14379. int rc = 0;
  14380. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14381. if (read_buf == NULL) {
  14382. rc = -ENOMEM;
  14383. goto exit;
  14384. }
  14385. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14386. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14387. disable_irq(cd->irq);
  14388. mutex_lock(&cd->system_lock);
  14389. cd->irq_disabled = true;
  14390. mutex_unlock(&cd->system_lock);
  14391. /*
  14392. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14393. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14394. */
  14395. usleep_range(5000, 6000);
  14396. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14397. __func__, (int)sizeof(ver_cmd));
  14398. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14399. ">>> User CMD");
  14400. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14401. if (rc) {
  14402. pt_debug(dev, DL_ERROR,
  14403. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14404. *net_toggled = 0;
  14405. strlcpy(err_str,
  14406. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14407. goto exit_enable_irq;
  14408. }
  14409. usleep_range(4000, 5000);
  14410. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14411. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14412. __func__, bytes_read);
  14413. if (bytes_read == 0) {
  14414. *net_toggled = 0;
  14415. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14416. __func__);
  14417. strlcpy(err_str,
  14418. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14419. rc = -EIO;
  14420. goto exit_enable_irq;
  14421. } else {
  14422. if (cd->bus_ops->bustype == BUS_I2C)
  14423. *net_toggled = 1;
  14424. else {
  14425. if ((bytes_read > 3) &&
  14426. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14427. PIP2_CMD_ID_VERSION)
  14428. *net_toggled = 1;
  14429. else {
  14430. *net_toggled = 0;
  14431. pt_debug(dev, DL_INFO,
  14432. "%s: BUS Read Failed, %d bytes read\n",
  14433. __func__, bytes_read);
  14434. strlcpy(err_str,
  14435. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14436. }
  14437. }
  14438. }
  14439. exit_enable_irq:
  14440. enable_irq(cd->irq);
  14441. usleep_range(5000, 6000);
  14442. mutex_lock(&cd->system_lock);
  14443. cd->irq_disabled = false;
  14444. mutex_unlock(&cd->system_lock);
  14445. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14446. exit:
  14447. kfree(read_buf);
  14448. return rc;
  14449. }
  14450. /*******************************************************************************
  14451. * FUNCTION: pt_bist_irq_test
  14452. *
  14453. * SUMMARY: Tests the connectivity of the IRQ net
  14454. *
  14455. * This test will ensure there is a good connection between the host
  14456. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14457. * asserted and if so keep reading messages off of the bus until
  14458. * it de-asserts. Possible outcomes:
  14459. * - IRQ was already de-asserted: Send a PIP command and if an
  14460. * interrupt is generated the test passes.
  14461. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14462. * test passes.
  14463. * - IRQ stays asserted: After reading the bus multiple times
  14464. * the IRQ stays asserted. Likely open or shorted to GND
  14465. *
  14466. * RETURN:
  14467. * 0 = Success
  14468. * !0 = Failure
  14469. *
  14470. * PARAMETERS:
  14471. * *dev - pointer to device structure
  14472. * *bus_toggled - pointer to where to store if bus toggled
  14473. * *irq_toggled - pointer to where to store if IRQ toggled
  14474. * *xres_toggled - pointer to where to store if XRES toggled
  14475. * *err_str - pointer to error string buffer
  14476. ******************************************************************************/
  14477. static int pt_bist_irq_test(struct device *dev,
  14478. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14479. {
  14480. struct pt_core_data *cd = dev_get_drvdata(dev);
  14481. u8 *read_buf = NULL;
  14482. u8 mode = PT_MODE_UNKNOWN;
  14483. u16 actual_read_len;
  14484. int bytes_read = 0;
  14485. int count = 0;
  14486. int rc = 0;
  14487. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14488. if (read_buf == NULL) {
  14489. rc = -ENOMEM;
  14490. goto exit;
  14491. }
  14492. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14493. cd->irq_count = 0;
  14494. /*
  14495. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14496. * the bus based on the 2 byte initial length read. Try up to 5x.
  14497. */
  14498. while (pt_check_irq_asserted(cd) && count < 5) {
  14499. count++;
  14500. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14501. }
  14502. if (count > 1 && count < 5 && bytes_read > 0) {
  14503. /*
  14504. * IRQ was stuck but data was successfully read from the
  14505. * bus releasing the IRQ line.
  14506. */
  14507. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14508. __func__, count, bytes_read);
  14509. *bus_toggled = 1;
  14510. *irq_toggled = 1;
  14511. goto exit;
  14512. }
  14513. if (count == 5 && bytes_read == 0) {
  14514. /*
  14515. * Looped 5x and read nothing off the bus yet the IRQ is still
  14516. * asserted. Possible conditions:
  14517. * - IRQ open circuit
  14518. * - IRQ shorted to GND
  14519. * - I2C/SPI bus is disconnected
  14520. * - FW holding the pin low
  14521. * Try entering the BL to see if communication works there.
  14522. */
  14523. mode = PT_MODE_IGNORE;
  14524. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14525. if (rc) {
  14526. pt_debug(dev, DL_ERROR,
  14527. "%s Failed to enter BL\n", __func__);
  14528. strlcpy(err_str,
  14529. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14530. *irq_toggled = 0;
  14531. goto exit;
  14532. }
  14533. /*
  14534. * If original mode was operational and we successfully
  14535. * entered the BL, then the XRES net must have toggled
  14536. */
  14537. if (mode == PT_MODE_OPERATIONAL)
  14538. *xres_toggled = 1;
  14539. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14540. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14541. &actual_read_len);
  14542. if (rc) {
  14543. /*
  14544. * Could not communicate to DUT in BL mode. Save the
  14545. * error string, slim chance but the XRES test below may
  14546. * show the IRQ is actually working.
  14547. */
  14548. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14549. pt_debug(dev, DL_ERROR,
  14550. "%s: %s, count=%d bytes_read=%d\n",
  14551. __func__, err_str, count, bytes_read);
  14552. *irq_toggled = 0;
  14553. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14554. goto exit;
  14555. } else {
  14556. *bus_toggled = 1;
  14557. *irq_toggled = 1;
  14558. goto exit;
  14559. }
  14560. }
  14561. if (pt_check_irq_asserted(cd)) {
  14562. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14563. rc = -EIO;
  14564. *irq_toggled = 0;
  14565. goto exit;
  14566. }
  14567. /* Try sending a PIP command to see if we get a response */
  14568. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14569. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14570. if (rc) {
  14571. /*
  14572. * Potential IRQ issue, no communication in App mode, attempt
  14573. * the same command in the BL
  14574. */
  14575. mode = PT_MODE_IGNORE;
  14576. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14577. if (rc) {
  14578. pt_debug(dev, DL_ERROR,
  14579. "%s Failed to enter BL\n", __func__);
  14580. *irq_toggled = 0;
  14581. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14582. goto exit;
  14583. }
  14584. /*
  14585. * If original mode was operational and we successfully
  14586. * entered the BL, this will be useful info for the tp_xres
  14587. * test below.
  14588. */
  14589. if (mode == PT_MODE_OPERATIONAL)
  14590. *xres_toggled = 1;
  14591. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14592. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14593. &actual_read_len);
  14594. if (rc) {
  14595. /*
  14596. * Could not communicate in FW mode or BL mode. Save the
  14597. * error string, slim chance but the XRES test below may
  14598. * show the IRQ is actually working.
  14599. */
  14600. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14601. pt_debug(dev, DL_ERROR,
  14602. "%s: request_active_pip_prot failed\n",
  14603. __func__);
  14604. *irq_toggled = 0;
  14605. goto exit;
  14606. }
  14607. }
  14608. if (cd->irq_count > 0) {
  14609. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14610. cd->irq_count);
  14611. *bus_toggled = 1;
  14612. *irq_toggled = 1;
  14613. goto exit;
  14614. }
  14615. exit:
  14616. kfree(read_buf);
  14617. return rc;
  14618. }
  14619. /*******************************************************************************
  14620. * FUNCTION: pt_bist_xres_test
  14621. *
  14622. * SUMMARY: Tests the connectivity of the TP_XRES net
  14623. *
  14624. * This test will ensure there is a good connection between the host
  14625. * and the DUT on the tp_xres pin. The pin will be toggled to
  14626. * generate a TP reset which will cause the DUT to output a reset
  14627. * sentinel. If the reset sentinel is seen the test passes. If it is
  14628. * not seen the test will attempt to send a soft reset to simply gain
  14629. * some additional information on the failure:
  14630. * - soft reset fails to send: XRES and IRQ likely open
  14631. * - soft reset passes: XRES likely open or stuck de-asserted
  14632. * - soft reset fails: XRES likely stuck asserted
  14633. *
  14634. * RETURN:
  14635. * 0 = Success
  14636. * !0 = Failure
  14637. *
  14638. * PARAMETERS:
  14639. * *dev - pointer to device structure
  14640. * *bus_toggled - pointer to where to store if bus toggled
  14641. * *irq_toggled - pointer to where to store if IRQ toggled
  14642. * *xres_toggled - pointer to where to store if XRES toggled
  14643. * *err_str - pointer to error string buffer
  14644. ******************************************************************************/
  14645. static int pt_bist_xres_test(struct device *dev,
  14646. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14647. {
  14648. struct pt_core_data *cd = dev_get_drvdata(dev);
  14649. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14650. u8 *read_buf = NULL;
  14651. u8 mode = PT_MODE_UNKNOWN;
  14652. int rc = 0;
  14653. int t = 0;
  14654. int timeout = 300;
  14655. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14656. if (read_buf == NULL) {
  14657. rc = -ENOMEM;
  14658. goto exit;
  14659. }
  14660. /* Clear the startup bit mask, reset and enum will re-populate it */
  14661. cd->startup_status = STARTUP_STATUS_START;
  14662. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14663. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14664. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14665. rc = -ENODEV;
  14666. goto exit;
  14667. }
  14668. /* Ensure we have nothing pending on active bus */
  14669. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14670. /* Perform a hard XRES toggle and wait for reset sentinel */
  14671. mutex_lock(&cd->system_lock);
  14672. cd->hid_reset_cmd_state = 1;
  14673. mutex_unlock(&cd->system_lock);
  14674. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14675. rc = pt_hw_hard_reset(cd);
  14676. /* Set timeout to 1s for the flashless case where a BL could be done */
  14677. if (cd->flashless_dut)
  14678. timeout = 1000;
  14679. /*
  14680. * To avoid the case that next PIP command can be confused by BL/FW
  14681. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14682. * followed by "wakeup event" function can lower the failure rate.
  14683. */
  14684. t = wait_event_timeout(cd->wait_q,
  14685. ((cd->startup_status != STARTUP_STATUS_START)
  14686. && (cd->hid_reset_cmd_state == 0)),
  14687. msecs_to_jiffies(timeout));
  14688. if (IS_TMO(t)) {
  14689. pt_debug(cd->dev, DL_ERROR,
  14690. "%s: TMO waiting for sentinel\n", __func__);
  14691. *xres_toggled = 0;
  14692. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14693. /*
  14694. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14695. * To enter the BL we need to generate an XRES so first try to
  14696. * launch the applicaiton
  14697. */
  14698. if (cd->mode == PT_MODE_BOOTLOADER)
  14699. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14700. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14701. __func__);
  14702. mode = PT_MODE_IGNORE;
  14703. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14704. if (rc) {
  14705. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14706. __func__);
  14707. *xres_toggled = 0;
  14708. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14709. goto exit;
  14710. } else {
  14711. /* Wait for the BL sentinel */
  14712. t = wait_event_timeout(cd->wait_q,
  14713. (cd->startup_status != STARTUP_STATUS_START),
  14714. msecs_to_jiffies(500));
  14715. if (IS_TMO(t)) {
  14716. pt_debug(cd->dev, DL_ERROR,
  14717. "%s: TMO waiting for BL sentinel\n",
  14718. __func__);
  14719. *xres_toggled = 0;
  14720. strlcpy(err_str,
  14721. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14722. rc = -ETIME;
  14723. goto exit;
  14724. }
  14725. }
  14726. }
  14727. mutex_lock(&cd->system_lock);
  14728. cd->hid_reset_cmd_state = 0;
  14729. mutex_unlock(&cd->system_lock);
  14730. /* Look for BL or FW reset sentinels */
  14731. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14732. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14733. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14734. /* If a sentinel was seen, all nets are working */
  14735. *xres_toggled = 1;
  14736. *irq_toggled = 1;
  14737. /*
  14738. * For SPI test, bus read result can be confused as FW sentinel
  14739. * if MISO(slave) is connected to MISO(host).
  14740. */
  14741. if (cd->bus_ops->bustype == BUS_I2C)
  14742. *bus_toggled = 1;
  14743. } else {
  14744. /*
  14745. * Hard reset failed, however some additional information
  14746. * could be determined. Try a soft reset to see if DUT resets
  14747. * with the possible outcomes:
  14748. * - if it resets the line is not stuck asserted
  14749. * - if it does not reset the line could be stuck asserted
  14750. */
  14751. *xres_toggled = 0;
  14752. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14753. msleep(30);
  14754. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14755. __func__, rc);
  14756. if (rc) {
  14757. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14758. pt_debug(dev, DL_ERROR,
  14759. "%s: Hard reset failed, soft reset failed %s\n",
  14760. __func__, err_str);
  14761. goto exit;
  14762. }
  14763. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14764. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14765. strlcpy(err_str,
  14766. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14767. pt_debug(dev, DL_ERROR,
  14768. "%s: Hard reset failed, soft reset passed-%s\n",
  14769. __func__, err_str);
  14770. } else if (cd->startup_status == 0) {
  14771. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14772. pt_debug(dev, DL_ERROR,
  14773. "%s: Hard reset failed, soft reset failed-%s\n",
  14774. __func__, err_str);
  14775. } else {
  14776. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14777. pt_debug(dev, DL_ERROR,
  14778. "%s: Hard and Soft reset failed - %s\n",
  14779. __func__, err_str);
  14780. }
  14781. }
  14782. exit:
  14783. kfree(read_buf);
  14784. return rc;
  14785. }
  14786. /*******************************************************************************
  14787. * FUNCTION: pt_bist_slave_irq_test
  14788. *
  14789. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14790. *
  14791. * This test will ensure there is a good IRQ connection between the master
  14792. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14793. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14794. * saw each slave trigger the IRQ with it's reset sentinel.
  14795. *
  14796. * RETURN:
  14797. * 0 = Success
  14798. * !0 = Failure
  14799. *
  14800. * PARAMETERS:
  14801. * *dev - pointer to device structure
  14802. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14803. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14804. * *err_str - pointer to error string buffer
  14805. * *slave_detect - pointer to slave detect buffer
  14806. * *boot_err - pointer to boot_err buffer
  14807. ******************************************************************************/
  14808. static int pt_bist_slave_irq_test(struct device *dev,
  14809. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14810. u8 *slave_detect, u8 *boot_err)
  14811. {
  14812. struct pt_core_data *cd = dev_get_drvdata(dev);
  14813. u8 mode = PT_MODE_UNKNOWN;
  14814. u8 status;
  14815. u8 boot;
  14816. u8 read_buf[12];
  14817. u8 detected = 0;
  14818. u8 last_err = -1;
  14819. u16 actual_read_len;
  14820. int result = 0;
  14821. int rc = 0;
  14822. /*
  14823. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14824. * detect bits. If the DUT was in FW, entering the BL will cause an
  14825. * XRES signal which will inadvertently test the XRES net as well
  14826. */
  14827. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14828. if (rc) {
  14829. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14830. __func__, rc);
  14831. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14832. goto exit;
  14833. }
  14834. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14835. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14836. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14837. &actual_read_len);
  14838. if (!rc) {
  14839. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14840. "PIP2 STATUS");
  14841. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14842. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14843. /* Slave detect is only valid if status ok and in boot exec */
  14844. if (status == PIP2_RSP_ERR_NONE &&
  14845. boot == PIP2_STATUS_BOOT_EXEC) {
  14846. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14847. SLAVE_DETECT_MASK;
  14848. } else {
  14849. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14850. rc = -EPERM;
  14851. }
  14852. } else {
  14853. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14854. __func__);
  14855. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14856. goto exit;
  14857. }
  14858. /*
  14859. * Retrieve boot error regardless of the state of the slave detect
  14860. * bit because the IRQ could have been stuck high or low.
  14861. */
  14862. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14863. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14864. read_buf, &actual_read_len);
  14865. if (!rc) {
  14866. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14867. "PIP2 GET_LAST_ERRNO");
  14868. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14869. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14870. if (last_err) {
  14871. pt_debug(cd->dev, DL_ERROR,
  14872. "%s: Master Boot Last Err = 0x%02X\n",
  14873. __func__, last_err);
  14874. }
  14875. } else {
  14876. pt_debug(cd->dev, DL_ERROR,
  14877. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14878. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14879. }
  14880. exit:
  14881. pt_debug(cd->dev, DL_INFO,
  14882. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14883. __func__, rc, detected, last_err);
  14884. if (err_str && last_err) {
  14885. if (detected)
  14886. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14887. "- Likely stuck low. Boot Error:",
  14888. last_err);
  14889. else
  14890. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14891. "- Likely stuck high. Boot Error:",
  14892. last_err);
  14893. }
  14894. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14895. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14896. last_err = PIP2_RSP_ERR_NONE;
  14897. if (slave_irq_toggled)
  14898. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14899. if (slave_bus_toggled) {
  14900. /* Leave as UNTEST if slave not detected */
  14901. if (detected)
  14902. *slave_bus_toggled = !last_err ? true : false;
  14903. }
  14904. if (slave_detect)
  14905. *slave_detect = detected;
  14906. if (boot_err)
  14907. *boot_err = last_err;
  14908. if (slave_irq_toggled && slave_bus_toggled)
  14909. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14910. __func__,
  14911. "Detected", detected,
  14912. "slave_irq_toggled", *slave_irq_toggled,
  14913. "slave_bus_toggled", *slave_bus_toggled);
  14914. return rc;
  14915. }
  14916. /*******************************************************************************
  14917. * FUNCTION: pt_bist_slave_xres_test
  14918. *
  14919. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14920. *
  14921. * This test will ensure there is a good TP_XRES connection between the
  14922. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14923. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14924. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14925. * it's reset sentinel.
  14926. *
  14927. * RETURN:
  14928. * 0 = Success
  14929. * !0 = Failure
  14930. *
  14931. * PARAMETERS:
  14932. * *dev - pointer to device structure
  14933. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14934. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14935. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14936. * *err_str - pointer to error string buffer
  14937. ******************************************************************************/
  14938. static int pt_bist_slave_xres_test(struct device *dev,
  14939. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14940. u8 *err_str)
  14941. {
  14942. struct pt_core_data *cd = dev_get_drvdata(dev);
  14943. u8 slave_detect = 0;
  14944. u8 boot_err = 0;
  14945. int rc = 0;
  14946. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14947. mutex_lock(&cd->system_lock);
  14948. cd->hid_reset_cmd_state = 1;
  14949. mutex_unlock(&cd->system_lock);
  14950. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14951. pt_hw_hard_reset(cd);
  14952. msleep(100);
  14953. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14954. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14955. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14956. if (!rc && *slave_irq_toggled == false) {
  14957. /*
  14958. * If the slave IRQ did not toggle, either the slave_detect
  14959. * bit was not set or we had a boot error. If the slave
  14960. * detect was not set the slave did not reset causing a boot
  14961. * error.
  14962. */
  14963. if (!slave_detect)
  14964. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14965. else
  14966. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14967. "- likely open or an IRQ issue. Boot Error:",
  14968. boot_err);
  14969. }
  14970. if (slave_xres_toggled) {
  14971. if (!rc)
  14972. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14973. else
  14974. *slave_xres_toggled = false;
  14975. }
  14976. return rc;
  14977. }
  14978. /*******************************************************************************
  14979. * FUNCTION: pt_bist_slave_bus_test
  14980. *
  14981. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14982. *
  14983. * This test will ensure a good SPI bus connection between the
  14984. * master DUT and the slave DUT(s). This bus connection is ensured by
  14985. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14986. * then there is a master/slave communication issue. Opening file 0 on
  14987. * the master will open it on the slave as well if the slave was detected.
  14988. *
  14989. * RETURN:
  14990. * 0 = Success
  14991. * !0 = Failure
  14992. *
  14993. * PARAMETERS:
  14994. * *dev - pointer to device structure
  14995. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14996. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14997. * *err_str - pointer to error string buffer
  14998. ******************************************************************************/
  14999. static int pt_bist_slave_bus_test(struct device *dev,
  15000. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  15001. {
  15002. struct pt_core_data *cd = dev_get_drvdata(dev);
  15003. u8 mode = PT_MODE_UNKNOWN;
  15004. u8 bus_toggled = false;
  15005. u8 file_handle;
  15006. int result = 0;
  15007. int rc = 0;
  15008. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  15009. if (rc) {
  15010. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  15011. __func__, rc);
  15012. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  15013. goto exit;
  15014. }
  15015. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  15016. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  15017. if (file_handle != PIP2_RAM_FILE) {
  15018. rc = -ENOENT;
  15019. bus_toggled = false;
  15020. pt_debug(dev, DL_ERROR,
  15021. "%s Failed to open bin file\n", __func__);
  15022. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  15023. goto exit;
  15024. } else {
  15025. bus_toggled = true;
  15026. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  15027. pt_debug(dev, DL_ERROR,
  15028. "%s: File Close failed, file_handle=%d\n",
  15029. __func__, file_handle);
  15030. }
  15031. }
  15032. exit:
  15033. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  15034. if (slave_irq_toggled)
  15035. *slave_irq_toggled = bus_toggled;
  15036. if (slave_bus_toggled)
  15037. *slave_bus_toggled = bus_toggled;
  15038. return rc;
  15039. }
  15040. /*******************************************************************************
  15041. * FUNCTION: pt_ttdl_bist_show
  15042. *
  15043. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  15044. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  15045. *
  15046. * NOTE: This function will reset the DUT and the startup_status bit
  15047. * mask. A pt_enum will be queued after completion.
  15048. *
  15049. * NOTE: The order of the net tests is done to optimize the time it takes
  15050. * to run. The first test is capable of verifying all nets, each subsequent
  15051. * test is only run if the previous was not able to see all nets toggle.
  15052. *
  15053. * RETURN:
  15054. * 0 = success
  15055. * !0 = failure
  15056. *
  15057. * PARAMETERS:
  15058. * *dev - pointer to device structure
  15059. * *attr - pointer to device attributes
  15060. * *buf - pointer to output buffer
  15061. ******************************************************************************/
  15062. static ssize_t pt_ttdl_bist_show(struct device *dev,
  15063. struct device_attribute *attr, char *buf)
  15064. {
  15065. struct pt_core_data *cd = dev_get_drvdata(dev);
  15066. ssize_t ret;
  15067. char *bus_err_str = NULL;
  15068. char *irq_err_str = NULL;
  15069. char *xres_err_str = NULL;
  15070. char *slave_bus_err_str = NULL;
  15071. char *slave_irq_err_str = NULL;
  15072. char *slave_xres_err_str = NULL;
  15073. u8 tests;
  15074. int rc = 0;
  15075. int num_tests = 0;
  15076. int status = 1; /* 0 = Pass, !0 = fail */
  15077. u8 bus_toggled = 0x0F; /* default to untested */
  15078. u8 i2c_toggled = 0x0F; /* default to untested */
  15079. u8 spi_toggled = 0x0F; /* default to untested */
  15080. u8 irq_toggled = 0x0F; /* default to untested */
  15081. u8 xres_toggled = 0x0F; /* default to untested */
  15082. u8 slave_bus_toggled = 0x0F; /* default to untested */
  15083. u8 slave_irq_toggled = 0x0F; /* default to untested */
  15084. u8 slave_xres_toggled = 0x0F; /* default to untested */
  15085. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15086. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15087. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15088. if (!bus_err_str || !irq_err_str || !xres_err_str)
  15089. goto print_results;
  15090. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15091. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15092. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15093. if (cd->multi_chip) {
  15094. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15095. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15096. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15097. if (!slave_bus_err_str ||
  15098. !slave_irq_err_str ||
  15099. !slave_xres_err_str)
  15100. goto print_results;
  15101. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15102. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15103. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15104. }
  15105. /* Turn off the TTDL WD during the test */
  15106. pt_stop_wd_timer(cd);
  15107. /* Shorten default PIP cmd timeout while running BIST */
  15108. cd->pip_cmd_timeout = 200;
  15109. /* Count the number of tests to run */
  15110. tests = cd->ttdl_bist_select;
  15111. while (tests) {
  15112. num_tests += tests & 1;
  15113. tests >>= 1;
  15114. }
  15115. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  15116. __func__, cd->ttdl_bist_select, num_tests);
  15117. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  15118. if (cd->flashless_dut) {
  15119. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  15120. __func__);
  15121. mutex_lock(&cd->system_lock);
  15122. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15123. mutex_unlock(&cd->system_lock);
  15124. }
  15125. /* --------------- TP_XRES BIST TEST --------------- */
  15126. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  15127. pt_debug(dev, DL_INFO,
  15128. "%s: ----- Start TP_XRES BIST -----", __func__);
  15129. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  15130. &xres_toggled, xres_err_str);
  15131. /* Done if the rest of all nets toggled */
  15132. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  15133. goto host_nets_complete;
  15134. }
  15135. /* Flush bus in case a PIP response is waiting from previous test */
  15136. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15137. /* --------------- IRQ BIST TEST --------------- */
  15138. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  15139. pt_debug(dev, DL_INFO,
  15140. "%s: ----- Start IRQ BIST -----", __func__);
  15141. bus_toggled = 0xFF;
  15142. irq_toggled = 0xFF;
  15143. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  15144. &xres_toggled, irq_err_str);
  15145. /* If this net failed clear results from previous net */
  15146. if (irq_toggled != 1) {
  15147. xres_toggled = 0x0F;
  15148. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15149. }
  15150. if (bus_toggled == 1 && irq_toggled == 1)
  15151. goto host_nets_complete;
  15152. }
  15153. /* Flush bus in case a PIP response is waiting from previous test */
  15154. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15155. /* --------------- BUS BIST TEST --------------- */
  15156. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15157. pt_debug(dev, DL_INFO,
  15158. "%s: ----- Start BUS BIST -----", __func__);
  15159. bus_toggled = 0xFF;
  15160. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15161. /* If this net failed clear results from previous net */
  15162. if (bus_toggled == 0) {
  15163. irq_toggled = 0x0F;
  15164. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15165. }
  15166. }
  15167. host_nets_complete:
  15168. /* --------------- SLAVE XRES BIST TEST --------------- */
  15169. if (cd->multi_chip &&
  15170. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15171. pt_debug(dev, DL_INFO,
  15172. "%s: ----- Start Slave XRES BIST -----", __func__);
  15173. slave_xres_toggled = 0xFF;
  15174. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15175. &slave_bus_toggled, &slave_xres_toggled,
  15176. slave_xres_err_str);
  15177. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15178. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15179. goto print_results;
  15180. }
  15181. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15182. if (cd->multi_chip &&
  15183. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15184. pt_debug(dev, DL_INFO,
  15185. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15186. slave_irq_toggled = 0xFF;
  15187. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15188. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15189. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15190. __func__, slave_irq_toggled);
  15191. if (slave_irq_toggled == 1) {
  15192. slave_bus_toggled = 1;
  15193. goto print_results;
  15194. }
  15195. }
  15196. /* --------------- SLAVE BUS BIST TEST --------------- */
  15197. if (cd->multi_chip &&
  15198. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15199. pt_debug(dev, DL_INFO,
  15200. "%s: ----- Start Slave BUS BIST -----", __func__);
  15201. slave_bus_toggled = 0xFF;
  15202. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15203. &slave_bus_toggled, slave_bus_err_str);
  15204. }
  15205. print_results:
  15206. /* Restore PIP command timeout */
  15207. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15208. /*
  15209. * We're done! - Perform a hard XRES toggle, allowing BL
  15210. * to load FW if there is any in Flash
  15211. */
  15212. mutex_lock(&cd->system_lock);
  15213. cd->hid_reset_cmd_state = 0;
  15214. mutex_unlock(&cd->system_lock);
  15215. pt_debug(dev, DL_INFO,
  15216. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15217. if (cd->flashless_dut) {
  15218. /*
  15219. * For Flashless solution, FW update is triggered after BL is
  15220. * seen that several miliseconds delay is needed.
  15221. */
  15222. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15223. __func__);
  15224. mutex_lock(&cd->system_lock);
  15225. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15226. mutex_unlock(&cd->system_lock);
  15227. /* Reset DUT and wait 100ms to see if loader started */
  15228. pt_hw_hard_reset(cd);
  15229. msleep(100);
  15230. if (cd->fw_updating) {
  15231. pt_debug(dev, DL_INFO,
  15232. "%s: ----- BIST Wait FW Loading ----",
  15233. __func__);
  15234. rc = _pt_request_wait_for_enum_state(
  15235. dev, 4000, STARTUP_STATUS_COMPLETE);
  15236. }
  15237. } else {
  15238. if (cd->mode == PT_MODE_BOOTLOADER) {
  15239. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15240. if (rc) {
  15241. pt_debug(dev, DL_ERROR,
  15242. "%s Failed to launch app\n", __func__);
  15243. rc = pt_hw_hard_reset(cd);
  15244. }
  15245. }
  15246. /*
  15247. * If FW exists the BL may have just started or will start soon,
  15248. * so the FW sentinel may be on it's way but with no FW it will
  15249. * not arrive, wait for it before deciding if we need to queue
  15250. * an enum.
  15251. */
  15252. rc = _pt_request_wait_for_enum_state(
  15253. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15254. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15255. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15256. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15257. __func__);
  15258. pt_queue_enum(cd);
  15259. rc = _pt_request_wait_for_enum_state(
  15260. dev, 2000, STARTUP_STATUS_COMPLETE);
  15261. }
  15262. }
  15263. msleep(20);
  15264. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15265. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15266. pt_start_wd_timer(cd);
  15267. /* Canned print if any memory allocation issues */
  15268. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15269. ret = scnprintf(buf, strlen(buf),
  15270. "Status: %d\n"
  15271. "I2C (SDA,SCL): [UNTEST]\n"
  15272. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15273. "IRQ connection: [UNTEST]\n"
  15274. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15275. if (cd->multi_chip) {
  15276. ret += scnprintf(buf + ret, strlen(buf),
  15277. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15278. "I/P IRQ connection: [UNTEST]\n"
  15279. "I/P TP_XRES connection: [UNTEST]\n");
  15280. }
  15281. } else {
  15282. status = 0;
  15283. if (bus_toggled == 1)
  15284. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15285. if (irq_toggled == 1)
  15286. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15287. if (xres_toggled == 1)
  15288. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15289. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15290. status += bus_toggled;
  15291. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15292. status += irq_toggled;
  15293. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15294. status += xres_toggled;
  15295. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15296. __func__, status, bus_toggled, irq_toggled,
  15297. xres_toggled);
  15298. if (cd->multi_chip) {
  15299. if (slave_irq_toggled == 1)
  15300. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15301. if (slave_xres_toggled == 1)
  15302. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15303. if (slave_bus_toggled == 1)
  15304. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15305. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15306. status += slave_bus_toggled;
  15307. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15308. status += slave_irq_toggled;
  15309. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15310. status += slave_xres_toggled;
  15311. pt_debug(dev, DL_ERROR,
  15312. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15313. __func__, status, bus_toggled, irq_toggled,
  15314. xres_toggled, slave_bus_toggled,
  15315. slave_irq_toggled, slave_xres_toggled);
  15316. }
  15317. if (cd->bus_ops->bustype == BUS_I2C)
  15318. i2c_toggled = bus_toggled;
  15319. else
  15320. spi_toggled = bus_toggled;
  15321. ret = scnprintf(buf, strlen(buf),
  15322. "Status: %d\n"
  15323. "I2C (SDA,SCL): %s %s\n"
  15324. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15325. "IRQ connection: %s %s\n"
  15326. "TP_XRES connection: %s %s\n",
  15327. status == num_tests ? 0 : 1,
  15328. i2c_toggled == 0x0F ? "[UNTEST]" :
  15329. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15330. i2c_toggled == 0x0F ? "" : bus_err_str,
  15331. spi_toggled == 0x0F ? "[UNTEST]" :
  15332. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15333. spi_toggled == 0x0F ? "" : bus_err_str,
  15334. irq_toggled == 0x0F ? "[UNTEST]" :
  15335. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15336. irq_err_str,
  15337. xres_toggled == 0x0F ? "[UNTEST]" :
  15338. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15339. xres_err_str);
  15340. if (cd->multi_chip) {
  15341. ret += scnprintf(buf + ret, strlen(buf),
  15342. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15343. "I/P IRQ connection: %s %s\n"
  15344. "I/P TP_XRES connection: %s %s\n",
  15345. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15346. slave_bus_toggled == 1 ? "[ OK ]" :
  15347. "[FAILED]", slave_bus_err_str,
  15348. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15349. slave_irq_toggled == 1 ? "[ OK ]" :
  15350. "[FAILED]", slave_irq_err_str,
  15351. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15352. slave_xres_toggled == 1 ? "[ OK ]" :
  15353. "[FAILED]", slave_xres_err_str);
  15354. }
  15355. }
  15356. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15357. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15358. __func__, cd->startup_status);
  15359. kfree(bus_err_str);
  15360. kfree(irq_err_str);
  15361. kfree(xres_err_str);
  15362. return ret;
  15363. }
  15364. /*******************************************************************************
  15365. * FUNCTION: pt_ttdl_bist_store
  15366. *
  15367. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15368. *
  15369. * RETURN: Size of passed in buffer if successful
  15370. *
  15371. * PARAMETERS:
  15372. * *dev - pointer to device structure
  15373. * *attr - pointer to device attributes
  15374. * *buf - pointer to command buffer
  15375. * size - size of buf
  15376. ******************************************************************************/
  15377. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15378. struct device_attribute *attr, const char *buf, size_t size)
  15379. {
  15380. struct pt_core_data *cd = dev_get_drvdata(dev);
  15381. u32 input_data[2] = {0};
  15382. int length;
  15383. int rc = 0;
  15384. /* Maximum input of one value */
  15385. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15386. ARRAY_SIZE(input_data));
  15387. if (length != 1) {
  15388. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15389. __func__);
  15390. rc = -EINVAL;
  15391. goto exit;
  15392. } else {
  15393. mutex_lock(&cd->system_lock);
  15394. cd->ttdl_bist_select = input_data[0];
  15395. mutex_unlock(&cd->system_lock);
  15396. }
  15397. exit:
  15398. if (rc)
  15399. return rc;
  15400. return size;
  15401. }
  15402. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15403. pt_ttdl_bist_store);
  15404. /*******************************************************************************
  15405. * FUNCTION: pt_flush_bus_store
  15406. *
  15407. * SUMMARY: Store method for the flush_bus sysfs node.
  15408. *
  15409. * RETURN: Size of passed in buffer if successful
  15410. *
  15411. * PARAMETERS:
  15412. * *dev - pointer to device structure
  15413. * *attr - pointer to device attributes
  15414. * *buf - pointer to command buffer
  15415. * size - size of buf
  15416. ******************************************************************************/
  15417. static ssize_t pt_flush_bus_store(struct device *dev,
  15418. struct device_attribute *attr, const char *buf, size_t size)
  15419. {
  15420. struct pt_core_data *cd = dev_get_drvdata(dev);
  15421. u32 input_data[2] = {0};
  15422. int length;
  15423. int rc = 0;
  15424. /* Maximum input of one value */
  15425. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15426. ARRAY_SIZE(input_data));
  15427. if (length != 1) {
  15428. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15429. __func__);
  15430. rc = -EINVAL;
  15431. goto exit;
  15432. }
  15433. mutex_lock(&cd->system_lock);
  15434. if (input_data[0] == 0)
  15435. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15436. else if (input_data[0] == 1)
  15437. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15438. else
  15439. rc = -EINVAL;
  15440. mutex_unlock(&cd->system_lock);
  15441. exit:
  15442. if (rc)
  15443. return rc;
  15444. return size;
  15445. }
  15446. /*******************************************************************************
  15447. * FUNCTION: pt_flush_bus_show
  15448. *
  15449. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15450. * based on either the size of the first two bytes or a blind 256 bytes.
  15451. *
  15452. * RETURN:
  15453. * 0 = success
  15454. * !0 = failure
  15455. *
  15456. * PARAMETERS:
  15457. * *dev - pointer to device structure
  15458. * *attr - pointer to device attributes
  15459. * *buf - pointer to output buffer
  15460. ******************************************************************************/
  15461. static ssize_t pt_flush_bus_show(struct device *dev,
  15462. struct device_attribute *attr, char *buf)
  15463. {
  15464. ssize_t ret = 0;
  15465. ssize_t bytes = 0;
  15466. struct pt_core_data *cd = dev_get_drvdata(dev);
  15467. mutex_lock(&cd->system_lock);
  15468. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15469. ret = scnprintf(buf, strlen(buf),
  15470. "Status: 0\n"
  15471. "%s: %zd\n",
  15472. "Bytes flushed", bytes);
  15473. mutex_unlock(&cd->system_lock);
  15474. return ret;
  15475. }
  15476. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15477. pt_flush_bus_store);
  15478. /*******************************************************************************
  15479. * FUNCTION: pt_pip2_ping_test_store
  15480. *
  15481. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15482. *
  15483. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15484. * someone may want to test sending invalid packet lengths so any values
  15485. * up to 255 are allowed.
  15486. *
  15487. * RETURN: Size of passed in buffer if successful
  15488. *
  15489. * PARAMETERS:
  15490. * *dev - pointer to device structure
  15491. * *attr - pointer to device attributes
  15492. * *buf - pointer to command buffer
  15493. * size - size of buf
  15494. ******************************************************************************/
  15495. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15496. struct device_attribute *attr, const char *buf, size_t size)
  15497. {
  15498. struct pt_core_data *cd = dev_get_drvdata(dev);
  15499. u32 input_data[2];
  15500. int length;
  15501. int rc = 0;
  15502. /* Maximum input of one value */
  15503. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15504. ARRAY_SIZE(input_data));
  15505. if (length != 1) {
  15506. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15507. __func__);
  15508. rc = -EINVAL;
  15509. goto exit;
  15510. }
  15511. mutex_lock(&cd->system_lock);
  15512. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15513. cd->ping_test_size = input_data[0];
  15514. else
  15515. rc = -EINVAL;
  15516. mutex_unlock(&cd->system_lock);
  15517. exit:
  15518. if (rc)
  15519. return rc;
  15520. return size;
  15521. }
  15522. /*******************************************************************************
  15523. * FUNCTION: pt_ping_test_show
  15524. *
  15525. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15526. * PING command and ramps up the optional payload from 0 to
  15527. * ping_test_size.
  15528. * The max payload size is 247:
  15529. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15530. *
  15531. * RETURN:
  15532. * 0 = success
  15533. * !0 = failure
  15534. *
  15535. * PARAMETERS:
  15536. * *dev - pointer to device structure
  15537. * *attr - pointer to device attributes
  15538. * *buf - pointer to output buffer
  15539. ******************************************************************************/
  15540. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15541. struct device_attribute *attr, char *buf)
  15542. {
  15543. struct pt_core_data *cd = dev_get_drvdata(dev);
  15544. ssize_t ret;
  15545. int last_packet_size;
  15546. int rc = 0;
  15547. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15548. if (rc) {
  15549. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15550. return ret;
  15551. }
  15552. ret = scnprintf(buf, strlen(buf),
  15553. "Status: %d\n"
  15554. "PING payload test passed with packet sizes 0 - %d\n",
  15555. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15556. last_packet_size);
  15557. return ret;
  15558. }
  15559. /*******************************************************************************
  15560. * FUNCTION: pt_t_refresh_store
  15561. *
  15562. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15563. * in integer as the number of interrupts to count. A timer is started to
  15564. * calculate the total time it takes to see that number of interrupts.
  15565. *
  15566. * RETURN: Size of passed in buffer if successful
  15567. *
  15568. * PARAMETERS:
  15569. * *dev - pointer to device structure
  15570. * *attr - pointer to device attributes
  15571. * *buf - pointer to buffer that hold the command parameters
  15572. * size - size of buf
  15573. ******************************************************************************/
  15574. static ssize_t pt_t_refresh_store(struct device *dev,
  15575. struct device_attribute *attr, const char *buf, size_t size)
  15576. {
  15577. struct pt_core_data *cd = dev_get_drvdata(dev);
  15578. u32 input_data[2];
  15579. int length;
  15580. int rc = 0;
  15581. /* Maximum input of one value */
  15582. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15583. ARRAY_SIZE(input_data));
  15584. if (length != 1) {
  15585. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15586. __func__);
  15587. rc = -EINVAL;
  15588. goto exit;
  15589. }
  15590. mutex_lock(&cd->system_lock);
  15591. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15592. input_data[0]);
  15593. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15594. cd->t_refresh_total = input_data[0];
  15595. cd->t_refresh_count = 0;
  15596. cd->t_refresh_active = 1;
  15597. } else {
  15598. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15599. rc = -EINVAL;
  15600. }
  15601. mutex_unlock(&cd->system_lock);
  15602. exit:
  15603. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15604. if (rc)
  15605. return rc;
  15606. return size;
  15607. }
  15608. /*******************************************************************************
  15609. * FUNCTION: pt_t_refresh_show
  15610. *
  15611. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15612. * of the T-Refresh timer counting the time it takes to see a user defined
  15613. * number of interrupts.
  15614. *
  15615. * RETURN:
  15616. * 0 = success
  15617. * !0 = failure
  15618. *
  15619. * PARAMETERS:
  15620. * *dev - pointer to device structure
  15621. * *attr - pointer to device attributes
  15622. * *buf - pointer to output buffer
  15623. ******************************************************************************/
  15624. static ssize_t pt_t_refresh_show(struct device *dev,
  15625. struct device_attribute *attr, char *buf)
  15626. {
  15627. ssize_t ret = 0;
  15628. struct pt_core_data *cd = dev_get_drvdata(dev);
  15629. u32 whole;
  15630. u16 fraction;
  15631. mutex_lock(&cd->system_lock);
  15632. /* Check if we have counted the number requested */
  15633. if (cd->t_refresh_count != cd->t_refresh_total) {
  15634. ret = scnprintf(buf, strlen(buf),
  15635. "Status: 0\n"
  15636. "%s: %d\n",
  15637. "Still counting... current IRQ count",
  15638. cd->t_refresh_count);
  15639. } else {
  15640. /* Ensure T-Refresh is de-activated */
  15641. cd->t_refresh_active = 0;
  15642. whole = cd->t_refresh_time / cd->t_refresh_count;
  15643. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15644. fraction = fraction * 1000 / cd->t_refresh_count;
  15645. ret = scnprintf(buf, strlen(buf),
  15646. "Status: 0\n"
  15647. "%s: %d\n"
  15648. "%s: %d\n"
  15649. "%s: %d\n"
  15650. "%s: %d.%02d\n",
  15651. "Requested IRQ Count ", cd->t_refresh_total,
  15652. "IRQ Counted ", cd->t_refresh_count,
  15653. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15654. "Average T-Refresh (ms) ", whole, fraction);
  15655. }
  15656. mutex_unlock(&cd->system_lock);
  15657. return ret;
  15658. }
  15659. /*******************************************************************************
  15660. * FUNCTION: pt_dut_status_show
  15661. *
  15662. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15663. * more items such as operation mode,easywake state are added in the future.
  15664. *
  15665. * RETURN: Char buffer with printed scan status information
  15666. *
  15667. * PARAMETERS:
  15668. * *dev - pointer to device structure
  15669. * *attr - pointer to device attributes
  15670. * *buf - pointer to output buffer
  15671. ******************************************************************************/
  15672. static ssize_t pt_dut_status_show(struct device *dev,
  15673. struct device_attribute *attr, char *buf)
  15674. {
  15675. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15676. u8 mode = PT_MODE_UNKNOWN;
  15677. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15678. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15679. struct pt_core_data *cd = dev_get_drvdata(dev);
  15680. ssize_t ret;
  15681. u16 calculated_crc = 0;
  15682. u16 stored_crc = 0;
  15683. u8 status;
  15684. int rc = 0;
  15685. /* In STANDBY the DUT will not repond to any PIP cmd */
  15686. if (cd->fw_sys_mode_in_standby_state) {
  15687. mode = PT_MODE_OPERATIONAL;
  15688. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15689. goto print_limited_results;
  15690. }
  15691. /* Retrieve mode and FW system mode which can only be 0-4 */
  15692. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15693. if (rc || mode == PT_MODE_UNKNOWN) {
  15694. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15695. "%s: %d\n"
  15696. "%s: n/a\n"
  15697. "%s: n/a\n"
  15698. "%s: n/a\n"
  15699. "%s: n/a\n",
  15700. "Status", rc,
  15701. "Active Exec ",
  15702. "FW System Mode ",
  15703. "Stored CRC ",
  15704. "Calculated CRC ");
  15705. return ret;
  15706. } else {
  15707. if (mode == PT_MODE_OPERATIONAL) {
  15708. if (sys_mode > FW_SYS_MODE_MAX)
  15709. sys_mode = FW_SYS_MODE_UNDEFINED;
  15710. if (sys_mode != FW_SYS_MODE_TEST)
  15711. goto print_limited_results;
  15712. rc = pt_pip_verify_config_block_crc_(cd,
  15713. PT_TCH_PARM_EBID, &status,
  15714. &calculated_crc, &stored_crc);
  15715. if (rc)
  15716. goto print_limited_results;
  15717. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15718. "%s: %d\n"
  15719. "%s: %s\n"
  15720. "%s: %s\n"
  15721. "%s: 0x%04X\n"
  15722. "%s: 0x%04X\n",
  15723. "Status", rc,
  15724. "Active Exec ", "FW",
  15725. "FW System Mode ", outputstring[sys_mode],
  15726. "Stored CRC ", stored_crc,
  15727. "Calculated CRC ", calculated_crc);
  15728. return ret;
  15729. } else {
  15730. /* When in BL or unknon mode Active Exec is "n/a" */
  15731. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15732. }
  15733. }
  15734. print_limited_results:
  15735. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15736. "%s: %d\n"
  15737. "%s: %s\n"
  15738. "%s: %s\n"
  15739. "%s: n/a\n"
  15740. "%s: n/a\n",
  15741. "Status", rc,
  15742. "Active Exec ",
  15743. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15744. "FW System Mode ", outputstring[sys_mode],
  15745. "Stored CRC ",
  15746. "Calculated CRC ");
  15747. return ret;
  15748. }
  15749. #endif /* TTDL_DIAGNOSTICS */
  15750. /*******************************************************************************
  15751. * Structures of sysfs attributes for all DUT dependent sysfs node
  15752. ******************************************************************************/
  15753. static struct attribute *early_attrs[] = {
  15754. &dev_attr_hw_version.attr,
  15755. &dev_attr_drv_version.attr,
  15756. &dev_attr_drv_ver.attr,
  15757. &dev_attr_fw_version.attr,
  15758. &dev_attr_sysinfo.attr,
  15759. &dev_attr_pip2_cmd_rsp.attr,
  15760. &dev_attr_command.attr,
  15761. &dev_attr_drv_debug.attr,
  15762. &dev_attr_hw_reset.attr,
  15763. &dev_attr_response.attr,
  15764. &dev_attr_ttdl_restart.attr,
  15765. #ifdef TTDL_DIAGNOSTICS
  15766. &dev_attr_ttdl_status.attr,
  15767. &dev_attr_pip2_enter_bl.attr,
  15768. &dev_attr_pip2_exit_bl.attr,
  15769. &dev_attr_err_gpio.attr,
  15770. &dev_attr_flush_bus.attr,
  15771. &dev_attr_ttdl_bist.attr,
  15772. #endif /* TTDL_DIAGNOSTICS */
  15773. NULL,
  15774. };
  15775. static struct attribute_group early_attr_group = {
  15776. .attrs = early_attrs,
  15777. };
  15778. static struct device_attribute pip2_attributes[] = {
  15779. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15780. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15781. #ifdef TTDL_DIAGNOSTICS
  15782. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15783. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15784. pt_pip2_ping_test_store),
  15785. #endif
  15786. };
  15787. static struct device_attribute attributes[] = {
  15788. __ATTR(dut_debug, 0644,
  15789. pt_dut_debug_show, pt_drv_debug_store),
  15790. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15791. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15792. __ATTR(get_param, 0644,
  15793. pt_get_param_show, pt_get_param_store),
  15794. #ifdef EASYWAKE_TSG6
  15795. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15796. pt_easy_wakeup_gesture_store),
  15797. __ATTR(easy_wakeup_gesture_id, 0444,
  15798. pt_easy_wakeup_gesture_id_show, NULL),
  15799. __ATTR(easy_wakeup_gesture_data, 0444,
  15800. pt_easy_wakeup_gesture_data_show, NULL),
  15801. #endif
  15802. #ifdef TTDL_DIAGNOSTICS
  15803. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15804. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15805. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15806. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15807. #endif /* TTDL_DIAGNOSTICS */
  15808. };
  15809. /*******************************************************************************
  15810. * FUNCTION: add_sysfs_interfaces
  15811. *
  15812. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15813. *
  15814. * RETURN:
  15815. * 0 = success
  15816. * !0 = failure
  15817. *
  15818. * PARAMETERS:
  15819. * *dev - pointer to device structure
  15820. ******************************************************************************/
  15821. static int add_sysfs_interfaces(struct device *dev)
  15822. {
  15823. int i;
  15824. int j = 0;
  15825. struct pt_core_data *cd = dev_get_drvdata(dev);
  15826. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15827. if (device_create_file(dev, attributes + i))
  15828. goto undo;
  15829. }
  15830. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15831. __func__, cd->active_dut_generation);
  15832. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15833. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15834. if (device_create_file(dev, pip2_attributes + j))
  15835. goto undo;
  15836. }
  15837. }
  15838. return 0;
  15839. undo:
  15840. for (i--; i >= 0; i--)
  15841. device_remove_file(dev, attributes + i);
  15842. for (j--; j >= 0; j--)
  15843. device_remove_file(dev, pip2_attributes + j);
  15844. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15845. __func__);
  15846. return -ENODEV;
  15847. }
  15848. /*******************************************************************************
  15849. * FUNCTION: remove_sysfs_interfaces
  15850. *
  15851. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15852. *
  15853. * RETURN: void
  15854. *
  15855. * PARAMETERS:
  15856. * *dev - pointer to device structure
  15857. ******************************************************************************/
  15858. static void remove_sysfs_interfaces(struct device *dev)
  15859. {
  15860. int i;
  15861. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15862. device_remove_file(dev, attributes + i);
  15863. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15864. device_remove_file(dev, pip2_attributes + i);
  15865. }
  15866. /*******************************************************************************
  15867. * FUNCTION: remove_sysfs_and_modules
  15868. *
  15869. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15870. *
  15871. * RETURN: void
  15872. *
  15873. * PARAMETERS:
  15874. * *dev - pointer to device structure
  15875. ******************************************************************************/
  15876. static void remove_sysfs_and_modules(struct device *dev)
  15877. {
  15878. struct pt_core_data *cd = dev_get_drvdata(dev);
  15879. /* Queued work should be removed before to release loader module */
  15880. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15881. pt_release_modules(cd);
  15882. pt_btn_release(dev);
  15883. pt_mt_release(dev);
  15884. remove_sysfs_interfaces(dev);
  15885. }
  15886. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15887. {
  15888. int retval;
  15889. /* Get pinctrl if target uses pinctrl */
  15890. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15891. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15892. retval = PTR_ERR(cd->ts_pinctrl);
  15893. dev_dbg(cd->dev,
  15894. "Target does not use pinctrl %d\n", retval);
  15895. goto err_pinctrl_get;
  15896. }
  15897. cd->pinctrl_state_active
  15898. = pinctrl_lookup_state(cd->ts_pinctrl,
  15899. PINCTRL_STATE_ACTIVE);
  15900. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15901. retval = PTR_ERR(cd->pinctrl_state_active);
  15902. dev_err(cd->dev,
  15903. "Can not lookup %s pinstate %d\n",
  15904. PINCTRL_STATE_ACTIVE, retval);
  15905. goto err_pinctrl_lookup;
  15906. }
  15907. cd->pinctrl_state_suspend
  15908. = pinctrl_lookup_state(cd->ts_pinctrl,
  15909. PINCTRL_STATE_SUSPEND);
  15910. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15911. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15912. dev_err(cd->dev,
  15913. "Can not lookup %s pinstate %d\n",
  15914. PINCTRL_STATE_SUSPEND, retval);
  15915. goto err_pinctrl_lookup;
  15916. }
  15917. cd->pinctrl_state_release
  15918. = pinctrl_lookup_state(cd->ts_pinctrl,
  15919. PINCTRL_STATE_RELEASE);
  15920. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15921. retval = PTR_ERR(cd->pinctrl_state_release);
  15922. dev_dbg(cd->dev,
  15923. "Can not lookup %s pinstate %d\n",
  15924. PINCTRL_STATE_RELEASE, retval);
  15925. }
  15926. return 0;
  15927. err_pinctrl_lookup:
  15928. devm_pinctrl_put(cd->ts_pinctrl);
  15929. err_pinctrl_get:
  15930. cd->ts_pinctrl = NULL;
  15931. return retval;
  15932. }
  15933. /*******************************************************************************
  15934. *******************************************************************************
  15935. * FUNCTION: pt_probe
  15936. *
  15937. * SUMMARY: Probe of the core module.
  15938. *
  15939. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15940. * probe functionality is split into two functions; pt_probe() and
  15941. * pt_probe_complete(). the initial setup is done in this function which
  15942. * then creates a WORK task which runs after the probe timer expires. This
  15943. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15944. *
  15945. * RETURN:
  15946. * 0 = success
  15947. * !0 = failure
  15948. *
  15949. * PARAMETERS:
  15950. * *ops - pointer to the bus
  15951. * *dev - pointer to the device structure
  15952. * irq - IRQ
  15953. * xfer_buf_size - size of the buffer
  15954. ******************************************************************************/
  15955. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15956. u16 irq, size_t xfer_buf_size)
  15957. {
  15958. struct pt_core_data *cd;
  15959. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15960. enum pt_atten_type type;
  15961. struct i2c_client *client = to_i2c_client(dev);
  15962. int rc = 0;
  15963. u8 pip_ver_major;
  15964. u8 pip_ver_minor;
  15965. u32 status = STARTUP_STATUS_START;
  15966. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15967. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15968. __func__);
  15969. rc = -ENODEV;
  15970. goto error_no_pdata;
  15971. }
  15972. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15973. if (!pdata->core_pdata->power) {
  15974. pt_debug(dev, DL_ERROR,
  15975. "%s: Missing platform data function\n",
  15976. __func__);
  15977. rc = -ENODEV;
  15978. goto error_no_pdata;
  15979. }
  15980. }
  15981. /* get context and debug print buffers */
  15982. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15983. if (!cd) {
  15984. rc = -ENOMEM;
  15985. goto error_no_pdata;
  15986. }
  15987. /* Initialize device info */
  15988. cd->dev = dev;
  15989. cd->pdata = pdata;
  15990. cd->cpdata = pdata->core_pdata;
  15991. cd->bus_ops = ops;
  15992. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15993. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15994. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15995. cd->hw_detected = false;
  15996. cd->pip2_prot_active = false;
  15997. cd->pip2_send_user_cmd = false;
  15998. cd->bl_pip_ver_ready = false;
  15999. cd->app_pip_ver_ready = false;
  16000. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  16001. cd->get_param_id = 0;
  16002. cd->watchdog_enabled = 0;
  16003. cd->startup_retry_count = 0;
  16004. cd->core_probe_complete = 0;
  16005. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  16006. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  16007. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  16008. cd->flashless_dut = 0;
  16009. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  16010. cd->bl_with_no_int = 0;
  16011. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  16012. cd->multi_chip = PT_FEATURE_DISABLE;
  16013. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  16014. cd->sleep_state = SS_SLEEP_NONE;
  16015. cd->quick_boot = false;
  16016. cd->drv_debug_suspend = false;
  16017. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  16018. cd->set_dut_generation = true;
  16019. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16020. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  16021. cd->set_dut_generation = true;
  16022. cd->active_dut_generation = DUT_PIP1_ONLY;
  16023. } else {
  16024. cd->set_dut_generation = false;
  16025. cd->active_dut_generation = DUT_UNKNOWN;
  16026. }
  16027. /* Initialize with platform data */
  16028. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  16029. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  16030. cd->hid_cmd_state = 1;
  16031. cd->fw_updating = false;
  16032. cd->multi_chip = 0;
  16033. #ifdef TTDL_DIAGNOSTICS
  16034. cd->t_refresh_active = 0;
  16035. cd->t_refresh_count = 0;
  16036. cd->pip2_crc_error_count = 0;
  16037. cd->wd_xres_count = 0;
  16038. cd->bl_retry_packet_count = 0;
  16039. cd->file_erase_timeout_count = 0;
  16040. cd->show_tt_data = false;
  16041. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  16042. cd->err_gpio = 0;
  16043. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  16044. cd->ttdl_bist_select = 0x07;
  16045. cd->force_pip2_seq = 0;
  16046. #endif /* TTDL_DIAGNOSTICS */
  16047. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  16048. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  16049. sizeof(PT_PIP2_BIN_FILE_PATH));
  16050. pt_init_hid_descriptor(&cd->hid_desc);
  16051. /* Read and store the descriptor lengths */
  16052. cd->hid_core.hid_report_desc_len =
  16053. le16_to_cpu(cd->hid_desc.report_desc_len);
  16054. cd->hid_core.hid_max_input_len =
  16055. le16_to_cpu(cd->hid_desc.max_input_len);
  16056. cd->hid_core.hid_max_output_len =
  16057. le16_to_cpu(cd->hid_desc.max_output_len);
  16058. /* Initialize mutexes and spinlocks */
  16059. mutex_init(&cd->module_list_lock);
  16060. mutex_init(&cd->system_lock);
  16061. mutex_init(&cd->sysfs_lock);
  16062. mutex_init(&cd->ttdl_restart_lock);
  16063. mutex_init(&cd->firmware_class_lock);
  16064. spin_lock_init(&cd->spinlock);
  16065. /* Initialize module list */
  16066. INIT_LIST_HEAD(&cd->module_list);
  16067. /* Initialize attention lists */
  16068. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  16069. INIT_LIST_HEAD(&cd->atten_list[type]);
  16070. /* Initialize parameter list */
  16071. INIT_LIST_HEAD(&cd->param_list);
  16072. /* Initialize wait queue */
  16073. init_waitqueue_head(&cd->wait_q);
  16074. rc = pt_ts_pinctrl_init(cd);
  16075. if (!rc && cd->ts_pinctrl) {
  16076. /*
  16077. * Pinctrl handle is optional. If pinctrl handle is found
  16078. * let pins to be configured in active state. If not
  16079. * found continue further without error.
  16080. */
  16081. rc = pinctrl_select_state(cd->ts_pinctrl,
  16082. cd->pinctrl_state_active);
  16083. if (rc < 0)
  16084. dev_err(&client->dev, "failed to select pin to active state\n");
  16085. }
  16086. rc = pt_get_regulator(cd, true);
  16087. if (rc) {
  16088. dev_err(&client->dev, "Failed to get voltage regulators\n");
  16089. goto error_alloc_data;
  16090. }
  16091. rc = pt_enable_regulator(cd, true);
  16092. if (rc) {
  16093. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  16094. goto error_get_regulator;
  16095. }
  16096. /* Initialize works */
  16097. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  16098. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  16099. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  16100. /* Initialize HID specific data */
  16101. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  16102. cd->cpdata->vendor_id : HID_VENDOR_ID;
  16103. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  16104. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  16105. cd->hid_core.hid_desc_register =
  16106. cpu_to_le16(cd->cpdata->hid_desc_register);
  16107. /* Set platform easywake value */
  16108. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  16109. #if defined(CONFIG_DRM) || defined(CONFIG_PANEL_NOTIFIER)
  16110. /* Setup active dsi panel */
  16111. active_panel = cd->cpdata->active_panel;
  16112. #endif
  16113. /* Set platform panel_id value */
  16114. cd->panel_id_support = cd->cpdata->panel_id_support;
  16115. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  16116. /* Set Panel ID to default to 0 */
  16117. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  16118. else
  16119. /* Set Panel ID to Not Enabled */
  16120. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  16121. /* Initialize hw_version default to FFFF.FFFF.FF */
  16122. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  16123. dev_set_drvdata(dev, cd);
  16124. /* PtSBC builds will call this function in pt_probe_complete() */
  16125. pt_add_core(dev);
  16126. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  16127. if (rc) {
  16128. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  16129. __func__);
  16130. goto error_enable_regulator;
  16131. }
  16132. /*
  16133. * Save the pointer to a global value, which will be used
  16134. * in ttdl_restart function
  16135. */
  16136. cd->bus_ops = ops;
  16137. /*
  16138. * When the IRQ GPIO is not direclty accessible and no function is
  16139. * defined to get the IRQ status, the IRQ passed in must be assigned
  16140. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16141. */
  16142. if (!cd->cpdata->irq_stat) {
  16143. cd->irq = irq;
  16144. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  16145. __func__, cd->irq);
  16146. }
  16147. /* Call platform init function before setting up the GPIO's */
  16148. if (cd->cpdata->init) {
  16149. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16150. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16151. } else {
  16152. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16153. __func__);
  16154. rc = 0;
  16155. }
  16156. if (rc < 0) {
  16157. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16158. __func__, rc);
  16159. }
  16160. /* Power on any needed regulator(s) */
  16161. if (cd->cpdata->setup_power) {
  16162. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16163. rc = cd->cpdata->setup_power(cd->cpdata,
  16164. PT_MT_POWER_ON, cd->dev);
  16165. } else {
  16166. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16167. __func__);
  16168. rc = 0;
  16169. }
  16170. if (rc < 0)
  16171. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16172. __func__, rc);
  16173. #ifdef TTDL_DIAGNOSTICS
  16174. cd->watchdog_irq_stuck_count = 0;
  16175. cd->bus_transmit_error_count = 0;
  16176. #endif /* TTDL_DIAGNOSTICS */
  16177. if (cd->cpdata->detect) {
  16178. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16179. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16180. pt_platform_detect_read);
  16181. if (!rc) {
  16182. cd->hw_detected = true;
  16183. pt_debug(cd->dev, DL_INFO,
  16184. "%s: HW detected\n", __func__);
  16185. } else {
  16186. cd->hw_detected = false;
  16187. pt_debug(cd->dev, DL_INFO,
  16188. "%s: No HW detected\n", __func__);
  16189. rc = -ENODEV;
  16190. goto error_detect;
  16191. }
  16192. } else {
  16193. pt_debug(dev, DL_ERROR,
  16194. "%s: PARADE No HW detect function pointer\n",
  16195. __func__);
  16196. /*
  16197. * "hw_reset" is not needed in the "if" statement,
  16198. * because "hw_reset" is already included in "hw_detect"
  16199. * function.
  16200. */
  16201. rc = pt_hw_hard_reset(cd);
  16202. if (rc)
  16203. pt_debug(cd->dev, DL_ERROR,
  16204. "%s: FAILED to execute HARD reset\n",
  16205. __func__);
  16206. }
  16207. if (cd->cpdata->setup_irq) {
  16208. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16209. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16210. if (rc) {
  16211. pt_debug(dev, DL_ERROR,
  16212. "%s: Error, couldn't setup IRQ\n", __func__);
  16213. goto error_setup_irq;
  16214. }
  16215. } else {
  16216. pt_debug(dev, DL_ERROR,
  16217. "%s: IRQ function pointer not setup\n",
  16218. __func__);
  16219. goto error_setup_irq;
  16220. }
  16221. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16222. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16223. (unsigned long)cd);
  16224. #else
  16225. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16226. #endif
  16227. pt_stop_wd_timer(cd);
  16228. #ifdef TTHE_TUNER_SUPPORT
  16229. mutex_init(&cd->tthe_lock);
  16230. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16231. 0644, NULL, cd, &tthe_debugfs_fops);
  16232. #endif
  16233. rc = device_init_wakeup(dev, 1);
  16234. if (rc < 0)
  16235. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16236. __func__, rc);
  16237. if (!enable_irq_wake(cd->irq)) {
  16238. cd->irq_wake = 1;
  16239. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16240. }
  16241. pm_runtime_get_noresume(dev);
  16242. pm_runtime_set_active(dev);
  16243. pm_runtime_enable(dev);
  16244. /* If IRQ asserted, read out all from buffer to release INT pin */
  16245. if (cd->cpdata->irq_stat) {
  16246. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16247. } else {
  16248. /* Force a read in case the reset sentinel already arrived */
  16249. rc = pt_read_input(cd);
  16250. if (!rc)
  16251. pt_parse_input(cd);
  16252. }
  16253. /* Without sleep DUT is not ready and will NAK the first write */
  16254. msleep(150);
  16255. /* Attempt to set the DUT generation if not yet set */
  16256. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16257. if (cd->bl_pip_ver_ready ||
  16258. (cd->app_pip_ver_ready &&
  16259. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16260. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16261. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16262. __func__, cd->active_dut_generation);
  16263. } else {
  16264. rc = _pt_detect_dut_generation(cd->dev,
  16265. &status, &cd->active_dut_generation,
  16266. &cd->mode);
  16267. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16268. || rc) {
  16269. pt_debug(cd->dev, DL_ERROR,
  16270. " === DUT Gen unknown, Skip Enum ===\n");
  16271. goto skip_enum;
  16272. }
  16273. }
  16274. }
  16275. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16276. &pip_ver_major, &pip_ver_minor);
  16277. if (pip_ver_major == 2) {
  16278. cd->bl_pip_ver_ready = true;
  16279. pt_debug(dev, DL_ERROR,
  16280. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16281. pip_ver_minor);
  16282. cd->hw_detected = true;
  16283. } else if (pip_ver_major == 1) {
  16284. cd->app_pip_ver_ready = true;
  16285. pt_debug(dev, DL_ERROR,
  16286. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16287. cd->hw_detected = true;
  16288. } else {
  16289. cd->sysinfo.ttdata.pip_ver_major = 0;
  16290. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16291. cd->app_pip_ver_ready = false;
  16292. cd->hw_detected = false;
  16293. pt_debug(dev, DL_ERROR,
  16294. " === PIP Version Not Detected, Skip Enum ===\n");
  16295. /* For legacy DUTS proceed, enum will attempt to launch app */
  16296. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16297. goto skip_enum;
  16298. }
  16299. rc = pt_enum_with_dut(cd, false, &status);
  16300. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16301. __func__, cd->startup_status, status);
  16302. if (rc == -ENODEV) {
  16303. pt_debug(cd->dev, DL_ERROR,
  16304. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16305. /* For PtSBC don't error out, allow TTDL to stay up */
  16306. rc = -EPROBE_DEFER;
  16307. goto error_after_startup;
  16308. }
  16309. /* Suspend scanning until probe is complete to avoid asyc touches */
  16310. pt_pip_suspend_scanning_(cd);
  16311. if (cd->hw_detected) {
  16312. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16313. __func__);
  16314. rc = add_sysfs_interfaces(dev);
  16315. if (rc < 0) {
  16316. pt_debug(dev, DL_ERROR,
  16317. "%s: Error, fail sysfs init\n", __func__);
  16318. goto error_after_startup;
  16319. }
  16320. } else {
  16321. pt_debug(dev, DL_ERROR,
  16322. "%s: No HW detected, sysfs interfaces not added\n",
  16323. __func__);
  16324. }
  16325. skip_enum:
  16326. pm_runtime_put_sync(dev);
  16327. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16328. rc = pt_mt_probe(dev);
  16329. if (rc < 0) {
  16330. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16331. __func__);
  16332. goto error_after_sysfs_create;
  16333. }
  16334. rc = pt_btn_probe(dev);
  16335. if (rc < 0) {
  16336. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16337. __func__);
  16338. goto error_after_startup_mt;
  16339. }
  16340. pt_probe_modules(cd);
  16341. #ifdef CONFIG_HAS_EARLYSUSPEND
  16342. pt_setup_early_suspend(cd);
  16343. #elif defined(CONFIG_PANEL_NOTIFIER)
  16344. pt_debug(dev, DL_ERROR, "%s: Probe: Setup Panel Event notifier\n", __func__);
  16345. pt_setup_panel_event_notifier(cd);
  16346. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16347. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16348. #elif defined(CONFIG_DRM)
  16349. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16350. pt_setup_drm_notifier(cd);
  16351. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16352. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16353. #elif defined(CONFIG_FB)
  16354. pt_setup_fb_notifier(cd);
  16355. #endif
  16356. #ifdef NEED_SUSPEND_NOTIFIER
  16357. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16358. register_pm_notifier(&cd->pm_notifier);
  16359. #endif
  16360. pt_pip_resume_scanning_(cd);
  16361. mutex_lock(&cd->system_lock);
  16362. cd->startup_status |= status;
  16363. cd->core_probe_complete = 1;
  16364. mutex_unlock(&cd->system_lock);
  16365. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16366. __func__);
  16367. return 0;
  16368. error_after_startup_mt:
  16369. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16370. pt_mt_release(dev);
  16371. error_after_sysfs_create:
  16372. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16373. pm_runtime_disable(dev);
  16374. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16375. device_wakeup_disable(dev);
  16376. #endif
  16377. device_init_wakeup(dev, 0);
  16378. cancel_work_sync(&cd->enum_work);
  16379. pt_stop_wd_timer(cd);
  16380. pt_free_si_ptrs(cd);
  16381. remove_sysfs_interfaces(dev);
  16382. error_after_startup:
  16383. pr_err("%s PARADE error_after_startup\n", __func__);
  16384. del_timer(&cd->watchdog_timer);
  16385. if (cd->cpdata->setup_irq)
  16386. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16387. error_setup_irq:
  16388. error_detect:
  16389. if (cd->cpdata->init)
  16390. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16391. if (cd->cpdata->setup_power)
  16392. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16393. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16394. error_enable_regulator:
  16395. pt_del_core(dev);
  16396. dev_set_drvdata(dev, NULL);
  16397. pt_enable_regulator(cd, false);
  16398. error_get_regulator:
  16399. pt_get_regulator(cd, false);
  16400. error_alloc_data:
  16401. kfree(cd);
  16402. error_no_pdata:
  16403. pr_err("%s failed.\n", __func__);
  16404. return rc;
  16405. }
  16406. EXPORT_SYMBOL_GPL(pt_probe);
  16407. /*******************************************************************************
  16408. * FUNCTION: pt_release
  16409. *
  16410. * SUMMARY: This function does the following cleanup:
  16411. * - Releases all probed modules
  16412. * - Stops the watchdog
  16413. * - Cancels all pending work tasks
  16414. * - Removes all created sysfs nodes
  16415. * - Removes all created debugfs nodes
  16416. * - Frees the IRQ
  16417. * - Deletes the core
  16418. * - Frees all pointers and HID reports
  16419. *
  16420. * RETURN:
  16421. * 0 = success
  16422. * !0 = failure
  16423. *
  16424. * PARAMETERS:
  16425. * *cd - pointer to the core data structure
  16426. ******************************************************************************/
  16427. int pt_release(struct pt_core_data *cd)
  16428. {
  16429. struct device *dev = cd->dev;
  16430. /*
  16431. * Suspend the device before freeing the startup_work and stopping
  16432. * the watchdog since sleep function restarts watchdog on failure
  16433. */
  16434. pm_runtime_suspend(dev);
  16435. pm_runtime_disable(dev);
  16436. /*
  16437. * Any 'work' that can trigger a new thread should be canceled first.
  16438. * The watchdog is also stopped again because another thread could have
  16439. * restarted it. The irq_work is cancelled last because it is used for
  16440. * all I2C/SPI communication.
  16441. */
  16442. pt_stop_wd_timer(cd);
  16443. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16444. cancel_work_sync(&cd->ttdl_restart_work);
  16445. cancel_work_sync(&cd->enum_work);
  16446. cancel_work_sync(&cd->resume_offload_work);
  16447. cancel_work_sync(&cd->suspend_offload_work);
  16448. cancel_work_sync(&cd->resume_work);
  16449. cancel_work_sync(&cd->suspend_work);
  16450. destroy_workqueue(cd->pt_workqueue);
  16451. pt_stop_wd_timer(cd);
  16452. pt_release_modules(cd);
  16453. pt_proximity_release(dev);
  16454. pt_btn_release(dev);
  16455. pt_mt_release(dev);
  16456. #ifdef CONFIG_HAS_EARLYSUSPEND
  16457. unregister_early_suspend(&cd->es);
  16458. #elif defined(CONFIG_PANEL_NOTIFIER)
  16459. if (active_panel)
  16460. panel_event_notifier_unregister(&cd->fb_notifier);
  16461. #elif defined(CONFIG_DRM)
  16462. if (active_panel)
  16463. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16464. #elif defined(CONFIG_FB)
  16465. fb_unregister_client(&cd->fb_notifier);
  16466. #endif
  16467. #ifdef NEED_SUSPEND_NOTIFIER
  16468. unregister_pm_notifier(&cd->pm_notifier);
  16469. #endif
  16470. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16471. device_wakeup_disable(dev);
  16472. #endif
  16473. device_init_wakeup(dev, 0);
  16474. #ifdef TTHE_TUNER_SUPPORT
  16475. mutex_lock(&cd->tthe_lock);
  16476. cd->tthe_exit = 1;
  16477. wake_up(&cd->wait_q);
  16478. mutex_unlock(&cd->tthe_lock);
  16479. debugfs_remove(cd->tthe_debugfs);
  16480. #endif
  16481. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16482. remove_sysfs_interfaces(dev);
  16483. disable_irq_nosync(cd->irq);
  16484. if (cd->cpdata->setup_irq)
  16485. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16486. if (cd->cpdata->init)
  16487. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16488. if (cd->cpdata->setup_power)
  16489. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16490. dev_set_drvdata(dev, NULL);
  16491. pt_del_core(dev);
  16492. if (cd->vcc_i2c)
  16493. regulator_set_load(cd->vcc_i2c, 0);
  16494. if (cd->vdd)
  16495. regulator_set_load(cd->vdd, 0);
  16496. pt_enable_regulator(cd, false);
  16497. pt_get_regulator(cd, false);
  16498. pt_free_si_ptrs(cd);
  16499. kfree(cd);
  16500. return 0;
  16501. }
  16502. EXPORT_SYMBOL_GPL(pt_release);
  16503. MODULE_LICENSE("GPL");
  16504. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16505. MODULE_AUTHOR("Parade Technologies <[email protected]>");