pt_core.c 518 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028
  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. #include <linux/soc/qcom/panel_event_notifier.h>
  35. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  36. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  37. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  38. #define FT_VTG_MIN_UV 2800000
  39. #define FT_VTG_MAX_UV 2800000
  40. #define FT_I2C_VTG_MIN_UV 1800000
  41. #define FT_I2C_VTG_MAX_UV 1800000
  42. #define PWR_SUSPEND_LOAD_UA 165
  43. #define I2C_SUSPEND_LOAD_UA 100
  44. #define PWR_ACTIVE_LOAD_MA 12000
  45. #define I2C_ACTIVE_LOAD_MA 30000
  46. #define PT_CORE_STARTUP_RETRY_COUNT 3
  47. #define PT_STATUS_STR_LEN (50)
  48. #if defined(CONFIG_DRM)
  49. static struct drm_panel *active_panel;
  50. #endif
  51. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  52. #define ENABLE_I2C_REG_ONLY
  53. #ifdef ENABLE_I2C_REG_ONLY
  54. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  55. #endif
  56. static const char *pt_driver_core_name = PT_CORE_NAME;
  57. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  58. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  59. struct pt_hid_field {
  60. int report_count;
  61. int report_size;
  62. int size; /* report_count * report_size */
  63. int offset;
  64. int data_type;
  65. int logical_min;
  66. int logical_max;
  67. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  68. u32 usage_page;
  69. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  70. struct pt_hid_report *report;
  71. bool record_field;
  72. };
  73. struct pt_hid_report {
  74. u8 id;
  75. u8 type;
  76. int size;
  77. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  78. int num_fields;
  79. int record_field_index;
  80. int header_size;
  81. int record_size;
  82. u32 usage_page;
  83. };
  84. struct atten_node {
  85. struct list_head node;
  86. char *id;
  87. struct device *dev;
  88. int (*func)(struct device *dev);
  89. int mode;
  90. };
  91. struct param_node {
  92. struct list_head node;
  93. u8 id;
  94. u32 value;
  95. u8 size;
  96. };
  97. struct module_node {
  98. struct list_head node;
  99. struct pt_module *module;
  100. void *data;
  101. };
  102. struct pt_hid_cmd {
  103. u8 opcode;
  104. u8 report_type;
  105. union {
  106. u8 report_id;
  107. u8 power_state;
  108. };
  109. u8 has_data_register;
  110. size_t write_length;
  111. u8 *write_buf;
  112. u8 *read_buf;
  113. u8 wait_interrupt;
  114. u8 reset_cmd;
  115. u16 timeout_ms;
  116. };
  117. struct pt_hid_output {
  118. u8 cmd_type;
  119. u16 length;
  120. u8 command_code;
  121. size_t write_length;
  122. u8 *write_buf;
  123. u8 novalidate;
  124. u8 reset_expected;
  125. u16 timeout_ms;
  126. };
  127. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  128. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  129. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  130. #define CREATE_PIP1_FW_CMD(command) \
  131. .cmd_type = PIP1_CMD_TYPE_FW, \
  132. .command_code = command
  133. #define CREATE_PIP1_BL_CMD(command) \
  134. .cmd_type = PIP1_CMD_TYPE_BL, \
  135. .command_code = command
  136. #define PT_MAX_PR_BUF_SIZE 2048
  137. /*******************************************************************************
  138. * FUNCTION: pt_pr_buf
  139. *
  140. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  141. *
  142. * RETURN: Void
  143. *
  144. * PARAMETERS:
  145. * *dev - pointer to Device structure
  146. * debug_level - requested debug level to print at
  147. * *buf - pointer to buffer to print
  148. * buf_len - size of buf
  149. * *data_name - Descriptive name of data prefixed to data
  150. ******************************************************************************/
  151. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  152. u16 buf_len, const char *data_name)
  153. {
  154. struct pt_core_data *cd = dev_get_drvdata(dev);
  155. int i;
  156. ssize_t pr_buf_index = 0;
  157. int max_size;
  158. /* only proceed if valid debug level and there is data to print */
  159. if (debug_level <= cd->debug_level && buf_len > 0) {
  160. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  161. if (!pr_buf)
  162. return;
  163. /*
  164. * With a space each printed char takes 3 bytes, subtract
  165. * the length of the data_name prefix as well as 11 bytes
  166. * for the " [0..xxx]: " printed before the data.
  167. */
  168. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  169. /* Ensure pr_buf_index stays within the 1018 size */
  170. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  171. data_name);
  172. for (i = 0; i < buf_len && i < max_size; i++)
  173. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  174. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  175. pt_debug(dev, debug_level, "%s\n", pr_buf);
  176. kfree(pr_buf);
  177. }
  178. }
  179. EXPORT_SYMBOL_GPL(pt_pr_buf);
  180. #ifdef TTHE_TUNER_SUPPORT
  181. /*******************************************************************************
  182. * FUNCTION: tthe_print
  183. *
  184. * SUMMARY: Format data name and time stamp as the header and format the
  185. * content of input buffer with hex base to "tthe_buf". And then wake up event
  186. * semaphore for tthe debugfs node.
  187. *
  188. * RETURN:
  189. * 0 = success
  190. * !0 = failure
  191. *
  192. * PARAMETERS:
  193. * *cd - pointer to core data
  194. * *buf - pointer to input buffer
  195. * buf_len - size of input buffer
  196. * *data_name - pointer to data name
  197. ******************************************************************************/
  198. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  199. const u8 *data_name)
  200. {
  201. int name_len = strlen(data_name);
  202. int i, n;
  203. u8 *p;
  204. int remain;
  205. u8 data_name_with_time_stamp[100];
  206. /* Prepend timestamp, if requested, to data_name */
  207. if (cd->show_timestamp) {
  208. scnprintf(data_name_with_time_stamp,
  209. sizeof(data_name_with_time_stamp),
  210. "[%u] %s", pt_get_time_stamp(), data_name);
  211. data_name = data_name_with_time_stamp;
  212. name_len = strlen(data_name);
  213. }
  214. mutex_lock(&cd->tthe_lock);
  215. if (!cd->tthe_buf)
  216. goto exit;
  217. /* Add 1 due to the '\n' that is appended at the end */
  218. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  219. goto exit;
  220. if (name_len + buf_len == 0)
  221. goto exit;
  222. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  223. if (remain < name_len)
  224. name_len = remain;
  225. p = cd->tthe_buf + cd->tthe_buf_len;
  226. memcpy(p, data_name, name_len);
  227. cd->tthe_buf_len += name_len;
  228. p += name_len;
  229. remain -= name_len;
  230. *p = 0;
  231. for (i = 0; i < buf_len; i++) {
  232. n = scnprintf(p, remain, "%02X ", buf[i]);
  233. if (n <= 0)
  234. break;
  235. p += n;
  236. remain -= n;
  237. cd->tthe_buf_len += n;
  238. }
  239. n = scnprintf(p, remain, "\n");
  240. cd->tthe_buf_len += n;
  241. exit:
  242. wake_up(&cd->wait_q);
  243. mutex_unlock(&cd->tthe_lock);
  244. return 0;
  245. }
  246. /*******************************************************************************
  247. * FUNCTION: _pt_request_tthe_print
  248. *
  249. * SUMMARY: Function pointer included in core_cmds to allow other modules
  250. * to request to print data to the "tthe_buffer".
  251. *
  252. * RETURN:
  253. * 0 = success
  254. * !0 = failure
  255. *
  256. * PARAMETERS:
  257. * *dev - pointer to device structure
  258. ******************************************************************************/
  259. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  260. int buf_len, const u8 *data_name)
  261. {
  262. struct pt_core_data *cd = dev_get_drvdata(dev);
  263. return tthe_print(cd, buf, buf_len, data_name);
  264. }
  265. #endif
  266. /*******************************************************************************
  267. * FUNCTION: pt_platform_detect_read
  268. *
  269. * SUMMARY: To be passed to platform dectect function to perform a read
  270. * operation.
  271. *
  272. * RETURN:
  273. * 0 = success
  274. * !0 = failure
  275. *
  276. * PARAMETERS:
  277. * *dev - pointer to Device structure
  278. * *buf - pointer to buffer where the data read will be stored
  279. * size - size to be read
  280. ******************************************************************************/
  281. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  282. {
  283. struct pt_core_data *cd = dev_get_drvdata(dev);
  284. return pt_adap_read_default(cd, buf, size);
  285. }
  286. /*******************************************************************************
  287. * FUNCTION: pt_add_parameter
  288. *
  289. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  290. * On every reset of the DUT this linked list is traversed and all
  291. * parameters in it are restored to the DUT.
  292. *
  293. * RETURN:
  294. * 0 = success
  295. * !0 = failure
  296. *
  297. * PARAMETERS:
  298. * *cd - pointer to core data
  299. * param_id - parameter ID to add
  300. * param_value - Value corresponding to the ID
  301. * param_size - Size of param_value
  302. ******************************************************************************/
  303. static int pt_add_parameter(struct pt_core_data *cd,
  304. u8 param_id, u32 param_value, u8 param_size)
  305. {
  306. struct param_node *param, *param_new;
  307. /* Check if parameter already exists in the list */
  308. spin_lock(&cd->spinlock);
  309. list_for_each_entry(param, &cd->param_list, node) {
  310. if (param->id == param_id) {
  311. /* Update parameter */
  312. param->value = param_value;
  313. pt_debug(cd->dev, DL_INFO,
  314. "%s: Update parameter id:%d value:%d size:%d\n",
  315. __func__, param_id, param_value, param_size);
  316. goto exit_unlock;
  317. }
  318. }
  319. spin_unlock(&cd->spinlock);
  320. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  321. if (!param_new)
  322. return -ENOMEM;
  323. param_new->id = param_id;
  324. param_new->value = param_value;
  325. param_new->size = param_size;
  326. pt_debug(cd->dev, DL_INFO,
  327. "%s: Add parameter id:%d value:%d size:%d\n",
  328. __func__, param_id, param_value, param_size);
  329. spin_lock(&cd->spinlock);
  330. list_add(&param_new->node, &cd->param_list);
  331. exit_unlock:
  332. spin_unlock(&cd->spinlock);
  333. return 0;
  334. }
  335. #ifdef TTDL_DIAGNOSTICS
  336. /*******************************************************************************
  337. * FUNCTION: pt_erase_parameter_list
  338. *
  339. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  340. * pairs. In some test cases this functionality is needed to ensure DUT
  341. * returns to a virgin state after a reset and no parameters are restored.
  342. *
  343. * RETURN:
  344. * 0 = success
  345. * !0 = failure
  346. *
  347. * PARAMETERS:
  348. * *cd - pointer to core data
  349. ******************************************************************************/
  350. static int pt_erase_parameter_list(struct pt_core_data *cd)
  351. {
  352. struct param_node *pos, *temp;
  353. spin_lock(&cd->spinlock);
  354. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  355. pt_debug(cd->dev, DL_INFO,
  356. "%s: Parameter Restore List - remove 0x%02x\n",
  357. __func__, pos->id);
  358. list_del(&pos->node);
  359. kfree(pos);
  360. }
  361. spin_unlock(&cd->spinlock);
  362. return 0;
  363. }
  364. /*******************************************************************************
  365. * FUNCTION: pt_count_parameter_list
  366. *
  367. * SUMMARY: Count the items in the RAM parameter restor list
  368. *
  369. * RETURN:
  370. * 0 = success
  371. * !0 = failure
  372. *
  373. * PARAMETERS:
  374. * *cd - pointer to core data
  375. ******************************************************************************/
  376. static int pt_count_parameter_list(struct pt_core_data *cd)
  377. {
  378. struct param_node *pos, *temp;
  379. int entries = 0;
  380. spin_lock(&cd->spinlock);
  381. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  382. entries++;
  383. spin_unlock(&cd->spinlock);
  384. return entries;
  385. }
  386. #endif /* TTDL_DIAGNOSTICS */
  387. /*******************************************************************************
  388. * FUNCTION: request_exclusive
  389. *
  390. * SUMMARY: Request exclusive access to the DUT
  391. *
  392. * RETURN:
  393. * 0 = success
  394. * !0 = failure
  395. *
  396. * PARAMETERS:
  397. * *cd - pointer to core data
  398. * *ownptr - pointer to device
  399. * timeout_ms - Timeout value
  400. ******************************************************************************/
  401. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  402. int timeout_ms)
  403. {
  404. int t = msecs_to_jiffies(timeout_ms);
  405. bool with_timeout = (timeout_ms != 0);
  406. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  407. __func__, timeout_ms);
  408. mutex_lock(&cd->system_lock);
  409. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  410. cd->exclusive_dev = ownptr;
  411. goto exit;
  412. }
  413. cd->exclusive_waits++;
  414. wait:
  415. mutex_unlock(&cd->system_lock);
  416. if (with_timeout) {
  417. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  418. if (IS_TMO(t)) {
  419. pt_debug(cd->dev, DL_ERROR,
  420. "%s: tmo waiting exclusive access\n", __func__);
  421. return -ETIME;
  422. }
  423. } else {
  424. wait_event(cd->wait_q, !cd->exclusive_dev);
  425. }
  426. mutex_lock(&cd->system_lock);
  427. if (cd->exclusive_dev)
  428. goto wait;
  429. cd->exclusive_dev = ownptr;
  430. cd->exclusive_waits--;
  431. exit:
  432. mutex_unlock(&cd->system_lock);
  433. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  434. __func__, ownptr);
  435. return 0;
  436. }
  437. /*******************************************************************************
  438. * FUNCTION: release_exclusive_
  439. *
  440. * SUMMARY: Release exclusive access to the DUT
  441. *
  442. * RETURN:
  443. * 0 = success
  444. *
  445. * PARAMETERS:
  446. * *cd - pointer to core data
  447. * *ownptr - pointer to device
  448. ******************************************************************************/
  449. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  450. {
  451. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  452. __func__);
  453. if (cd->exclusive_dev != ownptr)
  454. return -EINVAL;
  455. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  456. __func__, cd->exclusive_dev);
  457. cd->exclusive_dev = NULL;
  458. wake_up(&cd->wait_q);
  459. return 0;
  460. }
  461. /*******************************************************************************
  462. * FUNCTION: release_exclusive
  463. *
  464. * SUMMARY: Protected wrapper to release_exclusive_()
  465. *
  466. * RETURN:
  467. * 0 = success
  468. * !0 = failure
  469. *
  470. * PARAMETERS:
  471. * *cd - pointer to core data
  472. * *ownptr - pointer to device
  473. ******************************************************************************/
  474. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  475. {
  476. int rc;
  477. mutex_lock(&cd->system_lock);
  478. rc = release_exclusive_(cd, ownptr);
  479. mutex_unlock(&cd->system_lock);
  480. return rc;
  481. }
  482. /*******************************************************************************
  483. * FUNCTION: pt_hid_exec_cmd_
  484. *
  485. * SUMMARY: Send the HID command to the DUT
  486. *
  487. * RETURN:
  488. * 0 = success
  489. * !0 = failure
  490. *
  491. * PARAMETERS:
  492. * *cd - pointer to core data
  493. * *hid_cmd - pointer to the HID command to send
  494. ******************************************************************************/
  495. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  496. struct pt_hid_cmd *hid_cmd)
  497. {
  498. int rc = 0;
  499. u8 *cmd;
  500. u16 cmd_length;
  501. u8 cmd_offset = 0;
  502. cmd_length = 2 /* command register */
  503. + 2 /* command */
  504. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  505. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  506. + hid_cmd->write_length; /* Data length */
  507. if (cmd_length < 4 || cmd_length > PT_MAX_PIP1_MSG_SIZE)
  508. return -EPROTO;
  509. cmd = kzalloc(cmd_length, GFP_KERNEL);
  510. if (!cmd)
  511. return -ENOMEM;
  512. /* Set Command register */
  513. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  514. sizeof(cd->hid_desc.command_register));
  515. cmd_offset += sizeof(cd->hid_desc.command_register);
  516. /* Set Command */
  517. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  518. if (hid_cmd->report_id >= 0XF)
  519. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  520. else
  521. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  522. cmd_offset++;
  523. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  524. cmd_offset++;
  525. if (hid_cmd->report_id >= 0XF) {
  526. cmd[cmd_offset] = hid_cmd->report_id;
  527. cmd_offset++;
  528. }
  529. /* Set Data register */
  530. if (hid_cmd->has_data_register) {
  531. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  532. sizeof(cd->hid_desc.data_register));
  533. cmd_offset += sizeof(cd->hid_desc.data_register);
  534. }
  535. /* Set Data */
  536. if (hid_cmd->write_length && hid_cmd->write_buf) {
  537. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  538. hid_cmd->write_length);
  539. cmd_offset += hid_cmd->write_length;
  540. }
  541. pt_debug(cd->dev, DL_INFO,
  542. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  543. __func__, cmd_length, hid_cmd->report_id);
  544. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  545. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  546. hid_cmd->read_buf);
  547. if (rc)
  548. pt_debug(cd->dev, DL_ERROR,
  549. "%s: Fail pt_adap_transfer\n", __func__);
  550. kfree(cmd);
  551. return rc;
  552. }
  553. #ifdef TTDL_DIAGNOSTICS
  554. /*******************************************************************************
  555. * FUNCTION: pt_toggle_err_gpio
  556. *
  557. * SUMMARY: Toggles the pre-defined error GPIO
  558. *
  559. * RETURN: n/a
  560. *
  561. * PARAMETERS:
  562. * *cd - pointer to core data
  563. * type - type of err that occurred
  564. ******************************************************************************/
  565. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  566. {
  567. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  568. __func__, type);
  569. if (cd->err_gpio && type == cd->err_gpio_type) {
  570. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  571. gpio_direction_output(cd->err_gpio,
  572. !gpio_get_value(cd->err_gpio));
  573. }
  574. }
  575. /*******************************************************************************
  576. * FUNCTION: _pt_request_toggle_err_gpio
  577. *
  578. * SUMMARY: Function pointer included in core_cmds to allow other modules
  579. * to request to toggle the err_gpio
  580. *
  581. * RETURN: n/a
  582. *
  583. * PARAMETERS:
  584. * *cd - pointer to core data
  585. * type - type of err that occurred
  586. ******************************************************************************/
  587. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  588. {
  589. struct pt_core_data *cd = dev_get_drvdata(dev);
  590. pt_toggle_err_gpio(cd, type);
  591. }
  592. #endif /* TTDL_DIAGNOSTICS */
  593. /*******************************************************************************
  594. * FUNCTION: pt_hid_exec_cmd_and_wait_
  595. *
  596. * SUMMARY: Send the HID command to the DUT and wait for the response
  597. *
  598. * RETURN:
  599. * 0 = success
  600. * !0 = failure
  601. *
  602. * PARAMETERS:
  603. * *cd - pointer to core data
  604. * *hid_cmd - pointer to the HID command to send
  605. ******************************************************************************/
  606. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  607. struct pt_hid_cmd *hid_cmd)
  608. {
  609. int rc = 0;
  610. int t;
  611. u16 timeout_ms;
  612. int *cmd_state;
  613. if (hid_cmd->reset_cmd)
  614. cmd_state = &cd->hid_reset_cmd_state;
  615. else
  616. cmd_state = &cd->hid_cmd_state;
  617. if (hid_cmd->wait_interrupt) {
  618. mutex_lock(&cd->system_lock);
  619. *cmd_state = 1;
  620. mutex_unlock(&cd->system_lock);
  621. }
  622. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  623. if (rc) {
  624. if (hid_cmd->wait_interrupt)
  625. goto error;
  626. goto exit;
  627. }
  628. if (!hid_cmd->wait_interrupt)
  629. goto exit;
  630. if (hid_cmd->timeout_ms)
  631. timeout_ms = hid_cmd->timeout_ms;
  632. else
  633. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  634. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  635. msecs_to_jiffies(timeout_ms));
  636. if (IS_TMO(t)) {
  637. #ifdef TTDL_DIAGNOSTICS
  638. cd->bus_transmit_error_count++;
  639. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  640. #endif /* TTDL_DIAGNOSTICS */
  641. pt_debug(cd->dev, DL_ERROR,
  642. "%s: HID output cmd execution timed out\n",
  643. __func__);
  644. rc = -ETIME;
  645. goto error;
  646. }
  647. goto exit;
  648. error:
  649. mutex_lock(&cd->system_lock);
  650. *cmd_state = 0;
  651. mutex_unlock(&cd->system_lock);
  652. exit:
  653. return rc;
  654. }
  655. /*******************************************************************************
  656. * FUNCTION: pt_hid_cmd_reset_
  657. *
  658. * SUMMARY: Send the HID RESET command to the DUT
  659. *
  660. * RETURN:
  661. * 0 = success
  662. * !0 = failure
  663. *
  664. * PARAMETERS:
  665. * *cd - pointer to core data
  666. ******************************************************************************/
  667. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  668. {
  669. struct pt_hid_cmd hid_cmd = {
  670. .opcode = HID_CMD_RESET,
  671. .wait_interrupt = 1,
  672. .reset_cmd = 1,
  673. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  674. };
  675. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  676. }
  677. /*******************************************************************************
  678. * FUNCTION: pt_hid_cmd_reset
  679. *
  680. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  681. * access.
  682. *
  683. * RETURN:
  684. * 0 = success
  685. * !0 = failure
  686. *
  687. * PARAMETERS:
  688. * *cd - pointer to core data
  689. ******************************************************************************/
  690. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  691. {
  692. int rc = 0;
  693. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  694. if (rc < 0) {
  695. pt_debug(cd->dev, DL_ERROR,
  696. "%s: fail get exclusive ex=%p own=%p\n",
  697. __func__, cd->exclusive_dev, cd->dev);
  698. return rc;
  699. }
  700. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  701. rc = pt_hid_cmd_reset_(cd);
  702. if (release_exclusive(cd, cd->dev) < 0)
  703. pt_debug(cd->dev, DL_ERROR,
  704. "%s: fail to release exclusive\n", __func__);
  705. return rc;
  706. }
  707. /*******************************************************************************
  708. * FUNCTION: pt_hid_cmd_set_power_
  709. *
  710. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  711. *
  712. * RETURN:
  713. * 0 = success
  714. * !0 = failure
  715. *
  716. * PARAMETERS:
  717. * *cd - pointer to core data
  718. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  719. ******************************************************************************/
  720. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  721. u8 power_state)
  722. {
  723. int rc = 0;
  724. struct pt_hid_cmd hid_cmd = {
  725. .opcode = HID_CMD_SET_POWER,
  726. .wait_interrupt = 1,
  727. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  728. };
  729. hid_cmd.power_state = power_state;
  730. /* The chip won't give response if goes to Deep Standby */
  731. if (power_state == HID_POWER_STANDBY) {
  732. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  733. if (rc)
  734. pt_debug(cd->dev, DL_ERROR,
  735. "%s: Failed to set power to state:%d\n",
  736. __func__, power_state);
  737. else
  738. cd->fw_sys_mode_in_standby_state = true;
  739. return rc;
  740. }
  741. cd->fw_sys_mode_in_standby_state = false;
  742. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  743. if (rc) {
  744. pt_debug(cd->dev, DL_ERROR,
  745. "%s: Failed to set power to state:%d\n",
  746. __func__, power_state);
  747. return rc;
  748. }
  749. /* validate */
  750. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  751. || ((cd->response_buf[3] & 0x3) != power_state)
  752. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  753. rc = -EINVAL;
  754. return rc;
  755. }
  756. /*******************************************************************************
  757. * FUNCTION: pt_hid_cmd_set_power
  758. *
  759. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  760. * exclusive access.
  761. *
  762. * RETURN:
  763. * 0 = success
  764. * !0 = failure
  765. *
  766. * PARAMETERS:
  767. * *cd - pointer to core data
  768. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  769. ******************************************************************************/
  770. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  771. u8 power_state)
  772. {
  773. int rc;
  774. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  775. if (rc < 0) {
  776. pt_debug(cd->dev, DL_ERROR,
  777. "%s: fail get exclusive ex=%p own=%p\n",
  778. __func__, cd->exclusive_dev, cd->dev);
  779. return rc;
  780. }
  781. rc = pt_hid_cmd_set_power_(cd, power_state);
  782. if (release_exclusive(cd, cd->dev) < 0)
  783. pt_debug(cd->dev, DL_ERROR,
  784. "%s: fail to release exclusive\n", __func__);
  785. return rc;
  786. }
  787. static const u16 crc_table[16] = {
  788. 0x0000, 0x1021, 0x2042, 0x3063,
  789. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  790. 0x8108, 0x9129, 0xa14a, 0xb16b,
  791. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  792. };
  793. /*******************************************************************************
  794. * FUNCTION: _pt_compute_crc
  795. *
  796. * SUMMARY: Calculate CRC by CRC table.
  797. *
  798. * RETURN:
  799. * CRC calculation result
  800. *
  801. * PARAMETERS:
  802. * *buf - pointer to the data array to be calculated
  803. * size - size of data array
  804. ******************************************************************************/
  805. static u16 _pt_compute_crc(u8 *buf, u32 size)
  806. {
  807. u16 remainder = 0xFFFF;
  808. u16 xor_mask = 0x0000;
  809. u32 index;
  810. u32 byte_value;
  811. u32 table_index;
  812. u32 crc_bit_width = sizeof(u16) * 8;
  813. /* Divide the message by polynomial, via the table. */
  814. for (index = 0; index < size; index++) {
  815. byte_value = buf[index];
  816. table_index = ((byte_value >> 4) & 0x0F)
  817. ^ (remainder >> (crc_bit_width - 4));
  818. remainder = crc_table[table_index] ^ (remainder << 4);
  819. table_index = (byte_value & 0x0F)
  820. ^ (remainder >> (crc_bit_width - 4));
  821. remainder = crc_table[table_index] ^ (remainder << 4);
  822. }
  823. /* Perform the final remainder CRC. */
  824. return remainder ^ xor_mask;
  825. }
  826. u16 ccitt_Table[] = {
  827. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  828. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  829. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  830. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  831. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  832. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  833. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  834. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  835. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  836. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  837. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  838. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  839. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  840. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  841. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  842. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  843. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  844. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  845. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  846. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  847. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  848. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  849. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  850. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  851. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  852. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  853. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  854. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  855. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  856. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  857. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  858. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  859. };
  860. /*******************************************************************************
  861. * FUNCTION: crc_ccitt_calculate
  862. *
  863. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  864. *
  865. * RETURN:
  866. * CRC calculation result
  867. *
  868. * PARAMETERS:
  869. * *q - pointer to the data array to be calculated
  870. * len - size of data array
  871. ******************************************************************************/
  872. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  873. {
  874. unsigned short crc = 0xffff;
  875. while (len-- > 0)
  876. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  877. return crc;
  878. }
  879. /*******************************************************************************
  880. * FUNCTION: pt_pip2_cmd_calculate_crc
  881. *
  882. * SUMMARY: Calculate the CRC of a command packet
  883. *
  884. * RETURN: void
  885. *
  886. * PARAMETERS:
  887. * *cmd - pointer to command data
  888. * extra_bytes - Extra bytes included in command length
  889. ******************************************************************************/
  890. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  891. u8 extra_bytes)
  892. {
  893. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  894. unsigned short crc;
  895. buf[0] = cmd->len & 0xff;
  896. buf[1] = (cmd->len & 0xff00) >> 8;
  897. buf[2] = cmd->seq;
  898. buf[3] = cmd->id;
  899. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  900. /* Calculate the CRC for the first 4 bytes above and the data payload */
  901. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  902. cmd->crc[0] = (crc & 0xff00) >> 8;
  903. cmd->crc[1] = (crc & 0xff);
  904. }
  905. /*******************************************************************************
  906. * FUNCTION: pt_pip2_get_next_cmd_seq
  907. *
  908. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  909. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  910. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  911. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  912. * increment, just use the forced value.
  913. *
  914. * RETURN: Next command sequence number [0x08-0x0F]
  915. *
  916. * PARAMETERS:
  917. * *cd - pointer to core data
  918. ******************************************************************************/
  919. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  920. {
  921. #ifdef TTDL_DIAGNOSTICS
  922. if (cd->force_pip2_seq <= 0x07) {
  923. cd->pip2_cmd_tag_seq++;
  924. if (cd->pip2_cmd_tag_seq > 0x0F)
  925. cd->pip2_cmd_tag_seq = 0x08;
  926. } else {
  927. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  928. }
  929. #else
  930. cd->pip2_cmd_tag_seq++;
  931. if (cd->pip2_cmd_tag_seq > 0x0F)
  932. cd->pip2_cmd_tag_seq = 0x08;
  933. #endif
  934. return cd->pip2_cmd_tag_seq;
  935. }
  936. /*
  937. * Following macros are to define the response time (the interval between PIP2
  938. * command finishes sending and INT pin falls). The unit is in microsecond.
  939. * It has different time settings between the solution GPIO polling and Bus
  940. * polling due to the considration for system load.
  941. */
  942. #ifdef PT_POLL_RESP_BY_BUS
  943. #define POLL_RETRY_DEFAULT_INTERVAL 50
  944. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  945. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  946. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  947. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  948. #else
  949. #define POLL_RETRY_DEFAULT_INTERVAL 20
  950. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  951. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  952. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  953. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  954. #endif
  955. /*
  956. * id: the command id defined in PIP2
  957. * response_len: the (maximum) length of response.
  958. * response_time_min: minimum response time in microsecond
  959. * response_time_max: maximum response time in microsecond
  960. */
  961. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  962. {.id = PIP2_CMD_ID_PING,
  963. .response_len = 255,
  964. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  965. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  966. {.id = PIP2_CMD_ID_STATUS,
  967. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  968. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  969. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  970. {.id = PIP2_CMD_ID_CTRL,
  971. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  972. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  973. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  974. {.id = PIP2_CMD_ID_CONFIG,
  975. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  976. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  977. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  978. {.id = PIP2_CMD_ID_CLEAR,
  979. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  980. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  981. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  982. {.id = PIP2_CMD_ID_RESET,
  983. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  984. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  985. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  986. {.id = PIP2_CMD_ID_VERSION,
  987. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  988. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  989. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  990. {.id = PIP2_CMD_ID_FILE_OPEN,
  991. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  992. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  993. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  994. {.id = PIP2_CMD_ID_FILE_CLOSE,
  995. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  996. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  997. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  998. {.id = PIP2_CMD_ID_FILE_READ,
  999. .response_len = 255,
  1000. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1001. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1002. {.id = PIP2_CMD_ID_FILE_WRITE,
  1003. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1004. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_FLASH_INFO,
  1011. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1014. {.id = PIP2_CMD_ID_EXECUTE,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1016. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1022. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1023. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1024. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1025. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1026. {.id = PIP2_CMD_ID_READ_GPIO,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_EXECUTE_SCAN,
  1031. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1032. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1033. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1034. {.id = PIP2_CMD_SET_PARAMETER,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_GET_PARAMETER,
  1039. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1042. {.id = PIP2_CMD_SET_DDI_REG,
  1043. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1044. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1045. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1046. {.id = PIP2_CMD_GET_DDI_REG,
  1047. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1048. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1049. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1050. {.id = PIP2_CMD_ID_END,
  1051. .response_len = 255,
  1052. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1053. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1054. };
  1055. /*******************************************************************************
  1056. * FUNCTION: pt_pip2_get_cmd_response_len
  1057. *
  1058. * SUMMARY: Gets the expected response length based on the command ID
  1059. *
  1060. * RETURN: Expected response length
  1061. *
  1062. * PARAMETERS:
  1063. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1064. ******************************************************************************/
  1065. static int pt_pip2_get_cmd_response_len(u8 id)
  1066. {
  1067. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1068. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1069. p++;
  1070. if (p->id != PIP2_CMD_ID_END)
  1071. return p->response_len;
  1072. else
  1073. return -EPERM;
  1074. }
  1075. /*******************************************************************************
  1076. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1077. *
  1078. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1079. * finishes sending and INT pin falls) based on the command ID
  1080. *
  1081. * RETURN: Estimated minimum response time in microsecond
  1082. *
  1083. * PARAMETERS:
  1084. * id - Command ID
  1085. ******************************************************************************/
  1086. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1087. {
  1088. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1089. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1090. p++;
  1091. if (p->id != PIP2_CMD_ID_END)
  1092. return p->response_time_min;
  1093. else
  1094. return PIP2_RESP_DEFAULT_TIME_MIN;
  1095. }
  1096. /*******************************************************************************
  1097. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1098. *
  1099. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1100. * finishes sending and INT pin falls) based on the command ID
  1101. *
  1102. * RETURN: Estimated maximum response time in microsecond
  1103. *
  1104. * PARAMETERS:
  1105. * id - Command ID
  1106. ******************************************************************************/
  1107. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1108. {
  1109. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1110. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1111. p++;
  1112. if (p->id != PIP2_CMD_ID_END)
  1113. return p->response_time_max;
  1114. else
  1115. return PIP2_RESP_DEFAULT_TIME_MAX;
  1116. }
  1117. /*******************************************************************************
  1118. * FUNCTION: pt_pip2_validate_response
  1119. *
  1120. * SUMMARY: Validate the response of PIP2 command.
  1121. *
  1122. * RETURN:
  1123. * 0 = success
  1124. * !0 = failure
  1125. *
  1126. * PARAMETERS:
  1127. * *cd - pointer to core data
  1128. * *pip2_cmd - pointer to PIP2 command to send
  1129. * *read_buf - pointer to response buffer
  1130. * actual_read_len - actual read length of the response
  1131. ******************************************************************************/
  1132. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1133. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1134. u16 actual_read_len)
  1135. {
  1136. int rc = 0;
  1137. u8 response_seq = 0;
  1138. u8 reserved_bits = 0;
  1139. u8 cmd_id = 0;
  1140. u8 response_bit = 0;
  1141. unsigned short calc_crc = 0;
  1142. unsigned short resp_crc = 0;
  1143. /* Verify the length of response buffer */
  1144. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1145. pt_debug(cd->dev, DL_ERROR,
  1146. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1147. __func__, pip2_cmd->id, actual_read_len);
  1148. rc = -EINVAL;
  1149. goto exit;
  1150. }
  1151. /* Verify the CRC */
  1152. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1153. resp_crc = read_buf[actual_read_len - 2] << 8;
  1154. resp_crc |= read_buf[actual_read_len - 1];
  1155. if (resp_crc != calc_crc) {
  1156. pt_debug(cd->dev, DL_ERROR,
  1157. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1158. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1159. #ifdef TTDL_DIAGNOSTICS
  1160. cd->pip2_crc_error_count++;
  1161. #endif /* TTDL_DIAGNOSTICS */
  1162. rc = -EINVAL;
  1163. goto exit;
  1164. }
  1165. /* Verify the response bit is set */
  1166. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1167. if (!response_bit) {
  1168. pt_debug(cd->dev, DL_ERROR,
  1169. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1170. __func__, pip2_cmd->id, response_bit);
  1171. rc = -EINVAL;
  1172. goto exit;
  1173. }
  1174. /* Verify the command ID matches from command to response */
  1175. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1176. if (cmd_id != pip2_cmd->id) {
  1177. pt_debug(cd->dev, DL_ERROR,
  1178. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1179. __func__, pip2_cmd->id, cmd_id);
  1180. rc = -EINVAL;
  1181. goto exit;
  1182. }
  1183. /* Verify the SEQ number matches from command to response */
  1184. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1185. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1186. pt_debug(cd->dev, DL_ERROR,
  1187. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1188. __func__, pip2_cmd->id,
  1189. pip2_cmd->seq, response_seq);
  1190. rc = -EINVAL;
  1191. goto exit;
  1192. }
  1193. /* Verify the reserved bits are 0 */
  1194. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1195. if (reserved_bits)
  1196. pt_debug(cd->dev, DL_WARN,
  1197. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1198. __func__, pip2_cmd->id, reserved_bits);
  1199. exit:
  1200. if (rc)
  1201. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1202. "PIP RSP:");
  1203. return rc;
  1204. }
  1205. /*******************************************************************************
  1206. * FUNCTION: pt_hid_output_validate_bl_response
  1207. *
  1208. * SUMMARY: Validate the response of bootloader command.
  1209. *
  1210. * RETURN:
  1211. * 0 = success
  1212. * !0 = failure
  1213. *
  1214. * PARAMETERS:
  1215. * *cd - pointer to core data
  1216. * *hid_output - pointer to hid output data structure
  1217. ******************************************************************************/
  1218. static int pt_hid_output_validate_bl_response(
  1219. struct pt_core_data *cd,
  1220. struct pt_hid_output *hid_output)
  1221. {
  1222. u16 size;
  1223. u16 crc;
  1224. u8 status;
  1225. size = get_unaligned_le16(&cd->response_buf[0]);
  1226. if (hid_output->reset_expected && !size)
  1227. return 0;
  1228. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1229. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1230. pt_debug(cd->dev, DL_ERROR,
  1231. "%s: BL output response, wrong report_id\n", __func__);
  1232. return -EPROTO;
  1233. }
  1234. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1235. pt_debug(cd->dev, DL_ERROR,
  1236. "%s: BL output response, wrong SOP\n", __func__);
  1237. return -EPROTO;
  1238. }
  1239. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1240. pt_debug(cd->dev, DL_ERROR,
  1241. "%s: BL output response, wrong EOP\n", __func__);
  1242. return -EPROTO;
  1243. }
  1244. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1245. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1246. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1247. pt_debug(cd->dev, DL_ERROR,
  1248. "%s: BL output response, wrong CRC 0x%X\n",
  1249. __func__, crc);
  1250. return -EPROTO;
  1251. }
  1252. status = cd->response_buf[5];
  1253. if (status) {
  1254. pt_debug(cd->dev, DL_ERROR,
  1255. "%s: BL output response, ERROR:%d\n",
  1256. __func__, status);
  1257. return -EPROTO;
  1258. }
  1259. return 0;
  1260. }
  1261. /*******************************************************************************
  1262. * FUNCTION: pt_hid_output_validate_app_response
  1263. *
  1264. * SUMMARY: Validate the response of application command.
  1265. *
  1266. * RETURN:
  1267. * 0 = success
  1268. * !0 = failure
  1269. *
  1270. * PARAMETERS:
  1271. * *cd - pointer to core data
  1272. * *hid_output - pointer to hid output data structure
  1273. ******************************************************************************/
  1274. static int pt_hid_output_validate_app_response(
  1275. struct pt_core_data *cd,
  1276. struct pt_hid_output *hid_output)
  1277. {
  1278. int command_code;
  1279. u16 size;
  1280. size = get_unaligned_le16(&cd->response_buf[0]);
  1281. if (hid_output->reset_expected && !size)
  1282. return 0;
  1283. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1284. != PT_PIP_NON_HID_RESPONSE_ID) {
  1285. pt_debug(cd->dev, DL_ERROR,
  1286. "%s: APP output response, wrong report_id\n", __func__);
  1287. return -EPROTO;
  1288. }
  1289. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1290. & PIP1_RESP_COMMAND_ID_MASK;
  1291. if (command_code != hid_output->command_code) {
  1292. pt_debug(cd->dev, DL_ERROR,
  1293. "%s: APP output response, wrong command_code:%X\n",
  1294. __func__, command_code);
  1295. return -EPROTO;
  1296. }
  1297. return 0;
  1298. }
  1299. /*******************************************************************************
  1300. * FUNCTION: pt_check_set_parameter
  1301. *
  1302. * SUMMARY: Check command input and response for Set Parameter command.And
  1303. * store the parameter to the list for resume work if pass the check.
  1304. *
  1305. * PARAMETERS:
  1306. * *cd - pointer to core data
  1307. * *hid_output - pointer to hid output data structure
  1308. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1309. ******************************************************************************/
  1310. static void pt_check_set_parameter(struct pt_core_data *cd,
  1311. struct pt_hid_output *hid_output, bool raw)
  1312. {
  1313. u8 *param_buf;
  1314. u32 param_value = 0;
  1315. u8 param_size;
  1316. u8 param_id;
  1317. int i = 0;
  1318. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1319. return;
  1320. /* Check command input for Set Parameter command */
  1321. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1322. && !memcmp(&hid_output->write_buf[0],
  1323. &cd->hid_desc.output_register,
  1324. sizeof(cd->hid_desc.output_register))
  1325. && hid_output->write_buf[4] ==
  1326. PT_PIP_NON_HID_COMMAND_ID
  1327. && hid_output->write_buf[6] ==
  1328. PIP1_CMD_ID_SET_PARAM)
  1329. param_buf = &hid_output->write_buf[7];
  1330. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1331. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1332. && hid_output->write_length >= 3
  1333. && hid_output->write_length <= 6)
  1334. param_buf = &hid_output->write_buf[0];
  1335. else
  1336. return;
  1337. /* Get parameter ID, size and value */
  1338. param_id = param_buf[0];
  1339. param_size = param_buf[1];
  1340. if (param_size > 4) {
  1341. pt_debug(cd->dev, DL_ERROR,
  1342. "%s: Invalid parameter size\n", __func__);
  1343. return;
  1344. }
  1345. param_buf = &param_buf[2];
  1346. while (i < param_size)
  1347. param_value += *(param_buf++) << (8 * i++);
  1348. /* Check command response for Set Parameter command */
  1349. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1350. || (cd->response_buf[4] &
  1351. PIP1_RESP_COMMAND_ID_MASK) !=
  1352. PIP1_CMD_ID_SET_PARAM
  1353. || cd->response_buf[5] != param_id
  1354. || cd->response_buf[6] != param_size) {
  1355. pt_debug(cd->dev, DL_ERROR,
  1356. "%s: Set Parameter command not successful\n",
  1357. __func__);
  1358. return;
  1359. }
  1360. pt_add_parameter(cd, param_id, param_value, param_size);
  1361. }
  1362. /*******************************************************************************
  1363. * FUNCTION: pt_check_command
  1364. *
  1365. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1366. * called here to check output command and store parameter to the list.
  1367. *
  1368. * PARAMETERS:
  1369. * *cd - pointer to core data
  1370. * *hid_output - pointer to hid output data structure
  1371. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1372. ******************************************************************************/
  1373. static void pt_check_command(struct pt_core_data *cd,
  1374. struct pt_hid_output *hid_output, bool raw)
  1375. {
  1376. pt_check_set_parameter(cd, hid_output, raw);
  1377. }
  1378. /*******************************************************************************
  1379. * FUNCTION: pt_hid_output_validate_response
  1380. *
  1381. * SUMMARY: Validate the response of application or bootloader command.
  1382. *
  1383. * RETURN:
  1384. * 0 = success
  1385. * !0 = failure
  1386. *
  1387. * PARAMETERS:
  1388. * *cd - pointer to core data
  1389. * *hid_output - pointer to hid output data structure
  1390. ******************************************************************************/
  1391. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1392. struct pt_hid_output *hid_output)
  1393. {
  1394. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1395. return pt_hid_output_validate_bl_response(cd, hid_output);
  1396. return pt_hid_output_validate_app_response(cd, hid_output);
  1397. }
  1398. /*******************************************************************************
  1399. * FUNCTION: pt_hid_send_output_user_
  1400. *
  1401. * SUMMARY: Blindly send user data to the DUT.
  1402. *
  1403. * RETURN:
  1404. * 0 = success
  1405. * !0 = failure
  1406. *
  1407. * PARAMETERS:
  1408. * *cd - pointer to core data
  1409. * *hid_output - pointer to the command to send
  1410. ******************************************************************************/
  1411. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1412. struct pt_hid_output *hid_output)
  1413. {
  1414. int rc = 0;
  1415. int cmd;
  1416. if (!hid_output->length || !hid_output->write_buf)
  1417. return -EINVAL;
  1418. if (cd->pip2_prot_active) {
  1419. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1420. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1421. } else
  1422. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1423. pt_debug(cd->dev, DL_INFO,
  1424. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1425. __func__, hid_output->length, cmd);
  1426. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1427. hid_output->length, ">>> User CMD");
  1428. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1429. hid_output->write_buf, NULL);
  1430. if (rc)
  1431. pt_debug(cd->dev, DL_ERROR,
  1432. "%s: Fail pt_adap_transfer\n", __func__);
  1433. return rc;
  1434. }
  1435. /*******************************************************************************
  1436. * FUNCTION: pt_hid_send_output_user_and_wait_
  1437. *
  1438. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1439. *
  1440. * RETURN:
  1441. * 0 = success
  1442. * !0 = failure
  1443. *
  1444. * PARAMETERS:
  1445. * *cd - pointer to core data
  1446. * *hid_output - pointer to the command to send
  1447. ******************************************************************************/
  1448. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1449. struct pt_hid_output *hid_output)
  1450. {
  1451. int rc = 0;
  1452. int t;
  1453. mutex_lock(&cd->system_lock);
  1454. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1455. mutex_unlock(&cd->system_lock);
  1456. rc = pt_hid_send_output_user_(cd, hid_output);
  1457. if (rc)
  1458. goto error;
  1459. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1460. msecs_to_jiffies(cd->pip_cmd_timeout));
  1461. if (IS_TMO(t)) {
  1462. #ifdef TTDL_DIAGNOSTICS
  1463. cd->bus_transmit_error_count++;
  1464. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1465. #endif /* TTDL_DIAGNOSTICS */
  1466. pt_debug(cd->dev, DL_ERROR,
  1467. "%s: HID output cmd execution timed out\n",
  1468. __func__);
  1469. rc = -ETIME;
  1470. goto error;
  1471. }
  1472. pt_check_command(cd, hid_output, true);
  1473. goto exit;
  1474. error:
  1475. mutex_lock(&cd->system_lock);
  1476. cd->hid_cmd_state = 0;
  1477. mutex_unlock(&cd->system_lock);
  1478. exit:
  1479. return rc;
  1480. }
  1481. /*******************************************************************************
  1482. * FUNCTION: pt_check_irq_asserted
  1483. *
  1484. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1485. * the FW can hold the INT line low ~150us after the read is complete.
  1486. * NOTE: if irq_stat is not defined this function will return false
  1487. *
  1488. * RETURN:
  1489. * true = IRQ asserted
  1490. * false = IRQ not asserted
  1491. *
  1492. * PARAMETERS:
  1493. * *cd - pointer to core data
  1494. ******************************************************************************/
  1495. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1496. {
  1497. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1498. /*
  1499. * Workaround for FW defect, CDT165308
  1500. * bl_launch app creates a glitch in IRQ line
  1501. */
  1502. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1503. && cd->cpdata->irq_stat) {
  1504. /*
  1505. * in X1S panel and GC1546 panel, the width for the INT
  1506. * glitch is about 4us,the normal INT width of response
  1507. * will last more than 200us, so use 10us delay
  1508. * for distinguish the glitch the normal INT is enough.
  1509. */
  1510. udelay(10);
  1511. }
  1512. #endif
  1513. if (cd->cpdata->irq_stat) {
  1514. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1515. == PT_IRQ_ASSERTED_VALUE) {
  1516. /* Debounce to allow FW to release INT */
  1517. usleep_range(100, 200);
  1518. }
  1519. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1520. == PT_IRQ_ASSERTED_VALUE)
  1521. return true;
  1522. else
  1523. return false;
  1524. }
  1525. return true;
  1526. }
  1527. /*******************************************************************************
  1528. * FUNCTION: pt_flush_bus
  1529. *
  1530. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1531. * Used if IRQ is found to be stuck low
  1532. *
  1533. * RETURN: Length of bytes read from bus
  1534. *
  1535. * PARAMETERS:
  1536. * *cd - pointer to core data
  1537. * flush_type - type of flush
  1538. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1539. * - PT_FLUSH_BUS_FULL_256_READ
  1540. * *read_buf - pointer to store read data
  1541. ******************************************************************************/
  1542. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1543. u8 flush_type, u8 *read_buf)
  1544. {
  1545. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1546. u16 pip_len;
  1547. int bytes_read;
  1548. int rc = 0;
  1549. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1550. rc = pt_adap_read_default(cd, buf, 2);
  1551. if (rc) {
  1552. bytes_read = 0;
  1553. goto exit;
  1554. }
  1555. pip_len = get_unaligned_le16(&buf[0]);
  1556. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1557. #ifdef TTDL_DIAGNOSTICS
  1558. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1559. #endif
  1560. bytes_read = 2;
  1561. pt_debug(cd->dev, DL_INFO,
  1562. "%s: Empty buf detected - len=0x%04X\n",
  1563. __func__, pip_len);
  1564. } else if (pip_len == 0) {
  1565. bytes_read = 0;
  1566. pt_debug(cd->dev, DL_INFO,
  1567. "%s: Sentinel detected\n", __func__);
  1568. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1569. pt_debug(cd->dev, DL_ERROR,
  1570. "%s: Illegal len=0x%04x, force %d byte read\n",
  1571. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1572. rc = pt_adap_read_default(cd, buf,
  1573. PT_MAX_PIP2_MSG_SIZE);
  1574. if (!rc)
  1575. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1576. else
  1577. bytes_read = 0;
  1578. } else {
  1579. pt_debug(cd->dev, DL_INFO,
  1580. "%s: Flush read of %d bytes...\n",
  1581. __func__, pip_len);
  1582. rc = pt_adap_read_default(cd, buf, pip_len);
  1583. if (!rc)
  1584. bytes_read = pip_len;
  1585. else
  1586. bytes_read = 0;
  1587. }
  1588. } else {
  1589. pt_debug(cd->dev, DL_INFO,
  1590. "%s: Forced flush of max %d bytes...\n",
  1591. __func__, PT_MAX_PIP2_MSG_SIZE);
  1592. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1593. if (!rc)
  1594. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1595. else
  1596. bytes_read = 0;
  1597. }
  1598. if (read_buf && (bytes_read > 3))
  1599. memcpy(read_buf, buf, bytes_read);
  1600. exit:
  1601. return bytes_read;
  1602. }
  1603. /*******************************************************************************
  1604. * FUNCTION: pt_flush_bus_if_irq_asserted
  1605. *
  1606. * SUMMARY: This function will flush the active bus if the INT is found to be
  1607. * asserted.
  1608. *
  1609. * RETURN: bytes cleared from bus
  1610. *
  1611. * PARAMETERS:
  1612. * *cd - pointer the core data structure
  1613. * flush_type - type of flush
  1614. * - PT_FLUSH_BUS_BASED_ON_LEN
  1615. * - PT_FLUSH_BUS_FULL_256_READ
  1616. ******************************************************************************/
  1617. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1618. {
  1619. int count = 0;
  1620. int bytes_read = 0;
  1621. while (pt_check_irq_asserted(cd) && count < 5) {
  1622. count++;
  1623. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1624. if (bytes_read) {
  1625. pt_debug(cd->dev, DL_WARN,
  1626. "%s: Cleared %d bytes off bus\n",
  1627. __func__, bytes_read);
  1628. }
  1629. }
  1630. if (pt_check_irq_asserted(cd)) {
  1631. pt_debug(cd->dev, DL_ERROR,
  1632. "%s: IRQ still asserted, %d bytes read\n",
  1633. __func__, bytes_read);
  1634. } else {
  1635. pt_debug(cd->dev, DL_INFO,
  1636. "%s: IRQ cleared, %d bytes read\n",
  1637. __func__, bytes_read);
  1638. }
  1639. return bytes_read;
  1640. }
  1641. /*******************************************************************************
  1642. * FUNCTION: pt_hid_send_output_
  1643. *
  1644. * SUMMARY: Send a touch application command to the DUT
  1645. *
  1646. * RETURN:
  1647. * 0 = success
  1648. * !0 = failure
  1649. *
  1650. * PARAMETERS:
  1651. * *cd - pointer to core data
  1652. * *hid_output - pointer to the command to send
  1653. ******************************************************************************/
  1654. static int pt_hid_send_output_(struct pt_core_data *cd,
  1655. struct pt_hid_output *hid_output)
  1656. {
  1657. int rc = 0;
  1658. u8 *cmd;
  1659. u16 length;
  1660. u16 crc;
  1661. u8 report_id;
  1662. u8 cmd_offset = 0;
  1663. u8 cmd_allocated = 0;
  1664. switch (hid_output->cmd_type) {
  1665. case PIP1_CMD_TYPE_FW:
  1666. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1667. length = 5;
  1668. break;
  1669. case PIP1_CMD_TYPE_BL:
  1670. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1671. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1672. break;
  1673. default:
  1674. return -EINVAL;
  1675. }
  1676. length += hid_output->write_length;
  1677. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1678. cmd = kzalloc(length + 2, GFP_KERNEL);
  1679. if (!cmd)
  1680. return -ENOMEM;
  1681. cmd_allocated = 1;
  1682. } else {
  1683. cmd = cd->cmd_buf;
  1684. }
  1685. /* Set Output register */
  1686. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1687. sizeof(cd->hid_desc.output_register));
  1688. cmd_offset += sizeof(cd->hid_desc.output_register);
  1689. cmd[cmd_offset++] = LOW_BYTE(length);
  1690. cmd[cmd_offset++] = HI_BYTE(length);
  1691. cmd[cmd_offset++] = report_id;
  1692. cmd[cmd_offset++] = 0x0; /* reserved */
  1693. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1694. cmd[cmd_offset++] = PIP1_BL_SOP;
  1695. cmd[cmd_offset++] = hid_output->command_code;
  1696. /* Set Data Length for bootloader */
  1697. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1698. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1699. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1700. }
  1701. /* Set Data */
  1702. if (hid_output->write_length && hid_output->write_buf) {
  1703. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1704. hid_output->write_length);
  1705. cmd_offset += hid_output->write_length;
  1706. }
  1707. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1708. crc = _pt_compute_crc(&cmd[6],
  1709. hid_output->write_length + 4);
  1710. cmd[cmd_offset++] = LOW_BYTE(crc);
  1711. cmd[cmd_offset++] = HI_BYTE(crc);
  1712. cmd[cmd_offset++] = PIP1_BL_EOP;
  1713. }
  1714. pt_debug(cd->dev, DL_INFO,
  1715. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1716. __func__, length + 2, hid_output->command_code);
  1717. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1718. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1719. if (rc)
  1720. pt_debug(cd->dev, DL_ERROR,
  1721. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1722. if (cmd_allocated)
  1723. kfree(cmd);
  1724. return rc;
  1725. }
  1726. /*******************************************************************************
  1727. * FUNCTION: pt_pip1_send_output_and_wait_
  1728. *
  1729. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1730. *
  1731. * RETURN:
  1732. * 0 = success
  1733. * !0 = failure
  1734. *
  1735. * PARAMETERS:
  1736. * *cd - pointer to core data
  1737. * *hid_output - pointer to the command to send
  1738. ******************************************************************************/
  1739. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1740. struct pt_hid_output *hid_output)
  1741. {
  1742. int rc = 0;
  1743. int t;
  1744. u16 timeout_ms;
  1745. mutex_lock(&cd->system_lock);
  1746. cd->hid_cmd_state = hid_output->command_code + 1;
  1747. mutex_unlock(&cd->system_lock);
  1748. if (hid_output->timeout_ms)
  1749. timeout_ms = hid_output->timeout_ms;
  1750. else
  1751. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1752. rc = pt_hid_send_output_(cd, hid_output);
  1753. if (rc)
  1754. goto error;
  1755. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1756. msecs_to_jiffies(timeout_ms));
  1757. if (IS_TMO(t)) {
  1758. #ifdef TTDL_DIAGNOSTICS
  1759. cd->bus_transmit_error_count++;
  1760. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1761. #endif /* TTDL_DIAGNOSTICS */
  1762. pt_debug(cd->dev, DL_ERROR,
  1763. "%s: HID output cmd execution timed out (%dms)\n",
  1764. __func__, timeout_ms);
  1765. rc = -ETIME;
  1766. goto error;
  1767. }
  1768. if (!hid_output->novalidate)
  1769. rc = pt_hid_output_validate_response(cd, hid_output);
  1770. pt_check_command(cd, hid_output, false);
  1771. goto exit;
  1772. error:
  1773. mutex_lock(&cd->system_lock);
  1774. cd->hid_cmd_state = 0;
  1775. mutex_unlock(&cd->system_lock);
  1776. exit:
  1777. return rc;
  1778. }
  1779. /*******************************************************************************
  1780. * FUNCTION: pt_hid_output_user_cmd_
  1781. *
  1782. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1783. * to the DUT waiting for the response and loading it into the read buffer
  1784. *
  1785. * RETURN:
  1786. * 0 = success
  1787. * !0 = failure
  1788. *
  1789. * PARAMETERS:
  1790. * *cd - pointer to core data
  1791. * read_len - expected read length of the response
  1792. * *read_buf - pointer to where the response will be loaded
  1793. * write_len - length of the write buffer
  1794. * *write_buf - pointer to the write buffer
  1795. * *actual_read_len - pointer to the actual amount of data read back
  1796. ******************************************************************************/
  1797. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1798. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1799. u16 *actual_read_len)
  1800. {
  1801. int rc = 0;
  1802. u16 size;
  1803. struct pt_hid_output hid_output = {
  1804. .length = write_len,
  1805. .write_buf = write_buf,
  1806. };
  1807. #ifdef TTHE_TUNER_SUPPORT
  1808. if (!cd->pip2_send_user_cmd) {
  1809. int command_code = 0;
  1810. int len;
  1811. /* Print up to cmd ID */
  1812. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1813. if (write_len < len)
  1814. len = write_len;
  1815. else
  1816. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1817. & PIP1_CMD_COMMAND_ID_MASK;
  1818. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1819. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1820. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1821. tthe_print(cd, write_buf, len, "CMD=");
  1822. }
  1823. #endif
  1824. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1825. if (rc)
  1826. return rc;
  1827. /* Get the response size from the first 2 bytes in the response */
  1828. size = get_unaligned_le16(&cd->response_buf[0]);
  1829. /* Ensure size is not greater than max buffer size */
  1830. if (size > PT_MAX_PIP2_MSG_SIZE)
  1831. size = PT_MAX_PIP2_MSG_SIZE;
  1832. /* Minimum size to read is the 2 byte len field */
  1833. if (size == 0)
  1834. size = 2;
  1835. if (size > read_len) {
  1836. pt_debug(cd->dev, DL_ERROR,
  1837. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1838. __func__, size, read_len);
  1839. *actual_read_len = 0;
  1840. return -EIO;
  1841. }
  1842. memcpy(read_buf, cd->response_buf, size);
  1843. *actual_read_len = size;
  1844. return 0;
  1845. }
  1846. /*******************************************************************************
  1847. * FUNCTION: pt_hid_output_user_cmd
  1848. *
  1849. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1850. * the DUT.
  1851. *
  1852. * RETURN:
  1853. * 0 = success
  1854. * !0 = failure
  1855. *
  1856. * PARAMETERS:
  1857. * *cd - pointer to core data
  1858. * read_len - length of data to read
  1859. * *read_buf - pointer to store read data
  1860. * write_len - length of data to write
  1861. * *write_buf - pointer to buffer to write
  1862. * *actual_read_len - pointer to store data length actually read
  1863. ******************************************************************************/
  1864. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1865. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1866. u16 *actual_read_len)
  1867. {
  1868. int rc;
  1869. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1870. if (rc < 0) {
  1871. pt_debug(cd->dev, DL_ERROR,
  1872. "%s: fail get exclusive ex=%p own=%p\n",
  1873. __func__, cd->exclusive_dev, cd->dev);
  1874. return rc;
  1875. }
  1876. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1877. write_len, write_buf, actual_read_len);
  1878. if (release_exclusive(cd, cd->dev) < 0)
  1879. pt_debug(cd->dev, DL_ERROR,
  1880. "%s: fail to release exclusive\n", __func__);
  1881. return rc;
  1882. }
  1883. /*******************************************************************************
  1884. * FUNCTION: _pt_request_pip2_send_cmd
  1885. *
  1886. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1887. * interrupt and reads response data to read_buf
  1888. *
  1889. * RETURN:
  1890. * 0 = success
  1891. * !0 = failure
  1892. *
  1893. * PARAMETERS:
  1894. * *dev - pointer to device structure
  1895. * protect - flag to run in protected mode
  1896. * id - ID of PIP command
  1897. * *data - pointer to PIP data payload
  1898. * report_body_len - report length
  1899. * *read_buf - pointer to response buffer
  1900. * *actual_read_len - pointer to response buffer length
  1901. ******************************************************************************/
  1902. static int _pt_request_pip2_send_cmd(struct device *dev,
  1903. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1904. u16 *actual_read_len)
  1905. {
  1906. struct pt_core_data *cd = dev_get_drvdata(dev);
  1907. struct pip2_cmd_structure pip2_cmd;
  1908. int rc = 0;
  1909. u16 i = 0;
  1910. u16 j = 0;
  1911. u16 write_len;
  1912. u8 *write_buf = NULL;
  1913. u16 read_len;
  1914. u8 extra_bytes;
  1915. if (report_body_len > 247)
  1916. return -EPROTO;
  1917. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1918. /* Hard coded register for PIP2.x */
  1919. pip2_cmd.reg[0] = 0x01;
  1920. pip2_cmd.reg[1] = 0x01;
  1921. /*
  1922. * For PIP2.1+ the length field value includes itself:
  1923. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1924. *
  1925. * The overall write length must include only the register:
  1926. * ADD 2: 2 (Register)
  1927. */
  1928. extra_bytes = 6;
  1929. write_len = 2;
  1930. /* PIP2 the CMD ID is a 7bit field */
  1931. if (id > PIP2_CMD_ID_END) {
  1932. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1933. __func__, id);
  1934. rc = -EINVAL;
  1935. goto exit;
  1936. }
  1937. pip2_cmd.len = report_body_len + extra_bytes;
  1938. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1939. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1940. pip2_cmd.data = data;
  1941. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1942. /* Add the command length to the extra bytes based on PIP version */
  1943. write_len += pip2_cmd.len;
  1944. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1945. __func__, pip2_cmd.len, write_len);
  1946. write_buf = kzalloc(write_len, GFP_KERNEL);
  1947. if (write_buf == NULL) {
  1948. rc = -ENOMEM;
  1949. goto exit;
  1950. }
  1951. write_buf[i++] = pip2_cmd.reg[0];
  1952. write_buf[i++] = pip2_cmd.reg[1];
  1953. write_buf[i++] = pip2_cmd.len & 0xff;
  1954. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1955. write_buf[i++] = pip2_cmd.seq;
  1956. write_buf[i++] = pip2_cmd.id;
  1957. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1958. write_buf[j] = pip2_cmd.data[j-i];
  1959. write_buf[j++] = pip2_cmd.crc[0];
  1960. write_buf[j++] = pip2_cmd.crc[1];
  1961. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1962. if (read_len < 0)
  1963. read_len = 255;
  1964. pt_debug(dev, DL_INFO,
  1965. "%s cmd_id[0x%02X] expected response length:%d ",
  1966. __func__, pip2_cmd.id, read_len);
  1967. /*
  1968. * All PIP2 commands come through this function.
  1969. * Set flag for PIP2.x interface to allow response parsing to know
  1970. * how to decode the protocol header.
  1971. */
  1972. mutex_lock(&cd->system_lock);
  1973. cd->pip2_prot_active = true;
  1974. cd->pip2_send_user_cmd = true;
  1975. mutex_unlock(&cd->system_lock);
  1976. if (protect == PT_CORE_CMD_PROTECTED)
  1977. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1978. write_len, write_buf, actual_read_len);
  1979. else {
  1980. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1981. write_len, write_buf, actual_read_len);
  1982. }
  1983. if (rc) {
  1984. pt_debug(dev, DL_ERROR,
  1985. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1986. __func__, rc);
  1987. goto exit;
  1988. }
  1989. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1990. *actual_read_len);
  1991. exit:
  1992. mutex_lock(&cd->system_lock);
  1993. cd->pip2_prot_active = false;
  1994. cd->pip2_send_user_cmd = false;
  1995. mutex_unlock(&cd->system_lock);
  1996. kfree(write_buf);
  1997. return rc;
  1998. }
  1999. /*******************************************************************************
  2000. * FUNCTION: _pt_pip2_send_cmd_no_int
  2001. *
  2002. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2003. * reads response data to read_buf if response is available.
  2004. *
  2005. * NOTE:
  2006. * Interrupt MUST be disabled before to call this function.
  2007. *
  2008. * RETURN:
  2009. * 0 = success
  2010. * !0 = failure
  2011. *
  2012. * PARAMETERS:
  2013. * *dev - pointer to device structure
  2014. * protect - flag to run in protected mode
  2015. * id - ID of PIP command
  2016. * *data - pointer to PIP data payload
  2017. * report_body_len - report length
  2018. * *read_buf - pointer to response buffer
  2019. * *actual_read_len - pointer to response buffer length
  2020. ******************************************************************************/
  2021. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2022. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2023. u16 *actual_read_len)
  2024. {
  2025. int max_retry = 0;
  2026. int retry = 0;
  2027. int rc = 0;
  2028. u16 i = 0;
  2029. u16 j = 0;
  2030. u16 write_len;
  2031. u8 *write_buf = NULL;
  2032. u16 read_len;
  2033. u16 size = 0;
  2034. u8 response_seq = 0;
  2035. u8 extra_bytes;
  2036. u32 retry_interval = 0;
  2037. u32 retry_total_time = 0;
  2038. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2039. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2040. struct pt_core_data *cd = dev_get_drvdata(dev);
  2041. struct pip2_cmd_structure pip2_cmd;
  2042. if (report_body_len > 247)
  2043. return -EPROTO;
  2044. if (protect == PT_CORE_CMD_PROTECTED) {
  2045. rc = request_exclusive(cd,
  2046. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2047. if (rc < 0) {
  2048. pt_debug(cd->dev, DL_ERROR,
  2049. "%s: fail get exclusive ex=%p own=%p\n",
  2050. __func__, cd->exclusive_dev, cd->dev);
  2051. return rc;
  2052. }
  2053. }
  2054. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2055. /* Hard coded register for PIP2.x */
  2056. pip2_cmd.reg[0] = 0x01;
  2057. pip2_cmd.reg[1] = 0x01;
  2058. /*
  2059. * For PIP2.1+ the length field value includes itself:
  2060. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2061. *
  2062. * The overall write length must include only the register:
  2063. * ADD 2: 2 (Register)
  2064. */
  2065. extra_bytes = 6;
  2066. write_len = 2;
  2067. pip2_cmd.len = report_body_len + extra_bytes;
  2068. pip2_cmd.id = id;
  2069. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2070. pip2_cmd.data = data;
  2071. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2072. /* Add the command length to the extra bytes based on PIP version */
  2073. write_len += pip2_cmd.len;
  2074. write_buf = kzalloc(write_len, GFP_KERNEL);
  2075. if (write_buf == NULL) {
  2076. rc = -ENOMEM;
  2077. goto exit;
  2078. }
  2079. write_buf[i++] = pip2_cmd.reg[0];
  2080. write_buf[i++] = pip2_cmd.reg[1];
  2081. write_buf[i++] = pip2_cmd.len & 0xff;
  2082. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2083. write_buf[i++] = pip2_cmd.seq;
  2084. write_buf[i++] = pip2_cmd.id;
  2085. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2086. write_buf[j] = pip2_cmd.data[j-i];
  2087. write_buf[j++] = pip2_cmd.crc[0];
  2088. write_buf[j++] = pip2_cmd.crc[1];
  2089. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2090. if (read_len < 0)
  2091. read_len = 255;
  2092. pt_debug(dev, DL_INFO,
  2093. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2094. __func__, pip2_cmd.id, read_len);
  2095. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2096. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2097. if (rc) {
  2098. pt_debug(dev, DL_ERROR,
  2099. "%s: SPI write Error = %d\n",
  2100. __func__, rc);
  2101. goto exit;
  2102. }
  2103. #ifdef PT_POLL_RESP_BY_BUS
  2104. /*
  2105. * Frequent bus read can increase system load obviously. The expected
  2106. * first bus read should be valid and timely. The tollerance for
  2107. * usleep_range should be limited. The minimum response delay (between
  2108. * command finishes sending and INT pin falls) is less than 50
  2109. * microseconds. So the 10 microseconds should be maximum tollerance
  2110. * with the consideration that the unit to calculate the response delay
  2111. * is 10 microseconds and more precise is not necessary. Every
  2112. * additional 10 microseconds only contribute less than 3 milliseconds
  2113. * for whole BL.
  2114. */
  2115. usleep_range(resp_time_min, resp_time_min+10);
  2116. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2117. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2118. rc = pt_adap_read_default(cd, read_buf, read_len);
  2119. if (rc) {
  2120. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2121. __func__, rc);
  2122. break;
  2123. }
  2124. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2125. size = get_unaligned_le16(&read_buf[0]);
  2126. if ((size <= read_len) &&
  2127. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2128. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2129. break;
  2130. }
  2131. /*
  2132. * To reduce the bus and system load, increase the sleep
  2133. * step gradually:
  2134. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2135. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2136. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2137. */
  2138. retry++;
  2139. if (retry < 20) {
  2140. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2141. usleep_range(retry_interval,
  2142. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2143. } else if (retry < 40) {
  2144. retry_interval += 1000;
  2145. usleep_range(retry_interval, retry_interval + 1000);
  2146. } else {
  2147. retry_interval += 50000;
  2148. msleep(retry_interval/1000);
  2149. }
  2150. retry_total_time += retry_interval;
  2151. }
  2152. #else
  2153. /*
  2154. * Frequent GPIO read will not increase CPU/system load heavily if the
  2155. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2156. * fixed interval: 20 us.
  2157. */
  2158. usleep_range(resp_time_min, resp_time_min+10);
  2159. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2160. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2161. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2162. rc = pt_adap_read_default(cd, read_buf, read_len);
  2163. size = get_unaligned_le16(&read_buf[0]);
  2164. if (rc)
  2165. pt_debug(dev, DL_ERROR,
  2166. "%s: SPI read Error = %d\n",
  2167. __func__, rc);
  2168. else if (size > read_len) {
  2169. pt_debug(cd->dev, DL_ERROR,
  2170. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2171. __func__, size, read_len);
  2172. rc = -EIO;
  2173. }
  2174. break;
  2175. }
  2176. /*
  2177. * Poll GPIO with fixed interval 20 us, and tollerance is
  2178. * limited to 10 us to speed up the process.
  2179. */
  2180. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2181. usleep_range(retry_interval, retry_interval+10);
  2182. retry_total_time += retry_interval;
  2183. }
  2184. #endif
  2185. *actual_read_len = size;
  2186. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2187. pt_debug(dev, DL_ERROR,
  2188. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2189. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2190. *actual_read_len = 0;
  2191. rc = -EINVAL;
  2192. }
  2193. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2194. "<<< NO_INT Read");
  2195. exit:
  2196. kfree(write_buf);
  2197. if (protect == PT_CORE_CMD_PROTECTED) {
  2198. if (release_exclusive(cd, cd->dev) < 0)
  2199. pt_debug(cd->dev, DL_ERROR,
  2200. "%s: fail to release exclusive\n", __func__);
  2201. }
  2202. return rc;
  2203. }
  2204. /*******************************************************************************
  2205. * FUNCTION: pt_pip_null_
  2206. *
  2207. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2208. * This function is used by watchdog to check if the fw corrupts.
  2209. *
  2210. * RETURN:
  2211. * 0 = success
  2212. * !0 = failure
  2213. *
  2214. * PARAMETERS:
  2215. * *cd - pointer to core data
  2216. ******************************************************************************/
  2217. static int pt_pip_null_(struct pt_core_data *cd)
  2218. {
  2219. struct pt_hid_output hid_output = {
  2220. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2221. };
  2222. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2223. }
  2224. /*******************************************************************************
  2225. * FUNCTION: pt_pip_null
  2226. *
  2227. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2228. *
  2229. * RETURN:
  2230. * 0 = success
  2231. * !0 = failure
  2232. *
  2233. * PARAMETERS:
  2234. * *cd - pointer to core data
  2235. ******************************************************************************/
  2236. static int pt_pip_null(struct pt_core_data *cd)
  2237. {
  2238. int rc;
  2239. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2240. if (rc < 0) {
  2241. pt_debug(cd->dev, DL_ERROR,
  2242. "%s: fail get exclusive ex=%p own=%p\n",
  2243. __func__, cd->exclusive_dev, cd->dev);
  2244. return rc;
  2245. }
  2246. rc = pt_pip_null_(cd);
  2247. if (release_exclusive(cd, cd->dev) < 0)
  2248. pt_debug(cd->dev, DL_ERROR,
  2249. "%s: fail to release exclusive\n", __func__);
  2250. return rc;
  2251. }
  2252. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2253. /*******************************************************************************
  2254. * FUNCTION: pt_pip_start_bootloader_
  2255. *
  2256. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2257. *
  2258. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2259. * the WD active could cause this function to fail!
  2260. *
  2261. * RETURN:
  2262. * 0 = success
  2263. * !0 = failure
  2264. *
  2265. * PARAMETERS:
  2266. * *cd - pointer to core data
  2267. ******************************************************************************/
  2268. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2269. {
  2270. int rc = 0;
  2271. struct pt_hid_output hid_output = {
  2272. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2273. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2274. .reset_expected = 1,
  2275. };
  2276. if (cd->watchdog_enabled) {
  2277. pt_debug(cd->dev, DL_WARN,
  2278. "%s: watchdog isn't stopped before enter bl\n",
  2279. __func__);
  2280. goto exit;
  2281. }
  2282. /* Reset startup status after entering BL, new DUT enum required */
  2283. cd->startup_status = STARTUP_STATUS_START;
  2284. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2285. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2286. if (rc) {
  2287. pt_debug(cd->dev, DL_ERROR,
  2288. "%s: Start BL PIP cmd failed. rc = %d\n",
  2289. __func__, rc);
  2290. }
  2291. exit:
  2292. return rc;
  2293. }
  2294. /*******************************************************************************
  2295. * FUNCTION: pt_pip_start_bootloader
  2296. *
  2297. * SUMMARY: Protected function to force DUT to enter the BL
  2298. *
  2299. * RETURN:
  2300. * 0 = success
  2301. * !0 = failure
  2302. *
  2303. * PARAMETERS:
  2304. * *cd - pointer to core data structure
  2305. ******************************************************************************/
  2306. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2307. {
  2308. int rc;
  2309. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2310. if (rc < 0) {
  2311. pt_debug(cd->dev, DL_ERROR,
  2312. "%s: fail get exclusive ex=%p own=%p\n",
  2313. __func__, cd->exclusive_dev, cd->dev);
  2314. return rc;
  2315. }
  2316. rc = pt_pip_start_bootloader_(cd);
  2317. if (release_exclusive(cd, cd->dev) < 0)
  2318. pt_debug(cd->dev, DL_ERROR,
  2319. "%s: fail to release exclusive\n", __func__);
  2320. return rc;
  2321. }
  2322. /*******************************************************************************
  2323. * FUNCTION: _pt_request_pip_start_bl
  2324. *
  2325. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2326. * modules to request the DUT to enter the BL
  2327. *
  2328. * RETURN:
  2329. * 0 = success
  2330. * !0 = failure
  2331. *
  2332. * PARAMETERS:
  2333. * *dev - pointer to device structure
  2334. * protect - flag to run in protected mode
  2335. ******************************************************************************/
  2336. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2337. {
  2338. struct pt_core_data *cd = dev_get_drvdata(dev);
  2339. if (protect)
  2340. return pt_pip_start_bootloader(cd);
  2341. return pt_pip_start_bootloader_(cd);
  2342. }
  2343. /*******************************************************************************
  2344. * FUNCTION: pt_pip2_ver_load_ttdata
  2345. *
  2346. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2347. * command into the core data struct.
  2348. *
  2349. * RETURN: n/a
  2350. *
  2351. * PARAMETERS:
  2352. * *cd - pointer to core data structure
  2353. * len - Length of data in response_buf
  2354. ******************************************************************************/
  2355. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2356. {
  2357. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2358. struct pt_pip2_version_full *full_ver;
  2359. struct pt_pip2_version *ver;
  2360. /*
  2361. * The PIP2 VERSION command can return different lengths of data.
  2362. * The additional LOT fields are included when the packet
  2363. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2364. * NOTE:
  2365. * - The FW would swap the BL and FW versions when reporting
  2366. * the small packet.
  2367. * - Sub Lot bytes 16 and 17 are reserved.
  2368. */
  2369. if (len >= 0x1D) {
  2370. full_ver = (struct pt_pip2_version_full *)
  2371. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2372. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2373. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2374. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2375. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2376. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2377. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2378. /*
  2379. * BL PIP 2.02 and greater the version fields are
  2380. * swapped
  2381. */
  2382. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2383. ttdata->chip_rev =
  2384. get_unaligned_le16(&full_ver->chip_rev);
  2385. ttdata->chip_id =
  2386. get_unaligned_le16(&full_ver->chip_id);
  2387. } else {
  2388. ttdata->chip_rev =
  2389. get_unaligned_le16(&full_ver->chip_id);
  2390. ttdata->chip_id =
  2391. get_unaligned_le16(&full_ver->chip_rev);
  2392. }
  2393. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2394. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2395. sizeof(struct pt_pip2_version_full),
  2396. "PIP2 VERSION FULL");
  2397. } else {
  2398. ver = (struct pt_pip2_version *)
  2399. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2400. ttdata->pip_ver_major = ver->pip2_version_msb;
  2401. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2402. ttdata->bl_ver_major = ver->bl_version_msb;
  2403. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2404. ttdata->fw_ver_major = ver->fw_version_msb;
  2405. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2406. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2407. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2408. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2409. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2410. }
  2411. }
  2412. /*******************************************************************************
  2413. * FUNCTION: pt_si_get_ttdata
  2414. *
  2415. * SUMMARY: Function to load the version information from the system information
  2416. * PIP command into the core data struct.
  2417. *
  2418. * RETURN: n/a
  2419. *
  2420. * PARAMETERS:
  2421. * *cd - pointer to core data structure
  2422. ******************************************************************************/
  2423. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2424. {
  2425. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2426. struct pt_ttdata_dev *ttdata_dev =
  2427. (struct pt_ttdata_dev *)
  2428. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2429. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2430. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2431. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2432. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2433. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2434. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2435. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2436. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2437. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2438. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2439. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2440. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2441. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2442. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2443. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2444. }
  2445. /*******************************************************************************
  2446. * FUNCTION: pt_si_get_sensing_conf_data
  2447. *
  2448. * SUMMARY: Function to load the sensing information from the system information
  2449. * PIP command into the core data struct.
  2450. *
  2451. * RETURN: n/a
  2452. *
  2453. * PARAMETERS:
  2454. * *cd - pointer to core data structure
  2455. ******************************************************************************/
  2456. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2457. {
  2458. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2459. struct pt_sensing_conf_data_dev *scd_dev =
  2460. (struct pt_sensing_conf_data_dev *)
  2461. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2462. scd->electrodes_x = scd_dev->electrodes_x;
  2463. scd->electrodes_y = scd_dev->electrodes_y;
  2464. scd->origin_x = scd_dev->origin_x;
  2465. scd->origin_y = scd_dev->origin_y;
  2466. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2467. if (scd->origin_x & 0x02) {
  2468. scd->tx_num = scd->electrodes_x;
  2469. scd->rx_num = scd->electrodes_y;
  2470. } else {
  2471. scd->tx_num = scd->electrodes_y;
  2472. scd->rx_num = scd->electrodes_x;
  2473. }
  2474. /*
  2475. * When the Panel ID is coming from an XY pin and not a dedicated
  2476. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2477. * other DUTs as the loader will use cd->pid_for_loader to generate
  2478. * the bin file name but will ignore it if pid_for_loader is still
  2479. * set to PANEL_ID_NOT_ENABLED
  2480. */
  2481. if (cd->panel_id_support &
  2482. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2483. mutex_lock(&cd->system_lock);
  2484. cd->pid_for_loader = scd_dev->panel_id;
  2485. mutex_unlock(&cd->system_lock);
  2486. }
  2487. scd->panel_id = scd_dev->panel_id;
  2488. scd->btn = scd_dev->btn;
  2489. scd->scan_mode = scd_dev->scan_mode;
  2490. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2491. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2492. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2493. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2494. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2495. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2496. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2497. sizeof(struct pt_sensing_conf_data_dev),
  2498. "sensing_conf_data");
  2499. }
  2500. /*******************************************************************************
  2501. * FUNCTION: pt_si_setup
  2502. *
  2503. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2504. *
  2505. * RETURN:
  2506. * 0 = success
  2507. * !0 = failure
  2508. *
  2509. * PARAMETERS:
  2510. * *cd - pointer to core data structure
  2511. ******************************************************************************/
  2512. static int pt_si_setup(struct pt_core_data *cd)
  2513. {
  2514. struct pt_sysinfo *si = &cd->sysinfo;
  2515. int max_tch = si->sensing_conf_data.max_tch;
  2516. if (!si->xy_data)
  2517. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2518. GFP_KERNEL);
  2519. if (!si->xy_data)
  2520. return -ENOMEM;
  2521. if (!si->xy_mode)
  2522. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2523. if (!si->xy_mode) {
  2524. kfree(si->xy_data);
  2525. return -ENOMEM;
  2526. }
  2527. return 0;
  2528. }
  2529. /*******************************************************************************
  2530. * FUNCTION: pt_si_get_btn_data
  2531. *
  2532. * SUMMARY: Setup the core data button information based on the response of the
  2533. * System Information PIP command.
  2534. *
  2535. * RETURN:
  2536. * 0 = success
  2537. * !0 = failure
  2538. *
  2539. * PARAMETERS:
  2540. * *cd - pointer to core data structure
  2541. ******************************************************************************/
  2542. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2543. {
  2544. struct pt_sysinfo *si = &cd->sysinfo;
  2545. int num_btns = 0;
  2546. int num_defined_keys;
  2547. u16 *key_table;
  2548. int btn;
  2549. int i;
  2550. int rc = 0;
  2551. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2552. & PIP1_SYSINFO_BTN_MASK;
  2553. size_t btn_keys_size;
  2554. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2555. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2556. if (btns & (1 << i))
  2557. num_btns++;
  2558. }
  2559. si->num_btns = num_btns;
  2560. if (num_btns) {
  2561. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2562. if (!si->btn)
  2563. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2564. if (!si->btn)
  2565. return -ENOMEM;
  2566. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2567. num_defined_keys = 0;
  2568. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2569. num_defined_keys = 0;
  2570. else
  2571. num_defined_keys = cd->cpdata->sett
  2572. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2573. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2574. key_table = (u16 *)cd->cpdata->sett
  2575. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2576. si->btn[btn].key_code = key_table[btn];
  2577. si->btn[btn].enabled = true;
  2578. }
  2579. for (; btn < num_btns; btn++) {
  2580. si->btn[btn].key_code = KEY_RESERVED;
  2581. si->btn[btn].enabled = true;
  2582. }
  2583. return rc;
  2584. }
  2585. kfree(si->btn);
  2586. si->btn = NULL;
  2587. return rc;
  2588. }
  2589. /*******************************************************************************
  2590. * FUNCTION: pt_si_put_log_data
  2591. *
  2592. * SUMMARY: Prints all sys info data to kmsg log
  2593. *
  2594. * RETURN: n/a
  2595. *
  2596. * PARAMETERS:
  2597. * *cd - pointer to core data structure
  2598. ******************************************************************************/
  2599. static void pt_si_put_log_data(struct pt_core_data *cd)
  2600. {
  2601. struct pt_sysinfo *si = &cd->sysinfo;
  2602. struct pt_ttdata *ttdata = &si->ttdata;
  2603. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2604. int i;
  2605. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2606. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2608. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2610. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2611. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2612. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2614. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2616. __func__, ttdata->revctrl, ttdata->revctrl);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2618. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2620. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2622. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2624. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2626. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2627. for (i = 0; i < PT_NUM_MFGID; i++)
  2628. pt_debug(cd->dev, DL_DEBUG,
  2629. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2630. __func__, i, ttdata->mfg_id[i],
  2631. ttdata->mfg_id[i]);
  2632. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2633. __func__, ttdata->post_code, ttdata->post_code);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2635. __func__, scd->electrodes_x, scd->electrodes_x);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2637. __func__, scd->electrodes_y, scd->electrodes_y);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2639. __func__, scd->len_x, scd->len_x);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2641. __func__, scd->len_y, scd->len_y);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2643. __func__, scd->res_x, scd->res_x);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2645. __func__, scd->res_y, scd->res_y);
  2646. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2647. __func__, scd->max_z, scd->max_z);
  2648. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2649. __func__, scd->origin_x, scd->origin_x);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2651. __func__, scd->origin_y, scd->origin_y);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2653. __func__, scd->panel_id, scd->panel_id);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2655. __func__, scd->btn, scd->btn);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2657. __func__, scd->scan_mode, scd->scan_mode);
  2658. pt_debug(cd->dev, DL_DEBUG,
  2659. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2660. __func__, scd->max_tch, scd->max_tch);
  2661. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2662. __func__, si->xy_mode);
  2663. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2664. __func__, si->xy_data);
  2665. }
  2666. /*******************************************************************************
  2667. * FUNCTION: pt_get_sysinfo_regs
  2668. *
  2669. * SUMMARY: Setup all the core data System information based on the response
  2670. * of the System Information PIP command.
  2671. *
  2672. * RETURN:
  2673. * 0 = success
  2674. * !0 = failure
  2675. *
  2676. * PARAMETERS:
  2677. * *cd - pointer to core data structure
  2678. ******************************************************************************/
  2679. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2680. {
  2681. struct pt_sysinfo *si = &cd->sysinfo;
  2682. int rc;
  2683. rc = pt_si_get_btn_data(cd);
  2684. if (rc < 0)
  2685. return rc;
  2686. pt_si_get_ttdata(cd);
  2687. pt_si_get_sensing_conf_data(cd);
  2688. pt_si_setup(cd);
  2689. pt_si_put_log_data(cd);
  2690. si->ready = true;
  2691. return rc;
  2692. }
  2693. /*******************************************************************************
  2694. * FUNCTION: pt_free_si_ptrs
  2695. *
  2696. * SUMMARY: Frees all memory associated with the System Information within
  2697. * core data
  2698. *
  2699. * RETURN: n/a
  2700. *
  2701. * PARAMETERS:
  2702. * *cd - pointer to core data structure
  2703. ******************************************************************************/
  2704. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2705. {
  2706. struct pt_sysinfo *si = &cd->sysinfo;
  2707. kfree(si->btn);
  2708. kfree(si->xy_mode);
  2709. kfree(si->xy_data);
  2710. }
  2711. /*******************************************************************************
  2712. * FUNCTION: pt_hid_output_get_sysinfo_
  2713. *
  2714. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2715. * response.
  2716. *
  2717. * RETURN::
  2718. * 0 = success
  2719. * !0 = failure
  2720. *
  2721. * PARAMETERS:
  2722. * *cd - pointer to core data structure
  2723. ******************************************************************************/
  2724. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2725. {
  2726. int rc = 0;
  2727. struct pt_hid_output hid_output = {
  2728. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2729. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2730. };
  2731. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2732. if (rc)
  2733. return rc;
  2734. /* Parse the sysinfo data */
  2735. rc = pt_get_sysinfo_regs(cd);
  2736. if (rc)
  2737. pt_free_si_ptrs(cd);
  2738. return rc;
  2739. }
  2740. /*******************************************************************************
  2741. * FUNCTION: pt_hid_output_get_sysinfo
  2742. *
  2743. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2744. *
  2745. * RETURN::
  2746. * 0 = success
  2747. * !0 = failure
  2748. *
  2749. * PARAMETERS:
  2750. * *cd - pointer to core data structure
  2751. ******************************************************************************/
  2752. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2753. {
  2754. int rc;
  2755. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2756. if (rc < 0) {
  2757. pt_debug(cd->dev, DL_ERROR,
  2758. "%s: fail get exclusive ex=%p own=%p\n",
  2759. __func__, cd->exclusive_dev, cd->dev);
  2760. return rc;
  2761. }
  2762. rc = pt_hid_output_get_sysinfo_(cd);
  2763. if (release_exclusive(cd, cd->dev) < 0)
  2764. pt_debug(cd->dev, DL_ERROR,
  2765. "%s: fail to release exclusive\n", __func__);
  2766. return rc;
  2767. }
  2768. /*******************************************************************************
  2769. * FUNCTION: pt_pip_suspend_scanning_
  2770. *
  2771. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2772. *
  2773. * RETURN::
  2774. * 0 = success
  2775. * !0 = failure
  2776. *
  2777. * PARAMETERS:
  2778. * *cd - pointer to core data structure
  2779. ******************************************************************************/
  2780. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2781. {
  2782. int rc = 0;
  2783. struct pt_hid_output hid_output = {
  2784. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2785. };
  2786. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2787. if (rc) {
  2788. pt_debug(cd->dev, DL_ERROR,
  2789. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2790. __func__, rc);
  2791. }
  2792. return rc;
  2793. }
  2794. /*******************************************************************************
  2795. * FUNCTION: pt_pip_suspend_scanning
  2796. *
  2797. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2798. *
  2799. * RETURN::
  2800. * 0 = success
  2801. * !0 = failure
  2802. *
  2803. * PARAMETERS:
  2804. * *cd - pointer to core data structure
  2805. ******************************************************************************/
  2806. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2807. {
  2808. int rc;
  2809. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2810. if (rc < 0) {
  2811. pt_debug(cd->dev, DL_ERROR,
  2812. "%s: fail get exclusive ex=%p own=%p\n",
  2813. __func__, cd->exclusive_dev, cd->dev);
  2814. return rc;
  2815. }
  2816. rc = pt_pip_suspend_scanning_(cd);
  2817. if (release_exclusive(cd, cd->dev) < 0)
  2818. pt_debug(cd->dev, DL_ERROR,
  2819. "%s: fail to release exclusive\n", __func__);
  2820. return rc;
  2821. }
  2822. /*******************************************************************************
  2823. * FUNCTION: _pt_request_pip_suspend_scanning
  2824. *
  2825. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2826. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2827. *
  2828. * RETURN:
  2829. * 0 = success
  2830. * !0 = failure
  2831. *
  2832. * PARAMETERS:
  2833. * *dev - pointer to device structure
  2834. * protect - 0 = call non-protected function
  2835. * 1 = call protected function
  2836. ******************************************************************************/
  2837. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2838. int protect)
  2839. {
  2840. struct pt_core_data *cd = dev_get_drvdata(dev);
  2841. if (protect)
  2842. return pt_pip_suspend_scanning(cd);
  2843. return pt_pip_suspend_scanning_(cd);
  2844. }
  2845. /*******************************************************************************
  2846. * FUNCTION: pt_pip_resume_scanning_
  2847. *
  2848. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2849. *
  2850. * RETURN::
  2851. * 0 = success
  2852. * !0 = failure
  2853. *
  2854. * PARAMETERS:
  2855. * *cd - pointer to core data structure
  2856. ******************************************************************************/
  2857. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2858. {
  2859. int rc = 0;
  2860. struct pt_hid_output hid_output = {
  2861. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2862. };
  2863. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2864. if (rc) {
  2865. pt_debug(cd->dev, DL_ERROR,
  2866. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2867. __func__, rc);
  2868. }
  2869. return rc;
  2870. }
  2871. /*******************************************************************************
  2872. * FUNCTION: pt_pip_resume_scanning
  2873. *
  2874. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2875. *
  2876. * RETURN::
  2877. * 0 = success
  2878. * !0 = failure
  2879. *
  2880. * PARAMETERS:
  2881. * *cd - pointer to core data structure
  2882. ******************************************************************************/
  2883. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2884. {
  2885. int rc;
  2886. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2887. if (rc < 0) {
  2888. pt_debug(cd->dev, DL_ERROR,
  2889. "%s: fail get exclusive ex=%p own=%p\n",
  2890. __func__, cd->exclusive_dev, cd->dev);
  2891. return rc;
  2892. }
  2893. rc = pt_pip_resume_scanning_(cd);
  2894. if (release_exclusive(cd, cd->dev) < 0)
  2895. pt_debug(cd->dev, DL_ERROR,
  2896. "%s: fail to release exclusive\n", __func__);
  2897. return rc;
  2898. }
  2899. /*******************************************************************************
  2900. * FUNCTION: _pt_request_pip_resume_scanning
  2901. *
  2902. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2903. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2904. *
  2905. * RETURN::
  2906. * 0 = success
  2907. * !0 = failure
  2908. *
  2909. * PARAMETERS:
  2910. * *dev - pointer to device structure
  2911. * protect - 0 = call non-protected function
  2912. * 1 = call protected function
  2913. ******************************************************************************/
  2914. static int _pt_request_pip_resume_scanning(struct device *dev,
  2915. int protect)
  2916. {
  2917. struct pt_core_data *cd = dev_get_drvdata(dev);
  2918. if (protect)
  2919. return pt_pip_resume_scanning(cd);
  2920. return pt_pip_resume_scanning_(cd);
  2921. }
  2922. /*******************************************************************************
  2923. * FUNCTION: pt_pip_get_param_
  2924. *
  2925. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2926. * the 32bit parameter value
  2927. *
  2928. * RETURN:
  2929. * 0 = success
  2930. * !0 = failure
  2931. *
  2932. * PARAMETERS:
  2933. * *cd - pointer to core data
  2934. * param_id - parameter ID to retrieve
  2935. * *value - value of DUT parameter
  2936. ******************************************************************************/
  2937. static int pt_pip_get_param_(struct pt_core_data *cd,
  2938. u8 param_id, u32 *value)
  2939. {
  2940. int write_length = 1;
  2941. u8 param[1] = { param_id };
  2942. u8 read_param_id;
  2943. int param_size;
  2944. u8 *ptr;
  2945. int rc = 0;
  2946. int i;
  2947. struct pt_hid_output hid_output = {
  2948. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2949. .write_length = write_length,
  2950. .write_buf = param,
  2951. };
  2952. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2953. if (rc)
  2954. return rc;
  2955. read_param_id = cd->response_buf[5];
  2956. if (read_param_id != param_id)
  2957. return -EPROTO;
  2958. param_size = cd->response_buf[6];
  2959. ptr = &cd->response_buf[7];
  2960. *value = 0;
  2961. for (i = 0; i < param_size; i++)
  2962. *value += ptr[i] << (i * 8);
  2963. return 0;
  2964. }
  2965. /*******************************************************************************
  2966. * FUNCTION: pt_pip_get_param
  2967. *
  2968. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2969. *
  2970. * RETURN:
  2971. * 0 = success
  2972. * !0 = failure
  2973. *
  2974. * PARAMETERS:
  2975. * *cd - pointer to core data
  2976. * param_id - parameter ID to retrieve
  2977. * *value - value of DUT parameter
  2978. ******************************************************************************/
  2979. static int pt_pip_get_param(struct pt_core_data *cd,
  2980. u8 param_id, u32 *value)
  2981. {
  2982. int rc;
  2983. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2984. if (rc < 0) {
  2985. pt_debug(cd->dev, DL_ERROR,
  2986. "%s: fail get exclusive ex=%p own=%p\n",
  2987. __func__, cd->exclusive_dev, cd->dev);
  2988. return rc;
  2989. }
  2990. rc = pt_pip_get_param_(cd, param_id, value);
  2991. if (release_exclusive(cd, cd->dev) < 0)
  2992. pt_debug(cd->dev, DL_ERROR,
  2993. "%s: fail to release exclusive\n", __func__);
  2994. return rc;
  2995. }
  2996. /*******************************************************************************
  2997. * FUNCTION: _pt_request_pip_get_param
  2998. *
  2999. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3000. * calls to the protected or unprotected call to pt_pip_get_param
  3001. *
  3002. * RETURN:
  3003. * 0 = success
  3004. * !0 = failure
  3005. *
  3006. * PARAMETERS:
  3007. * *dev - pointer to device structure
  3008. * protect - flag to call protected or non protected function
  3009. * param_id - parameter ID to retrieve
  3010. * *value - value of DUT parameter
  3011. ******************************************************************************/
  3012. int _pt_request_pip_get_param(struct device *dev,
  3013. int protect, u8 param_id, u32 *value)
  3014. {
  3015. struct pt_core_data *cd = dev_get_drvdata(dev);
  3016. if (protect)
  3017. return pt_pip_get_param(cd, param_id, value);
  3018. return pt_pip_get_param_(cd, param_id, value);
  3019. }
  3020. /*******************************************************************************
  3021. * FUNCTION: pt_pip_set_param_
  3022. *
  3023. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3024. * passed in value to flash
  3025. *
  3026. * RETURN:
  3027. * 0 = success
  3028. * !0 = failure
  3029. *
  3030. * PARAMETERS:
  3031. * *cd - pointer to core data
  3032. * param_id - parameter ID to set
  3033. * value - value to write
  3034. * size - size to write
  3035. ******************************************************************************/
  3036. static int pt_pip_set_param_(struct pt_core_data *cd,
  3037. u8 param_id, u32 value, u8 size)
  3038. {
  3039. u8 write_buf[6];
  3040. u8 *ptr = &write_buf[2];
  3041. int rc = 0;
  3042. int i;
  3043. struct pt_hid_output hid_output = {
  3044. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3045. .write_buf = write_buf,
  3046. };
  3047. write_buf[0] = param_id;
  3048. write_buf[1] = size;
  3049. for (i = 0; i < size; i++) {
  3050. ptr[i] = value & 0xFF;
  3051. value = value >> 8;
  3052. }
  3053. hid_output.write_length = 2 + size;
  3054. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3055. if (rc)
  3056. return rc;
  3057. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3058. return -EPROTO;
  3059. return 0;
  3060. }
  3061. /*******************************************************************************
  3062. * FUNCTION: pt_pip_set_param
  3063. *
  3064. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3065. *
  3066. * RETURN:
  3067. * 0 = success
  3068. * !0 = failure
  3069. *
  3070. * PARAMETERS:
  3071. * *cd - pointer to core data
  3072. * param_id - parameter ID to set
  3073. * value - value to write
  3074. * size - size to write
  3075. ******************************************************************************/
  3076. static int pt_pip_set_param(struct pt_core_data *cd,
  3077. u8 param_id, u32 value, u8 size)
  3078. {
  3079. int rc;
  3080. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3081. if (rc < 0) {
  3082. pt_debug(cd->dev, DL_ERROR,
  3083. "%s: fail get exclusive ex=%p own=%p\n",
  3084. __func__, cd->exclusive_dev, cd->dev);
  3085. return rc;
  3086. }
  3087. rc = pt_pip_set_param_(cd, param_id, value, size);
  3088. if (release_exclusive(cd, cd->dev) < 0)
  3089. pt_debug(cd->dev, DL_ERROR,
  3090. "%s: fail to release exclusive\n", __func__);
  3091. return rc;
  3092. }
  3093. /*******************************************************************************
  3094. * FUNCTION: _pt_request_pip_set_param
  3095. *
  3096. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3097. * calls to the protected or unprotected call to pt_pip_set_param
  3098. *
  3099. * RETURN:
  3100. * 0 = success
  3101. * !0 = failure
  3102. *
  3103. * PARAMETERS:
  3104. * *dev - pointer to device structure
  3105. * protect - flag to call protected or non-protected
  3106. * param_id - parameter ID to set
  3107. * value - value to write
  3108. * size - size to write
  3109. ******************************************************************************/
  3110. int _pt_request_pip_set_param(struct device *dev, int protect,
  3111. u8 param_id, u32 value, u8 size)
  3112. {
  3113. struct pt_core_data *cd = dev_get_drvdata(dev);
  3114. if (protect)
  3115. return pt_pip_set_param(cd, param_id, value, size);
  3116. return pt_pip_set_param_(cd, param_id, value, size);
  3117. }
  3118. /*******************************************************************************
  3119. * FUNCTION: _pt_pip_enter_easywake_state_
  3120. *
  3121. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3122. *
  3123. * RETURN:
  3124. * 0 = success
  3125. * !0 = failure
  3126. *
  3127. * PARAMETERS:
  3128. * *dev - pointer to device structure
  3129. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3130. * *return_data - return status if easywake was entered
  3131. ******************************************************************************/
  3132. static int pt_hid_output_enter_easywake_state_(
  3133. struct pt_core_data *cd, u8 data, u8 *return_data)
  3134. {
  3135. int write_length = 1;
  3136. u8 param[1] = { data };
  3137. int rc = 0;
  3138. struct pt_hid_output hid_output = {
  3139. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3140. .write_length = write_length,
  3141. .write_buf = param,
  3142. };
  3143. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3144. if (rc)
  3145. return rc;
  3146. *return_data = cd->response_buf[5];
  3147. return rc;
  3148. }
  3149. /*******************************************************************************
  3150. * FUNCTION: pt_pip_verify_config_block_crc_
  3151. *
  3152. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3153. *
  3154. * RETURN:
  3155. * 0 = success
  3156. * !0 = failure
  3157. *
  3158. * PARAMETERS:
  3159. * *cd - pointer the core data structure
  3160. * ebid - enumerated block ID
  3161. * *status - PIP command status
  3162. * calculated_crc - calculated CRC
  3163. * stored_crc - stored CRC in config area
  3164. ******************************************************************************/
  3165. static int pt_pip_verify_config_block_crc_(
  3166. struct pt_core_data *cd, u8 ebid, u8 *status,
  3167. u16 *calculated_crc, u16 *stored_crc)
  3168. {
  3169. int write_length = 1;
  3170. u8 param[1] = { ebid };
  3171. u8 *ptr;
  3172. int rc = 0;
  3173. struct pt_hid_output hid_output = {
  3174. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3175. .write_length = write_length,
  3176. .write_buf = param,
  3177. };
  3178. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3179. if (rc)
  3180. return rc;
  3181. ptr = &cd->response_buf[5];
  3182. *status = ptr[0];
  3183. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3184. *stored_crc = get_unaligned_le16(&ptr[3]);
  3185. return 0;
  3186. }
  3187. /*******************************************************************************
  3188. * FUNCTION: pt_pip_verify_config_block_crc
  3189. *
  3190. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3191. * an exclusive access to the DUT.
  3192. *
  3193. * RETURN:
  3194. * 0 = success
  3195. * !0 = failure
  3196. *
  3197. * PARAMETERS:
  3198. * *cd - pointer the core data structure
  3199. * ebid - enumerated block ID
  3200. * *status - PIP command status
  3201. * calculated_crc - calculated CRC
  3202. * stored_crc - stored CRC in config area
  3203. ******************************************************************************/
  3204. static int pt_pip_verify_config_block_crc(
  3205. struct pt_core_data *cd, u8 ebid, u8 *status,
  3206. u16 *calculated_crc, u16 *stored_crc)
  3207. {
  3208. int rc;
  3209. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3210. if (rc < 0) {
  3211. pt_debug(cd->dev, DL_ERROR,
  3212. "%s: fail get exclusive ex=%p own=%p\n",
  3213. __func__, cd->exclusive_dev, cd->dev);
  3214. return rc;
  3215. }
  3216. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3217. calculated_crc, stored_crc);
  3218. if (release_exclusive(cd, cd->dev) < 0)
  3219. pt_debug(cd->dev, DL_ERROR,
  3220. "%s: fail to release exclusive\n", __func__);
  3221. return rc;
  3222. }
  3223. /*******************************************************************************
  3224. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3225. *
  3226. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3227. * calls to the protected or unprotected call to
  3228. * pt_pip_verify_config_block_crc_
  3229. *
  3230. * RETURN:
  3231. * 0 = success
  3232. * !0 = failure
  3233. *
  3234. * PARAMETERS:
  3235. * *dev - pointer to device structure
  3236. * protect - flag to call protected or non-protected
  3237. * ebid - enumerated block ID
  3238. * *status - PIP command status
  3239. * calculated_crc - calculated CRC
  3240. * stored_crc - stored CRC in config area
  3241. ******************************************************************************/
  3242. static int _pt_request_pip_verify_config_block_crc(
  3243. struct device *dev, int protect, u8 ebid, u8 *status,
  3244. u16 *calculated_crc, u16 *stored_crc)
  3245. {
  3246. struct pt_core_data *cd = dev_get_drvdata(dev);
  3247. if (protect)
  3248. return pt_pip_verify_config_block_crc(cd, ebid,
  3249. status, calculated_crc, stored_crc);
  3250. return pt_pip_verify_config_block_crc_(cd, ebid,
  3251. status, calculated_crc, stored_crc);
  3252. }
  3253. /*******************************************************************************
  3254. * FUNCTION: pt_pip_get_config_row_size_
  3255. *
  3256. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3257. *
  3258. * RETURN:
  3259. * 0 = success
  3260. * !0 = failure
  3261. *
  3262. * PARAMETERS:
  3263. * *cd - pointer to core data
  3264. * protect - flag to call protected or non-protected
  3265. * *row_size - pointer to store the retrieved row size
  3266. ******************************************************************************/
  3267. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3268. u16 *row_size)
  3269. {
  3270. int rc = 0;
  3271. struct pt_hid_output hid_output = {
  3272. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3273. };
  3274. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3275. if (rc)
  3276. return rc;
  3277. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3278. return 0;
  3279. }
  3280. /*******************************************************************************
  3281. * FUNCTION: pt_pip_get_config_row_size
  3282. *
  3283. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3284. * an exclusive access to the DUT.
  3285. *
  3286. * RETURN:
  3287. * 0 = success
  3288. * !0 = failure
  3289. *
  3290. * PARAMETERS:
  3291. * *cd - pointer to core data
  3292. * protect - flag to call protected or non-protected
  3293. * *row_size - pointer to store the retrieved row size
  3294. ******************************************************************************/
  3295. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3296. u16 *row_size)
  3297. {
  3298. int rc;
  3299. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3300. if (rc < 0) {
  3301. pt_debug(cd->dev, DL_ERROR,
  3302. "%s: fail get exclusive ex=%p own=%p\n",
  3303. __func__, cd->exclusive_dev, cd->dev);
  3304. return rc;
  3305. }
  3306. rc = pt_pip_get_config_row_size_(cd, row_size);
  3307. if (release_exclusive(cd, cd->dev) < 0)
  3308. pt_debug(cd->dev, DL_ERROR,
  3309. "%s: fail to release exclusive\n", __func__);
  3310. return rc;
  3311. }
  3312. /*******************************************************************************
  3313. * FUNCTION: _pt_request_pip_get_config_row_size
  3314. *
  3315. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3316. * calls to the protected or unprotected call to
  3317. * pt_pip_get_config_row_size_
  3318. *
  3319. * RETURN:
  3320. * 0 = success
  3321. * !0 = failure
  3322. *
  3323. * PARAMETERS:
  3324. * *dev - pointer to device structure
  3325. * protect - flag to call protected or non-protected
  3326. * *row_size - pointer to store the retrieved row size
  3327. ******************************************************************************/
  3328. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3329. int protect, u16 *row_size)
  3330. {
  3331. struct pt_core_data *cd = dev_get_drvdata(dev);
  3332. if (protect)
  3333. return pt_pip_get_config_row_size(cd, row_size);
  3334. return pt_pip_get_config_row_size_(cd, row_size);
  3335. }
  3336. /*******************************************************************************
  3337. * FUNCTION: pt_pip1_read_data_block_
  3338. *
  3339. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3340. * output data to the "read_buf" and update "crc".
  3341. *
  3342. * RETURN:
  3343. * 0 = success
  3344. * !0 = failure
  3345. *
  3346. * PARAMETERS:
  3347. * *cd - pointer to core data
  3348. * row_number - row number
  3349. * length - length of data to read
  3350. * ebid - block id
  3351. * *actual_read_len - Actual data length read
  3352. * *read_buf - pointer to the buffer to store read data
  3353. * read_buf_size - size of read_buf
  3354. * *crc - pointer to store CRC of row data
  3355. ******************************************************************************/
  3356. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3357. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3358. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3359. {
  3360. int read_ebid;
  3361. int status;
  3362. int rc = 0;
  3363. int write_length = 5;
  3364. u8 write_buf[5];
  3365. u8 cmd_offset = 0;
  3366. u16 calc_crc;
  3367. struct pt_hid_output hid_output = {
  3368. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3369. .write_length = write_length,
  3370. .write_buf = write_buf,
  3371. };
  3372. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3373. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3374. write_buf[cmd_offset++] = LOW_BYTE(length);
  3375. write_buf[cmd_offset++] = HI_BYTE(length);
  3376. write_buf[cmd_offset++] = ebid;
  3377. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3378. if (rc)
  3379. return rc;
  3380. status = cd->response_buf[5];
  3381. if (status)
  3382. return status;
  3383. read_ebid = cd->response_buf[6];
  3384. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3385. return -EPROTO;
  3386. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3387. if (length == 0 || *actual_read_len == 0)
  3388. return 0;
  3389. if (read_buf_size >= *actual_read_len &&
  3390. *actual_read_len < PT_MAX_PIP2_MSG_SIZE)
  3391. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3392. else
  3393. return -EPROTO;
  3394. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3395. /* Validate Row Data CRC */
  3396. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3397. if (*crc == calc_crc) {
  3398. return 0;
  3399. } else {
  3400. pt_debug(cd->dev, DL_ERROR,
  3401. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3402. __func__, *crc, calc_crc);
  3403. return -EPROTO;
  3404. }
  3405. }
  3406. /*******************************************************************************
  3407. * FUNCTION: _pt_request_pip_read_data_block
  3408. *
  3409. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3410. * calls to pt_pip1_read_data_block_
  3411. *
  3412. * RETURN:
  3413. * 0 = success
  3414. * !0 = failure
  3415. *
  3416. * PARAMETERS:
  3417. * *dev - pointer to device structure
  3418. * row_number - row number
  3419. * length - length of data to read
  3420. * ebid - block id
  3421. * *actual_read_len - Actual data length read
  3422. * *read_buf - pointer to the buffer to store read data
  3423. * *crc - pointer to store CRC of row data
  3424. ******************************************************************************/
  3425. static int _pt_request_pip_read_data_block(struct device *dev,
  3426. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3427. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3428. {
  3429. struct pt_core_data *cd = dev_get_drvdata(dev);
  3430. return pt_pip1_read_data_block_(cd, row_number, length,
  3431. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3432. }
  3433. /*******************************************************************************
  3434. * FUNCTION: pt_pip1_write_data_block_
  3435. *
  3436. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3437. * write data to the data block.
  3438. *
  3439. * RETURN:
  3440. * 0 = success
  3441. * !0 = failure
  3442. *
  3443. * PARAMETERS:
  3444. * *cd - pointer to core data
  3445. * row_number - row in config block to write to
  3446. * write_length - length of data to write
  3447. * ebid - enumerated block ID
  3448. * *write_buf - pointer to buffer to write
  3449. * *security_key - pointer to security key to allow write
  3450. * *actual_write_len - pointer to store data length actually written
  3451. ******************************************************************************/
  3452. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3453. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3454. u8 *security_key, u16 *actual_write_len)
  3455. {
  3456. /* row_number + write_len + ebid + security_key + crc */
  3457. u16 full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3458. u8 *full_write_buf;
  3459. u8 cmd_offset = 0;
  3460. u16 crc;
  3461. int status;
  3462. int rc = 0;
  3463. int read_ebid;
  3464. u8 *data;
  3465. struct pt_hid_output hid_output = {
  3466. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3467. .write_length = full_write_length,
  3468. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3469. };
  3470. if (write_length > PT_CAL_DATA_ROW_SIZE)
  3471. return -EINVAL;
  3472. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3473. if (!full_write_buf)
  3474. return -ENOMEM;
  3475. hid_output.write_buf = full_write_buf;
  3476. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3477. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3478. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3479. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3480. full_write_buf[cmd_offset++] = ebid;
  3481. data = &full_write_buf[cmd_offset];
  3482. memcpy(data, write_buf, write_length);
  3483. cmd_offset += write_length;
  3484. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3485. cmd_offset += 8;
  3486. crc = _pt_compute_crc(data, write_length);
  3487. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3488. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3489. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3490. if (rc)
  3491. goto exit;
  3492. status = cd->response_buf[5];
  3493. if (status) {
  3494. rc = -EINVAL;
  3495. goto exit;
  3496. }
  3497. read_ebid = cd->response_buf[6];
  3498. if (read_ebid != ebid) {
  3499. rc = -EPROTO;
  3500. goto exit;
  3501. }
  3502. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3503. exit:
  3504. kfree(full_write_buf);
  3505. return rc;
  3506. }
  3507. /*******************************************************************************
  3508. * FUNCTION: _pt_request_pip_write_data_block
  3509. *
  3510. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3511. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3512. *
  3513. * RETURN:
  3514. * 0 = success
  3515. * !0 = failure
  3516. *
  3517. * PARAMETERS:
  3518. * *dev - pointer to device structure
  3519. * protect - flag to call protected or non-protected
  3520. * row_number - row in config block to write to
  3521. * write_length - length of data to write
  3522. * ebid - enumerated block ID
  3523. * *write_buf - pointer to buffer to write
  3524. * *security_key - pointer to security key to allow write
  3525. * *actual_write_len - pointer to store data length actually written
  3526. ******************************************************************************/
  3527. static int _pt_request_pip_write_data_block(struct device *dev,
  3528. u16 row_number, u16 write_length, u8 ebid,
  3529. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3530. {
  3531. struct pt_core_data *cd = dev_get_drvdata(dev);
  3532. return pt_pip1_write_data_block_(cd, row_number,
  3533. write_length, ebid, write_buf, security_key,
  3534. actual_write_len);
  3535. }
  3536. /*******************************************************************************
  3537. * FUNCTION: pt_pip_get_data_structure_
  3538. *
  3539. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3540. * returning a structure of data defined by data_id
  3541. *
  3542. * RETURN:
  3543. * 0 = success
  3544. * !0 = failure
  3545. *
  3546. * PARAMETERS:
  3547. * *cd - pointer to core data
  3548. * read_offset - read pointer offset
  3549. * read_length - length of data to read
  3550. * data_id - data ID to read
  3551. * *status - pointer to store the read response status
  3552. * *data_format - pointer to store format of data read
  3553. * *actual_read_len - pointer to store data length actually read
  3554. * *data - pointer to store data read
  3555. ******************************************************************************/
  3556. static int pt_pip_get_data_structure_(
  3557. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3558. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3559. u8 *data)
  3560. {
  3561. int rc = 0;
  3562. u16 total_read_len = 0;
  3563. u16 read_len;
  3564. u16 off_buf = 0;
  3565. u8 write_buf[5];
  3566. u8 read_data_id;
  3567. struct pt_hid_output hid_output = {
  3568. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3569. .write_length = 5,
  3570. .write_buf = write_buf,
  3571. };
  3572. again:
  3573. write_buf[0] = LOW_BYTE(read_offset);
  3574. write_buf[1] = HI_BYTE(read_offset);
  3575. write_buf[2] = LOW_BYTE(read_length);
  3576. write_buf[3] = HI_BYTE(read_length);
  3577. write_buf[4] = data_id;
  3578. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3579. if (rc)
  3580. return rc;
  3581. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3582. goto set_status;
  3583. read_data_id = cd->response_buf[6];
  3584. if (read_data_id != data_id)
  3585. return -EPROTO;
  3586. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3587. if (read_len && data) {
  3588. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3589. total_read_len += read_len;
  3590. if (read_len < read_length) {
  3591. read_offset += read_len;
  3592. off_buf += read_len;
  3593. read_length -= read_len;
  3594. goto again;
  3595. }
  3596. }
  3597. if (data_format)
  3598. *data_format = cd->response_buf[9];
  3599. if (actual_read_len)
  3600. *actual_read_len = total_read_len;
  3601. set_status:
  3602. if (status)
  3603. *status = cd->response_buf[5];
  3604. return rc;
  3605. }
  3606. /*******************************************************************************
  3607. * FUNCTION: pt_pip_get_data_structure
  3608. *
  3609. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3610. * an exclusive access to the DUT.
  3611. *
  3612. * RETURN:
  3613. * 0 = success
  3614. * !0 = failure
  3615. *
  3616. * PARAMETERS:
  3617. * *cd - pointer to core data
  3618. * read_offset - read pointer offset
  3619. * read_length - length of data to read
  3620. * data_id - data ID to read
  3621. * *status - pointer to store the read response status
  3622. * *data_format - pointer to store format of data read
  3623. * *actual_read_len - pointer to store data length actually read
  3624. * *data - pointer to store data read
  3625. ******************************************************************************/
  3626. static int pt_pip_get_data_structure(
  3627. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3628. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3629. u8 *data)
  3630. {
  3631. int rc;
  3632. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3633. if (rc < 0) {
  3634. pt_debug(cd->dev, DL_ERROR,
  3635. "%s: fail get exclusive ex=%p own=%p\n",
  3636. __func__, cd->exclusive_dev, cd->dev);
  3637. return rc;
  3638. }
  3639. rc = pt_pip_get_data_structure_(cd, read_offset,
  3640. read_length, data_id, status, data_format,
  3641. actual_read_len, data);
  3642. if (release_exclusive(cd, cd->dev) < 0)
  3643. pt_debug(cd->dev, DL_ERROR,
  3644. "%s: fail to release exclusive\n", __func__);
  3645. return rc;
  3646. }
  3647. /*******************************************************************************
  3648. * FUNCTION: _pt_request_pip_get_data_structure
  3649. *
  3650. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3651. * calls to the protected or unprotected call to
  3652. * pt_pip_get_data_structure
  3653. *
  3654. * RETURN:
  3655. * 0 = success
  3656. * !0 = failure
  3657. *
  3658. * PARAMETERS:
  3659. * *dev - pointer to device structure
  3660. * protect - flag to call protected or non-protected
  3661. * read_offset - read pointer offset
  3662. * read_length - length of data to read
  3663. * data_id - data ID to read
  3664. * *status - pointer to store the read response status
  3665. * *data_format - pointer to store format of data read
  3666. * *actual_read_len - pointer to store data length actually read
  3667. * *data - pointer to store data read
  3668. ******************************************************************************/
  3669. static int _pt_request_pip_get_data_structure(struct device *dev,
  3670. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3671. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3672. {
  3673. struct pt_core_data *cd = dev_get_drvdata(dev);
  3674. if (protect)
  3675. return pt_pip_get_data_structure(cd,
  3676. read_offset, read_length, data_id, status,
  3677. data_format, actual_read_len, data);
  3678. return pt_pip_get_data_structure_(cd,
  3679. read_offset, read_length, data_id, status,
  3680. data_format, actual_read_len, data);
  3681. }
  3682. /*******************************************************************************
  3683. * FUNCTION: _pt_manage_local_cal_data
  3684. *
  3685. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3686. * CALIBRATION data. It stores it in a local static array and can be
  3687. * cleared, loaded or used to restore the CAL data back to the running FW.
  3688. * The CAL data is read or restored by use of the PIP1 commands:
  3689. * - READ_DATA_BLOCK (0x22)
  3690. * - WRITE_DATA_BLOCK (0x23)
  3691. *
  3692. * RETURN:
  3693. * 0 = success
  3694. * !0 = failure
  3695. *
  3696. * PARAMETERS:
  3697. * *dev - pointer to device structure
  3698. * action - One of the following actions:
  3699. * - PT_CAL_DATA_SAVE
  3700. * - PT_CAL_DATA_RESTORE
  3701. * - PT_CAL_DATA_CLEAR
  3702. * - PT_CAL_DATA_SIZE
  3703. * *size - pointer to the number of bytes transferred
  3704. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3705. ******************************************************************************/
  3706. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3707. unsigned short *crc)
  3708. {
  3709. struct pt_core_data *cd = dev_get_drvdata(dev);
  3710. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3711. unsigned short calc_id_crc = 0;
  3712. static u8 *cal_cache_data;
  3713. static u16 cal_cache_len;
  3714. static unsigned short cal_cache_chip_id;
  3715. int rc = 0;
  3716. u8 *tmp_data = NULL;
  3717. u8 row_number = 0;
  3718. u8 prefix[20];
  3719. u16 cal_size = 0;
  3720. u16 transfer_size;
  3721. u16 act_trans_len = 0;
  3722. u16 byte_offset = 0;
  3723. u16 cal_blk_size;
  3724. u16 total_rows;
  3725. u16 remain_bytes;
  3726. u16 data_block_crc;
  3727. u16 buf_size = 12;
  3728. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3729. __func__, action);
  3730. switch (action) {
  3731. case PT_CAL_DATA_SAVE:
  3732. /* Read the size of the CAL block and calculate # rows */
  3733. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3734. if (!tmp_data) {
  3735. rc = -ENOMEM;
  3736. goto exit;
  3737. }
  3738. /*
  3739. * Don't check rc as doing a read size will give a false
  3740. * error on the CRC check.
  3741. */
  3742. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3743. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3744. cal_blk_size = act_trans_len;
  3745. kfree(tmp_data);
  3746. pt_debug(dev, DL_INFO,
  3747. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3748. __func__, cal_cache_len, cal_blk_size);
  3749. /* Safety net to ensure we didn't read incorrect size */
  3750. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3751. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3752. __func__);
  3753. rc = 1;
  3754. goto exit;
  3755. }
  3756. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3757. if (cal_blk_size != cal_cache_len) {
  3758. kfree(cal_cache_data);
  3759. cal_cache_data = kzalloc(cal_blk_size + 2,
  3760. GFP_KERNEL);
  3761. if (!cal_cache_data) {
  3762. rc = -ENOMEM;
  3763. goto exit;
  3764. }
  3765. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3766. __func__);
  3767. }
  3768. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3769. /* Calculate how many rows [0-n] (PIP Transactions) */
  3770. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3771. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3772. /* Add row if we have a last partial row */
  3773. if (remain_bytes > 0)
  3774. total_rows++;
  3775. pt_debug(dev, DL_INFO,
  3776. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3777. __func__, cal_blk_size, total_rows, remain_bytes);
  3778. /* Read all rows unless an error occurs */
  3779. rc = 0;
  3780. while (rc == 0 && row_number <= total_rows) {
  3781. act_trans_len = 0;
  3782. if (remain_bytes > 0 && row_number == total_rows)
  3783. transfer_size = remain_bytes;
  3784. else
  3785. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3786. rc = pt_pip1_read_data_block_(cd, row_number,
  3787. transfer_size, PT_CAL_EBID,
  3788. &act_trans_len,
  3789. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3790. &data_block_crc);
  3791. if (rc) {
  3792. /* Error occurred, exit loop */
  3793. cal_size = 0;
  3794. break;
  3795. }
  3796. pt_debug(dev, DL_INFO,
  3797. "%s: CAL read rc=%d actual read len=%d\n",
  3798. __func__, rc, act_trans_len);
  3799. byte_offset += act_trans_len;
  3800. cal_size = byte_offset;
  3801. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3802. pt_pr_buf(dev, DL_INFO,
  3803. &cal_cache_data[byte_offset - act_trans_len],
  3804. act_trans_len, prefix);
  3805. row_number++;
  3806. }
  3807. if (cal_size > 0) {
  3808. /* Save a CRC of the chip info the CAL was saved from */
  3809. calc_id_crc = crc_ccitt_calculate(
  3810. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3811. cal_cache_chip_id = calc_id_crc;
  3812. cal_cache_len = cal_size;
  3813. pt_debug(dev, DL_INFO,
  3814. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3815. __func__, calc_id_crc, cal_size);
  3816. }
  3817. *size = cal_size;
  3818. *crc = calc_id_crc;
  3819. break;
  3820. case PT_CAL_DATA_RESTORE:
  3821. cal_size = cal_cache_len;
  3822. while ((rc == 0) && (byte_offset < cal_size)) {
  3823. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3824. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3825. else
  3826. transfer_size = cal_size - byte_offset;
  3827. rc = pt_pip1_write_data_block_(cd, row_number,
  3828. transfer_size, PT_CAL_EBID,
  3829. &cal_cache_data[byte_offset],
  3830. (u8 *)pt_data_block_security_key,
  3831. &act_trans_len);
  3832. byte_offset += act_trans_len;
  3833. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3834. __func__, byte_offset);
  3835. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3836. pt_pr_buf(dev, DL_INFO,
  3837. &cal_cache_data[byte_offset - act_trans_len],
  3838. act_trans_len, prefix);
  3839. if ((byte_offset > cal_size) ||
  3840. (act_trans_len != transfer_size))
  3841. rc = -EIO;
  3842. row_number++;
  3843. }
  3844. *size = byte_offset;
  3845. *crc = cal_cache_chip_id;
  3846. break;
  3847. case PT_CAL_DATA_CLEAR:
  3848. if (cal_cache_data)
  3849. memset(&cal_cache_data[0], 0, cal_cache_len);
  3850. cal_cache_len = 0;
  3851. cal_cache_chip_id = 0;
  3852. *size = 0;
  3853. *crc = 0;
  3854. break;
  3855. case PT_CAL_DATA_INFO:
  3856. default:
  3857. *size = cal_cache_len;
  3858. *crc = cal_cache_chip_id;
  3859. pt_debug(dev, DL_INFO,
  3860. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3861. __func__, cal_cache_chip_id,
  3862. cal_cache_len);
  3863. break;
  3864. }
  3865. exit:
  3866. pt_debug(dev, DL_INFO,
  3867. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3868. __func__, rc, *crc, *size);
  3869. return rc;
  3870. }
  3871. /*******************************************************************************
  3872. * FUNCTION: pt_pip_run_selftest_
  3873. *
  3874. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3875. * to execute a FW built in self test
  3876. *
  3877. * RETURN:
  3878. * 0 = success
  3879. * !0 = failure
  3880. *
  3881. * PARAMETERS:
  3882. * *cd - pointer to core data
  3883. * test_id - enumerated test ID to run
  3884. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3885. * *status - pointer to store the read response status
  3886. * *summary_results - pointer to store the results summary
  3887. * *results_available - pointer to store if results are available
  3888. *****************************************************************************/
  3889. static int pt_pip_run_selftest_(
  3890. struct pt_core_data *cd, u8 test_id,
  3891. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3892. u8 *results_available)
  3893. {
  3894. int rc = 0;
  3895. u8 write_buf[2];
  3896. struct pt_hid_output hid_output = {
  3897. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3898. .write_length = 2,
  3899. .write_buf = write_buf,
  3900. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3901. };
  3902. write_buf[0] = test_id;
  3903. write_buf[1] = write_idacs_to_flash;
  3904. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3905. hid_output.write_length = 1;
  3906. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3907. if (rc)
  3908. return rc;
  3909. if (status)
  3910. *status = cd->response_buf[5];
  3911. if (summary_result)
  3912. *summary_result = cd->response_buf[6];
  3913. /* results_available only available before PIP 1.03 */
  3914. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3915. if (results_available)
  3916. *results_available = cd->response_buf[7];
  3917. }
  3918. return rc;
  3919. }
  3920. /*******************************************************************************
  3921. * FUNCTION: pt_pip_run_selftest
  3922. *
  3923. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3924. * an exclusive access to the DUT.
  3925. *
  3926. * RETURN:
  3927. * 0 = success
  3928. * !0 = failure
  3929. *
  3930. * PARAMETERS:
  3931. * *cd - pointer to core data
  3932. * test_id - enumerated test ID to run
  3933. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3934. * *status - pointer to store the read response status
  3935. * *summary_results - pointer to store the results summary
  3936. * *results_available - pointer to store if results are available
  3937. ******************************************************************************/
  3938. static int pt_pip_run_selftest(
  3939. struct pt_core_data *cd, u8 test_id,
  3940. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3941. u8 *results_available)
  3942. {
  3943. int rc;
  3944. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3945. if (rc < 0) {
  3946. pt_debug(cd->dev, DL_ERROR,
  3947. "%s: fail get exclusive ex=%p own=%p\n",
  3948. __func__, cd->exclusive_dev, cd->dev);
  3949. return rc;
  3950. }
  3951. rc = pt_pip_run_selftest_(cd, test_id,
  3952. write_idacs_to_flash, status, summary_result,
  3953. results_available);
  3954. if (release_exclusive(cd, cd->dev) < 0)
  3955. pt_debug(cd->dev, DL_ERROR,
  3956. "%s: fail to release exclusive\n", __func__);
  3957. return rc;
  3958. }
  3959. /*******************************************************************************
  3960. * FUNCTION: _pt_request_pip_run_selftest
  3961. *
  3962. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3963. * calls to the protected or unprotected call to pt_pip_run_selftest
  3964. *
  3965. * RETURN:
  3966. * 0 = success
  3967. * !0 = failure
  3968. *
  3969. * PARAMETERS:
  3970. * *dev - pointer to device structure
  3971. * protect - flag to call protected or non-protected
  3972. * test_id - enumerated test ID to run
  3973. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3974. * *status - pointer to store the read response status
  3975. * *summary_results - pointer to store the results summary
  3976. * *results_available - pointer to store if results are available
  3977. ******************************************************************************/
  3978. static int _pt_request_pip_run_selftest(struct device *dev,
  3979. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3980. u8 *summary_result, u8 *results_available)
  3981. {
  3982. struct pt_core_data *cd = dev_get_drvdata(dev);
  3983. if (protect)
  3984. return pt_pip_run_selftest(cd, test_id,
  3985. write_idacs_to_flash, status, summary_result,
  3986. results_available);
  3987. return pt_pip_run_selftest_(cd, test_id,
  3988. write_idacs_to_flash, status, summary_result,
  3989. results_available);
  3990. }
  3991. /*******************************************************************************
  3992. * FUNCTION: _pt_pip_get_selftest_result_
  3993. *
  3994. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3995. * to retrieve the self test results from the self test already executed
  3996. *
  3997. * RETURN:
  3998. * 0 = success
  3999. * !0 = failure
  4000. *
  4001. * PARAMETERS:
  4002. * *cd - pointer to core data
  4003. * read_offset - read pointer offset
  4004. * read_length - length of data to read
  4005. * test_id - enumerated test ID to read selftest results from
  4006. * *status - pointer to store the read response status
  4007. * *actual_read_len - pointer to store data length actually read
  4008. * *status - pointer to where the cmd response statas is stored
  4009. ******************************************************************************/
  4010. static int pt_pip_get_selftest_result_(
  4011. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4012. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4013. {
  4014. int rc = 0;
  4015. u16 total_read_len = 0;
  4016. u16 read_len;
  4017. u16 off_buf = 0;
  4018. u8 write_buf[5];
  4019. u8 read_test_id;
  4020. bool repeat;
  4021. struct pt_hid_output hid_output = {
  4022. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4023. .write_length = 5,
  4024. .write_buf = write_buf,
  4025. };
  4026. /*
  4027. * Do not repeat reading for Auto Shorts test
  4028. * when PIP version < 1.3
  4029. */
  4030. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4031. || test_id != PT_ST_ID_AUTOSHORTS;
  4032. again:
  4033. write_buf[0] = LOW_BYTE(read_offset);
  4034. write_buf[1] = HI_BYTE(read_offset);
  4035. write_buf[2] = LOW_BYTE(read_length);
  4036. write_buf[3] = HI_BYTE(read_length);
  4037. write_buf[4] = test_id;
  4038. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4039. if (rc)
  4040. return rc;
  4041. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4042. goto set_status;
  4043. read_test_id = cd->response_buf[6];
  4044. if (read_test_id != test_id)
  4045. return -EPROTO;
  4046. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4047. if (read_len && data) {
  4048. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4049. total_read_len += read_len;
  4050. if (repeat && read_len < read_length) {
  4051. read_offset += read_len;
  4052. off_buf += read_len;
  4053. read_length -= read_len;
  4054. goto again;
  4055. }
  4056. }
  4057. if (actual_read_len)
  4058. *actual_read_len = total_read_len;
  4059. set_status:
  4060. if (status)
  4061. *status = cd->response_buf[5];
  4062. return rc;
  4063. }
  4064. /*******************************************************************************
  4065. * FUNCTION: _pt_pip_get_selftest_result
  4066. *
  4067. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4068. * access to the DUT
  4069. *
  4070. * RETURN:
  4071. * 0 = success
  4072. * !0 = failure
  4073. *
  4074. * PARAMETERS:
  4075. * *cd - pointer to core data
  4076. * read_offset - read pointer offset
  4077. * read_length - length of data to read
  4078. * test_id - enumerated test ID to read selftest results from
  4079. * *status - pointer to store the read response status
  4080. * *actual_read_len - pointer to store data length actually read
  4081. * *status - pointer to where the cmd response statas is stored
  4082. ******************************************************************************/
  4083. static int pt_pip_get_selftest_result(
  4084. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4085. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4086. {
  4087. int rc;
  4088. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4089. if (rc < 0) {
  4090. pt_debug(cd->dev, DL_ERROR,
  4091. "%s: fail get exclusive ex=%p own=%p\n",
  4092. __func__, cd->exclusive_dev, cd->dev);
  4093. return rc;
  4094. }
  4095. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4096. read_length, test_id, status, actual_read_len, data);
  4097. if (release_exclusive(cd, cd->dev) < 0)
  4098. pt_debug(cd->dev, DL_ERROR,
  4099. "%s: fail to release exclusive\n", __func__);
  4100. return rc;
  4101. }
  4102. /*******************************************************************************
  4103. * FUNCTION: _pt_request_pip_get_selftest_result
  4104. *
  4105. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4106. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4107. *
  4108. * RETURN:
  4109. * 0 = success
  4110. * !0 = failure
  4111. *
  4112. * PARAMETERS:
  4113. * *dev - pointer to device structure
  4114. * protect - flag to call protected or non-protected
  4115. * read_offset - read pointer offset
  4116. * read_length - length of data to read
  4117. * test_id - enumerated test ID to read selftest results from
  4118. * *status - pointer to store the read response status
  4119. * *actual_read_len - pointer to store data length actually read
  4120. * *data - pointer to where the data read is stored
  4121. ******************************************************************************/
  4122. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4123. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4124. u8 *status, u16 *actual_read_len, u8 *data)
  4125. {
  4126. struct pt_core_data *cd = dev_get_drvdata(dev);
  4127. if (protect)
  4128. return pt_pip_get_selftest_result(cd, read_offset,
  4129. read_length, test_id, status, actual_read_len,
  4130. data);
  4131. return pt_pip_get_selftest_result_(cd, read_offset,
  4132. read_length, test_id, status, actual_read_len,
  4133. data);
  4134. }
  4135. /*******************************************************************************
  4136. * FUNCTION: _pt_pip_load_self_test_param
  4137. *
  4138. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4139. * to load parameters needed by a self test
  4140. *
  4141. * RETURN:
  4142. * 0 = success
  4143. * !0 = failure
  4144. *
  4145. * PARAMETERS:
  4146. * *cd - pointer to core data
  4147. * self_test_id - enumerated test ID for which the parmeters belong
  4148. * load_offset - mem offset to where to load parameters
  4149. * load_length - length of parameter data to load
  4150. * *parameters - pointer to list of parameter data
  4151. * *status - pointer to store the response status
  4152. * *ret_test_id - pointer to returned test id the parameters were stored
  4153. * *act_load_len - pointer to store the actual load length that was writen
  4154. ******************************************************************************/
  4155. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4156. u8 self_test_id, u16 load_offset, u16 load_length,
  4157. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4158. {
  4159. int rc = 0;
  4160. int i;
  4161. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4162. struct pt_hid_output hid_output = {
  4163. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4164. .write_length = 5 + load_length,
  4165. .write_buf = write_buf,
  4166. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4167. };
  4168. write_buf[0] = LOW_BYTE(load_offset);
  4169. write_buf[1] = HI_BYTE(load_offset);
  4170. write_buf[2] = LOW_BYTE(load_length);
  4171. write_buf[3] = HI_BYTE(load_length);
  4172. write_buf[4] = self_test_id;
  4173. for (i = 0; i < load_length; i++)
  4174. write_buf[i + 5] = parameters[i];
  4175. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4176. if (rc)
  4177. return rc;
  4178. if (status)
  4179. *status = cd->response_buf[5];
  4180. if (ret_test_id)
  4181. *ret_test_id = cd->response_buf[6];
  4182. if (act_load_len)
  4183. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4184. return rc;
  4185. }
  4186. /*******************************************************************************
  4187. * FUNCTION: pt_pip_load_self_test_param
  4188. *
  4189. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4190. * access to the DUT.
  4191. *
  4192. * RETURN:
  4193. * 0 = success
  4194. * !0 = failure
  4195. *
  4196. * PARAMETERS:
  4197. * *cd - pointer to core data
  4198. * self_test_id - enumerated test ID for which the parmeters belong
  4199. * load_offset - mem offset to where to load parameters
  4200. * load_length - length of parameter data to load
  4201. * *parameters - pointer to list of parameter data
  4202. * *status - pointer to store the response status
  4203. * *ret_test_id - pointer to returned test id the parameters were stored
  4204. * *act_load_len - pointer to store the actual load length that was writen
  4205. ******************************************************************************/
  4206. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4207. u8 self_test_id, u16 load_offset, u16 load_length,
  4208. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4209. {
  4210. int rc;
  4211. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4212. if (rc < 0) {
  4213. pt_debug(cd->dev, DL_ERROR,
  4214. "%s: fail get exclusive ex=%p own=%p\n",
  4215. __func__, cd->exclusive_dev, cd->dev);
  4216. return rc;
  4217. }
  4218. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4219. load_length, parameters, status, ret_test_id, act_load_len);
  4220. if (release_exclusive(cd, cd->dev) < 0)
  4221. pt_debug(cd->dev, DL_ERROR,
  4222. "%s: fail to release exclusive\n", __func__);
  4223. return rc;
  4224. }
  4225. /*******************************************************************************
  4226. * FUNCTION: _pt_request_pip_load_self_test_param
  4227. *
  4228. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4229. * calls to the protected or unprotected call to
  4230. * pt_pip_load_self_test_param
  4231. *
  4232. * RETURN:
  4233. * 0 = success
  4234. * !0 = failure
  4235. *
  4236. * PARAMETERS:
  4237. * *dev - pointer to device structure
  4238. * protect - flag to call protected or non-protected
  4239. * self_test_id - enumerated test ID for which the parmeters belong
  4240. * load_offset - mem offset to where to load parameters
  4241. * load_length - length of parameter data to load
  4242. * *parameters - pointer to list of parameter data
  4243. * *status - pointer to store the response status
  4244. * *ret_test_id - pointer to returned test id the parameters were stored
  4245. * *act_load_len - pointer to store the actual load length that was writen
  4246. ******************************************************************************/
  4247. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4248. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4249. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4250. {
  4251. struct pt_core_data *cd = dev_get_drvdata(dev);
  4252. if (protect)
  4253. return pt_pip_load_self_test_param(cd, self_test_id,
  4254. load_offset, load_length, parameters, status, ret_test_id,
  4255. act_load_len);
  4256. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4257. load_length, parameters, status, ret_test_id, act_load_len);
  4258. }
  4259. /*******************************************************************************
  4260. * FUNCTION: pt_pip_calibrate_ext_
  4261. *
  4262. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4263. * for the response
  4264. *
  4265. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4266. *
  4267. * RETURN:
  4268. * 0 = success
  4269. * !0 = failure
  4270. *
  4271. * PARAMETERS:
  4272. * *cd - pointer to core data
  4273. * *cal_data - pointer to extended calibration data structure
  4274. * *status - pointer to where the command response status is stored
  4275. ******************************************************************************/
  4276. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4277. struct pt_cal_ext_data *cal_data, u8 *status)
  4278. {
  4279. int rc = 0;
  4280. int write_length = 4;
  4281. u8 write_buf[4];
  4282. u16 size = 0;
  4283. unsigned short crc = 0;
  4284. struct pt_hid_output hid_output = {
  4285. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4286. .write_length = write_length,
  4287. .write_buf = write_buf,
  4288. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4289. };
  4290. if (cal_data == NULL)
  4291. return -EINVAL;
  4292. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4293. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4294. if (rc)
  4295. return rc;
  4296. if (status)
  4297. *status = cd->response_buf[5];
  4298. /*
  4299. * When doing a calibration on a flashless DUT, save CAL data in
  4300. * the TTDL cache on any successful calibration
  4301. */
  4302. if (cd->response_buf[5] == 0 && cd->cal_cache_in_host) {
  4303. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4304. __func__);
  4305. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4306. &size, &crc);
  4307. if (rc)
  4308. pt_debug(cd->dev, DL_ERROR,
  4309. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4310. else
  4311. pt_debug(cd->dev, DL_INFO,
  4312. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4313. __func__, crc, size);
  4314. }
  4315. return 0;
  4316. }
  4317. /*******************************************************************************
  4318. * FUNCTION: pt_pip_calibrate_ext
  4319. *
  4320. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4321. * DUT
  4322. *
  4323. * RETURN:
  4324. * 0 = success
  4325. * !0 = failure
  4326. *
  4327. * PARAMETERS:
  4328. * *cd - pointer to core data
  4329. * *cal_data - pointer to extended calibration data structure
  4330. * *status - pointer to where the command response status is stored
  4331. ******************************************************************************/
  4332. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4333. struct pt_cal_ext_data *cal_data, u8 *status)
  4334. {
  4335. int rc;
  4336. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4337. if (rc < 0) {
  4338. pt_debug(cd->dev, DL_ERROR,
  4339. "%s: fail get exclusive ex=%p own=%p\n",
  4340. __func__, cd->exclusive_dev, cd->dev);
  4341. return rc;
  4342. }
  4343. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4344. if (release_exclusive(cd, cd->dev) < 0)
  4345. pt_debug(cd->dev, DL_ERROR,
  4346. "%s: fail to release exclusive\n", __func__);
  4347. return rc;
  4348. }
  4349. /*******************************************************************************
  4350. * FUNCTION: _pt_request_pip_calibrate_ext
  4351. *
  4352. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4353. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4354. *
  4355. * RETURN:
  4356. * 0 = success
  4357. * !0 = failure
  4358. *
  4359. * PARAMETERS:
  4360. * *dev - pointer to device structure
  4361. * protect - flag to call protected or non-protected
  4362. * *cal_data - pointer to extended calibration data structure
  4363. * *status - pointer to where the command response status is stored
  4364. ******************************************************************************/
  4365. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4366. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4367. {
  4368. struct pt_core_data *cd = dev_get_drvdata(dev);
  4369. if (protect)
  4370. return pt_pip_calibrate_ext(cd, cal_data, status);
  4371. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4372. }
  4373. /*******************************************************************************
  4374. * FUNCTION: pt_pip_calibrate_idacs_
  4375. *
  4376. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4377. * for the response
  4378. *
  4379. * RETURN:
  4380. * 0 = success
  4381. * !0 = failure
  4382. *
  4383. * PARAMETERS:
  4384. * *cd - pointer to core data
  4385. * mode - sense mode to calibrate (0-5)
  4386. * *status - pointer to where the command response status is stored
  4387. ******************************************************************************/
  4388. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4389. u8 mode, u8 *status)
  4390. {
  4391. int rc = 0;
  4392. int write_length = 1;
  4393. u8 write_buf[1];
  4394. u8 cmd_offset = 0;
  4395. struct pt_hid_output hid_output = {
  4396. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4397. .write_length = write_length,
  4398. .write_buf = write_buf,
  4399. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4400. };
  4401. write_buf[cmd_offset++] = mode;
  4402. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4403. if (rc)
  4404. return rc;
  4405. *status = cd->response_buf[5];
  4406. if (*status)
  4407. return -EINVAL;
  4408. return 0;
  4409. }
  4410. /*******************************************************************************
  4411. * FUNCTION: pt_pip_calibrate_idacs
  4412. *
  4413. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4414. * access to the DUT
  4415. *
  4416. * RETURN:
  4417. * 0 = success
  4418. * !0 = failure
  4419. *
  4420. * PARAMETERS:
  4421. * *cd - pointer to core data
  4422. * mode - sense mode to calibrate (0-5)
  4423. * *status - pointer to where the command response status is stored
  4424. ******************************************************************************/
  4425. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4426. u8 mode, u8 *status)
  4427. {
  4428. int rc;
  4429. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4430. if (rc < 0) {
  4431. pt_debug(cd->dev, DL_ERROR,
  4432. "%s: fail get exclusive ex=%p own=%p\n",
  4433. __func__, cd->exclusive_dev, cd->dev);
  4434. return rc;
  4435. }
  4436. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4437. if (release_exclusive(cd, cd->dev) < 0)
  4438. pt_debug(cd->dev, DL_ERROR,
  4439. "%s: fail to release exclusive\n", __func__);
  4440. return rc;
  4441. }
  4442. /*******************************************************************************
  4443. * FUNCTION: _pt_request_pip_calibrate_idacs
  4444. *
  4445. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4446. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4447. *
  4448. * RETURN:
  4449. * 0 = success
  4450. * !0 = failure
  4451. *
  4452. * PARAMETERS:
  4453. * *dev - pointer to device structure
  4454. * protect - flag to call protected or non-protected
  4455. * mode - sense mode to calibrate (0-5)
  4456. * *status - pointer to where the command response status is stored
  4457. ******************************************************************************/
  4458. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4459. int protect, u8 mode, u8 *status)
  4460. {
  4461. struct pt_core_data *cd = dev_get_drvdata(dev);
  4462. if (protect)
  4463. return pt_pip_calibrate_idacs(cd, mode, status);
  4464. return pt_pip_calibrate_idacs_(cd, mode, status);
  4465. }
  4466. /*******************************************************************************
  4467. * FUNCTION: pt_hid_output_initialize_baselines_
  4468. *
  4469. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4470. * waiting for the response.
  4471. *
  4472. * RETURN:
  4473. * 0 = success
  4474. * !0 = failure
  4475. *
  4476. * PARAMETERS:
  4477. * *cd - pointer to core data
  4478. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4479. * each or together with a single command.
  4480. * *status - pointer to where the command response status is stored
  4481. ******************************************************************************/
  4482. static int pt_hid_output_initialize_baselines_(
  4483. struct pt_core_data *cd, u8 test_id, u8 *status)
  4484. {
  4485. int rc = 0;
  4486. int write_length = 1;
  4487. u8 write_buf[1];
  4488. u8 cmd_offset = 0;
  4489. struct pt_hid_output hid_output = {
  4490. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4491. .write_length = write_length,
  4492. .write_buf = write_buf,
  4493. };
  4494. write_buf[cmd_offset++] = test_id;
  4495. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4496. if (rc)
  4497. return rc;
  4498. *status = cd->response_buf[5];
  4499. if (*status)
  4500. return -EINVAL;
  4501. return rc;
  4502. }
  4503. /*******************************************************************************
  4504. * FUNCTION: pt_hid_output_initialize_baselines
  4505. *
  4506. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4507. * access to the DUT
  4508. *
  4509. * RETURN:
  4510. * 0 = success
  4511. * !0 = failure
  4512. *
  4513. * PARAMETERS:
  4514. * *cd - pointer to core data
  4515. * test_id - enumerated ID against which to initialize the baseline
  4516. * *status - pointer to where the command response status is stored
  4517. ******************************************************************************/
  4518. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4519. u8 test_id, u8 *status)
  4520. {
  4521. int rc;
  4522. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4523. if (rc < 0) {
  4524. pt_debug(cd->dev, DL_ERROR,
  4525. "%s: fail get exclusive ex=%p own=%p\n",
  4526. __func__, cd->exclusive_dev, cd->dev);
  4527. return rc;
  4528. }
  4529. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4530. if (release_exclusive(cd, cd->dev) < 0)
  4531. pt_debug(cd->dev, DL_ERROR,
  4532. "%s: fail to release exclusive\n", __func__);
  4533. return rc;
  4534. }
  4535. /*******************************************************************************
  4536. * FUNCTION: _pt_request_pip_initialize_baselines
  4537. *
  4538. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4539. * calls to the protected or unprotected call to
  4540. * pt_pip_initialize_baselines
  4541. *
  4542. * RETURN:
  4543. * 0 = success
  4544. * !0 = failure
  4545. *
  4546. * PARAMETERS:
  4547. * *dev - pointer to device structure
  4548. * protect - flag to call protected or non-protected
  4549. * test_id - enumerated ID against which to initialize the baseline
  4550. * *status - pointer to where the command response status is stored
  4551. ******************************************************************************/
  4552. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4553. int protect, u8 test_id, u8 *status)
  4554. {
  4555. struct pt_core_data *cd = dev_get_drvdata(dev);
  4556. if (protect)
  4557. return pt_hid_output_initialize_baselines(cd, test_id,
  4558. status);
  4559. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4560. }
  4561. /*******************************************************************************
  4562. * FUNCTION: pt_hid_output_exec_panel_scan_
  4563. *
  4564. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4565. * the response
  4566. *
  4567. * RETURN:
  4568. * 0 = success
  4569. * !0 = failure
  4570. *
  4571. * PARAMETERS:
  4572. * *cd - pointer to core data
  4573. ******************************************************************************/
  4574. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4575. {
  4576. struct pt_hid_output hid_output = {
  4577. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4578. };
  4579. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4580. }
  4581. /*******************************************************************************
  4582. * FUNCTION: pt_hid_output_exec_panel_scan
  4583. *
  4584. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4585. * access to the DUT.
  4586. *
  4587. * RETURN:
  4588. * 0 = success
  4589. * !0 = failure
  4590. *
  4591. * PARAMETERS:
  4592. * *cd - pointer to core data
  4593. ******************************************************************************/
  4594. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4595. {
  4596. int rc;
  4597. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4598. if (rc < 0) {
  4599. pt_debug(cd->dev, DL_ERROR,
  4600. "%s: fail get exclusive ex=%p own=%p\n",
  4601. __func__, cd->exclusive_dev, cd->dev);
  4602. return rc;
  4603. }
  4604. rc = pt_hid_output_exec_panel_scan_(cd);
  4605. if (release_exclusive(cd, cd->dev) < 0)
  4606. pt_debug(cd->dev, DL_ERROR,
  4607. "%s: fail to release exclusive\n", __func__);
  4608. return rc;
  4609. }
  4610. /*******************************************************************************
  4611. * FUNCTION: pt_pip2_exec_panel_scan_
  4612. *
  4613. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4614. * the response
  4615. *
  4616. * RETURN:
  4617. * 0 = success
  4618. * !0 = failure
  4619. *
  4620. * PARAMETERS:
  4621. * *cd - pointer to core data
  4622. * scan_type - type of panel scan to perform (PIP2 only)
  4623. ******************************************************************************/
  4624. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4625. {
  4626. int rc = 0;
  4627. u8 data[2];
  4628. u8 read_buf[10];
  4629. u16 actual_read_len;
  4630. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4631. __func__, scan_type);
  4632. data[0] = scan_type;
  4633. rc = _pt_request_pip2_send_cmd(cd->dev,
  4634. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4635. data, 1, read_buf, &actual_read_len);
  4636. if (rc) {
  4637. pt_debug(cd->dev, DL_ERROR,
  4638. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4639. __func__, scan_type, rc);
  4640. }
  4641. return rc;
  4642. }
  4643. /*******************************************************************************
  4644. * FUNCTION: pt_pip2_exec_panel_scan
  4645. *
  4646. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4647. * access to the DUT.
  4648. *
  4649. * RETURN:
  4650. * 0 = success
  4651. * !0 = failure
  4652. *
  4653. * PARAMETERS:
  4654. * *cd - pointer to core data
  4655. * scan_type - type of panel scan to perform (PIP2 only)
  4656. ******************************************************************************/
  4657. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4658. {
  4659. int rc;
  4660. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4661. if (rc < 0) {
  4662. pt_debug(cd->dev, DL_ERROR,
  4663. "%s: fail get exclusive ex=%p own=%p\n",
  4664. __func__, cd->exclusive_dev, cd->dev);
  4665. return rc;
  4666. }
  4667. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4668. if (release_exclusive(cd, cd->dev) < 0)
  4669. pt_debug(cd->dev, DL_ERROR,
  4670. "%s: fail to release exclusive\n", __func__);
  4671. return rc;
  4672. }
  4673. /*******************************************************************************
  4674. * FUNCTION: _pt_request_pip_exec_panel_scan
  4675. *
  4676. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4677. * calls to the protected or unprotected call to
  4678. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4679. *
  4680. * RETURN:
  4681. * 0 = success
  4682. * !0 = failure
  4683. *
  4684. * PARAMETERS:
  4685. * *dev - pointer to device structure
  4686. * protect - flag to call protected or non-protected
  4687. * scan_type - type of panel scan to perform (PIP2 only)
  4688. ******************************************************************************/
  4689. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4690. int protect, u8 scan_type)
  4691. {
  4692. struct pt_core_data *cd = dev_get_drvdata(dev);
  4693. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4694. if (protect)
  4695. return pt_pip2_exec_panel_scan(cd, scan_type);
  4696. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4697. }
  4698. if (protect)
  4699. return pt_hid_output_exec_panel_scan(cd);
  4700. return pt_hid_output_exec_panel_scan_(cd);
  4701. }
  4702. /*******************************************************************************
  4703. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4704. *
  4705. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4706. * to retrieve the specified data type for a the last successful Execute
  4707. * Panel Scan command.
  4708. *
  4709. * RETURN:
  4710. * 0 = success
  4711. * !0 = failure
  4712. *
  4713. * PARAMETERS:
  4714. * *dev - pointer to device structure
  4715. * protect - flag to call protected or non-protected
  4716. * read_offset - read pointer offset
  4717. * read_count - length of data to read
  4718. * data_id - enumerated test ID to read selftest results from
  4719. * *response - pointer to store the read response status
  4720. * *config - pointer to store config data
  4721. * *actual_read_len - pointer to store data length actually read
  4722. * *read_buf - pointer to the read buffer
  4723. ******************************************************************************/
  4724. static int pt_hid_output_retrieve_panel_scan_(
  4725. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4726. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4727. u8 *read_buf)
  4728. {
  4729. int status;
  4730. u8 read_data_id;
  4731. int rc = 0;
  4732. int write_length = 5;
  4733. u8 write_buf[5];
  4734. u8 cmd_offset = 0;
  4735. u8 data_elem_size;
  4736. int size;
  4737. int data_size;
  4738. struct pt_hid_output hid_output = {
  4739. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4740. .write_length = write_length,
  4741. .write_buf = write_buf,
  4742. };
  4743. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4744. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4745. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4746. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4747. write_buf[cmd_offset++] = data_id;
  4748. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4749. if (rc)
  4750. return rc;
  4751. status = cd->response_buf[5];
  4752. if (status)
  4753. return -EINVAL;
  4754. read_data_id = cd->response_buf[6];
  4755. if (read_data_id != data_id)
  4756. return -EPROTO;
  4757. size = get_unaligned_le16(&cd->response_buf[0]);
  4758. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4759. *config = cd->response_buf[9];
  4760. data_elem_size = *config & 0x07;
  4761. data_size = *actual_read_len * data_elem_size;
  4762. if (read_buf)
  4763. memcpy(read_buf, &cd->response_buf[10], data_size);
  4764. if (response)
  4765. memcpy(response, cd->response_buf, size);
  4766. return rc;
  4767. }
  4768. /*******************************************************************************
  4769. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4770. *
  4771. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4772. * access to the DUT.
  4773. *
  4774. * RETURN:
  4775. * 0 = success
  4776. * !0 = failure
  4777. *
  4778. * PARAMETERS:
  4779. * *dev - pointer to device structure
  4780. * protect - flag to call protected or non-protected
  4781. * read_offset - read pointer offset
  4782. * read_count - length of data to read
  4783. * data_id - enumerated test ID to read selftest results from
  4784. * *response - pointer to store the read response status
  4785. * *config - pointer to store config data
  4786. * *actual_read_len - pointer to store data length actually read
  4787. * *read_buf - pointer to the read buffer
  4788. ******************************************************************************/
  4789. static int pt_hid_output_retrieve_panel_scan(
  4790. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4791. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4792. u8 *read_buf)
  4793. {
  4794. int rc;
  4795. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4796. if (rc < 0) {
  4797. pt_debug(cd->dev, DL_ERROR,
  4798. "%s: fail get exclusive ex=%p own=%p\n",
  4799. __func__, cd->exclusive_dev, cd->dev);
  4800. return rc;
  4801. }
  4802. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4803. read_count, data_id, response, config,
  4804. actual_read_len, read_buf);
  4805. if (release_exclusive(cd, cd->dev) < 0)
  4806. pt_debug(cd->dev, DL_ERROR,
  4807. "%s: fail to release exclusive\n", __func__);
  4808. return rc;
  4809. }
  4810. /*******************************************************************************
  4811. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4812. *
  4813. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4814. * calls to the protected or unprotected call to
  4815. * pt_hid_output_retrieve_panel_scan
  4816. *
  4817. * RETURN:
  4818. * 0 = success
  4819. * !0 = failure
  4820. *
  4821. * PARAMETERS:
  4822. * *dev - pointer to device structure
  4823. * protect - flag to call protected or non-protected
  4824. * read_offset - read pointer offset
  4825. * read_count - length of data to read
  4826. * data_id - enumerated test ID to read selftest results from
  4827. * *response - pointer to store the read response status
  4828. * *config - pointer to store config data
  4829. * *actual_read_len - pointer to store data length actually read
  4830. * *read_buf - pointer to the read buffer
  4831. ******************************************************************************/
  4832. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4833. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4834. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4835. {
  4836. struct pt_core_data *cd = dev_get_drvdata(dev);
  4837. if (protect)
  4838. return pt_hid_output_retrieve_panel_scan(cd,
  4839. read_offset, read_count, data_id, response,
  4840. config, actual_read_len, read_buf);
  4841. return pt_hid_output_retrieve_panel_scan_(cd,
  4842. read_offset, read_count, data_id, response,
  4843. config, actual_read_len, read_buf);
  4844. }
  4845. /*******************************************************************************
  4846. * FUNCTION: _pt_request_pip_user_cmd
  4847. *
  4848. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4849. * calls to the protected or unprotected call to
  4850. * pt_hid_output_user_cmd
  4851. *
  4852. * RETURN:
  4853. * 0 = success
  4854. * !0 = failure
  4855. *
  4856. * PARAMETERS:
  4857. * *dev - pointer to device structure
  4858. * protect - flag to call protected or non-protected
  4859. * read_len - length of data to read
  4860. * *read_buf - pointer to store read data
  4861. * write_len - length of data to write
  4862. * *write_buf - pointer to buffer to write
  4863. * *actual_read_len - pointer to store data length actually read
  4864. ******************************************************************************/
  4865. static int _pt_request_pip_user_cmd(struct device *dev,
  4866. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4867. u8 *write_buf, u16 *actual_read_len)
  4868. {
  4869. struct pt_core_data *cd = dev_get_drvdata(dev);
  4870. if (protect)
  4871. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4872. write_len, write_buf, actual_read_len);
  4873. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4874. write_len, write_buf, actual_read_len);
  4875. }
  4876. /*******************************************************************************
  4877. * FUNCTION: pt_hid_output_bl_get_information_
  4878. *
  4879. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4880. * DUT to retrieve bootloader version and chip identification information.
  4881. *
  4882. * RETURN:
  4883. * 0 = success
  4884. * !0 = failure
  4885. *
  4886. * PARAMETERS:
  4887. * *cd - pointer to core data
  4888. * *return_data - pointer to store the return data
  4889. *****************************************************************************/
  4890. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4891. u8 *return_data)
  4892. {
  4893. int rc;
  4894. int data_len;
  4895. struct pt_hid_output hid_output = {
  4896. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4897. };
  4898. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4899. if (rc)
  4900. return rc;
  4901. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4902. if (!data_len)
  4903. return -EPROTO;
  4904. memcpy(return_data, &cd->response_buf[8], data_len);
  4905. return 0;
  4906. }
  4907. /*******************************************************************************
  4908. * FUNCTION: pt_hid_output_bl_get_information
  4909. *
  4910. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4911. * access to the DUT.
  4912. *
  4913. * RETURN:
  4914. * 0 = success
  4915. * !0 = failure
  4916. *
  4917. * PARAMETERS:
  4918. * *cd - pointer to core data
  4919. * *return_data - pointer to store the return data
  4920. ******************************************************************************/
  4921. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4922. u8 *return_data)
  4923. {
  4924. int rc;
  4925. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4926. if (rc < 0) {
  4927. pt_debug(cd->dev, DL_ERROR,
  4928. "%s: fail get exclusive ex=%p own=%p\n",
  4929. __func__, cd->exclusive_dev, cd->dev);
  4930. return rc;
  4931. }
  4932. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4933. if (release_exclusive(cd, cd->dev) < 0)
  4934. pt_debug(cd->dev, DL_ERROR,
  4935. "%s: fail to release exclusive\n", __func__);
  4936. return rc;
  4937. }
  4938. /*******************************************************************************
  4939. * FUNCTION: _pt_request_pip_bl_get_information
  4940. *
  4941. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4942. * calls to the protected or unprotected call to
  4943. * pt_hid_output_bl_get_information
  4944. *
  4945. * RETURN:
  4946. * 0 = success
  4947. * !0 = failure
  4948. *
  4949. * PARAMETERS:
  4950. * *dev - pointer to device structure
  4951. * protect - flag to call protected or non-protected
  4952. * *return_data - pointer to store bl data
  4953. ******************************************************************************/
  4954. static int _pt_request_pip_bl_get_information(struct device *dev,
  4955. int protect, u8 *return_data)
  4956. {
  4957. struct pt_core_data *cd = dev_get_drvdata(dev);
  4958. if (protect)
  4959. return pt_hid_output_bl_get_information(cd, return_data);
  4960. return pt_hid_output_bl_get_information_(cd, return_data);
  4961. }
  4962. /*******************************************************************************
  4963. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4964. *
  4965. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4966. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4967. * and Design Data block in flash and enables the host to execute the Program
  4968. * and Verify Row command to bootload the application image and data.
  4969. *
  4970. * RETURN:
  4971. * 0 = success
  4972. * !0 = failure
  4973. *
  4974. * PARAMETERS:
  4975. * *cd - pointer to core data
  4976. * protect - flag to call protected or non-protected
  4977. * key_size - size of key
  4978. * *key_buf - pointer to key data to allow operation
  4979. * row_size - size of the meta data row
  4980. * *metadata_row_buf - pointer to meta data to write
  4981. ******************************************************************************/
  4982. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4983. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4984. {
  4985. u16 write_length = key_size + row_size;
  4986. u8 *write_buf;
  4987. int rc = 0;
  4988. struct pt_hid_output hid_output = {
  4989. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4990. .write_length = write_length,
  4991. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4992. };
  4993. write_buf = kzalloc(write_length, GFP_KERNEL);
  4994. if (!write_buf)
  4995. return -ENOMEM;
  4996. hid_output.write_buf = write_buf;
  4997. if (key_size)
  4998. memcpy(write_buf, key_buf, key_size);
  4999. if (row_size)
  5000. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5001. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5002. kfree(write_buf);
  5003. return rc;
  5004. }
  5005. /*******************************************************************************
  5006. * FUNCTION: pt_hid_output_bl_initiate_bl
  5007. *
  5008. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5009. * access to the DUT.
  5010. *
  5011. * RETURN:
  5012. * 0 = success
  5013. * !0 = failure
  5014. *
  5015. * PARAMETERS:
  5016. * *cd - pointer to core data
  5017. * key_size - size of key
  5018. * *key_buf - pointer to key data to allow operation
  5019. * row_size - size of the meta data row
  5020. * *metadata_row_buf - pointer to meta data to write
  5021. ******************************************************************************/
  5022. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5023. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5024. {
  5025. int rc;
  5026. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5027. if (rc < 0) {
  5028. pt_debug(cd->dev, DL_ERROR,
  5029. "%s: fail get exclusive ex=%p own=%p\n",
  5030. __func__, cd->exclusive_dev, cd->dev);
  5031. return rc;
  5032. }
  5033. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5034. row_size, metadata_row_buf);
  5035. if (release_exclusive(cd, cd->dev) < 0)
  5036. pt_debug(cd->dev, DL_ERROR,
  5037. "%s: fail to release exclusive\n", __func__);
  5038. return rc;
  5039. }
  5040. /*******************************************************************************
  5041. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5042. *
  5043. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5044. * calls to the protected or unprotected call to
  5045. * pt_hid_output_bl_initiate_bl
  5046. *
  5047. * RETURN:
  5048. * 0 = success
  5049. * !0 = failure
  5050. *
  5051. * PARAMETERS:
  5052. * *dev - pointer to device structure
  5053. * protect - flag to call protected or non-protected
  5054. * key_size - size of key
  5055. * *key_buf - pointer to key data to allow operation
  5056. * row_size - size of the meta data row
  5057. * *metadata_row_buf - pointer to meta data to write
  5058. ******************************************************************************/
  5059. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5060. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5061. u8 *metadata_row_buf)
  5062. {
  5063. struct pt_core_data *cd = dev_get_drvdata(dev);
  5064. if (protect)
  5065. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5066. row_size, metadata_row_buf);
  5067. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5068. row_size, metadata_row_buf);
  5069. }
  5070. /*******************************************************************************
  5071. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5072. *
  5073. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5074. * and program a 128-byte row into the flash, and then verifies written data.
  5075. *
  5076. * RETURN:
  5077. * 0 = success
  5078. * !0 = failure
  5079. *
  5080. * PARAMETERS:
  5081. * *cd - pointer to core data
  5082. * data_len - length of data_buf
  5083. * *data_buf - firmware image to program
  5084. ******************************************************************************/
  5085. static int pt_hid_output_bl_program_and_verify_(
  5086. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5087. {
  5088. struct pt_hid_output hid_output = {
  5089. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5090. .write_length = data_len,
  5091. .write_buf = data_buf,
  5092. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5093. };
  5094. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5095. }
  5096. /*******************************************************************************
  5097. * FUNCTION: pt_hid_output_bl_program_and_verify
  5098. *
  5099. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5100. * access to the DUT.
  5101. *
  5102. * RETURN:
  5103. * 0 = success
  5104. * !0 = failure
  5105. *
  5106. * PARAMETERS:
  5107. * *cd - pointer to core data
  5108. * data_len - length of data_buf
  5109. * *data_buf - firmware image to program
  5110. ******************************************************************************/
  5111. static int pt_hid_output_bl_program_and_verify(
  5112. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5113. {
  5114. int rc;
  5115. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5116. if (rc < 0) {
  5117. pt_debug(cd->dev, DL_ERROR,
  5118. "%s: fail get exclusive ex=%p own=%p\n",
  5119. __func__, cd->exclusive_dev, cd->dev);
  5120. return rc;
  5121. }
  5122. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5123. if (release_exclusive(cd, cd->dev) < 0)
  5124. pt_debug(cd->dev, DL_ERROR,
  5125. "%s: fail to release exclusive\n", __func__);
  5126. return rc;
  5127. }
  5128. /*******************************************************************************
  5129. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5130. *
  5131. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5132. * to request to have the BL program and verify a FW image
  5133. *
  5134. * RETURN:
  5135. * 0 = success
  5136. * !0 = failure
  5137. *
  5138. * PARAMETERS:
  5139. * *dev - pointer to device structure
  5140. * protect - boolean to determine to call the protected function
  5141. * data_len - length of data_buf
  5142. * *data_buf - firmware image to program
  5143. ******************************************************************************/
  5144. static int _pt_request_pip_bl_program_and_verify(
  5145. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5146. {
  5147. struct pt_core_data *cd = dev_get_drvdata(dev);
  5148. if (protect)
  5149. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5150. data_buf);
  5151. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5152. data_buf);
  5153. }
  5154. /*******************************************************************************
  5155. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5156. *
  5157. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5158. * perform a full verification of the application integrity by calculating the
  5159. * CRC of the image in flash and compare it to the expected CRC stored in the
  5160. * Metadata row.
  5161. *
  5162. * RETURN:
  5163. * 0 = success
  5164. * !0 = failure
  5165. *
  5166. * PARAMETERS:
  5167. * *cd - pointer to core data
  5168. * *result - pointer to store result
  5169. ******************************************************************************/
  5170. static int pt_hid_output_bl_verify_app_integrity_(
  5171. struct pt_core_data *cd, u8 *result)
  5172. {
  5173. int rc;
  5174. struct pt_hid_output hid_output = {
  5175. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5176. };
  5177. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5178. if (rc) {
  5179. *result = 0;
  5180. return rc;
  5181. }
  5182. *result = cd->response_buf[8];
  5183. return 0;
  5184. }
  5185. /*******************************************************************************
  5186. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5187. *
  5188. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5189. * exclusive access to the DUT.
  5190. *
  5191. * RETURN:
  5192. * 0 = success
  5193. * !0 = failure
  5194. *
  5195. * PARAMETERS:
  5196. * *cd - pointer to core data
  5197. * *result - pointer to store result
  5198. ******************************************************************************/
  5199. static int pt_hid_output_bl_verify_app_integrity(
  5200. struct pt_core_data *cd, u8 *result)
  5201. {
  5202. int rc;
  5203. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5204. if (rc < 0) {
  5205. pt_debug(cd->dev, DL_ERROR,
  5206. "%s: fail get exclusive ex=%p own=%p\n",
  5207. __func__, cd->exclusive_dev, cd->dev);
  5208. return rc;
  5209. }
  5210. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5211. if (release_exclusive(cd, cd->dev) < 0)
  5212. pt_debug(cd->dev, DL_ERROR,
  5213. "%s: fail to release exclusive\n", __func__);
  5214. return rc;
  5215. }
  5216. /*******************************************************************************
  5217. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5218. *
  5219. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5220. * to request to have the BL verify the application integrity (PIP1.x only)
  5221. *
  5222. * RETURN:
  5223. * 0 = success
  5224. * !0 = failure
  5225. *
  5226. * PARAMETERS:
  5227. * *dev - pointer to device structure
  5228. * protect - boolean to determine to call the protected function
  5229. * *result - pointer to store result
  5230. ******************************************************************************/
  5231. static int _pt_request_pip_bl_verify_app_integrity(
  5232. struct device *dev, int protect, u8 *result)
  5233. {
  5234. struct pt_core_data *cd = dev_get_drvdata(dev);
  5235. if (protect)
  5236. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5237. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5238. }
  5239. /*******************************************************************************
  5240. * FUNCTION: pt_hid_output_bl_launch_app_
  5241. *
  5242. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5243. * application from bootloader (PIP1.x only).
  5244. *
  5245. * RETURN:
  5246. * 0 = success
  5247. * !0 = failure
  5248. *
  5249. * PARAMETERS:
  5250. * *cd - pointer to core data
  5251. ******************************************************************************/
  5252. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5253. {
  5254. struct pt_hid_output hid_output = {
  5255. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5256. .reset_expected = 1,
  5257. };
  5258. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5259. }
  5260. /*******************************************************************************
  5261. * FUNCTION: pt_hid_output_bl_launch_app
  5262. *
  5263. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5264. * to the DUT.
  5265. *
  5266. * RETURN:
  5267. * 0 = success
  5268. * !0 = failure
  5269. *
  5270. * PARAMETERS:
  5271. * *cd - pointer to core data
  5272. ******************************************************************************/
  5273. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5274. {
  5275. int rc;
  5276. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5277. if (rc < 0) {
  5278. pt_debug(cd->dev, DL_ERROR,
  5279. "%s: fail get exclusive ex=%p own=%p\n",
  5280. __func__, cd->exclusive_dev, cd->dev);
  5281. return rc;
  5282. }
  5283. rc = pt_hid_output_bl_launch_app_(cd);
  5284. if (release_exclusive(cd, cd->dev) < 0)
  5285. pt_debug(cd->dev, DL_ERROR,
  5286. "%s: fail to release exclusive\n", __func__);
  5287. return rc;
  5288. }
  5289. /*******************************************************************************
  5290. * FUNCTION: _pt_request_pip_launch_app
  5291. *
  5292. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5293. * to request to have the BL launch the application. (PIP1.x only)
  5294. *
  5295. * RETURN:
  5296. * 0 = success
  5297. * !0 = failure
  5298. *
  5299. * PARAMETERS:
  5300. * *dev - pointer to device structure
  5301. * protect - boolean to determine to call the protected function
  5302. ******************************************************************************/
  5303. static int _pt_request_pip_launch_app(struct device *dev,
  5304. int protect)
  5305. {
  5306. struct pt_core_data *cd = dev_get_drvdata(dev);
  5307. if (protect)
  5308. return pt_hid_output_bl_launch_app(cd);
  5309. return pt_hid_output_bl_launch_app_(cd);
  5310. }
  5311. /*******************************************************************************
  5312. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5313. *
  5314. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5315. * value store in the System Information.
  5316. *
  5317. * RETURN:
  5318. * 0 = success
  5319. * !0 = failure
  5320. *
  5321. * PARAMETERS:
  5322. * *cd - pointer to core data
  5323. * *panel_id - pointer to where the panel ID will be stored
  5324. ******************************************************************************/
  5325. static int pt_hid_output_bl_get_panel_id_(
  5326. struct pt_core_data *cd, u8 *panel_id)
  5327. {
  5328. int rc;
  5329. struct pt_hid_output hid_output = {
  5330. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5331. };
  5332. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5333. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5334. pt_debug(cd->dev, DL_ERROR,
  5335. "%s: Get Panel ID command not supported\n",
  5336. __func__);
  5337. *panel_id = PANEL_ID_NOT_ENABLED;
  5338. return 0;
  5339. } else if (rc < 0) {
  5340. pt_debug(cd->dev, DL_ERROR,
  5341. "%s: Error on Get Panel ID command\n", __func__);
  5342. return rc;
  5343. }
  5344. *panel_id = cd->response_buf[8];
  5345. return 0;
  5346. }
  5347. /*******************************************************************************
  5348. * FUNCTION: pt_hid_output_bl_get_panel_id
  5349. *
  5350. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5351. * to the DUT.
  5352. *
  5353. * RETURN:
  5354. * 0 = success
  5355. * !0 = failure
  5356. *
  5357. * PARAMETERS:
  5358. * *cd - pointer to core data
  5359. * *panel_id - pointer to where the panel ID will be stored
  5360. ******************************************************************************/
  5361. static int pt_hid_output_bl_get_panel_id(
  5362. struct pt_core_data *cd, u8 *panel_id)
  5363. {
  5364. int rc;
  5365. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5366. if (rc < 0) {
  5367. pt_debug(cd->dev, DL_ERROR,
  5368. "%s: fail get exclusive ex=%p own=%p\n",
  5369. __func__, cd->exclusive_dev, cd->dev);
  5370. return rc;
  5371. }
  5372. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5373. if (release_exclusive(cd, cd->dev) < 0)
  5374. pt_debug(cd->dev, DL_ERROR,
  5375. "%s: fail to release exclusive\n", __func__);
  5376. return rc;
  5377. }
  5378. /*******************************************************************************
  5379. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5380. *
  5381. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5382. * to have the BL retrieve the panel ID
  5383. *
  5384. * RETURN:
  5385. * 0 = success
  5386. * !0 = failure
  5387. *
  5388. * PARAMETERS:
  5389. * *dev - pointer to device structure
  5390. * protect - flag to run in protected mode
  5391. * *panel_id - pointer to where the panel ID will be stored
  5392. ******************************************************************************/
  5393. static int _pt_request_pip_bl_get_panel_id(
  5394. struct device *dev, int protect, u8 *panel_id)
  5395. {
  5396. struct pt_core_data *cd = dev_get_drvdata(dev);
  5397. if (protect)
  5398. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5399. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5400. }
  5401. /*******************************************************************************
  5402. * FUNCTION: pt_pip2_get_mode_sysmode_
  5403. *
  5404. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5405. * PIP2 STATUS command.
  5406. *
  5407. * RETURN:
  5408. * 0 = success
  5409. * !0 = failure
  5410. *
  5411. * PARAMETERS:
  5412. * *cd - pointer to core data structure
  5413. * *mode - pointer to store the retrieved mode
  5414. * *sys_mode - pointer to store the FW system mode
  5415. ******************************************************************************/
  5416. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5417. u8 *mode, u8 *sys_mode)
  5418. {
  5419. u16 actual_read_len;
  5420. u8 read_buf[12];
  5421. u8 status, boot;
  5422. int rc = 0;
  5423. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5424. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5425. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5426. __func__, rc);
  5427. if (!rc) {
  5428. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5429. "PIP2 STATUS");
  5430. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5431. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5432. if (sys_mode) {
  5433. if (status == PIP2_RSP_ERR_NONE &&
  5434. boot == PIP2_STATUS_APP_EXEC)
  5435. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5436. else
  5437. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5438. }
  5439. if (mode) {
  5440. if (status == PIP2_RSP_ERR_NONE &&
  5441. boot == PIP2_STATUS_BOOT_EXEC)
  5442. *mode = PT_MODE_BOOTLOADER;
  5443. else if (status == PIP2_RSP_ERR_NONE &&
  5444. boot == PIP2_STATUS_APP_EXEC)
  5445. *mode = PT_MODE_OPERATIONAL;
  5446. else
  5447. *mode = PT_MODE_UNKNOWN;
  5448. }
  5449. } else {
  5450. if (mode)
  5451. *mode = PT_MODE_UNKNOWN;
  5452. if (sys_mode)
  5453. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5454. pt_debug(cd->dev, DL_WARN,
  5455. "%s: Mode and sys_mode could not be determined\n",
  5456. __func__);
  5457. }
  5458. return rc;
  5459. }
  5460. /*******************************************************************************
  5461. * FUNCTION: pt_pip2_get_mode_sysmode
  5462. *
  5463. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5464. * access to the DUT.
  5465. *
  5466. * RETURN:
  5467. * 0 = success
  5468. * !0 = failure
  5469. *
  5470. * PARAMETERS:
  5471. * *cd - pointer to core data structure
  5472. * *mode - pointer to store the retrieved mode
  5473. * *sys_mode - pointer to store the FW system mode
  5474. ******************************************************************************/
  5475. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5476. u8 *mode, u8 *sys_mode)
  5477. {
  5478. int rc;
  5479. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5480. if (rc < 0) {
  5481. pt_debug(cd->dev, DL_ERROR,
  5482. "%s: fail get exclusive ex=%p own=%p\n",
  5483. __func__, cd->exclusive_dev, cd->dev);
  5484. return rc;
  5485. }
  5486. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5487. if (release_exclusive(cd, cd->dev) < 0)
  5488. pt_debug(cd->dev, DL_ERROR,
  5489. "%s: fail to release exclusive\n", __func__);
  5490. return rc;
  5491. }
  5492. /*******************************************************************************
  5493. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5494. *
  5495. * SUMMARY: Function pointer included in core_commands struct for external
  5496. * calls to the protected or unprotected call to
  5497. * pt_pip2_get_mode_sysmode
  5498. *
  5499. * RETURN:
  5500. * 0 = success
  5501. * !0 = failure
  5502. *
  5503. * PARAMETERS:
  5504. * *dev - pointer to device structure
  5505. * *mode - pointer to store the retrieved mode
  5506. * *sys_mode - pointer to store the FW system mode
  5507. ******************************************************************************/
  5508. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5509. int protect, u8 *mode, u8 *sys_mode)
  5510. {
  5511. struct pt_core_data *cd = dev_get_drvdata(dev);
  5512. if (protect)
  5513. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5514. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5515. }
  5516. /*******************************************************************************
  5517. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5518. *
  5519. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5520. * mode only the following PIP commands will be serviced, any other PIP
  5521. * command the FW will respond with an "Invalid PIP Command" response.
  5522. * - Get HID Descriptor (Register 0x0001, no command ID)
  5523. * - Reset (Register 0x0005, RESET HID request)
  5524. * - Ping (Register 0x0004, Command ID 0x00
  5525. * - Get System Information (Register 0x0004, Command ID 0x02)
  5526. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5527. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5528. * This function will loop on the results of the STATUS command until
  5529. * the FW reports it is out of BOOT mode.
  5530. *
  5531. * NOTE:
  5532. * - This function will update cd->fw_system_mode
  5533. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5534. *
  5535. * RETURN:
  5536. * 0 = success
  5537. * !0 = failure
  5538. *
  5539. * PARAMETERS:
  5540. * *cd - pointer to core data
  5541. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5542. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5543. ******************************************************************************/
  5544. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5545. int *actual_wait)
  5546. {
  5547. int loop = 0;
  5548. u8 sys_mode = cd->fw_system_mode;
  5549. u8 pause = 10; /* in ms */
  5550. int rc = 0;
  5551. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5552. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5553. /*
  5554. * For PIP <1.11, no support for polling wait so do a hard
  5555. * coded wait and assume the FW is out of BOOT. Added 1 to
  5556. * timeout to make it clear in kmsg if non polling was done.
  5557. */
  5558. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5559. pt_debug(cd->dev, DL_ERROR,
  5560. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5561. __func__,
  5562. cd->sysinfo.ttdata.pip_ver_major,
  5563. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5564. msleep(*actual_wait);
  5565. sys_mode = FW_SYS_MODE_SCANNING;
  5566. }
  5567. if (sys_mode == FW_SYS_MODE_BOOT) {
  5568. while (!rc && loop <= max_loop &&
  5569. (sys_mode == FW_SYS_MODE_BOOT)) {
  5570. loop++;
  5571. usleep_range(9000, pause * 1000);
  5572. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5573. pt_debug(cd->dev, DL_DEBUG,
  5574. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5575. __func__, loop * pause, sys_mode);
  5576. }
  5577. *actual_wait = (int)(loop * pause);
  5578. pt_debug(cd->dev, DL_WARN,
  5579. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5580. __func__, *actual_wait, sys_mode);
  5581. if (rc)
  5582. sys_mode = FW_SYS_MODE_UNDEFINED;
  5583. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5584. sys_mode == FW_SYS_MODE_UNDEFINED)
  5585. rc = -EBUSY;
  5586. }
  5587. mutex_lock(&cd->system_lock);
  5588. cd->fw_system_mode = sys_mode;
  5589. mutex_unlock(&cd->system_lock);
  5590. return rc;
  5591. }
  5592. /*******************************************************************************
  5593. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5594. *
  5595. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5596. * access to the DUT.
  5597. *
  5598. * RETURN:
  5599. * 0 = success
  5600. * !0 = failure
  5601. *
  5602. * PARAMETERS:
  5603. * *cd - pointer to core data
  5604. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5605. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5606. ******************************************************************************/
  5607. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5608. int *actual_wait)
  5609. {
  5610. int rc;
  5611. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5612. if (rc < 0) {
  5613. pt_debug(cd->dev, DL_ERROR,
  5614. "%s: fail get exclusive ex=%p own=%p\n",
  5615. __func__, cd->exclusive_dev, cd->dev);
  5616. return rc;
  5617. }
  5618. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5619. if (release_exclusive(cd, cd->dev) < 0)
  5620. pt_debug(cd->dev, DL_ERROR,
  5621. "%s: fail to release exclusive\n", __func__);
  5622. return rc;
  5623. }
  5624. /*******************************************************************************
  5625. * FUNCTION: _pt_get_fw_sys_mode
  5626. *
  5627. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5628. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5629. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5630. * information but any PIP command above 0x1F requires scanning to be disabled
  5631. * before it will be operational. If scanning was not disabled before sending
  5632. * these PIP commands the FW will respond with a 6 byte error response. So to
  5633. * safely determine the scanning state, a PIP message that will not affect the
  5634. * operation of the FW was chosen:
  5635. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5636. * received scanning is active.
  5637. *
  5638. * RETURN:
  5639. * 0 = success
  5640. * !0 = failure
  5641. *
  5642. * PARAMETERS:
  5643. * *cd - pointer to core data
  5644. * *sys_mode - pointer to FW System mode
  5645. * *mode - pointer to mode (BL/FW)
  5646. ******************************************************************************/
  5647. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5648. {
  5649. int write_length = 1;
  5650. int report_length;
  5651. int rc = 0;
  5652. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5653. u8 tmp_mode = PT_MODE_UNKNOWN;
  5654. u8 param[1] = { PT_TCH_PARM_EBID };
  5655. struct pt_hid_output hid_output = {
  5656. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5657. .write_length = write_length,
  5658. .write_buf = param,
  5659. .novalidate = true,
  5660. };
  5661. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5662. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5663. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5664. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5665. __func__, tmp_sys_mode, tmp_mode);
  5666. if (!rc) {
  5667. if (tmp_mode != PT_MODE_OPERATIONAL)
  5668. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5669. }
  5670. goto exit;
  5671. }
  5672. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5673. if (cd->mode != PT_MODE_OPERATIONAL) {
  5674. tmp_mode = cd->mode;
  5675. goto exit;
  5676. }
  5677. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5678. if (rc)
  5679. goto exit;
  5680. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5681. if ((report_length == 0x06) &&
  5682. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5683. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5684. tmp_mode = PIP2_STATUS_APP_EXEC;
  5685. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5686. } else if ((report_length == 0x0A) &&
  5687. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5688. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5689. tmp_mode = PIP2_STATUS_APP_EXEC;
  5690. tmp_sys_mode = FW_SYS_MODE_TEST;
  5691. }
  5692. exit:
  5693. if (mode)
  5694. *mode = tmp_mode;
  5695. if (sys_mode)
  5696. *sys_mode = tmp_sys_mode;
  5697. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5698. __func__, tmp_mode, tmp_sys_mode);
  5699. return rc;
  5700. }
  5701. /*******************************************************************************
  5702. * FUNCTION: pt_get_fw_sys_mode
  5703. *
  5704. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5705. * is active or not.
  5706. *
  5707. * RETURN:
  5708. * 0 = success
  5709. * !0 = failure
  5710. *
  5711. * PARAMETERS:
  5712. * *cd - pointer to core data
  5713. * *sys_mode - pointer to fw system mode
  5714. * *mode - pointer to mode
  5715. ******************************************************************************/
  5716. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5717. {
  5718. int rc;
  5719. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5720. if (rc < 0) {
  5721. pt_debug(cd->dev, DL_ERROR,
  5722. "%s: fail get exclusive ex=%p own=%p\n",
  5723. __func__, cd->exclusive_dev, cd->dev);
  5724. return rc;
  5725. }
  5726. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5727. if (release_exclusive(cd, cd->dev) < 0)
  5728. pt_debug(cd->dev, DL_ERROR,
  5729. "%s: fail to release exclusive\n", __func__);
  5730. return rc;
  5731. }
  5732. /*******************************************************************************
  5733. * FUNCTION: _pt_request_get_fw_sys_mode
  5734. *
  5735. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5736. * to request to get scan state
  5737. *
  5738. * RETURN:
  5739. * 0 = success
  5740. * !0 = failure
  5741. *
  5742. * PARAMETERS:
  5743. * *dev - pointer to device structure
  5744. * protect - flag to call protected or non-protected
  5745. * *sys_mode - pointer to FW system mode
  5746. * *mode - pointer to mode
  5747. ******************************************************************************/
  5748. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5749. u8 *sys_mode, u8 *mode)
  5750. {
  5751. struct pt_core_data *cd = dev_get_drvdata(dev);
  5752. if (protect)
  5753. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5754. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5755. }
  5756. /* Default hid descriptor to provide basic register map */
  5757. const struct pt_hid_desc hid_desc_default = {
  5758. 230, /* hid_desc_len */
  5759. HID_APP_REPORT_ID, /* packet_id */
  5760. 0x00, /* reserved_byte */
  5761. 0x0100, /* bcd_version */
  5762. 0x00EC, /* report_desc_len */
  5763. 0x0002, /* report_desc_register */
  5764. 0x0003, /* input_register */
  5765. 0x00FE, /* max_input_len */
  5766. 0x0004, /* output_register */
  5767. 0x00FE, /* max_output_len */
  5768. 0x0005, /* command_register */
  5769. 0x0006, /* data_register */
  5770. 0x04B4, /* vendor_id */
  5771. 0xC101, /* product_id */
  5772. 0x0100, /* version_id */
  5773. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5774. };
  5775. /*******************************************************************************
  5776. * FUNCTION: pt_init_hid_descriptor
  5777. *
  5778. * SUMMARY: Setup default values for HID descriptor structure
  5779. *
  5780. *
  5781. * PARAMETERS:
  5782. * *desc - pointer to the HID descriptor data read back from DUT
  5783. ******************************************************************************/
  5784. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5785. {
  5786. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5787. }
  5788. /*******************************************************************************
  5789. * FUNCTION: pt_get_hid_descriptor_
  5790. *
  5791. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5792. * into the HID descriptor structure
  5793. *
  5794. * RETURN:
  5795. * 0 = success
  5796. * !0 = failure
  5797. *
  5798. * PARAMETERS:
  5799. * *cd - pointer to core data
  5800. * *desc - pointer to the HID descriptor data read back from DUT
  5801. ******************************************************************************/
  5802. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5803. struct pt_hid_desc *desc)
  5804. {
  5805. struct device *dev = cd->dev;
  5806. int rc = 0;
  5807. int t;
  5808. u8 cmd[2];
  5809. /*
  5810. * During startup the HID descriptor is required for all future
  5811. * processing. If IRQ is already asserted due to an early touch report
  5812. * the report must be cleared before sending command.
  5813. */
  5814. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5815. /* Read HID descriptor length and version */
  5816. mutex_lock(&cd->system_lock);
  5817. cd->hid_cmd_state = 1;
  5818. mutex_unlock(&cd->system_lock);
  5819. /* Set HID descriptor register */
  5820. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5821. sizeof(cd->hid_core.hid_desc_register));
  5822. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5823. __func__, sizeof(cmd));
  5824. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5825. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5826. if (rc) {
  5827. pt_debug(dev, DL_ERROR,
  5828. "%s: failed to get HID descriptor, rc=%d\n",
  5829. __func__, rc);
  5830. goto error;
  5831. }
  5832. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5833. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5834. if (IS_TMO(t)) {
  5835. #ifdef TTDL_DIAGNOSTICS
  5836. cd->bus_transmit_error_count++;
  5837. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5838. #endif /* TTDL_DIAGNOSTICS */
  5839. pt_debug(cd->dev, DL_ERROR,
  5840. "%s: HID get descriptor timed out\n", __func__);
  5841. rc = -ETIME;
  5842. goto error;
  5843. } else {
  5844. cd->hw_detected = true;
  5845. }
  5846. /* Load the HID descriptor including all registers */
  5847. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5848. /* Check HID descriptor length and version */
  5849. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5850. le16_to_cpu(desc->hid_desc_len),
  5851. le16_to_cpu(desc->bcd_version));
  5852. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5853. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5854. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5855. __func__);
  5856. return -ENODEV;
  5857. }
  5858. goto exit;
  5859. error:
  5860. mutex_lock(&cd->system_lock);
  5861. cd->hid_cmd_state = 0;
  5862. mutex_unlock(&cd->system_lock);
  5863. exit:
  5864. return rc;
  5865. }
  5866. /*******************************************************************************
  5867. * FUNCTION: pt_get_hid_descriptor
  5868. *
  5869. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5870. *
  5871. * RETURN:
  5872. * 0 = success
  5873. * !0 = failure
  5874. *
  5875. * PARAMETERS:
  5876. * *cd - pointer to core data
  5877. * *desc - pointer to the HID descriptor data read back from DUT
  5878. ******************************************************************************/
  5879. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5880. struct pt_hid_desc *desc)
  5881. {
  5882. int rc;
  5883. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5884. if (rc < 0) {
  5885. pt_debug(cd->dev, DL_ERROR,
  5886. "%s: fail get exclusive ex=%p own=%p\n",
  5887. __func__, cd->exclusive_dev, cd->dev);
  5888. return rc;
  5889. }
  5890. rc = pt_get_hid_descriptor_(cd, desc);
  5891. if (release_exclusive(cd, cd->dev) < 0)
  5892. pt_debug(cd->dev, DL_ERROR,
  5893. "%s: fail to release exclusive\n", __func__);
  5894. return rc;
  5895. }
  5896. /*******************************************************************************
  5897. * FUNCTION: pt_pip2_get_version_
  5898. *
  5899. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5900. * cd-ttdata
  5901. *
  5902. * RETURN:
  5903. * 0 = success
  5904. * !0 = failure
  5905. *
  5906. * PARAMETERS:
  5907. * *cd - pointer to core data
  5908. ******************************************************************************/
  5909. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5910. {
  5911. int rc = 0;
  5912. int status;
  5913. u8 read_buf[64];
  5914. u16 actual_read_len;
  5915. rc = _pt_request_pip2_send_cmd(cd->dev,
  5916. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5917. NULL, 0, read_buf, &actual_read_len);
  5918. if (rc) {
  5919. pt_debug(cd->dev, DL_ERROR,
  5920. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5921. __func__, rc);
  5922. return rc;
  5923. }
  5924. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5925. if (status == 0) {
  5926. /* Parse the PIP2 VERSION response into ttdata */
  5927. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5928. } else {
  5929. pt_debug(cd->dev, DL_ERROR,
  5930. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5931. __func__, status);
  5932. return status;
  5933. }
  5934. return rc;
  5935. }
  5936. /*******************************************************************************
  5937. * FUNCTION: pt_pip2_get_version
  5938. *
  5939. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5940. * access to the DUT.
  5941. *
  5942. * RETURN:
  5943. * 0 = success
  5944. * !0 = failure
  5945. *
  5946. * PARAMETERS:
  5947. * *cd - pointer to core data structure
  5948. ******************************************************************************/
  5949. static int pt_pip2_get_version(struct pt_core_data *cd)
  5950. {
  5951. int rc;
  5952. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5953. if (rc < 0) {
  5954. pt_debug(cd->dev, DL_ERROR,
  5955. "%s: fail get exclusive ex=%p own=%p\n",
  5956. __func__, cd->exclusive_dev, cd->dev);
  5957. return rc;
  5958. }
  5959. rc = pt_pip2_get_version_(cd);
  5960. if (release_exclusive(cd, cd->dev) < 0)
  5961. pt_debug(cd->dev, DL_ERROR,
  5962. "%s: fail to release exclusive\n", __func__);
  5963. return rc;
  5964. }
  5965. /*******************************************************************************
  5966. * FUNCTION: _pt_request_active_pip_protocol
  5967. *
  5968. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5969. * Function will return PIP version of BL or application based on
  5970. * when it's called.
  5971. *
  5972. * RETURN:
  5973. * 0 = success
  5974. * !0 = failure
  5975. *
  5976. * PARAMETERS:
  5977. * *dev - pointer to device structure
  5978. * protect - flag to run in protected mode
  5979. * *pip_version_major - pointer to store PIP major version
  5980. * *pip_version_minor - pointer to store PIP minor version
  5981. ******************************************************************************/
  5982. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5983. u8 *pip_version_major, u8 *pip_version_minor)
  5984. {
  5985. struct pt_core_data *cd = dev_get_drvdata(dev);
  5986. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5987. int rc = 0;
  5988. struct pt_hid_output sys_info = {
  5989. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5990. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5991. };
  5992. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5993. /* Skip PIP2 command if DUT generation is confirmed */
  5994. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5995. goto skip_pip2_command;
  5996. rc = pt_pip2_get_version_(cd);
  5997. if (!rc) {
  5998. *pip_version_major = ttdata->pip_ver_major;
  5999. *pip_version_minor = ttdata->pip_ver_minor;
  6000. pt_debug(dev, DL_INFO,
  6001. "%s: pip_version = %d.%d\n", __func__,
  6002. *pip_version_major, *pip_version_minor);
  6003. } else {
  6004. /*
  6005. * Legacy products do not support the pip2 protocol to get
  6006. * pip version. However, they do support the "get sysinfo"
  6007. * command to get pip version from FW, but the bootloader
  6008. * does not support it. This function will try "get sysinfo"
  6009. * command if the pip2 command failed but this cmd could also
  6010. * fail if DUT is stuck in bootloader mode.
  6011. */
  6012. pt_debug(dev, DL_INFO,
  6013. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6014. __func__, rc);
  6015. skip_pip2_command:
  6016. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6017. if (!rc) {
  6018. *pip_version_minor =
  6019. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6020. *pip_version_major =
  6021. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6022. pt_debug(dev, DL_INFO,
  6023. "%s: pip_version = %d.%d\n", __func__,
  6024. *pip_version_major, *pip_version_minor);
  6025. } else {
  6026. *pip_version_major = 0;
  6027. *pip_version_minor = 0;
  6028. pt_debug(dev, DL_ERROR,
  6029. "%s: pip_version Not Detected\n", __func__);
  6030. }
  6031. }
  6032. return rc;
  6033. }
  6034. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6035. /*******************************************************************************
  6036. * FUNCTION: _pt_detect_dut_generation
  6037. *
  6038. * SUMMARY: Determine the generation of device that we are communicating with:
  6039. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6040. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6041. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6042. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6043. * coming form the BL or the FW:
  6044. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6045. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6046. * This function will return a modified status if it detects the DUT
  6047. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6048. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6049. *
  6050. * RETURN:
  6051. * 0 = success
  6052. * !0 = failure
  6053. *
  6054. * PARAMETERS:
  6055. * *dev - pointer to device structure
  6056. * *status - pointer to status bitmask
  6057. * *dut_gen - pointer to store the dut_generation
  6058. * *mode - pointer to store the PT_MODE
  6059. ******************************************************************************/
  6060. static int _pt_detect_dut_generation(struct device *dev,
  6061. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6062. {
  6063. int rc = 0;
  6064. u8 dut_gen_tmp = DUT_UNKNOWN;
  6065. u8 mode_tmp = PT_MODE_UNKNOWN;
  6066. u8 attempt = 1;
  6067. u32 status_tmp = STARTUP_STATUS_START;
  6068. struct pt_core_data *cd = dev_get_drvdata(dev);
  6069. struct pt_hid_desc hid_desc;
  6070. memset(&hid_desc, 0, sizeof(hid_desc));
  6071. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6072. while (rc && attempt < 3) {
  6073. attempt++;
  6074. usleep_range(2000, 5000);
  6075. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6076. }
  6077. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6078. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6079. mode_tmp = PT_MODE_BOOTLOADER;
  6080. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6081. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6082. rc = pt_pip2_get_version_(cd);
  6083. if (!rc)
  6084. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6085. else
  6086. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6087. mode_tmp = PT_MODE_OPERATIONAL;
  6088. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6089. rc = 0; /* To return success instead of error code */
  6090. } else if (rc) {
  6091. rc = pt_pip2_get_version_(cd);
  6092. if (!rc) {
  6093. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6094. mode_tmp = PT_MODE_BOOTLOADER;
  6095. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6096. }
  6097. }
  6098. mutex_lock(&cd->system_lock);
  6099. if (dut_gen)
  6100. *dut_gen = dut_gen_tmp;
  6101. if (mode)
  6102. *mode = mode_tmp;
  6103. if (status)
  6104. *status = status_tmp;
  6105. mutex_unlock(&cd->system_lock);
  6106. #ifdef TTDL_DIAGNOSTICS
  6107. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6108. __func__, dut_gen_tmp, mode_tmp);
  6109. #endif /* TTDL_DIAGNOSTICS */
  6110. return rc;
  6111. }
  6112. /*******************************************************************************
  6113. * FUNCTION: _pt_request_dut_generation
  6114. *
  6115. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6116. * to get current dut generation.
  6117. *
  6118. * NOTE: This function WILL NOT try to determine dut generation.
  6119. *
  6120. * RETURN:
  6121. * The current dut generation.
  6122. *
  6123. * PARAMETERS:
  6124. * *dev - pointer to device structure
  6125. ******************************************************************************/
  6126. static int _pt_request_dut_generation(struct device *dev)
  6127. {
  6128. struct pt_core_data *cd = dev_get_drvdata(dev);
  6129. return cd->active_dut_generation;
  6130. }
  6131. #define HW_VERSION_LEN_MAX 13
  6132. /*******************************************************************************
  6133. * FUNCTION: _legacy_generate_hw_version
  6134. *
  6135. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6136. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6137. * command) to the hw_version.
  6138. *
  6139. * RETURN:
  6140. * 0 = success
  6141. * !0 = failure
  6142. *
  6143. * PARAMETERS:
  6144. * *cd - pointer to core data
  6145. * *hw_version - pointer to store the hardware version
  6146. ******************************************************************************/
  6147. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6148. char *hw_version)
  6149. {
  6150. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6151. if (cd->sysinfo.ready) {
  6152. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6153. ttdata->jtag_id_h, cd->pid_for_loader);
  6154. return 0;
  6155. } else if (cd->bl_info.ready) {
  6156. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6157. cd->bl_info.chip_id, cd->pid_for_loader);
  6158. return 0;
  6159. } else {
  6160. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6161. pt_debug(cd->dev, DL_ERROR,
  6162. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6163. return -ENODATA;
  6164. }
  6165. }
  6166. /*******************************************************************************
  6167. * FUNCTION: _pip2_generate_hw_version
  6168. *
  6169. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6170. * VERSION command) to the hw_version.
  6171. *
  6172. * RETURN:
  6173. * 0 = success
  6174. * !0 = failure
  6175. *
  6176. * PARAMETERS:
  6177. * *cd - pointer to core data
  6178. * *hw_version - pointer to store the hardware version
  6179. ******************************************************************************/
  6180. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6181. {
  6182. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6183. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6184. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6185. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6186. return 0;
  6187. } else {
  6188. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6189. pt_debug(cd->dev, DL_ERROR,
  6190. "%s: PIP Version are not ready\n", __func__);
  6191. return -ENODATA;
  6192. }
  6193. }
  6194. /*******************************************************************************
  6195. * FUNCTION: pt_generate_hw_version
  6196. *
  6197. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6198. * hw_version from static data.
  6199. *
  6200. * RETURN:
  6201. * 0 = success
  6202. * !0 = failure
  6203. *
  6204. * PARAMETERS:
  6205. * *cd - pointer to core data
  6206. * *hw_version - pointer to store the hardware version
  6207. ******************************************************************************/
  6208. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6209. {
  6210. int rc = 0;
  6211. if (!hw_version)
  6212. return -ENOMEM;
  6213. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6214. rc = _legacy_generate_hw_version(cd, hw_version);
  6215. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6216. rc = _pip2_generate_hw_version(cd, hw_version);
  6217. else {
  6218. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6219. rc = -ENODATA;
  6220. }
  6221. return rc;
  6222. }
  6223. /*******************************************************************************
  6224. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6225. *
  6226. * NOTE: The calling function must ensure to free *hw_version
  6227. *
  6228. * RETURN:
  6229. * 0 = success
  6230. * !0 = Failure
  6231. *
  6232. * PARAMETERS:
  6233. * *dev - pointer to device structure
  6234. * *hw_version - pointer to where the hw_version string will be stored
  6235. ******************************************************************************/
  6236. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6237. {
  6238. int rc = 0;
  6239. u16 actual_read_len;
  6240. u16 pip_ver;
  6241. u8 rd_buf[256];
  6242. u8 status;
  6243. u8 index = PIP2_RESP_STATUS_OFFSET;
  6244. u8 return_data[8];
  6245. u8 panel_id;
  6246. struct pt_core_data *cd = dev_get_drvdata(dev);
  6247. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6248. if (!hw_version)
  6249. return -ENOMEM;
  6250. if (!cd->hw_detected) {
  6251. /* No HW detected */
  6252. rc = -ENODEV;
  6253. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6254. __func__);
  6255. goto exit_error;
  6256. }
  6257. memset(return_data, 0, ARRAY_SIZE(return_data));
  6258. /* For Parade TC or TT parts */
  6259. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6260. rc = _pt_request_pip2_send_cmd(dev,
  6261. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6262. NULL, 0, rd_buf, &actual_read_len);
  6263. if (rc) {
  6264. pt_debug(dev, DL_ERROR,
  6265. "%s: Failed to send PIP2 VERSION cmd\n",
  6266. __func__);
  6267. goto exit_error;
  6268. }
  6269. status = rd_buf[index];
  6270. if (status == 0) {
  6271. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6272. /*
  6273. * BL PIP 2.02 and greater the version fields are
  6274. * swapped
  6275. */
  6276. if (pip_ver >= 0x0202) {
  6277. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6278. "%02X%02X.%02X%02X.FF",
  6279. rd_buf[index + 10], rd_buf[index + 9],
  6280. rd_buf[index + 8], rd_buf[index + 7]);
  6281. } else {
  6282. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6283. "%02X%02X.%02X%02X.FF",
  6284. rd_buf[index + 8], rd_buf[index + 7],
  6285. rd_buf[index + 10], rd_buf[index + 9]);
  6286. }
  6287. return STATUS_SUCCESS;
  6288. } else {
  6289. rc = status;
  6290. pt_debug(dev, DL_WARN,
  6291. "%s: PIP2 VERSION cmd response error\n",
  6292. __func__);
  6293. }
  6294. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6295. /*
  6296. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6297. * hard coded to FFFF
  6298. */
  6299. if (cd->mode == PT_MODE_OPERATIONAL) {
  6300. rc = pt_hid_output_get_sysinfo(cd);
  6301. if (!rc) {
  6302. panel_id =
  6303. cd->sysinfo.sensing_conf_data.panel_id;
  6304. } else {
  6305. panel_id = PANEL_ID_NOT_ENABLED;
  6306. }
  6307. /* In FW - simply retrieve from ttdata struct */
  6308. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6309. "%04X.FFFF.%02X",
  6310. ttdata->jtag_id_h,
  6311. panel_id);
  6312. return STATUS_SUCCESS;
  6313. } else {
  6314. /*
  6315. * Return the stored value if PT_PANEL_ID_BY_BL
  6316. * is not supported while other feature is.
  6317. */
  6318. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6319. rc = pt_hid_output_bl_get_information(
  6320. cd, return_data);
  6321. if (!rc) {
  6322. rc = pt_hid_output_bl_get_panel_id(
  6323. cd, &panel_id);
  6324. }
  6325. } else
  6326. panel_id = cd->pid_for_loader;
  6327. if (!rc) {
  6328. snprintf(hw_version,
  6329. HW_VERSION_LEN_MAX,
  6330. "%02X%02X.FFFF.%02X",
  6331. return_data[3], return_data[2],
  6332. panel_id);
  6333. return STATUS_SUCCESS;
  6334. }
  6335. }
  6336. } else {
  6337. /* Unknown generation */
  6338. rc = -ENODEV;
  6339. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6340. __func__);
  6341. }
  6342. exit_error:
  6343. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6344. return rc;
  6345. }
  6346. /*******************************************************************************
  6347. * FUNCTION: pt_start_wd_timer
  6348. *
  6349. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6350. *
  6351. * RETURN: void
  6352. *
  6353. * PARAMETERS:
  6354. * *cd - pointer to core data
  6355. ******************************************************************************/
  6356. static void pt_start_wd_timer(struct pt_core_data *cd)
  6357. {
  6358. if (cd->watchdog_interval < 100) {
  6359. pt_debug(cd->dev, DL_ERROR,
  6360. "%s: WARNING: Invalid watchdog interval: %d\n",
  6361. __func__, cd->watchdog_interval);
  6362. return;
  6363. }
  6364. if (cd->watchdog_force_stop) {
  6365. pt_debug(cd->dev, DL_INFO,
  6366. "%s: TTDL WD Forced stop\n", __func__);
  6367. return;
  6368. }
  6369. mod_timer(&cd->watchdog_timer, jiffies +
  6370. msecs_to_jiffies(cd->watchdog_interval));
  6371. cd->watchdog_enabled = 1;
  6372. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6373. }
  6374. /*******************************************************************************
  6375. * FUNCTION: pt_stop_wd_timer
  6376. *
  6377. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6378. *
  6379. * RETURN: void
  6380. *
  6381. * PARAMETERS:
  6382. * *cd - pointer to core data
  6383. ******************************************************************************/
  6384. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6385. {
  6386. if (!cd->watchdog_interval)
  6387. return;
  6388. /*
  6389. * Ensure we wait until the watchdog timer
  6390. * running on a different CPU finishes
  6391. */
  6392. del_timer_sync(&cd->watchdog_timer);
  6393. cancel_work_sync(&cd->watchdog_work);
  6394. del_timer_sync(&cd->watchdog_timer);
  6395. cd->watchdog_enabled = 0;
  6396. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6397. }
  6398. /*******************************************************************************
  6399. * FUNCTION: pt_hw_soft_reset
  6400. *
  6401. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6402. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6403. * schedule an enum if it fires when the DUT is being reset.
  6404. * This can cause a double reset.
  6405. *
  6406. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6407. * the WD active could cause this function to fail!
  6408. *
  6409. * RETURN:
  6410. * 0 = success
  6411. * !0 = failure
  6412. *
  6413. * PARAMETERS:
  6414. * *cd - pointer to core data struct
  6415. * protect - flag to call protected or non-protected
  6416. ******************************************************************************/
  6417. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6418. {
  6419. int rc = 0;
  6420. mutex_lock(&cd->system_lock);
  6421. cd->startup_status = STARTUP_STATUS_START;
  6422. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6423. mutex_unlock(&cd->system_lock);
  6424. if (protect)
  6425. rc = pt_hid_cmd_reset(cd);
  6426. else
  6427. rc = pt_hid_cmd_reset_(cd);
  6428. if (rc < 0) {
  6429. pt_debug(cd->dev, DL_ERROR,
  6430. "%s: FAILED to execute SOFT reset\n", __func__);
  6431. return rc;
  6432. }
  6433. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6434. __func__);
  6435. return 0;
  6436. }
  6437. /*******************************************************************************
  6438. * FUNCTION: pt_hw_hard_reset
  6439. *
  6440. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6441. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6442. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6443. * schedule an enum if it fires when the DUT is being reset.
  6444. * This can cause a double reset.
  6445. *
  6446. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6447. * the WD active could cause this function to fail!
  6448. *
  6449. * RETURN:
  6450. * 0 = success
  6451. * !0 = failure
  6452. *
  6453. * PARAMETERS:
  6454. * *cd - pointer to core data struct
  6455. ******************************************************************************/
  6456. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6457. {
  6458. if (cd->cpdata->xres) {
  6459. cd->startup_status = STARTUP_STATUS_START;
  6460. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6461. __func__);
  6462. cd->cpdata->xres(cd->cpdata, cd->dev);
  6463. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6464. __func__);
  6465. return 0;
  6466. }
  6467. pt_debug(cd->dev, DL_ERROR,
  6468. "%s: FAILED to execute HARD reset\n", __func__);
  6469. return -ENODEV;
  6470. }
  6471. /*******************************************************************************
  6472. * FUNCTION: pt_dut_reset
  6473. *
  6474. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6475. * soft reset.
  6476. *
  6477. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6478. * the WD active could cause this function to fail!
  6479. * NOTE: "protect" flag is only used for soft reset.
  6480. *
  6481. * RETURN:
  6482. * 0 = success
  6483. * !0 = failure
  6484. *
  6485. * PARAMETERS:
  6486. * *cd - pointer to core data structure
  6487. * protect - flag to call protected or non-protected
  6488. ******************************************************************************/
  6489. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6490. {
  6491. int rc = 0;
  6492. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6493. mutex_lock(&cd->system_lock);
  6494. cd->hid_reset_cmd_state = 1;
  6495. rc = pt_hw_hard_reset(cd);
  6496. mutex_unlock(&cd->system_lock);
  6497. if (rc == -ENODEV) {
  6498. mutex_lock(&cd->system_lock);
  6499. cd->hid_reset_cmd_state = 0;
  6500. mutex_unlock(&cd->system_lock);
  6501. pt_debug(cd->dev, DL_ERROR,
  6502. "%s: Hard reset failed, try soft reset\n", __func__);
  6503. rc = pt_hw_soft_reset(cd, protect);
  6504. }
  6505. if (rc)
  6506. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6507. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6508. return rc;
  6509. }
  6510. /*******************************************************************************
  6511. * FUNCTION: pt_dut_reset_and_wait
  6512. *
  6513. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6514. * complete
  6515. *
  6516. * RETURN:
  6517. * 0 = success
  6518. * !0 = failure
  6519. *
  6520. * PARAMETERS:
  6521. * *cd - pointer to core data structure
  6522. ******************************************************************************/
  6523. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6524. {
  6525. int rc = 0;
  6526. int t;
  6527. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6528. if (rc < 0)
  6529. goto exit;
  6530. t = wait_event_timeout(cd->wait_q,
  6531. (cd->hid_reset_cmd_state == 0),
  6532. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6533. if (IS_TMO(t)) {
  6534. #ifdef TTDL_DIAGNOSTICS
  6535. cd->bus_transmit_error_count++;
  6536. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6537. #endif /* TTDL_DIAGNOSTICS */
  6538. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6539. __func__);
  6540. rc = -ETIME;
  6541. goto exit;
  6542. }
  6543. exit:
  6544. return rc;
  6545. }
  6546. /*
  6547. * touch default parameters (from report descriptor) to resolve protocol for
  6548. * touch report
  6549. */
  6550. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6551. /* byte offset, size, min, max, bit offset, report */
  6552. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6553. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6554. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6555. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6556. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6557. };
  6558. /*
  6559. * button default parameters (from report descriptor) to resolve protocol for
  6560. * button report
  6561. */
  6562. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6563. /* byte offset, size, min, max, bit offset, report */
  6564. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6565. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6566. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6567. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6568. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6569. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6570. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6571. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6572. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6573. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6574. };
  6575. /*******************************************************************************
  6576. * FUNCTION: pt_init_pip_report_fields
  6577. *
  6578. * SUMMARY: Setup default values for touch/button report parsing.
  6579. *
  6580. * PARAMETERS:
  6581. * *cd - pointer to core data structure
  6582. ******************************************************************************/
  6583. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6584. {
  6585. struct pt_sysinfo *si = &cd->sysinfo;
  6586. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6587. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6588. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6589. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6590. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6591. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6592. cd->features.easywake = 1;
  6593. cd->features.noise_metric = 1;
  6594. cd->features.tracking_heatmap = 1;
  6595. cd->features.sensor_data = 1;
  6596. }
  6597. /*******************************************************************************
  6598. * FUNCTION: pt_get_mode
  6599. *
  6600. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6601. * message
  6602. *
  6603. * RETURN: Enum of the current mode
  6604. *
  6605. * PARAMETERS:
  6606. * *cd - pointer to the Core Data structure
  6607. * protect - run command in protected mode
  6608. * *mode - pointer to store the retrieved mode
  6609. ******************************************************************************/
  6610. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6611. {
  6612. if (desc->packet_id == HID_APP_REPORT_ID)
  6613. return PT_MODE_OPERATIONAL;
  6614. else if (desc->packet_id == HID_BL_REPORT_ID)
  6615. return PT_MODE_BOOTLOADER;
  6616. return PT_MODE_UNKNOWN;
  6617. }
  6618. /*******************************************************************************
  6619. * FUNCTION: _pt_request_get_mode
  6620. *
  6621. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6622. * to determine the current mode of the DUT by use of the Get HID
  6623. * Descriptor command.
  6624. *
  6625. * RETURN:
  6626. * 0 = success
  6627. * !0 = failure
  6628. *
  6629. * PARAMETERS:
  6630. * *dev - pointer to device structure
  6631. * protect - run command in protected mode
  6632. * *mode - pointer to store the retrieved mode
  6633. ******************************************************************************/
  6634. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6635. {
  6636. struct pt_core_data *cd = dev_get_drvdata(dev);
  6637. struct pt_hid_desc hid_desc;
  6638. int rc = 0;
  6639. memset(&hid_desc, 0, sizeof(hid_desc));
  6640. if (protect)
  6641. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6642. else
  6643. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6644. if (rc)
  6645. *mode = PT_MODE_UNKNOWN;
  6646. else
  6647. *mode = pt_get_mode(cd, &hid_desc);
  6648. return rc;
  6649. }
  6650. /*******************************************************************************
  6651. * FUNCTION: pt_queue_enum_
  6652. *
  6653. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6654. * function. It won't try to add/delete sysfs node or modules.
  6655. *
  6656. * RETURN: void
  6657. *
  6658. * PARAMETERS:
  6659. * *cd - pointer to core data
  6660. ******************************************************************************/
  6661. static void pt_queue_enum_(struct pt_core_data *cd)
  6662. {
  6663. if (cd->startup_state == STARTUP_NONE) {
  6664. cd->startup_state = STARTUP_QUEUED;
  6665. #ifdef TTDL_DIAGNOSTICS
  6666. if (!cd->bridge_mode)
  6667. schedule_work(&cd->enum_work);
  6668. else
  6669. cd->startup_state = STARTUP_NONE;
  6670. #else
  6671. schedule_work(&cd->enum_work);
  6672. #endif
  6673. pt_debug(cd->dev, DL_INFO,
  6674. "%s: enum_work queued\n", __func__);
  6675. } else {
  6676. pt_debug(cd->dev, DL_WARN,
  6677. "%s: Enum not queued - startup_state = %d\n",
  6678. __func__, cd->startup_state);
  6679. }
  6680. }
  6681. /*******************************************************************************
  6682. * FUNCTION: pt_queue_enum
  6683. *
  6684. * SUMMARY: Queues a TTDL enum within a mutex lock
  6685. *
  6686. * RETURN: void
  6687. *
  6688. * PARAMETERS:
  6689. * *cd - pointer to core data
  6690. ******************************************************************************/
  6691. static void pt_queue_enum(struct pt_core_data *cd)
  6692. {
  6693. mutex_lock(&cd->system_lock);
  6694. pt_queue_enum_(cd);
  6695. mutex_unlock(&cd->system_lock);
  6696. }
  6697. static void remove_sysfs_and_modules(struct device *dev);
  6698. /*******************************************************************************
  6699. * FUNCTION: pt_queue_restart
  6700. *
  6701. * SUMMARY: Queues a TTDL restart within a mutex lock
  6702. *
  6703. * RETURN: void
  6704. *
  6705. * PARAMETERS:
  6706. * *cd - pointer to core data
  6707. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6708. * False: will not perform remove action
  6709. ******************************************************************************/
  6710. static void pt_queue_restart(struct pt_core_data *cd)
  6711. {
  6712. mutex_lock(&cd->system_lock);
  6713. if (cd->startup_state == STARTUP_NONE) {
  6714. cd->startup_state = STARTUP_QUEUED;
  6715. schedule_work(&cd->ttdl_restart_work);
  6716. pt_debug(cd->dev, DL_INFO,
  6717. "%s: pt_ttdl_restart queued\n", __func__);
  6718. } else {
  6719. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6720. __func__, cd->startup_state);
  6721. }
  6722. mutex_unlock(&cd->system_lock);
  6723. }
  6724. /*******************************************************************************
  6725. * FUNCTION: call_atten_cb
  6726. *
  6727. * SUMMARY: Iterate over attention list call the function that registered.
  6728. *
  6729. * RETURN: void
  6730. *
  6731. * PARAMETERS:
  6732. * *cd - pointer to core data
  6733. * type - type of attention list
  6734. * mode - condition for execution
  6735. ******************************************************************************/
  6736. static void call_atten_cb(struct pt_core_data *cd,
  6737. enum pt_atten_type type, int mode)
  6738. {
  6739. struct atten_node *atten, *atten_n;
  6740. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6741. __func__, type, mode);
  6742. spin_lock(&cd->spinlock);
  6743. list_for_each_entry_safe(atten, atten_n,
  6744. &cd->atten_list[type], node) {
  6745. if (!mode || atten->mode & mode) {
  6746. spin_unlock(&cd->spinlock);
  6747. pt_debug(cd->dev, DL_DEBUG,
  6748. "%s: attention for '%s'",
  6749. __func__, dev_name(atten->dev));
  6750. atten->func(atten->dev);
  6751. spin_lock(&cd->spinlock);
  6752. }
  6753. }
  6754. spin_unlock(&cd->spinlock);
  6755. }
  6756. /*******************************************************************************
  6757. * FUNCTION: start_fw_upgrade
  6758. *
  6759. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6760. * start firmware upgrade.
  6761. *
  6762. * RETURN:
  6763. * 0 = success
  6764. *
  6765. * PARAMETERS:
  6766. * *data - pointer to core data
  6767. ******************************************************************************/
  6768. static int start_fw_upgrade(void *data)
  6769. {
  6770. struct pt_core_data *cd = (struct pt_core_data *)data;
  6771. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6772. return 0;
  6773. }
  6774. /*******************************************************************************
  6775. * FUNCTION: pt_put_device_into_easy_wakeup_
  6776. *
  6777. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6778. * wake up state.
  6779. *
  6780. * RETURN:
  6781. * 0 = success
  6782. * !0 = failure
  6783. *
  6784. * PARAMETERS:
  6785. * *cd - pointer to core data
  6786. ******************************************************************************/
  6787. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6788. {
  6789. int rc = 0;
  6790. u8 status = 0;
  6791. rc = pt_hid_output_enter_easywake_state_(cd,
  6792. cd->easy_wakeup_gesture, &status);
  6793. if (rc || status == 0)
  6794. return -EBUSY;
  6795. return rc;
  6796. }
  6797. /*******************************************************************************
  6798. * FUNCTION: pt_put_device_into_deep_sleep_
  6799. *
  6800. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6801. *
  6802. * RETURN:
  6803. * 0 = success
  6804. * !0 = error
  6805. *
  6806. * PARAMETERS:
  6807. * *cd - pointer to core data
  6808. ******************************************************************************/
  6809. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6810. {
  6811. int rc = 0;
  6812. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6813. if (rc)
  6814. rc = -EBUSY;
  6815. return rc;
  6816. }
  6817. /*******************************************************************************
  6818. * FUNCTION: pt_put_device_into_deep_standby_
  6819. *
  6820. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6821. *
  6822. * RETURN:
  6823. * 0 = success
  6824. * !0 = error
  6825. *
  6826. * PARAMETERS:
  6827. * *cd - pointer to core data
  6828. ******************************************************************************/
  6829. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6830. {
  6831. int rc = 0;
  6832. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6833. if (rc)
  6834. rc = -EBUSY;
  6835. return rc;
  6836. }
  6837. /*******************************************************************************
  6838. * FUNCTION: pt_core_poweroff_device_
  6839. *
  6840. * SUMMARY: Disable IRQ and HW power down the device.
  6841. *
  6842. * RETURN:
  6843. * 0 = success
  6844. * !0 = failure
  6845. *
  6846. * PARAMETERS:
  6847. * *cd - pointer to core data
  6848. ******************************************************************************/
  6849. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6850. {
  6851. int rc;
  6852. if (cd->irq_enabled) {
  6853. cd->irq_enabled = false;
  6854. disable_irq_nosync(cd->irq);
  6855. }
  6856. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6857. if (rc < 0)
  6858. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6859. __func__, rc);
  6860. return rc;
  6861. }
  6862. /*******************************************************************************
  6863. * FUNCTION: pt_core_sleep_
  6864. *
  6865. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6866. * configuration in the core platform data structure.
  6867. *
  6868. * RETURN:
  6869. * 0 = success
  6870. * !0 = failure
  6871. *
  6872. * PARAMETERS:
  6873. * *cd - pointer to core data
  6874. ******************************************************************************/
  6875. static int pt_core_sleep_(struct pt_core_data *cd)
  6876. {
  6877. int rc = 0;
  6878. mutex_lock(&cd->system_lock);
  6879. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6880. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6881. mutex_unlock(&cd->system_lock);
  6882. pt_debug(cd->dev, DL_INFO,
  6883. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6884. return 0;
  6885. } else {
  6886. cd->sleep_state = SS_SLEEPING;
  6887. }
  6888. mutex_unlock(&cd->system_lock);
  6889. /* Ensure watchdog and startup works stopped */
  6890. pt_stop_wd_timer(cd);
  6891. cancel_work_sync(&cd->enum_work);
  6892. pt_stop_wd_timer(cd);
  6893. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6894. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6895. rc = pt_core_poweroff_device_(cd);
  6896. if (rc)
  6897. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6898. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6899. pt_debug(cd->dev, DL_INFO,
  6900. "%s: Entering into deep standby mode:\n", __func__);
  6901. rc = pt_put_device_into_deep_standby_(cd);
  6902. if (rc)
  6903. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6904. } else {
  6905. pt_debug(cd->dev, DL_INFO,
  6906. "%s: Entering into deep sleep mode:\n", __func__);
  6907. rc = pt_put_device_into_deep_sleep_(cd);
  6908. if (rc)
  6909. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6910. }
  6911. mutex_lock(&cd->system_lock);
  6912. cd->sleep_state = SS_SLEEP_ON;
  6913. mutex_unlock(&cd->system_lock);
  6914. return rc;
  6915. }
  6916. /*******************************************************************************
  6917. * FUNCTION: pt_core_easywake_on_
  6918. *
  6919. * SUMMARY: Suspend the device with easy wake on the
  6920. * configuration in the core platform data structure.
  6921. *
  6922. * RETURN:
  6923. * 0 = success
  6924. * !0 = failure
  6925. *
  6926. * PARAMETERS:
  6927. * *cd - pointer to core data
  6928. ******************************************************************************/
  6929. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6930. {
  6931. int rc = 0;
  6932. mutex_lock(&cd->system_lock);
  6933. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6934. mutex_unlock(&cd->system_lock);
  6935. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6936. __func__, cd->sleep_state);
  6937. return 0;
  6938. }
  6939. mutex_unlock(&cd->system_lock);
  6940. /* Ensure watchdog and startup works stopped */
  6941. pt_stop_wd_timer(cd);
  6942. cancel_work_sync(&cd->enum_work);
  6943. pt_stop_wd_timer(cd);
  6944. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6945. rc = pt_put_device_into_easy_wakeup_(cd);
  6946. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6947. if (rc)
  6948. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6949. }
  6950. mutex_lock(&cd->system_lock);
  6951. cd->sleep_state = SS_EASY_WAKING_ON;
  6952. mutex_unlock(&cd->system_lock);
  6953. return rc;
  6954. }
  6955. /*******************************************************************************
  6956. * FUNCTION: pt_core_easywake_on
  6957. *
  6958. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6959. *
  6960. * RETURN:
  6961. * 0 = success
  6962. * !0 = failure
  6963. *
  6964. * PARAMETERS:
  6965. * *cd - pointer to core data
  6966. ******************************************************************************/
  6967. static int pt_core_easywake_on(struct pt_core_data *cd)
  6968. {
  6969. int rc = 0;
  6970. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6971. if (rc < 0) {
  6972. pt_debug(cd->dev, DL_ERROR,
  6973. "%s: fail get exclusive ex=%p own=%p\n",
  6974. __func__, cd->exclusive_dev, cd->dev);
  6975. return rc;
  6976. }
  6977. rc = pt_core_easywake_on_(cd);
  6978. if (release_exclusive(cd, cd->dev) < 0)
  6979. pt_debug(cd->dev, DL_ERROR,
  6980. "%s: fail to release exclusive\n", __func__);
  6981. else
  6982. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6983. __func__);
  6984. return rc;
  6985. }
  6986. /*******************************************************************************
  6987. * FUNCTION: pt_core_sleep
  6988. *
  6989. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6990. *
  6991. * RETURN:
  6992. * 0 = success
  6993. * !0 = failure
  6994. *
  6995. * PARAMETERS:
  6996. * *cd - pointer to core data
  6997. ******************************************************************************/
  6998. static int pt_core_sleep(struct pt_core_data *cd)
  6999. {
  7000. int rc = 0;
  7001. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7002. if (rc < 0) {
  7003. pt_debug(cd->dev, DL_ERROR,
  7004. "%s: fail get exclusive ex=%p own=%p\n",
  7005. __func__, cd->exclusive_dev, cd->dev);
  7006. return rc;
  7007. }
  7008. rc = pt_core_sleep_(cd);
  7009. if (release_exclusive(cd, cd->dev) < 0)
  7010. pt_debug(cd->dev, DL_ERROR,
  7011. "%s: fail to release exclusive\n", __func__);
  7012. else
  7013. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7014. __func__);
  7015. return rc;
  7016. }
  7017. /*******************************************************************************
  7018. * FUNCTION: pt_wakeup_host
  7019. *
  7020. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7021. *
  7022. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7023. *
  7024. * RETURN:
  7025. * 0 = success
  7026. * !0 = failure
  7027. *
  7028. * PARAMETERS:
  7029. * *cd - pointer to core data
  7030. ******************************************************************************/
  7031. static int pt_wakeup_host(struct pt_core_data *cd)
  7032. {
  7033. #ifndef EASYWAKE_TSG6
  7034. /* TSG5 EasyWake */
  7035. int rc = 0;
  7036. int event_id;
  7037. int size = get_unaligned_le16(&cd->input_buf[0]);
  7038. /* Validate report */
  7039. if (size != 4 || cd->input_buf[2] != 4)
  7040. rc = -EINVAL;
  7041. event_id = cd->input_buf[3];
  7042. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7043. __func__, event_id, rc);
  7044. if (rc) {
  7045. pt_core_sleep_(cd);
  7046. goto exit;
  7047. }
  7048. /* attention WAKE */
  7049. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7050. exit:
  7051. return rc;
  7052. #else
  7053. /* TSG6 FW1.3 EasyWake */
  7054. int rc = 0;
  7055. int i = 0;
  7056. int report_length;
  7057. /* Validate report */
  7058. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7059. rc = -EINVAL;
  7060. pt_core_sleep_(cd);
  7061. goto exit;
  7062. }
  7063. /* Get gesture id and gesture data length */
  7064. cd->gesture_id = cd->input_buf[3];
  7065. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7066. cd->gesture_data_length = report_length - 4;
  7067. pt_debug(cd->dev, DL_INFO,
  7068. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7069. __func__, cd->gesture_id, cd->gesture_data_length);
  7070. for (i = 0; i < cd->gesture_data_length; i++)
  7071. cd->gesture_data[i] = cd->input_buf[4 + i];
  7072. /* attention WAKE */
  7073. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7074. exit:
  7075. return rc;
  7076. #endif
  7077. }
  7078. /*******************************************************************************
  7079. * FUNCTION: pt_get_touch_axis
  7080. *
  7081. * SUMMARY: Function to calculate touch axis
  7082. *
  7083. * PARAMETERS:
  7084. * *cd - pointer to core data structure
  7085. * *axis - pointer to axis calculation result
  7086. * size - size in bytes
  7087. * max - max value of result
  7088. * *xy_data - pointer to input data to be parsed
  7089. * bofs - bit offset
  7090. ******************************************************************************/
  7091. static void pt_get_touch_axis(struct pt_core_data *cd,
  7092. int *axis, int size, int max, u8 *data, int bofs)
  7093. {
  7094. int nbyte;
  7095. int next;
  7096. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7097. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7098. next++;
  7099. }
  7100. *axis &= max - 1;
  7101. }
  7102. /*******************************************************************************
  7103. * FUNCTION: move_tracking_heatmap_data
  7104. *
  7105. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7106. * system information structure, xy_mode and xy_data.
  7107. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7108. * the tthe_tuner sysfs node under the label "THM"
  7109. *
  7110. * RETURN:
  7111. * 0 = success
  7112. * !0 = failure
  7113. *
  7114. * PARAMETERS:
  7115. * *cd - pointer to core data
  7116. * *si - pointer to the system information structure
  7117. ******************************************************************************/
  7118. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7119. struct pt_sysinfo *si)
  7120. {
  7121. #ifdef TTHE_TUNER_SUPPORT
  7122. int size = get_unaligned_le16(&cd->input_buf[0]);
  7123. if (size)
  7124. tthe_print(cd, cd->input_buf, size, "THM=");
  7125. #endif
  7126. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7127. return 0;
  7128. }
  7129. /*******************************************************************************
  7130. * FUNCTION: move_sensor_data
  7131. *
  7132. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7133. * information structure, xy_mode and xy_data.
  7134. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7135. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7136. *
  7137. * RETURN:
  7138. * 0 = success
  7139. * !0 = failure
  7140. *
  7141. * PARAMETERS:
  7142. * *cd - pointer to core data
  7143. * *si - pointer to the system information structure
  7144. ******************************************************************************/
  7145. static int move_sensor_data(struct pt_core_data *cd,
  7146. struct pt_sysinfo *si)
  7147. {
  7148. #ifdef TTHE_TUNER_SUPPORT
  7149. int size = get_unaligned_le16(&cd->input_buf[0]);
  7150. if (size)
  7151. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7152. #endif
  7153. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7154. return 0;
  7155. }
  7156. /*******************************************************************************
  7157. * FUNCTION: move_button_data
  7158. *
  7159. * SUMMARY: Move the valid button data from the input buffer into the system
  7160. * information structure, xy_mode and xy_data.
  7161. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7162. * the tthe_tuner sysfs node under the label "OpModeData"
  7163. *
  7164. * RETURN:
  7165. * 0 = success
  7166. * !0 = failure
  7167. *
  7168. * PARAMETERS:
  7169. * *cd - pointer to core data
  7170. * *si - pointer to the system information structure
  7171. ******************************************************************************/
  7172. static int move_button_data(struct pt_core_data *cd,
  7173. struct pt_sysinfo *si)
  7174. {
  7175. #ifdef TTHE_TUNER_SUPPORT
  7176. int size = get_unaligned_le16(&cd->input_buf[0]);
  7177. if (size)
  7178. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7179. #endif
  7180. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7181. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7182. "xy_mode");
  7183. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7184. BTN_REPORT_SIZE);
  7185. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7186. "xy_data");
  7187. return 0;
  7188. }
  7189. /*******************************************************************************
  7190. * FUNCTION: move_touch_data
  7191. *
  7192. * SUMMARY: Move the valid touch data from the input buffer into the system
  7193. * information structure, xy_mode and xy_data.
  7194. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7195. * the tthe_tuner sysfs node under the label "OpModeData"
  7196. *
  7197. * RETURN:
  7198. * 0 = success
  7199. * !0 = failure
  7200. *
  7201. * PARAMETERS:
  7202. * *cd - pointer to core data
  7203. * *si - pointer to the system information structure
  7204. ******************************************************************************/
  7205. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7206. {
  7207. int max_tch = si->sensing_conf_data.max_tch;
  7208. int num_cur_tch;
  7209. int length;
  7210. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7211. #ifdef TTHE_TUNER_SUPPORT
  7212. int size = get_unaligned_le16(&cd->input_buf[0]);
  7213. if (size)
  7214. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7215. #endif
  7216. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7217. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7218. si->desc.tch_header_size, "xy_mode");
  7219. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7220. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7221. if (unlikely(num_cur_tch > max_tch))
  7222. num_cur_tch = max_tch;
  7223. length = num_cur_tch * si->desc.tch_record_size;
  7224. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7225. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7226. return 0;
  7227. }
  7228. /*******************************************************************************
  7229. * FUNCTION: move_hid_pen_data
  7230. *
  7231. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7232. * information structure, xy_mode and xy_data.
  7233. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7234. * the tthe_tuner sysfs node under the label "HID" starting with the
  7235. * report ID.
  7236. *
  7237. * RETURN:
  7238. * 0 = success
  7239. * !0 = failure
  7240. *
  7241. * PARAMETERS:
  7242. * *cd - pointer to core data
  7243. * *si - pointer to the system information structure
  7244. ******************************************************************************/
  7245. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7246. {
  7247. #ifdef TTHE_TUNER_SUPPORT
  7248. int size = get_unaligned_le16(&cd->input_buf[0]);
  7249. if (size) {
  7250. /*
  7251. * HID over USB does not require the two byte length field, so
  7252. * this should print from input_buf[2] but to keep both finger
  7253. * and pen reports the same the length is included
  7254. */
  7255. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7256. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7257. "HID-USB=");
  7258. else
  7259. tthe_print(cd, &(cd->input_buf[0]), size,
  7260. "HID-I2C=");
  7261. }
  7262. #endif
  7263. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7264. return 0;
  7265. }
  7266. /*******************************************************************************
  7267. * FUNCTION: parse_touch_input
  7268. *
  7269. * SUMMARY: Parse the touch report and take action based on the touch
  7270. * report_id.
  7271. *
  7272. * RETURN:
  7273. * 0 = success
  7274. * !0 = failure
  7275. *
  7276. * PARAMETERS:
  7277. * *cd - pointer to core data
  7278. * size - size of touch record
  7279. ******************************************************************************/
  7280. static int parse_touch_input(struct pt_core_data *cd, int size)
  7281. {
  7282. struct pt_sysinfo *si = &cd->sysinfo;
  7283. int report_id = cd->input_buf[2];
  7284. int rc = -EINVAL;
  7285. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7286. __func__);
  7287. if (!si->ready) {
  7288. pt_debug(cd->dev, DL_ERROR,
  7289. "%s: Need system information to parse touches\n",
  7290. __func__);
  7291. return 0;
  7292. }
  7293. if (!si->xy_mode || !si->xy_data)
  7294. return rc;
  7295. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7296. rc = move_touch_data(cd, si);
  7297. else if (report_id == PT_HID_PEN_REPORT_ID)
  7298. rc = move_hid_pen_data(cd, si);
  7299. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7300. rc = move_button_data(cd, si);
  7301. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7302. rc = move_sensor_data(cd, si);
  7303. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7304. rc = move_tracking_heatmap_data(cd, si);
  7305. if (rc)
  7306. return rc;
  7307. /* attention IRQ */
  7308. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7309. return 0;
  7310. }
  7311. /*******************************************************************************
  7312. * FUNCTION: parse_command_input
  7313. *
  7314. * SUMMARY: Move the response data from the input buffer to the response buffer
  7315. *
  7316. * RETURN:
  7317. * 0 = success
  7318. * !0 = failure
  7319. *
  7320. * PARAMETERS:
  7321. * *cd - pointer to core data
  7322. * size - size of response data
  7323. ******************************************************************************/
  7324. static int parse_command_input(struct pt_core_data *cd, int size)
  7325. {
  7326. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7327. __func__);
  7328. memcpy(cd->response_buf, cd->input_buf, size);
  7329. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7330. if (size && cd->show_tt_data) {
  7331. if (cd->pip2_prot_active)
  7332. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7333. else
  7334. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7335. }
  7336. #endif
  7337. mutex_lock(&cd->system_lock);
  7338. cd->hid_cmd_state = 0;
  7339. mutex_unlock(&cd->system_lock);
  7340. wake_up(&cd->wait_q);
  7341. return 0;
  7342. }
  7343. /*******************************************************************************
  7344. * FUNCTION: pt_allow_enumeration
  7345. *
  7346. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7347. * FW sentinel is seen.
  7348. *
  7349. * RETURN:
  7350. * true = allow enumeration or fully re-probe
  7351. * false = skip enumeration and fully re-probe
  7352. *
  7353. * PARAMETERS:
  7354. * *cd - pointer to core data
  7355. ******************************************************************************/
  7356. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7357. {
  7358. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7359. (!cd->hid_reset_cmd_state) &&
  7360. (cd->core_probe_complete) &&
  7361. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7362. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7363. (cd->mode == PT_MODE_OPERATIONAL)) {
  7364. return true;
  7365. }
  7366. if ((!cd->hid_reset_cmd_state) &&
  7367. (cd->core_probe_complete) &&
  7368. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7369. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7370. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7371. return true;
  7372. }
  7373. pt_debug(cd->dev, DL_INFO,
  7374. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7375. __func__,
  7376. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7377. "core_probe_complete(1)", cd->core_probe_complete,
  7378. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7379. "active_dut_gen(0,2)", cd->active_dut_generation);
  7380. return false;
  7381. }
  7382. /*******************************************************************************
  7383. * FUNCTION: pt_is_touch_report
  7384. *
  7385. * SUMMARY: Determine if a report ID should be treated as a touch report
  7386. *
  7387. * RETURN:
  7388. * true = report ID is a touch report
  7389. * false = report ID is not a touch report
  7390. *
  7391. * PARAMETERS:
  7392. * id - Report ID
  7393. ******************************************************************************/
  7394. static bool pt_is_touch_report(int id)
  7395. {
  7396. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7397. id == PT_HID_PEN_REPORT_ID ||
  7398. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7399. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7400. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7401. return true;
  7402. else
  7403. return false;
  7404. }
  7405. /*******************************************************************************
  7406. * FUNCTION: pt_parse_input
  7407. *
  7408. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7409. * on if its a response to a command or asynchronous touch data / reset
  7410. * sentinel. PIP2.x messages have additional error checking that is
  7411. * parsed (SEQ match from cmd to rsp, CRC valid).
  7412. * Look for special packets based on unique lengths:
  7413. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7414. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7415. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7416. * 0xFFXX - Empty buffer (PIP 1.7+)
  7417. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7418. * the initial probe has not yet been done.
  7419. *
  7420. * RETURN:
  7421. * 0 = success
  7422. * !0 = failure
  7423. *
  7424. * PARAMETERS:
  7425. * *cd - pointer to core data
  7426. ******************************************************************************/
  7427. static int pt_parse_input(struct pt_core_data *cd)
  7428. {
  7429. int report_id;
  7430. int cmd_id;
  7431. int is_command = 0;
  7432. int size;
  7433. int print_size;
  7434. bool touch_report = true;
  7435. unsigned short calc_crc;
  7436. unsigned short resp_crc;
  7437. cd->fw_sys_mode_in_standby_state = false;
  7438. size = get_unaligned_le16(&cd->input_buf[0]);
  7439. print_size = size;
  7440. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7441. __func__, print_size);
  7442. if (print_size <= PT_MAX_INPUT)
  7443. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7444. "<<< Read buf");
  7445. if (size == 0 ||
  7446. (size == 11 &&
  7447. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7448. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7449. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7450. PIP2_CMD_COMMAND_ID_MASK) ==
  7451. PIP2_CMD_ID_STATUS)) {
  7452. touch_report = false;
  7453. cd->hw_detected = true;
  7454. cd->bl_pip_ver_ready = false;
  7455. cd->app_pip_ver_ready = false;
  7456. if (size == 0) {
  7457. mutex_lock(&cd->system_lock);
  7458. cd->pip2_prot_active = false;
  7459. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7460. /*
  7461. * For Gen5/6 this sentinel could be from
  7462. * the BL or FW. Attempt to set the correct
  7463. * mode based on the previous PIP command.
  7464. */
  7465. if (cd->hid_cmd_state ==
  7466. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7467. cd->mode = PT_MODE_OPERATIONAL;
  7468. cd->startup_status =
  7469. STARTUP_STATUS_FW_RESET_SENTINEL;
  7470. } else if (cd->hid_cmd_state ==
  7471. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7472. cd->hid_reset_cmd_state) {
  7473. cd->mode = PT_MODE_BOOTLOADER;
  7474. cd->startup_status =
  7475. STARTUP_STATUS_BL_RESET_SENTINEL;
  7476. } else {
  7477. cd->mode = PT_MODE_UNKNOWN;
  7478. cd->startup_status =
  7479. STARTUP_STATUS_START;
  7480. }
  7481. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7482. pt_debug(cd->dev, DL_INFO,
  7483. "%s: ATM Gen5/6 %s sentinel received\n",
  7484. __func__,
  7485. (cd->mode == PT_MODE_OPERATIONAL ?
  7486. "FW" :
  7487. (cd->mode == PT_MODE_BOOTLOADER ?
  7488. "BL" : "Unknown")));
  7489. } else {
  7490. cd->mode = PT_MODE_OPERATIONAL;
  7491. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7492. cd->startup_status =
  7493. STARTUP_STATUS_FW_RESET_SENTINEL;
  7494. pt_debug(cd->dev, DL_INFO,
  7495. "%s: ATM PT/TT FW sentinel received\n",
  7496. __func__);
  7497. }
  7498. mutex_unlock(&cd->system_lock);
  7499. if (pt_allow_enumeration(cd)) {
  7500. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7501. pt_debug(cd->dev, DL_INFO,
  7502. "%s: Queue Restart\n", __func__);
  7503. pt_queue_restart(cd);
  7504. } else {
  7505. pt_debug(cd->dev, DL_INFO,
  7506. "%s: Queue Enum\n", __func__);
  7507. pt_queue_enum(cd);
  7508. }
  7509. } else {
  7510. pt_debug(cd->dev, DL_INFO,
  7511. "%s: Sentinel - No Queued Action\n",
  7512. __func__);
  7513. }
  7514. } else {
  7515. /* Sentinel must be from TT/TC BL */
  7516. mutex_lock(&cd->system_lock);
  7517. cd->pip2_prot_active = true;
  7518. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7519. cd->mode = PT_MODE_BOOTLOADER;
  7520. cd->sysinfo.ready = false;
  7521. mutex_unlock(&cd->system_lock);
  7522. pt_debug(cd->dev, DL_INFO,
  7523. "%s: BL Reset sentinel received\n", __func__);
  7524. if (cd->flashless_dut &&
  7525. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7526. pt_debug(cd->dev, DL_INFO,
  7527. "%s: BL to RAM for flashless DUT\n",
  7528. __func__);
  7529. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7530. }
  7531. }
  7532. mutex_lock(&cd->system_lock);
  7533. memcpy(cd->response_buf, cd->input_buf, 2);
  7534. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7535. mutex_unlock(&cd->system_lock);
  7536. pt_debug(cd->dev, DL_WARN,
  7537. "%s: Device Initiated Reset\n", __func__);
  7538. wake_up(&cd->wait_q);
  7539. return 0;
  7540. }
  7541. cd->hid_reset_cmd_state = 0;
  7542. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7543. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7544. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7545. cd->hid_cmd_state = 0;
  7546. wake_up(&cd->wait_q);
  7547. mutex_unlock(&cd->system_lock);
  7548. return 0;
  7549. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7550. /*
  7551. * This debug message below is used by PBATS to calculate the
  7552. * time from the last lift off IRQ to when FW exits LFT mode.
  7553. */
  7554. touch_report = false;
  7555. pt_debug(cd->dev, DL_WARN,
  7556. "%s: DUT - Empty buffer detected\n", __func__);
  7557. return 0;
  7558. } else if (size > PT_MAX_INPUT || size < 0) {
  7559. pt_debug(cd->dev, DL_ERROR,
  7560. "%s: DUT - Unexpected len field in active bus data!\n",
  7561. __func__);
  7562. return -EINVAL;
  7563. }
  7564. if (cd->pip2_prot_active) {
  7565. pt_debug(cd->dev, DL_DEBUG,
  7566. "%s: Decode PIP2.x Response\n", __func__);
  7567. /* PIP2 does not have a report id, hard code it */
  7568. report_id = 0x00;
  7569. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7570. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7571. resp_crc = cd->input_buf[size - 2] << 8;
  7572. resp_crc |= cd->input_buf[size - 1];
  7573. if ((cd->pip2_cmd_tag_seq !=
  7574. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7575. (resp_crc != calc_crc) &&
  7576. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7577. == PT_PIP_TOUCH_REPORT_ID) ||
  7578. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7579. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7580. pt_debug(cd->dev, DL_WARN,
  7581. "%s: %s %d %s\n",
  7582. __func__,
  7583. "Received PIP1 report id =",
  7584. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7585. "when expecting a PIP2 report - IGNORE report");
  7586. return 0;
  7587. }
  7588. is_command = 1;
  7589. touch_report = false;
  7590. } else {
  7591. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7592. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7593. }
  7594. #ifdef TTDL_DIAGNOSTICS
  7595. pt_debug(cd->dev, DL_DEBUG,
  7596. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7597. __func__, cd->pip2_prot_active, report_id,
  7598. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7599. #endif /* TTDL_DIAGNOSTICS */
  7600. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7601. pt_wakeup_host(cd);
  7602. #ifdef TTHE_TUNER_SUPPORT
  7603. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7604. #endif
  7605. return 0;
  7606. }
  7607. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7608. msecs_to_jiffies(cd->watchdog_interval));
  7609. /*
  7610. * If it is PIP2 response, the report_id has been set to 0,
  7611. * so it will not be parsed as a touch packet.
  7612. */
  7613. if (!pt_is_touch_report(report_id)) {
  7614. is_command = 1;
  7615. touch_report = false;
  7616. }
  7617. if (unlikely(is_command)) {
  7618. parse_command_input(cd, size);
  7619. return 0;
  7620. }
  7621. if (touch_report)
  7622. parse_touch_input(cd, size);
  7623. return 0;
  7624. }
  7625. /*******************************************************************************
  7626. * FUNCTION: pt_read_input
  7627. *
  7628. * SUMMARY: Reads incoming data off of the active bus
  7629. *
  7630. * RETURN:
  7631. * 0 = success
  7632. * !0 = failure
  7633. *
  7634. * PARAMETERS:
  7635. * *cd - pointer to core data
  7636. ******************************************************************************/
  7637. static int pt_read_input(struct pt_core_data *cd)
  7638. {
  7639. struct device *dev = cd->dev;
  7640. int rc = 0;
  7641. int t;
  7642. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7643. /*
  7644. * Workaround for easywake failure
  7645. * Interrupt for easywake, wait for bus controller to wake
  7646. */
  7647. mutex_lock(&cd->system_lock);
  7648. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7649. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7650. mutex_unlock(&cd->system_lock);
  7651. if (!dev->power.is_suspended)
  7652. goto read;
  7653. t = wait_event_timeout(cd->wait_q,
  7654. (cd->wait_until_wake == 1),
  7655. msecs_to_jiffies(2000));
  7656. #ifdef TTDL_DIAGNOSTICS
  7657. if (IS_TMO(t)) {
  7658. cd->bus_transmit_error_count++;
  7659. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7660. pt_debug(dev, DL_ERROR,
  7661. "%s: !!!I2C Transmission Error %d\n",
  7662. __func__,
  7663. cd->bus_transmit_error_count);
  7664. }
  7665. #else
  7666. if (IS_TMO(t))
  7667. pt_queue_enum(cd);
  7668. #endif /* TTDL_DIAGNOSTICS */
  7669. goto read;
  7670. }
  7671. }
  7672. mutex_unlock(&cd->system_lock);
  7673. read:
  7674. /* Try reading up to 'retry' times */
  7675. while (retry-- != 0) {
  7676. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7677. PT_MAX_INPUT);
  7678. if (!rc) {
  7679. pt_debug(dev, DL_DEBUG,
  7680. "%s: Read input successfully\n", __func__);
  7681. goto read_exit;
  7682. }
  7683. usleep_range(1000, 2000);
  7684. }
  7685. pt_debug(dev, DL_ERROR,
  7686. "%s: Error getting report, rc=%d\n", __func__, rc);
  7687. read_exit:
  7688. return rc;
  7689. }
  7690. /*******************************************************************************
  7691. * FUNCTION: pt_irq
  7692. *
  7693. * SUMMARY: Process all detected interrupts
  7694. *
  7695. * RETURN:
  7696. * IRQ_HANDLED - Finished processing the interrupt
  7697. *
  7698. * PARAMETERS:
  7699. * irq - IRQ number
  7700. * *handle - pointer to core data struct
  7701. ******************************************************************************/
  7702. irqreturn_t pt_irq(int irq, void *handle)
  7703. {
  7704. struct pt_core_data *cd = handle;
  7705. int rc = 0;
  7706. if (!pt_check_irq_asserted(cd))
  7707. return IRQ_HANDLED;
  7708. rc = pt_read_input(cd);
  7709. #ifdef TTDL_DIAGNOSTICS
  7710. cd->irq_count++;
  7711. /* Used to calculate T-Refresh */
  7712. if (cd->t_refresh_active) {
  7713. if (cd->t_refresh_count == 0) {
  7714. cd->t_refresh_time = jiffies;
  7715. cd->t_refresh_count++;
  7716. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7717. cd->t_refresh_count++;
  7718. } else {
  7719. cd->t_refresh_active = 0;
  7720. cd->t_refresh_time =
  7721. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7722. }
  7723. }
  7724. #endif /* TTDL_DIAGNOSTICS */
  7725. if (!rc)
  7726. pt_parse_input(cd);
  7727. return IRQ_HANDLED;
  7728. }
  7729. /*******************************************************************************
  7730. * FUNCTION: _pt_subscribe_attention
  7731. *
  7732. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7733. * to subscribe themselves into the TTDL attention list
  7734. *
  7735. * RETURN:
  7736. * 0 = success
  7737. * !0 = failure
  7738. *
  7739. * PARAMETERS:
  7740. * *dev - pointer to device structure
  7741. * type - attention type enum
  7742. * *id - ID of the module calling this function
  7743. * *func - callback function pointer to be called when notified
  7744. * mode - attention mode
  7745. ******************************************************************************/
  7746. int _pt_subscribe_attention(struct device *dev,
  7747. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7748. int mode)
  7749. {
  7750. struct pt_core_data *cd = dev_get_drvdata(dev);
  7751. struct atten_node *atten, *atten_new;
  7752. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7753. if (!atten_new)
  7754. return -ENOMEM;
  7755. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7756. dev_name(cd->dev));
  7757. spin_lock(&cd->spinlock);
  7758. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7759. if (atten->id == id && atten->mode == mode) {
  7760. spin_unlock(&cd->spinlock);
  7761. kfree(atten_new);
  7762. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7763. __func__,
  7764. "already subscribed attention",
  7765. dev, "mode", mode);
  7766. return 0;
  7767. }
  7768. }
  7769. atten_new->id = id;
  7770. atten_new->dev = dev;
  7771. atten_new->mode = mode;
  7772. atten_new->func = func;
  7773. list_add(&atten_new->node, &cd->atten_list[type]);
  7774. spin_unlock(&cd->spinlock);
  7775. return 0;
  7776. }
  7777. /*******************************************************************************
  7778. * FUNCTION: _pt_unsubscribe_attention
  7779. *
  7780. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7781. * to unsubscribe themselves from the TTDL attention list
  7782. *
  7783. * RETURN:
  7784. * 0 = success
  7785. * !0 = failure
  7786. *
  7787. * PARAMETERS:
  7788. * *dev - pointer to device structure
  7789. * type - attention type enum
  7790. * *id - ID of the module calling this function
  7791. * *func - function pointer
  7792. * mode - attention mode
  7793. ******************************************************************************/
  7794. int _pt_unsubscribe_attention(struct device *dev,
  7795. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7796. int mode)
  7797. {
  7798. struct pt_core_data *cd = dev_get_drvdata(dev);
  7799. struct atten_node *atten, *atten_n;
  7800. spin_lock(&cd->spinlock);
  7801. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7802. if (atten->id == id && atten->mode == mode) {
  7803. list_del(&atten->node);
  7804. spin_unlock(&cd->spinlock);
  7805. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7806. __func__,
  7807. "unsub for atten->dev", atten->dev,
  7808. "atten->mode", atten->mode);
  7809. kfree(atten);
  7810. return 0;
  7811. }
  7812. }
  7813. spin_unlock(&cd->spinlock);
  7814. return -ENODEV;
  7815. }
  7816. /*******************************************************************************
  7817. * FUNCTION: _pt_request_exclusive
  7818. *
  7819. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7820. * to request exclusive access
  7821. *
  7822. * RETURN:
  7823. * 0 = success
  7824. * !0 = failure
  7825. *
  7826. * PARAMETERS:
  7827. * *dev - pointer to device structure
  7828. * timeout_ms - timeout to wait for exclusive access
  7829. ******************************************************************************/
  7830. static int _pt_request_exclusive(struct device *dev,
  7831. int timeout_ms)
  7832. {
  7833. struct pt_core_data *cd = dev_get_drvdata(dev);
  7834. return request_exclusive(cd, (void *)dev, timeout_ms);
  7835. }
  7836. /*******************************************************************************
  7837. * FUNCTION: _pt_release_exclusive
  7838. *
  7839. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7840. * to release exclusive access
  7841. *
  7842. * RETURN:
  7843. * 0 = success
  7844. * !0 = failure
  7845. *
  7846. * PARAMETERS:
  7847. * *dev - pointer to device structure
  7848. ******************************************************************************/
  7849. static int _pt_release_exclusive(struct device *dev)
  7850. {
  7851. struct pt_core_data *cd = dev_get_drvdata(dev);
  7852. return release_exclusive(cd, (void *)dev);
  7853. }
  7854. /*******************************************************************************
  7855. * FUNCTION: _pt_request_reset
  7856. *
  7857. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7858. * to request the DUT to be reset. Function returns err if refused or
  7859. * timeout occurs (Note: core uses fixed timeout period).
  7860. *
  7861. * NOTE: Function blocks until ISR occurs.
  7862. *
  7863. * RETURN:
  7864. * 0 = success
  7865. * !0 = failure
  7866. *
  7867. * PARAMETERS:
  7868. * *dev - pointer to device structure
  7869. * protect - flag to call protected or non-protected
  7870. ******************************************************************************/
  7871. static int _pt_request_reset(struct device *dev, int protect)
  7872. {
  7873. struct pt_core_data *cd = dev_get_drvdata(dev);
  7874. int rc;
  7875. rc = pt_dut_reset(cd, protect);
  7876. if (rc < 0) {
  7877. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7878. __func__, rc);
  7879. }
  7880. return rc;
  7881. }
  7882. /*******************************************************************************
  7883. * FUNCTION: _pt_request_enum
  7884. *
  7885. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7886. * to request TTDL to queue an enum. This function will return err
  7887. * if refused; if no error then enum has completed and system is in
  7888. * normal operation mode.
  7889. *
  7890. * RETURN:
  7891. * 0 = success
  7892. * !0 = failure
  7893. *
  7894. * PARAMETERS:
  7895. * *dev - pointer to device structure
  7896. * wait - boolean to determine if to wait for startup event
  7897. ******************************************************************************/
  7898. static int _pt_request_enum(struct device *dev, bool wait)
  7899. {
  7900. struct pt_core_data *cd = dev_get_drvdata(dev);
  7901. pt_queue_enum(cd);
  7902. if (wait)
  7903. wait_event_timeout(cd->wait_q,
  7904. cd->startup_state == STARTUP_NONE,
  7905. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7906. return 0;
  7907. }
  7908. /*******************************************************************************
  7909. * FUNCTION: _pt_request_sysinfo
  7910. *
  7911. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7912. * to request the pointer to the system information structure. This
  7913. * function will return NULL if sysinfo has not been acquired from the
  7914. * DUT yet.
  7915. *
  7916. * RETURN: Pointer to the system information struct
  7917. *
  7918. * PARAMETERS:
  7919. * *dev - pointer to device structure
  7920. ******************************************************************************/
  7921. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7922. {
  7923. struct pt_core_data *cd = dev_get_drvdata(dev);
  7924. int rc = 0;
  7925. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7926. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7927. !cd->sysinfo.ready) {
  7928. rc = pt_hid_output_get_sysinfo_(cd);
  7929. if (rc) {
  7930. pt_debug(cd->dev, DL_ERROR,
  7931. "%s: Error getting sysinfo rc=%d\n",
  7932. __func__, rc);
  7933. }
  7934. }
  7935. if (cd->sysinfo.ready)
  7936. return &cd->sysinfo;
  7937. return NULL;
  7938. }
  7939. /*******************************************************************************
  7940. * FUNCTION: _pt_request_loader_pdata
  7941. *
  7942. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7943. * to request the pointer to the loader platform data
  7944. *
  7945. * RETURN: Pointer to the loader platform data struct
  7946. *
  7947. * PARAMETERS:
  7948. * *dev - pointer to device structure
  7949. ******************************************************************************/
  7950. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7951. struct device *dev)
  7952. {
  7953. struct pt_core_data *cd = dev_get_drvdata(dev);
  7954. return cd->pdata->loader_pdata;
  7955. }
  7956. /*******************************************************************************
  7957. * FUNCTION: _pt_request_start_wd
  7958. *
  7959. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7960. * to request to start the TTDL watchdog
  7961. *
  7962. * RETURN:
  7963. * 0 = success
  7964. * !0 = failure
  7965. *
  7966. * PARAMETERS:
  7967. * *dev - pointer to device structure
  7968. ******************************************************************************/
  7969. static int _pt_request_start_wd(struct device *dev)
  7970. {
  7971. struct pt_core_data *cd = dev_get_drvdata(dev);
  7972. pt_start_wd_timer(cd);
  7973. return 0;
  7974. }
  7975. /*******************************************************************************
  7976. * FUNCTION: _pt_request_stop_wd
  7977. *
  7978. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7979. * to request to stop the TTDL watchdog
  7980. *
  7981. * RETURN:
  7982. * 0 = success
  7983. * !0 = failure
  7984. *
  7985. * PARAMETERS:
  7986. * *dev - pointer to device structure
  7987. ******************************************************************************/
  7988. static int _pt_request_stop_wd(struct device *dev)
  7989. {
  7990. struct pt_core_data *cd = dev_get_drvdata(dev);
  7991. pt_stop_wd_timer(cd);
  7992. return 0;
  7993. }
  7994. /*******************************************************************************
  7995. * FUNCTION: pt_pip2_launch_app
  7996. *
  7997. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7998. * the FW reset sentinel to indicate the function succeeded.
  7999. *
  8000. * NOTE: Calling this function when the DUT is in Application mode WILL result
  8001. * in a timeout delay and with the DUT being reset with an XRES.
  8002. *
  8003. * RETURN:
  8004. * 0 = success
  8005. * !0 = failure
  8006. *
  8007. * PARAMETERS:
  8008. * *cd - pointer to core data structure
  8009. * protect - flag to call protected or non-protected
  8010. ******************************************************************************/
  8011. static int pt_pip2_launch_app(struct device *dev, int protect)
  8012. {
  8013. struct pt_core_data *cd = dev_get_drvdata(dev);
  8014. u16 actual_read_len;
  8015. u16 tmp_startup_status = cd->startup_status;
  8016. u8 read_buf[12];
  8017. u8 status;
  8018. int time = 0;
  8019. int rc = 0;
  8020. mutex_lock(&cd->system_lock);
  8021. cd->startup_status = STARTUP_STATUS_START;
  8022. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8023. mutex_unlock(&cd->system_lock);
  8024. rc = _pt_request_pip2_send_cmd(dev, protect,
  8025. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8026. &actual_read_len);
  8027. if (rc) {
  8028. pt_debug(dev, DL_ERROR,
  8029. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8030. __func__, rc);
  8031. } else {
  8032. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8033. /* Test for no or invalid image in FLASH, no point to reset */
  8034. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8035. rc = status;
  8036. goto exit;
  8037. }
  8038. /* Any other boot failure */
  8039. if (status != 0) {
  8040. pt_debug(dev, DL_ERROR,
  8041. "%s: FW did not EXECUTE, status = %d\n",
  8042. __func__, status);
  8043. rc = status;
  8044. }
  8045. }
  8046. if (rc) {
  8047. pt_debug(dev, DL_ERROR,
  8048. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8049. __func__, rc);
  8050. goto exit;
  8051. }
  8052. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8053. msleep(20);
  8054. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8055. __func__, time, cd->startup_status);
  8056. time += 20;
  8057. }
  8058. if (cd->startup_status == STARTUP_STATUS_START) {
  8059. pt_debug(cd->dev, DL_WARN,
  8060. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8061. rc = -ETIME;
  8062. }
  8063. exit:
  8064. if (cd->startup_status == STARTUP_STATUS_START) {
  8065. /* Reset to original state because we could be stuck in BL */
  8066. mutex_lock(&cd->system_lock);
  8067. cd->startup_status = tmp_startup_status;
  8068. mutex_unlock(&cd->system_lock);
  8069. }
  8070. return rc;
  8071. }
  8072. /*******************************************************************************
  8073. * FUNCTION: _pt_request_pip2_launch_app
  8074. *
  8075. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8076. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8077. * consumed.
  8078. *
  8079. * RETURN:
  8080. * 0 = success
  8081. * !0 = failure
  8082. *
  8083. * PARAMETERS:
  8084. * *cd - pointer to core data structure
  8085. ******************************************************************************/
  8086. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8087. {
  8088. return pt_pip2_launch_app(dev, protect);
  8089. }
  8090. /*******************************************************************************
  8091. * FUNCTION: _pt_request_wait_for_enum_state
  8092. *
  8093. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8094. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8095. *
  8096. * RETURN:
  8097. * 0 = success
  8098. * !0 = failure
  8099. *
  8100. * PARAMETERS:
  8101. * *cd - pointer to core data structure
  8102. * timeout - timeout for how long to wait
  8103. * state - enum state to wait for
  8104. ******************************************************************************/
  8105. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8106. int state)
  8107. {
  8108. int rc = 0;
  8109. int t;
  8110. struct pt_core_data *cd = dev_get_drvdata(dev);
  8111. t = wait_event_timeout(cd->wait_q,
  8112. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8113. msecs_to_jiffies(timeout));
  8114. if (IS_TMO(t)) {
  8115. pt_debug(cd->dev, DL_ERROR,
  8116. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8117. __func__, state, timeout);
  8118. pt_debug(cd->dev, DL_WARN,
  8119. "%s: enum state reached 0x%04X\n",
  8120. __func__, cd->startup_status);
  8121. rc = -ETIME;
  8122. } else if (cd->startup_status & state) {
  8123. pt_debug(cd->dev, DL_INFO,
  8124. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8125. __func__, cd->startup_status,
  8126. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8127. } else {
  8128. if (t == 1) {
  8129. pt_debug(
  8130. cd->dev, DL_ERROR,
  8131. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8132. __func__, state, timeout);
  8133. rc = -ETIME;
  8134. } else {
  8135. pt_debug(
  8136. cd->dev, DL_ERROR,
  8137. "%s: Enum state 0x%04X not reached in %dms\n",
  8138. __func__, state, timeout - jiffies_to_msecs(t));
  8139. rc = -EINVAL;
  8140. }
  8141. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8142. __func__, cd->startup_status);
  8143. }
  8144. return rc;
  8145. }
  8146. /*******************************************************************************
  8147. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8148. *
  8149. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8150. * deep sleep.
  8151. *
  8152. * RETURN:
  8153. * 0 = success
  8154. * !0 = error
  8155. *
  8156. * PARAMETERS:
  8157. * *cd - pointer to core data
  8158. ******************************************************************************/
  8159. static int pt_core_wake_device_from_deep_sleep_(
  8160. struct pt_core_data *cd)
  8161. {
  8162. int rc;
  8163. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8164. if (rc)
  8165. rc = -EAGAIN;
  8166. /* Prevent failure on sequential wake/sleep requests from OS */
  8167. msleep(20);
  8168. return rc;
  8169. }
  8170. /*******************************************************************************
  8171. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8172. *
  8173. * SUMMARY: Wake up device from Easy-Wake state.
  8174. *
  8175. * RETURN:
  8176. * 0 = success
  8177. * !0 = failure
  8178. *
  8179. * PARAMETERS:
  8180. * *cd - pointer to core data
  8181. ******************************************************************************/
  8182. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8183. {
  8184. int rc = 0;
  8185. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8186. return rc;
  8187. }
  8188. /*******************************************************************************
  8189. * FUNCTION: pt_restore_parameters_
  8190. *
  8191. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8192. * back to the DUT
  8193. *
  8194. * RETURN:
  8195. * 0 = success
  8196. * !0 = failure
  8197. *
  8198. * PARAMETERS:
  8199. * *cd - pointer the core data structure
  8200. ******************************************************************************/
  8201. static int pt_restore_parameters_(struct pt_core_data *cd)
  8202. {
  8203. struct param_node *param;
  8204. int rc = 0;
  8205. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8206. goto exit;
  8207. spin_lock(&cd->spinlock);
  8208. list_for_each_entry(param, &cd->param_list, node) {
  8209. spin_unlock(&cd->spinlock);
  8210. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8211. __func__, param->id, param->value);
  8212. rc = pt_pip_set_param_(cd, param->id,
  8213. param->value, param->size);
  8214. if (rc)
  8215. goto exit;
  8216. spin_lock(&cd->spinlock);
  8217. }
  8218. spin_unlock(&cd->spinlock);
  8219. exit:
  8220. return rc;
  8221. }
  8222. /*******************************************************************************
  8223. * FUNCTION: pt_pip2_exit_bl_
  8224. *
  8225. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8226. * a DUT that may not have flash and will need FW to be loaded into RAM
  8227. *
  8228. * RETURN:
  8229. * 0 = success
  8230. * !0 = failure
  8231. *
  8232. * PARAMETERS:
  8233. * *cd - pointer the core data structure
  8234. * *status_str - pointer to optional status string buffer
  8235. * buf_size - size of status_str buffer
  8236. ******************************************************************************/
  8237. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8238. {
  8239. int rc;
  8240. int wait_time = 0;
  8241. u8 mode = PT_MODE_UNKNOWN;
  8242. bool load_status_str = false;
  8243. /*
  8244. * Below function has protective call to ensure no enum is still on
  8245. * going, while this kind of protection should be applied widely in
  8246. * future (TODO).
  8247. */
  8248. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8249. if (status_str && buf_size <= 50)
  8250. load_status_str = true;
  8251. if (mode == PT_MODE_BOOTLOADER) {
  8252. if (cd->flashless_dut == 1) {
  8253. rc = pt_hw_hard_reset(cd);
  8254. } else {
  8255. rc = pt_pip2_launch_app(cd->dev,
  8256. PT_CORE_CMD_UNPROTECTED);
  8257. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8258. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8259. __func__, "Invalid image in FLASH rc", rc);
  8260. } else if (rc) {
  8261. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8262. __func__, "Failed to launch app rc", rc);
  8263. }
  8264. }
  8265. if (!rc) {
  8266. if (cd->flashless_dut == 1) {
  8267. /* Wait for BL to complete before enum */
  8268. rc = _pt_request_wait_for_enum_state(cd->dev,
  8269. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8270. if (rc && load_status_str) {
  8271. strlcpy(status_str, "No FW sentinel after BL",
  8272. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8273. goto exit;
  8274. }
  8275. }
  8276. /*
  8277. * If the host wants to interact with the FW or do a
  8278. * forced calibration, the FW must be out of BOOT mode
  8279. * and the system information must have been retrieved.
  8280. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8281. * If, however, the enumeration does not reach this
  8282. * point, the DUT may still be in APP mode so test
  8283. * for all conditions.
  8284. */
  8285. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8286. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8287. if (!rc || cd->startup_status >=
  8288. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8289. mutex_lock(&cd->system_lock);
  8290. cd->mode = PT_MODE_OPERATIONAL;
  8291. mutex_unlock(&cd->system_lock);
  8292. }
  8293. if (rc) {
  8294. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8295. __func__, "Failed to enum with DUT",
  8296. cd->startup_status);
  8297. if (load_status_str && !(cd->startup_status &
  8298. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8299. strlcpy(status_str, "FW Stuck in Boot mode",
  8300. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8301. goto exit;
  8302. }
  8303. }
  8304. /*
  8305. * The coming FW sentinel could wake up the event
  8306. * queue, which has chance to be taken by next command
  8307. * wrongly. Following delay is a workaround to avoid
  8308. * this issue for most situations.
  8309. */
  8310. msleep(20);
  8311. pt_start_wd_timer(cd);
  8312. }
  8313. if (load_status_str) {
  8314. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8315. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8316. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8317. else if (!rc)
  8318. strlcpy(status_str, "Entered APP from BL mode",
  8319. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8320. else
  8321. strlcpy(status_str, "Failed to enter APP from BL mode",
  8322. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8323. }
  8324. } else if (mode == PT_MODE_OPERATIONAL) {
  8325. mutex_lock(&cd->system_lock);
  8326. cd->mode = mode;
  8327. mutex_unlock(&cd->system_lock);
  8328. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8329. if (load_status_str) {
  8330. if (!rc)
  8331. strlcpy(status_str, "Already in APP mode",
  8332. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8333. else
  8334. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8335. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8336. }
  8337. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8338. mutex_lock(&cd->system_lock);
  8339. cd->mode = mode;
  8340. mutex_unlock(&cd->system_lock);
  8341. if (load_status_str)
  8342. strlcpy(status_str, "Failed to determine active mode",
  8343. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8344. }
  8345. exit:
  8346. if (!rc)
  8347. pt_start_wd_timer(cd);
  8348. return rc;
  8349. }
  8350. /*******************************************************************************
  8351. * FUNCTION: pt_pip2_exit_bl
  8352. *
  8353. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8354. * access.
  8355. *
  8356. * RETURN:
  8357. * 0 = success
  8358. * !0 = failure
  8359. *
  8360. * PARAMETERS:
  8361. * *cd - pointer the core data structure
  8362. * *status_str - pointer to optional status string buffer
  8363. * buf_size - size of status_str buffer
  8364. ******************************************************************************/
  8365. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8366. {
  8367. int rc;
  8368. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8369. if (rc < 0) {
  8370. pt_debug(cd->dev, DL_ERROR,
  8371. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8372. cd->exclusive_dev, cd->dev);
  8373. return rc;
  8374. }
  8375. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8376. if (release_exclusive(cd, cd->dev) < 0)
  8377. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8378. __func__);
  8379. return rc;
  8380. }
  8381. /*******************************************************************************
  8382. * FUNCTION: _fast_startup
  8383. *
  8384. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8385. *
  8386. * RETURN:
  8387. * 0 = success
  8388. * !0 = failure
  8389. *
  8390. * PARAMETERS:
  8391. * *cd - pointer the core data structure
  8392. ******************************************************************************/
  8393. static int _fast_startup(struct pt_core_data *cd)
  8394. {
  8395. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8396. int rc = 0;
  8397. u8 mode = PT_MODE_UNKNOWN;
  8398. struct pt_hid_desc hid_desc;
  8399. int wait_time = 0;
  8400. memset(&hid_desc, 0, sizeof(hid_desc));
  8401. reset:
  8402. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8403. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8404. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8405. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8406. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8407. __func__);
  8408. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8409. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8410. if (rc < 0) {
  8411. pt_debug(cd->dev, DL_ERROR,
  8412. "%s: Error on getting HID descriptor r=%d\n",
  8413. __func__, rc);
  8414. if (retry--)
  8415. goto reset;
  8416. goto exit;
  8417. }
  8418. cd->mode = pt_get_mode(cd, &hid_desc);
  8419. if (cd->mode == PT_MODE_BOOTLOADER) {
  8420. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8421. __func__);
  8422. rc = pt_hid_output_bl_launch_app_(cd);
  8423. if (rc < 0) {
  8424. pt_debug(cd->dev, DL_ERROR,
  8425. "%s: Error on launch app r=%d\n",
  8426. __func__, rc);
  8427. if (retry--)
  8428. goto reset;
  8429. goto exit;
  8430. }
  8431. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8432. if (rc < 0) {
  8433. pt_debug(cd->dev, DL_ERROR,
  8434. "%s: Error on getting HID descriptor r=%d\n",
  8435. __func__, rc);
  8436. if (retry--)
  8437. goto reset;
  8438. goto exit;
  8439. }
  8440. cd->mode = pt_get_mode(cd, &hid_desc);
  8441. if (cd->mode == PT_MODE_BOOTLOADER) {
  8442. if (retry--)
  8443. goto reset;
  8444. goto exit;
  8445. }
  8446. }
  8447. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8448. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8449. } else {
  8450. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8451. __func__);
  8452. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8453. /* Wait for any sentinel before first try */
  8454. rc = _pt_request_wait_for_enum_state(
  8455. cd->dev, 150,
  8456. STARTUP_STATUS_BL_RESET_SENTINEL |
  8457. STARTUP_STATUS_FW_RESET_SENTINEL);
  8458. if (rc)
  8459. pt_debug(cd->dev, DL_ERROR,
  8460. "%s: No Sentinel detected rc = %d\n",
  8461. __func__, rc);
  8462. } else
  8463. pt_flush_bus_if_irq_asserted(cd,
  8464. PT_FLUSH_BUS_BASED_ON_LEN);
  8465. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8466. if (rc) {
  8467. pt_debug(cd->dev, DL_ERROR,
  8468. "%s: Get mode failed, mode unknown\n",
  8469. __func__);
  8470. mode = PT_MODE_UNKNOWN;
  8471. }
  8472. cd->mode = mode;
  8473. if (cd->mode == PT_MODE_BOOTLOADER) {
  8474. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8475. __func__);
  8476. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8477. if (rc) {
  8478. pt_debug(cd->dev, DL_ERROR,
  8479. "%s Failed to exit bootloader\n",
  8480. __func__);
  8481. msleep(50);
  8482. rc = -ENODEV;
  8483. if (retry--)
  8484. goto reset;
  8485. goto exit;
  8486. } else {
  8487. pt_debug(cd->dev, DL_INFO,
  8488. "%s: Exit bootloader successfully\n",
  8489. __func__);
  8490. }
  8491. if (cd->mode != PT_MODE_OPERATIONAL) {
  8492. pt_debug(cd->dev, DL_WARN,
  8493. "%s: restore mode failure mode = %d\n",
  8494. __func__, cd->mode);
  8495. if (retry--)
  8496. goto reset;
  8497. goto exit;
  8498. }
  8499. }
  8500. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8501. }
  8502. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8503. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8504. if (!rc) {
  8505. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8506. pt_debug(cd->dev, DL_WARN,
  8507. "%s: Exit FW BOOT Mode after %dms\n",
  8508. __func__, wait_time);
  8509. } else {
  8510. pt_debug(cd->dev, DL_WARN,
  8511. "%s: FW stuck in BOOT Mode after %dms\n",
  8512. __func__, wait_time);
  8513. goto exit;
  8514. }
  8515. if (!cd->sysinfo.ready) {
  8516. rc = pt_hid_output_get_sysinfo_(cd);
  8517. if (rc) {
  8518. pt_debug(cd->dev, DL_ERROR,
  8519. "%s: Error on getting sysinfo r=%d\n",
  8520. __func__, rc);
  8521. if (retry--)
  8522. goto reset;
  8523. goto exit;
  8524. }
  8525. }
  8526. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8527. rc = pt_restore_parameters_(cd);
  8528. if (rc)
  8529. pt_debug(cd->dev, DL_ERROR,
  8530. "%s: failed to restore parameters rc=%d\n",
  8531. __func__, rc);
  8532. else
  8533. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8534. exit:
  8535. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8536. return rc;
  8537. }
  8538. /*******************************************************************************
  8539. * FUNCTION: pt_core_poweron_device_
  8540. *
  8541. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8542. *
  8543. * RETURN:
  8544. * 0 = success
  8545. * !0 = failure
  8546. *
  8547. * PARAMETERS:
  8548. * *cd - pointer to core data
  8549. ******************************************************************************/
  8550. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8551. {
  8552. struct device *dev = cd->dev;
  8553. int rc = 0;
  8554. /*
  8555. * After power on action, the chip can general FW sentinel. It can
  8556. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8557. * _fast_startup() can perform a quick enumeration too, here doesn't
  8558. * need another enumeration.
  8559. */
  8560. mutex_lock(&cd->system_lock);
  8561. cd->startup_status = STARTUP_STATUS_START;
  8562. cd->hid_reset_cmd_state = 1;
  8563. mutex_unlock(&cd->system_lock);
  8564. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8565. if (rc < 0) {
  8566. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8567. __func__, rc);
  8568. goto exit;
  8569. }
  8570. if (!cd->irq_enabled) {
  8571. cd->irq_enabled = true;
  8572. enable_irq(cd->irq);
  8573. }
  8574. /* TBD: following function doesn't update startup_status */
  8575. rc = _fast_startup(cd);
  8576. exit:
  8577. return rc;
  8578. }
  8579. /*******************************************************************************
  8580. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8581. *
  8582. * SUMMARY: Reset device, and then trigger a full enumeration.
  8583. *
  8584. * RETURN:
  8585. * 0 = success
  8586. * !0 = failure
  8587. *
  8588. * PARAMETERS:
  8589. * *cd - pointer to core data
  8590. ******************************************************************************/
  8591. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8592. {
  8593. int rc;
  8594. rc = pt_dut_reset_and_wait(cd);
  8595. if (rc < 0) {
  8596. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8597. __func__, rc);
  8598. goto exit;
  8599. }
  8600. rc = _fast_startup(cd);
  8601. exit:
  8602. return rc;
  8603. }
  8604. /*******************************************************************************
  8605. * FUNCTION: pt_core_easywake_off_
  8606. *
  8607. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8608. * the configuration in the core platform data structure.
  8609. *
  8610. * RETURN:
  8611. * 0 = success
  8612. * !0 = failure
  8613. *
  8614. * PARAMETERS:
  8615. * *cd - pointer to core data
  8616. ******************************************************************************/
  8617. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8618. {
  8619. int rc = 0;
  8620. mutex_lock(&cd->system_lock);
  8621. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8622. mutex_unlock(&cd->system_lock);
  8623. pt_debug(cd->dev, DL_INFO,
  8624. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8625. return 0;
  8626. }
  8627. mutex_unlock(&cd->system_lock);
  8628. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8629. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8630. rc = pt_core_wake_device_from_easy_wake_(cd);
  8631. if (rc < 0)
  8632. pt_debug(cd->dev, DL_ERROR,
  8633. "%s - %d failed %d\n", __func__, rc);
  8634. }
  8635. mutex_lock(&cd->system_lock);
  8636. cd->sleep_state = SS_EASY_WAKING_OFF;
  8637. mutex_unlock(&cd->system_lock);
  8638. pt_start_wd_timer(cd);
  8639. return rc;
  8640. }
  8641. /*******************************************************************************
  8642. * FUNCTION: pt_core_easywake_off
  8643. *
  8644. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8645. *
  8646. * RETURN:
  8647. * 0 = success
  8648. * !0 = failure
  8649. *
  8650. * PARAMETERS:
  8651. * *cd - pointer to core data
  8652. ******************************************************************************/
  8653. static int pt_core_easywake_off(struct pt_core_data *cd)
  8654. {
  8655. int rc;
  8656. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8657. if (rc < 0) {
  8658. pt_debug(cd->dev, DL_ERROR,
  8659. "%s: fail get exclusive ex=%p own=%p\n",
  8660. __func__, cd->exclusive_dev, cd->dev);
  8661. return rc;
  8662. }
  8663. rc = pt_core_easywake_off_(cd);
  8664. if (release_exclusive(cd, cd->dev) < 0)
  8665. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8666. __func__);
  8667. else
  8668. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8669. __func__);
  8670. return rc;
  8671. }
  8672. /*******************************************************************************
  8673. * FUNCTION: pt_core_wake_
  8674. *
  8675. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8676. * the configuration in the core platform data structure.
  8677. *
  8678. * RETURN:
  8679. * 0 = success
  8680. * !0 = failure
  8681. *
  8682. * PARAMETERS:
  8683. * *cd - pointer to core data
  8684. ******************************************************************************/
  8685. static int pt_core_wake_(struct pt_core_data *cd)
  8686. {
  8687. int rc = 0;
  8688. mutex_lock(&cd->system_lock);
  8689. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8690. mutex_unlock(&cd->system_lock);
  8691. pt_debug(cd->dev, DL_INFO,
  8692. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8693. return 0;
  8694. } else {
  8695. cd->sleep_state = SS_WAKING;
  8696. }
  8697. mutex_unlock(&cd->system_lock);
  8698. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8699. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8700. pt_debug(cd->dev, DL_INFO,
  8701. "%s: Entering into poweron mode:\n", __func__);
  8702. rc = pt_core_poweron_device_(cd);
  8703. if (rc < 0)
  8704. pr_err("%s: Poweron error detected: rc=%d\n",
  8705. __func__, rc);
  8706. }
  8707. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8708. rc = pt_core_wake_device_from_deep_standby_(cd);
  8709. else /* Default action to exit DeepSleep */
  8710. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8711. }
  8712. mutex_lock(&cd->system_lock);
  8713. cd->sleep_state = SS_SLEEP_OFF;
  8714. mutex_unlock(&cd->system_lock);
  8715. pt_start_wd_timer(cd);
  8716. return rc;
  8717. }
  8718. /*******************************************************************************
  8719. * FUNCTION: pt_core_wake_
  8720. *
  8721. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8722. *
  8723. * RETURN:
  8724. * 0 = success
  8725. * !0 = failure
  8726. *
  8727. * PARAMETERS:
  8728. * *cd - pointer to core data
  8729. ******************************************************************************/
  8730. static int pt_core_wake(struct pt_core_data *cd)
  8731. {
  8732. int rc = 0;
  8733. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8734. if (rc < 0) {
  8735. pt_debug(cd->dev, DL_ERROR,
  8736. "%s: fail get exclusive ex=%p own=%p\n",
  8737. __func__, cd->exclusive_dev, cd->dev);
  8738. return rc;
  8739. }
  8740. rc = pt_core_wake_(cd);
  8741. if (release_exclusive(cd, cd->dev) < 0)
  8742. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8743. __func__);
  8744. else
  8745. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8746. __func__);
  8747. return rc;
  8748. }
  8749. /*******************************************************************************
  8750. * FUNCTION: pt_get_ic_crc_
  8751. *
  8752. * SUMMARY: This function retrieves the config block CRC
  8753. *
  8754. * NOTE: The post condition of calling this function will be that the DUT will
  8755. * be in SCANNINING mode if no failures occur
  8756. *
  8757. * RETURN:
  8758. * 0 = success
  8759. * !0 = failure
  8760. *
  8761. * PARAMETERS:
  8762. * *cd - pointer the core data structure
  8763. * ebid - enumerated block ID
  8764. ******************************************************************************/
  8765. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8766. {
  8767. struct pt_sysinfo *si = &cd->sysinfo;
  8768. int rc = 0;
  8769. u8 status;
  8770. u16 calculated_crc = 0;
  8771. u16 stored_crc = 0;
  8772. rc = pt_pip_suspend_scanning_(cd);
  8773. if (rc)
  8774. goto error;
  8775. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8776. &calculated_crc, &stored_crc);
  8777. if (rc)
  8778. goto exit;
  8779. if (status) {
  8780. rc = -EINVAL;
  8781. goto exit;
  8782. }
  8783. si->ttconfig.crc = stored_crc;
  8784. exit:
  8785. pt_pip_resume_scanning_(cd);
  8786. error:
  8787. pt_debug(cd->dev, DL_INFO,
  8788. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8789. __func__, ebid, calculated_crc, stored_crc, rc);
  8790. return rc;
  8791. }
  8792. /*******************************************************************************
  8793. * FUNCTION: pt_pip2_read_gpio
  8794. *
  8795. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8796. * bits into the passed in variable
  8797. *
  8798. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8799. *
  8800. * RETURN:
  8801. * 0 = success
  8802. * !0 = failure
  8803. *
  8804. * PARAMETERS:
  8805. * *dev - pointer to device structure
  8806. * *status - pointer to where the command response status is stored
  8807. * *gpio - pointer to device attributes structure
  8808. ******************************************************************************/
  8809. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8810. {
  8811. int rc = 0;
  8812. u16 actual_read_len;
  8813. u8 read_buf[12];
  8814. u8 tmp_status = 0;
  8815. u8 index = PIP2_RESP_STATUS_OFFSET;
  8816. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8817. rc = _pt_request_pip2_send_cmd(dev,
  8818. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8819. NULL, 0, read_buf, &actual_read_len);
  8820. if (rc) {
  8821. pt_debug(dev, DL_ERROR,
  8822. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8823. rc = -ECOMM;
  8824. } else {
  8825. tmp_status = read_buf[index];
  8826. }
  8827. if (status)
  8828. *status = tmp_status;
  8829. if (!rc && gpio && (tmp_status == 0)) {
  8830. *gpio = ((read_buf[index + 4] << 24) |
  8831. (read_buf[index + 3] << 16) |
  8832. (read_buf[index + 2] << 8) |
  8833. (read_buf[index + 1]));
  8834. }
  8835. return rc;
  8836. }
  8837. /*******************************************************************************
  8838. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8839. *
  8840. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8841. *
  8842. * RETURN:
  8843. * 0 = success
  8844. * !0 = failure
  8845. *
  8846. * PARAMETERS:
  8847. * *cd - pointer the core data structure
  8848. * *pid - pointer to store panel ID
  8849. ******************************************************************************/
  8850. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8851. {
  8852. u32 gpio_value = 0;
  8853. u8 status = 0;
  8854. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8855. int rc = 0;
  8856. if (!pid)
  8857. return -ENOMEM;
  8858. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8859. if (!rc) {
  8860. if (status == 0) {
  8861. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8862. PT_PANEL_ID_SHIFT;
  8863. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8864. __func__,
  8865. "BL mode PID", panel_id, "gpio", gpio_value);
  8866. *pid = panel_id;
  8867. } else {
  8868. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8869. __func__,
  8870. "BL read gpio failed status", status);
  8871. }
  8872. } else {
  8873. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8874. __func__,
  8875. "BL read gpio failed status", status);
  8876. }
  8877. return rc;
  8878. }
  8879. /*******************************************************************************
  8880. * FUNCTION: pt_enum_with_dut_
  8881. *
  8882. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8883. * The core data (cd) startup_status will store, as a bitmask, each
  8884. * state of the enumeration process. The startup will be attempted
  8885. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8886. *
  8887. * RETURN:
  8888. * 0 = success
  8889. * !0 = failure
  8890. *
  8891. * PARAMETERS:
  8892. * *cd - pointer the core data structure
  8893. * reset - Flag to reset the DUT before attempting to enumerate
  8894. * *status - poionter to store the enum status bitmask flags
  8895. ******************************************************************************/
  8896. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8897. u32 *enum_status)
  8898. {
  8899. int try = 1;
  8900. int rc = 0;
  8901. int wait_time = 0;
  8902. bool detected = false;
  8903. u8 return_data[8];
  8904. u8 mode = PT_MODE_UNKNOWN;
  8905. u8 pid = PANEL_ID_NOT_ENABLED;
  8906. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8907. struct pt_hid_desc hid_desc;
  8908. memset(&hid_desc, 0, sizeof(hid_desc));
  8909. #ifdef TTHE_TUNER_SUPPORT
  8910. tthe_print(cd, NULL, 0, "enter startup");
  8911. #endif
  8912. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8913. __func__, cd->startup_status, reset);
  8914. pt_stop_wd_timer(cd);
  8915. reset:
  8916. if (try > 1)
  8917. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8918. __func__, try);
  8919. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8920. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8921. pt_debug(cd->dev, DL_INFO,
  8922. "%s: PIP1 Enumeration start\n", __func__);
  8923. /* Only reset the DUT after the first try */
  8924. if (reset || (try > 1)) {
  8925. /*
  8926. * Reset hardware only for Legacy parts. Skip for TT/TC
  8927. * parts because if the FW image was loaded directly
  8928. * to SRAM issueing a reset ill wipe out what was just
  8929. * loaded.
  8930. */
  8931. rc = pt_dut_reset_and_wait(cd);
  8932. if (rc < 0) {
  8933. pt_debug(cd->dev, DL_ERROR,
  8934. "%s: Error on h/w reset r=%d\n",
  8935. __func__, rc);
  8936. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8937. goto reset;
  8938. goto exit;
  8939. }
  8940. /* sleep to allow FW to be launched if available */
  8941. msleep(120);
  8942. }
  8943. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8944. if (rc < 0) {
  8945. pt_debug(cd->dev, DL_ERROR,
  8946. "%s: Error getting HID Descriptor r=%d\n",
  8947. __func__, rc);
  8948. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8949. goto reset;
  8950. rc = -EIO;
  8951. goto exit;
  8952. }
  8953. detected = true;
  8954. cd->mode = pt_get_mode(cd, &hid_desc);
  8955. /*
  8956. * Most systems do not use an XY pin as the panel_id and so
  8957. * the BL is used to retrieve the panel_id, however for
  8958. * systems that do use an XY pin, the panel_id MUST be
  8959. * retrieved from the system information when running FW
  8960. * (done below) and so this section of code is skipped.
  8961. * Entering the BL here is only needed on XY_PIN systems.
  8962. */
  8963. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8964. if (cd->mode == PT_MODE_OPERATIONAL) {
  8965. rc = pt_pip_start_bootloader_(cd);
  8966. if (rc < 0) {
  8967. pt_debug(cd->dev, DL_ERROR,
  8968. "%s: Error on start bootloader r=%d\n",
  8969. __func__, rc);
  8970. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8971. goto reset;
  8972. goto exit;
  8973. }
  8974. cd->mode = PT_MODE_BOOTLOADER;
  8975. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8976. __func__);
  8977. }
  8978. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8979. if (!rc) {
  8980. cd->bl_info.ready = true;
  8981. cd->bl_info.chip_id =
  8982. get_unaligned_le16(&return_data[2]);
  8983. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8984. __func__, cd->bl_info.chip_id);
  8985. } else {
  8986. pt_debug(cd->dev, DL_ERROR,
  8987. "%s: failed to get chip ID, r=%d\n",
  8988. __func__, rc);
  8989. }
  8990. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8991. mutex_lock(&cd->system_lock);
  8992. if (!rc) {
  8993. cd->pid_for_loader = pid;
  8994. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8995. __func__, cd->pid_for_loader);
  8996. } else {
  8997. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8998. pt_debug(cd->dev, DL_WARN,
  8999. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9000. __func__, cd->pid_for_loader);
  9001. }
  9002. mutex_unlock(&cd->system_lock);
  9003. }
  9004. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9005. if (cd->mode == PT_MODE_BOOTLOADER) {
  9006. rc = pt_hid_output_bl_launch_app_(cd);
  9007. if (rc < 0) {
  9008. pt_debug(cd->dev, DL_ERROR,
  9009. "%s: Error on launch app r=%d\n",
  9010. __func__, rc);
  9011. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9012. goto reset;
  9013. rc = -ENODEV;
  9014. goto exit;
  9015. }
  9016. /* sleep to allow FW to be launched if available */
  9017. msleep(120);
  9018. /* Ensure the DUT is now in Application mode */
  9019. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9020. if (rc < 0) {
  9021. pt_debug(cd->dev, DL_ERROR,
  9022. "%s: Error getting HID Desc r=%d\n",
  9023. __func__, rc);
  9024. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9025. goto reset;
  9026. rc = -EIO;
  9027. goto exit;
  9028. }
  9029. cd->mode = pt_get_mode(cd, &hid_desc);
  9030. if (cd->mode == PT_MODE_BOOTLOADER) {
  9031. pt_debug(cd->dev, DL_WARN,
  9032. "%s: Error confiming exit BL\n",
  9033. __func__);
  9034. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9035. goto reset;
  9036. rc = -ENODEV;
  9037. goto exit;
  9038. }
  9039. }
  9040. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9041. cd->mode = PT_MODE_OPERATIONAL;
  9042. *enum_status |= STARTUP_STATUS_GET_DESC;
  9043. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9044. } else {
  9045. /* Generation is PIP2 Capable */
  9046. pt_debug(cd->dev, DL_INFO,
  9047. "%s: PIP2 Enumeration start\n", __func__);
  9048. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9049. if (rc) {
  9050. pt_debug(cd->dev, DL_ERROR,
  9051. "%s: Get mode failed, mode unknown\n",
  9052. __func__);
  9053. mode = PT_MODE_UNKNOWN;
  9054. } else
  9055. detected = true;
  9056. cd->mode = mode;
  9057. switch (cd->mode) {
  9058. case PT_MODE_OPERATIONAL:
  9059. pt_debug(cd->dev, DL_INFO,
  9060. "%s: Operational mode\n", __func__);
  9061. if (cd->app_pip_ver_ready == false) {
  9062. rc = pt_pip2_get_version_(cd);
  9063. if (!rc)
  9064. cd->app_pip_ver_ready = true;
  9065. else {
  9066. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9067. goto reset;
  9068. goto exit;
  9069. }
  9070. }
  9071. break;
  9072. case PT_MODE_BOOTLOADER:
  9073. pt_debug(cd->dev, DL_INFO,
  9074. "%s: Bootloader mode\n", __func__);
  9075. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9076. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9077. mutex_lock(&cd->system_lock);
  9078. if (!rc) {
  9079. cd->pid_for_loader = pid;
  9080. pt_debug(cd->dev, DL_INFO,
  9081. "%s: Panel ID: 0x%02X\n",
  9082. __func__, cd->pid_for_loader);
  9083. } else {
  9084. cd->pid_for_loader =
  9085. PANEL_ID_NOT_ENABLED;
  9086. pt_debug(cd->dev, DL_WARN,
  9087. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9088. __func__, cd->pid_for_loader);
  9089. }
  9090. mutex_unlock(&cd->system_lock);
  9091. }
  9092. if (cd->bl_pip_ver_ready == false) {
  9093. rc = pt_pip2_get_version_(cd);
  9094. if (!rc)
  9095. cd->bl_pip_ver_ready = true;
  9096. else {
  9097. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9098. goto reset;
  9099. goto exit;
  9100. }
  9101. }
  9102. /*
  9103. * Launch app command will fail in flashless mode.
  9104. * Skip launch app command here to save time for
  9105. * enumeration flow.
  9106. */
  9107. if (cd->flashless_dut)
  9108. goto exit;
  9109. /*
  9110. * pt_pip2_launch_app() is needed here instead of
  9111. * pt_pip2_exit_bl() because exit_bl will cause several
  9112. * hw_resets to occur and the auto BL on a flashless
  9113. * DUT will fail.
  9114. */
  9115. rc = pt_pip2_launch_app(cd->dev,
  9116. PT_CORE_CMD_UNPROTECTED);
  9117. if (rc) {
  9118. pt_debug(cd->dev, DL_ERROR,
  9119. "%s: Error on launch app r=%d\n",
  9120. __func__, rc);
  9121. msleep(50);
  9122. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9123. goto reset;
  9124. goto exit;
  9125. }
  9126. /*
  9127. * IRQ thread can be delayed if the serial log print is
  9128. * enabled. It causes next command to get wrong response
  9129. * Here the delay is to ensure pt_parse_input() to be
  9130. * finished.
  9131. */
  9132. msleep(60);
  9133. /* Check and update the mode */
  9134. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9135. if (rc) {
  9136. pt_debug(cd->dev, DL_ERROR,
  9137. "%s: Get mode failed, mode unknown\n",
  9138. __func__);
  9139. mode = PT_MODE_UNKNOWN;
  9140. }
  9141. cd->mode = mode;
  9142. if (cd->mode == PT_MODE_OPERATIONAL) {
  9143. pt_debug(cd->dev, DL_INFO,
  9144. "%s: Launched to Operational mode\n",
  9145. __func__);
  9146. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9147. pt_debug(cd->dev, DL_ERROR,
  9148. "%s: Launch failed, Bootloader mode\n",
  9149. __func__);
  9150. goto exit;
  9151. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9152. pt_debug(cd->dev, DL_ERROR,
  9153. "%s: Launch failed, Unknown mode\n",
  9154. __func__);
  9155. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9156. goto reset;
  9157. goto exit;
  9158. }
  9159. if (cd->app_pip_ver_ready == false) {
  9160. rc = pt_pip2_get_version_(cd);
  9161. if (!rc)
  9162. cd->app_pip_ver_ready = true;
  9163. else {
  9164. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9165. goto reset;
  9166. goto exit;
  9167. }
  9168. }
  9169. break;
  9170. default:
  9171. pt_debug(cd->dev, DL_ERROR,
  9172. "%s: Unknown mode\n", __func__);
  9173. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9174. goto reset;
  9175. goto exit;
  9176. }
  9177. *enum_status |= STARTUP_STATUS_GET_DESC;
  9178. }
  9179. pt_init_pip_report_fields(cd);
  9180. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9181. if (!cd->features.easywake)
  9182. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9183. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9184. rc = pt_hid_output_get_sysinfo_(cd);
  9185. if (rc) {
  9186. pt_debug(cd->dev, DL_ERROR,
  9187. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9188. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9189. goto reset;
  9190. goto exit;
  9191. }
  9192. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9193. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9194. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9195. if (!rc) {
  9196. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9197. pt_debug(cd->dev, DL_WARN,
  9198. "%s: Exit FW BOOT Mode after %dms\n",
  9199. __func__, wait_time);
  9200. } else {
  9201. pt_debug(cd->dev, DL_WARN,
  9202. "%s: FW stuck in BOOT Mode after %dms\n",
  9203. __func__, wait_time);
  9204. goto exit;
  9205. }
  9206. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9207. __func__,
  9208. cd->sysinfo.ttdata.pip_ver_major,
  9209. cd->sysinfo.ttdata.pip_ver_minor);
  9210. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9211. if (rc) {
  9212. pt_debug(cd->dev, DL_ERROR,
  9213. "%s: DUT Config block CRC failure rc=%d\n",
  9214. __func__, rc);
  9215. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9216. goto reset;
  9217. } else {
  9218. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9219. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9220. pt_debug(cd->dev, DL_ERROR,
  9221. "%s: scan state: %d, retry: %d\n",
  9222. __func__, sys_mode, try);
  9223. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9224. goto reset;
  9225. } else
  9226. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9227. }
  9228. rc = pt_restore_parameters_(cd);
  9229. if (rc) {
  9230. pt_debug(cd->dev, DL_ERROR,
  9231. "%s: Failed to restore parameters rc=%d\n",
  9232. __func__, rc);
  9233. } else
  9234. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9235. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9236. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9237. cd->startup_retry_count = 0;
  9238. exit:
  9239. /* Generate the HW Version of the connected DUT and store in cd */
  9240. pt_generate_hw_version(cd, cd->hw_version);
  9241. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9242. cd->hw_version);
  9243. pt_start_wd_timer(cd);
  9244. if (!detected)
  9245. rc = -ENODEV;
  9246. #ifdef TTHE_TUNER_SUPPORT
  9247. tthe_print(cd, NULL, 0, "exit startup");
  9248. #endif
  9249. pt_debug(cd->dev, DL_WARN,
  9250. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9251. __func__, rc, try);
  9252. return rc;
  9253. }
  9254. /*******************************************************************************
  9255. * FUNCTION: pt_enum_with_dut
  9256. *
  9257. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9258. * requesting exclusive access around the call.
  9259. *
  9260. * RETURN:
  9261. * 0 = success
  9262. * !0 = failure
  9263. *
  9264. * PARAMETERS:
  9265. * *cd - pointer the core data structure
  9266. * reset - Flag to reset the DUT before attempting to enumerate
  9267. * *status - pointer to store the enum status bitmask flags
  9268. ******************************************************************************/
  9269. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9270. {
  9271. int rc = 0;
  9272. mutex_lock(&cd->system_lock);
  9273. cd->startup_state = STARTUP_RUNNING;
  9274. mutex_unlock(&cd->system_lock);
  9275. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9276. if (rc) {
  9277. pt_debug(cd->dev, DL_ERROR,
  9278. "%s: fail get exclusive ex=%p own=%p\n",
  9279. __func__, cd->exclusive_dev, cd->dev);
  9280. goto exit;
  9281. }
  9282. rc = pt_enum_with_dut_(cd, reset, status);
  9283. if (release_exclusive(cd, cd->dev) < 0)
  9284. /* Don't return fail code, mode is already changed. */
  9285. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9286. __func__);
  9287. else
  9288. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9289. __func__);
  9290. exit:
  9291. mutex_lock(&cd->system_lock);
  9292. /* Clear startup state for any tasks waiting for startup completion */
  9293. cd->startup_state = STARTUP_NONE;
  9294. mutex_unlock(&cd->system_lock);
  9295. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9296. *status |= STARTUP_STATUS_COMPLETE;
  9297. /* Wake the waiters for end of startup */
  9298. wake_up(&cd->wait_q);
  9299. return rc;
  9300. }
  9301. static int add_sysfs_interfaces(struct device *dev);
  9302. static void remove_sysfs_interfaces(struct device *dev);
  9303. static void remove_sysfs_and_modules(struct device *dev);
  9304. static void pt_release_modules(struct pt_core_data *cd);
  9305. static void pt_probe_modules(struct pt_core_data *cd);
  9306. /*******************************************************************************
  9307. * FUNCTION: _pt_ttdl_restart
  9308. *
  9309. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9310. *
  9311. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9312. * existing sysfs nodes will produce an error.
  9313. *
  9314. * RETURN:
  9315. * 0 = success
  9316. * !0 = failure
  9317. *
  9318. * PARAMETERS:
  9319. * *dev - pointer to core device
  9320. ******************************************************************************/
  9321. static int _pt_ttdl_restart(struct device *dev)
  9322. {
  9323. int rc = 0;
  9324. struct pt_core_data *cd = dev_get_drvdata(dev);
  9325. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9326. struct i2c_client *client =
  9327. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9328. #endif
  9329. /*
  9330. * Make sure the device is awake, pt_mt_release function will
  9331. * cause pm sleep function and lead to deadlock.
  9332. */
  9333. pm_runtime_get_sync(dev);
  9334. /* Use ttdl_restart_lock to avoid reentry */
  9335. mutex_lock(&cd->ttdl_restart_lock);
  9336. remove_sysfs_and_modules(cd->dev);
  9337. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9338. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9339. pt_debug(dev, DL_ERROR,
  9340. "%s I2C functionality not Supported\n", __func__);
  9341. rc = -EIO;
  9342. goto ttdl_no_error;
  9343. }
  9344. #endif
  9345. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9346. rc = _pt_detect_dut_generation(cd->dev,
  9347. &cd->startup_status, &cd->active_dut_generation,
  9348. &cd->mode);
  9349. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9350. pt_debug(dev, DL_ERROR,
  9351. "%s: Error, Unknown DUT Generation rc=%d\n",
  9352. __func__, rc);
  9353. }
  9354. }
  9355. rc = add_sysfs_interfaces(cd->dev);
  9356. if (rc < 0)
  9357. pt_debug(cd->dev, DL_ERROR,
  9358. "%s: Error, failed sysfs nodes rc=%d\n",
  9359. __func__, rc);
  9360. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9361. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9362. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9363. if (rc < 0)
  9364. pt_debug(dev, DL_ERROR,
  9365. "%s: Error, Failed to Enumerate\n", __func__);
  9366. }
  9367. rc = pt_mt_probe(dev);
  9368. if (rc < 0) {
  9369. pt_debug(dev, DL_ERROR,
  9370. "%s: Error, fail mt probe\n", __func__);
  9371. }
  9372. rc = pt_btn_probe(dev);
  9373. if (rc < 0) {
  9374. pt_debug(dev, DL_ERROR,
  9375. "%s: Error, fail btn probe\n", __func__);
  9376. }
  9377. pt_probe_modules(cd);
  9378. pt_debug(cd->dev, DL_WARN,
  9379. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9380. rc = 0;
  9381. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9382. ttdl_no_error:
  9383. #endif
  9384. mutex_unlock(&cd->ttdl_restart_lock);
  9385. mutex_lock(&cd->system_lock);
  9386. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9387. cd->startup_state = STARTUP_NONE;
  9388. mutex_unlock(&cd->system_lock);
  9389. pm_runtime_put(dev);
  9390. return rc;
  9391. }
  9392. /*******************************************************************************
  9393. * FUNCTION: pt_restart_work_function
  9394. *
  9395. * SUMMARY: This is the wrapper function placed in a work queue to call
  9396. * _pt_ttdl_restart()
  9397. *
  9398. * RETURN: none
  9399. *
  9400. * PARAMETERS:
  9401. * *work - pointer to the work_struct
  9402. ******************************************************************************/
  9403. static void pt_restart_work_function(struct work_struct *work)
  9404. {
  9405. struct pt_core_data *cd = container_of(work,
  9406. struct pt_core_data, ttdl_restart_work);
  9407. int rc = 0;
  9408. rc = _pt_ttdl_restart(cd->dev);
  9409. if (rc < 0)
  9410. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9411. __func__, rc);
  9412. }
  9413. /*******************************************************************************
  9414. * FUNCTION: pt_enum_work_function
  9415. *
  9416. * SUMMARY: This is the wrapper function placed in a work queue to call
  9417. * pt_enum_with_dut()
  9418. *
  9419. * RETURN: none
  9420. *
  9421. * PARAMETERS:
  9422. * *work - pointer to the work_struct
  9423. ******************************************************************************/
  9424. static void pt_enum_work_function(struct work_struct *work)
  9425. {
  9426. struct pt_core_data *cd = container_of(work,
  9427. struct pt_core_data, enum_work);
  9428. int rc;
  9429. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9430. if (rc < 0)
  9431. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9432. __func__, rc);
  9433. }
  9434. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9435. {
  9436. int rc;
  9437. if (!get) {
  9438. rc = 0;
  9439. goto regulator_put;
  9440. }
  9441. cd->vdd = regulator_get(cd->dev, "vdd");
  9442. if (IS_ERR(cd->vdd)) {
  9443. rc = PTR_ERR(cd->vdd);
  9444. dev_err(cd->dev,
  9445. "Regulator get failed vdd rc=%d\n", rc);
  9446. goto regulator_put;
  9447. }
  9448. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9449. if (IS_ERR(cd->vcc_i2c)) {
  9450. rc = PTR_ERR(cd->vcc_i2c);
  9451. dev_err(cd->dev,
  9452. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9453. goto regulator_put;
  9454. }
  9455. return 0;
  9456. regulator_put:
  9457. if (cd->vdd) {
  9458. regulator_put(cd->vdd);
  9459. cd->vdd = NULL;
  9460. }
  9461. if (cd->vcc_i2c) {
  9462. regulator_put(cd->vcc_i2c);
  9463. cd->vcc_i2c = NULL;
  9464. }
  9465. return rc;
  9466. }
  9467. #ifdef ENABLE_I2C_REG_ONLY
  9468. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9469. {
  9470. int rc = 0;
  9471. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9472. if (!en) {
  9473. rc = 0;
  9474. goto disable_vcc_i2c_reg_only;
  9475. }
  9476. if (cd->vcc_i2c) {
  9477. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9478. if (rc < 0)
  9479. pt_debug(cd->dev, DL_INFO,
  9480. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9481. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9482. __func__, rc);
  9483. }
  9484. return 0;
  9485. disable_vcc_i2c_reg_only:
  9486. if (cd->vcc_i2c) {
  9487. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9488. if (rc < 0)
  9489. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9490. __func__, rc);
  9491. }
  9492. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9493. return rc;
  9494. }
  9495. #endif
  9496. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9497. {
  9498. int rc;
  9499. if (!en) {
  9500. rc = 0;
  9501. goto disable_vcc_i2c_reg;
  9502. }
  9503. if (cd->vdd) {
  9504. if (regulator_count_voltages(cd->vdd) > 0) {
  9505. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9506. FT_VTG_MAX_UV);
  9507. if (rc) {
  9508. dev_err(cd->dev,
  9509. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9510. goto exit;
  9511. }
  9512. }
  9513. rc = regulator_enable(cd->vdd);
  9514. if (rc) {
  9515. dev_err(cd->dev,
  9516. "Regulator vdd enable failed rc=%d\n", rc);
  9517. goto exit;
  9518. }
  9519. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9520. }
  9521. if (cd->vcc_i2c) {
  9522. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9523. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9524. FT_I2C_VTG_MAX_UV);
  9525. if (rc) {
  9526. dev_err(cd->dev,
  9527. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9528. goto disable_vdd_reg;
  9529. }
  9530. }
  9531. rc = regulator_enable(cd->vcc_i2c);
  9532. if (rc) {
  9533. dev_err(cd->dev,
  9534. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9535. goto disable_vdd_reg;
  9536. }
  9537. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9538. }
  9539. return 0;
  9540. disable_vcc_i2c_reg:
  9541. if (cd->vcc_i2c) {
  9542. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9543. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9544. FT_I2C_VTG_MAX_UV);
  9545. regulator_disable(cd->vcc_i2c);
  9546. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9547. }
  9548. disable_vdd_reg:
  9549. if (cd->vdd) {
  9550. if (regulator_count_voltages(cd->vdd) > 0)
  9551. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9552. FT_VTG_MAX_UV);
  9553. regulator_disable(cd->vdd);
  9554. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9555. }
  9556. exit:
  9557. return rc;
  9558. }
  9559. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9560. #define KERNEL_VER_GT_3_19
  9561. #endif
  9562. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9563. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9564. #if defined(CONFIG_PM_SLEEP)
  9565. /*******************************************************************************
  9566. * FUNCTION: pt_core_rt_suspend
  9567. *
  9568. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9569. *
  9570. * RETURN:
  9571. * 0 = success
  9572. * !0 = failure
  9573. *
  9574. * PARAMETERS:
  9575. * *dev - pointer to core device
  9576. ******************************************************************************/
  9577. static int pt_core_rt_suspend(struct device *dev)
  9578. {
  9579. struct pt_core_data *cd = dev_get_drvdata(dev);
  9580. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9581. __func__, cd->core_probe_complete);
  9582. return 0;
  9583. }
  9584. /*******************************************************************************
  9585. * FUNCTION: pt_core_rt_resume
  9586. *
  9587. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9588. *
  9589. * RETURN:
  9590. * 0 = success
  9591. * !0 = failure
  9592. *
  9593. * PARAMETERS:
  9594. * *dev - pointer to core device
  9595. ******************************************************************************/
  9596. static int pt_core_rt_resume(struct device *dev)
  9597. {
  9598. struct pt_core_data *cd = dev_get_drvdata(dev);
  9599. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9600. __func__, cd->core_probe_complete);
  9601. return 0;
  9602. }
  9603. #endif /* CONFIG_PM_SLEEP */
  9604. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9605. #if defined(CONFIG_PM_SLEEP)
  9606. /*******************************************************************************
  9607. * FUNCTION: pt_core_suspend_
  9608. *
  9609. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9610. * pt_core_sleep. This function may disable IRQ during sleep state.
  9611. *
  9612. * RETURN:
  9613. * 0 = success
  9614. * !0 = failure
  9615. *
  9616. * PARAMETERS:
  9617. * *dev - pointer to core device
  9618. ******************************************************************************/
  9619. static int pt_core_suspend_(struct device *dev)
  9620. {
  9621. int rc;
  9622. struct pt_core_data *cd = dev_get_drvdata(dev);
  9623. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9624. rc = pt_core_sleep(cd);
  9625. if (rc) {
  9626. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9627. __func__, rc);
  9628. return -EAGAIN;
  9629. }
  9630. rc = pt_enable_regulator(cd, false);
  9631. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9632. return 0;
  9633. /* Required to prevent interrupts before bus awake */
  9634. disable_irq(cd->irq);
  9635. cd->irq_disabled = 1;
  9636. if (device_may_wakeup(dev)) {
  9637. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9638. __func__);
  9639. if (!enable_irq_wake(cd->irq))
  9640. cd->irq_wake = 1;
  9641. } else {
  9642. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9643. __func__);
  9644. }
  9645. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9646. return rc;
  9647. }
  9648. /*******************************************************************************
  9649. * FUNCTION: pt_core_suspend
  9650. *
  9651. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9652. * woke up or put to sleep based on Kernel power state even when the display
  9653. * is off based on the check of TTDL core platform flag.
  9654. *
  9655. * RETURN:
  9656. * 0 = success
  9657. * !0 = failure
  9658. *
  9659. * PARAMETERS:
  9660. * *dev - pointer to core device
  9661. ******************************************************************************/
  9662. static int pt_core_suspend(struct device *dev)
  9663. {
  9664. struct pt_core_data *cd = dev_get_drvdata(dev);
  9665. int rc = 0;
  9666. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9667. return 0;
  9668. pt_debug(cd->dev, DL_INFO, "%s Suspend start\n", __func__);
  9669. cancel_work_sync(&cd->resume_work);
  9670. cancel_work_sync(&cd->suspend_work);
  9671. mutex_lock(&cd->system_lock);
  9672. cd->wait_until_wake = 0;
  9673. mutex_unlock(&cd->system_lock);
  9674. if (pm_suspend_via_firmware()) {
  9675. rc = pt_core_suspend_(cd->dev);
  9676. cd->quick_boot = true;
  9677. } else {
  9678. rc = pt_enable_i2c_regulator(cd, false);
  9679. if (rc < 0)
  9680. pt_debug(cd->dev, DL_ERROR,
  9681. "%s: Error on disabling i2c regulator\n", __func__);
  9682. }
  9683. pt_debug(cd->dev, DL_INFO, "%s Suspend exit - rc = %d\n", __func__, rc);
  9684. return rc;
  9685. }
  9686. /*******************************************************************************
  9687. * FUNCTION: pt_core_resume_
  9688. *
  9689. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9690. * pt_core_wake. This function may enable IRQ before wake up.
  9691. *
  9692. * RETURN:
  9693. * 0 = success
  9694. * !0 = failure
  9695. *
  9696. * PARAMETERS:
  9697. * *dev - pointer to core device
  9698. ******************************************************************************/
  9699. static int pt_core_resume_(struct device *dev)
  9700. {
  9701. int rc = 0;
  9702. struct pt_core_data *cd = dev_get_drvdata(dev);
  9703. dev_info(dev, "%s: Entering into resume mode:\n",
  9704. __func__);
  9705. rc = pt_enable_regulator(cd, true);
  9706. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9707. __func__, rc);
  9708. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9709. goto exit;
  9710. /*
  9711. * Bus pm does not call suspend if device runtime suspended
  9712. * This flag is covers that case
  9713. */
  9714. if (cd->irq_disabled) {
  9715. enable_irq(cd->irq);
  9716. cd->irq_disabled = 0;
  9717. }
  9718. if (device_may_wakeup(dev)) {
  9719. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9720. __func__);
  9721. if (cd->irq_wake) {
  9722. disable_irq_wake(cd->irq);
  9723. cd->irq_wake = 0;
  9724. }
  9725. } else {
  9726. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9727. __func__);
  9728. }
  9729. exit:
  9730. rc = pt_core_wake(cd);
  9731. if (rc) {
  9732. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9733. __func__, rc);
  9734. pt_enable_regulator(cd, false);
  9735. return -EAGAIN;
  9736. }
  9737. return rc;
  9738. }
  9739. /*******************************************************************************
  9740. * FUNCTION: pt_core_restore
  9741. *
  9742. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9743. * pt_core_wake. This function may enable IRQ before wake up.
  9744. *
  9745. * RETURN:
  9746. * 0 = success
  9747. * !0 = failure
  9748. *
  9749. * PARAMETERS:
  9750. * *dev - pointer to core device
  9751. ******************************************************************************/
  9752. static int pt_core_restore(struct device *dev)
  9753. {
  9754. int rc = 0;
  9755. struct pt_core_data *cd = dev_get_drvdata(dev);
  9756. dev_info(dev, "%s: Entering into resume mode:\n",
  9757. __func__);
  9758. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9759. return rc;
  9760. }
  9761. /*******************************************************************************
  9762. * FUNCTION: suspend_offload_work
  9763. *
  9764. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9765. * along with kernel power state even the display is off based on the check of
  9766. * TTDL core platform flag.
  9767. *
  9768. * RETURN:
  9769. * 0 = success
  9770. * !0 = failure
  9771. *
  9772. * PARAMETERS:
  9773. * *dev - pointer to core device
  9774. ******************************************************************************/
  9775. static void pt_suspend_offload_work(struct work_struct *work)
  9776. {
  9777. int rc = 0;
  9778. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9779. suspend_offload_work);
  9780. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9781. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9782. return;
  9783. rc = pt_core_suspend_(cd->dev);
  9784. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9785. }
  9786. /*******************************************************************************
  9787. * FUNCTION: resume_offload_work
  9788. *
  9789. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9790. * along with kernel power state even the display is off based on the check of
  9791. * TTDL core platform flag.
  9792. *
  9793. * RETURN:
  9794. * 0 = success
  9795. * !0 = failure
  9796. *
  9797. * PARAMETERS:
  9798. * *dev - pointer to core device
  9799. ******************************************************************************/
  9800. static void pt_resume_offload_work(struct work_struct *work)
  9801. {
  9802. int rc = 0;
  9803. int retry_count = 10;
  9804. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9805. resume_offload_work);
  9806. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9807. do {
  9808. retry_count--;
  9809. rc = pt_core_resume_(pt_data->dev);
  9810. if (rc < 0)
  9811. pt_debug(pt_data->dev, DL_ERROR,
  9812. "%s: Error on wake\n", __func__);
  9813. } while (retry_count && rc < 0);
  9814. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9815. rc = pt_core_easywake_on(pt_data);
  9816. if (rc < 0) {
  9817. pt_debug(pt_data->dev, DL_ERROR,
  9818. "%s: Error on enable touch to wake key\n",
  9819. __func__);
  9820. return;
  9821. }
  9822. pt_data->fb_state = FB_OFF;
  9823. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9824. #endif
  9825. pt_data->quick_boot = false;
  9826. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9827. }
  9828. /*******************************************************************************
  9829. * FUNCTION: pt_core_resume
  9830. *
  9831. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9832. * along with kernel power state even the display is off based on the check of
  9833. * TTDL core platform flag.
  9834. *
  9835. * RETURN:
  9836. * 0 = success
  9837. * !0 = failure
  9838. *
  9839. * PARAMETERS:
  9840. * *dev - pointer to core device
  9841. ******************************************************************************/
  9842. static int pt_core_resume(struct device *dev)
  9843. {
  9844. struct pt_core_data *cd = dev_get_drvdata(dev);
  9845. int rc = 0;
  9846. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9847. return 0;
  9848. if (pm_suspend_via_firmware()) {
  9849. rc = pt_core_restore(cd->dev);
  9850. } else {
  9851. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9852. rc = pt_enable_i2c_regulator(cd, true);
  9853. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9854. }
  9855. mutex_lock(&cd->system_lock);
  9856. cd->wait_until_wake = 1;
  9857. mutex_unlock(&cd->system_lock);
  9858. wake_up(&cd->wait_q);
  9859. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9860. return rc;
  9861. }
  9862. #endif
  9863. #ifdef NEED_SUSPEND_NOTIFIER
  9864. /*******************************************************************************
  9865. * FUNCTION: pt_pm_notifier
  9866. *
  9867. * SUMMARY: This function is registered to notifier chain and will perform
  9868. * suspend operation if match event PM_SUSPEND_PREPARE.
  9869. *
  9870. * RETURN:
  9871. * 0 = success
  9872. * !0 = failure
  9873. *
  9874. * PARAMETERS:
  9875. * *nb - pointer to notifier_block structure
  9876. * action - notifier event type
  9877. * *data - void pointer
  9878. ******************************************************************************/
  9879. static int pt_pm_notifier(struct notifier_block *nb,
  9880. unsigned long action, void *data)
  9881. {
  9882. struct pt_core_data *cd = container_of(nb,
  9883. struct pt_core_data, pm_notifier);
  9884. if (action == PM_SUSPEND_PREPARE) {
  9885. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9886. __func__);
  9887. /*
  9888. * If PM runtime is not suspended, either call runtime
  9889. * PM suspend callback or wait until it finishes
  9890. */
  9891. if (!pm_runtime_suspended(cd->dev))
  9892. pm_runtime_suspend(cd->dev);
  9893. (void) pt_core_suspend(cd->dev);
  9894. }
  9895. return NOTIFY_DONE;
  9896. }
  9897. #endif
  9898. const struct dev_pm_ops pt_pm_ops = {
  9899. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9900. .freeze = pt_core_suspend,
  9901. .restore = pt_core_restore,
  9902. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9903. NULL)
  9904. };
  9905. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9906. /*******************************************************************************
  9907. * FUNCTION: _pt_request_pip2_enter_bl
  9908. *
  9909. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9910. * XRES pin or a soft reset.
  9911. *
  9912. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9913. * the WD active could cause this function to fail!
  9914. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9915. * will not try to determine the current mode but will proceed with
  9916. * resetting the DUT and entering the BL.
  9917. *
  9918. * NOTE: The definition of result code:
  9919. * PT_ENTER_BL_PASS (0)
  9920. * PT_ENTER_BL_ERROR (1)
  9921. * PT_ENTER_BL_RESET_FAIL (2)
  9922. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9923. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9924. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9925. *
  9926. * RETURNS:
  9927. * 0 = success
  9928. * !0 = failure
  9929. *
  9930. *
  9931. * PARAMETERS:
  9932. * *dev - pointer to device structure
  9933. * *start_mode - pointer to the mode the DUT was in when this function
  9934. * starts
  9935. * *result - pointer to store the result when to enter BL
  9936. ******************************************************************************/
  9937. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9938. {
  9939. int rc = 0;
  9940. int t;
  9941. int tmp_result = PT_ENTER_BL_ERROR;
  9942. int flash_info_retry = 2;
  9943. u8 mode = PT_MODE_UNKNOWN;
  9944. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9945. u8 read_buf[32];
  9946. u16 actual_read_len;
  9947. struct pt_core_data *cd = dev_get_drvdata(dev);
  9948. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9949. u8 time = 0;
  9950. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9951. if (cd->watchdog_enabled) {
  9952. pt_debug(dev, DL_WARN,
  9953. "%s: Watchdog must be stopped before entering BL\n",
  9954. __func__);
  9955. goto exit;
  9956. }
  9957. cancel_work_sync(&cd->enum_work);
  9958. cancel_work_sync(&cd->watchdog_work);
  9959. /* if undefined assume operational/test to bypass all checks */
  9960. if (*start_mode == PT_MODE_IGNORE) {
  9961. mode = PT_MODE_OPERATIONAL;
  9962. sys_mode = FW_SYS_MODE_TEST;
  9963. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9964. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9965. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9966. if (rc) {
  9967. pt_debug(dev, DL_ERROR,
  9968. "%s: Get mode failed, mode unknown\n",
  9969. __func__);
  9970. }
  9971. *start_mode = mode;
  9972. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9973. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9974. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9975. sys_mode = FW_SYS_MODE_SCANNING;
  9976. }
  9977. _retry:
  9978. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9979. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9980. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9981. switch (mode) {
  9982. case PT_MODE_UNKNOWN:
  9983. /*
  9984. * When the mode could not be determined the DUT could be
  9985. * in App mode running corrupted FW or FW that is not
  9986. * responding to the mode request, assume no communication
  9987. * and do a hard reset
  9988. */
  9989. mutex_lock(&cd->system_lock);
  9990. cd->startup_status = STARTUP_STATUS_START;
  9991. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9992. mutex_unlock(&cd->system_lock);
  9993. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9994. if (rc) {
  9995. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9996. goto exit;
  9997. }
  9998. break;
  9999. case PT_MODE_OPERATIONAL:
  10000. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10001. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10002. __func__);
  10003. rc = pt_pip_suspend_scanning_(cd);
  10004. if (rc) {
  10005. /*
  10006. * Print to log but don't exit, the FW could be
  10007. * running but be hung or fail to respond to
  10008. * this request
  10009. */
  10010. pt_debug(dev, DL_ERROR,
  10011. "%s Suspend Scan Failed\n", __func__);
  10012. }
  10013. /* sleep to allow the suspend scan to be processed */
  10014. usleep_range(1000, 2000);
  10015. }
  10016. mutex_lock(&cd->system_lock);
  10017. cd->startup_status = STARTUP_STATUS_START;
  10018. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10019. mutex_unlock(&cd->system_lock);
  10020. /* Reset device to enter the BL */
  10021. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10022. if (rc) {
  10023. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10024. goto exit;
  10025. }
  10026. break;
  10027. case PT_MODE_BOOTLOADER:
  10028. /* Do nothing as we are already in the BL */
  10029. tmp_result = PT_ENTER_BL_PASS;
  10030. goto exit;
  10031. default:
  10032. /* Should NEVER get here */
  10033. tmp_result = PT_ENTER_BL_ERROR;
  10034. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10035. goto exit;
  10036. }
  10037. if (!cd->flashless_dut &&
  10038. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10039. /*
  10040. * Sending the special "Host Mode" command will instruct the
  10041. * BL to not execute the FW it has loaded into RAM.
  10042. * The command must be sent within a 40ms window from releasing
  10043. * the XRES pin. If the messages is sent too early it will NAK,
  10044. * so keep sending it every 2ms until it is accepted by the BL.
  10045. * A no-flash DUT does not require this command as there is no
  10046. * FW for the BL to load and execute.
  10047. */
  10048. usleep_range(4000, 6000);
  10049. pt_debug(cd->dev, DL_INFO,
  10050. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10051. __func__, (int)sizeof(host_mode_cmd));
  10052. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10053. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10054. rc = 1;
  10055. while (rc && time < 34) {
  10056. rc = pt_adap_write_read_specific(cd, 4,
  10057. host_mode_cmd, NULL);
  10058. usleep_range(1800, 2000);
  10059. time += 2;
  10060. }
  10061. /* Sleep to allow the BL to come up */
  10062. usleep_range(1000, 2000);
  10063. }
  10064. /*
  10065. * To avoid the case that next PIP command can be confused by BL/FW
  10066. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10067. * followed by "wakeup event" function can lower the failure rate.
  10068. */
  10069. t = wait_event_timeout(cd->wait_q,
  10070. ((cd->startup_status != STARTUP_STATUS_START)
  10071. && (cd->hid_reset_cmd_state == 0)),
  10072. msecs_to_jiffies(300));
  10073. if (IS_TMO(t)) {
  10074. pt_debug(cd->dev, DL_ERROR,
  10075. "%s: TMO waiting for BL sentinel\n", __func__);
  10076. }
  10077. /* Check if device is now in BL mode */
  10078. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10079. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10080. cd->startup_status);
  10081. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10082. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10083. mutex_lock(&cd->system_lock);
  10084. cd->pip2_prot_active = true;
  10085. cd->mode = PT_MODE_BOOTLOADER;
  10086. mutex_unlock(&cd->system_lock);
  10087. tmp_result = PT_ENTER_BL_PASS;
  10088. } else {
  10089. /*
  10090. * If the device doesn't enter BL mode as expected and rc is
  10091. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10092. * the function should return an error code to indicate this
  10093. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10094. */
  10095. if (!rc)
  10096. rc = -EINVAL;
  10097. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10098. mutex_lock(&cd->system_lock);
  10099. cd->mode = mode;
  10100. mutex_unlock(&cd->system_lock);
  10101. pt_debug(dev, DL_ERROR,
  10102. "%s ERROR: Not in BL as expected", __func__);
  10103. }
  10104. exit:
  10105. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10106. /* Check to get (buffered) flash information */
  10107. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10108. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10109. read_buf, &actual_read_len);
  10110. if (!rc) {
  10111. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10112. pt_debug(
  10113. dev, DL_WARN,
  10114. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10115. __func__,
  10116. read_buf[PIP2_RESP_BODY_OFFSET]);
  10117. /*
  10118. * If the BL was unable to cache the correct
  10119. * values when entering the first time due to
  10120. * the Flash part not having been powered up
  10121. * long enough, re-enter the BL to trigger the
  10122. * BL to re-attempt to cache the values.
  10123. */
  10124. if (flash_info_retry-- > 0) {
  10125. mode = PT_MODE_UNKNOWN;
  10126. pt_debug(dev, DL_WARN,
  10127. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10128. __func__, flash_info_retry);
  10129. goto _retry;
  10130. } else {
  10131. pt_debug(dev, DL_WARN,
  10132. "%s Manufacturer ID Unknown\n",
  10133. __func__);
  10134. tmp_result = PT_ENTER_BL_PASS;
  10135. }
  10136. }
  10137. } else {
  10138. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10139. pt_debug(
  10140. dev, DL_ERROR,
  10141. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10142. __func__);
  10143. }
  10144. }
  10145. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10146. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10147. "SUPPRESS");
  10148. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10149. if (result)
  10150. *result = tmp_result;
  10151. return rc;
  10152. }
  10153. /*******************************************************************************
  10154. * FUNCTION: _pt_pip2_file_open
  10155. *
  10156. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10157. *
  10158. * NOTE: The DUT must be in BL mode for this command to work
  10159. *
  10160. * RETURNS:
  10161. * <0 = Error
  10162. * >0 = file handle opened
  10163. *
  10164. * PARAMETERS:
  10165. * *dev - pointer to device structure
  10166. * file_no - PIP2 file number to open
  10167. ******************************************************************************/
  10168. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10169. {
  10170. int ret = 0;
  10171. u16 status;
  10172. u16 actual_read_len;
  10173. u8 file_handle;
  10174. u8 data[2];
  10175. u8 read_buf[10];
  10176. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10177. data[0] = file_no;
  10178. ret = _pt_request_pip2_send_cmd(dev,
  10179. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10180. data, 1, read_buf, &actual_read_len);
  10181. if (ret) {
  10182. pt_debug(dev, DL_ERROR,
  10183. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10184. __func__, file_no);
  10185. return -PIP2_RSP_ERR_NOT_OPEN;
  10186. }
  10187. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10188. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10189. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10190. (file_handle != file_no)) {
  10191. pt_debug(dev, DL_ERROR,
  10192. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10193. __func__, status, file_handle);
  10194. return -status;
  10195. }
  10196. return file_handle;
  10197. }
  10198. /*******************************************************************************
  10199. * FUNCTION: _pt_pip2_file_close
  10200. *
  10201. * SUMMARY: Using the BL PIP2 commands close a file
  10202. *
  10203. * NOTE: The DUT must be in BL mode for this command to work
  10204. *
  10205. * RETURNS:
  10206. * <0 = Error
  10207. * >0 = file handle closed
  10208. *
  10209. * PARAMETERS:
  10210. * *dev - pointer to device structure
  10211. * file_handle - handle to the file to be closed
  10212. ******************************************************************************/
  10213. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10214. {
  10215. int ret = 0;
  10216. u16 status;
  10217. u16 actual_read_len;
  10218. u8 data[2];
  10219. u8 read_buf[32];
  10220. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10221. data[0] = file_handle;
  10222. ret = _pt_request_pip2_send_cmd(dev,
  10223. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10224. data, 1, read_buf, &actual_read_len);
  10225. if (ret) {
  10226. pt_debug(dev, DL_ERROR,
  10227. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10228. __func__, file_handle);
  10229. return -ETIME;
  10230. }
  10231. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10232. if (status != 0x00) {
  10233. pt_debug(dev, DL_ERROR,
  10234. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10235. __func__, status, file_handle);
  10236. return -status;
  10237. }
  10238. return file_handle;
  10239. }
  10240. /*******************************************************************************
  10241. * FUNCTION: _pt_pip2_file_erase
  10242. *
  10243. * SUMMARY: Using the BL PIP2 commands erase a file
  10244. *
  10245. * NOTE: The DUT must be in BL mode for this command to work
  10246. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10247. * one retry is attempted for each sector in a file.
  10248. *
  10249. * RETURNS:
  10250. * <0 = Error
  10251. * >0 = file handle closed
  10252. *
  10253. * PARAMETERS:
  10254. * *dev - pointer to device structure
  10255. * file_handle - handle to the file to be erased
  10256. * *status - PIP2 erase status code
  10257. ******************************************************************************/
  10258. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10259. {
  10260. int ret = 0;
  10261. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10262. int retry = 1;
  10263. u16 actual_read_len;
  10264. u8 data[2];
  10265. u8 read_buf[10];
  10266. struct pt_core_data *cd = dev_get_drvdata(dev);
  10267. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10268. data[0] = file_handle;
  10269. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10270. *status = PIP2_RSP_ERR_TIMEOUT;
  10271. /* Increase waiting time for large file erase */
  10272. mutex_lock(&cd->system_lock);
  10273. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10274. mutex_unlock(&cd->system_lock);
  10275. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10276. ret = _pt_request_pip2_send_cmd(dev,
  10277. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10278. data, 2, read_buf, &actual_read_len);
  10279. if (ret)
  10280. break;
  10281. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10282. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10283. #ifdef TTDL_DIAGNOSTICS
  10284. cd->file_erase_timeout_count++;
  10285. #endif
  10286. pt_debug(dev, DL_WARN,
  10287. "%s: ERASE timeout %d for file = %d\n",
  10288. __func__, retry, file_handle);
  10289. }
  10290. retry++;
  10291. }
  10292. mutex_lock(&cd->system_lock);
  10293. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10294. mutex_unlock(&cd->system_lock);
  10295. if (ret) {
  10296. pt_debug(dev, DL_ERROR,
  10297. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10298. __func__, ret, file_handle);
  10299. return -EIO;
  10300. }
  10301. if (*status != 0x00) {
  10302. pt_debug(dev, DL_ERROR,
  10303. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10304. __func__, *status, file_handle);
  10305. return -EIO;
  10306. }
  10307. return file_handle;
  10308. }
  10309. /*******************************************************************************
  10310. * FUNCTION: _pt_pip2_file_read
  10311. *
  10312. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10313. *
  10314. * NOTE: The DUT must be in BL mode for this command to work
  10315. *
  10316. * RETURNS:
  10317. * <0 = Error
  10318. * >0 = number of bytes read
  10319. *
  10320. * PARAMETERS:
  10321. * *dev - pointer to device structure
  10322. * file_handle - File handle to read from
  10323. * num_bytes - number of bytes to read
  10324. ******************************************************************************/
  10325. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10326. u8 *read_buf)
  10327. {
  10328. int ret = 0;
  10329. u16 status;
  10330. u16 actual_read_len;
  10331. u8 data[3];
  10332. data[0] = file_handle;
  10333. data[1] = (num_bytes & 0x00FF);
  10334. data[2] = (num_bytes & 0xFF00) >> 8;
  10335. ret = _pt_request_pip2_send_cmd(dev,
  10336. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10337. data, 3, read_buf, &actual_read_len);
  10338. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10339. if (ret || ((status != 0x00) && (status != 0x03))) {
  10340. pt_debug(dev, DL_ERROR,
  10341. "%s File open failure with error code = %d\n",
  10342. __func__, status);
  10343. return -EPERM;
  10344. }
  10345. ret = num_bytes;
  10346. return ret;
  10347. }
  10348. /*******************************************************************************
  10349. * FUNCTION: _pt_read_us_file
  10350. *
  10351. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10352. * then read the entire file.
  10353. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10354. *
  10355. * RETURN:
  10356. * 0 = success
  10357. * !0 = failure
  10358. *
  10359. * PARAMETERS:
  10360. * *file_path - pointer to the file path
  10361. * *buf - pointer to the buffer to store the file contents
  10362. * *size - pointer to the size of the file
  10363. ******************************************************************************/
  10364. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10365. {
  10366. struct file *filp = NULL;
  10367. struct inode *inode = NULL;
  10368. unsigned int file_len = 0;
  10369. unsigned int read_len = 0;
  10370. mm_segment_t oldfs;
  10371. int rc = 0;
  10372. if (file_path == NULL || buf == NULL) {
  10373. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10374. return -EINVAL;
  10375. }
  10376. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10377. oldfs = force_uaccess_begin();
  10378. filp = filp_open_block(file_path, O_RDONLY, 0400);
  10379. if (IS_ERR(filp)) {
  10380. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10381. file_path);
  10382. rc = -ENOENT;
  10383. goto err;
  10384. }
  10385. if (filp->f_op == NULL) {
  10386. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10387. __func__);
  10388. rc = -EINVAL;
  10389. goto exit;
  10390. }
  10391. inode = filp->f_path.dentry->d_inode;
  10392. if (inode == NULL) {
  10393. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10394. __func__);
  10395. rc = -EINVAL;
  10396. goto exit;
  10397. }
  10398. file_len = i_size_read(inode->i_mapping->host);
  10399. if (file_len == 0) {
  10400. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10401. __func__, file_len);
  10402. rc = -EINVAL;
  10403. goto exit;
  10404. }
  10405. if (*size == 0)
  10406. read_len = file_len;
  10407. else
  10408. read_len = *size;
  10409. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10410. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10411. __func__, read_len);
  10412. rc = -EINVAL;
  10413. goto exit;
  10414. }
  10415. filp->private_data = inode->i_private;
  10416. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  10417. if (filp->f_op->read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10418. #else
  10419. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10420. #endif
  10421. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10422. rc = -EINVAL;
  10423. goto exit;
  10424. }
  10425. *size = read_len;
  10426. exit:
  10427. if (filp_close(filp, NULL) != 0)
  10428. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10429. err:
  10430. force_uaccess_end(oldfs);
  10431. return rc;
  10432. }
  10433. /*******************************************************************************
  10434. * FUNCTION: _pt_request_pip2_bin_hdr
  10435. *
  10436. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10437. * in the case of a flashless DUT, and parse the contents
  10438. *
  10439. * RETURNS:
  10440. * 0 = Success
  10441. * !0 = Error condition
  10442. *
  10443. * PARAMETERS:
  10444. * *dev - pointer to device structure
  10445. ******************************************************************************/
  10446. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10447. {
  10448. struct pt_core_data *cd = dev_get_drvdata(dev);
  10449. u8 file_handle;
  10450. u8 read_buf[255];
  10451. u8 hdr_len = 0;
  10452. u8 i;
  10453. int bytes_read;
  10454. int read_size;
  10455. int ret = 0;
  10456. int rc = 0;
  10457. bool load_hdr_struct = false;
  10458. if (cd->flashless_dut) {
  10459. read_size = sizeof(read_buf);
  10460. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10461. read_buf, &read_size);
  10462. if (rc) {
  10463. ret = rc;
  10464. pt_debug(dev, DL_ERROR,
  10465. "%s Failed to read fw image from US, rc=%d\n",
  10466. __func__, rc);
  10467. goto exit;
  10468. }
  10469. load_hdr_struct = true;
  10470. hdr_len = read_buf[0];
  10471. i = 0;
  10472. } else {
  10473. if (cd->mode != PT_MODE_BOOTLOADER) {
  10474. ret = -EPERM;
  10475. goto exit;
  10476. }
  10477. /* Open the bin file in Flash */
  10478. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10479. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10480. if (file_handle != PIP2_FW_FILE) {
  10481. ret = -ENOENT;
  10482. pt_debug(dev, DL_ERROR,
  10483. "%s Failed to open bin file\n", __func__);
  10484. goto exit;
  10485. }
  10486. /* Read the header length from the file */
  10487. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10488. read_size = 1;
  10489. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10490. read_buf);
  10491. if (bytes_read != read_size) {
  10492. ret = -EX_ERR_FREAD;
  10493. pt_debug(dev, DL_ERROR,
  10494. "%s Failed to bin file header len\n", __func__);
  10495. goto exit_close_file;
  10496. }
  10497. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10498. if (hdr_len == 0xFF) {
  10499. ret = -EX_ERR_FLEN;
  10500. pt_debug(dev, DL_ERROR,
  10501. "%s Bin header len is invalid\n", __func__);
  10502. goto exit_close_file;
  10503. }
  10504. /* Read the rest of the header from the bin file */
  10505. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10506. memset(read_buf, 0, sizeof(read_buf));
  10507. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10508. read_buf);
  10509. if (bytes_read != hdr_len) {
  10510. ret = -EX_ERR_FREAD;
  10511. pt_debug(dev, DL_ERROR,
  10512. "%s Failed to read bin file\n", __func__);
  10513. goto exit_close_file;
  10514. }
  10515. load_hdr_struct = true;
  10516. exit_close_file:
  10517. /* Close the file */
  10518. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10519. ret = -EX_ERR_FCLOSE;
  10520. pt_debug(dev, DL_ERROR,
  10521. "%s Failed to close bin file\n", __func__);
  10522. }
  10523. /*
  10524. * The length was already read so subtract 1 to make the rest of
  10525. * the offsets match the spec
  10526. */
  10527. i = PIP2_RESP_BODY_OFFSET - 1;
  10528. }
  10529. if (load_hdr_struct) {
  10530. hdr->length = hdr_len;
  10531. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10532. hdr->fw_major = (read_buf[i+3]);
  10533. hdr->fw_minor = (read_buf[i+4]);
  10534. hdr->fw_crc = (read_buf[i+5] << 24) |
  10535. (read_buf[i+6] << 16) |
  10536. (read_buf[i+7] << 8) |
  10537. (read_buf[i+8]);
  10538. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10539. (read_buf[i+10] << 16) |
  10540. (read_buf[i+11] << 8) |
  10541. (read_buf[i+12]);
  10542. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10543. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10544. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10545. if (hdr_len >= 22) {
  10546. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10547. (read_buf[i+20] << 16) |
  10548. (read_buf[i+21] << 8) |
  10549. (read_buf[i+22]);
  10550. } else {
  10551. hdr->hex_file_size = 0;
  10552. }
  10553. }
  10554. exit:
  10555. return ret;
  10556. }
  10557. /*******************************************************************************
  10558. * FUNCTION: _pt_pip2_file_get_stats
  10559. *
  10560. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10561. * opened file
  10562. *
  10563. * NOTE: The DUT must be in BL mode for this command to work
  10564. *
  10565. * RETURNS:
  10566. * !0 = Error
  10567. * 0 = Success
  10568. *
  10569. * PARAMETERS:
  10570. * *dev - pointer to device structure
  10571. * file_handle - File handle to read from
  10572. * *address - pointer to store address of file
  10573. * *file_size _ pointer to store size of file
  10574. ******************************************************************************/
  10575. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10576. u32 *file_size)
  10577. {
  10578. int ret = 1;
  10579. u16 status;
  10580. u16 actual_read_len;
  10581. u8 data[2];
  10582. u8 read_buf[16];
  10583. data[0] = file_handle;
  10584. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10585. ret = _pt_request_pip2_send_cmd(dev,
  10586. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10587. data, 2, read_buf, &actual_read_len);
  10588. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10589. if (ret || (status != 0x00)) {
  10590. pt_debug(dev, DL_ERROR,
  10591. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10592. __func__, status, file_handle, ret);
  10593. ret = -EIO;
  10594. goto exit;
  10595. }
  10596. pt_debug(dev, DL_DEBUG,
  10597. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10598. if (address) {
  10599. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10600. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10601. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10602. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10603. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10604. __func__, *address);
  10605. }
  10606. if (file_size) {
  10607. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10608. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10609. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10610. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10611. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10612. __func__, *file_size);
  10613. }
  10614. exit:
  10615. return ret;
  10616. }
  10617. /*******************************************************************************
  10618. * FUNCTION: _pt_pip2_file_seek_offset
  10619. *
  10620. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10621. * opened file
  10622. *
  10623. * NOTE: The DUT must be in BL mode for this command to work
  10624. * NOTE: File open/erase command can reset the offset
  10625. *
  10626. * RETURNS:
  10627. * !0 = Error
  10628. * 0 = Success
  10629. *
  10630. * PARAMETERS:
  10631. * *dev - pointer to device structure
  10632. * file_handle - File handle to read from
  10633. * read_offset - read offset of file
  10634. * write_offset - write offset of file
  10635. ******************************************************************************/
  10636. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10637. u32 read_offset, u32 write_offset)
  10638. {
  10639. int ret = 1;
  10640. u16 status;
  10641. u16 actual_read_len;
  10642. u8 data[10];
  10643. u8 read_buf[16];
  10644. data[0] = file_handle;
  10645. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10646. data[2] = 0xff & read_offset;
  10647. data[3] = 0xff & (read_offset >> 8);
  10648. data[4] = 0xff & (read_offset >> 16);
  10649. data[5] = 0xff & (read_offset >> 24);
  10650. data[6] = 0xff & write_offset;
  10651. data[7] = 0xff & (write_offset >> 8);
  10652. data[8] = 0xff & (write_offset >> 16);
  10653. data[9] = 0xff & (write_offset >> 24);
  10654. ret = _pt_request_pip2_send_cmd(dev,
  10655. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10656. data, 10, read_buf, &actual_read_len);
  10657. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10658. if (ret || (status != 0x00)) {
  10659. pt_debug(dev, DL_ERROR,
  10660. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10661. __func__, status, ret, file_handle);
  10662. ret = -EIO;
  10663. }
  10664. return ret;
  10665. }
  10666. /*******************************************************************************
  10667. * FUNCTION: _pt_pip2_file_crc
  10668. *
  10669. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10670. * the file.
  10671. *
  10672. * NOTE: The DUT must be in BL mode for this command to work
  10673. * NOTE: This command only can be used for BL version 1.8 or greater.
  10674. * BL version 1.8 added this change according to PGV-173.
  10675. *
  10676. * RETURNS:
  10677. * !0 = Error
  10678. * 0 = Success
  10679. *
  10680. * PARAMETERS:
  10681. * *dev - pointer to device structure
  10682. * file_handle - File handle to read from
  10683. * offset - start offset for CRC calculation
  10684. * length - number of bytes to calculate CRC over
  10685. * read_buf - pointer to the read buffer
  10686. ******************************************************************************/
  10687. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10688. u32 offset, u32 length, u8 *read_buf)
  10689. {
  10690. int rc = 1;
  10691. u16 actual_read_len;
  10692. u8 data[10];
  10693. data[0] = file_handle;
  10694. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10695. data[2] = 0xff & offset;
  10696. data[3] = 0xff & (offset >> 8);
  10697. data[4] = 0xff & (offset >> 16);
  10698. data[5] = 0xff & (offset >> 24);
  10699. data[6] = 0xff & length;
  10700. data[7] = 0xff & (length >> 8);
  10701. data[8] = 0xff & (length >> 16);
  10702. data[9] = 0xff & (length >> 24);
  10703. rc = _pt_request_pip2_send_cmd(dev,
  10704. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10705. data, 10, read_buf, &actual_read_len);
  10706. if (rc)
  10707. pt_debug(dev, DL_ERROR,
  10708. "%s Return FILE_CRC failure, rc = %d\n",
  10709. __func__, rc);
  10710. return rc;
  10711. }
  10712. /*******************************************************************************
  10713. * FUNCTION: pt_pip2_ping_test
  10714. *
  10715. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10716. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10717. * response payload matches what was sent.
  10718. * The max payload size is 247:
  10719. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10720. *
  10721. * RETURN:
  10722. * 0 = success
  10723. * !0 = failure
  10724. *
  10725. * PARAMETERS:
  10726. * *dev - pointer to device structure
  10727. * *attr - pointer to device attributes
  10728. * *buf - pointer to output buffer
  10729. ******************************************************************************/
  10730. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10731. {
  10732. u16 actual_read_len;
  10733. u8 *read_buf = NULL;
  10734. u8 *data = NULL;
  10735. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10736. int i = 1;
  10737. int j = 0;
  10738. int rc = 0;
  10739. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10740. if (!read_buf)
  10741. goto ping_test_exit;
  10742. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10743. if (!data)
  10744. goto ping_test_exit;
  10745. /* Load data payload with an array of walking 1's */
  10746. for (i = 0; i < 255; i++)
  10747. data[i] = 0x01 << (i % 8);
  10748. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10749. for (i = 0; i <= max_bytes; i++) {
  10750. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10751. PIP2_CMD_ID_PING, data, i, read_buf,
  10752. &actual_read_len);
  10753. if (rc) {
  10754. pt_debug(dev, DL_ERROR,
  10755. "%s: PING failed with %d byte payload\n",
  10756. __func__, i);
  10757. break;
  10758. }
  10759. /* Verify data returned matches data sent */
  10760. for (j = 0; j < i; j++) {
  10761. if (read_buf[data_offset + j] != data[j]) {
  10762. pt_debug(dev, DL_DEBUG,
  10763. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10764. __func__, i, j, data[j], j,
  10765. read_buf[data_offset + j]);
  10766. goto ping_test_exit;
  10767. }
  10768. }
  10769. }
  10770. ping_test_exit:
  10771. *last_packet_size = i - 1;
  10772. kfree(read_buf);
  10773. kfree(data);
  10774. return rc;
  10775. }
  10776. /*******************************************************************************
  10777. * FUNCTION: _pt_ic_parse_input_hex
  10778. *
  10779. * SUMMARY: Parse a char data array as space delimited hex values into
  10780. * an int array.
  10781. *
  10782. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10783. * buffer to store data is "u32". This function is still needed by the
  10784. * "command" sysfs node because the buffer type to store data is "u8".
  10785. *
  10786. * RETURN: Length of parsed data
  10787. *
  10788. * PARAMETERS:
  10789. * *dev - pointer to device structure
  10790. * *buf - pointer to buffer that holds the input array to parse
  10791. * buf_size - size of buf
  10792. * *ic_buf - pointer to array to store parsed data
  10793. * ic_buf_size - max size of ic_buf
  10794. ******************************************************************************/
  10795. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10796. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10797. {
  10798. const char *pbuf = buf;
  10799. unsigned long value;
  10800. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10801. u32 i = 0;
  10802. u32 j;
  10803. int last = 0;
  10804. int ret;
  10805. pt_debug(dev, DL_DEBUG,
  10806. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10807. __func__, pbuf, buf, buf_size, "scan buf size",
  10808. PT_MAX_PIP2_MSG_SIZE, buf);
  10809. while (pbuf <= (buf + buf_size)) {
  10810. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10811. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10812. __func__, "Max cmd size exceeded", i,
  10813. PT_MAX_PIP2_MSG_SIZE);
  10814. return -EINVAL;
  10815. }
  10816. if (i >= ic_buf_size) {
  10817. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10818. __func__, "Buffer size exceeded", i,
  10819. ic_buf_size);
  10820. return -EINVAL;
  10821. }
  10822. while (((*pbuf == ' ') || (*pbuf == ','))
  10823. && (pbuf < (buf + buf_size))) {
  10824. last = *pbuf;
  10825. pbuf++;
  10826. }
  10827. if (pbuf >= (buf + buf_size))
  10828. break;
  10829. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10830. if ((last == ',') && (*pbuf == ',')) {
  10831. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10832. __func__, "Invalid data format.");
  10833. return -EINVAL;
  10834. }
  10835. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10836. && (pbuf < (buf + buf_size))
  10837. && (*pbuf != ' ')
  10838. && (*pbuf != ','); j++) {
  10839. last = *pbuf;
  10840. scan_buf[j] = *pbuf++;
  10841. }
  10842. ret = kstrtoul(scan_buf, 16, &value);
  10843. if (ret < 0) {
  10844. pt_debug(dev, DL_ERROR,
  10845. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10846. "Invalid data format. ", scan_buf,
  10847. "Use \"0xHH,...,0xHH\"", " instead.",
  10848. i, ret);
  10849. return ret;
  10850. }
  10851. ic_buf[i] = value;
  10852. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10853. __func__, i, value);
  10854. i++;
  10855. }
  10856. return i;
  10857. }
  10858. /*******************************************************************************
  10859. * FUNCTION: _pt_ic_parse_input
  10860. *
  10861. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10862. * hex values or dec values into an int array with the following rules:
  10863. * 1) Hex values must have a "0x" prefix for each element or the first element
  10864. * only
  10865. * 2) Dec values do not have any prefix
  10866. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10867. * string
  10868. *
  10869. * RETURN: Number of values parsed
  10870. *
  10871. * PARAMETERS:
  10872. * *dev - pointer to device structure
  10873. * *buf - pointer to buffer that holds the input array to parse
  10874. * buf_size - size of buf
  10875. * *out_buf - pointer to array to store parsed data
  10876. * out_buf_size - max size of buffer to be stored
  10877. ******************************************************************************/
  10878. static int _pt_ic_parse_input(struct device *dev,
  10879. const char *buf, size_t buf_size,
  10880. u32 *out_buf, size_t out_buf_size)
  10881. {
  10882. const char *pbuf = buf;
  10883. unsigned long value;
  10884. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10885. u32 i = 0;
  10886. u32 j;
  10887. int last = 0;
  10888. int ret = 0;
  10889. u8 str_base = 0;
  10890. pt_debug(dev, DL_DEBUG,
  10891. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10892. __func__, buf_size, out_buf_size, "scan buf size",
  10893. PT_MAX_PIP2_MSG_SIZE, buf);
  10894. while (pbuf <= (buf + buf_size)) {
  10895. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10896. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10897. __func__, "Max cmd size exceeded", i,
  10898. PT_MAX_PIP2_MSG_SIZE);
  10899. ret = -EINVAL;
  10900. goto error;
  10901. }
  10902. if (i >= out_buf_size) {
  10903. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10904. __func__, "Buffer size exceeded", i,
  10905. out_buf_size);
  10906. ret = -EINVAL;
  10907. goto error;
  10908. }
  10909. while (((*pbuf == ' ') || (*pbuf == ','))
  10910. && (pbuf < (buf + buf_size))) {
  10911. last = *pbuf;
  10912. pbuf++;
  10913. }
  10914. if (pbuf >= (buf + buf_size))
  10915. break;
  10916. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10917. if ((last == ',') && (*pbuf == ',')) {
  10918. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10919. __func__, "Invalid data format.");
  10920. ret = -EINVAL;
  10921. goto error;
  10922. }
  10923. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10924. && (pbuf < (buf + buf_size))
  10925. && (*pbuf != ' ')
  10926. && (*pbuf != ','); j++) {
  10927. last = *pbuf;
  10928. scan_buf[j] = *pbuf++;
  10929. }
  10930. if (i == 0) {
  10931. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10932. (strncmp(scan_buf, "0X", 2) == 0))
  10933. str_base = 16;
  10934. else
  10935. str_base = 10;
  10936. } else {
  10937. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10938. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10939. (str_base == 10)) {
  10940. pt_debug(dev, DL_ERROR,
  10941. "%s: Decimal and Heximal data mixed\n",
  10942. __func__);
  10943. ret = -EINVAL;
  10944. goto error;
  10945. }
  10946. }
  10947. ret = kstrtoul(scan_buf, str_base, &value);
  10948. if (ret < 0) {
  10949. pt_debug(dev, DL_ERROR,
  10950. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10951. "Invalid data format. ", scan_buf,
  10952. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10953. " instead.", i, ret);
  10954. goto error;
  10955. }
  10956. out_buf[i] = value;
  10957. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10958. __func__, i, value, value);
  10959. i++;
  10960. }
  10961. ret = i;
  10962. error:
  10963. return ret;
  10964. }
  10965. #ifdef TTHE_TUNER_SUPPORT
  10966. /*******************************************************************************
  10967. * FUNCTION: tthe_debugfs_open
  10968. *
  10969. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10970. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10971. * printing a large number of fingers and sensor data without overflowing
  10972. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10973. * even multiple of PAGE_SIZE
  10974. *
  10975. * RETURN:
  10976. * 0 = success
  10977. * !0 = failure
  10978. *
  10979. * PARAMETERS:
  10980. * *inode - file inode number
  10981. * *filp - file pointer to debugfs file
  10982. ******************************************************************************/
  10983. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10984. {
  10985. struct pt_core_data *cd = inode->i_private;
  10986. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10987. filp->private_data = inode->i_private;
  10988. if (cd->tthe_buf)
  10989. return -EBUSY;
  10990. while (buf_size < 4096)
  10991. buf_size = buf_size << 1;
  10992. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10993. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10994. cd->tthe_buf_size = buf_size;
  10995. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10996. if (!cd->tthe_buf)
  10997. return -ENOMEM;
  10998. return 0;
  10999. }
  11000. /*******************************************************************************
  11001. * FUNCTION: tthe_debugfs_close
  11002. *
  11003. * SUMMARY: Close method for tthe_tuner debugfs node.
  11004. *
  11005. * RETURN:
  11006. * 0 = success
  11007. * !0 = failure
  11008. *
  11009. * PARAMETERS:
  11010. * *inode - file inode number
  11011. * *filp - file pointer to debugfs file
  11012. ******************************************************************************/
  11013. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11014. {
  11015. struct pt_core_data *cd = filp->private_data;
  11016. filp->private_data = NULL;
  11017. kfree(cd->tthe_buf);
  11018. cd->tthe_buf = NULL;
  11019. return 0;
  11020. }
  11021. /*******************************************************************************
  11022. * FUNCTION: tthe_debugfs_read
  11023. *
  11024. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11025. * tthe_buf to user buffer.
  11026. *
  11027. * RETURN: Size of debugfs data print
  11028. *
  11029. * PARAMETERS:
  11030. * *filp - file pointer to debugfs file
  11031. * *buf - the user space buffer to read to
  11032. * count - the maximum number of bytes to read
  11033. * *ppos - the current position in the buffer
  11034. ******************************************************************************/
  11035. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11036. size_t count, loff_t *ppos)
  11037. {
  11038. struct pt_core_data *cd = filp->private_data;
  11039. int size;
  11040. int ret;
  11041. static int partial_read;
  11042. wait_event_interruptible(cd->wait_q,
  11043. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11044. mutex_lock(&cd->tthe_lock);
  11045. if (cd->tthe_exit) {
  11046. mutex_unlock(&cd->tthe_lock);
  11047. return 0;
  11048. }
  11049. if (count > cd->tthe_buf_len)
  11050. size = cd->tthe_buf_len;
  11051. else
  11052. size = count;
  11053. if (!size) {
  11054. mutex_unlock(&cd->tthe_lock);
  11055. return 0;
  11056. }
  11057. if (partial_read) {
  11058. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11059. partial_read = 0;
  11060. } else {
  11061. ret = copy_to_user(buf, cd->tthe_buf, size);
  11062. }
  11063. if (size == count)
  11064. partial_read = count;
  11065. if (ret == size)
  11066. return -EFAULT;
  11067. size -= ret;
  11068. cd->tthe_buf_len -= size;
  11069. mutex_unlock(&cd->tthe_lock);
  11070. *ppos += size;
  11071. return size;
  11072. }
  11073. static const struct file_operations tthe_debugfs_fops = {
  11074. .open = tthe_debugfs_open,
  11075. .release = tthe_debugfs_close,
  11076. .read = tthe_debugfs_read,
  11077. };
  11078. #endif
  11079. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11080. .start_bl = _pt_request_pip_start_bl,
  11081. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11082. .resume_scanning = _pt_request_pip_resume_scanning,
  11083. .get_param = _pt_request_pip_get_param,
  11084. .set_param = _pt_request_pip_set_param,
  11085. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11086. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11087. .get_data_structure = _pt_request_pip_get_data_structure,
  11088. .run_selftest = _pt_request_pip_run_selftest,
  11089. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11090. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11091. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11092. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11093. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11094. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11095. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11096. .read_data_block = _pt_request_pip_read_data_block,
  11097. .write_data_block = _pt_request_pip_write_data_block,
  11098. .user_cmd = _pt_request_pip_user_cmd,
  11099. .get_bl_info = _pt_request_pip_bl_get_information,
  11100. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11101. .launch_app = _pt_request_pip_launch_app,
  11102. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11103. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11104. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11105. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11106. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11107. .pip2_file_open = _pt_pip2_file_open,
  11108. .pip2_file_close = _pt_pip2_file_close,
  11109. .pip2_file_erase = _pt_pip2_file_erase,
  11110. .read_us_file = _pt_read_us_file,
  11111. .manage_cal_data = _pt_manage_local_cal_data,
  11112. .calc_crc = crc_ccitt_calculate,
  11113. #ifdef TTDL_DIAGNOSTICS
  11114. .pip2_file_read = _pt_pip2_file_read,
  11115. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11116. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11117. .pip2_file_crc = _pt_pip2_file_crc,
  11118. #endif
  11119. };
  11120. static struct pt_core_commands _pt_core_commands = {
  11121. .subscribe_attention = _pt_subscribe_attention,
  11122. .unsubscribe_attention = _pt_unsubscribe_attention,
  11123. .request_exclusive = _pt_request_exclusive,
  11124. .release_exclusive = _pt_release_exclusive,
  11125. .request_reset = _pt_request_reset,
  11126. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11127. .request_enum = _pt_request_enum,
  11128. .request_sysinfo = _pt_request_sysinfo,
  11129. .request_loader_pdata = _pt_request_loader_pdata,
  11130. .request_stop_wd = _pt_request_stop_wd,
  11131. .request_start_wd = _pt_request_start_wd,
  11132. .request_get_mode = _pt_request_get_mode,
  11133. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11134. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11135. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11136. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11137. .request_dut_generation = _pt_request_dut_generation,
  11138. .request_hw_version = _pt_request_hw_version,
  11139. .parse_sysfs_input = _pt_ic_parse_input,
  11140. #ifdef TTHE_TUNER_SUPPORT
  11141. .request_tthe_print = _pt_request_tthe_print,
  11142. #endif
  11143. #ifdef TTDL_DIAGNOSTICS
  11144. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11145. #endif
  11146. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11147. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11148. };
  11149. struct pt_core_commands *pt_get_commands(void)
  11150. {
  11151. return &_pt_core_commands;
  11152. }
  11153. EXPORT_SYMBOL_GPL(pt_get_commands);
  11154. static DEFINE_MUTEX(core_list_lock);
  11155. static LIST_HEAD(core_list);
  11156. static DEFINE_MUTEX(module_list_lock);
  11157. static LIST_HEAD(module_list);
  11158. static int core_number;
  11159. /*******************************************************************************
  11160. * FUNCTION: pt_probe_module
  11161. *
  11162. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11163. *
  11164. * RETURN:
  11165. * 0 = success
  11166. * !0 = failure
  11167. *
  11168. * PARAMETERS:
  11169. * *cd - pointer to core data
  11170. * *module - pointer to module structure
  11171. ******************************************************************************/
  11172. static int pt_probe_module(struct pt_core_data *cd,
  11173. struct pt_module *module)
  11174. {
  11175. struct module_node *module_node;
  11176. int rc = 0;
  11177. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11178. if (!module_node)
  11179. return -ENOMEM;
  11180. module_node->module = module;
  11181. mutex_lock(&cd->module_list_lock);
  11182. list_add(&module_node->node, &cd->module_list);
  11183. mutex_unlock(&cd->module_list_lock);
  11184. rc = module->probe(cd->dev, &module_node->data);
  11185. if (rc) {
  11186. /*
  11187. * Remove from the list when probe fails
  11188. * in order not to call release
  11189. */
  11190. mutex_lock(&cd->module_list_lock);
  11191. list_del(&module_node->node);
  11192. mutex_unlock(&cd->module_list_lock);
  11193. kfree(module_node);
  11194. goto exit;
  11195. }
  11196. exit:
  11197. return rc;
  11198. }
  11199. /*******************************************************************************
  11200. * FUNCTION: pt_release_module
  11201. *
  11202. * SUMMARY: Call the release pointer and remove the module pointer from
  11203. * module_list.
  11204. *
  11205. * PARAMETERS:
  11206. * *cd - pointer to core data
  11207. * *module - pointer to module structure
  11208. ******************************************************************************/
  11209. static void pt_release_module(struct pt_core_data *cd,
  11210. struct pt_module *module)
  11211. {
  11212. struct module_node *m, *m_n;
  11213. mutex_lock(&cd->module_list_lock);
  11214. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11215. if (m->module == module) {
  11216. module->release(cd->dev, m->data);
  11217. list_del(&m->node);
  11218. kfree(m);
  11219. break;
  11220. }
  11221. mutex_unlock(&cd->module_list_lock);
  11222. }
  11223. /*******************************************************************************
  11224. * FUNCTION: pt_probe_modules
  11225. *
  11226. * SUMMARY: Iterate module_list and probe each module.
  11227. *
  11228. * PARAMETERS:
  11229. * *cd - pointer to core data
  11230. ******************************************************************************/
  11231. static void pt_probe_modules(struct pt_core_data *cd)
  11232. {
  11233. struct pt_module *m;
  11234. int rc = 0;
  11235. mutex_lock(&module_list_lock);
  11236. list_for_each_entry(m, &module_list, node) {
  11237. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11238. __func__, m->name);
  11239. rc = pt_probe_module(cd, m);
  11240. if (rc)
  11241. pt_debug(cd->dev, DL_ERROR,
  11242. "%s: Probe fails for module %s\n",
  11243. __func__, m->name);
  11244. }
  11245. mutex_unlock(&module_list_lock);
  11246. }
  11247. /*******************************************************************************
  11248. * FUNCTION: pt_release_modules
  11249. *
  11250. * SUMMARY: Iterate module_list and remove each module.
  11251. *
  11252. * PARAMETERS:
  11253. * *cd - pointer to core data
  11254. ******************************************************************************/
  11255. static void pt_release_modules(struct pt_core_data *cd)
  11256. {
  11257. struct pt_module *m;
  11258. mutex_lock(&module_list_lock);
  11259. list_for_each_entry(m, &module_list, node)
  11260. pt_release_module(cd, m);
  11261. mutex_unlock(&module_list_lock);
  11262. }
  11263. /*******************************************************************************
  11264. * FUNCTION: pt_get_core_data
  11265. *
  11266. * SUMMARY: Iterate core_list and get core data.
  11267. *
  11268. * RETURN:
  11269. * pointer to core data or null pointer if fail
  11270. *
  11271. * PARAMETERS:
  11272. * *id - pointer to core id
  11273. ******************************************************************************/
  11274. struct pt_core_data *pt_get_core_data(char *id)
  11275. {
  11276. struct pt_core_data *d;
  11277. list_for_each_entry(d, &core_list, node)
  11278. if (!strncmp(d->core_id, id, 20))
  11279. return d;
  11280. return NULL;
  11281. }
  11282. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11283. /*******************************************************************************
  11284. * FUNCTION: pt_add_core
  11285. *
  11286. * SUMMARY: Add core data to the core_list.
  11287. *
  11288. * PARAMETERS:
  11289. * *dev - pointer to device structure
  11290. ******************************************************************************/
  11291. static void pt_add_core(struct device *dev)
  11292. {
  11293. struct pt_core_data *d;
  11294. struct pt_core_data *cd = dev_get_drvdata(dev);
  11295. mutex_lock(&core_list_lock);
  11296. list_for_each_entry(d, &core_list, node)
  11297. if (d->dev == dev)
  11298. goto unlock;
  11299. list_add(&cd->node, &core_list);
  11300. unlock:
  11301. mutex_unlock(&core_list_lock);
  11302. }
  11303. /*******************************************************************************
  11304. * FUNCTION: pt_del_core
  11305. *
  11306. * SUMMARY: Remove core data from the core_list.
  11307. *
  11308. * PARAMETERS:
  11309. * *dev - pointer to device structure
  11310. ******************************************************************************/
  11311. static void pt_del_core(struct device *dev)
  11312. {
  11313. struct pt_core_data *d, *d_n;
  11314. mutex_lock(&core_list_lock);
  11315. list_for_each_entry_safe(d, d_n, &core_list, node)
  11316. if (d->dev == dev) {
  11317. list_del(&d->node);
  11318. goto unlock;
  11319. }
  11320. unlock:
  11321. mutex_unlock(&core_list_lock);
  11322. }
  11323. /*******************************************************************************
  11324. * FUNCTION: pt_register_module
  11325. *
  11326. * SUMMARY: Register the module to module_list and probe the module for each
  11327. * core.
  11328. *
  11329. * RETURN:
  11330. * 0 = success
  11331. * !0 = failure
  11332. *
  11333. * PARAMETERS:
  11334. * *module - pointer to module structure
  11335. ******************************************************************************/
  11336. int pt_register_module(struct pt_module *module)
  11337. {
  11338. struct pt_module *m;
  11339. struct pt_core_data *cd;
  11340. int rc = 0;
  11341. if (!module || !module->probe || !module->release)
  11342. return -EINVAL;
  11343. mutex_lock(&module_list_lock);
  11344. list_for_each_entry(m, &module_list, node)
  11345. if (m == module) {
  11346. rc = -EEXIST;
  11347. goto unlock;
  11348. }
  11349. list_add(&module->node, &module_list);
  11350. /* Probe the module for each core */
  11351. mutex_lock(&core_list_lock);
  11352. list_for_each_entry(cd, &core_list, node)
  11353. pt_probe_module(cd, module);
  11354. mutex_unlock(&core_list_lock);
  11355. unlock:
  11356. mutex_unlock(&module_list_lock);
  11357. return rc;
  11358. }
  11359. EXPORT_SYMBOL_GPL(pt_register_module);
  11360. /*******************************************************************************
  11361. * FUNCTION: pt_unregister_module
  11362. *
  11363. * SUMMARY: Release the module for each core and remove the module from
  11364. * module_list.
  11365. *
  11366. * RETURN:
  11367. * 0 = success
  11368. * !0 = failure
  11369. *
  11370. * PARAMETERS:
  11371. * *module - pointer to module structure
  11372. ******************************************************************************/
  11373. void pt_unregister_module(struct pt_module *module)
  11374. {
  11375. struct pt_module *m, *m_n;
  11376. struct pt_core_data *cd;
  11377. if (!module)
  11378. return;
  11379. mutex_lock(&module_list_lock);
  11380. /* Release the module for each core */
  11381. mutex_lock(&core_list_lock);
  11382. list_for_each_entry(cd, &core_list, node)
  11383. pt_release_module(cd, module);
  11384. mutex_unlock(&core_list_lock);
  11385. list_for_each_entry_safe(m, m_n, &module_list, node)
  11386. if (m == module) {
  11387. list_del(&m->node);
  11388. break;
  11389. }
  11390. mutex_unlock(&module_list_lock);
  11391. }
  11392. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11393. /*******************************************************************************
  11394. * FUNCTION: pt_get_module_data
  11395. *
  11396. * SUMMARY: Get module data from module_node by module_list.
  11397. *
  11398. * RETURN:
  11399. * pointer to module data
  11400. *
  11401. * PARAMETERS:
  11402. * *dev - pointer to device structure
  11403. * *module - pointer to module structure
  11404. ******************************************************************************/
  11405. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11406. {
  11407. struct pt_core_data *cd = dev_get_drvdata(dev);
  11408. struct module_node *m;
  11409. void *data = NULL;
  11410. mutex_lock(&cd->module_list_lock);
  11411. list_for_each_entry(m, &cd->module_list, node)
  11412. if (m->module == module) {
  11413. data = m->data;
  11414. break;
  11415. }
  11416. mutex_unlock(&cd->module_list_lock);
  11417. return data;
  11418. }
  11419. EXPORT_SYMBOL(pt_get_module_data);
  11420. #ifdef CONFIG_HAS_EARLYSUSPEND
  11421. /*******************************************************************************
  11422. * FUNCTION: pt_early_suspend
  11423. *
  11424. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11425. * attention list.
  11426. *
  11427. * PARAMETERS:
  11428. * *h - pointer to early_suspend structure
  11429. ******************************************************************************/
  11430. static void pt_early_suspend(struct early_suspend *h)
  11431. {
  11432. struct pt_core_data *cd =
  11433. container_of(h, struct pt_core_data, es);
  11434. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11435. }
  11436. /*******************************************************************************
  11437. * FUNCTION: pt_late_resume
  11438. *
  11439. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11440. * attention list.
  11441. *
  11442. * PARAMETERS:
  11443. * *h - pointer to early_suspend structure
  11444. ******************************************************************************/
  11445. static void pt_late_resume(struct early_suspend *h)
  11446. {
  11447. struct pt_core_data *cd =
  11448. container_of(h, struct pt_core_data, es);
  11449. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11450. }
  11451. /*******************************************************************************
  11452. * FUNCTION: pt_setup_early_suspend
  11453. *
  11454. * SUMMARY: Register early/suspend function to the system.
  11455. *
  11456. * PARAMETERS:
  11457. * *cd - pointer to core data
  11458. ******************************************************************************/
  11459. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11460. {
  11461. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11462. cd->es.suspend = pt_early_suspend;
  11463. cd->es.resume = pt_late_resume;
  11464. register_early_suspend(&cd->es);
  11465. }
  11466. #elif defined(CONFIG_DRM)
  11467. static void pt_resume_work(struct work_struct *work)
  11468. {
  11469. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11470. resume_work);
  11471. int rc = 0;
  11472. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11473. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11474. return;
  11475. rc = pt_core_easywake_off(pt_data);
  11476. if (rc < 0) {
  11477. pt_debug(pt_data->dev, DL_ERROR,
  11478. "%s: Error on wake\n", __func__);
  11479. }
  11480. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11481. return;
  11482. }
  11483. static void pt_suspend_work(struct work_struct *work)
  11484. {
  11485. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11486. suspend_work);
  11487. int rc = 0;
  11488. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11489. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11490. return;
  11491. rc = pt_core_easywake_on(pt_data);
  11492. if (rc < 0) {
  11493. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11494. return;
  11495. }
  11496. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11497. return;
  11498. }
  11499. /*******************************************************************************
  11500. * FUNCTION: drm_notifier_callback
  11501. *
  11502. * SUMMARY: Call back function for DRM notifier to allow to call
  11503. * resume/suspend attention list.
  11504. *
  11505. * RETURN:
  11506. * 0 = success
  11507. *
  11508. * PARAMETERS:
  11509. * *self - pointer to notifier_block structure
  11510. * event - event type of fb notifier
  11511. * *data - pointer to fb_event structure
  11512. ******************************************************************************/
  11513. static int drm_notifier_callback(struct notifier_block *self,
  11514. unsigned long event, void *data)
  11515. {
  11516. struct pt_core_data *cd =
  11517. container_of(self, struct pt_core_data, fb_notifier);
  11518. struct drm_panel_notifier *evdata = data;
  11519. int *blank;
  11520. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11521. if (!evdata)
  11522. goto exit;
  11523. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11524. event == DRM_PANEL_EVENT_BLANK)) {
  11525. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11526. __func__, event);
  11527. goto exit;
  11528. }
  11529. if (cd->quick_boot || cd->drv_debug_suspend)
  11530. goto exit;
  11531. blank = evdata->data;
  11532. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11533. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11534. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11535. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11536. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11537. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11538. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11539. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11540. __func__, event);
  11541. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11542. if (cd->fb_state != FB_ON) {
  11543. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11544. __func__);
  11545. cancel_work_sync(&cd->resume_offload_work);
  11546. cancel_work_sync(&cd->suspend_offload_work);
  11547. cancel_work_sync(&cd->resume_work);
  11548. cancel_work_sync(&cd->suspend_work);
  11549. queue_work(cd->pt_workqueue, &cd->resume_work);
  11550. #if defined(CONFIG_PM_SLEEP)
  11551. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11552. __func__);
  11553. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11554. pt_core_resume_(cd->dev);
  11555. #endif
  11556. cd->fb_state = FB_ON;
  11557. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11558. }
  11559. }
  11560. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11561. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11562. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11563. if (cd->fb_state != FB_OFF) {
  11564. #if defined(CONFIG_PM_SLEEP)
  11565. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11566. __func__);
  11567. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11568. pt_core_suspend_(cd->dev);
  11569. #endif
  11570. cancel_work_sync(&cd->resume_offload_work);
  11571. cancel_work_sync(&cd->suspend_offload_work);
  11572. cancel_work_sync(&cd->resume_work);
  11573. cancel_work_sync(&cd->suspend_work);
  11574. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11575. cd->fb_state = FB_OFF;
  11576. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11577. }
  11578. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11579. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11580. __func__, event);
  11581. }
  11582. } else {
  11583. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11584. __func__, *blank);
  11585. }
  11586. exit:
  11587. return 0;
  11588. }
  11589. /*******************************************************************************
  11590. * FUNCTION: pt_setup_drm_notifier
  11591. *
  11592. * SUMMARY: Set up call back function into drm notifier.
  11593. *
  11594. * PARAMETERS:
  11595. * *cd - pointer to core data
  11596. ******************************************************************************/
  11597. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11598. {
  11599. cd->fb_state = FB_NONE;
  11600. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11601. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11602. if (!active_panel)
  11603. pt_debug(cd->dev, DL_ERROR,
  11604. "%s: Active panel not registered!\n", __func__);
  11605. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11606. if (!cd->pt_workqueue) {
  11607. pt_debug(cd->dev, DL_ERROR,
  11608. "%s: worker thread creation failed !\n", __func__);
  11609. }
  11610. if (cd->pt_workqueue) {
  11611. INIT_WORK(&cd->resume_work, pt_resume_work);
  11612. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11613. }
  11614. if (active_panel &&
  11615. drm_panel_notifier_register(active_panel,
  11616. &cd->fb_notifier) < 0)
  11617. pt_debug(cd->dev, DL_ERROR,
  11618. "%s: Register notifier failed!\n", __func__);
  11619. }
  11620. #elif defined(CONFIG_FB)
  11621. /*******************************************************************************
  11622. * FUNCTION: fb_notifier_callback
  11623. *
  11624. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11625. * resume/suspend attention list.
  11626. *
  11627. * RETURN:
  11628. * 0 = success
  11629. *
  11630. * PARAMETERS:
  11631. * *self - pointer to notifier_block structure
  11632. * event - event type of fb notifier
  11633. * *data - pointer to fb_event structure
  11634. ******************************************************************************/
  11635. static int fb_notifier_callback(struct notifier_block *self,
  11636. unsigned long event, void *data)
  11637. {
  11638. struct pt_core_data *cd =
  11639. container_of(self, struct pt_core_data, fb_notifier);
  11640. struct fb_event *evdata = data;
  11641. int *blank;
  11642. if (event != FB_EVENT_BLANK || !evdata)
  11643. goto exit;
  11644. blank = evdata->data;
  11645. if (*blank == FB_BLANK_UNBLANK) {
  11646. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11647. if (cd->fb_state != FB_ON) {
  11648. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11649. #if defined(CONFIG_PM_SLEEP)
  11650. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11651. pt_core_resume_(cd->dev);
  11652. #endif
  11653. cd->fb_state = FB_ON;
  11654. }
  11655. } else if (*blank == FB_BLANK_POWERDOWN) {
  11656. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11657. if (cd->fb_state != FB_OFF) {
  11658. #if defined(CONFIG_PM_SLEEP)
  11659. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11660. pt_core_suspend_(cd->dev);
  11661. #endif
  11662. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11663. cd->fb_state = FB_OFF;
  11664. }
  11665. }
  11666. exit:
  11667. return 0;
  11668. }
  11669. /*******************************************************************************
  11670. * FUNCTION: pt_setup_fb_notifier
  11671. *
  11672. * SUMMARY: Set up call back function into fb notifier.
  11673. *
  11674. * PARAMETERS:
  11675. * *cd - pointer to core data
  11676. ******************************************************************************/
  11677. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11678. {
  11679. int rc = 0;
  11680. cd->fb_state = FB_ON;
  11681. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11682. rc = fb_register_client(&cd->fb_notifier);
  11683. if (rc)
  11684. pt_debug(cd->dev, DL_ERROR,
  11685. "Unable to register fb_notifier: %d\n", rc);
  11686. }
  11687. #endif
  11688. /*******************************************************************************
  11689. * FUNCTION: pt_watchdog_work
  11690. *
  11691. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11692. * sleeping then this function simply returns. If the DUT is awake the
  11693. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11694. * somehow a response is waiting on the DUT that has not been read. If
  11695. * this occurs the message is simply consumed. If or once the IRQ is
  11696. * cleared, a PIP PING message is sent to the DUT and if the response
  11697. * is received the watchdog succeeds and exits, if no response is seen
  11698. * a startup is queued unless the maximum number of startups have already
  11699. * been attempted, in that case a BL is attempted.
  11700. *
  11701. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11702. * work thread
  11703. *
  11704. * RETURN: void
  11705. *
  11706. * PARAMETERS:
  11707. * *work - pointer to a work structure for the watchdog work queue
  11708. ******************************************************************************/
  11709. static void pt_watchdog_work(struct work_struct *work)
  11710. {
  11711. int rc = 0;
  11712. struct pt_core_data *cd = container_of(work,
  11713. struct pt_core_data, watchdog_work);
  11714. /*
  11715. * if found the current sleep_state is SS_SLEEPING
  11716. * then no need to request_exclusive, directly return
  11717. */
  11718. if (cd->sleep_state == SS_SLEEPING)
  11719. return;
  11720. #ifdef TTDL_DIAGNOSTICS
  11721. cd->watchdog_count++;
  11722. #endif /* TTDL_DIAGNOSTICS */
  11723. /*
  11724. * The first WD interval was extended to allow DDI to come up.
  11725. * If the WD interval is not the default then adjust timer to the
  11726. * current setting. The user can override value with drv_debug sysfs.
  11727. */
  11728. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11729. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11730. msecs_to_jiffies(cd->watchdog_interval));
  11731. }
  11732. if (pt_check_irq_asserted(cd)) {
  11733. #ifdef TTDL_DIAGNOSTICS
  11734. cd->watchdog_irq_stuck_count++;
  11735. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11736. #endif /* TTDL_DIAGNOSTICS */
  11737. pt_debug(cd->dev, DL_ERROR,
  11738. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11739. __func__);
  11740. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11741. }
  11742. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11743. if (rc < 0) {
  11744. pt_debug(cd->dev, DL_ERROR,
  11745. "%s: fail get exclusive ex=%p own=%p\n",
  11746. __func__, cd->exclusive_dev, cd->dev);
  11747. goto queue_startup;
  11748. }
  11749. rc = pt_pip_null_(cd);
  11750. if (release_exclusive(cd, cd->dev) < 0)
  11751. pt_debug(cd->dev, DL_ERROR,
  11752. "%s: fail to release exclusive\n", __func__);
  11753. queue_startup:
  11754. if (rc) {
  11755. #ifdef TTDL_DIAGNOSTICS
  11756. cd->watchdog_failed_access_count++;
  11757. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11758. #endif /* TTDL_DIAGNOSTICS */
  11759. pt_debug(cd->dev, DL_ERROR,
  11760. "%s: failed to access device in WD, retry count=%d\n",
  11761. __func__, cd->startup_retry_count);
  11762. /* Already tried FW upgrade because of watchdog but failed */
  11763. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11764. return;
  11765. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11766. /*
  11767. * Any wrapper function that trys to disable the
  11768. * WD killing this worker cannot be called here.
  11769. */
  11770. rc = request_exclusive(cd, cd->dev,
  11771. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11772. if (rc < 0) {
  11773. pt_debug(cd->dev, DL_ERROR,
  11774. "%s: fail get exclusive ex=%p own=%p\n",
  11775. __func__, cd->exclusive_dev, cd->dev);
  11776. goto exit;
  11777. }
  11778. cd->hw_detected = false;
  11779. cd->startup_status = STARTUP_STATUS_START;
  11780. pt_debug(cd->dev, DL_DEBUG,
  11781. "%s: Startup Status Reset\n", __func__);
  11782. rc = pt_dut_reset_and_wait(cd);
  11783. if (release_exclusive(cd, cd->dev) < 0)
  11784. pt_debug(cd->dev, DL_ERROR,
  11785. "%s: fail to release exclusive\n",
  11786. __func__);
  11787. if (!rc) {
  11788. cd->hw_detected = true;
  11789. if (!cd->flashless_dut)
  11790. pt_queue_enum(cd);
  11791. }
  11792. #ifdef TTDL_DIAGNOSTICS
  11793. cd->wd_xres_count++;
  11794. pt_debug(cd->dev, DL_ERROR,
  11795. "%s: Comm Failed - DUT reset [#%d]\n",
  11796. __func__, cd->wd_xres_count);
  11797. #endif /* TTDL_DIAGNOSTICS */
  11798. } else {
  11799. /*
  11800. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11801. * reset the part to regain communications, try to BL
  11802. */
  11803. pt_debug(cd->dev, DL_ERROR,
  11804. "%s: WD DUT access failure, Start FW Upgrade\n",
  11805. __func__);
  11806. #ifdef TTDL_DIAGNOSTICS
  11807. /*
  11808. * When diagnostics is enabled allow TTDL to keep
  11809. * trying to find the DUT. This allows the DUT to be
  11810. * hot swap-able while the host stays running. In
  11811. * production this may not be wanted as a customer
  11812. * may have several touch drivers and any driver
  11813. * that doesn't match the current DUT should give
  11814. * up trying and give up using the bus.
  11815. */
  11816. pt_debug(cd->dev, DL_INFO,
  11817. "%s: Resetting startup_retry_count\n",
  11818. __func__);
  11819. cd->startup_retry_count = 0;
  11820. #endif /* TTDL_DIAGNOSTICS */
  11821. /*
  11822. * Since fw may be broken,reset sysinfo ready flag
  11823. * to let upgrade function work.
  11824. */
  11825. mutex_lock(&cd->system_lock);
  11826. cd->sysinfo.ready = false;
  11827. mutex_unlock(&cd->system_lock);
  11828. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11829. pt_debug(cd->dev, DL_ERROR,
  11830. "%s: Queue Restart\n", __func__);
  11831. pt_queue_restart(cd);
  11832. } else
  11833. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11834. }
  11835. } else {
  11836. cd->hw_detected = true;
  11837. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11838. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11839. pt_debug(cd->dev, DL_ERROR,
  11840. "%s: HW detected but not enumerated\n",
  11841. __func__);
  11842. pt_queue_enum(cd);
  11843. }
  11844. }
  11845. exit:
  11846. pt_start_wd_timer(cd);
  11847. }
  11848. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11849. /*******************************************************************************
  11850. * FUNCTION: pt_watchdog_timer
  11851. *
  11852. * SUMMARY: The function that is called when the WD timer expires. If the
  11853. * watchdog work is not already busy schedule the watchdog work queue.
  11854. *
  11855. * RETURN: void
  11856. *
  11857. * PARAMETERS:
  11858. * handle - Handle to the watchdog timer
  11859. ******************************************************************************/
  11860. static void pt_watchdog_timer(unsigned long handle)
  11861. {
  11862. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11863. if (!cd)
  11864. return;
  11865. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11866. __func__);
  11867. if (!work_pending(&cd->watchdog_work))
  11868. schedule_work(&cd->watchdog_work);
  11869. }
  11870. #else
  11871. /*******************************************************************************
  11872. * FUNCTION: pt_watchdog_timer
  11873. *
  11874. * SUMMARY: The function that is called when the WD timer expires. If the
  11875. * watchdog work is not already busy schedule the watchdog work queue.
  11876. *
  11877. * RETURN: void
  11878. *
  11879. * PARAMETERS:
  11880. * *t - Pointer to timer list
  11881. ******************************************************************************/
  11882. static void pt_watchdog_timer(struct timer_list *t)
  11883. {
  11884. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11885. if (!cd)
  11886. return;
  11887. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11888. __func__);
  11889. if (!work_pending(&cd->watchdog_work))
  11890. schedule_work(&cd->watchdog_work);
  11891. }
  11892. #endif
  11893. /*******************************************************************************
  11894. * Core sysfs show and store functions
  11895. ******************************************************************************/
  11896. /*******************************************************************************
  11897. * FUNCTION: pt_hw_version_show
  11898. *
  11899. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11900. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11901. *
  11902. * RETURN: size of data written to sysfs node
  11903. *
  11904. * PARAMETERS:
  11905. * *dev - pointer to device structure
  11906. * *attr - pointer to device attributes structure
  11907. * *buf - pointer to print output buffer
  11908. ******************************************************************************/
  11909. static ssize_t pt_hw_version_show(struct device *dev,
  11910. struct device_attribute *attr, char *buf)
  11911. {
  11912. struct pt_core_data *cd = dev_get_drvdata(dev);
  11913. _pt_request_hw_version(dev, cd->hw_version);
  11914. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11915. }
  11916. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11917. /*******************************************************************************
  11918. * FUNCTION: pt_drv_version_show
  11919. *
  11920. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11921. * TTDL version information
  11922. *
  11923. * RETURN: Char buffer with printed TTDL version information
  11924. *
  11925. * PARAMETERS:
  11926. * *dev - pointer to device structure
  11927. * *attr - pointer to device attributes
  11928. * *buf - pointer to output buffer
  11929. ******************************************************************************/
  11930. static ssize_t pt_drv_version_show(struct device *dev,
  11931. struct device_attribute *attr, char *buf)
  11932. {
  11933. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11934. "Driver: %s\nVersion: %s\nDate: %s\n",
  11935. pt_driver_core_name, pt_driver_core_version,
  11936. pt_driver_core_date);
  11937. }
  11938. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11939. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11940. /*******************************************************************************
  11941. * FUNCTION: pt_fw_version_show
  11942. *
  11943. * SUMMARY: Show method for the fw_version sysfs node that will
  11944. * show the firmware, bootloader and PIP version information
  11945. *
  11946. * RETURN: Size of printed buffer
  11947. *
  11948. * PARAMETERS:
  11949. * *dev - pointer to device structure
  11950. * *attr - pointer to device attributes
  11951. * *buf - pointer to output buffer
  11952. ******************************************************************************/
  11953. static ssize_t pt_fw_version_show(struct device *dev,
  11954. struct device_attribute *attr, char *buf)
  11955. {
  11956. struct pt_core_data *cd = dev_get_drvdata(dev);
  11957. struct pt_ttdata *ttdata;
  11958. int rc = 0;
  11959. if (cd->mode == PT_MODE_OPERATIONAL)
  11960. rc = pt_hid_output_get_sysinfo_(cd);
  11961. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11962. __func__, cd->mode, cd->sysinfo.ready);
  11963. if (cd->sysinfo.ready)
  11964. ttdata = &cd->sysinfo.ttdata;
  11965. else
  11966. rc = -ENODATA;
  11967. if (!rc) {
  11968. return scnprintf(buf, strlen(buf),
  11969. "Status: %d\n"
  11970. "FW : %d.%d.%d\n"
  11971. "Config: %d\n"
  11972. "BL : %d.%d\n"
  11973. "PIP : %d.%d\n",
  11974. rc,
  11975. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11976. ttdata->revctrl,
  11977. ttdata->fw_ver_conf,
  11978. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11979. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11980. } else {
  11981. return scnprintf(buf, strlen(buf),
  11982. "Status: %d\n"
  11983. "FW : n/a\n"
  11984. "Config: n/a\n"
  11985. "BL : n/a\n"
  11986. "PIP : n/a\n",
  11987. rc);
  11988. }
  11989. }
  11990. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11991. /*******************************************************************************
  11992. * FUNCTION: pt_sysinfo_show
  11993. *
  11994. * SUMMARY: Show method for the sysinfo sysfs node that will
  11995. * show all the information from get system information command.
  11996. *
  11997. * RETURN: Size of printed buffer
  11998. *
  11999. * PARAMETERS:
  12000. * *dev - pointer to device structure
  12001. * *attr - pointer to device attributes
  12002. * *buf - pointer to output buffer
  12003. ******************************************************************************/
  12004. static ssize_t pt_sysinfo_show(struct device *dev,
  12005. struct device_attribute *attr, char *buf)
  12006. {
  12007. struct pt_core_data *cd = dev_get_drvdata(dev);
  12008. struct pt_sysinfo *si;
  12009. struct pt_ttdata *ttdata = NULL;
  12010. struct pt_sensing_conf_data *scd = NULL;
  12011. int rc = 0;
  12012. if (cd->mode == PT_MODE_OPERATIONAL) {
  12013. rc = pt_hid_output_get_sysinfo_(cd);
  12014. if (cd->sysinfo.ready) {
  12015. si = &cd->sysinfo;
  12016. ttdata = &si->ttdata;
  12017. scd = &si->sensing_conf_data;
  12018. } else
  12019. rc = -ENODATA;
  12020. } else
  12021. rc = -EPERM;
  12022. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12023. __func__, cd->mode, cd->sysinfo.ready);
  12024. if (!rc && ttdata && scd) {
  12025. return scnprintf(buf, strlen(buf),
  12026. "Status: %d\n"
  12027. "pip_ver_major: 0x%02X\n"
  12028. "pip_ver_minor: 0x%02X\n"
  12029. "fw_pid : 0x%04X\n"
  12030. "fw_ver_major : 0x%02X\n"
  12031. "fw_ver_minor : 0x%02X\n"
  12032. "revctrl : 0x%08X\n"
  12033. "fw_ver_conf : 0x%04X\n"
  12034. "bl_ver_major : 0x%02X\n"
  12035. "bl_ver_minor : 0x%02X\n"
  12036. "jtag_id_h : 0x%04X\n"
  12037. "jtag_id_l : 0x%04X\n"
  12038. "mfg_id[0] : 0x%02X\n"
  12039. "mfg_id[1] : 0x%02X\n"
  12040. "mfg_id[2] : 0x%02X\n"
  12041. "mfg_id[3] : 0x%02X\n"
  12042. "mfg_id[4] : 0x%02X\n"
  12043. "mfg_id[5] : 0x%02X\n"
  12044. "mfg_id[6] : 0x%02X\n"
  12045. "mfg_id[7] : 0x%02X\n"
  12046. "post_code : 0x%04X\n"
  12047. "electrodes_x : 0x%02X\n"
  12048. "electrodes_y : 0x%02X\n"
  12049. "len_x : 0x%04X\n"
  12050. "len_y : 0x%04X\n"
  12051. "res_x : 0x%04X\n"
  12052. "res_y : 0x%04X\n"
  12053. "max_z : 0x%04X\n"
  12054. "origin_x : 0x%02X\n"
  12055. "origin_y : 0x%02X\n"
  12056. "panel_id : 0x%02X\n"
  12057. "btn : 0x%02X\n"
  12058. "scan_mode : 0x%02X\n"
  12059. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12060. rc,
  12061. ttdata->pip_ver_major,
  12062. ttdata->pip_ver_minor,
  12063. ttdata->fw_pid,
  12064. ttdata->fw_ver_major,
  12065. ttdata->fw_ver_minor,
  12066. ttdata->revctrl,
  12067. ttdata->fw_ver_conf,
  12068. ttdata->bl_ver_major,
  12069. ttdata->bl_ver_minor,
  12070. ttdata->jtag_id_h,
  12071. ttdata->jtag_id_l,
  12072. ttdata->mfg_id[0],
  12073. ttdata->mfg_id[1],
  12074. ttdata->mfg_id[2],
  12075. ttdata->mfg_id[3],
  12076. ttdata->mfg_id[4],
  12077. ttdata->mfg_id[5],
  12078. ttdata->mfg_id[6],
  12079. ttdata->mfg_id[7],
  12080. ttdata->post_code,
  12081. scd->electrodes_x,
  12082. scd->electrodes_y,
  12083. scd->len_x,
  12084. scd->len_y,
  12085. scd->res_x,
  12086. scd->res_y,
  12087. scd->max_z,
  12088. scd->origin_x,
  12089. scd->origin_y,
  12090. scd->panel_id,
  12091. scd->btn,
  12092. scd->scan_mode,
  12093. scd->max_tch);
  12094. } else {
  12095. return scnprintf(buf, strlen(buf),
  12096. "Status: %d\n",
  12097. rc);
  12098. }
  12099. }
  12100. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12101. /*******************************************************************************
  12102. * FUNCTION: pt_hw_reset_show
  12103. *
  12104. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12105. * by toggling the XRES line and then calls the startup function to
  12106. * allow TTDL to re-enumerate the DUT.
  12107. * The printed value reflects the status of the full reset/enum.
  12108. *
  12109. * PARAMETERS:
  12110. * *dev - pointer to Device structure
  12111. * *attr - pointer to the device attribute structure
  12112. * *buf - pointer to buffer to print
  12113. ******************************************************************************/
  12114. static ssize_t pt_hw_reset_show(struct device *dev,
  12115. struct device_attribute *attr, char *buf)
  12116. {
  12117. struct pt_core_data *cd = dev_get_drvdata(dev);
  12118. int rc = 0;
  12119. int time = 0;
  12120. u8 reset_status = 0;
  12121. int t;
  12122. struct pt_hid_desc hid_desc;
  12123. memset(&hid_desc, 0, sizeof(hid_desc));
  12124. /* Only allow DUT reset if no active BL in progress */
  12125. mutex_lock(&cd->firmware_class_lock);
  12126. mutex_lock(&cd->system_lock);
  12127. cd->startup_state = STARTUP_NONE;
  12128. mutex_unlock(&(cd->system_lock));
  12129. pt_stop_wd_timer(cd);
  12130. /* ensure no left over exclusive access is still locked */
  12131. release_exclusive(cd, cd->dev);
  12132. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12133. if (rc) {
  12134. mutex_unlock(&cd->firmware_class_lock);
  12135. pt_debug(cd->dev, DL_ERROR,
  12136. "%s: HW reset failed rc = %d\n", __func__, rc);
  12137. goto exit_hw_reset;
  12138. }
  12139. reset_status |= 0x01 << 0;
  12140. if (cd->flashless_dut) {
  12141. mutex_unlock(&cd->firmware_class_lock);
  12142. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12143. msecs_to_jiffies(200));
  12144. if (IS_TMO(t)) {
  12145. pt_debug(dev, DL_ERROR,
  12146. "%s: Timeout waiting for FW update",
  12147. __func__);
  12148. rc = -ETIME;
  12149. goto exit_hw_reset;
  12150. } else {
  12151. pt_debug(dev, DL_INFO,
  12152. "%s: ----- Wait FW Loading ----",
  12153. __func__);
  12154. rc = _pt_request_wait_for_enum_state(
  12155. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12156. if (rc) {
  12157. pt_debug(cd->dev, DL_ERROR,
  12158. "%s: No FW Sentinel detected rc = %d\n",
  12159. __func__, rc);
  12160. goto exit_hw_reset;
  12161. }
  12162. reset_status |= 0x01 << 1;
  12163. }
  12164. } else {
  12165. /* Wait for any sentinel */
  12166. rc = _pt_request_wait_for_enum_state(dev, 150,
  12167. STARTUP_STATUS_BL_RESET_SENTINEL |
  12168. STARTUP_STATUS_FW_RESET_SENTINEL);
  12169. if (rc) {
  12170. mutex_unlock(&cd->firmware_class_lock);
  12171. pt_debug(cd->dev, DL_ERROR,
  12172. "%s: No Sentinel detected rc = %d\n",
  12173. __func__, rc);
  12174. goto exit_hw_reset;
  12175. }
  12176. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12177. msleep(30);
  12178. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12179. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12180. if (rc < 0) {
  12181. pt_debug(cd->dev, DL_ERROR,
  12182. "%s: Error on getting HID descriptor r=%d\n",
  12183. __func__, rc);
  12184. goto exit_hw_reset;
  12185. }
  12186. cd->mode = pt_get_mode(cd, &hid_desc);
  12187. if (cd->mode == PT_MODE_BOOTLOADER)
  12188. rc = pt_hid_output_bl_launch_app_(cd);
  12189. } else {
  12190. if (cd->mode == PT_MODE_BOOTLOADER)
  12191. rc = pt_pip2_launch_app(dev,
  12192. PT_CORE_CMD_UNPROTECTED);
  12193. }
  12194. if (rc) {
  12195. mutex_unlock(&cd->firmware_class_lock);
  12196. pt_debug(cd->dev, DL_ERROR,
  12197. "%s: PIP launch app failed rc = %d\n",
  12198. __func__, rc);
  12199. goto exit_hw_reset;
  12200. }
  12201. mutex_unlock(&cd->firmware_class_lock);
  12202. reset_status |= 0x01 << 1;
  12203. msleep(20);
  12204. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12205. (cd->mode != PT_MODE_OPERATIONAL))
  12206. pt_queue_restart(cd);
  12207. else
  12208. pt_queue_enum(cd);
  12209. }
  12210. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12211. msleep(50);
  12212. pt_debug(cd->dev, DL_INFO,
  12213. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12214. __func__, time, STARTUP_STATUS_COMPLETE,
  12215. cd->startup_status);
  12216. time += 50;
  12217. }
  12218. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12219. rc = -ETIME;
  12220. goto exit_hw_reset;
  12221. }
  12222. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12223. __func__, cd->startup_status);
  12224. reset_status |= 0x01 << 2;
  12225. pt_start_wd_timer(cd);
  12226. exit_hw_reset:
  12227. return scnprintf(buf, strlen(buf),
  12228. "Status: %d\n"
  12229. "Reset Status: 0x%02X\n", rc, reset_status);
  12230. }
  12231. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12232. /*******************************************************************************
  12233. * FUNCTION: pt_pip2_cmd_rsp_store
  12234. *
  12235. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12236. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12237. * Command byte order:
  12238. * Byte [0] - PIP2 command ID
  12239. * Byte [1-n] - PIP2 command payload
  12240. *
  12241. * RETURN: Size of passed in buffer
  12242. *
  12243. * PARAMETERS:
  12244. * *dev - pointer to device structure
  12245. * *attr - pointer to device attributes
  12246. * *buf - pointer to buffer that hold the command parameters
  12247. * size - size of buf
  12248. ******************************************************************************/
  12249. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12250. struct device_attribute *attr, const char *buf, size_t size)
  12251. {
  12252. struct pt_core_data *cd = dev_get_drvdata(dev);
  12253. u16 actual_read_len;
  12254. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12255. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12256. u8 pip2_cmd_id = 0x00;
  12257. u8 *pip2_cmd_data = NULL;
  12258. int data_len = 0;
  12259. int length;
  12260. int rc = 0;
  12261. /* clear shared data */
  12262. mutex_lock(&cd->sysfs_lock);
  12263. cd->raw_cmd_status = 0;
  12264. cd->cmd_rsp_buf_len = 0;
  12265. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12266. mutex_unlock(&cd->sysfs_lock);
  12267. length = _pt_ic_parse_input_hex(dev, buf, size,
  12268. input_data, PT_MAX_PIP2_MSG_SIZE);
  12269. if (length <= 0 || length > (PT_MAX_PIP2_MSG_SIZE - 2)) {
  12270. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12271. __func__);
  12272. rc = -EINVAL;
  12273. goto exit;
  12274. }
  12275. /* Send PIP2 command if enough data was provided */
  12276. if (length >= 1) {
  12277. pip2_cmd_id = input_data[0];
  12278. pip2_cmd_data = &input_data[1];
  12279. data_len = length - 1;
  12280. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12281. pip2_cmd_id, pip2_cmd_data, data_len,
  12282. read_buf, &actual_read_len);
  12283. cd->raw_cmd_status = rc;
  12284. if (rc) {
  12285. pt_debug(dev, DL_ERROR,
  12286. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12287. __func__, pip2_cmd_id, rc);
  12288. goto exit;
  12289. } else {
  12290. cd->cmd_rsp_buf_len = actual_read_len;
  12291. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12292. pt_debug(dev, DL_ERROR,
  12293. "%s: PIP2 actual_read_len = %d\n",
  12294. __func__, actual_read_len);
  12295. }
  12296. } else {
  12297. rc = -EINVAL;
  12298. pt_debug(dev, DL_ERROR,
  12299. "%s: Insufficient data provided for PIP2 cmd\n",
  12300. __func__);
  12301. }
  12302. exit:
  12303. if (rc)
  12304. return rc;
  12305. return size;
  12306. }
  12307. /*******************************************************************************
  12308. * FUNCTION: pt_pip2_cmd_rsp_show
  12309. *
  12310. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12311. * response generated after using the store method of the pip2_cmd_rsp
  12312. * sysfs node, are available to be read here.
  12313. *
  12314. * PARAMETERS:
  12315. * *dev - pointer to Device structure
  12316. * *attr - pointer to the device attribute structure
  12317. * *buf - pointer to buffer to print
  12318. ******************************************************************************/
  12319. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12320. struct device_attribute *attr, char *buf)
  12321. {
  12322. struct pt_core_data *cd = dev_get_drvdata(dev);
  12323. int i;
  12324. ssize_t data_len;
  12325. int index;
  12326. mutex_lock(&cd->sysfs_lock);
  12327. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12328. "Status: %d\n", cd->raw_cmd_status);
  12329. if (cd->raw_cmd_status)
  12330. goto error;
  12331. /* Remove the CRC from the length of the response */
  12332. data_len = cd->cmd_rsp_buf_len - 2;
  12333. /* Start printing from the data payload */
  12334. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12335. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12336. "%02X ", cd->cmd_rsp_buf[i]);
  12337. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12338. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12339. "\n(%zd bytes)\n",
  12340. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12341. } else {
  12342. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12343. "\n(%zd bytes)\n", 0);
  12344. }
  12345. error:
  12346. mutex_unlock(&cd->sysfs_lock);
  12347. return index;
  12348. }
  12349. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12350. pt_pip2_cmd_rsp_store);
  12351. /*******************************************************************************
  12352. * FUNCTION: pt_command_store
  12353. *
  12354. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12355. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12356. * TTDL will not parse the command.
  12357. *
  12358. * RETURN: Size of passed in buffer
  12359. *
  12360. * PARAMETERS:
  12361. * *dev - pointer to device structure
  12362. * *attr - pointer to device attributes
  12363. * *buf - pointer to buffer that hold the command parameters
  12364. * size - size of buf
  12365. ******************************************************************************/
  12366. static ssize_t pt_command_store(struct device *dev,
  12367. struct device_attribute *attr, const char *buf, size_t size)
  12368. {
  12369. struct pt_core_data *cd = dev_get_drvdata(dev);
  12370. unsigned short crc;
  12371. u16 actual_read_len;
  12372. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12373. int length;
  12374. int len_field;
  12375. int rc = 0;
  12376. mutex_lock(&cd->sysfs_lock);
  12377. cd->cmd_rsp_buf_len = 0;
  12378. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12379. mutex_unlock(&cd->sysfs_lock);
  12380. length = _pt_ic_parse_input_hex(dev, buf, size,
  12381. input_data, PT_MAX_PIP2_MSG_SIZE);
  12382. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12383. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12384. __func__);
  12385. rc = -EINVAL;
  12386. goto pt_command_store_exit;
  12387. }
  12388. /* PIP2 messages begin with 01 01 */
  12389. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12390. cd->pip2_prot_active = 1;
  12391. /* Override next seq tag with what was sent */
  12392. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12393. /* For PIP2 cmd if length does not include crc, add it */
  12394. len_field = (input_data[3] << 8) | input_data[2];
  12395. if (len_field == length && length <= 254) {
  12396. crc = crc_ccitt_calculate(&input_data[2],
  12397. length - 2);
  12398. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12399. __func__, length, crc);
  12400. input_data[length] = (crc & 0xFF00) >> 8;
  12401. input_data[length + 1] = crc & 0x00FF;
  12402. length = length + 2;
  12403. }
  12404. }
  12405. /* write PIP command to log */
  12406. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12407. pm_runtime_get_sync(dev);
  12408. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12409. length, input_data, &actual_read_len);
  12410. pm_runtime_put(dev);
  12411. mutex_lock(&cd->sysfs_lock);
  12412. cd->raw_cmd_status = rc;
  12413. if (rc) {
  12414. cd->cmd_rsp_buf_len = 0;
  12415. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12416. __func__, buf);
  12417. } else {
  12418. cd->cmd_rsp_buf_len = actual_read_len;
  12419. }
  12420. cd->pip2_prot_active = 0;
  12421. mutex_unlock(&cd->sysfs_lock);
  12422. pt_command_store_exit:
  12423. if (rc)
  12424. return rc;
  12425. return size;
  12426. }
  12427. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12428. /*******************************************************************************
  12429. * FUNCTION: pt_response_show
  12430. *
  12431. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12432. * response generated after using the pt_command_store sysfs node, are
  12433. * available to be read here.
  12434. *
  12435. * PARAMETERS:
  12436. * *dev - pointer to Device structure
  12437. * *attr - pointer to the device attribute structure
  12438. * *buf - pointer to buffer to print
  12439. ******************************************************************************/
  12440. static ssize_t pt_response_show(struct device *dev,
  12441. struct device_attribute *attr, char *buf)
  12442. {
  12443. struct pt_core_data *cd = dev_get_drvdata(dev);
  12444. int i;
  12445. ssize_t num_read;
  12446. int index;
  12447. mutex_lock(&cd->sysfs_lock);
  12448. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12449. "Status: %d\n", cd->raw_cmd_status);
  12450. if (cd->raw_cmd_status)
  12451. goto error;
  12452. num_read = cd->cmd_rsp_buf_len;
  12453. for (i = 0; i < num_read; i++)
  12454. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12455. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12456. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12457. "(%zd bytes)\n", num_read);
  12458. error:
  12459. mutex_unlock(&cd->sysfs_lock);
  12460. return index;
  12461. }
  12462. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12463. /*******************************************************************************
  12464. * FUNCTION: pt_dut_debug_show
  12465. *
  12466. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12467. * are available for the store method.
  12468. *
  12469. * RETURN:
  12470. * 0 = success
  12471. * !0 = failure
  12472. *
  12473. * PARAMETERS:
  12474. * *dev - pointer to device structure
  12475. * *attr - pointer to device attributes
  12476. * *buf - pointer to output buffer
  12477. ******************************************************************************/
  12478. static ssize_t pt_dut_debug_show(struct device *dev,
  12479. struct device_attribute *attr, char *buf)
  12480. {
  12481. ssize_t ret;
  12482. ret = scnprintf(buf, strlen(buf),
  12483. "Status: 0\n"
  12484. "dut_debug sends the following commands to the DUT:\n"
  12485. "%d %s \t- %s\n"
  12486. "%d %s \t- %s\n"
  12487. "%d %s \t- %s\n"
  12488. "%d %s \t- %s\n"
  12489. "%d %s \t- %s\n"
  12490. "%d %s \t- %s\n"
  12491. "%d %s \t- %s\n"
  12492. "%d %s \t- %s\n"
  12493. "%d %s \t- %s\n"
  12494. "%d %s \t- %s\n"
  12495. "%d %s \t- %s\n"
  12496. "%d %s \t- %s\n"
  12497. "%d %s \t- %s\n"
  12498. "%d %s \t- %s\n"
  12499. "%d %s \t- %s\n"
  12500. "%d %s \t- %s\n"
  12501. "%d %s \t- %s\n"
  12502. ,
  12503. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12504. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12505. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12506. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12507. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12508. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12509. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12510. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12511. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12512. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12513. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12514. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12515. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12516. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12517. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12518. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12519. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12520. );
  12521. return ret;
  12522. }
  12523. /*******************************************************************************
  12524. * FUNCTION: pt_drv_debug_show
  12525. *
  12526. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12527. * are available for the store method.
  12528. *
  12529. * RETURN:
  12530. * 0 = success
  12531. * !0 = failure
  12532. *
  12533. * PARAMETERS:
  12534. * *dev - pointer to device structure
  12535. * *attr - pointer to device attributes
  12536. * *buf - pointer to output buffer
  12537. ******************************************************************************/
  12538. static ssize_t pt_drv_debug_show(struct device *dev,
  12539. struct device_attribute *attr, char *buf)
  12540. {
  12541. ssize_t ret;
  12542. ret = scnprintf(buf, strlen(buf),
  12543. "Status: 0\n"
  12544. "drv_debug supports the following values:\n"
  12545. "%d %s \t- %s\n"
  12546. "%d %s \t- %s\n"
  12547. "%d %s \t- %s\n"
  12548. "%d %s \t- %s\n"
  12549. "%d %s \t- %s\n"
  12550. "%d %s \t- %s\n"
  12551. "%d %s \t- %s\n"
  12552. "%d %s \t- %s\n"
  12553. "%d %s \t- %s\n"
  12554. "%d %s - %s\n"
  12555. "%d %s \t- %s\n"
  12556. "%d %s \t- %s\n"
  12557. #ifdef TTDL_DIAGNOSTICS
  12558. "%d %s \t- %s\n"
  12559. "%d %s \t- %s\n"
  12560. "%d %s \t- %s\n"
  12561. #endif /* TTDL_DIAGNOSTICS */
  12562. "%d %s \t- %s\n"
  12563. #ifdef TTDL_DIAGNOSTICS
  12564. "%d %s \t- %s\n"
  12565. "%d %s \t- %s\n"
  12566. "%d %s \t- %s\n"
  12567. "%d %s \t- %s\n"
  12568. "%d %s \t- %s\n"
  12569. "%d %s \t- %s\n"
  12570. "%d %s \t- %s\n"
  12571. "%d %s \t- %s\n"
  12572. "%d %s \t- %s\n"
  12573. #endif /* TTDL_DIAGNOSTICS */
  12574. ,
  12575. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12576. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12577. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12578. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12579. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12580. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12581. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12582. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12583. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12584. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12585. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12586. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12587. #ifdef TTDL_DIAGNOSTICS
  12588. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12589. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12590. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12591. #endif /* TTDL_DIAGNOSTICS */
  12592. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12593. #ifdef TTDL_DIAGNOSTICS
  12594. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12595. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12596. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12597. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12598. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12599. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12600. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12601. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12602. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12603. "TTHE_TUNER HID USB Format"
  12604. #endif /* TTDL_DIAGNOSTICS */
  12605. );
  12606. return ret;
  12607. }
  12608. /*******************************************************************************
  12609. * FUNCTION: pt_drv_debug_store
  12610. *
  12611. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12612. * dut_debug. Drv_debug will contain all functionality that can be run
  12613. * without a DUT preset and is available anytime TTDL is running.
  12614. * Dut_debug requires a DUT to be available and will only be created after
  12615. * a DUT has been detected.
  12616. * This function will eventually be split into two but until the overlap
  12617. * has been depricated this function contains all commands that can be
  12618. * used for TTDL/DUT debugging status and control.
  12619. * All commands require at least one value to be passed in *buf with some
  12620. * requiring two.
  12621. *
  12622. * RETURN: Size of passed in buffer
  12623. *
  12624. * PARAMETERS:
  12625. * *dev - pointer to device structure
  12626. * *attr - pointer to device attributes
  12627. * *buf - pointer to buffer that hold the command parameters
  12628. * size - size of buf
  12629. ******************************************************************************/
  12630. static ssize_t pt_drv_debug_store(struct device *dev,
  12631. struct device_attribute *attr, const char *buf, size_t size)
  12632. {
  12633. struct pt_core_data *cd = dev_get_drvdata(dev);
  12634. unsigned long value;
  12635. int rc = 0;
  12636. u8 return_data[8];
  12637. static u8 wd_disabled;
  12638. u32 input_data[3];
  12639. int length;
  12640. #ifdef TTDL_DIAGNOSTICS
  12641. struct i2c_client *client = to_i2c_client(dev);
  12642. unsigned short crc = 0;
  12643. u16 cal_size;
  12644. #endif
  12645. input_data[0] = 0;
  12646. input_data[1] = 0;
  12647. /* Maximmum input is two elements */
  12648. length = _pt_ic_parse_input(dev, buf, size,
  12649. input_data, ARRAY_SIZE(input_data));
  12650. if (length < 1 || length > 2) {
  12651. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12652. __func__);
  12653. rc = -EINVAL;
  12654. goto pt_drv_debug_store_exit;
  12655. }
  12656. value = input_data[0];
  12657. if (length == 1) {
  12658. pt_debug(dev, DL_DEBUG,
  12659. "%s: Debug Cmd Received (id=%d)\n",
  12660. __func__, input_data[0]);
  12661. } else if (length == 2) {
  12662. pt_debug(dev, DL_DEBUG,
  12663. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12664. __func__, input_data[0], input_data[1]);
  12665. } else {
  12666. pt_debug(dev, DL_DEBUG,
  12667. "%s: Invalid arguments received\n", __func__);
  12668. rc = -EINVAL;
  12669. goto pt_drv_debug_store_exit;
  12670. }
  12671. /* Start watchdog timer command */
  12672. if (value == PT_DRV_DBG_START_WD) {
  12673. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12674. wd_disabled = 0;
  12675. cd->watchdog_force_stop = false;
  12676. pt_start_wd_timer(cd);
  12677. goto pt_drv_debug_store_exit;
  12678. }
  12679. /* Stop watchdog timer temporarily */
  12680. pt_stop_wd_timer(cd);
  12681. if (value == PT_DRV_DBG_STOP_WD) {
  12682. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12683. wd_disabled = 1;
  12684. cd->watchdog_force_stop = true;
  12685. goto pt_drv_debug_store_exit;
  12686. }
  12687. switch (value) {
  12688. case PT_DRV_DBG_SUSPEND: /* 4 */
  12689. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12690. wd_disabled = 1;
  12691. rc = pt_core_suspend_(cd->dev);
  12692. if (rc)
  12693. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12694. __func__, rc);
  12695. else {
  12696. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12697. __func__);
  12698. cd->drv_debug_suspend = true;
  12699. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12700. }
  12701. break;
  12702. case PT_DRV_DBG_RESUME: /* 5 */
  12703. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12704. rc = pt_core_resume_(cd->dev);
  12705. if (rc)
  12706. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12707. __func__, rc);
  12708. else {
  12709. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12710. __func__);
  12711. cd->drv_debug_suspend = false;
  12712. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12713. }
  12714. break;
  12715. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12716. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12717. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12718. break;
  12719. case PT_DUT_DBG_HID_RESET: /* 50 */
  12720. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12721. pt_hid_cmd_reset(cd);
  12722. break;
  12723. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12724. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12725. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12726. wd_disabled = 0;
  12727. break;
  12728. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12729. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12730. __func__);
  12731. wd_disabled = 1;
  12732. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12733. break;
  12734. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12735. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12736. __func__);
  12737. wd_disabled = 1;
  12738. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12739. break;
  12740. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12741. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12742. pt_hid_output_bl_get_information(cd, return_data);
  12743. break;
  12744. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12745. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12746. __func__);
  12747. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12748. break;
  12749. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12750. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12751. pt_hid_output_bl_launch_app(cd);
  12752. break;
  12753. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12754. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12755. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12756. break;
  12757. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12758. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12759. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12760. break;
  12761. case PT_DUT_DBG_RESET: /* 98 */
  12762. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12763. rc = pt_hw_hard_reset(cd);
  12764. break;
  12765. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12766. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12767. pt_pip_null(cd);
  12768. break;
  12769. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12770. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12771. rc = pt_pip_start_bootloader(cd);
  12772. if (!rc) {
  12773. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12774. cd->mode = PT_MODE_BOOTLOADER;
  12775. }
  12776. break;
  12777. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12778. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12779. pt_hid_output_get_sysinfo(cd);
  12780. break;
  12781. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12782. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12783. pt_pip_suspend_scanning(cd);
  12784. break;
  12785. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12786. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12787. pt_pip_resume_scanning(cd);
  12788. break;
  12789. #ifdef TTHE_TUNER_SUPPORT
  12790. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12791. cd->tthe_exit = 1;
  12792. wake_up(&cd->wait_q);
  12793. kfree(cd->tthe_buf);
  12794. cd->tthe_buf = NULL;
  12795. cd->tthe_exit = 0;
  12796. break;
  12797. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12798. if (cd->tthe_buf)
  12799. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12800. else
  12801. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12802. __func__);
  12803. break;
  12804. #endif
  12805. #ifdef TTDL_DIAGNOSTICS
  12806. case PT_DUT_DBG_HID_DESC: /* 109 */
  12807. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12808. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12809. break;
  12810. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12811. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12812. __func__);
  12813. pt_erase_parameter_list(cd);
  12814. break;
  12815. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12816. rc = pt_read_input(cd);
  12817. if (!rc)
  12818. pt_parse_input(cd);
  12819. break;
  12820. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12821. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12822. &cal_size, &crc);
  12823. if (rc)
  12824. pt_debug(dev, DL_ERROR,
  12825. "%s: CAL Data clear failed rc=%d\n",
  12826. __func__, rc);
  12827. else
  12828. pt_debug(dev, DL_INFO,
  12829. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12830. __func__, crc, size);
  12831. break;
  12832. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12833. mutex_lock(&cd->system_lock);
  12834. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12835. cd->debug_level = input_data[1];
  12836. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12837. __func__, cd->debug_level);
  12838. } else {
  12839. rc = -EINVAL;
  12840. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12841. __func__, input_data[1]);
  12842. }
  12843. mutex_unlock(&(cd->system_lock));
  12844. break;
  12845. #endif
  12846. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12847. mutex_lock(&cd->system_lock);
  12848. if (input_data[1] > 100) {
  12849. cd->watchdog_interval = input_data[1];
  12850. pt_debug(dev, DL_INFO,
  12851. "%s: Set watchdog_ interval to: %d\n",
  12852. __func__, cd->watchdog_interval);
  12853. pt_start_wd_timer(cd);
  12854. } else {
  12855. rc = -EINVAL;
  12856. pt_debug(dev, DL_ERROR,
  12857. "%s: Invalid watchdog interval: %d\n",
  12858. __func__, input_data[1]);
  12859. }
  12860. mutex_unlock(&(cd->system_lock));
  12861. break;
  12862. #ifdef TTDL_DIAGNOSTICS
  12863. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12864. mutex_lock(&cd->system_lock);
  12865. if (input_data[1] == 0) {
  12866. cd->show_timestamp = 0;
  12867. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12868. __func__);
  12869. } else if (input_data[1] == 1) {
  12870. cd->show_timestamp = 1;
  12871. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12872. __func__);
  12873. } else {
  12874. rc = -EINVAL;
  12875. pt_debug(dev, DL_ERROR,
  12876. "%s: Invalid parameter: %d\n",
  12877. __func__, input_data[1]);
  12878. }
  12879. mutex_unlock(&(cd->system_lock));
  12880. break;
  12881. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12882. if (input_data[1] == 0) {
  12883. cd->cpdata->setup_power(cd->cpdata,
  12884. PT_MT_POWER_OFF, cd->dev);
  12885. pt_debug(dev, DL_INFO,
  12886. "%s: Initiate Power Off\n", __func__);
  12887. } else if (input_data[1] == 1) {
  12888. cd->cpdata->setup_power(cd->cpdata,
  12889. PT_MT_POWER_ON, cd->dev);
  12890. pt_debug(dev, DL_INFO,
  12891. "%s: Initiate Power On\n", __func__);
  12892. } else {
  12893. rc = -EINVAL;
  12894. pt_debug(dev, DL_ERROR,
  12895. "%s: Invalid parameter: %d\n",
  12896. __func__, input_data[1]);
  12897. }
  12898. break;
  12899. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12900. if (input_data[1] == 0) {
  12901. pm_runtime_put(dev);
  12902. pt_debug(dev, DL_ERROR,
  12903. "%s: Force call pm_runtime_put()\n", __func__);
  12904. } else if (input_data[1] == 1) {
  12905. pm_runtime_get_sync(dev);
  12906. pt_debug(dev, DL_ERROR,
  12907. "%s: Force call pm_runtime_get_sync()\n",
  12908. __func__);
  12909. } else {
  12910. rc = -EINVAL;
  12911. pt_debug(dev, DL_ERROR,
  12912. "%s: WARNING: Invalid parameter: %d\n",
  12913. __func__, input_data[1]);
  12914. }
  12915. break;
  12916. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12917. mutex_lock(&cd->system_lock);
  12918. if (input_data[1] == 0) {
  12919. cd->show_tt_data = false;
  12920. pt_debug(dev, DL_INFO,
  12921. "%s: Disable TT_DATA\n", __func__);
  12922. } else if (input_data[1] == 1) {
  12923. cd->show_tt_data = true;
  12924. pt_debug(dev, DL_INFO,
  12925. "%s: Enable TT_DATA\n", __func__);
  12926. } else {
  12927. rc = -EINVAL;
  12928. pt_debug(dev, DL_ERROR,
  12929. "%s: Invalid parameter: %d\n",
  12930. __func__, input_data[1]);
  12931. }
  12932. mutex_unlock(&(cd->system_lock));
  12933. break;
  12934. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12935. if (input_data[1] == cd->active_dut_generation) {
  12936. mutex_lock(&cd->system_lock);
  12937. cd->set_dut_generation = true;
  12938. mutex_unlock(&(cd->system_lock));
  12939. } else {
  12940. mutex_lock(&cd->system_lock);
  12941. if (input_data[1] == 0) {
  12942. cd->active_dut_generation = DUT_UNKNOWN;
  12943. cd->set_dut_generation = false;
  12944. } else if (input_data[1] == 1) {
  12945. cd->active_dut_generation = DUT_PIP1_ONLY;
  12946. cd->set_dut_generation = true;
  12947. } else if (input_data[1] == 2) {
  12948. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12949. cd->set_dut_generation = true;
  12950. } else {
  12951. rc = -EINVAL;
  12952. pt_debug(dev, DL_ERROR,
  12953. "%s: Invalid parameter: %d\n",
  12954. __func__, input_data[1]);
  12955. break;
  12956. }
  12957. cd->startup_status = STARTUP_STATUS_START;
  12958. pt_debug(cd->dev, DL_DEBUG,
  12959. "%s: Startup Status Reset\n", __func__);
  12960. mutex_unlock(&(cd->system_lock));
  12961. pt_debug(dev, DL_INFO,
  12962. "%s: Active DUT Generation Set to: %d\n",
  12963. __func__, cd->active_dut_generation);
  12964. /* Changing DUT generations full restart needed */
  12965. pt_queue_restart(cd);
  12966. }
  12967. break;
  12968. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12969. mutex_lock(&cd->system_lock);
  12970. if (input_data[1] == 0) {
  12971. cd->bridge_mode = false;
  12972. pt_debug(dev, DL_INFO,
  12973. "%s: Disable Bridge Mode\n", __func__);
  12974. } else if (input_data[1] == 1) {
  12975. cd->bridge_mode = true;
  12976. pt_debug(dev, DL_INFO,
  12977. "%s: Enable Bridge Mode\n", __func__);
  12978. } else {
  12979. rc = -EINVAL;
  12980. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12981. __func__, input_data[1]);
  12982. }
  12983. mutex_unlock(&(cd->system_lock));
  12984. break;
  12985. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12986. mutex_lock(&cd->system_lock);
  12987. /* Only a 7bit address is valid */
  12988. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12989. client->addr = input_data[1];
  12990. pt_debug(dev, DL_INFO,
  12991. "%s: Set I2C Address: 0x%2X\n",
  12992. __func__, client->addr);
  12993. } else {
  12994. rc = -EINVAL;
  12995. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12996. __func__, input_data[1]);
  12997. client->addr = 0x24;
  12998. }
  12999. mutex_unlock(&(cd->system_lock));
  13000. break;
  13001. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  13002. mutex_lock(&cd->system_lock);
  13003. if (input_data[1] == 0) {
  13004. cd->flashless_dut = 0;
  13005. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  13006. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  13007. __func__);
  13008. } else if (input_data[1] == 1) {
  13009. cd->flashless_dut = 1;
  13010. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  13011. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13012. __func__);
  13013. }
  13014. mutex_unlock(&(cd->system_lock));
  13015. break;
  13016. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13017. mutex_lock(&cd->system_lock);
  13018. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13019. cd->force_pip2_seq = input_data[1];
  13020. cd->pip2_cmd_tag_seq = input_data[1];
  13021. pt_debug(dev, DL_INFO,
  13022. "%s: Force PIP2 Seq to: 0x%02X\n",
  13023. __func__, input_data[1]);
  13024. } else {
  13025. cd->force_pip2_seq = 0;
  13026. pt_debug(dev, DL_INFO,
  13027. "%s: Clear Forced PIP2 Seq\n", __func__);
  13028. }
  13029. mutex_unlock(&(cd->system_lock));
  13030. break;
  13031. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13032. mutex_lock(&cd->system_lock);
  13033. if (input_data[1] == 0) {
  13034. cd->bl_with_no_int = 0;
  13035. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13036. } else if (input_data[1] == 1) {
  13037. cd->bl_with_no_int = 1;
  13038. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13039. __func__);
  13040. } else {
  13041. rc = -EINVAL;
  13042. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13043. __func__, input_data[1]);
  13044. }
  13045. mutex_unlock(&(cd->system_lock));
  13046. break;
  13047. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13048. mutex_lock(&cd->system_lock);
  13049. if (input_data[1] == 0) {
  13050. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13051. pt_debug(dev, DL_INFO,
  13052. "%s: Disable Calibration cache in host\n",
  13053. __func__);
  13054. } else if (input_data[1] == 1) {
  13055. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13056. pt_debug(dev, DL_INFO,
  13057. "%s: Enable Calibration cache in host\n",
  13058. __func__);
  13059. } else {
  13060. rc = -EINVAL;
  13061. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13062. __func__, input_data[1]);
  13063. }
  13064. mutex_unlock(&(cd->system_lock));
  13065. break;
  13066. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13067. mutex_lock(&cd->system_lock);
  13068. if (input_data[1] == 0) {
  13069. cd->multi_chip = PT_FEATURE_DISABLE;
  13070. cd->ttdl_bist_select = 0x07;
  13071. pt_debug(dev, DL_INFO,
  13072. "%s: Disable Multi-chip support\n", __func__);
  13073. } else if (input_data[1] == 1) {
  13074. cd->multi_chip = PT_FEATURE_ENABLE;
  13075. cd->ttdl_bist_select = 0x3F;
  13076. pt_debug(dev, DL_INFO,
  13077. "%s: Enable Multi-chip support\n", __func__);
  13078. } else {
  13079. rc = -EINVAL;
  13080. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13081. __func__, input_data[1]);
  13082. }
  13083. mutex_unlock(&(cd->system_lock));
  13084. break;
  13085. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13086. mutex_lock(&cd->system_lock);
  13087. if (input_data[1] <= 0x07) {
  13088. cd->panel_id_support = input_data[1];
  13089. pt_debug(dev, DL_INFO,
  13090. "%s: Set panel_id_support to %d\n",
  13091. __func__, cd->panel_id_support);
  13092. } else {
  13093. rc = -EINVAL;
  13094. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13095. __func__, input_data[1]);
  13096. }
  13097. mutex_unlock(&(cd->system_lock));
  13098. break;
  13099. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13100. mutex_lock(&cd->system_lock);
  13101. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13102. /*
  13103. * The timeout is changed for some cases so the
  13104. * pip_cmd_timeout_default is used to retore back to
  13105. * what the user requested as the new timeout.
  13106. */
  13107. cd->pip_cmd_timeout_default = input_data[1];
  13108. cd->pip_cmd_timeout = input_data[1];
  13109. pt_debug(dev, DL_INFO,
  13110. "%s: PIP Timeout = %d\n", __func__,
  13111. cd->pip_cmd_timeout_default);
  13112. } else {
  13113. rc = -EINVAL;
  13114. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13115. __func__, input_data[1]);
  13116. }
  13117. mutex_unlock(&(cd->system_lock));
  13118. break;
  13119. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13120. mutex_lock(&cd->system_lock);
  13121. if (input_data[1] == 0) {
  13122. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13123. pt_debug(dev, DL_INFO,
  13124. "%s: Disable tthe_tuner HID-USB format\n",
  13125. __func__);
  13126. } else if (input_data[1] == 1) {
  13127. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13128. pt_debug(dev, DL_INFO,
  13129. "%s: Enable tthe_tuner HID-USB format\n",
  13130. __func__);
  13131. } else {
  13132. rc = -EINVAL;
  13133. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13134. __func__, input_data[1]);
  13135. }
  13136. mutex_unlock(&(cd->system_lock));
  13137. break;
  13138. #endif /* TTDL_DIAGNOSTICS */
  13139. default:
  13140. rc = -EINVAL;
  13141. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13142. }
  13143. /* Enable watchdog timer */
  13144. if (!wd_disabled)
  13145. pt_start_wd_timer(cd);
  13146. pt_drv_debug_store_exit:
  13147. if (rc)
  13148. return rc;
  13149. return size;
  13150. }
  13151. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13152. pt_drv_debug_store);
  13153. /*******************************************************************************
  13154. * FUNCTION: pt_sleep_status_show
  13155. *
  13156. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13157. * sleep status as either ON or OFF
  13158. *
  13159. * RETURN:
  13160. * 0 = success
  13161. * !0 = failure
  13162. *
  13163. * PARAMETERS:
  13164. * *dev - pointer to device structure
  13165. * *attr - pointer to device attributes
  13166. * *buf - pointer to output buffer
  13167. ******************************************************************************/
  13168. static ssize_t pt_sleep_status_show(struct device *dev,
  13169. struct device_attribute *attr, char *buf)
  13170. {
  13171. struct pt_core_data *cd = dev_get_drvdata(dev);
  13172. ssize_t ret;
  13173. mutex_lock(&cd->system_lock);
  13174. if (cd->sleep_state == SS_SLEEP_ON)
  13175. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13176. else
  13177. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13178. mutex_unlock(&cd->system_lock);
  13179. return ret;
  13180. }
  13181. /*******************************************************************************
  13182. * FUNCTION: pt_panel_id_show
  13183. *
  13184. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13185. * detected panel ID from the DUT
  13186. *
  13187. * RETURN:
  13188. * 0 = success
  13189. * !0 = failure
  13190. *
  13191. * PARAMETERS:
  13192. * *dev - pointer to device structure
  13193. * *attr - pointer to device attributes
  13194. * *buf - pointer to output buffer
  13195. ******************************************************************************/
  13196. static ssize_t pt_panel_id_show(struct device *dev,
  13197. struct device_attribute *attr, char *buf)
  13198. {
  13199. struct pt_core_data *cd = dev_get_drvdata(dev);
  13200. ssize_t ret;
  13201. u8 pid = PANEL_ID_NOT_ENABLED;
  13202. int rc = 0;
  13203. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13204. /*
  13205. * The DUT should report the same panel ID from both the BL and
  13206. * the FW unless the panel_id feature is set to only
  13207. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13208. * to retrieve the panel_id.
  13209. */
  13210. if (cd->mode == PT_MODE_BOOTLOADER) {
  13211. /*
  13212. * Return the stored value if PT_PANEL_ID_BY_BL
  13213. * is not supported while other feature exits.
  13214. */
  13215. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13216. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13217. if (rc) {
  13218. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13219. "Failed to retrieve Panel ID. ",
  13220. "Using cached value\n",
  13221. __func__);
  13222. }
  13223. }
  13224. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13225. if (cd->panel_id_support &
  13226. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13227. /* For all systems sysinfo has the panel_id */
  13228. rc = pt_hid_output_get_sysinfo(cd);
  13229. if (!rc)
  13230. pid =
  13231. cd->sysinfo.sensing_conf_data.panel_id;
  13232. pt_debug(dev, DL_ERROR,
  13233. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13234. __func__, rc, pid);
  13235. }
  13236. } else {
  13237. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13238. __func__);
  13239. rc = -EPERM;
  13240. }
  13241. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13242. if (cd->mode == PT_MODE_BOOTLOADER) {
  13243. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13244. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13245. if (rc) {
  13246. pt_debug(dev, DL_ERROR,
  13247. "%s: BL get panel ID failed rc=%d\n",
  13248. __func__, rc);
  13249. }
  13250. }
  13251. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13252. if (cd->panel_id_support &
  13253. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13254. rc = pt_hid_output_get_sysinfo(cd);
  13255. if (!rc)
  13256. pid =
  13257. cd->sysinfo.sensing_conf_data.panel_id;
  13258. pt_debug(dev, DL_ERROR,
  13259. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13260. __func__, rc, pid);
  13261. }
  13262. } else {
  13263. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13264. __func__);
  13265. rc = -EPERM;
  13266. }
  13267. } else {
  13268. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13269. __func__);
  13270. rc = -EPERM;
  13271. }
  13272. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13273. rc, pid);
  13274. return ret;
  13275. }
  13276. /*******************************************************************************
  13277. * FUNCTION: pt_get_param_store
  13278. *
  13279. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13280. * ID to retrieve with the show method.
  13281. *
  13282. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13283. *
  13284. * RETURN: Size of passed in buffer if successful
  13285. *
  13286. * PARAMETERS:
  13287. * *dev - pointer to device structure
  13288. * *attr - pointer to device attributes
  13289. * *buf - pointer to output buffer
  13290. * size - size of buf
  13291. ******************************************************************************/
  13292. static ssize_t pt_get_param_store(struct device *dev,
  13293. struct device_attribute *attr, const char *buf, size_t size)
  13294. {
  13295. struct pt_core_data *cd = dev_get_drvdata(dev);
  13296. u32 input_data[2];
  13297. int length;
  13298. int rc = 0;
  13299. /* Maximum input of one value */
  13300. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13301. ARRAY_SIZE(input_data));
  13302. if (length != 1) {
  13303. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13304. __func__);
  13305. rc = -EINVAL;
  13306. goto exit;
  13307. }
  13308. mutex_lock(&cd->system_lock);
  13309. cd->get_param_id = input_data[0];
  13310. mutex_unlock(&(cd->system_lock));
  13311. exit:
  13312. if (rc)
  13313. return rc;
  13314. return size;
  13315. }
  13316. /*******************************************************************************
  13317. * FUNCTION: pt_get_param_show
  13318. *
  13319. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13320. * parameter data from the DUT based on the ID stored in the core
  13321. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13322. * communicate or some other error occures, an error status is returned
  13323. * with no value following.
  13324. * Output is in the form:
  13325. * Status: x
  13326. * 0xyyyyyyyy
  13327. * The 32bit data will only follow the status code if the status == 0
  13328. *
  13329. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13330. *
  13331. * RETURN:
  13332. * 0 = success
  13333. * !0 = failure
  13334. *
  13335. * PARAMETERS:
  13336. * *dev - pointer to device structure
  13337. * *attr - pointer to device attributes
  13338. * *buf - pointer to output buffer
  13339. ******************************************************************************/
  13340. static ssize_t pt_get_param_show(struct device *dev,
  13341. struct device_attribute *attr, char *buf)
  13342. {
  13343. struct pt_core_data *cd = dev_get_drvdata(dev);
  13344. ssize_t ret = 0;
  13345. int status;
  13346. u32 value = 0;
  13347. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13348. if (status) {
  13349. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13350. __func__, "pt_get_param", status);
  13351. ret = scnprintf(buf, strlen(buf),
  13352. "%s %d\n",
  13353. "Status:", status);
  13354. } else {
  13355. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13356. __func__, cd->get_param_id, value);
  13357. ret = scnprintf(buf, strlen(buf),
  13358. "Status: %d\n"
  13359. "0x%04X\n",
  13360. status, value);
  13361. }
  13362. return ret;
  13363. }
  13364. /*******************************************************************************
  13365. * FUNCTION: pt_ttdl_restart_show
  13366. *
  13367. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13368. * probed modules, calls startup() and then re-probes modules.
  13369. *
  13370. * RETURN: size of data written to sysfs node
  13371. *
  13372. * PARAMETERS:
  13373. * *dev - pointer to device structure
  13374. * *attr - pointer to device attributes structure
  13375. * *buf - pointer to print output buffer
  13376. ******************************************************************************/
  13377. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13378. struct device_attribute *attr, char *buf)
  13379. {
  13380. struct pt_core_data *cd = dev_get_drvdata(dev);
  13381. int t;
  13382. int rc = 0;
  13383. mutex_lock(&cd->system_lock);
  13384. cd->startup_state = STARTUP_NONE;
  13385. mutex_unlock(&(cd->system_lock));
  13386. /* ensure no left over exclusive access is still locked */
  13387. release_exclusive(cd, cd->dev);
  13388. pt_queue_restart(cd);
  13389. t = wait_event_timeout(cd->wait_q,
  13390. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13391. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13392. if (IS_TMO(t)) {
  13393. pt_debug(cd->dev, DL_ERROR,
  13394. "%s: TMO waiting for FW sentinel\n", __func__);
  13395. rc = -ETIME;
  13396. }
  13397. return scnprintf(buf, strlen(buf),
  13398. "Status: %d\n"
  13399. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13400. }
  13401. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13402. /*******************************************************************************
  13403. * FUNCTION: pt_pip2_gpio_read_show
  13404. *
  13405. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13406. * contents of the response to the passed in output buffer.
  13407. *
  13408. * RETURN: size of data written to sysfs node
  13409. *
  13410. * PARAMETERS:
  13411. * *dev - pointer to device structure
  13412. * *attr - pointer to device attributes structure
  13413. * *buf - pointer to print output buffer
  13414. ******************************************************************************/
  13415. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13416. struct device_attribute *attr, char *buf)
  13417. {
  13418. struct pt_core_data *cd = dev_get_drvdata(dev);
  13419. u8 status = 0;
  13420. u32 gpio_value = 0;
  13421. int rc = 0;
  13422. /* This functionality is only available in the BL */
  13423. if (cd->mode == PT_MODE_BOOTLOADER)
  13424. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13425. else
  13426. rc = -EPERM;
  13427. if (!rc) {
  13428. if (status == 0)
  13429. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13430. "Status: %d\n"
  13431. "DUT GPIO Reg: 0x%08X\n",
  13432. rc, gpio_value);
  13433. else
  13434. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13435. "Status: %d\n"
  13436. "DUT GPIO Reg: n/a\n",
  13437. status);
  13438. } else
  13439. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13440. "Status: %d\n"
  13441. "DUT GPIO Reg: n/a\n",
  13442. rc);
  13443. }
  13444. /*******************************************************************************
  13445. * FUNCTION: pt_pip2_version_show
  13446. *
  13447. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13448. * contents of the response to the passed in output buffer.
  13449. *
  13450. * RETURN: size of data written to sysfs node
  13451. *
  13452. * PARAMETERS:
  13453. * *dev - pointer to device structure
  13454. * *attr - pointer to device attributes structure
  13455. * *buf - pointer to print output buffer
  13456. ******************************************************************************/
  13457. static ssize_t pt_pip2_version_show(struct device *dev,
  13458. struct device_attribute *attr, char *buf)
  13459. {
  13460. int rc = 0;
  13461. struct pt_core_data *cd = dev_get_drvdata(dev);
  13462. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13463. rc = pt_pip2_get_version(cd);
  13464. if (!rc) {
  13465. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13466. "PIP VERSION : %02X.%02X\n"
  13467. "BL VERSION : %02X.%02X\n"
  13468. "FW VERSION : %02X.%02X\n"
  13469. "SILICON ID : %04X.%04X\n"
  13470. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13471. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13472. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13473. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13474. ttdata->chip_id, ttdata->chip_rev,
  13475. ttdata->uid[0], ttdata->uid[1],
  13476. ttdata->uid[2], ttdata->uid[3],
  13477. ttdata->uid[4], ttdata->uid[5],
  13478. ttdata->uid[6], ttdata->uid[7],
  13479. ttdata->uid[8], ttdata->uid[9],
  13480. ttdata->uid[10], ttdata->uid[11]);
  13481. } else {
  13482. pt_debug(dev, DL_ERROR,
  13483. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13484. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13485. "PIP VERSION : -\n"
  13486. "BL VERSION : -\n"
  13487. "FW VERSION : -\n"
  13488. "SILICON ID : -\n"
  13489. "UID : -\n");
  13490. }
  13491. }
  13492. #ifdef TTDL_DIAGNOSTICS
  13493. /*******************************************************************************
  13494. * FUNCTION: pt_ttdl_status_show
  13495. *
  13496. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13497. * variable states and GPIO levels. Additional information printed when
  13498. * TTDL_DIAGNOSTICS is enabled.
  13499. *
  13500. * NOTE: All counters will be reset to 0 when this function is called.
  13501. *
  13502. * RETURN:
  13503. * 0 = success
  13504. * !0 = failure
  13505. *
  13506. * PARAMETERS:
  13507. * *dev - pointer to device structure
  13508. * *attr - pointer to device attributes
  13509. * *buf - pointer to output buffer
  13510. ******************************************************************************/
  13511. static ssize_t pt_ttdl_status_show(struct device *dev,
  13512. struct device_attribute *attr, char *buf)
  13513. {
  13514. struct pt_core_data *cd = dev_get_drvdata(dev);
  13515. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13516. struct i2c_client *client = to_i2c_client(dev);
  13517. ssize_t ret;
  13518. u16 cal_size = 0;
  13519. unsigned short crc = 0;
  13520. if (cd->cal_cache_in_host)
  13521. _pt_manage_local_cal_data(dev,
  13522. PT_CAL_DATA_INFO, &cal_size, &crc);
  13523. ret = scnprintf(buf, strlen(buf),
  13524. "%s: 0x%04X\n"
  13525. "%s: %d\n"
  13526. "%s: %s\n"
  13527. "%s: %s %s\n"
  13528. "%s: %s\n"
  13529. "%s: 0x%02X\n"
  13530. "%s: %s\n"
  13531. "%s: %s\n"
  13532. "%s: %s\n"
  13533. "%s: %s\n"
  13534. "%s: %d\n"
  13535. "%s: %d\n"
  13536. "%s: %s\n"
  13537. "%s: %s\n"
  13538. "%s: %d\n"
  13539. #ifdef TTDL_DIAGNOSTICS
  13540. "%s: %d\n"
  13541. "%s: %d\n"
  13542. "%s: %d\n"
  13543. "%s: %d\n"
  13544. "%s: %d\n"
  13545. "%s: %d\n"
  13546. "%s: %d\n"
  13547. "%s: %d\n"
  13548. "%s: %d\n"
  13549. "%s: %d\n"
  13550. "%s: %s\n"
  13551. "%s: %s\n"
  13552. "%s: %s\n"
  13553. "%s: %d\n"
  13554. "%s: 0x%04X\n"
  13555. "%s: %s\n"
  13556. #endif /* TTDL_DIAGNOSTICS */
  13557. ,
  13558. "Startup Status ", cd->startup_status,
  13559. "TTDL Debug Level ", cd->debug_level,
  13560. "Mode ",
  13561. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13562. "Operational" : "BL") : "Unknown",
  13563. "DUT Generation ",
  13564. cd->active_dut_generation ?
  13565. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13566. "PT TC/TT" : "Gen5/6") : "Unknown",
  13567. cd->active_dut_generation ?
  13568. (cd->set_dut_generation == true ?
  13569. "(Set)" : "(Detected)") : "",
  13570. "HW Detected ",
  13571. cd->hw_detected ? "True" : "False",
  13572. "I2C Address ",
  13573. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13574. "Active Bus Module ",
  13575. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13576. "Flashless Mode ",
  13577. cd->flashless_dut == 1 ? "Yes" : "No",
  13578. "GPIO state - IRQ ",
  13579. cd->cpdata->irq_stat ?
  13580. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13581. "High" : "Low") : "not defined",
  13582. "GPIO state - TP_XRES ",
  13583. pdata->core_pdata->rst_gpio ?
  13584. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13585. "High" : "Low") : "not defined",
  13586. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13587. "Startup Retry Count ", cd->startup_retry_count,
  13588. "WD - Manual Force Stop ",
  13589. cd->watchdog_force_stop ? "True" : "False",
  13590. "WD - Enabled ",
  13591. cd->watchdog_enabled ? "True" : "False",
  13592. "WD - Interval (ms) ", cd->watchdog_interval
  13593. #ifdef TTDL_DIAGNOSTICS
  13594. , "WD - Triggered Count ", cd->watchdog_count,
  13595. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13596. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13597. "WD - XRES Count ", cd->wd_xres_count,
  13598. "IRQ Triggered Count ", cd->irq_count,
  13599. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13600. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13601. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13602. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13603. "Error GPIO trigger type ", cd->err_gpio_type,
  13604. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13605. "Suppress No-Flash Auto BL ",
  13606. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13607. "Yes" : "No",
  13608. "Calibration Cache on host ",
  13609. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13610. "Yes" : "No",
  13611. "Calibration Cache size ", cal_size,
  13612. "Calibration Cache chip ID ", crc,
  13613. "Multi-Chip Support ",
  13614. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13615. #endif /* TTDL_DIAGNOSTICS */
  13616. );
  13617. #ifdef TTDL_DIAGNOSTICS
  13618. /* Reset all diagnostic counters */
  13619. cd->watchdog_count = 0;
  13620. cd->watchdog_irq_stuck_count = 0;
  13621. cd->watchdog_failed_access_count = 0;
  13622. cd->wd_xres_count = 0;
  13623. cd->irq_count = 0;
  13624. cd->bl_retry_packet_count = 0;
  13625. cd->pip2_crc_error_count = 0;
  13626. cd->bus_transmit_error_count = 0;
  13627. #endif
  13628. return ret;
  13629. }
  13630. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13631. /*******************************************************************************
  13632. * FUNCTION: pt_pip2_enter_bl_show
  13633. *
  13634. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13635. * the DUT into the BL and show the success or failure of entering the BL
  13636. *
  13637. * RETURN:
  13638. * 0 = success
  13639. * !0 = failure
  13640. *
  13641. * PARAMETERS:
  13642. * *dev - pointer to device structure
  13643. * *attr - pointer to device attributes
  13644. * *buf - pointer to output buffer
  13645. ******************************************************************************/
  13646. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13647. struct device_attribute *attr, char *buf)
  13648. {
  13649. ssize_t ret = 0;
  13650. int rc = 0;
  13651. int result = 0;
  13652. u8 mode = PT_MODE_UNKNOWN;
  13653. struct pt_core_data *cd = dev_get_drvdata(dev);
  13654. /* Turn off the TTDL WD before enter bootloader */
  13655. pt_stop_wd_timer(cd);
  13656. /* Ensure NO enumeration work is queued or will be queued */
  13657. cancel_work_sync(&cd->enum_work);
  13658. mutex_lock(&cd->system_lock);
  13659. cd->bridge_mode = true;
  13660. mutex_unlock(&cd->system_lock);
  13661. /* set mode to operational to avoid any extra PIP traffic */
  13662. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13663. switch (result) {
  13664. case PT_ENTER_BL_PASS:
  13665. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13666. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13667. break;
  13668. case PT_ENTER_BL_ERROR:
  13669. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13670. rc,
  13671. " Unknown Error");
  13672. break;
  13673. case PT_ENTER_BL_RESET_FAIL:
  13674. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13675. rc,
  13676. " Soft Reset Failed");
  13677. break;
  13678. case PT_ENTER_BL_HID_START_BL_FAIL:
  13679. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13680. rc,
  13681. " PIP Start BL Cmd Failed");
  13682. break;
  13683. case PT_ENTER_BL_CONFIRM_FAIL:
  13684. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13685. rc,
  13686. " Error confirming DUT entered BL");
  13687. break;
  13688. default:
  13689. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13690. rc, " Unknown Error");
  13691. break;
  13692. };
  13693. /* Allow enumeration work to be queued again */
  13694. cd->bridge_mode = false;
  13695. return ret;
  13696. }
  13697. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13698. /*******************************************************************************
  13699. * FUNCTION: pt_pip2_exit_bl_show
  13700. *
  13701. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13702. * launch the APP and put the DUT Application mode
  13703. *
  13704. * RETURN:
  13705. * 0 = success
  13706. * !0 = failure
  13707. *
  13708. * PARAMETERS:
  13709. * *dev - pointer to device structure
  13710. * *attr - pointer to device attributes
  13711. * *buf - pointer to output buffer
  13712. ******************************************************************************/
  13713. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13714. struct device_attribute *attr, char *buf)
  13715. {
  13716. struct pt_core_data *cd = dev_get_drvdata(dev);
  13717. ssize_t ret = 0;
  13718. int rc = 0;
  13719. u8 status_str[PT_STATUS_STR_LEN];
  13720. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13721. /*
  13722. * Perform enum if startup_status doesn't reach to
  13723. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13724. */
  13725. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13726. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13727. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13728. strlcpy(status_str,
  13729. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13730. }
  13731. }
  13732. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13733. rc, status_str);
  13734. return ret;
  13735. }
  13736. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13737. #endif
  13738. #ifdef EASYWAKE_TSG6
  13739. /*******************************************************************************
  13740. * FUNCTION: pt_easy_wakeup_gesture_show
  13741. *
  13742. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13743. * current easy wakeup gesture
  13744. *
  13745. * RETURN:
  13746. * 0 = success
  13747. * !0 = failure
  13748. *
  13749. * PARAMETERS:
  13750. * *dev - pointer to device structure
  13751. * *attr - pointer to device attributes
  13752. * *buf - pointer to output buffer
  13753. ******************************************************************************/
  13754. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13755. struct device_attribute *attr, char *buf)
  13756. {
  13757. struct pt_core_data *cd = dev_get_drvdata(dev);
  13758. ssize_t ret;
  13759. mutex_lock(&cd->system_lock);
  13760. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13761. cd->easy_wakeup_gesture);
  13762. mutex_unlock(&cd->system_lock);
  13763. return ret;
  13764. }
  13765. /*******************************************************************************
  13766. * FUNCTION: pt_easy_wakeup_gesture_store
  13767. *
  13768. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13769. * allows the wake gesture to be set to a custom value.
  13770. *
  13771. * RETURN: Size of passed in buffer is success
  13772. *
  13773. * PARAMETERS:
  13774. * *dev - pointer to device structure
  13775. * *attr - pointer to device attributes
  13776. * *buf - pointer to buffer that hold the command parameters
  13777. * size - size of buf
  13778. ******************************************************************************/
  13779. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13780. struct device_attribute *attr, const char *buf, size_t size)
  13781. {
  13782. struct pt_core_data *cd = dev_get_drvdata(dev);
  13783. u32 input_data[2];
  13784. int length;
  13785. int rc = 0;
  13786. /* Maximum input of one value */
  13787. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13788. ARRAY_SIZE(input_data));
  13789. if (length != 1) {
  13790. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13791. __func__);
  13792. rc = -EINVAL;
  13793. goto exit;
  13794. }
  13795. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13796. __func__, cd->features.easywake);
  13797. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13798. rc = -EINVAL;
  13799. goto exit;
  13800. }
  13801. pm_runtime_get_sync(dev);
  13802. mutex_lock(&cd->system_lock);
  13803. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13804. cd->easy_wakeup_gesture = (u8)input_data[0];
  13805. pt_debug(dev, DL_INFO,
  13806. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13807. __func__, cd->easy_wakeup_gesture);
  13808. } else
  13809. rc = -ENODEV;
  13810. mutex_unlock(&cd->system_lock);
  13811. pm_runtime_put(dev);
  13812. exit:
  13813. if (rc)
  13814. return rc;
  13815. return size;
  13816. }
  13817. /*******************************************************************************
  13818. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13819. *
  13820. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13821. * show the TSG6 easywake gesture ID
  13822. *
  13823. * RETURN:
  13824. * 0 = success
  13825. * !0 = failure
  13826. *
  13827. * PARAMETERS:
  13828. * *dev - pointer to device structure
  13829. * *attr - pointer to device attributes
  13830. * *buf - pointer to output buffer
  13831. ******************************************************************************/
  13832. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13833. struct device_attribute *attr, char *buf)
  13834. {
  13835. struct pt_core_data *cd = dev_get_drvdata(dev);
  13836. ssize_t ret;
  13837. mutex_lock(&cd->system_lock);
  13838. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13839. cd->gesture_id);
  13840. mutex_unlock(&cd->system_lock);
  13841. return ret;
  13842. }
  13843. /*******************************************************************************
  13844. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13845. *
  13846. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13847. * show the TSG6 easywake gesture data in the following format:
  13848. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13849. *
  13850. * RETURN:
  13851. * 0 = success
  13852. * !0 = failure
  13853. *
  13854. * PARAMETERS:
  13855. * *dev - pointer to device structure
  13856. * *attr - pointer to device attributes
  13857. * *buf - pointer to output buffer
  13858. ******************************************************************************/
  13859. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13860. struct device_attribute *attr, char *buf)
  13861. {
  13862. struct pt_core_data *cd = dev_get_drvdata(dev);
  13863. ssize_t ret = 0;
  13864. int i;
  13865. mutex_lock(&cd->system_lock);
  13866. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13867. for (i = 0; i < cd->gesture_data_length; i++)
  13868. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13869. "0x%02X\n", cd->gesture_data[i]);
  13870. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13871. "(%d bytes)\n", cd->gesture_data_length);
  13872. mutex_unlock(&cd->system_lock);
  13873. return ret;
  13874. }
  13875. #endif /* EASYWAKE_TSG6 */
  13876. #ifdef TTDL_DIAGNOSTICS
  13877. /*******************************************************************************
  13878. * FUNCTION: pt_err_gpio_show
  13879. *
  13880. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13881. * setting up the gpio was successful
  13882. *
  13883. * RETURN: Char buffer with printed GPIO creation state
  13884. *
  13885. * PARAMETERS:
  13886. * *dev - pointer to device structure
  13887. * *attr - pointer to device attributes
  13888. * *buf - pointer to output buffer
  13889. ******************************************************************************/
  13890. static ssize_t pt_err_gpio_show(struct device *dev,
  13891. struct device_attribute *attr, char *buf)
  13892. {
  13893. struct pt_core_data *cd = dev_get_drvdata(dev);
  13894. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13895. "Err GPIO (%d) : %s\n"
  13896. "Err GPIO trig type: %d\n",
  13897. cd->err_gpio,
  13898. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13899. "HIGH" : "low") : "not defined"),
  13900. cd->err_gpio_type);
  13901. }
  13902. /*******************************************************************************
  13903. * FUNCTION: pt_err_gpio_store
  13904. *
  13905. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13906. * available host GPIO to be used to trigger when TTDL detects a PIP
  13907. * command/response timeout.
  13908. *
  13909. * RETURN:
  13910. * 0 = success
  13911. * !0 = failure
  13912. *
  13913. * PARAMETERS:
  13914. * *dev - pointer to device structure
  13915. * *attr - pointer to device attributes
  13916. * *buf - pointer to buffer that hold the command parameters
  13917. * size - size of buf
  13918. ******************************************************************************/
  13919. static ssize_t pt_err_gpio_store(struct device *dev,
  13920. struct device_attribute *attr, const char *buf, size_t size)
  13921. {
  13922. struct pt_core_data *cd = dev_get_drvdata(dev);
  13923. unsigned long gpio;
  13924. int rc = 0;
  13925. u32 input_data[3];
  13926. int length;
  13927. u8 err_type;
  13928. input_data[0] = 0;
  13929. input_data[1] = 0;
  13930. /* Maximmum input is two elements */
  13931. length = _pt_ic_parse_input(dev, buf, size,
  13932. input_data, ARRAY_SIZE(input_data));
  13933. if (length < 1 || length > 2) {
  13934. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13935. __func__);
  13936. rc = -EINVAL;
  13937. goto exit;
  13938. }
  13939. gpio = input_data[0];
  13940. err_type = (u8)input_data[1];
  13941. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13942. rc = -EINVAL;
  13943. goto exit;
  13944. }
  13945. mutex_lock(&cd->system_lock);
  13946. gpio_free(gpio);
  13947. rc = gpio_request(gpio, NULL);
  13948. if (rc) {
  13949. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13950. rc = -ENODEV;
  13951. } else {
  13952. cd->err_gpio = gpio;
  13953. cd->err_gpio_type = err_type;
  13954. gpio_direction_output(gpio, 0);
  13955. }
  13956. mutex_unlock(&cd->system_lock);
  13957. exit:
  13958. if (rc)
  13959. return rc;
  13960. return size;
  13961. }
  13962. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13963. pt_err_gpio_store);
  13964. /*******************************************************************************
  13965. * FUNCTION: pt_drv_irq_show
  13966. *
  13967. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13968. * TTDL interrupt is enabled/disabled
  13969. *
  13970. * RETURN:
  13971. * 0 = success
  13972. * !0 = failure
  13973. *
  13974. * PARAMETERS:
  13975. * *dev - pointer to device structure
  13976. * *attr - pointer to device attributes
  13977. * *buf - pointer to output buffer
  13978. ******************************************************************************/
  13979. static ssize_t pt_drv_irq_show(struct device *dev,
  13980. struct device_attribute *attr, char *buf)
  13981. {
  13982. struct pt_core_data *cd = dev_get_drvdata(dev);
  13983. ssize_t ret = 0;
  13984. mutex_lock(&cd->system_lock);
  13985. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13986. if (cd->irq_enabled)
  13987. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13988. "Driver interrupt: ENABLED\n");
  13989. else
  13990. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13991. "Driver interrupt: DISABLED\n");
  13992. mutex_unlock(&cd->system_lock);
  13993. return ret;
  13994. }
  13995. /*******************************************************************************
  13996. * FUNCTION: pt_drv_irq_store
  13997. *
  13998. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13999. * IRQ to be enabled/disabled.
  14000. *
  14001. * RETURN: Size of passed in buffer
  14002. *
  14003. * PARAMETERS:
  14004. * *dev - pointer to device structure
  14005. * *attr - pointer to device attributes
  14006. * *buf - pointer to buffer that hold the command parameters
  14007. * size - size of buf
  14008. ******************************************************************************/
  14009. static ssize_t pt_drv_irq_store(struct device *dev,
  14010. struct device_attribute *attr, const char *buf, size_t size)
  14011. {
  14012. struct pt_core_data *cd = dev_get_drvdata(dev);
  14013. u32 input_data[2];
  14014. int length;
  14015. int rc = 0;
  14016. /* Maximum input of one value */
  14017. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14018. ARRAY_SIZE(input_data));
  14019. if (length != 1) {
  14020. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14021. __func__);
  14022. rc = -EINVAL;
  14023. goto exit;
  14024. }
  14025. mutex_lock(&cd->system_lock);
  14026. switch (input_data[0]) {
  14027. case 0:
  14028. if (cd->irq_enabled) {
  14029. cd->irq_enabled = false;
  14030. /* Disable IRQ has no return value to check */
  14031. disable_irq_nosync(cd->irq);
  14032. pt_debug(dev, DL_INFO,
  14033. "%s: Driver IRQ now disabled\n",
  14034. __func__);
  14035. } else
  14036. pt_debug(dev, DL_INFO,
  14037. "%s: Driver IRQ already disabled\n",
  14038. __func__);
  14039. break;
  14040. case 1:
  14041. if (cd->irq_enabled == false) {
  14042. cd->irq_enabled = true;
  14043. enable_irq(cd->irq);
  14044. pt_debug(dev, DL_INFO,
  14045. "%s: Driver IRQ now enabled\n",
  14046. __func__);
  14047. } else
  14048. pt_debug(dev, DL_INFO,
  14049. "%s: Driver IRQ already enabled\n",
  14050. __func__);
  14051. break;
  14052. default:
  14053. rc = -EINVAL;
  14054. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14055. }
  14056. mutex_unlock(&(cd->system_lock));
  14057. exit:
  14058. if (rc)
  14059. return rc;
  14060. return size;
  14061. }
  14062. /*******************************************************************************
  14063. * FUNCTION: pt_pip2_bin_hdr_show
  14064. *
  14065. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14066. * the bin file header from flash and show each field
  14067. *
  14068. * RETURN:
  14069. * 0 = success
  14070. * !0 = failure
  14071. *
  14072. * PARAMETERS:
  14073. * *dev - pointer to device structure
  14074. * *attr - pointer to device attributes
  14075. * *buf - pointer to output buffer
  14076. ******************************************************************************/
  14077. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14078. struct device_attribute *attr, char *buf)
  14079. {
  14080. ssize_t ret = 0;
  14081. struct pt_bin_file_hdr hdr = {0};
  14082. int rc;
  14083. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14084. ret = scnprintf(buf, strlen(buf),
  14085. "%s: %d\n"
  14086. "%s: %d\n"
  14087. "%s: 0x%04X\n"
  14088. "%s: %d\n"
  14089. "%s: %d\n"
  14090. "%s: %d\n"
  14091. "%s: 0x%08X\n"
  14092. "%s: 0x%04X\n"
  14093. "%s: 0x%04X\n"
  14094. "%s: %d\n"
  14095. "%s: %d\n",
  14096. "Status", rc,
  14097. "Header Length ", hdr.length,
  14098. "TTPID ", hdr.ttpid,
  14099. "FW Major Ver ", hdr.fw_major,
  14100. "FW Minor Ver ", hdr.fw_minor,
  14101. "FW Rev Control ", hdr.fw_rev_ctrl,
  14102. "FW CRC ", hdr.fw_crc,
  14103. "Silicon Rev ", hdr.si_rev,
  14104. "Silicon ID ", hdr.si_id,
  14105. "Config Ver ", hdr.config_ver,
  14106. "HEX File Size ", hdr.hex_file_size
  14107. );
  14108. return ret;
  14109. }
  14110. /*******************************************************************************
  14111. * FUNCTION: pt_platform_data_show
  14112. *
  14113. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14114. * active platform data including: GPIOs, Vendor and Product IDs,
  14115. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14116. * HID registers, and Easy wake gesture
  14117. *
  14118. * RETURN: Size of printed data
  14119. *
  14120. * PARAMETERS:
  14121. * *dev - pointer to device structure
  14122. * *attr - pointer to device attributes
  14123. * *buf - pointer to output buffer
  14124. ******************************************************************************/
  14125. static ssize_t pt_platform_data_show(struct device *dev,
  14126. struct device_attribute *attr, char *buf)
  14127. {
  14128. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14129. struct pt_core_data *cd = dev_get_drvdata(dev);
  14130. ssize_t ret;
  14131. ret = scnprintf(buf, strlen(buf),
  14132. "%s: %d\n"
  14133. "%s: %d\n"
  14134. "%s: %d\n"
  14135. "%s: %d\n"
  14136. "%s: %d\n"
  14137. "%s: %d\n"
  14138. "%s: %d\n"
  14139. "%s: %d\n"
  14140. "%s: %d\n"
  14141. "%s: %d\n"
  14142. "%s: %d\n"
  14143. "%s: %d\n"
  14144. "%s: %d\n"
  14145. "%s: %d\n"
  14146. "%s: %d\n"
  14147. "%s: %d\n"
  14148. "%s: %d\n"
  14149. "%s: %d\n"
  14150. "%s: %d\n"
  14151. "%s: %d\n"
  14152. "%s: %d\n"
  14153. "%s: %d\n"
  14154. "%s: %d\n"
  14155. "%s: %d\n"
  14156. "%s: %d\n"
  14157. "%s: %d\n"
  14158. "%s: %d\n"
  14159. "%s: %s\n"
  14160. "%s: %s\n"
  14161. "%s: %d\n",
  14162. "Status", 0,
  14163. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14164. "Interrupt GPIO Value ",
  14165. pdata->core_pdata->irq_gpio ?
  14166. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14167. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14168. "Reset GPIO Value ",
  14169. pdata->core_pdata->rst_gpio ?
  14170. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14171. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14172. "DDI Reset GPIO Value ",
  14173. pdata->core_pdata->ddi_rst_gpio ?
  14174. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14175. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14176. "VDDI GPIO Value ",
  14177. pdata->core_pdata->vddi_gpio ?
  14178. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14179. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14180. "VCC GPIO Value ",
  14181. pdata->core_pdata->vcc_gpio ?
  14182. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14183. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14184. "AVDD GPIO Value ",
  14185. pdata->core_pdata->avdd_gpio ?
  14186. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14187. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14188. "AVEE GPIO Value ",
  14189. pdata->core_pdata->avee_gpio ?
  14190. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14191. "Vendor ID ", pdata->core_pdata->vendor_id,
  14192. "Product ID ", pdata->core_pdata->product_id,
  14193. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14194. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14195. "Core data flags ", pdata->core_pdata->flags,
  14196. "MT data flags ", pdata->mt_pdata->flags,
  14197. "Loader data flags ", pdata->loader_pdata->flags,
  14198. "Level trigger delay (us) ",
  14199. pdata->core_pdata->level_irq_udelay,
  14200. "HID Descriptor Register ",
  14201. pdata->core_pdata->hid_desc_register,
  14202. "HID Output Register ",
  14203. cd->hid_desc.output_register,
  14204. "HID Command Register ",
  14205. cd->hid_desc.command_register,
  14206. "Easy wakeup gesture ",
  14207. pdata->core_pdata->easy_wakeup_gesture,
  14208. "Config DUT generation ",
  14209. pdata->core_pdata->config_dut_generation ?
  14210. (pdata->core_pdata->config_dut_generation ==
  14211. CONFIG_DUT_PIP2_CAPABLE ?
  14212. "PT TC/TT" : "Gen5/6") : "Auto",
  14213. "Watchdog Force Stop ",
  14214. pdata->core_pdata->watchdog_force_stop ?
  14215. "True" : "False",
  14216. "Panel ID Support ",
  14217. pdata->core_pdata->panel_id_support);
  14218. return ret;
  14219. }
  14220. #define PT_ERR_STR_SIZE 64
  14221. /*******************************************************************************
  14222. * FUNCTION: pt_bist_bus_test
  14223. *
  14224. * SUMMARY: Tests the connectivity of the active bus pins:
  14225. * I2C - SDA and SCL
  14226. * SPI - MOSI, MISO, CLK
  14227. *
  14228. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14229. * bus. If any data is read we know the I2C/SPI pins are connected
  14230. *
  14231. * RETURN:
  14232. * 0 = Success
  14233. * !0 = Failure
  14234. *
  14235. * PARAMETERS:
  14236. * *dev - pointer to device structure
  14237. * *net_toggled - pointer to where to store if bus toggled
  14238. * *err_str - pointer to error string buffer
  14239. ******************************************************************************/
  14240. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14241. {
  14242. struct pt_core_data *cd = dev_get_drvdata(dev);
  14243. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14244. u8 *read_buf = NULL;
  14245. int bytes_read = 0;
  14246. int rc = 0;
  14247. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14248. if (read_buf == NULL) {
  14249. rc = -ENOMEM;
  14250. goto exit;
  14251. }
  14252. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14253. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14254. disable_irq(cd->irq);
  14255. mutex_lock(&cd->system_lock);
  14256. cd->irq_disabled = true;
  14257. mutex_unlock(&cd->system_lock);
  14258. /*
  14259. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14260. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14261. */
  14262. usleep_range(5000, 6000);
  14263. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14264. __func__, (int)sizeof(ver_cmd));
  14265. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14266. ">>> User CMD");
  14267. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14268. if (rc) {
  14269. pt_debug(dev, DL_ERROR,
  14270. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14271. *net_toggled = 0;
  14272. strlcpy(err_str,
  14273. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14274. goto exit_enable_irq;
  14275. }
  14276. usleep_range(4000, 5000);
  14277. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14278. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14279. __func__, bytes_read);
  14280. if (bytes_read == 0) {
  14281. *net_toggled = 0;
  14282. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14283. __func__);
  14284. strlcpy(err_str,
  14285. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14286. rc = -EIO;
  14287. goto exit_enable_irq;
  14288. } else {
  14289. if (cd->bus_ops->bustype == BUS_I2C)
  14290. *net_toggled = 1;
  14291. else {
  14292. if ((bytes_read > 3) &&
  14293. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14294. PIP2_CMD_ID_VERSION)
  14295. *net_toggled = 1;
  14296. else {
  14297. *net_toggled = 0;
  14298. pt_debug(dev, DL_INFO,
  14299. "%s: BUS Read Failed, %d bytes read\n",
  14300. __func__, bytes_read);
  14301. strlcpy(err_str,
  14302. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14303. }
  14304. }
  14305. }
  14306. exit_enable_irq:
  14307. enable_irq(cd->irq);
  14308. usleep_range(5000, 6000);
  14309. mutex_lock(&cd->system_lock);
  14310. cd->irq_disabled = false;
  14311. mutex_unlock(&cd->system_lock);
  14312. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14313. exit:
  14314. kfree(read_buf);
  14315. return rc;
  14316. }
  14317. /*******************************************************************************
  14318. * FUNCTION: pt_bist_irq_test
  14319. *
  14320. * SUMMARY: Tests the connectivity of the IRQ net
  14321. *
  14322. * This test will ensure there is a good connection between the host
  14323. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14324. * asserted and if so keep reading messages off of the bus until
  14325. * it de-asserts. Possible outcomes:
  14326. * - IRQ was already de-asserted: Send a PIP command and if an
  14327. * interrupt is generated the test passes.
  14328. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14329. * test passes.
  14330. * - IRQ stays asserted: After reading the bus multiple times
  14331. * the IRQ stays asserted. Likely open or shorted to GND
  14332. *
  14333. * RETURN:
  14334. * 0 = Success
  14335. * !0 = Failure
  14336. *
  14337. * PARAMETERS:
  14338. * *dev - pointer to device structure
  14339. * *bus_toggled - pointer to where to store if bus toggled
  14340. * *irq_toggled - pointer to where to store if IRQ toggled
  14341. * *xres_toggled - pointer to where to store if XRES toggled
  14342. * *err_str - pointer to error string buffer
  14343. ******************************************************************************/
  14344. static int pt_bist_irq_test(struct device *dev,
  14345. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14346. {
  14347. struct pt_core_data *cd = dev_get_drvdata(dev);
  14348. u8 *read_buf = NULL;
  14349. u8 mode = PT_MODE_UNKNOWN;
  14350. u16 actual_read_len;
  14351. int bytes_read = 0;
  14352. int count = 0;
  14353. int rc = 0;
  14354. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14355. if (read_buf == NULL) {
  14356. rc = -ENOMEM;
  14357. goto exit;
  14358. }
  14359. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14360. cd->irq_count = 0;
  14361. /*
  14362. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14363. * the bus based on the 2 byte initial length read. Try up to 5x.
  14364. */
  14365. while (pt_check_irq_asserted(cd) && count < 5) {
  14366. count++;
  14367. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14368. }
  14369. if (count > 1 && count < 5 && bytes_read > 0) {
  14370. /*
  14371. * IRQ was stuck but data was successfully read from the
  14372. * bus releasing the IRQ line.
  14373. */
  14374. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14375. __func__, count, bytes_read);
  14376. *bus_toggled = 1;
  14377. *irq_toggled = 1;
  14378. goto exit;
  14379. }
  14380. if (count == 5 && bytes_read == 0) {
  14381. /*
  14382. * Looped 5x and read nothing off the bus yet the IRQ is still
  14383. * asserted. Possible conditions:
  14384. * - IRQ open circuit
  14385. * - IRQ shorted to GND
  14386. * - I2C/SPI bus is disconnected
  14387. * - FW holding the pin low
  14388. * Try entering the BL to see if communication works there.
  14389. */
  14390. mode = PT_MODE_IGNORE;
  14391. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14392. if (rc) {
  14393. pt_debug(dev, DL_ERROR,
  14394. "%s Failed to enter BL\n", __func__);
  14395. strlcpy(err_str,
  14396. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14397. *irq_toggled = 0;
  14398. goto exit;
  14399. }
  14400. /*
  14401. * If original mode was operational and we successfully
  14402. * entered the BL, then the XRES net must have toggled
  14403. */
  14404. if (mode == PT_MODE_OPERATIONAL)
  14405. *xres_toggled = 1;
  14406. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14407. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14408. &actual_read_len);
  14409. if (rc) {
  14410. /*
  14411. * Could not communicate to DUT in BL mode. Save the
  14412. * error string, slim chance but the XRES test below may
  14413. * show the IRQ is actually working.
  14414. */
  14415. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14416. pt_debug(dev, DL_ERROR,
  14417. "%s: %s, count=%d bytes_read=%d\n",
  14418. __func__, err_str, count, bytes_read);
  14419. *irq_toggled = 0;
  14420. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14421. goto exit;
  14422. } else {
  14423. *bus_toggled = 1;
  14424. *irq_toggled = 1;
  14425. goto exit;
  14426. }
  14427. }
  14428. if (pt_check_irq_asserted(cd)) {
  14429. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14430. rc = -EIO;
  14431. *irq_toggled = 0;
  14432. goto exit;
  14433. }
  14434. /* Try sending a PIP command to see if we get a response */
  14435. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14436. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14437. if (rc) {
  14438. /*
  14439. * Potential IRQ issue, no communication in App mode, attempt
  14440. * the same command in the BL
  14441. */
  14442. mode = PT_MODE_IGNORE;
  14443. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14444. if (rc) {
  14445. pt_debug(dev, DL_ERROR,
  14446. "%s Failed to enter BL\n", __func__);
  14447. *irq_toggled = 0;
  14448. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14449. goto exit;
  14450. }
  14451. /*
  14452. * If original mode was operational and we successfully
  14453. * entered the BL, this will be useful info for the tp_xres
  14454. * test below.
  14455. */
  14456. if (mode == PT_MODE_OPERATIONAL)
  14457. *xres_toggled = 1;
  14458. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14459. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14460. &actual_read_len);
  14461. if (rc) {
  14462. /*
  14463. * Could not communicate in FW mode or BL mode. Save the
  14464. * error string, slim chance but the XRES test below may
  14465. * show the IRQ is actually working.
  14466. */
  14467. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14468. pt_debug(dev, DL_ERROR,
  14469. "%s: request_active_pip_prot failed\n",
  14470. __func__);
  14471. *irq_toggled = 0;
  14472. goto exit;
  14473. }
  14474. }
  14475. if (cd->irq_count > 0) {
  14476. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14477. cd->irq_count);
  14478. *bus_toggled = 1;
  14479. *irq_toggled = 1;
  14480. goto exit;
  14481. }
  14482. exit:
  14483. kfree(read_buf);
  14484. return rc;
  14485. }
  14486. /*******************************************************************************
  14487. * FUNCTION: pt_bist_xres_test
  14488. *
  14489. * SUMMARY: Tests the connectivity of the TP_XRES net
  14490. *
  14491. * This test will ensure there is a good connection between the host
  14492. * and the DUT on the tp_xres pin. The pin will be toggled to
  14493. * generate a TP reset which will cause the DUT to output a reset
  14494. * sentinel. If the reset sentinel is seen the test passes. If it is
  14495. * not seen the test will attempt to send a soft reset to simply gain
  14496. * some additional information on the failure:
  14497. * - soft reset fails to send: XRES and IRQ likely open
  14498. * - soft reset passes: XRES likely open or stuck de-asserted
  14499. * - soft reset fails: XRES likely stuck asserted
  14500. *
  14501. * RETURN:
  14502. * 0 = Success
  14503. * !0 = Failure
  14504. *
  14505. * PARAMETERS:
  14506. * *dev - pointer to device structure
  14507. * *bus_toggled - pointer to where to store if bus toggled
  14508. * *irq_toggled - pointer to where to store if IRQ toggled
  14509. * *xres_toggled - pointer to where to store if XRES toggled
  14510. * *err_str - pointer to error string buffer
  14511. ******************************************************************************/
  14512. static int pt_bist_xres_test(struct device *dev,
  14513. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14514. {
  14515. struct pt_core_data *cd = dev_get_drvdata(dev);
  14516. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14517. u8 *read_buf = NULL;
  14518. u8 mode = PT_MODE_UNKNOWN;
  14519. int rc = 0;
  14520. int t = 0;
  14521. int timeout = 300;
  14522. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14523. if (read_buf == NULL) {
  14524. rc = -ENOMEM;
  14525. goto exit;
  14526. }
  14527. /* Clear the startup bit mask, reset and enum will re-populate it */
  14528. cd->startup_status = STARTUP_STATUS_START;
  14529. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14530. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14531. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14532. rc = -ENODEV;
  14533. goto exit;
  14534. }
  14535. /* Ensure we have nothing pending on active bus */
  14536. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14537. /* Perform a hard XRES toggle and wait for reset sentinel */
  14538. mutex_lock(&cd->system_lock);
  14539. cd->hid_reset_cmd_state = 1;
  14540. mutex_unlock(&cd->system_lock);
  14541. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14542. rc = pt_hw_hard_reset(cd);
  14543. /* Set timeout to 1s for the flashless case where a BL could be done */
  14544. if (cd->flashless_dut)
  14545. timeout = 1000;
  14546. /*
  14547. * To avoid the case that next PIP command can be confused by BL/FW
  14548. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14549. * followed by "wakeup event" function can lower the failure rate.
  14550. */
  14551. t = wait_event_timeout(cd->wait_q,
  14552. ((cd->startup_status != STARTUP_STATUS_START)
  14553. && (cd->hid_reset_cmd_state == 0)),
  14554. msecs_to_jiffies(timeout));
  14555. if (IS_TMO(t)) {
  14556. pt_debug(cd->dev, DL_ERROR,
  14557. "%s: TMO waiting for sentinel\n", __func__);
  14558. *xres_toggled = 0;
  14559. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14560. /*
  14561. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14562. * To enter the BL we need to generate an XRES so first try to
  14563. * launch the applicaiton
  14564. */
  14565. if (cd->mode == PT_MODE_BOOTLOADER)
  14566. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14567. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14568. __func__);
  14569. mode = PT_MODE_IGNORE;
  14570. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14571. if (rc) {
  14572. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14573. __func__);
  14574. *xres_toggled = 0;
  14575. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14576. goto exit;
  14577. } else {
  14578. /* Wait for the BL sentinel */
  14579. t = wait_event_timeout(cd->wait_q,
  14580. (cd->startup_status != STARTUP_STATUS_START),
  14581. msecs_to_jiffies(500));
  14582. if (IS_TMO(t)) {
  14583. pt_debug(cd->dev, DL_ERROR,
  14584. "%s: TMO waiting for BL sentinel\n",
  14585. __func__);
  14586. *xres_toggled = 0;
  14587. strlcpy(err_str,
  14588. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14589. rc = -ETIME;
  14590. goto exit;
  14591. }
  14592. }
  14593. }
  14594. mutex_lock(&cd->system_lock);
  14595. cd->hid_reset_cmd_state = 0;
  14596. mutex_unlock(&cd->system_lock);
  14597. /* Look for BL or FW reset sentinels */
  14598. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14599. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14600. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14601. /* If a sentinel was seen, all nets are working */
  14602. *xres_toggled = 1;
  14603. *irq_toggled = 1;
  14604. /*
  14605. * For SPI test, bus read result can be confused as FW sentinel
  14606. * if MISO(slave) is connected to MISO(host).
  14607. */
  14608. if (cd->bus_ops->bustype == BUS_I2C)
  14609. *bus_toggled = 1;
  14610. } else {
  14611. /*
  14612. * Hard reset failed, however some additional information
  14613. * could be determined. Try a soft reset to see if DUT resets
  14614. * with the possible outcomes:
  14615. * - if it resets the line is not stuck asserted
  14616. * - if it does not reset the line could be stuck asserted
  14617. */
  14618. *xres_toggled = 0;
  14619. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14620. msleep(30);
  14621. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14622. __func__, rc);
  14623. if (rc) {
  14624. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14625. pt_debug(dev, DL_ERROR,
  14626. "%s: Hard reset failed, soft reset failed %s\n",
  14627. __func__, err_str);
  14628. goto exit;
  14629. }
  14630. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14631. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14632. strlcpy(err_str,
  14633. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14634. pt_debug(dev, DL_ERROR,
  14635. "%s: Hard reset failed, soft reset passed-%s\n",
  14636. __func__, err_str);
  14637. } else if (cd->startup_status == 0) {
  14638. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14639. pt_debug(dev, DL_ERROR,
  14640. "%s: Hard reset failed, soft reset failed-%s\n",
  14641. __func__, err_str);
  14642. } else {
  14643. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14644. pt_debug(dev, DL_ERROR,
  14645. "%s: Hard and Soft reset failed - %s\n",
  14646. __func__, err_str);
  14647. }
  14648. }
  14649. exit:
  14650. kfree(read_buf);
  14651. return rc;
  14652. }
  14653. /*******************************************************************************
  14654. * FUNCTION: pt_bist_slave_irq_test
  14655. *
  14656. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14657. *
  14658. * This test will ensure there is a good IRQ connection between the master
  14659. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14660. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14661. * saw each slave trigger the IRQ with it's reset sentinel.
  14662. *
  14663. * RETURN:
  14664. * 0 = Success
  14665. * !0 = Failure
  14666. *
  14667. * PARAMETERS:
  14668. * *dev - pointer to device structure
  14669. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14670. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14671. * *err_str - pointer to error string buffer
  14672. * *slave_detect - pointer to slave detect buffer
  14673. * *boot_err - pointer to boot_err buffer
  14674. ******************************************************************************/
  14675. static int pt_bist_slave_irq_test(struct device *dev,
  14676. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14677. u8 *slave_detect, u8 *boot_err)
  14678. {
  14679. struct pt_core_data *cd = dev_get_drvdata(dev);
  14680. u8 mode = PT_MODE_UNKNOWN;
  14681. u8 status;
  14682. u8 boot;
  14683. u8 read_buf[12];
  14684. u8 detected = 0;
  14685. u8 last_err = -1;
  14686. u16 actual_read_len;
  14687. int result = 0;
  14688. int rc = 0;
  14689. /*
  14690. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14691. * detect bits. If the DUT was in FW, entering the BL will cause an
  14692. * XRES signal which will inadvertently test the XRES net as well
  14693. */
  14694. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14695. if (rc) {
  14696. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14697. __func__, rc);
  14698. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14699. goto exit;
  14700. }
  14701. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14702. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14703. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14704. &actual_read_len);
  14705. if (!rc) {
  14706. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14707. "PIP2 STATUS");
  14708. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14709. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14710. /* Slave detect is only valid if status ok and in boot exec */
  14711. if (status == PIP2_RSP_ERR_NONE &&
  14712. boot == PIP2_STATUS_BOOT_EXEC) {
  14713. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14714. SLAVE_DETECT_MASK;
  14715. } else {
  14716. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14717. rc = -EPERM;
  14718. }
  14719. } else {
  14720. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14721. __func__);
  14722. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14723. goto exit;
  14724. }
  14725. /*
  14726. * Retrieve boot error regardless of the state of the slave detect
  14727. * bit because the IRQ could have been stuck high or low.
  14728. */
  14729. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14730. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14731. read_buf, &actual_read_len);
  14732. if (!rc) {
  14733. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14734. "PIP2 GET_LAST_ERRNO");
  14735. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14736. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14737. if (last_err) {
  14738. pt_debug(cd->dev, DL_ERROR,
  14739. "%s: Master Boot Last Err = 0x%02X\n",
  14740. __func__, last_err);
  14741. }
  14742. } else {
  14743. pt_debug(cd->dev, DL_ERROR,
  14744. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14745. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14746. }
  14747. exit:
  14748. pt_debug(cd->dev, DL_INFO,
  14749. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14750. __func__, rc, detected, last_err);
  14751. if (err_str && last_err) {
  14752. if (detected)
  14753. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14754. "- Likely stuck low. Boot Error:",
  14755. last_err);
  14756. else
  14757. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14758. "- Likely stuck high. Boot Error:",
  14759. last_err);
  14760. }
  14761. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14762. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14763. last_err = PIP2_RSP_ERR_NONE;
  14764. if (slave_irq_toggled)
  14765. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14766. if (slave_bus_toggled) {
  14767. /* Leave as UNTEST if slave not detected */
  14768. if (detected)
  14769. *slave_bus_toggled = !last_err ? true : false;
  14770. }
  14771. if (slave_detect)
  14772. *slave_detect = detected;
  14773. if (boot_err)
  14774. *boot_err = last_err;
  14775. if (slave_irq_toggled && slave_bus_toggled)
  14776. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14777. __func__,
  14778. "Detected", detected,
  14779. "slave_irq_toggled", *slave_irq_toggled,
  14780. "slave_bus_toggled", *slave_bus_toggled);
  14781. return rc;
  14782. }
  14783. /*******************************************************************************
  14784. * FUNCTION: pt_bist_slave_xres_test
  14785. *
  14786. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14787. *
  14788. * This test will ensure there is a good TP_XRES connection between the
  14789. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14790. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14791. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14792. * it's reset sentinel.
  14793. *
  14794. * RETURN:
  14795. * 0 = Success
  14796. * !0 = Failure
  14797. *
  14798. * PARAMETERS:
  14799. * *dev - pointer to device structure
  14800. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14801. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14802. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14803. * *err_str - pointer to error string buffer
  14804. ******************************************************************************/
  14805. static int pt_bist_slave_xres_test(struct device *dev,
  14806. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14807. u8 *err_str)
  14808. {
  14809. struct pt_core_data *cd = dev_get_drvdata(dev);
  14810. u8 slave_detect = 0;
  14811. u8 boot_err = 0;
  14812. int rc = 0;
  14813. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14814. mutex_lock(&cd->system_lock);
  14815. cd->hid_reset_cmd_state = 1;
  14816. mutex_unlock(&cd->system_lock);
  14817. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14818. pt_hw_hard_reset(cd);
  14819. msleep(100);
  14820. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14821. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14822. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14823. if (!rc && *slave_irq_toggled == false) {
  14824. /*
  14825. * If the slave IRQ did not toggle, either the slave_detect
  14826. * bit was not set or we had a boot error. If the slave
  14827. * detect was not set the slave did not reset causing a boot
  14828. * error.
  14829. */
  14830. if (!slave_detect)
  14831. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14832. else
  14833. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14834. "- likely open or an IRQ issue. Boot Error:",
  14835. boot_err);
  14836. }
  14837. if (slave_xres_toggled) {
  14838. if (!rc)
  14839. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14840. else
  14841. *slave_xres_toggled = false;
  14842. }
  14843. return rc;
  14844. }
  14845. /*******************************************************************************
  14846. * FUNCTION: pt_bist_slave_bus_test
  14847. *
  14848. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14849. *
  14850. * This test will ensure a good SPI bus connection between the
  14851. * master DUT and the slave DUT(s). This bus connection is ensured by
  14852. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14853. * then there is a master/slave communication issue. Opening file 0 on
  14854. * the master will open it on the slave as well if the slave was detected.
  14855. *
  14856. * RETURN:
  14857. * 0 = Success
  14858. * !0 = Failure
  14859. *
  14860. * PARAMETERS:
  14861. * *dev - pointer to device structure
  14862. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14863. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14864. * *err_str - pointer to error string buffer
  14865. ******************************************************************************/
  14866. static int pt_bist_slave_bus_test(struct device *dev,
  14867. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14868. {
  14869. struct pt_core_data *cd = dev_get_drvdata(dev);
  14870. u8 mode = PT_MODE_UNKNOWN;
  14871. u8 bus_toggled = false;
  14872. u8 file_handle;
  14873. int result = 0;
  14874. int rc = 0;
  14875. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14876. if (rc) {
  14877. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14878. __func__, rc);
  14879. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14880. goto exit;
  14881. }
  14882. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14883. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14884. if (file_handle != PIP2_RAM_FILE) {
  14885. rc = -ENOENT;
  14886. bus_toggled = false;
  14887. pt_debug(dev, DL_ERROR,
  14888. "%s Failed to open bin file\n", __func__);
  14889. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14890. goto exit;
  14891. } else {
  14892. bus_toggled = true;
  14893. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14894. pt_debug(dev, DL_ERROR,
  14895. "%s: File Close failed, file_handle=%d\n",
  14896. __func__, file_handle);
  14897. }
  14898. }
  14899. exit:
  14900. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14901. if (slave_irq_toggled)
  14902. *slave_irq_toggled = bus_toggled;
  14903. if (slave_bus_toggled)
  14904. *slave_bus_toggled = bus_toggled;
  14905. return rc;
  14906. }
  14907. /*******************************************************************************
  14908. * FUNCTION: pt_ttdl_bist_show
  14909. *
  14910. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14911. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14912. *
  14913. * NOTE: This function will reset the DUT and the startup_status bit
  14914. * mask. A pt_enum will be queued after completion.
  14915. *
  14916. * NOTE: The order of the net tests is done to optimize the time it takes
  14917. * to run. The first test is capable of verifying all nets, each subsequent
  14918. * test is only run if the previous was not able to see all nets toggle.
  14919. *
  14920. * RETURN:
  14921. * 0 = success
  14922. * !0 = failure
  14923. *
  14924. * PARAMETERS:
  14925. * *dev - pointer to device structure
  14926. * *attr - pointer to device attributes
  14927. * *buf - pointer to output buffer
  14928. ******************************************************************************/
  14929. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14930. struct device_attribute *attr, char *buf)
  14931. {
  14932. struct pt_core_data *cd = dev_get_drvdata(dev);
  14933. ssize_t ret;
  14934. char *bus_err_str = NULL;
  14935. char *irq_err_str = NULL;
  14936. char *xres_err_str = NULL;
  14937. char *slave_bus_err_str = NULL;
  14938. char *slave_irq_err_str = NULL;
  14939. char *slave_xres_err_str = NULL;
  14940. u8 tests;
  14941. int rc = 0;
  14942. int num_tests = 0;
  14943. int status = 1; /* 0 = Pass, !0 = fail */
  14944. u8 bus_toggled = 0x0F; /* default to untested */
  14945. u8 i2c_toggled = 0x0F; /* default to untested */
  14946. u8 spi_toggled = 0x0F; /* default to untested */
  14947. u8 irq_toggled = 0x0F; /* default to untested */
  14948. u8 xres_toggled = 0x0F; /* default to untested */
  14949. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14950. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14951. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14952. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14953. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14954. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14955. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14956. goto print_results;
  14957. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14958. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14959. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14960. if (cd->multi_chip) {
  14961. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14962. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14963. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14964. if (!slave_bus_err_str ||
  14965. !slave_irq_err_str ||
  14966. !slave_xres_err_str)
  14967. goto print_results;
  14968. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14969. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14970. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14971. }
  14972. /* Turn off the TTDL WD during the test */
  14973. pt_stop_wd_timer(cd);
  14974. /* Shorten default PIP cmd timeout while running BIST */
  14975. cd->pip_cmd_timeout = 200;
  14976. /* Count the number of tests to run */
  14977. tests = cd->ttdl_bist_select;
  14978. while (tests) {
  14979. num_tests += tests & 1;
  14980. tests >>= 1;
  14981. }
  14982. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14983. __func__, cd->ttdl_bist_select, num_tests);
  14984. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14985. if (cd->flashless_dut) {
  14986. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14987. __func__);
  14988. mutex_lock(&cd->system_lock);
  14989. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14990. mutex_unlock(&cd->system_lock);
  14991. }
  14992. /* --------------- TP_XRES BIST TEST --------------- */
  14993. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14994. pt_debug(dev, DL_INFO,
  14995. "%s: ----- Start TP_XRES BIST -----", __func__);
  14996. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14997. &xres_toggled, xres_err_str);
  14998. /* Done if the rest of all nets toggled */
  14999. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  15000. goto host_nets_complete;
  15001. }
  15002. /* Flush bus in case a PIP response is waiting from previous test */
  15003. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15004. /* --------------- IRQ BIST TEST --------------- */
  15005. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  15006. pt_debug(dev, DL_INFO,
  15007. "%s: ----- Start IRQ BIST -----", __func__);
  15008. bus_toggled = 0xFF;
  15009. irq_toggled = 0xFF;
  15010. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  15011. &xres_toggled, irq_err_str);
  15012. /* If this net failed clear results from previous net */
  15013. if (irq_toggled != 1) {
  15014. xres_toggled = 0x0F;
  15015. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15016. }
  15017. if (bus_toggled == 1 && irq_toggled == 1)
  15018. goto host_nets_complete;
  15019. }
  15020. /* Flush bus in case a PIP response is waiting from previous test */
  15021. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15022. /* --------------- BUS BIST TEST --------------- */
  15023. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15024. pt_debug(dev, DL_INFO,
  15025. "%s: ----- Start BUS BIST -----", __func__);
  15026. bus_toggled = 0xFF;
  15027. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15028. /* If this net failed clear results from previous net */
  15029. if (bus_toggled == 0) {
  15030. irq_toggled = 0x0F;
  15031. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15032. }
  15033. }
  15034. host_nets_complete:
  15035. /* --------------- SLAVE XRES BIST TEST --------------- */
  15036. if (cd->multi_chip &&
  15037. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15038. pt_debug(dev, DL_INFO,
  15039. "%s: ----- Start Slave XRES BIST -----", __func__);
  15040. slave_xres_toggled = 0xFF;
  15041. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15042. &slave_bus_toggled, &slave_xres_toggled,
  15043. slave_xres_err_str);
  15044. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15045. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15046. goto print_results;
  15047. }
  15048. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15049. if (cd->multi_chip &&
  15050. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15051. pt_debug(dev, DL_INFO,
  15052. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15053. slave_irq_toggled = 0xFF;
  15054. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15055. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15056. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15057. __func__, slave_irq_toggled);
  15058. if (slave_irq_toggled == 1) {
  15059. slave_bus_toggled = 1;
  15060. goto print_results;
  15061. }
  15062. }
  15063. /* --------------- SLAVE BUS BIST TEST --------------- */
  15064. if (cd->multi_chip &&
  15065. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15066. pt_debug(dev, DL_INFO,
  15067. "%s: ----- Start Slave BUS BIST -----", __func__);
  15068. slave_bus_toggled = 0xFF;
  15069. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15070. &slave_bus_toggled, slave_bus_err_str);
  15071. }
  15072. print_results:
  15073. /* Restore PIP command timeout */
  15074. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15075. /*
  15076. * We're done! - Perform a hard XRES toggle, allowing BL
  15077. * to load FW if there is any in Flash
  15078. */
  15079. mutex_lock(&cd->system_lock);
  15080. cd->hid_reset_cmd_state = 0;
  15081. mutex_unlock(&cd->system_lock);
  15082. pt_debug(dev, DL_INFO,
  15083. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15084. if (cd->flashless_dut) {
  15085. /*
  15086. * For Flashless solution, FW update is triggered after BL is
  15087. * seen that several miliseconds delay is needed.
  15088. */
  15089. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15090. __func__);
  15091. mutex_lock(&cd->system_lock);
  15092. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15093. mutex_unlock(&cd->system_lock);
  15094. /* Reset DUT and wait 100ms to see if loader started */
  15095. pt_hw_hard_reset(cd);
  15096. msleep(100);
  15097. if (cd->fw_updating) {
  15098. pt_debug(dev, DL_INFO,
  15099. "%s: ----- BIST Wait FW Loading ----",
  15100. __func__);
  15101. rc = _pt_request_wait_for_enum_state(
  15102. dev, 4000, STARTUP_STATUS_COMPLETE);
  15103. }
  15104. } else {
  15105. if (cd->mode == PT_MODE_BOOTLOADER) {
  15106. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15107. if (rc) {
  15108. pt_debug(dev, DL_ERROR,
  15109. "%s Failed to launch app\n", __func__);
  15110. rc = pt_hw_hard_reset(cd);
  15111. }
  15112. }
  15113. /*
  15114. * If FW exists the BL may have just started or will start soon,
  15115. * so the FW sentinel may be on it's way but with no FW it will
  15116. * not arrive, wait for it before deciding if we need to queue
  15117. * an enum.
  15118. */
  15119. rc = _pt_request_wait_for_enum_state(
  15120. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15121. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15122. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15123. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15124. __func__);
  15125. pt_queue_enum(cd);
  15126. rc = _pt_request_wait_for_enum_state(
  15127. dev, 2000, STARTUP_STATUS_COMPLETE);
  15128. }
  15129. }
  15130. msleep(20);
  15131. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15132. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15133. pt_start_wd_timer(cd);
  15134. /* Canned print if any memory allocation issues */
  15135. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15136. ret = scnprintf(buf, strlen(buf),
  15137. "Status: %d\n"
  15138. "I2C (SDA,SCL): [UNTEST]\n"
  15139. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15140. "IRQ connection: [UNTEST]\n"
  15141. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15142. if (cd->multi_chip) {
  15143. ret += scnprintf(buf + ret, strlen(buf),
  15144. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15145. "I/P IRQ connection: [UNTEST]\n"
  15146. "I/P TP_XRES connection: [UNTEST]\n");
  15147. }
  15148. } else {
  15149. status = 0;
  15150. if (bus_toggled == 1)
  15151. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15152. if (irq_toggled == 1)
  15153. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15154. if (xres_toggled == 1)
  15155. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15156. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15157. status += bus_toggled;
  15158. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15159. status += irq_toggled;
  15160. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15161. status += xres_toggled;
  15162. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15163. __func__, status, bus_toggled, irq_toggled,
  15164. xres_toggled);
  15165. if (cd->multi_chip) {
  15166. if (slave_irq_toggled == 1)
  15167. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15168. if (slave_xres_toggled == 1)
  15169. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15170. if (slave_bus_toggled == 1)
  15171. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15172. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15173. status += slave_bus_toggled;
  15174. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15175. status += slave_irq_toggled;
  15176. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15177. status += slave_xres_toggled;
  15178. pt_debug(dev, DL_ERROR,
  15179. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15180. __func__, status, bus_toggled, irq_toggled,
  15181. xres_toggled, slave_bus_toggled,
  15182. slave_irq_toggled, slave_xres_toggled);
  15183. }
  15184. if (cd->bus_ops->bustype == BUS_I2C)
  15185. i2c_toggled = bus_toggled;
  15186. else
  15187. spi_toggled = bus_toggled;
  15188. ret = scnprintf(buf, strlen(buf),
  15189. "Status: %d\n"
  15190. "I2C (SDA,SCL): %s %s\n"
  15191. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15192. "IRQ connection: %s %s\n"
  15193. "TP_XRES connection: %s %s\n",
  15194. status == num_tests ? 0 : 1,
  15195. i2c_toggled == 0x0F ? "[UNTEST]" :
  15196. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15197. i2c_toggled == 0x0F ? "" : bus_err_str,
  15198. spi_toggled == 0x0F ? "[UNTEST]" :
  15199. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15200. spi_toggled == 0x0F ? "" : bus_err_str,
  15201. irq_toggled == 0x0F ? "[UNTEST]" :
  15202. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15203. irq_err_str,
  15204. xres_toggled == 0x0F ? "[UNTEST]" :
  15205. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15206. xres_err_str);
  15207. if (cd->multi_chip) {
  15208. ret += scnprintf(buf + ret, strlen(buf),
  15209. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15210. "I/P IRQ connection: %s %s\n"
  15211. "I/P TP_XRES connection: %s %s\n",
  15212. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15213. slave_bus_toggled == 1 ? "[ OK ]" :
  15214. "[FAILED]", slave_bus_err_str,
  15215. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15216. slave_irq_toggled == 1 ? "[ OK ]" :
  15217. "[FAILED]", slave_irq_err_str,
  15218. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15219. slave_xres_toggled == 1 ? "[ OK ]" :
  15220. "[FAILED]", slave_xres_err_str);
  15221. }
  15222. }
  15223. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15224. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15225. __func__, cd->startup_status);
  15226. kfree(bus_err_str);
  15227. kfree(irq_err_str);
  15228. kfree(xres_err_str);
  15229. return ret;
  15230. }
  15231. /*******************************************************************************
  15232. * FUNCTION: pt_ttdl_bist_store
  15233. *
  15234. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15235. *
  15236. * RETURN: Size of passed in buffer if successful
  15237. *
  15238. * PARAMETERS:
  15239. * *dev - pointer to device structure
  15240. * *attr - pointer to device attributes
  15241. * *buf - pointer to command buffer
  15242. * size - size of buf
  15243. ******************************************************************************/
  15244. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15245. struct device_attribute *attr, const char *buf, size_t size)
  15246. {
  15247. struct pt_core_data *cd = dev_get_drvdata(dev);
  15248. u32 input_data[2] = {0};
  15249. int length;
  15250. int rc = 0;
  15251. /* Maximum input of one value */
  15252. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15253. ARRAY_SIZE(input_data));
  15254. if (length != 1) {
  15255. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15256. __func__);
  15257. rc = -EINVAL;
  15258. goto exit;
  15259. } else {
  15260. mutex_lock(&cd->system_lock);
  15261. cd->ttdl_bist_select = input_data[0];
  15262. mutex_unlock(&cd->system_lock);
  15263. }
  15264. exit:
  15265. if (rc)
  15266. return rc;
  15267. return size;
  15268. }
  15269. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15270. pt_ttdl_bist_store);
  15271. /*******************************************************************************
  15272. * FUNCTION: pt_flush_bus_store
  15273. *
  15274. * SUMMARY: Store method for the flush_bus sysfs node.
  15275. *
  15276. * RETURN: Size of passed in buffer if successful
  15277. *
  15278. * PARAMETERS:
  15279. * *dev - pointer to device structure
  15280. * *attr - pointer to device attributes
  15281. * *buf - pointer to command buffer
  15282. * size - size of buf
  15283. ******************************************************************************/
  15284. static ssize_t pt_flush_bus_store(struct device *dev,
  15285. struct device_attribute *attr, const char *buf, size_t size)
  15286. {
  15287. struct pt_core_data *cd = dev_get_drvdata(dev);
  15288. u32 input_data[2] = {0};
  15289. int length;
  15290. int rc = 0;
  15291. /* Maximum input of one value */
  15292. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15293. ARRAY_SIZE(input_data));
  15294. if (length != 1) {
  15295. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15296. __func__);
  15297. rc = -EINVAL;
  15298. goto exit;
  15299. }
  15300. mutex_lock(&cd->system_lock);
  15301. if (input_data[0] == 0)
  15302. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15303. else if (input_data[0] == 1)
  15304. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15305. else
  15306. rc = -EINVAL;
  15307. mutex_unlock(&cd->system_lock);
  15308. exit:
  15309. if (rc)
  15310. return rc;
  15311. return size;
  15312. }
  15313. /*******************************************************************************
  15314. * FUNCTION: pt_flush_bus_show
  15315. *
  15316. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15317. * based on either the size of the first two bytes or a blind 256 bytes.
  15318. *
  15319. * RETURN:
  15320. * 0 = success
  15321. * !0 = failure
  15322. *
  15323. * PARAMETERS:
  15324. * *dev - pointer to device structure
  15325. * *attr - pointer to device attributes
  15326. * *buf - pointer to output buffer
  15327. ******************************************************************************/
  15328. static ssize_t pt_flush_bus_show(struct device *dev,
  15329. struct device_attribute *attr, char *buf)
  15330. {
  15331. ssize_t ret = 0;
  15332. ssize_t bytes = 0;
  15333. struct pt_core_data *cd = dev_get_drvdata(dev);
  15334. mutex_lock(&cd->system_lock);
  15335. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15336. ret = scnprintf(buf, strlen(buf),
  15337. "Status: 0\n"
  15338. "%s: %zd\n",
  15339. "Bytes flushed", bytes);
  15340. mutex_unlock(&cd->system_lock);
  15341. return ret;
  15342. }
  15343. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15344. pt_flush_bus_store);
  15345. /*******************************************************************************
  15346. * FUNCTION: pt_pip2_ping_test_store
  15347. *
  15348. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15349. *
  15350. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15351. * someone may want to test sending invalid packet lengths so any values
  15352. * up to 255 are allowed.
  15353. *
  15354. * RETURN: Size of passed in buffer if successful
  15355. *
  15356. * PARAMETERS:
  15357. * *dev - pointer to device structure
  15358. * *attr - pointer to device attributes
  15359. * *buf - pointer to command buffer
  15360. * size - size of buf
  15361. ******************************************************************************/
  15362. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15363. struct device_attribute *attr, const char *buf, size_t size)
  15364. {
  15365. struct pt_core_data *cd = dev_get_drvdata(dev);
  15366. u32 input_data[2];
  15367. int length;
  15368. int rc = 0;
  15369. /* Maximum input of one value */
  15370. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15371. ARRAY_SIZE(input_data));
  15372. if (length != 1) {
  15373. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15374. __func__);
  15375. rc = -EINVAL;
  15376. goto exit;
  15377. }
  15378. mutex_lock(&cd->system_lock);
  15379. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15380. cd->ping_test_size = input_data[0];
  15381. else
  15382. rc = -EINVAL;
  15383. mutex_unlock(&cd->system_lock);
  15384. exit:
  15385. if (rc)
  15386. return rc;
  15387. return size;
  15388. }
  15389. /*******************************************************************************
  15390. * FUNCTION: pt_ping_test_show
  15391. *
  15392. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15393. * PING command and ramps up the optional payload from 0 to
  15394. * ping_test_size.
  15395. * The max payload size is 247:
  15396. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15397. *
  15398. * RETURN:
  15399. * 0 = success
  15400. * !0 = failure
  15401. *
  15402. * PARAMETERS:
  15403. * *dev - pointer to device structure
  15404. * *attr - pointer to device attributes
  15405. * *buf - pointer to output buffer
  15406. ******************************************************************************/
  15407. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15408. struct device_attribute *attr, char *buf)
  15409. {
  15410. struct pt_core_data *cd = dev_get_drvdata(dev);
  15411. ssize_t ret;
  15412. int last_packet_size;
  15413. int rc = 0;
  15414. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15415. if (rc) {
  15416. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15417. return ret;
  15418. }
  15419. ret = scnprintf(buf, strlen(buf),
  15420. "Status: %d\n"
  15421. "PING payload test passed with packet sizes 0 - %d\n",
  15422. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15423. last_packet_size);
  15424. return ret;
  15425. }
  15426. /*******************************************************************************
  15427. * FUNCTION: pt_t_refresh_store
  15428. *
  15429. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15430. * in integer as the number of interrupts to count. A timer is started to
  15431. * calculate the total time it takes to see that number of interrupts.
  15432. *
  15433. * RETURN: Size of passed in buffer if successful
  15434. *
  15435. * PARAMETERS:
  15436. * *dev - pointer to device structure
  15437. * *attr - pointer to device attributes
  15438. * *buf - pointer to buffer that hold the command parameters
  15439. * size - size of buf
  15440. ******************************************************************************/
  15441. static ssize_t pt_t_refresh_store(struct device *dev,
  15442. struct device_attribute *attr, const char *buf, size_t size)
  15443. {
  15444. struct pt_core_data *cd = dev_get_drvdata(dev);
  15445. u32 input_data[2];
  15446. int length;
  15447. int rc = 0;
  15448. /* Maximum input of one value */
  15449. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15450. ARRAY_SIZE(input_data));
  15451. if (length != 1) {
  15452. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15453. __func__);
  15454. rc = -EINVAL;
  15455. goto exit;
  15456. }
  15457. mutex_lock(&cd->system_lock);
  15458. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15459. input_data[0]);
  15460. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15461. cd->t_refresh_total = input_data[0];
  15462. cd->t_refresh_count = 0;
  15463. cd->t_refresh_active = 1;
  15464. } else {
  15465. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15466. rc = -EINVAL;
  15467. }
  15468. mutex_unlock(&cd->system_lock);
  15469. exit:
  15470. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15471. if (rc)
  15472. return rc;
  15473. return size;
  15474. }
  15475. /*******************************************************************************
  15476. * FUNCTION: pt_t_refresh_show
  15477. *
  15478. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15479. * of the T-Refresh timer counting the time it takes to see a user defined
  15480. * number of interrupts.
  15481. *
  15482. * RETURN:
  15483. * 0 = success
  15484. * !0 = failure
  15485. *
  15486. * PARAMETERS:
  15487. * *dev - pointer to device structure
  15488. * *attr - pointer to device attributes
  15489. * *buf - pointer to output buffer
  15490. ******************************************************************************/
  15491. static ssize_t pt_t_refresh_show(struct device *dev,
  15492. struct device_attribute *attr, char *buf)
  15493. {
  15494. ssize_t ret = 0;
  15495. struct pt_core_data *cd = dev_get_drvdata(dev);
  15496. u32 whole;
  15497. u16 fraction;
  15498. mutex_lock(&cd->system_lock);
  15499. /* Check if we have counted the number requested */
  15500. if (cd->t_refresh_count != cd->t_refresh_total) {
  15501. ret = scnprintf(buf, strlen(buf),
  15502. "Status: 0\n"
  15503. "%s: %d\n",
  15504. "Still counting... current IRQ count",
  15505. cd->t_refresh_count);
  15506. } else {
  15507. /* Ensure T-Refresh is de-activated */
  15508. cd->t_refresh_active = 0;
  15509. whole = cd->t_refresh_time / cd->t_refresh_count;
  15510. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15511. fraction = fraction * 1000 / cd->t_refresh_count;
  15512. ret = scnprintf(buf, strlen(buf),
  15513. "Status: 0\n"
  15514. "%s: %d\n"
  15515. "%s: %d\n"
  15516. "%s: %d\n"
  15517. "%s: %d.%02d\n",
  15518. "Requested IRQ Count ", cd->t_refresh_total,
  15519. "IRQ Counted ", cd->t_refresh_count,
  15520. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15521. "Average T-Refresh (ms) ", whole, fraction);
  15522. }
  15523. mutex_unlock(&cd->system_lock);
  15524. return ret;
  15525. }
  15526. /*******************************************************************************
  15527. * FUNCTION: pt_dut_status_show
  15528. *
  15529. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15530. * more items such as operation mode,easywake state are added in the future.
  15531. *
  15532. * RETURN: Char buffer with printed scan status information
  15533. *
  15534. * PARAMETERS:
  15535. * *dev - pointer to device structure
  15536. * *attr - pointer to device attributes
  15537. * *buf - pointer to output buffer
  15538. ******************************************************************************/
  15539. static ssize_t pt_dut_status_show(struct device *dev,
  15540. struct device_attribute *attr, char *buf)
  15541. {
  15542. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15543. u8 mode = PT_MODE_UNKNOWN;
  15544. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15545. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15546. struct pt_core_data *cd = dev_get_drvdata(dev);
  15547. ssize_t ret;
  15548. u16 calculated_crc = 0;
  15549. u16 stored_crc = 0;
  15550. u8 status;
  15551. int rc = 0;
  15552. /* In STANDBY the DUT will not repond to any PIP cmd */
  15553. if (cd->fw_sys_mode_in_standby_state) {
  15554. mode = PT_MODE_OPERATIONAL;
  15555. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15556. goto print_limited_results;
  15557. }
  15558. /* Retrieve mode and FW system mode which can only be 0-4 */
  15559. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15560. if (rc || mode == PT_MODE_UNKNOWN) {
  15561. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15562. "%s: %d\n"
  15563. "%s: n/a\n"
  15564. "%s: n/a\n"
  15565. "%s: n/a\n"
  15566. "%s: n/a\n",
  15567. "Status", rc,
  15568. "Active Exec ",
  15569. "FW System Mode ",
  15570. "Stored CRC ",
  15571. "Calculated CRC ");
  15572. return ret;
  15573. } else {
  15574. if (mode == PT_MODE_OPERATIONAL) {
  15575. if (sys_mode > FW_SYS_MODE_MAX)
  15576. sys_mode = FW_SYS_MODE_UNDEFINED;
  15577. if (sys_mode != FW_SYS_MODE_TEST)
  15578. goto print_limited_results;
  15579. rc = pt_pip_verify_config_block_crc_(cd,
  15580. PT_TCH_PARM_EBID, &status,
  15581. &calculated_crc, &stored_crc);
  15582. if (rc)
  15583. goto print_limited_results;
  15584. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15585. "%s: %d\n"
  15586. "%s: %s\n"
  15587. "%s: %s\n"
  15588. "%s: 0x%04X\n"
  15589. "%s: 0x%04X\n",
  15590. "Status", rc,
  15591. "Active Exec ", "FW",
  15592. "FW System Mode ", outputstring[sys_mode],
  15593. "Stored CRC ", stored_crc,
  15594. "Calculated CRC ", calculated_crc);
  15595. return ret;
  15596. } else {
  15597. /* When in BL or unknon mode Active Exec is "n/a" */
  15598. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15599. }
  15600. }
  15601. print_limited_results:
  15602. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15603. "%s: %d\n"
  15604. "%s: %s\n"
  15605. "%s: %s\n"
  15606. "%s: n/a\n"
  15607. "%s: n/a\n",
  15608. "Status", rc,
  15609. "Active Exec ",
  15610. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15611. "FW System Mode ", outputstring[sys_mode],
  15612. "Stored CRC ",
  15613. "Calculated CRC ");
  15614. return ret;
  15615. }
  15616. #endif /* TTDL_DIAGNOSTICS */
  15617. /*******************************************************************************
  15618. * Structures of sysfs attributes for all DUT dependent sysfs node
  15619. ******************************************************************************/
  15620. static struct attribute *early_attrs[] = {
  15621. &dev_attr_hw_version.attr,
  15622. &dev_attr_drv_version.attr,
  15623. &dev_attr_drv_ver.attr,
  15624. &dev_attr_fw_version.attr,
  15625. &dev_attr_sysinfo.attr,
  15626. &dev_attr_pip2_cmd_rsp.attr,
  15627. &dev_attr_command.attr,
  15628. &dev_attr_drv_debug.attr,
  15629. &dev_attr_hw_reset.attr,
  15630. &dev_attr_response.attr,
  15631. &dev_attr_ttdl_restart.attr,
  15632. #ifdef TTDL_DIAGNOSTICS
  15633. &dev_attr_ttdl_status.attr,
  15634. &dev_attr_pip2_enter_bl.attr,
  15635. &dev_attr_pip2_exit_bl.attr,
  15636. &dev_attr_err_gpio.attr,
  15637. &dev_attr_flush_bus.attr,
  15638. &dev_attr_ttdl_bist.attr,
  15639. #endif /* TTDL_DIAGNOSTICS */
  15640. NULL,
  15641. };
  15642. static struct attribute_group early_attr_group = {
  15643. .attrs = early_attrs,
  15644. };
  15645. static struct device_attribute pip2_attributes[] = {
  15646. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15647. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15648. #ifdef TTDL_DIAGNOSTICS
  15649. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15650. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15651. pt_pip2_ping_test_store),
  15652. #endif
  15653. };
  15654. static struct device_attribute attributes[] = {
  15655. __ATTR(dut_debug, 0644,
  15656. pt_dut_debug_show, pt_drv_debug_store),
  15657. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15658. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15659. __ATTR(get_param, 0644,
  15660. pt_get_param_show, pt_get_param_store),
  15661. #ifdef EASYWAKE_TSG6
  15662. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15663. pt_easy_wakeup_gesture_store),
  15664. __ATTR(easy_wakeup_gesture_id, 0444,
  15665. pt_easy_wakeup_gesture_id_show, NULL),
  15666. __ATTR(easy_wakeup_gesture_data, 0444,
  15667. pt_easy_wakeup_gesture_data_show, NULL),
  15668. #endif
  15669. #ifdef TTDL_DIAGNOSTICS
  15670. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15671. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15672. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15673. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15674. #endif /* TTDL_DIAGNOSTICS */
  15675. };
  15676. /*******************************************************************************
  15677. * FUNCTION: add_sysfs_interfaces
  15678. *
  15679. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15680. *
  15681. * RETURN:
  15682. * 0 = success
  15683. * !0 = failure
  15684. *
  15685. * PARAMETERS:
  15686. * *dev - pointer to device structure
  15687. ******************************************************************************/
  15688. static int add_sysfs_interfaces(struct device *dev)
  15689. {
  15690. int i;
  15691. int j = 0;
  15692. struct pt_core_data *cd = dev_get_drvdata(dev);
  15693. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15694. if (device_create_file(dev, attributes + i))
  15695. goto undo;
  15696. }
  15697. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15698. __func__, cd->active_dut_generation);
  15699. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15700. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15701. if (device_create_file(dev, pip2_attributes + j))
  15702. goto undo;
  15703. }
  15704. }
  15705. return 0;
  15706. undo:
  15707. for (i--; i >= 0; i--)
  15708. device_remove_file(dev, attributes + i);
  15709. for (j--; j >= 0; j--)
  15710. device_remove_file(dev, pip2_attributes + j);
  15711. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15712. __func__);
  15713. return -ENODEV;
  15714. }
  15715. /*******************************************************************************
  15716. * FUNCTION: remove_sysfs_interfaces
  15717. *
  15718. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15719. *
  15720. * RETURN: void
  15721. *
  15722. * PARAMETERS:
  15723. * *dev - pointer to device structure
  15724. ******************************************************************************/
  15725. static void remove_sysfs_interfaces(struct device *dev)
  15726. {
  15727. int i;
  15728. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15729. device_remove_file(dev, attributes + i);
  15730. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15731. device_remove_file(dev, pip2_attributes + i);
  15732. }
  15733. /*******************************************************************************
  15734. * FUNCTION: remove_sysfs_and_modules
  15735. *
  15736. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15737. *
  15738. * RETURN: void
  15739. *
  15740. * PARAMETERS:
  15741. * *dev - pointer to device structure
  15742. ******************************************************************************/
  15743. static void remove_sysfs_and_modules(struct device *dev)
  15744. {
  15745. struct pt_core_data *cd = dev_get_drvdata(dev);
  15746. /* Queued work should be removed before to release loader module */
  15747. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15748. pt_release_modules(cd);
  15749. pt_btn_release(dev);
  15750. pt_mt_release(dev);
  15751. remove_sysfs_interfaces(dev);
  15752. }
  15753. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15754. {
  15755. int retval;
  15756. /* Get pinctrl if target uses pinctrl */
  15757. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15758. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15759. retval = PTR_ERR(cd->ts_pinctrl);
  15760. dev_dbg(cd->dev,
  15761. "Target does not use pinctrl %d\n", retval);
  15762. goto err_pinctrl_get;
  15763. }
  15764. cd->pinctrl_state_active
  15765. = pinctrl_lookup_state(cd->ts_pinctrl,
  15766. PINCTRL_STATE_ACTIVE);
  15767. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15768. retval = PTR_ERR(cd->pinctrl_state_active);
  15769. dev_err(cd->dev,
  15770. "Can not lookup %s pinstate %d\n",
  15771. PINCTRL_STATE_ACTIVE, retval);
  15772. goto err_pinctrl_lookup;
  15773. }
  15774. cd->pinctrl_state_suspend
  15775. = pinctrl_lookup_state(cd->ts_pinctrl,
  15776. PINCTRL_STATE_SUSPEND);
  15777. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15778. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15779. dev_err(cd->dev,
  15780. "Can not lookup %s pinstate %d\n",
  15781. PINCTRL_STATE_SUSPEND, retval);
  15782. goto err_pinctrl_lookup;
  15783. }
  15784. cd->pinctrl_state_release
  15785. = pinctrl_lookup_state(cd->ts_pinctrl,
  15786. PINCTRL_STATE_RELEASE);
  15787. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15788. retval = PTR_ERR(cd->pinctrl_state_release);
  15789. dev_dbg(cd->dev,
  15790. "Can not lookup %s pinstate %d\n",
  15791. PINCTRL_STATE_RELEASE, retval);
  15792. }
  15793. return 0;
  15794. err_pinctrl_lookup:
  15795. devm_pinctrl_put(cd->ts_pinctrl);
  15796. err_pinctrl_get:
  15797. cd->ts_pinctrl = NULL;
  15798. return retval;
  15799. }
  15800. /*******************************************************************************
  15801. *******************************************************************************
  15802. * FUNCTION: pt_probe
  15803. *
  15804. * SUMMARY: Probe of the core module.
  15805. *
  15806. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15807. * probe functionality is split into two functions; pt_probe() and
  15808. * pt_probe_complete(). the initial setup is done in this function which
  15809. * then creates a WORK task which runs after the probe timer expires. This
  15810. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15811. *
  15812. * RETURN:
  15813. * 0 = success
  15814. * !0 = failure
  15815. *
  15816. * PARAMETERS:
  15817. * *ops - pointer to the bus
  15818. * *dev - pointer to the device structure
  15819. * irq - IRQ
  15820. * xfer_buf_size - size of the buffer
  15821. ******************************************************************************/
  15822. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15823. u16 irq, size_t xfer_buf_size)
  15824. {
  15825. struct pt_core_data *cd;
  15826. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15827. enum pt_atten_type type;
  15828. struct i2c_client *client = to_i2c_client(dev);
  15829. int rc = 0;
  15830. u8 pip_ver_major;
  15831. u8 pip_ver_minor;
  15832. u32 status = STARTUP_STATUS_START;
  15833. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15834. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15835. __func__);
  15836. rc = -ENODEV;
  15837. goto error_no_pdata;
  15838. }
  15839. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15840. if (!pdata->core_pdata->power) {
  15841. pt_debug(dev, DL_ERROR,
  15842. "%s: Missing platform data function\n",
  15843. __func__);
  15844. rc = -ENODEV;
  15845. goto error_no_pdata;
  15846. }
  15847. }
  15848. /* get context and debug print buffers */
  15849. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15850. if (!cd) {
  15851. rc = -ENOMEM;
  15852. goto error_no_pdata;
  15853. }
  15854. /* Initialize device info */
  15855. cd->dev = dev;
  15856. cd->pdata = pdata;
  15857. cd->cpdata = pdata->core_pdata;
  15858. cd->bus_ops = ops;
  15859. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15860. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15861. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15862. cd->hw_detected = false;
  15863. cd->pip2_prot_active = false;
  15864. cd->pip2_send_user_cmd = false;
  15865. cd->bl_pip_ver_ready = false;
  15866. cd->app_pip_ver_ready = false;
  15867. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15868. cd->get_param_id = 0;
  15869. cd->watchdog_enabled = 0;
  15870. cd->startup_retry_count = 0;
  15871. cd->core_probe_complete = 0;
  15872. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15873. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15874. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15875. cd->flashless_dut = 0;
  15876. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15877. cd->bl_with_no_int = 0;
  15878. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15879. cd->multi_chip = PT_FEATURE_DISABLE;
  15880. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15881. cd->sleep_state = SS_SLEEP_NONE;
  15882. cd->quick_boot = false;
  15883. cd->drv_debug_suspend = false;
  15884. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15885. cd->set_dut_generation = true;
  15886. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15887. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15888. cd->set_dut_generation = true;
  15889. cd->active_dut_generation = DUT_PIP1_ONLY;
  15890. } else {
  15891. cd->set_dut_generation = false;
  15892. cd->active_dut_generation = DUT_UNKNOWN;
  15893. }
  15894. /* Initialize with platform data */
  15895. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15896. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15897. cd->hid_cmd_state = 1;
  15898. cd->fw_updating = false;
  15899. cd->multi_chip = 0;
  15900. #ifdef TTDL_DIAGNOSTICS
  15901. cd->t_refresh_active = 0;
  15902. cd->t_refresh_count = 0;
  15903. cd->pip2_crc_error_count = 0;
  15904. cd->wd_xres_count = 0;
  15905. cd->bl_retry_packet_count = 0;
  15906. cd->file_erase_timeout_count = 0;
  15907. cd->show_tt_data = false;
  15908. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15909. cd->err_gpio = 0;
  15910. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15911. cd->ttdl_bist_select = 0x07;
  15912. cd->force_pip2_seq = 0;
  15913. #endif /* TTDL_DIAGNOSTICS */
  15914. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15915. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15916. sizeof(PT_PIP2_BIN_FILE_PATH));
  15917. pt_init_hid_descriptor(&cd->hid_desc);
  15918. /* Read and store the descriptor lengths */
  15919. cd->hid_core.hid_report_desc_len =
  15920. le16_to_cpu(cd->hid_desc.report_desc_len);
  15921. cd->hid_core.hid_max_input_len =
  15922. le16_to_cpu(cd->hid_desc.max_input_len);
  15923. cd->hid_core.hid_max_output_len =
  15924. le16_to_cpu(cd->hid_desc.max_output_len);
  15925. /* Initialize mutexes and spinlocks */
  15926. mutex_init(&cd->module_list_lock);
  15927. mutex_init(&cd->system_lock);
  15928. mutex_init(&cd->sysfs_lock);
  15929. mutex_init(&cd->ttdl_restart_lock);
  15930. mutex_init(&cd->firmware_class_lock);
  15931. spin_lock_init(&cd->spinlock);
  15932. /* Initialize module list */
  15933. INIT_LIST_HEAD(&cd->module_list);
  15934. /* Initialize attention lists */
  15935. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15936. INIT_LIST_HEAD(&cd->atten_list[type]);
  15937. /* Initialize parameter list */
  15938. INIT_LIST_HEAD(&cd->param_list);
  15939. /* Initialize wait queue */
  15940. init_waitqueue_head(&cd->wait_q);
  15941. rc = pt_ts_pinctrl_init(cd);
  15942. if (!rc && cd->ts_pinctrl) {
  15943. /*
  15944. * Pinctrl handle is optional. If pinctrl handle is found
  15945. * let pins to be configured in active state. If not
  15946. * found continue further without error.
  15947. */
  15948. rc = pinctrl_select_state(cd->ts_pinctrl,
  15949. cd->pinctrl_state_active);
  15950. if (rc < 0)
  15951. dev_err(&client->dev, "failed to select pin to active state\n");
  15952. }
  15953. rc = pt_get_regulator(cd, true);
  15954. if (rc) {
  15955. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15956. goto error_alloc_data;
  15957. }
  15958. rc = pt_enable_regulator(cd, true);
  15959. if (rc) {
  15960. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15961. goto error_get_regulator;
  15962. }
  15963. /* Initialize works */
  15964. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15965. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15966. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15967. /* Initialize HID specific data */
  15968. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15969. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15970. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15971. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15972. cd->hid_core.hid_desc_register =
  15973. cpu_to_le16(cd->cpdata->hid_desc_register);
  15974. /* Set platform easywake value */
  15975. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15976. #ifdef CONFIG_DRM
  15977. /* Setup active dsi panel */
  15978. active_panel = cd->cpdata->active_panel;
  15979. #endif
  15980. /* Set platform panel_id value */
  15981. cd->panel_id_support = cd->cpdata->panel_id_support;
  15982. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15983. /* Set Panel ID to default to 0 */
  15984. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15985. else
  15986. /* Set Panel ID to Not Enabled */
  15987. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15988. /* Initialize hw_version default to FFFF.FFFF.FF */
  15989. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15990. dev_set_drvdata(dev, cd);
  15991. /* PtSBC builds will call this function in pt_probe_complete() */
  15992. pt_add_core(dev);
  15993. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15994. if (rc) {
  15995. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15996. __func__);
  15997. goto error_enable_regulator;
  15998. }
  15999. /*
  16000. * Save the pointer to a global value, which will be used
  16001. * in ttdl_restart function
  16002. */
  16003. cd->bus_ops = ops;
  16004. /*
  16005. * When the IRQ GPIO is not direclty accessible and no function is
  16006. * defined to get the IRQ status, the IRQ passed in must be assigned
  16007. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16008. */
  16009. if (!cd->cpdata->irq_stat) {
  16010. cd->irq = irq;
  16011. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  16012. __func__, cd->irq);
  16013. }
  16014. /* Call platform init function before setting up the GPIO's */
  16015. if (cd->cpdata->init) {
  16016. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16017. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16018. } else {
  16019. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16020. __func__);
  16021. rc = 0;
  16022. }
  16023. if (rc < 0) {
  16024. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16025. __func__, rc);
  16026. }
  16027. /* Power on any needed regulator(s) */
  16028. if (cd->cpdata->setup_power) {
  16029. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16030. rc = cd->cpdata->setup_power(cd->cpdata,
  16031. PT_MT_POWER_ON, cd->dev);
  16032. } else {
  16033. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16034. __func__);
  16035. rc = 0;
  16036. }
  16037. if (rc < 0)
  16038. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16039. __func__, rc);
  16040. #ifdef TTDL_DIAGNOSTICS
  16041. cd->watchdog_irq_stuck_count = 0;
  16042. cd->bus_transmit_error_count = 0;
  16043. #endif /* TTDL_DIAGNOSTICS */
  16044. if (cd->cpdata->detect) {
  16045. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16046. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16047. pt_platform_detect_read);
  16048. if (!rc) {
  16049. cd->hw_detected = true;
  16050. pt_debug(cd->dev, DL_INFO,
  16051. "%s: HW detected\n", __func__);
  16052. } else {
  16053. cd->hw_detected = false;
  16054. pt_debug(cd->dev, DL_INFO,
  16055. "%s: No HW detected\n", __func__);
  16056. rc = -ENODEV;
  16057. goto error_detect;
  16058. }
  16059. } else {
  16060. pt_debug(dev, DL_ERROR,
  16061. "%s: PARADE No HW detect function pointer\n",
  16062. __func__);
  16063. /*
  16064. * "hw_reset" is not needed in the "if" statement,
  16065. * because "hw_reset" is already included in "hw_detect"
  16066. * function.
  16067. */
  16068. rc = pt_hw_hard_reset(cd);
  16069. if (rc)
  16070. pt_debug(cd->dev, DL_ERROR,
  16071. "%s: FAILED to execute HARD reset\n",
  16072. __func__);
  16073. }
  16074. if (cd->cpdata->setup_irq) {
  16075. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16076. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16077. if (rc) {
  16078. pt_debug(dev, DL_ERROR,
  16079. "%s: Error, couldn't setup IRQ\n", __func__);
  16080. goto error_setup_irq;
  16081. }
  16082. } else {
  16083. pt_debug(dev, DL_ERROR,
  16084. "%s: IRQ function pointer not setup\n",
  16085. __func__);
  16086. goto error_setup_irq;
  16087. }
  16088. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16089. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16090. (unsigned long)cd);
  16091. #else
  16092. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16093. #endif
  16094. pt_stop_wd_timer(cd);
  16095. #ifdef TTHE_TUNER_SUPPORT
  16096. mutex_init(&cd->tthe_lock);
  16097. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16098. 0644, NULL, cd, &tthe_debugfs_fops);
  16099. #endif
  16100. rc = device_init_wakeup(dev, 1);
  16101. if (rc < 0)
  16102. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16103. __func__, rc);
  16104. if (!enable_irq_wake(cd->irq)) {
  16105. cd->irq_wake = 1;
  16106. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16107. }
  16108. pm_runtime_get_noresume(dev);
  16109. pm_runtime_set_active(dev);
  16110. pm_runtime_enable(dev);
  16111. /* If IRQ asserted, read out all from buffer to release INT pin */
  16112. if (cd->cpdata->irq_stat) {
  16113. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16114. } else {
  16115. /* Force a read in case the reset sentinel already arrived */
  16116. rc = pt_read_input(cd);
  16117. if (!rc)
  16118. pt_parse_input(cd);
  16119. }
  16120. /* Without sleep DUT is not ready and will NAK the first write */
  16121. msleep(150);
  16122. /* Attempt to set the DUT generation if not yet set */
  16123. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16124. if (cd->bl_pip_ver_ready ||
  16125. (cd->app_pip_ver_ready &&
  16126. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16127. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16128. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16129. __func__, cd->active_dut_generation);
  16130. } else {
  16131. rc = _pt_detect_dut_generation(cd->dev,
  16132. &status, &cd->active_dut_generation,
  16133. &cd->mode);
  16134. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16135. || rc) {
  16136. pt_debug(cd->dev, DL_ERROR,
  16137. " === DUT Gen unknown, Skip Enum ===\n");
  16138. goto skip_enum;
  16139. }
  16140. }
  16141. }
  16142. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16143. &pip_ver_major, &pip_ver_minor);
  16144. if (pip_ver_major == 2) {
  16145. cd->bl_pip_ver_ready = true;
  16146. pt_debug(dev, DL_ERROR,
  16147. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16148. pip_ver_minor);
  16149. cd->hw_detected = true;
  16150. } else if (pip_ver_major == 1) {
  16151. cd->app_pip_ver_ready = true;
  16152. pt_debug(dev, DL_ERROR,
  16153. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16154. cd->hw_detected = true;
  16155. } else {
  16156. cd->sysinfo.ttdata.pip_ver_major = 0;
  16157. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16158. cd->app_pip_ver_ready = false;
  16159. cd->hw_detected = false;
  16160. pt_debug(dev, DL_ERROR,
  16161. " === PIP Version Not Detected, Skip Enum ===\n");
  16162. /* For legacy DUTS proceed, enum will attempt to launch app */
  16163. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16164. goto skip_enum;
  16165. }
  16166. rc = pt_enum_with_dut(cd, false, &status);
  16167. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16168. __func__, cd->startup_status, status);
  16169. if (rc == -ENODEV) {
  16170. pt_debug(cd->dev, DL_ERROR,
  16171. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16172. /* For PtSBC don't error out, allow TTDL to stay up */
  16173. rc = -EPROBE_DEFER;
  16174. goto error_after_startup;
  16175. }
  16176. /* Suspend scanning until probe is complete to avoid asyc touches */
  16177. pt_pip_suspend_scanning_(cd);
  16178. if (cd->hw_detected) {
  16179. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16180. __func__);
  16181. rc = add_sysfs_interfaces(dev);
  16182. if (rc < 0) {
  16183. pt_debug(dev, DL_ERROR,
  16184. "%s: Error, fail sysfs init\n", __func__);
  16185. goto error_after_startup;
  16186. }
  16187. } else {
  16188. pt_debug(dev, DL_ERROR,
  16189. "%s: No HW detected, sysfs interfaces not added\n",
  16190. __func__);
  16191. }
  16192. skip_enum:
  16193. pm_runtime_put_sync(dev);
  16194. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16195. rc = pt_mt_probe(dev);
  16196. if (rc < 0) {
  16197. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16198. __func__);
  16199. goto error_after_sysfs_create;
  16200. }
  16201. rc = pt_btn_probe(dev);
  16202. if (rc < 0) {
  16203. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16204. __func__);
  16205. goto error_after_startup_mt;
  16206. }
  16207. pt_probe_modules(cd);
  16208. #ifdef CONFIG_HAS_EARLYSUSPEND
  16209. pt_setup_early_suspend(cd);
  16210. #elif defined(CONFIG_DRM)
  16211. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16212. pt_setup_drm_notifier(cd);
  16213. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16214. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16215. #elif defined(CONFIG_FB)
  16216. pt_setup_fb_notifier(cd);
  16217. #endif
  16218. #ifdef NEED_SUSPEND_NOTIFIER
  16219. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16220. register_pm_notifier(&cd->pm_notifier);
  16221. #endif
  16222. pt_pip_resume_scanning_(cd);
  16223. mutex_lock(&cd->system_lock);
  16224. cd->startup_status |= status;
  16225. cd->core_probe_complete = 1;
  16226. mutex_unlock(&cd->system_lock);
  16227. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16228. __func__);
  16229. return 0;
  16230. error_after_startup_mt:
  16231. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16232. pt_mt_release(dev);
  16233. error_after_sysfs_create:
  16234. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16235. pm_runtime_disable(dev);
  16236. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16237. device_wakeup_disable(dev);
  16238. #endif
  16239. device_init_wakeup(dev, 0);
  16240. cancel_work_sync(&cd->enum_work);
  16241. pt_stop_wd_timer(cd);
  16242. pt_free_si_ptrs(cd);
  16243. remove_sysfs_interfaces(dev);
  16244. error_after_startup:
  16245. pr_err("%s PARADE error_after_startup\n", __func__);
  16246. del_timer(&cd->watchdog_timer);
  16247. if (cd->cpdata->setup_irq)
  16248. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16249. error_setup_irq:
  16250. error_detect:
  16251. if (cd->cpdata->init)
  16252. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16253. if (cd->cpdata->setup_power)
  16254. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16255. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16256. error_enable_regulator:
  16257. pt_del_core(dev);
  16258. dev_set_drvdata(dev, NULL);
  16259. pt_enable_regulator(cd, false);
  16260. error_get_regulator:
  16261. pt_get_regulator(cd, false);
  16262. error_alloc_data:
  16263. kfree(cd);
  16264. error_no_pdata:
  16265. pr_err("%s failed.\n", __func__);
  16266. return rc;
  16267. }
  16268. EXPORT_SYMBOL_GPL(pt_probe);
  16269. /*******************************************************************************
  16270. * FUNCTION: pt_release
  16271. *
  16272. * SUMMARY: This function does the following cleanup:
  16273. * - Releases all probed modules
  16274. * - Stops the watchdog
  16275. * - Cancels all pending work tasks
  16276. * - Removes all created sysfs nodes
  16277. * - Removes all created debugfs nodes
  16278. * - Frees the IRQ
  16279. * - Deletes the core
  16280. * - Frees all pointers and HID reports
  16281. *
  16282. * RETURN:
  16283. * 0 = success
  16284. * !0 = failure
  16285. *
  16286. * PARAMETERS:
  16287. * *cd - pointer to the core data structure
  16288. ******************************************************************************/
  16289. int pt_release(struct pt_core_data *cd)
  16290. {
  16291. struct device *dev = cd->dev;
  16292. /*
  16293. * Suspend the device before freeing the startup_work and stopping
  16294. * the watchdog since sleep function restarts watchdog on failure
  16295. */
  16296. pm_runtime_suspend(dev);
  16297. pm_runtime_disable(dev);
  16298. /*
  16299. * Any 'work' that can trigger a new thread should be canceled first.
  16300. * The watchdog is also stopped again because another thread could have
  16301. * restarted it. The irq_work is cancelled last because it is used for
  16302. * all I2C/SPI communication.
  16303. */
  16304. pt_stop_wd_timer(cd);
  16305. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16306. cancel_work_sync(&cd->ttdl_restart_work);
  16307. cancel_work_sync(&cd->enum_work);
  16308. cancel_work_sync(&cd->resume_offload_work);
  16309. cancel_work_sync(&cd->suspend_offload_work);
  16310. cancel_work_sync(&cd->resume_work);
  16311. cancel_work_sync(&cd->suspend_work);
  16312. destroy_workqueue(cd->pt_workqueue);
  16313. pt_stop_wd_timer(cd);
  16314. pt_release_modules(cd);
  16315. pt_proximity_release(dev);
  16316. pt_btn_release(dev);
  16317. pt_mt_release(dev);
  16318. #ifdef CONFIG_HAS_EARLYSUSPEND
  16319. unregister_early_suspend(&cd->es);
  16320. #elif defined(CONFIG_DRM)
  16321. if (active_panel)
  16322. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16323. #elif defined(CONFIG_FB)
  16324. fb_unregister_client(&cd->fb_notifier);
  16325. #endif
  16326. #ifdef NEED_SUSPEND_NOTIFIER
  16327. unregister_pm_notifier(&cd->pm_notifier);
  16328. #endif
  16329. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16330. device_wakeup_disable(dev);
  16331. #endif
  16332. device_init_wakeup(dev, 0);
  16333. #ifdef TTHE_TUNER_SUPPORT
  16334. mutex_lock(&cd->tthe_lock);
  16335. cd->tthe_exit = 1;
  16336. wake_up(&cd->wait_q);
  16337. mutex_unlock(&cd->tthe_lock);
  16338. debugfs_remove(cd->tthe_debugfs);
  16339. #endif
  16340. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16341. remove_sysfs_interfaces(dev);
  16342. disable_irq_nosync(cd->irq);
  16343. if (cd->cpdata->setup_irq)
  16344. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16345. if (cd->cpdata->init)
  16346. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16347. if (cd->cpdata->setup_power)
  16348. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16349. dev_set_drvdata(dev, NULL);
  16350. pt_del_core(dev);
  16351. if (cd->vcc_i2c)
  16352. regulator_set_load(cd->vcc_i2c, 0);
  16353. if (cd->vdd)
  16354. regulator_set_load(cd->vdd, 0);
  16355. pt_enable_regulator(cd, false);
  16356. pt_get_regulator(cd, false);
  16357. pt_free_si_ptrs(cd);
  16358. kfree(cd);
  16359. return 0;
  16360. }
  16361. EXPORT_SYMBOL_GPL(pt_release);
  16362. MODULE_LICENSE("GPL");
  16363. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16364. MODULE_AUTHOR("Parade Technologies <[email protected]>");