pt_core.c 515 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963
  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 "pt_regs.h"
  33. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  34. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  35. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  36. #define FT_VTG_MIN_UV 2800000
  37. #define FT_VTG_MAX_UV 2800000
  38. #define FT_I2C_VTG_MIN_UV 1800000
  39. #define FT_I2C_VTG_MAX_UV 1800000
  40. #define PWR_SUSPEND_LOAD_UA 165
  41. #define I2C_SUSPEND_LOAD_UA 100
  42. #define PWR_ACTIVE_LOAD_MA 12000
  43. #define I2C_ACTIVE_LOAD_MA 30000
  44. #define PT_CORE_STARTUP_RETRY_COUNT 3
  45. #define PT_STATUS_STR_LEN (50)
  46. #if defined(CONFIG_DRM)
  47. static struct drm_panel *active_panel;
  48. #endif
  49. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  50. #ifdef ENABLE_VDD_REG_ONLY
  51. static int pt_enable_vdd_regulator(struct pt_core_data *cd, bool en);
  52. #endif
  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. cmd = kzalloc(cmd_length, GFP_KERNEL);
  508. if (!cmd)
  509. return -ENOMEM;
  510. /* Set Command register */
  511. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  512. sizeof(cd->hid_desc.command_register));
  513. cmd_offset += sizeof(cd->hid_desc.command_register);
  514. /* Set Command */
  515. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  516. if (hid_cmd->report_id >= 0XF)
  517. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  518. else
  519. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  520. cmd_offset++;
  521. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  522. cmd_offset++;
  523. if (hid_cmd->report_id >= 0XF) {
  524. cmd[cmd_offset] = hid_cmd->report_id;
  525. cmd_offset++;
  526. }
  527. /* Set Data register */
  528. if (hid_cmd->has_data_register) {
  529. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  530. sizeof(cd->hid_desc.data_register));
  531. cmd_offset += sizeof(cd->hid_desc.data_register);
  532. }
  533. /* Set Data */
  534. if (hid_cmd->write_length && hid_cmd->write_buf) {
  535. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  536. hid_cmd->write_length);
  537. cmd_offset += hid_cmd->write_length;
  538. }
  539. pt_debug(cd->dev, DL_INFO,
  540. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  541. __func__, cmd_length, hid_cmd->report_id);
  542. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  543. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  544. hid_cmd->read_buf);
  545. if (rc)
  546. pt_debug(cd->dev, DL_ERROR,
  547. "%s: Fail pt_adap_transfer\n", __func__);
  548. kfree(cmd);
  549. return rc;
  550. }
  551. #ifdef TTDL_DIAGNOSTICS
  552. /*******************************************************************************
  553. * FUNCTION: pt_toggle_err_gpio
  554. *
  555. * SUMMARY: Toggles the pre-defined error GPIO
  556. *
  557. * RETURN: n/a
  558. *
  559. * PARAMETERS:
  560. * *cd - pointer to core data
  561. * type - type of err that occurred
  562. ******************************************************************************/
  563. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  564. {
  565. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  566. __func__, type);
  567. if (cd->err_gpio && type == cd->err_gpio_type) {
  568. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  569. gpio_direction_output(cd->err_gpio,
  570. !gpio_get_value(cd->err_gpio));
  571. }
  572. }
  573. /*******************************************************************************
  574. * FUNCTION: _pt_request_toggle_err_gpio
  575. *
  576. * SUMMARY: Function pointer included in core_cmds to allow other modules
  577. * to request to toggle the err_gpio
  578. *
  579. * RETURN: n/a
  580. *
  581. * PARAMETERS:
  582. * *cd - pointer to core data
  583. * type - type of err that occurred
  584. ******************************************************************************/
  585. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  586. {
  587. struct pt_core_data *cd = dev_get_drvdata(dev);
  588. pt_toggle_err_gpio(cd, type);
  589. }
  590. #endif /* TTDL_DIAGNOSTICS */
  591. /*******************************************************************************
  592. * FUNCTION: pt_hid_exec_cmd_and_wait_
  593. *
  594. * SUMMARY: Send the HID command to the DUT and wait for the response
  595. *
  596. * RETURN:
  597. * 0 = success
  598. * !0 = failure
  599. *
  600. * PARAMETERS:
  601. * *cd - pointer to core data
  602. * *hid_cmd - pointer to the HID command to send
  603. ******************************************************************************/
  604. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  605. struct pt_hid_cmd *hid_cmd)
  606. {
  607. int rc = 0;
  608. int t;
  609. u16 timeout_ms;
  610. int *cmd_state;
  611. if (hid_cmd->reset_cmd)
  612. cmd_state = &cd->hid_reset_cmd_state;
  613. else
  614. cmd_state = &cd->hid_cmd_state;
  615. if (hid_cmd->wait_interrupt) {
  616. mutex_lock(&cd->system_lock);
  617. *cmd_state = 1;
  618. mutex_unlock(&cd->system_lock);
  619. }
  620. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  621. if (rc) {
  622. if (hid_cmd->wait_interrupt)
  623. goto error;
  624. goto exit;
  625. }
  626. if (!hid_cmd->wait_interrupt)
  627. goto exit;
  628. if (hid_cmd->timeout_ms)
  629. timeout_ms = hid_cmd->timeout_ms;
  630. else
  631. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  632. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  633. msecs_to_jiffies(timeout_ms));
  634. if (IS_TMO(t)) {
  635. #ifdef TTDL_DIAGNOSTICS
  636. cd->bus_transmit_error_count++;
  637. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  638. #endif /* TTDL_DIAGNOSTICS */
  639. pt_debug(cd->dev, DL_ERROR,
  640. "%s: HID output cmd execution timed out\n",
  641. __func__);
  642. rc = -ETIME;
  643. goto error;
  644. }
  645. goto exit;
  646. error:
  647. mutex_lock(&cd->system_lock);
  648. *cmd_state = 0;
  649. mutex_unlock(&cd->system_lock);
  650. exit:
  651. return rc;
  652. }
  653. /*******************************************************************************
  654. * FUNCTION: pt_hid_cmd_reset_
  655. *
  656. * SUMMARY: Send the HID RESET command to the DUT
  657. *
  658. * RETURN:
  659. * 0 = success
  660. * !0 = failure
  661. *
  662. * PARAMETERS:
  663. * *cd - pointer to core data
  664. ******************************************************************************/
  665. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  666. {
  667. struct pt_hid_cmd hid_cmd = {
  668. .opcode = HID_CMD_RESET,
  669. .wait_interrupt = 1,
  670. .reset_cmd = 1,
  671. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  672. };
  673. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  674. }
  675. /*******************************************************************************
  676. * FUNCTION: pt_hid_cmd_reset
  677. *
  678. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  679. * access.
  680. *
  681. * RETURN:
  682. * 0 = success
  683. * !0 = failure
  684. *
  685. * PARAMETERS:
  686. * *cd - pointer to core data
  687. ******************************************************************************/
  688. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  689. {
  690. int rc = 0;
  691. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  692. if (rc < 0) {
  693. pt_debug(cd->dev, DL_ERROR,
  694. "%s: fail get exclusive ex=%p own=%p\n",
  695. __func__, cd->exclusive_dev, cd->dev);
  696. return rc;
  697. }
  698. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  699. rc = pt_hid_cmd_reset_(cd);
  700. if (release_exclusive(cd, cd->dev) < 0)
  701. pt_debug(cd->dev, DL_ERROR,
  702. "%s: fail to release exclusive\n", __func__);
  703. return rc;
  704. }
  705. /*******************************************************************************
  706. * FUNCTION: pt_hid_cmd_set_power_
  707. *
  708. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  709. *
  710. * RETURN:
  711. * 0 = success
  712. * !0 = failure
  713. *
  714. * PARAMETERS:
  715. * *cd - pointer to core data
  716. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  717. ******************************************************************************/
  718. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  719. u8 power_state)
  720. {
  721. int rc = 0;
  722. struct pt_hid_cmd hid_cmd = {
  723. .opcode = HID_CMD_SET_POWER,
  724. .wait_interrupt = 1,
  725. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  726. };
  727. hid_cmd.power_state = power_state;
  728. /* The chip won't give response if goes to Deep Standby */
  729. if (power_state == HID_POWER_STANDBY) {
  730. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  731. if (rc)
  732. pt_debug(cd->dev, DL_ERROR,
  733. "%s: Failed to set power to state:%d\n",
  734. __func__, power_state);
  735. else
  736. cd->fw_sys_mode_in_standby_state = true;
  737. return rc;
  738. }
  739. cd->fw_sys_mode_in_standby_state = false;
  740. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  741. if (rc) {
  742. pt_debug(cd->dev, DL_ERROR,
  743. "%s: Failed to set power to state:%d\n",
  744. __func__, power_state);
  745. return rc;
  746. }
  747. /* validate */
  748. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  749. || ((cd->response_buf[3] & 0x3) != power_state)
  750. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  751. rc = -EINVAL;
  752. return rc;
  753. }
  754. /*******************************************************************************
  755. * FUNCTION: pt_hid_cmd_set_power
  756. *
  757. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  758. * exclusive access.
  759. *
  760. * RETURN:
  761. * 0 = success
  762. * !0 = failure
  763. *
  764. * PARAMETERS:
  765. * *cd - pointer to core data
  766. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  767. ******************************************************************************/
  768. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  769. u8 power_state)
  770. {
  771. int rc;
  772. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  773. if (rc < 0) {
  774. pt_debug(cd->dev, DL_ERROR,
  775. "%s: fail get exclusive ex=%p own=%p\n",
  776. __func__, cd->exclusive_dev, cd->dev);
  777. return rc;
  778. }
  779. rc = pt_hid_cmd_set_power_(cd, power_state);
  780. if (release_exclusive(cd, cd->dev) < 0)
  781. pt_debug(cd->dev, DL_ERROR,
  782. "%s: fail to release exclusive\n", __func__);
  783. return rc;
  784. }
  785. static const u16 crc_table[16] = {
  786. 0x0000, 0x1021, 0x2042, 0x3063,
  787. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  788. 0x8108, 0x9129, 0xa14a, 0xb16b,
  789. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  790. };
  791. /*******************************************************************************
  792. * FUNCTION: _pt_compute_crc
  793. *
  794. * SUMMARY: Calculate CRC by CRC table.
  795. *
  796. * RETURN:
  797. * CRC calculation result
  798. *
  799. * PARAMETERS:
  800. * *buf - pointer to the data array to be calculated
  801. * size - size of data array
  802. ******************************************************************************/
  803. static u16 _pt_compute_crc(u8 *buf, u32 size)
  804. {
  805. u16 remainder = 0xFFFF;
  806. u16 xor_mask = 0x0000;
  807. u32 index;
  808. u32 byte_value;
  809. u32 table_index;
  810. u32 crc_bit_width = sizeof(u16) * 8;
  811. /* Divide the message by polynomial, via the table. */
  812. for (index = 0; index < size; index++) {
  813. byte_value = buf[index];
  814. table_index = ((byte_value >> 4) & 0x0F)
  815. ^ (remainder >> (crc_bit_width - 4));
  816. remainder = crc_table[table_index] ^ (remainder << 4);
  817. table_index = (byte_value & 0x0F)
  818. ^ (remainder >> (crc_bit_width - 4));
  819. remainder = crc_table[table_index] ^ (remainder << 4);
  820. }
  821. /* Perform the final remainder CRC. */
  822. return remainder ^ xor_mask;
  823. }
  824. u16 ccitt_Table[] = {
  825. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  826. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  827. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  828. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  829. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  830. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  831. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  832. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  833. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  834. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  835. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  836. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  837. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  838. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  839. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  840. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  841. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  842. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  843. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  844. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  845. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  846. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  847. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  848. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  849. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  850. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  851. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  852. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  853. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  854. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  855. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  856. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  857. };
  858. /*******************************************************************************
  859. * FUNCTION: crc_ccitt_calculate
  860. *
  861. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  862. *
  863. * RETURN:
  864. * CRC calculation result
  865. *
  866. * PARAMETERS:
  867. * *q - pointer to the data array to be calculated
  868. * len - size of data array
  869. ******************************************************************************/
  870. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  871. {
  872. unsigned short crc = 0xffff;
  873. while (len-- > 0)
  874. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  875. return crc;
  876. }
  877. /*******************************************************************************
  878. * FUNCTION: pt_pip2_cmd_calculate_crc
  879. *
  880. * SUMMARY: Calculate the CRC of a command packet
  881. *
  882. * RETURN: void
  883. *
  884. * PARAMETERS:
  885. * *cmd - pointer to command data
  886. * extra_bytes - Extra bytes included in command length
  887. ******************************************************************************/
  888. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  889. u8 extra_bytes)
  890. {
  891. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  892. unsigned short crc;
  893. buf[0] = cmd->len & 0xff;
  894. buf[1] = (cmd->len & 0xff00) >> 8;
  895. buf[2] = cmd->seq;
  896. buf[3] = cmd->id;
  897. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  898. /* Calculate the CRC for the first 4 bytes above and the data payload */
  899. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  900. cmd->crc[0] = (crc & 0xff00) >> 8;
  901. cmd->crc[1] = (crc & 0xff);
  902. }
  903. /*******************************************************************************
  904. * FUNCTION: pt_pip2_get_next_cmd_seq
  905. *
  906. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  907. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  908. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  909. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  910. * increment, just use the forced value.
  911. *
  912. * RETURN: Next command sequence number [0x08-0x0F]
  913. *
  914. * PARAMETERS:
  915. * *cd - pointer to core data
  916. ******************************************************************************/
  917. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  918. {
  919. #ifdef TTDL_DIAGNOSTICS
  920. if (cd->force_pip2_seq <= 0x07) {
  921. cd->pip2_cmd_tag_seq++;
  922. if (cd->pip2_cmd_tag_seq > 0x0F)
  923. cd->pip2_cmd_tag_seq = 0x08;
  924. } else {
  925. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  926. }
  927. #else
  928. cd->pip2_cmd_tag_seq++;
  929. if (cd->pip2_cmd_tag_seq > 0x0F)
  930. cd->pip2_cmd_tag_seq = 0x08;
  931. #endif
  932. return cd->pip2_cmd_tag_seq;
  933. }
  934. /*
  935. * Following macros are to define the response time (the interval between PIP2
  936. * command finishes sending and INT pin falls). The unit is in microsecond.
  937. * It has different time settings between the solution GPIO polling and Bus
  938. * polling due to the considration for system load.
  939. */
  940. #ifdef PT_POLL_RESP_BY_BUS
  941. #define POLL_RETRY_DEFAULT_INTERVAL 50
  942. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  943. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  944. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  945. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  946. #else
  947. #define POLL_RETRY_DEFAULT_INTERVAL 20
  948. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  949. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  950. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  951. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  952. #endif
  953. /*
  954. * id: the command id defined in PIP2
  955. * response_len: the (maximum) length of response.
  956. * response_time_min: minimum response time in microsecond
  957. * response_time_max: maximum response time in microsecond
  958. */
  959. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  960. {.id = PIP2_CMD_ID_PING,
  961. .response_len = 255,
  962. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  963. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  964. {.id = PIP2_CMD_ID_STATUS,
  965. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  966. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  967. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  968. {.id = PIP2_CMD_ID_CTRL,
  969. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  970. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  971. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  972. {.id = PIP2_CMD_ID_CONFIG,
  973. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  974. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  975. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  976. {.id = PIP2_CMD_ID_CLEAR,
  977. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  978. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  979. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  980. {.id = PIP2_CMD_ID_RESET,
  981. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  982. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  983. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  984. {.id = PIP2_CMD_ID_VERSION,
  985. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  986. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  987. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  988. {.id = PIP2_CMD_ID_FILE_OPEN,
  989. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  990. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  991. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  992. {.id = PIP2_CMD_ID_FILE_CLOSE,
  993. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  994. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  995. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  996. {.id = PIP2_CMD_ID_FILE_READ,
  997. .response_len = 255,
  998. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  999. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1000. {.id = PIP2_CMD_ID_FILE_WRITE,
  1001. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1002. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1003. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1004. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1005. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1006. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1007. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1008. {.id = PIP2_CMD_ID_FLASH_INFO,
  1009. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1010. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1011. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1012. {.id = PIP2_CMD_ID_EXECUTE,
  1013. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1014. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1015. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1016. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1017. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1018. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1019. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1020. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1021. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1022. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1023. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1024. {.id = PIP2_CMD_ID_READ_GPIO,
  1025. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1026. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1027. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1028. {.id = PIP2_CMD_EXECUTE_SCAN,
  1029. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1030. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1031. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1032. {.id = PIP2_CMD_SET_PARAMETER,
  1033. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1034. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1035. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1036. {.id = PIP2_CMD_GET_PARAMETER,
  1037. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1038. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1039. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1040. {.id = PIP2_CMD_SET_DDI_REG,
  1041. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1042. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1043. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1044. {.id = PIP2_CMD_GET_DDI_REG,
  1045. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1046. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1047. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1048. {.id = PIP2_CMD_ID_END,
  1049. .response_len = 255,
  1050. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1051. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1052. };
  1053. /*******************************************************************************
  1054. * FUNCTION: pt_pip2_get_cmd_response_len
  1055. *
  1056. * SUMMARY: Gets the expected response length based on the command ID
  1057. *
  1058. * RETURN: Expected response length
  1059. *
  1060. * PARAMETERS:
  1061. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1062. ******************************************************************************/
  1063. static int pt_pip2_get_cmd_response_len(u8 id)
  1064. {
  1065. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1066. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1067. p++;
  1068. if (p->id != PIP2_CMD_ID_END)
  1069. return p->response_len;
  1070. else
  1071. return -EPERM;
  1072. }
  1073. /*******************************************************************************
  1074. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1075. *
  1076. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1077. * finishes sending and INT pin falls) based on the command ID
  1078. *
  1079. * RETURN: Estimated minimum response time in microsecond
  1080. *
  1081. * PARAMETERS:
  1082. * id - Command ID
  1083. ******************************************************************************/
  1084. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1085. {
  1086. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1087. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1088. p++;
  1089. if (p->id != PIP2_CMD_ID_END)
  1090. return p->response_time_min;
  1091. else
  1092. return PIP2_RESP_DEFAULT_TIME_MIN;
  1093. }
  1094. /*******************************************************************************
  1095. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1096. *
  1097. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1098. * finishes sending and INT pin falls) based on the command ID
  1099. *
  1100. * RETURN: Estimated maximum response time in microsecond
  1101. *
  1102. * PARAMETERS:
  1103. * id - Command ID
  1104. ******************************************************************************/
  1105. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1106. {
  1107. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1108. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1109. p++;
  1110. if (p->id != PIP2_CMD_ID_END)
  1111. return p->response_time_max;
  1112. else
  1113. return PIP2_RESP_DEFAULT_TIME_MAX;
  1114. }
  1115. /*******************************************************************************
  1116. * FUNCTION: pt_pip2_validate_response
  1117. *
  1118. * SUMMARY: Validate the response of PIP2 command.
  1119. *
  1120. * RETURN:
  1121. * 0 = success
  1122. * !0 = failure
  1123. *
  1124. * PARAMETERS:
  1125. * *cd - pointer to core data
  1126. * *pip2_cmd - pointer to PIP2 command to send
  1127. * *read_buf - pointer to response buffer
  1128. * actual_read_len - actual read length of the response
  1129. ******************************************************************************/
  1130. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1131. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1132. u16 actual_read_len)
  1133. {
  1134. int rc = 0;
  1135. u8 response_seq = 0;
  1136. u8 reserved_bits = 0;
  1137. u8 cmd_id = 0;
  1138. u8 response_bit = 0;
  1139. unsigned short calc_crc = 0;
  1140. unsigned short resp_crc = 0;
  1141. /* Verify the length of response buffer */
  1142. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1143. pt_debug(cd->dev, DL_ERROR,
  1144. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1145. __func__, pip2_cmd->id, actual_read_len);
  1146. rc = -EINVAL;
  1147. goto exit;
  1148. }
  1149. /* Verify the CRC */
  1150. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1151. resp_crc = read_buf[actual_read_len - 2] << 8;
  1152. resp_crc |= read_buf[actual_read_len - 1];
  1153. if (resp_crc != calc_crc) {
  1154. pt_debug(cd->dev, DL_ERROR,
  1155. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1156. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1157. #ifdef TTDL_DIAGNOSTICS
  1158. cd->pip2_crc_error_count++;
  1159. #endif /* TTDL_DIAGNOSTICS */
  1160. rc = -EINVAL;
  1161. goto exit;
  1162. }
  1163. /* Verify the response bit is set */
  1164. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1165. if (!response_bit) {
  1166. pt_debug(cd->dev, DL_ERROR,
  1167. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1168. __func__, pip2_cmd->id, response_bit);
  1169. rc = -EINVAL;
  1170. goto exit;
  1171. }
  1172. /* Verify the command ID matches from command to response */
  1173. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1174. if (cmd_id != pip2_cmd->id) {
  1175. pt_debug(cd->dev, DL_ERROR,
  1176. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1177. __func__, pip2_cmd->id, cmd_id);
  1178. rc = -EINVAL;
  1179. goto exit;
  1180. }
  1181. /* Verify the SEQ number matches from command to response */
  1182. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1183. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1184. pt_debug(cd->dev, DL_ERROR,
  1185. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1186. __func__, pip2_cmd->id,
  1187. pip2_cmd->seq, response_seq);
  1188. rc = -EINVAL;
  1189. goto exit;
  1190. }
  1191. /* Verify the reserved bits are 0 */
  1192. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1193. if (reserved_bits)
  1194. pt_debug(cd->dev, DL_WARN,
  1195. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1196. __func__, pip2_cmd->id, reserved_bits);
  1197. exit:
  1198. if (rc)
  1199. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1200. "PIP RSP:");
  1201. return rc;
  1202. }
  1203. /*******************************************************************************
  1204. * FUNCTION: pt_hid_output_validate_bl_response
  1205. *
  1206. * SUMMARY: Validate the response of bootloader command.
  1207. *
  1208. * RETURN:
  1209. * 0 = success
  1210. * !0 = failure
  1211. *
  1212. * PARAMETERS:
  1213. * *cd - pointer to core data
  1214. * *hid_output - pointer to hid output data structure
  1215. ******************************************************************************/
  1216. static int pt_hid_output_validate_bl_response(
  1217. struct pt_core_data *cd,
  1218. struct pt_hid_output *hid_output)
  1219. {
  1220. u16 size;
  1221. u16 crc;
  1222. u8 status;
  1223. size = get_unaligned_le16(&cd->response_buf[0]);
  1224. if (hid_output->reset_expected && !size)
  1225. return 0;
  1226. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1227. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1228. pt_debug(cd->dev, DL_ERROR,
  1229. "%s: BL output response, wrong report_id\n", __func__);
  1230. return -EPROTO;
  1231. }
  1232. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1233. pt_debug(cd->dev, DL_ERROR,
  1234. "%s: BL output response, wrong SOP\n", __func__);
  1235. return -EPROTO;
  1236. }
  1237. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1238. pt_debug(cd->dev, DL_ERROR,
  1239. "%s: BL output response, wrong EOP\n", __func__);
  1240. return -EPROTO;
  1241. }
  1242. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1243. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1244. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1245. pt_debug(cd->dev, DL_ERROR,
  1246. "%s: BL output response, wrong CRC 0x%X\n",
  1247. __func__, crc);
  1248. return -EPROTO;
  1249. }
  1250. status = cd->response_buf[5];
  1251. if (status) {
  1252. pt_debug(cd->dev, DL_ERROR,
  1253. "%s: BL output response, ERROR:%d\n",
  1254. __func__, status);
  1255. return -EPROTO;
  1256. }
  1257. return 0;
  1258. }
  1259. /*******************************************************************************
  1260. * FUNCTION: pt_hid_output_validate_app_response
  1261. *
  1262. * SUMMARY: Validate the response of application command.
  1263. *
  1264. * RETURN:
  1265. * 0 = success
  1266. * !0 = failure
  1267. *
  1268. * PARAMETERS:
  1269. * *cd - pointer to core data
  1270. * *hid_output - pointer to hid output data structure
  1271. ******************************************************************************/
  1272. static int pt_hid_output_validate_app_response(
  1273. struct pt_core_data *cd,
  1274. struct pt_hid_output *hid_output)
  1275. {
  1276. int command_code;
  1277. u16 size;
  1278. size = get_unaligned_le16(&cd->response_buf[0]);
  1279. if (hid_output->reset_expected && !size)
  1280. return 0;
  1281. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1282. != PT_PIP_NON_HID_RESPONSE_ID) {
  1283. pt_debug(cd->dev, DL_ERROR,
  1284. "%s: APP output response, wrong report_id\n", __func__);
  1285. return -EPROTO;
  1286. }
  1287. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1288. & PIP1_RESP_COMMAND_ID_MASK;
  1289. if (command_code != hid_output->command_code) {
  1290. pt_debug(cd->dev, DL_ERROR,
  1291. "%s: APP output response, wrong command_code:%X\n",
  1292. __func__, command_code);
  1293. return -EPROTO;
  1294. }
  1295. return 0;
  1296. }
  1297. /*******************************************************************************
  1298. * FUNCTION: pt_check_set_parameter
  1299. *
  1300. * SUMMARY: Check command input and response for Set Parameter command.And
  1301. * store the parameter to the list for resume work if pass the check.
  1302. *
  1303. * PARAMETERS:
  1304. * *cd - pointer to core data
  1305. * *hid_output - pointer to hid output data structure
  1306. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1307. ******************************************************************************/
  1308. static void pt_check_set_parameter(struct pt_core_data *cd,
  1309. struct pt_hid_output *hid_output, bool raw)
  1310. {
  1311. u8 *param_buf;
  1312. u32 param_value = 0;
  1313. u8 param_size;
  1314. u8 param_id;
  1315. int i = 0;
  1316. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1317. return;
  1318. /* Check command input for Set Parameter command */
  1319. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1320. && !memcmp(&hid_output->write_buf[0],
  1321. &cd->hid_desc.output_register,
  1322. sizeof(cd->hid_desc.output_register))
  1323. && hid_output->write_buf[4] ==
  1324. PT_PIP_NON_HID_COMMAND_ID
  1325. && hid_output->write_buf[6] ==
  1326. PIP1_CMD_ID_SET_PARAM)
  1327. param_buf = &hid_output->write_buf[7];
  1328. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1329. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1330. && hid_output->write_length >= 3
  1331. && hid_output->write_length <= 6)
  1332. param_buf = &hid_output->write_buf[0];
  1333. else
  1334. return;
  1335. /* Get parameter ID, size and value */
  1336. param_id = param_buf[0];
  1337. param_size = param_buf[1];
  1338. if (param_size > 4) {
  1339. pt_debug(cd->dev, DL_ERROR,
  1340. "%s: Invalid parameter size\n", __func__);
  1341. return;
  1342. }
  1343. param_buf = &param_buf[2];
  1344. while (i < param_size)
  1345. param_value += *(param_buf++) << (8 * i++);
  1346. /* Check command response for Set Parameter command */
  1347. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1348. || (cd->response_buf[4] &
  1349. PIP1_RESP_COMMAND_ID_MASK) !=
  1350. PIP1_CMD_ID_SET_PARAM
  1351. || cd->response_buf[5] != param_id
  1352. || cd->response_buf[6] != param_size) {
  1353. pt_debug(cd->dev, DL_ERROR,
  1354. "%s: Set Parameter command not successful\n",
  1355. __func__);
  1356. return;
  1357. }
  1358. pt_add_parameter(cd, param_id, param_value, param_size);
  1359. }
  1360. /*******************************************************************************
  1361. * FUNCTION: pt_check_command
  1362. *
  1363. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1364. * called here to check output command and store parameter to the list.
  1365. *
  1366. * PARAMETERS:
  1367. * *cd - pointer to core data
  1368. * *hid_output - pointer to hid output data structure
  1369. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1370. ******************************************************************************/
  1371. static void pt_check_command(struct pt_core_data *cd,
  1372. struct pt_hid_output *hid_output, bool raw)
  1373. {
  1374. pt_check_set_parameter(cd, hid_output, raw);
  1375. }
  1376. /*******************************************************************************
  1377. * FUNCTION: pt_hid_output_validate_response
  1378. *
  1379. * SUMMARY: Validate the response of application or bootloader command.
  1380. *
  1381. * RETURN:
  1382. * 0 = success
  1383. * !0 = failure
  1384. *
  1385. * PARAMETERS:
  1386. * *cd - pointer to core data
  1387. * *hid_output - pointer to hid output data structure
  1388. ******************************************************************************/
  1389. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1390. struct pt_hid_output *hid_output)
  1391. {
  1392. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1393. return pt_hid_output_validate_bl_response(cd, hid_output);
  1394. return pt_hid_output_validate_app_response(cd, hid_output);
  1395. }
  1396. /*******************************************************************************
  1397. * FUNCTION: pt_hid_send_output_user_
  1398. *
  1399. * SUMMARY: Blindly send user data to the DUT.
  1400. *
  1401. * RETURN:
  1402. * 0 = success
  1403. * !0 = failure
  1404. *
  1405. * PARAMETERS:
  1406. * *cd - pointer to core data
  1407. * *hid_output - pointer to the command to send
  1408. ******************************************************************************/
  1409. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1410. struct pt_hid_output *hid_output)
  1411. {
  1412. int rc = 0;
  1413. int cmd;
  1414. if (!hid_output->length || !hid_output->write_buf)
  1415. return -EINVAL;
  1416. if (cd->pip2_prot_active) {
  1417. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1418. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1419. } else
  1420. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1421. pt_debug(cd->dev, DL_INFO,
  1422. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1423. __func__, hid_output->length, cmd);
  1424. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1425. hid_output->length, ">>> User CMD");
  1426. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1427. hid_output->write_buf, NULL);
  1428. if (rc)
  1429. pt_debug(cd->dev, DL_ERROR,
  1430. "%s: Fail pt_adap_transfer\n", __func__);
  1431. return rc;
  1432. }
  1433. /*******************************************************************************
  1434. * FUNCTION: pt_hid_send_output_user_and_wait_
  1435. *
  1436. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1437. *
  1438. * RETURN:
  1439. * 0 = success
  1440. * !0 = failure
  1441. *
  1442. * PARAMETERS:
  1443. * *cd - pointer to core data
  1444. * *hid_output - pointer to the command to send
  1445. ******************************************************************************/
  1446. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1447. struct pt_hid_output *hid_output)
  1448. {
  1449. int rc = 0;
  1450. int t;
  1451. mutex_lock(&cd->system_lock);
  1452. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1453. mutex_unlock(&cd->system_lock);
  1454. rc = pt_hid_send_output_user_(cd, hid_output);
  1455. if (rc)
  1456. goto error;
  1457. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1458. msecs_to_jiffies(cd->pip_cmd_timeout));
  1459. if (IS_TMO(t)) {
  1460. #ifdef TTDL_DIAGNOSTICS
  1461. cd->bus_transmit_error_count++;
  1462. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1463. #endif /* TTDL_DIAGNOSTICS */
  1464. pt_debug(cd->dev, DL_ERROR,
  1465. "%s: HID output cmd execution timed out\n",
  1466. __func__);
  1467. rc = -ETIME;
  1468. goto error;
  1469. }
  1470. pt_check_command(cd, hid_output, true);
  1471. goto exit;
  1472. error:
  1473. mutex_lock(&cd->system_lock);
  1474. cd->hid_cmd_state = 0;
  1475. mutex_unlock(&cd->system_lock);
  1476. exit:
  1477. return rc;
  1478. }
  1479. /*******************************************************************************
  1480. * FUNCTION: pt_check_irq_asserted
  1481. *
  1482. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1483. * the FW can hold the INT line low ~150us after the read is complete.
  1484. * NOTE: if irq_stat is not defined this function will return false
  1485. *
  1486. * RETURN:
  1487. * true = IRQ asserted
  1488. * false = IRQ not asserted
  1489. *
  1490. * PARAMETERS:
  1491. * *cd - pointer to core data
  1492. ******************************************************************************/
  1493. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1494. {
  1495. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1496. /*
  1497. * Workaround for FW defect, CDT165308
  1498. * bl_launch app creates a glitch in IRQ line
  1499. */
  1500. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1501. && cd->cpdata->irq_stat) {
  1502. /*
  1503. * in X1S panel and GC1546 panel, the width for the INT
  1504. * glitch is about 4us,the normal INT width of response
  1505. * will last more than 200us, so use 10us delay
  1506. * for distinguish the glitch the normal INT is enough.
  1507. */
  1508. udelay(10);
  1509. }
  1510. #endif
  1511. if (cd->cpdata->irq_stat) {
  1512. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1513. == PT_IRQ_ASSERTED_VALUE) {
  1514. /* Debounce to allow FW to release INT */
  1515. usleep_range(100, 200);
  1516. }
  1517. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1518. == PT_IRQ_ASSERTED_VALUE)
  1519. return true;
  1520. else
  1521. return false;
  1522. }
  1523. return true;
  1524. }
  1525. /*******************************************************************************
  1526. * FUNCTION: pt_flush_bus
  1527. *
  1528. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1529. * Used if IRQ is found to be stuck low
  1530. *
  1531. * RETURN: Length of bytes read from bus
  1532. *
  1533. * PARAMETERS:
  1534. * *cd - pointer to core data
  1535. * flush_type - type of flush
  1536. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1537. * - PT_FLUSH_BUS_FULL_256_READ
  1538. * *read_buf - pointer to store read data
  1539. ******************************************************************************/
  1540. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1541. u8 flush_type, u8 *read_buf)
  1542. {
  1543. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1544. u16 pip_len;
  1545. int bytes_read;
  1546. int rc = 0;
  1547. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1548. rc = pt_adap_read_default(cd, buf, 2);
  1549. if (rc) {
  1550. bytes_read = 0;
  1551. goto exit;
  1552. }
  1553. pip_len = get_unaligned_le16(&buf[0]);
  1554. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1555. #ifdef TTDL_DIAGNOSTICS
  1556. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1557. #endif
  1558. bytes_read = 2;
  1559. pt_debug(cd->dev, DL_INFO,
  1560. "%s: Empty buf detected - len=0x%04X\n",
  1561. __func__, pip_len);
  1562. } else if (pip_len == 0) {
  1563. bytes_read = 0;
  1564. pt_debug(cd->dev, DL_INFO,
  1565. "%s: Sentinel detected\n", __func__);
  1566. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1567. pt_debug(cd->dev, DL_ERROR,
  1568. "%s: Illegal len=0x%04x, force %d byte read\n",
  1569. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1570. rc = pt_adap_read_default(cd, buf,
  1571. PT_MAX_PIP2_MSG_SIZE);
  1572. if (!rc)
  1573. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1574. else
  1575. bytes_read = 0;
  1576. } else {
  1577. pt_debug(cd->dev, DL_INFO,
  1578. "%s: Flush read of %d bytes...\n",
  1579. __func__, pip_len);
  1580. rc = pt_adap_read_default(cd, buf, pip_len);
  1581. if (!rc)
  1582. bytes_read = pip_len;
  1583. else
  1584. bytes_read = 0;
  1585. }
  1586. } else {
  1587. pt_debug(cd->dev, DL_INFO,
  1588. "%s: Forced flush of max %d bytes...\n",
  1589. __func__, PT_MAX_PIP2_MSG_SIZE);
  1590. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1591. if (!rc)
  1592. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1593. else
  1594. bytes_read = 0;
  1595. }
  1596. if (read_buf && (bytes_read > 3))
  1597. memcpy(read_buf, buf, bytes_read);
  1598. exit:
  1599. return bytes_read;
  1600. }
  1601. /*******************************************************************************
  1602. * FUNCTION: pt_flush_bus_if_irq_asserted
  1603. *
  1604. * SUMMARY: This function will flush the active bus if the INT is found to be
  1605. * asserted.
  1606. *
  1607. * RETURN: bytes cleared from bus
  1608. *
  1609. * PARAMETERS:
  1610. * *cd - pointer the core data structure
  1611. * flush_type - type of flush
  1612. * - PT_FLUSH_BUS_BASED_ON_LEN
  1613. * - PT_FLUSH_BUS_FULL_256_READ
  1614. ******************************************************************************/
  1615. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1616. {
  1617. int count = 0;
  1618. int bytes_read = 0;
  1619. while (pt_check_irq_asserted(cd) && count < 5) {
  1620. count++;
  1621. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1622. if (bytes_read) {
  1623. pt_debug(cd->dev, DL_WARN,
  1624. "%s: Cleared %d bytes off bus\n",
  1625. __func__, bytes_read);
  1626. }
  1627. }
  1628. if (pt_check_irq_asserted(cd)) {
  1629. pt_debug(cd->dev, DL_ERROR,
  1630. "%s: IRQ still asserted, %d bytes read\n",
  1631. __func__, bytes_read);
  1632. } else {
  1633. pt_debug(cd->dev, DL_INFO,
  1634. "%s: IRQ cleared, %d bytes read\n",
  1635. __func__, bytes_read);
  1636. }
  1637. return bytes_read;
  1638. }
  1639. /*******************************************************************************
  1640. * FUNCTION: pt_hid_send_output_
  1641. *
  1642. * SUMMARY: Send a touch application command to the DUT
  1643. *
  1644. * RETURN:
  1645. * 0 = success
  1646. * !0 = failure
  1647. *
  1648. * PARAMETERS:
  1649. * *cd - pointer to core data
  1650. * *hid_output - pointer to the command to send
  1651. ******************************************************************************/
  1652. static int pt_hid_send_output_(struct pt_core_data *cd,
  1653. struct pt_hid_output *hid_output)
  1654. {
  1655. int rc = 0;
  1656. u8 *cmd;
  1657. u16 length;
  1658. u16 crc;
  1659. u8 report_id;
  1660. u8 cmd_offset = 0;
  1661. u8 cmd_allocated = 0;
  1662. switch (hid_output->cmd_type) {
  1663. case PIP1_CMD_TYPE_FW:
  1664. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1665. length = 5;
  1666. break;
  1667. case PIP1_CMD_TYPE_BL:
  1668. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1669. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1670. break;
  1671. default:
  1672. return -EINVAL;
  1673. }
  1674. length += hid_output->write_length;
  1675. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1676. cmd = kzalloc(length + 2, GFP_KERNEL);
  1677. if (!cmd)
  1678. return -ENOMEM;
  1679. cmd_allocated = 1;
  1680. } else {
  1681. cmd = cd->cmd_buf;
  1682. }
  1683. /* Set Output register */
  1684. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1685. sizeof(cd->hid_desc.output_register));
  1686. cmd_offset += sizeof(cd->hid_desc.output_register);
  1687. cmd[cmd_offset++] = LOW_BYTE(length);
  1688. cmd[cmd_offset++] = HI_BYTE(length);
  1689. cmd[cmd_offset++] = report_id;
  1690. cmd[cmd_offset++] = 0x0; /* reserved */
  1691. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1692. cmd[cmd_offset++] = PIP1_BL_SOP;
  1693. cmd[cmd_offset++] = hid_output->command_code;
  1694. /* Set Data Length for bootloader */
  1695. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1696. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1697. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1698. }
  1699. /* Set Data */
  1700. if (hid_output->write_length && hid_output->write_buf) {
  1701. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1702. hid_output->write_length);
  1703. cmd_offset += hid_output->write_length;
  1704. }
  1705. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1706. crc = _pt_compute_crc(&cmd[6],
  1707. hid_output->write_length + 4);
  1708. cmd[cmd_offset++] = LOW_BYTE(crc);
  1709. cmd[cmd_offset++] = HI_BYTE(crc);
  1710. cmd[cmd_offset++] = PIP1_BL_EOP;
  1711. }
  1712. pt_debug(cd->dev, DL_INFO,
  1713. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1714. __func__, length + 2, hid_output->command_code);
  1715. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1716. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1717. if (rc)
  1718. pt_debug(cd->dev, DL_ERROR,
  1719. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1720. if (cmd_allocated)
  1721. kfree(cmd);
  1722. return rc;
  1723. }
  1724. /*******************************************************************************
  1725. * FUNCTION: pt_pip1_send_output_and_wait_
  1726. *
  1727. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1728. *
  1729. * RETURN:
  1730. * 0 = success
  1731. * !0 = failure
  1732. *
  1733. * PARAMETERS:
  1734. * *cd - pointer to core data
  1735. * *hid_output - pointer to the command to send
  1736. ******************************************************************************/
  1737. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1738. struct pt_hid_output *hid_output)
  1739. {
  1740. int rc = 0;
  1741. int t;
  1742. u16 timeout_ms;
  1743. mutex_lock(&cd->system_lock);
  1744. cd->hid_cmd_state = hid_output->command_code + 1;
  1745. mutex_unlock(&cd->system_lock);
  1746. if (hid_output->timeout_ms)
  1747. timeout_ms = hid_output->timeout_ms;
  1748. else
  1749. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1750. rc = pt_hid_send_output_(cd, hid_output);
  1751. if (rc)
  1752. goto error;
  1753. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1754. msecs_to_jiffies(timeout_ms));
  1755. if (IS_TMO(t)) {
  1756. #ifdef TTDL_DIAGNOSTICS
  1757. cd->bus_transmit_error_count++;
  1758. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1759. #endif /* TTDL_DIAGNOSTICS */
  1760. pt_debug(cd->dev, DL_ERROR,
  1761. "%s: HID output cmd execution timed out (%dms)\n",
  1762. __func__, timeout_ms);
  1763. rc = -ETIME;
  1764. goto error;
  1765. }
  1766. if (!hid_output->novalidate)
  1767. rc = pt_hid_output_validate_response(cd, hid_output);
  1768. pt_check_command(cd, hid_output, false);
  1769. goto exit;
  1770. error:
  1771. mutex_lock(&cd->system_lock);
  1772. cd->hid_cmd_state = 0;
  1773. mutex_unlock(&cd->system_lock);
  1774. exit:
  1775. return rc;
  1776. }
  1777. /*******************************************************************************
  1778. * FUNCTION: pt_hid_output_user_cmd_
  1779. *
  1780. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1781. * to the DUT waiting for the response and loading it into the read buffer
  1782. *
  1783. * RETURN:
  1784. * 0 = success
  1785. * !0 = failure
  1786. *
  1787. * PARAMETERS:
  1788. * *cd - pointer to core data
  1789. * read_len - expected read length of the response
  1790. * *read_buf - pointer to where the response will be loaded
  1791. * write_len - length of the write buffer
  1792. * *write_buf - pointer to the write buffer
  1793. * *actual_read_len - pointer to the actual amount of data read back
  1794. ******************************************************************************/
  1795. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1796. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1797. u16 *actual_read_len)
  1798. {
  1799. int rc = 0;
  1800. u16 size;
  1801. struct pt_hid_output hid_output = {
  1802. .length = write_len,
  1803. .write_buf = write_buf,
  1804. };
  1805. #ifdef TTHE_TUNER_SUPPORT
  1806. if (!cd->pip2_send_user_cmd) {
  1807. int command_code = 0;
  1808. int len;
  1809. /* Print up to cmd ID */
  1810. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1811. if (write_len < len)
  1812. len = write_len;
  1813. else
  1814. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1815. & PIP1_CMD_COMMAND_ID_MASK;
  1816. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1817. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1818. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1819. tthe_print(cd, write_buf, len, "CMD=");
  1820. }
  1821. #endif
  1822. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1823. if (rc)
  1824. return rc;
  1825. /* Get the response size from the first 2 bytes in the response */
  1826. size = get_unaligned_le16(&cd->response_buf[0]);
  1827. /* Ensure size is not greater than max buffer size */
  1828. if (size > PT_MAX_PIP2_MSG_SIZE)
  1829. size = PT_MAX_PIP2_MSG_SIZE;
  1830. /* Minimum size to read is the 2 byte len field */
  1831. if (size == 0)
  1832. size = 2;
  1833. if (size > read_len) {
  1834. pt_debug(cd->dev, DL_ERROR,
  1835. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1836. __func__, size, read_len);
  1837. *actual_read_len = 0;
  1838. return -EIO;
  1839. }
  1840. memcpy(read_buf, cd->response_buf, size);
  1841. *actual_read_len = size;
  1842. return 0;
  1843. }
  1844. /*******************************************************************************
  1845. * FUNCTION: pt_hid_output_user_cmd
  1846. *
  1847. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1848. * the DUT.
  1849. *
  1850. * RETURN:
  1851. * 0 = success
  1852. * !0 = failure
  1853. *
  1854. * PARAMETERS:
  1855. * *cd - pointer to core data
  1856. * read_len - length of data to read
  1857. * *read_buf - pointer to store read data
  1858. * write_len - length of data to write
  1859. * *write_buf - pointer to buffer to write
  1860. * *actual_read_len - pointer to store data length actually read
  1861. ******************************************************************************/
  1862. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1863. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1864. u16 *actual_read_len)
  1865. {
  1866. int rc;
  1867. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1868. if (rc < 0) {
  1869. pt_debug(cd->dev, DL_ERROR,
  1870. "%s: fail get exclusive ex=%p own=%p\n",
  1871. __func__, cd->exclusive_dev, cd->dev);
  1872. return rc;
  1873. }
  1874. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1875. write_len, write_buf, actual_read_len);
  1876. if (release_exclusive(cd, cd->dev) < 0)
  1877. pt_debug(cd->dev, DL_ERROR,
  1878. "%s: fail to release exclusive\n", __func__);
  1879. return rc;
  1880. }
  1881. /*******************************************************************************
  1882. * FUNCTION: _pt_request_pip2_send_cmd
  1883. *
  1884. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1885. * interrupt and reads response data to read_buf
  1886. *
  1887. * RETURN:
  1888. * 0 = success
  1889. * !0 = failure
  1890. *
  1891. * PARAMETERS:
  1892. * *dev - pointer to device structure
  1893. * protect - flag to run in protected mode
  1894. * id - ID of PIP command
  1895. * *data - pointer to PIP data payload
  1896. * report_body_len - report length
  1897. * *read_buf - pointer to response buffer
  1898. * *actual_read_len - pointer to response buffer length
  1899. ******************************************************************************/
  1900. static int _pt_request_pip2_send_cmd(struct device *dev,
  1901. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1902. u16 *actual_read_len)
  1903. {
  1904. struct pt_core_data *cd = dev_get_drvdata(dev);
  1905. struct pip2_cmd_structure pip2_cmd;
  1906. int rc = 0;
  1907. int i = 0;
  1908. int j = 0;
  1909. u16 write_len;
  1910. u8 *write_buf = NULL;
  1911. u16 read_len;
  1912. u8 extra_bytes;
  1913. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1914. /* Hard coded register for PIP2.x */
  1915. pip2_cmd.reg[0] = 0x01;
  1916. pip2_cmd.reg[1] = 0x01;
  1917. /*
  1918. * For PIP2.1+ the length field value includes itself:
  1919. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1920. *
  1921. * The overall write length must include only the register:
  1922. * ADD 2: 2 (Register)
  1923. */
  1924. extra_bytes = 6;
  1925. write_len = 2;
  1926. /* PIP2 the CMD ID is a 7bit field */
  1927. if (id > PIP2_CMD_ID_END) {
  1928. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1929. __func__, id);
  1930. rc = -EINVAL;
  1931. goto exit;
  1932. }
  1933. pip2_cmd.len = report_body_len + extra_bytes;
  1934. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1935. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1936. pip2_cmd.data = data;
  1937. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1938. /* Add the command length to the extra bytes based on PIP version */
  1939. write_len += pip2_cmd.len;
  1940. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1941. __func__, pip2_cmd.len, write_len);
  1942. write_buf = kzalloc(write_len, GFP_KERNEL);
  1943. if (write_buf == NULL) {
  1944. rc = -ENOMEM;
  1945. goto exit;
  1946. }
  1947. write_buf[i++] = pip2_cmd.reg[0];
  1948. write_buf[i++] = pip2_cmd.reg[1];
  1949. write_buf[i++] = pip2_cmd.len & 0xff;
  1950. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1951. write_buf[i++] = pip2_cmd.seq;
  1952. write_buf[i++] = pip2_cmd.id;
  1953. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1954. write_buf[j] = pip2_cmd.data[j-i];
  1955. write_buf[j++] = pip2_cmd.crc[0];
  1956. write_buf[j++] = pip2_cmd.crc[1];
  1957. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1958. if (read_len < 0)
  1959. read_len = 255;
  1960. pt_debug(dev, DL_INFO,
  1961. "%s cmd_id[0x%02X] expected response length:%d ",
  1962. __func__, pip2_cmd.id, read_len);
  1963. /*
  1964. * All PIP2 commands come through this function.
  1965. * Set flag for PIP2.x interface to allow response parsing to know
  1966. * how to decode the protocol header.
  1967. */
  1968. mutex_lock(&cd->system_lock);
  1969. cd->pip2_prot_active = true;
  1970. cd->pip2_send_user_cmd = true;
  1971. mutex_unlock(&cd->system_lock);
  1972. if (protect == PT_CORE_CMD_PROTECTED)
  1973. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1974. write_len, write_buf, actual_read_len);
  1975. else {
  1976. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1977. write_len, write_buf, actual_read_len);
  1978. }
  1979. if (rc) {
  1980. pt_debug(dev, DL_ERROR,
  1981. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1982. __func__, rc);
  1983. goto exit;
  1984. }
  1985. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1986. *actual_read_len);
  1987. exit:
  1988. mutex_lock(&cd->system_lock);
  1989. cd->pip2_prot_active = false;
  1990. cd->pip2_send_user_cmd = false;
  1991. mutex_unlock(&cd->system_lock);
  1992. kfree(write_buf);
  1993. return rc;
  1994. }
  1995. /*******************************************************************************
  1996. * FUNCTION: _pt_pip2_send_cmd_no_int
  1997. *
  1998. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1999. * reads response data to read_buf if response is available.
  2000. *
  2001. * NOTE:
  2002. * Interrupt MUST be disabled before to call this function.
  2003. *
  2004. * RETURN:
  2005. * 0 = success
  2006. * !0 = failure
  2007. *
  2008. * PARAMETERS:
  2009. * *dev - pointer to device structure
  2010. * protect - flag to run in protected mode
  2011. * id - ID of PIP command
  2012. * *data - pointer to PIP data payload
  2013. * report_body_len - report length
  2014. * *read_buf - pointer to response buffer
  2015. * *actual_read_len - pointer to response buffer length
  2016. ******************************************************************************/
  2017. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2018. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2019. u16 *actual_read_len)
  2020. {
  2021. int max_retry = 0;
  2022. int retry = 0;
  2023. int rc = 0;
  2024. int i = 0;
  2025. int j = 0;
  2026. u16 write_len;
  2027. u8 *write_buf = NULL;
  2028. u16 read_len;
  2029. u16 size = 0;
  2030. u8 response_seq = 0;
  2031. u8 extra_bytes;
  2032. u32 retry_interval = 0;
  2033. u32 retry_total_time = 0;
  2034. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2035. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2036. struct pt_core_data *cd = dev_get_drvdata(dev);
  2037. struct pip2_cmd_structure pip2_cmd;
  2038. if (protect == PT_CORE_CMD_PROTECTED) {
  2039. rc = request_exclusive(cd,
  2040. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2041. if (rc < 0) {
  2042. pt_debug(cd->dev, DL_ERROR,
  2043. "%s: fail get exclusive ex=%p own=%p\n",
  2044. __func__, cd->exclusive_dev, cd->dev);
  2045. return rc;
  2046. }
  2047. }
  2048. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2049. /* Hard coded register for PIP2.x */
  2050. pip2_cmd.reg[0] = 0x01;
  2051. pip2_cmd.reg[1] = 0x01;
  2052. /*
  2053. * For PIP2.1+ the length field value includes itself:
  2054. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2055. *
  2056. * The overall write length must include only the register:
  2057. * ADD 2: 2 (Register)
  2058. */
  2059. extra_bytes = 6;
  2060. write_len = 2;
  2061. pip2_cmd.len = report_body_len + extra_bytes;
  2062. pip2_cmd.id = id;
  2063. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2064. pip2_cmd.data = data;
  2065. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2066. /* Add the command length to the extra bytes based on PIP version */
  2067. write_len += pip2_cmd.len;
  2068. write_buf = kzalloc(write_len, GFP_KERNEL);
  2069. if (write_buf == NULL) {
  2070. rc = -ENOMEM;
  2071. goto exit;
  2072. }
  2073. write_buf[i++] = pip2_cmd.reg[0];
  2074. write_buf[i++] = pip2_cmd.reg[1];
  2075. write_buf[i++] = pip2_cmd.len & 0xff;
  2076. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2077. write_buf[i++] = pip2_cmd.seq;
  2078. write_buf[i++] = pip2_cmd.id;
  2079. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2080. write_buf[j] = pip2_cmd.data[j-i];
  2081. write_buf[j++] = pip2_cmd.crc[0];
  2082. write_buf[j++] = pip2_cmd.crc[1];
  2083. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2084. if (read_len < 0)
  2085. read_len = 255;
  2086. pt_debug(dev, DL_INFO,
  2087. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2088. __func__, pip2_cmd.id, read_len);
  2089. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2090. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2091. if (rc) {
  2092. pt_debug(dev, DL_ERROR,
  2093. "%s: SPI write Error = %d\n",
  2094. __func__, rc);
  2095. goto exit;
  2096. }
  2097. #ifdef PT_POLL_RESP_BY_BUS
  2098. /*
  2099. * Frequent bus read can increase system load obviously. The expected
  2100. * first bus read should be valid and timely. The tollerance for
  2101. * usleep_range should be limited. The minimum response delay (between
  2102. * command finishes sending and INT pin falls) is less than 50
  2103. * microseconds. So the 10 microseconds should be maximum tollerance
  2104. * with the consideration that the unit to calculate the response delay
  2105. * is 10 microseconds and more precise is not necessary. Every
  2106. * additional 10 microseconds only contribute less than 3 milliseconds
  2107. * for whole BL.
  2108. */
  2109. usleep_range(resp_time_min, resp_time_min+10);
  2110. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2111. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2112. rc = pt_adap_read_default(cd, read_buf, read_len);
  2113. if (rc) {
  2114. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2115. __func__, rc);
  2116. break;
  2117. }
  2118. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2119. size = get_unaligned_le16(&read_buf[0]);
  2120. if ((size <= read_len) &&
  2121. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2122. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2123. break;
  2124. }
  2125. /*
  2126. * To reduce the bus and system load, increase the sleep
  2127. * step gradually:
  2128. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2129. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2130. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2131. */
  2132. retry++;
  2133. if (retry < 20) {
  2134. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2135. usleep_range(retry_interval,
  2136. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2137. } else if (retry < 40) {
  2138. retry_interval += 1000;
  2139. usleep_range(retry_interval, retry_interval + 1000);
  2140. } else {
  2141. retry_interval += 50000;
  2142. msleep(retry_interval/1000);
  2143. }
  2144. retry_total_time += retry_interval;
  2145. }
  2146. #else
  2147. /*
  2148. * Frequent GPIO read will not increase CPU/system load heavily if the
  2149. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2150. * fixed interval: 20 us.
  2151. */
  2152. usleep_range(resp_time_min, resp_time_min+10);
  2153. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2154. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2155. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2156. rc = pt_adap_read_default(cd, read_buf, read_len);
  2157. size = get_unaligned_le16(&read_buf[0]);
  2158. if (rc)
  2159. pt_debug(dev, DL_ERROR,
  2160. "%s: SPI read Error = %d\n",
  2161. __func__, rc);
  2162. else if (size > read_len) {
  2163. pt_debug(cd->dev, DL_ERROR,
  2164. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2165. __func__, size, read_len);
  2166. rc = -EIO;
  2167. }
  2168. break;
  2169. }
  2170. /*
  2171. * Poll GPIO with fixed interval 20 us, and tollerance is
  2172. * limited to 10 us to speed up the process.
  2173. */
  2174. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2175. usleep_range(retry_interval, retry_interval+10);
  2176. retry_total_time += retry_interval;
  2177. }
  2178. #endif
  2179. *actual_read_len = size;
  2180. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2181. pt_debug(dev, DL_ERROR,
  2182. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2183. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2184. *actual_read_len = 0;
  2185. rc = -EINVAL;
  2186. }
  2187. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2188. "<<< NO_INT Read");
  2189. exit:
  2190. kfree(write_buf);
  2191. if (protect == PT_CORE_CMD_PROTECTED) {
  2192. if (release_exclusive(cd, cd->dev) < 0)
  2193. pt_debug(cd->dev, DL_ERROR,
  2194. "%s: fail to release exclusive\n", __func__);
  2195. }
  2196. return rc;
  2197. }
  2198. /*******************************************************************************
  2199. * FUNCTION: pt_pip_null_
  2200. *
  2201. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2202. * This function is used by watchdog to check if the fw corrupts.
  2203. *
  2204. * RETURN:
  2205. * 0 = success
  2206. * !0 = failure
  2207. *
  2208. * PARAMETERS:
  2209. * *cd - pointer to core data
  2210. ******************************************************************************/
  2211. static int pt_pip_null_(struct pt_core_data *cd)
  2212. {
  2213. struct pt_hid_output hid_output = {
  2214. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2215. };
  2216. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2217. }
  2218. /*******************************************************************************
  2219. * FUNCTION: pt_pip_null
  2220. *
  2221. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2222. *
  2223. * RETURN:
  2224. * 0 = success
  2225. * !0 = failure
  2226. *
  2227. * PARAMETERS:
  2228. * *cd - pointer to core data
  2229. ******************************************************************************/
  2230. static int pt_pip_null(struct pt_core_data *cd)
  2231. {
  2232. int rc;
  2233. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2234. if (rc < 0) {
  2235. pt_debug(cd->dev, DL_ERROR,
  2236. "%s: fail get exclusive ex=%p own=%p\n",
  2237. __func__, cd->exclusive_dev, cd->dev);
  2238. return rc;
  2239. }
  2240. rc = pt_pip_null_(cd);
  2241. if (release_exclusive(cd, cd->dev) < 0)
  2242. pt_debug(cd->dev, DL_ERROR,
  2243. "%s: fail to release exclusive\n", __func__);
  2244. return rc;
  2245. }
  2246. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2247. /*******************************************************************************
  2248. * FUNCTION: pt_pip_start_bootloader_
  2249. *
  2250. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2251. *
  2252. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2253. * the WD active could cause this function to fail!
  2254. *
  2255. * RETURN:
  2256. * 0 = success
  2257. * !0 = failure
  2258. *
  2259. * PARAMETERS:
  2260. * *cd - pointer to core data
  2261. ******************************************************************************/
  2262. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2263. {
  2264. int rc = 0;
  2265. struct pt_hid_output hid_output = {
  2266. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2267. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2268. .reset_expected = 1,
  2269. };
  2270. if (cd->watchdog_enabled) {
  2271. pt_debug(cd->dev, DL_WARN,
  2272. "%s: watchdog isn't stopped before enter bl\n",
  2273. __func__);
  2274. goto exit;
  2275. }
  2276. /* Reset startup status after entering BL, new DUT enum required */
  2277. cd->startup_status = STARTUP_STATUS_START;
  2278. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2279. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2280. if (rc) {
  2281. pt_debug(cd->dev, DL_ERROR,
  2282. "%s: Start BL PIP cmd failed. rc = %d\n",
  2283. __func__, rc);
  2284. }
  2285. exit:
  2286. return rc;
  2287. }
  2288. /*******************************************************************************
  2289. * FUNCTION: pt_pip_start_bootloader
  2290. *
  2291. * SUMMARY: Protected function to force DUT to enter the BL
  2292. *
  2293. * RETURN:
  2294. * 0 = success
  2295. * !0 = failure
  2296. *
  2297. * PARAMETERS:
  2298. * *cd - pointer to core data structure
  2299. ******************************************************************************/
  2300. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2301. {
  2302. int rc;
  2303. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2304. if (rc < 0) {
  2305. pt_debug(cd->dev, DL_ERROR,
  2306. "%s: fail get exclusive ex=%p own=%p\n",
  2307. __func__, cd->exclusive_dev, cd->dev);
  2308. return rc;
  2309. }
  2310. rc = pt_pip_start_bootloader_(cd);
  2311. if (release_exclusive(cd, cd->dev) < 0)
  2312. pt_debug(cd->dev, DL_ERROR,
  2313. "%s: fail to release exclusive\n", __func__);
  2314. return rc;
  2315. }
  2316. /*******************************************************************************
  2317. * FUNCTION: _pt_request_pip_start_bl
  2318. *
  2319. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2320. * modules to request the DUT to enter the BL
  2321. *
  2322. * RETURN:
  2323. * 0 = success
  2324. * !0 = failure
  2325. *
  2326. * PARAMETERS:
  2327. * *dev - pointer to device structure
  2328. * protect - flag to run in protected mode
  2329. ******************************************************************************/
  2330. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2331. {
  2332. struct pt_core_data *cd = dev_get_drvdata(dev);
  2333. if (protect)
  2334. return pt_pip_start_bootloader(cd);
  2335. return pt_pip_start_bootloader_(cd);
  2336. }
  2337. /*******************************************************************************
  2338. * FUNCTION: pt_pip2_ver_load_ttdata
  2339. *
  2340. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2341. * command into the core data struct.
  2342. *
  2343. * RETURN: n/a
  2344. *
  2345. * PARAMETERS:
  2346. * *cd - pointer to core data structure
  2347. * len - Length of data in response_buf
  2348. ******************************************************************************/
  2349. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2350. {
  2351. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2352. struct pt_pip2_version_full *full_ver;
  2353. struct pt_pip2_version *ver;
  2354. /*
  2355. * The PIP2 VERSION command can return different lengths of data.
  2356. * The additional LOT fields are included when the packet
  2357. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2358. * NOTE:
  2359. * - The FW would swap the BL and FW versions when reporting
  2360. * the small packet.
  2361. * - Sub Lot bytes 16 and 17 are reserved.
  2362. */
  2363. if (len >= 0x1D) {
  2364. full_ver = (struct pt_pip2_version_full *)
  2365. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2366. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2367. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2368. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2369. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2370. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2371. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2372. /*
  2373. * BL PIP 2.02 and greater the version fields are
  2374. * swapped
  2375. */
  2376. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2377. ttdata->chip_rev =
  2378. get_unaligned_le16(&full_ver->chip_rev);
  2379. ttdata->chip_id =
  2380. get_unaligned_le16(&full_ver->chip_id);
  2381. } else {
  2382. ttdata->chip_rev =
  2383. get_unaligned_le16(&full_ver->chip_id);
  2384. ttdata->chip_id =
  2385. get_unaligned_le16(&full_ver->chip_rev);
  2386. }
  2387. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2388. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2389. sizeof(struct pt_pip2_version_full),
  2390. "PIP2 VERSION FULL");
  2391. } else {
  2392. ver = (struct pt_pip2_version *)
  2393. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2394. ttdata->pip_ver_major = ver->pip2_version_msb;
  2395. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2396. ttdata->bl_ver_major = ver->bl_version_msb;
  2397. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2398. ttdata->fw_ver_major = ver->fw_version_msb;
  2399. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2400. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2401. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2402. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2403. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2404. }
  2405. }
  2406. /*******************************************************************************
  2407. * FUNCTION: pt_si_get_ttdata
  2408. *
  2409. * SUMMARY: Function to load the version information from the system information
  2410. * PIP command into the core data struct.
  2411. *
  2412. * RETURN: n/a
  2413. *
  2414. * PARAMETERS:
  2415. * *cd - pointer to core data structure
  2416. ******************************************************************************/
  2417. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2418. {
  2419. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2420. struct pt_ttdata_dev *ttdata_dev =
  2421. (struct pt_ttdata_dev *)
  2422. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2423. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2424. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2425. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2426. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2427. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2428. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2429. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2430. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2431. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2432. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2433. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2434. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2435. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2436. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2437. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2438. }
  2439. /*******************************************************************************
  2440. * FUNCTION: pt_si_get_sensing_conf_data
  2441. *
  2442. * SUMMARY: Function to load the sensing information from the system information
  2443. * PIP command into the core data struct.
  2444. *
  2445. * RETURN: n/a
  2446. *
  2447. * PARAMETERS:
  2448. * *cd - pointer to core data structure
  2449. ******************************************************************************/
  2450. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2451. {
  2452. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2453. struct pt_sensing_conf_data_dev *scd_dev =
  2454. (struct pt_sensing_conf_data_dev *)
  2455. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2456. scd->electrodes_x = scd_dev->electrodes_x;
  2457. scd->electrodes_y = scd_dev->electrodes_y;
  2458. scd->origin_x = scd_dev->origin_x;
  2459. scd->origin_y = scd_dev->origin_y;
  2460. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2461. if (scd->origin_x & 0x02) {
  2462. scd->tx_num = scd->electrodes_x;
  2463. scd->rx_num = scd->electrodes_y;
  2464. } else {
  2465. scd->tx_num = scd->electrodes_y;
  2466. scd->rx_num = scd->electrodes_x;
  2467. }
  2468. /*
  2469. * When the Panel ID is coming from an XY pin and not a dedicated
  2470. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2471. * other DUTs as the loader will use cd->pid_for_loader to generate
  2472. * the bin file name but will ignore it if pid_for_loader is still
  2473. * set to PANEL_ID_NOT_ENABLED
  2474. */
  2475. if (cd->panel_id_support &
  2476. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2477. mutex_lock(&cd->system_lock);
  2478. cd->pid_for_loader = scd_dev->panel_id;
  2479. mutex_unlock(&cd->system_lock);
  2480. }
  2481. scd->panel_id = scd_dev->panel_id;
  2482. scd->btn = scd_dev->btn;
  2483. scd->scan_mode = scd_dev->scan_mode;
  2484. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2485. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2486. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2487. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2488. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2489. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2490. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2491. sizeof(struct pt_sensing_conf_data_dev),
  2492. "sensing_conf_data");
  2493. }
  2494. /*******************************************************************************
  2495. * FUNCTION: pt_si_setup
  2496. *
  2497. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2498. *
  2499. * RETURN:
  2500. * 0 = success
  2501. * !0 = failure
  2502. *
  2503. * PARAMETERS:
  2504. * *cd - pointer to core data structure
  2505. ******************************************************************************/
  2506. static int pt_si_setup(struct pt_core_data *cd)
  2507. {
  2508. struct pt_sysinfo *si = &cd->sysinfo;
  2509. int max_tch = si->sensing_conf_data.max_tch;
  2510. if (!si->xy_data)
  2511. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2512. GFP_KERNEL);
  2513. if (!si->xy_data)
  2514. return -ENOMEM;
  2515. if (!si->xy_mode)
  2516. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2517. if (!si->xy_mode) {
  2518. kfree(si->xy_data);
  2519. return -ENOMEM;
  2520. }
  2521. return 0;
  2522. }
  2523. /*******************************************************************************
  2524. * FUNCTION: pt_si_get_btn_data
  2525. *
  2526. * SUMMARY: Setup the core data button information based on the response of the
  2527. * System Information PIP command.
  2528. *
  2529. * RETURN:
  2530. * 0 = success
  2531. * !0 = failure
  2532. *
  2533. * PARAMETERS:
  2534. * *cd - pointer to core data structure
  2535. ******************************************************************************/
  2536. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2537. {
  2538. struct pt_sysinfo *si = &cd->sysinfo;
  2539. int num_btns = 0;
  2540. int num_defined_keys;
  2541. u16 *key_table;
  2542. int btn;
  2543. int i;
  2544. int rc = 0;
  2545. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2546. & PIP1_SYSINFO_BTN_MASK;
  2547. size_t btn_keys_size;
  2548. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2549. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2550. if (btns & (1 << i))
  2551. num_btns++;
  2552. }
  2553. si->num_btns = num_btns;
  2554. if (num_btns) {
  2555. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2556. if (!si->btn)
  2557. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2558. if (!si->btn)
  2559. return -ENOMEM;
  2560. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2561. num_defined_keys = 0;
  2562. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2563. num_defined_keys = 0;
  2564. else
  2565. num_defined_keys = cd->cpdata->sett
  2566. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2567. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2568. key_table = (u16 *)cd->cpdata->sett
  2569. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2570. si->btn[btn].key_code = key_table[btn];
  2571. si->btn[btn].enabled = true;
  2572. }
  2573. for (; btn < num_btns; btn++) {
  2574. si->btn[btn].key_code = KEY_RESERVED;
  2575. si->btn[btn].enabled = true;
  2576. }
  2577. return rc;
  2578. }
  2579. kfree(si->btn);
  2580. si->btn = NULL;
  2581. return rc;
  2582. }
  2583. /*******************************************************************************
  2584. * FUNCTION: pt_si_put_log_data
  2585. *
  2586. * SUMMARY: Prints all sys info data to kmsg log
  2587. *
  2588. * RETURN: n/a
  2589. *
  2590. * PARAMETERS:
  2591. * *cd - pointer to core data structure
  2592. ******************************************************************************/
  2593. static void pt_si_put_log_data(struct pt_core_data *cd)
  2594. {
  2595. struct pt_sysinfo *si = &cd->sysinfo;
  2596. struct pt_ttdata *ttdata = &si->ttdata;
  2597. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2598. int i;
  2599. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2600. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2601. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2602. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2603. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2604. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2605. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2606. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2608. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2610. __func__, ttdata->revctrl, ttdata->revctrl);
  2611. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2612. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2614. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2616. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2618. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2620. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2621. for (i = 0; i < PT_NUM_MFGID; i++)
  2622. pt_debug(cd->dev, DL_DEBUG,
  2623. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2624. __func__, i, ttdata->mfg_id[i],
  2625. ttdata->mfg_id[i]);
  2626. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2627. __func__, ttdata->post_code, ttdata->post_code);
  2628. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2629. __func__, scd->electrodes_x, scd->electrodes_x);
  2630. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2631. __func__, scd->electrodes_y, scd->electrodes_y);
  2632. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2633. __func__, scd->len_x, scd->len_x);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2635. __func__, scd->len_y, scd->len_y);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2637. __func__, scd->res_x, scd->res_x);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2639. __func__, scd->res_y, scd->res_y);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2641. __func__, scd->max_z, scd->max_z);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2643. __func__, scd->origin_x, scd->origin_x);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2645. __func__, scd->origin_y, scd->origin_y);
  2646. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2647. __func__, scd->panel_id, scd->panel_id);
  2648. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2649. __func__, scd->btn, scd->btn);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2651. __func__, scd->scan_mode, scd->scan_mode);
  2652. pt_debug(cd->dev, DL_DEBUG,
  2653. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2654. __func__, scd->max_tch, scd->max_tch);
  2655. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2656. __func__, si->xy_mode);
  2657. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2658. __func__, si->xy_data);
  2659. }
  2660. /*******************************************************************************
  2661. * FUNCTION: pt_get_sysinfo_regs
  2662. *
  2663. * SUMMARY: Setup all the core data System information based on the response
  2664. * of the System Information PIP command.
  2665. *
  2666. * RETURN:
  2667. * 0 = success
  2668. * !0 = failure
  2669. *
  2670. * PARAMETERS:
  2671. * *cd - pointer to core data structure
  2672. ******************************************************************************/
  2673. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2674. {
  2675. struct pt_sysinfo *si = &cd->sysinfo;
  2676. int rc;
  2677. rc = pt_si_get_btn_data(cd);
  2678. if (rc < 0)
  2679. return rc;
  2680. pt_si_get_ttdata(cd);
  2681. pt_si_get_sensing_conf_data(cd);
  2682. pt_si_setup(cd);
  2683. pt_si_put_log_data(cd);
  2684. si->ready = true;
  2685. return rc;
  2686. }
  2687. /*******************************************************************************
  2688. * FUNCTION: pt_free_si_ptrs
  2689. *
  2690. * SUMMARY: Frees all memory associated with the System Information within
  2691. * core data
  2692. *
  2693. * RETURN: n/a
  2694. *
  2695. * PARAMETERS:
  2696. * *cd - pointer to core data structure
  2697. ******************************************************************************/
  2698. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2699. {
  2700. struct pt_sysinfo *si = &cd->sysinfo;
  2701. kfree(si->btn);
  2702. kfree(si->xy_mode);
  2703. kfree(si->xy_data);
  2704. }
  2705. /*******************************************************************************
  2706. * FUNCTION: pt_hid_output_get_sysinfo_
  2707. *
  2708. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2709. * response.
  2710. *
  2711. * RETURN::
  2712. * 0 = success
  2713. * !0 = failure
  2714. *
  2715. * PARAMETERS:
  2716. * *cd - pointer to core data structure
  2717. ******************************************************************************/
  2718. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2719. {
  2720. int rc = 0;
  2721. struct pt_hid_output hid_output = {
  2722. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2723. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2724. };
  2725. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2726. if (rc)
  2727. return rc;
  2728. /* Parse the sysinfo data */
  2729. rc = pt_get_sysinfo_regs(cd);
  2730. if (rc)
  2731. pt_free_si_ptrs(cd);
  2732. return rc;
  2733. }
  2734. /*******************************************************************************
  2735. * FUNCTION: pt_hid_output_get_sysinfo
  2736. *
  2737. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2738. *
  2739. * RETURN::
  2740. * 0 = success
  2741. * !0 = failure
  2742. *
  2743. * PARAMETERS:
  2744. * *cd - pointer to core data structure
  2745. ******************************************************************************/
  2746. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2747. {
  2748. int rc;
  2749. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2750. if (rc < 0) {
  2751. pt_debug(cd->dev, DL_ERROR,
  2752. "%s: fail get exclusive ex=%p own=%p\n",
  2753. __func__, cd->exclusive_dev, cd->dev);
  2754. return rc;
  2755. }
  2756. rc = pt_hid_output_get_sysinfo_(cd);
  2757. if (release_exclusive(cd, cd->dev) < 0)
  2758. pt_debug(cd->dev, DL_ERROR,
  2759. "%s: fail to release exclusive\n", __func__);
  2760. return rc;
  2761. }
  2762. /*******************************************************************************
  2763. * FUNCTION: pt_pip_suspend_scanning_
  2764. *
  2765. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2766. *
  2767. * RETURN::
  2768. * 0 = success
  2769. * !0 = failure
  2770. *
  2771. * PARAMETERS:
  2772. * *cd - pointer to core data structure
  2773. ******************************************************************************/
  2774. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2775. {
  2776. int rc = 0;
  2777. struct pt_hid_output hid_output = {
  2778. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2779. };
  2780. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2781. if (rc) {
  2782. pt_debug(cd->dev, DL_ERROR,
  2783. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2784. __func__, rc);
  2785. }
  2786. return rc;
  2787. }
  2788. /*******************************************************************************
  2789. * FUNCTION: pt_pip_suspend_scanning
  2790. *
  2791. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2792. *
  2793. * RETURN::
  2794. * 0 = success
  2795. * !0 = failure
  2796. *
  2797. * PARAMETERS:
  2798. * *cd - pointer to core data structure
  2799. ******************************************************************************/
  2800. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2801. {
  2802. int rc;
  2803. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2804. if (rc < 0) {
  2805. pt_debug(cd->dev, DL_ERROR,
  2806. "%s: fail get exclusive ex=%p own=%p\n",
  2807. __func__, cd->exclusive_dev, cd->dev);
  2808. return rc;
  2809. }
  2810. rc = pt_pip_suspend_scanning_(cd);
  2811. if (release_exclusive(cd, cd->dev) < 0)
  2812. pt_debug(cd->dev, DL_ERROR,
  2813. "%s: fail to release exclusive\n", __func__);
  2814. return rc;
  2815. }
  2816. /*******************************************************************************
  2817. * FUNCTION: _pt_request_pip_suspend_scanning
  2818. *
  2819. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2820. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2821. *
  2822. * RETURN:
  2823. * 0 = success
  2824. * !0 = failure
  2825. *
  2826. * PARAMETERS:
  2827. * *dev - pointer to device structure
  2828. * protect - 0 = call non-protected function
  2829. * 1 = call protected function
  2830. ******************************************************************************/
  2831. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2832. int protect)
  2833. {
  2834. struct pt_core_data *cd = dev_get_drvdata(dev);
  2835. if (protect)
  2836. return pt_pip_suspend_scanning(cd);
  2837. return pt_pip_suspend_scanning_(cd);
  2838. }
  2839. /*******************************************************************************
  2840. * FUNCTION: pt_pip_resume_scanning_
  2841. *
  2842. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2843. *
  2844. * RETURN::
  2845. * 0 = success
  2846. * !0 = failure
  2847. *
  2848. * PARAMETERS:
  2849. * *cd - pointer to core data structure
  2850. ******************************************************************************/
  2851. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2852. {
  2853. int rc = 0;
  2854. struct pt_hid_output hid_output = {
  2855. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2856. };
  2857. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2858. if (rc) {
  2859. pt_debug(cd->dev, DL_ERROR,
  2860. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2861. __func__, rc);
  2862. }
  2863. return rc;
  2864. }
  2865. /*******************************************************************************
  2866. * FUNCTION: pt_pip_resume_scanning
  2867. *
  2868. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2869. *
  2870. * RETURN::
  2871. * 0 = success
  2872. * !0 = failure
  2873. *
  2874. * PARAMETERS:
  2875. * *cd - pointer to core data structure
  2876. ******************************************************************************/
  2877. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2878. {
  2879. int rc;
  2880. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2881. if (rc < 0) {
  2882. pt_debug(cd->dev, DL_ERROR,
  2883. "%s: fail get exclusive ex=%p own=%p\n",
  2884. __func__, cd->exclusive_dev, cd->dev);
  2885. return rc;
  2886. }
  2887. rc = pt_pip_resume_scanning_(cd);
  2888. if (release_exclusive(cd, cd->dev) < 0)
  2889. pt_debug(cd->dev, DL_ERROR,
  2890. "%s: fail to release exclusive\n", __func__);
  2891. return rc;
  2892. }
  2893. /*******************************************************************************
  2894. * FUNCTION: _pt_request_pip_resume_scanning
  2895. *
  2896. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2897. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2898. *
  2899. * RETURN::
  2900. * 0 = success
  2901. * !0 = failure
  2902. *
  2903. * PARAMETERS:
  2904. * *dev - pointer to device structure
  2905. * protect - 0 = call non-protected function
  2906. * 1 = call protected function
  2907. ******************************************************************************/
  2908. static int _pt_request_pip_resume_scanning(struct device *dev,
  2909. int protect)
  2910. {
  2911. struct pt_core_data *cd = dev_get_drvdata(dev);
  2912. if (protect)
  2913. return pt_pip_resume_scanning(cd);
  2914. return pt_pip_resume_scanning_(cd);
  2915. }
  2916. /*******************************************************************************
  2917. * FUNCTION: pt_pip_get_param_
  2918. *
  2919. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2920. * the 32bit parameter value
  2921. *
  2922. * RETURN:
  2923. * 0 = success
  2924. * !0 = failure
  2925. *
  2926. * PARAMETERS:
  2927. * *cd - pointer to core data
  2928. * param_id - parameter ID to retrieve
  2929. * *value - value of DUT parameter
  2930. ******************************************************************************/
  2931. static int pt_pip_get_param_(struct pt_core_data *cd,
  2932. u8 param_id, u32 *value)
  2933. {
  2934. int write_length = 1;
  2935. u8 param[1] = { param_id };
  2936. u8 read_param_id;
  2937. int param_size;
  2938. u8 *ptr;
  2939. int rc = 0;
  2940. int i;
  2941. struct pt_hid_output hid_output = {
  2942. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2943. .write_length = write_length,
  2944. .write_buf = param,
  2945. };
  2946. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2947. if (rc)
  2948. return rc;
  2949. read_param_id = cd->response_buf[5];
  2950. if (read_param_id != param_id)
  2951. return -EPROTO;
  2952. param_size = cd->response_buf[6];
  2953. ptr = &cd->response_buf[7];
  2954. *value = 0;
  2955. for (i = 0; i < param_size; i++)
  2956. *value += ptr[i] << (i * 8);
  2957. return 0;
  2958. }
  2959. /*******************************************************************************
  2960. * FUNCTION: pt_pip_get_param
  2961. *
  2962. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2963. *
  2964. * RETURN:
  2965. * 0 = success
  2966. * !0 = failure
  2967. *
  2968. * PARAMETERS:
  2969. * *cd - pointer to core data
  2970. * param_id - parameter ID to retrieve
  2971. * *value - value of DUT parameter
  2972. ******************************************************************************/
  2973. static int pt_pip_get_param(struct pt_core_data *cd,
  2974. u8 param_id, u32 *value)
  2975. {
  2976. int rc;
  2977. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2978. if (rc < 0) {
  2979. pt_debug(cd->dev, DL_ERROR,
  2980. "%s: fail get exclusive ex=%p own=%p\n",
  2981. __func__, cd->exclusive_dev, cd->dev);
  2982. return rc;
  2983. }
  2984. rc = pt_pip_get_param_(cd, param_id, value);
  2985. if (release_exclusive(cd, cd->dev) < 0)
  2986. pt_debug(cd->dev, DL_ERROR,
  2987. "%s: fail to release exclusive\n", __func__);
  2988. return rc;
  2989. }
  2990. /*******************************************************************************
  2991. * FUNCTION: _pt_request_pip_get_param
  2992. *
  2993. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2994. * calls to the protected or unprotected call to pt_pip_get_param
  2995. *
  2996. * RETURN:
  2997. * 0 = success
  2998. * !0 = failure
  2999. *
  3000. * PARAMETERS:
  3001. * *dev - pointer to device structure
  3002. * protect - flag to call protected or non protected function
  3003. * param_id - parameter ID to retrieve
  3004. * *value - value of DUT parameter
  3005. ******************************************************************************/
  3006. int _pt_request_pip_get_param(struct device *dev,
  3007. int protect, u8 param_id, u32 *value)
  3008. {
  3009. struct pt_core_data *cd = dev_get_drvdata(dev);
  3010. if (protect)
  3011. return pt_pip_get_param(cd, param_id, value);
  3012. return pt_pip_get_param_(cd, param_id, value);
  3013. }
  3014. /*******************************************************************************
  3015. * FUNCTION: pt_pip_set_param_
  3016. *
  3017. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3018. * passed in value to flash
  3019. *
  3020. * RETURN:
  3021. * 0 = success
  3022. * !0 = failure
  3023. *
  3024. * PARAMETERS:
  3025. * *cd - pointer to core data
  3026. * param_id - parameter ID to set
  3027. * value - value to write
  3028. * size - size to write
  3029. ******************************************************************************/
  3030. static int pt_pip_set_param_(struct pt_core_data *cd,
  3031. u8 param_id, u32 value, u8 size)
  3032. {
  3033. u8 write_buf[6];
  3034. u8 *ptr = &write_buf[2];
  3035. int rc = 0;
  3036. int i;
  3037. struct pt_hid_output hid_output = {
  3038. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3039. .write_buf = write_buf,
  3040. };
  3041. write_buf[0] = param_id;
  3042. write_buf[1] = size;
  3043. for (i = 0; i < size; i++) {
  3044. ptr[i] = value & 0xFF;
  3045. value = value >> 8;
  3046. }
  3047. hid_output.write_length = 2 + size;
  3048. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3049. if (rc)
  3050. return rc;
  3051. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3052. return -EPROTO;
  3053. return 0;
  3054. }
  3055. /*******************************************************************************
  3056. * FUNCTION: pt_pip_set_param
  3057. *
  3058. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3059. *
  3060. * RETURN:
  3061. * 0 = success
  3062. * !0 = failure
  3063. *
  3064. * PARAMETERS:
  3065. * *cd - pointer to core data
  3066. * param_id - parameter ID to set
  3067. * value - value to write
  3068. * size - size to write
  3069. ******************************************************************************/
  3070. static int pt_pip_set_param(struct pt_core_data *cd,
  3071. u8 param_id, u32 value, u8 size)
  3072. {
  3073. int rc;
  3074. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3075. if (rc < 0) {
  3076. pt_debug(cd->dev, DL_ERROR,
  3077. "%s: fail get exclusive ex=%p own=%p\n",
  3078. __func__, cd->exclusive_dev, cd->dev);
  3079. return rc;
  3080. }
  3081. rc = pt_pip_set_param_(cd, param_id, value, size);
  3082. if (release_exclusive(cd, cd->dev) < 0)
  3083. pt_debug(cd->dev, DL_ERROR,
  3084. "%s: fail to release exclusive\n", __func__);
  3085. return rc;
  3086. }
  3087. /*******************************************************************************
  3088. * FUNCTION: _pt_request_pip_set_param
  3089. *
  3090. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3091. * calls to the protected or unprotected call to pt_pip_set_param
  3092. *
  3093. * RETURN:
  3094. * 0 = success
  3095. * !0 = failure
  3096. *
  3097. * PARAMETERS:
  3098. * *dev - pointer to device structure
  3099. * protect - flag to call protected or non-protected
  3100. * param_id - parameter ID to set
  3101. * value - value to write
  3102. * size - size to write
  3103. ******************************************************************************/
  3104. int _pt_request_pip_set_param(struct device *dev, int protect,
  3105. u8 param_id, u32 value, u8 size)
  3106. {
  3107. struct pt_core_data *cd = dev_get_drvdata(dev);
  3108. if (protect)
  3109. return pt_pip_set_param(cd, param_id, value, size);
  3110. return pt_pip_set_param_(cd, param_id, value, size);
  3111. }
  3112. /*******************************************************************************
  3113. * FUNCTION: _pt_pip_enter_easywake_state_
  3114. *
  3115. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3116. *
  3117. * RETURN:
  3118. * 0 = success
  3119. * !0 = failure
  3120. *
  3121. * PARAMETERS:
  3122. * *dev - pointer to device structure
  3123. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3124. * *return_data - return status if easywake was entered
  3125. ******************************************************************************/
  3126. static int pt_hid_output_enter_easywake_state_(
  3127. struct pt_core_data *cd, u8 data, u8 *return_data)
  3128. {
  3129. int write_length = 1;
  3130. u8 param[1] = { data };
  3131. int rc = 0;
  3132. struct pt_hid_output hid_output = {
  3133. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3134. .write_length = write_length,
  3135. .write_buf = param,
  3136. };
  3137. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3138. if (rc)
  3139. return rc;
  3140. *return_data = cd->response_buf[5];
  3141. return rc;
  3142. }
  3143. /*******************************************************************************
  3144. * FUNCTION: pt_pip_verify_config_block_crc_
  3145. *
  3146. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3147. *
  3148. * RETURN:
  3149. * 0 = success
  3150. * !0 = failure
  3151. *
  3152. * PARAMETERS:
  3153. * *cd - pointer the core data structure
  3154. * ebid - enumerated block ID
  3155. * *status - PIP command status
  3156. * calculated_crc - calculated CRC
  3157. * stored_crc - stored CRC in config area
  3158. ******************************************************************************/
  3159. static int pt_pip_verify_config_block_crc_(
  3160. struct pt_core_data *cd, u8 ebid, u8 *status,
  3161. u16 *calculated_crc, u16 *stored_crc)
  3162. {
  3163. int write_length = 1;
  3164. u8 param[1] = { ebid };
  3165. u8 *ptr;
  3166. int rc = 0;
  3167. struct pt_hid_output hid_output = {
  3168. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3169. .write_length = write_length,
  3170. .write_buf = param,
  3171. };
  3172. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3173. if (rc)
  3174. return rc;
  3175. ptr = &cd->response_buf[5];
  3176. *status = ptr[0];
  3177. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3178. *stored_crc = get_unaligned_le16(&ptr[3]);
  3179. return 0;
  3180. }
  3181. /*******************************************************************************
  3182. * FUNCTION: pt_pip_verify_config_block_crc
  3183. *
  3184. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3185. * an exclusive access to the DUT.
  3186. *
  3187. * RETURN:
  3188. * 0 = success
  3189. * !0 = failure
  3190. *
  3191. * PARAMETERS:
  3192. * *cd - pointer the core data structure
  3193. * ebid - enumerated block ID
  3194. * *status - PIP command status
  3195. * calculated_crc - calculated CRC
  3196. * stored_crc - stored CRC in config area
  3197. ******************************************************************************/
  3198. static int pt_pip_verify_config_block_crc(
  3199. struct pt_core_data *cd, u8 ebid, u8 *status,
  3200. u16 *calculated_crc, u16 *stored_crc)
  3201. {
  3202. int rc;
  3203. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3204. if (rc < 0) {
  3205. pt_debug(cd->dev, DL_ERROR,
  3206. "%s: fail get exclusive ex=%p own=%p\n",
  3207. __func__, cd->exclusive_dev, cd->dev);
  3208. return rc;
  3209. }
  3210. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3211. calculated_crc, stored_crc);
  3212. if (release_exclusive(cd, cd->dev) < 0)
  3213. pt_debug(cd->dev, DL_ERROR,
  3214. "%s: fail to release exclusive\n", __func__);
  3215. return rc;
  3216. }
  3217. /*******************************************************************************
  3218. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3219. *
  3220. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3221. * calls to the protected or unprotected call to
  3222. * pt_pip_verify_config_block_crc_
  3223. *
  3224. * RETURN:
  3225. * 0 = success
  3226. * !0 = failure
  3227. *
  3228. * PARAMETERS:
  3229. * *dev - pointer to device structure
  3230. * protect - flag to call protected or non-protected
  3231. * ebid - enumerated block ID
  3232. * *status - PIP command status
  3233. * calculated_crc - calculated CRC
  3234. * stored_crc - stored CRC in config area
  3235. ******************************************************************************/
  3236. static int _pt_request_pip_verify_config_block_crc(
  3237. struct device *dev, int protect, u8 ebid, u8 *status,
  3238. u16 *calculated_crc, u16 *stored_crc)
  3239. {
  3240. struct pt_core_data *cd = dev_get_drvdata(dev);
  3241. if (protect)
  3242. return pt_pip_verify_config_block_crc(cd, ebid,
  3243. status, calculated_crc, stored_crc);
  3244. return pt_pip_verify_config_block_crc_(cd, ebid,
  3245. status, calculated_crc, stored_crc);
  3246. }
  3247. /*******************************************************************************
  3248. * FUNCTION: pt_pip_get_config_row_size_
  3249. *
  3250. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3251. *
  3252. * RETURN:
  3253. * 0 = success
  3254. * !0 = failure
  3255. *
  3256. * PARAMETERS:
  3257. * *cd - pointer to core data
  3258. * protect - flag to call protected or non-protected
  3259. * *row_size - pointer to store the retrieved row size
  3260. ******************************************************************************/
  3261. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3262. u16 *row_size)
  3263. {
  3264. int rc = 0;
  3265. struct pt_hid_output hid_output = {
  3266. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3267. };
  3268. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3269. if (rc)
  3270. return rc;
  3271. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3272. return 0;
  3273. }
  3274. /*******************************************************************************
  3275. * FUNCTION: pt_pip_get_config_row_size
  3276. *
  3277. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3278. * an exclusive access to the DUT.
  3279. *
  3280. * RETURN:
  3281. * 0 = success
  3282. * !0 = failure
  3283. *
  3284. * PARAMETERS:
  3285. * *cd - pointer to core data
  3286. * protect - flag to call protected or non-protected
  3287. * *row_size - pointer to store the retrieved row size
  3288. ******************************************************************************/
  3289. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3290. u16 *row_size)
  3291. {
  3292. int rc;
  3293. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3294. if (rc < 0) {
  3295. pt_debug(cd->dev, DL_ERROR,
  3296. "%s: fail get exclusive ex=%p own=%p\n",
  3297. __func__, cd->exclusive_dev, cd->dev);
  3298. return rc;
  3299. }
  3300. rc = pt_pip_get_config_row_size_(cd, row_size);
  3301. if (release_exclusive(cd, cd->dev) < 0)
  3302. pt_debug(cd->dev, DL_ERROR,
  3303. "%s: fail to release exclusive\n", __func__);
  3304. return rc;
  3305. }
  3306. /*******************************************************************************
  3307. * FUNCTION: _pt_request_pip_get_config_row_size
  3308. *
  3309. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3310. * calls to the protected or unprotected call to
  3311. * pt_pip_get_config_row_size_
  3312. *
  3313. * RETURN:
  3314. * 0 = success
  3315. * !0 = failure
  3316. *
  3317. * PARAMETERS:
  3318. * *dev - pointer to device structure
  3319. * protect - flag to call protected or non-protected
  3320. * *row_size - pointer to store the retrieved row size
  3321. ******************************************************************************/
  3322. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3323. int protect, u16 *row_size)
  3324. {
  3325. struct pt_core_data *cd = dev_get_drvdata(dev);
  3326. if (protect)
  3327. return pt_pip_get_config_row_size(cd, row_size);
  3328. return pt_pip_get_config_row_size_(cd, row_size);
  3329. }
  3330. /*******************************************************************************
  3331. * FUNCTION: pt_pip1_read_data_block_
  3332. *
  3333. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3334. * output data to the "read_buf" and update "crc".
  3335. *
  3336. * RETURN:
  3337. * 0 = success
  3338. * !0 = failure
  3339. *
  3340. * PARAMETERS:
  3341. * *cd - pointer to core data
  3342. * row_number - row number
  3343. * length - length of data to read
  3344. * ebid - block id
  3345. * *actual_read_len - Actual data length read
  3346. * *read_buf - pointer to the buffer to store read data
  3347. * read_buf_size - size of read_buf
  3348. * *crc - pointer to store CRC of row data
  3349. ******************************************************************************/
  3350. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3351. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3352. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3353. {
  3354. int read_ebid;
  3355. int status;
  3356. int rc = 0;
  3357. int write_length = 5;
  3358. u8 write_buf[5];
  3359. u8 cmd_offset = 0;
  3360. u16 calc_crc;
  3361. struct pt_hid_output hid_output = {
  3362. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3363. .write_length = write_length,
  3364. .write_buf = write_buf,
  3365. };
  3366. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3367. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3368. write_buf[cmd_offset++] = LOW_BYTE(length);
  3369. write_buf[cmd_offset++] = HI_BYTE(length);
  3370. write_buf[cmd_offset++] = ebid;
  3371. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3372. if (rc)
  3373. return rc;
  3374. status = cd->response_buf[5];
  3375. if (status)
  3376. return status;
  3377. read_ebid = cd->response_buf[6];
  3378. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3379. return -EPROTO;
  3380. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3381. if (length == 0 || *actual_read_len == 0)
  3382. return 0;
  3383. if (read_buf_size >= *actual_read_len)
  3384. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3385. else
  3386. return -EPROTO;
  3387. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3388. /* Validate Row Data CRC */
  3389. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3390. if (*crc == calc_crc) {
  3391. return 0;
  3392. } else {
  3393. pt_debug(cd->dev, DL_ERROR,
  3394. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3395. __func__, *crc, calc_crc);
  3396. return -EPROTO;
  3397. }
  3398. }
  3399. /*******************************************************************************
  3400. * FUNCTION: _pt_request_pip_read_data_block
  3401. *
  3402. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3403. * calls to pt_pip1_read_data_block_
  3404. *
  3405. * RETURN:
  3406. * 0 = success
  3407. * !0 = failure
  3408. *
  3409. * PARAMETERS:
  3410. * *dev - pointer to device structure
  3411. * row_number - row number
  3412. * length - length of data to read
  3413. * ebid - block id
  3414. * *actual_read_len - Actual data length read
  3415. * *read_buf - pointer to the buffer to store read data
  3416. * *crc - pointer to store CRC of row data
  3417. ******************************************************************************/
  3418. static int _pt_request_pip_read_data_block(struct device *dev,
  3419. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3420. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3421. {
  3422. struct pt_core_data *cd = dev_get_drvdata(dev);
  3423. return pt_pip1_read_data_block_(cd, row_number, length,
  3424. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3425. }
  3426. /*******************************************************************************
  3427. * FUNCTION: pt_pip1_write_data_block_
  3428. *
  3429. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3430. * write data to the data block.
  3431. *
  3432. * RETURN:
  3433. * 0 = success
  3434. * !0 = failure
  3435. *
  3436. * PARAMETERS:
  3437. * *cd - pointer to core data
  3438. * row_number - row in config block to write to
  3439. * write_length - length of data to write
  3440. * ebid - enumerated block ID
  3441. * *write_buf - pointer to buffer to write
  3442. * *security_key - pointer to security key to allow write
  3443. * *actual_write_len - pointer to store data length actually written
  3444. ******************************************************************************/
  3445. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3446. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3447. u8 *security_key, u16 *actual_write_len)
  3448. {
  3449. /* row_number + write_len + ebid + security_key + crc */
  3450. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3451. u8 *full_write_buf;
  3452. u8 cmd_offset = 0;
  3453. u16 crc;
  3454. int status;
  3455. int rc = 0;
  3456. int read_ebid;
  3457. u8 *data;
  3458. struct pt_hid_output hid_output = {
  3459. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3460. .write_length = full_write_length,
  3461. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3462. };
  3463. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3464. if (!full_write_buf)
  3465. return -ENOMEM;
  3466. hid_output.write_buf = full_write_buf;
  3467. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3468. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3469. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3470. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3471. full_write_buf[cmd_offset++] = ebid;
  3472. data = &full_write_buf[cmd_offset];
  3473. memcpy(data, write_buf, write_length);
  3474. cmd_offset += write_length;
  3475. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3476. cmd_offset += 8;
  3477. crc = _pt_compute_crc(data, write_length);
  3478. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3479. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3480. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3481. if (rc)
  3482. goto exit;
  3483. status = cd->response_buf[5];
  3484. if (status) {
  3485. rc = -EINVAL;
  3486. goto exit;
  3487. }
  3488. read_ebid = cd->response_buf[6];
  3489. if (read_ebid != ebid) {
  3490. rc = -EPROTO;
  3491. goto exit;
  3492. }
  3493. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3494. exit:
  3495. kfree(full_write_buf);
  3496. return rc;
  3497. }
  3498. /*******************************************************************************
  3499. * FUNCTION: _pt_request_pip_write_data_block
  3500. *
  3501. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3502. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3503. *
  3504. * RETURN:
  3505. * 0 = success
  3506. * !0 = failure
  3507. *
  3508. * PARAMETERS:
  3509. * *dev - pointer to device structure
  3510. * protect - flag to call protected or non-protected
  3511. * row_number - row in config block to write to
  3512. * write_length - length of data to write
  3513. * ebid - enumerated block ID
  3514. * *write_buf - pointer to buffer to write
  3515. * *security_key - pointer to security key to allow write
  3516. * *actual_write_len - pointer to store data length actually written
  3517. ******************************************************************************/
  3518. static int _pt_request_pip_write_data_block(struct device *dev,
  3519. u16 row_number, u16 write_length, u8 ebid,
  3520. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3521. {
  3522. struct pt_core_data *cd = dev_get_drvdata(dev);
  3523. return pt_pip1_write_data_block_(cd, row_number,
  3524. write_length, ebid, write_buf, security_key,
  3525. actual_write_len);
  3526. }
  3527. /*******************************************************************************
  3528. * FUNCTION: pt_pip_get_data_structure_
  3529. *
  3530. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3531. * returning a structure of data defined by data_id
  3532. *
  3533. * RETURN:
  3534. * 0 = success
  3535. * !0 = failure
  3536. *
  3537. * PARAMETERS:
  3538. * *cd - pointer to core data
  3539. * read_offset - read pointer offset
  3540. * read_length - length of data to read
  3541. * data_id - data ID to read
  3542. * *status - pointer to store the read response status
  3543. * *data_format - pointer to store format of data read
  3544. * *actual_read_len - pointer to store data length actually read
  3545. * *data - pointer to store data read
  3546. ******************************************************************************/
  3547. static int pt_pip_get_data_structure_(
  3548. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3549. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3550. u8 *data)
  3551. {
  3552. int rc = 0;
  3553. u16 total_read_len = 0;
  3554. u16 read_len;
  3555. u16 off_buf = 0;
  3556. u8 write_buf[5];
  3557. u8 read_data_id;
  3558. struct pt_hid_output hid_output = {
  3559. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3560. .write_length = 5,
  3561. .write_buf = write_buf,
  3562. };
  3563. again:
  3564. write_buf[0] = LOW_BYTE(read_offset);
  3565. write_buf[1] = HI_BYTE(read_offset);
  3566. write_buf[2] = LOW_BYTE(read_length);
  3567. write_buf[3] = HI_BYTE(read_length);
  3568. write_buf[4] = data_id;
  3569. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3570. if (rc)
  3571. return rc;
  3572. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3573. goto set_status;
  3574. read_data_id = cd->response_buf[6];
  3575. if (read_data_id != data_id)
  3576. return -EPROTO;
  3577. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3578. if (read_len && data) {
  3579. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3580. total_read_len += read_len;
  3581. if (read_len < read_length) {
  3582. read_offset += read_len;
  3583. off_buf += read_len;
  3584. read_length -= read_len;
  3585. goto again;
  3586. }
  3587. }
  3588. if (data_format)
  3589. *data_format = cd->response_buf[9];
  3590. if (actual_read_len)
  3591. *actual_read_len = total_read_len;
  3592. set_status:
  3593. if (status)
  3594. *status = cd->response_buf[5];
  3595. return rc;
  3596. }
  3597. /*******************************************************************************
  3598. * FUNCTION: pt_pip_get_data_structure
  3599. *
  3600. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3601. * an exclusive access to the DUT.
  3602. *
  3603. * RETURN:
  3604. * 0 = success
  3605. * !0 = failure
  3606. *
  3607. * PARAMETERS:
  3608. * *cd - pointer to core data
  3609. * read_offset - read pointer offset
  3610. * read_length - length of data to read
  3611. * data_id - data ID to read
  3612. * *status - pointer to store the read response status
  3613. * *data_format - pointer to store format of data read
  3614. * *actual_read_len - pointer to store data length actually read
  3615. * *data - pointer to store data read
  3616. ******************************************************************************/
  3617. static int pt_pip_get_data_structure(
  3618. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3619. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3620. u8 *data)
  3621. {
  3622. int rc;
  3623. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3624. if (rc < 0) {
  3625. pt_debug(cd->dev, DL_ERROR,
  3626. "%s: fail get exclusive ex=%p own=%p\n",
  3627. __func__, cd->exclusive_dev, cd->dev);
  3628. return rc;
  3629. }
  3630. rc = pt_pip_get_data_structure_(cd, read_offset,
  3631. read_length, data_id, status, data_format,
  3632. actual_read_len, data);
  3633. if (release_exclusive(cd, cd->dev) < 0)
  3634. pt_debug(cd->dev, DL_ERROR,
  3635. "%s: fail to release exclusive\n", __func__);
  3636. return rc;
  3637. }
  3638. /*******************************************************************************
  3639. * FUNCTION: _pt_request_pip_get_data_structure
  3640. *
  3641. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3642. * calls to the protected or unprotected call to
  3643. * pt_pip_get_data_structure
  3644. *
  3645. * RETURN:
  3646. * 0 = success
  3647. * !0 = failure
  3648. *
  3649. * PARAMETERS:
  3650. * *dev - pointer to device structure
  3651. * protect - flag to call protected or non-protected
  3652. * read_offset - read pointer offset
  3653. * read_length - length of data to read
  3654. * data_id - data ID to read
  3655. * *status - pointer to store the read response status
  3656. * *data_format - pointer to store format of data read
  3657. * *actual_read_len - pointer to store data length actually read
  3658. * *data - pointer to store data read
  3659. ******************************************************************************/
  3660. static int _pt_request_pip_get_data_structure(struct device *dev,
  3661. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3662. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3663. {
  3664. struct pt_core_data *cd = dev_get_drvdata(dev);
  3665. if (protect)
  3666. return pt_pip_get_data_structure(cd,
  3667. read_offset, read_length, data_id, status,
  3668. data_format, actual_read_len, data);
  3669. return pt_pip_get_data_structure_(cd,
  3670. read_offset, read_length, data_id, status,
  3671. data_format, actual_read_len, data);
  3672. }
  3673. /*******************************************************************************
  3674. * FUNCTION: _pt_manage_local_cal_data
  3675. *
  3676. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3677. * CALIBRATION data. It stores it in a local static array and can be
  3678. * cleared, loaded or used to restore the CAL data back to the running FW.
  3679. * The CAL data is read or restored by use of the PIP1 commands:
  3680. * - READ_DATA_BLOCK (0x22)
  3681. * - WRITE_DATA_BLOCK (0x23)
  3682. *
  3683. * RETURN:
  3684. * 0 = success
  3685. * !0 = failure
  3686. *
  3687. * PARAMETERS:
  3688. * *dev - pointer to device structure
  3689. * action - One of the following actions:
  3690. * - PT_CAL_DATA_SAVE
  3691. * - PT_CAL_DATA_RESTORE
  3692. * - PT_CAL_DATA_CLEAR
  3693. * - PT_CAL_DATA_SIZE
  3694. * *size - pointer to the number of bytes transferred
  3695. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3696. ******************************************************************************/
  3697. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3698. unsigned short *crc)
  3699. {
  3700. struct pt_core_data *cd = dev_get_drvdata(dev);
  3701. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3702. unsigned short calc_id_crc = 0;
  3703. static u8 *cal_cache_data;
  3704. static u16 cal_cache_len;
  3705. static unsigned short cal_cache_chip_id;
  3706. int rc = 0;
  3707. u8 *tmp_data = NULL;
  3708. u8 row_number = 0;
  3709. u8 prefix[20];
  3710. u16 cal_size = 0;
  3711. u16 transfer_size;
  3712. u16 act_trans_len = 0;
  3713. u16 byte_offset = 0;
  3714. u16 cal_blk_size;
  3715. u16 total_rows;
  3716. u16 remain_bytes;
  3717. u16 data_block_crc;
  3718. u16 buf_size = 12;
  3719. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3720. __func__, action);
  3721. switch (action) {
  3722. case PT_CAL_DATA_SAVE:
  3723. /* Read the size of the CAL block and calculate # rows */
  3724. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3725. if (!tmp_data) {
  3726. rc = -ENOMEM;
  3727. goto exit;
  3728. }
  3729. /*
  3730. * Don't check rc as doing a read size will give a false
  3731. * error on the CRC check.
  3732. */
  3733. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3734. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3735. cal_blk_size = act_trans_len;
  3736. kfree(tmp_data);
  3737. pt_debug(dev, DL_INFO,
  3738. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3739. __func__, cal_cache_len, cal_blk_size);
  3740. /* Safety net to ensure we didn't read incorrect size */
  3741. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3742. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3743. __func__);
  3744. rc = 1;
  3745. goto exit;
  3746. }
  3747. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3748. if (cal_blk_size != cal_cache_len) {
  3749. kfree(cal_cache_data);
  3750. cal_cache_data = kzalloc(cal_blk_size + 2,
  3751. GFP_KERNEL);
  3752. if (!cal_cache_data) {
  3753. rc = -ENOMEM;
  3754. goto exit;
  3755. }
  3756. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3757. __func__);
  3758. }
  3759. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3760. /* Calculate how many rows [0-n] (PIP Transactions) */
  3761. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3762. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3763. /* Add row if we have a last partial row */
  3764. if (remain_bytes > 0)
  3765. total_rows++;
  3766. pt_debug(dev, DL_INFO,
  3767. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3768. __func__, cal_blk_size, total_rows, remain_bytes);
  3769. /* Read all rows unless an error occurs */
  3770. rc = 0;
  3771. while (rc == 0 && row_number <= total_rows) {
  3772. act_trans_len = 0;
  3773. if (remain_bytes > 0 && row_number == total_rows)
  3774. transfer_size = remain_bytes;
  3775. else
  3776. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3777. rc = pt_pip1_read_data_block_(cd, row_number,
  3778. transfer_size, PT_CAL_EBID,
  3779. &act_trans_len,
  3780. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3781. &data_block_crc);
  3782. if (rc) {
  3783. /* Error occurred, exit loop */
  3784. cal_size = 0;
  3785. break;
  3786. }
  3787. pt_debug(dev, DL_INFO,
  3788. "%s: CAL read rc=%d actual read len=%d\n",
  3789. __func__, rc, act_trans_len);
  3790. byte_offset += act_trans_len;
  3791. cal_size = byte_offset;
  3792. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3793. pt_pr_buf(dev, DL_INFO,
  3794. &cal_cache_data[byte_offset - act_trans_len],
  3795. act_trans_len, prefix);
  3796. row_number++;
  3797. }
  3798. if (cal_size > 0) {
  3799. /* Save a CRC of the chip info the CAL was saved from */
  3800. calc_id_crc = crc_ccitt_calculate(
  3801. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3802. cal_cache_chip_id = calc_id_crc;
  3803. cal_cache_len = cal_size;
  3804. pt_debug(dev, DL_INFO,
  3805. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3806. __func__, calc_id_crc, cal_size);
  3807. }
  3808. *size = cal_size;
  3809. *crc = calc_id_crc;
  3810. break;
  3811. case PT_CAL_DATA_RESTORE:
  3812. cal_size = cal_cache_len;
  3813. while ((rc == 0) && (byte_offset < cal_size)) {
  3814. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3815. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3816. else
  3817. transfer_size = cal_size - byte_offset;
  3818. rc = pt_pip1_write_data_block_(cd, row_number,
  3819. transfer_size, PT_CAL_EBID,
  3820. &cal_cache_data[byte_offset],
  3821. (u8 *)pt_data_block_security_key,
  3822. &act_trans_len);
  3823. byte_offset += act_trans_len;
  3824. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3825. __func__, byte_offset);
  3826. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3827. pt_pr_buf(dev, DL_INFO,
  3828. &cal_cache_data[byte_offset - act_trans_len],
  3829. act_trans_len, prefix);
  3830. if ((byte_offset > cal_size) ||
  3831. (act_trans_len != transfer_size))
  3832. rc = -EIO;
  3833. row_number++;
  3834. }
  3835. *size = byte_offset;
  3836. *crc = cal_cache_chip_id;
  3837. break;
  3838. case PT_CAL_DATA_CLEAR:
  3839. if (cal_cache_data)
  3840. memset(&cal_cache_data[0], 0, cal_cache_len);
  3841. cal_cache_len = 0;
  3842. cal_cache_chip_id = 0;
  3843. *size = 0;
  3844. *crc = 0;
  3845. break;
  3846. case PT_CAL_DATA_INFO:
  3847. default:
  3848. *size = cal_cache_len;
  3849. *crc = cal_cache_chip_id;
  3850. pt_debug(dev, DL_INFO,
  3851. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3852. __func__, cal_cache_chip_id,
  3853. cal_cache_len);
  3854. break;
  3855. }
  3856. exit:
  3857. pt_debug(dev, DL_INFO,
  3858. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3859. __func__, rc, *crc, *size);
  3860. return rc;
  3861. }
  3862. /*******************************************************************************
  3863. * FUNCTION: pt_pip_run_selftest_
  3864. *
  3865. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3866. * to execute a FW built in self test
  3867. *
  3868. * RETURN:
  3869. * 0 = success
  3870. * !0 = failure
  3871. *
  3872. * PARAMETERS:
  3873. * *cd - pointer to core data
  3874. * test_id - enumerated test ID to run
  3875. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3876. * *status - pointer to store the read response status
  3877. * *summary_results - pointer to store the results summary
  3878. * *results_available - pointer to store if results are available
  3879. *****************************************************************************/
  3880. static int pt_pip_run_selftest_(
  3881. struct pt_core_data *cd, u8 test_id,
  3882. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3883. u8 *results_available)
  3884. {
  3885. int rc = 0;
  3886. u8 write_buf[2];
  3887. struct pt_hid_output hid_output = {
  3888. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3889. .write_length = 2,
  3890. .write_buf = write_buf,
  3891. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3892. };
  3893. write_buf[0] = test_id;
  3894. write_buf[1] = write_idacs_to_flash;
  3895. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3896. hid_output.write_length = 1;
  3897. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3898. if (rc)
  3899. return rc;
  3900. if (status)
  3901. *status = cd->response_buf[5];
  3902. if (summary_result)
  3903. *summary_result = cd->response_buf[6];
  3904. /* results_available only available before PIP 1.03 */
  3905. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3906. if (results_available)
  3907. *results_available = cd->response_buf[7];
  3908. }
  3909. return rc;
  3910. }
  3911. /*******************************************************************************
  3912. * FUNCTION: pt_pip_run_selftest
  3913. *
  3914. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3915. * an exclusive access to the DUT.
  3916. *
  3917. * RETURN:
  3918. * 0 = success
  3919. * !0 = failure
  3920. *
  3921. * PARAMETERS:
  3922. * *cd - pointer to core data
  3923. * test_id - enumerated test ID to run
  3924. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3925. * *status - pointer to store the read response status
  3926. * *summary_results - pointer to store the results summary
  3927. * *results_available - pointer to store if results are available
  3928. ******************************************************************************/
  3929. static int pt_pip_run_selftest(
  3930. struct pt_core_data *cd, u8 test_id,
  3931. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3932. u8 *results_available)
  3933. {
  3934. int rc;
  3935. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3936. if (rc < 0) {
  3937. pt_debug(cd->dev, DL_ERROR,
  3938. "%s: fail get exclusive ex=%p own=%p\n",
  3939. __func__, cd->exclusive_dev, cd->dev);
  3940. return rc;
  3941. }
  3942. rc = pt_pip_run_selftest_(cd, test_id,
  3943. write_idacs_to_flash, status, summary_result,
  3944. results_available);
  3945. if (release_exclusive(cd, cd->dev) < 0)
  3946. pt_debug(cd->dev, DL_ERROR,
  3947. "%s: fail to release exclusive\n", __func__);
  3948. return rc;
  3949. }
  3950. /*******************************************************************************
  3951. * FUNCTION: _pt_request_pip_run_selftest
  3952. *
  3953. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3954. * calls to the protected or unprotected call to pt_pip_run_selftest
  3955. *
  3956. * RETURN:
  3957. * 0 = success
  3958. * !0 = failure
  3959. *
  3960. * PARAMETERS:
  3961. * *dev - pointer to device structure
  3962. * protect - flag to call protected or non-protected
  3963. * test_id - enumerated test ID to run
  3964. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3965. * *status - pointer to store the read response status
  3966. * *summary_results - pointer to store the results summary
  3967. * *results_available - pointer to store if results are available
  3968. ******************************************************************************/
  3969. static int _pt_request_pip_run_selftest(struct device *dev,
  3970. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3971. u8 *summary_result, u8 *results_available)
  3972. {
  3973. struct pt_core_data *cd = dev_get_drvdata(dev);
  3974. if (protect)
  3975. return pt_pip_run_selftest(cd, test_id,
  3976. write_idacs_to_flash, status, summary_result,
  3977. results_available);
  3978. return pt_pip_run_selftest_(cd, test_id,
  3979. write_idacs_to_flash, status, summary_result,
  3980. results_available);
  3981. }
  3982. /*******************************************************************************
  3983. * FUNCTION: _pt_pip_get_selftest_result_
  3984. *
  3985. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3986. * to retrieve the self test results from the self test already executed
  3987. *
  3988. * RETURN:
  3989. * 0 = success
  3990. * !0 = failure
  3991. *
  3992. * PARAMETERS:
  3993. * *cd - pointer to core data
  3994. * read_offset - read pointer offset
  3995. * read_length - length of data to read
  3996. * test_id - enumerated test ID to read selftest results from
  3997. * *status - pointer to store the read response status
  3998. * *actual_read_len - pointer to store data length actually read
  3999. * *status - pointer to where the cmd response statas is stored
  4000. ******************************************************************************/
  4001. static int pt_pip_get_selftest_result_(
  4002. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4003. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4004. {
  4005. int rc = 0;
  4006. u16 total_read_len = 0;
  4007. u16 read_len;
  4008. u16 off_buf = 0;
  4009. u8 write_buf[5];
  4010. u8 read_test_id;
  4011. bool repeat;
  4012. struct pt_hid_output hid_output = {
  4013. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4014. .write_length = 5,
  4015. .write_buf = write_buf,
  4016. };
  4017. /*
  4018. * Do not repeat reading for Auto Shorts test
  4019. * when PIP version < 1.3
  4020. */
  4021. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4022. || test_id != PT_ST_ID_AUTOSHORTS;
  4023. again:
  4024. write_buf[0] = LOW_BYTE(read_offset);
  4025. write_buf[1] = HI_BYTE(read_offset);
  4026. write_buf[2] = LOW_BYTE(read_length);
  4027. write_buf[3] = HI_BYTE(read_length);
  4028. write_buf[4] = test_id;
  4029. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4030. if (rc)
  4031. return rc;
  4032. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4033. goto set_status;
  4034. read_test_id = cd->response_buf[6];
  4035. if (read_test_id != test_id)
  4036. return -EPROTO;
  4037. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4038. if (read_len && data) {
  4039. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4040. total_read_len += read_len;
  4041. if (repeat && read_len < read_length) {
  4042. read_offset += read_len;
  4043. off_buf += read_len;
  4044. read_length -= read_len;
  4045. goto again;
  4046. }
  4047. }
  4048. if (actual_read_len)
  4049. *actual_read_len = total_read_len;
  4050. set_status:
  4051. if (status)
  4052. *status = cd->response_buf[5];
  4053. return rc;
  4054. }
  4055. /*******************************************************************************
  4056. * FUNCTION: _pt_pip_get_selftest_result
  4057. *
  4058. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4059. * access to the DUT
  4060. *
  4061. * RETURN:
  4062. * 0 = success
  4063. * !0 = failure
  4064. *
  4065. * PARAMETERS:
  4066. * *cd - pointer to core data
  4067. * read_offset - read pointer offset
  4068. * read_length - length of data to read
  4069. * test_id - enumerated test ID to read selftest results from
  4070. * *status - pointer to store the read response status
  4071. * *actual_read_len - pointer to store data length actually read
  4072. * *status - pointer to where the cmd response statas is stored
  4073. ******************************************************************************/
  4074. static int pt_pip_get_selftest_result(
  4075. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4076. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4077. {
  4078. int rc;
  4079. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4080. if (rc < 0) {
  4081. pt_debug(cd->dev, DL_ERROR,
  4082. "%s: fail get exclusive ex=%p own=%p\n",
  4083. __func__, cd->exclusive_dev, cd->dev);
  4084. return rc;
  4085. }
  4086. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4087. read_length, test_id, status, actual_read_len, data);
  4088. if (release_exclusive(cd, cd->dev) < 0)
  4089. pt_debug(cd->dev, DL_ERROR,
  4090. "%s: fail to release exclusive\n", __func__);
  4091. return rc;
  4092. }
  4093. /*******************************************************************************
  4094. * FUNCTION: _pt_request_pip_get_selftest_result
  4095. *
  4096. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4097. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4098. *
  4099. * RETURN:
  4100. * 0 = success
  4101. * !0 = failure
  4102. *
  4103. * PARAMETERS:
  4104. * *dev - pointer to device structure
  4105. * protect - flag to call protected or non-protected
  4106. * read_offset - read pointer offset
  4107. * read_length - length of data to read
  4108. * test_id - enumerated test ID to read selftest results from
  4109. * *status - pointer to store the read response status
  4110. * *actual_read_len - pointer to store data length actually read
  4111. * *data - pointer to where the data read is stored
  4112. ******************************************************************************/
  4113. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4114. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4115. u8 *status, u16 *actual_read_len, u8 *data)
  4116. {
  4117. struct pt_core_data *cd = dev_get_drvdata(dev);
  4118. if (protect)
  4119. return pt_pip_get_selftest_result(cd, read_offset,
  4120. read_length, test_id, status, actual_read_len,
  4121. data);
  4122. return pt_pip_get_selftest_result_(cd, read_offset,
  4123. read_length, test_id, status, actual_read_len,
  4124. data);
  4125. }
  4126. /*******************************************************************************
  4127. * FUNCTION: _pt_pip_load_self_test_param
  4128. *
  4129. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4130. * to load parameters needed by a self test
  4131. *
  4132. * RETURN:
  4133. * 0 = success
  4134. * !0 = failure
  4135. *
  4136. * PARAMETERS:
  4137. * *cd - pointer to core data
  4138. * self_test_id - enumerated test ID for which the parmeters belong
  4139. * load_offset - mem offset to where to load parameters
  4140. * load_length - length of parameter data to load
  4141. * *parameters - pointer to list of parameter data
  4142. * *status - pointer to store the response status
  4143. * *ret_test_id - pointer to returned test id the parameters were stored
  4144. * *act_load_len - pointer to store the actual load length that was writen
  4145. ******************************************************************************/
  4146. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4147. u8 self_test_id, u16 load_offset, u16 load_length,
  4148. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4149. {
  4150. int rc = 0;
  4151. int i;
  4152. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4153. struct pt_hid_output hid_output = {
  4154. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4155. .write_length = 5 + load_length,
  4156. .write_buf = write_buf,
  4157. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4158. };
  4159. write_buf[0] = LOW_BYTE(load_offset);
  4160. write_buf[1] = HI_BYTE(load_offset);
  4161. write_buf[2] = LOW_BYTE(load_length);
  4162. write_buf[3] = HI_BYTE(load_length);
  4163. write_buf[4] = self_test_id;
  4164. for (i = 0; i < load_length; i++)
  4165. write_buf[i + 5] = parameters[i];
  4166. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4167. if (rc)
  4168. return rc;
  4169. if (status)
  4170. *status = cd->response_buf[5];
  4171. if (ret_test_id)
  4172. *ret_test_id = cd->response_buf[6];
  4173. if (act_load_len)
  4174. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4175. return rc;
  4176. }
  4177. /*******************************************************************************
  4178. * FUNCTION: pt_pip_load_self_test_param
  4179. *
  4180. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4181. * access to the DUT.
  4182. *
  4183. * RETURN:
  4184. * 0 = success
  4185. * !0 = failure
  4186. *
  4187. * PARAMETERS:
  4188. * *cd - pointer to core data
  4189. * self_test_id - enumerated test ID for which the parmeters belong
  4190. * load_offset - mem offset to where to load parameters
  4191. * load_length - length of parameter data to load
  4192. * *parameters - pointer to list of parameter data
  4193. * *status - pointer to store the response status
  4194. * *ret_test_id - pointer to returned test id the parameters were stored
  4195. * *act_load_len - pointer to store the actual load length that was writen
  4196. ******************************************************************************/
  4197. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4198. u8 self_test_id, u16 load_offset, u16 load_length,
  4199. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4200. {
  4201. int rc;
  4202. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4203. if (rc < 0) {
  4204. pt_debug(cd->dev, DL_ERROR,
  4205. "%s: fail get exclusive ex=%p own=%p\n",
  4206. __func__, cd->exclusive_dev, cd->dev);
  4207. return rc;
  4208. }
  4209. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4210. load_length, parameters, status, ret_test_id, act_load_len);
  4211. if (release_exclusive(cd, cd->dev) < 0)
  4212. pt_debug(cd->dev, DL_ERROR,
  4213. "%s: fail to release exclusive\n", __func__);
  4214. return rc;
  4215. }
  4216. /*******************************************************************************
  4217. * FUNCTION: _pt_request_pip_load_self_test_param
  4218. *
  4219. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4220. * calls to the protected or unprotected call to
  4221. * pt_pip_load_self_test_param
  4222. *
  4223. * RETURN:
  4224. * 0 = success
  4225. * !0 = failure
  4226. *
  4227. * PARAMETERS:
  4228. * *dev - pointer to device structure
  4229. * protect - flag to call protected or non-protected
  4230. * self_test_id - enumerated test ID for which the parmeters belong
  4231. * load_offset - mem offset to where to load parameters
  4232. * load_length - length of parameter data to load
  4233. * *parameters - pointer to list of parameter data
  4234. * *status - pointer to store the response status
  4235. * *ret_test_id - pointer to returned test id the parameters were stored
  4236. * *act_load_len - pointer to store the actual load length that was writen
  4237. ******************************************************************************/
  4238. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4239. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4240. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4241. {
  4242. struct pt_core_data *cd = dev_get_drvdata(dev);
  4243. if (protect)
  4244. return pt_pip_load_self_test_param(cd, self_test_id,
  4245. load_offset, load_length, parameters, status, ret_test_id,
  4246. act_load_len);
  4247. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4248. load_length, parameters, status, ret_test_id, act_load_len);
  4249. }
  4250. /*******************************************************************************
  4251. * FUNCTION: pt_pip_calibrate_ext_
  4252. *
  4253. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4254. * for the response
  4255. *
  4256. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4257. *
  4258. * RETURN:
  4259. * 0 = success
  4260. * !0 = failure
  4261. *
  4262. * PARAMETERS:
  4263. * *cd - pointer to core data
  4264. * *cal_data - pointer to extended calibration data structure
  4265. * *status - pointer to where the command response status is stored
  4266. ******************************************************************************/
  4267. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4268. struct pt_cal_ext_data *cal_data, u8 *status)
  4269. {
  4270. int rc = 0;
  4271. int write_length = 4;
  4272. u8 write_buf[4];
  4273. u16 size = 0;
  4274. unsigned short crc = 0;
  4275. struct pt_hid_output hid_output = {
  4276. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4277. .write_length = write_length,
  4278. .write_buf = write_buf,
  4279. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4280. };
  4281. if (cal_data == NULL)
  4282. return -EINVAL;
  4283. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4284. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4285. if (rc)
  4286. return rc;
  4287. if (status)
  4288. *status = cd->response_buf[5];
  4289. /*
  4290. * When doing a calibration on a flashless DUT, save CAL data in
  4291. * the TTDL cache on any successful calibration
  4292. */
  4293. if (*status == 0 && cd->cal_cache_in_host) {
  4294. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4295. __func__);
  4296. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4297. &size, &crc);
  4298. if (rc)
  4299. pt_debug(cd->dev, DL_ERROR,
  4300. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4301. else
  4302. pt_debug(cd->dev, DL_INFO,
  4303. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4304. __func__, crc, size);
  4305. }
  4306. return 0;
  4307. }
  4308. /*******************************************************************************
  4309. * FUNCTION: pt_pip_calibrate_ext
  4310. *
  4311. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4312. * DUT
  4313. *
  4314. * RETURN:
  4315. * 0 = success
  4316. * !0 = failure
  4317. *
  4318. * PARAMETERS:
  4319. * *cd - pointer to core data
  4320. * *cal_data - pointer to extended calibration data structure
  4321. * *status - pointer to where the command response status is stored
  4322. ******************************************************************************/
  4323. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4324. struct pt_cal_ext_data *cal_data, u8 *status)
  4325. {
  4326. int rc;
  4327. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4328. if (rc < 0) {
  4329. pt_debug(cd->dev, DL_ERROR,
  4330. "%s: fail get exclusive ex=%p own=%p\n",
  4331. __func__, cd->exclusive_dev, cd->dev);
  4332. return rc;
  4333. }
  4334. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4335. if (release_exclusive(cd, cd->dev) < 0)
  4336. pt_debug(cd->dev, DL_ERROR,
  4337. "%s: fail to release exclusive\n", __func__);
  4338. return rc;
  4339. }
  4340. /*******************************************************************************
  4341. * FUNCTION: _pt_request_pip_calibrate_ext
  4342. *
  4343. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4344. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4345. *
  4346. * RETURN:
  4347. * 0 = success
  4348. * !0 = failure
  4349. *
  4350. * PARAMETERS:
  4351. * *dev - pointer to device structure
  4352. * protect - flag to call protected or non-protected
  4353. * *cal_data - pointer to extended calibration data structure
  4354. * *status - pointer to where the command response status is stored
  4355. ******************************************************************************/
  4356. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4357. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4358. {
  4359. struct pt_core_data *cd = dev_get_drvdata(dev);
  4360. if (protect)
  4361. return pt_pip_calibrate_ext(cd, cal_data, status);
  4362. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4363. }
  4364. /*******************************************************************************
  4365. * FUNCTION: pt_pip_calibrate_idacs_
  4366. *
  4367. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4368. * for the response
  4369. *
  4370. * RETURN:
  4371. * 0 = success
  4372. * !0 = failure
  4373. *
  4374. * PARAMETERS:
  4375. * *cd - pointer to core data
  4376. * mode - sense mode to calibrate (0-5)
  4377. * *status - pointer to where the command response status is stored
  4378. ******************************************************************************/
  4379. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4380. u8 mode, u8 *status)
  4381. {
  4382. int rc = 0;
  4383. int write_length = 1;
  4384. u8 write_buf[1];
  4385. u8 cmd_offset = 0;
  4386. struct pt_hid_output hid_output = {
  4387. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4388. .write_length = write_length,
  4389. .write_buf = write_buf,
  4390. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4391. };
  4392. write_buf[cmd_offset++] = mode;
  4393. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4394. if (rc)
  4395. return rc;
  4396. *status = cd->response_buf[5];
  4397. if (*status)
  4398. return -EINVAL;
  4399. return 0;
  4400. }
  4401. /*******************************************************************************
  4402. * FUNCTION: pt_pip_calibrate_idacs
  4403. *
  4404. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4405. * access to the DUT
  4406. *
  4407. * RETURN:
  4408. * 0 = success
  4409. * !0 = failure
  4410. *
  4411. * PARAMETERS:
  4412. * *cd - pointer to core data
  4413. * mode - sense mode to calibrate (0-5)
  4414. * *status - pointer to where the command response status is stored
  4415. ******************************************************************************/
  4416. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4417. u8 mode, u8 *status)
  4418. {
  4419. int rc;
  4420. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4421. if (rc < 0) {
  4422. pt_debug(cd->dev, DL_ERROR,
  4423. "%s: fail get exclusive ex=%p own=%p\n",
  4424. __func__, cd->exclusive_dev, cd->dev);
  4425. return rc;
  4426. }
  4427. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4428. if (release_exclusive(cd, cd->dev) < 0)
  4429. pt_debug(cd->dev, DL_ERROR,
  4430. "%s: fail to release exclusive\n", __func__);
  4431. return rc;
  4432. }
  4433. /*******************************************************************************
  4434. * FUNCTION: _pt_request_pip_calibrate_idacs
  4435. *
  4436. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4437. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4438. *
  4439. * RETURN:
  4440. * 0 = success
  4441. * !0 = failure
  4442. *
  4443. * PARAMETERS:
  4444. * *dev - pointer to device structure
  4445. * protect - flag to call protected or non-protected
  4446. * mode - sense mode to calibrate (0-5)
  4447. * *status - pointer to where the command response status is stored
  4448. ******************************************************************************/
  4449. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4450. int protect, u8 mode, u8 *status)
  4451. {
  4452. struct pt_core_data *cd = dev_get_drvdata(dev);
  4453. if (protect)
  4454. return pt_pip_calibrate_idacs(cd, mode, status);
  4455. return pt_pip_calibrate_idacs_(cd, mode, status);
  4456. }
  4457. /*******************************************************************************
  4458. * FUNCTION: pt_hid_output_initialize_baselines_
  4459. *
  4460. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4461. * waiting for the response.
  4462. *
  4463. * RETURN:
  4464. * 0 = success
  4465. * !0 = failure
  4466. *
  4467. * PARAMETERS:
  4468. * *cd - pointer to core data
  4469. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4470. * each or together with a single command.
  4471. * *status - pointer to where the command response status is stored
  4472. ******************************************************************************/
  4473. static int pt_hid_output_initialize_baselines_(
  4474. struct pt_core_data *cd, u8 test_id, u8 *status)
  4475. {
  4476. int rc = 0;
  4477. int write_length = 1;
  4478. u8 write_buf[1];
  4479. u8 cmd_offset = 0;
  4480. struct pt_hid_output hid_output = {
  4481. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4482. .write_length = write_length,
  4483. .write_buf = write_buf,
  4484. };
  4485. write_buf[cmd_offset++] = test_id;
  4486. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4487. if (rc)
  4488. return rc;
  4489. *status = cd->response_buf[5];
  4490. if (*status)
  4491. return -EINVAL;
  4492. return rc;
  4493. }
  4494. /*******************************************************************************
  4495. * FUNCTION: pt_hid_output_initialize_baselines
  4496. *
  4497. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4498. * access to the DUT
  4499. *
  4500. * RETURN:
  4501. * 0 = success
  4502. * !0 = failure
  4503. *
  4504. * PARAMETERS:
  4505. * *cd - pointer to core data
  4506. * test_id - enumerated ID against which to initialize the baseline
  4507. * *status - pointer to where the command response status is stored
  4508. ******************************************************************************/
  4509. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4510. u8 test_id, u8 *status)
  4511. {
  4512. int rc;
  4513. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4514. if (rc < 0) {
  4515. pt_debug(cd->dev, DL_ERROR,
  4516. "%s: fail get exclusive ex=%p own=%p\n",
  4517. __func__, cd->exclusive_dev, cd->dev);
  4518. return rc;
  4519. }
  4520. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4521. if (release_exclusive(cd, cd->dev) < 0)
  4522. pt_debug(cd->dev, DL_ERROR,
  4523. "%s: fail to release exclusive\n", __func__);
  4524. return rc;
  4525. }
  4526. /*******************************************************************************
  4527. * FUNCTION: _pt_request_pip_initialize_baselines
  4528. *
  4529. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4530. * calls to the protected or unprotected call to
  4531. * pt_pip_initialize_baselines
  4532. *
  4533. * RETURN:
  4534. * 0 = success
  4535. * !0 = failure
  4536. *
  4537. * PARAMETERS:
  4538. * *dev - pointer to device structure
  4539. * protect - flag to call protected or non-protected
  4540. * test_id - enumerated ID against which to initialize the baseline
  4541. * *status - pointer to where the command response status is stored
  4542. ******************************************************************************/
  4543. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4544. int protect, u8 test_id, u8 *status)
  4545. {
  4546. struct pt_core_data *cd = dev_get_drvdata(dev);
  4547. if (protect)
  4548. return pt_hid_output_initialize_baselines(cd, test_id,
  4549. status);
  4550. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4551. }
  4552. /*******************************************************************************
  4553. * FUNCTION: pt_hid_output_exec_panel_scan_
  4554. *
  4555. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4556. * the response
  4557. *
  4558. * RETURN:
  4559. * 0 = success
  4560. * !0 = failure
  4561. *
  4562. * PARAMETERS:
  4563. * *cd - pointer to core data
  4564. ******************************************************************************/
  4565. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4566. {
  4567. struct pt_hid_output hid_output = {
  4568. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4569. };
  4570. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4571. }
  4572. /*******************************************************************************
  4573. * FUNCTION: pt_hid_output_exec_panel_scan
  4574. *
  4575. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4576. * access to the DUT.
  4577. *
  4578. * RETURN:
  4579. * 0 = success
  4580. * !0 = failure
  4581. *
  4582. * PARAMETERS:
  4583. * *cd - pointer to core data
  4584. ******************************************************************************/
  4585. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4586. {
  4587. int rc;
  4588. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4589. if (rc < 0) {
  4590. pt_debug(cd->dev, DL_ERROR,
  4591. "%s: fail get exclusive ex=%p own=%p\n",
  4592. __func__, cd->exclusive_dev, cd->dev);
  4593. return rc;
  4594. }
  4595. rc = pt_hid_output_exec_panel_scan_(cd);
  4596. if (release_exclusive(cd, cd->dev) < 0)
  4597. pt_debug(cd->dev, DL_ERROR,
  4598. "%s: fail to release exclusive\n", __func__);
  4599. return rc;
  4600. }
  4601. /*******************************************************************************
  4602. * FUNCTION: pt_pip2_exec_panel_scan_
  4603. *
  4604. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4605. * the response
  4606. *
  4607. * RETURN:
  4608. * 0 = success
  4609. * !0 = failure
  4610. *
  4611. * PARAMETERS:
  4612. * *cd - pointer to core data
  4613. * scan_type - type of panel scan to perform (PIP2 only)
  4614. ******************************************************************************/
  4615. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4616. {
  4617. int rc = 0;
  4618. u8 data[2];
  4619. u8 read_buf[10];
  4620. u16 actual_read_len;
  4621. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4622. __func__, scan_type);
  4623. data[0] = scan_type;
  4624. rc = _pt_request_pip2_send_cmd(cd->dev,
  4625. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4626. data, 1, read_buf, &actual_read_len);
  4627. if (rc) {
  4628. pt_debug(cd->dev, DL_ERROR,
  4629. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4630. __func__, scan_type, rc);
  4631. }
  4632. return rc;
  4633. }
  4634. /*******************************************************************************
  4635. * FUNCTION: pt_pip2_exec_panel_scan
  4636. *
  4637. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4638. * access to the DUT.
  4639. *
  4640. * RETURN:
  4641. * 0 = success
  4642. * !0 = failure
  4643. *
  4644. * PARAMETERS:
  4645. * *cd - pointer to core data
  4646. * scan_type - type of panel scan to perform (PIP2 only)
  4647. ******************************************************************************/
  4648. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4649. {
  4650. int rc;
  4651. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4652. if (rc < 0) {
  4653. pt_debug(cd->dev, DL_ERROR,
  4654. "%s: fail get exclusive ex=%p own=%p\n",
  4655. __func__, cd->exclusive_dev, cd->dev);
  4656. return rc;
  4657. }
  4658. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4659. if (release_exclusive(cd, cd->dev) < 0)
  4660. pt_debug(cd->dev, DL_ERROR,
  4661. "%s: fail to release exclusive\n", __func__);
  4662. return rc;
  4663. }
  4664. /*******************************************************************************
  4665. * FUNCTION: _pt_request_pip_exec_panel_scan
  4666. *
  4667. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4668. * calls to the protected or unprotected call to
  4669. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4670. *
  4671. * RETURN:
  4672. * 0 = success
  4673. * !0 = failure
  4674. *
  4675. * PARAMETERS:
  4676. * *dev - pointer to device structure
  4677. * protect - flag to call protected or non-protected
  4678. * scan_type - type of panel scan to perform (PIP2 only)
  4679. ******************************************************************************/
  4680. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4681. int protect, u8 scan_type)
  4682. {
  4683. struct pt_core_data *cd = dev_get_drvdata(dev);
  4684. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4685. if (protect)
  4686. return pt_pip2_exec_panel_scan(cd, scan_type);
  4687. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4688. }
  4689. if (protect)
  4690. return pt_hid_output_exec_panel_scan(cd);
  4691. return pt_hid_output_exec_panel_scan_(cd);
  4692. }
  4693. /*******************************************************************************
  4694. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4695. *
  4696. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4697. * to retrieve the specified data type for a the last successful Execute
  4698. * Panel Scan command.
  4699. *
  4700. * RETURN:
  4701. * 0 = success
  4702. * !0 = failure
  4703. *
  4704. * PARAMETERS:
  4705. * *dev - pointer to device structure
  4706. * protect - flag to call protected or non-protected
  4707. * read_offset - read pointer offset
  4708. * read_count - length of data to read
  4709. * data_id - enumerated test ID to read selftest results from
  4710. * *response - pointer to store the read response status
  4711. * *config - pointer to store config data
  4712. * *actual_read_len - pointer to store data length actually read
  4713. * *read_buf - pointer to the read buffer
  4714. ******************************************************************************/
  4715. static int pt_hid_output_retrieve_panel_scan_(
  4716. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4717. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4718. u8 *read_buf)
  4719. {
  4720. int status;
  4721. u8 read_data_id;
  4722. int rc = 0;
  4723. int write_length = 5;
  4724. u8 write_buf[5];
  4725. u8 cmd_offset = 0;
  4726. u8 data_elem_size;
  4727. int size;
  4728. int data_size;
  4729. struct pt_hid_output hid_output = {
  4730. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4731. .write_length = write_length,
  4732. .write_buf = write_buf,
  4733. };
  4734. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4735. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4736. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4737. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4738. write_buf[cmd_offset++] = data_id;
  4739. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4740. if (rc)
  4741. return rc;
  4742. status = cd->response_buf[5];
  4743. if (status)
  4744. return -EINVAL;
  4745. read_data_id = cd->response_buf[6];
  4746. if (read_data_id != data_id)
  4747. return -EPROTO;
  4748. size = get_unaligned_le16(&cd->response_buf[0]);
  4749. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4750. *config = cd->response_buf[9];
  4751. data_elem_size = *config & 0x07;
  4752. data_size = *actual_read_len * data_elem_size;
  4753. if (read_buf)
  4754. memcpy(read_buf, &cd->response_buf[10], data_size);
  4755. if (response)
  4756. memcpy(response, cd->response_buf, size);
  4757. return rc;
  4758. }
  4759. /*******************************************************************************
  4760. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4761. *
  4762. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4763. * access to the DUT.
  4764. *
  4765. * RETURN:
  4766. * 0 = success
  4767. * !0 = failure
  4768. *
  4769. * PARAMETERS:
  4770. * *dev - pointer to device structure
  4771. * protect - flag to call protected or non-protected
  4772. * read_offset - read pointer offset
  4773. * read_count - length of data to read
  4774. * data_id - enumerated test ID to read selftest results from
  4775. * *response - pointer to store the read response status
  4776. * *config - pointer to store config data
  4777. * *actual_read_len - pointer to store data length actually read
  4778. * *read_buf - pointer to the read buffer
  4779. ******************************************************************************/
  4780. static int pt_hid_output_retrieve_panel_scan(
  4781. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4782. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4783. u8 *read_buf)
  4784. {
  4785. int rc;
  4786. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4787. if (rc < 0) {
  4788. pt_debug(cd->dev, DL_ERROR,
  4789. "%s: fail get exclusive ex=%p own=%p\n",
  4790. __func__, cd->exclusive_dev, cd->dev);
  4791. return rc;
  4792. }
  4793. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4794. read_count, data_id, response, config,
  4795. actual_read_len, read_buf);
  4796. if (release_exclusive(cd, cd->dev) < 0)
  4797. pt_debug(cd->dev, DL_ERROR,
  4798. "%s: fail to release exclusive\n", __func__);
  4799. return rc;
  4800. }
  4801. /*******************************************************************************
  4802. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4803. *
  4804. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4805. * calls to the protected or unprotected call to
  4806. * pt_hid_output_retrieve_panel_scan
  4807. *
  4808. * RETURN:
  4809. * 0 = success
  4810. * !0 = failure
  4811. *
  4812. * PARAMETERS:
  4813. * *dev - pointer to device structure
  4814. * protect - flag to call protected or non-protected
  4815. * read_offset - read pointer offset
  4816. * read_count - length of data to read
  4817. * data_id - enumerated test ID to read selftest results from
  4818. * *response - pointer to store the read response status
  4819. * *config - pointer to store config data
  4820. * *actual_read_len - pointer to store data length actually read
  4821. * *read_buf - pointer to the read buffer
  4822. ******************************************************************************/
  4823. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4824. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4825. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4826. {
  4827. struct pt_core_data *cd = dev_get_drvdata(dev);
  4828. if (protect)
  4829. return pt_hid_output_retrieve_panel_scan(cd,
  4830. read_offset, read_count, data_id, response,
  4831. config, actual_read_len, read_buf);
  4832. return pt_hid_output_retrieve_panel_scan_(cd,
  4833. read_offset, read_count, data_id, response,
  4834. config, actual_read_len, read_buf);
  4835. }
  4836. /*******************************************************************************
  4837. * FUNCTION: _pt_request_pip_user_cmd
  4838. *
  4839. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4840. * calls to the protected or unprotected call to
  4841. * pt_hid_output_user_cmd
  4842. *
  4843. * RETURN:
  4844. * 0 = success
  4845. * !0 = failure
  4846. *
  4847. * PARAMETERS:
  4848. * *dev - pointer to device structure
  4849. * protect - flag to call protected or non-protected
  4850. * read_len - length of data to read
  4851. * *read_buf - pointer to store read data
  4852. * write_len - length of data to write
  4853. * *write_buf - pointer to buffer to write
  4854. * *actual_read_len - pointer to store data length actually read
  4855. ******************************************************************************/
  4856. static int _pt_request_pip_user_cmd(struct device *dev,
  4857. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4858. u8 *write_buf, u16 *actual_read_len)
  4859. {
  4860. struct pt_core_data *cd = dev_get_drvdata(dev);
  4861. if (protect)
  4862. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4863. write_len, write_buf, actual_read_len);
  4864. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4865. write_len, write_buf, actual_read_len);
  4866. }
  4867. /*******************************************************************************
  4868. * FUNCTION: pt_hid_output_bl_get_information_
  4869. *
  4870. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4871. * DUT to retrieve bootloader version and chip identification information.
  4872. *
  4873. * RETURN:
  4874. * 0 = success
  4875. * !0 = failure
  4876. *
  4877. * PARAMETERS:
  4878. * *cd - pointer to core data
  4879. * *return_data - pointer to store the return data
  4880. *****************************************************************************/
  4881. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4882. u8 *return_data)
  4883. {
  4884. int rc;
  4885. int data_len;
  4886. struct pt_hid_output hid_output = {
  4887. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4888. };
  4889. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4890. if (rc)
  4891. return rc;
  4892. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4893. if (!data_len)
  4894. return -EPROTO;
  4895. memcpy(return_data, &cd->response_buf[8], data_len);
  4896. return 0;
  4897. }
  4898. /*******************************************************************************
  4899. * FUNCTION: pt_hid_output_bl_get_information
  4900. *
  4901. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4902. * access to the DUT.
  4903. *
  4904. * RETURN:
  4905. * 0 = success
  4906. * !0 = failure
  4907. *
  4908. * PARAMETERS:
  4909. * *cd - pointer to core data
  4910. * *return_data - pointer to store the return data
  4911. ******************************************************************************/
  4912. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4913. u8 *return_data)
  4914. {
  4915. int rc;
  4916. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4917. if (rc < 0) {
  4918. pt_debug(cd->dev, DL_ERROR,
  4919. "%s: fail get exclusive ex=%p own=%p\n",
  4920. __func__, cd->exclusive_dev, cd->dev);
  4921. return rc;
  4922. }
  4923. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4924. if (release_exclusive(cd, cd->dev) < 0)
  4925. pt_debug(cd->dev, DL_ERROR,
  4926. "%s: fail to release exclusive\n", __func__);
  4927. return rc;
  4928. }
  4929. /*******************************************************************************
  4930. * FUNCTION: _pt_request_pip_bl_get_information
  4931. *
  4932. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4933. * calls to the protected or unprotected call to
  4934. * pt_hid_output_bl_get_information
  4935. *
  4936. * RETURN:
  4937. * 0 = success
  4938. * !0 = failure
  4939. *
  4940. * PARAMETERS:
  4941. * *dev - pointer to device structure
  4942. * protect - flag to call protected or non-protected
  4943. * *return_data - pointer to store bl data
  4944. ******************************************************************************/
  4945. static int _pt_request_pip_bl_get_information(struct device *dev,
  4946. int protect, u8 *return_data)
  4947. {
  4948. struct pt_core_data *cd = dev_get_drvdata(dev);
  4949. if (protect)
  4950. return pt_hid_output_bl_get_information(cd, return_data);
  4951. return pt_hid_output_bl_get_information_(cd, return_data);
  4952. }
  4953. /*******************************************************************************
  4954. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4955. *
  4956. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4957. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4958. * and Design Data block in flash and enables the host to execute the Program
  4959. * and Verify Row command to bootload the application image and data.
  4960. *
  4961. * RETURN:
  4962. * 0 = success
  4963. * !0 = failure
  4964. *
  4965. * PARAMETERS:
  4966. * *cd - pointer to core data
  4967. * protect - flag to call protected or non-protected
  4968. * key_size - size of key
  4969. * *key_buf - pointer to key data to allow operation
  4970. * row_size - size of the meta data row
  4971. * *metadata_row_buf - pointer to meta data to write
  4972. ******************************************************************************/
  4973. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4974. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4975. {
  4976. u16 write_length = key_size + row_size;
  4977. u8 *write_buf;
  4978. int rc = 0;
  4979. struct pt_hid_output hid_output = {
  4980. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4981. .write_length = write_length,
  4982. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4983. };
  4984. write_buf = kzalloc(write_length, GFP_KERNEL);
  4985. if (!write_buf)
  4986. return -ENOMEM;
  4987. hid_output.write_buf = write_buf;
  4988. if (key_size)
  4989. memcpy(write_buf, key_buf, key_size);
  4990. if (row_size)
  4991. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4992. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4993. kfree(write_buf);
  4994. return rc;
  4995. }
  4996. /*******************************************************************************
  4997. * FUNCTION: pt_hid_output_bl_initiate_bl
  4998. *
  4999. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5000. * access to the DUT.
  5001. *
  5002. * RETURN:
  5003. * 0 = success
  5004. * !0 = failure
  5005. *
  5006. * PARAMETERS:
  5007. * *cd - pointer to core data
  5008. * key_size - size of key
  5009. * *key_buf - pointer to key data to allow operation
  5010. * row_size - size of the meta data row
  5011. * *metadata_row_buf - pointer to meta data to write
  5012. ******************************************************************************/
  5013. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5014. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5015. {
  5016. int rc;
  5017. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5018. if (rc < 0) {
  5019. pt_debug(cd->dev, DL_ERROR,
  5020. "%s: fail get exclusive ex=%p own=%p\n",
  5021. __func__, cd->exclusive_dev, cd->dev);
  5022. return rc;
  5023. }
  5024. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5025. row_size, metadata_row_buf);
  5026. if (release_exclusive(cd, cd->dev) < 0)
  5027. pt_debug(cd->dev, DL_ERROR,
  5028. "%s: fail to release exclusive\n", __func__);
  5029. return rc;
  5030. }
  5031. /*******************************************************************************
  5032. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5033. *
  5034. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5035. * calls to the protected or unprotected call to
  5036. * pt_hid_output_bl_initiate_bl
  5037. *
  5038. * RETURN:
  5039. * 0 = success
  5040. * !0 = failure
  5041. *
  5042. * PARAMETERS:
  5043. * *dev - pointer to device structure
  5044. * protect - flag to call protected or non-protected
  5045. * key_size - size of key
  5046. * *key_buf - pointer to key data to allow operation
  5047. * row_size - size of the meta data row
  5048. * *metadata_row_buf - pointer to meta data to write
  5049. ******************************************************************************/
  5050. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5051. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5052. u8 *metadata_row_buf)
  5053. {
  5054. struct pt_core_data *cd = dev_get_drvdata(dev);
  5055. if (protect)
  5056. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5057. row_size, metadata_row_buf);
  5058. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5059. row_size, metadata_row_buf);
  5060. }
  5061. /*******************************************************************************
  5062. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5063. *
  5064. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5065. * and program a 128-byte row into the flash, and then verifies written data.
  5066. *
  5067. * RETURN:
  5068. * 0 = success
  5069. * !0 = failure
  5070. *
  5071. * PARAMETERS:
  5072. * *cd - pointer to core data
  5073. * data_len - length of data_buf
  5074. * *data_buf - firmware image to program
  5075. ******************************************************************************/
  5076. static int pt_hid_output_bl_program_and_verify_(
  5077. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5078. {
  5079. struct pt_hid_output hid_output = {
  5080. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5081. .write_length = data_len,
  5082. .write_buf = data_buf,
  5083. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5084. };
  5085. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5086. }
  5087. /*******************************************************************************
  5088. * FUNCTION: pt_hid_output_bl_program_and_verify
  5089. *
  5090. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5091. * access to the DUT.
  5092. *
  5093. * RETURN:
  5094. * 0 = success
  5095. * !0 = failure
  5096. *
  5097. * PARAMETERS:
  5098. * *cd - pointer to core data
  5099. * data_len - length of data_buf
  5100. * *data_buf - firmware image to program
  5101. ******************************************************************************/
  5102. static int pt_hid_output_bl_program_and_verify(
  5103. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5104. {
  5105. int rc;
  5106. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5107. if (rc < 0) {
  5108. pt_debug(cd->dev, DL_ERROR,
  5109. "%s: fail get exclusive ex=%p own=%p\n",
  5110. __func__, cd->exclusive_dev, cd->dev);
  5111. return rc;
  5112. }
  5113. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5114. if (release_exclusive(cd, cd->dev) < 0)
  5115. pt_debug(cd->dev, DL_ERROR,
  5116. "%s: fail to release exclusive\n", __func__);
  5117. return rc;
  5118. }
  5119. /*******************************************************************************
  5120. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5121. *
  5122. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5123. * to request to have the BL program and verify a FW image
  5124. *
  5125. * RETURN:
  5126. * 0 = success
  5127. * !0 = failure
  5128. *
  5129. * PARAMETERS:
  5130. * *dev - pointer to device structure
  5131. * protect - boolean to determine to call the protected function
  5132. * data_len - length of data_buf
  5133. * *data_buf - firmware image to program
  5134. ******************************************************************************/
  5135. static int _pt_request_pip_bl_program_and_verify(
  5136. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5137. {
  5138. struct pt_core_data *cd = dev_get_drvdata(dev);
  5139. if (protect)
  5140. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5141. data_buf);
  5142. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5143. data_buf);
  5144. }
  5145. /*******************************************************************************
  5146. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5147. *
  5148. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5149. * perform a full verification of the application integrity by calculating the
  5150. * CRC of the image in flash and compare it to the expected CRC stored in the
  5151. * Metadata row.
  5152. *
  5153. * RETURN:
  5154. * 0 = success
  5155. * !0 = failure
  5156. *
  5157. * PARAMETERS:
  5158. * *cd - pointer to core data
  5159. * *result - pointer to store result
  5160. ******************************************************************************/
  5161. static int pt_hid_output_bl_verify_app_integrity_(
  5162. struct pt_core_data *cd, u8 *result)
  5163. {
  5164. int rc;
  5165. struct pt_hid_output hid_output = {
  5166. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5167. };
  5168. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5169. if (rc) {
  5170. *result = 0;
  5171. return rc;
  5172. }
  5173. *result = cd->response_buf[8];
  5174. return 0;
  5175. }
  5176. /*******************************************************************************
  5177. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5178. *
  5179. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5180. * exclusive access to the DUT.
  5181. *
  5182. * RETURN:
  5183. * 0 = success
  5184. * !0 = failure
  5185. *
  5186. * PARAMETERS:
  5187. * *cd - pointer to core data
  5188. * *result - pointer to store result
  5189. ******************************************************************************/
  5190. static int pt_hid_output_bl_verify_app_integrity(
  5191. struct pt_core_data *cd, u8 *result)
  5192. {
  5193. int rc;
  5194. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5195. if (rc < 0) {
  5196. pt_debug(cd->dev, DL_ERROR,
  5197. "%s: fail get exclusive ex=%p own=%p\n",
  5198. __func__, cd->exclusive_dev, cd->dev);
  5199. return rc;
  5200. }
  5201. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5202. if (release_exclusive(cd, cd->dev) < 0)
  5203. pt_debug(cd->dev, DL_ERROR,
  5204. "%s: fail to release exclusive\n", __func__);
  5205. return rc;
  5206. }
  5207. /*******************************************************************************
  5208. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5209. *
  5210. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5211. * to request to have the BL verify the application integrity (PIP1.x only)
  5212. *
  5213. * RETURN:
  5214. * 0 = success
  5215. * !0 = failure
  5216. *
  5217. * PARAMETERS:
  5218. * *dev - pointer to device structure
  5219. * protect - boolean to determine to call the protected function
  5220. * *result - pointer to store result
  5221. ******************************************************************************/
  5222. static int _pt_request_pip_bl_verify_app_integrity(
  5223. struct device *dev, int protect, u8 *result)
  5224. {
  5225. struct pt_core_data *cd = dev_get_drvdata(dev);
  5226. if (protect)
  5227. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5228. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5229. }
  5230. /*******************************************************************************
  5231. * FUNCTION: pt_hid_output_bl_launch_app_
  5232. *
  5233. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5234. * application from bootloader (PIP1.x only).
  5235. *
  5236. * RETURN:
  5237. * 0 = success
  5238. * !0 = failure
  5239. *
  5240. * PARAMETERS:
  5241. * *cd - pointer to core data
  5242. ******************************************************************************/
  5243. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5244. {
  5245. struct pt_hid_output hid_output = {
  5246. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5247. .reset_expected = 1,
  5248. };
  5249. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5250. }
  5251. /*******************************************************************************
  5252. * FUNCTION: pt_hid_output_bl_launch_app
  5253. *
  5254. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5255. * to the DUT.
  5256. *
  5257. * RETURN:
  5258. * 0 = success
  5259. * !0 = failure
  5260. *
  5261. * PARAMETERS:
  5262. * *cd - pointer to core data
  5263. ******************************************************************************/
  5264. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5265. {
  5266. int rc;
  5267. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5268. if (rc < 0) {
  5269. pt_debug(cd->dev, DL_ERROR,
  5270. "%s: fail get exclusive ex=%p own=%p\n",
  5271. __func__, cd->exclusive_dev, cd->dev);
  5272. return rc;
  5273. }
  5274. rc = pt_hid_output_bl_launch_app_(cd);
  5275. if (release_exclusive(cd, cd->dev) < 0)
  5276. pt_debug(cd->dev, DL_ERROR,
  5277. "%s: fail to release exclusive\n", __func__);
  5278. return rc;
  5279. }
  5280. /*******************************************************************************
  5281. * FUNCTION: _pt_request_pip_launch_app
  5282. *
  5283. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5284. * to request to have the BL launch the application. (PIP1.x only)
  5285. *
  5286. * RETURN:
  5287. * 0 = success
  5288. * !0 = failure
  5289. *
  5290. * PARAMETERS:
  5291. * *dev - pointer to device structure
  5292. * protect - boolean to determine to call the protected function
  5293. ******************************************************************************/
  5294. static int _pt_request_pip_launch_app(struct device *dev,
  5295. int protect)
  5296. {
  5297. struct pt_core_data *cd = dev_get_drvdata(dev);
  5298. if (protect)
  5299. return pt_hid_output_bl_launch_app(cd);
  5300. return pt_hid_output_bl_launch_app_(cd);
  5301. }
  5302. /*******************************************************************************
  5303. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5304. *
  5305. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5306. * value store in the System Information.
  5307. *
  5308. * RETURN:
  5309. * 0 = success
  5310. * !0 = failure
  5311. *
  5312. * PARAMETERS:
  5313. * *cd - pointer to core data
  5314. * *panel_id - pointer to where the panel ID will be stored
  5315. ******************************************************************************/
  5316. static int pt_hid_output_bl_get_panel_id_(
  5317. struct pt_core_data *cd, u8 *panel_id)
  5318. {
  5319. int rc;
  5320. struct pt_hid_output hid_output = {
  5321. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5322. };
  5323. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5324. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5325. pt_debug(cd->dev, DL_ERROR,
  5326. "%s: Get Panel ID command not supported\n",
  5327. __func__);
  5328. *panel_id = PANEL_ID_NOT_ENABLED;
  5329. return 0;
  5330. } else if (rc < 0) {
  5331. pt_debug(cd->dev, DL_ERROR,
  5332. "%s: Error on Get Panel ID command\n", __func__);
  5333. return rc;
  5334. }
  5335. *panel_id = cd->response_buf[8];
  5336. return 0;
  5337. }
  5338. /*******************************************************************************
  5339. * FUNCTION: pt_hid_output_bl_get_panel_id
  5340. *
  5341. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5342. * to the DUT.
  5343. *
  5344. * RETURN:
  5345. * 0 = success
  5346. * !0 = failure
  5347. *
  5348. * PARAMETERS:
  5349. * *cd - pointer to core data
  5350. * *panel_id - pointer to where the panel ID will be stored
  5351. ******************************************************************************/
  5352. static int pt_hid_output_bl_get_panel_id(
  5353. struct pt_core_data *cd, u8 *panel_id)
  5354. {
  5355. int rc;
  5356. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5357. if (rc < 0) {
  5358. pt_debug(cd->dev, DL_ERROR,
  5359. "%s: fail get exclusive ex=%p own=%p\n",
  5360. __func__, cd->exclusive_dev, cd->dev);
  5361. return rc;
  5362. }
  5363. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5364. if (release_exclusive(cd, cd->dev) < 0)
  5365. pt_debug(cd->dev, DL_ERROR,
  5366. "%s: fail to release exclusive\n", __func__);
  5367. return rc;
  5368. }
  5369. /*******************************************************************************
  5370. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5371. *
  5372. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5373. * to have the BL retrieve the panel ID
  5374. *
  5375. * RETURN:
  5376. * 0 = success
  5377. * !0 = failure
  5378. *
  5379. * PARAMETERS:
  5380. * *dev - pointer to device structure
  5381. * protect - flag to run in protected mode
  5382. * *panel_id - pointer to where the panel ID will be stored
  5383. ******************************************************************************/
  5384. static int _pt_request_pip_bl_get_panel_id(
  5385. struct device *dev, int protect, u8 *panel_id)
  5386. {
  5387. struct pt_core_data *cd = dev_get_drvdata(dev);
  5388. if (protect)
  5389. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5390. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5391. }
  5392. /*******************************************************************************
  5393. * FUNCTION: pt_pip2_get_mode_sysmode_
  5394. *
  5395. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5396. * PIP2 STATUS command.
  5397. *
  5398. * RETURN:
  5399. * 0 = success
  5400. * !0 = failure
  5401. *
  5402. * PARAMETERS:
  5403. * *cd - pointer to core data structure
  5404. * *mode - pointer to store the retrieved mode
  5405. * *sys_mode - pointer to store the FW system mode
  5406. ******************************************************************************/
  5407. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5408. u8 *mode, u8 *sys_mode)
  5409. {
  5410. u16 actual_read_len;
  5411. u8 read_buf[12];
  5412. u8 status, boot;
  5413. int rc = 0;
  5414. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5415. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5416. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5417. __func__, rc);
  5418. if (!rc) {
  5419. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5420. "PIP2 STATUS");
  5421. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5422. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5423. if (sys_mode) {
  5424. if (status == PIP2_RSP_ERR_NONE &&
  5425. boot == PIP2_STATUS_APP_EXEC)
  5426. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5427. else
  5428. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5429. }
  5430. if (mode) {
  5431. if (status == PIP2_RSP_ERR_NONE &&
  5432. boot == PIP2_STATUS_BOOT_EXEC)
  5433. *mode = PT_MODE_BOOTLOADER;
  5434. else if (status == PIP2_RSP_ERR_NONE &&
  5435. boot == PIP2_STATUS_APP_EXEC)
  5436. *mode = PT_MODE_OPERATIONAL;
  5437. else
  5438. *mode = PT_MODE_UNKNOWN;
  5439. }
  5440. } else {
  5441. if (mode)
  5442. *mode = PT_MODE_UNKNOWN;
  5443. if (sys_mode)
  5444. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5445. pt_debug(cd->dev, DL_WARN,
  5446. "%s: Mode and sys_mode could not be determined\n",
  5447. __func__);
  5448. }
  5449. return rc;
  5450. }
  5451. /*******************************************************************************
  5452. * FUNCTION: pt_pip2_get_mode_sysmode
  5453. *
  5454. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5455. * access to the DUT.
  5456. *
  5457. * RETURN:
  5458. * 0 = success
  5459. * !0 = failure
  5460. *
  5461. * PARAMETERS:
  5462. * *cd - pointer to core data structure
  5463. * *mode - pointer to store the retrieved mode
  5464. * *sys_mode - pointer to store the FW system mode
  5465. ******************************************************************************/
  5466. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5467. u8 *mode, u8 *sys_mode)
  5468. {
  5469. int rc;
  5470. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5471. if (rc < 0) {
  5472. pt_debug(cd->dev, DL_ERROR,
  5473. "%s: fail get exclusive ex=%p own=%p\n",
  5474. __func__, cd->exclusive_dev, cd->dev);
  5475. return rc;
  5476. }
  5477. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5478. if (release_exclusive(cd, cd->dev) < 0)
  5479. pt_debug(cd->dev, DL_ERROR,
  5480. "%s: fail to release exclusive\n", __func__);
  5481. return rc;
  5482. }
  5483. /*******************************************************************************
  5484. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5485. *
  5486. * SUMMARY: Function pointer included in core_commands struct for external
  5487. * calls to the protected or unprotected call to
  5488. * pt_pip2_get_mode_sysmode
  5489. *
  5490. * RETURN:
  5491. * 0 = success
  5492. * !0 = failure
  5493. *
  5494. * PARAMETERS:
  5495. * *dev - pointer to device structure
  5496. * *mode - pointer to store the retrieved mode
  5497. * *sys_mode - pointer to store the FW system mode
  5498. ******************************************************************************/
  5499. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5500. int protect, u8 *mode, u8 *sys_mode)
  5501. {
  5502. struct pt_core_data *cd = dev_get_drvdata(dev);
  5503. if (protect)
  5504. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5505. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5506. }
  5507. /*******************************************************************************
  5508. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5509. *
  5510. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5511. * mode only the following PIP commands will be serviced, any other PIP
  5512. * command the FW will respond with an "Invalid PIP Command" response.
  5513. * - Get HID Descriptor (Register 0x0001, no command ID)
  5514. * - Reset (Register 0x0005, RESET HID request)
  5515. * - Ping (Register 0x0004, Command ID 0x00
  5516. * - Get System Information (Register 0x0004, Command ID 0x02)
  5517. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5518. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5519. * This function will loop on the results of the STATUS command until
  5520. * the FW reports it is out of BOOT mode.
  5521. *
  5522. * NOTE:
  5523. * - This function will update cd->fw_system_mode
  5524. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5525. *
  5526. * RETURN:
  5527. * 0 = success
  5528. * !0 = failure
  5529. *
  5530. * PARAMETERS:
  5531. * *cd - pointer to core data
  5532. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5533. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5534. ******************************************************************************/
  5535. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5536. int *actual_wait)
  5537. {
  5538. int loop = 0;
  5539. u8 sys_mode = cd->fw_system_mode;
  5540. u8 pause = 10; /* in ms */
  5541. int rc = 0;
  5542. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5543. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5544. /*
  5545. * For PIP <1.11, no support for polling wait so do a hard
  5546. * coded wait and assume the FW is out of BOOT. Added 1 to
  5547. * timeout to make it clear in kmsg if non polling was done.
  5548. */
  5549. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5550. pt_debug(cd->dev, DL_ERROR,
  5551. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5552. __func__,
  5553. cd->sysinfo.ttdata.pip_ver_major,
  5554. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5555. msleep(*actual_wait);
  5556. sys_mode = FW_SYS_MODE_SCANNING;
  5557. }
  5558. if (sys_mode == FW_SYS_MODE_BOOT) {
  5559. while (!rc && loop <= max_loop &&
  5560. (sys_mode == FW_SYS_MODE_BOOT)) {
  5561. loop++;
  5562. usleep_range(9000, pause * 1000);
  5563. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5564. pt_debug(cd->dev, DL_DEBUG,
  5565. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5566. __func__, loop * pause, sys_mode);
  5567. }
  5568. *actual_wait = (int)(loop * pause);
  5569. pt_debug(cd->dev, DL_WARN,
  5570. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5571. __func__, *actual_wait, sys_mode);
  5572. if (rc)
  5573. sys_mode = FW_SYS_MODE_UNDEFINED;
  5574. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5575. sys_mode == FW_SYS_MODE_UNDEFINED)
  5576. rc = -EBUSY;
  5577. }
  5578. mutex_lock(&cd->system_lock);
  5579. cd->fw_system_mode = sys_mode;
  5580. mutex_unlock(&cd->system_lock);
  5581. return rc;
  5582. }
  5583. /*******************************************************************************
  5584. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5585. *
  5586. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5587. * access to the DUT.
  5588. *
  5589. * RETURN:
  5590. * 0 = success
  5591. * !0 = failure
  5592. *
  5593. * PARAMETERS:
  5594. * *cd - pointer to core data
  5595. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5596. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5597. ******************************************************************************/
  5598. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5599. int *actual_wait)
  5600. {
  5601. int rc;
  5602. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5603. if (rc < 0) {
  5604. pt_debug(cd->dev, DL_ERROR,
  5605. "%s: fail get exclusive ex=%p own=%p\n",
  5606. __func__, cd->exclusive_dev, cd->dev);
  5607. return rc;
  5608. }
  5609. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5610. if (release_exclusive(cd, cd->dev) < 0)
  5611. pt_debug(cd->dev, DL_ERROR,
  5612. "%s: fail to release exclusive\n", __func__);
  5613. return rc;
  5614. }
  5615. /*******************************************************************************
  5616. * FUNCTION: _pt_get_fw_sys_mode
  5617. *
  5618. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5619. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5620. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5621. * information but any PIP command above 0x1F requires scanning to be disabled
  5622. * before it will be operational. If scanning was not disabled before sending
  5623. * these PIP commands the FW will respond with a 6 byte error response. So to
  5624. * safely determine the scanning state, a PIP message that will not affect the
  5625. * operation of the FW was chosen:
  5626. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5627. * received scanning is active.
  5628. *
  5629. * RETURN:
  5630. * 0 = success
  5631. * !0 = failure
  5632. *
  5633. * PARAMETERS:
  5634. * *cd - pointer to core data
  5635. * *sys_mode - pointer to FW System mode
  5636. * *mode - pointer to mode (BL/FW)
  5637. ******************************************************************************/
  5638. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5639. {
  5640. int write_length = 1;
  5641. int report_length;
  5642. int rc = 0;
  5643. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5644. u8 tmp_mode = PT_MODE_UNKNOWN;
  5645. u8 param[1] = { PT_TCH_PARM_EBID };
  5646. struct pt_hid_output hid_output = {
  5647. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5648. .write_length = write_length,
  5649. .write_buf = param,
  5650. .novalidate = true,
  5651. };
  5652. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5653. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5654. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5655. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5656. __func__, tmp_sys_mode, tmp_mode);
  5657. if (!rc) {
  5658. if (tmp_mode != PT_MODE_OPERATIONAL)
  5659. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5660. }
  5661. goto exit;
  5662. }
  5663. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5664. if (cd->mode != PT_MODE_OPERATIONAL) {
  5665. tmp_mode = cd->mode;
  5666. goto exit;
  5667. }
  5668. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5669. if (rc)
  5670. goto exit;
  5671. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5672. if ((report_length == 0x06) &&
  5673. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5674. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5675. tmp_mode = PIP2_STATUS_APP_EXEC;
  5676. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5677. } else if ((report_length == 0x0A) &&
  5678. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5679. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5680. tmp_mode = PIP2_STATUS_APP_EXEC;
  5681. tmp_sys_mode = FW_SYS_MODE_TEST;
  5682. }
  5683. exit:
  5684. if (mode)
  5685. *mode = tmp_mode;
  5686. if (sys_mode)
  5687. *sys_mode = tmp_sys_mode;
  5688. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5689. __func__, tmp_mode, tmp_sys_mode);
  5690. return rc;
  5691. }
  5692. /*******************************************************************************
  5693. * FUNCTION: pt_get_fw_sys_mode
  5694. *
  5695. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5696. * is active or not.
  5697. *
  5698. * RETURN:
  5699. * 0 = success
  5700. * !0 = failure
  5701. *
  5702. * PARAMETERS:
  5703. * *cd - pointer to core data
  5704. * *sys_mode - pointer to fw system mode
  5705. * *mode - pointer to mode
  5706. ******************************************************************************/
  5707. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5708. {
  5709. int rc;
  5710. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5711. if (rc < 0) {
  5712. pt_debug(cd->dev, DL_ERROR,
  5713. "%s: fail get exclusive ex=%p own=%p\n",
  5714. __func__, cd->exclusive_dev, cd->dev);
  5715. return rc;
  5716. }
  5717. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5718. if (release_exclusive(cd, cd->dev) < 0)
  5719. pt_debug(cd->dev, DL_ERROR,
  5720. "%s: fail to release exclusive\n", __func__);
  5721. return rc;
  5722. }
  5723. /*******************************************************************************
  5724. * FUNCTION: _pt_request_get_fw_sys_mode
  5725. *
  5726. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5727. * to request to get scan state
  5728. *
  5729. * RETURN:
  5730. * 0 = success
  5731. * !0 = failure
  5732. *
  5733. * PARAMETERS:
  5734. * *dev - pointer to device structure
  5735. * protect - flag to call protected or non-protected
  5736. * *sys_mode - pointer to FW system mode
  5737. * *mode - pointer to mode
  5738. ******************************************************************************/
  5739. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5740. u8 *sys_mode, u8 *mode)
  5741. {
  5742. struct pt_core_data *cd = dev_get_drvdata(dev);
  5743. if (protect)
  5744. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5745. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5746. }
  5747. /* Default hid descriptor to provide basic register map */
  5748. const struct pt_hid_desc hid_desc_default = {
  5749. 230, /* hid_desc_len */
  5750. HID_APP_REPORT_ID, /* packet_id */
  5751. 0x00, /* reserved_byte */
  5752. 0x0100, /* bcd_version */
  5753. 0x00EC, /* report_desc_len */
  5754. 0x0002, /* report_desc_register */
  5755. 0x0003, /* input_register */
  5756. 0x00FE, /* max_input_len */
  5757. 0x0004, /* output_register */
  5758. 0x00FE, /* max_output_len */
  5759. 0x0005, /* command_register */
  5760. 0x0006, /* data_register */
  5761. 0x04B4, /* vendor_id */
  5762. 0xC101, /* product_id */
  5763. 0x0100, /* version_id */
  5764. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5765. };
  5766. /*******************************************************************************
  5767. * FUNCTION: pt_init_hid_descriptor
  5768. *
  5769. * SUMMARY: Setup default values for HID descriptor structure
  5770. *
  5771. *
  5772. * PARAMETERS:
  5773. * *desc - pointer to the HID descriptor data read back from DUT
  5774. ******************************************************************************/
  5775. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5776. {
  5777. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5778. }
  5779. /*******************************************************************************
  5780. * FUNCTION: pt_get_hid_descriptor_
  5781. *
  5782. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5783. * into the HID descriptor structure
  5784. *
  5785. * RETURN:
  5786. * 0 = success
  5787. * !0 = failure
  5788. *
  5789. * PARAMETERS:
  5790. * *cd - pointer to core data
  5791. * *desc - pointer to the HID descriptor data read back from DUT
  5792. ******************************************************************************/
  5793. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5794. struct pt_hid_desc *desc)
  5795. {
  5796. struct device *dev = cd->dev;
  5797. int rc = 0;
  5798. int t;
  5799. u8 cmd[2];
  5800. /*
  5801. * During startup the HID descriptor is required for all future
  5802. * processing. If IRQ is already asserted due to an early touch report
  5803. * the report must be cleared before sending command.
  5804. */
  5805. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5806. /* Read HID descriptor length and version */
  5807. mutex_lock(&cd->system_lock);
  5808. cd->hid_cmd_state = 1;
  5809. mutex_unlock(&cd->system_lock);
  5810. /* Set HID descriptor register */
  5811. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5812. sizeof(cd->hid_core.hid_desc_register));
  5813. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5814. __func__, sizeof(cmd));
  5815. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5816. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5817. if (rc) {
  5818. pt_debug(dev, DL_ERROR,
  5819. "%s: failed to get HID descriptor, rc=%d\n",
  5820. __func__, rc);
  5821. goto error;
  5822. }
  5823. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5824. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5825. if (IS_TMO(t)) {
  5826. #ifdef TTDL_DIAGNOSTICS
  5827. cd->bus_transmit_error_count++;
  5828. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5829. #endif /* TTDL_DIAGNOSTICS */
  5830. pt_debug(cd->dev, DL_ERROR,
  5831. "%s: HID get descriptor timed out\n", __func__);
  5832. rc = -ETIME;
  5833. goto error;
  5834. } else {
  5835. cd->hw_detected = true;
  5836. }
  5837. /* Load the HID descriptor including all registers */
  5838. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5839. /* Check HID descriptor length and version */
  5840. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5841. le16_to_cpu(desc->hid_desc_len),
  5842. le16_to_cpu(desc->bcd_version));
  5843. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5844. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5845. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5846. __func__);
  5847. return -ENODEV;
  5848. }
  5849. goto exit;
  5850. error:
  5851. mutex_lock(&cd->system_lock);
  5852. cd->hid_cmd_state = 0;
  5853. mutex_unlock(&cd->system_lock);
  5854. exit:
  5855. return rc;
  5856. }
  5857. /*******************************************************************************
  5858. * FUNCTION: pt_get_hid_descriptor
  5859. *
  5860. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5861. *
  5862. * RETURN:
  5863. * 0 = success
  5864. * !0 = failure
  5865. *
  5866. * PARAMETERS:
  5867. * *cd - pointer to core data
  5868. * *desc - pointer to the HID descriptor data read back from DUT
  5869. ******************************************************************************/
  5870. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5871. struct pt_hid_desc *desc)
  5872. {
  5873. int rc;
  5874. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5875. if (rc < 0) {
  5876. pt_debug(cd->dev, DL_ERROR,
  5877. "%s: fail get exclusive ex=%p own=%p\n",
  5878. __func__, cd->exclusive_dev, cd->dev);
  5879. return rc;
  5880. }
  5881. rc = pt_get_hid_descriptor_(cd, desc);
  5882. if (release_exclusive(cd, cd->dev) < 0)
  5883. pt_debug(cd->dev, DL_ERROR,
  5884. "%s: fail to release exclusive\n", __func__);
  5885. return rc;
  5886. }
  5887. /*******************************************************************************
  5888. * FUNCTION: pt_pip2_get_version_
  5889. *
  5890. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5891. * cd-ttdata
  5892. *
  5893. * RETURN:
  5894. * 0 = success
  5895. * !0 = failure
  5896. *
  5897. * PARAMETERS:
  5898. * *cd - pointer to core data
  5899. ******************************************************************************/
  5900. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5901. {
  5902. int rc = 0;
  5903. int status;
  5904. u8 read_buf[64];
  5905. u16 actual_read_len;
  5906. rc = _pt_request_pip2_send_cmd(cd->dev,
  5907. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5908. NULL, 0, read_buf, &actual_read_len);
  5909. if (rc) {
  5910. pt_debug(cd->dev, DL_ERROR,
  5911. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5912. __func__, rc);
  5913. return rc;
  5914. }
  5915. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5916. if (status == 0) {
  5917. /* Parse the PIP2 VERSION response into ttdata */
  5918. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5919. } else {
  5920. pt_debug(cd->dev, DL_ERROR,
  5921. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5922. __func__, status);
  5923. return status;
  5924. }
  5925. return rc;
  5926. }
  5927. /*******************************************************************************
  5928. * FUNCTION: pt_pip2_get_version
  5929. *
  5930. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5931. * access to the DUT.
  5932. *
  5933. * RETURN:
  5934. * 0 = success
  5935. * !0 = failure
  5936. *
  5937. * PARAMETERS:
  5938. * *cd - pointer to core data structure
  5939. ******************************************************************************/
  5940. static int pt_pip2_get_version(struct pt_core_data *cd)
  5941. {
  5942. int rc;
  5943. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5944. if (rc < 0) {
  5945. pt_debug(cd->dev, DL_ERROR,
  5946. "%s: fail get exclusive ex=%p own=%p\n",
  5947. __func__, cd->exclusive_dev, cd->dev);
  5948. return rc;
  5949. }
  5950. rc = pt_pip2_get_version_(cd);
  5951. if (release_exclusive(cd, cd->dev) < 0)
  5952. pt_debug(cd->dev, DL_ERROR,
  5953. "%s: fail to release exclusive\n", __func__);
  5954. return rc;
  5955. }
  5956. /*******************************************************************************
  5957. * FUNCTION: _pt_request_active_pip_protocol
  5958. *
  5959. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5960. * Function will return PIP version of BL or application based on
  5961. * when it's called.
  5962. *
  5963. * RETURN:
  5964. * 0 = success
  5965. * !0 = failure
  5966. *
  5967. * PARAMETERS:
  5968. * *dev - pointer to device structure
  5969. * protect - flag to run in protected mode
  5970. * *pip_version_major - pointer to store PIP major version
  5971. * *pip_version_minor - pointer to store PIP minor version
  5972. ******************************************************************************/
  5973. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5974. u8 *pip_version_major, u8 *pip_version_minor)
  5975. {
  5976. struct pt_core_data *cd = dev_get_drvdata(dev);
  5977. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5978. int rc = 0;
  5979. struct pt_hid_output sys_info = {
  5980. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5981. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5982. };
  5983. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5984. /* Skip PIP2 command if DUT generation is confirmed */
  5985. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5986. goto skip_pip2_command;
  5987. rc = pt_pip2_get_version_(cd);
  5988. if (!rc) {
  5989. *pip_version_major = ttdata->pip_ver_major;
  5990. *pip_version_minor = ttdata->pip_ver_minor;
  5991. pt_debug(dev, DL_INFO,
  5992. "%s: pip_version = %d.%d\n", __func__,
  5993. *pip_version_major, *pip_version_minor);
  5994. } else {
  5995. /*
  5996. * Legacy products do not support the pip2 protocol to get
  5997. * pip version. However, they do support the "get sysinfo"
  5998. * command to get pip version from FW, but the bootloader
  5999. * does not support it. This function will try "get sysinfo"
  6000. * command if the pip2 command failed but this cmd could also
  6001. * fail if DUT is stuck in bootloader mode.
  6002. */
  6003. pt_debug(dev, DL_INFO,
  6004. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6005. __func__, rc);
  6006. skip_pip2_command:
  6007. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6008. if (!rc) {
  6009. *pip_version_minor =
  6010. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6011. *pip_version_major =
  6012. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6013. pt_debug(dev, DL_INFO,
  6014. "%s: pip_version = %d.%d\n", __func__,
  6015. *pip_version_major, *pip_version_minor);
  6016. } else {
  6017. *pip_version_major = 0;
  6018. *pip_version_minor = 0;
  6019. pt_debug(dev, DL_ERROR,
  6020. "%s: pip_version Not Detected\n", __func__);
  6021. }
  6022. }
  6023. return rc;
  6024. }
  6025. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6026. /*******************************************************************************
  6027. * FUNCTION: _pt_detect_dut_generation
  6028. *
  6029. * SUMMARY: Determine the generation of device that we are communicating with:
  6030. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6031. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6032. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6033. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6034. * coming form the BL or the FW:
  6035. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6036. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6037. * This function will return a modified status if it detects the DUT
  6038. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6039. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6040. *
  6041. * RETURN:
  6042. * 0 = success
  6043. * !0 = failure
  6044. *
  6045. * PARAMETERS:
  6046. * *dev - pointer to device structure
  6047. * *status - pointer to status bitmask
  6048. * *dut_gen - pointer to store the dut_generation
  6049. * *mode - pointer to store the PT_MODE
  6050. ******************************************************************************/
  6051. static int _pt_detect_dut_generation(struct device *dev,
  6052. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6053. {
  6054. int rc = 0;
  6055. u8 dut_gen_tmp = DUT_UNKNOWN;
  6056. u8 mode_tmp = PT_MODE_UNKNOWN;
  6057. u8 attempt = 1;
  6058. u32 status_tmp = STARTUP_STATUS_START;
  6059. struct pt_core_data *cd = dev_get_drvdata(dev);
  6060. struct pt_hid_desc hid_desc;
  6061. memset(&hid_desc, 0, sizeof(hid_desc));
  6062. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6063. while (rc && attempt < 3) {
  6064. attempt++;
  6065. usleep_range(2000, 5000);
  6066. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6067. }
  6068. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6069. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6070. mode_tmp = PT_MODE_BOOTLOADER;
  6071. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6072. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6073. rc = pt_pip2_get_version_(cd);
  6074. if (!rc)
  6075. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6076. else
  6077. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6078. mode_tmp = PT_MODE_OPERATIONAL;
  6079. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6080. rc = 0; /* To return success instead of error code */
  6081. } else if (rc) {
  6082. rc = pt_pip2_get_version_(cd);
  6083. if (!rc) {
  6084. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6085. mode_tmp = PT_MODE_BOOTLOADER;
  6086. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6087. }
  6088. }
  6089. mutex_lock(&cd->system_lock);
  6090. if (dut_gen)
  6091. *dut_gen = dut_gen_tmp;
  6092. if (mode)
  6093. *mode = mode_tmp;
  6094. if (status)
  6095. *status = status_tmp;
  6096. mutex_unlock(&cd->system_lock);
  6097. #ifdef TTDL_DIAGNOSTICS
  6098. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6099. __func__, dut_gen_tmp, mode_tmp);
  6100. #endif /* TTDL_DIAGNOSTICS */
  6101. return rc;
  6102. }
  6103. /*******************************************************************************
  6104. * FUNCTION: _pt_request_dut_generation
  6105. *
  6106. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6107. * to get current dut generation.
  6108. *
  6109. * NOTE: This function WILL NOT try to determine dut generation.
  6110. *
  6111. * RETURN:
  6112. * The current dut generation.
  6113. *
  6114. * PARAMETERS:
  6115. * *dev - pointer to device structure
  6116. ******************************************************************************/
  6117. static int _pt_request_dut_generation(struct device *dev)
  6118. {
  6119. struct pt_core_data *cd = dev_get_drvdata(dev);
  6120. return cd->active_dut_generation;
  6121. }
  6122. #define HW_VERSION_LEN_MAX 13
  6123. /*******************************************************************************
  6124. * FUNCTION: _legacy_generate_hw_version
  6125. *
  6126. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6127. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6128. * command) to the hw_version.
  6129. *
  6130. * RETURN:
  6131. * 0 = success
  6132. * !0 = failure
  6133. *
  6134. * PARAMETERS:
  6135. * *cd - pointer to core data
  6136. * *hw_version - pointer to store the hardware version
  6137. ******************************************************************************/
  6138. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6139. char *hw_version)
  6140. {
  6141. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6142. if (cd->sysinfo.ready) {
  6143. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6144. ttdata->jtag_id_h, cd->pid_for_loader);
  6145. return 0;
  6146. } else if (cd->bl_info.ready) {
  6147. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6148. cd->bl_info.chip_id, cd->pid_for_loader);
  6149. return 0;
  6150. } else {
  6151. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6152. pt_debug(cd->dev, DL_ERROR,
  6153. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6154. return -ENODATA;
  6155. }
  6156. }
  6157. /*******************************************************************************
  6158. * FUNCTION: _pip2_generate_hw_version
  6159. *
  6160. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6161. * VERSION command) to the hw_version.
  6162. *
  6163. * RETURN:
  6164. * 0 = success
  6165. * !0 = failure
  6166. *
  6167. * PARAMETERS:
  6168. * *cd - pointer to core data
  6169. * *hw_version - pointer to store the hardware version
  6170. ******************************************************************************/
  6171. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6172. {
  6173. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6174. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6175. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6176. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6177. return 0;
  6178. } else {
  6179. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6180. pt_debug(cd->dev, DL_ERROR,
  6181. "%s: PIP Version are not ready\n", __func__);
  6182. return -ENODATA;
  6183. }
  6184. }
  6185. /*******************************************************************************
  6186. * FUNCTION: pt_generate_hw_version
  6187. *
  6188. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6189. * hw_version from static data.
  6190. *
  6191. * RETURN:
  6192. * 0 = success
  6193. * !0 = failure
  6194. *
  6195. * PARAMETERS:
  6196. * *cd - pointer to core data
  6197. * *hw_version - pointer to store the hardware version
  6198. ******************************************************************************/
  6199. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6200. {
  6201. int rc = 0;
  6202. if (!hw_version)
  6203. return -ENOMEM;
  6204. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6205. rc = _legacy_generate_hw_version(cd, hw_version);
  6206. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6207. rc = _pip2_generate_hw_version(cd, hw_version);
  6208. else {
  6209. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6210. rc = -ENODATA;
  6211. }
  6212. return rc;
  6213. }
  6214. /*******************************************************************************
  6215. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6216. *
  6217. * NOTE: The calling function must ensure to free *hw_version
  6218. *
  6219. * RETURN:
  6220. * 0 = success
  6221. * !0 = Failure
  6222. *
  6223. * PARAMETERS:
  6224. * *dev - pointer to device structure
  6225. * *hw_version - pointer to where the hw_version string will be stored
  6226. ******************************************************************************/
  6227. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6228. {
  6229. int rc = 0;
  6230. u16 actual_read_len;
  6231. u16 pip_ver;
  6232. u8 rd_buf[256];
  6233. u8 status;
  6234. u8 index = PIP2_RESP_STATUS_OFFSET;
  6235. u8 return_data[8];
  6236. u8 panel_id;
  6237. struct pt_core_data *cd = dev_get_drvdata(dev);
  6238. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6239. if (!hw_version)
  6240. return -ENOMEM;
  6241. if (!cd->hw_detected) {
  6242. /* No HW detected */
  6243. rc = -ENODEV;
  6244. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6245. __func__);
  6246. goto exit_error;
  6247. }
  6248. /* For Parade TC or TT parts */
  6249. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6250. rc = _pt_request_pip2_send_cmd(dev,
  6251. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6252. NULL, 0, rd_buf, &actual_read_len);
  6253. if (rc) {
  6254. pt_debug(dev, DL_ERROR,
  6255. "%s: Failed to send PIP2 VERSION cmd\n",
  6256. __func__);
  6257. goto exit_error;
  6258. }
  6259. status = rd_buf[index];
  6260. if (status == 0) {
  6261. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6262. /*
  6263. * BL PIP 2.02 and greater the version fields are
  6264. * swapped
  6265. */
  6266. if (pip_ver >= 0x0202) {
  6267. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6268. "%02X%02X.%02X%02X.FF",
  6269. rd_buf[index + 10], rd_buf[index + 9],
  6270. rd_buf[index + 8], rd_buf[index + 7]);
  6271. } else {
  6272. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6273. "%02X%02X.%02X%02X.FF",
  6274. rd_buf[index + 8], rd_buf[index + 7],
  6275. rd_buf[index + 10], rd_buf[index + 9]);
  6276. }
  6277. return STATUS_SUCCESS;
  6278. } else {
  6279. rc = status;
  6280. pt_debug(dev, DL_WARN,
  6281. "%s: PIP2 VERSION cmd response error\n",
  6282. __func__);
  6283. }
  6284. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6285. /*
  6286. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6287. * hard coded to FFFF
  6288. */
  6289. if (cd->mode == PT_MODE_OPERATIONAL) {
  6290. rc = pt_hid_output_get_sysinfo(cd);
  6291. if (!rc) {
  6292. panel_id =
  6293. cd->sysinfo.sensing_conf_data.panel_id;
  6294. } else {
  6295. panel_id = PANEL_ID_NOT_ENABLED;
  6296. }
  6297. /* In FW - simply retrieve from ttdata struct */
  6298. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6299. "%04X.FFFF.%02X",
  6300. ttdata->jtag_id_h,
  6301. panel_id);
  6302. return STATUS_SUCCESS;
  6303. } else {
  6304. /*
  6305. * Return the stored value if PT_PANEL_ID_BY_BL
  6306. * is not supported while other feature is.
  6307. */
  6308. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6309. rc = pt_hid_output_bl_get_information(
  6310. cd, return_data);
  6311. if (!rc) {
  6312. rc = pt_hid_output_bl_get_panel_id(
  6313. cd, &panel_id);
  6314. }
  6315. } else
  6316. panel_id = cd->pid_for_loader;
  6317. if (!rc) {
  6318. snprintf(hw_version,
  6319. HW_VERSION_LEN_MAX,
  6320. "%02X%02X.FFFF.%02X",
  6321. return_data[3], return_data[2],
  6322. panel_id);
  6323. return STATUS_SUCCESS;
  6324. }
  6325. }
  6326. } else {
  6327. /* Unknown generation */
  6328. rc = -ENODEV;
  6329. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6330. __func__);
  6331. }
  6332. exit_error:
  6333. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6334. return rc;
  6335. }
  6336. /*******************************************************************************
  6337. * FUNCTION: pt_start_wd_timer
  6338. *
  6339. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6340. *
  6341. * RETURN: void
  6342. *
  6343. * PARAMETERS:
  6344. * *cd - pointer to core data
  6345. ******************************************************************************/
  6346. static void pt_start_wd_timer(struct pt_core_data *cd)
  6347. {
  6348. if (cd->watchdog_interval < 100) {
  6349. pt_debug(cd->dev, DL_ERROR,
  6350. "%s: WARNING: Invalid watchdog interval: %d\n",
  6351. __func__, cd->watchdog_interval);
  6352. return;
  6353. }
  6354. if (cd->watchdog_force_stop) {
  6355. pt_debug(cd->dev, DL_INFO,
  6356. "%s: TTDL WD Forced stop\n", __func__);
  6357. return;
  6358. }
  6359. mod_timer(&cd->watchdog_timer, jiffies +
  6360. msecs_to_jiffies(cd->watchdog_interval));
  6361. cd->watchdog_enabled = 1;
  6362. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6363. }
  6364. /*******************************************************************************
  6365. * FUNCTION: pt_stop_wd_timer
  6366. *
  6367. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6368. *
  6369. * RETURN: void
  6370. *
  6371. * PARAMETERS:
  6372. * *cd - pointer to core data
  6373. ******************************************************************************/
  6374. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6375. {
  6376. if (!cd->watchdog_interval)
  6377. return;
  6378. /*
  6379. * Ensure we wait until the watchdog timer
  6380. * running on a different CPU finishes
  6381. */
  6382. del_timer_sync(&cd->watchdog_timer);
  6383. cancel_work_sync(&cd->watchdog_work);
  6384. del_timer_sync(&cd->watchdog_timer);
  6385. cd->watchdog_enabled = 0;
  6386. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6387. }
  6388. /*******************************************************************************
  6389. * FUNCTION: pt_hw_soft_reset
  6390. *
  6391. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6392. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6393. * schedule an enum if it fires when the DUT is being reset.
  6394. * This can cause a double reset.
  6395. *
  6396. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6397. * the WD active could cause this function to fail!
  6398. *
  6399. * RETURN:
  6400. * 0 = success
  6401. * !0 = failure
  6402. *
  6403. * PARAMETERS:
  6404. * *cd - pointer to core data struct
  6405. * protect - flag to call protected or non-protected
  6406. ******************************************************************************/
  6407. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6408. {
  6409. int rc = 0;
  6410. mutex_lock(&cd->system_lock);
  6411. cd->startup_status = STARTUP_STATUS_START;
  6412. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6413. mutex_unlock(&cd->system_lock);
  6414. if (protect)
  6415. rc = pt_hid_cmd_reset(cd);
  6416. else
  6417. rc = pt_hid_cmd_reset_(cd);
  6418. if (rc < 0) {
  6419. pt_debug(cd->dev, DL_ERROR,
  6420. "%s: FAILED to execute SOFT reset\n", __func__);
  6421. return rc;
  6422. }
  6423. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6424. __func__);
  6425. return 0;
  6426. }
  6427. /*******************************************************************************
  6428. * FUNCTION: pt_hw_hard_reset
  6429. *
  6430. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6431. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6432. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6433. * schedule an enum if it fires when the DUT is being reset.
  6434. * This can cause a double reset.
  6435. *
  6436. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6437. * the WD active could cause this function to fail!
  6438. *
  6439. * RETURN:
  6440. * 0 = success
  6441. * !0 = failure
  6442. *
  6443. * PARAMETERS:
  6444. * *cd - pointer to core data struct
  6445. ******************************************************************************/
  6446. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6447. {
  6448. if (cd->cpdata->xres) {
  6449. cd->startup_status = STARTUP_STATUS_START;
  6450. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6451. __func__);
  6452. cd->cpdata->xres(cd->cpdata, cd->dev);
  6453. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6454. __func__);
  6455. return 0;
  6456. }
  6457. pt_debug(cd->dev, DL_ERROR,
  6458. "%s: FAILED to execute HARD reset\n", __func__);
  6459. return -ENODEV;
  6460. }
  6461. /*******************************************************************************
  6462. * FUNCTION: pt_dut_reset
  6463. *
  6464. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6465. * soft reset.
  6466. *
  6467. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6468. * the WD active could cause this function to fail!
  6469. * NOTE: "protect" flag is only used for soft reset.
  6470. *
  6471. * RETURN:
  6472. * 0 = success
  6473. * !0 = failure
  6474. *
  6475. * PARAMETERS:
  6476. * *cd - pointer to core data structure
  6477. * protect - flag to call protected or non-protected
  6478. ******************************************************************************/
  6479. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6480. {
  6481. int rc = 0;
  6482. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6483. mutex_lock(&cd->system_lock);
  6484. cd->hid_reset_cmd_state = 1;
  6485. rc = pt_hw_hard_reset(cd);
  6486. mutex_unlock(&cd->system_lock);
  6487. if (rc == -ENODEV) {
  6488. mutex_lock(&cd->system_lock);
  6489. cd->hid_reset_cmd_state = 0;
  6490. mutex_unlock(&cd->system_lock);
  6491. pt_debug(cd->dev, DL_ERROR,
  6492. "%s: Hard reset failed, try soft reset\n", __func__);
  6493. rc = pt_hw_soft_reset(cd, protect);
  6494. }
  6495. if (rc)
  6496. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6497. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6498. return rc;
  6499. }
  6500. /*******************************************************************************
  6501. * FUNCTION: pt_dut_reset_and_wait
  6502. *
  6503. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6504. * complete
  6505. *
  6506. * RETURN:
  6507. * 0 = success
  6508. * !0 = failure
  6509. *
  6510. * PARAMETERS:
  6511. * *cd - pointer to core data structure
  6512. ******************************************************************************/
  6513. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6514. {
  6515. int rc = 0;
  6516. int t;
  6517. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6518. if (rc < 0)
  6519. goto exit;
  6520. t = wait_event_timeout(cd->wait_q,
  6521. (cd->hid_reset_cmd_state == 0),
  6522. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6523. if (IS_TMO(t)) {
  6524. #ifdef TTDL_DIAGNOSTICS
  6525. cd->bus_transmit_error_count++;
  6526. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6527. #endif /* TTDL_DIAGNOSTICS */
  6528. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6529. __func__);
  6530. rc = -ETIME;
  6531. goto exit;
  6532. }
  6533. exit:
  6534. return rc;
  6535. }
  6536. /*
  6537. * touch default parameters (from report descriptor) to resolve protocol for
  6538. * touch report
  6539. */
  6540. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6541. /* byte offset, size, min, max, bit offset, report */
  6542. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6543. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6544. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6545. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6546. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6547. };
  6548. /*
  6549. * button default parameters (from report descriptor) to resolve protocol for
  6550. * button report
  6551. */
  6552. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6553. /* byte offset, size, min, max, bit offset, report */
  6554. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6555. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6556. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6557. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6558. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6559. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6560. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6561. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6562. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6563. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6564. };
  6565. /*******************************************************************************
  6566. * FUNCTION: pt_init_pip_report_fields
  6567. *
  6568. * SUMMARY: Setup default values for touch/button report parsing.
  6569. *
  6570. * PARAMETERS:
  6571. * *cd - pointer to core data structure
  6572. ******************************************************************************/
  6573. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6574. {
  6575. struct pt_sysinfo *si = &cd->sysinfo;
  6576. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6577. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6578. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6579. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6580. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6581. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6582. cd->features.easywake = 1;
  6583. cd->features.noise_metric = 1;
  6584. cd->features.tracking_heatmap = 1;
  6585. cd->features.sensor_data = 1;
  6586. }
  6587. /*******************************************************************************
  6588. * FUNCTION: pt_get_mode
  6589. *
  6590. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6591. * message
  6592. *
  6593. * RETURN: Enum of the current mode
  6594. *
  6595. * PARAMETERS:
  6596. * *cd - pointer to the Core Data structure
  6597. * protect - run command in protected mode
  6598. * *mode - pointer to store the retrieved mode
  6599. ******************************************************************************/
  6600. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6601. {
  6602. if (desc->packet_id == HID_APP_REPORT_ID)
  6603. return PT_MODE_OPERATIONAL;
  6604. else if (desc->packet_id == HID_BL_REPORT_ID)
  6605. return PT_MODE_BOOTLOADER;
  6606. return PT_MODE_UNKNOWN;
  6607. }
  6608. /*******************************************************************************
  6609. * FUNCTION: _pt_request_get_mode
  6610. *
  6611. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6612. * to determine the current mode of the DUT by use of the Get HID
  6613. * Descriptor command.
  6614. *
  6615. * RETURN:
  6616. * 0 = success
  6617. * !0 = failure
  6618. *
  6619. * PARAMETERS:
  6620. * *dev - pointer to device structure
  6621. * protect - run command in protected mode
  6622. * *mode - pointer to store the retrieved mode
  6623. ******************************************************************************/
  6624. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6625. {
  6626. struct pt_core_data *cd = dev_get_drvdata(dev);
  6627. struct pt_hid_desc hid_desc;
  6628. int rc = 0;
  6629. memset(&hid_desc, 0, sizeof(hid_desc));
  6630. if (protect)
  6631. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6632. else
  6633. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6634. if (rc)
  6635. *mode = PT_MODE_UNKNOWN;
  6636. else
  6637. *mode = pt_get_mode(cd, &hid_desc);
  6638. return rc;
  6639. }
  6640. /*******************************************************************************
  6641. * FUNCTION: pt_queue_enum_
  6642. *
  6643. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6644. * function. It won't try to add/delete sysfs node or modules.
  6645. *
  6646. * RETURN: void
  6647. *
  6648. * PARAMETERS:
  6649. * *cd - pointer to core data
  6650. ******************************************************************************/
  6651. static void pt_queue_enum_(struct pt_core_data *cd)
  6652. {
  6653. if (cd->startup_state == STARTUP_NONE) {
  6654. cd->startup_state = STARTUP_QUEUED;
  6655. #ifdef TTDL_DIAGNOSTICS
  6656. if (!cd->bridge_mode)
  6657. schedule_work(&cd->enum_work);
  6658. else
  6659. cd->startup_state = STARTUP_NONE;
  6660. #else
  6661. schedule_work(&cd->enum_work);
  6662. #endif
  6663. pt_debug(cd->dev, DL_INFO,
  6664. "%s: enum_work queued\n", __func__);
  6665. } else {
  6666. pt_debug(cd->dev, DL_WARN,
  6667. "%s: Enum not queued - startup_state = %d\n",
  6668. __func__, cd->startup_state);
  6669. }
  6670. }
  6671. /*******************************************************************************
  6672. * FUNCTION: pt_queue_enum
  6673. *
  6674. * SUMMARY: Queues a TTDL enum within a mutex lock
  6675. *
  6676. * RETURN: void
  6677. *
  6678. * PARAMETERS:
  6679. * *cd - pointer to core data
  6680. ******************************************************************************/
  6681. static void pt_queue_enum(struct pt_core_data *cd)
  6682. {
  6683. mutex_lock(&cd->system_lock);
  6684. pt_queue_enum_(cd);
  6685. mutex_unlock(&cd->system_lock);
  6686. }
  6687. static void remove_sysfs_and_modules(struct device *dev);
  6688. /*******************************************************************************
  6689. * FUNCTION: pt_queue_restart
  6690. *
  6691. * SUMMARY: Queues a TTDL restart within a mutex lock
  6692. *
  6693. * RETURN: void
  6694. *
  6695. * PARAMETERS:
  6696. * *cd - pointer to core data
  6697. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6698. * False: will not perform remove action
  6699. ******************************************************************************/
  6700. static void pt_queue_restart(struct pt_core_data *cd)
  6701. {
  6702. mutex_lock(&cd->system_lock);
  6703. if (cd->startup_state == STARTUP_NONE) {
  6704. cd->startup_state = STARTUP_QUEUED;
  6705. schedule_work(&cd->ttdl_restart_work);
  6706. pt_debug(cd->dev, DL_INFO,
  6707. "%s: pt_ttdl_restart queued\n", __func__);
  6708. } else {
  6709. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6710. __func__, cd->startup_state);
  6711. }
  6712. mutex_unlock(&cd->system_lock);
  6713. }
  6714. /*******************************************************************************
  6715. * FUNCTION: call_atten_cb
  6716. *
  6717. * SUMMARY: Iterate over attention list call the function that registered.
  6718. *
  6719. * RETURN: void
  6720. *
  6721. * PARAMETERS:
  6722. * *cd - pointer to core data
  6723. * type - type of attention list
  6724. * mode - condition for execution
  6725. ******************************************************************************/
  6726. static void call_atten_cb(struct pt_core_data *cd,
  6727. enum pt_atten_type type, int mode)
  6728. {
  6729. struct atten_node *atten, *atten_n;
  6730. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6731. __func__, type, mode);
  6732. spin_lock(&cd->spinlock);
  6733. list_for_each_entry_safe(atten, atten_n,
  6734. &cd->atten_list[type], node) {
  6735. if (!mode || atten->mode & mode) {
  6736. spin_unlock(&cd->spinlock);
  6737. pt_debug(cd->dev, DL_DEBUG,
  6738. "%s: attention for '%s'",
  6739. __func__, dev_name(atten->dev));
  6740. atten->func(atten->dev);
  6741. spin_lock(&cd->spinlock);
  6742. }
  6743. }
  6744. spin_unlock(&cd->spinlock);
  6745. }
  6746. /*******************************************************************************
  6747. * FUNCTION: start_fw_upgrade
  6748. *
  6749. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6750. * start firmware upgrade.
  6751. *
  6752. * RETURN:
  6753. * 0 = success
  6754. *
  6755. * PARAMETERS:
  6756. * *data - pointer to core data
  6757. ******************************************************************************/
  6758. static int start_fw_upgrade(void *data)
  6759. {
  6760. struct pt_core_data *cd = (struct pt_core_data *)data;
  6761. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6762. return 0;
  6763. }
  6764. /*******************************************************************************
  6765. * FUNCTION: pt_put_device_into_easy_wakeup_
  6766. *
  6767. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6768. * wake up state.
  6769. *
  6770. * RETURN:
  6771. * 0 = success
  6772. * !0 = failure
  6773. *
  6774. * PARAMETERS:
  6775. * *cd - pointer to core data
  6776. ******************************************************************************/
  6777. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6778. {
  6779. int rc = 0;
  6780. u8 status = 0;
  6781. mutex_lock(&cd->system_lock);
  6782. cd->wait_until_wake = 0;
  6783. mutex_unlock(&cd->system_lock);
  6784. rc = pt_hid_output_enter_easywake_state_(cd,
  6785. cd->easy_wakeup_gesture, &status);
  6786. if (rc || status == 0)
  6787. return -EBUSY;
  6788. return rc;
  6789. }
  6790. /*******************************************************************************
  6791. * FUNCTION: pt_put_device_into_deep_sleep_
  6792. *
  6793. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6794. *
  6795. * RETURN:
  6796. * 0 = success
  6797. * !0 = error
  6798. *
  6799. * PARAMETERS:
  6800. * *cd - pointer to core data
  6801. ******************************************************************************/
  6802. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6803. {
  6804. int rc = 0;
  6805. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6806. if (rc)
  6807. rc = -EBUSY;
  6808. return rc;
  6809. }
  6810. /*******************************************************************************
  6811. * FUNCTION: pt_put_device_into_deep_standby_
  6812. *
  6813. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6814. *
  6815. * RETURN:
  6816. * 0 = success
  6817. * !0 = error
  6818. *
  6819. * PARAMETERS:
  6820. * *cd - pointer to core data
  6821. ******************************************************************************/
  6822. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6823. {
  6824. int rc = 0;
  6825. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6826. if (rc)
  6827. rc = -EBUSY;
  6828. return rc;
  6829. }
  6830. /*******************************************************************************
  6831. * FUNCTION: pt_core_poweroff_device_
  6832. *
  6833. * SUMMARY: Disable IRQ and HW power down the device.
  6834. *
  6835. * RETURN:
  6836. * 0 = success
  6837. * !0 = failure
  6838. *
  6839. * PARAMETERS:
  6840. * *cd - pointer to core data
  6841. ******************************************************************************/
  6842. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6843. {
  6844. int rc;
  6845. if (cd->irq_enabled) {
  6846. cd->irq_enabled = false;
  6847. disable_irq_nosync(cd->irq);
  6848. }
  6849. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6850. if (rc < 0)
  6851. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6852. __func__, rc);
  6853. return rc;
  6854. }
  6855. /*******************************************************************************
  6856. * FUNCTION: pt_core_sleep_
  6857. *
  6858. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6859. * configuration in the core platform data structure.
  6860. *
  6861. * RETURN:
  6862. * 0 = success
  6863. * !0 = failure
  6864. *
  6865. * PARAMETERS:
  6866. * *cd - pointer to core data
  6867. ******************************************************************************/
  6868. static int pt_core_sleep_(struct pt_core_data *cd)
  6869. {
  6870. int rc = 0;
  6871. mutex_lock(&cd->system_lock);
  6872. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6873. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_SLEEP_NONE) {
  6874. cd->sleep_state = SS_SLEEPING;
  6875. } else {
  6876. mutex_unlock(&cd->system_lock);
  6877. pt_debug(cd->dev, DL_INFO,
  6878. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6879. return 0;
  6880. }
  6881. mutex_unlock(&cd->system_lock);
  6882. /* Ensure watchdog and startup works stopped */
  6883. pt_stop_wd_timer(cd);
  6884. cancel_work_sync(&cd->enum_work);
  6885. pt_stop_wd_timer(cd);
  6886. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6887. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6888. rc = pt_core_poweroff_device_(cd);
  6889. if (rc)
  6890. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6891. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6892. pt_debug(cd->dev, DL_INFO,
  6893. "%s: Entering into deep standby mode:\n", __func__);
  6894. rc = pt_put_device_into_deep_standby_(cd);
  6895. if (rc)
  6896. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6897. } else {
  6898. pt_debug(cd->dev, DL_INFO,
  6899. "%s: Entering into deep sleep mode:\n", __func__);
  6900. rc = pt_put_device_into_deep_sleep_(cd);
  6901. if (rc)
  6902. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6903. }
  6904. mutex_lock(&cd->system_lock);
  6905. cd->sleep_state = SS_SLEEP_ON;
  6906. mutex_unlock(&cd->system_lock);
  6907. return rc;
  6908. }
  6909. /*******************************************************************************
  6910. * FUNCTION: pt_core_easywake_on_
  6911. *
  6912. * SUMMARY: Suspend the device with easy wake on the
  6913. * configuration in the core platform data structure.
  6914. *
  6915. * RETURN:
  6916. * 0 = success
  6917. * !0 = failure
  6918. *
  6919. * PARAMETERS:
  6920. * *cd - pointer to core data
  6921. ******************************************************************************/
  6922. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6923. {
  6924. int rc = 0;
  6925. mutex_lock(&cd->system_lock);
  6926. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6927. mutex_unlock(&cd->system_lock);
  6928. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6929. __func__, cd->sleep_state);
  6930. return 0;
  6931. }
  6932. mutex_unlock(&cd->system_lock);
  6933. /* Ensure watchdog and startup works stopped */
  6934. pt_stop_wd_timer(cd);
  6935. cancel_work_sync(&cd->enum_work);
  6936. pt_stop_wd_timer(cd);
  6937. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6938. rc = pt_put_device_into_easy_wakeup_(cd);
  6939. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6940. if (rc)
  6941. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6942. }
  6943. mutex_lock(&cd->system_lock);
  6944. cd->sleep_state = SS_EASY_WAKING_ON;
  6945. mutex_unlock(&cd->system_lock);
  6946. return rc;
  6947. }
  6948. /*******************************************************************************
  6949. * FUNCTION: pt_core_easywake_on
  6950. *
  6951. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6952. *
  6953. * RETURN:
  6954. * 0 = success
  6955. * !0 = failure
  6956. *
  6957. * PARAMETERS:
  6958. * *cd - pointer to core data
  6959. ******************************************************************************/
  6960. static int pt_core_easywake_on(struct pt_core_data *cd)
  6961. {
  6962. int rc = 0;
  6963. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6964. if (rc < 0) {
  6965. pt_debug(cd->dev, DL_ERROR,
  6966. "%s: fail get exclusive ex=%p own=%p\n",
  6967. __func__, cd->exclusive_dev, cd->dev);
  6968. return rc;
  6969. }
  6970. rc = pt_core_easywake_on_(cd);
  6971. if (release_exclusive(cd, cd->dev) < 0)
  6972. pt_debug(cd->dev, DL_ERROR,
  6973. "%s: fail to release exclusive\n", __func__);
  6974. else
  6975. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6976. __func__);
  6977. return rc;
  6978. }
  6979. /*******************************************************************************
  6980. * FUNCTION: pt_core_sleep
  6981. *
  6982. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6983. *
  6984. * RETURN:
  6985. * 0 = success
  6986. * !0 = failure
  6987. *
  6988. * PARAMETERS:
  6989. * *cd - pointer to core data
  6990. ******************************************************************************/
  6991. static int pt_core_sleep(struct pt_core_data *cd)
  6992. {
  6993. int rc = 0;
  6994. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6995. if (rc < 0) {
  6996. pt_debug(cd->dev, DL_ERROR,
  6997. "%s: fail get exclusive ex=%p own=%p\n",
  6998. __func__, cd->exclusive_dev, cd->dev);
  6999. return rc;
  7000. }
  7001. rc = pt_core_sleep_(cd);
  7002. if (release_exclusive(cd, cd->dev) < 0)
  7003. pt_debug(cd->dev, DL_ERROR,
  7004. "%s: fail to release exclusive\n", __func__);
  7005. else
  7006. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7007. __func__);
  7008. return rc;
  7009. }
  7010. /*******************************************************************************
  7011. * FUNCTION: pt_wakeup_host
  7012. *
  7013. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7014. *
  7015. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7016. *
  7017. * RETURN:
  7018. * 0 = success
  7019. * !0 = failure
  7020. *
  7021. * PARAMETERS:
  7022. * *cd - pointer to core data
  7023. ******************************************************************************/
  7024. static int pt_wakeup_host(struct pt_core_data *cd)
  7025. {
  7026. #ifndef EASYWAKE_TSG6
  7027. /* TSG5 EasyWake */
  7028. int rc = 0;
  7029. int event_id;
  7030. int size = get_unaligned_le16(&cd->input_buf[0]);
  7031. /* Validate report */
  7032. if (size != 4 || cd->input_buf[2] != 4)
  7033. rc = -EINVAL;
  7034. event_id = cd->input_buf[3];
  7035. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7036. __func__, event_id, rc);
  7037. if (rc) {
  7038. pt_core_sleep_(cd);
  7039. goto exit;
  7040. }
  7041. /* attention WAKE */
  7042. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7043. exit:
  7044. return rc;
  7045. #else
  7046. /* TSG6 FW1.3 EasyWake */
  7047. int rc = 0;
  7048. int i = 0;
  7049. int report_length;
  7050. /* Validate report */
  7051. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7052. rc = -EINVAL;
  7053. pt_core_sleep_(cd);
  7054. goto exit;
  7055. }
  7056. /* Get gesture id and gesture data length */
  7057. cd->gesture_id = cd->input_buf[3];
  7058. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7059. cd->gesture_data_length = report_length - 4;
  7060. pt_debug(cd->dev, DL_INFO,
  7061. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7062. __func__, cd->gesture_id, cd->gesture_data_length);
  7063. for (i = 0; i < cd->gesture_data_length; i++)
  7064. cd->gesture_data[i] = cd->input_buf[4 + i];
  7065. /* attention WAKE */
  7066. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7067. exit:
  7068. return rc;
  7069. #endif
  7070. }
  7071. /*******************************************************************************
  7072. * FUNCTION: pt_get_touch_axis
  7073. *
  7074. * SUMMARY: Function to calculate touch axis
  7075. *
  7076. * PARAMETERS:
  7077. * *cd - pointer to core data structure
  7078. * *axis - pointer to axis calculation result
  7079. * size - size in bytes
  7080. * max - max value of result
  7081. * *xy_data - pointer to input data to be parsed
  7082. * bofs - bit offset
  7083. ******************************************************************************/
  7084. static void pt_get_touch_axis(struct pt_core_data *cd,
  7085. int *axis, int size, int max, u8 *data, int bofs)
  7086. {
  7087. int nbyte;
  7088. int next;
  7089. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7090. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7091. next++;
  7092. }
  7093. *axis &= max - 1;
  7094. }
  7095. /*******************************************************************************
  7096. * FUNCTION: move_tracking_heatmap_data
  7097. *
  7098. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7099. * system information structure, xy_mode and xy_data.
  7100. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7101. * the tthe_tuner sysfs node under the label "THM"
  7102. *
  7103. * RETURN:
  7104. * 0 = success
  7105. * !0 = failure
  7106. *
  7107. * PARAMETERS:
  7108. * *cd - pointer to core data
  7109. * *si - pointer to the system information structure
  7110. ******************************************************************************/
  7111. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7112. struct pt_sysinfo *si)
  7113. {
  7114. #ifdef TTHE_TUNER_SUPPORT
  7115. int size = get_unaligned_le16(&cd->input_buf[0]);
  7116. if (size)
  7117. tthe_print(cd, cd->input_buf, size, "THM=");
  7118. #endif
  7119. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7120. return 0;
  7121. }
  7122. /*******************************************************************************
  7123. * FUNCTION: move_sensor_data
  7124. *
  7125. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7126. * information structure, xy_mode and xy_data.
  7127. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7128. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7129. *
  7130. * RETURN:
  7131. * 0 = success
  7132. * !0 = failure
  7133. *
  7134. * PARAMETERS:
  7135. * *cd - pointer to core data
  7136. * *si - pointer to the system information structure
  7137. ******************************************************************************/
  7138. static int move_sensor_data(struct pt_core_data *cd,
  7139. struct pt_sysinfo *si)
  7140. {
  7141. #ifdef TTHE_TUNER_SUPPORT
  7142. int size = get_unaligned_le16(&cd->input_buf[0]);
  7143. if (size)
  7144. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7145. #endif
  7146. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7147. return 0;
  7148. }
  7149. /*******************************************************************************
  7150. * FUNCTION: move_button_data
  7151. *
  7152. * SUMMARY: Move the valid button data from the input buffer into the system
  7153. * information structure, xy_mode and xy_data.
  7154. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7155. * the tthe_tuner sysfs node under the label "OpModeData"
  7156. *
  7157. * RETURN:
  7158. * 0 = success
  7159. * !0 = failure
  7160. *
  7161. * PARAMETERS:
  7162. * *cd - pointer to core data
  7163. * *si - pointer to the system information structure
  7164. ******************************************************************************/
  7165. static int move_button_data(struct pt_core_data *cd,
  7166. struct pt_sysinfo *si)
  7167. {
  7168. #ifdef TTHE_TUNER_SUPPORT
  7169. int size = get_unaligned_le16(&cd->input_buf[0]);
  7170. if (size)
  7171. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7172. #endif
  7173. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7174. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7175. "xy_mode");
  7176. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7177. BTN_REPORT_SIZE);
  7178. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7179. "xy_data");
  7180. return 0;
  7181. }
  7182. /*******************************************************************************
  7183. * FUNCTION: move_touch_data
  7184. *
  7185. * SUMMARY: Move the valid touch data from the input buffer into the system
  7186. * information structure, xy_mode and xy_data.
  7187. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7188. * the tthe_tuner sysfs node under the label "OpModeData"
  7189. *
  7190. * RETURN:
  7191. * 0 = success
  7192. * !0 = failure
  7193. *
  7194. * PARAMETERS:
  7195. * *cd - pointer to core data
  7196. * *si - pointer to the system information structure
  7197. ******************************************************************************/
  7198. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7199. {
  7200. int max_tch = si->sensing_conf_data.max_tch;
  7201. int num_cur_tch;
  7202. int length;
  7203. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7204. #ifdef TTHE_TUNER_SUPPORT
  7205. int size = get_unaligned_le16(&cd->input_buf[0]);
  7206. if (size)
  7207. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7208. #endif
  7209. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7210. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7211. si->desc.tch_header_size, "xy_mode");
  7212. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7213. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7214. if (unlikely(num_cur_tch > max_tch))
  7215. num_cur_tch = max_tch;
  7216. length = num_cur_tch * si->desc.tch_record_size;
  7217. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7218. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7219. return 0;
  7220. }
  7221. /*******************************************************************************
  7222. * FUNCTION: move_hid_pen_data
  7223. *
  7224. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7225. * information structure, xy_mode and xy_data.
  7226. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7227. * the tthe_tuner sysfs node under the label "HID" starting with the
  7228. * report ID.
  7229. *
  7230. * RETURN:
  7231. * 0 = success
  7232. * !0 = failure
  7233. *
  7234. * PARAMETERS:
  7235. * *cd - pointer to core data
  7236. * *si - pointer to the system information structure
  7237. ******************************************************************************/
  7238. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7239. {
  7240. #ifdef TTHE_TUNER_SUPPORT
  7241. int size = get_unaligned_le16(&cd->input_buf[0]);
  7242. if (size) {
  7243. /*
  7244. * HID over USB does not require the two byte length field, so
  7245. * this should print from input_buf[2] but to keep both finger
  7246. * and pen reports the same the length is included
  7247. */
  7248. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7249. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7250. "HID-USB=");
  7251. else
  7252. tthe_print(cd, &(cd->input_buf[0]), size,
  7253. "HID-I2C=");
  7254. }
  7255. #endif
  7256. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7257. return 0;
  7258. }
  7259. /*******************************************************************************
  7260. * FUNCTION: parse_touch_input
  7261. *
  7262. * SUMMARY: Parse the touch report and take action based on the touch
  7263. * report_id.
  7264. *
  7265. * RETURN:
  7266. * 0 = success
  7267. * !0 = failure
  7268. *
  7269. * PARAMETERS:
  7270. * *cd - pointer to core data
  7271. * size - size of touch record
  7272. ******************************************************************************/
  7273. static int parse_touch_input(struct pt_core_data *cd, int size)
  7274. {
  7275. struct pt_sysinfo *si = &cd->sysinfo;
  7276. int report_id = cd->input_buf[2];
  7277. int rc = -EINVAL;
  7278. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7279. __func__);
  7280. if (!si->ready) {
  7281. pt_debug(cd->dev, DL_ERROR,
  7282. "%s: Need system information to parse touches\n",
  7283. __func__);
  7284. return 0;
  7285. }
  7286. if (!si->xy_mode || !si->xy_data)
  7287. return rc;
  7288. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7289. rc = move_touch_data(cd, si);
  7290. else if (report_id == PT_HID_PEN_REPORT_ID)
  7291. rc = move_hid_pen_data(cd, si);
  7292. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7293. rc = move_button_data(cd, si);
  7294. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7295. rc = move_sensor_data(cd, si);
  7296. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7297. rc = move_tracking_heatmap_data(cd, si);
  7298. if (rc)
  7299. return rc;
  7300. /* attention IRQ */
  7301. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7302. return 0;
  7303. }
  7304. /*******************************************************************************
  7305. * FUNCTION: parse_command_input
  7306. *
  7307. * SUMMARY: Move the response data from the input buffer to the response buffer
  7308. *
  7309. * RETURN:
  7310. * 0 = success
  7311. * !0 = failure
  7312. *
  7313. * PARAMETERS:
  7314. * *cd - pointer to core data
  7315. * size - size of response data
  7316. ******************************************************************************/
  7317. static int parse_command_input(struct pt_core_data *cd, int size)
  7318. {
  7319. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7320. __func__);
  7321. memcpy(cd->response_buf, cd->input_buf, size);
  7322. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7323. if (size && cd->show_tt_data) {
  7324. if (cd->pip2_prot_active)
  7325. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7326. else
  7327. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7328. }
  7329. #endif
  7330. mutex_lock(&cd->system_lock);
  7331. cd->hid_cmd_state = 0;
  7332. mutex_unlock(&cd->system_lock);
  7333. wake_up(&cd->wait_q);
  7334. return 0;
  7335. }
  7336. /*******************************************************************************
  7337. * FUNCTION: pt_allow_enumeration
  7338. *
  7339. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7340. * FW sentinel is seen.
  7341. *
  7342. * RETURN:
  7343. * true = allow enumeration or fully re-probe
  7344. * false = skip enumeration and fully re-probe
  7345. *
  7346. * PARAMETERS:
  7347. * *cd - pointer to core data
  7348. ******************************************************************************/
  7349. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7350. {
  7351. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7352. (!cd->hid_reset_cmd_state) &&
  7353. (cd->core_probe_complete) &&
  7354. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7355. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7356. (cd->mode == PT_MODE_OPERATIONAL)) {
  7357. return true;
  7358. }
  7359. if ((!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->active_dut_generation != DUT_PIP1_ONLY)) {
  7364. return true;
  7365. }
  7366. pt_debug(cd->dev, DL_INFO,
  7367. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7368. __func__,
  7369. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7370. "core_probe_complete(1)", cd->core_probe_complete,
  7371. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7372. "active_dut_gen(0,2)", cd->active_dut_generation);
  7373. return false;
  7374. }
  7375. /*******************************************************************************
  7376. * FUNCTION: pt_is_touch_report
  7377. *
  7378. * SUMMARY: Determine if a report ID should be treated as a touch report
  7379. *
  7380. * RETURN:
  7381. * true = report ID is a touch report
  7382. * false = report ID is not a touch report
  7383. *
  7384. * PARAMETERS:
  7385. * id - Report ID
  7386. ******************************************************************************/
  7387. static bool pt_is_touch_report(int id)
  7388. {
  7389. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7390. id == PT_HID_PEN_REPORT_ID ||
  7391. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7392. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7393. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7394. return true;
  7395. else
  7396. return false;
  7397. }
  7398. /*******************************************************************************
  7399. * FUNCTION: pt_parse_input
  7400. *
  7401. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7402. * on if its a response to a command or asynchronous touch data / reset
  7403. * sentinel. PIP2.x messages have additional error checking that is
  7404. * parsed (SEQ match from cmd to rsp, CRC valid).
  7405. * Look for special packets based on unique lengths:
  7406. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7407. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7408. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7409. * 0xFFXX - Empty buffer (PIP 1.7+)
  7410. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7411. * the initial probe has not yet been done.
  7412. *
  7413. * RETURN:
  7414. * 0 = success
  7415. * !0 = failure
  7416. *
  7417. * PARAMETERS:
  7418. * *cd - pointer to core data
  7419. ******************************************************************************/
  7420. static int pt_parse_input(struct pt_core_data *cd)
  7421. {
  7422. int report_id;
  7423. int cmd_id;
  7424. int is_command = 0;
  7425. int size;
  7426. int print_size;
  7427. bool touch_report = true;
  7428. unsigned short calc_crc;
  7429. unsigned short resp_crc;
  7430. cd->fw_sys_mode_in_standby_state = false;
  7431. size = get_unaligned_le16(&cd->input_buf[0]);
  7432. print_size = size;
  7433. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7434. __func__, print_size);
  7435. if (print_size <= PT_MAX_INPUT)
  7436. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7437. "<<< Read buf");
  7438. if (size == 0 ||
  7439. (size == 11 &&
  7440. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7441. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7442. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7443. PIP2_CMD_COMMAND_ID_MASK) ==
  7444. PIP2_CMD_ID_STATUS)) {
  7445. touch_report = false;
  7446. cd->hw_detected = true;
  7447. cd->bl_pip_ver_ready = false;
  7448. cd->app_pip_ver_ready = false;
  7449. if (size == 0) {
  7450. mutex_lock(&cd->system_lock);
  7451. cd->pip2_prot_active = false;
  7452. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7453. /*
  7454. * For Gen5/6 this sentinel could be from
  7455. * the BL or FW. Attempt to set the correct
  7456. * mode based on the previous PIP command.
  7457. */
  7458. if (cd->hid_cmd_state ==
  7459. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7460. cd->mode = PT_MODE_OPERATIONAL;
  7461. cd->startup_status =
  7462. STARTUP_STATUS_FW_RESET_SENTINEL;
  7463. } else if (cd->hid_cmd_state ==
  7464. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7465. cd->hid_reset_cmd_state) {
  7466. cd->mode = PT_MODE_BOOTLOADER;
  7467. cd->startup_status =
  7468. STARTUP_STATUS_BL_RESET_SENTINEL;
  7469. } else {
  7470. cd->mode = PT_MODE_UNKNOWN;
  7471. cd->startup_status =
  7472. STARTUP_STATUS_START;
  7473. }
  7474. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7475. pt_debug(cd->dev, DL_INFO,
  7476. "%s: ATM Gen5/6 %s sentinel received\n",
  7477. __func__,
  7478. (cd->mode == PT_MODE_OPERATIONAL ?
  7479. "FW" :
  7480. (cd->mode == PT_MODE_BOOTLOADER ?
  7481. "BL" : "Unknown")));
  7482. } else {
  7483. cd->mode = PT_MODE_OPERATIONAL;
  7484. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7485. cd->startup_status =
  7486. STARTUP_STATUS_FW_RESET_SENTINEL;
  7487. pt_debug(cd->dev, DL_INFO,
  7488. "%s: ATM PT/TT FW sentinel received\n",
  7489. __func__);
  7490. }
  7491. mutex_unlock(&cd->system_lock);
  7492. if (pt_allow_enumeration(cd)) {
  7493. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7494. pt_debug(cd->dev, DL_INFO,
  7495. "%s: Queue Restart\n", __func__);
  7496. pt_queue_restart(cd);
  7497. } else {
  7498. pt_debug(cd->dev, DL_INFO,
  7499. "%s: Queue Enum\n", __func__);
  7500. pt_queue_enum(cd);
  7501. }
  7502. } else {
  7503. pt_debug(cd->dev, DL_INFO,
  7504. "%s: Sentinel - No Queued Action\n",
  7505. __func__);
  7506. }
  7507. } else {
  7508. /* Sentinel must be from TT/TC BL */
  7509. mutex_lock(&cd->system_lock);
  7510. cd->pip2_prot_active = true;
  7511. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7512. cd->mode = PT_MODE_BOOTLOADER;
  7513. cd->sysinfo.ready = false;
  7514. mutex_unlock(&cd->system_lock);
  7515. pt_debug(cd->dev, DL_INFO,
  7516. "%s: BL Reset sentinel received\n", __func__);
  7517. if (cd->flashless_dut &&
  7518. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7519. pt_debug(cd->dev, DL_INFO,
  7520. "%s: BL to RAM for flashless DUT\n",
  7521. __func__);
  7522. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7523. }
  7524. }
  7525. mutex_lock(&cd->system_lock);
  7526. memcpy(cd->response_buf, cd->input_buf, 2);
  7527. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7528. mutex_unlock(&cd->system_lock);
  7529. pt_debug(cd->dev, DL_WARN,
  7530. "%s: Device Initiated Reset\n", __func__);
  7531. wake_up(&cd->wait_q);
  7532. return 0;
  7533. }
  7534. cd->hid_reset_cmd_state = 0;
  7535. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7536. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7537. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7538. cd->hid_cmd_state = 0;
  7539. wake_up(&cd->wait_q);
  7540. mutex_unlock(&cd->system_lock);
  7541. return 0;
  7542. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7543. /*
  7544. * This debug message below is used by PBATS to calculate the
  7545. * time from the last lift off IRQ to when FW exits LFT mode.
  7546. */
  7547. touch_report = false;
  7548. pt_debug(cd->dev, DL_WARN,
  7549. "%s: DUT - Empty buffer detected\n", __func__);
  7550. return 0;
  7551. } else if (size > PT_MAX_INPUT) {
  7552. pt_debug(cd->dev, DL_ERROR,
  7553. "%s: DUT - Unexpected len field in active bus data!\n",
  7554. __func__);
  7555. return -EINVAL;
  7556. }
  7557. if (cd->pip2_prot_active) {
  7558. pt_debug(cd->dev, DL_DEBUG,
  7559. "%s: Decode PIP2.x Response\n", __func__);
  7560. /* PIP2 does not have a report id, hard code it */
  7561. report_id = 0x00;
  7562. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7563. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7564. resp_crc = cd->input_buf[size - 2] << 8;
  7565. resp_crc |= cd->input_buf[size - 1];
  7566. if ((cd->pip2_cmd_tag_seq !=
  7567. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7568. (resp_crc != calc_crc) &&
  7569. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7570. == PT_PIP_TOUCH_REPORT_ID) ||
  7571. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7572. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7573. pt_debug(cd->dev, DL_WARN,
  7574. "%s: %s %d %s\n",
  7575. __func__,
  7576. "Received PIP1 report id =",
  7577. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7578. "when expecting a PIP2 report - IGNORE report");
  7579. return 0;
  7580. }
  7581. is_command = 1;
  7582. touch_report = false;
  7583. } else {
  7584. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7585. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7586. }
  7587. #ifdef TTDL_DIAGNOSTICS
  7588. pt_debug(cd->dev, DL_DEBUG,
  7589. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7590. __func__, cd->pip2_prot_active, report_id,
  7591. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7592. #endif /* TTDL_DIAGNOSTICS */
  7593. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7594. pt_wakeup_host(cd);
  7595. #ifdef TTHE_TUNER_SUPPORT
  7596. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7597. #endif
  7598. return 0;
  7599. }
  7600. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7601. msecs_to_jiffies(cd->watchdog_interval));
  7602. /*
  7603. * If it is PIP2 response, the report_id has been set to 0,
  7604. * so it will not be parsed as a touch packet.
  7605. */
  7606. if (!pt_is_touch_report(report_id)) {
  7607. is_command = 1;
  7608. touch_report = false;
  7609. }
  7610. if (unlikely(is_command)) {
  7611. parse_command_input(cd, size);
  7612. return 0;
  7613. }
  7614. if (touch_report)
  7615. parse_touch_input(cd, size);
  7616. return 0;
  7617. }
  7618. /*******************************************************************************
  7619. * FUNCTION: pt_read_input
  7620. *
  7621. * SUMMARY: Reads incoming data off of the active bus
  7622. *
  7623. * RETURN:
  7624. * 0 = success
  7625. * !0 = failure
  7626. *
  7627. * PARAMETERS:
  7628. * *cd - pointer to core data
  7629. ******************************************************************************/
  7630. static int pt_read_input(struct pt_core_data *cd)
  7631. {
  7632. struct device *dev = cd->dev;
  7633. int rc = 0;
  7634. int t;
  7635. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7636. /*
  7637. * Workaround for easywake failure
  7638. * Interrupt for easywake, wait for bus controller to wake
  7639. */
  7640. mutex_lock(&cd->system_lock);
  7641. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7642. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7643. mutex_unlock(&cd->system_lock);
  7644. if (!dev->power.is_suspended)
  7645. goto read;
  7646. t = wait_event_timeout(cd->wait_q,
  7647. (cd->wait_until_wake == 1),
  7648. msecs_to_jiffies(2000));
  7649. #ifdef TTDL_DIAGNOSTICS
  7650. if (IS_TMO(t)) {
  7651. cd->bus_transmit_error_count++;
  7652. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7653. pt_debug(dev, DL_ERROR,
  7654. "%s: !!!I2C Transmission Error %d\n",
  7655. __func__,
  7656. cd->bus_transmit_error_count);
  7657. }
  7658. #else
  7659. if (IS_TMO(t))
  7660. pt_queue_enum(cd);
  7661. #endif /* TTDL_DIAGNOSTICS */
  7662. goto read;
  7663. }
  7664. }
  7665. mutex_unlock(&cd->system_lock);
  7666. read:
  7667. /* Try reading up to 'retry' times */
  7668. while (retry-- != 0) {
  7669. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7670. PT_MAX_INPUT);
  7671. if (!rc) {
  7672. pt_debug(dev, DL_DEBUG,
  7673. "%s: Read input successfully\n", __func__);
  7674. goto read_exit;
  7675. }
  7676. usleep_range(1000, 2000);
  7677. }
  7678. pt_debug(dev, DL_ERROR,
  7679. "%s: Error getting report, rc=%d\n", __func__, rc);
  7680. read_exit:
  7681. return rc;
  7682. }
  7683. /*******************************************************************************
  7684. * FUNCTION: pt_irq
  7685. *
  7686. * SUMMARY: Process all detected interrupts
  7687. *
  7688. * RETURN:
  7689. * IRQ_HANDLED - Finished processing the interrupt
  7690. *
  7691. * PARAMETERS:
  7692. * irq - IRQ number
  7693. * *handle - pointer to core data struct
  7694. ******************************************************************************/
  7695. irqreturn_t pt_irq(int irq, void *handle)
  7696. {
  7697. struct pt_core_data *cd = handle;
  7698. int rc = 0;
  7699. if (!pt_check_irq_asserted(cd))
  7700. return IRQ_HANDLED;
  7701. rc = pt_read_input(cd);
  7702. #ifdef TTDL_DIAGNOSTICS
  7703. cd->irq_count++;
  7704. /* Used to calculate T-Refresh */
  7705. if (cd->t_refresh_active) {
  7706. if (cd->t_refresh_count == 0) {
  7707. cd->t_refresh_time = jiffies;
  7708. cd->t_refresh_count++;
  7709. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7710. cd->t_refresh_count++;
  7711. } else {
  7712. cd->t_refresh_active = 0;
  7713. cd->t_refresh_time =
  7714. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7715. }
  7716. }
  7717. #endif /* TTDL_DIAGNOSTICS */
  7718. if (!rc)
  7719. pt_parse_input(cd);
  7720. return IRQ_HANDLED;
  7721. }
  7722. /*******************************************************************************
  7723. * FUNCTION: _pt_subscribe_attention
  7724. *
  7725. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7726. * to subscribe themselves into the TTDL attention list
  7727. *
  7728. * RETURN:
  7729. * 0 = success
  7730. * !0 = failure
  7731. *
  7732. * PARAMETERS:
  7733. * *dev - pointer to device structure
  7734. * type - attention type enum
  7735. * *id - ID of the module calling this function
  7736. * *func - callback function pointer to be called when notified
  7737. * mode - attention mode
  7738. ******************************************************************************/
  7739. int _pt_subscribe_attention(struct device *dev,
  7740. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7741. int mode)
  7742. {
  7743. struct pt_core_data *cd = dev_get_drvdata(dev);
  7744. struct atten_node *atten, *atten_new;
  7745. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7746. if (!atten_new)
  7747. return -ENOMEM;
  7748. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7749. dev_name(cd->dev));
  7750. spin_lock(&cd->spinlock);
  7751. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7752. if (atten->id == id && atten->mode == mode) {
  7753. spin_unlock(&cd->spinlock);
  7754. kfree(atten_new);
  7755. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7756. __func__,
  7757. "already subscribed attention",
  7758. dev, "mode", mode);
  7759. return 0;
  7760. }
  7761. }
  7762. atten_new->id = id;
  7763. atten_new->dev = dev;
  7764. atten_new->mode = mode;
  7765. atten_new->func = func;
  7766. list_add(&atten_new->node, &cd->atten_list[type]);
  7767. spin_unlock(&cd->spinlock);
  7768. return 0;
  7769. }
  7770. /*******************************************************************************
  7771. * FUNCTION: _pt_unsubscribe_attention
  7772. *
  7773. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7774. * to unsubscribe themselves from the TTDL attention list
  7775. *
  7776. * RETURN:
  7777. * 0 = success
  7778. * !0 = failure
  7779. *
  7780. * PARAMETERS:
  7781. * *dev - pointer to device structure
  7782. * type - attention type enum
  7783. * *id - ID of the module calling this function
  7784. * *func - function pointer
  7785. * mode - attention mode
  7786. ******************************************************************************/
  7787. int _pt_unsubscribe_attention(struct device *dev,
  7788. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7789. int mode)
  7790. {
  7791. struct pt_core_data *cd = dev_get_drvdata(dev);
  7792. struct atten_node *atten, *atten_n;
  7793. spin_lock(&cd->spinlock);
  7794. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7795. if (atten->id == id && atten->mode == mode) {
  7796. list_del(&atten->node);
  7797. spin_unlock(&cd->spinlock);
  7798. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7799. __func__,
  7800. "unsub for atten->dev", atten->dev,
  7801. "atten->mode", atten->mode);
  7802. kfree(atten);
  7803. return 0;
  7804. }
  7805. }
  7806. spin_unlock(&cd->spinlock);
  7807. return -ENODEV;
  7808. }
  7809. /*******************************************************************************
  7810. * FUNCTION: _pt_request_exclusive
  7811. *
  7812. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7813. * to request exclusive access
  7814. *
  7815. * RETURN:
  7816. * 0 = success
  7817. * !0 = failure
  7818. *
  7819. * PARAMETERS:
  7820. * *dev - pointer to device structure
  7821. * timeout_ms - timeout to wait for exclusive access
  7822. ******************************************************************************/
  7823. static int _pt_request_exclusive(struct device *dev,
  7824. int timeout_ms)
  7825. {
  7826. struct pt_core_data *cd = dev_get_drvdata(dev);
  7827. return request_exclusive(cd, (void *)dev, timeout_ms);
  7828. }
  7829. /*******************************************************************************
  7830. * FUNCTION: _pt_release_exclusive
  7831. *
  7832. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7833. * to release exclusive access
  7834. *
  7835. * RETURN:
  7836. * 0 = success
  7837. * !0 = failure
  7838. *
  7839. * PARAMETERS:
  7840. * *dev - pointer to device structure
  7841. ******************************************************************************/
  7842. static int _pt_release_exclusive(struct device *dev)
  7843. {
  7844. struct pt_core_data *cd = dev_get_drvdata(dev);
  7845. return release_exclusive(cd, (void *)dev);
  7846. }
  7847. /*******************************************************************************
  7848. * FUNCTION: _pt_request_reset
  7849. *
  7850. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7851. * to request the DUT to be reset. Function returns err if refused or
  7852. * timeout occurs (Note: core uses fixed timeout period).
  7853. *
  7854. * NOTE: Function blocks until ISR occurs.
  7855. *
  7856. * RETURN:
  7857. * 0 = success
  7858. * !0 = failure
  7859. *
  7860. * PARAMETERS:
  7861. * *dev - pointer to device structure
  7862. * protect - flag to call protected or non-protected
  7863. ******************************************************************************/
  7864. static int _pt_request_reset(struct device *dev, int protect)
  7865. {
  7866. struct pt_core_data *cd = dev_get_drvdata(dev);
  7867. int rc;
  7868. rc = pt_dut_reset(cd, protect);
  7869. if (rc < 0) {
  7870. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7871. __func__, rc);
  7872. }
  7873. return rc;
  7874. }
  7875. /*******************************************************************************
  7876. * FUNCTION: _pt_request_enum
  7877. *
  7878. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7879. * to request TTDL to queue an enum. This function will return err
  7880. * if refused; if no error then enum has completed and system is in
  7881. * normal operation mode.
  7882. *
  7883. * RETURN:
  7884. * 0 = success
  7885. * !0 = failure
  7886. *
  7887. * PARAMETERS:
  7888. * *dev - pointer to device structure
  7889. * wait - boolean to determine if to wait for startup event
  7890. ******************************************************************************/
  7891. static int _pt_request_enum(struct device *dev, bool wait)
  7892. {
  7893. struct pt_core_data *cd = dev_get_drvdata(dev);
  7894. pt_queue_enum(cd);
  7895. if (wait)
  7896. wait_event_timeout(cd->wait_q,
  7897. cd->startup_state == STARTUP_NONE,
  7898. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7899. return 0;
  7900. }
  7901. /*******************************************************************************
  7902. * FUNCTION: _pt_request_sysinfo
  7903. *
  7904. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7905. * to request the pointer to the system information structure. This
  7906. * function will return NULL if sysinfo has not been acquired from the
  7907. * DUT yet.
  7908. *
  7909. * RETURN: Pointer to the system information struct
  7910. *
  7911. * PARAMETERS:
  7912. * *dev - pointer to device structure
  7913. ******************************************************************************/
  7914. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7915. {
  7916. struct pt_core_data *cd = dev_get_drvdata(dev);
  7917. int rc = 0;
  7918. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7919. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7920. !cd->sysinfo.ready) {
  7921. rc = pt_hid_output_get_sysinfo_(cd);
  7922. if (rc) {
  7923. pt_debug(cd->dev, DL_ERROR,
  7924. "%s: Error getting sysinfo rc=%d\n",
  7925. __func__, rc);
  7926. }
  7927. }
  7928. if (cd->sysinfo.ready)
  7929. return &cd->sysinfo;
  7930. return NULL;
  7931. }
  7932. /*******************************************************************************
  7933. * FUNCTION: _pt_request_loader_pdata
  7934. *
  7935. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7936. * to request the pointer to the loader platform data
  7937. *
  7938. * RETURN: Pointer to the loader platform data struct
  7939. *
  7940. * PARAMETERS:
  7941. * *dev - pointer to device structure
  7942. ******************************************************************************/
  7943. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7944. struct device *dev)
  7945. {
  7946. struct pt_core_data *cd = dev_get_drvdata(dev);
  7947. return cd->pdata->loader_pdata;
  7948. }
  7949. /*******************************************************************************
  7950. * FUNCTION: _pt_request_start_wd
  7951. *
  7952. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7953. * to request to start the TTDL watchdog
  7954. *
  7955. * RETURN:
  7956. * 0 = success
  7957. * !0 = failure
  7958. *
  7959. * PARAMETERS:
  7960. * *dev - pointer to device structure
  7961. ******************************************************************************/
  7962. static int _pt_request_start_wd(struct device *dev)
  7963. {
  7964. struct pt_core_data *cd = dev_get_drvdata(dev);
  7965. pt_start_wd_timer(cd);
  7966. return 0;
  7967. }
  7968. /*******************************************************************************
  7969. * FUNCTION: _pt_request_stop_wd
  7970. *
  7971. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7972. * to request to stop the TTDL watchdog
  7973. *
  7974. * RETURN:
  7975. * 0 = success
  7976. * !0 = failure
  7977. *
  7978. * PARAMETERS:
  7979. * *dev - pointer to device structure
  7980. ******************************************************************************/
  7981. static int _pt_request_stop_wd(struct device *dev)
  7982. {
  7983. struct pt_core_data *cd = dev_get_drvdata(dev);
  7984. pt_stop_wd_timer(cd);
  7985. return 0;
  7986. }
  7987. /*******************************************************************************
  7988. * FUNCTION: pt_pip2_launch_app
  7989. *
  7990. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7991. * the FW reset sentinel to indicate the function succeeded.
  7992. *
  7993. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7994. * in a timeout delay and with the DUT being reset with an XRES.
  7995. *
  7996. * RETURN:
  7997. * 0 = success
  7998. * !0 = failure
  7999. *
  8000. * PARAMETERS:
  8001. * *cd - pointer to core data structure
  8002. * protect - flag to call protected or non-protected
  8003. ******************************************************************************/
  8004. static int pt_pip2_launch_app(struct device *dev, int protect)
  8005. {
  8006. struct pt_core_data *cd = dev_get_drvdata(dev);
  8007. u16 actual_read_len;
  8008. u16 tmp_startup_status = cd->startup_status;
  8009. u8 read_buf[12];
  8010. u8 status;
  8011. int time = 0;
  8012. int rc = 0;
  8013. mutex_lock(&cd->system_lock);
  8014. cd->startup_status = STARTUP_STATUS_START;
  8015. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8016. mutex_unlock(&cd->system_lock);
  8017. rc = _pt_request_pip2_send_cmd(dev, protect,
  8018. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8019. &actual_read_len);
  8020. if (rc) {
  8021. pt_debug(dev, DL_ERROR,
  8022. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8023. __func__, rc);
  8024. } else {
  8025. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8026. /* Test for no or invalid image in FLASH, no point to reset */
  8027. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8028. rc = status;
  8029. goto exit;
  8030. }
  8031. /* Any other boot failure */
  8032. if (status != 0) {
  8033. pt_debug(dev, DL_ERROR,
  8034. "%s: FW did not EXECUTE, status = %d\n",
  8035. __func__, status);
  8036. rc = status;
  8037. }
  8038. }
  8039. if (rc) {
  8040. pt_debug(dev, DL_ERROR,
  8041. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8042. __func__, rc);
  8043. goto exit;
  8044. }
  8045. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8046. msleep(20);
  8047. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8048. __func__, time, cd->startup_status);
  8049. time += 20;
  8050. }
  8051. if (cd->startup_status == STARTUP_STATUS_START) {
  8052. pt_debug(cd->dev, DL_WARN,
  8053. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8054. rc = -ETIME;
  8055. }
  8056. exit:
  8057. if (cd->startup_status == STARTUP_STATUS_START) {
  8058. /* Reset to original state because we could be stuck in BL */
  8059. mutex_lock(&cd->system_lock);
  8060. cd->startup_status = tmp_startup_status;
  8061. mutex_unlock(&cd->system_lock);
  8062. }
  8063. return rc;
  8064. }
  8065. /*******************************************************************************
  8066. * FUNCTION: _pt_request_pip2_launch_app
  8067. *
  8068. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8069. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8070. * consumed.
  8071. *
  8072. * RETURN:
  8073. * 0 = success
  8074. * !0 = failure
  8075. *
  8076. * PARAMETERS:
  8077. * *cd - pointer to core data structure
  8078. ******************************************************************************/
  8079. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8080. {
  8081. return pt_pip2_launch_app(dev, protect);
  8082. }
  8083. /*******************************************************************************
  8084. * FUNCTION: _pt_request_wait_for_enum_state
  8085. *
  8086. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8087. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8088. *
  8089. * RETURN:
  8090. * 0 = success
  8091. * !0 = failure
  8092. *
  8093. * PARAMETERS:
  8094. * *cd - pointer to core data structure
  8095. * timeout - timeout for how long to wait
  8096. * state - enum state to wait for
  8097. ******************************************************************************/
  8098. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8099. int state)
  8100. {
  8101. int rc = 0;
  8102. int t;
  8103. struct pt_core_data *cd = dev_get_drvdata(dev);
  8104. t = wait_event_timeout(cd->wait_q,
  8105. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8106. msecs_to_jiffies(timeout));
  8107. if (IS_TMO(t)) {
  8108. pt_debug(cd->dev, DL_ERROR,
  8109. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8110. __func__, state, timeout);
  8111. pt_debug(cd->dev, DL_WARN,
  8112. "%s: enum state reached 0x%04X\n",
  8113. __func__, cd->startup_status);
  8114. rc = -ETIME;
  8115. } else if (cd->startup_status & state) {
  8116. pt_debug(cd->dev, DL_INFO,
  8117. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8118. __func__, cd->startup_status,
  8119. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8120. } else {
  8121. if (t == 1) {
  8122. pt_debug(
  8123. cd->dev, DL_ERROR,
  8124. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8125. __func__, state, timeout);
  8126. rc = -ETIME;
  8127. } else {
  8128. pt_debug(
  8129. cd->dev, DL_ERROR,
  8130. "%s: Enum state 0x%04X not reached in %dms\n",
  8131. __func__, state, timeout - jiffies_to_msecs(t));
  8132. rc = -EINVAL;
  8133. }
  8134. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8135. __func__, cd->startup_status);
  8136. }
  8137. return rc;
  8138. }
  8139. /*******************************************************************************
  8140. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8141. *
  8142. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8143. * deep sleep.
  8144. *
  8145. * RETURN:
  8146. * 0 = success
  8147. * !0 = error
  8148. *
  8149. * PARAMETERS:
  8150. * *cd - pointer to core data
  8151. ******************************************************************************/
  8152. static int pt_core_wake_device_from_deep_sleep_(
  8153. struct pt_core_data *cd)
  8154. {
  8155. int rc;
  8156. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8157. if (rc)
  8158. rc = -EAGAIN;
  8159. /* Prevent failure on sequential wake/sleep requests from OS */
  8160. msleep(20);
  8161. return rc;
  8162. }
  8163. /*******************************************************************************
  8164. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8165. *
  8166. * SUMMARY: Wake up device from Easy-Wake state.
  8167. *
  8168. * RETURN:
  8169. * 0 = success
  8170. * !0 = failure
  8171. *
  8172. * PARAMETERS:
  8173. * *cd - pointer to core data
  8174. ******************************************************************************/
  8175. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8176. {
  8177. int rc = 0;
  8178. mutex_lock(&cd->system_lock);
  8179. cd->wait_until_wake = 1;
  8180. mutex_unlock(&cd->system_lock);
  8181. wake_up(&cd->wait_q);
  8182. msleep(20);
  8183. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8184. return rc;
  8185. }
  8186. /*******************************************************************************
  8187. * FUNCTION: pt_restore_parameters_
  8188. *
  8189. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8190. * back to the DUT
  8191. *
  8192. * RETURN:
  8193. * 0 = success
  8194. * !0 = failure
  8195. *
  8196. * PARAMETERS:
  8197. * *cd - pointer the core data structure
  8198. ******************************************************************************/
  8199. static int pt_restore_parameters_(struct pt_core_data *cd)
  8200. {
  8201. struct param_node *param;
  8202. int rc = 0;
  8203. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8204. goto exit;
  8205. spin_lock(&cd->spinlock);
  8206. list_for_each_entry(param, &cd->param_list, node) {
  8207. spin_unlock(&cd->spinlock);
  8208. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8209. __func__, param->id, param->value);
  8210. rc = pt_pip_set_param_(cd, param->id,
  8211. param->value, param->size);
  8212. if (rc)
  8213. goto exit;
  8214. spin_lock(&cd->spinlock);
  8215. }
  8216. spin_unlock(&cd->spinlock);
  8217. exit:
  8218. return rc;
  8219. }
  8220. /*******************************************************************************
  8221. * FUNCTION: pt_pip2_exit_bl_
  8222. *
  8223. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8224. * a DUT that may not have flash and will need FW to be loaded into RAM
  8225. *
  8226. * RETURN:
  8227. * 0 = success
  8228. * !0 = failure
  8229. *
  8230. * PARAMETERS:
  8231. * *cd - pointer the core data structure
  8232. * *status_str - pointer to optional status string buffer
  8233. * buf_size - size of status_str buffer
  8234. ******************************************************************************/
  8235. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8236. {
  8237. int rc;
  8238. int wait_time = 0;
  8239. u8 mode = PT_MODE_UNKNOWN;
  8240. bool load_status_str = false;
  8241. /*
  8242. * Below function has protective call to ensure no enum is still on
  8243. * going, while this kind of protection should be applied widely in
  8244. * future (TODO).
  8245. */
  8246. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8247. if (status_str && buf_size <= 50)
  8248. load_status_str = true;
  8249. if (mode == PT_MODE_BOOTLOADER) {
  8250. if (cd->flashless_dut == 1) {
  8251. rc = pt_hw_hard_reset(cd);
  8252. } else {
  8253. rc = pt_pip2_launch_app(cd->dev,
  8254. PT_CORE_CMD_UNPROTECTED);
  8255. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8256. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8257. __func__, "Invalid image in FLASH rc", rc);
  8258. } else if (rc) {
  8259. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8260. __func__, "Failed to launch app rc", rc);
  8261. }
  8262. }
  8263. if (!rc) {
  8264. if (cd->flashless_dut == 1) {
  8265. /* Wait for BL to complete before enum */
  8266. rc = _pt_request_wait_for_enum_state(cd->dev,
  8267. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8268. if (rc && load_status_str) {
  8269. strlcpy(status_str, "No FW sentinel after BL",
  8270. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8271. goto exit;
  8272. }
  8273. }
  8274. /*
  8275. * If the host wants to interact with the FW or do a
  8276. * forced calibration, the FW must be out of BOOT mode
  8277. * and the system information must have been retrieved.
  8278. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8279. * If, however, the enumeration does not reach this
  8280. * point, the DUT may still be in APP mode so test
  8281. * for all conditions.
  8282. */
  8283. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8284. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8285. if (!rc || cd->startup_status >=
  8286. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8287. mutex_lock(&cd->system_lock);
  8288. cd->mode = PT_MODE_OPERATIONAL;
  8289. mutex_unlock(&cd->system_lock);
  8290. }
  8291. if (rc) {
  8292. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8293. __func__, "Failed to enum with DUT",
  8294. cd->startup_status);
  8295. if (load_status_str && !(cd->startup_status &
  8296. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8297. strlcpy(status_str, "FW Stuck in Boot mode",
  8298. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8299. goto exit;
  8300. }
  8301. }
  8302. /*
  8303. * The coming FW sentinel could wake up the event
  8304. * queue, which has chance to be taken by next command
  8305. * wrongly. Following delay is a workaround to avoid
  8306. * this issue for most situations.
  8307. */
  8308. msleep(20);
  8309. pt_start_wd_timer(cd);
  8310. }
  8311. if (load_status_str) {
  8312. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8313. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8314. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8315. else if (!rc)
  8316. strlcpy(status_str, "Entered APP from BL mode",
  8317. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8318. else
  8319. strlcpy(status_str, "Failed to enter APP from BL mode",
  8320. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8321. }
  8322. } else if (mode == PT_MODE_OPERATIONAL) {
  8323. mutex_lock(&cd->system_lock);
  8324. cd->mode = mode;
  8325. mutex_unlock(&cd->system_lock);
  8326. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8327. if (load_status_str) {
  8328. if (!rc)
  8329. strlcpy(status_str, "Already in APP mode",
  8330. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8331. else
  8332. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8333. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8334. }
  8335. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8336. mutex_lock(&cd->system_lock);
  8337. cd->mode = mode;
  8338. mutex_unlock(&cd->system_lock);
  8339. if (load_status_str)
  8340. strlcpy(status_str, "Failed to determine active mode",
  8341. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8342. }
  8343. exit:
  8344. if (!rc)
  8345. pt_start_wd_timer(cd);
  8346. return rc;
  8347. }
  8348. /*******************************************************************************
  8349. * FUNCTION: pt_pip2_exit_bl
  8350. *
  8351. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8352. * access.
  8353. *
  8354. * RETURN:
  8355. * 0 = success
  8356. * !0 = failure
  8357. *
  8358. * PARAMETERS:
  8359. * *cd - pointer the core data structure
  8360. * *status_str - pointer to optional status string buffer
  8361. * buf_size - size of status_str buffer
  8362. ******************************************************************************/
  8363. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8364. {
  8365. int rc;
  8366. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8367. if (rc < 0) {
  8368. pt_debug(cd->dev, DL_ERROR,
  8369. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8370. cd->exclusive_dev, cd->dev);
  8371. return rc;
  8372. }
  8373. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8374. if (release_exclusive(cd, cd->dev) < 0)
  8375. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8376. __func__);
  8377. return rc;
  8378. }
  8379. /*******************************************************************************
  8380. * FUNCTION: _fast_startup
  8381. *
  8382. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8383. *
  8384. * RETURN:
  8385. * 0 = success
  8386. * !0 = failure
  8387. *
  8388. * PARAMETERS:
  8389. * *cd - pointer the core data structure
  8390. ******************************************************************************/
  8391. static int _fast_startup(struct pt_core_data *cd)
  8392. {
  8393. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8394. int rc = 0;
  8395. u8 mode = PT_MODE_UNKNOWN;
  8396. struct pt_hid_desc hid_desc;
  8397. int wait_time = 0;
  8398. memset(&hid_desc, 0, sizeof(hid_desc));
  8399. reset:
  8400. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8401. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8402. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8403. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8404. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8405. __func__);
  8406. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8407. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8408. if (rc < 0) {
  8409. pt_debug(cd->dev, DL_ERROR,
  8410. "%s: Error on getting HID descriptor r=%d\n",
  8411. __func__, rc);
  8412. if (retry--)
  8413. goto reset;
  8414. goto exit;
  8415. }
  8416. cd->mode = pt_get_mode(cd, &hid_desc);
  8417. if (cd->mode == PT_MODE_BOOTLOADER) {
  8418. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8419. __func__);
  8420. rc = pt_hid_output_bl_launch_app_(cd);
  8421. if (rc < 0) {
  8422. pt_debug(cd->dev, DL_ERROR,
  8423. "%s: Error on launch app r=%d\n",
  8424. __func__, rc);
  8425. if (retry--)
  8426. goto reset;
  8427. goto exit;
  8428. }
  8429. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8430. if (rc < 0) {
  8431. pt_debug(cd->dev, DL_ERROR,
  8432. "%s: Error on getting HID descriptor r=%d\n",
  8433. __func__, rc);
  8434. if (retry--)
  8435. goto reset;
  8436. goto exit;
  8437. }
  8438. cd->mode = pt_get_mode(cd, &hid_desc);
  8439. if (cd->mode == PT_MODE_BOOTLOADER) {
  8440. if (retry--)
  8441. goto reset;
  8442. goto exit;
  8443. }
  8444. }
  8445. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8446. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8447. } else {
  8448. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8449. __func__);
  8450. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8451. /* Wait for any sentinel before first try */
  8452. rc = _pt_request_wait_for_enum_state(
  8453. cd->dev, 150,
  8454. STARTUP_STATUS_BL_RESET_SENTINEL |
  8455. STARTUP_STATUS_FW_RESET_SENTINEL);
  8456. if (rc)
  8457. pt_debug(cd->dev, DL_ERROR,
  8458. "%s: No Sentinel detected rc = %d\n",
  8459. __func__, rc);
  8460. } else
  8461. pt_flush_bus_if_irq_asserted(cd,
  8462. PT_FLUSH_BUS_BASED_ON_LEN);
  8463. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8464. if (rc) {
  8465. pt_debug(cd->dev, DL_ERROR,
  8466. "%s: Get mode failed, mode unknown\n",
  8467. __func__);
  8468. mode = PT_MODE_UNKNOWN;
  8469. }
  8470. cd->mode = mode;
  8471. if (cd->mode == PT_MODE_BOOTLOADER) {
  8472. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8473. __func__);
  8474. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8475. if (rc) {
  8476. pt_debug(cd->dev, DL_ERROR,
  8477. "%s Failed to exit bootloader\n",
  8478. __func__);
  8479. msleep(50);
  8480. rc = -ENODEV;
  8481. if (retry--)
  8482. goto reset;
  8483. goto exit;
  8484. } else {
  8485. pt_debug(cd->dev, DL_INFO,
  8486. "%s: Exit bootloader successfully\n",
  8487. __func__);
  8488. }
  8489. if (cd->mode != PT_MODE_OPERATIONAL) {
  8490. pt_debug(cd->dev, DL_WARN,
  8491. "%s: restore mode failure mode = %d\n",
  8492. __func__, cd->mode);
  8493. if (retry--)
  8494. goto reset;
  8495. goto exit;
  8496. }
  8497. }
  8498. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8499. }
  8500. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8501. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8502. if (!rc) {
  8503. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8504. pt_debug(cd->dev, DL_WARN,
  8505. "%s: Exit FW BOOT Mode after %dms\n",
  8506. __func__, wait_time);
  8507. } else {
  8508. pt_debug(cd->dev, DL_WARN,
  8509. "%s: FW stuck in BOOT Mode after %dms\n",
  8510. __func__, wait_time);
  8511. goto exit;
  8512. }
  8513. if (!cd->sysinfo.ready) {
  8514. rc = pt_hid_output_get_sysinfo_(cd);
  8515. if (rc) {
  8516. pt_debug(cd->dev, DL_ERROR,
  8517. "%s: Error on getting sysinfo r=%d\n",
  8518. __func__, rc);
  8519. if (retry--)
  8520. goto reset;
  8521. goto exit;
  8522. }
  8523. }
  8524. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8525. rc = pt_restore_parameters_(cd);
  8526. if (rc)
  8527. pt_debug(cd->dev, DL_ERROR,
  8528. "%s: failed to restore parameters rc=%d\n",
  8529. __func__, rc);
  8530. else
  8531. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8532. exit:
  8533. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8534. return rc;
  8535. }
  8536. /*******************************************************************************
  8537. * FUNCTION: pt_core_poweron_device_
  8538. *
  8539. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8540. *
  8541. * RETURN:
  8542. * 0 = success
  8543. * !0 = failure
  8544. *
  8545. * PARAMETERS:
  8546. * *cd - pointer to core data
  8547. ******************************************************************************/
  8548. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8549. {
  8550. struct device *dev = cd->dev;
  8551. int rc = 0;
  8552. /*
  8553. * After power on action, the chip can general FW sentinel. It can
  8554. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8555. * _fast_startup() can perform a quick enumeration too, here doesn't
  8556. * need another enumeration.
  8557. */
  8558. mutex_lock(&cd->system_lock);
  8559. cd->startup_status = STARTUP_STATUS_START;
  8560. cd->hid_reset_cmd_state = 1;
  8561. mutex_unlock(&cd->system_lock);
  8562. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8563. if (rc < 0) {
  8564. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8565. __func__, rc);
  8566. goto exit;
  8567. }
  8568. if (!cd->irq_enabled) {
  8569. cd->irq_enabled = true;
  8570. enable_irq(cd->irq);
  8571. }
  8572. /* TBD: following function doesn't update startup_status */
  8573. rc = _fast_startup(cd);
  8574. exit:
  8575. return rc;
  8576. }
  8577. /*******************************************************************************
  8578. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8579. *
  8580. * SUMMARY: Reset device, and then trigger a full enumeration.
  8581. *
  8582. * RETURN:
  8583. * 0 = success
  8584. * !0 = failure
  8585. *
  8586. * PARAMETERS:
  8587. * *cd - pointer to core data
  8588. ******************************************************************************/
  8589. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8590. {
  8591. int rc;
  8592. rc = pt_dut_reset_and_wait(cd);
  8593. if (rc < 0) {
  8594. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8595. __func__, rc);
  8596. goto exit;
  8597. }
  8598. rc = _fast_startup(cd);
  8599. exit:
  8600. return rc;
  8601. }
  8602. /*******************************************************************************
  8603. * FUNCTION: pt_core_easywake_off_
  8604. *
  8605. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8606. * the configuration in the core platform data structure.
  8607. *
  8608. * RETURN:
  8609. * 0 = success
  8610. * !0 = failure
  8611. *
  8612. * PARAMETERS:
  8613. * *cd - pointer to core data
  8614. ******************************************************************************/
  8615. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8616. {
  8617. int rc = 0;
  8618. mutex_lock(&cd->system_lock);
  8619. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8620. mutex_unlock(&cd->system_lock);
  8621. pt_debug(cd->dev, DL_INFO,
  8622. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8623. return 0;
  8624. }
  8625. mutex_unlock(&cd->system_lock);
  8626. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8627. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8628. rc = pt_core_wake_device_from_easy_wake_(cd);
  8629. if (rc < 0)
  8630. pt_debug(cd->dev, DL_ERROR,
  8631. "%s - %d failed %d\n", __func__, rc);
  8632. }
  8633. mutex_lock(&cd->system_lock);
  8634. cd->sleep_state = SS_EASY_WAKING_OFF;
  8635. mutex_unlock(&cd->system_lock);
  8636. pt_start_wd_timer(cd);
  8637. return rc;
  8638. }
  8639. /*******************************************************************************
  8640. * FUNCTION: pt_core_easywake_off
  8641. *
  8642. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8643. *
  8644. * RETURN:
  8645. * 0 = success
  8646. * !0 = failure
  8647. *
  8648. * PARAMETERS:
  8649. * *cd - pointer to core data
  8650. ******************************************************************************/
  8651. static int pt_core_easywake_off(struct pt_core_data *cd)
  8652. {
  8653. int rc;
  8654. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8655. if (rc < 0) {
  8656. pt_debug(cd->dev, DL_ERROR,
  8657. "%s: fail get exclusive ex=%p own=%p\n",
  8658. __func__, cd->exclusive_dev, cd->dev);
  8659. return rc;
  8660. }
  8661. rc = pt_core_easywake_off_(cd);
  8662. if (release_exclusive(cd, cd->dev) < 0)
  8663. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8664. __func__);
  8665. else
  8666. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8667. __func__);
  8668. return rc;
  8669. }
  8670. /*******************************************************************************
  8671. * FUNCTION: pt_core_wake_
  8672. *
  8673. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8674. * the configuration in the core platform data structure.
  8675. *
  8676. * RETURN:
  8677. * 0 = success
  8678. * !0 = failure
  8679. *
  8680. * PARAMETERS:
  8681. * *cd - pointer to core data
  8682. ******************************************************************************/
  8683. static int pt_core_wake_(struct pt_core_data *cd)
  8684. {
  8685. int rc = 0;
  8686. mutex_lock(&cd->system_lock);
  8687. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEP_NONE) {
  8688. cd->sleep_state = SS_WAKING;
  8689. } else {
  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. }
  8695. mutex_unlock(&cd->system_lock);
  8696. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8697. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8698. pt_debug(cd->dev, DL_INFO,
  8699. "%s: Entering into poweron mode:\n", __func__);
  8700. rc = pt_core_poweron_device_(cd);
  8701. if (rc < 0)
  8702. pr_err("%s: Poweron error detected: rc=%d\n",
  8703. __func__, rc);
  8704. }
  8705. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8706. rc = pt_core_wake_device_from_deep_standby_(cd);
  8707. else /* Default action to exit DeepSleep */
  8708. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8709. }
  8710. mutex_lock(&cd->system_lock);
  8711. cd->sleep_state = SS_SLEEP_OFF;
  8712. mutex_unlock(&cd->system_lock);
  8713. pt_start_wd_timer(cd);
  8714. return rc;
  8715. }
  8716. /*******************************************************************************
  8717. * FUNCTION: pt_core_wake_
  8718. *
  8719. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8720. *
  8721. * RETURN:
  8722. * 0 = success
  8723. * !0 = failure
  8724. *
  8725. * PARAMETERS:
  8726. * *cd - pointer to core data
  8727. ******************************************************************************/
  8728. static int pt_core_wake(struct pt_core_data *cd)
  8729. {
  8730. int rc = 0;
  8731. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8732. if (rc < 0) {
  8733. pt_debug(cd->dev, DL_ERROR,
  8734. "%s: fail get exclusive ex=%p own=%p\n",
  8735. __func__, cd->exclusive_dev, cd->dev);
  8736. return rc;
  8737. }
  8738. rc = pt_core_wake_(cd);
  8739. if (release_exclusive(cd, cd->dev) < 0)
  8740. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8741. __func__);
  8742. else
  8743. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8744. __func__);
  8745. return rc;
  8746. }
  8747. /*******************************************************************************
  8748. * FUNCTION: pt_get_ic_crc_
  8749. *
  8750. * SUMMARY: This function retrieves the config block CRC
  8751. *
  8752. * NOTE: The post condition of calling this function will be that the DUT will
  8753. * be in SCANNINING mode if no failures occur
  8754. *
  8755. * RETURN:
  8756. * 0 = success
  8757. * !0 = failure
  8758. *
  8759. * PARAMETERS:
  8760. * *cd - pointer the core data structure
  8761. * ebid - enumerated block ID
  8762. ******************************************************************************/
  8763. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8764. {
  8765. struct pt_sysinfo *si = &cd->sysinfo;
  8766. int rc = 0;
  8767. u8 status;
  8768. u16 calculated_crc = 0;
  8769. u16 stored_crc = 0;
  8770. rc = pt_pip_suspend_scanning_(cd);
  8771. if (rc)
  8772. goto error;
  8773. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8774. &calculated_crc, &stored_crc);
  8775. if (rc)
  8776. goto exit;
  8777. if (status) {
  8778. rc = -EINVAL;
  8779. goto exit;
  8780. }
  8781. si->ttconfig.crc = stored_crc;
  8782. exit:
  8783. pt_pip_resume_scanning_(cd);
  8784. error:
  8785. pt_debug(cd->dev, DL_INFO,
  8786. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8787. __func__, ebid, calculated_crc, stored_crc, rc);
  8788. return rc;
  8789. }
  8790. /*******************************************************************************
  8791. * FUNCTION: pt_pip2_read_gpio
  8792. *
  8793. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8794. * bits into the passed in variable
  8795. *
  8796. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8797. *
  8798. * RETURN:
  8799. * 0 = success
  8800. * !0 = failure
  8801. *
  8802. * PARAMETERS:
  8803. * *dev - pointer to device structure
  8804. * *status - pointer to where the command response status is stored
  8805. * *gpio - pointer to device attributes structure
  8806. ******************************************************************************/
  8807. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8808. {
  8809. int rc = 0;
  8810. u16 actual_read_len;
  8811. u8 read_buf[12];
  8812. u8 tmp_status = 0;
  8813. u8 index = PIP2_RESP_STATUS_OFFSET;
  8814. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8815. rc = _pt_request_pip2_send_cmd(dev,
  8816. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8817. NULL, 0, read_buf, &actual_read_len);
  8818. if (rc) {
  8819. pt_debug(dev, DL_ERROR,
  8820. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8821. rc = -ECOMM;
  8822. } else {
  8823. tmp_status = read_buf[index];
  8824. }
  8825. if (status)
  8826. *status = tmp_status;
  8827. if (!rc && gpio && (tmp_status == 0)) {
  8828. *gpio = ((read_buf[index + 4] << 24) |
  8829. (read_buf[index + 3] << 16) |
  8830. (read_buf[index + 2] << 8) |
  8831. (read_buf[index + 1]));
  8832. }
  8833. return rc;
  8834. }
  8835. /*******************************************************************************
  8836. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8837. *
  8838. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8839. *
  8840. * RETURN:
  8841. * 0 = success
  8842. * !0 = failure
  8843. *
  8844. * PARAMETERS:
  8845. * *cd - pointer the core data structure
  8846. * *pid - pointer to store panel ID
  8847. ******************************************************************************/
  8848. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8849. {
  8850. u32 gpio_value = 0;
  8851. u8 status = 0;
  8852. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8853. int rc = 0;
  8854. if (!pid)
  8855. return -ENOMEM;
  8856. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8857. if (!rc) {
  8858. if (status == 0) {
  8859. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8860. PT_PANEL_ID_SHIFT;
  8861. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8862. __func__,
  8863. "BL mode PID", panel_id, "gpio", gpio_value);
  8864. *pid = panel_id;
  8865. } else {
  8866. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8867. __func__,
  8868. "BL read gpio failed status", status);
  8869. }
  8870. } else {
  8871. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8872. __func__,
  8873. "BL read gpio failed status", status);
  8874. }
  8875. return rc;
  8876. }
  8877. /*******************************************************************************
  8878. * FUNCTION: pt_enum_with_dut_
  8879. *
  8880. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8881. * The core data (cd) startup_status will store, as a bitmask, each
  8882. * state of the enumeration process. The startup will be attempted
  8883. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8884. *
  8885. * RETURN:
  8886. * 0 = success
  8887. * !0 = failure
  8888. *
  8889. * PARAMETERS:
  8890. * *cd - pointer the core data structure
  8891. * reset - Flag to reset the DUT before attempting to enumerate
  8892. * *status - poionter to store the enum status bitmask flags
  8893. ******************************************************************************/
  8894. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8895. u32 *enum_status)
  8896. {
  8897. int try = 1;
  8898. int rc = 0;
  8899. int wait_time = 0;
  8900. bool detected = false;
  8901. u8 return_data[8];
  8902. u8 mode = PT_MODE_UNKNOWN;
  8903. u8 pid = PANEL_ID_NOT_ENABLED;
  8904. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8905. struct pt_hid_desc hid_desc;
  8906. memset(&hid_desc, 0, sizeof(hid_desc));
  8907. #ifdef TTHE_TUNER_SUPPORT
  8908. tthe_print(cd, NULL, 0, "enter startup");
  8909. #endif
  8910. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8911. __func__, cd->startup_status, reset);
  8912. pt_stop_wd_timer(cd);
  8913. reset:
  8914. if (try > 1)
  8915. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8916. __func__, try);
  8917. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8918. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8919. pt_debug(cd->dev, DL_INFO,
  8920. "%s: PIP1 Enumeration start\n", __func__);
  8921. /* Only reset the DUT after the first try */
  8922. if (reset || (try > 1)) {
  8923. /*
  8924. * Reset hardware only for Legacy parts. Skip for TT/TC
  8925. * parts because if the FW image was loaded directly
  8926. * to SRAM issueing a reset ill wipe out what was just
  8927. * loaded.
  8928. */
  8929. rc = pt_dut_reset_and_wait(cd);
  8930. if (rc < 0) {
  8931. pt_debug(cd->dev, DL_ERROR,
  8932. "%s: Error on h/w reset r=%d\n",
  8933. __func__, rc);
  8934. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8935. goto reset;
  8936. goto exit;
  8937. }
  8938. /* sleep to allow FW to be launched if available */
  8939. msleep(120);
  8940. }
  8941. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8942. if (rc < 0) {
  8943. pt_debug(cd->dev, DL_ERROR,
  8944. "%s: Error getting HID Descriptor r=%d\n",
  8945. __func__, rc);
  8946. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8947. goto reset;
  8948. rc = -EIO;
  8949. goto exit;
  8950. }
  8951. detected = true;
  8952. cd->mode = pt_get_mode(cd, &hid_desc);
  8953. /*
  8954. * Most systems do not use an XY pin as the panel_id and so
  8955. * the BL is used to retrieve the panel_id, however for
  8956. * systems that do use an XY pin, the panel_id MUST be
  8957. * retrieved from the system information when running FW
  8958. * (done below) and so this section of code is skipped.
  8959. * Entering the BL here is only needed on XY_PIN systems.
  8960. */
  8961. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8962. if (cd->mode == PT_MODE_OPERATIONAL) {
  8963. rc = pt_pip_start_bootloader_(cd);
  8964. if (rc < 0) {
  8965. pt_debug(cd->dev, DL_ERROR,
  8966. "%s: Error on start bootloader r=%d\n",
  8967. __func__, rc);
  8968. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8969. goto reset;
  8970. goto exit;
  8971. }
  8972. cd->mode = PT_MODE_BOOTLOADER;
  8973. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8974. __func__);
  8975. }
  8976. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8977. if (!rc) {
  8978. cd->bl_info.ready = true;
  8979. cd->bl_info.chip_id =
  8980. get_unaligned_le16(&return_data[2]);
  8981. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8982. __func__, cd->bl_info.chip_id);
  8983. } else {
  8984. pt_debug(cd->dev, DL_ERROR,
  8985. "%s: failed to get chip ID, r=%d\n",
  8986. __func__, rc);
  8987. }
  8988. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8989. mutex_lock(&cd->system_lock);
  8990. if (!rc) {
  8991. cd->pid_for_loader = pid;
  8992. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8993. __func__, cd->pid_for_loader);
  8994. } else {
  8995. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8996. pt_debug(cd->dev, DL_WARN,
  8997. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8998. __func__, cd->pid_for_loader);
  8999. }
  9000. mutex_unlock(&cd->system_lock);
  9001. }
  9002. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9003. if (cd->mode == PT_MODE_BOOTLOADER) {
  9004. rc = pt_hid_output_bl_launch_app_(cd);
  9005. if (rc < 0) {
  9006. pt_debug(cd->dev, DL_ERROR,
  9007. "%s: Error on launch app r=%d\n",
  9008. __func__, rc);
  9009. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9010. goto reset;
  9011. rc = -ENODEV;
  9012. goto exit;
  9013. }
  9014. /* sleep to allow FW to be launched if available */
  9015. msleep(120);
  9016. /* Ensure the DUT is now in Application mode */
  9017. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9018. if (rc < 0) {
  9019. pt_debug(cd->dev, DL_ERROR,
  9020. "%s: Error getting HID Desc r=%d\n",
  9021. __func__, rc);
  9022. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9023. goto reset;
  9024. rc = -EIO;
  9025. goto exit;
  9026. }
  9027. cd->mode = pt_get_mode(cd, &hid_desc);
  9028. if (cd->mode == PT_MODE_BOOTLOADER) {
  9029. pt_debug(cd->dev, DL_WARN,
  9030. "%s: Error confiming exit BL\n",
  9031. __func__);
  9032. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9033. goto reset;
  9034. rc = -ENODEV;
  9035. goto exit;
  9036. }
  9037. }
  9038. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9039. cd->mode = PT_MODE_OPERATIONAL;
  9040. *enum_status |= STARTUP_STATUS_GET_DESC;
  9041. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9042. } else {
  9043. /* Generation is PIP2 Capable */
  9044. pt_debug(cd->dev, DL_INFO,
  9045. "%s: PIP2 Enumeration start\n", __func__);
  9046. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9047. if (rc) {
  9048. pt_debug(cd->dev, DL_ERROR,
  9049. "%s: Get mode failed, mode unknown\n",
  9050. __func__);
  9051. mode = PT_MODE_UNKNOWN;
  9052. } else
  9053. detected = true;
  9054. cd->mode = mode;
  9055. switch (cd->mode) {
  9056. case PT_MODE_OPERATIONAL:
  9057. pt_debug(cd->dev, DL_INFO,
  9058. "%s: Operational mode\n", __func__);
  9059. if (cd->app_pip_ver_ready == false) {
  9060. rc = pt_pip2_get_version_(cd);
  9061. if (!rc)
  9062. cd->app_pip_ver_ready = true;
  9063. else {
  9064. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9065. goto reset;
  9066. goto exit;
  9067. }
  9068. }
  9069. break;
  9070. case PT_MODE_BOOTLOADER:
  9071. pt_debug(cd->dev, DL_INFO,
  9072. "%s: Bootloader mode\n", __func__);
  9073. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9074. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9075. mutex_lock(&cd->system_lock);
  9076. if (!rc) {
  9077. cd->pid_for_loader = pid;
  9078. pt_debug(cd->dev, DL_INFO,
  9079. "%s: Panel ID: 0x%02X\n",
  9080. __func__, cd->pid_for_loader);
  9081. } else {
  9082. cd->pid_for_loader =
  9083. PANEL_ID_NOT_ENABLED;
  9084. pt_debug(cd->dev, DL_WARN,
  9085. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9086. __func__, cd->pid_for_loader);
  9087. }
  9088. mutex_unlock(&cd->system_lock);
  9089. }
  9090. if (cd->bl_pip_ver_ready == false) {
  9091. rc = pt_pip2_get_version_(cd);
  9092. if (!rc)
  9093. cd->bl_pip_ver_ready = true;
  9094. else {
  9095. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9096. goto reset;
  9097. goto exit;
  9098. }
  9099. }
  9100. /*
  9101. * Launch app command will fail in flashless mode.
  9102. * Skip launch app command here to save time for
  9103. * enumeration flow.
  9104. */
  9105. if (cd->flashless_dut)
  9106. goto exit;
  9107. /*
  9108. * pt_pip2_launch_app() is needed here instead of
  9109. * pt_pip2_exit_bl() because exit_bl will cause several
  9110. * hw_resets to occur and the auto BL on a flashless
  9111. * DUT will fail.
  9112. */
  9113. rc = pt_pip2_launch_app(cd->dev,
  9114. PT_CORE_CMD_UNPROTECTED);
  9115. if (rc) {
  9116. pt_debug(cd->dev, DL_ERROR,
  9117. "%s: Error on launch app r=%d\n",
  9118. __func__, rc);
  9119. msleep(50);
  9120. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9121. goto reset;
  9122. goto exit;
  9123. }
  9124. /*
  9125. * IRQ thread can be delayed if the serial log print is
  9126. * enabled. It causes next command to get wrong response
  9127. * Here the delay is to ensure pt_parse_input() to be
  9128. * finished.
  9129. */
  9130. msleep(60);
  9131. /* Check and update the mode */
  9132. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9133. if (rc) {
  9134. pt_debug(cd->dev, DL_ERROR,
  9135. "%s: Get mode failed, mode unknown\n",
  9136. __func__);
  9137. mode = PT_MODE_UNKNOWN;
  9138. }
  9139. cd->mode = mode;
  9140. if (cd->mode == PT_MODE_OPERATIONAL) {
  9141. pt_debug(cd->dev, DL_INFO,
  9142. "%s: Launched to Operational mode\n",
  9143. __func__);
  9144. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9145. pt_debug(cd->dev, DL_ERROR,
  9146. "%s: Launch failed, Bootloader mode\n",
  9147. __func__);
  9148. goto exit;
  9149. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9150. pt_debug(cd->dev, DL_ERROR,
  9151. "%s: Launch failed, Unknown mode\n",
  9152. __func__);
  9153. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9154. goto reset;
  9155. goto exit;
  9156. }
  9157. if (cd->app_pip_ver_ready == false) {
  9158. rc = pt_pip2_get_version_(cd);
  9159. if (!rc)
  9160. cd->app_pip_ver_ready = true;
  9161. else {
  9162. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9163. goto reset;
  9164. goto exit;
  9165. }
  9166. }
  9167. break;
  9168. default:
  9169. pt_debug(cd->dev, DL_ERROR,
  9170. "%s: Unknown mode\n", __func__);
  9171. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9172. goto reset;
  9173. goto exit;
  9174. }
  9175. *enum_status |= STARTUP_STATUS_GET_DESC;
  9176. }
  9177. pt_init_pip_report_fields(cd);
  9178. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9179. if (!cd->features.easywake)
  9180. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9181. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9182. rc = pt_hid_output_get_sysinfo_(cd);
  9183. if (rc) {
  9184. pt_debug(cd->dev, DL_ERROR,
  9185. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9186. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9187. goto reset;
  9188. goto exit;
  9189. }
  9190. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9191. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9192. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9193. if (!rc) {
  9194. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9195. pt_debug(cd->dev, DL_WARN,
  9196. "%s: Exit FW BOOT Mode after %dms\n",
  9197. __func__, wait_time);
  9198. } else {
  9199. pt_debug(cd->dev, DL_WARN,
  9200. "%s: FW stuck in BOOT Mode after %dms\n",
  9201. __func__, wait_time);
  9202. goto exit;
  9203. }
  9204. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9205. __func__,
  9206. cd->sysinfo.ttdata.pip_ver_major,
  9207. cd->sysinfo.ttdata.pip_ver_minor);
  9208. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9209. if (rc) {
  9210. pt_debug(cd->dev, DL_ERROR,
  9211. "%s: DUT Config block CRC failure rc=%d\n",
  9212. __func__, rc);
  9213. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9214. goto reset;
  9215. } else {
  9216. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9217. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9218. pt_debug(cd->dev, DL_ERROR,
  9219. "%s: scan state: %d, retry: %d\n",
  9220. __func__, sys_mode, try);
  9221. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9222. goto reset;
  9223. } else
  9224. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9225. }
  9226. rc = pt_restore_parameters_(cd);
  9227. if (rc) {
  9228. pt_debug(cd->dev, DL_ERROR,
  9229. "%s: Failed to restore parameters rc=%d\n",
  9230. __func__, rc);
  9231. } else
  9232. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9233. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9234. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9235. cd->startup_retry_count = 0;
  9236. exit:
  9237. /* Generate the HW Version of the connected DUT and store in cd */
  9238. pt_generate_hw_version(cd, cd->hw_version);
  9239. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9240. cd->hw_version);
  9241. pt_start_wd_timer(cd);
  9242. if (!detected)
  9243. rc = -ENODEV;
  9244. #ifdef TTHE_TUNER_SUPPORT
  9245. tthe_print(cd, NULL, 0, "exit startup");
  9246. #endif
  9247. pt_debug(cd->dev, DL_WARN,
  9248. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9249. __func__, rc, try);
  9250. return rc;
  9251. }
  9252. /*******************************************************************************
  9253. * FUNCTION: pt_enum_with_dut
  9254. *
  9255. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9256. * requesting exclusive access around the call.
  9257. *
  9258. * RETURN:
  9259. * 0 = success
  9260. * !0 = failure
  9261. *
  9262. * PARAMETERS:
  9263. * *cd - pointer the core data structure
  9264. * reset - Flag to reset the DUT before attempting to enumerate
  9265. * *status - pointer to store the enum status bitmask flags
  9266. ******************************************************************************/
  9267. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9268. {
  9269. int rc = 0;
  9270. mutex_lock(&cd->system_lock);
  9271. cd->startup_state = STARTUP_RUNNING;
  9272. mutex_unlock(&cd->system_lock);
  9273. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9274. if (rc) {
  9275. pt_debug(cd->dev, DL_ERROR,
  9276. "%s: fail get exclusive ex=%p own=%p\n",
  9277. __func__, cd->exclusive_dev, cd->dev);
  9278. goto exit;
  9279. }
  9280. rc = pt_enum_with_dut_(cd, reset, status);
  9281. if (release_exclusive(cd, cd->dev) < 0)
  9282. /* Don't return fail code, mode is already changed. */
  9283. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9284. __func__);
  9285. else
  9286. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9287. __func__);
  9288. exit:
  9289. mutex_lock(&cd->system_lock);
  9290. /* Clear startup state for any tasks waiting for startup completion */
  9291. cd->startup_state = STARTUP_NONE;
  9292. mutex_unlock(&cd->system_lock);
  9293. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9294. *status |= STARTUP_STATUS_COMPLETE;
  9295. /* Wake the waiters for end of startup */
  9296. wake_up(&cd->wait_q);
  9297. return rc;
  9298. }
  9299. static int add_sysfs_interfaces(struct device *dev);
  9300. static void remove_sysfs_interfaces(struct device *dev);
  9301. static void remove_sysfs_and_modules(struct device *dev);
  9302. static void pt_release_modules(struct pt_core_data *cd);
  9303. static void pt_probe_modules(struct pt_core_data *cd);
  9304. /*******************************************************************************
  9305. * FUNCTION: _pt_ttdl_restart
  9306. *
  9307. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9308. *
  9309. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9310. * existing sysfs nodes will produce an error.
  9311. *
  9312. * RETURN:
  9313. * 0 = success
  9314. * !0 = failure
  9315. *
  9316. * PARAMETERS:
  9317. * *dev - pointer to core device
  9318. ******************************************************************************/
  9319. static int _pt_ttdl_restart(struct device *dev)
  9320. {
  9321. int rc = 0;
  9322. struct pt_core_data *cd = dev_get_drvdata(dev);
  9323. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9324. struct i2c_client *client =
  9325. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9326. #endif
  9327. /*
  9328. * Make sure the device is awake, pt_mt_release function will
  9329. * cause pm sleep function and lead to deadlock.
  9330. */
  9331. pm_runtime_get_sync(dev);
  9332. /* Use ttdl_restart_lock to avoid reentry */
  9333. mutex_lock(&cd->ttdl_restart_lock);
  9334. remove_sysfs_and_modules(cd->dev);
  9335. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9336. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9337. pt_debug(dev, DL_ERROR,
  9338. "%s I2C functionality not Supported\n", __func__);
  9339. rc = -EIO;
  9340. goto ttdl_no_error;
  9341. }
  9342. #endif
  9343. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9344. rc = _pt_detect_dut_generation(cd->dev,
  9345. &cd->startup_status, &cd->active_dut_generation,
  9346. &cd->mode);
  9347. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9348. pt_debug(dev, DL_ERROR,
  9349. "%s: Error, Unknown DUT Generation rc=%d\n",
  9350. __func__, rc);
  9351. }
  9352. }
  9353. rc = add_sysfs_interfaces(cd->dev);
  9354. if (rc < 0)
  9355. pt_debug(cd->dev, DL_ERROR,
  9356. "%s: Error, failed sysfs nodes rc=%d\n",
  9357. __func__, rc);
  9358. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9359. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9360. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9361. if (rc < 0)
  9362. pt_debug(dev, DL_ERROR,
  9363. "%s: Error, Failed to Enumerate\n", __func__);
  9364. }
  9365. rc = pt_mt_probe(dev);
  9366. if (rc < 0) {
  9367. pt_debug(dev, DL_ERROR,
  9368. "%s: Error, fail mt probe\n", __func__);
  9369. }
  9370. rc = pt_btn_probe(dev);
  9371. if (rc < 0) {
  9372. pt_debug(dev, DL_ERROR,
  9373. "%s: Error, fail btn probe\n", __func__);
  9374. }
  9375. pt_probe_modules(cd);
  9376. pt_debug(cd->dev, DL_WARN,
  9377. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9378. rc = 0;
  9379. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9380. ttdl_no_error:
  9381. #endif
  9382. mutex_unlock(&cd->ttdl_restart_lock);
  9383. mutex_lock(&cd->system_lock);
  9384. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9385. cd->startup_state = STARTUP_NONE;
  9386. mutex_unlock(&cd->system_lock);
  9387. pm_runtime_put(dev);
  9388. return rc;
  9389. }
  9390. /*******************************************************************************
  9391. * FUNCTION: pt_restart_work_function
  9392. *
  9393. * SUMMARY: This is the wrapper function placed in a work queue to call
  9394. * _pt_ttdl_restart()
  9395. *
  9396. * RETURN: none
  9397. *
  9398. * PARAMETERS:
  9399. * *work - pointer to the work_struct
  9400. ******************************************************************************/
  9401. static void pt_restart_work_function(struct work_struct *work)
  9402. {
  9403. struct pt_core_data *cd = container_of(work,
  9404. struct pt_core_data, ttdl_restart_work);
  9405. int rc = 0;
  9406. rc = _pt_ttdl_restart(cd->dev);
  9407. if (rc < 0)
  9408. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9409. __func__, rc);
  9410. }
  9411. /*******************************************************************************
  9412. * FUNCTION: pt_enum_work_function
  9413. *
  9414. * SUMMARY: This is the wrapper function placed in a work queue to call
  9415. * pt_enum_with_dut()
  9416. *
  9417. * RETURN: none
  9418. *
  9419. * PARAMETERS:
  9420. * *work - pointer to the work_struct
  9421. ******************************************************************************/
  9422. static void pt_enum_work_function(struct work_struct *work)
  9423. {
  9424. struct pt_core_data *cd = container_of(work,
  9425. struct pt_core_data, enum_work);
  9426. int rc;
  9427. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9428. if (rc < 0)
  9429. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9430. __func__, rc);
  9431. }
  9432. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9433. {
  9434. int rc;
  9435. if (!get) {
  9436. rc = 0;
  9437. goto regulator_put;
  9438. }
  9439. cd->vdd = regulator_get(cd->dev, "vdd");
  9440. if (IS_ERR(cd->vdd)) {
  9441. rc = PTR_ERR(cd->vdd);
  9442. dev_err(cd->dev,
  9443. "Regulator get failed vdd rc=%d\n", rc);
  9444. goto regulator_put;
  9445. }
  9446. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9447. if (IS_ERR(cd->vcc_i2c)) {
  9448. rc = PTR_ERR(cd->vcc_i2c);
  9449. dev_err(cd->dev,
  9450. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9451. goto regulator_put;
  9452. }
  9453. return 0;
  9454. regulator_put:
  9455. if (cd->vdd) {
  9456. regulator_put(cd->vdd);
  9457. cd->vdd = NULL;
  9458. }
  9459. if (cd->vcc_i2c) {
  9460. regulator_put(cd->vcc_i2c);
  9461. cd->vcc_i2c = NULL;
  9462. }
  9463. return rc;
  9464. }
  9465. #ifdef ENABLE_I2C_REG_ONLY
  9466. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9467. {
  9468. int rc;
  9469. if (!en) {
  9470. rc = 0;
  9471. goto disable_vcc_i2c_reg_only;
  9472. }
  9473. if (cd->vcc_i2c) {
  9474. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9475. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9476. FT_I2C_VTG_MAX_UV);
  9477. if (rc) {
  9478. dev_err(cd->dev,
  9479. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9480. goto disable_vcc_i2c_reg_only;
  9481. }
  9482. }
  9483. rc = regulator_enable(cd->vcc_i2c);
  9484. if (rc) {
  9485. dev_err(cd->dev,
  9486. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9487. goto disable_vcc_i2c_reg_only;
  9488. }
  9489. }
  9490. return 0;
  9491. disable_vcc_i2c_reg_only:
  9492. if (cd->vcc_i2c) {
  9493. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9494. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9495. FT_I2C_VTG_MAX_UV);
  9496. regulator_disable(cd->vcc_i2c);
  9497. }
  9498. return rc;
  9499. }
  9500. #endif
  9501. #ifdef ENABLE_VDD_REG_ONLY
  9502. static int pt_enable_vdd_regulator(struct pt_core_data *cd, bool en)
  9503. {
  9504. int rc;
  9505. if (!en) {
  9506. rc = 0;
  9507. goto disable_vdd_reg_only;
  9508. }
  9509. if (cd->vdd) {
  9510. if (regulator_count_voltages(cd->vdd) > 0) {
  9511. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9512. FT_VTG_MAX_UV);
  9513. if (rc) {
  9514. dev_err(cd->dev,
  9515. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9516. goto disable_vdd_reg_only;
  9517. }
  9518. }
  9519. rc = regulator_enable(cd->vdd);
  9520. if (rc) {
  9521. dev_err(cd->dev,
  9522. "Regulator vdd enable failed rc=%d\n", rc);
  9523. goto disable_vdd_reg_only;
  9524. }
  9525. }
  9526. return 0;
  9527. disable_vdd_reg_only:
  9528. if (cd->vdd) {
  9529. if (regulator_count_voltages(cd->vdd) > 0)
  9530. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9531. FT_VTG_MAX_UV);
  9532. regulator_disable(cd->vdd);
  9533. }
  9534. return rc;
  9535. }
  9536. #endif
  9537. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9538. {
  9539. int rc;
  9540. if (!en) {
  9541. rc = 0;
  9542. goto disable_vcc_i2c_reg;
  9543. }
  9544. if (cd->vdd) {
  9545. if (regulator_count_voltages(cd->vdd) > 0) {
  9546. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9547. FT_VTG_MAX_UV);
  9548. if (rc) {
  9549. dev_err(cd->dev,
  9550. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9551. goto exit;
  9552. }
  9553. }
  9554. rc = regulator_enable(cd->vdd);
  9555. if (rc) {
  9556. dev_err(cd->dev,
  9557. "Regulator vdd enable failed rc=%d\n", rc);
  9558. goto exit;
  9559. }
  9560. }
  9561. if (cd->vcc_i2c) {
  9562. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9563. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9564. FT_I2C_VTG_MAX_UV);
  9565. if (rc) {
  9566. dev_err(cd->dev,
  9567. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9568. goto disable_vdd_reg;
  9569. }
  9570. }
  9571. rc = regulator_enable(cd->vcc_i2c);
  9572. if (rc) {
  9573. dev_err(cd->dev,
  9574. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9575. goto disable_vdd_reg;
  9576. }
  9577. }
  9578. return 0;
  9579. disable_vcc_i2c_reg:
  9580. if (cd->vcc_i2c) {
  9581. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9582. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9583. FT_I2C_VTG_MAX_UV);
  9584. regulator_disable(cd->vcc_i2c);
  9585. }
  9586. disable_vdd_reg:
  9587. if (cd->vdd) {
  9588. if (regulator_count_voltages(cd->vdd) > 0)
  9589. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9590. FT_VTG_MAX_UV);
  9591. regulator_disable(cd->vdd);
  9592. }
  9593. exit:
  9594. return rc;
  9595. }
  9596. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9597. #define KERNEL_VER_GT_3_19
  9598. #endif
  9599. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9600. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9601. #if defined(CONFIG_PM_SLEEP)
  9602. /*******************************************************************************
  9603. * FUNCTION: pt_core_rt_suspend
  9604. *
  9605. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9606. *
  9607. * RETURN:
  9608. * 0 = success
  9609. * !0 = failure
  9610. *
  9611. * PARAMETERS:
  9612. * *dev - pointer to core device
  9613. ******************************************************************************/
  9614. static int pt_core_rt_suspend(struct device *dev)
  9615. {
  9616. struct pt_core_data *cd = dev_get_drvdata(dev);
  9617. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9618. __func__, cd->core_probe_complete);
  9619. return 0;
  9620. }
  9621. /*******************************************************************************
  9622. * FUNCTION: pt_core_rt_resume
  9623. *
  9624. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9625. *
  9626. * RETURN:
  9627. * 0 = success
  9628. * !0 = failure
  9629. *
  9630. * PARAMETERS:
  9631. * *dev - pointer to core device
  9632. ******************************************************************************/
  9633. static int pt_core_rt_resume(struct device *dev)
  9634. {
  9635. struct pt_core_data *cd = dev_get_drvdata(dev);
  9636. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9637. __func__, cd->core_probe_complete);
  9638. return 0;
  9639. }
  9640. #endif /* CONFIG_PM_SLEEP */
  9641. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9642. #if defined(CONFIG_PM_SLEEP)
  9643. /*******************************************************************************
  9644. * FUNCTION: pt_core_suspend_
  9645. *
  9646. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9647. * pt_core_sleep. This function may disable IRQ during sleep state.
  9648. *
  9649. * RETURN:
  9650. * 0 = success
  9651. * !0 = failure
  9652. *
  9653. * PARAMETERS:
  9654. * *dev - pointer to core device
  9655. ******************************************************************************/
  9656. static int pt_core_suspend_(struct device *dev)
  9657. {
  9658. int rc;
  9659. struct pt_core_data *cd = dev_get_drvdata(dev);
  9660. pt_debug(dev, DL_INFO, "%s: Entering into suspend mode:\n",
  9661. __func__);
  9662. rc = pt_core_sleep(cd);
  9663. if (rc) {
  9664. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9665. return -EAGAIN;
  9666. }
  9667. #ifdef ENABLE_VDD_REG_ONLY
  9668. rc = pt_enable_vdd_regulator(cd, false);
  9669. if (rc) {
  9670. dev_err(dev, "%s: Failed to disable vdd regulators: rc=%d\n",
  9671. __func__, rc);
  9672. }
  9673. #endif
  9674. #ifdef ENABLE_I2C_REG_ONLY
  9675. rc = pt_enable_i2c_regulator(cd, false);
  9676. if (rc) {
  9677. dev_err(dev, "%s: Failed to disable vdd regulators: rc=%d\n",
  9678. __func__, rc);
  9679. }
  9680. #endif
  9681. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9682. return 0;
  9683. /* Required to prevent interrupts before bus awake */
  9684. disable_irq(cd->irq);
  9685. cd->irq_disabled = 1;
  9686. if (device_may_wakeup(dev)) {
  9687. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9688. __func__);
  9689. if (!enable_irq_wake(cd->irq))
  9690. cd->irq_wake = 1;
  9691. } else {
  9692. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9693. __func__);
  9694. }
  9695. return rc;
  9696. }
  9697. /*******************************************************************************
  9698. * FUNCTION: pt_core_suspend
  9699. *
  9700. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9701. * woke up or put to sleep based on Kernel power state even when the display
  9702. * is off based on the check of TTDL core platform flag.
  9703. *
  9704. * RETURN:
  9705. * 0 = success
  9706. * !0 = failure
  9707. *
  9708. * PARAMETERS:
  9709. * *dev - pointer to core device
  9710. ******************************************************************************/
  9711. static int pt_core_suspend(struct device *dev)
  9712. {
  9713. struct pt_core_data *cd = dev_get_drvdata(dev);
  9714. int rc = 0;
  9715. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9716. return 0;
  9717. rc = pt_core_suspend_(dev);
  9718. pt_debug(dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9719. return 0;
  9720. }
  9721. /*******************************************************************************
  9722. * FUNCTION: pt_core_resume_
  9723. *
  9724. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9725. * pt_core_wake. This function may enable IRQ before wake up.
  9726. *
  9727. * RETURN:
  9728. * 0 = success
  9729. * !0 = failure
  9730. *
  9731. * PARAMETERS:
  9732. * *dev - pointer to core device
  9733. ******************************************************************************/
  9734. static int pt_core_resume_(struct device *dev)
  9735. {
  9736. int rc = 0;
  9737. struct pt_core_data *cd = dev_get_drvdata(dev);
  9738. dev_info(dev, "%s: Entering into resume mode:\n",
  9739. __func__);
  9740. #ifdef ENABLE_VDD_REG_ONLY
  9741. rc = pt_enable_vdd_regulator(cd, true);
  9742. if (rc < 0) {
  9743. dev_err(dev, "%s: Failed to enable vdd regulators: rc=%d\n",
  9744. __func__, rc);
  9745. }
  9746. #endif
  9747. #ifdef ENABLE_I2C_REG_ONLY
  9748. rc = pt_enable_i2c_regulator(cd, true);
  9749. if (rc < 0) {
  9750. dev_err(dev, "%s: Failed to enable vdd regulators: rc=%d\n",
  9751. __func__, rc);
  9752. }
  9753. #endif
  9754. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9755. __func__, rc);
  9756. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9757. goto exit;
  9758. /*
  9759. * Bus pm does not call suspend if device runtime suspended
  9760. * This flag is covers that case
  9761. */
  9762. if (cd->irq_disabled) {
  9763. enable_irq(cd->irq);
  9764. cd->irq_disabled = 0;
  9765. }
  9766. if (device_may_wakeup(dev)) {
  9767. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9768. __func__);
  9769. if (cd->irq_wake) {
  9770. disable_irq_wake(cd->irq);
  9771. cd->irq_wake = 0;
  9772. }
  9773. } else {
  9774. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9775. __func__);
  9776. }
  9777. exit:
  9778. rc = pt_core_wake(cd);
  9779. if (rc) {
  9780. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9781. __func__, rc);
  9782. return -EAGAIN;
  9783. }
  9784. return 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. do {
  9807. retry_count--;
  9808. rc = pt_core_resume_(pt_data->dev);
  9809. if (rc < 0)
  9810. pt_debug(pt_data->dev, DL_ERROR,
  9811. "%s: Error on wake\n", __func__);
  9812. } while (retry_count && rc < 0);
  9813. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9814. rc = pt_core_easywake_on(pt_data);
  9815. if (rc < 0) {
  9816. pt_debug(pt_data->dev, DL_ERROR,
  9817. "%s: Error on enable touch to wake key\n",
  9818. __func__);
  9819. return;
  9820. }
  9821. pt_data->fb_state = FB_OFF;
  9822. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9823. #endif
  9824. }
  9825. /*******************************************************************************
  9826. * FUNCTION: pt_core_resume
  9827. *
  9828. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9829. * along with kernel power state even the display is off based on the check of
  9830. * TTDL core platform flag.
  9831. *
  9832. * RETURN:
  9833. * 0 = success
  9834. * !0 = failure
  9835. *
  9836. * PARAMETERS:
  9837. * *dev - pointer to core device
  9838. ******************************************************************************/
  9839. static int pt_core_resume(struct device *dev)
  9840. {
  9841. struct pt_core_data *cd = dev_get_drvdata(dev);
  9842. int rc = 0;
  9843. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9844. return 0;
  9845. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9846. pt_debug(cd->dev, DL_ERROR, "%s End\n", __func__);
  9847. return rc;
  9848. }
  9849. #endif
  9850. #ifdef NEED_SUSPEND_NOTIFIER
  9851. /*******************************************************************************
  9852. * FUNCTION: pt_pm_notifier
  9853. *
  9854. * SUMMARY: This function is registered to notifier chain and will perform
  9855. * suspend operation if match event PM_SUSPEND_PREPARE.
  9856. *
  9857. * RETURN:
  9858. * 0 = success
  9859. * !0 = failure
  9860. *
  9861. * PARAMETERS:
  9862. * *nb - pointer to notifier_block structure
  9863. * action - notifier event type
  9864. * *data - void pointer
  9865. ******************************************************************************/
  9866. static int pt_pm_notifier(struct notifier_block *nb,
  9867. unsigned long action, void *data)
  9868. {
  9869. struct pt_core_data *cd = container_of(nb,
  9870. struct pt_core_data, pm_notifier);
  9871. if (action == PM_SUSPEND_PREPARE) {
  9872. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9873. __func__);
  9874. /*
  9875. * If PM runtime is not suspended, either call runtime
  9876. * PM suspend callback or wait until it finishes
  9877. */
  9878. if (!pm_runtime_suspended(cd->dev))
  9879. pm_runtime_suspend(cd->dev);
  9880. (void) pt_core_suspend(cd->dev);
  9881. }
  9882. return NOTIFY_DONE;
  9883. }
  9884. #endif
  9885. const struct dev_pm_ops pt_pm_ops = {
  9886. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9887. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9888. NULL)
  9889. };
  9890. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9891. /*******************************************************************************
  9892. * FUNCTION: _pt_request_pip2_enter_bl
  9893. *
  9894. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9895. * XRES pin or a soft reset.
  9896. *
  9897. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9898. * the WD active could cause this function to fail!
  9899. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9900. * will not try to determine the current mode but will proceed with
  9901. * resetting the DUT and entering the BL.
  9902. *
  9903. * NOTE: The definition of result code:
  9904. * PT_ENTER_BL_PASS (0)
  9905. * PT_ENTER_BL_ERROR (1)
  9906. * PT_ENTER_BL_RESET_FAIL (2)
  9907. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9908. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9909. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9910. *
  9911. * RETURNS:
  9912. * 0 = success
  9913. * !0 = failure
  9914. *
  9915. *
  9916. * PARAMETERS:
  9917. * *dev - pointer to device structure
  9918. * *start_mode - pointer to the mode the DUT was in when this function
  9919. * starts
  9920. * *result - pointer to store the result when to enter BL
  9921. ******************************************************************************/
  9922. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9923. {
  9924. int rc = 0;
  9925. int t;
  9926. int tmp_result = PT_ENTER_BL_ERROR;
  9927. int flash_info_retry = 2;
  9928. u8 mode = PT_MODE_UNKNOWN;
  9929. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9930. u8 read_buf[32];
  9931. u16 actual_read_len;
  9932. struct pt_core_data *cd = dev_get_drvdata(dev);
  9933. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9934. u8 time = 0;
  9935. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9936. if (cd->watchdog_enabled) {
  9937. pt_debug(dev, DL_WARN,
  9938. "%s: Watchdog must be stopped before entering BL\n",
  9939. __func__);
  9940. goto exit;
  9941. }
  9942. cancel_work_sync(&cd->enum_work);
  9943. cancel_work_sync(&cd->watchdog_work);
  9944. /* if undefined assume operational/test to bypass all checks */
  9945. if (*start_mode == PT_MODE_IGNORE) {
  9946. mode = PT_MODE_OPERATIONAL;
  9947. sys_mode = FW_SYS_MODE_TEST;
  9948. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9949. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9950. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9951. if (rc) {
  9952. pt_debug(dev, DL_ERROR,
  9953. "%s: Get mode failed, mode unknown\n",
  9954. __func__);
  9955. }
  9956. *start_mode = mode;
  9957. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9958. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9959. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9960. sys_mode = FW_SYS_MODE_SCANNING;
  9961. }
  9962. _retry:
  9963. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9964. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9965. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9966. switch (mode) {
  9967. case PT_MODE_UNKNOWN:
  9968. /*
  9969. * When the mode could not be determined the DUT could be
  9970. * in App mode running corrupted FW or FW that is not
  9971. * responding to the mode request, assume no communication
  9972. * and do a hard reset
  9973. */
  9974. mutex_lock(&cd->system_lock);
  9975. cd->startup_status = STARTUP_STATUS_START;
  9976. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9977. mutex_unlock(&cd->system_lock);
  9978. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9979. if (rc) {
  9980. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9981. goto exit;
  9982. }
  9983. break;
  9984. case PT_MODE_OPERATIONAL:
  9985. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9986. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9987. __func__);
  9988. rc = pt_pip_suspend_scanning_(cd);
  9989. if (rc) {
  9990. /*
  9991. * Print to log but don't exit, the FW could be
  9992. * running but be hung or fail to respond to
  9993. * this request
  9994. */
  9995. pt_debug(dev, DL_ERROR,
  9996. "%s Suspend Scan Failed\n", __func__);
  9997. }
  9998. /* sleep to allow the suspend scan to be processed */
  9999. usleep_range(1000, 2000);
  10000. }
  10001. mutex_lock(&cd->system_lock);
  10002. cd->startup_status = STARTUP_STATUS_START;
  10003. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10004. mutex_unlock(&cd->system_lock);
  10005. /* Reset device to enter the BL */
  10006. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10007. if (rc) {
  10008. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10009. goto exit;
  10010. }
  10011. break;
  10012. case PT_MODE_BOOTLOADER:
  10013. /* Do nothing as we are already in the BL */
  10014. tmp_result = PT_ENTER_BL_PASS;
  10015. goto exit;
  10016. default:
  10017. /* Should NEVER get here */
  10018. tmp_result = PT_ENTER_BL_ERROR;
  10019. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10020. goto exit;
  10021. }
  10022. if (!cd->flashless_dut &&
  10023. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10024. /*
  10025. * Sending the special "Host Mode" command will instruct the
  10026. * BL to not execute the FW it has loaded into RAM.
  10027. * The command must be sent within a 40ms window from releasing
  10028. * the XRES pin. If the messages is sent too early it will NAK,
  10029. * so keep sending it every 2ms until it is accepted by the BL.
  10030. * A no-flash DUT does not require this command as there is no
  10031. * FW for the BL to load and execute.
  10032. */
  10033. usleep_range(4000, 6000);
  10034. pt_debug(cd->dev, DL_INFO,
  10035. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10036. __func__, (int)sizeof(host_mode_cmd));
  10037. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10038. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10039. rc = 1;
  10040. while (rc && time < 34) {
  10041. rc = pt_adap_write_read_specific(cd, 4,
  10042. host_mode_cmd, NULL);
  10043. usleep_range(1800, 2000);
  10044. time += 2;
  10045. }
  10046. /* Sleep to allow the BL to come up */
  10047. usleep_range(1000, 2000);
  10048. }
  10049. /*
  10050. * To avoid the case that next PIP command can be confused by BL/FW
  10051. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10052. * followed by "wakeup event" function can lower the failure rate.
  10053. */
  10054. t = wait_event_timeout(cd->wait_q,
  10055. ((cd->startup_status != STARTUP_STATUS_START)
  10056. && (cd->hid_reset_cmd_state == 0)),
  10057. msecs_to_jiffies(300));
  10058. if (IS_TMO(t)) {
  10059. pt_debug(cd->dev, DL_ERROR,
  10060. "%s: TMO waiting for BL sentinel\n", __func__);
  10061. }
  10062. /* Check if device is now in BL mode */
  10063. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10064. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10065. cd->startup_status);
  10066. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10067. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10068. mutex_lock(&cd->system_lock);
  10069. cd->pip2_prot_active = true;
  10070. cd->mode = PT_MODE_BOOTLOADER;
  10071. mutex_unlock(&cd->system_lock);
  10072. tmp_result = PT_ENTER_BL_PASS;
  10073. } else {
  10074. /*
  10075. * If the device doesn't enter BL mode as expected and rc is
  10076. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10077. * the function should return an error code to indicate this
  10078. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10079. */
  10080. if (!rc)
  10081. rc = -EINVAL;
  10082. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10083. mutex_lock(&cd->system_lock);
  10084. cd->mode = mode;
  10085. mutex_unlock(&cd->system_lock);
  10086. pt_debug(dev, DL_ERROR,
  10087. "%s ERROR: Not in BL as expected", __func__);
  10088. }
  10089. exit:
  10090. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10091. /* Check to get (buffered) flash information */
  10092. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10093. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10094. read_buf, &actual_read_len);
  10095. if (!rc) {
  10096. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10097. pt_debug(
  10098. dev, DL_WARN,
  10099. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10100. __func__,
  10101. read_buf[PIP2_RESP_BODY_OFFSET]);
  10102. /*
  10103. * If the BL was unable to cache the correct
  10104. * values when entering the first time due to
  10105. * the Flash part not having been powered up
  10106. * long enough, re-enter the BL to trigger the
  10107. * BL to re-attempt to cache the values.
  10108. */
  10109. if (flash_info_retry-- > 0) {
  10110. mode = PT_MODE_UNKNOWN;
  10111. pt_debug(dev, DL_WARN,
  10112. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10113. __func__, flash_info_retry);
  10114. goto _retry;
  10115. } else {
  10116. pt_debug(dev, DL_WARN,
  10117. "%s Manufacturer ID Unknown\n",
  10118. __func__);
  10119. tmp_result = PT_ENTER_BL_PASS;
  10120. }
  10121. }
  10122. } else {
  10123. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10124. pt_debug(
  10125. dev, DL_ERROR,
  10126. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10127. __func__);
  10128. }
  10129. }
  10130. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10131. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10132. "SUPPRESS");
  10133. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10134. if (result)
  10135. *result = tmp_result;
  10136. return rc;
  10137. }
  10138. /*******************************************************************************
  10139. * FUNCTION: _pt_pip2_file_open
  10140. *
  10141. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10142. *
  10143. * NOTE: The DUT must be in BL mode for this command to work
  10144. *
  10145. * RETURNS:
  10146. * <0 = Error
  10147. * >0 = file handle opened
  10148. *
  10149. * PARAMETERS:
  10150. * *dev - pointer to device structure
  10151. * file_no - PIP2 file number to open
  10152. ******************************************************************************/
  10153. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10154. {
  10155. int ret = 0;
  10156. u16 status;
  10157. u16 actual_read_len;
  10158. u8 file_handle;
  10159. u8 data[2];
  10160. u8 read_buf[10];
  10161. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10162. data[0] = file_no;
  10163. ret = _pt_request_pip2_send_cmd(dev,
  10164. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10165. data, 1, read_buf, &actual_read_len);
  10166. if (ret) {
  10167. pt_debug(dev, DL_ERROR,
  10168. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10169. __func__, file_no);
  10170. return -PIP2_RSP_ERR_NOT_OPEN;
  10171. }
  10172. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10173. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10174. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10175. (file_handle != file_no)) {
  10176. pt_debug(dev, DL_ERROR,
  10177. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10178. __func__, status, file_handle);
  10179. return -status;
  10180. }
  10181. return file_handle;
  10182. }
  10183. /*******************************************************************************
  10184. * FUNCTION: _pt_pip2_file_close
  10185. *
  10186. * SUMMARY: Using the BL PIP2 commands close a file
  10187. *
  10188. * NOTE: The DUT must be in BL mode for this command to work
  10189. *
  10190. * RETURNS:
  10191. * <0 = Error
  10192. * >0 = file handle closed
  10193. *
  10194. * PARAMETERS:
  10195. * *dev - pointer to device structure
  10196. * file_handle - handle to the file to be closed
  10197. ******************************************************************************/
  10198. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10199. {
  10200. int ret = 0;
  10201. u16 status;
  10202. u16 actual_read_len;
  10203. u8 data[2];
  10204. u8 read_buf[32];
  10205. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10206. data[0] = file_handle;
  10207. ret = _pt_request_pip2_send_cmd(dev,
  10208. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10209. data, 1, read_buf, &actual_read_len);
  10210. if (ret) {
  10211. pt_debug(dev, DL_ERROR,
  10212. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10213. __func__, file_handle);
  10214. return -ETIME;
  10215. }
  10216. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10217. if (status != 0x00) {
  10218. pt_debug(dev, DL_ERROR,
  10219. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10220. __func__, status, file_handle);
  10221. return -status;
  10222. }
  10223. return file_handle;
  10224. }
  10225. /*******************************************************************************
  10226. * FUNCTION: _pt_pip2_file_erase
  10227. *
  10228. * SUMMARY: Using the BL PIP2 commands erase a file
  10229. *
  10230. * NOTE: The DUT must be in BL mode for this command to work
  10231. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10232. * one retry is attempted for each sector in a file.
  10233. *
  10234. * RETURNS:
  10235. * <0 = Error
  10236. * >0 = file handle closed
  10237. *
  10238. * PARAMETERS:
  10239. * *dev - pointer to device structure
  10240. * file_handle - handle to the file to be erased
  10241. * *status - PIP2 erase status code
  10242. ******************************************************************************/
  10243. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10244. {
  10245. int ret = 0;
  10246. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10247. int retry = 1;
  10248. u16 actual_read_len;
  10249. u8 data[2];
  10250. u8 read_buf[10];
  10251. struct pt_core_data *cd = dev_get_drvdata(dev);
  10252. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10253. data[0] = file_handle;
  10254. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10255. *status = PIP2_RSP_ERR_TIMEOUT;
  10256. /* Increase waiting time for large file erase */
  10257. mutex_lock(&cd->system_lock);
  10258. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10259. mutex_unlock(&cd->system_lock);
  10260. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10261. ret = _pt_request_pip2_send_cmd(dev,
  10262. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10263. data, 2, read_buf, &actual_read_len);
  10264. if (ret)
  10265. break;
  10266. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10267. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10268. #ifdef TTDL_DIAGNOSTICS
  10269. cd->file_erase_timeout_count++;
  10270. #endif
  10271. pt_debug(dev, DL_WARN,
  10272. "%s: ERASE timeout %d for file = %d\n",
  10273. __func__, retry, file_handle);
  10274. }
  10275. retry++;
  10276. }
  10277. mutex_lock(&cd->system_lock);
  10278. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10279. mutex_unlock(&cd->system_lock);
  10280. if (ret) {
  10281. pt_debug(dev, DL_ERROR,
  10282. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10283. __func__, ret, file_handle);
  10284. return -EIO;
  10285. }
  10286. if (*status != 0x00) {
  10287. pt_debug(dev, DL_ERROR,
  10288. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10289. __func__, *status, file_handle);
  10290. return -EIO;
  10291. }
  10292. return file_handle;
  10293. }
  10294. /*******************************************************************************
  10295. * FUNCTION: _pt_pip2_file_read
  10296. *
  10297. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10298. *
  10299. * NOTE: The DUT must be in BL mode for this command to work
  10300. *
  10301. * RETURNS:
  10302. * <0 = Error
  10303. * >0 = number of bytes read
  10304. *
  10305. * PARAMETERS:
  10306. * *dev - pointer to device structure
  10307. * file_handle - File handle to read from
  10308. * num_bytes - number of bytes to read
  10309. ******************************************************************************/
  10310. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10311. u8 *read_buf)
  10312. {
  10313. int ret = 0;
  10314. u16 status;
  10315. u16 actual_read_len;
  10316. u8 data[3];
  10317. data[0] = file_handle;
  10318. data[1] = (num_bytes & 0x00FF);
  10319. data[2] = (num_bytes & 0xFF00) >> 8;
  10320. ret = _pt_request_pip2_send_cmd(dev,
  10321. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10322. data, 3, read_buf, &actual_read_len);
  10323. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10324. if (ret || ((status != 0x00) && (status != 0x03))) {
  10325. pt_debug(dev, DL_ERROR,
  10326. "%s File open failure with error code = %d\n",
  10327. __func__, status);
  10328. return -EPERM;
  10329. }
  10330. ret = num_bytes;
  10331. return ret;
  10332. }
  10333. /*******************************************************************************
  10334. * FUNCTION: _pt_read_us_file
  10335. *
  10336. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10337. * then read the entire file.
  10338. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10339. *
  10340. * RETURN:
  10341. * 0 = success
  10342. * !0 = failure
  10343. *
  10344. * PARAMETERS:
  10345. * *file_path - pointer to the file path
  10346. * *buf - pointer to the buffer to store the file contents
  10347. * *size - pointer to the size of the file
  10348. ******************************************************************************/
  10349. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10350. {
  10351. struct file *filp = NULL;
  10352. struct inode *inode = NULL;
  10353. unsigned int file_len = 0;
  10354. unsigned int read_len = 0;
  10355. mm_segment_t oldfs;
  10356. int rc = 0;
  10357. if (file_path == NULL || buf == NULL) {
  10358. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10359. return -EINVAL;
  10360. }
  10361. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10362. oldfs = get_fs();
  10363. set_fs(KERNEL_DS);
  10364. filp = filp_open(file_path, O_RDONLY, 0400);
  10365. if (IS_ERR(filp)) {
  10366. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10367. file_path);
  10368. rc = -ENOENT;
  10369. goto err;
  10370. }
  10371. if (filp->f_op == NULL) {
  10372. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10373. __func__);
  10374. rc = -EINVAL;
  10375. goto exit;
  10376. }
  10377. inode = filp->f_path.dentry->d_inode;
  10378. if (inode == NULL) {
  10379. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10380. __func__);
  10381. rc = -EINVAL;
  10382. goto exit;
  10383. }
  10384. file_len = i_size_read(inode->i_mapping->host);
  10385. if (file_len == 0) {
  10386. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10387. __func__, file_len);
  10388. rc = -EINVAL;
  10389. goto exit;
  10390. }
  10391. if (*size == 0)
  10392. read_len = file_len;
  10393. else
  10394. read_len = *size;
  10395. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10396. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10397. __func__, read_len);
  10398. rc = -EINVAL;
  10399. goto exit;
  10400. }
  10401. filp->private_data = inode->i_private;
  10402. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10403. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10404. rc = -EINVAL;
  10405. goto exit;
  10406. }
  10407. *size = read_len;
  10408. exit:
  10409. if (filp_close(filp, NULL) != 0)
  10410. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10411. err:
  10412. set_fs(oldfs);
  10413. return rc;
  10414. }
  10415. /*******************************************************************************
  10416. * FUNCTION: _pt_request_pip2_bin_hdr
  10417. *
  10418. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10419. * in the case of a flashless DUT, and parse the contents
  10420. *
  10421. * RETURNS:
  10422. * 0 = Success
  10423. * !0 = Error condition
  10424. *
  10425. * PARAMETERS:
  10426. * *dev - pointer to device structure
  10427. ******************************************************************************/
  10428. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10429. {
  10430. struct pt_core_data *cd = dev_get_drvdata(dev);
  10431. u8 file_handle;
  10432. u8 read_buf[255];
  10433. u8 hdr_len = 0;
  10434. u8 i;
  10435. int bytes_read;
  10436. int read_size;
  10437. int ret = 0;
  10438. int rc = 0;
  10439. bool load_hdr_struct = false;
  10440. if (cd->flashless_dut) {
  10441. read_size = sizeof(read_buf);
  10442. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10443. read_buf, &read_size);
  10444. if (rc) {
  10445. ret = rc;
  10446. pt_debug(dev, DL_ERROR,
  10447. "%s Failed to read fw image from US, rc=%d\n",
  10448. __func__, rc);
  10449. goto exit;
  10450. }
  10451. load_hdr_struct = true;
  10452. hdr_len = read_buf[0];
  10453. i = 0;
  10454. } else {
  10455. if (cd->mode != PT_MODE_BOOTLOADER) {
  10456. ret = -EPERM;
  10457. goto exit;
  10458. }
  10459. /* Open the bin file in Flash */
  10460. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10461. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10462. if (file_handle != PIP2_FW_FILE) {
  10463. ret = -ENOENT;
  10464. pt_debug(dev, DL_ERROR,
  10465. "%s Failed to open bin file\n", __func__);
  10466. goto exit;
  10467. }
  10468. /* Read the header length from the file */
  10469. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10470. read_size = 1;
  10471. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10472. read_buf);
  10473. if (bytes_read != read_size) {
  10474. ret = -EX_ERR_FREAD;
  10475. pt_debug(dev, DL_ERROR,
  10476. "%s Failed to bin file header len\n", __func__);
  10477. goto exit_close_file;
  10478. }
  10479. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10480. if (hdr_len == 0xFF) {
  10481. ret = -EX_ERR_FLEN;
  10482. pt_debug(dev, DL_ERROR,
  10483. "%s Bin header len is invalid\n", __func__);
  10484. goto exit_close_file;
  10485. }
  10486. /* Read the rest of the header from the bin file */
  10487. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10488. memset(read_buf, 0, sizeof(read_buf));
  10489. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10490. read_buf);
  10491. if (bytes_read != hdr_len) {
  10492. ret = -EX_ERR_FREAD;
  10493. pt_debug(dev, DL_ERROR,
  10494. "%s Failed to read bin file\n", __func__);
  10495. goto exit_close_file;
  10496. }
  10497. load_hdr_struct = true;
  10498. exit_close_file:
  10499. /* Close the file */
  10500. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10501. ret = -EX_ERR_FCLOSE;
  10502. pt_debug(dev, DL_ERROR,
  10503. "%s Failed to close bin file\n", __func__);
  10504. }
  10505. /*
  10506. * The length was already read so subtract 1 to make the rest of
  10507. * the offsets match the spec
  10508. */
  10509. i = PIP2_RESP_BODY_OFFSET - 1;
  10510. }
  10511. if (load_hdr_struct) {
  10512. hdr->length = hdr_len;
  10513. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10514. hdr->fw_major = (read_buf[i+3]);
  10515. hdr->fw_minor = (read_buf[i+4]);
  10516. hdr->fw_crc = (read_buf[i+5] << 24) |
  10517. (read_buf[i+6] << 16) |
  10518. (read_buf[i+7] << 8) |
  10519. (read_buf[i+8]);
  10520. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10521. (read_buf[i+10] << 16) |
  10522. (read_buf[i+11] << 8) |
  10523. (read_buf[i+12]);
  10524. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10525. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10526. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10527. if (hdr_len >= 22) {
  10528. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10529. (read_buf[i+20] << 16) |
  10530. (read_buf[i+21] << 8) |
  10531. (read_buf[i+22]);
  10532. } else {
  10533. hdr->hex_file_size = 0;
  10534. }
  10535. }
  10536. exit:
  10537. return ret;
  10538. }
  10539. /*******************************************************************************
  10540. * FUNCTION: _pt_pip2_file_get_stats
  10541. *
  10542. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10543. * opened file
  10544. *
  10545. * NOTE: The DUT must be in BL mode for this command to work
  10546. *
  10547. * RETURNS:
  10548. * !0 = Error
  10549. * 0 = Success
  10550. *
  10551. * PARAMETERS:
  10552. * *dev - pointer to device structure
  10553. * file_handle - File handle to read from
  10554. * *address - pointer to store address of file
  10555. * *file_size _ pointer to store size of file
  10556. ******************************************************************************/
  10557. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10558. u32 *file_size)
  10559. {
  10560. int ret = 1;
  10561. u16 status;
  10562. u16 actual_read_len;
  10563. u8 data[2];
  10564. u8 read_buf[16];
  10565. data[0] = file_handle;
  10566. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10567. ret = _pt_request_pip2_send_cmd(dev,
  10568. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10569. data, 2, read_buf, &actual_read_len);
  10570. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10571. if (ret || (status != 0x00)) {
  10572. pt_debug(dev, DL_ERROR,
  10573. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10574. __func__, status, file_handle, ret);
  10575. ret = -EIO;
  10576. goto exit;
  10577. }
  10578. pt_debug(dev, DL_DEBUG,
  10579. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10580. if (address) {
  10581. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10582. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10583. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10584. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10585. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10586. __func__, *address);
  10587. }
  10588. if (file_size) {
  10589. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10590. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10591. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10592. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10593. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10594. __func__, *file_size);
  10595. }
  10596. exit:
  10597. return ret;
  10598. }
  10599. /*******************************************************************************
  10600. * FUNCTION: _pt_pip2_file_seek_offset
  10601. *
  10602. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10603. * opened file
  10604. *
  10605. * NOTE: The DUT must be in BL mode for this command to work
  10606. * NOTE: File open/erase command can reset the offset
  10607. *
  10608. * RETURNS:
  10609. * !0 = Error
  10610. * 0 = Success
  10611. *
  10612. * PARAMETERS:
  10613. * *dev - pointer to device structure
  10614. * file_handle - File handle to read from
  10615. * read_offset - read offset of file
  10616. * write_offset - write offset of file
  10617. ******************************************************************************/
  10618. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10619. u32 read_offset, u32 write_offset)
  10620. {
  10621. int ret = 1;
  10622. u16 status;
  10623. u16 actual_read_len;
  10624. u8 data[10];
  10625. u8 read_buf[16];
  10626. data[0] = file_handle;
  10627. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10628. data[2] = 0xff & read_offset;
  10629. data[3] = 0xff & (read_offset >> 8);
  10630. data[4] = 0xff & (read_offset >> 16);
  10631. data[5] = 0xff & (read_offset >> 24);
  10632. data[6] = 0xff & write_offset;
  10633. data[7] = 0xff & (write_offset >> 8);
  10634. data[8] = 0xff & (write_offset >> 16);
  10635. data[9] = 0xff & (write_offset >> 24);
  10636. ret = _pt_request_pip2_send_cmd(dev,
  10637. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10638. data, 10, read_buf, &actual_read_len);
  10639. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10640. if (ret || (status != 0x00)) {
  10641. pt_debug(dev, DL_ERROR,
  10642. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10643. __func__, status, ret, file_handle);
  10644. ret = -EIO;
  10645. }
  10646. return ret;
  10647. }
  10648. /*******************************************************************************
  10649. * FUNCTION: _pt_pip2_file_crc
  10650. *
  10651. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10652. * the file.
  10653. *
  10654. * NOTE: The DUT must be in BL mode for this command to work
  10655. * NOTE: This command only can be used for BL version 1.8 or greater.
  10656. * BL version 1.8 added this change according to PGV-173.
  10657. *
  10658. * RETURNS:
  10659. * !0 = Error
  10660. * 0 = Success
  10661. *
  10662. * PARAMETERS:
  10663. * *dev - pointer to device structure
  10664. * file_handle - File handle to read from
  10665. * offset - start offset for CRC calculation
  10666. * length - number of bytes to calculate CRC over
  10667. * read_buf - pointer to the read buffer
  10668. ******************************************************************************/
  10669. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10670. u32 offset, u32 length, u8 *read_buf)
  10671. {
  10672. int rc = 1;
  10673. u16 actual_read_len;
  10674. u8 data[10];
  10675. data[0] = file_handle;
  10676. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10677. data[2] = 0xff & offset;
  10678. data[3] = 0xff & (offset >> 8);
  10679. data[4] = 0xff & (offset >> 16);
  10680. data[5] = 0xff & (offset >> 24);
  10681. data[6] = 0xff & length;
  10682. data[7] = 0xff & (length >> 8);
  10683. data[8] = 0xff & (length >> 16);
  10684. data[9] = 0xff & (length >> 24);
  10685. rc = _pt_request_pip2_send_cmd(dev,
  10686. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10687. data, 10, read_buf, &actual_read_len);
  10688. if (rc)
  10689. pt_debug(dev, DL_ERROR,
  10690. "%s Return FILE_CRC failure, rc = %d\n",
  10691. __func__, rc);
  10692. return rc;
  10693. }
  10694. /*******************************************************************************
  10695. * FUNCTION: pt_pip2_ping_test
  10696. *
  10697. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10698. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10699. * response payload matches what was sent.
  10700. * The max payload size is 247:
  10701. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10702. *
  10703. * RETURN:
  10704. * 0 = success
  10705. * !0 = failure
  10706. *
  10707. * PARAMETERS:
  10708. * *dev - pointer to device structure
  10709. * *attr - pointer to device attributes
  10710. * *buf - pointer to output buffer
  10711. ******************************************************************************/
  10712. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10713. {
  10714. u16 actual_read_len;
  10715. u8 *read_buf = NULL;
  10716. u8 *data = NULL;
  10717. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10718. int i = 1;
  10719. int j = 0;
  10720. int rc = 0;
  10721. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10722. if (!read_buf)
  10723. goto ping_test_exit;
  10724. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10725. if (!data)
  10726. goto ping_test_exit;
  10727. /* Load data payload with an array of walking 1's */
  10728. for (i = 0; i < 255; i++)
  10729. data[i] = 0x01 << (i % 8);
  10730. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10731. for (i = 0; i <= max_bytes; i++) {
  10732. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10733. PIP2_CMD_ID_PING, data, i, read_buf,
  10734. &actual_read_len);
  10735. if (rc) {
  10736. pt_debug(dev, DL_ERROR,
  10737. "%s: PING failed with %d byte payload\n",
  10738. __func__, i);
  10739. break;
  10740. }
  10741. /* Verify data returned matches data sent */
  10742. for (j = 0; j < i; j++) {
  10743. if (read_buf[data_offset + j] != data[j]) {
  10744. pt_debug(dev, DL_DEBUG,
  10745. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10746. __func__, i, j, data[j], j,
  10747. read_buf[data_offset + j]);
  10748. goto ping_test_exit;
  10749. }
  10750. }
  10751. }
  10752. ping_test_exit:
  10753. *last_packet_size = i - 1;
  10754. kfree(read_buf);
  10755. kfree(data);
  10756. return rc;
  10757. }
  10758. /*******************************************************************************
  10759. * FUNCTION: _pt_ic_parse_input_hex
  10760. *
  10761. * SUMMARY: Parse a char data array as space delimited hex values into
  10762. * an int array.
  10763. *
  10764. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10765. * buffer to store data is "u32". This function is still needed by the
  10766. * "command" sysfs node because the buffer type to store data is "u8".
  10767. *
  10768. * RETURN: Length of parsed data
  10769. *
  10770. * PARAMETERS:
  10771. * *dev - pointer to device structure
  10772. * *buf - pointer to buffer that holds the input array to parse
  10773. * buf_size - size of buf
  10774. * *ic_buf - pointer to array to store parsed data
  10775. * ic_buf_size - max size of ic_buf
  10776. ******************************************************************************/
  10777. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10778. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10779. {
  10780. const char *pbuf = buf;
  10781. unsigned long value;
  10782. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10783. u32 i = 0;
  10784. u32 j;
  10785. int last = 0;
  10786. int ret;
  10787. pt_debug(dev, DL_DEBUG,
  10788. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10789. __func__, pbuf, buf, buf_size, "scan buf size",
  10790. PT_MAX_PIP2_MSG_SIZE, buf);
  10791. while (pbuf <= (buf + buf_size)) {
  10792. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10793. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10794. __func__, "Max cmd size exceeded", i,
  10795. PT_MAX_PIP2_MSG_SIZE);
  10796. return -EINVAL;
  10797. }
  10798. if (i >= ic_buf_size) {
  10799. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10800. __func__, "Buffer size exceeded", i,
  10801. ic_buf_size);
  10802. return -EINVAL;
  10803. }
  10804. while (((*pbuf == ' ') || (*pbuf == ','))
  10805. && (pbuf < (buf + buf_size))) {
  10806. last = *pbuf;
  10807. pbuf++;
  10808. }
  10809. if (pbuf >= (buf + buf_size))
  10810. break;
  10811. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10812. if ((last == ',') && (*pbuf == ',')) {
  10813. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10814. __func__, "Invalid data format.");
  10815. return -EINVAL;
  10816. }
  10817. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10818. && (pbuf < (buf + buf_size))
  10819. && (*pbuf != ' ')
  10820. && (*pbuf != ','); j++) {
  10821. last = *pbuf;
  10822. scan_buf[j] = *pbuf++;
  10823. }
  10824. ret = kstrtoul(scan_buf, 16, &value);
  10825. if (ret < 0) {
  10826. pt_debug(dev, DL_ERROR,
  10827. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10828. "Invalid data format. ", scan_buf,
  10829. "Use \"0xHH,...,0xHH\"", " instead.",
  10830. i, ret);
  10831. return ret;
  10832. }
  10833. ic_buf[i] = value;
  10834. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10835. __func__, i, value);
  10836. i++;
  10837. }
  10838. return i;
  10839. }
  10840. /*******************************************************************************
  10841. * FUNCTION: _pt_ic_parse_input
  10842. *
  10843. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10844. * hex values or dec values into an int array with the following rules:
  10845. * 1) Hex values must have a "0x" prefix for each element or the first element
  10846. * only
  10847. * 2) Dec values do not have any prefix
  10848. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10849. * string
  10850. *
  10851. * RETURN: Number of values parsed
  10852. *
  10853. * PARAMETERS:
  10854. * *dev - pointer to device structure
  10855. * *buf - pointer to buffer that holds the input array to parse
  10856. * buf_size - size of buf
  10857. * *out_buf - pointer to array to store parsed data
  10858. * out_buf_size - max size of buffer to be stored
  10859. ******************************************************************************/
  10860. static int _pt_ic_parse_input(struct device *dev,
  10861. const char *buf, size_t buf_size,
  10862. u32 *out_buf, size_t out_buf_size)
  10863. {
  10864. const char *pbuf = buf;
  10865. unsigned long value;
  10866. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10867. u32 i = 0;
  10868. u32 j;
  10869. int last = 0;
  10870. int ret = 0;
  10871. u8 str_base = 0;
  10872. pt_debug(dev, DL_DEBUG,
  10873. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10874. __func__, buf_size, out_buf_size, "scan buf size",
  10875. PT_MAX_PIP2_MSG_SIZE, buf);
  10876. while (pbuf <= (buf + buf_size)) {
  10877. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10878. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10879. __func__, "Max cmd size exceeded", i,
  10880. PT_MAX_PIP2_MSG_SIZE);
  10881. ret = -EINVAL;
  10882. goto error;
  10883. }
  10884. if (i >= out_buf_size) {
  10885. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10886. __func__, "Buffer size exceeded", i,
  10887. out_buf_size);
  10888. ret = -EINVAL;
  10889. goto error;
  10890. }
  10891. while (((*pbuf == ' ') || (*pbuf == ','))
  10892. && (pbuf < (buf + buf_size))) {
  10893. last = *pbuf;
  10894. pbuf++;
  10895. }
  10896. if (pbuf >= (buf + buf_size))
  10897. break;
  10898. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10899. if ((last == ',') && (*pbuf == ',')) {
  10900. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10901. __func__, "Invalid data format.");
  10902. ret = -EINVAL;
  10903. goto error;
  10904. }
  10905. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10906. && (pbuf < (buf + buf_size))
  10907. && (*pbuf != ' ')
  10908. && (*pbuf != ','); j++) {
  10909. last = *pbuf;
  10910. scan_buf[j] = *pbuf++;
  10911. }
  10912. if (i == 0) {
  10913. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10914. (strncmp(scan_buf, "0X", 2) == 0))
  10915. str_base = 16;
  10916. else
  10917. str_base = 10;
  10918. } else {
  10919. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10920. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10921. (str_base == 10)) {
  10922. pt_debug(dev, DL_ERROR,
  10923. "%s: Decimal and Heximal data mixed\n",
  10924. __func__);
  10925. ret = -EINVAL;
  10926. goto error;
  10927. }
  10928. }
  10929. ret = kstrtoul(scan_buf, str_base, &value);
  10930. if (ret < 0) {
  10931. pt_debug(dev, DL_ERROR,
  10932. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10933. "Invalid data format. ", scan_buf,
  10934. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10935. " instead.", i, ret);
  10936. goto error;
  10937. }
  10938. out_buf[i] = value;
  10939. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10940. __func__, i, value, value);
  10941. i++;
  10942. }
  10943. ret = i;
  10944. error:
  10945. return ret;
  10946. }
  10947. #ifdef TTHE_TUNER_SUPPORT
  10948. /*******************************************************************************
  10949. * FUNCTION: tthe_debugfs_open
  10950. *
  10951. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10952. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10953. * printing a large number of fingers and sensor data without overflowing
  10954. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10955. * even multiple of PAGE_SIZE
  10956. *
  10957. * RETURN:
  10958. * 0 = success
  10959. * !0 = failure
  10960. *
  10961. * PARAMETERS:
  10962. * *inode - file inode number
  10963. * *filp - file pointer to debugfs file
  10964. ******************************************************************************/
  10965. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10966. {
  10967. struct pt_core_data *cd = inode->i_private;
  10968. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10969. filp->private_data = inode->i_private;
  10970. if (cd->tthe_buf)
  10971. return -EBUSY;
  10972. while (buf_size < 4096)
  10973. buf_size = buf_size << 1;
  10974. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10975. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10976. cd->tthe_buf_size = buf_size;
  10977. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10978. if (!cd->tthe_buf)
  10979. return -ENOMEM;
  10980. return 0;
  10981. }
  10982. /*******************************************************************************
  10983. * FUNCTION: tthe_debugfs_close
  10984. *
  10985. * SUMMARY: Close method for tthe_tuner debugfs node.
  10986. *
  10987. * RETURN:
  10988. * 0 = success
  10989. * !0 = failure
  10990. *
  10991. * PARAMETERS:
  10992. * *inode - file inode number
  10993. * *filp - file pointer to debugfs file
  10994. ******************************************************************************/
  10995. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10996. {
  10997. struct pt_core_data *cd = filp->private_data;
  10998. filp->private_data = NULL;
  10999. kfree(cd->tthe_buf);
  11000. cd->tthe_buf = NULL;
  11001. return 0;
  11002. }
  11003. /*******************************************************************************
  11004. * FUNCTION: tthe_debugfs_read
  11005. *
  11006. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11007. * tthe_buf to user buffer.
  11008. *
  11009. * RETURN: Size of debugfs data print
  11010. *
  11011. * PARAMETERS:
  11012. * *filp - file pointer to debugfs file
  11013. * *buf - the user space buffer to read to
  11014. * count - the maximum number of bytes to read
  11015. * *ppos - the current position in the buffer
  11016. ******************************************************************************/
  11017. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11018. size_t count, loff_t *ppos)
  11019. {
  11020. struct pt_core_data *cd = filp->private_data;
  11021. int size;
  11022. int ret;
  11023. static int partial_read;
  11024. wait_event_interruptible(cd->wait_q,
  11025. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11026. mutex_lock(&cd->tthe_lock);
  11027. if (cd->tthe_exit) {
  11028. mutex_unlock(&cd->tthe_lock);
  11029. return 0;
  11030. }
  11031. if (count > cd->tthe_buf_len)
  11032. size = cd->tthe_buf_len;
  11033. else
  11034. size = count;
  11035. if (!size) {
  11036. mutex_unlock(&cd->tthe_lock);
  11037. return 0;
  11038. }
  11039. if (partial_read) {
  11040. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11041. partial_read = 0;
  11042. } else {
  11043. ret = copy_to_user(buf, cd->tthe_buf, size);
  11044. }
  11045. if (size == count)
  11046. partial_read = count;
  11047. if (ret == size)
  11048. return -EFAULT;
  11049. size -= ret;
  11050. cd->tthe_buf_len -= size;
  11051. mutex_unlock(&cd->tthe_lock);
  11052. *ppos += size;
  11053. return size;
  11054. }
  11055. static const struct file_operations tthe_debugfs_fops = {
  11056. .open = tthe_debugfs_open,
  11057. .release = tthe_debugfs_close,
  11058. .read = tthe_debugfs_read,
  11059. };
  11060. #endif
  11061. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11062. .start_bl = _pt_request_pip_start_bl,
  11063. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11064. .resume_scanning = _pt_request_pip_resume_scanning,
  11065. .get_param = _pt_request_pip_get_param,
  11066. .set_param = _pt_request_pip_set_param,
  11067. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11068. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11069. .get_data_structure = _pt_request_pip_get_data_structure,
  11070. .run_selftest = _pt_request_pip_run_selftest,
  11071. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11072. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11073. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11074. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11075. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11076. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11077. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11078. .read_data_block = _pt_request_pip_read_data_block,
  11079. .write_data_block = _pt_request_pip_write_data_block,
  11080. .user_cmd = _pt_request_pip_user_cmd,
  11081. .get_bl_info = _pt_request_pip_bl_get_information,
  11082. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11083. .launch_app = _pt_request_pip_launch_app,
  11084. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11085. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11086. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11087. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11088. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11089. .pip2_file_open = _pt_pip2_file_open,
  11090. .pip2_file_close = _pt_pip2_file_close,
  11091. .pip2_file_erase = _pt_pip2_file_erase,
  11092. .read_us_file = _pt_read_us_file,
  11093. .manage_cal_data = _pt_manage_local_cal_data,
  11094. .calc_crc = crc_ccitt_calculate,
  11095. #ifdef TTDL_DIAGNOSTICS
  11096. .pip2_file_read = _pt_pip2_file_read,
  11097. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11098. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11099. .pip2_file_crc = _pt_pip2_file_crc,
  11100. #endif
  11101. };
  11102. static struct pt_core_commands _pt_core_commands = {
  11103. .subscribe_attention = _pt_subscribe_attention,
  11104. .unsubscribe_attention = _pt_unsubscribe_attention,
  11105. .request_exclusive = _pt_request_exclusive,
  11106. .release_exclusive = _pt_release_exclusive,
  11107. .request_reset = _pt_request_reset,
  11108. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11109. .request_enum = _pt_request_enum,
  11110. .request_sysinfo = _pt_request_sysinfo,
  11111. .request_loader_pdata = _pt_request_loader_pdata,
  11112. .request_stop_wd = _pt_request_stop_wd,
  11113. .request_start_wd = _pt_request_start_wd,
  11114. .request_get_mode = _pt_request_get_mode,
  11115. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11116. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11117. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11118. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11119. .request_dut_generation = _pt_request_dut_generation,
  11120. .request_hw_version = _pt_request_hw_version,
  11121. .parse_sysfs_input = _pt_ic_parse_input,
  11122. #ifdef TTHE_TUNER_SUPPORT
  11123. .request_tthe_print = _pt_request_tthe_print,
  11124. #endif
  11125. #ifdef TTDL_DIAGNOSTICS
  11126. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11127. #endif
  11128. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11129. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11130. };
  11131. struct pt_core_commands *pt_get_commands(void)
  11132. {
  11133. return &_pt_core_commands;
  11134. }
  11135. EXPORT_SYMBOL_GPL(pt_get_commands);
  11136. static DEFINE_MUTEX(core_list_lock);
  11137. static LIST_HEAD(core_list);
  11138. static DEFINE_MUTEX(module_list_lock);
  11139. static LIST_HEAD(module_list);
  11140. static int core_number;
  11141. /*******************************************************************************
  11142. * FUNCTION: pt_probe_module
  11143. *
  11144. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11145. *
  11146. * RETURN:
  11147. * 0 = success
  11148. * !0 = failure
  11149. *
  11150. * PARAMETERS:
  11151. * *cd - pointer to core data
  11152. * *module - pointer to module structure
  11153. ******************************************************************************/
  11154. static int pt_probe_module(struct pt_core_data *cd,
  11155. struct pt_module *module)
  11156. {
  11157. struct module_node *module_node;
  11158. int rc = 0;
  11159. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11160. if (!module_node)
  11161. return -ENOMEM;
  11162. module_node->module = module;
  11163. mutex_lock(&cd->module_list_lock);
  11164. list_add(&module_node->node, &cd->module_list);
  11165. mutex_unlock(&cd->module_list_lock);
  11166. rc = module->probe(cd->dev, &module_node->data);
  11167. if (rc) {
  11168. /*
  11169. * Remove from the list when probe fails
  11170. * in order not to call release
  11171. */
  11172. mutex_lock(&cd->module_list_lock);
  11173. list_del(&module_node->node);
  11174. mutex_unlock(&cd->module_list_lock);
  11175. kfree(module_node);
  11176. goto exit;
  11177. }
  11178. exit:
  11179. return rc;
  11180. }
  11181. /*******************************************************************************
  11182. * FUNCTION: pt_release_module
  11183. *
  11184. * SUMMARY: Call the release pointer and remove the module pointer from
  11185. * module_list.
  11186. *
  11187. * PARAMETERS:
  11188. * *cd - pointer to core data
  11189. * *module - pointer to module structure
  11190. ******************************************************************************/
  11191. static void pt_release_module(struct pt_core_data *cd,
  11192. struct pt_module *module)
  11193. {
  11194. struct module_node *m, *m_n;
  11195. mutex_lock(&cd->module_list_lock);
  11196. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11197. if (m->module == module) {
  11198. module->release(cd->dev, m->data);
  11199. list_del(&m->node);
  11200. kfree(m);
  11201. break;
  11202. }
  11203. mutex_unlock(&cd->module_list_lock);
  11204. }
  11205. /*******************************************************************************
  11206. * FUNCTION: pt_probe_modules
  11207. *
  11208. * SUMMARY: Iterate module_list and probe each module.
  11209. *
  11210. * PARAMETERS:
  11211. * *cd - pointer to core data
  11212. ******************************************************************************/
  11213. static void pt_probe_modules(struct pt_core_data *cd)
  11214. {
  11215. struct pt_module *m;
  11216. int rc = 0;
  11217. mutex_lock(&module_list_lock);
  11218. list_for_each_entry(m, &module_list, node) {
  11219. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11220. __func__, m->name);
  11221. rc = pt_probe_module(cd, m);
  11222. if (rc)
  11223. pt_debug(cd->dev, DL_ERROR,
  11224. "%s: Probe fails for module %s\n",
  11225. __func__, m->name);
  11226. }
  11227. mutex_unlock(&module_list_lock);
  11228. }
  11229. /*******************************************************************************
  11230. * FUNCTION: pt_release_modules
  11231. *
  11232. * SUMMARY: Iterate module_list and remove each module.
  11233. *
  11234. * PARAMETERS:
  11235. * *cd - pointer to core data
  11236. ******************************************************************************/
  11237. static void pt_release_modules(struct pt_core_data *cd)
  11238. {
  11239. struct pt_module *m;
  11240. mutex_lock(&module_list_lock);
  11241. list_for_each_entry(m, &module_list, node)
  11242. pt_release_module(cd, m);
  11243. mutex_unlock(&module_list_lock);
  11244. }
  11245. /*******************************************************************************
  11246. * FUNCTION: pt_get_core_data
  11247. *
  11248. * SUMMARY: Iterate core_list and get core data.
  11249. *
  11250. * RETURN:
  11251. * pointer to core data or null pointer if fail
  11252. *
  11253. * PARAMETERS:
  11254. * *id - pointer to core id
  11255. ******************************************************************************/
  11256. struct pt_core_data *pt_get_core_data(char *id)
  11257. {
  11258. struct pt_core_data *d;
  11259. list_for_each_entry(d, &core_list, node)
  11260. if (!strncmp(d->core_id, id, 20))
  11261. return d;
  11262. return NULL;
  11263. }
  11264. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11265. /*******************************************************************************
  11266. * FUNCTION: pt_add_core
  11267. *
  11268. * SUMMARY: Add core data to the core_list.
  11269. *
  11270. * PARAMETERS:
  11271. * *dev - pointer to device structure
  11272. ******************************************************************************/
  11273. static void pt_add_core(struct device *dev)
  11274. {
  11275. struct pt_core_data *d;
  11276. struct pt_core_data *cd = dev_get_drvdata(dev);
  11277. mutex_lock(&core_list_lock);
  11278. list_for_each_entry(d, &core_list, node)
  11279. if (d->dev == dev)
  11280. goto unlock;
  11281. list_add(&cd->node, &core_list);
  11282. unlock:
  11283. mutex_unlock(&core_list_lock);
  11284. }
  11285. /*******************************************************************************
  11286. * FUNCTION: pt_del_core
  11287. *
  11288. * SUMMARY: Remove core data from the core_list.
  11289. *
  11290. * PARAMETERS:
  11291. * *dev - pointer to device structure
  11292. ******************************************************************************/
  11293. static void pt_del_core(struct device *dev)
  11294. {
  11295. struct pt_core_data *d, *d_n;
  11296. mutex_lock(&core_list_lock);
  11297. list_for_each_entry_safe(d, d_n, &core_list, node)
  11298. if (d->dev == dev) {
  11299. list_del(&d->node);
  11300. goto unlock;
  11301. }
  11302. unlock:
  11303. mutex_unlock(&core_list_lock);
  11304. }
  11305. /*******************************************************************************
  11306. * FUNCTION: pt_register_module
  11307. *
  11308. * SUMMARY: Register the module to module_list and probe the module for each
  11309. * core.
  11310. *
  11311. * RETURN:
  11312. * 0 = success
  11313. * !0 = failure
  11314. *
  11315. * PARAMETERS:
  11316. * *module - pointer to module structure
  11317. ******************************************************************************/
  11318. int pt_register_module(struct pt_module *module)
  11319. {
  11320. struct pt_module *m;
  11321. struct pt_core_data *cd;
  11322. int rc = 0;
  11323. if (!module || !module->probe || !module->release)
  11324. return -EINVAL;
  11325. mutex_lock(&module_list_lock);
  11326. list_for_each_entry(m, &module_list, node)
  11327. if (m == module) {
  11328. rc = -EEXIST;
  11329. goto unlock;
  11330. }
  11331. list_add(&module->node, &module_list);
  11332. /* Probe the module for each core */
  11333. mutex_lock(&core_list_lock);
  11334. list_for_each_entry(cd, &core_list, node)
  11335. pt_probe_module(cd, module);
  11336. mutex_unlock(&core_list_lock);
  11337. unlock:
  11338. mutex_unlock(&module_list_lock);
  11339. return rc;
  11340. }
  11341. EXPORT_SYMBOL_GPL(pt_register_module);
  11342. /*******************************************************************************
  11343. * FUNCTION: pt_unregister_module
  11344. *
  11345. * SUMMARY: Release the module for each core and remove the module from
  11346. * module_list.
  11347. *
  11348. * RETURN:
  11349. * 0 = success
  11350. * !0 = failure
  11351. *
  11352. * PARAMETERS:
  11353. * *module - pointer to module structure
  11354. ******************************************************************************/
  11355. void pt_unregister_module(struct pt_module *module)
  11356. {
  11357. struct pt_module *m, *m_n;
  11358. struct pt_core_data *cd;
  11359. if (!module)
  11360. return;
  11361. mutex_lock(&module_list_lock);
  11362. /* Release the module for each core */
  11363. mutex_lock(&core_list_lock);
  11364. list_for_each_entry(cd, &core_list, node)
  11365. pt_release_module(cd, module);
  11366. mutex_unlock(&core_list_lock);
  11367. list_for_each_entry_safe(m, m_n, &module_list, node)
  11368. if (m == module) {
  11369. list_del(&m->node);
  11370. break;
  11371. }
  11372. mutex_unlock(&module_list_lock);
  11373. }
  11374. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11375. /*******************************************************************************
  11376. * FUNCTION: pt_get_module_data
  11377. *
  11378. * SUMMARY: Get module data from module_node by module_list.
  11379. *
  11380. * RETURN:
  11381. * pointer to module data
  11382. *
  11383. * PARAMETERS:
  11384. * *dev - pointer to device structure
  11385. * *module - pointer to module structure
  11386. ******************************************************************************/
  11387. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11388. {
  11389. struct pt_core_data *cd = dev_get_drvdata(dev);
  11390. struct module_node *m;
  11391. void *data = NULL;
  11392. mutex_lock(&cd->module_list_lock);
  11393. list_for_each_entry(m, &cd->module_list, node)
  11394. if (m->module == module) {
  11395. data = m->data;
  11396. break;
  11397. }
  11398. mutex_unlock(&cd->module_list_lock);
  11399. return data;
  11400. }
  11401. EXPORT_SYMBOL(pt_get_module_data);
  11402. #ifdef CONFIG_HAS_EARLYSUSPEND
  11403. /*******************************************************************************
  11404. * FUNCTION: pt_early_suspend
  11405. *
  11406. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11407. * attention list.
  11408. *
  11409. * PARAMETERS:
  11410. * *h - pointer to early_suspend structure
  11411. ******************************************************************************/
  11412. static void pt_early_suspend(struct early_suspend *h)
  11413. {
  11414. struct pt_core_data *cd =
  11415. container_of(h, struct pt_core_data, es);
  11416. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11417. }
  11418. /*******************************************************************************
  11419. * FUNCTION: pt_late_resume
  11420. *
  11421. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11422. * attention list.
  11423. *
  11424. * PARAMETERS:
  11425. * *h - pointer to early_suspend structure
  11426. ******************************************************************************/
  11427. static void pt_late_resume(struct early_suspend *h)
  11428. {
  11429. struct pt_core_data *cd =
  11430. container_of(h, struct pt_core_data, es);
  11431. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11432. }
  11433. /*******************************************************************************
  11434. * FUNCTION: pt_setup_early_suspend
  11435. *
  11436. * SUMMARY: Register early/suspend function to the system.
  11437. *
  11438. * PARAMETERS:
  11439. * *cd - pointer to core data
  11440. ******************************************************************************/
  11441. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11442. {
  11443. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11444. cd->es.suspend = pt_early_suspend;
  11445. cd->es.resume = pt_late_resume;
  11446. register_early_suspend(&cd->es);
  11447. }
  11448. #elif defined(CONFIG_DRM)
  11449. static void pt_resume_work(struct work_struct *work)
  11450. {
  11451. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11452. resume_work);
  11453. int rc = 0;
  11454. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11455. return;
  11456. rc = pt_core_easywake_off(pt_data);
  11457. if (rc < 0) {
  11458. pt_debug(pt_data->dev, DL_ERROR,
  11459. "%s: Error on wake\n", __func__);
  11460. }
  11461. return;
  11462. }
  11463. static void pt_suspend_work(struct work_struct *work)
  11464. {
  11465. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11466. suspend_work);
  11467. int rc = 0;
  11468. pt_debug(pt_data->dev, DL_INFO, "%s Start\n", __func__);
  11469. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11470. return;
  11471. rc = pt_core_easywake_on(pt_data);
  11472. if (rc < 0) {
  11473. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11474. return;
  11475. }
  11476. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  11477. return;
  11478. }
  11479. /*******************************************************************************
  11480. * FUNCTION: drm_notifier_callback
  11481. *
  11482. * SUMMARY: Call back function for DRM notifier to allow to call
  11483. * resume/suspend attention list.
  11484. *
  11485. * RETURN:
  11486. * 0 = success
  11487. *
  11488. * PARAMETERS:
  11489. * *self - pointer to notifier_block structure
  11490. * event - event type of fb notifier
  11491. * *data - pointer to fb_event structure
  11492. ******************************************************************************/
  11493. static int drm_notifier_callback(struct notifier_block *self,
  11494. unsigned long event, void *data)
  11495. {
  11496. struct pt_core_data *cd =
  11497. container_of(self, struct pt_core_data, fb_notifier);
  11498. struct drm_panel_notifier *evdata = data;
  11499. int *blank;
  11500. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11501. if (!evdata)
  11502. goto exit;
  11503. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11504. event == DRM_PANEL_EVENT_BLANK)) {
  11505. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11506. __func__, event);
  11507. goto exit;
  11508. }
  11509. blank = evdata->data;
  11510. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11511. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11512. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11513. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11514. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11515. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11516. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11517. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11518. __func__, event);
  11519. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11520. if (cd->fb_state != FB_ON) {
  11521. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11522. __func__);
  11523. queue_work(cd->pt_workqueue, &cd->resume_work);
  11524. #if defined(CONFIG_PM_SLEEP)
  11525. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11526. __func__);
  11527. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11528. pt_core_resume_(cd->dev);
  11529. #endif
  11530. cd->fb_state = FB_ON;
  11531. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11532. }
  11533. }
  11534. } else if (*blank == DRM_PANEL_BLANK_LP) {
  11535. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11536. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11537. if (cd->fb_state != FB_OFF) {
  11538. #if defined(CONFIG_PM_SLEEP)
  11539. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11540. __func__);
  11541. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11542. pt_core_suspend_(cd->dev);
  11543. #endif
  11544. cancel_work_sync(&cd->resume_work);
  11545. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11546. cd->fb_state = FB_OFF;
  11547. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11548. }
  11549. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11550. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11551. __func__, event);
  11552. }
  11553. } else {
  11554. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11555. __func__, *blank);
  11556. }
  11557. exit:
  11558. return 0;
  11559. }
  11560. /*******************************************************************************
  11561. * FUNCTION: pt_setup_drm_notifier
  11562. *
  11563. * SUMMARY: Set up call back function into drm notifier.
  11564. *
  11565. * PARAMETERS:
  11566. * *cd - pointer to core data
  11567. ******************************************************************************/
  11568. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11569. {
  11570. cd->fb_state = FB_NONE;
  11571. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11572. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11573. if (!active_panel)
  11574. pt_debug(cd->dev, DL_ERROR,
  11575. "%s: Active panel not registered!\n", __func__);
  11576. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11577. if (!cd->pt_workqueue) {
  11578. pt_debug(cd->dev, DL_ERROR,
  11579. "%s: worker thread creation failed !\n", __func__);
  11580. }
  11581. if (cd->pt_workqueue) {
  11582. INIT_WORK(&cd->resume_work, pt_resume_work);
  11583. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11584. }
  11585. if (active_panel &&
  11586. drm_panel_notifier_register(active_panel,
  11587. &cd->fb_notifier) < 0)
  11588. pt_debug(cd->dev, DL_ERROR,
  11589. "%s: Register notifier failed!\n", __func__);
  11590. }
  11591. #elif defined(CONFIG_FB)
  11592. /*******************************************************************************
  11593. * FUNCTION: fb_notifier_callback
  11594. *
  11595. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11596. * resume/suspend attention list.
  11597. *
  11598. * RETURN:
  11599. * 0 = success
  11600. *
  11601. * PARAMETERS:
  11602. * *self - pointer to notifier_block structure
  11603. * event - event type of fb notifier
  11604. * *data - pointer to fb_event structure
  11605. ******************************************************************************/
  11606. static int fb_notifier_callback(struct notifier_block *self,
  11607. unsigned long event, void *data)
  11608. {
  11609. struct pt_core_data *cd =
  11610. container_of(self, struct pt_core_data, fb_notifier);
  11611. struct fb_event *evdata = data;
  11612. int *blank;
  11613. if (event != FB_EVENT_BLANK || !evdata)
  11614. goto exit;
  11615. blank = evdata->data;
  11616. if (*blank == FB_BLANK_UNBLANK) {
  11617. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11618. if (cd->fb_state != FB_ON) {
  11619. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11620. #if defined(CONFIG_PM_SLEEP)
  11621. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11622. pt_core_resume_(cd->dev);
  11623. #endif
  11624. cd->fb_state = FB_ON;
  11625. }
  11626. } else if (*blank == FB_BLANK_POWERDOWN) {
  11627. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11628. if (cd->fb_state != FB_OFF) {
  11629. #if defined(CONFIG_PM_SLEEP)
  11630. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11631. pt_core_suspend_(cd->dev);
  11632. #endif
  11633. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11634. cd->fb_state = FB_OFF;
  11635. }
  11636. }
  11637. exit:
  11638. return 0;
  11639. }
  11640. /*******************************************************************************
  11641. * FUNCTION: pt_setup_fb_notifier
  11642. *
  11643. * SUMMARY: Set up call back function into fb notifier.
  11644. *
  11645. * PARAMETERS:
  11646. * *cd - pointer to core data
  11647. ******************************************************************************/
  11648. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11649. {
  11650. int rc = 0;
  11651. cd->fb_state = FB_ON;
  11652. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11653. rc = fb_register_client(&cd->fb_notifier);
  11654. if (rc)
  11655. pt_debug(cd->dev, DL_ERROR,
  11656. "Unable to register fb_notifier: %d\n", rc);
  11657. }
  11658. #endif
  11659. /*******************************************************************************
  11660. * FUNCTION: pt_watchdog_work
  11661. *
  11662. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11663. * sleeping then this function simply returns. If the DUT is awake the
  11664. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11665. * somehow a response is waiting on the DUT that has not been read. If
  11666. * this occurs the message is simply consumed. If or once the IRQ is
  11667. * cleared, a PIP PING message is sent to the DUT and if the response
  11668. * is received the watchdog succeeds and exits, if no response is seen
  11669. * a startup is queued unless the maximum number of startups have already
  11670. * been attempted, in that case a BL is attempted.
  11671. *
  11672. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11673. * work thread
  11674. *
  11675. * RETURN: void
  11676. *
  11677. * PARAMETERS:
  11678. * *work - pointer to a work structure for the watchdog work queue
  11679. ******************************************************************************/
  11680. static void pt_watchdog_work(struct work_struct *work)
  11681. {
  11682. int rc = 0;
  11683. struct pt_core_data *cd = container_of(work,
  11684. struct pt_core_data, watchdog_work);
  11685. /*
  11686. * if found the current sleep_state is SS_SLEEPING
  11687. * then no need to request_exclusive, directly return
  11688. */
  11689. if (cd->sleep_state == SS_SLEEPING)
  11690. return;
  11691. #ifdef TTDL_DIAGNOSTICS
  11692. cd->watchdog_count++;
  11693. #endif /* TTDL_DIAGNOSTICS */
  11694. /*
  11695. * The first WD interval was extended to allow DDI to come up.
  11696. * If the WD interval is not the default then adjust timer to the
  11697. * current setting. The user can override value with drv_debug sysfs.
  11698. */
  11699. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11700. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11701. msecs_to_jiffies(cd->watchdog_interval));
  11702. }
  11703. if (pt_check_irq_asserted(cd)) {
  11704. #ifdef TTDL_DIAGNOSTICS
  11705. cd->watchdog_irq_stuck_count++;
  11706. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11707. #endif /* TTDL_DIAGNOSTICS */
  11708. pt_debug(cd->dev, DL_ERROR,
  11709. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11710. __func__);
  11711. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11712. }
  11713. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11714. if (rc < 0) {
  11715. pt_debug(cd->dev, DL_ERROR,
  11716. "%s: fail get exclusive ex=%p own=%p\n",
  11717. __func__, cd->exclusive_dev, cd->dev);
  11718. goto queue_startup;
  11719. }
  11720. rc = pt_pip_null_(cd);
  11721. if (release_exclusive(cd, cd->dev) < 0)
  11722. pt_debug(cd->dev, DL_ERROR,
  11723. "%s: fail to release exclusive\n", __func__);
  11724. queue_startup:
  11725. if (rc) {
  11726. #ifdef TTDL_DIAGNOSTICS
  11727. cd->watchdog_failed_access_count++;
  11728. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11729. #endif /* TTDL_DIAGNOSTICS */
  11730. pt_debug(cd->dev, DL_ERROR,
  11731. "%s: failed to access device in WD, retry count=%d\n",
  11732. __func__, cd->startup_retry_count);
  11733. /* Already tried FW upgrade because of watchdog but failed */
  11734. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11735. return;
  11736. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11737. /*
  11738. * Any wrapper function that trys to disable the
  11739. * WD killing this worker cannot be called here.
  11740. */
  11741. rc = request_exclusive(cd, cd->dev,
  11742. 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 exit;
  11748. }
  11749. cd->hw_detected = false;
  11750. cd->startup_status = STARTUP_STATUS_START;
  11751. pt_debug(cd->dev, DL_DEBUG,
  11752. "%s: Startup Status Reset\n", __func__);
  11753. rc = pt_dut_reset_and_wait(cd);
  11754. if (release_exclusive(cd, cd->dev) < 0)
  11755. pt_debug(cd->dev, DL_ERROR,
  11756. "%s: fail to release exclusive\n",
  11757. __func__);
  11758. if (!rc) {
  11759. cd->hw_detected = true;
  11760. if (!cd->flashless_dut)
  11761. pt_queue_enum(cd);
  11762. }
  11763. #ifdef TTDL_DIAGNOSTICS
  11764. cd->wd_xres_count++;
  11765. pt_debug(cd->dev, DL_ERROR,
  11766. "%s: Comm Failed - DUT reset [#%d]\n",
  11767. __func__, cd->wd_xres_count);
  11768. #endif /* TTDL_DIAGNOSTICS */
  11769. } else {
  11770. /*
  11771. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11772. * reset the part to regain communications, try to BL
  11773. */
  11774. pt_debug(cd->dev, DL_ERROR,
  11775. "%s: WD DUT access failure, Start FW Upgrade\n",
  11776. __func__);
  11777. #ifdef TTDL_DIAGNOSTICS
  11778. /*
  11779. * When diagnostics is enabled allow TTDL to keep
  11780. * trying to find the DUT. This allows the DUT to be
  11781. * hot swap-able while the host stays running. In
  11782. * production this may not be wanted as a customer
  11783. * may have several touch drivers and any driver
  11784. * that doesn't match the current DUT should give
  11785. * up trying and give up using the bus.
  11786. */
  11787. pt_debug(cd->dev, DL_INFO,
  11788. "%s: Resetting startup_retry_count\n",
  11789. __func__);
  11790. cd->startup_retry_count = 0;
  11791. #endif /* TTDL_DIAGNOSTICS */
  11792. /*
  11793. * Since fw may be broken,reset sysinfo ready flag
  11794. * to let upgrade function work.
  11795. */
  11796. mutex_lock(&cd->system_lock);
  11797. cd->sysinfo.ready = false;
  11798. mutex_unlock(&cd->system_lock);
  11799. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11800. pt_debug(cd->dev, DL_ERROR,
  11801. "%s: Queue Restart\n", __func__);
  11802. pt_queue_restart(cd);
  11803. } else
  11804. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11805. }
  11806. } else {
  11807. cd->hw_detected = true;
  11808. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11809. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11810. pt_debug(cd->dev, DL_ERROR,
  11811. "%s: HW detected but not enumerated\n",
  11812. __func__);
  11813. pt_queue_enum(cd);
  11814. }
  11815. }
  11816. exit:
  11817. pt_start_wd_timer(cd);
  11818. }
  11819. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11820. /*******************************************************************************
  11821. * FUNCTION: pt_watchdog_timer
  11822. *
  11823. * SUMMARY: The function that is called when the WD timer expires. If the
  11824. * watchdog work is not already busy schedule the watchdog work queue.
  11825. *
  11826. * RETURN: void
  11827. *
  11828. * PARAMETERS:
  11829. * handle - Handle to the watchdog timer
  11830. ******************************************************************************/
  11831. static void pt_watchdog_timer(unsigned long handle)
  11832. {
  11833. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11834. if (!cd)
  11835. return;
  11836. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11837. __func__);
  11838. if (!work_pending(&cd->watchdog_work))
  11839. schedule_work(&cd->watchdog_work);
  11840. }
  11841. #else
  11842. /*******************************************************************************
  11843. * FUNCTION: pt_watchdog_timer
  11844. *
  11845. * SUMMARY: The function that is called when the WD timer expires. If the
  11846. * watchdog work is not already busy schedule the watchdog work queue.
  11847. *
  11848. * RETURN: void
  11849. *
  11850. * PARAMETERS:
  11851. * *t - Pointer to timer list
  11852. ******************************************************************************/
  11853. static void pt_watchdog_timer(struct timer_list *t)
  11854. {
  11855. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11856. if (!cd)
  11857. return;
  11858. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11859. __func__);
  11860. if (!work_pending(&cd->watchdog_work))
  11861. schedule_work(&cd->watchdog_work);
  11862. }
  11863. #endif
  11864. /*******************************************************************************
  11865. * Core sysfs show and store functions
  11866. ******************************************************************************/
  11867. /*******************************************************************************
  11868. * FUNCTION: pt_hw_version_show
  11869. *
  11870. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11871. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11872. *
  11873. * RETURN: size of data written to sysfs node
  11874. *
  11875. * PARAMETERS:
  11876. * *dev - pointer to device structure
  11877. * *attr - pointer to device attributes structure
  11878. * *buf - pointer to print output buffer
  11879. ******************************************************************************/
  11880. static ssize_t pt_hw_version_show(struct device *dev,
  11881. struct device_attribute *attr, char *buf)
  11882. {
  11883. struct pt_core_data *cd = dev_get_drvdata(dev);
  11884. _pt_request_hw_version(dev, cd->hw_version);
  11885. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11886. }
  11887. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11888. /*******************************************************************************
  11889. * FUNCTION: pt_drv_version_show
  11890. *
  11891. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11892. * TTDL version information
  11893. *
  11894. * RETURN: Char buffer with printed TTDL version information
  11895. *
  11896. * PARAMETERS:
  11897. * *dev - pointer to device structure
  11898. * *attr - pointer to device attributes
  11899. * *buf - pointer to output buffer
  11900. ******************************************************************************/
  11901. static ssize_t pt_drv_version_show(struct device *dev,
  11902. struct device_attribute *attr, char *buf)
  11903. {
  11904. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11905. "Driver: %s\nVersion: %s\nDate: %s\n",
  11906. pt_driver_core_name, pt_driver_core_version,
  11907. pt_driver_core_date);
  11908. }
  11909. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11910. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11911. /*******************************************************************************
  11912. * FUNCTION: pt_fw_version_show
  11913. *
  11914. * SUMMARY: Show method for the fw_version sysfs node that will
  11915. * show the firmware, bootloader and PIP version information
  11916. *
  11917. * RETURN: Size of printed buffer
  11918. *
  11919. * PARAMETERS:
  11920. * *dev - pointer to device structure
  11921. * *attr - pointer to device attributes
  11922. * *buf - pointer to output buffer
  11923. ******************************************************************************/
  11924. static ssize_t pt_fw_version_show(struct device *dev,
  11925. struct device_attribute *attr, char *buf)
  11926. {
  11927. struct pt_core_data *cd = dev_get_drvdata(dev);
  11928. struct pt_ttdata *ttdata;
  11929. int rc = 0;
  11930. if (cd->mode == PT_MODE_OPERATIONAL)
  11931. rc = pt_hid_output_get_sysinfo_(cd);
  11932. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11933. __func__, cd->mode, cd->sysinfo.ready);
  11934. if (cd->sysinfo.ready)
  11935. ttdata = &cd->sysinfo.ttdata;
  11936. else
  11937. rc = -ENODATA;
  11938. if (!rc) {
  11939. return scnprintf(buf, strlen(buf),
  11940. "Status: %d\n"
  11941. "FW : %d.%d.%d\n"
  11942. "Config: %d\n"
  11943. "BL : %d.%d\n"
  11944. "PIP : %d.%d\n",
  11945. rc,
  11946. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11947. ttdata->revctrl,
  11948. ttdata->fw_ver_conf,
  11949. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11950. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11951. } else {
  11952. return scnprintf(buf, strlen(buf),
  11953. "Status: %d\n"
  11954. "FW : n/a\n"
  11955. "Config: n/a\n"
  11956. "BL : n/a\n"
  11957. "PIP : n/a\n",
  11958. rc);
  11959. }
  11960. }
  11961. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11962. /*******************************************************************************
  11963. * FUNCTION: pt_sysinfo_show
  11964. *
  11965. * SUMMARY: Show method for the sysinfo sysfs node that will
  11966. * show all the information from get system information command.
  11967. *
  11968. * RETURN: Size of printed buffer
  11969. *
  11970. * PARAMETERS:
  11971. * *dev - pointer to device structure
  11972. * *attr - pointer to device attributes
  11973. * *buf - pointer to output buffer
  11974. ******************************************************************************/
  11975. static ssize_t pt_sysinfo_show(struct device *dev,
  11976. struct device_attribute *attr, char *buf)
  11977. {
  11978. struct pt_core_data *cd = dev_get_drvdata(dev);
  11979. struct pt_sysinfo *si;
  11980. struct pt_ttdata *ttdata = NULL;
  11981. struct pt_sensing_conf_data *scd = NULL;
  11982. int rc = 0;
  11983. if (cd->mode == PT_MODE_OPERATIONAL) {
  11984. rc = pt_hid_output_get_sysinfo_(cd);
  11985. if (cd->sysinfo.ready) {
  11986. si = &cd->sysinfo;
  11987. ttdata = &si->ttdata;
  11988. scd = &si->sensing_conf_data;
  11989. } else
  11990. rc = -ENODATA;
  11991. } else
  11992. rc = -EPERM;
  11993. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11994. __func__, cd->mode, cd->sysinfo.ready);
  11995. if (!rc && ttdata && scd) {
  11996. return scnprintf(buf, strlen(buf),
  11997. "Status: %d\n"
  11998. "pip_ver_major: 0x%02X\n"
  11999. "pip_ver_minor: 0x%02X\n"
  12000. "fw_pid : 0x%04X\n"
  12001. "fw_ver_major : 0x%02X\n"
  12002. "fw_ver_minor : 0x%02X\n"
  12003. "revctrl : 0x%08X\n"
  12004. "fw_ver_conf : 0x%04X\n"
  12005. "bl_ver_major : 0x%02X\n"
  12006. "bl_ver_minor : 0x%02X\n"
  12007. "jtag_id_h : 0x%04X\n"
  12008. "jtag_id_l : 0x%04X\n"
  12009. "mfg_id[0] : 0x%02X\n"
  12010. "mfg_id[1] : 0x%02X\n"
  12011. "mfg_id[2] : 0x%02X\n"
  12012. "mfg_id[3] : 0x%02X\n"
  12013. "mfg_id[4] : 0x%02X\n"
  12014. "mfg_id[5] : 0x%02X\n"
  12015. "mfg_id[6] : 0x%02X\n"
  12016. "mfg_id[7] : 0x%02X\n"
  12017. "post_code : 0x%04X\n"
  12018. "electrodes_x : 0x%02X\n"
  12019. "electrodes_y : 0x%02X\n"
  12020. "len_x : 0x%04X\n"
  12021. "len_y : 0x%04X\n"
  12022. "res_x : 0x%04X\n"
  12023. "res_y : 0x%04X\n"
  12024. "max_z : 0x%04X\n"
  12025. "origin_x : 0x%02X\n"
  12026. "origin_y : 0x%02X\n"
  12027. "panel_id : 0x%02X\n"
  12028. "btn : 0x%02X\n"
  12029. "scan_mode : 0x%02X\n"
  12030. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12031. rc,
  12032. ttdata->pip_ver_major,
  12033. ttdata->pip_ver_minor,
  12034. ttdata->fw_pid,
  12035. ttdata->fw_ver_major,
  12036. ttdata->fw_ver_minor,
  12037. ttdata->revctrl,
  12038. ttdata->fw_ver_conf,
  12039. ttdata->bl_ver_major,
  12040. ttdata->bl_ver_minor,
  12041. ttdata->jtag_id_h,
  12042. ttdata->jtag_id_l,
  12043. ttdata->mfg_id[0],
  12044. ttdata->mfg_id[1],
  12045. ttdata->mfg_id[2],
  12046. ttdata->mfg_id[3],
  12047. ttdata->mfg_id[4],
  12048. ttdata->mfg_id[5],
  12049. ttdata->mfg_id[6],
  12050. ttdata->mfg_id[7],
  12051. ttdata->post_code,
  12052. scd->electrodes_x,
  12053. scd->electrodes_y,
  12054. scd->len_x,
  12055. scd->len_y,
  12056. scd->res_x,
  12057. scd->res_y,
  12058. scd->max_z,
  12059. scd->origin_x,
  12060. scd->origin_y,
  12061. scd->panel_id,
  12062. scd->btn,
  12063. scd->scan_mode,
  12064. scd->max_tch);
  12065. } else {
  12066. return scnprintf(buf, strlen(buf),
  12067. "Status: %d\n",
  12068. rc);
  12069. }
  12070. }
  12071. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12072. /*******************************************************************************
  12073. * FUNCTION: pt_hw_reset_show
  12074. *
  12075. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12076. * by toggling the XRES line and then calls the startup function to
  12077. * allow TTDL to re-enumerate the DUT.
  12078. * The printed value reflects the status of the full reset/enum.
  12079. *
  12080. * PARAMETERS:
  12081. * *dev - pointer to Device structure
  12082. * *attr - pointer to the device attribute structure
  12083. * *buf - pointer to buffer to print
  12084. ******************************************************************************/
  12085. static ssize_t pt_hw_reset_show(struct device *dev,
  12086. struct device_attribute *attr, char *buf)
  12087. {
  12088. struct pt_core_data *cd = dev_get_drvdata(dev);
  12089. int rc = 0;
  12090. int time = 0;
  12091. u8 reset_status = 0;
  12092. int t;
  12093. struct pt_hid_desc hid_desc;
  12094. memset(&hid_desc, 0, sizeof(hid_desc));
  12095. /* Only allow DUT reset if no active BL in progress */
  12096. mutex_lock(&cd->firmware_class_lock);
  12097. mutex_lock(&cd->system_lock);
  12098. cd->startup_state = STARTUP_NONE;
  12099. mutex_unlock(&(cd->system_lock));
  12100. pt_stop_wd_timer(cd);
  12101. /* ensure no left over exclusive access is still locked */
  12102. release_exclusive(cd, cd->dev);
  12103. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12104. if (rc) {
  12105. mutex_unlock(&cd->firmware_class_lock);
  12106. pt_debug(cd->dev, DL_ERROR,
  12107. "%s: HW reset failed rc = %d\n", __func__, rc);
  12108. goto exit_hw_reset;
  12109. }
  12110. reset_status |= 0x01 << 0;
  12111. if (cd->flashless_dut) {
  12112. mutex_unlock(&cd->firmware_class_lock);
  12113. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12114. msecs_to_jiffies(200));
  12115. if (IS_TMO(t)) {
  12116. pt_debug(dev, DL_ERROR,
  12117. "%s: Timeout waiting for FW update",
  12118. __func__);
  12119. rc = -ETIME;
  12120. goto exit_hw_reset;
  12121. } else {
  12122. pt_debug(dev, DL_INFO,
  12123. "%s: ----- Wait FW Loading ----",
  12124. __func__);
  12125. rc = _pt_request_wait_for_enum_state(
  12126. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12127. if (rc) {
  12128. pt_debug(cd->dev, DL_ERROR,
  12129. "%s: No FW Sentinel detected rc = %d\n",
  12130. __func__, rc);
  12131. goto exit_hw_reset;
  12132. }
  12133. reset_status |= 0x01 << 1;
  12134. }
  12135. } else {
  12136. /* Wait for any sentinel */
  12137. rc = _pt_request_wait_for_enum_state(dev, 150,
  12138. STARTUP_STATUS_BL_RESET_SENTINEL |
  12139. STARTUP_STATUS_FW_RESET_SENTINEL);
  12140. if (rc) {
  12141. mutex_unlock(&cd->firmware_class_lock);
  12142. pt_debug(cd->dev, DL_ERROR,
  12143. "%s: No Sentinel detected rc = %d\n",
  12144. __func__, rc);
  12145. goto exit_hw_reset;
  12146. }
  12147. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12148. msleep(30);
  12149. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12150. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12151. if (rc < 0) {
  12152. pt_debug(cd->dev, DL_ERROR,
  12153. "%s: Error on getting HID descriptor r=%d\n",
  12154. __func__, rc);
  12155. goto exit_hw_reset;
  12156. }
  12157. cd->mode = pt_get_mode(cd, &hid_desc);
  12158. if (cd->mode == PT_MODE_BOOTLOADER)
  12159. rc = pt_hid_output_bl_launch_app_(cd);
  12160. } else {
  12161. if (cd->mode == PT_MODE_BOOTLOADER)
  12162. rc = pt_pip2_launch_app(dev,
  12163. PT_CORE_CMD_UNPROTECTED);
  12164. }
  12165. if (rc) {
  12166. mutex_unlock(&cd->firmware_class_lock);
  12167. pt_debug(cd->dev, DL_ERROR,
  12168. "%s: PIP launch app failed rc = %d\n",
  12169. __func__, rc);
  12170. goto exit_hw_reset;
  12171. }
  12172. mutex_unlock(&cd->firmware_class_lock);
  12173. reset_status |= 0x01 << 1;
  12174. msleep(20);
  12175. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12176. (cd->mode != PT_MODE_OPERATIONAL))
  12177. pt_queue_restart(cd);
  12178. else
  12179. pt_queue_enum(cd);
  12180. }
  12181. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12182. msleep(50);
  12183. pt_debug(cd->dev, DL_INFO,
  12184. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12185. __func__, time, STARTUP_STATUS_COMPLETE,
  12186. cd->startup_status);
  12187. time += 50;
  12188. }
  12189. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12190. rc = -ETIME;
  12191. goto exit_hw_reset;
  12192. }
  12193. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12194. __func__, cd->startup_status);
  12195. reset_status |= 0x01 << 2;
  12196. pt_start_wd_timer(cd);
  12197. exit_hw_reset:
  12198. return scnprintf(buf, strlen(buf),
  12199. "Status: %d\n"
  12200. "Reset Status: 0x%02X\n", rc, reset_status);
  12201. }
  12202. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12203. /*******************************************************************************
  12204. * FUNCTION: pt_pip2_cmd_rsp_store
  12205. *
  12206. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12207. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12208. * Command byte order:
  12209. * Byte [0] - PIP2 command ID
  12210. * Byte [1-n] - PIP2 command payload
  12211. *
  12212. * RETURN: Size of passed in buffer
  12213. *
  12214. * PARAMETERS:
  12215. * *dev - pointer to device structure
  12216. * *attr - pointer to device attributes
  12217. * *buf - pointer to buffer that hold the command parameters
  12218. * size - size of buf
  12219. ******************************************************************************/
  12220. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12221. struct device_attribute *attr, const char *buf, size_t size)
  12222. {
  12223. struct pt_core_data *cd = dev_get_drvdata(dev);
  12224. u16 actual_read_len;
  12225. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12226. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12227. u8 pip2_cmd_id = 0x00;
  12228. u8 *pip2_cmd_data = NULL;
  12229. int data_len = 0;
  12230. int length;
  12231. int rc = 0;
  12232. /* clear shared data */
  12233. mutex_lock(&cd->sysfs_lock);
  12234. cd->raw_cmd_status = 0;
  12235. cd->cmd_rsp_buf_len = 0;
  12236. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12237. mutex_unlock(&cd->sysfs_lock);
  12238. length = _pt_ic_parse_input_hex(dev, buf, size,
  12239. input_data, PT_MAX_PIP2_MSG_SIZE);
  12240. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12241. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12242. __func__);
  12243. rc = -EINVAL;
  12244. goto exit;
  12245. }
  12246. /* Send PIP2 command if enough data was provided */
  12247. if (length >= 1) {
  12248. pip2_cmd_id = input_data[0];
  12249. pip2_cmd_data = &input_data[1];
  12250. data_len = length - 1;
  12251. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12252. pip2_cmd_id, pip2_cmd_data, data_len,
  12253. read_buf, &actual_read_len);
  12254. cd->raw_cmd_status = rc;
  12255. if (rc) {
  12256. pt_debug(dev, DL_ERROR,
  12257. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12258. __func__, pip2_cmd_id, rc);
  12259. goto exit;
  12260. } else {
  12261. cd->cmd_rsp_buf_len = actual_read_len;
  12262. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12263. pt_debug(dev, DL_ERROR,
  12264. "%s: PIP2 actual_read_len = %d\n",
  12265. __func__, actual_read_len);
  12266. }
  12267. } else {
  12268. rc = -EINVAL;
  12269. pt_debug(dev, DL_ERROR,
  12270. "%s: Insufficient data provided for PIP2 cmd\n",
  12271. __func__);
  12272. }
  12273. exit:
  12274. if (rc)
  12275. return rc;
  12276. return size;
  12277. }
  12278. /*******************************************************************************
  12279. * FUNCTION: pt_pip2_cmd_rsp_show
  12280. *
  12281. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12282. * response generated after using the store method of the pip2_cmd_rsp
  12283. * sysfs node, are available to be read here.
  12284. *
  12285. * PARAMETERS:
  12286. * *dev - pointer to Device structure
  12287. * *attr - pointer to the device attribute structure
  12288. * *buf - pointer to buffer to print
  12289. ******************************************************************************/
  12290. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12291. struct device_attribute *attr, char *buf)
  12292. {
  12293. struct pt_core_data *cd = dev_get_drvdata(dev);
  12294. int i;
  12295. ssize_t data_len;
  12296. int index;
  12297. mutex_lock(&cd->sysfs_lock);
  12298. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12299. "Status: %d\n", cd->raw_cmd_status);
  12300. if (cd->raw_cmd_status)
  12301. goto error;
  12302. /* Remove the CRC from the length of the response */
  12303. data_len = cd->cmd_rsp_buf_len - 2;
  12304. /* Start printing from the data payload */
  12305. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12306. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12307. "%02X ", cd->cmd_rsp_buf[i]);
  12308. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12309. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12310. "\n(%zd bytes)\n",
  12311. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12312. } else {
  12313. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12314. "\n(%zd bytes)\n", 0);
  12315. }
  12316. error:
  12317. mutex_unlock(&cd->sysfs_lock);
  12318. return index;
  12319. }
  12320. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12321. pt_pip2_cmd_rsp_store);
  12322. /*******************************************************************************
  12323. * FUNCTION: pt_command_store
  12324. *
  12325. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12326. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12327. * TTDL will not parse the command.
  12328. *
  12329. * RETURN: Size of passed in buffer
  12330. *
  12331. * PARAMETERS:
  12332. * *dev - pointer to device structure
  12333. * *attr - pointer to device attributes
  12334. * *buf - pointer to buffer that hold the command parameters
  12335. * size - size of buf
  12336. ******************************************************************************/
  12337. static ssize_t pt_command_store(struct device *dev,
  12338. struct device_attribute *attr, const char *buf, size_t size)
  12339. {
  12340. struct pt_core_data *cd = dev_get_drvdata(dev);
  12341. unsigned short crc;
  12342. u16 actual_read_len;
  12343. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12344. int length;
  12345. int len_field;
  12346. int rc = 0;
  12347. mutex_lock(&cd->sysfs_lock);
  12348. cd->cmd_rsp_buf_len = 0;
  12349. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12350. mutex_unlock(&cd->sysfs_lock);
  12351. length = _pt_ic_parse_input_hex(dev, buf, size,
  12352. input_data, PT_MAX_PIP2_MSG_SIZE);
  12353. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12354. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12355. __func__);
  12356. rc = -EINVAL;
  12357. goto pt_command_store_exit;
  12358. }
  12359. /* PIP2 messages begin with 01 01 */
  12360. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12361. cd->pip2_prot_active = 1;
  12362. /* Override next seq tag with what was sent */
  12363. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12364. /* For PIP2 cmd if length does not include crc, add it */
  12365. len_field = (input_data[3] << 8) | input_data[2];
  12366. if (len_field == length && length <= 254) {
  12367. crc = crc_ccitt_calculate(&input_data[2],
  12368. length - 2);
  12369. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12370. __func__, length, crc);
  12371. input_data[length] = (crc & 0xFF00) >> 8;
  12372. input_data[length + 1] = crc & 0x00FF;
  12373. length = length + 2;
  12374. }
  12375. }
  12376. /* write PIP command to log */
  12377. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12378. pm_runtime_get_sync(dev);
  12379. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12380. length, input_data, &actual_read_len);
  12381. pm_runtime_put(dev);
  12382. mutex_lock(&cd->sysfs_lock);
  12383. cd->raw_cmd_status = rc;
  12384. if (rc) {
  12385. cd->cmd_rsp_buf_len = 0;
  12386. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12387. __func__, buf);
  12388. } else {
  12389. cd->cmd_rsp_buf_len = actual_read_len;
  12390. }
  12391. cd->pip2_prot_active = 0;
  12392. mutex_unlock(&cd->sysfs_lock);
  12393. pt_command_store_exit:
  12394. if (rc)
  12395. return rc;
  12396. return size;
  12397. }
  12398. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12399. /*******************************************************************************
  12400. * FUNCTION: pt_response_show
  12401. *
  12402. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12403. * response generated after using the pt_command_store sysfs node, are
  12404. * available to be read here.
  12405. *
  12406. * PARAMETERS:
  12407. * *dev - pointer to Device structure
  12408. * *attr - pointer to the device attribute structure
  12409. * *buf - pointer to buffer to print
  12410. ******************************************************************************/
  12411. static ssize_t pt_response_show(struct device *dev,
  12412. struct device_attribute *attr, char *buf)
  12413. {
  12414. struct pt_core_data *cd = dev_get_drvdata(dev);
  12415. int i;
  12416. ssize_t num_read;
  12417. int index;
  12418. mutex_lock(&cd->sysfs_lock);
  12419. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12420. "Status: %d\n", cd->raw_cmd_status);
  12421. if (cd->raw_cmd_status)
  12422. goto error;
  12423. num_read = cd->cmd_rsp_buf_len;
  12424. for (i = 0; i < num_read; i++)
  12425. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12426. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12427. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12428. "(%zd bytes)\n", num_read);
  12429. error:
  12430. mutex_unlock(&cd->sysfs_lock);
  12431. return index;
  12432. }
  12433. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12434. /*******************************************************************************
  12435. * FUNCTION: pt_dut_debug_show
  12436. *
  12437. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12438. * are available for the store method.
  12439. *
  12440. * RETURN:
  12441. * 0 = success
  12442. * !0 = failure
  12443. *
  12444. * PARAMETERS:
  12445. * *dev - pointer to device structure
  12446. * *attr - pointer to device attributes
  12447. * *buf - pointer to output buffer
  12448. ******************************************************************************/
  12449. static ssize_t pt_dut_debug_show(struct device *dev,
  12450. struct device_attribute *attr, char *buf)
  12451. {
  12452. ssize_t ret;
  12453. ret = scnprintf(buf, strlen(buf),
  12454. "Status: 0\n"
  12455. "dut_debug sends the following commands to the DUT:\n"
  12456. "%d %s \t- %s\n"
  12457. "%d %s \t- %s\n"
  12458. "%d %s \t- %s\n"
  12459. "%d %s \t- %s\n"
  12460. "%d %s \t- %s\n"
  12461. "%d %s \t- %s\n"
  12462. "%d %s \t- %s\n"
  12463. "%d %s \t- %s\n"
  12464. "%d %s \t- %s\n"
  12465. "%d %s \t- %s\n"
  12466. "%d %s \t- %s\n"
  12467. "%d %s \t- %s\n"
  12468. "%d %s \t- %s\n"
  12469. "%d %s \t- %s\n"
  12470. "%d %s \t- %s\n"
  12471. "%d %s \t- %s\n"
  12472. "%d %s \t- %s\n"
  12473. ,
  12474. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12475. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12476. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12477. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12478. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12479. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12480. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12481. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12482. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12483. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12484. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12485. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12486. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12487. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12488. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12489. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12490. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12491. );
  12492. return ret;
  12493. }
  12494. /*******************************************************************************
  12495. * FUNCTION: pt_drv_debug_show
  12496. *
  12497. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12498. * are available for the store method.
  12499. *
  12500. * RETURN:
  12501. * 0 = success
  12502. * !0 = failure
  12503. *
  12504. * PARAMETERS:
  12505. * *dev - pointer to device structure
  12506. * *attr - pointer to device attributes
  12507. * *buf - pointer to output buffer
  12508. ******************************************************************************/
  12509. static ssize_t pt_drv_debug_show(struct device *dev,
  12510. struct device_attribute *attr, char *buf)
  12511. {
  12512. ssize_t ret;
  12513. ret = scnprintf(buf, strlen(buf),
  12514. "Status: 0\n"
  12515. "drv_debug supports the following values:\n"
  12516. "%d %s \t- %s\n"
  12517. "%d %s \t- %s\n"
  12518. "%d %s \t- %s\n"
  12519. "%d %s \t- %s\n"
  12520. "%d %s \t- %s\n"
  12521. "%d %s \t- %s\n"
  12522. "%d %s \t- %s\n"
  12523. "%d %s \t- %s\n"
  12524. "%d %s \t- %s\n"
  12525. "%d %s - %s\n"
  12526. "%d %s \t- %s\n"
  12527. "%d %s \t- %s\n"
  12528. #ifdef TTDL_DIAGNOSTICS
  12529. "%d %s \t- %s\n"
  12530. "%d %s \t- %s\n"
  12531. "%d %s \t- %s\n"
  12532. #endif /* TTDL_DIAGNOSTICS */
  12533. "%d %s \t- %s\n"
  12534. #ifdef TTDL_DIAGNOSTICS
  12535. "%d %s \t- %s\n"
  12536. "%d %s \t- %s\n"
  12537. "%d %s \t- %s\n"
  12538. "%d %s \t- %s\n"
  12539. "%d %s \t- %s\n"
  12540. "%d %s \t- %s\n"
  12541. "%d %s \t- %s\n"
  12542. "%d %s \t- %s\n"
  12543. "%d %s \t- %s\n"
  12544. #endif /* TTDL_DIAGNOSTICS */
  12545. ,
  12546. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12547. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12548. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12549. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12550. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12551. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12552. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12553. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12554. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12555. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12556. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12557. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12558. #ifdef TTDL_DIAGNOSTICS
  12559. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12560. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12561. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12562. #endif /* TTDL_DIAGNOSTICS */
  12563. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12564. #ifdef TTDL_DIAGNOSTICS
  12565. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12566. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12567. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12568. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12569. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12570. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12571. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12572. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12573. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12574. "TTHE_TUNER HID USB Format"
  12575. #endif /* TTDL_DIAGNOSTICS */
  12576. );
  12577. return ret;
  12578. }
  12579. /*******************************************************************************
  12580. * FUNCTION: pt_drv_debug_store
  12581. *
  12582. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12583. * dut_debug. Drv_debug will contain all functionality that can be run
  12584. * without a DUT preset and is available anytime TTDL is running.
  12585. * Dut_debug requires a DUT to be available and will only be created after
  12586. * a DUT has been detected.
  12587. * This function will eventually be split into two but until the overlap
  12588. * has been depricated this function contains all commands that can be
  12589. * used for TTDL/DUT debugging status and control.
  12590. * All commands require at least one value to be passed in *buf with some
  12591. * requiring two.
  12592. *
  12593. * RETURN: Size of passed in buffer
  12594. *
  12595. * PARAMETERS:
  12596. * *dev - pointer to device structure
  12597. * *attr - pointer to device attributes
  12598. * *buf - pointer to buffer that hold the command parameters
  12599. * size - size of buf
  12600. ******************************************************************************/
  12601. static ssize_t pt_drv_debug_store(struct device *dev,
  12602. struct device_attribute *attr, const char *buf, size_t size)
  12603. {
  12604. struct pt_core_data *cd = dev_get_drvdata(dev);
  12605. unsigned long value;
  12606. int rc = 0;
  12607. u8 return_data[8];
  12608. static u8 wd_disabled;
  12609. u32 input_data[3];
  12610. int length;
  12611. #ifdef TTDL_DIAGNOSTICS
  12612. struct i2c_client *client = to_i2c_client(dev);
  12613. unsigned short crc = 0;
  12614. u16 cal_size;
  12615. #endif
  12616. input_data[0] = 0;
  12617. input_data[1] = 0;
  12618. /* Maximmum input is two elements */
  12619. length = _pt_ic_parse_input(dev, buf, size,
  12620. input_data, ARRAY_SIZE(input_data));
  12621. if (length < 1 || length > 2) {
  12622. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12623. __func__);
  12624. rc = -EINVAL;
  12625. goto pt_drv_debug_store_exit;
  12626. }
  12627. value = input_data[0];
  12628. if (length == 1) {
  12629. pt_debug(dev, DL_DEBUG,
  12630. "%s: Debug Cmd Received (id=%d)\n",
  12631. __func__, input_data[0]);
  12632. } else if (length == 2) {
  12633. pt_debug(dev, DL_DEBUG,
  12634. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12635. __func__, input_data[0], input_data[1]);
  12636. } else {
  12637. pt_debug(dev, DL_DEBUG,
  12638. "%s: Invalid arguments received\n", __func__);
  12639. rc = -EINVAL;
  12640. goto pt_drv_debug_store_exit;
  12641. }
  12642. /* Start watchdog timer command */
  12643. if (value == PT_DRV_DBG_START_WD) {
  12644. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12645. wd_disabled = 0;
  12646. cd->watchdog_force_stop = false;
  12647. pt_start_wd_timer(cd);
  12648. goto pt_drv_debug_store_exit;
  12649. }
  12650. /* Stop watchdog timer temporarily */
  12651. pt_stop_wd_timer(cd);
  12652. if (value == PT_DRV_DBG_STOP_WD) {
  12653. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12654. wd_disabled = 1;
  12655. cd->watchdog_force_stop = true;
  12656. goto pt_drv_debug_store_exit;
  12657. }
  12658. switch (value) {
  12659. case PT_DRV_DBG_SUSPEND: /* 4 */
  12660. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12661. wd_disabled = 1;
  12662. rc = pt_core_sleep(cd);
  12663. if (rc)
  12664. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12665. __func__, rc);
  12666. else
  12667. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12668. __func__);
  12669. break;
  12670. case PT_DRV_DBG_RESUME: /* 5 */
  12671. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12672. rc = pt_core_wake(cd);
  12673. if (rc)
  12674. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12675. __func__, rc);
  12676. else
  12677. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12678. __func__);
  12679. break;
  12680. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12681. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12682. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12683. break;
  12684. case PT_DUT_DBG_HID_RESET: /* 50 */
  12685. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12686. pt_hid_cmd_reset(cd);
  12687. break;
  12688. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12689. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12690. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12691. wd_disabled = 0;
  12692. break;
  12693. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12694. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12695. __func__);
  12696. wd_disabled = 1;
  12697. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12698. break;
  12699. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12700. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12701. __func__);
  12702. wd_disabled = 1;
  12703. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12704. break;
  12705. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12706. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12707. pt_hid_output_bl_get_information(cd, return_data);
  12708. break;
  12709. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12710. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12711. __func__);
  12712. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12713. break;
  12714. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12715. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12716. pt_hid_output_bl_launch_app(cd);
  12717. break;
  12718. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12719. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12720. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12721. break;
  12722. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12723. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12724. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12725. break;
  12726. case PT_DUT_DBG_RESET: /* 98 */
  12727. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12728. rc = pt_hw_hard_reset(cd);
  12729. break;
  12730. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12731. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12732. pt_pip_null(cd);
  12733. break;
  12734. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12735. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12736. rc = pt_pip_start_bootloader(cd);
  12737. if (!rc) {
  12738. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12739. cd->mode = PT_MODE_BOOTLOADER;
  12740. }
  12741. break;
  12742. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12743. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12744. pt_hid_output_get_sysinfo(cd);
  12745. break;
  12746. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12747. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12748. pt_pip_suspend_scanning(cd);
  12749. break;
  12750. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12751. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12752. pt_pip_resume_scanning(cd);
  12753. break;
  12754. #ifdef TTHE_TUNER_SUPPORT
  12755. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12756. cd->tthe_exit = 1;
  12757. wake_up(&cd->wait_q);
  12758. kfree(cd->tthe_buf);
  12759. cd->tthe_buf = NULL;
  12760. cd->tthe_exit = 0;
  12761. break;
  12762. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12763. if (cd->tthe_buf)
  12764. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12765. else
  12766. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12767. __func__);
  12768. break;
  12769. #endif
  12770. #ifdef TTDL_DIAGNOSTICS
  12771. case PT_DUT_DBG_HID_DESC: /* 109 */
  12772. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12773. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12774. break;
  12775. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12776. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12777. __func__);
  12778. pt_erase_parameter_list(cd);
  12779. break;
  12780. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12781. rc = pt_read_input(cd);
  12782. if (!rc)
  12783. pt_parse_input(cd);
  12784. break;
  12785. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12786. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12787. &cal_size, &crc);
  12788. if (rc)
  12789. pt_debug(dev, DL_ERROR,
  12790. "%s: CAL Data clear failed rc=%d\n",
  12791. __func__, rc);
  12792. else
  12793. pt_debug(dev, DL_INFO,
  12794. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12795. __func__, crc, size);
  12796. break;
  12797. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12798. mutex_lock(&cd->system_lock);
  12799. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12800. cd->debug_level = input_data[1];
  12801. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12802. __func__, cd->debug_level);
  12803. } else {
  12804. rc = -EINVAL;
  12805. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12806. __func__, input_data[1]);
  12807. }
  12808. mutex_unlock(&(cd->system_lock));
  12809. break;
  12810. #endif
  12811. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12812. mutex_lock(&cd->system_lock);
  12813. if (input_data[1] > 100) {
  12814. cd->watchdog_interval = input_data[1];
  12815. pt_debug(dev, DL_INFO,
  12816. "%s: Set watchdog_ interval to: %d\n",
  12817. __func__, cd->watchdog_interval);
  12818. pt_start_wd_timer(cd);
  12819. } else {
  12820. rc = -EINVAL;
  12821. pt_debug(dev, DL_ERROR,
  12822. "%s: Invalid watchdog interval: %d\n",
  12823. __func__, input_data[1]);
  12824. }
  12825. mutex_unlock(&(cd->system_lock));
  12826. break;
  12827. #ifdef TTDL_DIAGNOSTICS
  12828. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12829. mutex_lock(&cd->system_lock);
  12830. if (input_data[1] == 0) {
  12831. cd->show_timestamp = 0;
  12832. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12833. __func__);
  12834. } else if (input_data[1] == 1) {
  12835. cd->show_timestamp = 1;
  12836. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12837. __func__);
  12838. } else {
  12839. rc = -EINVAL;
  12840. pt_debug(dev, DL_ERROR,
  12841. "%s: Invalid parameter: %d\n",
  12842. __func__, input_data[1]);
  12843. }
  12844. mutex_unlock(&(cd->system_lock));
  12845. break;
  12846. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12847. if (input_data[1] == 0) {
  12848. cd->cpdata->setup_power(cd->cpdata,
  12849. PT_MT_POWER_OFF, cd->dev);
  12850. pt_debug(dev, DL_INFO,
  12851. "%s: Initiate Power Off\n", __func__);
  12852. } else if (input_data[1] == 1) {
  12853. cd->cpdata->setup_power(cd->cpdata,
  12854. PT_MT_POWER_ON, cd->dev);
  12855. pt_debug(dev, DL_INFO,
  12856. "%s: Initiate Power On\n", __func__);
  12857. } else {
  12858. rc = -EINVAL;
  12859. pt_debug(dev, DL_ERROR,
  12860. "%s: Invalid parameter: %d\n",
  12861. __func__, input_data[1]);
  12862. }
  12863. break;
  12864. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12865. if (input_data[1] == 0) {
  12866. pm_runtime_put(dev);
  12867. pt_debug(dev, DL_ERROR,
  12868. "%s: Force call pm_runtime_put()\n", __func__);
  12869. } else if (input_data[1] == 1) {
  12870. pm_runtime_get_sync(dev);
  12871. pt_debug(dev, DL_ERROR,
  12872. "%s: Force call pm_runtime_get_sync()\n",
  12873. __func__);
  12874. } else {
  12875. rc = -EINVAL;
  12876. pt_debug(dev, DL_ERROR,
  12877. "%s: WARNING: Invalid parameter: %d\n",
  12878. __func__, input_data[1]);
  12879. }
  12880. break;
  12881. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12882. mutex_lock(&cd->system_lock);
  12883. if (input_data[1] == 0) {
  12884. cd->show_tt_data = false;
  12885. pt_debug(dev, DL_INFO,
  12886. "%s: Disable TT_DATA\n", __func__);
  12887. } else if (input_data[1] == 1) {
  12888. cd->show_tt_data = true;
  12889. pt_debug(dev, DL_INFO,
  12890. "%s: Enable TT_DATA\n", __func__);
  12891. } else {
  12892. rc = -EINVAL;
  12893. pt_debug(dev, DL_ERROR,
  12894. "%s: Invalid parameter: %d\n",
  12895. __func__, input_data[1]);
  12896. }
  12897. mutex_unlock(&(cd->system_lock));
  12898. break;
  12899. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12900. if (input_data[1] == cd->active_dut_generation) {
  12901. mutex_lock(&cd->system_lock);
  12902. cd->set_dut_generation = true;
  12903. mutex_unlock(&(cd->system_lock));
  12904. } else {
  12905. mutex_lock(&cd->system_lock);
  12906. if (input_data[1] == 0) {
  12907. cd->active_dut_generation = DUT_UNKNOWN;
  12908. cd->set_dut_generation = false;
  12909. } else if (input_data[1] == 1) {
  12910. cd->active_dut_generation = DUT_PIP1_ONLY;
  12911. cd->set_dut_generation = true;
  12912. } else if (input_data[1] == 2) {
  12913. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12914. cd->set_dut_generation = true;
  12915. } else {
  12916. rc = -EINVAL;
  12917. pt_debug(dev, DL_ERROR,
  12918. "%s: Invalid parameter: %d\n",
  12919. __func__, input_data[1]);
  12920. break;
  12921. }
  12922. cd->startup_status = STARTUP_STATUS_START;
  12923. pt_debug(cd->dev, DL_DEBUG,
  12924. "%s: Startup Status Reset\n", __func__);
  12925. mutex_unlock(&(cd->system_lock));
  12926. pt_debug(dev, DL_INFO,
  12927. "%s: Active DUT Generation Set to: %d\n",
  12928. __func__, cd->active_dut_generation);
  12929. /* Changing DUT generations full restart needed */
  12930. pt_queue_restart(cd);
  12931. }
  12932. break;
  12933. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12934. mutex_lock(&cd->system_lock);
  12935. if (input_data[1] == 0) {
  12936. cd->bridge_mode = false;
  12937. pt_debug(dev, DL_INFO,
  12938. "%s: Disable Bridge Mode\n", __func__);
  12939. } else if (input_data[1] == 1) {
  12940. cd->bridge_mode = true;
  12941. pt_debug(dev, DL_INFO,
  12942. "%s: Enable Bridge Mode\n", __func__);
  12943. } else {
  12944. rc = -EINVAL;
  12945. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12946. __func__, input_data[1]);
  12947. }
  12948. mutex_unlock(&(cd->system_lock));
  12949. break;
  12950. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12951. mutex_lock(&cd->system_lock);
  12952. /* Only a 7bit address is valid */
  12953. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12954. client->addr = input_data[1];
  12955. pt_debug(dev, DL_INFO,
  12956. "%s: Set I2C Address: 0x%2X\n",
  12957. __func__, client->addr);
  12958. } else {
  12959. rc = -EINVAL;
  12960. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12961. __func__, input_data[1]);
  12962. client->addr = 0x24;
  12963. }
  12964. mutex_unlock(&(cd->system_lock));
  12965. break;
  12966. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12967. mutex_lock(&cd->system_lock);
  12968. if (input_data[1] == 0) {
  12969. cd->flashless_dut = 0;
  12970. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12971. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12972. __func__);
  12973. } else if (input_data[1] == 1) {
  12974. cd->flashless_dut = 1;
  12975. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12976. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12977. __func__);
  12978. }
  12979. mutex_unlock(&(cd->system_lock));
  12980. break;
  12981. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12982. mutex_lock(&cd->system_lock);
  12983. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12984. cd->force_pip2_seq = input_data[1];
  12985. cd->pip2_cmd_tag_seq = input_data[1];
  12986. pt_debug(dev, DL_INFO,
  12987. "%s: Force PIP2 Seq to: 0x%02X\n",
  12988. __func__, input_data[1]);
  12989. } else {
  12990. cd->force_pip2_seq = 0;
  12991. pt_debug(dev, DL_INFO,
  12992. "%s: Clear Forced PIP2 Seq\n", __func__);
  12993. }
  12994. mutex_unlock(&(cd->system_lock));
  12995. break;
  12996. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12997. mutex_lock(&cd->system_lock);
  12998. if (input_data[1] == 0) {
  12999. cd->bl_with_no_int = 0;
  13000. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13001. } else if (input_data[1] == 1) {
  13002. cd->bl_with_no_int = 1;
  13003. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13004. __func__);
  13005. } else {
  13006. rc = -EINVAL;
  13007. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13008. __func__, input_data[1]);
  13009. }
  13010. mutex_unlock(&(cd->system_lock));
  13011. break;
  13012. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13013. mutex_lock(&cd->system_lock);
  13014. if (input_data[1] == 0) {
  13015. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13016. pt_debug(dev, DL_INFO,
  13017. "%s: Disable Calibration cache in host\n",
  13018. __func__);
  13019. } else if (input_data[1] == 1) {
  13020. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13021. pt_debug(dev, DL_INFO,
  13022. "%s: Enable Calibration cache in host\n",
  13023. __func__);
  13024. } else {
  13025. rc = -EINVAL;
  13026. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13027. __func__, input_data[1]);
  13028. }
  13029. mutex_unlock(&(cd->system_lock));
  13030. break;
  13031. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13032. mutex_lock(&cd->system_lock);
  13033. if (input_data[1] == 0) {
  13034. cd->multi_chip = PT_FEATURE_DISABLE;
  13035. cd->ttdl_bist_select = 0x07;
  13036. pt_debug(dev, DL_INFO,
  13037. "%s: Disable Multi-chip support\n", __func__);
  13038. } else if (input_data[1] == 1) {
  13039. cd->multi_chip = PT_FEATURE_ENABLE;
  13040. cd->ttdl_bist_select = 0x3F;
  13041. pt_debug(dev, DL_INFO,
  13042. "%s: Enable Multi-chip support\n", __func__);
  13043. } else {
  13044. rc = -EINVAL;
  13045. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13046. __func__, input_data[1]);
  13047. }
  13048. mutex_unlock(&(cd->system_lock));
  13049. break;
  13050. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13051. mutex_lock(&cd->system_lock);
  13052. if (input_data[1] <= 0x07) {
  13053. cd->panel_id_support = input_data[1];
  13054. pt_debug(dev, DL_INFO,
  13055. "%s: Set panel_id_support to %d\n",
  13056. __func__, cd->panel_id_support);
  13057. } else {
  13058. rc = -EINVAL;
  13059. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13060. __func__, input_data[1]);
  13061. }
  13062. mutex_unlock(&(cd->system_lock));
  13063. break;
  13064. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13065. mutex_lock(&cd->system_lock);
  13066. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13067. /*
  13068. * The timeout is changed for some cases so the
  13069. * pip_cmd_timeout_default is used to retore back to
  13070. * what the user requested as the new timeout.
  13071. */
  13072. cd->pip_cmd_timeout_default = input_data[1];
  13073. cd->pip_cmd_timeout = input_data[1];
  13074. pt_debug(dev, DL_INFO,
  13075. "%s: PIP Timeout = %d\n", __func__,
  13076. cd->pip_cmd_timeout_default);
  13077. } else {
  13078. rc = -EINVAL;
  13079. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13080. __func__, input_data[1]);
  13081. }
  13082. mutex_unlock(&(cd->system_lock));
  13083. break;
  13084. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13085. mutex_lock(&cd->system_lock);
  13086. if (input_data[1] == 0) {
  13087. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13088. pt_debug(dev, DL_INFO,
  13089. "%s: Disable tthe_tuner HID-USB format\n",
  13090. __func__);
  13091. } else if (input_data[1] == 1) {
  13092. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13093. pt_debug(dev, DL_INFO,
  13094. "%s: Enable tthe_tuner HID-USB format\n",
  13095. __func__);
  13096. } else {
  13097. rc = -EINVAL;
  13098. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13099. __func__, input_data[1]);
  13100. }
  13101. mutex_unlock(&(cd->system_lock));
  13102. break;
  13103. #endif /* TTDL_DIAGNOSTICS */
  13104. default:
  13105. rc = -EINVAL;
  13106. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13107. }
  13108. /* Enable watchdog timer */
  13109. if (!wd_disabled)
  13110. pt_start_wd_timer(cd);
  13111. pt_drv_debug_store_exit:
  13112. if (rc)
  13113. return rc;
  13114. return size;
  13115. }
  13116. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13117. pt_drv_debug_store);
  13118. /*******************************************************************************
  13119. * FUNCTION: pt_sleep_status_show
  13120. *
  13121. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13122. * sleep status as either ON or OFF
  13123. *
  13124. * RETURN:
  13125. * 0 = success
  13126. * !0 = failure
  13127. *
  13128. * PARAMETERS:
  13129. * *dev - pointer to device structure
  13130. * *attr - pointer to device attributes
  13131. * *buf - pointer to output buffer
  13132. ******************************************************************************/
  13133. static ssize_t pt_sleep_status_show(struct device *dev,
  13134. struct device_attribute *attr, char *buf)
  13135. {
  13136. struct pt_core_data *cd = dev_get_drvdata(dev);
  13137. ssize_t ret;
  13138. mutex_lock(&cd->system_lock);
  13139. if (cd->sleep_state == SS_SLEEP_ON)
  13140. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13141. else
  13142. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13143. mutex_unlock(&cd->system_lock);
  13144. return ret;
  13145. }
  13146. /*******************************************************************************
  13147. * FUNCTION: pt_panel_id_show
  13148. *
  13149. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13150. * detected panel ID from the DUT
  13151. *
  13152. * RETURN:
  13153. * 0 = success
  13154. * !0 = failure
  13155. *
  13156. * PARAMETERS:
  13157. * *dev - pointer to device structure
  13158. * *attr - pointer to device attributes
  13159. * *buf - pointer to output buffer
  13160. ******************************************************************************/
  13161. static ssize_t pt_panel_id_show(struct device *dev,
  13162. struct device_attribute *attr, char *buf)
  13163. {
  13164. struct pt_core_data *cd = dev_get_drvdata(dev);
  13165. ssize_t ret;
  13166. u8 pid = PANEL_ID_NOT_ENABLED;
  13167. int rc = 0;
  13168. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13169. /*
  13170. * The DUT should report the same panel ID from both the BL and
  13171. * the FW unless the panel_id feature is set to only
  13172. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13173. * to retrieve the panel_id.
  13174. */
  13175. if (cd->mode == PT_MODE_BOOTLOADER) {
  13176. /*
  13177. * Return the stored value if PT_PANEL_ID_BY_BL
  13178. * is not supported while other feature exits.
  13179. */
  13180. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13181. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13182. if (rc) {
  13183. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13184. "Failed to retrieve Panel ID. ",
  13185. "Using cached value\n",
  13186. __func__);
  13187. }
  13188. }
  13189. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13190. if (cd->panel_id_support &
  13191. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13192. /* For all systems sysinfo has the panel_id */
  13193. rc = pt_hid_output_get_sysinfo(cd);
  13194. if (!rc)
  13195. pid =
  13196. cd->sysinfo.sensing_conf_data.panel_id;
  13197. pt_debug(dev, DL_ERROR,
  13198. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13199. __func__, rc, pid);
  13200. }
  13201. } else {
  13202. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13203. __func__);
  13204. rc = -EPERM;
  13205. }
  13206. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13207. if (cd->mode == PT_MODE_BOOTLOADER) {
  13208. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13209. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13210. if (rc) {
  13211. pt_debug(dev, DL_ERROR,
  13212. "%s: BL get panel ID failed rc=%d\n",
  13213. __func__, rc);
  13214. }
  13215. }
  13216. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13217. if (cd->panel_id_support &
  13218. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13219. rc = pt_hid_output_get_sysinfo(cd);
  13220. if (!rc)
  13221. pid =
  13222. cd->sysinfo.sensing_conf_data.panel_id;
  13223. pt_debug(dev, DL_ERROR,
  13224. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13225. __func__, rc, pid);
  13226. }
  13227. } else {
  13228. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13229. __func__);
  13230. rc = -EPERM;
  13231. }
  13232. } else {
  13233. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13234. __func__);
  13235. rc = -EPERM;
  13236. }
  13237. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13238. rc, pid);
  13239. return ret;
  13240. }
  13241. /*******************************************************************************
  13242. * FUNCTION: pt_get_param_store
  13243. *
  13244. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13245. * ID to retrieve with the show method.
  13246. *
  13247. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13248. *
  13249. * RETURN: Size of passed in buffer if successful
  13250. *
  13251. * PARAMETERS:
  13252. * *dev - pointer to device structure
  13253. * *attr - pointer to device attributes
  13254. * *buf - pointer to output buffer
  13255. * size - size of buf
  13256. ******************************************************************************/
  13257. static ssize_t pt_get_param_store(struct device *dev,
  13258. struct device_attribute *attr, const char *buf, size_t size)
  13259. {
  13260. struct pt_core_data *cd = dev_get_drvdata(dev);
  13261. u32 input_data[2];
  13262. int length;
  13263. int rc = 0;
  13264. /* Maximum input of one value */
  13265. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13266. ARRAY_SIZE(input_data));
  13267. if (length != 1) {
  13268. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13269. __func__);
  13270. rc = -EINVAL;
  13271. goto exit;
  13272. }
  13273. mutex_lock(&cd->system_lock);
  13274. cd->get_param_id = input_data[0];
  13275. mutex_unlock(&(cd->system_lock));
  13276. exit:
  13277. if (rc)
  13278. return rc;
  13279. return size;
  13280. }
  13281. /*******************************************************************************
  13282. * FUNCTION: pt_get_param_show
  13283. *
  13284. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13285. * parameter data from the DUT based on the ID stored in the core
  13286. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13287. * communicate or some other error occures, an error status is returned
  13288. * with no value following.
  13289. * Output is in the form:
  13290. * Status: x
  13291. * 0xyyyyyyyy
  13292. * The 32bit data will only follow the status code if the status == 0
  13293. *
  13294. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13295. *
  13296. * RETURN:
  13297. * 0 = success
  13298. * !0 = failure
  13299. *
  13300. * PARAMETERS:
  13301. * *dev - pointer to device structure
  13302. * *attr - pointer to device attributes
  13303. * *buf - pointer to output buffer
  13304. ******************************************************************************/
  13305. static ssize_t pt_get_param_show(struct device *dev,
  13306. struct device_attribute *attr, char *buf)
  13307. {
  13308. struct pt_core_data *cd = dev_get_drvdata(dev);
  13309. ssize_t ret = 0;
  13310. int status;
  13311. u32 value = 0;
  13312. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13313. if (status) {
  13314. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13315. __func__, "pt_get_param", status);
  13316. ret = scnprintf(buf, strlen(buf),
  13317. "%s %d\n",
  13318. "Status:", status);
  13319. } else {
  13320. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13321. __func__, cd->get_param_id, value);
  13322. ret = scnprintf(buf, strlen(buf),
  13323. "Status: %d\n"
  13324. "0x%04X\n",
  13325. status, value);
  13326. }
  13327. return ret;
  13328. }
  13329. /*******************************************************************************
  13330. * FUNCTION: pt_ttdl_restart_show
  13331. *
  13332. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13333. * probed modules, calls startup() and then re-probes modules.
  13334. *
  13335. * RETURN: size of data written to sysfs node
  13336. *
  13337. * PARAMETERS:
  13338. * *dev - pointer to device structure
  13339. * *attr - pointer to device attributes structure
  13340. * *buf - pointer to print output buffer
  13341. ******************************************************************************/
  13342. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13343. struct device_attribute *attr, char *buf)
  13344. {
  13345. struct pt_core_data *cd = dev_get_drvdata(dev);
  13346. int t;
  13347. int rc = 0;
  13348. mutex_lock(&cd->system_lock);
  13349. cd->startup_state = STARTUP_NONE;
  13350. mutex_unlock(&(cd->system_lock));
  13351. /* ensure no left over exclusive access is still locked */
  13352. release_exclusive(cd, cd->dev);
  13353. pt_queue_restart(cd);
  13354. t = wait_event_timeout(cd->wait_q,
  13355. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13356. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13357. if (IS_TMO(t)) {
  13358. pt_debug(cd->dev, DL_ERROR,
  13359. "%s: TMO waiting for FW sentinel\n", __func__);
  13360. rc = -ETIME;
  13361. }
  13362. return scnprintf(buf, strlen(buf),
  13363. "Status: %d\n"
  13364. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13365. }
  13366. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13367. /*******************************************************************************
  13368. * FUNCTION: pt_pip2_gpio_read_show
  13369. *
  13370. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13371. * contents of the response to the passed in output buffer.
  13372. *
  13373. * RETURN: size of data written to sysfs node
  13374. *
  13375. * PARAMETERS:
  13376. * *dev - pointer to device structure
  13377. * *attr - pointer to device attributes structure
  13378. * *buf - pointer to print output buffer
  13379. ******************************************************************************/
  13380. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13381. struct device_attribute *attr, char *buf)
  13382. {
  13383. struct pt_core_data *cd = dev_get_drvdata(dev);
  13384. u8 status = 0;
  13385. u32 gpio_value = 0;
  13386. int rc = 0;
  13387. /* This functionality is only available in the BL */
  13388. if (cd->mode == PT_MODE_BOOTLOADER)
  13389. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13390. else
  13391. rc = -EPERM;
  13392. if (!rc) {
  13393. if (status == 0)
  13394. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13395. "Status: %d\n"
  13396. "DUT GPIO Reg: 0x%08X\n",
  13397. rc, gpio_value);
  13398. else
  13399. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13400. "Status: %d\n"
  13401. "DUT GPIO Reg: n/a\n",
  13402. status);
  13403. } else
  13404. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13405. "Status: %d\n"
  13406. "DUT GPIO Reg: n/a\n",
  13407. rc);
  13408. }
  13409. /*******************************************************************************
  13410. * FUNCTION: pt_pip2_version_show
  13411. *
  13412. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13413. * contents of the response to the passed in output buffer.
  13414. *
  13415. * RETURN: size of data written to sysfs node
  13416. *
  13417. * PARAMETERS:
  13418. * *dev - pointer to device structure
  13419. * *attr - pointer to device attributes structure
  13420. * *buf - pointer to print output buffer
  13421. ******************************************************************************/
  13422. static ssize_t pt_pip2_version_show(struct device *dev,
  13423. struct device_attribute *attr, char *buf)
  13424. {
  13425. int rc = 0;
  13426. struct pt_core_data *cd = dev_get_drvdata(dev);
  13427. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13428. rc = pt_pip2_get_version(cd);
  13429. if (!rc) {
  13430. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13431. "PIP VERSION : %02X.%02X\n"
  13432. "BL VERSION : %02X.%02X\n"
  13433. "FW VERSION : %02X.%02X\n"
  13434. "SILICON ID : %04X.%04X\n"
  13435. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13436. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13437. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13438. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13439. ttdata->chip_id, ttdata->chip_rev,
  13440. ttdata->uid[0], ttdata->uid[1],
  13441. ttdata->uid[2], ttdata->uid[3],
  13442. ttdata->uid[4], ttdata->uid[5],
  13443. ttdata->uid[6], ttdata->uid[7],
  13444. ttdata->uid[8], ttdata->uid[9],
  13445. ttdata->uid[10], ttdata->uid[11]);
  13446. } else {
  13447. pt_debug(dev, DL_ERROR,
  13448. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13449. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13450. "PIP VERSION : -\n"
  13451. "BL VERSION : -\n"
  13452. "FW VERSION : -\n"
  13453. "SILICON ID : -\n"
  13454. "UID : -\n");
  13455. }
  13456. }
  13457. #ifdef TTDL_DIAGNOSTICS
  13458. /*******************************************************************************
  13459. * FUNCTION: pt_ttdl_status_show
  13460. *
  13461. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13462. * variable states and GPIO levels. Additional information printed when
  13463. * TTDL_DIAGNOSTICS is enabled.
  13464. *
  13465. * NOTE: All counters will be reset to 0 when this function is called.
  13466. *
  13467. * RETURN:
  13468. * 0 = success
  13469. * !0 = failure
  13470. *
  13471. * PARAMETERS:
  13472. * *dev - pointer to device structure
  13473. * *attr - pointer to device attributes
  13474. * *buf - pointer to output buffer
  13475. ******************************************************************************/
  13476. static ssize_t pt_ttdl_status_show(struct device *dev,
  13477. struct device_attribute *attr, char *buf)
  13478. {
  13479. struct pt_core_data *cd = dev_get_drvdata(dev);
  13480. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13481. struct i2c_client *client = to_i2c_client(dev);
  13482. ssize_t ret;
  13483. u16 cal_size = 0;
  13484. unsigned short crc = 0;
  13485. if (cd->cal_cache_in_host)
  13486. _pt_manage_local_cal_data(dev,
  13487. PT_CAL_DATA_INFO, &cal_size, &crc);
  13488. ret = scnprintf(buf, strlen(buf),
  13489. "%s: 0x%04X\n"
  13490. "%s: %d\n"
  13491. "%s: %s\n"
  13492. "%s: %s %s\n"
  13493. "%s: %s\n"
  13494. "%s: 0x%02X\n"
  13495. "%s: %s\n"
  13496. "%s: %s\n"
  13497. "%s: %s\n"
  13498. "%s: %s\n"
  13499. "%s: %d\n"
  13500. "%s: %d\n"
  13501. "%s: %s\n"
  13502. "%s: %s\n"
  13503. "%s: %d\n"
  13504. #ifdef TTDL_DIAGNOSTICS
  13505. "%s: %d\n"
  13506. "%s: %d\n"
  13507. "%s: %d\n"
  13508. "%s: %d\n"
  13509. "%s: %d\n"
  13510. "%s: %d\n"
  13511. "%s: %d\n"
  13512. "%s: %d\n"
  13513. "%s: %d\n"
  13514. "%s: %d\n"
  13515. "%s: %s\n"
  13516. "%s: %s\n"
  13517. "%s: %s\n"
  13518. "%s: %d\n"
  13519. "%s: 0x%04X\n"
  13520. "%s: %s\n"
  13521. #endif /* TTDL_DIAGNOSTICS */
  13522. ,
  13523. "Startup Status ", cd->startup_status,
  13524. "TTDL Debug Level ", cd->debug_level,
  13525. "Mode ",
  13526. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13527. "Operational" : "BL") : "Unknown",
  13528. "DUT Generation ",
  13529. cd->active_dut_generation ?
  13530. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13531. "PT TC/TT" : "Gen5/6") : "Unknown",
  13532. cd->active_dut_generation ?
  13533. (cd->set_dut_generation == true ?
  13534. "(Set)" : "(Detected)") : "",
  13535. "HW Detected ",
  13536. cd->hw_detected ? "True" : "False",
  13537. "I2C Address ",
  13538. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13539. "Active Bus Module ",
  13540. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13541. "Flashless Mode ",
  13542. cd->flashless_dut == 1 ? "Yes" : "No",
  13543. "GPIO state - IRQ ",
  13544. cd->cpdata->irq_stat ?
  13545. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13546. "High" : "Low") : "not defined",
  13547. "GPIO state - TP_XRES ",
  13548. pdata->core_pdata->rst_gpio ?
  13549. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13550. "High" : "Low") : "not defined",
  13551. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13552. "Startup Retry Count ", cd->startup_retry_count,
  13553. "WD - Manual Force Stop ",
  13554. cd->watchdog_force_stop ? "True" : "False",
  13555. "WD - Enabled ",
  13556. cd->watchdog_enabled ? "True" : "False",
  13557. "WD - Interval (ms) ", cd->watchdog_interval
  13558. #ifdef TTDL_DIAGNOSTICS
  13559. , "WD - Triggered Count ", cd->watchdog_count,
  13560. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13561. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13562. "WD - XRES Count ", cd->wd_xres_count,
  13563. "IRQ Triggered Count ", cd->irq_count,
  13564. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13565. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13566. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13567. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13568. "Error GPIO trigger type ", cd->err_gpio_type,
  13569. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13570. "Suppress No-Flash Auto BL ",
  13571. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13572. "Yes" : "No",
  13573. "Calibration Cache on host ",
  13574. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13575. "Yes" : "No",
  13576. "Calibration Cache size ", cal_size,
  13577. "Calibration Cache chip ID ", crc,
  13578. "Multi-Chip Support ",
  13579. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13580. #endif /* TTDL_DIAGNOSTICS */
  13581. );
  13582. #ifdef TTDL_DIAGNOSTICS
  13583. /* Reset all diagnostic counters */
  13584. cd->watchdog_count = 0;
  13585. cd->watchdog_irq_stuck_count = 0;
  13586. cd->watchdog_failed_access_count = 0;
  13587. cd->wd_xres_count = 0;
  13588. cd->irq_count = 0;
  13589. cd->bl_retry_packet_count = 0;
  13590. cd->pip2_crc_error_count = 0;
  13591. cd->bus_transmit_error_count = 0;
  13592. #endif
  13593. return ret;
  13594. }
  13595. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13596. /*******************************************************************************
  13597. * FUNCTION: pt_pip2_enter_bl_show
  13598. *
  13599. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13600. * the DUT into the BL and show the success or failure of entering the BL
  13601. *
  13602. * RETURN:
  13603. * 0 = success
  13604. * !0 = failure
  13605. *
  13606. * PARAMETERS:
  13607. * *dev - pointer to device structure
  13608. * *attr - pointer to device attributes
  13609. * *buf - pointer to output buffer
  13610. ******************************************************************************/
  13611. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13612. struct device_attribute *attr, char *buf)
  13613. {
  13614. ssize_t ret = 0;
  13615. int rc = 0;
  13616. int result = 0;
  13617. u8 mode = PT_MODE_UNKNOWN;
  13618. struct pt_core_data *cd = dev_get_drvdata(dev);
  13619. /* Turn off the TTDL WD before enter bootloader */
  13620. pt_stop_wd_timer(cd);
  13621. /* Ensure NO enumeration work is queued or will be queued */
  13622. cancel_work_sync(&cd->enum_work);
  13623. mutex_lock(&cd->system_lock);
  13624. cd->bridge_mode = true;
  13625. mutex_unlock(&cd->system_lock);
  13626. /* set mode to operational to avoid any extra PIP traffic */
  13627. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13628. switch (result) {
  13629. case PT_ENTER_BL_PASS:
  13630. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13631. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13632. break;
  13633. case PT_ENTER_BL_ERROR:
  13634. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13635. rc,
  13636. " Unknown Error");
  13637. break;
  13638. case PT_ENTER_BL_RESET_FAIL:
  13639. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13640. rc,
  13641. " Soft Reset Failed");
  13642. break;
  13643. case PT_ENTER_BL_HID_START_BL_FAIL:
  13644. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13645. rc,
  13646. " PIP Start BL Cmd Failed");
  13647. break;
  13648. case PT_ENTER_BL_CONFIRM_FAIL:
  13649. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13650. rc,
  13651. " Error confirming DUT entered BL");
  13652. break;
  13653. default:
  13654. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13655. rc, " Unknown Error");
  13656. break;
  13657. };
  13658. /* Allow enumeration work to be queued again */
  13659. cd->bridge_mode = false;
  13660. return ret;
  13661. }
  13662. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13663. /*******************************************************************************
  13664. * FUNCTION: pt_pip2_exit_bl_show
  13665. *
  13666. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13667. * launch the APP and put the DUT Application mode
  13668. *
  13669. * RETURN:
  13670. * 0 = success
  13671. * !0 = failure
  13672. *
  13673. * PARAMETERS:
  13674. * *dev - pointer to device structure
  13675. * *attr - pointer to device attributes
  13676. * *buf - pointer to output buffer
  13677. ******************************************************************************/
  13678. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13679. struct device_attribute *attr, char *buf)
  13680. {
  13681. struct pt_core_data *cd = dev_get_drvdata(dev);
  13682. ssize_t ret = 0;
  13683. int rc = 0;
  13684. u8 status_str[PT_STATUS_STR_LEN];
  13685. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13686. /*
  13687. * Perform enum if startup_status doesn't reach to
  13688. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13689. */
  13690. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13691. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13692. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13693. strlcpy(status_str,
  13694. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13695. }
  13696. }
  13697. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13698. rc, status_str);
  13699. return ret;
  13700. }
  13701. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13702. #endif
  13703. #ifdef EASYWAKE_TSG6
  13704. /*******************************************************************************
  13705. * FUNCTION: pt_easy_wakeup_gesture_show
  13706. *
  13707. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13708. * current easy wakeup gesture
  13709. *
  13710. * RETURN:
  13711. * 0 = success
  13712. * !0 = failure
  13713. *
  13714. * PARAMETERS:
  13715. * *dev - pointer to device structure
  13716. * *attr - pointer to device attributes
  13717. * *buf - pointer to output buffer
  13718. ******************************************************************************/
  13719. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13720. struct device_attribute *attr, char *buf)
  13721. {
  13722. struct pt_core_data *cd = dev_get_drvdata(dev);
  13723. ssize_t ret;
  13724. mutex_lock(&cd->system_lock);
  13725. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13726. cd->easy_wakeup_gesture);
  13727. mutex_unlock(&cd->system_lock);
  13728. return ret;
  13729. }
  13730. /*******************************************************************************
  13731. * FUNCTION: pt_easy_wakeup_gesture_store
  13732. *
  13733. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13734. * allows the wake gesture to be set to a custom value.
  13735. *
  13736. * RETURN: Size of passed in buffer is success
  13737. *
  13738. * PARAMETERS:
  13739. * *dev - pointer to device structure
  13740. * *attr - pointer to device attributes
  13741. * *buf - pointer to buffer that hold the command parameters
  13742. * size - size of buf
  13743. ******************************************************************************/
  13744. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13745. struct device_attribute *attr, const char *buf, size_t size)
  13746. {
  13747. struct pt_core_data *cd = dev_get_drvdata(dev);
  13748. u32 input_data[2];
  13749. int length;
  13750. int rc = 0;
  13751. /* Maximum input of one value */
  13752. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13753. ARRAY_SIZE(input_data));
  13754. if (length != 1) {
  13755. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13756. __func__);
  13757. rc = -EINVAL;
  13758. goto exit;
  13759. }
  13760. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13761. __func__, cd->features.easywake);
  13762. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13763. rc = -EINVAL;
  13764. goto exit;
  13765. }
  13766. pm_runtime_get_sync(dev);
  13767. mutex_lock(&cd->system_lock);
  13768. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13769. cd->easy_wakeup_gesture = (u8)input_data[0];
  13770. pt_debug(dev, DL_INFO,
  13771. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13772. __func__, cd->easy_wakeup_gesture);
  13773. } else
  13774. rc = -ENODEV;
  13775. mutex_unlock(&cd->system_lock);
  13776. pm_runtime_put(dev);
  13777. exit:
  13778. if (rc)
  13779. return rc;
  13780. return size;
  13781. }
  13782. /*******************************************************************************
  13783. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13784. *
  13785. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13786. * show the TSG6 easywake gesture ID
  13787. *
  13788. * RETURN:
  13789. * 0 = success
  13790. * !0 = failure
  13791. *
  13792. * PARAMETERS:
  13793. * *dev - pointer to device structure
  13794. * *attr - pointer to device attributes
  13795. * *buf - pointer to output buffer
  13796. ******************************************************************************/
  13797. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13798. struct device_attribute *attr, char *buf)
  13799. {
  13800. struct pt_core_data *cd = dev_get_drvdata(dev);
  13801. ssize_t ret;
  13802. mutex_lock(&cd->system_lock);
  13803. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13804. cd->gesture_id);
  13805. mutex_unlock(&cd->system_lock);
  13806. return ret;
  13807. }
  13808. /*******************************************************************************
  13809. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13810. *
  13811. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13812. * show the TSG6 easywake gesture data in the following format:
  13813. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13814. *
  13815. * RETURN:
  13816. * 0 = success
  13817. * !0 = failure
  13818. *
  13819. * PARAMETERS:
  13820. * *dev - pointer to device structure
  13821. * *attr - pointer to device attributes
  13822. * *buf - pointer to output buffer
  13823. ******************************************************************************/
  13824. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13825. struct device_attribute *attr, char *buf)
  13826. {
  13827. struct pt_core_data *cd = dev_get_drvdata(dev);
  13828. ssize_t ret = 0;
  13829. int i;
  13830. mutex_lock(&cd->system_lock);
  13831. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13832. for (i = 0; i < cd->gesture_data_length; i++)
  13833. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13834. "0x%02X\n", cd->gesture_data[i]);
  13835. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13836. "(%d bytes)\n", cd->gesture_data_length);
  13837. mutex_unlock(&cd->system_lock);
  13838. return ret;
  13839. }
  13840. #endif /* EASYWAKE_TSG6 */
  13841. #ifdef TTDL_DIAGNOSTICS
  13842. /*******************************************************************************
  13843. * FUNCTION: pt_err_gpio_show
  13844. *
  13845. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13846. * setting up the gpio was successful
  13847. *
  13848. * RETURN: Char buffer with printed GPIO creation state
  13849. *
  13850. * PARAMETERS:
  13851. * *dev - pointer to device structure
  13852. * *attr - pointer to device attributes
  13853. * *buf - pointer to output buffer
  13854. ******************************************************************************/
  13855. static ssize_t pt_err_gpio_show(struct device *dev,
  13856. struct device_attribute *attr, char *buf)
  13857. {
  13858. struct pt_core_data *cd = dev_get_drvdata(dev);
  13859. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13860. "Err GPIO (%d) : %s\n"
  13861. "Err GPIO trig type: %d\n",
  13862. cd->err_gpio,
  13863. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13864. "HIGH" : "low") : "not defined"),
  13865. cd->err_gpio_type);
  13866. }
  13867. /*******************************************************************************
  13868. * FUNCTION: pt_err_gpio_store
  13869. *
  13870. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13871. * available host GPIO to be used to trigger when TTDL detects a PIP
  13872. * command/response timeout.
  13873. *
  13874. * RETURN:
  13875. * 0 = success
  13876. * !0 = failure
  13877. *
  13878. * PARAMETERS:
  13879. * *dev - pointer to device structure
  13880. * *attr - pointer to device attributes
  13881. * *buf - pointer to buffer that hold the command parameters
  13882. * size - size of buf
  13883. ******************************************************************************/
  13884. static ssize_t pt_err_gpio_store(struct device *dev,
  13885. struct device_attribute *attr, const char *buf, size_t size)
  13886. {
  13887. struct pt_core_data *cd = dev_get_drvdata(dev);
  13888. unsigned long gpio;
  13889. int rc = 0;
  13890. u32 input_data[3];
  13891. int length;
  13892. u8 err_type;
  13893. input_data[0] = 0;
  13894. input_data[1] = 0;
  13895. /* Maximmum input is two elements */
  13896. length = _pt_ic_parse_input(dev, buf, size,
  13897. input_data, ARRAY_SIZE(input_data));
  13898. if (length < 1 || length > 2) {
  13899. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13900. __func__);
  13901. rc = -EINVAL;
  13902. goto exit;
  13903. }
  13904. gpio = input_data[0];
  13905. err_type = (u8)input_data[1];
  13906. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13907. rc = -EINVAL;
  13908. goto exit;
  13909. }
  13910. mutex_lock(&cd->system_lock);
  13911. gpio_free(gpio);
  13912. rc = gpio_request(gpio, NULL);
  13913. if (rc) {
  13914. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13915. rc = -ENODEV;
  13916. } else {
  13917. cd->err_gpio = gpio;
  13918. cd->err_gpio_type = err_type;
  13919. gpio_direction_output(gpio, 0);
  13920. }
  13921. mutex_unlock(&cd->system_lock);
  13922. exit:
  13923. if (rc)
  13924. return rc;
  13925. return size;
  13926. }
  13927. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13928. pt_err_gpio_store);
  13929. /*******************************************************************************
  13930. * FUNCTION: pt_drv_irq_show
  13931. *
  13932. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13933. * TTDL interrupt is enabled/disabled
  13934. *
  13935. * RETURN:
  13936. * 0 = success
  13937. * !0 = failure
  13938. *
  13939. * PARAMETERS:
  13940. * *dev - pointer to device structure
  13941. * *attr - pointer to device attributes
  13942. * *buf - pointer to output buffer
  13943. ******************************************************************************/
  13944. static ssize_t pt_drv_irq_show(struct device *dev,
  13945. struct device_attribute *attr, char *buf)
  13946. {
  13947. struct pt_core_data *cd = dev_get_drvdata(dev);
  13948. ssize_t ret = 0;
  13949. mutex_lock(&cd->system_lock);
  13950. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13951. if (cd->irq_enabled)
  13952. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13953. "Driver interrupt: ENABLED\n");
  13954. else
  13955. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13956. "Driver interrupt: DISABLED\n");
  13957. mutex_unlock(&cd->system_lock);
  13958. return ret;
  13959. }
  13960. /*******************************************************************************
  13961. * FUNCTION: pt_drv_irq_store
  13962. *
  13963. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13964. * IRQ to be enabled/disabled.
  13965. *
  13966. * RETURN: Size of passed in buffer
  13967. *
  13968. * PARAMETERS:
  13969. * *dev - pointer to device structure
  13970. * *attr - pointer to device attributes
  13971. * *buf - pointer to buffer that hold the command parameters
  13972. * size - size of buf
  13973. ******************************************************************************/
  13974. static ssize_t pt_drv_irq_store(struct device *dev,
  13975. struct device_attribute *attr, const char *buf, size_t size)
  13976. {
  13977. struct pt_core_data *cd = dev_get_drvdata(dev);
  13978. u32 input_data[2];
  13979. int length;
  13980. int rc = 0;
  13981. /* Maximum input of one value */
  13982. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13983. ARRAY_SIZE(input_data));
  13984. if (length != 1) {
  13985. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13986. __func__);
  13987. rc = -EINVAL;
  13988. goto exit;
  13989. }
  13990. mutex_lock(&cd->system_lock);
  13991. switch (input_data[0]) {
  13992. case 0:
  13993. if (cd->irq_enabled) {
  13994. cd->irq_enabled = false;
  13995. /* Disable IRQ has no return value to check */
  13996. disable_irq_nosync(cd->irq);
  13997. pt_debug(dev, DL_INFO,
  13998. "%s: Driver IRQ now disabled\n",
  13999. __func__);
  14000. } else
  14001. pt_debug(dev, DL_INFO,
  14002. "%s: Driver IRQ already disabled\n",
  14003. __func__);
  14004. break;
  14005. case 1:
  14006. if (cd->irq_enabled == false) {
  14007. cd->irq_enabled = true;
  14008. enable_irq(cd->irq);
  14009. pt_debug(dev, DL_INFO,
  14010. "%s: Driver IRQ now enabled\n",
  14011. __func__);
  14012. } else
  14013. pt_debug(dev, DL_INFO,
  14014. "%s: Driver IRQ already enabled\n",
  14015. __func__);
  14016. break;
  14017. default:
  14018. rc = -EINVAL;
  14019. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14020. }
  14021. mutex_unlock(&(cd->system_lock));
  14022. exit:
  14023. if (rc)
  14024. return rc;
  14025. return size;
  14026. }
  14027. /*******************************************************************************
  14028. * FUNCTION: pt_pip2_bin_hdr_show
  14029. *
  14030. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14031. * the bin file header from flash and show each field
  14032. *
  14033. * RETURN:
  14034. * 0 = success
  14035. * !0 = failure
  14036. *
  14037. * PARAMETERS:
  14038. * *dev - pointer to device structure
  14039. * *attr - pointer to device attributes
  14040. * *buf - pointer to output buffer
  14041. ******************************************************************************/
  14042. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14043. struct device_attribute *attr, char *buf)
  14044. {
  14045. ssize_t ret = 0;
  14046. struct pt_bin_file_hdr hdr = {0};
  14047. int rc;
  14048. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14049. ret = scnprintf(buf, strlen(buf),
  14050. "%s: %d\n"
  14051. "%s: %d\n"
  14052. "%s: 0x%04X\n"
  14053. "%s: %d\n"
  14054. "%s: %d\n"
  14055. "%s: %d\n"
  14056. "%s: 0x%08X\n"
  14057. "%s: 0x%04X\n"
  14058. "%s: 0x%04X\n"
  14059. "%s: %d\n"
  14060. "%s: %d\n",
  14061. "Status", rc,
  14062. "Header Length ", hdr.length,
  14063. "TTPID ", hdr.ttpid,
  14064. "FW Major Ver ", hdr.fw_major,
  14065. "FW Minor Ver ", hdr.fw_minor,
  14066. "FW Rev Control ", hdr.fw_rev_ctrl,
  14067. "FW CRC ", hdr.fw_crc,
  14068. "Silicon Rev ", hdr.si_rev,
  14069. "Silicon ID ", hdr.si_id,
  14070. "Config Ver ", hdr.config_ver,
  14071. "HEX File Size ", hdr.hex_file_size
  14072. );
  14073. return ret;
  14074. }
  14075. /*******************************************************************************
  14076. * FUNCTION: pt_platform_data_show
  14077. *
  14078. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14079. * active platform data including: GPIOs, Vendor and Product IDs,
  14080. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14081. * HID registers, and Easy wake gesture
  14082. *
  14083. * RETURN: Size of printed data
  14084. *
  14085. * PARAMETERS:
  14086. * *dev - pointer to device structure
  14087. * *attr - pointer to device attributes
  14088. * *buf - pointer to output buffer
  14089. ******************************************************************************/
  14090. static ssize_t pt_platform_data_show(struct device *dev,
  14091. struct device_attribute *attr, char *buf)
  14092. {
  14093. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14094. struct pt_core_data *cd = dev_get_drvdata(dev);
  14095. ssize_t ret;
  14096. ret = scnprintf(buf, strlen(buf),
  14097. "%s: %d\n"
  14098. "%s: %d\n"
  14099. "%s: %d\n"
  14100. "%s: %d\n"
  14101. "%s: %d\n"
  14102. "%s: %d\n"
  14103. "%s: %d\n"
  14104. "%s: %d\n"
  14105. "%s: %d\n"
  14106. "%s: %d\n"
  14107. "%s: %d\n"
  14108. "%s: %d\n"
  14109. "%s: %d\n"
  14110. "%s: %d\n"
  14111. "%s: %d\n"
  14112. "%s: %d\n"
  14113. "%s: %d\n"
  14114. "%s: %d\n"
  14115. "%s: %d\n"
  14116. "%s: %d\n"
  14117. "%s: %d\n"
  14118. "%s: %d\n"
  14119. "%s: %d\n"
  14120. "%s: %d\n"
  14121. "%s: %d\n"
  14122. "%s: %d\n"
  14123. "%s: %d\n"
  14124. "%s: %s\n"
  14125. "%s: %s\n"
  14126. "%s: %d\n",
  14127. "Status", 0,
  14128. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14129. "Interrupt GPIO Value ",
  14130. pdata->core_pdata->irq_gpio ?
  14131. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14132. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14133. "Reset GPIO Value ",
  14134. pdata->core_pdata->rst_gpio ?
  14135. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14136. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14137. "DDI Reset GPIO Value ",
  14138. pdata->core_pdata->ddi_rst_gpio ?
  14139. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14140. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14141. "VDDI GPIO Value ",
  14142. pdata->core_pdata->vddi_gpio ?
  14143. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14144. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14145. "VCC GPIO Value ",
  14146. pdata->core_pdata->vcc_gpio ?
  14147. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14148. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14149. "AVDD GPIO Value ",
  14150. pdata->core_pdata->avdd_gpio ?
  14151. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14152. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14153. "AVEE GPIO Value ",
  14154. pdata->core_pdata->avee_gpio ?
  14155. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14156. "Vendor ID ", pdata->core_pdata->vendor_id,
  14157. "Product ID ", pdata->core_pdata->product_id,
  14158. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14159. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14160. "Core data flags ", pdata->core_pdata->flags,
  14161. "MT data flags ", pdata->mt_pdata->flags,
  14162. "Loader data flags ", pdata->loader_pdata->flags,
  14163. "Level trigger delay (us) ",
  14164. pdata->core_pdata->level_irq_udelay,
  14165. "HID Descriptor Register ",
  14166. pdata->core_pdata->hid_desc_register,
  14167. "HID Output Register ",
  14168. cd->hid_desc.output_register,
  14169. "HID Command Register ",
  14170. cd->hid_desc.command_register,
  14171. "Easy wakeup gesture ",
  14172. pdata->core_pdata->easy_wakeup_gesture,
  14173. "Config DUT generation ",
  14174. pdata->core_pdata->config_dut_generation ?
  14175. (pdata->core_pdata->config_dut_generation ==
  14176. CONFIG_DUT_PIP2_CAPABLE ?
  14177. "PT TC/TT" : "Gen5/6") : "Auto",
  14178. "Watchdog Force Stop ",
  14179. pdata->core_pdata->watchdog_force_stop ?
  14180. "True" : "False",
  14181. "Panel ID Support ",
  14182. pdata->core_pdata->panel_id_support);
  14183. return ret;
  14184. }
  14185. #define PT_ERR_STR_SIZE 64
  14186. /*******************************************************************************
  14187. * FUNCTION: pt_bist_bus_test
  14188. *
  14189. * SUMMARY: Tests the connectivity of the active bus pins:
  14190. * I2C - SDA and SCL
  14191. * SPI - MOSI, MISO, CLK
  14192. *
  14193. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14194. * bus. If any data is read we know the I2C/SPI pins are connected
  14195. *
  14196. * RETURN:
  14197. * 0 = Success
  14198. * !0 = Failure
  14199. *
  14200. * PARAMETERS:
  14201. * *dev - pointer to device structure
  14202. * *net_toggled - pointer to where to store if bus toggled
  14203. * *err_str - pointer to error string buffer
  14204. ******************************************************************************/
  14205. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14206. {
  14207. struct pt_core_data *cd = dev_get_drvdata(dev);
  14208. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14209. u8 *read_buf = NULL;
  14210. int bytes_read = 0;
  14211. int rc = 0;
  14212. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14213. if (read_buf == NULL) {
  14214. rc = -ENOMEM;
  14215. goto exit;
  14216. }
  14217. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14218. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14219. disable_irq(cd->irq);
  14220. mutex_lock(&cd->system_lock);
  14221. cd->irq_disabled = true;
  14222. mutex_unlock(&cd->system_lock);
  14223. /*
  14224. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14225. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14226. */
  14227. usleep_range(5000, 6000);
  14228. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14229. __func__, (int)sizeof(ver_cmd));
  14230. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14231. ">>> User CMD");
  14232. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14233. if (rc) {
  14234. pt_debug(dev, DL_ERROR,
  14235. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14236. *net_toggled = 0;
  14237. strlcpy(err_str,
  14238. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14239. goto exit_enable_irq;
  14240. }
  14241. usleep_range(4000, 5000);
  14242. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14243. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14244. __func__, bytes_read);
  14245. if (bytes_read == 0) {
  14246. *net_toggled = 0;
  14247. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14248. __func__);
  14249. strlcpy(err_str,
  14250. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14251. rc = -EIO;
  14252. goto exit_enable_irq;
  14253. } else {
  14254. if (cd->bus_ops->bustype == BUS_I2C)
  14255. *net_toggled = 1;
  14256. else {
  14257. if ((bytes_read > 3) &&
  14258. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14259. PIP2_CMD_ID_VERSION)
  14260. *net_toggled = 1;
  14261. else {
  14262. *net_toggled = 0;
  14263. pt_debug(dev, DL_INFO,
  14264. "%s: BUS Read Failed, %d bytes read\n",
  14265. __func__, bytes_read);
  14266. strlcpy(err_str,
  14267. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14268. }
  14269. }
  14270. }
  14271. exit_enable_irq:
  14272. enable_irq(cd->irq);
  14273. usleep_range(5000, 6000);
  14274. mutex_lock(&cd->system_lock);
  14275. cd->irq_disabled = false;
  14276. mutex_unlock(&cd->system_lock);
  14277. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14278. exit:
  14279. kfree(read_buf);
  14280. return rc;
  14281. }
  14282. /*******************************************************************************
  14283. * FUNCTION: pt_bist_irq_test
  14284. *
  14285. * SUMMARY: Tests the connectivity of the IRQ net
  14286. *
  14287. * This test will ensure there is a good connection between the host
  14288. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14289. * asserted and if so keep reading messages off of the bus until
  14290. * it de-asserts. Possible outcomes:
  14291. * - IRQ was already de-asserted: Send a PIP command and if an
  14292. * interrupt is generated the test passes.
  14293. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14294. * test passes.
  14295. * - IRQ stays asserted: After reading the bus multiple times
  14296. * the IRQ stays asserted. Likely open or shorted to GND
  14297. *
  14298. * RETURN:
  14299. * 0 = Success
  14300. * !0 = Failure
  14301. *
  14302. * PARAMETERS:
  14303. * *dev - pointer to device structure
  14304. * *bus_toggled - pointer to where to store if bus toggled
  14305. * *irq_toggled - pointer to where to store if IRQ toggled
  14306. * *xres_toggled - pointer to where to store if XRES toggled
  14307. * *err_str - pointer to error string buffer
  14308. ******************************************************************************/
  14309. static int pt_bist_irq_test(struct device *dev,
  14310. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14311. {
  14312. struct pt_core_data *cd = dev_get_drvdata(dev);
  14313. u8 *read_buf = NULL;
  14314. u8 mode = PT_MODE_UNKNOWN;
  14315. u16 actual_read_len;
  14316. int bytes_read = 0;
  14317. int count = 0;
  14318. int rc = 0;
  14319. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14320. if (read_buf == NULL) {
  14321. rc = -ENOMEM;
  14322. goto exit;
  14323. }
  14324. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14325. cd->irq_count = 0;
  14326. /*
  14327. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14328. * the bus based on the 2 byte initial length read. Try up to 5x.
  14329. */
  14330. while (pt_check_irq_asserted(cd) && count < 5) {
  14331. count++;
  14332. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14333. }
  14334. if (count > 1 && count < 5 && bytes_read > 0) {
  14335. /*
  14336. * IRQ was stuck but data was successfully read from the
  14337. * bus releasing the IRQ line.
  14338. */
  14339. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14340. __func__, count, bytes_read);
  14341. *bus_toggled = 1;
  14342. *irq_toggled = 1;
  14343. goto exit;
  14344. }
  14345. if (count == 5 && bytes_read == 0) {
  14346. /*
  14347. * Looped 5x and read nothing off the bus yet the IRQ is still
  14348. * asserted. Possible conditions:
  14349. * - IRQ open circuit
  14350. * - IRQ shorted to GND
  14351. * - I2C/SPI bus is disconnected
  14352. * - FW holding the pin low
  14353. * Try entering the BL to see if communication works there.
  14354. */
  14355. mode = PT_MODE_IGNORE;
  14356. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14357. if (rc) {
  14358. pt_debug(dev, DL_ERROR,
  14359. "%s Failed to enter BL\n", __func__);
  14360. strlcpy(err_str,
  14361. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14362. *irq_toggled = 0;
  14363. goto exit;
  14364. }
  14365. /*
  14366. * If original mode was operational and we successfully
  14367. * entered the BL, then the XRES net must have toggled
  14368. */
  14369. if (mode == PT_MODE_OPERATIONAL)
  14370. *xres_toggled = 1;
  14371. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14372. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14373. &actual_read_len);
  14374. if (rc) {
  14375. /*
  14376. * Could not communicate to DUT in BL mode. Save the
  14377. * error string, slim chance but the XRES test below may
  14378. * show the IRQ is actually working.
  14379. */
  14380. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14381. pt_debug(dev, DL_ERROR,
  14382. "%s: %s, count=%d bytes_read=%d\n",
  14383. __func__, err_str, count, bytes_read);
  14384. *irq_toggled = 0;
  14385. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14386. goto exit;
  14387. } else {
  14388. *bus_toggled = 1;
  14389. *irq_toggled = 1;
  14390. goto exit;
  14391. }
  14392. }
  14393. if (pt_check_irq_asserted(cd)) {
  14394. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14395. rc = -EIO;
  14396. *irq_toggled = 0;
  14397. goto exit;
  14398. }
  14399. /* Try sending a PIP command to see if we get a response */
  14400. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14401. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14402. if (rc) {
  14403. /*
  14404. * Potential IRQ issue, no communication in App mode, attempt
  14405. * the same command in the BL
  14406. */
  14407. mode = PT_MODE_IGNORE;
  14408. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14409. if (rc) {
  14410. pt_debug(dev, DL_ERROR,
  14411. "%s Failed to enter BL\n", __func__);
  14412. *irq_toggled = 0;
  14413. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14414. goto exit;
  14415. }
  14416. /*
  14417. * If original mode was operational and we successfully
  14418. * entered the BL, this will be useful info for the tp_xres
  14419. * test below.
  14420. */
  14421. if (mode == PT_MODE_OPERATIONAL)
  14422. *xres_toggled = 1;
  14423. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14424. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14425. &actual_read_len);
  14426. if (rc) {
  14427. /*
  14428. * Could not communicate in FW mode or BL mode. Save the
  14429. * error string, slim chance but the XRES test below may
  14430. * show the IRQ is actually working.
  14431. */
  14432. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14433. pt_debug(dev, DL_ERROR,
  14434. "%s: request_active_pip_prot failed\n",
  14435. __func__);
  14436. *irq_toggled = 0;
  14437. goto exit;
  14438. }
  14439. }
  14440. if (cd->irq_count > 0) {
  14441. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14442. cd->irq_count);
  14443. *bus_toggled = 1;
  14444. *irq_toggled = 1;
  14445. goto exit;
  14446. }
  14447. exit:
  14448. kfree(read_buf);
  14449. return rc;
  14450. }
  14451. /*******************************************************************************
  14452. * FUNCTION: pt_bist_xres_test
  14453. *
  14454. * SUMMARY: Tests the connectivity of the TP_XRES net
  14455. *
  14456. * This test will ensure there is a good connection between the host
  14457. * and the DUT on the tp_xres pin. The pin will be toggled to
  14458. * generate a TP reset which will cause the DUT to output a reset
  14459. * sentinel. If the reset sentinel is seen the test passes. If it is
  14460. * not seen the test will attempt to send a soft reset to simply gain
  14461. * some additional information on the failure:
  14462. * - soft reset fails to send: XRES and IRQ likely open
  14463. * - soft reset passes: XRES likely open or stuck de-asserted
  14464. * - soft reset fails: XRES likely stuck asserted
  14465. *
  14466. * RETURN:
  14467. * 0 = Success
  14468. * !0 = Failure
  14469. *
  14470. * PARAMETERS:
  14471. * *dev - pointer to device structure
  14472. * *bus_toggled - pointer to where to store if bus toggled
  14473. * *irq_toggled - pointer to where to store if IRQ toggled
  14474. * *xres_toggled - pointer to where to store if XRES toggled
  14475. * *err_str - pointer to error string buffer
  14476. ******************************************************************************/
  14477. static int pt_bist_xres_test(struct device *dev,
  14478. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14479. {
  14480. struct pt_core_data *cd = dev_get_drvdata(dev);
  14481. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14482. u8 *read_buf = NULL;
  14483. u8 mode = PT_MODE_UNKNOWN;
  14484. int rc = 0;
  14485. int t = 0;
  14486. int timeout = 300;
  14487. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14488. if (read_buf == NULL) {
  14489. rc = -ENOMEM;
  14490. goto exit;
  14491. }
  14492. /* Clear the startup bit mask, reset and enum will re-populate it */
  14493. cd->startup_status = STARTUP_STATUS_START;
  14494. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14495. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14496. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14497. rc = -ENODEV;
  14498. goto exit;
  14499. }
  14500. /* Ensure we have nothing pending on active bus */
  14501. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14502. /* Perform a hard XRES toggle and wait for reset sentinel */
  14503. mutex_lock(&cd->system_lock);
  14504. cd->hid_reset_cmd_state = 1;
  14505. mutex_unlock(&cd->system_lock);
  14506. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14507. rc = pt_hw_hard_reset(cd);
  14508. /* Set timeout to 1s for the flashless case where a BL could be done */
  14509. if (cd->flashless_dut)
  14510. timeout = 1000;
  14511. /*
  14512. * To avoid the case that next PIP command can be confused by BL/FW
  14513. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14514. * followed by "wakeup event" function can lower the failure rate.
  14515. */
  14516. t = wait_event_timeout(cd->wait_q,
  14517. ((cd->startup_status != STARTUP_STATUS_START)
  14518. && (cd->hid_reset_cmd_state == 0)),
  14519. msecs_to_jiffies(timeout));
  14520. if (IS_TMO(t)) {
  14521. pt_debug(cd->dev, DL_ERROR,
  14522. "%s: TMO waiting for sentinel\n", __func__);
  14523. *xres_toggled = 0;
  14524. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14525. /*
  14526. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14527. * To enter the BL we need to generate an XRES so first try to
  14528. * launch the applicaiton
  14529. */
  14530. if (cd->mode == PT_MODE_BOOTLOADER)
  14531. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14532. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14533. __func__);
  14534. mode = PT_MODE_IGNORE;
  14535. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14536. if (rc) {
  14537. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14538. __func__);
  14539. *xres_toggled = 0;
  14540. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14541. goto exit;
  14542. } else {
  14543. /* Wait for the BL sentinel */
  14544. t = wait_event_timeout(cd->wait_q,
  14545. (cd->startup_status != STARTUP_STATUS_START),
  14546. msecs_to_jiffies(500));
  14547. if (IS_TMO(t)) {
  14548. pt_debug(cd->dev, DL_ERROR,
  14549. "%s: TMO waiting for BL sentinel\n",
  14550. __func__);
  14551. *xres_toggled = 0;
  14552. strlcpy(err_str,
  14553. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14554. rc = -ETIME;
  14555. goto exit;
  14556. }
  14557. }
  14558. }
  14559. mutex_lock(&cd->system_lock);
  14560. cd->hid_reset_cmd_state = 0;
  14561. mutex_unlock(&cd->system_lock);
  14562. /* Look for BL or FW reset sentinels */
  14563. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14564. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14565. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14566. /* If a sentinel was seen, all nets are working */
  14567. *xres_toggled = 1;
  14568. *irq_toggled = 1;
  14569. /*
  14570. * For SPI test, bus read result can be confused as FW sentinel
  14571. * if MISO(slave) is connected to MISO(host).
  14572. */
  14573. if (cd->bus_ops->bustype == BUS_I2C)
  14574. *bus_toggled = 1;
  14575. } else {
  14576. /*
  14577. * Hard reset failed, however some additional information
  14578. * could be determined. Try a soft reset to see if DUT resets
  14579. * with the possible outcomes:
  14580. * - if it resets the line is not stuck asserted
  14581. * - if it does not reset the line could be stuck asserted
  14582. */
  14583. *xres_toggled = 0;
  14584. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14585. msleep(30);
  14586. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14587. __func__, rc);
  14588. if (rc) {
  14589. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14590. pt_debug(dev, DL_ERROR,
  14591. "%s: Hard reset failed, soft reset failed %s\n",
  14592. __func__, err_str);
  14593. goto exit;
  14594. }
  14595. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14596. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14597. strlcpy(err_str,
  14598. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14599. pt_debug(dev, DL_ERROR,
  14600. "%s: Hard reset failed, soft reset passed-%s\n",
  14601. __func__, err_str);
  14602. } else if (cd->startup_status == 0) {
  14603. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14604. pt_debug(dev, DL_ERROR,
  14605. "%s: Hard reset failed, soft reset failed-%s\n",
  14606. __func__, err_str);
  14607. } else {
  14608. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14609. pt_debug(dev, DL_ERROR,
  14610. "%s: Hard and Soft reset failed - %s\n",
  14611. __func__, err_str);
  14612. }
  14613. }
  14614. exit:
  14615. kfree(read_buf);
  14616. return rc;
  14617. }
  14618. /*******************************************************************************
  14619. * FUNCTION: pt_bist_slave_irq_test
  14620. *
  14621. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14622. *
  14623. * This test will ensure there is a good IRQ connection between the master
  14624. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14625. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14626. * saw each slave trigger the IRQ with it's reset sentinel.
  14627. *
  14628. * RETURN:
  14629. * 0 = Success
  14630. * !0 = Failure
  14631. *
  14632. * PARAMETERS:
  14633. * *dev - pointer to device structure
  14634. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14635. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14636. * *err_str - pointer to error string buffer
  14637. * *slave_detect - pointer to slave detect buffer
  14638. * *boot_err - pointer to boot_err buffer
  14639. ******************************************************************************/
  14640. static int pt_bist_slave_irq_test(struct device *dev,
  14641. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14642. u8 *slave_detect, u8 *boot_err)
  14643. {
  14644. struct pt_core_data *cd = dev_get_drvdata(dev);
  14645. u8 mode = PT_MODE_UNKNOWN;
  14646. u8 status;
  14647. u8 boot;
  14648. u8 read_buf[12];
  14649. u8 detected = 0;
  14650. u8 last_err = -1;
  14651. u16 actual_read_len;
  14652. int result = 0;
  14653. int rc = 0;
  14654. /*
  14655. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14656. * detect bits. If the DUT was in FW, entering the BL will cause an
  14657. * XRES signal which will inadvertently test the XRES net as well
  14658. */
  14659. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14660. if (rc) {
  14661. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14662. __func__, rc);
  14663. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14664. goto exit;
  14665. }
  14666. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14667. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14668. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14669. &actual_read_len);
  14670. if (!rc) {
  14671. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14672. "PIP2 STATUS");
  14673. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14674. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14675. /* Slave detect is only valid if status ok and in boot exec */
  14676. if (status == PIP2_RSP_ERR_NONE &&
  14677. boot == PIP2_STATUS_BOOT_EXEC) {
  14678. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14679. SLAVE_DETECT_MASK;
  14680. } else {
  14681. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14682. rc = -EPERM;
  14683. }
  14684. } else {
  14685. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14686. __func__);
  14687. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14688. goto exit;
  14689. }
  14690. /*
  14691. * Retrieve boot error regardless of the state of the slave detect
  14692. * bit because the IRQ could have been stuck high or low.
  14693. */
  14694. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14695. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14696. read_buf, &actual_read_len);
  14697. if (!rc) {
  14698. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14699. "PIP2 GET_LAST_ERRNO");
  14700. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14701. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14702. if (last_err) {
  14703. pt_debug(cd->dev, DL_ERROR,
  14704. "%s: Master Boot Last Err = 0x%02X\n",
  14705. __func__, last_err);
  14706. }
  14707. } else {
  14708. pt_debug(cd->dev, DL_ERROR,
  14709. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14710. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14711. }
  14712. exit:
  14713. pt_debug(cd->dev, DL_INFO,
  14714. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14715. __func__, rc, detected, last_err);
  14716. if (err_str && last_err) {
  14717. if (detected)
  14718. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14719. "- Likely stuck low. Boot Error:",
  14720. last_err);
  14721. else
  14722. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14723. "- Likely stuck high. Boot Error:",
  14724. last_err);
  14725. }
  14726. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14727. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14728. last_err = PIP2_RSP_ERR_NONE;
  14729. if (slave_irq_toggled)
  14730. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14731. if (slave_bus_toggled) {
  14732. /* Leave as UNTEST if slave not detected */
  14733. if (detected)
  14734. *slave_bus_toggled = !last_err ? true : false;
  14735. }
  14736. if (slave_detect)
  14737. *slave_detect = detected;
  14738. if (boot_err)
  14739. *boot_err = last_err;
  14740. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14741. __func__,
  14742. "Detected", detected,
  14743. "slave_irq_toggled", *slave_irq_toggled,
  14744. "slave_bus_toggled", *slave_bus_toggled);
  14745. return rc;
  14746. }
  14747. /*******************************************************************************
  14748. * FUNCTION: pt_bist_slave_xres_test
  14749. *
  14750. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14751. *
  14752. * This test will ensure there is a good TP_XRES connection between the
  14753. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14754. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14755. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14756. * it's reset sentinel.
  14757. *
  14758. * RETURN:
  14759. * 0 = Success
  14760. * !0 = Failure
  14761. *
  14762. * PARAMETERS:
  14763. * *dev - pointer to device structure
  14764. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14765. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14766. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14767. * *err_str - pointer to error string buffer
  14768. ******************************************************************************/
  14769. static int pt_bist_slave_xres_test(struct device *dev,
  14770. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14771. u8 *err_str)
  14772. {
  14773. struct pt_core_data *cd = dev_get_drvdata(dev);
  14774. u8 slave_detect = 0;
  14775. u8 boot_err = 0;
  14776. int rc = 0;
  14777. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14778. mutex_lock(&cd->system_lock);
  14779. cd->hid_reset_cmd_state = 1;
  14780. mutex_unlock(&cd->system_lock);
  14781. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14782. pt_hw_hard_reset(cd);
  14783. msleep(100);
  14784. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14785. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14786. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14787. if (!rc && *slave_irq_toggled == false) {
  14788. /*
  14789. * If the slave IRQ did not toggle, either the slave_detect
  14790. * bit was not set or we had a boot error. If the slave
  14791. * detect was not set the slave did not reset causing a boot
  14792. * error.
  14793. */
  14794. if (!slave_detect)
  14795. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14796. else
  14797. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14798. "- likely open or an IRQ issue. Boot Error:",
  14799. boot_err);
  14800. }
  14801. if (slave_xres_toggled) {
  14802. if (!rc)
  14803. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14804. else
  14805. *slave_xres_toggled = false;
  14806. }
  14807. return rc;
  14808. }
  14809. /*******************************************************************************
  14810. * FUNCTION: pt_bist_slave_bus_test
  14811. *
  14812. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14813. *
  14814. * This test will ensure a good SPI bus connection between the
  14815. * master DUT and the slave DUT(s). This bus connection is ensured by
  14816. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14817. * then there is a master/slave communication issue. Opening file 0 on
  14818. * the master will open it on the slave as well if the slave was detected.
  14819. *
  14820. * RETURN:
  14821. * 0 = Success
  14822. * !0 = Failure
  14823. *
  14824. * PARAMETERS:
  14825. * *dev - pointer to device structure
  14826. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14827. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14828. * *err_str - pointer to error string buffer
  14829. ******************************************************************************/
  14830. static int pt_bist_slave_bus_test(struct device *dev,
  14831. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14832. {
  14833. struct pt_core_data *cd = dev_get_drvdata(dev);
  14834. u8 mode = PT_MODE_UNKNOWN;
  14835. u8 bus_toggled = false;
  14836. u8 file_handle;
  14837. int result = 0;
  14838. int rc = 0;
  14839. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14840. if (rc) {
  14841. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14842. __func__, rc);
  14843. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14844. goto exit;
  14845. }
  14846. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14847. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14848. if (file_handle != PIP2_RAM_FILE) {
  14849. rc = -ENOENT;
  14850. bus_toggled = false;
  14851. pt_debug(dev, DL_ERROR,
  14852. "%s Failed to open bin file\n", __func__);
  14853. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14854. goto exit;
  14855. } else {
  14856. bus_toggled = true;
  14857. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14858. pt_debug(dev, DL_ERROR,
  14859. "%s: File Close failed, file_handle=%d\n",
  14860. __func__, file_handle);
  14861. }
  14862. }
  14863. exit:
  14864. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14865. if (slave_irq_toggled)
  14866. *slave_irq_toggled = bus_toggled;
  14867. if (slave_bus_toggled)
  14868. *slave_bus_toggled = bus_toggled;
  14869. return rc;
  14870. }
  14871. /*******************************************************************************
  14872. * FUNCTION: pt_ttdl_bist_show
  14873. *
  14874. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14875. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14876. *
  14877. * NOTE: This function will reset the DUT and the startup_status bit
  14878. * mask. A pt_enum will be queued after completion.
  14879. *
  14880. * NOTE: The order of the net tests is done to optimize the time it takes
  14881. * to run. The first test is capable of verifying all nets, each subsequent
  14882. * test is only run if the previous was not able to see all nets toggle.
  14883. *
  14884. * RETURN:
  14885. * 0 = success
  14886. * !0 = failure
  14887. *
  14888. * PARAMETERS:
  14889. * *dev - pointer to device structure
  14890. * *attr - pointer to device attributes
  14891. * *buf - pointer to output buffer
  14892. ******************************************************************************/
  14893. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14894. struct device_attribute *attr, char *buf)
  14895. {
  14896. struct pt_core_data *cd = dev_get_drvdata(dev);
  14897. ssize_t ret;
  14898. char *bus_err_str = NULL;
  14899. char *irq_err_str = NULL;
  14900. char *xres_err_str = NULL;
  14901. char *slave_bus_err_str = NULL;
  14902. char *slave_irq_err_str = NULL;
  14903. char *slave_xres_err_str = NULL;
  14904. u8 tests;
  14905. int rc = 0;
  14906. int num_tests = 0;
  14907. int status = 1; /* 0 = Pass, !0 = fail */
  14908. u8 bus_toggled = 0x0F; /* default to untested */
  14909. u8 i2c_toggled = 0x0F; /* default to untested */
  14910. u8 spi_toggled = 0x0F; /* default to untested */
  14911. u8 irq_toggled = 0x0F; /* default to untested */
  14912. u8 xres_toggled = 0x0F; /* default to untested */
  14913. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14914. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14915. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14916. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14917. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14918. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14919. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14920. goto print_results;
  14921. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14922. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14923. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14924. if (cd->multi_chip) {
  14925. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14926. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14927. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14928. if (!slave_bus_err_str ||
  14929. !slave_irq_err_str ||
  14930. !slave_xres_err_str)
  14931. goto print_results;
  14932. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14933. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14934. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14935. }
  14936. /* Turn off the TTDL WD during the test */
  14937. pt_stop_wd_timer(cd);
  14938. /* Shorten default PIP cmd timeout while running BIST */
  14939. cd->pip_cmd_timeout = 200;
  14940. /* Count the number of tests to run */
  14941. tests = cd->ttdl_bist_select;
  14942. while (tests) {
  14943. num_tests += tests & 1;
  14944. tests >>= 1;
  14945. }
  14946. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14947. __func__, cd->ttdl_bist_select, num_tests);
  14948. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14949. if (cd->flashless_dut) {
  14950. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14951. __func__);
  14952. mutex_lock(&cd->system_lock);
  14953. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14954. mutex_unlock(&cd->system_lock);
  14955. }
  14956. /* --------------- TP_XRES BIST TEST --------------- */
  14957. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14958. pt_debug(dev, DL_INFO,
  14959. "%s: ----- Start TP_XRES BIST -----", __func__);
  14960. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14961. &xres_toggled, xres_err_str);
  14962. /* Done if the rest of all nets toggled */
  14963. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14964. goto host_nets_complete;
  14965. }
  14966. /* Flush bus in case a PIP response is waiting from previous test */
  14967. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14968. /* --------------- IRQ BIST TEST --------------- */
  14969. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14970. pt_debug(dev, DL_INFO,
  14971. "%s: ----- Start IRQ BIST -----", __func__);
  14972. bus_toggled = 0xFF;
  14973. irq_toggled = 0xFF;
  14974. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14975. &xres_toggled, irq_err_str);
  14976. /* If this net failed clear results from previous net */
  14977. if (irq_toggled != 1) {
  14978. xres_toggled = 0x0F;
  14979. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14980. }
  14981. if (bus_toggled == 1 && irq_toggled == 1)
  14982. goto host_nets_complete;
  14983. }
  14984. /* Flush bus in case a PIP response is waiting from previous test */
  14985. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14986. /* --------------- BUS BIST TEST --------------- */
  14987. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14988. pt_debug(dev, DL_INFO,
  14989. "%s: ----- Start BUS BIST -----", __func__);
  14990. bus_toggled = 0xFF;
  14991. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14992. /* If this net failed clear results from previous net */
  14993. if (bus_toggled == 0) {
  14994. irq_toggled = 0x0F;
  14995. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14996. }
  14997. }
  14998. host_nets_complete:
  14999. /* --------------- SLAVE XRES BIST TEST --------------- */
  15000. if (cd->multi_chip &&
  15001. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15002. pt_debug(dev, DL_INFO,
  15003. "%s: ----- Start Slave XRES BIST -----", __func__);
  15004. slave_xres_toggled = 0xFF;
  15005. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15006. &slave_bus_toggled, &slave_xres_toggled,
  15007. slave_xres_err_str);
  15008. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15009. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15010. goto print_results;
  15011. }
  15012. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15013. if (cd->multi_chip &&
  15014. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15015. pt_debug(dev, DL_INFO,
  15016. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15017. slave_irq_toggled = 0xFF;
  15018. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15019. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15020. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15021. __func__, slave_irq_toggled);
  15022. if (slave_irq_toggled == 1) {
  15023. slave_bus_toggled = 1;
  15024. goto print_results;
  15025. }
  15026. }
  15027. /* --------------- SLAVE BUS BIST TEST --------------- */
  15028. if (cd->multi_chip &&
  15029. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15030. pt_debug(dev, DL_INFO,
  15031. "%s: ----- Start Slave BUS BIST -----", __func__);
  15032. slave_bus_toggled = 0xFF;
  15033. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15034. &slave_bus_toggled, slave_bus_err_str);
  15035. }
  15036. print_results:
  15037. /* Restore PIP command timeout */
  15038. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15039. /*
  15040. * We're done! - Perform a hard XRES toggle, allowing BL
  15041. * to load FW if there is any in Flash
  15042. */
  15043. mutex_lock(&cd->system_lock);
  15044. cd->hid_reset_cmd_state = 0;
  15045. mutex_unlock(&cd->system_lock);
  15046. pt_debug(dev, DL_INFO,
  15047. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15048. if (cd->flashless_dut) {
  15049. /*
  15050. * For Flashless solution, FW update is triggered after BL is
  15051. * seen that several miliseconds delay is needed.
  15052. */
  15053. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15054. __func__);
  15055. mutex_lock(&cd->system_lock);
  15056. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15057. mutex_unlock(&cd->system_lock);
  15058. /* Reset DUT and wait 100ms to see if loader started */
  15059. pt_hw_hard_reset(cd);
  15060. msleep(100);
  15061. if (cd->fw_updating) {
  15062. pt_debug(dev, DL_INFO,
  15063. "%s: ----- BIST Wait FW Loading ----",
  15064. __func__);
  15065. rc = _pt_request_wait_for_enum_state(
  15066. dev, 4000, STARTUP_STATUS_COMPLETE);
  15067. }
  15068. } else {
  15069. if (cd->mode == PT_MODE_BOOTLOADER) {
  15070. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15071. if (rc) {
  15072. pt_debug(dev, DL_ERROR,
  15073. "%s Failed to launch app\n", __func__);
  15074. rc = pt_hw_hard_reset(cd);
  15075. }
  15076. }
  15077. /*
  15078. * If FW exists the BL may have just started or will start soon,
  15079. * so the FW sentinel may be on it's way but with no FW it will
  15080. * not arrive, wait for it before deciding if we need to queue
  15081. * an enum.
  15082. */
  15083. rc = _pt_request_wait_for_enum_state(
  15084. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15085. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15086. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15087. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15088. __func__);
  15089. pt_queue_enum(cd);
  15090. rc = _pt_request_wait_for_enum_state(
  15091. dev, 2000, STARTUP_STATUS_COMPLETE);
  15092. }
  15093. }
  15094. msleep(20);
  15095. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15096. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15097. pt_start_wd_timer(cd);
  15098. /* Canned print if any memory allocation issues */
  15099. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15100. ret = scnprintf(buf, strlen(buf),
  15101. "Status: %d\n"
  15102. "I2C (SDA,SCL): [UNTEST]\n"
  15103. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15104. "IRQ connection: [UNTEST]\n"
  15105. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15106. if (cd->multi_chip) {
  15107. ret += scnprintf(buf + ret, strlen(buf),
  15108. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15109. "I/P IRQ connection: [UNTEST]\n"
  15110. "I/P TP_XRES connection: [UNTEST]\n");
  15111. }
  15112. } else {
  15113. status = 0;
  15114. if (bus_toggled == 1)
  15115. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15116. if (irq_toggled == 1)
  15117. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15118. if (xres_toggled == 1)
  15119. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15120. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15121. status += bus_toggled;
  15122. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15123. status += irq_toggled;
  15124. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15125. status += xres_toggled;
  15126. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15127. __func__, status, bus_toggled, irq_toggled,
  15128. xres_toggled);
  15129. if (cd->multi_chip) {
  15130. if (slave_irq_toggled == 1)
  15131. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15132. if (slave_xres_toggled == 1)
  15133. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15134. if (slave_bus_toggled == 1)
  15135. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15136. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15137. status += slave_bus_toggled;
  15138. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15139. status += slave_irq_toggled;
  15140. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15141. status += slave_xres_toggled;
  15142. pt_debug(dev, DL_ERROR,
  15143. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15144. __func__, status, bus_toggled, irq_toggled,
  15145. xres_toggled, slave_bus_toggled,
  15146. slave_irq_toggled, slave_xres_toggled);
  15147. }
  15148. if (cd->bus_ops->bustype == BUS_I2C)
  15149. i2c_toggled = bus_toggled;
  15150. else
  15151. spi_toggled = bus_toggled;
  15152. ret = scnprintf(buf, strlen(buf),
  15153. "Status: %d\n"
  15154. "I2C (SDA,SCL): %s %s\n"
  15155. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15156. "IRQ connection: %s %s\n"
  15157. "TP_XRES connection: %s %s\n",
  15158. status == num_tests ? 0 : 1,
  15159. i2c_toggled == 0x0F ? "[UNTEST]" :
  15160. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15161. i2c_toggled == 0x0F ? "" : bus_err_str,
  15162. spi_toggled == 0x0F ? "[UNTEST]" :
  15163. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15164. spi_toggled == 0x0F ? "" : bus_err_str,
  15165. irq_toggled == 0x0F ? "[UNTEST]" :
  15166. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15167. irq_err_str,
  15168. xres_toggled == 0x0F ? "[UNTEST]" :
  15169. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15170. xres_err_str);
  15171. if (cd->multi_chip) {
  15172. ret += scnprintf(buf + ret, strlen(buf),
  15173. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15174. "I/P IRQ connection: %s %s\n"
  15175. "I/P TP_XRES connection: %s %s\n",
  15176. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15177. slave_bus_toggled == 1 ? "[ OK ]" :
  15178. "[FAILED]", slave_bus_err_str,
  15179. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15180. slave_irq_toggled == 1 ? "[ OK ]" :
  15181. "[FAILED]", slave_irq_err_str,
  15182. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15183. slave_xres_toggled == 1 ? "[ OK ]" :
  15184. "[FAILED]", slave_xres_err_str);
  15185. }
  15186. }
  15187. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15188. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15189. __func__, cd->startup_status);
  15190. kfree(bus_err_str);
  15191. kfree(irq_err_str);
  15192. kfree(xres_err_str);
  15193. return ret;
  15194. }
  15195. /*******************************************************************************
  15196. * FUNCTION: pt_ttdl_bist_store
  15197. *
  15198. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15199. *
  15200. * RETURN: Size of passed in buffer if successful
  15201. *
  15202. * PARAMETERS:
  15203. * *dev - pointer to device structure
  15204. * *attr - pointer to device attributes
  15205. * *buf - pointer to command buffer
  15206. * size - size of buf
  15207. ******************************************************************************/
  15208. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15209. struct device_attribute *attr, const char *buf, size_t size)
  15210. {
  15211. struct pt_core_data *cd = dev_get_drvdata(dev);
  15212. u32 input_data[2] = {0};
  15213. int length;
  15214. int rc = 0;
  15215. /* Maximum input of one value */
  15216. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15217. ARRAY_SIZE(input_data));
  15218. if (length != 1) {
  15219. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15220. __func__);
  15221. rc = -EINVAL;
  15222. goto exit;
  15223. } else {
  15224. mutex_lock(&cd->system_lock);
  15225. cd->ttdl_bist_select = input_data[0];
  15226. mutex_unlock(&cd->system_lock);
  15227. }
  15228. exit:
  15229. if (rc)
  15230. return rc;
  15231. return size;
  15232. }
  15233. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15234. pt_ttdl_bist_store);
  15235. /*******************************************************************************
  15236. * FUNCTION: pt_flush_bus_store
  15237. *
  15238. * SUMMARY: Store method for the flush_bus sysfs node.
  15239. *
  15240. * RETURN: Size of passed in buffer if successful
  15241. *
  15242. * PARAMETERS:
  15243. * *dev - pointer to device structure
  15244. * *attr - pointer to device attributes
  15245. * *buf - pointer to command buffer
  15246. * size - size of buf
  15247. ******************************************************************************/
  15248. static ssize_t pt_flush_bus_store(struct device *dev,
  15249. struct device_attribute *attr, const char *buf, size_t size)
  15250. {
  15251. struct pt_core_data *cd = dev_get_drvdata(dev);
  15252. u32 input_data[2] = {0};
  15253. int length;
  15254. int rc = 0;
  15255. /* Maximum input of one value */
  15256. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15257. ARRAY_SIZE(input_data));
  15258. if (length != 1) {
  15259. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15260. __func__);
  15261. rc = -EINVAL;
  15262. goto exit;
  15263. }
  15264. mutex_lock(&cd->system_lock);
  15265. if (input_data[0] == 0)
  15266. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15267. else if (input_data[0] == 1)
  15268. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15269. else
  15270. rc = -EINVAL;
  15271. mutex_unlock(&cd->system_lock);
  15272. exit:
  15273. if (rc)
  15274. return rc;
  15275. return size;
  15276. }
  15277. /*******************************************************************************
  15278. * FUNCTION: pt_flush_bus_show
  15279. *
  15280. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15281. * based on either the size of the first two bytes or a blind 256 bytes.
  15282. *
  15283. * RETURN:
  15284. * 0 = success
  15285. * !0 = failure
  15286. *
  15287. * PARAMETERS:
  15288. * *dev - pointer to device structure
  15289. * *attr - pointer to device attributes
  15290. * *buf - pointer to output buffer
  15291. ******************************************************************************/
  15292. static ssize_t pt_flush_bus_show(struct device *dev,
  15293. struct device_attribute *attr, char *buf)
  15294. {
  15295. ssize_t ret = 0;
  15296. ssize_t bytes = 0;
  15297. struct pt_core_data *cd = dev_get_drvdata(dev);
  15298. mutex_lock(&cd->system_lock);
  15299. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15300. ret = scnprintf(buf, strlen(buf),
  15301. "Status: 0\n"
  15302. "%s: %zd\n",
  15303. "Bytes flushed", bytes);
  15304. mutex_unlock(&cd->system_lock);
  15305. return ret;
  15306. }
  15307. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15308. pt_flush_bus_store);
  15309. /*******************************************************************************
  15310. * FUNCTION: pt_pip2_ping_test_store
  15311. *
  15312. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15313. *
  15314. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15315. * someone may want to test sending invalid packet lengths so any values
  15316. * up to 255 are allowed.
  15317. *
  15318. * RETURN: Size of passed in buffer if successful
  15319. *
  15320. * PARAMETERS:
  15321. * *dev - pointer to device structure
  15322. * *attr - pointer to device attributes
  15323. * *buf - pointer to command buffer
  15324. * size - size of buf
  15325. ******************************************************************************/
  15326. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15327. struct device_attribute *attr, const char *buf, size_t size)
  15328. {
  15329. struct pt_core_data *cd = dev_get_drvdata(dev);
  15330. u32 input_data[2];
  15331. int length;
  15332. int rc = 0;
  15333. /* Maximum input of one value */
  15334. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15335. ARRAY_SIZE(input_data));
  15336. if (length != 1) {
  15337. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15338. __func__);
  15339. rc = -EINVAL;
  15340. goto exit;
  15341. }
  15342. mutex_lock(&cd->system_lock);
  15343. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15344. cd->ping_test_size = input_data[0];
  15345. else
  15346. rc = -EINVAL;
  15347. mutex_unlock(&cd->system_lock);
  15348. exit:
  15349. if (rc)
  15350. return rc;
  15351. return size;
  15352. }
  15353. /*******************************************************************************
  15354. * FUNCTION: pt_ping_test_show
  15355. *
  15356. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15357. * PING command and ramps up the optional payload from 0 to
  15358. * ping_test_size.
  15359. * The max payload size is 247:
  15360. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15361. *
  15362. * RETURN:
  15363. * 0 = success
  15364. * !0 = failure
  15365. *
  15366. * PARAMETERS:
  15367. * *dev - pointer to device structure
  15368. * *attr - pointer to device attributes
  15369. * *buf - pointer to output buffer
  15370. ******************************************************************************/
  15371. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15372. struct device_attribute *attr, char *buf)
  15373. {
  15374. struct pt_core_data *cd = dev_get_drvdata(dev);
  15375. ssize_t ret;
  15376. int last_packet_size;
  15377. int rc = 0;
  15378. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15379. if (rc) {
  15380. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15381. return ret;
  15382. }
  15383. ret = scnprintf(buf, strlen(buf),
  15384. "Status: %d\n"
  15385. "PING payload test passed with packet sizes 0 - %d\n",
  15386. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15387. last_packet_size);
  15388. return ret;
  15389. }
  15390. /*******************************************************************************
  15391. * FUNCTION: pt_t_refresh_store
  15392. *
  15393. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15394. * in integer as the number of interrupts to count. A timer is started to
  15395. * calculate the total time it takes to see that number of interrupts.
  15396. *
  15397. * RETURN: Size of passed in buffer if successful
  15398. *
  15399. * PARAMETERS:
  15400. * *dev - pointer to device structure
  15401. * *attr - pointer to device attributes
  15402. * *buf - pointer to buffer that hold the command parameters
  15403. * size - size of buf
  15404. ******************************************************************************/
  15405. static ssize_t pt_t_refresh_store(struct device *dev,
  15406. struct device_attribute *attr, const char *buf, size_t size)
  15407. {
  15408. struct pt_core_data *cd = dev_get_drvdata(dev);
  15409. u32 input_data[2];
  15410. int length;
  15411. int rc = 0;
  15412. /* Maximum input of one value */
  15413. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15414. ARRAY_SIZE(input_data));
  15415. if (length != 1) {
  15416. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15417. __func__);
  15418. rc = -EINVAL;
  15419. goto exit;
  15420. }
  15421. mutex_lock(&cd->system_lock);
  15422. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15423. input_data[0]);
  15424. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15425. cd->t_refresh_total = input_data[0];
  15426. cd->t_refresh_count = 0;
  15427. cd->t_refresh_active = 1;
  15428. } else {
  15429. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15430. rc = -EINVAL;
  15431. }
  15432. mutex_unlock(&cd->system_lock);
  15433. exit:
  15434. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15435. if (rc)
  15436. return rc;
  15437. return size;
  15438. }
  15439. /*******************************************************************************
  15440. * FUNCTION: pt_t_refresh_show
  15441. *
  15442. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15443. * of the T-Refresh timer counting the time it takes to see a user defined
  15444. * number of interrupts.
  15445. *
  15446. * RETURN:
  15447. * 0 = success
  15448. * !0 = failure
  15449. *
  15450. * PARAMETERS:
  15451. * *dev - pointer to device structure
  15452. * *attr - pointer to device attributes
  15453. * *buf - pointer to output buffer
  15454. ******************************************************************************/
  15455. static ssize_t pt_t_refresh_show(struct device *dev,
  15456. struct device_attribute *attr, char *buf)
  15457. {
  15458. ssize_t ret = 0;
  15459. struct pt_core_data *cd = dev_get_drvdata(dev);
  15460. u32 whole;
  15461. u16 fraction;
  15462. mutex_lock(&cd->system_lock);
  15463. /* Check if we have counted the number requested */
  15464. if (cd->t_refresh_count != cd->t_refresh_total) {
  15465. ret = scnprintf(buf, strlen(buf),
  15466. "Status: 0\n"
  15467. "%s: %d\n",
  15468. "Still counting... current IRQ count",
  15469. cd->t_refresh_count);
  15470. } else {
  15471. /* Ensure T-Refresh is de-activated */
  15472. cd->t_refresh_active = 0;
  15473. whole = cd->t_refresh_time / cd->t_refresh_count;
  15474. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15475. fraction = fraction * 1000 / cd->t_refresh_count;
  15476. ret = scnprintf(buf, strlen(buf),
  15477. "Status: 0\n"
  15478. "%s: %d\n"
  15479. "%s: %d\n"
  15480. "%s: %d\n"
  15481. "%s: %d.%02d\n",
  15482. "Requested IRQ Count ", cd->t_refresh_total,
  15483. "IRQ Counted ", cd->t_refresh_count,
  15484. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15485. "Average T-Refresh (ms) ", whole, fraction);
  15486. }
  15487. mutex_unlock(&cd->system_lock);
  15488. return ret;
  15489. }
  15490. /*******************************************************************************
  15491. * FUNCTION: pt_dut_status_show
  15492. *
  15493. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15494. * more items such as operation mode,easywake state are added in the future.
  15495. *
  15496. * RETURN: Char buffer with printed scan status information
  15497. *
  15498. * PARAMETERS:
  15499. * *dev - pointer to device structure
  15500. * *attr - pointer to device attributes
  15501. * *buf - pointer to output buffer
  15502. ******************************************************************************/
  15503. static ssize_t pt_dut_status_show(struct device *dev,
  15504. struct device_attribute *attr, char *buf)
  15505. {
  15506. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15507. u8 mode = PT_MODE_UNKNOWN;
  15508. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15509. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15510. struct pt_core_data *cd = dev_get_drvdata(dev);
  15511. ssize_t ret;
  15512. u16 calculated_crc = 0;
  15513. u16 stored_crc = 0;
  15514. u8 status;
  15515. int rc = 0;
  15516. /* In STANDBY the DUT will not repond to any PIP cmd */
  15517. if (cd->fw_sys_mode_in_standby_state) {
  15518. mode = PT_MODE_OPERATIONAL;
  15519. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15520. goto print_limited_results;
  15521. }
  15522. /* Retrieve mode and FW system mode which can only be 0-4 */
  15523. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15524. if (rc || mode == PT_MODE_UNKNOWN) {
  15525. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15526. "%s: %d\n"
  15527. "%s: n/a\n"
  15528. "%s: n/a\n"
  15529. "%s: n/a\n"
  15530. "%s: n/a\n",
  15531. "Status", rc,
  15532. "Active Exec ",
  15533. "FW System Mode ",
  15534. "Stored CRC ",
  15535. "Calculated CRC ");
  15536. return ret;
  15537. } else {
  15538. if (mode == PT_MODE_OPERATIONAL) {
  15539. if (sys_mode > FW_SYS_MODE_MAX)
  15540. sys_mode = FW_SYS_MODE_UNDEFINED;
  15541. if (sys_mode != FW_SYS_MODE_TEST)
  15542. goto print_limited_results;
  15543. rc = pt_pip_verify_config_block_crc_(cd,
  15544. PT_TCH_PARM_EBID, &status,
  15545. &calculated_crc, &stored_crc);
  15546. if (rc)
  15547. goto print_limited_results;
  15548. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15549. "%s: %d\n"
  15550. "%s: %s\n"
  15551. "%s: %s\n"
  15552. "%s: 0x%04X\n"
  15553. "%s: 0x%04X\n",
  15554. "Status", rc,
  15555. "Active Exec ", "FW",
  15556. "FW System Mode ", outputstring[sys_mode],
  15557. "Stored CRC ", stored_crc,
  15558. "Calculated CRC ", calculated_crc);
  15559. return ret;
  15560. } else {
  15561. /* When in BL or unknon mode Active Exec is "n/a" */
  15562. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15563. }
  15564. }
  15565. print_limited_results:
  15566. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15567. "%s: %d\n"
  15568. "%s: %s\n"
  15569. "%s: %s\n"
  15570. "%s: n/a\n"
  15571. "%s: n/a\n",
  15572. "Status", rc,
  15573. "Active Exec ",
  15574. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15575. "FW System Mode ", outputstring[sys_mode],
  15576. "Stored CRC ",
  15577. "Calculated CRC ");
  15578. return ret;
  15579. }
  15580. #endif /* TTDL_DIAGNOSTICS */
  15581. /*******************************************************************************
  15582. * Structures of sysfs attributes for all DUT dependent sysfs node
  15583. ******************************************************************************/
  15584. static struct attribute *early_attrs[] = {
  15585. &dev_attr_hw_version.attr,
  15586. &dev_attr_drv_version.attr,
  15587. &dev_attr_drv_ver.attr,
  15588. &dev_attr_fw_version.attr,
  15589. &dev_attr_sysinfo.attr,
  15590. &dev_attr_pip2_cmd_rsp.attr,
  15591. &dev_attr_command.attr,
  15592. &dev_attr_drv_debug.attr,
  15593. &dev_attr_hw_reset.attr,
  15594. &dev_attr_response.attr,
  15595. &dev_attr_ttdl_restart.attr,
  15596. #ifdef TTDL_DIAGNOSTICS
  15597. &dev_attr_ttdl_status.attr,
  15598. &dev_attr_pip2_enter_bl.attr,
  15599. &dev_attr_pip2_exit_bl.attr,
  15600. &dev_attr_err_gpio.attr,
  15601. &dev_attr_flush_bus.attr,
  15602. &dev_attr_ttdl_bist.attr,
  15603. #endif /* TTDL_DIAGNOSTICS */
  15604. NULL,
  15605. };
  15606. static struct attribute_group early_attr_group = {
  15607. .attrs = early_attrs,
  15608. };
  15609. static struct device_attribute pip2_attributes[] = {
  15610. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15611. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15612. #ifdef TTDL_DIAGNOSTICS
  15613. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15614. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15615. pt_pip2_ping_test_store),
  15616. #endif
  15617. };
  15618. static struct device_attribute attributes[] = {
  15619. __ATTR(dut_debug, 0644,
  15620. pt_dut_debug_show, pt_drv_debug_store),
  15621. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15622. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15623. __ATTR(get_param, 0644,
  15624. pt_get_param_show, pt_get_param_store),
  15625. #ifdef EASYWAKE_TSG6
  15626. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15627. pt_easy_wakeup_gesture_store),
  15628. __ATTR(easy_wakeup_gesture_id, 0444,
  15629. pt_easy_wakeup_gesture_id_show, NULL),
  15630. __ATTR(easy_wakeup_gesture_data, 0444,
  15631. pt_easy_wakeup_gesture_data_show, NULL),
  15632. #endif
  15633. #ifdef TTDL_DIAGNOSTICS
  15634. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15635. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15636. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15637. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15638. #endif /* TTDL_DIAGNOSTICS */
  15639. };
  15640. /*******************************************************************************
  15641. * FUNCTION: add_sysfs_interfaces
  15642. *
  15643. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15644. *
  15645. * RETURN:
  15646. * 0 = success
  15647. * !0 = failure
  15648. *
  15649. * PARAMETERS:
  15650. * *dev - pointer to device structure
  15651. ******************************************************************************/
  15652. static int add_sysfs_interfaces(struct device *dev)
  15653. {
  15654. int i;
  15655. int j = 0;
  15656. struct pt_core_data *cd = dev_get_drvdata(dev);
  15657. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15658. if (device_create_file(dev, attributes + i))
  15659. goto undo;
  15660. }
  15661. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15662. __func__, cd->active_dut_generation);
  15663. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15664. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15665. if (device_create_file(dev, pip2_attributes + j))
  15666. goto undo;
  15667. }
  15668. }
  15669. return 0;
  15670. undo:
  15671. for (i--; i >= 0; i--)
  15672. device_remove_file(dev, attributes + i);
  15673. for (j--; j >= 0; j--)
  15674. device_remove_file(dev, pip2_attributes + j);
  15675. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15676. __func__);
  15677. return -ENODEV;
  15678. }
  15679. /*******************************************************************************
  15680. * FUNCTION: remove_sysfs_interfaces
  15681. *
  15682. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15683. *
  15684. * RETURN: void
  15685. *
  15686. * PARAMETERS:
  15687. * *dev - pointer to device structure
  15688. ******************************************************************************/
  15689. static void remove_sysfs_interfaces(struct device *dev)
  15690. {
  15691. int i;
  15692. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15693. device_remove_file(dev, attributes + i);
  15694. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15695. device_remove_file(dev, pip2_attributes + i);
  15696. }
  15697. /*******************************************************************************
  15698. * FUNCTION: remove_sysfs_and_modules
  15699. *
  15700. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15701. *
  15702. * RETURN: void
  15703. *
  15704. * PARAMETERS:
  15705. * *dev - pointer to device structure
  15706. ******************************************************************************/
  15707. static void remove_sysfs_and_modules(struct device *dev)
  15708. {
  15709. struct pt_core_data *cd = dev_get_drvdata(dev);
  15710. /* Queued work should be removed before to release loader module */
  15711. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15712. pt_release_modules(cd);
  15713. pt_btn_release(dev);
  15714. pt_mt_release(dev);
  15715. remove_sysfs_interfaces(dev);
  15716. }
  15717. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15718. {
  15719. int retval;
  15720. /* Get pinctrl if target uses pinctrl */
  15721. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15722. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15723. retval = PTR_ERR(cd->ts_pinctrl);
  15724. dev_dbg(cd->dev,
  15725. "Target does not use pinctrl %d\n", retval);
  15726. goto err_pinctrl_get;
  15727. }
  15728. cd->pinctrl_state_active
  15729. = pinctrl_lookup_state(cd->ts_pinctrl,
  15730. PINCTRL_STATE_ACTIVE);
  15731. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15732. retval = PTR_ERR(cd->pinctrl_state_active);
  15733. dev_err(cd->dev,
  15734. "Can not lookup %s pinstate %d\n",
  15735. PINCTRL_STATE_ACTIVE, retval);
  15736. goto err_pinctrl_lookup;
  15737. }
  15738. cd->pinctrl_state_suspend
  15739. = pinctrl_lookup_state(cd->ts_pinctrl,
  15740. PINCTRL_STATE_SUSPEND);
  15741. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15742. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15743. dev_err(cd->dev,
  15744. "Can not lookup %s pinstate %d\n",
  15745. PINCTRL_STATE_SUSPEND, retval);
  15746. goto err_pinctrl_lookup;
  15747. }
  15748. cd->pinctrl_state_release
  15749. = pinctrl_lookup_state(cd->ts_pinctrl,
  15750. PINCTRL_STATE_RELEASE);
  15751. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15752. retval = PTR_ERR(cd->pinctrl_state_release);
  15753. dev_dbg(cd->dev,
  15754. "Can not lookup %s pinstate %d\n",
  15755. PINCTRL_STATE_RELEASE, retval);
  15756. }
  15757. return 0;
  15758. err_pinctrl_lookup:
  15759. devm_pinctrl_put(cd->ts_pinctrl);
  15760. err_pinctrl_get:
  15761. cd->ts_pinctrl = NULL;
  15762. return retval;
  15763. }
  15764. /*******************************************************************************
  15765. *******************************************************************************
  15766. * FUNCTION: pt_probe
  15767. *
  15768. * SUMMARY: Probe of the core module.
  15769. *
  15770. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15771. * probe functionality is split into two functions; pt_probe() and
  15772. * pt_probe_complete(). the initial setup is done in this function which
  15773. * then creates a WORK task which runs after the probe timer expires. This
  15774. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15775. *
  15776. * RETURN:
  15777. * 0 = success
  15778. * !0 = failure
  15779. *
  15780. * PARAMETERS:
  15781. * *ops - pointer to the bus
  15782. * *dev - pointer to the device structure
  15783. * irq - IRQ
  15784. * xfer_buf_size - size of the buffer
  15785. ******************************************************************************/
  15786. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15787. u16 irq, size_t xfer_buf_size)
  15788. {
  15789. struct pt_core_data *cd;
  15790. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15791. enum pt_atten_type type;
  15792. struct i2c_client *client = to_i2c_client(dev);
  15793. int rc = 0;
  15794. u8 pip_ver_major;
  15795. u8 pip_ver_minor;
  15796. u32 status = STARTUP_STATUS_START;
  15797. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15798. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15799. __func__);
  15800. rc = -ENODEV;
  15801. goto error_no_pdata;
  15802. }
  15803. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15804. if (!pdata->core_pdata->power) {
  15805. pt_debug(dev, DL_ERROR,
  15806. "%s: Missing platform data function\n",
  15807. __func__);
  15808. rc = -ENODEV;
  15809. goto error_no_pdata;
  15810. }
  15811. }
  15812. /* get context and debug print buffers */
  15813. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15814. if (!cd) {
  15815. rc = -ENOMEM;
  15816. goto error_no_pdata;
  15817. }
  15818. /* Initialize device info */
  15819. cd->dev = dev;
  15820. cd->pdata = pdata;
  15821. cd->cpdata = pdata->core_pdata;
  15822. cd->bus_ops = ops;
  15823. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15824. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15825. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15826. cd->hw_detected = false;
  15827. cd->pip2_prot_active = false;
  15828. cd->pip2_send_user_cmd = false;
  15829. cd->bl_pip_ver_ready = false;
  15830. cd->app_pip_ver_ready = false;
  15831. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15832. cd->get_param_id = 0;
  15833. cd->watchdog_enabled = 0;
  15834. cd->startup_retry_count = 0;
  15835. cd->core_probe_complete = 0;
  15836. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15837. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15838. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15839. cd->flashless_dut = 0;
  15840. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15841. cd->bl_with_no_int = 0;
  15842. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15843. cd->multi_chip = PT_FEATURE_DISABLE;
  15844. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15845. cd->sleep_state = SS_SLEEP_NONE;
  15846. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15847. cd->set_dut_generation = true;
  15848. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15849. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15850. cd->set_dut_generation = true;
  15851. cd->active_dut_generation = DUT_PIP1_ONLY;
  15852. } else {
  15853. cd->set_dut_generation = false;
  15854. cd->active_dut_generation = DUT_UNKNOWN;
  15855. }
  15856. /* Initialize with platform data */
  15857. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15858. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15859. cd->hid_cmd_state = 1;
  15860. cd->fw_updating = false;
  15861. cd->multi_chip = 0;
  15862. #ifdef TTDL_DIAGNOSTICS
  15863. cd->t_refresh_active = 0;
  15864. cd->t_refresh_count = 0;
  15865. cd->pip2_crc_error_count = 0;
  15866. cd->wd_xres_count = 0;
  15867. cd->bl_retry_packet_count = 0;
  15868. cd->file_erase_timeout_count = 0;
  15869. cd->show_tt_data = false;
  15870. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15871. cd->err_gpio = 0;
  15872. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15873. cd->ttdl_bist_select = 0x07;
  15874. cd->force_pip2_seq = 0;
  15875. #endif /* TTDL_DIAGNOSTICS */
  15876. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15877. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15878. sizeof(PT_PIP2_BIN_FILE_PATH));
  15879. pt_init_hid_descriptor(&cd->hid_desc);
  15880. /* Read and store the descriptor lengths */
  15881. cd->hid_core.hid_report_desc_len =
  15882. le16_to_cpu(cd->hid_desc.report_desc_len);
  15883. cd->hid_core.hid_max_input_len =
  15884. le16_to_cpu(cd->hid_desc.max_input_len);
  15885. cd->hid_core.hid_max_output_len =
  15886. le16_to_cpu(cd->hid_desc.max_output_len);
  15887. /* Initialize mutexes and spinlocks */
  15888. mutex_init(&cd->module_list_lock);
  15889. mutex_init(&cd->system_lock);
  15890. mutex_init(&cd->sysfs_lock);
  15891. mutex_init(&cd->ttdl_restart_lock);
  15892. mutex_init(&cd->firmware_class_lock);
  15893. spin_lock_init(&cd->spinlock);
  15894. /* Initialize module list */
  15895. INIT_LIST_HEAD(&cd->module_list);
  15896. /* Initialize attention lists */
  15897. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15898. INIT_LIST_HEAD(&cd->atten_list[type]);
  15899. /* Initialize parameter list */
  15900. INIT_LIST_HEAD(&cd->param_list);
  15901. /* Initialize wait queue */
  15902. init_waitqueue_head(&cd->wait_q);
  15903. rc = pt_ts_pinctrl_init(cd);
  15904. if (!rc && cd->ts_pinctrl) {
  15905. /*
  15906. * Pinctrl handle is optional. If pinctrl handle is found
  15907. * let pins to be configured in active state. If not
  15908. * found continue further without error.
  15909. */
  15910. rc = pinctrl_select_state(cd->ts_pinctrl,
  15911. cd->pinctrl_state_active);
  15912. if (rc < 0)
  15913. dev_err(&client->dev, "failed to select pin to active state\n");
  15914. }
  15915. rc = pt_get_regulator(cd, true);
  15916. if (rc) {
  15917. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15918. goto error_alloc_data;
  15919. }
  15920. rc = pt_enable_regulator(cd, true);
  15921. if (rc) {
  15922. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15923. goto error_get_regulator;
  15924. }
  15925. /* Initialize works */
  15926. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15927. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15928. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15929. /* Initialize HID specific data */
  15930. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15931. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15932. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15933. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15934. cd->hid_core.hid_desc_register =
  15935. cpu_to_le16(cd->cpdata->hid_desc_register);
  15936. /* Set platform easywake value */
  15937. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15938. #ifdef CONFIG_DRM
  15939. /* Setup active dsi panel */
  15940. active_panel = cd->cpdata->active_panel;
  15941. #endif
  15942. /* Set platform panel_id value */
  15943. cd->panel_id_support = cd->cpdata->panel_id_support;
  15944. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15945. /* Set Panel ID to default to 0 */
  15946. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15947. else
  15948. /* Set Panel ID to Not Enabled */
  15949. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15950. /* Initialize hw_version default to FFFF.FFFF.FF */
  15951. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15952. dev_set_drvdata(dev, cd);
  15953. /* PtSBC builds will call this function in pt_probe_complete() */
  15954. pt_add_core(dev);
  15955. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15956. if (rc) {
  15957. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15958. __func__);
  15959. goto error_enable_regulator;
  15960. }
  15961. /*
  15962. * Save the pointer to a global value, which will be used
  15963. * in ttdl_restart function
  15964. */
  15965. cd->bus_ops = ops;
  15966. /*
  15967. * When the IRQ GPIO is not direclty accessible and no function is
  15968. * defined to get the IRQ status, the IRQ passed in must be assigned
  15969. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15970. */
  15971. if (!cd->cpdata->irq_stat) {
  15972. cd->irq = irq;
  15973. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15974. __func__, cd->irq);
  15975. }
  15976. /* Call platform init function before setting up the GPIO's */
  15977. if (cd->cpdata->init) {
  15978. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15979. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15980. } else {
  15981. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15982. __func__);
  15983. rc = 0;
  15984. }
  15985. if (rc < 0) {
  15986. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15987. __func__, rc);
  15988. }
  15989. /* Power on any needed regulator(s) */
  15990. if (cd->cpdata->setup_power) {
  15991. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15992. rc = cd->cpdata->setup_power(cd->cpdata,
  15993. PT_MT_POWER_ON, cd->dev);
  15994. } else {
  15995. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15996. __func__);
  15997. rc = 0;
  15998. }
  15999. if (rc < 0)
  16000. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16001. __func__, rc);
  16002. #ifdef TTDL_DIAGNOSTICS
  16003. cd->watchdog_irq_stuck_count = 0;
  16004. cd->bus_transmit_error_count = 0;
  16005. #endif /* TTDL_DIAGNOSTICS */
  16006. if (cd->cpdata->detect) {
  16007. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16008. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16009. pt_platform_detect_read);
  16010. if (!rc) {
  16011. cd->hw_detected = true;
  16012. pt_debug(cd->dev, DL_INFO,
  16013. "%s: HW detected\n", __func__);
  16014. } else {
  16015. cd->hw_detected = false;
  16016. pt_debug(cd->dev, DL_INFO,
  16017. "%s: No HW detected\n", __func__);
  16018. rc = -ENODEV;
  16019. goto error_detect;
  16020. }
  16021. } else {
  16022. pt_debug(dev, DL_ERROR,
  16023. "%s: PARADE No HW detect function pointer\n",
  16024. __func__);
  16025. /*
  16026. * "hw_reset" is not needed in the "if" statement,
  16027. * because "hw_reset" is already included in "hw_detect"
  16028. * function.
  16029. */
  16030. rc = pt_hw_hard_reset(cd);
  16031. if (rc)
  16032. pt_debug(cd->dev, DL_ERROR,
  16033. "%s: FAILED to execute HARD reset\n",
  16034. __func__);
  16035. }
  16036. if (cd->cpdata->setup_irq) {
  16037. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16038. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16039. if (rc) {
  16040. pt_debug(dev, DL_ERROR,
  16041. "%s: Error, couldn't setup IRQ\n", __func__);
  16042. goto error_setup_irq;
  16043. }
  16044. } else {
  16045. pt_debug(dev, DL_ERROR,
  16046. "%s: IRQ function pointer not setup\n",
  16047. __func__);
  16048. goto error_setup_irq;
  16049. }
  16050. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16051. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16052. (unsigned long)cd);
  16053. #else
  16054. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16055. #endif
  16056. pt_stop_wd_timer(cd);
  16057. #ifdef TTHE_TUNER_SUPPORT
  16058. mutex_init(&cd->tthe_lock);
  16059. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16060. 0644, NULL, cd, &tthe_debugfs_fops);
  16061. #endif
  16062. rc = device_init_wakeup(dev, 1);
  16063. if (rc < 0)
  16064. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16065. __func__, rc);
  16066. pm_runtime_get_noresume(dev);
  16067. pm_runtime_set_active(dev);
  16068. pm_runtime_enable(dev);
  16069. /* If IRQ asserted, read out all from buffer to release INT pin */
  16070. if (cd->cpdata->irq_stat) {
  16071. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16072. } else {
  16073. /* Force a read in case the reset sentinel already arrived */
  16074. rc = pt_read_input(cd);
  16075. if (!rc)
  16076. pt_parse_input(cd);
  16077. }
  16078. /* Without sleep DUT is not ready and will NAK the first write */
  16079. msleep(150);
  16080. /* Attempt to set the DUT generation if not yet set */
  16081. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16082. if (cd->bl_pip_ver_ready ||
  16083. (cd->app_pip_ver_ready &&
  16084. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16085. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16086. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16087. __func__, cd->active_dut_generation);
  16088. } else {
  16089. rc = _pt_detect_dut_generation(cd->dev,
  16090. &status, &cd->active_dut_generation,
  16091. &cd->mode);
  16092. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16093. || rc) {
  16094. pt_debug(cd->dev, DL_ERROR,
  16095. " === DUT Gen unknown, Skip Enum ===\n");
  16096. goto skip_enum;
  16097. }
  16098. }
  16099. }
  16100. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16101. &pip_ver_major, &pip_ver_minor);
  16102. if (pip_ver_major == 2) {
  16103. cd->bl_pip_ver_ready = true;
  16104. pt_debug(dev, DL_ERROR,
  16105. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16106. pip_ver_minor);
  16107. cd->hw_detected = true;
  16108. } else if (pip_ver_major == 1) {
  16109. cd->app_pip_ver_ready = true;
  16110. pt_debug(dev, DL_ERROR,
  16111. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16112. cd->hw_detected = true;
  16113. } else {
  16114. cd->sysinfo.ttdata.pip_ver_major = 0;
  16115. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16116. cd->app_pip_ver_ready = false;
  16117. cd->hw_detected = false;
  16118. pt_debug(dev, DL_ERROR,
  16119. " === PIP Version Not Detected, Skip Enum ===\n");
  16120. /* For legacy DUTS proceed, enum will attempt to launch app */
  16121. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16122. goto skip_enum;
  16123. }
  16124. rc = pt_enum_with_dut(cd, false, &status);
  16125. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16126. __func__, cd->startup_status, status);
  16127. if (rc == -ENODEV) {
  16128. pt_debug(cd->dev, DL_ERROR,
  16129. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16130. /* For PtSBC don't error out, allow TTDL to stay up */
  16131. goto error_after_startup;
  16132. }
  16133. /* Suspend scanning until probe is complete to avoid asyc touches */
  16134. pt_pip_suspend_scanning_(cd);
  16135. if (cd->hw_detected) {
  16136. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16137. __func__);
  16138. rc = add_sysfs_interfaces(dev);
  16139. if (rc < 0) {
  16140. pt_debug(dev, DL_ERROR,
  16141. "%s: Error, fail sysfs init\n", __func__);
  16142. goto error_after_startup;
  16143. }
  16144. } else {
  16145. pt_debug(dev, DL_ERROR,
  16146. "%s: No HW detected, sysfs interfaces not added\n",
  16147. __func__);
  16148. }
  16149. skip_enum:
  16150. pm_runtime_put_sync(dev);
  16151. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16152. rc = pt_mt_probe(dev);
  16153. if (rc < 0) {
  16154. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16155. __func__);
  16156. goto error_after_sysfs_create;
  16157. }
  16158. rc = pt_btn_probe(dev);
  16159. if (rc < 0) {
  16160. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16161. __func__);
  16162. goto error_after_startup_mt;
  16163. }
  16164. pt_probe_modules(cd);
  16165. #ifdef CONFIG_HAS_EARLYSUSPEND
  16166. pt_setup_early_suspend(cd);
  16167. #elif defined(CONFIG_DRM)
  16168. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16169. pt_setup_drm_notifier(cd);
  16170. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16171. #elif defined(CONFIG_FB)
  16172. pt_setup_fb_notifier(cd);
  16173. #endif
  16174. #ifdef NEED_SUSPEND_NOTIFIER
  16175. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16176. register_pm_notifier(&cd->pm_notifier);
  16177. #endif
  16178. pt_pip_resume_scanning_(cd);
  16179. mutex_lock(&cd->system_lock);
  16180. cd->startup_status |= status;
  16181. cd->core_probe_complete = 1;
  16182. mutex_unlock(&cd->system_lock);
  16183. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16184. __func__);
  16185. return 0;
  16186. error_after_startup_mt:
  16187. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16188. pt_mt_release(dev);
  16189. error_after_sysfs_create:
  16190. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16191. pm_runtime_disable(dev);
  16192. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16193. device_wakeup_disable(dev);
  16194. #endif
  16195. device_init_wakeup(dev, 0);
  16196. cancel_work_sync(&cd->enum_work);
  16197. pt_stop_wd_timer(cd);
  16198. pt_free_si_ptrs(cd);
  16199. remove_sysfs_interfaces(dev);
  16200. error_after_startup:
  16201. pr_err("%s PARADE error_after_startup\n", __func__);
  16202. del_timer(&cd->watchdog_timer);
  16203. if (cd->cpdata->setup_irq)
  16204. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16205. error_setup_irq:
  16206. error_detect:
  16207. if (cd->cpdata->init)
  16208. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16209. if (cd->cpdata->setup_power)
  16210. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16211. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16212. error_enable_regulator:
  16213. pt_del_core(dev);
  16214. dev_set_drvdata(dev, NULL);
  16215. pt_enable_regulator(cd, false);
  16216. error_get_regulator:
  16217. pt_get_regulator(cd, false);
  16218. error_alloc_data:
  16219. kfree(cd);
  16220. error_no_pdata:
  16221. pr_err("%s failed.\n", __func__);
  16222. return rc;
  16223. }
  16224. EXPORT_SYMBOL_GPL(pt_probe);
  16225. /*******************************************************************************
  16226. * FUNCTION: pt_release
  16227. *
  16228. * SUMMARY: This function does the following cleanup:
  16229. * - Releases all probed modules
  16230. * - Stops the watchdog
  16231. * - Cancels all pending work tasks
  16232. * - Removes all created sysfs nodes
  16233. * - Removes all created debugfs nodes
  16234. * - Frees the IRQ
  16235. * - Deletes the core
  16236. * - Frees all pointers and HID reports
  16237. *
  16238. * RETURN:
  16239. * 0 = success
  16240. * !0 = failure
  16241. *
  16242. * PARAMETERS:
  16243. * *cd - pointer to the core data structure
  16244. ******************************************************************************/
  16245. int pt_release(struct pt_core_data *cd)
  16246. {
  16247. struct device *dev = cd->dev;
  16248. /*
  16249. * Suspend the device before freeing the startup_work and stopping
  16250. * the watchdog since sleep function restarts watchdog on failure
  16251. */
  16252. pm_runtime_suspend(dev);
  16253. pm_runtime_disable(dev);
  16254. /*
  16255. * Any 'work' that can trigger a new thread should be canceled first.
  16256. * The watchdog is also stopped again because another thread could have
  16257. * restarted it. The irq_work is cancelled last because it is used for
  16258. * all I2C/SPI communication.
  16259. */
  16260. pt_stop_wd_timer(cd);
  16261. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16262. cancel_work_sync(&cd->ttdl_restart_work);
  16263. cancel_work_sync(&cd->enum_work);
  16264. pt_stop_wd_timer(cd);
  16265. pt_release_modules(cd);
  16266. pt_proximity_release(dev);
  16267. pt_btn_release(dev);
  16268. pt_mt_release(dev);
  16269. #ifdef CONFIG_HAS_EARLYSUSPEND
  16270. unregister_early_suspend(&cd->es);
  16271. #elif defined(CONFIG_DRM)
  16272. if (active_panel)
  16273. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16274. #elif defined(CONFIG_FB)
  16275. fb_unregister_client(&cd->fb_notifier);
  16276. #endif
  16277. #ifdef NEED_SUSPEND_NOTIFIER
  16278. unregister_pm_notifier(&cd->pm_notifier);
  16279. #endif
  16280. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16281. device_wakeup_disable(dev);
  16282. #endif
  16283. device_init_wakeup(dev, 0);
  16284. #ifdef TTHE_TUNER_SUPPORT
  16285. mutex_lock(&cd->tthe_lock);
  16286. cd->tthe_exit = 1;
  16287. wake_up(&cd->wait_q);
  16288. mutex_unlock(&cd->tthe_lock);
  16289. debugfs_remove(cd->tthe_debugfs);
  16290. #endif
  16291. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16292. remove_sysfs_interfaces(dev);
  16293. disable_irq_nosync(cd->irq);
  16294. if (cd->cpdata->setup_irq)
  16295. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16296. if (cd->cpdata->init)
  16297. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16298. if (cd->cpdata->setup_power)
  16299. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16300. dev_set_drvdata(dev, NULL);
  16301. pt_del_core(dev);
  16302. pt_enable_regulator(cd, false);
  16303. pt_get_regulator(cd, false);
  16304. pt_free_si_ptrs(cd);
  16305. kfree(cd);
  16306. return 0;
  16307. }
  16308. EXPORT_SYMBOL_GPL(pt_release);
  16309. MODULE_LICENSE("GPL");
  16310. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16311. MODULE_AUTHOR("Parade Technologies <[email protected]>");