pt_core.c 515 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966
  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. #define ENABLE_VDD_REG_ONLY
  51. #define ENABLE_I2C_REG_ONLY
  52. #ifdef ENABLE_VDD_REG_ONLY
  53. static int pt_enable_vdd_regulator(struct pt_core_data *cd, bool en);
  54. #endif
  55. #ifdef ENABLE_I2C_REG_ONLY
  56. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  57. #endif
  58. static const char *pt_driver_core_name = PT_CORE_NAME;
  59. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  60. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  61. struct pt_hid_field {
  62. int report_count;
  63. int report_size;
  64. int size; /* report_count * report_size */
  65. int offset;
  66. int data_type;
  67. int logical_min;
  68. int logical_max;
  69. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  70. u32 usage_page;
  71. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  72. struct pt_hid_report *report;
  73. bool record_field;
  74. };
  75. struct pt_hid_report {
  76. u8 id;
  77. u8 type;
  78. int size;
  79. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  80. int num_fields;
  81. int record_field_index;
  82. int header_size;
  83. int record_size;
  84. u32 usage_page;
  85. };
  86. struct atten_node {
  87. struct list_head node;
  88. char *id;
  89. struct device *dev;
  90. int (*func)(struct device *dev);
  91. int mode;
  92. };
  93. struct param_node {
  94. struct list_head node;
  95. u8 id;
  96. u32 value;
  97. u8 size;
  98. };
  99. struct module_node {
  100. struct list_head node;
  101. struct pt_module *module;
  102. void *data;
  103. };
  104. struct pt_hid_cmd {
  105. u8 opcode;
  106. u8 report_type;
  107. union {
  108. u8 report_id;
  109. u8 power_state;
  110. };
  111. u8 has_data_register;
  112. size_t write_length;
  113. u8 *write_buf;
  114. u8 *read_buf;
  115. u8 wait_interrupt;
  116. u8 reset_cmd;
  117. u16 timeout_ms;
  118. };
  119. struct pt_hid_output {
  120. u8 cmd_type;
  121. u16 length;
  122. u8 command_code;
  123. size_t write_length;
  124. u8 *write_buf;
  125. u8 novalidate;
  126. u8 reset_expected;
  127. u16 timeout_ms;
  128. };
  129. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  130. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  131. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  132. #define CREATE_PIP1_FW_CMD(command) \
  133. .cmd_type = PIP1_CMD_TYPE_FW, \
  134. .command_code = command
  135. #define CREATE_PIP1_BL_CMD(command) \
  136. .cmd_type = PIP1_CMD_TYPE_BL, \
  137. .command_code = command
  138. #define PT_MAX_PR_BUF_SIZE 2048
  139. /*******************************************************************************
  140. * FUNCTION: pt_pr_buf
  141. *
  142. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  143. *
  144. * RETURN: Void
  145. *
  146. * PARAMETERS:
  147. * *dev - pointer to Device structure
  148. * debug_level - requested debug level to print at
  149. * *buf - pointer to buffer to print
  150. * buf_len - size of buf
  151. * *data_name - Descriptive name of data prefixed to data
  152. ******************************************************************************/
  153. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  154. u16 buf_len, const char *data_name)
  155. {
  156. struct pt_core_data *cd = dev_get_drvdata(dev);
  157. int i;
  158. ssize_t pr_buf_index = 0;
  159. int max_size;
  160. /* only proceed if valid debug level and there is data to print */
  161. if (debug_level <= cd->debug_level && buf_len > 0) {
  162. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  163. if (!pr_buf)
  164. return;
  165. /*
  166. * With a space each printed char takes 3 bytes, subtract
  167. * the length of the data_name prefix as well as 11 bytes
  168. * for the " [0..xxx]: " printed before the data.
  169. */
  170. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  171. /* Ensure pr_buf_index stays within the 1018 size */
  172. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  173. data_name);
  174. for (i = 0; i < buf_len && i < max_size; i++)
  175. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  176. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  177. pt_debug(dev, debug_level, "%s\n", pr_buf);
  178. kfree(pr_buf);
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(pt_pr_buf);
  182. #ifdef TTHE_TUNER_SUPPORT
  183. /*******************************************************************************
  184. * FUNCTION: tthe_print
  185. *
  186. * SUMMARY: Format data name and time stamp as the header and format the
  187. * content of input buffer with hex base to "tthe_buf". And then wake up event
  188. * semaphore for tthe debugfs node.
  189. *
  190. * RETURN:
  191. * 0 = success
  192. * !0 = failure
  193. *
  194. * PARAMETERS:
  195. * *cd - pointer to core data
  196. * *buf - pointer to input buffer
  197. * buf_len - size of input buffer
  198. * *data_name - pointer to data name
  199. ******************************************************************************/
  200. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  201. const u8 *data_name)
  202. {
  203. int name_len = strlen(data_name);
  204. int i, n;
  205. u8 *p;
  206. int remain;
  207. u8 data_name_with_time_stamp[100];
  208. /* Prepend timestamp, if requested, to data_name */
  209. if (cd->show_timestamp) {
  210. scnprintf(data_name_with_time_stamp,
  211. sizeof(data_name_with_time_stamp),
  212. "[%u] %s", pt_get_time_stamp(), data_name);
  213. data_name = data_name_with_time_stamp;
  214. name_len = strlen(data_name);
  215. }
  216. mutex_lock(&cd->tthe_lock);
  217. if (!cd->tthe_buf)
  218. goto exit;
  219. /* Add 1 due to the '\n' that is appended at the end */
  220. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  221. goto exit;
  222. if (name_len + buf_len == 0)
  223. goto exit;
  224. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  225. if (remain < name_len)
  226. name_len = remain;
  227. p = cd->tthe_buf + cd->tthe_buf_len;
  228. memcpy(p, data_name, name_len);
  229. cd->tthe_buf_len += name_len;
  230. p += name_len;
  231. remain -= name_len;
  232. *p = 0;
  233. for (i = 0; i < buf_len; i++) {
  234. n = scnprintf(p, remain, "%02X ", buf[i]);
  235. if (n <= 0)
  236. break;
  237. p += n;
  238. remain -= n;
  239. cd->tthe_buf_len += n;
  240. }
  241. n = scnprintf(p, remain, "\n");
  242. cd->tthe_buf_len += n;
  243. exit:
  244. wake_up(&cd->wait_q);
  245. mutex_unlock(&cd->tthe_lock);
  246. return 0;
  247. }
  248. /*******************************************************************************
  249. * FUNCTION: _pt_request_tthe_print
  250. *
  251. * SUMMARY: Function pointer included in core_cmds to allow other modules
  252. * to request to print data to the "tthe_buffer".
  253. *
  254. * RETURN:
  255. * 0 = success
  256. * !0 = failure
  257. *
  258. * PARAMETERS:
  259. * *dev - pointer to device structure
  260. ******************************************************************************/
  261. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  262. int buf_len, const u8 *data_name)
  263. {
  264. struct pt_core_data *cd = dev_get_drvdata(dev);
  265. return tthe_print(cd, buf, buf_len, data_name);
  266. }
  267. #endif
  268. /*******************************************************************************
  269. * FUNCTION: pt_platform_detect_read
  270. *
  271. * SUMMARY: To be passed to platform dectect function to perform a read
  272. * operation.
  273. *
  274. * RETURN:
  275. * 0 = success
  276. * !0 = failure
  277. *
  278. * PARAMETERS:
  279. * *dev - pointer to Device structure
  280. * *buf - pointer to buffer where the data read will be stored
  281. * size - size to be read
  282. ******************************************************************************/
  283. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  284. {
  285. struct pt_core_data *cd = dev_get_drvdata(dev);
  286. return pt_adap_read_default(cd, buf, size);
  287. }
  288. /*******************************************************************************
  289. * FUNCTION: pt_add_parameter
  290. *
  291. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  292. * On every reset of the DUT this linked list is traversed and all
  293. * parameters in it are restored to the DUT.
  294. *
  295. * RETURN:
  296. * 0 = success
  297. * !0 = failure
  298. *
  299. * PARAMETERS:
  300. * *cd - pointer to core data
  301. * param_id - parameter ID to add
  302. * param_value - Value corresponding to the ID
  303. * param_size - Size of param_value
  304. ******************************************************************************/
  305. static int pt_add_parameter(struct pt_core_data *cd,
  306. u8 param_id, u32 param_value, u8 param_size)
  307. {
  308. struct param_node *param, *param_new;
  309. /* Check if parameter already exists in the list */
  310. spin_lock(&cd->spinlock);
  311. list_for_each_entry(param, &cd->param_list, node) {
  312. if (param->id == param_id) {
  313. /* Update parameter */
  314. param->value = param_value;
  315. pt_debug(cd->dev, DL_INFO,
  316. "%s: Update parameter id:%d value:%d size:%d\n",
  317. __func__, param_id, param_value, param_size);
  318. goto exit_unlock;
  319. }
  320. }
  321. spin_unlock(&cd->spinlock);
  322. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  323. if (!param_new)
  324. return -ENOMEM;
  325. param_new->id = param_id;
  326. param_new->value = param_value;
  327. param_new->size = param_size;
  328. pt_debug(cd->dev, DL_INFO,
  329. "%s: Add parameter id:%d value:%d size:%d\n",
  330. __func__, param_id, param_value, param_size);
  331. spin_lock(&cd->spinlock);
  332. list_add(&param_new->node, &cd->param_list);
  333. exit_unlock:
  334. spin_unlock(&cd->spinlock);
  335. return 0;
  336. }
  337. #ifdef TTDL_DIAGNOSTICS
  338. /*******************************************************************************
  339. * FUNCTION: pt_erase_parameter_list
  340. *
  341. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  342. * pairs. In some test cases this functionality is needed to ensure DUT
  343. * returns to a virgin state after a reset and no parameters are restored.
  344. *
  345. * RETURN:
  346. * 0 = success
  347. * !0 = failure
  348. *
  349. * PARAMETERS:
  350. * *cd - pointer to core data
  351. ******************************************************************************/
  352. static int pt_erase_parameter_list(struct pt_core_data *cd)
  353. {
  354. struct param_node *pos, *temp;
  355. spin_lock(&cd->spinlock);
  356. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  357. pt_debug(cd->dev, DL_INFO,
  358. "%s: Parameter Restore List - remove 0x%02x\n",
  359. __func__, pos->id);
  360. list_del(&pos->node);
  361. kfree(pos);
  362. }
  363. spin_unlock(&cd->spinlock);
  364. return 0;
  365. }
  366. /*******************************************************************************
  367. * FUNCTION: pt_count_parameter_list
  368. *
  369. * SUMMARY: Count the items in the RAM parameter restor list
  370. *
  371. * RETURN:
  372. * 0 = success
  373. * !0 = failure
  374. *
  375. * PARAMETERS:
  376. * *cd - pointer to core data
  377. ******************************************************************************/
  378. static int pt_count_parameter_list(struct pt_core_data *cd)
  379. {
  380. struct param_node *pos, *temp;
  381. int entries = 0;
  382. spin_lock(&cd->spinlock);
  383. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  384. entries++;
  385. spin_unlock(&cd->spinlock);
  386. return entries;
  387. }
  388. #endif /* TTDL_DIAGNOSTICS */
  389. /*******************************************************************************
  390. * FUNCTION: request_exclusive
  391. *
  392. * SUMMARY: Request exclusive access to the DUT
  393. *
  394. * RETURN:
  395. * 0 = success
  396. * !0 = failure
  397. *
  398. * PARAMETERS:
  399. * *cd - pointer to core data
  400. * *ownptr - pointer to device
  401. * timeout_ms - Timeout value
  402. ******************************************************************************/
  403. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  404. int timeout_ms)
  405. {
  406. int t = msecs_to_jiffies(timeout_ms);
  407. bool with_timeout = (timeout_ms != 0);
  408. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  409. __func__, timeout_ms);
  410. mutex_lock(&cd->system_lock);
  411. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  412. cd->exclusive_dev = ownptr;
  413. goto exit;
  414. }
  415. cd->exclusive_waits++;
  416. wait:
  417. mutex_unlock(&cd->system_lock);
  418. if (with_timeout) {
  419. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  420. if (IS_TMO(t)) {
  421. pt_debug(cd->dev, DL_ERROR,
  422. "%s: tmo waiting exclusive access\n", __func__);
  423. return -ETIME;
  424. }
  425. } else {
  426. wait_event(cd->wait_q, !cd->exclusive_dev);
  427. }
  428. mutex_lock(&cd->system_lock);
  429. if (cd->exclusive_dev)
  430. goto wait;
  431. cd->exclusive_dev = ownptr;
  432. cd->exclusive_waits--;
  433. exit:
  434. mutex_unlock(&cd->system_lock);
  435. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  436. __func__, ownptr);
  437. return 0;
  438. }
  439. /*******************************************************************************
  440. * FUNCTION: release_exclusive_
  441. *
  442. * SUMMARY: Release exclusive access to the DUT
  443. *
  444. * RETURN:
  445. * 0 = success
  446. *
  447. * PARAMETERS:
  448. * *cd - pointer to core data
  449. * *ownptr - pointer to device
  450. ******************************************************************************/
  451. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  452. {
  453. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  454. __func__);
  455. if (cd->exclusive_dev != ownptr)
  456. return -EINVAL;
  457. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  458. __func__, cd->exclusive_dev);
  459. cd->exclusive_dev = NULL;
  460. wake_up(&cd->wait_q);
  461. return 0;
  462. }
  463. /*******************************************************************************
  464. * FUNCTION: release_exclusive
  465. *
  466. * SUMMARY: Protected wrapper to release_exclusive_()
  467. *
  468. * RETURN:
  469. * 0 = success
  470. * !0 = failure
  471. *
  472. * PARAMETERS:
  473. * *cd - pointer to core data
  474. * *ownptr - pointer to device
  475. ******************************************************************************/
  476. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  477. {
  478. int rc;
  479. mutex_lock(&cd->system_lock);
  480. rc = release_exclusive_(cd, ownptr);
  481. mutex_unlock(&cd->system_lock);
  482. return rc;
  483. }
  484. /*******************************************************************************
  485. * FUNCTION: pt_hid_exec_cmd_
  486. *
  487. * SUMMARY: Send the HID command to the DUT
  488. *
  489. * RETURN:
  490. * 0 = success
  491. * !0 = failure
  492. *
  493. * PARAMETERS:
  494. * *cd - pointer to core data
  495. * *hid_cmd - pointer to the HID command to send
  496. ******************************************************************************/
  497. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  498. struct pt_hid_cmd *hid_cmd)
  499. {
  500. int rc = 0;
  501. u8 *cmd;
  502. u16 cmd_length;
  503. u8 cmd_offset = 0;
  504. cmd_length = 2 /* command register */
  505. + 2 /* command */
  506. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  507. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  508. + hid_cmd->write_length; /* Data length */
  509. cmd = kzalloc(cmd_length, GFP_KERNEL);
  510. if (!cmd)
  511. return -ENOMEM;
  512. /* Set Command register */
  513. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  514. sizeof(cd->hid_desc.command_register));
  515. cmd_offset += sizeof(cd->hid_desc.command_register);
  516. /* Set Command */
  517. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  518. if (hid_cmd->report_id >= 0XF)
  519. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  520. else
  521. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  522. cmd_offset++;
  523. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  524. cmd_offset++;
  525. if (hid_cmd->report_id >= 0XF) {
  526. cmd[cmd_offset] = hid_cmd->report_id;
  527. cmd_offset++;
  528. }
  529. /* Set Data register */
  530. if (hid_cmd->has_data_register) {
  531. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  532. sizeof(cd->hid_desc.data_register));
  533. cmd_offset += sizeof(cd->hid_desc.data_register);
  534. }
  535. /* Set Data */
  536. if (hid_cmd->write_length && hid_cmd->write_buf) {
  537. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  538. hid_cmd->write_length);
  539. cmd_offset += hid_cmd->write_length;
  540. }
  541. pt_debug(cd->dev, DL_INFO,
  542. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  543. __func__, cmd_length, hid_cmd->report_id);
  544. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  545. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  546. hid_cmd->read_buf);
  547. if (rc)
  548. pt_debug(cd->dev, DL_ERROR,
  549. "%s: Fail pt_adap_transfer\n", __func__);
  550. kfree(cmd);
  551. return rc;
  552. }
  553. #ifdef TTDL_DIAGNOSTICS
  554. /*******************************************************************************
  555. * FUNCTION: pt_toggle_err_gpio
  556. *
  557. * SUMMARY: Toggles the pre-defined error GPIO
  558. *
  559. * RETURN: n/a
  560. *
  561. * PARAMETERS:
  562. * *cd - pointer to core data
  563. * type - type of err that occurred
  564. ******************************************************************************/
  565. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  566. {
  567. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  568. __func__, type);
  569. if (cd->err_gpio && type == cd->err_gpio_type) {
  570. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  571. gpio_direction_output(cd->err_gpio,
  572. !gpio_get_value(cd->err_gpio));
  573. }
  574. }
  575. /*******************************************************************************
  576. * FUNCTION: _pt_request_toggle_err_gpio
  577. *
  578. * SUMMARY: Function pointer included in core_cmds to allow other modules
  579. * to request to toggle the err_gpio
  580. *
  581. * RETURN: n/a
  582. *
  583. * PARAMETERS:
  584. * *cd - pointer to core data
  585. * type - type of err that occurred
  586. ******************************************************************************/
  587. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  588. {
  589. struct pt_core_data *cd = dev_get_drvdata(dev);
  590. pt_toggle_err_gpio(cd, type);
  591. }
  592. #endif /* TTDL_DIAGNOSTICS */
  593. /*******************************************************************************
  594. * FUNCTION: pt_hid_exec_cmd_and_wait_
  595. *
  596. * SUMMARY: Send the HID command to the DUT and wait for the response
  597. *
  598. * RETURN:
  599. * 0 = success
  600. * !0 = failure
  601. *
  602. * PARAMETERS:
  603. * *cd - pointer to core data
  604. * *hid_cmd - pointer to the HID command to send
  605. ******************************************************************************/
  606. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  607. struct pt_hid_cmd *hid_cmd)
  608. {
  609. int rc = 0;
  610. int t;
  611. u16 timeout_ms;
  612. int *cmd_state;
  613. if (hid_cmd->reset_cmd)
  614. cmd_state = &cd->hid_reset_cmd_state;
  615. else
  616. cmd_state = &cd->hid_cmd_state;
  617. if (hid_cmd->wait_interrupt) {
  618. mutex_lock(&cd->system_lock);
  619. *cmd_state = 1;
  620. mutex_unlock(&cd->system_lock);
  621. }
  622. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  623. if (rc) {
  624. if (hid_cmd->wait_interrupt)
  625. goto error;
  626. goto exit;
  627. }
  628. if (!hid_cmd->wait_interrupt)
  629. goto exit;
  630. if (hid_cmd->timeout_ms)
  631. timeout_ms = hid_cmd->timeout_ms;
  632. else
  633. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  634. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  635. msecs_to_jiffies(timeout_ms));
  636. if (IS_TMO(t)) {
  637. #ifdef TTDL_DIAGNOSTICS
  638. cd->bus_transmit_error_count++;
  639. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  640. #endif /* TTDL_DIAGNOSTICS */
  641. pt_debug(cd->dev, DL_ERROR,
  642. "%s: HID output cmd execution timed out\n",
  643. __func__);
  644. rc = -ETIME;
  645. goto error;
  646. }
  647. goto exit;
  648. error:
  649. mutex_lock(&cd->system_lock);
  650. *cmd_state = 0;
  651. mutex_unlock(&cd->system_lock);
  652. exit:
  653. return rc;
  654. }
  655. /*******************************************************************************
  656. * FUNCTION: pt_hid_cmd_reset_
  657. *
  658. * SUMMARY: Send the HID RESET command to the DUT
  659. *
  660. * RETURN:
  661. * 0 = success
  662. * !0 = failure
  663. *
  664. * PARAMETERS:
  665. * *cd - pointer to core data
  666. ******************************************************************************/
  667. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  668. {
  669. struct pt_hid_cmd hid_cmd = {
  670. .opcode = HID_CMD_RESET,
  671. .wait_interrupt = 1,
  672. .reset_cmd = 1,
  673. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  674. };
  675. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  676. }
  677. /*******************************************************************************
  678. * FUNCTION: pt_hid_cmd_reset
  679. *
  680. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  681. * access.
  682. *
  683. * RETURN:
  684. * 0 = success
  685. * !0 = failure
  686. *
  687. * PARAMETERS:
  688. * *cd - pointer to core data
  689. ******************************************************************************/
  690. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  691. {
  692. int rc = 0;
  693. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  694. if (rc < 0) {
  695. pt_debug(cd->dev, DL_ERROR,
  696. "%s: fail get exclusive ex=%p own=%p\n",
  697. __func__, cd->exclusive_dev, cd->dev);
  698. return rc;
  699. }
  700. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  701. rc = pt_hid_cmd_reset_(cd);
  702. if (release_exclusive(cd, cd->dev) < 0)
  703. pt_debug(cd->dev, DL_ERROR,
  704. "%s: fail to release exclusive\n", __func__);
  705. return rc;
  706. }
  707. /*******************************************************************************
  708. * FUNCTION: pt_hid_cmd_set_power_
  709. *
  710. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  711. *
  712. * RETURN:
  713. * 0 = success
  714. * !0 = failure
  715. *
  716. * PARAMETERS:
  717. * *cd - pointer to core data
  718. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  719. ******************************************************************************/
  720. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  721. u8 power_state)
  722. {
  723. int rc = 0;
  724. struct pt_hid_cmd hid_cmd = {
  725. .opcode = HID_CMD_SET_POWER,
  726. .wait_interrupt = 1,
  727. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  728. };
  729. hid_cmd.power_state = power_state;
  730. /* The chip won't give response if goes to Deep Standby */
  731. if (power_state == HID_POWER_STANDBY) {
  732. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  733. if (rc)
  734. pt_debug(cd->dev, DL_ERROR,
  735. "%s: Failed to set power to state:%d\n",
  736. __func__, power_state);
  737. else
  738. cd->fw_sys_mode_in_standby_state = true;
  739. return rc;
  740. }
  741. cd->fw_sys_mode_in_standby_state = false;
  742. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  743. if (rc) {
  744. pt_debug(cd->dev, DL_ERROR,
  745. "%s: Failed to set power to state:%d\n",
  746. __func__, power_state);
  747. return rc;
  748. }
  749. /* validate */
  750. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  751. || ((cd->response_buf[3] & 0x3) != power_state)
  752. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  753. rc = -EINVAL;
  754. return rc;
  755. }
  756. /*******************************************************************************
  757. * FUNCTION: pt_hid_cmd_set_power
  758. *
  759. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  760. * exclusive access.
  761. *
  762. * RETURN:
  763. * 0 = success
  764. * !0 = failure
  765. *
  766. * PARAMETERS:
  767. * *cd - pointer to core data
  768. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  769. ******************************************************************************/
  770. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  771. u8 power_state)
  772. {
  773. int rc;
  774. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  775. if (rc < 0) {
  776. pt_debug(cd->dev, DL_ERROR,
  777. "%s: fail get exclusive ex=%p own=%p\n",
  778. __func__, cd->exclusive_dev, cd->dev);
  779. return rc;
  780. }
  781. rc = pt_hid_cmd_set_power_(cd, power_state);
  782. if (release_exclusive(cd, cd->dev) < 0)
  783. pt_debug(cd->dev, DL_ERROR,
  784. "%s: fail to release exclusive\n", __func__);
  785. return rc;
  786. }
  787. static const u16 crc_table[16] = {
  788. 0x0000, 0x1021, 0x2042, 0x3063,
  789. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  790. 0x8108, 0x9129, 0xa14a, 0xb16b,
  791. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  792. };
  793. /*******************************************************************************
  794. * FUNCTION: _pt_compute_crc
  795. *
  796. * SUMMARY: Calculate CRC by CRC table.
  797. *
  798. * RETURN:
  799. * CRC calculation result
  800. *
  801. * PARAMETERS:
  802. * *buf - pointer to the data array to be calculated
  803. * size - size of data array
  804. ******************************************************************************/
  805. static u16 _pt_compute_crc(u8 *buf, u32 size)
  806. {
  807. u16 remainder = 0xFFFF;
  808. u16 xor_mask = 0x0000;
  809. u32 index;
  810. u32 byte_value;
  811. u32 table_index;
  812. u32 crc_bit_width = sizeof(u16) * 8;
  813. /* Divide the message by polynomial, via the table. */
  814. for (index = 0; index < size; index++) {
  815. byte_value = buf[index];
  816. table_index = ((byte_value >> 4) & 0x0F)
  817. ^ (remainder >> (crc_bit_width - 4));
  818. remainder = crc_table[table_index] ^ (remainder << 4);
  819. table_index = (byte_value & 0x0F)
  820. ^ (remainder >> (crc_bit_width - 4));
  821. remainder = crc_table[table_index] ^ (remainder << 4);
  822. }
  823. /* Perform the final remainder CRC. */
  824. return remainder ^ xor_mask;
  825. }
  826. u16 ccitt_Table[] = {
  827. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  828. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  829. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  830. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  831. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  832. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  833. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  834. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  835. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  836. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  837. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  838. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  839. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  840. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  841. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  842. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  843. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  844. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  845. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  846. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  847. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  848. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  849. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  850. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  851. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  852. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  853. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  854. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  855. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  856. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  857. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  858. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  859. };
  860. /*******************************************************************************
  861. * FUNCTION: crc_ccitt_calculate
  862. *
  863. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  864. *
  865. * RETURN:
  866. * CRC calculation result
  867. *
  868. * PARAMETERS:
  869. * *q - pointer to the data array to be calculated
  870. * len - size of data array
  871. ******************************************************************************/
  872. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  873. {
  874. unsigned short crc = 0xffff;
  875. while (len-- > 0)
  876. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  877. return crc;
  878. }
  879. /*******************************************************************************
  880. * FUNCTION: pt_pip2_cmd_calculate_crc
  881. *
  882. * SUMMARY: Calculate the CRC of a command packet
  883. *
  884. * RETURN: void
  885. *
  886. * PARAMETERS:
  887. * *cmd - pointer to command data
  888. * extra_bytes - Extra bytes included in command length
  889. ******************************************************************************/
  890. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  891. u8 extra_bytes)
  892. {
  893. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  894. unsigned short crc;
  895. buf[0] = cmd->len & 0xff;
  896. buf[1] = (cmd->len & 0xff00) >> 8;
  897. buf[2] = cmd->seq;
  898. buf[3] = cmd->id;
  899. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  900. /* Calculate the CRC for the first 4 bytes above and the data payload */
  901. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  902. cmd->crc[0] = (crc & 0xff00) >> 8;
  903. cmd->crc[1] = (crc & 0xff);
  904. }
  905. /*******************************************************************************
  906. * FUNCTION: pt_pip2_get_next_cmd_seq
  907. *
  908. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  909. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  910. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  911. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  912. * increment, just use the forced value.
  913. *
  914. * RETURN: Next command sequence number [0x08-0x0F]
  915. *
  916. * PARAMETERS:
  917. * *cd - pointer to core data
  918. ******************************************************************************/
  919. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  920. {
  921. #ifdef TTDL_DIAGNOSTICS
  922. if (cd->force_pip2_seq <= 0x07) {
  923. cd->pip2_cmd_tag_seq++;
  924. if (cd->pip2_cmd_tag_seq > 0x0F)
  925. cd->pip2_cmd_tag_seq = 0x08;
  926. } else {
  927. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  928. }
  929. #else
  930. cd->pip2_cmd_tag_seq++;
  931. if (cd->pip2_cmd_tag_seq > 0x0F)
  932. cd->pip2_cmd_tag_seq = 0x08;
  933. #endif
  934. return cd->pip2_cmd_tag_seq;
  935. }
  936. /*
  937. * Following macros are to define the response time (the interval between PIP2
  938. * command finishes sending and INT pin falls). The unit is in microsecond.
  939. * It has different time settings between the solution GPIO polling and Bus
  940. * polling due to the considration for system load.
  941. */
  942. #ifdef PT_POLL_RESP_BY_BUS
  943. #define POLL_RETRY_DEFAULT_INTERVAL 50
  944. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  945. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  946. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  947. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  948. #else
  949. #define POLL_RETRY_DEFAULT_INTERVAL 20
  950. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  951. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  952. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  953. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  954. #endif
  955. /*
  956. * id: the command id defined in PIP2
  957. * response_len: the (maximum) length of response.
  958. * response_time_min: minimum response time in microsecond
  959. * response_time_max: maximum response time in microsecond
  960. */
  961. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  962. {.id = PIP2_CMD_ID_PING,
  963. .response_len = 255,
  964. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  965. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  966. {.id = PIP2_CMD_ID_STATUS,
  967. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  968. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  969. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  970. {.id = PIP2_CMD_ID_CTRL,
  971. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  972. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  973. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  974. {.id = PIP2_CMD_ID_CONFIG,
  975. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  976. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  977. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  978. {.id = PIP2_CMD_ID_CLEAR,
  979. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  980. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  981. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  982. {.id = PIP2_CMD_ID_RESET,
  983. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  984. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  985. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  986. {.id = PIP2_CMD_ID_VERSION,
  987. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  988. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  989. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  990. {.id = PIP2_CMD_ID_FILE_OPEN,
  991. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  992. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  993. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  994. {.id = PIP2_CMD_ID_FILE_CLOSE,
  995. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  996. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  997. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  998. {.id = PIP2_CMD_ID_FILE_READ,
  999. .response_len = 255,
  1000. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1001. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1002. {.id = PIP2_CMD_ID_FILE_WRITE,
  1003. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1004. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_FLASH_INFO,
  1011. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1014. {.id = PIP2_CMD_ID_EXECUTE,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1016. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1022. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1023. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1024. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1025. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1026. {.id = PIP2_CMD_ID_READ_GPIO,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_EXECUTE_SCAN,
  1031. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1032. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1033. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1034. {.id = PIP2_CMD_SET_PARAMETER,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_GET_PARAMETER,
  1039. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1042. {.id = PIP2_CMD_SET_DDI_REG,
  1043. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1044. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1045. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1046. {.id = PIP2_CMD_GET_DDI_REG,
  1047. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1048. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1049. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1050. {.id = PIP2_CMD_ID_END,
  1051. .response_len = 255,
  1052. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1053. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1054. };
  1055. /*******************************************************************************
  1056. * FUNCTION: pt_pip2_get_cmd_response_len
  1057. *
  1058. * SUMMARY: Gets the expected response length based on the command ID
  1059. *
  1060. * RETURN: Expected response length
  1061. *
  1062. * PARAMETERS:
  1063. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1064. ******************************************************************************/
  1065. static int pt_pip2_get_cmd_response_len(u8 id)
  1066. {
  1067. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1068. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1069. p++;
  1070. if (p->id != PIP2_CMD_ID_END)
  1071. return p->response_len;
  1072. else
  1073. return -EPERM;
  1074. }
  1075. /*******************************************************************************
  1076. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1077. *
  1078. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1079. * finishes sending and INT pin falls) based on the command ID
  1080. *
  1081. * RETURN: Estimated minimum response time in microsecond
  1082. *
  1083. * PARAMETERS:
  1084. * id - Command ID
  1085. ******************************************************************************/
  1086. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1087. {
  1088. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1089. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1090. p++;
  1091. if (p->id != PIP2_CMD_ID_END)
  1092. return p->response_time_min;
  1093. else
  1094. return PIP2_RESP_DEFAULT_TIME_MIN;
  1095. }
  1096. /*******************************************************************************
  1097. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1098. *
  1099. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1100. * finishes sending and INT pin falls) based on the command ID
  1101. *
  1102. * RETURN: Estimated maximum response time in microsecond
  1103. *
  1104. * PARAMETERS:
  1105. * id - Command ID
  1106. ******************************************************************************/
  1107. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1108. {
  1109. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1110. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1111. p++;
  1112. if (p->id != PIP2_CMD_ID_END)
  1113. return p->response_time_max;
  1114. else
  1115. return PIP2_RESP_DEFAULT_TIME_MAX;
  1116. }
  1117. /*******************************************************************************
  1118. * FUNCTION: pt_pip2_validate_response
  1119. *
  1120. * SUMMARY: Validate the response of PIP2 command.
  1121. *
  1122. * RETURN:
  1123. * 0 = success
  1124. * !0 = failure
  1125. *
  1126. * PARAMETERS:
  1127. * *cd - pointer to core data
  1128. * *pip2_cmd - pointer to PIP2 command to send
  1129. * *read_buf - pointer to response buffer
  1130. * actual_read_len - actual read length of the response
  1131. ******************************************************************************/
  1132. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1133. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1134. u16 actual_read_len)
  1135. {
  1136. int rc = 0;
  1137. u8 response_seq = 0;
  1138. u8 reserved_bits = 0;
  1139. u8 cmd_id = 0;
  1140. u8 response_bit = 0;
  1141. unsigned short calc_crc = 0;
  1142. unsigned short resp_crc = 0;
  1143. /* Verify the length of response buffer */
  1144. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1145. pt_debug(cd->dev, DL_ERROR,
  1146. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1147. __func__, pip2_cmd->id, actual_read_len);
  1148. rc = -EINVAL;
  1149. goto exit;
  1150. }
  1151. /* Verify the CRC */
  1152. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1153. resp_crc = read_buf[actual_read_len - 2] << 8;
  1154. resp_crc |= read_buf[actual_read_len - 1];
  1155. if (resp_crc != calc_crc) {
  1156. pt_debug(cd->dev, DL_ERROR,
  1157. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1158. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1159. #ifdef TTDL_DIAGNOSTICS
  1160. cd->pip2_crc_error_count++;
  1161. #endif /* TTDL_DIAGNOSTICS */
  1162. rc = -EINVAL;
  1163. goto exit;
  1164. }
  1165. /* Verify the response bit is set */
  1166. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1167. if (!response_bit) {
  1168. pt_debug(cd->dev, DL_ERROR,
  1169. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1170. __func__, pip2_cmd->id, response_bit);
  1171. rc = -EINVAL;
  1172. goto exit;
  1173. }
  1174. /* Verify the command ID matches from command to response */
  1175. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1176. if (cmd_id != pip2_cmd->id) {
  1177. pt_debug(cd->dev, DL_ERROR,
  1178. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1179. __func__, pip2_cmd->id, cmd_id);
  1180. rc = -EINVAL;
  1181. goto exit;
  1182. }
  1183. /* Verify the SEQ number matches from command to response */
  1184. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1185. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1186. pt_debug(cd->dev, DL_ERROR,
  1187. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1188. __func__, pip2_cmd->id,
  1189. pip2_cmd->seq, response_seq);
  1190. rc = -EINVAL;
  1191. goto exit;
  1192. }
  1193. /* Verify the reserved bits are 0 */
  1194. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1195. if (reserved_bits)
  1196. pt_debug(cd->dev, DL_WARN,
  1197. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1198. __func__, pip2_cmd->id, reserved_bits);
  1199. exit:
  1200. if (rc)
  1201. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1202. "PIP RSP:");
  1203. return rc;
  1204. }
  1205. /*******************************************************************************
  1206. * FUNCTION: pt_hid_output_validate_bl_response
  1207. *
  1208. * SUMMARY: Validate the response of bootloader command.
  1209. *
  1210. * RETURN:
  1211. * 0 = success
  1212. * !0 = failure
  1213. *
  1214. * PARAMETERS:
  1215. * *cd - pointer to core data
  1216. * *hid_output - pointer to hid output data structure
  1217. ******************************************************************************/
  1218. static int pt_hid_output_validate_bl_response(
  1219. struct pt_core_data *cd,
  1220. struct pt_hid_output *hid_output)
  1221. {
  1222. u16 size;
  1223. u16 crc;
  1224. u8 status;
  1225. size = get_unaligned_le16(&cd->response_buf[0]);
  1226. if (hid_output->reset_expected && !size)
  1227. return 0;
  1228. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1229. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1230. pt_debug(cd->dev, DL_ERROR,
  1231. "%s: BL output response, wrong report_id\n", __func__);
  1232. return -EPROTO;
  1233. }
  1234. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1235. pt_debug(cd->dev, DL_ERROR,
  1236. "%s: BL output response, wrong SOP\n", __func__);
  1237. return -EPROTO;
  1238. }
  1239. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1240. pt_debug(cd->dev, DL_ERROR,
  1241. "%s: BL output response, wrong EOP\n", __func__);
  1242. return -EPROTO;
  1243. }
  1244. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1245. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1246. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1247. pt_debug(cd->dev, DL_ERROR,
  1248. "%s: BL output response, wrong CRC 0x%X\n",
  1249. __func__, crc);
  1250. return -EPROTO;
  1251. }
  1252. status = cd->response_buf[5];
  1253. if (status) {
  1254. pt_debug(cd->dev, DL_ERROR,
  1255. "%s: BL output response, ERROR:%d\n",
  1256. __func__, status);
  1257. return -EPROTO;
  1258. }
  1259. return 0;
  1260. }
  1261. /*******************************************************************************
  1262. * FUNCTION: pt_hid_output_validate_app_response
  1263. *
  1264. * SUMMARY: Validate the response of application command.
  1265. *
  1266. * RETURN:
  1267. * 0 = success
  1268. * !0 = failure
  1269. *
  1270. * PARAMETERS:
  1271. * *cd - pointer to core data
  1272. * *hid_output - pointer to hid output data structure
  1273. ******************************************************************************/
  1274. static int pt_hid_output_validate_app_response(
  1275. struct pt_core_data *cd,
  1276. struct pt_hid_output *hid_output)
  1277. {
  1278. int command_code;
  1279. u16 size;
  1280. size = get_unaligned_le16(&cd->response_buf[0]);
  1281. if (hid_output->reset_expected && !size)
  1282. return 0;
  1283. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1284. != PT_PIP_NON_HID_RESPONSE_ID) {
  1285. pt_debug(cd->dev, DL_ERROR,
  1286. "%s: APP output response, wrong report_id\n", __func__);
  1287. return -EPROTO;
  1288. }
  1289. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1290. & PIP1_RESP_COMMAND_ID_MASK;
  1291. if (command_code != hid_output->command_code) {
  1292. pt_debug(cd->dev, DL_ERROR,
  1293. "%s: APP output response, wrong command_code:%X\n",
  1294. __func__, command_code);
  1295. return -EPROTO;
  1296. }
  1297. return 0;
  1298. }
  1299. /*******************************************************************************
  1300. * FUNCTION: pt_check_set_parameter
  1301. *
  1302. * SUMMARY: Check command input and response for Set Parameter command.And
  1303. * store the parameter to the list for resume work if pass the check.
  1304. *
  1305. * PARAMETERS:
  1306. * *cd - pointer to core data
  1307. * *hid_output - pointer to hid output data structure
  1308. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1309. ******************************************************************************/
  1310. static void pt_check_set_parameter(struct pt_core_data *cd,
  1311. struct pt_hid_output *hid_output, bool raw)
  1312. {
  1313. u8 *param_buf;
  1314. u32 param_value = 0;
  1315. u8 param_size;
  1316. u8 param_id;
  1317. int i = 0;
  1318. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1319. return;
  1320. /* Check command input for Set Parameter command */
  1321. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1322. && !memcmp(&hid_output->write_buf[0],
  1323. &cd->hid_desc.output_register,
  1324. sizeof(cd->hid_desc.output_register))
  1325. && hid_output->write_buf[4] ==
  1326. PT_PIP_NON_HID_COMMAND_ID
  1327. && hid_output->write_buf[6] ==
  1328. PIP1_CMD_ID_SET_PARAM)
  1329. param_buf = &hid_output->write_buf[7];
  1330. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1331. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1332. && hid_output->write_length >= 3
  1333. && hid_output->write_length <= 6)
  1334. param_buf = &hid_output->write_buf[0];
  1335. else
  1336. return;
  1337. /* Get parameter ID, size and value */
  1338. param_id = param_buf[0];
  1339. param_size = param_buf[1];
  1340. if (param_size > 4) {
  1341. pt_debug(cd->dev, DL_ERROR,
  1342. "%s: Invalid parameter size\n", __func__);
  1343. return;
  1344. }
  1345. param_buf = &param_buf[2];
  1346. while (i < param_size)
  1347. param_value += *(param_buf++) << (8 * i++);
  1348. /* Check command response for Set Parameter command */
  1349. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1350. || (cd->response_buf[4] &
  1351. PIP1_RESP_COMMAND_ID_MASK) !=
  1352. PIP1_CMD_ID_SET_PARAM
  1353. || cd->response_buf[5] != param_id
  1354. || cd->response_buf[6] != param_size) {
  1355. pt_debug(cd->dev, DL_ERROR,
  1356. "%s: Set Parameter command not successful\n",
  1357. __func__);
  1358. return;
  1359. }
  1360. pt_add_parameter(cd, param_id, param_value, param_size);
  1361. }
  1362. /*******************************************************************************
  1363. * FUNCTION: pt_check_command
  1364. *
  1365. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1366. * called here to check output command and store parameter to the list.
  1367. *
  1368. * PARAMETERS:
  1369. * *cd - pointer to core data
  1370. * *hid_output - pointer to hid output data structure
  1371. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1372. ******************************************************************************/
  1373. static void pt_check_command(struct pt_core_data *cd,
  1374. struct pt_hid_output *hid_output, bool raw)
  1375. {
  1376. pt_check_set_parameter(cd, hid_output, raw);
  1377. }
  1378. /*******************************************************************************
  1379. * FUNCTION: pt_hid_output_validate_response
  1380. *
  1381. * SUMMARY: Validate the response of application or bootloader command.
  1382. *
  1383. * RETURN:
  1384. * 0 = success
  1385. * !0 = failure
  1386. *
  1387. * PARAMETERS:
  1388. * *cd - pointer to core data
  1389. * *hid_output - pointer to hid output data structure
  1390. ******************************************************************************/
  1391. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1392. struct pt_hid_output *hid_output)
  1393. {
  1394. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1395. return pt_hid_output_validate_bl_response(cd, hid_output);
  1396. return pt_hid_output_validate_app_response(cd, hid_output);
  1397. }
  1398. /*******************************************************************************
  1399. * FUNCTION: pt_hid_send_output_user_
  1400. *
  1401. * SUMMARY: Blindly send user data to the DUT.
  1402. *
  1403. * RETURN:
  1404. * 0 = success
  1405. * !0 = failure
  1406. *
  1407. * PARAMETERS:
  1408. * *cd - pointer to core data
  1409. * *hid_output - pointer to the command to send
  1410. ******************************************************************************/
  1411. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1412. struct pt_hid_output *hid_output)
  1413. {
  1414. int rc = 0;
  1415. int cmd;
  1416. if (!hid_output->length || !hid_output->write_buf)
  1417. return -EINVAL;
  1418. if (cd->pip2_prot_active) {
  1419. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1420. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1421. } else
  1422. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1423. pt_debug(cd->dev, DL_INFO,
  1424. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1425. __func__, hid_output->length, cmd);
  1426. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1427. hid_output->length, ">>> User CMD");
  1428. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1429. hid_output->write_buf, NULL);
  1430. if (rc)
  1431. pt_debug(cd->dev, DL_ERROR,
  1432. "%s: Fail pt_adap_transfer\n", __func__);
  1433. return rc;
  1434. }
  1435. /*******************************************************************************
  1436. * FUNCTION: pt_hid_send_output_user_and_wait_
  1437. *
  1438. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1439. *
  1440. * RETURN:
  1441. * 0 = success
  1442. * !0 = failure
  1443. *
  1444. * PARAMETERS:
  1445. * *cd - pointer to core data
  1446. * *hid_output - pointer to the command to send
  1447. ******************************************************************************/
  1448. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1449. struct pt_hid_output *hid_output)
  1450. {
  1451. int rc = 0;
  1452. int t;
  1453. mutex_lock(&cd->system_lock);
  1454. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1455. mutex_unlock(&cd->system_lock);
  1456. rc = pt_hid_send_output_user_(cd, hid_output);
  1457. if (rc)
  1458. goto error;
  1459. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1460. msecs_to_jiffies(cd->pip_cmd_timeout));
  1461. if (IS_TMO(t)) {
  1462. #ifdef TTDL_DIAGNOSTICS
  1463. cd->bus_transmit_error_count++;
  1464. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1465. #endif /* TTDL_DIAGNOSTICS */
  1466. pt_debug(cd->dev, DL_ERROR,
  1467. "%s: HID output cmd execution timed out\n",
  1468. __func__);
  1469. rc = -ETIME;
  1470. goto error;
  1471. }
  1472. pt_check_command(cd, hid_output, true);
  1473. goto exit;
  1474. error:
  1475. mutex_lock(&cd->system_lock);
  1476. cd->hid_cmd_state = 0;
  1477. mutex_unlock(&cd->system_lock);
  1478. exit:
  1479. return rc;
  1480. }
  1481. /*******************************************************************************
  1482. * FUNCTION: pt_check_irq_asserted
  1483. *
  1484. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1485. * the FW can hold the INT line low ~150us after the read is complete.
  1486. * NOTE: if irq_stat is not defined this function will return false
  1487. *
  1488. * RETURN:
  1489. * true = IRQ asserted
  1490. * false = IRQ not asserted
  1491. *
  1492. * PARAMETERS:
  1493. * *cd - pointer to core data
  1494. ******************************************************************************/
  1495. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1496. {
  1497. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1498. /*
  1499. * Workaround for FW defect, CDT165308
  1500. * bl_launch app creates a glitch in IRQ line
  1501. */
  1502. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1503. && cd->cpdata->irq_stat) {
  1504. /*
  1505. * in X1S panel and GC1546 panel, the width for the INT
  1506. * glitch is about 4us,the normal INT width of response
  1507. * will last more than 200us, so use 10us delay
  1508. * for distinguish the glitch the normal INT is enough.
  1509. */
  1510. udelay(10);
  1511. }
  1512. #endif
  1513. if (cd->cpdata->irq_stat) {
  1514. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1515. == PT_IRQ_ASSERTED_VALUE) {
  1516. /* Debounce to allow FW to release INT */
  1517. usleep_range(100, 200);
  1518. }
  1519. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1520. == PT_IRQ_ASSERTED_VALUE)
  1521. return true;
  1522. else
  1523. return false;
  1524. }
  1525. return true;
  1526. }
  1527. /*******************************************************************************
  1528. * FUNCTION: pt_flush_bus
  1529. *
  1530. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1531. * Used if IRQ is found to be stuck low
  1532. *
  1533. * RETURN: Length of bytes read from bus
  1534. *
  1535. * PARAMETERS:
  1536. * *cd - pointer to core data
  1537. * flush_type - type of flush
  1538. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1539. * - PT_FLUSH_BUS_FULL_256_READ
  1540. * *read_buf - pointer to store read data
  1541. ******************************************************************************/
  1542. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1543. u8 flush_type, u8 *read_buf)
  1544. {
  1545. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1546. u16 pip_len;
  1547. int bytes_read;
  1548. int rc = 0;
  1549. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1550. rc = pt_adap_read_default(cd, buf, 2);
  1551. if (rc) {
  1552. bytes_read = 0;
  1553. goto exit;
  1554. }
  1555. pip_len = get_unaligned_le16(&buf[0]);
  1556. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1557. #ifdef TTDL_DIAGNOSTICS
  1558. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1559. #endif
  1560. bytes_read = 2;
  1561. pt_debug(cd->dev, DL_INFO,
  1562. "%s: Empty buf detected - len=0x%04X\n",
  1563. __func__, pip_len);
  1564. } else if (pip_len == 0) {
  1565. bytes_read = 0;
  1566. pt_debug(cd->dev, DL_INFO,
  1567. "%s: Sentinel detected\n", __func__);
  1568. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1569. pt_debug(cd->dev, DL_ERROR,
  1570. "%s: Illegal len=0x%04x, force %d byte read\n",
  1571. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1572. rc = pt_adap_read_default(cd, buf,
  1573. PT_MAX_PIP2_MSG_SIZE);
  1574. if (!rc)
  1575. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1576. else
  1577. bytes_read = 0;
  1578. } else {
  1579. pt_debug(cd->dev, DL_INFO,
  1580. "%s: Flush read of %d bytes...\n",
  1581. __func__, pip_len);
  1582. rc = pt_adap_read_default(cd, buf, pip_len);
  1583. if (!rc)
  1584. bytes_read = pip_len;
  1585. else
  1586. bytes_read = 0;
  1587. }
  1588. } else {
  1589. pt_debug(cd->dev, DL_INFO,
  1590. "%s: Forced flush of max %d bytes...\n",
  1591. __func__, PT_MAX_PIP2_MSG_SIZE);
  1592. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1593. if (!rc)
  1594. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1595. else
  1596. bytes_read = 0;
  1597. }
  1598. if (read_buf && (bytes_read > 3))
  1599. memcpy(read_buf, buf, bytes_read);
  1600. exit:
  1601. return bytes_read;
  1602. }
  1603. /*******************************************************************************
  1604. * FUNCTION: pt_flush_bus_if_irq_asserted
  1605. *
  1606. * SUMMARY: This function will flush the active bus if the INT is found to be
  1607. * asserted.
  1608. *
  1609. * RETURN: bytes cleared from bus
  1610. *
  1611. * PARAMETERS:
  1612. * *cd - pointer the core data structure
  1613. * flush_type - type of flush
  1614. * - PT_FLUSH_BUS_BASED_ON_LEN
  1615. * - PT_FLUSH_BUS_FULL_256_READ
  1616. ******************************************************************************/
  1617. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1618. {
  1619. int count = 0;
  1620. int bytes_read = 0;
  1621. while (pt_check_irq_asserted(cd) && count < 5) {
  1622. count++;
  1623. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1624. if (bytes_read) {
  1625. pt_debug(cd->dev, DL_WARN,
  1626. "%s: Cleared %d bytes off bus\n",
  1627. __func__, bytes_read);
  1628. }
  1629. }
  1630. if (pt_check_irq_asserted(cd)) {
  1631. pt_debug(cd->dev, DL_ERROR,
  1632. "%s: IRQ still asserted, %d bytes read\n",
  1633. __func__, bytes_read);
  1634. } else {
  1635. pt_debug(cd->dev, DL_INFO,
  1636. "%s: IRQ cleared, %d bytes read\n",
  1637. __func__, bytes_read);
  1638. }
  1639. return bytes_read;
  1640. }
  1641. /*******************************************************************************
  1642. * FUNCTION: pt_hid_send_output_
  1643. *
  1644. * SUMMARY: Send a touch application command to the DUT
  1645. *
  1646. * RETURN:
  1647. * 0 = success
  1648. * !0 = failure
  1649. *
  1650. * PARAMETERS:
  1651. * *cd - pointer to core data
  1652. * *hid_output - pointer to the command to send
  1653. ******************************************************************************/
  1654. static int pt_hid_send_output_(struct pt_core_data *cd,
  1655. struct pt_hid_output *hid_output)
  1656. {
  1657. int rc = 0;
  1658. u8 *cmd;
  1659. u16 length;
  1660. u16 crc;
  1661. u8 report_id;
  1662. u8 cmd_offset = 0;
  1663. u8 cmd_allocated = 0;
  1664. switch (hid_output->cmd_type) {
  1665. case PIP1_CMD_TYPE_FW:
  1666. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1667. length = 5;
  1668. break;
  1669. case PIP1_CMD_TYPE_BL:
  1670. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1671. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1672. break;
  1673. default:
  1674. return -EINVAL;
  1675. }
  1676. length += hid_output->write_length;
  1677. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1678. cmd = kzalloc(length + 2, GFP_KERNEL);
  1679. if (!cmd)
  1680. return -ENOMEM;
  1681. cmd_allocated = 1;
  1682. } else {
  1683. cmd = cd->cmd_buf;
  1684. }
  1685. /* Set Output register */
  1686. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1687. sizeof(cd->hid_desc.output_register));
  1688. cmd_offset += sizeof(cd->hid_desc.output_register);
  1689. cmd[cmd_offset++] = LOW_BYTE(length);
  1690. cmd[cmd_offset++] = HI_BYTE(length);
  1691. cmd[cmd_offset++] = report_id;
  1692. cmd[cmd_offset++] = 0x0; /* reserved */
  1693. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1694. cmd[cmd_offset++] = PIP1_BL_SOP;
  1695. cmd[cmd_offset++] = hid_output->command_code;
  1696. /* Set Data Length for bootloader */
  1697. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1698. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1699. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1700. }
  1701. /* Set Data */
  1702. if (hid_output->write_length && hid_output->write_buf) {
  1703. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1704. hid_output->write_length);
  1705. cmd_offset += hid_output->write_length;
  1706. }
  1707. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1708. crc = _pt_compute_crc(&cmd[6],
  1709. hid_output->write_length + 4);
  1710. cmd[cmd_offset++] = LOW_BYTE(crc);
  1711. cmd[cmd_offset++] = HI_BYTE(crc);
  1712. cmd[cmd_offset++] = PIP1_BL_EOP;
  1713. }
  1714. pt_debug(cd->dev, DL_INFO,
  1715. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1716. __func__, length + 2, hid_output->command_code);
  1717. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1718. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1719. if (rc)
  1720. pt_debug(cd->dev, DL_ERROR,
  1721. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1722. if (cmd_allocated)
  1723. kfree(cmd);
  1724. return rc;
  1725. }
  1726. /*******************************************************************************
  1727. * FUNCTION: pt_pip1_send_output_and_wait_
  1728. *
  1729. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1730. *
  1731. * RETURN:
  1732. * 0 = success
  1733. * !0 = failure
  1734. *
  1735. * PARAMETERS:
  1736. * *cd - pointer to core data
  1737. * *hid_output - pointer to the command to send
  1738. ******************************************************************************/
  1739. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1740. struct pt_hid_output *hid_output)
  1741. {
  1742. int rc = 0;
  1743. int t;
  1744. u16 timeout_ms;
  1745. mutex_lock(&cd->system_lock);
  1746. cd->hid_cmd_state = hid_output->command_code + 1;
  1747. mutex_unlock(&cd->system_lock);
  1748. if (hid_output->timeout_ms)
  1749. timeout_ms = hid_output->timeout_ms;
  1750. else
  1751. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1752. rc = pt_hid_send_output_(cd, hid_output);
  1753. if (rc)
  1754. goto error;
  1755. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1756. msecs_to_jiffies(timeout_ms));
  1757. if (IS_TMO(t)) {
  1758. #ifdef TTDL_DIAGNOSTICS
  1759. cd->bus_transmit_error_count++;
  1760. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1761. #endif /* TTDL_DIAGNOSTICS */
  1762. pt_debug(cd->dev, DL_ERROR,
  1763. "%s: HID output cmd execution timed out (%dms)\n",
  1764. __func__, timeout_ms);
  1765. rc = -ETIME;
  1766. goto error;
  1767. }
  1768. if (!hid_output->novalidate)
  1769. rc = pt_hid_output_validate_response(cd, hid_output);
  1770. pt_check_command(cd, hid_output, false);
  1771. goto exit;
  1772. error:
  1773. mutex_lock(&cd->system_lock);
  1774. cd->hid_cmd_state = 0;
  1775. mutex_unlock(&cd->system_lock);
  1776. exit:
  1777. return rc;
  1778. }
  1779. /*******************************************************************************
  1780. * FUNCTION: pt_hid_output_user_cmd_
  1781. *
  1782. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1783. * to the DUT waiting for the response and loading it into the read buffer
  1784. *
  1785. * RETURN:
  1786. * 0 = success
  1787. * !0 = failure
  1788. *
  1789. * PARAMETERS:
  1790. * *cd - pointer to core data
  1791. * read_len - expected read length of the response
  1792. * *read_buf - pointer to where the response will be loaded
  1793. * write_len - length of the write buffer
  1794. * *write_buf - pointer to the write buffer
  1795. * *actual_read_len - pointer to the actual amount of data read back
  1796. ******************************************************************************/
  1797. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1798. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1799. u16 *actual_read_len)
  1800. {
  1801. int rc = 0;
  1802. u16 size;
  1803. struct pt_hid_output hid_output = {
  1804. .length = write_len,
  1805. .write_buf = write_buf,
  1806. };
  1807. #ifdef TTHE_TUNER_SUPPORT
  1808. if (!cd->pip2_send_user_cmd) {
  1809. int command_code = 0;
  1810. int len;
  1811. /* Print up to cmd ID */
  1812. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1813. if (write_len < len)
  1814. len = write_len;
  1815. else
  1816. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1817. & PIP1_CMD_COMMAND_ID_MASK;
  1818. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1819. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1820. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1821. tthe_print(cd, write_buf, len, "CMD=");
  1822. }
  1823. #endif
  1824. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1825. if (rc)
  1826. return rc;
  1827. /* Get the response size from the first 2 bytes in the response */
  1828. size = get_unaligned_le16(&cd->response_buf[0]);
  1829. /* Ensure size is not greater than max buffer size */
  1830. if (size > PT_MAX_PIP2_MSG_SIZE)
  1831. size = PT_MAX_PIP2_MSG_SIZE;
  1832. /* Minimum size to read is the 2 byte len field */
  1833. if (size == 0)
  1834. size = 2;
  1835. if (size > read_len) {
  1836. pt_debug(cd->dev, DL_ERROR,
  1837. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1838. __func__, size, read_len);
  1839. *actual_read_len = 0;
  1840. return -EIO;
  1841. }
  1842. memcpy(read_buf, cd->response_buf, size);
  1843. *actual_read_len = size;
  1844. return 0;
  1845. }
  1846. /*******************************************************************************
  1847. * FUNCTION: pt_hid_output_user_cmd
  1848. *
  1849. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1850. * the DUT.
  1851. *
  1852. * RETURN:
  1853. * 0 = success
  1854. * !0 = failure
  1855. *
  1856. * PARAMETERS:
  1857. * *cd - pointer to core data
  1858. * read_len - length of data to read
  1859. * *read_buf - pointer to store read data
  1860. * write_len - length of data to write
  1861. * *write_buf - pointer to buffer to write
  1862. * *actual_read_len - pointer to store data length actually read
  1863. ******************************************************************************/
  1864. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1865. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1866. u16 *actual_read_len)
  1867. {
  1868. int rc;
  1869. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1870. if (rc < 0) {
  1871. pt_debug(cd->dev, DL_ERROR,
  1872. "%s: fail get exclusive ex=%p own=%p\n",
  1873. __func__, cd->exclusive_dev, cd->dev);
  1874. return rc;
  1875. }
  1876. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1877. write_len, write_buf, actual_read_len);
  1878. if (release_exclusive(cd, cd->dev) < 0)
  1879. pt_debug(cd->dev, DL_ERROR,
  1880. "%s: fail to release exclusive\n", __func__);
  1881. return rc;
  1882. }
  1883. /*******************************************************************************
  1884. * FUNCTION: _pt_request_pip2_send_cmd
  1885. *
  1886. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1887. * interrupt and reads response data to read_buf
  1888. *
  1889. * RETURN:
  1890. * 0 = success
  1891. * !0 = failure
  1892. *
  1893. * PARAMETERS:
  1894. * *dev - pointer to device structure
  1895. * protect - flag to run in protected mode
  1896. * id - ID of PIP command
  1897. * *data - pointer to PIP data payload
  1898. * report_body_len - report length
  1899. * *read_buf - pointer to response buffer
  1900. * *actual_read_len - pointer to response buffer length
  1901. ******************************************************************************/
  1902. static int _pt_request_pip2_send_cmd(struct device *dev,
  1903. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1904. u16 *actual_read_len)
  1905. {
  1906. struct pt_core_data *cd = dev_get_drvdata(dev);
  1907. struct pip2_cmd_structure pip2_cmd;
  1908. int rc = 0;
  1909. int i = 0;
  1910. int j = 0;
  1911. u16 write_len;
  1912. u8 *write_buf = NULL;
  1913. u16 read_len;
  1914. u8 extra_bytes;
  1915. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1916. /* Hard coded register for PIP2.x */
  1917. pip2_cmd.reg[0] = 0x01;
  1918. pip2_cmd.reg[1] = 0x01;
  1919. /*
  1920. * For PIP2.1+ the length field value includes itself:
  1921. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1922. *
  1923. * The overall write length must include only the register:
  1924. * ADD 2: 2 (Register)
  1925. */
  1926. extra_bytes = 6;
  1927. write_len = 2;
  1928. /* PIP2 the CMD ID is a 7bit field */
  1929. if (id > PIP2_CMD_ID_END) {
  1930. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1931. __func__, id);
  1932. rc = -EINVAL;
  1933. goto exit;
  1934. }
  1935. pip2_cmd.len = report_body_len + extra_bytes;
  1936. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1937. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1938. pip2_cmd.data = data;
  1939. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1940. /* Add the command length to the extra bytes based on PIP version */
  1941. write_len += pip2_cmd.len;
  1942. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1943. __func__, pip2_cmd.len, write_len);
  1944. write_buf = kzalloc(write_len, GFP_KERNEL);
  1945. if (write_buf == NULL) {
  1946. rc = -ENOMEM;
  1947. goto exit;
  1948. }
  1949. write_buf[i++] = pip2_cmd.reg[0];
  1950. write_buf[i++] = pip2_cmd.reg[1];
  1951. write_buf[i++] = pip2_cmd.len & 0xff;
  1952. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1953. write_buf[i++] = pip2_cmd.seq;
  1954. write_buf[i++] = pip2_cmd.id;
  1955. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1956. write_buf[j] = pip2_cmd.data[j-i];
  1957. write_buf[j++] = pip2_cmd.crc[0];
  1958. write_buf[j++] = pip2_cmd.crc[1];
  1959. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1960. if (read_len < 0)
  1961. read_len = 255;
  1962. pt_debug(dev, DL_INFO,
  1963. "%s cmd_id[0x%02X] expected response length:%d ",
  1964. __func__, pip2_cmd.id, read_len);
  1965. /*
  1966. * All PIP2 commands come through this function.
  1967. * Set flag for PIP2.x interface to allow response parsing to know
  1968. * how to decode the protocol header.
  1969. */
  1970. mutex_lock(&cd->system_lock);
  1971. cd->pip2_prot_active = true;
  1972. cd->pip2_send_user_cmd = true;
  1973. mutex_unlock(&cd->system_lock);
  1974. if (protect == PT_CORE_CMD_PROTECTED)
  1975. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1976. write_len, write_buf, actual_read_len);
  1977. else {
  1978. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1979. write_len, write_buf, actual_read_len);
  1980. }
  1981. if (rc) {
  1982. pt_debug(dev, DL_ERROR,
  1983. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1984. __func__, rc);
  1985. goto exit;
  1986. }
  1987. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1988. *actual_read_len);
  1989. exit:
  1990. mutex_lock(&cd->system_lock);
  1991. cd->pip2_prot_active = false;
  1992. cd->pip2_send_user_cmd = false;
  1993. mutex_unlock(&cd->system_lock);
  1994. kfree(write_buf);
  1995. return rc;
  1996. }
  1997. /*******************************************************************************
  1998. * FUNCTION: _pt_pip2_send_cmd_no_int
  1999. *
  2000. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2001. * reads response data to read_buf if response is available.
  2002. *
  2003. * NOTE:
  2004. * Interrupt MUST be disabled before to call this function.
  2005. *
  2006. * RETURN:
  2007. * 0 = success
  2008. * !0 = failure
  2009. *
  2010. * PARAMETERS:
  2011. * *dev - pointer to device structure
  2012. * protect - flag to run in protected mode
  2013. * id - ID of PIP command
  2014. * *data - pointer to PIP data payload
  2015. * report_body_len - report length
  2016. * *read_buf - pointer to response buffer
  2017. * *actual_read_len - pointer to response buffer length
  2018. ******************************************************************************/
  2019. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2020. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2021. u16 *actual_read_len)
  2022. {
  2023. int max_retry = 0;
  2024. int retry = 0;
  2025. int rc = 0;
  2026. int i = 0;
  2027. int j = 0;
  2028. u16 write_len;
  2029. u8 *write_buf = NULL;
  2030. u16 read_len;
  2031. u16 size = 0;
  2032. u8 response_seq = 0;
  2033. u8 extra_bytes;
  2034. u32 retry_interval = 0;
  2035. u32 retry_total_time = 0;
  2036. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2037. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2038. struct pt_core_data *cd = dev_get_drvdata(dev);
  2039. struct pip2_cmd_structure pip2_cmd;
  2040. if (protect == PT_CORE_CMD_PROTECTED) {
  2041. rc = request_exclusive(cd,
  2042. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2043. if (rc < 0) {
  2044. pt_debug(cd->dev, DL_ERROR,
  2045. "%s: fail get exclusive ex=%p own=%p\n",
  2046. __func__, cd->exclusive_dev, cd->dev);
  2047. return rc;
  2048. }
  2049. }
  2050. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2051. /* Hard coded register for PIP2.x */
  2052. pip2_cmd.reg[0] = 0x01;
  2053. pip2_cmd.reg[1] = 0x01;
  2054. /*
  2055. * For PIP2.1+ the length field value includes itself:
  2056. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2057. *
  2058. * The overall write length must include only the register:
  2059. * ADD 2: 2 (Register)
  2060. */
  2061. extra_bytes = 6;
  2062. write_len = 2;
  2063. pip2_cmd.len = report_body_len + extra_bytes;
  2064. pip2_cmd.id = id;
  2065. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2066. pip2_cmd.data = data;
  2067. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2068. /* Add the command length to the extra bytes based on PIP version */
  2069. write_len += pip2_cmd.len;
  2070. write_buf = kzalloc(write_len, GFP_KERNEL);
  2071. if (write_buf == NULL) {
  2072. rc = -ENOMEM;
  2073. goto exit;
  2074. }
  2075. write_buf[i++] = pip2_cmd.reg[0];
  2076. write_buf[i++] = pip2_cmd.reg[1];
  2077. write_buf[i++] = pip2_cmd.len & 0xff;
  2078. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2079. write_buf[i++] = pip2_cmd.seq;
  2080. write_buf[i++] = pip2_cmd.id;
  2081. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2082. write_buf[j] = pip2_cmd.data[j-i];
  2083. write_buf[j++] = pip2_cmd.crc[0];
  2084. write_buf[j++] = pip2_cmd.crc[1];
  2085. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2086. if (read_len < 0)
  2087. read_len = 255;
  2088. pt_debug(dev, DL_INFO,
  2089. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2090. __func__, pip2_cmd.id, read_len);
  2091. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2092. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2093. if (rc) {
  2094. pt_debug(dev, DL_ERROR,
  2095. "%s: SPI write Error = %d\n",
  2096. __func__, rc);
  2097. goto exit;
  2098. }
  2099. #ifdef PT_POLL_RESP_BY_BUS
  2100. /*
  2101. * Frequent bus read can increase system load obviously. The expected
  2102. * first bus read should be valid and timely. The tollerance for
  2103. * usleep_range should be limited. The minimum response delay (between
  2104. * command finishes sending and INT pin falls) is less than 50
  2105. * microseconds. So the 10 microseconds should be maximum tollerance
  2106. * with the consideration that the unit to calculate the response delay
  2107. * is 10 microseconds and more precise is not necessary. Every
  2108. * additional 10 microseconds only contribute less than 3 milliseconds
  2109. * for whole BL.
  2110. */
  2111. usleep_range(resp_time_min, resp_time_min+10);
  2112. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2113. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2114. rc = pt_adap_read_default(cd, read_buf, read_len);
  2115. if (rc) {
  2116. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2117. __func__, rc);
  2118. break;
  2119. }
  2120. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2121. size = get_unaligned_le16(&read_buf[0]);
  2122. if ((size <= read_len) &&
  2123. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2124. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2125. break;
  2126. }
  2127. /*
  2128. * To reduce the bus and system load, increase the sleep
  2129. * step gradually:
  2130. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2131. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2132. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2133. */
  2134. retry++;
  2135. if (retry < 20) {
  2136. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2137. usleep_range(retry_interval,
  2138. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2139. } else if (retry < 40) {
  2140. retry_interval += 1000;
  2141. usleep_range(retry_interval, retry_interval + 1000);
  2142. } else {
  2143. retry_interval += 50000;
  2144. msleep(retry_interval/1000);
  2145. }
  2146. retry_total_time += retry_interval;
  2147. }
  2148. #else
  2149. /*
  2150. * Frequent GPIO read will not increase CPU/system load heavily if the
  2151. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2152. * fixed interval: 20 us.
  2153. */
  2154. usleep_range(resp_time_min, resp_time_min+10);
  2155. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2156. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2157. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2158. rc = pt_adap_read_default(cd, read_buf, read_len);
  2159. size = get_unaligned_le16(&read_buf[0]);
  2160. if (rc)
  2161. pt_debug(dev, DL_ERROR,
  2162. "%s: SPI read Error = %d\n",
  2163. __func__, rc);
  2164. else if (size > read_len) {
  2165. pt_debug(cd->dev, DL_ERROR,
  2166. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2167. __func__, size, read_len);
  2168. rc = -EIO;
  2169. }
  2170. break;
  2171. }
  2172. /*
  2173. * Poll GPIO with fixed interval 20 us, and tollerance is
  2174. * limited to 10 us to speed up the process.
  2175. */
  2176. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2177. usleep_range(retry_interval, retry_interval+10);
  2178. retry_total_time += retry_interval;
  2179. }
  2180. #endif
  2181. *actual_read_len = size;
  2182. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2183. pt_debug(dev, DL_ERROR,
  2184. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2185. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2186. *actual_read_len = 0;
  2187. rc = -EINVAL;
  2188. }
  2189. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2190. "<<< NO_INT Read");
  2191. exit:
  2192. kfree(write_buf);
  2193. if (protect == PT_CORE_CMD_PROTECTED) {
  2194. if (release_exclusive(cd, cd->dev) < 0)
  2195. pt_debug(cd->dev, DL_ERROR,
  2196. "%s: fail to release exclusive\n", __func__);
  2197. }
  2198. return rc;
  2199. }
  2200. /*******************************************************************************
  2201. * FUNCTION: pt_pip_null_
  2202. *
  2203. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2204. * This function is used by watchdog to check if the fw corrupts.
  2205. *
  2206. * RETURN:
  2207. * 0 = success
  2208. * !0 = failure
  2209. *
  2210. * PARAMETERS:
  2211. * *cd - pointer to core data
  2212. ******************************************************************************/
  2213. static int pt_pip_null_(struct pt_core_data *cd)
  2214. {
  2215. struct pt_hid_output hid_output = {
  2216. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2217. };
  2218. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2219. }
  2220. /*******************************************************************************
  2221. * FUNCTION: pt_pip_null
  2222. *
  2223. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2224. *
  2225. * RETURN:
  2226. * 0 = success
  2227. * !0 = failure
  2228. *
  2229. * PARAMETERS:
  2230. * *cd - pointer to core data
  2231. ******************************************************************************/
  2232. static int pt_pip_null(struct pt_core_data *cd)
  2233. {
  2234. int rc;
  2235. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2236. if (rc < 0) {
  2237. pt_debug(cd->dev, DL_ERROR,
  2238. "%s: fail get exclusive ex=%p own=%p\n",
  2239. __func__, cd->exclusive_dev, cd->dev);
  2240. return rc;
  2241. }
  2242. rc = pt_pip_null_(cd);
  2243. if (release_exclusive(cd, cd->dev) < 0)
  2244. pt_debug(cd->dev, DL_ERROR,
  2245. "%s: fail to release exclusive\n", __func__);
  2246. return rc;
  2247. }
  2248. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2249. /*******************************************************************************
  2250. * FUNCTION: pt_pip_start_bootloader_
  2251. *
  2252. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2253. *
  2254. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2255. * the WD active could cause this function to fail!
  2256. *
  2257. * RETURN:
  2258. * 0 = success
  2259. * !0 = failure
  2260. *
  2261. * PARAMETERS:
  2262. * *cd - pointer to core data
  2263. ******************************************************************************/
  2264. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2265. {
  2266. int rc = 0;
  2267. struct pt_hid_output hid_output = {
  2268. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2269. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2270. .reset_expected = 1,
  2271. };
  2272. if (cd->watchdog_enabled) {
  2273. pt_debug(cd->dev, DL_WARN,
  2274. "%s: watchdog isn't stopped before enter bl\n",
  2275. __func__);
  2276. goto exit;
  2277. }
  2278. /* Reset startup status after entering BL, new DUT enum required */
  2279. cd->startup_status = STARTUP_STATUS_START;
  2280. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2281. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2282. if (rc) {
  2283. pt_debug(cd->dev, DL_ERROR,
  2284. "%s: Start BL PIP cmd failed. rc = %d\n",
  2285. __func__, rc);
  2286. }
  2287. exit:
  2288. return rc;
  2289. }
  2290. /*******************************************************************************
  2291. * FUNCTION: pt_pip_start_bootloader
  2292. *
  2293. * SUMMARY: Protected function to force DUT to enter the BL
  2294. *
  2295. * RETURN:
  2296. * 0 = success
  2297. * !0 = failure
  2298. *
  2299. * PARAMETERS:
  2300. * *cd - pointer to core data structure
  2301. ******************************************************************************/
  2302. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2303. {
  2304. int rc;
  2305. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2306. if (rc < 0) {
  2307. pt_debug(cd->dev, DL_ERROR,
  2308. "%s: fail get exclusive ex=%p own=%p\n",
  2309. __func__, cd->exclusive_dev, cd->dev);
  2310. return rc;
  2311. }
  2312. rc = pt_pip_start_bootloader_(cd);
  2313. if (release_exclusive(cd, cd->dev) < 0)
  2314. pt_debug(cd->dev, DL_ERROR,
  2315. "%s: fail to release exclusive\n", __func__);
  2316. return rc;
  2317. }
  2318. /*******************************************************************************
  2319. * FUNCTION: _pt_request_pip_start_bl
  2320. *
  2321. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2322. * modules to request the DUT to enter the BL
  2323. *
  2324. * RETURN:
  2325. * 0 = success
  2326. * !0 = failure
  2327. *
  2328. * PARAMETERS:
  2329. * *dev - pointer to device structure
  2330. * protect - flag to run in protected mode
  2331. ******************************************************************************/
  2332. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2333. {
  2334. struct pt_core_data *cd = dev_get_drvdata(dev);
  2335. if (protect)
  2336. return pt_pip_start_bootloader(cd);
  2337. return pt_pip_start_bootloader_(cd);
  2338. }
  2339. /*******************************************************************************
  2340. * FUNCTION: pt_pip2_ver_load_ttdata
  2341. *
  2342. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2343. * command into the core data struct.
  2344. *
  2345. * RETURN: n/a
  2346. *
  2347. * PARAMETERS:
  2348. * *cd - pointer to core data structure
  2349. * len - Length of data in response_buf
  2350. ******************************************************************************/
  2351. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2352. {
  2353. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2354. struct pt_pip2_version_full *full_ver;
  2355. struct pt_pip2_version *ver;
  2356. /*
  2357. * The PIP2 VERSION command can return different lengths of data.
  2358. * The additional LOT fields are included when the packet
  2359. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2360. * NOTE:
  2361. * - The FW would swap the BL and FW versions when reporting
  2362. * the small packet.
  2363. * - Sub Lot bytes 16 and 17 are reserved.
  2364. */
  2365. if (len >= 0x1D) {
  2366. full_ver = (struct pt_pip2_version_full *)
  2367. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2368. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2369. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2370. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2371. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2372. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2373. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2374. /*
  2375. * BL PIP 2.02 and greater the version fields are
  2376. * swapped
  2377. */
  2378. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2379. ttdata->chip_rev =
  2380. get_unaligned_le16(&full_ver->chip_rev);
  2381. ttdata->chip_id =
  2382. get_unaligned_le16(&full_ver->chip_id);
  2383. } else {
  2384. ttdata->chip_rev =
  2385. get_unaligned_le16(&full_ver->chip_id);
  2386. ttdata->chip_id =
  2387. get_unaligned_le16(&full_ver->chip_rev);
  2388. }
  2389. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2390. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2391. sizeof(struct pt_pip2_version_full),
  2392. "PIP2 VERSION FULL");
  2393. } else {
  2394. ver = (struct pt_pip2_version *)
  2395. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2396. ttdata->pip_ver_major = ver->pip2_version_msb;
  2397. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2398. ttdata->bl_ver_major = ver->bl_version_msb;
  2399. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2400. ttdata->fw_ver_major = ver->fw_version_msb;
  2401. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2402. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2403. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2404. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2405. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2406. }
  2407. }
  2408. /*******************************************************************************
  2409. * FUNCTION: pt_si_get_ttdata
  2410. *
  2411. * SUMMARY: Function to load the version information from the system information
  2412. * PIP command into the core data struct.
  2413. *
  2414. * RETURN: n/a
  2415. *
  2416. * PARAMETERS:
  2417. * *cd - pointer to core data structure
  2418. ******************************************************************************/
  2419. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2420. {
  2421. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2422. struct pt_ttdata_dev *ttdata_dev =
  2423. (struct pt_ttdata_dev *)
  2424. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2425. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2426. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2427. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2428. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2429. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2430. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2431. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2432. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2433. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2434. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2435. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2436. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2437. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2438. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2439. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2440. }
  2441. /*******************************************************************************
  2442. * FUNCTION: pt_si_get_sensing_conf_data
  2443. *
  2444. * SUMMARY: Function to load the sensing information from the system information
  2445. * PIP command into the core data struct.
  2446. *
  2447. * RETURN: n/a
  2448. *
  2449. * PARAMETERS:
  2450. * *cd - pointer to core data structure
  2451. ******************************************************************************/
  2452. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2453. {
  2454. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2455. struct pt_sensing_conf_data_dev *scd_dev =
  2456. (struct pt_sensing_conf_data_dev *)
  2457. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2458. scd->electrodes_x = scd_dev->electrodes_x;
  2459. scd->electrodes_y = scd_dev->electrodes_y;
  2460. scd->origin_x = scd_dev->origin_x;
  2461. scd->origin_y = scd_dev->origin_y;
  2462. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2463. if (scd->origin_x & 0x02) {
  2464. scd->tx_num = scd->electrodes_x;
  2465. scd->rx_num = scd->electrodes_y;
  2466. } else {
  2467. scd->tx_num = scd->electrodes_y;
  2468. scd->rx_num = scd->electrodes_x;
  2469. }
  2470. /*
  2471. * When the Panel ID is coming from an XY pin and not a dedicated
  2472. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2473. * other DUTs as the loader will use cd->pid_for_loader to generate
  2474. * the bin file name but will ignore it if pid_for_loader is still
  2475. * set to PANEL_ID_NOT_ENABLED
  2476. */
  2477. if (cd->panel_id_support &
  2478. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2479. mutex_lock(&cd->system_lock);
  2480. cd->pid_for_loader = scd_dev->panel_id;
  2481. mutex_unlock(&cd->system_lock);
  2482. }
  2483. scd->panel_id = scd_dev->panel_id;
  2484. scd->btn = scd_dev->btn;
  2485. scd->scan_mode = scd_dev->scan_mode;
  2486. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2487. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2488. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2489. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2490. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2491. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2492. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2493. sizeof(struct pt_sensing_conf_data_dev),
  2494. "sensing_conf_data");
  2495. }
  2496. /*******************************************************************************
  2497. * FUNCTION: pt_si_setup
  2498. *
  2499. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2500. *
  2501. * RETURN:
  2502. * 0 = success
  2503. * !0 = failure
  2504. *
  2505. * PARAMETERS:
  2506. * *cd - pointer to core data structure
  2507. ******************************************************************************/
  2508. static int pt_si_setup(struct pt_core_data *cd)
  2509. {
  2510. struct pt_sysinfo *si = &cd->sysinfo;
  2511. int max_tch = si->sensing_conf_data.max_tch;
  2512. if (!si->xy_data)
  2513. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2514. GFP_KERNEL);
  2515. if (!si->xy_data)
  2516. return -ENOMEM;
  2517. if (!si->xy_mode)
  2518. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2519. if (!si->xy_mode) {
  2520. kfree(si->xy_data);
  2521. return -ENOMEM;
  2522. }
  2523. return 0;
  2524. }
  2525. /*******************************************************************************
  2526. * FUNCTION: pt_si_get_btn_data
  2527. *
  2528. * SUMMARY: Setup the core data button information based on the response of the
  2529. * System Information PIP command.
  2530. *
  2531. * RETURN:
  2532. * 0 = success
  2533. * !0 = failure
  2534. *
  2535. * PARAMETERS:
  2536. * *cd - pointer to core data structure
  2537. ******************************************************************************/
  2538. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2539. {
  2540. struct pt_sysinfo *si = &cd->sysinfo;
  2541. int num_btns = 0;
  2542. int num_defined_keys;
  2543. u16 *key_table;
  2544. int btn;
  2545. int i;
  2546. int rc = 0;
  2547. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2548. & PIP1_SYSINFO_BTN_MASK;
  2549. size_t btn_keys_size;
  2550. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2551. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2552. if (btns & (1 << i))
  2553. num_btns++;
  2554. }
  2555. si->num_btns = num_btns;
  2556. if (num_btns) {
  2557. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2558. if (!si->btn)
  2559. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2560. if (!si->btn)
  2561. return -ENOMEM;
  2562. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2563. num_defined_keys = 0;
  2564. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2565. num_defined_keys = 0;
  2566. else
  2567. num_defined_keys = cd->cpdata->sett
  2568. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2569. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2570. key_table = (u16 *)cd->cpdata->sett
  2571. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2572. si->btn[btn].key_code = key_table[btn];
  2573. si->btn[btn].enabled = true;
  2574. }
  2575. for (; btn < num_btns; btn++) {
  2576. si->btn[btn].key_code = KEY_RESERVED;
  2577. si->btn[btn].enabled = true;
  2578. }
  2579. return rc;
  2580. }
  2581. kfree(si->btn);
  2582. si->btn = NULL;
  2583. return rc;
  2584. }
  2585. /*******************************************************************************
  2586. * FUNCTION: pt_si_put_log_data
  2587. *
  2588. * SUMMARY: Prints all sys info data to kmsg log
  2589. *
  2590. * RETURN: n/a
  2591. *
  2592. * PARAMETERS:
  2593. * *cd - pointer to core data structure
  2594. ******************************************************************************/
  2595. static void pt_si_put_log_data(struct pt_core_data *cd)
  2596. {
  2597. struct pt_sysinfo *si = &cd->sysinfo;
  2598. struct pt_ttdata *ttdata = &si->ttdata;
  2599. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2600. int i;
  2601. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2602. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2603. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2604. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2605. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2606. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2608. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2610. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2611. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2612. __func__, ttdata->revctrl, ttdata->revctrl);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2614. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2616. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2618. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2620. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2622. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2623. for (i = 0; i < PT_NUM_MFGID; i++)
  2624. pt_debug(cd->dev, DL_DEBUG,
  2625. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2626. __func__, i, ttdata->mfg_id[i],
  2627. ttdata->mfg_id[i]);
  2628. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2629. __func__, ttdata->post_code, ttdata->post_code);
  2630. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2631. __func__, scd->electrodes_x, scd->electrodes_x);
  2632. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2633. __func__, scd->electrodes_y, scd->electrodes_y);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2635. __func__, scd->len_x, scd->len_x);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2637. __func__, scd->len_y, scd->len_y);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2639. __func__, scd->res_x, scd->res_x);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2641. __func__, scd->res_y, scd->res_y);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2643. __func__, scd->max_z, scd->max_z);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2645. __func__, scd->origin_x, scd->origin_x);
  2646. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2647. __func__, scd->origin_y, scd->origin_y);
  2648. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2649. __func__, scd->panel_id, scd->panel_id);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2651. __func__, scd->btn, scd->btn);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2653. __func__, scd->scan_mode, scd->scan_mode);
  2654. pt_debug(cd->dev, DL_DEBUG,
  2655. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2656. __func__, scd->max_tch, scd->max_tch);
  2657. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2658. __func__, si->xy_mode);
  2659. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2660. __func__, si->xy_data);
  2661. }
  2662. /*******************************************************************************
  2663. * FUNCTION: pt_get_sysinfo_regs
  2664. *
  2665. * SUMMARY: Setup all the core data System information based on the response
  2666. * of the System Information PIP command.
  2667. *
  2668. * RETURN:
  2669. * 0 = success
  2670. * !0 = failure
  2671. *
  2672. * PARAMETERS:
  2673. * *cd - pointer to core data structure
  2674. ******************************************************************************/
  2675. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2676. {
  2677. struct pt_sysinfo *si = &cd->sysinfo;
  2678. int rc;
  2679. rc = pt_si_get_btn_data(cd);
  2680. if (rc < 0)
  2681. return rc;
  2682. pt_si_get_ttdata(cd);
  2683. pt_si_get_sensing_conf_data(cd);
  2684. pt_si_setup(cd);
  2685. pt_si_put_log_data(cd);
  2686. si->ready = true;
  2687. return rc;
  2688. }
  2689. /*******************************************************************************
  2690. * FUNCTION: pt_free_si_ptrs
  2691. *
  2692. * SUMMARY: Frees all memory associated with the System Information within
  2693. * core data
  2694. *
  2695. * RETURN: n/a
  2696. *
  2697. * PARAMETERS:
  2698. * *cd - pointer to core data structure
  2699. ******************************************************************************/
  2700. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2701. {
  2702. struct pt_sysinfo *si = &cd->sysinfo;
  2703. kfree(si->btn);
  2704. kfree(si->xy_mode);
  2705. kfree(si->xy_data);
  2706. }
  2707. /*******************************************************************************
  2708. * FUNCTION: pt_hid_output_get_sysinfo_
  2709. *
  2710. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2711. * response.
  2712. *
  2713. * RETURN::
  2714. * 0 = success
  2715. * !0 = failure
  2716. *
  2717. * PARAMETERS:
  2718. * *cd - pointer to core data structure
  2719. ******************************************************************************/
  2720. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2721. {
  2722. int rc = 0;
  2723. struct pt_hid_output hid_output = {
  2724. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2725. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2726. };
  2727. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2728. if (rc)
  2729. return rc;
  2730. /* Parse the sysinfo data */
  2731. rc = pt_get_sysinfo_regs(cd);
  2732. if (rc)
  2733. pt_free_si_ptrs(cd);
  2734. return rc;
  2735. }
  2736. /*******************************************************************************
  2737. * FUNCTION: pt_hid_output_get_sysinfo
  2738. *
  2739. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2740. *
  2741. * RETURN::
  2742. * 0 = success
  2743. * !0 = failure
  2744. *
  2745. * PARAMETERS:
  2746. * *cd - pointer to core data structure
  2747. ******************************************************************************/
  2748. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2749. {
  2750. int rc;
  2751. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2752. if (rc < 0) {
  2753. pt_debug(cd->dev, DL_ERROR,
  2754. "%s: fail get exclusive ex=%p own=%p\n",
  2755. __func__, cd->exclusive_dev, cd->dev);
  2756. return rc;
  2757. }
  2758. rc = pt_hid_output_get_sysinfo_(cd);
  2759. if (release_exclusive(cd, cd->dev) < 0)
  2760. pt_debug(cd->dev, DL_ERROR,
  2761. "%s: fail to release exclusive\n", __func__);
  2762. return rc;
  2763. }
  2764. /*******************************************************************************
  2765. * FUNCTION: pt_pip_suspend_scanning_
  2766. *
  2767. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2768. *
  2769. * RETURN::
  2770. * 0 = success
  2771. * !0 = failure
  2772. *
  2773. * PARAMETERS:
  2774. * *cd - pointer to core data structure
  2775. ******************************************************************************/
  2776. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2777. {
  2778. int rc = 0;
  2779. struct pt_hid_output hid_output = {
  2780. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2781. };
  2782. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2783. if (rc) {
  2784. pt_debug(cd->dev, DL_ERROR,
  2785. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2786. __func__, rc);
  2787. }
  2788. return rc;
  2789. }
  2790. /*******************************************************************************
  2791. * FUNCTION: pt_pip_suspend_scanning
  2792. *
  2793. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2794. *
  2795. * RETURN::
  2796. * 0 = success
  2797. * !0 = failure
  2798. *
  2799. * PARAMETERS:
  2800. * *cd - pointer to core data structure
  2801. ******************************************************************************/
  2802. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2803. {
  2804. int rc;
  2805. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2806. if (rc < 0) {
  2807. pt_debug(cd->dev, DL_ERROR,
  2808. "%s: fail get exclusive ex=%p own=%p\n",
  2809. __func__, cd->exclusive_dev, cd->dev);
  2810. return rc;
  2811. }
  2812. rc = pt_pip_suspend_scanning_(cd);
  2813. if (release_exclusive(cd, cd->dev) < 0)
  2814. pt_debug(cd->dev, DL_ERROR,
  2815. "%s: fail to release exclusive\n", __func__);
  2816. return rc;
  2817. }
  2818. /*******************************************************************************
  2819. * FUNCTION: _pt_request_pip_suspend_scanning
  2820. *
  2821. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2822. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2823. *
  2824. * RETURN:
  2825. * 0 = success
  2826. * !0 = failure
  2827. *
  2828. * PARAMETERS:
  2829. * *dev - pointer to device structure
  2830. * protect - 0 = call non-protected function
  2831. * 1 = call protected function
  2832. ******************************************************************************/
  2833. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2834. int protect)
  2835. {
  2836. struct pt_core_data *cd = dev_get_drvdata(dev);
  2837. if (protect)
  2838. return pt_pip_suspend_scanning(cd);
  2839. return pt_pip_suspend_scanning_(cd);
  2840. }
  2841. /*******************************************************************************
  2842. * FUNCTION: pt_pip_resume_scanning_
  2843. *
  2844. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2845. *
  2846. * RETURN::
  2847. * 0 = success
  2848. * !0 = failure
  2849. *
  2850. * PARAMETERS:
  2851. * *cd - pointer to core data structure
  2852. ******************************************************************************/
  2853. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2854. {
  2855. int rc = 0;
  2856. struct pt_hid_output hid_output = {
  2857. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2858. };
  2859. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2860. if (rc) {
  2861. pt_debug(cd->dev, DL_ERROR,
  2862. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2863. __func__, rc);
  2864. }
  2865. return rc;
  2866. }
  2867. /*******************************************************************************
  2868. * FUNCTION: pt_pip_resume_scanning
  2869. *
  2870. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2871. *
  2872. * RETURN::
  2873. * 0 = success
  2874. * !0 = failure
  2875. *
  2876. * PARAMETERS:
  2877. * *cd - pointer to core data structure
  2878. ******************************************************************************/
  2879. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2880. {
  2881. int rc;
  2882. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2883. if (rc < 0) {
  2884. pt_debug(cd->dev, DL_ERROR,
  2885. "%s: fail get exclusive ex=%p own=%p\n",
  2886. __func__, cd->exclusive_dev, cd->dev);
  2887. return rc;
  2888. }
  2889. rc = pt_pip_resume_scanning_(cd);
  2890. if (release_exclusive(cd, cd->dev) < 0)
  2891. pt_debug(cd->dev, DL_ERROR,
  2892. "%s: fail to release exclusive\n", __func__);
  2893. return rc;
  2894. }
  2895. /*******************************************************************************
  2896. * FUNCTION: _pt_request_pip_resume_scanning
  2897. *
  2898. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2899. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2900. *
  2901. * RETURN::
  2902. * 0 = success
  2903. * !0 = failure
  2904. *
  2905. * PARAMETERS:
  2906. * *dev - pointer to device structure
  2907. * protect - 0 = call non-protected function
  2908. * 1 = call protected function
  2909. ******************************************************************************/
  2910. static int _pt_request_pip_resume_scanning(struct device *dev,
  2911. int protect)
  2912. {
  2913. struct pt_core_data *cd = dev_get_drvdata(dev);
  2914. if (protect)
  2915. return pt_pip_resume_scanning(cd);
  2916. return pt_pip_resume_scanning_(cd);
  2917. }
  2918. /*******************************************************************************
  2919. * FUNCTION: pt_pip_get_param_
  2920. *
  2921. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2922. * the 32bit parameter value
  2923. *
  2924. * RETURN:
  2925. * 0 = success
  2926. * !0 = failure
  2927. *
  2928. * PARAMETERS:
  2929. * *cd - pointer to core data
  2930. * param_id - parameter ID to retrieve
  2931. * *value - value of DUT parameter
  2932. ******************************************************************************/
  2933. static int pt_pip_get_param_(struct pt_core_data *cd,
  2934. u8 param_id, u32 *value)
  2935. {
  2936. int write_length = 1;
  2937. u8 param[1] = { param_id };
  2938. u8 read_param_id;
  2939. int param_size;
  2940. u8 *ptr;
  2941. int rc = 0;
  2942. int i;
  2943. struct pt_hid_output hid_output = {
  2944. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2945. .write_length = write_length,
  2946. .write_buf = param,
  2947. };
  2948. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2949. if (rc)
  2950. return rc;
  2951. read_param_id = cd->response_buf[5];
  2952. if (read_param_id != param_id)
  2953. return -EPROTO;
  2954. param_size = cd->response_buf[6];
  2955. ptr = &cd->response_buf[7];
  2956. *value = 0;
  2957. for (i = 0; i < param_size; i++)
  2958. *value += ptr[i] << (i * 8);
  2959. return 0;
  2960. }
  2961. /*******************************************************************************
  2962. * FUNCTION: pt_pip_get_param
  2963. *
  2964. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2965. *
  2966. * RETURN:
  2967. * 0 = success
  2968. * !0 = failure
  2969. *
  2970. * PARAMETERS:
  2971. * *cd - pointer to core data
  2972. * param_id - parameter ID to retrieve
  2973. * *value - value of DUT parameter
  2974. ******************************************************************************/
  2975. static int pt_pip_get_param(struct pt_core_data *cd,
  2976. u8 param_id, u32 *value)
  2977. {
  2978. int rc;
  2979. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2980. if (rc < 0) {
  2981. pt_debug(cd->dev, DL_ERROR,
  2982. "%s: fail get exclusive ex=%p own=%p\n",
  2983. __func__, cd->exclusive_dev, cd->dev);
  2984. return rc;
  2985. }
  2986. rc = pt_pip_get_param_(cd, param_id, value);
  2987. if (release_exclusive(cd, cd->dev) < 0)
  2988. pt_debug(cd->dev, DL_ERROR,
  2989. "%s: fail to release exclusive\n", __func__);
  2990. return rc;
  2991. }
  2992. /*******************************************************************************
  2993. * FUNCTION: _pt_request_pip_get_param
  2994. *
  2995. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2996. * calls to the protected or unprotected call to pt_pip_get_param
  2997. *
  2998. * RETURN:
  2999. * 0 = success
  3000. * !0 = failure
  3001. *
  3002. * PARAMETERS:
  3003. * *dev - pointer to device structure
  3004. * protect - flag to call protected or non protected function
  3005. * param_id - parameter ID to retrieve
  3006. * *value - value of DUT parameter
  3007. ******************************************************************************/
  3008. int _pt_request_pip_get_param(struct device *dev,
  3009. int protect, u8 param_id, u32 *value)
  3010. {
  3011. struct pt_core_data *cd = dev_get_drvdata(dev);
  3012. if (protect)
  3013. return pt_pip_get_param(cd, param_id, value);
  3014. return pt_pip_get_param_(cd, param_id, value);
  3015. }
  3016. /*******************************************************************************
  3017. * FUNCTION: pt_pip_set_param_
  3018. *
  3019. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3020. * passed in value to flash
  3021. *
  3022. * RETURN:
  3023. * 0 = success
  3024. * !0 = failure
  3025. *
  3026. * PARAMETERS:
  3027. * *cd - pointer to core data
  3028. * param_id - parameter ID to set
  3029. * value - value to write
  3030. * size - size to write
  3031. ******************************************************************************/
  3032. static int pt_pip_set_param_(struct pt_core_data *cd,
  3033. u8 param_id, u32 value, u8 size)
  3034. {
  3035. u8 write_buf[6];
  3036. u8 *ptr = &write_buf[2];
  3037. int rc = 0;
  3038. int i;
  3039. struct pt_hid_output hid_output = {
  3040. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3041. .write_buf = write_buf,
  3042. };
  3043. write_buf[0] = param_id;
  3044. write_buf[1] = size;
  3045. for (i = 0; i < size; i++) {
  3046. ptr[i] = value & 0xFF;
  3047. value = value >> 8;
  3048. }
  3049. hid_output.write_length = 2 + size;
  3050. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3051. if (rc)
  3052. return rc;
  3053. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3054. return -EPROTO;
  3055. return 0;
  3056. }
  3057. /*******************************************************************************
  3058. * FUNCTION: pt_pip_set_param
  3059. *
  3060. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3061. *
  3062. * RETURN:
  3063. * 0 = success
  3064. * !0 = failure
  3065. *
  3066. * PARAMETERS:
  3067. * *cd - pointer to core data
  3068. * param_id - parameter ID to set
  3069. * value - value to write
  3070. * size - size to write
  3071. ******************************************************************************/
  3072. static int pt_pip_set_param(struct pt_core_data *cd,
  3073. u8 param_id, u32 value, u8 size)
  3074. {
  3075. int rc;
  3076. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3077. if (rc < 0) {
  3078. pt_debug(cd->dev, DL_ERROR,
  3079. "%s: fail get exclusive ex=%p own=%p\n",
  3080. __func__, cd->exclusive_dev, cd->dev);
  3081. return rc;
  3082. }
  3083. rc = pt_pip_set_param_(cd, param_id, value, size);
  3084. if (release_exclusive(cd, cd->dev) < 0)
  3085. pt_debug(cd->dev, DL_ERROR,
  3086. "%s: fail to release exclusive\n", __func__);
  3087. return rc;
  3088. }
  3089. /*******************************************************************************
  3090. * FUNCTION: _pt_request_pip_set_param
  3091. *
  3092. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3093. * calls to the protected or unprotected call to pt_pip_set_param
  3094. *
  3095. * RETURN:
  3096. * 0 = success
  3097. * !0 = failure
  3098. *
  3099. * PARAMETERS:
  3100. * *dev - pointer to device structure
  3101. * protect - flag to call protected or non-protected
  3102. * param_id - parameter ID to set
  3103. * value - value to write
  3104. * size - size to write
  3105. ******************************************************************************/
  3106. int _pt_request_pip_set_param(struct device *dev, int protect,
  3107. u8 param_id, u32 value, u8 size)
  3108. {
  3109. struct pt_core_data *cd = dev_get_drvdata(dev);
  3110. if (protect)
  3111. return pt_pip_set_param(cd, param_id, value, size);
  3112. return pt_pip_set_param_(cd, param_id, value, size);
  3113. }
  3114. /*******************************************************************************
  3115. * FUNCTION: _pt_pip_enter_easywake_state_
  3116. *
  3117. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3118. *
  3119. * RETURN:
  3120. * 0 = success
  3121. * !0 = failure
  3122. *
  3123. * PARAMETERS:
  3124. * *dev - pointer to device structure
  3125. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3126. * *return_data - return status if easywake was entered
  3127. ******************************************************************************/
  3128. static int pt_hid_output_enter_easywake_state_(
  3129. struct pt_core_data *cd, u8 data, u8 *return_data)
  3130. {
  3131. int write_length = 1;
  3132. u8 param[1] = { data };
  3133. int rc = 0;
  3134. struct pt_hid_output hid_output = {
  3135. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3136. .write_length = write_length,
  3137. .write_buf = param,
  3138. };
  3139. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3140. if (rc)
  3141. return rc;
  3142. *return_data = cd->response_buf[5];
  3143. return rc;
  3144. }
  3145. /*******************************************************************************
  3146. * FUNCTION: pt_pip_verify_config_block_crc_
  3147. *
  3148. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3149. *
  3150. * RETURN:
  3151. * 0 = success
  3152. * !0 = failure
  3153. *
  3154. * PARAMETERS:
  3155. * *cd - pointer the core data structure
  3156. * ebid - enumerated block ID
  3157. * *status - PIP command status
  3158. * calculated_crc - calculated CRC
  3159. * stored_crc - stored CRC in config area
  3160. ******************************************************************************/
  3161. static int pt_pip_verify_config_block_crc_(
  3162. struct pt_core_data *cd, u8 ebid, u8 *status,
  3163. u16 *calculated_crc, u16 *stored_crc)
  3164. {
  3165. int write_length = 1;
  3166. u8 param[1] = { ebid };
  3167. u8 *ptr;
  3168. int rc = 0;
  3169. struct pt_hid_output hid_output = {
  3170. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3171. .write_length = write_length,
  3172. .write_buf = param,
  3173. };
  3174. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3175. if (rc)
  3176. return rc;
  3177. ptr = &cd->response_buf[5];
  3178. *status = ptr[0];
  3179. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3180. *stored_crc = get_unaligned_le16(&ptr[3]);
  3181. return 0;
  3182. }
  3183. /*******************************************************************************
  3184. * FUNCTION: pt_pip_verify_config_block_crc
  3185. *
  3186. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3187. * an exclusive access to the DUT.
  3188. *
  3189. * RETURN:
  3190. * 0 = success
  3191. * !0 = failure
  3192. *
  3193. * PARAMETERS:
  3194. * *cd - pointer the core data structure
  3195. * ebid - enumerated block ID
  3196. * *status - PIP command status
  3197. * calculated_crc - calculated CRC
  3198. * stored_crc - stored CRC in config area
  3199. ******************************************************************************/
  3200. static int pt_pip_verify_config_block_crc(
  3201. struct pt_core_data *cd, u8 ebid, u8 *status,
  3202. u16 *calculated_crc, u16 *stored_crc)
  3203. {
  3204. int rc;
  3205. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3206. if (rc < 0) {
  3207. pt_debug(cd->dev, DL_ERROR,
  3208. "%s: fail get exclusive ex=%p own=%p\n",
  3209. __func__, cd->exclusive_dev, cd->dev);
  3210. return rc;
  3211. }
  3212. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3213. calculated_crc, stored_crc);
  3214. if (release_exclusive(cd, cd->dev) < 0)
  3215. pt_debug(cd->dev, DL_ERROR,
  3216. "%s: fail to release exclusive\n", __func__);
  3217. return rc;
  3218. }
  3219. /*******************************************************************************
  3220. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3221. *
  3222. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3223. * calls to the protected or unprotected call to
  3224. * pt_pip_verify_config_block_crc_
  3225. *
  3226. * RETURN:
  3227. * 0 = success
  3228. * !0 = failure
  3229. *
  3230. * PARAMETERS:
  3231. * *dev - pointer to device structure
  3232. * protect - flag to call protected or non-protected
  3233. * ebid - enumerated block ID
  3234. * *status - PIP command status
  3235. * calculated_crc - calculated CRC
  3236. * stored_crc - stored CRC in config area
  3237. ******************************************************************************/
  3238. static int _pt_request_pip_verify_config_block_crc(
  3239. struct device *dev, int protect, u8 ebid, u8 *status,
  3240. u16 *calculated_crc, u16 *stored_crc)
  3241. {
  3242. struct pt_core_data *cd = dev_get_drvdata(dev);
  3243. if (protect)
  3244. return pt_pip_verify_config_block_crc(cd, ebid,
  3245. status, calculated_crc, stored_crc);
  3246. return pt_pip_verify_config_block_crc_(cd, ebid,
  3247. status, calculated_crc, stored_crc);
  3248. }
  3249. /*******************************************************************************
  3250. * FUNCTION: pt_pip_get_config_row_size_
  3251. *
  3252. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3253. *
  3254. * RETURN:
  3255. * 0 = success
  3256. * !0 = failure
  3257. *
  3258. * PARAMETERS:
  3259. * *cd - pointer to core data
  3260. * protect - flag to call protected or non-protected
  3261. * *row_size - pointer to store the retrieved row size
  3262. ******************************************************************************/
  3263. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3264. u16 *row_size)
  3265. {
  3266. int rc = 0;
  3267. struct pt_hid_output hid_output = {
  3268. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3269. };
  3270. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3271. if (rc)
  3272. return rc;
  3273. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3274. return 0;
  3275. }
  3276. /*******************************************************************************
  3277. * FUNCTION: pt_pip_get_config_row_size
  3278. *
  3279. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3280. * an exclusive access to the DUT.
  3281. *
  3282. * RETURN:
  3283. * 0 = success
  3284. * !0 = failure
  3285. *
  3286. * PARAMETERS:
  3287. * *cd - pointer to core data
  3288. * protect - flag to call protected or non-protected
  3289. * *row_size - pointer to store the retrieved row size
  3290. ******************************************************************************/
  3291. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3292. u16 *row_size)
  3293. {
  3294. int rc;
  3295. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3296. if (rc < 0) {
  3297. pt_debug(cd->dev, DL_ERROR,
  3298. "%s: fail get exclusive ex=%p own=%p\n",
  3299. __func__, cd->exclusive_dev, cd->dev);
  3300. return rc;
  3301. }
  3302. rc = pt_pip_get_config_row_size_(cd, row_size);
  3303. if (release_exclusive(cd, cd->dev) < 0)
  3304. pt_debug(cd->dev, DL_ERROR,
  3305. "%s: fail to release exclusive\n", __func__);
  3306. return rc;
  3307. }
  3308. /*******************************************************************************
  3309. * FUNCTION: _pt_request_pip_get_config_row_size
  3310. *
  3311. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3312. * calls to the protected or unprotected call to
  3313. * pt_pip_get_config_row_size_
  3314. *
  3315. * RETURN:
  3316. * 0 = success
  3317. * !0 = failure
  3318. *
  3319. * PARAMETERS:
  3320. * *dev - pointer to device structure
  3321. * protect - flag to call protected or non-protected
  3322. * *row_size - pointer to store the retrieved row size
  3323. ******************************************************************************/
  3324. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3325. int protect, u16 *row_size)
  3326. {
  3327. struct pt_core_data *cd = dev_get_drvdata(dev);
  3328. if (protect)
  3329. return pt_pip_get_config_row_size(cd, row_size);
  3330. return pt_pip_get_config_row_size_(cd, row_size);
  3331. }
  3332. /*******************************************************************************
  3333. * FUNCTION: pt_pip1_read_data_block_
  3334. *
  3335. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3336. * output data to the "read_buf" and update "crc".
  3337. *
  3338. * RETURN:
  3339. * 0 = success
  3340. * !0 = failure
  3341. *
  3342. * PARAMETERS:
  3343. * *cd - pointer to core data
  3344. * row_number - row number
  3345. * length - length of data to read
  3346. * ebid - block id
  3347. * *actual_read_len - Actual data length read
  3348. * *read_buf - pointer to the buffer to store read data
  3349. * read_buf_size - size of read_buf
  3350. * *crc - pointer to store CRC of row data
  3351. ******************************************************************************/
  3352. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3353. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3354. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3355. {
  3356. int read_ebid;
  3357. int status;
  3358. int rc = 0;
  3359. int write_length = 5;
  3360. u8 write_buf[5];
  3361. u8 cmd_offset = 0;
  3362. u16 calc_crc;
  3363. struct pt_hid_output hid_output = {
  3364. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3365. .write_length = write_length,
  3366. .write_buf = write_buf,
  3367. };
  3368. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3369. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3370. write_buf[cmd_offset++] = LOW_BYTE(length);
  3371. write_buf[cmd_offset++] = HI_BYTE(length);
  3372. write_buf[cmd_offset++] = ebid;
  3373. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3374. if (rc)
  3375. return rc;
  3376. status = cd->response_buf[5];
  3377. if (status)
  3378. return status;
  3379. read_ebid = cd->response_buf[6];
  3380. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3381. return -EPROTO;
  3382. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3383. if (length == 0 || *actual_read_len == 0)
  3384. return 0;
  3385. if (read_buf_size >= *actual_read_len)
  3386. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3387. else
  3388. return -EPROTO;
  3389. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3390. /* Validate Row Data CRC */
  3391. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3392. if (*crc == calc_crc) {
  3393. return 0;
  3394. } else {
  3395. pt_debug(cd->dev, DL_ERROR,
  3396. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3397. __func__, *crc, calc_crc);
  3398. return -EPROTO;
  3399. }
  3400. }
  3401. /*******************************************************************************
  3402. * FUNCTION: _pt_request_pip_read_data_block
  3403. *
  3404. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3405. * calls to pt_pip1_read_data_block_
  3406. *
  3407. * RETURN:
  3408. * 0 = success
  3409. * !0 = failure
  3410. *
  3411. * PARAMETERS:
  3412. * *dev - pointer to device structure
  3413. * row_number - row number
  3414. * length - length of data to read
  3415. * ebid - block id
  3416. * *actual_read_len - Actual data length read
  3417. * *read_buf - pointer to the buffer to store read data
  3418. * *crc - pointer to store CRC of row data
  3419. ******************************************************************************/
  3420. static int _pt_request_pip_read_data_block(struct device *dev,
  3421. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3422. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3423. {
  3424. struct pt_core_data *cd = dev_get_drvdata(dev);
  3425. return pt_pip1_read_data_block_(cd, row_number, length,
  3426. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3427. }
  3428. /*******************************************************************************
  3429. * FUNCTION: pt_pip1_write_data_block_
  3430. *
  3431. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3432. * write data to the data block.
  3433. *
  3434. * RETURN:
  3435. * 0 = success
  3436. * !0 = failure
  3437. *
  3438. * PARAMETERS:
  3439. * *cd - pointer to core data
  3440. * row_number - row in config block to write to
  3441. * write_length - length of data to write
  3442. * ebid - enumerated block ID
  3443. * *write_buf - pointer to buffer to write
  3444. * *security_key - pointer to security key to allow write
  3445. * *actual_write_len - pointer to store data length actually written
  3446. ******************************************************************************/
  3447. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3448. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3449. u8 *security_key, u16 *actual_write_len)
  3450. {
  3451. /* row_number + write_len + ebid + security_key + crc */
  3452. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3453. u8 *full_write_buf;
  3454. u8 cmd_offset = 0;
  3455. u16 crc;
  3456. int status;
  3457. int rc = 0;
  3458. int read_ebid;
  3459. u8 *data;
  3460. struct pt_hid_output hid_output = {
  3461. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3462. .write_length = full_write_length,
  3463. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3464. };
  3465. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3466. if (!full_write_buf)
  3467. return -ENOMEM;
  3468. hid_output.write_buf = full_write_buf;
  3469. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3470. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3471. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3472. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3473. full_write_buf[cmd_offset++] = ebid;
  3474. data = &full_write_buf[cmd_offset];
  3475. memcpy(data, write_buf, write_length);
  3476. cmd_offset += write_length;
  3477. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3478. cmd_offset += 8;
  3479. crc = _pt_compute_crc(data, write_length);
  3480. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3481. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3482. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3483. if (rc)
  3484. goto exit;
  3485. status = cd->response_buf[5];
  3486. if (status) {
  3487. rc = -EINVAL;
  3488. goto exit;
  3489. }
  3490. read_ebid = cd->response_buf[6];
  3491. if (read_ebid != ebid) {
  3492. rc = -EPROTO;
  3493. goto exit;
  3494. }
  3495. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3496. exit:
  3497. kfree(full_write_buf);
  3498. return rc;
  3499. }
  3500. /*******************************************************************************
  3501. * FUNCTION: _pt_request_pip_write_data_block
  3502. *
  3503. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3504. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3505. *
  3506. * RETURN:
  3507. * 0 = success
  3508. * !0 = failure
  3509. *
  3510. * PARAMETERS:
  3511. * *dev - pointer to device structure
  3512. * protect - flag to call protected or non-protected
  3513. * row_number - row in config block to write to
  3514. * write_length - length of data to write
  3515. * ebid - enumerated block ID
  3516. * *write_buf - pointer to buffer to write
  3517. * *security_key - pointer to security key to allow write
  3518. * *actual_write_len - pointer to store data length actually written
  3519. ******************************************************************************/
  3520. static int _pt_request_pip_write_data_block(struct device *dev,
  3521. u16 row_number, u16 write_length, u8 ebid,
  3522. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3523. {
  3524. struct pt_core_data *cd = dev_get_drvdata(dev);
  3525. return pt_pip1_write_data_block_(cd, row_number,
  3526. write_length, ebid, write_buf, security_key,
  3527. actual_write_len);
  3528. }
  3529. /*******************************************************************************
  3530. * FUNCTION: pt_pip_get_data_structure_
  3531. *
  3532. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3533. * returning a structure of data defined by data_id
  3534. *
  3535. * RETURN:
  3536. * 0 = success
  3537. * !0 = failure
  3538. *
  3539. * PARAMETERS:
  3540. * *cd - pointer to core data
  3541. * read_offset - read pointer offset
  3542. * read_length - length of data to read
  3543. * data_id - data ID to read
  3544. * *status - pointer to store the read response status
  3545. * *data_format - pointer to store format of data read
  3546. * *actual_read_len - pointer to store data length actually read
  3547. * *data - pointer to store data read
  3548. ******************************************************************************/
  3549. static int pt_pip_get_data_structure_(
  3550. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3551. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3552. u8 *data)
  3553. {
  3554. int rc = 0;
  3555. u16 total_read_len = 0;
  3556. u16 read_len;
  3557. u16 off_buf = 0;
  3558. u8 write_buf[5];
  3559. u8 read_data_id;
  3560. struct pt_hid_output hid_output = {
  3561. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3562. .write_length = 5,
  3563. .write_buf = write_buf,
  3564. };
  3565. again:
  3566. write_buf[0] = LOW_BYTE(read_offset);
  3567. write_buf[1] = HI_BYTE(read_offset);
  3568. write_buf[2] = LOW_BYTE(read_length);
  3569. write_buf[3] = HI_BYTE(read_length);
  3570. write_buf[4] = data_id;
  3571. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3572. if (rc)
  3573. return rc;
  3574. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3575. goto set_status;
  3576. read_data_id = cd->response_buf[6];
  3577. if (read_data_id != data_id)
  3578. return -EPROTO;
  3579. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3580. if (read_len && data) {
  3581. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3582. total_read_len += read_len;
  3583. if (read_len < read_length) {
  3584. read_offset += read_len;
  3585. off_buf += read_len;
  3586. read_length -= read_len;
  3587. goto again;
  3588. }
  3589. }
  3590. if (data_format)
  3591. *data_format = cd->response_buf[9];
  3592. if (actual_read_len)
  3593. *actual_read_len = total_read_len;
  3594. set_status:
  3595. if (status)
  3596. *status = cd->response_buf[5];
  3597. return rc;
  3598. }
  3599. /*******************************************************************************
  3600. * FUNCTION: pt_pip_get_data_structure
  3601. *
  3602. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3603. * an exclusive access to the DUT.
  3604. *
  3605. * RETURN:
  3606. * 0 = success
  3607. * !0 = failure
  3608. *
  3609. * PARAMETERS:
  3610. * *cd - pointer to core data
  3611. * read_offset - read pointer offset
  3612. * read_length - length of data to read
  3613. * data_id - data ID to read
  3614. * *status - pointer to store the read response status
  3615. * *data_format - pointer to store format of data read
  3616. * *actual_read_len - pointer to store data length actually read
  3617. * *data - pointer to store data read
  3618. ******************************************************************************/
  3619. static int pt_pip_get_data_structure(
  3620. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3621. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3622. u8 *data)
  3623. {
  3624. int rc;
  3625. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3626. if (rc < 0) {
  3627. pt_debug(cd->dev, DL_ERROR,
  3628. "%s: fail get exclusive ex=%p own=%p\n",
  3629. __func__, cd->exclusive_dev, cd->dev);
  3630. return rc;
  3631. }
  3632. rc = pt_pip_get_data_structure_(cd, read_offset,
  3633. read_length, data_id, status, data_format,
  3634. actual_read_len, data);
  3635. if (release_exclusive(cd, cd->dev) < 0)
  3636. pt_debug(cd->dev, DL_ERROR,
  3637. "%s: fail to release exclusive\n", __func__);
  3638. return rc;
  3639. }
  3640. /*******************************************************************************
  3641. * FUNCTION: _pt_request_pip_get_data_structure
  3642. *
  3643. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3644. * calls to the protected or unprotected call to
  3645. * pt_pip_get_data_structure
  3646. *
  3647. * RETURN:
  3648. * 0 = success
  3649. * !0 = failure
  3650. *
  3651. * PARAMETERS:
  3652. * *dev - pointer to device structure
  3653. * protect - flag to call protected or non-protected
  3654. * read_offset - read pointer offset
  3655. * read_length - length of data to read
  3656. * data_id - data ID to read
  3657. * *status - pointer to store the read response status
  3658. * *data_format - pointer to store format of data read
  3659. * *actual_read_len - pointer to store data length actually read
  3660. * *data - pointer to store data read
  3661. ******************************************************************************/
  3662. static int _pt_request_pip_get_data_structure(struct device *dev,
  3663. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3664. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3665. {
  3666. struct pt_core_data *cd = dev_get_drvdata(dev);
  3667. if (protect)
  3668. return pt_pip_get_data_structure(cd,
  3669. read_offset, read_length, data_id, status,
  3670. data_format, actual_read_len, data);
  3671. return pt_pip_get_data_structure_(cd,
  3672. read_offset, read_length, data_id, status,
  3673. data_format, actual_read_len, data);
  3674. }
  3675. /*******************************************************************************
  3676. * FUNCTION: _pt_manage_local_cal_data
  3677. *
  3678. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3679. * CALIBRATION data. It stores it in a local static array and can be
  3680. * cleared, loaded or used to restore the CAL data back to the running FW.
  3681. * The CAL data is read or restored by use of the PIP1 commands:
  3682. * - READ_DATA_BLOCK (0x22)
  3683. * - WRITE_DATA_BLOCK (0x23)
  3684. *
  3685. * RETURN:
  3686. * 0 = success
  3687. * !0 = failure
  3688. *
  3689. * PARAMETERS:
  3690. * *dev - pointer to device structure
  3691. * action - One of the following actions:
  3692. * - PT_CAL_DATA_SAVE
  3693. * - PT_CAL_DATA_RESTORE
  3694. * - PT_CAL_DATA_CLEAR
  3695. * - PT_CAL_DATA_SIZE
  3696. * *size - pointer to the number of bytes transferred
  3697. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3698. ******************************************************************************/
  3699. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3700. unsigned short *crc)
  3701. {
  3702. struct pt_core_data *cd = dev_get_drvdata(dev);
  3703. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3704. unsigned short calc_id_crc = 0;
  3705. static u8 *cal_cache_data;
  3706. static u16 cal_cache_len;
  3707. static unsigned short cal_cache_chip_id;
  3708. int rc = 0;
  3709. u8 *tmp_data = NULL;
  3710. u8 row_number = 0;
  3711. u8 prefix[20];
  3712. u16 cal_size = 0;
  3713. u16 transfer_size;
  3714. u16 act_trans_len = 0;
  3715. u16 byte_offset = 0;
  3716. u16 cal_blk_size;
  3717. u16 total_rows;
  3718. u16 remain_bytes;
  3719. u16 data_block_crc;
  3720. u16 buf_size = 12;
  3721. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3722. __func__, action);
  3723. switch (action) {
  3724. case PT_CAL_DATA_SAVE:
  3725. /* Read the size of the CAL block and calculate # rows */
  3726. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3727. if (!tmp_data) {
  3728. rc = -ENOMEM;
  3729. goto exit;
  3730. }
  3731. /*
  3732. * Don't check rc as doing a read size will give a false
  3733. * error on the CRC check.
  3734. */
  3735. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3736. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3737. cal_blk_size = act_trans_len;
  3738. kfree(tmp_data);
  3739. pt_debug(dev, DL_INFO,
  3740. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3741. __func__, cal_cache_len, cal_blk_size);
  3742. /* Safety net to ensure we didn't read incorrect size */
  3743. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3744. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3745. __func__);
  3746. rc = 1;
  3747. goto exit;
  3748. }
  3749. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3750. if (cal_blk_size != cal_cache_len) {
  3751. kfree(cal_cache_data);
  3752. cal_cache_data = kzalloc(cal_blk_size + 2,
  3753. GFP_KERNEL);
  3754. if (!cal_cache_data) {
  3755. rc = -ENOMEM;
  3756. goto exit;
  3757. }
  3758. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3759. __func__);
  3760. }
  3761. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3762. /* Calculate how many rows [0-n] (PIP Transactions) */
  3763. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3764. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3765. /* Add row if we have a last partial row */
  3766. if (remain_bytes > 0)
  3767. total_rows++;
  3768. pt_debug(dev, DL_INFO,
  3769. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3770. __func__, cal_blk_size, total_rows, remain_bytes);
  3771. /* Read all rows unless an error occurs */
  3772. rc = 0;
  3773. while (rc == 0 && row_number <= total_rows) {
  3774. act_trans_len = 0;
  3775. if (remain_bytes > 0 && row_number == total_rows)
  3776. transfer_size = remain_bytes;
  3777. else
  3778. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3779. rc = pt_pip1_read_data_block_(cd, row_number,
  3780. transfer_size, PT_CAL_EBID,
  3781. &act_trans_len,
  3782. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3783. &data_block_crc);
  3784. if (rc) {
  3785. /* Error occurred, exit loop */
  3786. cal_size = 0;
  3787. break;
  3788. }
  3789. pt_debug(dev, DL_INFO,
  3790. "%s: CAL read rc=%d actual read len=%d\n",
  3791. __func__, rc, act_trans_len);
  3792. byte_offset += act_trans_len;
  3793. cal_size = byte_offset;
  3794. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3795. pt_pr_buf(dev, DL_INFO,
  3796. &cal_cache_data[byte_offset - act_trans_len],
  3797. act_trans_len, prefix);
  3798. row_number++;
  3799. }
  3800. if (cal_size > 0) {
  3801. /* Save a CRC of the chip info the CAL was saved from */
  3802. calc_id_crc = crc_ccitt_calculate(
  3803. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3804. cal_cache_chip_id = calc_id_crc;
  3805. cal_cache_len = cal_size;
  3806. pt_debug(dev, DL_INFO,
  3807. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3808. __func__, calc_id_crc, cal_size);
  3809. }
  3810. *size = cal_size;
  3811. *crc = calc_id_crc;
  3812. break;
  3813. case PT_CAL_DATA_RESTORE:
  3814. cal_size = cal_cache_len;
  3815. while ((rc == 0) && (byte_offset < cal_size)) {
  3816. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3817. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3818. else
  3819. transfer_size = cal_size - byte_offset;
  3820. rc = pt_pip1_write_data_block_(cd, row_number,
  3821. transfer_size, PT_CAL_EBID,
  3822. &cal_cache_data[byte_offset],
  3823. (u8 *)pt_data_block_security_key,
  3824. &act_trans_len);
  3825. byte_offset += act_trans_len;
  3826. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3827. __func__, byte_offset);
  3828. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3829. pt_pr_buf(dev, DL_INFO,
  3830. &cal_cache_data[byte_offset - act_trans_len],
  3831. act_trans_len, prefix);
  3832. if ((byte_offset > cal_size) ||
  3833. (act_trans_len != transfer_size))
  3834. rc = -EIO;
  3835. row_number++;
  3836. }
  3837. *size = byte_offset;
  3838. *crc = cal_cache_chip_id;
  3839. break;
  3840. case PT_CAL_DATA_CLEAR:
  3841. if (cal_cache_data)
  3842. memset(&cal_cache_data[0], 0, cal_cache_len);
  3843. cal_cache_len = 0;
  3844. cal_cache_chip_id = 0;
  3845. *size = 0;
  3846. *crc = 0;
  3847. break;
  3848. case PT_CAL_DATA_INFO:
  3849. default:
  3850. *size = cal_cache_len;
  3851. *crc = cal_cache_chip_id;
  3852. pt_debug(dev, DL_INFO,
  3853. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3854. __func__, cal_cache_chip_id,
  3855. cal_cache_len);
  3856. break;
  3857. }
  3858. exit:
  3859. pt_debug(dev, DL_INFO,
  3860. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3861. __func__, rc, *crc, *size);
  3862. return rc;
  3863. }
  3864. /*******************************************************************************
  3865. * FUNCTION: pt_pip_run_selftest_
  3866. *
  3867. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3868. * to execute a FW built in self test
  3869. *
  3870. * RETURN:
  3871. * 0 = success
  3872. * !0 = failure
  3873. *
  3874. * PARAMETERS:
  3875. * *cd - pointer to core data
  3876. * test_id - enumerated test ID to run
  3877. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3878. * *status - pointer to store the read response status
  3879. * *summary_results - pointer to store the results summary
  3880. * *results_available - pointer to store if results are available
  3881. *****************************************************************************/
  3882. static int pt_pip_run_selftest_(
  3883. struct pt_core_data *cd, u8 test_id,
  3884. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3885. u8 *results_available)
  3886. {
  3887. int rc = 0;
  3888. u8 write_buf[2];
  3889. struct pt_hid_output hid_output = {
  3890. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3891. .write_length = 2,
  3892. .write_buf = write_buf,
  3893. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3894. };
  3895. write_buf[0] = test_id;
  3896. write_buf[1] = write_idacs_to_flash;
  3897. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3898. hid_output.write_length = 1;
  3899. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3900. if (rc)
  3901. return rc;
  3902. if (status)
  3903. *status = cd->response_buf[5];
  3904. if (summary_result)
  3905. *summary_result = cd->response_buf[6];
  3906. /* results_available only available before PIP 1.03 */
  3907. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3908. if (results_available)
  3909. *results_available = cd->response_buf[7];
  3910. }
  3911. return rc;
  3912. }
  3913. /*******************************************************************************
  3914. * FUNCTION: pt_pip_run_selftest
  3915. *
  3916. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3917. * an exclusive access to the DUT.
  3918. *
  3919. * RETURN:
  3920. * 0 = success
  3921. * !0 = failure
  3922. *
  3923. * PARAMETERS:
  3924. * *cd - pointer to core data
  3925. * test_id - enumerated test ID to run
  3926. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3927. * *status - pointer to store the read response status
  3928. * *summary_results - pointer to store the results summary
  3929. * *results_available - pointer to store if results are available
  3930. ******************************************************************************/
  3931. static int pt_pip_run_selftest(
  3932. struct pt_core_data *cd, u8 test_id,
  3933. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3934. u8 *results_available)
  3935. {
  3936. int rc;
  3937. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3938. if (rc < 0) {
  3939. pt_debug(cd->dev, DL_ERROR,
  3940. "%s: fail get exclusive ex=%p own=%p\n",
  3941. __func__, cd->exclusive_dev, cd->dev);
  3942. return rc;
  3943. }
  3944. rc = pt_pip_run_selftest_(cd, test_id,
  3945. write_idacs_to_flash, status, summary_result,
  3946. results_available);
  3947. if (release_exclusive(cd, cd->dev) < 0)
  3948. pt_debug(cd->dev, DL_ERROR,
  3949. "%s: fail to release exclusive\n", __func__);
  3950. return rc;
  3951. }
  3952. /*******************************************************************************
  3953. * FUNCTION: _pt_request_pip_run_selftest
  3954. *
  3955. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3956. * calls to the protected or unprotected call to pt_pip_run_selftest
  3957. *
  3958. * RETURN:
  3959. * 0 = success
  3960. * !0 = failure
  3961. *
  3962. * PARAMETERS:
  3963. * *dev - pointer to device structure
  3964. * protect - flag to call protected or non-protected
  3965. * test_id - enumerated test ID to run
  3966. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3967. * *status - pointer to store the read response status
  3968. * *summary_results - pointer to store the results summary
  3969. * *results_available - pointer to store if results are available
  3970. ******************************************************************************/
  3971. static int _pt_request_pip_run_selftest(struct device *dev,
  3972. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3973. u8 *summary_result, u8 *results_available)
  3974. {
  3975. struct pt_core_data *cd = dev_get_drvdata(dev);
  3976. if (protect)
  3977. return pt_pip_run_selftest(cd, test_id,
  3978. write_idacs_to_flash, status, summary_result,
  3979. results_available);
  3980. return pt_pip_run_selftest_(cd, test_id,
  3981. write_idacs_to_flash, status, summary_result,
  3982. results_available);
  3983. }
  3984. /*******************************************************************************
  3985. * FUNCTION: _pt_pip_get_selftest_result_
  3986. *
  3987. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3988. * to retrieve the self test results from the self test already executed
  3989. *
  3990. * RETURN:
  3991. * 0 = success
  3992. * !0 = failure
  3993. *
  3994. * PARAMETERS:
  3995. * *cd - pointer to core data
  3996. * read_offset - read pointer offset
  3997. * read_length - length of data to read
  3998. * test_id - enumerated test ID to read selftest results from
  3999. * *status - pointer to store the read response status
  4000. * *actual_read_len - pointer to store data length actually read
  4001. * *status - pointer to where the cmd response statas is stored
  4002. ******************************************************************************/
  4003. static int pt_pip_get_selftest_result_(
  4004. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4005. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4006. {
  4007. int rc = 0;
  4008. u16 total_read_len = 0;
  4009. u16 read_len;
  4010. u16 off_buf = 0;
  4011. u8 write_buf[5];
  4012. u8 read_test_id;
  4013. bool repeat;
  4014. struct pt_hid_output hid_output = {
  4015. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4016. .write_length = 5,
  4017. .write_buf = write_buf,
  4018. };
  4019. /*
  4020. * Do not repeat reading for Auto Shorts test
  4021. * when PIP version < 1.3
  4022. */
  4023. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4024. || test_id != PT_ST_ID_AUTOSHORTS;
  4025. again:
  4026. write_buf[0] = LOW_BYTE(read_offset);
  4027. write_buf[1] = HI_BYTE(read_offset);
  4028. write_buf[2] = LOW_BYTE(read_length);
  4029. write_buf[3] = HI_BYTE(read_length);
  4030. write_buf[4] = test_id;
  4031. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4032. if (rc)
  4033. return rc;
  4034. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4035. goto set_status;
  4036. read_test_id = cd->response_buf[6];
  4037. if (read_test_id != test_id)
  4038. return -EPROTO;
  4039. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4040. if (read_len && data) {
  4041. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4042. total_read_len += read_len;
  4043. if (repeat && read_len < read_length) {
  4044. read_offset += read_len;
  4045. off_buf += read_len;
  4046. read_length -= read_len;
  4047. goto again;
  4048. }
  4049. }
  4050. if (actual_read_len)
  4051. *actual_read_len = total_read_len;
  4052. set_status:
  4053. if (status)
  4054. *status = cd->response_buf[5];
  4055. return rc;
  4056. }
  4057. /*******************************************************************************
  4058. * FUNCTION: _pt_pip_get_selftest_result
  4059. *
  4060. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4061. * access to the DUT
  4062. *
  4063. * RETURN:
  4064. * 0 = success
  4065. * !0 = failure
  4066. *
  4067. * PARAMETERS:
  4068. * *cd - pointer to core data
  4069. * read_offset - read pointer offset
  4070. * read_length - length of data to read
  4071. * test_id - enumerated test ID to read selftest results from
  4072. * *status - pointer to store the read response status
  4073. * *actual_read_len - pointer to store data length actually read
  4074. * *status - pointer to where the cmd response statas is stored
  4075. ******************************************************************************/
  4076. static int pt_pip_get_selftest_result(
  4077. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4078. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4079. {
  4080. int rc;
  4081. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4082. if (rc < 0) {
  4083. pt_debug(cd->dev, DL_ERROR,
  4084. "%s: fail get exclusive ex=%p own=%p\n",
  4085. __func__, cd->exclusive_dev, cd->dev);
  4086. return rc;
  4087. }
  4088. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4089. read_length, test_id, status, actual_read_len, data);
  4090. if (release_exclusive(cd, cd->dev) < 0)
  4091. pt_debug(cd->dev, DL_ERROR,
  4092. "%s: fail to release exclusive\n", __func__);
  4093. return rc;
  4094. }
  4095. /*******************************************************************************
  4096. * FUNCTION: _pt_request_pip_get_selftest_result
  4097. *
  4098. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4099. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4100. *
  4101. * RETURN:
  4102. * 0 = success
  4103. * !0 = failure
  4104. *
  4105. * PARAMETERS:
  4106. * *dev - pointer to device structure
  4107. * protect - flag to call protected or non-protected
  4108. * read_offset - read pointer offset
  4109. * read_length - length of data to read
  4110. * test_id - enumerated test ID to read selftest results from
  4111. * *status - pointer to store the read response status
  4112. * *actual_read_len - pointer to store data length actually read
  4113. * *data - pointer to where the data read is stored
  4114. ******************************************************************************/
  4115. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4116. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4117. u8 *status, u16 *actual_read_len, u8 *data)
  4118. {
  4119. struct pt_core_data *cd = dev_get_drvdata(dev);
  4120. if (protect)
  4121. return pt_pip_get_selftest_result(cd, read_offset,
  4122. read_length, test_id, status, actual_read_len,
  4123. data);
  4124. return pt_pip_get_selftest_result_(cd, read_offset,
  4125. read_length, test_id, status, actual_read_len,
  4126. data);
  4127. }
  4128. /*******************************************************************************
  4129. * FUNCTION: _pt_pip_load_self_test_param
  4130. *
  4131. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4132. * to load parameters needed by a self test
  4133. *
  4134. * RETURN:
  4135. * 0 = success
  4136. * !0 = failure
  4137. *
  4138. * PARAMETERS:
  4139. * *cd - pointer to core data
  4140. * self_test_id - enumerated test ID for which the parmeters belong
  4141. * load_offset - mem offset to where to load parameters
  4142. * load_length - length of parameter data to load
  4143. * *parameters - pointer to list of parameter data
  4144. * *status - pointer to store the response status
  4145. * *ret_test_id - pointer to returned test id the parameters were stored
  4146. * *act_load_len - pointer to store the actual load length that was writen
  4147. ******************************************************************************/
  4148. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4149. u8 self_test_id, u16 load_offset, u16 load_length,
  4150. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4151. {
  4152. int rc = 0;
  4153. int i;
  4154. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4155. struct pt_hid_output hid_output = {
  4156. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4157. .write_length = 5 + load_length,
  4158. .write_buf = write_buf,
  4159. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4160. };
  4161. write_buf[0] = LOW_BYTE(load_offset);
  4162. write_buf[1] = HI_BYTE(load_offset);
  4163. write_buf[2] = LOW_BYTE(load_length);
  4164. write_buf[3] = HI_BYTE(load_length);
  4165. write_buf[4] = self_test_id;
  4166. for (i = 0; i < load_length; i++)
  4167. write_buf[i + 5] = parameters[i];
  4168. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4169. if (rc)
  4170. return rc;
  4171. if (status)
  4172. *status = cd->response_buf[5];
  4173. if (ret_test_id)
  4174. *ret_test_id = cd->response_buf[6];
  4175. if (act_load_len)
  4176. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4177. return rc;
  4178. }
  4179. /*******************************************************************************
  4180. * FUNCTION: pt_pip_load_self_test_param
  4181. *
  4182. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4183. * access to the DUT.
  4184. *
  4185. * RETURN:
  4186. * 0 = success
  4187. * !0 = failure
  4188. *
  4189. * PARAMETERS:
  4190. * *cd - pointer to core data
  4191. * self_test_id - enumerated test ID for which the parmeters belong
  4192. * load_offset - mem offset to where to load parameters
  4193. * load_length - length of parameter data to load
  4194. * *parameters - pointer to list of parameter data
  4195. * *status - pointer to store the response status
  4196. * *ret_test_id - pointer to returned test id the parameters were stored
  4197. * *act_load_len - pointer to store the actual load length that was writen
  4198. ******************************************************************************/
  4199. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4200. u8 self_test_id, u16 load_offset, u16 load_length,
  4201. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4202. {
  4203. int rc;
  4204. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4205. if (rc < 0) {
  4206. pt_debug(cd->dev, DL_ERROR,
  4207. "%s: fail get exclusive ex=%p own=%p\n",
  4208. __func__, cd->exclusive_dev, cd->dev);
  4209. return rc;
  4210. }
  4211. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4212. load_length, parameters, status, ret_test_id, act_load_len);
  4213. if (release_exclusive(cd, cd->dev) < 0)
  4214. pt_debug(cd->dev, DL_ERROR,
  4215. "%s: fail to release exclusive\n", __func__);
  4216. return rc;
  4217. }
  4218. /*******************************************************************************
  4219. * FUNCTION: _pt_request_pip_load_self_test_param
  4220. *
  4221. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4222. * calls to the protected or unprotected call to
  4223. * pt_pip_load_self_test_param
  4224. *
  4225. * RETURN:
  4226. * 0 = success
  4227. * !0 = failure
  4228. *
  4229. * PARAMETERS:
  4230. * *dev - pointer to device structure
  4231. * protect - flag to call protected or non-protected
  4232. * self_test_id - enumerated test ID for which the parmeters belong
  4233. * load_offset - mem offset to where to load parameters
  4234. * load_length - length of parameter data to load
  4235. * *parameters - pointer to list of parameter data
  4236. * *status - pointer to store the response status
  4237. * *ret_test_id - pointer to returned test id the parameters were stored
  4238. * *act_load_len - pointer to store the actual load length that was writen
  4239. ******************************************************************************/
  4240. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4241. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4242. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4243. {
  4244. struct pt_core_data *cd = dev_get_drvdata(dev);
  4245. if (protect)
  4246. return pt_pip_load_self_test_param(cd, self_test_id,
  4247. load_offset, load_length, parameters, status, ret_test_id,
  4248. act_load_len);
  4249. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4250. load_length, parameters, status, ret_test_id, act_load_len);
  4251. }
  4252. /*******************************************************************************
  4253. * FUNCTION: pt_pip_calibrate_ext_
  4254. *
  4255. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4256. * for the response
  4257. *
  4258. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4259. *
  4260. * RETURN:
  4261. * 0 = success
  4262. * !0 = failure
  4263. *
  4264. * PARAMETERS:
  4265. * *cd - pointer to core data
  4266. * *cal_data - pointer to extended calibration data structure
  4267. * *status - pointer to where the command response status is stored
  4268. ******************************************************************************/
  4269. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4270. struct pt_cal_ext_data *cal_data, u8 *status)
  4271. {
  4272. int rc = 0;
  4273. int write_length = 4;
  4274. u8 write_buf[4];
  4275. u16 size = 0;
  4276. unsigned short crc = 0;
  4277. struct pt_hid_output hid_output = {
  4278. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4279. .write_length = write_length,
  4280. .write_buf = write_buf,
  4281. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4282. };
  4283. if (cal_data == NULL)
  4284. return -EINVAL;
  4285. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4286. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4287. if (rc)
  4288. return rc;
  4289. if (status)
  4290. *status = cd->response_buf[5];
  4291. /*
  4292. * When doing a calibration on a flashless DUT, save CAL data in
  4293. * the TTDL cache on any successful calibration
  4294. */
  4295. if (*status == 0 && cd->cal_cache_in_host) {
  4296. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4297. __func__);
  4298. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4299. &size, &crc);
  4300. if (rc)
  4301. pt_debug(cd->dev, DL_ERROR,
  4302. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4303. else
  4304. pt_debug(cd->dev, DL_INFO,
  4305. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4306. __func__, crc, size);
  4307. }
  4308. return 0;
  4309. }
  4310. /*******************************************************************************
  4311. * FUNCTION: pt_pip_calibrate_ext
  4312. *
  4313. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4314. * DUT
  4315. *
  4316. * RETURN:
  4317. * 0 = success
  4318. * !0 = failure
  4319. *
  4320. * PARAMETERS:
  4321. * *cd - pointer to core data
  4322. * *cal_data - pointer to extended calibration data structure
  4323. * *status - pointer to where the command response status is stored
  4324. ******************************************************************************/
  4325. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4326. struct pt_cal_ext_data *cal_data, u8 *status)
  4327. {
  4328. int rc;
  4329. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4330. if (rc < 0) {
  4331. pt_debug(cd->dev, DL_ERROR,
  4332. "%s: fail get exclusive ex=%p own=%p\n",
  4333. __func__, cd->exclusive_dev, cd->dev);
  4334. return rc;
  4335. }
  4336. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4337. if (release_exclusive(cd, cd->dev) < 0)
  4338. pt_debug(cd->dev, DL_ERROR,
  4339. "%s: fail to release exclusive\n", __func__);
  4340. return rc;
  4341. }
  4342. /*******************************************************************************
  4343. * FUNCTION: _pt_request_pip_calibrate_ext
  4344. *
  4345. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4346. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4347. *
  4348. * RETURN:
  4349. * 0 = success
  4350. * !0 = failure
  4351. *
  4352. * PARAMETERS:
  4353. * *dev - pointer to device structure
  4354. * protect - flag to call protected or non-protected
  4355. * *cal_data - pointer to extended calibration data structure
  4356. * *status - pointer to where the command response status is stored
  4357. ******************************************************************************/
  4358. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4359. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4360. {
  4361. struct pt_core_data *cd = dev_get_drvdata(dev);
  4362. if (protect)
  4363. return pt_pip_calibrate_ext(cd, cal_data, status);
  4364. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4365. }
  4366. /*******************************************************************************
  4367. * FUNCTION: pt_pip_calibrate_idacs_
  4368. *
  4369. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4370. * for the response
  4371. *
  4372. * RETURN:
  4373. * 0 = success
  4374. * !0 = failure
  4375. *
  4376. * PARAMETERS:
  4377. * *cd - pointer to core data
  4378. * mode - sense mode to calibrate (0-5)
  4379. * *status - pointer to where the command response status is stored
  4380. ******************************************************************************/
  4381. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4382. u8 mode, u8 *status)
  4383. {
  4384. int rc = 0;
  4385. int write_length = 1;
  4386. u8 write_buf[1];
  4387. u8 cmd_offset = 0;
  4388. struct pt_hid_output hid_output = {
  4389. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4390. .write_length = write_length,
  4391. .write_buf = write_buf,
  4392. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4393. };
  4394. write_buf[cmd_offset++] = mode;
  4395. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4396. if (rc)
  4397. return rc;
  4398. *status = cd->response_buf[5];
  4399. if (*status)
  4400. return -EINVAL;
  4401. return 0;
  4402. }
  4403. /*******************************************************************************
  4404. * FUNCTION: pt_pip_calibrate_idacs
  4405. *
  4406. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4407. * access to the DUT
  4408. *
  4409. * RETURN:
  4410. * 0 = success
  4411. * !0 = failure
  4412. *
  4413. * PARAMETERS:
  4414. * *cd - pointer to core data
  4415. * mode - sense mode to calibrate (0-5)
  4416. * *status - pointer to where the command response status is stored
  4417. ******************************************************************************/
  4418. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4419. u8 mode, u8 *status)
  4420. {
  4421. int rc;
  4422. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4423. if (rc < 0) {
  4424. pt_debug(cd->dev, DL_ERROR,
  4425. "%s: fail get exclusive ex=%p own=%p\n",
  4426. __func__, cd->exclusive_dev, cd->dev);
  4427. return rc;
  4428. }
  4429. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4430. if (release_exclusive(cd, cd->dev) < 0)
  4431. pt_debug(cd->dev, DL_ERROR,
  4432. "%s: fail to release exclusive\n", __func__);
  4433. return rc;
  4434. }
  4435. /*******************************************************************************
  4436. * FUNCTION: _pt_request_pip_calibrate_idacs
  4437. *
  4438. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4439. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4440. *
  4441. * RETURN:
  4442. * 0 = success
  4443. * !0 = failure
  4444. *
  4445. * PARAMETERS:
  4446. * *dev - pointer to device structure
  4447. * protect - flag to call protected or non-protected
  4448. * mode - sense mode to calibrate (0-5)
  4449. * *status - pointer to where the command response status is stored
  4450. ******************************************************************************/
  4451. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4452. int protect, u8 mode, u8 *status)
  4453. {
  4454. struct pt_core_data *cd = dev_get_drvdata(dev);
  4455. if (protect)
  4456. return pt_pip_calibrate_idacs(cd, mode, status);
  4457. return pt_pip_calibrate_idacs_(cd, mode, status);
  4458. }
  4459. /*******************************************************************************
  4460. * FUNCTION: pt_hid_output_initialize_baselines_
  4461. *
  4462. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4463. * waiting for the response.
  4464. *
  4465. * RETURN:
  4466. * 0 = success
  4467. * !0 = failure
  4468. *
  4469. * PARAMETERS:
  4470. * *cd - pointer to core data
  4471. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4472. * each or together with a single command.
  4473. * *status - pointer to where the command response status is stored
  4474. ******************************************************************************/
  4475. static int pt_hid_output_initialize_baselines_(
  4476. struct pt_core_data *cd, u8 test_id, u8 *status)
  4477. {
  4478. int rc = 0;
  4479. int write_length = 1;
  4480. u8 write_buf[1];
  4481. u8 cmd_offset = 0;
  4482. struct pt_hid_output hid_output = {
  4483. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4484. .write_length = write_length,
  4485. .write_buf = write_buf,
  4486. };
  4487. write_buf[cmd_offset++] = test_id;
  4488. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4489. if (rc)
  4490. return rc;
  4491. *status = cd->response_buf[5];
  4492. if (*status)
  4493. return -EINVAL;
  4494. return rc;
  4495. }
  4496. /*******************************************************************************
  4497. * FUNCTION: pt_hid_output_initialize_baselines
  4498. *
  4499. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4500. * access to the DUT
  4501. *
  4502. * RETURN:
  4503. * 0 = success
  4504. * !0 = failure
  4505. *
  4506. * PARAMETERS:
  4507. * *cd - pointer to core data
  4508. * test_id - enumerated ID against which to initialize the baseline
  4509. * *status - pointer to where the command response status is stored
  4510. ******************************************************************************/
  4511. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4512. u8 test_id, u8 *status)
  4513. {
  4514. int rc;
  4515. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4516. if (rc < 0) {
  4517. pt_debug(cd->dev, DL_ERROR,
  4518. "%s: fail get exclusive ex=%p own=%p\n",
  4519. __func__, cd->exclusive_dev, cd->dev);
  4520. return rc;
  4521. }
  4522. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4523. if (release_exclusive(cd, cd->dev) < 0)
  4524. pt_debug(cd->dev, DL_ERROR,
  4525. "%s: fail to release exclusive\n", __func__);
  4526. return rc;
  4527. }
  4528. /*******************************************************************************
  4529. * FUNCTION: _pt_request_pip_initialize_baselines
  4530. *
  4531. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4532. * calls to the protected or unprotected call to
  4533. * pt_pip_initialize_baselines
  4534. *
  4535. * RETURN:
  4536. * 0 = success
  4537. * !0 = failure
  4538. *
  4539. * PARAMETERS:
  4540. * *dev - pointer to device structure
  4541. * protect - flag to call protected or non-protected
  4542. * test_id - enumerated ID against which to initialize the baseline
  4543. * *status - pointer to where the command response status is stored
  4544. ******************************************************************************/
  4545. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4546. int protect, u8 test_id, u8 *status)
  4547. {
  4548. struct pt_core_data *cd = dev_get_drvdata(dev);
  4549. if (protect)
  4550. return pt_hid_output_initialize_baselines(cd, test_id,
  4551. status);
  4552. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4553. }
  4554. /*******************************************************************************
  4555. * FUNCTION: pt_hid_output_exec_panel_scan_
  4556. *
  4557. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4558. * the response
  4559. *
  4560. * RETURN:
  4561. * 0 = success
  4562. * !0 = failure
  4563. *
  4564. * PARAMETERS:
  4565. * *cd - pointer to core data
  4566. ******************************************************************************/
  4567. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4568. {
  4569. struct pt_hid_output hid_output = {
  4570. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4571. };
  4572. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4573. }
  4574. /*******************************************************************************
  4575. * FUNCTION: pt_hid_output_exec_panel_scan
  4576. *
  4577. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4578. * access to the DUT.
  4579. *
  4580. * RETURN:
  4581. * 0 = success
  4582. * !0 = failure
  4583. *
  4584. * PARAMETERS:
  4585. * *cd - pointer to core data
  4586. ******************************************************************************/
  4587. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4588. {
  4589. int rc;
  4590. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4591. if (rc < 0) {
  4592. pt_debug(cd->dev, DL_ERROR,
  4593. "%s: fail get exclusive ex=%p own=%p\n",
  4594. __func__, cd->exclusive_dev, cd->dev);
  4595. return rc;
  4596. }
  4597. rc = pt_hid_output_exec_panel_scan_(cd);
  4598. if (release_exclusive(cd, cd->dev) < 0)
  4599. pt_debug(cd->dev, DL_ERROR,
  4600. "%s: fail to release exclusive\n", __func__);
  4601. return rc;
  4602. }
  4603. /*******************************************************************************
  4604. * FUNCTION: pt_pip2_exec_panel_scan_
  4605. *
  4606. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4607. * the response
  4608. *
  4609. * RETURN:
  4610. * 0 = success
  4611. * !0 = failure
  4612. *
  4613. * PARAMETERS:
  4614. * *cd - pointer to core data
  4615. * scan_type - type of panel scan to perform (PIP2 only)
  4616. ******************************************************************************/
  4617. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4618. {
  4619. int rc = 0;
  4620. u8 data[2];
  4621. u8 read_buf[10];
  4622. u16 actual_read_len;
  4623. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4624. __func__, scan_type);
  4625. data[0] = scan_type;
  4626. rc = _pt_request_pip2_send_cmd(cd->dev,
  4627. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4628. data, 1, read_buf, &actual_read_len);
  4629. if (rc) {
  4630. pt_debug(cd->dev, DL_ERROR,
  4631. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4632. __func__, scan_type, rc);
  4633. }
  4634. return rc;
  4635. }
  4636. /*******************************************************************************
  4637. * FUNCTION: pt_pip2_exec_panel_scan
  4638. *
  4639. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4640. * access to the DUT.
  4641. *
  4642. * RETURN:
  4643. * 0 = success
  4644. * !0 = failure
  4645. *
  4646. * PARAMETERS:
  4647. * *cd - pointer to core data
  4648. * scan_type - type of panel scan to perform (PIP2 only)
  4649. ******************************************************************************/
  4650. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4651. {
  4652. int rc;
  4653. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4654. if (rc < 0) {
  4655. pt_debug(cd->dev, DL_ERROR,
  4656. "%s: fail get exclusive ex=%p own=%p\n",
  4657. __func__, cd->exclusive_dev, cd->dev);
  4658. return rc;
  4659. }
  4660. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4661. if (release_exclusive(cd, cd->dev) < 0)
  4662. pt_debug(cd->dev, DL_ERROR,
  4663. "%s: fail to release exclusive\n", __func__);
  4664. return rc;
  4665. }
  4666. /*******************************************************************************
  4667. * FUNCTION: _pt_request_pip_exec_panel_scan
  4668. *
  4669. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4670. * calls to the protected or unprotected call to
  4671. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4672. *
  4673. * RETURN:
  4674. * 0 = success
  4675. * !0 = failure
  4676. *
  4677. * PARAMETERS:
  4678. * *dev - pointer to device structure
  4679. * protect - flag to call protected or non-protected
  4680. * scan_type - type of panel scan to perform (PIP2 only)
  4681. ******************************************************************************/
  4682. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4683. int protect, u8 scan_type)
  4684. {
  4685. struct pt_core_data *cd = dev_get_drvdata(dev);
  4686. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4687. if (protect)
  4688. return pt_pip2_exec_panel_scan(cd, scan_type);
  4689. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4690. }
  4691. if (protect)
  4692. return pt_hid_output_exec_panel_scan(cd);
  4693. return pt_hid_output_exec_panel_scan_(cd);
  4694. }
  4695. /*******************************************************************************
  4696. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4697. *
  4698. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4699. * to retrieve the specified data type for a the last successful Execute
  4700. * Panel Scan command.
  4701. *
  4702. * RETURN:
  4703. * 0 = success
  4704. * !0 = failure
  4705. *
  4706. * PARAMETERS:
  4707. * *dev - pointer to device structure
  4708. * protect - flag to call protected or non-protected
  4709. * read_offset - read pointer offset
  4710. * read_count - length of data to read
  4711. * data_id - enumerated test ID to read selftest results from
  4712. * *response - pointer to store the read response status
  4713. * *config - pointer to store config data
  4714. * *actual_read_len - pointer to store data length actually read
  4715. * *read_buf - pointer to the read buffer
  4716. ******************************************************************************/
  4717. static int pt_hid_output_retrieve_panel_scan_(
  4718. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4719. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4720. u8 *read_buf)
  4721. {
  4722. int status;
  4723. u8 read_data_id;
  4724. int rc = 0;
  4725. int write_length = 5;
  4726. u8 write_buf[5];
  4727. u8 cmd_offset = 0;
  4728. u8 data_elem_size;
  4729. int size;
  4730. int data_size;
  4731. struct pt_hid_output hid_output = {
  4732. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4733. .write_length = write_length,
  4734. .write_buf = write_buf,
  4735. };
  4736. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4737. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4738. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4739. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4740. write_buf[cmd_offset++] = data_id;
  4741. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4742. if (rc)
  4743. return rc;
  4744. status = cd->response_buf[5];
  4745. if (status)
  4746. return -EINVAL;
  4747. read_data_id = cd->response_buf[6];
  4748. if (read_data_id != data_id)
  4749. return -EPROTO;
  4750. size = get_unaligned_le16(&cd->response_buf[0]);
  4751. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4752. *config = cd->response_buf[9];
  4753. data_elem_size = *config & 0x07;
  4754. data_size = *actual_read_len * data_elem_size;
  4755. if (read_buf)
  4756. memcpy(read_buf, &cd->response_buf[10], data_size);
  4757. if (response)
  4758. memcpy(response, cd->response_buf, size);
  4759. return rc;
  4760. }
  4761. /*******************************************************************************
  4762. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4763. *
  4764. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4765. * access to the DUT.
  4766. *
  4767. * RETURN:
  4768. * 0 = success
  4769. * !0 = failure
  4770. *
  4771. * PARAMETERS:
  4772. * *dev - pointer to device structure
  4773. * protect - flag to call protected or non-protected
  4774. * read_offset - read pointer offset
  4775. * read_count - length of data to read
  4776. * data_id - enumerated test ID to read selftest results from
  4777. * *response - pointer to store the read response status
  4778. * *config - pointer to store config data
  4779. * *actual_read_len - pointer to store data length actually read
  4780. * *read_buf - pointer to the read buffer
  4781. ******************************************************************************/
  4782. static int pt_hid_output_retrieve_panel_scan(
  4783. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4784. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4785. u8 *read_buf)
  4786. {
  4787. int rc;
  4788. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4789. if (rc < 0) {
  4790. pt_debug(cd->dev, DL_ERROR,
  4791. "%s: fail get exclusive ex=%p own=%p\n",
  4792. __func__, cd->exclusive_dev, cd->dev);
  4793. return rc;
  4794. }
  4795. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4796. read_count, data_id, response, config,
  4797. actual_read_len, read_buf);
  4798. if (release_exclusive(cd, cd->dev) < 0)
  4799. pt_debug(cd->dev, DL_ERROR,
  4800. "%s: fail to release exclusive\n", __func__);
  4801. return rc;
  4802. }
  4803. /*******************************************************************************
  4804. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4805. *
  4806. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4807. * calls to the protected or unprotected call to
  4808. * pt_hid_output_retrieve_panel_scan
  4809. *
  4810. * RETURN:
  4811. * 0 = success
  4812. * !0 = failure
  4813. *
  4814. * PARAMETERS:
  4815. * *dev - pointer to device structure
  4816. * protect - flag to call protected or non-protected
  4817. * read_offset - read pointer offset
  4818. * read_count - length of data to read
  4819. * data_id - enumerated test ID to read selftest results from
  4820. * *response - pointer to store the read response status
  4821. * *config - pointer to store config data
  4822. * *actual_read_len - pointer to store data length actually read
  4823. * *read_buf - pointer to the read buffer
  4824. ******************************************************************************/
  4825. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4826. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4827. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4828. {
  4829. struct pt_core_data *cd = dev_get_drvdata(dev);
  4830. if (protect)
  4831. return pt_hid_output_retrieve_panel_scan(cd,
  4832. read_offset, read_count, data_id, response,
  4833. config, actual_read_len, read_buf);
  4834. return pt_hid_output_retrieve_panel_scan_(cd,
  4835. read_offset, read_count, data_id, response,
  4836. config, actual_read_len, read_buf);
  4837. }
  4838. /*******************************************************************************
  4839. * FUNCTION: _pt_request_pip_user_cmd
  4840. *
  4841. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4842. * calls to the protected or unprotected call to
  4843. * pt_hid_output_user_cmd
  4844. *
  4845. * RETURN:
  4846. * 0 = success
  4847. * !0 = failure
  4848. *
  4849. * PARAMETERS:
  4850. * *dev - pointer to device structure
  4851. * protect - flag to call protected or non-protected
  4852. * read_len - length of data to read
  4853. * *read_buf - pointer to store read data
  4854. * write_len - length of data to write
  4855. * *write_buf - pointer to buffer to write
  4856. * *actual_read_len - pointer to store data length actually read
  4857. ******************************************************************************/
  4858. static int _pt_request_pip_user_cmd(struct device *dev,
  4859. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4860. u8 *write_buf, u16 *actual_read_len)
  4861. {
  4862. struct pt_core_data *cd = dev_get_drvdata(dev);
  4863. if (protect)
  4864. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4865. write_len, write_buf, actual_read_len);
  4866. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4867. write_len, write_buf, actual_read_len);
  4868. }
  4869. /*******************************************************************************
  4870. * FUNCTION: pt_hid_output_bl_get_information_
  4871. *
  4872. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4873. * DUT to retrieve bootloader version and chip identification information.
  4874. *
  4875. * RETURN:
  4876. * 0 = success
  4877. * !0 = failure
  4878. *
  4879. * PARAMETERS:
  4880. * *cd - pointer to core data
  4881. * *return_data - pointer to store the return data
  4882. *****************************************************************************/
  4883. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4884. u8 *return_data)
  4885. {
  4886. int rc;
  4887. int data_len;
  4888. struct pt_hid_output hid_output = {
  4889. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4890. };
  4891. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4892. if (rc)
  4893. return rc;
  4894. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4895. if (!data_len)
  4896. return -EPROTO;
  4897. memcpy(return_data, &cd->response_buf[8], data_len);
  4898. return 0;
  4899. }
  4900. /*******************************************************************************
  4901. * FUNCTION: pt_hid_output_bl_get_information
  4902. *
  4903. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4904. * access to the DUT.
  4905. *
  4906. * RETURN:
  4907. * 0 = success
  4908. * !0 = failure
  4909. *
  4910. * PARAMETERS:
  4911. * *cd - pointer to core data
  4912. * *return_data - pointer to store the return data
  4913. ******************************************************************************/
  4914. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4915. u8 *return_data)
  4916. {
  4917. int rc;
  4918. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4919. if (rc < 0) {
  4920. pt_debug(cd->dev, DL_ERROR,
  4921. "%s: fail get exclusive ex=%p own=%p\n",
  4922. __func__, cd->exclusive_dev, cd->dev);
  4923. return rc;
  4924. }
  4925. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4926. if (release_exclusive(cd, cd->dev) < 0)
  4927. pt_debug(cd->dev, DL_ERROR,
  4928. "%s: fail to release exclusive\n", __func__);
  4929. return rc;
  4930. }
  4931. /*******************************************************************************
  4932. * FUNCTION: _pt_request_pip_bl_get_information
  4933. *
  4934. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4935. * calls to the protected or unprotected call to
  4936. * pt_hid_output_bl_get_information
  4937. *
  4938. * RETURN:
  4939. * 0 = success
  4940. * !0 = failure
  4941. *
  4942. * PARAMETERS:
  4943. * *dev - pointer to device structure
  4944. * protect - flag to call protected or non-protected
  4945. * *return_data - pointer to store bl data
  4946. ******************************************************************************/
  4947. static int _pt_request_pip_bl_get_information(struct device *dev,
  4948. int protect, u8 *return_data)
  4949. {
  4950. struct pt_core_data *cd = dev_get_drvdata(dev);
  4951. if (protect)
  4952. return pt_hid_output_bl_get_information(cd, return_data);
  4953. return pt_hid_output_bl_get_information_(cd, return_data);
  4954. }
  4955. /*******************************************************************************
  4956. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4957. *
  4958. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4959. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4960. * and Design Data block in flash and enables the host to execute the Program
  4961. * and Verify Row command to bootload the application image and data.
  4962. *
  4963. * RETURN:
  4964. * 0 = success
  4965. * !0 = failure
  4966. *
  4967. * PARAMETERS:
  4968. * *cd - pointer to core data
  4969. * protect - flag to call protected or non-protected
  4970. * key_size - size of key
  4971. * *key_buf - pointer to key data to allow operation
  4972. * row_size - size of the meta data row
  4973. * *metadata_row_buf - pointer to meta data to write
  4974. ******************************************************************************/
  4975. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4976. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4977. {
  4978. u16 write_length = key_size + row_size;
  4979. u8 *write_buf;
  4980. int rc = 0;
  4981. struct pt_hid_output hid_output = {
  4982. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4983. .write_length = write_length,
  4984. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4985. };
  4986. write_buf = kzalloc(write_length, GFP_KERNEL);
  4987. if (!write_buf)
  4988. return -ENOMEM;
  4989. hid_output.write_buf = write_buf;
  4990. if (key_size)
  4991. memcpy(write_buf, key_buf, key_size);
  4992. if (row_size)
  4993. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4994. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4995. kfree(write_buf);
  4996. return rc;
  4997. }
  4998. /*******************************************************************************
  4999. * FUNCTION: pt_hid_output_bl_initiate_bl
  5000. *
  5001. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5002. * access to the DUT.
  5003. *
  5004. * RETURN:
  5005. * 0 = success
  5006. * !0 = failure
  5007. *
  5008. * PARAMETERS:
  5009. * *cd - pointer to core data
  5010. * key_size - size of key
  5011. * *key_buf - pointer to key data to allow operation
  5012. * row_size - size of the meta data row
  5013. * *metadata_row_buf - pointer to meta data to write
  5014. ******************************************************************************/
  5015. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5016. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5017. {
  5018. int rc;
  5019. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5020. if (rc < 0) {
  5021. pt_debug(cd->dev, DL_ERROR,
  5022. "%s: fail get exclusive ex=%p own=%p\n",
  5023. __func__, cd->exclusive_dev, cd->dev);
  5024. return rc;
  5025. }
  5026. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5027. row_size, metadata_row_buf);
  5028. if (release_exclusive(cd, cd->dev) < 0)
  5029. pt_debug(cd->dev, DL_ERROR,
  5030. "%s: fail to release exclusive\n", __func__);
  5031. return rc;
  5032. }
  5033. /*******************************************************************************
  5034. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5035. *
  5036. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5037. * calls to the protected or unprotected call to
  5038. * pt_hid_output_bl_initiate_bl
  5039. *
  5040. * RETURN:
  5041. * 0 = success
  5042. * !0 = failure
  5043. *
  5044. * PARAMETERS:
  5045. * *dev - pointer to device structure
  5046. * protect - flag to call protected or non-protected
  5047. * key_size - size of key
  5048. * *key_buf - pointer to key data to allow operation
  5049. * row_size - size of the meta data row
  5050. * *metadata_row_buf - pointer to meta data to write
  5051. ******************************************************************************/
  5052. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5053. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5054. u8 *metadata_row_buf)
  5055. {
  5056. struct pt_core_data *cd = dev_get_drvdata(dev);
  5057. if (protect)
  5058. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5059. row_size, metadata_row_buf);
  5060. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5061. row_size, metadata_row_buf);
  5062. }
  5063. /*******************************************************************************
  5064. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5065. *
  5066. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5067. * and program a 128-byte row into the flash, and then verifies written data.
  5068. *
  5069. * RETURN:
  5070. * 0 = success
  5071. * !0 = failure
  5072. *
  5073. * PARAMETERS:
  5074. * *cd - pointer to core data
  5075. * data_len - length of data_buf
  5076. * *data_buf - firmware image to program
  5077. ******************************************************************************/
  5078. static int pt_hid_output_bl_program_and_verify_(
  5079. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5080. {
  5081. struct pt_hid_output hid_output = {
  5082. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5083. .write_length = data_len,
  5084. .write_buf = data_buf,
  5085. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5086. };
  5087. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5088. }
  5089. /*******************************************************************************
  5090. * FUNCTION: pt_hid_output_bl_program_and_verify
  5091. *
  5092. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5093. * access to the DUT.
  5094. *
  5095. * RETURN:
  5096. * 0 = success
  5097. * !0 = failure
  5098. *
  5099. * PARAMETERS:
  5100. * *cd - pointer to core data
  5101. * data_len - length of data_buf
  5102. * *data_buf - firmware image to program
  5103. ******************************************************************************/
  5104. static int pt_hid_output_bl_program_and_verify(
  5105. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5106. {
  5107. int rc;
  5108. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5109. if (rc < 0) {
  5110. pt_debug(cd->dev, DL_ERROR,
  5111. "%s: fail get exclusive ex=%p own=%p\n",
  5112. __func__, cd->exclusive_dev, cd->dev);
  5113. return rc;
  5114. }
  5115. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5116. if (release_exclusive(cd, cd->dev) < 0)
  5117. pt_debug(cd->dev, DL_ERROR,
  5118. "%s: fail to release exclusive\n", __func__);
  5119. return rc;
  5120. }
  5121. /*******************************************************************************
  5122. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5123. *
  5124. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5125. * to request to have the BL program and verify a FW image
  5126. *
  5127. * RETURN:
  5128. * 0 = success
  5129. * !0 = failure
  5130. *
  5131. * PARAMETERS:
  5132. * *dev - pointer to device structure
  5133. * protect - boolean to determine to call the protected function
  5134. * data_len - length of data_buf
  5135. * *data_buf - firmware image to program
  5136. ******************************************************************************/
  5137. static int _pt_request_pip_bl_program_and_verify(
  5138. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5139. {
  5140. struct pt_core_data *cd = dev_get_drvdata(dev);
  5141. if (protect)
  5142. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5143. data_buf);
  5144. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5145. data_buf);
  5146. }
  5147. /*******************************************************************************
  5148. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5149. *
  5150. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5151. * perform a full verification of the application integrity by calculating the
  5152. * CRC of the image in flash and compare it to the expected CRC stored in the
  5153. * Metadata row.
  5154. *
  5155. * RETURN:
  5156. * 0 = success
  5157. * !0 = failure
  5158. *
  5159. * PARAMETERS:
  5160. * *cd - pointer to core data
  5161. * *result - pointer to store result
  5162. ******************************************************************************/
  5163. static int pt_hid_output_bl_verify_app_integrity_(
  5164. struct pt_core_data *cd, u8 *result)
  5165. {
  5166. int rc;
  5167. struct pt_hid_output hid_output = {
  5168. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5169. };
  5170. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5171. if (rc) {
  5172. *result = 0;
  5173. return rc;
  5174. }
  5175. *result = cd->response_buf[8];
  5176. return 0;
  5177. }
  5178. /*******************************************************************************
  5179. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5180. *
  5181. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5182. * exclusive access to the DUT.
  5183. *
  5184. * RETURN:
  5185. * 0 = success
  5186. * !0 = failure
  5187. *
  5188. * PARAMETERS:
  5189. * *cd - pointer to core data
  5190. * *result - pointer to store result
  5191. ******************************************************************************/
  5192. static int pt_hid_output_bl_verify_app_integrity(
  5193. struct pt_core_data *cd, u8 *result)
  5194. {
  5195. int rc;
  5196. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5197. if (rc < 0) {
  5198. pt_debug(cd->dev, DL_ERROR,
  5199. "%s: fail get exclusive ex=%p own=%p\n",
  5200. __func__, cd->exclusive_dev, cd->dev);
  5201. return rc;
  5202. }
  5203. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5204. if (release_exclusive(cd, cd->dev) < 0)
  5205. pt_debug(cd->dev, DL_ERROR,
  5206. "%s: fail to release exclusive\n", __func__);
  5207. return rc;
  5208. }
  5209. /*******************************************************************************
  5210. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5211. *
  5212. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5213. * to request to have the BL verify the application integrity (PIP1.x only)
  5214. *
  5215. * RETURN:
  5216. * 0 = success
  5217. * !0 = failure
  5218. *
  5219. * PARAMETERS:
  5220. * *dev - pointer to device structure
  5221. * protect - boolean to determine to call the protected function
  5222. * *result - pointer to store result
  5223. ******************************************************************************/
  5224. static int _pt_request_pip_bl_verify_app_integrity(
  5225. struct device *dev, int protect, u8 *result)
  5226. {
  5227. struct pt_core_data *cd = dev_get_drvdata(dev);
  5228. if (protect)
  5229. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5230. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5231. }
  5232. /*******************************************************************************
  5233. * FUNCTION: pt_hid_output_bl_launch_app_
  5234. *
  5235. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5236. * application from bootloader (PIP1.x only).
  5237. *
  5238. * RETURN:
  5239. * 0 = success
  5240. * !0 = failure
  5241. *
  5242. * PARAMETERS:
  5243. * *cd - pointer to core data
  5244. ******************************************************************************/
  5245. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5246. {
  5247. struct pt_hid_output hid_output = {
  5248. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5249. .reset_expected = 1,
  5250. };
  5251. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5252. }
  5253. /*******************************************************************************
  5254. * FUNCTION: pt_hid_output_bl_launch_app
  5255. *
  5256. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5257. * to the DUT.
  5258. *
  5259. * RETURN:
  5260. * 0 = success
  5261. * !0 = failure
  5262. *
  5263. * PARAMETERS:
  5264. * *cd - pointer to core data
  5265. ******************************************************************************/
  5266. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5267. {
  5268. int rc;
  5269. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5270. if (rc < 0) {
  5271. pt_debug(cd->dev, DL_ERROR,
  5272. "%s: fail get exclusive ex=%p own=%p\n",
  5273. __func__, cd->exclusive_dev, cd->dev);
  5274. return rc;
  5275. }
  5276. rc = pt_hid_output_bl_launch_app_(cd);
  5277. if (release_exclusive(cd, cd->dev) < 0)
  5278. pt_debug(cd->dev, DL_ERROR,
  5279. "%s: fail to release exclusive\n", __func__);
  5280. return rc;
  5281. }
  5282. /*******************************************************************************
  5283. * FUNCTION: _pt_request_pip_launch_app
  5284. *
  5285. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5286. * to request to have the BL launch the application. (PIP1.x only)
  5287. *
  5288. * RETURN:
  5289. * 0 = success
  5290. * !0 = failure
  5291. *
  5292. * PARAMETERS:
  5293. * *dev - pointer to device structure
  5294. * protect - boolean to determine to call the protected function
  5295. ******************************************************************************/
  5296. static int _pt_request_pip_launch_app(struct device *dev,
  5297. int protect)
  5298. {
  5299. struct pt_core_data *cd = dev_get_drvdata(dev);
  5300. if (protect)
  5301. return pt_hid_output_bl_launch_app(cd);
  5302. return pt_hid_output_bl_launch_app_(cd);
  5303. }
  5304. /*******************************************************************************
  5305. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5306. *
  5307. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5308. * value store in the System Information.
  5309. *
  5310. * RETURN:
  5311. * 0 = success
  5312. * !0 = failure
  5313. *
  5314. * PARAMETERS:
  5315. * *cd - pointer to core data
  5316. * *panel_id - pointer to where the panel ID will be stored
  5317. ******************************************************************************/
  5318. static int pt_hid_output_bl_get_panel_id_(
  5319. struct pt_core_data *cd, u8 *panel_id)
  5320. {
  5321. int rc;
  5322. struct pt_hid_output hid_output = {
  5323. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5324. };
  5325. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5326. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5327. pt_debug(cd->dev, DL_ERROR,
  5328. "%s: Get Panel ID command not supported\n",
  5329. __func__);
  5330. *panel_id = PANEL_ID_NOT_ENABLED;
  5331. return 0;
  5332. } else if (rc < 0) {
  5333. pt_debug(cd->dev, DL_ERROR,
  5334. "%s: Error on Get Panel ID command\n", __func__);
  5335. return rc;
  5336. }
  5337. *panel_id = cd->response_buf[8];
  5338. return 0;
  5339. }
  5340. /*******************************************************************************
  5341. * FUNCTION: pt_hid_output_bl_get_panel_id
  5342. *
  5343. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5344. * to the DUT.
  5345. *
  5346. * RETURN:
  5347. * 0 = success
  5348. * !0 = failure
  5349. *
  5350. * PARAMETERS:
  5351. * *cd - pointer to core data
  5352. * *panel_id - pointer to where the panel ID will be stored
  5353. ******************************************************************************/
  5354. static int pt_hid_output_bl_get_panel_id(
  5355. struct pt_core_data *cd, u8 *panel_id)
  5356. {
  5357. int rc;
  5358. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5359. if (rc < 0) {
  5360. pt_debug(cd->dev, DL_ERROR,
  5361. "%s: fail get exclusive ex=%p own=%p\n",
  5362. __func__, cd->exclusive_dev, cd->dev);
  5363. return rc;
  5364. }
  5365. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5366. if (release_exclusive(cd, cd->dev) < 0)
  5367. pt_debug(cd->dev, DL_ERROR,
  5368. "%s: fail to release exclusive\n", __func__);
  5369. return rc;
  5370. }
  5371. /*******************************************************************************
  5372. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5373. *
  5374. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5375. * to have the BL retrieve the panel ID
  5376. *
  5377. * RETURN:
  5378. * 0 = success
  5379. * !0 = failure
  5380. *
  5381. * PARAMETERS:
  5382. * *dev - pointer to device structure
  5383. * protect - flag to run in protected mode
  5384. * *panel_id - pointer to where the panel ID will be stored
  5385. ******************************************************************************/
  5386. static int _pt_request_pip_bl_get_panel_id(
  5387. struct device *dev, int protect, u8 *panel_id)
  5388. {
  5389. struct pt_core_data *cd = dev_get_drvdata(dev);
  5390. if (protect)
  5391. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5392. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5393. }
  5394. /*******************************************************************************
  5395. * FUNCTION: pt_pip2_get_mode_sysmode_
  5396. *
  5397. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5398. * PIP2 STATUS command.
  5399. *
  5400. * RETURN:
  5401. * 0 = success
  5402. * !0 = failure
  5403. *
  5404. * PARAMETERS:
  5405. * *cd - pointer to core data structure
  5406. * *mode - pointer to store the retrieved mode
  5407. * *sys_mode - pointer to store the FW system mode
  5408. ******************************************************************************/
  5409. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5410. u8 *mode, u8 *sys_mode)
  5411. {
  5412. u16 actual_read_len;
  5413. u8 read_buf[12];
  5414. u8 status, boot;
  5415. int rc = 0;
  5416. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5417. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5418. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5419. __func__, rc);
  5420. if (!rc) {
  5421. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5422. "PIP2 STATUS");
  5423. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5424. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5425. if (sys_mode) {
  5426. if (status == PIP2_RSP_ERR_NONE &&
  5427. boot == PIP2_STATUS_APP_EXEC)
  5428. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5429. else
  5430. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5431. }
  5432. if (mode) {
  5433. if (status == PIP2_RSP_ERR_NONE &&
  5434. boot == PIP2_STATUS_BOOT_EXEC)
  5435. *mode = PT_MODE_BOOTLOADER;
  5436. else if (status == PIP2_RSP_ERR_NONE &&
  5437. boot == PIP2_STATUS_APP_EXEC)
  5438. *mode = PT_MODE_OPERATIONAL;
  5439. else
  5440. *mode = PT_MODE_UNKNOWN;
  5441. }
  5442. } else {
  5443. if (mode)
  5444. *mode = PT_MODE_UNKNOWN;
  5445. if (sys_mode)
  5446. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5447. pt_debug(cd->dev, DL_WARN,
  5448. "%s: Mode and sys_mode could not be determined\n",
  5449. __func__);
  5450. }
  5451. return rc;
  5452. }
  5453. /*******************************************************************************
  5454. * FUNCTION: pt_pip2_get_mode_sysmode
  5455. *
  5456. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5457. * access to the DUT.
  5458. *
  5459. * RETURN:
  5460. * 0 = success
  5461. * !0 = failure
  5462. *
  5463. * PARAMETERS:
  5464. * *cd - pointer to core data structure
  5465. * *mode - pointer to store the retrieved mode
  5466. * *sys_mode - pointer to store the FW system mode
  5467. ******************************************************************************/
  5468. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5469. u8 *mode, u8 *sys_mode)
  5470. {
  5471. int rc;
  5472. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5473. if (rc < 0) {
  5474. pt_debug(cd->dev, DL_ERROR,
  5475. "%s: fail get exclusive ex=%p own=%p\n",
  5476. __func__, cd->exclusive_dev, cd->dev);
  5477. return rc;
  5478. }
  5479. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5480. if (release_exclusive(cd, cd->dev) < 0)
  5481. pt_debug(cd->dev, DL_ERROR,
  5482. "%s: fail to release exclusive\n", __func__);
  5483. return rc;
  5484. }
  5485. /*******************************************************************************
  5486. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5487. *
  5488. * SUMMARY: Function pointer included in core_commands struct for external
  5489. * calls to the protected or unprotected call to
  5490. * pt_pip2_get_mode_sysmode
  5491. *
  5492. * RETURN:
  5493. * 0 = success
  5494. * !0 = failure
  5495. *
  5496. * PARAMETERS:
  5497. * *dev - pointer to device structure
  5498. * *mode - pointer to store the retrieved mode
  5499. * *sys_mode - pointer to store the FW system mode
  5500. ******************************************************************************/
  5501. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5502. int protect, u8 *mode, u8 *sys_mode)
  5503. {
  5504. struct pt_core_data *cd = dev_get_drvdata(dev);
  5505. if (protect)
  5506. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5507. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5508. }
  5509. /*******************************************************************************
  5510. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5511. *
  5512. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5513. * mode only the following PIP commands will be serviced, any other PIP
  5514. * command the FW will respond with an "Invalid PIP Command" response.
  5515. * - Get HID Descriptor (Register 0x0001, no command ID)
  5516. * - Reset (Register 0x0005, RESET HID request)
  5517. * - Ping (Register 0x0004, Command ID 0x00
  5518. * - Get System Information (Register 0x0004, Command ID 0x02)
  5519. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5520. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5521. * This function will loop on the results of the STATUS command until
  5522. * the FW reports it is out of BOOT mode.
  5523. *
  5524. * NOTE:
  5525. * - This function will update cd->fw_system_mode
  5526. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5527. *
  5528. * RETURN:
  5529. * 0 = success
  5530. * !0 = failure
  5531. *
  5532. * PARAMETERS:
  5533. * *cd - pointer to core data
  5534. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5535. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5536. ******************************************************************************/
  5537. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5538. int *actual_wait)
  5539. {
  5540. int loop = 0;
  5541. u8 sys_mode = cd->fw_system_mode;
  5542. u8 pause = 10; /* in ms */
  5543. int rc = 0;
  5544. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5545. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5546. /*
  5547. * For PIP <1.11, no support for polling wait so do a hard
  5548. * coded wait and assume the FW is out of BOOT. Added 1 to
  5549. * timeout to make it clear in kmsg if non polling was done.
  5550. */
  5551. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5552. pt_debug(cd->dev, DL_ERROR,
  5553. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5554. __func__,
  5555. cd->sysinfo.ttdata.pip_ver_major,
  5556. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5557. msleep(*actual_wait);
  5558. sys_mode = FW_SYS_MODE_SCANNING;
  5559. }
  5560. if (sys_mode == FW_SYS_MODE_BOOT) {
  5561. while (!rc && loop <= max_loop &&
  5562. (sys_mode == FW_SYS_MODE_BOOT)) {
  5563. loop++;
  5564. usleep_range(9000, pause * 1000);
  5565. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5566. pt_debug(cd->dev, DL_DEBUG,
  5567. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5568. __func__, loop * pause, sys_mode);
  5569. }
  5570. *actual_wait = (int)(loop * pause);
  5571. pt_debug(cd->dev, DL_WARN,
  5572. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5573. __func__, *actual_wait, sys_mode);
  5574. if (rc)
  5575. sys_mode = FW_SYS_MODE_UNDEFINED;
  5576. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5577. sys_mode == FW_SYS_MODE_UNDEFINED)
  5578. rc = -EBUSY;
  5579. }
  5580. mutex_lock(&cd->system_lock);
  5581. cd->fw_system_mode = sys_mode;
  5582. mutex_unlock(&cd->system_lock);
  5583. return rc;
  5584. }
  5585. /*******************************************************************************
  5586. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5587. *
  5588. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5589. * access to the DUT.
  5590. *
  5591. * RETURN:
  5592. * 0 = success
  5593. * !0 = failure
  5594. *
  5595. * PARAMETERS:
  5596. * *cd - pointer to core data
  5597. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5598. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5599. ******************************************************************************/
  5600. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5601. int *actual_wait)
  5602. {
  5603. int rc;
  5604. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5605. if (rc < 0) {
  5606. pt_debug(cd->dev, DL_ERROR,
  5607. "%s: fail get exclusive ex=%p own=%p\n",
  5608. __func__, cd->exclusive_dev, cd->dev);
  5609. return rc;
  5610. }
  5611. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5612. if (release_exclusive(cd, cd->dev) < 0)
  5613. pt_debug(cd->dev, DL_ERROR,
  5614. "%s: fail to release exclusive\n", __func__);
  5615. return rc;
  5616. }
  5617. /*******************************************************************************
  5618. * FUNCTION: _pt_get_fw_sys_mode
  5619. *
  5620. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5621. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5622. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5623. * information but any PIP command above 0x1F requires scanning to be disabled
  5624. * before it will be operational. If scanning was not disabled before sending
  5625. * these PIP commands the FW will respond with a 6 byte error response. So to
  5626. * safely determine the scanning state, a PIP message that will not affect the
  5627. * operation of the FW was chosen:
  5628. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5629. * received scanning is active.
  5630. *
  5631. * RETURN:
  5632. * 0 = success
  5633. * !0 = failure
  5634. *
  5635. * PARAMETERS:
  5636. * *cd - pointer to core data
  5637. * *sys_mode - pointer to FW System mode
  5638. * *mode - pointer to mode (BL/FW)
  5639. ******************************************************************************/
  5640. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5641. {
  5642. int write_length = 1;
  5643. int report_length;
  5644. int rc = 0;
  5645. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5646. u8 tmp_mode = PT_MODE_UNKNOWN;
  5647. u8 param[1] = { PT_TCH_PARM_EBID };
  5648. struct pt_hid_output hid_output = {
  5649. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5650. .write_length = write_length,
  5651. .write_buf = param,
  5652. .novalidate = true,
  5653. };
  5654. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5655. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5656. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5657. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5658. __func__, tmp_sys_mode, tmp_mode);
  5659. if (!rc) {
  5660. if (tmp_mode != PT_MODE_OPERATIONAL)
  5661. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5662. }
  5663. goto exit;
  5664. }
  5665. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5666. if (cd->mode != PT_MODE_OPERATIONAL) {
  5667. tmp_mode = cd->mode;
  5668. goto exit;
  5669. }
  5670. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5671. if (rc)
  5672. goto exit;
  5673. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5674. if ((report_length == 0x06) &&
  5675. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5676. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5677. tmp_mode = PIP2_STATUS_APP_EXEC;
  5678. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5679. } else if ((report_length == 0x0A) &&
  5680. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5681. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5682. tmp_mode = PIP2_STATUS_APP_EXEC;
  5683. tmp_sys_mode = FW_SYS_MODE_TEST;
  5684. }
  5685. exit:
  5686. if (mode)
  5687. *mode = tmp_mode;
  5688. if (sys_mode)
  5689. *sys_mode = tmp_sys_mode;
  5690. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5691. __func__, tmp_mode, tmp_sys_mode);
  5692. return rc;
  5693. }
  5694. /*******************************************************************************
  5695. * FUNCTION: pt_get_fw_sys_mode
  5696. *
  5697. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5698. * is active or not.
  5699. *
  5700. * RETURN:
  5701. * 0 = success
  5702. * !0 = failure
  5703. *
  5704. * PARAMETERS:
  5705. * *cd - pointer to core data
  5706. * *sys_mode - pointer to fw system mode
  5707. * *mode - pointer to mode
  5708. ******************************************************************************/
  5709. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5710. {
  5711. int rc;
  5712. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5713. if (rc < 0) {
  5714. pt_debug(cd->dev, DL_ERROR,
  5715. "%s: fail get exclusive ex=%p own=%p\n",
  5716. __func__, cd->exclusive_dev, cd->dev);
  5717. return rc;
  5718. }
  5719. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5720. if (release_exclusive(cd, cd->dev) < 0)
  5721. pt_debug(cd->dev, DL_ERROR,
  5722. "%s: fail to release exclusive\n", __func__);
  5723. return rc;
  5724. }
  5725. /*******************************************************************************
  5726. * FUNCTION: _pt_request_get_fw_sys_mode
  5727. *
  5728. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5729. * to request to get scan state
  5730. *
  5731. * RETURN:
  5732. * 0 = success
  5733. * !0 = failure
  5734. *
  5735. * PARAMETERS:
  5736. * *dev - pointer to device structure
  5737. * protect - flag to call protected or non-protected
  5738. * *sys_mode - pointer to FW system mode
  5739. * *mode - pointer to mode
  5740. ******************************************************************************/
  5741. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5742. u8 *sys_mode, u8 *mode)
  5743. {
  5744. struct pt_core_data *cd = dev_get_drvdata(dev);
  5745. if (protect)
  5746. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5747. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5748. }
  5749. /* Default hid descriptor to provide basic register map */
  5750. const struct pt_hid_desc hid_desc_default = {
  5751. 230, /* hid_desc_len */
  5752. HID_APP_REPORT_ID, /* packet_id */
  5753. 0x00, /* reserved_byte */
  5754. 0x0100, /* bcd_version */
  5755. 0x00EC, /* report_desc_len */
  5756. 0x0002, /* report_desc_register */
  5757. 0x0003, /* input_register */
  5758. 0x00FE, /* max_input_len */
  5759. 0x0004, /* output_register */
  5760. 0x00FE, /* max_output_len */
  5761. 0x0005, /* command_register */
  5762. 0x0006, /* data_register */
  5763. 0x04B4, /* vendor_id */
  5764. 0xC101, /* product_id */
  5765. 0x0100, /* version_id */
  5766. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5767. };
  5768. /*******************************************************************************
  5769. * FUNCTION: pt_init_hid_descriptor
  5770. *
  5771. * SUMMARY: Setup default values for HID descriptor structure
  5772. *
  5773. *
  5774. * PARAMETERS:
  5775. * *desc - pointer to the HID descriptor data read back from DUT
  5776. ******************************************************************************/
  5777. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5778. {
  5779. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5780. }
  5781. /*******************************************************************************
  5782. * FUNCTION: pt_get_hid_descriptor_
  5783. *
  5784. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5785. * into the HID descriptor structure
  5786. *
  5787. * RETURN:
  5788. * 0 = success
  5789. * !0 = failure
  5790. *
  5791. * PARAMETERS:
  5792. * *cd - pointer to core data
  5793. * *desc - pointer to the HID descriptor data read back from DUT
  5794. ******************************************************************************/
  5795. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5796. struct pt_hid_desc *desc)
  5797. {
  5798. struct device *dev = cd->dev;
  5799. int rc = 0;
  5800. int t;
  5801. u8 cmd[2];
  5802. /*
  5803. * During startup the HID descriptor is required for all future
  5804. * processing. If IRQ is already asserted due to an early touch report
  5805. * the report must be cleared before sending command.
  5806. */
  5807. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5808. /* Read HID descriptor length and version */
  5809. mutex_lock(&cd->system_lock);
  5810. cd->hid_cmd_state = 1;
  5811. mutex_unlock(&cd->system_lock);
  5812. /* Set HID descriptor register */
  5813. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5814. sizeof(cd->hid_core.hid_desc_register));
  5815. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5816. __func__, sizeof(cmd));
  5817. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5818. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5819. if (rc) {
  5820. pt_debug(dev, DL_ERROR,
  5821. "%s: failed to get HID descriptor, rc=%d\n",
  5822. __func__, rc);
  5823. goto error;
  5824. }
  5825. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5826. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5827. if (IS_TMO(t)) {
  5828. #ifdef TTDL_DIAGNOSTICS
  5829. cd->bus_transmit_error_count++;
  5830. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5831. #endif /* TTDL_DIAGNOSTICS */
  5832. pt_debug(cd->dev, DL_ERROR,
  5833. "%s: HID get descriptor timed out\n", __func__);
  5834. rc = -ETIME;
  5835. goto error;
  5836. } else {
  5837. cd->hw_detected = true;
  5838. }
  5839. /* Load the HID descriptor including all registers */
  5840. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5841. /* Check HID descriptor length and version */
  5842. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5843. le16_to_cpu(desc->hid_desc_len),
  5844. le16_to_cpu(desc->bcd_version));
  5845. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5846. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5847. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5848. __func__);
  5849. return -ENODEV;
  5850. }
  5851. goto exit;
  5852. error:
  5853. mutex_lock(&cd->system_lock);
  5854. cd->hid_cmd_state = 0;
  5855. mutex_unlock(&cd->system_lock);
  5856. exit:
  5857. return rc;
  5858. }
  5859. /*******************************************************************************
  5860. * FUNCTION: pt_get_hid_descriptor
  5861. *
  5862. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5863. *
  5864. * RETURN:
  5865. * 0 = success
  5866. * !0 = failure
  5867. *
  5868. * PARAMETERS:
  5869. * *cd - pointer to core data
  5870. * *desc - pointer to the HID descriptor data read back from DUT
  5871. ******************************************************************************/
  5872. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5873. struct pt_hid_desc *desc)
  5874. {
  5875. int rc;
  5876. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5877. if (rc < 0) {
  5878. pt_debug(cd->dev, DL_ERROR,
  5879. "%s: fail get exclusive ex=%p own=%p\n",
  5880. __func__, cd->exclusive_dev, cd->dev);
  5881. return rc;
  5882. }
  5883. rc = pt_get_hid_descriptor_(cd, desc);
  5884. if (release_exclusive(cd, cd->dev) < 0)
  5885. pt_debug(cd->dev, DL_ERROR,
  5886. "%s: fail to release exclusive\n", __func__);
  5887. return rc;
  5888. }
  5889. /*******************************************************************************
  5890. * FUNCTION: pt_pip2_get_version_
  5891. *
  5892. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5893. * cd-ttdata
  5894. *
  5895. * RETURN:
  5896. * 0 = success
  5897. * !0 = failure
  5898. *
  5899. * PARAMETERS:
  5900. * *cd - pointer to core data
  5901. ******************************************************************************/
  5902. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5903. {
  5904. int rc = 0;
  5905. int status;
  5906. u8 read_buf[64];
  5907. u16 actual_read_len;
  5908. rc = _pt_request_pip2_send_cmd(cd->dev,
  5909. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5910. NULL, 0, read_buf, &actual_read_len);
  5911. if (rc) {
  5912. pt_debug(cd->dev, DL_ERROR,
  5913. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5914. __func__, rc);
  5915. return rc;
  5916. }
  5917. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5918. if (status == 0) {
  5919. /* Parse the PIP2 VERSION response into ttdata */
  5920. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5921. } else {
  5922. pt_debug(cd->dev, DL_ERROR,
  5923. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5924. __func__, status);
  5925. return status;
  5926. }
  5927. return rc;
  5928. }
  5929. /*******************************************************************************
  5930. * FUNCTION: pt_pip2_get_version
  5931. *
  5932. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5933. * access to the DUT.
  5934. *
  5935. * RETURN:
  5936. * 0 = success
  5937. * !0 = failure
  5938. *
  5939. * PARAMETERS:
  5940. * *cd - pointer to core data structure
  5941. ******************************************************************************/
  5942. static int pt_pip2_get_version(struct pt_core_data *cd)
  5943. {
  5944. int rc;
  5945. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5946. if (rc < 0) {
  5947. pt_debug(cd->dev, DL_ERROR,
  5948. "%s: fail get exclusive ex=%p own=%p\n",
  5949. __func__, cd->exclusive_dev, cd->dev);
  5950. return rc;
  5951. }
  5952. rc = pt_pip2_get_version_(cd);
  5953. if (release_exclusive(cd, cd->dev) < 0)
  5954. pt_debug(cd->dev, DL_ERROR,
  5955. "%s: fail to release exclusive\n", __func__);
  5956. return rc;
  5957. }
  5958. /*******************************************************************************
  5959. * FUNCTION: _pt_request_active_pip_protocol
  5960. *
  5961. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5962. * Function will return PIP version of BL or application based on
  5963. * when it's called.
  5964. *
  5965. * RETURN:
  5966. * 0 = success
  5967. * !0 = failure
  5968. *
  5969. * PARAMETERS:
  5970. * *dev - pointer to device structure
  5971. * protect - flag to run in protected mode
  5972. * *pip_version_major - pointer to store PIP major version
  5973. * *pip_version_minor - pointer to store PIP minor version
  5974. ******************************************************************************/
  5975. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5976. u8 *pip_version_major, u8 *pip_version_minor)
  5977. {
  5978. struct pt_core_data *cd = dev_get_drvdata(dev);
  5979. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5980. int rc = 0;
  5981. struct pt_hid_output sys_info = {
  5982. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5983. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5984. };
  5985. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5986. /* Skip PIP2 command if DUT generation is confirmed */
  5987. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5988. goto skip_pip2_command;
  5989. rc = pt_pip2_get_version_(cd);
  5990. if (!rc) {
  5991. *pip_version_major = ttdata->pip_ver_major;
  5992. *pip_version_minor = ttdata->pip_ver_minor;
  5993. pt_debug(dev, DL_INFO,
  5994. "%s: pip_version = %d.%d\n", __func__,
  5995. *pip_version_major, *pip_version_minor);
  5996. } else {
  5997. /*
  5998. * Legacy products do not support the pip2 protocol to get
  5999. * pip version. However, they do support the "get sysinfo"
  6000. * command to get pip version from FW, but the bootloader
  6001. * does not support it. This function will try "get sysinfo"
  6002. * command if the pip2 command failed but this cmd could also
  6003. * fail if DUT is stuck in bootloader mode.
  6004. */
  6005. pt_debug(dev, DL_INFO,
  6006. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6007. __func__, rc);
  6008. skip_pip2_command:
  6009. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6010. if (!rc) {
  6011. *pip_version_minor =
  6012. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6013. *pip_version_major =
  6014. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6015. pt_debug(dev, DL_INFO,
  6016. "%s: pip_version = %d.%d\n", __func__,
  6017. *pip_version_major, *pip_version_minor);
  6018. } else {
  6019. *pip_version_major = 0;
  6020. *pip_version_minor = 0;
  6021. pt_debug(dev, DL_ERROR,
  6022. "%s: pip_version Not Detected\n", __func__);
  6023. }
  6024. }
  6025. return rc;
  6026. }
  6027. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6028. /*******************************************************************************
  6029. * FUNCTION: _pt_detect_dut_generation
  6030. *
  6031. * SUMMARY: Determine the generation of device that we are communicating with:
  6032. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6033. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6034. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6035. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6036. * coming form the BL or the FW:
  6037. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6038. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6039. * This function will return a modified status if it detects the DUT
  6040. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6041. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6042. *
  6043. * RETURN:
  6044. * 0 = success
  6045. * !0 = failure
  6046. *
  6047. * PARAMETERS:
  6048. * *dev - pointer to device structure
  6049. * *status - pointer to status bitmask
  6050. * *dut_gen - pointer to store the dut_generation
  6051. * *mode - pointer to store the PT_MODE
  6052. ******************************************************************************/
  6053. static int _pt_detect_dut_generation(struct device *dev,
  6054. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6055. {
  6056. int rc = 0;
  6057. u8 dut_gen_tmp = DUT_UNKNOWN;
  6058. u8 mode_tmp = PT_MODE_UNKNOWN;
  6059. u8 attempt = 1;
  6060. u32 status_tmp = STARTUP_STATUS_START;
  6061. struct pt_core_data *cd = dev_get_drvdata(dev);
  6062. struct pt_hid_desc hid_desc;
  6063. memset(&hid_desc, 0, sizeof(hid_desc));
  6064. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6065. while (rc && attempt < 3) {
  6066. attempt++;
  6067. usleep_range(2000, 5000);
  6068. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6069. }
  6070. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6071. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6072. mode_tmp = PT_MODE_BOOTLOADER;
  6073. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6074. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6075. rc = pt_pip2_get_version_(cd);
  6076. if (!rc)
  6077. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6078. else
  6079. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6080. mode_tmp = PT_MODE_OPERATIONAL;
  6081. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6082. rc = 0; /* To return success instead of error code */
  6083. } else if (rc) {
  6084. rc = pt_pip2_get_version_(cd);
  6085. if (!rc) {
  6086. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6087. mode_tmp = PT_MODE_BOOTLOADER;
  6088. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6089. }
  6090. }
  6091. mutex_lock(&cd->system_lock);
  6092. if (dut_gen)
  6093. *dut_gen = dut_gen_tmp;
  6094. if (mode)
  6095. *mode = mode_tmp;
  6096. if (status)
  6097. *status = status_tmp;
  6098. mutex_unlock(&cd->system_lock);
  6099. #ifdef TTDL_DIAGNOSTICS
  6100. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6101. __func__, dut_gen_tmp, mode_tmp);
  6102. #endif /* TTDL_DIAGNOSTICS */
  6103. return rc;
  6104. }
  6105. /*******************************************************************************
  6106. * FUNCTION: _pt_request_dut_generation
  6107. *
  6108. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6109. * to get current dut generation.
  6110. *
  6111. * NOTE: This function WILL NOT try to determine dut generation.
  6112. *
  6113. * RETURN:
  6114. * The current dut generation.
  6115. *
  6116. * PARAMETERS:
  6117. * *dev - pointer to device structure
  6118. ******************************************************************************/
  6119. static int _pt_request_dut_generation(struct device *dev)
  6120. {
  6121. struct pt_core_data *cd = dev_get_drvdata(dev);
  6122. return cd->active_dut_generation;
  6123. }
  6124. #define HW_VERSION_LEN_MAX 13
  6125. /*******************************************************************************
  6126. * FUNCTION: _legacy_generate_hw_version
  6127. *
  6128. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6129. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6130. * command) to the hw_version.
  6131. *
  6132. * RETURN:
  6133. * 0 = success
  6134. * !0 = failure
  6135. *
  6136. * PARAMETERS:
  6137. * *cd - pointer to core data
  6138. * *hw_version - pointer to store the hardware version
  6139. ******************************************************************************/
  6140. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6141. char *hw_version)
  6142. {
  6143. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6144. if (cd->sysinfo.ready) {
  6145. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6146. ttdata->jtag_id_h, cd->pid_for_loader);
  6147. return 0;
  6148. } else if (cd->bl_info.ready) {
  6149. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6150. cd->bl_info.chip_id, cd->pid_for_loader);
  6151. return 0;
  6152. } else {
  6153. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6154. pt_debug(cd->dev, DL_ERROR,
  6155. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6156. return -ENODATA;
  6157. }
  6158. }
  6159. /*******************************************************************************
  6160. * FUNCTION: _pip2_generate_hw_version
  6161. *
  6162. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6163. * VERSION command) to the hw_version.
  6164. *
  6165. * RETURN:
  6166. * 0 = success
  6167. * !0 = failure
  6168. *
  6169. * PARAMETERS:
  6170. * *cd - pointer to core data
  6171. * *hw_version - pointer to store the hardware version
  6172. ******************************************************************************/
  6173. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6174. {
  6175. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6176. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6177. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6178. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6179. return 0;
  6180. } else {
  6181. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6182. pt_debug(cd->dev, DL_ERROR,
  6183. "%s: PIP Version are not ready\n", __func__);
  6184. return -ENODATA;
  6185. }
  6186. }
  6187. /*******************************************************************************
  6188. * FUNCTION: pt_generate_hw_version
  6189. *
  6190. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6191. * hw_version from static data.
  6192. *
  6193. * RETURN:
  6194. * 0 = success
  6195. * !0 = failure
  6196. *
  6197. * PARAMETERS:
  6198. * *cd - pointer to core data
  6199. * *hw_version - pointer to store the hardware version
  6200. ******************************************************************************/
  6201. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6202. {
  6203. int rc = 0;
  6204. if (!hw_version)
  6205. return -ENOMEM;
  6206. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6207. rc = _legacy_generate_hw_version(cd, hw_version);
  6208. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6209. rc = _pip2_generate_hw_version(cd, hw_version);
  6210. else {
  6211. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6212. rc = -ENODATA;
  6213. }
  6214. return rc;
  6215. }
  6216. /*******************************************************************************
  6217. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6218. *
  6219. * NOTE: The calling function must ensure to free *hw_version
  6220. *
  6221. * RETURN:
  6222. * 0 = success
  6223. * !0 = Failure
  6224. *
  6225. * PARAMETERS:
  6226. * *dev - pointer to device structure
  6227. * *hw_version - pointer to where the hw_version string will be stored
  6228. ******************************************************************************/
  6229. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6230. {
  6231. int rc = 0;
  6232. u16 actual_read_len;
  6233. u16 pip_ver;
  6234. u8 rd_buf[256];
  6235. u8 status;
  6236. u8 index = PIP2_RESP_STATUS_OFFSET;
  6237. u8 return_data[8];
  6238. u8 panel_id;
  6239. struct pt_core_data *cd = dev_get_drvdata(dev);
  6240. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6241. if (!hw_version)
  6242. return -ENOMEM;
  6243. if (!cd->hw_detected) {
  6244. /* No HW detected */
  6245. rc = -ENODEV;
  6246. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6247. __func__);
  6248. goto exit_error;
  6249. }
  6250. /* For Parade TC or TT parts */
  6251. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6252. rc = _pt_request_pip2_send_cmd(dev,
  6253. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6254. NULL, 0, rd_buf, &actual_read_len);
  6255. if (rc) {
  6256. pt_debug(dev, DL_ERROR,
  6257. "%s: Failed to send PIP2 VERSION cmd\n",
  6258. __func__);
  6259. goto exit_error;
  6260. }
  6261. status = rd_buf[index];
  6262. if (status == 0) {
  6263. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6264. /*
  6265. * BL PIP 2.02 and greater the version fields are
  6266. * swapped
  6267. */
  6268. if (pip_ver >= 0x0202) {
  6269. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6270. "%02X%02X.%02X%02X.FF",
  6271. rd_buf[index + 10], rd_buf[index + 9],
  6272. rd_buf[index + 8], rd_buf[index + 7]);
  6273. } else {
  6274. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6275. "%02X%02X.%02X%02X.FF",
  6276. rd_buf[index + 8], rd_buf[index + 7],
  6277. rd_buf[index + 10], rd_buf[index + 9]);
  6278. }
  6279. return STATUS_SUCCESS;
  6280. } else {
  6281. rc = status;
  6282. pt_debug(dev, DL_WARN,
  6283. "%s: PIP2 VERSION cmd response error\n",
  6284. __func__);
  6285. }
  6286. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6287. /*
  6288. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6289. * hard coded to FFFF
  6290. */
  6291. if (cd->mode == PT_MODE_OPERATIONAL) {
  6292. rc = pt_hid_output_get_sysinfo(cd);
  6293. if (!rc) {
  6294. panel_id =
  6295. cd->sysinfo.sensing_conf_data.panel_id;
  6296. } else {
  6297. panel_id = PANEL_ID_NOT_ENABLED;
  6298. }
  6299. /* In FW - simply retrieve from ttdata struct */
  6300. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6301. "%04X.FFFF.%02X",
  6302. ttdata->jtag_id_h,
  6303. panel_id);
  6304. return STATUS_SUCCESS;
  6305. } else {
  6306. /*
  6307. * Return the stored value if PT_PANEL_ID_BY_BL
  6308. * is not supported while other feature is.
  6309. */
  6310. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6311. rc = pt_hid_output_bl_get_information(
  6312. cd, return_data);
  6313. if (!rc) {
  6314. rc = pt_hid_output_bl_get_panel_id(
  6315. cd, &panel_id);
  6316. }
  6317. } else
  6318. panel_id = cd->pid_for_loader;
  6319. if (!rc) {
  6320. snprintf(hw_version,
  6321. HW_VERSION_LEN_MAX,
  6322. "%02X%02X.FFFF.%02X",
  6323. return_data[3], return_data[2],
  6324. panel_id);
  6325. return STATUS_SUCCESS;
  6326. }
  6327. }
  6328. } else {
  6329. /* Unknown generation */
  6330. rc = -ENODEV;
  6331. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6332. __func__);
  6333. }
  6334. exit_error:
  6335. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6336. return rc;
  6337. }
  6338. /*******************************************************************************
  6339. * FUNCTION: pt_start_wd_timer
  6340. *
  6341. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6342. *
  6343. * RETURN: void
  6344. *
  6345. * PARAMETERS:
  6346. * *cd - pointer to core data
  6347. ******************************************************************************/
  6348. static void pt_start_wd_timer(struct pt_core_data *cd)
  6349. {
  6350. if (cd->watchdog_interval < 100) {
  6351. pt_debug(cd->dev, DL_ERROR,
  6352. "%s: WARNING: Invalid watchdog interval: %d\n",
  6353. __func__, cd->watchdog_interval);
  6354. return;
  6355. }
  6356. if (cd->watchdog_force_stop) {
  6357. pt_debug(cd->dev, DL_INFO,
  6358. "%s: TTDL WD Forced stop\n", __func__);
  6359. return;
  6360. }
  6361. mod_timer(&cd->watchdog_timer, jiffies +
  6362. msecs_to_jiffies(cd->watchdog_interval));
  6363. cd->watchdog_enabled = 1;
  6364. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6365. }
  6366. /*******************************************************************************
  6367. * FUNCTION: pt_stop_wd_timer
  6368. *
  6369. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6370. *
  6371. * RETURN: void
  6372. *
  6373. * PARAMETERS:
  6374. * *cd - pointer to core data
  6375. ******************************************************************************/
  6376. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6377. {
  6378. if (!cd->watchdog_interval)
  6379. return;
  6380. /*
  6381. * Ensure we wait until the watchdog timer
  6382. * running on a different CPU finishes
  6383. */
  6384. del_timer_sync(&cd->watchdog_timer);
  6385. cancel_work_sync(&cd->watchdog_work);
  6386. del_timer_sync(&cd->watchdog_timer);
  6387. cd->watchdog_enabled = 0;
  6388. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6389. }
  6390. /*******************************************************************************
  6391. * FUNCTION: pt_hw_soft_reset
  6392. *
  6393. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6394. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6395. * schedule an enum if it fires when the DUT is being reset.
  6396. * This can cause a double reset.
  6397. *
  6398. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6399. * the WD active could cause this function to fail!
  6400. *
  6401. * RETURN:
  6402. * 0 = success
  6403. * !0 = failure
  6404. *
  6405. * PARAMETERS:
  6406. * *cd - pointer to core data struct
  6407. * protect - flag to call protected or non-protected
  6408. ******************************************************************************/
  6409. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6410. {
  6411. int rc = 0;
  6412. mutex_lock(&cd->system_lock);
  6413. cd->startup_status = STARTUP_STATUS_START;
  6414. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6415. mutex_unlock(&cd->system_lock);
  6416. if (protect)
  6417. rc = pt_hid_cmd_reset(cd);
  6418. else
  6419. rc = pt_hid_cmd_reset_(cd);
  6420. if (rc < 0) {
  6421. pt_debug(cd->dev, DL_ERROR,
  6422. "%s: FAILED to execute SOFT reset\n", __func__);
  6423. return rc;
  6424. }
  6425. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6426. __func__);
  6427. return 0;
  6428. }
  6429. /*******************************************************************************
  6430. * FUNCTION: pt_hw_hard_reset
  6431. *
  6432. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6433. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6434. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6435. * schedule an enum if it fires when the DUT is being reset.
  6436. * This can cause a double reset.
  6437. *
  6438. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6439. * the WD active could cause this function to fail!
  6440. *
  6441. * RETURN:
  6442. * 0 = success
  6443. * !0 = failure
  6444. *
  6445. * PARAMETERS:
  6446. * *cd - pointer to core data struct
  6447. ******************************************************************************/
  6448. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6449. {
  6450. if (cd->cpdata->xres) {
  6451. cd->startup_status = STARTUP_STATUS_START;
  6452. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6453. __func__);
  6454. cd->cpdata->xres(cd->cpdata, cd->dev);
  6455. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6456. __func__);
  6457. return 0;
  6458. }
  6459. pt_debug(cd->dev, DL_ERROR,
  6460. "%s: FAILED to execute HARD reset\n", __func__);
  6461. return -ENODEV;
  6462. }
  6463. /*******************************************************************************
  6464. * FUNCTION: pt_dut_reset
  6465. *
  6466. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6467. * soft reset.
  6468. *
  6469. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6470. * the WD active could cause this function to fail!
  6471. * NOTE: "protect" flag is only used for soft reset.
  6472. *
  6473. * RETURN:
  6474. * 0 = success
  6475. * !0 = failure
  6476. *
  6477. * PARAMETERS:
  6478. * *cd - pointer to core data structure
  6479. * protect - flag to call protected or non-protected
  6480. ******************************************************************************/
  6481. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6482. {
  6483. int rc = 0;
  6484. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6485. mutex_lock(&cd->system_lock);
  6486. cd->hid_reset_cmd_state = 1;
  6487. rc = pt_hw_hard_reset(cd);
  6488. mutex_unlock(&cd->system_lock);
  6489. if (rc == -ENODEV) {
  6490. mutex_lock(&cd->system_lock);
  6491. cd->hid_reset_cmd_state = 0;
  6492. mutex_unlock(&cd->system_lock);
  6493. pt_debug(cd->dev, DL_ERROR,
  6494. "%s: Hard reset failed, try soft reset\n", __func__);
  6495. rc = pt_hw_soft_reset(cd, protect);
  6496. }
  6497. if (rc)
  6498. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6499. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6500. return rc;
  6501. }
  6502. /*******************************************************************************
  6503. * FUNCTION: pt_dut_reset_and_wait
  6504. *
  6505. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6506. * complete
  6507. *
  6508. * RETURN:
  6509. * 0 = success
  6510. * !0 = failure
  6511. *
  6512. * PARAMETERS:
  6513. * *cd - pointer to core data structure
  6514. ******************************************************************************/
  6515. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6516. {
  6517. int rc = 0;
  6518. int t;
  6519. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6520. if (rc < 0)
  6521. goto exit;
  6522. t = wait_event_timeout(cd->wait_q,
  6523. (cd->hid_reset_cmd_state == 0),
  6524. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6525. if (IS_TMO(t)) {
  6526. #ifdef TTDL_DIAGNOSTICS
  6527. cd->bus_transmit_error_count++;
  6528. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6529. #endif /* TTDL_DIAGNOSTICS */
  6530. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6531. __func__);
  6532. rc = -ETIME;
  6533. goto exit;
  6534. }
  6535. exit:
  6536. return rc;
  6537. }
  6538. /*
  6539. * touch default parameters (from report descriptor) to resolve protocol for
  6540. * touch report
  6541. */
  6542. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6543. /* byte offset, size, min, max, bit offset, report */
  6544. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6545. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6546. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6547. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6548. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6549. };
  6550. /*
  6551. * button default parameters (from report descriptor) to resolve protocol for
  6552. * button report
  6553. */
  6554. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6555. /* byte offset, size, min, max, bit offset, report */
  6556. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6557. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6558. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6559. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6560. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6561. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6562. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6563. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6564. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6565. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6566. };
  6567. /*******************************************************************************
  6568. * FUNCTION: pt_init_pip_report_fields
  6569. *
  6570. * SUMMARY: Setup default values for touch/button report parsing.
  6571. *
  6572. * PARAMETERS:
  6573. * *cd - pointer to core data structure
  6574. ******************************************************************************/
  6575. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6576. {
  6577. struct pt_sysinfo *si = &cd->sysinfo;
  6578. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6579. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6580. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6581. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6582. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6583. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6584. cd->features.easywake = 1;
  6585. cd->features.noise_metric = 1;
  6586. cd->features.tracking_heatmap = 1;
  6587. cd->features.sensor_data = 1;
  6588. }
  6589. /*******************************************************************************
  6590. * FUNCTION: pt_get_mode
  6591. *
  6592. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6593. * message
  6594. *
  6595. * RETURN: Enum of the current mode
  6596. *
  6597. * PARAMETERS:
  6598. * *cd - pointer to the Core Data structure
  6599. * protect - run command in protected mode
  6600. * *mode - pointer to store the retrieved mode
  6601. ******************************************************************************/
  6602. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6603. {
  6604. if (desc->packet_id == HID_APP_REPORT_ID)
  6605. return PT_MODE_OPERATIONAL;
  6606. else if (desc->packet_id == HID_BL_REPORT_ID)
  6607. return PT_MODE_BOOTLOADER;
  6608. return PT_MODE_UNKNOWN;
  6609. }
  6610. /*******************************************************************************
  6611. * FUNCTION: _pt_request_get_mode
  6612. *
  6613. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6614. * to determine the current mode of the DUT by use of the Get HID
  6615. * Descriptor command.
  6616. *
  6617. * RETURN:
  6618. * 0 = success
  6619. * !0 = failure
  6620. *
  6621. * PARAMETERS:
  6622. * *dev - pointer to device structure
  6623. * protect - run command in protected mode
  6624. * *mode - pointer to store the retrieved mode
  6625. ******************************************************************************/
  6626. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6627. {
  6628. struct pt_core_data *cd = dev_get_drvdata(dev);
  6629. struct pt_hid_desc hid_desc;
  6630. int rc = 0;
  6631. memset(&hid_desc, 0, sizeof(hid_desc));
  6632. if (protect)
  6633. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6634. else
  6635. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6636. if (rc)
  6637. *mode = PT_MODE_UNKNOWN;
  6638. else
  6639. *mode = pt_get_mode(cd, &hid_desc);
  6640. return rc;
  6641. }
  6642. /*******************************************************************************
  6643. * FUNCTION: pt_queue_enum_
  6644. *
  6645. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6646. * function. It won't try to add/delete sysfs node or modules.
  6647. *
  6648. * RETURN: void
  6649. *
  6650. * PARAMETERS:
  6651. * *cd - pointer to core data
  6652. ******************************************************************************/
  6653. static void pt_queue_enum_(struct pt_core_data *cd)
  6654. {
  6655. if (cd->startup_state == STARTUP_NONE) {
  6656. cd->startup_state = STARTUP_QUEUED;
  6657. #ifdef TTDL_DIAGNOSTICS
  6658. if (!cd->bridge_mode)
  6659. schedule_work(&cd->enum_work);
  6660. else
  6661. cd->startup_state = STARTUP_NONE;
  6662. #else
  6663. schedule_work(&cd->enum_work);
  6664. #endif
  6665. pt_debug(cd->dev, DL_INFO,
  6666. "%s: enum_work queued\n", __func__);
  6667. } else {
  6668. pt_debug(cd->dev, DL_WARN,
  6669. "%s: Enum not queued - startup_state = %d\n",
  6670. __func__, cd->startup_state);
  6671. }
  6672. }
  6673. /*******************************************************************************
  6674. * FUNCTION: pt_queue_enum
  6675. *
  6676. * SUMMARY: Queues a TTDL enum within a mutex lock
  6677. *
  6678. * RETURN: void
  6679. *
  6680. * PARAMETERS:
  6681. * *cd - pointer to core data
  6682. ******************************************************************************/
  6683. static void pt_queue_enum(struct pt_core_data *cd)
  6684. {
  6685. mutex_lock(&cd->system_lock);
  6686. pt_queue_enum_(cd);
  6687. mutex_unlock(&cd->system_lock);
  6688. }
  6689. static void remove_sysfs_and_modules(struct device *dev);
  6690. /*******************************************************************************
  6691. * FUNCTION: pt_queue_restart
  6692. *
  6693. * SUMMARY: Queues a TTDL restart within a mutex lock
  6694. *
  6695. * RETURN: void
  6696. *
  6697. * PARAMETERS:
  6698. * *cd - pointer to core data
  6699. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6700. * False: will not perform remove action
  6701. ******************************************************************************/
  6702. static void pt_queue_restart(struct pt_core_data *cd)
  6703. {
  6704. mutex_lock(&cd->system_lock);
  6705. if (cd->startup_state == STARTUP_NONE) {
  6706. cd->startup_state = STARTUP_QUEUED;
  6707. schedule_work(&cd->ttdl_restart_work);
  6708. pt_debug(cd->dev, DL_INFO,
  6709. "%s: pt_ttdl_restart queued\n", __func__);
  6710. } else {
  6711. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6712. __func__, cd->startup_state);
  6713. }
  6714. mutex_unlock(&cd->system_lock);
  6715. }
  6716. /*******************************************************************************
  6717. * FUNCTION: call_atten_cb
  6718. *
  6719. * SUMMARY: Iterate over attention list call the function that registered.
  6720. *
  6721. * RETURN: void
  6722. *
  6723. * PARAMETERS:
  6724. * *cd - pointer to core data
  6725. * type - type of attention list
  6726. * mode - condition for execution
  6727. ******************************************************************************/
  6728. static void call_atten_cb(struct pt_core_data *cd,
  6729. enum pt_atten_type type, int mode)
  6730. {
  6731. struct atten_node *atten, *atten_n;
  6732. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6733. __func__, type, mode);
  6734. spin_lock(&cd->spinlock);
  6735. list_for_each_entry_safe(atten, atten_n,
  6736. &cd->atten_list[type], node) {
  6737. if (!mode || atten->mode & mode) {
  6738. spin_unlock(&cd->spinlock);
  6739. pt_debug(cd->dev, DL_DEBUG,
  6740. "%s: attention for '%s'",
  6741. __func__, dev_name(atten->dev));
  6742. atten->func(atten->dev);
  6743. spin_lock(&cd->spinlock);
  6744. }
  6745. }
  6746. spin_unlock(&cd->spinlock);
  6747. }
  6748. /*******************************************************************************
  6749. * FUNCTION: start_fw_upgrade
  6750. *
  6751. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6752. * start firmware upgrade.
  6753. *
  6754. * RETURN:
  6755. * 0 = success
  6756. *
  6757. * PARAMETERS:
  6758. * *data - pointer to core data
  6759. ******************************************************************************/
  6760. static int start_fw_upgrade(void *data)
  6761. {
  6762. struct pt_core_data *cd = (struct pt_core_data *)data;
  6763. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6764. return 0;
  6765. }
  6766. /*******************************************************************************
  6767. * FUNCTION: pt_put_device_into_easy_wakeup_
  6768. *
  6769. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6770. * wake up state.
  6771. *
  6772. * RETURN:
  6773. * 0 = success
  6774. * !0 = failure
  6775. *
  6776. * PARAMETERS:
  6777. * *cd - pointer to core data
  6778. ******************************************************************************/
  6779. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6780. {
  6781. int rc = 0;
  6782. u8 status = 0;
  6783. mutex_lock(&cd->system_lock);
  6784. cd->wait_until_wake = 0;
  6785. mutex_unlock(&cd->system_lock);
  6786. rc = pt_hid_output_enter_easywake_state_(cd,
  6787. cd->easy_wakeup_gesture, &status);
  6788. if (rc || status == 0)
  6789. return -EBUSY;
  6790. return rc;
  6791. }
  6792. /*******************************************************************************
  6793. * FUNCTION: pt_put_device_into_deep_sleep_
  6794. *
  6795. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6796. *
  6797. * RETURN:
  6798. * 0 = success
  6799. * !0 = error
  6800. *
  6801. * PARAMETERS:
  6802. * *cd - pointer to core data
  6803. ******************************************************************************/
  6804. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6805. {
  6806. int rc = 0;
  6807. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6808. if (rc)
  6809. rc = -EBUSY;
  6810. return rc;
  6811. }
  6812. /*******************************************************************************
  6813. * FUNCTION: pt_put_device_into_deep_standby_
  6814. *
  6815. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6816. *
  6817. * RETURN:
  6818. * 0 = success
  6819. * !0 = error
  6820. *
  6821. * PARAMETERS:
  6822. * *cd - pointer to core data
  6823. ******************************************************************************/
  6824. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6825. {
  6826. int rc = 0;
  6827. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6828. if (rc)
  6829. rc = -EBUSY;
  6830. return rc;
  6831. }
  6832. /*******************************************************************************
  6833. * FUNCTION: pt_core_poweroff_device_
  6834. *
  6835. * SUMMARY: Disable IRQ and HW power down the device.
  6836. *
  6837. * RETURN:
  6838. * 0 = success
  6839. * !0 = failure
  6840. *
  6841. * PARAMETERS:
  6842. * *cd - pointer to core data
  6843. ******************************************************************************/
  6844. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6845. {
  6846. int rc;
  6847. if (cd->irq_enabled) {
  6848. cd->irq_enabled = false;
  6849. disable_irq_nosync(cd->irq);
  6850. }
  6851. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6852. if (rc < 0)
  6853. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6854. __func__, rc);
  6855. return rc;
  6856. }
  6857. /*******************************************************************************
  6858. * FUNCTION: pt_core_sleep_
  6859. *
  6860. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6861. * configuration in the core platform data structure.
  6862. *
  6863. * RETURN:
  6864. * 0 = success
  6865. * !0 = failure
  6866. *
  6867. * PARAMETERS:
  6868. * *cd - pointer to core data
  6869. ******************************************************************************/
  6870. static int pt_core_sleep_(struct pt_core_data *cd)
  6871. {
  6872. int rc = 0;
  6873. mutex_lock(&cd->system_lock);
  6874. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6875. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  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. } else {
  6881. cd->sleep_state = SS_SLEEPING;
  6882. }
  6883. mutex_unlock(&cd->system_lock);
  6884. /* Ensure watchdog and startup works stopped */
  6885. pt_stop_wd_timer(cd);
  6886. cancel_work_sync(&cd->enum_work);
  6887. pt_stop_wd_timer(cd);
  6888. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6889. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6890. rc = pt_core_poweroff_device_(cd);
  6891. if (rc)
  6892. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6893. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6894. pt_debug(cd->dev, DL_INFO,
  6895. "%s: Entering into deep standby mode:\n", __func__);
  6896. rc = pt_put_device_into_deep_standby_(cd);
  6897. if (rc)
  6898. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6899. } else {
  6900. pt_debug(cd->dev, DL_INFO,
  6901. "%s: Entering into deep sleep mode:\n", __func__);
  6902. rc = pt_put_device_into_deep_sleep_(cd);
  6903. if (rc)
  6904. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6905. }
  6906. mutex_lock(&cd->system_lock);
  6907. cd->sleep_state = SS_SLEEP_ON;
  6908. mutex_unlock(&cd->system_lock);
  6909. return rc;
  6910. }
  6911. /*******************************************************************************
  6912. * FUNCTION: pt_core_easywake_on_
  6913. *
  6914. * SUMMARY: Suspend the device with easy wake on the
  6915. * configuration in the core platform data structure.
  6916. *
  6917. * RETURN:
  6918. * 0 = success
  6919. * !0 = failure
  6920. *
  6921. * PARAMETERS:
  6922. * *cd - pointer to core data
  6923. ******************************************************************************/
  6924. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6925. {
  6926. int rc = 0;
  6927. mutex_lock(&cd->system_lock);
  6928. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6929. mutex_unlock(&cd->system_lock);
  6930. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6931. __func__, cd->sleep_state);
  6932. return 0;
  6933. }
  6934. mutex_unlock(&cd->system_lock);
  6935. /* Ensure watchdog and startup works stopped */
  6936. pt_stop_wd_timer(cd);
  6937. cancel_work_sync(&cd->enum_work);
  6938. pt_stop_wd_timer(cd);
  6939. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6940. rc = pt_put_device_into_easy_wakeup_(cd);
  6941. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6942. if (rc)
  6943. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6944. }
  6945. mutex_lock(&cd->system_lock);
  6946. cd->sleep_state = SS_EASY_WAKING_ON;
  6947. mutex_unlock(&cd->system_lock);
  6948. return rc;
  6949. }
  6950. /*******************************************************************************
  6951. * FUNCTION: pt_core_easywake_on
  6952. *
  6953. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6954. *
  6955. * RETURN:
  6956. * 0 = success
  6957. * !0 = failure
  6958. *
  6959. * PARAMETERS:
  6960. * *cd - pointer to core data
  6961. ******************************************************************************/
  6962. static int pt_core_easywake_on(struct pt_core_data *cd)
  6963. {
  6964. int rc = 0;
  6965. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6966. if (rc < 0) {
  6967. pt_debug(cd->dev, DL_ERROR,
  6968. "%s: fail get exclusive ex=%p own=%p\n",
  6969. __func__, cd->exclusive_dev, cd->dev);
  6970. return rc;
  6971. }
  6972. rc = pt_core_easywake_on_(cd);
  6973. if (release_exclusive(cd, cd->dev) < 0)
  6974. pt_debug(cd->dev, DL_ERROR,
  6975. "%s: fail to release exclusive\n", __func__);
  6976. else
  6977. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6978. __func__);
  6979. return rc;
  6980. }
  6981. /*******************************************************************************
  6982. * FUNCTION: pt_core_sleep
  6983. *
  6984. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6985. *
  6986. * RETURN:
  6987. * 0 = success
  6988. * !0 = failure
  6989. *
  6990. * PARAMETERS:
  6991. * *cd - pointer to core data
  6992. ******************************************************************************/
  6993. static int pt_core_sleep(struct pt_core_data *cd)
  6994. {
  6995. int rc = 0;
  6996. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6997. if (rc < 0) {
  6998. pt_debug(cd->dev, DL_ERROR,
  6999. "%s: fail get exclusive ex=%p own=%p\n",
  7000. __func__, cd->exclusive_dev, cd->dev);
  7001. return rc;
  7002. }
  7003. rc = pt_core_sleep_(cd);
  7004. if (release_exclusive(cd, cd->dev) < 0)
  7005. pt_debug(cd->dev, DL_ERROR,
  7006. "%s: fail to release exclusive\n", __func__);
  7007. else
  7008. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7009. __func__);
  7010. return rc;
  7011. }
  7012. /*******************************************************************************
  7013. * FUNCTION: pt_wakeup_host
  7014. *
  7015. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7016. *
  7017. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7018. *
  7019. * RETURN:
  7020. * 0 = success
  7021. * !0 = failure
  7022. *
  7023. * PARAMETERS:
  7024. * *cd - pointer to core data
  7025. ******************************************************************************/
  7026. static int pt_wakeup_host(struct pt_core_data *cd)
  7027. {
  7028. #ifndef EASYWAKE_TSG6
  7029. /* TSG5 EasyWake */
  7030. int rc = 0;
  7031. int event_id;
  7032. int size = get_unaligned_le16(&cd->input_buf[0]);
  7033. /* Validate report */
  7034. if (size != 4 || cd->input_buf[2] != 4)
  7035. rc = -EINVAL;
  7036. event_id = cd->input_buf[3];
  7037. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7038. __func__, event_id, rc);
  7039. if (rc) {
  7040. pt_core_sleep_(cd);
  7041. goto exit;
  7042. }
  7043. /* attention WAKE */
  7044. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7045. exit:
  7046. return rc;
  7047. #else
  7048. /* TSG6 FW1.3 EasyWake */
  7049. int rc = 0;
  7050. int i = 0;
  7051. int report_length;
  7052. /* Validate report */
  7053. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7054. rc = -EINVAL;
  7055. pt_core_sleep_(cd);
  7056. goto exit;
  7057. }
  7058. /* Get gesture id and gesture data length */
  7059. cd->gesture_id = cd->input_buf[3];
  7060. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7061. cd->gesture_data_length = report_length - 4;
  7062. pt_debug(cd->dev, DL_INFO,
  7063. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7064. __func__, cd->gesture_id, cd->gesture_data_length);
  7065. for (i = 0; i < cd->gesture_data_length; i++)
  7066. cd->gesture_data[i] = cd->input_buf[4 + i];
  7067. /* attention WAKE */
  7068. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7069. exit:
  7070. return rc;
  7071. #endif
  7072. }
  7073. /*******************************************************************************
  7074. * FUNCTION: pt_get_touch_axis
  7075. *
  7076. * SUMMARY: Function to calculate touch axis
  7077. *
  7078. * PARAMETERS:
  7079. * *cd - pointer to core data structure
  7080. * *axis - pointer to axis calculation result
  7081. * size - size in bytes
  7082. * max - max value of result
  7083. * *xy_data - pointer to input data to be parsed
  7084. * bofs - bit offset
  7085. ******************************************************************************/
  7086. static void pt_get_touch_axis(struct pt_core_data *cd,
  7087. int *axis, int size, int max, u8 *data, int bofs)
  7088. {
  7089. int nbyte;
  7090. int next;
  7091. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7092. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7093. next++;
  7094. }
  7095. *axis &= max - 1;
  7096. }
  7097. /*******************************************************************************
  7098. * FUNCTION: move_tracking_heatmap_data
  7099. *
  7100. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7101. * system information structure, xy_mode and xy_data.
  7102. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7103. * the tthe_tuner sysfs node under the label "THM"
  7104. *
  7105. * RETURN:
  7106. * 0 = success
  7107. * !0 = failure
  7108. *
  7109. * PARAMETERS:
  7110. * *cd - pointer to core data
  7111. * *si - pointer to the system information structure
  7112. ******************************************************************************/
  7113. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7114. struct pt_sysinfo *si)
  7115. {
  7116. #ifdef TTHE_TUNER_SUPPORT
  7117. int size = get_unaligned_le16(&cd->input_buf[0]);
  7118. if (size)
  7119. tthe_print(cd, cd->input_buf, size, "THM=");
  7120. #endif
  7121. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7122. return 0;
  7123. }
  7124. /*******************************************************************************
  7125. * FUNCTION: move_sensor_data
  7126. *
  7127. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7128. * information structure, xy_mode and xy_data.
  7129. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7130. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7131. *
  7132. * RETURN:
  7133. * 0 = success
  7134. * !0 = failure
  7135. *
  7136. * PARAMETERS:
  7137. * *cd - pointer to core data
  7138. * *si - pointer to the system information structure
  7139. ******************************************************************************/
  7140. static int move_sensor_data(struct pt_core_data *cd,
  7141. struct pt_sysinfo *si)
  7142. {
  7143. #ifdef TTHE_TUNER_SUPPORT
  7144. int size = get_unaligned_le16(&cd->input_buf[0]);
  7145. if (size)
  7146. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7147. #endif
  7148. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7149. return 0;
  7150. }
  7151. /*******************************************************************************
  7152. * FUNCTION: move_button_data
  7153. *
  7154. * SUMMARY: Move the valid button data from the input buffer into the system
  7155. * information structure, xy_mode and xy_data.
  7156. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7157. * the tthe_tuner sysfs node under the label "OpModeData"
  7158. *
  7159. * RETURN:
  7160. * 0 = success
  7161. * !0 = failure
  7162. *
  7163. * PARAMETERS:
  7164. * *cd - pointer to core data
  7165. * *si - pointer to the system information structure
  7166. ******************************************************************************/
  7167. static int move_button_data(struct pt_core_data *cd,
  7168. struct pt_sysinfo *si)
  7169. {
  7170. #ifdef TTHE_TUNER_SUPPORT
  7171. int size = get_unaligned_le16(&cd->input_buf[0]);
  7172. if (size)
  7173. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7174. #endif
  7175. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7176. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7177. "xy_mode");
  7178. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7179. BTN_REPORT_SIZE);
  7180. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7181. "xy_data");
  7182. return 0;
  7183. }
  7184. /*******************************************************************************
  7185. * FUNCTION: move_touch_data
  7186. *
  7187. * SUMMARY: Move the valid touch data from the input buffer into the system
  7188. * information structure, xy_mode and xy_data.
  7189. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7190. * the tthe_tuner sysfs node under the label "OpModeData"
  7191. *
  7192. * RETURN:
  7193. * 0 = success
  7194. * !0 = failure
  7195. *
  7196. * PARAMETERS:
  7197. * *cd - pointer to core data
  7198. * *si - pointer to the system information structure
  7199. ******************************************************************************/
  7200. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7201. {
  7202. int max_tch = si->sensing_conf_data.max_tch;
  7203. int num_cur_tch;
  7204. int length;
  7205. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7206. #ifdef TTHE_TUNER_SUPPORT
  7207. int size = get_unaligned_le16(&cd->input_buf[0]);
  7208. if (size)
  7209. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7210. #endif
  7211. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7212. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7213. si->desc.tch_header_size, "xy_mode");
  7214. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7215. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7216. if (unlikely(num_cur_tch > max_tch))
  7217. num_cur_tch = max_tch;
  7218. length = num_cur_tch * si->desc.tch_record_size;
  7219. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7220. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7221. return 0;
  7222. }
  7223. /*******************************************************************************
  7224. * FUNCTION: move_hid_pen_data
  7225. *
  7226. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7227. * information structure, xy_mode and xy_data.
  7228. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7229. * the tthe_tuner sysfs node under the label "HID" starting with the
  7230. * report ID.
  7231. *
  7232. * RETURN:
  7233. * 0 = success
  7234. * !0 = failure
  7235. *
  7236. * PARAMETERS:
  7237. * *cd - pointer to core data
  7238. * *si - pointer to the system information structure
  7239. ******************************************************************************/
  7240. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7241. {
  7242. #ifdef TTHE_TUNER_SUPPORT
  7243. int size = get_unaligned_le16(&cd->input_buf[0]);
  7244. if (size) {
  7245. /*
  7246. * HID over USB does not require the two byte length field, so
  7247. * this should print from input_buf[2] but to keep both finger
  7248. * and pen reports the same the length is included
  7249. */
  7250. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7251. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7252. "HID-USB=");
  7253. else
  7254. tthe_print(cd, &(cd->input_buf[0]), size,
  7255. "HID-I2C=");
  7256. }
  7257. #endif
  7258. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7259. return 0;
  7260. }
  7261. /*******************************************************************************
  7262. * FUNCTION: parse_touch_input
  7263. *
  7264. * SUMMARY: Parse the touch report and take action based on the touch
  7265. * report_id.
  7266. *
  7267. * RETURN:
  7268. * 0 = success
  7269. * !0 = failure
  7270. *
  7271. * PARAMETERS:
  7272. * *cd - pointer to core data
  7273. * size - size of touch record
  7274. ******************************************************************************/
  7275. static int parse_touch_input(struct pt_core_data *cd, int size)
  7276. {
  7277. struct pt_sysinfo *si = &cd->sysinfo;
  7278. int report_id = cd->input_buf[2];
  7279. int rc = -EINVAL;
  7280. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7281. __func__);
  7282. if (!si->ready) {
  7283. pt_debug(cd->dev, DL_ERROR,
  7284. "%s: Need system information to parse touches\n",
  7285. __func__);
  7286. return 0;
  7287. }
  7288. if (!si->xy_mode || !si->xy_data)
  7289. return rc;
  7290. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7291. rc = move_touch_data(cd, si);
  7292. else if (report_id == PT_HID_PEN_REPORT_ID)
  7293. rc = move_hid_pen_data(cd, si);
  7294. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7295. rc = move_button_data(cd, si);
  7296. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7297. rc = move_sensor_data(cd, si);
  7298. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7299. rc = move_tracking_heatmap_data(cd, si);
  7300. if (rc)
  7301. return rc;
  7302. /* attention IRQ */
  7303. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7304. return 0;
  7305. }
  7306. /*******************************************************************************
  7307. * FUNCTION: parse_command_input
  7308. *
  7309. * SUMMARY: Move the response data from the input buffer to the response buffer
  7310. *
  7311. * RETURN:
  7312. * 0 = success
  7313. * !0 = failure
  7314. *
  7315. * PARAMETERS:
  7316. * *cd - pointer to core data
  7317. * size - size of response data
  7318. ******************************************************************************/
  7319. static int parse_command_input(struct pt_core_data *cd, int size)
  7320. {
  7321. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7322. __func__);
  7323. memcpy(cd->response_buf, cd->input_buf, size);
  7324. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7325. if (size && cd->show_tt_data) {
  7326. if (cd->pip2_prot_active)
  7327. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7328. else
  7329. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7330. }
  7331. #endif
  7332. mutex_lock(&cd->system_lock);
  7333. cd->hid_cmd_state = 0;
  7334. mutex_unlock(&cd->system_lock);
  7335. wake_up(&cd->wait_q);
  7336. return 0;
  7337. }
  7338. /*******************************************************************************
  7339. * FUNCTION: pt_allow_enumeration
  7340. *
  7341. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7342. * FW sentinel is seen.
  7343. *
  7344. * RETURN:
  7345. * true = allow enumeration or fully re-probe
  7346. * false = skip enumeration and fully re-probe
  7347. *
  7348. * PARAMETERS:
  7349. * *cd - pointer to core data
  7350. ******************************************************************************/
  7351. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7352. {
  7353. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7354. (!cd->hid_reset_cmd_state) &&
  7355. (cd->core_probe_complete) &&
  7356. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7357. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7358. (cd->mode == PT_MODE_OPERATIONAL)) {
  7359. return true;
  7360. }
  7361. if ((!cd->hid_reset_cmd_state) &&
  7362. (cd->core_probe_complete) &&
  7363. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7364. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7365. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7366. return true;
  7367. }
  7368. pt_debug(cd->dev, DL_INFO,
  7369. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7370. __func__,
  7371. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7372. "core_probe_complete(1)", cd->core_probe_complete,
  7373. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7374. "active_dut_gen(0,2)", cd->active_dut_generation);
  7375. return false;
  7376. }
  7377. /*******************************************************************************
  7378. * FUNCTION: pt_is_touch_report
  7379. *
  7380. * SUMMARY: Determine if a report ID should be treated as a touch report
  7381. *
  7382. * RETURN:
  7383. * true = report ID is a touch report
  7384. * false = report ID is not a touch report
  7385. *
  7386. * PARAMETERS:
  7387. * id - Report ID
  7388. ******************************************************************************/
  7389. static bool pt_is_touch_report(int id)
  7390. {
  7391. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7392. id == PT_HID_PEN_REPORT_ID ||
  7393. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7394. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7395. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7396. return true;
  7397. else
  7398. return false;
  7399. }
  7400. /*******************************************************************************
  7401. * FUNCTION: pt_parse_input
  7402. *
  7403. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7404. * on if its a response to a command or asynchronous touch data / reset
  7405. * sentinel. PIP2.x messages have additional error checking that is
  7406. * parsed (SEQ match from cmd to rsp, CRC valid).
  7407. * Look for special packets based on unique lengths:
  7408. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7409. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7410. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7411. * 0xFFXX - Empty buffer (PIP 1.7+)
  7412. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7413. * the initial probe has not yet been done.
  7414. *
  7415. * RETURN:
  7416. * 0 = success
  7417. * !0 = failure
  7418. *
  7419. * PARAMETERS:
  7420. * *cd - pointer to core data
  7421. ******************************************************************************/
  7422. static int pt_parse_input(struct pt_core_data *cd)
  7423. {
  7424. int report_id;
  7425. int cmd_id;
  7426. int is_command = 0;
  7427. int size;
  7428. int print_size;
  7429. bool touch_report = true;
  7430. unsigned short calc_crc;
  7431. unsigned short resp_crc;
  7432. cd->fw_sys_mode_in_standby_state = false;
  7433. size = get_unaligned_le16(&cd->input_buf[0]);
  7434. print_size = size;
  7435. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7436. __func__, print_size);
  7437. if (print_size <= PT_MAX_INPUT)
  7438. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7439. "<<< Read buf");
  7440. if (size == 0 ||
  7441. (size == 11 &&
  7442. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7443. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7444. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7445. PIP2_CMD_COMMAND_ID_MASK) ==
  7446. PIP2_CMD_ID_STATUS)) {
  7447. touch_report = false;
  7448. cd->hw_detected = true;
  7449. cd->bl_pip_ver_ready = false;
  7450. cd->app_pip_ver_ready = false;
  7451. if (size == 0) {
  7452. mutex_lock(&cd->system_lock);
  7453. cd->pip2_prot_active = false;
  7454. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7455. /*
  7456. * For Gen5/6 this sentinel could be from
  7457. * the BL or FW. Attempt to set the correct
  7458. * mode based on the previous PIP command.
  7459. */
  7460. if (cd->hid_cmd_state ==
  7461. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7462. cd->mode = PT_MODE_OPERATIONAL;
  7463. cd->startup_status =
  7464. STARTUP_STATUS_FW_RESET_SENTINEL;
  7465. } else if (cd->hid_cmd_state ==
  7466. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7467. cd->hid_reset_cmd_state) {
  7468. cd->mode = PT_MODE_BOOTLOADER;
  7469. cd->startup_status =
  7470. STARTUP_STATUS_BL_RESET_SENTINEL;
  7471. } else {
  7472. cd->mode = PT_MODE_UNKNOWN;
  7473. cd->startup_status =
  7474. STARTUP_STATUS_START;
  7475. }
  7476. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7477. pt_debug(cd->dev, DL_INFO,
  7478. "%s: ATM Gen5/6 %s sentinel received\n",
  7479. __func__,
  7480. (cd->mode == PT_MODE_OPERATIONAL ?
  7481. "FW" :
  7482. (cd->mode == PT_MODE_BOOTLOADER ?
  7483. "BL" : "Unknown")));
  7484. } else {
  7485. cd->mode = PT_MODE_OPERATIONAL;
  7486. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7487. cd->startup_status =
  7488. STARTUP_STATUS_FW_RESET_SENTINEL;
  7489. pt_debug(cd->dev, DL_INFO,
  7490. "%s: ATM PT/TT FW sentinel received\n",
  7491. __func__);
  7492. }
  7493. mutex_unlock(&cd->system_lock);
  7494. if (pt_allow_enumeration(cd)) {
  7495. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7496. pt_debug(cd->dev, DL_INFO,
  7497. "%s: Queue Restart\n", __func__);
  7498. pt_queue_restart(cd);
  7499. } else {
  7500. pt_debug(cd->dev, DL_INFO,
  7501. "%s: Queue Enum\n", __func__);
  7502. pt_queue_enum(cd);
  7503. }
  7504. } else {
  7505. pt_debug(cd->dev, DL_INFO,
  7506. "%s: Sentinel - No Queued Action\n",
  7507. __func__);
  7508. }
  7509. } else {
  7510. /* Sentinel must be from TT/TC BL */
  7511. mutex_lock(&cd->system_lock);
  7512. cd->pip2_prot_active = true;
  7513. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7514. cd->mode = PT_MODE_BOOTLOADER;
  7515. cd->sysinfo.ready = false;
  7516. mutex_unlock(&cd->system_lock);
  7517. pt_debug(cd->dev, DL_INFO,
  7518. "%s: BL Reset sentinel received\n", __func__);
  7519. if (cd->flashless_dut &&
  7520. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7521. pt_debug(cd->dev, DL_INFO,
  7522. "%s: BL to RAM for flashless DUT\n",
  7523. __func__);
  7524. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7525. }
  7526. }
  7527. mutex_lock(&cd->system_lock);
  7528. memcpy(cd->response_buf, cd->input_buf, 2);
  7529. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7530. mutex_unlock(&cd->system_lock);
  7531. pt_debug(cd->dev, DL_WARN,
  7532. "%s: Device Initiated Reset\n", __func__);
  7533. wake_up(&cd->wait_q);
  7534. return 0;
  7535. }
  7536. cd->hid_reset_cmd_state = 0;
  7537. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7538. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7539. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7540. cd->hid_cmd_state = 0;
  7541. wake_up(&cd->wait_q);
  7542. mutex_unlock(&cd->system_lock);
  7543. return 0;
  7544. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7545. /*
  7546. * This debug message below is used by PBATS to calculate the
  7547. * time from the last lift off IRQ to when FW exits LFT mode.
  7548. */
  7549. touch_report = false;
  7550. pt_debug(cd->dev, DL_WARN,
  7551. "%s: DUT - Empty buffer detected\n", __func__);
  7552. return 0;
  7553. } else if (size > PT_MAX_INPUT) {
  7554. pt_debug(cd->dev, DL_ERROR,
  7555. "%s: DUT - Unexpected len field in active bus data!\n",
  7556. __func__);
  7557. return -EINVAL;
  7558. }
  7559. if (cd->pip2_prot_active) {
  7560. pt_debug(cd->dev, DL_DEBUG,
  7561. "%s: Decode PIP2.x Response\n", __func__);
  7562. /* PIP2 does not have a report id, hard code it */
  7563. report_id = 0x00;
  7564. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7565. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7566. resp_crc = cd->input_buf[size - 2] << 8;
  7567. resp_crc |= cd->input_buf[size - 1];
  7568. if ((cd->pip2_cmd_tag_seq !=
  7569. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7570. (resp_crc != calc_crc) &&
  7571. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7572. == PT_PIP_TOUCH_REPORT_ID) ||
  7573. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7574. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7575. pt_debug(cd->dev, DL_WARN,
  7576. "%s: %s %d %s\n",
  7577. __func__,
  7578. "Received PIP1 report id =",
  7579. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7580. "when expecting a PIP2 report - IGNORE report");
  7581. return 0;
  7582. }
  7583. is_command = 1;
  7584. touch_report = false;
  7585. } else {
  7586. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7587. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7588. }
  7589. #ifdef TTDL_DIAGNOSTICS
  7590. pt_debug(cd->dev, DL_DEBUG,
  7591. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7592. __func__, cd->pip2_prot_active, report_id,
  7593. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7594. #endif /* TTDL_DIAGNOSTICS */
  7595. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7596. pt_wakeup_host(cd);
  7597. #ifdef TTHE_TUNER_SUPPORT
  7598. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7599. #endif
  7600. return 0;
  7601. }
  7602. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7603. msecs_to_jiffies(cd->watchdog_interval));
  7604. /*
  7605. * If it is PIP2 response, the report_id has been set to 0,
  7606. * so it will not be parsed as a touch packet.
  7607. */
  7608. if (!pt_is_touch_report(report_id)) {
  7609. is_command = 1;
  7610. touch_report = false;
  7611. }
  7612. if (unlikely(is_command)) {
  7613. parse_command_input(cd, size);
  7614. return 0;
  7615. }
  7616. if (touch_report)
  7617. parse_touch_input(cd, size);
  7618. return 0;
  7619. }
  7620. /*******************************************************************************
  7621. * FUNCTION: pt_read_input
  7622. *
  7623. * SUMMARY: Reads incoming data off of the active bus
  7624. *
  7625. * RETURN:
  7626. * 0 = success
  7627. * !0 = failure
  7628. *
  7629. * PARAMETERS:
  7630. * *cd - pointer to core data
  7631. ******************************************************************************/
  7632. static int pt_read_input(struct pt_core_data *cd)
  7633. {
  7634. struct device *dev = cd->dev;
  7635. int rc = 0;
  7636. int t;
  7637. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7638. /*
  7639. * Workaround for easywake failure
  7640. * Interrupt for easywake, wait for bus controller to wake
  7641. */
  7642. mutex_lock(&cd->system_lock);
  7643. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7644. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7645. mutex_unlock(&cd->system_lock);
  7646. if (!dev->power.is_suspended)
  7647. goto read;
  7648. t = wait_event_timeout(cd->wait_q,
  7649. (cd->wait_until_wake == 1),
  7650. msecs_to_jiffies(2000));
  7651. #ifdef TTDL_DIAGNOSTICS
  7652. if (IS_TMO(t)) {
  7653. cd->bus_transmit_error_count++;
  7654. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7655. pt_debug(dev, DL_ERROR,
  7656. "%s: !!!I2C Transmission Error %d\n",
  7657. __func__,
  7658. cd->bus_transmit_error_count);
  7659. }
  7660. #else
  7661. if (IS_TMO(t))
  7662. pt_queue_enum(cd);
  7663. #endif /* TTDL_DIAGNOSTICS */
  7664. goto read;
  7665. }
  7666. }
  7667. mutex_unlock(&cd->system_lock);
  7668. read:
  7669. /* Try reading up to 'retry' times */
  7670. while (retry-- != 0) {
  7671. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7672. PT_MAX_INPUT);
  7673. if (!rc) {
  7674. pt_debug(dev, DL_DEBUG,
  7675. "%s: Read input successfully\n", __func__);
  7676. goto read_exit;
  7677. }
  7678. usleep_range(1000, 2000);
  7679. }
  7680. pt_debug(dev, DL_ERROR,
  7681. "%s: Error getting report, rc=%d\n", __func__, rc);
  7682. read_exit:
  7683. return rc;
  7684. }
  7685. /*******************************************************************************
  7686. * FUNCTION: pt_irq
  7687. *
  7688. * SUMMARY: Process all detected interrupts
  7689. *
  7690. * RETURN:
  7691. * IRQ_HANDLED - Finished processing the interrupt
  7692. *
  7693. * PARAMETERS:
  7694. * irq - IRQ number
  7695. * *handle - pointer to core data struct
  7696. ******************************************************************************/
  7697. irqreturn_t pt_irq(int irq, void *handle)
  7698. {
  7699. struct pt_core_data *cd = handle;
  7700. int rc = 0;
  7701. if (!pt_check_irq_asserted(cd))
  7702. return IRQ_HANDLED;
  7703. rc = pt_read_input(cd);
  7704. #ifdef TTDL_DIAGNOSTICS
  7705. cd->irq_count++;
  7706. /* Used to calculate T-Refresh */
  7707. if (cd->t_refresh_active) {
  7708. if (cd->t_refresh_count == 0) {
  7709. cd->t_refresh_time = jiffies;
  7710. cd->t_refresh_count++;
  7711. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7712. cd->t_refresh_count++;
  7713. } else {
  7714. cd->t_refresh_active = 0;
  7715. cd->t_refresh_time =
  7716. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7717. }
  7718. }
  7719. #endif /* TTDL_DIAGNOSTICS */
  7720. if (!rc)
  7721. pt_parse_input(cd);
  7722. return IRQ_HANDLED;
  7723. }
  7724. /*******************************************************************************
  7725. * FUNCTION: _pt_subscribe_attention
  7726. *
  7727. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7728. * to subscribe themselves into the TTDL attention list
  7729. *
  7730. * RETURN:
  7731. * 0 = success
  7732. * !0 = failure
  7733. *
  7734. * PARAMETERS:
  7735. * *dev - pointer to device structure
  7736. * type - attention type enum
  7737. * *id - ID of the module calling this function
  7738. * *func - callback function pointer to be called when notified
  7739. * mode - attention mode
  7740. ******************************************************************************/
  7741. int _pt_subscribe_attention(struct device *dev,
  7742. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7743. int mode)
  7744. {
  7745. struct pt_core_data *cd = dev_get_drvdata(dev);
  7746. struct atten_node *atten, *atten_new;
  7747. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7748. if (!atten_new)
  7749. return -ENOMEM;
  7750. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7751. dev_name(cd->dev));
  7752. spin_lock(&cd->spinlock);
  7753. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7754. if (atten->id == id && atten->mode == mode) {
  7755. spin_unlock(&cd->spinlock);
  7756. kfree(atten_new);
  7757. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7758. __func__,
  7759. "already subscribed attention",
  7760. dev, "mode", mode);
  7761. return 0;
  7762. }
  7763. }
  7764. atten_new->id = id;
  7765. atten_new->dev = dev;
  7766. atten_new->mode = mode;
  7767. atten_new->func = func;
  7768. list_add(&atten_new->node, &cd->atten_list[type]);
  7769. spin_unlock(&cd->spinlock);
  7770. return 0;
  7771. }
  7772. /*******************************************************************************
  7773. * FUNCTION: _pt_unsubscribe_attention
  7774. *
  7775. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7776. * to unsubscribe themselves from the TTDL attention list
  7777. *
  7778. * RETURN:
  7779. * 0 = success
  7780. * !0 = failure
  7781. *
  7782. * PARAMETERS:
  7783. * *dev - pointer to device structure
  7784. * type - attention type enum
  7785. * *id - ID of the module calling this function
  7786. * *func - function pointer
  7787. * mode - attention mode
  7788. ******************************************************************************/
  7789. int _pt_unsubscribe_attention(struct device *dev,
  7790. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7791. int mode)
  7792. {
  7793. struct pt_core_data *cd = dev_get_drvdata(dev);
  7794. struct atten_node *atten, *atten_n;
  7795. spin_lock(&cd->spinlock);
  7796. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7797. if (atten->id == id && atten->mode == mode) {
  7798. list_del(&atten->node);
  7799. spin_unlock(&cd->spinlock);
  7800. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7801. __func__,
  7802. "unsub for atten->dev", atten->dev,
  7803. "atten->mode", atten->mode);
  7804. kfree(atten);
  7805. return 0;
  7806. }
  7807. }
  7808. spin_unlock(&cd->spinlock);
  7809. return -ENODEV;
  7810. }
  7811. /*******************************************************************************
  7812. * FUNCTION: _pt_request_exclusive
  7813. *
  7814. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7815. * to request exclusive access
  7816. *
  7817. * RETURN:
  7818. * 0 = success
  7819. * !0 = failure
  7820. *
  7821. * PARAMETERS:
  7822. * *dev - pointer to device structure
  7823. * timeout_ms - timeout to wait for exclusive access
  7824. ******************************************************************************/
  7825. static int _pt_request_exclusive(struct device *dev,
  7826. int timeout_ms)
  7827. {
  7828. struct pt_core_data *cd = dev_get_drvdata(dev);
  7829. return request_exclusive(cd, (void *)dev, timeout_ms);
  7830. }
  7831. /*******************************************************************************
  7832. * FUNCTION: _pt_release_exclusive
  7833. *
  7834. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7835. * to release exclusive access
  7836. *
  7837. * RETURN:
  7838. * 0 = success
  7839. * !0 = failure
  7840. *
  7841. * PARAMETERS:
  7842. * *dev - pointer to device structure
  7843. ******************************************************************************/
  7844. static int _pt_release_exclusive(struct device *dev)
  7845. {
  7846. struct pt_core_data *cd = dev_get_drvdata(dev);
  7847. return release_exclusive(cd, (void *)dev);
  7848. }
  7849. /*******************************************************************************
  7850. * FUNCTION: _pt_request_reset
  7851. *
  7852. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7853. * to request the DUT to be reset. Function returns err if refused or
  7854. * timeout occurs (Note: core uses fixed timeout period).
  7855. *
  7856. * NOTE: Function blocks until ISR occurs.
  7857. *
  7858. * RETURN:
  7859. * 0 = success
  7860. * !0 = failure
  7861. *
  7862. * PARAMETERS:
  7863. * *dev - pointer to device structure
  7864. * protect - flag to call protected or non-protected
  7865. ******************************************************************************/
  7866. static int _pt_request_reset(struct device *dev, int protect)
  7867. {
  7868. struct pt_core_data *cd = dev_get_drvdata(dev);
  7869. int rc;
  7870. rc = pt_dut_reset(cd, protect);
  7871. if (rc < 0) {
  7872. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7873. __func__, rc);
  7874. }
  7875. return rc;
  7876. }
  7877. /*******************************************************************************
  7878. * FUNCTION: _pt_request_enum
  7879. *
  7880. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7881. * to request TTDL to queue an enum. This function will return err
  7882. * if refused; if no error then enum has completed and system is in
  7883. * normal operation mode.
  7884. *
  7885. * RETURN:
  7886. * 0 = success
  7887. * !0 = failure
  7888. *
  7889. * PARAMETERS:
  7890. * *dev - pointer to device structure
  7891. * wait - boolean to determine if to wait for startup event
  7892. ******************************************************************************/
  7893. static int _pt_request_enum(struct device *dev, bool wait)
  7894. {
  7895. struct pt_core_data *cd = dev_get_drvdata(dev);
  7896. pt_queue_enum(cd);
  7897. if (wait)
  7898. wait_event_timeout(cd->wait_q,
  7899. cd->startup_state == STARTUP_NONE,
  7900. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7901. return 0;
  7902. }
  7903. /*******************************************************************************
  7904. * FUNCTION: _pt_request_sysinfo
  7905. *
  7906. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7907. * to request the pointer to the system information structure. This
  7908. * function will return NULL if sysinfo has not been acquired from the
  7909. * DUT yet.
  7910. *
  7911. * RETURN: Pointer to the system information struct
  7912. *
  7913. * PARAMETERS:
  7914. * *dev - pointer to device structure
  7915. ******************************************************************************/
  7916. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7917. {
  7918. struct pt_core_data *cd = dev_get_drvdata(dev);
  7919. int rc = 0;
  7920. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7921. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7922. !cd->sysinfo.ready) {
  7923. rc = pt_hid_output_get_sysinfo_(cd);
  7924. if (rc) {
  7925. pt_debug(cd->dev, DL_ERROR,
  7926. "%s: Error getting sysinfo rc=%d\n",
  7927. __func__, rc);
  7928. }
  7929. }
  7930. if (cd->sysinfo.ready)
  7931. return &cd->sysinfo;
  7932. return NULL;
  7933. }
  7934. /*******************************************************************************
  7935. * FUNCTION: _pt_request_loader_pdata
  7936. *
  7937. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7938. * to request the pointer to the loader platform data
  7939. *
  7940. * RETURN: Pointer to the loader platform data struct
  7941. *
  7942. * PARAMETERS:
  7943. * *dev - pointer to device structure
  7944. ******************************************************************************/
  7945. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7946. struct device *dev)
  7947. {
  7948. struct pt_core_data *cd = dev_get_drvdata(dev);
  7949. return cd->pdata->loader_pdata;
  7950. }
  7951. /*******************************************************************************
  7952. * FUNCTION: _pt_request_start_wd
  7953. *
  7954. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7955. * to request to start the TTDL watchdog
  7956. *
  7957. * RETURN:
  7958. * 0 = success
  7959. * !0 = failure
  7960. *
  7961. * PARAMETERS:
  7962. * *dev - pointer to device structure
  7963. ******************************************************************************/
  7964. static int _pt_request_start_wd(struct device *dev)
  7965. {
  7966. struct pt_core_data *cd = dev_get_drvdata(dev);
  7967. pt_start_wd_timer(cd);
  7968. return 0;
  7969. }
  7970. /*******************************************************************************
  7971. * FUNCTION: _pt_request_stop_wd
  7972. *
  7973. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7974. * to request to stop the TTDL watchdog
  7975. *
  7976. * RETURN:
  7977. * 0 = success
  7978. * !0 = failure
  7979. *
  7980. * PARAMETERS:
  7981. * *dev - pointer to device structure
  7982. ******************************************************************************/
  7983. static int _pt_request_stop_wd(struct device *dev)
  7984. {
  7985. struct pt_core_data *cd = dev_get_drvdata(dev);
  7986. pt_stop_wd_timer(cd);
  7987. return 0;
  7988. }
  7989. /*******************************************************************************
  7990. * FUNCTION: pt_pip2_launch_app
  7991. *
  7992. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7993. * the FW reset sentinel to indicate the function succeeded.
  7994. *
  7995. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7996. * in a timeout delay and with the DUT being reset with an XRES.
  7997. *
  7998. * RETURN:
  7999. * 0 = success
  8000. * !0 = failure
  8001. *
  8002. * PARAMETERS:
  8003. * *cd - pointer to core data structure
  8004. * protect - flag to call protected or non-protected
  8005. ******************************************************************************/
  8006. static int pt_pip2_launch_app(struct device *dev, int protect)
  8007. {
  8008. struct pt_core_data *cd = dev_get_drvdata(dev);
  8009. u16 actual_read_len;
  8010. u16 tmp_startup_status = cd->startup_status;
  8011. u8 read_buf[12];
  8012. u8 status;
  8013. int time = 0;
  8014. int rc = 0;
  8015. mutex_lock(&cd->system_lock);
  8016. cd->startup_status = STARTUP_STATUS_START;
  8017. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8018. mutex_unlock(&cd->system_lock);
  8019. rc = _pt_request_pip2_send_cmd(dev, protect,
  8020. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8021. &actual_read_len);
  8022. if (rc) {
  8023. pt_debug(dev, DL_ERROR,
  8024. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8025. __func__, rc);
  8026. } else {
  8027. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8028. /* Test for no or invalid image in FLASH, no point to reset */
  8029. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8030. rc = status;
  8031. goto exit;
  8032. }
  8033. /* Any other boot failure */
  8034. if (status != 0) {
  8035. pt_debug(dev, DL_ERROR,
  8036. "%s: FW did not EXECUTE, status = %d\n",
  8037. __func__, status);
  8038. rc = status;
  8039. }
  8040. }
  8041. if (rc) {
  8042. pt_debug(dev, DL_ERROR,
  8043. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8044. __func__, rc);
  8045. goto exit;
  8046. }
  8047. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8048. msleep(20);
  8049. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8050. __func__, time, cd->startup_status);
  8051. time += 20;
  8052. }
  8053. if (cd->startup_status == STARTUP_STATUS_START) {
  8054. pt_debug(cd->dev, DL_WARN,
  8055. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8056. rc = -ETIME;
  8057. }
  8058. exit:
  8059. if (cd->startup_status == STARTUP_STATUS_START) {
  8060. /* Reset to original state because we could be stuck in BL */
  8061. mutex_lock(&cd->system_lock);
  8062. cd->startup_status = tmp_startup_status;
  8063. mutex_unlock(&cd->system_lock);
  8064. }
  8065. return rc;
  8066. }
  8067. /*******************************************************************************
  8068. * FUNCTION: _pt_request_pip2_launch_app
  8069. *
  8070. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8071. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8072. * consumed.
  8073. *
  8074. * RETURN:
  8075. * 0 = success
  8076. * !0 = failure
  8077. *
  8078. * PARAMETERS:
  8079. * *cd - pointer to core data structure
  8080. ******************************************************************************/
  8081. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8082. {
  8083. return pt_pip2_launch_app(dev, protect);
  8084. }
  8085. /*******************************************************************************
  8086. * FUNCTION: _pt_request_wait_for_enum_state
  8087. *
  8088. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8089. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8090. *
  8091. * RETURN:
  8092. * 0 = success
  8093. * !0 = failure
  8094. *
  8095. * PARAMETERS:
  8096. * *cd - pointer to core data structure
  8097. * timeout - timeout for how long to wait
  8098. * state - enum state to wait for
  8099. ******************************************************************************/
  8100. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8101. int state)
  8102. {
  8103. int rc = 0;
  8104. int t;
  8105. struct pt_core_data *cd = dev_get_drvdata(dev);
  8106. t = wait_event_timeout(cd->wait_q,
  8107. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8108. msecs_to_jiffies(timeout));
  8109. if (IS_TMO(t)) {
  8110. pt_debug(cd->dev, DL_ERROR,
  8111. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8112. __func__, state, timeout);
  8113. pt_debug(cd->dev, DL_WARN,
  8114. "%s: enum state reached 0x%04X\n",
  8115. __func__, cd->startup_status);
  8116. rc = -ETIME;
  8117. } else if (cd->startup_status & state) {
  8118. pt_debug(cd->dev, DL_INFO,
  8119. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8120. __func__, cd->startup_status,
  8121. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8122. } else {
  8123. if (t == 1) {
  8124. pt_debug(
  8125. cd->dev, DL_ERROR,
  8126. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8127. __func__, state, timeout);
  8128. rc = -ETIME;
  8129. } else {
  8130. pt_debug(
  8131. cd->dev, DL_ERROR,
  8132. "%s: Enum state 0x%04X not reached in %dms\n",
  8133. __func__, state, timeout - jiffies_to_msecs(t));
  8134. rc = -EINVAL;
  8135. }
  8136. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8137. __func__, cd->startup_status);
  8138. }
  8139. return rc;
  8140. }
  8141. /*******************************************************************************
  8142. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8143. *
  8144. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8145. * deep sleep.
  8146. *
  8147. * RETURN:
  8148. * 0 = success
  8149. * !0 = error
  8150. *
  8151. * PARAMETERS:
  8152. * *cd - pointer to core data
  8153. ******************************************************************************/
  8154. static int pt_core_wake_device_from_deep_sleep_(
  8155. struct pt_core_data *cd)
  8156. {
  8157. int rc;
  8158. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8159. if (rc)
  8160. rc = -EAGAIN;
  8161. /* Prevent failure on sequential wake/sleep requests from OS */
  8162. msleep(20);
  8163. return rc;
  8164. }
  8165. /*******************************************************************************
  8166. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8167. *
  8168. * SUMMARY: Wake up device from Easy-Wake state.
  8169. *
  8170. * RETURN:
  8171. * 0 = success
  8172. * !0 = failure
  8173. *
  8174. * PARAMETERS:
  8175. * *cd - pointer to core data
  8176. ******************************************************************************/
  8177. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8178. {
  8179. int rc = 0;
  8180. mutex_lock(&cd->system_lock);
  8181. cd->wait_until_wake = 1;
  8182. mutex_unlock(&cd->system_lock);
  8183. wake_up(&cd->wait_q);
  8184. msleep(20);
  8185. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8186. return rc;
  8187. }
  8188. /*******************************************************************************
  8189. * FUNCTION: pt_restore_parameters_
  8190. *
  8191. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8192. * back to the DUT
  8193. *
  8194. * RETURN:
  8195. * 0 = success
  8196. * !0 = failure
  8197. *
  8198. * PARAMETERS:
  8199. * *cd - pointer the core data structure
  8200. ******************************************************************************/
  8201. static int pt_restore_parameters_(struct pt_core_data *cd)
  8202. {
  8203. struct param_node *param;
  8204. int rc = 0;
  8205. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8206. goto exit;
  8207. spin_lock(&cd->spinlock);
  8208. list_for_each_entry(param, &cd->param_list, node) {
  8209. spin_unlock(&cd->spinlock);
  8210. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8211. __func__, param->id, param->value);
  8212. rc = pt_pip_set_param_(cd, param->id,
  8213. param->value, param->size);
  8214. if (rc)
  8215. goto exit;
  8216. spin_lock(&cd->spinlock);
  8217. }
  8218. spin_unlock(&cd->spinlock);
  8219. exit:
  8220. return rc;
  8221. }
  8222. /*******************************************************************************
  8223. * FUNCTION: pt_pip2_exit_bl_
  8224. *
  8225. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8226. * a DUT that may not have flash and will need FW to be loaded into RAM
  8227. *
  8228. * RETURN:
  8229. * 0 = success
  8230. * !0 = failure
  8231. *
  8232. * PARAMETERS:
  8233. * *cd - pointer the core data structure
  8234. * *status_str - pointer to optional status string buffer
  8235. * buf_size - size of status_str buffer
  8236. ******************************************************************************/
  8237. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8238. {
  8239. int rc;
  8240. int wait_time = 0;
  8241. u8 mode = PT_MODE_UNKNOWN;
  8242. bool load_status_str = false;
  8243. /*
  8244. * Below function has protective call to ensure no enum is still on
  8245. * going, while this kind of protection should be applied widely in
  8246. * future (TODO).
  8247. */
  8248. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8249. if (status_str && buf_size <= 50)
  8250. load_status_str = true;
  8251. if (mode == PT_MODE_BOOTLOADER) {
  8252. if (cd->flashless_dut == 1) {
  8253. rc = pt_hw_hard_reset(cd);
  8254. } else {
  8255. rc = pt_pip2_launch_app(cd->dev,
  8256. PT_CORE_CMD_UNPROTECTED);
  8257. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8258. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8259. __func__, "Invalid image in FLASH rc", rc);
  8260. } else if (rc) {
  8261. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8262. __func__, "Failed to launch app rc", rc);
  8263. }
  8264. }
  8265. if (!rc) {
  8266. if (cd->flashless_dut == 1) {
  8267. /* Wait for BL to complete before enum */
  8268. rc = _pt_request_wait_for_enum_state(cd->dev,
  8269. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8270. if (rc && load_status_str) {
  8271. strlcpy(status_str, "No FW sentinel after BL",
  8272. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8273. goto exit;
  8274. }
  8275. }
  8276. /*
  8277. * If the host wants to interact with the FW or do a
  8278. * forced calibration, the FW must be out of BOOT mode
  8279. * and the system information must have been retrieved.
  8280. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8281. * If, however, the enumeration does not reach this
  8282. * point, the DUT may still be in APP mode so test
  8283. * for all conditions.
  8284. */
  8285. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8286. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8287. if (!rc || cd->startup_status >=
  8288. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8289. mutex_lock(&cd->system_lock);
  8290. cd->mode = PT_MODE_OPERATIONAL;
  8291. mutex_unlock(&cd->system_lock);
  8292. }
  8293. if (rc) {
  8294. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8295. __func__, "Failed to enum with DUT",
  8296. cd->startup_status);
  8297. if (load_status_str && !(cd->startup_status &
  8298. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8299. strlcpy(status_str, "FW Stuck in Boot mode",
  8300. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8301. goto exit;
  8302. }
  8303. }
  8304. /*
  8305. * The coming FW sentinel could wake up the event
  8306. * queue, which has chance to be taken by next command
  8307. * wrongly. Following delay is a workaround to avoid
  8308. * this issue for most situations.
  8309. */
  8310. msleep(20);
  8311. pt_start_wd_timer(cd);
  8312. }
  8313. if (load_status_str) {
  8314. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8315. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8316. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8317. else if (!rc)
  8318. strlcpy(status_str, "Entered APP from BL mode",
  8319. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8320. else
  8321. strlcpy(status_str, "Failed to enter APP from BL mode",
  8322. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8323. }
  8324. } else if (mode == PT_MODE_OPERATIONAL) {
  8325. mutex_lock(&cd->system_lock);
  8326. cd->mode = mode;
  8327. mutex_unlock(&cd->system_lock);
  8328. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8329. if (load_status_str) {
  8330. if (!rc)
  8331. strlcpy(status_str, "Already in APP mode",
  8332. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8333. else
  8334. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8335. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8336. }
  8337. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8338. mutex_lock(&cd->system_lock);
  8339. cd->mode = mode;
  8340. mutex_unlock(&cd->system_lock);
  8341. if (load_status_str)
  8342. strlcpy(status_str, "Failed to determine active mode",
  8343. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8344. }
  8345. exit:
  8346. if (!rc)
  8347. pt_start_wd_timer(cd);
  8348. return rc;
  8349. }
  8350. /*******************************************************************************
  8351. * FUNCTION: pt_pip2_exit_bl
  8352. *
  8353. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8354. * access.
  8355. *
  8356. * RETURN:
  8357. * 0 = success
  8358. * !0 = failure
  8359. *
  8360. * PARAMETERS:
  8361. * *cd - pointer the core data structure
  8362. * *status_str - pointer to optional status string buffer
  8363. * buf_size - size of status_str buffer
  8364. ******************************************************************************/
  8365. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8366. {
  8367. int rc;
  8368. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8369. if (rc < 0) {
  8370. pt_debug(cd->dev, DL_ERROR,
  8371. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8372. cd->exclusive_dev, cd->dev);
  8373. return rc;
  8374. }
  8375. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8376. if (release_exclusive(cd, cd->dev) < 0)
  8377. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8378. __func__);
  8379. return rc;
  8380. }
  8381. /*******************************************************************************
  8382. * FUNCTION: _fast_startup
  8383. *
  8384. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8385. *
  8386. * RETURN:
  8387. * 0 = success
  8388. * !0 = failure
  8389. *
  8390. * PARAMETERS:
  8391. * *cd - pointer the core data structure
  8392. ******************************************************************************/
  8393. static int _fast_startup(struct pt_core_data *cd)
  8394. {
  8395. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8396. int rc = 0;
  8397. u8 mode = PT_MODE_UNKNOWN;
  8398. struct pt_hid_desc hid_desc;
  8399. int wait_time = 0;
  8400. memset(&hid_desc, 0, sizeof(hid_desc));
  8401. reset:
  8402. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8403. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8404. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8405. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8406. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8407. __func__);
  8408. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8409. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8410. if (rc < 0) {
  8411. pt_debug(cd->dev, DL_ERROR,
  8412. "%s: Error on getting HID descriptor r=%d\n",
  8413. __func__, rc);
  8414. if (retry--)
  8415. goto reset;
  8416. goto exit;
  8417. }
  8418. cd->mode = pt_get_mode(cd, &hid_desc);
  8419. if (cd->mode == PT_MODE_BOOTLOADER) {
  8420. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8421. __func__);
  8422. rc = pt_hid_output_bl_launch_app_(cd);
  8423. if (rc < 0) {
  8424. pt_debug(cd->dev, DL_ERROR,
  8425. "%s: Error on launch app r=%d\n",
  8426. __func__, rc);
  8427. if (retry--)
  8428. goto reset;
  8429. goto exit;
  8430. }
  8431. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8432. if (rc < 0) {
  8433. pt_debug(cd->dev, DL_ERROR,
  8434. "%s: Error on getting HID descriptor r=%d\n",
  8435. __func__, rc);
  8436. if (retry--)
  8437. goto reset;
  8438. goto exit;
  8439. }
  8440. cd->mode = pt_get_mode(cd, &hid_desc);
  8441. if (cd->mode == PT_MODE_BOOTLOADER) {
  8442. if (retry--)
  8443. goto reset;
  8444. goto exit;
  8445. }
  8446. }
  8447. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8448. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8449. } else {
  8450. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8451. __func__);
  8452. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8453. /* Wait for any sentinel before first try */
  8454. rc = _pt_request_wait_for_enum_state(
  8455. cd->dev, 150,
  8456. STARTUP_STATUS_BL_RESET_SENTINEL |
  8457. STARTUP_STATUS_FW_RESET_SENTINEL);
  8458. if (rc)
  8459. pt_debug(cd->dev, DL_ERROR,
  8460. "%s: No Sentinel detected rc = %d\n",
  8461. __func__, rc);
  8462. } else
  8463. pt_flush_bus_if_irq_asserted(cd,
  8464. PT_FLUSH_BUS_BASED_ON_LEN);
  8465. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8466. if (rc) {
  8467. pt_debug(cd->dev, DL_ERROR,
  8468. "%s: Get mode failed, mode unknown\n",
  8469. __func__);
  8470. mode = PT_MODE_UNKNOWN;
  8471. }
  8472. cd->mode = mode;
  8473. if (cd->mode == PT_MODE_BOOTLOADER) {
  8474. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8475. __func__);
  8476. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8477. if (rc) {
  8478. pt_debug(cd->dev, DL_ERROR,
  8479. "%s Failed to exit bootloader\n",
  8480. __func__);
  8481. msleep(50);
  8482. rc = -ENODEV;
  8483. if (retry--)
  8484. goto reset;
  8485. goto exit;
  8486. } else {
  8487. pt_debug(cd->dev, DL_INFO,
  8488. "%s: Exit bootloader successfully\n",
  8489. __func__);
  8490. }
  8491. if (cd->mode != PT_MODE_OPERATIONAL) {
  8492. pt_debug(cd->dev, DL_WARN,
  8493. "%s: restore mode failure mode = %d\n",
  8494. __func__, cd->mode);
  8495. if (retry--)
  8496. goto reset;
  8497. goto exit;
  8498. }
  8499. }
  8500. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8501. }
  8502. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8503. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8504. if (!rc) {
  8505. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8506. pt_debug(cd->dev, DL_WARN,
  8507. "%s: Exit FW BOOT Mode after %dms\n",
  8508. __func__, wait_time);
  8509. } else {
  8510. pt_debug(cd->dev, DL_WARN,
  8511. "%s: FW stuck in BOOT Mode after %dms\n",
  8512. __func__, wait_time);
  8513. goto exit;
  8514. }
  8515. if (!cd->sysinfo.ready) {
  8516. rc = pt_hid_output_get_sysinfo_(cd);
  8517. if (rc) {
  8518. pt_debug(cd->dev, DL_ERROR,
  8519. "%s: Error on getting sysinfo r=%d\n",
  8520. __func__, rc);
  8521. if (retry--)
  8522. goto reset;
  8523. goto exit;
  8524. }
  8525. }
  8526. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8527. rc = pt_restore_parameters_(cd);
  8528. if (rc)
  8529. pt_debug(cd->dev, DL_ERROR,
  8530. "%s: failed to restore parameters rc=%d\n",
  8531. __func__, rc);
  8532. else
  8533. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8534. exit:
  8535. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8536. return rc;
  8537. }
  8538. /*******************************************************************************
  8539. * FUNCTION: pt_core_poweron_device_
  8540. *
  8541. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8542. *
  8543. * RETURN:
  8544. * 0 = success
  8545. * !0 = failure
  8546. *
  8547. * PARAMETERS:
  8548. * *cd - pointer to core data
  8549. ******************************************************************************/
  8550. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8551. {
  8552. struct device *dev = cd->dev;
  8553. int rc = 0;
  8554. /*
  8555. * After power on action, the chip can general FW sentinel. It can
  8556. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8557. * _fast_startup() can perform a quick enumeration too, here doesn't
  8558. * need another enumeration.
  8559. */
  8560. mutex_lock(&cd->system_lock);
  8561. cd->startup_status = STARTUP_STATUS_START;
  8562. cd->hid_reset_cmd_state = 1;
  8563. mutex_unlock(&cd->system_lock);
  8564. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8565. if (rc < 0) {
  8566. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8567. __func__, rc);
  8568. goto exit;
  8569. }
  8570. if (!cd->irq_enabled) {
  8571. cd->irq_enabled = true;
  8572. enable_irq(cd->irq);
  8573. }
  8574. /* TBD: following function doesn't update startup_status */
  8575. rc = _fast_startup(cd);
  8576. exit:
  8577. return rc;
  8578. }
  8579. /*******************************************************************************
  8580. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8581. *
  8582. * SUMMARY: Reset device, and then trigger a full enumeration.
  8583. *
  8584. * RETURN:
  8585. * 0 = success
  8586. * !0 = failure
  8587. *
  8588. * PARAMETERS:
  8589. * *cd - pointer to core data
  8590. ******************************************************************************/
  8591. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8592. {
  8593. int rc;
  8594. rc = pt_dut_reset_and_wait(cd);
  8595. if (rc < 0) {
  8596. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8597. __func__, rc);
  8598. goto exit;
  8599. }
  8600. rc = _fast_startup(cd);
  8601. exit:
  8602. return rc;
  8603. }
  8604. /*******************************************************************************
  8605. * FUNCTION: pt_core_easywake_off_
  8606. *
  8607. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8608. * the configuration in the core platform data structure.
  8609. *
  8610. * RETURN:
  8611. * 0 = success
  8612. * !0 = failure
  8613. *
  8614. * PARAMETERS:
  8615. * *cd - pointer to core data
  8616. ******************************************************************************/
  8617. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8618. {
  8619. int rc = 0;
  8620. mutex_lock(&cd->system_lock);
  8621. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8622. mutex_unlock(&cd->system_lock);
  8623. pt_debug(cd->dev, DL_INFO,
  8624. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8625. return 0;
  8626. }
  8627. mutex_unlock(&cd->system_lock);
  8628. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8629. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8630. rc = pt_core_wake_device_from_easy_wake_(cd);
  8631. if (rc < 0)
  8632. pt_debug(cd->dev, DL_ERROR,
  8633. "%s - %d failed %d\n", __func__, rc);
  8634. }
  8635. mutex_lock(&cd->system_lock);
  8636. cd->sleep_state = SS_EASY_WAKING_OFF;
  8637. mutex_unlock(&cd->system_lock);
  8638. pt_start_wd_timer(cd);
  8639. return rc;
  8640. }
  8641. /*******************************************************************************
  8642. * FUNCTION: pt_core_easywake_off
  8643. *
  8644. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8645. *
  8646. * RETURN:
  8647. * 0 = success
  8648. * !0 = failure
  8649. *
  8650. * PARAMETERS:
  8651. * *cd - pointer to core data
  8652. ******************************************************************************/
  8653. static int pt_core_easywake_off(struct pt_core_data *cd)
  8654. {
  8655. int rc;
  8656. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8657. if (rc < 0) {
  8658. pt_debug(cd->dev, DL_ERROR,
  8659. "%s: fail get exclusive ex=%p own=%p\n",
  8660. __func__, cd->exclusive_dev, cd->dev);
  8661. return rc;
  8662. }
  8663. rc = pt_core_easywake_off_(cd);
  8664. if (release_exclusive(cd, cd->dev) < 0)
  8665. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8666. __func__);
  8667. else
  8668. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8669. __func__);
  8670. return rc;
  8671. }
  8672. /*******************************************************************************
  8673. * FUNCTION: pt_core_wake_
  8674. *
  8675. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8676. * the configuration in the core platform data structure.
  8677. *
  8678. * RETURN:
  8679. * 0 = success
  8680. * !0 = failure
  8681. *
  8682. * PARAMETERS:
  8683. * *cd - pointer to core data
  8684. ******************************************************************************/
  8685. static int pt_core_wake_(struct pt_core_data *cd)
  8686. {
  8687. int rc = 0;
  8688. mutex_lock(&cd->system_lock);
  8689. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8690. mutex_unlock(&cd->system_lock);
  8691. pt_debug(cd->dev, DL_INFO,
  8692. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8693. return 0;
  8694. } else {
  8695. cd->sleep_state = SS_WAKING;
  8696. }
  8697. mutex_unlock(&cd->system_lock);
  8698. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8699. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8700. pt_debug(cd->dev, DL_INFO,
  8701. "%s: Entering into poweron mode:\n", __func__);
  8702. rc = pt_core_poweron_device_(cd);
  8703. if (rc < 0)
  8704. pr_err("%s: Poweron error detected: rc=%d\n",
  8705. __func__, rc);
  8706. }
  8707. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8708. rc = pt_core_wake_device_from_deep_standby_(cd);
  8709. else /* Default action to exit DeepSleep */
  8710. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8711. }
  8712. mutex_lock(&cd->system_lock);
  8713. cd->sleep_state = SS_SLEEP_OFF;
  8714. mutex_unlock(&cd->system_lock);
  8715. pt_start_wd_timer(cd);
  8716. return rc;
  8717. }
  8718. /*******************************************************************************
  8719. * FUNCTION: pt_core_wake_
  8720. *
  8721. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8722. *
  8723. * RETURN:
  8724. * 0 = success
  8725. * !0 = failure
  8726. *
  8727. * PARAMETERS:
  8728. * *cd - pointer to core data
  8729. ******************************************************************************/
  8730. static int pt_core_wake(struct pt_core_data *cd)
  8731. {
  8732. int rc = 0;
  8733. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8734. if (rc < 0) {
  8735. pt_debug(cd->dev, DL_ERROR,
  8736. "%s: fail get exclusive ex=%p own=%p\n",
  8737. __func__, cd->exclusive_dev, cd->dev);
  8738. return rc;
  8739. }
  8740. rc = pt_core_wake_(cd);
  8741. if (release_exclusive(cd, cd->dev) < 0)
  8742. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8743. __func__);
  8744. else
  8745. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8746. __func__);
  8747. return rc;
  8748. }
  8749. /*******************************************************************************
  8750. * FUNCTION: pt_get_ic_crc_
  8751. *
  8752. * SUMMARY: This function retrieves the config block CRC
  8753. *
  8754. * NOTE: The post condition of calling this function will be that the DUT will
  8755. * be in SCANNINING mode if no failures occur
  8756. *
  8757. * RETURN:
  8758. * 0 = success
  8759. * !0 = failure
  8760. *
  8761. * PARAMETERS:
  8762. * *cd - pointer the core data structure
  8763. * ebid - enumerated block ID
  8764. ******************************************************************************/
  8765. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8766. {
  8767. struct pt_sysinfo *si = &cd->sysinfo;
  8768. int rc = 0;
  8769. u8 status;
  8770. u16 calculated_crc = 0;
  8771. u16 stored_crc = 0;
  8772. rc = pt_pip_suspend_scanning_(cd);
  8773. if (rc)
  8774. goto error;
  8775. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8776. &calculated_crc, &stored_crc);
  8777. if (rc)
  8778. goto exit;
  8779. if (status) {
  8780. rc = -EINVAL;
  8781. goto exit;
  8782. }
  8783. si->ttconfig.crc = stored_crc;
  8784. exit:
  8785. pt_pip_resume_scanning_(cd);
  8786. error:
  8787. pt_debug(cd->dev, DL_INFO,
  8788. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8789. __func__, ebid, calculated_crc, stored_crc, rc);
  8790. return rc;
  8791. }
  8792. /*******************************************************************************
  8793. * FUNCTION: pt_pip2_read_gpio
  8794. *
  8795. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8796. * bits into the passed in variable
  8797. *
  8798. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8799. *
  8800. * RETURN:
  8801. * 0 = success
  8802. * !0 = failure
  8803. *
  8804. * PARAMETERS:
  8805. * *dev - pointer to device structure
  8806. * *status - pointer to where the command response status is stored
  8807. * *gpio - pointer to device attributes structure
  8808. ******************************************************************************/
  8809. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8810. {
  8811. int rc = 0;
  8812. u16 actual_read_len;
  8813. u8 read_buf[12];
  8814. u8 tmp_status = 0;
  8815. u8 index = PIP2_RESP_STATUS_OFFSET;
  8816. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8817. rc = _pt_request_pip2_send_cmd(dev,
  8818. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8819. NULL, 0, read_buf, &actual_read_len);
  8820. if (rc) {
  8821. pt_debug(dev, DL_ERROR,
  8822. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8823. rc = -ECOMM;
  8824. } else {
  8825. tmp_status = read_buf[index];
  8826. }
  8827. if (status)
  8828. *status = tmp_status;
  8829. if (!rc && gpio && (tmp_status == 0)) {
  8830. *gpio = ((read_buf[index + 4] << 24) |
  8831. (read_buf[index + 3] << 16) |
  8832. (read_buf[index + 2] << 8) |
  8833. (read_buf[index + 1]));
  8834. }
  8835. return rc;
  8836. }
  8837. /*******************************************************************************
  8838. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8839. *
  8840. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8841. *
  8842. * RETURN:
  8843. * 0 = success
  8844. * !0 = failure
  8845. *
  8846. * PARAMETERS:
  8847. * *cd - pointer the core data structure
  8848. * *pid - pointer to store panel ID
  8849. ******************************************************************************/
  8850. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8851. {
  8852. u32 gpio_value = 0;
  8853. u8 status = 0;
  8854. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8855. int rc = 0;
  8856. if (!pid)
  8857. return -ENOMEM;
  8858. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8859. if (!rc) {
  8860. if (status == 0) {
  8861. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8862. PT_PANEL_ID_SHIFT;
  8863. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8864. __func__,
  8865. "BL mode PID", panel_id, "gpio", gpio_value);
  8866. *pid = panel_id;
  8867. } else {
  8868. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8869. __func__,
  8870. "BL read gpio failed status", status);
  8871. }
  8872. } else {
  8873. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8874. __func__,
  8875. "BL read gpio failed status", status);
  8876. }
  8877. return rc;
  8878. }
  8879. /*******************************************************************************
  8880. * FUNCTION: pt_enum_with_dut_
  8881. *
  8882. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8883. * The core data (cd) startup_status will store, as a bitmask, each
  8884. * state of the enumeration process. The startup will be attempted
  8885. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8886. *
  8887. * RETURN:
  8888. * 0 = success
  8889. * !0 = failure
  8890. *
  8891. * PARAMETERS:
  8892. * *cd - pointer the core data structure
  8893. * reset - Flag to reset the DUT before attempting to enumerate
  8894. * *status - poionter to store the enum status bitmask flags
  8895. ******************************************************************************/
  8896. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8897. u32 *enum_status)
  8898. {
  8899. int try = 1;
  8900. int rc = 0;
  8901. int wait_time = 0;
  8902. bool detected = false;
  8903. u8 return_data[8];
  8904. u8 mode = PT_MODE_UNKNOWN;
  8905. u8 pid = PANEL_ID_NOT_ENABLED;
  8906. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8907. struct pt_hid_desc hid_desc;
  8908. memset(&hid_desc, 0, sizeof(hid_desc));
  8909. #ifdef TTHE_TUNER_SUPPORT
  8910. tthe_print(cd, NULL, 0, "enter startup");
  8911. #endif
  8912. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8913. __func__, cd->startup_status, reset);
  8914. pt_stop_wd_timer(cd);
  8915. reset:
  8916. if (try > 1)
  8917. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8918. __func__, try);
  8919. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8920. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8921. pt_debug(cd->dev, DL_INFO,
  8922. "%s: PIP1 Enumeration start\n", __func__);
  8923. /* Only reset the DUT after the first try */
  8924. if (reset || (try > 1)) {
  8925. /*
  8926. * Reset hardware only for Legacy parts. Skip for TT/TC
  8927. * parts because if the FW image was loaded directly
  8928. * to SRAM issueing a reset ill wipe out what was just
  8929. * loaded.
  8930. */
  8931. rc = pt_dut_reset_and_wait(cd);
  8932. if (rc < 0) {
  8933. pt_debug(cd->dev, DL_ERROR,
  8934. "%s: Error on h/w reset r=%d\n",
  8935. __func__, rc);
  8936. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8937. goto reset;
  8938. goto exit;
  8939. }
  8940. /* sleep to allow FW to be launched if available */
  8941. msleep(120);
  8942. }
  8943. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8944. if (rc < 0) {
  8945. pt_debug(cd->dev, DL_ERROR,
  8946. "%s: Error getting HID Descriptor r=%d\n",
  8947. __func__, rc);
  8948. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8949. goto reset;
  8950. rc = -EIO;
  8951. goto exit;
  8952. }
  8953. detected = true;
  8954. cd->mode = pt_get_mode(cd, &hid_desc);
  8955. /*
  8956. * Most systems do not use an XY pin as the panel_id and so
  8957. * the BL is used to retrieve the panel_id, however for
  8958. * systems that do use an XY pin, the panel_id MUST be
  8959. * retrieved from the system information when running FW
  8960. * (done below) and so this section of code is skipped.
  8961. * Entering the BL here is only needed on XY_PIN systems.
  8962. */
  8963. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8964. if (cd->mode == PT_MODE_OPERATIONAL) {
  8965. rc = pt_pip_start_bootloader_(cd);
  8966. if (rc < 0) {
  8967. pt_debug(cd->dev, DL_ERROR,
  8968. "%s: Error on start bootloader r=%d\n",
  8969. __func__, rc);
  8970. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8971. goto reset;
  8972. goto exit;
  8973. }
  8974. cd->mode = PT_MODE_BOOTLOADER;
  8975. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8976. __func__);
  8977. }
  8978. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8979. if (!rc) {
  8980. cd->bl_info.ready = true;
  8981. cd->bl_info.chip_id =
  8982. get_unaligned_le16(&return_data[2]);
  8983. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8984. __func__, cd->bl_info.chip_id);
  8985. } else {
  8986. pt_debug(cd->dev, DL_ERROR,
  8987. "%s: failed to get chip ID, r=%d\n",
  8988. __func__, rc);
  8989. }
  8990. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8991. mutex_lock(&cd->system_lock);
  8992. if (!rc) {
  8993. cd->pid_for_loader = pid;
  8994. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8995. __func__, cd->pid_for_loader);
  8996. } else {
  8997. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8998. pt_debug(cd->dev, DL_WARN,
  8999. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9000. __func__, cd->pid_for_loader);
  9001. }
  9002. mutex_unlock(&cd->system_lock);
  9003. }
  9004. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9005. if (cd->mode == PT_MODE_BOOTLOADER) {
  9006. rc = pt_hid_output_bl_launch_app_(cd);
  9007. if (rc < 0) {
  9008. pt_debug(cd->dev, DL_ERROR,
  9009. "%s: Error on launch app r=%d\n",
  9010. __func__, rc);
  9011. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9012. goto reset;
  9013. rc = -ENODEV;
  9014. goto exit;
  9015. }
  9016. /* sleep to allow FW to be launched if available */
  9017. msleep(120);
  9018. /* Ensure the DUT is now in Application mode */
  9019. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9020. if (rc < 0) {
  9021. pt_debug(cd->dev, DL_ERROR,
  9022. "%s: Error getting HID Desc r=%d\n",
  9023. __func__, rc);
  9024. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9025. goto reset;
  9026. rc = -EIO;
  9027. goto exit;
  9028. }
  9029. cd->mode = pt_get_mode(cd, &hid_desc);
  9030. if (cd->mode == PT_MODE_BOOTLOADER) {
  9031. pt_debug(cd->dev, DL_WARN,
  9032. "%s: Error confiming exit BL\n",
  9033. __func__);
  9034. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9035. goto reset;
  9036. rc = -ENODEV;
  9037. goto exit;
  9038. }
  9039. }
  9040. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9041. cd->mode = PT_MODE_OPERATIONAL;
  9042. *enum_status |= STARTUP_STATUS_GET_DESC;
  9043. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9044. } else {
  9045. /* Generation is PIP2 Capable */
  9046. pt_debug(cd->dev, DL_INFO,
  9047. "%s: PIP2 Enumeration start\n", __func__);
  9048. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9049. if (rc) {
  9050. pt_debug(cd->dev, DL_ERROR,
  9051. "%s: Get mode failed, mode unknown\n",
  9052. __func__);
  9053. mode = PT_MODE_UNKNOWN;
  9054. } else
  9055. detected = true;
  9056. cd->mode = mode;
  9057. switch (cd->mode) {
  9058. case PT_MODE_OPERATIONAL:
  9059. pt_debug(cd->dev, DL_INFO,
  9060. "%s: Operational mode\n", __func__);
  9061. if (cd->app_pip_ver_ready == false) {
  9062. rc = pt_pip2_get_version_(cd);
  9063. if (!rc)
  9064. cd->app_pip_ver_ready = true;
  9065. else {
  9066. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9067. goto reset;
  9068. goto exit;
  9069. }
  9070. }
  9071. break;
  9072. case PT_MODE_BOOTLOADER:
  9073. pt_debug(cd->dev, DL_INFO,
  9074. "%s: Bootloader mode\n", __func__);
  9075. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9076. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9077. mutex_lock(&cd->system_lock);
  9078. if (!rc) {
  9079. cd->pid_for_loader = pid;
  9080. pt_debug(cd->dev, DL_INFO,
  9081. "%s: Panel ID: 0x%02X\n",
  9082. __func__, cd->pid_for_loader);
  9083. } else {
  9084. cd->pid_for_loader =
  9085. PANEL_ID_NOT_ENABLED;
  9086. pt_debug(cd->dev, DL_WARN,
  9087. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9088. __func__, cd->pid_for_loader);
  9089. }
  9090. mutex_unlock(&cd->system_lock);
  9091. }
  9092. if (cd->bl_pip_ver_ready == false) {
  9093. rc = pt_pip2_get_version_(cd);
  9094. if (!rc)
  9095. cd->bl_pip_ver_ready = true;
  9096. else {
  9097. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9098. goto reset;
  9099. goto exit;
  9100. }
  9101. }
  9102. /*
  9103. * Launch app command will fail in flashless mode.
  9104. * Skip launch app command here to save time for
  9105. * enumeration flow.
  9106. */
  9107. if (cd->flashless_dut)
  9108. goto exit;
  9109. /*
  9110. * pt_pip2_launch_app() is needed here instead of
  9111. * pt_pip2_exit_bl() because exit_bl will cause several
  9112. * hw_resets to occur and the auto BL on a flashless
  9113. * DUT will fail.
  9114. */
  9115. rc = pt_pip2_launch_app(cd->dev,
  9116. PT_CORE_CMD_UNPROTECTED);
  9117. if (rc) {
  9118. pt_debug(cd->dev, DL_ERROR,
  9119. "%s: Error on launch app r=%d\n",
  9120. __func__, rc);
  9121. msleep(50);
  9122. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9123. goto reset;
  9124. goto exit;
  9125. }
  9126. /*
  9127. * IRQ thread can be delayed if the serial log print is
  9128. * enabled. It causes next command to get wrong response
  9129. * Here the delay is to ensure pt_parse_input() to be
  9130. * finished.
  9131. */
  9132. msleep(60);
  9133. /* Check and update the mode */
  9134. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9135. if (rc) {
  9136. pt_debug(cd->dev, DL_ERROR,
  9137. "%s: Get mode failed, mode unknown\n",
  9138. __func__);
  9139. mode = PT_MODE_UNKNOWN;
  9140. }
  9141. cd->mode = mode;
  9142. if (cd->mode == PT_MODE_OPERATIONAL) {
  9143. pt_debug(cd->dev, DL_INFO,
  9144. "%s: Launched to Operational mode\n",
  9145. __func__);
  9146. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9147. pt_debug(cd->dev, DL_ERROR,
  9148. "%s: Launch failed, Bootloader mode\n",
  9149. __func__);
  9150. goto exit;
  9151. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9152. pt_debug(cd->dev, DL_ERROR,
  9153. "%s: Launch failed, Unknown mode\n",
  9154. __func__);
  9155. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9156. goto reset;
  9157. goto exit;
  9158. }
  9159. if (cd->app_pip_ver_ready == false) {
  9160. rc = pt_pip2_get_version_(cd);
  9161. if (!rc)
  9162. cd->app_pip_ver_ready = true;
  9163. else {
  9164. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9165. goto reset;
  9166. goto exit;
  9167. }
  9168. }
  9169. break;
  9170. default:
  9171. pt_debug(cd->dev, DL_ERROR,
  9172. "%s: Unknown mode\n", __func__);
  9173. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9174. goto reset;
  9175. goto exit;
  9176. }
  9177. *enum_status |= STARTUP_STATUS_GET_DESC;
  9178. }
  9179. pt_init_pip_report_fields(cd);
  9180. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9181. if (!cd->features.easywake)
  9182. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9183. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9184. rc = pt_hid_output_get_sysinfo_(cd);
  9185. if (rc) {
  9186. pt_debug(cd->dev, DL_ERROR,
  9187. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9188. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9189. goto reset;
  9190. goto exit;
  9191. }
  9192. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9193. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9194. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9195. if (!rc) {
  9196. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9197. pt_debug(cd->dev, DL_WARN,
  9198. "%s: Exit FW BOOT Mode after %dms\n",
  9199. __func__, wait_time);
  9200. } else {
  9201. pt_debug(cd->dev, DL_WARN,
  9202. "%s: FW stuck in BOOT Mode after %dms\n",
  9203. __func__, wait_time);
  9204. goto exit;
  9205. }
  9206. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9207. __func__,
  9208. cd->sysinfo.ttdata.pip_ver_major,
  9209. cd->sysinfo.ttdata.pip_ver_minor);
  9210. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9211. if (rc) {
  9212. pt_debug(cd->dev, DL_ERROR,
  9213. "%s: DUT Config block CRC failure rc=%d\n",
  9214. __func__, rc);
  9215. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9216. goto reset;
  9217. } else {
  9218. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9219. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9220. pt_debug(cd->dev, DL_ERROR,
  9221. "%s: scan state: %d, retry: %d\n",
  9222. __func__, sys_mode, try);
  9223. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9224. goto reset;
  9225. } else
  9226. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9227. }
  9228. rc = pt_restore_parameters_(cd);
  9229. if (rc) {
  9230. pt_debug(cd->dev, DL_ERROR,
  9231. "%s: Failed to restore parameters rc=%d\n",
  9232. __func__, rc);
  9233. } else
  9234. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9235. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9236. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9237. cd->startup_retry_count = 0;
  9238. exit:
  9239. /* Generate the HW Version of the connected DUT and store in cd */
  9240. pt_generate_hw_version(cd, cd->hw_version);
  9241. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9242. cd->hw_version);
  9243. pt_start_wd_timer(cd);
  9244. if (!detected)
  9245. rc = -ENODEV;
  9246. #ifdef TTHE_TUNER_SUPPORT
  9247. tthe_print(cd, NULL, 0, "exit startup");
  9248. #endif
  9249. pt_debug(cd->dev, DL_WARN,
  9250. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9251. __func__, rc, try);
  9252. return rc;
  9253. }
  9254. /*******************************************************************************
  9255. * FUNCTION: pt_enum_with_dut
  9256. *
  9257. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9258. * requesting exclusive access around the call.
  9259. *
  9260. * RETURN:
  9261. * 0 = success
  9262. * !0 = failure
  9263. *
  9264. * PARAMETERS:
  9265. * *cd - pointer the core data structure
  9266. * reset - Flag to reset the DUT before attempting to enumerate
  9267. * *status - pointer to store the enum status bitmask flags
  9268. ******************************************************************************/
  9269. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9270. {
  9271. int rc = 0;
  9272. mutex_lock(&cd->system_lock);
  9273. cd->startup_state = STARTUP_RUNNING;
  9274. mutex_unlock(&cd->system_lock);
  9275. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9276. if (rc) {
  9277. pt_debug(cd->dev, DL_ERROR,
  9278. "%s: fail get exclusive ex=%p own=%p\n",
  9279. __func__, cd->exclusive_dev, cd->dev);
  9280. goto exit;
  9281. }
  9282. rc = pt_enum_with_dut_(cd, reset, status);
  9283. if (release_exclusive(cd, cd->dev) < 0)
  9284. /* Don't return fail code, mode is already changed. */
  9285. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9286. __func__);
  9287. else
  9288. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9289. __func__);
  9290. exit:
  9291. mutex_lock(&cd->system_lock);
  9292. /* Clear startup state for any tasks waiting for startup completion */
  9293. cd->startup_state = STARTUP_NONE;
  9294. mutex_unlock(&cd->system_lock);
  9295. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9296. *status |= STARTUP_STATUS_COMPLETE;
  9297. /* Wake the waiters for end of startup */
  9298. wake_up(&cd->wait_q);
  9299. return rc;
  9300. }
  9301. static int add_sysfs_interfaces(struct device *dev);
  9302. static void remove_sysfs_interfaces(struct device *dev);
  9303. static void remove_sysfs_and_modules(struct device *dev);
  9304. static void pt_release_modules(struct pt_core_data *cd);
  9305. static void pt_probe_modules(struct pt_core_data *cd);
  9306. /*******************************************************************************
  9307. * FUNCTION: _pt_ttdl_restart
  9308. *
  9309. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9310. *
  9311. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9312. * existing sysfs nodes will produce an error.
  9313. *
  9314. * RETURN:
  9315. * 0 = success
  9316. * !0 = failure
  9317. *
  9318. * PARAMETERS:
  9319. * *dev - pointer to core device
  9320. ******************************************************************************/
  9321. static int _pt_ttdl_restart(struct device *dev)
  9322. {
  9323. int rc = 0;
  9324. struct pt_core_data *cd = dev_get_drvdata(dev);
  9325. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9326. struct i2c_client *client =
  9327. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9328. #endif
  9329. /*
  9330. * Make sure the device is awake, pt_mt_release function will
  9331. * cause pm sleep function and lead to deadlock.
  9332. */
  9333. pm_runtime_get_sync(dev);
  9334. /* Use ttdl_restart_lock to avoid reentry */
  9335. mutex_lock(&cd->ttdl_restart_lock);
  9336. remove_sysfs_and_modules(cd->dev);
  9337. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9338. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9339. pt_debug(dev, DL_ERROR,
  9340. "%s I2C functionality not Supported\n", __func__);
  9341. rc = -EIO;
  9342. goto ttdl_no_error;
  9343. }
  9344. #endif
  9345. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9346. rc = _pt_detect_dut_generation(cd->dev,
  9347. &cd->startup_status, &cd->active_dut_generation,
  9348. &cd->mode);
  9349. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9350. pt_debug(dev, DL_ERROR,
  9351. "%s: Error, Unknown DUT Generation rc=%d\n",
  9352. __func__, rc);
  9353. }
  9354. }
  9355. rc = add_sysfs_interfaces(cd->dev);
  9356. if (rc < 0)
  9357. pt_debug(cd->dev, DL_ERROR,
  9358. "%s: Error, failed sysfs nodes rc=%d\n",
  9359. __func__, rc);
  9360. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9361. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9362. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9363. if (rc < 0)
  9364. pt_debug(dev, DL_ERROR,
  9365. "%s: Error, Failed to Enumerate\n", __func__);
  9366. }
  9367. rc = pt_mt_probe(dev);
  9368. if (rc < 0) {
  9369. pt_debug(dev, DL_ERROR,
  9370. "%s: Error, fail mt probe\n", __func__);
  9371. }
  9372. rc = pt_btn_probe(dev);
  9373. if (rc < 0) {
  9374. pt_debug(dev, DL_ERROR,
  9375. "%s: Error, fail btn probe\n", __func__);
  9376. }
  9377. pt_probe_modules(cd);
  9378. pt_debug(cd->dev, DL_WARN,
  9379. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9380. rc = 0;
  9381. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9382. ttdl_no_error:
  9383. #endif
  9384. mutex_unlock(&cd->ttdl_restart_lock);
  9385. mutex_lock(&cd->system_lock);
  9386. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9387. cd->startup_state = STARTUP_NONE;
  9388. mutex_unlock(&cd->system_lock);
  9389. pm_runtime_put(dev);
  9390. return rc;
  9391. }
  9392. /*******************************************************************************
  9393. * FUNCTION: pt_restart_work_function
  9394. *
  9395. * SUMMARY: This is the wrapper function placed in a work queue to call
  9396. * _pt_ttdl_restart()
  9397. *
  9398. * RETURN: none
  9399. *
  9400. * PARAMETERS:
  9401. * *work - pointer to the work_struct
  9402. ******************************************************************************/
  9403. static void pt_restart_work_function(struct work_struct *work)
  9404. {
  9405. struct pt_core_data *cd = container_of(work,
  9406. struct pt_core_data, ttdl_restart_work);
  9407. int rc = 0;
  9408. rc = _pt_ttdl_restart(cd->dev);
  9409. if (rc < 0)
  9410. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9411. __func__, rc);
  9412. }
  9413. /*******************************************************************************
  9414. * FUNCTION: pt_enum_work_function
  9415. *
  9416. * SUMMARY: This is the wrapper function placed in a work queue to call
  9417. * pt_enum_with_dut()
  9418. *
  9419. * RETURN: none
  9420. *
  9421. * PARAMETERS:
  9422. * *work - pointer to the work_struct
  9423. ******************************************************************************/
  9424. static void pt_enum_work_function(struct work_struct *work)
  9425. {
  9426. struct pt_core_data *cd = container_of(work,
  9427. struct pt_core_data, enum_work);
  9428. int rc;
  9429. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9430. if (rc < 0)
  9431. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9432. __func__, rc);
  9433. }
  9434. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9435. {
  9436. int rc;
  9437. if (!get) {
  9438. rc = 0;
  9439. goto regulator_put;
  9440. }
  9441. cd->vdd = regulator_get(cd->dev, "vdd");
  9442. if (IS_ERR(cd->vdd)) {
  9443. rc = PTR_ERR(cd->vdd);
  9444. dev_err(cd->dev,
  9445. "Regulator get failed vdd rc=%d\n", rc);
  9446. goto regulator_put;
  9447. }
  9448. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9449. if (IS_ERR(cd->vcc_i2c)) {
  9450. rc = PTR_ERR(cd->vcc_i2c);
  9451. dev_err(cd->dev,
  9452. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9453. goto regulator_put;
  9454. }
  9455. return 0;
  9456. regulator_put:
  9457. if (cd->vdd) {
  9458. regulator_put(cd->vdd);
  9459. cd->vdd = NULL;
  9460. }
  9461. if (cd->vcc_i2c) {
  9462. regulator_put(cd->vcc_i2c);
  9463. cd->vcc_i2c = NULL;
  9464. }
  9465. return rc;
  9466. }
  9467. #ifdef ENABLE_I2C_REG_ONLY
  9468. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9469. {
  9470. int rc;
  9471. if (!en) {
  9472. rc = 0;
  9473. goto disable_vcc_i2c_reg_only;
  9474. }
  9475. if (cd->vcc_i2c) {
  9476. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9477. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9478. FT_I2C_VTG_MAX_UV);
  9479. if (rc) {
  9480. dev_err(cd->dev,
  9481. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9482. goto disable_vcc_i2c_reg_only;
  9483. }
  9484. }
  9485. rc = regulator_enable(cd->vcc_i2c);
  9486. if (rc) {
  9487. dev_err(cd->dev,
  9488. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9489. goto disable_vcc_i2c_reg_only;
  9490. }
  9491. }
  9492. return 0;
  9493. disable_vcc_i2c_reg_only:
  9494. if (cd->vcc_i2c) {
  9495. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9496. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9497. FT_I2C_VTG_MAX_UV);
  9498. regulator_disable(cd->vcc_i2c);
  9499. }
  9500. return rc;
  9501. }
  9502. #endif
  9503. #ifdef ENABLE_VDD_REG_ONLY
  9504. static int pt_enable_vdd_regulator(struct pt_core_data *cd, bool en)
  9505. {
  9506. int rc;
  9507. if (!en) {
  9508. rc = 0;
  9509. goto disable_vdd_reg_only;
  9510. }
  9511. if (cd->vdd) {
  9512. if (regulator_count_voltages(cd->vdd) > 0) {
  9513. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9514. FT_VTG_MAX_UV);
  9515. if (rc) {
  9516. dev_err(cd->dev,
  9517. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9518. goto disable_vdd_reg_only;
  9519. }
  9520. }
  9521. rc = regulator_enable(cd->vdd);
  9522. if (rc) {
  9523. dev_err(cd->dev,
  9524. "Regulator vdd enable failed rc=%d\n", rc);
  9525. goto disable_vdd_reg_only;
  9526. }
  9527. }
  9528. return 0;
  9529. disable_vdd_reg_only:
  9530. if (cd->vdd) {
  9531. if (regulator_count_voltages(cd->vdd) > 0)
  9532. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9533. FT_VTG_MAX_UV);
  9534. regulator_disable(cd->vdd);
  9535. }
  9536. return rc;
  9537. }
  9538. #endif
  9539. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9540. {
  9541. int rc;
  9542. if (!en) {
  9543. rc = 0;
  9544. goto disable_vcc_i2c_reg;
  9545. }
  9546. if (cd->vdd) {
  9547. if (regulator_count_voltages(cd->vdd) > 0) {
  9548. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9549. FT_VTG_MAX_UV);
  9550. if (rc) {
  9551. dev_err(cd->dev,
  9552. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9553. goto exit;
  9554. }
  9555. }
  9556. rc = regulator_enable(cd->vdd);
  9557. if (rc) {
  9558. dev_err(cd->dev,
  9559. "Regulator vdd enable failed rc=%d\n", rc);
  9560. goto exit;
  9561. }
  9562. }
  9563. if (cd->vcc_i2c) {
  9564. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9565. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9566. FT_I2C_VTG_MAX_UV);
  9567. if (rc) {
  9568. dev_err(cd->dev,
  9569. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9570. goto disable_vdd_reg;
  9571. }
  9572. }
  9573. rc = regulator_enable(cd->vcc_i2c);
  9574. if (rc) {
  9575. dev_err(cd->dev,
  9576. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9577. goto disable_vdd_reg;
  9578. }
  9579. }
  9580. return 0;
  9581. disable_vcc_i2c_reg:
  9582. if (cd->vcc_i2c) {
  9583. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9584. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9585. FT_I2C_VTG_MAX_UV);
  9586. regulator_disable(cd->vcc_i2c);
  9587. }
  9588. disable_vdd_reg:
  9589. if (cd->vdd) {
  9590. if (regulator_count_voltages(cd->vdd) > 0)
  9591. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9592. FT_VTG_MAX_UV);
  9593. regulator_disable(cd->vdd);
  9594. }
  9595. exit:
  9596. return rc;
  9597. }
  9598. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9599. #define KERNEL_VER_GT_3_19
  9600. #endif
  9601. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9602. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9603. #if defined(CONFIG_PM_SLEEP)
  9604. /*******************************************************************************
  9605. * FUNCTION: pt_core_rt_suspend
  9606. *
  9607. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9608. *
  9609. * RETURN:
  9610. * 0 = success
  9611. * !0 = failure
  9612. *
  9613. * PARAMETERS:
  9614. * *dev - pointer to core device
  9615. ******************************************************************************/
  9616. static int pt_core_rt_suspend(struct device *dev)
  9617. {
  9618. struct pt_core_data *cd = dev_get_drvdata(dev);
  9619. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9620. __func__, cd->core_probe_complete);
  9621. return 0;
  9622. }
  9623. /*******************************************************************************
  9624. * FUNCTION: pt_core_rt_resume
  9625. *
  9626. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9627. *
  9628. * RETURN:
  9629. * 0 = success
  9630. * !0 = failure
  9631. *
  9632. * PARAMETERS:
  9633. * *dev - pointer to core device
  9634. ******************************************************************************/
  9635. static int pt_core_rt_resume(struct device *dev)
  9636. {
  9637. struct pt_core_data *cd = dev_get_drvdata(dev);
  9638. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9639. __func__, cd->core_probe_complete);
  9640. return 0;
  9641. }
  9642. #endif /* CONFIG_PM_SLEEP */
  9643. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9644. #if defined(CONFIG_PM_SLEEP)
  9645. /*******************************************************************************
  9646. * FUNCTION: pt_core_suspend_
  9647. *
  9648. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9649. * pt_core_sleep. This function may disable IRQ during sleep state.
  9650. *
  9651. * RETURN:
  9652. * 0 = success
  9653. * !0 = failure
  9654. *
  9655. * PARAMETERS:
  9656. * *dev - pointer to core device
  9657. ******************************************************************************/
  9658. static int pt_core_suspend_(struct device *dev)
  9659. {
  9660. int rc;
  9661. struct pt_core_data *cd = dev_get_drvdata(dev);
  9662. pt_debug(dev, DL_INFO, "%s: Entering into suspend mode:\n",
  9663. __func__);
  9664. rc = pt_core_sleep(cd);
  9665. if (rc) {
  9666. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9667. return -EAGAIN;
  9668. }
  9669. #ifdef ENABLE_VDD_REG_ONLY
  9670. rc = pt_enable_vdd_regulator(cd, false);
  9671. if (rc) {
  9672. dev_err(dev, "%s: Failed to disable vdd regulators: rc=%d\n",
  9673. __func__, rc);
  9674. }
  9675. #endif
  9676. #ifdef ENABLE_I2C_REG_ONLY
  9677. rc = pt_enable_i2c_regulator(cd, false);
  9678. if (rc) {
  9679. dev_err(dev, "%s: Failed to disable vdd regulators: rc=%d\n",
  9680. __func__, rc);
  9681. }
  9682. #endif
  9683. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9684. return 0;
  9685. /* Required to prevent interrupts before bus awake */
  9686. disable_irq(cd->irq);
  9687. cd->irq_disabled = 1;
  9688. if (device_may_wakeup(dev)) {
  9689. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9690. __func__);
  9691. if (!enable_irq_wake(cd->irq))
  9692. cd->irq_wake = 1;
  9693. } else {
  9694. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9695. __func__);
  9696. }
  9697. return rc;
  9698. }
  9699. /*******************************************************************************
  9700. * FUNCTION: pt_core_suspend
  9701. *
  9702. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9703. * woke up or put to sleep based on Kernel power state even when the display
  9704. * is off based on the check of TTDL core platform flag.
  9705. *
  9706. * RETURN:
  9707. * 0 = success
  9708. * !0 = failure
  9709. *
  9710. * PARAMETERS:
  9711. * *dev - pointer to core device
  9712. ******************************************************************************/
  9713. static int pt_core_suspend(struct device *dev)
  9714. {
  9715. struct pt_core_data *cd = dev_get_drvdata(dev);
  9716. int rc = 0;
  9717. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9718. return 0;
  9719. rc = pt_core_suspend_(dev);
  9720. pt_debug(dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9721. return 0;
  9722. }
  9723. /*******************************************************************************
  9724. * FUNCTION: pt_core_resume_
  9725. *
  9726. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9727. * pt_core_wake. This function may enable IRQ before wake up.
  9728. *
  9729. * RETURN:
  9730. * 0 = success
  9731. * !0 = failure
  9732. *
  9733. * PARAMETERS:
  9734. * *dev - pointer to core device
  9735. ******************************************************************************/
  9736. static int pt_core_resume_(struct device *dev)
  9737. {
  9738. int rc = 0;
  9739. struct pt_core_data *cd = dev_get_drvdata(dev);
  9740. dev_info(dev, "%s: Entering into resume mode:\n",
  9741. __func__);
  9742. #ifdef ENABLE_VDD_REG_ONLY
  9743. rc = pt_enable_vdd_regulator(cd, true);
  9744. if (rc < 0) {
  9745. dev_err(dev, "%s: Failed to enable vdd regulators: rc=%d\n",
  9746. __func__, rc);
  9747. }
  9748. #endif
  9749. #ifdef ENABLE_I2C_REG_ONLY
  9750. rc = pt_enable_i2c_regulator(cd, true);
  9751. if (rc < 0) {
  9752. dev_err(dev, "%s: Failed to enable vdd regulators: rc=%d\n",
  9753. __func__, rc);
  9754. }
  9755. #endif
  9756. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9757. __func__, rc);
  9758. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9759. goto exit;
  9760. /*
  9761. * Bus pm does not call suspend if device runtime suspended
  9762. * This flag is covers that case
  9763. */
  9764. if (cd->irq_disabled) {
  9765. enable_irq(cd->irq);
  9766. cd->irq_disabled = 0;
  9767. }
  9768. if (device_may_wakeup(dev)) {
  9769. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9770. __func__);
  9771. if (cd->irq_wake) {
  9772. disable_irq_wake(cd->irq);
  9773. cd->irq_wake = 0;
  9774. }
  9775. } else {
  9776. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9777. __func__);
  9778. }
  9779. exit:
  9780. rc = pt_core_wake(cd);
  9781. if (rc) {
  9782. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9783. __func__, rc);
  9784. return -EAGAIN;
  9785. }
  9786. return rc;
  9787. }
  9788. /*******************************************************************************
  9789. * FUNCTION: resume_offload_work
  9790. *
  9791. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9792. * along with kernel power state even the display is off based on the check of
  9793. * TTDL core platform flag.
  9794. *
  9795. * RETURN:
  9796. * 0 = success
  9797. * !0 = failure
  9798. *
  9799. * PARAMETERS:
  9800. * *dev - pointer to core device
  9801. ******************************************************************************/
  9802. static void pt_resume_offload_work(struct work_struct *work)
  9803. {
  9804. int rc = 0;
  9805. int retry_count = 10;
  9806. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9807. resume_offload_work);
  9808. do {
  9809. retry_count--;
  9810. rc = pt_core_resume_(pt_data->dev);
  9811. if (rc < 0)
  9812. pt_debug(pt_data->dev, DL_ERROR,
  9813. "%s: Error on wake\n", __func__);
  9814. } while (retry_count && rc < 0);
  9815. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9816. rc = pt_core_easywake_on(pt_data);
  9817. if (rc < 0) {
  9818. pt_debug(pt_data->dev, DL_ERROR,
  9819. "%s: Error on enable touch to wake key\n",
  9820. __func__);
  9821. return;
  9822. }
  9823. pt_data->fb_state = FB_OFF;
  9824. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9825. #endif
  9826. }
  9827. /*******************************************************************************
  9828. * FUNCTION: pt_core_resume
  9829. *
  9830. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9831. * along with kernel power state even the display is off based on the check of
  9832. * TTDL core platform flag.
  9833. *
  9834. * RETURN:
  9835. * 0 = success
  9836. * !0 = failure
  9837. *
  9838. * PARAMETERS:
  9839. * *dev - pointer to core device
  9840. ******************************************************************************/
  9841. static int pt_core_resume(struct device *dev)
  9842. {
  9843. struct pt_core_data *cd = dev_get_drvdata(dev);
  9844. int rc = 0;
  9845. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9846. return 0;
  9847. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9848. pt_debug(cd->dev, DL_ERROR, "%s End\n", __func__);
  9849. return rc;
  9850. }
  9851. #endif
  9852. #ifdef NEED_SUSPEND_NOTIFIER
  9853. /*******************************************************************************
  9854. * FUNCTION: pt_pm_notifier
  9855. *
  9856. * SUMMARY: This function is registered to notifier chain and will perform
  9857. * suspend operation if match event PM_SUSPEND_PREPARE.
  9858. *
  9859. * RETURN:
  9860. * 0 = success
  9861. * !0 = failure
  9862. *
  9863. * PARAMETERS:
  9864. * *nb - pointer to notifier_block structure
  9865. * action - notifier event type
  9866. * *data - void pointer
  9867. ******************************************************************************/
  9868. static int pt_pm_notifier(struct notifier_block *nb,
  9869. unsigned long action, void *data)
  9870. {
  9871. struct pt_core_data *cd = container_of(nb,
  9872. struct pt_core_data, pm_notifier);
  9873. if (action == PM_SUSPEND_PREPARE) {
  9874. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9875. __func__);
  9876. /*
  9877. * If PM runtime is not suspended, either call runtime
  9878. * PM suspend callback or wait until it finishes
  9879. */
  9880. if (!pm_runtime_suspended(cd->dev))
  9881. pm_runtime_suspend(cd->dev);
  9882. (void) pt_core_suspend(cd->dev);
  9883. }
  9884. return NOTIFY_DONE;
  9885. }
  9886. #endif
  9887. const struct dev_pm_ops pt_pm_ops = {
  9888. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9889. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9890. NULL)
  9891. };
  9892. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9893. /*******************************************************************************
  9894. * FUNCTION: _pt_request_pip2_enter_bl
  9895. *
  9896. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9897. * XRES pin or a soft reset.
  9898. *
  9899. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9900. * the WD active could cause this function to fail!
  9901. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9902. * will not try to determine the current mode but will proceed with
  9903. * resetting the DUT and entering the BL.
  9904. *
  9905. * NOTE: The definition of result code:
  9906. * PT_ENTER_BL_PASS (0)
  9907. * PT_ENTER_BL_ERROR (1)
  9908. * PT_ENTER_BL_RESET_FAIL (2)
  9909. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9910. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9911. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9912. *
  9913. * RETURNS:
  9914. * 0 = success
  9915. * !0 = failure
  9916. *
  9917. *
  9918. * PARAMETERS:
  9919. * *dev - pointer to device structure
  9920. * *start_mode - pointer to the mode the DUT was in when this function
  9921. * starts
  9922. * *result - pointer to store the result when to enter BL
  9923. ******************************************************************************/
  9924. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9925. {
  9926. int rc = 0;
  9927. int t;
  9928. int tmp_result = PT_ENTER_BL_ERROR;
  9929. int flash_info_retry = 2;
  9930. u8 mode = PT_MODE_UNKNOWN;
  9931. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9932. u8 read_buf[32];
  9933. u16 actual_read_len;
  9934. struct pt_core_data *cd = dev_get_drvdata(dev);
  9935. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9936. u8 time = 0;
  9937. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9938. if (cd->watchdog_enabled) {
  9939. pt_debug(dev, DL_WARN,
  9940. "%s: Watchdog must be stopped before entering BL\n",
  9941. __func__);
  9942. goto exit;
  9943. }
  9944. cancel_work_sync(&cd->enum_work);
  9945. cancel_work_sync(&cd->watchdog_work);
  9946. /* if undefined assume operational/test to bypass all checks */
  9947. if (*start_mode == PT_MODE_IGNORE) {
  9948. mode = PT_MODE_OPERATIONAL;
  9949. sys_mode = FW_SYS_MODE_TEST;
  9950. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9951. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9952. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9953. if (rc) {
  9954. pt_debug(dev, DL_ERROR,
  9955. "%s: Get mode failed, mode unknown\n",
  9956. __func__);
  9957. }
  9958. *start_mode = mode;
  9959. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9960. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9961. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9962. sys_mode = FW_SYS_MODE_SCANNING;
  9963. }
  9964. _retry:
  9965. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9966. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9967. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9968. switch (mode) {
  9969. case PT_MODE_UNKNOWN:
  9970. /*
  9971. * When the mode could not be determined the DUT could be
  9972. * in App mode running corrupted FW or FW that is not
  9973. * responding to the mode request, assume no communication
  9974. * and do a hard reset
  9975. */
  9976. mutex_lock(&cd->system_lock);
  9977. cd->startup_status = STARTUP_STATUS_START;
  9978. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9979. mutex_unlock(&cd->system_lock);
  9980. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9981. if (rc) {
  9982. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9983. goto exit;
  9984. }
  9985. break;
  9986. case PT_MODE_OPERATIONAL:
  9987. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9988. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9989. __func__);
  9990. rc = pt_pip_suspend_scanning_(cd);
  9991. if (rc) {
  9992. /*
  9993. * Print to log but don't exit, the FW could be
  9994. * running but be hung or fail to respond to
  9995. * this request
  9996. */
  9997. pt_debug(dev, DL_ERROR,
  9998. "%s Suspend Scan Failed\n", __func__);
  9999. }
  10000. /* sleep to allow the suspend scan to be processed */
  10001. usleep_range(1000, 2000);
  10002. }
  10003. mutex_lock(&cd->system_lock);
  10004. cd->startup_status = STARTUP_STATUS_START;
  10005. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10006. mutex_unlock(&cd->system_lock);
  10007. /* Reset device to enter the BL */
  10008. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10009. if (rc) {
  10010. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10011. goto exit;
  10012. }
  10013. break;
  10014. case PT_MODE_BOOTLOADER:
  10015. /* Do nothing as we are already in the BL */
  10016. tmp_result = PT_ENTER_BL_PASS;
  10017. goto exit;
  10018. default:
  10019. /* Should NEVER get here */
  10020. tmp_result = PT_ENTER_BL_ERROR;
  10021. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10022. goto exit;
  10023. }
  10024. if (!cd->flashless_dut &&
  10025. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10026. /*
  10027. * Sending the special "Host Mode" command will instruct the
  10028. * BL to not execute the FW it has loaded into RAM.
  10029. * The command must be sent within a 40ms window from releasing
  10030. * the XRES pin. If the messages is sent too early it will NAK,
  10031. * so keep sending it every 2ms until it is accepted by the BL.
  10032. * A no-flash DUT does not require this command as there is no
  10033. * FW for the BL to load and execute.
  10034. */
  10035. usleep_range(4000, 6000);
  10036. pt_debug(cd->dev, DL_INFO,
  10037. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10038. __func__, (int)sizeof(host_mode_cmd));
  10039. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10040. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10041. rc = 1;
  10042. while (rc && time < 34) {
  10043. rc = pt_adap_write_read_specific(cd, 4,
  10044. host_mode_cmd, NULL);
  10045. usleep_range(1800, 2000);
  10046. time += 2;
  10047. }
  10048. /* Sleep to allow the BL to come up */
  10049. usleep_range(1000, 2000);
  10050. }
  10051. /*
  10052. * To avoid the case that next PIP command can be confused by BL/FW
  10053. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10054. * followed by "wakeup event" function can lower the failure rate.
  10055. */
  10056. t = wait_event_timeout(cd->wait_q,
  10057. ((cd->startup_status != STARTUP_STATUS_START)
  10058. && (cd->hid_reset_cmd_state == 0)),
  10059. msecs_to_jiffies(300));
  10060. if (IS_TMO(t)) {
  10061. pt_debug(cd->dev, DL_ERROR,
  10062. "%s: TMO waiting for BL sentinel\n", __func__);
  10063. }
  10064. /* Check if device is now in BL mode */
  10065. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10066. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10067. cd->startup_status);
  10068. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10069. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10070. mutex_lock(&cd->system_lock);
  10071. cd->pip2_prot_active = true;
  10072. cd->mode = PT_MODE_BOOTLOADER;
  10073. mutex_unlock(&cd->system_lock);
  10074. tmp_result = PT_ENTER_BL_PASS;
  10075. } else {
  10076. /*
  10077. * If the device doesn't enter BL mode as expected and rc is
  10078. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10079. * the function should return an error code to indicate this
  10080. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10081. */
  10082. if (!rc)
  10083. rc = -EINVAL;
  10084. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10085. mutex_lock(&cd->system_lock);
  10086. cd->mode = mode;
  10087. mutex_unlock(&cd->system_lock);
  10088. pt_debug(dev, DL_ERROR,
  10089. "%s ERROR: Not in BL as expected", __func__);
  10090. }
  10091. exit:
  10092. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10093. /* Check to get (buffered) flash information */
  10094. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10095. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10096. read_buf, &actual_read_len);
  10097. if (!rc) {
  10098. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10099. pt_debug(
  10100. dev, DL_WARN,
  10101. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10102. __func__,
  10103. read_buf[PIP2_RESP_BODY_OFFSET]);
  10104. /*
  10105. * If the BL was unable to cache the correct
  10106. * values when entering the first time due to
  10107. * the Flash part not having been powered up
  10108. * long enough, re-enter the BL to trigger the
  10109. * BL to re-attempt to cache the values.
  10110. */
  10111. if (flash_info_retry-- > 0) {
  10112. mode = PT_MODE_UNKNOWN;
  10113. pt_debug(dev, DL_WARN,
  10114. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10115. __func__, flash_info_retry);
  10116. goto _retry;
  10117. } else {
  10118. pt_debug(dev, DL_WARN,
  10119. "%s Manufacturer ID Unknown\n",
  10120. __func__);
  10121. tmp_result = PT_ENTER_BL_PASS;
  10122. }
  10123. }
  10124. } else {
  10125. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10126. pt_debug(
  10127. dev, DL_ERROR,
  10128. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10129. __func__);
  10130. }
  10131. }
  10132. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10133. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10134. "SUPPRESS");
  10135. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10136. if (result)
  10137. *result = tmp_result;
  10138. return rc;
  10139. }
  10140. /*******************************************************************************
  10141. * FUNCTION: _pt_pip2_file_open
  10142. *
  10143. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10144. *
  10145. * NOTE: The DUT must be in BL mode for this command to work
  10146. *
  10147. * RETURNS:
  10148. * <0 = Error
  10149. * >0 = file handle opened
  10150. *
  10151. * PARAMETERS:
  10152. * *dev - pointer to device structure
  10153. * file_no - PIP2 file number to open
  10154. ******************************************************************************/
  10155. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10156. {
  10157. int ret = 0;
  10158. u16 status;
  10159. u16 actual_read_len;
  10160. u8 file_handle;
  10161. u8 data[2];
  10162. u8 read_buf[10];
  10163. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10164. data[0] = file_no;
  10165. ret = _pt_request_pip2_send_cmd(dev,
  10166. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10167. data, 1, read_buf, &actual_read_len);
  10168. if (ret) {
  10169. pt_debug(dev, DL_ERROR,
  10170. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10171. __func__, file_no);
  10172. return -PIP2_RSP_ERR_NOT_OPEN;
  10173. }
  10174. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10175. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10176. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10177. (file_handle != file_no)) {
  10178. pt_debug(dev, DL_ERROR,
  10179. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10180. __func__, status, file_handle);
  10181. return -status;
  10182. }
  10183. return file_handle;
  10184. }
  10185. /*******************************************************************************
  10186. * FUNCTION: _pt_pip2_file_close
  10187. *
  10188. * SUMMARY: Using the BL PIP2 commands close a file
  10189. *
  10190. * NOTE: The DUT must be in BL mode for this command to work
  10191. *
  10192. * RETURNS:
  10193. * <0 = Error
  10194. * >0 = file handle closed
  10195. *
  10196. * PARAMETERS:
  10197. * *dev - pointer to device structure
  10198. * file_handle - handle to the file to be closed
  10199. ******************************************************************************/
  10200. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10201. {
  10202. int ret = 0;
  10203. u16 status;
  10204. u16 actual_read_len;
  10205. u8 data[2];
  10206. u8 read_buf[32];
  10207. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10208. data[0] = file_handle;
  10209. ret = _pt_request_pip2_send_cmd(dev,
  10210. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10211. data, 1, read_buf, &actual_read_len);
  10212. if (ret) {
  10213. pt_debug(dev, DL_ERROR,
  10214. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10215. __func__, file_handle);
  10216. return -ETIME;
  10217. }
  10218. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10219. if (status != 0x00) {
  10220. pt_debug(dev, DL_ERROR,
  10221. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10222. __func__, status, file_handle);
  10223. return -status;
  10224. }
  10225. return file_handle;
  10226. }
  10227. /*******************************************************************************
  10228. * FUNCTION: _pt_pip2_file_erase
  10229. *
  10230. * SUMMARY: Using the BL PIP2 commands erase a file
  10231. *
  10232. * NOTE: The DUT must be in BL mode for this command to work
  10233. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10234. * one retry is attempted for each sector in a file.
  10235. *
  10236. * RETURNS:
  10237. * <0 = Error
  10238. * >0 = file handle closed
  10239. *
  10240. * PARAMETERS:
  10241. * *dev - pointer to device structure
  10242. * file_handle - handle to the file to be erased
  10243. * *status - PIP2 erase status code
  10244. ******************************************************************************/
  10245. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10246. {
  10247. int ret = 0;
  10248. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10249. int retry = 1;
  10250. u16 actual_read_len;
  10251. u8 data[2];
  10252. u8 read_buf[10];
  10253. struct pt_core_data *cd = dev_get_drvdata(dev);
  10254. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10255. data[0] = file_handle;
  10256. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10257. *status = PIP2_RSP_ERR_TIMEOUT;
  10258. /* Increase waiting time for large file erase */
  10259. mutex_lock(&cd->system_lock);
  10260. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10261. mutex_unlock(&cd->system_lock);
  10262. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10263. ret = _pt_request_pip2_send_cmd(dev,
  10264. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10265. data, 2, read_buf, &actual_read_len);
  10266. if (ret)
  10267. break;
  10268. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10269. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10270. #ifdef TTDL_DIAGNOSTICS
  10271. cd->file_erase_timeout_count++;
  10272. #endif
  10273. pt_debug(dev, DL_WARN,
  10274. "%s: ERASE timeout %d for file = %d\n",
  10275. __func__, retry, file_handle);
  10276. }
  10277. retry++;
  10278. }
  10279. mutex_lock(&cd->system_lock);
  10280. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10281. mutex_unlock(&cd->system_lock);
  10282. if (ret) {
  10283. pt_debug(dev, DL_ERROR,
  10284. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10285. __func__, ret, file_handle);
  10286. return -EIO;
  10287. }
  10288. if (*status != 0x00) {
  10289. pt_debug(dev, DL_ERROR,
  10290. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10291. __func__, *status, file_handle);
  10292. return -EIO;
  10293. }
  10294. return file_handle;
  10295. }
  10296. /*******************************************************************************
  10297. * FUNCTION: _pt_pip2_file_read
  10298. *
  10299. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10300. *
  10301. * NOTE: The DUT must be in BL mode for this command to work
  10302. *
  10303. * RETURNS:
  10304. * <0 = Error
  10305. * >0 = number of bytes read
  10306. *
  10307. * PARAMETERS:
  10308. * *dev - pointer to device structure
  10309. * file_handle - File handle to read from
  10310. * num_bytes - number of bytes to read
  10311. ******************************************************************************/
  10312. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10313. u8 *read_buf)
  10314. {
  10315. int ret = 0;
  10316. u16 status;
  10317. u16 actual_read_len;
  10318. u8 data[3];
  10319. data[0] = file_handle;
  10320. data[1] = (num_bytes & 0x00FF);
  10321. data[2] = (num_bytes & 0xFF00) >> 8;
  10322. ret = _pt_request_pip2_send_cmd(dev,
  10323. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10324. data, 3, read_buf, &actual_read_len);
  10325. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10326. if (ret || ((status != 0x00) && (status != 0x03))) {
  10327. pt_debug(dev, DL_ERROR,
  10328. "%s File open failure with error code = %d\n",
  10329. __func__, status);
  10330. return -EPERM;
  10331. }
  10332. ret = num_bytes;
  10333. return ret;
  10334. }
  10335. /*******************************************************************************
  10336. * FUNCTION: _pt_read_us_file
  10337. *
  10338. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10339. * then read the entire file.
  10340. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10341. *
  10342. * RETURN:
  10343. * 0 = success
  10344. * !0 = failure
  10345. *
  10346. * PARAMETERS:
  10347. * *file_path - pointer to the file path
  10348. * *buf - pointer to the buffer to store the file contents
  10349. * *size - pointer to the size of the file
  10350. ******************************************************************************/
  10351. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10352. {
  10353. struct file *filp = NULL;
  10354. struct inode *inode = NULL;
  10355. unsigned int file_len = 0;
  10356. unsigned int read_len = 0;
  10357. mm_segment_t oldfs;
  10358. int rc = 0;
  10359. if (file_path == NULL || buf == NULL) {
  10360. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10361. return -EINVAL;
  10362. }
  10363. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10364. oldfs = get_fs();
  10365. set_fs(KERNEL_DS);
  10366. filp = filp_open(file_path, O_RDONLY, 0400);
  10367. if (IS_ERR(filp)) {
  10368. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10369. file_path);
  10370. rc = -ENOENT;
  10371. goto err;
  10372. }
  10373. if (filp->f_op == NULL) {
  10374. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10375. __func__);
  10376. rc = -EINVAL;
  10377. goto exit;
  10378. }
  10379. inode = filp->f_path.dentry->d_inode;
  10380. if (inode == NULL) {
  10381. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10382. __func__);
  10383. rc = -EINVAL;
  10384. goto exit;
  10385. }
  10386. file_len = i_size_read(inode->i_mapping->host);
  10387. if (file_len == 0) {
  10388. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10389. __func__, file_len);
  10390. rc = -EINVAL;
  10391. goto exit;
  10392. }
  10393. if (*size == 0)
  10394. read_len = file_len;
  10395. else
  10396. read_len = *size;
  10397. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10398. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10399. __func__, read_len);
  10400. rc = -EINVAL;
  10401. goto exit;
  10402. }
  10403. filp->private_data = inode->i_private;
  10404. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10405. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10406. rc = -EINVAL;
  10407. goto exit;
  10408. }
  10409. *size = read_len;
  10410. exit:
  10411. if (filp_close(filp, NULL) != 0)
  10412. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10413. err:
  10414. set_fs(oldfs);
  10415. return rc;
  10416. }
  10417. /*******************************************************************************
  10418. * FUNCTION: _pt_request_pip2_bin_hdr
  10419. *
  10420. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10421. * in the case of a flashless DUT, and parse the contents
  10422. *
  10423. * RETURNS:
  10424. * 0 = Success
  10425. * !0 = Error condition
  10426. *
  10427. * PARAMETERS:
  10428. * *dev - pointer to device structure
  10429. ******************************************************************************/
  10430. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10431. {
  10432. struct pt_core_data *cd = dev_get_drvdata(dev);
  10433. u8 file_handle;
  10434. u8 read_buf[255];
  10435. u8 hdr_len = 0;
  10436. u8 i;
  10437. int bytes_read;
  10438. int read_size;
  10439. int ret = 0;
  10440. int rc = 0;
  10441. bool load_hdr_struct = false;
  10442. if (cd->flashless_dut) {
  10443. read_size = sizeof(read_buf);
  10444. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10445. read_buf, &read_size);
  10446. if (rc) {
  10447. ret = rc;
  10448. pt_debug(dev, DL_ERROR,
  10449. "%s Failed to read fw image from US, rc=%d\n",
  10450. __func__, rc);
  10451. goto exit;
  10452. }
  10453. load_hdr_struct = true;
  10454. hdr_len = read_buf[0];
  10455. i = 0;
  10456. } else {
  10457. if (cd->mode != PT_MODE_BOOTLOADER) {
  10458. ret = -EPERM;
  10459. goto exit;
  10460. }
  10461. /* Open the bin file in Flash */
  10462. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10463. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10464. if (file_handle != PIP2_FW_FILE) {
  10465. ret = -ENOENT;
  10466. pt_debug(dev, DL_ERROR,
  10467. "%s Failed to open bin file\n", __func__);
  10468. goto exit;
  10469. }
  10470. /* Read the header length from the file */
  10471. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10472. read_size = 1;
  10473. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10474. read_buf);
  10475. if (bytes_read != read_size) {
  10476. ret = -EX_ERR_FREAD;
  10477. pt_debug(dev, DL_ERROR,
  10478. "%s Failed to bin file header len\n", __func__);
  10479. goto exit_close_file;
  10480. }
  10481. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10482. if (hdr_len == 0xFF) {
  10483. ret = -EX_ERR_FLEN;
  10484. pt_debug(dev, DL_ERROR,
  10485. "%s Bin header len is invalid\n", __func__);
  10486. goto exit_close_file;
  10487. }
  10488. /* Read the rest of the header from the bin file */
  10489. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10490. memset(read_buf, 0, sizeof(read_buf));
  10491. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10492. read_buf);
  10493. if (bytes_read != hdr_len) {
  10494. ret = -EX_ERR_FREAD;
  10495. pt_debug(dev, DL_ERROR,
  10496. "%s Failed to read bin file\n", __func__);
  10497. goto exit_close_file;
  10498. }
  10499. load_hdr_struct = true;
  10500. exit_close_file:
  10501. /* Close the file */
  10502. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10503. ret = -EX_ERR_FCLOSE;
  10504. pt_debug(dev, DL_ERROR,
  10505. "%s Failed to close bin file\n", __func__);
  10506. }
  10507. /*
  10508. * The length was already read so subtract 1 to make the rest of
  10509. * the offsets match the spec
  10510. */
  10511. i = PIP2_RESP_BODY_OFFSET - 1;
  10512. }
  10513. if (load_hdr_struct) {
  10514. hdr->length = hdr_len;
  10515. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10516. hdr->fw_major = (read_buf[i+3]);
  10517. hdr->fw_minor = (read_buf[i+4]);
  10518. hdr->fw_crc = (read_buf[i+5] << 24) |
  10519. (read_buf[i+6] << 16) |
  10520. (read_buf[i+7] << 8) |
  10521. (read_buf[i+8]);
  10522. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10523. (read_buf[i+10] << 16) |
  10524. (read_buf[i+11] << 8) |
  10525. (read_buf[i+12]);
  10526. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10527. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10528. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10529. if (hdr_len >= 22) {
  10530. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10531. (read_buf[i+20] << 16) |
  10532. (read_buf[i+21] << 8) |
  10533. (read_buf[i+22]);
  10534. } else {
  10535. hdr->hex_file_size = 0;
  10536. }
  10537. }
  10538. exit:
  10539. return ret;
  10540. }
  10541. /*******************************************************************************
  10542. * FUNCTION: _pt_pip2_file_get_stats
  10543. *
  10544. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10545. * opened file
  10546. *
  10547. * NOTE: The DUT must be in BL mode for this command to work
  10548. *
  10549. * RETURNS:
  10550. * !0 = Error
  10551. * 0 = Success
  10552. *
  10553. * PARAMETERS:
  10554. * *dev - pointer to device structure
  10555. * file_handle - File handle to read from
  10556. * *address - pointer to store address of file
  10557. * *file_size _ pointer to store size of file
  10558. ******************************************************************************/
  10559. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10560. u32 *file_size)
  10561. {
  10562. int ret = 1;
  10563. u16 status;
  10564. u16 actual_read_len;
  10565. u8 data[2];
  10566. u8 read_buf[16];
  10567. data[0] = file_handle;
  10568. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10569. ret = _pt_request_pip2_send_cmd(dev,
  10570. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10571. data, 2, read_buf, &actual_read_len);
  10572. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10573. if (ret || (status != 0x00)) {
  10574. pt_debug(dev, DL_ERROR,
  10575. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10576. __func__, status, file_handle, ret);
  10577. ret = -EIO;
  10578. goto exit;
  10579. }
  10580. pt_debug(dev, DL_DEBUG,
  10581. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10582. if (address) {
  10583. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10584. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10585. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10586. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10587. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10588. __func__, *address);
  10589. }
  10590. if (file_size) {
  10591. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10592. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10593. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10594. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10595. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10596. __func__, *file_size);
  10597. }
  10598. exit:
  10599. return ret;
  10600. }
  10601. /*******************************************************************************
  10602. * FUNCTION: _pt_pip2_file_seek_offset
  10603. *
  10604. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10605. * opened file
  10606. *
  10607. * NOTE: The DUT must be in BL mode for this command to work
  10608. * NOTE: File open/erase command can reset the offset
  10609. *
  10610. * RETURNS:
  10611. * !0 = Error
  10612. * 0 = Success
  10613. *
  10614. * PARAMETERS:
  10615. * *dev - pointer to device structure
  10616. * file_handle - File handle to read from
  10617. * read_offset - read offset of file
  10618. * write_offset - write offset of file
  10619. ******************************************************************************/
  10620. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10621. u32 read_offset, u32 write_offset)
  10622. {
  10623. int ret = 1;
  10624. u16 status;
  10625. u16 actual_read_len;
  10626. u8 data[10];
  10627. u8 read_buf[16];
  10628. data[0] = file_handle;
  10629. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10630. data[2] = 0xff & read_offset;
  10631. data[3] = 0xff & (read_offset >> 8);
  10632. data[4] = 0xff & (read_offset >> 16);
  10633. data[5] = 0xff & (read_offset >> 24);
  10634. data[6] = 0xff & write_offset;
  10635. data[7] = 0xff & (write_offset >> 8);
  10636. data[8] = 0xff & (write_offset >> 16);
  10637. data[9] = 0xff & (write_offset >> 24);
  10638. ret = _pt_request_pip2_send_cmd(dev,
  10639. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10640. data, 10, read_buf, &actual_read_len);
  10641. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10642. if (ret || (status != 0x00)) {
  10643. pt_debug(dev, DL_ERROR,
  10644. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10645. __func__, status, ret, file_handle);
  10646. ret = -EIO;
  10647. }
  10648. return ret;
  10649. }
  10650. /*******************************************************************************
  10651. * FUNCTION: _pt_pip2_file_crc
  10652. *
  10653. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10654. * the file.
  10655. *
  10656. * NOTE: The DUT must be in BL mode for this command to work
  10657. * NOTE: This command only can be used for BL version 1.8 or greater.
  10658. * BL version 1.8 added this change according to PGV-173.
  10659. *
  10660. * RETURNS:
  10661. * !0 = Error
  10662. * 0 = Success
  10663. *
  10664. * PARAMETERS:
  10665. * *dev - pointer to device structure
  10666. * file_handle - File handle to read from
  10667. * offset - start offset for CRC calculation
  10668. * length - number of bytes to calculate CRC over
  10669. * read_buf - pointer to the read buffer
  10670. ******************************************************************************/
  10671. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10672. u32 offset, u32 length, u8 *read_buf)
  10673. {
  10674. int rc = 1;
  10675. u16 actual_read_len;
  10676. u8 data[10];
  10677. data[0] = file_handle;
  10678. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10679. data[2] = 0xff & offset;
  10680. data[3] = 0xff & (offset >> 8);
  10681. data[4] = 0xff & (offset >> 16);
  10682. data[5] = 0xff & (offset >> 24);
  10683. data[6] = 0xff & length;
  10684. data[7] = 0xff & (length >> 8);
  10685. data[8] = 0xff & (length >> 16);
  10686. data[9] = 0xff & (length >> 24);
  10687. rc = _pt_request_pip2_send_cmd(dev,
  10688. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10689. data, 10, read_buf, &actual_read_len);
  10690. if (rc)
  10691. pt_debug(dev, DL_ERROR,
  10692. "%s Return FILE_CRC failure, rc = %d\n",
  10693. __func__, rc);
  10694. return rc;
  10695. }
  10696. /*******************************************************************************
  10697. * FUNCTION: pt_pip2_ping_test
  10698. *
  10699. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10700. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10701. * response payload matches what was sent.
  10702. * The max payload size is 247:
  10703. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10704. *
  10705. * RETURN:
  10706. * 0 = success
  10707. * !0 = failure
  10708. *
  10709. * PARAMETERS:
  10710. * *dev - pointer to device structure
  10711. * *attr - pointer to device attributes
  10712. * *buf - pointer to output buffer
  10713. ******************************************************************************/
  10714. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10715. {
  10716. u16 actual_read_len;
  10717. u8 *read_buf = NULL;
  10718. u8 *data = NULL;
  10719. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10720. int i = 1;
  10721. int j = 0;
  10722. int rc = 0;
  10723. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10724. if (!read_buf)
  10725. goto ping_test_exit;
  10726. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10727. if (!data)
  10728. goto ping_test_exit;
  10729. /* Load data payload with an array of walking 1's */
  10730. for (i = 0; i < 255; i++)
  10731. data[i] = 0x01 << (i % 8);
  10732. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10733. for (i = 0; i <= max_bytes; i++) {
  10734. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10735. PIP2_CMD_ID_PING, data, i, read_buf,
  10736. &actual_read_len);
  10737. if (rc) {
  10738. pt_debug(dev, DL_ERROR,
  10739. "%s: PING failed with %d byte payload\n",
  10740. __func__, i);
  10741. break;
  10742. }
  10743. /* Verify data returned matches data sent */
  10744. for (j = 0; j < i; j++) {
  10745. if (read_buf[data_offset + j] != data[j]) {
  10746. pt_debug(dev, DL_DEBUG,
  10747. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10748. __func__, i, j, data[j], j,
  10749. read_buf[data_offset + j]);
  10750. goto ping_test_exit;
  10751. }
  10752. }
  10753. }
  10754. ping_test_exit:
  10755. *last_packet_size = i - 1;
  10756. kfree(read_buf);
  10757. kfree(data);
  10758. return rc;
  10759. }
  10760. /*******************************************************************************
  10761. * FUNCTION: _pt_ic_parse_input_hex
  10762. *
  10763. * SUMMARY: Parse a char data array as space delimited hex values into
  10764. * an int array.
  10765. *
  10766. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10767. * buffer to store data is "u32". This function is still needed by the
  10768. * "command" sysfs node because the buffer type to store data is "u8".
  10769. *
  10770. * RETURN: Length of parsed data
  10771. *
  10772. * PARAMETERS:
  10773. * *dev - pointer to device structure
  10774. * *buf - pointer to buffer that holds the input array to parse
  10775. * buf_size - size of buf
  10776. * *ic_buf - pointer to array to store parsed data
  10777. * ic_buf_size - max size of ic_buf
  10778. ******************************************************************************/
  10779. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10780. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10781. {
  10782. const char *pbuf = buf;
  10783. unsigned long value;
  10784. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10785. u32 i = 0;
  10786. u32 j;
  10787. int last = 0;
  10788. int ret;
  10789. pt_debug(dev, DL_DEBUG,
  10790. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10791. __func__, pbuf, buf, buf_size, "scan buf size",
  10792. PT_MAX_PIP2_MSG_SIZE, buf);
  10793. while (pbuf <= (buf + buf_size)) {
  10794. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10795. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10796. __func__, "Max cmd size exceeded", i,
  10797. PT_MAX_PIP2_MSG_SIZE);
  10798. return -EINVAL;
  10799. }
  10800. if (i >= ic_buf_size) {
  10801. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10802. __func__, "Buffer size exceeded", i,
  10803. ic_buf_size);
  10804. return -EINVAL;
  10805. }
  10806. while (((*pbuf == ' ') || (*pbuf == ','))
  10807. && (pbuf < (buf + buf_size))) {
  10808. last = *pbuf;
  10809. pbuf++;
  10810. }
  10811. if (pbuf >= (buf + buf_size))
  10812. break;
  10813. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10814. if ((last == ',') && (*pbuf == ',')) {
  10815. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10816. __func__, "Invalid data format.");
  10817. return -EINVAL;
  10818. }
  10819. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10820. && (pbuf < (buf + buf_size))
  10821. && (*pbuf != ' ')
  10822. && (*pbuf != ','); j++) {
  10823. last = *pbuf;
  10824. scan_buf[j] = *pbuf++;
  10825. }
  10826. ret = kstrtoul(scan_buf, 16, &value);
  10827. if (ret < 0) {
  10828. pt_debug(dev, DL_ERROR,
  10829. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10830. "Invalid data format. ", scan_buf,
  10831. "Use \"0xHH,...,0xHH\"", " instead.",
  10832. i, ret);
  10833. return ret;
  10834. }
  10835. ic_buf[i] = value;
  10836. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10837. __func__, i, value);
  10838. i++;
  10839. }
  10840. return i;
  10841. }
  10842. /*******************************************************************************
  10843. * FUNCTION: _pt_ic_parse_input
  10844. *
  10845. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10846. * hex values or dec values into an int array with the following rules:
  10847. * 1) Hex values must have a "0x" prefix for each element or the first element
  10848. * only
  10849. * 2) Dec values do not have any prefix
  10850. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10851. * string
  10852. *
  10853. * RETURN: Number of values parsed
  10854. *
  10855. * PARAMETERS:
  10856. * *dev - pointer to device structure
  10857. * *buf - pointer to buffer that holds the input array to parse
  10858. * buf_size - size of buf
  10859. * *out_buf - pointer to array to store parsed data
  10860. * out_buf_size - max size of buffer to be stored
  10861. ******************************************************************************/
  10862. static int _pt_ic_parse_input(struct device *dev,
  10863. const char *buf, size_t buf_size,
  10864. u32 *out_buf, size_t out_buf_size)
  10865. {
  10866. const char *pbuf = buf;
  10867. unsigned long value;
  10868. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10869. u32 i = 0;
  10870. u32 j;
  10871. int last = 0;
  10872. int ret = 0;
  10873. u8 str_base = 0;
  10874. pt_debug(dev, DL_DEBUG,
  10875. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10876. __func__, buf_size, out_buf_size, "scan buf size",
  10877. PT_MAX_PIP2_MSG_SIZE, buf);
  10878. while (pbuf <= (buf + buf_size)) {
  10879. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10880. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10881. __func__, "Max cmd size exceeded", i,
  10882. PT_MAX_PIP2_MSG_SIZE);
  10883. ret = -EINVAL;
  10884. goto error;
  10885. }
  10886. if (i >= out_buf_size) {
  10887. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10888. __func__, "Buffer size exceeded", i,
  10889. out_buf_size);
  10890. ret = -EINVAL;
  10891. goto error;
  10892. }
  10893. while (((*pbuf == ' ') || (*pbuf == ','))
  10894. && (pbuf < (buf + buf_size))) {
  10895. last = *pbuf;
  10896. pbuf++;
  10897. }
  10898. if (pbuf >= (buf + buf_size))
  10899. break;
  10900. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10901. if ((last == ',') && (*pbuf == ',')) {
  10902. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10903. __func__, "Invalid data format.");
  10904. ret = -EINVAL;
  10905. goto error;
  10906. }
  10907. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10908. && (pbuf < (buf + buf_size))
  10909. && (*pbuf != ' ')
  10910. && (*pbuf != ','); j++) {
  10911. last = *pbuf;
  10912. scan_buf[j] = *pbuf++;
  10913. }
  10914. if (i == 0) {
  10915. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10916. (strncmp(scan_buf, "0X", 2) == 0))
  10917. str_base = 16;
  10918. else
  10919. str_base = 10;
  10920. } else {
  10921. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10922. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10923. (str_base == 10)) {
  10924. pt_debug(dev, DL_ERROR,
  10925. "%s: Decimal and Heximal data mixed\n",
  10926. __func__);
  10927. ret = -EINVAL;
  10928. goto error;
  10929. }
  10930. }
  10931. ret = kstrtoul(scan_buf, str_base, &value);
  10932. if (ret < 0) {
  10933. pt_debug(dev, DL_ERROR,
  10934. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10935. "Invalid data format. ", scan_buf,
  10936. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10937. " instead.", i, ret);
  10938. goto error;
  10939. }
  10940. out_buf[i] = value;
  10941. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10942. __func__, i, value, value);
  10943. i++;
  10944. }
  10945. ret = i;
  10946. error:
  10947. return ret;
  10948. }
  10949. #ifdef TTHE_TUNER_SUPPORT
  10950. /*******************************************************************************
  10951. * FUNCTION: tthe_debugfs_open
  10952. *
  10953. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10954. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10955. * printing a large number of fingers and sensor data without overflowing
  10956. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10957. * even multiple of PAGE_SIZE
  10958. *
  10959. * RETURN:
  10960. * 0 = success
  10961. * !0 = failure
  10962. *
  10963. * PARAMETERS:
  10964. * *inode - file inode number
  10965. * *filp - file pointer to debugfs file
  10966. ******************************************************************************/
  10967. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10968. {
  10969. struct pt_core_data *cd = inode->i_private;
  10970. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10971. filp->private_data = inode->i_private;
  10972. if (cd->tthe_buf)
  10973. return -EBUSY;
  10974. while (buf_size < 4096)
  10975. buf_size = buf_size << 1;
  10976. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10977. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10978. cd->tthe_buf_size = buf_size;
  10979. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10980. if (!cd->tthe_buf)
  10981. return -ENOMEM;
  10982. return 0;
  10983. }
  10984. /*******************************************************************************
  10985. * FUNCTION: tthe_debugfs_close
  10986. *
  10987. * SUMMARY: Close method for tthe_tuner debugfs node.
  10988. *
  10989. * RETURN:
  10990. * 0 = success
  10991. * !0 = failure
  10992. *
  10993. * PARAMETERS:
  10994. * *inode - file inode number
  10995. * *filp - file pointer to debugfs file
  10996. ******************************************************************************/
  10997. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10998. {
  10999. struct pt_core_data *cd = filp->private_data;
  11000. filp->private_data = NULL;
  11001. kfree(cd->tthe_buf);
  11002. cd->tthe_buf = NULL;
  11003. return 0;
  11004. }
  11005. /*******************************************************************************
  11006. * FUNCTION: tthe_debugfs_read
  11007. *
  11008. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11009. * tthe_buf to user buffer.
  11010. *
  11011. * RETURN: Size of debugfs data print
  11012. *
  11013. * PARAMETERS:
  11014. * *filp - file pointer to debugfs file
  11015. * *buf - the user space buffer to read to
  11016. * count - the maximum number of bytes to read
  11017. * *ppos - the current position in the buffer
  11018. ******************************************************************************/
  11019. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11020. size_t count, loff_t *ppos)
  11021. {
  11022. struct pt_core_data *cd = filp->private_data;
  11023. int size;
  11024. int ret;
  11025. static int partial_read;
  11026. wait_event_interruptible(cd->wait_q,
  11027. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11028. mutex_lock(&cd->tthe_lock);
  11029. if (cd->tthe_exit) {
  11030. mutex_unlock(&cd->tthe_lock);
  11031. return 0;
  11032. }
  11033. if (count > cd->tthe_buf_len)
  11034. size = cd->tthe_buf_len;
  11035. else
  11036. size = count;
  11037. if (!size) {
  11038. mutex_unlock(&cd->tthe_lock);
  11039. return 0;
  11040. }
  11041. if (partial_read) {
  11042. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11043. partial_read = 0;
  11044. } else {
  11045. ret = copy_to_user(buf, cd->tthe_buf, size);
  11046. }
  11047. if (size == count)
  11048. partial_read = count;
  11049. if (ret == size)
  11050. return -EFAULT;
  11051. size -= ret;
  11052. cd->tthe_buf_len -= size;
  11053. mutex_unlock(&cd->tthe_lock);
  11054. *ppos += size;
  11055. return size;
  11056. }
  11057. static const struct file_operations tthe_debugfs_fops = {
  11058. .open = tthe_debugfs_open,
  11059. .release = tthe_debugfs_close,
  11060. .read = tthe_debugfs_read,
  11061. };
  11062. #endif
  11063. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11064. .start_bl = _pt_request_pip_start_bl,
  11065. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11066. .resume_scanning = _pt_request_pip_resume_scanning,
  11067. .get_param = _pt_request_pip_get_param,
  11068. .set_param = _pt_request_pip_set_param,
  11069. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11070. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11071. .get_data_structure = _pt_request_pip_get_data_structure,
  11072. .run_selftest = _pt_request_pip_run_selftest,
  11073. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11074. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11075. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11076. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11077. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11078. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11079. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11080. .read_data_block = _pt_request_pip_read_data_block,
  11081. .write_data_block = _pt_request_pip_write_data_block,
  11082. .user_cmd = _pt_request_pip_user_cmd,
  11083. .get_bl_info = _pt_request_pip_bl_get_information,
  11084. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11085. .launch_app = _pt_request_pip_launch_app,
  11086. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11087. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11088. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11089. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11090. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11091. .pip2_file_open = _pt_pip2_file_open,
  11092. .pip2_file_close = _pt_pip2_file_close,
  11093. .pip2_file_erase = _pt_pip2_file_erase,
  11094. .read_us_file = _pt_read_us_file,
  11095. .manage_cal_data = _pt_manage_local_cal_data,
  11096. .calc_crc = crc_ccitt_calculate,
  11097. #ifdef TTDL_DIAGNOSTICS
  11098. .pip2_file_read = _pt_pip2_file_read,
  11099. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11100. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11101. .pip2_file_crc = _pt_pip2_file_crc,
  11102. #endif
  11103. };
  11104. static struct pt_core_commands _pt_core_commands = {
  11105. .subscribe_attention = _pt_subscribe_attention,
  11106. .unsubscribe_attention = _pt_unsubscribe_attention,
  11107. .request_exclusive = _pt_request_exclusive,
  11108. .release_exclusive = _pt_release_exclusive,
  11109. .request_reset = _pt_request_reset,
  11110. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11111. .request_enum = _pt_request_enum,
  11112. .request_sysinfo = _pt_request_sysinfo,
  11113. .request_loader_pdata = _pt_request_loader_pdata,
  11114. .request_stop_wd = _pt_request_stop_wd,
  11115. .request_start_wd = _pt_request_start_wd,
  11116. .request_get_mode = _pt_request_get_mode,
  11117. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11118. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11119. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11120. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11121. .request_dut_generation = _pt_request_dut_generation,
  11122. .request_hw_version = _pt_request_hw_version,
  11123. .parse_sysfs_input = _pt_ic_parse_input,
  11124. #ifdef TTHE_TUNER_SUPPORT
  11125. .request_tthe_print = _pt_request_tthe_print,
  11126. #endif
  11127. #ifdef TTDL_DIAGNOSTICS
  11128. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11129. #endif
  11130. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11131. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11132. };
  11133. struct pt_core_commands *pt_get_commands(void)
  11134. {
  11135. return &_pt_core_commands;
  11136. }
  11137. EXPORT_SYMBOL_GPL(pt_get_commands);
  11138. static DEFINE_MUTEX(core_list_lock);
  11139. static LIST_HEAD(core_list);
  11140. static DEFINE_MUTEX(module_list_lock);
  11141. static LIST_HEAD(module_list);
  11142. static int core_number;
  11143. /*******************************************************************************
  11144. * FUNCTION: pt_probe_module
  11145. *
  11146. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11147. *
  11148. * RETURN:
  11149. * 0 = success
  11150. * !0 = failure
  11151. *
  11152. * PARAMETERS:
  11153. * *cd - pointer to core data
  11154. * *module - pointer to module structure
  11155. ******************************************************************************/
  11156. static int pt_probe_module(struct pt_core_data *cd,
  11157. struct pt_module *module)
  11158. {
  11159. struct module_node *module_node;
  11160. int rc = 0;
  11161. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11162. if (!module_node)
  11163. return -ENOMEM;
  11164. module_node->module = module;
  11165. mutex_lock(&cd->module_list_lock);
  11166. list_add(&module_node->node, &cd->module_list);
  11167. mutex_unlock(&cd->module_list_lock);
  11168. rc = module->probe(cd->dev, &module_node->data);
  11169. if (rc) {
  11170. /*
  11171. * Remove from the list when probe fails
  11172. * in order not to call release
  11173. */
  11174. mutex_lock(&cd->module_list_lock);
  11175. list_del(&module_node->node);
  11176. mutex_unlock(&cd->module_list_lock);
  11177. kfree(module_node);
  11178. goto exit;
  11179. }
  11180. exit:
  11181. return rc;
  11182. }
  11183. /*******************************************************************************
  11184. * FUNCTION: pt_release_module
  11185. *
  11186. * SUMMARY: Call the release pointer and remove the module pointer from
  11187. * module_list.
  11188. *
  11189. * PARAMETERS:
  11190. * *cd - pointer to core data
  11191. * *module - pointer to module structure
  11192. ******************************************************************************/
  11193. static void pt_release_module(struct pt_core_data *cd,
  11194. struct pt_module *module)
  11195. {
  11196. struct module_node *m, *m_n;
  11197. mutex_lock(&cd->module_list_lock);
  11198. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11199. if (m->module == module) {
  11200. module->release(cd->dev, m->data);
  11201. list_del(&m->node);
  11202. kfree(m);
  11203. break;
  11204. }
  11205. mutex_unlock(&cd->module_list_lock);
  11206. }
  11207. /*******************************************************************************
  11208. * FUNCTION: pt_probe_modules
  11209. *
  11210. * SUMMARY: Iterate module_list and probe each module.
  11211. *
  11212. * PARAMETERS:
  11213. * *cd - pointer to core data
  11214. ******************************************************************************/
  11215. static void pt_probe_modules(struct pt_core_data *cd)
  11216. {
  11217. struct pt_module *m;
  11218. int rc = 0;
  11219. mutex_lock(&module_list_lock);
  11220. list_for_each_entry(m, &module_list, node) {
  11221. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11222. __func__, m->name);
  11223. rc = pt_probe_module(cd, m);
  11224. if (rc)
  11225. pt_debug(cd->dev, DL_ERROR,
  11226. "%s: Probe fails for module %s\n",
  11227. __func__, m->name);
  11228. }
  11229. mutex_unlock(&module_list_lock);
  11230. }
  11231. /*******************************************************************************
  11232. * FUNCTION: pt_release_modules
  11233. *
  11234. * SUMMARY: Iterate module_list and remove each module.
  11235. *
  11236. * PARAMETERS:
  11237. * *cd - pointer to core data
  11238. ******************************************************************************/
  11239. static void pt_release_modules(struct pt_core_data *cd)
  11240. {
  11241. struct pt_module *m;
  11242. mutex_lock(&module_list_lock);
  11243. list_for_each_entry(m, &module_list, node)
  11244. pt_release_module(cd, m);
  11245. mutex_unlock(&module_list_lock);
  11246. }
  11247. /*******************************************************************************
  11248. * FUNCTION: pt_get_core_data
  11249. *
  11250. * SUMMARY: Iterate core_list and get core data.
  11251. *
  11252. * RETURN:
  11253. * pointer to core data or null pointer if fail
  11254. *
  11255. * PARAMETERS:
  11256. * *id - pointer to core id
  11257. ******************************************************************************/
  11258. struct pt_core_data *pt_get_core_data(char *id)
  11259. {
  11260. struct pt_core_data *d;
  11261. list_for_each_entry(d, &core_list, node)
  11262. if (!strncmp(d->core_id, id, 20))
  11263. return d;
  11264. return NULL;
  11265. }
  11266. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11267. /*******************************************************************************
  11268. * FUNCTION: pt_add_core
  11269. *
  11270. * SUMMARY: Add core data to the core_list.
  11271. *
  11272. * PARAMETERS:
  11273. * *dev - pointer to device structure
  11274. ******************************************************************************/
  11275. static void pt_add_core(struct device *dev)
  11276. {
  11277. struct pt_core_data *d;
  11278. struct pt_core_data *cd = dev_get_drvdata(dev);
  11279. mutex_lock(&core_list_lock);
  11280. list_for_each_entry(d, &core_list, node)
  11281. if (d->dev == dev)
  11282. goto unlock;
  11283. list_add(&cd->node, &core_list);
  11284. unlock:
  11285. mutex_unlock(&core_list_lock);
  11286. }
  11287. /*******************************************************************************
  11288. * FUNCTION: pt_del_core
  11289. *
  11290. * SUMMARY: Remove core data from the core_list.
  11291. *
  11292. * PARAMETERS:
  11293. * *dev - pointer to device structure
  11294. ******************************************************************************/
  11295. static void pt_del_core(struct device *dev)
  11296. {
  11297. struct pt_core_data *d, *d_n;
  11298. mutex_lock(&core_list_lock);
  11299. list_for_each_entry_safe(d, d_n, &core_list, node)
  11300. if (d->dev == dev) {
  11301. list_del(&d->node);
  11302. goto unlock;
  11303. }
  11304. unlock:
  11305. mutex_unlock(&core_list_lock);
  11306. }
  11307. /*******************************************************************************
  11308. * FUNCTION: pt_register_module
  11309. *
  11310. * SUMMARY: Register the module to module_list and probe the module for each
  11311. * core.
  11312. *
  11313. * RETURN:
  11314. * 0 = success
  11315. * !0 = failure
  11316. *
  11317. * PARAMETERS:
  11318. * *module - pointer to module structure
  11319. ******************************************************************************/
  11320. int pt_register_module(struct pt_module *module)
  11321. {
  11322. struct pt_module *m;
  11323. struct pt_core_data *cd;
  11324. int rc = 0;
  11325. if (!module || !module->probe || !module->release)
  11326. return -EINVAL;
  11327. mutex_lock(&module_list_lock);
  11328. list_for_each_entry(m, &module_list, node)
  11329. if (m == module) {
  11330. rc = -EEXIST;
  11331. goto unlock;
  11332. }
  11333. list_add(&module->node, &module_list);
  11334. /* Probe the module for each core */
  11335. mutex_lock(&core_list_lock);
  11336. list_for_each_entry(cd, &core_list, node)
  11337. pt_probe_module(cd, module);
  11338. mutex_unlock(&core_list_lock);
  11339. unlock:
  11340. mutex_unlock(&module_list_lock);
  11341. return rc;
  11342. }
  11343. EXPORT_SYMBOL_GPL(pt_register_module);
  11344. /*******************************************************************************
  11345. * FUNCTION: pt_unregister_module
  11346. *
  11347. * SUMMARY: Release the module for each core and remove the module from
  11348. * module_list.
  11349. *
  11350. * RETURN:
  11351. * 0 = success
  11352. * !0 = failure
  11353. *
  11354. * PARAMETERS:
  11355. * *module - pointer to module structure
  11356. ******************************************************************************/
  11357. void pt_unregister_module(struct pt_module *module)
  11358. {
  11359. struct pt_module *m, *m_n;
  11360. struct pt_core_data *cd;
  11361. if (!module)
  11362. return;
  11363. mutex_lock(&module_list_lock);
  11364. /* Release the module for each core */
  11365. mutex_lock(&core_list_lock);
  11366. list_for_each_entry(cd, &core_list, node)
  11367. pt_release_module(cd, module);
  11368. mutex_unlock(&core_list_lock);
  11369. list_for_each_entry_safe(m, m_n, &module_list, node)
  11370. if (m == module) {
  11371. list_del(&m->node);
  11372. break;
  11373. }
  11374. mutex_unlock(&module_list_lock);
  11375. }
  11376. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11377. /*******************************************************************************
  11378. * FUNCTION: pt_get_module_data
  11379. *
  11380. * SUMMARY: Get module data from module_node by module_list.
  11381. *
  11382. * RETURN:
  11383. * pointer to module data
  11384. *
  11385. * PARAMETERS:
  11386. * *dev - pointer to device structure
  11387. * *module - pointer to module structure
  11388. ******************************************************************************/
  11389. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11390. {
  11391. struct pt_core_data *cd = dev_get_drvdata(dev);
  11392. struct module_node *m;
  11393. void *data = NULL;
  11394. mutex_lock(&cd->module_list_lock);
  11395. list_for_each_entry(m, &cd->module_list, node)
  11396. if (m->module == module) {
  11397. data = m->data;
  11398. break;
  11399. }
  11400. mutex_unlock(&cd->module_list_lock);
  11401. return data;
  11402. }
  11403. EXPORT_SYMBOL(pt_get_module_data);
  11404. #ifdef CONFIG_HAS_EARLYSUSPEND
  11405. /*******************************************************************************
  11406. * FUNCTION: pt_early_suspend
  11407. *
  11408. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11409. * attention list.
  11410. *
  11411. * PARAMETERS:
  11412. * *h - pointer to early_suspend structure
  11413. ******************************************************************************/
  11414. static void pt_early_suspend(struct early_suspend *h)
  11415. {
  11416. struct pt_core_data *cd =
  11417. container_of(h, struct pt_core_data, es);
  11418. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11419. }
  11420. /*******************************************************************************
  11421. * FUNCTION: pt_late_resume
  11422. *
  11423. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11424. * attention list.
  11425. *
  11426. * PARAMETERS:
  11427. * *h - pointer to early_suspend structure
  11428. ******************************************************************************/
  11429. static void pt_late_resume(struct early_suspend *h)
  11430. {
  11431. struct pt_core_data *cd =
  11432. container_of(h, struct pt_core_data, es);
  11433. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11434. }
  11435. /*******************************************************************************
  11436. * FUNCTION: pt_setup_early_suspend
  11437. *
  11438. * SUMMARY: Register early/suspend function to the system.
  11439. *
  11440. * PARAMETERS:
  11441. * *cd - pointer to core data
  11442. ******************************************************************************/
  11443. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11444. {
  11445. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11446. cd->es.suspend = pt_early_suspend;
  11447. cd->es.resume = pt_late_resume;
  11448. register_early_suspend(&cd->es);
  11449. }
  11450. #elif defined(CONFIG_DRM)
  11451. static void pt_resume_work(struct work_struct *work)
  11452. {
  11453. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11454. resume_work);
  11455. int rc = 0;
  11456. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11457. return;
  11458. rc = pt_core_easywake_off(pt_data);
  11459. if (rc < 0) {
  11460. pt_debug(pt_data->dev, DL_ERROR,
  11461. "%s: Error on wake\n", __func__);
  11462. }
  11463. return;
  11464. }
  11465. static void pt_suspend_work(struct work_struct *work)
  11466. {
  11467. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11468. suspend_work);
  11469. int rc = 0;
  11470. pt_debug(pt_data->dev, DL_INFO, "%s Start\n", __func__);
  11471. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11472. return;
  11473. rc = pt_core_easywake_on(pt_data);
  11474. if (rc < 0) {
  11475. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11476. return;
  11477. }
  11478. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  11479. return;
  11480. }
  11481. /*******************************************************************************
  11482. * FUNCTION: drm_notifier_callback
  11483. *
  11484. * SUMMARY: Call back function for DRM notifier to allow to call
  11485. * resume/suspend attention list.
  11486. *
  11487. * RETURN:
  11488. * 0 = success
  11489. *
  11490. * PARAMETERS:
  11491. * *self - pointer to notifier_block structure
  11492. * event - event type of fb notifier
  11493. * *data - pointer to fb_event structure
  11494. ******************************************************************************/
  11495. static int drm_notifier_callback(struct notifier_block *self,
  11496. unsigned long event, void *data)
  11497. {
  11498. struct pt_core_data *cd =
  11499. container_of(self, struct pt_core_data, fb_notifier);
  11500. struct drm_panel_notifier *evdata = data;
  11501. int *blank;
  11502. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11503. if (!evdata)
  11504. goto exit;
  11505. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11506. event == DRM_PANEL_EVENT_BLANK)) {
  11507. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11508. __func__, event);
  11509. goto exit;
  11510. }
  11511. blank = evdata->data;
  11512. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11513. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11514. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11515. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11516. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11517. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11518. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11519. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11520. __func__, event);
  11521. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11522. if (cd->fb_state != FB_ON) {
  11523. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11524. __func__);
  11525. queue_work(cd->pt_workqueue, &cd->resume_work);
  11526. #if defined(CONFIG_PM_SLEEP)
  11527. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11528. __func__);
  11529. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11530. pt_core_resume_(cd->dev);
  11531. #endif
  11532. cd->fb_state = FB_ON;
  11533. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11534. }
  11535. }
  11536. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11537. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11538. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11539. if (cd->fb_state != FB_OFF) {
  11540. #if defined(CONFIG_PM_SLEEP)
  11541. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11542. __func__);
  11543. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11544. pt_core_suspend_(cd->dev);
  11545. #endif
  11546. cancel_work_sync(&cd->resume_work);
  11547. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11548. cd->fb_state = FB_OFF;
  11549. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11550. }
  11551. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11552. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11553. __func__, event);
  11554. }
  11555. } else {
  11556. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11557. __func__, *blank);
  11558. }
  11559. exit:
  11560. return 0;
  11561. }
  11562. /*******************************************************************************
  11563. * FUNCTION: pt_setup_drm_notifier
  11564. *
  11565. * SUMMARY: Set up call back function into drm notifier.
  11566. *
  11567. * PARAMETERS:
  11568. * *cd - pointer to core data
  11569. ******************************************************************************/
  11570. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11571. {
  11572. cd->fb_state = FB_NONE;
  11573. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11574. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11575. if (!active_panel)
  11576. pt_debug(cd->dev, DL_ERROR,
  11577. "%s: Active panel not registered!\n", __func__);
  11578. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11579. if (!cd->pt_workqueue) {
  11580. pt_debug(cd->dev, DL_ERROR,
  11581. "%s: worker thread creation failed !\n", __func__);
  11582. }
  11583. if (cd->pt_workqueue) {
  11584. INIT_WORK(&cd->resume_work, pt_resume_work);
  11585. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11586. }
  11587. if (active_panel &&
  11588. drm_panel_notifier_register(active_panel,
  11589. &cd->fb_notifier) < 0)
  11590. pt_debug(cd->dev, DL_ERROR,
  11591. "%s: Register notifier failed!\n", __func__);
  11592. }
  11593. #elif defined(CONFIG_FB)
  11594. /*******************************************************************************
  11595. * FUNCTION: fb_notifier_callback
  11596. *
  11597. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11598. * resume/suspend attention list.
  11599. *
  11600. * RETURN:
  11601. * 0 = success
  11602. *
  11603. * PARAMETERS:
  11604. * *self - pointer to notifier_block structure
  11605. * event - event type of fb notifier
  11606. * *data - pointer to fb_event structure
  11607. ******************************************************************************/
  11608. static int fb_notifier_callback(struct notifier_block *self,
  11609. unsigned long event, void *data)
  11610. {
  11611. struct pt_core_data *cd =
  11612. container_of(self, struct pt_core_data, fb_notifier);
  11613. struct fb_event *evdata = data;
  11614. int *blank;
  11615. if (event != FB_EVENT_BLANK || !evdata)
  11616. goto exit;
  11617. blank = evdata->data;
  11618. if (*blank == FB_BLANK_UNBLANK) {
  11619. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11620. if (cd->fb_state != FB_ON) {
  11621. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11622. #if defined(CONFIG_PM_SLEEP)
  11623. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11624. pt_core_resume_(cd->dev);
  11625. #endif
  11626. cd->fb_state = FB_ON;
  11627. }
  11628. } else if (*blank == FB_BLANK_POWERDOWN) {
  11629. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11630. if (cd->fb_state != FB_OFF) {
  11631. #if defined(CONFIG_PM_SLEEP)
  11632. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11633. pt_core_suspend_(cd->dev);
  11634. #endif
  11635. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11636. cd->fb_state = FB_OFF;
  11637. }
  11638. }
  11639. exit:
  11640. return 0;
  11641. }
  11642. /*******************************************************************************
  11643. * FUNCTION: pt_setup_fb_notifier
  11644. *
  11645. * SUMMARY: Set up call back function into fb notifier.
  11646. *
  11647. * PARAMETERS:
  11648. * *cd - pointer to core data
  11649. ******************************************************************************/
  11650. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11651. {
  11652. int rc = 0;
  11653. cd->fb_state = FB_ON;
  11654. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11655. rc = fb_register_client(&cd->fb_notifier);
  11656. if (rc)
  11657. pt_debug(cd->dev, DL_ERROR,
  11658. "Unable to register fb_notifier: %d\n", rc);
  11659. }
  11660. #endif
  11661. /*******************************************************************************
  11662. * FUNCTION: pt_watchdog_work
  11663. *
  11664. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11665. * sleeping then this function simply returns. If the DUT is awake the
  11666. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11667. * somehow a response is waiting on the DUT that has not been read. If
  11668. * this occurs the message is simply consumed. If or once the IRQ is
  11669. * cleared, a PIP PING message is sent to the DUT and if the response
  11670. * is received the watchdog succeeds and exits, if no response is seen
  11671. * a startup is queued unless the maximum number of startups have already
  11672. * been attempted, in that case a BL is attempted.
  11673. *
  11674. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11675. * work thread
  11676. *
  11677. * RETURN: void
  11678. *
  11679. * PARAMETERS:
  11680. * *work - pointer to a work structure for the watchdog work queue
  11681. ******************************************************************************/
  11682. static void pt_watchdog_work(struct work_struct *work)
  11683. {
  11684. int rc = 0;
  11685. struct pt_core_data *cd = container_of(work,
  11686. struct pt_core_data, watchdog_work);
  11687. /*
  11688. * if found the current sleep_state is SS_SLEEPING
  11689. * then no need to request_exclusive, directly return
  11690. */
  11691. if (cd->sleep_state == SS_SLEEPING)
  11692. return;
  11693. #ifdef TTDL_DIAGNOSTICS
  11694. cd->watchdog_count++;
  11695. #endif /* TTDL_DIAGNOSTICS */
  11696. /*
  11697. * The first WD interval was extended to allow DDI to come up.
  11698. * If the WD interval is not the default then adjust timer to the
  11699. * current setting. The user can override value with drv_debug sysfs.
  11700. */
  11701. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11702. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11703. msecs_to_jiffies(cd->watchdog_interval));
  11704. }
  11705. if (pt_check_irq_asserted(cd)) {
  11706. #ifdef TTDL_DIAGNOSTICS
  11707. cd->watchdog_irq_stuck_count++;
  11708. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11709. #endif /* TTDL_DIAGNOSTICS */
  11710. pt_debug(cd->dev, DL_ERROR,
  11711. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11712. __func__);
  11713. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11714. }
  11715. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11716. if (rc < 0) {
  11717. pt_debug(cd->dev, DL_ERROR,
  11718. "%s: fail get exclusive ex=%p own=%p\n",
  11719. __func__, cd->exclusive_dev, cd->dev);
  11720. goto queue_startup;
  11721. }
  11722. rc = pt_pip_null_(cd);
  11723. if (release_exclusive(cd, cd->dev) < 0)
  11724. pt_debug(cd->dev, DL_ERROR,
  11725. "%s: fail to release exclusive\n", __func__);
  11726. queue_startup:
  11727. if (rc) {
  11728. #ifdef TTDL_DIAGNOSTICS
  11729. cd->watchdog_failed_access_count++;
  11730. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11731. #endif /* TTDL_DIAGNOSTICS */
  11732. pt_debug(cd->dev, DL_ERROR,
  11733. "%s: failed to access device in WD, retry count=%d\n",
  11734. __func__, cd->startup_retry_count);
  11735. /* Already tried FW upgrade because of watchdog but failed */
  11736. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11737. return;
  11738. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11739. /*
  11740. * Any wrapper function that trys to disable the
  11741. * WD killing this worker cannot be called here.
  11742. */
  11743. rc = request_exclusive(cd, cd->dev,
  11744. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11745. if (rc < 0) {
  11746. pt_debug(cd->dev, DL_ERROR,
  11747. "%s: fail get exclusive ex=%p own=%p\n",
  11748. __func__, cd->exclusive_dev, cd->dev);
  11749. goto exit;
  11750. }
  11751. cd->hw_detected = false;
  11752. cd->startup_status = STARTUP_STATUS_START;
  11753. pt_debug(cd->dev, DL_DEBUG,
  11754. "%s: Startup Status Reset\n", __func__);
  11755. rc = pt_dut_reset_and_wait(cd);
  11756. if (release_exclusive(cd, cd->dev) < 0)
  11757. pt_debug(cd->dev, DL_ERROR,
  11758. "%s: fail to release exclusive\n",
  11759. __func__);
  11760. if (!rc) {
  11761. cd->hw_detected = true;
  11762. if (!cd->flashless_dut)
  11763. pt_queue_enum(cd);
  11764. }
  11765. #ifdef TTDL_DIAGNOSTICS
  11766. cd->wd_xres_count++;
  11767. pt_debug(cd->dev, DL_ERROR,
  11768. "%s: Comm Failed - DUT reset [#%d]\n",
  11769. __func__, cd->wd_xres_count);
  11770. #endif /* TTDL_DIAGNOSTICS */
  11771. } else {
  11772. /*
  11773. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11774. * reset the part to regain communications, try to BL
  11775. */
  11776. pt_debug(cd->dev, DL_ERROR,
  11777. "%s: WD DUT access failure, Start FW Upgrade\n",
  11778. __func__);
  11779. #ifdef TTDL_DIAGNOSTICS
  11780. /*
  11781. * When diagnostics is enabled allow TTDL to keep
  11782. * trying to find the DUT. This allows the DUT to be
  11783. * hot swap-able while the host stays running. In
  11784. * production this may not be wanted as a customer
  11785. * may have several touch drivers and any driver
  11786. * that doesn't match the current DUT should give
  11787. * up trying and give up using the bus.
  11788. */
  11789. pt_debug(cd->dev, DL_INFO,
  11790. "%s: Resetting startup_retry_count\n",
  11791. __func__);
  11792. cd->startup_retry_count = 0;
  11793. #endif /* TTDL_DIAGNOSTICS */
  11794. /*
  11795. * Since fw may be broken,reset sysinfo ready flag
  11796. * to let upgrade function work.
  11797. */
  11798. mutex_lock(&cd->system_lock);
  11799. cd->sysinfo.ready = false;
  11800. mutex_unlock(&cd->system_lock);
  11801. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11802. pt_debug(cd->dev, DL_ERROR,
  11803. "%s: Queue Restart\n", __func__);
  11804. pt_queue_restart(cd);
  11805. } else
  11806. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11807. }
  11808. } else {
  11809. cd->hw_detected = true;
  11810. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11811. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11812. pt_debug(cd->dev, DL_ERROR,
  11813. "%s: HW detected but not enumerated\n",
  11814. __func__);
  11815. pt_queue_enum(cd);
  11816. }
  11817. }
  11818. exit:
  11819. pt_start_wd_timer(cd);
  11820. }
  11821. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11822. /*******************************************************************************
  11823. * FUNCTION: pt_watchdog_timer
  11824. *
  11825. * SUMMARY: The function that is called when the WD timer expires. If the
  11826. * watchdog work is not already busy schedule the watchdog work queue.
  11827. *
  11828. * RETURN: void
  11829. *
  11830. * PARAMETERS:
  11831. * handle - Handle to the watchdog timer
  11832. ******************************************************************************/
  11833. static void pt_watchdog_timer(unsigned long handle)
  11834. {
  11835. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11836. if (!cd)
  11837. return;
  11838. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11839. __func__);
  11840. if (!work_pending(&cd->watchdog_work))
  11841. schedule_work(&cd->watchdog_work);
  11842. }
  11843. #else
  11844. /*******************************************************************************
  11845. * FUNCTION: pt_watchdog_timer
  11846. *
  11847. * SUMMARY: The function that is called when the WD timer expires. If the
  11848. * watchdog work is not already busy schedule the watchdog work queue.
  11849. *
  11850. * RETURN: void
  11851. *
  11852. * PARAMETERS:
  11853. * *t - Pointer to timer list
  11854. ******************************************************************************/
  11855. static void pt_watchdog_timer(struct timer_list *t)
  11856. {
  11857. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11858. if (!cd)
  11859. return;
  11860. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11861. __func__);
  11862. if (!work_pending(&cd->watchdog_work))
  11863. schedule_work(&cd->watchdog_work);
  11864. }
  11865. #endif
  11866. /*******************************************************************************
  11867. * Core sysfs show and store functions
  11868. ******************************************************************************/
  11869. /*******************************************************************************
  11870. * FUNCTION: pt_hw_version_show
  11871. *
  11872. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11873. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11874. *
  11875. * RETURN: size of data written to sysfs node
  11876. *
  11877. * PARAMETERS:
  11878. * *dev - pointer to device structure
  11879. * *attr - pointer to device attributes structure
  11880. * *buf - pointer to print output buffer
  11881. ******************************************************************************/
  11882. static ssize_t pt_hw_version_show(struct device *dev,
  11883. struct device_attribute *attr, char *buf)
  11884. {
  11885. struct pt_core_data *cd = dev_get_drvdata(dev);
  11886. _pt_request_hw_version(dev, cd->hw_version);
  11887. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11888. }
  11889. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11890. /*******************************************************************************
  11891. * FUNCTION: pt_drv_version_show
  11892. *
  11893. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11894. * TTDL version information
  11895. *
  11896. * RETURN: Char buffer with printed TTDL version information
  11897. *
  11898. * PARAMETERS:
  11899. * *dev - pointer to device structure
  11900. * *attr - pointer to device attributes
  11901. * *buf - pointer to output buffer
  11902. ******************************************************************************/
  11903. static ssize_t pt_drv_version_show(struct device *dev,
  11904. struct device_attribute *attr, char *buf)
  11905. {
  11906. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11907. "Driver: %s\nVersion: %s\nDate: %s\n",
  11908. pt_driver_core_name, pt_driver_core_version,
  11909. pt_driver_core_date);
  11910. }
  11911. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11912. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11913. /*******************************************************************************
  11914. * FUNCTION: pt_fw_version_show
  11915. *
  11916. * SUMMARY: Show method for the fw_version sysfs node that will
  11917. * show the firmware, bootloader and PIP version information
  11918. *
  11919. * RETURN: Size of printed buffer
  11920. *
  11921. * PARAMETERS:
  11922. * *dev - pointer to device structure
  11923. * *attr - pointer to device attributes
  11924. * *buf - pointer to output buffer
  11925. ******************************************************************************/
  11926. static ssize_t pt_fw_version_show(struct device *dev,
  11927. struct device_attribute *attr, char *buf)
  11928. {
  11929. struct pt_core_data *cd = dev_get_drvdata(dev);
  11930. struct pt_ttdata *ttdata;
  11931. int rc = 0;
  11932. if (cd->mode == PT_MODE_OPERATIONAL)
  11933. rc = pt_hid_output_get_sysinfo_(cd);
  11934. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11935. __func__, cd->mode, cd->sysinfo.ready);
  11936. if (cd->sysinfo.ready)
  11937. ttdata = &cd->sysinfo.ttdata;
  11938. else
  11939. rc = -ENODATA;
  11940. if (!rc) {
  11941. return scnprintf(buf, strlen(buf),
  11942. "Status: %d\n"
  11943. "FW : %d.%d.%d\n"
  11944. "Config: %d\n"
  11945. "BL : %d.%d\n"
  11946. "PIP : %d.%d\n",
  11947. rc,
  11948. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11949. ttdata->revctrl,
  11950. ttdata->fw_ver_conf,
  11951. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11952. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11953. } else {
  11954. return scnprintf(buf, strlen(buf),
  11955. "Status: %d\n"
  11956. "FW : n/a\n"
  11957. "Config: n/a\n"
  11958. "BL : n/a\n"
  11959. "PIP : n/a\n",
  11960. rc);
  11961. }
  11962. }
  11963. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11964. /*******************************************************************************
  11965. * FUNCTION: pt_sysinfo_show
  11966. *
  11967. * SUMMARY: Show method for the sysinfo sysfs node that will
  11968. * show all the information from get system information command.
  11969. *
  11970. * RETURN: Size of printed buffer
  11971. *
  11972. * PARAMETERS:
  11973. * *dev - pointer to device structure
  11974. * *attr - pointer to device attributes
  11975. * *buf - pointer to output buffer
  11976. ******************************************************************************/
  11977. static ssize_t pt_sysinfo_show(struct device *dev,
  11978. struct device_attribute *attr, char *buf)
  11979. {
  11980. struct pt_core_data *cd = dev_get_drvdata(dev);
  11981. struct pt_sysinfo *si;
  11982. struct pt_ttdata *ttdata = NULL;
  11983. struct pt_sensing_conf_data *scd = NULL;
  11984. int rc = 0;
  11985. if (cd->mode == PT_MODE_OPERATIONAL) {
  11986. rc = pt_hid_output_get_sysinfo_(cd);
  11987. if (cd->sysinfo.ready) {
  11988. si = &cd->sysinfo;
  11989. ttdata = &si->ttdata;
  11990. scd = &si->sensing_conf_data;
  11991. } else
  11992. rc = -ENODATA;
  11993. } else
  11994. rc = -EPERM;
  11995. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11996. __func__, cd->mode, cd->sysinfo.ready);
  11997. if (!rc && ttdata && scd) {
  11998. return scnprintf(buf, strlen(buf),
  11999. "Status: %d\n"
  12000. "pip_ver_major: 0x%02X\n"
  12001. "pip_ver_minor: 0x%02X\n"
  12002. "fw_pid : 0x%04X\n"
  12003. "fw_ver_major : 0x%02X\n"
  12004. "fw_ver_minor : 0x%02X\n"
  12005. "revctrl : 0x%08X\n"
  12006. "fw_ver_conf : 0x%04X\n"
  12007. "bl_ver_major : 0x%02X\n"
  12008. "bl_ver_minor : 0x%02X\n"
  12009. "jtag_id_h : 0x%04X\n"
  12010. "jtag_id_l : 0x%04X\n"
  12011. "mfg_id[0] : 0x%02X\n"
  12012. "mfg_id[1] : 0x%02X\n"
  12013. "mfg_id[2] : 0x%02X\n"
  12014. "mfg_id[3] : 0x%02X\n"
  12015. "mfg_id[4] : 0x%02X\n"
  12016. "mfg_id[5] : 0x%02X\n"
  12017. "mfg_id[6] : 0x%02X\n"
  12018. "mfg_id[7] : 0x%02X\n"
  12019. "post_code : 0x%04X\n"
  12020. "electrodes_x : 0x%02X\n"
  12021. "electrodes_y : 0x%02X\n"
  12022. "len_x : 0x%04X\n"
  12023. "len_y : 0x%04X\n"
  12024. "res_x : 0x%04X\n"
  12025. "res_y : 0x%04X\n"
  12026. "max_z : 0x%04X\n"
  12027. "origin_x : 0x%02X\n"
  12028. "origin_y : 0x%02X\n"
  12029. "panel_id : 0x%02X\n"
  12030. "btn : 0x%02X\n"
  12031. "scan_mode : 0x%02X\n"
  12032. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12033. rc,
  12034. ttdata->pip_ver_major,
  12035. ttdata->pip_ver_minor,
  12036. ttdata->fw_pid,
  12037. ttdata->fw_ver_major,
  12038. ttdata->fw_ver_minor,
  12039. ttdata->revctrl,
  12040. ttdata->fw_ver_conf,
  12041. ttdata->bl_ver_major,
  12042. ttdata->bl_ver_minor,
  12043. ttdata->jtag_id_h,
  12044. ttdata->jtag_id_l,
  12045. ttdata->mfg_id[0],
  12046. ttdata->mfg_id[1],
  12047. ttdata->mfg_id[2],
  12048. ttdata->mfg_id[3],
  12049. ttdata->mfg_id[4],
  12050. ttdata->mfg_id[5],
  12051. ttdata->mfg_id[6],
  12052. ttdata->mfg_id[7],
  12053. ttdata->post_code,
  12054. scd->electrodes_x,
  12055. scd->electrodes_y,
  12056. scd->len_x,
  12057. scd->len_y,
  12058. scd->res_x,
  12059. scd->res_y,
  12060. scd->max_z,
  12061. scd->origin_x,
  12062. scd->origin_y,
  12063. scd->panel_id,
  12064. scd->btn,
  12065. scd->scan_mode,
  12066. scd->max_tch);
  12067. } else {
  12068. return scnprintf(buf, strlen(buf),
  12069. "Status: %d\n",
  12070. rc);
  12071. }
  12072. }
  12073. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12074. /*******************************************************************************
  12075. * FUNCTION: pt_hw_reset_show
  12076. *
  12077. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12078. * by toggling the XRES line and then calls the startup function to
  12079. * allow TTDL to re-enumerate the DUT.
  12080. * The printed value reflects the status of the full reset/enum.
  12081. *
  12082. * PARAMETERS:
  12083. * *dev - pointer to Device structure
  12084. * *attr - pointer to the device attribute structure
  12085. * *buf - pointer to buffer to print
  12086. ******************************************************************************/
  12087. static ssize_t pt_hw_reset_show(struct device *dev,
  12088. struct device_attribute *attr, char *buf)
  12089. {
  12090. struct pt_core_data *cd = dev_get_drvdata(dev);
  12091. int rc = 0;
  12092. int time = 0;
  12093. u8 reset_status = 0;
  12094. int t;
  12095. struct pt_hid_desc hid_desc;
  12096. memset(&hid_desc, 0, sizeof(hid_desc));
  12097. /* Only allow DUT reset if no active BL in progress */
  12098. mutex_lock(&cd->firmware_class_lock);
  12099. mutex_lock(&cd->system_lock);
  12100. cd->startup_state = STARTUP_NONE;
  12101. mutex_unlock(&(cd->system_lock));
  12102. pt_stop_wd_timer(cd);
  12103. /* ensure no left over exclusive access is still locked */
  12104. release_exclusive(cd, cd->dev);
  12105. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12106. if (rc) {
  12107. mutex_unlock(&cd->firmware_class_lock);
  12108. pt_debug(cd->dev, DL_ERROR,
  12109. "%s: HW reset failed rc = %d\n", __func__, rc);
  12110. goto exit_hw_reset;
  12111. }
  12112. reset_status |= 0x01 << 0;
  12113. if (cd->flashless_dut) {
  12114. mutex_unlock(&cd->firmware_class_lock);
  12115. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12116. msecs_to_jiffies(200));
  12117. if (IS_TMO(t)) {
  12118. pt_debug(dev, DL_ERROR,
  12119. "%s: Timeout waiting for FW update",
  12120. __func__);
  12121. rc = -ETIME;
  12122. goto exit_hw_reset;
  12123. } else {
  12124. pt_debug(dev, DL_INFO,
  12125. "%s: ----- Wait FW Loading ----",
  12126. __func__);
  12127. rc = _pt_request_wait_for_enum_state(
  12128. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12129. if (rc) {
  12130. pt_debug(cd->dev, DL_ERROR,
  12131. "%s: No FW Sentinel detected rc = %d\n",
  12132. __func__, rc);
  12133. goto exit_hw_reset;
  12134. }
  12135. reset_status |= 0x01 << 1;
  12136. }
  12137. } else {
  12138. /* Wait for any sentinel */
  12139. rc = _pt_request_wait_for_enum_state(dev, 150,
  12140. STARTUP_STATUS_BL_RESET_SENTINEL |
  12141. STARTUP_STATUS_FW_RESET_SENTINEL);
  12142. if (rc) {
  12143. mutex_unlock(&cd->firmware_class_lock);
  12144. pt_debug(cd->dev, DL_ERROR,
  12145. "%s: No Sentinel detected rc = %d\n",
  12146. __func__, rc);
  12147. goto exit_hw_reset;
  12148. }
  12149. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12150. msleep(30);
  12151. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12152. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12153. if (rc < 0) {
  12154. pt_debug(cd->dev, DL_ERROR,
  12155. "%s: Error on getting HID descriptor r=%d\n",
  12156. __func__, rc);
  12157. goto exit_hw_reset;
  12158. }
  12159. cd->mode = pt_get_mode(cd, &hid_desc);
  12160. if (cd->mode == PT_MODE_BOOTLOADER)
  12161. rc = pt_hid_output_bl_launch_app_(cd);
  12162. } else {
  12163. if (cd->mode == PT_MODE_BOOTLOADER)
  12164. rc = pt_pip2_launch_app(dev,
  12165. PT_CORE_CMD_UNPROTECTED);
  12166. }
  12167. if (rc) {
  12168. mutex_unlock(&cd->firmware_class_lock);
  12169. pt_debug(cd->dev, DL_ERROR,
  12170. "%s: PIP launch app failed rc = %d\n",
  12171. __func__, rc);
  12172. goto exit_hw_reset;
  12173. }
  12174. mutex_unlock(&cd->firmware_class_lock);
  12175. reset_status |= 0x01 << 1;
  12176. msleep(20);
  12177. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12178. (cd->mode != PT_MODE_OPERATIONAL))
  12179. pt_queue_restart(cd);
  12180. else
  12181. pt_queue_enum(cd);
  12182. }
  12183. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12184. msleep(50);
  12185. pt_debug(cd->dev, DL_INFO,
  12186. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12187. __func__, time, STARTUP_STATUS_COMPLETE,
  12188. cd->startup_status);
  12189. time += 50;
  12190. }
  12191. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12192. rc = -ETIME;
  12193. goto exit_hw_reset;
  12194. }
  12195. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12196. __func__, cd->startup_status);
  12197. reset_status |= 0x01 << 2;
  12198. pt_start_wd_timer(cd);
  12199. exit_hw_reset:
  12200. return scnprintf(buf, strlen(buf),
  12201. "Status: %d\n"
  12202. "Reset Status: 0x%02X\n", rc, reset_status);
  12203. }
  12204. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12205. /*******************************************************************************
  12206. * FUNCTION: pt_pip2_cmd_rsp_store
  12207. *
  12208. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12209. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12210. * Command byte order:
  12211. * Byte [0] - PIP2 command ID
  12212. * Byte [1-n] - PIP2 command payload
  12213. *
  12214. * RETURN: Size of passed in buffer
  12215. *
  12216. * PARAMETERS:
  12217. * *dev - pointer to device structure
  12218. * *attr - pointer to device attributes
  12219. * *buf - pointer to buffer that hold the command parameters
  12220. * size - size of buf
  12221. ******************************************************************************/
  12222. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12223. struct device_attribute *attr, const char *buf, size_t size)
  12224. {
  12225. struct pt_core_data *cd = dev_get_drvdata(dev);
  12226. u16 actual_read_len;
  12227. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12228. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12229. u8 pip2_cmd_id = 0x00;
  12230. u8 *pip2_cmd_data = NULL;
  12231. int data_len = 0;
  12232. int length;
  12233. int rc = 0;
  12234. /* clear shared data */
  12235. mutex_lock(&cd->sysfs_lock);
  12236. cd->raw_cmd_status = 0;
  12237. cd->cmd_rsp_buf_len = 0;
  12238. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12239. mutex_unlock(&cd->sysfs_lock);
  12240. length = _pt_ic_parse_input_hex(dev, buf, size,
  12241. input_data, PT_MAX_PIP2_MSG_SIZE);
  12242. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12243. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12244. __func__);
  12245. rc = -EINVAL;
  12246. goto exit;
  12247. }
  12248. /* Send PIP2 command if enough data was provided */
  12249. if (length >= 1) {
  12250. pip2_cmd_id = input_data[0];
  12251. pip2_cmd_data = &input_data[1];
  12252. data_len = length - 1;
  12253. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12254. pip2_cmd_id, pip2_cmd_data, data_len,
  12255. read_buf, &actual_read_len);
  12256. cd->raw_cmd_status = rc;
  12257. if (rc) {
  12258. pt_debug(dev, DL_ERROR,
  12259. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12260. __func__, pip2_cmd_id, rc);
  12261. goto exit;
  12262. } else {
  12263. cd->cmd_rsp_buf_len = actual_read_len;
  12264. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12265. pt_debug(dev, DL_ERROR,
  12266. "%s: PIP2 actual_read_len = %d\n",
  12267. __func__, actual_read_len);
  12268. }
  12269. } else {
  12270. rc = -EINVAL;
  12271. pt_debug(dev, DL_ERROR,
  12272. "%s: Insufficient data provided for PIP2 cmd\n",
  12273. __func__);
  12274. }
  12275. exit:
  12276. if (rc)
  12277. return rc;
  12278. return size;
  12279. }
  12280. /*******************************************************************************
  12281. * FUNCTION: pt_pip2_cmd_rsp_show
  12282. *
  12283. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12284. * response generated after using the store method of the pip2_cmd_rsp
  12285. * sysfs node, are available to be read here.
  12286. *
  12287. * PARAMETERS:
  12288. * *dev - pointer to Device structure
  12289. * *attr - pointer to the device attribute structure
  12290. * *buf - pointer to buffer to print
  12291. ******************************************************************************/
  12292. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12293. struct device_attribute *attr, char *buf)
  12294. {
  12295. struct pt_core_data *cd = dev_get_drvdata(dev);
  12296. int i;
  12297. ssize_t data_len;
  12298. int index;
  12299. mutex_lock(&cd->sysfs_lock);
  12300. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12301. "Status: %d\n", cd->raw_cmd_status);
  12302. if (cd->raw_cmd_status)
  12303. goto error;
  12304. /* Remove the CRC from the length of the response */
  12305. data_len = cd->cmd_rsp_buf_len - 2;
  12306. /* Start printing from the data payload */
  12307. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12308. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12309. "%02X ", cd->cmd_rsp_buf[i]);
  12310. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12311. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12312. "\n(%zd bytes)\n",
  12313. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12314. } else {
  12315. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12316. "\n(%zd bytes)\n", 0);
  12317. }
  12318. error:
  12319. mutex_unlock(&cd->sysfs_lock);
  12320. return index;
  12321. }
  12322. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12323. pt_pip2_cmd_rsp_store);
  12324. /*******************************************************************************
  12325. * FUNCTION: pt_command_store
  12326. *
  12327. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12328. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12329. * TTDL will not parse the command.
  12330. *
  12331. * RETURN: Size of passed in buffer
  12332. *
  12333. * PARAMETERS:
  12334. * *dev - pointer to device structure
  12335. * *attr - pointer to device attributes
  12336. * *buf - pointer to buffer that hold the command parameters
  12337. * size - size of buf
  12338. ******************************************************************************/
  12339. static ssize_t pt_command_store(struct device *dev,
  12340. struct device_attribute *attr, const char *buf, size_t size)
  12341. {
  12342. struct pt_core_data *cd = dev_get_drvdata(dev);
  12343. unsigned short crc;
  12344. u16 actual_read_len;
  12345. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12346. int length;
  12347. int len_field;
  12348. int rc = 0;
  12349. mutex_lock(&cd->sysfs_lock);
  12350. cd->cmd_rsp_buf_len = 0;
  12351. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12352. mutex_unlock(&cd->sysfs_lock);
  12353. length = _pt_ic_parse_input_hex(dev, buf, size,
  12354. input_data, PT_MAX_PIP2_MSG_SIZE);
  12355. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12356. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12357. __func__);
  12358. rc = -EINVAL;
  12359. goto pt_command_store_exit;
  12360. }
  12361. /* PIP2 messages begin with 01 01 */
  12362. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12363. cd->pip2_prot_active = 1;
  12364. /* Override next seq tag with what was sent */
  12365. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12366. /* For PIP2 cmd if length does not include crc, add it */
  12367. len_field = (input_data[3] << 8) | input_data[2];
  12368. if (len_field == length && length <= 254) {
  12369. crc = crc_ccitt_calculate(&input_data[2],
  12370. length - 2);
  12371. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12372. __func__, length, crc);
  12373. input_data[length] = (crc & 0xFF00) >> 8;
  12374. input_data[length + 1] = crc & 0x00FF;
  12375. length = length + 2;
  12376. }
  12377. }
  12378. /* write PIP command to log */
  12379. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12380. pm_runtime_get_sync(dev);
  12381. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12382. length, input_data, &actual_read_len);
  12383. pm_runtime_put(dev);
  12384. mutex_lock(&cd->sysfs_lock);
  12385. cd->raw_cmd_status = rc;
  12386. if (rc) {
  12387. cd->cmd_rsp_buf_len = 0;
  12388. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12389. __func__, buf);
  12390. } else {
  12391. cd->cmd_rsp_buf_len = actual_read_len;
  12392. }
  12393. cd->pip2_prot_active = 0;
  12394. mutex_unlock(&cd->sysfs_lock);
  12395. pt_command_store_exit:
  12396. if (rc)
  12397. return rc;
  12398. return size;
  12399. }
  12400. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12401. /*******************************************************************************
  12402. * FUNCTION: pt_response_show
  12403. *
  12404. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12405. * response generated after using the pt_command_store sysfs node, are
  12406. * available to be read here.
  12407. *
  12408. * PARAMETERS:
  12409. * *dev - pointer to Device structure
  12410. * *attr - pointer to the device attribute structure
  12411. * *buf - pointer to buffer to print
  12412. ******************************************************************************/
  12413. static ssize_t pt_response_show(struct device *dev,
  12414. struct device_attribute *attr, char *buf)
  12415. {
  12416. struct pt_core_data *cd = dev_get_drvdata(dev);
  12417. int i;
  12418. ssize_t num_read;
  12419. int index;
  12420. mutex_lock(&cd->sysfs_lock);
  12421. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12422. "Status: %d\n", cd->raw_cmd_status);
  12423. if (cd->raw_cmd_status)
  12424. goto error;
  12425. num_read = cd->cmd_rsp_buf_len;
  12426. for (i = 0; i < num_read; i++)
  12427. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12428. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12429. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12430. "(%zd bytes)\n", num_read);
  12431. error:
  12432. mutex_unlock(&cd->sysfs_lock);
  12433. return index;
  12434. }
  12435. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12436. /*******************************************************************************
  12437. * FUNCTION: pt_dut_debug_show
  12438. *
  12439. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12440. * are available for the store method.
  12441. *
  12442. * RETURN:
  12443. * 0 = success
  12444. * !0 = failure
  12445. *
  12446. * PARAMETERS:
  12447. * *dev - pointer to device structure
  12448. * *attr - pointer to device attributes
  12449. * *buf - pointer to output buffer
  12450. ******************************************************************************/
  12451. static ssize_t pt_dut_debug_show(struct device *dev,
  12452. struct device_attribute *attr, char *buf)
  12453. {
  12454. ssize_t ret;
  12455. ret = scnprintf(buf, strlen(buf),
  12456. "Status: 0\n"
  12457. "dut_debug sends the following commands to the DUT:\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. "%d %s \t- %s\n"
  12474. "%d %s \t- %s\n"
  12475. ,
  12476. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12477. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12478. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12479. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12480. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12481. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12482. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12483. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12484. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12485. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12486. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12487. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12488. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12489. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12490. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12491. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12492. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12493. );
  12494. return ret;
  12495. }
  12496. /*******************************************************************************
  12497. * FUNCTION: pt_drv_debug_show
  12498. *
  12499. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12500. * are available for the store method.
  12501. *
  12502. * RETURN:
  12503. * 0 = success
  12504. * !0 = failure
  12505. *
  12506. * PARAMETERS:
  12507. * *dev - pointer to device structure
  12508. * *attr - pointer to device attributes
  12509. * *buf - pointer to output buffer
  12510. ******************************************************************************/
  12511. static ssize_t pt_drv_debug_show(struct device *dev,
  12512. struct device_attribute *attr, char *buf)
  12513. {
  12514. ssize_t ret;
  12515. ret = scnprintf(buf, strlen(buf),
  12516. "Status: 0\n"
  12517. "drv_debug supports the following values:\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 \t- %s\n"
  12526. "%d %s \t- %s\n"
  12527. "%d %s - %s\n"
  12528. "%d %s \t- %s\n"
  12529. "%d %s \t- %s\n"
  12530. #ifdef TTDL_DIAGNOSTICS
  12531. "%d %s \t- %s\n"
  12532. "%d %s \t- %s\n"
  12533. "%d %s \t- %s\n"
  12534. #endif /* TTDL_DIAGNOSTICS */
  12535. "%d %s \t- %s\n"
  12536. #ifdef TTDL_DIAGNOSTICS
  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. "%d %s \t- %s\n"
  12545. "%d %s \t- %s\n"
  12546. #endif /* TTDL_DIAGNOSTICS */
  12547. ,
  12548. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12549. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12550. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12551. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12552. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12553. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12554. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12555. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12556. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12557. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12558. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12559. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12560. #ifdef TTDL_DIAGNOSTICS
  12561. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12562. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12563. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12564. #endif /* TTDL_DIAGNOSTICS */
  12565. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12566. #ifdef TTDL_DIAGNOSTICS
  12567. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12568. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12569. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12570. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12571. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12572. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12573. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12574. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12575. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12576. "TTHE_TUNER HID USB Format"
  12577. #endif /* TTDL_DIAGNOSTICS */
  12578. );
  12579. return ret;
  12580. }
  12581. /*******************************************************************************
  12582. * FUNCTION: pt_drv_debug_store
  12583. *
  12584. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12585. * dut_debug. Drv_debug will contain all functionality that can be run
  12586. * without a DUT preset and is available anytime TTDL is running.
  12587. * Dut_debug requires a DUT to be available and will only be created after
  12588. * a DUT has been detected.
  12589. * This function will eventually be split into two but until the overlap
  12590. * has been depricated this function contains all commands that can be
  12591. * used for TTDL/DUT debugging status and control.
  12592. * All commands require at least one value to be passed in *buf with some
  12593. * requiring two.
  12594. *
  12595. * RETURN: Size of passed in buffer
  12596. *
  12597. * PARAMETERS:
  12598. * *dev - pointer to device structure
  12599. * *attr - pointer to device attributes
  12600. * *buf - pointer to buffer that hold the command parameters
  12601. * size - size of buf
  12602. ******************************************************************************/
  12603. static ssize_t pt_drv_debug_store(struct device *dev,
  12604. struct device_attribute *attr, const char *buf, size_t size)
  12605. {
  12606. struct pt_core_data *cd = dev_get_drvdata(dev);
  12607. unsigned long value;
  12608. int rc = 0;
  12609. u8 return_data[8];
  12610. static u8 wd_disabled;
  12611. u32 input_data[3];
  12612. int length;
  12613. #ifdef TTDL_DIAGNOSTICS
  12614. struct i2c_client *client = to_i2c_client(dev);
  12615. unsigned short crc = 0;
  12616. u16 cal_size;
  12617. #endif
  12618. input_data[0] = 0;
  12619. input_data[1] = 0;
  12620. /* Maximmum input is two elements */
  12621. length = _pt_ic_parse_input(dev, buf, size,
  12622. input_data, ARRAY_SIZE(input_data));
  12623. if (length < 1 || length > 2) {
  12624. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12625. __func__);
  12626. rc = -EINVAL;
  12627. goto pt_drv_debug_store_exit;
  12628. }
  12629. value = input_data[0];
  12630. if (length == 1) {
  12631. pt_debug(dev, DL_DEBUG,
  12632. "%s: Debug Cmd Received (id=%d)\n",
  12633. __func__, input_data[0]);
  12634. } else if (length == 2) {
  12635. pt_debug(dev, DL_DEBUG,
  12636. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12637. __func__, input_data[0], input_data[1]);
  12638. } else {
  12639. pt_debug(dev, DL_DEBUG,
  12640. "%s: Invalid arguments received\n", __func__);
  12641. rc = -EINVAL;
  12642. goto pt_drv_debug_store_exit;
  12643. }
  12644. /* Start watchdog timer command */
  12645. if (value == PT_DRV_DBG_START_WD) {
  12646. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12647. wd_disabled = 0;
  12648. cd->watchdog_force_stop = false;
  12649. pt_start_wd_timer(cd);
  12650. goto pt_drv_debug_store_exit;
  12651. }
  12652. /* Stop watchdog timer temporarily */
  12653. pt_stop_wd_timer(cd);
  12654. if (value == PT_DRV_DBG_STOP_WD) {
  12655. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12656. wd_disabled = 1;
  12657. cd->watchdog_force_stop = true;
  12658. goto pt_drv_debug_store_exit;
  12659. }
  12660. switch (value) {
  12661. case PT_DRV_DBG_SUSPEND: /* 4 */
  12662. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12663. wd_disabled = 1;
  12664. rc = pt_core_sleep(cd);
  12665. if (rc)
  12666. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12667. __func__, rc);
  12668. else
  12669. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12670. __func__);
  12671. break;
  12672. case PT_DRV_DBG_RESUME: /* 5 */
  12673. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12674. rc = pt_core_wake(cd);
  12675. if (rc)
  12676. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12677. __func__, rc);
  12678. else
  12679. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12680. __func__);
  12681. break;
  12682. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12683. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12684. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12685. break;
  12686. case PT_DUT_DBG_HID_RESET: /* 50 */
  12687. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12688. pt_hid_cmd_reset(cd);
  12689. break;
  12690. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12691. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12692. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12693. wd_disabled = 0;
  12694. break;
  12695. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12696. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12697. __func__);
  12698. wd_disabled = 1;
  12699. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12700. break;
  12701. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12702. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12703. __func__);
  12704. wd_disabled = 1;
  12705. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12706. break;
  12707. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12708. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12709. pt_hid_output_bl_get_information(cd, return_data);
  12710. break;
  12711. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12712. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12713. __func__);
  12714. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12715. break;
  12716. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12717. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12718. pt_hid_output_bl_launch_app(cd);
  12719. break;
  12720. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12721. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12722. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12723. break;
  12724. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12725. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12726. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12727. break;
  12728. case PT_DUT_DBG_RESET: /* 98 */
  12729. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12730. rc = pt_hw_hard_reset(cd);
  12731. break;
  12732. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12733. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12734. pt_pip_null(cd);
  12735. break;
  12736. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12737. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12738. rc = pt_pip_start_bootloader(cd);
  12739. if (!rc) {
  12740. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12741. cd->mode = PT_MODE_BOOTLOADER;
  12742. }
  12743. break;
  12744. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12745. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12746. pt_hid_output_get_sysinfo(cd);
  12747. break;
  12748. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12749. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12750. pt_pip_suspend_scanning(cd);
  12751. break;
  12752. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12753. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12754. pt_pip_resume_scanning(cd);
  12755. break;
  12756. #ifdef TTHE_TUNER_SUPPORT
  12757. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12758. cd->tthe_exit = 1;
  12759. wake_up(&cd->wait_q);
  12760. kfree(cd->tthe_buf);
  12761. cd->tthe_buf = NULL;
  12762. cd->tthe_exit = 0;
  12763. break;
  12764. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12765. if (cd->tthe_buf)
  12766. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12767. else
  12768. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12769. __func__);
  12770. break;
  12771. #endif
  12772. #ifdef TTDL_DIAGNOSTICS
  12773. case PT_DUT_DBG_HID_DESC: /* 109 */
  12774. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12775. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12776. break;
  12777. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12778. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12779. __func__);
  12780. pt_erase_parameter_list(cd);
  12781. break;
  12782. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12783. rc = pt_read_input(cd);
  12784. if (!rc)
  12785. pt_parse_input(cd);
  12786. break;
  12787. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12788. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12789. &cal_size, &crc);
  12790. if (rc)
  12791. pt_debug(dev, DL_ERROR,
  12792. "%s: CAL Data clear failed rc=%d\n",
  12793. __func__, rc);
  12794. else
  12795. pt_debug(dev, DL_INFO,
  12796. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12797. __func__, crc, size);
  12798. break;
  12799. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12800. mutex_lock(&cd->system_lock);
  12801. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12802. cd->debug_level = input_data[1];
  12803. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12804. __func__, cd->debug_level);
  12805. } else {
  12806. rc = -EINVAL;
  12807. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12808. __func__, input_data[1]);
  12809. }
  12810. mutex_unlock(&(cd->system_lock));
  12811. break;
  12812. #endif
  12813. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12814. mutex_lock(&cd->system_lock);
  12815. if (input_data[1] > 100) {
  12816. cd->watchdog_interval = input_data[1];
  12817. pt_debug(dev, DL_INFO,
  12818. "%s: Set watchdog_ interval to: %d\n",
  12819. __func__, cd->watchdog_interval);
  12820. pt_start_wd_timer(cd);
  12821. } else {
  12822. rc = -EINVAL;
  12823. pt_debug(dev, DL_ERROR,
  12824. "%s: Invalid watchdog interval: %d\n",
  12825. __func__, input_data[1]);
  12826. }
  12827. mutex_unlock(&(cd->system_lock));
  12828. break;
  12829. #ifdef TTDL_DIAGNOSTICS
  12830. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12831. mutex_lock(&cd->system_lock);
  12832. if (input_data[1] == 0) {
  12833. cd->show_timestamp = 0;
  12834. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12835. __func__);
  12836. } else if (input_data[1] == 1) {
  12837. cd->show_timestamp = 1;
  12838. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12839. __func__);
  12840. } else {
  12841. rc = -EINVAL;
  12842. pt_debug(dev, DL_ERROR,
  12843. "%s: Invalid parameter: %d\n",
  12844. __func__, input_data[1]);
  12845. }
  12846. mutex_unlock(&(cd->system_lock));
  12847. break;
  12848. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12849. if (input_data[1] == 0) {
  12850. cd->cpdata->setup_power(cd->cpdata,
  12851. PT_MT_POWER_OFF, cd->dev);
  12852. pt_debug(dev, DL_INFO,
  12853. "%s: Initiate Power Off\n", __func__);
  12854. } else if (input_data[1] == 1) {
  12855. cd->cpdata->setup_power(cd->cpdata,
  12856. PT_MT_POWER_ON, cd->dev);
  12857. pt_debug(dev, DL_INFO,
  12858. "%s: Initiate Power On\n", __func__);
  12859. } else {
  12860. rc = -EINVAL;
  12861. pt_debug(dev, DL_ERROR,
  12862. "%s: Invalid parameter: %d\n",
  12863. __func__, input_data[1]);
  12864. }
  12865. break;
  12866. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12867. if (input_data[1] == 0) {
  12868. pm_runtime_put(dev);
  12869. pt_debug(dev, DL_ERROR,
  12870. "%s: Force call pm_runtime_put()\n", __func__);
  12871. } else if (input_data[1] == 1) {
  12872. pm_runtime_get_sync(dev);
  12873. pt_debug(dev, DL_ERROR,
  12874. "%s: Force call pm_runtime_get_sync()\n",
  12875. __func__);
  12876. } else {
  12877. rc = -EINVAL;
  12878. pt_debug(dev, DL_ERROR,
  12879. "%s: WARNING: Invalid parameter: %d\n",
  12880. __func__, input_data[1]);
  12881. }
  12882. break;
  12883. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12884. mutex_lock(&cd->system_lock);
  12885. if (input_data[1] == 0) {
  12886. cd->show_tt_data = false;
  12887. pt_debug(dev, DL_INFO,
  12888. "%s: Disable TT_DATA\n", __func__);
  12889. } else if (input_data[1] == 1) {
  12890. cd->show_tt_data = true;
  12891. pt_debug(dev, DL_INFO,
  12892. "%s: Enable TT_DATA\n", __func__);
  12893. } else {
  12894. rc = -EINVAL;
  12895. pt_debug(dev, DL_ERROR,
  12896. "%s: Invalid parameter: %d\n",
  12897. __func__, input_data[1]);
  12898. }
  12899. mutex_unlock(&(cd->system_lock));
  12900. break;
  12901. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12902. if (input_data[1] == cd->active_dut_generation) {
  12903. mutex_lock(&cd->system_lock);
  12904. cd->set_dut_generation = true;
  12905. mutex_unlock(&(cd->system_lock));
  12906. } else {
  12907. mutex_lock(&cd->system_lock);
  12908. if (input_data[1] == 0) {
  12909. cd->active_dut_generation = DUT_UNKNOWN;
  12910. cd->set_dut_generation = false;
  12911. } else if (input_data[1] == 1) {
  12912. cd->active_dut_generation = DUT_PIP1_ONLY;
  12913. cd->set_dut_generation = true;
  12914. } else if (input_data[1] == 2) {
  12915. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12916. cd->set_dut_generation = true;
  12917. } else {
  12918. rc = -EINVAL;
  12919. pt_debug(dev, DL_ERROR,
  12920. "%s: Invalid parameter: %d\n",
  12921. __func__, input_data[1]);
  12922. break;
  12923. }
  12924. cd->startup_status = STARTUP_STATUS_START;
  12925. pt_debug(cd->dev, DL_DEBUG,
  12926. "%s: Startup Status Reset\n", __func__);
  12927. mutex_unlock(&(cd->system_lock));
  12928. pt_debug(dev, DL_INFO,
  12929. "%s: Active DUT Generation Set to: %d\n",
  12930. __func__, cd->active_dut_generation);
  12931. /* Changing DUT generations full restart needed */
  12932. pt_queue_restart(cd);
  12933. }
  12934. break;
  12935. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12936. mutex_lock(&cd->system_lock);
  12937. if (input_data[1] == 0) {
  12938. cd->bridge_mode = false;
  12939. pt_debug(dev, DL_INFO,
  12940. "%s: Disable Bridge Mode\n", __func__);
  12941. } else if (input_data[1] == 1) {
  12942. cd->bridge_mode = true;
  12943. pt_debug(dev, DL_INFO,
  12944. "%s: Enable Bridge Mode\n", __func__);
  12945. } else {
  12946. rc = -EINVAL;
  12947. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12948. __func__, input_data[1]);
  12949. }
  12950. mutex_unlock(&(cd->system_lock));
  12951. break;
  12952. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12953. mutex_lock(&cd->system_lock);
  12954. /* Only a 7bit address is valid */
  12955. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12956. client->addr = input_data[1];
  12957. pt_debug(dev, DL_INFO,
  12958. "%s: Set I2C Address: 0x%2X\n",
  12959. __func__, client->addr);
  12960. } else {
  12961. rc = -EINVAL;
  12962. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12963. __func__, input_data[1]);
  12964. client->addr = 0x24;
  12965. }
  12966. mutex_unlock(&(cd->system_lock));
  12967. break;
  12968. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12969. mutex_lock(&cd->system_lock);
  12970. if (input_data[1] == 0) {
  12971. cd->flashless_dut = 0;
  12972. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12973. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12974. __func__);
  12975. } else if (input_data[1] == 1) {
  12976. cd->flashless_dut = 1;
  12977. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12978. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12979. __func__);
  12980. }
  12981. mutex_unlock(&(cd->system_lock));
  12982. break;
  12983. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12984. mutex_lock(&cd->system_lock);
  12985. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12986. cd->force_pip2_seq = input_data[1];
  12987. cd->pip2_cmd_tag_seq = input_data[1];
  12988. pt_debug(dev, DL_INFO,
  12989. "%s: Force PIP2 Seq to: 0x%02X\n",
  12990. __func__, input_data[1]);
  12991. } else {
  12992. cd->force_pip2_seq = 0;
  12993. pt_debug(dev, DL_INFO,
  12994. "%s: Clear Forced PIP2 Seq\n", __func__);
  12995. }
  12996. mutex_unlock(&(cd->system_lock));
  12997. break;
  12998. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12999. mutex_lock(&cd->system_lock);
  13000. if (input_data[1] == 0) {
  13001. cd->bl_with_no_int = 0;
  13002. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13003. } else if (input_data[1] == 1) {
  13004. cd->bl_with_no_int = 1;
  13005. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13006. __func__);
  13007. } else {
  13008. rc = -EINVAL;
  13009. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13010. __func__, input_data[1]);
  13011. }
  13012. mutex_unlock(&(cd->system_lock));
  13013. break;
  13014. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13015. mutex_lock(&cd->system_lock);
  13016. if (input_data[1] == 0) {
  13017. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13018. pt_debug(dev, DL_INFO,
  13019. "%s: Disable Calibration cache in host\n",
  13020. __func__);
  13021. } else if (input_data[1] == 1) {
  13022. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13023. pt_debug(dev, DL_INFO,
  13024. "%s: Enable Calibration cache in host\n",
  13025. __func__);
  13026. } else {
  13027. rc = -EINVAL;
  13028. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13029. __func__, input_data[1]);
  13030. }
  13031. mutex_unlock(&(cd->system_lock));
  13032. break;
  13033. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13034. mutex_lock(&cd->system_lock);
  13035. if (input_data[1] == 0) {
  13036. cd->multi_chip = PT_FEATURE_DISABLE;
  13037. cd->ttdl_bist_select = 0x07;
  13038. pt_debug(dev, DL_INFO,
  13039. "%s: Disable Multi-chip support\n", __func__);
  13040. } else if (input_data[1] == 1) {
  13041. cd->multi_chip = PT_FEATURE_ENABLE;
  13042. cd->ttdl_bist_select = 0x3F;
  13043. pt_debug(dev, DL_INFO,
  13044. "%s: Enable Multi-chip support\n", __func__);
  13045. } else {
  13046. rc = -EINVAL;
  13047. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13048. __func__, input_data[1]);
  13049. }
  13050. mutex_unlock(&(cd->system_lock));
  13051. break;
  13052. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13053. mutex_lock(&cd->system_lock);
  13054. if (input_data[1] <= 0x07) {
  13055. cd->panel_id_support = input_data[1];
  13056. pt_debug(dev, DL_INFO,
  13057. "%s: Set panel_id_support to %d\n",
  13058. __func__, cd->panel_id_support);
  13059. } else {
  13060. rc = -EINVAL;
  13061. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13062. __func__, input_data[1]);
  13063. }
  13064. mutex_unlock(&(cd->system_lock));
  13065. break;
  13066. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13067. mutex_lock(&cd->system_lock);
  13068. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13069. /*
  13070. * The timeout is changed for some cases so the
  13071. * pip_cmd_timeout_default is used to retore back to
  13072. * what the user requested as the new timeout.
  13073. */
  13074. cd->pip_cmd_timeout_default = input_data[1];
  13075. cd->pip_cmd_timeout = input_data[1];
  13076. pt_debug(dev, DL_INFO,
  13077. "%s: PIP Timeout = %d\n", __func__,
  13078. cd->pip_cmd_timeout_default);
  13079. } else {
  13080. rc = -EINVAL;
  13081. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13082. __func__, input_data[1]);
  13083. }
  13084. mutex_unlock(&(cd->system_lock));
  13085. break;
  13086. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13087. mutex_lock(&cd->system_lock);
  13088. if (input_data[1] == 0) {
  13089. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13090. pt_debug(dev, DL_INFO,
  13091. "%s: Disable tthe_tuner HID-USB format\n",
  13092. __func__);
  13093. } else if (input_data[1] == 1) {
  13094. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13095. pt_debug(dev, DL_INFO,
  13096. "%s: Enable tthe_tuner HID-USB format\n",
  13097. __func__);
  13098. } else {
  13099. rc = -EINVAL;
  13100. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13101. __func__, input_data[1]);
  13102. }
  13103. mutex_unlock(&(cd->system_lock));
  13104. break;
  13105. #endif /* TTDL_DIAGNOSTICS */
  13106. default:
  13107. rc = -EINVAL;
  13108. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13109. }
  13110. /* Enable watchdog timer */
  13111. if (!wd_disabled)
  13112. pt_start_wd_timer(cd);
  13113. pt_drv_debug_store_exit:
  13114. if (rc)
  13115. return rc;
  13116. return size;
  13117. }
  13118. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13119. pt_drv_debug_store);
  13120. /*******************************************************************************
  13121. * FUNCTION: pt_sleep_status_show
  13122. *
  13123. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13124. * sleep status as either ON or OFF
  13125. *
  13126. * RETURN:
  13127. * 0 = success
  13128. * !0 = failure
  13129. *
  13130. * PARAMETERS:
  13131. * *dev - pointer to device structure
  13132. * *attr - pointer to device attributes
  13133. * *buf - pointer to output buffer
  13134. ******************************************************************************/
  13135. static ssize_t pt_sleep_status_show(struct device *dev,
  13136. struct device_attribute *attr, char *buf)
  13137. {
  13138. struct pt_core_data *cd = dev_get_drvdata(dev);
  13139. ssize_t ret;
  13140. mutex_lock(&cd->system_lock);
  13141. if (cd->sleep_state == SS_SLEEP_ON)
  13142. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13143. else
  13144. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13145. mutex_unlock(&cd->system_lock);
  13146. return ret;
  13147. }
  13148. /*******************************************************************************
  13149. * FUNCTION: pt_panel_id_show
  13150. *
  13151. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13152. * detected panel ID from the DUT
  13153. *
  13154. * RETURN:
  13155. * 0 = success
  13156. * !0 = failure
  13157. *
  13158. * PARAMETERS:
  13159. * *dev - pointer to device structure
  13160. * *attr - pointer to device attributes
  13161. * *buf - pointer to output buffer
  13162. ******************************************************************************/
  13163. static ssize_t pt_panel_id_show(struct device *dev,
  13164. struct device_attribute *attr, char *buf)
  13165. {
  13166. struct pt_core_data *cd = dev_get_drvdata(dev);
  13167. ssize_t ret;
  13168. u8 pid = PANEL_ID_NOT_ENABLED;
  13169. int rc = 0;
  13170. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13171. /*
  13172. * The DUT should report the same panel ID from both the BL and
  13173. * the FW unless the panel_id feature is set to only
  13174. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13175. * to retrieve the panel_id.
  13176. */
  13177. if (cd->mode == PT_MODE_BOOTLOADER) {
  13178. /*
  13179. * Return the stored value if PT_PANEL_ID_BY_BL
  13180. * is not supported while other feature exits.
  13181. */
  13182. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13183. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13184. if (rc) {
  13185. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13186. "Failed to retrieve Panel ID. ",
  13187. "Using cached value\n",
  13188. __func__);
  13189. }
  13190. }
  13191. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13192. if (cd->panel_id_support &
  13193. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13194. /* For all systems sysinfo has the panel_id */
  13195. rc = pt_hid_output_get_sysinfo(cd);
  13196. if (!rc)
  13197. pid =
  13198. cd->sysinfo.sensing_conf_data.panel_id;
  13199. pt_debug(dev, DL_ERROR,
  13200. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13201. __func__, rc, pid);
  13202. }
  13203. } else {
  13204. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13205. __func__);
  13206. rc = -EPERM;
  13207. }
  13208. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13209. if (cd->mode == PT_MODE_BOOTLOADER) {
  13210. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13211. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13212. if (rc) {
  13213. pt_debug(dev, DL_ERROR,
  13214. "%s: BL get panel ID failed rc=%d\n",
  13215. __func__, rc);
  13216. }
  13217. }
  13218. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13219. if (cd->panel_id_support &
  13220. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13221. rc = pt_hid_output_get_sysinfo(cd);
  13222. if (!rc)
  13223. pid =
  13224. cd->sysinfo.sensing_conf_data.panel_id;
  13225. pt_debug(dev, DL_ERROR,
  13226. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13227. __func__, rc, pid);
  13228. }
  13229. } else {
  13230. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13231. __func__);
  13232. rc = -EPERM;
  13233. }
  13234. } else {
  13235. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13236. __func__);
  13237. rc = -EPERM;
  13238. }
  13239. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13240. rc, pid);
  13241. return ret;
  13242. }
  13243. /*******************************************************************************
  13244. * FUNCTION: pt_get_param_store
  13245. *
  13246. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13247. * ID to retrieve with the show method.
  13248. *
  13249. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13250. *
  13251. * RETURN: Size of passed in buffer if successful
  13252. *
  13253. * PARAMETERS:
  13254. * *dev - pointer to device structure
  13255. * *attr - pointer to device attributes
  13256. * *buf - pointer to output buffer
  13257. * size - size of buf
  13258. ******************************************************************************/
  13259. static ssize_t pt_get_param_store(struct device *dev,
  13260. struct device_attribute *attr, const char *buf, size_t size)
  13261. {
  13262. struct pt_core_data *cd = dev_get_drvdata(dev);
  13263. u32 input_data[2];
  13264. int length;
  13265. int rc = 0;
  13266. /* Maximum input of one value */
  13267. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13268. ARRAY_SIZE(input_data));
  13269. if (length != 1) {
  13270. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13271. __func__);
  13272. rc = -EINVAL;
  13273. goto exit;
  13274. }
  13275. mutex_lock(&cd->system_lock);
  13276. cd->get_param_id = input_data[0];
  13277. mutex_unlock(&(cd->system_lock));
  13278. exit:
  13279. if (rc)
  13280. return rc;
  13281. return size;
  13282. }
  13283. /*******************************************************************************
  13284. * FUNCTION: pt_get_param_show
  13285. *
  13286. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13287. * parameter data from the DUT based on the ID stored in the core
  13288. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13289. * communicate or some other error occures, an error status is returned
  13290. * with no value following.
  13291. * Output is in the form:
  13292. * Status: x
  13293. * 0xyyyyyyyy
  13294. * The 32bit data will only follow the status code if the status == 0
  13295. *
  13296. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13297. *
  13298. * RETURN:
  13299. * 0 = success
  13300. * !0 = failure
  13301. *
  13302. * PARAMETERS:
  13303. * *dev - pointer to device structure
  13304. * *attr - pointer to device attributes
  13305. * *buf - pointer to output buffer
  13306. ******************************************************************************/
  13307. static ssize_t pt_get_param_show(struct device *dev,
  13308. struct device_attribute *attr, char *buf)
  13309. {
  13310. struct pt_core_data *cd = dev_get_drvdata(dev);
  13311. ssize_t ret = 0;
  13312. int status;
  13313. u32 value = 0;
  13314. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13315. if (status) {
  13316. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13317. __func__, "pt_get_param", status);
  13318. ret = scnprintf(buf, strlen(buf),
  13319. "%s %d\n",
  13320. "Status:", status);
  13321. } else {
  13322. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13323. __func__, cd->get_param_id, value);
  13324. ret = scnprintf(buf, strlen(buf),
  13325. "Status: %d\n"
  13326. "0x%04X\n",
  13327. status, value);
  13328. }
  13329. return ret;
  13330. }
  13331. /*******************************************************************************
  13332. * FUNCTION: pt_ttdl_restart_show
  13333. *
  13334. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13335. * probed modules, calls startup() and then re-probes modules.
  13336. *
  13337. * RETURN: size of data written to sysfs node
  13338. *
  13339. * PARAMETERS:
  13340. * *dev - pointer to device structure
  13341. * *attr - pointer to device attributes structure
  13342. * *buf - pointer to print output buffer
  13343. ******************************************************************************/
  13344. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13345. struct device_attribute *attr, char *buf)
  13346. {
  13347. struct pt_core_data *cd = dev_get_drvdata(dev);
  13348. int t;
  13349. int rc = 0;
  13350. mutex_lock(&cd->system_lock);
  13351. cd->startup_state = STARTUP_NONE;
  13352. mutex_unlock(&(cd->system_lock));
  13353. /* ensure no left over exclusive access is still locked */
  13354. release_exclusive(cd, cd->dev);
  13355. pt_queue_restart(cd);
  13356. t = wait_event_timeout(cd->wait_q,
  13357. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13358. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13359. if (IS_TMO(t)) {
  13360. pt_debug(cd->dev, DL_ERROR,
  13361. "%s: TMO waiting for FW sentinel\n", __func__);
  13362. rc = -ETIME;
  13363. }
  13364. return scnprintf(buf, strlen(buf),
  13365. "Status: %d\n"
  13366. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13367. }
  13368. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13369. /*******************************************************************************
  13370. * FUNCTION: pt_pip2_gpio_read_show
  13371. *
  13372. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13373. * contents of the response to the passed in output buffer.
  13374. *
  13375. * RETURN: size of data written to sysfs node
  13376. *
  13377. * PARAMETERS:
  13378. * *dev - pointer to device structure
  13379. * *attr - pointer to device attributes structure
  13380. * *buf - pointer to print output buffer
  13381. ******************************************************************************/
  13382. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13383. struct device_attribute *attr, char *buf)
  13384. {
  13385. struct pt_core_data *cd = dev_get_drvdata(dev);
  13386. u8 status = 0;
  13387. u32 gpio_value = 0;
  13388. int rc = 0;
  13389. /* This functionality is only available in the BL */
  13390. if (cd->mode == PT_MODE_BOOTLOADER)
  13391. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13392. else
  13393. rc = -EPERM;
  13394. if (!rc) {
  13395. if (status == 0)
  13396. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13397. "Status: %d\n"
  13398. "DUT GPIO Reg: 0x%08X\n",
  13399. rc, gpio_value);
  13400. else
  13401. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13402. "Status: %d\n"
  13403. "DUT GPIO Reg: n/a\n",
  13404. status);
  13405. } else
  13406. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13407. "Status: %d\n"
  13408. "DUT GPIO Reg: n/a\n",
  13409. rc);
  13410. }
  13411. /*******************************************************************************
  13412. * FUNCTION: pt_pip2_version_show
  13413. *
  13414. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13415. * contents of the response to the passed in output buffer.
  13416. *
  13417. * RETURN: size of data written to sysfs node
  13418. *
  13419. * PARAMETERS:
  13420. * *dev - pointer to device structure
  13421. * *attr - pointer to device attributes structure
  13422. * *buf - pointer to print output buffer
  13423. ******************************************************************************/
  13424. static ssize_t pt_pip2_version_show(struct device *dev,
  13425. struct device_attribute *attr, char *buf)
  13426. {
  13427. int rc = 0;
  13428. struct pt_core_data *cd = dev_get_drvdata(dev);
  13429. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13430. rc = pt_pip2_get_version(cd);
  13431. if (!rc) {
  13432. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13433. "PIP VERSION : %02X.%02X\n"
  13434. "BL VERSION : %02X.%02X\n"
  13435. "FW VERSION : %02X.%02X\n"
  13436. "SILICON ID : %04X.%04X\n"
  13437. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13438. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13439. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13440. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13441. ttdata->chip_id, ttdata->chip_rev,
  13442. ttdata->uid[0], ttdata->uid[1],
  13443. ttdata->uid[2], ttdata->uid[3],
  13444. ttdata->uid[4], ttdata->uid[5],
  13445. ttdata->uid[6], ttdata->uid[7],
  13446. ttdata->uid[8], ttdata->uid[9],
  13447. ttdata->uid[10], ttdata->uid[11]);
  13448. } else {
  13449. pt_debug(dev, DL_ERROR,
  13450. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13451. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13452. "PIP VERSION : -\n"
  13453. "BL VERSION : -\n"
  13454. "FW VERSION : -\n"
  13455. "SILICON ID : -\n"
  13456. "UID : -\n");
  13457. }
  13458. }
  13459. #ifdef TTDL_DIAGNOSTICS
  13460. /*******************************************************************************
  13461. * FUNCTION: pt_ttdl_status_show
  13462. *
  13463. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13464. * variable states and GPIO levels. Additional information printed when
  13465. * TTDL_DIAGNOSTICS is enabled.
  13466. *
  13467. * NOTE: All counters will be reset to 0 when this function is called.
  13468. *
  13469. * RETURN:
  13470. * 0 = success
  13471. * !0 = failure
  13472. *
  13473. * PARAMETERS:
  13474. * *dev - pointer to device structure
  13475. * *attr - pointer to device attributes
  13476. * *buf - pointer to output buffer
  13477. ******************************************************************************/
  13478. static ssize_t pt_ttdl_status_show(struct device *dev,
  13479. struct device_attribute *attr, char *buf)
  13480. {
  13481. struct pt_core_data *cd = dev_get_drvdata(dev);
  13482. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13483. struct i2c_client *client = to_i2c_client(dev);
  13484. ssize_t ret;
  13485. u16 cal_size = 0;
  13486. unsigned short crc = 0;
  13487. if (cd->cal_cache_in_host)
  13488. _pt_manage_local_cal_data(dev,
  13489. PT_CAL_DATA_INFO, &cal_size, &crc);
  13490. ret = scnprintf(buf, strlen(buf),
  13491. "%s: 0x%04X\n"
  13492. "%s: %d\n"
  13493. "%s: %s\n"
  13494. "%s: %s %s\n"
  13495. "%s: %s\n"
  13496. "%s: 0x%02X\n"
  13497. "%s: %s\n"
  13498. "%s: %s\n"
  13499. "%s: %s\n"
  13500. "%s: %s\n"
  13501. "%s: %d\n"
  13502. "%s: %d\n"
  13503. "%s: %s\n"
  13504. "%s: %s\n"
  13505. "%s: %d\n"
  13506. #ifdef TTDL_DIAGNOSTICS
  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: %d\n"
  13516. "%s: %d\n"
  13517. "%s: %s\n"
  13518. "%s: %s\n"
  13519. "%s: %s\n"
  13520. "%s: %d\n"
  13521. "%s: 0x%04X\n"
  13522. "%s: %s\n"
  13523. #endif /* TTDL_DIAGNOSTICS */
  13524. ,
  13525. "Startup Status ", cd->startup_status,
  13526. "TTDL Debug Level ", cd->debug_level,
  13527. "Mode ",
  13528. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13529. "Operational" : "BL") : "Unknown",
  13530. "DUT Generation ",
  13531. cd->active_dut_generation ?
  13532. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13533. "PT TC/TT" : "Gen5/6") : "Unknown",
  13534. cd->active_dut_generation ?
  13535. (cd->set_dut_generation == true ?
  13536. "(Set)" : "(Detected)") : "",
  13537. "HW Detected ",
  13538. cd->hw_detected ? "True" : "False",
  13539. "I2C Address ",
  13540. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13541. "Active Bus Module ",
  13542. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13543. "Flashless Mode ",
  13544. cd->flashless_dut == 1 ? "Yes" : "No",
  13545. "GPIO state - IRQ ",
  13546. cd->cpdata->irq_stat ?
  13547. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13548. "High" : "Low") : "not defined",
  13549. "GPIO state - TP_XRES ",
  13550. pdata->core_pdata->rst_gpio ?
  13551. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13552. "High" : "Low") : "not defined",
  13553. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13554. "Startup Retry Count ", cd->startup_retry_count,
  13555. "WD - Manual Force Stop ",
  13556. cd->watchdog_force_stop ? "True" : "False",
  13557. "WD - Enabled ",
  13558. cd->watchdog_enabled ? "True" : "False",
  13559. "WD - Interval (ms) ", cd->watchdog_interval
  13560. #ifdef TTDL_DIAGNOSTICS
  13561. , "WD - Triggered Count ", cd->watchdog_count,
  13562. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13563. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13564. "WD - XRES Count ", cd->wd_xres_count,
  13565. "IRQ Triggered Count ", cd->irq_count,
  13566. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13567. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13568. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13569. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13570. "Error GPIO trigger type ", cd->err_gpio_type,
  13571. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13572. "Suppress No-Flash Auto BL ",
  13573. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13574. "Yes" : "No",
  13575. "Calibration Cache on host ",
  13576. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13577. "Yes" : "No",
  13578. "Calibration Cache size ", cal_size,
  13579. "Calibration Cache chip ID ", crc,
  13580. "Multi-Chip Support ",
  13581. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13582. #endif /* TTDL_DIAGNOSTICS */
  13583. );
  13584. #ifdef TTDL_DIAGNOSTICS
  13585. /* Reset all diagnostic counters */
  13586. cd->watchdog_count = 0;
  13587. cd->watchdog_irq_stuck_count = 0;
  13588. cd->watchdog_failed_access_count = 0;
  13589. cd->wd_xres_count = 0;
  13590. cd->irq_count = 0;
  13591. cd->bl_retry_packet_count = 0;
  13592. cd->pip2_crc_error_count = 0;
  13593. cd->bus_transmit_error_count = 0;
  13594. #endif
  13595. return ret;
  13596. }
  13597. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13598. /*******************************************************************************
  13599. * FUNCTION: pt_pip2_enter_bl_show
  13600. *
  13601. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13602. * the DUT into the BL and show the success or failure of entering the BL
  13603. *
  13604. * RETURN:
  13605. * 0 = success
  13606. * !0 = failure
  13607. *
  13608. * PARAMETERS:
  13609. * *dev - pointer to device structure
  13610. * *attr - pointer to device attributes
  13611. * *buf - pointer to output buffer
  13612. ******************************************************************************/
  13613. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13614. struct device_attribute *attr, char *buf)
  13615. {
  13616. ssize_t ret = 0;
  13617. int rc = 0;
  13618. int result = 0;
  13619. u8 mode = PT_MODE_UNKNOWN;
  13620. struct pt_core_data *cd = dev_get_drvdata(dev);
  13621. /* Turn off the TTDL WD before enter bootloader */
  13622. pt_stop_wd_timer(cd);
  13623. /* Ensure NO enumeration work is queued or will be queued */
  13624. cancel_work_sync(&cd->enum_work);
  13625. mutex_lock(&cd->system_lock);
  13626. cd->bridge_mode = true;
  13627. mutex_unlock(&cd->system_lock);
  13628. /* set mode to operational to avoid any extra PIP traffic */
  13629. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13630. switch (result) {
  13631. case PT_ENTER_BL_PASS:
  13632. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13633. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13634. break;
  13635. case PT_ENTER_BL_ERROR:
  13636. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13637. rc,
  13638. " Unknown Error");
  13639. break;
  13640. case PT_ENTER_BL_RESET_FAIL:
  13641. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13642. rc,
  13643. " Soft Reset Failed");
  13644. break;
  13645. case PT_ENTER_BL_HID_START_BL_FAIL:
  13646. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13647. rc,
  13648. " PIP Start BL Cmd Failed");
  13649. break;
  13650. case PT_ENTER_BL_CONFIRM_FAIL:
  13651. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13652. rc,
  13653. " Error confirming DUT entered BL");
  13654. break;
  13655. default:
  13656. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13657. rc, " Unknown Error");
  13658. break;
  13659. };
  13660. /* Allow enumeration work to be queued again */
  13661. cd->bridge_mode = false;
  13662. return ret;
  13663. }
  13664. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13665. /*******************************************************************************
  13666. * FUNCTION: pt_pip2_exit_bl_show
  13667. *
  13668. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13669. * launch the APP and put the DUT Application mode
  13670. *
  13671. * RETURN:
  13672. * 0 = success
  13673. * !0 = failure
  13674. *
  13675. * PARAMETERS:
  13676. * *dev - pointer to device structure
  13677. * *attr - pointer to device attributes
  13678. * *buf - pointer to output buffer
  13679. ******************************************************************************/
  13680. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13681. struct device_attribute *attr, char *buf)
  13682. {
  13683. struct pt_core_data *cd = dev_get_drvdata(dev);
  13684. ssize_t ret = 0;
  13685. int rc = 0;
  13686. u8 status_str[PT_STATUS_STR_LEN];
  13687. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13688. /*
  13689. * Perform enum if startup_status doesn't reach to
  13690. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13691. */
  13692. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13693. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13694. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13695. strlcpy(status_str,
  13696. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13697. }
  13698. }
  13699. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13700. rc, status_str);
  13701. return ret;
  13702. }
  13703. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13704. #endif
  13705. #ifdef EASYWAKE_TSG6
  13706. /*******************************************************************************
  13707. * FUNCTION: pt_easy_wakeup_gesture_show
  13708. *
  13709. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13710. * current easy wakeup gesture
  13711. *
  13712. * RETURN:
  13713. * 0 = success
  13714. * !0 = failure
  13715. *
  13716. * PARAMETERS:
  13717. * *dev - pointer to device structure
  13718. * *attr - pointer to device attributes
  13719. * *buf - pointer to output buffer
  13720. ******************************************************************************/
  13721. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13722. struct device_attribute *attr, char *buf)
  13723. {
  13724. struct pt_core_data *cd = dev_get_drvdata(dev);
  13725. ssize_t ret;
  13726. mutex_lock(&cd->system_lock);
  13727. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13728. cd->easy_wakeup_gesture);
  13729. mutex_unlock(&cd->system_lock);
  13730. return ret;
  13731. }
  13732. /*******************************************************************************
  13733. * FUNCTION: pt_easy_wakeup_gesture_store
  13734. *
  13735. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13736. * allows the wake gesture to be set to a custom value.
  13737. *
  13738. * RETURN: Size of passed in buffer is success
  13739. *
  13740. * PARAMETERS:
  13741. * *dev - pointer to device structure
  13742. * *attr - pointer to device attributes
  13743. * *buf - pointer to buffer that hold the command parameters
  13744. * size - size of buf
  13745. ******************************************************************************/
  13746. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13747. struct device_attribute *attr, const char *buf, size_t size)
  13748. {
  13749. struct pt_core_data *cd = dev_get_drvdata(dev);
  13750. u32 input_data[2];
  13751. int length;
  13752. int rc = 0;
  13753. /* Maximum input of one value */
  13754. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13755. ARRAY_SIZE(input_data));
  13756. if (length != 1) {
  13757. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13758. __func__);
  13759. rc = -EINVAL;
  13760. goto exit;
  13761. }
  13762. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13763. __func__, cd->features.easywake);
  13764. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13765. rc = -EINVAL;
  13766. goto exit;
  13767. }
  13768. pm_runtime_get_sync(dev);
  13769. mutex_lock(&cd->system_lock);
  13770. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13771. cd->easy_wakeup_gesture = (u8)input_data[0];
  13772. pt_debug(dev, DL_INFO,
  13773. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13774. __func__, cd->easy_wakeup_gesture);
  13775. } else
  13776. rc = -ENODEV;
  13777. mutex_unlock(&cd->system_lock);
  13778. pm_runtime_put(dev);
  13779. exit:
  13780. if (rc)
  13781. return rc;
  13782. return size;
  13783. }
  13784. /*******************************************************************************
  13785. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13786. *
  13787. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13788. * show the TSG6 easywake gesture ID
  13789. *
  13790. * RETURN:
  13791. * 0 = success
  13792. * !0 = failure
  13793. *
  13794. * PARAMETERS:
  13795. * *dev - pointer to device structure
  13796. * *attr - pointer to device attributes
  13797. * *buf - pointer to output buffer
  13798. ******************************************************************************/
  13799. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13800. struct device_attribute *attr, char *buf)
  13801. {
  13802. struct pt_core_data *cd = dev_get_drvdata(dev);
  13803. ssize_t ret;
  13804. mutex_lock(&cd->system_lock);
  13805. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13806. cd->gesture_id);
  13807. mutex_unlock(&cd->system_lock);
  13808. return ret;
  13809. }
  13810. /*******************************************************************************
  13811. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13812. *
  13813. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13814. * show the TSG6 easywake gesture data in the following format:
  13815. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13816. *
  13817. * RETURN:
  13818. * 0 = success
  13819. * !0 = failure
  13820. *
  13821. * PARAMETERS:
  13822. * *dev - pointer to device structure
  13823. * *attr - pointer to device attributes
  13824. * *buf - pointer to output buffer
  13825. ******************************************************************************/
  13826. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13827. struct device_attribute *attr, char *buf)
  13828. {
  13829. struct pt_core_data *cd = dev_get_drvdata(dev);
  13830. ssize_t ret = 0;
  13831. int i;
  13832. mutex_lock(&cd->system_lock);
  13833. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13834. for (i = 0; i < cd->gesture_data_length; i++)
  13835. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13836. "0x%02X\n", cd->gesture_data[i]);
  13837. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13838. "(%d bytes)\n", cd->gesture_data_length);
  13839. mutex_unlock(&cd->system_lock);
  13840. return ret;
  13841. }
  13842. #endif /* EASYWAKE_TSG6 */
  13843. #ifdef TTDL_DIAGNOSTICS
  13844. /*******************************************************************************
  13845. * FUNCTION: pt_err_gpio_show
  13846. *
  13847. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13848. * setting up the gpio was successful
  13849. *
  13850. * RETURN: Char buffer with printed GPIO creation state
  13851. *
  13852. * PARAMETERS:
  13853. * *dev - pointer to device structure
  13854. * *attr - pointer to device attributes
  13855. * *buf - pointer to output buffer
  13856. ******************************************************************************/
  13857. static ssize_t pt_err_gpio_show(struct device *dev,
  13858. struct device_attribute *attr, char *buf)
  13859. {
  13860. struct pt_core_data *cd = dev_get_drvdata(dev);
  13861. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13862. "Err GPIO (%d) : %s\n"
  13863. "Err GPIO trig type: %d\n",
  13864. cd->err_gpio,
  13865. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13866. "HIGH" : "low") : "not defined"),
  13867. cd->err_gpio_type);
  13868. }
  13869. /*******************************************************************************
  13870. * FUNCTION: pt_err_gpio_store
  13871. *
  13872. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13873. * available host GPIO to be used to trigger when TTDL detects a PIP
  13874. * command/response timeout.
  13875. *
  13876. * RETURN:
  13877. * 0 = success
  13878. * !0 = failure
  13879. *
  13880. * PARAMETERS:
  13881. * *dev - pointer to device structure
  13882. * *attr - pointer to device attributes
  13883. * *buf - pointer to buffer that hold the command parameters
  13884. * size - size of buf
  13885. ******************************************************************************/
  13886. static ssize_t pt_err_gpio_store(struct device *dev,
  13887. struct device_attribute *attr, const char *buf, size_t size)
  13888. {
  13889. struct pt_core_data *cd = dev_get_drvdata(dev);
  13890. unsigned long gpio;
  13891. int rc = 0;
  13892. u32 input_data[3];
  13893. int length;
  13894. u8 err_type;
  13895. input_data[0] = 0;
  13896. input_data[1] = 0;
  13897. /* Maximmum input is two elements */
  13898. length = _pt_ic_parse_input(dev, buf, size,
  13899. input_data, ARRAY_SIZE(input_data));
  13900. if (length < 1 || length > 2) {
  13901. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13902. __func__);
  13903. rc = -EINVAL;
  13904. goto exit;
  13905. }
  13906. gpio = input_data[0];
  13907. err_type = (u8)input_data[1];
  13908. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13909. rc = -EINVAL;
  13910. goto exit;
  13911. }
  13912. mutex_lock(&cd->system_lock);
  13913. gpio_free(gpio);
  13914. rc = gpio_request(gpio, NULL);
  13915. if (rc) {
  13916. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13917. rc = -ENODEV;
  13918. } else {
  13919. cd->err_gpio = gpio;
  13920. cd->err_gpio_type = err_type;
  13921. gpio_direction_output(gpio, 0);
  13922. }
  13923. mutex_unlock(&cd->system_lock);
  13924. exit:
  13925. if (rc)
  13926. return rc;
  13927. return size;
  13928. }
  13929. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13930. pt_err_gpio_store);
  13931. /*******************************************************************************
  13932. * FUNCTION: pt_drv_irq_show
  13933. *
  13934. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13935. * TTDL interrupt is enabled/disabled
  13936. *
  13937. * RETURN:
  13938. * 0 = success
  13939. * !0 = failure
  13940. *
  13941. * PARAMETERS:
  13942. * *dev - pointer to device structure
  13943. * *attr - pointer to device attributes
  13944. * *buf - pointer to output buffer
  13945. ******************************************************************************/
  13946. static ssize_t pt_drv_irq_show(struct device *dev,
  13947. struct device_attribute *attr, char *buf)
  13948. {
  13949. struct pt_core_data *cd = dev_get_drvdata(dev);
  13950. ssize_t ret = 0;
  13951. mutex_lock(&cd->system_lock);
  13952. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13953. if (cd->irq_enabled)
  13954. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13955. "Driver interrupt: ENABLED\n");
  13956. else
  13957. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13958. "Driver interrupt: DISABLED\n");
  13959. mutex_unlock(&cd->system_lock);
  13960. return ret;
  13961. }
  13962. /*******************************************************************************
  13963. * FUNCTION: pt_drv_irq_store
  13964. *
  13965. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13966. * IRQ to be enabled/disabled.
  13967. *
  13968. * RETURN: Size of passed in buffer
  13969. *
  13970. * PARAMETERS:
  13971. * *dev - pointer to device structure
  13972. * *attr - pointer to device attributes
  13973. * *buf - pointer to buffer that hold the command parameters
  13974. * size - size of buf
  13975. ******************************************************************************/
  13976. static ssize_t pt_drv_irq_store(struct device *dev,
  13977. struct device_attribute *attr, const char *buf, size_t size)
  13978. {
  13979. struct pt_core_data *cd = dev_get_drvdata(dev);
  13980. u32 input_data[2];
  13981. int length;
  13982. int rc = 0;
  13983. /* Maximum input of one value */
  13984. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13985. ARRAY_SIZE(input_data));
  13986. if (length != 1) {
  13987. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13988. __func__);
  13989. rc = -EINVAL;
  13990. goto exit;
  13991. }
  13992. mutex_lock(&cd->system_lock);
  13993. switch (input_data[0]) {
  13994. case 0:
  13995. if (cd->irq_enabled) {
  13996. cd->irq_enabled = false;
  13997. /* Disable IRQ has no return value to check */
  13998. disable_irq_nosync(cd->irq);
  13999. pt_debug(dev, DL_INFO,
  14000. "%s: Driver IRQ now disabled\n",
  14001. __func__);
  14002. } else
  14003. pt_debug(dev, DL_INFO,
  14004. "%s: Driver IRQ already disabled\n",
  14005. __func__);
  14006. break;
  14007. case 1:
  14008. if (cd->irq_enabled == false) {
  14009. cd->irq_enabled = true;
  14010. enable_irq(cd->irq);
  14011. pt_debug(dev, DL_INFO,
  14012. "%s: Driver IRQ now enabled\n",
  14013. __func__);
  14014. } else
  14015. pt_debug(dev, DL_INFO,
  14016. "%s: Driver IRQ already enabled\n",
  14017. __func__);
  14018. break;
  14019. default:
  14020. rc = -EINVAL;
  14021. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14022. }
  14023. mutex_unlock(&(cd->system_lock));
  14024. exit:
  14025. if (rc)
  14026. return rc;
  14027. return size;
  14028. }
  14029. /*******************************************************************************
  14030. * FUNCTION: pt_pip2_bin_hdr_show
  14031. *
  14032. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14033. * the bin file header from flash and show each field
  14034. *
  14035. * RETURN:
  14036. * 0 = success
  14037. * !0 = failure
  14038. *
  14039. * PARAMETERS:
  14040. * *dev - pointer to device structure
  14041. * *attr - pointer to device attributes
  14042. * *buf - pointer to output buffer
  14043. ******************************************************************************/
  14044. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14045. struct device_attribute *attr, char *buf)
  14046. {
  14047. ssize_t ret = 0;
  14048. struct pt_bin_file_hdr hdr = {0};
  14049. int rc;
  14050. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14051. ret = scnprintf(buf, strlen(buf),
  14052. "%s: %d\n"
  14053. "%s: %d\n"
  14054. "%s: 0x%04X\n"
  14055. "%s: %d\n"
  14056. "%s: %d\n"
  14057. "%s: %d\n"
  14058. "%s: 0x%08X\n"
  14059. "%s: 0x%04X\n"
  14060. "%s: 0x%04X\n"
  14061. "%s: %d\n"
  14062. "%s: %d\n",
  14063. "Status", rc,
  14064. "Header Length ", hdr.length,
  14065. "TTPID ", hdr.ttpid,
  14066. "FW Major Ver ", hdr.fw_major,
  14067. "FW Minor Ver ", hdr.fw_minor,
  14068. "FW Rev Control ", hdr.fw_rev_ctrl,
  14069. "FW CRC ", hdr.fw_crc,
  14070. "Silicon Rev ", hdr.si_rev,
  14071. "Silicon ID ", hdr.si_id,
  14072. "Config Ver ", hdr.config_ver,
  14073. "HEX File Size ", hdr.hex_file_size
  14074. );
  14075. return ret;
  14076. }
  14077. /*******************************************************************************
  14078. * FUNCTION: pt_platform_data_show
  14079. *
  14080. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14081. * active platform data including: GPIOs, Vendor and Product IDs,
  14082. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14083. * HID registers, and Easy wake gesture
  14084. *
  14085. * RETURN: Size of printed data
  14086. *
  14087. * PARAMETERS:
  14088. * *dev - pointer to device structure
  14089. * *attr - pointer to device attributes
  14090. * *buf - pointer to output buffer
  14091. ******************************************************************************/
  14092. static ssize_t pt_platform_data_show(struct device *dev,
  14093. struct device_attribute *attr, char *buf)
  14094. {
  14095. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14096. struct pt_core_data *cd = dev_get_drvdata(dev);
  14097. ssize_t ret;
  14098. ret = scnprintf(buf, strlen(buf),
  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: %d\n"
  14125. "%s: %d\n"
  14126. "%s: %s\n"
  14127. "%s: %s\n"
  14128. "%s: %d\n",
  14129. "Status", 0,
  14130. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14131. "Interrupt GPIO Value ",
  14132. pdata->core_pdata->irq_gpio ?
  14133. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14134. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14135. "Reset GPIO Value ",
  14136. pdata->core_pdata->rst_gpio ?
  14137. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14138. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14139. "DDI Reset GPIO Value ",
  14140. pdata->core_pdata->ddi_rst_gpio ?
  14141. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14142. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14143. "VDDI GPIO Value ",
  14144. pdata->core_pdata->vddi_gpio ?
  14145. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14146. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14147. "VCC GPIO Value ",
  14148. pdata->core_pdata->vcc_gpio ?
  14149. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14150. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14151. "AVDD GPIO Value ",
  14152. pdata->core_pdata->avdd_gpio ?
  14153. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14154. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14155. "AVEE GPIO Value ",
  14156. pdata->core_pdata->avee_gpio ?
  14157. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14158. "Vendor ID ", pdata->core_pdata->vendor_id,
  14159. "Product ID ", pdata->core_pdata->product_id,
  14160. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14161. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14162. "Core data flags ", pdata->core_pdata->flags,
  14163. "MT data flags ", pdata->mt_pdata->flags,
  14164. "Loader data flags ", pdata->loader_pdata->flags,
  14165. "Level trigger delay (us) ",
  14166. pdata->core_pdata->level_irq_udelay,
  14167. "HID Descriptor Register ",
  14168. pdata->core_pdata->hid_desc_register,
  14169. "HID Output Register ",
  14170. cd->hid_desc.output_register,
  14171. "HID Command Register ",
  14172. cd->hid_desc.command_register,
  14173. "Easy wakeup gesture ",
  14174. pdata->core_pdata->easy_wakeup_gesture,
  14175. "Config DUT generation ",
  14176. pdata->core_pdata->config_dut_generation ?
  14177. (pdata->core_pdata->config_dut_generation ==
  14178. CONFIG_DUT_PIP2_CAPABLE ?
  14179. "PT TC/TT" : "Gen5/6") : "Auto",
  14180. "Watchdog Force Stop ",
  14181. pdata->core_pdata->watchdog_force_stop ?
  14182. "True" : "False",
  14183. "Panel ID Support ",
  14184. pdata->core_pdata->panel_id_support);
  14185. return ret;
  14186. }
  14187. #define PT_ERR_STR_SIZE 64
  14188. /*******************************************************************************
  14189. * FUNCTION: pt_bist_bus_test
  14190. *
  14191. * SUMMARY: Tests the connectivity of the active bus pins:
  14192. * I2C - SDA and SCL
  14193. * SPI - MOSI, MISO, CLK
  14194. *
  14195. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14196. * bus. If any data is read we know the I2C/SPI pins are connected
  14197. *
  14198. * RETURN:
  14199. * 0 = Success
  14200. * !0 = Failure
  14201. *
  14202. * PARAMETERS:
  14203. * *dev - pointer to device structure
  14204. * *net_toggled - pointer to where to store if bus toggled
  14205. * *err_str - pointer to error string buffer
  14206. ******************************************************************************/
  14207. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14208. {
  14209. struct pt_core_data *cd = dev_get_drvdata(dev);
  14210. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14211. u8 *read_buf = NULL;
  14212. int bytes_read = 0;
  14213. int rc = 0;
  14214. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14215. if (read_buf == NULL) {
  14216. rc = -ENOMEM;
  14217. goto exit;
  14218. }
  14219. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14220. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14221. disable_irq(cd->irq);
  14222. mutex_lock(&cd->system_lock);
  14223. cd->irq_disabled = true;
  14224. mutex_unlock(&cd->system_lock);
  14225. /*
  14226. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14227. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14228. */
  14229. usleep_range(5000, 6000);
  14230. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14231. __func__, (int)sizeof(ver_cmd));
  14232. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14233. ">>> User CMD");
  14234. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14235. if (rc) {
  14236. pt_debug(dev, DL_ERROR,
  14237. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14238. *net_toggled = 0;
  14239. strlcpy(err_str,
  14240. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14241. goto exit_enable_irq;
  14242. }
  14243. usleep_range(4000, 5000);
  14244. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14245. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14246. __func__, bytes_read);
  14247. if (bytes_read == 0) {
  14248. *net_toggled = 0;
  14249. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14250. __func__);
  14251. strlcpy(err_str,
  14252. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14253. rc = -EIO;
  14254. goto exit_enable_irq;
  14255. } else {
  14256. if (cd->bus_ops->bustype == BUS_I2C)
  14257. *net_toggled = 1;
  14258. else {
  14259. if ((bytes_read > 3) &&
  14260. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14261. PIP2_CMD_ID_VERSION)
  14262. *net_toggled = 1;
  14263. else {
  14264. *net_toggled = 0;
  14265. pt_debug(dev, DL_INFO,
  14266. "%s: BUS Read Failed, %d bytes read\n",
  14267. __func__, bytes_read);
  14268. strlcpy(err_str,
  14269. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14270. }
  14271. }
  14272. }
  14273. exit_enable_irq:
  14274. enable_irq(cd->irq);
  14275. usleep_range(5000, 6000);
  14276. mutex_lock(&cd->system_lock);
  14277. cd->irq_disabled = false;
  14278. mutex_unlock(&cd->system_lock);
  14279. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14280. exit:
  14281. kfree(read_buf);
  14282. return rc;
  14283. }
  14284. /*******************************************************************************
  14285. * FUNCTION: pt_bist_irq_test
  14286. *
  14287. * SUMMARY: Tests the connectivity of the IRQ net
  14288. *
  14289. * This test will ensure there is a good connection between the host
  14290. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14291. * asserted and if so keep reading messages off of the bus until
  14292. * it de-asserts. Possible outcomes:
  14293. * - IRQ was already de-asserted: Send a PIP command and if an
  14294. * interrupt is generated the test passes.
  14295. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14296. * test passes.
  14297. * - IRQ stays asserted: After reading the bus multiple times
  14298. * the IRQ stays asserted. Likely open or shorted to GND
  14299. *
  14300. * RETURN:
  14301. * 0 = Success
  14302. * !0 = Failure
  14303. *
  14304. * PARAMETERS:
  14305. * *dev - pointer to device structure
  14306. * *bus_toggled - pointer to where to store if bus toggled
  14307. * *irq_toggled - pointer to where to store if IRQ toggled
  14308. * *xres_toggled - pointer to where to store if XRES toggled
  14309. * *err_str - pointer to error string buffer
  14310. ******************************************************************************/
  14311. static int pt_bist_irq_test(struct device *dev,
  14312. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14313. {
  14314. struct pt_core_data *cd = dev_get_drvdata(dev);
  14315. u8 *read_buf = NULL;
  14316. u8 mode = PT_MODE_UNKNOWN;
  14317. u16 actual_read_len;
  14318. int bytes_read = 0;
  14319. int count = 0;
  14320. int rc = 0;
  14321. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14322. if (read_buf == NULL) {
  14323. rc = -ENOMEM;
  14324. goto exit;
  14325. }
  14326. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14327. cd->irq_count = 0;
  14328. /*
  14329. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14330. * the bus based on the 2 byte initial length read. Try up to 5x.
  14331. */
  14332. while (pt_check_irq_asserted(cd) && count < 5) {
  14333. count++;
  14334. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14335. }
  14336. if (count > 1 && count < 5 && bytes_read > 0) {
  14337. /*
  14338. * IRQ was stuck but data was successfully read from the
  14339. * bus releasing the IRQ line.
  14340. */
  14341. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14342. __func__, count, bytes_read);
  14343. *bus_toggled = 1;
  14344. *irq_toggled = 1;
  14345. goto exit;
  14346. }
  14347. if (count == 5 && bytes_read == 0) {
  14348. /*
  14349. * Looped 5x and read nothing off the bus yet the IRQ is still
  14350. * asserted. Possible conditions:
  14351. * - IRQ open circuit
  14352. * - IRQ shorted to GND
  14353. * - I2C/SPI bus is disconnected
  14354. * - FW holding the pin low
  14355. * Try entering the BL to see if communication works there.
  14356. */
  14357. mode = PT_MODE_IGNORE;
  14358. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14359. if (rc) {
  14360. pt_debug(dev, DL_ERROR,
  14361. "%s Failed to enter BL\n", __func__);
  14362. strlcpy(err_str,
  14363. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14364. *irq_toggled = 0;
  14365. goto exit;
  14366. }
  14367. /*
  14368. * If original mode was operational and we successfully
  14369. * entered the BL, then the XRES net must have toggled
  14370. */
  14371. if (mode == PT_MODE_OPERATIONAL)
  14372. *xres_toggled = 1;
  14373. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14374. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14375. &actual_read_len);
  14376. if (rc) {
  14377. /*
  14378. * Could not communicate to DUT in BL mode. Save the
  14379. * error string, slim chance but the XRES test below may
  14380. * show the IRQ is actually working.
  14381. */
  14382. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14383. pt_debug(dev, DL_ERROR,
  14384. "%s: %s, count=%d bytes_read=%d\n",
  14385. __func__, err_str, count, bytes_read);
  14386. *irq_toggled = 0;
  14387. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14388. goto exit;
  14389. } else {
  14390. *bus_toggled = 1;
  14391. *irq_toggled = 1;
  14392. goto exit;
  14393. }
  14394. }
  14395. if (pt_check_irq_asserted(cd)) {
  14396. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14397. rc = -EIO;
  14398. *irq_toggled = 0;
  14399. goto exit;
  14400. }
  14401. /* Try sending a PIP command to see if we get a response */
  14402. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14403. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14404. if (rc) {
  14405. /*
  14406. * Potential IRQ issue, no communication in App mode, attempt
  14407. * the same command in the BL
  14408. */
  14409. mode = PT_MODE_IGNORE;
  14410. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14411. if (rc) {
  14412. pt_debug(dev, DL_ERROR,
  14413. "%s Failed to enter BL\n", __func__);
  14414. *irq_toggled = 0;
  14415. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14416. goto exit;
  14417. }
  14418. /*
  14419. * If original mode was operational and we successfully
  14420. * entered the BL, this will be useful info for the tp_xres
  14421. * test below.
  14422. */
  14423. if (mode == PT_MODE_OPERATIONAL)
  14424. *xres_toggled = 1;
  14425. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14426. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14427. &actual_read_len);
  14428. if (rc) {
  14429. /*
  14430. * Could not communicate in FW mode or BL mode. Save the
  14431. * error string, slim chance but the XRES test below may
  14432. * show the IRQ is actually working.
  14433. */
  14434. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14435. pt_debug(dev, DL_ERROR,
  14436. "%s: request_active_pip_prot failed\n",
  14437. __func__);
  14438. *irq_toggled = 0;
  14439. goto exit;
  14440. }
  14441. }
  14442. if (cd->irq_count > 0) {
  14443. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14444. cd->irq_count);
  14445. *bus_toggled = 1;
  14446. *irq_toggled = 1;
  14447. goto exit;
  14448. }
  14449. exit:
  14450. kfree(read_buf);
  14451. return rc;
  14452. }
  14453. /*******************************************************************************
  14454. * FUNCTION: pt_bist_xres_test
  14455. *
  14456. * SUMMARY: Tests the connectivity of the TP_XRES net
  14457. *
  14458. * This test will ensure there is a good connection between the host
  14459. * and the DUT on the tp_xres pin. The pin will be toggled to
  14460. * generate a TP reset which will cause the DUT to output a reset
  14461. * sentinel. If the reset sentinel is seen the test passes. If it is
  14462. * not seen the test will attempt to send a soft reset to simply gain
  14463. * some additional information on the failure:
  14464. * - soft reset fails to send: XRES and IRQ likely open
  14465. * - soft reset passes: XRES likely open or stuck de-asserted
  14466. * - soft reset fails: XRES likely stuck asserted
  14467. *
  14468. * RETURN:
  14469. * 0 = Success
  14470. * !0 = Failure
  14471. *
  14472. * PARAMETERS:
  14473. * *dev - pointer to device structure
  14474. * *bus_toggled - pointer to where to store if bus toggled
  14475. * *irq_toggled - pointer to where to store if IRQ toggled
  14476. * *xres_toggled - pointer to where to store if XRES toggled
  14477. * *err_str - pointer to error string buffer
  14478. ******************************************************************************/
  14479. static int pt_bist_xres_test(struct device *dev,
  14480. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14481. {
  14482. struct pt_core_data *cd = dev_get_drvdata(dev);
  14483. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14484. u8 *read_buf = NULL;
  14485. u8 mode = PT_MODE_UNKNOWN;
  14486. int rc = 0;
  14487. int t = 0;
  14488. int timeout = 300;
  14489. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14490. if (read_buf == NULL) {
  14491. rc = -ENOMEM;
  14492. goto exit;
  14493. }
  14494. /* Clear the startup bit mask, reset and enum will re-populate it */
  14495. cd->startup_status = STARTUP_STATUS_START;
  14496. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14497. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14498. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14499. rc = -ENODEV;
  14500. goto exit;
  14501. }
  14502. /* Ensure we have nothing pending on active bus */
  14503. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14504. /* Perform a hard XRES toggle and wait for reset sentinel */
  14505. mutex_lock(&cd->system_lock);
  14506. cd->hid_reset_cmd_state = 1;
  14507. mutex_unlock(&cd->system_lock);
  14508. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14509. rc = pt_hw_hard_reset(cd);
  14510. /* Set timeout to 1s for the flashless case where a BL could be done */
  14511. if (cd->flashless_dut)
  14512. timeout = 1000;
  14513. /*
  14514. * To avoid the case that next PIP command can be confused by BL/FW
  14515. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14516. * followed by "wakeup event" function can lower the failure rate.
  14517. */
  14518. t = wait_event_timeout(cd->wait_q,
  14519. ((cd->startup_status != STARTUP_STATUS_START)
  14520. && (cd->hid_reset_cmd_state == 0)),
  14521. msecs_to_jiffies(timeout));
  14522. if (IS_TMO(t)) {
  14523. pt_debug(cd->dev, DL_ERROR,
  14524. "%s: TMO waiting for sentinel\n", __func__);
  14525. *xres_toggled = 0;
  14526. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14527. /*
  14528. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14529. * To enter the BL we need to generate an XRES so first try to
  14530. * launch the applicaiton
  14531. */
  14532. if (cd->mode == PT_MODE_BOOTLOADER)
  14533. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14534. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14535. __func__);
  14536. mode = PT_MODE_IGNORE;
  14537. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14538. if (rc) {
  14539. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14540. __func__);
  14541. *xres_toggled = 0;
  14542. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14543. goto exit;
  14544. } else {
  14545. /* Wait for the BL sentinel */
  14546. t = wait_event_timeout(cd->wait_q,
  14547. (cd->startup_status != STARTUP_STATUS_START),
  14548. msecs_to_jiffies(500));
  14549. if (IS_TMO(t)) {
  14550. pt_debug(cd->dev, DL_ERROR,
  14551. "%s: TMO waiting for BL sentinel\n",
  14552. __func__);
  14553. *xres_toggled = 0;
  14554. strlcpy(err_str,
  14555. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14556. rc = -ETIME;
  14557. goto exit;
  14558. }
  14559. }
  14560. }
  14561. mutex_lock(&cd->system_lock);
  14562. cd->hid_reset_cmd_state = 0;
  14563. mutex_unlock(&cd->system_lock);
  14564. /* Look for BL or FW reset sentinels */
  14565. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14566. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14567. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14568. /* If a sentinel was seen, all nets are working */
  14569. *xres_toggled = 1;
  14570. *irq_toggled = 1;
  14571. /*
  14572. * For SPI test, bus read result can be confused as FW sentinel
  14573. * if MISO(slave) is connected to MISO(host).
  14574. */
  14575. if (cd->bus_ops->bustype == BUS_I2C)
  14576. *bus_toggled = 1;
  14577. } else {
  14578. /*
  14579. * Hard reset failed, however some additional information
  14580. * could be determined. Try a soft reset to see if DUT resets
  14581. * with the possible outcomes:
  14582. * - if it resets the line is not stuck asserted
  14583. * - if it does not reset the line could be stuck asserted
  14584. */
  14585. *xres_toggled = 0;
  14586. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14587. msleep(30);
  14588. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14589. __func__, rc);
  14590. if (rc) {
  14591. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14592. pt_debug(dev, DL_ERROR,
  14593. "%s: Hard reset failed, soft reset failed %s\n",
  14594. __func__, err_str);
  14595. goto exit;
  14596. }
  14597. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14598. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14599. strlcpy(err_str,
  14600. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14601. pt_debug(dev, DL_ERROR,
  14602. "%s: Hard reset failed, soft reset passed-%s\n",
  14603. __func__, err_str);
  14604. } else if (cd->startup_status == 0) {
  14605. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14606. pt_debug(dev, DL_ERROR,
  14607. "%s: Hard reset failed, soft reset failed-%s\n",
  14608. __func__, err_str);
  14609. } else {
  14610. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14611. pt_debug(dev, DL_ERROR,
  14612. "%s: Hard and Soft reset failed - %s\n",
  14613. __func__, err_str);
  14614. }
  14615. }
  14616. exit:
  14617. kfree(read_buf);
  14618. return rc;
  14619. }
  14620. /*******************************************************************************
  14621. * FUNCTION: pt_bist_slave_irq_test
  14622. *
  14623. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14624. *
  14625. * This test will ensure there is a good IRQ connection between the master
  14626. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14627. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14628. * saw each slave trigger the IRQ with it's reset sentinel.
  14629. *
  14630. * RETURN:
  14631. * 0 = Success
  14632. * !0 = Failure
  14633. *
  14634. * PARAMETERS:
  14635. * *dev - pointer to device structure
  14636. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14637. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14638. * *err_str - pointer to error string buffer
  14639. * *slave_detect - pointer to slave detect buffer
  14640. * *boot_err - pointer to boot_err buffer
  14641. ******************************************************************************/
  14642. static int pt_bist_slave_irq_test(struct device *dev,
  14643. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14644. u8 *slave_detect, u8 *boot_err)
  14645. {
  14646. struct pt_core_data *cd = dev_get_drvdata(dev);
  14647. u8 mode = PT_MODE_UNKNOWN;
  14648. u8 status;
  14649. u8 boot;
  14650. u8 read_buf[12];
  14651. u8 detected = 0;
  14652. u8 last_err = -1;
  14653. u16 actual_read_len;
  14654. int result = 0;
  14655. int rc = 0;
  14656. /*
  14657. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14658. * detect bits. If the DUT was in FW, entering the BL will cause an
  14659. * XRES signal which will inadvertently test the XRES net as well
  14660. */
  14661. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14662. if (rc) {
  14663. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14664. __func__, rc);
  14665. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14666. goto exit;
  14667. }
  14668. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14669. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14670. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14671. &actual_read_len);
  14672. if (!rc) {
  14673. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14674. "PIP2 STATUS");
  14675. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14676. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14677. /* Slave detect is only valid if status ok and in boot exec */
  14678. if (status == PIP2_RSP_ERR_NONE &&
  14679. boot == PIP2_STATUS_BOOT_EXEC) {
  14680. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14681. SLAVE_DETECT_MASK;
  14682. } else {
  14683. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14684. rc = -EPERM;
  14685. }
  14686. } else {
  14687. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14688. __func__);
  14689. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14690. goto exit;
  14691. }
  14692. /*
  14693. * Retrieve boot error regardless of the state of the slave detect
  14694. * bit because the IRQ could have been stuck high or low.
  14695. */
  14696. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14697. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14698. read_buf, &actual_read_len);
  14699. if (!rc) {
  14700. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14701. "PIP2 GET_LAST_ERRNO");
  14702. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14703. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14704. if (last_err) {
  14705. pt_debug(cd->dev, DL_ERROR,
  14706. "%s: Master Boot Last Err = 0x%02X\n",
  14707. __func__, last_err);
  14708. }
  14709. } else {
  14710. pt_debug(cd->dev, DL_ERROR,
  14711. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14712. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14713. }
  14714. exit:
  14715. pt_debug(cd->dev, DL_INFO,
  14716. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14717. __func__, rc, detected, last_err);
  14718. if (err_str && last_err) {
  14719. if (detected)
  14720. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14721. "- Likely stuck low. Boot Error:",
  14722. last_err);
  14723. else
  14724. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14725. "- Likely stuck high. Boot Error:",
  14726. last_err);
  14727. }
  14728. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14729. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14730. last_err = PIP2_RSP_ERR_NONE;
  14731. if (slave_irq_toggled)
  14732. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14733. if (slave_bus_toggled) {
  14734. /* Leave as UNTEST if slave not detected */
  14735. if (detected)
  14736. *slave_bus_toggled = !last_err ? true : false;
  14737. }
  14738. if (slave_detect)
  14739. *slave_detect = detected;
  14740. if (boot_err)
  14741. *boot_err = last_err;
  14742. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14743. __func__,
  14744. "Detected", detected,
  14745. "slave_irq_toggled", *slave_irq_toggled,
  14746. "slave_bus_toggled", *slave_bus_toggled);
  14747. return rc;
  14748. }
  14749. /*******************************************************************************
  14750. * FUNCTION: pt_bist_slave_xres_test
  14751. *
  14752. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14753. *
  14754. * This test will ensure there is a good TP_XRES connection between the
  14755. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14756. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14757. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14758. * it's reset sentinel.
  14759. *
  14760. * RETURN:
  14761. * 0 = Success
  14762. * !0 = Failure
  14763. *
  14764. * PARAMETERS:
  14765. * *dev - pointer to device structure
  14766. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14767. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14768. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14769. * *err_str - pointer to error string buffer
  14770. ******************************************************************************/
  14771. static int pt_bist_slave_xres_test(struct device *dev,
  14772. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14773. u8 *err_str)
  14774. {
  14775. struct pt_core_data *cd = dev_get_drvdata(dev);
  14776. u8 slave_detect = 0;
  14777. u8 boot_err = 0;
  14778. int rc = 0;
  14779. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14780. mutex_lock(&cd->system_lock);
  14781. cd->hid_reset_cmd_state = 1;
  14782. mutex_unlock(&cd->system_lock);
  14783. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14784. pt_hw_hard_reset(cd);
  14785. msleep(100);
  14786. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14787. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14788. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14789. if (!rc && *slave_irq_toggled == false) {
  14790. /*
  14791. * If the slave IRQ did not toggle, either the slave_detect
  14792. * bit was not set or we had a boot error. If the slave
  14793. * detect was not set the slave did not reset causing a boot
  14794. * error.
  14795. */
  14796. if (!slave_detect)
  14797. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14798. else
  14799. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14800. "- likely open or an IRQ issue. Boot Error:",
  14801. boot_err);
  14802. }
  14803. if (slave_xres_toggled) {
  14804. if (!rc)
  14805. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14806. else
  14807. *slave_xres_toggled = false;
  14808. }
  14809. return rc;
  14810. }
  14811. /*******************************************************************************
  14812. * FUNCTION: pt_bist_slave_bus_test
  14813. *
  14814. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14815. *
  14816. * This test will ensure a good SPI bus connection between the
  14817. * master DUT and the slave DUT(s). This bus connection is ensured by
  14818. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14819. * then there is a master/slave communication issue. Opening file 0 on
  14820. * the master will open it on the slave as well if the slave was detected.
  14821. *
  14822. * RETURN:
  14823. * 0 = Success
  14824. * !0 = Failure
  14825. *
  14826. * PARAMETERS:
  14827. * *dev - pointer to device structure
  14828. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14829. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14830. * *err_str - pointer to error string buffer
  14831. ******************************************************************************/
  14832. static int pt_bist_slave_bus_test(struct device *dev,
  14833. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14834. {
  14835. struct pt_core_data *cd = dev_get_drvdata(dev);
  14836. u8 mode = PT_MODE_UNKNOWN;
  14837. u8 bus_toggled = false;
  14838. u8 file_handle;
  14839. int result = 0;
  14840. int rc = 0;
  14841. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14842. if (rc) {
  14843. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14844. __func__, rc);
  14845. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14846. goto exit;
  14847. }
  14848. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14849. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14850. if (file_handle != PIP2_RAM_FILE) {
  14851. rc = -ENOENT;
  14852. bus_toggled = false;
  14853. pt_debug(dev, DL_ERROR,
  14854. "%s Failed to open bin file\n", __func__);
  14855. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14856. goto exit;
  14857. } else {
  14858. bus_toggled = true;
  14859. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14860. pt_debug(dev, DL_ERROR,
  14861. "%s: File Close failed, file_handle=%d\n",
  14862. __func__, file_handle);
  14863. }
  14864. }
  14865. exit:
  14866. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14867. if (slave_irq_toggled)
  14868. *slave_irq_toggled = bus_toggled;
  14869. if (slave_bus_toggled)
  14870. *slave_bus_toggled = bus_toggled;
  14871. return rc;
  14872. }
  14873. /*******************************************************************************
  14874. * FUNCTION: pt_ttdl_bist_show
  14875. *
  14876. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14877. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14878. *
  14879. * NOTE: This function will reset the DUT and the startup_status bit
  14880. * mask. A pt_enum will be queued after completion.
  14881. *
  14882. * NOTE: The order of the net tests is done to optimize the time it takes
  14883. * to run. The first test is capable of verifying all nets, each subsequent
  14884. * test is only run if the previous was not able to see all nets toggle.
  14885. *
  14886. * RETURN:
  14887. * 0 = success
  14888. * !0 = failure
  14889. *
  14890. * PARAMETERS:
  14891. * *dev - pointer to device structure
  14892. * *attr - pointer to device attributes
  14893. * *buf - pointer to output buffer
  14894. ******************************************************************************/
  14895. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14896. struct device_attribute *attr, char *buf)
  14897. {
  14898. struct pt_core_data *cd = dev_get_drvdata(dev);
  14899. ssize_t ret;
  14900. char *bus_err_str = NULL;
  14901. char *irq_err_str = NULL;
  14902. char *xres_err_str = NULL;
  14903. char *slave_bus_err_str = NULL;
  14904. char *slave_irq_err_str = NULL;
  14905. char *slave_xres_err_str = NULL;
  14906. u8 tests;
  14907. int rc = 0;
  14908. int num_tests = 0;
  14909. int status = 1; /* 0 = Pass, !0 = fail */
  14910. u8 bus_toggled = 0x0F; /* default to untested */
  14911. u8 i2c_toggled = 0x0F; /* default to untested */
  14912. u8 spi_toggled = 0x0F; /* default to untested */
  14913. u8 irq_toggled = 0x0F; /* default to untested */
  14914. u8 xres_toggled = 0x0F; /* default to untested */
  14915. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14916. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14917. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14918. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14919. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14920. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14921. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14922. goto print_results;
  14923. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14924. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14925. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14926. if (cd->multi_chip) {
  14927. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14928. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14929. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14930. if (!slave_bus_err_str ||
  14931. !slave_irq_err_str ||
  14932. !slave_xres_err_str)
  14933. goto print_results;
  14934. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14935. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14936. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14937. }
  14938. /* Turn off the TTDL WD during the test */
  14939. pt_stop_wd_timer(cd);
  14940. /* Shorten default PIP cmd timeout while running BIST */
  14941. cd->pip_cmd_timeout = 200;
  14942. /* Count the number of tests to run */
  14943. tests = cd->ttdl_bist_select;
  14944. while (tests) {
  14945. num_tests += tests & 1;
  14946. tests >>= 1;
  14947. }
  14948. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14949. __func__, cd->ttdl_bist_select, num_tests);
  14950. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14951. if (cd->flashless_dut) {
  14952. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14953. __func__);
  14954. mutex_lock(&cd->system_lock);
  14955. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14956. mutex_unlock(&cd->system_lock);
  14957. }
  14958. /* --------------- TP_XRES BIST TEST --------------- */
  14959. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14960. pt_debug(dev, DL_INFO,
  14961. "%s: ----- Start TP_XRES BIST -----", __func__);
  14962. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14963. &xres_toggled, xres_err_str);
  14964. /* Done if the rest of all nets toggled */
  14965. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14966. goto host_nets_complete;
  14967. }
  14968. /* Flush bus in case a PIP response is waiting from previous test */
  14969. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14970. /* --------------- IRQ BIST TEST --------------- */
  14971. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14972. pt_debug(dev, DL_INFO,
  14973. "%s: ----- Start IRQ BIST -----", __func__);
  14974. bus_toggled = 0xFF;
  14975. irq_toggled = 0xFF;
  14976. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14977. &xres_toggled, irq_err_str);
  14978. /* If this net failed clear results from previous net */
  14979. if (irq_toggled != 1) {
  14980. xres_toggled = 0x0F;
  14981. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14982. }
  14983. if (bus_toggled == 1 && irq_toggled == 1)
  14984. goto host_nets_complete;
  14985. }
  14986. /* Flush bus in case a PIP response is waiting from previous test */
  14987. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14988. /* --------------- BUS BIST TEST --------------- */
  14989. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14990. pt_debug(dev, DL_INFO,
  14991. "%s: ----- Start BUS BIST -----", __func__);
  14992. bus_toggled = 0xFF;
  14993. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14994. /* If this net failed clear results from previous net */
  14995. if (bus_toggled == 0) {
  14996. irq_toggled = 0x0F;
  14997. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14998. }
  14999. }
  15000. host_nets_complete:
  15001. /* --------------- SLAVE XRES BIST TEST --------------- */
  15002. if (cd->multi_chip &&
  15003. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15004. pt_debug(dev, DL_INFO,
  15005. "%s: ----- Start Slave XRES BIST -----", __func__);
  15006. slave_xres_toggled = 0xFF;
  15007. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15008. &slave_bus_toggled, &slave_xres_toggled,
  15009. slave_xres_err_str);
  15010. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15011. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15012. goto print_results;
  15013. }
  15014. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15015. if (cd->multi_chip &&
  15016. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15017. pt_debug(dev, DL_INFO,
  15018. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15019. slave_irq_toggled = 0xFF;
  15020. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15021. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15022. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15023. __func__, slave_irq_toggled);
  15024. if (slave_irq_toggled == 1) {
  15025. slave_bus_toggled = 1;
  15026. goto print_results;
  15027. }
  15028. }
  15029. /* --------------- SLAVE BUS BIST TEST --------------- */
  15030. if (cd->multi_chip &&
  15031. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15032. pt_debug(dev, DL_INFO,
  15033. "%s: ----- Start Slave BUS BIST -----", __func__);
  15034. slave_bus_toggled = 0xFF;
  15035. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15036. &slave_bus_toggled, slave_bus_err_str);
  15037. }
  15038. print_results:
  15039. /* Restore PIP command timeout */
  15040. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15041. /*
  15042. * We're done! - Perform a hard XRES toggle, allowing BL
  15043. * to load FW if there is any in Flash
  15044. */
  15045. mutex_lock(&cd->system_lock);
  15046. cd->hid_reset_cmd_state = 0;
  15047. mutex_unlock(&cd->system_lock);
  15048. pt_debug(dev, DL_INFO,
  15049. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15050. if (cd->flashless_dut) {
  15051. /*
  15052. * For Flashless solution, FW update is triggered after BL is
  15053. * seen that several miliseconds delay is needed.
  15054. */
  15055. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15056. __func__);
  15057. mutex_lock(&cd->system_lock);
  15058. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15059. mutex_unlock(&cd->system_lock);
  15060. /* Reset DUT and wait 100ms to see if loader started */
  15061. pt_hw_hard_reset(cd);
  15062. msleep(100);
  15063. if (cd->fw_updating) {
  15064. pt_debug(dev, DL_INFO,
  15065. "%s: ----- BIST Wait FW Loading ----",
  15066. __func__);
  15067. rc = _pt_request_wait_for_enum_state(
  15068. dev, 4000, STARTUP_STATUS_COMPLETE);
  15069. }
  15070. } else {
  15071. if (cd->mode == PT_MODE_BOOTLOADER) {
  15072. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15073. if (rc) {
  15074. pt_debug(dev, DL_ERROR,
  15075. "%s Failed to launch app\n", __func__);
  15076. rc = pt_hw_hard_reset(cd);
  15077. }
  15078. }
  15079. /*
  15080. * If FW exists the BL may have just started or will start soon,
  15081. * so the FW sentinel may be on it's way but with no FW it will
  15082. * not arrive, wait for it before deciding if we need to queue
  15083. * an enum.
  15084. */
  15085. rc = _pt_request_wait_for_enum_state(
  15086. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15087. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15088. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15089. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15090. __func__);
  15091. pt_queue_enum(cd);
  15092. rc = _pt_request_wait_for_enum_state(
  15093. dev, 2000, STARTUP_STATUS_COMPLETE);
  15094. }
  15095. }
  15096. msleep(20);
  15097. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15098. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15099. pt_start_wd_timer(cd);
  15100. /* Canned print if any memory allocation issues */
  15101. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15102. ret = scnprintf(buf, strlen(buf),
  15103. "Status: %d\n"
  15104. "I2C (SDA,SCL): [UNTEST]\n"
  15105. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15106. "IRQ connection: [UNTEST]\n"
  15107. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15108. if (cd->multi_chip) {
  15109. ret += scnprintf(buf + ret, strlen(buf),
  15110. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15111. "I/P IRQ connection: [UNTEST]\n"
  15112. "I/P TP_XRES connection: [UNTEST]\n");
  15113. }
  15114. } else {
  15115. status = 0;
  15116. if (bus_toggled == 1)
  15117. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15118. if (irq_toggled == 1)
  15119. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15120. if (xres_toggled == 1)
  15121. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15122. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15123. status += bus_toggled;
  15124. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15125. status += irq_toggled;
  15126. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15127. status += xres_toggled;
  15128. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15129. __func__, status, bus_toggled, irq_toggled,
  15130. xres_toggled);
  15131. if (cd->multi_chip) {
  15132. if (slave_irq_toggled == 1)
  15133. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15134. if (slave_xres_toggled == 1)
  15135. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15136. if (slave_bus_toggled == 1)
  15137. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15138. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15139. status += slave_bus_toggled;
  15140. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15141. status += slave_irq_toggled;
  15142. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15143. status += slave_xres_toggled;
  15144. pt_debug(dev, DL_ERROR,
  15145. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15146. __func__, status, bus_toggled, irq_toggled,
  15147. xres_toggled, slave_bus_toggled,
  15148. slave_irq_toggled, slave_xres_toggled);
  15149. }
  15150. if (cd->bus_ops->bustype == BUS_I2C)
  15151. i2c_toggled = bus_toggled;
  15152. else
  15153. spi_toggled = bus_toggled;
  15154. ret = scnprintf(buf, strlen(buf),
  15155. "Status: %d\n"
  15156. "I2C (SDA,SCL): %s %s\n"
  15157. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15158. "IRQ connection: %s %s\n"
  15159. "TP_XRES connection: %s %s\n",
  15160. status == num_tests ? 0 : 1,
  15161. i2c_toggled == 0x0F ? "[UNTEST]" :
  15162. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15163. i2c_toggled == 0x0F ? "" : bus_err_str,
  15164. spi_toggled == 0x0F ? "[UNTEST]" :
  15165. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15166. spi_toggled == 0x0F ? "" : bus_err_str,
  15167. irq_toggled == 0x0F ? "[UNTEST]" :
  15168. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15169. irq_err_str,
  15170. xres_toggled == 0x0F ? "[UNTEST]" :
  15171. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15172. xres_err_str);
  15173. if (cd->multi_chip) {
  15174. ret += scnprintf(buf + ret, strlen(buf),
  15175. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15176. "I/P IRQ connection: %s %s\n"
  15177. "I/P TP_XRES connection: %s %s\n",
  15178. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15179. slave_bus_toggled == 1 ? "[ OK ]" :
  15180. "[FAILED]", slave_bus_err_str,
  15181. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15182. slave_irq_toggled == 1 ? "[ OK ]" :
  15183. "[FAILED]", slave_irq_err_str,
  15184. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15185. slave_xres_toggled == 1 ? "[ OK ]" :
  15186. "[FAILED]", slave_xres_err_str);
  15187. }
  15188. }
  15189. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15190. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15191. __func__, cd->startup_status);
  15192. kfree(bus_err_str);
  15193. kfree(irq_err_str);
  15194. kfree(xres_err_str);
  15195. return ret;
  15196. }
  15197. /*******************************************************************************
  15198. * FUNCTION: pt_ttdl_bist_store
  15199. *
  15200. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15201. *
  15202. * RETURN: Size of passed in buffer if successful
  15203. *
  15204. * PARAMETERS:
  15205. * *dev - pointer to device structure
  15206. * *attr - pointer to device attributes
  15207. * *buf - pointer to command buffer
  15208. * size - size of buf
  15209. ******************************************************************************/
  15210. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15211. struct device_attribute *attr, const char *buf, size_t size)
  15212. {
  15213. struct pt_core_data *cd = dev_get_drvdata(dev);
  15214. u32 input_data[2] = {0};
  15215. int length;
  15216. int rc = 0;
  15217. /* Maximum input of one value */
  15218. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15219. ARRAY_SIZE(input_data));
  15220. if (length != 1) {
  15221. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15222. __func__);
  15223. rc = -EINVAL;
  15224. goto exit;
  15225. } else {
  15226. mutex_lock(&cd->system_lock);
  15227. cd->ttdl_bist_select = input_data[0];
  15228. mutex_unlock(&cd->system_lock);
  15229. }
  15230. exit:
  15231. if (rc)
  15232. return rc;
  15233. return size;
  15234. }
  15235. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15236. pt_ttdl_bist_store);
  15237. /*******************************************************************************
  15238. * FUNCTION: pt_flush_bus_store
  15239. *
  15240. * SUMMARY: Store method for the flush_bus sysfs node.
  15241. *
  15242. * RETURN: Size of passed in buffer if successful
  15243. *
  15244. * PARAMETERS:
  15245. * *dev - pointer to device structure
  15246. * *attr - pointer to device attributes
  15247. * *buf - pointer to command buffer
  15248. * size - size of buf
  15249. ******************************************************************************/
  15250. static ssize_t pt_flush_bus_store(struct device *dev,
  15251. struct device_attribute *attr, const char *buf, size_t size)
  15252. {
  15253. struct pt_core_data *cd = dev_get_drvdata(dev);
  15254. u32 input_data[2] = {0};
  15255. int length;
  15256. int rc = 0;
  15257. /* Maximum input of one value */
  15258. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15259. ARRAY_SIZE(input_data));
  15260. if (length != 1) {
  15261. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15262. __func__);
  15263. rc = -EINVAL;
  15264. goto exit;
  15265. }
  15266. mutex_lock(&cd->system_lock);
  15267. if (input_data[0] == 0)
  15268. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15269. else if (input_data[0] == 1)
  15270. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15271. else
  15272. rc = -EINVAL;
  15273. mutex_unlock(&cd->system_lock);
  15274. exit:
  15275. if (rc)
  15276. return rc;
  15277. return size;
  15278. }
  15279. /*******************************************************************************
  15280. * FUNCTION: pt_flush_bus_show
  15281. *
  15282. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15283. * based on either the size of the first two bytes or a blind 256 bytes.
  15284. *
  15285. * RETURN:
  15286. * 0 = success
  15287. * !0 = failure
  15288. *
  15289. * PARAMETERS:
  15290. * *dev - pointer to device structure
  15291. * *attr - pointer to device attributes
  15292. * *buf - pointer to output buffer
  15293. ******************************************************************************/
  15294. static ssize_t pt_flush_bus_show(struct device *dev,
  15295. struct device_attribute *attr, char *buf)
  15296. {
  15297. ssize_t ret = 0;
  15298. ssize_t bytes = 0;
  15299. struct pt_core_data *cd = dev_get_drvdata(dev);
  15300. mutex_lock(&cd->system_lock);
  15301. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15302. ret = scnprintf(buf, strlen(buf),
  15303. "Status: 0\n"
  15304. "%s: %zd\n",
  15305. "Bytes flushed", bytes);
  15306. mutex_unlock(&cd->system_lock);
  15307. return ret;
  15308. }
  15309. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15310. pt_flush_bus_store);
  15311. /*******************************************************************************
  15312. * FUNCTION: pt_pip2_ping_test_store
  15313. *
  15314. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15315. *
  15316. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15317. * someone may want to test sending invalid packet lengths so any values
  15318. * up to 255 are allowed.
  15319. *
  15320. * RETURN: Size of passed in buffer if successful
  15321. *
  15322. * PARAMETERS:
  15323. * *dev - pointer to device structure
  15324. * *attr - pointer to device attributes
  15325. * *buf - pointer to command buffer
  15326. * size - size of buf
  15327. ******************************************************************************/
  15328. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15329. struct device_attribute *attr, const char *buf, size_t size)
  15330. {
  15331. struct pt_core_data *cd = dev_get_drvdata(dev);
  15332. u32 input_data[2];
  15333. int length;
  15334. int rc = 0;
  15335. /* Maximum input of one value */
  15336. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15337. ARRAY_SIZE(input_data));
  15338. if (length != 1) {
  15339. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15340. __func__);
  15341. rc = -EINVAL;
  15342. goto exit;
  15343. }
  15344. mutex_lock(&cd->system_lock);
  15345. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15346. cd->ping_test_size = input_data[0];
  15347. else
  15348. rc = -EINVAL;
  15349. mutex_unlock(&cd->system_lock);
  15350. exit:
  15351. if (rc)
  15352. return rc;
  15353. return size;
  15354. }
  15355. /*******************************************************************************
  15356. * FUNCTION: pt_ping_test_show
  15357. *
  15358. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15359. * PING command and ramps up the optional payload from 0 to
  15360. * ping_test_size.
  15361. * The max payload size is 247:
  15362. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15363. *
  15364. * RETURN:
  15365. * 0 = success
  15366. * !0 = failure
  15367. *
  15368. * PARAMETERS:
  15369. * *dev - pointer to device structure
  15370. * *attr - pointer to device attributes
  15371. * *buf - pointer to output buffer
  15372. ******************************************************************************/
  15373. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15374. struct device_attribute *attr, char *buf)
  15375. {
  15376. struct pt_core_data *cd = dev_get_drvdata(dev);
  15377. ssize_t ret;
  15378. int last_packet_size;
  15379. int rc = 0;
  15380. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15381. if (rc) {
  15382. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15383. return ret;
  15384. }
  15385. ret = scnprintf(buf, strlen(buf),
  15386. "Status: %d\n"
  15387. "PING payload test passed with packet sizes 0 - %d\n",
  15388. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15389. last_packet_size);
  15390. return ret;
  15391. }
  15392. /*******************************************************************************
  15393. * FUNCTION: pt_t_refresh_store
  15394. *
  15395. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15396. * in integer as the number of interrupts to count. A timer is started to
  15397. * calculate the total time it takes to see that number of interrupts.
  15398. *
  15399. * RETURN: Size of passed in buffer if successful
  15400. *
  15401. * PARAMETERS:
  15402. * *dev - pointer to device structure
  15403. * *attr - pointer to device attributes
  15404. * *buf - pointer to buffer that hold the command parameters
  15405. * size - size of buf
  15406. ******************************************************************************/
  15407. static ssize_t pt_t_refresh_store(struct device *dev,
  15408. struct device_attribute *attr, const char *buf, size_t size)
  15409. {
  15410. struct pt_core_data *cd = dev_get_drvdata(dev);
  15411. u32 input_data[2];
  15412. int length;
  15413. int rc = 0;
  15414. /* Maximum input of one value */
  15415. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15416. ARRAY_SIZE(input_data));
  15417. if (length != 1) {
  15418. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15419. __func__);
  15420. rc = -EINVAL;
  15421. goto exit;
  15422. }
  15423. mutex_lock(&cd->system_lock);
  15424. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15425. input_data[0]);
  15426. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15427. cd->t_refresh_total = input_data[0];
  15428. cd->t_refresh_count = 0;
  15429. cd->t_refresh_active = 1;
  15430. } else {
  15431. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15432. rc = -EINVAL;
  15433. }
  15434. mutex_unlock(&cd->system_lock);
  15435. exit:
  15436. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15437. if (rc)
  15438. return rc;
  15439. return size;
  15440. }
  15441. /*******************************************************************************
  15442. * FUNCTION: pt_t_refresh_show
  15443. *
  15444. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15445. * of the T-Refresh timer counting the time it takes to see a user defined
  15446. * number of interrupts.
  15447. *
  15448. * RETURN:
  15449. * 0 = success
  15450. * !0 = failure
  15451. *
  15452. * PARAMETERS:
  15453. * *dev - pointer to device structure
  15454. * *attr - pointer to device attributes
  15455. * *buf - pointer to output buffer
  15456. ******************************************************************************/
  15457. static ssize_t pt_t_refresh_show(struct device *dev,
  15458. struct device_attribute *attr, char *buf)
  15459. {
  15460. ssize_t ret = 0;
  15461. struct pt_core_data *cd = dev_get_drvdata(dev);
  15462. u32 whole;
  15463. u16 fraction;
  15464. mutex_lock(&cd->system_lock);
  15465. /* Check if we have counted the number requested */
  15466. if (cd->t_refresh_count != cd->t_refresh_total) {
  15467. ret = scnprintf(buf, strlen(buf),
  15468. "Status: 0\n"
  15469. "%s: %d\n",
  15470. "Still counting... current IRQ count",
  15471. cd->t_refresh_count);
  15472. } else {
  15473. /* Ensure T-Refresh is de-activated */
  15474. cd->t_refresh_active = 0;
  15475. whole = cd->t_refresh_time / cd->t_refresh_count;
  15476. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15477. fraction = fraction * 1000 / cd->t_refresh_count;
  15478. ret = scnprintf(buf, strlen(buf),
  15479. "Status: 0\n"
  15480. "%s: %d\n"
  15481. "%s: %d\n"
  15482. "%s: %d\n"
  15483. "%s: %d.%02d\n",
  15484. "Requested IRQ Count ", cd->t_refresh_total,
  15485. "IRQ Counted ", cd->t_refresh_count,
  15486. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15487. "Average T-Refresh (ms) ", whole, fraction);
  15488. }
  15489. mutex_unlock(&cd->system_lock);
  15490. return ret;
  15491. }
  15492. /*******************************************************************************
  15493. * FUNCTION: pt_dut_status_show
  15494. *
  15495. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15496. * more items such as operation mode,easywake state are added in the future.
  15497. *
  15498. * RETURN: Char buffer with printed scan status information
  15499. *
  15500. * PARAMETERS:
  15501. * *dev - pointer to device structure
  15502. * *attr - pointer to device attributes
  15503. * *buf - pointer to output buffer
  15504. ******************************************************************************/
  15505. static ssize_t pt_dut_status_show(struct device *dev,
  15506. struct device_attribute *attr, char *buf)
  15507. {
  15508. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15509. u8 mode = PT_MODE_UNKNOWN;
  15510. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15511. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15512. struct pt_core_data *cd = dev_get_drvdata(dev);
  15513. ssize_t ret;
  15514. u16 calculated_crc = 0;
  15515. u16 stored_crc = 0;
  15516. u8 status;
  15517. int rc = 0;
  15518. /* In STANDBY the DUT will not repond to any PIP cmd */
  15519. if (cd->fw_sys_mode_in_standby_state) {
  15520. mode = PT_MODE_OPERATIONAL;
  15521. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15522. goto print_limited_results;
  15523. }
  15524. /* Retrieve mode and FW system mode which can only be 0-4 */
  15525. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15526. if (rc || mode == PT_MODE_UNKNOWN) {
  15527. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15528. "%s: %d\n"
  15529. "%s: n/a\n"
  15530. "%s: n/a\n"
  15531. "%s: n/a\n"
  15532. "%s: n/a\n",
  15533. "Status", rc,
  15534. "Active Exec ",
  15535. "FW System Mode ",
  15536. "Stored CRC ",
  15537. "Calculated CRC ");
  15538. return ret;
  15539. } else {
  15540. if (mode == PT_MODE_OPERATIONAL) {
  15541. if (sys_mode > FW_SYS_MODE_MAX)
  15542. sys_mode = FW_SYS_MODE_UNDEFINED;
  15543. if (sys_mode != FW_SYS_MODE_TEST)
  15544. goto print_limited_results;
  15545. rc = pt_pip_verify_config_block_crc_(cd,
  15546. PT_TCH_PARM_EBID, &status,
  15547. &calculated_crc, &stored_crc);
  15548. if (rc)
  15549. goto print_limited_results;
  15550. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15551. "%s: %d\n"
  15552. "%s: %s\n"
  15553. "%s: %s\n"
  15554. "%s: 0x%04X\n"
  15555. "%s: 0x%04X\n",
  15556. "Status", rc,
  15557. "Active Exec ", "FW",
  15558. "FW System Mode ", outputstring[sys_mode],
  15559. "Stored CRC ", stored_crc,
  15560. "Calculated CRC ", calculated_crc);
  15561. return ret;
  15562. } else {
  15563. /* When in BL or unknon mode Active Exec is "n/a" */
  15564. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15565. }
  15566. }
  15567. print_limited_results:
  15568. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15569. "%s: %d\n"
  15570. "%s: %s\n"
  15571. "%s: %s\n"
  15572. "%s: n/a\n"
  15573. "%s: n/a\n",
  15574. "Status", rc,
  15575. "Active Exec ",
  15576. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15577. "FW System Mode ", outputstring[sys_mode],
  15578. "Stored CRC ",
  15579. "Calculated CRC ");
  15580. return ret;
  15581. }
  15582. #endif /* TTDL_DIAGNOSTICS */
  15583. /*******************************************************************************
  15584. * Structures of sysfs attributes for all DUT dependent sysfs node
  15585. ******************************************************************************/
  15586. static struct attribute *early_attrs[] = {
  15587. &dev_attr_hw_version.attr,
  15588. &dev_attr_drv_version.attr,
  15589. &dev_attr_drv_ver.attr,
  15590. &dev_attr_fw_version.attr,
  15591. &dev_attr_sysinfo.attr,
  15592. &dev_attr_pip2_cmd_rsp.attr,
  15593. &dev_attr_command.attr,
  15594. &dev_attr_drv_debug.attr,
  15595. &dev_attr_hw_reset.attr,
  15596. &dev_attr_response.attr,
  15597. &dev_attr_ttdl_restart.attr,
  15598. #ifdef TTDL_DIAGNOSTICS
  15599. &dev_attr_ttdl_status.attr,
  15600. &dev_attr_pip2_enter_bl.attr,
  15601. &dev_attr_pip2_exit_bl.attr,
  15602. &dev_attr_err_gpio.attr,
  15603. &dev_attr_flush_bus.attr,
  15604. &dev_attr_ttdl_bist.attr,
  15605. #endif /* TTDL_DIAGNOSTICS */
  15606. NULL,
  15607. };
  15608. static struct attribute_group early_attr_group = {
  15609. .attrs = early_attrs,
  15610. };
  15611. static struct device_attribute pip2_attributes[] = {
  15612. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15613. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15614. #ifdef TTDL_DIAGNOSTICS
  15615. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15616. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15617. pt_pip2_ping_test_store),
  15618. #endif
  15619. };
  15620. static struct device_attribute attributes[] = {
  15621. __ATTR(dut_debug, 0644,
  15622. pt_dut_debug_show, pt_drv_debug_store),
  15623. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15624. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15625. __ATTR(get_param, 0644,
  15626. pt_get_param_show, pt_get_param_store),
  15627. #ifdef EASYWAKE_TSG6
  15628. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15629. pt_easy_wakeup_gesture_store),
  15630. __ATTR(easy_wakeup_gesture_id, 0444,
  15631. pt_easy_wakeup_gesture_id_show, NULL),
  15632. __ATTR(easy_wakeup_gesture_data, 0444,
  15633. pt_easy_wakeup_gesture_data_show, NULL),
  15634. #endif
  15635. #ifdef TTDL_DIAGNOSTICS
  15636. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15637. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15638. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15639. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15640. #endif /* TTDL_DIAGNOSTICS */
  15641. };
  15642. /*******************************************************************************
  15643. * FUNCTION: add_sysfs_interfaces
  15644. *
  15645. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15646. *
  15647. * RETURN:
  15648. * 0 = success
  15649. * !0 = failure
  15650. *
  15651. * PARAMETERS:
  15652. * *dev - pointer to device structure
  15653. ******************************************************************************/
  15654. static int add_sysfs_interfaces(struct device *dev)
  15655. {
  15656. int i;
  15657. int j = 0;
  15658. struct pt_core_data *cd = dev_get_drvdata(dev);
  15659. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15660. if (device_create_file(dev, attributes + i))
  15661. goto undo;
  15662. }
  15663. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15664. __func__, cd->active_dut_generation);
  15665. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15666. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15667. if (device_create_file(dev, pip2_attributes + j))
  15668. goto undo;
  15669. }
  15670. }
  15671. return 0;
  15672. undo:
  15673. for (i--; i >= 0; i--)
  15674. device_remove_file(dev, attributes + i);
  15675. for (j--; j >= 0; j--)
  15676. device_remove_file(dev, pip2_attributes + j);
  15677. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15678. __func__);
  15679. return -ENODEV;
  15680. }
  15681. /*******************************************************************************
  15682. * FUNCTION: remove_sysfs_interfaces
  15683. *
  15684. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15685. *
  15686. * RETURN: void
  15687. *
  15688. * PARAMETERS:
  15689. * *dev - pointer to device structure
  15690. ******************************************************************************/
  15691. static void remove_sysfs_interfaces(struct device *dev)
  15692. {
  15693. int i;
  15694. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15695. device_remove_file(dev, attributes + i);
  15696. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15697. device_remove_file(dev, pip2_attributes + i);
  15698. }
  15699. /*******************************************************************************
  15700. * FUNCTION: remove_sysfs_and_modules
  15701. *
  15702. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15703. *
  15704. * RETURN: void
  15705. *
  15706. * PARAMETERS:
  15707. * *dev - pointer to device structure
  15708. ******************************************************************************/
  15709. static void remove_sysfs_and_modules(struct device *dev)
  15710. {
  15711. struct pt_core_data *cd = dev_get_drvdata(dev);
  15712. /* Queued work should be removed before to release loader module */
  15713. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15714. pt_release_modules(cd);
  15715. pt_btn_release(dev);
  15716. pt_mt_release(dev);
  15717. remove_sysfs_interfaces(dev);
  15718. }
  15719. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15720. {
  15721. int retval;
  15722. /* Get pinctrl if target uses pinctrl */
  15723. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15724. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15725. retval = PTR_ERR(cd->ts_pinctrl);
  15726. dev_dbg(cd->dev,
  15727. "Target does not use pinctrl %d\n", retval);
  15728. goto err_pinctrl_get;
  15729. }
  15730. cd->pinctrl_state_active
  15731. = pinctrl_lookup_state(cd->ts_pinctrl,
  15732. PINCTRL_STATE_ACTIVE);
  15733. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15734. retval = PTR_ERR(cd->pinctrl_state_active);
  15735. dev_err(cd->dev,
  15736. "Can not lookup %s pinstate %d\n",
  15737. PINCTRL_STATE_ACTIVE, retval);
  15738. goto err_pinctrl_lookup;
  15739. }
  15740. cd->pinctrl_state_suspend
  15741. = pinctrl_lookup_state(cd->ts_pinctrl,
  15742. PINCTRL_STATE_SUSPEND);
  15743. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15744. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15745. dev_err(cd->dev,
  15746. "Can not lookup %s pinstate %d\n",
  15747. PINCTRL_STATE_SUSPEND, retval);
  15748. goto err_pinctrl_lookup;
  15749. }
  15750. cd->pinctrl_state_release
  15751. = pinctrl_lookup_state(cd->ts_pinctrl,
  15752. PINCTRL_STATE_RELEASE);
  15753. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15754. retval = PTR_ERR(cd->pinctrl_state_release);
  15755. dev_dbg(cd->dev,
  15756. "Can not lookup %s pinstate %d\n",
  15757. PINCTRL_STATE_RELEASE, retval);
  15758. }
  15759. return 0;
  15760. err_pinctrl_lookup:
  15761. devm_pinctrl_put(cd->ts_pinctrl);
  15762. err_pinctrl_get:
  15763. cd->ts_pinctrl = NULL;
  15764. return retval;
  15765. }
  15766. /*******************************************************************************
  15767. *******************************************************************************
  15768. * FUNCTION: pt_probe
  15769. *
  15770. * SUMMARY: Probe of the core module.
  15771. *
  15772. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15773. * probe functionality is split into two functions; pt_probe() and
  15774. * pt_probe_complete(). the initial setup is done in this function which
  15775. * then creates a WORK task which runs after the probe timer expires. This
  15776. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15777. *
  15778. * RETURN:
  15779. * 0 = success
  15780. * !0 = failure
  15781. *
  15782. * PARAMETERS:
  15783. * *ops - pointer to the bus
  15784. * *dev - pointer to the device structure
  15785. * irq - IRQ
  15786. * xfer_buf_size - size of the buffer
  15787. ******************************************************************************/
  15788. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15789. u16 irq, size_t xfer_buf_size)
  15790. {
  15791. struct pt_core_data *cd;
  15792. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15793. enum pt_atten_type type;
  15794. struct i2c_client *client = to_i2c_client(dev);
  15795. int rc = 0;
  15796. u8 pip_ver_major;
  15797. u8 pip_ver_minor;
  15798. u32 status = STARTUP_STATUS_START;
  15799. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15800. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15801. __func__);
  15802. rc = -ENODEV;
  15803. goto error_no_pdata;
  15804. }
  15805. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15806. if (!pdata->core_pdata->power) {
  15807. pt_debug(dev, DL_ERROR,
  15808. "%s: Missing platform data function\n",
  15809. __func__);
  15810. rc = -ENODEV;
  15811. goto error_no_pdata;
  15812. }
  15813. }
  15814. /* get context and debug print buffers */
  15815. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15816. if (!cd) {
  15817. rc = -ENOMEM;
  15818. goto error_no_pdata;
  15819. }
  15820. /* Initialize device info */
  15821. cd->dev = dev;
  15822. cd->pdata = pdata;
  15823. cd->cpdata = pdata->core_pdata;
  15824. cd->bus_ops = ops;
  15825. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15826. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15827. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15828. cd->hw_detected = false;
  15829. cd->pip2_prot_active = false;
  15830. cd->pip2_send_user_cmd = false;
  15831. cd->bl_pip_ver_ready = false;
  15832. cd->app_pip_ver_ready = false;
  15833. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15834. cd->get_param_id = 0;
  15835. cd->watchdog_enabled = 0;
  15836. cd->startup_retry_count = 0;
  15837. cd->core_probe_complete = 0;
  15838. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15839. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15840. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15841. cd->flashless_dut = 0;
  15842. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15843. cd->bl_with_no_int = 0;
  15844. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15845. cd->multi_chip = PT_FEATURE_DISABLE;
  15846. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15847. cd->sleep_state = SS_SLEEP_NONE;
  15848. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15849. cd->set_dut_generation = true;
  15850. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15851. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15852. cd->set_dut_generation = true;
  15853. cd->active_dut_generation = DUT_PIP1_ONLY;
  15854. } else {
  15855. cd->set_dut_generation = false;
  15856. cd->active_dut_generation = DUT_UNKNOWN;
  15857. }
  15858. /* Initialize with platform data */
  15859. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15860. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15861. cd->hid_cmd_state = 1;
  15862. cd->fw_updating = false;
  15863. cd->multi_chip = 0;
  15864. #ifdef TTDL_DIAGNOSTICS
  15865. cd->t_refresh_active = 0;
  15866. cd->t_refresh_count = 0;
  15867. cd->pip2_crc_error_count = 0;
  15868. cd->wd_xres_count = 0;
  15869. cd->bl_retry_packet_count = 0;
  15870. cd->file_erase_timeout_count = 0;
  15871. cd->show_tt_data = false;
  15872. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15873. cd->err_gpio = 0;
  15874. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15875. cd->ttdl_bist_select = 0x07;
  15876. cd->force_pip2_seq = 0;
  15877. #endif /* TTDL_DIAGNOSTICS */
  15878. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15879. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15880. sizeof(PT_PIP2_BIN_FILE_PATH));
  15881. pt_init_hid_descriptor(&cd->hid_desc);
  15882. /* Read and store the descriptor lengths */
  15883. cd->hid_core.hid_report_desc_len =
  15884. le16_to_cpu(cd->hid_desc.report_desc_len);
  15885. cd->hid_core.hid_max_input_len =
  15886. le16_to_cpu(cd->hid_desc.max_input_len);
  15887. cd->hid_core.hid_max_output_len =
  15888. le16_to_cpu(cd->hid_desc.max_output_len);
  15889. /* Initialize mutexes and spinlocks */
  15890. mutex_init(&cd->module_list_lock);
  15891. mutex_init(&cd->system_lock);
  15892. mutex_init(&cd->sysfs_lock);
  15893. mutex_init(&cd->ttdl_restart_lock);
  15894. mutex_init(&cd->firmware_class_lock);
  15895. spin_lock_init(&cd->spinlock);
  15896. /* Initialize module list */
  15897. INIT_LIST_HEAD(&cd->module_list);
  15898. /* Initialize attention lists */
  15899. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15900. INIT_LIST_HEAD(&cd->atten_list[type]);
  15901. /* Initialize parameter list */
  15902. INIT_LIST_HEAD(&cd->param_list);
  15903. /* Initialize wait queue */
  15904. init_waitqueue_head(&cd->wait_q);
  15905. rc = pt_ts_pinctrl_init(cd);
  15906. if (!rc && cd->ts_pinctrl) {
  15907. /*
  15908. * Pinctrl handle is optional. If pinctrl handle is found
  15909. * let pins to be configured in active state. If not
  15910. * found continue further without error.
  15911. */
  15912. rc = pinctrl_select_state(cd->ts_pinctrl,
  15913. cd->pinctrl_state_active);
  15914. if (rc < 0)
  15915. dev_err(&client->dev, "failed to select pin to active state\n");
  15916. }
  15917. rc = pt_get_regulator(cd, true);
  15918. if (rc) {
  15919. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15920. goto error_alloc_data;
  15921. }
  15922. rc = pt_enable_regulator(cd, true);
  15923. if (rc) {
  15924. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15925. goto error_get_regulator;
  15926. }
  15927. /* Initialize works */
  15928. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15929. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15930. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15931. /* Initialize HID specific data */
  15932. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15933. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15934. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15935. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15936. cd->hid_core.hid_desc_register =
  15937. cpu_to_le16(cd->cpdata->hid_desc_register);
  15938. /* Set platform easywake value */
  15939. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15940. #ifdef CONFIG_DRM
  15941. /* Setup active dsi panel */
  15942. active_panel = cd->cpdata->active_panel;
  15943. #endif
  15944. /* Set platform panel_id value */
  15945. cd->panel_id_support = cd->cpdata->panel_id_support;
  15946. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15947. /* Set Panel ID to default to 0 */
  15948. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15949. else
  15950. /* Set Panel ID to Not Enabled */
  15951. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15952. /* Initialize hw_version default to FFFF.FFFF.FF */
  15953. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15954. dev_set_drvdata(dev, cd);
  15955. /* PtSBC builds will call this function in pt_probe_complete() */
  15956. pt_add_core(dev);
  15957. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15958. if (rc) {
  15959. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15960. __func__);
  15961. goto error_enable_regulator;
  15962. }
  15963. /*
  15964. * Save the pointer to a global value, which will be used
  15965. * in ttdl_restart function
  15966. */
  15967. cd->bus_ops = ops;
  15968. /*
  15969. * When the IRQ GPIO is not direclty accessible and no function is
  15970. * defined to get the IRQ status, the IRQ passed in must be assigned
  15971. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15972. */
  15973. if (!cd->cpdata->irq_stat) {
  15974. cd->irq = irq;
  15975. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15976. __func__, cd->irq);
  15977. }
  15978. /* Call platform init function before setting up the GPIO's */
  15979. if (cd->cpdata->init) {
  15980. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15981. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15982. } else {
  15983. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15984. __func__);
  15985. rc = 0;
  15986. }
  15987. if (rc < 0) {
  15988. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15989. __func__, rc);
  15990. }
  15991. /* Power on any needed regulator(s) */
  15992. if (cd->cpdata->setup_power) {
  15993. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15994. rc = cd->cpdata->setup_power(cd->cpdata,
  15995. PT_MT_POWER_ON, cd->dev);
  15996. } else {
  15997. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15998. __func__);
  15999. rc = 0;
  16000. }
  16001. if (rc < 0)
  16002. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16003. __func__, rc);
  16004. #ifdef TTDL_DIAGNOSTICS
  16005. cd->watchdog_irq_stuck_count = 0;
  16006. cd->bus_transmit_error_count = 0;
  16007. #endif /* TTDL_DIAGNOSTICS */
  16008. if (cd->cpdata->detect) {
  16009. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16010. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16011. pt_platform_detect_read);
  16012. if (!rc) {
  16013. cd->hw_detected = true;
  16014. pt_debug(cd->dev, DL_INFO,
  16015. "%s: HW detected\n", __func__);
  16016. } else {
  16017. cd->hw_detected = false;
  16018. pt_debug(cd->dev, DL_INFO,
  16019. "%s: No HW detected\n", __func__);
  16020. rc = -ENODEV;
  16021. goto error_detect;
  16022. }
  16023. } else {
  16024. pt_debug(dev, DL_ERROR,
  16025. "%s: PARADE No HW detect function pointer\n",
  16026. __func__);
  16027. /*
  16028. * "hw_reset" is not needed in the "if" statement,
  16029. * because "hw_reset" is already included in "hw_detect"
  16030. * function.
  16031. */
  16032. rc = pt_hw_hard_reset(cd);
  16033. if (rc)
  16034. pt_debug(cd->dev, DL_ERROR,
  16035. "%s: FAILED to execute HARD reset\n",
  16036. __func__);
  16037. }
  16038. if (cd->cpdata->setup_irq) {
  16039. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16040. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16041. if (rc) {
  16042. pt_debug(dev, DL_ERROR,
  16043. "%s: Error, couldn't setup IRQ\n", __func__);
  16044. goto error_setup_irq;
  16045. }
  16046. } else {
  16047. pt_debug(dev, DL_ERROR,
  16048. "%s: IRQ function pointer not setup\n",
  16049. __func__);
  16050. goto error_setup_irq;
  16051. }
  16052. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16053. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16054. (unsigned long)cd);
  16055. #else
  16056. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16057. #endif
  16058. pt_stop_wd_timer(cd);
  16059. #ifdef TTHE_TUNER_SUPPORT
  16060. mutex_init(&cd->tthe_lock);
  16061. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16062. 0644, NULL, cd, &tthe_debugfs_fops);
  16063. #endif
  16064. rc = device_init_wakeup(dev, 1);
  16065. if (rc < 0)
  16066. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16067. __func__, rc);
  16068. pm_runtime_get_noresume(dev);
  16069. pm_runtime_set_active(dev);
  16070. pm_runtime_enable(dev);
  16071. /* If IRQ asserted, read out all from buffer to release INT pin */
  16072. if (cd->cpdata->irq_stat) {
  16073. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16074. } else {
  16075. /* Force a read in case the reset sentinel already arrived */
  16076. rc = pt_read_input(cd);
  16077. if (!rc)
  16078. pt_parse_input(cd);
  16079. }
  16080. /* Without sleep DUT is not ready and will NAK the first write */
  16081. msleep(150);
  16082. /* Attempt to set the DUT generation if not yet set */
  16083. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16084. if (cd->bl_pip_ver_ready ||
  16085. (cd->app_pip_ver_ready &&
  16086. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16087. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16088. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16089. __func__, cd->active_dut_generation);
  16090. } else {
  16091. rc = _pt_detect_dut_generation(cd->dev,
  16092. &status, &cd->active_dut_generation,
  16093. &cd->mode);
  16094. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16095. || rc) {
  16096. pt_debug(cd->dev, DL_ERROR,
  16097. " === DUT Gen unknown, Skip Enum ===\n");
  16098. goto skip_enum;
  16099. }
  16100. }
  16101. }
  16102. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16103. &pip_ver_major, &pip_ver_minor);
  16104. if (pip_ver_major == 2) {
  16105. cd->bl_pip_ver_ready = true;
  16106. pt_debug(dev, DL_ERROR,
  16107. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16108. pip_ver_minor);
  16109. cd->hw_detected = true;
  16110. } else if (pip_ver_major == 1) {
  16111. cd->app_pip_ver_ready = true;
  16112. pt_debug(dev, DL_ERROR,
  16113. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16114. cd->hw_detected = true;
  16115. } else {
  16116. cd->sysinfo.ttdata.pip_ver_major = 0;
  16117. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16118. cd->app_pip_ver_ready = false;
  16119. cd->hw_detected = false;
  16120. pt_debug(dev, DL_ERROR,
  16121. " === PIP Version Not Detected, Skip Enum ===\n");
  16122. /* For legacy DUTS proceed, enum will attempt to launch app */
  16123. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16124. goto skip_enum;
  16125. }
  16126. rc = pt_enum_with_dut(cd, false, &status);
  16127. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16128. __func__, cd->startup_status, status);
  16129. if (rc == -ENODEV) {
  16130. pt_debug(cd->dev, DL_ERROR,
  16131. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16132. /* For PtSBC don't error out, allow TTDL to stay up */
  16133. goto error_after_startup;
  16134. }
  16135. /* Suspend scanning until probe is complete to avoid asyc touches */
  16136. pt_pip_suspend_scanning_(cd);
  16137. if (cd->hw_detected) {
  16138. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16139. __func__);
  16140. rc = add_sysfs_interfaces(dev);
  16141. if (rc < 0) {
  16142. pt_debug(dev, DL_ERROR,
  16143. "%s: Error, fail sysfs init\n", __func__);
  16144. goto error_after_startup;
  16145. }
  16146. } else {
  16147. pt_debug(dev, DL_ERROR,
  16148. "%s: No HW detected, sysfs interfaces not added\n",
  16149. __func__);
  16150. }
  16151. skip_enum:
  16152. pm_runtime_put_sync(dev);
  16153. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16154. rc = pt_mt_probe(dev);
  16155. if (rc < 0) {
  16156. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16157. __func__);
  16158. goto error_after_sysfs_create;
  16159. }
  16160. rc = pt_btn_probe(dev);
  16161. if (rc < 0) {
  16162. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16163. __func__);
  16164. goto error_after_startup_mt;
  16165. }
  16166. pt_probe_modules(cd);
  16167. #ifdef CONFIG_HAS_EARLYSUSPEND
  16168. pt_setup_early_suspend(cd);
  16169. #elif defined(CONFIG_DRM)
  16170. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16171. pt_setup_drm_notifier(cd);
  16172. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16173. #elif defined(CONFIG_FB)
  16174. pt_setup_fb_notifier(cd);
  16175. #endif
  16176. #ifdef NEED_SUSPEND_NOTIFIER
  16177. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16178. register_pm_notifier(&cd->pm_notifier);
  16179. #endif
  16180. pt_pip_resume_scanning_(cd);
  16181. mutex_lock(&cd->system_lock);
  16182. cd->startup_status |= status;
  16183. cd->core_probe_complete = 1;
  16184. mutex_unlock(&cd->system_lock);
  16185. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16186. __func__);
  16187. return 0;
  16188. error_after_startup_mt:
  16189. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16190. pt_mt_release(dev);
  16191. error_after_sysfs_create:
  16192. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16193. pm_runtime_disable(dev);
  16194. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16195. device_wakeup_disable(dev);
  16196. #endif
  16197. device_init_wakeup(dev, 0);
  16198. cancel_work_sync(&cd->enum_work);
  16199. pt_stop_wd_timer(cd);
  16200. pt_free_si_ptrs(cd);
  16201. remove_sysfs_interfaces(dev);
  16202. error_after_startup:
  16203. pr_err("%s PARADE error_after_startup\n", __func__);
  16204. del_timer(&cd->watchdog_timer);
  16205. if (cd->cpdata->setup_irq)
  16206. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16207. error_setup_irq:
  16208. error_detect:
  16209. if (cd->cpdata->init)
  16210. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16211. if (cd->cpdata->setup_power)
  16212. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16213. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16214. error_enable_regulator:
  16215. pt_del_core(dev);
  16216. dev_set_drvdata(dev, NULL);
  16217. pt_enable_regulator(cd, false);
  16218. error_get_regulator:
  16219. pt_get_regulator(cd, false);
  16220. error_alloc_data:
  16221. kfree(cd);
  16222. error_no_pdata:
  16223. pr_err("%s failed.\n", __func__);
  16224. return rc;
  16225. }
  16226. EXPORT_SYMBOL_GPL(pt_probe);
  16227. /*******************************************************************************
  16228. * FUNCTION: pt_release
  16229. *
  16230. * SUMMARY: This function does the following cleanup:
  16231. * - Releases all probed modules
  16232. * - Stops the watchdog
  16233. * - Cancels all pending work tasks
  16234. * - Removes all created sysfs nodes
  16235. * - Removes all created debugfs nodes
  16236. * - Frees the IRQ
  16237. * - Deletes the core
  16238. * - Frees all pointers and HID reports
  16239. *
  16240. * RETURN:
  16241. * 0 = success
  16242. * !0 = failure
  16243. *
  16244. * PARAMETERS:
  16245. * *cd - pointer to the core data structure
  16246. ******************************************************************************/
  16247. int pt_release(struct pt_core_data *cd)
  16248. {
  16249. struct device *dev = cd->dev;
  16250. /*
  16251. * Suspend the device before freeing the startup_work and stopping
  16252. * the watchdog since sleep function restarts watchdog on failure
  16253. */
  16254. pm_runtime_suspend(dev);
  16255. pm_runtime_disable(dev);
  16256. /*
  16257. * Any 'work' that can trigger a new thread should be canceled first.
  16258. * The watchdog is also stopped again because another thread could have
  16259. * restarted it. The irq_work is cancelled last because it is used for
  16260. * all I2C/SPI communication.
  16261. */
  16262. pt_stop_wd_timer(cd);
  16263. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16264. cancel_work_sync(&cd->ttdl_restart_work);
  16265. cancel_work_sync(&cd->enum_work);
  16266. pt_stop_wd_timer(cd);
  16267. pt_release_modules(cd);
  16268. pt_proximity_release(dev);
  16269. pt_btn_release(dev);
  16270. pt_mt_release(dev);
  16271. #ifdef CONFIG_HAS_EARLYSUSPEND
  16272. unregister_early_suspend(&cd->es);
  16273. #elif defined(CONFIG_DRM)
  16274. if (active_panel)
  16275. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16276. #elif defined(CONFIG_FB)
  16277. fb_unregister_client(&cd->fb_notifier);
  16278. #endif
  16279. #ifdef NEED_SUSPEND_NOTIFIER
  16280. unregister_pm_notifier(&cd->pm_notifier);
  16281. #endif
  16282. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16283. device_wakeup_disable(dev);
  16284. #endif
  16285. device_init_wakeup(dev, 0);
  16286. #ifdef TTHE_TUNER_SUPPORT
  16287. mutex_lock(&cd->tthe_lock);
  16288. cd->tthe_exit = 1;
  16289. wake_up(&cd->wait_q);
  16290. mutex_unlock(&cd->tthe_lock);
  16291. debugfs_remove(cd->tthe_debugfs);
  16292. #endif
  16293. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16294. remove_sysfs_interfaces(dev);
  16295. disable_irq_nosync(cd->irq);
  16296. if (cd->cpdata->setup_irq)
  16297. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16298. if (cd->cpdata->init)
  16299. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16300. if (cd->cpdata->setup_power)
  16301. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16302. dev_set_drvdata(dev, NULL);
  16303. pt_del_core(dev);
  16304. pt_enable_regulator(cd, false);
  16305. pt_get_regulator(cd, false);
  16306. pt_free_si_ptrs(cd);
  16307. kfree(cd);
  16308. return 0;
  16309. }
  16310. EXPORT_SYMBOL_GPL(pt_release);
  16311. MODULE_LICENSE("GPL");
  16312. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16313. MODULE_AUTHOR("Parade Technologies <[email protected]>");