pt_core.c 534 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include <linux/suspend.h>
  33. #include <glink_interface.h>
  34. #include <linux/remoteproc/qcom_rproc.h>
  35. #include "pt_regs.h"
  36. #if defined(CONFIG_PANEL_NOTIFIER)
  37. #include <linux/soc/qcom/panel_event_notifier.h>
  38. #endif
  39. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  40. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  41. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  42. #define FT_VTG_MIN_UV 2800000
  43. #define FT_VTG_MAX_UV 2800000
  44. #define FT_I2C_VTG_MIN_UV 1800000
  45. #define FT_I2C_VTG_MAX_UV 1800000
  46. #define PWR_SUSPEND_LOAD_UA 165
  47. #define I2C_SUSPEND_LOAD_UA 100
  48. #define PWR_ACTIVE_LOAD_MA 12000
  49. #define I2C_ACTIVE_LOAD_MA 30000
  50. #define PT_CORE_STARTUP_RETRY_COUNT 3
  51. #define PT_STATUS_STR_LEN (50)
  52. #define PT_DATA_SIZE (2 * 256)
  53. #if defined(CONFIG_DRM) || defined(CONFIG_PANEL_NOTIFIER)
  54. static struct drm_panel *active_panel;
  55. #endif
  56. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  57. #define ENABLE_I2C_REG_ONLY
  58. enum core_states {
  59. STATE_NONE,
  60. STATE_RESUME,
  61. STATE_SUSPEND
  62. };
  63. #ifdef ENABLE_I2C_REG_ONLY
  64. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  65. #endif
  66. #define PT_AMBIENT_MODE
  67. #ifdef PT_AMBIENT_MODE
  68. static int pt_device_exit(struct i2c_client *client);
  69. int pt_device_entry(struct device *dev,
  70. u16 irq, size_t xfer_buf_size);
  71. #endif
  72. static const char *pt_driver_core_name = PT_CORE_NAME;
  73. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  74. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  75. enum core_states pt_core_state = STATE_NONE;
  76. uint32_t pt_slate_resp_ack;
  77. struct pt_hid_field {
  78. int report_count;
  79. int report_size;
  80. int size; /* report_count * report_size */
  81. int offset;
  82. int data_type;
  83. int logical_min;
  84. int logical_max;
  85. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  86. u32 usage_page;
  87. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  88. struct pt_hid_report *report;
  89. bool record_field;
  90. };
  91. struct pt_hid_report {
  92. u8 id;
  93. u8 type;
  94. int size;
  95. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  96. int num_fields;
  97. int record_field_index;
  98. int header_size;
  99. int record_size;
  100. u32 usage_page;
  101. };
  102. struct atten_node {
  103. struct list_head node;
  104. char *id;
  105. struct device *dev;
  106. int (*func)(struct device *dev);
  107. int mode;
  108. };
  109. struct param_node {
  110. struct list_head node;
  111. u8 id;
  112. u32 value;
  113. u8 size;
  114. };
  115. struct module_node {
  116. struct list_head node;
  117. struct pt_module *module;
  118. void *data;
  119. };
  120. struct pt_hid_cmd {
  121. u8 opcode;
  122. u8 report_type;
  123. union {
  124. u8 report_id;
  125. u8 power_state;
  126. };
  127. u8 has_data_register;
  128. size_t write_length;
  129. u8 *write_buf;
  130. u8 *read_buf;
  131. u8 wait_interrupt;
  132. u8 reset_cmd;
  133. u16 timeout_ms;
  134. };
  135. struct pt_hid_output {
  136. u8 cmd_type;
  137. u16 length;
  138. u8 command_code;
  139. size_t write_length;
  140. u8 *write_buf;
  141. u8 novalidate;
  142. u8 reset_expected;
  143. u16 timeout_ms;
  144. };
  145. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  146. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  147. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  148. #define CREATE_PIP1_FW_CMD(command) \
  149. .cmd_type = PIP1_CMD_TYPE_FW, \
  150. .command_code = command
  151. #define CREATE_PIP1_BL_CMD(command) \
  152. .cmd_type = PIP1_CMD_TYPE_BL, \
  153. .command_code = command
  154. #define PT_MAX_PR_BUF_SIZE 2048
  155. /*******************************************************************************
  156. * FUNCTION: pt_pr_buf
  157. *
  158. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  159. *
  160. * RETURN: Void
  161. *
  162. * PARAMETERS:
  163. * *dev - pointer to Device structure
  164. * debug_level - requested debug level to print at
  165. * *buf - pointer to buffer to print
  166. * buf_len - size of buf
  167. * *data_name - Descriptive name of data prefixed to data
  168. ******************************************************************************/
  169. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  170. u16 buf_len, const char *data_name)
  171. {
  172. struct pt_core_data *cd = dev_get_drvdata(dev);
  173. int i;
  174. ssize_t pr_buf_index = 0;
  175. int max_size;
  176. /* only proceed if valid debug level and there is data to print */
  177. if (debug_level <= cd->debug_level && buf_len > 0) {
  178. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  179. if (!pr_buf)
  180. return;
  181. /*
  182. * With a space each printed char takes 3 bytes, subtract
  183. * the length of the data_name prefix as well as 11 bytes
  184. * for the " [0..xxx]: " printed before the data.
  185. */
  186. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  187. /* Ensure pr_buf_index stays within the 1018 size */
  188. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  189. data_name);
  190. for (i = 0; i < buf_len && i < max_size; i++)
  191. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  192. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  193. pt_debug(dev, debug_level, "%s\n", pr_buf);
  194. kfree(pr_buf);
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(pt_pr_buf);
  198. #ifdef TTHE_TUNER_SUPPORT
  199. /*******************************************************************************
  200. * FUNCTION: tthe_print
  201. *
  202. * SUMMARY: Format data name and time stamp as the header and format the
  203. * content of input buffer with hex base to "tthe_buf". And then wake up event
  204. * semaphore for tthe debugfs node.
  205. *
  206. * RETURN:
  207. * 0 = success
  208. * !0 = failure
  209. *
  210. * PARAMETERS:
  211. * *cd - pointer to core data
  212. * *buf - pointer to input buffer
  213. * buf_len - size of input buffer
  214. * *data_name - pointer to data name
  215. ******************************************************************************/
  216. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  217. const u8 *data_name)
  218. {
  219. int name_len = strlen(data_name);
  220. int i, n;
  221. u8 *p;
  222. int remain;
  223. u8 data_name_with_time_stamp[100];
  224. /* Prepend timestamp, if requested, to data_name */
  225. if (cd->show_timestamp) {
  226. scnprintf(data_name_with_time_stamp,
  227. sizeof(data_name_with_time_stamp),
  228. "[%u] %s", pt_get_time_stamp(), data_name);
  229. data_name = data_name_with_time_stamp;
  230. name_len = strlen(data_name);
  231. }
  232. mutex_lock(&cd->tthe_lock);
  233. if (!cd->tthe_buf)
  234. goto exit;
  235. /* Add 1 due to the '\n' that is appended at the end */
  236. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  237. goto exit;
  238. if (name_len + buf_len == 0)
  239. goto exit;
  240. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  241. if (remain < name_len)
  242. name_len = remain;
  243. p = cd->tthe_buf + cd->tthe_buf_len;
  244. memcpy(p, data_name, name_len);
  245. cd->tthe_buf_len += name_len;
  246. p += name_len;
  247. remain -= name_len;
  248. *p = 0;
  249. for (i = 0; i < buf_len; i++) {
  250. n = scnprintf(p, remain, "%02X ", buf[i]);
  251. if (n <= 0)
  252. break;
  253. p += n;
  254. remain -= n;
  255. cd->tthe_buf_len += n;
  256. }
  257. n = scnprintf(p, remain, "\n");
  258. cd->tthe_buf_len += n;
  259. exit:
  260. wake_up(&cd->wait_q);
  261. mutex_unlock(&cd->tthe_lock);
  262. return 0;
  263. }
  264. /*******************************************************************************
  265. * FUNCTION: _pt_request_tthe_print
  266. *
  267. * SUMMARY: Function pointer included in core_cmds to allow other modules
  268. * to request to print data to the "tthe_buffer".
  269. *
  270. * RETURN:
  271. * 0 = success
  272. * !0 = failure
  273. *
  274. * PARAMETERS:
  275. * *dev - pointer to device structure
  276. ******************************************************************************/
  277. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  278. int buf_len, const u8 *data_name)
  279. {
  280. struct pt_core_data *cd = dev_get_drvdata(dev);
  281. return tthe_print(cd, buf, buf_len, data_name);
  282. }
  283. #endif
  284. /*******************************************************************************
  285. * FUNCTION: pt_platform_detect_read
  286. *
  287. * SUMMARY: To be passed to platform dectect function to perform a read
  288. * operation.
  289. *
  290. * RETURN:
  291. * 0 = success
  292. * !0 = failure
  293. *
  294. * PARAMETERS:
  295. * *dev - pointer to Device structure
  296. * *buf - pointer to buffer where the data read will be stored
  297. * size - size to be read
  298. ******************************************************************************/
  299. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  300. {
  301. struct pt_core_data *cd = dev_get_drvdata(dev);
  302. return pt_adap_read_default(cd, buf, size);
  303. }
  304. /*******************************************************************************
  305. * FUNCTION: pt_add_parameter
  306. *
  307. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  308. * On every reset of the DUT this linked list is traversed and all
  309. * parameters in it are restored to the DUT.
  310. *
  311. * RETURN:
  312. * 0 = success
  313. * !0 = failure
  314. *
  315. * PARAMETERS:
  316. * *cd - pointer to core data
  317. * param_id - parameter ID to add
  318. * param_value - Value corresponding to the ID
  319. * param_size - Size of param_value
  320. ******************************************************************************/
  321. static int pt_add_parameter(struct pt_core_data *cd,
  322. u8 param_id, u32 param_value, u8 param_size)
  323. {
  324. struct param_node *param, *param_new;
  325. /* Check if parameter already exists in the list */
  326. spin_lock(&cd->spinlock);
  327. list_for_each_entry(param, &cd->param_list, node) {
  328. if (param->id == param_id) {
  329. /* Update parameter */
  330. param->value = param_value;
  331. pt_debug(cd->dev, DL_INFO,
  332. "%s: Update parameter id:%d value:%d size:%d\n",
  333. __func__, param_id, param_value, param_size);
  334. goto exit_unlock;
  335. }
  336. }
  337. spin_unlock(&cd->spinlock);
  338. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  339. if (!param_new)
  340. return -ENOMEM;
  341. param_new->id = param_id;
  342. param_new->value = param_value;
  343. param_new->size = param_size;
  344. pt_debug(cd->dev, DL_INFO,
  345. "%s: Add parameter id:%d value:%d size:%d\n",
  346. __func__, param_id, param_value, param_size);
  347. spin_lock(&cd->spinlock);
  348. list_add(&param_new->node, &cd->param_list);
  349. exit_unlock:
  350. spin_unlock(&cd->spinlock);
  351. return 0;
  352. }
  353. #ifdef TTDL_DIAGNOSTICS
  354. /*******************************************************************************
  355. * FUNCTION: pt_erase_parameter_list
  356. *
  357. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  358. * pairs. In some test cases this functionality is needed to ensure DUT
  359. * returns to a virgin state after a reset and no parameters are restored.
  360. *
  361. * RETURN:
  362. * 0 = success
  363. * !0 = failure
  364. *
  365. * PARAMETERS:
  366. * *cd - pointer to core data
  367. ******************************************************************************/
  368. static int pt_erase_parameter_list(struct pt_core_data *cd)
  369. {
  370. struct param_node *pos, *temp;
  371. spin_lock(&cd->spinlock);
  372. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  373. pt_debug(cd->dev, DL_INFO,
  374. "%s: Parameter Restore List - remove 0x%02x\n",
  375. __func__, pos->id);
  376. list_del(&pos->node);
  377. kfree(pos);
  378. }
  379. spin_unlock(&cd->spinlock);
  380. return 0;
  381. }
  382. /*******************************************************************************
  383. * FUNCTION: pt_count_parameter_list
  384. *
  385. * SUMMARY: Count the items in the RAM parameter restor list
  386. *
  387. * RETURN:
  388. * 0 = success
  389. * !0 = failure
  390. *
  391. * PARAMETERS:
  392. * *cd - pointer to core data
  393. ******************************************************************************/
  394. static int pt_count_parameter_list(struct pt_core_data *cd)
  395. {
  396. struct param_node *pos, *temp;
  397. int entries = 0;
  398. spin_lock(&cd->spinlock);
  399. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  400. entries++;
  401. spin_unlock(&cd->spinlock);
  402. return entries;
  403. }
  404. #endif /* TTDL_DIAGNOSTICS */
  405. /*******************************************************************************
  406. * FUNCTION: request_exclusive
  407. *
  408. * SUMMARY: Request exclusive access to the DUT
  409. *
  410. * RETURN:
  411. * 0 = success
  412. * !0 = failure
  413. *
  414. * PARAMETERS:
  415. * *cd - pointer to core data
  416. * *ownptr - pointer to device
  417. * timeout_ms - Timeout value
  418. ******************************************************************************/
  419. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  420. int timeout_ms)
  421. {
  422. int t = msecs_to_jiffies(timeout_ms);
  423. bool with_timeout = (timeout_ms != 0);
  424. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  425. __func__, timeout_ms);
  426. mutex_lock(&cd->system_lock);
  427. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  428. cd->exclusive_dev = ownptr;
  429. goto exit;
  430. }
  431. cd->exclusive_waits++;
  432. wait:
  433. mutex_unlock(&cd->system_lock);
  434. if (with_timeout) {
  435. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  436. if (IS_TMO(t)) {
  437. pt_debug(cd->dev, DL_ERROR,
  438. "%s: tmo waiting exclusive access\n", __func__);
  439. return -ETIME;
  440. }
  441. } else {
  442. wait_event(cd->wait_q, !cd->exclusive_dev);
  443. }
  444. mutex_lock(&cd->system_lock);
  445. if (cd->exclusive_dev)
  446. goto wait;
  447. cd->exclusive_dev = ownptr;
  448. cd->exclusive_waits--;
  449. exit:
  450. mutex_unlock(&cd->system_lock);
  451. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  452. __func__, ownptr);
  453. return 0;
  454. }
  455. /*******************************************************************************
  456. * FUNCTION: release_exclusive_
  457. *
  458. * SUMMARY: Release exclusive access to the DUT
  459. *
  460. * RETURN:
  461. * 0 = success
  462. *
  463. * PARAMETERS:
  464. * *cd - pointer to core data
  465. * *ownptr - pointer to device
  466. ******************************************************************************/
  467. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  468. {
  469. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  470. __func__);
  471. if (cd->exclusive_dev != ownptr)
  472. return -EINVAL;
  473. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  474. __func__, cd->exclusive_dev);
  475. cd->exclusive_dev = NULL;
  476. wake_up(&cd->wait_q);
  477. return 0;
  478. }
  479. /*******************************************************************************
  480. * FUNCTION: release_exclusive
  481. *
  482. * SUMMARY: Protected wrapper to release_exclusive_()
  483. *
  484. * RETURN:
  485. * 0 = success
  486. * !0 = failure
  487. *
  488. * PARAMETERS:
  489. * *cd - pointer to core data
  490. * *ownptr - pointer to device
  491. ******************************************************************************/
  492. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  493. {
  494. int rc;
  495. mutex_lock(&cd->system_lock);
  496. rc = release_exclusive_(cd, ownptr);
  497. mutex_unlock(&cd->system_lock);
  498. return rc;
  499. }
  500. /*******************************************************************************
  501. * FUNCTION: pt_hid_exec_cmd_
  502. *
  503. * SUMMARY: Send the HID command to the DUT
  504. *
  505. * RETURN:
  506. * 0 = success
  507. * !0 = failure
  508. *
  509. * PARAMETERS:
  510. * *cd - pointer to core data
  511. * *hid_cmd - pointer to the HID command to send
  512. ******************************************************************************/
  513. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  514. struct pt_hid_cmd *hid_cmd)
  515. {
  516. int rc = 0;
  517. u8 *cmd;
  518. u16 cmd_length;
  519. u8 cmd_offset = 0;
  520. cmd_length = 2 /* command register */
  521. + 2 /* command */
  522. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  523. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  524. + hid_cmd->write_length; /* Data length */
  525. if (cmd_length < 4 || cmd_length > PT_MAX_PIP1_MSG_SIZE)
  526. return -EPROTO;
  527. cmd = kzalloc(cmd_length, GFP_KERNEL);
  528. if (!cmd)
  529. return -ENOMEM;
  530. /* Set Command register */
  531. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  532. sizeof(cd->hid_desc.command_register));
  533. cmd_offset += sizeof(cd->hid_desc.command_register);
  534. /* Set Command */
  535. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  536. if (hid_cmd->report_id >= 0XF)
  537. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  538. else
  539. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  540. cmd_offset++;
  541. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  542. cmd_offset++;
  543. if (hid_cmd->report_id >= 0XF) {
  544. cmd[cmd_offset] = hid_cmd->report_id;
  545. cmd_offset++;
  546. }
  547. /* Set Data register */
  548. if (hid_cmd->has_data_register) {
  549. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  550. sizeof(cd->hid_desc.data_register));
  551. cmd_offset += sizeof(cd->hid_desc.data_register);
  552. }
  553. /* Set Data */
  554. if (hid_cmd->write_length && hid_cmd->write_buf) {
  555. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  556. hid_cmd->write_length);
  557. cmd_offset += hid_cmd->write_length;
  558. }
  559. pt_debug(cd->dev, DL_INFO,
  560. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  561. __func__, cmd_length, hid_cmd->report_id);
  562. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  563. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  564. hid_cmd->read_buf);
  565. if (rc)
  566. pt_debug(cd->dev, DL_ERROR,
  567. "%s: Fail pt_adap_transfer\n", __func__);
  568. kfree(cmd);
  569. return rc;
  570. }
  571. #ifdef TTDL_DIAGNOSTICS
  572. /*******************************************************************************
  573. * FUNCTION: pt_toggle_err_gpio
  574. *
  575. * SUMMARY: Toggles the pre-defined error GPIO
  576. *
  577. * RETURN: n/a
  578. *
  579. * PARAMETERS:
  580. * *cd - pointer to core data
  581. * type - type of err that occurred
  582. ******************************************************************************/
  583. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  584. {
  585. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  586. __func__, type);
  587. if (cd->err_gpio && type == cd->err_gpio_type) {
  588. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  589. gpio_direction_output(cd->err_gpio,
  590. !gpio_get_value(cd->err_gpio));
  591. }
  592. }
  593. /*******************************************************************************
  594. * FUNCTION: _pt_request_toggle_err_gpio
  595. *
  596. * SUMMARY: Function pointer included in core_cmds to allow other modules
  597. * to request to toggle the err_gpio
  598. *
  599. * RETURN: n/a
  600. *
  601. * PARAMETERS:
  602. * *cd - pointer to core data
  603. * type - type of err that occurred
  604. ******************************************************************************/
  605. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  606. {
  607. struct pt_core_data *cd = dev_get_drvdata(dev);
  608. pt_toggle_err_gpio(cd, type);
  609. }
  610. #endif /* TTDL_DIAGNOSTICS */
  611. /*******************************************************************************
  612. * FUNCTION: pt_hid_exec_cmd_and_wait_
  613. *
  614. * SUMMARY: Send the HID command to the DUT and wait for the response
  615. *
  616. * RETURN:
  617. * 0 = success
  618. * !0 = failure
  619. *
  620. * PARAMETERS:
  621. * *cd - pointer to core data
  622. * *hid_cmd - pointer to the HID command to send
  623. ******************************************************************************/
  624. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  625. struct pt_hid_cmd *hid_cmd)
  626. {
  627. int rc = 0;
  628. int t;
  629. u16 timeout_ms;
  630. int *cmd_state;
  631. if (hid_cmd->reset_cmd)
  632. cmd_state = &cd->hid_reset_cmd_state;
  633. else
  634. cmd_state = &cd->hid_cmd_state;
  635. if (hid_cmd->wait_interrupt) {
  636. mutex_lock(&cd->system_lock);
  637. *cmd_state = 1;
  638. mutex_unlock(&cd->system_lock);
  639. }
  640. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  641. if (rc) {
  642. if (hid_cmd->wait_interrupt)
  643. goto error;
  644. goto exit;
  645. }
  646. if (!hid_cmd->wait_interrupt)
  647. goto exit;
  648. if (hid_cmd->timeout_ms)
  649. timeout_ms = hid_cmd->timeout_ms;
  650. else
  651. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  652. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  653. msecs_to_jiffies(timeout_ms));
  654. if (IS_TMO(t)) {
  655. #ifdef TTDL_DIAGNOSTICS
  656. cd->bus_transmit_error_count++;
  657. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  658. #endif /* TTDL_DIAGNOSTICS */
  659. pt_debug(cd->dev, DL_ERROR,
  660. "%s: HID output cmd execution timed out\n",
  661. __func__);
  662. rc = -ETIME;
  663. goto error;
  664. }
  665. goto exit;
  666. error:
  667. mutex_lock(&cd->system_lock);
  668. *cmd_state = 0;
  669. mutex_unlock(&cd->system_lock);
  670. exit:
  671. return rc;
  672. }
  673. /*******************************************************************************
  674. * FUNCTION: pt_hid_cmd_reset_
  675. *
  676. * SUMMARY: Send the HID RESET command to the DUT
  677. *
  678. * RETURN:
  679. * 0 = success
  680. * !0 = failure
  681. *
  682. * PARAMETERS:
  683. * *cd - pointer to core data
  684. ******************************************************************************/
  685. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  686. {
  687. struct pt_hid_cmd hid_cmd = {
  688. .opcode = HID_CMD_RESET,
  689. .wait_interrupt = 1,
  690. .reset_cmd = 1,
  691. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  692. };
  693. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  694. }
  695. /*******************************************************************************
  696. * FUNCTION: pt_hid_cmd_reset
  697. *
  698. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  699. * access.
  700. *
  701. * RETURN:
  702. * 0 = success
  703. * !0 = failure
  704. *
  705. * PARAMETERS:
  706. * *cd - pointer to core data
  707. ******************************************************************************/
  708. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  709. {
  710. int rc = 0;
  711. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  712. if (rc < 0) {
  713. pt_debug(cd->dev, DL_ERROR,
  714. "%s: fail get exclusive ex=%p own=%p\n",
  715. __func__, cd->exclusive_dev, cd->dev);
  716. return rc;
  717. }
  718. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  719. rc = pt_hid_cmd_reset_(cd);
  720. if (release_exclusive(cd, cd->dev) < 0)
  721. pt_debug(cd->dev, DL_ERROR,
  722. "%s: fail to release exclusive\n", __func__);
  723. return rc;
  724. }
  725. /*******************************************************************************
  726. * FUNCTION: pt_hid_cmd_set_power_
  727. *
  728. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  729. *
  730. * RETURN:
  731. * 0 = success
  732. * !0 = failure
  733. *
  734. * PARAMETERS:
  735. * *cd - pointer to core data
  736. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  737. ******************************************************************************/
  738. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  739. u8 power_state)
  740. {
  741. int rc = 0;
  742. struct pt_hid_cmd hid_cmd = {
  743. .opcode = HID_CMD_SET_POWER,
  744. .wait_interrupt = 1,
  745. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  746. };
  747. hid_cmd.power_state = power_state;
  748. /* The chip won't give response if goes to Deep Standby */
  749. if (power_state == HID_POWER_STANDBY) {
  750. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  751. if (rc)
  752. pt_debug(cd->dev, DL_ERROR,
  753. "%s: Failed to set power to state:%d\n",
  754. __func__, power_state);
  755. else
  756. cd->fw_sys_mode_in_standby_state = true;
  757. return rc;
  758. }
  759. cd->fw_sys_mode_in_standby_state = false;
  760. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  761. if (rc) {
  762. pt_debug(cd->dev, DL_ERROR,
  763. "%s: Failed to set power to state:%d\n",
  764. __func__, power_state);
  765. return rc;
  766. }
  767. /* validate */
  768. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  769. || ((cd->response_buf[3] & 0x3) != power_state)
  770. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  771. rc = -EINVAL;
  772. return rc;
  773. }
  774. /*******************************************************************************
  775. * FUNCTION: pt_hid_cmd_set_power
  776. *
  777. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  778. * exclusive access.
  779. *
  780. * RETURN:
  781. * 0 = success
  782. * !0 = failure
  783. *
  784. * PARAMETERS:
  785. * *cd - pointer to core data
  786. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  787. ******************************************************************************/
  788. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  789. u8 power_state)
  790. {
  791. int rc;
  792. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  793. if (rc < 0) {
  794. pt_debug(cd->dev, DL_ERROR,
  795. "%s: fail get exclusive ex=%p own=%p\n",
  796. __func__, cd->exclusive_dev, cd->dev);
  797. return rc;
  798. }
  799. rc = pt_hid_cmd_set_power_(cd, power_state);
  800. if (release_exclusive(cd, cd->dev) < 0)
  801. pt_debug(cd->dev, DL_ERROR,
  802. "%s: fail to release exclusive\n", __func__);
  803. return rc;
  804. }
  805. static const u16 crc_table[16] = {
  806. 0x0000, 0x1021, 0x2042, 0x3063,
  807. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  808. 0x8108, 0x9129, 0xa14a, 0xb16b,
  809. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  810. };
  811. /*******************************************************************************
  812. * FUNCTION: _pt_compute_crc
  813. *
  814. * SUMMARY: Calculate CRC by CRC table.
  815. *
  816. * RETURN:
  817. * CRC calculation result
  818. *
  819. * PARAMETERS:
  820. * *buf - pointer to the data array to be calculated
  821. * size - size of data array
  822. ******************************************************************************/
  823. static u16 _pt_compute_crc(u8 *buf, u32 size)
  824. {
  825. u16 remainder = 0xFFFF;
  826. u16 xor_mask = 0x0000;
  827. u32 index;
  828. u32 byte_value;
  829. u32 table_index;
  830. u32 crc_bit_width = sizeof(u16) * 8;
  831. /* Divide the message by polynomial, via the table. */
  832. for (index = 0; index < size; index++) {
  833. byte_value = buf[index];
  834. table_index = ((byte_value >> 4) & 0x0F)
  835. ^ (remainder >> (crc_bit_width - 4));
  836. remainder = crc_table[table_index] ^ (remainder << 4);
  837. table_index = (byte_value & 0x0F)
  838. ^ (remainder >> (crc_bit_width - 4));
  839. remainder = crc_table[table_index] ^ (remainder << 4);
  840. }
  841. /* Perform the final remainder CRC. */
  842. return remainder ^ xor_mask;
  843. }
  844. u16 ccitt_Table[] = {
  845. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  846. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  847. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  848. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  849. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  850. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  851. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  852. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  853. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  854. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  855. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  856. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  857. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  858. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  859. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  860. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  861. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  862. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  863. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  864. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  865. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  866. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  867. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  868. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  869. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  870. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  871. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  872. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  873. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  874. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  875. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  876. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  877. };
  878. /*******************************************************************************
  879. * FUNCTION: crc_ccitt_calculate
  880. *
  881. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  882. *
  883. * RETURN:
  884. * CRC calculation result
  885. *
  886. * PARAMETERS:
  887. * *q - pointer to the data array to be calculated
  888. * len - size of data array
  889. ******************************************************************************/
  890. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  891. {
  892. unsigned short crc = 0xffff;
  893. while (len-- > 0)
  894. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  895. return crc;
  896. }
  897. /*******************************************************************************
  898. * FUNCTION: pt_pip2_cmd_calculate_crc
  899. *
  900. * SUMMARY: Calculate the CRC of a command packet
  901. *
  902. * RETURN: void
  903. *
  904. * PARAMETERS:
  905. * *cmd - pointer to command data
  906. * extra_bytes - Extra bytes included in command length
  907. ******************************************************************************/
  908. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  909. u8 extra_bytes)
  910. {
  911. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  912. unsigned short crc;
  913. buf[0] = cmd->len & 0xff;
  914. buf[1] = (cmd->len & 0xff00) >> 8;
  915. buf[2] = cmd->seq;
  916. buf[3] = cmd->id;
  917. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  918. /* Calculate the CRC for the first 4 bytes above and the data payload */
  919. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  920. cmd->crc[0] = (crc & 0xff00) >> 8;
  921. cmd->crc[1] = (crc & 0xff);
  922. }
  923. /*******************************************************************************
  924. * FUNCTION: pt_pip2_get_next_cmd_seq
  925. *
  926. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  927. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  928. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  929. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  930. * increment, just use the forced value.
  931. *
  932. * RETURN: Next command sequence number [0x08-0x0F]
  933. *
  934. * PARAMETERS:
  935. * *cd - pointer to core data
  936. ******************************************************************************/
  937. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  938. {
  939. #ifdef TTDL_DIAGNOSTICS
  940. if (cd->force_pip2_seq <= 0x07) {
  941. cd->pip2_cmd_tag_seq++;
  942. if (cd->pip2_cmd_tag_seq > 0x0F)
  943. cd->pip2_cmd_tag_seq = 0x08;
  944. } else {
  945. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  946. }
  947. #else
  948. cd->pip2_cmd_tag_seq++;
  949. if (cd->pip2_cmd_tag_seq > 0x0F)
  950. cd->pip2_cmd_tag_seq = 0x08;
  951. #endif
  952. return cd->pip2_cmd_tag_seq;
  953. }
  954. /*
  955. * Following macros are to define the response time (the interval between PIP2
  956. * command finishes sending and INT pin falls). The unit is in microsecond.
  957. * It has different time settings between the solution GPIO polling and Bus
  958. * polling due to the considration for system load.
  959. */
  960. #ifdef PT_POLL_RESP_BY_BUS
  961. #define POLL_RETRY_DEFAULT_INTERVAL 50
  962. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  963. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  964. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  965. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  966. #else
  967. #define POLL_RETRY_DEFAULT_INTERVAL 20
  968. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  969. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  970. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  971. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  972. #endif
  973. /*
  974. * id: the command id defined in PIP2
  975. * response_len: the (maximum) length of response.
  976. * response_time_min: minimum response time in microsecond
  977. * response_time_max: maximum response time in microsecond
  978. */
  979. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  980. {.id = PIP2_CMD_ID_PING,
  981. .response_len = 255,
  982. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  983. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  984. {.id = PIP2_CMD_ID_STATUS,
  985. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  986. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  987. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  988. {.id = PIP2_CMD_ID_CTRL,
  989. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  990. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  991. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  992. {.id = PIP2_CMD_ID_CONFIG,
  993. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  994. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  995. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  996. {.id = PIP2_CMD_ID_CLEAR,
  997. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  998. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  999. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1000. {.id = PIP2_CMD_ID_RESET,
  1001. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1002. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1003. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1004. {.id = PIP2_CMD_ID_VERSION,
  1005. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  1006. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1007. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1008. {.id = PIP2_CMD_ID_FILE_OPEN,
  1009. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  1010. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1011. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1012. {.id = PIP2_CMD_ID_FILE_CLOSE,
  1013. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1014. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1015. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1016. {.id = PIP2_CMD_ID_FILE_READ,
  1017. .response_len = 255,
  1018. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1019. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1020. {.id = PIP2_CMD_ID_FILE_WRITE,
  1021. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1022. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1023. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1024. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1025. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1026. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1027. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1028. {.id = PIP2_CMD_ID_FLASH_INFO,
  1029. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1030. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1031. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1032. {.id = PIP2_CMD_ID_EXECUTE,
  1033. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1034. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1035. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1036. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1037. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1038. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1039. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1040. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1041. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1042. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1043. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1044. {.id = PIP2_CMD_ID_READ_GPIO,
  1045. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1046. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1047. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1048. {.id = PIP2_CMD_EXECUTE_SCAN,
  1049. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1050. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1051. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1052. {.id = PIP2_CMD_SET_PARAMETER,
  1053. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1054. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1055. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1056. {.id = PIP2_CMD_GET_PARAMETER,
  1057. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1058. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1059. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1060. {.id = PIP2_CMD_SET_DDI_REG,
  1061. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1062. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1063. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1064. {.id = PIP2_CMD_GET_DDI_REG,
  1065. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1066. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1067. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1068. {.id = PIP2_CMD_ID_END,
  1069. .response_len = 255,
  1070. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1071. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1072. };
  1073. /*******************************************************************************
  1074. * FUNCTION: pt_pip2_get_cmd_response_len
  1075. *
  1076. * SUMMARY: Gets the expected response length based on the command ID
  1077. *
  1078. * RETURN: Expected response length
  1079. *
  1080. * PARAMETERS:
  1081. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1082. ******************************************************************************/
  1083. static int pt_pip2_get_cmd_response_len(u8 id)
  1084. {
  1085. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1086. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1087. p++;
  1088. if (p->id != PIP2_CMD_ID_END)
  1089. return p->response_len;
  1090. else
  1091. return -EPERM;
  1092. }
  1093. /*******************************************************************************
  1094. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1095. *
  1096. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1097. * finishes sending and INT pin falls) based on the command ID
  1098. *
  1099. * RETURN: Estimated minimum response time in microsecond
  1100. *
  1101. * PARAMETERS:
  1102. * id - Command ID
  1103. ******************************************************************************/
  1104. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1105. {
  1106. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1107. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1108. p++;
  1109. if (p->id != PIP2_CMD_ID_END)
  1110. return p->response_time_min;
  1111. else
  1112. return PIP2_RESP_DEFAULT_TIME_MIN;
  1113. }
  1114. /*******************************************************************************
  1115. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1116. *
  1117. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1118. * finishes sending and INT pin falls) based on the command ID
  1119. *
  1120. * RETURN: Estimated maximum response time in microsecond
  1121. *
  1122. * PARAMETERS:
  1123. * id - Command ID
  1124. ******************************************************************************/
  1125. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1126. {
  1127. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1128. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1129. p++;
  1130. if (p->id != PIP2_CMD_ID_END)
  1131. return p->response_time_max;
  1132. else
  1133. return PIP2_RESP_DEFAULT_TIME_MAX;
  1134. }
  1135. /*******************************************************************************
  1136. * FUNCTION: pt_pip2_validate_response
  1137. *
  1138. * SUMMARY: Validate the response of PIP2 command.
  1139. *
  1140. * RETURN:
  1141. * 0 = success
  1142. * !0 = failure
  1143. *
  1144. * PARAMETERS:
  1145. * *cd - pointer to core data
  1146. * *pip2_cmd - pointer to PIP2 command to send
  1147. * *read_buf - pointer to response buffer
  1148. * actual_read_len - actual read length of the response
  1149. ******************************************************************************/
  1150. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1151. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1152. u16 actual_read_len)
  1153. {
  1154. int rc = 0;
  1155. u8 response_seq = 0;
  1156. u8 reserved_bits = 0;
  1157. u8 cmd_id = 0;
  1158. u8 response_bit = 0;
  1159. unsigned short calc_crc = 0;
  1160. unsigned short resp_crc = 0;
  1161. /* Verify the length of response buffer */
  1162. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1163. pt_debug(cd->dev, DL_ERROR,
  1164. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1165. __func__, pip2_cmd->id, actual_read_len);
  1166. rc = -EINVAL;
  1167. goto exit;
  1168. }
  1169. /* Verify the CRC */
  1170. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1171. resp_crc = read_buf[actual_read_len - 2] << 8;
  1172. resp_crc |= read_buf[actual_read_len - 1];
  1173. if (resp_crc != calc_crc) {
  1174. pt_debug(cd->dev, DL_ERROR,
  1175. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1176. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1177. #ifdef TTDL_DIAGNOSTICS
  1178. cd->pip2_crc_error_count++;
  1179. #endif /* TTDL_DIAGNOSTICS */
  1180. rc = -EINVAL;
  1181. goto exit;
  1182. }
  1183. /* Verify the response bit is set */
  1184. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1185. if (!response_bit) {
  1186. pt_debug(cd->dev, DL_ERROR,
  1187. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1188. __func__, pip2_cmd->id, response_bit);
  1189. rc = -EINVAL;
  1190. goto exit;
  1191. }
  1192. /* Verify the command ID matches from command to response */
  1193. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1194. if (cmd_id != pip2_cmd->id) {
  1195. pt_debug(cd->dev, DL_ERROR,
  1196. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1197. __func__, pip2_cmd->id, cmd_id);
  1198. rc = -EINVAL;
  1199. goto exit;
  1200. }
  1201. /* Verify the SEQ number matches from command to response */
  1202. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1203. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1204. pt_debug(cd->dev, DL_ERROR,
  1205. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1206. __func__, pip2_cmd->id,
  1207. pip2_cmd->seq, response_seq);
  1208. rc = -EINVAL;
  1209. goto exit;
  1210. }
  1211. /* Verify the reserved bits are 0 */
  1212. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1213. if (reserved_bits)
  1214. pt_debug(cd->dev, DL_WARN,
  1215. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1216. __func__, pip2_cmd->id, reserved_bits);
  1217. exit:
  1218. if (rc)
  1219. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1220. "PIP RSP:");
  1221. return rc;
  1222. }
  1223. /*******************************************************************************
  1224. * FUNCTION: pt_hid_output_validate_bl_response
  1225. *
  1226. * SUMMARY: Validate the response of bootloader command.
  1227. *
  1228. * RETURN:
  1229. * 0 = success
  1230. * !0 = failure
  1231. *
  1232. * PARAMETERS:
  1233. * *cd - pointer to core data
  1234. * *hid_output - pointer to hid output data structure
  1235. ******************************************************************************/
  1236. static int pt_hid_output_validate_bl_response(
  1237. struct pt_core_data *cd,
  1238. struct pt_hid_output *hid_output)
  1239. {
  1240. u16 size;
  1241. u16 crc;
  1242. u8 status;
  1243. size = get_unaligned_le16(&cd->response_buf[0]);
  1244. if (hid_output->reset_expected && !size)
  1245. return 0;
  1246. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1247. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1248. pt_debug(cd->dev, DL_ERROR,
  1249. "%s: BL output response, wrong report_id\n", __func__);
  1250. return -EPROTO;
  1251. }
  1252. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1253. pt_debug(cd->dev, DL_ERROR,
  1254. "%s: BL output response, wrong SOP\n", __func__);
  1255. return -EPROTO;
  1256. }
  1257. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1258. pt_debug(cd->dev, DL_ERROR,
  1259. "%s: BL output response, wrong EOP\n", __func__);
  1260. return -EPROTO;
  1261. }
  1262. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1263. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1264. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1265. pt_debug(cd->dev, DL_ERROR,
  1266. "%s: BL output response, wrong CRC 0x%X\n",
  1267. __func__, crc);
  1268. return -EPROTO;
  1269. }
  1270. status = cd->response_buf[5];
  1271. if (status) {
  1272. pt_debug(cd->dev, DL_ERROR,
  1273. "%s: BL output response, ERROR:%d\n",
  1274. __func__, status);
  1275. return -EPROTO;
  1276. }
  1277. return 0;
  1278. }
  1279. /*******************************************************************************
  1280. * FUNCTION: pt_hid_output_validate_app_response
  1281. *
  1282. * SUMMARY: Validate the response of application command.
  1283. *
  1284. * RETURN:
  1285. * 0 = success
  1286. * !0 = failure
  1287. *
  1288. * PARAMETERS:
  1289. * *cd - pointer to core data
  1290. * *hid_output - pointer to hid output data structure
  1291. ******************************************************************************/
  1292. static int pt_hid_output_validate_app_response(
  1293. struct pt_core_data *cd,
  1294. struct pt_hid_output *hid_output)
  1295. {
  1296. int command_code;
  1297. u16 size;
  1298. size = get_unaligned_le16(&cd->response_buf[0]);
  1299. if (hid_output->reset_expected && !size)
  1300. return 0;
  1301. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1302. != PT_PIP_NON_HID_RESPONSE_ID) {
  1303. pt_debug(cd->dev, DL_ERROR,
  1304. "%s: APP output response, wrong report_id\n", __func__);
  1305. return -EPROTO;
  1306. }
  1307. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1308. & PIP1_RESP_COMMAND_ID_MASK;
  1309. if (command_code != hid_output->command_code) {
  1310. pt_debug(cd->dev, DL_ERROR,
  1311. "%s: APP output response, wrong command_code:%X\n",
  1312. __func__, command_code);
  1313. return -EPROTO;
  1314. }
  1315. return 0;
  1316. }
  1317. /*******************************************************************************
  1318. * FUNCTION: pt_check_set_parameter
  1319. *
  1320. * SUMMARY: Check command input and response for Set Parameter command.And
  1321. * store the parameter to the list for resume work if pass the check.
  1322. *
  1323. * PARAMETERS:
  1324. * *cd - pointer to core data
  1325. * *hid_output - pointer to hid output data structure
  1326. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1327. ******************************************************************************/
  1328. static void pt_check_set_parameter(struct pt_core_data *cd,
  1329. struct pt_hid_output *hid_output, bool raw)
  1330. {
  1331. u8 *param_buf;
  1332. u32 param_value = 0;
  1333. u8 param_size;
  1334. u8 param_id;
  1335. int i = 0;
  1336. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1337. return;
  1338. /* Check command input for Set Parameter command */
  1339. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1340. && !memcmp(&hid_output->write_buf[0],
  1341. &cd->hid_desc.output_register,
  1342. sizeof(cd->hid_desc.output_register))
  1343. && hid_output->write_buf[4] ==
  1344. PT_PIP_NON_HID_COMMAND_ID
  1345. && hid_output->write_buf[6] ==
  1346. PIP1_CMD_ID_SET_PARAM)
  1347. param_buf = &hid_output->write_buf[7];
  1348. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1349. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1350. && hid_output->write_length >= 3
  1351. && hid_output->write_length <= 6)
  1352. param_buf = &hid_output->write_buf[0];
  1353. else
  1354. return;
  1355. /* Get parameter ID, size and value */
  1356. param_id = param_buf[0];
  1357. param_size = param_buf[1];
  1358. if (param_size > 4) {
  1359. pt_debug(cd->dev, DL_ERROR,
  1360. "%s: Invalid parameter size\n", __func__);
  1361. return;
  1362. }
  1363. param_buf = &param_buf[2];
  1364. while (i < param_size)
  1365. param_value += *(param_buf++) << (8 * i++);
  1366. /* Check command response for Set Parameter command */
  1367. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1368. || (cd->response_buf[4] &
  1369. PIP1_RESP_COMMAND_ID_MASK) !=
  1370. PIP1_CMD_ID_SET_PARAM
  1371. || cd->response_buf[5] != param_id
  1372. || cd->response_buf[6] != param_size) {
  1373. pt_debug(cd->dev, DL_ERROR,
  1374. "%s: Set Parameter command not successful\n",
  1375. __func__);
  1376. return;
  1377. }
  1378. pt_add_parameter(cd, param_id, param_value, param_size);
  1379. }
  1380. /*******************************************************************************
  1381. * FUNCTION: pt_check_command
  1382. *
  1383. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1384. * called here to check output command and store parameter to the list.
  1385. *
  1386. * PARAMETERS:
  1387. * *cd - pointer to core data
  1388. * *hid_output - pointer to hid output data structure
  1389. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1390. ******************************************************************************/
  1391. static void pt_check_command(struct pt_core_data *cd,
  1392. struct pt_hid_output *hid_output, bool raw)
  1393. {
  1394. pt_check_set_parameter(cd, hid_output, raw);
  1395. }
  1396. /*******************************************************************************
  1397. * FUNCTION: pt_hid_output_validate_response
  1398. *
  1399. * SUMMARY: Validate the response of application or bootloader command.
  1400. *
  1401. * RETURN:
  1402. * 0 = success
  1403. * !0 = failure
  1404. *
  1405. * PARAMETERS:
  1406. * *cd - pointer to core data
  1407. * *hid_output - pointer to hid output data structure
  1408. ******************************************************************************/
  1409. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1410. struct pt_hid_output *hid_output)
  1411. {
  1412. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1413. return pt_hid_output_validate_bl_response(cd, hid_output);
  1414. return pt_hid_output_validate_app_response(cd, hid_output);
  1415. }
  1416. /*******************************************************************************
  1417. * FUNCTION: pt_hid_send_output_user_
  1418. *
  1419. * SUMMARY: Blindly send user data to the DUT.
  1420. *
  1421. * RETURN:
  1422. * 0 = success
  1423. * !0 = failure
  1424. *
  1425. * PARAMETERS:
  1426. * *cd - pointer to core data
  1427. * *hid_output - pointer to the command to send
  1428. ******************************************************************************/
  1429. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1430. struct pt_hid_output *hid_output)
  1431. {
  1432. int rc = 0;
  1433. int cmd;
  1434. if (!hid_output->length || !hid_output->write_buf)
  1435. return -EINVAL;
  1436. if (cd->pip2_prot_active) {
  1437. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1438. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1439. } else
  1440. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1441. pt_debug(cd->dev, DL_INFO,
  1442. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1443. __func__, hid_output->length, cmd);
  1444. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1445. hid_output->length, ">>> User CMD");
  1446. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1447. hid_output->write_buf, NULL);
  1448. if (rc)
  1449. pt_debug(cd->dev, DL_ERROR,
  1450. "%s: Fail pt_adap_transfer\n", __func__);
  1451. return rc;
  1452. }
  1453. /*******************************************************************************
  1454. * FUNCTION: pt_hid_send_output_user_and_wait_
  1455. *
  1456. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1457. *
  1458. * RETURN:
  1459. * 0 = success
  1460. * !0 = failure
  1461. *
  1462. * PARAMETERS:
  1463. * *cd - pointer to core data
  1464. * *hid_output - pointer to the command to send
  1465. ******************************************************************************/
  1466. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1467. struct pt_hid_output *hid_output)
  1468. {
  1469. int rc = 0;
  1470. int t;
  1471. mutex_lock(&cd->system_lock);
  1472. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1473. mutex_unlock(&cd->system_lock);
  1474. rc = pt_hid_send_output_user_(cd, hid_output);
  1475. if (rc)
  1476. goto error;
  1477. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1478. msecs_to_jiffies(cd->pip_cmd_timeout));
  1479. if (IS_TMO(t)) {
  1480. #ifdef TTDL_DIAGNOSTICS
  1481. cd->bus_transmit_error_count++;
  1482. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1483. #endif /* TTDL_DIAGNOSTICS */
  1484. pt_debug(cd->dev, DL_ERROR,
  1485. "%s: HID output cmd execution timed out\n",
  1486. __func__);
  1487. rc = -ETIME;
  1488. goto error;
  1489. }
  1490. pt_check_command(cd, hid_output, true);
  1491. goto exit;
  1492. error:
  1493. mutex_lock(&cd->system_lock);
  1494. cd->hid_cmd_state = 0;
  1495. mutex_unlock(&cd->system_lock);
  1496. exit:
  1497. return rc;
  1498. }
  1499. /*******************************************************************************
  1500. * FUNCTION: pt_check_irq_asserted
  1501. *
  1502. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1503. * the FW can hold the INT line low ~150us after the read is complete.
  1504. * NOTE: if irq_stat is not defined this function will return false
  1505. *
  1506. * RETURN:
  1507. * true = IRQ asserted
  1508. * false = IRQ not asserted
  1509. *
  1510. * PARAMETERS:
  1511. * *cd - pointer to core data
  1512. ******************************************************************************/
  1513. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1514. {
  1515. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1516. /*
  1517. * Workaround for FW defect, CDT165308
  1518. * bl_launch app creates a glitch in IRQ line
  1519. */
  1520. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1521. && cd->cpdata->irq_stat) {
  1522. /*
  1523. * in X1S panel and GC1546 panel, the width for the INT
  1524. * glitch is about 4us,the normal INT width of response
  1525. * will last more than 200us, so use 10us delay
  1526. * for distinguish the glitch the normal INT is enough.
  1527. */
  1528. udelay(10);
  1529. }
  1530. #endif
  1531. if (cd->cpdata->irq_stat) {
  1532. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1533. == PT_IRQ_ASSERTED_VALUE) {
  1534. /* Debounce to allow FW to release INT */
  1535. usleep_range(100, 200);
  1536. }
  1537. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1538. == PT_IRQ_ASSERTED_VALUE)
  1539. return true;
  1540. else
  1541. return false;
  1542. }
  1543. return true;
  1544. }
  1545. /*******************************************************************************
  1546. * FUNCTION: pt_flush_bus
  1547. *
  1548. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1549. * Used if IRQ is found to be stuck low
  1550. *
  1551. * RETURN: Length of bytes read from bus
  1552. *
  1553. * PARAMETERS:
  1554. * *cd - pointer to core data
  1555. * flush_type - type of flush
  1556. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1557. * - PT_FLUSH_BUS_FULL_256_READ
  1558. * *read_buf - pointer to store read data
  1559. ******************************************************************************/
  1560. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1561. u8 flush_type, u8 *read_buf)
  1562. {
  1563. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1564. u16 pip_len;
  1565. int bytes_read;
  1566. int rc = 0;
  1567. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1568. rc = pt_adap_read_default(cd, buf, 2);
  1569. if (rc) {
  1570. bytes_read = 0;
  1571. goto exit;
  1572. }
  1573. pip_len = get_unaligned_le16(&buf[0]);
  1574. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1575. #ifdef TTDL_DIAGNOSTICS
  1576. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1577. #endif
  1578. bytes_read = 2;
  1579. pt_debug(cd->dev, DL_INFO,
  1580. "%s: Empty buf detected - len=0x%04X\n",
  1581. __func__, pip_len);
  1582. } else if (pip_len == 0) {
  1583. bytes_read = 0;
  1584. pt_debug(cd->dev, DL_INFO,
  1585. "%s: Sentinel detected\n", __func__);
  1586. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1587. pt_debug(cd->dev, DL_ERROR,
  1588. "%s: Illegal len=0x%04x, force %d byte read\n",
  1589. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1590. rc = pt_adap_read_default(cd, buf,
  1591. PT_MAX_PIP2_MSG_SIZE);
  1592. if (!rc)
  1593. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1594. else
  1595. bytes_read = 0;
  1596. } else {
  1597. pt_debug(cd->dev, DL_INFO,
  1598. "%s: Flush read of %d bytes...\n",
  1599. __func__, pip_len);
  1600. rc = pt_adap_read_default(cd, buf, pip_len);
  1601. if (!rc)
  1602. bytes_read = pip_len;
  1603. else
  1604. bytes_read = 0;
  1605. }
  1606. } else {
  1607. pt_debug(cd->dev, DL_INFO,
  1608. "%s: Forced flush of max %d bytes...\n",
  1609. __func__, PT_MAX_PIP2_MSG_SIZE);
  1610. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1611. if (!rc)
  1612. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1613. else
  1614. bytes_read = 0;
  1615. }
  1616. if (read_buf && (bytes_read > 3))
  1617. memcpy(read_buf, buf, bytes_read);
  1618. exit:
  1619. return bytes_read;
  1620. }
  1621. /*******************************************************************************
  1622. * FUNCTION: pt_flush_bus_if_irq_asserted
  1623. *
  1624. * SUMMARY: This function will flush the active bus if the INT is found to be
  1625. * asserted.
  1626. *
  1627. * RETURN: bytes cleared from bus
  1628. *
  1629. * PARAMETERS:
  1630. * *cd - pointer the core data structure
  1631. * flush_type - type of flush
  1632. * - PT_FLUSH_BUS_BASED_ON_LEN
  1633. * - PT_FLUSH_BUS_FULL_256_READ
  1634. ******************************************************************************/
  1635. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1636. {
  1637. int count = 0;
  1638. int bytes_read = 0;
  1639. while (pt_check_irq_asserted(cd) && count < 5) {
  1640. count++;
  1641. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1642. if (bytes_read) {
  1643. pt_debug(cd->dev, DL_WARN,
  1644. "%s: Cleared %d bytes off bus\n",
  1645. __func__, bytes_read);
  1646. }
  1647. }
  1648. if (pt_check_irq_asserted(cd)) {
  1649. pt_debug(cd->dev, DL_ERROR,
  1650. "%s: IRQ still asserted, %d bytes read\n",
  1651. __func__, bytes_read);
  1652. } else {
  1653. pt_debug(cd->dev, DL_INFO,
  1654. "%s: IRQ cleared, %d bytes read\n",
  1655. __func__, bytes_read);
  1656. }
  1657. return bytes_read;
  1658. }
  1659. /*******************************************************************************
  1660. * FUNCTION: pt_hid_send_output_
  1661. *
  1662. * SUMMARY: Send a touch application command to the DUT
  1663. *
  1664. * RETURN:
  1665. * 0 = success
  1666. * !0 = failure
  1667. *
  1668. * PARAMETERS:
  1669. * *cd - pointer to core data
  1670. * *hid_output - pointer to the command to send
  1671. ******************************************************************************/
  1672. static int pt_hid_send_output_(struct pt_core_data *cd,
  1673. struct pt_hid_output *hid_output)
  1674. {
  1675. int rc = 0;
  1676. u8 *cmd;
  1677. u16 length;
  1678. u16 crc;
  1679. u8 report_id;
  1680. u8 cmd_offset = 0;
  1681. u8 cmd_allocated = 0;
  1682. switch (hid_output->cmd_type) {
  1683. case PIP1_CMD_TYPE_FW:
  1684. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1685. length = 5;
  1686. break;
  1687. case PIP1_CMD_TYPE_BL:
  1688. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1689. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1690. break;
  1691. default:
  1692. return -EINVAL;
  1693. }
  1694. length += hid_output->write_length;
  1695. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1696. cmd = kzalloc(length + 2, GFP_KERNEL);
  1697. if (!cmd)
  1698. return -ENOMEM;
  1699. cmd_allocated = 1;
  1700. } else {
  1701. cmd = cd->cmd_buf;
  1702. }
  1703. /* Set Output register */
  1704. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1705. sizeof(cd->hid_desc.output_register));
  1706. cmd_offset += sizeof(cd->hid_desc.output_register);
  1707. cmd[cmd_offset++] = LOW_BYTE(length);
  1708. cmd[cmd_offset++] = HI_BYTE(length);
  1709. cmd[cmd_offset++] = report_id;
  1710. cmd[cmd_offset++] = 0x0; /* reserved */
  1711. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1712. cmd[cmd_offset++] = PIP1_BL_SOP;
  1713. cmd[cmd_offset++] = hid_output->command_code;
  1714. /* Set Data Length for bootloader */
  1715. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1716. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1717. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1718. }
  1719. /* Set Data */
  1720. if (hid_output->write_length && hid_output->write_buf) {
  1721. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1722. hid_output->write_length);
  1723. cmd_offset += hid_output->write_length;
  1724. }
  1725. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1726. crc = _pt_compute_crc(&cmd[6],
  1727. hid_output->write_length + 4);
  1728. cmd[cmd_offset++] = LOW_BYTE(crc);
  1729. cmd[cmd_offset++] = HI_BYTE(crc);
  1730. cmd[cmd_offset++] = PIP1_BL_EOP;
  1731. }
  1732. pt_debug(cd->dev, DL_INFO,
  1733. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1734. __func__, length + 2, hid_output->command_code);
  1735. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1736. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1737. if (rc)
  1738. pt_debug(cd->dev, DL_ERROR,
  1739. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1740. if (cmd_allocated)
  1741. kfree(cmd);
  1742. return rc;
  1743. }
  1744. /*******************************************************************************
  1745. * FUNCTION: pt_pip1_send_output_and_wait_
  1746. *
  1747. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1748. *
  1749. * RETURN:
  1750. * 0 = success
  1751. * !0 = failure
  1752. *
  1753. * PARAMETERS:
  1754. * *cd - pointer to core data
  1755. * *hid_output - pointer to the command to send
  1756. ******************************************************************************/
  1757. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1758. struct pt_hid_output *hid_output)
  1759. {
  1760. int rc = 0;
  1761. int t;
  1762. u16 timeout_ms;
  1763. mutex_lock(&cd->system_lock);
  1764. cd->hid_cmd_state = hid_output->command_code + 1;
  1765. mutex_unlock(&cd->system_lock);
  1766. if (hid_output->timeout_ms)
  1767. timeout_ms = hid_output->timeout_ms;
  1768. else
  1769. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1770. rc = pt_hid_send_output_(cd, hid_output);
  1771. if (rc)
  1772. goto error;
  1773. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1774. msecs_to_jiffies(timeout_ms));
  1775. if (IS_TMO(t)) {
  1776. #ifdef TTDL_DIAGNOSTICS
  1777. cd->bus_transmit_error_count++;
  1778. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1779. #endif /* TTDL_DIAGNOSTICS */
  1780. pt_debug(cd->dev, DL_ERROR,
  1781. "%s: HID output cmd execution timed out (%dms)\n",
  1782. __func__, timeout_ms);
  1783. rc = -ETIME;
  1784. goto error;
  1785. }
  1786. if (!hid_output->novalidate)
  1787. rc = pt_hid_output_validate_response(cd, hid_output);
  1788. pt_check_command(cd, hid_output, false);
  1789. goto exit;
  1790. error:
  1791. mutex_lock(&cd->system_lock);
  1792. cd->hid_cmd_state = 0;
  1793. mutex_unlock(&cd->system_lock);
  1794. exit:
  1795. return rc;
  1796. }
  1797. /*******************************************************************************
  1798. * FUNCTION: pt_hid_output_user_cmd_
  1799. *
  1800. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1801. * to the DUT waiting for the response and loading it into the read buffer
  1802. *
  1803. * RETURN:
  1804. * 0 = success
  1805. * !0 = failure
  1806. *
  1807. * PARAMETERS:
  1808. * *cd - pointer to core data
  1809. * read_len - expected read length of the response
  1810. * *read_buf - pointer to where the response will be loaded
  1811. * write_len - length of the write buffer
  1812. * *write_buf - pointer to the write buffer
  1813. * *actual_read_len - pointer to the actual amount of data read back
  1814. ******************************************************************************/
  1815. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1816. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1817. u16 *actual_read_len)
  1818. {
  1819. int rc = 0;
  1820. u16 size;
  1821. struct pt_hid_output hid_output = {
  1822. .length = write_len,
  1823. .write_buf = write_buf,
  1824. };
  1825. #ifdef TTHE_TUNER_SUPPORT
  1826. if (!cd->pip2_send_user_cmd) {
  1827. int command_code = 0;
  1828. int len;
  1829. /* Print up to cmd ID */
  1830. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1831. if (write_len < len)
  1832. len = write_len;
  1833. else
  1834. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1835. & PIP1_CMD_COMMAND_ID_MASK;
  1836. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1837. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1838. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1839. tthe_print(cd, write_buf, len, "CMD=");
  1840. }
  1841. #endif
  1842. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1843. if (rc)
  1844. return rc;
  1845. /* Get the response size from the first 2 bytes in the response */
  1846. size = get_unaligned_le16(&cd->response_buf[0]);
  1847. /* Ensure size is not greater than max buffer size */
  1848. if (size > PT_MAX_PIP2_MSG_SIZE)
  1849. size = PT_MAX_PIP2_MSG_SIZE;
  1850. /* Minimum size to read is the 2 byte len field */
  1851. if (size == 0)
  1852. size = 2;
  1853. if (size > read_len) {
  1854. pt_debug(cd->dev, DL_ERROR,
  1855. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1856. __func__, size, read_len);
  1857. *actual_read_len = 0;
  1858. return -EIO;
  1859. }
  1860. memcpy(read_buf, cd->response_buf, size);
  1861. *actual_read_len = size;
  1862. return 0;
  1863. }
  1864. /*******************************************************************************
  1865. * FUNCTION: pt_hid_output_user_cmd
  1866. *
  1867. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1868. * the DUT.
  1869. *
  1870. * RETURN:
  1871. * 0 = success
  1872. * !0 = failure
  1873. *
  1874. * PARAMETERS:
  1875. * *cd - pointer to core data
  1876. * read_len - length of data to read
  1877. * *read_buf - pointer to store read data
  1878. * write_len - length of data to write
  1879. * *write_buf - pointer to buffer to write
  1880. * *actual_read_len - pointer to store data length actually read
  1881. ******************************************************************************/
  1882. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1883. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1884. u16 *actual_read_len)
  1885. {
  1886. int rc;
  1887. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1888. if (rc < 0) {
  1889. pt_debug(cd->dev, DL_ERROR,
  1890. "%s: fail get exclusive ex=%p own=%p\n",
  1891. __func__, cd->exclusive_dev, cd->dev);
  1892. return rc;
  1893. }
  1894. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1895. write_len, write_buf, actual_read_len);
  1896. if (release_exclusive(cd, cd->dev) < 0)
  1897. pt_debug(cd->dev, DL_ERROR,
  1898. "%s: fail to release exclusive\n", __func__);
  1899. return rc;
  1900. }
  1901. /*******************************************************************************
  1902. * FUNCTION: _pt_request_pip2_send_cmd
  1903. *
  1904. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1905. * interrupt and reads response data to read_buf
  1906. *
  1907. * RETURN:
  1908. * 0 = success
  1909. * !0 = failure
  1910. *
  1911. * PARAMETERS:
  1912. * *dev - pointer to device structure
  1913. * protect - flag to run in protected mode
  1914. * id - ID of PIP command
  1915. * *data - pointer to PIP data payload
  1916. * report_body_len - report length
  1917. * *read_buf - pointer to response buffer
  1918. * *actual_read_len - pointer to response buffer length
  1919. ******************************************************************************/
  1920. static int _pt_request_pip2_send_cmd(struct device *dev,
  1921. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1922. u16 *actual_read_len)
  1923. {
  1924. struct pt_core_data *cd = dev_get_drvdata(dev);
  1925. struct pip2_cmd_structure pip2_cmd;
  1926. int rc = 0;
  1927. u16 i = 0;
  1928. u16 j = 0;
  1929. u16 write_len;
  1930. u8 *write_buf = NULL;
  1931. u16 read_len;
  1932. u8 extra_bytes;
  1933. if (report_body_len > 247)
  1934. return -EPROTO;
  1935. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1936. /* Hard coded register for PIP2.x */
  1937. pip2_cmd.reg[0] = 0x01;
  1938. pip2_cmd.reg[1] = 0x01;
  1939. /*
  1940. * For PIP2.1+ the length field value includes itself:
  1941. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1942. *
  1943. * The overall write length must include only the register:
  1944. * ADD 2: 2 (Register)
  1945. */
  1946. extra_bytes = 6;
  1947. write_len = 2;
  1948. /* PIP2 the CMD ID is a 7bit field */
  1949. if (id > PIP2_CMD_ID_END) {
  1950. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1951. __func__, id);
  1952. rc = -EINVAL;
  1953. goto exit;
  1954. }
  1955. pip2_cmd.len = report_body_len + extra_bytes;
  1956. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1957. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1958. pip2_cmd.data = data;
  1959. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1960. /* Add the command length to the extra bytes based on PIP version */
  1961. write_len += pip2_cmd.len;
  1962. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1963. __func__, pip2_cmd.len, write_len);
  1964. write_buf = kzalloc(write_len, GFP_KERNEL);
  1965. if (write_buf == NULL) {
  1966. rc = -ENOMEM;
  1967. goto exit;
  1968. }
  1969. write_buf[i++] = pip2_cmd.reg[0];
  1970. write_buf[i++] = pip2_cmd.reg[1];
  1971. write_buf[i++] = pip2_cmd.len & 0xff;
  1972. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1973. write_buf[i++] = pip2_cmd.seq;
  1974. write_buf[i++] = pip2_cmd.id;
  1975. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1976. write_buf[j] = pip2_cmd.data[j-i];
  1977. write_buf[j++] = pip2_cmd.crc[0];
  1978. write_buf[j++] = pip2_cmd.crc[1];
  1979. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1980. if (read_len < 0)
  1981. read_len = 255;
  1982. pt_debug(dev, DL_INFO,
  1983. "%s cmd_id[0x%02X] expected response length:%d ",
  1984. __func__, pip2_cmd.id, read_len);
  1985. /*
  1986. * All PIP2 commands come through this function.
  1987. * Set flag for PIP2.x interface to allow response parsing to know
  1988. * how to decode the protocol header.
  1989. */
  1990. mutex_lock(&cd->system_lock);
  1991. cd->pip2_prot_active = true;
  1992. cd->pip2_send_user_cmd = true;
  1993. mutex_unlock(&cd->system_lock);
  1994. if (protect == PT_CORE_CMD_PROTECTED)
  1995. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1996. write_len, write_buf, actual_read_len);
  1997. else {
  1998. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1999. write_len, write_buf, actual_read_len);
  2000. }
  2001. if (rc) {
  2002. pt_debug(dev, DL_ERROR,
  2003. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  2004. __func__, rc);
  2005. goto exit;
  2006. }
  2007. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  2008. *actual_read_len);
  2009. exit:
  2010. mutex_lock(&cd->system_lock);
  2011. cd->pip2_prot_active = false;
  2012. cd->pip2_send_user_cmd = false;
  2013. mutex_unlock(&cd->system_lock);
  2014. kfree(write_buf);
  2015. return rc;
  2016. }
  2017. /*******************************************************************************
  2018. * FUNCTION: _pt_pip2_send_cmd_no_int
  2019. *
  2020. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2021. * reads response data to read_buf if response is available.
  2022. *
  2023. * NOTE:
  2024. * Interrupt MUST be disabled before to call this function.
  2025. *
  2026. * RETURN:
  2027. * 0 = success
  2028. * !0 = failure
  2029. *
  2030. * PARAMETERS:
  2031. * *dev - pointer to device structure
  2032. * protect - flag to run in protected mode
  2033. * id - ID of PIP command
  2034. * *data - pointer to PIP data payload
  2035. * report_body_len - report length
  2036. * *read_buf - pointer to response buffer
  2037. * *actual_read_len - pointer to response buffer length
  2038. ******************************************************************************/
  2039. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2040. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2041. u16 *actual_read_len)
  2042. {
  2043. int max_retry = 0;
  2044. int retry = 0;
  2045. int rc = 0;
  2046. u16 i = 0;
  2047. u16 j = 0;
  2048. u16 write_len;
  2049. u8 *write_buf = NULL;
  2050. u16 read_len;
  2051. u16 size = 0;
  2052. u8 response_seq = 0;
  2053. u8 extra_bytes;
  2054. u32 retry_interval = 0;
  2055. u32 retry_total_time = 0;
  2056. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2057. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2058. struct pt_core_data *cd = dev_get_drvdata(dev);
  2059. struct pip2_cmd_structure pip2_cmd;
  2060. if (report_body_len > 247)
  2061. return -EPROTO;
  2062. if (protect == PT_CORE_CMD_PROTECTED) {
  2063. rc = request_exclusive(cd,
  2064. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2065. if (rc < 0) {
  2066. pt_debug(cd->dev, DL_ERROR,
  2067. "%s: fail get exclusive ex=%p own=%p\n",
  2068. __func__, cd->exclusive_dev, cd->dev);
  2069. return rc;
  2070. }
  2071. }
  2072. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2073. /* Hard coded register for PIP2.x */
  2074. pip2_cmd.reg[0] = 0x01;
  2075. pip2_cmd.reg[1] = 0x01;
  2076. /*
  2077. * For PIP2.1+ the length field value includes itself:
  2078. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2079. *
  2080. * The overall write length must include only the register:
  2081. * ADD 2: 2 (Register)
  2082. */
  2083. extra_bytes = 6;
  2084. write_len = 2;
  2085. pip2_cmd.len = report_body_len + extra_bytes;
  2086. pip2_cmd.id = id;
  2087. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2088. pip2_cmd.data = data;
  2089. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2090. /* Add the command length to the extra bytes based on PIP version */
  2091. write_len += pip2_cmd.len;
  2092. write_buf = kzalloc(write_len, GFP_KERNEL);
  2093. if (write_buf == NULL) {
  2094. rc = -ENOMEM;
  2095. goto exit;
  2096. }
  2097. write_buf[i++] = pip2_cmd.reg[0];
  2098. write_buf[i++] = pip2_cmd.reg[1];
  2099. write_buf[i++] = pip2_cmd.len & 0xff;
  2100. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2101. write_buf[i++] = pip2_cmd.seq;
  2102. write_buf[i++] = pip2_cmd.id;
  2103. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2104. write_buf[j] = pip2_cmd.data[j-i];
  2105. write_buf[j++] = pip2_cmd.crc[0];
  2106. write_buf[j++] = pip2_cmd.crc[1];
  2107. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2108. if (read_len < 0)
  2109. read_len = 255;
  2110. pt_debug(dev, DL_INFO,
  2111. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2112. __func__, pip2_cmd.id, read_len);
  2113. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2114. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2115. if (rc) {
  2116. pt_debug(dev, DL_ERROR,
  2117. "%s: SPI write Error = %d\n",
  2118. __func__, rc);
  2119. goto exit;
  2120. }
  2121. #ifdef PT_POLL_RESP_BY_BUS
  2122. /*
  2123. * Frequent bus read can increase system load obviously. The expected
  2124. * first bus read should be valid and timely. The tollerance for
  2125. * usleep_range should be limited. The minimum response delay (between
  2126. * command finishes sending and INT pin falls) is less than 50
  2127. * microseconds. So the 10 microseconds should be maximum tollerance
  2128. * with the consideration that the unit to calculate the response delay
  2129. * is 10 microseconds and more precise is not necessary. Every
  2130. * additional 10 microseconds only contribute less than 3 milliseconds
  2131. * for whole BL.
  2132. */
  2133. usleep_range(resp_time_min, resp_time_min+10);
  2134. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2135. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2136. rc = pt_adap_read_default(cd, read_buf, read_len);
  2137. if (rc) {
  2138. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2139. __func__, rc);
  2140. break;
  2141. }
  2142. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2143. size = get_unaligned_le16(&read_buf[0]);
  2144. if ((size <= read_len) &&
  2145. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2146. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2147. break;
  2148. }
  2149. /*
  2150. * To reduce the bus and system load, increase the sleep
  2151. * step gradually:
  2152. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2153. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2154. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2155. */
  2156. retry++;
  2157. if (retry < 20) {
  2158. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2159. usleep_range(retry_interval,
  2160. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2161. } else if (retry < 40) {
  2162. retry_interval += 1000;
  2163. usleep_range(retry_interval, retry_interval + 1000);
  2164. } else {
  2165. retry_interval += 50000;
  2166. msleep(retry_interval/1000);
  2167. }
  2168. retry_total_time += retry_interval;
  2169. }
  2170. #else
  2171. /*
  2172. * Frequent GPIO read will not increase CPU/system load heavily if the
  2173. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2174. * fixed interval: 20 us.
  2175. */
  2176. usleep_range(resp_time_min, resp_time_min+10);
  2177. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2178. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2179. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2180. rc = pt_adap_read_default(cd, read_buf, read_len);
  2181. size = get_unaligned_le16(&read_buf[0]);
  2182. if (rc)
  2183. pt_debug(dev, DL_ERROR,
  2184. "%s: SPI read Error = %d\n",
  2185. __func__, rc);
  2186. else if (size > read_len) {
  2187. pt_debug(cd->dev, DL_ERROR,
  2188. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2189. __func__, size, read_len);
  2190. rc = -EIO;
  2191. }
  2192. break;
  2193. }
  2194. /*
  2195. * Poll GPIO with fixed interval 20 us, and tollerance is
  2196. * limited to 10 us to speed up the process.
  2197. */
  2198. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2199. usleep_range(retry_interval, retry_interval+10);
  2200. retry_total_time += retry_interval;
  2201. }
  2202. #endif
  2203. *actual_read_len = size;
  2204. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2205. pt_debug(dev, DL_ERROR,
  2206. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2207. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2208. *actual_read_len = 0;
  2209. rc = -EINVAL;
  2210. }
  2211. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2212. "<<< NO_INT Read");
  2213. exit:
  2214. kfree(write_buf);
  2215. if (protect == PT_CORE_CMD_PROTECTED) {
  2216. if (release_exclusive(cd, cd->dev) < 0)
  2217. pt_debug(cd->dev, DL_ERROR,
  2218. "%s: fail to release exclusive\n", __func__);
  2219. }
  2220. return rc;
  2221. }
  2222. /*******************************************************************************
  2223. * FUNCTION: pt_pip_null_
  2224. *
  2225. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2226. * This function is used by watchdog to check if the fw corrupts.
  2227. *
  2228. * RETURN:
  2229. * 0 = success
  2230. * !0 = failure
  2231. *
  2232. * PARAMETERS:
  2233. * *cd - pointer to core data
  2234. ******************************************************************************/
  2235. static int pt_pip_null_(struct pt_core_data *cd)
  2236. {
  2237. struct pt_hid_output hid_output = {
  2238. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2239. };
  2240. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2241. }
  2242. /*******************************************************************************
  2243. * FUNCTION: pt_pip_null
  2244. *
  2245. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2246. *
  2247. * RETURN:
  2248. * 0 = success
  2249. * !0 = failure
  2250. *
  2251. * PARAMETERS:
  2252. * *cd - pointer to core data
  2253. ******************************************************************************/
  2254. static int pt_pip_null(struct pt_core_data *cd)
  2255. {
  2256. int rc;
  2257. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2258. if (rc < 0) {
  2259. pt_debug(cd->dev, DL_ERROR,
  2260. "%s: fail get exclusive ex=%p own=%p\n",
  2261. __func__, cd->exclusive_dev, cd->dev);
  2262. return rc;
  2263. }
  2264. rc = pt_pip_null_(cd);
  2265. if (release_exclusive(cd, cd->dev) < 0)
  2266. pt_debug(cd->dev, DL_ERROR,
  2267. "%s: fail to release exclusive\n", __func__);
  2268. return rc;
  2269. }
  2270. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2271. /*******************************************************************************
  2272. * FUNCTION: pt_pip_start_bootloader_
  2273. *
  2274. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2275. *
  2276. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2277. * the WD active could cause this function to fail!
  2278. *
  2279. * RETURN:
  2280. * 0 = success
  2281. * !0 = failure
  2282. *
  2283. * PARAMETERS:
  2284. * *cd - pointer to core data
  2285. ******************************************************************************/
  2286. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2287. {
  2288. int rc = 0;
  2289. struct pt_hid_output hid_output = {
  2290. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2291. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2292. .reset_expected = 1,
  2293. };
  2294. if (cd->watchdog_enabled) {
  2295. pt_debug(cd->dev, DL_WARN,
  2296. "%s: watchdog isn't stopped before enter bl\n",
  2297. __func__);
  2298. goto exit;
  2299. }
  2300. /* Reset startup status after entering BL, new DUT enum required */
  2301. cd->startup_status = STARTUP_STATUS_START;
  2302. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2303. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2304. if (rc) {
  2305. pt_debug(cd->dev, DL_ERROR,
  2306. "%s: Start BL PIP cmd failed. rc = %d\n",
  2307. __func__, rc);
  2308. }
  2309. exit:
  2310. return rc;
  2311. }
  2312. /*******************************************************************************
  2313. * FUNCTION: pt_pip_start_bootloader
  2314. *
  2315. * SUMMARY: Protected function to force DUT to enter the BL
  2316. *
  2317. * RETURN:
  2318. * 0 = success
  2319. * !0 = failure
  2320. *
  2321. * PARAMETERS:
  2322. * *cd - pointer to core data structure
  2323. ******************************************************************************/
  2324. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2325. {
  2326. int rc;
  2327. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2328. if (rc < 0) {
  2329. pt_debug(cd->dev, DL_ERROR,
  2330. "%s: fail get exclusive ex=%p own=%p\n",
  2331. __func__, cd->exclusive_dev, cd->dev);
  2332. return rc;
  2333. }
  2334. rc = pt_pip_start_bootloader_(cd);
  2335. if (release_exclusive(cd, cd->dev) < 0)
  2336. pt_debug(cd->dev, DL_ERROR,
  2337. "%s: fail to release exclusive\n", __func__);
  2338. return rc;
  2339. }
  2340. /*******************************************************************************
  2341. * FUNCTION: _pt_request_pip_start_bl
  2342. *
  2343. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2344. * modules to request the DUT to enter the BL
  2345. *
  2346. * RETURN:
  2347. * 0 = success
  2348. * !0 = failure
  2349. *
  2350. * PARAMETERS:
  2351. * *dev - pointer to device structure
  2352. * protect - flag to run in protected mode
  2353. ******************************************************************************/
  2354. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2355. {
  2356. struct pt_core_data *cd = dev_get_drvdata(dev);
  2357. if (protect)
  2358. return pt_pip_start_bootloader(cd);
  2359. return pt_pip_start_bootloader_(cd);
  2360. }
  2361. /*******************************************************************************
  2362. * FUNCTION: pt_pip2_ver_load_ttdata
  2363. *
  2364. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2365. * command into the core data struct.
  2366. *
  2367. * RETURN: n/a
  2368. *
  2369. * PARAMETERS:
  2370. * *cd - pointer to core data structure
  2371. * len - Length of data in response_buf
  2372. ******************************************************************************/
  2373. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2374. {
  2375. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2376. struct pt_pip2_version_full *full_ver;
  2377. struct pt_pip2_version *ver;
  2378. /*
  2379. * The PIP2 VERSION command can return different lengths of data.
  2380. * The additional LOT fields are included when the packet
  2381. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2382. * NOTE:
  2383. * - The FW would swap the BL and FW versions when reporting
  2384. * the small packet.
  2385. * - Sub Lot bytes 16 and 17 are reserved.
  2386. */
  2387. if (len >= 0x1D) {
  2388. full_ver = (struct pt_pip2_version_full *)
  2389. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2390. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2391. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2392. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2393. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2394. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2395. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2396. /*
  2397. * BL PIP 2.02 and greater the version fields are
  2398. * swapped
  2399. */
  2400. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2401. ttdata->chip_rev =
  2402. get_unaligned_le16(&full_ver->chip_rev);
  2403. ttdata->chip_id =
  2404. get_unaligned_le16(&full_ver->chip_id);
  2405. } else {
  2406. ttdata->chip_rev =
  2407. get_unaligned_le16(&full_ver->chip_id);
  2408. ttdata->chip_id =
  2409. get_unaligned_le16(&full_ver->chip_rev);
  2410. }
  2411. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2412. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2413. sizeof(struct pt_pip2_version_full),
  2414. "PIP2 VERSION FULL");
  2415. } else {
  2416. ver = (struct pt_pip2_version *)
  2417. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2418. ttdata->pip_ver_major = ver->pip2_version_msb;
  2419. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2420. ttdata->bl_ver_major = ver->bl_version_msb;
  2421. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2422. ttdata->fw_ver_major = ver->fw_version_msb;
  2423. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2424. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2425. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2426. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2427. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2428. }
  2429. }
  2430. /*******************************************************************************
  2431. * FUNCTION: pt_si_get_ttdata
  2432. *
  2433. * SUMMARY: Function to load the version information from the system information
  2434. * PIP command into the core data struct.
  2435. *
  2436. * RETURN: n/a
  2437. *
  2438. * PARAMETERS:
  2439. * *cd - pointer to core data structure
  2440. ******************************************************************************/
  2441. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2442. {
  2443. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2444. struct pt_ttdata_dev *ttdata_dev =
  2445. (struct pt_ttdata_dev *)
  2446. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2447. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2448. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2449. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2450. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2451. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2452. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2453. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2454. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2455. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2456. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2457. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2458. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2459. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2460. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2461. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2462. }
  2463. /*******************************************************************************
  2464. * FUNCTION: pt_si_get_sensing_conf_data
  2465. *
  2466. * SUMMARY: Function to load the sensing information from the system information
  2467. * PIP command into the core data struct.
  2468. *
  2469. * RETURN: n/a
  2470. *
  2471. * PARAMETERS:
  2472. * *cd - pointer to core data structure
  2473. ******************************************************************************/
  2474. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2475. {
  2476. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2477. struct pt_sensing_conf_data_dev *scd_dev =
  2478. (struct pt_sensing_conf_data_dev *)
  2479. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2480. scd->electrodes_x = scd_dev->electrodes_x;
  2481. scd->electrodes_y = scd_dev->electrodes_y;
  2482. scd->origin_x = scd_dev->origin_x;
  2483. scd->origin_y = scd_dev->origin_y;
  2484. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2485. if (scd->origin_x & 0x02) {
  2486. scd->tx_num = scd->electrodes_x;
  2487. scd->rx_num = scd->electrodes_y;
  2488. } else {
  2489. scd->tx_num = scd->electrodes_y;
  2490. scd->rx_num = scd->electrodes_x;
  2491. }
  2492. /*
  2493. * When the Panel ID is coming from an XY pin and not a dedicated
  2494. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2495. * other DUTs as the loader will use cd->pid_for_loader to generate
  2496. * the bin file name but will ignore it if pid_for_loader is still
  2497. * set to PANEL_ID_NOT_ENABLED
  2498. */
  2499. if (cd->panel_id_support &
  2500. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2501. mutex_lock(&cd->system_lock);
  2502. cd->pid_for_loader = scd_dev->panel_id;
  2503. mutex_unlock(&cd->system_lock);
  2504. }
  2505. scd->panel_id = scd_dev->panel_id;
  2506. scd->btn = scd_dev->btn;
  2507. scd->scan_mode = scd_dev->scan_mode;
  2508. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2509. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2510. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2511. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2512. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2513. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2514. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2515. sizeof(struct pt_sensing_conf_data_dev),
  2516. "sensing_conf_data");
  2517. }
  2518. /*******************************************************************************
  2519. * FUNCTION: pt_si_setup
  2520. *
  2521. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2522. *
  2523. * RETURN:
  2524. * 0 = success
  2525. * !0 = failure
  2526. *
  2527. * PARAMETERS:
  2528. * *cd - pointer to core data structure
  2529. ******************************************************************************/
  2530. static int pt_si_setup(struct pt_core_data *cd)
  2531. {
  2532. struct pt_sysinfo *si = &cd->sysinfo;
  2533. int max_tch = si->sensing_conf_data.max_tch;
  2534. if (!si->xy_data)
  2535. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2536. GFP_KERNEL);
  2537. if (!si->xy_data)
  2538. return -ENOMEM;
  2539. if (!si->xy_mode)
  2540. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2541. if (!si->xy_mode) {
  2542. kfree(si->xy_data);
  2543. return -ENOMEM;
  2544. }
  2545. return 0;
  2546. }
  2547. /*******************************************************************************
  2548. * FUNCTION: pt_si_get_btn_data
  2549. *
  2550. * SUMMARY: Setup the core data button information based on the response of the
  2551. * System Information PIP command.
  2552. *
  2553. * RETURN:
  2554. * 0 = success
  2555. * !0 = failure
  2556. *
  2557. * PARAMETERS:
  2558. * *cd - pointer to core data structure
  2559. ******************************************************************************/
  2560. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2561. {
  2562. struct pt_sysinfo *si = &cd->sysinfo;
  2563. int num_btns = 0;
  2564. int num_defined_keys;
  2565. u16 *key_table;
  2566. int btn;
  2567. int i;
  2568. int rc = 0;
  2569. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2570. & PIP1_SYSINFO_BTN_MASK;
  2571. size_t btn_keys_size;
  2572. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2573. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2574. if (btns & (1 << i))
  2575. num_btns++;
  2576. }
  2577. si->num_btns = num_btns;
  2578. if (num_btns) {
  2579. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2580. if (!si->btn)
  2581. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2582. if (!si->btn)
  2583. return -ENOMEM;
  2584. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2585. num_defined_keys = 0;
  2586. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2587. num_defined_keys = 0;
  2588. else
  2589. num_defined_keys = cd->cpdata->sett
  2590. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2591. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2592. key_table = (u16 *)cd->cpdata->sett
  2593. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2594. si->btn[btn].key_code = key_table[btn];
  2595. si->btn[btn].enabled = true;
  2596. }
  2597. for (; btn < num_btns; btn++) {
  2598. si->btn[btn].key_code = KEY_RESERVED;
  2599. si->btn[btn].enabled = true;
  2600. }
  2601. return rc;
  2602. }
  2603. kfree(si->btn);
  2604. si->btn = NULL;
  2605. return rc;
  2606. }
  2607. /*******************************************************************************
  2608. * FUNCTION: pt_si_put_log_data
  2609. *
  2610. * SUMMARY: Prints all sys info data to kmsg log
  2611. *
  2612. * RETURN: n/a
  2613. *
  2614. * PARAMETERS:
  2615. * *cd - pointer to core data structure
  2616. ******************************************************************************/
  2617. static void pt_si_put_log_data(struct pt_core_data *cd)
  2618. {
  2619. struct pt_sysinfo *si = &cd->sysinfo;
  2620. struct pt_ttdata *ttdata = &si->ttdata;
  2621. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2622. int i;
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2624. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2626. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2628. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2630. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2632. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2634. __func__, ttdata->revctrl, ttdata->revctrl);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2636. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2638. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2639. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2640. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2641. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2642. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2643. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2644. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2645. for (i = 0; i < PT_NUM_MFGID; i++)
  2646. pt_debug(cd->dev, DL_DEBUG,
  2647. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2648. __func__, i, ttdata->mfg_id[i],
  2649. ttdata->mfg_id[i]);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2651. __func__, ttdata->post_code, ttdata->post_code);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2653. __func__, scd->electrodes_x, scd->electrodes_x);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2655. __func__, scd->electrodes_y, scd->electrodes_y);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2657. __func__, scd->len_x, scd->len_x);
  2658. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2659. __func__, scd->len_y, scd->len_y);
  2660. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2661. __func__, scd->res_x, scd->res_x);
  2662. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2663. __func__, scd->res_y, scd->res_y);
  2664. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2665. __func__, scd->max_z, scd->max_z);
  2666. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2667. __func__, scd->origin_x, scd->origin_x);
  2668. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2669. __func__, scd->origin_y, scd->origin_y);
  2670. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2671. __func__, scd->panel_id, scd->panel_id);
  2672. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2673. __func__, scd->btn, scd->btn);
  2674. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2675. __func__, scd->scan_mode, scd->scan_mode);
  2676. pt_debug(cd->dev, DL_DEBUG,
  2677. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2678. __func__, scd->max_tch, scd->max_tch);
  2679. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2680. __func__, si->xy_mode);
  2681. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2682. __func__, si->xy_data);
  2683. }
  2684. /*******************************************************************************
  2685. * FUNCTION: pt_get_sysinfo_regs
  2686. *
  2687. * SUMMARY: Setup all the core data System information based on the response
  2688. * of the System Information PIP command.
  2689. *
  2690. * RETURN:
  2691. * 0 = success
  2692. * !0 = failure
  2693. *
  2694. * PARAMETERS:
  2695. * *cd - pointer to core data structure
  2696. ******************************************************************************/
  2697. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2698. {
  2699. struct pt_sysinfo *si = &cd->sysinfo;
  2700. int rc;
  2701. rc = pt_si_get_btn_data(cd);
  2702. if (rc < 0)
  2703. return rc;
  2704. pt_si_get_ttdata(cd);
  2705. pt_si_get_sensing_conf_data(cd);
  2706. pt_si_setup(cd);
  2707. pt_si_put_log_data(cd);
  2708. si->ready = true;
  2709. return rc;
  2710. }
  2711. /*******************************************************************************
  2712. * FUNCTION: pt_free_si_ptrs
  2713. *
  2714. * SUMMARY: Frees all memory associated with the System Information within
  2715. * core data
  2716. *
  2717. * RETURN: n/a
  2718. *
  2719. * PARAMETERS:
  2720. * *cd - pointer to core data structure
  2721. ******************************************************************************/
  2722. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2723. {
  2724. struct pt_sysinfo *si = &cd->sysinfo;
  2725. kfree(si->btn);
  2726. kfree(si->xy_mode);
  2727. kfree(si->xy_data);
  2728. }
  2729. /*******************************************************************************
  2730. * FUNCTION: pt_hid_output_get_sysinfo_
  2731. *
  2732. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2733. * response.
  2734. *
  2735. * RETURN::
  2736. * 0 = success
  2737. * !0 = failure
  2738. *
  2739. * PARAMETERS:
  2740. * *cd - pointer to core data structure
  2741. ******************************************************************************/
  2742. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2743. {
  2744. int rc = 0;
  2745. struct pt_hid_output hid_output = {
  2746. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2747. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2748. };
  2749. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2750. if (rc)
  2751. return rc;
  2752. /* Parse the sysinfo data */
  2753. rc = pt_get_sysinfo_regs(cd);
  2754. if (rc)
  2755. pt_free_si_ptrs(cd);
  2756. return rc;
  2757. }
  2758. /*******************************************************************************
  2759. * FUNCTION: pt_hid_output_get_sysinfo
  2760. *
  2761. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2762. *
  2763. * RETURN::
  2764. * 0 = success
  2765. * !0 = failure
  2766. *
  2767. * PARAMETERS:
  2768. * *cd - pointer to core data structure
  2769. ******************************************************************************/
  2770. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2771. {
  2772. int rc;
  2773. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2774. if (rc < 0) {
  2775. pt_debug(cd->dev, DL_ERROR,
  2776. "%s: fail get exclusive ex=%p own=%p\n",
  2777. __func__, cd->exclusive_dev, cd->dev);
  2778. return rc;
  2779. }
  2780. rc = pt_hid_output_get_sysinfo_(cd);
  2781. if (release_exclusive(cd, cd->dev) < 0)
  2782. pt_debug(cd->dev, DL_ERROR,
  2783. "%s: fail to release exclusive\n", __func__);
  2784. return rc;
  2785. }
  2786. /*******************************************************************************
  2787. * FUNCTION: pt_pip_suspend_scanning_
  2788. *
  2789. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2790. *
  2791. * RETURN::
  2792. * 0 = success
  2793. * !0 = failure
  2794. *
  2795. * PARAMETERS:
  2796. * *cd - pointer to core data structure
  2797. ******************************************************************************/
  2798. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2799. {
  2800. int rc = 0;
  2801. struct pt_hid_output hid_output = {
  2802. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2803. };
  2804. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2805. if (rc) {
  2806. pt_debug(cd->dev, DL_ERROR,
  2807. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2808. __func__, rc);
  2809. }
  2810. return rc;
  2811. }
  2812. /*******************************************************************************
  2813. * FUNCTION: pt_pip_suspend_scanning
  2814. *
  2815. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2816. *
  2817. * RETURN::
  2818. * 0 = success
  2819. * !0 = failure
  2820. *
  2821. * PARAMETERS:
  2822. * *cd - pointer to core data structure
  2823. ******************************************************************************/
  2824. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2825. {
  2826. int rc;
  2827. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2828. if (rc < 0) {
  2829. pt_debug(cd->dev, DL_ERROR,
  2830. "%s: fail get exclusive ex=%p own=%p\n",
  2831. __func__, cd->exclusive_dev, cd->dev);
  2832. return rc;
  2833. }
  2834. rc = pt_pip_suspend_scanning_(cd);
  2835. if (release_exclusive(cd, cd->dev) < 0)
  2836. pt_debug(cd->dev, DL_ERROR,
  2837. "%s: fail to release exclusive\n", __func__);
  2838. return rc;
  2839. }
  2840. /*******************************************************************************
  2841. * FUNCTION: _pt_request_pip_suspend_scanning
  2842. *
  2843. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2844. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2845. *
  2846. * RETURN:
  2847. * 0 = success
  2848. * !0 = failure
  2849. *
  2850. * PARAMETERS:
  2851. * *dev - pointer to device structure
  2852. * protect - 0 = call non-protected function
  2853. * 1 = call protected function
  2854. ******************************************************************************/
  2855. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2856. int protect)
  2857. {
  2858. struct pt_core_data *cd = dev_get_drvdata(dev);
  2859. if (protect)
  2860. return pt_pip_suspend_scanning(cd);
  2861. return pt_pip_suspend_scanning_(cd);
  2862. }
  2863. /*******************************************************************************
  2864. * FUNCTION: pt_pip_resume_scanning_
  2865. *
  2866. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2867. *
  2868. * RETURN::
  2869. * 0 = success
  2870. * !0 = failure
  2871. *
  2872. * PARAMETERS:
  2873. * *cd - pointer to core data structure
  2874. ******************************************************************************/
  2875. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2876. {
  2877. int rc = 0;
  2878. struct pt_hid_output hid_output = {
  2879. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2880. };
  2881. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2882. if (rc) {
  2883. pt_debug(cd->dev, DL_ERROR,
  2884. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2885. __func__, rc);
  2886. }
  2887. return rc;
  2888. }
  2889. /*******************************************************************************
  2890. * FUNCTION: pt_pip_resume_scanning
  2891. *
  2892. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2893. *
  2894. * RETURN::
  2895. * 0 = success
  2896. * !0 = failure
  2897. *
  2898. * PARAMETERS:
  2899. * *cd - pointer to core data structure
  2900. ******************************************************************************/
  2901. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2902. {
  2903. int rc;
  2904. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2905. if (rc < 0) {
  2906. pt_debug(cd->dev, DL_ERROR,
  2907. "%s: fail get exclusive ex=%p own=%p\n",
  2908. __func__, cd->exclusive_dev, cd->dev);
  2909. return rc;
  2910. }
  2911. rc = pt_pip_resume_scanning_(cd);
  2912. if (release_exclusive(cd, cd->dev) < 0)
  2913. pt_debug(cd->dev, DL_ERROR,
  2914. "%s: fail to release exclusive\n", __func__);
  2915. return rc;
  2916. }
  2917. /*******************************************************************************
  2918. * FUNCTION: _pt_request_pip_resume_scanning
  2919. *
  2920. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2921. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2922. *
  2923. * RETURN::
  2924. * 0 = success
  2925. * !0 = failure
  2926. *
  2927. * PARAMETERS:
  2928. * *dev - pointer to device structure
  2929. * protect - 0 = call non-protected function
  2930. * 1 = call protected function
  2931. ******************************************************************************/
  2932. static int _pt_request_pip_resume_scanning(struct device *dev,
  2933. int protect)
  2934. {
  2935. struct pt_core_data *cd = dev_get_drvdata(dev);
  2936. if (protect)
  2937. return pt_pip_resume_scanning(cd);
  2938. return pt_pip_resume_scanning_(cd);
  2939. }
  2940. /*******************************************************************************
  2941. * FUNCTION: pt_pip_get_param_
  2942. *
  2943. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2944. * the 32bit parameter value
  2945. *
  2946. * RETURN:
  2947. * 0 = success
  2948. * !0 = failure
  2949. *
  2950. * PARAMETERS:
  2951. * *cd - pointer to core data
  2952. * param_id - parameter ID to retrieve
  2953. * *value - value of DUT parameter
  2954. ******************************************************************************/
  2955. static int pt_pip_get_param_(struct pt_core_data *cd,
  2956. u8 param_id, u32 *value)
  2957. {
  2958. int write_length = 1;
  2959. u8 param[1] = { param_id };
  2960. u8 read_param_id;
  2961. int param_size;
  2962. u8 *ptr;
  2963. int rc = 0;
  2964. int i;
  2965. struct pt_hid_output hid_output = {
  2966. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2967. .write_length = write_length,
  2968. .write_buf = param,
  2969. };
  2970. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2971. if (rc)
  2972. return rc;
  2973. read_param_id = cd->response_buf[5];
  2974. if (read_param_id != param_id)
  2975. return -EPROTO;
  2976. param_size = cd->response_buf[6];
  2977. ptr = &cd->response_buf[7];
  2978. *value = 0;
  2979. for (i = 0; i < param_size; i++)
  2980. *value += ptr[i] << (i * 8);
  2981. return 0;
  2982. }
  2983. /*******************************************************************************
  2984. * FUNCTION: pt_pip_get_param
  2985. *
  2986. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2987. *
  2988. * RETURN:
  2989. * 0 = success
  2990. * !0 = failure
  2991. *
  2992. * PARAMETERS:
  2993. * *cd - pointer to core data
  2994. * param_id - parameter ID to retrieve
  2995. * *value - value of DUT parameter
  2996. ******************************************************************************/
  2997. static int pt_pip_get_param(struct pt_core_data *cd,
  2998. u8 param_id, u32 *value)
  2999. {
  3000. int rc;
  3001. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3002. if (rc < 0) {
  3003. pt_debug(cd->dev, DL_ERROR,
  3004. "%s: fail get exclusive ex=%p own=%p\n",
  3005. __func__, cd->exclusive_dev, cd->dev);
  3006. return rc;
  3007. }
  3008. rc = pt_pip_get_param_(cd, param_id, value);
  3009. if (release_exclusive(cd, cd->dev) < 0)
  3010. pt_debug(cd->dev, DL_ERROR,
  3011. "%s: fail to release exclusive\n", __func__);
  3012. return rc;
  3013. }
  3014. /*******************************************************************************
  3015. * FUNCTION: _pt_request_pip_get_param
  3016. *
  3017. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3018. * calls to the protected or unprotected call to pt_pip_get_param
  3019. *
  3020. * RETURN:
  3021. * 0 = success
  3022. * !0 = failure
  3023. *
  3024. * PARAMETERS:
  3025. * *dev - pointer to device structure
  3026. * protect - flag to call protected or non protected function
  3027. * param_id - parameter ID to retrieve
  3028. * *value - value of DUT parameter
  3029. ******************************************************************************/
  3030. int _pt_request_pip_get_param(struct device *dev,
  3031. int protect, u8 param_id, u32 *value)
  3032. {
  3033. struct pt_core_data *cd = dev_get_drvdata(dev);
  3034. if (protect)
  3035. return pt_pip_get_param(cd, param_id, value);
  3036. return pt_pip_get_param_(cd, param_id, value);
  3037. }
  3038. /*******************************************************************************
  3039. * FUNCTION: pt_pip_set_param_
  3040. *
  3041. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3042. * passed in value to flash
  3043. *
  3044. * RETURN:
  3045. * 0 = success
  3046. * !0 = failure
  3047. *
  3048. * PARAMETERS:
  3049. * *cd - pointer to core data
  3050. * param_id - parameter ID to set
  3051. * value - value to write
  3052. * size - size to write
  3053. ******************************************************************************/
  3054. static int pt_pip_set_param_(struct pt_core_data *cd,
  3055. u8 param_id, u32 value, u8 size)
  3056. {
  3057. u8 write_buf[6];
  3058. u8 *ptr = &write_buf[2];
  3059. int rc = 0;
  3060. int i;
  3061. struct pt_hid_output hid_output = {
  3062. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3063. .write_buf = write_buf,
  3064. };
  3065. write_buf[0] = param_id;
  3066. write_buf[1] = size;
  3067. for (i = 0; i < size; i++) {
  3068. ptr[i] = value & 0xFF;
  3069. value = value >> 8;
  3070. }
  3071. hid_output.write_length = 2 + size;
  3072. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3073. if (rc)
  3074. return rc;
  3075. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3076. return -EPROTO;
  3077. return 0;
  3078. }
  3079. /*******************************************************************************
  3080. * FUNCTION: pt_pip_set_param
  3081. *
  3082. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3083. *
  3084. * RETURN:
  3085. * 0 = success
  3086. * !0 = failure
  3087. *
  3088. * PARAMETERS:
  3089. * *cd - pointer to core data
  3090. * param_id - parameter ID to set
  3091. * value - value to write
  3092. * size - size to write
  3093. ******************************************************************************/
  3094. static int pt_pip_set_param(struct pt_core_data *cd,
  3095. u8 param_id, u32 value, u8 size)
  3096. {
  3097. int rc;
  3098. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3099. if (rc < 0) {
  3100. pt_debug(cd->dev, DL_ERROR,
  3101. "%s: fail get exclusive ex=%p own=%p\n",
  3102. __func__, cd->exclusive_dev, cd->dev);
  3103. return rc;
  3104. }
  3105. rc = pt_pip_set_param_(cd, param_id, value, size);
  3106. if (release_exclusive(cd, cd->dev) < 0)
  3107. pt_debug(cd->dev, DL_ERROR,
  3108. "%s: fail to release exclusive\n", __func__);
  3109. return rc;
  3110. }
  3111. /*******************************************************************************
  3112. * FUNCTION: _pt_request_pip_set_param
  3113. *
  3114. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3115. * calls to the protected or unprotected call to pt_pip_set_param
  3116. *
  3117. * RETURN:
  3118. * 0 = success
  3119. * !0 = failure
  3120. *
  3121. * PARAMETERS:
  3122. * *dev - pointer to device structure
  3123. * protect - flag to call protected or non-protected
  3124. * param_id - parameter ID to set
  3125. * value - value to write
  3126. * size - size to write
  3127. ******************************************************************************/
  3128. int _pt_request_pip_set_param(struct device *dev, int protect,
  3129. u8 param_id, u32 value, u8 size)
  3130. {
  3131. struct pt_core_data *cd = dev_get_drvdata(dev);
  3132. if (protect)
  3133. return pt_pip_set_param(cd, param_id, value, size);
  3134. return pt_pip_set_param_(cd, param_id, value, size);
  3135. }
  3136. /*******************************************************************************
  3137. * FUNCTION: _pt_pip_enter_easywake_state_
  3138. *
  3139. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3140. *
  3141. * RETURN:
  3142. * 0 = success
  3143. * !0 = failure
  3144. *
  3145. * PARAMETERS:
  3146. * *dev - pointer to device structure
  3147. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3148. * *return_data - return status if easywake was entered
  3149. ******************************************************************************/
  3150. static int pt_hid_output_enter_easywake_state_(
  3151. struct pt_core_data *cd, u8 data, u8 *return_data)
  3152. {
  3153. int write_length = 1;
  3154. u8 param[1] = { data };
  3155. int rc = 0;
  3156. struct pt_hid_output hid_output = {
  3157. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3158. .write_length = write_length,
  3159. .write_buf = param,
  3160. };
  3161. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3162. if (rc)
  3163. return rc;
  3164. *return_data = cd->response_buf[5];
  3165. return rc;
  3166. }
  3167. /*******************************************************************************
  3168. * FUNCTION: pt_pip_verify_config_block_crc_
  3169. *
  3170. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3171. *
  3172. * RETURN:
  3173. * 0 = success
  3174. * !0 = failure
  3175. *
  3176. * PARAMETERS:
  3177. * *cd - pointer the core data structure
  3178. * ebid - enumerated block ID
  3179. * *status - PIP command status
  3180. * calculated_crc - calculated CRC
  3181. * stored_crc - stored CRC in config area
  3182. ******************************************************************************/
  3183. static int pt_pip_verify_config_block_crc_(
  3184. struct pt_core_data *cd, u8 ebid, u8 *status,
  3185. u16 *calculated_crc, u16 *stored_crc)
  3186. {
  3187. int write_length = 1;
  3188. u8 param[1] = { ebid };
  3189. u8 *ptr;
  3190. int rc = 0;
  3191. struct pt_hid_output hid_output = {
  3192. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3193. .write_length = write_length,
  3194. .write_buf = param,
  3195. };
  3196. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3197. if (rc)
  3198. return rc;
  3199. ptr = &cd->response_buf[5];
  3200. *status = ptr[0];
  3201. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3202. *stored_crc = get_unaligned_le16(&ptr[3]);
  3203. return 0;
  3204. }
  3205. /*******************************************************************************
  3206. * FUNCTION: pt_pip_verify_config_block_crc
  3207. *
  3208. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3209. * an exclusive access to the DUT.
  3210. *
  3211. * RETURN:
  3212. * 0 = success
  3213. * !0 = failure
  3214. *
  3215. * PARAMETERS:
  3216. * *cd - pointer the core data structure
  3217. * ebid - enumerated block ID
  3218. * *status - PIP command status
  3219. * calculated_crc - calculated CRC
  3220. * stored_crc - stored CRC in config area
  3221. ******************************************************************************/
  3222. static int pt_pip_verify_config_block_crc(
  3223. struct pt_core_data *cd, u8 ebid, u8 *status,
  3224. u16 *calculated_crc, u16 *stored_crc)
  3225. {
  3226. int rc;
  3227. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3228. if (rc < 0) {
  3229. pt_debug(cd->dev, DL_ERROR,
  3230. "%s: fail get exclusive ex=%p own=%p\n",
  3231. __func__, cd->exclusive_dev, cd->dev);
  3232. return rc;
  3233. }
  3234. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3235. calculated_crc, stored_crc);
  3236. if (release_exclusive(cd, cd->dev) < 0)
  3237. pt_debug(cd->dev, DL_ERROR,
  3238. "%s: fail to release exclusive\n", __func__);
  3239. return rc;
  3240. }
  3241. /*******************************************************************************
  3242. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3243. *
  3244. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3245. * calls to the protected or unprotected call to
  3246. * pt_pip_verify_config_block_crc_
  3247. *
  3248. * RETURN:
  3249. * 0 = success
  3250. * !0 = failure
  3251. *
  3252. * PARAMETERS:
  3253. * *dev - pointer to device structure
  3254. * protect - flag to call protected or non-protected
  3255. * ebid - enumerated block ID
  3256. * *status - PIP command status
  3257. * calculated_crc - calculated CRC
  3258. * stored_crc - stored CRC in config area
  3259. ******************************************************************************/
  3260. static int _pt_request_pip_verify_config_block_crc(
  3261. struct device *dev, int protect, u8 ebid, u8 *status,
  3262. u16 *calculated_crc, u16 *stored_crc)
  3263. {
  3264. struct pt_core_data *cd = dev_get_drvdata(dev);
  3265. if (protect)
  3266. return pt_pip_verify_config_block_crc(cd, ebid,
  3267. status, calculated_crc, stored_crc);
  3268. return pt_pip_verify_config_block_crc_(cd, ebid,
  3269. status, calculated_crc, stored_crc);
  3270. }
  3271. /*******************************************************************************
  3272. * FUNCTION: pt_pip_get_config_row_size_
  3273. *
  3274. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3275. *
  3276. * RETURN:
  3277. * 0 = success
  3278. * !0 = failure
  3279. *
  3280. * PARAMETERS:
  3281. * *cd - pointer to core data
  3282. * protect - flag to call protected or non-protected
  3283. * *row_size - pointer to store the retrieved row size
  3284. ******************************************************************************/
  3285. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3286. u16 *row_size)
  3287. {
  3288. int rc = 0;
  3289. struct pt_hid_output hid_output = {
  3290. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3291. };
  3292. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3293. if (rc)
  3294. return rc;
  3295. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3296. return 0;
  3297. }
  3298. /*******************************************************************************
  3299. * FUNCTION: pt_pip_get_config_row_size
  3300. *
  3301. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3302. * an exclusive access to the DUT.
  3303. *
  3304. * RETURN:
  3305. * 0 = success
  3306. * !0 = failure
  3307. *
  3308. * PARAMETERS:
  3309. * *cd - pointer to core data
  3310. * protect - flag to call protected or non-protected
  3311. * *row_size - pointer to store the retrieved row size
  3312. ******************************************************************************/
  3313. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3314. u16 *row_size)
  3315. {
  3316. int rc;
  3317. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3318. if (rc < 0) {
  3319. pt_debug(cd->dev, DL_ERROR,
  3320. "%s: fail get exclusive ex=%p own=%p\n",
  3321. __func__, cd->exclusive_dev, cd->dev);
  3322. return rc;
  3323. }
  3324. rc = pt_pip_get_config_row_size_(cd, row_size);
  3325. if (release_exclusive(cd, cd->dev) < 0)
  3326. pt_debug(cd->dev, DL_ERROR,
  3327. "%s: fail to release exclusive\n", __func__);
  3328. return rc;
  3329. }
  3330. /*******************************************************************************
  3331. * FUNCTION: _pt_request_pip_get_config_row_size
  3332. *
  3333. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3334. * calls to the protected or unprotected call to
  3335. * pt_pip_get_config_row_size_
  3336. *
  3337. * RETURN:
  3338. * 0 = success
  3339. * !0 = failure
  3340. *
  3341. * PARAMETERS:
  3342. * *dev - pointer to device structure
  3343. * protect - flag to call protected or non-protected
  3344. * *row_size - pointer to store the retrieved row size
  3345. ******************************************************************************/
  3346. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3347. int protect, u16 *row_size)
  3348. {
  3349. struct pt_core_data *cd = dev_get_drvdata(dev);
  3350. if (protect)
  3351. return pt_pip_get_config_row_size(cd, row_size);
  3352. return pt_pip_get_config_row_size_(cd, row_size);
  3353. }
  3354. /*******************************************************************************
  3355. * FUNCTION: pt_pip1_read_data_block_
  3356. *
  3357. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3358. * output data to the "read_buf" and update "crc".
  3359. *
  3360. * RETURN:
  3361. * 0 = success
  3362. * !0 = failure
  3363. *
  3364. * PARAMETERS:
  3365. * *cd - pointer to core data
  3366. * row_number - row number
  3367. * length - length of data to read
  3368. * ebid - block id
  3369. * *actual_read_len - Actual data length read
  3370. * *read_buf - pointer to the buffer to store read data
  3371. * read_buf_size - size of read_buf
  3372. * *crc - pointer to store CRC of row data
  3373. ******************************************************************************/
  3374. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3375. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3376. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3377. {
  3378. int read_ebid;
  3379. int status;
  3380. int rc = 0;
  3381. int write_length = 5;
  3382. u8 write_buf[5];
  3383. u8 cmd_offset = 0;
  3384. u16 calc_crc;
  3385. struct pt_hid_output hid_output = {
  3386. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3387. .write_length = write_length,
  3388. .write_buf = write_buf,
  3389. };
  3390. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3391. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3392. write_buf[cmd_offset++] = LOW_BYTE(length);
  3393. write_buf[cmd_offset++] = HI_BYTE(length);
  3394. write_buf[cmd_offset++] = ebid;
  3395. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3396. if (rc)
  3397. return rc;
  3398. status = cd->response_buf[5];
  3399. if (status)
  3400. return status;
  3401. read_ebid = cd->response_buf[6];
  3402. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3403. return -EPROTO;
  3404. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3405. if (length == 0 || *actual_read_len == 0)
  3406. return 0;
  3407. if (read_buf_size >= *actual_read_len &&
  3408. *actual_read_len < PT_MAX_PIP2_MSG_SIZE)
  3409. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3410. else
  3411. return -EPROTO;
  3412. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3413. /* Validate Row Data CRC */
  3414. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3415. if (*crc == calc_crc) {
  3416. return 0;
  3417. } else {
  3418. pt_debug(cd->dev, DL_ERROR,
  3419. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3420. __func__, *crc, calc_crc);
  3421. return -EPROTO;
  3422. }
  3423. }
  3424. /*******************************************************************************
  3425. * FUNCTION: _pt_request_pip_read_data_block
  3426. *
  3427. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3428. * calls to pt_pip1_read_data_block_
  3429. *
  3430. * RETURN:
  3431. * 0 = success
  3432. * !0 = failure
  3433. *
  3434. * PARAMETERS:
  3435. * *dev - pointer to device structure
  3436. * row_number - row number
  3437. * length - length of data to read
  3438. * ebid - block id
  3439. * *actual_read_len - Actual data length read
  3440. * *read_buf - pointer to the buffer to store read data
  3441. * *crc - pointer to store CRC of row data
  3442. ******************************************************************************/
  3443. static int _pt_request_pip_read_data_block(struct device *dev,
  3444. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3445. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3446. {
  3447. struct pt_core_data *cd = dev_get_drvdata(dev);
  3448. return pt_pip1_read_data_block_(cd, row_number, length,
  3449. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3450. }
  3451. /*******************************************************************************
  3452. * FUNCTION: pt_pip1_write_data_block_
  3453. *
  3454. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3455. * write data to the data block.
  3456. *
  3457. * RETURN:
  3458. * 0 = success
  3459. * !0 = failure
  3460. *
  3461. * PARAMETERS:
  3462. * *cd - pointer to core data
  3463. * row_number - row in config block to write to
  3464. * write_length - length of data to write
  3465. * ebid - enumerated block ID
  3466. * *write_buf - pointer to buffer to write
  3467. * *security_key - pointer to security key to allow write
  3468. * *actual_write_len - pointer to store data length actually written
  3469. ******************************************************************************/
  3470. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3471. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3472. u8 *security_key, u16 *actual_write_len)
  3473. {
  3474. /* row_number + write_len + ebid + security_key + crc */
  3475. u16 full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3476. u8 *full_write_buf;
  3477. u8 cmd_offset = 0;
  3478. u16 crc;
  3479. int status;
  3480. int rc = 0;
  3481. int read_ebid;
  3482. u8 *data;
  3483. struct pt_hid_output hid_output = {
  3484. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3485. .write_length = full_write_length,
  3486. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3487. };
  3488. if (write_length > PT_CAL_DATA_ROW_SIZE)
  3489. return -EINVAL;
  3490. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3491. if (!full_write_buf)
  3492. return -ENOMEM;
  3493. hid_output.write_buf = full_write_buf;
  3494. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3495. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3496. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3497. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3498. full_write_buf[cmd_offset++] = ebid;
  3499. data = &full_write_buf[cmd_offset];
  3500. memcpy(data, write_buf, write_length);
  3501. cmd_offset += write_length;
  3502. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3503. cmd_offset += 8;
  3504. crc = _pt_compute_crc(data, write_length);
  3505. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3506. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3507. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3508. if (rc)
  3509. goto exit;
  3510. status = cd->response_buf[5];
  3511. if (status) {
  3512. rc = -EINVAL;
  3513. goto exit;
  3514. }
  3515. read_ebid = cd->response_buf[6];
  3516. if (read_ebid != ebid) {
  3517. rc = -EPROTO;
  3518. goto exit;
  3519. }
  3520. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3521. exit:
  3522. kfree(full_write_buf);
  3523. return rc;
  3524. }
  3525. /*******************************************************************************
  3526. * FUNCTION: _pt_request_pip_write_data_block
  3527. *
  3528. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3529. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3530. *
  3531. * RETURN:
  3532. * 0 = success
  3533. * !0 = failure
  3534. *
  3535. * PARAMETERS:
  3536. * *dev - pointer to device structure
  3537. * protect - flag to call protected or non-protected
  3538. * row_number - row in config block to write to
  3539. * write_length - length of data to write
  3540. * ebid - enumerated block ID
  3541. * *write_buf - pointer to buffer to write
  3542. * *security_key - pointer to security key to allow write
  3543. * *actual_write_len - pointer to store data length actually written
  3544. ******************************************************************************/
  3545. static int _pt_request_pip_write_data_block(struct device *dev,
  3546. u16 row_number, u16 write_length, u8 ebid,
  3547. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3548. {
  3549. struct pt_core_data *cd = dev_get_drvdata(dev);
  3550. return pt_pip1_write_data_block_(cd, row_number,
  3551. write_length, ebid, write_buf, security_key,
  3552. actual_write_len);
  3553. }
  3554. /*******************************************************************************
  3555. * FUNCTION: pt_pip_get_data_structure_
  3556. *
  3557. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3558. * returning a structure of data defined by data_id
  3559. *
  3560. * RETURN:
  3561. * 0 = success
  3562. * !0 = failure
  3563. *
  3564. * PARAMETERS:
  3565. * *cd - pointer to core data
  3566. * read_offset - read pointer offset
  3567. * read_length - length of data to read
  3568. * data_id - data ID to read
  3569. * *status - pointer to store the read response status
  3570. * *data_format - pointer to store format of data read
  3571. * *actual_read_len - pointer to store data length actually read
  3572. * *data - pointer to store data read
  3573. ******************************************************************************/
  3574. static int pt_pip_get_data_structure_(
  3575. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3576. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3577. u8 *data)
  3578. {
  3579. int rc = 0;
  3580. u16 total_read_len = 0;
  3581. u16 read_len;
  3582. u16 off_buf = 0;
  3583. u8 write_buf[5];
  3584. u8 read_data_id;
  3585. struct pt_hid_output hid_output = {
  3586. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3587. .write_length = 5,
  3588. .write_buf = write_buf,
  3589. };
  3590. again:
  3591. write_buf[0] = LOW_BYTE(read_offset);
  3592. write_buf[1] = HI_BYTE(read_offset);
  3593. write_buf[2] = LOW_BYTE(read_length);
  3594. write_buf[3] = HI_BYTE(read_length);
  3595. write_buf[4] = data_id;
  3596. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3597. if (rc)
  3598. return rc;
  3599. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3600. goto set_status;
  3601. read_data_id = cd->response_buf[6];
  3602. if (read_data_id != data_id)
  3603. return -EPROTO;
  3604. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3605. if (read_len && data) {
  3606. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3607. total_read_len += read_len;
  3608. if (read_len < read_length) {
  3609. read_offset += read_len;
  3610. off_buf += read_len;
  3611. read_length -= read_len;
  3612. goto again;
  3613. }
  3614. }
  3615. if (data_format)
  3616. *data_format = cd->response_buf[9];
  3617. if (actual_read_len)
  3618. *actual_read_len = total_read_len;
  3619. set_status:
  3620. if (status)
  3621. *status = cd->response_buf[5];
  3622. return rc;
  3623. }
  3624. /*******************************************************************************
  3625. * FUNCTION: pt_pip_get_data_structure
  3626. *
  3627. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3628. * an exclusive access to the DUT.
  3629. *
  3630. * RETURN:
  3631. * 0 = success
  3632. * !0 = failure
  3633. *
  3634. * PARAMETERS:
  3635. * *cd - pointer to core data
  3636. * read_offset - read pointer offset
  3637. * read_length - length of data to read
  3638. * data_id - data ID to read
  3639. * *status - pointer to store the read response status
  3640. * *data_format - pointer to store format of data read
  3641. * *actual_read_len - pointer to store data length actually read
  3642. * *data - pointer to store data read
  3643. ******************************************************************************/
  3644. static int pt_pip_get_data_structure(
  3645. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3646. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3647. u8 *data)
  3648. {
  3649. int rc;
  3650. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3651. if (rc < 0) {
  3652. pt_debug(cd->dev, DL_ERROR,
  3653. "%s: fail get exclusive ex=%p own=%p\n",
  3654. __func__, cd->exclusive_dev, cd->dev);
  3655. return rc;
  3656. }
  3657. rc = pt_pip_get_data_structure_(cd, read_offset,
  3658. read_length, data_id, status, data_format,
  3659. actual_read_len, data);
  3660. if (release_exclusive(cd, cd->dev) < 0)
  3661. pt_debug(cd->dev, DL_ERROR,
  3662. "%s: fail to release exclusive\n", __func__);
  3663. return rc;
  3664. }
  3665. /*******************************************************************************
  3666. * FUNCTION: _pt_request_pip_get_data_structure
  3667. *
  3668. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3669. * calls to the protected or unprotected call to
  3670. * pt_pip_get_data_structure
  3671. *
  3672. * RETURN:
  3673. * 0 = success
  3674. * !0 = failure
  3675. *
  3676. * PARAMETERS:
  3677. * *dev - pointer to device structure
  3678. * protect - flag to call protected or non-protected
  3679. * read_offset - read pointer offset
  3680. * read_length - length of data to read
  3681. * data_id - data ID to read
  3682. * *status - pointer to store the read response status
  3683. * *data_format - pointer to store format of data read
  3684. * *actual_read_len - pointer to store data length actually read
  3685. * *data - pointer to store data read
  3686. ******************************************************************************/
  3687. static int _pt_request_pip_get_data_structure(struct device *dev,
  3688. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3689. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3690. {
  3691. struct pt_core_data *cd = dev_get_drvdata(dev);
  3692. if (protect)
  3693. return pt_pip_get_data_structure(cd,
  3694. read_offset, read_length, data_id, status,
  3695. data_format, actual_read_len, data);
  3696. return pt_pip_get_data_structure_(cd,
  3697. read_offset, read_length, data_id, status,
  3698. data_format, actual_read_len, data);
  3699. }
  3700. /*******************************************************************************
  3701. * FUNCTION: _pt_manage_local_cal_data
  3702. *
  3703. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3704. * CALIBRATION data. It stores it in a local static array and can be
  3705. * cleared, loaded or used to restore the CAL data back to the running FW.
  3706. * The CAL data is read or restored by use of the PIP1 commands:
  3707. * - READ_DATA_BLOCK (0x22)
  3708. * - WRITE_DATA_BLOCK (0x23)
  3709. *
  3710. * RETURN:
  3711. * 0 = success
  3712. * !0 = failure
  3713. *
  3714. * PARAMETERS:
  3715. * *dev - pointer to device structure
  3716. * action - One of the following actions:
  3717. * - PT_CAL_DATA_SAVE
  3718. * - PT_CAL_DATA_RESTORE
  3719. * - PT_CAL_DATA_CLEAR
  3720. * - PT_CAL_DATA_SIZE
  3721. * *size - pointer to the number of bytes transferred
  3722. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3723. ******************************************************************************/
  3724. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3725. unsigned short *crc)
  3726. {
  3727. struct pt_core_data *cd = dev_get_drvdata(dev);
  3728. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3729. unsigned short calc_id_crc = 0;
  3730. static u8 *cal_cache_data;
  3731. static u16 cal_cache_len;
  3732. static unsigned short cal_cache_chip_id;
  3733. int rc = 0;
  3734. u8 *tmp_data = NULL;
  3735. u8 row_number = 0;
  3736. u8 prefix[20];
  3737. u16 cal_size = 0;
  3738. u16 transfer_size;
  3739. u16 act_trans_len = 0;
  3740. u16 byte_offset = 0;
  3741. u16 cal_blk_size;
  3742. u16 total_rows;
  3743. u16 remain_bytes;
  3744. u16 data_block_crc;
  3745. u16 buf_size = 12;
  3746. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3747. __func__, action);
  3748. switch (action) {
  3749. case PT_CAL_DATA_SAVE:
  3750. /* Read the size of the CAL block and calculate # rows */
  3751. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3752. if (!tmp_data) {
  3753. rc = -ENOMEM;
  3754. goto exit;
  3755. }
  3756. /*
  3757. * Don't check rc as doing a read size will give a false
  3758. * error on the CRC check.
  3759. */
  3760. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3761. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3762. cal_blk_size = act_trans_len;
  3763. kfree(tmp_data);
  3764. pt_debug(dev, DL_INFO,
  3765. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3766. __func__, cal_cache_len, cal_blk_size);
  3767. /* Safety net to ensure we didn't read incorrect size */
  3768. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3769. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3770. __func__);
  3771. rc = 1;
  3772. goto exit;
  3773. }
  3774. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3775. if (cal_blk_size != cal_cache_len) {
  3776. kfree(cal_cache_data);
  3777. cal_cache_data = kzalloc(cal_blk_size + 2,
  3778. GFP_KERNEL);
  3779. if (!cal_cache_data) {
  3780. rc = -ENOMEM;
  3781. goto exit;
  3782. }
  3783. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3784. __func__);
  3785. }
  3786. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3787. /* Calculate how many rows [0-n] (PIP Transactions) */
  3788. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3789. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3790. /* Add row if we have a last partial row */
  3791. if (remain_bytes > 0)
  3792. total_rows++;
  3793. pt_debug(dev, DL_INFO,
  3794. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3795. __func__, cal_blk_size, total_rows, remain_bytes);
  3796. /* Read all rows unless an error occurs */
  3797. rc = 0;
  3798. while (rc == 0 && row_number <= total_rows) {
  3799. act_trans_len = 0;
  3800. if (remain_bytes > 0 && row_number == total_rows)
  3801. transfer_size = remain_bytes;
  3802. else
  3803. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3804. rc = pt_pip1_read_data_block_(cd, row_number,
  3805. transfer_size, PT_CAL_EBID,
  3806. &act_trans_len,
  3807. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3808. &data_block_crc);
  3809. if (rc) {
  3810. /* Error occurred, exit loop */
  3811. cal_size = 0;
  3812. break;
  3813. }
  3814. pt_debug(dev, DL_INFO,
  3815. "%s: CAL read rc=%d actual read len=%d\n",
  3816. __func__, rc, act_trans_len);
  3817. byte_offset += act_trans_len;
  3818. cal_size = byte_offset;
  3819. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3820. pt_pr_buf(dev, DL_INFO,
  3821. &cal_cache_data[byte_offset - act_trans_len],
  3822. act_trans_len, prefix);
  3823. row_number++;
  3824. }
  3825. if (cal_size > 0) {
  3826. /* Save a CRC of the chip info the CAL was saved from */
  3827. calc_id_crc = crc_ccitt_calculate(
  3828. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3829. cal_cache_chip_id = calc_id_crc;
  3830. cal_cache_len = cal_size;
  3831. pt_debug(dev, DL_INFO,
  3832. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3833. __func__, calc_id_crc, cal_size);
  3834. }
  3835. *size = cal_size;
  3836. *crc = calc_id_crc;
  3837. break;
  3838. case PT_CAL_DATA_RESTORE:
  3839. cal_size = cal_cache_len;
  3840. while ((rc == 0) && (byte_offset < cal_size)) {
  3841. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3842. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3843. else
  3844. transfer_size = cal_size - byte_offset;
  3845. rc = pt_pip1_write_data_block_(cd, row_number,
  3846. transfer_size, PT_CAL_EBID,
  3847. &cal_cache_data[byte_offset],
  3848. (u8 *)pt_data_block_security_key,
  3849. &act_trans_len);
  3850. byte_offset += act_trans_len;
  3851. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3852. __func__, byte_offset);
  3853. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3854. pt_pr_buf(dev, DL_INFO,
  3855. &cal_cache_data[byte_offset - act_trans_len],
  3856. act_trans_len, prefix);
  3857. if ((byte_offset > cal_size) ||
  3858. (act_trans_len != transfer_size))
  3859. rc = -EIO;
  3860. row_number++;
  3861. }
  3862. *size = byte_offset;
  3863. *crc = cal_cache_chip_id;
  3864. break;
  3865. case PT_CAL_DATA_CLEAR:
  3866. if (cal_cache_data)
  3867. memset(&cal_cache_data[0], 0, cal_cache_len);
  3868. cal_cache_len = 0;
  3869. cal_cache_chip_id = 0;
  3870. *size = 0;
  3871. *crc = 0;
  3872. break;
  3873. case PT_CAL_DATA_INFO:
  3874. default:
  3875. *size = cal_cache_len;
  3876. *crc = cal_cache_chip_id;
  3877. pt_debug(dev, DL_INFO,
  3878. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3879. __func__, cal_cache_chip_id,
  3880. cal_cache_len);
  3881. break;
  3882. }
  3883. exit:
  3884. pt_debug(dev, DL_INFO,
  3885. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3886. __func__, rc, *crc, *size);
  3887. return rc;
  3888. }
  3889. /*******************************************************************************
  3890. * FUNCTION: pt_pip_run_selftest_
  3891. *
  3892. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3893. * to execute a FW built in self test
  3894. *
  3895. * RETURN:
  3896. * 0 = success
  3897. * !0 = failure
  3898. *
  3899. * PARAMETERS:
  3900. * *cd - pointer to core data
  3901. * test_id - enumerated test ID to run
  3902. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3903. * *status - pointer to store the read response status
  3904. * *summary_results - pointer to store the results summary
  3905. * *results_available - pointer to store if results are available
  3906. *****************************************************************************/
  3907. static int pt_pip_run_selftest_(
  3908. struct pt_core_data *cd, u8 test_id,
  3909. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3910. u8 *results_available)
  3911. {
  3912. int rc = 0;
  3913. u8 write_buf[2];
  3914. struct pt_hid_output hid_output = {
  3915. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3916. .write_length = 2,
  3917. .write_buf = write_buf,
  3918. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3919. };
  3920. write_buf[0] = test_id;
  3921. write_buf[1] = write_idacs_to_flash;
  3922. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3923. hid_output.write_length = 1;
  3924. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3925. if (rc)
  3926. return rc;
  3927. if (status)
  3928. *status = cd->response_buf[5];
  3929. if (summary_result)
  3930. *summary_result = cd->response_buf[6];
  3931. /* results_available only available before PIP 1.03 */
  3932. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3933. if (results_available)
  3934. *results_available = cd->response_buf[7];
  3935. }
  3936. return rc;
  3937. }
  3938. /*******************************************************************************
  3939. * FUNCTION: pt_pip_run_selftest
  3940. *
  3941. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3942. * an exclusive access to the DUT.
  3943. *
  3944. * RETURN:
  3945. * 0 = success
  3946. * !0 = failure
  3947. *
  3948. * PARAMETERS:
  3949. * *cd - pointer to core data
  3950. * test_id - enumerated test ID to run
  3951. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3952. * *status - pointer to store the read response status
  3953. * *summary_results - pointer to store the results summary
  3954. * *results_available - pointer to store if results are available
  3955. ******************************************************************************/
  3956. static int pt_pip_run_selftest(
  3957. struct pt_core_data *cd, u8 test_id,
  3958. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3959. u8 *results_available)
  3960. {
  3961. int rc;
  3962. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3963. if (rc < 0) {
  3964. pt_debug(cd->dev, DL_ERROR,
  3965. "%s: fail get exclusive ex=%p own=%p\n",
  3966. __func__, cd->exclusive_dev, cd->dev);
  3967. return rc;
  3968. }
  3969. rc = pt_pip_run_selftest_(cd, test_id,
  3970. write_idacs_to_flash, status, summary_result,
  3971. results_available);
  3972. if (release_exclusive(cd, cd->dev) < 0)
  3973. pt_debug(cd->dev, DL_ERROR,
  3974. "%s: fail to release exclusive\n", __func__);
  3975. return rc;
  3976. }
  3977. /*******************************************************************************
  3978. * FUNCTION: _pt_request_pip_run_selftest
  3979. *
  3980. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3981. * calls to the protected or unprotected call to pt_pip_run_selftest
  3982. *
  3983. * RETURN:
  3984. * 0 = success
  3985. * !0 = failure
  3986. *
  3987. * PARAMETERS:
  3988. * *dev - pointer to device structure
  3989. * protect - flag to call protected or non-protected
  3990. * test_id - enumerated test ID to run
  3991. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3992. * *status - pointer to store the read response status
  3993. * *summary_results - pointer to store the results summary
  3994. * *results_available - pointer to store if results are available
  3995. ******************************************************************************/
  3996. static int _pt_request_pip_run_selftest(struct device *dev,
  3997. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3998. u8 *summary_result, u8 *results_available)
  3999. {
  4000. struct pt_core_data *cd = dev_get_drvdata(dev);
  4001. if (protect)
  4002. return pt_pip_run_selftest(cd, test_id,
  4003. write_idacs_to_flash, status, summary_result,
  4004. results_available);
  4005. return pt_pip_run_selftest_(cd, test_id,
  4006. write_idacs_to_flash, status, summary_result,
  4007. results_available);
  4008. }
  4009. /*******************************************************************************
  4010. * FUNCTION: _pt_pip_get_selftest_result_
  4011. *
  4012. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  4013. * to retrieve the self test results from the self test already executed
  4014. *
  4015. * RETURN:
  4016. * 0 = success
  4017. * !0 = failure
  4018. *
  4019. * PARAMETERS:
  4020. * *cd - pointer to core data
  4021. * read_offset - read pointer offset
  4022. * read_length - length of data to read
  4023. * test_id - enumerated test ID to read selftest results from
  4024. * *status - pointer to store the read response status
  4025. * *actual_read_len - pointer to store data length actually read
  4026. * *status - pointer to where the cmd response statas is stored
  4027. ******************************************************************************/
  4028. static int pt_pip_get_selftest_result_(
  4029. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4030. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4031. {
  4032. int rc = 0;
  4033. u16 total_read_len = 0;
  4034. u16 read_len;
  4035. u16 off_buf = 0;
  4036. u8 write_buf[5];
  4037. u8 read_test_id;
  4038. bool repeat;
  4039. struct pt_hid_output hid_output = {
  4040. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4041. .write_length = 5,
  4042. .write_buf = write_buf,
  4043. };
  4044. /*
  4045. * Do not repeat reading for Auto Shorts test
  4046. * when PIP version < 1.3
  4047. */
  4048. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4049. || test_id != PT_ST_ID_AUTOSHORTS;
  4050. again:
  4051. write_buf[0] = LOW_BYTE(read_offset);
  4052. write_buf[1] = HI_BYTE(read_offset);
  4053. write_buf[2] = LOW_BYTE(read_length);
  4054. write_buf[3] = HI_BYTE(read_length);
  4055. write_buf[4] = test_id;
  4056. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4057. if (rc)
  4058. return rc;
  4059. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4060. goto set_status;
  4061. read_test_id = cd->response_buf[6];
  4062. if (read_test_id != test_id)
  4063. return -EPROTO;
  4064. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4065. if (read_len && data) {
  4066. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4067. total_read_len += read_len;
  4068. if (repeat && read_len < read_length) {
  4069. read_offset += read_len;
  4070. off_buf += read_len;
  4071. read_length -= read_len;
  4072. goto again;
  4073. }
  4074. }
  4075. if (actual_read_len)
  4076. *actual_read_len = total_read_len;
  4077. set_status:
  4078. if (status)
  4079. *status = cd->response_buf[5];
  4080. return rc;
  4081. }
  4082. /*******************************************************************************
  4083. * FUNCTION: _pt_pip_get_selftest_result
  4084. *
  4085. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4086. * access to the DUT
  4087. *
  4088. * RETURN:
  4089. * 0 = success
  4090. * !0 = failure
  4091. *
  4092. * PARAMETERS:
  4093. * *cd - pointer to core data
  4094. * read_offset - read pointer offset
  4095. * read_length - length of data to read
  4096. * test_id - enumerated test ID to read selftest results from
  4097. * *status - pointer to store the read response status
  4098. * *actual_read_len - pointer to store data length actually read
  4099. * *status - pointer to where the cmd response statas is stored
  4100. ******************************************************************************/
  4101. static int pt_pip_get_selftest_result(
  4102. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4103. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4104. {
  4105. int rc;
  4106. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4107. if (rc < 0) {
  4108. pt_debug(cd->dev, DL_ERROR,
  4109. "%s: fail get exclusive ex=%p own=%p\n",
  4110. __func__, cd->exclusive_dev, cd->dev);
  4111. return rc;
  4112. }
  4113. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4114. read_length, test_id, status, actual_read_len, data);
  4115. if (release_exclusive(cd, cd->dev) < 0)
  4116. pt_debug(cd->dev, DL_ERROR,
  4117. "%s: fail to release exclusive\n", __func__);
  4118. return rc;
  4119. }
  4120. /*******************************************************************************
  4121. * FUNCTION: _pt_request_pip_get_selftest_result
  4122. *
  4123. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4124. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4125. *
  4126. * RETURN:
  4127. * 0 = success
  4128. * !0 = failure
  4129. *
  4130. * PARAMETERS:
  4131. * *dev - pointer to device structure
  4132. * protect - flag to call protected or non-protected
  4133. * read_offset - read pointer offset
  4134. * read_length - length of data to read
  4135. * test_id - enumerated test ID to read selftest results from
  4136. * *status - pointer to store the read response status
  4137. * *actual_read_len - pointer to store data length actually read
  4138. * *data - pointer to where the data read is stored
  4139. ******************************************************************************/
  4140. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4141. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4142. u8 *status, u16 *actual_read_len, u8 *data)
  4143. {
  4144. struct pt_core_data *cd = dev_get_drvdata(dev);
  4145. if (protect)
  4146. return pt_pip_get_selftest_result(cd, read_offset,
  4147. read_length, test_id, status, actual_read_len,
  4148. data);
  4149. return pt_pip_get_selftest_result_(cd, read_offset,
  4150. read_length, test_id, status, actual_read_len,
  4151. data);
  4152. }
  4153. /*******************************************************************************
  4154. * FUNCTION: _pt_pip_load_self_test_param
  4155. *
  4156. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4157. * to load parameters needed by a self test
  4158. *
  4159. * RETURN:
  4160. * 0 = success
  4161. * !0 = failure
  4162. *
  4163. * PARAMETERS:
  4164. * *cd - pointer to core data
  4165. * self_test_id - enumerated test ID for which the parmeters belong
  4166. * load_offset - mem offset to where to load parameters
  4167. * load_length - length of parameter data to load
  4168. * *parameters - pointer to list of parameter data
  4169. * *status - pointer to store the response status
  4170. * *ret_test_id - pointer to returned test id the parameters were stored
  4171. * *act_load_len - pointer to store the actual load length that was writen
  4172. ******************************************************************************/
  4173. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4174. u8 self_test_id, u16 load_offset, u16 load_length,
  4175. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4176. {
  4177. int rc = 0;
  4178. int i;
  4179. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4180. struct pt_hid_output hid_output = {
  4181. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4182. .write_length = 5 + load_length,
  4183. .write_buf = write_buf,
  4184. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4185. };
  4186. write_buf[0] = LOW_BYTE(load_offset);
  4187. write_buf[1] = HI_BYTE(load_offset);
  4188. write_buf[2] = LOW_BYTE(load_length);
  4189. write_buf[3] = HI_BYTE(load_length);
  4190. write_buf[4] = self_test_id;
  4191. for (i = 0; i < load_length; i++)
  4192. write_buf[i + 5] = parameters[i];
  4193. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4194. if (rc)
  4195. return rc;
  4196. if (status)
  4197. *status = cd->response_buf[5];
  4198. if (ret_test_id)
  4199. *ret_test_id = cd->response_buf[6];
  4200. if (act_load_len)
  4201. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4202. return rc;
  4203. }
  4204. /*******************************************************************************
  4205. * FUNCTION: pt_pip_load_self_test_param
  4206. *
  4207. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4208. * access to the DUT.
  4209. *
  4210. * RETURN:
  4211. * 0 = success
  4212. * !0 = failure
  4213. *
  4214. * PARAMETERS:
  4215. * *cd - pointer to core data
  4216. * self_test_id - enumerated test ID for which the parmeters belong
  4217. * load_offset - mem offset to where to load parameters
  4218. * load_length - length of parameter data to load
  4219. * *parameters - pointer to list of parameter data
  4220. * *status - pointer to store the response status
  4221. * *ret_test_id - pointer to returned test id the parameters were stored
  4222. * *act_load_len - pointer to store the actual load length that was writen
  4223. ******************************************************************************/
  4224. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4225. u8 self_test_id, u16 load_offset, u16 load_length,
  4226. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4227. {
  4228. int rc;
  4229. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4230. if (rc < 0) {
  4231. pt_debug(cd->dev, DL_ERROR,
  4232. "%s: fail get exclusive ex=%p own=%p\n",
  4233. __func__, cd->exclusive_dev, cd->dev);
  4234. return rc;
  4235. }
  4236. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4237. load_length, parameters, status, ret_test_id, act_load_len);
  4238. if (release_exclusive(cd, cd->dev) < 0)
  4239. pt_debug(cd->dev, DL_ERROR,
  4240. "%s: fail to release exclusive\n", __func__);
  4241. return rc;
  4242. }
  4243. /*******************************************************************************
  4244. * FUNCTION: _pt_request_pip_load_self_test_param
  4245. *
  4246. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4247. * calls to the protected or unprotected call to
  4248. * pt_pip_load_self_test_param
  4249. *
  4250. * RETURN:
  4251. * 0 = success
  4252. * !0 = failure
  4253. *
  4254. * PARAMETERS:
  4255. * *dev - pointer to device structure
  4256. * protect - flag to call protected or non-protected
  4257. * self_test_id - enumerated test ID for which the parmeters belong
  4258. * load_offset - mem offset to where to load parameters
  4259. * load_length - length of parameter data to load
  4260. * *parameters - pointer to list of parameter data
  4261. * *status - pointer to store the response status
  4262. * *ret_test_id - pointer to returned test id the parameters were stored
  4263. * *act_load_len - pointer to store the actual load length that was writen
  4264. ******************************************************************************/
  4265. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4266. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4267. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4268. {
  4269. struct pt_core_data *cd = dev_get_drvdata(dev);
  4270. if (protect)
  4271. return pt_pip_load_self_test_param(cd, self_test_id,
  4272. load_offset, load_length, parameters, status, ret_test_id,
  4273. act_load_len);
  4274. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4275. load_length, parameters, status, ret_test_id, act_load_len);
  4276. }
  4277. /*******************************************************************************
  4278. * FUNCTION: pt_pip_calibrate_ext_
  4279. *
  4280. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4281. * for the response
  4282. *
  4283. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4284. *
  4285. * RETURN:
  4286. * 0 = success
  4287. * !0 = failure
  4288. *
  4289. * PARAMETERS:
  4290. * *cd - pointer to core data
  4291. * *cal_data - pointer to extended calibration data structure
  4292. * *status - pointer to where the command response status is stored
  4293. ******************************************************************************/
  4294. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4295. struct pt_cal_ext_data *cal_data, u8 *status)
  4296. {
  4297. int rc = 0;
  4298. int write_length = 4;
  4299. u8 write_buf[4];
  4300. u16 size = 0;
  4301. unsigned short crc = 0;
  4302. struct pt_hid_output hid_output = {
  4303. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4304. .write_length = write_length,
  4305. .write_buf = write_buf,
  4306. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4307. };
  4308. if (cal_data == NULL)
  4309. return -EINVAL;
  4310. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4311. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4312. if (rc)
  4313. return rc;
  4314. if (status)
  4315. *status = cd->response_buf[5];
  4316. /*
  4317. * When doing a calibration on a flashless DUT, save CAL data in
  4318. * the TTDL cache on any successful calibration
  4319. */
  4320. if (cd->response_buf[5] == 0 && cd->cal_cache_in_host) {
  4321. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4322. __func__);
  4323. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4324. &size, &crc);
  4325. if (rc)
  4326. pt_debug(cd->dev, DL_ERROR,
  4327. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4328. else
  4329. pt_debug(cd->dev, DL_INFO,
  4330. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4331. __func__, crc, size);
  4332. }
  4333. return 0;
  4334. }
  4335. /*******************************************************************************
  4336. * FUNCTION: pt_pip_calibrate_ext
  4337. *
  4338. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4339. * DUT
  4340. *
  4341. * RETURN:
  4342. * 0 = success
  4343. * !0 = failure
  4344. *
  4345. * PARAMETERS:
  4346. * *cd - pointer to core data
  4347. * *cal_data - pointer to extended calibration data structure
  4348. * *status - pointer to where the command response status is stored
  4349. ******************************************************************************/
  4350. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4351. struct pt_cal_ext_data *cal_data, u8 *status)
  4352. {
  4353. int rc;
  4354. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4355. if (rc < 0) {
  4356. pt_debug(cd->dev, DL_ERROR,
  4357. "%s: fail get exclusive ex=%p own=%p\n",
  4358. __func__, cd->exclusive_dev, cd->dev);
  4359. return rc;
  4360. }
  4361. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4362. if (release_exclusive(cd, cd->dev) < 0)
  4363. pt_debug(cd->dev, DL_ERROR,
  4364. "%s: fail to release exclusive\n", __func__);
  4365. return rc;
  4366. }
  4367. /*******************************************************************************
  4368. * FUNCTION: _pt_request_pip_calibrate_ext
  4369. *
  4370. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4371. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4372. *
  4373. * RETURN:
  4374. * 0 = success
  4375. * !0 = failure
  4376. *
  4377. * PARAMETERS:
  4378. * *dev - pointer to device structure
  4379. * protect - flag to call protected or non-protected
  4380. * *cal_data - pointer to extended calibration data structure
  4381. * *status - pointer to where the command response status is stored
  4382. ******************************************************************************/
  4383. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4384. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4385. {
  4386. struct pt_core_data *cd = dev_get_drvdata(dev);
  4387. if (protect)
  4388. return pt_pip_calibrate_ext(cd, cal_data, status);
  4389. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4390. }
  4391. /*******************************************************************************
  4392. * FUNCTION: pt_pip_calibrate_idacs_
  4393. *
  4394. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4395. * for the response
  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 = 0;
  4410. int write_length = 1;
  4411. u8 write_buf[1];
  4412. u8 cmd_offset = 0;
  4413. struct pt_hid_output hid_output = {
  4414. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4415. .write_length = write_length,
  4416. .write_buf = write_buf,
  4417. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4418. };
  4419. write_buf[cmd_offset++] = mode;
  4420. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4421. if (rc)
  4422. return rc;
  4423. *status = cd->response_buf[5];
  4424. if (*status)
  4425. return -EINVAL;
  4426. return 0;
  4427. }
  4428. /*******************************************************************************
  4429. * FUNCTION: pt_pip_calibrate_idacs
  4430. *
  4431. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4432. * access to the DUT
  4433. *
  4434. * RETURN:
  4435. * 0 = success
  4436. * !0 = failure
  4437. *
  4438. * PARAMETERS:
  4439. * *cd - pointer to core data
  4440. * mode - sense mode to calibrate (0-5)
  4441. * *status - pointer to where the command response status is stored
  4442. ******************************************************************************/
  4443. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4444. u8 mode, u8 *status)
  4445. {
  4446. int rc;
  4447. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4448. if (rc < 0) {
  4449. pt_debug(cd->dev, DL_ERROR,
  4450. "%s: fail get exclusive ex=%p own=%p\n",
  4451. __func__, cd->exclusive_dev, cd->dev);
  4452. return rc;
  4453. }
  4454. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4455. if (release_exclusive(cd, cd->dev) < 0)
  4456. pt_debug(cd->dev, DL_ERROR,
  4457. "%s: fail to release exclusive\n", __func__);
  4458. return rc;
  4459. }
  4460. /*******************************************************************************
  4461. * FUNCTION: _pt_request_pip_calibrate_idacs
  4462. *
  4463. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4464. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4465. *
  4466. * RETURN:
  4467. * 0 = success
  4468. * !0 = failure
  4469. *
  4470. * PARAMETERS:
  4471. * *dev - pointer to device structure
  4472. * protect - flag to call protected or non-protected
  4473. * mode - sense mode to calibrate (0-5)
  4474. * *status - pointer to where the command response status is stored
  4475. ******************************************************************************/
  4476. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4477. int protect, u8 mode, u8 *status)
  4478. {
  4479. struct pt_core_data *cd = dev_get_drvdata(dev);
  4480. if (protect)
  4481. return pt_pip_calibrate_idacs(cd, mode, status);
  4482. return pt_pip_calibrate_idacs_(cd, mode, status);
  4483. }
  4484. /*******************************************************************************
  4485. * FUNCTION: pt_hid_output_initialize_baselines_
  4486. *
  4487. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4488. * waiting for the response.
  4489. *
  4490. * RETURN:
  4491. * 0 = success
  4492. * !0 = failure
  4493. *
  4494. * PARAMETERS:
  4495. * *cd - pointer to core data
  4496. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4497. * each or together with a single command.
  4498. * *status - pointer to where the command response status is stored
  4499. ******************************************************************************/
  4500. static int pt_hid_output_initialize_baselines_(
  4501. struct pt_core_data *cd, u8 test_id, u8 *status)
  4502. {
  4503. int rc = 0;
  4504. int write_length = 1;
  4505. u8 write_buf[1];
  4506. u8 cmd_offset = 0;
  4507. struct pt_hid_output hid_output = {
  4508. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4509. .write_length = write_length,
  4510. .write_buf = write_buf,
  4511. };
  4512. write_buf[cmd_offset++] = test_id;
  4513. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4514. if (rc)
  4515. return rc;
  4516. *status = cd->response_buf[5];
  4517. if (*status)
  4518. return -EINVAL;
  4519. return rc;
  4520. }
  4521. /*******************************************************************************
  4522. * FUNCTION: pt_hid_output_initialize_baselines
  4523. *
  4524. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4525. * access to the DUT
  4526. *
  4527. * RETURN:
  4528. * 0 = success
  4529. * !0 = failure
  4530. *
  4531. * PARAMETERS:
  4532. * *cd - pointer to core data
  4533. * test_id - enumerated ID against which to initialize the baseline
  4534. * *status - pointer to where the command response status is stored
  4535. ******************************************************************************/
  4536. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4537. u8 test_id, u8 *status)
  4538. {
  4539. int rc;
  4540. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4541. if (rc < 0) {
  4542. pt_debug(cd->dev, DL_ERROR,
  4543. "%s: fail get exclusive ex=%p own=%p\n",
  4544. __func__, cd->exclusive_dev, cd->dev);
  4545. return rc;
  4546. }
  4547. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4548. if (release_exclusive(cd, cd->dev) < 0)
  4549. pt_debug(cd->dev, DL_ERROR,
  4550. "%s: fail to release exclusive\n", __func__);
  4551. return rc;
  4552. }
  4553. /*******************************************************************************
  4554. * FUNCTION: _pt_request_pip_initialize_baselines
  4555. *
  4556. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4557. * calls to the protected or unprotected call to
  4558. * pt_pip_initialize_baselines
  4559. *
  4560. * RETURN:
  4561. * 0 = success
  4562. * !0 = failure
  4563. *
  4564. * PARAMETERS:
  4565. * *dev - pointer to device structure
  4566. * protect - flag to call protected or non-protected
  4567. * test_id - enumerated ID against which to initialize the baseline
  4568. * *status - pointer to where the command response status is stored
  4569. ******************************************************************************/
  4570. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4571. int protect, u8 test_id, u8 *status)
  4572. {
  4573. struct pt_core_data *cd = dev_get_drvdata(dev);
  4574. if (protect)
  4575. return pt_hid_output_initialize_baselines(cd, test_id,
  4576. status);
  4577. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4578. }
  4579. /*******************************************************************************
  4580. * FUNCTION: pt_hid_output_exec_panel_scan_
  4581. *
  4582. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4583. * the response
  4584. *
  4585. * RETURN:
  4586. * 0 = success
  4587. * !0 = failure
  4588. *
  4589. * PARAMETERS:
  4590. * *cd - pointer to core data
  4591. ******************************************************************************/
  4592. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4593. {
  4594. struct pt_hid_output hid_output = {
  4595. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4596. };
  4597. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4598. }
  4599. /*******************************************************************************
  4600. * FUNCTION: pt_hid_output_exec_panel_scan
  4601. *
  4602. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4603. * access to the DUT.
  4604. *
  4605. * RETURN:
  4606. * 0 = success
  4607. * !0 = failure
  4608. *
  4609. * PARAMETERS:
  4610. * *cd - pointer to core data
  4611. ******************************************************************************/
  4612. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4613. {
  4614. int rc;
  4615. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4616. if (rc < 0) {
  4617. pt_debug(cd->dev, DL_ERROR,
  4618. "%s: fail get exclusive ex=%p own=%p\n",
  4619. __func__, cd->exclusive_dev, cd->dev);
  4620. return rc;
  4621. }
  4622. rc = pt_hid_output_exec_panel_scan_(cd);
  4623. if (release_exclusive(cd, cd->dev) < 0)
  4624. pt_debug(cd->dev, DL_ERROR,
  4625. "%s: fail to release exclusive\n", __func__);
  4626. return rc;
  4627. }
  4628. /*******************************************************************************
  4629. * FUNCTION: pt_pip2_exec_panel_scan_
  4630. *
  4631. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4632. * the response
  4633. *
  4634. * RETURN:
  4635. * 0 = success
  4636. * !0 = failure
  4637. *
  4638. * PARAMETERS:
  4639. * *cd - pointer to core data
  4640. * scan_type - type of panel scan to perform (PIP2 only)
  4641. ******************************************************************************/
  4642. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4643. {
  4644. int rc = 0;
  4645. u8 data[2];
  4646. u8 read_buf[10];
  4647. u16 actual_read_len;
  4648. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4649. __func__, scan_type);
  4650. data[0] = scan_type;
  4651. rc = _pt_request_pip2_send_cmd(cd->dev,
  4652. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4653. data, 1, read_buf, &actual_read_len);
  4654. if (rc) {
  4655. pt_debug(cd->dev, DL_ERROR,
  4656. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4657. __func__, scan_type, rc);
  4658. }
  4659. return rc;
  4660. }
  4661. /*******************************************************************************
  4662. * FUNCTION: pt_pip2_exec_panel_scan
  4663. *
  4664. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4665. * access to the DUT.
  4666. *
  4667. * RETURN:
  4668. * 0 = success
  4669. * !0 = failure
  4670. *
  4671. * PARAMETERS:
  4672. * *cd - pointer to core data
  4673. * scan_type - type of panel scan to perform (PIP2 only)
  4674. ******************************************************************************/
  4675. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4676. {
  4677. int rc;
  4678. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4679. if (rc < 0) {
  4680. pt_debug(cd->dev, DL_ERROR,
  4681. "%s: fail get exclusive ex=%p own=%p\n",
  4682. __func__, cd->exclusive_dev, cd->dev);
  4683. return rc;
  4684. }
  4685. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4686. if (release_exclusive(cd, cd->dev) < 0)
  4687. pt_debug(cd->dev, DL_ERROR,
  4688. "%s: fail to release exclusive\n", __func__);
  4689. return rc;
  4690. }
  4691. /*******************************************************************************
  4692. * FUNCTION: _pt_request_pip_exec_panel_scan
  4693. *
  4694. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4695. * calls to the protected or unprotected call to
  4696. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4697. *
  4698. * RETURN:
  4699. * 0 = success
  4700. * !0 = failure
  4701. *
  4702. * PARAMETERS:
  4703. * *dev - pointer to device structure
  4704. * protect - flag to call protected or non-protected
  4705. * scan_type - type of panel scan to perform (PIP2 only)
  4706. ******************************************************************************/
  4707. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4708. int protect, u8 scan_type)
  4709. {
  4710. struct pt_core_data *cd = dev_get_drvdata(dev);
  4711. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4712. if (protect)
  4713. return pt_pip2_exec_panel_scan(cd, scan_type);
  4714. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4715. }
  4716. if (protect)
  4717. return pt_hid_output_exec_panel_scan(cd);
  4718. return pt_hid_output_exec_panel_scan_(cd);
  4719. }
  4720. /*******************************************************************************
  4721. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4722. *
  4723. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4724. * to retrieve the specified data type for a the last successful Execute
  4725. * Panel Scan command.
  4726. *
  4727. * RETURN:
  4728. * 0 = success
  4729. * !0 = failure
  4730. *
  4731. * PARAMETERS:
  4732. * *dev - pointer to device structure
  4733. * protect - flag to call protected or non-protected
  4734. * read_offset - read pointer offset
  4735. * read_count - length of data to read
  4736. * data_id - enumerated test ID to read selftest results from
  4737. * *response - pointer to store the read response status
  4738. * *config - pointer to store config data
  4739. * *actual_read_len - pointer to store data length actually read
  4740. * *read_buf - pointer to the read buffer
  4741. ******************************************************************************/
  4742. static int pt_hid_output_retrieve_panel_scan_(
  4743. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4744. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4745. u8 *read_buf)
  4746. {
  4747. int status;
  4748. u8 read_data_id;
  4749. int rc = 0;
  4750. int write_length = 5;
  4751. u8 write_buf[5];
  4752. u8 cmd_offset = 0;
  4753. u8 data_elem_size;
  4754. int size;
  4755. int data_size;
  4756. struct pt_hid_output hid_output = {
  4757. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4758. .write_length = write_length,
  4759. .write_buf = write_buf,
  4760. };
  4761. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4762. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4763. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4764. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4765. write_buf[cmd_offset++] = data_id;
  4766. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4767. if (rc)
  4768. return rc;
  4769. status = cd->response_buf[5];
  4770. if (status)
  4771. return -EINVAL;
  4772. read_data_id = cd->response_buf[6];
  4773. if (read_data_id != data_id)
  4774. return -EPROTO;
  4775. size = get_unaligned_le16(&cd->response_buf[0]);
  4776. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4777. *config = cd->response_buf[9];
  4778. data_elem_size = *config & 0x07;
  4779. data_size = *actual_read_len * data_elem_size;
  4780. if (read_buf)
  4781. memcpy(read_buf, &cd->response_buf[10], data_size);
  4782. if (response)
  4783. memcpy(response, cd->response_buf, size);
  4784. return rc;
  4785. }
  4786. /*******************************************************************************
  4787. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4788. *
  4789. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4790. * access to the DUT.
  4791. *
  4792. * RETURN:
  4793. * 0 = success
  4794. * !0 = failure
  4795. *
  4796. * PARAMETERS:
  4797. * *dev - pointer to device structure
  4798. * protect - flag to call protected or non-protected
  4799. * read_offset - read pointer offset
  4800. * read_count - length of data to read
  4801. * data_id - enumerated test ID to read selftest results from
  4802. * *response - pointer to store the read response status
  4803. * *config - pointer to store config data
  4804. * *actual_read_len - pointer to store data length actually read
  4805. * *read_buf - pointer to the read buffer
  4806. ******************************************************************************/
  4807. static int pt_hid_output_retrieve_panel_scan(
  4808. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4809. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4810. u8 *read_buf)
  4811. {
  4812. int rc;
  4813. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4814. if (rc < 0) {
  4815. pt_debug(cd->dev, DL_ERROR,
  4816. "%s: fail get exclusive ex=%p own=%p\n",
  4817. __func__, cd->exclusive_dev, cd->dev);
  4818. return rc;
  4819. }
  4820. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4821. read_count, data_id, response, config,
  4822. actual_read_len, read_buf);
  4823. if (release_exclusive(cd, cd->dev) < 0)
  4824. pt_debug(cd->dev, DL_ERROR,
  4825. "%s: fail to release exclusive\n", __func__);
  4826. return rc;
  4827. }
  4828. /*******************************************************************************
  4829. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4830. *
  4831. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4832. * calls to the protected or unprotected call to
  4833. * pt_hid_output_retrieve_panel_scan
  4834. *
  4835. * RETURN:
  4836. * 0 = success
  4837. * !0 = failure
  4838. *
  4839. * PARAMETERS:
  4840. * *dev - pointer to device structure
  4841. * protect - flag to call protected or non-protected
  4842. * read_offset - read pointer offset
  4843. * read_count - length of data to read
  4844. * data_id - enumerated test ID to read selftest results from
  4845. * *response - pointer to store the read response status
  4846. * *config - pointer to store config data
  4847. * *actual_read_len - pointer to store data length actually read
  4848. * *read_buf - pointer to the read buffer
  4849. ******************************************************************************/
  4850. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4851. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4852. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4853. {
  4854. struct pt_core_data *cd = dev_get_drvdata(dev);
  4855. if (protect)
  4856. return pt_hid_output_retrieve_panel_scan(cd,
  4857. read_offset, read_count, data_id, response,
  4858. config, actual_read_len, read_buf);
  4859. return pt_hid_output_retrieve_panel_scan_(cd,
  4860. read_offset, read_count, data_id, response,
  4861. config, actual_read_len, read_buf);
  4862. }
  4863. /*******************************************************************************
  4864. * FUNCTION: _pt_request_pip_user_cmd
  4865. *
  4866. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4867. * calls to the protected or unprotected call to
  4868. * pt_hid_output_user_cmd
  4869. *
  4870. * RETURN:
  4871. * 0 = success
  4872. * !0 = failure
  4873. *
  4874. * PARAMETERS:
  4875. * *dev - pointer to device structure
  4876. * protect - flag to call protected or non-protected
  4877. * read_len - length of data to read
  4878. * *read_buf - pointer to store read data
  4879. * write_len - length of data to write
  4880. * *write_buf - pointer to buffer to write
  4881. * *actual_read_len - pointer to store data length actually read
  4882. ******************************************************************************/
  4883. static int _pt_request_pip_user_cmd(struct device *dev,
  4884. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4885. u8 *write_buf, u16 *actual_read_len)
  4886. {
  4887. struct pt_core_data *cd = dev_get_drvdata(dev);
  4888. if (protect)
  4889. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4890. write_len, write_buf, actual_read_len);
  4891. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4892. write_len, write_buf, actual_read_len);
  4893. }
  4894. /*******************************************************************************
  4895. * FUNCTION: pt_hid_output_bl_get_information_
  4896. *
  4897. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4898. * DUT to retrieve bootloader version and chip identification information.
  4899. *
  4900. * RETURN:
  4901. * 0 = success
  4902. * !0 = failure
  4903. *
  4904. * PARAMETERS:
  4905. * *cd - pointer to core data
  4906. * *return_data - pointer to store the return data
  4907. *****************************************************************************/
  4908. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4909. u8 *return_data)
  4910. {
  4911. int rc;
  4912. int data_len;
  4913. struct pt_hid_output hid_output = {
  4914. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4915. };
  4916. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4917. if (rc)
  4918. return rc;
  4919. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4920. if (!data_len)
  4921. return -EPROTO;
  4922. memcpy(return_data, &cd->response_buf[8], data_len);
  4923. return 0;
  4924. }
  4925. /*******************************************************************************
  4926. * FUNCTION: pt_hid_output_bl_get_information
  4927. *
  4928. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4929. * access to the DUT.
  4930. *
  4931. * RETURN:
  4932. * 0 = success
  4933. * !0 = failure
  4934. *
  4935. * PARAMETERS:
  4936. * *cd - pointer to core data
  4937. * *return_data - pointer to store the return data
  4938. ******************************************************************************/
  4939. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4940. u8 *return_data)
  4941. {
  4942. int rc;
  4943. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4944. if (rc < 0) {
  4945. pt_debug(cd->dev, DL_ERROR,
  4946. "%s: fail get exclusive ex=%p own=%p\n",
  4947. __func__, cd->exclusive_dev, cd->dev);
  4948. return rc;
  4949. }
  4950. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4951. if (release_exclusive(cd, cd->dev) < 0)
  4952. pt_debug(cd->dev, DL_ERROR,
  4953. "%s: fail to release exclusive\n", __func__);
  4954. return rc;
  4955. }
  4956. /*******************************************************************************
  4957. * FUNCTION: _pt_request_pip_bl_get_information
  4958. *
  4959. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4960. * calls to the protected or unprotected call to
  4961. * pt_hid_output_bl_get_information
  4962. *
  4963. * RETURN:
  4964. * 0 = success
  4965. * !0 = failure
  4966. *
  4967. * PARAMETERS:
  4968. * *dev - pointer to device structure
  4969. * protect - flag to call protected or non-protected
  4970. * *return_data - pointer to store bl data
  4971. ******************************************************************************/
  4972. static int _pt_request_pip_bl_get_information(struct device *dev,
  4973. int protect, u8 *return_data)
  4974. {
  4975. struct pt_core_data *cd = dev_get_drvdata(dev);
  4976. if (protect)
  4977. return pt_hid_output_bl_get_information(cd, return_data);
  4978. return pt_hid_output_bl_get_information_(cd, return_data);
  4979. }
  4980. /*******************************************************************************
  4981. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4982. *
  4983. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4984. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4985. * and Design Data block in flash and enables the host to execute the Program
  4986. * and Verify Row command to bootload the application image and data.
  4987. *
  4988. * RETURN:
  4989. * 0 = success
  4990. * !0 = failure
  4991. *
  4992. * PARAMETERS:
  4993. * *cd - pointer to core data
  4994. * protect - flag to call protected or non-protected
  4995. * key_size - size of key
  4996. * *key_buf - pointer to key data to allow operation
  4997. * row_size - size of the meta data row
  4998. * *metadata_row_buf - pointer to meta data to write
  4999. ******************************************************************************/
  5000. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  5001. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5002. {
  5003. u16 write_length = key_size + row_size;
  5004. u8 *write_buf;
  5005. int rc = 0;
  5006. struct pt_hid_output hid_output = {
  5007. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  5008. .write_length = write_length,
  5009. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  5010. };
  5011. write_buf = kzalloc(write_length, GFP_KERNEL);
  5012. if (!write_buf)
  5013. return -ENOMEM;
  5014. hid_output.write_buf = write_buf;
  5015. if (key_size)
  5016. memcpy(write_buf, key_buf, key_size);
  5017. if (row_size)
  5018. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5019. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5020. kfree(write_buf);
  5021. return rc;
  5022. }
  5023. /*******************************************************************************
  5024. * FUNCTION: pt_hid_output_bl_initiate_bl
  5025. *
  5026. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5027. * access to the DUT.
  5028. *
  5029. * RETURN:
  5030. * 0 = success
  5031. * !0 = failure
  5032. *
  5033. * PARAMETERS:
  5034. * *cd - pointer to core data
  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_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5041. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5042. {
  5043. int rc;
  5044. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5045. if (rc < 0) {
  5046. pt_debug(cd->dev, DL_ERROR,
  5047. "%s: fail get exclusive ex=%p own=%p\n",
  5048. __func__, cd->exclusive_dev, cd->dev);
  5049. return rc;
  5050. }
  5051. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5052. row_size, metadata_row_buf);
  5053. if (release_exclusive(cd, cd->dev) < 0)
  5054. pt_debug(cd->dev, DL_ERROR,
  5055. "%s: fail to release exclusive\n", __func__);
  5056. return rc;
  5057. }
  5058. /*******************************************************************************
  5059. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5060. *
  5061. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5062. * calls to the protected or unprotected call to
  5063. * pt_hid_output_bl_initiate_bl
  5064. *
  5065. * RETURN:
  5066. * 0 = success
  5067. * !0 = failure
  5068. *
  5069. * PARAMETERS:
  5070. * *dev - pointer to device structure
  5071. * protect - flag to call protected or non-protected
  5072. * key_size - size of key
  5073. * *key_buf - pointer to key data to allow operation
  5074. * row_size - size of the meta data row
  5075. * *metadata_row_buf - pointer to meta data to write
  5076. ******************************************************************************/
  5077. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5078. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5079. u8 *metadata_row_buf)
  5080. {
  5081. struct pt_core_data *cd = dev_get_drvdata(dev);
  5082. if (protect)
  5083. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5084. row_size, metadata_row_buf);
  5085. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5086. row_size, metadata_row_buf);
  5087. }
  5088. /*******************************************************************************
  5089. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5090. *
  5091. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5092. * and program a 128-byte row into the flash, and then verifies written data.
  5093. *
  5094. * RETURN:
  5095. * 0 = success
  5096. * !0 = failure
  5097. *
  5098. * PARAMETERS:
  5099. * *cd - pointer to core data
  5100. * data_len - length of data_buf
  5101. * *data_buf - firmware image to program
  5102. ******************************************************************************/
  5103. static int pt_hid_output_bl_program_and_verify_(
  5104. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5105. {
  5106. struct pt_hid_output hid_output = {
  5107. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5108. .write_length = data_len,
  5109. .write_buf = data_buf,
  5110. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5111. };
  5112. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5113. }
  5114. /*******************************************************************************
  5115. * FUNCTION: pt_hid_output_bl_program_and_verify
  5116. *
  5117. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5118. * access to the DUT.
  5119. *
  5120. * RETURN:
  5121. * 0 = success
  5122. * !0 = failure
  5123. *
  5124. * PARAMETERS:
  5125. * *cd - pointer to core data
  5126. * data_len - length of data_buf
  5127. * *data_buf - firmware image to program
  5128. ******************************************************************************/
  5129. static int pt_hid_output_bl_program_and_verify(
  5130. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5131. {
  5132. int rc;
  5133. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5134. if (rc < 0) {
  5135. pt_debug(cd->dev, DL_ERROR,
  5136. "%s: fail get exclusive ex=%p own=%p\n",
  5137. __func__, cd->exclusive_dev, cd->dev);
  5138. return rc;
  5139. }
  5140. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5141. if (release_exclusive(cd, cd->dev) < 0)
  5142. pt_debug(cd->dev, DL_ERROR,
  5143. "%s: fail to release exclusive\n", __func__);
  5144. return rc;
  5145. }
  5146. /*******************************************************************************
  5147. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5148. *
  5149. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5150. * to request to have the BL program and verify a FW image
  5151. *
  5152. * RETURN:
  5153. * 0 = success
  5154. * !0 = failure
  5155. *
  5156. * PARAMETERS:
  5157. * *dev - pointer to device structure
  5158. * protect - boolean to determine to call the protected function
  5159. * data_len - length of data_buf
  5160. * *data_buf - firmware image to program
  5161. ******************************************************************************/
  5162. static int _pt_request_pip_bl_program_and_verify(
  5163. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5164. {
  5165. struct pt_core_data *cd = dev_get_drvdata(dev);
  5166. if (protect)
  5167. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5168. data_buf);
  5169. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5170. data_buf);
  5171. }
  5172. /*******************************************************************************
  5173. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5174. *
  5175. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5176. * perform a full verification of the application integrity by calculating the
  5177. * CRC of the image in flash and compare it to the expected CRC stored in the
  5178. * Metadata row.
  5179. *
  5180. * RETURN:
  5181. * 0 = success
  5182. * !0 = failure
  5183. *
  5184. * PARAMETERS:
  5185. * *cd - pointer to core data
  5186. * *result - pointer to store result
  5187. ******************************************************************************/
  5188. static int pt_hid_output_bl_verify_app_integrity_(
  5189. struct pt_core_data *cd, u8 *result)
  5190. {
  5191. int rc;
  5192. struct pt_hid_output hid_output = {
  5193. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5194. };
  5195. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5196. if (rc) {
  5197. *result = 0;
  5198. return rc;
  5199. }
  5200. *result = cd->response_buf[8];
  5201. return 0;
  5202. }
  5203. /*******************************************************************************
  5204. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5205. *
  5206. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5207. * exclusive access to the DUT.
  5208. *
  5209. * RETURN:
  5210. * 0 = success
  5211. * !0 = failure
  5212. *
  5213. * PARAMETERS:
  5214. * *cd - pointer to core data
  5215. * *result - pointer to store result
  5216. ******************************************************************************/
  5217. static int pt_hid_output_bl_verify_app_integrity(
  5218. struct pt_core_data *cd, u8 *result)
  5219. {
  5220. int rc;
  5221. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5222. if (rc < 0) {
  5223. pt_debug(cd->dev, DL_ERROR,
  5224. "%s: fail get exclusive ex=%p own=%p\n",
  5225. __func__, cd->exclusive_dev, cd->dev);
  5226. return rc;
  5227. }
  5228. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5229. if (release_exclusive(cd, cd->dev) < 0)
  5230. pt_debug(cd->dev, DL_ERROR,
  5231. "%s: fail to release exclusive\n", __func__);
  5232. return rc;
  5233. }
  5234. /*******************************************************************************
  5235. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5236. *
  5237. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5238. * to request to have the BL verify the application integrity (PIP1.x only)
  5239. *
  5240. * RETURN:
  5241. * 0 = success
  5242. * !0 = failure
  5243. *
  5244. * PARAMETERS:
  5245. * *dev - pointer to device structure
  5246. * protect - boolean to determine to call the protected function
  5247. * *result - pointer to store result
  5248. ******************************************************************************/
  5249. static int _pt_request_pip_bl_verify_app_integrity(
  5250. struct device *dev, int protect, u8 *result)
  5251. {
  5252. struct pt_core_data *cd = dev_get_drvdata(dev);
  5253. if (protect)
  5254. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5255. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5256. }
  5257. /*******************************************************************************
  5258. * FUNCTION: pt_hid_output_bl_launch_app_
  5259. *
  5260. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5261. * application from bootloader (PIP1.x only).
  5262. *
  5263. * RETURN:
  5264. * 0 = success
  5265. * !0 = failure
  5266. *
  5267. * PARAMETERS:
  5268. * *cd - pointer to core data
  5269. ******************************************************************************/
  5270. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5271. {
  5272. struct pt_hid_output hid_output = {
  5273. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5274. .reset_expected = 1,
  5275. };
  5276. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5277. }
  5278. /*******************************************************************************
  5279. * FUNCTION: pt_hid_output_bl_launch_app
  5280. *
  5281. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5282. * to the DUT.
  5283. *
  5284. * RETURN:
  5285. * 0 = success
  5286. * !0 = failure
  5287. *
  5288. * PARAMETERS:
  5289. * *cd - pointer to core data
  5290. ******************************************************************************/
  5291. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5292. {
  5293. int rc;
  5294. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5295. if (rc < 0) {
  5296. pt_debug(cd->dev, DL_ERROR,
  5297. "%s: fail get exclusive ex=%p own=%p\n",
  5298. __func__, cd->exclusive_dev, cd->dev);
  5299. return rc;
  5300. }
  5301. rc = pt_hid_output_bl_launch_app_(cd);
  5302. if (release_exclusive(cd, cd->dev) < 0)
  5303. pt_debug(cd->dev, DL_ERROR,
  5304. "%s: fail to release exclusive\n", __func__);
  5305. return rc;
  5306. }
  5307. /*******************************************************************************
  5308. * FUNCTION: _pt_request_pip_launch_app
  5309. *
  5310. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5311. * to request to have the BL launch the application. (PIP1.x only)
  5312. *
  5313. * RETURN:
  5314. * 0 = success
  5315. * !0 = failure
  5316. *
  5317. * PARAMETERS:
  5318. * *dev - pointer to device structure
  5319. * protect - boolean to determine to call the protected function
  5320. ******************************************************************************/
  5321. static int _pt_request_pip_launch_app(struct device *dev,
  5322. int protect)
  5323. {
  5324. struct pt_core_data *cd = dev_get_drvdata(dev);
  5325. if (protect)
  5326. return pt_hid_output_bl_launch_app(cd);
  5327. return pt_hid_output_bl_launch_app_(cd);
  5328. }
  5329. /*******************************************************************************
  5330. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5331. *
  5332. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5333. * value store in the System Information.
  5334. *
  5335. * RETURN:
  5336. * 0 = success
  5337. * !0 = failure
  5338. *
  5339. * PARAMETERS:
  5340. * *cd - pointer to core data
  5341. * *panel_id - pointer to where the panel ID will be stored
  5342. ******************************************************************************/
  5343. static int pt_hid_output_bl_get_panel_id_(
  5344. struct pt_core_data *cd, u8 *panel_id)
  5345. {
  5346. int rc;
  5347. struct pt_hid_output hid_output = {
  5348. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5349. };
  5350. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5351. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5352. pt_debug(cd->dev, DL_ERROR,
  5353. "%s: Get Panel ID command not supported\n",
  5354. __func__);
  5355. *panel_id = PANEL_ID_NOT_ENABLED;
  5356. return 0;
  5357. } else if (rc < 0) {
  5358. pt_debug(cd->dev, DL_ERROR,
  5359. "%s: Error on Get Panel ID command\n", __func__);
  5360. return rc;
  5361. }
  5362. *panel_id = cd->response_buf[8];
  5363. return 0;
  5364. }
  5365. /*******************************************************************************
  5366. * FUNCTION: pt_hid_output_bl_get_panel_id
  5367. *
  5368. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5369. * to the DUT.
  5370. *
  5371. * RETURN:
  5372. * 0 = success
  5373. * !0 = failure
  5374. *
  5375. * PARAMETERS:
  5376. * *cd - pointer to core data
  5377. * *panel_id - pointer to where the panel ID will be stored
  5378. ******************************************************************************/
  5379. static int pt_hid_output_bl_get_panel_id(
  5380. struct pt_core_data *cd, u8 *panel_id)
  5381. {
  5382. int rc;
  5383. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5384. if (rc < 0) {
  5385. pt_debug(cd->dev, DL_ERROR,
  5386. "%s: fail get exclusive ex=%p own=%p\n",
  5387. __func__, cd->exclusive_dev, cd->dev);
  5388. return rc;
  5389. }
  5390. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5391. if (release_exclusive(cd, cd->dev) < 0)
  5392. pt_debug(cd->dev, DL_ERROR,
  5393. "%s: fail to release exclusive\n", __func__);
  5394. return rc;
  5395. }
  5396. /*******************************************************************************
  5397. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5398. *
  5399. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5400. * to have the BL retrieve the panel ID
  5401. *
  5402. * RETURN:
  5403. * 0 = success
  5404. * !0 = failure
  5405. *
  5406. * PARAMETERS:
  5407. * *dev - pointer to device structure
  5408. * protect - flag to run in protected mode
  5409. * *panel_id - pointer to where the panel ID will be stored
  5410. ******************************************************************************/
  5411. static int _pt_request_pip_bl_get_panel_id(
  5412. struct device *dev, int protect, u8 *panel_id)
  5413. {
  5414. struct pt_core_data *cd = dev_get_drvdata(dev);
  5415. if (protect)
  5416. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5417. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5418. }
  5419. /*******************************************************************************
  5420. * FUNCTION: pt_pip2_get_mode_sysmode_
  5421. *
  5422. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5423. * PIP2 STATUS command.
  5424. *
  5425. * RETURN:
  5426. * 0 = success
  5427. * !0 = failure
  5428. *
  5429. * PARAMETERS:
  5430. * *cd - pointer to core data structure
  5431. * *mode - pointer to store the retrieved mode
  5432. * *sys_mode - pointer to store the FW system mode
  5433. ******************************************************************************/
  5434. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5435. u8 *mode, u8 *sys_mode)
  5436. {
  5437. u16 actual_read_len;
  5438. u8 read_buf[12];
  5439. u8 status, boot;
  5440. int rc = 0;
  5441. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5442. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5443. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5444. __func__, rc);
  5445. if (!rc) {
  5446. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5447. "PIP2 STATUS");
  5448. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5449. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5450. if (sys_mode) {
  5451. if (status == PIP2_RSP_ERR_NONE &&
  5452. boot == PIP2_STATUS_APP_EXEC)
  5453. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5454. else
  5455. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5456. }
  5457. if (mode) {
  5458. if (status == PIP2_RSP_ERR_NONE &&
  5459. boot == PIP2_STATUS_BOOT_EXEC)
  5460. *mode = PT_MODE_BOOTLOADER;
  5461. else if (status == PIP2_RSP_ERR_NONE &&
  5462. boot == PIP2_STATUS_APP_EXEC)
  5463. *mode = PT_MODE_OPERATIONAL;
  5464. else
  5465. *mode = PT_MODE_UNKNOWN;
  5466. }
  5467. } else {
  5468. if (mode)
  5469. *mode = PT_MODE_UNKNOWN;
  5470. if (sys_mode)
  5471. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5472. pt_debug(cd->dev, DL_WARN,
  5473. "%s: Mode and sys_mode could not be determined\n",
  5474. __func__);
  5475. }
  5476. return rc;
  5477. }
  5478. /*******************************************************************************
  5479. * FUNCTION: pt_pip2_get_mode_sysmode
  5480. *
  5481. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5482. * access to the DUT.
  5483. *
  5484. * RETURN:
  5485. * 0 = success
  5486. * !0 = failure
  5487. *
  5488. * PARAMETERS:
  5489. * *cd - pointer to core data structure
  5490. * *mode - pointer to store the retrieved mode
  5491. * *sys_mode - pointer to store the FW system mode
  5492. ******************************************************************************/
  5493. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5494. u8 *mode, u8 *sys_mode)
  5495. {
  5496. int rc;
  5497. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5498. if (rc < 0) {
  5499. pt_debug(cd->dev, DL_ERROR,
  5500. "%s: fail get exclusive ex=%p own=%p\n",
  5501. __func__, cd->exclusive_dev, cd->dev);
  5502. return rc;
  5503. }
  5504. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5505. if (release_exclusive(cd, cd->dev) < 0)
  5506. pt_debug(cd->dev, DL_ERROR,
  5507. "%s: fail to release exclusive\n", __func__);
  5508. return rc;
  5509. }
  5510. /*******************************************************************************
  5511. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5512. *
  5513. * SUMMARY: Function pointer included in core_commands struct for external
  5514. * calls to the protected or unprotected call to
  5515. * pt_pip2_get_mode_sysmode
  5516. *
  5517. * RETURN:
  5518. * 0 = success
  5519. * !0 = failure
  5520. *
  5521. * PARAMETERS:
  5522. * *dev - pointer to device structure
  5523. * *mode - pointer to store the retrieved mode
  5524. * *sys_mode - pointer to store the FW system mode
  5525. ******************************************************************************/
  5526. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5527. int protect, u8 *mode, u8 *sys_mode)
  5528. {
  5529. struct pt_core_data *cd = dev_get_drvdata(dev);
  5530. if (protect)
  5531. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5532. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5533. }
  5534. /*******************************************************************************
  5535. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5536. *
  5537. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5538. * mode only the following PIP commands will be serviced, any other PIP
  5539. * command the FW will respond with an "Invalid PIP Command" response.
  5540. * - Get HID Descriptor (Register 0x0001, no command ID)
  5541. * - Reset (Register 0x0005, RESET HID request)
  5542. * - Ping (Register 0x0004, Command ID 0x00
  5543. * - Get System Information (Register 0x0004, Command ID 0x02)
  5544. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5545. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5546. * This function will loop on the results of the STATUS command until
  5547. * the FW reports it is out of BOOT mode.
  5548. *
  5549. * NOTE:
  5550. * - This function will update cd->fw_system_mode
  5551. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5552. *
  5553. * RETURN:
  5554. * 0 = success
  5555. * !0 = failure
  5556. *
  5557. * PARAMETERS:
  5558. * *cd - pointer to core data
  5559. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5560. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5561. ******************************************************************************/
  5562. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5563. int *actual_wait)
  5564. {
  5565. int loop = 0;
  5566. u8 sys_mode = cd->fw_system_mode;
  5567. u8 pause = 10; /* in ms */
  5568. int rc = 0;
  5569. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5570. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5571. /*
  5572. * For PIP <1.11, no support for polling wait so do a hard
  5573. * coded wait and assume the FW is out of BOOT. Added 1 to
  5574. * timeout to make it clear in kmsg if non polling was done.
  5575. */
  5576. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5577. pt_debug(cd->dev, DL_ERROR,
  5578. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5579. __func__,
  5580. cd->sysinfo.ttdata.pip_ver_major,
  5581. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5582. msleep(*actual_wait);
  5583. sys_mode = FW_SYS_MODE_SCANNING;
  5584. }
  5585. if (sys_mode == FW_SYS_MODE_BOOT) {
  5586. while (!rc && loop <= max_loop &&
  5587. (sys_mode == FW_SYS_MODE_BOOT)) {
  5588. loop++;
  5589. usleep_range(9000, pause * 1000);
  5590. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5591. pt_debug(cd->dev, DL_DEBUG,
  5592. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5593. __func__, loop * pause, sys_mode);
  5594. }
  5595. *actual_wait = (int)(loop * pause);
  5596. pt_debug(cd->dev, DL_WARN,
  5597. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5598. __func__, *actual_wait, sys_mode);
  5599. if (rc)
  5600. sys_mode = FW_SYS_MODE_UNDEFINED;
  5601. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5602. sys_mode == FW_SYS_MODE_UNDEFINED)
  5603. rc = -EBUSY;
  5604. }
  5605. mutex_lock(&cd->system_lock);
  5606. cd->fw_system_mode = sys_mode;
  5607. mutex_unlock(&cd->system_lock);
  5608. return rc;
  5609. }
  5610. /*******************************************************************************
  5611. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5612. *
  5613. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5614. * access to the DUT.
  5615. *
  5616. * RETURN:
  5617. * 0 = success
  5618. * !0 = failure
  5619. *
  5620. * PARAMETERS:
  5621. * *cd - pointer to core data
  5622. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5623. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5624. ******************************************************************************/
  5625. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5626. int *actual_wait)
  5627. {
  5628. int rc;
  5629. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5630. if (rc < 0) {
  5631. pt_debug(cd->dev, DL_ERROR,
  5632. "%s: fail get exclusive ex=%p own=%p\n",
  5633. __func__, cd->exclusive_dev, cd->dev);
  5634. return rc;
  5635. }
  5636. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5637. if (release_exclusive(cd, cd->dev) < 0)
  5638. pt_debug(cd->dev, DL_ERROR,
  5639. "%s: fail to release exclusive\n", __func__);
  5640. return rc;
  5641. }
  5642. /*******************************************************************************
  5643. * FUNCTION: _pt_get_fw_sys_mode
  5644. *
  5645. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5646. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5647. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5648. * information but any PIP command above 0x1F requires scanning to be disabled
  5649. * before it will be operational. If scanning was not disabled before sending
  5650. * these PIP commands the FW will respond with a 6 byte error response. So to
  5651. * safely determine the scanning state, a PIP message that will not affect the
  5652. * operation of the FW was chosen:
  5653. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5654. * received scanning is active.
  5655. *
  5656. * RETURN:
  5657. * 0 = success
  5658. * !0 = failure
  5659. *
  5660. * PARAMETERS:
  5661. * *cd - pointer to core data
  5662. * *sys_mode - pointer to FW System mode
  5663. * *mode - pointer to mode (BL/FW)
  5664. ******************************************************************************/
  5665. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5666. {
  5667. int write_length = 1;
  5668. int report_length;
  5669. int rc = 0;
  5670. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5671. u8 tmp_mode = PT_MODE_UNKNOWN;
  5672. u8 param[1] = { PT_TCH_PARM_EBID };
  5673. struct pt_hid_output hid_output = {
  5674. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5675. .write_length = write_length,
  5676. .write_buf = param,
  5677. .novalidate = true,
  5678. };
  5679. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5680. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5681. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5682. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5683. __func__, tmp_sys_mode, tmp_mode);
  5684. if (!rc) {
  5685. if (tmp_mode != PT_MODE_OPERATIONAL)
  5686. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5687. }
  5688. goto exit;
  5689. }
  5690. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5691. if (cd->mode != PT_MODE_OPERATIONAL) {
  5692. tmp_mode = cd->mode;
  5693. goto exit;
  5694. }
  5695. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5696. if (rc)
  5697. goto exit;
  5698. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5699. if ((report_length == 0x06) &&
  5700. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5701. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5702. tmp_mode = PIP2_STATUS_APP_EXEC;
  5703. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5704. } else if ((report_length == 0x0A) &&
  5705. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5706. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5707. tmp_mode = PIP2_STATUS_APP_EXEC;
  5708. tmp_sys_mode = FW_SYS_MODE_TEST;
  5709. }
  5710. exit:
  5711. if (mode)
  5712. *mode = tmp_mode;
  5713. if (sys_mode)
  5714. *sys_mode = tmp_sys_mode;
  5715. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5716. __func__, tmp_mode, tmp_sys_mode);
  5717. return rc;
  5718. }
  5719. /*******************************************************************************
  5720. * FUNCTION: pt_get_fw_sys_mode
  5721. *
  5722. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5723. * is active or not.
  5724. *
  5725. * RETURN:
  5726. * 0 = success
  5727. * !0 = failure
  5728. *
  5729. * PARAMETERS:
  5730. * *cd - pointer to core data
  5731. * *sys_mode - pointer to fw system mode
  5732. * *mode - pointer to mode
  5733. ******************************************************************************/
  5734. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5735. {
  5736. int rc;
  5737. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5738. if (rc < 0) {
  5739. pt_debug(cd->dev, DL_ERROR,
  5740. "%s: fail get exclusive ex=%p own=%p\n",
  5741. __func__, cd->exclusive_dev, cd->dev);
  5742. return rc;
  5743. }
  5744. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5745. if (release_exclusive(cd, cd->dev) < 0)
  5746. pt_debug(cd->dev, DL_ERROR,
  5747. "%s: fail to release exclusive\n", __func__);
  5748. return rc;
  5749. }
  5750. /*******************************************************************************
  5751. * FUNCTION: _pt_request_get_fw_sys_mode
  5752. *
  5753. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5754. * to request to get scan state
  5755. *
  5756. * RETURN:
  5757. * 0 = success
  5758. * !0 = failure
  5759. *
  5760. * PARAMETERS:
  5761. * *dev - pointer to device structure
  5762. * protect - flag to call protected or non-protected
  5763. * *sys_mode - pointer to FW system mode
  5764. * *mode - pointer to mode
  5765. ******************************************************************************/
  5766. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5767. u8 *sys_mode, u8 *mode)
  5768. {
  5769. struct pt_core_data *cd = dev_get_drvdata(dev);
  5770. if (protect)
  5771. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5772. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5773. }
  5774. /* Default hid descriptor to provide basic register map */
  5775. const struct pt_hid_desc hid_desc_default = {
  5776. 230, /* hid_desc_len */
  5777. HID_APP_REPORT_ID, /* packet_id */
  5778. 0x00, /* reserved_byte */
  5779. 0x0100, /* bcd_version */
  5780. 0x00EC, /* report_desc_len */
  5781. 0x0002, /* report_desc_register */
  5782. 0x0003, /* input_register */
  5783. 0x00FE, /* max_input_len */
  5784. 0x0004, /* output_register */
  5785. 0x00FE, /* max_output_len */
  5786. 0x0005, /* command_register */
  5787. 0x0006, /* data_register */
  5788. 0x04B4, /* vendor_id */
  5789. 0xC101, /* product_id */
  5790. 0x0100, /* version_id */
  5791. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5792. };
  5793. /*******************************************************************************
  5794. * FUNCTION: pt_init_hid_descriptor
  5795. *
  5796. * SUMMARY: Setup default values for HID descriptor structure
  5797. *
  5798. *
  5799. * PARAMETERS:
  5800. * *desc - pointer to the HID descriptor data read back from DUT
  5801. ******************************************************************************/
  5802. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5803. {
  5804. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5805. }
  5806. /*******************************************************************************
  5807. * FUNCTION: pt_get_hid_descriptor_
  5808. *
  5809. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5810. * into the HID descriptor structure
  5811. *
  5812. * RETURN:
  5813. * 0 = success
  5814. * !0 = failure
  5815. *
  5816. * PARAMETERS:
  5817. * *cd - pointer to core data
  5818. * *desc - pointer to the HID descriptor data read back from DUT
  5819. ******************************************************************************/
  5820. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5821. struct pt_hid_desc *desc)
  5822. {
  5823. struct device *dev = cd->dev;
  5824. int rc = 0;
  5825. int t;
  5826. u8 cmd[2];
  5827. /*
  5828. * During startup the HID descriptor is required for all future
  5829. * processing. If IRQ is already asserted due to an early touch report
  5830. * the report must be cleared before sending command.
  5831. */
  5832. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5833. /* Read HID descriptor length and version */
  5834. mutex_lock(&cd->system_lock);
  5835. cd->hid_cmd_state = 1;
  5836. mutex_unlock(&cd->system_lock);
  5837. /* Set HID descriptor register */
  5838. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5839. sizeof(cd->hid_core.hid_desc_register));
  5840. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5841. __func__, sizeof(cmd));
  5842. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5843. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5844. if (rc) {
  5845. pt_debug(dev, DL_ERROR,
  5846. "%s: failed to get HID descriptor, rc=%d\n",
  5847. __func__, rc);
  5848. goto error;
  5849. }
  5850. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5851. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5852. if (IS_TMO(t)) {
  5853. #ifdef TTDL_DIAGNOSTICS
  5854. cd->bus_transmit_error_count++;
  5855. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5856. #endif /* TTDL_DIAGNOSTICS */
  5857. pt_debug(cd->dev, DL_ERROR,
  5858. "%s: HID get descriptor timed out\n", __func__);
  5859. rc = -ETIME;
  5860. goto error;
  5861. } else {
  5862. cd->hw_detected = true;
  5863. }
  5864. /* Load the HID descriptor including all registers */
  5865. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5866. /* Check HID descriptor length and version */
  5867. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5868. le16_to_cpu(desc->hid_desc_len),
  5869. le16_to_cpu(desc->bcd_version));
  5870. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5871. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5872. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5873. __func__);
  5874. return -ENODEV;
  5875. }
  5876. goto exit;
  5877. error:
  5878. mutex_lock(&cd->system_lock);
  5879. cd->hid_cmd_state = 0;
  5880. mutex_unlock(&cd->system_lock);
  5881. exit:
  5882. return rc;
  5883. }
  5884. /*******************************************************************************
  5885. * FUNCTION: pt_get_hid_descriptor
  5886. *
  5887. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5888. *
  5889. * RETURN:
  5890. * 0 = success
  5891. * !0 = failure
  5892. *
  5893. * PARAMETERS:
  5894. * *cd - pointer to core data
  5895. * *desc - pointer to the HID descriptor data read back from DUT
  5896. ******************************************************************************/
  5897. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5898. struct pt_hid_desc *desc)
  5899. {
  5900. int rc;
  5901. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5902. if (rc < 0) {
  5903. pt_debug(cd->dev, DL_ERROR,
  5904. "%s: fail get exclusive ex=%p own=%p\n",
  5905. __func__, cd->exclusive_dev, cd->dev);
  5906. return rc;
  5907. }
  5908. rc = pt_get_hid_descriptor_(cd, desc);
  5909. if (release_exclusive(cd, cd->dev) < 0)
  5910. pt_debug(cd->dev, DL_ERROR,
  5911. "%s: fail to release exclusive\n", __func__);
  5912. return rc;
  5913. }
  5914. /*******************************************************************************
  5915. * FUNCTION: pt_pip2_get_version_
  5916. *
  5917. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5918. * cd-ttdata
  5919. *
  5920. * RETURN:
  5921. * 0 = success
  5922. * !0 = failure
  5923. *
  5924. * PARAMETERS:
  5925. * *cd - pointer to core data
  5926. ******************************************************************************/
  5927. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5928. {
  5929. int rc = 0;
  5930. int status;
  5931. u8 read_buf[64];
  5932. u16 actual_read_len;
  5933. rc = _pt_request_pip2_send_cmd(cd->dev,
  5934. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5935. NULL, 0, read_buf, &actual_read_len);
  5936. if (rc) {
  5937. pt_debug(cd->dev, DL_ERROR,
  5938. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5939. __func__, rc);
  5940. return rc;
  5941. }
  5942. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5943. if (status == 0) {
  5944. /* Parse the PIP2 VERSION response into ttdata */
  5945. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5946. } else {
  5947. pt_debug(cd->dev, DL_ERROR,
  5948. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5949. __func__, status);
  5950. return status;
  5951. }
  5952. return rc;
  5953. }
  5954. /*******************************************************************************
  5955. * FUNCTION: pt_pip2_get_version
  5956. *
  5957. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5958. * access to the DUT.
  5959. *
  5960. * RETURN:
  5961. * 0 = success
  5962. * !0 = failure
  5963. *
  5964. * PARAMETERS:
  5965. * *cd - pointer to core data structure
  5966. ******************************************************************************/
  5967. static int pt_pip2_get_version(struct pt_core_data *cd)
  5968. {
  5969. int rc;
  5970. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5971. if (rc < 0) {
  5972. pt_debug(cd->dev, DL_ERROR,
  5973. "%s: fail get exclusive ex=%p own=%p\n",
  5974. __func__, cd->exclusive_dev, cd->dev);
  5975. return rc;
  5976. }
  5977. rc = pt_pip2_get_version_(cd);
  5978. if (release_exclusive(cd, cd->dev) < 0)
  5979. pt_debug(cd->dev, DL_ERROR,
  5980. "%s: fail to release exclusive\n", __func__);
  5981. return rc;
  5982. }
  5983. /*******************************************************************************
  5984. * FUNCTION: _pt_request_active_pip_protocol
  5985. *
  5986. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5987. * Function will return PIP version of BL or application based on
  5988. * when it's called.
  5989. *
  5990. * RETURN:
  5991. * 0 = success
  5992. * !0 = failure
  5993. *
  5994. * PARAMETERS:
  5995. * *dev - pointer to device structure
  5996. * protect - flag to run in protected mode
  5997. * *pip_version_major - pointer to store PIP major version
  5998. * *pip_version_minor - pointer to store PIP minor version
  5999. ******************************************************************************/
  6000. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  6001. u8 *pip_version_major, u8 *pip_version_minor)
  6002. {
  6003. struct pt_core_data *cd = dev_get_drvdata(dev);
  6004. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6005. int rc = 0;
  6006. struct pt_hid_output sys_info = {
  6007. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  6008. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  6009. };
  6010. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  6011. /* Skip PIP2 command if DUT generation is confirmed */
  6012. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6013. goto skip_pip2_command;
  6014. rc = pt_pip2_get_version_(cd);
  6015. if (!rc) {
  6016. *pip_version_major = ttdata->pip_ver_major;
  6017. *pip_version_minor = ttdata->pip_ver_minor;
  6018. pt_debug(dev, DL_INFO,
  6019. "%s: pip_version = %d.%d\n", __func__,
  6020. *pip_version_major, *pip_version_minor);
  6021. } else {
  6022. /*
  6023. * Legacy products do not support the pip2 protocol to get
  6024. * pip version. However, they do support the "get sysinfo"
  6025. * command to get pip version from FW, but the bootloader
  6026. * does not support it. This function will try "get sysinfo"
  6027. * command if the pip2 command failed but this cmd could also
  6028. * fail if DUT is stuck in bootloader mode.
  6029. */
  6030. pt_debug(dev, DL_INFO,
  6031. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6032. __func__, rc);
  6033. skip_pip2_command:
  6034. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6035. if (!rc) {
  6036. *pip_version_minor =
  6037. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6038. *pip_version_major =
  6039. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6040. pt_debug(dev, DL_INFO,
  6041. "%s: pip_version = %d.%d\n", __func__,
  6042. *pip_version_major, *pip_version_minor);
  6043. } else {
  6044. *pip_version_major = 0;
  6045. *pip_version_minor = 0;
  6046. pt_debug(dev, DL_ERROR,
  6047. "%s: pip_version Not Detected\n", __func__);
  6048. }
  6049. }
  6050. return rc;
  6051. }
  6052. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6053. /*******************************************************************************
  6054. * FUNCTION: _pt_detect_dut_generation
  6055. *
  6056. * SUMMARY: Determine the generation of device that we are communicating with:
  6057. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6058. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6059. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6060. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6061. * coming form the BL or the FW:
  6062. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6063. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6064. * This function will return a modified status if it detects the DUT
  6065. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6066. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6067. *
  6068. * RETURN:
  6069. * 0 = success
  6070. * !0 = failure
  6071. *
  6072. * PARAMETERS:
  6073. * *dev - pointer to device structure
  6074. * *status - pointer to status bitmask
  6075. * *dut_gen - pointer to store the dut_generation
  6076. * *mode - pointer to store the PT_MODE
  6077. ******************************************************************************/
  6078. static int _pt_detect_dut_generation(struct device *dev,
  6079. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6080. {
  6081. int rc = 0;
  6082. u8 dut_gen_tmp = DUT_UNKNOWN;
  6083. u8 mode_tmp = PT_MODE_UNKNOWN;
  6084. u8 attempt = 1;
  6085. u32 status_tmp = STARTUP_STATUS_START;
  6086. struct pt_core_data *cd = dev_get_drvdata(dev);
  6087. struct pt_hid_desc hid_desc;
  6088. memset(&hid_desc, 0, sizeof(hid_desc));
  6089. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6090. while (rc && attempt < 3) {
  6091. attempt++;
  6092. usleep_range(2000, 5000);
  6093. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6094. }
  6095. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6096. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6097. mode_tmp = PT_MODE_BOOTLOADER;
  6098. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6099. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6100. rc = pt_pip2_get_version_(cd);
  6101. if (!rc)
  6102. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6103. else
  6104. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6105. mode_tmp = PT_MODE_OPERATIONAL;
  6106. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6107. rc = 0; /* To return success instead of error code */
  6108. } else if (rc) {
  6109. rc = pt_pip2_get_version_(cd);
  6110. if (!rc) {
  6111. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6112. mode_tmp = PT_MODE_BOOTLOADER;
  6113. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6114. }
  6115. }
  6116. mutex_lock(&cd->system_lock);
  6117. if (dut_gen)
  6118. *dut_gen = dut_gen_tmp;
  6119. if (mode)
  6120. *mode = mode_tmp;
  6121. if (status)
  6122. *status = status_tmp;
  6123. mutex_unlock(&cd->system_lock);
  6124. #ifdef TTDL_DIAGNOSTICS
  6125. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6126. __func__, dut_gen_tmp, mode_tmp);
  6127. #endif /* TTDL_DIAGNOSTICS */
  6128. return rc;
  6129. }
  6130. /*******************************************************************************
  6131. * FUNCTION: _pt_request_dut_generation
  6132. *
  6133. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6134. * to get current dut generation.
  6135. *
  6136. * NOTE: This function WILL NOT try to determine dut generation.
  6137. *
  6138. * RETURN:
  6139. * The current dut generation.
  6140. *
  6141. * PARAMETERS:
  6142. * *dev - pointer to device structure
  6143. ******************************************************************************/
  6144. static int _pt_request_dut_generation(struct device *dev)
  6145. {
  6146. struct pt_core_data *cd = dev_get_drvdata(dev);
  6147. return cd->active_dut_generation;
  6148. }
  6149. #define HW_VERSION_LEN_MAX 13
  6150. /*******************************************************************************
  6151. * FUNCTION: _legacy_generate_hw_version
  6152. *
  6153. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6154. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6155. * command) to the hw_version.
  6156. *
  6157. * RETURN:
  6158. * 0 = success
  6159. * !0 = failure
  6160. *
  6161. * PARAMETERS:
  6162. * *cd - pointer to core data
  6163. * *hw_version - pointer to store the hardware version
  6164. ******************************************************************************/
  6165. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6166. char *hw_version)
  6167. {
  6168. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6169. if (cd->sysinfo.ready) {
  6170. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6171. ttdata->jtag_id_h, cd->pid_for_loader);
  6172. return 0;
  6173. } else if (cd->bl_info.ready) {
  6174. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6175. cd->bl_info.chip_id, cd->pid_for_loader);
  6176. return 0;
  6177. } else {
  6178. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6179. pt_debug(cd->dev, DL_ERROR,
  6180. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6181. return -ENODATA;
  6182. }
  6183. }
  6184. /*******************************************************************************
  6185. * FUNCTION: _pip2_generate_hw_version
  6186. *
  6187. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6188. * VERSION command) to the hw_version.
  6189. *
  6190. * RETURN:
  6191. * 0 = success
  6192. * !0 = failure
  6193. *
  6194. * PARAMETERS:
  6195. * *cd - pointer to core data
  6196. * *hw_version - pointer to store the hardware version
  6197. ******************************************************************************/
  6198. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6199. {
  6200. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6201. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6202. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6203. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6204. return 0;
  6205. } else {
  6206. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6207. pt_debug(cd->dev, DL_ERROR,
  6208. "%s: PIP Version are not ready\n", __func__);
  6209. return -ENODATA;
  6210. }
  6211. }
  6212. /*******************************************************************************
  6213. * FUNCTION: pt_generate_hw_version
  6214. *
  6215. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6216. * hw_version from static data.
  6217. *
  6218. * RETURN:
  6219. * 0 = success
  6220. * !0 = failure
  6221. *
  6222. * PARAMETERS:
  6223. * *cd - pointer to core data
  6224. * *hw_version - pointer to store the hardware version
  6225. ******************************************************************************/
  6226. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6227. {
  6228. int rc = 0;
  6229. if (!hw_version)
  6230. return -ENOMEM;
  6231. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6232. rc = _legacy_generate_hw_version(cd, hw_version);
  6233. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6234. rc = _pip2_generate_hw_version(cd, hw_version);
  6235. else {
  6236. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6237. rc = -ENODATA;
  6238. }
  6239. return rc;
  6240. }
  6241. /*******************************************************************************
  6242. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6243. *
  6244. * NOTE: The calling function must ensure to free *hw_version
  6245. *
  6246. * RETURN:
  6247. * 0 = success
  6248. * !0 = Failure
  6249. *
  6250. * PARAMETERS:
  6251. * *dev - pointer to device structure
  6252. * *hw_version - pointer to where the hw_version string will be stored
  6253. ******************************************************************************/
  6254. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6255. {
  6256. int rc = 0;
  6257. u16 actual_read_len;
  6258. u16 pip_ver;
  6259. u8 rd_buf[256];
  6260. u8 status;
  6261. u8 index = PIP2_RESP_STATUS_OFFSET;
  6262. u8 return_data[8];
  6263. u8 panel_id;
  6264. struct pt_core_data *cd = dev_get_drvdata(dev);
  6265. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6266. if (!hw_version)
  6267. return -ENOMEM;
  6268. if (!cd->hw_detected) {
  6269. /* No HW detected */
  6270. rc = -ENODEV;
  6271. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6272. __func__);
  6273. goto exit_error;
  6274. }
  6275. memset(return_data, 0, ARRAY_SIZE(return_data));
  6276. /* For Parade TC or TT parts */
  6277. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6278. rc = _pt_request_pip2_send_cmd(dev,
  6279. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6280. NULL, 0, rd_buf, &actual_read_len);
  6281. if (rc) {
  6282. pt_debug(dev, DL_ERROR,
  6283. "%s: Failed to send PIP2 VERSION cmd\n",
  6284. __func__);
  6285. goto exit_error;
  6286. }
  6287. status = rd_buf[index];
  6288. if (status == 0) {
  6289. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6290. /*
  6291. * BL PIP 2.02 and greater the version fields are
  6292. * swapped
  6293. */
  6294. if (pip_ver >= 0x0202) {
  6295. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6296. "%02X%02X.%02X%02X.FF",
  6297. rd_buf[index + 10], rd_buf[index + 9],
  6298. rd_buf[index + 8], rd_buf[index + 7]);
  6299. } else {
  6300. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6301. "%02X%02X.%02X%02X.FF",
  6302. rd_buf[index + 8], rd_buf[index + 7],
  6303. rd_buf[index + 10], rd_buf[index + 9]);
  6304. }
  6305. return STATUS_SUCCESS;
  6306. } else {
  6307. rc = status;
  6308. pt_debug(dev, DL_WARN,
  6309. "%s: PIP2 VERSION cmd response error\n",
  6310. __func__);
  6311. }
  6312. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6313. /*
  6314. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6315. * hard coded to FFFF
  6316. */
  6317. if (cd->mode == PT_MODE_OPERATIONAL) {
  6318. rc = pt_hid_output_get_sysinfo(cd);
  6319. if (!rc) {
  6320. panel_id =
  6321. cd->sysinfo.sensing_conf_data.panel_id;
  6322. } else {
  6323. panel_id = PANEL_ID_NOT_ENABLED;
  6324. }
  6325. /* In FW - simply retrieve from ttdata struct */
  6326. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6327. "%04X.FFFF.%02X",
  6328. ttdata->jtag_id_h,
  6329. panel_id);
  6330. return STATUS_SUCCESS;
  6331. } else {
  6332. /*
  6333. * Return the stored value if PT_PANEL_ID_BY_BL
  6334. * is not supported while other feature is.
  6335. */
  6336. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6337. rc = pt_hid_output_bl_get_information(
  6338. cd, return_data);
  6339. if (!rc) {
  6340. rc = pt_hid_output_bl_get_panel_id(
  6341. cd, &panel_id);
  6342. }
  6343. } else
  6344. panel_id = cd->pid_for_loader;
  6345. if (!rc) {
  6346. snprintf(hw_version,
  6347. HW_VERSION_LEN_MAX,
  6348. "%02X%02X.FFFF.%02X",
  6349. return_data[3], return_data[2],
  6350. panel_id);
  6351. return STATUS_SUCCESS;
  6352. }
  6353. }
  6354. } else {
  6355. /* Unknown generation */
  6356. rc = -ENODEV;
  6357. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6358. __func__);
  6359. }
  6360. exit_error:
  6361. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6362. return rc;
  6363. }
  6364. /*******************************************************************************
  6365. * FUNCTION: pt_start_wd_timer
  6366. *
  6367. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6368. *
  6369. * RETURN: void
  6370. *
  6371. * PARAMETERS:
  6372. * *cd - pointer to core data
  6373. ******************************************************************************/
  6374. static void pt_start_wd_timer(struct pt_core_data *cd)
  6375. {
  6376. if (cd->watchdog_interval < 100) {
  6377. pt_debug(cd->dev, DL_ERROR,
  6378. "%s: WARNING: Invalid watchdog interval: %d\n",
  6379. __func__, cd->watchdog_interval);
  6380. return;
  6381. }
  6382. if (cd->watchdog_force_stop) {
  6383. pt_debug(cd->dev, DL_INFO,
  6384. "%s: TTDL WD Forced stop\n", __func__);
  6385. return;
  6386. }
  6387. mod_timer(&cd->watchdog_timer, jiffies +
  6388. msecs_to_jiffies(cd->watchdog_interval));
  6389. cd->watchdog_enabled = 1;
  6390. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6391. }
  6392. /*******************************************************************************
  6393. * FUNCTION: pt_stop_wd_timer
  6394. *
  6395. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6396. *
  6397. * RETURN: void
  6398. *
  6399. * PARAMETERS:
  6400. * *cd - pointer to core data
  6401. ******************************************************************************/
  6402. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6403. {
  6404. if (!cd->watchdog_interval)
  6405. return;
  6406. /*
  6407. * Ensure we wait until the watchdog timer
  6408. * running on a different CPU finishes
  6409. */
  6410. del_timer_sync(&cd->watchdog_timer);
  6411. cancel_work_sync(&cd->watchdog_work);
  6412. del_timer_sync(&cd->watchdog_timer);
  6413. cd->watchdog_enabled = 0;
  6414. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6415. }
  6416. /*******************************************************************************
  6417. * FUNCTION: pt_hw_soft_reset
  6418. *
  6419. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6420. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6421. * schedule an enum if it fires when the DUT is being reset.
  6422. * This can cause a double reset.
  6423. *
  6424. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6425. * the WD active could cause this function to fail!
  6426. *
  6427. * RETURN:
  6428. * 0 = success
  6429. * !0 = failure
  6430. *
  6431. * PARAMETERS:
  6432. * *cd - pointer to core data struct
  6433. * protect - flag to call protected or non-protected
  6434. ******************************************************************************/
  6435. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6436. {
  6437. int rc = 0;
  6438. mutex_lock(&cd->system_lock);
  6439. cd->startup_status = STARTUP_STATUS_START;
  6440. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6441. mutex_unlock(&cd->system_lock);
  6442. if (protect)
  6443. rc = pt_hid_cmd_reset(cd);
  6444. else
  6445. rc = pt_hid_cmd_reset_(cd);
  6446. if (rc < 0) {
  6447. pt_debug(cd->dev, DL_ERROR,
  6448. "%s: FAILED to execute SOFT reset\n", __func__);
  6449. return rc;
  6450. }
  6451. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6452. __func__);
  6453. return 0;
  6454. }
  6455. /*******************************************************************************
  6456. * FUNCTION: pt_hw_hard_reset
  6457. *
  6458. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6459. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6460. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6461. * schedule an enum if it fires when the DUT is being reset.
  6462. * This can cause a double reset.
  6463. *
  6464. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6465. * the WD active could cause this function to fail!
  6466. *
  6467. * RETURN:
  6468. * 0 = success
  6469. * !0 = failure
  6470. *
  6471. * PARAMETERS:
  6472. * *cd - pointer to core data struct
  6473. ******************************************************************************/
  6474. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6475. {
  6476. if (cd->cpdata->xres) {
  6477. cd->startup_status = STARTUP_STATUS_START;
  6478. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6479. __func__);
  6480. cd->cpdata->xres(cd->cpdata, cd->dev);
  6481. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6482. __func__);
  6483. return 0;
  6484. }
  6485. pt_debug(cd->dev, DL_ERROR,
  6486. "%s: FAILED to execute HARD reset\n", __func__);
  6487. return -ENODEV;
  6488. }
  6489. /*******************************************************************************
  6490. * FUNCTION: pt_dut_reset
  6491. *
  6492. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6493. * soft reset.
  6494. *
  6495. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6496. * the WD active could cause this function to fail!
  6497. * NOTE: "protect" flag is only used for soft reset.
  6498. *
  6499. * RETURN:
  6500. * 0 = success
  6501. * !0 = failure
  6502. *
  6503. * PARAMETERS:
  6504. * *cd - pointer to core data structure
  6505. * protect - flag to call protected or non-protected
  6506. ******************************************************************************/
  6507. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6508. {
  6509. int rc = 0;
  6510. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6511. mutex_lock(&cd->system_lock);
  6512. cd->hid_reset_cmd_state = 1;
  6513. rc = pt_hw_hard_reset(cd);
  6514. mutex_unlock(&cd->system_lock);
  6515. if (rc == -ENODEV) {
  6516. mutex_lock(&cd->system_lock);
  6517. cd->hid_reset_cmd_state = 0;
  6518. mutex_unlock(&cd->system_lock);
  6519. pt_debug(cd->dev, DL_ERROR,
  6520. "%s: Hard reset failed, try soft reset\n", __func__);
  6521. rc = pt_hw_soft_reset(cd, protect);
  6522. }
  6523. if (rc)
  6524. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6525. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6526. return rc;
  6527. }
  6528. /*******************************************************************************
  6529. * FUNCTION: pt_dut_reset_and_wait
  6530. *
  6531. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6532. * complete
  6533. *
  6534. * RETURN:
  6535. * 0 = success
  6536. * !0 = failure
  6537. *
  6538. * PARAMETERS:
  6539. * *cd - pointer to core data structure
  6540. ******************************************************************************/
  6541. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6542. {
  6543. int rc = 0;
  6544. int t;
  6545. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6546. if (rc < 0)
  6547. goto exit;
  6548. t = wait_event_timeout(cd->wait_q,
  6549. (cd->hid_reset_cmd_state == 0),
  6550. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6551. if (IS_TMO(t)) {
  6552. #ifdef TTDL_DIAGNOSTICS
  6553. cd->bus_transmit_error_count++;
  6554. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6555. #endif /* TTDL_DIAGNOSTICS */
  6556. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6557. __func__);
  6558. rc = -ETIME;
  6559. goto exit;
  6560. }
  6561. exit:
  6562. return rc;
  6563. }
  6564. /*
  6565. * touch default parameters (from report descriptor) to resolve protocol for
  6566. * touch report
  6567. */
  6568. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6569. /* byte offset, size, min, max, bit offset, report */
  6570. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6571. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6572. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6573. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6574. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6575. };
  6576. /*
  6577. * button default parameters (from report descriptor) to resolve protocol for
  6578. * button report
  6579. */
  6580. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6581. /* byte offset, size, min, max, bit offset, report */
  6582. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6583. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6584. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6585. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6586. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6587. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6588. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6589. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6590. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6591. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6592. };
  6593. /*******************************************************************************
  6594. * FUNCTION: pt_init_pip_report_fields
  6595. *
  6596. * SUMMARY: Setup default values for touch/button report parsing.
  6597. *
  6598. * PARAMETERS:
  6599. * *cd - pointer to core data structure
  6600. ******************************************************************************/
  6601. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6602. {
  6603. struct pt_sysinfo *si = &cd->sysinfo;
  6604. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6605. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6606. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6607. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6608. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6609. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6610. cd->features.easywake = 1;
  6611. cd->features.noise_metric = 1;
  6612. cd->features.tracking_heatmap = 1;
  6613. cd->features.sensor_data = 1;
  6614. }
  6615. /*******************************************************************************
  6616. * FUNCTION: pt_get_mode
  6617. *
  6618. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6619. * message
  6620. *
  6621. * RETURN: Enum of the current mode
  6622. *
  6623. * PARAMETERS:
  6624. * *cd - pointer to the Core Data structure
  6625. * protect - run command in protected mode
  6626. * *mode - pointer to store the retrieved mode
  6627. ******************************************************************************/
  6628. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6629. {
  6630. if (desc->packet_id == HID_APP_REPORT_ID)
  6631. return PT_MODE_OPERATIONAL;
  6632. else if (desc->packet_id == HID_BL_REPORT_ID)
  6633. return PT_MODE_BOOTLOADER;
  6634. return PT_MODE_UNKNOWN;
  6635. }
  6636. /*******************************************************************************
  6637. * FUNCTION: _pt_request_get_mode
  6638. *
  6639. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6640. * to determine the current mode of the DUT by use of the Get HID
  6641. * Descriptor command.
  6642. *
  6643. * RETURN:
  6644. * 0 = success
  6645. * !0 = failure
  6646. *
  6647. * PARAMETERS:
  6648. * *dev - pointer to device structure
  6649. * protect - run command in protected mode
  6650. * *mode - pointer to store the retrieved mode
  6651. ******************************************************************************/
  6652. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6653. {
  6654. struct pt_core_data *cd = dev_get_drvdata(dev);
  6655. struct pt_hid_desc hid_desc;
  6656. int rc = 0;
  6657. memset(&hid_desc, 0, sizeof(hid_desc));
  6658. if (protect)
  6659. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6660. else
  6661. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6662. if (rc)
  6663. *mode = PT_MODE_UNKNOWN;
  6664. else
  6665. *mode = pt_get_mode(cd, &hid_desc);
  6666. return rc;
  6667. }
  6668. /*******************************************************************************
  6669. * FUNCTION: pt_queue_enum_
  6670. *
  6671. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6672. * function. It won't try to add/delete sysfs node or modules.
  6673. *
  6674. * RETURN: void
  6675. *
  6676. * PARAMETERS:
  6677. * *cd - pointer to core data
  6678. ******************************************************************************/
  6679. static void pt_queue_enum_(struct pt_core_data *cd)
  6680. {
  6681. if (cd->startup_state == STARTUP_NONE) {
  6682. cd->startup_state = STARTUP_QUEUED;
  6683. #ifdef TTDL_DIAGNOSTICS
  6684. if (!cd->bridge_mode)
  6685. schedule_work(&cd->enum_work);
  6686. else
  6687. cd->startup_state = STARTUP_NONE;
  6688. #else
  6689. schedule_work(&cd->enum_work);
  6690. #endif
  6691. pt_debug(cd->dev, DL_INFO,
  6692. "%s: enum_work queued\n", __func__);
  6693. } else {
  6694. pt_debug(cd->dev, DL_WARN,
  6695. "%s: Enum not queued - startup_state = %d\n",
  6696. __func__, cd->startup_state);
  6697. }
  6698. }
  6699. /*******************************************************************************
  6700. * FUNCTION: pt_queue_enum
  6701. *
  6702. * SUMMARY: Queues a TTDL enum within a mutex lock
  6703. *
  6704. * RETURN: void
  6705. *
  6706. * PARAMETERS:
  6707. * *cd - pointer to core data
  6708. ******************************************************************************/
  6709. static void pt_queue_enum(struct pt_core_data *cd)
  6710. {
  6711. mutex_lock(&cd->system_lock);
  6712. pt_queue_enum_(cd);
  6713. mutex_unlock(&cd->system_lock);
  6714. }
  6715. static void remove_sysfs_and_modules(struct device *dev);
  6716. /*******************************************************************************
  6717. * FUNCTION: pt_queue_restart
  6718. *
  6719. * SUMMARY: Queues a TTDL restart within a mutex lock
  6720. *
  6721. * RETURN: void
  6722. *
  6723. * PARAMETERS:
  6724. * *cd - pointer to core data
  6725. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6726. * False: will not perform remove action
  6727. ******************************************************************************/
  6728. static void pt_queue_restart(struct pt_core_data *cd)
  6729. {
  6730. mutex_lock(&cd->system_lock);
  6731. if (cd->startup_state == STARTUP_NONE) {
  6732. cd->startup_state = STARTUP_QUEUED;
  6733. schedule_work(&cd->ttdl_restart_work);
  6734. pt_debug(cd->dev, DL_INFO,
  6735. "%s: pt_ttdl_restart queued\n", __func__);
  6736. } else {
  6737. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6738. __func__, cd->startup_state);
  6739. }
  6740. mutex_unlock(&cd->system_lock);
  6741. }
  6742. /*******************************************************************************
  6743. * FUNCTION: call_atten_cb
  6744. *
  6745. * SUMMARY: Iterate over attention list call the function that registered.
  6746. *
  6747. * RETURN: void
  6748. *
  6749. * PARAMETERS:
  6750. * *cd - pointer to core data
  6751. * type - type of attention list
  6752. * mode - condition for execution
  6753. ******************************************************************************/
  6754. static void call_atten_cb(struct pt_core_data *cd,
  6755. enum pt_atten_type type, int mode)
  6756. {
  6757. struct atten_node *atten, *atten_n;
  6758. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6759. __func__, type, mode);
  6760. spin_lock(&cd->spinlock);
  6761. list_for_each_entry_safe(atten, atten_n,
  6762. &cd->atten_list[type], node) {
  6763. if (!mode || atten->mode & mode) {
  6764. spin_unlock(&cd->spinlock);
  6765. pt_debug(cd->dev, DL_DEBUG,
  6766. "%s: attention for '%s'",
  6767. __func__, dev_name(atten->dev));
  6768. atten->func(atten->dev);
  6769. spin_lock(&cd->spinlock);
  6770. }
  6771. }
  6772. spin_unlock(&cd->spinlock);
  6773. }
  6774. /*******************************************************************************
  6775. * FUNCTION: start_fw_upgrade
  6776. *
  6777. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6778. * start firmware upgrade.
  6779. *
  6780. * RETURN:
  6781. * 0 = success
  6782. *
  6783. * PARAMETERS:
  6784. * *data - pointer to core data
  6785. ******************************************************************************/
  6786. static int start_fw_upgrade(void *data)
  6787. {
  6788. struct pt_core_data *cd = (struct pt_core_data *)data;
  6789. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6790. return 0;
  6791. }
  6792. /*******************************************************************************
  6793. * FUNCTION: pt_put_device_into_easy_wakeup_
  6794. *
  6795. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6796. * wake up state.
  6797. *
  6798. * RETURN:
  6799. * 0 = success
  6800. * !0 = failure
  6801. *
  6802. * PARAMETERS:
  6803. * *cd - pointer to core data
  6804. ******************************************************************************/
  6805. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6806. {
  6807. int rc = 0;
  6808. u8 status = 0;
  6809. rc = pt_hid_output_enter_easywake_state_(cd,
  6810. cd->easy_wakeup_gesture, &status);
  6811. if (rc || status == 0)
  6812. return -EBUSY;
  6813. return rc;
  6814. }
  6815. /*******************************************************************************
  6816. * FUNCTION: pt_put_device_into_deep_sleep_
  6817. *
  6818. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6819. *
  6820. * RETURN:
  6821. * 0 = success
  6822. * !0 = error
  6823. *
  6824. * PARAMETERS:
  6825. * *cd - pointer to core data
  6826. ******************************************************************************/
  6827. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6828. {
  6829. int rc = 0;
  6830. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6831. if (rc)
  6832. rc = -EBUSY;
  6833. return rc;
  6834. }
  6835. /*******************************************************************************
  6836. * FUNCTION: pt_put_device_into_deep_standby_
  6837. *
  6838. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6839. *
  6840. * RETURN:
  6841. * 0 = success
  6842. * !0 = error
  6843. *
  6844. * PARAMETERS:
  6845. * *cd - pointer to core data
  6846. ******************************************************************************/
  6847. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6848. {
  6849. int rc = 0;
  6850. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6851. if (rc)
  6852. rc = -EBUSY;
  6853. return rc;
  6854. }
  6855. /*******************************************************************************
  6856. * FUNCTION: pt_core_poweroff_device_
  6857. *
  6858. * SUMMARY: Disable IRQ and HW power down the device.
  6859. *
  6860. * RETURN:
  6861. * 0 = success
  6862. * !0 = failure
  6863. *
  6864. * PARAMETERS:
  6865. * *cd - pointer to core data
  6866. ******************************************************************************/
  6867. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6868. {
  6869. int rc;
  6870. if (cd->irq_enabled) {
  6871. cd->irq_enabled = false;
  6872. disable_irq_nosync(cd->irq);
  6873. }
  6874. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6875. if (rc < 0)
  6876. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6877. __func__, rc);
  6878. return rc;
  6879. }
  6880. /*******************************************************************************
  6881. * FUNCTION: pt_core_sleep_
  6882. *
  6883. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6884. * configuration in the core platform data structure.
  6885. *
  6886. * RETURN:
  6887. * 0 = success
  6888. * !0 = failure
  6889. *
  6890. * PARAMETERS:
  6891. * *cd - pointer to core data
  6892. ******************************************************************************/
  6893. static int pt_core_sleep_(struct pt_core_data *cd)
  6894. {
  6895. int rc = 0;
  6896. mutex_lock(&cd->system_lock);
  6897. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6898. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6899. mutex_unlock(&cd->system_lock);
  6900. pt_debug(cd->dev, DL_INFO,
  6901. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6902. return 0;
  6903. } else {
  6904. cd->sleep_state = SS_SLEEPING;
  6905. }
  6906. mutex_unlock(&cd->system_lock);
  6907. /* Ensure watchdog and startup works stopped */
  6908. pt_stop_wd_timer(cd);
  6909. cancel_work_sync(&cd->enum_work);
  6910. pt_stop_wd_timer(cd);
  6911. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6912. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6913. rc = pt_core_poweroff_device_(cd);
  6914. if (rc)
  6915. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6916. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6917. pt_debug(cd->dev, DL_INFO,
  6918. "%s: Entering into deep standby mode:\n", __func__);
  6919. rc = pt_put_device_into_deep_standby_(cd);
  6920. if (rc)
  6921. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6922. } else {
  6923. pt_debug(cd->dev, DL_INFO,
  6924. "%s: Entering into deep sleep mode:\n", __func__);
  6925. rc = pt_put_device_into_deep_sleep_(cd);
  6926. if (rc)
  6927. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6928. }
  6929. mutex_lock(&cd->system_lock);
  6930. cd->sleep_state = SS_SLEEP_ON;
  6931. mutex_unlock(&cd->system_lock);
  6932. return rc;
  6933. }
  6934. /*******************************************************************************
  6935. * FUNCTION: pt_core_easywake_on_
  6936. *
  6937. * SUMMARY: Suspend the device with easy wake on the
  6938. * configuration in the core platform data structure.
  6939. *
  6940. * RETURN:
  6941. * 0 = success
  6942. * !0 = failure
  6943. *
  6944. * PARAMETERS:
  6945. * *cd - pointer to core data
  6946. ******************************************************************************/
  6947. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6948. {
  6949. int rc = 0;
  6950. mutex_lock(&cd->system_lock);
  6951. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6952. mutex_unlock(&cd->system_lock);
  6953. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6954. __func__, cd->sleep_state);
  6955. return 0;
  6956. }
  6957. mutex_unlock(&cd->system_lock);
  6958. /* Ensure watchdog and startup works stopped */
  6959. pt_stop_wd_timer(cd);
  6960. cancel_work_sync(&cd->enum_work);
  6961. pt_stop_wd_timer(cd);
  6962. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6963. rc = pt_put_device_into_easy_wakeup_(cd);
  6964. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6965. if (rc)
  6966. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6967. }
  6968. mutex_lock(&cd->system_lock);
  6969. cd->sleep_state = SS_EASY_WAKING_ON;
  6970. mutex_unlock(&cd->system_lock);
  6971. return rc;
  6972. }
  6973. /*******************************************************************************
  6974. * FUNCTION: pt_core_easywake_on
  6975. *
  6976. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6977. *
  6978. * RETURN:
  6979. * 0 = success
  6980. * !0 = failure
  6981. *
  6982. * PARAMETERS:
  6983. * *cd - pointer to core data
  6984. ******************************************************************************/
  6985. static int pt_core_easywake_on(struct pt_core_data *cd)
  6986. {
  6987. int rc = 0;
  6988. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6989. if (rc < 0) {
  6990. pt_debug(cd->dev, DL_ERROR,
  6991. "%s: fail get exclusive ex=%p own=%p\n",
  6992. __func__, cd->exclusive_dev, cd->dev);
  6993. return rc;
  6994. }
  6995. rc = pt_core_easywake_on_(cd);
  6996. if (release_exclusive(cd, cd->dev) < 0)
  6997. pt_debug(cd->dev, DL_ERROR,
  6998. "%s: fail to release exclusive\n", __func__);
  6999. else
  7000. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7001. __func__);
  7002. return rc;
  7003. }
  7004. /*******************************************************************************
  7005. * FUNCTION: pt_core_sleep
  7006. *
  7007. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  7008. *
  7009. * RETURN:
  7010. * 0 = success
  7011. * !0 = failure
  7012. *
  7013. * PARAMETERS:
  7014. * *cd - pointer to core data
  7015. ******************************************************************************/
  7016. static int pt_core_sleep(struct pt_core_data *cd)
  7017. {
  7018. int rc = 0;
  7019. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7020. if (rc < 0) {
  7021. pt_debug(cd->dev, DL_ERROR,
  7022. "%s: fail get exclusive ex=%p own=%p\n",
  7023. __func__, cd->exclusive_dev, cd->dev);
  7024. return rc;
  7025. }
  7026. rc = pt_core_sleep_(cd);
  7027. if (release_exclusive(cd, cd->dev) < 0)
  7028. pt_debug(cd->dev, DL_ERROR,
  7029. "%s: fail to release exclusive\n", __func__);
  7030. else
  7031. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7032. __func__);
  7033. return rc;
  7034. }
  7035. /*******************************************************************************
  7036. * FUNCTION: pt_wakeup_host
  7037. *
  7038. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7039. *
  7040. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7041. *
  7042. * RETURN:
  7043. * 0 = success
  7044. * !0 = failure
  7045. *
  7046. * PARAMETERS:
  7047. * *cd - pointer to core data
  7048. ******************************************************************************/
  7049. static int pt_wakeup_host(struct pt_core_data *cd)
  7050. {
  7051. #ifndef EASYWAKE_TSG6
  7052. /* TSG5 EasyWake */
  7053. int rc = 0;
  7054. int event_id;
  7055. int size = get_unaligned_le16(&cd->input_buf[0]);
  7056. /* Validate report */
  7057. if (size != 4 || cd->input_buf[2] != 4)
  7058. rc = -EINVAL;
  7059. event_id = cd->input_buf[3];
  7060. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7061. __func__, event_id, rc);
  7062. if (rc) {
  7063. pt_core_sleep_(cd);
  7064. goto exit;
  7065. }
  7066. /* attention WAKE */
  7067. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7068. exit:
  7069. return rc;
  7070. #else
  7071. /* TSG6 FW1.3 EasyWake */
  7072. int rc = 0;
  7073. int i = 0;
  7074. int report_length;
  7075. /* Validate report */
  7076. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7077. rc = -EINVAL;
  7078. pt_core_sleep_(cd);
  7079. goto exit;
  7080. }
  7081. /* Get gesture id and gesture data length */
  7082. cd->gesture_id = cd->input_buf[3];
  7083. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7084. cd->gesture_data_length = report_length - 4;
  7085. pt_debug(cd->dev, DL_INFO,
  7086. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7087. __func__, cd->gesture_id, cd->gesture_data_length);
  7088. for (i = 0; i < cd->gesture_data_length; i++)
  7089. cd->gesture_data[i] = cd->input_buf[4 + i];
  7090. /* attention WAKE */
  7091. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7092. exit:
  7093. return rc;
  7094. #endif
  7095. }
  7096. /*******************************************************************************
  7097. * FUNCTION: pt_get_touch_axis
  7098. *
  7099. * SUMMARY: Function to calculate touch axis
  7100. *
  7101. * PARAMETERS:
  7102. * *cd - pointer to core data structure
  7103. * *axis - pointer to axis calculation result
  7104. * size - size in bytes
  7105. * max - max value of result
  7106. * *xy_data - pointer to input data to be parsed
  7107. * bofs - bit offset
  7108. ******************************************************************************/
  7109. static void pt_get_touch_axis(struct pt_core_data *cd,
  7110. int *axis, int size, int max, u8 *data, int bofs)
  7111. {
  7112. int nbyte;
  7113. int next;
  7114. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7115. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7116. next++;
  7117. }
  7118. *axis &= max - 1;
  7119. }
  7120. /*******************************************************************************
  7121. * FUNCTION: move_tracking_heatmap_data
  7122. *
  7123. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7124. * system information structure, xy_mode and xy_data.
  7125. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7126. * the tthe_tuner sysfs node under the label "THM"
  7127. *
  7128. * RETURN:
  7129. * 0 = success
  7130. * !0 = failure
  7131. *
  7132. * PARAMETERS:
  7133. * *cd - pointer to core data
  7134. * *si - pointer to the system information structure
  7135. ******************************************************************************/
  7136. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7137. struct pt_sysinfo *si)
  7138. {
  7139. #ifdef TTHE_TUNER_SUPPORT
  7140. int size = get_unaligned_le16(&cd->input_buf[0]);
  7141. if (size)
  7142. tthe_print(cd, cd->input_buf, size, "THM=");
  7143. #endif
  7144. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7145. return 0;
  7146. }
  7147. /*******************************************************************************
  7148. * FUNCTION: move_sensor_data
  7149. *
  7150. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7151. * information structure, xy_mode and xy_data.
  7152. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7153. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7154. *
  7155. * RETURN:
  7156. * 0 = success
  7157. * !0 = failure
  7158. *
  7159. * PARAMETERS:
  7160. * *cd - pointer to core data
  7161. * *si - pointer to the system information structure
  7162. ******************************************************************************/
  7163. static int move_sensor_data(struct pt_core_data *cd,
  7164. struct pt_sysinfo *si)
  7165. {
  7166. #ifdef TTHE_TUNER_SUPPORT
  7167. int size = get_unaligned_le16(&cd->input_buf[0]);
  7168. if (size)
  7169. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7170. #endif
  7171. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7172. return 0;
  7173. }
  7174. /*******************************************************************************
  7175. * FUNCTION: move_button_data
  7176. *
  7177. * SUMMARY: Move the valid button data from the input buffer into the system
  7178. * information structure, xy_mode and xy_data.
  7179. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7180. * the tthe_tuner sysfs node under the label "OpModeData"
  7181. *
  7182. * RETURN:
  7183. * 0 = success
  7184. * !0 = failure
  7185. *
  7186. * PARAMETERS:
  7187. * *cd - pointer to core data
  7188. * *si - pointer to the system information structure
  7189. ******************************************************************************/
  7190. static int move_button_data(struct pt_core_data *cd,
  7191. struct pt_sysinfo *si)
  7192. {
  7193. #ifdef TTHE_TUNER_SUPPORT
  7194. int size = get_unaligned_le16(&cd->input_buf[0]);
  7195. if (size)
  7196. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7197. #endif
  7198. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7199. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7200. "xy_mode");
  7201. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7202. BTN_REPORT_SIZE);
  7203. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7204. "xy_data");
  7205. return 0;
  7206. }
  7207. /*******************************************************************************
  7208. * FUNCTION: move_touch_data
  7209. *
  7210. * SUMMARY: Move the valid touch data from the input buffer into the system
  7211. * information structure, xy_mode and xy_data.
  7212. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7213. * the tthe_tuner sysfs node under the label "OpModeData"
  7214. *
  7215. * RETURN:
  7216. * 0 = success
  7217. * !0 = failure
  7218. *
  7219. * PARAMETERS:
  7220. * *cd - pointer to core data
  7221. * *si - pointer to the system information structure
  7222. ******************************************************************************/
  7223. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7224. {
  7225. int max_tch = si->sensing_conf_data.max_tch;
  7226. int num_cur_tch;
  7227. int length;
  7228. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7229. #ifdef TTHE_TUNER_SUPPORT
  7230. int size = get_unaligned_le16(&cd->input_buf[0]);
  7231. if (size)
  7232. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7233. #endif
  7234. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7235. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7236. si->desc.tch_header_size, "xy_mode");
  7237. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7238. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7239. if (unlikely(num_cur_tch > max_tch))
  7240. num_cur_tch = max_tch;
  7241. length = num_cur_tch * si->desc.tch_record_size;
  7242. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7243. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7244. return 0;
  7245. }
  7246. /*******************************************************************************
  7247. * FUNCTION: move_hid_pen_data
  7248. *
  7249. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7250. * information structure, xy_mode and xy_data.
  7251. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7252. * the tthe_tuner sysfs node under the label "HID" starting with the
  7253. * report ID.
  7254. *
  7255. * RETURN:
  7256. * 0 = success
  7257. * !0 = failure
  7258. *
  7259. * PARAMETERS:
  7260. * *cd - pointer to core data
  7261. * *si - pointer to the system information structure
  7262. ******************************************************************************/
  7263. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7264. {
  7265. #ifdef TTHE_TUNER_SUPPORT
  7266. int size = get_unaligned_le16(&cd->input_buf[0]);
  7267. if (size) {
  7268. /*
  7269. * HID over USB does not require the two byte length field, so
  7270. * this should print from input_buf[2] but to keep both finger
  7271. * and pen reports the same the length is included
  7272. */
  7273. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7274. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7275. "HID-USB=");
  7276. else
  7277. tthe_print(cd, &(cd->input_buf[0]), size,
  7278. "HID-I2C=");
  7279. }
  7280. #endif
  7281. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7282. return 0;
  7283. }
  7284. /*******************************************************************************
  7285. * FUNCTION: parse_touch_input
  7286. *
  7287. * SUMMARY: Parse the touch report and take action based on the touch
  7288. * report_id.
  7289. *
  7290. * RETURN:
  7291. * 0 = success
  7292. * !0 = failure
  7293. *
  7294. * PARAMETERS:
  7295. * *cd - pointer to core data
  7296. * size - size of touch record
  7297. ******************************************************************************/
  7298. static int parse_touch_input(struct pt_core_data *cd, int size)
  7299. {
  7300. struct pt_sysinfo *si = &cd->sysinfo;
  7301. int report_id = cd->input_buf[2];
  7302. int rc = -EINVAL;
  7303. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7304. __func__);
  7305. if (!si->ready) {
  7306. pt_debug(cd->dev, DL_ERROR,
  7307. "%s: Need system information to parse touches\n",
  7308. __func__);
  7309. return 0;
  7310. }
  7311. if (!si->xy_mode || !si->xy_data)
  7312. return rc;
  7313. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7314. rc = move_touch_data(cd, si);
  7315. else if (report_id == PT_HID_PEN_REPORT_ID)
  7316. rc = move_hid_pen_data(cd, si);
  7317. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7318. rc = move_button_data(cd, si);
  7319. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7320. rc = move_sensor_data(cd, si);
  7321. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7322. rc = move_tracking_heatmap_data(cd, si);
  7323. if (rc)
  7324. return rc;
  7325. /* attention IRQ */
  7326. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7327. return 0;
  7328. }
  7329. /*******************************************************************************
  7330. * FUNCTION: parse_command_input
  7331. *
  7332. * SUMMARY: Move the response data from the input buffer to the response buffer
  7333. *
  7334. * RETURN:
  7335. * 0 = success
  7336. * !0 = failure
  7337. *
  7338. * PARAMETERS:
  7339. * *cd - pointer to core data
  7340. * size - size of response data
  7341. ******************************************************************************/
  7342. static int parse_command_input(struct pt_core_data *cd, int size)
  7343. {
  7344. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7345. __func__);
  7346. memcpy(cd->response_buf, cd->input_buf, size);
  7347. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7348. if (size && cd->show_tt_data) {
  7349. if (cd->pip2_prot_active)
  7350. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7351. else
  7352. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7353. }
  7354. #endif
  7355. mutex_lock(&cd->system_lock);
  7356. cd->hid_cmd_state = 0;
  7357. mutex_unlock(&cd->system_lock);
  7358. wake_up(&cd->wait_q);
  7359. return 0;
  7360. }
  7361. /*******************************************************************************
  7362. * FUNCTION: pt_allow_enumeration
  7363. *
  7364. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7365. * FW sentinel is seen.
  7366. *
  7367. * RETURN:
  7368. * true = allow enumeration or fully re-probe
  7369. * false = skip enumeration and fully re-probe
  7370. *
  7371. * PARAMETERS:
  7372. * *cd - pointer to core data
  7373. ******************************************************************************/
  7374. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7375. {
  7376. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7377. (!cd->hid_reset_cmd_state) &&
  7378. (cd->core_probe_complete) &&
  7379. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7380. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7381. (cd->mode == PT_MODE_OPERATIONAL)) {
  7382. return true;
  7383. }
  7384. if ((!cd->hid_reset_cmd_state) &&
  7385. (cd->core_probe_complete) &&
  7386. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7387. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7388. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7389. return true;
  7390. }
  7391. pt_debug(cd->dev, DL_INFO,
  7392. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7393. __func__,
  7394. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7395. "core_probe_complete(1)", cd->core_probe_complete,
  7396. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7397. "active_dut_gen(0,2)", cd->active_dut_generation);
  7398. return false;
  7399. }
  7400. /*******************************************************************************
  7401. * FUNCTION: pt_is_touch_report
  7402. *
  7403. * SUMMARY: Determine if a report ID should be treated as a touch report
  7404. *
  7405. * RETURN:
  7406. * true = report ID is a touch report
  7407. * false = report ID is not a touch report
  7408. *
  7409. * PARAMETERS:
  7410. * id - Report ID
  7411. ******************************************************************************/
  7412. static bool pt_is_touch_report(int id)
  7413. {
  7414. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7415. id == PT_HID_PEN_REPORT_ID ||
  7416. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7417. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7418. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7419. return true;
  7420. else
  7421. return false;
  7422. }
  7423. /*******************************************************************************
  7424. * FUNCTION: pt_parse_input
  7425. *
  7426. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7427. * on if its a response to a command or asynchronous touch data / reset
  7428. * sentinel. PIP2.x messages have additional error checking that is
  7429. * parsed (SEQ match from cmd to rsp, CRC valid).
  7430. * Look for special packets based on unique lengths:
  7431. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7432. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7433. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7434. * 0xFFXX - Empty buffer (PIP 1.7+)
  7435. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7436. * the initial probe has not yet been done.
  7437. *
  7438. * RETURN:
  7439. * 0 = success
  7440. * !0 = failure
  7441. *
  7442. * PARAMETERS:
  7443. * *cd - pointer to core data
  7444. ******************************************************************************/
  7445. static int pt_parse_input(struct pt_core_data *cd)
  7446. {
  7447. int report_id;
  7448. int cmd_id;
  7449. int is_command = 0;
  7450. int size;
  7451. int print_size;
  7452. bool touch_report = true;
  7453. unsigned short calc_crc;
  7454. unsigned short resp_crc;
  7455. cd->fw_sys_mode_in_standby_state = false;
  7456. size = get_unaligned_le16(&cd->input_buf[0]);
  7457. print_size = size;
  7458. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7459. __func__, print_size);
  7460. if (print_size <= PT_MAX_INPUT)
  7461. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7462. "<<< Read buf");
  7463. if (size == 0 ||
  7464. (size == 11 &&
  7465. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7466. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7467. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7468. PIP2_CMD_COMMAND_ID_MASK) ==
  7469. PIP2_CMD_ID_STATUS)) {
  7470. touch_report = false;
  7471. cd->hw_detected = true;
  7472. cd->bl_pip_ver_ready = false;
  7473. cd->app_pip_ver_ready = false;
  7474. if (size == 0) {
  7475. mutex_lock(&cd->system_lock);
  7476. cd->pip2_prot_active = false;
  7477. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7478. /*
  7479. * For Gen5/6 this sentinel could be from
  7480. * the BL or FW. Attempt to set the correct
  7481. * mode based on the previous PIP command.
  7482. */
  7483. if (cd->hid_cmd_state ==
  7484. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7485. cd->mode = PT_MODE_OPERATIONAL;
  7486. cd->startup_status =
  7487. STARTUP_STATUS_FW_RESET_SENTINEL;
  7488. } else if (cd->hid_cmd_state ==
  7489. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7490. cd->hid_reset_cmd_state) {
  7491. cd->mode = PT_MODE_BOOTLOADER;
  7492. cd->startup_status =
  7493. STARTUP_STATUS_BL_RESET_SENTINEL;
  7494. } else {
  7495. cd->mode = PT_MODE_UNKNOWN;
  7496. cd->startup_status =
  7497. STARTUP_STATUS_START;
  7498. }
  7499. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7500. pt_debug(cd->dev, DL_INFO,
  7501. "%s: ATM Gen5/6 %s sentinel received\n",
  7502. __func__,
  7503. (cd->mode == PT_MODE_OPERATIONAL ?
  7504. "FW" :
  7505. (cd->mode == PT_MODE_BOOTLOADER ?
  7506. "BL" : "Unknown")));
  7507. } else {
  7508. cd->mode = PT_MODE_OPERATIONAL;
  7509. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7510. cd->startup_status =
  7511. STARTUP_STATUS_FW_RESET_SENTINEL;
  7512. pt_debug(cd->dev, DL_INFO,
  7513. "%s: ATM PT/TT FW sentinel received\n",
  7514. __func__);
  7515. }
  7516. mutex_unlock(&cd->system_lock);
  7517. if (pt_allow_enumeration(cd)) {
  7518. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7519. pt_debug(cd->dev, DL_INFO,
  7520. "%s: Queue Restart\n", __func__);
  7521. pt_queue_restart(cd);
  7522. } else {
  7523. pt_debug(cd->dev, DL_INFO,
  7524. "%s: Queue Enum\n", __func__);
  7525. pt_queue_enum(cd);
  7526. }
  7527. } else {
  7528. pt_debug(cd->dev, DL_INFO,
  7529. "%s: Sentinel - No Queued Action\n",
  7530. __func__);
  7531. }
  7532. } else {
  7533. /* Sentinel must be from TT/TC BL */
  7534. mutex_lock(&cd->system_lock);
  7535. cd->pip2_prot_active = true;
  7536. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7537. cd->mode = PT_MODE_BOOTLOADER;
  7538. cd->sysinfo.ready = false;
  7539. mutex_unlock(&cd->system_lock);
  7540. pt_debug(cd->dev, DL_INFO,
  7541. "%s: BL Reset sentinel received\n", __func__);
  7542. if (cd->flashless_dut &&
  7543. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7544. pt_debug(cd->dev, DL_INFO,
  7545. "%s: BL to RAM for flashless DUT\n",
  7546. __func__);
  7547. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7548. }
  7549. }
  7550. mutex_lock(&cd->system_lock);
  7551. memcpy(cd->response_buf, cd->input_buf, 2);
  7552. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7553. mutex_unlock(&cd->system_lock);
  7554. pt_debug(cd->dev, DL_WARN,
  7555. "%s: Device Initiated Reset\n", __func__);
  7556. wake_up(&cd->wait_q);
  7557. return 0;
  7558. }
  7559. cd->hid_reset_cmd_state = 0;
  7560. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7561. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7562. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7563. cd->hid_cmd_state = 0;
  7564. wake_up(&cd->wait_q);
  7565. mutex_unlock(&cd->system_lock);
  7566. return 0;
  7567. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7568. /*
  7569. * This debug message below is used by PBATS to calculate the
  7570. * time from the last lift off IRQ to when FW exits LFT mode.
  7571. */
  7572. touch_report = false;
  7573. pt_debug(cd->dev, DL_WARN,
  7574. "%s: DUT - Empty buffer detected\n", __func__);
  7575. return 0;
  7576. } else if (size > PT_MAX_INPUT || size < 0) {
  7577. pt_debug(cd->dev, DL_ERROR,
  7578. "%s: DUT - Unexpected len field in active bus data!\n",
  7579. __func__);
  7580. return -EINVAL;
  7581. }
  7582. if (cd->pip2_prot_active) {
  7583. pt_debug(cd->dev, DL_DEBUG,
  7584. "%s: Decode PIP2.x Response\n", __func__);
  7585. /* PIP2 does not have a report id, hard code it */
  7586. report_id = 0x00;
  7587. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7588. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7589. resp_crc = cd->input_buf[size - 2] << 8;
  7590. resp_crc |= cd->input_buf[size - 1];
  7591. if ((cd->pip2_cmd_tag_seq !=
  7592. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7593. (resp_crc != calc_crc) &&
  7594. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7595. == PT_PIP_TOUCH_REPORT_ID) ||
  7596. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7597. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7598. pt_debug(cd->dev, DL_WARN,
  7599. "%s: %s %d %s\n",
  7600. __func__,
  7601. "Received PIP1 report id =",
  7602. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7603. "when expecting a PIP2 report - IGNORE report");
  7604. return 0;
  7605. }
  7606. is_command = 1;
  7607. touch_report = false;
  7608. } else {
  7609. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7610. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7611. }
  7612. #ifdef TTDL_DIAGNOSTICS
  7613. pt_debug(cd->dev, DL_DEBUG,
  7614. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7615. __func__, cd->pip2_prot_active, report_id,
  7616. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7617. #endif /* TTDL_DIAGNOSTICS */
  7618. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7619. pt_wakeup_host(cd);
  7620. #ifdef TTHE_TUNER_SUPPORT
  7621. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7622. #endif
  7623. return 0;
  7624. }
  7625. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7626. msecs_to_jiffies(cd->watchdog_interval));
  7627. /*
  7628. * If it is PIP2 response, the report_id has been set to 0,
  7629. * so it will not be parsed as a touch packet.
  7630. */
  7631. if (!pt_is_touch_report(report_id)) {
  7632. is_command = 1;
  7633. touch_report = false;
  7634. }
  7635. if (unlikely(is_command)) {
  7636. parse_command_input(cd, size);
  7637. return 0;
  7638. }
  7639. if (touch_report)
  7640. parse_touch_input(cd, size);
  7641. return 0;
  7642. }
  7643. /*******************************************************************************
  7644. * FUNCTION: pt_read_input
  7645. *
  7646. * SUMMARY: Reads incoming data off of the active bus
  7647. *
  7648. * RETURN:
  7649. * 0 = success
  7650. * !0 = failure
  7651. *
  7652. * PARAMETERS:
  7653. * *cd - pointer to core data
  7654. ******************************************************************************/
  7655. static int pt_read_input(struct pt_core_data *cd)
  7656. {
  7657. struct device *dev = cd->dev;
  7658. int rc = 0;
  7659. int t;
  7660. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7661. /*
  7662. * Workaround for easywake failure
  7663. * Interrupt for easywake, wait for bus controller to wake
  7664. */
  7665. mutex_lock(&cd->system_lock);
  7666. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7667. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7668. mutex_unlock(&cd->system_lock);
  7669. if (!dev->power.is_suspended)
  7670. goto read;
  7671. t = wait_event_timeout(cd->wait_q,
  7672. (cd->wait_until_wake == 1),
  7673. msecs_to_jiffies(2000));
  7674. #ifdef TTDL_DIAGNOSTICS
  7675. if (IS_TMO(t)) {
  7676. cd->bus_transmit_error_count++;
  7677. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7678. pt_debug(dev, DL_ERROR,
  7679. "%s: !!!I2C Transmission Error %d\n",
  7680. __func__,
  7681. cd->bus_transmit_error_count);
  7682. }
  7683. #else
  7684. if (IS_TMO(t))
  7685. pt_queue_enum(cd);
  7686. #endif /* TTDL_DIAGNOSTICS */
  7687. goto read;
  7688. }
  7689. }
  7690. mutex_unlock(&cd->system_lock);
  7691. read:
  7692. /* Try reading up to 'retry' times */
  7693. while (retry-- != 0) {
  7694. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7695. PT_MAX_INPUT);
  7696. if (!rc) {
  7697. pt_debug(dev, DL_DEBUG,
  7698. "%s: Read input successfully\n", __func__);
  7699. goto read_exit;
  7700. }
  7701. usleep_range(1000, 2000);
  7702. }
  7703. pt_debug(dev, DL_ERROR,
  7704. "%s: Error getting report, rc=%d\n", __func__, rc);
  7705. read_exit:
  7706. return rc;
  7707. }
  7708. /*******************************************************************************
  7709. * FUNCTION: pt_irq
  7710. *
  7711. * SUMMARY: Process all detected interrupts
  7712. *
  7713. * RETURN:
  7714. * IRQ_HANDLED - Finished processing the interrupt
  7715. *
  7716. * PARAMETERS:
  7717. * irq - IRQ number
  7718. * *handle - pointer to core data struct
  7719. ******************************************************************************/
  7720. irqreturn_t pt_irq(int irq, void *handle)
  7721. {
  7722. struct pt_core_data *cd = handle;
  7723. int rc = 0;
  7724. if (!pt_check_irq_asserted(cd))
  7725. return IRQ_HANDLED;
  7726. rc = pt_read_input(cd);
  7727. #ifdef TTDL_DIAGNOSTICS
  7728. cd->irq_count++;
  7729. /* Used to calculate T-Refresh */
  7730. if (cd->t_refresh_active) {
  7731. if (cd->t_refresh_count == 0) {
  7732. cd->t_refresh_time = jiffies;
  7733. cd->t_refresh_count++;
  7734. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7735. cd->t_refresh_count++;
  7736. } else {
  7737. cd->t_refresh_active = 0;
  7738. cd->t_refresh_time =
  7739. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7740. }
  7741. }
  7742. #endif /* TTDL_DIAGNOSTICS */
  7743. if (!rc)
  7744. pt_parse_input(cd);
  7745. return IRQ_HANDLED;
  7746. }
  7747. /*******************************************************************************
  7748. * FUNCTION: _pt_subscribe_attention
  7749. *
  7750. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7751. * to subscribe themselves into the TTDL attention list
  7752. *
  7753. * RETURN:
  7754. * 0 = success
  7755. * !0 = failure
  7756. *
  7757. * PARAMETERS:
  7758. * *dev - pointer to device structure
  7759. * type - attention type enum
  7760. * *id - ID of the module calling this function
  7761. * *func - callback function pointer to be called when notified
  7762. * mode - attention mode
  7763. ******************************************************************************/
  7764. int _pt_subscribe_attention(struct device *dev,
  7765. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7766. int mode)
  7767. {
  7768. struct pt_core_data *cd = dev_get_drvdata(dev);
  7769. struct atten_node *atten, *atten_new;
  7770. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7771. if (!atten_new)
  7772. return -ENOMEM;
  7773. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7774. dev_name(cd->dev));
  7775. spin_lock(&cd->spinlock);
  7776. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7777. if (atten->id == id && atten->mode == mode) {
  7778. spin_unlock(&cd->spinlock);
  7779. kfree(atten_new);
  7780. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7781. __func__,
  7782. "already subscribed attention",
  7783. dev, "mode", mode);
  7784. return 0;
  7785. }
  7786. }
  7787. atten_new->id = id;
  7788. atten_new->dev = dev;
  7789. atten_new->mode = mode;
  7790. atten_new->func = func;
  7791. list_add(&atten_new->node, &cd->atten_list[type]);
  7792. spin_unlock(&cd->spinlock);
  7793. return 0;
  7794. }
  7795. /*******************************************************************************
  7796. * FUNCTION: _pt_unsubscribe_attention
  7797. *
  7798. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7799. * to unsubscribe themselves from the TTDL attention list
  7800. *
  7801. * RETURN:
  7802. * 0 = success
  7803. * !0 = failure
  7804. *
  7805. * PARAMETERS:
  7806. * *dev - pointer to device structure
  7807. * type - attention type enum
  7808. * *id - ID of the module calling this function
  7809. * *func - function pointer
  7810. * mode - attention mode
  7811. ******************************************************************************/
  7812. int _pt_unsubscribe_attention(struct device *dev,
  7813. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7814. int mode)
  7815. {
  7816. struct pt_core_data *cd = dev_get_drvdata(dev);
  7817. struct atten_node *atten, *atten_n;
  7818. spin_lock(&cd->spinlock);
  7819. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7820. if (atten->id == id && atten->mode == mode) {
  7821. list_del(&atten->node);
  7822. spin_unlock(&cd->spinlock);
  7823. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7824. __func__,
  7825. "unsub for atten->dev", atten->dev,
  7826. "atten->mode", atten->mode);
  7827. kfree(atten);
  7828. return 0;
  7829. }
  7830. }
  7831. spin_unlock(&cd->spinlock);
  7832. return -ENODEV;
  7833. }
  7834. /*******************************************************************************
  7835. * FUNCTION: _pt_request_exclusive
  7836. *
  7837. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7838. * to request exclusive access
  7839. *
  7840. * RETURN:
  7841. * 0 = success
  7842. * !0 = failure
  7843. *
  7844. * PARAMETERS:
  7845. * *dev - pointer to device structure
  7846. * timeout_ms - timeout to wait for exclusive access
  7847. ******************************************************************************/
  7848. static int _pt_request_exclusive(struct device *dev,
  7849. int timeout_ms)
  7850. {
  7851. struct pt_core_data *cd = dev_get_drvdata(dev);
  7852. return request_exclusive(cd, (void *)dev, timeout_ms);
  7853. }
  7854. /*******************************************************************************
  7855. * FUNCTION: _pt_release_exclusive
  7856. *
  7857. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7858. * to release exclusive access
  7859. *
  7860. * RETURN:
  7861. * 0 = success
  7862. * !0 = failure
  7863. *
  7864. * PARAMETERS:
  7865. * *dev - pointer to device structure
  7866. ******************************************************************************/
  7867. static int _pt_release_exclusive(struct device *dev)
  7868. {
  7869. struct pt_core_data *cd = dev_get_drvdata(dev);
  7870. return release_exclusive(cd, (void *)dev);
  7871. }
  7872. /*******************************************************************************
  7873. * FUNCTION: _pt_request_reset
  7874. *
  7875. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7876. * to request the DUT to be reset. Function returns err if refused or
  7877. * timeout occurs (Note: core uses fixed timeout period).
  7878. *
  7879. * NOTE: Function blocks until ISR occurs.
  7880. *
  7881. * RETURN:
  7882. * 0 = success
  7883. * !0 = failure
  7884. *
  7885. * PARAMETERS:
  7886. * *dev - pointer to device structure
  7887. * protect - flag to call protected or non-protected
  7888. ******************************************************************************/
  7889. static int _pt_request_reset(struct device *dev, int protect)
  7890. {
  7891. struct pt_core_data *cd = dev_get_drvdata(dev);
  7892. int rc;
  7893. rc = pt_dut_reset(cd, protect);
  7894. if (rc < 0) {
  7895. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7896. __func__, rc);
  7897. }
  7898. return rc;
  7899. }
  7900. /*******************************************************************************
  7901. * FUNCTION: _pt_request_enum
  7902. *
  7903. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7904. * to request TTDL to queue an enum. This function will return err
  7905. * if refused; if no error then enum has completed and system is in
  7906. * normal operation mode.
  7907. *
  7908. * RETURN:
  7909. * 0 = success
  7910. * !0 = failure
  7911. *
  7912. * PARAMETERS:
  7913. * *dev - pointer to device structure
  7914. * wait - boolean to determine if to wait for startup event
  7915. ******************************************************************************/
  7916. static int _pt_request_enum(struct device *dev, bool wait)
  7917. {
  7918. struct pt_core_data *cd = dev_get_drvdata(dev);
  7919. pt_queue_enum(cd);
  7920. if (wait)
  7921. wait_event_timeout(cd->wait_q,
  7922. cd->startup_state == STARTUP_NONE,
  7923. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7924. return 0;
  7925. }
  7926. /*******************************************************************************
  7927. * FUNCTION: _pt_request_sysinfo
  7928. *
  7929. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7930. * to request the pointer to the system information structure. This
  7931. * function will return NULL if sysinfo has not been acquired from the
  7932. * DUT yet.
  7933. *
  7934. * RETURN: Pointer to the system information struct
  7935. *
  7936. * PARAMETERS:
  7937. * *dev - pointer to device structure
  7938. ******************************************************************************/
  7939. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7940. {
  7941. struct pt_core_data *cd = dev_get_drvdata(dev);
  7942. int rc = 0;
  7943. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7944. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7945. !cd->sysinfo.ready) {
  7946. rc = pt_hid_output_get_sysinfo_(cd);
  7947. if (rc) {
  7948. pt_debug(cd->dev, DL_ERROR,
  7949. "%s: Error getting sysinfo rc=%d\n",
  7950. __func__, rc);
  7951. }
  7952. }
  7953. if (cd->sysinfo.ready)
  7954. return &cd->sysinfo;
  7955. return NULL;
  7956. }
  7957. /*******************************************************************************
  7958. * FUNCTION: _pt_request_loader_pdata
  7959. *
  7960. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7961. * to request the pointer to the loader platform data
  7962. *
  7963. * RETURN: Pointer to the loader platform data struct
  7964. *
  7965. * PARAMETERS:
  7966. * *dev - pointer to device structure
  7967. ******************************************************************************/
  7968. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7969. struct device *dev)
  7970. {
  7971. struct pt_core_data *cd = dev_get_drvdata(dev);
  7972. return cd->pdata->loader_pdata;
  7973. }
  7974. /*******************************************************************************
  7975. * FUNCTION: _pt_request_start_wd
  7976. *
  7977. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7978. * to request to start the TTDL watchdog
  7979. *
  7980. * RETURN:
  7981. * 0 = success
  7982. * !0 = failure
  7983. *
  7984. * PARAMETERS:
  7985. * *dev - pointer to device structure
  7986. ******************************************************************************/
  7987. static int _pt_request_start_wd(struct device *dev)
  7988. {
  7989. struct pt_core_data *cd = dev_get_drvdata(dev);
  7990. pt_start_wd_timer(cd);
  7991. return 0;
  7992. }
  7993. /*******************************************************************************
  7994. * FUNCTION: _pt_request_stop_wd
  7995. *
  7996. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7997. * to request to stop the TTDL watchdog
  7998. *
  7999. * RETURN:
  8000. * 0 = success
  8001. * !0 = failure
  8002. *
  8003. * PARAMETERS:
  8004. * *dev - pointer to device structure
  8005. ******************************************************************************/
  8006. static int _pt_request_stop_wd(struct device *dev)
  8007. {
  8008. struct pt_core_data *cd = dev_get_drvdata(dev);
  8009. pt_stop_wd_timer(cd);
  8010. return 0;
  8011. }
  8012. /*******************************************************************************
  8013. * FUNCTION: pt_pip2_launch_app
  8014. *
  8015. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  8016. * the FW reset sentinel to indicate the function succeeded.
  8017. *
  8018. * NOTE: Calling this function when the DUT is in Application mode WILL result
  8019. * in a timeout delay and with the DUT being reset with an XRES.
  8020. *
  8021. * RETURN:
  8022. * 0 = success
  8023. * !0 = failure
  8024. *
  8025. * PARAMETERS:
  8026. * *cd - pointer to core data structure
  8027. * protect - flag to call protected or non-protected
  8028. ******************************************************************************/
  8029. static int pt_pip2_launch_app(struct device *dev, int protect)
  8030. {
  8031. struct pt_core_data *cd = dev_get_drvdata(dev);
  8032. u16 actual_read_len;
  8033. u16 tmp_startup_status = cd->startup_status;
  8034. u8 read_buf[12];
  8035. u8 status;
  8036. int time = 0;
  8037. int rc = 0;
  8038. mutex_lock(&cd->system_lock);
  8039. cd->startup_status = STARTUP_STATUS_START;
  8040. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8041. mutex_unlock(&cd->system_lock);
  8042. rc = _pt_request_pip2_send_cmd(dev, protect,
  8043. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8044. &actual_read_len);
  8045. if (rc) {
  8046. pt_debug(dev, DL_ERROR,
  8047. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8048. __func__, rc);
  8049. } else {
  8050. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8051. /* Test for no or invalid image in FLASH, no point to reset */
  8052. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8053. rc = status;
  8054. goto exit;
  8055. }
  8056. /* Any other boot failure */
  8057. if (status != 0) {
  8058. pt_debug(dev, DL_ERROR,
  8059. "%s: FW did not EXECUTE, status = %d\n",
  8060. __func__, status);
  8061. rc = status;
  8062. }
  8063. }
  8064. if (rc) {
  8065. pt_debug(dev, DL_ERROR,
  8066. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8067. __func__, rc);
  8068. goto exit;
  8069. }
  8070. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8071. msleep(20);
  8072. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8073. __func__, time, cd->startup_status);
  8074. time += 20;
  8075. }
  8076. if (cd->startup_status == STARTUP_STATUS_START) {
  8077. pt_debug(cd->dev, DL_WARN,
  8078. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8079. rc = -ETIME;
  8080. }
  8081. exit:
  8082. if (cd->startup_status == STARTUP_STATUS_START) {
  8083. /* Reset to original state because we could be stuck in BL */
  8084. mutex_lock(&cd->system_lock);
  8085. cd->startup_status = tmp_startup_status;
  8086. mutex_unlock(&cd->system_lock);
  8087. }
  8088. return rc;
  8089. }
  8090. /*******************************************************************************
  8091. * FUNCTION: _pt_request_pip2_launch_app
  8092. *
  8093. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8094. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8095. * consumed.
  8096. *
  8097. * RETURN:
  8098. * 0 = success
  8099. * !0 = failure
  8100. *
  8101. * PARAMETERS:
  8102. * *cd - pointer to core data structure
  8103. ******************************************************************************/
  8104. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8105. {
  8106. return pt_pip2_launch_app(dev, protect);
  8107. }
  8108. /*******************************************************************************
  8109. * FUNCTION: _pt_request_wait_for_enum_state
  8110. *
  8111. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8112. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8113. *
  8114. * RETURN:
  8115. * 0 = success
  8116. * !0 = failure
  8117. *
  8118. * PARAMETERS:
  8119. * *cd - pointer to core data structure
  8120. * timeout - timeout for how long to wait
  8121. * state - enum state to wait for
  8122. ******************************************************************************/
  8123. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8124. int state)
  8125. {
  8126. int rc = 0;
  8127. int t;
  8128. struct pt_core_data *cd = dev_get_drvdata(dev);
  8129. t = wait_event_timeout(cd->wait_q,
  8130. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8131. msecs_to_jiffies(timeout));
  8132. if (IS_TMO(t)) {
  8133. pt_debug(cd->dev, DL_ERROR,
  8134. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8135. __func__, state, timeout);
  8136. pt_debug(cd->dev, DL_WARN,
  8137. "%s: enum state reached 0x%04X\n",
  8138. __func__, cd->startup_status);
  8139. rc = -ETIME;
  8140. } else if (cd->startup_status & state) {
  8141. pt_debug(cd->dev, DL_INFO,
  8142. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8143. __func__, cd->startup_status,
  8144. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8145. } else {
  8146. if (t == 1) {
  8147. pt_debug(
  8148. cd->dev, DL_ERROR,
  8149. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8150. __func__, state, timeout);
  8151. rc = -ETIME;
  8152. } else {
  8153. pt_debug(
  8154. cd->dev, DL_ERROR,
  8155. "%s: Enum state 0x%04X not reached in %dms\n",
  8156. __func__, state, timeout - jiffies_to_msecs(t));
  8157. rc = -EINVAL;
  8158. }
  8159. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8160. __func__, cd->startup_status);
  8161. }
  8162. return rc;
  8163. }
  8164. /*******************************************************************************
  8165. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8166. *
  8167. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8168. * deep sleep.
  8169. *
  8170. * RETURN:
  8171. * 0 = success
  8172. * !0 = error
  8173. *
  8174. * PARAMETERS:
  8175. * *cd - pointer to core data
  8176. ******************************************************************************/
  8177. static int pt_core_wake_device_from_deep_sleep_(
  8178. struct pt_core_data *cd)
  8179. {
  8180. int rc;
  8181. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8182. if (rc)
  8183. rc = -EAGAIN;
  8184. /* Prevent failure on sequential wake/sleep requests from OS */
  8185. msleep(20);
  8186. return rc;
  8187. }
  8188. /*******************************************************************************
  8189. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8190. *
  8191. * SUMMARY: Wake up device from Easy-Wake state.
  8192. *
  8193. * RETURN:
  8194. * 0 = success
  8195. * !0 = failure
  8196. *
  8197. * PARAMETERS:
  8198. * *cd - pointer to core data
  8199. ******************************************************************************/
  8200. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8201. {
  8202. int rc = 0;
  8203. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8204. return rc;
  8205. }
  8206. /*******************************************************************************
  8207. * FUNCTION: pt_restore_parameters_
  8208. *
  8209. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8210. * back to the DUT
  8211. *
  8212. * RETURN:
  8213. * 0 = success
  8214. * !0 = failure
  8215. *
  8216. * PARAMETERS:
  8217. * *cd - pointer the core data structure
  8218. ******************************************************************************/
  8219. static int pt_restore_parameters_(struct pt_core_data *cd)
  8220. {
  8221. struct param_node *param;
  8222. int rc = 0;
  8223. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8224. goto exit;
  8225. spin_lock(&cd->spinlock);
  8226. list_for_each_entry(param, &cd->param_list, node) {
  8227. spin_unlock(&cd->spinlock);
  8228. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8229. __func__, param->id, param->value);
  8230. rc = pt_pip_set_param_(cd, param->id,
  8231. param->value, param->size);
  8232. if (rc)
  8233. goto exit;
  8234. spin_lock(&cd->spinlock);
  8235. }
  8236. spin_unlock(&cd->spinlock);
  8237. exit:
  8238. return rc;
  8239. }
  8240. /*******************************************************************************
  8241. * FUNCTION: pt_pip2_exit_bl_
  8242. *
  8243. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8244. * a DUT that may not have flash and will need FW to be loaded into RAM
  8245. *
  8246. * RETURN:
  8247. * 0 = success
  8248. * !0 = failure
  8249. *
  8250. * PARAMETERS:
  8251. * *cd - pointer the core data structure
  8252. * *status_str - pointer to optional status string buffer
  8253. * buf_size - size of status_str buffer
  8254. ******************************************************************************/
  8255. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8256. {
  8257. int rc;
  8258. int wait_time = 0;
  8259. u8 mode = PT_MODE_UNKNOWN;
  8260. bool load_status_str = false;
  8261. /*
  8262. * Below function has protective call to ensure no enum is still on
  8263. * going, while this kind of protection should be applied widely in
  8264. * future (TODO).
  8265. */
  8266. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8267. if (status_str && buf_size <= 50)
  8268. load_status_str = true;
  8269. if (mode == PT_MODE_BOOTLOADER) {
  8270. if (cd->flashless_dut == 1) {
  8271. rc = pt_hw_hard_reset(cd);
  8272. } else {
  8273. rc = pt_pip2_launch_app(cd->dev,
  8274. PT_CORE_CMD_UNPROTECTED);
  8275. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8276. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8277. __func__, "Invalid image in FLASH rc", rc);
  8278. } else if (rc) {
  8279. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8280. __func__, "Failed to launch app rc", rc);
  8281. }
  8282. }
  8283. if (!rc) {
  8284. if (cd->flashless_dut == 1) {
  8285. /* Wait for BL to complete before enum */
  8286. rc = _pt_request_wait_for_enum_state(cd->dev,
  8287. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8288. if (rc && load_status_str) {
  8289. strlcpy(status_str, "No FW sentinel after BL",
  8290. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8291. goto exit;
  8292. }
  8293. }
  8294. /*
  8295. * If the host wants to interact with the FW or do a
  8296. * forced calibration, the FW must be out of BOOT mode
  8297. * and the system information must have been retrieved.
  8298. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8299. * If, however, the enumeration does not reach this
  8300. * point, the DUT may still be in APP mode so test
  8301. * for all conditions.
  8302. */
  8303. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8304. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8305. if (!rc || cd->startup_status >=
  8306. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8307. mutex_lock(&cd->system_lock);
  8308. cd->mode = PT_MODE_OPERATIONAL;
  8309. mutex_unlock(&cd->system_lock);
  8310. }
  8311. if (rc) {
  8312. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8313. __func__, "Failed to enum with DUT",
  8314. cd->startup_status);
  8315. if (load_status_str && !(cd->startup_status &
  8316. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8317. strlcpy(status_str, "FW Stuck in Boot mode",
  8318. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8319. goto exit;
  8320. }
  8321. }
  8322. /*
  8323. * The coming FW sentinel could wake up the event
  8324. * queue, which has chance to be taken by next command
  8325. * wrongly. Following delay is a workaround to avoid
  8326. * this issue for most situations.
  8327. */
  8328. msleep(20);
  8329. pt_start_wd_timer(cd);
  8330. }
  8331. if (load_status_str) {
  8332. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8333. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8334. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8335. else if (!rc)
  8336. strlcpy(status_str, "Entered APP from BL mode",
  8337. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8338. else
  8339. strlcpy(status_str, "Failed to enter APP from BL mode",
  8340. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8341. }
  8342. } else if (mode == PT_MODE_OPERATIONAL) {
  8343. mutex_lock(&cd->system_lock);
  8344. cd->mode = mode;
  8345. mutex_unlock(&cd->system_lock);
  8346. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8347. if (load_status_str) {
  8348. if (!rc)
  8349. strlcpy(status_str, "Already in APP mode",
  8350. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8351. else
  8352. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8353. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8354. }
  8355. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8356. mutex_lock(&cd->system_lock);
  8357. cd->mode = mode;
  8358. mutex_unlock(&cd->system_lock);
  8359. if (load_status_str)
  8360. strlcpy(status_str, "Failed to determine active mode",
  8361. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8362. }
  8363. exit:
  8364. if (!rc)
  8365. pt_start_wd_timer(cd);
  8366. return rc;
  8367. }
  8368. /*******************************************************************************
  8369. * FUNCTION: pt_pip2_exit_bl
  8370. *
  8371. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8372. * access.
  8373. *
  8374. * RETURN:
  8375. * 0 = success
  8376. * !0 = failure
  8377. *
  8378. * PARAMETERS:
  8379. * *cd - pointer the core data structure
  8380. * *status_str - pointer to optional status string buffer
  8381. * buf_size - size of status_str buffer
  8382. ******************************************************************************/
  8383. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8384. {
  8385. int rc;
  8386. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8387. if (rc < 0) {
  8388. pt_debug(cd->dev, DL_ERROR,
  8389. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8390. cd->exclusive_dev, cd->dev);
  8391. return rc;
  8392. }
  8393. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8394. if (release_exclusive(cd, cd->dev) < 0)
  8395. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8396. __func__);
  8397. return rc;
  8398. }
  8399. /*******************************************************************************
  8400. * FUNCTION: _fast_startup
  8401. *
  8402. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8403. *
  8404. * RETURN:
  8405. * 0 = success
  8406. * !0 = failure
  8407. *
  8408. * PARAMETERS:
  8409. * *cd - pointer the core data structure
  8410. ******************************************************************************/
  8411. static int _fast_startup(struct pt_core_data *cd)
  8412. {
  8413. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8414. int rc = 0;
  8415. u8 mode = PT_MODE_UNKNOWN;
  8416. struct pt_hid_desc hid_desc;
  8417. int wait_time = 0;
  8418. memset(&hid_desc, 0, sizeof(hid_desc));
  8419. reset:
  8420. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8421. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8422. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8423. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8424. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8425. __func__);
  8426. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8427. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8428. if (rc < 0) {
  8429. pt_debug(cd->dev, DL_ERROR,
  8430. "%s: Error on getting HID descriptor r=%d\n",
  8431. __func__, rc);
  8432. if (retry--)
  8433. goto reset;
  8434. goto exit;
  8435. }
  8436. cd->mode = pt_get_mode(cd, &hid_desc);
  8437. if (cd->mode == PT_MODE_BOOTLOADER) {
  8438. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8439. __func__);
  8440. rc = pt_hid_output_bl_launch_app_(cd);
  8441. if (rc < 0) {
  8442. pt_debug(cd->dev, DL_ERROR,
  8443. "%s: Error on launch app r=%d\n",
  8444. __func__, rc);
  8445. if (retry--)
  8446. goto reset;
  8447. goto exit;
  8448. }
  8449. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8450. if (rc < 0) {
  8451. pt_debug(cd->dev, DL_ERROR,
  8452. "%s: Error on getting HID descriptor r=%d\n",
  8453. __func__, rc);
  8454. if (retry--)
  8455. goto reset;
  8456. goto exit;
  8457. }
  8458. cd->mode = pt_get_mode(cd, &hid_desc);
  8459. if (cd->mode == PT_MODE_BOOTLOADER) {
  8460. if (retry--)
  8461. goto reset;
  8462. goto exit;
  8463. }
  8464. }
  8465. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8466. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8467. } else {
  8468. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8469. __func__);
  8470. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8471. /* Wait for any sentinel before first try */
  8472. rc = _pt_request_wait_for_enum_state(
  8473. cd->dev, 150,
  8474. STARTUP_STATUS_BL_RESET_SENTINEL |
  8475. STARTUP_STATUS_FW_RESET_SENTINEL);
  8476. if (rc)
  8477. pt_debug(cd->dev, DL_ERROR,
  8478. "%s: No Sentinel detected rc = %d\n",
  8479. __func__, rc);
  8480. } else
  8481. pt_flush_bus_if_irq_asserted(cd,
  8482. PT_FLUSH_BUS_BASED_ON_LEN);
  8483. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8484. if (rc) {
  8485. pt_debug(cd->dev, DL_ERROR,
  8486. "%s: Get mode failed, mode unknown\n",
  8487. __func__);
  8488. mode = PT_MODE_UNKNOWN;
  8489. }
  8490. cd->mode = mode;
  8491. if (cd->mode == PT_MODE_BOOTLOADER) {
  8492. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8493. __func__);
  8494. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8495. if (rc) {
  8496. pt_debug(cd->dev, DL_ERROR,
  8497. "%s Failed to exit bootloader\n",
  8498. __func__);
  8499. msleep(50);
  8500. rc = -ENODEV;
  8501. if (retry--)
  8502. goto reset;
  8503. goto exit;
  8504. } else {
  8505. pt_debug(cd->dev, DL_INFO,
  8506. "%s: Exit bootloader successfully\n",
  8507. __func__);
  8508. }
  8509. if (cd->mode != PT_MODE_OPERATIONAL) {
  8510. pt_debug(cd->dev, DL_WARN,
  8511. "%s: restore mode failure mode = %d\n",
  8512. __func__, cd->mode);
  8513. if (retry--)
  8514. goto reset;
  8515. goto exit;
  8516. }
  8517. }
  8518. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8519. }
  8520. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8521. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8522. if (!rc) {
  8523. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8524. pt_debug(cd->dev, DL_WARN,
  8525. "%s: Exit FW BOOT Mode after %dms\n",
  8526. __func__, wait_time);
  8527. } else {
  8528. pt_debug(cd->dev, DL_WARN,
  8529. "%s: FW stuck in BOOT Mode after %dms\n",
  8530. __func__, wait_time);
  8531. goto exit;
  8532. }
  8533. if (!cd->sysinfo.ready) {
  8534. rc = pt_hid_output_get_sysinfo_(cd);
  8535. if (rc) {
  8536. pt_debug(cd->dev, DL_ERROR,
  8537. "%s: Error on getting sysinfo r=%d\n",
  8538. __func__, rc);
  8539. if (retry--)
  8540. goto reset;
  8541. goto exit;
  8542. }
  8543. }
  8544. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8545. rc = pt_restore_parameters_(cd);
  8546. if (rc)
  8547. pt_debug(cd->dev, DL_ERROR,
  8548. "%s: failed to restore parameters rc=%d\n",
  8549. __func__, rc);
  8550. else
  8551. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8552. exit:
  8553. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8554. return rc;
  8555. }
  8556. /*******************************************************************************
  8557. * FUNCTION: pt_core_poweron_device_
  8558. *
  8559. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8560. *
  8561. * RETURN:
  8562. * 0 = success
  8563. * !0 = failure
  8564. *
  8565. * PARAMETERS:
  8566. * *cd - pointer to core data
  8567. ******************************************************************************/
  8568. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8569. {
  8570. struct device *dev = cd->dev;
  8571. int rc = 0;
  8572. /*
  8573. * After power on action, the chip can general FW sentinel. It can
  8574. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8575. * _fast_startup() can perform a quick enumeration too, here doesn't
  8576. * need another enumeration.
  8577. */
  8578. mutex_lock(&cd->system_lock);
  8579. cd->startup_status = STARTUP_STATUS_START;
  8580. cd->hid_reset_cmd_state = 1;
  8581. mutex_unlock(&cd->system_lock);
  8582. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8583. if (rc < 0) {
  8584. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8585. __func__, rc);
  8586. goto exit;
  8587. }
  8588. if (!cd->irq_enabled) {
  8589. cd->irq_enabled = true;
  8590. enable_irq(cd->irq);
  8591. }
  8592. /* TBD: following function doesn't update startup_status */
  8593. rc = _fast_startup(cd);
  8594. exit:
  8595. return rc;
  8596. }
  8597. /*******************************************************************************
  8598. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8599. *
  8600. * SUMMARY: Reset device, and then trigger a full enumeration.
  8601. *
  8602. * RETURN:
  8603. * 0 = success
  8604. * !0 = failure
  8605. *
  8606. * PARAMETERS:
  8607. * *cd - pointer to core data
  8608. ******************************************************************************/
  8609. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8610. {
  8611. int rc;
  8612. rc = pt_dut_reset_and_wait(cd);
  8613. if (rc < 0) {
  8614. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8615. __func__, rc);
  8616. goto exit;
  8617. }
  8618. rc = _fast_startup(cd);
  8619. exit:
  8620. return rc;
  8621. }
  8622. /*******************************************************************************
  8623. * FUNCTION: pt_core_easywake_off_
  8624. *
  8625. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8626. * the configuration in the core platform data structure.
  8627. *
  8628. * RETURN:
  8629. * 0 = success
  8630. * !0 = failure
  8631. *
  8632. * PARAMETERS:
  8633. * *cd - pointer to core data
  8634. ******************************************************************************/
  8635. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8636. {
  8637. int rc = 0;
  8638. mutex_lock(&cd->system_lock);
  8639. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8640. mutex_unlock(&cd->system_lock);
  8641. pt_debug(cd->dev, DL_INFO,
  8642. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8643. return 0;
  8644. }
  8645. mutex_unlock(&cd->system_lock);
  8646. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8647. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8648. rc = pt_core_wake_device_from_easy_wake_(cd);
  8649. if (rc < 0)
  8650. pt_debug(cd->dev, DL_ERROR,
  8651. "%s - %d failed %d\n", __func__, rc);
  8652. }
  8653. mutex_lock(&cd->system_lock);
  8654. cd->sleep_state = SS_EASY_WAKING_OFF;
  8655. mutex_unlock(&cd->system_lock);
  8656. pt_start_wd_timer(cd);
  8657. return rc;
  8658. }
  8659. /*******************************************************************************
  8660. * FUNCTION: pt_core_easywake_off
  8661. *
  8662. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8663. *
  8664. * RETURN:
  8665. * 0 = success
  8666. * !0 = failure
  8667. *
  8668. * PARAMETERS:
  8669. * *cd - pointer to core data
  8670. ******************************************************************************/
  8671. static int pt_core_easywake_off(struct pt_core_data *cd)
  8672. {
  8673. int rc;
  8674. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8675. if (rc < 0) {
  8676. pt_debug(cd->dev, DL_ERROR,
  8677. "%s: fail get exclusive ex=%p own=%p\n",
  8678. __func__, cd->exclusive_dev, cd->dev);
  8679. return rc;
  8680. }
  8681. rc = pt_core_easywake_off_(cd);
  8682. if (release_exclusive(cd, cd->dev) < 0)
  8683. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8684. __func__);
  8685. else
  8686. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8687. __func__);
  8688. return rc;
  8689. }
  8690. /*******************************************************************************
  8691. * FUNCTION: pt_core_wake_
  8692. *
  8693. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8694. * the configuration in the core platform data structure.
  8695. *
  8696. * RETURN:
  8697. * 0 = success
  8698. * !0 = failure
  8699. *
  8700. * PARAMETERS:
  8701. * *cd - pointer to core data
  8702. ******************************************************************************/
  8703. static int pt_core_wake_(struct pt_core_data *cd)
  8704. {
  8705. int rc = 0;
  8706. mutex_lock(&cd->system_lock);
  8707. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8708. mutex_unlock(&cd->system_lock);
  8709. pt_debug(cd->dev, DL_INFO,
  8710. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8711. return 0;
  8712. } else {
  8713. cd->sleep_state = SS_WAKING;
  8714. }
  8715. mutex_unlock(&cd->system_lock);
  8716. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8717. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8718. pt_debug(cd->dev, DL_INFO,
  8719. "%s: Entering into poweron mode:\n", __func__);
  8720. rc = pt_core_poweron_device_(cd);
  8721. if (rc < 0)
  8722. pr_err("%s: Poweron error detected: rc=%d\n",
  8723. __func__, rc);
  8724. }
  8725. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8726. rc = pt_core_wake_device_from_deep_standby_(cd);
  8727. else /* Default action to exit DeepSleep */
  8728. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8729. }
  8730. mutex_lock(&cd->system_lock);
  8731. cd->sleep_state = SS_SLEEP_OFF;
  8732. mutex_unlock(&cd->system_lock);
  8733. pt_start_wd_timer(cd);
  8734. return rc;
  8735. }
  8736. /*******************************************************************************
  8737. * FUNCTION: pt_core_wake_
  8738. *
  8739. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8740. *
  8741. * RETURN:
  8742. * 0 = success
  8743. * !0 = failure
  8744. *
  8745. * PARAMETERS:
  8746. * *cd - pointer to core data
  8747. ******************************************************************************/
  8748. static int pt_core_wake(struct pt_core_data *cd)
  8749. {
  8750. int rc = 0;
  8751. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8752. if (rc < 0) {
  8753. pt_debug(cd->dev, DL_ERROR,
  8754. "%s: fail get exclusive ex=%p own=%p\n",
  8755. __func__, cd->exclusive_dev, cd->dev);
  8756. return rc;
  8757. }
  8758. rc = pt_core_wake_(cd);
  8759. if (release_exclusive(cd, cd->dev) < 0)
  8760. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8761. __func__);
  8762. else
  8763. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8764. __func__);
  8765. return rc;
  8766. }
  8767. /*******************************************************************************
  8768. * FUNCTION: pt_get_ic_crc_
  8769. *
  8770. * SUMMARY: This function retrieves the config block CRC
  8771. *
  8772. * NOTE: The post condition of calling this function will be that the DUT will
  8773. * be in SCANNINING mode if no failures occur
  8774. *
  8775. * RETURN:
  8776. * 0 = success
  8777. * !0 = failure
  8778. *
  8779. * PARAMETERS:
  8780. * *cd - pointer the core data structure
  8781. * ebid - enumerated block ID
  8782. ******************************************************************************/
  8783. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8784. {
  8785. struct pt_sysinfo *si = &cd->sysinfo;
  8786. int rc = 0;
  8787. u8 status;
  8788. u16 calculated_crc = 0;
  8789. u16 stored_crc = 0;
  8790. rc = pt_pip_suspend_scanning_(cd);
  8791. if (rc)
  8792. goto error;
  8793. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8794. &calculated_crc, &stored_crc);
  8795. if (rc)
  8796. goto exit;
  8797. if (status) {
  8798. rc = -EINVAL;
  8799. goto exit;
  8800. }
  8801. si->ttconfig.crc = stored_crc;
  8802. exit:
  8803. pt_pip_resume_scanning_(cd);
  8804. error:
  8805. pt_debug(cd->dev, DL_INFO,
  8806. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8807. __func__, ebid, calculated_crc, stored_crc, rc);
  8808. return rc;
  8809. }
  8810. /*******************************************************************************
  8811. * FUNCTION: pt_pip2_read_gpio
  8812. *
  8813. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8814. * bits into the passed in variable
  8815. *
  8816. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8817. *
  8818. * RETURN:
  8819. * 0 = success
  8820. * !0 = failure
  8821. *
  8822. * PARAMETERS:
  8823. * *dev - pointer to device structure
  8824. * *status - pointer to where the command response status is stored
  8825. * *gpio - pointer to device attributes structure
  8826. ******************************************************************************/
  8827. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8828. {
  8829. int rc = 0;
  8830. u16 actual_read_len;
  8831. u8 read_buf[12];
  8832. u8 tmp_status = 0;
  8833. u8 index = PIP2_RESP_STATUS_OFFSET;
  8834. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8835. rc = _pt_request_pip2_send_cmd(dev,
  8836. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8837. NULL, 0, read_buf, &actual_read_len);
  8838. if (rc) {
  8839. pt_debug(dev, DL_ERROR,
  8840. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8841. rc = -ECOMM;
  8842. } else {
  8843. tmp_status = read_buf[index];
  8844. }
  8845. if (status)
  8846. *status = tmp_status;
  8847. if (!rc && gpio && (tmp_status == 0)) {
  8848. *gpio = ((read_buf[index + 4] << 24) |
  8849. (read_buf[index + 3] << 16) |
  8850. (read_buf[index + 2] << 8) |
  8851. (read_buf[index + 1]));
  8852. }
  8853. return rc;
  8854. }
  8855. /*******************************************************************************
  8856. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8857. *
  8858. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8859. *
  8860. * RETURN:
  8861. * 0 = success
  8862. * !0 = failure
  8863. *
  8864. * PARAMETERS:
  8865. * *cd - pointer the core data structure
  8866. * *pid - pointer to store panel ID
  8867. ******************************************************************************/
  8868. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8869. {
  8870. u32 gpio_value = 0;
  8871. u8 status = 0;
  8872. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8873. int rc = 0;
  8874. if (!pid)
  8875. return -ENOMEM;
  8876. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8877. if (!rc) {
  8878. if (status == 0) {
  8879. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8880. PT_PANEL_ID_SHIFT;
  8881. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8882. __func__,
  8883. "BL mode PID", panel_id, "gpio", gpio_value);
  8884. *pid = panel_id;
  8885. } else {
  8886. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8887. __func__,
  8888. "BL read gpio failed status", status);
  8889. }
  8890. } else {
  8891. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8892. __func__,
  8893. "BL read gpio failed status", status);
  8894. }
  8895. return rc;
  8896. }
  8897. /*******************************************************************************
  8898. * FUNCTION: pt_enum_with_dut_
  8899. *
  8900. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8901. * The core data (cd) startup_status will store, as a bitmask, each
  8902. * state of the enumeration process. The startup will be attempted
  8903. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8904. *
  8905. * RETURN:
  8906. * 0 = success
  8907. * !0 = failure
  8908. *
  8909. * PARAMETERS:
  8910. * *cd - pointer the core data structure
  8911. * reset - Flag to reset the DUT before attempting to enumerate
  8912. * *status - poionter to store the enum status bitmask flags
  8913. ******************************************************************************/
  8914. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8915. u32 *enum_status)
  8916. {
  8917. int try = 1;
  8918. int rc = 0;
  8919. int wait_time = 0;
  8920. bool detected = false;
  8921. u8 return_data[8];
  8922. u8 mode = PT_MODE_UNKNOWN;
  8923. u8 pid = PANEL_ID_NOT_ENABLED;
  8924. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8925. struct pt_hid_desc hid_desc;
  8926. memset(&hid_desc, 0, sizeof(hid_desc));
  8927. #ifdef TTHE_TUNER_SUPPORT
  8928. tthe_print(cd, NULL, 0, "enter startup");
  8929. #endif
  8930. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8931. __func__, cd->startup_status, reset);
  8932. pt_stop_wd_timer(cd);
  8933. reset:
  8934. if (try > 1)
  8935. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8936. __func__, try);
  8937. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8938. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8939. pt_debug(cd->dev, DL_INFO,
  8940. "%s: PIP1 Enumeration start\n", __func__);
  8941. /* Only reset the DUT after the first try */
  8942. if (reset || (try > 1)) {
  8943. /*
  8944. * Reset hardware only for Legacy parts. Skip for TT/TC
  8945. * parts because if the FW image was loaded directly
  8946. * to SRAM issueing a reset ill wipe out what was just
  8947. * loaded.
  8948. */
  8949. rc = pt_dut_reset_and_wait(cd);
  8950. if (rc < 0) {
  8951. pt_debug(cd->dev, DL_ERROR,
  8952. "%s: Error on h/w reset r=%d\n",
  8953. __func__, rc);
  8954. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8955. goto reset;
  8956. goto exit;
  8957. }
  8958. /* sleep to allow FW to be launched if available */
  8959. msleep(120);
  8960. }
  8961. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8962. if (rc < 0) {
  8963. pt_debug(cd->dev, DL_ERROR,
  8964. "%s: Error getting HID Descriptor r=%d\n",
  8965. __func__, rc);
  8966. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8967. goto reset;
  8968. rc = -EIO;
  8969. goto exit;
  8970. }
  8971. detected = true;
  8972. cd->mode = pt_get_mode(cd, &hid_desc);
  8973. /*
  8974. * Most systems do not use an XY pin as the panel_id and so
  8975. * the BL is used to retrieve the panel_id, however for
  8976. * systems that do use an XY pin, the panel_id MUST be
  8977. * retrieved from the system information when running FW
  8978. * (done below) and so this section of code is skipped.
  8979. * Entering the BL here is only needed on XY_PIN systems.
  8980. */
  8981. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8982. if (cd->mode == PT_MODE_OPERATIONAL) {
  8983. rc = pt_pip_start_bootloader_(cd);
  8984. if (rc < 0) {
  8985. pt_debug(cd->dev, DL_ERROR,
  8986. "%s: Error on start bootloader r=%d\n",
  8987. __func__, rc);
  8988. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8989. goto reset;
  8990. goto exit;
  8991. }
  8992. cd->mode = PT_MODE_BOOTLOADER;
  8993. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8994. __func__);
  8995. }
  8996. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8997. if (!rc) {
  8998. cd->bl_info.ready = true;
  8999. cd->bl_info.chip_id =
  9000. get_unaligned_le16(&return_data[2]);
  9001. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  9002. __func__, cd->bl_info.chip_id);
  9003. } else {
  9004. pt_debug(cd->dev, DL_ERROR,
  9005. "%s: failed to get chip ID, r=%d\n",
  9006. __func__, rc);
  9007. }
  9008. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  9009. mutex_lock(&cd->system_lock);
  9010. if (!rc) {
  9011. cd->pid_for_loader = pid;
  9012. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  9013. __func__, cd->pid_for_loader);
  9014. } else {
  9015. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  9016. pt_debug(cd->dev, DL_WARN,
  9017. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9018. __func__, cd->pid_for_loader);
  9019. }
  9020. mutex_unlock(&cd->system_lock);
  9021. }
  9022. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9023. if (cd->mode == PT_MODE_BOOTLOADER) {
  9024. rc = pt_hid_output_bl_launch_app_(cd);
  9025. if (rc < 0) {
  9026. pt_debug(cd->dev, DL_ERROR,
  9027. "%s: Error on launch app r=%d\n",
  9028. __func__, rc);
  9029. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9030. goto reset;
  9031. rc = -ENODEV;
  9032. goto exit;
  9033. }
  9034. /* sleep to allow FW to be launched if available */
  9035. msleep(120);
  9036. /* Ensure the DUT is now in Application mode */
  9037. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9038. if (rc < 0) {
  9039. pt_debug(cd->dev, DL_ERROR,
  9040. "%s: Error getting HID Desc r=%d\n",
  9041. __func__, rc);
  9042. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9043. goto reset;
  9044. rc = -EIO;
  9045. goto exit;
  9046. }
  9047. cd->mode = pt_get_mode(cd, &hid_desc);
  9048. if (cd->mode == PT_MODE_BOOTLOADER) {
  9049. pt_debug(cd->dev, DL_WARN,
  9050. "%s: Error confiming exit BL\n",
  9051. __func__);
  9052. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9053. goto reset;
  9054. rc = -ENODEV;
  9055. goto exit;
  9056. }
  9057. }
  9058. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9059. cd->mode = PT_MODE_OPERATIONAL;
  9060. *enum_status |= STARTUP_STATUS_GET_DESC;
  9061. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9062. } else {
  9063. /* Generation is PIP2 Capable */
  9064. pt_debug(cd->dev, DL_INFO,
  9065. "%s: PIP2 Enumeration start\n", __func__);
  9066. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9067. if (rc) {
  9068. pt_debug(cd->dev, DL_ERROR,
  9069. "%s: Get mode failed, mode unknown\n",
  9070. __func__);
  9071. mode = PT_MODE_UNKNOWN;
  9072. } else
  9073. detected = true;
  9074. cd->mode = mode;
  9075. switch (cd->mode) {
  9076. case PT_MODE_OPERATIONAL:
  9077. pt_debug(cd->dev, DL_INFO,
  9078. "%s: Operational mode\n", __func__);
  9079. if (cd->app_pip_ver_ready == false) {
  9080. rc = pt_pip2_get_version_(cd);
  9081. if (!rc)
  9082. cd->app_pip_ver_ready = true;
  9083. else {
  9084. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9085. goto reset;
  9086. goto exit;
  9087. }
  9088. }
  9089. break;
  9090. case PT_MODE_BOOTLOADER:
  9091. pt_debug(cd->dev, DL_INFO,
  9092. "%s: Bootloader mode\n", __func__);
  9093. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9094. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9095. mutex_lock(&cd->system_lock);
  9096. if (!rc) {
  9097. cd->pid_for_loader = pid;
  9098. pt_debug(cd->dev, DL_INFO,
  9099. "%s: Panel ID: 0x%02X\n",
  9100. __func__, cd->pid_for_loader);
  9101. } else {
  9102. cd->pid_for_loader =
  9103. PANEL_ID_NOT_ENABLED;
  9104. pt_debug(cd->dev, DL_WARN,
  9105. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9106. __func__, cd->pid_for_loader);
  9107. }
  9108. mutex_unlock(&cd->system_lock);
  9109. }
  9110. if (cd->bl_pip_ver_ready == false) {
  9111. rc = pt_pip2_get_version_(cd);
  9112. if (!rc)
  9113. cd->bl_pip_ver_ready = true;
  9114. else {
  9115. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9116. goto reset;
  9117. goto exit;
  9118. }
  9119. }
  9120. /*
  9121. * Launch app command will fail in flashless mode.
  9122. * Skip launch app command here to save time for
  9123. * enumeration flow.
  9124. */
  9125. if (cd->flashless_dut)
  9126. goto exit;
  9127. /*
  9128. * pt_pip2_launch_app() is needed here instead of
  9129. * pt_pip2_exit_bl() because exit_bl will cause several
  9130. * hw_resets to occur and the auto BL on a flashless
  9131. * DUT will fail.
  9132. */
  9133. rc = pt_pip2_launch_app(cd->dev,
  9134. PT_CORE_CMD_UNPROTECTED);
  9135. if (rc) {
  9136. pt_debug(cd->dev, DL_ERROR,
  9137. "%s: Error on launch app r=%d\n",
  9138. __func__, rc);
  9139. msleep(50);
  9140. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9141. goto reset;
  9142. goto exit;
  9143. }
  9144. /*
  9145. * IRQ thread can be delayed if the serial log print is
  9146. * enabled. It causes next command to get wrong response
  9147. * Here the delay is to ensure pt_parse_input() to be
  9148. * finished.
  9149. */
  9150. msleep(60);
  9151. /* Check and update the mode */
  9152. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9153. if (rc) {
  9154. pt_debug(cd->dev, DL_ERROR,
  9155. "%s: Get mode failed, mode unknown\n",
  9156. __func__);
  9157. mode = PT_MODE_UNKNOWN;
  9158. }
  9159. cd->mode = mode;
  9160. if (cd->mode == PT_MODE_OPERATIONAL) {
  9161. pt_debug(cd->dev, DL_INFO,
  9162. "%s: Launched to Operational mode\n",
  9163. __func__);
  9164. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9165. pt_debug(cd->dev, DL_ERROR,
  9166. "%s: Launch failed, Bootloader mode\n",
  9167. __func__);
  9168. goto exit;
  9169. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9170. pt_debug(cd->dev, DL_ERROR,
  9171. "%s: Launch failed, Unknown mode\n",
  9172. __func__);
  9173. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9174. goto reset;
  9175. goto exit;
  9176. }
  9177. if (cd->app_pip_ver_ready == false) {
  9178. rc = pt_pip2_get_version_(cd);
  9179. if (!rc)
  9180. cd->app_pip_ver_ready = true;
  9181. else {
  9182. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9183. goto reset;
  9184. goto exit;
  9185. }
  9186. }
  9187. break;
  9188. default:
  9189. pt_debug(cd->dev, DL_ERROR,
  9190. "%s: Unknown mode\n", __func__);
  9191. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9192. goto reset;
  9193. goto exit;
  9194. }
  9195. *enum_status |= STARTUP_STATUS_GET_DESC;
  9196. }
  9197. pt_init_pip_report_fields(cd);
  9198. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9199. if (!cd->features.easywake)
  9200. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9201. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9202. rc = pt_hid_output_get_sysinfo_(cd);
  9203. if (rc) {
  9204. pt_debug(cd->dev, DL_ERROR,
  9205. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9206. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9207. goto reset;
  9208. goto exit;
  9209. }
  9210. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9211. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9212. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9213. if (!rc) {
  9214. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9215. pt_debug(cd->dev, DL_WARN,
  9216. "%s: Exit FW BOOT Mode after %dms\n",
  9217. __func__, wait_time);
  9218. } else {
  9219. pt_debug(cd->dev, DL_WARN,
  9220. "%s: FW stuck in BOOT Mode after %dms\n",
  9221. __func__, wait_time);
  9222. goto exit;
  9223. }
  9224. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9225. __func__,
  9226. cd->sysinfo.ttdata.pip_ver_major,
  9227. cd->sysinfo.ttdata.pip_ver_minor);
  9228. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9229. if (rc) {
  9230. pt_debug(cd->dev, DL_ERROR,
  9231. "%s: DUT Config block CRC failure rc=%d\n",
  9232. __func__, rc);
  9233. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9234. goto reset;
  9235. } else {
  9236. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9237. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9238. pt_debug(cd->dev, DL_ERROR,
  9239. "%s: scan state: %d, retry: %d\n",
  9240. __func__, sys_mode, try);
  9241. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9242. goto reset;
  9243. } else
  9244. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9245. }
  9246. rc = pt_restore_parameters_(cd);
  9247. if (rc) {
  9248. pt_debug(cd->dev, DL_ERROR,
  9249. "%s: Failed to restore parameters rc=%d\n",
  9250. __func__, rc);
  9251. } else
  9252. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9253. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9254. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9255. cd->startup_retry_count = 0;
  9256. exit:
  9257. /* Generate the HW Version of the connected DUT and store in cd */
  9258. pt_generate_hw_version(cd, cd->hw_version);
  9259. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9260. cd->hw_version);
  9261. pt_start_wd_timer(cd);
  9262. if (!detected)
  9263. rc = -ENODEV;
  9264. #ifdef TTHE_TUNER_SUPPORT
  9265. tthe_print(cd, NULL, 0, "exit startup");
  9266. #endif
  9267. pt_debug(cd->dev, DL_WARN,
  9268. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9269. __func__, rc, try);
  9270. return rc;
  9271. }
  9272. /*******************************************************************************
  9273. * FUNCTION: pt_enum_with_dut
  9274. *
  9275. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9276. * requesting exclusive access around the call.
  9277. *
  9278. * RETURN:
  9279. * 0 = success
  9280. * !0 = failure
  9281. *
  9282. * PARAMETERS:
  9283. * *cd - pointer the core data structure
  9284. * reset - Flag to reset the DUT before attempting to enumerate
  9285. * *status - pointer to store the enum status bitmask flags
  9286. ******************************************************************************/
  9287. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9288. {
  9289. int rc = 0;
  9290. mutex_lock(&cd->system_lock);
  9291. cd->startup_state = STARTUP_RUNNING;
  9292. mutex_unlock(&cd->system_lock);
  9293. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9294. if (rc) {
  9295. pt_debug(cd->dev, DL_ERROR,
  9296. "%s: fail get exclusive ex=%p own=%p\n",
  9297. __func__, cd->exclusive_dev, cd->dev);
  9298. goto exit;
  9299. }
  9300. rc = pt_enum_with_dut_(cd, reset, status);
  9301. if (release_exclusive(cd, cd->dev) < 0)
  9302. /* Don't return fail code, mode is already changed. */
  9303. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9304. __func__);
  9305. else
  9306. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9307. __func__);
  9308. exit:
  9309. mutex_lock(&cd->system_lock);
  9310. /* Clear startup state for any tasks waiting for startup completion */
  9311. cd->startup_state = STARTUP_NONE;
  9312. mutex_unlock(&cd->system_lock);
  9313. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9314. *status |= STARTUP_STATUS_COMPLETE;
  9315. /* Wake the waiters for end of startup */
  9316. wake_up(&cd->wait_q);
  9317. return rc;
  9318. }
  9319. static int add_sysfs_interfaces(struct device *dev);
  9320. static void remove_sysfs_interfaces(struct device *dev);
  9321. static void remove_sysfs_and_modules(struct device *dev);
  9322. static void pt_release_modules(struct pt_core_data *cd);
  9323. static void pt_probe_modules(struct pt_core_data *cd);
  9324. /*******************************************************************************
  9325. * FUNCTION: _pt_ttdl_restart
  9326. *
  9327. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9328. *
  9329. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9330. * existing sysfs nodes will produce an error.
  9331. *
  9332. * RETURN:
  9333. * 0 = success
  9334. * !0 = failure
  9335. *
  9336. * PARAMETERS:
  9337. * *dev - pointer to core device
  9338. ******************************************************************************/
  9339. static int _pt_ttdl_restart(struct device *dev)
  9340. {
  9341. int rc = 0;
  9342. struct pt_core_data *cd = dev_get_drvdata(dev);
  9343. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9344. struct i2c_client *client =
  9345. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9346. #endif
  9347. /*
  9348. * Make sure the device is awake, pt_mt_release function will
  9349. * cause pm sleep function and lead to deadlock.
  9350. */
  9351. pm_runtime_get_sync(dev);
  9352. /* Use ttdl_restart_lock to avoid reentry */
  9353. mutex_lock(&cd->ttdl_restart_lock);
  9354. remove_sysfs_and_modules(cd->dev);
  9355. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9356. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9357. pt_debug(dev, DL_ERROR,
  9358. "%s I2C functionality not Supported\n", __func__);
  9359. rc = -EIO;
  9360. goto ttdl_no_error;
  9361. }
  9362. #endif
  9363. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9364. rc = _pt_detect_dut_generation(cd->dev,
  9365. &cd->startup_status, &cd->active_dut_generation,
  9366. &cd->mode);
  9367. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9368. pt_debug(dev, DL_ERROR,
  9369. "%s: Error, Unknown DUT Generation rc=%d\n",
  9370. __func__, rc);
  9371. }
  9372. }
  9373. rc = add_sysfs_interfaces(cd->dev);
  9374. if (rc < 0)
  9375. pt_debug(cd->dev, DL_ERROR,
  9376. "%s: Error, failed sysfs nodes rc=%d\n",
  9377. __func__, rc);
  9378. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9379. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9380. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9381. if (rc < 0)
  9382. pt_debug(dev, DL_ERROR,
  9383. "%s: Error, Failed to Enumerate\n", __func__);
  9384. }
  9385. rc = pt_mt_probe(dev);
  9386. if (rc < 0) {
  9387. pt_debug(dev, DL_ERROR,
  9388. "%s: Error, fail mt probe\n", __func__);
  9389. }
  9390. rc = pt_btn_probe(dev);
  9391. if (rc < 0) {
  9392. pt_debug(dev, DL_ERROR,
  9393. "%s: Error, fail btn probe\n", __func__);
  9394. }
  9395. pt_probe_modules(cd);
  9396. pt_debug(cd->dev, DL_WARN,
  9397. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9398. rc = 0;
  9399. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9400. ttdl_no_error:
  9401. #endif
  9402. mutex_unlock(&cd->ttdl_restart_lock);
  9403. mutex_lock(&cd->system_lock);
  9404. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9405. cd->startup_state = STARTUP_NONE;
  9406. mutex_unlock(&cd->system_lock);
  9407. pm_runtime_put(dev);
  9408. return rc;
  9409. }
  9410. /*******************************************************************************
  9411. * FUNCTION: pt_restart_work_function
  9412. *
  9413. * SUMMARY: This is the wrapper function placed in a work queue to call
  9414. * _pt_ttdl_restart()
  9415. *
  9416. * RETURN: none
  9417. *
  9418. * PARAMETERS:
  9419. * *work - pointer to the work_struct
  9420. ******************************************************************************/
  9421. static void pt_restart_work_function(struct work_struct *work)
  9422. {
  9423. struct pt_core_data *cd = container_of(work,
  9424. struct pt_core_data, ttdl_restart_work);
  9425. int rc = 0;
  9426. rc = _pt_ttdl_restart(cd->dev);
  9427. if (rc < 0)
  9428. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9429. __func__, rc);
  9430. }
  9431. /*******************************************************************************
  9432. * FUNCTION: pt_enum_work_function
  9433. *
  9434. * SUMMARY: This is the wrapper function placed in a work queue to call
  9435. * pt_enum_with_dut()
  9436. *
  9437. * RETURN: none
  9438. *
  9439. * PARAMETERS:
  9440. * *work - pointer to the work_struct
  9441. ******************************************************************************/
  9442. static void pt_enum_work_function(struct work_struct *work)
  9443. {
  9444. struct pt_core_data *cd = container_of(work,
  9445. struct pt_core_data, enum_work);
  9446. int rc;
  9447. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9448. if (rc < 0)
  9449. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9450. __func__, rc);
  9451. }
  9452. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9453. {
  9454. int rc;
  9455. if (!get) {
  9456. rc = 0;
  9457. goto regulator_put;
  9458. }
  9459. cd->vdd = regulator_get(cd->dev, "vdd");
  9460. if (IS_ERR(cd->vdd)) {
  9461. rc = PTR_ERR(cd->vdd);
  9462. dev_err(cd->dev,
  9463. "Regulator get failed vdd rc=%d\n", rc);
  9464. goto regulator_put;
  9465. }
  9466. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9467. if (IS_ERR(cd->vcc_i2c)) {
  9468. rc = PTR_ERR(cd->vcc_i2c);
  9469. dev_err(cd->dev,
  9470. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9471. goto regulator_put;
  9472. }
  9473. return 0;
  9474. regulator_put:
  9475. if (cd->vdd) {
  9476. regulator_put(cd->vdd);
  9477. cd->vdd = NULL;
  9478. }
  9479. if (cd->vcc_i2c) {
  9480. regulator_put(cd->vcc_i2c);
  9481. cd->vcc_i2c = NULL;
  9482. }
  9483. return rc;
  9484. }
  9485. #ifdef ENABLE_I2C_REG_ONLY
  9486. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9487. {
  9488. int rc = 0;
  9489. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9490. if (!en) {
  9491. rc = 0;
  9492. goto disable_vcc_i2c_reg_only;
  9493. }
  9494. if (cd->vcc_i2c) {
  9495. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9496. if (rc < 0)
  9497. pt_debug(cd->dev, DL_INFO,
  9498. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9499. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9500. __func__, rc);
  9501. }
  9502. return 0;
  9503. disable_vcc_i2c_reg_only:
  9504. if (cd->vcc_i2c) {
  9505. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9506. if (rc < 0)
  9507. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9508. __func__, rc);
  9509. }
  9510. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9511. return rc;
  9512. }
  9513. #endif
  9514. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9515. {
  9516. int rc;
  9517. if (!en) {
  9518. rc = 0;
  9519. goto disable_vcc_i2c_reg;
  9520. }
  9521. if (cd->vdd) {
  9522. if (regulator_count_voltages(cd->vdd) > 0) {
  9523. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9524. FT_VTG_MAX_UV);
  9525. if (rc) {
  9526. dev_err(cd->dev,
  9527. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9528. goto exit;
  9529. }
  9530. }
  9531. rc = regulator_enable(cd->vdd);
  9532. if (rc) {
  9533. dev_err(cd->dev,
  9534. "Regulator vdd enable failed rc=%d\n", rc);
  9535. goto exit;
  9536. }
  9537. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9538. }
  9539. if (cd->vcc_i2c) {
  9540. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9541. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9542. FT_I2C_VTG_MAX_UV);
  9543. if (rc) {
  9544. dev_err(cd->dev,
  9545. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9546. goto disable_vdd_reg;
  9547. }
  9548. }
  9549. rc = regulator_enable(cd->vcc_i2c);
  9550. if (rc) {
  9551. dev_err(cd->dev,
  9552. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9553. goto disable_vdd_reg;
  9554. }
  9555. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9556. }
  9557. return 0;
  9558. disable_vcc_i2c_reg:
  9559. if (cd->vcc_i2c) {
  9560. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9561. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9562. FT_I2C_VTG_MAX_UV);
  9563. regulator_disable(cd->vcc_i2c);
  9564. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9565. }
  9566. disable_vdd_reg:
  9567. if (cd->vdd) {
  9568. if (regulator_count_voltages(cd->vdd) > 0)
  9569. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9570. FT_VTG_MAX_UV);
  9571. regulator_disable(cd->vdd);
  9572. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9573. }
  9574. exit:
  9575. return rc;
  9576. }
  9577. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9578. #define KERNEL_VER_GT_3_19
  9579. #endif
  9580. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9581. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9582. #if defined(CONFIG_PM_SLEEP)
  9583. /*******************************************************************************
  9584. * FUNCTION: pt_core_rt_suspend
  9585. *
  9586. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9587. *
  9588. * RETURN:
  9589. * 0 = success
  9590. * !0 = failure
  9591. *
  9592. * PARAMETERS:
  9593. * *dev - pointer to core device
  9594. ******************************************************************************/
  9595. static int pt_core_rt_suspend(struct device *dev)
  9596. {
  9597. struct pt_core_data *cd = dev_get_drvdata(dev);
  9598. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9599. __func__, cd->core_probe_complete);
  9600. return 0;
  9601. }
  9602. /*******************************************************************************
  9603. * FUNCTION: pt_core_rt_resume
  9604. *
  9605. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9606. *
  9607. * RETURN:
  9608. * 0 = success
  9609. * !0 = failure
  9610. *
  9611. * PARAMETERS:
  9612. * *dev - pointer to core device
  9613. ******************************************************************************/
  9614. static int pt_core_rt_resume(struct device *dev)
  9615. {
  9616. struct pt_core_data *cd = dev_get_drvdata(dev);
  9617. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9618. __func__, cd->core_probe_complete);
  9619. return 0;
  9620. }
  9621. #endif /* CONFIG_PM_SLEEP */
  9622. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9623. #if defined(CONFIG_PM_SLEEP)
  9624. /*******************************************************************************
  9625. * FUNCTION: pt_core_suspend_
  9626. *
  9627. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9628. * pt_core_sleep. This function may disable IRQ during sleep state.
  9629. *
  9630. * RETURN:
  9631. * 0 = success
  9632. * !0 = failure
  9633. *
  9634. * PARAMETERS:
  9635. * *dev - pointer to core device
  9636. ******************************************************************************/
  9637. static int pt_core_suspend_(struct device *dev)
  9638. {
  9639. int rc;
  9640. struct pt_core_data *cd = dev_get_drvdata(dev);
  9641. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9642. rc = pt_core_sleep(cd);
  9643. if (rc) {
  9644. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9645. __func__, rc);
  9646. return -EAGAIN;
  9647. }
  9648. rc = pt_enable_regulator(cd, false);
  9649. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9650. return 0;
  9651. /* Required to prevent interrupts before bus awake */
  9652. disable_irq(cd->irq);
  9653. cd->irq_disabled = 1;
  9654. if (device_may_wakeup(dev)) {
  9655. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9656. __func__);
  9657. if (!enable_irq_wake(cd->irq))
  9658. cd->irq_wake = 1;
  9659. } else {
  9660. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9661. __func__);
  9662. }
  9663. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9664. return rc;
  9665. }
  9666. /*******************************************************************************
  9667. * FUNCTION: pt_core_suspend
  9668. *
  9669. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9670. * woke up or put to sleep based on Kernel power state even when the display
  9671. * is off based on the check of TTDL core platform flag.
  9672. *
  9673. * RETURN:
  9674. * 0 = success
  9675. * !0 = failure
  9676. *
  9677. * PARAMETERS:
  9678. * *dev - pointer to core device
  9679. ******************************************************************************/
  9680. static int pt_core_suspend(struct device *dev)
  9681. {
  9682. struct pt_core_data *cd = dev_get_drvdata(dev);
  9683. int rc = 0;
  9684. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9685. return 0;
  9686. if (pt_core_state == STATE_SUSPEND)
  9687. {
  9688. pt_debug(cd->dev, DL_INFO, "%s Already in Suspend state\n", __func__);
  9689. return 0;
  9690. }
  9691. pt_debug(cd->dev, DL_INFO, "%s Suspend start\n", __func__);
  9692. cancel_work_sync(&cd->resume_work);
  9693. cancel_work_sync(&cd->suspend_work);
  9694. mutex_lock(&cd->system_lock);
  9695. cd->wait_until_wake = 0;
  9696. mutex_unlock(&cd->system_lock);
  9697. if (pm_suspend_via_firmware() || cd->touch_offload) {
  9698. rc = pt_core_suspend_(cd->dev);
  9699. cd->quick_boot = true;
  9700. } else {
  9701. rc = pt_enable_i2c_regulator(cd, false);
  9702. if (rc < 0)
  9703. pt_debug(cd->dev, DL_ERROR,
  9704. "%s: Error on disabling i2c regulator\n", __func__);
  9705. }
  9706. pt_debug(cd->dev, DL_INFO, "%s Suspend exit - rc = %d\n", __func__, rc);
  9707. return rc;
  9708. }
  9709. /*******************************************************************************
  9710. * FUNCTION: pt_core_resume_
  9711. *
  9712. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9713. * pt_core_wake. This function may enable IRQ before wake up.
  9714. *
  9715. * RETURN:
  9716. * 0 = success
  9717. * !0 = failure
  9718. *
  9719. * PARAMETERS:
  9720. * *dev - pointer to core device
  9721. ******************************************************************************/
  9722. static int pt_core_resume_(struct device *dev)
  9723. {
  9724. int rc = 0;
  9725. struct pt_core_data *cd = dev_get_drvdata(dev);
  9726. dev_info(dev, "%s: Entering into resume mode:\n",
  9727. __func__);
  9728. rc = pt_enable_regulator(cd, true);
  9729. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9730. __func__, rc);
  9731. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9732. goto exit;
  9733. /*
  9734. * Bus pm does not call suspend if device runtime suspended
  9735. * This flag is covers that case
  9736. */
  9737. if (cd->irq_disabled) {
  9738. enable_irq(cd->irq);
  9739. cd->irq_disabled = 0;
  9740. }
  9741. if (device_may_wakeup(dev)) {
  9742. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9743. __func__);
  9744. if (cd->irq_wake) {
  9745. disable_irq_wake(cd->irq);
  9746. cd->irq_wake = 0;
  9747. }
  9748. } else {
  9749. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9750. __func__);
  9751. }
  9752. exit:
  9753. rc = pt_core_wake(cd);
  9754. if (rc) {
  9755. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9756. __func__, rc);
  9757. return -EAGAIN;
  9758. }
  9759. return rc;
  9760. }
  9761. /*******************************************************************************
  9762. * FUNCTION: pt_core_restore
  9763. *
  9764. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9765. * pt_core_wake. This function may enable IRQ before wake up.
  9766. *
  9767. * RETURN:
  9768. * 0 = success
  9769. * !0 = failure
  9770. *
  9771. * PARAMETERS:
  9772. * *dev - pointer to core device
  9773. ******************************************************************************/
  9774. static int pt_core_restore(struct device *dev)
  9775. {
  9776. int rc = 0;
  9777. struct pt_core_data *cd = dev_get_drvdata(dev);
  9778. dev_info(dev, "%s: Entering into resume mode:\n",
  9779. __func__);
  9780. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9781. return rc;
  9782. }
  9783. /*******************************************************************************
  9784. * FUNCTION: suspend_offload_work
  9785. *
  9786. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9787. * along with kernel power state even the display is off based on the check of
  9788. * TTDL core platform flag.
  9789. *
  9790. * RETURN:
  9791. * 0 = success
  9792. * !0 = failure
  9793. *
  9794. * PARAMETERS:
  9795. * *dev - pointer to core device
  9796. ******************************************************************************/
  9797. static void pt_suspend_offload_work(struct work_struct *work)
  9798. {
  9799. int rc = 0;
  9800. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9801. suspend_offload_work);
  9802. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9803. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9804. return;
  9805. rc = pt_core_suspend_(cd->dev);
  9806. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9807. }
  9808. /*******************************************************************************
  9809. * FUNCTION: resume_offload_work
  9810. *
  9811. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9812. * along with kernel power state even the display is off based on the check of
  9813. * TTDL core platform flag.
  9814. *
  9815. * RETURN:
  9816. * 0 = success
  9817. * !0 = failure
  9818. *
  9819. * PARAMETERS:
  9820. * *dev - pointer to core device
  9821. ******************************************************************************/
  9822. static void pt_resume_offload_work(struct work_struct *work)
  9823. {
  9824. int rc = 0;
  9825. int retry_count = 1000;
  9826. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9827. resume_offload_work);
  9828. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9829. do {
  9830. retry_count--;
  9831. rc = pt_core_resume_(pt_data->dev);
  9832. if (rc < 0)
  9833. pt_debug(pt_data->dev, DL_ERROR,
  9834. "%s: Error on wake\n", __func__);
  9835. } while (retry_count && rc < 0);
  9836. if (rc < 0){
  9837. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9838. return;
  9839. }
  9840. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9841. rc = pt_core_easywake_on(pt_data);
  9842. if (rc < 0) {
  9843. pt_debug(pt_data->dev, DL_ERROR,
  9844. "%s: Error on enable touch to wake key\n",
  9845. __func__);
  9846. return;
  9847. }
  9848. pt_data->fb_state = FB_OFF;
  9849. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9850. #endif
  9851. pt_data->quick_boot = false;
  9852. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9853. }
  9854. /*******************************************************************************
  9855. * FUNCTION: pt_core_resume
  9856. *
  9857. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9858. * along with kernel power state even the display is off based on the check of
  9859. * TTDL core platform flag.
  9860. *
  9861. * RETURN:
  9862. * 0 = success
  9863. * !0 = failure
  9864. *
  9865. * PARAMETERS:
  9866. * *dev - pointer to core device
  9867. ******************************************************************************/
  9868. static int pt_core_resume(struct device *dev)
  9869. {
  9870. struct pt_core_data *cd = dev_get_drvdata(dev);
  9871. int rc = 0;
  9872. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9873. return 0;
  9874. if (pm_suspend_via_firmware() || cd->touch_offload) {
  9875. rc = pt_core_restore(cd->dev);
  9876. } else {
  9877. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9878. rc = pt_enable_i2c_regulator(cd, true);
  9879. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9880. }
  9881. mutex_lock(&cd->system_lock);
  9882. cd->wait_until_wake = 1;
  9883. mutex_unlock(&cd->system_lock);
  9884. wake_up(&cd->wait_q);
  9885. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9886. return rc;
  9887. }
  9888. #endif
  9889. #ifdef NEED_SUSPEND_NOTIFIER
  9890. /*******************************************************************************
  9891. * FUNCTION: pt_pm_notifier
  9892. *
  9893. * SUMMARY: This function is registered to notifier chain and will perform
  9894. * suspend operation if match event PM_SUSPEND_PREPARE.
  9895. *
  9896. * RETURN:
  9897. * 0 = success
  9898. * !0 = failure
  9899. *
  9900. * PARAMETERS:
  9901. * *nb - pointer to notifier_block structure
  9902. * action - notifier event type
  9903. * *data - void pointer
  9904. ******************************************************************************/
  9905. static int pt_pm_notifier(struct notifier_block *nb,
  9906. unsigned long action, void *data)
  9907. {
  9908. struct pt_core_data *cd = container_of(nb,
  9909. struct pt_core_data, pm_notifier);
  9910. if (action == PM_SUSPEND_PREPARE) {
  9911. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9912. __func__);
  9913. /*
  9914. * If PM runtime is not suspended, either call runtime
  9915. * PM suspend callback or wait until it finishes
  9916. */
  9917. if (!pm_runtime_suspended(cd->dev))
  9918. pm_runtime_suspend(cd->dev);
  9919. (void) pt_core_suspend(cd->dev);
  9920. }
  9921. return NOTIFY_DONE;
  9922. }
  9923. #endif
  9924. const struct dev_pm_ops pt_pm_ops = {
  9925. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9926. .freeze = pt_core_suspend,
  9927. .restore = pt_core_restore,
  9928. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9929. NULL)
  9930. };
  9931. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9932. /*******************************************************************************
  9933. * FUNCTION: _pt_request_pip2_enter_bl
  9934. *
  9935. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9936. * XRES pin or a soft reset.
  9937. *
  9938. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9939. * the WD active could cause this function to fail!
  9940. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9941. * will not try to determine the current mode but will proceed with
  9942. * resetting the DUT and entering the BL.
  9943. *
  9944. * NOTE: The definition of result code:
  9945. * PT_ENTER_BL_PASS (0)
  9946. * PT_ENTER_BL_ERROR (1)
  9947. * PT_ENTER_BL_RESET_FAIL (2)
  9948. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9949. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9950. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9951. *
  9952. * RETURNS:
  9953. * 0 = success
  9954. * !0 = failure
  9955. *
  9956. *
  9957. * PARAMETERS:
  9958. * *dev - pointer to device structure
  9959. * *start_mode - pointer to the mode the DUT was in when this function
  9960. * starts
  9961. * *result - pointer to store the result when to enter BL
  9962. ******************************************************************************/
  9963. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9964. {
  9965. int rc = 0;
  9966. int t;
  9967. int tmp_result = PT_ENTER_BL_ERROR;
  9968. int flash_info_retry = 2;
  9969. u8 mode = PT_MODE_UNKNOWN;
  9970. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9971. u8 read_buf[32];
  9972. u16 actual_read_len;
  9973. struct pt_core_data *cd = dev_get_drvdata(dev);
  9974. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9975. u8 time = 0;
  9976. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9977. if (cd->watchdog_enabled) {
  9978. pt_debug(dev, DL_WARN,
  9979. "%s: Watchdog must be stopped before entering BL\n",
  9980. __func__);
  9981. goto exit;
  9982. }
  9983. cancel_work_sync(&cd->enum_work);
  9984. cancel_work_sync(&cd->watchdog_work);
  9985. /* if undefined assume operational/test to bypass all checks */
  9986. if (*start_mode == PT_MODE_IGNORE) {
  9987. mode = PT_MODE_OPERATIONAL;
  9988. sys_mode = FW_SYS_MODE_TEST;
  9989. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9990. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9991. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9992. if (rc) {
  9993. pt_debug(dev, DL_ERROR,
  9994. "%s: Get mode failed, mode unknown\n",
  9995. __func__);
  9996. }
  9997. *start_mode = mode;
  9998. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9999. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  10000. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  10001. sys_mode = FW_SYS_MODE_SCANNING;
  10002. }
  10003. _retry:
  10004. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  10005. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  10006. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  10007. switch (mode) {
  10008. case PT_MODE_UNKNOWN:
  10009. /*
  10010. * When the mode could not be determined the DUT could be
  10011. * in App mode running corrupted FW or FW that is not
  10012. * responding to the mode request, assume no communication
  10013. * and do a hard reset
  10014. */
  10015. mutex_lock(&cd->system_lock);
  10016. cd->startup_status = STARTUP_STATUS_START;
  10017. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10018. mutex_unlock(&cd->system_lock);
  10019. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10020. if (rc) {
  10021. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10022. goto exit;
  10023. }
  10024. break;
  10025. case PT_MODE_OPERATIONAL:
  10026. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10027. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10028. __func__);
  10029. rc = pt_pip_suspend_scanning_(cd);
  10030. if (rc) {
  10031. /*
  10032. * Print to log but don't exit, the FW could be
  10033. * running but be hung or fail to respond to
  10034. * this request
  10035. */
  10036. pt_debug(dev, DL_ERROR,
  10037. "%s Suspend Scan Failed\n", __func__);
  10038. }
  10039. /* sleep to allow the suspend scan to be processed */
  10040. usleep_range(1000, 2000);
  10041. }
  10042. mutex_lock(&cd->system_lock);
  10043. cd->startup_status = STARTUP_STATUS_START;
  10044. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10045. mutex_unlock(&cd->system_lock);
  10046. /* Reset device to enter the BL */
  10047. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10048. if (rc) {
  10049. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10050. goto exit;
  10051. }
  10052. break;
  10053. case PT_MODE_BOOTLOADER:
  10054. /* Do nothing as we are already in the BL */
  10055. tmp_result = PT_ENTER_BL_PASS;
  10056. goto exit;
  10057. default:
  10058. /* Should NEVER get here */
  10059. tmp_result = PT_ENTER_BL_ERROR;
  10060. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10061. goto exit;
  10062. }
  10063. if (!cd->flashless_dut &&
  10064. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10065. /*
  10066. * Sending the special "Host Mode" command will instruct the
  10067. * BL to not execute the FW it has loaded into RAM.
  10068. * The command must be sent within a 40ms window from releasing
  10069. * the XRES pin. If the messages is sent too early it will NAK,
  10070. * so keep sending it every 2ms until it is accepted by the BL.
  10071. * A no-flash DUT does not require this command as there is no
  10072. * FW for the BL to load and execute.
  10073. */
  10074. usleep_range(4000, 6000);
  10075. pt_debug(cd->dev, DL_INFO,
  10076. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10077. __func__, (int)sizeof(host_mode_cmd));
  10078. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10079. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10080. rc = 1;
  10081. while (rc && time < 34) {
  10082. rc = pt_adap_write_read_specific(cd, 4,
  10083. host_mode_cmd, NULL);
  10084. usleep_range(1800, 2000);
  10085. time += 2;
  10086. }
  10087. /* Sleep to allow the BL to come up */
  10088. usleep_range(1000, 2000);
  10089. }
  10090. /*
  10091. * To avoid the case that next PIP command can be confused by BL/FW
  10092. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10093. * followed by "wakeup event" function can lower the failure rate.
  10094. */
  10095. t = wait_event_timeout(cd->wait_q,
  10096. ((cd->startup_status != STARTUP_STATUS_START)
  10097. && (cd->hid_reset_cmd_state == 0)),
  10098. msecs_to_jiffies(300));
  10099. if (IS_TMO(t)) {
  10100. pt_debug(cd->dev, DL_ERROR,
  10101. "%s: TMO waiting for BL sentinel\n", __func__);
  10102. }
  10103. /* Check if device is now in BL mode */
  10104. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10105. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10106. cd->startup_status);
  10107. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10108. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10109. mutex_lock(&cd->system_lock);
  10110. cd->pip2_prot_active = true;
  10111. cd->mode = PT_MODE_BOOTLOADER;
  10112. mutex_unlock(&cd->system_lock);
  10113. tmp_result = PT_ENTER_BL_PASS;
  10114. } else {
  10115. /*
  10116. * If the device doesn't enter BL mode as expected and rc is
  10117. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10118. * the function should return an error code to indicate this
  10119. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10120. */
  10121. if (!rc)
  10122. rc = -EINVAL;
  10123. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10124. mutex_lock(&cd->system_lock);
  10125. cd->mode = mode;
  10126. mutex_unlock(&cd->system_lock);
  10127. pt_debug(dev, DL_ERROR,
  10128. "%s ERROR: Not in BL as expected", __func__);
  10129. }
  10130. exit:
  10131. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10132. /* Check to get (buffered) flash information */
  10133. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10134. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10135. read_buf, &actual_read_len);
  10136. if (!rc) {
  10137. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10138. pt_debug(
  10139. dev, DL_WARN,
  10140. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10141. __func__,
  10142. read_buf[PIP2_RESP_BODY_OFFSET]);
  10143. /*
  10144. * If the BL was unable to cache the correct
  10145. * values when entering the first time due to
  10146. * the Flash part not having been powered up
  10147. * long enough, re-enter the BL to trigger the
  10148. * BL to re-attempt to cache the values.
  10149. */
  10150. if (flash_info_retry-- > 0) {
  10151. mode = PT_MODE_UNKNOWN;
  10152. pt_debug(dev, DL_WARN,
  10153. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10154. __func__, flash_info_retry);
  10155. goto _retry;
  10156. } else {
  10157. pt_debug(dev, DL_WARN,
  10158. "%s Manufacturer ID Unknown\n",
  10159. __func__);
  10160. tmp_result = PT_ENTER_BL_PASS;
  10161. }
  10162. }
  10163. } else {
  10164. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10165. pt_debug(
  10166. dev, DL_ERROR,
  10167. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10168. __func__);
  10169. }
  10170. }
  10171. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10172. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10173. "SUPPRESS");
  10174. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10175. if (result)
  10176. *result = tmp_result;
  10177. return rc;
  10178. }
  10179. /*******************************************************************************
  10180. * FUNCTION: _pt_pip2_file_open
  10181. *
  10182. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10183. *
  10184. * NOTE: The DUT must be in BL mode for this command to work
  10185. *
  10186. * RETURNS:
  10187. * <0 = Error
  10188. * >0 = file handle opened
  10189. *
  10190. * PARAMETERS:
  10191. * *dev - pointer to device structure
  10192. * file_no - PIP2 file number to open
  10193. ******************************************************************************/
  10194. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10195. {
  10196. int ret = 0;
  10197. u16 status;
  10198. u16 actual_read_len;
  10199. u8 file_handle;
  10200. u8 data[2];
  10201. u8 read_buf[10];
  10202. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10203. data[0] = file_no;
  10204. ret = _pt_request_pip2_send_cmd(dev,
  10205. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10206. data, 1, read_buf, &actual_read_len);
  10207. if (ret) {
  10208. pt_debug(dev, DL_ERROR,
  10209. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10210. __func__, file_no);
  10211. return -PIP2_RSP_ERR_NOT_OPEN;
  10212. }
  10213. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10214. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10215. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10216. (file_handle != file_no)) {
  10217. pt_debug(dev, DL_ERROR,
  10218. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10219. __func__, status, file_handle);
  10220. return -status;
  10221. }
  10222. return file_handle;
  10223. }
  10224. /*******************************************************************************
  10225. * FUNCTION: _pt_pip2_file_close
  10226. *
  10227. * SUMMARY: Using the BL PIP2 commands close a file
  10228. *
  10229. * NOTE: The DUT must be in BL mode for this command to work
  10230. *
  10231. * RETURNS:
  10232. * <0 = Error
  10233. * >0 = file handle closed
  10234. *
  10235. * PARAMETERS:
  10236. * *dev - pointer to device structure
  10237. * file_handle - handle to the file to be closed
  10238. ******************************************************************************/
  10239. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10240. {
  10241. int ret = 0;
  10242. u16 status;
  10243. u16 actual_read_len;
  10244. u8 data[2];
  10245. u8 read_buf[32];
  10246. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10247. data[0] = file_handle;
  10248. ret = _pt_request_pip2_send_cmd(dev,
  10249. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10250. data, 1, read_buf, &actual_read_len);
  10251. if (ret) {
  10252. pt_debug(dev, DL_ERROR,
  10253. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10254. __func__, file_handle);
  10255. return -ETIME;
  10256. }
  10257. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10258. if (status != 0x00) {
  10259. pt_debug(dev, DL_ERROR,
  10260. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10261. __func__, status, file_handle);
  10262. return -status;
  10263. }
  10264. return file_handle;
  10265. }
  10266. /*******************************************************************************
  10267. * FUNCTION: _pt_pip2_file_erase
  10268. *
  10269. * SUMMARY: Using the BL PIP2 commands erase a file
  10270. *
  10271. * NOTE: The DUT must be in BL mode for this command to work
  10272. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10273. * one retry is attempted for each sector in a file.
  10274. *
  10275. * RETURNS:
  10276. * <0 = Error
  10277. * >0 = file handle closed
  10278. *
  10279. * PARAMETERS:
  10280. * *dev - pointer to device structure
  10281. * file_handle - handle to the file to be erased
  10282. * *status - PIP2 erase status code
  10283. ******************************************************************************/
  10284. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10285. {
  10286. int ret = 0;
  10287. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10288. int retry = 1;
  10289. u16 actual_read_len;
  10290. u8 data[2];
  10291. u8 read_buf[10];
  10292. struct pt_core_data *cd = dev_get_drvdata(dev);
  10293. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10294. data[0] = file_handle;
  10295. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10296. *status = PIP2_RSP_ERR_TIMEOUT;
  10297. /* Increase waiting time for large file erase */
  10298. mutex_lock(&cd->system_lock);
  10299. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10300. mutex_unlock(&cd->system_lock);
  10301. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10302. ret = _pt_request_pip2_send_cmd(dev,
  10303. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10304. data, 2, read_buf, &actual_read_len);
  10305. if (ret)
  10306. break;
  10307. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10308. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10309. #ifdef TTDL_DIAGNOSTICS
  10310. cd->file_erase_timeout_count++;
  10311. #endif
  10312. pt_debug(dev, DL_WARN,
  10313. "%s: ERASE timeout %d for file = %d\n",
  10314. __func__, retry, file_handle);
  10315. }
  10316. retry++;
  10317. }
  10318. mutex_lock(&cd->system_lock);
  10319. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10320. mutex_unlock(&cd->system_lock);
  10321. if (ret) {
  10322. pt_debug(dev, DL_ERROR,
  10323. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10324. __func__, ret, file_handle);
  10325. return -EIO;
  10326. }
  10327. if (*status != 0x00) {
  10328. pt_debug(dev, DL_ERROR,
  10329. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10330. __func__, *status, file_handle);
  10331. return -EIO;
  10332. }
  10333. return file_handle;
  10334. }
  10335. /*******************************************************************************
  10336. * FUNCTION: _pt_pip2_file_read
  10337. *
  10338. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10339. *
  10340. * NOTE: The DUT must be in BL mode for this command to work
  10341. *
  10342. * RETURNS:
  10343. * <0 = Error
  10344. * >0 = number of bytes read
  10345. *
  10346. * PARAMETERS:
  10347. * *dev - pointer to device structure
  10348. * file_handle - File handle to read from
  10349. * num_bytes - number of bytes to read
  10350. ******************************************************************************/
  10351. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10352. u8 *read_buf)
  10353. {
  10354. int ret = 0;
  10355. u16 status;
  10356. u16 actual_read_len;
  10357. u8 data[3];
  10358. data[0] = file_handle;
  10359. data[1] = (num_bytes & 0x00FF);
  10360. data[2] = (num_bytes & 0xFF00) >> 8;
  10361. ret = _pt_request_pip2_send_cmd(dev,
  10362. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10363. data, 3, read_buf, &actual_read_len);
  10364. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10365. if (ret || ((status != 0x00) && (status != 0x03))) {
  10366. pt_debug(dev, DL_ERROR,
  10367. "%s File open failure with error code = %d\n",
  10368. __func__, status);
  10369. return -EPERM;
  10370. }
  10371. ret = num_bytes;
  10372. return ret;
  10373. }
  10374. /*******************************************************************************
  10375. * FUNCTION: _pt_read_us_file
  10376. *
  10377. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10378. * then read the entire file.
  10379. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10380. *
  10381. * RETURN:
  10382. * 0 = success
  10383. * !0 = failure
  10384. *
  10385. * PARAMETERS:
  10386. * *file_path - pointer to the file path
  10387. * *buf - pointer to the buffer to store the file contents
  10388. * *size - pointer to the size of the file
  10389. ******************************************************************************/
  10390. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10391. {
  10392. struct file *filp = NULL;
  10393. struct inode *inode = NULL;
  10394. unsigned int file_len = 0;
  10395. unsigned int read_len = 0;
  10396. mm_segment_t oldfs;
  10397. int rc = 0;
  10398. if (file_path == NULL || buf == NULL) {
  10399. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10400. return -EINVAL;
  10401. }
  10402. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10403. oldfs = force_uaccess_begin();
  10404. filp = filp_open_block(file_path, O_RDONLY, 0400);
  10405. if (IS_ERR(filp)) {
  10406. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10407. file_path);
  10408. rc = -ENOENT;
  10409. goto err;
  10410. }
  10411. if (filp->f_op == NULL) {
  10412. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10413. __func__);
  10414. rc = -EINVAL;
  10415. goto exit;
  10416. }
  10417. inode = filp->f_path.dentry->d_inode;
  10418. if (inode == NULL) {
  10419. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10420. __func__);
  10421. rc = -EINVAL;
  10422. goto exit;
  10423. }
  10424. file_len = i_size_read(inode->i_mapping->host);
  10425. if (file_len == 0) {
  10426. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10427. __func__, file_len);
  10428. rc = -EINVAL;
  10429. goto exit;
  10430. }
  10431. if (*size == 0)
  10432. read_len = file_len;
  10433. else
  10434. read_len = *size;
  10435. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10436. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10437. __func__, read_len);
  10438. rc = -EINVAL;
  10439. goto exit;
  10440. }
  10441. filp->private_data = inode->i_private;
  10442. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  10443. if (filp->f_op->read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10444. #else
  10445. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10446. #endif
  10447. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10448. rc = -EINVAL;
  10449. goto exit;
  10450. }
  10451. *size = read_len;
  10452. exit:
  10453. if (filp_close(filp, NULL) != 0)
  10454. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10455. err:
  10456. force_uaccess_end(oldfs);
  10457. return rc;
  10458. }
  10459. /*******************************************************************************
  10460. * FUNCTION: _pt_request_pip2_bin_hdr
  10461. *
  10462. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10463. * in the case of a flashless DUT, and parse the contents
  10464. *
  10465. * RETURNS:
  10466. * 0 = Success
  10467. * !0 = Error condition
  10468. *
  10469. * PARAMETERS:
  10470. * *dev - pointer to device structure
  10471. ******************************************************************************/
  10472. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10473. {
  10474. struct pt_core_data *cd = dev_get_drvdata(dev);
  10475. u8 file_handle;
  10476. u8 read_buf[255];
  10477. u8 hdr_len = 0;
  10478. u8 i;
  10479. int bytes_read;
  10480. int read_size;
  10481. int ret = 0;
  10482. int rc = 0;
  10483. bool load_hdr_struct = false;
  10484. if (cd->flashless_dut) {
  10485. read_size = sizeof(read_buf);
  10486. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10487. read_buf, &read_size);
  10488. if (rc) {
  10489. ret = rc;
  10490. pt_debug(dev, DL_ERROR,
  10491. "%s Failed to read fw image from US, rc=%d\n",
  10492. __func__, rc);
  10493. goto exit;
  10494. }
  10495. load_hdr_struct = true;
  10496. hdr_len = read_buf[0];
  10497. i = 0;
  10498. } else {
  10499. if (cd->mode != PT_MODE_BOOTLOADER) {
  10500. ret = -EPERM;
  10501. goto exit;
  10502. }
  10503. /* Open the bin file in Flash */
  10504. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10505. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10506. if (file_handle != PIP2_FW_FILE) {
  10507. ret = -ENOENT;
  10508. pt_debug(dev, DL_ERROR,
  10509. "%s Failed to open bin file\n", __func__);
  10510. goto exit;
  10511. }
  10512. /* Read the header length from the file */
  10513. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10514. read_size = 1;
  10515. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10516. read_buf);
  10517. if (bytes_read != read_size) {
  10518. ret = -EX_ERR_FREAD;
  10519. pt_debug(dev, DL_ERROR,
  10520. "%s Failed to bin file header len\n", __func__);
  10521. goto exit_close_file;
  10522. }
  10523. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10524. if (hdr_len == 0xFF) {
  10525. ret = -EX_ERR_FLEN;
  10526. pt_debug(dev, DL_ERROR,
  10527. "%s Bin header len is invalid\n", __func__);
  10528. goto exit_close_file;
  10529. }
  10530. /* Read the rest of the header from the bin file */
  10531. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10532. memset(read_buf, 0, sizeof(read_buf));
  10533. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10534. read_buf);
  10535. if (bytes_read != hdr_len) {
  10536. ret = -EX_ERR_FREAD;
  10537. pt_debug(dev, DL_ERROR,
  10538. "%s Failed to read bin file\n", __func__);
  10539. goto exit_close_file;
  10540. }
  10541. load_hdr_struct = true;
  10542. exit_close_file:
  10543. /* Close the file */
  10544. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10545. ret = -EX_ERR_FCLOSE;
  10546. pt_debug(dev, DL_ERROR,
  10547. "%s Failed to close bin file\n", __func__);
  10548. }
  10549. /*
  10550. * The length was already read so subtract 1 to make the rest of
  10551. * the offsets match the spec
  10552. */
  10553. i = PIP2_RESP_BODY_OFFSET - 1;
  10554. }
  10555. if (load_hdr_struct) {
  10556. hdr->length = hdr_len;
  10557. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10558. hdr->fw_major = (read_buf[i+3]);
  10559. hdr->fw_minor = (read_buf[i+4]);
  10560. hdr->fw_crc = (read_buf[i+5] << 24) |
  10561. (read_buf[i+6] << 16) |
  10562. (read_buf[i+7] << 8) |
  10563. (read_buf[i+8]);
  10564. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10565. (read_buf[i+10] << 16) |
  10566. (read_buf[i+11] << 8) |
  10567. (read_buf[i+12]);
  10568. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10569. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10570. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10571. if (hdr_len >= 22) {
  10572. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10573. (read_buf[i+20] << 16) |
  10574. (read_buf[i+21] << 8) |
  10575. (read_buf[i+22]);
  10576. } else {
  10577. hdr->hex_file_size = 0;
  10578. }
  10579. }
  10580. exit:
  10581. return ret;
  10582. }
  10583. /*******************************************************************************
  10584. * FUNCTION: _pt_pip2_file_get_stats
  10585. *
  10586. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10587. * opened file
  10588. *
  10589. * NOTE: The DUT must be in BL mode for this command to work
  10590. *
  10591. * RETURNS:
  10592. * !0 = Error
  10593. * 0 = Success
  10594. *
  10595. * PARAMETERS:
  10596. * *dev - pointer to device structure
  10597. * file_handle - File handle to read from
  10598. * *address - pointer to store address of file
  10599. * *file_size _ pointer to store size of file
  10600. ******************************************************************************/
  10601. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10602. u32 *file_size)
  10603. {
  10604. int ret = 1;
  10605. u16 status;
  10606. u16 actual_read_len;
  10607. u8 data[2];
  10608. u8 read_buf[16];
  10609. data[0] = file_handle;
  10610. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10611. ret = _pt_request_pip2_send_cmd(dev,
  10612. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10613. data, 2, read_buf, &actual_read_len);
  10614. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10615. if (ret || (status != 0x00)) {
  10616. pt_debug(dev, DL_ERROR,
  10617. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10618. __func__, status, file_handle, ret);
  10619. ret = -EIO;
  10620. goto exit;
  10621. }
  10622. pt_debug(dev, DL_DEBUG,
  10623. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10624. if (address) {
  10625. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10626. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10627. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10628. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10629. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10630. __func__, *address);
  10631. }
  10632. if (file_size) {
  10633. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10634. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10635. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10636. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10637. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10638. __func__, *file_size);
  10639. }
  10640. exit:
  10641. return ret;
  10642. }
  10643. /*******************************************************************************
  10644. * FUNCTION: _pt_pip2_file_seek_offset
  10645. *
  10646. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10647. * opened file
  10648. *
  10649. * NOTE: The DUT must be in BL mode for this command to work
  10650. * NOTE: File open/erase command can reset the offset
  10651. *
  10652. * RETURNS:
  10653. * !0 = Error
  10654. * 0 = Success
  10655. *
  10656. * PARAMETERS:
  10657. * *dev - pointer to device structure
  10658. * file_handle - File handle to read from
  10659. * read_offset - read offset of file
  10660. * write_offset - write offset of file
  10661. ******************************************************************************/
  10662. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10663. u32 read_offset, u32 write_offset)
  10664. {
  10665. int ret = 1;
  10666. u16 status;
  10667. u16 actual_read_len;
  10668. u8 data[10];
  10669. u8 read_buf[16];
  10670. data[0] = file_handle;
  10671. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10672. data[2] = 0xff & read_offset;
  10673. data[3] = 0xff & (read_offset >> 8);
  10674. data[4] = 0xff & (read_offset >> 16);
  10675. data[5] = 0xff & (read_offset >> 24);
  10676. data[6] = 0xff & write_offset;
  10677. data[7] = 0xff & (write_offset >> 8);
  10678. data[8] = 0xff & (write_offset >> 16);
  10679. data[9] = 0xff & (write_offset >> 24);
  10680. ret = _pt_request_pip2_send_cmd(dev,
  10681. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10682. data, 10, read_buf, &actual_read_len);
  10683. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10684. if (ret || (status != 0x00)) {
  10685. pt_debug(dev, DL_ERROR,
  10686. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10687. __func__, status, ret, file_handle);
  10688. ret = -EIO;
  10689. }
  10690. return ret;
  10691. }
  10692. /*******************************************************************************
  10693. * FUNCTION: _pt_pip2_file_crc
  10694. *
  10695. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10696. * the file.
  10697. *
  10698. * NOTE: The DUT must be in BL mode for this command to work
  10699. * NOTE: This command only can be used for BL version 1.8 or greater.
  10700. * BL version 1.8 added this change according to PGV-173.
  10701. *
  10702. * RETURNS:
  10703. * !0 = Error
  10704. * 0 = Success
  10705. *
  10706. * PARAMETERS:
  10707. * *dev - pointer to device structure
  10708. * file_handle - File handle to read from
  10709. * offset - start offset for CRC calculation
  10710. * length - number of bytes to calculate CRC over
  10711. * read_buf - pointer to the read buffer
  10712. ******************************************************************************/
  10713. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10714. u32 offset, u32 length, u8 *read_buf)
  10715. {
  10716. int rc = 1;
  10717. u16 actual_read_len;
  10718. u8 data[10];
  10719. data[0] = file_handle;
  10720. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10721. data[2] = 0xff & offset;
  10722. data[3] = 0xff & (offset >> 8);
  10723. data[4] = 0xff & (offset >> 16);
  10724. data[5] = 0xff & (offset >> 24);
  10725. data[6] = 0xff & length;
  10726. data[7] = 0xff & (length >> 8);
  10727. data[8] = 0xff & (length >> 16);
  10728. data[9] = 0xff & (length >> 24);
  10729. rc = _pt_request_pip2_send_cmd(dev,
  10730. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10731. data, 10, read_buf, &actual_read_len);
  10732. if (rc)
  10733. pt_debug(dev, DL_ERROR,
  10734. "%s Return FILE_CRC failure, rc = %d\n",
  10735. __func__, rc);
  10736. return rc;
  10737. }
  10738. /*******************************************************************************
  10739. * FUNCTION: pt_pip2_ping_test
  10740. *
  10741. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10742. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10743. * response payload matches what was sent.
  10744. * The max payload size is 247:
  10745. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10746. *
  10747. * RETURN:
  10748. * 0 = success
  10749. * !0 = failure
  10750. *
  10751. * PARAMETERS:
  10752. * *dev - pointer to device structure
  10753. * *attr - pointer to device attributes
  10754. * *buf - pointer to output buffer
  10755. ******************************************************************************/
  10756. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10757. {
  10758. u16 actual_read_len;
  10759. u8 *read_buf = NULL;
  10760. u8 *data = NULL;
  10761. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10762. int i = 1;
  10763. int j = 0;
  10764. int rc = 0;
  10765. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10766. if (!read_buf)
  10767. goto ping_test_exit;
  10768. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10769. if (!data)
  10770. goto ping_test_exit;
  10771. /* Load data payload with an array of walking 1's */
  10772. for (i = 0; i < 255; i++)
  10773. data[i] = 0x01 << (i % 8);
  10774. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10775. for (i = 0; i <= max_bytes; i++) {
  10776. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10777. PIP2_CMD_ID_PING, data, i, read_buf,
  10778. &actual_read_len);
  10779. if (rc) {
  10780. pt_debug(dev, DL_ERROR,
  10781. "%s: PING failed with %d byte payload\n",
  10782. __func__, i);
  10783. break;
  10784. }
  10785. /* Verify data returned matches data sent */
  10786. for (j = 0; j < i; j++) {
  10787. if (read_buf[data_offset + j] != data[j]) {
  10788. pt_debug(dev, DL_DEBUG,
  10789. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10790. __func__, i, j, data[j], j,
  10791. read_buf[data_offset + j]);
  10792. goto ping_test_exit;
  10793. }
  10794. }
  10795. }
  10796. ping_test_exit:
  10797. *last_packet_size = i - 1;
  10798. kfree(read_buf);
  10799. kfree(data);
  10800. return rc;
  10801. }
  10802. /*******************************************************************************
  10803. * FUNCTION: _pt_ic_parse_input_hex
  10804. *
  10805. * SUMMARY: Parse a char data array as space delimited hex values into
  10806. * an int array.
  10807. *
  10808. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10809. * buffer to store data is "u32". This function is still needed by the
  10810. * "command" sysfs node because the buffer type to store data is "u8".
  10811. *
  10812. * RETURN: Length of parsed data
  10813. *
  10814. * PARAMETERS:
  10815. * *dev - pointer to device structure
  10816. * *buf - pointer to buffer that holds the input array to parse
  10817. * buf_size - size of buf
  10818. * *ic_buf - pointer to array to store parsed data
  10819. * ic_buf_size - max size of ic_buf
  10820. ******************************************************************************/
  10821. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10822. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10823. {
  10824. const char *pbuf = buf;
  10825. unsigned long value;
  10826. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10827. u32 i = 0;
  10828. u32 j;
  10829. int last = 0;
  10830. int ret;
  10831. pt_debug(dev, DL_DEBUG,
  10832. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10833. __func__, pbuf, buf, buf_size, "scan buf size",
  10834. PT_MAX_PIP2_MSG_SIZE, buf);
  10835. while (pbuf <= (buf + buf_size)) {
  10836. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10837. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10838. __func__, "Max cmd size exceeded", i,
  10839. PT_MAX_PIP2_MSG_SIZE);
  10840. return -EINVAL;
  10841. }
  10842. if (i >= ic_buf_size) {
  10843. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10844. __func__, "Buffer size exceeded", i,
  10845. ic_buf_size);
  10846. return -EINVAL;
  10847. }
  10848. while (((*pbuf == ' ') || (*pbuf == ','))
  10849. && (pbuf < (buf + buf_size))) {
  10850. last = *pbuf;
  10851. pbuf++;
  10852. }
  10853. if (pbuf >= (buf + buf_size))
  10854. break;
  10855. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10856. if ((last == ',') && (*pbuf == ',')) {
  10857. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10858. __func__, "Invalid data format.");
  10859. return -EINVAL;
  10860. }
  10861. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10862. && (pbuf < (buf + buf_size))
  10863. && (*pbuf != ' ')
  10864. && (*pbuf != ','); j++) {
  10865. last = *pbuf;
  10866. scan_buf[j] = *pbuf++;
  10867. }
  10868. ret = kstrtoul(scan_buf, 16, &value);
  10869. if (ret < 0) {
  10870. pt_debug(dev, DL_ERROR,
  10871. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10872. "Invalid data format. ", scan_buf,
  10873. "Use \"0xHH,...,0xHH\"", " instead.",
  10874. i, ret);
  10875. return ret;
  10876. }
  10877. ic_buf[i] = value;
  10878. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10879. __func__, i, value);
  10880. i++;
  10881. }
  10882. return i;
  10883. }
  10884. /*******************************************************************************
  10885. * FUNCTION: _pt_ic_parse_input
  10886. *
  10887. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10888. * hex values or dec values into an int array with the following rules:
  10889. * 1) Hex values must have a "0x" prefix for each element or the first element
  10890. * only
  10891. * 2) Dec values do not have any prefix
  10892. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10893. * string
  10894. *
  10895. * RETURN: Number of values parsed
  10896. *
  10897. * PARAMETERS:
  10898. * *dev - pointer to device structure
  10899. * *buf - pointer to buffer that holds the input array to parse
  10900. * buf_size - size of buf
  10901. * *out_buf - pointer to array to store parsed data
  10902. * out_buf_size - max size of buffer to be stored
  10903. ******************************************************************************/
  10904. static int _pt_ic_parse_input(struct device *dev,
  10905. const char *buf, size_t buf_size,
  10906. u32 *out_buf, size_t out_buf_size)
  10907. {
  10908. const char *pbuf = buf;
  10909. unsigned long value;
  10910. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10911. u32 i = 0;
  10912. u32 j;
  10913. int last = 0;
  10914. int ret = 0;
  10915. u8 str_base = 0;
  10916. pt_debug(dev, DL_DEBUG,
  10917. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10918. __func__, buf_size, out_buf_size, "scan buf size",
  10919. PT_MAX_PIP2_MSG_SIZE, buf);
  10920. while (pbuf <= (buf + buf_size)) {
  10921. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10922. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10923. __func__, "Max cmd size exceeded", i,
  10924. PT_MAX_PIP2_MSG_SIZE);
  10925. ret = -EINVAL;
  10926. goto error;
  10927. }
  10928. if (i >= out_buf_size) {
  10929. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10930. __func__, "Buffer size exceeded", i,
  10931. out_buf_size);
  10932. ret = -EINVAL;
  10933. goto error;
  10934. }
  10935. while (((*pbuf == ' ') || (*pbuf == ','))
  10936. && (pbuf < (buf + buf_size))) {
  10937. last = *pbuf;
  10938. pbuf++;
  10939. }
  10940. if (pbuf >= (buf + buf_size))
  10941. break;
  10942. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10943. if ((last == ',') && (*pbuf == ',')) {
  10944. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10945. __func__, "Invalid data format.");
  10946. ret = -EINVAL;
  10947. goto error;
  10948. }
  10949. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10950. && (pbuf < (buf + buf_size))
  10951. && (*pbuf != ' ')
  10952. && (*pbuf != ','); j++) {
  10953. last = *pbuf;
  10954. scan_buf[j] = *pbuf++;
  10955. }
  10956. if (i == 0) {
  10957. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10958. (strncmp(scan_buf, "0X", 2) == 0))
  10959. str_base = 16;
  10960. else
  10961. str_base = 10;
  10962. } else {
  10963. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10964. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10965. (str_base == 10)) {
  10966. pt_debug(dev, DL_ERROR,
  10967. "%s: Decimal and Heximal data mixed\n",
  10968. __func__);
  10969. ret = -EINVAL;
  10970. goto error;
  10971. }
  10972. }
  10973. ret = kstrtoul(scan_buf, str_base, &value);
  10974. if (ret < 0) {
  10975. pt_debug(dev, DL_ERROR,
  10976. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10977. "Invalid data format. ", scan_buf,
  10978. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10979. " instead.", i, ret);
  10980. goto error;
  10981. }
  10982. out_buf[i] = value;
  10983. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10984. __func__, i, value, value);
  10985. i++;
  10986. }
  10987. ret = i;
  10988. error:
  10989. return ret;
  10990. }
  10991. #ifdef TTHE_TUNER_SUPPORT
  10992. /*******************************************************************************
  10993. * FUNCTION: tthe_debugfs_open
  10994. *
  10995. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10996. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10997. * printing a large number of fingers and sensor data without overflowing
  10998. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10999. * even multiple of PAGE_SIZE
  11000. *
  11001. * RETURN:
  11002. * 0 = success
  11003. * !0 = failure
  11004. *
  11005. * PARAMETERS:
  11006. * *inode - file inode number
  11007. * *filp - file pointer to debugfs file
  11008. ******************************************************************************/
  11009. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  11010. {
  11011. struct pt_core_data *cd = inode->i_private;
  11012. u32 buf_size = PT_MAX_PRBUF_SIZE;
  11013. filp->private_data = inode->i_private;
  11014. if (cd->tthe_buf)
  11015. return -EBUSY;
  11016. while (buf_size < 4096)
  11017. buf_size = buf_size << 1;
  11018. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  11019. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  11020. cd->tthe_buf_size = buf_size;
  11021. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  11022. if (!cd->tthe_buf)
  11023. return -ENOMEM;
  11024. return 0;
  11025. }
  11026. /*******************************************************************************
  11027. * FUNCTION: tthe_debugfs_close
  11028. *
  11029. * SUMMARY: Close method for tthe_tuner debugfs node.
  11030. *
  11031. * RETURN:
  11032. * 0 = success
  11033. * !0 = failure
  11034. *
  11035. * PARAMETERS:
  11036. * *inode - file inode number
  11037. * *filp - file pointer to debugfs file
  11038. ******************************************************************************/
  11039. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11040. {
  11041. struct pt_core_data *cd = filp->private_data;
  11042. filp->private_data = NULL;
  11043. kfree(cd->tthe_buf);
  11044. cd->tthe_buf = NULL;
  11045. return 0;
  11046. }
  11047. /*******************************************************************************
  11048. * FUNCTION: tthe_debugfs_read
  11049. *
  11050. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11051. * tthe_buf to user buffer.
  11052. *
  11053. * RETURN: Size of debugfs data print
  11054. *
  11055. * PARAMETERS:
  11056. * *filp - file pointer to debugfs file
  11057. * *buf - the user space buffer to read to
  11058. * count - the maximum number of bytes to read
  11059. * *ppos - the current position in the buffer
  11060. ******************************************************************************/
  11061. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11062. size_t count, loff_t *ppos)
  11063. {
  11064. struct pt_core_data *cd = filp->private_data;
  11065. int size;
  11066. int ret;
  11067. static int partial_read;
  11068. wait_event_interruptible(cd->wait_q,
  11069. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11070. mutex_lock(&cd->tthe_lock);
  11071. if (cd->tthe_exit) {
  11072. mutex_unlock(&cd->tthe_lock);
  11073. return 0;
  11074. }
  11075. if (count > cd->tthe_buf_len)
  11076. size = cd->tthe_buf_len;
  11077. else
  11078. size = count;
  11079. if (!size) {
  11080. mutex_unlock(&cd->tthe_lock);
  11081. return 0;
  11082. }
  11083. if (partial_read) {
  11084. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11085. partial_read = 0;
  11086. } else {
  11087. ret = copy_to_user(buf, cd->tthe_buf, size);
  11088. }
  11089. if (size == count)
  11090. partial_read = count;
  11091. if (ret == size)
  11092. return -EFAULT;
  11093. size -= ret;
  11094. cd->tthe_buf_len -= size;
  11095. mutex_unlock(&cd->tthe_lock);
  11096. *ppos += size;
  11097. return size;
  11098. }
  11099. static const struct file_operations tthe_debugfs_fops = {
  11100. .open = tthe_debugfs_open,
  11101. .release = tthe_debugfs_close,
  11102. .read = tthe_debugfs_read,
  11103. };
  11104. #endif
  11105. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11106. .start_bl = _pt_request_pip_start_bl,
  11107. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11108. .resume_scanning = _pt_request_pip_resume_scanning,
  11109. .get_param = _pt_request_pip_get_param,
  11110. .set_param = _pt_request_pip_set_param,
  11111. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11112. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11113. .get_data_structure = _pt_request_pip_get_data_structure,
  11114. .run_selftest = _pt_request_pip_run_selftest,
  11115. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11116. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11117. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11118. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11119. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11120. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11121. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11122. .read_data_block = _pt_request_pip_read_data_block,
  11123. .write_data_block = _pt_request_pip_write_data_block,
  11124. .user_cmd = _pt_request_pip_user_cmd,
  11125. .get_bl_info = _pt_request_pip_bl_get_information,
  11126. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11127. .launch_app = _pt_request_pip_launch_app,
  11128. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11129. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11130. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11131. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11132. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11133. .pip2_file_open = _pt_pip2_file_open,
  11134. .pip2_file_close = _pt_pip2_file_close,
  11135. .pip2_file_erase = _pt_pip2_file_erase,
  11136. .read_us_file = _pt_read_us_file,
  11137. .manage_cal_data = _pt_manage_local_cal_data,
  11138. .calc_crc = crc_ccitt_calculate,
  11139. #ifdef TTDL_DIAGNOSTICS
  11140. .pip2_file_read = _pt_pip2_file_read,
  11141. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11142. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11143. .pip2_file_crc = _pt_pip2_file_crc,
  11144. #endif
  11145. };
  11146. static struct pt_core_commands _pt_core_commands = {
  11147. .subscribe_attention = _pt_subscribe_attention,
  11148. .unsubscribe_attention = _pt_unsubscribe_attention,
  11149. .request_exclusive = _pt_request_exclusive,
  11150. .release_exclusive = _pt_release_exclusive,
  11151. .request_reset = _pt_request_reset,
  11152. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11153. .request_enum = _pt_request_enum,
  11154. .request_sysinfo = _pt_request_sysinfo,
  11155. .request_loader_pdata = _pt_request_loader_pdata,
  11156. .request_stop_wd = _pt_request_stop_wd,
  11157. .request_start_wd = _pt_request_start_wd,
  11158. .request_get_mode = _pt_request_get_mode,
  11159. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11160. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11161. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11162. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11163. .request_dut_generation = _pt_request_dut_generation,
  11164. .request_hw_version = _pt_request_hw_version,
  11165. .parse_sysfs_input = _pt_ic_parse_input,
  11166. #ifdef TTHE_TUNER_SUPPORT
  11167. .request_tthe_print = _pt_request_tthe_print,
  11168. #endif
  11169. #ifdef TTDL_DIAGNOSTICS
  11170. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11171. #endif
  11172. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11173. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11174. };
  11175. struct pt_core_commands *pt_get_commands(void)
  11176. {
  11177. return &_pt_core_commands;
  11178. }
  11179. EXPORT_SYMBOL_GPL(pt_get_commands);
  11180. static DEFINE_MUTEX(core_list_lock);
  11181. static LIST_HEAD(core_list);
  11182. static DEFINE_MUTEX(module_list_lock);
  11183. static LIST_HEAD(module_list);
  11184. static int core_number;
  11185. /*******************************************************************************
  11186. * FUNCTION: pt_probe_module
  11187. *
  11188. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11189. *
  11190. * RETURN:
  11191. * 0 = success
  11192. * !0 = failure
  11193. *
  11194. * PARAMETERS:
  11195. * *cd - pointer to core data
  11196. * *module - pointer to module structure
  11197. ******************************************************************************/
  11198. static int pt_probe_module(struct pt_core_data *cd,
  11199. struct pt_module *module)
  11200. {
  11201. struct module_node *module_node;
  11202. int rc = 0;
  11203. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11204. if (!module_node)
  11205. return -ENOMEM;
  11206. module_node->module = module;
  11207. mutex_lock(&cd->module_list_lock);
  11208. list_add(&module_node->node, &cd->module_list);
  11209. mutex_unlock(&cd->module_list_lock);
  11210. rc = module->probe(cd->dev, &module_node->data);
  11211. if (rc) {
  11212. /*
  11213. * Remove from the list when probe fails
  11214. * in order not to call release
  11215. */
  11216. mutex_lock(&cd->module_list_lock);
  11217. list_del(&module_node->node);
  11218. mutex_unlock(&cd->module_list_lock);
  11219. kfree(module_node);
  11220. goto exit;
  11221. }
  11222. exit:
  11223. return rc;
  11224. }
  11225. /*******************************************************************************
  11226. * FUNCTION: pt_release_module
  11227. *
  11228. * SUMMARY: Call the release pointer and remove the module pointer from
  11229. * module_list.
  11230. *
  11231. * PARAMETERS:
  11232. * *cd - pointer to core data
  11233. * *module - pointer to module structure
  11234. ******************************************************************************/
  11235. static void pt_release_module(struct pt_core_data *cd,
  11236. struct pt_module *module)
  11237. {
  11238. struct module_node *m, *m_n;
  11239. mutex_lock(&cd->module_list_lock);
  11240. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11241. if (m->module == module) {
  11242. module->release(cd->dev, m->data);
  11243. list_del(&m->node);
  11244. kfree(m);
  11245. break;
  11246. }
  11247. mutex_unlock(&cd->module_list_lock);
  11248. }
  11249. /*******************************************************************************
  11250. * FUNCTION: pt_probe_modules
  11251. *
  11252. * SUMMARY: Iterate module_list and probe each module.
  11253. *
  11254. * PARAMETERS:
  11255. * *cd - pointer to core data
  11256. ******************************************************************************/
  11257. static void pt_probe_modules(struct pt_core_data *cd)
  11258. {
  11259. struct pt_module *m;
  11260. int rc = 0;
  11261. mutex_lock(&module_list_lock);
  11262. list_for_each_entry(m, &module_list, node) {
  11263. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11264. __func__, m->name);
  11265. rc = pt_probe_module(cd, m);
  11266. if (rc)
  11267. pt_debug(cd->dev, DL_ERROR,
  11268. "%s: Probe fails for module %s\n",
  11269. __func__, m->name);
  11270. }
  11271. mutex_unlock(&module_list_lock);
  11272. }
  11273. /*******************************************************************************
  11274. * FUNCTION: pt_release_modules
  11275. *
  11276. * SUMMARY: Iterate module_list and remove each module.
  11277. *
  11278. * PARAMETERS:
  11279. * *cd - pointer to core data
  11280. ******************************************************************************/
  11281. static void pt_release_modules(struct pt_core_data *cd)
  11282. {
  11283. struct pt_module *m;
  11284. mutex_lock(&module_list_lock);
  11285. list_for_each_entry(m, &module_list, node)
  11286. pt_release_module(cd, m);
  11287. mutex_unlock(&module_list_lock);
  11288. }
  11289. /*******************************************************************************
  11290. * FUNCTION: pt_get_core_data
  11291. *
  11292. * SUMMARY: Iterate core_list and get core data.
  11293. *
  11294. * RETURN:
  11295. * pointer to core data or null pointer if fail
  11296. *
  11297. * PARAMETERS:
  11298. * *id - pointer to core id
  11299. ******************************************************************************/
  11300. struct pt_core_data *pt_get_core_data(char *id)
  11301. {
  11302. struct pt_core_data *d;
  11303. list_for_each_entry(d, &core_list, node)
  11304. if (!strncmp(d->core_id, id, 20))
  11305. return d;
  11306. return NULL;
  11307. }
  11308. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11309. /*******************************************************************************
  11310. * FUNCTION: pt_add_core
  11311. *
  11312. * SUMMARY: Add core data to the core_list.
  11313. *
  11314. * PARAMETERS:
  11315. * *dev - pointer to device structure
  11316. ******************************************************************************/
  11317. static void pt_add_core(struct device *dev)
  11318. {
  11319. struct pt_core_data *d;
  11320. struct pt_core_data *cd = dev_get_drvdata(dev);
  11321. mutex_lock(&core_list_lock);
  11322. list_for_each_entry(d, &core_list, node)
  11323. if (d->dev == dev)
  11324. goto unlock;
  11325. list_add(&cd->node, &core_list);
  11326. unlock:
  11327. mutex_unlock(&core_list_lock);
  11328. }
  11329. /*******************************************************************************
  11330. * FUNCTION: pt_del_core
  11331. *
  11332. * SUMMARY: Remove core data from the core_list.
  11333. *
  11334. * PARAMETERS:
  11335. * *dev - pointer to device structure
  11336. ******************************************************************************/
  11337. static void pt_del_core(struct device *dev)
  11338. {
  11339. struct pt_core_data *d, *d_n;
  11340. mutex_lock(&core_list_lock);
  11341. list_for_each_entry_safe(d, d_n, &core_list, node)
  11342. if (d->dev == dev) {
  11343. list_del(&d->node);
  11344. goto unlock;
  11345. }
  11346. unlock:
  11347. mutex_unlock(&core_list_lock);
  11348. }
  11349. /*******************************************************************************
  11350. * FUNCTION: pt_register_module
  11351. *
  11352. * SUMMARY: Register the module to module_list and probe the module for each
  11353. * core.
  11354. *
  11355. * RETURN:
  11356. * 0 = success
  11357. * !0 = failure
  11358. *
  11359. * PARAMETERS:
  11360. * *module - pointer to module structure
  11361. ******************************************************************************/
  11362. int pt_register_module(struct pt_module *module)
  11363. {
  11364. struct pt_module *m;
  11365. struct pt_core_data *cd;
  11366. int rc = 0;
  11367. if (!module || !module->probe || !module->release)
  11368. return -EINVAL;
  11369. mutex_lock(&module_list_lock);
  11370. list_for_each_entry(m, &module_list, node)
  11371. if (m == module) {
  11372. rc = -EEXIST;
  11373. goto unlock;
  11374. }
  11375. list_add(&module->node, &module_list);
  11376. /* Probe the module for each core */
  11377. mutex_lock(&core_list_lock);
  11378. list_for_each_entry(cd, &core_list, node)
  11379. pt_probe_module(cd, module);
  11380. mutex_unlock(&core_list_lock);
  11381. unlock:
  11382. mutex_unlock(&module_list_lock);
  11383. return rc;
  11384. }
  11385. EXPORT_SYMBOL_GPL(pt_register_module);
  11386. /*******************************************************************************
  11387. * FUNCTION: pt_unregister_module
  11388. *
  11389. * SUMMARY: Release the module for each core and remove the module from
  11390. * module_list.
  11391. *
  11392. * RETURN:
  11393. * 0 = success
  11394. * !0 = failure
  11395. *
  11396. * PARAMETERS:
  11397. * *module - pointer to module structure
  11398. ******************************************************************************/
  11399. void pt_unregister_module(struct pt_module *module)
  11400. {
  11401. struct pt_module *m, *m_n;
  11402. struct pt_core_data *cd;
  11403. if (!module)
  11404. return;
  11405. mutex_lock(&module_list_lock);
  11406. /* Release the module for each core */
  11407. mutex_lock(&core_list_lock);
  11408. list_for_each_entry(cd, &core_list, node)
  11409. pt_release_module(cd, module);
  11410. mutex_unlock(&core_list_lock);
  11411. list_for_each_entry_safe(m, m_n, &module_list, node)
  11412. if (m == module) {
  11413. list_del(&m->node);
  11414. break;
  11415. }
  11416. mutex_unlock(&module_list_lock);
  11417. }
  11418. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11419. /*******************************************************************************
  11420. * FUNCTION: pt_get_module_data
  11421. *
  11422. * SUMMARY: Get module data from module_node by module_list.
  11423. *
  11424. * RETURN:
  11425. * pointer to module data
  11426. *
  11427. * PARAMETERS:
  11428. * *dev - pointer to device structure
  11429. * *module - pointer to module structure
  11430. ******************************************************************************/
  11431. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11432. {
  11433. struct pt_core_data *cd = dev_get_drvdata(dev);
  11434. struct module_node *m;
  11435. void *data = NULL;
  11436. mutex_lock(&cd->module_list_lock);
  11437. list_for_each_entry(m, &cd->module_list, node)
  11438. if (m->module == module) {
  11439. data = m->data;
  11440. break;
  11441. }
  11442. mutex_unlock(&cd->module_list_lock);
  11443. return data;
  11444. }
  11445. EXPORT_SYMBOL(pt_get_module_data);
  11446. #ifdef CONFIG_HAS_EARLYSUSPEND
  11447. /*******************************************************************************
  11448. * FUNCTION: pt_early_suspend
  11449. *
  11450. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11451. * attention list.
  11452. *
  11453. * PARAMETERS:
  11454. * *h - pointer to early_suspend structure
  11455. ******************************************************************************/
  11456. static void pt_early_suspend(struct early_suspend *h)
  11457. {
  11458. struct pt_core_data *cd =
  11459. container_of(h, struct pt_core_data, es);
  11460. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11461. }
  11462. /*******************************************************************************
  11463. * FUNCTION: pt_late_resume
  11464. *
  11465. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11466. * attention list.
  11467. *
  11468. * PARAMETERS:
  11469. * *h - pointer to early_suspend structure
  11470. ******************************************************************************/
  11471. static void pt_late_resume(struct early_suspend *h)
  11472. {
  11473. struct pt_core_data *cd =
  11474. container_of(h, struct pt_core_data, es);
  11475. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11476. }
  11477. /*******************************************************************************
  11478. * FUNCTION: pt_setup_early_suspend
  11479. *
  11480. * SUMMARY: Register early/suspend function to the system.
  11481. *
  11482. * PARAMETERS:
  11483. * *cd - pointer to core data
  11484. ******************************************************************************/
  11485. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11486. {
  11487. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11488. cd->es.suspend = pt_early_suspend;
  11489. cd->es.resume = pt_late_resume;
  11490. register_early_suspend(&cd->es);
  11491. }
  11492. #elif defined(CONFIG_DRM)
  11493. static void pt_resume_work(struct work_struct *work)
  11494. {
  11495. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11496. resume_work);
  11497. int rc = 0;
  11498. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11499. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11500. return;
  11501. rc = pt_core_easywake_off(pt_data);
  11502. if (rc < 0) {
  11503. pt_debug(pt_data->dev, DL_ERROR,
  11504. "%s: Error on wake\n", __func__);
  11505. }
  11506. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11507. return;
  11508. }
  11509. static void pt_suspend_work(struct work_struct *work)
  11510. {
  11511. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11512. suspend_work);
  11513. int rc = 0;
  11514. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11515. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11516. return;
  11517. rc = pt_core_easywake_on(pt_data);
  11518. if (rc < 0) {
  11519. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11520. return;
  11521. }
  11522. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11523. return;
  11524. }
  11525. #if defined(CONFIG_PANEL_NOTIFIER)
  11526. /*******************************************************************************
  11527. * FUNCTION: panel_event_notifier_callback
  11528. *
  11529. * SUMMARY: Call back function for Panel Event notifier to allow to call
  11530. * resume/suspend attention list.
  11531. *
  11532. * PARAMETERS:
  11533. * tag - type of input panel.
  11534. * *notification - pointer to notification details.
  11535. * *client_data - pointer to core data
  11536. ******************************************************************************/
  11537. static void panel_event_notifier_callback(enum panel_event_notifier_tag tag,
  11538. struct panel_event_notification *notification, void *client_data)
  11539. {
  11540. struct pt_core_data *cd = client_data;
  11541. if(!notification)
  11542. {
  11543. pt_debug(cd->dev,DL_INFO, "%s: Invalid notification\n", __func__);
  11544. return;
  11545. }
  11546. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11547. if (cd->quick_boot || cd->drv_debug_suspend)
  11548. goto exit;
  11549. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%d,fb_state %d",
  11550. __func__, notification->notif_type, cd->fb_state);
  11551. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11552. __func__, (notification->notif_type == DRM_PANEL_EVENT_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11553. if (notification->notif_type == DRM_PANEL_EVENT_UNBLANK) {
  11554. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11555. if (notification->notif_data.early_trigger) {
  11556. pr_err("%s: resume: event = %d, not care\n", __func__, notification->notif_type);
  11557. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %d, not care\n",
  11558. __func__, notification->notif_type);
  11559. } else {
  11560. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11561. __func__);
  11562. cancel_work_sync(&cd->resume_offload_work);
  11563. cancel_work_sync(&cd->suspend_offload_work);
  11564. cancel_work_sync(&cd->resume_work);
  11565. cancel_work_sync(&cd->suspend_work);
  11566. queue_work(cd->pt_workqueue, &cd->resume_work);
  11567. #if defined(CONFIG_PM_SLEEP)
  11568. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11569. __func__);
  11570. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11571. pt_core_resume_(cd->dev);
  11572. #endif
  11573. cd->fb_state = FB_ON;
  11574. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11575. }
  11576. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11577. pt_debug(cd->dev, DL_INFO, "%s: BLANK!\n", __func__);
  11578. if (notification->notif_data.early_trigger) {
  11579. #if defined(CONFIG_PM_SLEEP)
  11580. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11581. __func__);
  11582. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11583. pt_core_suspend_(cd->dev);
  11584. #endif
  11585. cancel_work_sync(&cd->resume_offload_work);
  11586. cancel_work_sync(&cd->suspend_offload_work);
  11587. cancel_work_sync(&cd->resume_work);
  11588. cancel_work_sync(&cd->suspend_work);
  11589. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11590. cd->fb_state = FB_OFF;
  11591. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11592. } else {
  11593. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11594. __func__, notification->notif_type);
  11595. }
  11596. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK_LP) {
  11597. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11598. if (notification->notif_data.early_trigger) {
  11599. #if defined(CONFIG_PM_SLEEP)
  11600. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n", __func__);
  11601. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11602. pt_core_suspend_(cd->dev);
  11603. #endif
  11604. cancel_work_sync(&cd->resume_offload_work);
  11605. cancel_work_sync(&cd->suspend_offload_work);
  11606. cancel_work_sync(&cd->resume_work);
  11607. cancel_work_sync(&cd->suspend_work);
  11608. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11609. cd->fb_state = FB_OFF;
  11610. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11611. } else {
  11612. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11613. __func__, notification->notif_type);
  11614. }
  11615. } else {
  11616. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11617. __func__, notification->notif_type);
  11618. }
  11619. exit:
  11620. return;
  11621. }
  11622. /*******************************************************************************
  11623. * FUNCTION: pt_setup_panel_event_notifier
  11624. *
  11625. * SUMMARY: Set up call back function into drm notifier.
  11626. *
  11627. * PARAMETERS:
  11628. * *cd - pointer to core data
  11629. ******************************************************************************/
  11630. static void pt_setup_panel_event_notifier(struct pt_core_data *cd)
  11631. {
  11632. void *cookie = NULL;
  11633. if (!active_panel)
  11634. pt_debug(cd->dev, DL_ERROR,
  11635. "%s: Active panel not registered!\n", __func__);
  11636. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11637. if (!cd->pt_workqueue) {
  11638. pt_debug(cd->dev, DL_ERROR,
  11639. "%s: worker thread creation failed !\n", __func__);
  11640. }
  11641. if (cd->pt_workqueue) {
  11642. INIT_WORK(&cd->resume_work, pt_resume_work);
  11643. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11644. }
  11645. cookie = panel_event_notifier_register(PANEL_EVENT_NOTIFICATION_PRIMARY,
  11646. PANEL_EVENT_NOTIFIER_CLIENT_PRIMARY_TOUCH,
  11647. active_panel,&panel_event_notifier_callback, cd);
  11648. if (active_panel && !cookie)
  11649. {
  11650. pt_debug(cd->dev, DL_ERROR,
  11651. "%s: Register notifier failed!\n", __func__);
  11652. }
  11653. cd->entry = cookie;
  11654. }
  11655. #else
  11656. /*******************************************************************************
  11657. * FUNCTION: drm_notifier_callback
  11658. *
  11659. * SUMMARY: Call back function for DRM notifier to allow to call
  11660. * resume/suspend attention list.
  11661. *
  11662. * RETURN:
  11663. * 0 = success
  11664. *
  11665. * PARAMETERS:
  11666. * *self - pointer to notifier_block structure
  11667. * event - event type of fb notifier
  11668. * *data - pointer to fb_event structure
  11669. ******************************************************************************/
  11670. static int drm_notifier_callback(struct notifier_block *self,
  11671. unsigned long event, void *data)
  11672. {
  11673. struct pt_core_data *cd =
  11674. container_of(self, struct pt_core_data, fb_notifier);
  11675. struct drm_panel_notifier *evdata = data;
  11676. int *blank;
  11677. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11678. if (!evdata)
  11679. goto exit;
  11680. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11681. event == DRM_PANEL_EVENT_BLANK)) {
  11682. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11683. __func__, event);
  11684. goto exit;
  11685. }
  11686. if (cd->quick_boot || cd->drv_debug_suspend)
  11687. goto exit;
  11688. blank = evdata->data;
  11689. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11690. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11691. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11692. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11693. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11694. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11695. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11696. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11697. __func__, event);
  11698. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11699. if (cd->fb_state != FB_ON) {
  11700. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11701. __func__);
  11702. cancel_work_sync(&cd->resume_offload_work);
  11703. cancel_work_sync(&cd->suspend_offload_work);
  11704. cancel_work_sync(&cd->resume_work);
  11705. cancel_work_sync(&cd->suspend_work);
  11706. queue_work(cd->pt_workqueue, &cd->resume_work);
  11707. #if defined(CONFIG_PM_SLEEP)
  11708. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11709. __func__);
  11710. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11711. pt_core_resume_(cd->dev);
  11712. #endif
  11713. cd->fb_state = FB_ON;
  11714. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11715. }
  11716. }
  11717. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11718. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11719. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11720. if (cd->fb_state != FB_OFF) {
  11721. #if defined(CONFIG_PM_SLEEP)
  11722. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11723. __func__);
  11724. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11725. pt_core_suspend_(cd->dev);
  11726. #endif
  11727. cancel_work_sync(&cd->resume_offload_work);
  11728. cancel_work_sync(&cd->suspend_offload_work);
  11729. cancel_work_sync(&cd->resume_work);
  11730. cancel_work_sync(&cd->suspend_work);
  11731. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11732. cd->fb_state = FB_OFF;
  11733. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11734. }
  11735. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11736. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11737. __func__, event);
  11738. }
  11739. } else {
  11740. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11741. __func__, *blank);
  11742. }
  11743. exit:
  11744. return 0;
  11745. }
  11746. /*******************************************************************************
  11747. * FUNCTION: pt_setup_drm_notifier
  11748. *
  11749. * SUMMARY: Set up call back function into drm notifier.
  11750. *
  11751. * PARAMETERS:
  11752. * *cd - pointer to core data
  11753. ******************************************************************************/
  11754. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11755. {
  11756. cd->fb_state = FB_NONE;
  11757. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11758. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11759. if (!active_panel)
  11760. pt_debug(cd->dev, DL_ERROR,
  11761. "%s: Active panel not registered!\n", __func__);
  11762. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11763. if (!cd->pt_workqueue) {
  11764. pt_debug(cd->dev, DL_ERROR,
  11765. "%s: worker thread creation failed !\n", __func__);
  11766. }
  11767. if (cd->pt_workqueue) {
  11768. INIT_WORK(&cd->resume_work, pt_resume_work);
  11769. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11770. }
  11771. if (active_panel &&
  11772. drm_panel_notifier_register(active_panel,
  11773. &cd->fb_notifier) < 0)
  11774. pt_debug(cd->dev, DL_ERROR,
  11775. "%s: Register notifier failed!\n", __func__);
  11776. }
  11777. #endif
  11778. #elif defined(CONFIG_FB)
  11779. /*******************************************************************************
  11780. * FUNCTION: fb_notifier_callback
  11781. *
  11782. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11783. * resume/suspend attention list.
  11784. *
  11785. * RETURN:
  11786. * 0 = success
  11787. *
  11788. * PARAMETERS:
  11789. * *self - pointer to notifier_block structure
  11790. * event - event type of fb notifier
  11791. * *data - pointer to fb_event structure
  11792. ******************************************************************************/
  11793. static int fb_notifier_callback(struct notifier_block *self,
  11794. unsigned long event, void *data)
  11795. {
  11796. struct pt_core_data *cd =
  11797. container_of(self, struct pt_core_data, fb_notifier);
  11798. struct fb_event *evdata = data;
  11799. int *blank;
  11800. if (event != FB_EVENT_BLANK || !evdata)
  11801. goto exit;
  11802. blank = evdata->data;
  11803. if (*blank == FB_BLANK_UNBLANK) {
  11804. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11805. if (cd->fb_state != FB_ON) {
  11806. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11807. #if defined(CONFIG_PM_SLEEP)
  11808. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11809. pt_core_resume_(cd->dev);
  11810. #endif
  11811. cd->fb_state = FB_ON;
  11812. }
  11813. } else if (*blank == FB_BLANK_POWERDOWN) {
  11814. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11815. if (cd->fb_state != FB_OFF) {
  11816. #if defined(CONFIG_PM_SLEEP)
  11817. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11818. pt_core_suspend_(cd->dev);
  11819. #endif
  11820. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11821. cd->fb_state = FB_OFF;
  11822. }
  11823. }
  11824. exit:
  11825. return 0;
  11826. }
  11827. /*******************************************************************************
  11828. * FUNCTION: pt_setup_fb_notifier
  11829. *
  11830. * SUMMARY: Set up call back function into fb notifier.
  11831. *
  11832. * PARAMETERS:
  11833. * *cd - pointer to core data
  11834. ******************************************************************************/
  11835. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11836. {
  11837. int rc = 0;
  11838. cd->fb_state = FB_ON;
  11839. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11840. rc = fb_register_client(&cd->fb_notifier);
  11841. if (rc)
  11842. pt_debug(cd->dev, DL_ERROR,
  11843. "Unable to register fb_notifier: %d\n", rc);
  11844. }
  11845. #endif
  11846. /*******************************************************************************
  11847. * FUNCTION: pt_watchdog_work
  11848. *
  11849. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11850. * sleeping then this function simply returns. If the DUT is awake the
  11851. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11852. * somehow a response is waiting on the DUT that has not been read. If
  11853. * this occurs the message is simply consumed. If or once the IRQ is
  11854. * cleared, a PIP PING message is sent to the DUT and if the response
  11855. * is received the watchdog succeeds and exits, if no response is seen
  11856. * a startup is queued unless the maximum number of startups have already
  11857. * been attempted, in that case a BL is attempted.
  11858. *
  11859. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11860. * work thread
  11861. *
  11862. * RETURN: void
  11863. *
  11864. * PARAMETERS:
  11865. * *work - pointer to a work structure for the watchdog work queue
  11866. ******************************************************************************/
  11867. static void pt_watchdog_work(struct work_struct *work)
  11868. {
  11869. int rc = 0;
  11870. struct pt_core_data *cd = container_of(work,
  11871. struct pt_core_data, watchdog_work);
  11872. /*
  11873. * if found the current sleep_state is SS_SLEEPING
  11874. * then no need to request_exclusive, directly return
  11875. */
  11876. if (cd->sleep_state == SS_SLEEPING)
  11877. return;
  11878. #ifdef TTDL_DIAGNOSTICS
  11879. cd->watchdog_count++;
  11880. #endif /* TTDL_DIAGNOSTICS */
  11881. /*
  11882. * The first WD interval was extended to allow DDI to come up.
  11883. * If the WD interval is not the default then adjust timer to the
  11884. * current setting. The user can override value with drv_debug sysfs.
  11885. */
  11886. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11887. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11888. msecs_to_jiffies(cd->watchdog_interval));
  11889. }
  11890. if (pt_check_irq_asserted(cd)) {
  11891. #ifdef TTDL_DIAGNOSTICS
  11892. cd->watchdog_irq_stuck_count++;
  11893. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11894. #endif /* TTDL_DIAGNOSTICS */
  11895. pt_debug(cd->dev, DL_ERROR,
  11896. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11897. __func__);
  11898. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11899. }
  11900. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11901. if (rc < 0) {
  11902. pt_debug(cd->dev, DL_ERROR,
  11903. "%s: fail get exclusive ex=%p own=%p\n",
  11904. __func__, cd->exclusive_dev, cd->dev);
  11905. goto queue_startup;
  11906. }
  11907. rc = pt_pip_null_(cd);
  11908. if (release_exclusive(cd, cd->dev) < 0)
  11909. pt_debug(cd->dev, DL_ERROR,
  11910. "%s: fail to release exclusive\n", __func__);
  11911. queue_startup:
  11912. if (rc) {
  11913. #ifdef TTDL_DIAGNOSTICS
  11914. cd->watchdog_failed_access_count++;
  11915. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11916. #endif /* TTDL_DIAGNOSTICS */
  11917. pt_debug(cd->dev, DL_ERROR,
  11918. "%s: failed to access device in WD, retry count=%d\n",
  11919. __func__, cd->startup_retry_count);
  11920. /* Already tried FW upgrade because of watchdog but failed */
  11921. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11922. return;
  11923. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11924. /*
  11925. * Any wrapper function that trys to disable the
  11926. * WD killing this worker cannot be called here.
  11927. */
  11928. rc = request_exclusive(cd, cd->dev,
  11929. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11930. if (rc < 0) {
  11931. pt_debug(cd->dev, DL_ERROR,
  11932. "%s: fail get exclusive ex=%p own=%p\n",
  11933. __func__, cd->exclusive_dev, cd->dev);
  11934. goto exit;
  11935. }
  11936. cd->hw_detected = false;
  11937. cd->startup_status = STARTUP_STATUS_START;
  11938. pt_debug(cd->dev, DL_DEBUG,
  11939. "%s: Startup Status Reset\n", __func__);
  11940. rc = pt_dut_reset_and_wait(cd);
  11941. if (release_exclusive(cd, cd->dev) < 0)
  11942. pt_debug(cd->dev, DL_ERROR,
  11943. "%s: fail to release exclusive\n",
  11944. __func__);
  11945. if (!rc) {
  11946. cd->hw_detected = true;
  11947. if (!cd->flashless_dut)
  11948. pt_queue_enum(cd);
  11949. }
  11950. #ifdef TTDL_DIAGNOSTICS
  11951. cd->wd_xres_count++;
  11952. pt_debug(cd->dev, DL_ERROR,
  11953. "%s: Comm Failed - DUT reset [#%d]\n",
  11954. __func__, cd->wd_xres_count);
  11955. #endif /* TTDL_DIAGNOSTICS */
  11956. } else {
  11957. /*
  11958. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11959. * reset the part to regain communications, try to BL
  11960. */
  11961. pt_debug(cd->dev, DL_ERROR,
  11962. "%s: WD DUT access failure, Start FW Upgrade\n",
  11963. __func__);
  11964. #ifdef TTDL_DIAGNOSTICS
  11965. /*
  11966. * When diagnostics is enabled allow TTDL to keep
  11967. * trying to find the DUT. This allows the DUT to be
  11968. * hot swap-able while the host stays running. In
  11969. * production this may not be wanted as a customer
  11970. * may have several touch drivers and any driver
  11971. * that doesn't match the current DUT should give
  11972. * up trying and give up using the bus.
  11973. */
  11974. pt_debug(cd->dev, DL_INFO,
  11975. "%s: Resetting startup_retry_count\n",
  11976. __func__);
  11977. cd->startup_retry_count = 0;
  11978. #endif /* TTDL_DIAGNOSTICS */
  11979. /*
  11980. * Since fw may be broken,reset sysinfo ready flag
  11981. * to let upgrade function work.
  11982. */
  11983. mutex_lock(&cd->system_lock);
  11984. cd->sysinfo.ready = false;
  11985. mutex_unlock(&cd->system_lock);
  11986. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11987. pt_debug(cd->dev, DL_ERROR,
  11988. "%s: Queue Restart\n", __func__);
  11989. pt_queue_restart(cd);
  11990. } else
  11991. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11992. }
  11993. } else {
  11994. cd->hw_detected = true;
  11995. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11996. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11997. pt_debug(cd->dev, DL_ERROR,
  11998. "%s: HW detected but not enumerated\n",
  11999. __func__);
  12000. pt_queue_enum(cd);
  12001. }
  12002. }
  12003. exit:
  12004. pt_start_wd_timer(cd);
  12005. }
  12006. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  12007. /*******************************************************************************
  12008. * FUNCTION: pt_watchdog_timer
  12009. *
  12010. * SUMMARY: The function that is called when the WD timer expires. If the
  12011. * watchdog work is not already busy schedule the watchdog work queue.
  12012. *
  12013. * RETURN: void
  12014. *
  12015. * PARAMETERS:
  12016. * handle - Handle to the watchdog timer
  12017. ******************************************************************************/
  12018. static void pt_watchdog_timer(unsigned long handle)
  12019. {
  12020. struct pt_core_data *cd = (struct pt_core_data *)handle;
  12021. if (!cd)
  12022. return;
  12023. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12024. __func__);
  12025. if (!work_pending(&cd->watchdog_work))
  12026. schedule_work(&cd->watchdog_work);
  12027. }
  12028. #else
  12029. /*******************************************************************************
  12030. * FUNCTION: pt_watchdog_timer
  12031. *
  12032. * SUMMARY: The function that is called when the WD timer expires. If the
  12033. * watchdog work is not already busy schedule the watchdog work queue.
  12034. *
  12035. * RETURN: void
  12036. *
  12037. * PARAMETERS:
  12038. * *t - Pointer to timer list
  12039. ******************************************************************************/
  12040. static void pt_watchdog_timer(struct timer_list *t)
  12041. {
  12042. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  12043. if (!cd)
  12044. return;
  12045. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12046. __func__);
  12047. if (!work_pending(&cd->watchdog_work))
  12048. schedule_work(&cd->watchdog_work);
  12049. }
  12050. #endif
  12051. /*******************************************************************************
  12052. * Core sysfs show and store functions
  12053. ******************************************************************************/
  12054. /*******************************************************************************
  12055. * FUNCTION: pt_hw_version_show
  12056. *
  12057. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  12058. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  12059. *
  12060. * RETURN: size of data written to sysfs node
  12061. *
  12062. * PARAMETERS:
  12063. * *dev - pointer to device structure
  12064. * *attr - pointer to device attributes structure
  12065. * *buf - pointer to print output buffer
  12066. ******************************************************************************/
  12067. static ssize_t pt_hw_version_show(struct device *dev,
  12068. struct device_attribute *attr, char *buf)
  12069. {
  12070. struct pt_core_data *cd = dev_get_drvdata(dev);
  12071. _pt_request_hw_version(dev, cd->hw_version);
  12072. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  12073. }
  12074. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  12075. /*******************************************************************************
  12076. * FUNCTION: pt_drv_version_show
  12077. *
  12078. * SUMMARY: Show method for the drv_version sysfs node that will show the
  12079. * TTDL version information
  12080. *
  12081. * RETURN: Char buffer with printed TTDL version information
  12082. *
  12083. * PARAMETERS:
  12084. * *dev - pointer to device structure
  12085. * *attr - pointer to device attributes
  12086. * *buf - pointer to output buffer
  12087. ******************************************************************************/
  12088. static ssize_t pt_drv_version_show(struct device *dev,
  12089. struct device_attribute *attr, char *buf)
  12090. {
  12091. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12092. "Driver: %s\nVersion: %s\nDate: %s\n",
  12093. pt_driver_core_name, pt_driver_core_version,
  12094. pt_driver_core_date);
  12095. }
  12096. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  12097. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  12098. /*******************************************************************************
  12099. * FUNCTION: pt_fw_version_show
  12100. *
  12101. * SUMMARY: Show method for the fw_version sysfs node that will
  12102. * show the firmware, bootloader and PIP version information
  12103. *
  12104. * RETURN: Size of printed buffer
  12105. *
  12106. * PARAMETERS:
  12107. * *dev - pointer to device structure
  12108. * *attr - pointer to device attributes
  12109. * *buf - pointer to output buffer
  12110. ******************************************************************************/
  12111. static ssize_t pt_fw_version_show(struct device *dev,
  12112. struct device_attribute *attr, char *buf)
  12113. {
  12114. struct pt_core_data *cd = dev_get_drvdata(dev);
  12115. struct pt_ttdata *ttdata;
  12116. int rc = 0;
  12117. if (cd->mode == PT_MODE_OPERATIONAL)
  12118. rc = pt_hid_output_get_sysinfo_(cd);
  12119. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12120. __func__, cd->mode, cd->sysinfo.ready);
  12121. if (cd->sysinfo.ready)
  12122. ttdata = &cd->sysinfo.ttdata;
  12123. else
  12124. rc = -ENODATA;
  12125. if (!rc) {
  12126. return scnprintf(buf, strlen(buf),
  12127. "Status: %d\n"
  12128. "FW : %d.%d.%d\n"
  12129. "Config: %d\n"
  12130. "BL : %d.%d\n"
  12131. "PIP : %d.%d\n",
  12132. rc,
  12133. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  12134. ttdata->revctrl,
  12135. ttdata->fw_ver_conf,
  12136. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  12137. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  12138. } else {
  12139. return scnprintf(buf, strlen(buf),
  12140. "Status: %d\n"
  12141. "FW : n/a\n"
  12142. "Config: n/a\n"
  12143. "BL : n/a\n"
  12144. "PIP : n/a\n",
  12145. rc);
  12146. }
  12147. }
  12148. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  12149. /*******************************************************************************
  12150. * FUNCTION: pt_sysinfo_show
  12151. *
  12152. * SUMMARY: Show method for the sysinfo sysfs node that will
  12153. * show all the information from get system information command.
  12154. *
  12155. * RETURN: Size of printed buffer
  12156. *
  12157. * PARAMETERS:
  12158. * *dev - pointer to device structure
  12159. * *attr - pointer to device attributes
  12160. * *buf - pointer to output buffer
  12161. ******************************************************************************/
  12162. static ssize_t pt_sysinfo_show(struct device *dev,
  12163. struct device_attribute *attr, char *buf)
  12164. {
  12165. struct pt_core_data *cd = dev_get_drvdata(dev);
  12166. struct pt_sysinfo *si;
  12167. struct pt_ttdata *ttdata = NULL;
  12168. struct pt_sensing_conf_data *scd = NULL;
  12169. int rc = 0;
  12170. if (cd->mode == PT_MODE_OPERATIONAL) {
  12171. rc = pt_hid_output_get_sysinfo_(cd);
  12172. if (cd->sysinfo.ready) {
  12173. si = &cd->sysinfo;
  12174. ttdata = &si->ttdata;
  12175. scd = &si->sensing_conf_data;
  12176. } else
  12177. rc = -ENODATA;
  12178. } else
  12179. rc = -EPERM;
  12180. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12181. __func__, cd->mode, cd->sysinfo.ready);
  12182. if (!rc && ttdata && scd) {
  12183. return scnprintf(buf, strlen(buf),
  12184. "Status: %d\n"
  12185. "pip_ver_major: 0x%02X\n"
  12186. "pip_ver_minor: 0x%02X\n"
  12187. "fw_pid : 0x%04X\n"
  12188. "fw_ver_major : 0x%02X\n"
  12189. "fw_ver_minor : 0x%02X\n"
  12190. "revctrl : 0x%08X\n"
  12191. "fw_ver_conf : 0x%04X\n"
  12192. "bl_ver_major : 0x%02X\n"
  12193. "bl_ver_minor : 0x%02X\n"
  12194. "jtag_id_h : 0x%04X\n"
  12195. "jtag_id_l : 0x%04X\n"
  12196. "mfg_id[0] : 0x%02X\n"
  12197. "mfg_id[1] : 0x%02X\n"
  12198. "mfg_id[2] : 0x%02X\n"
  12199. "mfg_id[3] : 0x%02X\n"
  12200. "mfg_id[4] : 0x%02X\n"
  12201. "mfg_id[5] : 0x%02X\n"
  12202. "mfg_id[6] : 0x%02X\n"
  12203. "mfg_id[7] : 0x%02X\n"
  12204. "post_code : 0x%04X\n"
  12205. "electrodes_x : 0x%02X\n"
  12206. "electrodes_y : 0x%02X\n"
  12207. "len_x : 0x%04X\n"
  12208. "len_y : 0x%04X\n"
  12209. "res_x : 0x%04X\n"
  12210. "res_y : 0x%04X\n"
  12211. "max_z : 0x%04X\n"
  12212. "origin_x : 0x%02X\n"
  12213. "origin_y : 0x%02X\n"
  12214. "panel_id : 0x%02X\n"
  12215. "btn : 0x%02X\n"
  12216. "scan_mode : 0x%02X\n"
  12217. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12218. rc,
  12219. ttdata->pip_ver_major,
  12220. ttdata->pip_ver_minor,
  12221. ttdata->fw_pid,
  12222. ttdata->fw_ver_major,
  12223. ttdata->fw_ver_minor,
  12224. ttdata->revctrl,
  12225. ttdata->fw_ver_conf,
  12226. ttdata->bl_ver_major,
  12227. ttdata->bl_ver_minor,
  12228. ttdata->jtag_id_h,
  12229. ttdata->jtag_id_l,
  12230. ttdata->mfg_id[0],
  12231. ttdata->mfg_id[1],
  12232. ttdata->mfg_id[2],
  12233. ttdata->mfg_id[3],
  12234. ttdata->mfg_id[4],
  12235. ttdata->mfg_id[5],
  12236. ttdata->mfg_id[6],
  12237. ttdata->mfg_id[7],
  12238. ttdata->post_code,
  12239. scd->electrodes_x,
  12240. scd->electrodes_y,
  12241. scd->len_x,
  12242. scd->len_y,
  12243. scd->res_x,
  12244. scd->res_y,
  12245. scd->max_z,
  12246. scd->origin_x,
  12247. scd->origin_y,
  12248. scd->panel_id,
  12249. scd->btn,
  12250. scd->scan_mode,
  12251. scd->max_tch);
  12252. } else {
  12253. return scnprintf(buf, strlen(buf),
  12254. "Status: %d\n",
  12255. rc);
  12256. }
  12257. }
  12258. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12259. /*******************************************************************************
  12260. * FUNCTION: pt_hw_reset_show
  12261. *
  12262. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12263. * by toggling the XRES line and then calls the startup function to
  12264. * allow TTDL to re-enumerate the DUT.
  12265. * The printed value reflects the status of the full reset/enum.
  12266. *
  12267. * PARAMETERS:
  12268. * *dev - pointer to Device structure
  12269. * *attr - pointer to the device attribute structure
  12270. * *buf - pointer to buffer to print
  12271. ******************************************************************************/
  12272. static ssize_t pt_hw_reset_show(struct device *dev,
  12273. struct device_attribute *attr, char *buf)
  12274. {
  12275. struct pt_core_data *cd = dev_get_drvdata(dev);
  12276. int rc = 0;
  12277. int time = 0;
  12278. u8 reset_status = 0;
  12279. int t;
  12280. struct pt_hid_desc hid_desc;
  12281. memset(&hid_desc, 0, sizeof(hid_desc));
  12282. /* Only allow DUT reset if no active BL in progress */
  12283. mutex_lock(&cd->firmware_class_lock);
  12284. mutex_lock(&cd->system_lock);
  12285. cd->startup_state = STARTUP_NONE;
  12286. mutex_unlock(&(cd->system_lock));
  12287. pt_stop_wd_timer(cd);
  12288. /* ensure no left over exclusive access is still locked */
  12289. release_exclusive(cd, cd->dev);
  12290. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12291. if (rc) {
  12292. mutex_unlock(&cd->firmware_class_lock);
  12293. pt_debug(cd->dev, DL_ERROR,
  12294. "%s: HW reset failed rc = %d\n", __func__, rc);
  12295. goto exit_hw_reset;
  12296. }
  12297. reset_status |= 0x01 << 0;
  12298. if (cd->flashless_dut) {
  12299. mutex_unlock(&cd->firmware_class_lock);
  12300. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12301. msecs_to_jiffies(200));
  12302. if (IS_TMO(t)) {
  12303. pt_debug(dev, DL_ERROR,
  12304. "%s: Timeout waiting for FW update",
  12305. __func__);
  12306. rc = -ETIME;
  12307. goto exit_hw_reset;
  12308. } else {
  12309. pt_debug(dev, DL_INFO,
  12310. "%s: ----- Wait FW Loading ----",
  12311. __func__);
  12312. rc = _pt_request_wait_for_enum_state(
  12313. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12314. if (rc) {
  12315. pt_debug(cd->dev, DL_ERROR,
  12316. "%s: No FW Sentinel detected rc = %d\n",
  12317. __func__, rc);
  12318. goto exit_hw_reset;
  12319. }
  12320. reset_status |= 0x01 << 1;
  12321. }
  12322. } else {
  12323. /* Wait for any sentinel */
  12324. rc = _pt_request_wait_for_enum_state(dev, 150,
  12325. STARTUP_STATUS_BL_RESET_SENTINEL |
  12326. STARTUP_STATUS_FW_RESET_SENTINEL);
  12327. if (rc) {
  12328. mutex_unlock(&cd->firmware_class_lock);
  12329. pt_debug(cd->dev, DL_ERROR,
  12330. "%s: No Sentinel detected rc = %d\n",
  12331. __func__, rc);
  12332. goto exit_hw_reset;
  12333. }
  12334. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12335. msleep(30);
  12336. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12337. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12338. if (rc < 0) {
  12339. pt_debug(cd->dev, DL_ERROR,
  12340. "%s: Error on getting HID descriptor r=%d\n",
  12341. __func__, rc);
  12342. goto exit_hw_reset;
  12343. }
  12344. cd->mode = pt_get_mode(cd, &hid_desc);
  12345. if (cd->mode == PT_MODE_BOOTLOADER)
  12346. rc = pt_hid_output_bl_launch_app_(cd);
  12347. } else {
  12348. if (cd->mode == PT_MODE_BOOTLOADER)
  12349. rc = pt_pip2_launch_app(dev,
  12350. PT_CORE_CMD_UNPROTECTED);
  12351. }
  12352. if (rc) {
  12353. mutex_unlock(&cd->firmware_class_lock);
  12354. pt_debug(cd->dev, DL_ERROR,
  12355. "%s: PIP launch app failed rc = %d\n",
  12356. __func__, rc);
  12357. goto exit_hw_reset;
  12358. }
  12359. mutex_unlock(&cd->firmware_class_lock);
  12360. reset_status |= 0x01 << 1;
  12361. msleep(20);
  12362. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12363. (cd->mode != PT_MODE_OPERATIONAL))
  12364. pt_queue_restart(cd);
  12365. else
  12366. pt_queue_enum(cd);
  12367. }
  12368. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12369. msleep(50);
  12370. pt_debug(cd->dev, DL_INFO,
  12371. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12372. __func__, time, STARTUP_STATUS_COMPLETE,
  12373. cd->startup_status);
  12374. time += 50;
  12375. }
  12376. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12377. rc = -ETIME;
  12378. goto exit_hw_reset;
  12379. }
  12380. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12381. __func__, cd->startup_status);
  12382. reset_status |= 0x01 << 2;
  12383. pt_start_wd_timer(cd);
  12384. exit_hw_reset:
  12385. return scnprintf(buf, strlen(buf),
  12386. "Status: %d\n"
  12387. "Reset Status: 0x%02X\n", rc, reset_status);
  12388. }
  12389. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12390. /*******************************************************************************
  12391. * FUNCTION: pt_pip2_cmd_rsp_store
  12392. *
  12393. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12394. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12395. * Command byte order:
  12396. * Byte [0] - PIP2 command ID
  12397. * Byte [1-n] - PIP2 command payload
  12398. *
  12399. * RETURN: Size of passed in buffer
  12400. *
  12401. * PARAMETERS:
  12402. * *dev - pointer to device structure
  12403. * *attr - pointer to device attributes
  12404. * *buf - pointer to buffer that hold the command parameters
  12405. * size - size of buf
  12406. ******************************************************************************/
  12407. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12408. struct device_attribute *attr, const char *buf, size_t size)
  12409. {
  12410. struct pt_core_data *cd = dev_get_drvdata(dev);
  12411. u16 actual_read_len;
  12412. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12413. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12414. u8 pip2_cmd_id = 0x00;
  12415. u8 *pip2_cmd_data = NULL;
  12416. int data_len = 0;
  12417. int length;
  12418. int rc = 0;
  12419. /* clear shared data */
  12420. mutex_lock(&cd->sysfs_lock);
  12421. cd->raw_cmd_status = 0;
  12422. cd->cmd_rsp_buf_len = 0;
  12423. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12424. mutex_unlock(&cd->sysfs_lock);
  12425. length = _pt_ic_parse_input_hex(dev, buf, size,
  12426. input_data, PT_MAX_PIP2_MSG_SIZE);
  12427. if (length <= 0 || length > (PT_MAX_PIP2_MSG_SIZE - 2)) {
  12428. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12429. __func__);
  12430. rc = -EINVAL;
  12431. goto exit;
  12432. }
  12433. /* Send PIP2 command if enough data was provided */
  12434. if (length >= 1) {
  12435. pip2_cmd_id = input_data[0];
  12436. pip2_cmd_data = &input_data[1];
  12437. data_len = length - 1;
  12438. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12439. pip2_cmd_id, pip2_cmd_data, data_len,
  12440. read_buf, &actual_read_len);
  12441. cd->raw_cmd_status = rc;
  12442. if (rc) {
  12443. pt_debug(dev, DL_ERROR,
  12444. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12445. __func__, pip2_cmd_id, rc);
  12446. goto exit;
  12447. } else {
  12448. cd->cmd_rsp_buf_len = actual_read_len;
  12449. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12450. pt_debug(dev, DL_ERROR,
  12451. "%s: PIP2 actual_read_len = %d\n",
  12452. __func__, actual_read_len);
  12453. }
  12454. } else {
  12455. rc = -EINVAL;
  12456. pt_debug(dev, DL_ERROR,
  12457. "%s: Insufficient data provided for PIP2 cmd\n",
  12458. __func__);
  12459. }
  12460. exit:
  12461. if (rc)
  12462. return rc;
  12463. return size;
  12464. }
  12465. /*******************************************************************************
  12466. * FUNCTION: pt_pip2_cmd_rsp_show
  12467. *
  12468. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12469. * response generated after using the store method of the pip2_cmd_rsp
  12470. * sysfs node, are available to be read here.
  12471. *
  12472. * PARAMETERS:
  12473. * *dev - pointer to Device structure
  12474. * *attr - pointer to the device attribute structure
  12475. * *buf - pointer to buffer to print
  12476. ******************************************************************************/
  12477. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12478. struct device_attribute *attr, char *buf)
  12479. {
  12480. struct pt_core_data *cd = dev_get_drvdata(dev);
  12481. int i;
  12482. ssize_t data_len;
  12483. int index;
  12484. mutex_lock(&cd->sysfs_lock);
  12485. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12486. "Status: %d\n", cd->raw_cmd_status);
  12487. if (cd->raw_cmd_status)
  12488. goto error;
  12489. /* Remove the CRC from the length of the response */
  12490. data_len = cd->cmd_rsp_buf_len - 2;
  12491. /* Start printing from the data payload */
  12492. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12493. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12494. "%02X ", cd->cmd_rsp_buf[i]);
  12495. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12496. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12497. "\n(%zd bytes)\n",
  12498. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12499. } else {
  12500. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12501. "\n(%zd bytes)\n", 0);
  12502. }
  12503. error:
  12504. mutex_unlock(&cd->sysfs_lock);
  12505. return index;
  12506. }
  12507. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12508. pt_pip2_cmd_rsp_store);
  12509. /*******************************************************************************
  12510. * FUNCTION: pt_command_store
  12511. *
  12512. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12513. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12514. * TTDL will not parse the command.
  12515. *
  12516. * RETURN: Size of passed in buffer
  12517. *
  12518. * PARAMETERS:
  12519. * *dev - pointer to device structure
  12520. * *attr - pointer to device attributes
  12521. * *buf - pointer to buffer that hold the command parameters
  12522. * size - size of buf
  12523. ******************************************************************************/
  12524. static ssize_t pt_command_store(struct device *dev,
  12525. struct device_attribute *attr, const char *buf, size_t size)
  12526. {
  12527. struct pt_core_data *cd = dev_get_drvdata(dev);
  12528. unsigned short crc;
  12529. u16 actual_read_len;
  12530. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12531. int length;
  12532. int len_field;
  12533. int rc = 0;
  12534. mutex_lock(&cd->sysfs_lock);
  12535. cd->cmd_rsp_buf_len = 0;
  12536. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12537. mutex_unlock(&cd->sysfs_lock);
  12538. length = _pt_ic_parse_input_hex(dev, buf, size,
  12539. input_data, PT_MAX_PIP2_MSG_SIZE);
  12540. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12541. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12542. __func__);
  12543. rc = -EINVAL;
  12544. goto pt_command_store_exit;
  12545. }
  12546. /* PIP2 messages begin with 01 01 */
  12547. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12548. cd->pip2_prot_active = 1;
  12549. /* Override next seq tag with what was sent */
  12550. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12551. /* For PIP2 cmd if length does not include crc, add it */
  12552. len_field = (input_data[3] << 8) | input_data[2];
  12553. if (len_field == length && length <= 254) {
  12554. crc = crc_ccitt_calculate(&input_data[2],
  12555. length - 2);
  12556. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12557. __func__, length, crc);
  12558. input_data[length] = (crc & 0xFF00) >> 8;
  12559. input_data[length + 1] = crc & 0x00FF;
  12560. length = length + 2;
  12561. }
  12562. }
  12563. /* write PIP command to log */
  12564. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12565. pm_runtime_get_sync(dev);
  12566. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12567. length, input_data, &actual_read_len);
  12568. pm_runtime_put(dev);
  12569. mutex_lock(&cd->sysfs_lock);
  12570. cd->raw_cmd_status = rc;
  12571. if (rc) {
  12572. cd->cmd_rsp_buf_len = 0;
  12573. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12574. __func__, buf);
  12575. } else {
  12576. cd->cmd_rsp_buf_len = actual_read_len;
  12577. }
  12578. cd->pip2_prot_active = 0;
  12579. mutex_unlock(&cd->sysfs_lock);
  12580. pt_command_store_exit:
  12581. if (rc)
  12582. return rc;
  12583. return size;
  12584. }
  12585. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12586. /*******************************************************************************
  12587. * FUNCTION: pt_response_show
  12588. *
  12589. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12590. * response generated after using the pt_command_store sysfs node, are
  12591. * available to be read here.
  12592. *
  12593. * PARAMETERS:
  12594. * *dev - pointer to Device structure
  12595. * *attr - pointer to the device attribute structure
  12596. * *buf - pointer to buffer to print
  12597. ******************************************************************************/
  12598. static ssize_t pt_response_show(struct device *dev,
  12599. struct device_attribute *attr, char *buf)
  12600. {
  12601. struct pt_core_data *cd = dev_get_drvdata(dev);
  12602. int i;
  12603. ssize_t num_read;
  12604. int index;
  12605. mutex_lock(&cd->sysfs_lock);
  12606. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12607. "Status: %d\n", cd->raw_cmd_status);
  12608. if (cd->raw_cmd_status)
  12609. goto error;
  12610. num_read = cd->cmd_rsp_buf_len;
  12611. for (i = 0; i < num_read; i++)
  12612. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12613. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12614. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12615. "(%zd bytes)\n", num_read);
  12616. error:
  12617. mutex_unlock(&cd->sysfs_lock);
  12618. return index;
  12619. }
  12620. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12621. /*******************************************************************************
  12622. * FUNCTION: pt_dut_debug_show
  12623. *
  12624. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12625. * are available for the store method.
  12626. *
  12627. * RETURN:
  12628. * 0 = success
  12629. * !0 = failure
  12630. *
  12631. * PARAMETERS:
  12632. * *dev - pointer to device structure
  12633. * *attr - pointer to device attributes
  12634. * *buf - pointer to output buffer
  12635. ******************************************************************************/
  12636. static ssize_t pt_dut_debug_show(struct device *dev,
  12637. struct device_attribute *attr, char *buf)
  12638. {
  12639. ssize_t ret;
  12640. ret = scnprintf(buf, strlen(buf),
  12641. "Status: 0\n"
  12642. "dut_debug sends the following commands to the DUT:\n"
  12643. "%d %s \t- %s\n"
  12644. "%d %s \t- %s\n"
  12645. "%d %s \t- %s\n"
  12646. "%d %s \t- %s\n"
  12647. "%d %s \t- %s\n"
  12648. "%d %s \t- %s\n"
  12649. "%d %s \t- %s\n"
  12650. "%d %s \t- %s\n"
  12651. "%d %s \t- %s\n"
  12652. "%d %s \t- %s\n"
  12653. "%d %s \t- %s\n"
  12654. "%d %s \t- %s\n"
  12655. "%d %s \t- %s\n"
  12656. "%d %s \t- %s\n"
  12657. "%d %s \t- %s\n"
  12658. "%d %s \t- %s\n"
  12659. "%d %s \t- %s\n"
  12660. ,
  12661. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12662. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12663. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12664. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12665. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12666. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12667. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12668. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12669. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12670. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12671. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12672. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12673. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12674. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12675. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12676. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12677. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12678. );
  12679. return ret;
  12680. }
  12681. /*******************************************************************************
  12682. * FUNCTION: pt_drv_debug_show
  12683. *
  12684. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12685. * are available for the store method.
  12686. *
  12687. * RETURN:
  12688. * 0 = success
  12689. * !0 = failure
  12690. *
  12691. * PARAMETERS:
  12692. * *dev - pointer to device structure
  12693. * *attr - pointer to device attributes
  12694. * *buf - pointer to output buffer
  12695. ******************************************************************************/
  12696. static ssize_t pt_drv_debug_show(struct device *dev,
  12697. struct device_attribute *attr, char *buf)
  12698. {
  12699. ssize_t ret;
  12700. ret = scnprintf(buf, strlen(buf),
  12701. "Status: 0\n"
  12702. "drv_debug supports the following values:\n"
  12703. "%d %s \t- %s\n"
  12704. "%d %s \t- %s\n"
  12705. "%d %s \t- %s\n"
  12706. "%d %s \t- %s\n"
  12707. "%d %s \t- %s\n"
  12708. "%d %s \t- %s\n"
  12709. "%d %s \t- %s\n"
  12710. "%d %s \t- %s\n"
  12711. "%d %s \t- %s\n"
  12712. "%d %s - %s\n"
  12713. "%d %s \t- %s\n"
  12714. "%d %s \t- %s\n"
  12715. #ifdef TTDL_DIAGNOSTICS
  12716. "%d %s \t- %s\n"
  12717. "%d %s \t- %s\n"
  12718. "%d %s \t- %s\n"
  12719. #endif /* TTDL_DIAGNOSTICS */
  12720. "%d %s \t- %s\n"
  12721. #ifdef TTDL_DIAGNOSTICS
  12722. "%d %s \t- %s\n"
  12723. "%d %s \t- %s\n"
  12724. "%d %s \t- %s\n"
  12725. "%d %s \t- %s\n"
  12726. "%d %s \t- %s\n"
  12727. "%d %s \t- %s\n"
  12728. "%d %s \t- %s\n"
  12729. "%d %s \t- %s\n"
  12730. "%d %s \t- %s\n"
  12731. #endif /* TTDL_DIAGNOSTICS */
  12732. ,
  12733. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12734. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12735. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12736. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12737. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12738. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12739. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12740. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12741. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12742. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12743. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12744. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12745. #ifdef TTDL_DIAGNOSTICS
  12746. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12747. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12748. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12749. #endif /* TTDL_DIAGNOSTICS */
  12750. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12751. #ifdef TTDL_DIAGNOSTICS
  12752. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12753. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12754. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12755. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12756. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12757. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12758. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12759. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12760. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12761. "TTHE_TUNER HID USB Format"
  12762. #endif /* TTDL_DIAGNOSTICS */
  12763. );
  12764. return ret;
  12765. }
  12766. /*******************************************************************************
  12767. * FUNCTION: pt_drv_debug_store
  12768. *
  12769. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12770. * dut_debug. Drv_debug will contain all functionality that can be run
  12771. * without a DUT preset and is available anytime TTDL is running.
  12772. * Dut_debug requires a DUT to be available and will only be created after
  12773. * a DUT has been detected.
  12774. * This function will eventually be split into two but until the overlap
  12775. * has been depricated this function contains all commands that can be
  12776. * used for TTDL/DUT debugging status and control.
  12777. * All commands require at least one value to be passed in *buf with some
  12778. * requiring two.
  12779. *
  12780. * RETURN: Size of passed in buffer
  12781. *
  12782. * PARAMETERS:
  12783. * *dev - pointer to device structure
  12784. * *attr - pointer to device attributes
  12785. * *buf - pointer to buffer that hold the command parameters
  12786. * size - size of buf
  12787. ******************************************************************************/
  12788. static ssize_t pt_drv_debug_store(struct device *dev,
  12789. struct device_attribute *attr, const char *buf, size_t size)
  12790. {
  12791. struct pt_core_data *cd = dev_get_drvdata(dev);
  12792. unsigned long value;
  12793. int rc = 0;
  12794. u8 return_data[8];
  12795. static u8 wd_disabled;
  12796. u32 input_data[3];
  12797. int length;
  12798. #ifdef TTDL_DIAGNOSTICS
  12799. struct i2c_client *client = to_i2c_client(dev);
  12800. unsigned short crc = 0;
  12801. u16 cal_size;
  12802. #endif
  12803. input_data[0] = 0;
  12804. input_data[1] = 0;
  12805. /* Maximmum input is two elements */
  12806. length = _pt_ic_parse_input(dev, buf, size,
  12807. input_data, ARRAY_SIZE(input_data));
  12808. if (length < 1 || length > 2) {
  12809. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12810. __func__);
  12811. rc = -EINVAL;
  12812. goto pt_drv_debug_store_exit;
  12813. }
  12814. value = input_data[0];
  12815. if (length == 1) {
  12816. pt_debug(dev, DL_DEBUG,
  12817. "%s: Debug Cmd Received (id=%d)\n",
  12818. __func__, input_data[0]);
  12819. } else if (length == 2) {
  12820. pt_debug(dev, DL_DEBUG,
  12821. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12822. __func__, input_data[0], input_data[1]);
  12823. } else {
  12824. pt_debug(dev, DL_DEBUG,
  12825. "%s: Invalid arguments received\n", __func__);
  12826. rc = -EINVAL;
  12827. goto pt_drv_debug_store_exit;
  12828. }
  12829. /* Start watchdog timer command */
  12830. if (value == PT_DRV_DBG_START_WD) {
  12831. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12832. wd_disabled = 0;
  12833. cd->watchdog_force_stop = false;
  12834. pt_start_wd_timer(cd);
  12835. goto pt_drv_debug_store_exit;
  12836. }
  12837. /* Stop watchdog timer temporarily */
  12838. pt_stop_wd_timer(cd);
  12839. if (value == PT_DRV_DBG_STOP_WD) {
  12840. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12841. wd_disabled = 1;
  12842. cd->watchdog_force_stop = true;
  12843. goto pt_drv_debug_store_exit;
  12844. }
  12845. switch (value) {
  12846. case PT_DRV_DBG_SUSPEND: /* 4 */
  12847. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12848. wd_disabled = 1;
  12849. rc = pt_core_suspend_(cd->dev);
  12850. if (rc)
  12851. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12852. __func__, rc);
  12853. else {
  12854. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12855. __func__);
  12856. cd->drv_debug_suspend = true;
  12857. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12858. }
  12859. break;
  12860. case PT_DRV_DBG_RESUME: /* 5 */
  12861. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12862. rc = pt_core_resume_(cd->dev);
  12863. if (rc)
  12864. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12865. __func__, rc);
  12866. else {
  12867. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12868. __func__);
  12869. cd->drv_debug_suspend = false;
  12870. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12871. }
  12872. break;
  12873. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12874. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12875. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12876. break;
  12877. case PT_DUT_DBG_HID_RESET: /* 50 */
  12878. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12879. pt_hid_cmd_reset(cd);
  12880. break;
  12881. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12882. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12883. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12884. wd_disabled = 0;
  12885. break;
  12886. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12887. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12888. __func__);
  12889. wd_disabled = 1;
  12890. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12891. break;
  12892. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12893. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12894. __func__);
  12895. wd_disabled = 1;
  12896. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12897. break;
  12898. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12899. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12900. pt_hid_output_bl_get_information(cd, return_data);
  12901. break;
  12902. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12903. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12904. __func__);
  12905. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12906. break;
  12907. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12908. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12909. pt_hid_output_bl_launch_app(cd);
  12910. break;
  12911. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12912. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12913. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12914. break;
  12915. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12916. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12917. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12918. break;
  12919. case PT_DUT_DBG_RESET: /* 98 */
  12920. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12921. rc = pt_hw_hard_reset(cd);
  12922. break;
  12923. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12924. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12925. pt_pip_null(cd);
  12926. break;
  12927. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12928. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12929. rc = pt_pip_start_bootloader(cd);
  12930. if (!rc) {
  12931. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12932. cd->mode = PT_MODE_BOOTLOADER;
  12933. }
  12934. break;
  12935. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12936. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12937. pt_hid_output_get_sysinfo(cd);
  12938. break;
  12939. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12940. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12941. pt_pip_suspend_scanning(cd);
  12942. break;
  12943. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12944. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12945. pt_pip_resume_scanning(cd);
  12946. break;
  12947. #ifdef TTHE_TUNER_SUPPORT
  12948. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12949. cd->tthe_exit = 1;
  12950. wake_up(&cd->wait_q);
  12951. kfree(cd->tthe_buf);
  12952. cd->tthe_buf = NULL;
  12953. cd->tthe_exit = 0;
  12954. break;
  12955. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12956. if (cd->tthe_buf)
  12957. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12958. else
  12959. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12960. __func__);
  12961. break;
  12962. #endif
  12963. #ifdef TTDL_DIAGNOSTICS
  12964. case PT_DUT_DBG_HID_DESC: /* 109 */
  12965. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12966. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12967. break;
  12968. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12969. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12970. __func__);
  12971. pt_erase_parameter_list(cd);
  12972. break;
  12973. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12974. rc = pt_read_input(cd);
  12975. if (!rc)
  12976. pt_parse_input(cd);
  12977. break;
  12978. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12979. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12980. &cal_size, &crc);
  12981. if (rc)
  12982. pt_debug(dev, DL_ERROR,
  12983. "%s: CAL Data clear failed rc=%d\n",
  12984. __func__, rc);
  12985. else
  12986. pt_debug(dev, DL_INFO,
  12987. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12988. __func__, crc, size);
  12989. break;
  12990. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12991. mutex_lock(&cd->system_lock);
  12992. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12993. cd->debug_level = input_data[1];
  12994. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12995. __func__, cd->debug_level);
  12996. } else {
  12997. rc = -EINVAL;
  12998. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12999. __func__, input_data[1]);
  13000. }
  13001. mutex_unlock(&(cd->system_lock));
  13002. break;
  13003. #endif
  13004. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  13005. mutex_lock(&cd->system_lock);
  13006. if (input_data[1] > 100) {
  13007. cd->watchdog_interval = input_data[1];
  13008. pt_debug(dev, DL_INFO,
  13009. "%s: Set watchdog_ interval to: %d\n",
  13010. __func__, cd->watchdog_interval);
  13011. pt_start_wd_timer(cd);
  13012. } else {
  13013. rc = -EINVAL;
  13014. pt_debug(dev, DL_ERROR,
  13015. "%s: Invalid watchdog interval: %d\n",
  13016. __func__, input_data[1]);
  13017. }
  13018. mutex_unlock(&(cd->system_lock));
  13019. break;
  13020. #ifdef TTDL_DIAGNOSTICS
  13021. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  13022. mutex_lock(&cd->system_lock);
  13023. if (input_data[1] == 0) {
  13024. cd->show_timestamp = 0;
  13025. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  13026. __func__);
  13027. } else if (input_data[1] == 1) {
  13028. cd->show_timestamp = 1;
  13029. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  13030. __func__);
  13031. } else {
  13032. rc = -EINVAL;
  13033. pt_debug(dev, DL_ERROR,
  13034. "%s: Invalid parameter: %d\n",
  13035. __func__, input_data[1]);
  13036. }
  13037. mutex_unlock(&(cd->system_lock));
  13038. break;
  13039. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  13040. if (input_data[1] == 0) {
  13041. cd->cpdata->setup_power(cd->cpdata,
  13042. PT_MT_POWER_OFF, cd->dev);
  13043. pt_debug(dev, DL_INFO,
  13044. "%s: Initiate Power Off\n", __func__);
  13045. } else if (input_data[1] == 1) {
  13046. cd->cpdata->setup_power(cd->cpdata,
  13047. PT_MT_POWER_ON, cd->dev);
  13048. pt_debug(dev, DL_INFO,
  13049. "%s: Initiate Power On\n", __func__);
  13050. } else {
  13051. rc = -EINVAL;
  13052. pt_debug(dev, DL_ERROR,
  13053. "%s: Invalid parameter: %d\n",
  13054. __func__, input_data[1]);
  13055. }
  13056. break;
  13057. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  13058. if (input_data[1] == 0) {
  13059. pm_runtime_put(dev);
  13060. pt_debug(dev, DL_ERROR,
  13061. "%s: Force call pm_runtime_put()\n", __func__);
  13062. } else if (input_data[1] == 1) {
  13063. pm_runtime_get_sync(dev);
  13064. pt_debug(dev, DL_ERROR,
  13065. "%s: Force call pm_runtime_get_sync()\n",
  13066. __func__);
  13067. } else {
  13068. rc = -EINVAL;
  13069. pt_debug(dev, DL_ERROR,
  13070. "%s: WARNING: Invalid parameter: %d\n",
  13071. __func__, input_data[1]);
  13072. }
  13073. break;
  13074. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  13075. mutex_lock(&cd->system_lock);
  13076. if (input_data[1] == 0) {
  13077. cd->show_tt_data = false;
  13078. pt_debug(dev, DL_INFO,
  13079. "%s: Disable TT_DATA\n", __func__);
  13080. } else if (input_data[1] == 1) {
  13081. cd->show_tt_data = true;
  13082. pt_debug(dev, DL_INFO,
  13083. "%s: Enable TT_DATA\n", __func__);
  13084. } else {
  13085. rc = -EINVAL;
  13086. pt_debug(dev, DL_ERROR,
  13087. "%s: Invalid parameter: %d\n",
  13088. __func__, input_data[1]);
  13089. }
  13090. mutex_unlock(&(cd->system_lock));
  13091. break;
  13092. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  13093. if (input_data[1] == cd->active_dut_generation) {
  13094. mutex_lock(&cd->system_lock);
  13095. cd->set_dut_generation = true;
  13096. mutex_unlock(&(cd->system_lock));
  13097. } else {
  13098. mutex_lock(&cd->system_lock);
  13099. if (input_data[1] == 0) {
  13100. cd->active_dut_generation = DUT_UNKNOWN;
  13101. cd->set_dut_generation = false;
  13102. } else if (input_data[1] == 1) {
  13103. cd->active_dut_generation = DUT_PIP1_ONLY;
  13104. cd->set_dut_generation = true;
  13105. } else if (input_data[1] == 2) {
  13106. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  13107. cd->set_dut_generation = true;
  13108. } else {
  13109. rc = -EINVAL;
  13110. pt_debug(dev, DL_ERROR,
  13111. "%s: Invalid parameter: %d\n",
  13112. __func__, input_data[1]);
  13113. break;
  13114. }
  13115. cd->startup_status = STARTUP_STATUS_START;
  13116. pt_debug(cd->dev, DL_DEBUG,
  13117. "%s: Startup Status Reset\n", __func__);
  13118. mutex_unlock(&(cd->system_lock));
  13119. pt_debug(dev, DL_INFO,
  13120. "%s: Active DUT Generation Set to: %d\n",
  13121. __func__, cd->active_dut_generation);
  13122. /* Changing DUT generations full restart needed */
  13123. pt_queue_restart(cd);
  13124. }
  13125. break;
  13126. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  13127. mutex_lock(&cd->system_lock);
  13128. if (input_data[1] == 0) {
  13129. cd->bridge_mode = false;
  13130. pt_debug(dev, DL_INFO,
  13131. "%s: Disable Bridge Mode\n", __func__);
  13132. } else if (input_data[1] == 1) {
  13133. cd->bridge_mode = true;
  13134. pt_debug(dev, DL_INFO,
  13135. "%s: Enable Bridge Mode\n", __func__);
  13136. } else {
  13137. rc = -EINVAL;
  13138. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13139. __func__, input_data[1]);
  13140. }
  13141. mutex_unlock(&(cd->system_lock));
  13142. break;
  13143. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  13144. mutex_lock(&cd->system_lock);
  13145. /* Only a 7bit address is valid */
  13146. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  13147. client->addr = input_data[1];
  13148. pt_debug(dev, DL_INFO,
  13149. "%s: Set I2C Address: 0x%2X\n",
  13150. __func__, client->addr);
  13151. } else {
  13152. rc = -EINVAL;
  13153. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  13154. __func__, input_data[1]);
  13155. client->addr = 0x24;
  13156. }
  13157. mutex_unlock(&(cd->system_lock));
  13158. break;
  13159. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  13160. mutex_lock(&cd->system_lock);
  13161. if (input_data[1] == 0) {
  13162. cd->flashless_dut = 0;
  13163. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  13164. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  13165. __func__);
  13166. } else if (input_data[1] == 1) {
  13167. cd->flashless_dut = 1;
  13168. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  13169. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13170. __func__);
  13171. }
  13172. mutex_unlock(&(cd->system_lock));
  13173. break;
  13174. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13175. mutex_lock(&cd->system_lock);
  13176. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13177. cd->force_pip2_seq = input_data[1];
  13178. cd->pip2_cmd_tag_seq = input_data[1];
  13179. pt_debug(dev, DL_INFO,
  13180. "%s: Force PIP2 Seq to: 0x%02X\n",
  13181. __func__, input_data[1]);
  13182. } else {
  13183. cd->force_pip2_seq = 0;
  13184. pt_debug(dev, DL_INFO,
  13185. "%s: Clear Forced PIP2 Seq\n", __func__);
  13186. }
  13187. mutex_unlock(&(cd->system_lock));
  13188. break;
  13189. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13190. mutex_lock(&cd->system_lock);
  13191. if (input_data[1] == 0) {
  13192. cd->bl_with_no_int = 0;
  13193. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13194. } else if (input_data[1] == 1) {
  13195. cd->bl_with_no_int = 1;
  13196. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13197. __func__);
  13198. } else {
  13199. rc = -EINVAL;
  13200. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13201. __func__, input_data[1]);
  13202. }
  13203. mutex_unlock(&(cd->system_lock));
  13204. break;
  13205. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13206. mutex_lock(&cd->system_lock);
  13207. if (input_data[1] == 0) {
  13208. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13209. pt_debug(dev, DL_INFO,
  13210. "%s: Disable Calibration cache in host\n",
  13211. __func__);
  13212. } else if (input_data[1] == 1) {
  13213. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13214. pt_debug(dev, DL_INFO,
  13215. "%s: Enable Calibration cache in host\n",
  13216. __func__);
  13217. } else {
  13218. rc = -EINVAL;
  13219. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13220. __func__, input_data[1]);
  13221. }
  13222. mutex_unlock(&(cd->system_lock));
  13223. break;
  13224. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13225. mutex_lock(&cd->system_lock);
  13226. if (input_data[1] == 0) {
  13227. cd->multi_chip = PT_FEATURE_DISABLE;
  13228. cd->ttdl_bist_select = 0x07;
  13229. pt_debug(dev, DL_INFO,
  13230. "%s: Disable Multi-chip support\n", __func__);
  13231. } else if (input_data[1] == 1) {
  13232. cd->multi_chip = PT_FEATURE_ENABLE;
  13233. cd->ttdl_bist_select = 0x3F;
  13234. pt_debug(dev, DL_INFO,
  13235. "%s: Enable Multi-chip support\n", __func__);
  13236. } else {
  13237. rc = -EINVAL;
  13238. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13239. __func__, input_data[1]);
  13240. }
  13241. mutex_unlock(&(cd->system_lock));
  13242. break;
  13243. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13244. mutex_lock(&cd->system_lock);
  13245. if (input_data[1] <= 0x07) {
  13246. cd->panel_id_support = input_data[1];
  13247. pt_debug(dev, DL_INFO,
  13248. "%s: Set panel_id_support to %d\n",
  13249. __func__, cd->panel_id_support);
  13250. } else {
  13251. rc = -EINVAL;
  13252. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13253. __func__, input_data[1]);
  13254. }
  13255. mutex_unlock(&(cd->system_lock));
  13256. break;
  13257. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13258. mutex_lock(&cd->system_lock);
  13259. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13260. /*
  13261. * The timeout is changed for some cases so the
  13262. * pip_cmd_timeout_default is used to retore back to
  13263. * what the user requested as the new timeout.
  13264. */
  13265. cd->pip_cmd_timeout_default = input_data[1];
  13266. cd->pip_cmd_timeout = input_data[1];
  13267. pt_debug(dev, DL_INFO,
  13268. "%s: PIP Timeout = %d\n", __func__,
  13269. cd->pip_cmd_timeout_default);
  13270. } else {
  13271. rc = -EINVAL;
  13272. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13273. __func__, input_data[1]);
  13274. }
  13275. mutex_unlock(&(cd->system_lock));
  13276. break;
  13277. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13278. mutex_lock(&cd->system_lock);
  13279. if (input_data[1] == 0) {
  13280. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13281. pt_debug(dev, DL_INFO,
  13282. "%s: Disable tthe_tuner HID-USB format\n",
  13283. __func__);
  13284. } else if (input_data[1] == 1) {
  13285. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13286. pt_debug(dev, DL_INFO,
  13287. "%s: Enable tthe_tuner HID-USB format\n",
  13288. __func__);
  13289. } else {
  13290. rc = -EINVAL;
  13291. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13292. __func__, input_data[1]);
  13293. }
  13294. mutex_unlock(&(cd->system_lock));
  13295. break;
  13296. #endif /* TTDL_DIAGNOSTICS */
  13297. default:
  13298. rc = -EINVAL;
  13299. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13300. }
  13301. /* Enable watchdog timer */
  13302. if (!wd_disabled)
  13303. pt_start_wd_timer(cd);
  13304. pt_drv_debug_store_exit:
  13305. if (rc)
  13306. return rc;
  13307. return size;
  13308. }
  13309. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13310. pt_drv_debug_store);
  13311. /*******************************************************************************
  13312. * FUNCTION: pt_sleep_status_show
  13313. *
  13314. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13315. * sleep status as either ON or OFF
  13316. *
  13317. * RETURN:
  13318. * 0 = success
  13319. * !0 = failure
  13320. *
  13321. * PARAMETERS:
  13322. * *dev - pointer to device structure
  13323. * *attr - pointer to device attributes
  13324. * *buf - pointer to output buffer
  13325. ******************************************************************************/
  13326. static ssize_t pt_sleep_status_show(struct device *dev,
  13327. struct device_attribute *attr, char *buf)
  13328. {
  13329. struct pt_core_data *cd = dev_get_drvdata(dev);
  13330. ssize_t ret;
  13331. mutex_lock(&cd->system_lock);
  13332. if (cd->sleep_state == SS_SLEEP_ON)
  13333. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13334. else
  13335. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13336. mutex_unlock(&cd->system_lock);
  13337. return ret;
  13338. }
  13339. /*******************************************************************************
  13340. * FUNCTION: pt_panel_id_show
  13341. *
  13342. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13343. * detected panel ID from the DUT
  13344. *
  13345. * RETURN:
  13346. * 0 = success
  13347. * !0 = failure
  13348. *
  13349. * PARAMETERS:
  13350. * *dev - pointer to device structure
  13351. * *attr - pointer to device attributes
  13352. * *buf - pointer to output buffer
  13353. ******************************************************************************/
  13354. static ssize_t pt_panel_id_show(struct device *dev,
  13355. struct device_attribute *attr, char *buf)
  13356. {
  13357. struct pt_core_data *cd = dev_get_drvdata(dev);
  13358. ssize_t ret;
  13359. u8 pid = PANEL_ID_NOT_ENABLED;
  13360. int rc = 0;
  13361. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13362. /*
  13363. * The DUT should report the same panel ID from both the BL and
  13364. * the FW unless the panel_id feature is set to only
  13365. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13366. * to retrieve the panel_id.
  13367. */
  13368. if (cd->mode == PT_MODE_BOOTLOADER) {
  13369. /*
  13370. * Return the stored value if PT_PANEL_ID_BY_BL
  13371. * is not supported while other feature exits.
  13372. */
  13373. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13374. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13375. if (rc) {
  13376. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13377. "Failed to retrieve Panel ID. ",
  13378. "Using cached value\n",
  13379. __func__);
  13380. }
  13381. }
  13382. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13383. if (cd->panel_id_support &
  13384. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13385. /* For all systems sysinfo has the panel_id */
  13386. rc = pt_hid_output_get_sysinfo(cd);
  13387. if (!rc)
  13388. pid =
  13389. cd->sysinfo.sensing_conf_data.panel_id;
  13390. pt_debug(dev, DL_ERROR,
  13391. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13392. __func__, rc, pid);
  13393. }
  13394. } else {
  13395. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13396. __func__);
  13397. rc = -EPERM;
  13398. }
  13399. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13400. if (cd->mode == PT_MODE_BOOTLOADER) {
  13401. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13402. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13403. if (rc) {
  13404. pt_debug(dev, DL_ERROR,
  13405. "%s: BL get panel ID failed rc=%d\n",
  13406. __func__, rc);
  13407. }
  13408. }
  13409. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13410. if (cd->panel_id_support &
  13411. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13412. rc = pt_hid_output_get_sysinfo(cd);
  13413. if (!rc)
  13414. pid =
  13415. cd->sysinfo.sensing_conf_data.panel_id;
  13416. pt_debug(dev, DL_ERROR,
  13417. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13418. __func__, rc, pid);
  13419. }
  13420. } else {
  13421. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13422. __func__);
  13423. rc = -EPERM;
  13424. }
  13425. } else {
  13426. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13427. __func__);
  13428. rc = -EPERM;
  13429. }
  13430. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13431. rc, pid);
  13432. return ret;
  13433. }
  13434. /*******************************************************************************
  13435. * FUNCTION: pt_get_param_store
  13436. *
  13437. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13438. * ID to retrieve with the show method.
  13439. *
  13440. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13441. *
  13442. * RETURN: Size of passed in buffer if successful
  13443. *
  13444. * PARAMETERS:
  13445. * *dev - pointer to device structure
  13446. * *attr - pointer to device attributes
  13447. * *buf - pointer to output buffer
  13448. * size - size of buf
  13449. ******************************************************************************/
  13450. static ssize_t pt_get_param_store(struct device *dev,
  13451. struct device_attribute *attr, const char *buf, size_t size)
  13452. {
  13453. struct pt_core_data *cd = dev_get_drvdata(dev);
  13454. u32 input_data[2];
  13455. int length;
  13456. int rc = 0;
  13457. /* Maximum input of one value */
  13458. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13459. ARRAY_SIZE(input_data));
  13460. if (length != 1) {
  13461. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13462. __func__);
  13463. rc = -EINVAL;
  13464. goto exit;
  13465. }
  13466. mutex_lock(&cd->system_lock);
  13467. cd->get_param_id = input_data[0];
  13468. mutex_unlock(&(cd->system_lock));
  13469. exit:
  13470. if (rc)
  13471. return rc;
  13472. return size;
  13473. }
  13474. /*******************************************************************************
  13475. * FUNCTION: pt_get_param_show
  13476. *
  13477. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13478. * parameter data from the DUT based on the ID stored in the core
  13479. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13480. * communicate or some other error occures, an error status is returned
  13481. * with no value following.
  13482. * Output is in the form:
  13483. * Status: x
  13484. * 0xyyyyyyyy
  13485. * The 32bit data will only follow the status code if the status == 0
  13486. *
  13487. * NOTE: This sysfs node is only available after a DUT has been enumerated
  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_get_param_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 status;
  13504. u32 value = 0;
  13505. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13506. if (status) {
  13507. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13508. __func__, "pt_get_param", status);
  13509. ret = scnprintf(buf, strlen(buf),
  13510. "%s %d\n",
  13511. "Status:", status);
  13512. } else {
  13513. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13514. __func__, cd->get_param_id, value);
  13515. ret = scnprintf(buf, strlen(buf),
  13516. "Status: %d\n"
  13517. "0x%04X\n",
  13518. status, value);
  13519. }
  13520. return ret;
  13521. }
  13522. /*******************************************************************************
  13523. * FUNCTION: pt_ttdl_restart_show
  13524. *
  13525. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13526. * probed modules, calls startup() and then re-probes modules.
  13527. *
  13528. * RETURN: size of data written to sysfs node
  13529. *
  13530. * PARAMETERS:
  13531. * *dev - pointer to device structure
  13532. * *attr - pointer to device attributes structure
  13533. * *buf - pointer to print output buffer
  13534. ******************************************************************************/
  13535. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13536. struct device_attribute *attr, char *buf)
  13537. {
  13538. struct pt_core_data *cd = dev_get_drvdata(dev);
  13539. int t;
  13540. int rc = 0;
  13541. mutex_lock(&cd->system_lock);
  13542. cd->startup_state = STARTUP_NONE;
  13543. mutex_unlock(&(cd->system_lock));
  13544. /* ensure no left over exclusive access is still locked */
  13545. release_exclusive(cd, cd->dev);
  13546. pt_queue_restart(cd);
  13547. t = wait_event_timeout(cd->wait_q,
  13548. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13549. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13550. if (IS_TMO(t)) {
  13551. pt_debug(cd->dev, DL_ERROR,
  13552. "%s: TMO waiting for FW sentinel\n", __func__);
  13553. rc = -ETIME;
  13554. }
  13555. return scnprintf(buf, strlen(buf),
  13556. "Status: %d\n"
  13557. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13558. }
  13559. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13560. /*******************************************************************************
  13561. * FUNCTION: pt_pip2_gpio_read_show
  13562. *
  13563. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13564. * contents of the response to the passed in output buffer.
  13565. *
  13566. * RETURN: size of data written to sysfs node
  13567. *
  13568. * PARAMETERS:
  13569. * *dev - pointer to device structure
  13570. * *attr - pointer to device attributes structure
  13571. * *buf - pointer to print output buffer
  13572. ******************************************************************************/
  13573. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13574. struct device_attribute *attr, char *buf)
  13575. {
  13576. struct pt_core_data *cd = dev_get_drvdata(dev);
  13577. u8 status = 0;
  13578. u32 gpio_value = 0;
  13579. int rc = 0;
  13580. /* This functionality is only available in the BL */
  13581. if (cd->mode == PT_MODE_BOOTLOADER)
  13582. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13583. else
  13584. rc = -EPERM;
  13585. if (!rc) {
  13586. if (status == 0)
  13587. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13588. "Status: %d\n"
  13589. "DUT GPIO Reg: 0x%08X\n",
  13590. rc, gpio_value);
  13591. else
  13592. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13593. "Status: %d\n"
  13594. "DUT GPIO Reg: n/a\n",
  13595. status);
  13596. } else
  13597. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13598. "Status: %d\n"
  13599. "DUT GPIO Reg: n/a\n",
  13600. rc);
  13601. }
  13602. /*******************************************************************************
  13603. * FUNCTION: pt_device_exit
  13604. *
  13605. * SUMMARY: Remove functon for the I2C module
  13606. *
  13607. * PARAMETERS:
  13608. * *client - pointer to i2c client structure
  13609. ******************************************************************************/
  13610. #ifdef PT_AMBIENT_MODE
  13611. static int pt_device_exit(struct i2c_client *client)
  13612. {
  13613. struct pt_core_data *cd = i2c_get_clientdata(client);
  13614. struct device *dev = cd->dev;
  13615. void *glink_pt_send_msg;
  13616. int glink_touch_enter = TOUCH_ENTER;
  13617. pt_debug(dev, DL_INFO,"%s: Start pt_device_exit\n", __func__);
  13618. glink_pt_send_msg = &glink_touch_enter;
  13619. pt_debug(dev, DL_INFO, "[touch]glink_pt_send_msg = %0x\n", glink_pt_send_msg);
  13620. glink_touch_tx_msg(glink_pt_send_msg, TOUCH_MSG_SIZE);
  13621. if (active_panel)
  13622. panel_event_notifier_unregister(cd->entry);
  13623. pt_core_state = STATE_SUSPEND;
  13624. pm_runtime_suspend(dev);
  13625. pm_runtime_disable(dev);
  13626. pt_stop_wd_timer(cd);
  13627. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  13628. cancel_work_sync(&cd->ttdl_restart_work);
  13629. cancel_work_sync(&cd->enum_work);
  13630. cancel_work_sync(&cd->resume_offload_work);
  13631. cancel_work_sync(&cd->suspend_offload_work);
  13632. cancel_work_sync(&cd->resume_work);
  13633. cancel_work_sync(&cd->suspend_work);
  13634. pt_stop_wd_timer(cd);
  13635. device_init_wakeup(dev, 0);
  13636. disable_irq_nosync(cd->irq);
  13637. if (cd->cpdata->setup_irq)
  13638. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  13639. if (cd->cpdata->init)
  13640. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  13641. if (cd->cpdata->setup_power)
  13642. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  13643. pt_debug(dev, DL_INFO,"%s: End pt_device_exit \n", __func__);
  13644. return 0;
  13645. }
  13646. #endif
  13647. /*******************************************************************************
  13648. * FUNCTION: pt_touch_offload_store
  13649. *
  13650. * SUMMARY: The store method for the touch_offload sysfs node that allows the TTDL
  13651. * to be enabled/disabled.
  13652. *
  13653. * RETURN: Size of passed in buffer
  13654. *
  13655. * PARAMETERS:
  13656. * *dev - pointer to device structure
  13657. * *attr - pointer to device attributes
  13658. * *buf - pointer to buffer that hold the command parameters
  13659. * size - size of buf
  13660. ******************************************************************************/
  13661. static ssize_t pt_touch_offload_store(struct device *dev,
  13662. struct device_attribute *attr, const char *buf, size_t size)
  13663. {
  13664. struct pt_core_data *cd = dev_get_drvdata(dev);
  13665. struct i2c_client *client = to_i2c_client(dev);
  13666. u32 input_data[2];
  13667. int length;
  13668. int rc = 0;
  13669. /* Maximum input of one value */
  13670. length = _pt_ic_parse_input(dev, buf, size, input_data, ARRAY_SIZE(input_data));
  13671. if (length != 1) {
  13672. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n", __func__);
  13673. rc = -EINVAL;
  13674. goto exit;
  13675. }
  13676. switch (input_data[0]) {
  13677. case 0:
  13678. pt_debug(dev, DL_ERROR, "%s: TTDL: Core Touch Offload OFF\n", __func__);
  13679. cd->touch_offload = true;
  13680. rc = pt_device_exit(client);
  13681. if (rc)
  13682. pt_debug(dev, DL_ERROR, "%s: Power off error detected rc=%d\n",
  13683. __func__, rc);
  13684. else {
  13685. cd->touch_offload = true;
  13686. pt_debug(dev, DL_ERROR, "%s: Debugfs PT DEVICE EXIT flag set:\n",
  13687. __func__);
  13688. }
  13689. break;
  13690. case 1:
  13691. pt_debug(dev, DL_ERROR, "%s: TTDL: Core Touch Offload ON\n", __func__);
  13692. rc = pt_device_entry(&client->dev, client->irq, PT_DATA_SIZE);
  13693. if (rc)
  13694. pt_debug(dev, DL_ERROR, "%s: Power on error detected rc=%d\n",
  13695. __func__, rc);
  13696. else {
  13697. cd->touch_offload = false;
  13698. pt_debug(dev, DL_ERROR, "%s: Debugfs PT DEVICE ENTRY flag set:\n",
  13699. __func__);
  13700. }
  13701. break;
  13702. default:
  13703. rc = -EINVAL;
  13704. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13705. }
  13706. exit:
  13707. if (rc)
  13708. return rc;
  13709. return size;
  13710. }
  13711. /*******************************************************************************
  13712. * FUNCTION: pt_touch_offload_show
  13713. *
  13714. * SUMMARY: The show method for the touch_offload sysfs node that allows the TTDL
  13715. * to verify touch offload enable or disabled.
  13716. *
  13717. * RETURN: size of data written to sysfs node
  13718. *
  13719. * PARAMETERS:
  13720. * *dev - pointer to device structure
  13721. * *attr - pointer to device attributes
  13722. * *buf - pointer to print output buffer
  13723. ******************************************************************************/
  13724. static ssize_t pt_touch_offload_show(struct device *dev,
  13725. struct device_attribute *attr, char *buf)
  13726. {
  13727. struct pt_core_data *cd = dev_get_drvdata(dev);
  13728. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13729. "Touch offload : %s\n",
  13730. (cd->touch_offload)? "Enabled" : "Disabled");
  13731. }
  13732. /*******************************************************************************
  13733. * FUNCTION: pt_pip2_version_show
  13734. *
  13735. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13736. * contents of the response to the passed in output buffer.
  13737. *
  13738. * RETURN: size of data written to sysfs node
  13739. *
  13740. * PARAMETERS:
  13741. * *dev - pointer to device structure
  13742. * *attr - pointer to device attributes structure
  13743. * *buf - pointer to print output buffer
  13744. ******************************************************************************/
  13745. static ssize_t pt_pip2_version_show(struct device *dev,
  13746. struct device_attribute *attr, char *buf)
  13747. {
  13748. int rc = 0;
  13749. struct pt_core_data *cd = dev_get_drvdata(dev);
  13750. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13751. rc = pt_pip2_get_version(cd);
  13752. if (!rc) {
  13753. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13754. "PIP VERSION : %02X.%02X\n"
  13755. "BL VERSION : %02X.%02X\n"
  13756. "FW VERSION : %02X.%02X\n"
  13757. "SILICON ID : %04X.%04X\n"
  13758. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13759. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13760. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13761. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13762. ttdata->chip_id, ttdata->chip_rev,
  13763. ttdata->uid[0], ttdata->uid[1],
  13764. ttdata->uid[2], ttdata->uid[3],
  13765. ttdata->uid[4], ttdata->uid[5],
  13766. ttdata->uid[6], ttdata->uid[7],
  13767. ttdata->uid[8], ttdata->uid[9],
  13768. ttdata->uid[10], ttdata->uid[11]);
  13769. } else {
  13770. pt_debug(dev, DL_ERROR,
  13771. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13772. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13773. "PIP VERSION : -\n"
  13774. "BL VERSION : -\n"
  13775. "FW VERSION : -\n"
  13776. "SILICON ID : -\n"
  13777. "UID : -\n");
  13778. }
  13779. }
  13780. #ifdef TTDL_DIAGNOSTICS
  13781. /*******************************************************************************
  13782. * FUNCTION: pt_ttdl_status_show
  13783. *
  13784. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13785. * variable states and GPIO levels. Additional information printed when
  13786. * TTDL_DIAGNOSTICS is enabled.
  13787. *
  13788. * NOTE: All counters will be reset to 0 when this function is called.
  13789. *
  13790. * RETURN:
  13791. * 0 = success
  13792. * !0 = failure
  13793. *
  13794. * PARAMETERS:
  13795. * *dev - pointer to device structure
  13796. * *attr - pointer to device attributes
  13797. * *buf - pointer to output buffer
  13798. ******************************************************************************/
  13799. static ssize_t pt_ttdl_status_show(struct device *dev,
  13800. struct device_attribute *attr, char *buf)
  13801. {
  13802. struct pt_core_data *cd = dev_get_drvdata(dev);
  13803. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13804. struct i2c_client *client = to_i2c_client(dev);
  13805. ssize_t ret;
  13806. u16 cal_size = 0;
  13807. unsigned short crc = 0;
  13808. if (cd->cal_cache_in_host)
  13809. _pt_manage_local_cal_data(dev,
  13810. PT_CAL_DATA_INFO, &cal_size, &crc);
  13811. ret = scnprintf(buf, strlen(buf),
  13812. "%s: 0x%04X\n"
  13813. "%s: %d\n"
  13814. "%s: %s\n"
  13815. "%s: %s %s\n"
  13816. "%s: %s\n"
  13817. "%s: 0x%02X\n"
  13818. "%s: %s\n"
  13819. "%s: %s\n"
  13820. "%s: %s\n"
  13821. "%s: %s\n"
  13822. "%s: %d\n"
  13823. "%s: %d\n"
  13824. "%s: %s\n"
  13825. "%s: %s\n"
  13826. "%s: %d\n"
  13827. #ifdef TTDL_DIAGNOSTICS
  13828. "%s: %d\n"
  13829. "%s: %d\n"
  13830. "%s: %d\n"
  13831. "%s: %d\n"
  13832. "%s: %d\n"
  13833. "%s: %d\n"
  13834. "%s: %d\n"
  13835. "%s: %d\n"
  13836. "%s: %d\n"
  13837. "%s: %d\n"
  13838. "%s: %s\n"
  13839. "%s: %s\n"
  13840. "%s: %s\n"
  13841. "%s: %d\n"
  13842. "%s: 0x%04X\n"
  13843. "%s: %s\n"
  13844. #endif /* TTDL_DIAGNOSTICS */
  13845. ,
  13846. "Startup Status ", cd->startup_status,
  13847. "TTDL Debug Level ", cd->debug_level,
  13848. "Mode ",
  13849. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13850. "Operational" : "BL") : "Unknown",
  13851. "DUT Generation ",
  13852. cd->active_dut_generation ?
  13853. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13854. "PT TC/TT" : "Gen5/6") : "Unknown",
  13855. cd->active_dut_generation ?
  13856. (cd->set_dut_generation == true ?
  13857. "(Set)" : "(Detected)") : "",
  13858. "HW Detected ",
  13859. cd->hw_detected ? "True" : "False",
  13860. "I2C Address ",
  13861. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13862. "Active Bus Module ",
  13863. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13864. "Flashless Mode ",
  13865. cd->flashless_dut == 1 ? "Yes" : "No",
  13866. "GPIO state - IRQ ",
  13867. cd->cpdata->irq_stat ?
  13868. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13869. "High" : "Low") : "not defined",
  13870. "GPIO state - TP_XRES ",
  13871. pdata->core_pdata->rst_gpio ?
  13872. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13873. "High" : "Low") : "not defined",
  13874. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13875. "Startup Retry Count ", cd->startup_retry_count,
  13876. "WD - Manual Force Stop ",
  13877. cd->watchdog_force_stop ? "True" : "False",
  13878. "WD - Enabled ",
  13879. cd->watchdog_enabled ? "True" : "False",
  13880. "WD - Interval (ms) ", cd->watchdog_interval
  13881. #ifdef TTDL_DIAGNOSTICS
  13882. , "WD - Triggered Count ", cd->watchdog_count,
  13883. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13884. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13885. "WD - XRES Count ", cd->wd_xres_count,
  13886. "IRQ Triggered Count ", cd->irq_count,
  13887. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13888. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13889. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13890. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13891. "Error GPIO trigger type ", cd->err_gpio_type,
  13892. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13893. "Suppress No-Flash Auto BL ",
  13894. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13895. "Yes" : "No",
  13896. "Calibration Cache on host ",
  13897. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13898. "Yes" : "No",
  13899. "Calibration Cache size ", cal_size,
  13900. "Calibration Cache chip ID ", crc,
  13901. "Multi-Chip Support ",
  13902. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13903. #endif /* TTDL_DIAGNOSTICS */
  13904. );
  13905. #ifdef TTDL_DIAGNOSTICS
  13906. /* Reset all diagnostic counters */
  13907. cd->watchdog_count = 0;
  13908. cd->watchdog_irq_stuck_count = 0;
  13909. cd->watchdog_failed_access_count = 0;
  13910. cd->wd_xres_count = 0;
  13911. cd->irq_count = 0;
  13912. cd->bl_retry_packet_count = 0;
  13913. cd->pip2_crc_error_count = 0;
  13914. cd->bus_transmit_error_count = 0;
  13915. #endif
  13916. return ret;
  13917. }
  13918. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13919. /*******************************************************************************
  13920. * FUNCTION: pt_pip2_enter_bl_show
  13921. *
  13922. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13923. * the DUT into the BL and show the success or failure of entering the BL
  13924. *
  13925. * RETURN:
  13926. * 0 = success
  13927. * !0 = failure
  13928. *
  13929. * PARAMETERS:
  13930. * *dev - pointer to device structure
  13931. * *attr - pointer to device attributes
  13932. * *buf - pointer to output buffer
  13933. ******************************************************************************/
  13934. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13935. struct device_attribute *attr, char *buf)
  13936. {
  13937. ssize_t ret = 0;
  13938. int rc = 0;
  13939. int result = 0;
  13940. u8 mode = PT_MODE_UNKNOWN;
  13941. struct pt_core_data *cd = dev_get_drvdata(dev);
  13942. /* Turn off the TTDL WD before enter bootloader */
  13943. pt_stop_wd_timer(cd);
  13944. /* Ensure NO enumeration work is queued or will be queued */
  13945. cancel_work_sync(&cd->enum_work);
  13946. mutex_lock(&cd->system_lock);
  13947. cd->bridge_mode = true;
  13948. mutex_unlock(&cd->system_lock);
  13949. /* set mode to operational to avoid any extra PIP traffic */
  13950. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13951. switch (result) {
  13952. case PT_ENTER_BL_PASS:
  13953. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13954. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13955. break;
  13956. case PT_ENTER_BL_ERROR:
  13957. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13958. rc,
  13959. " Unknown Error");
  13960. break;
  13961. case PT_ENTER_BL_RESET_FAIL:
  13962. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13963. rc,
  13964. " Soft Reset Failed");
  13965. break;
  13966. case PT_ENTER_BL_HID_START_BL_FAIL:
  13967. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13968. rc,
  13969. " PIP Start BL Cmd Failed");
  13970. break;
  13971. case PT_ENTER_BL_CONFIRM_FAIL:
  13972. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13973. rc,
  13974. " Error confirming DUT entered BL");
  13975. break;
  13976. default:
  13977. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13978. rc, " Unknown Error");
  13979. break;
  13980. };
  13981. /* Allow enumeration work to be queued again */
  13982. cd->bridge_mode = false;
  13983. return ret;
  13984. }
  13985. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13986. /*******************************************************************************
  13987. * FUNCTION: pt_pip2_exit_bl_show
  13988. *
  13989. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13990. * launch the APP and put the DUT Application mode
  13991. *
  13992. * RETURN:
  13993. * 0 = success
  13994. * !0 = failure
  13995. *
  13996. * PARAMETERS:
  13997. * *dev - pointer to device structure
  13998. * *attr - pointer to device attributes
  13999. * *buf - pointer to output buffer
  14000. ******************************************************************************/
  14001. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  14002. struct device_attribute *attr, char *buf)
  14003. {
  14004. struct pt_core_data *cd = dev_get_drvdata(dev);
  14005. ssize_t ret = 0;
  14006. int rc = 0;
  14007. u8 status_str[PT_STATUS_STR_LEN];
  14008. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  14009. /*
  14010. * Perform enum if startup_status doesn't reach to
  14011. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  14012. */
  14013. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  14014. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  14015. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  14016. strlcpy(status_str,
  14017. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  14018. }
  14019. }
  14020. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  14021. rc, status_str);
  14022. return ret;
  14023. }
  14024. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  14025. #endif
  14026. #ifdef EASYWAKE_TSG6
  14027. /*******************************************************************************
  14028. * FUNCTION: pt_easy_wakeup_gesture_show
  14029. *
  14030. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  14031. * current easy wakeup gesture
  14032. *
  14033. * RETURN:
  14034. * 0 = success
  14035. * !0 = failure
  14036. *
  14037. * PARAMETERS:
  14038. * *dev - pointer to device structure
  14039. * *attr - pointer to device attributes
  14040. * *buf - pointer to output buffer
  14041. ******************************************************************************/
  14042. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  14043. struct device_attribute *attr, char *buf)
  14044. {
  14045. struct pt_core_data *cd = dev_get_drvdata(dev);
  14046. ssize_t ret;
  14047. mutex_lock(&cd->system_lock);
  14048. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  14049. cd->easy_wakeup_gesture);
  14050. mutex_unlock(&cd->system_lock);
  14051. return ret;
  14052. }
  14053. /*******************************************************************************
  14054. * FUNCTION: pt_easy_wakeup_gesture_store
  14055. *
  14056. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  14057. * allows the wake gesture to be set to a custom value.
  14058. *
  14059. * RETURN: Size of passed in buffer is success
  14060. *
  14061. * PARAMETERS:
  14062. * *dev - pointer to device structure
  14063. * *attr - pointer to device attributes
  14064. * *buf - pointer to buffer that hold the command parameters
  14065. * size - size of buf
  14066. ******************************************************************************/
  14067. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  14068. struct device_attribute *attr, const char *buf, size_t size)
  14069. {
  14070. struct pt_core_data *cd = dev_get_drvdata(dev);
  14071. u32 input_data[2];
  14072. int length;
  14073. int rc = 0;
  14074. /* Maximum input of one value */
  14075. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14076. ARRAY_SIZE(input_data));
  14077. if (length != 1) {
  14078. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14079. __func__);
  14080. rc = -EINVAL;
  14081. goto exit;
  14082. }
  14083. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  14084. __func__, cd->features.easywake);
  14085. if (!cd->features.easywake || input_data[0] > 0xFF) {
  14086. rc = -EINVAL;
  14087. goto exit;
  14088. }
  14089. pm_runtime_get_sync(dev);
  14090. mutex_lock(&cd->system_lock);
  14091. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  14092. cd->easy_wakeup_gesture = (u8)input_data[0];
  14093. pt_debug(dev, DL_INFO,
  14094. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  14095. __func__, cd->easy_wakeup_gesture);
  14096. } else
  14097. rc = -ENODEV;
  14098. mutex_unlock(&cd->system_lock);
  14099. pm_runtime_put(dev);
  14100. exit:
  14101. if (rc)
  14102. return rc;
  14103. return size;
  14104. }
  14105. /*******************************************************************************
  14106. * FUNCTION: pt_easy_wakeup_gesture_id_show
  14107. *
  14108. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  14109. * show the TSG6 easywake gesture ID
  14110. *
  14111. * RETURN:
  14112. * 0 = success
  14113. * !0 = failure
  14114. *
  14115. * PARAMETERS:
  14116. * *dev - pointer to device structure
  14117. * *attr - pointer to device attributes
  14118. * *buf - pointer to output buffer
  14119. ******************************************************************************/
  14120. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  14121. struct device_attribute *attr, char *buf)
  14122. {
  14123. struct pt_core_data *cd = dev_get_drvdata(dev);
  14124. ssize_t ret;
  14125. mutex_lock(&cd->system_lock);
  14126. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  14127. cd->gesture_id);
  14128. mutex_unlock(&cd->system_lock);
  14129. return ret;
  14130. }
  14131. /*******************************************************************************
  14132. * FUNCTION: pt_easy_wakeup_gesture_data_show
  14133. *
  14134. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  14135. * show the TSG6 easywake gesture data in the following format:
  14136. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  14137. *
  14138. * RETURN:
  14139. * 0 = success
  14140. * !0 = failure
  14141. *
  14142. * PARAMETERS:
  14143. * *dev - pointer to device structure
  14144. * *attr - pointer to device attributes
  14145. * *buf - pointer to output buffer
  14146. ******************************************************************************/
  14147. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  14148. struct device_attribute *attr, char *buf)
  14149. {
  14150. struct pt_core_data *cd = dev_get_drvdata(dev);
  14151. ssize_t ret = 0;
  14152. int i;
  14153. mutex_lock(&cd->system_lock);
  14154. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  14155. for (i = 0; i < cd->gesture_data_length; i++)
  14156. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14157. "0x%02X\n", cd->gesture_data[i]);
  14158. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14159. "(%d bytes)\n", cd->gesture_data_length);
  14160. mutex_unlock(&cd->system_lock);
  14161. return ret;
  14162. }
  14163. #endif /* EASYWAKE_TSG6 */
  14164. #ifdef TTDL_DIAGNOSTICS
  14165. /*******************************************************************************
  14166. * FUNCTION: pt_err_gpio_show
  14167. *
  14168. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  14169. * setting up the gpio was successful
  14170. *
  14171. * RETURN: Char buffer with printed GPIO creation state
  14172. *
  14173. * PARAMETERS:
  14174. * *dev - pointer to device structure
  14175. * *attr - pointer to device attributes
  14176. * *buf - pointer to output buffer
  14177. ******************************************************************************/
  14178. static ssize_t pt_err_gpio_show(struct device *dev,
  14179. struct device_attribute *attr, char *buf)
  14180. {
  14181. struct pt_core_data *cd = dev_get_drvdata(dev);
  14182. return scnprintf(buf, strlen(buf), "Status: 0\n"
  14183. "Err GPIO (%d) : %s\n"
  14184. "Err GPIO trig type: %d\n",
  14185. cd->err_gpio,
  14186. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  14187. "HIGH" : "low") : "not defined"),
  14188. cd->err_gpio_type);
  14189. }
  14190. /*******************************************************************************
  14191. * FUNCTION: pt_err_gpio_store
  14192. *
  14193. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  14194. * available host GPIO to be used to trigger when TTDL detects a PIP
  14195. * command/response timeout.
  14196. *
  14197. * RETURN:
  14198. * 0 = success
  14199. * !0 = failure
  14200. *
  14201. * PARAMETERS:
  14202. * *dev - pointer to device structure
  14203. * *attr - pointer to device attributes
  14204. * *buf - pointer to buffer that hold the command parameters
  14205. * size - size of buf
  14206. ******************************************************************************/
  14207. static ssize_t pt_err_gpio_store(struct device *dev,
  14208. struct device_attribute *attr, const char *buf, size_t size)
  14209. {
  14210. struct pt_core_data *cd = dev_get_drvdata(dev);
  14211. unsigned long gpio;
  14212. int rc = 0;
  14213. u32 input_data[3];
  14214. int length;
  14215. u8 err_type;
  14216. input_data[0] = 0;
  14217. input_data[1] = 0;
  14218. /* Maximmum input is two elements */
  14219. length = _pt_ic_parse_input(dev, buf, size,
  14220. input_data, ARRAY_SIZE(input_data));
  14221. if (length < 1 || length > 2) {
  14222. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14223. __func__);
  14224. rc = -EINVAL;
  14225. goto exit;
  14226. }
  14227. gpio = input_data[0];
  14228. err_type = (u8)input_data[1];
  14229. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  14230. rc = -EINVAL;
  14231. goto exit;
  14232. }
  14233. mutex_lock(&cd->system_lock);
  14234. gpio_free(gpio);
  14235. rc = gpio_request(gpio, NULL);
  14236. if (rc) {
  14237. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  14238. rc = -ENODEV;
  14239. } else {
  14240. cd->err_gpio = gpio;
  14241. cd->err_gpio_type = err_type;
  14242. gpio_direction_output(gpio, 0);
  14243. }
  14244. mutex_unlock(&cd->system_lock);
  14245. exit:
  14246. if (rc)
  14247. return rc;
  14248. return size;
  14249. }
  14250. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  14251. pt_err_gpio_store);
  14252. /*******************************************************************************
  14253. * FUNCTION: pt_drv_irq_show
  14254. *
  14255. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  14256. * TTDL interrupt is enabled/disabled
  14257. *
  14258. * RETURN:
  14259. * 0 = success
  14260. * !0 = failure
  14261. *
  14262. * PARAMETERS:
  14263. * *dev - pointer to device structure
  14264. * *attr - pointer to device attributes
  14265. * *buf - pointer to output buffer
  14266. ******************************************************************************/
  14267. static ssize_t pt_drv_irq_show(struct device *dev,
  14268. struct device_attribute *attr, char *buf)
  14269. {
  14270. struct pt_core_data *cd = dev_get_drvdata(dev);
  14271. ssize_t ret = 0;
  14272. mutex_lock(&cd->system_lock);
  14273. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  14274. if (cd->irq_enabled)
  14275. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14276. "Driver interrupt: ENABLED\n");
  14277. else
  14278. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14279. "Driver interrupt: DISABLED\n");
  14280. mutex_unlock(&cd->system_lock);
  14281. return ret;
  14282. }
  14283. /*******************************************************************************
  14284. * FUNCTION: pt_drv_irq_store
  14285. *
  14286. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  14287. * IRQ to be enabled/disabled.
  14288. *
  14289. * RETURN: Size of passed in buffer
  14290. *
  14291. * PARAMETERS:
  14292. * *dev - pointer to device structure
  14293. * *attr - pointer to device attributes
  14294. * *buf - pointer to buffer that hold the command parameters
  14295. * size - size of buf
  14296. ******************************************************************************/
  14297. static ssize_t pt_drv_irq_store(struct device *dev,
  14298. struct device_attribute *attr, const char *buf, size_t size)
  14299. {
  14300. struct pt_core_data *cd = dev_get_drvdata(dev);
  14301. u32 input_data[2];
  14302. int length;
  14303. int rc = 0;
  14304. /* Maximum input of one value */
  14305. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14306. ARRAY_SIZE(input_data));
  14307. if (length != 1) {
  14308. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14309. __func__);
  14310. rc = -EINVAL;
  14311. goto exit;
  14312. }
  14313. mutex_lock(&cd->system_lock);
  14314. switch (input_data[0]) {
  14315. case 0:
  14316. if (cd->irq_enabled) {
  14317. cd->irq_enabled = false;
  14318. /* Disable IRQ has no return value to check */
  14319. disable_irq_nosync(cd->irq);
  14320. pt_debug(dev, DL_INFO,
  14321. "%s: Driver IRQ now disabled\n",
  14322. __func__);
  14323. } else
  14324. pt_debug(dev, DL_INFO,
  14325. "%s: Driver IRQ already disabled\n",
  14326. __func__);
  14327. break;
  14328. case 1:
  14329. if (cd->irq_enabled == false) {
  14330. cd->irq_enabled = true;
  14331. enable_irq(cd->irq);
  14332. pt_debug(dev, DL_INFO,
  14333. "%s: Driver IRQ now enabled\n",
  14334. __func__);
  14335. } else
  14336. pt_debug(dev, DL_INFO,
  14337. "%s: Driver IRQ already enabled\n",
  14338. __func__);
  14339. break;
  14340. default:
  14341. rc = -EINVAL;
  14342. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14343. }
  14344. mutex_unlock(&(cd->system_lock));
  14345. exit:
  14346. if (rc)
  14347. return rc;
  14348. return size;
  14349. }
  14350. /*******************************************************************************
  14351. * FUNCTION: pt_pip2_bin_hdr_show
  14352. *
  14353. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14354. * the bin file header from flash and show each field
  14355. *
  14356. * RETURN:
  14357. * 0 = success
  14358. * !0 = failure
  14359. *
  14360. * PARAMETERS:
  14361. * *dev - pointer to device structure
  14362. * *attr - pointer to device attributes
  14363. * *buf - pointer to output buffer
  14364. ******************************************************************************/
  14365. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14366. struct device_attribute *attr, char *buf)
  14367. {
  14368. ssize_t ret = 0;
  14369. struct pt_bin_file_hdr hdr = {0};
  14370. int rc;
  14371. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14372. ret = scnprintf(buf, strlen(buf),
  14373. "%s: %d\n"
  14374. "%s: %d\n"
  14375. "%s: 0x%04X\n"
  14376. "%s: %d\n"
  14377. "%s: %d\n"
  14378. "%s: %d\n"
  14379. "%s: 0x%08X\n"
  14380. "%s: 0x%04X\n"
  14381. "%s: 0x%04X\n"
  14382. "%s: %d\n"
  14383. "%s: %d\n",
  14384. "Status", rc,
  14385. "Header Length ", hdr.length,
  14386. "TTPID ", hdr.ttpid,
  14387. "FW Major Ver ", hdr.fw_major,
  14388. "FW Minor Ver ", hdr.fw_minor,
  14389. "FW Rev Control ", hdr.fw_rev_ctrl,
  14390. "FW CRC ", hdr.fw_crc,
  14391. "Silicon Rev ", hdr.si_rev,
  14392. "Silicon ID ", hdr.si_id,
  14393. "Config Ver ", hdr.config_ver,
  14394. "HEX File Size ", hdr.hex_file_size
  14395. );
  14396. return ret;
  14397. }
  14398. /*******************************************************************************
  14399. * FUNCTION: pt_platform_data_show
  14400. *
  14401. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14402. * active platform data including: GPIOs, Vendor and Product IDs,
  14403. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14404. * HID registers, and Easy wake gesture
  14405. *
  14406. * RETURN: Size of printed data
  14407. *
  14408. * PARAMETERS:
  14409. * *dev - pointer to device structure
  14410. * *attr - pointer to device attributes
  14411. * *buf - pointer to output buffer
  14412. ******************************************************************************/
  14413. static ssize_t pt_platform_data_show(struct device *dev,
  14414. struct device_attribute *attr, char *buf)
  14415. {
  14416. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14417. struct pt_core_data *cd = dev_get_drvdata(dev);
  14418. ssize_t ret;
  14419. ret = scnprintf(buf, strlen(buf),
  14420. "%s: %d\n"
  14421. "%s: %d\n"
  14422. "%s: %d\n"
  14423. "%s: %d\n"
  14424. "%s: %d\n"
  14425. "%s: %d\n"
  14426. "%s: %d\n"
  14427. "%s: %d\n"
  14428. "%s: %d\n"
  14429. "%s: %d\n"
  14430. "%s: %d\n"
  14431. "%s: %d\n"
  14432. "%s: %d\n"
  14433. "%s: %d\n"
  14434. "%s: %d\n"
  14435. "%s: %d\n"
  14436. "%s: %d\n"
  14437. "%s: %d\n"
  14438. "%s: %d\n"
  14439. "%s: %d\n"
  14440. "%s: %d\n"
  14441. "%s: %d\n"
  14442. "%s: %d\n"
  14443. "%s: %d\n"
  14444. "%s: %d\n"
  14445. "%s: %d\n"
  14446. "%s: %d\n"
  14447. "%s: %s\n"
  14448. "%s: %s\n"
  14449. "%s: %d\n",
  14450. "Status", 0,
  14451. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14452. "Interrupt GPIO Value ",
  14453. pdata->core_pdata->irq_gpio ?
  14454. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14455. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14456. "Reset GPIO Value ",
  14457. pdata->core_pdata->rst_gpio ?
  14458. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14459. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14460. "DDI Reset GPIO Value ",
  14461. pdata->core_pdata->ddi_rst_gpio ?
  14462. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14463. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14464. "VDDI GPIO Value ",
  14465. pdata->core_pdata->vddi_gpio ?
  14466. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14467. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14468. "VCC GPIO Value ",
  14469. pdata->core_pdata->vcc_gpio ?
  14470. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14471. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14472. "AVDD GPIO Value ",
  14473. pdata->core_pdata->avdd_gpio ?
  14474. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14475. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14476. "AVEE GPIO Value ",
  14477. pdata->core_pdata->avee_gpio ?
  14478. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14479. "Vendor ID ", pdata->core_pdata->vendor_id,
  14480. "Product ID ", pdata->core_pdata->product_id,
  14481. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14482. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14483. "Core data flags ", pdata->core_pdata->flags,
  14484. "MT data flags ", pdata->mt_pdata->flags,
  14485. "Loader data flags ", pdata->loader_pdata->flags,
  14486. "Level trigger delay (us) ",
  14487. pdata->core_pdata->level_irq_udelay,
  14488. "HID Descriptor Register ",
  14489. pdata->core_pdata->hid_desc_register,
  14490. "HID Output Register ",
  14491. cd->hid_desc.output_register,
  14492. "HID Command Register ",
  14493. cd->hid_desc.command_register,
  14494. "Easy wakeup gesture ",
  14495. pdata->core_pdata->easy_wakeup_gesture,
  14496. "Config DUT generation ",
  14497. pdata->core_pdata->config_dut_generation ?
  14498. (pdata->core_pdata->config_dut_generation ==
  14499. CONFIG_DUT_PIP2_CAPABLE ?
  14500. "PT TC/TT" : "Gen5/6") : "Auto",
  14501. "Watchdog Force Stop ",
  14502. pdata->core_pdata->watchdog_force_stop ?
  14503. "True" : "False",
  14504. "Panel ID Support ",
  14505. pdata->core_pdata->panel_id_support);
  14506. return ret;
  14507. }
  14508. #define PT_ERR_STR_SIZE 64
  14509. /*******************************************************************************
  14510. * FUNCTION: pt_bist_bus_test
  14511. *
  14512. * SUMMARY: Tests the connectivity of the active bus pins:
  14513. * I2C - SDA and SCL
  14514. * SPI - MOSI, MISO, CLK
  14515. *
  14516. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14517. * bus. If any data is read we know the I2C/SPI pins are connected
  14518. *
  14519. * RETURN:
  14520. * 0 = Success
  14521. * !0 = Failure
  14522. *
  14523. * PARAMETERS:
  14524. * *dev - pointer to device structure
  14525. * *net_toggled - pointer to where to store if bus toggled
  14526. * *err_str - pointer to error string buffer
  14527. ******************************************************************************/
  14528. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14529. {
  14530. struct pt_core_data *cd = dev_get_drvdata(dev);
  14531. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14532. u8 *read_buf = NULL;
  14533. int bytes_read = 0;
  14534. int rc = 0;
  14535. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14536. if (read_buf == NULL) {
  14537. rc = -ENOMEM;
  14538. goto exit;
  14539. }
  14540. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14541. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14542. disable_irq(cd->irq);
  14543. mutex_lock(&cd->system_lock);
  14544. cd->irq_disabled = true;
  14545. mutex_unlock(&cd->system_lock);
  14546. /*
  14547. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14548. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14549. */
  14550. usleep_range(5000, 6000);
  14551. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14552. __func__, (int)sizeof(ver_cmd));
  14553. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14554. ">>> User CMD");
  14555. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14556. if (rc) {
  14557. pt_debug(dev, DL_ERROR,
  14558. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14559. *net_toggled = 0;
  14560. strlcpy(err_str,
  14561. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14562. goto exit_enable_irq;
  14563. }
  14564. usleep_range(4000, 5000);
  14565. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14566. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14567. __func__, bytes_read);
  14568. if (bytes_read == 0) {
  14569. *net_toggled = 0;
  14570. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14571. __func__);
  14572. strlcpy(err_str,
  14573. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14574. rc = -EIO;
  14575. goto exit_enable_irq;
  14576. } else {
  14577. if (cd->bus_ops->bustype == BUS_I2C)
  14578. *net_toggled = 1;
  14579. else {
  14580. if ((bytes_read > 3) &&
  14581. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14582. PIP2_CMD_ID_VERSION)
  14583. *net_toggled = 1;
  14584. else {
  14585. *net_toggled = 0;
  14586. pt_debug(dev, DL_INFO,
  14587. "%s: BUS Read Failed, %d bytes read\n",
  14588. __func__, bytes_read);
  14589. strlcpy(err_str,
  14590. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14591. }
  14592. }
  14593. }
  14594. exit_enable_irq:
  14595. enable_irq(cd->irq);
  14596. usleep_range(5000, 6000);
  14597. mutex_lock(&cd->system_lock);
  14598. cd->irq_disabled = false;
  14599. mutex_unlock(&cd->system_lock);
  14600. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14601. exit:
  14602. kfree(read_buf);
  14603. return rc;
  14604. }
  14605. /*******************************************************************************
  14606. * FUNCTION: pt_bist_irq_test
  14607. *
  14608. * SUMMARY: Tests the connectivity of the IRQ net
  14609. *
  14610. * This test will ensure there is a good connection between the host
  14611. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14612. * asserted and if so keep reading messages off of the bus until
  14613. * it de-asserts. Possible outcomes:
  14614. * - IRQ was already de-asserted: Send a PIP command and if an
  14615. * interrupt is generated the test passes.
  14616. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14617. * test passes.
  14618. * - IRQ stays asserted: After reading the bus multiple times
  14619. * the IRQ stays asserted. Likely open or shorted to GND
  14620. *
  14621. * RETURN:
  14622. * 0 = Success
  14623. * !0 = Failure
  14624. *
  14625. * PARAMETERS:
  14626. * *dev - pointer to device structure
  14627. * *bus_toggled - pointer to where to store if bus toggled
  14628. * *irq_toggled - pointer to where to store if IRQ toggled
  14629. * *xres_toggled - pointer to where to store if XRES toggled
  14630. * *err_str - pointer to error string buffer
  14631. ******************************************************************************/
  14632. static int pt_bist_irq_test(struct device *dev,
  14633. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14634. {
  14635. struct pt_core_data *cd = dev_get_drvdata(dev);
  14636. u8 *read_buf = NULL;
  14637. u8 mode = PT_MODE_UNKNOWN;
  14638. u16 actual_read_len;
  14639. int bytes_read = 0;
  14640. int count = 0;
  14641. int rc = 0;
  14642. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14643. if (read_buf == NULL) {
  14644. rc = -ENOMEM;
  14645. goto exit;
  14646. }
  14647. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14648. cd->irq_count = 0;
  14649. /*
  14650. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14651. * the bus based on the 2 byte initial length read. Try up to 5x.
  14652. */
  14653. while (pt_check_irq_asserted(cd) && count < 5) {
  14654. count++;
  14655. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14656. }
  14657. if (count > 1 && count < 5 && bytes_read > 0) {
  14658. /*
  14659. * IRQ was stuck but data was successfully read from the
  14660. * bus releasing the IRQ line.
  14661. */
  14662. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14663. __func__, count, bytes_read);
  14664. *bus_toggled = 1;
  14665. *irq_toggled = 1;
  14666. goto exit;
  14667. }
  14668. if (count == 5 && bytes_read == 0) {
  14669. /*
  14670. * Looped 5x and read nothing off the bus yet the IRQ is still
  14671. * asserted. Possible conditions:
  14672. * - IRQ open circuit
  14673. * - IRQ shorted to GND
  14674. * - I2C/SPI bus is disconnected
  14675. * - FW holding the pin low
  14676. * Try entering the BL to see if communication works there.
  14677. */
  14678. mode = PT_MODE_IGNORE;
  14679. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14680. if (rc) {
  14681. pt_debug(dev, DL_ERROR,
  14682. "%s Failed to enter BL\n", __func__);
  14683. strlcpy(err_str,
  14684. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14685. *irq_toggled = 0;
  14686. goto exit;
  14687. }
  14688. /*
  14689. * If original mode was operational and we successfully
  14690. * entered the BL, then the XRES net must have toggled
  14691. */
  14692. if (mode == PT_MODE_OPERATIONAL)
  14693. *xres_toggled = 1;
  14694. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14695. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14696. &actual_read_len);
  14697. if (rc) {
  14698. /*
  14699. * Could not communicate to DUT in BL mode. Save the
  14700. * error string, slim chance but the XRES test below may
  14701. * show the IRQ is actually working.
  14702. */
  14703. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14704. pt_debug(dev, DL_ERROR,
  14705. "%s: %s, count=%d bytes_read=%d\n",
  14706. __func__, err_str, count, bytes_read);
  14707. *irq_toggled = 0;
  14708. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14709. goto exit;
  14710. } else {
  14711. *bus_toggled = 1;
  14712. *irq_toggled = 1;
  14713. goto exit;
  14714. }
  14715. }
  14716. if (pt_check_irq_asserted(cd)) {
  14717. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14718. rc = -EIO;
  14719. *irq_toggled = 0;
  14720. goto exit;
  14721. }
  14722. /* Try sending a PIP command to see if we get a response */
  14723. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14724. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14725. if (rc) {
  14726. /*
  14727. * Potential IRQ issue, no communication in App mode, attempt
  14728. * the same command in the BL
  14729. */
  14730. mode = PT_MODE_IGNORE;
  14731. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14732. if (rc) {
  14733. pt_debug(dev, DL_ERROR,
  14734. "%s Failed to enter BL\n", __func__);
  14735. *irq_toggled = 0;
  14736. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14737. goto exit;
  14738. }
  14739. /*
  14740. * If original mode was operational and we successfully
  14741. * entered the BL, this will be useful info for the tp_xres
  14742. * test below.
  14743. */
  14744. if (mode == PT_MODE_OPERATIONAL)
  14745. *xres_toggled = 1;
  14746. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14747. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14748. &actual_read_len);
  14749. if (rc) {
  14750. /*
  14751. * Could not communicate in FW mode or BL mode. Save the
  14752. * error string, slim chance but the XRES test below may
  14753. * show the IRQ is actually working.
  14754. */
  14755. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14756. pt_debug(dev, DL_ERROR,
  14757. "%s: request_active_pip_prot failed\n",
  14758. __func__);
  14759. *irq_toggled = 0;
  14760. goto exit;
  14761. }
  14762. }
  14763. if (cd->irq_count > 0) {
  14764. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14765. cd->irq_count);
  14766. *bus_toggled = 1;
  14767. *irq_toggled = 1;
  14768. goto exit;
  14769. }
  14770. exit:
  14771. kfree(read_buf);
  14772. return rc;
  14773. }
  14774. /*******************************************************************************
  14775. * FUNCTION: pt_bist_xres_test
  14776. *
  14777. * SUMMARY: Tests the connectivity of the TP_XRES net
  14778. *
  14779. * This test will ensure there is a good connection between the host
  14780. * and the DUT on the tp_xres pin. The pin will be toggled to
  14781. * generate a TP reset which will cause the DUT to output a reset
  14782. * sentinel. If the reset sentinel is seen the test passes. If it is
  14783. * not seen the test will attempt to send a soft reset to simply gain
  14784. * some additional information on the failure:
  14785. * - soft reset fails to send: XRES and IRQ likely open
  14786. * - soft reset passes: XRES likely open or stuck de-asserted
  14787. * - soft reset fails: XRES likely stuck asserted
  14788. *
  14789. * RETURN:
  14790. * 0 = Success
  14791. * !0 = Failure
  14792. *
  14793. * PARAMETERS:
  14794. * *dev - pointer to device structure
  14795. * *bus_toggled - pointer to where to store if bus toggled
  14796. * *irq_toggled - pointer to where to store if IRQ toggled
  14797. * *xres_toggled - pointer to where to store if XRES toggled
  14798. * *err_str - pointer to error string buffer
  14799. ******************************************************************************/
  14800. static int pt_bist_xres_test(struct device *dev,
  14801. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14802. {
  14803. struct pt_core_data *cd = dev_get_drvdata(dev);
  14804. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14805. u8 *read_buf = NULL;
  14806. u8 mode = PT_MODE_UNKNOWN;
  14807. int rc = 0;
  14808. int t = 0;
  14809. int timeout = 300;
  14810. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14811. if (read_buf == NULL) {
  14812. rc = -ENOMEM;
  14813. goto exit;
  14814. }
  14815. /* Clear the startup bit mask, reset and enum will re-populate it */
  14816. cd->startup_status = STARTUP_STATUS_START;
  14817. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14818. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14819. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14820. rc = -ENODEV;
  14821. goto exit;
  14822. }
  14823. /* Ensure we have nothing pending on active bus */
  14824. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14825. /* Perform a hard XRES toggle and wait for reset sentinel */
  14826. mutex_lock(&cd->system_lock);
  14827. cd->hid_reset_cmd_state = 1;
  14828. mutex_unlock(&cd->system_lock);
  14829. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14830. rc = pt_hw_hard_reset(cd);
  14831. /* Set timeout to 1s for the flashless case where a BL could be done */
  14832. if (cd->flashless_dut)
  14833. timeout = 1000;
  14834. /*
  14835. * To avoid the case that next PIP command can be confused by BL/FW
  14836. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14837. * followed by "wakeup event" function can lower the failure rate.
  14838. */
  14839. t = wait_event_timeout(cd->wait_q,
  14840. ((cd->startup_status != STARTUP_STATUS_START)
  14841. && (cd->hid_reset_cmd_state == 0)),
  14842. msecs_to_jiffies(timeout));
  14843. if (IS_TMO(t)) {
  14844. pt_debug(cd->dev, DL_ERROR,
  14845. "%s: TMO waiting for sentinel\n", __func__);
  14846. *xres_toggled = 0;
  14847. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14848. /*
  14849. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14850. * To enter the BL we need to generate an XRES so first try to
  14851. * launch the applicaiton
  14852. */
  14853. if (cd->mode == PT_MODE_BOOTLOADER)
  14854. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14855. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14856. __func__);
  14857. mode = PT_MODE_IGNORE;
  14858. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14859. if (rc) {
  14860. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14861. __func__);
  14862. *xres_toggled = 0;
  14863. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14864. goto exit;
  14865. } else {
  14866. /* Wait for the BL sentinel */
  14867. t = wait_event_timeout(cd->wait_q,
  14868. (cd->startup_status != STARTUP_STATUS_START),
  14869. msecs_to_jiffies(500));
  14870. if (IS_TMO(t)) {
  14871. pt_debug(cd->dev, DL_ERROR,
  14872. "%s: TMO waiting for BL sentinel\n",
  14873. __func__);
  14874. *xres_toggled = 0;
  14875. strlcpy(err_str,
  14876. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14877. rc = -ETIME;
  14878. goto exit;
  14879. }
  14880. }
  14881. }
  14882. mutex_lock(&cd->system_lock);
  14883. cd->hid_reset_cmd_state = 0;
  14884. mutex_unlock(&cd->system_lock);
  14885. /* Look for BL or FW reset sentinels */
  14886. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14887. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14888. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14889. /* If a sentinel was seen, all nets are working */
  14890. *xres_toggled = 1;
  14891. *irq_toggled = 1;
  14892. /*
  14893. * For SPI test, bus read result can be confused as FW sentinel
  14894. * if MISO(slave) is connected to MISO(host).
  14895. */
  14896. if (cd->bus_ops->bustype == BUS_I2C)
  14897. *bus_toggled = 1;
  14898. } else {
  14899. /*
  14900. * Hard reset failed, however some additional information
  14901. * could be determined. Try a soft reset to see if DUT resets
  14902. * with the possible outcomes:
  14903. * - if it resets the line is not stuck asserted
  14904. * - if it does not reset the line could be stuck asserted
  14905. */
  14906. *xres_toggled = 0;
  14907. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14908. msleep(30);
  14909. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14910. __func__, rc);
  14911. if (rc) {
  14912. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14913. pt_debug(dev, DL_ERROR,
  14914. "%s: Hard reset failed, soft reset failed %s\n",
  14915. __func__, err_str);
  14916. goto exit;
  14917. }
  14918. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14919. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14920. strlcpy(err_str,
  14921. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14922. pt_debug(dev, DL_ERROR,
  14923. "%s: Hard reset failed, soft reset passed-%s\n",
  14924. __func__, err_str);
  14925. } else if (cd->startup_status == 0) {
  14926. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14927. pt_debug(dev, DL_ERROR,
  14928. "%s: Hard reset failed, soft reset failed-%s\n",
  14929. __func__, err_str);
  14930. } else {
  14931. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14932. pt_debug(dev, DL_ERROR,
  14933. "%s: Hard and Soft reset failed - %s\n",
  14934. __func__, err_str);
  14935. }
  14936. }
  14937. exit:
  14938. kfree(read_buf);
  14939. return rc;
  14940. }
  14941. /*******************************************************************************
  14942. * FUNCTION: pt_bist_slave_irq_test
  14943. *
  14944. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14945. *
  14946. * This test will ensure there is a good IRQ connection between the master
  14947. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14948. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14949. * saw each slave trigger the IRQ with it's reset sentinel.
  14950. *
  14951. * RETURN:
  14952. * 0 = Success
  14953. * !0 = Failure
  14954. *
  14955. * PARAMETERS:
  14956. * *dev - pointer to device structure
  14957. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14958. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14959. * *err_str - pointer to error string buffer
  14960. * *slave_detect - pointer to slave detect buffer
  14961. * *boot_err - pointer to boot_err buffer
  14962. ******************************************************************************/
  14963. static int pt_bist_slave_irq_test(struct device *dev,
  14964. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14965. u8 *slave_detect, u8 *boot_err)
  14966. {
  14967. struct pt_core_data *cd = dev_get_drvdata(dev);
  14968. u8 mode = PT_MODE_UNKNOWN;
  14969. u8 status;
  14970. u8 boot;
  14971. u8 read_buf[12];
  14972. u8 detected = 0;
  14973. u8 last_err = -1;
  14974. u16 actual_read_len;
  14975. int result = 0;
  14976. int rc = 0;
  14977. /*
  14978. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14979. * detect bits. If the DUT was in FW, entering the BL will cause an
  14980. * XRES signal which will inadvertently test the XRES net as well
  14981. */
  14982. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14983. if (rc) {
  14984. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14985. __func__, rc);
  14986. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14987. goto exit;
  14988. }
  14989. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14990. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14991. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14992. &actual_read_len);
  14993. if (!rc) {
  14994. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14995. "PIP2 STATUS");
  14996. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14997. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14998. /* Slave detect is only valid if status ok and in boot exec */
  14999. if (status == PIP2_RSP_ERR_NONE &&
  15000. boot == PIP2_STATUS_BOOT_EXEC) {
  15001. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  15002. SLAVE_DETECT_MASK;
  15003. } else {
  15004. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  15005. rc = -EPERM;
  15006. }
  15007. } else {
  15008. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  15009. __func__);
  15010. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  15011. goto exit;
  15012. }
  15013. /*
  15014. * Retrieve boot error regardless of the state of the slave detect
  15015. * bit because the IRQ could have been stuck high or low.
  15016. */
  15017. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  15018. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  15019. read_buf, &actual_read_len);
  15020. if (!rc) {
  15021. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  15022. "PIP2 GET_LAST_ERRNO");
  15023. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  15024. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  15025. if (last_err) {
  15026. pt_debug(cd->dev, DL_ERROR,
  15027. "%s: Master Boot Last Err = 0x%02X\n",
  15028. __func__, last_err);
  15029. }
  15030. } else {
  15031. pt_debug(cd->dev, DL_ERROR,
  15032. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  15033. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  15034. }
  15035. exit:
  15036. pt_debug(cd->dev, DL_INFO,
  15037. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  15038. __func__, rc, detected, last_err);
  15039. if (err_str && last_err) {
  15040. if (detected)
  15041. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15042. "- Likely stuck low. Boot Error:",
  15043. last_err);
  15044. else
  15045. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15046. "- Likely stuck high. Boot Error:",
  15047. last_err);
  15048. }
  15049. /* Ignore an invalid image error as BIST doesn't need valid FW */
  15050. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  15051. last_err = PIP2_RSP_ERR_NONE;
  15052. if (slave_irq_toggled)
  15053. *slave_irq_toggled = (detected && !last_err) ? true : false;
  15054. if (slave_bus_toggled) {
  15055. /* Leave as UNTEST if slave not detected */
  15056. if (detected)
  15057. *slave_bus_toggled = !last_err ? true : false;
  15058. }
  15059. if (slave_detect)
  15060. *slave_detect = detected;
  15061. if (boot_err)
  15062. *boot_err = last_err;
  15063. if (slave_irq_toggled && slave_bus_toggled)
  15064. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  15065. __func__,
  15066. "Detected", detected,
  15067. "slave_irq_toggled", *slave_irq_toggled,
  15068. "slave_bus_toggled", *slave_bus_toggled);
  15069. return rc;
  15070. }
  15071. /*******************************************************************************
  15072. * FUNCTION: pt_bist_slave_xres_test
  15073. *
  15074. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  15075. *
  15076. * This test will ensure there is a good TP_XRES connection between the
  15077. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  15078. * master, the STATUS command is sent and the 'Slave Detect' bits are
  15079. * verified to ensure the master DUT saw each slave trigger the IRQ with
  15080. * it's reset sentinel.
  15081. *
  15082. * RETURN:
  15083. * 0 = Success
  15084. * !0 = Failure
  15085. *
  15086. * PARAMETERS:
  15087. * *dev - pointer to device structure
  15088. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  15089. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  15090. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  15091. * *err_str - pointer to error string buffer
  15092. ******************************************************************************/
  15093. static int pt_bist_slave_xres_test(struct device *dev,
  15094. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  15095. u8 *err_str)
  15096. {
  15097. struct pt_core_data *cd = dev_get_drvdata(dev);
  15098. u8 slave_detect = 0;
  15099. u8 boot_err = 0;
  15100. int rc = 0;
  15101. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  15102. mutex_lock(&cd->system_lock);
  15103. cd->hid_reset_cmd_state = 1;
  15104. mutex_unlock(&cd->system_lock);
  15105. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  15106. pt_hw_hard_reset(cd);
  15107. msleep(100);
  15108. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  15109. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  15110. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  15111. if (!rc && *slave_irq_toggled == false) {
  15112. /*
  15113. * If the slave IRQ did not toggle, either the slave_detect
  15114. * bit was not set or we had a boot error. If the slave
  15115. * detect was not set the slave did not reset causing a boot
  15116. * error.
  15117. */
  15118. if (!slave_detect)
  15119. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  15120. else
  15121. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15122. "- likely open or an IRQ issue. Boot Error:",
  15123. boot_err);
  15124. }
  15125. if (slave_xres_toggled) {
  15126. if (!rc)
  15127. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  15128. else
  15129. *slave_xres_toggled = false;
  15130. }
  15131. return rc;
  15132. }
  15133. /*******************************************************************************
  15134. * FUNCTION: pt_bist_slave_bus_test
  15135. *
  15136. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  15137. *
  15138. * This test will ensure a good SPI bus connection between the
  15139. * master DUT and the slave DUT(s). This bus connection is ensured by
  15140. * opening file 0 (SRAM loader). If there is a slave and the open fails
  15141. * then there is a master/slave communication issue. Opening file 0 on
  15142. * the master will open it on the slave as well if the slave was detected.
  15143. *
  15144. * RETURN:
  15145. * 0 = Success
  15146. * !0 = Failure
  15147. *
  15148. * PARAMETERS:
  15149. * *dev - pointer to device structure
  15150. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  15151. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  15152. * *err_str - pointer to error string buffer
  15153. ******************************************************************************/
  15154. static int pt_bist_slave_bus_test(struct device *dev,
  15155. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  15156. {
  15157. struct pt_core_data *cd = dev_get_drvdata(dev);
  15158. u8 mode = PT_MODE_UNKNOWN;
  15159. u8 bus_toggled = false;
  15160. u8 file_handle;
  15161. int result = 0;
  15162. int rc = 0;
  15163. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  15164. if (rc) {
  15165. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  15166. __func__, rc);
  15167. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  15168. goto exit;
  15169. }
  15170. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  15171. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  15172. if (file_handle != PIP2_RAM_FILE) {
  15173. rc = -ENOENT;
  15174. bus_toggled = false;
  15175. pt_debug(dev, DL_ERROR,
  15176. "%s Failed to open bin file\n", __func__);
  15177. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  15178. goto exit;
  15179. } else {
  15180. bus_toggled = true;
  15181. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  15182. pt_debug(dev, DL_ERROR,
  15183. "%s: File Close failed, file_handle=%d\n",
  15184. __func__, file_handle);
  15185. }
  15186. }
  15187. exit:
  15188. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  15189. if (slave_irq_toggled)
  15190. *slave_irq_toggled = bus_toggled;
  15191. if (slave_bus_toggled)
  15192. *slave_bus_toggled = bus_toggled;
  15193. return rc;
  15194. }
  15195. /*******************************************************************************
  15196. * FUNCTION: pt_ttdl_bist_show
  15197. *
  15198. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  15199. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  15200. *
  15201. * NOTE: This function will reset the DUT and the startup_status bit
  15202. * mask. A pt_enum will be queued after completion.
  15203. *
  15204. * NOTE: The order of the net tests is done to optimize the time it takes
  15205. * to run. The first test is capable of verifying all nets, each subsequent
  15206. * test is only run if the previous was not able to see all nets toggle.
  15207. *
  15208. * RETURN:
  15209. * 0 = success
  15210. * !0 = failure
  15211. *
  15212. * PARAMETERS:
  15213. * *dev - pointer to device structure
  15214. * *attr - pointer to device attributes
  15215. * *buf - pointer to output buffer
  15216. ******************************************************************************/
  15217. static ssize_t pt_ttdl_bist_show(struct device *dev,
  15218. struct device_attribute *attr, char *buf)
  15219. {
  15220. struct pt_core_data *cd = dev_get_drvdata(dev);
  15221. ssize_t ret;
  15222. char *bus_err_str = NULL;
  15223. char *irq_err_str = NULL;
  15224. char *xres_err_str = NULL;
  15225. char *slave_bus_err_str = NULL;
  15226. char *slave_irq_err_str = NULL;
  15227. char *slave_xres_err_str = NULL;
  15228. u8 tests;
  15229. int rc = 0;
  15230. int num_tests = 0;
  15231. int status = 1; /* 0 = Pass, !0 = fail */
  15232. u8 bus_toggled = 0x0F; /* default to untested */
  15233. u8 i2c_toggled = 0x0F; /* default to untested */
  15234. u8 spi_toggled = 0x0F; /* default to untested */
  15235. u8 irq_toggled = 0x0F; /* default to untested */
  15236. u8 xres_toggled = 0x0F; /* default to untested */
  15237. u8 slave_bus_toggled = 0x0F; /* default to untested */
  15238. u8 slave_irq_toggled = 0x0F; /* default to untested */
  15239. u8 slave_xres_toggled = 0x0F; /* default to untested */
  15240. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15241. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15242. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15243. if (!bus_err_str || !irq_err_str || !xres_err_str)
  15244. goto print_results;
  15245. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15246. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15247. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15248. if (cd->multi_chip) {
  15249. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15250. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15251. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15252. if (!slave_bus_err_str ||
  15253. !slave_irq_err_str ||
  15254. !slave_xres_err_str)
  15255. goto print_results;
  15256. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15257. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15258. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15259. }
  15260. /* Turn off the TTDL WD during the test */
  15261. pt_stop_wd_timer(cd);
  15262. /* Shorten default PIP cmd timeout while running BIST */
  15263. cd->pip_cmd_timeout = 200;
  15264. /* Count the number of tests to run */
  15265. tests = cd->ttdl_bist_select;
  15266. while (tests) {
  15267. num_tests += tests & 1;
  15268. tests >>= 1;
  15269. }
  15270. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  15271. __func__, cd->ttdl_bist_select, num_tests);
  15272. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  15273. if (cd->flashless_dut) {
  15274. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  15275. __func__);
  15276. mutex_lock(&cd->system_lock);
  15277. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15278. mutex_unlock(&cd->system_lock);
  15279. }
  15280. /* --------------- TP_XRES BIST TEST --------------- */
  15281. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  15282. pt_debug(dev, DL_INFO,
  15283. "%s: ----- Start TP_XRES BIST -----", __func__);
  15284. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  15285. &xres_toggled, xres_err_str);
  15286. /* Done if the rest of all nets toggled */
  15287. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  15288. goto host_nets_complete;
  15289. }
  15290. /* Flush bus in case a PIP response is waiting from previous test */
  15291. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15292. /* --------------- IRQ BIST TEST --------------- */
  15293. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  15294. pt_debug(dev, DL_INFO,
  15295. "%s: ----- Start IRQ BIST -----", __func__);
  15296. bus_toggled = 0xFF;
  15297. irq_toggled = 0xFF;
  15298. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  15299. &xres_toggled, irq_err_str);
  15300. /* If this net failed clear results from previous net */
  15301. if (irq_toggled != 1) {
  15302. xres_toggled = 0x0F;
  15303. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15304. }
  15305. if (bus_toggled == 1 && irq_toggled == 1)
  15306. goto host_nets_complete;
  15307. }
  15308. /* Flush bus in case a PIP response is waiting from previous test */
  15309. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15310. /* --------------- BUS BIST TEST --------------- */
  15311. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15312. pt_debug(dev, DL_INFO,
  15313. "%s: ----- Start BUS BIST -----", __func__);
  15314. bus_toggled = 0xFF;
  15315. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15316. /* If this net failed clear results from previous net */
  15317. if (bus_toggled == 0) {
  15318. irq_toggled = 0x0F;
  15319. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15320. }
  15321. }
  15322. host_nets_complete:
  15323. /* --------------- SLAVE XRES BIST TEST --------------- */
  15324. if (cd->multi_chip &&
  15325. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15326. pt_debug(dev, DL_INFO,
  15327. "%s: ----- Start Slave XRES BIST -----", __func__);
  15328. slave_xres_toggled = 0xFF;
  15329. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15330. &slave_bus_toggled, &slave_xres_toggled,
  15331. slave_xres_err_str);
  15332. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15333. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15334. goto print_results;
  15335. }
  15336. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15337. if (cd->multi_chip &&
  15338. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15339. pt_debug(dev, DL_INFO,
  15340. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15341. slave_irq_toggled = 0xFF;
  15342. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15343. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15344. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15345. __func__, slave_irq_toggled);
  15346. if (slave_irq_toggled == 1) {
  15347. slave_bus_toggled = 1;
  15348. goto print_results;
  15349. }
  15350. }
  15351. /* --------------- SLAVE BUS BIST TEST --------------- */
  15352. if (cd->multi_chip &&
  15353. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15354. pt_debug(dev, DL_INFO,
  15355. "%s: ----- Start Slave BUS BIST -----", __func__);
  15356. slave_bus_toggled = 0xFF;
  15357. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15358. &slave_bus_toggled, slave_bus_err_str);
  15359. }
  15360. print_results:
  15361. /* Restore PIP command timeout */
  15362. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15363. /*
  15364. * We're done! - Perform a hard XRES toggle, allowing BL
  15365. * to load FW if there is any in Flash
  15366. */
  15367. mutex_lock(&cd->system_lock);
  15368. cd->hid_reset_cmd_state = 0;
  15369. mutex_unlock(&cd->system_lock);
  15370. pt_debug(dev, DL_INFO,
  15371. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15372. if (cd->flashless_dut) {
  15373. /*
  15374. * For Flashless solution, FW update is triggered after BL is
  15375. * seen that several miliseconds delay is needed.
  15376. */
  15377. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15378. __func__);
  15379. mutex_lock(&cd->system_lock);
  15380. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15381. mutex_unlock(&cd->system_lock);
  15382. /* Reset DUT and wait 100ms to see if loader started */
  15383. pt_hw_hard_reset(cd);
  15384. msleep(100);
  15385. if (cd->fw_updating) {
  15386. pt_debug(dev, DL_INFO,
  15387. "%s: ----- BIST Wait FW Loading ----",
  15388. __func__);
  15389. rc = _pt_request_wait_for_enum_state(
  15390. dev, 4000, STARTUP_STATUS_COMPLETE);
  15391. }
  15392. } else {
  15393. if (cd->mode == PT_MODE_BOOTLOADER) {
  15394. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15395. if (rc) {
  15396. pt_debug(dev, DL_ERROR,
  15397. "%s Failed to launch app\n", __func__);
  15398. rc = pt_hw_hard_reset(cd);
  15399. }
  15400. }
  15401. /*
  15402. * If FW exists the BL may have just started or will start soon,
  15403. * so the FW sentinel may be on it's way but with no FW it will
  15404. * not arrive, wait for it before deciding if we need to queue
  15405. * an enum.
  15406. */
  15407. rc = _pt_request_wait_for_enum_state(
  15408. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15409. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15410. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15411. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15412. __func__);
  15413. pt_queue_enum(cd);
  15414. rc = _pt_request_wait_for_enum_state(
  15415. dev, 2000, STARTUP_STATUS_COMPLETE);
  15416. }
  15417. }
  15418. msleep(20);
  15419. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15420. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15421. pt_start_wd_timer(cd);
  15422. /* Canned print if any memory allocation issues */
  15423. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15424. ret = scnprintf(buf, strlen(buf),
  15425. "Status: %d\n"
  15426. "I2C (SDA,SCL): [UNTEST]\n"
  15427. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15428. "IRQ connection: [UNTEST]\n"
  15429. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15430. if (cd->multi_chip) {
  15431. ret += scnprintf(buf + ret, strlen(buf),
  15432. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15433. "I/P IRQ connection: [UNTEST]\n"
  15434. "I/P TP_XRES connection: [UNTEST]\n");
  15435. }
  15436. } else {
  15437. status = 0;
  15438. if (bus_toggled == 1)
  15439. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15440. if (irq_toggled == 1)
  15441. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15442. if (xres_toggled == 1)
  15443. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15444. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15445. status += bus_toggled;
  15446. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15447. status += irq_toggled;
  15448. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15449. status += xres_toggled;
  15450. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15451. __func__, status, bus_toggled, irq_toggled,
  15452. xres_toggled);
  15453. if (cd->multi_chip) {
  15454. if (slave_irq_toggled == 1)
  15455. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15456. if (slave_xres_toggled == 1)
  15457. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15458. if (slave_bus_toggled == 1)
  15459. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15460. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15461. status += slave_bus_toggled;
  15462. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15463. status += slave_irq_toggled;
  15464. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15465. status += slave_xres_toggled;
  15466. pt_debug(dev, DL_ERROR,
  15467. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15468. __func__, status, bus_toggled, irq_toggled,
  15469. xres_toggled, slave_bus_toggled,
  15470. slave_irq_toggled, slave_xres_toggled);
  15471. }
  15472. if (cd->bus_ops->bustype == BUS_I2C)
  15473. i2c_toggled = bus_toggled;
  15474. else
  15475. spi_toggled = bus_toggled;
  15476. ret = scnprintf(buf, strlen(buf),
  15477. "Status: %d\n"
  15478. "I2C (SDA,SCL): %s %s\n"
  15479. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15480. "IRQ connection: %s %s\n"
  15481. "TP_XRES connection: %s %s\n",
  15482. status == num_tests ? 0 : 1,
  15483. i2c_toggled == 0x0F ? "[UNTEST]" :
  15484. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15485. i2c_toggled == 0x0F ? "" : bus_err_str,
  15486. spi_toggled == 0x0F ? "[UNTEST]" :
  15487. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15488. spi_toggled == 0x0F ? "" : bus_err_str,
  15489. irq_toggled == 0x0F ? "[UNTEST]" :
  15490. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15491. irq_err_str,
  15492. xres_toggled == 0x0F ? "[UNTEST]" :
  15493. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15494. xres_err_str);
  15495. if (cd->multi_chip) {
  15496. ret += scnprintf(buf + ret, strlen(buf),
  15497. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15498. "I/P IRQ connection: %s %s\n"
  15499. "I/P TP_XRES connection: %s %s\n",
  15500. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15501. slave_bus_toggled == 1 ? "[ OK ]" :
  15502. "[FAILED]", slave_bus_err_str,
  15503. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15504. slave_irq_toggled == 1 ? "[ OK ]" :
  15505. "[FAILED]", slave_irq_err_str,
  15506. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15507. slave_xres_toggled == 1 ? "[ OK ]" :
  15508. "[FAILED]", slave_xres_err_str);
  15509. }
  15510. }
  15511. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15512. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15513. __func__, cd->startup_status);
  15514. kfree(bus_err_str);
  15515. kfree(irq_err_str);
  15516. kfree(xres_err_str);
  15517. return ret;
  15518. }
  15519. /*******************************************************************************
  15520. * FUNCTION: pt_ttdl_bist_store
  15521. *
  15522. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15523. *
  15524. * RETURN: Size of passed in buffer if successful
  15525. *
  15526. * PARAMETERS:
  15527. * *dev - pointer to device structure
  15528. * *attr - pointer to device attributes
  15529. * *buf - pointer to command buffer
  15530. * size - size of buf
  15531. ******************************************************************************/
  15532. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15533. struct device_attribute *attr, const char *buf, size_t size)
  15534. {
  15535. struct pt_core_data *cd = dev_get_drvdata(dev);
  15536. u32 input_data[2] = {0};
  15537. int length;
  15538. int rc = 0;
  15539. /* Maximum input of one value */
  15540. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15541. ARRAY_SIZE(input_data));
  15542. if (length != 1) {
  15543. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15544. __func__);
  15545. rc = -EINVAL;
  15546. goto exit;
  15547. } else {
  15548. mutex_lock(&cd->system_lock);
  15549. cd->ttdl_bist_select = input_data[0];
  15550. mutex_unlock(&cd->system_lock);
  15551. }
  15552. exit:
  15553. if (rc)
  15554. return rc;
  15555. return size;
  15556. }
  15557. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15558. pt_ttdl_bist_store);
  15559. /*******************************************************************************
  15560. * FUNCTION: pt_flush_bus_store
  15561. *
  15562. * SUMMARY: Store method for the flush_bus sysfs node.
  15563. *
  15564. * RETURN: Size of passed in buffer if successful
  15565. *
  15566. * PARAMETERS:
  15567. * *dev - pointer to device structure
  15568. * *attr - pointer to device attributes
  15569. * *buf - pointer to command buffer
  15570. * size - size of buf
  15571. ******************************************************************************/
  15572. static ssize_t pt_flush_bus_store(struct device *dev,
  15573. struct device_attribute *attr, const char *buf, size_t size)
  15574. {
  15575. struct pt_core_data *cd = dev_get_drvdata(dev);
  15576. u32 input_data[2] = {0};
  15577. int length;
  15578. int rc = 0;
  15579. /* Maximum input of one value */
  15580. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15581. ARRAY_SIZE(input_data));
  15582. if (length != 1) {
  15583. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15584. __func__);
  15585. rc = -EINVAL;
  15586. goto exit;
  15587. }
  15588. mutex_lock(&cd->system_lock);
  15589. if (input_data[0] == 0)
  15590. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15591. else if (input_data[0] == 1)
  15592. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15593. else
  15594. rc = -EINVAL;
  15595. mutex_unlock(&cd->system_lock);
  15596. exit:
  15597. if (rc)
  15598. return rc;
  15599. return size;
  15600. }
  15601. /*******************************************************************************
  15602. * FUNCTION: pt_flush_bus_show
  15603. *
  15604. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15605. * based on either the size of the first two bytes or a blind 256 bytes.
  15606. *
  15607. * RETURN:
  15608. * 0 = success
  15609. * !0 = failure
  15610. *
  15611. * PARAMETERS:
  15612. * *dev - pointer to device structure
  15613. * *attr - pointer to device attributes
  15614. * *buf - pointer to output buffer
  15615. ******************************************************************************/
  15616. static ssize_t pt_flush_bus_show(struct device *dev,
  15617. struct device_attribute *attr, char *buf)
  15618. {
  15619. ssize_t ret = 0;
  15620. ssize_t bytes = 0;
  15621. struct pt_core_data *cd = dev_get_drvdata(dev);
  15622. mutex_lock(&cd->system_lock);
  15623. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15624. ret = scnprintf(buf, strlen(buf),
  15625. "Status: 0\n"
  15626. "%s: %zd\n",
  15627. "Bytes flushed", bytes);
  15628. mutex_unlock(&cd->system_lock);
  15629. return ret;
  15630. }
  15631. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15632. pt_flush_bus_store);
  15633. /*******************************************************************************
  15634. * FUNCTION: pt_pip2_ping_test_store
  15635. *
  15636. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15637. *
  15638. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15639. * someone may want to test sending invalid packet lengths so any values
  15640. * up to 255 are allowed.
  15641. *
  15642. * RETURN: Size of passed in buffer if successful
  15643. *
  15644. * PARAMETERS:
  15645. * *dev - pointer to device structure
  15646. * *attr - pointer to device attributes
  15647. * *buf - pointer to command buffer
  15648. * size - size of buf
  15649. ******************************************************************************/
  15650. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15651. struct device_attribute *attr, const char *buf, size_t size)
  15652. {
  15653. struct pt_core_data *cd = dev_get_drvdata(dev);
  15654. u32 input_data[2];
  15655. int length;
  15656. int rc = 0;
  15657. /* Maximum input of one value */
  15658. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15659. ARRAY_SIZE(input_data));
  15660. if (length != 1) {
  15661. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15662. __func__);
  15663. rc = -EINVAL;
  15664. goto exit;
  15665. }
  15666. mutex_lock(&cd->system_lock);
  15667. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15668. cd->ping_test_size = input_data[0];
  15669. else
  15670. rc = -EINVAL;
  15671. mutex_unlock(&cd->system_lock);
  15672. exit:
  15673. if (rc)
  15674. return rc;
  15675. return size;
  15676. }
  15677. /*******************************************************************************
  15678. * FUNCTION: pt_ping_test_show
  15679. *
  15680. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15681. * PING command and ramps up the optional payload from 0 to
  15682. * ping_test_size.
  15683. * The max payload size is 247:
  15684. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15685. *
  15686. * RETURN:
  15687. * 0 = success
  15688. * !0 = failure
  15689. *
  15690. * PARAMETERS:
  15691. * *dev - pointer to device structure
  15692. * *attr - pointer to device attributes
  15693. * *buf - pointer to output buffer
  15694. ******************************************************************************/
  15695. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15696. struct device_attribute *attr, char *buf)
  15697. {
  15698. struct pt_core_data *cd = dev_get_drvdata(dev);
  15699. ssize_t ret;
  15700. int last_packet_size;
  15701. int rc = 0;
  15702. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15703. if (rc) {
  15704. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15705. return ret;
  15706. }
  15707. ret = scnprintf(buf, strlen(buf),
  15708. "Status: %d\n"
  15709. "PING payload test passed with packet sizes 0 - %d\n",
  15710. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15711. last_packet_size);
  15712. return ret;
  15713. }
  15714. /*******************************************************************************
  15715. * FUNCTION: pt_t_refresh_store
  15716. *
  15717. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15718. * in integer as the number of interrupts to count. A timer is started to
  15719. * calculate the total time it takes to see that number of interrupts.
  15720. *
  15721. * RETURN: Size of passed in buffer if successful
  15722. *
  15723. * PARAMETERS:
  15724. * *dev - pointer to device structure
  15725. * *attr - pointer to device attributes
  15726. * *buf - pointer to buffer that hold the command parameters
  15727. * size - size of buf
  15728. ******************************************************************************/
  15729. static ssize_t pt_t_refresh_store(struct device *dev,
  15730. struct device_attribute *attr, const char *buf, size_t size)
  15731. {
  15732. struct pt_core_data *cd = dev_get_drvdata(dev);
  15733. u32 input_data[2];
  15734. int length;
  15735. int rc = 0;
  15736. /* Maximum input of one value */
  15737. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15738. ARRAY_SIZE(input_data));
  15739. if (length != 1) {
  15740. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15741. __func__);
  15742. rc = -EINVAL;
  15743. goto exit;
  15744. }
  15745. mutex_lock(&cd->system_lock);
  15746. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15747. input_data[0]);
  15748. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15749. cd->t_refresh_total = input_data[0];
  15750. cd->t_refresh_count = 0;
  15751. cd->t_refresh_active = 1;
  15752. } else {
  15753. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15754. rc = -EINVAL;
  15755. }
  15756. mutex_unlock(&cd->system_lock);
  15757. exit:
  15758. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15759. if (rc)
  15760. return rc;
  15761. return size;
  15762. }
  15763. /*******************************************************************************
  15764. * FUNCTION: pt_t_refresh_show
  15765. *
  15766. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15767. * of the T-Refresh timer counting the time it takes to see a user defined
  15768. * number of interrupts.
  15769. *
  15770. * RETURN:
  15771. * 0 = success
  15772. * !0 = failure
  15773. *
  15774. * PARAMETERS:
  15775. * *dev - pointer to device structure
  15776. * *attr - pointer to device attributes
  15777. * *buf - pointer to output buffer
  15778. ******************************************************************************/
  15779. static ssize_t pt_t_refresh_show(struct device *dev,
  15780. struct device_attribute *attr, char *buf)
  15781. {
  15782. ssize_t ret = 0;
  15783. struct pt_core_data *cd = dev_get_drvdata(dev);
  15784. u32 whole;
  15785. u16 fraction;
  15786. mutex_lock(&cd->system_lock);
  15787. /* Check if we have counted the number requested */
  15788. if (cd->t_refresh_count != cd->t_refresh_total) {
  15789. ret = scnprintf(buf, strlen(buf),
  15790. "Status: 0\n"
  15791. "%s: %d\n",
  15792. "Still counting... current IRQ count",
  15793. cd->t_refresh_count);
  15794. } else {
  15795. /* Ensure T-Refresh is de-activated */
  15796. cd->t_refresh_active = 0;
  15797. whole = cd->t_refresh_time / cd->t_refresh_count;
  15798. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15799. fraction = fraction * 1000 / cd->t_refresh_count;
  15800. ret = scnprintf(buf, strlen(buf),
  15801. "Status: 0\n"
  15802. "%s: %d\n"
  15803. "%s: %d\n"
  15804. "%s: %d\n"
  15805. "%s: %d.%02d\n",
  15806. "Requested IRQ Count ", cd->t_refresh_total,
  15807. "IRQ Counted ", cd->t_refresh_count,
  15808. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15809. "Average T-Refresh (ms) ", whole, fraction);
  15810. }
  15811. mutex_unlock(&cd->system_lock);
  15812. return ret;
  15813. }
  15814. /*******************************************************************************
  15815. * FUNCTION: pt_dut_status_show
  15816. *
  15817. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15818. * more items such as operation mode,easywake state are added in the future.
  15819. *
  15820. * RETURN: Char buffer with printed scan status information
  15821. *
  15822. * PARAMETERS:
  15823. * *dev - pointer to device structure
  15824. * *attr - pointer to device attributes
  15825. * *buf - pointer to output buffer
  15826. ******************************************************************************/
  15827. static ssize_t pt_dut_status_show(struct device *dev,
  15828. struct device_attribute *attr, char *buf)
  15829. {
  15830. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15831. u8 mode = PT_MODE_UNKNOWN;
  15832. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15833. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15834. struct pt_core_data *cd = dev_get_drvdata(dev);
  15835. ssize_t ret;
  15836. u16 calculated_crc = 0;
  15837. u16 stored_crc = 0;
  15838. u8 status;
  15839. int rc = 0;
  15840. /* In STANDBY the DUT will not repond to any PIP cmd */
  15841. if (cd->fw_sys_mode_in_standby_state) {
  15842. mode = PT_MODE_OPERATIONAL;
  15843. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15844. goto print_limited_results;
  15845. }
  15846. /* Retrieve mode and FW system mode which can only be 0-4 */
  15847. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15848. if (rc || mode == PT_MODE_UNKNOWN) {
  15849. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15850. "%s: %d\n"
  15851. "%s: n/a\n"
  15852. "%s: n/a\n"
  15853. "%s: n/a\n"
  15854. "%s: n/a\n",
  15855. "Status", rc,
  15856. "Active Exec ",
  15857. "FW System Mode ",
  15858. "Stored CRC ",
  15859. "Calculated CRC ");
  15860. return ret;
  15861. } else {
  15862. if (mode == PT_MODE_OPERATIONAL) {
  15863. if (sys_mode > FW_SYS_MODE_MAX)
  15864. sys_mode = FW_SYS_MODE_UNDEFINED;
  15865. if (sys_mode != FW_SYS_MODE_TEST)
  15866. goto print_limited_results;
  15867. rc = pt_pip_verify_config_block_crc_(cd,
  15868. PT_TCH_PARM_EBID, &status,
  15869. &calculated_crc, &stored_crc);
  15870. if (rc)
  15871. goto print_limited_results;
  15872. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15873. "%s: %d\n"
  15874. "%s: %s\n"
  15875. "%s: %s\n"
  15876. "%s: 0x%04X\n"
  15877. "%s: 0x%04X\n",
  15878. "Status", rc,
  15879. "Active Exec ", "FW",
  15880. "FW System Mode ", outputstring[sys_mode],
  15881. "Stored CRC ", stored_crc,
  15882. "Calculated CRC ", calculated_crc);
  15883. return ret;
  15884. } else {
  15885. /* When in BL or unknon mode Active Exec is "n/a" */
  15886. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15887. }
  15888. }
  15889. print_limited_results:
  15890. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15891. "%s: %d\n"
  15892. "%s: %s\n"
  15893. "%s: %s\n"
  15894. "%s: n/a\n"
  15895. "%s: n/a\n",
  15896. "Status", rc,
  15897. "Active Exec ",
  15898. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15899. "FW System Mode ", outputstring[sys_mode],
  15900. "Stored CRC ",
  15901. "Calculated CRC ");
  15902. return ret;
  15903. }
  15904. #endif /* TTDL_DIAGNOSTICS */
  15905. /*******************************************************************************
  15906. * Structures of sysfs attributes for all DUT dependent sysfs node
  15907. ******************************************************************************/
  15908. static struct attribute *early_attrs[] = {
  15909. &dev_attr_hw_version.attr,
  15910. &dev_attr_drv_version.attr,
  15911. &dev_attr_drv_ver.attr,
  15912. &dev_attr_fw_version.attr,
  15913. &dev_attr_sysinfo.attr,
  15914. &dev_attr_pip2_cmd_rsp.attr,
  15915. &dev_attr_command.attr,
  15916. &dev_attr_drv_debug.attr,
  15917. &dev_attr_hw_reset.attr,
  15918. &dev_attr_response.attr,
  15919. &dev_attr_ttdl_restart.attr,
  15920. #ifdef TTDL_DIAGNOSTICS
  15921. &dev_attr_ttdl_status.attr,
  15922. &dev_attr_pip2_enter_bl.attr,
  15923. &dev_attr_pip2_exit_bl.attr,
  15924. &dev_attr_err_gpio.attr,
  15925. &dev_attr_flush_bus.attr,
  15926. &dev_attr_ttdl_bist.attr,
  15927. #endif /* TTDL_DIAGNOSTICS */
  15928. NULL,
  15929. };
  15930. static struct attribute_group early_attr_group = {
  15931. .attrs = early_attrs,
  15932. };
  15933. static struct device_attribute pip2_attributes[] = {
  15934. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15935. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15936. #ifdef TTDL_DIAGNOSTICS
  15937. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15938. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15939. pt_pip2_ping_test_store),
  15940. #endif
  15941. };
  15942. static struct device_attribute attributes[] = {
  15943. __ATTR(dut_debug, 0644,
  15944. pt_dut_debug_show, pt_drv_debug_store),
  15945. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15946. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15947. __ATTR(get_param, 0644,
  15948. pt_get_param_show, pt_get_param_store),
  15949. __ATTR(pt_touch_offload, 0644,
  15950. pt_touch_offload_show, pt_touch_offload_store),
  15951. #ifdef EASYWAKE_TSG6
  15952. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15953. pt_easy_wakeup_gesture_store),
  15954. __ATTR(easy_wakeup_gesture_id, 0444,
  15955. pt_easy_wakeup_gesture_id_show, NULL),
  15956. __ATTR(easy_wakeup_gesture_data, 0444,
  15957. pt_easy_wakeup_gesture_data_show, NULL),
  15958. #endif
  15959. #ifdef TTDL_DIAGNOSTICS
  15960. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15961. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15962. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15963. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15964. #endif /* TTDL_DIAGNOSTICS */
  15965. };
  15966. /*******************************************************************************
  15967. * FUNCTION: add_sysfs_interfaces
  15968. *
  15969. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15970. *
  15971. * RETURN:
  15972. * 0 = success
  15973. * !0 = failure
  15974. *
  15975. * PARAMETERS:
  15976. * *dev - pointer to device structure
  15977. ******************************************************************************/
  15978. static int add_sysfs_interfaces(struct device *dev)
  15979. {
  15980. int i;
  15981. int j = 0;
  15982. struct pt_core_data *cd = dev_get_drvdata(dev);
  15983. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15984. if (device_create_file(dev, attributes + i))
  15985. goto undo;
  15986. }
  15987. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15988. __func__, cd->active_dut_generation);
  15989. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15990. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15991. if (device_create_file(dev, pip2_attributes + j))
  15992. goto undo;
  15993. }
  15994. }
  15995. return 0;
  15996. undo:
  15997. for (i--; i >= 0; i--)
  15998. device_remove_file(dev, attributes + i);
  15999. for (j--; j >= 0; j--)
  16000. device_remove_file(dev, pip2_attributes + j);
  16001. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  16002. __func__);
  16003. return -ENODEV;
  16004. }
  16005. /*******************************************************************************
  16006. * FUNCTION: remove_sysfs_interfaces
  16007. *
  16008. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  16009. *
  16010. * RETURN: void
  16011. *
  16012. * PARAMETERS:
  16013. * *dev - pointer to device structure
  16014. ******************************************************************************/
  16015. static void remove_sysfs_interfaces(struct device *dev)
  16016. {
  16017. int i;
  16018. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  16019. device_remove_file(dev, attributes + i);
  16020. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  16021. device_remove_file(dev, pip2_attributes + i);
  16022. }
  16023. /*******************************************************************************
  16024. * FUNCTION: remove_sysfs_and_modules
  16025. *
  16026. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  16027. *
  16028. * RETURN: void
  16029. *
  16030. * PARAMETERS:
  16031. * *dev - pointer to device structure
  16032. ******************************************************************************/
  16033. static void remove_sysfs_and_modules(struct device *dev)
  16034. {
  16035. struct pt_core_data *cd = dev_get_drvdata(dev);
  16036. /* Queued work should be removed before to release loader module */
  16037. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16038. pt_release_modules(cd);
  16039. pt_btn_release(dev);
  16040. pt_mt_release(dev);
  16041. remove_sysfs_interfaces(dev);
  16042. }
  16043. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  16044. {
  16045. int retval;
  16046. /* Get pinctrl if target uses pinctrl */
  16047. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  16048. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  16049. retval = PTR_ERR(cd->ts_pinctrl);
  16050. dev_dbg(cd->dev,
  16051. "Target does not use pinctrl %d\n", retval);
  16052. goto err_pinctrl_get;
  16053. }
  16054. cd->pinctrl_state_active
  16055. = pinctrl_lookup_state(cd->ts_pinctrl,
  16056. PINCTRL_STATE_ACTIVE);
  16057. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  16058. retval = PTR_ERR(cd->pinctrl_state_active);
  16059. dev_err(cd->dev,
  16060. "Can not lookup %s pinstate %d\n",
  16061. PINCTRL_STATE_ACTIVE, retval);
  16062. goto err_pinctrl_lookup;
  16063. }
  16064. cd->pinctrl_state_suspend
  16065. = pinctrl_lookup_state(cd->ts_pinctrl,
  16066. PINCTRL_STATE_SUSPEND);
  16067. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  16068. retval = PTR_ERR(cd->pinctrl_state_suspend);
  16069. dev_err(cd->dev,
  16070. "Can not lookup %s pinstate %d\n",
  16071. PINCTRL_STATE_SUSPEND, retval);
  16072. goto err_pinctrl_lookup;
  16073. }
  16074. cd->pinctrl_state_release
  16075. = pinctrl_lookup_state(cd->ts_pinctrl,
  16076. PINCTRL_STATE_RELEASE);
  16077. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  16078. retval = PTR_ERR(cd->pinctrl_state_release);
  16079. dev_dbg(cd->dev,
  16080. "Can not lookup %s pinstate %d\n",
  16081. PINCTRL_STATE_RELEASE, retval);
  16082. }
  16083. return 0;
  16084. err_pinctrl_lookup:
  16085. devm_pinctrl_put(cd->ts_pinctrl);
  16086. err_pinctrl_get:
  16087. cd->ts_pinctrl = NULL;
  16088. return retval;
  16089. }
  16090. void touch_notify_glink_pt_channel_state(bool state)
  16091. {
  16092. pr_info("%s:[touch] touch_notify_glink\n", __func__);
  16093. }
  16094. void glink_touch_pt_rx_msg(void *data, int len)
  16095. {
  16096. int rc = 0;
  16097. pr_info("%s: TOUCH_RX_MSG Start:\n", __func__);
  16098. if (len > TOUCH_GLINK_INTENT_SIZE) {
  16099. pr_err("Invalid TOUCH glink intent size\n");
  16100. return;
  16101. }
  16102. /* check SLATE response */
  16103. pt_slate_resp_ack = *(uint32_t *)&data[8];
  16104. if (pt_slate_resp_ack == 0x01) {
  16105. pr_err("Bad SLATE response\n");
  16106. rc = -EINVAL;
  16107. goto err_ret;
  16108. }
  16109. pr_info("%s: TOUCH_RX_MSG End:\n", __func__);
  16110. err_ret:
  16111. return;
  16112. }
  16113. /*******************************************************************************
  16114. *******************************************************************************
  16115. * FUNCTION: pt_probe
  16116. *
  16117. * SUMMARY: Probe of the core module.
  16118. *
  16119. * NOTE: For the Parade Technologies development platform (PtSBC) the
  16120. * probe functionality is split into two functions; pt_probe() and
  16121. * pt_probe_complete(). the initial setup is done in this function which
  16122. * then creates a WORK task which runs after the probe timer expires. This
  16123. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  16124. *
  16125. * RETURN:
  16126. * 0 = success
  16127. * !0 = failure
  16128. *
  16129. * PARAMETERS:
  16130. * *ops - pointer to the bus
  16131. * *dev - pointer to the device structure
  16132. * irq - IRQ
  16133. * xfer_buf_size - size of the buffer
  16134. ******************************************************************************/
  16135. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  16136. u16 irq, size_t xfer_buf_size)
  16137. {
  16138. struct pt_core_data *cd;
  16139. struct pt_platform_data *pdata = dev_get_platdata(dev);
  16140. enum pt_atten_type type;
  16141. struct i2c_client *client = to_i2c_client(dev);
  16142. int rc = 0;
  16143. u8 pip_ver_major;
  16144. u8 pip_ver_minor;
  16145. u32 status = STARTUP_STATUS_START;
  16146. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  16147. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  16148. __func__);
  16149. rc = -ENODEV;
  16150. goto error_no_pdata;
  16151. }
  16152. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  16153. if (!pdata->core_pdata->power) {
  16154. pt_debug(dev, DL_ERROR,
  16155. "%s: Missing platform data function\n",
  16156. __func__);
  16157. rc = -ENODEV;
  16158. goto error_no_pdata;
  16159. }
  16160. }
  16161. /* get context and debug print buffers */
  16162. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  16163. if (!cd) {
  16164. rc = -ENOMEM;
  16165. goto error_no_pdata;
  16166. }
  16167. /* Initialize device info */
  16168. cd->dev = dev;
  16169. cd->pdata = pdata;
  16170. cd->cpdata = pdata->core_pdata;
  16171. cd->bus_ops = ops;
  16172. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  16173. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  16174. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  16175. cd->hw_detected = false;
  16176. cd->pip2_prot_active = false;
  16177. cd->pip2_send_user_cmd = false;
  16178. cd->bl_pip_ver_ready = false;
  16179. cd->app_pip_ver_ready = false;
  16180. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  16181. cd->get_param_id = 0;
  16182. cd->watchdog_enabled = 0;
  16183. cd->startup_retry_count = 0;
  16184. cd->core_probe_complete = 0;
  16185. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  16186. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  16187. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  16188. cd->flashless_dut = 0;
  16189. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  16190. cd->bl_with_no_int = 0;
  16191. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  16192. cd->multi_chip = PT_FEATURE_DISABLE;
  16193. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  16194. cd->sleep_state = SS_SLEEP_NONE;
  16195. cd->quick_boot = false;
  16196. cd->drv_debug_suspend = false;
  16197. cd->touch_offload = false;
  16198. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  16199. cd->set_dut_generation = true;
  16200. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16201. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  16202. cd->set_dut_generation = true;
  16203. cd->active_dut_generation = DUT_PIP1_ONLY;
  16204. } else {
  16205. cd->set_dut_generation = false;
  16206. cd->active_dut_generation = DUT_UNKNOWN;
  16207. }
  16208. /* Initialize with platform data */
  16209. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  16210. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  16211. cd->hid_cmd_state = 1;
  16212. cd->fw_updating = false;
  16213. cd->multi_chip = 0;
  16214. #ifdef TTDL_DIAGNOSTICS
  16215. cd->t_refresh_active = 0;
  16216. cd->t_refresh_count = 0;
  16217. cd->pip2_crc_error_count = 0;
  16218. cd->wd_xres_count = 0;
  16219. cd->bl_retry_packet_count = 0;
  16220. cd->file_erase_timeout_count = 0;
  16221. cd->show_tt_data = false;
  16222. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  16223. cd->err_gpio = 0;
  16224. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  16225. cd->ttdl_bist_select = 0x07;
  16226. cd->force_pip2_seq = 0;
  16227. #endif /* TTDL_DIAGNOSTICS */
  16228. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  16229. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  16230. sizeof(PT_PIP2_BIN_FILE_PATH));
  16231. pt_init_hid_descriptor(&cd->hid_desc);
  16232. /* Read and store the descriptor lengths */
  16233. cd->hid_core.hid_report_desc_len =
  16234. le16_to_cpu(cd->hid_desc.report_desc_len);
  16235. cd->hid_core.hid_max_input_len =
  16236. le16_to_cpu(cd->hid_desc.max_input_len);
  16237. cd->hid_core.hid_max_output_len =
  16238. le16_to_cpu(cd->hid_desc.max_output_len);
  16239. /* Initialize mutexes and spinlocks */
  16240. mutex_init(&cd->module_list_lock);
  16241. mutex_init(&cd->system_lock);
  16242. mutex_init(&cd->sysfs_lock);
  16243. mutex_init(&cd->ttdl_restart_lock);
  16244. mutex_init(&cd->firmware_class_lock);
  16245. spin_lock_init(&cd->spinlock);
  16246. /* Initialize module list */
  16247. INIT_LIST_HEAD(&cd->module_list);
  16248. /* Initialize attention lists */
  16249. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  16250. INIT_LIST_HEAD(&cd->atten_list[type]);
  16251. /* Initialize parameter list */
  16252. INIT_LIST_HEAD(&cd->param_list);
  16253. /* Initialize wait queue */
  16254. init_waitqueue_head(&cd->wait_q);
  16255. rc = pt_ts_pinctrl_init(cd);
  16256. if (!rc && cd->ts_pinctrl) {
  16257. /*
  16258. * Pinctrl handle is optional. If pinctrl handle is found
  16259. * let pins to be configured in active state. If not
  16260. * found continue further without error.
  16261. */
  16262. rc = pinctrl_select_state(cd->ts_pinctrl,
  16263. cd->pinctrl_state_active);
  16264. if (rc < 0)
  16265. dev_err(&client->dev, "failed to select pin to active state\n");
  16266. }
  16267. rc = pt_get_regulator(cd, true);
  16268. if (rc) {
  16269. dev_err(&client->dev, "Failed to get voltage regulators\n");
  16270. goto error_alloc_data;
  16271. }
  16272. rc = pt_enable_regulator(cd, true);
  16273. if (rc) {
  16274. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  16275. goto error_get_regulator;
  16276. }
  16277. /* Initialize works */
  16278. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  16279. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  16280. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  16281. /* Initialize HID specific data */
  16282. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  16283. cd->cpdata->vendor_id : HID_VENDOR_ID;
  16284. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  16285. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  16286. cd->hid_core.hid_desc_register =
  16287. cpu_to_le16(cd->cpdata->hid_desc_register);
  16288. /* Set platform easywake value */
  16289. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  16290. #if defined(CONFIG_DRM) || defined(CONFIG_PANEL_NOTIFIER)
  16291. /* Setup active dsi panel */
  16292. active_panel = cd->cpdata->active_panel;
  16293. #endif
  16294. /* Set platform panel_id value */
  16295. cd->panel_id_support = cd->cpdata->panel_id_support;
  16296. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  16297. /* Set Panel ID to default to 0 */
  16298. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  16299. else
  16300. /* Set Panel ID to Not Enabled */
  16301. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  16302. /* Initialize hw_version default to FFFF.FFFF.FF */
  16303. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  16304. dev_set_drvdata(dev, cd);
  16305. /* PtSBC builds will call this function in pt_probe_complete() */
  16306. pt_add_core(dev);
  16307. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  16308. if (rc) {
  16309. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  16310. __func__);
  16311. goto error_enable_regulator;
  16312. }
  16313. /*
  16314. * Save the pointer to a global value, which will be used
  16315. * in ttdl_restart function
  16316. */
  16317. cd->bus_ops = ops;
  16318. glink_touch_channel_init(&touch_notify_glink_pt_channel_state, &glink_touch_pt_rx_msg);
  16319. /*
  16320. * When the IRQ GPIO is not direclty accessible and no function is
  16321. * defined to get the IRQ status, the IRQ passed in must be assigned
  16322. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16323. */
  16324. if (!cd->cpdata->irq_stat) {
  16325. cd->irq = irq;
  16326. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  16327. __func__, cd->irq);
  16328. }
  16329. /* Call platform init function before setting up the GPIO's */
  16330. if (cd->cpdata->init) {
  16331. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16332. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16333. } else {
  16334. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16335. __func__);
  16336. rc = 0;
  16337. }
  16338. if (rc < 0) {
  16339. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16340. __func__, rc);
  16341. }
  16342. /* Power on any needed regulator(s) */
  16343. if (cd->cpdata->setup_power) {
  16344. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16345. rc = cd->cpdata->setup_power(cd->cpdata,
  16346. PT_MT_POWER_ON, cd->dev);
  16347. } else {
  16348. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16349. __func__);
  16350. rc = 0;
  16351. }
  16352. if (rc < 0)
  16353. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16354. __func__, rc);
  16355. #ifdef TTDL_DIAGNOSTICS
  16356. cd->watchdog_irq_stuck_count = 0;
  16357. cd->bus_transmit_error_count = 0;
  16358. #endif /* TTDL_DIAGNOSTICS */
  16359. if (cd->cpdata->detect) {
  16360. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16361. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16362. pt_platform_detect_read);
  16363. if (!rc) {
  16364. cd->hw_detected = true;
  16365. pt_debug(cd->dev, DL_INFO,
  16366. "%s: HW detected\n", __func__);
  16367. } else {
  16368. cd->hw_detected = false;
  16369. pt_debug(cd->dev, DL_INFO,
  16370. "%s: No HW detected\n", __func__);
  16371. rc = -ENODEV;
  16372. goto error_detect;
  16373. }
  16374. } else {
  16375. pt_debug(dev, DL_ERROR,
  16376. "%s: PARADE No HW detect function pointer\n",
  16377. __func__);
  16378. /*
  16379. * "hw_reset" is not needed in the "if" statement,
  16380. * because "hw_reset" is already included in "hw_detect"
  16381. * function.
  16382. */
  16383. rc = pt_hw_hard_reset(cd);
  16384. if (rc)
  16385. pt_debug(cd->dev, DL_ERROR,
  16386. "%s: FAILED to execute HARD reset\n",
  16387. __func__);
  16388. }
  16389. if (cd->cpdata->setup_irq) {
  16390. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16391. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16392. if (rc) {
  16393. pt_debug(dev, DL_ERROR,
  16394. "%s: Error, couldn't setup IRQ\n", __func__);
  16395. goto error_setup_irq;
  16396. }
  16397. } else {
  16398. pt_debug(dev, DL_ERROR,
  16399. "%s: IRQ function pointer not setup\n",
  16400. __func__);
  16401. goto error_setup_irq;
  16402. }
  16403. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16404. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16405. (unsigned long)cd);
  16406. #else
  16407. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16408. #endif
  16409. pt_stop_wd_timer(cd);
  16410. #ifdef TTHE_TUNER_SUPPORT
  16411. mutex_init(&cd->tthe_lock);
  16412. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16413. 0644, NULL, cd, &tthe_debugfs_fops);
  16414. #endif
  16415. rc = device_init_wakeup(dev, 1);
  16416. if (rc < 0)
  16417. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16418. __func__, rc);
  16419. if (!enable_irq_wake(cd->irq)) {
  16420. cd->irq_wake = 1;
  16421. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16422. }
  16423. pm_runtime_get_noresume(dev);
  16424. pm_runtime_set_active(dev);
  16425. pm_runtime_enable(dev);
  16426. /* If IRQ asserted, read out all from buffer to release INT pin */
  16427. if (cd->cpdata->irq_stat) {
  16428. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16429. } else {
  16430. /* Force a read in case the reset sentinel already arrived */
  16431. rc = pt_read_input(cd);
  16432. if (!rc)
  16433. pt_parse_input(cd);
  16434. }
  16435. /* Without sleep DUT is not ready and will NAK the first write */
  16436. msleep(150);
  16437. /* Attempt to set the DUT generation if not yet set */
  16438. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16439. if (cd->bl_pip_ver_ready ||
  16440. (cd->app_pip_ver_ready &&
  16441. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16442. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16443. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16444. __func__, cd->active_dut_generation);
  16445. } else {
  16446. rc = _pt_detect_dut_generation(cd->dev,
  16447. &status, &cd->active_dut_generation,
  16448. &cd->mode);
  16449. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16450. || rc) {
  16451. pt_debug(cd->dev, DL_ERROR,
  16452. " === DUT Gen unknown, Skip Enum ===\n");
  16453. goto skip_enum;
  16454. }
  16455. }
  16456. }
  16457. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16458. &pip_ver_major, &pip_ver_minor);
  16459. if (pip_ver_major == 2) {
  16460. cd->bl_pip_ver_ready = true;
  16461. pt_debug(dev, DL_ERROR,
  16462. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16463. pip_ver_minor);
  16464. cd->hw_detected = true;
  16465. } else if (pip_ver_major == 1) {
  16466. cd->app_pip_ver_ready = true;
  16467. pt_debug(dev, DL_ERROR,
  16468. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16469. cd->hw_detected = true;
  16470. } else {
  16471. cd->sysinfo.ttdata.pip_ver_major = 0;
  16472. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16473. cd->app_pip_ver_ready = false;
  16474. cd->hw_detected = false;
  16475. pt_debug(dev, DL_ERROR,
  16476. " === PIP Version Not Detected, Skip Enum ===\n");
  16477. /* For legacy DUTS proceed, enum will attempt to launch app */
  16478. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16479. goto skip_enum;
  16480. }
  16481. rc = pt_enum_with_dut(cd, false, &status);
  16482. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16483. __func__, cd->startup_status, status);
  16484. if (rc == -ENODEV) {
  16485. pt_debug(cd->dev, DL_ERROR,
  16486. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16487. /* For PtSBC don't error out, allow TTDL to stay up */
  16488. rc = -EPROBE_DEFER;
  16489. goto error_after_startup;
  16490. }
  16491. /* Suspend scanning until probe is complete to avoid asyc touches */
  16492. pt_pip_suspend_scanning_(cd);
  16493. if (cd->hw_detected) {
  16494. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16495. __func__);
  16496. rc = add_sysfs_interfaces(dev);
  16497. if (rc < 0) {
  16498. pt_debug(dev, DL_ERROR,
  16499. "%s: Error, fail sysfs init\n", __func__);
  16500. goto error_after_startup;
  16501. }
  16502. } else {
  16503. pt_debug(dev, DL_ERROR,
  16504. "%s: No HW detected, sysfs interfaces not added\n",
  16505. __func__);
  16506. }
  16507. skip_enum:
  16508. pm_runtime_put_sync(dev);
  16509. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16510. rc = pt_mt_probe(dev);
  16511. if (rc < 0) {
  16512. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16513. __func__);
  16514. goto error_after_sysfs_create;
  16515. }
  16516. rc = pt_btn_probe(dev);
  16517. if (rc < 0) {
  16518. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16519. __func__);
  16520. goto error_after_startup_mt;
  16521. }
  16522. pt_probe_modules(cd);
  16523. #ifdef CONFIG_HAS_EARLYSUSPEND
  16524. pt_setup_early_suspend(cd);
  16525. #elif defined(CONFIG_PANEL_NOTIFIER)
  16526. pt_debug(dev, DL_ERROR, "%s: Probe: Setup Panel Event notifier\n", __func__);
  16527. pt_setup_panel_event_notifier(cd);
  16528. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16529. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16530. #elif defined(CONFIG_DRM)
  16531. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16532. pt_setup_drm_notifier(cd);
  16533. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16534. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16535. #elif defined(CONFIG_FB)
  16536. pt_setup_fb_notifier(cd);
  16537. #endif
  16538. #ifdef NEED_SUSPEND_NOTIFIER
  16539. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16540. register_pm_notifier(&cd->pm_notifier);
  16541. #endif
  16542. pt_pip_resume_scanning_(cd);
  16543. mutex_lock(&cd->system_lock);
  16544. cd->startup_status |= status;
  16545. cd->core_probe_complete = 1;
  16546. mutex_unlock(&cd->system_lock);
  16547. pt_core_state = STATE_RESUME;
  16548. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16549. __func__);
  16550. return 0;
  16551. error_after_startup_mt:
  16552. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16553. pt_mt_release(dev);
  16554. error_after_sysfs_create:
  16555. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16556. pm_runtime_disable(dev);
  16557. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16558. device_wakeup_disable(dev);
  16559. #endif
  16560. device_init_wakeup(dev, 0);
  16561. cancel_work_sync(&cd->enum_work);
  16562. pt_stop_wd_timer(cd);
  16563. pt_free_si_ptrs(cd);
  16564. remove_sysfs_interfaces(dev);
  16565. error_after_startup:
  16566. pr_err("%s PARADE error_after_startup\n", __func__);
  16567. del_timer(&cd->watchdog_timer);
  16568. if (cd->cpdata->setup_irq)
  16569. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16570. error_setup_irq:
  16571. error_detect:
  16572. if (cd->cpdata->init)
  16573. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16574. if (cd->cpdata->setup_power)
  16575. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16576. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16577. error_enable_regulator:
  16578. pt_del_core(dev);
  16579. dev_set_drvdata(dev, NULL);
  16580. pt_enable_regulator(cd, false);
  16581. error_get_regulator:
  16582. pt_get_regulator(cd, false);
  16583. error_alloc_data:
  16584. kfree(cd);
  16585. error_no_pdata:
  16586. pr_err("%s failed.\n", __func__);
  16587. return rc;
  16588. }
  16589. /*******************************************************************************
  16590. * FUNCTION: pt_device_entry
  16591. *
  16592. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  16593. * woke up or put to sleep based on Kernel power state even when the display
  16594. * is off based on the check of TTDL core platform flag.
  16595. *
  16596. * RETURN:
  16597. * 0 = success
  16598. * !0 = failure
  16599. *
  16600. * PARAMETERS:
  16601. * *dev - pointer to core device
  16602. ******************************************************************************/
  16603. #ifdef PT_AMBIENT_MODE
  16604. int pt_device_entry(struct device *dev,
  16605. u16 irq, size_t xfer_buf_size)
  16606. {
  16607. struct pt_core_data *cd = dev_get_drvdata(dev);
  16608. struct pt_platform_data *pdata = dev_get_platdata(dev);
  16609. struct i2c_client *client = to_i2c_client(dev);
  16610. int rc = 0;
  16611. void *glink_pt_send_msg;
  16612. int glink_touch_exit = TOUCH_EXIT;
  16613. pt_debug(dev, DL_INFO, "%s: Start pt_device_entry\n", __func__);
  16614. cd->dev = dev;
  16615. cd->pdata = pdata;
  16616. cd->cpdata = pdata->core_pdata;
  16617. glink_pt_send_msg = &glink_touch_exit;
  16618. pt_debug(dev, DL_INFO, "[touch]glink_pt_send_msg = %d\n", glink_pt_send_msg);
  16619. glink_touch_tx_msg(glink_pt_send_msg, TOUCH_MSG_SIZE);
  16620. msleep(150);
  16621. if (!rc && cd->ts_pinctrl) {
  16622. /*
  16623. * Pinctrl handle is optional. If pinctrl handle is found
  16624. * let pins to be configured in active state. If not
  16625. * found continue further without error.
  16626. */
  16627. rc = pinctrl_select_state(cd->ts_pinctrl, cd->pinctrl_state_active);
  16628. if (rc < 0)
  16629. dev_err(&client->dev, "failed to select pin to active state\n");
  16630. }
  16631. /* Set platform easywake value */
  16632. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  16633. /*
  16634. * When the IRQ GPIO is not direclty accessible and no function is
  16635. * defined to get the IRQ status, the IRQ passed in must be assigned
  16636. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16637. */
  16638. if (!cd->cpdata->irq_stat) {
  16639. cd->irq = irq;
  16640. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n", __func__, cd->irq);
  16641. }
  16642. /* Call platform init function before setting up the GPIO's */
  16643. if (cd->cpdata->init) {
  16644. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16645. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16646. } else {
  16647. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n", __func__);
  16648. rc = 0;
  16649. }
  16650. if (rc < 0) {
  16651. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n", __func__, rc);
  16652. }
  16653. /* Power on any needed regulator(s) */
  16654. if (cd->cpdata->setup_power) {
  16655. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16656. rc = cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16657. } else {
  16658. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n", __func__);
  16659. rc = 0;
  16660. }
  16661. if (rc < 0)
  16662. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n", __func__, rc);
  16663. if (cd->cpdata->detect) {
  16664. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16665. rc = cd->cpdata->detect(cd->cpdata, cd->dev, pt_platform_detect_read);
  16666. if (!rc) {
  16667. cd->hw_detected = true;
  16668. pt_debug(cd->dev, DL_INFO, "%s: HW detected\n", __func__);
  16669. } else {
  16670. cd->hw_detected = false;
  16671. pt_debug(cd->dev, DL_INFO, "%s: No HW detected\n", __func__);
  16672. rc = -ENODEV;
  16673. goto pt_error_detect;
  16674. }
  16675. } else {
  16676. pt_debug(dev, DL_ERROR, "%s: PARADE No HW detect function pointer\n", __func__);
  16677. /*
  16678. * "hw_reset" is not needed in the "if" statement,
  16679. * because "hw_reset" is already included in "hw_detect"
  16680. * function.
  16681. */
  16682. rc = pt_hw_hard_reset(cd);
  16683. if (rc)
  16684. pt_debug(cd->dev, DL_ERROR, "%s: FAILED to execute HARD reset\n", __func__);
  16685. }
  16686. if (cd->cpdata->setup_irq) {
  16687. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16688. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16689. if (rc) {
  16690. pt_debug(dev, DL_ERROR, "%s: Error, couldn't setup IRQ\n", __func__);
  16691. goto pt_error_setup_irq;
  16692. }
  16693. } else {
  16694. pt_debug(dev, DL_ERROR, "%s: IRQ function pointer not setup\n", __func__);
  16695. goto pt_error_setup_irq;
  16696. }
  16697. rc = device_init_wakeup(dev, 1);
  16698. if (rc < 0)
  16699. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n", __func__, rc);
  16700. if (!enable_irq_wake(cd->irq)) {
  16701. cd->irq_wake = 1;
  16702. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16703. }
  16704. pm_runtime_get_noresume(dev);
  16705. pm_runtime_set_active(dev);
  16706. pm_runtime_enable(dev);
  16707. /* Without sleep DUT is not ready and will NAK the first write */
  16708. msleep(150);
  16709. pm_runtime_put_sync(dev);
  16710. #if defined(CONFIG_PANEL_NOTIFIER)
  16711. /* Setup active dsi panel */
  16712. active_panel = cd->cpdata->active_panel;
  16713. pt_debug(dev, DL_ERROR, "%s: Probe: Setup Panel Event notifier\n", __func__);
  16714. pt_setup_panel_event_notifier(cd);
  16715. #endif
  16716. mutex_lock(&cd->system_lock);
  16717. cd->core_probe_complete = 1;
  16718. mutex_unlock(&cd->system_lock);
  16719. pt_debug(dev, DL_INFO, "%s: ####TTDL Core Device Probe Completed Successfully\n", __func__);
  16720. pt_core_state = STATE_RESUME;
  16721. return 0;
  16722. pt_error_setup_irq:
  16723. device_init_wakeup(dev, 0);
  16724. pt_error_detect:
  16725. if (cd->cpdata->init)
  16726. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16727. if (cd->cpdata->setup_power)
  16728. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16729. return rc;
  16730. }
  16731. #endif
  16732. EXPORT_SYMBOL_GPL(pt_probe);
  16733. /*******************************************************************************
  16734. * FUNCTION: pt_release
  16735. *
  16736. * SUMMARY: This function does the following cleanup:
  16737. * - Releases all probed modules
  16738. * - Stops the watchdog
  16739. * - Cancels all pending work tasks
  16740. * - Removes all created sysfs nodes
  16741. * - Removes all created debugfs nodes
  16742. * - Frees the IRQ
  16743. * - Deletes the core
  16744. * - Frees all pointers and HID reports
  16745. *
  16746. * RETURN:
  16747. * 0 = success
  16748. * !0 = failure
  16749. *
  16750. * PARAMETERS:
  16751. * *cd - pointer to the core data structure
  16752. ******************************************************************************/
  16753. int pt_release(struct pt_core_data *cd)
  16754. {
  16755. struct device *dev = cd->dev;
  16756. /*
  16757. * Suspend the device before freeing the startup_work and stopping
  16758. * the watchdog since sleep function restarts watchdog on failure
  16759. */
  16760. pm_runtime_suspend(dev);
  16761. pm_runtime_disable(dev);
  16762. /*
  16763. * Any 'work' that can trigger a new thread should be canceled first.
  16764. * The watchdog is also stopped again because another thread could have
  16765. * restarted it. The irq_work is cancelled last because it is used for
  16766. * all I2C/SPI communication.
  16767. */
  16768. pt_stop_wd_timer(cd);
  16769. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16770. cancel_work_sync(&cd->ttdl_restart_work);
  16771. cancel_work_sync(&cd->enum_work);
  16772. cancel_work_sync(&cd->resume_offload_work);
  16773. cancel_work_sync(&cd->suspend_offload_work);
  16774. cancel_work_sync(&cd->resume_work);
  16775. cancel_work_sync(&cd->suspend_work);
  16776. destroy_workqueue(cd->pt_workqueue);
  16777. pt_stop_wd_timer(cd);
  16778. pt_release_modules(cd);
  16779. pt_proximity_release(dev);
  16780. pt_btn_release(dev);
  16781. pt_mt_release(dev);
  16782. #ifdef CONFIG_HAS_EARLYSUSPEND
  16783. unregister_early_suspend(&cd->es);
  16784. #elif defined(CONFIG_PANEL_NOTIFIER)
  16785. if (active_panel)
  16786. panel_event_notifier_unregister(cd->entry);
  16787. #elif defined(CONFIG_DRM)
  16788. if (active_panel)
  16789. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16790. #elif defined(CONFIG_FB)
  16791. fb_unregister_client(&cd->fb_notifier);
  16792. #endif
  16793. #ifdef NEED_SUSPEND_NOTIFIER
  16794. unregister_pm_notifier(&cd->pm_notifier);
  16795. #endif
  16796. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16797. device_wakeup_disable(dev);
  16798. #endif
  16799. device_init_wakeup(dev, 0);
  16800. #ifdef TTHE_TUNER_SUPPORT
  16801. mutex_lock(&cd->tthe_lock);
  16802. cd->tthe_exit = 1;
  16803. wake_up(&cd->wait_q);
  16804. mutex_unlock(&cd->tthe_lock);
  16805. debugfs_remove(cd->tthe_debugfs);
  16806. #endif
  16807. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16808. remove_sysfs_interfaces(dev);
  16809. disable_irq_nosync(cd->irq);
  16810. if (cd->cpdata->setup_irq)
  16811. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16812. if (cd->cpdata->init)
  16813. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16814. if (cd->cpdata->setup_power)
  16815. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16816. dev_set_drvdata(dev, NULL);
  16817. pt_del_core(dev);
  16818. if (cd->vcc_i2c)
  16819. regulator_set_load(cd->vcc_i2c, 0);
  16820. if (cd->vdd)
  16821. regulator_set_load(cd->vdd, 0);
  16822. pt_enable_regulator(cd, false);
  16823. pt_get_regulator(cd, false);
  16824. pt_free_si_ptrs(cd);
  16825. kfree(cd);
  16826. return 0;
  16827. }
  16828. EXPORT_SYMBOL_GPL(pt_release);
  16829. MODULE_LICENSE("GPL");
  16830. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16831. MODULE_AUTHOR("Parade Technologies <[email protected]>");