pt_core.c 505 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578
  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 PT_CORE_STARTUP_RETRY_COUNT 3
  41. #define PT_STATUS_STR_LEN (50)
  42. #if defined(CONFIG_DRM)
  43. static struct drm_panel *active_panel;
  44. #endif
  45. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  46. static const char *pt_driver_core_name = PT_CORE_NAME;
  47. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  48. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  49. struct pt_hid_field {
  50. int report_count;
  51. int report_size;
  52. int size; /* report_count * report_size */
  53. int offset;
  54. int data_type;
  55. int logical_min;
  56. int logical_max;
  57. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  58. u32 usage_page;
  59. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  60. struct pt_hid_report *report;
  61. bool record_field;
  62. };
  63. struct pt_hid_report {
  64. u8 id;
  65. u8 type;
  66. int size;
  67. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  68. int num_fields;
  69. int record_field_index;
  70. int header_size;
  71. int record_size;
  72. u32 usage_page;
  73. };
  74. struct atten_node {
  75. struct list_head node;
  76. char *id;
  77. struct device *dev;
  78. int (*func)(struct device *dev);
  79. int mode;
  80. };
  81. struct param_node {
  82. struct list_head node;
  83. u8 id;
  84. u32 value;
  85. u8 size;
  86. };
  87. struct module_node {
  88. struct list_head node;
  89. struct pt_module *module;
  90. void *data;
  91. };
  92. struct pt_hid_cmd {
  93. u8 opcode;
  94. u8 report_type;
  95. union {
  96. u8 report_id;
  97. u8 power_state;
  98. };
  99. u8 has_data_register;
  100. size_t write_length;
  101. u8 *write_buf;
  102. u8 *read_buf;
  103. u8 wait_interrupt;
  104. u8 reset_cmd;
  105. u16 timeout_ms;
  106. };
  107. struct pt_hid_output {
  108. u8 cmd_type;
  109. u16 length;
  110. u8 command_code;
  111. size_t write_length;
  112. u8 *write_buf;
  113. u8 novalidate;
  114. u8 reset_expected;
  115. u16 timeout_ms;
  116. };
  117. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  118. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  119. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  120. #define CREATE_PIP1_FW_CMD(command) \
  121. .cmd_type = PIP1_CMD_TYPE_FW, \
  122. .command_code = command
  123. #define CREATE_PIP1_BL_CMD(command) \
  124. .cmd_type = PIP1_CMD_TYPE_BL, \
  125. .command_code = command
  126. #define PT_MAX_PR_BUF_SIZE 2048
  127. /*******************************************************************************
  128. * FUNCTION: pt_pr_buf
  129. *
  130. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  131. *
  132. * RETURN: Void
  133. *
  134. * PARAMETERS:
  135. * *dev - pointer to Device structure
  136. * debug_level - requested debug level to print at
  137. * *buf - pointer to buffer to print
  138. * buf_len - size of buf
  139. * *data_name - Descriptive name of data prefixed to data
  140. ******************************************************************************/
  141. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  142. u16 buf_len, const char *data_name)
  143. {
  144. struct pt_core_data *cd = dev_get_drvdata(dev);
  145. int i;
  146. ssize_t pr_buf_index = 0;
  147. int max_size;
  148. /* only proceed if valid debug level and there is data to print */
  149. if (debug_level <= cd->debug_level && buf_len > 0) {
  150. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  151. if (!pr_buf)
  152. return;
  153. /*
  154. * With a space each printed char takes 3 bytes, subtract
  155. * the length of the data_name prefix as well as 11 bytes
  156. * for the " [0..xxx]: " printed before the data.
  157. */
  158. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  159. /* Ensure pr_buf_index stays within the 1018 size */
  160. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  161. data_name);
  162. for (i = 0; i < buf_len && i < max_size; i++)
  163. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  164. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  165. pt_debug(dev, debug_level, "%s\n", pr_buf);
  166. kfree(pr_buf);
  167. }
  168. }
  169. EXPORT_SYMBOL_GPL(pt_pr_buf);
  170. #ifdef TTHE_TUNER_SUPPORT
  171. /*******************************************************************************
  172. * FUNCTION: tthe_print
  173. *
  174. * SUMMARY: Format data name and time stamp as the header and format the
  175. * content of input buffer with hex base to "tthe_buf". And then wake up event
  176. * semaphore for tthe debugfs node.
  177. *
  178. * RETURN:
  179. * 0 = success
  180. * !0 = failure
  181. *
  182. * PARAMETERS:
  183. * *cd - pointer to core data
  184. * *buf - pointer to input buffer
  185. * buf_len - size of input buffer
  186. * *data_name - pointer to data name
  187. ******************************************************************************/
  188. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  189. const u8 *data_name)
  190. {
  191. int name_len = strlen(data_name);
  192. int i, n;
  193. u8 *p;
  194. int remain;
  195. u8 data_name_with_time_stamp[100];
  196. /* Prepend timestamp, if requested, to data_name */
  197. if (cd->show_timestamp) {
  198. scnprintf(data_name_with_time_stamp,
  199. sizeof(data_name_with_time_stamp),
  200. "[%u] %s", pt_get_time_stamp(), data_name);
  201. data_name = data_name_with_time_stamp;
  202. name_len = strlen(data_name);
  203. }
  204. mutex_lock(&cd->tthe_lock);
  205. if (!cd->tthe_buf)
  206. goto exit;
  207. /* Add 1 due to the '\n' that is appended at the end */
  208. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  209. goto exit;
  210. if (name_len + buf_len == 0)
  211. goto exit;
  212. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  213. if (remain < name_len)
  214. name_len = remain;
  215. p = cd->tthe_buf + cd->tthe_buf_len;
  216. memcpy(p, data_name, name_len);
  217. cd->tthe_buf_len += name_len;
  218. p += name_len;
  219. remain -= name_len;
  220. *p = 0;
  221. for (i = 0; i < buf_len; i++) {
  222. n = scnprintf(p, remain, "%02X ", buf[i]);
  223. if (n <= 0)
  224. break;
  225. p += n;
  226. remain -= n;
  227. cd->tthe_buf_len += n;
  228. }
  229. n = scnprintf(p, remain, "\n");
  230. cd->tthe_buf_len += n;
  231. exit:
  232. wake_up(&cd->wait_q);
  233. mutex_unlock(&cd->tthe_lock);
  234. return 0;
  235. }
  236. /*******************************************************************************
  237. * FUNCTION: _pt_request_tthe_print
  238. *
  239. * SUMMARY: Function pointer included in core_cmds to allow other modules
  240. * to request to print data to the "tthe_buffer".
  241. *
  242. * RETURN:
  243. * 0 = success
  244. * !0 = failure
  245. *
  246. * PARAMETERS:
  247. * *dev - pointer to device structure
  248. ******************************************************************************/
  249. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  250. int buf_len, const u8 *data_name)
  251. {
  252. struct pt_core_data *cd = dev_get_drvdata(dev);
  253. return tthe_print(cd, buf, buf_len, data_name);
  254. }
  255. #endif
  256. /*******************************************************************************
  257. * FUNCTION: pt_platform_detect_read
  258. *
  259. * SUMMARY: To be passed to platform dectect function to perform a read
  260. * operation.
  261. *
  262. * RETURN:
  263. * 0 = success
  264. * !0 = failure
  265. *
  266. * PARAMETERS:
  267. * *dev - pointer to Device structure
  268. * *buf - pointer to buffer where the data read will be stored
  269. * size - size to be read
  270. ******************************************************************************/
  271. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  272. {
  273. struct pt_core_data *cd = dev_get_drvdata(dev);
  274. return pt_adap_read_default(cd, buf, size);
  275. }
  276. /*******************************************************************************
  277. * FUNCTION: pt_add_parameter
  278. *
  279. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  280. * On every reset of the DUT this linked list is traversed and all
  281. * parameters in it are restored to the DUT.
  282. *
  283. * RETURN:
  284. * 0 = success
  285. * !0 = failure
  286. *
  287. * PARAMETERS:
  288. * *cd - pointer to core data
  289. * param_id - parameter ID to add
  290. * param_value - Value corresponding to the ID
  291. * param_size - Size of param_value
  292. ******************************************************************************/
  293. static int pt_add_parameter(struct pt_core_data *cd,
  294. u8 param_id, u32 param_value, u8 param_size)
  295. {
  296. struct param_node *param, *param_new;
  297. /* Check if parameter already exists in the list */
  298. spin_lock(&cd->spinlock);
  299. list_for_each_entry(param, &cd->param_list, node) {
  300. if (param->id == param_id) {
  301. /* Update parameter */
  302. param->value = param_value;
  303. pt_debug(cd->dev, DL_INFO,
  304. "%s: Update parameter id:%d value:%d size:%d\n",
  305. __func__, param_id, param_value, param_size);
  306. goto exit_unlock;
  307. }
  308. }
  309. spin_unlock(&cd->spinlock);
  310. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  311. if (!param_new)
  312. return -ENOMEM;
  313. param_new->id = param_id;
  314. param_new->value = param_value;
  315. param_new->size = param_size;
  316. pt_debug(cd->dev, DL_INFO,
  317. "%s: Add parameter id:%d value:%d size:%d\n",
  318. __func__, param_id, param_value, param_size);
  319. spin_lock(&cd->spinlock);
  320. list_add(&param_new->node, &cd->param_list);
  321. exit_unlock:
  322. spin_unlock(&cd->spinlock);
  323. return 0;
  324. }
  325. #ifdef TTDL_DIAGNOSTICS
  326. /*******************************************************************************
  327. * FUNCTION: pt_erase_parameter_list
  328. *
  329. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  330. * pairs. In some test cases this functionality is needed to ensure DUT
  331. * returns to a virgin state after a reset and no parameters are restored.
  332. *
  333. * RETURN:
  334. * 0 = success
  335. * !0 = failure
  336. *
  337. * PARAMETERS:
  338. * *cd - pointer to core data
  339. ******************************************************************************/
  340. static int pt_erase_parameter_list(struct pt_core_data *cd)
  341. {
  342. struct param_node *pos, *temp;
  343. spin_lock(&cd->spinlock);
  344. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  345. pt_debug(cd->dev, DL_INFO,
  346. "%s: Parameter Restore List - remove 0x%02x\n",
  347. __func__, pos->id);
  348. list_del(&pos->node);
  349. kfree(pos);
  350. }
  351. spin_unlock(&cd->spinlock);
  352. return 0;
  353. }
  354. /*******************************************************************************
  355. * FUNCTION: pt_count_parameter_list
  356. *
  357. * SUMMARY: Count the items in the RAM parameter restor list
  358. *
  359. * RETURN:
  360. * 0 = success
  361. * !0 = failure
  362. *
  363. * PARAMETERS:
  364. * *cd - pointer to core data
  365. ******************************************************************************/
  366. static int pt_count_parameter_list(struct pt_core_data *cd)
  367. {
  368. struct param_node *pos, *temp;
  369. int entries = 0;
  370. spin_lock(&cd->spinlock);
  371. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  372. entries++;
  373. spin_unlock(&cd->spinlock);
  374. return entries;
  375. }
  376. #endif /* TTDL_DIAGNOSTICS */
  377. /*******************************************************************************
  378. * FUNCTION: request_exclusive
  379. *
  380. * SUMMARY: Request exclusive access to the DUT
  381. *
  382. * RETURN:
  383. * 0 = success
  384. * !0 = failure
  385. *
  386. * PARAMETERS:
  387. * *cd - pointer to core data
  388. * *ownptr - pointer to device
  389. * timeout_ms - Timeout value
  390. ******************************************************************************/
  391. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  392. int timeout_ms)
  393. {
  394. int t = msecs_to_jiffies(timeout_ms);
  395. bool with_timeout = (timeout_ms != 0);
  396. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  397. __func__, timeout_ms);
  398. mutex_lock(&cd->system_lock);
  399. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  400. cd->exclusive_dev = ownptr;
  401. goto exit;
  402. }
  403. cd->exclusive_waits++;
  404. wait:
  405. mutex_unlock(&cd->system_lock);
  406. if (with_timeout) {
  407. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  408. if (IS_TMO(t)) {
  409. pt_debug(cd->dev, DL_ERROR,
  410. "%s: tmo waiting exclusive access\n", __func__);
  411. return -ETIME;
  412. }
  413. } else {
  414. wait_event(cd->wait_q, !cd->exclusive_dev);
  415. }
  416. mutex_lock(&cd->system_lock);
  417. if (cd->exclusive_dev)
  418. goto wait;
  419. cd->exclusive_dev = ownptr;
  420. cd->exclusive_waits--;
  421. exit:
  422. mutex_unlock(&cd->system_lock);
  423. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  424. __func__, ownptr);
  425. return 0;
  426. }
  427. /*******************************************************************************
  428. * FUNCTION: release_exclusive_
  429. *
  430. * SUMMARY: Release exclusive access to the DUT
  431. *
  432. * RETURN:
  433. * 0 = success
  434. *
  435. * PARAMETERS:
  436. * *cd - pointer to core data
  437. * *ownptr - pointer to device
  438. ******************************************************************************/
  439. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  440. {
  441. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  442. __func__);
  443. if (cd->exclusive_dev != ownptr)
  444. return -EINVAL;
  445. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  446. __func__, cd->exclusive_dev);
  447. cd->exclusive_dev = NULL;
  448. wake_up(&cd->wait_q);
  449. return 0;
  450. }
  451. /*******************************************************************************
  452. * FUNCTION: release_exclusive
  453. *
  454. * SUMMARY: Protected wrapper to release_exclusive_()
  455. *
  456. * RETURN:
  457. * 0 = success
  458. * !0 = failure
  459. *
  460. * PARAMETERS:
  461. * *cd - pointer to core data
  462. * *ownptr - pointer to device
  463. ******************************************************************************/
  464. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  465. {
  466. int rc;
  467. mutex_lock(&cd->system_lock);
  468. rc = release_exclusive_(cd, ownptr);
  469. mutex_unlock(&cd->system_lock);
  470. return rc;
  471. }
  472. /*******************************************************************************
  473. * FUNCTION: pt_hid_exec_cmd_
  474. *
  475. * SUMMARY: Send the HID command to the DUT
  476. *
  477. * RETURN:
  478. * 0 = success
  479. * !0 = failure
  480. *
  481. * PARAMETERS:
  482. * *cd - pointer to core data
  483. * *hid_cmd - pointer to the HID command to send
  484. ******************************************************************************/
  485. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  486. struct pt_hid_cmd *hid_cmd)
  487. {
  488. int rc = 0;
  489. u8 *cmd;
  490. u16 cmd_length;
  491. u8 cmd_offset = 0;
  492. cmd_length = 2 /* command register */
  493. + 2 /* command */
  494. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  495. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  496. + hid_cmd->write_length; /* Data length */
  497. cmd = kzalloc(cmd_length, GFP_KERNEL);
  498. if (!cmd)
  499. return -ENOMEM;
  500. /* Set Command register */
  501. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  502. sizeof(cd->hid_desc.command_register));
  503. cmd_offset += sizeof(cd->hid_desc.command_register);
  504. /* Set Command */
  505. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  506. if (hid_cmd->report_id >= 0XF)
  507. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  508. else
  509. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  510. cmd_offset++;
  511. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  512. cmd_offset++;
  513. if (hid_cmd->report_id >= 0XF) {
  514. cmd[cmd_offset] = hid_cmd->report_id;
  515. cmd_offset++;
  516. }
  517. /* Set Data register */
  518. if (hid_cmd->has_data_register) {
  519. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  520. sizeof(cd->hid_desc.data_register));
  521. cmd_offset += sizeof(cd->hid_desc.data_register);
  522. }
  523. /* Set Data */
  524. if (hid_cmd->write_length && hid_cmd->write_buf) {
  525. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  526. hid_cmd->write_length);
  527. cmd_offset += hid_cmd->write_length;
  528. }
  529. pt_debug(cd->dev, DL_INFO,
  530. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  531. __func__, cmd_length, hid_cmd->report_id);
  532. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  533. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  534. hid_cmd->read_buf);
  535. if (rc)
  536. pt_debug(cd->dev, DL_ERROR,
  537. "%s: Fail pt_adap_transfer\n", __func__);
  538. kfree(cmd);
  539. return rc;
  540. }
  541. #ifdef TTDL_DIAGNOSTICS
  542. /*******************************************************************************
  543. * FUNCTION: pt_toggle_err_gpio
  544. *
  545. * SUMMARY: Toggles the pre-defined error GPIO
  546. *
  547. * RETURN: n/a
  548. *
  549. * PARAMETERS:
  550. * *cd - pointer to core data
  551. * type - type of err that occurred
  552. ******************************************************************************/
  553. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  554. {
  555. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  556. __func__, type);
  557. if (cd->err_gpio && type == cd->err_gpio_type) {
  558. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  559. gpio_direction_output(cd->err_gpio,
  560. !gpio_get_value(cd->err_gpio));
  561. }
  562. }
  563. /*******************************************************************************
  564. * FUNCTION: _pt_request_toggle_err_gpio
  565. *
  566. * SUMMARY: Function pointer included in core_cmds to allow other modules
  567. * to request to toggle the err_gpio
  568. *
  569. * RETURN: n/a
  570. *
  571. * PARAMETERS:
  572. * *cd - pointer to core data
  573. * type - type of err that occurred
  574. ******************************************************************************/
  575. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  576. {
  577. struct pt_core_data *cd = dev_get_drvdata(dev);
  578. pt_toggle_err_gpio(cd, type);
  579. }
  580. #endif /* TTDL_DIAGNOSTICS */
  581. /*******************************************************************************
  582. * FUNCTION: pt_hid_exec_cmd_and_wait_
  583. *
  584. * SUMMARY: Send the HID command to the DUT and wait for the response
  585. *
  586. * RETURN:
  587. * 0 = success
  588. * !0 = failure
  589. *
  590. * PARAMETERS:
  591. * *cd - pointer to core data
  592. * *hid_cmd - pointer to the HID command to send
  593. ******************************************************************************/
  594. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  595. struct pt_hid_cmd *hid_cmd)
  596. {
  597. int rc = 0;
  598. int t;
  599. u16 timeout_ms;
  600. int *cmd_state;
  601. if (hid_cmd->reset_cmd)
  602. cmd_state = &cd->hid_reset_cmd_state;
  603. else
  604. cmd_state = &cd->hid_cmd_state;
  605. if (hid_cmd->wait_interrupt) {
  606. mutex_lock(&cd->system_lock);
  607. *cmd_state = 1;
  608. mutex_unlock(&cd->system_lock);
  609. }
  610. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  611. if (rc) {
  612. if (hid_cmd->wait_interrupt)
  613. goto error;
  614. goto exit;
  615. }
  616. if (!hid_cmd->wait_interrupt)
  617. goto exit;
  618. if (hid_cmd->timeout_ms)
  619. timeout_ms = hid_cmd->timeout_ms;
  620. else
  621. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  622. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  623. msecs_to_jiffies(timeout_ms));
  624. if (IS_TMO(t)) {
  625. #ifdef TTDL_DIAGNOSTICS
  626. cd->bus_transmit_error_count++;
  627. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  628. #endif /* TTDL_DIAGNOSTICS */
  629. pt_debug(cd->dev, DL_ERROR,
  630. "%s: HID output cmd execution timed out\n",
  631. __func__);
  632. rc = -ETIME;
  633. goto error;
  634. }
  635. goto exit;
  636. error:
  637. mutex_lock(&cd->system_lock);
  638. *cmd_state = 0;
  639. mutex_unlock(&cd->system_lock);
  640. exit:
  641. return rc;
  642. }
  643. /*******************************************************************************
  644. * FUNCTION: pt_hid_cmd_reset_
  645. *
  646. * SUMMARY: Send the HID RESET command to the DUT
  647. *
  648. * RETURN:
  649. * 0 = success
  650. * !0 = failure
  651. *
  652. * PARAMETERS:
  653. * *cd - pointer to core data
  654. ******************************************************************************/
  655. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  656. {
  657. struct pt_hid_cmd hid_cmd = {
  658. .opcode = HID_CMD_RESET,
  659. .wait_interrupt = 1,
  660. .reset_cmd = 1,
  661. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  662. };
  663. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  664. }
  665. /*******************************************************************************
  666. * FUNCTION: pt_hid_cmd_reset
  667. *
  668. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  669. * access.
  670. *
  671. * RETURN:
  672. * 0 = success
  673. * !0 = failure
  674. *
  675. * PARAMETERS:
  676. * *cd - pointer to core data
  677. ******************************************************************************/
  678. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  679. {
  680. int rc = 0;
  681. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  682. if (rc < 0) {
  683. pt_debug(cd->dev, DL_ERROR,
  684. "%s: fail get exclusive ex=%p own=%p\n",
  685. __func__, cd->exclusive_dev, cd->dev);
  686. return rc;
  687. }
  688. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  689. rc = pt_hid_cmd_reset_(cd);
  690. if (release_exclusive(cd, cd->dev) < 0)
  691. pt_debug(cd->dev, DL_ERROR,
  692. "%s: fail to release exclusive\n", __func__);
  693. return rc;
  694. }
  695. /*******************************************************************************
  696. * FUNCTION: pt_hid_cmd_set_power_
  697. *
  698. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  699. *
  700. * RETURN:
  701. * 0 = success
  702. * !0 = failure
  703. *
  704. * PARAMETERS:
  705. * *cd - pointer to core data
  706. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  707. ******************************************************************************/
  708. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  709. u8 power_state)
  710. {
  711. int rc = 0;
  712. struct pt_hid_cmd hid_cmd = {
  713. .opcode = HID_CMD_SET_POWER,
  714. .wait_interrupt = 1,
  715. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  716. };
  717. hid_cmd.power_state = power_state;
  718. /* The chip won't give response if goes to Deep Standby */
  719. if (power_state == HID_POWER_STANDBY) {
  720. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  721. if (rc)
  722. pt_debug(cd->dev, DL_ERROR,
  723. "%s: Failed to set power to state:%d\n",
  724. __func__, power_state);
  725. else
  726. cd->fw_sys_mode_in_standby_state = true;
  727. return rc;
  728. }
  729. cd->fw_sys_mode_in_standby_state = false;
  730. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  731. if (rc) {
  732. pt_debug(cd->dev, DL_ERROR,
  733. "%s: Failed to set power to state:%d\n",
  734. __func__, power_state);
  735. return rc;
  736. }
  737. /* validate */
  738. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  739. || ((cd->response_buf[3] & 0x3) != power_state)
  740. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  741. rc = -EINVAL;
  742. return rc;
  743. }
  744. /*******************************************************************************
  745. * FUNCTION: pt_hid_cmd_set_power
  746. *
  747. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  748. * exclusive access.
  749. *
  750. * RETURN:
  751. * 0 = success
  752. * !0 = failure
  753. *
  754. * PARAMETERS:
  755. * *cd - pointer to core data
  756. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  757. ******************************************************************************/
  758. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  759. u8 power_state)
  760. {
  761. int rc;
  762. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  763. if (rc < 0) {
  764. pt_debug(cd->dev, DL_ERROR,
  765. "%s: fail get exclusive ex=%p own=%p\n",
  766. __func__, cd->exclusive_dev, cd->dev);
  767. return rc;
  768. }
  769. rc = pt_hid_cmd_set_power_(cd, power_state);
  770. if (release_exclusive(cd, cd->dev) < 0)
  771. pt_debug(cd->dev, DL_ERROR,
  772. "%s: fail to release exclusive\n", __func__);
  773. return rc;
  774. }
  775. static const u16 crc_table[16] = {
  776. 0x0000, 0x1021, 0x2042, 0x3063,
  777. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  778. 0x8108, 0x9129, 0xa14a, 0xb16b,
  779. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  780. };
  781. /*******************************************************************************
  782. * FUNCTION: _pt_compute_crc
  783. *
  784. * SUMMARY: Calculate CRC by CRC table.
  785. *
  786. * RETURN:
  787. * CRC calculation result
  788. *
  789. * PARAMETERS:
  790. * *buf - pointer to the data array to be calculated
  791. * size - size of data array
  792. ******************************************************************************/
  793. static u16 _pt_compute_crc(u8 *buf, u32 size)
  794. {
  795. u16 remainder = 0xFFFF;
  796. u16 xor_mask = 0x0000;
  797. u32 index;
  798. u32 byte_value;
  799. u32 table_index;
  800. u32 crc_bit_width = sizeof(u16) * 8;
  801. /* Divide the message by polynomial, via the table. */
  802. for (index = 0; index < size; index++) {
  803. byte_value = buf[index];
  804. table_index = ((byte_value >> 4) & 0x0F)
  805. ^ (remainder >> (crc_bit_width - 4));
  806. remainder = crc_table[table_index] ^ (remainder << 4);
  807. table_index = (byte_value & 0x0F)
  808. ^ (remainder >> (crc_bit_width - 4));
  809. remainder = crc_table[table_index] ^ (remainder << 4);
  810. }
  811. /* Perform the final remainder CRC. */
  812. return remainder ^ xor_mask;
  813. }
  814. u16 ccitt_Table[] = {
  815. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  816. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  817. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  818. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  819. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  820. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  821. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  822. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  823. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  824. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  825. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  826. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  827. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  828. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  829. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  830. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  831. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  832. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  833. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  834. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  835. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  836. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  837. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  838. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  839. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  840. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  841. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  842. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  843. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  844. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  845. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  846. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  847. };
  848. /*******************************************************************************
  849. * FUNCTION: crc_ccitt_calculate
  850. *
  851. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  852. *
  853. * RETURN:
  854. * CRC calculation result
  855. *
  856. * PARAMETERS:
  857. * *q - pointer to the data array to be calculated
  858. * len - size of data array
  859. ******************************************************************************/
  860. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  861. {
  862. unsigned short crc = 0xffff;
  863. while (len-- > 0)
  864. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  865. return crc;
  866. }
  867. /*******************************************************************************
  868. * FUNCTION: pt_pip2_cmd_calculate_crc
  869. *
  870. * SUMMARY: Calculate the CRC of a command packet
  871. *
  872. * RETURN: void
  873. *
  874. * PARAMETERS:
  875. * *cmd - pointer to command data
  876. * extra_bytes - Extra bytes included in command length
  877. ******************************************************************************/
  878. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  879. u8 extra_bytes)
  880. {
  881. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  882. unsigned short crc;
  883. buf[0] = cmd->len & 0xff;
  884. buf[1] = (cmd->len & 0xff00) >> 8;
  885. buf[2] = cmd->seq;
  886. buf[3] = cmd->id;
  887. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  888. /* Calculate the CRC for the first 4 bytes above and the data payload */
  889. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  890. cmd->crc[0] = (crc & 0xff00) >> 8;
  891. cmd->crc[1] = (crc & 0xff);
  892. }
  893. /*******************************************************************************
  894. * FUNCTION: pt_pip2_get_next_cmd_seq
  895. *
  896. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  897. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  898. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  899. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  900. * increment, just use the forced value.
  901. *
  902. * RETURN: Next command sequence number [0x08-0x0F]
  903. *
  904. * PARAMETERS:
  905. * *cd - pointer to core data
  906. ******************************************************************************/
  907. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  908. {
  909. #ifdef TTDL_DIAGNOSTICS
  910. if (cd->force_pip2_seq <= 0x07) {
  911. cd->pip2_cmd_tag_seq++;
  912. if (cd->pip2_cmd_tag_seq > 0x0F)
  913. cd->pip2_cmd_tag_seq = 0x08;
  914. } else {
  915. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  916. }
  917. #else
  918. cd->pip2_cmd_tag_seq++;
  919. if (cd->pip2_cmd_tag_seq > 0x0F)
  920. cd->pip2_cmd_tag_seq = 0x08;
  921. #endif
  922. return cd->pip2_cmd_tag_seq;
  923. }
  924. /*
  925. * Following macros are to define the response time (the interval between PIP2
  926. * command finishes sending and INT pin falls). The unit is in microsecond.
  927. * It has different time settings between the solution GPIO polling and Bus
  928. * polling due to the considration for system load.
  929. */
  930. #ifdef PT_POLL_RESP_BY_BUS
  931. #define POLL_RETRY_DEFAULT_INTERVAL 50
  932. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  933. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  934. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  935. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  936. #else
  937. #define POLL_RETRY_DEFAULT_INTERVAL 20
  938. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  939. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  940. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  941. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  942. #endif
  943. /*
  944. * id: the command id defined in PIP2
  945. * response_len: the (maximum) length of response.
  946. * response_time_min: minimum response time in microsecond
  947. * response_time_max: maximum response time in microsecond
  948. */
  949. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  950. {.id = PIP2_CMD_ID_PING,
  951. .response_len = 255,
  952. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  953. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  954. {.id = PIP2_CMD_ID_STATUS,
  955. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  956. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  957. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  958. {.id = PIP2_CMD_ID_CTRL,
  959. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  960. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  961. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  962. {.id = PIP2_CMD_ID_CONFIG,
  963. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  964. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  965. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  966. {.id = PIP2_CMD_ID_CLEAR,
  967. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  968. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  969. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  970. {.id = PIP2_CMD_ID_RESET,
  971. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  972. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  973. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  974. {.id = PIP2_CMD_ID_VERSION,
  975. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  976. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  977. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  978. {.id = PIP2_CMD_ID_FILE_OPEN,
  979. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  980. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  981. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  982. {.id = PIP2_CMD_ID_FILE_CLOSE,
  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_FILE_READ,
  987. .response_len = 255,
  988. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  989. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  990. {.id = PIP2_CMD_ID_FILE_WRITE,
  991. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  992. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  993. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  994. {.id = PIP2_CMD_ID_FILE_IOCTL,
  995. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  996. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  997. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  998. {.id = PIP2_CMD_ID_FLASH_INFO,
  999. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1000. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1001. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1002. {.id = PIP2_CMD_ID_EXECUTE,
  1003. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1004. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1011. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1014. {.id = PIP2_CMD_ID_READ_GPIO,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1016. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_EXECUTE_SCAN,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1022. {.id = PIP2_CMD_SET_PARAMETER,
  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_GET_PARAMETER,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_SET_DDI_REG,
  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_GET_DDI_REG,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_ID_END,
  1039. .response_len = 255,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1042. };
  1043. /*******************************************************************************
  1044. * FUNCTION: pt_pip2_get_cmd_response_len
  1045. *
  1046. * SUMMARY: Gets the expected response length based on the command ID
  1047. *
  1048. * RETURN: Expected response length
  1049. *
  1050. * PARAMETERS:
  1051. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1052. ******************************************************************************/
  1053. static int pt_pip2_get_cmd_response_len(u8 id)
  1054. {
  1055. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1056. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1057. p++;
  1058. if (p->id != PIP2_CMD_ID_END)
  1059. return p->response_len;
  1060. else
  1061. return -EPERM;
  1062. }
  1063. /*******************************************************************************
  1064. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1065. *
  1066. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1067. * finishes sending and INT pin falls) based on the command ID
  1068. *
  1069. * RETURN: Estimated minimum response time in microsecond
  1070. *
  1071. * PARAMETERS:
  1072. * id - Command ID
  1073. ******************************************************************************/
  1074. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1075. {
  1076. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1077. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1078. p++;
  1079. if (p->id != PIP2_CMD_ID_END)
  1080. return p->response_time_min;
  1081. else
  1082. return PIP2_RESP_DEFAULT_TIME_MIN;
  1083. }
  1084. /*******************************************************************************
  1085. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1086. *
  1087. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1088. * finishes sending and INT pin falls) based on the command ID
  1089. *
  1090. * RETURN: Estimated maximum response time in microsecond
  1091. *
  1092. * PARAMETERS:
  1093. * id - Command ID
  1094. ******************************************************************************/
  1095. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1096. {
  1097. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1098. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1099. p++;
  1100. if (p->id != PIP2_CMD_ID_END)
  1101. return p->response_time_max;
  1102. else
  1103. return PIP2_RESP_DEFAULT_TIME_MAX;
  1104. }
  1105. /*******************************************************************************
  1106. * FUNCTION: pt_pip2_validate_response
  1107. *
  1108. * SUMMARY: Validate the response of PIP2 command.
  1109. *
  1110. * RETURN:
  1111. * 0 = success
  1112. * !0 = failure
  1113. *
  1114. * PARAMETERS:
  1115. * *cd - pointer to core data
  1116. * *pip2_cmd - pointer to PIP2 command to send
  1117. * *read_buf - pointer to response buffer
  1118. * actual_read_len - actual read length of the response
  1119. ******************************************************************************/
  1120. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1121. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1122. u16 actual_read_len)
  1123. {
  1124. int rc = 0;
  1125. u8 response_seq = 0;
  1126. u8 reserved_bits = 0;
  1127. u8 cmd_id = 0;
  1128. u8 response_bit = 0;
  1129. unsigned short calc_crc = 0;
  1130. unsigned short resp_crc = 0;
  1131. /* Verify the length of response buffer */
  1132. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1133. pt_debug(cd->dev, DL_ERROR,
  1134. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1135. __func__, pip2_cmd->id, actual_read_len);
  1136. rc = -EINVAL;
  1137. goto exit;
  1138. }
  1139. /* Verify the CRC */
  1140. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1141. resp_crc = read_buf[actual_read_len - 2] << 8;
  1142. resp_crc |= read_buf[actual_read_len - 1];
  1143. if (resp_crc != calc_crc) {
  1144. pt_debug(cd->dev, DL_ERROR,
  1145. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1146. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1147. #ifdef TTDL_DIAGNOSTICS
  1148. cd->pip2_crc_error_count++;
  1149. #endif /* TTDL_DIAGNOSTICS */
  1150. rc = -EINVAL;
  1151. goto exit;
  1152. }
  1153. /* Verify the response bit is set */
  1154. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1155. if (!response_bit) {
  1156. pt_debug(cd->dev, DL_ERROR,
  1157. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1158. __func__, pip2_cmd->id, response_bit);
  1159. rc = -EINVAL;
  1160. goto exit;
  1161. }
  1162. /* Verify the command ID matches from command to response */
  1163. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1164. if (cmd_id != pip2_cmd->id) {
  1165. pt_debug(cd->dev, DL_ERROR,
  1166. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1167. __func__, pip2_cmd->id, cmd_id);
  1168. rc = -EINVAL;
  1169. goto exit;
  1170. }
  1171. /* Verify the SEQ number matches from command to response */
  1172. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1173. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1174. pt_debug(cd->dev, DL_ERROR,
  1175. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1176. __func__, pip2_cmd->id,
  1177. pip2_cmd->seq, response_seq);
  1178. rc = -EINVAL;
  1179. goto exit;
  1180. }
  1181. /* Verify the reserved bits are 0 */
  1182. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1183. if (reserved_bits)
  1184. pt_debug(cd->dev, DL_WARN,
  1185. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1186. __func__, pip2_cmd->id, reserved_bits);
  1187. exit:
  1188. if (rc)
  1189. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1190. "PIP RSP:");
  1191. return rc;
  1192. }
  1193. /*******************************************************************************
  1194. * FUNCTION: pt_hid_output_validate_bl_response
  1195. *
  1196. * SUMMARY: Validate the response of bootloader command.
  1197. *
  1198. * RETURN:
  1199. * 0 = success
  1200. * !0 = failure
  1201. *
  1202. * PARAMETERS:
  1203. * *cd - pointer to core data
  1204. * *hid_output - pointer to hid output data structure
  1205. ******************************************************************************/
  1206. static int pt_hid_output_validate_bl_response(
  1207. struct pt_core_data *cd,
  1208. struct pt_hid_output *hid_output)
  1209. {
  1210. u16 size;
  1211. u16 crc;
  1212. u8 status;
  1213. size = get_unaligned_le16(&cd->response_buf[0]);
  1214. if (hid_output->reset_expected && !size)
  1215. return 0;
  1216. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1217. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1218. pt_debug(cd->dev, DL_ERROR,
  1219. "%s: BL output response, wrong report_id\n", __func__);
  1220. return -EPROTO;
  1221. }
  1222. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1223. pt_debug(cd->dev, DL_ERROR,
  1224. "%s: BL output response, wrong SOP\n", __func__);
  1225. return -EPROTO;
  1226. }
  1227. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1228. pt_debug(cd->dev, DL_ERROR,
  1229. "%s: BL output response, wrong EOP\n", __func__);
  1230. return -EPROTO;
  1231. }
  1232. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1233. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1234. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1235. pt_debug(cd->dev, DL_ERROR,
  1236. "%s: BL output response, wrong CRC 0x%X\n",
  1237. __func__, crc);
  1238. return -EPROTO;
  1239. }
  1240. status = cd->response_buf[5];
  1241. if (status) {
  1242. pt_debug(cd->dev, DL_ERROR,
  1243. "%s: BL output response, ERROR:%d\n",
  1244. __func__, status);
  1245. return -EPROTO;
  1246. }
  1247. return 0;
  1248. }
  1249. /*******************************************************************************
  1250. * FUNCTION: pt_hid_output_validate_app_response
  1251. *
  1252. * SUMMARY: Validate the response of application command.
  1253. *
  1254. * RETURN:
  1255. * 0 = success
  1256. * !0 = failure
  1257. *
  1258. * PARAMETERS:
  1259. * *cd - pointer to core data
  1260. * *hid_output - pointer to hid output data structure
  1261. ******************************************************************************/
  1262. static int pt_hid_output_validate_app_response(
  1263. struct pt_core_data *cd,
  1264. struct pt_hid_output *hid_output)
  1265. {
  1266. int command_code;
  1267. u16 size;
  1268. size = get_unaligned_le16(&cd->response_buf[0]);
  1269. if (hid_output->reset_expected && !size)
  1270. return 0;
  1271. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1272. != PT_PIP_NON_HID_RESPONSE_ID) {
  1273. pt_debug(cd->dev, DL_ERROR,
  1274. "%s: APP output response, wrong report_id\n", __func__);
  1275. return -EPROTO;
  1276. }
  1277. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1278. & PIP1_RESP_COMMAND_ID_MASK;
  1279. if (command_code != hid_output->command_code) {
  1280. pt_debug(cd->dev, DL_ERROR,
  1281. "%s: APP output response, wrong command_code:%X\n",
  1282. __func__, command_code);
  1283. return -EPROTO;
  1284. }
  1285. return 0;
  1286. }
  1287. /*******************************************************************************
  1288. * FUNCTION: pt_check_set_parameter
  1289. *
  1290. * SUMMARY: Check command input and response for Set Parameter command.And
  1291. * store the parameter to the list for resume work if pass the check.
  1292. *
  1293. * PARAMETERS:
  1294. * *cd - pointer to core data
  1295. * *hid_output - pointer to hid output data structure
  1296. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1297. ******************************************************************************/
  1298. static void pt_check_set_parameter(struct pt_core_data *cd,
  1299. struct pt_hid_output *hid_output, bool raw)
  1300. {
  1301. u8 *param_buf;
  1302. u32 param_value = 0;
  1303. u8 param_size;
  1304. u8 param_id;
  1305. int i = 0;
  1306. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1307. return;
  1308. /* Check command input for Set Parameter command */
  1309. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1310. && !memcmp(&hid_output->write_buf[0],
  1311. &cd->hid_desc.output_register,
  1312. sizeof(cd->hid_desc.output_register))
  1313. && hid_output->write_buf[4] ==
  1314. PT_PIP_NON_HID_COMMAND_ID
  1315. && hid_output->write_buf[6] ==
  1316. PIP1_CMD_ID_SET_PARAM)
  1317. param_buf = &hid_output->write_buf[7];
  1318. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1319. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1320. && hid_output->write_length >= 3
  1321. && hid_output->write_length <= 6)
  1322. param_buf = &hid_output->write_buf[0];
  1323. else
  1324. return;
  1325. /* Get parameter ID, size and value */
  1326. param_id = param_buf[0];
  1327. param_size = param_buf[1];
  1328. if (param_size > 4) {
  1329. pt_debug(cd->dev, DL_ERROR,
  1330. "%s: Invalid parameter size\n", __func__);
  1331. return;
  1332. }
  1333. param_buf = &param_buf[2];
  1334. while (i < param_size)
  1335. param_value += *(param_buf++) << (8 * i++);
  1336. /* Check command response for Set Parameter command */
  1337. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1338. || (cd->response_buf[4] &
  1339. PIP1_RESP_COMMAND_ID_MASK) !=
  1340. PIP1_CMD_ID_SET_PARAM
  1341. || cd->response_buf[5] != param_id
  1342. || cd->response_buf[6] != param_size) {
  1343. pt_debug(cd->dev, DL_ERROR,
  1344. "%s: Set Parameter command not successful\n",
  1345. __func__);
  1346. return;
  1347. }
  1348. pt_add_parameter(cd, param_id, param_value, param_size);
  1349. }
  1350. /*******************************************************************************
  1351. * FUNCTION: pt_check_command
  1352. *
  1353. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1354. * called here to check output command and store parameter to the list.
  1355. *
  1356. * PARAMETERS:
  1357. * *cd - pointer to core data
  1358. * *hid_output - pointer to hid output data structure
  1359. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1360. ******************************************************************************/
  1361. static void pt_check_command(struct pt_core_data *cd,
  1362. struct pt_hid_output *hid_output, bool raw)
  1363. {
  1364. pt_check_set_parameter(cd, hid_output, raw);
  1365. }
  1366. /*******************************************************************************
  1367. * FUNCTION: pt_hid_output_validate_response
  1368. *
  1369. * SUMMARY: Validate the response of application or bootloader command.
  1370. *
  1371. * RETURN:
  1372. * 0 = success
  1373. * !0 = failure
  1374. *
  1375. * PARAMETERS:
  1376. * *cd - pointer to core data
  1377. * *hid_output - pointer to hid output data structure
  1378. ******************************************************************************/
  1379. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1380. struct pt_hid_output *hid_output)
  1381. {
  1382. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1383. return pt_hid_output_validate_bl_response(cd, hid_output);
  1384. return pt_hid_output_validate_app_response(cd, hid_output);
  1385. }
  1386. /*******************************************************************************
  1387. * FUNCTION: pt_hid_send_output_user_
  1388. *
  1389. * SUMMARY: Blindly send user data to the DUT.
  1390. *
  1391. * RETURN:
  1392. * 0 = success
  1393. * !0 = failure
  1394. *
  1395. * PARAMETERS:
  1396. * *cd - pointer to core data
  1397. * *hid_output - pointer to the command to send
  1398. ******************************************************************************/
  1399. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1400. struct pt_hid_output *hid_output)
  1401. {
  1402. int rc = 0;
  1403. int cmd;
  1404. if (!hid_output->length || !hid_output->write_buf)
  1405. return -EINVAL;
  1406. if (cd->pip2_prot_active) {
  1407. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1408. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1409. } else
  1410. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1411. pt_debug(cd->dev, DL_INFO,
  1412. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1413. __func__, hid_output->length, cmd);
  1414. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1415. hid_output->length, ">>> User CMD");
  1416. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1417. hid_output->write_buf, NULL);
  1418. if (rc)
  1419. pt_debug(cd->dev, DL_ERROR,
  1420. "%s: Fail pt_adap_transfer\n", __func__);
  1421. return rc;
  1422. }
  1423. /*******************************************************************************
  1424. * FUNCTION: pt_hid_send_output_user_and_wait_
  1425. *
  1426. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1427. *
  1428. * RETURN:
  1429. * 0 = success
  1430. * !0 = failure
  1431. *
  1432. * PARAMETERS:
  1433. * *cd - pointer to core data
  1434. * *hid_output - pointer to the command to send
  1435. ******************************************************************************/
  1436. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1437. struct pt_hid_output *hid_output)
  1438. {
  1439. int rc = 0;
  1440. int t;
  1441. mutex_lock(&cd->system_lock);
  1442. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1443. mutex_unlock(&cd->system_lock);
  1444. rc = pt_hid_send_output_user_(cd, hid_output);
  1445. if (rc)
  1446. goto error;
  1447. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1448. msecs_to_jiffies(cd->pip_cmd_timeout));
  1449. if (IS_TMO(t)) {
  1450. #ifdef TTDL_DIAGNOSTICS
  1451. cd->bus_transmit_error_count++;
  1452. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1453. #endif /* TTDL_DIAGNOSTICS */
  1454. pt_debug(cd->dev, DL_ERROR,
  1455. "%s: HID output cmd execution timed out\n",
  1456. __func__);
  1457. rc = -ETIME;
  1458. goto error;
  1459. }
  1460. pt_check_command(cd, hid_output, true);
  1461. goto exit;
  1462. error:
  1463. mutex_lock(&cd->system_lock);
  1464. cd->hid_cmd_state = 0;
  1465. mutex_unlock(&cd->system_lock);
  1466. exit:
  1467. return rc;
  1468. }
  1469. /*******************************************************************************
  1470. * FUNCTION: pt_check_irq_asserted
  1471. *
  1472. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1473. * the FW can hold the INT line low ~150us after the read is complete.
  1474. * NOTE: if irq_stat is not defined this function will return false
  1475. *
  1476. * RETURN:
  1477. * true = IRQ asserted
  1478. * false = IRQ not asserted
  1479. *
  1480. * PARAMETERS:
  1481. * *cd - pointer to core data
  1482. ******************************************************************************/
  1483. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1484. {
  1485. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1486. /*
  1487. * Workaround for FW defect, CDT165308
  1488. * bl_launch app creates a glitch in IRQ line
  1489. */
  1490. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1491. && cd->cpdata->irq_stat) {
  1492. /*
  1493. * in X1S panel and GC1546 panel, the width for the INT
  1494. * glitch is about 4us,the normal INT width of response
  1495. * will last more than 200us, so use 10us delay
  1496. * for distinguish the glitch the normal INT is enough.
  1497. */
  1498. udelay(10);
  1499. }
  1500. #endif
  1501. if (cd->cpdata->irq_stat) {
  1502. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1503. == PT_IRQ_ASSERTED_VALUE) {
  1504. /* Debounce to allow FW to release INT */
  1505. usleep_range(100, 200);
  1506. }
  1507. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1508. == PT_IRQ_ASSERTED_VALUE)
  1509. return true;
  1510. else
  1511. return false;
  1512. }
  1513. return true;
  1514. }
  1515. /*******************************************************************************
  1516. * FUNCTION: pt_flush_bus
  1517. *
  1518. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1519. * Used if IRQ is found to be stuck low
  1520. *
  1521. * RETURN: Length of bytes read from bus
  1522. *
  1523. * PARAMETERS:
  1524. * *cd - pointer to core data
  1525. * flush_type - type of flush
  1526. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1527. * - PT_FLUSH_BUS_FULL_256_READ
  1528. * *read_buf - pointer to store read data
  1529. ******************************************************************************/
  1530. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1531. u8 flush_type, u8 *read_buf)
  1532. {
  1533. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1534. u16 pip_len;
  1535. int bytes_read;
  1536. int rc = 0;
  1537. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1538. rc = pt_adap_read_default(cd, buf, 2);
  1539. if (rc) {
  1540. bytes_read = 0;
  1541. goto exit;
  1542. }
  1543. pip_len = get_unaligned_le16(&buf[0]);
  1544. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1545. #ifdef TTDL_DIAGNOSTICS
  1546. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1547. #endif
  1548. bytes_read = 2;
  1549. pt_debug(cd->dev, DL_INFO,
  1550. "%s: Empty buf detected - len=0x%04X\n",
  1551. __func__, pip_len);
  1552. } else if (pip_len == 0) {
  1553. bytes_read = 0;
  1554. pt_debug(cd->dev, DL_INFO,
  1555. "%s: Sentinel detected\n", __func__);
  1556. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1557. pt_debug(cd->dev, DL_ERROR,
  1558. "%s: Illegal len=0x%04x, force %d byte read\n",
  1559. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1560. rc = pt_adap_read_default(cd, buf,
  1561. PT_MAX_PIP2_MSG_SIZE);
  1562. if (!rc)
  1563. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1564. else
  1565. bytes_read = 0;
  1566. } else {
  1567. pt_debug(cd->dev, DL_INFO,
  1568. "%s: Flush read of %d bytes...\n",
  1569. __func__, pip_len);
  1570. rc = pt_adap_read_default(cd, buf, pip_len);
  1571. if (!rc)
  1572. bytes_read = pip_len;
  1573. else
  1574. bytes_read = 0;
  1575. }
  1576. } else {
  1577. pt_debug(cd->dev, DL_INFO,
  1578. "%s: Forced flush of max %d bytes...\n",
  1579. __func__, PT_MAX_PIP2_MSG_SIZE);
  1580. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1581. if (!rc)
  1582. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1583. else
  1584. bytes_read = 0;
  1585. }
  1586. if (read_buf && (bytes_read > 3))
  1587. memcpy(read_buf, buf, bytes_read);
  1588. exit:
  1589. return bytes_read;
  1590. }
  1591. /*******************************************************************************
  1592. * FUNCTION: pt_flush_bus_if_irq_asserted
  1593. *
  1594. * SUMMARY: This function will flush the active bus if the INT is found to be
  1595. * asserted.
  1596. *
  1597. * RETURN: bytes cleared from bus
  1598. *
  1599. * PARAMETERS:
  1600. * *cd - pointer the core data structure
  1601. * flush_type - type of flush
  1602. * - PT_FLUSH_BUS_BASED_ON_LEN
  1603. * - PT_FLUSH_BUS_FULL_256_READ
  1604. ******************************************************************************/
  1605. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1606. {
  1607. int count = 0;
  1608. int bytes_read = 0;
  1609. while (pt_check_irq_asserted(cd) && count < 5) {
  1610. count++;
  1611. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1612. if (bytes_read) {
  1613. pt_debug(cd->dev, DL_WARN,
  1614. "%s: Cleared %d bytes off bus\n",
  1615. __func__, bytes_read);
  1616. }
  1617. }
  1618. if (pt_check_irq_asserted(cd)) {
  1619. pt_debug(cd->dev, DL_ERROR,
  1620. "%s: IRQ still asserted, %d bytes read\n",
  1621. __func__, bytes_read);
  1622. } else {
  1623. pt_debug(cd->dev, DL_INFO,
  1624. "%s: IRQ cleared, %d bytes read\n",
  1625. __func__, bytes_read);
  1626. }
  1627. return bytes_read;
  1628. }
  1629. /*******************************************************************************
  1630. * FUNCTION: pt_hid_send_output_
  1631. *
  1632. * SUMMARY: Send a touch application command to the DUT
  1633. *
  1634. * RETURN:
  1635. * 0 = success
  1636. * !0 = failure
  1637. *
  1638. * PARAMETERS:
  1639. * *cd - pointer to core data
  1640. * *hid_output - pointer to the command to send
  1641. ******************************************************************************/
  1642. static int pt_hid_send_output_(struct pt_core_data *cd,
  1643. struct pt_hid_output *hid_output)
  1644. {
  1645. int rc = 0;
  1646. u8 *cmd;
  1647. u16 length;
  1648. u16 crc;
  1649. u8 report_id;
  1650. u8 cmd_offset = 0;
  1651. u8 cmd_allocated = 0;
  1652. switch (hid_output->cmd_type) {
  1653. case PIP1_CMD_TYPE_FW:
  1654. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1655. length = 5;
  1656. break;
  1657. case PIP1_CMD_TYPE_BL:
  1658. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1659. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1660. break;
  1661. default:
  1662. return -EINVAL;
  1663. }
  1664. length += hid_output->write_length;
  1665. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1666. cmd = kzalloc(length + 2, GFP_KERNEL);
  1667. if (!cmd)
  1668. return -ENOMEM;
  1669. cmd_allocated = 1;
  1670. } else {
  1671. cmd = cd->cmd_buf;
  1672. }
  1673. /* Set Output register */
  1674. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1675. sizeof(cd->hid_desc.output_register));
  1676. cmd_offset += sizeof(cd->hid_desc.output_register);
  1677. cmd[cmd_offset++] = LOW_BYTE(length);
  1678. cmd[cmd_offset++] = HI_BYTE(length);
  1679. cmd[cmd_offset++] = report_id;
  1680. cmd[cmd_offset++] = 0x0; /* reserved */
  1681. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1682. cmd[cmd_offset++] = PIP1_BL_SOP;
  1683. cmd[cmd_offset++] = hid_output->command_code;
  1684. /* Set Data Length for bootloader */
  1685. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1686. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1687. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1688. }
  1689. /* Set Data */
  1690. if (hid_output->write_length && hid_output->write_buf) {
  1691. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1692. hid_output->write_length);
  1693. cmd_offset += hid_output->write_length;
  1694. }
  1695. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1696. crc = _pt_compute_crc(&cmd[6],
  1697. hid_output->write_length + 4);
  1698. cmd[cmd_offset++] = LOW_BYTE(crc);
  1699. cmd[cmd_offset++] = HI_BYTE(crc);
  1700. cmd[cmd_offset++] = PIP1_BL_EOP;
  1701. }
  1702. pt_debug(cd->dev, DL_INFO,
  1703. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1704. __func__, length + 2, hid_output->command_code);
  1705. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1706. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1707. if (rc)
  1708. pt_debug(cd->dev, DL_ERROR,
  1709. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1710. if (cmd_allocated)
  1711. kfree(cmd);
  1712. return rc;
  1713. }
  1714. /*******************************************************************************
  1715. * FUNCTION: pt_pip1_send_output_and_wait_
  1716. *
  1717. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1718. *
  1719. * RETURN:
  1720. * 0 = success
  1721. * !0 = failure
  1722. *
  1723. * PARAMETERS:
  1724. * *cd - pointer to core data
  1725. * *hid_output - pointer to the command to send
  1726. ******************************************************************************/
  1727. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1728. struct pt_hid_output *hid_output)
  1729. {
  1730. int rc = 0;
  1731. int t;
  1732. u16 timeout_ms;
  1733. mutex_lock(&cd->system_lock);
  1734. cd->hid_cmd_state = hid_output->command_code + 1;
  1735. mutex_unlock(&cd->system_lock);
  1736. if (hid_output->timeout_ms)
  1737. timeout_ms = hid_output->timeout_ms;
  1738. else
  1739. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1740. rc = pt_hid_send_output_(cd, hid_output);
  1741. if (rc)
  1742. goto error;
  1743. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1744. msecs_to_jiffies(timeout_ms));
  1745. if (IS_TMO(t)) {
  1746. #ifdef TTDL_DIAGNOSTICS
  1747. cd->bus_transmit_error_count++;
  1748. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1749. #endif /* TTDL_DIAGNOSTICS */
  1750. pt_debug(cd->dev, DL_ERROR,
  1751. "%s: HID output cmd execution timed out (%dms)\n",
  1752. __func__, timeout_ms);
  1753. rc = -ETIME;
  1754. goto error;
  1755. }
  1756. if (!hid_output->novalidate)
  1757. rc = pt_hid_output_validate_response(cd, hid_output);
  1758. pt_check_command(cd, hid_output, false);
  1759. goto exit;
  1760. error:
  1761. mutex_lock(&cd->system_lock);
  1762. cd->hid_cmd_state = 0;
  1763. mutex_unlock(&cd->system_lock);
  1764. exit:
  1765. return rc;
  1766. }
  1767. /*******************************************************************************
  1768. * FUNCTION: pt_hid_output_user_cmd_
  1769. *
  1770. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1771. * to the DUT waiting for the response and loading it into the read buffer
  1772. *
  1773. * RETURN:
  1774. * 0 = success
  1775. * !0 = failure
  1776. *
  1777. * PARAMETERS:
  1778. * *cd - pointer to core data
  1779. * read_len - expected read length of the response
  1780. * *read_buf - pointer to where the response will be loaded
  1781. * write_len - length of the write buffer
  1782. * *write_buf - pointer to the write buffer
  1783. * *actual_read_len - pointer to the actual amount of data read back
  1784. ******************************************************************************/
  1785. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1786. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1787. u16 *actual_read_len)
  1788. {
  1789. int rc = 0;
  1790. u16 size;
  1791. struct pt_hid_output hid_output = {
  1792. .length = write_len,
  1793. .write_buf = write_buf,
  1794. };
  1795. #ifdef TTHE_TUNER_SUPPORT
  1796. if (!cd->pip2_send_user_cmd) {
  1797. int command_code = 0;
  1798. int len;
  1799. /* Print up to cmd ID */
  1800. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1801. if (write_len < len)
  1802. len = write_len;
  1803. else
  1804. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1805. & PIP1_CMD_COMMAND_ID_MASK;
  1806. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1807. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1808. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1809. tthe_print(cd, write_buf, len, "CMD=");
  1810. }
  1811. #endif
  1812. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1813. if (rc)
  1814. return rc;
  1815. /* Get the response size from the first 2 bytes in the response */
  1816. size = get_unaligned_le16(&cd->response_buf[0]);
  1817. /* Ensure size is not greater than max buffer size */
  1818. if (size > PT_MAX_PIP2_MSG_SIZE)
  1819. size = PT_MAX_PIP2_MSG_SIZE;
  1820. /* Minimum size to read is the 2 byte len field */
  1821. if (size == 0)
  1822. size = 2;
  1823. if (size > read_len) {
  1824. pt_debug(cd->dev, DL_ERROR,
  1825. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1826. __func__, size, read_len);
  1827. *actual_read_len = 0;
  1828. return -EIO;
  1829. }
  1830. memcpy(read_buf, cd->response_buf, size);
  1831. *actual_read_len = size;
  1832. return 0;
  1833. }
  1834. /*******************************************************************************
  1835. * FUNCTION: pt_hid_output_user_cmd
  1836. *
  1837. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1838. * the DUT.
  1839. *
  1840. * RETURN:
  1841. * 0 = success
  1842. * !0 = failure
  1843. *
  1844. * PARAMETERS:
  1845. * *cd - pointer to core data
  1846. * read_len - length of data to read
  1847. * *read_buf - pointer to store read data
  1848. * write_len - length of data to write
  1849. * *write_buf - pointer to buffer to write
  1850. * *actual_read_len - pointer to store data length actually read
  1851. ******************************************************************************/
  1852. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1853. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1854. u16 *actual_read_len)
  1855. {
  1856. int rc;
  1857. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1858. if (rc < 0) {
  1859. pt_debug(cd->dev, DL_ERROR,
  1860. "%s: fail get exclusive ex=%p own=%p\n",
  1861. __func__, cd->exclusive_dev, cd->dev);
  1862. return rc;
  1863. }
  1864. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1865. write_len, write_buf, actual_read_len);
  1866. if (release_exclusive(cd, cd->dev) < 0)
  1867. pt_debug(cd->dev, DL_ERROR,
  1868. "%s: fail to release exclusive\n", __func__);
  1869. return rc;
  1870. }
  1871. /*******************************************************************************
  1872. * FUNCTION: _pt_request_pip2_send_cmd
  1873. *
  1874. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1875. * interrupt and reads response data to read_buf
  1876. *
  1877. * RETURN:
  1878. * 0 = success
  1879. * !0 = failure
  1880. *
  1881. * PARAMETERS:
  1882. * *dev - pointer to device structure
  1883. * protect - flag to run in protected mode
  1884. * id - ID of PIP command
  1885. * *data - pointer to PIP data payload
  1886. * report_body_len - report length
  1887. * *read_buf - pointer to response buffer
  1888. * *actual_read_len - pointer to response buffer length
  1889. ******************************************************************************/
  1890. static int _pt_request_pip2_send_cmd(struct device *dev,
  1891. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1892. u16 *actual_read_len)
  1893. {
  1894. struct pt_core_data *cd = dev_get_drvdata(dev);
  1895. struct pip2_cmd_structure pip2_cmd;
  1896. int rc = 0;
  1897. int i = 0;
  1898. int j = 0;
  1899. u16 write_len;
  1900. u8 *write_buf = NULL;
  1901. u16 read_len;
  1902. u8 extra_bytes;
  1903. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1904. /* Hard coded register for PIP2.x */
  1905. pip2_cmd.reg[0] = 0x01;
  1906. pip2_cmd.reg[1] = 0x01;
  1907. /*
  1908. * For PIP2.1+ the length field value includes itself:
  1909. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1910. *
  1911. * The overall write length must include only the register:
  1912. * ADD 2: 2 (Register)
  1913. */
  1914. extra_bytes = 6;
  1915. write_len = 2;
  1916. /* PIP2 the CMD ID is a 7bit field */
  1917. if (id > PIP2_CMD_ID_END) {
  1918. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1919. __func__, id);
  1920. rc = -EINVAL;
  1921. goto exit;
  1922. }
  1923. pip2_cmd.len = report_body_len + extra_bytes;
  1924. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1925. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1926. pip2_cmd.data = data;
  1927. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1928. /* Add the command length to the extra bytes based on PIP version */
  1929. write_len += pip2_cmd.len;
  1930. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1931. __func__, pip2_cmd.len, write_len);
  1932. write_buf = kzalloc(write_len, GFP_KERNEL);
  1933. if (write_buf == NULL) {
  1934. rc = -ENOMEM;
  1935. goto exit;
  1936. }
  1937. write_buf[i++] = pip2_cmd.reg[0];
  1938. write_buf[i++] = pip2_cmd.reg[1];
  1939. write_buf[i++] = pip2_cmd.len & 0xff;
  1940. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1941. write_buf[i++] = pip2_cmd.seq;
  1942. write_buf[i++] = pip2_cmd.id;
  1943. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1944. write_buf[j] = pip2_cmd.data[j-i];
  1945. write_buf[j++] = pip2_cmd.crc[0];
  1946. write_buf[j++] = pip2_cmd.crc[1];
  1947. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1948. if (read_len < 0)
  1949. read_len = 255;
  1950. pt_debug(dev, DL_INFO,
  1951. "%s cmd_id[0x%02X] expected response length:%d ",
  1952. __func__, pip2_cmd.id, read_len);
  1953. /*
  1954. * All PIP2 commands come through this function.
  1955. * Set flag for PIP2.x interface to allow response parsing to know
  1956. * how to decode the protocol header.
  1957. */
  1958. mutex_lock(&cd->system_lock);
  1959. cd->pip2_prot_active = true;
  1960. cd->pip2_send_user_cmd = true;
  1961. mutex_unlock(&cd->system_lock);
  1962. if (protect == PT_CORE_CMD_PROTECTED)
  1963. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1964. write_len, write_buf, actual_read_len);
  1965. else {
  1966. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1967. write_len, write_buf, actual_read_len);
  1968. }
  1969. if (rc) {
  1970. pt_debug(dev, DL_ERROR,
  1971. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1972. __func__, rc);
  1973. goto exit;
  1974. }
  1975. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1976. *actual_read_len);
  1977. exit:
  1978. mutex_lock(&cd->system_lock);
  1979. cd->pip2_prot_active = false;
  1980. cd->pip2_send_user_cmd = false;
  1981. mutex_unlock(&cd->system_lock);
  1982. kfree(write_buf);
  1983. return rc;
  1984. }
  1985. /*******************************************************************************
  1986. * FUNCTION: _pt_pip2_send_cmd_no_int
  1987. *
  1988. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1989. * reads response data to read_buf if response is available.
  1990. *
  1991. * NOTE:
  1992. * Interrupt MUST be disabled before to call this function.
  1993. *
  1994. * RETURN:
  1995. * 0 = success
  1996. * !0 = failure
  1997. *
  1998. * PARAMETERS:
  1999. * *dev - pointer to device structure
  2000. * protect - flag to run in protected mode
  2001. * id - ID of PIP command
  2002. * *data - pointer to PIP data payload
  2003. * report_body_len - report length
  2004. * *read_buf - pointer to response buffer
  2005. * *actual_read_len - pointer to response buffer length
  2006. ******************************************************************************/
  2007. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2008. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2009. u16 *actual_read_len)
  2010. {
  2011. int max_retry = 0;
  2012. int retry = 0;
  2013. int rc = 0;
  2014. int i = 0;
  2015. int j = 0;
  2016. u16 write_len;
  2017. u8 *write_buf = NULL;
  2018. u16 read_len;
  2019. u16 size = 0;
  2020. u8 response_seq = 0;
  2021. u8 extra_bytes;
  2022. u32 retry_interval = 0;
  2023. u32 retry_total_time = 0;
  2024. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2025. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2026. struct pt_core_data *cd = dev_get_drvdata(dev);
  2027. struct pip2_cmd_structure pip2_cmd;
  2028. if (protect == PT_CORE_CMD_PROTECTED) {
  2029. rc = request_exclusive(cd,
  2030. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2031. if (rc < 0) {
  2032. pt_debug(cd->dev, DL_ERROR,
  2033. "%s: fail get exclusive ex=%p own=%p\n",
  2034. __func__, cd->exclusive_dev, cd->dev);
  2035. return rc;
  2036. }
  2037. }
  2038. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2039. /* Hard coded register for PIP2.x */
  2040. pip2_cmd.reg[0] = 0x01;
  2041. pip2_cmd.reg[1] = 0x01;
  2042. /*
  2043. * For PIP2.1+ the length field value includes itself:
  2044. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2045. *
  2046. * The overall write length must include only the register:
  2047. * ADD 2: 2 (Register)
  2048. */
  2049. extra_bytes = 6;
  2050. write_len = 2;
  2051. pip2_cmd.len = report_body_len + extra_bytes;
  2052. pip2_cmd.id = id;
  2053. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2054. pip2_cmd.data = data;
  2055. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2056. /* Add the command length to the extra bytes based on PIP version */
  2057. write_len += pip2_cmd.len;
  2058. write_buf = kzalloc(write_len, GFP_KERNEL);
  2059. if (write_buf == NULL) {
  2060. rc = -ENOMEM;
  2061. goto exit;
  2062. }
  2063. write_buf[i++] = pip2_cmd.reg[0];
  2064. write_buf[i++] = pip2_cmd.reg[1];
  2065. write_buf[i++] = pip2_cmd.len & 0xff;
  2066. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2067. write_buf[i++] = pip2_cmd.seq;
  2068. write_buf[i++] = pip2_cmd.id;
  2069. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2070. write_buf[j] = pip2_cmd.data[j-i];
  2071. write_buf[j++] = pip2_cmd.crc[0];
  2072. write_buf[j++] = pip2_cmd.crc[1];
  2073. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2074. if (read_len < 0)
  2075. read_len = 255;
  2076. pt_debug(dev, DL_INFO,
  2077. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2078. __func__, pip2_cmd.id, read_len);
  2079. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2080. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2081. if (rc) {
  2082. pt_debug(dev, DL_ERROR,
  2083. "%s: SPI write Error = %d\n",
  2084. __func__, rc);
  2085. goto exit;
  2086. }
  2087. #ifdef PT_POLL_RESP_BY_BUS
  2088. /*
  2089. * Frequent bus read can increase system load obviously. The expected
  2090. * first bus read should be valid and timely. The tollerance for
  2091. * usleep_range should be limited. The minimum response delay (between
  2092. * command finishes sending and INT pin falls) is less than 50
  2093. * microseconds. So the 10 microseconds should be maximum tollerance
  2094. * with the consideration that the unit to calculate the response delay
  2095. * is 10 microseconds and more precise is not necessary. Every
  2096. * additional 10 microseconds only contribute less than 3 milliseconds
  2097. * for whole BL.
  2098. */
  2099. usleep_range(resp_time_min, resp_time_min+10);
  2100. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2101. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2102. rc = pt_adap_read_default(cd, read_buf, read_len);
  2103. if (rc) {
  2104. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2105. __func__, rc);
  2106. break;
  2107. }
  2108. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2109. size = get_unaligned_le16(&read_buf[0]);
  2110. if ((size <= read_len) &&
  2111. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2112. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2113. break;
  2114. }
  2115. /*
  2116. * To reduce the bus and system load, increase the sleep
  2117. * step gradually:
  2118. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2119. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2120. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2121. */
  2122. retry++;
  2123. if (retry < 20) {
  2124. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2125. usleep_range(retry_interval,
  2126. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2127. } else if (retry < 40) {
  2128. retry_interval += 1000;
  2129. usleep_range(retry_interval, retry_interval + 1000);
  2130. } else {
  2131. retry_interval += 50000;
  2132. msleep(retry_interval/1000);
  2133. }
  2134. retry_total_time += retry_interval;
  2135. }
  2136. #else
  2137. /*
  2138. * Frequent GPIO read will not increase CPU/system load heavily if the
  2139. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2140. * fixed interval: 20 us.
  2141. */
  2142. usleep_range(resp_time_min, resp_time_min+10);
  2143. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2144. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2145. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2146. rc = pt_adap_read_default(cd, read_buf, read_len);
  2147. size = get_unaligned_le16(&read_buf[0]);
  2148. if (rc)
  2149. pt_debug(dev, DL_ERROR,
  2150. "%s: SPI read Error = %d\n",
  2151. __func__, rc);
  2152. else if (size > read_len) {
  2153. pt_debug(cd->dev, DL_ERROR,
  2154. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2155. __func__, size, read_len);
  2156. rc = -EIO;
  2157. }
  2158. break;
  2159. }
  2160. /*
  2161. * Poll GPIO with fixed interval 20 us, and tollerance is
  2162. * limited to 10 us to speed up the process.
  2163. */
  2164. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2165. usleep_range(retry_interval, retry_interval+10);
  2166. retry_total_time += retry_interval;
  2167. }
  2168. #endif
  2169. *actual_read_len = size;
  2170. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2171. pt_debug(dev, DL_ERROR,
  2172. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2173. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2174. *actual_read_len = 0;
  2175. rc = -EINVAL;
  2176. }
  2177. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2178. "<<< NO_INT Read");
  2179. exit:
  2180. kfree(write_buf);
  2181. if (protect == PT_CORE_CMD_PROTECTED) {
  2182. if (release_exclusive(cd, cd->dev) < 0)
  2183. pt_debug(cd->dev, DL_ERROR,
  2184. "%s: fail to release exclusive\n", __func__);
  2185. }
  2186. return rc;
  2187. }
  2188. /*******************************************************************************
  2189. * FUNCTION: pt_pip_null_
  2190. *
  2191. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2192. * This function is used by watchdog to check if the fw corrupts.
  2193. *
  2194. * RETURN:
  2195. * 0 = success
  2196. * !0 = failure
  2197. *
  2198. * PARAMETERS:
  2199. * *cd - pointer to core data
  2200. ******************************************************************************/
  2201. static int pt_pip_null_(struct pt_core_data *cd)
  2202. {
  2203. struct pt_hid_output hid_output = {
  2204. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2205. };
  2206. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2207. }
  2208. /*******************************************************************************
  2209. * FUNCTION: pt_pip_null
  2210. *
  2211. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2212. *
  2213. * RETURN:
  2214. * 0 = success
  2215. * !0 = failure
  2216. *
  2217. * PARAMETERS:
  2218. * *cd - pointer to core data
  2219. ******************************************************************************/
  2220. static int pt_pip_null(struct pt_core_data *cd)
  2221. {
  2222. int rc;
  2223. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2224. if (rc < 0) {
  2225. pt_debug(cd->dev, DL_ERROR,
  2226. "%s: fail get exclusive ex=%p own=%p\n",
  2227. __func__, cd->exclusive_dev, cd->dev);
  2228. return rc;
  2229. }
  2230. rc = pt_pip_null_(cd);
  2231. if (release_exclusive(cd, cd->dev) < 0)
  2232. pt_debug(cd->dev, DL_ERROR,
  2233. "%s: fail to release exclusive\n", __func__);
  2234. return rc;
  2235. }
  2236. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2237. /*******************************************************************************
  2238. * FUNCTION: pt_pip_start_bootloader_
  2239. *
  2240. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2241. *
  2242. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2243. * the WD active could cause this function to fail!
  2244. *
  2245. * RETURN:
  2246. * 0 = success
  2247. * !0 = failure
  2248. *
  2249. * PARAMETERS:
  2250. * *cd - pointer to core data
  2251. ******************************************************************************/
  2252. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2253. {
  2254. int rc = 0;
  2255. struct pt_hid_output hid_output = {
  2256. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2257. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2258. .reset_expected = 1,
  2259. };
  2260. if (cd->watchdog_enabled) {
  2261. pt_debug(cd->dev, DL_WARN,
  2262. "%s: watchdog isn't stopped before enter bl\n",
  2263. __func__);
  2264. goto exit;
  2265. }
  2266. /* Reset startup status after entering BL, new DUT enum required */
  2267. cd->startup_status = STARTUP_STATUS_START;
  2268. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2269. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2270. if (rc) {
  2271. pt_debug(cd->dev, DL_ERROR,
  2272. "%s: Start BL PIP cmd failed. rc = %d\n",
  2273. __func__, rc);
  2274. }
  2275. exit:
  2276. return rc;
  2277. }
  2278. /*******************************************************************************
  2279. * FUNCTION: pt_pip_start_bootloader
  2280. *
  2281. * SUMMARY: Protected function to force DUT to enter the BL
  2282. *
  2283. * RETURN:
  2284. * 0 = success
  2285. * !0 = failure
  2286. *
  2287. * PARAMETERS:
  2288. * *cd - pointer to core data structure
  2289. ******************************************************************************/
  2290. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2291. {
  2292. int rc;
  2293. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2294. if (rc < 0) {
  2295. pt_debug(cd->dev, DL_ERROR,
  2296. "%s: fail get exclusive ex=%p own=%p\n",
  2297. __func__, cd->exclusive_dev, cd->dev);
  2298. return rc;
  2299. }
  2300. rc = pt_pip_start_bootloader_(cd);
  2301. if (release_exclusive(cd, cd->dev) < 0)
  2302. pt_debug(cd->dev, DL_ERROR,
  2303. "%s: fail to release exclusive\n", __func__);
  2304. return rc;
  2305. }
  2306. /*******************************************************************************
  2307. * FUNCTION: _pt_request_pip_start_bl
  2308. *
  2309. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2310. * modules to request the DUT to enter the BL
  2311. *
  2312. * RETURN:
  2313. * 0 = success
  2314. * !0 = failure
  2315. *
  2316. * PARAMETERS:
  2317. * *dev - pointer to device structure
  2318. * protect - flag to run in protected mode
  2319. ******************************************************************************/
  2320. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2321. {
  2322. struct pt_core_data *cd = dev_get_drvdata(dev);
  2323. if (protect)
  2324. return pt_pip_start_bootloader(cd);
  2325. return pt_pip_start_bootloader_(cd);
  2326. }
  2327. /*******************************************************************************
  2328. * FUNCTION: pt_pip2_ver_load_ttdata
  2329. *
  2330. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2331. * command into the core data struct.
  2332. *
  2333. * RETURN: n/a
  2334. *
  2335. * PARAMETERS:
  2336. * *cd - pointer to core data structure
  2337. * len - Length of data in response_buf
  2338. ******************************************************************************/
  2339. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2340. {
  2341. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2342. struct pt_pip2_version_full *full_ver;
  2343. struct pt_pip2_version *ver;
  2344. /*
  2345. * The PIP2 VERSION command can return different lengths of data.
  2346. * The additional LOT fields are included when the packet
  2347. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2348. * NOTE:
  2349. * - The FW would swap the BL and FW versions when reporting
  2350. * the small packet.
  2351. * - Sub Lot bytes 16 and 17 are reserved.
  2352. */
  2353. if (len >= 0x1D) {
  2354. full_ver = (struct pt_pip2_version_full *)
  2355. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2356. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2357. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2358. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2359. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2360. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2361. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2362. /*
  2363. * BL PIP 2.02 and greater the version fields are
  2364. * swapped
  2365. */
  2366. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2367. ttdata->chip_rev =
  2368. get_unaligned_le16(&full_ver->chip_rev);
  2369. ttdata->chip_id =
  2370. get_unaligned_le16(&full_ver->chip_id);
  2371. } else {
  2372. ttdata->chip_rev =
  2373. get_unaligned_le16(&full_ver->chip_id);
  2374. ttdata->chip_id =
  2375. get_unaligned_le16(&full_ver->chip_rev);
  2376. }
  2377. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2378. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2379. sizeof(struct pt_pip2_version_full),
  2380. "PIP2 VERSION FULL");
  2381. } else {
  2382. ver = (struct pt_pip2_version *)
  2383. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2384. ttdata->pip_ver_major = ver->pip2_version_msb;
  2385. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2386. ttdata->bl_ver_major = ver->bl_version_msb;
  2387. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2388. ttdata->fw_ver_major = ver->fw_version_msb;
  2389. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2390. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2391. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2392. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2393. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2394. }
  2395. }
  2396. /*******************************************************************************
  2397. * FUNCTION: pt_si_get_ttdata
  2398. *
  2399. * SUMMARY: Function to load the version information from the system information
  2400. * PIP command into the core data struct.
  2401. *
  2402. * RETURN: n/a
  2403. *
  2404. * PARAMETERS:
  2405. * *cd - pointer to core data structure
  2406. ******************************************************************************/
  2407. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2408. {
  2409. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2410. struct pt_ttdata_dev *ttdata_dev =
  2411. (struct pt_ttdata_dev *)
  2412. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2413. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2414. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2415. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2416. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2417. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2418. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2419. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2420. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2421. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2422. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2423. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2424. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2425. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2426. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2427. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2428. }
  2429. /*******************************************************************************
  2430. * FUNCTION: pt_si_get_sensing_conf_data
  2431. *
  2432. * SUMMARY: Function to load the sensing information from the system information
  2433. * PIP command into the core data struct.
  2434. *
  2435. * RETURN: n/a
  2436. *
  2437. * PARAMETERS:
  2438. * *cd - pointer to core data structure
  2439. ******************************************************************************/
  2440. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2441. {
  2442. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2443. struct pt_sensing_conf_data_dev *scd_dev =
  2444. (struct pt_sensing_conf_data_dev *)
  2445. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2446. scd->electrodes_x = scd_dev->electrodes_x;
  2447. scd->electrodes_y = scd_dev->electrodes_y;
  2448. scd->origin_x = scd_dev->origin_x;
  2449. scd->origin_y = scd_dev->origin_y;
  2450. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2451. if (scd->origin_x & 0x02) {
  2452. scd->tx_num = scd->electrodes_x;
  2453. scd->rx_num = scd->electrodes_y;
  2454. } else {
  2455. scd->tx_num = scd->electrodes_y;
  2456. scd->rx_num = scd->electrodes_x;
  2457. }
  2458. /*
  2459. * When the Panel ID is coming from an XY pin and not a dedicated
  2460. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2461. * other DUTs as the loader will use cd->pid_for_loader to generate
  2462. * the bin file name but will ignore it if pid_for_loader is still
  2463. * set to PANEL_ID_NOT_ENABLED
  2464. */
  2465. if (cd->panel_id_support &
  2466. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2467. mutex_lock(&cd->system_lock);
  2468. cd->pid_for_loader = scd_dev->panel_id;
  2469. mutex_unlock(&cd->system_lock);
  2470. }
  2471. scd->panel_id = scd_dev->panel_id;
  2472. scd->btn = scd_dev->btn;
  2473. scd->scan_mode = scd_dev->scan_mode;
  2474. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2475. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2476. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2477. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2478. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2479. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2480. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2481. sizeof(struct pt_sensing_conf_data_dev),
  2482. "sensing_conf_data");
  2483. }
  2484. /*******************************************************************************
  2485. * FUNCTION: pt_si_setup
  2486. *
  2487. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2488. *
  2489. * RETURN:
  2490. * 0 = success
  2491. * !0 = failure
  2492. *
  2493. * PARAMETERS:
  2494. * *cd - pointer to core data structure
  2495. ******************************************************************************/
  2496. static int pt_si_setup(struct pt_core_data *cd)
  2497. {
  2498. struct pt_sysinfo *si = &cd->sysinfo;
  2499. int max_tch = si->sensing_conf_data.max_tch;
  2500. if (!si->xy_data)
  2501. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2502. GFP_KERNEL);
  2503. if (!si->xy_data)
  2504. return -ENOMEM;
  2505. if (!si->xy_mode)
  2506. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2507. if (!si->xy_mode) {
  2508. kfree(si->xy_data);
  2509. return -ENOMEM;
  2510. }
  2511. return 0;
  2512. }
  2513. /*******************************************************************************
  2514. * FUNCTION: pt_si_get_btn_data
  2515. *
  2516. * SUMMARY: Setup the core data button information based on the response of the
  2517. * System Information PIP command.
  2518. *
  2519. * RETURN:
  2520. * 0 = success
  2521. * !0 = failure
  2522. *
  2523. * PARAMETERS:
  2524. * *cd - pointer to core data structure
  2525. ******************************************************************************/
  2526. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2527. {
  2528. struct pt_sysinfo *si = &cd->sysinfo;
  2529. int num_btns = 0;
  2530. int num_defined_keys;
  2531. u16 *key_table;
  2532. int btn;
  2533. int i;
  2534. int rc = 0;
  2535. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2536. & PIP1_SYSINFO_BTN_MASK;
  2537. size_t btn_keys_size;
  2538. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2539. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2540. if (btns & (1 << i))
  2541. num_btns++;
  2542. }
  2543. si->num_btns = num_btns;
  2544. if (num_btns) {
  2545. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2546. if (!si->btn)
  2547. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2548. if (!si->btn)
  2549. return -ENOMEM;
  2550. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2551. num_defined_keys = 0;
  2552. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2553. num_defined_keys = 0;
  2554. else
  2555. num_defined_keys = cd->cpdata->sett
  2556. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2557. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2558. key_table = (u16 *)cd->cpdata->sett
  2559. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2560. si->btn[btn].key_code = key_table[btn];
  2561. si->btn[btn].enabled = true;
  2562. }
  2563. for (; btn < num_btns; btn++) {
  2564. si->btn[btn].key_code = KEY_RESERVED;
  2565. si->btn[btn].enabled = true;
  2566. }
  2567. return rc;
  2568. }
  2569. kfree(si->btn);
  2570. si->btn = NULL;
  2571. return rc;
  2572. }
  2573. /*******************************************************************************
  2574. * FUNCTION: pt_si_put_log_data
  2575. *
  2576. * SUMMARY: Prints all sys info data to kmsg log
  2577. *
  2578. * RETURN: n/a
  2579. *
  2580. * PARAMETERS:
  2581. * *cd - pointer to core data structure
  2582. ******************************************************************************/
  2583. static void pt_si_put_log_data(struct pt_core_data *cd)
  2584. {
  2585. struct pt_sysinfo *si = &cd->sysinfo;
  2586. struct pt_ttdata *ttdata = &si->ttdata;
  2587. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2588. int i;
  2589. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2590. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2591. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2592. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2593. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2594. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2595. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2596. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2597. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2598. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2599. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2600. __func__, ttdata->revctrl, ttdata->revctrl);
  2601. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2602. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2603. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2604. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2605. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2606. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2608. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2610. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2611. for (i = 0; i < PT_NUM_MFGID; i++)
  2612. pt_debug(cd->dev, DL_DEBUG,
  2613. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2614. __func__, i, ttdata->mfg_id[i],
  2615. ttdata->mfg_id[i]);
  2616. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2617. __func__, ttdata->post_code, ttdata->post_code);
  2618. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2619. __func__, scd->electrodes_x, scd->electrodes_x);
  2620. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2621. __func__, scd->electrodes_y, scd->electrodes_y);
  2622. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2623. __func__, scd->len_x, scd->len_x);
  2624. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2625. __func__, scd->len_y, scd->len_y);
  2626. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2627. __func__, scd->res_x, scd->res_x);
  2628. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2629. __func__, scd->res_y, scd->res_y);
  2630. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2631. __func__, scd->max_z, scd->max_z);
  2632. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2633. __func__, scd->origin_x, scd->origin_x);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2635. __func__, scd->origin_y, scd->origin_y);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2637. __func__, scd->panel_id, scd->panel_id);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2639. __func__, scd->btn, scd->btn);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2641. __func__, scd->scan_mode, scd->scan_mode);
  2642. pt_debug(cd->dev, DL_DEBUG,
  2643. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2644. __func__, scd->max_tch, scd->max_tch);
  2645. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2646. __func__, si->xy_mode);
  2647. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2648. __func__, si->xy_data);
  2649. }
  2650. /*******************************************************************************
  2651. * FUNCTION: pt_get_sysinfo_regs
  2652. *
  2653. * SUMMARY: Setup all the core data System information based on the response
  2654. * of the System Information PIP command.
  2655. *
  2656. * RETURN:
  2657. * 0 = success
  2658. * !0 = failure
  2659. *
  2660. * PARAMETERS:
  2661. * *cd - pointer to core data structure
  2662. ******************************************************************************/
  2663. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2664. {
  2665. struct pt_sysinfo *si = &cd->sysinfo;
  2666. int rc;
  2667. rc = pt_si_get_btn_data(cd);
  2668. if (rc < 0)
  2669. return rc;
  2670. pt_si_get_ttdata(cd);
  2671. pt_si_get_sensing_conf_data(cd);
  2672. pt_si_setup(cd);
  2673. pt_si_put_log_data(cd);
  2674. si->ready = true;
  2675. return rc;
  2676. }
  2677. /*******************************************************************************
  2678. * FUNCTION: pt_free_si_ptrs
  2679. *
  2680. * SUMMARY: Frees all memory associated with the System Information within
  2681. * core data
  2682. *
  2683. * RETURN: n/a
  2684. *
  2685. * PARAMETERS:
  2686. * *cd - pointer to core data structure
  2687. ******************************************************************************/
  2688. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2689. {
  2690. struct pt_sysinfo *si = &cd->sysinfo;
  2691. kfree(si->btn);
  2692. kfree(si->xy_mode);
  2693. kfree(si->xy_data);
  2694. }
  2695. /*******************************************************************************
  2696. * FUNCTION: pt_hid_output_get_sysinfo_
  2697. *
  2698. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2699. * response.
  2700. *
  2701. * RETURN::
  2702. * 0 = success
  2703. * !0 = failure
  2704. *
  2705. * PARAMETERS:
  2706. * *cd - pointer to core data structure
  2707. ******************************************************************************/
  2708. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2709. {
  2710. int rc = 0;
  2711. struct pt_hid_output hid_output = {
  2712. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2713. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2714. };
  2715. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2716. if (rc)
  2717. return rc;
  2718. /* Parse the sysinfo data */
  2719. rc = pt_get_sysinfo_regs(cd);
  2720. if (rc)
  2721. pt_free_si_ptrs(cd);
  2722. return rc;
  2723. }
  2724. /*******************************************************************************
  2725. * FUNCTION: pt_hid_output_get_sysinfo
  2726. *
  2727. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2728. *
  2729. * RETURN::
  2730. * 0 = success
  2731. * !0 = failure
  2732. *
  2733. * PARAMETERS:
  2734. * *cd - pointer to core data structure
  2735. ******************************************************************************/
  2736. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2737. {
  2738. int rc;
  2739. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2740. if (rc < 0) {
  2741. pt_debug(cd->dev, DL_ERROR,
  2742. "%s: fail get exclusive ex=%p own=%p\n",
  2743. __func__, cd->exclusive_dev, cd->dev);
  2744. return rc;
  2745. }
  2746. rc = pt_hid_output_get_sysinfo_(cd);
  2747. if (release_exclusive(cd, cd->dev) < 0)
  2748. pt_debug(cd->dev, DL_ERROR,
  2749. "%s: fail to release exclusive\n", __func__);
  2750. return rc;
  2751. }
  2752. /*******************************************************************************
  2753. * FUNCTION: pt_pip_suspend_scanning_
  2754. *
  2755. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2756. *
  2757. * RETURN::
  2758. * 0 = success
  2759. * !0 = failure
  2760. *
  2761. * PARAMETERS:
  2762. * *cd - pointer to core data structure
  2763. ******************************************************************************/
  2764. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2765. {
  2766. int rc = 0;
  2767. struct pt_hid_output hid_output = {
  2768. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2769. };
  2770. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2771. if (rc) {
  2772. pt_debug(cd->dev, DL_ERROR,
  2773. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2774. __func__, rc);
  2775. }
  2776. return rc;
  2777. }
  2778. /*******************************************************************************
  2779. * FUNCTION: pt_pip_suspend_scanning
  2780. *
  2781. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2782. *
  2783. * RETURN::
  2784. * 0 = success
  2785. * !0 = failure
  2786. *
  2787. * PARAMETERS:
  2788. * *cd - pointer to core data structure
  2789. ******************************************************************************/
  2790. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2791. {
  2792. int rc;
  2793. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2794. if (rc < 0) {
  2795. pt_debug(cd->dev, DL_ERROR,
  2796. "%s: fail get exclusive ex=%p own=%p\n",
  2797. __func__, cd->exclusive_dev, cd->dev);
  2798. return rc;
  2799. }
  2800. rc = pt_pip_suspend_scanning_(cd);
  2801. if (release_exclusive(cd, cd->dev) < 0)
  2802. pt_debug(cd->dev, DL_ERROR,
  2803. "%s: fail to release exclusive\n", __func__);
  2804. return rc;
  2805. }
  2806. /*******************************************************************************
  2807. * FUNCTION: _pt_request_pip_suspend_scanning
  2808. *
  2809. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2810. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2811. *
  2812. * RETURN:
  2813. * 0 = success
  2814. * !0 = failure
  2815. *
  2816. * PARAMETERS:
  2817. * *dev - pointer to device structure
  2818. * protect - 0 = call non-protected function
  2819. * 1 = call protected function
  2820. ******************************************************************************/
  2821. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2822. int protect)
  2823. {
  2824. struct pt_core_data *cd = dev_get_drvdata(dev);
  2825. if (protect)
  2826. return pt_pip_suspend_scanning(cd);
  2827. return pt_pip_suspend_scanning_(cd);
  2828. }
  2829. /*******************************************************************************
  2830. * FUNCTION: pt_pip_resume_scanning_
  2831. *
  2832. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2833. *
  2834. * RETURN::
  2835. * 0 = success
  2836. * !0 = failure
  2837. *
  2838. * PARAMETERS:
  2839. * *cd - pointer to core data structure
  2840. ******************************************************************************/
  2841. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2842. {
  2843. int rc = 0;
  2844. struct pt_hid_output hid_output = {
  2845. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2846. };
  2847. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2848. if (rc) {
  2849. pt_debug(cd->dev, DL_ERROR,
  2850. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2851. __func__, rc);
  2852. }
  2853. return rc;
  2854. }
  2855. /*******************************************************************************
  2856. * FUNCTION: pt_pip_resume_scanning
  2857. *
  2858. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2859. *
  2860. * RETURN::
  2861. * 0 = success
  2862. * !0 = failure
  2863. *
  2864. * PARAMETERS:
  2865. * *cd - pointer to core data structure
  2866. ******************************************************************************/
  2867. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2868. {
  2869. int rc;
  2870. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2871. if (rc < 0) {
  2872. pt_debug(cd->dev, DL_ERROR,
  2873. "%s: fail get exclusive ex=%p own=%p\n",
  2874. __func__, cd->exclusive_dev, cd->dev);
  2875. return rc;
  2876. }
  2877. rc = pt_pip_resume_scanning_(cd);
  2878. if (release_exclusive(cd, cd->dev) < 0)
  2879. pt_debug(cd->dev, DL_ERROR,
  2880. "%s: fail to release exclusive\n", __func__);
  2881. return rc;
  2882. }
  2883. /*******************************************************************************
  2884. * FUNCTION: _pt_request_pip_resume_scanning
  2885. *
  2886. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2887. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2888. *
  2889. * RETURN::
  2890. * 0 = success
  2891. * !0 = failure
  2892. *
  2893. * PARAMETERS:
  2894. * *dev - pointer to device structure
  2895. * protect - 0 = call non-protected function
  2896. * 1 = call protected function
  2897. ******************************************************************************/
  2898. static int _pt_request_pip_resume_scanning(struct device *dev,
  2899. int protect)
  2900. {
  2901. struct pt_core_data *cd = dev_get_drvdata(dev);
  2902. if (protect)
  2903. return pt_pip_resume_scanning(cd);
  2904. return pt_pip_resume_scanning_(cd);
  2905. }
  2906. /*******************************************************************************
  2907. * FUNCTION: pt_pip_get_param_
  2908. *
  2909. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2910. * the 32bit parameter value
  2911. *
  2912. * RETURN:
  2913. * 0 = success
  2914. * !0 = failure
  2915. *
  2916. * PARAMETERS:
  2917. * *cd - pointer to core data
  2918. * param_id - parameter ID to retrieve
  2919. * *value - value of DUT parameter
  2920. ******************************************************************************/
  2921. static int pt_pip_get_param_(struct pt_core_data *cd,
  2922. u8 param_id, u32 *value)
  2923. {
  2924. int write_length = 1;
  2925. u8 param[1] = { param_id };
  2926. u8 read_param_id;
  2927. int param_size;
  2928. u8 *ptr;
  2929. int rc = 0;
  2930. int i;
  2931. struct pt_hid_output hid_output = {
  2932. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2933. .write_length = write_length,
  2934. .write_buf = param,
  2935. };
  2936. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2937. if (rc)
  2938. return rc;
  2939. read_param_id = cd->response_buf[5];
  2940. if (read_param_id != param_id)
  2941. return -EPROTO;
  2942. param_size = cd->response_buf[6];
  2943. ptr = &cd->response_buf[7];
  2944. *value = 0;
  2945. for (i = 0; i < param_size; i++)
  2946. *value += ptr[i] << (i * 8);
  2947. return 0;
  2948. }
  2949. /*******************************************************************************
  2950. * FUNCTION: pt_pip_get_param
  2951. *
  2952. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2953. *
  2954. * RETURN:
  2955. * 0 = success
  2956. * !0 = failure
  2957. *
  2958. * PARAMETERS:
  2959. * *cd - pointer to core data
  2960. * param_id - parameter ID to retrieve
  2961. * *value - value of DUT parameter
  2962. ******************************************************************************/
  2963. static int pt_pip_get_param(struct pt_core_data *cd,
  2964. u8 param_id, u32 *value)
  2965. {
  2966. int rc;
  2967. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2968. if (rc < 0) {
  2969. pt_debug(cd->dev, DL_ERROR,
  2970. "%s: fail get exclusive ex=%p own=%p\n",
  2971. __func__, cd->exclusive_dev, cd->dev);
  2972. return rc;
  2973. }
  2974. rc = pt_pip_get_param_(cd, param_id, value);
  2975. if (release_exclusive(cd, cd->dev) < 0)
  2976. pt_debug(cd->dev, DL_ERROR,
  2977. "%s: fail to release exclusive\n", __func__);
  2978. return rc;
  2979. }
  2980. /*******************************************************************************
  2981. * FUNCTION: _pt_request_pip_get_param
  2982. *
  2983. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2984. * calls to the protected or unprotected call to pt_pip_get_param
  2985. *
  2986. * RETURN:
  2987. * 0 = success
  2988. * !0 = failure
  2989. *
  2990. * PARAMETERS:
  2991. * *dev - pointer to device structure
  2992. * protect - flag to call protected or non protected function
  2993. * param_id - parameter ID to retrieve
  2994. * *value - value of DUT parameter
  2995. ******************************************************************************/
  2996. int _pt_request_pip_get_param(struct device *dev,
  2997. int protect, u8 param_id, u32 *value)
  2998. {
  2999. struct pt_core_data *cd = dev_get_drvdata(dev);
  3000. if (protect)
  3001. return pt_pip_get_param(cd, param_id, value);
  3002. return pt_pip_get_param_(cd, param_id, value);
  3003. }
  3004. /*******************************************************************************
  3005. * FUNCTION: pt_pip_set_param_
  3006. *
  3007. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3008. * passed in value to flash
  3009. *
  3010. * RETURN:
  3011. * 0 = success
  3012. * !0 = failure
  3013. *
  3014. * PARAMETERS:
  3015. * *cd - pointer to core data
  3016. * param_id - parameter ID to set
  3017. * value - value to write
  3018. * size - size to write
  3019. ******************************************************************************/
  3020. static int pt_pip_set_param_(struct pt_core_data *cd,
  3021. u8 param_id, u32 value, u8 size)
  3022. {
  3023. u8 write_buf[6];
  3024. u8 *ptr = &write_buf[2];
  3025. int rc = 0;
  3026. int i;
  3027. struct pt_hid_output hid_output = {
  3028. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3029. .write_buf = write_buf,
  3030. };
  3031. write_buf[0] = param_id;
  3032. write_buf[1] = size;
  3033. for (i = 0; i < size; i++) {
  3034. ptr[i] = value & 0xFF;
  3035. value = value >> 8;
  3036. }
  3037. hid_output.write_length = 2 + size;
  3038. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3039. if (rc)
  3040. return rc;
  3041. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3042. return -EPROTO;
  3043. return 0;
  3044. }
  3045. /*******************************************************************************
  3046. * FUNCTION: pt_pip_set_param
  3047. *
  3048. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3049. *
  3050. * RETURN:
  3051. * 0 = success
  3052. * !0 = failure
  3053. *
  3054. * PARAMETERS:
  3055. * *cd - pointer to core data
  3056. * param_id - parameter ID to set
  3057. * value - value to write
  3058. * size - size to write
  3059. ******************************************************************************/
  3060. static int pt_pip_set_param(struct pt_core_data *cd,
  3061. u8 param_id, u32 value, u8 size)
  3062. {
  3063. int rc;
  3064. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3065. if (rc < 0) {
  3066. pt_debug(cd->dev, DL_ERROR,
  3067. "%s: fail get exclusive ex=%p own=%p\n",
  3068. __func__, cd->exclusive_dev, cd->dev);
  3069. return rc;
  3070. }
  3071. rc = pt_pip_set_param_(cd, param_id, value, size);
  3072. if (release_exclusive(cd, cd->dev) < 0)
  3073. pt_debug(cd->dev, DL_ERROR,
  3074. "%s: fail to release exclusive\n", __func__);
  3075. return rc;
  3076. }
  3077. /*******************************************************************************
  3078. * FUNCTION: _pt_request_pip_set_param
  3079. *
  3080. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3081. * calls to the protected or unprotected call to pt_pip_set_param
  3082. *
  3083. * RETURN:
  3084. * 0 = success
  3085. * !0 = failure
  3086. *
  3087. * PARAMETERS:
  3088. * *dev - pointer to device structure
  3089. * protect - flag to call protected or non-protected
  3090. * param_id - parameter ID to set
  3091. * value - value to write
  3092. * size - size to write
  3093. ******************************************************************************/
  3094. int _pt_request_pip_set_param(struct device *dev, int protect,
  3095. u8 param_id, u32 value, u8 size)
  3096. {
  3097. struct pt_core_data *cd = dev_get_drvdata(dev);
  3098. if (protect)
  3099. return pt_pip_set_param(cd, param_id, value, size);
  3100. return pt_pip_set_param_(cd, param_id, value, size);
  3101. }
  3102. /*******************************************************************************
  3103. * FUNCTION: _pt_pip_enter_easywake_state_
  3104. *
  3105. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3106. *
  3107. * RETURN:
  3108. * 0 = success
  3109. * !0 = failure
  3110. *
  3111. * PARAMETERS:
  3112. * *dev - pointer to device structure
  3113. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3114. * *return_data - return status if easywake was entered
  3115. ******************************************************************************/
  3116. static int pt_hid_output_enter_easywake_state_(
  3117. struct pt_core_data *cd, u8 data, u8 *return_data)
  3118. {
  3119. int write_length = 1;
  3120. u8 param[1] = { data };
  3121. int rc = 0;
  3122. struct pt_hid_output hid_output = {
  3123. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3124. .write_length = write_length,
  3125. .write_buf = param,
  3126. };
  3127. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3128. if (rc)
  3129. return rc;
  3130. *return_data = cd->response_buf[5];
  3131. return rc;
  3132. }
  3133. /*******************************************************************************
  3134. * FUNCTION: pt_pip_verify_config_block_crc_
  3135. *
  3136. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3137. *
  3138. * RETURN:
  3139. * 0 = success
  3140. * !0 = failure
  3141. *
  3142. * PARAMETERS:
  3143. * *cd - pointer the core data structure
  3144. * ebid - enumerated block ID
  3145. * *status - PIP command status
  3146. * calculated_crc - calculated CRC
  3147. * stored_crc - stored CRC in config area
  3148. ******************************************************************************/
  3149. static int pt_pip_verify_config_block_crc_(
  3150. struct pt_core_data *cd, u8 ebid, u8 *status,
  3151. u16 *calculated_crc, u16 *stored_crc)
  3152. {
  3153. int write_length = 1;
  3154. u8 param[1] = { ebid };
  3155. u8 *ptr;
  3156. int rc = 0;
  3157. struct pt_hid_output hid_output = {
  3158. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3159. .write_length = write_length,
  3160. .write_buf = param,
  3161. };
  3162. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3163. if (rc)
  3164. return rc;
  3165. ptr = &cd->response_buf[5];
  3166. *status = ptr[0];
  3167. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3168. *stored_crc = get_unaligned_le16(&ptr[3]);
  3169. return 0;
  3170. }
  3171. /*******************************************************************************
  3172. * FUNCTION: pt_pip_verify_config_block_crc
  3173. *
  3174. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3175. * an exclusive access to the DUT.
  3176. *
  3177. * RETURN:
  3178. * 0 = success
  3179. * !0 = failure
  3180. *
  3181. * PARAMETERS:
  3182. * *cd - pointer the core data structure
  3183. * ebid - enumerated block ID
  3184. * *status - PIP command status
  3185. * calculated_crc - calculated CRC
  3186. * stored_crc - stored CRC in config area
  3187. ******************************************************************************/
  3188. static int pt_pip_verify_config_block_crc(
  3189. struct pt_core_data *cd, u8 ebid, u8 *status,
  3190. u16 *calculated_crc, u16 *stored_crc)
  3191. {
  3192. int rc;
  3193. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3194. if (rc < 0) {
  3195. pt_debug(cd->dev, DL_ERROR,
  3196. "%s: fail get exclusive ex=%p own=%p\n",
  3197. __func__, cd->exclusive_dev, cd->dev);
  3198. return rc;
  3199. }
  3200. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3201. calculated_crc, stored_crc);
  3202. if (release_exclusive(cd, cd->dev) < 0)
  3203. pt_debug(cd->dev, DL_ERROR,
  3204. "%s: fail to release exclusive\n", __func__);
  3205. return rc;
  3206. }
  3207. /*******************************************************************************
  3208. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3209. *
  3210. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3211. * calls to the protected or unprotected call to
  3212. * pt_pip_verify_config_block_crc_
  3213. *
  3214. * RETURN:
  3215. * 0 = success
  3216. * !0 = failure
  3217. *
  3218. * PARAMETERS:
  3219. * *dev - pointer to device structure
  3220. * protect - flag to call protected or non-protected
  3221. * ebid - enumerated block ID
  3222. * *status - PIP command status
  3223. * calculated_crc - calculated CRC
  3224. * stored_crc - stored CRC in config area
  3225. ******************************************************************************/
  3226. static int _pt_request_pip_verify_config_block_crc(
  3227. struct device *dev, int protect, u8 ebid, u8 *status,
  3228. u16 *calculated_crc, u16 *stored_crc)
  3229. {
  3230. struct pt_core_data *cd = dev_get_drvdata(dev);
  3231. if (protect)
  3232. return pt_pip_verify_config_block_crc(cd, ebid,
  3233. status, calculated_crc, stored_crc);
  3234. return pt_pip_verify_config_block_crc_(cd, ebid,
  3235. status, calculated_crc, stored_crc);
  3236. }
  3237. /*******************************************************************************
  3238. * FUNCTION: pt_pip_get_config_row_size_
  3239. *
  3240. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3241. *
  3242. * RETURN:
  3243. * 0 = success
  3244. * !0 = failure
  3245. *
  3246. * PARAMETERS:
  3247. * *cd - pointer to core data
  3248. * protect - flag to call protected or non-protected
  3249. * *row_size - pointer to store the retrieved row size
  3250. ******************************************************************************/
  3251. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3252. u16 *row_size)
  3253. {
  3254. int rc = 0;
  3255. struct pt_hid_output hid_output = {
  3256. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3257. };
  3258. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3259. if (rc)
  3260. return rc;
  3261. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3262. return 0;
  3263. }
  3264. /*******************************************************************************
  3265. * FUNCTION: pt_pip_get_config_row_size
  3266. *
  3267. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3268. * an exclusive access to the DUT.
  3269. *
  3270. * RETURN:
  3271. * 0 = success
  3272. * !0 = failure
  3273. *
  3274. * PARAMETERS:
  3275. * *cd - pointer to core data
  3276. * protect - flag to call protected or non-protected
  3277. * *row_size - pointer to store the retrieved row size
  3278. ******************************************************************************/
  3279. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3280. u16 *row_size)
  3281. {
  3282. int rc;
  3283. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3284. if (rc < 0) {
  3285. pt_debug(cd->dev, DL_ERROR,
  3286. "%s: fail get exclusive ex=%p own=%p\n",
  3287. __func__, cd->exclusive_dev, cd->dev);
  3288. return rc;
  3289. }
  3290. rc = pt_pip_get_config_row_size_(cd, row_size);
  3291. if (release_exclusive(cd, cd->dev) < 0)
  3292. pt_debug(cd->dev, DL_ERROR,
  3293. "%s: fail to release exclusive\n", __func__);
  3294. return rc;
  3295. }
  3296. /*******************************************************************************
  3297. * FUNCTION: _pt_request_pip_get_config_row_size
  3298. *
  3299. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3300. * calls to the protected or unprotected call to
  3301. * pt_pip_get_config_row_size_
  3302. *
  3303. * RETURN:
  3304. * 0 = success
  3305. * !0 = failure
  3306. *
  3307. * PARAMETERS:
  3308. * *dev - pointer to device structure
  3309. * protect - flag to call protected or non-protected
  3310. * *row_size - pointer to store the retrieved row size
  3311. ******************************************************************************/
  3312. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3313. int protect, u16 *row_size)
  3314. {
  3315. struct pt_core_data *cd = dev_get_drvdata(dev);
  3316. if (protect)
  3317. return pt_pip_get_config_row_size(cd, row_size);
  3318. return pt_pip_get_config_row_size_(cd, row_size);
  3319. }
  3320. /*******************************************************************************
  3321. * FUNCTION: pt_pip1_read_data_block_
  3322. *
  3323. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3324. * output data to the "read_buf" and update "crc".
  3325. *
  3326. * RETURN:
  3327. * 0 = success
  3328. * !0 = failure
  3329. *
  3330. * PARAMETERS:
  3331. * *cd - pointer to core data
  3332. * row_number - row number
  3333. * length - length of data to read
  3334. * ebid - block id
  3335. * *actual_read_len - Actual data length read
  3336. * *read_buf - pointer to the buffer to store read data
  3337. * read_buf_size - size of read_buf
  3338. * *crc - pointer to store CRC of row data
  3339. ******************************************************************************/
  3340. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3341. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3342. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3343. {
  3344. int read_ebid;
  3345. int status;
  3346. int rc = 0;
  3347. int write_length = 5;
  3348. u8 write_buf[5];
  3349. u8 cmd_offset = 0;
  3350. u16 calc_crc;
  3351. struct pt_hid_output hid_output = {
  3352. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3353. .write_length = write_length,
  3354. .write_buf = write_buf,
  3355. };
  3356. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3357. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3358. write_buf[cmd_offset++] = LOW_BYTE(length);
  3359. write_buf[cmd_offset++] = HI_BYTE(length);
  3360. write_buf[cmd_offset++] = ebid;
  3361. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3362. if (rc)
  3363. return rc;
  3364. status = cd->response_buf[5];
  3365. if (status)
  3366. return status;
  3367. read_ebid = cd->response_buf[6];
  3368. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3369. return -EPROTO;
  3370. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3371. if (length == 0 || *actual_read_len == 0)
  3372. return 0;
  3373. if (read_buf_size >= *actual_read_len)
  3374. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3375. else
  3376. return -EPROTO;
  3377. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3378. /* Validate Row Data CRC */
  3379. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3380. if (*crc == calc_crc) {
  3381. return 0;
  3382. } else {
  3383. pt_debug(cd->dev, DL_ERROR,
  3384. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3385. __func__, *crc, calc_crc);
  3386. return -EPROTO;
  3387. }
  3388. }
  3389. /*******************************************************************************
  3390. * FUNCTION: _pt_request_pip_read_data_block
  3391. *
  3392. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3393. * calls to pt_pip1_read_data_block_
  3394. *
  3395. * RETURN:
  3396. * 0 = success
  3397. * !0 = failure
  3398. *
  3399. * PARAMETERS:
  3400. * *dev - pointer to device structure
  3401. * row_number - row number
  3402. * length - length of data to read
  3403. * ebid - block id
  3404. * *actual_read_len - Actual data length read
  3405. * *read_buf - pointer to the buffer to store read data
  3406. * *crc - pointer to store CRC of row data
  3407. ******************************************************************************/
  3408. static int _pt_request_pip_read_data_block(struct device *dev,
  3409. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3410. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3411. {
  3412. struct pt_core_data *cd = dev_get_drvdata(dev);
  3413. return pt_pip1_read_data_block_(cd, row_number, length,
  3414. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3415. }
  3416. /*******************************************************************************
  3417. * FUNCTION: pt_pip1_write_data_block_
  3418. *
  3419. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3420. * write data to the data block.
  3421. *
  3422. * RETURN:
  3423. * 0 = success
  3424. * !0 = failure
  3425. *
  3426. * PARAMETERS:
  3427. * *cd - pointer to core data
  3428. * row_number - row in config block to write to
  3429. * write_length - length of data to write
  3430. * ebid - enumerated block ID
  3431. * *write_buf - pointer to buffer to write
  3432. * *security_key - pointer to security key to allow write
  3433. * *actual_write_len - pointer to store data length actually written
  3434. ******************************************************************************/
  3435. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3436. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3437. u8 *security_key, u16 *actual_write_len)
  3438. {
  3439. /* row_number + write_len + ebid + security_key + crc */
  3440. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3441. u8 *full_write_buf;
  3442. u8 cmd_offset = 0;
  3443. u16 crc;
  3444. int status;
  3445. int rc = 0;
  3446. int read_ebid;
  3447. u8 *data;
  3448. struct pt_hid_output hid_output = {
  3449. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3450. .write_length = full_write_length,
  3451. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3452. };
  3453. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3454. if (!full_write_buf)
  3455. return -ENOMEM;
  3456. hid_output.write_buf = full_write_buf;
  3457. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3458. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3459. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3460. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3461. full_write_buf[cmd_offset++] = ebid;
  3462. data = &full_write_buf[cmd_offset];
  3463. memcpy(data, write_buf, write_length);
  3464. cmd_offset += write_length;
  3465. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3466. cmd_offset += 8;
  3467. crc = _pt_compute_crc(data, write_length);
  3468. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3469. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3470. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3471. if (rc)
  3472. goto exit;
  3473. status = cd->response_buf[5];
  3474. if (status) {
  3475. rc = -EINVAL;
  3476. goto exit;
  3477. }
  3478. read_ebid = cd->response_buf[6];
  3479. if (read_ebid != ebid) {
  3480. rc = -EPROTO;
  3481. goto exit;
  3482. }
  3483. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3484. exit:
  3485. kfree(full_write_buf);
  3486. return rc;
  3487. }
  3488. /*******************************************************************************
  3489. * FUNCTION: _pt_request_pip_write_data_block
  3490. *
  3491. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3492. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3493. *
  3494. * RETURN:
  3495. * 0 = success
  3496. * !0 = failure
  3497. *
  3498. * PARAMETERS:
  3499. * *dev - pointer to device structure
  3500. * protect - flag to call protected or non-protected
  3501. * row_number - row in config block to write to
  3502. * write_length - length of data to write
  3503. * ebid - enumerated block ID
  3504. * *write_buf - pointer to buffer to write
  3505. * *security_key - pointer to security key to allow write
  3506. * *actual_write_len - pointer to store data length actually written
  3507. ******************************************************************************/
  3508. static int _pt_request_pip_write_data_block(struct device *dev,
  3509. u16 row_number, u16 write_length, u8 ebid,
  3510. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3511. {
  3512. struct pt_core_data *cd = dev_get_drvdata(dev);
  3513. return pt_pip1_write_data_block_(cd, row_number,
  3514. write_length, ebid, write_buf, security_key,
  3515. actual_write_len);
  3516. }
  3517. /*******************************************************************************
  3518. * FUNCTION: pt_pip_get_data_structure_
  3519. *
  3520. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3521. * returning a structure of data defined by data_id
  3522. *
  3523. * RETURN:
  3524. * 0 = success
  3525. * !0 = failure
  3526. *
  3527. * PARAMETERS:
  3528. * *cd - pointer to core data
  3529. * read_offset - read pointer offset
  3530. * read_length - length of data to read
  3531. * data_id - data ID to read
  3532. * *status - pointer to store the read response status
  3533. * *data_format - pointer to store format of data read
  3534. * *actual_read_len - pointer to store data length actually read
  3535. * *data - pointer to store data read
  3536. ******************************************************************************/
  3537. static int pt_pip_get_data_structure_(
  3538. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3539. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3540. u8 *data)
  3541. {
  3542. int rc = 0;
  3543. u16 total_read_len = 0;
  3544. u16 read_len;
  3545. u16 off_buf = 0;
  3546. u8 write_buf[5];
  3547. u8 read_data_id;
  3548. struct pt_hid_output hid_output = {
  3549. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3550. .write_length = 5,
  3551. .write_buf = write_buf,
  3552. };
  3553. again:
  3554. write_buf[0] = LOW_BYTE(read_offset);
  3555. write_buf[1] = HI_BYTE(read_offset);
  3556. write_buf[2] = LOW_BYTE(read_length);
  3557. write_buf[3] = HI_BYTE(read_length);
  3558. write_buf[4] = data_id;
  3559. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3560. if (rc)
  3561. return rc;
  3562. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3563. goto set_status;
  3564. read_data_id = cd->response_buf[6];
  3565. if (read_data_id != data_id)
  3566. return -EPROTO;
  3567. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3568. if (read_len && data) {
  3569. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3570. total_read_len += read_len;
  3571. if (read_len < read_length) {
  3572. read_offset += read_len;
  3573. off_buf += read_len;
  3574. read_length -= read_len;
  3575. goto again;
  3576. }
  3577. }
  3578. if (data_format)
  3579. *data_format = cd->response_buf[9];
  3580. if (actual_read_len)
  3581. *actual_read_len = total_read_len;
  3582. set_status:
  3583. if (status)
  3584. *status = cd->response_buf[5];
  3585. return rc;
  3586. }
  3587. /*******************************************************************************
  3588. * FUNCTION: pt_pip_get_data_structure
  3589. *
  3590. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3591. * an exclusive access to the DUT.
  3592. *
  3593. * RETURN:
  3594. * 0 = success
  3595. * !0 = failure
  3596. *
  3597. * PARAMETERS:
  3598. * *cd - pointer to core data
  3599. * read_offset - read pointer offset
  3600. * read_length - length of data to read
  3601. * data_id - data ID to read
  3602. * *status - pointer to store the read response status
  3603. * *data_format - pointer to store format of data read
  3604. * *actual_read_len - pointer to store data length actually read
  3605. * *data - pointer to store data read
  3606. ******************************************************************************/
  3607. static int pt_pip_get_data_structure(
  3608. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3609. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3610. u8 *data)
  3611. {
  3612. int rc;
  3613. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3614. if (rc < 0) {
  3615. pt_debug(cd->dev, DL_ERROR,
  3616. "%s: fail get exclusive ex=%p own=%p\n",
  3617. __func__, cd->exclusive_dev, cd->dev);
  3618. return rc;
  3619. }
  3620. rc = pt_pip_get_data_structure_(cd, read_offset,
  3621. read_length, data_id, status, data_format,
  3622. actual_read_len, data);
  3623. if (release_exclusive(cd, cd->dev) < 0)
  3624. pt_debug(cd->dev, DL_ERROR,
  3625. "%s: fail to release exclusive\n", __func__);
  3626. return rc;
  3627. }
  3628. /*******************************************************************************
  3629. * FUNCTION: _pt_request_pip_get_data_structure
  3630. *
  3631. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3632. * calls to the protected or unprotected call to
  3633. * pt_pip_get_data_structure
  3634. *
  3635. * RETURN:
  3636. * 0 = success
  3637. * !0 = failure
  3638. *
  3639. * PARAMETERS:
  3640. * *dev - pointer to device structure
  3641. * protect - flag to call protected or non-protected
  3642. * read_offset - read pointer offset
  3643. * read_length - length of data to read
  3644. * data_id - data ID to read
  3645. * *status - pointer to store the read response status
  3646. * *data_format - pointer to store format of data read
  3647. * *actual_read_len - pointer to store data length actually read
  3648. * *data - pointer to store data read
  3649. ******************************************************************************/
  3650. static int _pt_request_pip_get_data_structure(struct device *dev,
  3651. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3652. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3653. {
  3654. struct pt_core_data *cd = dev_get_drvdata(dev);
  3655. if (protect)
  3656. return pt_pip_get_data_structure(cd,
  3657. read_offset, read_length, data_id, status,
  3658. data_format, actual_read_len, data);
  3659. return pt_pip_get_data_structure_(cd,
  3660. read_offset, read_length, data_id, status,
  3661. data_format, actual_read_len, data);
  3662. }
  3663. /*******************************************************************************
  3664. * FUNCTION: _pt_manage_local_cal_data
  3665. *
  3666. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3667. * CALIBRATION data. It stores it in a local static array and can be
  3668. * cleared, loaded or used to restore the CAL data back to the running FW.
  3669. * The CAL data is read or restored by use of the PIP1 commands:
  3670. * - READ_DATA_BLOCK (0x22)
  3671. * - WRITE_DATA_BLOCK (0x23)
  3672. *
  3673. * RETURN:
  3674. * 0 = success
  3675. * !0 = failure
  3676. *
  3677. * PARAMETERS:
  3678. * *dev - pointer to device structure
  3679. * action - One of the following actions:
  3680. * - PT_CAL_DATA_SAVE
  3681. * - PT_CAL_DATA_RESTORE
  3682. * - PT_CAL_DATA_CLEAR
  3683. * - PT_CAL_DATA_SIZE
  3684. * *size - pointer to the number of bytes transferred
  3685. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3686. ******************************************************************************/
  3687. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3688. unsigned short *crc)
  3689. {
  3690. struct pt_core_data *cd = dev_get_drvdata(dev);
  3691. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3692. unsigned short calc_id_crc = 0;
  3693. static u8 *cal_cache_data;
  3694. static u16 cal_cache_len;
  3695. static unsigned short cal_cache_chip_id;
  3696. int rc = 0;
  3697. u8 *tmp_data = NULL;
  3698. u8 row_number = 0;
  3699. u8 prefix[20];
  3700. u16 cal_size = 0;
  3701. u16 transfer_size;
  3702. u16 act_trans_len = 0;
  3703. u16 byte_offset = 0;
  3704. u16 cal_blk_size;
  3705. u16 total_rows;
  3706. u16 remain_bytes;
  3707. u16 data_block_crc;
  3708. u16 buf_size = 12;
  3709. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3710. __func__, action);
  3711. switch (action) {
  3712. case PT_CAL_DATA_SAVE:
  3713. /* Read the size of the CAL block and calculate # rows */
  3714. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3715. if (!tmp_data) {
  3716. rc = -ENOMEM;
  3717. goto exit;
  3718. }
  3719. /*
  3720. * Don't check rc as doing a read size will give a false
  3721. * error on the CRC check.
  3722. */
  3723. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3724. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3725. cal_blk_size = act_trans_len;
  3726. kfree(tmp_data);
  3727. pt_debug(dev, DL_INFO,
  3728. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3729. __func__, cal_cache_len, cal_blk_size);
  3730. /* Safety net to ensure we didn't read incorrect size */
  3731. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3732. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3733. __func__);
  3734. rc = 1;
  3735. goto exit;
  3736. }
  3737. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3738. if (cal_blk_size != cal_cache_len) {
  3739. kfree(cal_cache_data);
  3740. cal_cache_data = kzalloc(cal_blk_size + 2,
  3741. GFP_KERNEL);
  3742. if (!cal_cache_data) {
  3743. rc = -ENOMEM;
  3744. goto exit;
  3745. }
  3746. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3747. __func__);
  3748. }
  3749. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3750. /* Calculate how many rows [0-n] (PIP Transactions) */
  3751. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3752. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3753. /* Add row if we have a last partial row */
  3754. if (remain_bytes > 0)
  3755. total_rows++;
  3756. pt_debug(dev, DL_INFO,
  3757. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3758. __func__, cal_blk_size, total_rows, remain_bytes);
  3759. /* Read all rows unless an error occurs */
  3760. rc = 0;
  3761. while (rc == 0 && row_number <= total_rows) {
  3762. act_trans_len = 0;
  3763. if (remain_bytes > 0 && row_number == total_rows)
  3764. transfer_size = remain_bytes;
  3765. else
  3766. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3767. rc = pt_pip1_read_data_block_(cd, row_number,
  3768. transfer_size, PT_CAL_EBID,
  3769. &act_trans_len,
  3770. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3771. &data_block_crc);
  3772. if (rc) {
  3773. /* Error occurred, exit loop */
  3774. cal_size = 0;
  3775. break;
  3776. }
  3777. pt_debug(dev, DL_INFO,
  3778. "%s: CAL read rc=%d actual read len=%d\n",
  3779. __func__, rc, act_trans_len);
  3780. byte_offset += act_trans_len;
  3781. cal_size = byte_offset;
  3782. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3783. pt_pr_buf(dev, DL_INFO,
  3784. &cal_cache_data[byte_offset - act_trans_len],
  3785. act_trans_len, prefix);
  3786. row_number++;
  3787. }
  3788. if (cal_size > 0) {
  3789. /* Save a CRC of the chip info the CAL was saved from */
  3790. calc_id_crc = crc_ccitt_calculate(
  3791. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3792. cal_cache_chip_id = calc_id_crc;
  3793. cal_cache_len = cal_size;
  3794. pt_debug(dev, DL_INFO,
  3795. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3796. __func__, calc_id_crc, cal_size);
  3797. }
  3798. *size = cal_size;
  3799. *crc = calc_id_crc;
  3800. break;
  3801. case PT_CAL_DATA_RESTORE:
  3802. cal_size = cal_cache_len;
  3803. while ((rc == 0) && (byte_offset < cal_size)) {
  3804. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3805. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3806. else
  3807. transfer_size = cal_size - byte_offset;
  3808. rc = pt_pip1_write_data_block_(cd, row_number,
  3809. transfer_size, PT_CAL_EBID,
  3810. &cal_cache_data[byte_offset],
  3811. (u8 *)pt_data_block_security_key,
  3812. &act_trans_len);
  3813. byte_offset += act_trans_len;
  3814. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3815. __func__, byte_offset);
  3816. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3817. pt_pr_buf(dev, DL_INFO,
  3818. &cal_cache_data[byte_offset - act_trans_len],
  3819. act_trans_len, prefix);
  3820. if ((byte_offset > cal_size) ||
  3821. (act_trans_len != transfer_size))
  3822. rc = -EIO;
  3823. row_number++;
  3824. }
  3825. *size = byte_offset;
  3826. *crc = cal_cache_chip_id;
  3827. break;
  3828. case PT_CAL_DATA_CLEAR:
  3829. if (cal_cache_data)
  3830. memset(&cal_cache_data[0], 0, cal_cache_len);
  3831. cal_cache_len = 0;
  3832. cal_cache_chip_id = 0;
  3833. *size = 0;
  3834. *crc = 0;
  3835. break;
  3836. case PT_CAL_DATA_INFO:
  3837. default:
  3838. *size = cal_cache_len;
  3839. *crc = cal_cache_chip_id;
  3840. pt_debug(dev, DL_INFO,
  3841. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3842. __func__, cal_cache_chip_id,
  3843. cal_cache_len);
  3844. break;
  3845. }
  3846. exit:
  3847. pt_debug(dev, DL_INFO,
  3848. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3849. __func__, rc, *crc, *size);
  3850. return rc;
  3851. }
  3852. /*******************************************************************************
  3853. * FUNCTION: pt_pip_run_selftest_
  3854. *
  3855. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3856. * to execute a FW built in self test
  3857. *
  3858. * RETURN:
  3859. * 0 = success
  3860. * !0 = failure
  3861. *
  3862. * PARAMETERS:
  3863. * *cd - pointer to core data
  3864. * test_id - enumerated test ID to run
  3865. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3866. * *status - pointer to store the read response status
  3867. * *summary_results - pointer to store the results summary
  3868. * *results_available - pointer to store if results are available
  3869. *****************************************************************************/
  3870. static int pt_pip_run_selftest_(
  3871. struct pt_core_data *cd, u8 test_id,
  3872. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3873. u8 *results_available)
  3874. {
  3875. int rc = 0;
  3876. u8 write_buf[2];
  3877. struct pt_hid_output hid_output = {
  3878. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3879. .write_length = 2,
  3880. .write_buf = write_buf,
  3881. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3882. };
  3883. write_buf[0] = test_id;
  3884. write_buf[1] = write_idacs_to_flash;
  3885. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3886. hid_output.write_length = 1;
  3887. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3888. if (rc)
  3889. return rc;
  3890. if (status)
  3891. *status = cd->response_buf[5];
  3892. if (summary_result)
  3893. *summary_result = cd->response_buf[6];
  3894. /* results_available only available before PIP 1.03 */
  3895. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3896. if (results_available)
  3897. *results_available = cd->response_buf[7];
  3898. }
  3899. return rc;
  3900. }
  3901. /*******************************************************************************
  3902. * FUNCTION: pt_pip_run_selftest
  3903. *
  3904. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3905. * an exclusive access to the DUT.
  3906. *
  3907. * RETURN:
  3908. * 0 = success
  3909. * !0 = failure
  3910. *
  3911. * PARAMETERS:
  3912. * *cd - pointer to core data
  3913. * test_id - enumerated test ID to run
  3914. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3915. * *status - pointer to store the read response status
  3916. * *summary_results - pointer to store the results summary
  3917. * *results_available - pointer to store if results are available
  3918. ******************************************************************************/
  3919. static int pt_pip_run_selftest(
  3920. struct pt_core_data *cd, u8 test_id,
  3921. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3922. u8 *results_available)
  3923. {
  3924. int rc;
  3925. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3926. if (rc < 0) {
  3927. pt_debug(cd->dev, DL_ERROR,
  3928. "%s: fail get exclusive ex=%p own=%p\n",
  3929. __func__, cd->exclusive_dev, cd->dev);
  3930. return rc;
  3931. }
  3932. rc = pt_pip_run_selftest_(cd, test_id,
  3933. write_idacs_to_flash, status, summary_result,
  3934. results_available);
  3935. if (release_exclusive(cd, cd->dev) < 0)
  3936. pt_debug(cd->dev, DL_ERROR,
  3937. "%s: fail to release exclusive\n", __func__);
  3938. return rc;
  3939. }
  3940. /*******************************************************************************
  3941. * FUNCTION: _pt_request_pip_run_selftest
  3942. *
  3943. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3944. * calls to the protected or unprotected call to pt_pip_run_selftest
  3945. *
  3946. * RETURN:
  3947. * 0 = success
  3948. * !0 = failure
  3949. *
  3950. * PARAMETERS:
  3951. * *dev - pointer to device structure
  3952. * protect - flag to call protected or non-protected
  3953. * test_id - enumerated test ID to run
  3954. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3955. * *status - pointer to store the read response status
  3956. * *summary_results - pointer to store the results summary
  3957. * *results_available - pointer to store if results are available
  3958. ******************************************************************************/
  3959. static int _pt_request_pip_run_selftest(struct device *dev,
  3960. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3961. u8 *summary_result, u8 *results_available)
  3962. {
  3963. struct pt_core_data *cd = dev_get_drvdata(dev);
  3964. if (protect)
  3965. return pt_pip_run_selftest(cd, test_id,
  3966. write_idacs_to_flash, status, summary_result,
  3967. results_available);
  3968. return pt_pip_run_selftest_(cd, test_id,
  3969. write_idacs_to_flash, status, summary_result,
  3970. results_available);
  3971. }
  3972. /*******************************************************************************
  3973. * FUNCTION: _pt_pip_get_selftest_result_
  3974. *
  3975. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3976. * to retrieve the self test results from the self test already executed
  3977. *
  3978. * RETURN:
  3979. * 0 = success
  3980. * !0 = failure
  3981. *
  3982. * PARAMETERS:
  3983. * *cd - pointer to core data
  3984. * read_offset - read pointer offset
  3985. * read_length - length of data to read
  3986. * test_id - enumerated test ID to read selftest results from
  3987. * *status - pointer to store the read response status
  3988. * *actual_read_len - pointer to store data length actually read
  3989. * *status - pointer to where the cmd response statas is stored
  3990. ******************************************************************************/
  3991. static int pt_pip_get_selftest_result_(
  3992. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3993. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  3994. {
  3995. int rc = 0;
  3996. u16 total_read_len = 0;
  3997. u16 read_len;
  3998. u16 off_buf = 0;
  3999. u8 write_buf[5];
  4000. u8 read_test_id;
  4001. bool repeat;
  4002. struct pt_hid_output hid_output = {
  4003. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4004. .write_length = 5,
  4005. .write_buf = write_buf,
  4006. };
  4007. /*
  4008. * Do not repeat reading for Auto Shorts test
  4009. * when PIP version < 1.3
  4010. */
  4011. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4012. || test_id != PT_ST_ID_AUTOSHORTS;
  4013. again:
  4014. write_buf[0] = LOW_BYTE(read_offset);
  4015. write_buf[1] = HI_BYTE(read_offset);
  4016. write_buf[2] = LOW_BYTE(read_length);
  4017. write_buf[3] = HI_BYTE(read_length);
  4018. write_buf[4] = test_id;
  4019. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4020. if (rc)
  4021. return rc;
  4022. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4023. goto set_status;
  4024. read_test_id = cd->response_buf[6];
  4025. if (read_test_id != test_id)
  4026. return -EPROTO;
  4027. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4028. if (read_len && data) {
  4029. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4030. total_read_len += read_len;
  4031. if (repeat && read_len < read_length) {
  4032. read_offset += read_len;
  4033. off_buf += read_len;
  4034. read_length -= read_len;
  4035. goto again;
  4036. }
  4037. }
  4038. if (actual_read_len)
  4039. *actual_read_len = total_read_len;
  4040. set_status:
  4041. if (status)
  4042. *status = cd->response_buf[5];
  4043. return rc;
  4044. }
  4045. /*******************************************************************************
  4046. * FUNCTION: _pt_pip_get_selftest_result
  4047. *
  4048. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4049. * access to the DUT
  4050. *
  4051. * RETURN:
  4052. * 0 = success
  4053. * !0 = failure
  4054. *
  4055. * PARAMETERS:
  4056. * *cd - pointer to core data
  4057. * read_offset - read pointer offset
  4058. * read_length - length of data to read
  4059. * test_id - enumerated test ID to read selftest results from
  4060. * *status - pointer to store the read response status
  4061. * *actual_read_len - pointer to store data length actually read
  4062. * *status - pointer to where the cmd response statas is stored
  4063. ******************************************************************************/
  4064. static int pt_pip_get_selftest_result(
  4065. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4066. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4067. {
  4068. int rc;
  4069. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4070. if (rc < 0) {
  4071. pt_debug(cd->dev, DL_ERROR,
  4072. "%s: fail get exclusive ex=%p own=%p\n",
  4073. __func__, cd->exclusive_dev, cd->dev);
  4074. return rc;
  4075. }
  4076. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4077. read_length, test_id, status, actual_read_len, data);
  4078. if (release_exclusive(cd, cd->dev) < 0)
  4079. pt_debug(cd->dev, DL_ERROR,
  4080. "%s: fail to release exclusive\n", __func__);
  4081. return rc;
  4082. }
  4083. /*******************************************************************************
  4084. * FUNCTION: _pt_request_pip_get_selftest_result
  4085. *
  4086. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4087. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4088. *
  4089. * RETURN:
  4090. * 0 = success
  4091. * !0 = failure
  4092. *
  4093. * PARAMETERS:
  4094. * *dev - pointer to device structure
  4095. * protect - flag to call protected or non-protected
  4096. * read_offset - read pointer offset
  4097. * read_length - length of data to read
  4098. * test_id - enumerated test ID to read selftest results from
  4099. * *status - pointer to store the read response status
  4100. * *actual_read_len - pointer to store data length actually read
  4101. * *data - pointer to where the data read is stored
  4102. ******************************************************************************/
  4103. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4104. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4105. u8 *status, u16 *actual_read_len, u8 *data)
  4106. {
  4107. struct pt_core_data *cd = dev_get_drvdata(dev);
  4108. if (protect)
  4109. return pt_pip_get_selftest_result(cd, read_offset,
  4110. read_length, test_id, status, actual_read_len,
  4111. data);
  4112. return pt_pip_get_selftest_result_(cd, read_offset,
  4113. read_length, test_id, status, actual_read_len,
  4114. data);
  4115. }
  4116. /*******************************************************************************
  4117. * FUNCTION: _pt_pip_load_self_test_param
  4118. *
  4119. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4120. * to load parameters needed by a self test
  4121. *
  4122. * RETURN:
  4123. * 0 = success
  4124. * !0 = failure
  4125. *
  4126. * PARAMETERS:
  4127. * *cd - pointer to core data
  4128. * self_test_id - enumerated test ID for which the parmeters belong
  4129. * load_offset - mem offset to where to load parameters
  4130. * load_length - length of parameter data to load
  4131. * *parameters - pointer to list of parameter data
  4132. * *status - pointer to store the response status
  4133. * *ret_test_id - pointer to returned test id the parameters were stored
  4134. * *act_load_len - pointer to store the actual load length that was writen
  4135. ******************************************************************************/
  4136. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4137. u8 self_test_id, u16 load_offset, u16 load_length,
  4138. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4139. {
  4140. int rc = 0;
  4141. int i;
  4142. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4143. struct pt_hid_output hid_output = {
  4144. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4145. .write_length = 5 + load_length,
  4146. .write_buf = write_buf,
  4147. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4148. };
  4149. write_buf[0] = LOW_BYTE(load_offset);
  4150. write_buf[1] = HI_BYTE(load_offset);
  4151. write_buf[2] = LOW_BYTE(load_length);
  4152. write_buf[3] = HI_BYTE(load_length);
  4153. write_buf[4] = self_test_id;
  4154. for (i = 0; i < load_length; i++)
  4155. write_buf[i + 5] = parameters[i];
  4156. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4157. if (rc)
  4158. return rc;
  4159. if (status)
  4160. *status = cd->response_buf[5];
  4161. if (ret_test_id)
  4162. *ret_test_id = cd->response_buf[6];
  4163. if (act_load_len)
  4164. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4165. return rc;
  4166. }
  4167. /*******************************************************************************
  4168. * FUNCTION: pt_pip_load_self_test_param
  4169. *
  4170. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4171. * access to the DUT.
  4172. *
  4173. * RETURN:
  4174. * 0 = success
  4175. * !0 = failure
  4176. *
  4177. * PARAMETERS:
  4178. * *cd - pointer to core data
  4179. * self_test_id - enumerated test ID for which the parmeters belong
  4180. * load_offset - mem offset to where to load parameters
  4181. * load_length - length of parameter data to load
  4182. * *parameters - pointer to list of parameter data
  4183. * *status - pointer to store the response status
  4184. * *ret_test_id - pointer to returned test id the parameters were stored
  4185. * *act_load_len - pointer to store the actual load length that was writen
  4186. ******************************************************************************/
  4187. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4188. u8 self_test_id, u16 load_offset, u16 load_length,
  4189. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4190. {
  4191. int rc;
  4192. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4193. if (rc < 0) {
  4194. pt_debug(cd->dev, DL_ERROR,
  4195. "%s: fail get exclusive ex=%p own=%p\n",
  4196. __func__, cd->exclusive_dev, cd->dev);
  4197. return rc;
  4198. }
  4199. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4200. load_length, parameters, status, ret_test_id, act_load_len);
  4201. if (release_exclusive(cd, cd->dev) < 0)
  4202. pt_debug(cd->dev, DL_ERROR,
  4203. "%s: fail to release exclusive\n", __func__);
  4204. return rc;
  4205. }
  4206. /*******************************************************************************
  4207. * FUNCTION: _pt_request_pip_load_self_test_param
  4208. *
  4209. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4210. * calls to the protected or unprotected call to
  4211. * pt_pip_load_self_test_param
  4212. *
  4213. * RETURN:
  4214. * 0 = success
  4215. * !0 = failure
  4216. *
  4217. * PARAMETERS:
  4218. * *dev - pointer to device structure
  4219. * protect - flag to call protected or non-protected
  4220. * self_test_id - enumerated test ID for which the parmeters belong
  4221. * load_offset - mem offset to where to load parameters
  4222. * load_length - length of parameter data to load
  4223. * *parameters - pointer to list of parameter data
  4224. * *status - pointer to store the response status
  4225. * *ret_test_id - pointer to returned test id the parameters were stored
  4226. * *act_load_len - pointer to store the actual load length that was writen
  4227. ******************************************************************************/
  4228. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4229. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4230. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4231. {
  4232. struct pt_core_data *cd = dev_get_drvdata(dev);
  4233. if (protect)
  4234. return pt_pip_load_self_test_param(cd, self_test_id,
  4235. load_offset, load_length, parameters, status, ret_test_id,
  4236. act_load_len);
  4237. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4238. load_length, parameters, status, ret_test_id, act_load_len);
  4239. }
  4240. /*******************************************************************************
  4241. * FUNCTION: pt_pip_calibrate_ext_
  4242. *
  4243. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4244. * for the response
  4245. *
  4246. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4247. *
  4248. * RETURN:
  4249. * 0 = success
  4250. * !0 = failure
  4251. *
  4252. * PARAMETERS:
  4253. * *cd - pointer to core data
  4254. * *cal_data - pointer to extended calibration data structure
  4255. * *status - pointer to where the command response status is stored
  4256. ******************************************************************************/
  4257. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4258. struct pt_cal_ext_data *cal_data, u8 *status)
  4259. {
  4260. int rc = 0;
  4261. int write_length = 4;
  4262. u8 write_buf[4];
  4263. u16 size = 0;
  4264. unsigned short crc = 0;
  4265. struct pt_hid_output hid_output = {
  4266. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4267. .write_length = write_length,
  4268. .write_buf = write_buf,
  4269. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4270. };
  4271. if (cal_data == NULL)
  4272. return -EINVAL;
  4273. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4274. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4275. if (rc)
  4276. return rc;
  4277. if (status)
  4278. *status = cd->response_buf[5];
  4279. /*
  4280. * When doing a calibration on a flashless DUT, save CAL data in
  4281. * the TTDL cache on any successful calibration
  4282. */
  4283. if (*status == 0 && cd->cal_cache_in_host) {
  4284. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4285. __func__);
  4286. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4287. &size, &crc);
  4288. if (rc)
  4289. pt_debug(cd->dev, DL_ERROR,
  4290. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4291. else
  4292. pt_debug(cd->dev, DL_INFO,
  4293. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4294. __func__, crc, size);
  4295. }
  4296. return 0;
  4297. }
  4298. /*******************************************************************************
  4299. * FUNCTION: pt_pip_calibrate_ext
  4300. *
  4301. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4302. * DUT
  4303. *
  4304. * RETURN:
  4305. * 0 = success
  4306. * !0 = failure
  4307. *
  4308. * PARAMETERS:
  4309. * *cd - pointer to core data
  4310. * *cal_data - pointer to extended calibration data structure
  4311. * *status - pointer to where the command response status is stored
  4312. ******************************************************************************/
  4313. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4314. struct pt_cal_ext_data *cal_data, u8 *status)
  4315. {
  4316. int rc;
  4317. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4318. if (rc < 0) {
  4319. pt_debug(cd->dev, DL_ERROR,
  4320. "%s: fail get exclusive ex=%p own=%p\n",
  4321. __func__, cd->exclusive_dev, cd->dev);
  4322. return rc;
  4323. }
  4324. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4325. if (release_exclusive(cd, cd->dev) < 0)
  4326. pt_debug(cd->dev, DL_ERROR,
  4327. "%s: fail to release exclusive\n", __func__);
  4328. return rc;
  4329. }
  4330. /*******************************************************************************
  4331. * FUNCTION: _pt_request_pip_calibrate_ext
  4332. *
  4333. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4334. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4335. *
  4336. * RETURN:
  4337. * 0 = success
  4338. * !0 = failure
  4339. *
  4340. * PARAMETERS:
  4341. * *dev - pointer to device structure
  4342. * protect - flag to call protected or non-protected
  4343. * *cal_data - pointer to extended calibration data structure
  4344. * *status - pointer to where the command response status is stored
  4345. ******************************************************************************/
  4346. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4347. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4348. {
  4349. struct pt_core_data *cd = dev_get_drvdata(dev);
  4350. if (protect)
  4351. return pt_pip_calibrate_ext(cd, cal_data, status);
  4352. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4353. }
  4354. /*******************************************************************************
  4355. * FUNCTION: pt_pip_calibrate_idacs_
  4356. *
  4357. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4358. * for the response
  4359. *
  4360. * RETURN:
  4361. * 0 = success
  4362. * !0 = failure
  4363. *
  4364. * PARAMETERS:
  4365. * *cd - pointer to core data
  4366. * mode - sense mode to calibrate (0-5)
  4367. * *status - pointer to where the command response status is stored
  4368. ******************************************************************************/
  4369. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4370. u8 mode, u8 *status)
  4371. {
  4372. int rc = 0;
  4373. int write_length = 1;
  4374. u8 write_buf[1];
  4375. u8 cmd_offset = 0;
  4376. struct pt_hid_output hid_output = {
  4377. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4378. .write_length = write_length,
  4379. .write_buf = write_buf,
  4380. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4381. };
  4382. write_buf[cmd_offset++] = mode;
  4383. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4384. if (rc)
  4385. return rc;
  4386. *status = cd->response_buf[5];
  4387. if (*status)
  4388. return -EINVAL;
  4389. return 0;
  4390. }
  4391. /*******************************************************************************
  4392. * FUNCTION: pt_pip_calibrate_idacs
  4393. *
  4394. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4395. * access to the DUT
  4396. *
  4397. * RETURN:
  4398. * 0 = success
  4399. * !0 = failure
  4400. *
  4401. * PARAMETERS:
  4402. * *cd - pointer to core data
  4403. * mode - sense mode to calibrate (0-5)
  4404. * *status - pointer to where the command response status is stored
  4405. ******************************************************************************/
  4406. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4407. u8 mode, u8 *status)
  4408. {
  4409. int rc;
  4410. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4411. if (rc < 0) {
  4412. pt_debug(cd->dev, DL_ERROR,
  4413. "%s: fail get exclusive ex=%p own=%p\n",
  4414. __func__, cd->exclusive_dev, cd->dev);
  4415. return rc;
  4416. }
  4417. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4418. if (release_exclusive(cd, cd->dev) < 0)
  4419. pt_debug(cd->dev, DL_ERROR,
  4420. "%s: fail to release exclusive\n", __func__);
  4421. return rc;
  4422. }
  4423. /*******************************************************************************
  4424. * FUNCTION: _pt_request_pip_calibrate_idacs
  4425. *
  4426. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4427. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4428. *
  4429. * RETURN:
  4430. * 0 = success
  4431. * !0 = failure
  4432. *
  4433. * PARAMETERS:
  4434. * *dev - pointer to device structure
  4435. * protect - flag to call protected or non-protected
  4436. * mode - sense mode to calibrate (0-5)
  4437. * *status - pointer to where the command response status is stored
  4438. ******************************************************************************/
  4439. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4440. int protect, u8 mode, u8 *status)
  4441. {
  4442. struct pt_core_data *cd = dev_get_drvdata(dev);
  4443. if (protect)
  4444. return pt_pip_calibrate_idacs(cd, mode, status);
  4445. return pt_pip_calibrate_idacs_(cd, mode, status);
  4446. }
  4447. /*******************************************************************************
  4448. * FUNCTION: pt_hid_output_initialize_baselines_
  4449. *
  4450. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4451. * waiting for the response.
  4452. *
  4453. * RETURN:
  4454. * 0 = success
  4455. * !0 = failure
  4456. *
  4457. * PARAMETERS:
  4458. * *cd - pointer to core data
  4459. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4460. * each or together with a single command.
  4461. * *status - pointer to where the command response status is stored
  4462. ******************************************************************************/
  4463. static int pt_hid_output_initialize_baselines_(
  4464. struct pt_core_data *cd, u8 test_id, u8 *status)
  4465. {
  4466. int rc = 0;
  4467. int write_length = 1;
  4468. u8 write_buf[1];
  4469. u8 cmd_offset = 0;
  4470. struct pt_hid_output hid_output = {
  4471. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4472. .write_length = write_length,
  4473. .write_buf = write_buf,
  4474. };
  4475. write_buf[cmd_offset++] = test_id;
  4476. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4477. if (rc)
  4478. return rc;
  4479. *status = cd->response_buf[5];
  4480. if (*status)
  4481. return -EINVAL;
  4482. return rc;
  4483. }
  4484. /*******************************************************************************
  4485. * FUNCTION: pt_hid_output_initialize_baselines
  4486. *
  4487. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4488. * access to the DUT
  4489. *
  4490. * RETURN:
  4491. * 0 = success
  4492. * !0 = failure
  4493. *
  4494. * PARAMETERS:
  4495. * *cd - pointer to core data
  4496. * test_id - enumerated ID against which to initialize the baseline
  4497. * *status - pointer to where the command response status is stored
  4498. ******************************************************************************/
  4499. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4500. u8 test_id, u8 *status)
  4501. {
  4502. int rc;
  4503. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4504. if (rc < 0) {
  4505. pt_debug(cd->dev, DL_ERROR,
  4506. "%s: fail get exclusive ex=%p own=%p\n",
  4507. __func__, cd->exclusive_dev, cd->dev);
  4508. return rc;
  4509. }
  4510. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4511. if (release_exclusive(cd, cd->dev) < 0)
  4512. pt_debug(cd->dev, DL_ERROR,
  4513. "%s: fail to release exclusive\n", __func__);
  4514. return rc;
  4515. }
  4516. /*******************************************************************************
  4517. * FUNCTION: _pt_request_pip_initialize_baselines
  4518. *
  4519. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4520. * calls to the protected or unprotected call to
  4521. * pt_pip_initialize_baselines
  4522. *
  4523. * RETURN:
  4524. * 0 = success
  4525. * !0 = failure
  4526. *
  4527. * PARAMETERS:
  4528. * *dev - pointer to device structure
  4529. * protect - flag to call protected or non-protected
  4530. * test_id - enumerated ID against which to initialize the baseline
  4531. * *status - pointer to where the command response status is stored
  4532. ******************************************************************************/
  4533. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4534. int protect, u8 test_id, u8 *status)
  4535. {
  4536. struct pt_core_data *cd = dev_get_drvdata(dev);
  4537. if (protect)
  4538. return pt_hid_output_initialize_baselines(cd, test_id,
  4539. status);
  4540. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4541. }
  4542. /*******************************************************************************
  4543. * FUNCTION: pt_hid_output_exec_panel_scan_
  4544. *
  4545. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4546. * the response
  4547. *
  4548. * RETURN:
  4549. * 0 = success
  4550. * !0 = failure
  4551. *
  4552. * PARAMETERS:
  4553. * *cd - pointer to core data
  4554. ******************************************************************************/
  4555. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4556. {
  4557. struct pt_hid_output hid_output = {
  4558. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4559. };
  4560. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4561. }
  4562. /*******************************************************************************
  4563. * FUNCTION: pt_hid_output_exec_panel_scan
  4564. *
  4565. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4566. * access to the DUT.
  4567. *
  4568. * RETURN:
  4569. * 0 = success
  4570. * !0 = failure
  4571. *
  4572. * PARAMETERS:
  4573. * *cd - pointer to core data
  4574. ******************************************************************************/
  4575. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4576. {
  4577. int rc;
  4578. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4579. if (rc < 0) {
  4580. pt_debug(cd->dev, DL_ERROR,
  4581. "%s: fail get exclusive ex=%p own=%p\n",
  4582. __func__, cd->exclusive_dev, cd->dev);
  4583. return rc;
  4584. }
  4585. rc = pt_hid_output_exec_panel_scan_(cd);
  4586. if (release_exclusive(cd, cd->dev) < 0)
  4587. pt_debug(cd->dev, DL_ERROR,
  4588. "%s: fail to release exclusive\n", __func__);
  4589. return rc;
  4590. }
  4591. /*******************************************************************************
  4592. * FUNCTION: pt_pip2_exec_panel_scan_
  4593. *
  4594. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4595. * the response
  4596. *
  4597. * RETURN:
  4598. * 0 = success
  4599. * !0 = failure
  4600. *
  4601. * PARAMETERS:
  4602. * *cd - pointer to core data
  4603. * scan_type - type of panel scan to perform (PIP2 only)
  4604. ******************************************************************************/
  4605. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4606. {
  4607. int rc = 0;
  4608. u8 data[2];
  4609. u8 read_buf[10];
  4610. u16 actual_read_len;
  4611. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4612. __func__, scan_type);
  4613. data[0] = scan_type;
  4614. rc = _pt_request_pip2_send_cmd(cd->dev,
  4615. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4616. data, 1, read_buf, &actual_read_len);
  4617. if (rc) {
  4618. pt_debug(cd->dev, DL_ERROR,
  4619. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4620. __func__, scan_type, rc);
  4621. }
  4622. return rc;
  4623. }
  4624. /*******************************************************************************
  4625. * FUNCTION: pt_pip2_exec_panel_scan
  4626. *
  4627. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4628. * access to the DUT.
  4629. *
  4630. * RETURN:
  4631. * 0 = success
  4632. * !0 = failure
  4633. *
  4634. * PARAMETERS:
  4635. * *cd - pointer to core data
  4636. * scan_type - type of panel scan to perform (PIP2 only)
  4637. ******************************************************************************/
  4638. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4639. {
  4640. int rc;
  4641. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4642. if (rc < 0) {
  4643. pt_debug(cd->dev, DL_ERROR,
  4644. "%s: fail get exclusive ex=%p own=%p\n",
  4645. __func__, cd->exclusive_dev, cd->dev);
  4646. return rc;
  4647. }
  4648. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4649. if (release_exclusive(cd, cd->dev) < 0)
  4650. pt_debug(cd->dev, DL_ERROR,
  4651. "%s: fail to release exclusive\n", __func__);
  4652. return rc;
  4653. }
  4654. /*******************************************************************************
  4655. * FUNCTION: _pt_request_pip_exec_panel_scan
  4656. *
  4657. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4658. * calls to the protected or unprotected call to
  4659. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4660. *
  4661. * RETURN:
  4662. * 0 = success
  4663. * !0 = failure
  4664. *
  4665. * PARAMETERS:
  4666. * *dev - pointer to device structure
  4667. * protect - flag to call protected or non-protected
  4668. * scan_type - type of panel scan to perform (PIP2 only)
  4669. ******************************************************************************/
  4670. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4671. int protect, u8 scan_type)
  4672. {
  4673. struct pt_core_data *cd = dev_get_drvdata(dev);
  4674. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4675. if (protect)
  4676. return pt_pip2_exec_panel_scan(cd, scan_type);
  4677. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4678. }
  4679. if (protect)
  4680. return pt_hid_output_exec_panel_scan(cd);
  4681. return pt_hid_output_exec_panel_scan_(cd);
  4682. }
  4683. /*******************************************************************************
  4684. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4685. *
  4686. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4687. * to retrieve the specified data type for a the last successful Execute
  4688. * Panel Scan command.
  4689. *
  4690. * RETURN:
  4691. * 0 = success
  4692. * !0 = failure
  4693. *
  4694. * PARAMETERS:
  4695. * *dev - pointer to device structure
  4696. * protect - flag to call protected or non-protected
  4697. * read_offset - read pointer offset
  4698. * read_count - length of data to read
  4699. * data_id - enumerated test ID to read selftest results from
  4700. * *response - pointer to store the read response status
  4701. * *config - pointer to store config data
  4702. * *actual_read_len - pointer to store data length actually read
  4703. * *read_buf - pointer to the read buffer
  4704. ******************************************************************************/
  4705. static int pt_hid_output_retrieve_panel_scan_(
  4706. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4707. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4708. u8 *read_buf)
  4709. {
  4710. int status;
  4711. u8 read_data_id;
  4712. int rc = 0;
  4713. int write_length = 5;
  4714. u8 write_buf[5];
  4715. u8 cmd_offset = 0;
  4716. u8 data_elem_size;
  4717. int size;
  4718. int data_size;
  4719. struct pt_hid_output hid_output = {
  4720. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4721. .write_length = write_length,
  4722. .write_buf = write_buf,
  4723. };
  4724. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4725. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4726. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4727. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4728. write_buf[cmd_offset++] = data_id;
  4729. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4730. if (rc)
  4731. return rc;
  4732. status = cd->response_buf[5];
  4733. if (status)
  4734. return -EINVAL;
  4735. read_data_id = cd->response_buf[6];
  4736. if (read_data_id != data_id)
  4737. return -EPROTO;
  4738. size = get_unaligned_le16(&cd->response_buf[0]);
  4739. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4740. *config = cd->response_buf[9];
  4741. data_elem_size = *config & 0x07;
  4742. data_size = *actual_read_len * data_elem_size;
  4743. if (read_buf)
  4744. memcpy(read_buf, &cd->response_buf[10], data_size);
  4745. if (response)
  4746. memcpy(response, cd->response_buf, size);
  4747. return rc;
  4748. }
  4749. /*******************************************************************************
  4750. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4751. *
  4752. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4753. * access to the DUT.
  4754. *
  4755. * RETURN:
  4756. * 0 = success
  4757. * !0 = failure
  4758. *
  4759. * PARAMETERS:
  4760. * *dev - pointer to device structure
  4761. * protect - flag to call protected or non-protected
  4762. * read_offset - read pointer offset
  4763. * read_count - length of data to read
  4764. * data_id - enumerated test ID to read selftest results from
  4765. * *response - pointer to store the read response status
  4766. * *config - pointer to store config data
  4767. * *actual_read_len - pointer to store data length actually read
  4768. * *read_buf - pointer to the read buffer
  4769. ******************************************************************************/
  4770. static int pt_hid_output_retrieve_panel_scan(
  4771. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4772. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4773. u8 *read_buf)
  4774. {
  4775. int rc;
  4776. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4777. if (rc < 0) {
  4778. pt_debug(cd->dev, DL_ERROR,
  4779. "%s: fail get exclusive ex=%p own=%p\n",
  4780. __func__, cd->exclusive_dev, cd->dev);
  4781. return rc;
  4782. }
  4783. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4784. read_count, data_id, response, config,
  4785. actual_read_len, read_buf);
  4786. if (release_exclusive(cd, cd->dev) < 0)
  4787. pt_debug(cd->dev, DL_ERROR,
  4788. "%s: fail to release exclusive\n", __func__);
  4789. return rc;
  4790. }
  4791. /*******************************************************************************
  4792. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4793. *
  4794. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4795. * calls to the protected or unprotected call to
  4796. * pt_hid_output_retrieve_panel_scan
  4797. *
  4798. * RETURN:
  4799. * 0 = success
  4800. * !0 = failure
  4801. *
  4802. * PARAMETERS:
  4803. * *dev - pointer to device structure
  4804. * protect - flag to call protected or non-protected
  4805. * read_offset - read pointer offset
  4806. * read_count - length of data to read
  4807. * data_id - enumerated test ID to read selftest results from
  4808. * *response - pointer to store the read response status
  4809. * *config - pointer to store config data
  4810. * *actual_read_len - pointer to store data length actually read
  4811. * *read_buf - pointer to the read buffer
  4812. ******************************************************************************/
  4813. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4814. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4815. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4816. {
  4817. struct pt_core_data *cd = dev_get_drvdata(dev);
  4818. if (protect)
  4819. return pt_hid_output_retrieve_panel_scan(cd,
  4820. read_offset, read_count, data_id, response,
  4821. config, actual_read_len, read_buf);
  4822. return pt_hid_output_retrieve_panel_scan_(cd,
  4823. read_offset, read_count, data_id, response,
  4824. config, actual_read_len, read_buf);
  4825. }
  4826. /*******************************************************************************
  4827. * FUNCTION: _pt_request_pip_user_cmd
  4828. *
  4829. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4830. * calls to the protected or unprotected call to
  4831. * pt_hid_output_user_cmd
  4832. *
  4833. * RETURN:
  4834. * 0 = success
  4835. * !0 = failure
  4836. *
  4837. * PARAMETERS:
  4838. * *dev - pointer to device structure
  4839. * protect - flag to call protected or non-protected
  4840. * read_len - length of data to read
  4841. * *read_buf - pointer to store read data
  4842. * write_len - length of data to write
  4843. * *write_buf - pointer to buffer to write
  4844. * *actual_read_len - pointer to store data length actually read
  4845. ******************************************************************************/
  4846. static int _pt_request_pip_user_cmd(struct device *dev,
  4847. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4848. u8 *write_buf, u16 *actual_read_len)
  4849. {
  4850. struct pt_core_data *cd = dev_get_drvdata(dev);
  4851. if (protect)
  4852. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4853. write_len, write_buf, actual_read_len);
  4854. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4855. write_len, write_buf, actual_read_len);
  4856. }
  4857. /*******************************************************************************
  4858. * FUNCTION: pt_hid_output_bl_get_information_
  4859. *
  4860. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4861. * DUT to retrieve bootloader version and chip identification information.
  4862. *
  4863. * RETURN:
  4864. * 0 = success
  4865. * !0 = failure
  4866. *
  4867. * PARAMETERS:
  4868. * *cd - pointer to core data
  4869. * *return_data - pointer to store the return data
  4870. *****************************************************************************/
  4871. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4872. u8 *return_data)
  4873. {
  4874. int rc;
  4875. int data_len;
  4876. struct pt_hid_output hid_output = {
  4877. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4878. };
  4879. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4880. if (rc)
  4881. return rc;
  4882. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4883. if (!data_len)
  4884. return -EPROTO;
  4885. memcpy(return_data, &cd->response_buf[8], data_len);
  4886. return 0;
  4887. }
  4888. /*******************************************************************************
  4889. * FUNCTION: pt_hid_output_bl_get_information
  4890. *
  4891. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4892. * access to the DUT.
  4893. *
  4894. * RETURN:
  4895. * 0 = success
  4896. * !0 = failure
  4897. *
  4898. * PARAMETERS:
  4899. * *cd - pointer to core data
  4900. * *return_data - pointer to store the return data
  4901. ******************************************************************************/
  4902. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4903. u8 *return_data)
  4904. {
  4905. int rc;
  4906. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4907. if (rc < 0) {
  4908. pt_debug(cd->dev, DL_ERROR,
  4909. "%s: fail get exclusive ex=%p own=%p\n",
  4910. __func__, cd->exclusive_dev, cd->dev);
  4911. return rc;
  4912. }
  4913. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4914. if (release_exclusive(cd, cd->dev) < 0)
  4915. pt_debug(cd->dev, DL_ERROR,
  4916. "%s: fail to release exclusive\n", __func__);
  4917. return rc;
  4918. }
  4919. /*******************************************************************************
  4920. * FUNCTION: _pt_request_pip_bl_get_information
  4921. *
  4922. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4923. * calls to the protected or unprotected call to
  4924. * pt_hid_output_bl_get_information
  4925. *
  4926. * RETURN:
  4927. * 0 = success
  4928. * !0 = failure
  4929. *
  4930. * PARAMETERS:
  4931. * *dev - pointer to device structure
  4932. * protect - flag to call protected or non-protected
  4933. * *return_data - pointer to store bl data
  4934. ******************************************************************************/
  4935. static int _pt_request_pip_bl_get_information(struct device *dev,
  4936. int protect, u8 *return_data)
  4937. {
  4938. struct pt_core_data *cd = dev_get_drvdata(dev);
  4939. if (protect)
  4940. return pt_hid_output_bl_get_information(cd, return_data);
  4941. return pt_hid_output_bl_get_information_(cd, return_data);
  4942. }
  4943. /*******************************************************************************
  4944. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4945. *
  4946. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4947. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4948. * and Design Data block in flash and enables the host to execute the Program
  4949. * and Verify Row command to bootload the application image and data.
  4950. *
  4951. * RETURN:
  4952. * 0 = success
  4953. * !0 = failure
  4954. *
  4955. * PARAMETERS:
  4956. * *cd - pointer to core data
  4957. * protect - flag to call protected or non-protected
  4958. * key_size - size of key
  4959. * *key_buf - pointer to key data to allow operation
  4960. * row_size - size of the meta data row
  4961. * *metadata_row_buf - pointer to meta data to write
  4962. ******************************************************************************/
  4963. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4964. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4965. {
  4966. u16 write_length = key_size + row_size;
  4967. u8 *write_buf;
  4968. int rc = 0;
  4969. struct pt_hid_output hid_output = {
  4970. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4971. .write_length = write_length,
  4972. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4973. };
  4974. write_buf = kzalloc(write_length, GFP_KERNEL);
  4975. if (!write_buf)
  4976. return -ENOMEM;
  4977. hid_output.write_buf = write_buf;
  4978. if (key_size)
  4979. memcpy(write_buf, key_buf, key_size);
  4980. if (row_size)
  4981. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4982. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4983. kfree(write_buf);
  4984. return rc;
  4985. }
  4986. /*******************************************************************************
  4987. * FUNCTION: pt_hid_output_bl_initiate_bl
  4988. *
  4989. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4990. * access to the DUT.
  4991. *
  4992. * RETURN:
  4993. * 0 = success
  4994. * !0 = failure
  4995. *
  4996. * PARAMETERS:
  4997. * *cd - pointer to core data
  4998. * key_size - size of key
  4999. * *key_buf - pointer to key data to allow operation
  5000. * row_size - size of the meta data row
  5001. * *metadata_row_buf - pointer to meta data to write
  5002. ******************************************************************************/
  5003. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5004. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5005. {
  5006. int rc;
  5007. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5008. if (rc < 0) {
  5009. pt_debug(cd->dev, DL_ERROR,
  5010. "%s: fail get exclusive ex=%p own=%p\n",
  5011. __func__, cd->exclusive_dev, cd->dev);
  5012. return rc;
  5013. }
  5014. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5015. row_size, metadata_row_buf);
  5016. if (release_exclusive(cd, cd->dev) < 0)
  5017. pt_debug(cd->dev, DL_ERROR,
  5018. "%s: fail to release exclusive\n", __func__);
  5019. return rc;
  5020. }
  5021. /*******************************************************************************
  5022. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5023. *
  5024. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5025. * calls to the protected or unprotected call to
  5026. * pt_hid_output_bl_initiate_bl
  5027. *
  5028. * RETURN:
  5029. * 0 = success
  5030. * !0 = failure
  5031. *
  5032. * PARAMETERS:
  5033. * *dev - pointer to device structure
  5034. * protect - flag to call protected or non-protected
  5035. * key_size - size of key
  5036. * *key_buf - pointer to key data to allow operation
  5037. * row_size - size of the meta data row
  5038. * *metadata_row_buf - pointer to meta data to write
  5039. ******************************************************************************/
  5040. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5041. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5042. u8 *metadata_row_buf)
  5043. {
  5044. struct pt_core_data *cd = dev_get_drvdata(dev);
  5045. if (protect)
  5046. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5047. row_size, metadata_row_buf);
  5048. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5049. row_size, metadata_row_buf);
  5050. }
  5051. /*******************************************************************************
  5052. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5053. *
  5054. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5055. * and program a 128-byte row into the flash, and then verifies written data.
  5056. *
  5057. * RETURN:
  5058. * 0 = success
  5059. * !0 = failure
  5060. *
  5061. * PARAMETERS:
  5062. * *cd - pointer to core data
  5063. * data_len - length of data_buf
  5064. * *data_buf - firmware image to program
  5065. ******************************************************************************/
  5066. static int pt_hid_output_bl_program_and_verify_(
  5067. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5068. {
  5069. struct pt_hid_output hid_output = {
  5070. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5071. .write_length = data_len,
  5072. .write_buf = data_buf,
  5073. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5074. };
  5075. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5076. }
  5077. /*******************************************************************************
  5078. * FUNCTION: pt_hid_output_bl_program_and_verify
  5079. *
  5080. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5081. * access to the DUT.
  5082. *
  5083. * RETURN:
  5084. * 0 = success
  5085. * !0 = failure
  5086. *
  5087. * PARAMETERS:
  5088. * *cd - pointer to core data
  5089. * data_len - length of data_buf
  5090. * *data_buf - firmware image to program
  5091. ******************************************************************************/
  5092. static int pt_hid_output_bl_program_and_verify(
  5093. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5094. {
  5095. int rc;
  5096. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5097. if (rc < 0) {
  5098. pt_debug(cd->dev, DL_ERROR,
  5099. "%s: fail get exclusive ex=%p own=%p\n",
  5100. __func__, cd->exclusive_dev, cd->dev);
  5101. return rc;
  5102. }
  5103. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5104. if (release_exclusive(cd, cd->dev) < 0)
  5105. pt_debug(cd->dev, DL_ERROR,
  5106. "%s: fail to release exclusive\n", __func__);
  5107. return rc;
  5108. }
  5109. /*******************************************************************************
  5110. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5111. *
  5112. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5113. * to request to have the BL program and verify a FW image
  5114. *
  5115. * RETURN:
  5116. * 0 = success
  5117. * !0 = failure
  5118. *
  5119. * PARAMETERS:
  5120. * *dev - pointer to device structure
  5121. * protect - boolean to determine to call the protected function
  5122. * data_len - length of data_buf
  5123. * *data_buf - firmware image to program
  5124. ******************************************************************************/
  5125. static int _pt_request_pip_bl_program_and_verify(
  5126. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5127. {
  5128. struct pt_core_data *cd = dev_get_drvdata(dev);
  5129. if (protect)
  5130. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5131. data_buf);
  5132. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5133. data_buf);
  5134. }
  5135. /*******************************************************************************
  5136. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5137. *
  5138. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5139. * perform a full verification of the application integrity by calculating the
  5140. * CRC of the image in flash and compare it to the expected CRC stored in the
  5141. * Metadata row.
  5142. *
  5143. * RETURN:
  5144. * 0 = success
  5145. * !0 = failure
  5146. *
  5147. * PARAMETERS:
  5148. * *cd - pointer to core data
  5149. * *result - pointer to store result
  5150. ******************************************************************************/
  5151. static int pt_hid_output_bl_verify_app_integrity_(
  5152. struct pt_core_data *cd, u8 *result)
  5153. {
  5154. int rc;
  5155. struct pt_hid_output hid_output = {
  5156. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5157. };
  5158. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5159. if (rc) {
  5160. *result = 0;
  5161. return rc;
  5162. }
  5163. *result = cd->response_buf[8];
  5164. return 0;
  5165. }
  5166. /*******************************************************************************
  5167. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5168. *
  5169. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5170. * exclusive access to the DUT.
  5171. *
  5172. * RETURN:
  5173. * 0 = success
  5174. * !0 = failure
  5175. *
  5176. * PARAMETERS:
  5177. * *cd - pointer to core data
  5178. * *result - pointer to store result
  5179. ******************************************************************************/
  5180. static int pt_hid_output_bl_verify_app_integrity(
  5181. struct pt_core_data *cd, u8 *result)
  5182. {
  5183. int rc;
  5184. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5185. if (rc < 0) {
  5186. pt_debug(cd->dev, DL_ERROR,
  5187. "%s: fail get exclusive ex=%p own=%p\n",
  5188. __func__, cd->exclusive_dev, cd->dev);
  5189. return rc;
  5190. }
  5191. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5192. if (release_exclusive(cd, cd->dev) < 0)
  5193. pt_debug(cd->dev, DL_ERROR,
  5194. "%s: fail to release exclusive\n", __func__);
  5195. return rc;
  5196. }
  5197. /*******************************************************************************
  5198. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5199. *
  5200. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5201. * to request to have the BL verify the application integrity (PIP1.x only)
  5202. *
  5203. * RETURN:
  5204. * 0 = success
  5205. * !0 = failure
  5206. *
  5207. * PARAMETERS:
  5208. * *dev - pointer to device structure
  5209. * protect - boolean to determine to call the protected function
  5210. * *result - pointer to store result
  5211. ******************************************************************************/
  5212. static int _pt_request_pip_bl_verify_app_integrity(
  5213. struct device *dev, int protect, u8 *result)
  5214. {
  5215. struct pt_core_data *cd = dev_get_drvdata(dev);
  5216. if (protect)
  5217. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5218. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5219. }
  5220. /*******************************************************************************
  5221. * FUNCTION: pt_hid_output_bl_launch_app_
  5222. *
  5223. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5224. * application from bootloader (PIP1.x only).
  5225. *
  5226. * RETURN:
  5227. * 0 = success
  5228. * !0 = failure
  5229. *
  5230. * PARAMETERS:
  5231. * *cd - pointer to core data
  5232. ******************************************************************************/
  5233. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5234. {
  5235. struct pt_hid_output hid_output = {
  5236. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5237. .reset_expected = 1,
  5238. };
  5239. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5240. }
  5241. /*******************************************************************************
  5242. * FUNCTION: pt_hid_output_bl_launch_app
  5243. *
  5244. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5245. * to the DUT.
  5246. *
  5247. * RETURN:
  5248. * 0 = success
  5249. * !0 = failure
  5250. *
  5251. * PARAMETERS:
  5252. * *cd - pointer to core data
  5253. ******************************************************************************/
  5254. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5255. {
  5256. int rc;
  5257. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5258. if (rc < 0) {
  5259. pt_debug(cd->dev, DL_ERROR,
  5260. "%s: fail get exclusive ex=%p own=%p\n",
  5261. __func__, cd->exclusive_dev, cd->dev);
  5262. return rc;
  5263. }
  5264. rc = pt_hid_output_bl_launch_app_(cd);
  5265. if (release_exclusive(cd, cd->dev) < 0)
  5266. pt_debug(cd->dev, DL_ERROR,
  5267. "%s: fail to release exclusive\n", __func__);
  5268. return rc;
  5269. }
  5270. /*******************************************************************************
  5271. * FUNCTION: _pt_request_pip_launch_app
  5272. *
  5273. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5274. * to request to have the BL launch the application. (PIP1.x only)
  5275. *
  5276. * RETURN:
  5277. * 0 = success
  5278. * !0 = failure
  5279. *
  5280. * PARAMETERS:
  5281. * *dev - pointer to device structure
  5282. * protect - boolean to determine to call the protected function
  5283. ******************************************************************************/
  5284. static int _pt_request_pip_launch_app(struct device *dev,
  5285. int protect)
  5286. {
  5287. struct pt_core_data *cd = dev_get_drvdata(dev);
  5288. if (protect)
  5289. return pt_hid_output_bl_launch_app(cd);
  5290. return pt_hid_output_bl_launch_app_(cd);
  5291. }
  5292. /*******************************************************************************
  5293. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5294. *
  5295. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5296. * value store in the System Information.
  5297. *
  5298. * RETURN:
  5299. * 0 = success
  5300. * !0 = failure
  5301. *
  5302. * PARAMETERS:
  5303. * *cd - pointer to core data
  5304. * *panel_id - pointer to where the panel ID will be stored
  5305. ******************************************************************************/
  5306. static int pt_hid_output_bl_get_panel_id_(
  5307. struct pt_core_data *cd, u8 *panel_id)
  5308. {
  5309. int rc;
  5310. struct pt_hid_output hid_output = {
  5311. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5312. };
  5313. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5314. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5315. pt_debug(cd->dev, DL_ERROR,
  5316. "%s: Get Panel ID command not supported\n",
  5317. __func__);
  5318. *panel_id = PANEL_ID_NOT_ENABLED;
  5319. return 0;
  5320. } else if (rc < 0) {
  5321. pt_debug(cd->dev, DL_ERROR,
  5322. "%s: Error on Get Panel ID command\n", __func__);
  5323. return rc;
  5324. }
  5325. *panel_id = cd->response_buf[8];
  5326. return 0;
  5327. }
  5328. /*******************************************************************************
  5329. * FUNCTION: pt_hid_output_bl_get_panel_id
  5330. *
  5331. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5332. * to the DUT.
  5333. *
  5334. * RETURN:
  5335. * 0 = success
  5336. * !0 = failure
  5337. *
  5338. * PARAMETERS:
  5339. * *cd - pointer to core data
  5340. * *panel_id - pointer to where the panel ID will be stored
  5341. ******************************************************************************/
  5342. static int pt_hid_output_bl_get_panel_id(
  5343. struct pt_core_data *cd, u8 *panel_id)
  5344. {
  5345. int rc;
  5346. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5347. if (rc < 0) {
  5348. pt_debug(cd->dev, DL_ERROR,
  5349. "%s: fail get exclusive ex=%p own=%p\n",
  5350. __func__, cd->exclusive_dev, cd->dev);
  5351. return rc;
  5352. }
  5353. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5354. if (release_exclusive(cd, cd->dev) < 0)
  5355. pt_debug(cd->dev, DL_ERROR,
  5356. "%s: fail to release exclusive\n", __func__);
  5357. return rc;
  5358. }
  5359. /*******************************************************************************
  5360. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5361. *
  5362. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5363. * to have the BL retrieve the panel ID
  5364. *
  5365. * RETURN:
  5366. * 0 = success
  5367. * !0 = failure
  5368. *
  5369. * PARAMETERS:
  5370. * *dev - pointer to device structure
  5371. * protect - flag to run in protected mode
  5372. * *panel_id - pointer to where the panel ID will be stored
  5373. ******************************************************************************/
  5374. static int _pt_request_pip_bl_get_panel_id(
  5375. struct device *dev, int protect, u8 *panel_id)
  5376. {
  5377. struct pt_core_data *cd = dev_get_drvdata(dev);
  5378. if (protect)
  5379. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5380. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5381. }
  5382. /*******************************************************************************
  5383. * FUNCTION: pt_pip2_get_mode_sysmode_
  5384. *
  5385. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5386. * PIP2 STATUS command.
  5387. *
  5388. * RETURN:
  5389. * 0 = success
  5390. * !0 = failure
  5391. *
  5392. * PARAMETERS:
  5393. * *cd - pointer to core data structure
  5394. * *mode - pointer to store the retrieved mode
  5395. * *sys_mode - pointer to store the FW system mode
  5396. ******************************************************************************/
  5397. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5398. u8 *mode, u8 *sys_mode)
  5399. {
  5400. u16 actual_read_len;
  5401. u8 read_buf[12];
  5402. u8 status, boot;
  5403. int rc = 0;
  5404. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5405. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5406. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5407. __func__, rc);
  5408. if (!rc) {
  5409. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5410. "PIP2 STATUS");
  5411. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5412. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5413. if (sys_mode) {
  5414. if (status == PIP2_RSP_ERR_NONE &&
  5415. boot == PIP2_STATUS_APP_EXEC)
  5416. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5417. else
  5418. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5419. }
  5420. if (mode) {
  5421. if (status == PIP2_RSP_ERR_NONE &&
  5422. boot == PIP2_STATUS_BOOT_EXEC)
  5423. *mode = PT_MODE_BOOTLOADER;
  5424. else if (status == PIP2_RSP_ERR_NONE &&
  5425. boot == PIP2_STATUS_APP_EXEC)
  5426. *mode = PT_MODE_OPERATIONAL;
  5427. else
  5428. *mode = PT_MODE_UNKNOWN;
  5429. }
  5430. } else {
  5431. if (mode)
  5432. *mode = PT_MODE_UNKNOWN;
  5433. if (sys_mode)
  5434. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5435. pt_debug(cd->dev, DL_WARN,
  5436. "%s: Mode and sys_mode could not be determined\n",
  5437. __func__);
  5438. }
  5439. return rc;
  5440. }
  5441. /*******************************************************************************
  5442. * FUNCTION: pt_pip2_get_mode_sysmode
  5443. *
  5444. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5445. * access to the DUT.
  5446. *
  5447. * RETURN:
  5448. * 0 = success
  5449. * !0 = failure
  5450. *
  5451. * PARAMETERS:
  5452. * *cd - pointer to core data structure
  5453. * *mode - pointer to store the retrieved mode
  5454. * *sys_mode - pointer to store the FW system mode
  5455. ******************************************************************************/
  5456. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5457. u8 *mode, u8 *sys_mode)
  5458. {
  5459. int rc;
  5460. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5461. if (rc < 0) {
  5462. pt_debug(cd->dev, DL_ERROR,
  5463. "%s: fail get exclusive ex=%p own=%p\n",
  5464. __func__, cd->exclusive_dev, cd->dev);
  5465. return rc;
  5466. }
  5467. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5468. if (release_exclusive(cd, cd->dev) < 0)
  5469. pt_debug(cd->dev, DL_ERROR,
  5470. "%s: fail to release exclusive\n", __func__);
  5471. return rc;
  5472. }
  5473. /*******************************************************************************
  5474. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5475. *
  5476. * SUMMARY: Function pointer included in core_commands struct for external
  5477. * calls to the protected or unprotected call to
  5478. * pt_pip2_get_mode_sysmode
  5479. *
  5480. * RETURN:
  5481. * 0 = success
  5482. * !0 = failure
  5483. *
  5484. * PARAMETERS:
  5485. * *dev - pointer to device structure
  5486. * *mode - pointer to store the retrieved mode
  5487. * *sys_mode - pointer to store the FW system mode
  5488. ******************************************************************************/
  5489. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5490. int protect, u8 *mode, u8 *sys_mode)
  5491. {
  5492. struct pt_core_data *cd = dev_get_drvdata(dev);
  5493. if (protect)
  5494. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5495. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5496. }
  5497. /*******************************************************************************
  5498. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5499. *
  5500. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5501. * mode only the following PIP commands will be serviced, any other PIP
  5502. * command the FW will respond with an "Invalid PIP Command" response.
  5503. * - Get HID Descriptor (Register 0x0001, no command ID)
  5504. * - Reset (Register 0x0005, RESET HID request)
  5505. * - Ping (Register 0x0004, Command ID 0x00
  5506. * - Get System Information (Register 0x0004, Command ID 0x02)
  5507. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5508. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5509. * This function will loop on the results of the STATUS command until
  5510. * the FW reports it is out of BOOT mode.
  5511. *
  5512. * NOTE:
  5513. * - This function will update cd->fw_system_mode
  5514. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5515. *
  5516. * RETURN:
  5517. * 0 = success
  5518. * !0 = failure
  5519. *
  5520. * PARAMETERS:
  5521. * *cd - pointer to core data
  5522. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5523. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5524. ******************************************************************************/
  5525. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5526. int *actual_wait)
  5527. {
  5528. int loop = 0;
  5529. u8 sys_mode = cd->fw_system_mode;
  5530. u8 pause = 10; /* in ms */
  5531. int rc = 0;
  5532. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5533. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5534. /*
  5535. * For PIP <1.11, no support for polling wait so do a hard
  5536. * coded wait and assume the FW is out of BOOT. Added 1 to
  5537. * timeout to make it clear in kmsg if non polling was done.
  5538. */
  5539. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5540. pt_debug(cd->dev, DL_ERROR,
  5541. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5542. __func__,
  5543. cd->sysinfo.ttdata.pip_ver_major,
  5544. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5545. msleep(*actual_wait);
  5546. sys_mode = FW_SYS_MODE_SCANNING;
  5547. }
  5548. if (sys_mode == FW_SYS_MODE_BOOT) {
  5549. while (!rc && loop <= max_loop &&
  5550. (sys_mode == FW_SYS_MODE_BOOT)) {
  5551. loop++;
  5552. usleep_range(9000, pause * 1000);
  5553. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5554. pt_debug(cd->dev, DL_DEBUG,
  5555. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5556. __func__, loop * pause, sys_mode);
  5557. }
  5558. *actual_wait = (int)(loop * pause);
  5559. pt_debug(cd->dev, DL_WARN,
  5560. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5561. __func__, *actual_wait, sys_mode);
  5562. if (rc)
  5563. sys_mode = FW_SYS_MODE_UNDEFINED;
  5564. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5565. sys_mode == FW_SYS_MODE_UNDEFINED)
  5566. rc = -EBUSY;
  5567. }
  5568. mutex_lock(&cd->system_lock);
  5569. cd->fw_system_mode = sys_mode;
  5570. mutex_unlock(&cd->system_lock);
  5571. return rc;
  5572. }
  5573. /*******************************************************************************
  5574. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5575. *
  5576. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5577. * access to the DUT.
  5578. *
  5579. * RETURN:
  5580. * 0 = success
  5581. * !0 = failure
  5582. *
  5583. * PARAMETERS:
  5584. * *cd - pointer to core data
  5585. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5586. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5587. ******************************************************************************/
  5588. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5589. int *actual_wait)
  5590. {
  5591. int rc;
  5592. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5593. if (rc < 0) {
  5594. pt_debug(cd->dev, DL_ERROR,
  5595. "%s: fail get exclusive ex=%p own=%p\n",
  5596. __func__, cd->exclusive_dev, cd->dev);
  5597. return rc;
  5598. }
  5599. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5600. if (release_exclusive(cd, cd->dev) < 0)
  5601. pt_debug(cd->dev, DL_ERROR,
  5602. "%s: fail to release exclusive\n", __func__);
  5603. return rc;
  5604. }
  5605. /*******************************************************************************
  5606. * FUNCTION: _pt_get_fw_sys_mode
  5607. *
  5608. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5609. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5610. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5611. * information but any PIP command above 0x1F requires scanning to be disabled
  5612. * before it will be operational. If scanning was not disabled before sending
  5613. * these PIP commands the FW will respond with a 6 byte error response. So to
  5614. * safely determine the scanning state, a PIP message that will not affect the
  5615. * operation of the FW was chosen:
  5616. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5617. * received scanning is active.
  5618. *
  5619. * RETURN:
  5620. * 0 = success
  5621. * !0 = failure
  5622. *
  5623. * PARAMETERS:
  5624. * *cd - pointer to core data
  5625. * *sys_mode - pointer to FW System mode
  5626. * *mode - pointer to mode (BL/FW)
  5627. ******************************************************************************/
  5628. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5629. {
  5630. int write_length = 1;
  5631. int report_length;
  5632. int rc = 0;
  5633. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5634. u8 tmp_mode = PT_MODE_UNKNOWN;
  5635. u8 param[1] = { PT_TCH_PARM_EBID };
  5636. struct pt_hid_output hid_output = {
  5637. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5638. .write_length = write_length,
  5639. .write_buf = param,
  5640. .novalidate = true,
  5641. };
  5642. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5643. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5644. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5645. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5646. __func__, tmp_sys_mode, tmp_mode);
  5647. if (!rc) {
  5648. if (tmp_mode != PT_MODE_OPERATIONAL)
  5649. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5650. }
  5651. goto exit;
  5652. }
  5653. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5654. if (cd->mode != PT_MODE_OPERATIONAL) {
  5655. tmp_mode = cd->mode;
  5656. goto exit;
  5657. }
  5658. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5659. if (rc)
  5660. goto exit;
  5661. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5662. if ((report_length == 0x06) &&
  5663. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5664. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5665. tmp_mode = PIP2_STATUS_APP_EXEC;
  5666. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5667. } else if ((report_length == 0x0A) &&
  5668. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5669. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5670. tmp_mode = PIP2_STATUS_APP_EXEC;
  5671. tmp_sys_mode = FW_SYS_MODE_TEST;
  5672. }
  5673. exit:
  5674. if (mode)
  5675. *mode = tmp_mode;
  5676. if (sys_mode)
  5677. *sys_mode = tmp_sys_mode;
  5678. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5679. __func__, tmp_mode, tmp_sys_mode);
  5680. return rc;
  5681. }
  5682. /*******************************************************************************
  5683. * FUNCTION: pt_get_fw_sys_mode
  5684. *
  5685. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5686. * is active or not.
  5687. *
  5688. * RETURN:
  5689. * 0 = success
  5690. * !0 = failure
  5691. *
  5692. * PARAMETERS:
  5693. * *cd - pointer to core data
  5694. * *sys_mode - pointer to fw system mode
  5695. * *mode - pointer to mode
  5696. ******************************************************************************/
  5697. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5698. {
  5699. int rc;
  5700. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5701. if (rc < 0) {
  5702. pt_debug(cd->dev, DL_ERROR,
  5703. "%s: fail get exclusive ex=%p own=%p\n",
  5704. __func__, cd->exclusive_dev, cd->dev);
  5705. return rc;
  5706. }
  5707. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5708. if (release_exclusive(cd, cd->dev) < 0)
  5709. pt_debug(cd->dev, DL_ERROR,
  5710. "%s: fail to release exclusive\n", __func__);
  5711. return rc;
  5712. }
  5713. /*******************************************************************************
  5714. * FUNCTION: _pt_request_get_fw_sys_mode
  5715. *
  5716. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5717. * to request to get scan state
  5718. *
  5719. * RETURN:
  5720. * 0 = success
  5721. * !0 = failure
  5722. *
  5723. * PARAMETERS:
  5724. * *dev - pointer to device structure
  5725. * protect - flag to call protected or non-protected
  5726. * *sys_mode - pointer to FW system mode
  5727. * *mode - pointer to mode
  5728. ******************************************************************************/
  5729. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5730. u8 *sys_mode, u8 *mode)
  5731. {
  5732. struct pt_core_data *cd = dev_get_drvdata(dev);
  5733. if (protect)
  5734. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5735. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5736. }
  5737. /* Default hid descriptor to provide basic register map */
  5738. const struct pt_hid_desc hid_desc_default = {
  5739. 230, /* hid_desc_len */
  5740. HID_APP_REPORT_ID, /* packet_id */
  5741. 0x00, /* reserved_byte */
  5742. 0x0100, /* bcd_version */
  5743. 0x00EC, /* report_desc_len */
  5744. 0x0002, /* report_desc_register */
  5745. 0x0003, /* input_register */
  5746. 0x00FE, /* max_input_len */
  5747. 0x0004, /* output_register */
  5748. 0x00FE, /* max_output_len */
  5749. 0x0005, /* command_register */
  5750. 0x0006, /* data_register */
  5751. 0x04B4, /* vendor_id */
  5752. 0xC101, /* product_id */
  5753. 0x0100, /* version_id */
  5754. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5755. };
  5756. /*******************************************************************************
  5757. * FUNCTION: pt_init_hid_descriptor
  5758. *
  5759. * SUMMARY: Setup default values for HID descriptor structure
  5760. *
  5761. *
  5762. * PARAMETERS:
  5763. * *desc - pointer to the HID descriptor data read back from DUT
  5764. ******************************************************************************/
  5765. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5766. {
  5767. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5768. }
  5769. /*******************************************************************************
  5770. * FUNCTION: pt_get_hid_descriptor_
  5771. *
  5772. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5773. * into the HID descriptor structure
  5774. *
  5775. * RETURN:
  5776. * 0 = success
  5777. * !0 = failure
  5778. *
  5779. * PARAMETERS:
  5780. * *cd - pointer to core data
  5781. * *desc - pointer to the HID descriptor data read back from DUT
  5782. ******************************************************************************/
  5783. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5784. struct pt_hid_desc *desc)
  5785. {
  5786. struct device *dev = cd->dev;
  5787. int rc = 0;
  5788. int t;
  5789. u8 cmd[2];
  5790. /*
  5791. * During startup the HID descriptor is required for all future
  5792. * processing. If IRQ is already asserted due to an early touch report
  5793. * the report must be cleared before sending command.
  5794. */
  5795. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5796. /* Read HID descriptor length and version */
  5797. mutex_lock(&cd->system_lock);
  5798. cd->hid_cmd_state = 1;
  5799. mutex_unlock(&cd->system_lock);
  5800. /* Set HID descriptor register */
  5801. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5802. sizeof(cd->hid_core.hid_desc_register));
  5803. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5804. __func__, sizeof(cmd));
  5805. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5806. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5807. if (rc) {
  5808. pt_debug(dev, DL_ERROR,
  5809. "%s: failed to get HID descriptor, rc=%d\n",
  5810. __func__, rc);
  5811. goto error;
  5812. }
  5813. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5814. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5815. if (IS_TMO(t)) {
  5816. #ifdef TTDL_DIAGNOSTICS
  5817. cd->bus_transmit_error_count++;
  5818. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5819. #endif /* TTDL_DIAGNOSTICS */
  5820. pt_debug(cd->dev, DL_ERROR,
  5821. "%s: HID get descriptor timed out\n", __func__);
  5822. rc = -ETIME;
  5823. goto error;
  5824. } else {
  5825. cd->hw_detected = true;
  5826. }
  5827. /* Load the HID descriptor including all registers */
  5828. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5829. /* Check HID descriptor length and version */
  5830. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5831. le16_to_cpu(desc->hid_desc_len),
  5832. le16_to_cpu(desc->bcd_version));
  5833. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5834. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5835. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5836. __func__);
  5837. return -ENODEV;
  5838. }
  5839. goto exit;
  5840. error:
  5841. mutex_lock(&cd->system_lock);
  5842. cd->hid_cmd_state = 0;
  5843. mutex_unlock(&cd->system_lock);
  5844. exit:
  5845. return rc;
  5846. }
  5847. /*******************************************************************************
  5848. * FUNCTION: pt_get_hid_descriptor
  5849. *
  5850. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5851. *
  5852. * RETURN:
  5853. * 0 = success
  5854. * !0 = failure
  5855. *
  5856. * PARAMETERS:
  5857. * *cd - pointer to core data
  5858. * *desc - pointer to the HID descriptor data read back from DUT
  5859. ******************************************************************************/
  5860. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5861. struct pt_hid_desc *desc)
  5862. {
  5863. int rc;
  5864. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5865. if (rc < 0) {
  5866. pt_debug(cd->dev, DL_ERROR,
  5867. "%s: fail get exclusive ex=%p own=%p\n",
  5868. __func__, cd->exclusive_dev, cd->dev);
  5869. return rc;
  5870. }
  5871. rc = pt_get_hid_descriptor_(cd, desc);
  5872. if (release_exclusive(cd, cd->dev) < 0)
  5873. pt_debug(cd->dev, DL_ERROR,
  5874. "%s: fail to release exclusive\n", __func__);
  5875. return rc;
  5876. }
  5877. /*******************************************************************************
  5878. * FUNCTION: pt_pip2_get_version_
  5879. *
  5880. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5881. * cd-ttdata
  5882. *
  5883. * RETURN:
  5884. * 0 = success
  5885. * !0 = failure
  5886. *
  5887. * PARAMETERS:
  5888. * *cd - pointer to core data
  5889. ******************************************************************************/
  5890. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5891. {
  5892. int rc = 0;
  5893. int status;
  5894. u8 read_buf[64];
  5895. u16 actual_read_len;
  5896. rc = _pt_request_pip2_send_cmd(cd->dev,
  5897. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5898. NULL, 0, read_buf, &actual_read_len);
  5899. if (rc) {
  5900. pt_debug(cd->dev, DL_ERROR,
  5901. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5902. __func__, rc);
  5903. return rc;
  5904. }
  5905. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5906. if (status == 0) {
  5907. /* Parse the PIP2 VERSION response into ttdata */
  5908. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5909. } else {
  5910. pt_debug(cd->dev, DL_ERROR,
  5911. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5912. __func__, status);
  5913. return status;
  5914. }
  5915. return rc;
  5916. }
  5917. /*******************************************************************************
  5918. * FUNCTION: pt_pip2_get_version
  5919. *
  5920. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5921. * access to the DUT.
  5922. *
  5923. * RETURN:
  5924. * 0 = success
  5925. * !0 = failure
  5926. *
  5927. * PARAMETERS:
  5928. * *cd - pointer to core data structure
  5929. ******************************************************************************/
  5930. static int pt_pip2_get_version(struct pt_core_data *cd)
  5931. {
  5932. int rc;
  5933. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5934. if (rc < 0) {
  5935. pt_debug(cd->dev, DL_ERROR,
  5936. "%s: fail get exclusive ex=%p own=%p\n",
  5937. __func__, cd->exclusive_dev, cd->dev);
  5938. return rc;
  5939. }
  5940. rc = pt_pip2_get_version_(cd);
  5941. if (release_exclusive(cd, cd->dev) < 0)
  5942. pt_debug(cd->dev, DL_ERROR,
  5943. "%s: fail to release exclusive\n", __func__);
  5944. return rc;
  5945. }
  5946. /*******************************************************************************
  5947. * FUNCTION: _pt_request_active_pip_protocol
  5948. *
  5949. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5950. * Function will return PIP version of BL or application based on
  5951. * when it's called.
  5952. *
  5953. * RETURN:
  5954. * 0 = success
  5955. * !0 = failure
  5956. *
  5957. * PARAMETERS:
  5958. * *dev - pointer to device structure
  5959. * protect - flag to run in protected mode
  5960. * *pip_version_major - pointer to store PIP major version
  5961. * *pip_version_minor - pointer to store PIP minor version
  5962. ******************************************************************************/
  5963. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5964. u8 *pip_version_major, u8 *pip_version_minor)
  5965. {
  5966. struct pt_core_data *cd = dev_get_drvdata(dev);
  5967. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5968. int rc = 0;
  5969. struct pt_hid_output sys_info = {
  5970. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5971. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5972. };
  5973. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5974. /* Skip PIP2 command if DUT generation is confirmed */
  5975. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5976. goto skip_pip2_command;
  5977. rc = pt_pip2_get_version_(cd);
  5978. if (!rc) {
  5979. *pip_version_major = ttdata->pip_ver_major;
  5980. *pip_version_minor = ttdata->pip_ver_minor;
  5981. pt_debug(dev, DL_INFO,
  5982. "%s: pip_version = %d.%d\n", __func__,
  5983. *pip_version_major, *pip_version_minor);
  5984. } else {
  5985. /*
  5986. * Legacy products do not support the pip2 protocol to get
  5987. * pip version. However, they do support the "get sysinfo"
  5988. * command to get pip version from FW, but the bootloader
  5989. * does not support it. This function will try "get sysinfo"
  5990. * command if the pip2 command failed but this cmd could also
  5991. * fail if DUT is stuck in bootloader mode.
  5992. */
  5993. pt_debug(dev, DL_INFO,
  5994. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  5995. __func__, rc);
  5996. skip_pip2_command:
  5997. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  5998. if (!rc) {
  5999. *pip_version_minor =
  6000. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6001. *pip_version_major =
  6002. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6003. pt_debug(dev, DL_INFO,
  6004. "%s: pip_version = %d.%d\n", __func__,
  6005. *pip_version_major, *pip_version_minor);
  6006. } else {
  6007. *pip_version_major = 0;
  6008. *pip_version_minor = 0;
  6009. pt_debug(dev, DL_ERROR,
  6010. "%s: pip_version Not Detected\n", __func__);
  6011. }
  6012. }
  6013. return rc;
  6014. }
  6015. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6016. /*******************************************************************************
  6017. * FUNCTION: _pt_detect_dut_generation
  6018. *
  6019. * SUMMARY: Determine the generation of device that we are communicating with:
  6020. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6021. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6022. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6023. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6024. * coming form the BL or the FW:
  6025. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6026. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6027. * This function will return a modified status if it detects the DUT
  6028. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6029. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6030. *
  6031. * RETURN:
  6032. * 0 = success
  6033. * !0 = failure
  6034. *
  6035. * PARAMETERS:
  6036. * *dev - pointer to device structure
  6037. * *status - pointer to status bitmask
  6038. * *dut_gen - pointer to store the dut_generation
  6039. * *mode - pointer to store the PT_MODE
  6040. ******************************************************************************/
  6041. static int _pt_detect_dut_generation(struct device *dev,
  6042. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6043. {
  6044. int rc = 0;
  6045. u8 dut_gen_tmp = DUT_UNKNOWN;
  6046. u8 mode_tmp = PT_MODE_UNKNOWN;
  6047. u8 attempt = 1;
  6048. u32 status_tmp = STARTUP_STATUS_START;
  6049. struct pt_core_data *cd = dev_get_drvdata(dev);
  6050. struct pt_hid_desc hid_desc;
  6051. memset(&hid_desc, 0, sizeof(hid_desc));
  6052. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6053. while (rc && attempt < 3) {
  6054. attempt++;
  6055. usleep_range(2000, 5000);
  6056. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6057. }
  6058. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6059. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6060. mode_tmp = PT_MODE_BOOTLOADER;
  6061. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6062. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6063. rc = pt_pip2_get_version_(cd);
  6064. if (!rc)
  6065. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6066. else
  6067. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6068. mode_tmp = PT_MODE_OPERATIONAL;
  6069. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6070. rc = 0; /* To return success instead of error code */
  6071. } else if (rc) {
  6072. rc = pt_pip2_get_version_(cd);
  6073. if (!rc) {
  6074. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6075. mode_tmp = PT_MODE_BOOTLOADER;
  6076. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6077. }
  6078. }
  6079. mutex_lock(&cd->system_lock);
  6080. if (dut_gen)
  6081. *dut_gen = dut_gen_tmp;
  6082. if (mode)
  6083. *mode = mode_tmp;
  6084. if (status)
  6085. *status = status_tmp;
  6086. mutex_unlock(&cd->system_lock);
  6087. #ifdef TTDL_DIAGNOSTICS
  6088. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6089. __func__, dut_gen_tmp, mode_tmp);
  6090. #endif /* TTDL_DIAGNOSTICS */
  6091. return rc;
  6092. }
  6093. /*******************************************************************************
  6094. * FUNCTION: _pt_request_dut_generation
  6095. *
  6096. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6097. * to get current dut generation.
  6098. *
  6099. * NOTE: This function WILL NOT try to determine dut generation.
  6100. *
  6101. * RETURN:
  6102. * The current dut generation.
  6103. *
  6104. * PARAMETERS:
  6105. * *dev - pointer to device structure
  6106. ******************************************************************************/
  6107. static int _pt_request_dut_generation(struct device *dev)
  6108. {
  6109. struct pt_core_data *cd = dev_get_drvdata(dev);
  6110. return cd->active_dut_generation;
  6111. }
  6112. #define HW_VERSION_LEN_MAX 13
  6113. /*******************************************************************************
  6114. * FUNCTION: _legacy_generate_hw_version
  6115. *
  6116. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6117. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6118. * command) to the hw_version.
  6119. *
  6120. * RETURN:
  6121. * 0 = success
  6122. * !0 = failure
  6123. *
  6124. * PARAMETERS:
  6125. * *cd - pointer to core data
  6126. * *hw_version - pointer to store the hardware version
  6127. ******************************************************************************/
  6128. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6129. char *hw_version)
  6130. {
  6131. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6132. if (cd->sysinfo.ready) {
  6133. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6134. ttdata->jtag_id_h, cd->pid_for_loader);
  6135. return 0;
  6136. } else if (cd->bl_info.ready) {
  6137. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6138. cd->bl_info.chip_id, cd->pid_for_loader);
  6139. return 0;
  6140. } else {
  6141. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6142. pt_debug(cd->dev, DL_ERROR,
  6143. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6144. return -ENODATA;
  6145. }
  6146. }
  6147. /*******************************************************************************
  6148. * FUNCTION: _pip2_generate_hw_version
  6149. *
  6150. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6151. * VERSION command) to the hw_version.
  6152. *
  6153. * RETURN:
  6154. * 0 = success
  6155. * !0 = failure
  6156. *
  6157. * PARAMETERS:
  6158. * *cd - pointer to core data
  6159. * *hw_version - pointer to store the hardware version
  6160. ******************************************************************************/
  6161. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6162. {
  6163. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6164. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6165. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6166. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6167. return 0;
  6168. } else {
  6169. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6170. pt_debug(cd->dev, DL_ERROR,
  6171. "%s: PIP Version are not ready\n", __func__);
  6172. return -ENODATA;
  6173. }
  6174. }
  6175. /*******************************************************************************
  6176. * FUNCTION: pt_generate_hw_version
  6177. *
  6178. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6179. * hw_version from static data.
  6180. *
  6181. * RETURN:
  6182. * 0 = success
  6183. * !0 = failure
  6184. *
  6185. * PARAMETERS:
  6186. * *cd - pointer to core data
  6187. * *hw_version - pointer to store the hardware version
  6188. ******************************************************************************/
  6189. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6190. {
  6191. int rc = 0;
  6192. if (!hw_version)
  6193. return -ENOMEM;
  6194. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6195. rc = _legacy_generate_hw_version(cd, hw_version);
  6196. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6197. rc = _pip2_generate_hw_version(cd, hw_version);
  6198. else {
  6199. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6200. rc = -ENODATA;
  6201. }
  6202. return rc;
  6203. }
  6204. /*******************************************************************************
  6205. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6206. *
  6207. * NOTE: The calling function must ensure to free *hw_version
  6208. *
  6209. * RETURN:
  6210. * 0 = success
  6211. * !0 = Failure
  6212. *
  6213. * PARAMETERS:
  6214. * *dev - pointer to device structure
  6215. * *hw_version - pointer to where the hw_version string will be stored
  6216. ******************************************************************************/
  6217. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6218. {
  6219. int rc = 0;
  6220. u16 actual_read_len;
  6221. u16 pip_ver;
  6222. u8 rd_buf[256];
  6223. u8 status;
  6224. u8 index = PIP2_RESP_STATUS_OFFSET;
  6225. u8 return_data[8];
  6226. u8 panel_id;
  6227. struct pt_core_data *cd = dev_get_drvdata(dev);
  6228. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6229. if (!hw_version)
  6230. return -ENOMEM;
  6231. if (!cd->hw_detected) {
  6232. /* No HW detected */
  6233. rc = -ENODEV;
  6234. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6235. __func__);
  6236. goto exit_error;
  6237. }
  6238. /* For Parade TC or TT parts */
  6239. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6240. rc = _pt_request_pip2_send_cmd(dev,
  6241. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6242. NULL, 0, rd_buf, &actual_read_len);
  6243. if (rc) {
  6244. pt_debug(dev, DL_ERROR,
  6245. "%s: Failed to send PIP2 VERSION cmd\n",
  6246. __func__);
  6247. goto exit_error;
  6248. }
  6249. status = rd_buf[index];
  6250. if (status == 0) {
  6251. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6252. /*
  6253. * BL PIP 2.02 and greater the version fields are
  6254. * swapped
  6255. */
  6256. if (pip_ver >= 0x0202) {
  6257. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6258. "%02X%02X.%02X%02X.FF",
  6259. rd_buf[index + 10], rd_buf[index + 9],
  6260. rd_buf[index + 8], rd_buf[index + 7]);
  6261. } else {
  6262. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6263. "%02X%02X.%02X%02X.FF",
  6264. rd_buf[index + 8], rd_buf[index + 7],
  6265. rd_buf[index + 10], rd_buf[index + 9]);
  6266. }
  6267. return STATUS_SUCCESS;
  6268. } else {
  6269. rc = status;
  6270. pt_debug(dev, DL_WARN,
  6271. "%s: PIP2 VERSION cmd response error\n",
  6272. __func__);
  6273. }
  6274. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6275. /*
  6276. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6277. * hard coded to FFFF
  6278. */
  6279. if (cd->mode == PT_MODE_OPERATIONAL) {
  6280. rc = pt_hid_output_get_sysinfo(cd);
  6281. if (!rc) {
  6282. panel_id =
  6283. cd->sysinfo.sensing_conf_data.panel_id;
  6284. } else {
  6285. panel_id = PANEL_ID_NOT_ENABLED;
  6286. }
  6287. /* In FW - simply retrieve from ttdata struct */
  6288. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6289. "%04X.FFFF.%02X",
  6290. ttdata->jtag_id_h,
  6291. panel_id);
  6292. return STATUS_SUCCESS;
  6293. } else {
  6294. /*
  6295. * Return the stored value if PT_PANEL_ID_BY_BL
  6296. * is not supported while other feature is.
  6297. */
  6298. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6299. rc = pt_hid_output_bl_get_information(
  6300. cd, return_data);
  6301. if (!rc) {
  6302. rc = pt_hid_output_bl_get_panel_id(
  6303. cd, &panel_id);
  6304. }
  6305. } else
  6306. panel_id = cd->pid_for_loader;
  6307. if (!rc) {
  6308. snprintf(hw_version,
  6309. HW_VERSION_LEN_MAX,
  6310. "%02X%02X.FFFF.%02X",
  6311. return_data[3], return_data[2],
  6312. panel_id);
  6313. return STATUS_SUCCESS;
  6314. }
  6315. }
  6316. } else {
  6317. /* Unknown generation */
  6318. rc = -ENODEV;
  6319. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6320. __func__);
  6321. }
  6322. exit_error:
  6323. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6324. return rc;
  6325. }
  6326. /*******************************************************************************
  6327. * FUNCTION: pt_start_wd_timer
  6328. *
  6329. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6330. *
  6331. * RETURN: void
  6332. *
  6333. * PARAMETERS:
  6334. * *cd - pointer to core data
  6335. ******************************************************************************/
  6336. static void pt_start_wd_timer(struct pt_core_data *cd)
  6337. {
  6338. if (cd->watchdog_interval < 100) {
  6339. pt_debug(cd->dev, DL_ERROR,
  6340. "%s: WARNING: Invalid watchdog interval: %d\n",
  6341. __func__, cd->watchdog_interval);
  6342. return;
  6343. }
  6344. if (cd->watchdog_force_stop) {
  6345. pt_debug(cd->dev, DL_INFO,
  6346. "%s: TTDL WD Forced stop\n", __func__);
  6347. return;
  6348. }
  6349. mod_timer(&cd->watchdog_timer, jiffies +
  6350. msecs_to_jiffies(cd->watchdog_interval));
  6351. cd->watchdog_enabled = 1;
  6352. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6353. }
  6354. /*******************************************************************************
  6355. * FUNCTION: pt_stop_wd_timer
  6356. *
  6357. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6358. *
  6359. * RETURN: void
  6360. *
  6361. * PARAMETERS:
  6362. * *cd - pointer to core data
  6363. ******************************************************************************/
  6364. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6365. {
  6366. if (!cd->watchdog_interval)
  6367. return;
  6368. /*
  6369. * Ensure we wait until the watchdog timer
  6370. * running on a different CPU finishes
  6371. */
  6372. del_timer_sync(&cd->watchdog_timer);
  6373. cancel_work_sync(&cd->watchdog_work);
  6374. del_timer_sync(&cd->watchdog_timer);
  6375. cd->watchdog_enabled = 0;
  6376. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6377. }
  6378. /*******************************************************************************
  6379. * FUNCTION: pt_hw_soft_reset
  6380. *
  6381. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6382. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6383. * schedule an enum if it fires when the DUT is being reset.
  6384. * This can cause a double reset.
  6385. *
  6386. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6387. * the WD active could cause this function to fail!
  6388. *
  6389. * RETURN:
  6390. * 0 = success
  6391. * !0 = failure
  6392. *
  6393. * PARAMETERS:
  6394. * *cd - pointer to core data struct
  6395. * protect - flag to call protected or non-protected
  6396. ******************************************************************************/
  6397. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6398. {
  6399. int rc = 0;
  6400. mutex_lock(&cd->system_lock);
  6401. cd->startup_status = STARTUP_STATUS_START;
  6402. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6403. mutex_unlock(&cd->system_lock);
  6404. if (protect)
  6405. rc = pt_hid_cmd_reset(cd);
  6406. else
  6407. rc = pt_hid_cmd_reset_(cd);
  6408. if (rc < 0) {
  6409. pt_debug(cd->dev, DL_ERROR,
  6410. "%s: FAILED to execute SOFT reset\n", __func__);
  6411. return rc;
  6412. }
  6413. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6414. __func__);
  6415. return 0;
  6416. }
  6417. /*******************************************************************************
  6418. * FUNCTION: pt_hw_hard_reset
  6419. *
  6420. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6421. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6422. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6423. * schedule an enum if it fires when the DUT is being reset.
  6424. * This can cause a double reset.
  6425. *
  6426. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6427. * the WD active could cause this function to fail!
  6428. *
  6429. * RETURN:
  6430. * 0 = success
  6431. * !0 = failure
  6432. *
  6433. * PARAMETERS:
  6434. * *cd - pointer to core data struct
  6435. ******************************************************************************/
  6436. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6437. {
  6438. if (cd->cpdata->xres) {
  6439. cd->startup_status = STARTUP_STATUS_START;
  6440. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6441. __func__);
  6442. cd->cpdata->xres(cd->cpdata, cd->dev);
  6443. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6444. __func__);
  6445. return 0;
  6446. }
  6447. pt_debug(cd->dev, DL_ERROR,
  6448. "%s: FAILED to execute HARD reset\n", __func__);
  6449. return -ENODEV;
  6450. }
  6451. /*******************************************************************************
  6452. * FUNCTION: pt_dut_reset
  6453. *
  6454. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6455. * soft reset.
  6456. *
  6457. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6458. * the WD active could cause this function to fail!
  6459. * NOTE: "protect" flag is only used for soft reset.
  6460. *
  6461. * RETURN:
  6462. * 0 = success
  6463. * !0 = failure
  6464. *
  6465. * PARAMETERS:
  6466. * *cd - pointer to core data structure
  6467. * protect - flag to call protected or non-protected
  6468. ******************************************************************************/
  6469. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6470. {
  6471. int rc = 0;
  6472. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6473. mutex_lock(&cd->system_lock);
  6474. cd->hid_reset_cmd_state = 1;
  6475. rc = pt_hw_hard_reset(cd);
  6476. mutex_unlock(&cd->system_lock);
  6477. if (rc == -ENODEV) {
  6478. mutex_lock(&cd->system_lock);
  6479. cd->hid_reset_cmd_state = 0;
  6480. mutex_unlock(&cd->system_lock);
  6481. pt_debug(cd->dev, DL_ERROR,
  6482. "%s: Hard reset failed, try soft reset\n", __func__);
  6483. rc = pt_hw_soft_reset(cd, protect);
  6484. }
  6485. if (rc)
  6486. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6487. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6488. return rc;
  6489. }
  6490. /*******************************************************************************
  6491. * FUNCTION: pt_dut_reset_and_wait
  6492. *
  6493. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6494. * complete
  6495. *
  6496. * RETURN:
  6497. * 0 = success
  6498. * !0 = failure
  6499. *
  6500. * PARAMETERS:
  6501. * *cd - pointer to core data structure
  6502. ******************************************************************************/
  6503. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6504. {
  6505. int rc = 0;
  6506. int t;
  6507. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6508. if (rc < 0)
  6509. goto exit;
  6510. t = wait_event_timeout(cd->wait_q,
  6511. (cd->hid_reset_cmd_state == 0),
  6512. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6513. if (IS_TMO(t)) {
  6514. #ifdef TTDL_DIAGNOSTICS
  6515. cd->bus_transmit_error_count++;
  6516. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6517. #endif /* TTDL_DIAGNOSTICS */
  6518. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6519. __func__);
  6520. rc = -ETIME;
  6521. goto exit;
  6522. }
  6523. exit:
  6524. return rc;
  6525. }
  6526. /*
  6527. * touch default parameters (from report descriptor) to resolve protocol for
  6528. * touch report
  6529. */
  6530. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6531. /* byte offset, size, min, max, bit offset, report */
  6532. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6533. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6534. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6535. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6536. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6537. };
  6538. /*
  6539. * button default parameters (from report descriptor) to resolve protocol for
  6540. * button report
  6541. */
  6542. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6543. /* byte offset, size, min, max, bit offset, report */
  6544. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6545. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6546. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6547. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6548. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6549. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6550. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6551. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6552. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6553. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6554. };
  6555. /*******************************************************************************
  6556. * FUNCTION: pt_init_pip_report_fields
  6557. *
  6558. * SUMMARY: Setup default values for touch/button report parsing.
  6559. *
  6560. * PARAMETERS:
  6561. * *cd - pointer to core data structure
  6562. ******************************************************************************/
  6563. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6564. {
  6565. struct pt_sysinfo *si = &cd->sysinfo;
  6566. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6567. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6568. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6569. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6570. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6571. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6572. cd->features.easywake = 1;
  6573. cd->features.noise_metric = 1;
  6574. cd->features.tracking_heatmap = 1;
  6575. cd->features.sensor_data = 1;
  6576. }
  6577. /*******************************************************************************
  6578. * FUNCTION: pt_get_mode
  6579. *
  6580. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6581. * message
  6582. *
  6583. * RETURN: Enum of the current mode
  6584. *
  6585. * PARAMETERS:
  6586. * *cd - pointer to the Core Data structure
  6587. * protect - run command in protected mode
  6588. * *mode - pointer to store the retrieved mode
  6589. ******************************************************************************/
  6590. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6591. {
  6592. if (desc->packet_id == HID_APP_REPORT_ID)
  6593. return PT_MODE_OPERATIONAL;
  6594. else if (desc->packet_id == HID_BL_REPORT_ID)
  6595. return PT_MODE_BOOTLOADER;
  6596. return PT_MODE_UNKNOWN;
  6597. }
  6598. /*******************************************************************************
  6599. * FUNCTION: _pt_request_get_mode
  6600. *
  6601. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6602. * to determine the current mode of the DUT by use of the Get HID
  6603. * Descriptor command.
  6604. *
  6605. * RETURN:
  6606. * 0 = success
  6607. * !0 = failure
  6608. *
  6609. * PARAMETERS:
  6610. * *dev - pointer to device structure
  6611. * protect - run command in protected mode
  6612. * *mode - pointer to store the retrieved mode
  6613. ******************************************************************************/
  6614. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6615. {
  6616. struct pt_core_data *cd = dev_get_drvdata(dev);
  6617. struct pt_hid_desc hid_desc;
  6618. int rc = 0;
  6619. memset(&hid_desc, 0, sizeof(hid_desc));
  6620. if (protect)
  6621. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6622. else
  6623. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6624. if (rc)
  6625. *mode = PT_MODE_UNKNOWN;
  6626. else
  6627. *mode = pt_get_mode(cd, &hid_desc);
  6628. return rc;
  6629. }
  6630. /*******************************************************************************
  6631. * FUNCTION: pt_queue_enum_
  6632. *
  6633. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6634. * function. It won't try to add/delete sysfs node or modules.
  6635. *
  6636. * RETURN: void
  6637. *
  6638. * PARAMETERS:
  6639. * *cd - pointer to core data
  6640. ******************************************************************************/
  6641. static void pt_queue_enum_(struct pt_core_data *cd)
  6642. {
  6643. if (cd->startup_state == STARTUP_NONE) {
  6644. cd->startup_state = STARTUP_QUEUED;
  6645. #ifdef TTDL_DIAGNOSTICS
  6646. if (!cd->bridge_mode)
  6647. schedule_work(&cd->enum_work);
  6648. else
  6649. cd->startup_state = STARTUP_NONE;
  6650. #else
  6651. schedule_work(&cd->enum_work);
  6652. #endif
  6653. pt_debug(cd->dev, DL_INFO,
  6654. "%s: enum_work queued\n", __func__);
  6655. } else {
  6656. pt_debug(cd->dev, DL_WARN,
  6657. "%s: Enum not queued - startup_state = %d\n",
  6658. __func__, cd->startup_state);
  6659. }
  6660. }
  6661. /*******************************************************************************
  6662. * FUNCTION: pt_queue_enum
  6663. *
  6664. * SUMMARY: Queues a TTDL enum within a mutex lock
  6665. *
  6666. * RETURN: void
  6667. *
  6668. * PARAMETERS:
  6669. * *cd - pointer to core data
  6670. ******************************************************************************/
  6671. static void pt_queue_enum(struct pt_core_data *cd)
  6672. {
  6673. mutex_lock(&cd->system_lock);
  6674. pt_queue_enum_(cd);
  6675. mutex_unlock(&cd->system_lock);
  6676. }
  6677. static void remove_sysfs_and_modules(struct device *dev);
  6678. /*******************************************************************************
  6679. * FUNCTION: pt_queue_restart
  6680. *
  6681. * SUMMARY: Queues a TTDL restart within a mutex lock
  6682. *
  6683. * RETURN: void
  6684. *
  6685. * PARAMETERS:
  6686. * *cd - pointer to core data
  6687. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6688. * False: will not perform remove action
  6689. ******************************************************************************/
  6690. static void pt_queue_restart(struct pt_core_data *cd)
  6691. {
  6692. mutex_lock(&cd->system_lock);
  6693. if (cd->startup_state == STARTUP_NONE) {
  6694. cd->startup_state = STARTUP_QUEUED;
  6695. schedule_work(&cd->ttdl_restart_work);
  6696. pt_debug(cd->dev, DL_INFO,
  6697. "%s: pt_ttdl_restart queued\n", __func__);
  6698. } else {
  6699. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6700. __func__, cd->startup_state);
  6701. }
  6702. mutex_unlock(&cd->system_lock);
  6703. }
  6704. /*******************************************************************************
  6705. * FUNCTION: call_atten_cb
  6706. *
  6707. * SUMMARY: Iterate over attention list call the function that registered.
  6708. *
  6709. * RETURN: void
  6710. *
  6711. * PARAMETERS:
  6712. * *cd - pointer to core data
  6713. * type - type of attention list
  6714. * mode - condition for execution
  6715. ******************************************************************************/
  6716. static void call_atten_cb(struct pt_core_data *cd,
  6717. enum pt_atten_type type, int mode)
  6718. {
  6719. struct atten_node *atten, *atten_n;
  6720. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6721. __func__, type, mode);
  6722. spin_lock(&cd->spinlock);
  6723. list_for_each_entry_safe(atten, atten_n,
  6724. &cd->atten_list[type], node) {
  6725. if (!mode || atten->mode & mode) {
  6726. spin_unlock(&cd->spinlock);
  6727. pt_debug(cd->dev, DL_DEBUG,
  6728. "%s: attention for '%s'",
  6729. __func__, dev_name(atten->dev));
  6730. atten->func(atten->dev);
  6731. spin_lock(&cd->spinlock);
  6732. }
  6733. }
  6734. spin_unlock(&cd->spinlock);
  6735. }
  6736. /*******************************************************************************
  6737. * FUNCTION: start_fw_upgrade
  6738. *
  6739. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6740. * start firmware upgrade.
  6741. *
  6742. * RETURN:
  6743. * 0 = success
  6744. *
  6745. * PARAMETERS:
  6746. * *data - pointer to core data
  6747. ******************************************************************************/
  6748. static int start_fw_upgrade(void *data)
  6749. {
  6750. struct pt_core_data *cd = (struct pt_core_data *)data;
  6751. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6752. return 0;
  6753. }
  6754. /*******************************************************************************
  6755. * FUNCTION: pt_put_device_into_easy_wakeup_
  6756. *
  6757. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6758. * wake up state.
  6759. *
  6760. * RETURN:
  6761. * 0 = success
  6762. * !0 = failure
  6763. *
  6764. * PARAMETERS:
  6765. * *cd - pointer to core data
  6766. ******************************************************************************/
  6767. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6768. {
  6769. int rc = 0;
  6770. u8 status = 0;
  6771. mutex_lock(&cd->system_lock);
  6772. cd->wait_until_wake = 0;
  6773. mutex_unlock(&cd->system_lock);
  6774. rc = pt_hid_output_enter_easywake_state_(cd,
  6775. cd->easy_wakeup_gesture, &status);
  6776. if (rc || status == 0)
  6777. return -EBUSY;
  6778. return rc;
  6779. }
  6780. /*******************************************************************************
  6781. * FUNCTION: pt_put_device_into_deep_sleep_
  6782. *
  6783. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6784. *
  6785. * RETURN:
  6786. * 0 = success
  6787. * !0 = error
  6788. *
  6789. * PARAMETERS:
  6790. * *cd - pointer to core data
  6791. ******************************************************************************/
  6792. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6793. {
  6794. int rc = 0;
  6795. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6796. if (rc)
  6797. rc = -EBUSY;
  6798. return rc;
  6799. }
  6800. /*******************************************************************************
  6801. * FUNCTION: pt_put_device_into_deep_standby_
  6802. *
  6803. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6804. *
  6805. * RETURN:
  6806. * 0 = success
  6807. * !0 = error
  6808. *
  6809. * PARAMETERS:
  6810. * *cd - pointer to core data
  6811. ******************************************************************************/
  6812. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6813. {
  6814. int rc = 0;
  6815. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6816. if (rc)
  6817. rc = -EBUSY;
  6818. return rc;
  6819. }
  6820. /*******************************************************************************
  6821. * FUNCTION: pt_core_poweroff_device_
  6822. *
  6823. * SUMMARY: Disable IRQ and HW power down the device.
  6824. *
  6825. * RETURN:
  6826. * 0 = success
  6827. * !0 = failure
  6828. *
  6829. * PARAMETERS:
  6830. * *cd - pointer to core data
  6831. ******************************************************************************/
  6832. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6833. {
  6834. int rc;
  6835. if (cd->irq_enabled) {
  6836. cd->irq_enabled = false;
  6837. disable_irq_nosync(cd->irq);
  6838. }
  6839. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6840. if (rc < 0)
  6841. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6842. __func__, rc);
  6843. return rc;
  6844. }
  6845. /*******************************************************************************
  6846. * FUNCTION: pt_core_sleep_
  6847. *
  6848. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6849. * configuration in the core platform data structure.
  6850. *
  6851. * RETURN:
  6852. * 0 = success
  6853. * !0 = failure
  6854. *
  6855. * PARAMETERS:
  6856. * *cd - pointer to core data
  6857. ******************************************************************************/
  6858. static int pt_core_sleep_(struct pt_core_data *cd)
  6859. {
  6860. int rc = 0;
  6861. mutex_lock(&cd->system_lock);
  6862. if (cd->sleep_state == SS_SLEEP_OFF) {
  6863. cd->sleep_state = SS_SLEEPING;
  6864. } else {
  6865. mutex_unlock(&cd->system_lock);
  6866. return 1;
  6867. }
  6868. mutex_unlock(&cd->system_lock);
  6869. /* Ensure watchdog and startup works stopped */
  6870. pt_stop_wd_timer(cd);
  6871. cancel_work_sync(&cd->enum_work);
  6872. pt_stop_wd_timer(cd);
  6873. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture) && cd->runtime)
  6874. rc = pt_put_device_into_easy_wakeup_(cd);
  6875. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  6876. rc = pt_core_poweroff_device_(cd);
  6877. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  6878. rc = pt_put_device_into_deep_standby_(cd);
  6879. else
  6880. rc = pt_put_device_into_deep_sleep_(cd);
  6881. mutex_lock(&cd->system_lock);
  6882. cd->sleep_state = SS_SLEEP_ON;
  6883. mutex_unlock(&cd->system_lock);
  6884. return rc;
  6885. }
  6886. /*******************************************************************************
  6887. * FUNCTION: pt_core_sleep
  6888. *
  6889. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6890. *
  6891. * RETURN:
  6892. * 0 = success
  6893. * !0 = failure
  6894. *
  6895. * PARAMETERS:
  6896. * *cd - pointer to core data
  6897. ******************************************************************************/
  6898. static int pt_core_sleep(struct pt_core_data *cd)
  6899. {
  6900. int rc = 0;
  6901. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6902. if (rc < 0) {
  6903. pt_debug(cd->dev, DL_ERROR,
  6904. "%s: fail get exclusive ex=%p own=%p\n",
  6905. __func__, cd->exclusive_dev, cd->dev);
  6906. return rc;
  6907. }
  6908. rc = pt_core_sleep_(cd);
  6909. if (release_exclusive(cd, cd->dev) < 0)
  6910. pt_debug(cd->dev, DL_ERROR,
  6911. "%s: fail to release exclusive\n", __func__);
  6912. else
  6913. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6914. __func__);
  6915. return rc;
  6916. }
  6917. /*******************************************************************************
  6918. * FUNCTION: pt_wakeup_host
  6919. *
  6920. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  6921. *
  6922. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  6923. *
  6924. * RETURN:
  6925. * 0 = success
  6926. * !0 = failure
  6927. *
  6928. * PARAMETERS:
  6929. * *cd - pointer to core data
  6930. ******************************************************************************/
  6931. static int pt_wakeup_host(struct pt_core_data *cd)
  6932. {
  6933. #ifndef EASYWAKE_TSG6
  6934. /* TSG5 EasyWake */
  6935. int rc = 0;
  6936. int event_id;
  6937. int size = get_unaligned_le16(&cd->input_buf[0]);
  6938. /* Validate report */
  6939. if (size != 4 || cd->input_buf[2] != 4)
  6940. rc = -EINVAL;
  6941. event_id = cd->input_buf[3];
  6942. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  6943. __func__, event_id, rc);
  6944. if (rc) {
  6945. pt_core_sleep_(cd);
  6946. goto exit;
  6947. }
  6948. /* attention WAKE */
  6949. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6950. exit:
  6951. return rc;
  6952. #else
  6953. /* TSG6 FW1.3 EasyWake */
  6954. int rc = 0;
  6955. int i = 0;
  6956. int report_length;
  6957. /* Validate report */
  6958. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  6959. rc = -EINVAL;
  6960. pt_core_sleep_(cd);
  6961. goto exit;
  6962. }
  6963. /* Get gesture id and gesture data length */
  6964. cd->gesture_id = cd->input_buf[3];
  6965. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  6966. cd->gesture_data_length = report_length - 4;
  6967. pt_debug(cd->dev, DL_INFO,
  6968. "%s: gesture_id = %d, gesture_data_length = %d\n",
  6969. __func__, cd->gesture_id, cd->gesture_data_length);
  6970. for (i = 0; i < cd->gesture_data_length; i++)
  6971. cd->gesture_data[i] = cd->input_buf[4 + i];
  6972. /* attention WAKE */
  6973. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6974. exit:
  6975. return rc;
  6976. #endif
  6977. }
  6978. /*******************************************************************************
  6979. * FUNCTION: pt_get_touch_axis
  6980. *
  6981. * SUMMARY: Function to calculate touch axis
  6982. *
  6983. * PARAMETERS:
  6984. * *cd - pointer to core data structure
  6985. * *axis - pointer to axis calculation result
  6986. * size - size in bytes
  6987. * max - max value of result
  6988. * *xy_data - pointer to input data to be parsed
  6989. * bofs - bit offset
  6990. ******************************************************************************/
  6991. static void pt_get_touch_axis(struct pt_core_data *cd,
  6992. int *axis, int size, int max, u8 *data, int bofs)
  6993. {
  6994. int nbyte;
  6995. int next;
  6996. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  6997. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  6998. next++;
  6999. }
  7000. *axis &= max - 1;
  7001. }
  7002. /*******************************************************************************
  7003. * FUNCTION: move_tracking_heatmap_data
  7004. *
  7005. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7006. * system information structure, xy_mode and xy_data.
  7007. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7008. * the tthe_tuner sysfs node under the label "THM"
  7009. *
  7010. * RETURN:
  7011. * 0 = success
  7012. * !0 = failure
  7013. *
  7014. * PARAMETERS:
  7015. * *cd - pointer to core data
  7016. * *si - pointer to the system information structure
  7017. ******************************************************************************/
  7018. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7019. struct pt_sysinfo *si)
  7020. {
  7021. #ifdef TTHE_TUNER_SUPPORT
  7022. int size = get_unaligned_le16(&cd->input_buf[0]);
  7023. if (size)
  7024. tthe_print(cd, cd->input_buf, size, "THM=");
  7025. #endif
  7026. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7027. return 0;
  7028. }
  7029. /*******************************************************************************
  7030. * FUNCTION: move_sensor_data
  7031. *
  7032. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7033. * information structure, xy_mode and xy_data.
  7034. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7035. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7036. *
  7037. * RETURN:
  7038. * 0 = success
  7039. * !0 = failure
  7040. *
  7041. * PARAMETERS:
  7042. * *cd - pointer to core data
  7043. * *si - pointer to the system information structure
  7044. ******************************************************************************/
  7045. static int move_sensor_data(struct pt_core_data *cd,
  7046. struct pt_sysinfo *si)
  7047. {
  7048. #ifdef TTHE_TUNER_SUPPORT
  7049. int size = get_unaligned_le16(&cd->input_buf[0]);
  7050. if (size)
  7051. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7052. #endif
  7053. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7054. return 0;
  7055. }
  7056. /*******************************************************************************
  7057. * FUNCTION: move_button_data
  7058. *
  7059. * SUMMARY: Move the valid button data from the input buffer into the system
  7060. * information structure, xy_mode and xy_data.
  7061. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7062. * the tthe_tuner sysfs node under the label "OpModeData"
  7063. *
  7064. * RETURN:
  7065. * 0 = success
  7066. * !0 = failure
  7067. *
  7068. * PARAMETERS:
  7069. * *cd - pointer to core data
  7070. * *si - pointer to the system information structure
  7071. ******************************************************************************/
  7072. static int move_button_data(struct pt_core_data *cd,
  7073. struct pt_sysinfo *si)
  7074. {
  7075. #ifdef TTHE_TUNER_SUPPORT
  7076. int size = get_unaligned_le16(&cd->input_buf[0]);
  7077. if (size)
  7078. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7079. #endif
  7080. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7081. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7082. "xy_mode");
  7083. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7084. BTN_REPORT_SIZE);
  7085. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7086. "xy_data");
  7087. return 0;
  7088. }
  7089. /*******************************************************************************
  7090. * FUNCTION: move_touch_data
  7091. *
  7092. * SUMMARY: Move the valid touch data from the input buffer into the system
  7093. * information structure, xy_mode and xy_data.
  7094. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7095. * the tthe_tuner sysfs node under the label "OpModeData"
  7096. *
  7097. * RETURN:
  7098. * 0 = success
  7099. * !0 = failure
  7100. *
  7101. * PARAMETERS:
  7102. * *cd - pointer to core data
  7103. * *si - pointer to the system information structure
  7104. ******************************************************************************/
  7105. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7106. {
  7107. int max_tch = si->sensing_conf_data.max_tch;
  7108. int num_cur_tch;
  7109. int length;
  7110. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7111. #ifdef TTHE_TUNER_SUPPORT
  7112. int size = get_unaligned_le16(&cd->input_buf[0]);
  7113. if (size)
  7114. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7115. #endif
  7116. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7117. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7118. si->desc.tch_header_size, "xy_mode");
  7119. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7120. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7121. if (unlikely(num_cur_tch > max_tch))
  7122. num_cur_tch = max_tch;
  7123. length = num_cur_tch * si->desc.tch_record_size;
  7124. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7125. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7126. return 0;
  7127. }
  7128. /*******************************************************************************
  7129. * FUNCTION: move_hid_pen_data
  7130. *
  7131. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7132. * information structure, xy_mode and xy_data.
  7133. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7134. * the tthe_tuner sysfs node under the label "HID" starting with the
  7135. * report ID.
  7136. *
  7137. * RETURN:
  7138. * 0 = success
  7139. * !0 = failure
  7140. *
  7141. * PARAMETERS:
  7142. * *cd - pointer to core data
  7143. * *si - pointer to the system information structure
  7144. ******************************************************************************/
  7145. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7146. {
  7147. #ifdef TTHE_TUNER_SUPPORT
  7148. int size = get_unaligned_le16(&cd->input_buf[0]);
  7149. if (size) {
  7150. /*
  7151. * HID over USB does not require the two byte length field, so
  7152. * this should print from input_buf[2] but to keep both finger
  7153. * and pen reports the same the length is included
  7154. */
  7155. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7156. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7157. "HID-USB=");
  7158. else
  7159. tthe_print(cd, &(cd->input_buf[0]), size,
  7160. "HID-I2C=");
  7161. }
  7162. #endif
  7163. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7164. return 0;
  7165. }
  7166. /*******************************************************************************
  7167. * FUNCTION: parse_touch_input
  7168. *
  7169. * SUMMARY: Parse the touch report and take action based on the touch
  7170. * report_id.
  7171. *
  7172. * RETURN:
  7173. * 0 = success
  7174. * !0 = failure
  7175. *
  7176. * PARAMETERS:
  7177. * *cd - pointer to core data
  7178. * size - size of touch record
  7179. ******************************************************************************/
  7180. static int parse_touch_input(struct pt_core_data *cd, int size)
  7181. {
  7182. struct pt_sysinfo *si = &cd->sysinfo;
  7183. int report_id = cd->input_buf[2];
  7184. int rc = -EINVAL;
  7185. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7186. __func__);
  7187. if (!si->ready) {
  7188. pt_debug(cd->dev, DL_ERROR,
  7189. "%s: Need system information to parse touches\n",
  7190. __func__);
  7191. return 0;
  7192. }
  7193. if (!si->xy_mode || !si->xy_data)
  7194. return rc;
  7195. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7196. rc = move_touch_data(cd, si);
  7197. else if (report_id == PT_HID_PEN_REPORT_ID)
  7198. rc = move_hid_pen_data(cd, si);
  7199. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7200. rc = move_button_data(cd, si);
  7201. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7202. rc = move_sensor_data(cd, si);
  7203. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7204. rc = move_tracking_heatmap_data(cd, si);
  7205. if (rc)
  7206. return rc;
  7207. /* attention IRQ */
  7208. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7209. return 0;
  7210. }
  7211. /*******************************************************************************
  7212. * FUNCTION: parse_command_input
  7213. *
  7214. * SUMMARY: Move the response data from the input buffer to the response buffer
  7215. *
  7216. * RETURN:
  7217. * 0 = success
  7218. * !0 = failure
  7219. *
  7220. * PARAMETERS:
  7221. * *cd - pointer to core data
  7222. * size - size of response data
  7223. ******************************************************************************/
  7224. static int parse_command_input(struct pt_core_data *cd, int size)
  7225. {
  7226. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7227. __func__);
  7228. memcpy(cd->response_buf, cd->input_buf, size);
  7229. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7230. if (size && cd->show_tt_data) {
  7231. if (cd->pip2_prot_active)
  7232. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7233. else
  7234. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7235. }
  7236. #endif
  7237. mutex_lock(&cd->system_lock);
  7238. cd->hid_cmd_state = 0;
  7239. mutex_unlock(&cd->system_lock);
  7240. wake_up(&cd->wait_q);
  7241. return 0;
  7242. }
  7243. /*******************************************************************************
  7244. * FUNCTION: pt_allow_enumeration
  7245. *
  7246. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7247. * FW sentinel is seen.
  7248. *
  7249. * RETURN:
  7250. * true = allow enumeration or fully re-probe
  7251. * false = skip enumeration and fully re-probe
  7252. *
  7253. * PARAMETERS:
  7254. * *cd - pointer to core data
  7255. ******************************************************************************/
  7256. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7257. {
  7258. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7259. (!cd->hid_reset_cmd_state) &&
  7260. (cd->core_probe_complete) &&
  7261. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7262. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7263. (cd->mode == PT_MODE_OPERATIONAL)) {
  7264. return true;
  7265. }
  7266. if ((!cd->hid_reset_cmd_state) &&
  7267. (cd->core_probe_complete) &&
  7268. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7269. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7270. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7271. return true;
  7272. }
  7273. pt_debug(cd->dev, DL_INFO,
  7274. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7275. __func__,
  7276. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7277. "core_probe_complete(1)", cd->core_probe_complete,
  7278. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7279. "active_dut_gen(0,2)", cd->active_dut_generation);
  7280. return false;
  7281. }
  7282. /*******************************************************************************
  7283. * FUNCTION: pt_is_touch_report
  7284. *
  7285. * SUMMARY: Determine if a report ID should be treated as a touch report
  7286. *
  7287. * RETURN:
  7288. * true = report ID is a touch report
  7289. * false = report ID is not a touch report
  7290. *
  7291. * PARAMETERS:
  7292. * id - Report ID
  7293. ******************************************************************************/
  7294. static bool pt_is_touch_report(int id)
  7295. {
  7296. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7297. id == PT_HID_PEN_REPORT_ID ||
  7298. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7299. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7300. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7301. return true;
  7302. else
  7303. return false;
  7304. }
  7305. /*******************************************************************************
  7306. * FUNCTION: pt_parse_input
  7307. *
  7308. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7309. * on if its a response to a command or asynchronous touch data / reset
  7310. * sentinel. PIP2.x messages have additional error checking that is
  7311. * parsed (SEQ match from cmd to rsp, CRC valid).
  7312. * Look for special packets based on unique lengths:
  7313. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7314. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7315. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7316. * 0xFFXX - Empty buffer (PIP 1.7+)
  7317. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7318. * the initial probe has not yet been done.
  7319. *
  7320. * RETURN:
  7321. * 0 = success
  7322. * !0 = failure
  7323. *
  7324. * PARAMETERS:
  7325. * *cd - pointer to core data
  7326. ******************************************************************************/
  7327. static int pt_parse_input(struct pt_core_data *cd)
  7328. {
  7329. int report_id;
  7330. int cmd_id;
  7331. int is_command = 0;
  7332. int size;
  7333. int print_size;
  7334. bool touch_report = true;
  7335. unsigned short calc_crc;
  7336. unsigned short resp_crc;
  7337. cd->fw_sys_mode_in_standby_state = false;
  7338. size = get_unaligned_le16(&cd->input_buf[0]);
  7339. print_size = size;
  7340. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7341. __func__, print_size);
  7342. if (print_size <= PT_MAX_INPUT)
  7343. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7344. "<<< Read buf");
  7345. if (size == 0 ||
  7346. (size == 11 &&
  7347. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7348. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7349. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7350. PIP2_CMD_COMMAND_ID_MASK) ==
  7351. PIP2_CMD_ID_STATUS)) {
  7352. touch_report = false;
  7353. cd->hw_detected = true;
  7354. cd->bl_pip_ver_ready = false;
  7355. cd->app_pip_ver_ready = false;
  7356. if (size == 0) {
  7357. mutex_lock(&cd->system_lock);
  7358. cd->pip2_prot_active = false;
  7359. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7360. /*
  7361. * For Gen5/6 this sentinel could be from
  7362. * the BL or FW. Attempt to set the correct
  7363. * mode based on the previous PIP command.
  7364. */
  7365. if (cd->hid_cmd_state ==
  7366. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7367. cd->mode = PT_MODE_OPERATIONAL;
  7368. cd->startup_status =
  7369. STARTUP_STATUS_FW_RESET_SENTINEL;
  7370. } else if (cd->hid_cmd_state ==
  7371. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7372. cd->hid_reset_cmd_state) {
  7373. cd->mode = PT_MODE_BOOTLOADER;
  7374. cd->startup_status =
  7375. STARTUP_STATUS_BL_RESET_SENTINEL;
  7376. } else {
  7377. cd->mode = PT_MODE_UNKNOWN;
  7378. cd->startup_status =
  7379. STARTUP_STATUS_START;
  7380. }
  7381. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7382. pt_debug(cd->dev, DL_INFO,
  7383. "%s: ATM Gen5/6 %s sentinel received\n",
  7384. __func__,
  7385. (cd->mode == PT_MODE_OPERATIONAL ?
  7386. "FW" :
  7387. (cd->mode == PT_MODE_BOOTLOADER ?
  7388. "BL" : "Unknown")));
  7389. } else {
  7390. cd->mode = PT_MODE_OPERATIONAL;
  7391. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7392. cd->startup_status =
  7393. STARTUP_STATUS_FW_RESET_SENTINEL;
  7394. pt_debug(cd->dev, DL_INFO,
  7395. "%s: ATM PT/TT FW sentinel received\n",
  7396. __func__);
  7397. }
  7398. mutex_unlock(&cd->system_lock);
  7399. if (pt_allow_enumeration(cd)) {
  7400. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7401. pt_debug(cd->dev, DL_INFO,
  7402. "%s: Queue Restart\n", __func__);
  7403. pt_queue_restart(cd);
  7404. } else {
  7405. pt_debug(cd->dev, DL_INFO,
  7406. "%s: Queue Enum\n", __func__);
  7407. pt_queue_enum(cd);
  7408. }
  7409. } else {
  7410. pt_debug(cd->dev, DL_INFO,
  7411. "%s: Sentinel - No Queued Action\n",
  7412. __func__);
  7413. }
  7414. } else {
  7415. /* Sentinel must be from TT/TC BL */
  7416. mutex_lock(&cd->system_lock);
  7417. cd->pip2_prot_active = true;
  7418. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7419. cd->mode = PT_MODE_BOOTLOADER;
  7420. cd->sysinfo.ready = false;
  7421. mutex_unlock(&cd->system_lock);
  7422. pt_debug(cd->dev, DL_INFO,
  7423. "%s: BL Reset sentinel received\n", __func__);
  7424. if (cd->flashless_dut &&
  7425. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7426. pt_debug(cd->dev, DL_INFO,
  7427. "%s: BL to RAM for flashless DUT\n",
  7428. __func__);
  7429. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7430. }
  7431. }
  7432. mutex_lock(&cd->system_lock);
  7433. memcpy(cd->response_buf, cd->input_buf, 2);
  7434. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7435. mutex_unlock(&cd->system_lock);
  7436. pt_debug(cd->dev, DL_WARN,
  7437. "%s: Device Initiated Reset\n", __func__);
  7438. wake_up(&cd->wait_q);
  7439. return 0;
  7440. }
  7441. cd->hid_reset_cmd_state = 0;
  7442. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7443. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7444. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7445. cd->hid_cmd_state = 0;
  7446. wake_up(&cd->wait_q);
  7447. mutex_unlock(&cd->system_lock);
  7448. return 0;
  7449. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7450. /*
  7451. * This debug message below is used by PBATS to calculate the
  7452. * time from the last lift off IRQ to when FW exits LFT mode.
  7453. */
  7454. touch_report = false;
  7455. pt_debug(cd->dev, DL_WARN,
  7456. "%s: DUT - Empty buffer detected\n", __func__);
  7457. return 0;
  7458. } else if (size > PT_MAX_INPUT) {
  7459. pt_debug(cd->dev, DL_ERROR,
  7460. "%s: DUT - Unexpected len field in active bus data!\n",
  7461. __func__);
  7462. return -EINVAL;
  7463. }
  7464. if (cd->pip2_prot_active) {
  7465. pt_debug(cd->dev, DL_DEBUG,
  7466. "%s: Decode PIP2.x Response\n", __func__);
  7467. /* PIP2 does not have a report id, hard code it */
  7468. report_id = 0x00;
  7469. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7470. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7471. resp_crc = cd->input_buf[size - 2] << 8;
  7472. resp_crc |= cd->input_buf[size - 1];
  7473. if ((cd->pip2_cmd_tag_seq !=
  7474. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7475. (resp_crc != calc_crc) &&
  7476. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7477. == PT_PIP_TOUCH_REPORT_ID) ||
  7478. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7479. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7480. pt_debug(cd->dev, DL_WARN,
  7481. "%s: %s %d %s\n",
  7482. __func__,
  7483. "Received PIP1 report id =",
  7484. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7485. "when expecting a PIP2 report - IGNORE report");
  7486. return 0;
  7487. }
  7488. is_command = 1;
  7489. touch_report = false;
  7490. } else {
  7491. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7492. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7493. }
  7494. #ifdef TTDL_DIAGNOSTICS
  7495. pt_debug(cd->dev, DL_DEBUG,
  7496. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7497. __func__, cd->pip2_prot_active, report_id,
  7498. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7499. #endif /* TTDL_DIAGNOSTICS */
  7500. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7501. pt_wakeup_host(cd);
  7502. #ifdef TTHE_TUNER_SUPPORT
  7503. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7504. #endif
  7505. return 0;
  7506. }
  7507. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7508. msecs_to_jiffies(cd->watchdog_interval));
  7509. /*
  7510. * If it is PIP2 response, the report_id has been set to 0,
  7511. * so it will not be parsed as a touch packet.
  7512. */
  7513. if (!pt_is_touch_report(report_id)) {
  7514. is_command = 1;
  7515. touch_report = false;
  7516. }
  7517. if (unlikely(is_command)) {
  7518. parse_command_input(cd, size);
  7519. return 0;
  7520. }
  7521. if (touch_report)
  7522. parse_touch_input(cd, size);
  7523. return 0;
  7524. }
  7525. /*******************************************************************************
  7526. * FUNCTION: pt_read_input
  7527. *
  7528. * SUMMARY: Reads incoming data off of the active bus
  7529. *
  7530. * RETURN:
  7531. * 0 = success
  7532. * !0 = failure
  7533. *
  7534. * PARAMETERS:
  7535. * *cd - pointer to core data
  7536. ******************************************************************************/
  7537. static int pt_read_input(struct pt_core_data *cd)
  7538. {
  7539. struct device *dev = cd->dev;
  7540. int rc = 0;
  7541. int t;
  7542. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7543. /*
  7544. * Workaround for easywake failure
  7545. * Interrupt for easywake, wait for bus controller to wake
  7546. */
  7547. mutex_lock(&cd->system_lock);
  7548. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture) && cd->runtime) {
  7549. if (cd->sleep_state == SS_SLEEP_ON) {
  7550. mutex_unlock(&cd->system_lock);
  7551. if (!dev->power.is_suspended)
  7552. goto read;
  7553. t = wait_event_timeout(cd->wait_q,
  7554. (cd->wait_until_wake == 1),
  7555. msecs_to_jiffies(2000));
  7556. #ifdef TTDL_DIAGNOSTICS
  7557. if (IS_TMO(t)) {
  7558. cd->bus_transmit_error_count++;
  7559. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7560. pt_debug(dev, DL_ERROR,
  7561. "%s: !!!I2C Transmission Error %d\n",
  7562. __func__,
  7563. cd->bus_transmit_error_count);
  7564. }
  7565. #else
  7566. if (IS_TMO(t))
  7567. pt_queue_enum(cd);
  7568. #endif /* TTDL_DIAGNOSTICS */
  7569. goto read;
  7570. }
  7571. }
  7572. mutex_unlock(&cd->system_lock);
  7573. read:
  7574. /* Try reading up to 'retry' times */
  7575. while (retry-- != 0) {
  7576. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7577. PT_MAX_INPUT);
  7578. if (!rc) {
  7579. pt_debug(dev, DL_DEBUG,
  7580. "%s: Read input successfully\n", __func__);
  7581. goto read_exit;
  7582. }
  7583. usleep_range(1000, 2000);
  7584. }
  7585. pt_debug(dev, DL_ERROR,
  7586. "%s: Error getting report, rc=%d\n", __func__, rc);
  7587. read_exit:
  7588. return rc;
  7589. }
  7590. /*******************************************************************************
  7591. * FUNCTION: pt_irq
  7592. *
  7593. * SUMMARY: Process all detected interrupts
  7594. *
  7595. * RETURN:
  7596. * IRQ_HANDLED - Finished processing the interrupt
  7597. *
  7598. * PARAMETERS:
  7599. * irq - IRQ number
  7600. * *handle - pointer to core data struct
  7601. ******************************************************************************/
  7602. irqreturn_t pt_irq(int irq, void *handle)
  7603. {
  7604. struct pt_core_data *cd = handle;
  7605. int rc = 0;
  7606. if (!pt_check_irq_asserted(cd))
  7607. return IRQ_HANDLED;
  7608. rc = pt_read_input(cd);
  7609. #ifdef TTDL_DIAGNOSTICS
  7610. cd->irq_count++;
  7611. /* Used to calculate T-Refresh */
  7612. if (cd->t_refresh_active) {
  7613. if (cd->t_refresh_count == 0) {
  7614. cd->t_refresh_time = jiffies;
  7615. cd->t_refresh_count++;
  7616. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7617. cd->t_refresh_count++;
  7618. } else {
  7619. cd->t_refresh_active = 0;
  7620. cd->t_refresh_time =
  7621. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7622. }
  7623. }
  7624. #endif /* TTDL_DIAGNOSTICS */
  7625. if (!rc)
  7626. pt_parse_input(cd);
  7627. return IRQ_HANDLED;
  7628. }
  7629. /*******************************************************************************
  7630. * FUNCTION: _pt_subscribe_attention
  7631. *
  7632. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7633. * to subscribe themselves into the TTDL attention list
  7634. *
  7635. * RETURN:
  7636. * 0 = success
  7637. * !0 = failure
  7638. *
  7639. * PARAMETERS:
  7640. * *dev - pointer to device structure
  7641. * type - attention type enum
  7642. * *id - ID of the module calling this function
  7643. * *func - callback function pointer to be called when notified
  7644. * mode - attention mode
  7645. ******************************************************************************/
  7646. int _pt_subscribe_attention(struct device *dev,
  7647. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7648. int mode)
  7649. {
  7650. struct pt_core_data *cd = dev_get_drvdata(dev);
  7651. struct atten_node *atten, *atten_new;
  7652. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7653. if (!atten_new)
  7654. return -ENOMEM;
  7655. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7656. dev_name(cd->dev));
  7657. spin_lock(&cd->spinlock);
  7658. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7659. if (atten->id == id && atten->mode == mode) {
  7660. spin_unlock(&cd->spinlock);
  7661. kfree(atten_new);
  7662. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7663. __func__,
  7664. "already subscribed attention",
  7665. dev, "mode", mode);
  7666. return 0;
  7667. }
  7668. }
  7669. atten_new->id = id;
  7670. atten_new->dev = dev;
  7671. atten_new->mode = mode;
  7672. atten_new->func = func;
  7673. list_add(&atten_new->node, &cd->atten_list[type]);
  7674. spin_unlock(&cd->spinlock);
  7675. return 0;
  7676. }
  7677. /*******************************************************************************
  7678. * FUNCTION: _pt_unsubscribe_attention
  7679. *
  7680. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7681. * to unsubscribe themselves from the TTDL attention list
  7682. *
  7683. * RETURN:
  7684. * 0 = success
  7685. * !0 = failure
  7686. *
  7687. * PARAMETERS:
  7688. * *dev - pointer to device structure
  7689. * type - attention type enum
  7690. * *id - ID of the module calling this function
  7691. * *func - function pointer
  7692. * mode - attention mode
  7693. ******************************************************************************/
  7694. int _pt_unsubscribe_attention(struct device *dev,
  7695. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7696. int mode)
  7697. {
  7698. struct pt_core_data *cd = dev_get_drvdata(dev);
  7699. struct atten_node *atten, *atten_n;
  7700. spin_lock(&cd->spinlock);
  7701. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7702. if (atten->id == id && atten->mode == mode) {
  7703. list_del(&atten->node);
  7704. spin_unlock(&cd->spinlock);
  7705. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7706. __func__,
  7707. "unsub for atten->dev", atten->dev,
  7708. "atten->mode", atten->mode);
  7709. kfree(atten);
  7710. return 0;
  7711. }
  7712. }
  7713. spin_unlock(&cd->spinlock);
  7714. return -ENODEV;
  7715. }
  7716. /*******************************************************************************
  7717. * FUNCTION: _pt_request_exclusive
  7718. *
  7719. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7720. * to request exclusive access
  7721. *
  7722. * RETURN:
  7723. * 0 = success
  7724. * !0 = failure
  7725. *
  7726. * PARAMETERS:
  7727. * *dev - pointer to device structure
  7728. * timeout_ms - timeout to wait for exclusive access
  7729. ******************************************************************************/
  7730. static int _pt_request_exclusive(struct device *dev,
  7731. int timeout_ms)
  7732. {
  7733. struct pt_core_data *cd = dev_get_drvdata(dev);
  7734. return request_exclusive(cd, (void *)dev, timeout_ms);
  7735. }
  7736. /*******************************************************************************
  7737. * FUNCTION: _pt_release_exclusive
  7738. *
  7739. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7740. * to release exclusive access
  7741. *
  7742. * RETURN:
  7743. * 0 = success
  7744. * !0 = failure
  7745. *
  7746. * PARAMETERS:
  7747. * *dev - pointer to device structure
  7748. ******************************************************************************/
  7749. static int _pt_release_exclusive(struct device *dev)
  7750. {
  7751. struct pt_core_data *cd = dev_get_drvdata(dev);
  7752. return release_exclusive(cd, (void *)dev);
  7753. }
  7754. /*******************************************************************************
  7755. * FUNCTION: _pt_request_reset
  7756. *
  7757. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7758. * to request the DUT to be reset. Function returns err if refused or
  7759. * timeout occurs (Note: core uses fixed timeout period).
  7760. *
  7761. * NOTE: Function blocks until ISR occurs.
  7762. *
  7763. * RETURN:
  7764. * 0 = success
  7765. * !0 = failure
  7766. *
  7767. * PARAMETERS:
  7768. * *dev - pointer to device structure
  7769. * protect - flag to call protected or non-protected
  7770. ******************************************************************************/
  7771. static int _pt_request_reset(struct device *dev, int protect)
  7772. {
  7773. struct pt_core_data *cd = dev_get_drvdata(dev);
  7774. int rc;
  7775. rc = pt_dut_reset(cd, protect);
  7776. if (rc < 0) {
  7777. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7778. __func__, rc);
  7779. }
  7780. return rc;
  7781. }
  7782. /*******************************************************************************
  7783. * FUNCTION: _pt_request_enum
  7784. *
  7785. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7786. * to request TTDL to queue an enum. This function will return err
  7787. * if refused; if no error then enum has completed and system is in
  7788. * normal operation mode.
  7789. *
  7790. * RETURN:
  7791. * 0 = success
  7792. * !0 = failure
  7793. *
  7794. * PARAMETERS:
  7795. * *dev - pointer to device structure
  7796. * wait - boolean to determine if to wait for startup event
  7797. ******************************************************************************/
  7798. static int _pt_request_enum(struct device *dev, bool wait)
  7799. {
  7800. struct pt_core_data *cd = dev_get_drvdata(dev);
  7801. pt_queue_enum(cd);
  7802. if (wait)
  7803. wait_event_timeout(cd->wait_q,
  7804. cd->startup_state == STARTUP_NONE,
  7805. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7806. return 0;
  7807. }
  7808. /*******************************************************************************
  7809. * FUNCTION: _pt_request_sysinfo
  7810. *
  7811. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7812. * to request the pointer to the system information structure. This
  7813. * function will return NULL if sysinfo has not been acquired from the
  7814. * DUT yet.
  7815. *
  7816. * RETURN: Pointer to the system information struct
  7817. *
  7818. * PARAMETERS:
  7819. * *dev - pointer to device structure
  7820. ******************************************************************************/
  7821. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7822. {
  7823. struct pt_core_data *cd = dev_get_drvdata(dev);
  7824. int rc = 0;
  7825. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7826. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7827. !cd->sysinfo.ready) {
  7828. rc = pt_hid_output_get_sysinfo_(cd);
  7829. if (rc) {
  7830. pt_debug(cd->dev, DL_ERROR,
  7831. "%s: Error getting sysinfo rc=%d\n",
  7832. __func__, rc);
  7833. }
  7834. }
  7835. if (cd->sysinfo.ready)
  7836. return &cd->sysinfo;
  7837. return NULL;
  7838. }
  7839. /*******************************************************************************
  7840. * FUNCTION: _pt_request_loader_pdata
  7841. *
  7842. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7843. * to request the pointer to the loader platform data
  7844. *
  7845. * RETURN: Pointer to the loader platform data struct
  7846. *
  7847. * PARAMETERS:
  7848. * *dev - pointer to device structure
  7849. ******************************************************************************/
  7850. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7851. struct device *dev)
  7852. {
  7853. struct pt_core_data *cd = dev_get_drvdata(dev);
  7854. return cd->pdata->loader_pdata;
  7855. }
  7856. /*******************************************************************************
  7857. * FUNCTION: _pt_request_start_wd
  7858. *
  7859. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7860. * to request to start the TTDL watchdog
  7861. *
  7862. * RETURN:
  7863. * 0 = success
  7864. * !0 = failure
  7865. *
  7866. * PARAMETERS:
  7867. * *dev - pointer to device structure
  7868. ******************************************************************************/
  7869. static int _pt_request_start_wd(struct device *dev)
  7870. {
  7871. struct pt_core_data *cd = dev_get_drvdata(dev);
  7872. pt_start_wd_timer(cd);
  7873. return 0;
  7874. }
  7875. /*******************************************************************************
  7876. * FUNCTION: _pt_request_stop_wd
  7877. *
  7878. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7879. * to request to stop the TTDL watchdog
  7880. *
  7881. * RETURN:
  7882. * 0 = success
  7883. * !0 = failure
  7884. *
  7885. * PARAMETERS:
  7886. * *dev - pointer to device structure
  7887. ******************************************************************************/
  7888. static int _pt_request_stop_wd(struct device *dev)
  7889. {
  7890. struct pt_core_data *cd = dev_get_drvdata(dev);
  7891. pt_stop_wd_timer(cd);
  7892. return 0;
  7893. }
  7894. /*******************************************************************************
  7895. * FUNCTION: pt_pip2_launch_app
  7896. *
  7897. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7898. * the FW reset sentinel to indicate the function succeeded.
  7899. *
  7900. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7901. * in a timeout delay and with the DUT being reset with an XRES.
  7902. *
  7903. * RETURN:
  7904. * 0 = success
  7905. * !0 = failure
  7906. *
  7907. * PARAMETERS:
  7908. * *cd - pointer to core data structure
  7909. * protect - flag to call protected or non-protected
  7910. ******************************************************************************/
  7911. static int pt_pip2_launch_app(struct device *dev, int protect)
  7912. {
  7913. struct pt_core_data *cd = dev_get_drvdata(dev);
  7914. u16 actual_read_len;
  7915. u16 tmp_startup_status = cd->startup_status;
  7916. u8 read_buf[12];
  7917. u8 status;
  7918. int time = 0;
  7919. int rc = 0;
  7920. mutex_lock(&cd->system_lock);
  7921. cd->startup_status = STARTUP_STATUS_START;
  7922. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  7923. mutex_unlock(&cd->system_lock);
  7924. rc = _pt_request_pip2_send_cmd(dev, protect,
  7925. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  7926. &actual_read_len);
  7927. if (rc) {
  7928. pt_debug(dev, DL_ERROR,
  7929. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  7930. __func__, rc);
  7931. } else {
  7932. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  7933. /* Test for no or invalid image in FLASH, no point to reset */
  7934. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  7935. rc = status;
  7936. goto exit;
  7937. }
  7938. /* Any other boot failure */
  7939. if (status != 0) {
  7940. pt_debug(dev, DL_ERROR,
  7941. "%s: FW did not EXECUTE, status = %d\n",
  7942. __func__, status);
  7943. rc = status;
  7944. }
  7945. }
  7946. if (rc) {
  7947. pt_debug(dev, DL_ERROR,
  7948. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  7949. __func__, rc);
  7950. goto exit;
  7951. }
  7952. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  7953. msleep(20);
  7954. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  7955. __func__, time, cd->startup_status);
  7956. time += 20;
  7957. }
  7958. if (cd->startup_status == STARTUP_STATUS_START) {
  7959. pt_debug(cd->dev, DL_WARN,
  7960. "%s: TMO waiting for FW reset sentinel\n", __func__);
  7961. rc = -ETIME;
  7962. }
  7963. exit:
  7964. if (cd->startup_status == STARTUP_STATUS_START) {
  7965. /* Reset to original state because we could be stuck in BL */
  7966. mutex_lock(&cd->system_lock);
  7967. cd->startup_status = tmp_startup_status;
  7968. mutex_unlock(&cd->system_lock);
  7969. }
  7970. return rc;
  7971. }
  7972. /*******************************************************************************
  7973. * FUNCTION: _pt_request_pip2_launch_app
  7974. *
  7975. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  7976. * inserted to ensure the reset has allowed the BL reset sentinel to be
  7977. * consumed.
  7978. *
  7979. * RETURN:
  7980. * 0 = success
  7981. * !0 = failure
  7982. *
  7983. * PARAMETERS:
  7984. * *cd - pointer to core data structure
  7985. ******************************************************************************/
  7986. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  7987. {
  7988. return pt_pip2_launch_app(dev, protect);
  7989. }
  7990. /*******************************************************************************
  7991. * FUNCTION: _pt_request_wait_for_enum_state
  7992. *
  7993. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  7994. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  7995. *
  7996. * RETURN:
  7997. * 0 = success
  7998. * !0 = failure
  7999. *
  8000. * PARAMETERS:
  8001. * *cd - pointer to core data structure
  8002. * timeout - timeout for how long to wait
  8003. * state - enum state to wait for
  8004. ******************************************************************************/
  8005. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8006. int state)
  8007. {
  8008. int rc = 0;
  8009. int t;
  8010. struct pt_core_data *cd = dev_get_drvdata(dev);
  8011. t = wait_event_timeout(cd->wait_q,
  8012. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8013. msecs_to_jiffies(timeout));
  8014. if (IS_TMO(t)) {
  8015. pt_debug(cd->dev, DL_ERROR,
  8016. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8017. __func__, state, timeout);
  8018. pt_debug(cd->dev, DL_WARN,
  8019. "%s: enum state reached 0x%04X\n",
  8020. __func__, cd->startup_status);
  8021. rc = -ETIME;
  8022. } else if (cd->startup_status & state) {
  8023. pt_debug(cd->dev, DL_INFO,
  8024. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8025. __func__, cd->startup_status,
  8026. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8027. } else {
  8028. if (t == 1) {
  8029. pt_debug(
  8030. cd->dev, DL_ERROR,
  8031. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8032. __func__, state, timeout);
  8033. rc = -ETIME;
  8034. } else {
  8035. pt_debug(
  8036. cd->dev, DL_ERROR,
  8037. "%s: Enum state 0x%04X not reached in %dms\n",
  8038. __func__, state, timeout - jiffies_to_msecs(t));
  8039. rc = -EINVAL;
  8040. }
  8041. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8042. __func__, cd->startup_status);
  8043. }
  8044. return rc;
  8045. }
  8046. /*******************************************************************************
  8047. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8048. *
  8049. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8050. * deep sleep.
  8051. *
  8052. * RETURN:
  8053. * 0 = success
  8054. * !0 = error
  8055. *
  8056. * PARAMETERS:
  8057. * *cd - pointer to core data
  8058. ******************************************************************************/
  8059. static int pt_core_wake_device_from_deep_sleep_(
  8060. struct pt_core_data *cd)
  8061. {
  8062. int rc;
  8063. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8064. if (rc)
  8065. rc = -EAGAIN;
  8066. /* Prevent failure on sequential wake/sleep requests from OS */
  8067. msleep(20);
  8068. return rc;
  8069. }
  8070. /*******************************************************************************
  8071. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8072. *
  8073. * SUMMARY: Wake up device from Easy-Wake state.
  8074. *
  8075. * RETURN:
  8076. * 0 = success
  8077. * !0 = failure
  8078. *
  8079. * PARAMETERS:
  8080. * *cd - pointer to core data
  8081. ******************************************************************************/
  8082. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8083. {
  8084. mutex_lock(&cd->system_lock);
  8085. cd->wait_until_wake = 1;
  8086. mutex_unlock(&cd->system_lock);
  8087. wake_up(&cd->wait_q);
  8088. msleep(20);
  8089. return pt_core_wake_device_from_deep_sleep_(cd);
  8090. }
  8091. /*******************************************************************************
  8092. * FUNCTION: pt_restore_parameters_
  8093. *
  8094. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8095. * back to the DUT
  8096. *
  8097. * RETURN:
  8098. * 0 = success
  8099. * !0 = failure
  8100. *
  8101. * PARAMETERS:
  8102. * *cd - pointer the core data structure
  8103. ******************************************************************************/
  8104. static int pt_restore_parameters_(struct pt_core_data *cd)
  8105. {
  8106. struct param_node *param;
  8107. int rc = 0;
  8108. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8109. goto exit;
  8110. spin_lock(&cd->spinlock);
  8111. list_for_each_entry(param, &cd->param_list, node) {
  8112. spin_unlock(&cd->spinlock);
  8113. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8114. __func__, param->id, param->value);
  8115. rc = pt_pip_set_param_(cd, param->id,
  8116. param->value, param->size);
  8117. if (rc)
  8118. goto exit;
  8119. spin_lock(&cd->spinlock);
  8120. }
  8121. spin_unlock(&cd->spinlock);
  8122. exit:
  8123. return rc;
  8124. }
  8125. /*******************************************************************************
  8126. * FUNCTION: pt_pip2_exit_bl_
  8127. *
  8128. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8129. * a DUT that may not have flash and will need FW to be loaded into RAM
  8130. *
  8131. * RETURN:
  8132. * 0 = success
  8133. * !0 = failure
  8134. *
  8135. * PARAMETERS:
  8136. * *cd - pointer the core data structure
  8137. * *status_str - pointer to optional status string buffer
  8138. * buf_size - size of status_str buffer
  8139. ******************************************************************************/
  8140. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8141. {
  8142. int rc;
  8143. int wait_time = 0;
  8144. u8 mode = PT_MODE_UNKNOWN;
  8145. bool load_status_str = false;
  8146. /*
  8147. * Below function has protective call to ensure no enum is still on
  8148. * going, while this kind of protection should be applied widely in
  8149. * future (TODO).
  8150. */
  8151. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8152. if (status_str && buf_size <= 50)
  8153. load_status_str = true;
  8154. if (mode == PT_MODE_BOOTLOADER) {
  8155. if (cd->flashless_dut == 1) {
  8156. rc = pt_hw_hard_reset(cd);
  8157. } else {
  8158. rc = pt_pip2_launch_app(cd->dev,
  8159. PT_CORE_CMD_UNPROTECTED);
  8160. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8161. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8162. __func__, "Invalid image in FLASH rc", rc);
  8163. } else if (rc) {
  8164. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8165. __func__, "Failed to launch app rc", rc);
  8166. }
  8167. }
  8168. if (!rc) {
  8169. if (cd->flashless_dut == 1) {
  8170. /* Wait for BL to complete before enum */
  8171. rc = _pt_request_wait_for_enum_state(cd->dev,
  8172. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8173. if (rc && load_status_str) {
  8174. strlcpy(status_str, "No FW sentinel after BL",
  8175. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8176. goto exit;
  8177. }
  8178. }
  8179. /*
  8180. * If the host wants to interact with the FW or do a
  8181. * forced calibration, the FW must be out of BOOT mode
  8182. * and the system information must have been retrieved.
  8183. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8184. * If, however, the enumeration does not reach this
  8185. * point, the DUT may still be in APP mode so test
  8186. * for all conditions.
  8187. */
  8188. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8189. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8190. if (!rc || cd->startup_status >=
  8191. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8192. mutex_lock(&cd->system_lock);
  8193. cd->mode = PT_MODE_OPERATIONAL;
  8194. mutex_unlock(&cd->system_lock);
  8195. }
  8196. if (rc) {
  8197. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8198. __func__, "Failed to enum with DUT",
  8199. cd->startup_status);
  8200. if (load_status_str && !(cd->startup_status &
  8201. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8202. strlcpy(status_str, "FW Stuck in Boot mode",
  8203. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8204. goto exit;
  8205. }
  8206. }
  8207. /*
  8208. * The coming FW sentinel could wake up the event
  8209. * queue, which has chance to be taken by next command
  8210. * wrongly. Following delay is a workaround to avoid
  8211. * this issue for most situations.
  8212. */
  8213. msleep(20);
  8214. pt_start_wd_timer(cd);
  8215. }
  8216. if (load_status_str) {
  8217. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8218. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8219. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8220. else if (!rc)
  8221. strlcpy(status_str, "Entered APP from BL mode",
  8222. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8223. else
  8224. strlcpy(status_str, "Failed to enter APP from BL mode",
  8225. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8226. }
  8227. } else if (mode == PT_MODE_OPERATIONAL) {
  8228. mutex_lock(&cd->system_lock);
  8229. cd->mode = mode;
  8230. mutex_unlock(&cd->system_lock);
  8231. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8232. if (load_status_str) {
  8233. if (!rc)
  8234. strlcpy(status_str, "Already in APP mode",
  8235. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8236. else
  8237. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8238. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8239. }
  8240. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8241. mutex_lock(&cd->system_lock);
  8242. cd->mode = mode;
  8243. mutex_unlock(&cd->system_lock);
  8244. if (load_status_str)
  8245. strlcpy(status_str, "Failed to determine active mode",
  8246. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8247. }
  8248. exit:
  8249. if (!rc)
  8250. pt_start_wd_timer(cd);
  8251. return rc;
  8252. }
  8253. /*******************************************************************************
  8254. * FUNCTION: pt_pip2_exit_bl
  8255. *
  8256. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8257. * access.
  8258. *
  8259. * RETURN:
  8260. * 0 = success
  8261. * !0 = failure
  8262. *
  8263. * PARAMETERS:
  8264. * *cd - pointer the core data structure
  8265. * *status_str - pointer to optional status string buffer
  8266. * buf_size - size of status_str buffer
  8267. ******************************************************************************/
  8268. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8269. {
  8270. int rc;
  8271. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8272. if (rc < 0) {
  8273. pt_debug(cd->dev, DL_ERROR,
  8274. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8275. cd->exclusive_dev, cd->dev);
  8276. return rc;
  8277. }
  8278. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8279. if (release_exclusive(cd, cd->dev) < 0)
  8280. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8281. __func__);
  8282. return rc;
  8283. }
  8284. /*******************************************************************************
  8285. * FUNCTION: _fast_startup
  8286. *
  8287. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8288. *
  8289. * RETURN:
  8290. * 0 = success
  8291. * !0 = failure
  8292. *
  8293. * PARAMETERS:
  8294. * *cd - pointer the core data structure
  8295. ******************************************************************************/
  8296. static int _fast_startup(struct pt_core_data *cd)
  8297. {
  8298. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8299. int rc = 0;
  8300. u8 mode = PT_MODE_UNKNOWN;
  8301. struct pt_hid_desc hid_desc;
  8302. int wait_time = 0;
  8303. memset(&hid_desc, 0, sizeof(hid_desc));
  8304. reset:
  8305. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8306. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8307. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8308. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8309. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8310. __func__);
  8311. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8312. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8313. if (rc < 0) {
  8314. pt_debug(cd->dev, DL_ERROR,
  8315. "%s: Error on getting HID descriptor r=%d\n",
  8316. __func__, rc);
  8317. if (retry--)
  8318. goto reset;
  8319. goto exit;
  8320. }
  8321. cd->mode = pt_get_mode(cd, &hid_desc);
  8322. if (cd->mode == PT_MODE_BOOTLOADER) {
  8323. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8324. __func__);
  8325. rc = pt_hid_output_bl_launch_app_(cd);
  8326. if (rc < 0) {
  8327. pt_debug(cd->dev, DL_ERROR,
  8328. "%s: Error on launch app r=%d\n",
  8329. __func__, rc);
  8330. if (retry--)
  8331. goto reset;
  8332. goto exit;
  8333. }
  8334. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8335. if (rc < 0) {
  8336. pt_debug(cd->dev, DL_ERROR,
  8337. "%s: Error on getting HID descriptor r=%d\n",
  8338. __func__, rc);
  8339. if (retry--)
  8340. goto reset;
  8341. goto exit;
  8342. }
  8343. cd->mode = pt_get_mode(cd, &hid_desc);
  8344. if (cd->mode == PT_MODE_BOOTLOADER) {
  8345. if (retry--)
  8346. goto reset;
  8347. goto exit;
  8348. }
  8349. }
  8350. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8351. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8352. } else {
  8353. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8354. __func__);
  8355. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8356. /* Wait for any sentinel before first try */
  8357. rc = _pt_request_wait_for_enum_state(
  8358. cd->dev, 150,
  8359. STARTUP_STATUS_BL_RESET_SENTINEL |
  8360. STARTUP_STATUS_FW_RESET_SENTINEL);
  8361. if (rc)
  8362. pt_debug(cd->dev, DL_ERROR,
  8363. "%s: No Sentinel detected rc = %d\n",
  8364. __func__, rc);
  8365. } else
  8366. pt_flush_bus_if_irq_asserted(cd,
  8367. PT_FLUSH_BUS_BASED_ON_LEN);
  8368. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8369. if (rc) {
  8370. pt_debug(cd->dev, DL_ERROR,
  8371. "%s: Get mode failed, mode unknown\n",
  8372. __func__);
  8373. mode = PT_MODE_UNKNOWN;
  8374. }
  8375. cd->mode = mode;
  8376. if (cd->mode == PT_MODE_BOOTLOADER) {
  8377. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8378. __func__);
  8379. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8380. if (rc) {
  8381. pt_debug(cd->dev, DL_ERROR,
  8382. "%s Failed to exit bootloader\n",
  8383. __func__);
  8384. msleep(50);
  8385. rc = -ENODEV;
  8386. if (retry--)
  8387. goto reset;
  8388. goto exit;
  8389. } else {
  8390. pt_debug(cd->dev, DL_INFO,
  8391. "%s: Exit bootloader successfully\n",
  8392. __func__);
  8393. }
  8394. if (cd->mode != PT_MODE_OPERATIONAL) {
  8395. pt_debug(cd->dev, DL_WARN,
  8396. "%s: restore mode failure mode = %d\n",
  8397. __func__, cd->mode);
  8398. if (retry--)
  8399. goto reset;
  8400. goto exit;
  8401. }
  8402. }
  8403. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8404. }
  8405. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8406. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8407. if (!rc) {
  8408. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8409. pt_debug(cd->dev, DL_WARN,
  8410. "%s: Exit FW BOOT Mode after %dms\n",
  8411. __func__, wait_time);
  8412. } else {
  8413. pt_debug(cd->dev, DL_WARN,
  8414. "%s: FW stuck in BOOT Mode after %dms\n",
  8415. __func__, wait_time);
  8416. goto exit;
  8417. }
  8418. if (!cd->sysinfo.ready) {
  8419. rc = pt_hid_output_get_sysinfo_(cd);
  8420. if (rc) {
  8421. pt_debug(cd->dev, DL_ERROR,
  8422. "%s: Error on getting sysinfo r=%d\n",
  8423. __func__, rc);
  8424. if (retry--)
  8425. goto reset;
  8426. goto exit;
  8427. }
  8428. }
  8429. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8430. rc = pt_restore_parameters_(cd);
  8431. if (rc)
  8432. pt_debug(cd->dev, DL_ERROR,
  8433. "%s: failed to restore parameters rc=%d\n",
  8434. __func__, rc);
  8435. else
  8436. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8437. exit:
  8438. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8439. return rc;
  8440. }
  8441. /*******************************************************************************
  8442. * FUNCTION: pt_core_poweron_device_
  8443. *
  8444. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8445. *
  8446. * RETURN:
  8447. * 0 = success
  8448. * !0 = failure
  8449. *
  8450. * PARAMETERS:
  8451. * *cd - pointer to core data
  8452. ******************************************************************************/
  8453. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8454. {
  8455. struct device *dev = cd->dev;
  8456. int rc = 0;
  8457. /*
  8458. * After power on action, the chip can general FW sentinel. It can
  8459. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8460. * _fast_startup() can perform a quick enumeration too, here doesn't
  8461. * need another enumeration.
  8462. */
  8463. mutex_lock(&cd->system_lock);
  8464. cd->startup_status = STARTUP_STATUS_START;
  8465. cd->hid_reset_cmd_state = 1;
  8466. mutex_unlock(&cd->system_lock);
  8467. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8468. if (rc < 0) {
  8469. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8470. __func__, rc);
  8471. goto exit;
  8472. }
  8473. if (!cd->irq_enabled) {
  8474. cd->irq_enabled = true;
  8475. enable_irq(cd->irq);
  8476. }
  8477. /* TBD: following function doesn't update startup_status */
  8478. rc = _fast_startup(cd);
  8479. exit:
  8480. return rc;
  8481. }
  8482. /*******************************************************************************
  8483. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8484. *
  8485. * SUMMARY: Reset device, and then trigger a full enumeration.
  8486. *
  8487. * RETURN:
  8488. * 0 = success
  8489. * !0 = failure
  8490. *
  8491. * PARAMETERS:
  8492. * *cd - pointer to core data
  8493. ******************************************************************************/
  8494. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8495. {
  8496. int rc;
  8497. rc = pt_dut_reset_and_wait(cd);
  8498. if (rc < 0) {
  8499. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8500. __func__, rc);
  8501. goto exit;
  8502. }
  8503. rc = _fast_startup(cd);
  8504. exit:
  8505. return rc;
  8506. }
  8507. /*******************************************************************************
  8508. * FUNCTION: pt_core_wake_
  8509. *
  8510. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8511. * the configuration in the core platform data structure.
  8512. *
  8513. * RETURN:
  8514. * 0 = success
  8515. * !0 = failure
  8516. *
  8517. * PARAMETERS:
  8518. * *cd - pointer to core data
  8519. ******************************************************************************/
  8520. static int pt_core_wake_(struct pt_core_data *cd)
  8521. {
  8522. int rc = 0;
  8523. mutex_lock(&cd->system_lock);
  8524. if (cd->sleep_state == SS_SLEEP_ON) {
  8525. cd->sleep_state = SS_WAKING;
  8526. } else {
  8527. mutex_unlock(&cd->system_lock);
  8528. return 1;
  8529. }
  8530. mutex_unlock(&cd->system_lock);
  8531. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8532. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture) && cd->runtime)
  8533. rc = pt_core_wake_device_from_easy_wake_(cd);
  8534. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8535. pt_debug(cd->dev, DL_INFO,
  8536. "%s: Entering into poweron mode:\n", __func__);
  8537. rc = pt_core_poweron_device_(cd);
  8538. if (rc < 0)
  8539. pr_err("%s: Poweron error detected: rc=%d\n",
  8540. __func__, rc);
  8541. }
  8542. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8543. rc = pt_core_wake_device_from_deep_standby_(cd);
  8544. else /* Default action to exit DeepSleep */
  8545. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8546. }
  8547. mutex_lock(&cd->system_lock);
  8548. cd->sleep_state = SS_SLEEP_OFF;
  8549. mutex_unlock(&cd->system_lock);
  8550. pt_start_wd_timer(cd);
  8551. return rc;
  8552. }
  8553. /*******************************************************************************
  8554. * FUNCTION: pt_core_wake_
  8555. *
  8556. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8557. *
  8558. * RETURN:
  8559. * 0 = success
  8560. * !0 = failure
  8561. *
  8562. * PARAMETERS:
  8563. * *cd - pointer to core data
  8564. ******************************************************************************/
  8565. static int pt_core_wake(struct pt_core_data *cd)
  8566. {
  8567. int rc;
  8568. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8569. if (rc < 0) {
  8570. pt_debug(cd->dev, DL_ERROR,
  8571. "%s: fail get exclusive ex=%p own=%p\n",
  8572. __func__, cd->exclusive_dev, cd->dev);
  8573. return rc;
  8574. }
  8575. rc = pt_core_wake_(cd);
  8576. if (release_exclusive(cd, cd->dev) < 0)
  8577. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8578. __func__);
  8579. else
  8580. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8581. __func__);
  8582. return rc;
  8583. }
  8584. /*******************************************************************************
  8585. * FUNCTION: pt_get_ic_crc_
  8586. *
  8587. * SUMMARY: This function retrieves the config block CRC
  8588. *
  8589. * NOTE: The post condition of calling this function will be that the DUT will
  8590. * be in SCANNINING mode if no failures occur
  8591. *
  8592. * RETURN:
  8593. * 0 = success
  8594. * !0 = failure
  8595. *
  8596. * PARAMETERS:
  8597. * *cd - pointer the core data structure
  8598. * ebid - enumerated block ID
  8599. ******************************************************************************/
  8600. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8601. {
  8602. struct pt_sysinfo *si = &cd->sysinfo;
  8603. int rc = 0;
  8604. u8 status;
  8605. u16 calculated_crc = 0;
  8606. u16 stored_crc = 0;
  8607. rc = pt_pip_suspend_scanning_(cd);
  8608. if (rc)
  8609. goto error;
  8610. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8611. &calculated_crc, &stored_crc);
  8612. if (rc)
  8613. goto exit;
  8614. if (status) {
  8615. rc = -EINVAL;
  8616. goto exit;
  8617. }
  8618. si->ttconfig.crc = stored_crc;
  8619. exit:
  8620. pt_pip_resume_scanning_(cd);
  8621. error:
  8622. pt_debug(cd->dev, DL_INFO,
  8623. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8624. __func__, ebid, calculated_crc, stored_crc, rc);
  8625. return rc;
  8626. }
  8627. /*******************************************************************************
  8628. * FUNCTION: pt_pip2_read_gpio
  8629. *
  8630. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8631. * bits into the passed in variable
  8632. *
  8633. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8634. *
  8635. * RETURN:
  8636. * 0 = success
  8637. * !0 = failure
  8638. *
  8639. * PARAMETERS:
  8640. * *dev - pointer to device structure
  8641. * *status - pointer to where the command response status is stored
  8642. * *gpio - pointer to device attributes structure
  8643. ******************************************************************************/
  8644. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8645. {
  8646. int rc = 0;
  8647. u16 actual_read_len;
  8648. u8 read_buf[12];
  8649. u8 tmp_status = 0;
  8650. u8 index = PIP2_RESP_STATUS_OFFSET;
  8651. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8652. rc = _pt_request_pip2_send_cmd(dev,
  8653. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8654. NULL, 0, read_buf, &actual_read_len);
  8655. if (rc) {
  8656. pt_debug(dev, DL_ERROR,
  8657. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8658. rc = -ECOMM;
  8659. } else {
  8660. tmp_status = read_buf[index];
  8661. }
  8662. if (status)
  8663. *status = tmp_status;
  8664. if (!rc && gpio && (tmp_status == 0)) {
  8665. *gpio = ((read_buf[index + 4] << 24) |
  8666. (read_buf[index + 3] << 16) |
  8667. (read_buf[index + 2] << 8) |
  8668. (read_buf[index + 1]));
  8669. }
  8670. return rc;
  8671. }
  8672. /*******************************************************************************
  8673. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8674. *
  8675. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8676. *
  8677. * RETURN:
  8678. * 0 = success
  8679. * !0 = failure
  8680. *
  8681. * PARAMETERS:
  8682. * *cd - pointer the core data structure
  8683. * *pid - pointer to store panel ID
  8684. ******************************************************************************/
  8685. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8686. {
  8687. u32 gpio_value = 0;
  8688. u8 status = 0;
  8689. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8690. int rc = 0;
  8691. if (!pid)
  8692. return -ENOMEM;
  8693. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8694. if (!rc) {
  8695. if (status == 0) {
  8696. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8697. PT_PANEL_ID_SHIFT;
  8698. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8699. __func__,
  8700. "BL mode PID", panel_id, "gpio", gpio_value);
  8701. *pid = panel_id;
  8702. } else {
  8703. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8704. __func__,
  8705. "BL read gpio failed status", status);
  8706. }
  8707. } else {
  8708. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8709. __func__,
  8710. "BL read gpio failed status", status);
  8711. }
  8712. return rc;
  8713. }
  8714. /*******************************************************************************
  8715. * FUNCTION: pt_enum_with_dut_
  8716. *
  8717. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8718. * The core data (cd) startup_status will store, as a bitmask, each
  8719. * state of the enumeration process. The startup will be attempted
  8720. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8721. *
  8722. * RETURN:
  8723. * 0 = success
  8724. * !0 = failure
  8725. *
  8726. * PARAMETERS:
  8727. * *cd - pointer the core data structure
  8728. * reset - Flag to reset the DUT before attempting to enumerate
  8729. * *status - poionter to store the enum status bitmask flags
  8730. ******************************************************************************/
  8731. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8732. u32 *enum_status)
  8733. {
  8734. int try = 1;
  8735. int rc = 0;
  8736. int wait_time = 0;
  8737. bool detected = false;
  8738. u8 return_data[8];
  8739. u8 mode = PT_MODE_UNKNOWN;
  8740. u8 pid = PANEL_ID_NOT_ENABLED;
  8741. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8742. struct pt_hid_desc hid_desc;
  8743. memset(&hid_desc, 0, sizeof(hid_desc));
  8744. #ifdef TTHE_TUNER_SUPPORT
  8745. tthe_print(cd, NULL, 0, "enter startup");
  8746. #endif
  8747. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8748. __func__, cd->startup_status, reset);
  8749. pt_stop_wd_timer(cd);
  8750. reset:
  8751. if (try > 1)
  8752. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8753. __func__, try);
  8754. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8755. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8756. pt_debug(cd->dev, DL_INFO,
  8757. "%s: PIP1 Enumeration start\n", __func__);
  8758. /* Only reset the DUT after the first try */
  8759. if (reset || (try > 1)) {
  8760. /*
  8761. * Reset hardware only for Legacy parts. Skip for TT/TC
  8762. * parts because if the FW image was loaded directly
  8763. * to SRAM issueing a reset ill wipe out what was just
  8764. * loaded.
  8765. */
  8766. rc = pt_dut_reset_and_wait(cd);
  8767. if (rc < 0) {
  8768. pt_debug(cd->dev, DL_ERROR,
  8769. "%s: Error on h/w reset r=%d\n",
  8770. __func__, rc);
  8771. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8772. goto reset;
  8773. goto exit;
  8774. }
  8775. /* sleep to allow FW to be launched if available */
  8776. msleep(120);
  8777. }
  8778. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8779. if (rc < 0) {
  8780. pt_debug(cd->dev, DL_ERROR,
  8781. "%s: Error getting HID Descriptor r=%d\n",
  8782. __func__, rc);
  8783. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8784. goto reset;
  8785. rc = -EIO;
  8786. goto exit;
  8787. }
  8788. detected = true;
  8789. cd->mode = pt_get_mode(cd, &hid_desc);
  8790. /*
  8791. * Most systems do not use an XY pin as the panel_id and so
  8792. * the BL is used to retrieve the panel_id, however for
  8793. * systems that do use an XY pin, the panel_id MUST be
  8794. * retrieved from the system information when running FW
  8795. * (done below) and so this section of code is skipped.
  8796. * Entering the BL here is only needed on XY_PIN systems.
  8797. */
  8798. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8799. if (cd->mode == PT_MODE_OPERATIONAL) {
  8800. rc = pt_pip_start_bootloader_(cd);
  8801. if (rc < 0) {
  8802. pt_debug(cd->dev, DL_ERROR,
  8803. "%s: Error on start bootloader r=%d\n",
  8804. __func__, rc);
  8805. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8806. goto reset;
  8807. goto exit;
  8808. }
  8809. cd->mode = PT_MODE_BOOTLOADER;
  8810. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8811. __func__);
  8812. }
  8813. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8814. if (!rc) {
  8815. cd->bl_info.ready = true;
  8816. cd->bl_info.chip_id =
  8817. get_unaligned_le16(&return_data[2]);
  8818. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8819. __func__, cd->bl_info.chip_id);
  8820. } else {
  8821. pt_debug(cd->dev, DL_ERROR,
  8822. "%s: failed to get chip ID, r=%d\n",
  8823. __func__, rc);
  8824. }
  8825. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8826. mutex_lock(&cd->system_lock);
  8827. if (!rc) {
  8828. cd->pid_for_loader = pid;
  8829. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8830. __func__, cd->pid_for_loader);
  8831. } else {
  8832. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8833. pt_debug(cd->dev, DL_WARN,
  8834. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8835. __func__, cd->pid_for_loader);
  8836. }
  8837. mutex_unlock(&cd->system_lock);
  8838. }
  8839. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  8840. if (cd->mode == PT_MODE_BOOTLOADER) {
  8841. rc = pt_hid_output_bl_launch_app_(cd);
  8842. if (rc < 0) {
  8843. pt_debug(cd->dev, DL_ERROR,
  8844. "%s: Error on launch app r=%d\n",
  8845. __func__, rc);
  8846. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8847. goto reset;
  8848. rc = -ENODEV;
  8849. goto exit;
  8850. }
  8851. /* sleep to allow FW to be launched if available */
  8852. msleep(120);
  8853. /* Ensure the DUT is now in Application mode */
  8854. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8855. if (rc < 0) {
  8856. pt_debug(cd->dev, DL_ERROR,
  8857. "%s: Error getting HID Desc r=%d\n",
  8858. __func__, rc);
  8859. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8860. goto reset;
  8861. rc = -EIO;
  8862. goto exit;
  8863. }
  8864. cd->mode = pt_get_mode(cd, &hid_desc);
  8865. if (cd->mode == PT_MODE_BOOTLOADER) {
  8866. pt_debug(cd->dev, DL_WARN,
  8867. "%s: Error confiming exit BL\n",
  8868. __func__);
  8869. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8870. goto reset;
  8871. rc = -ENODEV;
  8872. goto exit;
  8873. }
  8874. }
  8875. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  8876. cd->mode = PT_MODE_OPERATIONAL;
  8877. *enum_status |= STARTUP_STATUS_GET_DESC;
  8878. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8879. } else {
  8880. /* Generation is PIP2 Capable */
  8881. pt_debug(cd->dev, DL_INFO,
  8882. "%s: PIP2 Enumeration start\n", __func__);
  8883. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8884. if (rc) {
  8885. pt_debug(cd->dev, DL_ERROR,
  8886. "%s: Get mode failed, mode unknown\n",
  8887. __func__);
  8888. mode = PT_MODE_UNKNOWN;
  8889. } else
  8890. detected = true;
  8891. cd->mode = mode;
  8892. switch (cd->mode) {
  8893. case PT_MODE_OPERATIONAL:
  8894. pt_debug(cd->dev, DL_INFO,
  8895. "%s: Operational mode\n", __func__);
  8896. if (cd->app_pip_ver_ready == false) {
  8897. rc = pt_pip2_get_version_(cd);
  8898. if (!rc)
  8899. cd->app_pip_ver_ready = true;
  8900. else {
  8901. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8902. goto reset;
  8903. goto exit;
  8904. }
  8905. }
  8906. break;
  8907. case PT_MODE_BOOTLOADER:
  8908. pt_debug(cd->dev, DL_INFO,
  8909. "%s: Bootloader mode\n", __func__);
  8910. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8911. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  8912. mutex_lock(&cd->system_lock);
  8913. if (!rc) {
  8914. cd->pid_for_loader = pid;
  8915. pt_debug(cd->dev, DL_INFO,
  8916. "%s: Panel ID: 0x%02X\n",
  8917. __func__, cd->pid_for_loader);
  8918. } else {
  8919. cd->pid_for_loader =
  8920. PANEL_ID_NOT_ENABLED;
  8921. pt_debug(cd->dev, DL_WARN,
  8922. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8923. __func__, cd->pid_for_loader);
  8924. }
  8925. mutex_unlock(&cd->system_lock);
  8926. }
  8927. if (cd->bl_pip_ver_ready == false) {
  8928. rc = pt_pip2_get_version_(cd);
  8929. if (!rc)
  8930. cd->bl_pip_ver_ready = true;
  8931. else {
  8932. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8933. goto reset;
  8934. goto exit;
  8935. }
  8936. }
  8937. /*
  8938. * Launch app command will fail in flashless mode.
  8939. * Skip launch app command here to save time for
  8940. * enumeration flow.
  8941. */
  8942. if (cd->flashless_dut)
  8943. goto exit;
  8944. /*
  8945. * pt_pip2_launch_app() is needed here instead of
  8946. * pt_pip2_exit_bl() because exit_bl will cause several
  8947. * hw_resets to occur and the auto BL on a flashless
  8948. * DUT will fail.
  8949. */
  8950. rc = pt_pip2_launch_app(cd->dev,
  8951. PT_CORE_CMD_UNPROTECTED);
  8952. if (rc) {
  8953. pt_debug(cd->dev, DL_ERROR,
  8954. "%s: Error on launch app r=%d\n",
  8955. __func__, rc);
  8956. msleep(50);
  8957. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8958. goto reset;
  8959. goto exit;
  8960. }
  8961. /*
  8962. * IRQ thread can be delayed if the serial log print is
  8963. * enabled. It causes next command to get wrong response
  8964. * Here the delay is to ensure pt_parse_input() to be
  8965. * finished.
  8966. */
  8967. msleep(60);
  8968. /* Check and update the mode */
  8969. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8970. if (rc) {
  8971. pt_debug(cd->dev, DL_ERROR,
  8972. "%s: Get mode failed, mode unknown\n",
  8973. __func__);
  8974. mode = PT_MODE_UNKNOWN;
  8975. }
  8976. cd->mode = mode;
  8977. if (cd->mode == PT_MODE_OPERATIONAL) {
  8978. pt_debug(cd->dev, DL_INFO,
  8979. "%s: Launched to Operational mode\n",
  8980. __func__);
  8981. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  8982. pt_debug(cd->dev, DL_ERROR,
  8983. "%s: Launch failed, Bootloader mode\n",
  8984. __func__);
  8985. goto exit;
  8986. } else if (cd->mode == PT_MODE_UNKNOWN) {
  8987. pt_debug(cd->dev, DL_ERROR,
  8988. "%s: Launch failed, Unknown mode\n",
  8989. __func__);
  8990. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8991. goto reset;
  8992. goto exit;
  8993. }
  8994. if (cd->app_pip_ver_ready == false) {
  8995. rc = pt_pip2_get_version_(cd);
  8996. if (!rc)
  8997. cd->app_pip_ver_ready = true;
  8998. else {
  8999. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9000. goto reset;
  9001. goto exit;
  9002. }
  9003. }
  9004. break;
  9005. default:
  9006. pt_debug(cd->dev, DL_ERROR,
  9007. "%s: Unknown mode\n", __func__);
  9008. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9009. goto reset;
  9010. goto exit;
  9011. }
  9012. *enum_status |= STARTUP_STATUS_GET_DESC;
  9013. }
  9014. pt_init_pip_report_fields(cd);
  9015. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9016. if (!cd->features.easywake)
  9017. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9018. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9019. rc = pt_hid_output_get_sysinfo_(cd);
  9020. if (rc) {
  9021. pt_debug(cd->dev, DL_ERROR,
  9022. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9023. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9024. goto reset;
  9025. goto exit;
  9026. }
  9027. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9028. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9029. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9030. if (!rc) {
  9031. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9032. pt_debug(cd->dev, DL_WARN,
  9033. "%s: Exit FW BOOT Mode after %dms\n",
  9034. __func__, wait_time);
  9035. } else {
  9036. pt_debug(cd->dev, DL_WARN,
  9037. "%s: FW stuck in BOOT Mode after %dms\n",
  9038. __func__, wait_time);
  9039. goto exit;
  9040. }
  9041. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9042. __func__,
  9043. cd->sysinfo.ttdata.pip_ver_major,
  9044. cd->sysinfo.ttdata.pip_ver_minor);
  9045. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9046. if (rc) {
  9047. pt_debug(cd->dev, DL_ERROR,
  9048. "%s: DUT Config block CRC failure rc=%d\n",
  9049. __func__, rc);
  9050. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9051. goto reset;
  9052. } else {
  9053. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9054. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9055. pt_debug(cd->dev, DL_ERROR,
  9056. "%s: scan state: %d, retry: %d\n",
  9057. __func__, sys_mode, try);
  9058. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9059. goto reset;
  9060. } else
  9061. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9062. }
  9063. rc = pt_restore_parameters_(cd);
  9064. if (rc) {
  9065. pt_debug(cd->dev, DL_ERROR,
  9066. "%s: Failed to restore parameters rc=%d\n",
  9067. __func__, rc);
  9068. } else
  9069. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9070. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9071. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9072. cd->startup_retry_count = 0;
  9073. exit:
  9074. /* Generate the HW Version of the connected DUT and store in cd */
  9075. pt_generate_hw_version(cd, cd->hw_version);
  9076. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9077. cd->hw_version);
  9078. pt_start_wd_timer(cd);
  9079. if (!detected)
  9080. rc = -ENODEV;
  9081. #ifdef TTHE_TUNER_SUPPORT
  9082. tthe_print(cd, NULL, 0, "exit startup");
  9083. #endif
  9084. pt_debug(cd->dev, DL_WARN,
  9085. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9086. __func__, rc, try);
  9087. return rc;
  9088. }
  9089. /*******************************************************************************
  9090. * FUNCTION: pt_enum_with_dut
  9091. *
  9092. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9093. * requesting exclusive access around the call.
  9094. *
  9095. * RETURN:
  9096. * 0 = success
  9097. * !0 = failure
  9098. *
  9099. * PARAMETERS:
  9100. * *cd - pointer the core data structure
  9101. * reset - Flag to reset the DUT before attempting to enumerate
  9102. * *status - pointer to store the enum status bitmask flags
  9103. ******************************************************************************/
  9104. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9105. {
  9106. int rc = 0;
  9107. mutex_lock(&cd->system_lock);
  9108. cd->startup_state = STARTUP_RUNNING;
  9109. mutex_unlock(&cd->system_lock);
  9110. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9111. if (rc) {
  9112. pt_debug(cd->dev, DL_ERROR,
  9113. "%s: fail get exclusive ex=%p own=%p\n",
  9114. __func__, cd->exclusive_dev, cd->dev);
  9115. goto exit;
  9116. }
  9117. rc = pt_enum_with_dut_(cd, reset, status);
  9118. if (release_exclusive(cd, cd->dev) < 0)
  9119. /* Don't return fail code, mode is already changed. */
  9120. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9121. __func__);
  9122. else
  9123. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9124. __func__);
  9125. exit:
  9126. mutex_lock(&cd->system_lock);
  9127. /* Clear startup state for any tasks waiting for startup completion */
  9128. cd->startup_state = STARTUP_NONE;
  9129. mutex_unlock(&cd->system_lock);
  9130. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9131. *status |= STARTUP_STATUS_COMPLETE;
  9132. /* Wake the waiters for end of startup */
  9133. wake_up(&cd->wait_q);
  9134. return rc;
  9135. }
  9136. static int add_sysfs_interfaces(struct device *dev);
  9137. static void remove_sysfs_interfaces(struct device *dev);
  9138. static void remove_sysfs_and_modules(struct device *dev);
  9139. static void pt_release_modules(struct pt_core_data *cd);
  9140. static void pt_probe_modules(struct pt_core_data *cd);
  9141. /*******************************************************************************
  9142. * FUNCTION: _pt_ttdl_restart
  9143. *
  9144. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9145. *
  9146. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9147. * existing sysfs nodes will produce an error.
  9148. *
  9149. * RETURN:
  9150. * 0 = success
  9151. * !0 = failure
  9152. *
  9153. * PARAMETERS:
  9154. * *dev - pointer to core device
  9155. ******************************************************************************/
  9156. static int _pt_ttdl_restart(struct device *dev)
  9157. {
  9158. int rc = 0;
  9159. struct pt_core_data *cd = dev_get_drvdata(dev);
  9160. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9161. struct i2c_client *client =
  9162. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9163. #endif
  9164. /*
  9165. * Make sure the device is awake, pt_mt_release function will
  9166. * cause pm sleep function and lead to deadlock.
  9167. */
  9168. pm_runtime_get_sync(dev);
  9169. /* Use ttdl_restart_lock to avoid reentry */
  9170. mutex_lock(&cd->ttdl_restart_lock);
  9171. remove_sysfs_and_modules(cd->dev);
  9172. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9173. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9174. pt_debug(dev, DL_ERROR,
  9175. "%s I2C functionality not Supported\n", __func__);
  9176. rc = -EIO;
  9177. goto ttdl_no_error;
  9178. }
  9179. #endif
  9180. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9181. rc = _pt_detect_dut_generation(cd->dev,
  9182. &cd->startup_status, &cd->active_dut_generation,
  9183. &cd->mode);
  9184. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9185. pt_debug(dev, DL_ERROR,
  9186. "%s: Error, Unknown DUT Generation rc=%d\n",
  9187. __func__, rc);
  9188. }
  9189. }
  9190. rc = add_sysfs_interfaces(cd->dev);
  9191. if (rc < 0)
  9192. pt_debug(cd->dev, DL_ERROR,
  9193. "%s: Error, failed sysfs nodes rc=%d\n",
  9194. __func__, rc);
  9195. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9196. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9197. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9198. if (rc < 0)
  9199. pt_debug(dev, DL_ERROR,
  9200. "%s: Error, Failed to Enumerate\n", __func__);
  9201. }
  9202. rc = pt_mt_probe(dev);
  9203. if (rc < 0) {
  9204. pt_debug(dev, DL_ERROR,
  9205. "%s: Error, fail mt probe\n", __func__);
  9206. }
  9207. rc = pt_btn_probe(dev);
  9208. if (rc < 0) {
  9209. pt_debug(dev, DL_ERROR,
  9210. "%s: Error, fail btn probe\n", __func__);
  9211. }
  9212. pt_probe_modules(cd);
  9213. pt_debug(cd->dev, DL_WARN,
  9214. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9215. rc = 0;
  9216. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9217. ttdl_no_error:
  9218. #endif
  9219. mutex_unlock(&cd->ttdl_restart_lock);
  9220. mutex_lock(&cd->system_lock);
  9221. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9222. cd->startup_state = STARTUP_NONE;
  9223. mutex_unlock(&cd->system_lock);
  9224. pm_runtime_put(dev);
  9225. return rc;
  9226. }
  9227. /*******************************************************************************
  9228. * FUNCTION: pt_restart_work_function
  9229. *
  9230. * SUMMARY: This is the wrapper function placed in a work queue to call
  9231. * _pt_ttdl_restart()
  9232. *
  9233. * RETURN: none
  9234. *
  9235. * PARAMETERS:
  9236. * *work - pointer to the work_struct
  9237. ******************************************************************************/
  9238. static void pt_restart_work_function(struct work_struct *work)
  9239. {
  9240. struct pt_core_data *cd = container_of(work,
  9241. struct pt_core_data, ttdl_restart_work);
  9242. int rc = 0;
  9243. rc = _pt_ttdl_restart(cd->dev);
  9244. if (rc < 0)
  9245. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9246. __func__, rc);
  9247. }
  9248. /*******************************************************************************
  9249. * FUNCTION: pt_enum_work_function
  9250. *
  9251. * SUMMARY: This is the wrapper function placed in a work queue to call
  9252. * pt_enum_with_dut()
  9253. *
  9254. * RETURN: none
  9255. *
  9256. * PARAMETERS:
  9257. * *work - pointer to the work_struct
  9258. ******************************************************************************/
  9259. static void pt_enum_work_function(struct work_struct *work)
  9260. {
  9261. struct pt_core_data *cd = container_of(work,
  9262. struct pt_core_data, enum_work);
  9263. int rc;
  9264. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9265. if (rc < 0)
  9266. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9267. __func__, rc);
  9268. }
  9269. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9270. {
  9271. int rc;
  9272. if (!get) {
  9273. rc = 0;
  9274. goto regulator_put;
  9275. }
  9276. cd->vdd = regulator_get(cd->dev, "vdd");
  9277. if (IS_ERR(cd->vdd)) {
  9278. rc = PTR_ERR(cd->vdd);
  9279. dev_err(cd->dev,
  9280. "Regulator get failed vdd rc=%d\n", rc);
  9281. goto regulator_put;
  9282. }
  9283. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9284. if (IS_ERR(cd->vcc_i2c)) {
  9285. rc = PTR_ERR(cd->vcc_i2c);
  9286. dev_err(cd->dev,
  9287. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9288. goto regulator_put;
  9289. }
  9290. return 0;
  9291. regulator_put:
  9292. if (cd->vdd) {
  9293. regulator_put(cd->vdd);
  9294. cd->vdd = NULL;
  9295. }
  9296. if (cd->vcc_i2c) {
  9297. regulator_put(cd->vcc_i2c);
  9298. cd->vcc_i2c = NULL;
  9299. }
  9300. return rc;
  9301. }
  9302. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9303. {
  9304. int rc;
  9305. if (!en) {
  9306. rc = 0;
  9307. goto disable_vcc_i2c_reg;
  9308. }
  9309. if (cd->vdd) {
  9310. if (regulator_count_voltages(cd->vdd) > 0) {
  9311. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9312. FT_VTG_MAX_UV);
  9313. if (rc) {
  9314. dev_err(cd->dev,
  9315. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9316. goto exit;
  9317. }
  9318. }
  9319. rc = regulator_enable(cd->vdd);
  9320. if (rc) {
  9321. dev_err(cd->dev,
  9322. "Regulator vdd enable failed rc=%d\n", rc);
  9323. goto exit;
  9324. }
  9325. }
  9326. if (cd->vcc_i2c) {
  9327. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9328. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9329. FT_I2C_VTG_MAX_UV);
  9330. if (rc) {
  9331. dev_err(cd->dev,
  9332. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9333. goto disable_vdd_reg;
  9334. }
  9335. }
  9336. rc = regulator_enable(cd->vcc_i2c);
  9337. if (rc) {
  9338. dev_err(cd->dev,
  9339. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9340. goto disable_vdd_reg;
  9341. }
  9342. }
  9343. return 0;
  9344. disable_vcc_i2c_reg:
  9345. if (cd->vcc_i2c) {
  9346. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9347. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9348. FT_I2C_VTG_MAX_UV);
  9349. regulator_disable(cd->vcc_i2c);
  9350. }
  9351. disable_vdd_reg:
  9352. if (cd->vdd) {
  9353. if (regulator_count_voltages(cd->vdd) > 0)
  9354. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9355. FT_VTG_MAX_UV);
  9356. regulator_disable(cd->vdd);
  9357. }
  9358. exit:
  9359. return rc;
  9360. }
  9361. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9362. #define KERNEL_VER_GT_3_19
  9363. #endif
  9364. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9365. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9366. #if defined(CONFIG_PM_SLEEP)
  9367. /*******************************************************************************
  9368. * FUNCTION: pt_core_rt_suspend
  9369. *
  9370. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9371. *
  9372. * RETURN:
  9373. * 0 = success
  9374. * !0 = failure
  9375. *
  9376. * PARAMETERS:
  9377. * *dev - pointer to core device
  9378. ******************************************************************************/
  9379. static int pt_core_rt_suspend(struct device *dev)
  9380. {
  9381. struct pt_core_data *cd = dev_get_drvdata(dev);
  9382. int rc = 0;
  9383. dev_info(dev, "%s: Entering into runtime suspend mode:\n",
  9384. __func__);
  9385. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9386. return 0;
  9387. if (cd->sleep_state == SS_SLEEP_OFF)
  9388. cd->runtime = 1;
  9389. rc = pt_core_sleep(cd);
  9390. if (rc < 0) {
  9391. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9392. return -EAGAIN;
  9393. }
  9394. return 0;
  9395. }
  9396. /*******************************************************************************
  9397. * FUNCTION: pt_core_rt_resume
  9398. *
  9399. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9400. *
  9401. * RETURN:
  9402. * 0 = success
  9403. * !0 = failure
  9404. *
  9405. * PARAMETERS:
  9406. * *dev - pointer to core device
  9407. ******************************************************************************/
  9408. static int pt_core_rt_resume(struct device *dev)
  9409. {
  9410. struct pt_core_data *cd = dev_get_drvdata(dev);
  9411. int rc = 0;
  9412. dev_info(dev, "%s: Entering into runtime resume mode:\n",
  9413. __func__);
  9414. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9415. return 0;
  9416. rc = pt_core_wake(cd);
  9417. if (rc < 0) {
  9418. pt_debug(dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9419. return -EAGAIN;
  9420. }
  9421. if (cd->sleep_state == SS_SLEEP_OFF)
  9422. cd->runtime = 0;
  9423. return 0;
  9424. }
  9425. #endif /* CONFIG_PM_SLEEP */
  9426. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9427. #if defined(CONFIG_PM_SLEEP)
  9428. /*******************************************************************************
  9429. * FUNCTION: pt_core_suspend_
  9430. *
  9431. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9432. * pt_core_sleep. This function may disable IRQ during sleep state.
  9433. *
  9434. * RETURN:
  9435. * 0 = success
  9436. * !0 = failure
  9437. *
  9438. * PARAMETERS:
  9439. * *dev - pointer to core device
  9440. ******************************************************************************/
  9441. static int pt_core_suspend_(struct device *dev)
  9442. {
  9443. int rc;
  9444. struct pt_core_data *cd = dev_get_drvdata(dev);
  9445. pt_debug(dev, DL_INFO, "%s: Entering into suspend mode:\n",
  9446. __func__);
  9447. rc = pt_core_sleep(cd);
  9448. if (rc < 0) {
  9449. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9450. return -EAGAIN;
  9451. }
  9452. rc = pt_enable_regulator(cd, false);
  9453. if (rc) {
  9454. dev_err(dev, "%s: Failed to disable regulators: rc=%d\n",
  9455. __func__, rc);
  9456. }
  9457. dev_info(dev, "%s: Sayantan1: Voltage regulators disabled: rc=%d\n",
  9458. __func__, rc);
  9459. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture) && !cd->runtime)
  9460. return 0;
  9461. /* Required to prevent interrupts before bus awake */
  9462. disable_irq(cd->irq);
  9463. cd->irq_disabled = 1;
  9464. if (device_may_wakeup(dev)) {
  9465. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9466. __func__);
  9467. if (!enable_irq_wake(cd->irq))
  9468. cd->irq_wake = 1;
  9469. } else {
  9470. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9471. __func__);
  9472. }
  9473. return rc;
  9474. }
  9475. /*******************************************************************************
  9476. * FUNCTION: pt_core_suspend
  9477. *
  9478. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9479. * woke up or put to sleep based on Kernel power state even when the display
  9480. * is off based on the check of TTDL core platform flag.
  9481. *
  9482. * RETURN:
  9483. * 0 = success
  9484. * !0 = failure
  9485. *
  9486. * PARAMETERS:
  9487. * *dev - pointer to core device
  9488. ******************************************************************************/
  9489. static int pt_core_suspend(struct device *dev)
  9490. {
  9491. struct pt_core_data *cd = dev_get_drvdata(dev);
  9492. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9493. return 0;
  9494. return pt_core_suspend_(dev);
  9495. }
  9496. /*******************************************************************************
  9497. * FUNCTION: pt_core_resume_
  9498. *
  9499. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9500. * pt_core_wake. This function may enable IRQ before wake up.
  9501. *
  9502. * RETURN:
  9503. * 0 = success
  9504. * !0 = failure
  9505. *
  9506. * PARAMETERS:
  9507. * *dev - pointer to core device
  9508. ******************************************************************************/
  9509. static int pt_core_resume_(struct device *dev)
  9510. {
  9511. int rc = 0;
  9512. struct pt_core_data *cd = dev_get_drvdata(dev);
  9513. dev_info(dev, "%s: Entering into resume mode:\n",
  9514. __func__);
  9515. rc = pt_enable_regulator(cd, true);
  9516. if (rc < 0) {
  9517. dev_err(dev, "%s: Failed to enable regulators: rc=%d\n",
  9518. __func__, rc);
  9519. }
  9520. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9521. __func__, rc);
  9522. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture) && !cd->runtime)
  9523. goto exit;
  9524. /*
  9525. * Bus pm does not call suspend if device runtime suspended
  9526. * This flag is covers that case
  9527. */
  9528. if (cd->irq_disabled) {
  9529. enable_irq(cd->irq);
  9530. cd->irq_disabled = 0;
  9531. }
  9532. if (device_may_wakeup(dev)) {
  9533. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9534. __func__);
  9535. if (cd->irq_wake) {
  9536. disable_irq_wake(cd->irq);
  9537. cd->irq_wake = 0;
  9538. }
  9539. } else {
  9540. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9541. __func__);
  9542. }
  9543. exit:
  9544. pt_core_wake(cd);
  9545. return 0;
  9546. }
  9547. /*******************************************************************************
  9548. * FUNCTION: pt_core_resume
  9549. *
  9550. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9551. * along with kernel power state even the display is off based on the check of
  9552. * TTDL core platform flag.
  9553. *
  9554. * RETURN:
  9555. * 0 = success
  9556. * !0 = failure
  9557. *
  9558. * PARAMETERS:
  9559. * *dev - pointer to core device
  9560. ******************************************************************************/
  9561. static int pt_core_resume(struct device *dev)
  9562. {
  9563. struct pt_core_data *cd = dev_get_drvdata(dev);
  9564. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9565. return 0;
  9566. return pt_core_resume_(dev);
  9567. }
  9568. #endif
  9569. #ifdef NEED_SUSPEND_NOTIFIER
  9570. /*******************************************************************************
  9571. * FUNCTION: pt_pm_notifier
  9572. *
  9573. * SUMMARY: This function is registered to notifier chain and will perform
  9574. * suspend operation if match event PM_SUSPEND_PREPARE.
  9575. *
  9576. * RETURN:
  9577. * 0 = success
  9578. * !0 = failure
  9579. *
  9580. * PARAMETERS:
  9581. * *nb - pointer to notifier_block structure
  9582. * action - notifier event type
  9583. * *data - void pointer
  9584. ******************************************************************************/
  9585. static int pt_pm_notifier(struct notifier_block *nb,
  9586. unsigned long action, void *data)
  9587. {
  9588. struct pt_core_data *cd = container_of(nb,
  9589. struct pt_core_data, pm_notifier);
  9590. if (action == PM_SUSPEND_PREPARE) {
  9591. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9592. __func__);
  9593. /*
  9594. * If PM runtime is not suspended, either call runtime
  9595. * PM suspend callback or wait until it finishes
  9596. */
  9597. if (!pm_runtime_suspended(cd->dev))
  9598. pm_runtime_suspend(cd->dev);
  9599. (void) pt_core_suspend(cd->dev);
  9600. }
  9601. return NOTIFY_DONE;
  9602. }
  9603. #endif
  9604. const struct dev_pm_ops pt_pm_ops = {
  9605. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9606. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9607. NULL)
  9608. };
  9609. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9610. /*******************************************************************************
  9611. * FUNCTION: _pt_request_pip2_enter_bl
  9612. *
  9613. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9614. * XRES pin or a soft reset.
  9615. *
  9616. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9617. * the WD active could cause this function to fail!
  9618. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9619. * will not try to determine the current mode but will proceed with
  9620. * resetting the DUT and entering the BL.
  9621. *
  9622. * NOTE: The definition of result code:
  9623. * PT_ENTER_BL_PASS (0)
  9624. * PT_ENTER_BL_ERROR (1)
  9625. * PT_ENTER_BL_RESET_FAIL (2)
  9626. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9627. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9628. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9629. *
  9630. * RETURNS:
  9631. * 0 = success
  9632. * !0 = failure
  9633. *
  9634. *
  9635. * PARAMETERS:
  9636. * *dev - pointer to device structure
  9637. * *start_mode - pointer to the mode the DUT was in when this function
  9638. * starts
  9639. * *result - pointer to store the result when to enter BL
  9640. ******************************************************************************/
  9641. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9642. {
  9643. int rc = 0;
  9644. int t;
  9645. int tmp_result = PT_ENTER_BL_ERROR;
  9646. int flash_info_retry = 2;
  9647. u8 mode = PT_MODE_UNKNOWN;
  9648. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9649. u8 read_buf[32];
  9650. u16 actual_read_len;
  9651. struct pt_core_data *cd = dev_get_drvdata(dev);
  9652. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9653. u8 time = 0;
  9654. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9655. if (cd->watchdog_enabled) {
  9656. pt_debug(dev, DL_WARN,
  9657. "%s: Watchdog must be stopped before entering BL\n",
  9658. __func__);
  9659. goto exit;
  9660. }
  9661. cancel_work_sync(&cd->enum_work);
  9662. cancel_work_sync(&cd->watchdog_work);
  9663. /* if undefined assume operational/test to bypass all checks */
  9664. if (*start_mode == PT_MODE_IGNORE) {
  9665. mode = PT_MODE_OPERATIONAL;
  9666. sys_mode = FW_SYS_MODE_TEST;
  9667. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9668. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9669. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9670. if (rc) {
  9671. pt_debug(dev, DL_ERROR,
  9672. "%s: Get mode failed, mode unknown\n",
  9673. __func__);
  9674. }
  9675. *start_mode = mode;
  9676. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9677. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9678. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9679. sys_mode = FW_SYS_MODE_SCANNING;
  9680. }
  9681. _retry:
  9682. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9683. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9684. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9685. switch (mode) {
  9686. case PT_MODE_UNKNOWN:
  9687. /*
  9688. * When the mode could not be determined the DUT could be
  9689. * in App mode running corrupted FW or FW that is not
  9690. * responding to the mode request, assume no communication
  9691. * and do a hard reset
  9692. */
  9693. mutex_lock(&cd->system_lock);
  9694. cd->startup_status = STARTUP_STATUS_START;
  9695. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9696. mutex_unlock(&cd->system_lock);
  9697. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9698. if (rc) {
  9699. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9700. goto exit;
  9701. }
  9702. break;
  9703. case PT_MODE_OPERATIONAL:
  9704. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9705. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9706. __func__);
  9707. rc = pt_pip_suspend_scanning_(cd);
  9708. if (rc) {
  9709. /*
  9710. * Print to log but don't exit, the FW could be
  9711. * running but be hung or fail to respond to
  9712. * this request
  9713. */
  9714. pt_debug(dev, DL_ERROR,
  9715. "%s Suspend Scan Failed\n", __func__);
  9716. }
  9717. /* sleep to allow the suspend scan to be processed */
  9718. usleep_range(1000, 2000);
  9719. }
  9720. mutex_lock(&cd->system_lock);
  9721. cd->startup_status = STARTUP_STATUS_START;
  9722. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9723. mutex_unlock(&cd->system_lock);
  9724. /* Reset device to enter the BL */
  9725. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9726. if (rc) {
  9727. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9728. goto exit;
  9729. }
  9730. break;
  9731. case PT_MODE_BOOTLOADER:
  9732. /* Do nothing as we are already in the BL */
  9733. tmp_result = PT_ENTER_BL_PASS;
  9734. goto exit;
  9735. default:
  9736. /* Should NEVER get here */
  9737. tmp_result = PT_ENTER_BL_ERROR;
  9738. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  9739. goto exit;
  9740. }
  9741. if (!cd->flashless_dut &&
  9742. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  9743. /*
  9744. * Sending the special "Host Mode" command will instruct the
  9745. * BL to not execute the FW it has loaded into RAM.
  9746. * The command must be sent within a 40ms window from releasing
  9747. * the XRES pin. If the messages is sent too early it will NAK,
  9748. * so keep sending it every 2ms until it is accepted by the BL.
  9749. * A no-flash DUT does not require this command as there is no
  9750. * FW for the BL to load and execute.
  9751. */
  9752. usleep_range(4000, 6000);
  9753. pt_debug(cd->dev, DL_INFO,
  9754. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  9755. __func__, (int)sizeof(host_mode_cmd));
  9756. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  9757. (int)sizeof(host_mode_cmd), ">>> User CMD");
  9758. rc = 1;
  9759. while (rc && time < 34) {
  9760. rc = pt_adap_write_read_specific(cd, 4,
  9761. host_mode_cmd, NULL);
  9762. usleep_range(1800, 2000);
  9763. time += 2;
  9764. }
  9765. /* Sleep to allow the BL to come up */
  9766. usleep_range(1000, 2000);
  9767. }
  9768. /*
  9769. * To avoid the case that next PIP command can be confused by BL/FW
  9770. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  9771. * followed by "wakeup event" function can lower the failure rate.
  9772. */
  9773. t = wait_event_timeout(cd->wait_q,
  9774. ((cd->startup_status != STARTUP_STATUS_START)
  9775. && (cd->hid_reset_cmd_state == 0)),
  9776. msecs_to_jiffies(300));
  9777. if (IS_TMO(t)) {
  9778. pt_debug(cd->dev, DL_ERROR,
  9779. "%s: TMO waiting for BL sentinel\n", __func__);
  9780. }
  9781. /* Check if device is now in BL mode */
  9782. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9783. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  9784. cd->startup_status);
  9785. if (!rc && mode == PT_MODE_BOOTLOADER) {
  9786. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  9787. mutex_lock(&cd->system_lock);
  9788. cd->pip2_prot_active = true;
  9789. cd->mode = PT_MODE_BOOTLOADER;
  9790. mutex_unlock(&cd->system_lock);
  9791. tmp_result = PT_ENTER_BL_PASS;
  9792. } else {
  9793. /*
  9794. * If the device doesn't enter BL mode as expected and rc is
  9795. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  9796. * the function should return an error code to indicate this
  9797. * failure PT_ENTER_BL_CONFIRM_FAIL.
  9798. */
  9799. if (!rc)
  9800. rc = -EINVAL;
  9801. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  9802. mutex_lock(&cd->system_lock);
  9803. cd->mode = mode;
  9804. mutex_unlock(&cd->system_lock);
  9805. pt_debug(dev, DL_ERROR,
  9806. "%s ERROR: Not in BL as expected", __func__);
  9807. }
  9808. exit:
  9809. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  9810. /* Check to get (buffered) flash information */
  9811. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  9812. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  9813. read_buf, &actual_read_len);
  9814. if (!rc) {
  9815. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  9816. pt_debug(
  9817. dev, DL_WARN,
  9818. "%s Unavailable Manufacturer ID: 0x%02x\n",
  9819. __func__,
  9820. read_buf[PIP2_RESP_BODY_OFFSET]);
  9821. /*
  9822. * If the BL was unable to cache the correct
  9823. * values when entering the first time due to
  9824. * the Flash part not having been powered up
  9825. * long enough, re-enter the BL to trigger the
  9826. * BL to re-attempt to cache the values.
  9827. */
  9828. if (flash_info_retry-- > 0) {
  9829. mode = PT_MODE_UNKNOWN;
  9830. pt_debug(dev, DL_WARN,
  9831. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  9832. __func__, flash_info_retry);
  9833. goto _retry;
  9834. } else {
  9835. pt_debug(dev, DL_WARN,
  9836. "%s Manufacturer ID Unknown\n",
  9837. __func__);
  9838. tmp_result = PT_ENTER_BL_PASS;
  9839. }
  9840. }
  9841. } else {
  9842. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  9843. pt_debug(
  9844. dev, DL_ERROR,
  9845. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  9846. __func__);
  9847. }
  9848. }
  9849. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  9850. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  9851. "SUPPRESS");
  9852. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  9853. if (result)
  9854. *result = tmp_result;
  9855. return rc;
  9856. }
  9857. /*******************************************************************************
  9858. * FUNCTION: _pt_pip2_file_open
  9859. *
  9860. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  9861. *
  9862. * NOTE: The DUT must be in BL mode for this command to work
  9863. *
  9864. * RETURNS:
  9865. * <0 = Error
  9866. * >0 = file handle opened
  9867. *
  9868. * PARAMETERS:
  9869. * *dev - pointer to device structure
  9870. * file_no - PIP2 file number to open
  9871. ******************************************************************************/
  9872. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  9873. {
  9874. int ret = 0;
  9875. u16 status;
  9876. u16 actual_read_len;
  9877. u8 file_handle;
  9878. u8 data[2];
  9879. u8 read_buf[10];
  9880. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  9881. data[0] = file_no;
  9882. ret = _pt_request_pip2_send_cmd(dev,
  9883. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  9884. data, 1, read_buf, &actual_read_len);
  9885. if (ret) {
  9886. pt_debug(dev, DL_ERROR,
  9887. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  9888. __func__, file_no);
  9889. return -PIP2_RSP_ERR_NOT_OPEN;
  9890. }
  9891. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9892. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  9893. if (ret || ((status != 0x00) && (status != 0x03)) ||
  9894. (file_handle != file_no)) {
  9895. pt_debug(dev, DL_ERROR,
  9896. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  9897. __func__, status, file_handle);
  9898. return -status;
  9899. }
  9900. return file_handle;
  9901. }
  9902. /*******************************************************************************
  9903. * FUNCTION: _pt_pip2_file_close
  9904. *
  9905. * SUMMARY: Using the BL PIP2 commands close a file
  9906. *
  9907. * NOTE: The DUT must be in BL mode for this command to work
  9908. *
  9909. * RETURNS:
  9910. * <0 = Error
  9911. * >0 = file handle closed
  9912. *
  9913. * PARAMETERS:
  9914. * *dev - pointer to device structure
  9915. * file_handle - handle to the file to be closed
  9916. ******************************************************************************/
  9917. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  9918. {
  9919. int ret = 0;
  9920. u16 status;
  9921. u16 actual_read_len;
  9922. u8 data[2];
  9923. u8 read_buf[32];
  9924. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  9925. data[0] = file_handle;
  9926. ret = _pt_request_pip2_send_cmd(dev,
  9927. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  9928. data, 1, read_buf, &actual_read_len);
  9929. if (ret) {
  9930. pt_debug(dev, DL_ERROR,
  9931. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  9932. __func__, file_handle);
  9933. return -ETIME;
  9934. }
  9935. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9936. if (status != 0x00) {
  9937. pt_debug(dev, DL_ERROR,
  9938. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  9939. __func__, status, file_handle);
  9940. return -status;
  9941. }
  9942. return file_handle;
  9943. }
  9944. /*******************************************************************************
  9945. * FUNCTION: _pt_pip2_file_erase
  9946. *
  9947. * SUMMARY: Using the BL PIP2 commands erase a file
  9948. *
  9949. * NOTE: The DUT must be in BL mode for this command to work
  9950. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  9951. * one retry is attempted for each sector in a file.
  9952. *
  9953. * RETURNS:
  9954. * <0 = Error
  9955. * >0 = file handle closed
  9956. *
  9957. * PARAMETERS:
  9958. * *dev - pointer to device structure
  9959. * file_handle - handle to the file to be erased
  9960. * *status - PIP2 erase status code
  9961. ******************************************************************************/
  9962. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  9963. {
  9964. int ret = 0;
  9965. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  9966. int retry = 1;
  9967. u16 actual_read_len;
  9968. u8 data[2];
  9969. u8 read_buf[10];
  9970. struct pt_core_data *cd = dev_get_drvdata(dev);
  9971. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  9972. data[0] = file_handle;
  9973. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  9974. *status = PIP2_RSP_ERR_TIMEOUT;
  9975. /* Increase waiting time for large file erase */
  9976. mutex_lock(&cd->system_lock);
  9977. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  9978. mutex_unlock(&cd->system_lock);
  9979. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  9980. ret = _pt_request_pip2_send_cmd(dev,
  9981. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  9982. data, 2, read_buf, &actual_read_len);
  9983. if (ret)
  9984. break;
  9985. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9986. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  9987. #ifdef TTDL_DIAGNOSTICS
  9988. cd->file_erase_timeout_count++;
  9989. #endif
  9990. pt_debug(dev, DL_WARN,
  9991. "%s: ERASE timeout %d for file = %d\n",
  9992. __func__, retry, file_handle);
  9993. }
  9994. retry++;
  9995. }
  9996. mutex_lock(&cd->system_lock);
  9997. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  9998. mutex_unlock(&cd->system_lock);
  9999. if (ret) {
  10000. pt_debug(dev, DL_ERROR,
  10001. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10002. __func__, ret, file_handle);
  10003. return -EIO;
  10004. }
  10005. if (*status != 0x00) {
  10006. pt_debug(dev, DL_ERROR,
  10007. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10008. __func__, *status, file_handle);
  10009. return -EIO;
  10010. }
  10011. return file_handle;
  10012. }
  10013. /*******************************************************************************
  10014. * FUNCTION: _pt_pip2_file_read
  10015. *
  10016. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10017. *
  10018. * NOTE: The DUT must be in BL mode for this command to work
  10019. *
  10020. * RETURNS:
  10021. * <0 = Error
  10022. * >0 = number of bytes read
  10023. *
  10024. * PARAMETERS:
  10025. * *dev - pointer to device structure
  10026. * file_handle - File handle to read from
  10027. * num_bytes - number of bytes to read
  10028. ******************************************************************************/
  10029. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10030. u8 *read_buf)
  10031. {
  10032. int ret = 0;
  10033. u16 status;
  10034. u16 actual_read_len;
  10035. u8 data[3];
  10036. data[0] = file_handle;
  10037. data[1] = (num_bytes & 0x00FF);
  10038. data[2] = (num_bytes & 0xFF00) >> 8;
  10039. ret = _pt_request_pip2_send_cmd(dev,
  10040. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10041. data, 3, read_buf, &actual_read_len);
  10042. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10043. if (ret || ((status != 0x00) && (status != 0x03))) {
  10044. pt_debug(dev, DL_ERROR,
  10045. "%s File open failure with error code = %d\n",
  10046. __func__, status);
  10047. return -EPERM;
  10048. }
  10049. ret = num_bytes;
  10050. return ret;
  10051. }
  10052. /*******************************************************************************
  10053. * FUNCTION: _pt_read_us_file
  10054. *
  10055. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10056. * then read the entire file.
  10057. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10058. *
  10059. * RETURN:
  10060. * 0 = success
  10061. * !0 = failure
  10062. *
  10063. * PARAMETERS:
  10064. * *file_path - pointer to the file path
  10065. * *buf - pointer to the buffer to store the file contents
  10066. * *size - pointer to the size of the file
  10067. ******************************************************************************/
  10068. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10069. {
  10070. struct file *filp = NULL;
  10071. struct inode *inode = NULL;
  10072. unsigned int file_len = 0;
  10073. unsigned int read_len = 0;
  10074. mm_segment_t oldfs;
  10075. int rc = 0;
  10076. if (file_path == NULL || buf == NULL) {
  10077. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10078. return -EINVAL;
  10079. }
  10080. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10081. oldfs = get_fs();
  10082. set_fs(KERNEL_DS);
  10083. filp = filp_open(file_path, O_RDONLY, 0400);
  10084. if (IS_ERR(filp)) {
  10085. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10086. file_path);
  10087. rc = -ENOENT;
  10088. goto err;
  10089. }
  10090. if (filp->f_op == NULL) {
  10091. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10092. __func__);
  10093. rc = -EINVAL;
  10094. goto exit;
  10095. }
  10096. inode = filp->f_path.dentry->d_inode;
  10097. if (inode == NULL) {
  10098. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10099. __func__);
  10100. rc = -EINVAL;
  10101. goto exit;
  10102. }
  10103. file_len = i_size_read(inode->i_mapping->host);
  10104. if (file_len == 0) {
  10105. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10106. __func__, file_len);
  10107. rc = -EINVAL;
  10108. goto exit;
  10109. }
  10110. if (*size == 0)
  10111. read_len = file_len;
  10112. else
  10113. read_len = *size;
  10114. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10115. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10116. __func__, read_len);
  10117. rc = -EINVAL;
  10118. goto exit;
  10119. }
  10120. filp->private_data = inode->i_private;
  10121. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10122. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10123. rc = -EINVAL;
  10124. goto exit;
  10125. }
  10126. *size = read_len;
  10127. exit:
  10128. if (filp_close(filp, NULL) != 0)
  10129. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10130. err:
  10131. set_fs(oldfs);
  10132. return rc;
  10133. }
  10134. /*******************************************************************************
  10135. * FUNCTION: _pt_request_pip2_bin_hdr
  10136. *
  10137. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10138. * in the case of a flashless DUT, and parse the contents
  10139. *
  10140. * RETURNS:
  10141. * 0 = Success
  10142. * !0 = Error condition
  10143. *
  10144. * PARAMETERS:
  10145. * *dev - pointer to device structure
  10146. ******************************************************************************/
  10147. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10148. {
  10149. struct pt_core_data *cd = dev_get_drvdata(dev);
  10150. u8 file_handle;
  10151. u8 read_buf[255];
  10152. u8 hdr_len = 0;
  10153. u8 i;
  10154. int bytes_read;
  10155. int read_size;
  10156. int ret = 0;
  10157. int rc = 0;
  10158. bool load_hdr_struct = false;
  10159. if (cd->flashless_dut) {
  10160. read_size = sizeof(read_buf);
  10161. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10162. read_buf, &read_size);
  10163. if (rc) {
  10164. ret = rc;
  10165. pt_debug(dev, DL_ERROR,
  10166. "%s Failed to read fw image from US, rc=%d\n",
  10167. __func__, rc);
  10168. goto exit;
  10169. }
  10170. load_hdr_struct = true;
  10171. hdr_len = read_buf[0];
  10172. i = 0;
  10173. } else {
  10174. if (cd->mode != PT_MODE_BOOTLOADER) {
  10175. ret = -EPERM;
  10176. goto exit;
  10177. }
  10178. /* Open the bin file in Flash */
  10179. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10180. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10181. if (file_handle != PIP2_FW_FILE) {
  10182. ret = -ENOENT;
  10183. pt_debug(dev, DL_ERROR,
  10184. "%s Failed to open bin file\n", __func__);
  10185. goto exit;
  10186. }
  10187. /* Read the header length from the file */
  10188. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10189. read_size = 1;
  10190. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10191. read_buf);
  10192. if (bytes_read != read_size) {
  10193. ret = -EX_ERR_FREAD;
  10194. pt_debug(dev, DL_ERROR,
  10195. "%s Failed to bin file header len\n", __func__);
  10196. goto exit_close_file;
  10197. }
  10198. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10199. if (hdr_len == 0xFF) {
  10200. ret = -EX_ERR_FLEN;
  10201. pt_debug(dev, DL_ERROR,
  10202. "%s Bin header len is invalid\n", __func__);
  10203. goto exit_close_file;
  10204. }
  10205. /* Read the rest of the header from the bin file */
  10206. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10207. memset(read_buf, 0, sizeof(read_buf));
  10208. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10209. read_buf);
  10210. if (bytes_read != hdr_len) {
  10211. ret = -EX_ERR_FREAD;
  10212. pt_debug(dev, DL_ERROR,
  10213. "%s Failed to read bin file\n", __func__);
  10214. goto exit_close_file;
  10215. }
  10216. load_hdr_struct = true;
  10217. exit_close_file:
  10218. /* Close the file */
  10219. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10220. ret = -EX_ERR_FCLOSE;
  10221. pt_debug(dev, DL_ERROR,
  10222. "%s Failed to close bin file\n", __func__);
  10223. }
  10224. /*
  10225. * The length was already read so subtract 1 to make the rest of
  10226. * the offsets match the spec
  10227. */
  10228. i = PIP2_RESP_BODY_OFFSET - 1;
  10229. }
  10230. if (load_hdr_struct) {
  10231. hdr->length = hdr_len;
  10232. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10233. hdr->fw_major = (read_buf[i+3]);
  10234. hdr->fw_minor = (read_buf[i+4]);
  10235. hdr->fw_crc = (read_buf[i+5] << 24) |
  10236. (read_buf[i+6] << 16) |
  10237. (read_buf[i+7] << 8) |
  10238. (read_buf[i+8]);
  10239. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10240. (read_buf[i+10] << 16) |
  10241. (read_buf[i+11] << 8) |
  10242. (read_buf[i+12]);
  10243. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10244. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10245. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10246. if (hdr_len >= 22) {
  10247. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10248. (read_buf[i+20] << 16) |
  10249. (read_buf[i+21] << 8) |
  10250. (read_buf[i+22]);
  10251. } else {
  10252. hdr->hex_file_size = 0;
  10253. }
  10254. }
  10255. exit:
  10256. return ret;
  10257. }
  10258. /*******************************************************************************
  10259. * FUNCTION: _pt_pip2_file_get_stats
  10260. *
  10261. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10262. * opened file
  10263. *
  10264. * NOTE: The DUT must be in BL mode for this command to work
  10265. *
  10266. * RETURNS:
  10267. * !0 = Error
  10268. * 0 = Success
  10269. *
  10270. * PARAMETERS:
  10271. * *dev - pointer to device structure
  10272. * file_handle - File handle to read from
  10273. * *address - pointer to store address of file
  10274. * *file_size _ pointer to store size of file
  10275. ******************************************************************************/
  10276. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10277. u32 *file_size)
  10278. {
  10279. int ret = 1;
  10280. u16 status;
  10281. u16 actual_read_len;
  10282. u8 data[2];
  10283. u8 read_buf[16];
  10284. data[0] = file_handle;
  10285. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10286. ret = _pt_request_pip2_send_cmd(dev,
  10287. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10288. data, 2, read_buf, &actual_read_len);
  10289. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10290. if (ret || (status != 0x00)) {
  10291. pt_debug(dev, DL_ERROR,
  10292. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10293. __func__, status, file_handle, ret);
  10294. ret = -EIO;
  10295. goto exit;
  10296. }
  10297. pt_debug(dev, DL_DEBUG,
  10298. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10299. if (address) {
  10300. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10301. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10302. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10303. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10304. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10305. __func__, *address);
  10306. }
  10307. if (file_size) {
  10308. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10309. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10310. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10311. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10312. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10313. __func__, *file_size);
  10314. }
  10315. exit:
  10316. return ret;
  10317. }
  10318. /*******************************************************************************
  10319. * FUNCTION: _pt_pip2_file_seek_offset
  10320. *
  10321. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10322. * opened file
  10323. *
  10324. * NOTE: The DUT must be in BL mode for this command to work
  10325. * NOTE: File open/erase command can reset the offset
  10326. *
  10327. * RETURNS:
  10328. * !0 = Error
  10329. * 0 = Success
  10330. *
  10331. * PARAMETERS:
  10332. * *dev - pointer to device structure
  10333. * file_handle - File handle to read from
  10334. * read_offset - read offset of file
  10335. * write_offset - write offset of file
  10336. ******************************************************************************/
  10337. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10338. u32 read_offset, u32 write_offset)
  10339. {
  10340. int ret = 1;
  10341. u16 status;
  10342. u16 actual_read_len;
  10343. u8 data[10];
  10344. u8 read_buf[16];
  10345. data[0] = file_handle;
  10346. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10347. data[2] = 0xff & read_offset;
  10348. data[3] = 0xff & (read_offset >> 8);
  10349. data[4] = 0xff & (read_offset >> 16);
  10350. data[5] = 0xff & (read_offset >> 24);
  10351. data[6] = 0xff & write_offset;
  10352. data[7] = 0xff & (write_offset >> 8);
  10353. data[8] = 0xff & (write_offset >> 16);
  10354. data[9] = 0xff & (write_offset >> 24);
  10355. ret = _pt_request_pip2_send_cmd(dev,
  10356. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10357. data, 10, read_buf, &actual_read_len);
  10358. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10359. if (ret || (status != 0x00)) {
  10360. pt_debug(dev, DL_ERROR,
  10361. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10362. __func__, status, ret, file_handle);
  10363. ret = -EIO;
  10364. }
  10365. return ret;
  10366. }
  10367. /*******************************************************************************
  10368. * FUNCTION: _pt_pip2_file_crc
  10369. *
  10370. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10371. * the file.
  10372. *
  10373. * NOTE: The DUT must be in BL mode for this command to work
  10374. * NOTE: This command only can be used for BL version 1.8 or greater.
  10375. * BL version 1.8 added this change according to PGV-173.
  10376. *
  10377. * RETURNS:
  10378. * !0 = Error
  10379. * 0 = Success
  10380. *
  10381. * PARAMETERS:
  10382. * *dev - pointer to device structure
  10383. * file_handle - File handle to read from
  10384. * offset - start offset for CRC calculation
  10385. * length - number of bytes to calculate CRC over
  10386. * read_buf - pointer to the read buffer
  10387. ******************************************************************************/
  10388. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10389. u32 offset, u32 length, u8 *read_buf)
  10390. {
  10391. int rc = 1;
  10392. u16 actual_read_len;
  10393. u8 data[10];
  10394. data[0] = file_handle;
  10395. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10396. data[2] = 0xff & offset;
  10397. data[3] = 0xff & (offset >> 8);
  10398. data[4] = 0xff & (offset >> 16);
  10399. data[5] = 0xff & (offset >> 24);
  10400. data[6] = 0xff & length;
  10401. data[7] = 0xff & (length >> 8);
  10402. data[8] = 0xff & (length >> 16);
  10403. data[9] = 0xff & (length >> 24);
  10404. rc = _pt_request_pip2_send_cmd(dev,
  10405. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10406. data, 10, read_buf, &actual_read_len);
  10407. if (rc)
  10408. pt_debug(dev, DL_ERROR,
  10409. "%s Return FILE_CRC failure, rc = %d\n",
  10410. __func__, rc);
  10411. return rc;
  10412. }
  10413. /*******************************************************************************
  10414. * FUNCTION: pt_pip2_ping_test
  10415. *
  10416. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10417. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10418. * response payload matches what was sent.
  10419. * The max payload size is 247:
  10420. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10421. *
  10422. * RETURN:
  10423. * 0 = success
  10424. * !0 = failure
  10425. *
  10426. * PARAMETERS:
  10427. * *dev - pointer to device structure
  10428. * *attr - pointer to device attributes
  10429. * *buf - pointer to output buffer
  10430. ******************************************************************************/
  10431. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10432. {
  10433. u16 actual_read_len;
  10434. u8 *read_buf = NULL;
  10435. u8 *data = NULL;
  10436. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10437. int i = 1;
  10438. int j = 0;
  10439. int rc = 0;
  10440. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10441. if (!read_buf)
  10442. goto ping_test_exit;
  10443. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10444. if (!data)
  10445. goto ping_test_exit;
  10446. /* Load data payload with an array of walking 1's */
  10447. for (i = 0; i < 255; i++)
  10448. data[i] = 0x01 << (i % 8);
  10449. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10450. for (i = 0; i <= max_bytes; i++) {
  10451. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10452. PIP2_CMD_ID_PING, data, i, read_buf,
  10453. &actual_read_len);
  10454. if (rc) {
  10455. pt_debug(dev, DL_ERROR,
  10456. "%s: PING failed with %d byte payload\n",
  10457. __func__, i);
  10458. break;
  10459. }
  10460. /* Verify data returned matches data sent */
  10461. for (j = 0; j < i; j++) {
  10462. if (read_buf[data_offset + j] != data[j]) {
  10463. pt_debug(dev, DL_DEBUG,
  10464. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10465. __func__, i, j, data[j], j,
  10466. read_buf[data_offset + j]);
  10467. goto ping_test_exit;
  10468. }
  10469. }
  10470. }
  10471. ping_test_exit:
  10472. *last_packet_size = i - 1;
  10473. kfree(read_buf);
  10474. kfree(data);
  10475. return rc;
  10476. }
  10477. /*******************************************************************************
  10478. * FUNCTION: _pt_ic_parse_input_hex
  10479. *
  10480. * SUMMARY: Parse a char data array as space delimited hex values into
  10481. * an int array.
  10482. *
  10483. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10484. * buffer to store data is "u32". This function is still needed by the
  10485. * "command" sysfs node because the buffer type to store data is "u8".
  10486. *
  10487. * RETURN: Length of parsed data
  10488. *
  10489. * PARAMETERS:
  10490. * *dev - pointer to device structure
  10491. * *buf - pointer to buffer that holds the input array to parse
  10492. * buf_size - size of buf
  10493. * *ic_buf - pointer to array to store parsed data
  10494. * ic_buf_size - max size of ic_buf
  10495. ******************************************************************************/
  10496. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10497. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10498. {
  10499. const char *pbuf = buf;
  10500. unsigned long value;
  10501. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10502. u32 i = 0;
  10503. u32 j;
  10504. int last = 0;
  10505. int ret;
  10506. pt_debug(dev, DL_DEBUG,
  10507. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10508. __func__, pbuf, buf, buf_size, "scan buf size",
  10509. PT_MAX_PIP2_MSG_SIZE, buf);
  10510. while (pbuf <= (buf + buf_size)) {
  10511. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10512. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10513. __func__, "Max cmd size exceeded", i,
  10514. PT_MAX_PIP2_MSG_SIZE);
  10515. return -EINVAL;
  10516. }
  10517. if (i >= ic_buf_size) {
  10518. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10519. __func__, "Buffer size exceeded", i,
  10520. ic_buf_size);
  10521. return -EINVAL;
  10522. }
  10523. while (((*pbuf == ' ') || (*pbuf == ','))
  10524. && (pbuf < (buf + buf_size))) {
  10525. last = *pbuf;
  10526. pbuf++;
  10527. }
  10528. if (pbuf >= (buf + buf_size))
  10529. break;
  10530. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10531. if ((last == ',') && (*pbuf == ',')) {
  10532. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10533. __func__, "Invalid data format.");
  10534. return -EINVAL;
  10535. }
  10536. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10537. && (pbuf < (buf + buf_size))
  10538. && (*pbuf != ' ')
  10539. && (*pbuf != ','); j++) {
  10540. last = *pbuf;
  10541. scan_buf[j] = *pbuf++;
  10542. }
  10543. ret = kstrtoul(scan_buf, 16, &value);
  10544. if (ret < 0) {
  10545. pt_debug(dev, DL_ERROR,
  10546. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10547. "Invalid data format. ", scan_buf,
  10548. "Use \"0xHH,...,0xHH\"", " instead.",
  10549. i, ret);
  10550. return ret;
  10551. }
  10552. ic_buf[i] = value;
  10553. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10554. __func__, i, value);
  10555. i++;
  10556. }
  10557. return i;
  10558. }
  10559. /*******************************************************************************
  10560. * FUNCTION: _pt_ic_parse_input
  10561. *
  10562. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10563. * hex values or dec values into an int array with the following rules:
  10564. * 1) Hex values must have a "0x" prefix for each element or the first element
  10565. * only
  10566. * 2) Dec values do not have any prefix
  10567. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10568. * string
  10569. *
  10570. * RETURN: Number of values parsed
  10571. *
  10572. * PARAMETERS:
  10573. * *dev - pointer to device structure
  10574. * *buf - pointer to buffer that holds the input array to parse
  10575. * buf_size - size of buf
  10576. * *out_buf - pointer to array to store parsed data
  10577. * out_buf_size - max size of buffer to be stored
  10578. ******************************************************************************/
  10579. static int _pt_ic_parse_input(struct device *dev,
  10580. const char *buf, size_t buf_size,
  10581. u32 *out_buf, size_t out_buf_size)
  10582. {
  10583. const char *pbuf = buf;
  10584. unsigned long value;
  10585. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10586. u32 i = 0;
  10587. u32 j;
  10588. int last = 0;
  10589. int ret = 0;
  10590. u8 str_base = 0;
  10591. pt_debug(dev, DL_DEBUG,
  10592. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10593. __func__, buf_size, out_buf_size, "scan buf size",
  10594. PT_MAX_PIP2_MSG_SIZE, buf);
  10595. while (pbuf <= (buf + buf_size)) {
  10596. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10597. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10598. __func__, "Max cmd size exceeded", i,
  10599. PT_MAX_PIP2_MSG_SIZE);
  10600. ret = -EINVAL;
  10601. goto error;
  10602. }
  10603. if (i >= out_buf_size) {
  10604. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10605. __func__, "Buffer size exceeded", i,
  10606. out_buf_size);
  10607. ret = -EINVAL;
  10608. goto error;
  10609. }
  10610. while (((*pbuf == ' ') || (*pbuf == ','))
  10611. && (pbuf < (buf + buf_size))) {
  10612. last = *pbuf;
  10613. pbuf++;
  10614. }
  10615. if (pbuf >= (buf + buf_size))
  10616. break;
  10617. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10618. if ((last == ',') && (*pbuf == ',')) {
  10619. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10620. __func__, "Invalid data format.");
  10621. ret = -EINVAL;
  10622. goto error;
  10623. }
  10624. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10625. && (pbuf < (buf + buf_size))
  10626. && (*pbuf != ' ')
  10627. && (*pbuf != ','); j++) {
  10628. last = *pbuf;
  10629. scan_buf[j] = *pbuf++;
  10630. }
  10631. if (i == 0) {
  10632. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10633. (strncmp(scan_buf, "0X", 2) == 0))
  10634. str_base = 16;
  10635. else
  10636. str_base = 10;
  10637. } else {
  10638. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10639. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10640. (str_base == 10)) {
  10641. pt_debug(dev, DL_ERROR,
  10642. "%s: Decimal and Heximal data mixed\n",
  10643. __func__);
  10644. ret = -EINVAL;
  10645. goto error;
  10646. }
  10647. }
  10648. ret = kstrtoul(scan_buf, str_base, &value);
  10649. if (ret < 0) {
  10650. pt_debug(dev, DL_ERROR,
  10651. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10652. "Invalid data format. ", scan_buf,
  10653. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10654. " instead.", i, ret);
  10655. goto error;
  10656. }
  10657. out_buf[i] = value;
  10658. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10659. __func__, i, value, value);
  10660. i++;
  10661. }
  10662. ret = i;
  10663. error:
  10664. return ret;
  10665. }
  10666. #ifdef TTHE_TUNER_SUPPORT
  10667. /*******************************************************************************
  10668. * FUNCTION: tthe_debugfs_open
  10669. *
  10670. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10671. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10672. * printing a large number of fingers and sensor data without overflowing
  10673. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10674. * even multiple of PAGE_SIZE
  10675. *
  10676. * RETURN:
  10677. * 0 = success
  10678. * !0 = failure
  10679. *
  10680. * PARAMETERS:
  10681. * *inode - file inode number
  10682. * *filp - file pointer to debugfs file
  10683. ******************************************************************************/
  10684. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10685. {
  10686. struct pt_core_data *cd = inode->i_private;
  10687. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10688. filp->private_data = inode->i_private;
  10689. if (cd->tthe_buf)
  10690. return -EBUSY;
  10691. while (buf_size < 4096)
  10692. buf_size = buf_size << 1;
  10693. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10694. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10695. cd->tthe_buf_size = buf_size;
  10696. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10697. if (!cd->tthe_buf)
  10698. return -ENOMEM;
  10699. return 0;
  10700. }
  10701. /*******************************************************************************
  10702. * FUNCTION: tthe_debugfs_close
  10703. *
  10704. * SUMMARY: Close method for tthe_tuner debugfs node.
  10705. *
  10706. * RETURN:
  10707. * 0 = success
  10708. * !0 = failure
  10709. *
  10710. * PARAMETERS:
  10711. * *inode - file inode number
  10712. * *filp - file pointer to debugfs file
  10713. ******************************************************************************/
  10714. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10715. {
  10716. struct pt_core_data *cd = filp->private_data;
  10717. filp->private_data = NULL;
  10718. kfree(cd->tthe_buf);
  10719. cd->tthe_buf = NULL;
  10720. return 0;
  10721. }
  10722. /*******************************************************************************
  10723. * FUNCTION: tthe_debugfs_read
  10724. *
  10725. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  10726. * tthe_buf to user buffer.
  10727. *
  10728. * RETURN: Size of debugfs data print
  10729. *
  10730. * PARAMETERS:
  10731. * *filp - file pointer to debugfs file
  10732. * *buf - the user space buffer to read to
  10733. * count - the maximum number of bytes to read
  10734. * *ppos - the current position in the buffer
  10735. ******************************************************************************/
  10736. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  10737. size_t count, loff_t *ppos)
  10738. {
  10739. struct pt_core_data *cd = filp->private_data;
  10740. int size;
  10741. int ret;
  10742. static int partial_read;
  10743. wait_event_interruptible(cd->wait_q,
  10744. cd->tthe_buf_len != 0 || cd->tthe_exit);
  10745. mutex_lock(&cd->tthe_lock);
  10746. if (cd->tthe_exit) {
  10747. mutex_unlock(&cd->tthe_lock);
  10748. return 0;
  10749. }
  10750. if (count > cd->tthe_buf_len)
  10751. size = cd->tthe_buf_len;
  10752. else
  10753. size = count;
  10754. if (!size) {
  10755. mutex_unlock(&cd->tthe_lock);
  10756. return 0;
  10757. }
  10758. if (partial_read) {
  10759. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  10760. partial_read = 0;
  10761. } else {
  10762. ret = copy_to_user(buf, cd->tthe_buf, size);
  10763. }
  10764. if (size == count)
  10765. partial_read = count;
  10766. if (ret == size)
  10767. return -EFAULT;
  10768. size -= ret;
  10769. cd->tthe_buf_len -= size;
  10770. mutex_unlock(&cd->tthe_lock);
  10771. *ppos += size;
  10772. return size;
  10773. }
  10774. static const struct file_operations tthe_debugfs_fops = {
  10775. .open = tthe_debugfs_open,
  10776. .release = tthe_debugfs_close,
  10777. .read = tthe_debugfs_read,
  10778. };
  10779. #endif
  10780. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  10781. .start_bl = _pt_request_pip_start_bl,
  10782. .suspend_scanning = _pt_request_pip_suspend_scanning,
  10783. .resume_scanning = _pt_request_pip_resume_scanning,
  10784. .get_param = _pt_request_pip_get_param,
  10785. .set_param = _pt_request_pip_set_param,
  10786. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  10787. .get_config_row_size = _pt_request_pip_get_config_row_size,
  10788. .get_data_structure = _pt_request_pip_get_data_structure,
  10789. .run_selftest = _pt_request_pip_run_selftest,
  10790. .get_selftest_result = _pt_request_pip_get_selftest_result,
  10791. .load_self_test_param = _pt_request_pip_load_self_test_param,
  10792. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  10793. .calibrate_ext = _pt_request_pip_calibrate_ext,
  10794. .initialize_baselines = _pt_request_pip_initialize_baselines,
  10795. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  10796. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  10797. .read_data_block = _pt_request_pip_read_data_block,
  10798. .write_data_block = _pt_request_pip_write_data_block,
  10799. .user_cmd = _pt_request_pip_user_cmd,
  10800. .get_bl_info = _pt_request_pip_bl_get_information,
  10801. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  10802. .launch_app = _pt_request_pip_launch_app,
  10803. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  10804. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  10805. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  10806. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  10807. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  10808. .pip2_file_open = _pt_pip2_file_open,
  10809. .pip2_file_close = _pt_pip2_file_close,
  10810. .pip2_file_erase = _pt_pip2_file_erase,
  10811. .read_us_file = _pt_read_us_file,
  10812. .manage_cal_data = _pt_manage_local_cal_data,
  10813. .calc_crc = crc_ccitt_calculate,
  10814. #ifdef TTDL_DIAGNOSTICS
  10815. .pip2_file_read = _pt_pip2_file_read,
  10816. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  10817. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  10818. .pip2_file_crc = _pt_pip2_file_crc,
  10819. #endif
  10820. };
  10821. static struct pt_core_commands _pt_core_commands = {
  10822. .subscribe_attention = _pt_subscribe_attention,
  10823. .unsubscribe_attention = _pt_unsubscribe_attention,
  10824. .request_exclusive = _pt_request_exclusive,
  10825. .release_exclusive = _pt_release_exclusive,
  10826. .request_reset = _pt_request_reset,
  10827. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  10828. .request_enum = _pt_request_enum,
  10829. .request_sysinfo = _pt_request_sysinfo,
  10830. .request_loader_pdata = _pt_request_loader_pdata,
  10831. .request_stop_wd = _pt_request_stop_wd,
  10832. .request_start_wd = _pt_request_start_wd,
  10833. .request_get_mode = _pt_request_get_mode,
  10834. .request_active_pip_prot = _pt_request_active_pip_protocol,
  10835. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  10836. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  10837. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  10838. .request_dut_generation = _pt_request_dut_generation,
  10839. .request_hw_version = _pt_request_hw_version,
  10840. .parse_sysfs_input = _pt_ic_parse_input,
  10841. #ifdef TTHE_TUNER_SUPPORT
  10842. .request_tthe_print = _pt_request_tthe_print,
  10843. #endif
  10844. #ifdef TTDL_DIAGNOSTICS
  10845. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  10846. #endif
  10847. .nonhid_cmd = &_pt_core_nonhid_cmd,
  10848. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  10849. };
  10850. struct pt_core_commands *pt_get_commands(void)
  10851. {
  10852. return &_pt_core_commands;
  10853. }
  10854. EXPORT_SYMBOL_GPL(pt_get_commands);
  10855. static DEFINE_MUTEX(core_list_lock);
  10856. static LIST_HEAD(core_list);
  10857. static DEFINE_MUTEX(module_list_lock);
  10858. static LIST_HEAD(module_list);
  10859. static int core_number;
  10860. /*******************************************************************************
  10861. * FUNCTION: pt_probe_module
  10862. *
  10863. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  10864. *
  10865. * RETURN:
  10866. * 0 = success
  10867. * !0 = failure
  10868. *
  10869. * PARAMETERS:
  10870. * *cd - pointer to core data
  10871. * *module - pointer to module structure
  10872. ******************************************************************************/
  10873. static int pt_probe_module(struct pt_core_data *cd,
  10874. struct pt_module *module)
  10875. {
  10876. struct module_node *module_node;
  10877. int rc = 0;
  10878. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  10879. if (!module_node)
  10880. return -ENOMEM;
  10881. module_node->module = module;
  10882. mutex_lock(&cd->module_list_lock);
  10883. list_add(&module_node->node, &cd->module_list);
  10884. mutex_unlock(&cd->module_list_lock);
  10885. rc = module->probe(cd->dev, &module_node->data);
  10886. if (rc) {
  10887. /*
  10888. * Remove from the list when probe fails
  10889. * in order not to call release
  10890. */
  10891. mutex_lock(&cd->module_list_lock);
  10892. list_del(&module_node->node);
  10893. mutex_unlock(&cd->module_list_lock);
  10894. kfree(module_node);
  10895. goto exit;
  10896. }
  10897. exit:
  10898. return rc;
  10899. }
  10900. /*******************************************************************************
  10901. * FUNCTION: pt_release_module
  10902. *
  10903. * SUMMARY: Call the release pointer and remove the module pointer from
  10904. * module_list.
  10905. *
  10906. * PARAMETERS:
  10907. * *cd - pointer to core data
  10908. * *module - pointer to module structure
  10909. ******************************************************************************/
  10910. static void pt_release_module(struct pt_core_data *cd,
  10911. struct pt_module *module)
  10912. {
  10913. struct module_node *m, *m_n;
  10914. mutex_lock(&cd->module_list_lock);
  10915. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  10916. if (m->module == module) {
  10917. module->release(cd->dev, m->data);
  10918. list_del(&m->node);
  10919. kfree(m);
  10920. break;
  10921. }
  10922. mutex_unlock(&cd->module_list_lock);
  10923. }
  10924. /*******************************************************************************
  10925. * FUNCTION: pt_probe_modules
  10926. *
  10927. * SUMMARY: Iterate module_list and probe each module.
  10928. *
  10929. * PARAMETERS:
  10930. * *cd - pointer to core data
  10931. ******************************************************************************/
  10932. static void pt_probe_modules(struct pt_core_data *cd)
  10933. {
  10934. struct pt_module *m;
  10935. int rc = 0;
  10936. mutex_lock(&module_list_lock);
  10937. list_for_each_entry(m, &module_list, node) {
  10938. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  10939. __func__, m->name);
  10940. rc = pt_probe_module(cd, m);
  10941. if (rc)
  10942. pt_debug(cd->dev, DL_ERROR,
  10943. "%s: Probe fails for module %s\n",
  10944. __func__, m->name);
  10945. }
  10946. mutex_unlock(&module_list_lock);
  10947. }
  10948. /*******************************************************************************
  10949. * FUNCTION: pt_release_modules
  10950. *
  10951. * SUMMARY: Iterate module_list and remove each module.
  10952. *
  10953. * PARAMETERS:
  10954. * *cd - pointer to core data
  10955. ******************************************************************************/
  10956. static void pt_release_modules(struct pt_core_data *cd)
  10957. {
  10958. struct pt_module *m;
  10959. mutex_lock(&module_list_lock);
  10960. list_for_each_entry(m, &module_list, node)
  10961. pt_release_module(cd, m);
  10962. mutex_unlock(&module_list_lock);
  10963. }
  10964. /*******************************************************************************
  10965. * FUNCTION: pt_get_core_data
  10966. *
  10967. * SUMMARY: Iterate core_list and get core data.
  10968. *
  10969. * RETURN:
  10970. * pointer to core data or null pointer if fail
  10971. *
  10972. * PARAMETERS:
  10973. * *id - pointer to core id
  10974. ******************************************************************************/
  10975. struct pt_core_data *pt_get_core_data(char *id)
  10976. {
  10977. struct pt_core_data *d;
  10978. list_for_each_entry(d, &core_list, node)
  10979. if (!strncmp(d->core_id, id, 20))
  10980. return d;
  10981. return NULL;
  10982. }
  10983. EXPORT_SYMBOL_GPL(pt_get_core_data);
  10984. /*******************************************************************************
  10985. * FUNCTION: pt_add_core
  10986. *
  10987. * SUMMARY: Add core data to the core_list.
  10988. *
  10989. * PARAMETERS:
  10990. * *dev - pointer to device structure
  10991. ******************************************************************************/
  10992. static void pt_add_core(struct device *dev)
  10993. {
  10994. struct pt_core_data *d;
  10995. struct pt_core_data *cd = dev_get_drvdata(dev);
  10996. mutex_lock(&core_list_lock);
  10997. list_for_each_entry(d, &core_list, node)
  10998. if (d->dev == dev)
  10999. goto unlock;
  11000. list_add(&cd->node, &core_list);
  11001. unlock:
  11002. mutex_unlock(&core_list_lock);
  11003. }
  11004. /*******************************************************************************
  11005. * FUNCTION: pt_del_core
  11006. *
  11007. * SUMMARY: Remove core data from the core_list.
  11008. *
  11009. * PARAMETERS:
  11010. * *dev - pointer to device structure
  11011. ******************************************************************************/
  11012. static void pt_del_core(struct device *dev)
  11013. {
  11014. struct pt_core_data *d, *d_n;
  11015. mutex_lock(&core_list_lock);
  11016. list_for_each_entry_safe(d, d_n, &core_list, node)
  11017. if (d->dev == dev) {
  11018. list_del(&d->node);
  11019. goto unlock;
  11020. }
  11021. unlock:
  11022. mutex_unlock(&core_list_lock);
  11023. }
  11024. /*******************************************************************************
  11025. * FUNCTION: pt_register_module
  11026. *
  11027. * SUMMARY: Register the module to module_list and probe the module for each
  11028. * core.
  11029. *
  11030. * RETURN:
  11031. * 0 = success
  11032. * !0 = failure
  11033. *
  11034. * PARAMETERS:
  11035. * *module - pointer to module structure
  11036. ******************************************************************************/
  11037. int pt_register_module(struct pt_module *module)
  11038. {
  11039. struct pt_module *m;
  11040. struct pt_core_data *cd;
  11041. int rc = 0;
  11042. if (!module || !module->probe || !module->release)
  11043. return -EINVAL;
  11044. mutex_lock(&module_list_lock);
  11045. list_for_each_entry(m, &module_list, node)
  11046. if (m == module) {
  11047. rc = -EEXIST;
  11048. goto unlock;
  11049. }
  11050. list_add(&module->node, &module_list);
  11051. /* Probe the module for each core */
  11052. mutex_lock(&core_list_lock);
  11053. list_for_each_entry(cd, &core_list, node)
  11054. pt_probe_module(cd, module);
  11055. mutex_unlock(&core_list_lock);
  11056. unlock:
  11057. mutex_unlock(&module_list_lock);
  11058. return rc;
  11059. }
  11060. EXPORT_SYMBOL_GPL(pt_register_module);
  11061. /*******************************************************************************
  11062. * FUNCTION: pt_unregister_module
  11063. *
  11064. * SUMMARY: Release the module for each core and remove the module from
  11065. * module_list.
  11066. *
  11067. * RETURN:
  11068. * 0 = success
  11069. * !0 = failure
  11070. *
  11071. * PARAMETERS:
  11072. * *module - pointer to module structure
  11073. ******************************************************************************/
  11074. void pt_unregister_module(struct pt_module *module)
  11075. {
  11076. struct pt_module *m, *m_n;
  11077. struct pt_core_data *cd;
  11078. if (!module)
  11079. return;
  11080. mutex_lock(&module_list_lock);
  11081. /* Release the module for each core */
  11082. mutex_lock(&core_list_lock);
  11083. list_for_each_entry(cd, &core_list, node)
  11084. pt_release_module(cd, module);
  11085. mutex_unlock(&core_list_lock);
  11086. list_for_each_entry_safe(m, m_n, &module_list, node)
  11087. if (m == module) {
  11088. list_del(&m->node);
  11089. break;
  11090. }
  11091. mutex_unlock(&module_list_lock);
  11092. }
  11093. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11094. /*******************************************************************************
  11095. * FUNCTION: pt_get_module_data
  11096. *
  11097. * SUMMARY: Get module data from module_node by module_list.
  11098. *
  11099. * RETURN:
  11100. * pointer to module data
  11101. *
  11102. * PARAMETERS:
  11103. * *dev - pointer to device structure
  11104. * *module - pointer to module structure
  11105. ******************************************************************************/
  11106. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11107. {
  11108. struct pt_core_data *cd = dev_get_drvdata(dev);
  11109. struct module_node *m;
  11110. void *data = NULL;
  11111. mutex_lock(&cd->module_list_lock);
  11112. list_for_each_entry(m, &cd->module_list, node)
  11113. if (m->module == module) {
  11114. data = m->data;
  11115. break;
  11116. }
  11117. mutex_unlock(&cd->module_list_lock);
  11118. return data;
  11119. }
  11120. EXPORT_SYMBOL(pt_get_module_data);
  11121. #ifdef CONFIG_HAS_EARLYSUSPEND
  11122. /*******************************************************************************
  11123. * FUNCTION: pt_early_suspend
  11124. *
  11125. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11126. * attention list.
  11127. *
  11128. * PARAMETERS:
  11129. * *h - pointer to early_suspend structure
  11130. ******************************************************************************/
  11131. static void pt_early_suspend(struct early_suspend *h)
  11132. {
  11133. struct pt_core_data *cd =
  11134. container_of(h, struct pt_core_data, es);
  11135. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11136. }
  11137. /*******************************************************************************
  11138. * FUNCTION: pt_late_resume
  11139. *
  11140. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11141. * attention list.
  11142. *
  11143. * PARAMETERS:
  11144. * *h - pointer to early_suspend structure
  11145. ******************************************************************************/
  11146. static void pt_late_resume(struct early_suspend *h)
  11147. {
  11148. struct pt_core_data *cd =
  11149. container_of(h, struct pt_core_data, es);
  11150. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11151. }
  11152. /*******************************************************************************
  11153. * FUNCTION: pt_setup_early_suspend
  11154. *
  11155. * SUMMARY: Register early/suspend function to the system.
  11156. *
  11157. * PARAMETERS:
  11158. * *cd - pointer to core data
  11159. ******************************************************************************/
  11160. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11161. {
  11162. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11163. cd->es.suspend = pt_early_suspend;
  11164. cd->es.resume = pt_late_resume;
  11165. register_early_suspend(&cd->es);
  11166. }
  11167. #elif defined(CONFIG_DRM)
  11168. /*******************************************************************************
  11169. * FUNCTION: drm_notifier_callback
  11170. *
  11171. * SUMMARY: Call back function for DRM notifier to allow to call
  11172. * resume/suspend attention list.
  11173. *
  11174. * RETURN:
  11175. * 0 = success
  11176. *
  11177. * PARAMETERS:
  11178. * *self - pointer to notifier_block structure
  11179. * event - event type of fb notifier
  11180. * *data - pointer to fb_event structure
  11181. ******************************************************************************/
  11182. static int drm_notifier_callback(struct notifier_block *self,
  11183. unsigned long event, void *data)
  11184. {
  11185. struct pt_core_data *cd =
  11186. container_of(self, struct pt_core_data, fb_notifier);
  11187. struct drm_panel_notifier *evdata = data;
  11188. int *blank;
  11189. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11190. if (!evdata)
  11191. goto exit;
  11192. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11193. event == DRM_PANEL_EVENT_BLANK)) {
  11194. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11195. __func__, event);
  11196. goto exit;
  11197. }
  11198. blank = evdata->data;
  11199. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d", __func__, event, *blank);
  11200. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11201. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11202. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11203. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11204. __func__, event);
  11205. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11206. if (cd->fb_state != FB_ON) {
  11207. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11208. #if defined(CONFIG_PM_SLEEP)
  11209. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11210. __func__);
  11211. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11212. pt_core_resume_(cd->dev);
  11213. #endif
  11214. cd->fb_state = FB_ON;
  11215. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11216. }
  11217. }
  11218. } else if (*blank == DRM_PANEL_BLANK_LP) {
  11219. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11220. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11221. if (cd->fb_state != FB_OFF) {
  11222. #if defined(CONFIG_PM_SLEEP)
  11223. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11224. __func__);
  11225. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11226. pt_core_suspend_(cd->dev);
  11227. #endif
  11228. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11229. cd->fb_state = FB_OFF;
  11230. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11231. }
  11232. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11233. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11234. __func__, event);
  11235. }
  11236. } else {
  11237. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11238. __func__, *blank);
  11239. }
  11240. exit:
  11241. return 0;
  11242. }
  11243. /*******************************************************************************
  11244. * FUNCTION: pt_setup_drm_notifier
  11245. *
  11246. * SUMMARY: Set up call back function into drm notifier.
  11247. *
  11248. * PARAMETERS:
  11249. * *cd - pointer to core data
  11250. ******************************************************************************/
  11251. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11252. {
  11253. cd->fb_state = FB_ON;
  11254. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11255. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11256. if (!active_panel)
  11257. pt_debug(cd->dev, DL_ERROR,
  11258. "%s: Active panel not registered!\n", __func__);
  11259. if (active_panel &&
  11260. drm_panel_notifier_register(active_panel,
  11261. &cd->fb_notifier) < 0)
  11262. pt_debug(cd->dev, DL_ERROR,
  11263. "%s: Register notifier failed!\n", __func__);
  11264. }
  11265. #elif defined(CONFIG_FB)
  11266. /*******************************************************************************
  11267. * FUNCTION: fb_notifier_callback
  11268. *
  11269. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11270. * resume/suspend attention list.
  11271. *
  11272. * RETURN:
  11273. * 0 = success
  11274. *
  11275. * PARAMETERS:
  11276. * *self - pointer to notifier_block structure
  11277. * event - event type of fb notifier
  11278. * *data - pointer to fb_event structure
  11279. ******************************************************************************/
  11280. static int fb_notifier_callback(struct notifier_block *self,
  11281. unsigned long event, void *data)
  11282. {
  11283. struct pt_core_data *cd =
  11284. container_of(self, struct pt_core_data, fb_notifier);
  11285. struct fb_event *evdata = data;
  11286. int *blank;
  11287. if (event != FB_EVENT_BLANK || !evdata)
  11288. goto exit;
  11289. blank = evdata->data;
  11290. if (*blank == FB_BLANK_UNBLANK) {
  11291. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11292. if (cd->fb_state != FB_ON) {
  11293. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11294. #if defined(CONFIG_PM_SLEEP)
  11295. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11296. pt_core_resume_(cd->dev);
  11297. #endif
  11298. cd->fb_state = FB_ON;
  11299. }
  11300. } else if (*blank == FB_BLANK_POWERDOWN) {
  11301. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11302. if (cd->fb_state != FB_OFF) {
  11303. #if defined(CONFIG_PM_SLEEP)
  11304. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11305. pt_core_suspend_(cd->dev);
  11306. #endif
  11307. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11308. cd->fb_state = FB_OFF;
  11309. }
  11310. }
  11311. exit:
  11312. return 0;
  11313. }
  11314. /*******************************************************************************
  11315. * FUNCTION: pt_setup_fb_notifier
  11316. *
  11317. * SUMMARY: Set up call back function into fb notifier.
  11318. *
  11319. * PARAMETERS:
  11320. * *cd - pointer to core data
  11321. ******************************************************************************/
  11322. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11323. {
  11324. int rc = 0;
  11325. cd->fb_state = FB_ON;
  11326. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11327. rc = fb_register_client(&cd->fb_notifier);
  11328. if (rc)
  11329. pt_debug(cd->dev, DL_ERROR,
  11330. "Unable to register fb_notifier: %d\n", rc);
  11331. }
  11332. #endif
  11333. /*******************************************************************************
  11334. * FUNCTION: pt_watchdog_work
  11335. *
  11336. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11337. * sleeping then this function simply returns. If the DUT is awake the
  11338. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11339. * somehow a response is waiting on the DUT that has not been read. If
  11340. * this occurs the message is simply consumed. If or once the IRQ is
  11341. * cleared, a PIP PING message is sent to the DUT and if the response
  11342. * is received the watchdog succeeds and exits, if no response is seen
  11343. * a startup is queued unless the maximum number of startups have already
  11344. * been attempted, in that case a BL is attempted.
  11345. *
  11346. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11347. * work thread
  11348. *
  11349. * RETURN: void
  11350. *
  11351. * PARAMETERS:
  11352. * *work - pointer to a work structure for the watchdog work queue
  11353. ******************************************************************************/
  11354. static void pt_watchdog_work(struct work_struct *work)
  11355. {
  11356. int rc = 0;
  11357. struct pt_core_data *cd = container_of(work,
  11358. struct pt_core_data, watchdog_work);
  11359. /*
  11360. * if found the current sleep_state is SS_SLEEPING
  11361. * then no need to request_exclusive, directly return
  11362. */
  11363. if (cd->sleep_state == SS_SLEEPING)
  11364. return;
  11365. #ifdef TTDL_DIAGNOSTICS
  11366. cd->watchdog_count++;
  11367. #endif /* TTDL_DIAGNOSTICS */
  11368. /*
  11369. * The first WD interval was extended to allow DDI to come up.
  11370. * If the WD interval is not the default then adjust timer to the
  11371. * current setting. The user can override value with drv_debug sysfs.
  11372. */
  11373. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11374. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11375. msecs_to_jiffies(cd->watchdog_interval));
  11376. }
  11377. if (pt_check_irq_asserted(cd)) {
  11378. #ifdef TTDL_DIAGNOSTICS
  11379. cd->watchdog_irq_stuck_count++;
  11380. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11381. #endif /* TTDL_DIAGNOSTICS */
  11382. pt_debug(cd->dev, DL_ERROR,
  11383. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11384. __func__);
  11385. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11386. }
  11387. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11388. if (rc < 0) {
  11389. pt_debug(cd->dev, DL_ERROR,
  11390. "%s: fail get exclusive ex=%p own=%p\n",
  11391. __func__, cd->exclusive_dev, cd->dev);
  11392. goto queue_startup;
  11393. }
  11394. rc = pt_pip_null_(cd);
  11395. if (release_exclusive(cd, cd->dev) < 0)
  11396. pt_debug(cd->dev, DL_ERROR,
  11397. "%s: fail to release exclusive\n", __func__);
  11398. queue_startup:
  11399. if (rc) {
  11400. #ifdef TTDL_DIAGNOSTICS
  11401. cd->watchdog_failed_access_count++;
  11402. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11403. #endif /* TTDL_DIAGNOSTICS */
  11404. pt_debug(cd->dev, DL_ERROR,
  11405. "%s: failed to access device in WD, retry count=%d\n",
  11406. __func__, cd->startup_retry_count);
  11407. /* Already tried FW upgrade because of watchdog but failed */
  11408. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11409. return;
  11410. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11411. /*
  11412. * Any wrapper function that trys to disable the
  11413. * WD killing this worker cannot be called here.
  11414. */
  11415. rc = request_exclusive(cd, cd->dev,
  11416. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11417. if (rc < 0) {
  11418. pt_debug(cd->dev, DL_ERROR,
  11419. "%s: fail get exclusive ex=%p own=%p\n",
  11420. __func__, cd->exclusive_dev, cd->dev);
  11421. goto exit;
  11422. }
  11423. cd->hw_detected = false;
  11424. cd->startup_status = STARTUP_STATUS_START;
  11425. pt_debug(cd->dev, DL_DEBUG,
  11426. "%s: Startup Status Reset\n", __func__);
  11427. rc = pt_dut_reset_and_wait(cd);
  11428. if (release_exclusive(cd, cd->dev) < 0)
  11429. pt_debug(cd->dev, DL_ERROR,
  11430. "%s: fail to release exclusive\n",
  11431. __func__);
  11432. if (!rc) {
  11433. cd->hw_detected = true;
  11434. if (!cd->flashless_dut)
  11435. pt_queue_enum(cd);
  11436. }
  11437. #ifdef TTDL_DIAGNOSTICS
  11438. cd->wd_xres_count++;
  11439. pt_debug(cd->dev, DL_ERROR,
  11440. "%s: Comm Failed - DUT reset [#%d]\n",
  11441. __func__, cd->wd_xres_count);
  11442. #endif /* TTDL_DIAGNOSTICS */
  11443. } else {
  11444. /*
  11445. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11446. * reset the part to regain communications, try to BL
  11447. */
  11448. pt_debug(cd->dev, DL_ERROR,
  11449. "%s: WD DUT access failure, Start FW Upgrade\n",
  11450. __func__);
  11451. #ifdef TTDL_DIAGNOSTICS
  11452. /*
  11453. * When diagnostics is enabled allow TTDL to keep
  11454. * trying to find the DUT. This allows the DUT to be
  11455. * hot swap-able while the host stays running. In
  11456. * production this may not be wanted as a customer
  11457. * may have several touch drivers and any driver
  11458. * that doesn't match the current DUT should give
  11459. * up trying and give up using the bus.
  11460. */
  11461. pt_debug(cd->dev, DL_INFO,
  11462. "%s: Resetting startup_retry_count\n",
  11463. __func__);
  11464. cd->startup_retry_count = 0;
  11465. #endif /* TTDL_DIAGNOSTICS */
  11466. /*
  11467. * Since fw may be broken,reset sysinfo ready flag
  11468. * to let upgrade function work.
  11469. */
  11470. mutex_lock(&cd->system_lock);
  11471. cd->sysinfo.ready = false;
  11472. mutex_unlock(&cd->system_lock);
  11473. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11474. pt_debug(cd->dev, DL_ERROR,
  11475. "%s: Queue Restart\n", __func__);
  11476. pt_queue_restart(cd);
  11477. } else
  11478. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11479. }
  11480. } else {
  11481. cd->hw_detected = true;
  11482. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11483. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11484. pt_debug(cd->dev, DL_ERROR,
  11485. "%s: HW detected but not enumerated\n",
  11486. __func__);
  11487. pt_queue_enum(cd);
  11488. }
  11489. }
  11490. exit:
  11491. pt_start_wd_timer(cd);
  11492. }
  11493. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11494. /*******************************************************************************
  11495. * FUNCTION: pt_watchdog_timer
  11496. *
  11497. * SUMMARY: The function that is called when the WD timer expires. If the
  11498. * watchdog work is not already busy schedule the watchdog work queue.
  11499. *
  11500. * RETURN: void
  11501. *
  11502. * PARAMETERS:
  11503. * handle - Handle to the watchdog timer
  11504. ******************************************************************************/
  11505. static void pt_watchdog_timer(unsigned long handle)
  11506. {
  11507. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11508. if (!cd)
  11509. return;
  11510. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11511. __func__);
  11512. if (!work_pending(&cd->watchdog_work))
  11513. schedule_work(&cd->watchdog_work);
  11514. }
  11515. #else
  11516. /*******************************************************************************
  11517. * FUNCTION: pt_watchdog_timer
  11518. *
  11519. * SUMMARY: The function that is called when the WD timer expires. If the
  11520. * watchdog work is not already busy schedule the watchdog work queue.
  11521. *
  11522. * RETURN: void
  11523. *
  11524. * PARAMETERS:
  11525. * *t - Pointer to timer list
  11526. ******************************************************************************/
  11527. static void pt_watchdog_timer(struct timer_list *t)
  11528. {
  11529. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11530. if (!cd)
  11531. return;
  11532. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11533. __func__);
  11534. if (!work_pending(&cd->watchdog_work))
  11535. schedule_work(&cd->watchdog_work);
  11536. }
  11537. #endif
  11538. /*******************************************************************************
  11539. * Core sysfs show and store functions
  11540. ******************************************************************************/
  11541. /*******************************************************************************
  11542. * FUNCTION: pt_hw_version_show
  11543. *
  11544. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11545. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11546. *
  11547. * RETURN: size of data written to sysfs node
  11548. *
  11549. * PARAMETERS:
  11550. * *dev - pointer to device structure
  11551. * *attr - pointer to device attributes structure
  11552. * *buf - pointer to print output buffer
  11553. ******************************************************************************/
  11554. static ssize_t pt_hw_version_show(struct device *dev,
  11555. struct device_attribute *attr, char *buf)
  11556. {
  11557. struct pt_core_data *cd = dev_get_drvdata(dev);
  11558. _pt_request_hw_version(dev, cd->hw_version);
  11559. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11560. }
  11561. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11562. /*******************************************************************************
  11563. * FUNCTION: pt_drv_version_show
  11564. *
  11565. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11566. * TTDL version information
  11567. *
  11568. * RETURN: Char buffer with printed TTDL version information
  11569. *
  11570. * PARAMETERS:
  11571. * *dev - pointer to device structure
  11572. * *attr - pointer to device attributes
  11573. * *buf - pointer to output buffer
  11574. ******************************************************************************/
  11575. static ssize_t pt_drv_version_show(struct device *dev,
  11576. struct device_attribute *attr, char *buf)
  11577. {
  11578. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11579. "Driver: %s\nVersion: %s\nDate: %s\n",
  11580. pt_driver_core_name, pt_driver_core_version,
  11581. pt_driver_core_date);
  11582. }
  11583. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11584. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11585. /*******************************************************************************
  11586. * FUNCTION: pt_fw_version_show
  11587. *
  11588. * SUMMARY: Show method for the fw_version sysfs node that will
  11589. * show the firmware, bootloader and PIP version information
  11590. *
  11591. * RETURN: Size of printed buffer
  11592. *
  11593. * PARAMETERS:
  11594. * *dev - pointer to device structure
  11595. * *attr - pointer to device attributes
  11596. * *buf - pointer to output buffer
  11597. ******************************************************************************/
  11598. static ssize_t pt_fw_version_show(struct device *dev,
  11599. struct device_attribute *attr, char *buf)
  11600. {
  11601. struct pt_core_data *cd = dev_get_drvdata(dev);
  11602. struct pt_ttdata *ttdata;
  11603. int rc = 0;
  11604. if (cd->mode == PT_MODE_OPERATIONAL)
  11605. rc = pt_hid_output_get_sysinfo_(cd);
  11606. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11607. __func__, cd->mode, cd->sysinfo.ready);
  11608. if (cd->sysinfo.ready)
  11609. ttdata = &cd->sysinfo.ttdata;
  11610. else
  11611. rc = -ENODATA;
  11612. if (!rc) {
  11613. return scnprintf(buf, strlen(buf),
  11614. "Status: %d\n"
  11615. "FW : %d.%d.%d\n"
  11616. "Config: %d\n"
  11617. "BL : %d.%d\n"
  11618. "PIP : %d.%d\n",
  11619. rc,
  11620. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11621. ttdata->revctrl,
  11622. ttdata->fw_ver_conf,
  11623. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11624. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11625. } else {
  11626. return scnprintf(buf, strlen(buf),
  11627. "Status: %d\n"
  11628. "FW : n/a\n"
  11629. "Config: n/a\n"
  11630. "BL : n/a\n"
  11631. "PIP : n/a\n",
  11632. rc);
  11633. }
  11634. }
  11635. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11636. /*******************************************************************************
  11637. * FUNCTION: pt_sysinfo_show
  11638. *
  11639. * SUMMARY: Show method for the sysinfo sysfs node that will
  11640. * show all the information from get system information command.
  11641. *
  11642. * RETURN: Size of printed buffer
  11643. *
  11644. * PARAMETERS:
  11645. * *dev - pointer to device structure
  11646. * *attr - pointer to device attributes
  11647. * *buf - pointer to output buffer
  11648. ******************************************************************************/
  11649. static ssize_t pt_sysinfo_show(struct device *dev,
  11650. struct device_attribute *attr, char *buf)
  11651. {
  11652. struct pt_core_data *cd = dev_get_drvdata(dev);
  11653. struct pt_sysinfo *si;
  11654. struct pt_ttdata *ttdata = NULL;
  11655. struct pt_sensing_conf_data *scd = NULL;
  11656. int rc = 0;
  11657. if (cd->mode == PT_MODE_OPERATIONAL) {
  11658. rc = pt_hid_output_get_sysinfo_(cd);
  11659. if (cd->sysinfo.ready) {
  11660. si = &cd->sysinfo;
  11661. ttdata = &si->ttdata;
  11662. scd = &si->sensing_conf_data;
  11663. } else
  11664. rc = -ENODATA;
  11665. } else
  11666. rc = -EPERM;
  11667. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11668. __func__, cd->mode, cd->sysinfo.ready);
  11669. if (!rc && ttdata && scd) {
  11670. return scnprintf(buf, strlen(buf),
  11671. "Status: %d\n"
  11672. "pip_ver_major: 0x%02X\n"
  11673. "pip_ver_minor: 0x%02X\n"
  11674. "fw_pid : 0x%04X\n"
  11675. "fw_ver_major : 0x%02X\n"
  11676. "fw_ver_minor : 0x%02X\n"
  11677. "revctrl : 0x%08X\n"
  11678. "fw_ver_conf : 0x%04X\n"
  11679. "bl_ver_major : 0x%02X\n"
  11680. "bl_ver_minor : 0x%02X\n"
  11681. "jtag_id_h : 0x%04X\n"
  11682. "jtag_id_l : 0x%04X\n"
  11683. "mfg_id[0] : 0x%02X\n"
  11684. "mfg_id[1] : 0x%02X\n"
  11685. "mfg_id[2] : 0x%02X\n"
  11686. "mfg_id[3] : 0x%02X\n"
  11687. "mfg_id[4] : 0x%02X\n"
  11688. "mfg_id[5] : 0x%02X\n"
  11689. "mfg_id[6] : 0x%02X\n"
  11690. "mfg_id[7] : 0x%02X\n"
  11691. "post_code : 0x%04X\n"
  11692. "electrodes_x : 0x%02X\n"
  11693. "electrodes_y : 0x%02X\n"
  11694. "len_x : 0x%04X\n"
  11695. "len_y : 0x%04X\n"
  11696. "res_x : 0x%04X\n"
  11697. "res_y : 0x%04X\n"
  11698. "max_z : 0x%04X\n"
  11699. "origin_x : 0x%02X\n"
  11700. "origin_y : 0x%02X\n"
  11701. "panel_id : 0x%02X\n"
  11702. "btn : 0x%02X\n"
  11703. "scan_mode : 0x%02X\n"
  11704. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  11705. rc,
  11706. ttdata->pip_ver_major,
  11707. ttdata->pip_ver_minor,
  11708. ttdata->fw_pid,
  11709. ttdata->fw_ver_major,
  11710. ttdata->fw_ver_minor,
  11711. ttdata->revctrl,
  11712. ttdata->fw_ver_conf,
  11713. ttdata->bl_ver_major,
  11714. ttdata->bl_ver_minor,
  11715. ttdata->jtag_id_h,
  11716. ttdata->jtag_id_l,
  11717. ttdata->mfg_id[0],
  11718. ttdata->mfg_id[1],
  11719. ttdata->mfg_id[2],
  11720. ttdata->mfg_id[3],
  11721. ttdata->mfg_id[4],
  11722. ttdata->mfg_id[5],
  11723. ttdata->mfg_id[6],
  11724. ttdata->mfg_id[7],
  11725. ttdata->post_code,
  11726. scd->electrodes_x,
  11727. scd->electrodes_y,
  11728. scd->len_x,
  11729. scd->len_y,
  11730. scd->res_x,
  11731. scd->res_y,
  11732. scd->max_z,
  11733. scd->origin_x,
  11734. scd->origin_y,
  11735. scd->panel_id,
  11736. scd->btn,
  11737. scd->scan_mode,
  11738. scd->max_tch);
  11739. } else {
  11740. return scnprintf(buf, strlen(buf),
  11741. "Status: %d\n",
  11742. rc);
  11743. }
  11744. }
  11745. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  11746. /*******************************************************************************
  11747. * FUNCTION: pt_hw_reset_show
  11748. *
  11749. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  11750. * by toggling the XRES line and then calls the startup function to
  11751. * allow TTDL to re-enumerate the DUT.
  11752. * The printed value reflects the status of the full reset/enum.
  11753. *
  11754. * PARAMETERS:
  11755. * *dev - pointer to Device structure
  11756. * *attr - pointer to the device attribute structure
  11757. * *buf - pointer to buffer to print
  11758. ******************************************************************************/
  11759. static ssize_t pt_hw_reset_show(struct device *dev,
  11760. struct device_attribute *attr, char *buf)
  11761. {
  11762. struct pt_core_data *cd = dev_get_drvdata(dev);
  11763. int rc = 0;
  11764. int time = 0;
  11765. u8 reset_status = 0;
  11766. int t;
  11767. struct pt_hid_desc hid_desc;
  11768. memset(&hid_desc, 0, sizeof(hid_desc));
  11769. /* Only allow DUT reset if no active BL in progress */
  11770. mutex_lock(&cd->firmware_class_lock);
  11771. mutex_lock(&cd->system_lock);
  11772. cd->startup_state = STARTUP_NONE;
  11773. mutex_unlock(&(cd->system_lock));
  11774. pt_stop_wd_timer(cd);
  11775. /* ensure no left over exclusive access is still locked */
  11776. release_exclusive(cd, cd->dev);
  11777. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  11778. if (rc) {
  11779. mutex_unlock(&cd->firmware_class_lock);
  11780. pt_debug(cd->dev, DL_ERROR,
  11781. "%s: HW reset failed rc = %d\n", __func__, rc);
  11782. goto exit_hw_reset;
  11783. }
  11784. reset_status |= 0x01 << 0;
  11785. if (cd->flashless_dut) {
  11786. mutex_unlock(&cd->firmware_class_lock);
  11787. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  11788. msecs_to_jiffies(200));
  11789. if (IS_TMO(t)) {
  11790. pt_debug(dev, DL_ERROR,
  11791. "%s: Timeout waiting for FW update",
  11792. __func__);
  11793. rc = -ETIME;
  11794. goto exit_hw_reset;
  11795. } else {
  11796. pt_debug(dev, DL_INFO,
  11797. "%s: ----- Wait FW Loading ----",
  11798. __func__);
  11799. rc = _pt_request_wait_for_enum_state(
  11800. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  11801. if (rc) {
  11802. pt_debug(cd->dev, DL_ERROR,
  11803. "%s: No FW Sentinel detected rc = %d\n",
  11804. __func__, rc);
  11805. goto exit_hw_reset;
  11806. }
  11807. reset_status |= 0x01 << 1;
  11808. }
  11809. } else {
  11810. /* Wait for any sentinel */
  11811. rc = _pt_request_wait_for_enum_state(dev, 150,
  11812. STARTUP_STATUS_BL_RESET_SENTINEL |
  11813. STARTUP_STATUS_FW_RESET_SENTINEL);
  11814. if (rc) {
  11815. mutex_unlock(&cd->firmware_class_lock);
  11816. pt_debug(cd->dev, DL_ERROR,
  11817. "%s: No Sentinel detected rc = %d\n",
  11818. __func__, rc);
  11819. goto exit_hw_reset;
  11820. }
  11821. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  11822. msleep(30);
  11823. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  11824. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  11825. if (rc < 0) {
  11826. pt_debug(cd->dev, DL_ERROR,
  11827. "%s: Error on getting HID descriptor r=%d\n",
  11828. __func__, rc);
  11829. goto exit_hw_reset;
  11830. }
  11831. cd->mode = pt_get_mode(cd, &hid_desc);
  11832. if (cd->mode == PT_MODE_BOOTLOADER)
  11833. rc = pt_hid_output_bl_launch_app_(cd);
  11834. } else {
  11835. if (cd->mode == PT_MODE_BOOTLOADER)
  11836. rc = pt_pip2_launch_app(dev,
  11837. PT_CORE_CMD_UNPROTECTED);
  11838. }
  11839. if (rc) {
  11840. mutex_unlock(&cd->firmware_class_lock);
  11841. pt_debug(cd->dev, DL_ERROR,
  11842. "%s: PIP launch app failed rc = %d\n",
  11843. __func__, rc);
  11844. goto exit_hw_reset;
  11845. }
  11846. mutex_unlock(&cd->firmware_class_lock);
  11847. reset_status |= 0x01 << 1;
  11848. msleep(20);
  11849. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  11850. (cd->mode != PT_MODE_OPERATIONAL))
  11851. pt_queue_restart(cd);
  11852. else
  11853. pt_queue_enum(cd);
  11854. }
  11855. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  11856. msleep(50);
  11857. pt_debug(cd->dev, DL_INFO,
  11858. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  11859. __func__, time, STARTUP_STATUS_COMPLETE,
  11860. cd->startup_status);
  11861. time += 50;
  11862. }
  11863. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  11864. rc = -ETIME;
  11865. goto exit_hw_reset;
  11866. }
  11867. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  11868. __func__, cd->startup_status);
  11869. reset_status |= 0x01 << 2;
  11870. pt_start_wd_timer(cd);
  11871. exit_hw_reset:
  11872. return scnprintf(buf, strlen(buf),
  11873. "Status: %d\n"
  11874. "Reset Status: 0x%02X\n", rc, reset_status);
  11875. }
  11876. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  11877. /*******************************************************************************
  11878. * FUNCTION: pt_pip2_cmd_rsp_store
  11879. *
  11880. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  11881. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  11882. * Command byte order:
  11883. * Byte [0] - PIP2 command ID
  11884. * Byte [1-n] - PIP2 command payload
  11885. *
  11886. * RETURN: Size of passed in buffer
  11887. *
  11888. * PARAMETERS:
  11889. * *dev - pointer to device structure
  11890. * *attr - pointer to device attributes
  11891. * *buf - pointer to buffer that hold the command parameters
  11892. * size - size of buf
  11893. ******************************************************************************/
  11894. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  11895. struct device_attribute *attr, const char *buf, size_t size)
  11896. {
  11897. struct pt_core_data *cd = dev_get_drvdata(dev);
  11898. u16 actual_read_len;
  11899. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11900. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  11901. u8 pip2_cmd_id = 0x00;
  11902. u8 *pip2_cmd_data = NULL;
  11903. int data_len = 0;
  11904. int length;
  11905. int rc = 0;
  11906. /* clear shared data */
  11907. mutex_lock(&cd->sysfs_lock);
  11908. cd->raw_cmd_status = 0;
  11909. cd->cmd_rsp_buf_len = 0;
  11910. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11911. mutex_unlock(&cd->sysfs_lock);
  11912. length = _pt_ic_parse_input_hex(dev, buf, size,
  11913. input_data, PT_MAX_PIP2_MSG_SIZE);
  11914. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11915. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11916. __func__);
  11917. rc = -EINVAL;
  11918. goto exit;
  11919. }
  11920. /* Send PIP2 command if enough data was provided */
  11921. if (length >= 1) {
  11922. pip2_cmd_id = input_data[0];
  11923. pip2_cmd_data = &input_data[1];
  11924. data_len = length - 1;
  11925. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  11926. pip2_cmd_id, pip2_cmd_data, data_len,
  11927. read_buf, &actual_read_len);
  11928. cd->raw_cmd_status = rc;
  11929. if (rc) {
  11930. pt_debug(dev, DL_ERROR,
  11931. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  11932. __func__, pip2_cmd_id, rc);
  11933. goto exit;
  11934. } else {
  11935. cd->cmd_rsp_buf_len = actual_read_len;
  11936. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  11937. pt_debug(dev, DL_ERROR,
  11938. "%s: PIP2 actual_read_len = %d\n",
  11939. __func__, actual_read_len);
  11940. }
  11941. } else {
  11942. rc = -EINVAL;
  11943. pt_debug(dev, DL_ERROR,
  11944. "%s: Insufficient data provided for PIP2 cmd\n",
  11945. __func__);
  11946. }
  11947. exit:
  11948. if (rc)
  11949. return rc;
  11950. return size;
  11951. }
  11952. /*******************************************************************************
  11953. * FUNCTION: pt_pip2_cmd_rsp_show
  11954. *
  11955. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  11956. * response generated after using the store method of the pip2_cmd_rsp
  11957. * sysfs node, are available to be read here.
  11958. *
  11959. * PARAMETERS:
  11960. * *dev - pointer to Device structure
  11961. * *attr - pointer to the device attribute structure
  11962. * *buf - pointer to buffer to print
  11963. ******************************************************************************/
  11964. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  11965. struct device_attribute *attr, char *buf)
  11966. {
  11967. struct pt_core_data *cd = dev_get_drvdata(dev);
  11968. int i;
  11969. ssize_t data_len;
  11970. int index;
  11971. mutex_lock(&cd->sysfs_lock);
  11972. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11973. "Status: %d\n", cd->raw_cmd_status);
  11974. if (cd->raw_cmd_status)
  11975. goto error;
  11976. /* Remove the CRC from the length of the response */
  11977. data_len = cd->cmd_rsp_buf_len - 2;
  11978. /* Start printing from the data payload */
  11979. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  11980. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11981. "%02X ", cd->cmd_rsp_buf[i]);
  11982. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  11983. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11984. "\n(%zd bytes)\n",
  11985. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  11986. } else {
  11987. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11988. "\n(%zd bytes)\n", 0);
  11989. }
  11990. error:
  11991. mutex_unlock(&cd->sysfs_lock);
  11992. return index;
  11993. }
  11994. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  11995. pt_pip2_cmd_rsp_store);
  11996. /*******************************************************************************
  11997. * FUNCTION: pt_command_store
  11998. *
  11999. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12000. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12001. * TTDL will not parse the command.
  12002. *
  12003. * RETURN: Size of passed in buffer
  12004. *
  12005. * PARAMETERS:
  12006. * *dev - pointer to device structure
  12007. * *attr - pointer to device attributes
  12008. * *buf - pointer to buffer that hold the command parameters
  12009. * size - size of buf
  12010. ******************************************************************************/
  12011. static ssize_t pt_command_store(struct device *dev,
  12012. struct device_attribute *attr, const char *buf, size_t size)
  12013. {
  12014. struct pt_core_data *cd = dev_get_drvdata(dev);
  12015. unsigned short crc;
  12016. u16 actual_read_len;
  12017. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12018. int length;
  12019. int len_field;
  12020. int rc = 0;
  12021. mutex_lock(&cd->sysfs_lock);
  12022. cd->cmd_rsp_buf_len = 0;
  12023. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12024. mutex_unlock(&cd->sysfs_lock);
  12025. length = _pt_ic_parse_input_hex(dev, buf, size,
  12026. input_data, PT_MAX_PIP2_MSG_SIZE);
  12027. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12028. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12029. __func__);
  12030. rc = -EINVAL;
  12031. goto pt_command_store_exit;
  12032. }
  12033. /* PIP2 messages begin with 01 01 */
  12034. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12035. cd->pip2_prot_active = 1;
  12036. /* Override next seq tag with what was sent */
  12037. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12038. /* For PIP2 cmd if length does not include crc, add it */
  12039. len_field = (input_data[3] << 8) | input_data[2];
  12040. if (len_field == length && length <= 254) {
  12041. crc = crc_ccitt_calculate(&input_data[2],
  12042. length - 2);
  12043. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12044. __func__, length, crc);
  12045. input_data[length] = (crc & 0xFF00) >> 8;
  12046. input_data[length + 1] = crc & 0x00FF;
  12047. length = length + 2;
  12048. }
  12049. }
  12050. /* write PIP command to log */
  12051. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12052. pm_runtime_get_sync(dev);
  12053. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12054. length, input_data, &actual_read_len);
  12055. pm_runtime_put(dev);
  12056. mutex_lock(&cd->sysfs_lock);
  12057. cd->raw_cmd_status = rc;
  12058. if (rc) {
  12059. cd->cmd_rsp_buf_len = 0;
  12060. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12061. __func__, buf);
  12062. } else {
  12063. cd->cmd_rsp_buf_len = actual_read_len;
  12064. }
  12065. cd->pip2_prot_active = 0;
  12066. mutex_unlock(&cd->sysfs_lock);
  12067. pt_command_store_exit:
  12068. if (rc)
  12069. return rc;
  12070. return size;
  12071. }
  12072. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12073. /*******************************************************************************
  12074. * FUNCTION: pt_response_show
  12075. *
  12076. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12077. * response generated after using the pt_command_store sysfs node, are
  12078. * available to be read here.
  12079. *
  12080. * PARAMETERS:
  12081. * *dev - pointer to Device structure
  12082. * *attr - pointer to the device attribute structure
  12083. * *buf - pointer to buffer to print
  12084. ******************************************************************************/
  12085. static ssize_t pt_response_show(struct device *dev,
  12086. struct device_attribute *attr, char *buf)
  12087. {
  12088. struct pt_core_data *cd = dev_get_drvdata(dev);
  12089. int i;
  12090. ssize_t num_read;
  12091. int index;
  12092. mutex_lock(&cd->sysfs_lock);
  12093. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12094. "Status: %d\n", cd->raw_cmd_status);
  12095. if (cd->raw_cmd_status)
  12096. goto error;
  12097. num_read = cd->cmd_rsp_buf_len;
  12098. for (i = 0; i < num_read; i++)
  12099. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12100. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12101. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12102. "(%zd bytes)\n", num_read);
  12103. error:
  12104. mutex_unlock(&cd->sysfs_lock);
  12105. return index;
  12106. }
  12107. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12108. /*******************************************************************************
  12109. * FUNCTION: pt_dut_debug_show
  12110. *
  12111. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12112. * are available for the store method.
  12113. *
  12114. * RETURN:
  12115. * 0 = success
  12116. * !0 = failure
  12117. *
  12118. * PARAMETERS:
  12119. * *dev - pointer to device structure
  12120. * *attr - pointer to device attributes
  12121. * *buf - pointer to output buffer
  12122. ******************************************************************************/
  12123. static ssize_t pt_dut_debug_show(struct device *dev,
  12124. struct device_attribute *attr, char *buf)
  12125. {
  12126. ssize_t ret;
  12127. ret = scnprintf(buf, strlen(buf),
  12128. "Status: 0\n"
  12129. "dut_debug sends the following commands to the DUT:\n"
  12130. "%d %s \t- %s\n"
  12131. "%d %s \t- %s\n"
  12132. "%d %s \t- %s\n"
  12133. "%d %s \t- %s\n"
  12134. "%d %s \t- %s\n"
  12135. "%d %s \t- %s\n"
  12136. "%d %s \t- %s\n"
  12137. "%d %s \t- %s\n"
  12138. "%d %s \t- %s\n"
  12139. "%d %s \t- %s\n"
  12140. "%d %s \t- %s\n"
  12141. "%d %s \t- %s\n"
  12142. "%d %s \t- %s\n"
  12143. "%d %s \t- %s\n"
  12144. "%d %s \t- %s\n"
  12145. "%d %s \t- %s\n"
  12146. "%d %s \t- %s\n"
  12147. ,
  12148. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12149. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12150. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12151. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12152. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12153. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12154. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12155. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12156. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12157. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12158. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12159. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12160. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12161. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12162. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12163. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12164. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12165. );
  12166. return ret;
  12167. }
  12168. /*******************************************************************************
  12169. * FUNCTION: pt_drv_debug_show
  12170. *
  12171. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12172. * are available for the store method.
  12173. *
  12174. * RETURN:
  12175. * 0 = success
  12176. * !0 = failure
  12177. *
  12178. * PARAMETERS:
  12179. * *dev - pointer to device structure
  12180. * *attr - pointer to device attributes
  12181. * *buf - pointer to output buffer
  12182. ******************************************************************************/
  12183. static ssize_t pt_drv_debug_show(struct device *dev,
  12184. struct device_attribute *attr, char *buf)
  12185. {
  12186. ssize_t ret;
  12187. ret = scnprintf(buf, strlen(buf),
  12188. "Status: 0\n"
  12189. "drv_debug supports the following values:\n"
  12190. "%d %s \t- %s\n"
  12191. "%d %s \t- %s\n"
  12192. "%d %s \t- %s\n"
  12193. "%d %s \t- %s\n"
  12194. "%d %s \t- %s\n"
  12195. "%d %s \t- %s\n"
  12196. "%d %s \t- %s\n"
  12197. "%d %s \t- %s\n"
  12198. "%d %s \t- %s\n"
  12199. "%d %s - %s\n"
  12200. "%d %s \t- %s\n"
  12201. "%d %s \t- %s\n"
  12202. #ifdef TTDL_DIAGNOSTICS
  12203. "%d %s \t- %s\n"
  12204. "%d %s \t- %s\n"
  12205. "%d %s \t- %s\n"
  12206. #endif /* TTDL_DIAGNOSTICS */
  12207. "%d %s \t- %s\n"
  12208. #ifdef TTDL_DIAGNOSTICS
  12209. "%d %s \t- %s\n"
  12210. "%d %s \t- %s\n"
  12211. "%d %s \t- %s\n"
  12212. "%d %s \t- %s\n"
  12213. "%d %s \t- %s\n"
  12214. "%d %s \t- %s\n"
  12215. "%d %s \t- %s\n"
  12216. "%d %s \t- %s\n"
  12217. "%d %s \t- %s\n"
  12218. #endif /* TTDL_DIAGNOSTICS */
  12219. ,
  12220. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12221. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12222. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12223. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12224. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12225. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12226. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12227. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12228. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12229. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12230. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12231. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12232. #ifdef TTDL_DIAGNOSTICS
  12233. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12234. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12235. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12236. #endif /* TTDL_DIAGNOSTICS */
  12237. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12238. #ifdef TTDL_DIAGNOSTICS
  12239. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12240. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12241. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12242. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12243. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12244. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12245. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12246. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12247. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12248. "TTHE_TUNER HID USB Format"
  12249. #endif /* TTDL_DIAGNOSTICS */
  12250. );
  12251. return ret;
  12252. }
  12253. /*******************************************************************************
  12254. * FUNCTION: pt_drv_debug_store
  12255. *
  12256. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12257. * dut_debug. Drv_debug will contain all functionality that can be run
  12258. * without a DUT preset and is available anytime TTDL is running.
  12259. * Dut_debug requires a DUT to be available and will only be created after
  12260. * a DUT has been detected.
  12261. * This function will eventually be split into two but until the overlap
  12262. * has been depricated this function contains all commands that can be
  12263. * used for TTDL/DUT debugging status and control.
  12264. * All commands require at least one value to be passed in *buf with some
  12265. * requiring two.
  12266. *
  12267. * RETURN: Size of passed in buffer
  12268. *
  12269. * PARAMETERS:
  12270. * *dev - pointer to device structure
  12271. * *attr - pointer to device attributes
  12272. * *buf - pointer to buffer that hold the command parameters
  12273. * size - size of buf
  12274. ******************************************************************************/
  12275. static ssize_t pt_drv_debug_store(struct device *dev,
  12276. struct device_attribute *attr, const char *buf, size_t size)
  12277. {
  12278. struct pt_core_data *cd = dev_get_drvdata(dev);
  12279. unsigned long value;
  12280. int rc = 0;
  12281. u8 return_data[8];
  12282. static u8 wd_disabled;
  12283. u32 input_data[3];
  12284. int length;
  12285. #ifdef TTDL_DIAGNOSTICS
  12286. struct i2c_client *client = to_i2c_client(dev);
  12287. unsigned short crc = 0;
  12288. u16 cal_size;
  12289. #endif
  12290. input_data[0] = 0;
  12291. input_data[1] = 0;
  12292. /* Maximmum input is two elements */
  12293. length = _pt_ic_parse_input(dev, buf, size,
  12294. input_data, ARRAY_SIZE(input_data));
  12295. if (length < 1 || length > 2) {
  12296. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12297. __func__);
  12298. rc = -EINVAL;
  12299. goto pt_drv_debug_store_exit;
  12300. }
  12301. value = input_data[0];
  12302. if (length == 1) {
  12303. pt_debug(dev, DL_DEBUG,
  12304. "%s: Debug Cmd Received (id=%d)\n",
  12305. __func__, input_data[0]);
  12306. } else if (length == 2) {
  12307. pt_debug(dev, DL_DEBUG,
  12308. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12309. __func__, input_data[0], input_data[1]);
  12310. } else {
  12311. pt_debug(dev, DL_DEBUG,
  12312. "%s: Invalid arguments received\n", __func__);
  12313. rc = -EINVAL;
  12314. goto pt_drv_debug_store_exit;
  12315. }
  12316. /* Start watchdog timer command */
  12317. if (value == PT_DRV_DBG_START_WD) {
  12318. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12319. wd_disabled = 0;
  12320. cd->watchdog_force_stop = false;
  12321. pt_start_wd_timer(cd);
  12322. goto pt_drv_debug_store_exit;
  12323. }
  12324. /* Stop watchdog timer temporarily */
  12325. pt_stop_wd_timer(cd);
  12326. if (value == PT_DRV_DBG_STOP_WD) {
  12327. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12328. wd_disabled = 1;
  12329. cd->watchdog_force_stop = true;
  12330. goto pt_drv_debug_store_exit;
  12331. }
  12332. switch (value) {
  12333. case PT_DRV_DBG_SUSPEND: /* 4 */
  12334. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12335. wd_disabled = 1;
  12336. rc = pt_core_sleep(cd);
  12337. if (rc)
  12338. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12339. __func__, rc);
  12340. else
  12341. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12342. __func__);
  12343. break;
  12344. case PT_DRV_DBG_RESUME: /* 5 */
  12345. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12346. rc = pt_core_wake(cd);
  12347. if (rc)
  12348. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12349. __func__, rc);
  12350. else
  12351. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12352. __func__);
  12353. break;
  12354. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12355. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12356. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12357. break;
  12358. case PT_DUT_DBG_HID_RESET: /* 50 */
  12359. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12360. pt_hid_cmd_reset(cd);
  12361. break;
  12362. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12363. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12364. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12365. wd_disabled = 0;
  12366. break;
  12367. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12368. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12369. __func__);
  12370. wd_disabled = 1;
  12371. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12372. break;
  12373. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12374. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12375. __func__);
  12376. wd_disabled = 1;
  12377. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12378. break;
  12379. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12380. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12381. pt_hid_output_bl_get_information(cd, return_data);
  12382. break;
  12383. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12384. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12385. __func__);
  12386. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12387. break;
  12388. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12389. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12390. pt_hid_output_bl_launch_app(cd);
  12391. break;
  12392. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12393. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12394. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12395. break;
  12396. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12397. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12398. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12399. break;
  12400. case PT_DUT_DBG_RESET: /* 98 */
  12401. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12402. rc = pt_hw_hard_reset(cd);
  12403. break;
  12404. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12405. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12406. pt_pip_null(cd);
  12407. break;
  12408. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12409. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12410. rc = pt_pip_start_bootloader(cd);
  12411. if (!rc) {
  12412. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12413. cd->mode = PT_MODE_BOOTLOADER;
  12414. }
  12415. break;
  12416. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12417. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12418. pt_hid_output_get_sysinfo(cd);
  12419. break;
  12420. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12421. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12422. pt_pip_suspend_scanning(cd);
  12423. break;
  12424. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12425. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12426. pt_pip_resume_scanning(cd);
  12427. break;
  12428. #ifdef TTHE_TUNER_SUPPORT
  12429. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12430. cd->tthe_exit = 1;
  12431. wake_up(&cd->wait_q);
  12432. kfree(cd->tthe_buf);
  12433. cd->tthe_buf = NULL;
  12434. cd->tthe_exit = 0;
  12435. break;
  12436. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12437. if (cd->tthe_buf)
  12438. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12439. else
  12440. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12441. __func__);
  12442. break;
  12443. #endif
  12444. #ifdef TTDL_DIAGNOSTICS
  12445. case PT_DUT_DBG_HID_DESC: /* 109 */
  12446. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12447. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12448. break;
  12449. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12450. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12451. __func__);
  12452. pt_erase_parameter_list(cd);
  12453. break;
  12454. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12455. rc = pt_read_input(cd);
  12456. if (!rc)
  12457. pt_parse_input(cd);
  12458. break;
  12459. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12460. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12461. &cal_size, &crc);
  12462. if (rc)
  12463. pt_debug(dev, DL_ERROR,
  12464. "%s: CAL Data clear failed rc=%d\n",
  12465. __func__, rc);
  12466. else
  12467. pt_debug(dev, DL_INFO,
  12468. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12469. __func__, crc, size);
  12470. break;
  12471. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12472. mutex_lock(&cd->system_lock);
  12473. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12474. cd->debug_level = input_data[1];
  12475. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12476. __func__, cd->debug_level);
  12477. } else {
  12478. rc = -EINVAL;
  12479. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12480. __func__, input_data[1]);
  12481. }
  12482. mutex_unlock(&(cd->system_lock));
  12483. break;
  12484. #endif
  12485. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12486. mutex_lock(&cd->system_lock);
  12487. if (input_data[1] > 100) {
  12488. cd->watchdog_interval = input_data[1];
  12489. pt_debug(dev, DL_INFO,
  12490. "%s: Set watchdog_ interval to: %d\n",
  12491. __func__, cd->watchdog_interval);
  12492. pt_start_wd_timer(cd);
  12493. } else {
  12494. rc = -EINVAL;
  12495. pt_debug(dev, DL_ERROR,
  12496. "%s: Invalid watchdog interval: %d\n",
  12497. __func__, input_data[1]);
  12498. }
  12499. mutex_unlock(&(cd->system_lock));
  12500. break;
  12501. #ifdef TTDL_DIAGNOSTICS
  12502. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12503. mutex_lock(&cd->system_lock);
  12504. if (input_data[1] == 0) {
  12505. cd->show_timestamp = 0;
  12506. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12507. __func__);
  12508. } else if (input_data[1] == 1) {
  12509. cd->show_timestamp = 1;
  12510. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12511. __func__);
  12512. } else {
  12513. rc = -EINVAL;
  12514. pt_debug(dev, DL_ERROR,
  12515. "%s: Invalid parameter: %d\n",
  12516. __func__, input_data[1]);
  12517. }
  12518. mutex_unlock(&(cd->system_lock));
  12519. break;
  12520. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12521. if (input_data[1] == 0) {
  12522. cd->cpdata->setup_power(cd->cpdata,
  12523. PT_MT_POWER_OFF, cd->dev);
  12524. pt_debug(dev, DL_INFO,
  12525. "%s: Initiate Power Off\n", __func__);
  12526. } else if (input_data[1] == 1) {
  12527. cd->cpdata->setup_power(cd->cpdata,
  12528. PT_MT_POWER_ON, cd->dev);
  12529. pt_debug(dev, DL_INFO,
  12530. "%s: Initiate Power On\n", __func__);
  12531. } else {
  12532. rc = -EINVAL;
  12533. pt_debug(dev, DL_ERROR,
  12534. "%s: Invalid parameter: %d\n",
  12535. __func__, input_data[1]);
  12536. }
  12537. break;
  12538. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12539. if (input_data[1] == 0) {
  12540. pm_runtime_put(dev);
  12541. pt_debug(dev, DL_ERROR,
  12542. "%s: Force call pm_runtime_put()\n", __func__);
  12543. } else if (input_data[1] == 1) {
  12544. pm_runtime_get_sync(dev);
  12545. pt_debug(dev, DL_ERROR,
  12546. "%s: Force call pm_runtime_get_sync()\n",
  12547. __func__);
  12548. } else {
  12549. rc = -EINVAL;
  12550. pt_debug(dev, DL_ERROR,
  12551. "%s: WARNING: Invalid parameter: %d\n",
  12552. __func__, input_data[1]);
  12553. }
  12554. break;
  12555. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12556. mutex_lock(&cd->system_lock);
  12557. if (input_data[1] == 0) {
  12558. cd->show_tt_data = false;
  12559. pt_debug(dev, DL_INFO,
  12560. "%s: Disable TT_DATA\n", __func__);
  12561. } else if (input_data[1] == 1) {
  12562. cd->show_tt_data = true;
  12563. pt_debug(dev, DL_INFO,
  12564. "%s: Enable TT_DATA\n", __func__);
  12565. } else {
  12566. rc = -EINVAL;
  12567. pt_debug(dev, DL_ERROR,
  12568. "%s: Invalid parameter: %d\n",
  12569. __func__, input_data[1]);
  12570. }
  12571. mutex_unlock(&(cd->system_lock));
  12572. break;
  12573. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12574. if (input_data[1] == cd->active_dut_generation) {
  12575. mutex_lock(&cd->system_lock);
  12576. cd->set_dut_generation = true;
  12577. mutex_unlock(&(cd->system_lock));
  12578. } else {
  12579. mutex_lock(&cd->system_lock);
  12580. if (input_data[1] == 0) {
  12581. cd->active_dut_generation = DUT_UNKNOWN;
  12582. cd->set_dut_generation = false;
  12583. } else if (input_data[1] == 1) {
  12584. cd->active_dut_generation = DUT_PIP1_ONLY;
  12585. cd->set_dut_generation = true;
  12586. } else if (input_data[1] == 2) {
  12587. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12588. cd->set_dut_generation = true;
  12589. } else {
  12590. rc = -EINVAL;
  12591. pt_debug(dev, DL_ERROR,
  12592. "%s: Invalid parameter: %d\n",
  12593. __func__, input_data[1]);
  12594. break;
  12595. }
  12596. cd->startup_status = STARTUP_STATUS_START;
  12597. pt_debug(cd->dev, DL_DEBUG,
  12598. "%s: Startup Status Reset\n", __func__);
  12599. mutex_unlock(&(cd->system_lock));
  12600. pt_debug(dev, DL_INFO,
  12601. "%s: Active DUT Generation Set to: %d\n",
  12602. __func__, cd->active_dut_generation);
  12603. /* Changing DUT generations full restart needed */
  12604. pt_queue_restart(cd);
  12605. }
  12606. break;
  12607. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12608. mutex_lock(&cd->system_lock);
  12609. if (input_data[1] == 0) {
  12610. cd->bridge_mode = false;
  12611. pt_debug(dev, DL_INFO,
  12612. "%s: Disable Bridge Mode\n", __func__);
  12613. } else if (input_data[1] == 1) {
  12614. cd->bridge_mode = true;
  12615. pt_debug(dev, DL_INFO,
  12616. "%s: Enable Bridge Mode\n", __func__);
  12617. } else {
  12618. rc = -EINVAL;
  12619. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12620. __func__, input_data[1]);
  12621. }
  12622. mutex_unlock(&(cd->system_lock));
  12623. break;
  12624. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12625. mutex_lock(&cd->system_lock);
  12626. /* Only a 7bit address is valid */
  12627. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12628. client->addr = input_data[1];
  12629. pt_debug(dev, DL_INFO,
  12630. "%s: Set I2C Address: 0x%2X\n",
  12631. __func__, client->addr);
  12632. } else {
  12633. rc = -EINVAL;
  12634. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12635. __func__, input_data[1]);
  12636. client->addr = 0x24;
  12637. }
  12638. mutex_unlock(&(cd->system_lock));
  12639. break;
  12640. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12641. mutex_lock(&cd->system_lock);
  12642. if (input_data[1] == 0) {
  12643. cd->flashless_dut = 0;
  12644. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12645. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12646. __func__);
  12647. } else if (input_data[1] == 1) {
  12648. cd->flashless_dut = 1;
  12649. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12650. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12651. __func__);
  12652. }
  12653. mutex_unlock(&(cd->system_lock));
  12654. break;
  12655. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12656. mutex_lock(&cd->system_lock);
  12657. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12658. cd->force_pip2_seq = input_data[1];
  12659. cd->pip2_cmd_tag_seq = input_data[1];
  12660. pt_debug(dev, DL_INFO,
  12661. "%s: Force PIP2 Seq to: 0x%02X\n",
  12662. __func__, input_data[1]);
  12663. } else {
  12664. cd->force_pip2_seq = 0;
  12665. pt_debug(dev, DL_INFO,
  12666. "%s: Clear Forced PIP2 Seq\n", __func__);
  12667. }
  12668. mutex_unlock(&(cd->system_lock));
  12669. break;
  12670. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12671. mutex_lock(&cd->system_lock);
  12672. if (input_data[1] == 0) {
  12673. cd->bl_with_no_int = 0;
  12674. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  12675. } else if (input_data[1] == 1) {
  12676. cd->bl_with_no_int = 1;
  12677. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  12678. __func__);
  12679. } else {
  12680. rc = -EINVAL;
  12681. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12682. __func__, input_data[1]);
  12683. }
  12684. mutex_unlock(&(cd->system_lock));
  12685. break;
  12686. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  12687. mutex_lock(&cd->system_lock);
  12688. if (input_data[1] == 0) {
  12689. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  12690. pt_debug(dev, DL_INFO,
  12691. "%s: Disable Calibration cache in host\n",
  12692. __func__);
  12693. } else if (input_data[1] == 1) {
  12694. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  12695. pt_debug(dev, DL_INFO,
  12696. "%s: Enable Calibration cache in host\n",
  12697. __func__);
  12698. } else {
  12699. rc = -EINVAL;
  12700. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12701. __func__, input_data[1]);
  12702. }
  12703. mutex_unlock(&(cd->system_lock));
  12704. break;
  12705. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  12706. mutex_lock(&cd->system_lock);
  12707. if (input_data[1] == 0) {
  12708. cd->multi_chip = PT_FEATURE_DISABLE;
  12709. cd->ttdl_bist_select = 0x07;
  12710. pt_debug(dev, DL_INFO,
  12711. "%s: Disable Multi-chip support\n", __func__);
  12712. } else if (input_data[1] == 1) {
  12713. cd->multi_chip = PT_FEATURE_ENABLE;
  12714. cd->ttdl_bist_select = 0x3F;
  12715. pt_debug(dev, DL_INFO,
  12716. "%s: Enable Multi-chip support\n", __func__);
  12717. } else {
  12718. rc = -EINVAL;
  12719. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12720. __func__, input_data[1]);
  12721. }
  12722. mutex_unlock(&(cd->system_lock));
  12723. break;
  12724. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  12725. mutex_lock(&cd->system_lock);
  12726. if (input_data[1] <= 0x07) {
  12727. cd->panel_id_support = input_data[1];
  12728. pt_debug(dev, DL_INFO,
  12729. "%s: Set panel_id_support to %d\n",
  12730. __func__, cd->panel_id_support);
  12731. } else {
  12732. rc = -EINVAL;
  12733. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12734. __func__, input_data[1]);
  12735. }
  12736. mutex_unlock(&(cd->system_lock));
  12737. break;
  12738. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  12739. mutex_lock(&cd->system_lock);
  12740. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  12741. /*
  12742. * The timeout is changed for some cases so the
  12743. * pip_cmd_timeout_default is used to retore back to
  12744. * what the user requested as the new timeout.
  12745. */
  12746. cd->pip_cmd_timeout_default = input_data[1];
  12747. cd->pip_cmd_timeout = input_data[1];
  12748. pt_debug(dev, DL_INFO,
  12749. "%s: PIP Timeout = %d\n", __func__,
  12750. cd->pip_cmd_timeout_default);
  12751. } else {
  12752. rc = -EINVAL;
  12753. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12754. __func__, input_data[1]);
  12755. }
  12756. mutex_unlock(&(cd->system_lock));
  12757. break;
  12758. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  12759. mutex_lock(&cd->system_lock);
  12760. if (input_data[1] == 0) {
  12761. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  12762. pt_debug(dev, DL_INFO,
  12763. "%s: Disable tthe_tuner HID-USB format\n",
  12764. __func__);
  12765. } else if (input_data[1] == 1) {
  12766. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  12767. pt_debug(dev, DL_INFO,
  12768. "%s: Enable tthe_tuner HID-USB format\n",
  12769. __func__);
  12770. } else {
  12771. rc = -EINVAL;
  12772. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12773. __func__, input_data[1]);
  12774. }
  12775. mutex_unlock(&(cd->system_lock));
  12776. break;
  12777. #endif /* TTDL_DIAGNOSTICS */
  12778. default:
  12779. rc = -EINVAL;
  12780. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  12781. }
  12782. /* Enable watchdog timer */
  12783. if (!wd_disabled)
  12784. pt_start_wd_timer(cd);
  12785. pt_drv_debug_store_exit:
  12786. if (rc)
  12787. return rc;
  12788. return size;
  12789. }
  12790. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  12791. pt_drv_debug_store);
  12792. /*******************************************************************************
  12793. * FUNCTION: pt_sleep_status_show
  12794. *
  12795. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  12796. * sleep status as either ON or OFF
  12797. *
  12798. * RETURN:
  12799. * 0 = success
  12800. * !0 = failure
  12801. *
  12802. * PARAMETERS:
  12803. * *dev - pointer to device structure
  12804. * *attr - pointer to device attributes
  12805. * *buf - pointer to output buffer
  12806. ******************************************************************************/
  12807. static ssize_t pt_sleep_status_show(struct device *dev,
  12808. struct device_attribute *attr, char *buf)
  12809. {
  12810. struct pt_core_data *cd = dev_get_drvdata(dev);
  12811. ssize_t ret;
  12812. mutex_lock(&cd->system_lock);
  12813. if (cd->sleep_state == SS_SLEEP_ON)
  12814. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  12815. else
  12816. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  12817. mutex_unlock(&cd->system_lock);
  12818. return ret;
  12819. }
  12820. /*******************************************************************************
  12821. * FUNCTION: pt_panel_id_show
  12822. *
  12823. * SUMMARY: Show method for the panel_id sysfs node that will show the
  12824. * detected panel ID from the DUT
  12825. *
  12826. * RETURN:
  12827. * 0 = success
  12828. * !0 = failure
  12829. *
  12830. * PARAMETERS:
  12831. * *dev - pointer to device structure
  12832. * *attr - pointer to device attributes
  12833. * *buf - pointer to output buffer
  12834. ******************************************************************************/
  12835. static ssize_t pt_panel_id_show(struct device *dev,
  12836. struct device_attribute *attr, char *buf)
  12837. {
  12838. struct pt_core_data *cd = dev_get_drvdata(dev);
  12839. ssize_t ret;
  12840. u8 pid = PANEL_ID_NOT_ENABLED;
  12841. int rc = 0;
  12842. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12843. /*
  12844. * The DUT should report the same panel ID from both the BL and
  12845. * the FW unless the panel_id feature is set to only
  12846. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  12847. * to retrieve the panel_id.
  12848. */
  12849. if (cd->mode == PT_MODE_BOOTLOADER) {
  12850. /*
  12851. * Return the stored value if PT_PANEL_ID_BY_BL
  12852. * is not supported while other feature exits.
  12853. */
  12854. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12855. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  12856. if (rc) {
  12857. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  12858. "Failed to retrieve Panel ID. ",
  12859. "Using cached value\n",
  12860. __func__);
  12861. }
  12862. }
  12863. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12864. if (cd->panel_id_support &
  12865. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12866. /* For all systems sysinfo has the panel_id */
  12867. rc = pt_hid_output_get_sysinfo(cd);
  12868. if (!rc)
  12869. pid =
  12870. cd->sysinfo.sensing_conf_data.panel_id;
  12871. pt_debug(dev, DL_ERROR,
  12872. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  12873. __func__, rc, pid);
  12874. }
  12875. } else {
  12876. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12877. __func__);
  12878. rc = -EPERM;
  12879. }
  12880. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  12881. if (cd->mode == PT_MODE_BOOTLOADER) {
  12882. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12883. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  12884. if (rc) {
  12885. pt_debug(dev, DL_ERROR,
  12886. "%s: BL get panel ID failed rc=%d\n",
  12887. __func__, rc);
  12888. }
  12889. }
  12890. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12891. if (cd->panel_id_support &
  12892. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12893. rc = pt_hid_output_get_sysinfo(cd);
  12894. if (!rc)
  12895. pid =
  12896. cd->sysinfo.sensing_conf_data.panel_id;
  12897. pt_debug(dev, DL_ERROR,
  12898. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  12899. __func__, rc, pid);
  12900. }
  12901. } else {
  12902. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12903. __func__);
  12904. rc = -EPERM;
  12905. }
  12906. } else {
  12907. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  12908. __func__);
  12909. rc = -EPERM;
  12910. }
  12911. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  12912. rc, pid);
  12913. return ret;
  12914. }
  12915. /*******************************************************************************
  12916. * FUNCTION: pt_get_param_store
  12917. *
  12918. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  12919. * ID to retrieve with the show method.
  12920. *
  12921. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12922. *
  12923. * RETURN: Size of passed in buffer if successful
  12924. *
  12925. * PARAMETERS:
  12926. * *dev - pointer to device structure
  12927. * *attr - pointer to device attributes
  12928. * *buf - pointer to output buffer
  12929. * size - size of buf
  12930. ******************************************************************************/
  12931. static ssize_t pt_get_param_store(struct device *dev,
  12932. struct device_attribute *attr, const char *buf, size_t size)
  12933. {
  12934. struct pt_core_data *cd = dev_get_drvdata(dev);
  12935. u32 input_data[2];
  12936. int length;
  12937. int rc = 0;
  12938. /* Maximum input of one value */
  12939. length = _pt_ic_parse_input(dev, buf, size, input_data,
  12940. ARRAY_SIZE(input_data));
  12941. if (length != 1) {
  12942. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12943. __func__);
  12944. rc = -EINVAL;
  12945. goto exit;
  12946. }
  12947. mutex_lock(&cd->system_lock);
  12948. cd->get_param_id = input_data[0];
  12949. mutex_unlock(&(cd->system_lock));
  12950. exit:
  12951. if (rc)
  12952. return rc;
  12953. return size;
  12954. }
  12955. /*******************************************************************************
  12956. * FUNCTION: pt_get_param_show
  12957. *
  12958. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  12959. * parameter data from the DUT based on the ID stored in the core
  12960. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  12961. * communicate or some other error occures, an error status is returned
  12962. * with no value following.
  12963. * Output is in the form:
  12964. * Status: x
  12965. * 0xyyyyyyyy
  12966. * The 32bit data will only follow the status code if the status == 0
  12967. *
  12968. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12969. *
  12970. * RETURN:
  12971. * 0 = success
  12972. * !0 = failure
  12973. *
  12974. * PARAMETERS:
  12975. * *dev - pointer to device structure
  12976. * *attr - pointer to device attributes
  12977. * *buf - pointer to output buffer
  12978. ******************************************************************************/
  12979. static ssize_t pt_get_param_show(struct device *dev,
  12980. struct device_attribute *attr, char *buf)
  12981. {
  12982. struct pt_core_data *cd = dev_get_drvdata(dev);
  12983. ssize_t ret = 0;
  12984. int status;
  12985. u32 value = 0;
  12986. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  12987. if (status) {
  12988. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  12989. __func__, "pt_get_param", status);
  12990. ret = scnprintf(buf, strlen(buf),
  12991. "%s %d\n",
  12992. "Status:", status);
  12993. } else {
  12994. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  12995. __func__, cd->get_param_id, value);
  12996. ret = scnprintf(buf, strlen(buf),
  12997. "Status: %d\n"
  12998. "0x%04X\n",
  12999. status, value);
  13000. }
  13001. return ret;
  13002. }
  13003. /*******************************************************************************
  13004. * FUNCTION: pt_ttdl_restart_show
  13005. *
  13006. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13007. * probed modules, calls startup() and then re-probes modules.
  13008. *
  13009. * RETURN: size of data written to sysfs node
  13010. *
  13011. * PARAMETERS:
  13012. * *dev - pointer to device structure
  13013. * *attr - pointer to device attributes structure
  13014. * *buf - pointer to print output buffer
  13015. ******************************************************************************/
  13016. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13017. struct device_attribute *attr, char *buf)
  13018. {
  13019. struct pt_core_data *cd = dev_get_drvdata(dev);
  13020. int t;
  13021. int rc = 0;
  13022. mutex_lock(&cd->system_lock);
  13023. cd->startup_state = STARTUP_NONE;
  13024. mutex_unlock(&(cd->system_lock));
  13025. /* ensure no left over exclusive access is still locked */
  13026. release_exclusive(cd, cd->dev);
  13027. pt_queue_restart(cd);
  13028. t = wait_event_timeout(cd->wait_q,
  13029. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13030. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13031. if (IS_TMO(t)) {
  13032. pt_debug(cd->dev, DL_ERROR,
  13033. "%s: TMO waiting for FW sentinel\n", __func__);
  13034. rc = -ETIME;
  13035. }
  13036. return scnprintf(buf, strlen(buf),
  13037. "Status: %d\n"
  13038. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13039. }
  13040. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13041. /*******************************************************************************
  13042. * FUNCTION: pt_pip2_gpio_read_show
  13043. *
  13044. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13045. * contents of the response to the passed in output buffer.
  13046. *
  13047. * RETURN: size of data written to sysfs node
  13048. *
  13049. * PARAMETERS:
  13050. * *dev - pointer to device structure
  13051. * *attr - pointer to device attributes structure
  13052. * *buf - pointer to print output buffer
  13053. ******************************************************************************/
  13054. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13055. struct device_attribute *attr, char *buf)
  13056. {
  13057. struct pt_core_data *cd = dev_get_drvdata(dev);
  13058. u8 status = 0;
  13059. u32 gpio_value = 0;
  13060. int rc = 0;
  13061. /* This functionality is only available in the BL */
  13062. if (cd->mode == PT_MODE_BOOTLOADER)
  13063. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13064. else
  13065. rc = -EPERM;
  13066. if (!rc) {
  13067. if (status == 0)
  13068. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13069. "Status: %d\n"
  13070. "DUT GPIO Reg: 0x%08X\n",
  13071. rc, gpio_value);
  13072. else
  13073. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13074. "Status: %d\n"
  13075. "DUT GPIO Reg: n/a\n",
  13076. status);
  13077. } else
  13078. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13079. "Status: %d\n"
  13080. "DUT GPIO Reg: n/a\n",
  13081. rc);
  13082. }
  13083. /*******************************************************************************
  13084. * FUNCTION: pt_pip2_version_show
  13085. *
  13086. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13087. * contents of the response to the passed in output buffer.
  13088. *
  13089. * RETURN: size of data written to sysfs node
  13090. *
  13091. * PARAMETERS:
  13092. * *dev - pointer to device structure
  13093. * *attr - pointer to device attributes structure
  13094. * *buf - pointer to print output buffer
  13095. ******************************************************************************/
  13096. static ssize_t pt_pip2_version_show(struct device *dev,
  13097. struct device_attribute *attr, char *buf)
  13098. {
  13099. int rc = 0;
  13100. struct pt_core_data *cd = dev_get_drvdata(dev);
  13101. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13102. rc = pt_pip2_get_version(cd);
  13103. if (!rc) {
  13104. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13105. "PIP VERSION : %02X.%02X\n"
  13106. "BL VERSION : %02X.%02X\n"
  13107. "FW VERSION : %02X.%02X\n"
  13108. "SILICON ID : %04X.%04X\n"
  13109. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13110. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13111. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13112. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13113. ttdata->chip_id, ttdata->chip_rev,
  13114. ttdata->uid[0], ttdata->uid[1],
  13115. ttdata->uid[2], ttdata->uid[3],
  13116. ttdata->uid[4], ttdata->uid[5],
  13117. ttdata->uid[6], ttdata->uid[7],
  13118. ttdata->uid[8], ttdata->uid[9],
  13119. ttdata->uid[10], ttdata->uid[11]);
  13120. } else {
  13121. pt_debug(dev, DL_ERROR,
  13122. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13123. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13124. "PIP VERSION : -\n"
  13125. "BL VERSION : -\n"
  13126. "FW VERSION : -\n"
  13127. "SILICON ID : -\n"
  13128. "UID : -\n");
  13129. }
  13130. }
  13131. #ifdef TTDL_DIAGNOSTICS
  13132. /*******************************************************************************
  13133. * FUNCTION: pt_ttdl_status_show
  13134. *
  13135. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13136. * variable states and GPIO levels. Additional information printed when
  13137. * TTDL_DIAGNOSTICS is enabled.
  13138. *
  13139. * NOTE: All counters will be reset to 0 when this function is called.
  13140. *
  13141. * RETURN:
  13142. * 0 = success
  13143. * !0 = failure
  13144. *
  13145. * PARAMETERS:
  13146. * *dev - pointer to device structure
  13147. * *attr - pointer to device attributes
  13148. * *buf - pointer to output buffer
  13149. ******************************************************************************/
  13150. static ssize_t pt_ttdl_status_show(struct device *dev,
  13151. struct device_attribute *attr, char *buf)
  13152. {
  13153. struct pt_core_data *cd = dev_get_drvdata(dev);
  13154. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13155. struct i2c_client *client = to_i2c_client(dev);
  13156. ssize_t ret;
  13157. u16 cal_size = 0;
  13158. unsigned short crc = 0;
  13159. if (cd->cal_cache_in_host)
  13160. _pt_manage_local_cal_data(dev,
  13161. PT_CAL_DATA_INFO, &cal_size, &crc);
  13162. ret = scnprintf(buf, strlen(buf),
  13163. "%s: 0x%04X\n"
  13164. "%s: %d\n"
  13165. "%s: %s\n"
  13166. "%s: %s %s\n"
  13167. "%s: %s\n"
  13168. "%s: 0x%02X\n"
  13169. "%s: %s\n"
  13170. "%s: %s\n"
  13171. "%s: %s\n"
  13172. "%s: %s\n"
  13173. "%s: %d\n"
  13174. "%s: %d\n"
  13175. "%s: %s\n"
  13176. "%s: %s\n"
  13177. "%s: %d\n"
  13178. #ifdef TTDL_DIAGNOSTICS
  13179. "%s: %d\n"
  13180. "%s: %d\n"
  13181. "%s: %d\n"
  13182. "%s: %d\n"
  13183. "%s: %d\n"
  13184. "%s: %d\n"
  13185. "%s: %d\n"
  13186. "%s: %d\n"
  13187. "%s: %d\n"
  13188. "%s: %d\n"
  13189. "%s: %s\n"
  13190. "%s: %s\n"
  13191. "%s: %s\n"
  13192. "%s: %d\n"
  13193. "%s: 0x%04X\n"
  13194. "%s: %s\n"
  13195. #endif /* TTDL_DIAGNOSTICS */
  13196. ,
  13197. "Startup Status ", cd->startup_status,
  13198. "TTDL Debug Level ", cd->debug_level,
  13199. "Mode ",
  13200. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13201. "Operational" : "BL") : "Unknown",
  13202. "DUT Generation ",
  13203. cd->active_dut_generation ?
  13204. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13205. "PT TC/TT" : "Gen5/6") : "Unknown",
  13206. cd->active_dut_generation ?
  13207. (cd->set_dut_generation == true ?
  13208. "(Set)" : "(Detected)") : "",
  13209. "HW Detected ",
  13210. cd->hw_detected ? "True" : "False",
  13211. "I2C Address ",
  13212. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13213. "Active Bus Module ",
  13214. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13215. "Flashless Mode ",
  13216. cd->flashless_dut == 1 ? "Yes" : "No",
  13217. "GPIO state - IRQ ",
  13218. cd->cpdata->irq_stat ?
  13219. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13220. "High" : "Low") : "not defined",
  13221. "GPIO state - TP_XRES ",
  13222. pdata->core_pdata->rst_gpio ?
  13223. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13224. "High" : "Low") : "not defined",
  13225. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13226. "Startup Retry Count ", cd->startup_retry_count,
  13227. "WD - Manual Force Stop ",
  13228. cd->watchdog_force_stop ? "True" : "False",
  13229. "WD - Enabled ",
  13230. cd->watchdog_enabled ? "True" : "False",
  13231. "WD - Interval (ms) ", cd->watchdog_interval
  13232. #ifdef TTDL_DIAGNOSTICS
  13233. , "WD - Triggered Count ", cd->watchdog_count,
  13234. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13235. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13236. "WD - XRES Count ", cd->wd_xres_count,
  13237. "IRQ Triggered Count ", cd->irq_count,
  13238. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13239. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13240. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13241. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13242. "Error GPIO trigger type ", cd->err_gpio_type,
  13243. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13244. "Suppress No-Flash Auto BL ",
  13245. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13246. "Yes" : "No",
  13247. "Calibration Cache on host ",
  13248. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13249. "Yes" : "No",
  13250. "Calibration Cache size ", cal_size,
  13251. "Calibration Cache chip ID ", crc,
  13252. "Multi-Chip Support ",
  13253. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13254. #endif /* TTDL_DIAGNOSTICS */
  13255. );
  13256. #ifdef TTDL_DIAGNOSTICS
  13257. /* Reset all diagnostic counters */
  13258. cd->watchdog_count = 0;
  13259. cd->watchdog_irq_stuck_count = 0;
  13260. cd->watchdog_failed_access_count = 0;
  13261. cd->wd_xres_count = 0;
  13262. cd->irq_count = 0;
  13263. cd->bl_retry_packet_count = 0;
  13264. cd->pip2_crc_error_count = 0;
  13265. cd->bus_transmit_error_count = 0;
  13266. #endif
  13267. return ret;
  13268. }
  13269. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13270. /*******************************************************************************
  13271. * FUNCTION: pt_pip2_enter_bl_show
  13272. *
  13273. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13274. * the DUT into the BL and show the success or failure of entering the BL
  13275. *
  13276. * RETURN:
  13277. * 0 = success
  13278. * !0 = failure
  13279. *
  13280. * PARAMETERS:
  13281. * *dev - pointer to device structure
  13282. * *attr - pointer to device attributes
  13283. * *buf - pointer to output buffer
  13284. ******************************************************************************/
  13285. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13286. struct device_attribute *attr, char *buf)
  13287. {
  13288. ssize_t ret = 0;
  13289. int rc = 0;
  13290. int result = 0;
  13291. u8 mode = PT_MODE_UNKNOWN;
  13292. struct pt_core_data *cd = dev_get_drvdata(dev);
  13293. /* Turn off the TTDL WD before enter bootloader */
  13294. pt_stop_wd_timer(cd);
  13295. /* Ensure NO enumeration work is queued or will be queued */
  13296. cancel_work_sync(&cd->enum_work);
  13297. mutex_lock(&cd->system_lock);
  13298. cd->bridge_mode = true;
  13299. mutex_unlock(&cd->system_lock);
  13300. /* set mode to operational to avoid any extra PIP traffic */
  13301. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13302. switch (result) {
  13303. case PT_ENTER_BL_PASS:
  13304. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13305. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13306. break;
  13307. case PT_ENTER_BL_ERROR:
  13308. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13309. rc,
  13310. " Unknown Error");
  13311. break;
  13312. case PT_ENTER_BL_RESET_FAIL:
  13313. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13314. rc,
  13315. " Soft Reset Failed");
  13316. break;
  13317. case PT_ENTER_BL_HID_START_BL_FAIL:
  13318. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13319. rc,
  13320. " PIP Start BL Cmd Failed");
  13321. break;
  13322. case PT_ENTER_BL_CONFIRM_FAIL:
  13323. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13324. rc,
  13325. " Error confirming DUT entered BL");
  13326. break;
  13327. default:
  13328. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13329. rc, " Unknown Error");
  13330. break;
  13331. };
  13332. /* Allow enumeration work to be queued again */
  13333. cd->bridge_mode = false;
  13334. return ret;
  13335. }
  13336. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13337. /*******************************************************************************
  13338. * FUNCTION: pt_pip2_exit_bl_show
  13339. *
  13340. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13341. * launch the APP and put the DUT Application mode
  13342. *
  13343. * RETURN:
  13344. * 0 = success
  13345. * !0 = failure
  13346. *
  13347. * PARAMETERS:
  13348. * *dev - pointer to device structure
  13349. * *attr - pointer to device attributes
  13350. * *buf - pointer to output buffer
  13351. ******************************************************************************/
  13352. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13353. struct device_attribute *attr, char *buf)
  13354. {
  13355. struct pt_core_data *cd = dev_get_drvdata(dev);
  13356. ssize_t ret = 0;
  13357. int rc = 0;
  13358. u8 status_str[PT_STATUS_STR_LEN];
  13359. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13360. /*
  13361. * Perform enum if startup_status doesn't reach to
  13362. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13363. */
  13364. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13365. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13366. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13367. strlcpy(status_str,
  13368. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13369. }
  13370. }
  13371. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13372. rc, status_str);
  13373. return ret;
  13374. }
  13375. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13376. #endif
  13377. #ifdef EASYWAKE_TSG6
  13378. /*******************************************************************************
  13379. * FUNCTION: pt_easy_wakeup_gesture_show
  13380. *
  13381. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13382. * current easy wakeup gesture
  13383. *
  13384. * RETURN:
  13385. * 0 = success
  13386. * !0 = failure
  13387. *
  13388. * PARAMETERS:
  13389. * *dev - pointer to device structure
  13390. * *attr - pointer to device attributes
  13391. * *buf - pointer to output buffer
  13392. ******************************************************************************/
  13393. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13394. struct device_attribute *attr, char *buf)
  13395. {
  13396. struct pt_core_data *cd = dev_get_drvdata(dev);
  13397. ssize_t ret;
  13398. mutex_lock(&cd->system_lock);
  13399. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13400. cd->easy_wakeup_gesture);
  13401. mutex_unlock(&cd->system_lock);
  13402. return ret;
  13403. }
  13404. /*******************************************************************************
  13405. * FUNCTION: pt_easy_wakeup_gesture_store
  13406. *
  13407. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13408. * allows the wake gesture to be set to a custom value.
  13409. *
  13410. * RETURN: Size of passed in buffer is success
  13411. *
  13412. * PARAMETERS:
  13413. * *dev - pointer to device structure
  13414. * *attr - pointer to device attributes
  13415. * *buf - pointer to buffer that hold the command parameters
  13416. * size - size of buf
  13417. ******************************************************************************/
  13418. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13419. struct device_attribute *attr, const char *buf, size_t size)
  13420. {
  13421. struct pt_core_data *cd = dev_get_drvdata(dev);
  13422. u32 input_data[2];
  13423. int length;
  13424. int rc = 0;
  13425. /* Maximum input of one value */
  13426. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13427. ARRAY_SIZE(input_data));
  13428. if (length != 1) {
  13429. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13430. __func__);
  13431. rc = -EINVAL;
  13432. goto exit;
  13433. }
  13434. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13435. __func__, cd->features.easywake);
  13436. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13437. rc = -EINVAL;
  13438. goto exit;
  13439. }
  13440. pm_runtime_get_sync(dev);
  13441. mutex_lock(&cd->system_lock);
  13442. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13443. cd->easy_wakeup_gesture = (u8)input_data[0];
  13444. pt_debug(dev, DL_INFO,
  13445. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13446. __func__, cd->easy_wakeup_gesture);
  13447. } else
  13448. rc = -ENODEV;
  13449. mutex_unlock(&cd->system_lock);
  13450. pm_runtime_put(dev);
  13451. exit:
  13452. if (rc)
  13453. return rc;
  13454. return size;
  13455. }
  13456. /*******************************************************************************
  13457. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13458. *
  13459. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13460. * show the TSG6 easywake gesture ID
  13461. *
  13462. * RETURN:
  13463. * 0 = success
  13464. * !0 = failure
  13465. *
  13466. * PARAMETERS:
  13467. * *dev - pointer to device structure
  13468. * *attr - pointer to device attributes
  13469. * *buf - pointer to output buffer
  13470. ******************************************************************************/
  13471. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13472. struct device_attribute *attr, char *buf)
  13473. {
  13474. struct pt_core_data *cd = dev_get_drvdata(dev);
  13475. ssize_t ret;
  13476. mutex_lock(&cd->system_lock);
  13477. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13478. cd->gesture_id);
  13479. mutex_unlock(&cd->system_lock);
  13480. return ret;
  13481. }
  13482. /*******************************************************************************
  13483. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13484. *
  13485. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13486. * show the TSG6 easywake gesture data in the following format:
  13487. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13488. *
  13489. * RETURN:
  13490. * 0 = success
  13491. * !0 = failure
  13492. *
  13493. * PARAMETERS:
  13494. * *dev - pointer to device structure
  13495. * *attr - pointer to device attributes
  13496. * *buf - pointer to output buffer
  13497. ******************************************************************************/
  13498. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13499. struct device_attribute *attr, char *buf)
  13500. {
  13501. struct pt_core_data *cd = dev_get_drvdata(dev);
  13502. ssize_t ret = 0;
  13503. int i;
  13504. mutex_lock(&cd->system_lock);
  13505. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13506. for (i = 0; i < cd->gesture_data_length; i++)
  13507. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13508. "0x%02X\n", cd->gesture_data[i]);
  13509. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13510. "(%d bytes)\n", cd->gesture_data_length);
  13511. mutex_unlock(&cd->system_lock);
  13512. return ret;
  13513. }
  13514. #endif /* EASYWAKE_TSG6 */
  13515. #ifdef TTDL_DIAGNOSTICS
  13516. /*******************************************************************************
  13517. * FUNCTION: pt_err_gpio_show
  13518. *
  13519. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13520. * setting up the gpio was successful
  13521. *
  13522. * RETURN: Char buffer with printed GPIO creation state
  13523. *
  13524. * PARAMETERS:
  13525. * *dev - pointer to device structure
  13526. * *attr - pointer to device attributes
  13527. * *buf - pointer to output buffer
  13528. ******************************************************************************/
  13529. static ssize_t pt_err_gpio_show(struct device *dev,
  13530. struct device_attribute *attr, char *buf)
  13531. {
  13532. struct pt_core_data *cd = dev_get_drvdata(dev);
  13533. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13534. "Err GPIO (%d) : %s\n"
  13535. "Err GPIO trig type: %d\n",
  13536. cd->err_gpio,
  13537. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13538. "HIGH" : "low") : "not defined"),
  13539. cd->err_gpio_type);
  13540. }
  13541. /*******************************************************************************
  13542. * FUNCTION: pt_err_gpio_store
  13543. *
  13544. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13545. * available host GPIO to be used to trigger when TTDL detects a PIP
  13546. * command/response timeout.
  13547. *
  13548. * RETURN:
  13549. * 0 = success
  13550. * !0 = failure
  13551. *
  13552. * PARAMETERS:
  13553. * *dev - pointer to device structure
  13554. * *attr - pointer to device attributes
  13555. * *buf - pointer to buffer that hold the command parameters
  13556. * size - size of buf
  13557. ******************************************************************************/
  13558. static ssize_t pt_err_gpio_store(struct device *dev,
  13559. struct device_attribute *attr, const char *buf, size_t size)
  13560. {
  13561. struct pt_core_data *cd = dev_get_drvdata(dev);
  13562. unsigned long gpio;
  13563. int rc = 0;
  13564. u32 input_data[3];
  13565. int length;
  13566. u8 err_type;
  13567. input_data[0] = 0;
  13568. input_data[1] = 0;
  13569. /* Maximmum input is two elements */
  13570. length = _pt_ic_parse_input(dev, buf, size,
  13571. input_data, ARRAY_SIZE(input_data));
  13572. if (length < 1 || length > 2) {
  13573. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13574. __func__);
  13575. rc = -EINVAL;
  13576. goto exit;
  13577. }
  13578. gpio = input_data[0];
  13579. err_type = (u8)input_data[1];
  13580. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13581. rc = -EINVAL;
  13582. goto exit;
  13583. }
  13584. mutex_lock(&cd->system_lock);
  13585. gpio_free(gpio);
  13586. rc = gpio_request(gpio, NULL);
  13587. if (rc) {
  13588. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13589. rc = -ENODEV;
  13590. } else {
  13591. cd->err_gpio = gpio;
  13592. cd->err_gpio_type = err_type;
  13593. gpio_direction_output(gpio, 0);
  13594. }
  13595. mutex_unlock(&cd->system_lock);
  13596. exit:
  13597. if (rc)
  13598. return rc;
  13599. return size;
  13600. }
  13601. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13602. pt_err_gpio_store);
  13603. /*******************************************************************************
  13604. * FUNCTION: pt_drv_irq_show
  13605. *
  13606. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13607. * TTDL interrupt is enabled/disabled
  13608. *
  13609. * RETURN:
  13610. * 0 = success
  13611. * !0 = failure
  13612. *
  13613. * PARAMETERS:
  13614. * *dev - pointer to device structure
  13615. * *attr - pointer to device attributes
  13616. * *buf - pointer to output buffer
  13617. ******************************************************************************/
  13618. static ssize_t pt_drv_irq_show(struct device *dev,
  13619. struct device_attribute *attr, char *buf)
  13620. {
  13621. struct pt_core_data *cd = dev_get_drvdata(dev);
  13622. ssize_t ret = 0;
  13623. mutex_lock(&cd->system_lock);
  13624. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13625. if (cd->irq_enabled)
  13626. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13627. "Driver interrupt: ENABLED\n");
  13628. else
  13629. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13630. "Driver interrupt: DISABLED\n");
  13631. mutex_unlock(&cd->system_lock);
  13632. return ret;
  13633. }
  13634. /*******************************************************************************
  13635. * FUNCTION: pt_drv_irq_store
  13636. *
  13637. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13638. * IRQ to be enabled/disabled.
  13639. *
  13640. * RETURN: Size of passed in buffer
  13641. *
  13642. * PARAMETERS:
  13643. * *dev - pointer to device structure
  13644. * *attr - pointer to device attributes
  13645. * *buf - pointer to buffer that hold the command parameters
  13646. * size - size of buf
  13647. ******************************************************************************/
  13648. static ssize_t pt_drv_irq_store(struct device *dev,
  13649. struct device_attribute *attr, const char *buf, size_t size)
  13650. {
  13651. struct pt_core_data *cd = dev_get_drvdata(dev);
  13652. u32 input_data[2];
  13653. int length;
  13654. int rc = 0;
  13655. /* Maximum input of one value */
  13656. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13657. ARRAY_SIZE(input_data));
  13658. if (length != 1) {
  13659. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13660. __func__);
  13661. rc = -EINVAL;
  13662. goto exit;
  13663. }
  13664. mutex_lock(&cd->system_lock);
  13665. switch (input_data[0]) {
  13666. case 0:
  13667. if (cd->irq_enabled) {
  13668. cd->irq_enabled = false;
  13669. /* Disable IRQ has no return value to check */
  13670. disable_irq_nosync(cd->irq);
  13671. pt_debug(dev, DL_INFO,
  13672. "%s: Driver IRQ now disabled\n",
  13673. __func__);
  13674. } else
  13675. pt_debug(dev, DL_INFO,
  13676. "%s: Driver IRQ already disabled\n",
  13677. __func__);
  13678. break;
  13679. case 1:
  13680. if (cd->irq_enabled == false) {
  13681. cd->irq_enabled = true;
  13682. enable_irq(cd->irq);
  13683. pt_debug(dev, DL_INFO,
  13684. "%s: Driver IRQ now enabled\n",
  13685. __func__);
  13686. } else
  13687. pt_debug(dev, DL_INFO,
  13688. "%s: Driver IRQ already enabled\n",
  13689. __func__);
  13690. break;
  13691. default:
  13692. rc = -EINVAL;
  13693. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13694. }
  13695. mutex_unlock(&(cd->system_lock));
  13696. exit:
  13697. if (rc)
  13698. return rc;
  13699. return size;
  13700. }
  13701. /*******************************************************************************
  13702. * FUNCTION: pt_pip2_bin_hdr_show
  13703. *
  13704. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  13705. * the bin file header from flash and show each field
  13706. *
  13707. * RETURN:
  13708. * 0 = success
  13709. * !0 = failure
  13710. *
  13711. * PARAMETERS:
  13712. * *dev - pointer to device structure
  13713. * *attr - pointer to device attributes
  13714. * *buf - pointer to output buffer
  13715. ******************************************************************************/
  13716. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  13717. struct device_attribute *attr, char *buf)
  13718. {
  13719. ssize_t ret = 0;
  13720. struct pt_bin_file_hdr hdr = {0};
  13721. int rc;
  13722. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  13723. ret = scnprintf(buf, strlen(buf),
  13724. "%s: %d\n"
  13725. "%s: %d\n"
  13726. "%s: 0x%04X\n"
  13727. "%s: %d\n"
  13728. "%s: %d\n"
  13729. "%s: %d\n"
  13730. "%s: 0x%08X\n"
  13731. "%s: 0x%04X\n"
  13732. "%s: 0x%04X\n"
  13733. "%s: %d\n"
  13734. "%s: %d\n",
  13735. "Status", rc,
  13736. "Header Length ", hdr.length,
  13737. "TTPID ", hdr.ttpid,
  13738. "FW Major Ver ", hdr.fw_major,
  13739. "FW Minor Ver ", hdr.fw_minor,
  13740. "FW Rev Control ", hdr.fw_rev_ctrl,
  13741. "FW CRC ", hdr.fw_crc,
  13742. "Silicon Rev ", hdr.si_rev,
  13743. "Silicon ID ", hdr.si_id,
  13744. "Config Ver ", hdr.config_ver,
  13745. "HEX File Size ", hdr.hex_file_size
  13746. );
  13747. return ret;
  13748. }
  13749. /*******************************************************************************
  13750. * FUNCTION: pt_platform_data_show
  13751. *
  13752. * SUMMARY: Show method for the platform_data sysfs node that will show the
  13753. * active platform data including: GPIOs, Vendor and Product IDs,
  13754. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  13755. * HID registers, and Easy wake gesture
  13756. *
  13757. * RETURN: Size of printed data
  13758. *
  13759. * PARAMETERS:
  13760. * *dev - pointer to device structure
  13761. * *attr - pointer to device attributes
  13762. * *buf - pointer to output buffer
  13763. ******************************************************************************/
  13764. static ssize_t pt_platform_data_show(struct device *dev,
  13765. struct device_attribute *attr, char *buf)
  13766. {
  13767. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13768. struct pt_core_data *cd = dev_get_drvdata(dev);
  13769. ssize_t ret;
  13770. ret = scnprintf(buf, strlen(buf),
  13771. "%s: %d\n"
  13772. "%s: %d\n"
  13773. "%s: %d\n"
  13774. "%s: %d\n"
  13775. "%s: %d\n"
  13776. "%s: %d\n"
  13777. "%s: %d\n"
  13778. "%s: %d\n"
  13779. "%s: %d\n"
  13780. "%s: %d\n"
  13781. "%s: %d\n"
  13782. "%s: %d\n"
  13783. "%s: %d\n"
  13784. "%s: %d\n"
  13785. "%s: %d\n"
  13786. "%s: %d\n"
  13787. "%s: %d\n"
  13788. "%s: %d\n"
  13789. "%s: %d\n"
  13790. "%s: %d\n"
  13791. "%s: %d\n"
  13792. "%s: %d\n"
  13793. "%s: %d\n"
  13794. "%s: %d\n"
  13795. "%s: %d\n"
  13796. "%s: %d\n"
  13797. "%s: %d\n"
  13798. "%s: %s\n"
  13799. "%s: %s\n"
  13800. "%s: %d\n",
  13801. "Status", 0,
  13802. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  13803. "Interrupt GPIO Value ",
  13804. pdata->core_pdata->irq_gpio ?
  13805. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  13806. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  13807. "Reset GPIO Value ",
  13808. pdata->core_pdata->rst_gpio ?
  13809. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  13810. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  13811. "DDI Reset GPIO Value ",
  13812. pdata->core_pdata->ddi_rst_gpio ?
  13813. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  13814. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  13815. "VDDI GPIO Value ",
  13816. pdata->core_pdata->vddi_gpio ?
  13817. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  13818. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  13819. "VCC GPIO Value ",
  13820. pdata->core_pdata->vcc_gpio ?
  13821. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  13822. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  13823. "AVDD GPIO Value ",
  13824. pdata->core_pdata->avdd_gpio ?
  13825. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  13826. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  13827. "AVEE GPIO Value ",
  13828. pdata->core_pdata->avee_gpio ?
  13829. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  13830. "Vendor ID ", pdata->core_pdata->vendor_id,
  13831. "Product ID ", pdata->core_pdata->product_id,
  13832. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  13833. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  13834. "Core data flags ", pdata->core_pdata->flags,
  13835. "MT data flags ", pdata->mt_pdata->flags,
  13836. "Loader data flags ", pdata->loader_pdata->flags,
  13837. "Level trigger delay (us) ",
  13838. pdata->core_pdata->level_irq_udelay,
  13839. "HID Descriptor Register ",
  13840. pdata->core_pdata->hid_desc_register,
  13841. "HID Output Register ",
  13842. cd->hid_desc.output_register,
  13843. "HID Command Register ",
  13844. cd->hid_desc.command_register,
  13845. "Easy wakeup gesture ",
  13846. pdata->core_pdata->easy_wakeup_gesture,
  13847. "Config DUT generation ",
  13848. pdata->core_pdata->config_dut_generation ?
  13849. (pdata->core_pdata->config_dut_generation ==
  13850. CONFIG_DUT_PIP2_CAPABLE ?
  13851. "PT TC/TT" : "Gen5/6") : "Auto",
  13852. "Watchdog Force Stop ",
  13853. pdata->core_pdata->watchdog_force_stop ?
  13854. "True" : "False",
  13855. "Panel ID Support ",
  13856. pdata->core_pdata->panel_id_support);
  13857. return ret;
  13858. }
  13859. #define PT_ERR_STR_SIZE 64
  13860. /*******************************************************************************
  13861. * FUNCTION: pt_bist_bus_test
  13862. *
  13863. * SUMMARY: Tests the connectivity of the active bus pins:
  13864. * I2C - SDA and SCL
  13865. * SPI - MOSI, MISO, CLK
  13866. *
  13867. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  13868. * bus. If any data is read we know the I2C/SPI pins are connected
  13869. *
  13870. * RETURN:
  13871. * 0 = Success
  13872. * !0 = Failure
  13873. *
  13874. * PARAMETERS:
  13875. * *dev - pointer to device structure
  13876. * *net_toggled - pointer to where to store if bus toggled
  13877. * *err_str - pointer to error string buffer
  13878. ******************************************************************************/
  13879. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  13880. {
  13881. struct pt_core_data *cd = dev_get_drvdata(dev);
  13882. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  13883. u8 *read_buf = NULL;
  13884. int bytes_read = 0;
  13885. int rc = 0;
  13886. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13887. if (read_buf == NULL) {
  13888. rc = -ENOMEM;
  13889. goto exit;
  13890. }
  13891. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13892. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  13893. disable_irq(cd->irq);
  13894. mutex_lock(&cd->system_lock);
  13895. cd->irq_disabled = true;
  13896. mutex_unlock(&cd->system_lock);
  13897. /*
  13898. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  13899. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  13900. */
  13901. usleep_range(5000, 6000);
  13902. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  13903. __func__, (int)sizeof(ver_cmd));
  13904. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  13905. ">>> User CMD");
  13906. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  13907. if (rc) {
  13908. pt_debug(dev, DL_ERROR,
  13909. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  13910. *net_toggled = 0;
  13911. strlcpy(err_str,
  13912. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  13913. goto exit_enable_irq;
  13914. }
  13915. usleep_range(4000, 5000);
  13916. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  13917. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  13918. __func__, bytes_read);
  13919. if (bytes_read == 0) {
  13920. *net_toggled = 0;
  13921. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  13922. __func__);
  13923. strlcpy(err_str,
  13924. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13925. rc = -EIO;
  13926. goto exit_enable_irq;
  13927. } else {
  13928. if (cd->bus_ops->bustype == BUS_I2C)
  13929. *net_toggled = 1;
  13930. else {
  13931. if ((bytes_read > 3) &&
  13932. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  13933. PIP2_CMD_ID_VERSION)
  13934. *net_toggled = 1;
  13935. else {
  13936. *net_toggled = 0;
  13937. pt_debug(dev, DL_INFO,
  13938. "%s: BUS Read Failed, %d bytes read\n",
  13939. __func__, bytes_read);
  13940. strlcpy(err_str,
  13941. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13942. }
  13943. }
  13944. }
  13945. exit_enable_irq:
  13946. enable_irq(cd->irq);
  13947. usleep_range(5000, 6000);
  13948. mutex_lock(&cd->system_lock);
  13949. cd->irq_disabled = false;
  13950. mutex_unlock(&cd->system_lock);
  13951. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  13952. exit:
  13953. kfree(read_buf);
  13954. return rc;
  13955. }
  13956. /*******************************************************************************
  13957. * FUNCTION: pt_bist_irq_test
  13958. *
  13959. * SUMMARY: Tests the connectivity of the IRQ net
  13960. *
  13961. * This test will ensure there is a good connection between the host
  13962. * and the DUT on the irq pin. First determine if the IRQ is stuck
  13963. * asserted and if so keep reading messages off of the bus until
  13964. * it de-asserts. Possible outcomes:
  13965. * - IRQ was already de-asserted: Send a PIP command and if an
  13966. * interrupt is generated the test passes.
  13967. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  13968. * test passes.
  13969. * - IRQ stays asserted: After reading the bus multiple times
  13970. * the IRQ stays asserted. Likely open or shorted to GND
  13971. *
  13972. * RETURN:
  13973. * 0 = Success
  13974. * !0 = Failure
  13975. *
  13976. * PARAMETERS:
  13977. * *dev - pointer to device structure
  13978. * *bus_toggled - pointer to where to store if bus toggled
  13979. * *irq_toggled - pointer to where to store if IRQ toggled
  13980. * *xres_toggled - pointer to where to store if XRES toggled
  13981. * *err_str - pointer to error string buffer
  13982. ******************************************************************************/
  13983. static int pt_bist_irq_test(struct device *dev,
  13984. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  13985. {
  13986. struct pt_core_data *cd = dev_get_drvdata(dev);
  13987. u8 *read_buf = NULL;
  13988. u8 mode = PT_MODE_UNKNOWN;
  13989. u16 actual_read_len;
  13990. int bytes_read = 0;
  13991. int count = 0;
  13992. int rc = 0;
  13993. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13994. if (read_buf == NULL) {
  13995. rc = -ENOMEM;
  13996. goto exit;
  13997. }
  13998. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  13999. cd->irq_count = 0;
  14000. /*
  14001. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14002. * the bus based on the 2 byte initial length read. Try up to 5x.
  14003. */
  14004. while (pt_check_irq_asserted(cd) && count < 5) {
  14005. count++;
  14006. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14007. }
  14008. if (count > 1 && count < 5 && bytes_read > 0) {
  14009. /*
  14010. * IRQ was stuck but data was successfully read from the
  14011. * bus releasing the IRQ line.
  14012. */
  14013. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14014. __func__, count, bytes_read);
  14015. *bus_toggled = 1;
  14016. *irq_toggled = 1;
  14017. goto exit;
  14018. }
  14019. if (count == 5 && bytes_read == 0) {
  14020. /*
  14021. * Looped 5x and read nothing off the bus yet the IRQ is still
  14022. * asserted. Possible conditions:
  14023. * - IRQ open circuit
  14024. * - IRQ shorted to GND
  14025. * - I2C/SPI bus is disconnected
  14026. * - FW holding the pin low
  14027. * Try entering the BL to see if communication works there.
  14028. */
  14029. mode = PT_MODE_IGNORE;
  14030. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14031. if (rc) {
  14032. pt_debug(dev, DL_ERROR,
  14033. "%s Failed to enter BL\n", __func__);
  14034. strlcpy(err_str,
  14035. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14036. *irq_toggled = 0;
  14037. goto exit;
  14038. }
  14039. /*
  14040. * If original mode was operational and we successfully
  14041. * entered the BL, then the XRES net must have toggled
  14042. */
  14043. if (mode == PT_MODE_OPERATIONAL)
  14044. *xres_toggled = 1;
  14045. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14046. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14047. &actual_read_len);
  14048. if (rc) {
  14049. /*
  14050. * Could not communicate to DUT in BL mode. Save the
  14051. * error string, slim chance but the XRES test below may
  14052. * show the IRQ is actually working.
  14053. */
  14054. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14055. pt_debug(dev, DL_ERROR,
  14056. "%s: %s, count=%d bytes_read=%d\n",
  14057. __func__, err_str, count, bytes_read);
  14058. *irq_toggled = 0;
  14059. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14060. goto exit;
  14061. } else {
  14062. *bus_toggled = 1;
  14063. *irq_toggled = 1;
  14064. goto exit;
  14065. }
  14066. }
  14067. if (pt_check_irq_asserted(cd)) {
  14068. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14069. rc = -EIO;
  14070. *irq_toggled = 0;
  14071. goto exit;
  14072. }
  14073. /* Try sending a PIP command to see if we get a response */
  14074. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14075. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14076. if (rc) {
  14077. /*
  14078. * Potential IRQ issue, no communication in App mode, attempt
  14079. * the same command in the BL
  14080. */
  14081. mode = PT_MODE_IGNORE;
  14082. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14083. if (rc) {
  14084. pt_debug(dev, DL_ERROR,
  14085. "%s Failed to enter BL\n", __func__);
  14086. *irq_toggled = 0;
  14087. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14088. goto exit;
  14089. }
  14090. /*
  14091. * If original mode was operational and we successfully
  14092. * entered the BL, this will be useful info for the tp_xres
  14093. * test below.
  14094. */
  14095. if (mode == PT_MODE_OPERATIONAL)
  14096. *xres_toggled = 1;
  14097. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14098. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14099. &actual_read_len);
  14100. if (rc) {
  14101. /*
  14102. * Could not communicate in FW mode or BL mode. Save the
  14103. * error string, slim chance but the XRES test below may
  14104. * show the IRQ is actually working.
  14105. */
  14106. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14107. pt_debug(dev, DL_ERROR,
  14108. "%s: request_active_pip_prot failed\n",
  14109. __func__);
  14110. *irq_toggled = 0;
  14111. goto exit;
  14112. }
  14113. }
  14114. if (cd->irq_count > 0) {
  14115. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14116. cd->irq_count);
  14117. *bus_toggled = 1;
  14118. *irq_toggled = 1;
  14119. goto exit;
  14120. }
  14121. exit:
  14122. kfree(read_buf);
  14123. return rc;
  14124. }
  14125. /*******************************************************************************
  14126. * FUNCTION: pt_bist_xres_test
  14127. *
  14128. * SUMMARY: Tests the connectivity of the TP_XRES net
  14129. *
  14130. * This test will ensure there is a good connection between the host
  14131. * and the DUT on the tp_xres pin. The pin will be toggled to
  14132. * generate a TP reset which will cause the DUT to output a reset
  14133. * sentinel. If the reset sentinel is seen the test passes. If it is
  14134. * not seen the test will attempt to send a soft reset to simply gain
  14135. * some additional information on the failure:
  14136. * - soft reset fails to send: XRES and IRQ likely open
  14137. * - soft reset passes: XRES likely open or stuck de-asserted
  14138. * - soft reset fails: XRES likely stuck asserted
  14139. *
  14140. * RETURN:
  14141. * 0 = Success
  14142. * !0 = Failure
  14143. *
  14144. * PARAMETERS:
  14145. * *dev - pointer to device structure
  14146. * *bus_toggled - pointer to where to store if bus toggled
  14147. * *irq_toggled - pointer to where to store if IRQ toggled
  14148. * *xres_toggled - pointer to where to store if XRES toggled
  14149. * *err_str - pointer to error string buffer
  14150. ******************************************************************************/
  14151. static int pt_bist_xres_test(struct device *dev,
  14152. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14153. {
  14154. struct pt_core_data *cd = dev_get_drvdata(dev);
  14155. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14156. u8 *read_buf = NULL;
  14157. u8 mode = PT_MODE_UNKNOWN;
  14158. int rc = 0;
  14159. int t = 0;
  14160. int timeout = 300;
  14161. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14162. if (read_buf == NULL) {
  14163. rc = -ENOMEM;
  14164. goto exit;
  14165. }
  14166. /* Clear the startup bit mask, reset and enum will re-populate it */
  14167. cd->startup_status = STARTUP_STATUS_START;
  14168. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14169. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14170. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14171. rc = -ENODEV;
  14172. goto exit;
  14173. }
  14174. /* Ensure we have nothing pending on active bus */
  14175. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14176. /* Perform a hard XRES toggle and wait for reset sentinel */
  14177. mutex_lock(&cd->system_lock);
  14178. cd->hid_reset_cmd_state = 1;
  14179. mutex_unlock(&cd->system_lock);
  14180. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14181. rc = pt_hw_hard_reset(cd);
  14182. /* Set timeout to 1s for the flashless case where a BL could be done */
  14183. if (cd->flashless_dut)
  14184. timeout = 1000;
  14185. /*
  14186. * To avoid the case that next PIP command can be confused by BL/FW
  14187. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14188. * followed by "wakeup event" function can lower the failure rate.
  14189. */
  14190. t = wait_event_timeout(cd->wait_q,
  14191. ((cd->startup_status != STARTUP_STATUS_START)
  14192. && (cd->hid_reset_cmd_state == 0)),
  14193. msecs_to_jiffies(timeout));
  14194. if (IS_TMO(t)) {
  14195. pt_debug(cd->dev, DL_ERROR,
  14196. "%s: TMO waiting for sentinel\n", __func__);
  14197. *xres_toggled = 0;
  14198. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14199. /*
  14200. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14201. * To enter the BL we need to generate an XRES so first try to
  14202. * launch the applicaiton
  14203. */
  14204. if (cd->mode == PT_MODE_BOOTLOADER)
  14205. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14206. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14207. __func__);
  14208. mode = PT_MODE_IGNORE;
  14209. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14210. if (rc) {
  14211. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14212. __func__);
  14213. *xres_toggled = 0;
  14214. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14215. goto exit;
  14216. } else {
  14217. /* Wait for the BL sentinel */
  14218. t = wait_event_timeout(cd->wait_q,
  14219. (cd->startup_status != STARTUP_STATUS_START),
  14220. msecs_to_jiffies(500));
  14221. if (IS_TMO(t)) {
  14222. pt_debug(cd->dev, DL_ERROR,
  14223. "%s: TMO waiting for BL sentinel\n",
  14224. __func__);
  14225. *xres_toggled = 0;
  14226. strlcpy(err_str,
  14227. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14228. rc = -ETIME;
  14229. goto exit;
  14230. }
  14231. }
  14232. }
  14233. mutex_lock(&cd->system_lock);
  14234. cd->hid_reset_cmd_state = 0;
  14235. mutex_unlock(&cd->system_lock);
  14236. /* Look for BL or FW reset sentinels */
  14237. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14238. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14239. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14240. /* If a sentinel was seen, all nets are working */
  14241. *xres_toggled = 1;
  14242. *irq_toggled = 1;
  14243. /*
  14244. * For SPI test, bus read result can be confused as FW sentinel
  14245. * if MISO(slave) is connected to MISO(host).
  14246. */
  14247. if (cd->bus_ops->bustype == BUS_I2C)
  14248. *bus_toggled = 1;
  14249. } else {
  14250. /*
  14251. * Hard reset failed, however some additional information
  14252. * could be determined. Try a soft reset to see if DUT resets
  14253. * with the possible outcomes:
  14254. * - if it resets the line is not stuck asserted
  14255. * - if it does not reset the line could be stuck asserted
  14256. */
  14257. *xres_toggled = 0;
  14258. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14259. msleep(30);
  14260. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14261. __func__, rc);
  14262. if (rc) {
  14263. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14264. pt_debug(dev, DL_ERROR,
  14265. "%s: Hard reset failed, soft reset failed %s\n",
  14266. __func__, err_str);
  14267. goto exit;
  14268. }
  14269. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14270. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14271. strlcpy(err_str,
  14272. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14273. pt_debug(dev, DL_ERROR,
  14274. "%s: Hard reset failed, soft reset passed-%s\n",
  14275. __func__, err_str);
  14276. } else if (cd->startup_status == 0) {
  14277. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14278. pt_debug(dev, DL_ERROR,
  14279. "%s: Hard reset failed, soft reset failed-%s\n",
  14280. __func__, err_str);
  14281. } else {
  14282. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14283. pt_debug(dev, DL_ERROR,
  14284. "%s: Hard and Soft reset failed - %s\n",
  14285. __func__, err_str);
  14286. }
  14287. }
  14288. exit:
  14289. kfree(read_buf);
  14290. return rc;
  14291. }
  14292. /*******************************************************************************
  14293. * FUNCTION: pt_bist_slave_irq_test
  14294. *
  14295. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14296. *
  14297. * This test will ensure there is a good IRQ connection between the master
  14298. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14299. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14300. * saw each slave trigger the IRQ with it's reset sentinel.
  14301. *
  14302. * RETURN:
  14303. * 0 = Success
  14304. * !0 = Failure
  14305. *
  14306. * PARAMETERS:
  14307. * *dev - pointer to device structure
  14308. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14309. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14310. * *err_str - pointer to error string buffer
  14311. * *slave_detect - pointer to slave detect buffer
  14312. * *boot_err - pointer to boot_err buffer
  14313. ******************************************************************************/
  14314. static int pt_bist_slave_irq_test(struct device *dev,
  14315. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14316. u8 *slave_detect, u8 *boot_err)
  14317. {
  14318. struct pt_core_data *cd = dev_get_drvdata(dev);
  14319. u8 mode = PT_MODE_UNKNOWN;
  14320. u8 status;
  14321. u8 boot;
  14322. u8 read_buf[12];
  14323. u8 detected = 0;
  14324. u8 last_err = -1;
  14325. u16 actual_read_len;
  14326. int result = 0;
  14327. int rc = 0;
  14328. /*
  14329. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14330. * detect bits. If the DUT was in FW, entering the BL will cause an
  14331. * XRES signal which will inadvertently test the XRES net as well
  14332. */
  14333. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14334. if (rc) {
  14335. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14336. __func__, rc);
  14337. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14338. goto exit;
  14339. }
  14340. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14341. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14342. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14343. &actual_read_len);
  14344. if (!rc) {
  14345. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14346. "PIP2 STATUS");
  14347. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14348. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14349. /* Slave detect is only valid if status ok and in boot exec */
  14350. if (status == PIP2_RSP_ERR_NONE &&
  14351. boot == PIP2_STATUS_BOOT_EXEC) {
  14352. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14353. SLAVE_DETECT_MASK;
  14354. } else {
  14355. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14356. rc = -EPERM;
  14357. }
  14358. } else {
  14359. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14360. __func__);
  14361. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14362. goto exit;
  14363. }
  14364. /*
  14365. * Retrieve boot error regardless of the state of the slave detect
  14366. * bit because the IRQ could have been stuck high or low.
  14367. */
  14368. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14369. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14370. read_buf, &actual_read_len);
  14371. if (!rc) {
  14372. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14373. "PIP2 GET_LAST_ERRNO");
  14374. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14375. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14376. if (last_err) {
  14377. pt_debug(cd->dev, DL_ERROR,
  14378. "%s: Master Boot Last Err = 0x%02X\n",
  14379. __func__, last_err);
  14380. }
  14381. } else {
  14382. pt_debug(cd->dev, DL_ERROR,
  14383. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14384. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14385. }
  14386. exit:
  14387. pt_debug(cd->dev, DL_INFO,
  14388. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14389. __func__, rc, detected, last_err);
  14390. if (err_str && last_err) {
  14391. if (detected)
  14392. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14393. "- Likely stuck low. Boot Error:",
  14394. last_err);
  14395. else
  14396. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14397. "- Likely stuck high. Boot Error:",
  14398. last_err);
  14399. }
  14400. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14401. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14402. last_err = PIP2_RSP_ERR_NONE;
  14403. if (slave_irq_toggled)
  14404. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14405. if (slave_bus_toggled) {
  14406. /* Leave as UNTEST if slave not detected */
  14407. if (detected)
  14408. *slave_bus_toggled = !last_err ? true : false;
  14409. }
  14410. if (slave_detect)
  14411. *slave_detect = detected;
  14412. if (boot_err)
  14413. *boot_err = last_err;
  14414. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14415. __func__,
  14416. "Detected", detected,
  14417. "slave_irq_toggled", *slave_irq_toggled,
  14418. "slave_bus_toggled", *slave_bus_toggled);
  14419. return rc;
  14420. }
  14421. /*******************************************************************************
  14422. * FUNCTION: pt_bist_slave_xres_test
  14423. *
  14424. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14425. *
  14426. * This test will ensure there is a good TP_XRES connection between the
  14427. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14428. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14429. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14430. * it's reset sentinel.
  14431. *
  14432. * RETURN:
  14433. * 0 = Success
  14434. * !0 = Failure
  14435. *
  14436. * PARAMETERS:
  14437. * *dev - pointer to device structure
  14438. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14439. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14440. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14441. * *err_str - pointer to error string buffer
  14442. ******************************************************************************/
  14443. static int pt_bist_slave_xres_test(struct device *dev,
  14444. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14445. u8 *err_str)
  14446. {
  14447. struct pt_core_data *cd = dev_get_drvdata(dev);
  14448. u8 slave_detect = 0;
  14449. u8 boot_err = 0;
  14450. int rc = 0;
  14451. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14452. mutex_lock(&cd->system_lock);
  14453. cd->hid_reset_cmd_state = 1;
  14454. mutex_unlock(&cd->system_lock);
  14455. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14456. pt_hw_hard_reset(cd);
  14457. msleep(100);
  14458. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14459. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14460. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14461. if (!rc && *slave_irq_toggled == false) {
  14462. /*
  14463. * If the slave IRQ did not toggle, either the slave_detect
  14464. * bit was not set or we had a boot error. If the slave
  14465. * detect was not set the slave did not reset causing a boot
  14466. * error.
  14467. */
  14468. if (!slave_detect)
  14469. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14470. else
  14471. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14472. "- likely open or an IRQ issue. Boot Error:",
  14473. boot_err);
  14474. }
  14475. if (slave_xres_toggled) {
  14476. if (!rc)
  14477. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14478. else
  14479. *slave_xres_toggled = false;
  14480. }
  14481. return rc;
  14482. }
  14483. /*******************************************************************************
  14484. * FUNCTION: pt_bist_slave_bus_test
  14485. *
  14486. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14487. *
  14488. * This test will ensure a good SPI bus connection between the
  14489. * master DUT and the slave DUT(s). This bus connection is ensured by
  14490. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14491. * then there is a master/slave communication issue. Opening file 0 on
  14492. * the master will open it on the slave as well if the slave was detected.
  14493. *
  14494. * RETURN:
  14495. * 0 = Success
  14496. * !0 = Failure
  14497. *
  14498. * PARAMETERS:
  14499. * *dev - pointer to device structure
  14500. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14501. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14502. * *err_str - pointer to error string buffer
  14503. ******************************************************************************/
  14504. static int pt_bist_slave_bus_test(struct device *dev,
  14505. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14506. {
  14507. struct pt_core_data *cd = dev_get_drvdata(dev);
  14508. u8 mode = PT_MODE_UNKNOWN;
  14509. u8 bus_toggled = false;
  14510. u8 file_handle;
  14511. int result = 0;
  14512. int rc = 0;
  14513. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14514. if (rc) {
  14515. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14516. __func__, rc);
  14517. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14518. goto exit;
  14519. }
  14520. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14521. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14522. if (file_handle != PIP2_RAM_FILE) {
  14523. rc = -ENOENT;
  14524. bus_toggled = false;
  14525. pt_debug(dev, DL_ERROR,
  14526. "%s Failed to open bin file\n", __func__);
  14527. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14528. goto exit;
  14529. } else {
  14530. bus_toggled = true;
  14531. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14532. pt_debug(dev, DL_ERROR,
  14533. "%s: File Close failed, file_handle=%d\n",
  14534. __func__, file_handle);
  14535. }
  14536. }
  14537. exit:
  14538. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14539. if (slave_irq_toggled)
  14540. *slave_irq_toggled = bus_toggled;
  14541. if (slave_bus_toggled)
  14542. *slave_bus_toggled = bus_toggled;
  14543. return rc;
  14544. }
  14545. /*******************************************************************************
  14546. * FUNCTION: pt_ttdl_bist_show
  14547. *
  14548. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14549. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14550. *
  14551. * NOTE: This function will reset the DUT and the startup_status bit
  14552. * mask. A pt_enum will be queued after completion.
  14553. *
  14554. * NOTE: The order of the net tests is done to optimize the time it takes
  14555. * to run. The first test is capable of verifying all nets, each subsequent
  14556. * test is only run if the previous was not able to see all nets toggle.
  14557. *
  14558. * RETURN:
  14559. * 0 = success
  14560. * !0 = failure
  14561. *
  14562. * PARAMETERS:
  14563. * *dev - pointer to device structure
  14564. * *attr - pointer to device attributes
  14565. * *buf - pointer to output buffer
  14566. ******************************************************************************/
  14567. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14568. struct device_attribute *attr, char *buf)
  14569. {
  14570. struct pt_core_data *cd = dev_get_drvdata(dev);
  14571. ssize_t ret;
  14572. char *bus_err_str = NULL;
  14573. char *irq_err_str = NULL;
  14574. char *xres_err_str = NULL;
  14575. char *slave_bus_err_str = NULL;
  14576. char *slave_irq_err_str = NULL;
  14577. char *slave_xres_err_str = NULL;
  14578. u8 tests;
  14579. int rc = 0;
  14580. int num_tests = 0;
  14581. int status = 1; /* 0 = Pass, !0 = fail */
  14582. u8 bus_toggled = 0x0F; /* default to untested */
  14583. u8 i2c_toggled = 0x0F; /* default to untested */
  14584. u8 spi_toggled = 0x0F; /* default to untested */
  14585. u8 irq_toggled = 0x0F; /* default to untested */
  14586. u8 xres_toggled = 0x0F; /* default to untested */
  14587. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14588. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14589. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14590. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14591. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14592. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14593. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14594. goto print_results;
  14595. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14596. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14597. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14598. if (cd->multi_chip) {
  14599. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14600. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14601. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14602. if (!slave_bus_err_str ||
  14603. !slave_irq_err_str ||
  14604. !slave_xres_err_str)
  14605. goto print_results;
  14606. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14607. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14608. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14609. }
  14610. /* Turn off the TTDL WD during the test */
  14611. pt_stop_wd_timer(cd);
  14612. /* Shorten default PIP cmd timeout while running BIST */
  14613. cd->pip_cmd_timeout = 200;
  14614. /* Count the number of tests to run */
  14615. tests = cd->ttdl_bist_select;
  14616. while (tests) {
  14617. num_tests += tests & 1;
  14618. tests >>= 1;
  14619. }
  14620. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14621. __func__, cd->ttdl_bist_select, num_tests);
  14622. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14623. if (cd->flashless_dut) {
  14624. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14625. __func__);
  14626. mutex_lock(&cd->system_lock);
  14627. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14628. mutex_unlock(&cd->system_lock);
  14629. }
  14630. /* --------------- TP_XRES BIST TEST --------------- */
  14631. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14632. pt_debug(dev, DL_INFO,
  14633. "%s: ----- Start TP_XRES BIST -----", __func__);
  14634. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14635. &xres_toggled, xres_err_str);
  14636. /* Done if the rest of all nets toggled */
  14637. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14638. goto host_nets_complete;
  14639. }
  14640. /* Flush bus in case a PIP response is waiting from previous test */
  14641. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14642. /* --------------- IRQ BIST TEST --------------- */
  14643. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14644. pt_debug(dev, DL_INFO,
  14645. "%s: ----- Start IRQ BIST -----", __func__);
  14646. bus_toggled = 0xFF;
  14647. irq_toggled = 0xFF;
  14648. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14649. &xres_toggled, irq_err_str);
  14650. /* If this net failed clear results from previous net */
  14651. if (irq_toggled != 1) {
  14652. xres_toggled = 0x0F;
  14653. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14654. }
  14655. if (bus_toggled == 1 && irq_toggled == 1)
  14656. goto host_nets_complete;
  14657. }
  14658. /* Flush bus in case a PIP response is waiting from previous test */
  14659. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14660. /* --------------- BUS BIST TEST --------------- */
  14661. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14662. pt_debug(dev, DL_INFO,
  14663. "%s: ----- Start BUS BIST -----", __func__);
  14664. bus_toggled = 0xFF;
  14665. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14666. /* If this net failed clear results from previous net */
  14667. if (bus_toggled == 0) {
  14668. irq_toggled = 0x0F;
  14669. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14670. }
  14671. }
  14672. host_nets_complete:
  14673. /* --------------- SLAVE XRES BIST TEST --------------- */
  14674. if (cd->multi_chip &&
  14675. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  14676. pt_debug(dev, DL_INFO,
  14677. "%s: ----- Start Slave XRES BIST -----", __func__);
  14678. slave_xres_toggled = 0xFF;
  14679. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  14680. &slave_bus_toggled, &slave_xres_toggled,
  14681. slave_xres_err_str);
  14682. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  14683. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  14684. goto print_results;
  14685. }
  14686. /* --------------- SLAVE IRQ BIST TEST --------------- */
  14687. if (cd->multi_chip &&
  14688. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  14689. pt_debug(dev, DL_INFO,
  14690. "%s: ----- Start Slave IRQ BIST -----", __func__);
  14691. slave_irq_toggled = 0xFF;
  14692. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  14693. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  14694. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  14695. __func__, slave_irq_toggled);
  14696. if (slave_irq_toggled == 1) {
  14697. slave_bus_toggled = 1;
  14698. goto print_results;
  14699. }
  14700. }
  14701. /* --------------- SLAVE BUS BIST TEST --------------- */
  14702. if (cd->multi_chip &&
  14703. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  14704. pt_debug(dev, DL_INFO,
  14705. "%s: ----- Start Slave BUS BIST -----", __func__);
  14706. slave_bus_toggled = 0xFF;
  14707. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  14708. &slave_bus_toggled, slave_bus_err_str);
  14709. }
  14710. print_results:
  14711. /* Restore PIP command timeout */
  14712. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  14713. /*
  14714. * We're done! - Perform a hard XRES toggle, allowing BL
  14715. * to load FW if there is any in Flash
  14716. */
  14717. mutex_lock(&cd->system_lock);
  14718. cd->hid_reset_cmd_state = 0;
  14719. mutex_unlock(&cd->system_lock);
  14720. pt_debug(dev, DL_INFO,
  14721. "%s: TTDL BIST Complete - Final reset\n", __func__);
  14722. if (cd->flashless_dut) {
  14723. /*
  14724. * For Flashless solution, FW update is triggered after BL is
  14725. * seen that several miliseconds delay is needed.
  14726. */
  14727. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  14728. __func__);
  14729. mutex_lock(&cd->system_lock);
  14730. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  14731. mutex_unlock(&cd->system_lock);
  14732. /* Reset DUT and wait 100ms to see if loader started */
  14733. pt_hw_hard_reset(cd);
  14734. msleep(100);
  14735. if (cd->fw_updating) {
  14736. pt_debug(dev, DL_INFO,
  14737. "%s: ----- BIST Wait FW Loading ----",
  14738. __func__);
  14739. rc = _pt_request_wait_for_enum_state(
  14740. dev, 4000, STARTUP_STATUS_COMPLETE);
  14741. }
  14742. } else {
  14743. if (cd->mode == PT_MODE_BOOTLOADER) {
  14744. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14745. if (rc) {
  14746. pt_debug(dev, DL_ERROR,
  14747. "%s Failed to launch app\n", __func__);
  14748. rc = pt_hw_hard_reset(cd);
  14749. }
  14750. }
  14751. /*
  14752. * If FW exists the BL may have just started or will start soon,
  14753. * so the FW sentinel may be on it's way but with no FW it will
  14754. * not arrive, wait for it before deciding if we need to queue
  14755. * an enum.
  14756. */
  14757. rc = _pt_request_wait_for_enum_state(
  14758. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  14759. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  14760. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  14761. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  14762. __func__);
  14763. pt_queue_enum(cd);
  14764. rc = _pt_request_wait_for_enum_state(
  14765. dev, 2000, STARTUP_STATUS_COMPLETE);
  14766. }
  14767. }
  14768. msleep(20);
  14769. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  14770. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  14771. pt_start_wd_timer(cd);
  14772. /* Canned print if any memory allocation issues */
  14773. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  14774. ret = scnprintf(buf, strlen(buf),
  14775. "Status: %d\n"
  14776. "I2C (SDA,SCL): [UNTEST]\n"
  14777. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14778. "IRQ connection: [UNTEST]\n"
  14779. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  14780. if (cd->multi_chip) {
  14781. ret += scnprintf(buf + ret, strlen(buf),
  14782. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14783. "I/P IRQ connection: [UNTEST]\n"
  14784. "I/P TP_XRES connection: [UNTEST]\n");
  14785. }
  14786. } else {
  14787. status = 0;
  14788. if (bus_toggled == 1)
  14789. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14790. if (irq_toggled == 1)
  14791. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14792. if (xres_toggled == 1)
  14793. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14794. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  14795. status += bus_toggled;
  14796. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  14797. status += irq_toggled;
  14798. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  14799. status += xres_toggled;
  14800. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  14801. __func__, status, bus_toggled, irq_toggled,
  14802. xres_toggled);
  14803. if (cd->multi_chip) {
  14804. if (slave_irq_toggled == 1)
  14805. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14806. if (slave_xres_toggled == 1)
  14807. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14808. if (slave_bus_toggled == 1)
  14809. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14810. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  14811. status += slave_bus_toggled;
  14812. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  14813. status += slave_irq_toggled;
  14814. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  14815. status += slave_xres_toggled;
  14816. pt_debug(dev, DL_ERROR,
  14817. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  14818. __func__, status, bus_toggled, irq_toggled,
  14819. xres_toggled, slave_bus_toggled,
  14820. slave_irq_toggled, slave_xres_toggled);
  14821. }
  14822. if (cd->bus_ops->bustype == BUS_I2C)
  14823. i2c_toggled = bus_toggled;
  14824. else
  14825. spi_toggled = bus_toggled;
  14826. ret = scnprintf(buf, strlen(buf),
  14827. "Status: %d\n"
  14828. "I2C (SDA,SCL): %s %s\n"
  14829. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14830. "IRQ connection: %s %s\n"
  14831. "TP_XRES connection: %s %s\n",
  14832. status == num_tests ? 0 : 1,
  14833. i2c_toggled == 0x0F ? "[UNTEST]" :
  14834. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14835. i2c_toggled == 0x0F ? "" : bus_err_str,
  14836. spi_toggled == 0x0F ? "[UNTEST]" :
  14837. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14838. spi_toggled == 0x0F ? "" : bus_err_str,
  14839. irq_toggled == 0x0F ? "[UNTEST]" :
  14840. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14841. irq_err_str,
  14842. xres_toggled == 0x0F ? "[UNTEST]" :
  14843. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14844. xres_err_str);
  14845. if (cd->multi_chip) {
  14846. ret += scnprintf(buf + ret, strlen(buf),
  14847. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14848. "I/P IRQ connection: %s %s\n"
  14849. "I/P TP_XRES connection: %s %s\n",
  14850. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  14851. slave_bus_toggled == 1 ? "[ OK ]" :
  14852. "[FAILED]", slave_bus_err_str,
  14853. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  14854. slave_irq_toggled == 1 ? "[ OK ]" :
  14855. "[FAILED]", slave_irq_err_str,
  14856. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  14857. slave_xres_toggled == 1 ? "[ OK ]" :
  14858. "[FAILED]", slave_xres_err_str);
  14859. }
  14860. }
  14861. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  14862. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  14863. __func__, cd->startup_status);
  14864. kfree(bus_err_str);
  14865. kfree(irq_err_str);
  14866. kfree(xres_err_str);
  14867. return ret;
  14868. }
  14869. /*******************************************************************************
  14870. * FUNCTION: pt_ttdl_bist_store
  14871. *
  14872. * SUMMARY: Store method for the ttdl_bist sysfs node.
  14873. *
  14874. * RETURN: Size of passed in buffer if successful
  14875. *
  14876. * PARAMETERS:
  14877. * *dev - pointer to device structure
  14878. * *attr - pointer to device attributes
  14879. * *buf - pointer to command buffer
  14880. * size - size of buf
  14881. ******************************************************************************/
  14882. static ssize_t pt_ttdl_bist_store(struct device *dev,
  14883. struct device_attribute *attr, const char *buf, size_t size)
  14884. {
  14885. struct pt_core_data *cd = dev_get_drvdata(dev);
  14886. u32 input_data[2] = {0};
  14887. int length;
  14888. int rc = 0;
  14889. /* Maximum input of one value */
  14890. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14891. ARRAY_SIZE(input_data));
  14892. if (length != 1) {
  14893. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14894. __func__);
  14895. rc = -EINVAL;
  14896. goto exit;
  14897. } else {
  14898. mutex_lock(&cd->system_lock);
  14899. cd->ttdl_bist_select = input_data[0];
  14900. mutex_unlock(&cd->system_lock);
  14901. }
  14902. exit:
  14903. if (rc)
  14904. return rc;
  14905. return size;
  14906. }
  14907. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  14908. pt_ttdl_bist_store);
  14909. /*******************************************************************************
  14910. * FUNCTION: pt_flush_bus_store
  14911. *
  14912. * SUMMARY: Store method for the flush_bus sysfs node.
  14913. *
  14914. * RETURN: Size of passed in buffer if successful
  14915. *
  14916. * PARAMETERS:
  14917. * *dev - pointer to device structure
  14918. * *attr - pointer to device attributes
  14919. * *buf - pointer to command buffer
  14920. * size - size of buf
  14921. ******************************************************************************/
  14922. static ssize_t pt_flush_bus_store(struct device *dev,
  14923. struct device_attribute *attr, const char *buf, size_t size)
  14924. {
  14925. struct pt_core_data *cd = dev_get_drvdata(dev);
  14926. u32 input_data[2] = {0};
  14927. int length;
  14928. int rc = 0;
  14929. /* Maximum input of one value */
  14930. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14931. ARRAY_SIZE(input_data));
  14932. if (length != 1) {
  14933. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14934. __func__);
  14935. rc = -EINVAL;
  14936. goto exit;
  14937. }
  14938. mutex_lock(&cd->system_lock);
  14939. if (input_data[0] == 0)
  14940. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  14941. else if (input_data[0] == 1)
  14942. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  14943. else
  14944. rc = -EINVAL;
  14945. mutex_unlock(&cd->system_lock);
  14946. exit:
  14947. if (rc)
  14948. return rc;
  14949. return size;
  14950. }
  14951. /*******************************************************************************
  14952. * FUNCTION: pt_flush_bus_show
  14953. *
  14954. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  14955. * based on either the size of the first two bytes or a blind 256 bytes.
  14956. *
  14957. * RETURN:
  14958. * 0 = success
  14959. * !0 = failure
  14960. *
  14961. * PARAMETERS:
  14962. * *dev - pointer to device structure
  14963. * *attr - pointer to device attributes
  14964. * *buf - pointer to output buffer
  14965. ******************************************************************************/
  14966. static ssize_t pt_flush_bus_show(struct device *dev,
  14967. struct device_attribute *attr, char *buf)
  14968. {
  14969. ssize_t ret = 0;
  14970. ssize_t bytes = 0;
  14971. struct pt_core_data *cd = dev_get_drvdata(dev);
  14972. mutex_lock(&cd->system_lock);
  14973. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  14974. ret = scnprintf(buf, strlen(buf),
  14975. "Status: 0\n"
  14976. "%s: %zd\n",
  14977. "Bytes flushed", bytes);
  14978. mutex_unlock(&cd->system_lock);
  14979. return ret;
  14980. }
  14981. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  14982. pt_flush_bus_store);
  14983. /*******************************************************************************
  14984. * FUNCTION: pt_pip2_ping_test_store
  14985. *
  14986. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  14987. *
  14988. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  14989. * someone may want to test sending invalid packet lengths so any values
  14990. * up to 255 are allowed.
  14991. *
  14992. * RETURN: Size of passed in buffer if successful
  14993. *
  14994. * PARAMETERS:
  14995. * *dev - pointer to device structure
  14996. * *attr - pointer to device attributes
  14997. * *buf - pointer to command buffer
  14998. * size - size of buf
  14999. ******************************************************************************/
  15000. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15001. struct device_attribute *attr, const char *buf, size_t size)
  15002. {
  15003. struct pt_core_data *cd = dev_get_drvdata(dev);
  15004. u32 input_data[2];
  15005. int length;
  15006. int rc = 0;
  15007. /* Maximum input of one value */
  15008. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15009. ARRAY_SIZE(input_data));
  15010. if (length != 1) {
  15011. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15012. __func__);
  15013. rc = -EINVAL;
  15014. goto exit;
  15015. }
  15016. mutex_lock(&cd->system_lock);
  15017. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15018. cd->ping_test_size = input_data[0];
  15019. else
  15020. rc = -EINVAL;
  15021. mutex_unlock(&cd->system_lock);
  15022. exit:
  15023. if (rc)
  15024. return rc;
  15025. return size;
  15026. }
  15027. /*******************************************************************************
  15028. * FUNCTION: pt_ping_test_show
  15029. *
  15030. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15031. * PING command and ramps up the optional payload from 0 to
  15032. * ping_test_size.
  15033. * The max payload size is 247:
  15034. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15035. *
  15036. * RETURN:
  15037. * 0 = success
  15038. * !0 = failure
  15039. *
  15040. * PARAMETERS:
  15041. * *dev - pointer to device structure
  15042. * *attr - pointer to device attributes
  15043. * *buf - pointer to output buffer
  15044. ******************************************************************************/
  15045. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15046. struct device_attribute *attr, char *buf)
  15047. {
  15048. struct pt_core_data *cd = dev_get_drvdata(dev);
  15049. ssize_t ret;
  15050. int last_packet_size;
  15051. int rc = 0;
  15052. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15053. if (rc) {
  15054. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15055. return ret;
  15056. }
  15057. ret = scnprintf(buf, strlen(buf),
  15058. "Status: %d\n"
  15059. "PING payload test passed with packet sizes 0 - %d\n",
  15060. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15061. last_packet_size);
  15062. return ret;
  15063. }
  15064. /*******************************************************************************
  15065. * FUNCTION: pt_t_refresh_store
  15066. *
  15067. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15068. * in integer as the number of interrupts to count. A timer is started to
  15069. * calculate the total time it takes to see that number of interrupts.
  15070. *
  15071. * RETURN: Size of passed in buffer if successful
  15072. *
  15073. * PARAMETERS:
  15074. * *dev - pointer to device structure
  15075. * *attr - pointer to device attributes
  15076. * *buf - pointer to buffer that hold the command parameters
  15077. * size - size of buf
  15078. ******************************************************************************/
  15079. static ssize_t pt_t_refresh_store(struct device *dev,
  15080. struct device_attribute *attr, const char *buf, size_t size)
  15081. {
  15082. struct pt_core_data *cd = dev_get_drvdata(dev);
  15083. u32 input_data[2];
  15084. int length;
  15085. int rc = 0;
  15086. /* Maximum input of one value */
  15087. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15088. ARRAY_SIZE(input_data));
  15089. if (length != 1) {
  15090. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15091. __func__);
  15092. rc = -EINVAL;
  15093. goto exit;
  15094. }
  15095. mutex_lock(&cd->system_lock);
  15096. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15097. input_data[0]);
  15098. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15099. cd->t_refresh_total = input_data[0];
  15100. cd->t_refresh_count = 0;
  15101. cd->t_refresh_active = 1;
  15102. } else {
  15103. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15104. rc = -EINVAL;
  15105. }
  15106. mutex_unlock(&cd->system_lock);
  15107. exit:
  15108. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15109. if (rc)
  15110. return rc;
  15111. return size;
  15112. }
  15113. /*******************************************************************************
  15114. * FUNCTION: pt_t_refresh_show
  15115. *
  15116. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15117. * of the T-Refresh timer counting the time it takes to see a user defined
  15118. * number of interrupts.
  15119. *
  15120. * RETURN:
  15121. * 0 = success
  15122. * !0 = failure
  15123. *
  15124. * PARAMETERS:
  15125. * *dev - pointer to device structure
  15126. * *attr - pointer to device attributes
  15127. * *buf - pointer to output buffer
  15128. ******************************************************************************/
  15129. static ssize_t pt_t_refresh_show(struct device *dev,
  15130. struct device_attribute *attr, char *buf)
  15131. {
  15132. ssize_t ret = 0;
  15133. struct pt_core_data *cd = dev_get_drvdata(dev);
  15134. u32 whole;
  15135. u16 fraction;
  15136. mutex_lock(&cd->system_lock);
  15137. /* Check if we have counted the number requested */
  15138. if (cd->t_refresh_count != cd->t_refresh_total) {
  15139. ret = scnprintf(buf, strlen(buf),
  15140. "Status: 0\n"
  15141. "%s: %d\n",
  15142. "Still counting... current IRQ count",
  15143. cd->t_refresh_count);
  15144. } else {
  15145. /* Ensure T-Refresh is de-activated */
  15146. cd->t_refresh_active = 0;
  15147. whole = cd->t_refresh_time / cd->t_refresh_count;
  15148. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15149. fraction = fraction * 1000 / cd->t_refresh_count;
  15150. ret = scnprintf(buf, strlen(buf),
  15151. "Status: 0\n"
  15152. "%s: %d\n"
  15153. "%s: %d\n"
  15154. "%s: %d\n"
  15155. "%s: %d.%02d\n",
  15156. "Requested IRQ Count ", cd->t_refresh_total,
  15157. "IRQ Counted ", cd->t_refresh_count,
  15158. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15159. "Average T-Refresh (ms) ", whole, fraction);
  15160. }
  15161. mutex_unlock(&cd->system_lock);
  15162. return ret;
  15163. }
  15164. /*******************************************************************************
  15165. * FUNCTION: pt_dut_status_show
  15166. *
  15167. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15168. * more items such as operation mode,easywake state are added in the future.
  15169. *
  15170. * RETURN: Char buffer with printed scan status information
  15171. *
  15172. * PARAMETERS:
  15173. * *dev - pointer to device structure
  15174. * *attr - pointer to device attributes
  15175. * *buf - pointer to output buffer
  15176. ******************************************************************************/
  15177. static ssize_t pt_dut_status_show(struct device *dev,
  15178. struct device_attribute *attr, char *buf)
  15179. {
  15180. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15181. u8 mode = PT_MODE_UNKNOWN;
  15182. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15183. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15184. struct pt_core_data *cd = dev_get_drvdata(dev);
  15185. ssize_t ret;
  15186. u16 calculated_crc = 0;
  15187. u16 stored_crc = 0;
  15188. u8 status;
  15189. int rc = 0;
  15190. /* In STANDBY the DUT will not repond to any PIP cmd */
  15191. if (cd->fw_sys_mode_in_standby_state) {
  15192. mode = PT_MODE_OPERATIONAL;
  15193. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15194. goto print_limited_results;
  15195. }
  15196. /* Retrieve mode and FW system mode which can only be 0-4 */
  15197. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15198. if (rc || mode == PT_MODE_UNKNOWN) {
  15199. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15200. "%s: %d\n"
  15201. "%s: n/a\n"
  15202. "%s: n/a\n"
  15203. "%s: n/a\n"
  15204. "%s: n/a\n",
  15205. "Status", rc,
  15206. "Active Exec ",
  15207. "FW System Mode ",
  15208. "Stored CRC ",
  15209. "Calculated CRC ");
  15210. return ret;
  15211. } else {
  15212. if (mode == PT_MODE_OPERATIONAL) {
  15213. if (sys_mode > FW_SYS_MODE_MAX)
  15214. sys_mode = FW_SYS_MODE_UNDEFINED;
  15215. if (sys_mode != FW_SYS_MODE_TEST)
  15216. goto print_limited_results;
  15217. rc = pt_pip_verify_config_block_crc_(cd,
  15218. PT_TCH_PARM_EBID, &status,
  15219. &calculated_crc, &stored_crc);
  15220. if (rc)
  15221. goto print_limited_results;
  15222. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15223. "%s: %d\n"
  15224. "%s: %s\n"
  15225. "%s: %s\n"
  15226. "%s: 0x%04X\n"
  15227. "%s: 0x%04X\n",
  15228. "Status", rc,
  15229. "Active Exec ", "FW",
  15230. "FW System Mode ", outputstring[sys_mode],
  15231. "Stored CRC ", stored_crc,
  15232. "Calculated CRC ", calculated_crc);
  15233. return ret;
  15234. } else {
  15235. /* When in BL or unknon mode Active Exec is "n/a" */
  15236. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15237. }
  15238. }
  15239. print_limited_results:
  15240. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15241. "%s: %d\n"
  15242. "%s: %s\n"
  15243. "%s: %s\n"
  15244. "%s: n/a\n"
  15245. "%s: n/a\n",
  15246. "Status", rc,
  15247. "Active Exec ",
  15248. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15249. "FW System Mode ", outputstring[sys_mode],
  15250. "Stored CRC ",
  15251. "Calculated CRC ");
  15252. return ret;
  15253. }
  15254. #endif /* TTDL_DIAGNOSTICS */
  15255. /*******************************************************************************
  15256. * Structures of sysfs attributes for all DUT dependent sysfs node
  15257. ******************************************************************************/
  15258. static struct attribute *early_attrs[] = {
  15259. &dev_attr_hw_version.attr,
  15260. &dev_attr_drv_version.attr,
  15261. &dev_attr_drv_ver.attr,
  15262. &dev_attr_fw_version.attr,
  15263. &dev_attr_sysinfo.attr,
  15264. &dev_attr_pip2_cmd_rsp.attr,
  15265. &dev_attr_command.attr,
  15266. &dev_attr_drv_debug.attr,
  15267. &dev_attr_hw_reset.attr,
  15268. &dev_attr_response.attr,
  15269. &dev_attr_ttdl_restart.attr,
  15270. #ifdef TTDL_DIAGNOSTICS
  15271. &dev_attr_ttdl_status.attr,
  15272. &dev_attr_pip2_enter_bl.attr,
  15273. &dev_attr_pip2_exit_bl.attr,
  15274. &dev_attr_err_gpio.attr,
  15275. &dev_attr_flush_bus.attr,
  15276. &dev_attr_ttdl_bist.attr,
  15277. #endif /* TTDL_DIAGNOSTICS */
  15278. NULL,
  15279. };
  15280. static struct attribute_group early_attr_group = {
  15281. .attrs = early_attrs,
  15282. };
  15283. static struct device_attribute pip2_attributes[] = {
  15284. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15285. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15286. #ifdef TTDL_DIAGNOSTICS
  15287. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15288. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15289. pt_pip2_ping_test_store),
  15290. #endif
  15291. };
  15292. static struct device_attribute attributes[] = {
  15293. __ATTR(dut_debug, 0644,
  15294. pt_dut_debug_show, pt_drv_debug_store),
  15295. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15296. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15297. __ATTR(get_param, 0644,
  15298. pt_get_param_show, pt_get_param_store),
  15299. #ifdef EASYWAKE_TSG6
  15300. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15301. pt_easy_wakeup_gesture_store),
  15302. __ATTR(easy_wakeup_gesture_id, 0444,
  15303. pt_easy_wakeup_gesture_id_show, NULL),
  15304. __ATTR(easy_wakeup_gesture_data, 0444,
  15305. pt_easy_wakeup_gesture_data_show, NULL),
  15306. #endif
  15307. #ifdef TTDL_DIAGNOSTICS
  15308. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15309. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15310. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15311. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15312. #endif /* TTDL_DIAGNOSTICS */
  15313. };
  15314. /*******************************************************************************
  15315. * FUNCTION: add_sysfs_interfaces
  15316. *
  15317. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15318. *
  15319. * RETURN:
  15320. * 0 = success
  15321. * !0 = failure
  15322. *
  15323. * PARAMETERS:
  15324. * *dev - pointer to device structure
  15325. ******************************************************************************/
  15326. static int add_sysfs_interfaces(struct device *dev)
  15327. {
  15328. int i;
  15329. int j = 0;
  15330. struct pt_core_data *cd = dev_get_drvdata(dev);
  15331. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15332. if (device_create_file(dev, attributes + i))
  15333. goto undo;
  15334. }
  15335. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15336. __func__, cd->active_dut_generation);
  15337. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15338. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15339. if (device_create_file(dev, pip2_attributes + j))
  15340. goto undo;
  15341. }
  15342. }
  15343. return 0;
  15344. undo:
  15345. for (i--; i >= 0; i--)
  15346. device_remove_file(dev, attributes + i);
  15347. for (j--; j >= 0; j--)
  15348. device_remove_file(dev, pip2_attributes + j);
  15349. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15350. __func__);
  15351. return -ENODEV;
  15352. }
  15353. /*******************************************************************************
  15354. * FUNCTION: remove_sysfs_interfaces
  15355. *
  15356. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15357. *
  15358. * RETURN: void
  15359. *
  15360. * PARAMETERS:
  15361. * *dev - pointer to device structure
  15362. ******************************************************************************/
  15363. static void remove_sysfs_interfaces(struct device *dev)
  15364. {
  15365. int i;
  15366. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15367. device_remove_file(dev, attributes + i);
  15368. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15369. device_remove_file(dev, pip2_attributes + i);
  15370. }
  15371. /*******************************************************************************
  15372. * FUNCTION: remove_sysfs_and_modules
  15373. *
  15374. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15375. *
  15376. * RETURN: void
  15377. *
  15378. * PARAMETERS:
  15379. * *dev - pointer to device structure
  15380. ******************************************************************************/
  15381. static void remove_sysfs_and_modules(struct device *dev)
  15382. {
  15383. struct pt_core_data *cd = dev_get_drvdata(dev);
  15384. /* Queued work should be removed before to release loader module */
  15385. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15386. pt_release_modules(cd);
  15387. pt_btn_release(dev);
  15388. pt_mt_release(dev);
  15389. remove_sysfs_interfaces(dev);
  15390. }
  15391. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15392. {
  15393. int retval;
  15394. /* Get pinctrl if target uses pinctrl */
  15395. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15396. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15397. retval = PTR_ERR(cd->ts_pinctrl);
  15398. dev_dbg(cd->dev,
  15399. "Target does not use pinctrl %d\n", retval);
  15400. goto err_pinctrl_get;
  15401. }
  15402. cd->pinctrl_state_active
  15403. = pinctrl_lookup_state(cd->ts_pinctrl,
  15404. PINCTRL_STATE_ACTIVE);
  15405. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15406. retval = PTR_ERR(cd->pinctrl_state_active);
  15407. dev_err(cd->dev,
  15408. "Can not lookup %s pinstate %d\n",
  15409. PINCTRL_STATE_ACTIVE, retval);
  15410. goto err_pinctrl_lookup;
  15411. }
  15412. cd->pinctrl_state_suspend
  15413. = pinctrl_lookup_state(cd->ts_pinctrl,
  15414. PINCTRL_STATE_SUSPEND);
  15415. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15416. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15417. dev_err(cd->dev,
  15418. "Can not lookup %s pinstate %d\n",
  15419. PINCTRL_STATE_SUSPEND, retval);
  15420. goto err_pinctrl_lookup;
  15421. }
  15422. cd->pinctrl_state_release
  15423. = pinctrl_lookup_state(cd->ts_pinctrl,
  15424. PINCTRL_STATE_RELEASE);
  15425. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15426. retval = PTR_ERR(cd->pinctrl_state_release);
  15427. dev_dbg(cd->dev,
  15428. "Can not lookup %s pinstate %d\n",
  15429. PINCTRL_STATE_RELEASE, retval);
  15430. }
  15431. return 0;
  15432. err_pinctrl_lookup:
  15433. devm_pinctrl_put(cd->ts_pinctrl);
  15434. err_pinctrl_get:
  15435. cd->ts_pinctrl = NULL;
  15436. return retval;
  15437. }
  15438. /*******************************************************************************
  15439. *******************************************************************************
  15440. * FUNCTION: pt_probe
  15441. *
  15442. * SUMMARY: Probe of the core module.
  15443. *
  15444. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15445. * probe functionality is split into two functions; pt_probe() and
  15446. * pt_probe_complete(). the initial setup is done in this function which
  15447. * then creates a WORK task which runs after the probe timer expires. This
  15448. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15449. *
  15450. * RETURN:
  15451. * 0 = success
  15452. * !0 = failure
  15453. *
  15454. * PARAMETERS:
  15455. * *ops - pointer to the bus
  15456. * *dev - pointer to the device structure
  15457. * irq - IRQ
  15458. * xfer_buf_size - size of the buffer
  15459. ******************************************************************************/
  15460. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15461. u16 irq, size_t xfer_buf_size)
  15462. {
  15463. struct pt_core_data *cd;
  15464. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15465. enum pt_atten_type type;
  15466. struct i2c_client *client = to_i2c_client(dev);
  15467. int rc = 0;
  15468. u8 pip_ver_major;
  15469. u8 pip_ver_minor;
  15470. u32 status = STARTUP_STATUS_START;
  15471. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15472. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15473. __func__);
  15474. rc = -ENODEV;
  15475. goto error_no_pdata;
  15476. }
  15477. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15478. if (!pdata->core_pdata->power) {
  15479. pt_debug(dev, DL_ERROR,
  15480. "%s: Missing platform data function\n",
  15481. __func__);
  15482. rc = -ENODEV;
  15483. goto error_no_pdata;
  15484. }
  15485. }
  15486. /* get context and debug print buffers */
  15487. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15488. if (!cd) {
  15489. rc = -ENOMEM;
  15490. goto error_no_pdata;
  15491. }
  15492. /* Initialize device info */
  15493. cd->dev = dev;
  15494. cd->pdata = pdata;
  15495. cd->cpdata = pdata->core_pdata;
  15496. cd->bus_ops = ops;
  15497. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15498. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15499. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15500. cd->hw_detected = false;
  15501. cd->pip2_prot_active = false;
  15502. cd->pip2_send_user_cmd = false;
  15503. cd->bl_pip_ver_ready = false;
  15504. cd->app_pip_ver_ready = false;
  15505. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15506. cd->get_param_id = 0;
  15507. cd->watchdog_enabled = 0;
  15508. cd->startup_retry_count = 0;
  15509. cd->core_probe_complete = 0;
  15510. cd->runtime = 0;
  15511. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15512. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15513. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15514. cd->flashless_dut = 0;
  15515. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15516. cd->bl_with_no_int = 0;
  15517. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15518. cd->multi_chip = PT_FEATURE_DISABLE;
  15519. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15520. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15521. cd->set_dut_generation = true;
  15522. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15523. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15524. cd->set_dut_generation = true;
  15525. cd->active_dut_generation = DUT_PIP1_ONLY;
  15526. } else {
  15527. cd->set_dut_generation = false;
  15528. cd->active_dut_generation = DUT_UNKNOWN;
  15529. }
  15530. /* Initialize with platform data */
  15531. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15532. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15533. cd->hid_cmd_state = 1;
  15534. cd->fw_updating = false;
  15535. cd->multi_chip = 0;
  15536. #ifdef TTDL_DIAGNOSTICS
  15537. cd->t_refresh_active = 0;
  15538. cd->t_refresh_count = 0;
  15539. cd->pip2_crc_error_count = 0;
  15540. cd->wd_xres_count = 0;
  15541. cd->bl_retry_packet_count = 0;
  15542. cd->file_erase_timeout_count = 0;
  15543. cd->show_tt_data = false;
  15544. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15545. cd->err_gpio = 0;
  15546. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15547. cd->ttdl_bist_select = 0x07;
  15548. cd->force_pip2_seq = 0;
  15549. #endif /* TTDL_DIAGNOSTICS */
  15550. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15551. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15552. sizeof(PT_PIP2_BIN_FILE_PATH));
  15553. pt_init_hid_descriptor(&cd->hid_desc);
  15554. /* Read and store the descriptor lengths */
  15555. cd->hid_core.hid_report_desc_len =
  15556. le16_to_cpu(cd->hid_desc.report_desc_len);
  15557. cd->hid_core.hid_max_input_len =
  15558. le16_to_cpu(cd->hid_desc.max_input_len);
  15559. cd->hid_core.hid_max_output_len =
  15560. le16_to_cpu(cd->hid_desc.max_output_len);
  15561. /* Initialize mutexes and spinlocks */
  15562. mutex_init(&cd->module_list_lock);
  15563. mutex_init(&cd->system_lock);
  15564. mutex_init(&cd->sysfs_lock);
  15565. mutex_init(&cd->ttdl_restart_lock);
  15566. mutex_init(&cd->firmware_class_lock);
  15567. spin_lock_init(&cd->spinlock);
  15568. /* Initialize module list */
  15569. INIT_LIST_HEAD(&cd->module_list);
  15570. /* Initialize attention lists */
  15571. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15572. INIT_LIST_HEAD(&cd->atten_list[type]);
  15573. /* Initialize parameter list */
  15574. INIT_LIST_HEAD(&cd->param_list);
  15575. /* Initialize wait queue */
  15576. init_waitqueue_head(&cd->wait_q);
  15577. rc = pt_ts_pinctrl_init(cd);
  15578. if (!rc && cd->ts_pinctrl) {
  15579. /*
  15580. * Pinctrl handle is optional. If pinctrl handle is found
  15581. * let pins to be configured in active state. If not
  15582. * found continue further without error.
  15583. */
  15584. rc = pinctrl_select_state(cd->ts_pinctrl,
  15585. cd->pinctrl_state_active);
  15586. if (rc < 0)
  15587. dev_err(&client->dev, "failed to select pin to active state\n");
  15588. }
  15589. rc = pt_get_regulator(cd, true);
  15590. if (rc) {
  15591. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15592. goto error_alloc_data;
  15593. }
  15594. rc = pt_enable_regulator(cd, true);
  15595. if (rc) {
  15596. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15597. goto error_get_regulator;
  15598. }
  15599. /* Initialize works */
  15600. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15601. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15602. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15603. /* Initialize HID specific data */
  15604. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15605. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15606. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15607. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15608. cd->hid_core.hid_desc_register =
  15609. cpu_to_le16(cd->cpdata->hid_desc_register);
  15610. /* Set platform easywake value */
  15611. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15612. #ifdef CONFIG_DRM
  15613. /* Setup active dsi panel */
  15614. active_panel = cd->cpdata->active_panel;
  15615. #endif
  15616. /* Set platform panel_id value */
  15617. cd->panel_id_support = cd->cpdata->panel_id_support;
  15618. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15619. /* Set Panel ID to default to 0 */
  15620. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15621. else
  15622. /* Set Panel ID to Not Enabled */
  15623. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15624. /* Initialize hw_version default to FFFF.FFFF.FF */
  15625. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15626. dev_set_drvdata(dev, cd);
  15627. /* PtSBC builds will call this function in pt_probe_complete() */
  15628. pt_add_core(dev);
  15629. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15630. if (rc) {
  15631. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15632. __func__);
  15633. goto error_enable_regulator;
  15634. }
  15635. /*
  15636. * Save the pointer to a global value, which will be used
  15637. * in ttdl_restart function
  15638. */
  15639. cd->bus_ops = ops;
  15640. /*
  15641. * When the IRQ GPIO is not direclty accessible and no function is
  15642. * defined to get the IRQ status, the IRQ passed in must be assigned
  15643. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15644. */
  15645. if (!cd->cpdata->irq_stat) {
  15646. cd->irq = irq;
  15647. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15648. __func__, cd->irq);
  15649. }
  15650. /* Call platform init function before setting up the GPIO's */
  15651. if (cd->cpdata->init) {
  15652. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15653. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15654. } else {
  15655. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15656. __func__);
  15657. rc = 0;
  15658. }
  15659. if (rc < 0) {
  15660. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15661. __func__, rc);
  15662. }
  15663. /* Power on any needed regulator(s) */
  15664. if (cd->cpdata->setup_power) {
  15665. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15666. rc = cd->cpdata->setup_power(cd->cpdata,
  15667. PT_MT_POWER_ON, cd->dev);
  15668. } else {
  15669. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15670. __func__);
  15671. rc = 0;
  15672. }
  15673. if (rc < 0)
  15674. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  15675. __func__, rc);
  15676. #ifdef TTDL_DIAGNOSTICS
  15677. cd->watchdog_irq_stuck_count = 0;
  15678. cd->bus_transmit_error_count = 0;
  15679. #endif /* TTDL_DIAGNOSTICS */
  15680. if (cd->cpdata->detect) {
  15681. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  15682. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  15683. pt_platform_detect_read);
  15684. if (!rc) {
  15685. cd->hw_detected = true;
  15686. pt_debug(cd->dev, DL_INFO,
  15687. "%s: HW detected\n", __func__);
  15688. } else {
  15689. cd->hw_detected = false;
  15690. pt_debug(cd->dev, DL_INFO,
  15691. "%s: No HW detected\n", __func__);
  15692. rc = -ENODEV;
  15693. goto error_detect;
  15694. }
  15695. } else {
  15696. pt_debug(dev, DL_ERROR,
  15697. "%s: PARADE No HW detect function pointer\n",
  15698. __func__);
  15699. /*
  15700. * "hw_reset" is not needed in the "if" statement,
  15701. * because "hw_reset" is already included in "hw_detect"
  15702. * function.
  15703. */
  15704. rc = pt_hw_hard_reset(cd);
  15705. if (rc)
  15706. pt_debug(cd->dev, DL_ERROR,
  15707. "%s: FAILED to execute HARD reset\n",
  15708. __func__);
  15709. }
  15710. if (cd->cpdata->setup_irq) {
  15711. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  15712. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  15713. if (rc) {
  15714. pt_debug(dev, DL_ERROR,
  15715. "%s: Error, couldn't setup IRQ\n", __func__);
  15716. goto error_setup_irq;
  15717. }
  15718. } else {
  15719. pt_debug(dev, DL_ERROR,
  15720. "%s: IRQ function pointer not setup\n",
  15721. __func__);
  15722. goto error_setup_irq;
  15723. }
  15724. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  15725. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  15726. (unsigned long)cd);
  15727. #else
  15728. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  15729. #endif
  15730. pt_stop_wd_timer(cd);
  15731. #ifdef TTHE_TUNER_SUPPORT
  15732. mutex_init(&cd->tthe_lock);
  15733. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  15734. 0644, NULL, cd, &tthe_debugfs_fops);
  15735. #endif
  15736. rc = device_init_wakeup(dev, 1);
  15737. if (rc < 0)
  15738. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  15739. __func__, rc);
  15740. pm_runtime_get_noresume(dev);
  15741. pm_runtime_set_active(dev);
  15742. pm_runtime_enable(dev);
  15743. /* If IRQ asserted, read out all from buffer to release INT pin */
  15744. if (cd->cpdata->irq_stat) {
  15745. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  15746. } else {
  15747. /* Force a read in case the reset sentinel already arrived */
  15748. rc = pt_read_input(cd);
  15749. if (!rc)
  15750. pt_parse_input(cd);
  15751. }
  15752. /* Without sleep DUT is not ready and will NAK the first write */
  15753. msleep(150);
  15754. /* Attempt to set the DUT generation if not yet set */
  15755. if (cd->active_dut_generation == DUT_UNKNOWN) {
  15756. if (cd->bl_pip_ver_ready ||
  15757. (cd->app_pip_ver_ready &&
  15758. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  15759. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15760. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  15761. __func__, cd->active_dut_generation);
  15762. } else {
  15763. rc = _pt_detect_dut_generation(cd->dev,
  15764. &status, &cd->active_dut_generation,
  15765. &cd->mode);
  15766. if ((cd->active_dut_generation == DUT_UNKNOWN)
  15767. || rc) {
  15768. pt_debug(cd->dev, DL_ERROR,
  15769. " === DUT Gen unknown, Skip Enum ===\n");
  15770. goto skip_enum;
  15771. }
  15772. }
  15773. }
  15774. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  15775. &pip_ver_major, &pip_ver_minor);
  15776. if (pip_ver_major == 2) {
  15777. cd->bl_pip_ver_ready = true;
  15778. pt_debug(dev, DL_ERROR,
  15779. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  15780. pip_ver_minor);
  15781. cd->hw_detected = true;
  15782. } else if (pip_ver_major == 1) {
  15783. cd->app_pip_ver_ready = true;
  15784. pt_debug(dev, DL_ERROR,
  15785. " === PIP1.%d Detected ===\n", pip_ver_minor);
  15786. cd->hw_detected = true;
  15787. } else {
  15788. cd->sysinfo.ttdata.pip_ver_major = 0;
  15789. cd->sysinfo.ttdata.pip_ver_minor = 0;
  15790. cd->app_pip_ver_ready = false;
  15791. cd->hw_detected = false;
  15792. pt_debug(dev, DL_ERROR,
  15793. " === PIP Version Not Detected, Skip Enum ===\n");
  15794. /* For legacy DUTS proceed, enum will attempt to launch app */
  15795. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  15796. goto skip_enum;
  15797. }
  15798. rc = pt_enum_with_dut(cd, false, &status);
  15799. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  15800. __func__, cd->startup_status, status);
  15801. if (rc == -ENODEV) {
  15802. pt_debug(cd->dev, DL_ERROR,
  15803. "%s: Enumeration Failed r=%d\n", __func__, rc);
  15804. /* For PtSBC don't error out, allow TTDL to stay up */
  15805. goto error_after_startup;
  15806. }
  15807. /* Suspend scanning until probe is complete to avoid asyc touches */
  15808. pt_pip_suspend_scanning_(cd);
  15809. if (cd->hw_detected) {
  15810. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  15811. __func__);
  15812. rc = add_sysfs_interfaces(dev);
  15813. if (rc < 0) {
  15814. pt_debug(dev, DL_ERROR,
  15815. "%s: Error, fail sysfs init\n", __func__);
  15816. goto error_after_startup;
  15817. }
  15818. } else {
  15819. pt_debug(dev, DL_ERROR,
  15820. "%s: No HW detected, sysfs interfaces not added\n",
  15821. __func__);
  15822. }
  15823. skip_enum:
  15824. pm_runtime_put_sync(dev);
  15825. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  15826. rc = pt_mt_probe(dev);
  15827. if (rc < 0) {
  15828. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  15829. __func__);
  15830. goto error_after_sysfs_create;
  15831. }
  15832. rc = pt_btn_probe(dev);
  15833. if (rc < 0) {
  15834. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  15835. __func__);
  15836. goto error_after_startup_mt;
  15837. }
  15838. pt_probe_modules(cd);
  15839. #ifdef CONFIG_HAS_EARLYSUSPEND
  15840. pt_setup_early_suspend(cd);
  15841. #elif defined(CONFIG_DRM)
  15842. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  15843. pt_setup_drm_notifier(cd);
  15844. #elif defined(CONFIG_FB)
  15845. pt_setup_fb_notifier(cd);
  15846. #endif
  15847. #ifdef NEED_SUSPEND_NOTIFIER
  15848. cd->pm_notifier.notifier_call = pt_pm_notifier;
  15849. register_pm_notifier(&cd->pm_notifier);
  15850. #endif
  15851. pt_pip_resume_scanning_(cd);
  15852. mutex_lock(&cd->system_lock);
  15853. cd->startup_status |= status;
  15854. cd->core_probe_complete = 1;
  15855. mutex_unlock(&cd->system_lock);
  15856. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  15857. __func__);
  15858. return 0;
  15859. error_after_startup_mt:
  15860. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  15861. pt_mt_release(dev);
  15862. error_after_sysfs_create:
  15863. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  15864. pm_runtime_disable(dev);
  15865. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15866. device_wakeup_disable(dev);
  15867. #endif
  15868. device_init_wakeup(dev, 0);
  15869. cancel_work_sync(&cd->enum_work);
  15870. pt_stop_wd_timer(cd);
  15871. pt_free_si_ptrs(cd);
  15872. remove_sysfs_interfaces(dev);
  15873. error_after_startup:
  15874. pr_err("%s PARADE error_after_startup\n", __func__);
  15875. del_timer(&cd->watchdog_timer);
  15876. if (cd->cpdata->setup_irq)
  15877. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15878. error_setup_irq:
  15879. error_detect:
  15880. if (cd->cpdata->init)
  15881. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15882. if (cd->cpdata->setup_power)
  15883. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15884. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15885. error_enable_regulator:
  15886. pt_del_core(dev);
  15887. dev_set_drvdata(dev, NULL);
  15888. pt_enable_regulator(cd, false);
  15889. error_get_regulator:
  15890. pt_get_regulator(cd, false);
  15891. error_alloc_data:
  15892. kfree(cd);
  15893. error_no_pdata:
  15894. pr_err("%s failed.\n", __func__);
  15895. return rc;
  15896. }
  15897. EXPORT_SYMBOL_GPL(pt_probe);
  15898. /*******************************************************************************
  15899. * FUNCTION: pt_release
  15900. *
  15901. * SUMMARY: This function does the following cleanup:
  15902. * - Releases all probed modules
  15903. * - Stops the watchdog
  15904. * - Cancels all pending work tasks
  15905. * - Removes all created sysfs nodes
  15906. * - Removes all created debugfs nodes
  15907. * - Frees the IRQ
  15908. * - Deletes the core
  15909. * - Frees all pointers and HID reports
  15910. *
  15911. * RETURN:
  15912. * 0 = success
  15913. * !0 = failure
  15914. *
  15915. * PARAMETERS:
  15916. * *cd - pointer to the core data structure
  15917. ******************************************************************************/
  15918. int pt_release(struct pt_core_data *cd)
  15919. {
  15920. struct device *dev = cd->dev;
  15921. /*
  15922. * Suspend the device before freeing the startup_work and stopping
  15923. * the watchdog since sleep function restarts watchdog on failure
  15924. */
  15925. pm_runtime_suspend(dev);
  15926. pm_runtime_disable(dev);
  15927. /*
  15928. * Any 'work' that can trigger a new thread should be canceled first.
  15929. * The watchdog is also stopped again because another thread could have
  15930. * restarted it. The irq_work is cancelled last because it is used for
  15931. * all I2C/SPI communication.
  15932. */
  15933. pt_stop_wd_timer(cd);
  15934. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15935. cancel_work_sync(&cd->ttdl_restart_work);
  15936. cancel_work_sync(&cd->enum_work);
  15937. pt_stop_wd_timer(cd);
  15938. pt_release_modules(cd);
  15939. pt_proximity_release(dev);
  15940. pt_btn_release(dev);
  15941. pt_mt_release(dev);
  15942. #ifdef CONFIG_HAS_EARLYSUSPEND
  15943. unregister_early_suspend(&cd->es);
  15944. #elif defined(CONFIG_DRM)
  15945. if (active_panel)
  15946. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  15947. #elif defined(CONFIG_FB)
  15948. fb_unregister_client(&cd->fb_notifier);
  15949. #endif
  15950. #ifdef NEED_SUSPEND_NOTIFIER
  15951. unregister_pm_notifier(&cd->pm_notifier);
  15952. #endif
  15953. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15954. device_wakeup_disable(dev);
  15955. #endif
  15956. device_init_wakeup(dev, 0);
  15957. #ifdef TTHE_TUNER_SUPPORT
  15958. mutex_lock(&cd->tthe_lock);
  15959. cd->tthe_exit = 1;
  15960. wake_up(&cd->wait_q);
  15961. mutex_unlock(&cd->tthe_lock);
  15962. debugfs_remove(cd->tthe_debugfs);
  15963. #endif
  15964. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15965. remove_sysfs_interfaces(dev);
  15966. disable_irq_nosync(cd->irq);
  15967. if (cd->cpdata->setup_irq)
  15968. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15969. if (cd->cpdata->init)
  15970. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15971. if (cd->cpdata->setup_power)
  15972. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15973. dev_set_drvdata(dev, NULL);
  15974. pt_del_core(dev);
  15975. pt_enable_regulator(cd, false);
  15976. pt_get_regulator(cd, false);
  15977. pt_free_si_ptrs(cd);
  15978. kfree(cd);
  15979. return 0;
  15980. }
  15981. EXPORT_SYMBOL_GPL(pt_release);
  15982. MODULE_LICENSE("GPL");
  15983. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  15984. MODULE_AUTHOR("Parade Technologies <[email protected]>");